Metal depth stencil not working

1

I have a MetalKit project on MacOS where everything works as expected except for the depth stencil.

Because I could only find examples of the depth stencil for iOS I'm following those. The snippet below is being called immediately after the render pipeline was created but the return value from newDepthStencilStateWithDescriptor seems to be invalid (the device and label are both null).

It's possible I setup the MTKView wrong but I can't be sure because there are no examples of the depth stencil using MTKView and MacOS.

If anyone can explain how this could happen, how I could debug it or if they have example code I could compare with that would be great.

desc := MTLDepthStencilDescriptor.alloc.init.autorelease;
desc.setDepthCompareFunction(MTLCompareFunctionLess);
desc.setDepthWriteEnabled(true);
desc.setLabel(NSSTR('MY DEPTH STENCIL'));

depthStencilState := device.newDepthStencilStateWithDescriptor(desc);

<MTLDepthStencilDescriptorInternal: 0x1003153a0>
    label = MY DEPTH STENCIL 
    depthCompareFunction = MTLCompareFunctionLess 
    depthWriteEnabled = 1 
    frontFace: 
        stencilCompareFunction        = MTLCompareFunctionAlways 
        stencilFailOperation          = MTLStencilOperationKeep 
        stencilPassDepthFailOperation = MTLStencilOperationKeep 
        stencilPassDepthPassOperation = MTLStencilOperationKeep 
        stencilReadMask               = 0xffffffff 
        stencilWriteMask              = 0xffffffff 
    backFace: 
        stencilCompareFunction        = MTLCompareFunctionAlways 
        stencilFailOperation          = MTLStencilOperationKeep 
        stencilPassDepthFailOperation = MTLStencilOperationKeep 
        stencilPassDepthPassOperation = MTLStencilOperationKeep 
        stencilReadMask               = 0xffffffff 
        stencilWriteMask              = 0xffffffff
<MTLIGDepthStencilState: 0x1003241d0>
    label = <none> 
    device = <null>

Note the depth stencil not working and fragments drawing in the wrong order.

enter image description here

macos
metal
metalkit
asked on Stack Overflow Jul 2, 2018 by GenericPtr

1 Answer

0

The reason I was getting inconsistent output is because in the Xcode projects I tested Metal Validation was enabled but in my project it was not. If I enable validation the values are consistent.

With validation enabled the same code returns:

<MTLDebugDepthStencilState: 0x100758d40> -> <MTLIGDepthStencilState: 0x1007582a0>
    label = <none> 
    device = <null><MTLDepthStencilDescriptorInternal: 0x100758e20>
        label = MY DEPTH STENCIL 
        depthCompareFunction = MTLCompareFunctionLess 
        depthWriteEnabled = 1 
        frontFace: <MTLStencilDescriptorInternal: 0x100758f60>
            stencilCompareFunction        = MTLCompareFunctionAlways 
            stencilFailureOperation       = MTLStencilOperationKeep 
            stencilPassDepthFailOperation = MTLStencilOperationKeep 
            stencilPassDepthPassOperation = MTLStencilOperationKeep 
            stencilReadMask               = 0xffffffff 
            stencilWriteMask              = 0xffffffff 
        backFace: <MTLStencilDescriptorInternal: 0x100758f90>
            stencilCompareFunction        = MTLCompareFunctionAlways 
            stencilFailureOperation       = MTLStencilOperationKeep 
            stencilPassDepthFailOperation = MTLStencilOperationKeep 
            stencilPassDepthPassOperation = MTLStencilOperationKeep 
            stencilReadMask               = 0xffffffff 
            stencilWriteMask              = 0xffffffff

So the whole problem was a red herring after all. The depth stencil still doesn't work though so the problem may be in my shader or vertex data.

answered on Stack Overflow Jul 3, 2018 by GenericPtr

User contributions licensed under CC BY-SA 3.0