From e688bb2cdf9bfa8f73c762f500d6ab2ee3c36e92 Mon Sep 17 00:00:00 2001 From: "Unknown W. Brackets" Date: Sun, 24 Oct 2021 12:27:47 -0700 Subject: [PATCH] GPU: Correct software transform projection. Now reading the new fog value. --- GPU/Common/SoftwareTransformCommon.cpp | 10 ++++------ GPU/Common/VertexShaderGenerator.cpp | 16 ++++++++++++---- GPU/GLES/ShaderManagerGLES.cpp | 5 ++++- 3 files changed, 20 insertions(+), 11 deletions(-) diff --git a/GPU/Common/SoftwareTransformCommon.cpp b/GPU/Common/SoftwareTransformCommon.cpp index dfff93ac7d..2480224cb6 100644 --- a/GPU/Common/SoftwareTransformCommon.cpp +++ b/GPU/Common/SoftwareTransformCommon.cpp @@ -213,6 +213,7 @@ void SoftwareTransform::Decode(int prim, u32 vertType, const DecVtxFormat &decVt // TODO: Write to a flexible buffer, we don't always need all four components. TransformedVertex &vert = transformed[index]; reader.ReadPos(vert.pos); + vert.posw = 1.0f; if (reader.hasColor0()) { if (provokeIndOffset != 0 && index + provokeIndOffset < maxIndex) { @@ -429,12 +430,9 @@ void SoftwareTransform::Decode(int prim, u32 vertType, const DecVtxFormat &decVt fogCoef = (v[2] + fog_end) * fog_slope; // TODO: Write to a flexible buffer, we don't always need all four components. - Vec4f projected; - Vec3ByMatrix44(projected.AsArray(), v, projMatrix_.m); - Vec3f viewportPos = projected.xyz() / projected.w; - memcpy(&transformed[index].x, viewportPos.AsArray(), 3 * sizeof(float)); - transformed[index].posw = fogCoef; - memcpy(&transformed[index].u, uv, 3 * sizeof(float)); + Vec3ByMatrix44(transformed[index].pos, v, projMatrix_.m); + transformed[index].fog = fogCoef; + memcpy(&transformed[index].uv, uv, 3 * sizeof(float)); transformed[index].color0_32 = c0.ToRGBA(); transformed[index].color1_32 = c1.ToRGBA(); diff --git a/GPU/Common/VertexShaderGenerator.cpp b/GPU/Common/VertexShaderGenerator.cpp index 6ed7c2a5ca..419539ed27 100644 --- a/GPU/Common/VertexShaderGenerator.cpp +++ b/GPU/Common/VertexShaderGenerator.cpp @@ -236,11 +236,12 @@ bool GenerateVertexShader(const VShaderID &id, char *buffer, const ShaderLanguag if (useHWTransform) WRITE(p, "layout (location = %d) in vec3 position;\n", (int)PspAttributeLocation::POSITION); else - // we pass the fog coord in w WRITE(p, "layout (location = %d) in vec4 position;\n", (int)PspAttributeLocation::POSITION); if (useHWTransform && hasNormal) WRITE(p, "layout (location = %d) in vec3 normal;\n", (int)PspAttributeLocation::NORMAL); + if (!useHWTransform && enableFog) + WRITE(p, "layout (location = %d) in float fog;\n", (int)PspAttributeLocation::NORMAL); if (doTexture && hasTexcoord) { if (!useHWTransform && doTextureTransform && !isModeThrough) { @@ -386,6 +387,9 @@ bool GenerateVertexShader(const VShaderID &id, char *buffer, const ShaderLanguag if (lmode) { WRITE(p, " vec3 color1 : COLOR1;\n"); } + if (enableFog) { + WRITE(p, " float fog : NORMAL;\n"); + } WRITE(p, "};\n"); } @@ -435,6 +439,10 @@ bool GenerateVertexShader(const VShaderID &id, char *buffer, const ShaderLanguag WRITE(p, "%s mediump vec3 normal;\n", compat.attribute); *attrMask |= 1 << ATTR_NORMAL; } + if (!useHWTransform && enableFog) { + WRITE(p, "%s highp float fog;\n", compat.attribute); + *attrMask |= 1 << ATTR_NORMAL; + } if (doTexture && hasTexcoord) { if (!useHWTransform && doTextureTransform && !isModeThrough) { @@ -789,7 +797,7 @@ bool GenerateVertexShader(const VShaderID &id, char *buffer, const ShaderLanguag WRITE(p, " %sv_color1 = splat3(0.0);\n", compat.vsOutPrefix); } if (enableFog) { - WRITE(p, " %sv_fogdepth = position.w;\n", compat.vsOutPrefix); + WRITE(p, " %sv_fogdepth = fog;\n", compat.vsOutPrefix); } if (isModeThrough) { WRITE(p, " vec4 outPos = mul(u_proj_through, vec4(position.xyz, 1.0));\n"); @@ -804,9 +812,9 @@ bool GenerateVertexShader(const VShaderID &id, char *buffer, const ShaderLanguag // The viewport is used in this case, so need to compensate for that. if (gstate_c.Supports(GPU_ROUND_DEPTH_TO_16BIT)) { - WRITE(p, " vec4 outPos = depthRoundZVP(mul(displayRotation, vec4(position.xyz, 1.0)));\n"); + WRITE(p, " vec4 outPos = depthRoundZVP(mul(displayRotation, position));\n"); } else { - WRITE(p, " vec4 outPos = mul(displayRotation, vec4(position.xyz, 1.0));\n"); + WRITE(p, " vec4 outPos = mul(displayRotation, position);\n"); } } } else { diff --git a/GPU/GLES/ShaderManagerGLES.cpp b/GPU/GLES/ShaderManagerGLES.cpp index 7392f7da10..7d59ee150f 100644 --- a/GPU/GLES/ShaderManagerGLES.cpp +++ b/GPU/GLES/ShaderManagerGLES.cpp @@ -85,7 +85,10 @@ LinkedShader::LinkedShader(GLRenderManager *render, VShaderID VSID, Shader *vs, std::vector semantics; semantics.push_back({ ATTR_POSITION, "position" }); semantics.push_back({ ATTR_TEXCOORD, "texcoord" }); - semantics.push_back({ ATTR_NORMAL, "normal" }); + if (useHWTransform_) + semantics.push_back({ ATTR_NORMAL, "normal" }); + else + semantics.push_back({ ATTR_NORMAL, "fog" }); semantics.push_back({ ATTR_W1, "w1" }); semantics.push_back({ ATTR_W2, "w2" }); semantics.push_back({ ATTR_COLOR0, "color0" });