Cross-backend cbuffer-routing for the 4-value BatchRenderMode
enum that drove the batch FS body's transparency-mode branches.
Previously TRANSPARENCY / TRANSPARENCY_ONLY_OPAQUE /
TRANSPARENCY_ONLY_TRANSPARENT lived as 3 compile-time DefineMacro
calls on the C++ shadergen path and 3 specialisation constants
(constant_id 100 / 101 / 102) on the Vulkan pre-baked SPIR-V
path. Both encodings collapsed the 4 enum values across 3
booleans:
TransparencyDisabled (0): TRANS=0, OO=0, OT=0
TransparentAndOpaque (1): TRANS=1, OO=0, OT=0
OnlyOpaque (2): TRANS=1, OO=1, OT=0
OnlyTransparent (3): TRANS=1, OO=0, OT=1
Post-routing the 3 macros / spec consts are gone. The FS body
branches on u_render_mode (4-state uint at cbuffer offset 60 -
the former u_pad2 reserved slot) and derives the three former
booleans inline as (u_render_mode != 0u), (u_render_mode == 2u),
(u_render_mode == 3u). PSO blend state still varies per
BatchRenderMode at the GraphicsPipelineBuilder level
(m_batch_pipelines's [render_mode] matrix dim stays); the win
is purely on the FS bytecode side - 4x fewer distinct DXBC /
SPIR-V variants per (filter, render, texture) cell.
What changes:
src/core/gpu_hw.h
BatchUBOData: rename u_pad2 -> u_render_mode at offset 60.
Same 64-byte total cbuffer size, same alignment, same offset
for every other field. No external ABI break - the binding
layout is opaque-blob-shaped from the host's perspective.
src/core/gpu_hw_shadergen.cpp
Drop the 3 DefineMacro calls. WriteBatchUniformBuffer's
field list trades u_pad2 for u_render_mode. The FS body's
`#if TRANSPARENCY ... #elif TRANSPARENCY && TEXTURED ...
#else` pre-routing structure (with inner
`#if TRANSPARENCY_ONLY_OPAQUE / #if TRANSPARENCY_ONLY_TRANSPARENT`
discards) collapses to:
if (u_render_mode != 0u) {
if (semitransparent) {
// emit colour + dual-source aux + optional OO discard
if (u_render_mode == 2u) discard;
} else {
// emit colour + dual-source aux + optional OT discard
if (u_render_mode == 3u) discard;
}
} else {
// emit colour + dual-source aux without transparency premultiply
}
The TEXTURED gate stays compile-time - it's a structural
shader-source dimension (texture_mode-dependent shadergen
branch that decides whether to emit SampleFromVRAM /
palette / texture-window helpers at all).
src/core/gpu_hw.cpp
FlushRender now stamps u_render_mode in m_batch_ubo_data
before each draw:
- Single-pass: set u_render_mode = m_batch.GetRenderMode(),
mark dirty if it changed, then the existing
m_batch_ubo_dirty check uploads. Same upload count as
pre-routing for the common case.
- Two-pass NeedsTwoPassRendering(): set u_render_mode =
OnlyOpaque, upload, draw; set u_render_mode =
OnlyTransparent, upload, draw. +1 ~64-byte UBO upload
per two-pass FlushRender. Worth it - the FS variant
matrix collapses by 4x on every backend that pre-bakes.
src/core/gpu_hw_shadergen.cpp (premultiply branch)
`#if TRANSPARENCY` -> `if (u_render_mode != 0u)` for the
src_alpha_factor premultiply.
src/core/gpu_hw_vulkan.cpp
Drop the 3 fs_spec.AddBool calls for id=100/101/102 from
the batch FS pipeline build. RESOLUTION_SCALE / DITHERING /
INTERLACING / DITHERING_SCALED / TRUE_COLOR / PALETTE_*BIT /
RAW_TEXTURE / BINALPHA stay (their spec const semantics
are unchanged). The local `brm` BatchRenderMode local
drops too; render_mode reaches the FS via u_render_mode
in the cbuffer, populated by gpu_hw.cpp before each draw.
data/shaders/vulkan/batch_untextured.frag.glsl
data/shaders/vulkan/batch_textured_nearest.frag.glsl
data/shaders/vulkan/batch_textured_bilinear.frag.glsl
data/shaders/vulkan/batch_textured_jinc2.frag.glsl
data/shaders/vulkan/batch_textured_xbr.frag.glsl
5 Vulkan FS templates drop the 3 spec const declarations
(constant_id = 100/101/102), add u_render_mode to the
UBO declaration (layout(offset = 60) uint u_render_mode),
and replace TRANSPARENCY / _ONLY_OPAQUE / _ONLY_TRANSPARENT
body references with (u_render_mode != 0u) / (==2u) /
(==3u). Same shape changes as the prior cbuffer-routing
arc commits (DITHERING, INTERLACING, UV_LIMITS, PGXP_DEPTH).
src/common/vulkan/embedded_spirv/batch_*_fs_*.inc (60 files)
All 60 batch FS SPIR-V blobs regenerated. The SPIR-V
instruction count grows by ~30-50 words per blob (one UBO
field load + 1-3 compare + branch instructions per former
spec-const reference site - 5 sites per untextured / 4-7
sites per textured filter family). Net SPIR-V footprint
grows by ~50-100 KiB across the 60 blobs.
src/common/vulkan/embedded_spirv/batch_vs_*.inc (12 files)
Untouched - the VS doesn't reference TRANSPARENCY.
What does NOT change in this commit:
- D3D12 pre-baked untextured DXBC (24 blobs at
batch_untextured_ps_*.inc) stays as-is. The blobs were
built from data/shaders/d3d12/batch_untextured.ps.hlsl
pre-this-commit with TRANSPARENCY as a `-D` macro; they
still produce correct output because each baked variant
has the right transparency behavior baked in, and the
PickBatchUntexturedFSBytecode picker still selects the
right variant from render_mode. The new u_render_mode
cbuffer field is set in the UBO but the pre-baked DXBC
doesn't read it (it doesn't reference the field).
A follow-up commit can re-bake those 24 blobs with the
routed HLSL, dropping the transparency axis to take the
untextured slice from 24 -> 12 variants (or 6 if we also
drop the dual_source axis for untextured-where-it-doesn't-
matter cases - the d=true case for untextured non-trans
is reachable only when filter != Nearest which is rare).
Not blocking on this commit.
- PSO matrix shape stays <5, 9, 4, 2, 7> = 2520 PSOs (filter
x texture x render x depth_test x transparency). The
[render_mode] dim stays because PSO blend state still
varies per BatchRenderMode value. m_batch_fragment_shader_blobs
stays <9, 4, 7> = 252 slots, though now the [render_mode]
dim's 4 slots can store identical FS bytecode if every
backend's runtime compile path produces the same DXBC for
all 4 render_modes (which it will, post-routing). A
follow-up could shrink the dim from 4 to 1 once we trust
the C++ shadergen output to be invariant under render_mode.
- The C++ shadergen TEXTURED branch is still compile-time.
Routing TEXTURED to cbuffer would require declaring all
textured-only helpers (SampleFromVRAM, ApplyTextureWindow,
FloatToIntegerCoords) and the texture binding even for
untextured shaders - bigger architecture change without
proportional payoff.
- render_mode dependency at the call site GetBatchFragmentShader
on the D3D12 side. The function still takes render_mode as
a parameter so it can hash into the right slot in
m_batch_fragment_shader_blobs. The shader source it
generates is now invariant under render_mode (the shadergen
call's transparency parameter is unused for the FS body),
so technically the 4 [render_mode] slots end up holding
identical ID3DBlob refs. Cleaning that up is the follow-up
"shrink [render_mode] dim" work mentioned above.
Test:
- glslangValidator on all 5 Vulkan FS templates: passes.
- python3 tools/regen_vulkan_spirv.py: produces 91 SPIR-V
blobs (12 VS + 60 FS + 19 non-batch), all valid.
- Boot any PSX title on Vulkan: render_mode flips per-batch
pick up the new u_render_mode value via cbuffer. Two-pass
semitransparent draws (e.g. Final Fantasy menus, MGS
smoke) see the matching enum value on each pass via the
+1 UBO re-upload between OnlyOpaque / OnlyTransparent
DrawIndexed calls. Visual diff: zero.
- Boot any PSX title on D3D11 / D3D12 / OpenGL: the
runtime-compiled FS bytecode loses the 3 TRANSPARENCY
macros and gains a u_render_mode runtime branch. Pre-
baked D3D12 untextured slots still work via the picker.
Visual diff: zero.
- PGXP-flip mid-session: independent of this commit;
behaviour unchanged.
Next:
- Re-bake D3D12 untextured DXBC with the routed HLSL
template (24 -> 12 variants, drop the transparency axis).
Small follow-up.
- Pre-bake the 4 textured filter slices on the routed
matrix: 72 variants per template (3 interp x 2 persp x 2
dual x 6 texture_mode) instead of 288 with TRANSPARENCY
as a `-D` axis. Each slice gets a foundation commit
(HLSL + regen + .inc) and an activation commit
(extern decls + picker + GetBatchPipeline wiring),
mirroring the untextured arc.
|
||
|---|---|---|
| .. | ||
| adaptive_downsample_blur.frag.glsl | ||
| adaptive_downsample_composite.frag.glsl | ||
| adaptive_downsample_mip.frag.glsl | ||
| batch.vert.glsl | ||
| batch_textured_bilinear.frag.glsl | ||
| batch_textured_jinc2.frag.glsl | ||
| batch_textured_nearest.frag.glsl | ||
| batch_textured_xbr.frag.glsl | ||
| batch_untextured.frag.glsl | ||
| box_sample_downsample.frag.glsl | ||
| display.frag.glsl | ||
| display_msaa.frag.glsl | ||
| present_cursor.frag.glsl | ||
| present_display.frag.glsl | ||
| present_fullscreen.vert.glsl | ||
| README.md | ||
| screen_quad.vert.glsl | ||
| uv_quad.vert.glsl | ||
| vram_copy.frag.glsl | ||
| vram_fill.frag.glsl | ||
| vram_readback.frag.glsl | ||
| vram_readback_msaa.frag.glsl | ||
| vram_update_depth.frag.glsl | ||
| vram_update_depth_msaa.frag.glsl | ||
| vram_write_ssbo.frag.glsl | ||
| vram_write_texbuf.frag.glsl | ||
Vulkan shader sources (pre-baked SPIR-V)
This directory holds the GLSL sources for shaders used by the Vulkan backend
that have been moved off the runtime ShaderGen path. Each .glsl file here
has a matching pre-compiled SPIR-V blob checked in to
src/common/vulkan/embedded_spirv/.
Why this exists
Historically the Vulkan backend generated GLSL at runtime via
src/core/shadergen.cpp and src/core/gpu_hw_shadergen.cpp, then handed the
text to glslang inside src/common/vulkan/shader_compiler.cpp to produce
SPIR-V. Both dep/glslang/ (4.4 MB of vendored source, 99 translation units)
and the disk-backed runtime shader cache exist solely to support that path.
Pre-baking the SPIR-V removes:
- the glslang dependency from the libretro core build,
- the per-launch shader compilation stall, and
- a class of "stale cache after glslang version bump" runtime failures.
The OpenGL / OpenGL ES / D3D11 / D3D12 backends still use the runtime
ShaderGen path; only the Vulkan backend reads from the embedded SPIR-V.
Build-time constraint
The core build (Makefile.libretro and src/common/CMakeLists.txt) must
never invoke an external shader compiler. The .inc files under
src/common/vulkan/embedded_spirv/ are checked into the repository and
compiled like any other C++ source. The tools/regen_vulkan_spirv.py helper
exists for contributors who edit the .glsl sources in this directory; it is
not part of the build.
Regenerating the SPIR-V blobs
After editing a .glsl file here, run:
python3 tools/regen_vulkan_spirv.py
This invokes glslangValidator --target-env vulkan1.0 on each .glsl file
and rewrites the matching .inc under src/common/vulkan/embedded_spirv/.
Both the .glsl and the regenerated .inc must be committed together so the
two stay in sync.
glslangValidator is part of the Vulkan SDK (or the glslang-tools package
on Debian / Ubuntu). It is not invoked by the core build.