dugla
dugla

Reputation: 12954

Metal. Why does setting MTLCullMode to none turn off depth comparison?

I an rendering a simple box:

MDLMesh(boxWithExtent: ...)

In my draw loop when I turn off back-face culling:

renderCommandEncoder.setCullMode(.none)

All depth comparison is disabled and sides of the box are drawn completely wrong with back-facing quads in front of front-facing.

Huh?

My intent is to include back-facing surfaces in the depth comparison not ignore them. This is important for when I have, for example, a shape with semi-transparent textures that reveal the shape's internals which have a different shading style. How to I force depth comparison?

UPDATE

So Warren's suggestion is an improvement but it is still not correct.

My depthStencilDescriptor:

let depthStencilDescriptor = MTLDepthStencilDescriptor()
depthStencilDescriptor.depthCompareFunction = .less
depthStencilDescriptor.isDepthWriteEnabled = true

depthStencilState = device.makeDepthStencilState(descriptor: depthStencilDescriptor)

Within my draw loop I set depth stencil state:

renderCommandEncoder.setDepthStencilState(depthStencilState)

The resultant rendering enter image description here

Description. This is a box mesh. Each box face uses a shader the paints a disk texture. The texture is transparent outside the body of the disk. The shader paints a red/white spiral texture on front-facings quads and a blue/black spiral texture on back-facing quads. The box sits in front of a camera aligned quad textured with a mobil image.

Notice how one of the textures paints over the rear back-facing quad with the background texture color. Notice also that the rear-most back-facing quad is not drawn at all.

Upvotes: 2

Views: 1104

Answers (1)

dugla
dugla

Reputation: 12954

Actually it is not possible to achieve the effect I am after. I basically want to do a simple composite - Porter/Duff - here but that is order dependent. Order cannot be guaranteed here so I am basically hosed.

Upvotes: 1

Related Questions