TsundereStation/data/shaders/vulkan/batch_textured_nearest.frag.glsl
LibretroAdmin c6e9f87be0 core/vulkan: route the Nearest FS UV_LIMITS axis to a cbuffer scalar
Follow-up to 078783e (Vulkan VS UV_LIMITS collapse) and the
earlier 8b43518 (C++ shadergen UV_LIMITS routing). Brings the
Vulkan Nearest FS into the same shape as the C++ shadergen
routing - UV_LIMITS is no longer a SPIR-V-structural axis on
the Vulkan side; the Nearest FS source has a single shape and
runtime-branches on u_uv_limits.

With this commit m_using_uv_limits is no longer a shader-
source-changed trigger for any backend, so it leaves the
rebuild-trigger predicates entirely. It remains as a member
field that drives the per-vertex ComputePolygonUVLimits CPU
optimisation (gpu_hw.cpp:740) - that's an orthogonal concern
from shader / pipeline routing.

What changes:

  data/shaders/vulkan/batch_textured_nearest.frag.glsl
    - Adds u_uv_limits to the BatchUBOData declaration with
      an explicit layout(offset = 56) (skips past the spec-
      const-handled fields between offset 32 and 55, which
      Vulkan does not redeclare in its UBO because per-
      session settings are spec consts on this path -
      RESOLUTION_SCALE, TRUE_COLOR, SCALED_DITHERING,
      DITHERING, INTERLACING, PGXP_DEPTH). Explicit offset
      via GL_ARB_enhanced_layouts is core in GLSL 4.40+ and
      supported by Vulkan GLSL.
    - v_uv_limits input declaration drops its
      #if defined(UV_LIMITS) guard - always declared,
      matching the always-emitted output from the
      post-collapse batch VS.
    - Body sample/clamp block: the two-branch
      #if UV_LIMITS / #else compile-time fork collapses to
      a single shape with a runtime `if (u_uv_limits != 0u)`
      branch. The "scale uv_limits up by RESOLUTION_SCALE
      for non-palette modes" computation moves inside the
      branch so it only runs when the values are actually
      used.
    - Header comment: 5 axes / 48 blobs -> 4 axes / 24 blobs.

  tools/regen_vulkan_spirv.py
    - _batch_fs_textured_nearest_variants() drops the
      uv_axes dimension. The Nearest cube is now in parity
      shape with the Bilinear / JINC2 / xBR templates
      (UV_LIMITS implicit on those by ShouldUseUVLimits()
      coupling - TEXTURE_FILTERING != Nearest forces
      m_using_uv_limits true, so they never needed a UV
      variant axis).

  src/common/vulkan/embedded_spirv/
    - 24 new batch_textured_nearest_fs_*.inc files (no
      _nouv / _uv suffix) with the collapsed-shape SPIR-V.
    - 48 deleted batch_textured_nearest_fs_*_nouv.inc and
      _uv.inc files (the obsolete two-shape variants).
    - 151 .inc files in the directory total (was 175;
      the VS collapse already took it from 181).

  src/common/vulkan/embedded_shaders.{h,cpp}
    - k_batch_textured_nearest_fs_blobs[48] ->
      k_batch_textured_nearest_fs_blobs[24]. The table loses
      the alternating _nouv / _uv pairs; the surviving
      entries match the new filename layout.
    - GetBatchTexturedNearestFragmentShaderBlob drops the
      bool uv_limits parameter. Index encoding becomes
        index = interp*8 + persp*4 + dual*2 + pgxp  (0..23)
      (was: interp*16 + persp*8 + dual*4 + pgxp*2 + uv).
    - 48 #include lines collapse to 24, matching the
      regenerated filename set.
    - Header docstring updated to match (5 axes -> 4, blob
      array size, UV_LIMITS note added).

  src/core/gpu_hw_vulkan.cpp
    - The single GetBatchTexturedNearestFragmentShaderBlob
      call site (the m_texture_filter == Nearest branch of
      the per-pipeline FS lookup) drops its sixth argument
      (m_using_uv_limits). Bilinear / JINC2 / xBR call sites
      are unchanged - those helpers never had uv_limits as
      an argument because their templates already had
      UV_LIMITS implicit.

  src/core/gpu_hw.cpp
    - non_dim_diff in UpdateHWSettings drops the
      m_using_uv_limits != use_uv_limits clause.
    - shader_source_changed in UpdateHWSettings drops the
      same clause.
    - display_only_source_changed's other_source_diff
      drops it too.
    All three trigger predicates are now uv_limits-blind.
    m_using_uv_limits flips no longer invalidate the shader
    cache or PSO matrix on any backend.
    m_using_uv_limits itself stays as a member field -
    UpdateHWSettings still computes use_uv_limits via
    ShouldUseUVLimits() and assigns it for the runtime
    ComputePolygonUVLimits CPU-side decision and for the
    u_uv_limits cbuffer push - but it no longer triggers
    rebuilds.

