1#if defined IGRAPHICS_NANOVG && defined IGRAPHICS_METAL
3#include "TestCustomShaderControl.h"
8TestCustomShaderControl::~TestCustomShaderControl()
13void TestCustomShaderControl::CleanUp()
16 nvgDeleteFramebuffer(mFBO);
18 if (mRenderPassDescriptor)
20 MTLRenderPassDescriptor* rpd = (MTLRenderPassDescriptor*) mRenderPassDescriptor;
22 mRenderPassDescriptor =
nullptr;
28 id<MTLRenderPipelineState> renderPipeline = (id<MTLRenderPipelineState>) mRenderPipeline;
29 [renderPipeline release];
30 mRenderPipeline =
nullptr;
48 invalidateFBO =
false;
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));
56 auto rpd = (MTLRenderPassDescriptor*) mRenderPassDescriptor;
59 rpd = [MTLRenderPassDescriptor
new];
61 rpd.colorAttachments[0].texture = dstTex;
63 rpd.colorAttachments[0].loadAction = MTLLoadActionClear;
64 rpd.colorAttachments[0].clearColor = MTLClearColorMake(0, 0, 0, 0);
66 rpd.colorAttachments[0].storeAction = MTLStoreActionStore;
68 id<MTLLibrary> defaultLibrary = [dev newDefaultLibrary];
70 MTLRenderPipelineDescriptor* psd = [[MTLRenderPipelineDescriptor alloc] init];
71 psd.label =
@"Offscreen Render Pipeline";
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];
78 [defaultLibrary release];
79 mRenderPassDescriptor = (
void*) rpd;
84 auto commandQueue =
static_cast<id<MTLCommandQueue>
>(mnvgCommandQueue(pCtx));
86 id<MTLCommandBuffer> commandBuffer = [commandQueue commandBuffer];
88 commandBuffer.label =
@"Command Buffer";
91 static const SimpleVertex triVertices[] =
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 } },
99 id<MTLRenderCommandEncoder> renderEncoder = [commandBuffer renderCommandEncoderWithDescriptor:(MTLRenderPassDescriptor*) mRenderPassDescriptor];
101 renderEncoder.label =
@"Offscreen Render Pass";
102 [renderEncoder setRenderPipelineState:(id<MTLRenderPipelineState>) mRenderPipeline];
104 [renderEncoder setVertexBytes:&triVertices
105 length:
sizeof(triVertices)
106 atIndex:VertexInputIndexVertices];
108 [renderEncoder drawPrimitives:MTLPrimitiveTypeTriangle
113 [renderEncoder endEncoding];
116 [commandBuffer commit];
117 [commandBuffer waitUntilCompleted];
120 APIBitmap apibmp {mFBO->image, int(w), int(h), 1, 1.};
121 IBitmap bmp {&apibmp, 1,
false};
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.
The lowest level base class of an IGraphics context.
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.
float GetTotalScale() const
Gets the combined draw and screen/display scaling factor.
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.
virtual void FillRect(const IColor &color, const IRECT &bounds, const IBlend *pBlend=0)
Fill a rectangular region of the graphics context with a color.
void Draw(IGraphics &g) override
Draw the control to the graphics context.