iPlug2 - C++ Audio Plug-in Framework
TestMPSControl.mm
1 #if defined IGRAPHICS_NANOVG && defined IGRAPHICS_METAL
2 
3 #include "TestMPSControl.h"
4 #include "nanovg_mtl.h"
5 #import <MetalPerformanceShaders/MetalPerformanceShaders.h>
6 
7 // https://developer.apple.com/documentation/metalperformanceshaders?language=objc
8 
10 {
11  if (@available(macOS 10.13, *))
12  {
13  NVGcontext* pCtx = static_cast<NVGcontext*>(g.GetDrawContext());
14 
15  if(!mFBO) {
16  mFBO = nvgCreateFramebuffer(pCtx, mBitmap.W() * mBitmap.GetScale(), mBitmap.H() * mBitmap.GetScale(), 0);
17  }
18 
19  id<MTLDevice> dev = static_cast<id<MTLDevice>>(mnvgDevice(pCtx));
20  id<MTLCommandQueue> commandQueue = static_cast<id<MTLCommandQueue>>(mnvgCommandQueue(pCtx));
21  id<MTLTexture> srcTex = static_cast<id<MTLTexture>>(mnvgImageHandle(pCtx, mBitmap.GetAPIBitmap()->GetBitmap()));
22  id<MTLTexture> dstTex = static_cast<id<MTLTexture>>(mnvgImageHandle(pCtx, mFBO->image));
23  id<MTLCommandBuffer> commandBuffer = [commandQueue commandBuffer];
24 
25  MPSUnaryImageKernel* pKernel = nullptr;
26 
27  switch (mKernelType) {
28  case 0: pKernel = [[MPSImageGaussianBlur alloc] initWithDevice:dev sigma:GetValue() * 10.]; break;
29  case 1: pKernel = [[MPSImageSobel alloc] initWithDevice:dev]; break;
30  case 2: pKernel = [[MPSImageThresholdToZero alloc] initWithDevice:dev thresholdValue:GetValue() linearGrayColorTransform:nil]; break;
31  default: break;
32  }
33 
34  [pKernel encodeToCommandBuffer:commandBuffer sourceTexture:srcTex destinationTexture:dstTex];
35  [commandBuffer commit];
36  [commandBuffer waitUntilCompleted];
37 
38  [pKernel release];
39 
40  APIBitmap apibmp {mFBO->image, int(mBitmap.W() * mBitmap.GetScale()), int(mBitmap.H() * mBitmap.GetScale()), 1, 1.};
41  IBitmap bmp {&apibmp, 1, false};
42 
43  g.DrawFittedBitmap(bmp, mRECT);
44  } // macOS 10.13
45 }
46 
47 #endif // IGRAPHICS_METAL
User-facing bitmap abstraction that you use to manage bitmap data, independant of draw class/platform...
The lowest level base class of an IGraphics context.
Definition: IGraphics.h:86
virtual void DrawFittedBitmap(const IBitmap &bitmap, const IRECT &bounds, const IBlend *pBlend=0)
Draw a bitmap (raster) image to the graphics context, scaling the image to fit the bounds...
Definition: IGraphics.cpp:2752
A base class interface for a bitmap abstraction around the different drawing back end bitmap represen...
virtual void * GetDrawContext()=0
Gets a void pointer to underlying drawing context, for the IGraphics backend See draw class implement...
double GetValue(int valIdx=0) const
Get the control&#39;s value.
Definition: IControl.cpp:151
void Draw(IGraphics &g) override
Draw the control to the graphics context.