Improvements to -1 to 1 depth mode.

- Transformation is only applied on the last stage in the vertex pipeline.
- Should fix some issues with geometry and tessellation (hopefully)
- Reading back FragCoord Z on fragment will transform back to -1 to 1.
This commit is contained in:
riperiperi 2022-05-04 21:39:28 +01:00
parent daaa7fda11
commit 8097480b02
3 changed files with 33 additions and 4 deletions

View file

@ -259,7 +259,15 @@ namespace Ryujinx.Graphics.Shader.CodeGen.Glsl
{
case AttributeConsts.PositionX: return $"(gl_FragCoord.x / {DefaultNames.SupportBlockRenderScaleName}[0])";
case AttributeConsts.PositionY: return $"(gl_FragCoord.y / {DefaultNames.SupportBlockRenderScaleName}[0])";
case AttributeConsts.PositionZ: return "gl_FragCoord.z";
case AttributeConsts.PositionZ:
if (config.Options.TargetApi == TargetApi.Vulkan && config.GpuAccessor.QueryTransformDepthMinusOneToOne())
{
return "((gl_FragCoord.z - (gl_FragCoord.w * 0.5)) * 2.0)";
}
else
{
return "gl_FragCoord.z";
}
case AttributeConsts.PositionW: return "gl_FragCoord.w";
case AttributeConsts.FrontFacing:

View file

@ -339,6 +339,20 @@ namespace Ryujinx.Graphics.Shader.CodeGen.Spirv
value = FDiv(TypeFP32(), value, scale);
}
else if (Config.Options.TargetApi == TargetApi.Vulkan &&
attr == AttributeConsts.PositionZ &&
Config.GpuAccessor.QueryTransformDepthMinusOneToOne())
{
var constTwo = Constant(TypeFP32(), 2.0f);
var constZeroPointFive = Constant(TypeFP32(), 0.5f);
var elemPointerW = GetAttributeElemPointer(AttributeConsts.PositionW, isOutAttr, null, out var elemTypeW);
var valueW = Load(GetType(elemTypeW), elemPointerW);
var halfW = FMul(TypeFP32(), valueW, constZeroPointFive);
value = FMul(TypeFP32(), FSub(TypeFP32(), value, halfW), constTwo);
}
else if (attr == AttributeConsts.FrontFacing && Config.GpuAccessor.QueryHostHasFrontFacingBug())
{
// Workaround for what appears to be a bug on Intel compiler.

View file

@ -182,7 +182,7 @@ namespace Ryujinx.Graphics.Shader.Translation
this.Copy(Attribute(AttributeConsts.PositionY), this.FPFusedMultiplyAdd(y, yScale, negativeOne));
}
if (Config.GpuAccessor.QueryTransformDepthMinusOneToOne())
if (Config.Options.TargetApi == TargetApi.Vulkan && Config.GpuAccessor.QueryTransformDepthMinusOneToOne())
{
Operand z = Attribute(AttributeConsts.PositionZ | AttributeConsts.LoadOutputMask);
Operand w = Attribute(AttributeConsts.PositionW | AttributeConsts.LoadOutputMask);
@ -207,7 +207,7 @@ namespace Ryujinx.Graphics.Shader.Translation
oldYLocal = null;
}
if (Config.GpuAccessor.QueryTransformDepthMinusOneToOne())
if (Config.Options.TargetApi == TargetApi.Vulkan && Config.GpuAccessor.QueryTransformDepthMinusOneToOne())
{
oldZLocal = Local();
this.Copy(oldYLocal, Attribute(AttributeConsts.PositionZ | AttributeConsts.LoadOutputMask));
@ -281,8 +281,15 @@ namespace Ryujinx.Graphics.Shader.Translation
Operand src = Register(Config.GetDepthRegister(), RegisterType.Gpr);
if (Config.Options.TargetApi == TargetApi.Vulkan && Config.GpuAccessor.QueryTransformDepthMinusOneToOne())
{
this.Copy(dest, this.FPFusedMultiplyAdd(src, ConstF(0.5f), ConstF(0.5f)));
}
else
{
this.Copy(dest, src);
}
}
AlphaTestOp alphaTestOp = Config.GpuAccessor.QueryAlphaTestCompare();