What this completes:

  The UV_LIMITS axis is now fully routed across all four
  backends.
    D3D11 / D3D12 / OpenGL (C++ shadergen path):
      - Shader source invariant under m_uv_limits since
        8b43518.
      - Input layout binds ATTR4 unconditionally when
        textured (D3D11 / D3D12 since 8b43518; OpenGL was
        already unconditional).
    Vulkan (pre-baked SPIR-V path):
      - VS shape collapsed since 078783e (12 VS blobs).
      - Nearest FS shape collapsed in this commit (24 FS
        blobs).
      - Bilinear / JINC2 / xBR templates unchanged - they
        never had UV_LIMITS as an axis (settings-coupling
        guarantee).
      - Vulkan FS UBO grows by one field (u_uv_limits at
        offset 56); other Vulkan FS templates that don't
        read UV limits don't need to redeclare it - they
        just ignore the extra bytes in the backing buffer.
    All backends:
      - m_using_uv_limits is no longer a rebuild trigger.

Cost matrix delta:

  Per-session flip that touches m_using_uv_limits (e.g.
  PGXP on / off under filter Nearest, or filter cross-
  Nearest transition):
    Pre-commit (Vulkan only): 24 Nearest FS pipelines per
      cell rebuilt to swap from _nouv to _uv variants.
      Tens of milliseconds cold, instant with disk
      pipeline cache.
    Post-commit: 0 FS rebuilds across all backends. The
      u_uv_limits cbuffer write picks up at next
      FlushRender.

  Cold launch precompile pass on Vulkan: 24 fewer Nearest
  FS pipelines to enumerate. Tens of ms on cold cache.

  Steady-state runtime cost (Vulkan Nearest FS):
    + 4-byte UBO read on every fragment.
    + One branch on u_uv_limits (sub-coherent across the
      warp because u_uv_limits is uniform).
    + The "scale uv_limits up by RESOLUTION_SCALE" math
      moved inside the branch, so when u_uv_limits == 0
      that work is also skipped - net cost reduction on
      the non-UV path.

Disk footprint:
  Before:  175 .inc files, ~12.9 MiB.
  After:   151 .inc files, ~11.3 MiB. (-24 files, ~1.6
           MiB. The 24 new files are larger than the 24
           old _nouv files they implicitly replace because
           they always emit the v_uv_limits flat input
           declaration and the body branch.)

Test:
  - Build: tools/regen_vulkan_spirv.py runs cleanly,
    producing 151 blobs. Embedded_shaders.cpp compiles
    against the smaller table. gpu_hw_vulkan.cpp link is
    clean.
  - Boot any PSX title with PGXP off + filter Nearest:
    u_uv_limits = 0, the FS branch takes the no-clamp
    path. Output identical to pre-commit.
  - Toggle PGXP on with filter Nearest: u_uv_limits flips
    to 1 via the existing UpdateHWSettings cbuffer push;
    the FS picks the clamp path on next FlushRender. No
    pipeline rebuild fired.
  - Toggle filter to Bilinear: m_texture_filtering changes,
    which still fires shader_source_changed (filter is a
    distinct rebuild trigger). The Bilinear FS picks up
    its template normally, which always uses UV limits
    (settings coupling guarantee).
  - Cross-test on non-Vulkan backends: D3D11 / D3D12 /
    OpenGL also no longer rebuild on UV_LIMITS flip alone
    (the trigger drop in gpu_hw.cpp affects them too).
    Their shader sources were already invariant since
    8b43518, so the only behaviour change is the
    rebuild-trigger removal.

