implement depth uniforms

This commit is contained in:
Samuliak 2024-07-04 11:34:54 +02:00
parent e8727d5dbd
commit 13439699ee
2 changed files with 28 additions and 5 deletions

View file

@ -369,6 +369,16 @@ void RendererMTL::drawVertices(PICA::PrimType primType, std::span<const PICA::Ve
}
}
// Depth uniforms
struct {
float depthScale;
float depthOffset;
bool depthMapEnable;
} depthUniforms;
depthUniforms.depthScale = Floats::f24::fromRaw(regs[PICA::InternalRegs::DepthScale] & 0xffffff).toFloat32();
depthUniforms.depthOffset = Floats::f24::fromRaw(regs[PICA::InternalRegs::DepthOffset] & 0xffffff).toFloat32();
depthUniforms.depthMapEnable = regs[PICA::InternalRegs::DepthmapEnable] & 1;
// -------- Pipeline --------
Metal::PipelineHash pipelineHash{colorRenderTarget->format, DepthFmt::Unknown1};
if (depthStencilRenderTarget) {
@ -436,6 +446,7 @@ void RendererMTL::drawVertices(PICA::PrimType primType, std::span<const PICA::Ve
bindTexturesToSlots(renderCommandEncoder);
renderCommandEncoder->setVertexBytes(&regs[0x48], (0x200 - 0x48) * sizeof(regs[0]), 0);
renderCommandEncoder->setFragmentBytes(&regs[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()));

View file

@ -143,17 +143,31 @@ float decodeFP(uint hex, uint E, uint M) {
return as_type<float>(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