From 2f1bbd314e0524cd70884f0a1ce1e900d8033289 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Henrik=20Rydg=C3=A5rd?= Date: Sat, 31 Oct 2020 11:31:16 +0100 Subject: [PATCH] Fragment shader gen: Remove unnecessary allocations --- Common/GPU/D3D9/D3D9ShaderCompiler.cpp | 4 +++ GPU/Directx9/ShaderManagerDX9.cpp | 2 +- GPU/GLES/FragmentShaderGeneratorGLES.cpp | 31 ++++++++++-------------- 3 files changed, 18 insertions(+), 19 deletions(-) diff --git a/Common/GPU/D3D9/D3D9ShaderCompiler.cpp b/Common/GPU/D3D9/D3D9ShaderCompiler.cpp index a46131bda1..835fbbc9fa 100644 --- a/Common/GPU/D3D9/D3D9ShaderCompiler.cpp +++ b/Common/GPU/D3D9/D3D9ShaderCompiler.cpp @@ -36,6 +36,10 @@ LPD3DBLOB CompileShaderToByteCodeD3D9(const char *code, const char *target, std: OutputDebugStringUTF8(errorMessage->c_str()); pErrorMsg->Release(); + if (pShaderCode) { + pShaderCode->Release(); + pShaderCode = nullptr; + } pShaderCode = nullptr; } else if (FAILED(hr)) { *errorMessage = GetStringErrorMsg(hr); diff --git a/GPU/Directx9/ShaderManagerDX9.cpp b/GPU/Directx9/ShaderManagerDX9.cpp index 4b78d25860..775dd9015a 100644 --- a/GPU/Directx9/ShaderManagerDX9.cpp +++ b/GPU/Directx9/ShaderManagerDX9.cpp @@ -512,7 +512,7 @@ void ShaderManagerDX9::VSUpdateUniforms(u64 dirtyUniforms) { ShaderManagerDX9::ShaderManagerDX9(Draw::DrawContext *draw, LPDIRECT3DDEVICE9 device) : ShaderManagerCommon(draw), device_(device), compat_(HLSL_D3D9) { - codeBuffer_ = new char[16384]; + codeBuffer_ = new char[32768]; } ShaderManagerDX9::~ShaderManagerDX9() { diff --git a/GPU/GLES/FragmentShaderGeneratorGLES.cpp b/GPU/GLES/FragmentShaderGeneratorGLES.cpp index a48c5a9b35..91d585cd72 100644 --- a/GPU/GLES/FragmentShaderGeneratorGLES.cpp +++ b/GPU/GLES/FragmentShaderGeneratorGLES.cpp @@ -67,6 +67,7 @@ const char *hlsl_d3d9_preamble_fs = bool GenerateFragmentShaderGLSL(const FShaderID &id, char *buffer, const GLSLShaderCompat &compat, uint64_t *uniformMask, std::string *errorString) { *uniformMask = 0; + errorString->clear(); bool highpFog = false; bool highpTexcoord = false; @@ -695,20 +696,17 @@ bool GenerateFragmentShaderGLSL(const FShaderID &id, char *buffer, const GLSLSha if (enableFog) { WRITE(p, " float fogCoef = clamp(%sv_fogdepth, 0.0, 1.0);\n", compat.inPrefix); WRITE(p, " v = mix(vec4(u_fogcolor, v.a), v, fogCoef);\n"); - // WRITE(p, " v.x = v_depth;\n"); } // Texture access is at half texels [0.5/256, 255.5/256], but colors are normalized [0, 255]. // So we have to scale to account for the difference. - std::string alphaTestXCoord = "0"; + char alphaTestXCoord[64] = "0"; if (enableFragmentTestCache) { if (enableColorTest && !colorTestAgainstZero) { WRITE(p, " vec4 vScale256 = v * %f + %f;\n", 255.0 / 256.0, 0.5 / 256.0); - alphaTestXCoord = "vScale256.a"; + truncate_cpy(alphaTestXCoord, "vScale256.a"); } else if (enableAlphaTest && !alphaTestAgainstZero) { - char temp[64]; - snprintf(temp, sizeof(temp), "v.a * %f + %f", 255.0 / 256.0, 0.5 / 256.0); - alphaTestXCoord = temp; + snprintf(alphaTestXCoord, sizeof(alphaTestXCoord), "v.a * %f + %f", 255.0 / 256.0, 0.5 / 256.0); } } @@ -728,7 +726,7 @@ bool GenerateFragmentShaderGLSL(const FShaderID &id, char *buffer, const GLSLSha WRITE(p, " %s\n", discardStatement); } } else if (enableFragmentTestCache) { - WRITE(p, " float aResult = %s(testtex, vec2(%s, 0)).a;\n", compat.texture, alphaTestXCoord.c_str()); + WRITE(p, " float aResult = %s(testtex, vec2(%s, 0)).a;\n", compat.texture, alphaTestXCoord); WRITE(p, " if (aResult < 0.5) %s\n", discardStatement); } else { const char *alphaTestFuncs[] = { "#", "#", " != ", " == ", " >= ", " > ", " <= ", " < " }; @@ -919,36 +917,33 @@ bool GenerateFragmentShaderGLSL(const FShaderID &id, char *buffer, const GLSLSha } } - std::string replacedAlpha = "0.0"; - char replacedAlphaTemp[64] = ""; + char replacedAlpha[64] = "0.0"; if (stencilToAlpha != REPLACE_ALPHA_NO) { switch (replaceAlphaWithStencilType) { case STENCIL_VALUE_UNIFORM: - replacedAlpha = "u_stencilReplaceValue"; + truncate_cpy(replacedAlpha, "u_stencilReplaceValue"); break; case STENCIL_VALUE_ZERO: - replacedAlpha = "0.0"; + truncate_cpy(replacedAlpha, "0.0"); break; case STENCIL_VALUE_ONE: case STENCIL_VALUE_INVERT: // In invert, we subtract by one, but we want to output one here. - replacedAlpha = "1.0"; + truncate_cpy(replacedAlpha, "1.0"); break; case STENCIL_VALUE_INCR_4: case STENCIL_VALUE_DECR_4: // We're adding/subtracting, just by the smallest value in 4-bit. - snprintf(replacedAlphaTemp, sizeof(replacedAlphaTemp), "%f", 1.0 / 15.0); - replacedAlpha = replacedAlphaTemp; + snprintf(replacedAlpha, sizeof(replacedAlpha), "%f", 1.0 / 15.0); break; case STENCIL_VALUE_INCR_8: case STENCIL_VALUE_DECR_8: // We're adding/subtracting, just by the smallest value in 8-bit. - snprintf(replacedAlphaTemp, sizeof(replacedAlphaTemp), "%f", 1.0 / 255.0); - replacedAlpha = replacedAlphaTemp; + snprintf(replacedAlpha, sizeof(replacedAlpha), "%f", 1.0 / 255.0); break; case STENCIL_VALUE_KEEP: @@ -963,7 +958,7 @@ bool GenerateFragmentShaderGLSL(const FShaderID &id, char *buffer, const GLSLSha break; case REPLACE_ALPHA_YES: - WRITE(p, " v.a = %s;\n", replacedAlpha.c_str()); + WRITE(p, " v.a = %s;\n", replacedAlpha); break; case REPLACE_ALPHA_NO: @@ -1014,7 +1009,7 @@ bool GenerateFragmentShaderGLSL(const FShaderID &id, char *buffer, const GLSLSha } if (stencilToAlpha == REPLACE_ALPHA_DUALSOURCE) { - WRITE(p, " %s = vec4(v.rgb, %s);\n", compat.fragColor0, replacedAlpha.c_str()); + WRITE(p, " %s = vec4(v.rgb, %s);\n", compat.fragColor0, replacedAlpha); WRITE(p, " %s = vec4(0.0, 0.0, 0.0, v.a);\n", compat.fragColor1); } else if (compat.shaderLanguage != HLSL_D3D9) { WRITE(p, " %s = v;\n", compat.fragColor0);