Skip to content

Commit

Permalink
vk: share shader code between regular and oit renderers
Browse files Browse the repository at this point in the history
  • Loading branch information
flyinghead committed Feb 1, 2024
1 parent cdaaeb5 commit 36684bf
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 86 deletions.
88 changes: 8 additions & 80 deletions core/rend/vulkan/oit/oit_shaders.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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());
}

Expand Down
19 changes: 13 additions & 6 deletions core/rend/vulkan/shaders.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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(
Expand Down Expand Up @@ -176,7 +179,9 @@ vec4 palettePixelBilinear(sampler2D tex, vec3 coords)
}
#endif
)";

static const char FragmentShaderMain[] = R"(
void main()
{
// Clip inside the box
Expand Down Expand Up @@ -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());
}

Expand Down

0 comments on commit 36684bf

Please sign in to comment.