Depth testing switches off when I set the view port

Hi, I've been studying Metal using the Apple docs and the wonderful RW tutorials.


I've managed to get some primitives on screen just fine but the problem comes with depth testing.

If I use


func mtkView(_ view: MTKView, drawableSizeWillChange size: CGSize) {
         metalViewPort.originX = 0
         metalViewPort.originY = 0
         metalViewPort.height = Double(size.height)
         metalViewPort.width = Double(size.width)
     }


and during the draw loop:


//Setup code
commandEncoder.setViewport(aMetalView)
//draw stuff


The depth testing passes everything. Simply commenting out the setViewport on line 2 above makes it operate correctly and models obscure, move and rotate exactly as expected in 3D space.


Before I post reams of poorly written code, can someone tell me if this is generally expected behaviour?

Replies

Are you setting Znear/Zfar on the viewport properly? These are used to translate depth values before depth testing, if memory serves. Wrong Znear/Zfar can probably make all depths seem equal to test, or some such. Just a quick guess...

Hi Mike,


It's being set with:


storedPMatrix = matrix_float4x4(projectionFov: radians(fromDegrees: 65), aspect: viewWidth/viewHieght, nearZ: 0.1, farZ: 100)


and this is declared in a library I got from the RW tutorials site:


init(projectionFov fov: Float, aspect: Float, nearZ: Float, farZ: Float) {
    let y = 1 / tan(fov * 0.5)
    let x = y / aspect
    let z = farZ / (nearZ - farZ)
    columns = (
      float4( x,  0,  0,  0),
      float4( 0,  y,  0,  0),
      float4( 0,  0,  z, -1),
      float4( 0,  0,  z * nearZ,  0)
    )
  }