From 17778131bddee7df8efbee3d78abc5937b42a990 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Henrik=20Rydg=C3=A5rd?= Date: Sun, 1 Nov 2020 12:45:35 +0100 Subject: [PATCH] Share the uniform buffer declarations between D3D11 and VK. --- GPU/Common/FragmentShaderGenerator.cpp | 5 +- GPU/Common/ShaderUniforms.h | 68 ---------------------- GPU/Directx9/VertexShaderGeneratorHLSL.cpp | 6 +- GPU/GLES/VertexShaderGeneratorGLES.cpp | 7 ++- 4 files changed, 11 insertions(+), 75 deletions(-) diff --git a/GPU/Common/FragmentShaderGenerator.cpp b/GPU/Common/FragmentShaderGenerator.cpp index f593c82115..bb51a14c36 100644 --- a/GPU/Common/FragmentShaderGenerator.cpp +++ b/GPU/Common/FragmentShaderGenerator.cpp @@ -51,6 +51,9 @@ const char *hlsl_preamble_fs = "#define vec4 float4\n" "#define uvec3 uint3\n" "#define ivec3 int3\n" +"#define ivec4 int4\n" +"#define mat4 float4x4\n" +"#define mat3x4 float4x3\n" // note how the conventions are backwards "#define splat3(x) float3(x, x, x)\n" "#define mix lerp\n" "#define mod(x, y) fmod(x, y)\n"; @@ -246,7 +249,7 @@ bool GenerateFragmentShader(const FShaderID &id, char *buffer, const ShaderLangu WRITE(p, "Texture2D fboTex : register(t1);\n"); } } - WRITE(p, "cbuffer base : register(b0) {\n%s};\n", cb_baseStr); + WRITE(p, "cbuffer base : register(b0) {\n%s};\n", ub_baseStr); } if (enableAlphaTest) { diff --git a/GPU/Common/ShaderUniforms.h b/GPU/Common/ShaderUniforms.h index fc8ac8e95e..a2ecf4a71b 100644 --- a/GPU/Common/ShaderUniforms.h +++ b/GPU/Common/ShaderUniforms.h @@ -72,34 +72,6 @@ R"( mat4 u_proj; vec2 u_texclampoff; )"; -// HLSL code is shared so these names are changed to match those in DX9. -static const char *cb_baseStr = -R"( float4x4 u_proj; - float4x4 u_proj_through; - float4x3 u_view; - float4x3 u_world; - float4x3 u_texmtx; - float4 u_uvscaleoffset; - float4 u_depthRange; - float2 u_fogcoef; - float u_stencilReplaceValue; - float4 u_matambientalpha; - uint u_spline_counts; - uint u_depal_mask_shift_off_fmt; - int pad2; - int pad3; - float4 u_cullRangeMin; - float4 u_cullRangeMax; - float3 u_fogcolor; - float3 u_texenv; - int4 u_alphacolorref; - uint4 u_alphacolormask; - float3 u_blendFixA; - float3 u_blendFixB; - float4 u_texclamp; - float2 u_texclampoff; -)"; - // 512 bytes. Would like to shrink more. Some colors only have 8-bit precision and we expand // them to float unnecessarily, could just as well expand in the shader. struct UB_VS_Lights { @@ -151,42 +123,6 @@ R"( vec4 u_ambient; vec3 u_lightspecular3; )"; -// HLSL code is shared so these names are changed to match those in DX9. -static const char *cb_vs_lightsStr = -R"( float4 u_ambient; - float3 u_matdiffuse; - float4 u_matspecular; - float3 u_matemissive; - float3 u_lightpos0; - float3 u_lightpos1; - float3 u_lightpos2; - float3 u_lightpos3; - float3 u_lightdir0; - float3 u_lightdir1; - float3 u_lightdir2; - float3 u_lightdir3; - float3 u_lightatt0; - float3 u_lightatt1; - float3 u_lightatt2; - float3 u_lightatt3; - float4 u_lightangle_spotCoef0; - float4 u_lightangle_spotCoef1; - float4 u_lightangle_spotCoef2; - float4 u_lightangle_spotCoef3; - float3 u_lightambient0; - float3 u_lightambient1; - float3 u_lightambient2; - float3 u_lightambient3; - float3 u_lightdiffuse0; - float3 u_lightdiffuse1; - float3 u_lightdiffuse2; - float3 u_lightdiffuse3; - float3 u_lightspecular0; - float3 u_lightspecular1; - float3 u_lightspecular2; - float3 u_lightspecular3; -)"; - // With some cleverness, we could get away with uploading just half this when only the four or five first // bones are being used. This is 384b. struct UB_VS_Bones { @@ -197,10 +133,6 @@ static const char *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; )"; -static const char *cb_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; -)"; - void CalcCullRange(float minValues[4], float maxValues[4], bool flipViewport, bool hasNegZ); void BaseUpdateUniforms(UB_VS_FS_Base *ub, uint64_t dirtyUniforms, bool flipViewport, bool useBufferedRendering); diff --git a/GPU/Directx9/VertexShaderGeneratorHLSL.cpp b/GPU/Directx9/VertexShaderGeneratorHLSL.cpp index 0c603ada48..7e980f70be 100644 --- a/GPU/Directx9/VertexShaderGeneratorHLSL.cpp +++ b/GPU/Directx9/VertexShaderGeneratorHLSL.cpp @@ -173,9 +173,9 @@ bool GenerateVertexShaderHLSL(const VShaderID &id, char *buffer, ShaderLanguage WRITE(p, "vec4 u_cullRangeMax : register(c%i);\n", CONST_VS_CULLRANGEMAX); } } else { - WRITE(p, "cbuffer base : register(b0) {\n%s};\n", cb_baseStr); - WRITE(p, "cbuffer lights: register(b1) {\n%s};\n", cb_vs_lightsStr); - WRITE(p, "cbuffer bones : register(b2) {\n%s};\n", cb_vs_bonesStr); + WRITE(p, "cbuffer base : register(b0) {\n%s};\n", ub_baseStr); + WRITE(p, "cbuffer lights: register(b1) {\n%s};\n", ub_vs_lightsStr); + WRITE(p, "cbuffer bones : register(b2) {\n%s};\n", ub_vs_bonesStr); } bool scaleUV = !isModeThrough && (uvGenMode == GE_TEXMAP_TEXTURE_COORDS || uvGenMode == GE_TEXMAP_UNKNOWN); diff --git a/GPU/GLES/VertexShaderGeneratorGLES.cpp b/GPU/GLES/VertexShaderGeneratorGLES.cpp index a026ccf469..fdc05c58bd 100644 --- a/GPU/GLES/VertexShaderGeneratorGLES.cpp +++ b/GPU/GLES/VertexShaderGeneratorGLES.cpp @@ -139,6 +139,7 @@ const char *hlsl_preamble_vs = "#define vec3 float3\n" "#define vec4 float4\n" "#define ivec2 int2\n" +"#define ivec4 int4\n" "#define mat4 float4x4\n" "#define mat3x4 float4x3\n" // note how the conventions are backwards "#define splat3(x) vec3(x, x, x)\n" @@ -297,9 +298,9 @@ bool GenerateVertexShaderGLSL(const VShaderID &id, char *buffer, const ShaderLan } else if (compat.shaderLanguage == HLSL_D3D11 || compat.shaderLanguage == HLSL_D3D9) { // Note: These two share some code after this hellishly large if/else. if (compat.shaderLanguage == HLSL_D3D11) { - WRITE(p, "cbuffer base : register(b0) {\n%s};\n", cb_baseStr); - WRITE(p, "cbuffer lights: register(b1) {\n%s};\n", cb_vs_lightsStr); - WRITE(p, "cbuffer bones : register(b2) {\n%s};\n", cb_vs_bonesStr); + WRITE(p, "cbuffer base : register(b0) {\n%s};\n", ub_baseStr); + WRITE(p, "cbuffer lights: register(b1) {\n%s};\n", ub_vs_lightsStr); + WRITE(p, "cbuffer bones : register(b2) {\n%s};\n", ub_vs_bonesStr); } else { WRITE(p, "#pragma warning( disable : 3571 )\n"); if (isModeThrough) {