From 9d1355e13737a6a7911f1caa281d7decabb6746b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Henrik=20Rydg=C3=A5rd?= Date: Mon, 26 Sep 2022 09:30:54 +0200 Subject: [PATCH] Always do the vertex shader part of the fog computation. In #16104, we drastically reduced the number of shader variants for games that use flexible lighting setups. I looked at a few games and it seems that a lot of games have the same shaders with fog on/off, while fog is super cheap to compute. So let's just always do it, reducing vertex shader variants further (though the amount of pipelines will probably remain the same, since we still specialize the fragment shader). Might also be worth adding a dynamic bool for the fragment shader, but if so, doing it separately. --- GPU/Common/ShaderId.cpp | 3 -- GPU/Common/ShaderId.h | 1 - GPU/Common/ShaderUniforms.h | 8 ++--- GPU/Common/VertexShaderGenerator.cpp | 51 ++++++++++------------------ GPU/GLES/ShaderManagerGLES.cpp | 1 - GPU/GPUCommon.cpp | 2 +- GPU/Vulkan/PipelineManagerVulkan.cpp | 3 +- GPU/Vulkan/ShaderManagerVulkan.cpp | 1 - 8 files changed, 23 insertions(+), 47 deletions(-) diff --git a/GPU/Common/ShaderId.cpp b/GPU/Common/ShaderId.cpp index 7530e74aaf..5ed727a638 100644 --- a/GPU/Common/ShaderId.cpp +++ b/GPU/Common/ShaderId.cpp @@ -22,7 +22,6 @@ std::string VertexShaderDesc(const VShaderID &id) { if (id.Bit(VS_BIT_HAS_TEXCOORD)) desc << "T "; if (id.Bit(VS_BIT_HAS_NORMAL)) desc << "N "; if (id.Bit(VS_BIT_LMODE)) desc << "LM "; - if (id.Bit(VS_BIT_ENABLE_FOG)) desc << "Fog "; if (id.Bit(VS_BIT_NORM_REVERSE)) desc << "RevN "; if (id.Bit(VS_BIT_DO_TEXTURE)) desc << "Tex "; int uvgMode = id.Bits(VS_BIT_UVGEN_MODE, 2); @@ -83,14 +82,12 @@ void ComputeVertexShaderID(VShaderID *id_out, u32 vertType, bool useHWTransform, _assert_(hasNormal); } - bool enableFog = gstate.isFogEnabled() && !isModeThrough && !gstate.isModeClear(); bool lmode = gstate.isUsingSecondaryColor() && gstate.isLightingEnabled() && !isModeThrough && !gstate.isModeClear(); bool vertexRangeCulling = gstate_c.Supports(GPU_SUPPORTS_VS_RANGE_CULLING) && !isModeThrough && gstate_c.submitType == SubmitType::DRAW; // neither hw nor sw spline/bezier. See #11692 VShaderID id; id.SetBit(VS_BIT_LMODE, lmode); - id.SetBit(VS_BIT_ENABLE_FOG, enableFog); id.SetBit(VS_BIT_IS_THROUGH, isModeThrough); id.SetBit(VS_BIT_HAS_COLOR, hasColor); id.SetBit(VS_BIT_VERTEX_RANGE_CULLING, vertexRangeCulling); diff --git a/GPU/Common/ShaderId.h b/GPU/Common/ShaderId.h index a27d7ad7fa..b63f93739e 100644 --- a/GPU/Common/ShaderId.h +++ b/GPU/Common/ShaderId.h @@ -12,7 +12,6 @@ enum VShaderBit : uint8_t { VS_BIT_LMODE = 0, VS_BIT_IS_THROUGH = 1, - VS_BIT_ENABLE_FOG = 2, VS_BIT_HAS_COLOR = 3, VS_BIT_DO_TEXTURE = 4, VS_BIT_VERTEX_RANGE_CULLING = 5, diff --git a/GPU/Common/ShaderUniforms.h b/GPU/Common/ShaderUniforms.h index ab71309e93..3872461437 100644 --- a/GPU/Common/ShaderUniforms.h +++ b/GPU/Common/ShaderUniforms.h @@ -36,7 +36,7 @@ struct UB_VS_FS_Base { uint32_t spline_counts; uint32_t depal_mask_shift_off_fmt; // 4 params packed into one. uint32_t colorWriteMask; float mipBias; // Fragment data - float fogColor[4]; // .w is unused + float fogColor[4]; float texEnvColor[4]; // .w is unused int alphaColorRef[4]; int colorTestMask[4]; @@ -46,7 +46,7 @@ struct UB_VS_FS_Base { float texClampOffset[4]; // .zw are unused }; -static const char *ub_baseStr = +static const char * const ub_baseStr = R"( mat4 u_proj; mat4 u_proj_through; mat3x4 u_view; @@ -91,7 +91,7 @@ struct UB_VS_Lights { float lightSpecular[4][4]; }; -static const char *ub_vs_lightsStr = +static const char * const ub_vs_lightsStr = R"( vec4 u_ambient; vec3 u_matdiffuse; vec4 u_matspecular; @@ -133,7 +133,7 @@ struct UB_VS_Bones { float bones[8][12]; }; -static const char *ub_vs_bonesStr = +static const char * const ub_vs_bonesStr = R"( mat3x4 u_bone0; mat3x4 u_bone1; mat3x4 u_bone2; mat3x4 u_bone3; mat3x4 u_bone4; mat3x4 u_bone5; mat3x4 u_bone6; mat3x4 u_bone7; mat3x4 u_bone8; )"; diff --git a/GPU/Common/VertexShaderGenerator.cpp b/GPU/Common/VertexShaderGenerator.cpp index 8263457d60..8de59ab191 100644 --- a/GPU/Common/VertexShaderGenerator.cpp +++ b/GPU/Common/VertexShaderGenerator.cpp @@ -177,7 +177,6 @@ bool GenerateVertexShader(const VShaderID &id, char *buffer, const ShaderLanguag bool hasColor = id.Bit(VS_BIT_HAS_COLOR) || !useHWTransform; bool hasNormal = id.Bit(VS_BIT_HAS_NORMAL) && useHWTransform; bool hasTexcoord = id.Bit(VS_BIT_HAS_TEXCOORD) || !useHWTransform; - bool enableFog = id.Bit(VS_BIT_ENABLE_FOG); bool flipNormal = id.Bit(VS_BIT_NORM_REVERSE); int ls0 = id.Bits(VS_BIT_LS0, 2); int ls1 = id.Bits(VS_BIT_LS1, 2); @@ -258,7 +257,7 @@ bool GenerateVertexShader(const VShaderID &id, char *buffer, const ShaderLanguag if (useHWTransform && hasNormal) WRITE(p, "layout (location = %d) in vec3 normal;\n", (int)PspAttributeLocation::NORMAL); - if (!useHWTransform && enableFog) + if (!useHWTransform) WRITE(p, "layout (location = %d) in float fog;\n", (int)PspAttributeLocation::NORMAL); if (doTexture && hasTexcoord) { @@ -283,11 +282,7 @@ bool GenerateVertexShader(const VShaderID &id, char *buffer, const ShaderLanguag WRITE(p, "layout (location = 0) out highp vec3 v_texcoord;\n"); } - if (enableFog) { - // See the fragment shader generator - WRITE(p, "layout (location = 3) out highp float v_fogdepth;\n"); - } - + WRITE(p, "layout (location = 3) out highp float v_fogdepth;\n"); WRITE(p, "invariant gl_Position;\n"); } else if (compat.shaderLanguage == HLSL_D3D11 || compat.shaderLanguage == HLSL_D3D9) { @@ -306,9 +301,7 @@ bool GenerateVertexShader(const VShaderID &id, char *buffer, const ShaderLanguag WRITE(p, "float u_rotation : register(c%i);\n", CONST_VS_ROTATION); } - if (enableFog) { - WRITE(p, "vec2 u_fogcoef : register(c%i);\n", CONST_VS_FOGCOEF); - } + WRITE(p, "vec2 u_fogcoef : register(c%i);\n", CONST_VS_FOGCOEF); if (useHWTransform || !hasColor) WRITE(p, "vec4 u_matambientalpha : register(c%i);\n", CONST_VS_MATAMBIENTALPHA); // matambient + matalpha @@ -409,9 +402,7 @@ 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, " float fog : NORMAL;\n"); WRITE(p, "};\n"); } @@ -424,9 +415,7 @@ bool GenerateVertexShader(const VShaderID &id, char *buffer, const ShaderLanguag if (lmode) WRITE(p, " vec3 v_color1 : COLOR1;\n"); - if (enableFog) { - WRITE(p, " float v_fogdepth : TEXCOORD1;\n"); - } + WRITE(p, " float v_fogdepth : TEXCOORD1;\n"); if (compat.shaderLanguage == HLSL_D3D9) { WRITE(p, " vec4 gl_Position : POSITION;\n"); } else { @@ -468,7 +457,7 @@ 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) { + if (!useHWTransform) { WRITE(p, "%s highp float fog;\n", compat.attribute); *attrMask |= 1 << ATTR_NORMAL; } @@ -584,10 +573,8 @@ bool GenerateVertexShader(const VShaderID &id, char *buffer, const ShaderLanguag WRITE(p, "uniform lowp vec4 u_matambientalpha;\n"); // matambient + matalpha *uniformMask |= DIRTY_MATAMBIENTALPHA; } - if (enableFog) { - WRITE(p, "uniform highp vec2 u_fogcoef;\n"); - *uniformMask |= DIRTY_FOGCOEF; - } + WRITE(p, "uniform highp vec2 u_fogcoef;\n"); + *uniformMask |= DIRTY_FOGCOEF; if (!isModeThrough) { WRITE(p, "uniform highp vec4 u_depthRange;\n"); @@ -605,13 +592,11 @@ bool GenerateVertexShader(const VShaderID &id, char *buffer, const ShaderLanguag WRITE(p, "%s %s vec3 v_texcoord;\n", compat.varying_vs, highpTexcoord ? "highp" : "mediump"); } - if (enableFog) { - // See the fragment shader generator - if (highpFog) { - WRITE(p, "%s highp float v_fogdepth;\n", compat.varying_vs); - } else { - WRITE(p, "%s mediump float v_fogdepth;\n", compat.varying_vs); - } + // See the fragment shader generator + if (highpFog) { + WRITE(p, "%s highp float v_fogdepth;\n", compat.varying_vs); + } else { + WRITE(p, "%s mediump float v_fogdepth;\n", compat.varying_vs); } } @@ -821,7 +806,7 @@ bool GenerateVertexShader(const VShaderID &id, char *buffer, const ShaderLanguag } else { WRITE(p, " vec4 position = In.position;\n"); } - if (!useHWTransform && enableFog) { + if (!useHWTransform) { WRITE(p, " float fog = In.fog;\n"); } if (enableBones) { @@ -847,9 +832,7 @@ bool GenerateVertexShader(const VShaderID &id, char *buffer, const ShaderLanguag if (lmode) WRITE(p, " %sv_color1 = splat3(0.0);\n", compat.vsOutPrefix); } - if (enableFog) { - WRITE(p, " %sv_fogdepth = fog;\n", compat.vsOutPrefix); - } + WRITE(p, " %sv_fogdepth = fog;\n", compat.vsOutPrefix); if (isModeThrough) { // The proj_through matrix already has the rotation, if needed. WRITE(p, " vec4 outPos = mul(u_proj_through, vec4(position.xyz, 1.0));\n"); @@ -1023,6 +1006,7 @@ bool GenerateVertexShader(const VShaderID &id, char *buffer, const ShaderLanguag if (lightUberShader) { // TODO: Actually loop in the shader. For now, we write it all out. + // Will need to change how the data is stored to loop efficiently. // u_lightControl is computed in PackLightControlBits(). for (int i = 0; i < 4; i++) { p.F(" if ((u_lightControl & %du) != 0u) { \n", 1 << i); @@ -1271,8 +1255,7 @@ bool GenerateVertexShader(const VShaderID &id, char *buffer, const ShaderLanguag } // Compute fogdepth - if (enableFog) - WRITE(p, " %sv_fogdepth = (viewPos.z + u_fogcoef.x) * u_fogcoef.y;\n", compat.vsOutPrefix); + WRITE(p, " %sv_fogdepth = (viewPos.z + u_fogcoef.x) * u_fogcoef.y;\n", compat.vsOutPrefix); } if (clipClampedDepth) { diff --git a/GPU/GLES/ShaderManagerGLES.cpp b/GPU/GLES/ShaderManagerGLES.cpp index a0a8aac16d..2dbd9a6bd4 100644 --- a/GPU/GLES/ShaderManagerGLES.cpp +++ b/GPU/GLES/ShaderManagerGLES.cpp @@ -841,7 +841,6 @@ LinkedShader *ShaderManagerGLES::ApplyFragmentShader(VShaderID VSID, Shader *vs, if (ls == nullptr) { _dbg_assert_(FSID.Bit(FS_BIT_LMODE) == VSID.Bit(VS_BIT_LMODE)); _dbg_assert_(FSID.Bit(FS_BIT_DO_TEXTURE) == VSID.Bit(VS_BIT_DO_TEXTURE)); - _dbg_assert_(FSID.Bit(FS_BIT_ENABLE_FOG) == VSID.Bit(VS_BIT_ENABLE_FOG)); _dbg_assert_(FSID.Bit(FS_BIT_FLATSHADE) == VSID.Bit(VS_BIT_FLATSHADE)); // Check if we can link these. diff --git a/GPU/GPUCommon.cpp b/GPU/GPUCommon.cpp index bb78a5be96..00ac51f9c1 100644 --- a/GPU/GPUCommon.cpp +++ b/GPU/GPUCommon.cpp @@ -89,7 +89,7 @@ const CommonCommandTableEntry commonCommandTable[] = { // These affect the fragment shader so need flushing. { GE_CMD_CLEARMODE, FLAG_FLUSHBEFOREONCHANGE, DIRTY_BLEND_STATE | DIRTY_DEPTHSTENCIL_STATE | DIRTY_RASTER_STATE | DIRTY_VIEWPORTSCISSOR_STATE | DIRTY_CULLRANGE | DIRTY_VERTEXSHADER_STATE | DIRTY_FRAGMENTSHADER_STATE }, { GE_CMD_TEXTUREMAPENABLE, FLAG_FLUSHBEFOREONCHANGE, DIRTY_VERTEXSHADER_STATE | DIRTY_FRAGMENTSHADER_STATE }, - { GE_CMD_FOGENABLE, FLAG_FLUSHBEFOREONCHANGE, DIRTY_VERTEXSHADER_STATE | DIRTY_FRAGMENTSHADER_STATE }, + { GE_CMD_FOGENABLE, FLAG_FLUSHBEFOREONCHANGE, DIRTY_FRAGMENTSHADER_STATE }, { GE_CMD_TEXMODE, FLAG_FLUSHBEFOREONCHANGE, DIRTY_TEXTURE_PARAMS | DIRTY_FRAGMENTSHADER_STATE }, { GE_CMD_TEXSHADELS, FLAG_FLUSHBEFOREONCHANGE, DIRTY_VERTEXSHADER_STATE }, // Raster state for Direct3D 9, uncommon. diff --git a/GPU/Vulkan/PipelineManagerVulkan.cpp b/GPU/Vulkan/PipelineManagerVulkan.cpp index c965130113..2ea530d25a 100644 --- a/GPU/Vulkan/PipelineManagerVulkan.cpp +++ b/GPU/Vulkan/PipelineManagerVulkan.cpp @@ -270,8 +270,7 @@ static VulkanPipeline *CreateVulkanPipeline(VulkanRenderManager *renderManager, } else { bool needsUV = vs->GetID().Bit(VS_BIT_DO_TEXTURE); bool needsColor1 = vs->GetID().Bit(VS_BIT_LMODE); - bool needsFog = vs->GetID().Bit(VS_BIT_ENABLE_FOG); - attributeCount = SetupVertexAttribsPretransformed(attrs, needsUV, needsColor1, needsFog); + attributeCount = SetupVertexAttribsPretransformed(attrs, needsUV, needsColor1, true); vertexStride = (int)sizeof(TransformedVertex); } diff --git a/GPU/Vulkan/ShaderManagerVulkan.cpp b/GPU/Vulkan/ShaderManagerVulkan.cpp index a9504977d1..acdeba7b4a 100644 --- a/GPU/Vulkan/ShaderManagerVulkan.cpp +++ b/GPU/Vulkan/ShaderManagerVulkan.cpp @@ -250,7 +250,6 @@ void ShaderManagerVulkan::GetShaders(int prim, u32 vertType, VulkanVertexShader _dbg_assert_(FSID.Bit(FS_BIT_LMODE) == VSID.Bit(VS_BIT_LMODE)); _dbg_assert_(FSID.Bit(FS_BIT_DO_TEXTURE) == VSID.Bit(VS_BIT_DO_TEXTURE)); - _dbg_assert_(FSID.Bit(FS_BIT_ENABLE_FOG) == VSID.Bit(VS_BIT_ENABLE_FOG)); _dbg_assert_(FSID.Bit(FS_BIT_FLATSHADE) == VSID.Bit(VS_BIT_FLATSHADE)); // Just update uniforms if this is the same shader as last time.