diff --git a/modules/javafx.graphics/src/main/native-prism-mtl/MetalContext.h b/modules/javafx.graphics/src/main/native-prism-mtl/MetalContext.h index ae2ef46f22..bb817a210d 100644 --- a/modules/javafx.graphics/src/main/native-prism-mtl/MetalContext.h +++ b/modules/javafx.graphics/src/main/native-prism-mtl/MetalContext.h @@ -144,7 +144,6 @@ typedef enum VertexInputIndex { - (id) getPhongPipelineState; - (NSUInteger) getCurrentBufferIndex; -- (void) resetRenderPass; - (void) updateDepthDetails:(bool)depthTest; - (void) verifyDepthTexture; @@ -199,6 +198,7 @@ typedef enum VertexInputIndex { y:(float)y z:(float)z; - (vector_float4) getCameraPosition; - (MTLScissorRect) getScissorRect; +- (bool) clearDepth; - (bool) isDepthEnabled; - (bool) isScissorEnabled; - (bool) isCurrentRTT:(MetalRTTexture*)rttPtr; diff --git a/modules/javafx.graphics/src/main/native-prism-mtl/MetalContext.m b/modules/javafx.graphics/src/main/native-prism-mtl/MetalContext.m index 4e735abadc..9353949eba 100644 --- a/modules/javafx.graphics/src/main/native-prism-mtl/MetalContext.m +++ b/modules/javafx.graphics/src/main/native-prism-mtl/MetalContext.m @@ -429,6 +429,11 @@ - (NSInteger) drawIndexedQuads:(struct PrismSourceVertex const *)pSrcXYZUVs [renderEncoder setRenderPipelineState:[shader getPipelineState:[rtt isMSAAEnabled] compositeMode:compositeMode]]; + if (depthEnabled) { + id depthStencilState = + [[self getPipelineManager] getDepthStencilState]; + [renderEncoder setDepthStencilState:depthStencilState]; + } if ([shader getArgumentBufferLength] != 0) { [shader copyArgBufferToRingBuffer]; @@ -534,14 +539,6 @@ - (void) setWorldTransformIdentityMatrix worldMatrix = matrix_identity_float4x4; } -- (void) resetRenderPass -{ - CTX_LOG(@"MetalContext.resetRenderPass()"); - if (depthEnabled) { - rttPassDesc.depthAttachment.loadAction = MTLLoadActionLoad; - } -} - - (void) clearRTT:(float)red green:(float)green blue:(float)blue @@ -551,7 +548,27 @@ - (void) clearRTT:(float)red CTX_LOG(@">>>> MetalContext.clearRTT() %f, %f, %f, %f", red, green, blue, alpha); CTX_LOG(@">>>> MetalContext.clearRTT() %d", clearDepth); - clearDepthTexture = clearDepth; + clearDepthTexture = false; + if (clearDepth && + [rtt getDepthTexture] != nil) { + CTX_LOG(@" MetalContext.clearRTT() clearing depth attachment"); + clearDepthTexture = true; + rttPassDesc.depthAttachment.clearDepth = 1.0; + rttPassDesc.depthAttachment.loadAction = MTLLoadActionClear; + if ([[self getRTT] isMSAAEnabled]) { + CTX_LOG(@"MetalContext.clearRTT() MSAA"); + rttPassDesc.depthAttachment.storeAction = MTLStoreActionStoreAndMultisampleResolve; + rttPassDesc.depthAttachment.texture = [rtt getDepthMSAATexture]; + rttPassDesc.depthAttachment.resolveTexture = [rtt getDepthTexture]; + } else { + CTX_LOG(@"MetalContext.clearRTT() non-MSAA"); + rttPassDesc.depthAttachment.storeAction = MTLStoreActionStore; + rttPassDesc.depthAttachment.texture = [[self getRTT] getDepthTexture]; + rttPassDesc.depthAttachment.resolveTexture = nil; + } + } else { + rttPassDesc.depthAttachment = nil; + } clearColor[0] = red; clearColor[1] = green; clearColor[2] = blue; @@ -560,6 +577,11 @@ - (void) clearRTT:(float)red id renderEncoder = [self getCurrentRenderEncoder]; [renderEncoder setRenderPipelineState:[pipelineManager getClearRttPipeState]]; + if (clearDepthTexture) { + id depthStencilState = + [[self getPipelineManager] getDepthStencilState]; + [renderEncoder setDepthStencilState:depthStencilState]; + } [renderEncoder setFrontFacingWinding:MTLWindingClockwise]; [renderEncoder setCullMode:MTLCullModeNone]; [renderEncoder setTriangleFillMode:MTLTriangleFillModeFill]; @@ -590,7 +612,7 @@ - (void) clearRTT:(float)red indexBuffer:indexBuffer indexBufferOffset:0]; - if (clearDepthTexture && !depthEnabled) { + if (clearDepth && !depthEnabled) { [self endCurrentRenderEncoder]; } @@ -727,14 +749,6 @@ - (NSInteger) setDeviceParametersFor2D - (NSInteger) setDeviceParametersFor3D { CTX_LOG(@"MetalContext_setDeviceParametersFor3D()"); - if (clearDepthTexture) { - CTX_LOG(@"MetalContext_setDeviceParametersFor3D clearDepthTexture is true"); - rttPassDesc.depthAttachment.clearDepth = 1.0; - rttPassDesc.depthAttachment.loadAction = MTLLoadActionClear; - } else { - rttPassDesc.depthAttachment.loadAction = MTLLoadActionLoad; - } - // TODO: MTL: Check whether we need to do shader initialization here /*if (!phongShader) { phongShader = ([[MetalPhongShader alloc] createPhongShader:self]); @@ -772,6 +786,8 @@ - (void) verifyDepthTexture [rtt createDepthTexture]; rttPassDesc.depthAttachment.clearDepth = 1.0; rttPassDesc.depthAttachment.loadAction = MTLLoadActionClear; + } else { + rttPassDesc.depthAttachment.loadAction = MTLLoadActionLoad; } } @@ -827,6 +843,11 @@ - (MTLScissorRect) getScissorRect return scissorRect; } +- (bool) clearDepth +{ + return clearDepthTexture; +} + - (bool) isDepthEnabled { return depthEnabled; diff --git a/modules/javafx.graphics/src/main/native-prism-mtl/MetalMeshView.m b/modules/javafx.graphics/src/main/native-prism-mtl/MetalMeshView.m index 274d25f4ab..d2747fc9f7 100644 --- a/modules/javafx.graphics/src/main/native-prism-mtl/MetalMeshView.m +++ b/modules/javafx.graphics/src/main/native-prism-mtl/MetalMeshView.m @@ -258,7 +258,6 @@ - (void) render indexType:[mesh getIndexType] indexBuffer:[mesh getIndexBuffer] indexBufferOffset:0]; - [context resetRenderPass]; } - (void) release diff --git a/modules/javafx.graphics/src/main/native-prism-mtl/MetalPipelineManager.m b/modules/javafx.graphics/src/main/native-prism-mtl/MetalPipelineManager.m index 531a84e27d..c01decb53b 100644 --- a/modules/javafx.graphics/src/main/native-prism-mtl/MetalPipelineManager.m +++ b/modules/javafx.graphics/src/main/native-prism-mtl/MetalPipelineManager.m @@ -85,7 +85,7 @@ - (void) init:(MetalContext*) ctx libData:(dispatch_data_t) libData depthStencilDescriptor.depthWriteEnabled = NO; depthStencilState[0] = [[context getDevice] newDepthStencilStateWithDescriptor:depthStencilDescriptor]; - depthStencilDescriptor.depthCompareFunction = MTLCompareFunctionLess; + depthStencilDescriptor.depthCompareFunction = MTLCompareFunctionLessEqual; depthStencilDescriptor.depthWriteEnabled = YES; depthStencilState[1] = [[context getDevice] newDepthStencilStateWithDescriptor:depthStencilDescriptor]; } @@ -129,7 +129,7 @@ - (void) init:(MetalContext*) ctx libData:(dispatch_data_t) libData } NSNumber *keySampleCount = [NSNumber numberWithInt:sampleCount]; id clearRttPipeState; - if ([context isDepthEnabled]) { + if ([context clearDepth]) { clearRttPipeState = clearRttPipeStateDepthDict[keySampleCount]; } else { clearRttPipeState = clearRttPipeStateNoDepthDict[keySampleCount]; @@ -140,7 +140,7 @@ - (void) init:(MetalContext*) ctx libData:(dispatch_data_t) libData pipeDesc.fragmentFunction = [self getFunction:@"clearFF"]; pipeDesc.colorAttachments[0].pixelFormat = MTLPixelFormatBGRA8Unorm; //[[context getRTT] getPixelFormat]; //rtt.pixelFormat pipeDesc.sampleCount = sampleCount; - if ([context isDepthEnabled]) { + if ([context clearDepth]) { pipeDesc.depthAttachmentPixelFormat = MTLPixelFormatDepth32Float; } else { pipeDesc.depthAttachmentPixelFormat = MTLPixelFormatInvalid; @@ -151,7 +151,7 @@ - (void) init:(MetalContext*) ctx libData:(dispatch_data_t) libData [pipeDesc release]; pipeDesc = nil; NSAssert(clearRttPipeState, @"Failed to create clear pipeline state: %@", error); - if ([context isDepthEnabled]) { + if ([context clearDepth]) { [clearRttPipeStateDepthDict setObject:clearRttPipeState forKey:keySampleCount]; } else { [clearRttPipeStateNoDepthDict setObject:clearRttPipeState forKey:keySampleCount];