TsundereStation/data/shaders/vulkan/display_msaa.frag.glsl
LibretroAdmin 59b6fac749 common/vulkan, core: pre-bake display FS (MSAA blob pair, 5-6 spec consts)
Patch 9 in the glslang-elimination series. The display FS is the
densest shader in the project - shadergen takes depth_24bit (bool) x
interlace_mode (None/InterleavedFields/SeparateFields) x smooth_chroma
(bool) source variants, and the source m_vram_texture's sample count
varies with MSAA on top of that. The depth_24bit / interlace_mode pair
is the only knob set that varies at runtime within a session
(GPUSTAT.display_area_color_depth_24 and the CRTC interlace mode flip
during normal gameplay); smooth_chroma, MSAA, and RESOLUTION_SCALE are
all session-constant (toggling any of them goes through UpdateSettings
-> DestroyPipelines).

Shader converted:

  DisplayFragmentShader. Reads the upscaled m_vram_texture, applies
  field-discard for interlaced output, and writes the visible scanout
  rectangle to m_display_texture - either as direct 16bpp samples or as
  reconstructed 24bpp YUV-smoothed colour depending on the GPUSTAT bit.

  Blobs:
    display_fs       - source sampler is sampler2D       (non-MSAA path).
    display_msaa_fs  - source sampler is sampler2DMS, plus an extra
                       MULTISAMPLES spec constant for the LoadVRAM
                       averaging loop bound.

  Common spec constants on both blobs:
    id =   0 RESOLUTION_SCALE (uint) - common-knob convention.
    id = 100 DEPTH_24BIT      (bool) - 16bpp vs 24bpp reconstruction.
    id = 101 INTERLACED       (bool) - field-discard logic gate.
    id = 102 INTERLEAVED      (bool) - only meaningful when INTERLACED.
                                       Selects interleaved-both-fields
                                       coord folding vs separate-field.
    id = 103 SMOOTH_CHROMA    (bool) - only meaningful when DEPTH_24BIT.
                                       Enables bilinear UV smoothing.

  MSAA blob adds:
    id =   1 MULTISAMPLES     (uint) - common-knob convention.

The m_display_pipelines[depth_24][interlace_mode] 2x3 slot table is
preserved; each entry gets its own spec-const-specialised pipeline at
first-touch, like the VRAM fill pattern. interlace_mode is folded into
INTERLACED / INTERLEAVED at the call site:
  None              -> INTERLACED=false, INTERLEAVED=*  (INTERLEAVED
                                                         irrelevant when
                                                         INTERLACED is
                                                         false)
  InterleavedFields -> INTERLACED=true,  INTERLEAVED=true
  SeparateFields    -> INTERLACED=true,  INTERLEAVED=false

The render target (m_display_texture) is always single-sample at this
stage (line 1114 hardcodes VK_SAMPLE_COUNT_1_BIT regardless of MSAA);
the pipeline's multisample state is therefore single-sample on both
blobs, matching the original which did not call SetMultisamples either.
The MSAA-ness lives entirely in the input sampler type and the LoadVRAM
loop on the MSAA blob.

Bindings unchanged: push_constant for the UBO, set=0 binding=1 for the
source sampler, matching m_single_sampler_pipeline_layout.

The fixYCoord call from the original shadergen is dropped because the
Vulkan-side fixYCoord is the identity function (PSX VRAM Y matches
Vulkan Y); removing it spares a redundant function definition. The
GLSL-side helper macros (float4 / uint2 / etc) from WriteHeader are
also dropped since the .glsl files only need to work for Vulkan and
GLSL-native types are clearer than HLSL-style aliases.

Smoke tests:
  - g++ -fsyntax-only on the touched common TU.
  - spirv-val --target-env vulkan1.0 on both blobs (2391 / 2535
    words, 9.5K / 9.9K).
  - --target-env vulkan1.0 vs un-flagged glslangValidator: byte-
    identical SPIR-V for both blobs.

