iPlug2 - C++ Audio Plug-in Framework
Loading...
Searching...
No Matches
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 auto* 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 auto dev = static_cast<id<MTLDevice>>(mnvgDevice(pCtx));
20 auto commandQueue = static_cast<id<MTLCommandQueue>>(mnvgCommandQueue(pCtx));
21 auto srcTex = static_cast<id<MTLTexture>>(mnvgImageHandle(pCtx, mBitmap.GetAPIBitmap()->GetBitmap()));
22 auto 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
A base class interface for a bitmap abstraction around the different drawing back end bitmap represen...
User-facing bitmap abstraction that you use to manage bitmap data, independant of draw class/platform...
double GetValue(int valIdx=0) const
Get the control's value.
Definition: IControl.cpp:153
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:2774
virtual void * GetDrawContext()=0
Gets a void pointer to underlying drawing context, for the IGraphics backend See draw class implement...
void Draw(IGraphics &g) override
Draw the control to the graphics context.