TsundereStation/data/shaders/vulkan/batch.vert.glsl
LibretroAdmin 078783e20b core/vulkan: drop the UV_LIMITS attribute-layout VS variant
Follow-up to 8b43518 (UV_LIMITS routing for the C++ shadergen,
D3D11 / D3D12 / OpenGL only). Vulkan has its own pre-baked
SPIR-V pipeline rooted in data/shaders/vulkan/*.glsl, compiled
via tools/regen_vulkan_spirv.py, and indexed by per-axis blob
tables in src/common/vulkan/embedded_shaders.cpp. This commit
parallels the C++ shadergen routing on the VS side: collapses
the UV_LIMITS attribute-layout variant from the batch VS so
that "textured" always means "textured with v_uv_limits", and
the C++ Vulkan input layout binds ATTR4 unconditionally when
textured.

The FS-side UV_LIMITS axis (the Nearest filter's two-variant
nouv / uv expansion) is NOT in this commit. Routing it
through a UBO field requires extending the Vulkan batch FS
UBO declaration with u_uv_limits, body-level runtime
branches, and regenerating the 48 Nearest FS .inc blobs. That
is the natural next commit; this one limits the blast radius
to the VS side, which is a simpler structural change with
fewer downstream touch points.

What changes:

  data/shaders/vulkan/batch.vert.glsl
    - Drops the #if defined(UV_LIMITS) guards in three sites
      (a_uv_limits input declaration, v_uv_limits varying
      output declaration, v_uv_limits assignment in main()).
      When TEXTURED, a_uv_limits / v_uv_limits are always
      declared and v_uv_limits is always written.
    - Header comment updated from "3 attribute layouts" to
      "2 attribute layouts" and the total variant count from
      18 to 12.

  tools/regen_vulkan_spirv.py
    - _batch_vs_variants(): drops the third attr_axes entry
      ("textured_uvlim", ["TEXTURED", "UV_LIMITS"]). The
      remaining "textured" entry's GLSL output now always
      includes the a_uv_limits / v_uv_limits declarations
      because the source-side #if guards are gone, so the
      single "textured" variant produces the same SPIR-V that
      "textured_uvlim" used to. Variant count drops from 18
      to 12.
    - The Nearest FS variants table is unchanged (still has
      the UV_LIMITS axis); collapsing that side is the
      follow-up.

  src/common/vulkan/embedded_spirv/
    - Six batch_vs_textured_uvlim_*.inc files DELETED (no
      longer in the regen output set).
    - Six batch_vs_textured_*.inc files REGENERATED with the
      new GLSL that always emits a_uv_limits / v_uv_limits.
      Content shape: same length / structure as the old
      "textured_uvlim" .inc files (since both correspond to
      the "textured + UV_LIMITS" build); the OLD plain
      "textured" .inc files (the smaller no-UV variant) are
      effectively replaced.
    - Other 169 blobs untouched. Total goes 181 -> 175.

  src/common/vulkan/embedded_shaders.{h,cpp}
    - k_batch_vs_blobs[18] -> k_batch_vs_blobs[12]. The
      table loses the "attr = 1 (textured, no UV limits)"
      block; the surviving "attr = 1" block is what used to
      be "attr = 2 (textured + UV limits)", just renamed for
      cleaner end state.
    - GetBatchVertexShaderBlob() drops the bool uv_limits
      parameter. Index encoding becomes
        attr = textured ? 1 : 0  (was: !textured ? 0 :
                                  (uv_limits ? 2 : 1))
      Same interp / persp arithmetic.
    - Header docstring and #include block updated to match
      (six fewer #includes; the six uvlim ones).

  src/core/gpu_hw_vulkan.cpp
    - The single GetBatchVertexShaderBlob() call site at the
      CompilePipelines VS-fill loop drops its second
      argument (m_using_uv_limits) so the new four-arg
      signature is satisfied.
    - The input-layout builder at the per-pipeline
      gpbuilder.AddVertexAttribute(4, ...) site drops its
      `if (m_using_uv_limits)` guard. ATTR4 / a_uv_limits is
      now bound unconditionally when textured, matching the
      always-emitted shader input. BatchVertex carries the
      uv_limits field unconditionally (gpu_hw.h:47), so the
      binding always points at valid memory; the FS-side
      u_uv_limits scalar still decides whether the value is
      meaningful.

What does NOT change in this commit:

  - The Vulkan batch FS Nearest variants still have
    UV_LIMITS as a build axis. m_using_uv_limits is still
    threaded to the FS blob lookup at
    gpu_hw_vulkan.cpp:2139 (now line ~2140 after the diff).
    The Nearest FS GLSL still has #if defined(UV_LIMITS)
    guards. The 48 nearest_uv / nearest_nouv blobs remain.
    Routing that side is the natural next commit; the
    delta there is much larger (48 blobs, FS UBO grows,
    runtime branch in the Nearest body).
  - m_using_uv_limits STAYS in shader_source_changed across
    backends. Vulkan still needs it to pick the right
    Nearest FS blob, so the rebuild trigger has to stay
    until the Nearest FS routing also lands.
  - m_using_uv_limits STAYS as a member field. Still
    drives the per-vertex ComputePolygonUVLimits decision
    (gpu_hw.cpp:740) and still passed into the FS variant
    lookup. The VS lookup no longer takes it.
  - Bilinear / JINC2 / xBR FS templates: untouched. They
    already assumed UV_LIMITS via the settings-layer
    ShouldUseUVLimits() coupling (TEXTURE_FILTERING !=
    Nearest implies m_using_uv_limits = true), so they
    never had a UV_LIMITS variant axis to collapse.

Variant-count delta:

  Batch VS: 18 -> 12 SPIR-V blobs. Pipeline cache footprint
  drops by 6 VS pipelines per (filter, render, texture)
  cell pair (filter side is per-FS so doesn't multiply VS
  directly, but the VkPipeline cache key combines them and
  6 fewer VS blobs mean 6 fewer paths through the
  pipeline-create call).

Disk SPIR-V footprint:
  Before: 181 .inc files, 13 MiB total.
  After:  175 .inc files, 13 MiB - ~120 KiB = ~12.9 MiB.
  (Six VS blobs at ~120 KiB each, ~2-2.5 KiB SPIR-V plus
  the C-array boilerplate.)

Cost matrix delta (Vulkan):

  UV_LIMITS flip mid-session (in practice triggered by PGXP
  toggle or filter cross-Nearest transition, both of which
  separately trigger m_using_uv_limits flips):
    Pre-commit: VS pipeline-cache lookup picks a different
                blob from the 18-entry table on flip.
                Pipeline creation cost on cold cache:
                ~2 vkCreateGraphicsPipelines for the two
                new (textured, textured_uvlim) VS variants
                if both end up needed.
    Post-commit: VS pipeline-cache lookup is invariant
                 under m_using_uv_limits at the VS axis;
                 the FS axis still flips. Saves ~2 cold VS
                 pipeline creates on the toggle. Symbolic
                 (small absolute cost) but it unlocks the
                 next Nearest-FS routing commit, which is
                 the actual large win on the Vulkan side.

  Cold launch (precompile pass): 6 fewer VS pipelines to
  enumerate / create. Probably tens of ms on cold cache.

  Steady-state runtime cost: unchanged. The VS uniform
  always-emits-v_uv_limits adds one MUL per vertex and one
  vec4 of interpolant bandwidth, both sub-noise.

Test:
  - Build: tools/regen_vulkan_spirv.py runs cleanly with
    glslangValidator from the Vulkan SDK / glslang-tools
    package, producing 175 blobs. The Vulkan backend builds
    and links against the smaller blob table.
  - Boot any PSX title with PGXP off + filter Nearest:
    untextured / textured paths render identically. The
    "textured" VS variant is now the always-UV blob;
    BatchVertex.uv_limits is zero on these vertices (no
    ComputePolygonUVLimits ran), v_uv_limits is propagated
    as zero, and the Nearest FS (still UV_LIMITS-keyed in
    this commit) picks its "nouv" variant.
  - Toggle PGXP on or filter to Bilinear:
    m_using_uv_limits flips to true, the Nearest FS picks
    its "uv" variant, the VS variant is still the same
    (post-commit there's only one textured VS variant).
    Output identical to pre-commit equivalent state.
  - Spec const flips (PGXP_DEPTH, RESOLUTION_SCALE) at
    pipeline-create time: still produce the right pipeline
    via the smaller blob table.

Next:
  - Vulkan Nearest FS UV_LIMITS routing: extend Vulkan FS
    UBO with u_uv_limits, drop the #if defined(UV_LIMITS)
    guards from batch_textured_nearest.frag.glsl, runtime
    branch in the body, regen 48 -> 24 Nearest FS blobs,
    drop m_using_uv_limits from the FS lookup +
    shader_source_changed. After that, all backends have
    UV_LIMITS fully routed.
  - PGXP_DEPTH for the FS (always-emit SV_Depth / FragDepth
    perf measurement first).
  - Pre-bake commits once remaining axes are decided.
2026-05-19 02:54:36 +02:00

154 lines
6.4 KiB
GLSL

// Pre-baked Vulkan source template for
// GPU_HW_ShaderGen::GenerateBatchVertexShader.
//
// Batch VS is the first of the two batch shaders, and its state space
// has THREE structurally distinct axes that SPIR-V decorations cannot
// be specialised across:
//
// - Vertex attribute layout. 2 input attributes (untextured) or 5
// (textured: pos, col, texcoord, texpage, uv_limits). UV_LIMITS
// used to be a third attribute-layout variant gating a_uv_limits;
// it is now routed via the FS-side u_uv_limits cbuffer scalar so
// the VS unconditionally emits a_uv_limits + v_uv_limits when
// textured. The 'in <type> <name>' declarations are part of the
// SPIR-V module interface, not a spec-constant-controlled knob.
//
// - Output interpolation qualifier. The fragment shader pairs with
// these and the qualifiers compile to OpMemberDecorate Sample /
// Centroid in the output VertexData block. There are three
// mutually exclusive states (no qualifier, centroid, sample),
// selected per session by UsingMSAA() and UsingPerSampleShading().
//
// - Color output perspective. The 'noperspective' qualifier on
// v_col0 compiles to OpMemberDecorate NoPerspective, again not
// spec-constant-controllable. Two states, selected per session by
// m_disable_color_perspective.
//
// Total structural variant count: 2 (attribute layouts) x 3
// (interpolation) x 2 (perspective) = 12. Body-level knobs that do
// not affect decorations (PGXP_DEPTH, RESOLUTION_SCALE) are handled
// as specialisation constants on every blob.
//
// This file is the SINGLE GLSL source. The regen tool compiles it
// twelve times with different -D combinations to produce the
// matching .inc blobs; the manifest of variants lives in
// tools/regen_vulkan_spirv.py. The C++ side picks one of the 12 at
// pipeline-create time via the helper in embedded_shaders.cpp.
//
// Spec constants (every blob):
// constant_id = 0 RESOLUTION_SCALE (uint, common-knob convention).
// Drives the 1x-resolution vertex-offset gate, the
// upscaled texcoord scaling, and the texpage
// base-coordinate scaling.
// constant_id = 3 PGXP_DEPTH (bool, common-knob convention).
// Selects between mask-bit Z (a_pos.z) and the
// PGXP-replayed perspective-correct depth
// (a_pos.w).
//
// Bindings: vertex inputs only - no descriptor sets, no push
// constants. The pipeline layout is m_batch_pipeline_layout which
// declares one UBO at set=0 binding=0 for the FS; the VS does not
// read it.
#version 450 core
// ---- Specialisation constants (body-level knobs) -------------------
layout(constant_id = 0) const uint RESOLUTION_SCALE = 1u;
layout(constant_id = 3) const bool PGXP_DEPTH = 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
// ---- Vertex inputs (attribute layout axis) -------------------------
layout(location = 0) in vec4 a_pos;
layout(location = 1) in vec4 a_col0;
#if defined(TEXTURED)
layout(location = 2) in uint a_texcoord;
layout(location = 3) in uint a_texpage;
// a_uv_limits emitted unconditionally when TEXTURED. UV_LIMITS used
// to gate this declaration, matching a separate attribute layout
// variant; routed via the FS-side u_uv_limits cbuffer scalar now,
// so the VS just always passes the value through to v_uv_limits and
// the FS decides at runtime whether to consume it. BatchVertex
// always carries the uv_limits field (gpu_hw.h:47), so the C++
// input layout always has a valid offset to bind ATTR4 to - the
// values are zero when ComputePolygonUVLimits didn't run and the
// FS short-circuits on u_uv_limits in that case.
layout(location = 4) in vec4 a_uv_limits;
#endif
// ---- Vertex outputs (interpolation + perspective axis) -------------
layout(location = 0) out VertexData {
COLOR_INTERP vec4 v_col0;
#if defined(TEXTURED)
INTERP vec2 v_tex0;
flat uvec4 v_texpage;
flat vec4 v_uv_limits;
#endif
};
// --------------------------------------------------------------------
void main()
{
// 1x-resolution sub-pixel offset matches the shadergen original;
// the >1x case has the offset pre-applied in CPU-side vertex prep,
// so the shader uses 0 there.
float vertex_offset = (RESOLUTION_SCALE == 1u) ? 0.5 : 0.0;
// PSX VRAM addressing is 0..1023 horizontally, mapped to NDC -1..+1.
float pos_x = ((a_pos.x + vertex_offset) / 512.0) - 1.0;
float pos_y = ((a_pos.y + vertex_offset) / -256.0) + 1.0;
// Vulkan flips Y vs GL/D3D; shadergen does this unconditionally
// under #if API_VULKAN. We are Vulkan-only here so it is also
// unconditional.
pos_y = -pos_y;
// Depth source selection. With PGXP enabled and the depth-buffer
// option on, the PGXP path replays a perspective-correct depth in
// a_pos.w and the mask-bit Z slot (a_pos.z) is ignored. Otherwise
// use the conventional mask-bit Z.
float pos_z = PGXP_DEPTH ? a_pos.w : a_pos.z;
float pos_w = a_pos.w;
gl_Position = vec4(pos_x * pos_w, pos_y * pos_w, pos_z * pos_w, pos_w);
v_col0 = a_col0;
#if defined(TEXTURED)
// Texture coordinates are packed into a single uint per vertex
// (lower 16 bits = U, upper 16 bits = V) and scaled by the upscale
// factor to address into the upscaled VRAM atlas.
v_tex0 = vec2(float((a_texcoord & 0xFFFFu) * RESOLUTION_SCALE),
float((a_texcoord >> 16) * RESOLUTION_SCALE));
// a_texpage is similarly packed:
// bits 0..3 page base X (in 64-texel native units)
// bit 4 page base Y (256-texel native unit, 0 or 1)
// bits 16..21 CLUT X (in 16-texel native units)
// bits 22..30 CLUT Y (native rows)
v_texpage.x = ( a_texpage & 15u ) * 64u * RESOLUTION_SCALE;
v_texpage.y = ((a_texpage >> 4) & 1u ) * 256u * RESOLUTION_SCALE;
v_texpage.z = ((a_texpage >> 16) & 63u ) * 16u * RESOLUTION_SCALE;
v_texpage.w = ((a_texpage >> 22) & 511u) * RESOLUTION_SCALE;
// UV limits are sent normalised; expand to 0..255 PSX-native here.
// Always written when textured (UV_LIMITS guard dropped at the
// collapse commit); the FS-side u_uv_limits gates whether to
// consume the value.
v_uv_limits = a_uv_limits * vec4(255.0, 255.0, 255.0, 255.0);
#endif
}