Like VRAM fill / copy / write, the display pipeline is built on its
own first-touch path and is not on the UpdateSettings SSAA-toggle
hot-build path. shader_precompile=Disabled is expected to behave
identically to Enabled / Lazy.

Test priority:
  - shader_precompile=Enabled / Lazy: routine smoke (any game).
  - shader_precompile=Disabled: any game that flips interlace_mode
    during gameplay (interlaced cutscenes -> progressive gameplay
    transitions) or that toggles 24bpp display mode (FMV intros in
    most JRPGs). Smooth-chroma path is exercised by enabling the
    Chroma Smoothing 24-bit option in the core options and watching
    an FMV. Worth doing a coarse SSAA round-trip too even though the
    display pipeline shouldn't be sensitive to it.
2026-05-18 00:02:50 +02:00

163 lines
5.4 KiB
GLSL

// Pre-baked Vulkan source for
// GPU_HW_ShaderGen::GenerateDisplayFragmentShader(depth_24bit,
// interlace_mode,
// smooth_chroma)
// when the source m_vram_texture is multisampled (sampler2DMS).
//
// See display.frag.glsl for the non-MSAA blob and the full rationale
// for the two-blob structural split. This file is identical to its
// non-MSAA twin in spec-constant interface and main-body logic; the
// only differences are:
//
// - samp0 is declared sampler2DMS instead of sampler2D.
// - LoadVRAM averages MULTISAMPLES texelFetch results instead of
// reading a single sample. The loop bound is a specialisation
// constant, so the driver unrolls and folds it at pipeline-create
// time when it specialises this blob for a concrete sample count.
// - An additional spec constant at constant_id=1 carries the
// MULTISAMPLES value (matches the project-wide convention used by
// the VRAM read / update-depth MSAA blobs).
//
// Spec constants used by this shader (a strict superset of the
// non-MSAA blob's set):
// constant_id = 0 RESOLUTION_SCALE (uint, common-knob convention).
// constant_id = 1 MULTISAMPLES (uint, common-knob convention).
// Drives the LoadVRAM averaging loop bound.
// constant_id = 100 DEPTH_24BIT (bool, shader-specific).
// constant_id = 101 INTERLACED (bool, shader-specific).
// constant_id = 102 INTERLEAVED (bool, shader-specific).
// constant_id = 103 SMOOTH_CHROMA (bool, shader-specific).
//
// Bindings (match m_single_sampler_pipeline_layout):
// push_constant - the UBO block below
// set=0 binding=1 - uniform sampler2DMS samp0 (the upscaled
// m_vram_texture's view in MSAA mode)
#version 450 core
layout(constant_id = 0) const uint RESOLUTION_SCALE = 1u;
layout(constant_id = 1) const uint MULTISAMPLES = 1u;
layout(constant_id = 100) const bool DEPTH_24BIT = false;
layout(constant_id = 101) const bool INTERLACED = false;
layout(constant_id = 102) const bool INTERLEAVED = false;
layout(constant_id = 103) const bool SMOOTH_CHROMA = false;
layout(push_constant) uniform PushConstants {
uvec2 u_vram_offset;
uint u_crop_left;
uint u_field_offset;
};
layout(set = 0, binding = 1) uniform sampler2DMS samp0;
// Unused; kept for interface-match with the screen-quad VS.
layout(location = 0) in VertexData {
vec2 v_tex0;
};
layout(location = 0) out vec4 o_col0;
vec3 RGBToYUV(vec3 rgb)
{
return vec3(dot(rgb, vec3( 0.299, 0.587, 0.114)),
dot(rgb, vec3(-0.14713, -0.28886, 0.436)),
dot(rgb, vec3( 0.615, -0.51499, -0.10001)));
}
vec3 YUVToRGB(vec3 yuv)
{
return vec3(dot(yuv, vec3(1.0, 0.0, 1.13983)),
dot(yuv, vec3(1.0, -0.39465, -0.58060)),
dot(yuv, vec3(1.0, 2.03211, 0.0)));
}
vec4 LoadVRAM(ivec2 coords)
{
vec4 value = texelFetch(samp0, coords, 0);
for (uint sample_index = 1u; sample_index < MULTISAMPLES; sample_index++)
value += texelFetch(samp0, coords, int(sample_index));
return value / float(MULTISAMPLES);
}
uint RGBA8ToRGBA5551(vec4 v)
{
uint r = uint(roundEven(v.r * 31.0));
uint g = uint(roundEven(v.g * 31.0));
uint b = uint(roundEven(v.b * 31.0));
uint a = (v.a != 0.0) ? 1u : 0u;
return r | (g << 5) | (b << 10) | (a << 15);
}
vec3 SampleVRAM24(uvec2 icoords)
{
uvec2 clamp_size = uvec2(1024u, 512u);
uvec2 vram_coords = u_vram_offset + uvec2((icoords.x * 3u) / 2u, icoords.y);
uint s0 = RGBA8ToRGBA5551(LoadVRAM(ivec2((vram_coords % clamp_size) * RESOLUTION_SCALE)));
uint s1 = RGBA8ToRGBA5551(LoadVRAM(ivec2(((vram_coords + uvec2(1u, 0u)) % clamp_size) * RESOLUTION_SCALE)));
uint s1s0 = ((s1 << 16u) | s0) >> ((icoords.x & 1u) * 8u);
return vec3(float( s1s0 & 0xFFu) / 255.0,
float((s1s0 >> 8u) & 0xFFu) / 255.0,
float((s1s0 >> 16u) & 0xFFu) / 255.0);
}
vec3 SampleVRAMAverage2x2(uvec2 icoords)
{
vec3 value = SampleVRAM24(icoords);
value += SampleVRAM24(icoords + uvec2(0u, 1u));
value += SampleVRAM24(icoords + uvec2(1u, 0u));
value += SampleVRAM24(icoords + uvec2(1u, 1u));
return value * 0.25;
}
vec3 SampleVRAM24Smoothed(uvec2 icoords)
{
ivec2 base = ivec2(icoords) - 1;
uvec2 low = uvec2(max(base & ~1, ivec2(0, 0)));
uvec2 high = low + 2u;
vec2 coeff = vec2(base & 1) * 0.5 + 0.25;
vec3 p = SampleVRAM24(icoords);
vec3 p00 = SampleVRAMAverage2x2(low);
vec3 p01 = SampleVRAMAverage2x2(uvec2(low.x, high.y));
vec3 p10 = SampleVRAMAverage2x2(uvec2(high.x, low.y));
vec3 p11 = SampleVRAMAverage2x2(high);
vec3 s = mix(mix(p00, p10, coeff.x),
mix(p01, p11, coeff.x),
coeff.y);
float y = RGBToYUV(p).x;
vec2 uv = RGBToYUV(s).yz;
return YUVToRGB(vec3(y, uv));
}
void main()
{
uvec2 VRAM_SIZE = uvec2(1024u, 512u) * RESOLUTION_SCALE;
uvec2 icoords = uvec2(gl_FragCoord.xy) + uvec2(u_crop_left, 0u);
if (INTERLACED)
{
if ((icoords.y & 1u) != u_field_offset)
discard;
if (INTERLEAVED)
icoords.y &= ~1u;
else
icoords.y /= 2u;
}
if (DEPTH_24BIT)
{
if (SMOOTH_CHROMA)
o_col0 = vec4(SampleVRAM24Smoothed(icoords), 1.0);
else
o_col0 = vec4(SampleVRAM24(icoords), 1.0);
}
else
{
o_col0 = vec4(LoadVRAM(ivec2((icoords + u_vram_offset) % VRAM_SIZE)).rgb, 1.0);
}
}