From 36684bf82c7b458ba27802e6060683636bd47a4a Mon Sep 17 00:00:00 2001 From: Flyinghead Date: Thu, 1 Feb 2024 18:19:03 +0100 Subject: [PATCH] vk: share shader code between regular and oit renderers --- core/rend/vulkan/oit/oit_shaders.cpp | 88 +++------------------------- core/rend/vulkan/shaders.cpp | 19 ++++-- 2 files changed, 21 insertions(+), 86 deletions(-) diff --git a/core/rend/vulkan/oit/oit_shaders.cpp b/core/rend/vulkan/oit/oit_shaders.cpp index ecaba7d5f1..bb99e6b2f7 100644 --- a/core/rend/vulkan/oit/oit_shaders.cpp +++ b/core/rend/vulkan/oit/oit_shaders.cpp @@ -23,6 +23,8 @@ #include "rend/gl4/glsl.h" #include "cfg/option.h" +extern const char *FragmentShaderCommon; + static const char OITVertexShaderSource[] = R"( layout (std140, set = 0, binding = 0) uniform VertexShaderUniforms { @@ -131,9 +133,7 @@ layout (set = 0, binding = 3, std430) readonly buffer TrPolyParamBuffer { )"; -static const char OITFragmentShaderSource[] = R"( -#define PI 3.1415926 - +static const char OITFragmentShaderTop[] = R"( #define PASS_DEPTH 0 #define PASS_COLOR 1 #define PASS_OIT 2 @@ -196,84 +196,10 @@ layout (location = 6) flat in uint vtx_index; #if pp_FogCtrl != 2 || pp_TwoVolumes == 1 layout (set = 0, binding = 2) uniform sampler2D fog_table; - -float fog_mode2(float w) -{ - float z = clamp( -#if DIV_POS_Z == 1 - uniformBuffer.sp_FOG_DENSITY / w -#else - uniformBuffer.sp_FOG_DENSITY * w -#endif - , 1.0, 255.9999); - float exp = floor(log2(z)); - float m = z * 16.0 / pow(2.0, exp) - 16.0; - float idx = floor(m) + exp * 16.0 + 0.5; - vec4 fog_coef = texture(fog_table, vec2(idx / 128.0, 0.75 - (m - floor(m)) / 2.0)); - return fog_coef.r; -} -#endif - -vec4 colorClamp(vec4 col) -{ -// TODO This can change in two-volume mode -#if ColorClamping == 1 - return clamp(col, uniformBuffer.colorClampMin, uniformBuffer.colorClampMax); -#else - return col; -#endif -} - -#if pp_Palette != 0 - -vec4 getPaletteEntry(float colIdx) -{ - vec2 c = vec2(colIdx * 255.0 / 1023.0 + pushConstants.palette_index, 0.5); - return texture(palette, c); -} - -#endif - -#if pp_Palette == 1 - -vec4 palettePixel(sampler2D tex, vec3 coords) -{ -#if DIV_POS_Z == 1 - return getPaletteEntry(texture(tex, coords.xy).r); -#else - return getPaletteEntry(textureProj(tex, coords).r); -#endif -} - -#elif pp_Palette == 2 - -vec4 palettePixelBilinear(sampler2D tex, vec3 coords) -{ -#if DIV_POS_Z == 0 - coords.xy /= coords.z; -#endif - vec2 texSize = vec2(textureSize(tex, 0)); - vec2 pixCoord = coords.xy * texSize - 0.5; // coordinates of top left pixel - vec2 originPixCoord = floor(pixCoord); - - vec2 sampleUV = (originPixCoord + 0.5) / texSize; // UV coordinates of center of top left pixel - - // Sample from all surrounding texels - vec4 c00 = getPaletteEntry(texture(tex, sampleUV).r); - vec4 c01 = getPaletteEntry(textureOffset(tex, sampleUV, ivec2(0, 1)).r); - vec4 c11 = getPaletteEntry(textureOffset(tex, sampleUV, ivec2(1, 1)).r); - vec4 c10 = getPaletteEntry(textureOffset(tex, sampleUV, ivec2(1, 0)).r); - - vec2 weight = pixCoord - originPixCoord; - - // Bi-linear mixing - vec4 temp0 = mix(c00, c10, weight.x); - vec4 temp1 = mix(c01, c11, weight.x); - return mix(temp0, temp1, weight.y); -} - #endif +)"; +static const char OITFragmentShaderMain[] = R"( void main() { setFragDepth(vtx_uv.z); @@ -842,7 +768,9 @@ vk::UniqueShaderModule OITShaderManager::compileShader(const FragmentShaderParam .addConstant("PASS", (int)params.pass) .addSource(GouraudSource) .addSource(OITShaderHeader) - .addSource(OITFragmentShaderSource); + .addSource(OITFragmentShaderTop) + .addSource(FragmentShaderCommon) + .addSource(OITFragmentShaderMain); return ShaderCompiler::Compile(vk::ShaderStageFlagBits::eFragment, src.generate()); } diff --git a/core/rend/vulkan/shaders.cpp b/core/rend/vulkan/shaders.cpp index 6198710224..55eb36c387 100644 --- a/core/rend/vulkan/shaders.cpp +++ b/core/rend/vulkan/shaders.cpp @@ -62,9 +62,7 @@ void main() } )"; -static const char FragmentShaderSource[] = R"( -#define PI 3.1415926 - +static const char FragmentShaderTop[] = R"( layout (location = 0) out vec4 FragColor; #define gl_FragColor FragColor @@ -89,6 +87,9 @@ layout (push_constant) uniform pushBlock #if pp_Texture == 1 layout (set = 1, binding = 0) uniform sampler2D tex; #endif +#if pp_FogCtrl != 2 +layout (set = 0, binding = 2) uniform sampler2D fog_table; +#endif #if pp_Palette != 0 layout (set = 0, binding = 3) uniform sampler2D palette; #endif @@ -97,10 +98,12 @@ layout (set = 0, binding = 3) uniform sampler2D palette; layout (location = 0) INTERPOLATION in highp vec4 vtx_base; layout (location = 1) INTERPOLATION in highp vec4 vtx_offs; layout (location = 2) in highp vec3 vtx_uv; +)"; -#if pp_FogCtrl != 2 -layout (set = 0, binding = 2) uniform sampler2D fog_table; +const char *FragmentShaderCommon = R"( +#define PI 3.1415926 +#if pp_FogCtrl != 2 || pp_TwoVolumes == 1 float fog_mode2(float w) { float z = clamp( @@ -176,7 +179,9 @@ vec4 palettePixelBilinear(sampler2D tex, vec3 coords) } #endif +)"; +static const char FragmentShaderMain[] = R"( void main() { // Clip inside the box @@ -784,7 +789,9 @@ vk::UniqueShaderModule ShaderManager::compileShader(const FragmentShaderParams& .addConstant("DIV_POS_Z", (int)params.divPosZ) .addConstant("DITHERING", (int)params.dithering) .addSource(GouraudSource) - .addSource(FragmentShaderSource); + .addSource(FragmentShaderTop) + .addSource(FragmentShaderCommon) + .addSource(FragmentShaderMain); return ShaderCompiler::Compile(vk::ShaderStageFlagBits::eFragment, src.generate()); }