Next:
  - PGXP_DEPTH for the FS (always-emit SV_Depth / FragDepth
    perf measurement first - the FS keeps captured
    m_pgxp_depth because the !m_pgxp_depth value gates
    DeclareFragmentEntryPoint's depth_output parameter,
    which controls whether SV_Depth is declared in the FS
    entry signature; routing that requires either always-
    emitting SV_Depth or hoisting that decision through
    the DeclareFragmentEntryPoint surface).
  - per_sample / dcp collapse decisions (forced-always
    via settings or keep as variant axes).
  - Pre-bake the resulting reduced D3D12 FS matrix.
2026-05-19 03:07:27 +02:00

356 lines
13 KiB
GLSL

// Pre-baked Vulkan source template for the TEXTURED + NEAREST-FILTER
// slice of GPU_HW_ShaderGen::GenerateBatchFragmentShader (texture_mode
// != GPUTextureMode::Disabled AND m_texture_filter ==
// GPUTextureFilter::Nearest). Subsequent patches add equivalent
// templates for Bilinear / BilinearBinAlpha, JINC2 / JINC2BinAlpha,
// and xBR / xBRBinAlpha; the remaining "is m_texture_filter Nearest"
// gate in GetBatchFragmentShader on the C++ side picks this template
// for nearest-only sessions.
//
// Four SPIR-V-structural axes for this template:
//
// - Input interpolation qualifier (none / centroid / sample). 3.
// - Color input perspective (standard / noperspective). 2.
// - Dual-source output (1 vs 2 outputs). 2.
// - PGXP depth output (writes gl_FragDepth or omits it). 2.
//
// 3 x 2 x 2 x 2 = 24 blobs.
//
// UV_LIMITS used to be a fifth axis (without / with v_uv_limits flat
// input) but has been collapsed to a runtime branch on the
// u_uv_limits cbuffer scalar - v_uv_limits is now always declared
// (the batch VS always emits it when textured, see the matching VS
// collapse) and consumed iff u_uv_limits != 0 at runtime.
//
// Per-call specialisation constants (collapse onto each blob):
//
// id = 0 RESOLUTION_SCALE (uint, per-session)
// id = 100 TRANSPARENCY (bool)
// id = 101 TRANSPARENCY_ONLY_OPAQUE (bool)
// id = 102 TRANSPARENCY_ONLY_TRANSPARENT (bool)
// id = 103 DITHERING (bool)
// id = 104 INTERLACING (bool)
// id = 105 DITHERING_SCALED (bool, per-session)
// id = 106 TRUE_COLOR (bool, per-session)
// id = 107 PALETTE_4_BIT (bool, derived from
// texture_mode actual mode)
// id = 108 PALETTE_8_BIT (bool, derived from
// texture_mode actual mode)
// id = 109 RAW_TEXTURE (bool, RawTextureBit set
// in texture_mode)
//
// PALETTE is derived inside the shader as PALETTE_4_BIT ||
// PALETTE_8_BIT; only at most one of the two is true for any draw.
// The texture_mode encoding is described in src/core/gpu_types.h.
//
// Bindings: pipeline layout m_batch_pipeline_layout. UBO at set=0
// binding=0 supplies BatchUBOData (texture window + transparency +
// interlacing). Combined-image-sampler at set=0 binding=1 is the
// upscaled m_vram_read_texture, single-sample (texture sampling
// always reads the non-multisampled shadow VRAM regardless of
// m_multisamples).
#version 450 core
// ---- Specialisation constants --------------------------------------
layout(constant_id = 0) const uint RESOLUTION_SCALE = 1u;
layout(constant_id = 100) const bool TRANSPARENCY = false;
layout(constant_id = 101) const bool TRANSPARENCY_ONLY_OPAQUE = false;
layout(constant_id = 102) const bool TRANSPARENCY_ONLY_TRANSPARENT = false;
layout(constant_id = 103) const bool DITHERING = false;
layout(constant_id = 104) const bool INTERLACING = false;
layout(constant_id = 105) const bool DITHERING_SCALED = false;
layout(constant_id = 106) const bool TRUE_COLOR = false;
layout(constant_id = 107) const bool PALETTE_4_BIT = false;
layout(constant_id = 108) const bool PALETTE_8_BIT = false;
layout(constant_id = 109) const bool RAW_TEXTURE = false;
// ---- Interpolation qualifier macros --------------------------------
#if defined(INTERP_SAMPLE)
# define INTERP sample
#elif defined(INTERP_CENTROID)
# define INTERP centroid
#else
# define INTERP
#endif
#if defined(NOPERSP)
# define COLOR_INTERP noperspective INTERP
#else
# define COLOR_INTERP INTERP
#endif
// ---- Batch UBO -----------------------------------------------------
// First six fields mirror the C++ BatchUBOData declaration head
// (gpu_hw.h:111) verbatim. The std140 offsets are 0..31. The trailing
// C++ struct fields between offset 32 and 55 (u_resolution_scale,
// u_true_color, u_scaled_dithering, u_dithering, u_interlacing,
// u_pgxp_depth) are handled on the Vulkan path via specialisation
// constants on every pipeline blob and so are not redeclared here.
// u_uv_limits at offset 56 is read at runtime; the explicit
// layout(offset=56) is the GL_ARB_enhanced_layouts core-since-4.40
// way to skip past the spec-const-handled fields without redeclaring
// them.
layout(std140, set = 0, binding = 0) uniform BatchUBOData {
uvec2 u_texture_window_and;
uvec2 u_texture_window_or;
float u_src_alpha_factor;
float u_dst_alpha_factor;
uint u_interlaced_displayed_field;
bool u_set_mask_while_drawing;
layout(offset = 56) uint u_uv_limits;
};
// ---- VRAM atlas sampler --------------------------------------------
layout(set = 0, binding = 1) uniform sampler2D samp0;
// ---- Inputs from the batch VS --------------------------------------
// v_uv_limits is always declared - the batch VS always emits it when
// textured (post-UV_LIMITS-collapse, see batch.vert.glsl). Whether to
// consume it for clamping is a runtime branch on u_uv_limits below.
layout(location = 0) in VertexData {
COLOR_INTERP vec4 v_col0;
INTERP vec2 v_tex0;
flat uvec4 v_texpage;
flat vec4 v_uv_limits;
};
// ---- Outputs -------------------------------------------------------
layout(location = 0, index = 0) out vec4 o_col0;
#if defined(DUAL_SOURCE)
layout(location = 0, index = 1) out vec4 o_col1;
#endif
// ---- Dithering matrix ----------------------------------------------
const int s_dither_values[16] = int[16](
-4, 0, -3, 1,
2, -2, 3, -1,
-3, 1, -4, 0,
3, -1, 2, -2
);
uvec3 ApplyDithering(uvec2 coord, uvec3 icol)
{
uvec2 fc = DITHERING_SCALED
? (coord & uvec2(3u, 3u))
: ((coord / RESOLUTION_SCALE) & uvec2(3u, 3u));
int offset = s_dither_values[fc.y * 4u + fc.x];
if (TRUE_COLOR)
return uvec3(clamp(ivec3(icol) + ivec3(offset, offset, offset), 0, 255));
else
return uvec3(clamp((ivec3(icol) + ivec3(offset, offset, offset)) >> 3, 0, 31));
}
// ---- Texture-window helpers ----------------------------------------
uvec2 ApplyTextureWindow(uvec2 coords)
{
uint x = (coords.x & u_texture_window_and.x) | u_texture_window_or.x;
uint y = (coords.y & u_texture_window_and.y) | u_texture_window_or.y;
return uvec2(x, y);
}
uvec2 ApplyUpscaledTextureWindow(uvec2 coords)
{
uvec2 native_coords = coords / RESOLUTION_SCALE;
uvec2 coords_offset = coords % RESOLUTION_SCALE;
return (ApplyTextureWindow(native_coords) * RESOLUTION_SCALE) + coords_offset;
}
uvec2 FloatToIntegerCoords(vec2 coords)
{
// 1x-resolution-scale: round to nearest texel (the +0.5 vertex offset
// from the batch VS aligned us on a texel centre). >1x: floor, since
// the vertex offset is pre-applied CPU-side and we want the bottom-
// left of the upscaled cell.
return uvec2((RESOLUTION_SCALE == 1u) ? roundEven(coords) : floor(coords));
}
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);
}
// ---- VRAM atlas sample ---------------------------------------------
vec4 SampleFromVRAM(uvec4 texpage, vec2 coords)
{
const vec2 rcp_vram_size = vec2(1.0) / vec2(uvec2(1024u, 512u) * RESOLUTION_SCALE);
bool palette = PALETTE_4_BIT || PALETTE_8_BIT;
if (palette)
{
// 4-bit and 8-bit paletted: address is 4 or 2 native-texel-wide
// packed cells in the VRAM atlas; the per-subpixel index pulls the
// 4- or 8-bit palette entry from that cell.
uvec2 icoord = ApplyTextureWindow(FloatToIntegerCoords(coords));
uvec2 index_coord = icoord;
if (PALETTE_4_BIT)
index_coord.x /= 4u;
else // PALETTE_8_BIT
index_coord.x /= 2u;
uvec2 vicoord = uvec2(texpage.x + index_coord.x * RESOLUTION_SCALE,
texpage.y + index_coord.y * RESOLUTION_SCALE);
vec4 texel = texture(samp0, vec2(vicoord) * rcp_vram_size);
uint vram_value = RGBA8ToRGBA5551(texel);
uint palette_index;
if (PALETTE_4_BIT)
{
uint subpixel = icoord.x & 3u;
palette_index = (vram_value >> (subpixel * 4u)) & 0x0Fu;
}
else // PALETTE_8_BIT
{
uint subpixel = icoord.x & 1u;
palette_index = (vram_value >> (subpixel * 8u)) & 0xFFu;
}
uvec2 palette_icoord = uvec2(texpage.z + (palette_index * RESOLUTION_SCALE),
texpage.w);
return texture(samp0, vec2(palette_icoord) * rcp_vram_size);
}
else
{
// Direct 16bpp - render-to-texture effects, upscaled VRAM coords.
uvec2 icoord = ApplyUpscaledTextureWindow(FloatToIntegerCoords(coords));
uvec2 direct_icoord = uvec2(texpage.x + icoord.x, texpage.y + icoord.y);
return texture(samp0, vec2(direct_icoord) * rcp_vram_size);
}
}
// --------------------------------------------------------------------
void main()
{
uvec3 vertcol = uvec3(v_col0.rgb * vec3(255.0, 255.0, 255.0));
// INTERLACING field discard (fixYCoord is identity on Vulkan).
if (INTERLACING)
{
if ((uint(gl_FragCoord.y) & 1u) == u_interlaced_displayed_field)
discard;
}
// Texture coords. Palette textures index the VRAM atlas with native-
// resolution coords; direct-16bpp textures use upscaled coords.
bool palette = PALETTE_4_BIT || PALETTE_8_BIT;
vec2 coords = v_tex0;
if (palette)
coords /= float(RESOLUTION_SCALE);
// UV-clamping path. Pre-UV_LIMITS-routing this was gated by the
// compile-time UV_LIMITS macro. Now u_uv_limits is a runtime
// cbuffer scalar - 1 when PGXP is on (under any filter) OR the
// texture filter is non-Nearest (which can't happen in this
// Nearest template, so for this FS u_uv_limits is effectively
// "is PGXP on"). When u_uv_limits is 0, the clamping path is
// skipped and v_uv_limits contents (potentially zero, since
// ComputePolygonUVLimits didn't run on the vertex) are not read.
vec4 texcol;
if (u_uv_limits != 0u)
{
vec4 uv_limits = v_uv_limits;
if (!palette)
{
// Direct-16bpp: extend the UV range to all upscaled pixels so 1-
// pixel-high polygon-based framebuffer effects (e.g. MegaMan Legends
// 2 haze) are not downsampled.
uv_limits = uv_limits * float(RESOLUTION_SCALE);
uv_limits.zw += float(RESOLUTION_SCALE - 1u);
}
texcol = SampleFromVRAM(v_texpage, clamp(coords, uv_limits.xy, uv_limits.zw));
}
else
{
texcol = SampleFromVRAM(v_texpage, coords);
}
// Transparent-texel discard: VRAM rgba 0000h is the "no draw" sentinel.
if (all(equal(texcol, vec4(0.0, 0.0, 0.0, 0.0))))
discard;
float ialpha = 1.0;
bool semitransparent = (texcol.a >= 0.5);
uvec3 icolor;
if (!TRUE_COLOR)
{
icolor = uvec3(texcol.rgb * vec3(255.0, 255.0, 255.0)) >> 3;
if (!RAW_TEXTURE)
{
icolor = (icolor * vertcol) >> 4;
if (DITHERING)
icolor = ApplyDithering(uvec2(gl_FragCoord.xy), icolor);
else
icolor = min(icolor >> 3, uvec3(31u, 31u, 31u));
}
}
else
{
icolor = uvec3(texcol.rgb * vec3(255.0, 255.0, 255.0));
if (!RAW_TEXTURE)
{
icolor = (icolor * vertcol) >> 7;
if (DITHERING)
icolor = ApplyDithering(uvec2(gl_FragCoord.xy), icolor);
else
icolor = min(icolor, uvec3(255u, 255u, 255u));
}
}
// Mask bit: textured polygons mask if either u_set_mask_while_drawing
// is set or the texel was semitransparent.
float oalpha = u_set_mask_while_drawing ? 1.0 : (semitransparent ? 1.0 : 0.0);
// Premultiplied colour.
float premultiply_alpha = TRANSPARENCY
? (ialpha * (semitransparent ? u_src_alpha_factor : 1.0))
: ialpha;
vec3 color = TRUE_COLOR
? ((vec3(icolor) * premultiply_alpha) / vec3(255.0, 255.0, 255.0))
: (floor(vec3(icolor) * premultiply_alpha) / vec3(31.0, 31.0, 31.0));
// Output. Textured path differs from untextured in that the
// TRANSPARENCY branch is the "TEXTURED && TRANSPARENCY" branch in
// the shadergen, which also drives the per-texel discard logic for
// TRANSPARENCY_ONLY_OPAQUE / TRANSPARENCY_ONLY_TRANSPARENT.
if (TRANSPARENCY)
{
if (semitransparent)
{
o_col0 = vec4(color, oalpha);
#if defined(DUAL_SOURCE)
o_col1 = vec4(0.0, 0.0, 0.0, u_dst_alpha_factor / ialpha);
#endif
if (TRANSPARENCY_ONLY_OPAQUE)
discard;
}
else
{
o_col0 = vec4(color, oalpha);
#if defined(DUAL_SOURCE)
o_col1 = vec4(0.0, 0.0, 0.0, 1.0 - ialpha);
#endif
if (TRANSPARENCY_ONLY_TRANSPARENT)
discard;
}
}
else
{
o_col0 = vec4(color, oalpha);
#if defined(DUAL_SOURCE)
o_col1 = vec4(0.0, 0.0, 0.0, 1.0 - ialpha);
#endif
}
#if !defined(PGXP_DEPTH)
gl_FragDepth = oalpha * gl_FragCoord.z;
#endif
}