iPlug2 - C++ Audio Plug-in Framework
Loading...
Searching...
No Matches
TestCustomShaderControl.mm
1#if defined IGRAPHICS_NANOVG && defined IGRAPHICS_METAL
2
3#include "TestCustomShaderControl.h"
4#include "nanovg_mtl.h"
5#import <Metal/Metal.h>
6#import "ShaderTypes.h"
7
8TestCustomShaderControl::~TestCustomShaderControl()
9{
10 CleanUp();
11}
12
13void TestCustomShaderControl::CleanUp()
14{
15 if (mFBO)
16 nvgDeleteFramebuffer(mFBO);
17
18 if (mRenderPassDescriptor)
19 {
20 MTLRenderPassDescriptor* rpd = (MTLRenderPassDescriptor*) mRenderPassDescriptor;
21 [rpd release];
22 mRenderPassDescriptor = nullptr;
23 }
24
25
26 if (mRenderPipeline)
27 {
28 id<MTLRenderPipelineState> renderPipeline = (id<MTLRenderPipelineState>) mRenderPipeline;
29 [renderPipeline release];
30 mRenderPipeline = nullptr;
31 }
32}
33
35{
36 g.DrawDottedRect(COLOR_BLACK, mRECT);
37 g.FillRect(mMouseIsOver ? COLOR_TRANSLUCENT : COLOR_TRANSPARENT, mRECT);
38
39 auto* pCtx = static_cast<NVGcontext*>(g.GetDrawContext());
40
41 auto w = mRECT.W() * g.GetTotalScale();
42 auto h = mRECT.H() * g.GetTotalScale();
43
44 if (invalidateFBO)
45 {
46 CleanUp();
47
48 invalidateFBO = false;
49
50 NSError *error;
51
52 mFBO = nvgCreateFramebuffer(pCtx, w, h, 0);
53 auto dev = static_cast<id<MTLDevice>>(mnvgDevice(pCtx));
54 auto dstTex = static_cast<id<MTLTexture>>(mnvgImageHandle(pCtx, mFBO->image));
55
56 auto rpd = (MTLRenderPassDescriptor*) mRenderPassDescriptor;
57
58 // Set up a render pass descriptor for the render pass to render into
59 rpd = [MTLRenderPassDescriptor new];
60
61 rpd.colorAttachments[0].texture = dstTex;
62
63 rpd.colorAttachments[0].loadAction = MTLLoadActionClear;
64 rpd.colorAttachments[0].clearColor = MTLClearColorMake(0, 0, 0, 0);
65
66 rpd.colorAttachments[0].storeAction = MTLStoreActionStore;
67
68 id<MTLLibrary> defaultLibrary = [dev newDefaultLibrary];
69
70 MTLRenderPipelineDescriptor* psd = [[MTLRenderPipelineDescriptor alloc] init];
71 psd.label = @"Offscreen Render Pipeline";
72 psd.sampleCount = 1;
73 psd.vertexFunction = [defaultLibrary newFunctionWithName:@"simpleVertexShader"];
74 psd.fragmentFunction = [defaultLibrary newFunctionWithName:@"simpleFragmentShader"];
75 psd.colorAttachments[0].pixelFormat = dstTex.pixelFormat;
76 mRenderPipeline = [dev newRenderPipelineStateWithDescriptor:psd error:&error];
77 [psd release];
78 [defaultLibrary release];
79 mRenderPassDescriptor = (void*) rpd;
80 }
81
82 @autoreleasepool {
83
84 auto commandQueue = static_cast<id<MTLCommandQueue>>(mnvgCommandQueue(pCtx));
85
86 id<MTLCommandBuffer> commandBuffer = [commandQueue commandBuffer];
87
88 commandBuffer.label = @"Command Buffer";
89
90 {
91 static const SimpleVertex triVertices[] =
92 {
93 // Positions , Colors
94 { { 0.5, -0.5 }, { 1.0, 0.0, 0.0, 1.0 } },
95 { { -0.5, -0.5 }, { 0.0, 1.0, 0.0, 1.0 } },
96 { { 0.0, 0.5 }, { 0.0, 0.0, 1.0, 1.0 } },
97 };
98
99 id<MTLRenderCommandEncoder> renderEncoder = [commandBuffer renderCommandEncoderWithDescriptor:(MTLRenderPassDescriptor*) mRenderPassDescriptor];
100
101 renderEncoder.label = @"Offscreen Render Pass";
102 [renderEncoder setRenderPipelineState:(id<MTLRenderPipelineState>) mRenderPipeline];
103
104 [renderEncoder setVertexBytes:&triVertices
105 length:sizeof(triVertices)
106 atIndex:VertexInputIndexVertices];
107
108 [renderEncoder drawPrimitives:MTLPrimitiveTypeTriangle
109 vertexStart:0
110 vertexCount:3];
111
112 // End encoding commands for this render pass.
113 [renderEncoder endEncoding];
114 }
115
116 [commandBuffer commit];
117 [commandBuffer waitUntilCompleted];
118
119
120 APIBitmap apibmp {mFBO->image, int(w), int(h), 1, 1.};
121 IBitmap bmp {&apibmp, 1, false};
122
123 g.DrawFittedBitmap(bmp, mRECT);
124
125 } // @autoreleasepool
126}
127
128#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...
bool mMouseIsOver
if mGraphics::mHandleMouseOver = true, this will be true when the mouse is over control.
Definition: IControl.h:560
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
float GetTotalScale() const
Gets the combined draw and screen/display scaling factor.
Definition: IGraphics.h:1113
virtual void * GetDrawContext()=0
Gets a void pointer to underlying drawing context, for the IGraphics backend See draw class implement...
virtual void DrawDottedRect(const IColor &color, const IRECT &bounds, const IBlend *pBlend=0, float thickness=1.f, float dashLen=2.f)
Draw a dotted rectangle to the graphics context.
Definition: IGraphics.cpp:2539
virtual void FillRect(const IColor &color, const IRECT &bounds, const IBlend *pBlend=0)
Fill a rectangular region of the graphics context with a color.
Definition: IGraphics.cpp:2569
void Draw(IGraphics &g) override
Draw the control to the graphics context.
float W() const
float H() const