Patch 12 in the glslang-elimination series, and the first step into
Bucket B (batch shaders). The batch VS state space has three SPIR-V-
structurally-distinct axes that specialisation constants cannot fold:
1. Vertex attribute layout. 2 inputs (untextured), 4 (textured
without UV limits) or 5 (textured with UV limits). The 'in <type>
<name>' declarations are part of the SPIR-V module interface.
2. Output interpolation qualifier on the v_col0 / v_tex0 outputs.
None / centroid / sample, selected per session by UsingMSAA() /
UsingPerSampleShading(). These compile to OpMemberDecorate
Centroid / Sample on the VertexData output block, which is not
a spec-constant-controllable property of the SPIR-V module.
3. Color output perspective. m_disable_color_perspective toggles a
'noperspective' qualifier on v_col0, compiling to
OpMemberDecorate NoPerspective.
3 x 3 x 2 = 18 structurally distinct blobs. Body-level knobs
(RESOLUTION_SCALE, PGXP_DEPTH) are common-knob specialisation
constants on every blob (constant_id = 0 and 3 per the project
convention).
To avoid maintaining 18 hand-edited .glsl files, the source is a
single template (data/shaders/vulkan/batch.vert.glsl) with #if
guards on TEXTURED, UV_LIMITS, INTERP_CENTROID / INTERP_SAMPLE, and
NOPERSP. The regen tool (tools/regen_vulkan_spirv.py) is extended
with a TEMPLATE_VARIANTS manifest that compiles the template once
per variant with the corresponding -D flags. Build-time impact is
zero - the script is still developer-only and the .inc files are
checked in.
The matching index encoding lives in src/common/vulkan/embedded_-
shaders.cpp next to the lookup helper:
attr = !textured ? 0 : (uv_limits ? 2 : 1) (0..2)
interp = per_sample_shading ? 2 : (msaa ? 1 : 0) (0..2)
persp = noperspective_color ? 1 : 0 (0..1)
index = attr * 6 + interp * 2 + persp (0..17)
The C++ side calls GetBatchVertexShaderBlob with the per-session
state (m_using_uv_limits, m_multisamples > 1, m_per_sample_shading,
m_disable_color_perspective) and the per-call 'textured' loop
variable to pick the right blob in CompilePipelines. The existing
m_batch_vertex_shaders[2] cache shape is preserved - the helper just
selects which two of the 18 blobs to instantiate this session.
Spec constants are applied at PIPELINE-create time inside
GetBatchPipeline rather than at module-create time, matching the
SpecConstants infrastructure laid down in patch 3. The values are
session-constant, so they are the same for every batch pipeline in
a session and the driver caches them keyed by (module + spec values)
in the pipeline cache.
Header-side ergonomics: instead of declaring 36 individual extern
arrays (18 SPIR-V + 18 size), the .h declares a small
EmbeddedShaderBlob struct and a single
k_batch_vs_blobs[18] array, plus the GetBatchVertexShaderBlob
helper. The same struct is intended to be reused for the eventual
batch FS conversion (likely many more blobs given the texture filter
state space).
Smoke tests:
- g++ -fsyntax-only on the touched common TU.
- spirv-val --target-env vulkan1.0 on all 18 blobs - 18/18 valid.
- Total .inc size: ~53 KiB across all 18 blobs.
- --target-env vulkan1.0 vs un-flagged glslangValidator: byte-
identical for every variant.
The batch VS is filled in CompilePipelines for all three precompile
modes (Enabled, Lazy, Disabled), so this conversion does not change
the hot-path-pre-build pattern from the patch 10-fix. The two VS
modules are present before any batch draw on every code path.
Test priority:
- Routine smoke under Enabled / Lazy / Disabled. Any rendering at
all exercises both VS modules immediately, so visual regressions
show up on the first frame of the first game booted.
- SSAA / MSAA round-trip in Disabled mode (per the patch-5-fix
protocol), specifically to confirm the structural-blob-pick
flips through 'none' -> 'centroid' -> 'sample' interp variants
cleanly as m_multisamples changes.
- Toggle 'disable color perspective' core option (under Texture
Correction) to flip the perspective axis; visual difference is
subtle but the blob-pick change is real.
- Toggle PGXP Geometry Correction (drives UV_LIMITS via
m_using_uv_limits) and PGXP Depth Buffer (drives PGXP_DEPTH
spec const) to flip the body and attribute-layout axes.
After this lands, the only remaining runtime glslang consumer in
the Vulkan backend is the batch FS (GenerateBatchFragmentShader at
gpu_hw_shadergen.cpp line ~667). That is a far larger state space
- six texture filters x ~14 boolean macros plus the structural
USE_DUAL_SOURCE / PALETTE_* axes - and will need to land in
several patches, one or two texture filters at a time.
145 lines
5.6 KiB
GLSL
145 lines
5.6 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), 4
|
|
// (textured without UV limits) or 5 (textured with UV limits).
|
|
// 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: 3 (attribute layouts) x 3
|
|
// (interpolation) x 2 (perspective) = 18. 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
|
|
// eighteen 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 18 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;
|
|
# if defined(UV_LIMITS)
|
|
layout(location = 4) in vec4 a_uv_limits;
|
|
# endif
|
|
#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;
|
|
# if defined(UV_LIMITS)
|
|
flat vec4 v_uv_limits;
|
|
# endif
|
|
#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;
|
|
|
|
# if defined(UV_LIMITS)
|
|
// UV limits are sent normalised; expand to 0..255 PSX-native here.
|
|
v_uv_limits = a_uv_limits * vec4(255.0, 255.0, 255.0, 255.0);
|
|
# endif
|
|
#endif
|
|
}
|