From 88134a6786b102dbb9b473fa42d748ae9c64af91 Mon Sep 17 00:00:00 2001 From: Pokechu22 Date: Tue, 28 Dec 2021 13:01:57 -0800 Subject: VertexShaderGen: Simplify normal calculation This is a readability change; there should be no functional or performance differences. --- Source/Core/VideoCommon/UberShaderVertex.cpp | 1 + 1 file changed, 1 insertion(+) (limited to 'Source/Core/VideoCommon/UberShaderVertex.cpp') diff --git a/Source/Core/VideoCommon/UberShaderVertex.cpp b/Source/Core/VideoCommon/UberShaderVertex.cpp index 68915351d1..b21798b6f9 100644 --- a/Source/Core/VideoCommon/UberShaderVertex.cpp +++ b/Source/Core/VideoCommon/UberShaderVertex.cpp @@ -153,6 +153,7 @@ ShaderCode GenVertexShader(APIType api_type, const ShaderHostConfig& host_config " N2 = " I_POSNORMALMATRIX "[5].xyz;\n" "}}\n" "\n" + "// Multiply the position vector by the position matrix\n" "float4 pos = float4(dot(P0, rawpos), dot(P1, rawpos), dot(P2, rawpos), 1.0);\n" "o.pos = float4(dot(" I_PROJECTION "[0], pos), dot(" I_PROJECTION "[1], pos), dot(" I_PROJECTION "[2], pos), dot(" I_PROJECTION "[3], pos));\n" -- cgit v1.2.3 From 04fdadd9d5f9588055fb54571410febfa710b86d Mon Sep 17 00:00:00 2001 From: Pokechu22 Date: Fri, 22 Apr 2022 12:50:44 -0700 Subject: VideoCommon: Rename norm0/norm1/norm2 to normal/tangent/binormal --- Source/Core/VideoCommon/UberShaderVertex.cpp | 71 ++++++++++++++-------------- 1 file changed, 36 insertions(+), 35 deletions(-) (limited to 'Source/Core/VideoCommon/UberShaderVertex.cpp') diff --git a/Source/Core/VideoCommon/UberShaderVertex.cpp b/Source/Core/VideoCommon/UberShaderVertex.cpp index b21798b6f9..91568a4cec 100644 --- a/Source/Core/VideoCommon/UberShaderVertex.cpp +++ b/Source/Core/VideoCommon/UberShaderVertex.cpp @@ -57,9 +57,9 @@ ShaderCode GenVertexShader(APIType api_type, const ShaderHostConfig& host_config { out.Write("ATTRIBUTE_LOCATION({}) in float4 rawpos;\n", SHADER_POSITION_ATTRIB); out.Write("ATTRIBUTE_LOCATION({}) in uint4 posmtx;\n", SHADER_POSMTX_ATTRIB); - out.Write("ATTRIBUTE_LOCATION({}) in float3 rawnorm0;\n", SHADER_NORM0_ATTRIB); - out.Write("ATTRIBUTE_LOCATION({}) in float3 rawnorm1;\n", SHADER_NORM1_ATTRIB); - out.Write("ATTRIBUTE_LOCATION({}) in float3 rawnorm2;\n", SHADER_NORM2_ATTRIB); + out.Write("ATTRIBUTE_LOCATION({}) in float3 rawnormal;\n", SHADER_NORMAL_ATTRIB); + out.Write("ATTRIBUTE_LOCATION({}) in float3 rawtangent;\n", SHADER_TANGENT_ATTRIB); + out.Write("ATTRIBUTE_LOCATION({}) in float3 rawbinormal;\n", SHADER_BINORMAL_ATTRIB); out.Write("ATTRIBUTE_LOCATION({}) in float4 rawcolor0;\n", SHADER_COLOR0_ATTRIB); out.Write("ATTRIBUTE_LOCATION({}) in float4 rawcolor1;\n", SHADER_COLOR1_ATTRIB); for (int i = 0; i < 8; ++i) @@ -106,9 +106,9 @@ ShaderCode GenVertexShader(APIType api_type, const ShaderHostConfig& host_config out.Write("VS_OUTPUT main(\n"); // inputs - out.Write(" float3 rawnorm0 : NORMAL0,\n" - " float3 rawnorm1 : NORMAL1,\n" - " float3 rawnorm2 : NORMAL2,\n" + out.Write(" float3 rawnormal : NORMAL,\n" + " float3 rawtangent : TANGENT,\n" + " float3 rawbinormal : BINORMAL,\n" " float4 rawcolor0 : COLOR0,\n" " float4 rawcolor1 : COLOR1,\n"); for (int i = 0; i < 8; ++i) @@ -131,7 +131,7 @@ ShaderCode GenVertexShader(APIType api_type, const ShaderHostConfig& host_config "float3 N1;\n" "float3 N2;\n" "\n" - "if ((components & {}u) != 0u) {{// VB_HAS_POSMTXIDX\n", + "if ((components & {}u) != 0u) {{ // VB_HAS_POSMTXIDX\n", VB_HAS_POSMTXIDX); out.Write(" // Vertex format has a per-vertex matrix\n" " int posidx = int(posmtx.r);\n" @@ -159,21 +159,22 @@ ShaderCode GenVertexShader(APIType api_type, const ShaderHostConfig& host_config "[1], pos), dot(" I_PROJECTION "[2], pos), dot(" I_PROJECTION "[3], pos));\n" "\n" "// Only the first normal gets normalized (TODO: why?)\n" - "float3 _norm0 = float3(0.0, 0.0, 0.0);\n" - "if ((components & {}u) != 0u) // VB_HAS_NRM0\n", - VB_HAS_NRM0); - out.Write( - " _norm0 = normalize(float3(dot(N0, rawnorm0), dot(N1, rawnorm0), dot(N2, rawnorm0)));\n" - "\n" - "float3 _norm1 = float3(0.0, 0.0, 0.0);\n" - "if ((components & {}u) != 0u) // VB_HAS_NRM1\n", - VB_HAS_NRM1); - out.Write(" _norm1 = float3(dot(N0, rawnorm1), dot(N1, rawnorm1), dot(N2, rawnorm1));\n" + "float3 _normal = float3(0.0, 0.0, 0.0);\n" + "if ((components & {}u) != 0u) // VB_HAS_NORMAL\n", + VB_HAS_NORMAL); + out.Write(" _normal = normalize(float3(dot(N0, rawnormal), dot(N1, rawnormal), dot(N2, " + "rawnormal)));\n" + "\n" + "float3 _tangent = float3(0.0, 0.0, 0.0);\n" + "if ((components & {}u) != 0u) // VB_HAS_TANGENT\n", + VB_HAS_TANGENT); + out.Write(" _tangent = float3(dot(N0, rawtangent), dot(N1, rawtangent), dot(N2, rawtangent));\n" "\n" - "float3 _norm2 = float3(0.0, 0.0, 0.0);\n" - "if ((components & {}u) != 0u) // VB_HAS_NRM2\n", - VB_HAS_NRM2); - out.Write(" _norm2 = float3(dot(N0, rawnorm2), dot(N1, rawnorm2), dot(N2, rawnorm2));\n" + "float3 _binormal = float3(0.0, 0.0, 0.0);\n" + "if ((components & {}u) != 0u) // VB_HAS_BINORMAL\n", + VB_HAS_BINORMAL); + out.Write(" _binormal = float3(dot(N0, rawbinormal), dot(N1, rawbinormal), dot(N2, " + "rawbinormal));\n" "\n"); // Hardware Lighting @@ -209,7 +210,7 @@ ShaderCode GenVertexShader(APIType api_type, const ShaderHostConfig& host_config "}}\n" "\n"); - WriteVertexLighting(out, api_type, "pos.xyz", "_norm0", "vertex_color_0", "vertex_color_1", + WriteVertexLighting(out, api_type, "pos.xyz", "_normal", "vertex_color_0", "vertex_color_1", "o.colors_0", "o.colors_1"); // Texture Coordinates @@ -247,7 +248,7 @@ ShaderCode GenVertexShader(APIType api_type, const ShaderHostConfig& host_config if (per_pixel_lighting) { - out.Write("o.Normal = _norm0;\n" + out.Write("o.Normal = _normal;\n" "o.WorldPos = pos.xyz;\n"); } @@ -394,19 +395,19 @@ static void GenVertexShaderTexGens(APIType api_type, u32 num_texgen, ShaderCode& out.Write(" coord.xyz = rawpos.xyz;\n"); out.Write(" break;\n\n"); out.Write(" case {:s}:\n", SourceRow::Normal); - out.Write( - " coord.xyz = ((components & {}u /* VB_HAS_NRM0 */) != 0u) ? rawnorm0.xyz : coord.xyz;", - VB_HAS_NRM0); + out.Write(" coord.xyz = ((components & {}u /* VB_HAS_NORMAL */) != 0u) ? rawnormal.xyz : " + "coord.xyz;", + VB_HAS_NORMAL); out.Write(" break;\n\n"); out.Write(" case {:s}:\n", SourceRow::BinormalT); - out.Write( - " coord.xyz = ((components & {}u /* VB_HAS_NRM1 */) != 0u) ? rawnorm1.xyz : coord.xyz;", - VB_HAS_NRM1); + out.Write(" coord.xyz = ((components & {}u /* VB_HAS_TANGENT */) != 0u) ? rawtangent.xyz : " + "coord.xyz;", + VB_HAS_TANGENT); out.Write(" break;\n\n"); out.Write(" case {:s}:\n", SourceRow::BinormalB); - out.Write( - " coord.xyz = ((components & {}u /* VB_HAS_NRM2 */) != 0u) ? rawnorm2.xyz : coord.xyz;", - VB_HAS_NRM2); + out.Write(" coord.xyz = ((components & {}u /* VB_HAS_BINORMAL */) != 0u) ? rawbinormal.xyz : " + "coord.xyz;", + VB_HAS_BINORMAL); out.Write(" break;\n\n"); for (u32 i = 0; i < 8; i++) { @@ -449,10 +450,10 @@ static void GenVertexShaderTexGens(APIType api_type, u32 num_texgen, ShaderCode& out.Write(" case {}u: output_tex.xyz = o.tex{}; break;\n", i, i); out.Write(" default: output_tex.xyz = float3(0.0, 0.0, 0.0); break;\n" " }}\n"); - out.Write(" if ((components & {}u) != 0u) {{ // VB_HAS_NRM1 | VB_HAS_NRM2\n", - VB_HAS_NRM1 | VB_HAS_NRM2); // Should this be VB_HAS_NRM1 | VB_HAS_NRM2 + out.Write(" if ((components & {}u) != 0u) {{ // VB_HAS_TANGENT | VB_HAS_BINORMAL\n", + VB_HAS_TANGENT | VB_HAS_BINORMAL); out.Write(" float3 ldir = normalize(" I_LIGHTS "[light].pos.xyz - pos.xyz);\n" - " output_tex.xyz += float3(dot(ldir, _norm1), dot(ldir, _norm2), 0.0);\n" + " output_tex.xyz += float3(dot(ldir, _tangent), dot(ldir, _binormal), 0.0);\n" " }}\n" " }}\n" " break;\n\n"); -- cgit v1.2.3 From 2a5c77f43ff1d69e78f12f13435a38c5eb2ed854 Mon Sep 17 00:00:00 2001 From: Pokechu22 Date: Wed, 13 Apr 2022 22:03:34 -0700 Subject: VideoCommon: Handle emboss texgen with only a single normal Fixes a large number of effects in Rogue Squadron 2 and 3. --- Source/Core/VideoCommon/UberShaderVertex.cpp | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) (limited to 'Source/Core/VideoCommon/UberShaderVertex.cpp') diff --git a/Source/Core/VideoCommon/UberShaderVertex.cpp b/Source/Core/VideoCommon/UberShaderVertex.cpp index 91568a4cec..c68c996b0a 100644 --- a/Source/Core/VideoCommon/UberShaderVertex.cpp +++ b/Source/Core/VideoCommon/UberShaderVertex.cpp @@ -169,12 +169,18 @@ ShaderCode GenVertexShader(APIType api_type, const ShaderHostConfig& host_config "if ((components & {}u) != 0u) // VB_HAS_TANGENT\n", VB_HAS_TANGENT); out.Write(" _tangent = float3(dot(N0, rawtangent), dot(N1, rawtangent), dot(N2, rawtangent));\n" + "else\n" + " _tangent = float3(dot(N0, " I_CACHED_TANGENT ".xyz), dot(N1, " I_CACHED_TANGENT + ".xyz), dot(N2, " I_CACHED_TANGENT ".xyz));\n" "\n" "float3 _binormal = float3(0.0, 0.0, 0.0);\n" "if ((components & {}u) != 0u) // VB_HAS_BINORMAL\n", VB_HAS_BINORMAL); out.Write(" _binormal = float3(dot(N0, rawbinormal), dot(N1, rawbinormal), dot(N2, " "rawbinormal));\n" + "else\n" + " _binormal = float3(dot(N0, " I_CACHED_BINORMAL ".xyz), dot(N1, " I_CACHED_BINORMAL + ".xyz), dot(N2, " I_CACHED_BINORMAL ".xyz));\n" "\n"); // Hardware Lighting @@ -449,12 +455,9 @@ static void GenVertexShaderTexGens(APIType api_type, u32 num_texgen, ShaderCode& for (u32 i = 0; i < num_texgen; i++) out.Write(" case {}u: output_tex.xyz = o.tex{}; break;\n", i, i); out.Write(" default: output_tex.xyz = float3(0.0, 0.0, 0.0); break;\n" - " }}\n"); - out.Write(" if ((components & {}u) != 0u) {{ // VB_HAS_TANGENT | VB_HAS_BINORMAL\n", - VB_HAS_TANGENT | VB_HAS_BINORMAL); - out.Write(" float3 ldir = normalize(" I_LIGHTS "[light].pos.xyz - pos.xyz);\n" - " output_tex.xyz += float3(dot(ldir, _tangent), dot(ldir, _binormal), 0.0);\n" " }}\n" + " float3 ldir = normalize(" I_LIGHTS "[light].pos.xyz - pos.xyz);\n" + " output_tex.xyz += float3(dot(ldir, _tangent), dot(ldir, _binormal), 0.0);\n" " }}\n" " break;\n\n"); out.Write(" case {:s}:\n", TexGenType::Color0); -- cgit v1.2.3 From 784079853d9daa4d5e4de0b49b610ec0e58a2b0c Mon Sep 17 00:00:00 2001 From: Pokechu22 Date: Tue, 19 Apr 2022 17:46:20 -0700 Subject: VideoCommon: Add comment explaining why only the first normal gets normalized Co-authored-by: Scott Mansell --- Source/Core/VideoCommon/UberShaderVertex.cpp | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) (limited to 'Source/Core/VideoCommon/UberShaderVertex.cpp') diff --git a/Source/Core/VideoCommon/UberShaderVertex.cpp b/Source/Core/VideoCommon/UberShaderVertex.cpp index c68c996b0a..40b4cd65e5 100644 --- a/Source/Core/VideoCommon/UberShaderVertex.cpp +++ b/Source/Core/VideoCommon/UberShaderVertex.cpp @@ -158,7 +158,11 @@ ShaderCode GenVertexShader(APIType api_type, const ShaderHostConfig& host_config "o.pos = float4(dot(" I_PROJECTION "[0], pos), dot(" I_PROJECTION "[1], pos), dot(" I_PROJECTION "[2], pos), dot(" I_PROJECTION "[3], pos));\n" "\n" - "// Only the first normal gets normalized (TODO: why?)\n" + "// The scale of the transform matrix is used to control the size of the emboss map\n" + "// effect by changing the scale of the transformed binormals (which only get used by\n" + "// emboss map texgens). By normalising the first transformed normal (which is used\n" + "// by lighting calculations and needs to be unit length), the same transform matrix\n" + "// can do double duty, scaling for emboss mapping, and not scaling for lighting.\n" "float3 _normal = float3(0.0, 0.0, 0.0);\n" "if ((components & {}u) != 0u) // VB_HAS_NORMAL\n", VB_HAS_NORMAL); -- cgit v1.2.3