Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 19 additions & 7 deletions src/nanovg_mtl.m
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,7 @@ @interface MNVGcontext : NSObject
@property (nonatomic, strong) id<MTLFunction> fragmentFunction;
@property (nonatomic, strong) id<MTLFunction> vertexFunction;
@property MTLPixelFormat piplelinePixelFormat;
@property NSUInteger pipelineSampleCount;
@property (nonatomic, strong) id<MTLRenderPipelineState> pipelineState;
@property (nonatomic, strong) id<MTLRenderPipelineState>
stencilOnlyPipelineState;
Expand Down Expand Up @@ -253,7 +254,8 @@ - (void)stroke:(MNVGcall*)call;
- (void)triangles:(MNVGcall*)call;

- (void)updateRenderPipelineStatesForBlend:(MNVGblend*)blend
pixelFormat:(MTLPixelFormat)pixelFormat;
pixelFormat:(MTLPixelFormat)pixelFormat
sampleCount:(NSUInteger)sampleCount;

@end

Expand Down Expand Up @@ -1441,19 +1443,20 @@ - (void)renderFlush {
(uint)colorTexture.height};
}
if (textureSize.x == 0 || textureSize.y == 0) return;
[self updateStencilTextureToSize:&textureSize];

id<CAMetalDrawable> drawable = nil;
if (colorTexture == nil) {
drawable = _metalLayer.nextDrawable;
colorTexture = drawable.texture;
}
[self updateStencilTextureToSize:&textureSize sampleCount:colorTexture.sampleCount];
_renderEncoder = [self renderCommandEncoderWithColorTexture:colorTexture];
MNVGcall* call = &buffers->calls[0];
for (int i = buffers->ncalls; i--; ++call) {
MNVGblend* blend = &call->blendFunc;
[self updateRenderPipelineStatesForBlend:blend
pixelFormat:colorTexture.pixelFormat];
pixelFormat:colorTexture.pixelFormat
sampleCount:colorTexture.sampleCount];

if (call->type == MNVG_FILL)
[self fill:call];
Expand All @@ -1474,7 +1477,7 @@ - (void)renderFlush {

#if TARGET_OS_OSX
// Makes mnvgReadPixels() work as expected on Mac.
if (s_framebuffer != NULL) {
if (s_framebuffer != NULL && colorTexture.storageMode == MTLResourceStorageModeManaged) {
id<MTLBlitCommandEncoder> blitCommandEncoder = [_buffers->commandBuffer
blitCommandEncoder];
[blitCommandEncoder synchronizeResource:colorTexture];
Expand Down Expand Up @@ -1758,10 +1761,12 @@ - (void)triangles:(MNVGcall*)call {
}

- (void)updateRenderPipelineStatesForBlend:(MNVGblend*)blend
pixelFormat:(MTLPixelFormat)pixelFormat {
pixelFormat:(MTLPixelFormat)pixelFormat
sampleCount:(NSUInteger)sampleCount {
if (_pipelineState != nil &&
_stencilOnlyPipelineState != nil &&
_piplelinePixelFormat == pixelFormat &&
_pipelineSampleCount == sampleCount &&
_blendFunc->srcRGB == blend->srcRGB &&
_blendFunc->dstRGB == blend->dstRGB &&
_blendFunc->srcAlpha == blend->srcAlpha &&
Expand All @@ -1775,6 +1780,7 @@ - (void)updateRenderPipelineStatesForBlend:(MNVGblend*)blend
MTLRenderPipelineColorAttachmentDescriptor* colorAttachmentDescriptor = \
pipelineStateDescriptor.colorAttachments[0];
colorAttachmentDescriptor.pixelFormat = pixelFormat;
pipelineStateDescriptor.sampleCount = sampleCount;
pipelineStateDescriptor.stencilAttachmentPixelFormat = kStencilFormat;
pipelineStateDescriptor.fragmentFunction = _fragmentFunction;
pipelineStateDescriptor.vertexFunction = _vertexFunction;
Expand Down Expand Up @@ -1805,12 +1811,15 @@ - (void)updateRenderPipelineStatesForBlend:(MNVGblend*)blend
[self checkError:error withMessage:"init pipeline stencil only state"];

_piplelinePixelFormat = pixelFormat;
_pipelineSampleCount = sampleCount;
}

// Re-creates stencil texture whenever the specified size is bigger.
- (void)updateStencilTextureToSize:(vector_uint2*)size {
- (void)updateStencilTextureToSize:(vector_uint2*)size
sampleCount:(NSUInteger)sampleCount {
if (_buffers->stencilTexture != nil &&
(_buffers->stencilTexture.width < size->x ||
(_buffers->stencilTexture.sampleCount != sampleCount ||
_buffers->stencilTexture.width < size->x ||
_buffers->stencilTexture.height < size->y)) {
_buffers->stencilTexture = nil;
}
Expand All @@ -1820,6 +1829,9 @@ - (void)updateStencilTextureToSize:(vector_uint2*)size {
width:size->x
height:size->y
mipmapped:NO];
stencilTextureDescriptor.textureType = \
(sampleCount > 1) ? MTLTextureType2DMultisample : MTLTextureType2D;
stencilTextureDescriptor.sampleCount = sampleCount;
stencilTextureDescriptor.usage = MTLTextureUsageRenderTarget;
#if TARGET_OS_OSX || TARGET_OS_SIMULATOR
stencilTextureDescriptor.storageMode = MTLStorageModePrivate;
Expand Down