diff --git a/src/core/renderer_mtl/renderer_mtl.cpp b/src/core/renderer_mtl/renderer_mtl.cpp index e449cc87..b55f2de8 100644 --- a/src/core/renderer_mtl/renderer_mtl.cpp +++ b/src/core/renderer_mtl/renderer_mtl.cpp @@ -369,6 +369,16 @@ void RendererMTL::drawVertices(PICA::PrimType primType, std::spanformat, DepthFmt::Unknown1}; if (depthStencilRenderTarget) { @@ -436,6 +446,7 @@ void RendererMTL::drawVertices(PICA::PrimType primType, std::spansetVertexBytes(®s[0x48], (0x200 - 0x48) * sizeof(regs[0]), 0); renderCommandEncoder->setFragmentBytes(®s[0x48], (0x200 - 0x48) * sizeof(regs[0]), 0); + renderCommandEncoder->setVertexBytes(&depthUniforms, sizeof(depthUniforms), 2); renderCommandEncoder->setFragmentBytes(&logicOp, sizeof(logicOp), 2); renderCommandEncoder->drawPrimitives(toMTLPrimitiveType(primType), NS::UInteger(0), NS::UInteger(vertices.size())); diff --git a/src/host_shaders/metal_shaders.metal b/src/host_shaders/metal_shaders.metal index ef794abb..8fb02b7d 100644 --- a/src/host_shaders/metal_shaders.metal +++ b/src/host_shaders/metal_shaders.metal @@ -143,17 +143,31 @@ float decodeFP(uint hex, uint E, uint M) { return as_type(hex); } -vertex DrawVertexOutWithClip vertexDraw(DrawVertexIn in [[stage_in]], constant PicaRegs& picaRegs [[buffer(0)]], constant VertTEV& tev [[buffer(1)]]) { +struct DepthUniforms { + float depthScale; + float depthOffset; + bool depthMapEnable; +}; + +vertex DrawVertexOutWithClip vertexDraw(DrawVertexIn in [[stage_in]], constant PicaRegs& picaRegs [[buffer(0)]], constant VertTEV& tev [[buffer(1)]], constant DepthUniforms& depthUniforms [[buffer(2)]]) { DrawVertexOut out; // Position out.position = in.position; // Flip the y position out.position.y = -out.position.y; - // in.position.z is in range of [-1 ... 1], convert it to [0 ... 1] out.position.xyz /= out.position.w; + + // Apply depth uniforms + out.position.z = out.position.z * depthUniforms.depthScale + depthUniforms.depthOffset; + // TODO: is this correct? + if (!depthUniforms.depthMapEnable) { // Divide z by w if depthmap enable == 0 (ie using W-buffering) + out.position.z /= out.position.w; + } + + // in.position.z is in range of [-1, 1], convert it to [0, 1] + out.position.z = (out.position.z + 1.0) * 0.5; out.position.w = 1.0; - out.position.z = (-out.position.z + 1.0) * 0.5; // Color out.color = min(abs(in.color), 1.0); @@ -560,8 +574,6 @@ fragment float4 fragmentDraw(DrawVertexOut in [[stage_in]], float4 prevColor [[c } } - // TODO: depth - float4 color = performLogicOp(logicOp, globals.tevSources[15], prevColor); // Perform alpha test