TsundereStation/data/shaders/d3d12/vram_write.ps.hlsl
LibretroAdmin ba013aa6de core/d3d12: pre-bake the VRAM write pixel shader
Same template as cb4b443 (vram_copy pre-bake), applied to vram_write_ps.
vram_write_ps is the CPU->VRAM upload path - hit on every game's
framebuffer streaming and FMV staging path, so it runs many times
per frame in practice. Pre-baking it removes another D3DCompile
from cold-flips and another path that's exposed to shader-cache
invalidation.

Variant axis: PGXP_DEPTH on/off only (2 blobs). The shadergen's
`use_ssbo` parameter is GLSL/Vulkan-only - the D3D12 caller at
GPU_HW_D3D12::GetVRAMWritePipeline always passes `false`, so the
use_ssbo dimension collapses to a single value for D3D12 and only
the texture-buffer source path (Buffer<uint4> samp0) gets pre-baked.

Files added:

* data/shaders/d3d12/vram_write.ps.hlsl: standalone HLSL source
  matching GenerateVRAMWriteFragmentShader(false)'s D3D12 output
  post-9d2b49d. 44-byte cbuffer member-for-member identical to
  VRAMWriteUBOData (u_base_coords, u_end_coords, u_size,
  u_buffer_base_offset, u_mask_or_bits, u_depth_value,
  u_resolution_scale, u_pad0). VRAM_SIZE / RESOLUTION_SCALE aliases
  inlined. RGBA5551ToRGBA8 helper inlined (no shadergen prelude
  available to standalone HLSL). PGXP_DEPTH branch in body controls
  the o_depth assignment.

* src/common/d3d12/embedded_dxbc/vram_write_ps_pgxp0.inc: DXBC for
  /DPGXP_DEPTH=0 (PGXP off, conditional o_depth). 2048 bytes.
* src/common/d3d12/embedded_dxbc/vram_write_ps_pgxp1.inc: DXBC for
  /DPGXP_DEPTH=1 (PGXP on, o_depth = 1.0). 2028 bytes.

Both built with Microsoft fxc via Wine + d3dcompiler_47.dll - full
RDEF/ISGN/OSGN/SHEX/STAT chunk set, creator stamped "Microsoft (R)
HLSL Shader Compiler 10.1". Validator (check_microsoft_creator in
tools/regen_d3d12_dxbc.py) passes both.

Files modified:

* tools/regen_d3d12_dxbc.py: TEMPLATE_VARIANTS gains the
  vram_write.ps.hlsl entry with two rows (pgxp0, pgxp1). Same
  shape as the vram_copy entry.

* src/common/d3d12/embedded_shaders.h: extern declarations added
  for the two new blobs and their size constants. Header docblock
  explains the use_ssbo collapse.

* src/common/d3d12/embedded_shaders.cpp: two #include lines for
  the new .inc files, inside namespace D3D12::EmbeddedShaders.

* src/core/gpu_hw_d3d12.cpp GetVRAMWritePipeline: shadergen call +
  m_shader_cache.GetPixelShader path replaced with an inline
  D3D12_SHADER_BYTECODE picked via m_pgxp_depth_buffer. PSO build
  downstream is unchanged (same root signature, MSAA, depth state,
  shader-cache PSO disk-cache via gpbuilder.Create). The
  m_shadergen-not-yet-built check is dropped for this path since
  the embedded blob is statically linked.

Cost matrix delta (D3D12) for vram_write_ps:
  Cold-flip PGXP toggle: ~50 ms x 2 D3DCompile + ~10 ms x 2 PSO
                         -> ~10 ms x 2 PSO only.
  Disk-cache-warm: ~1 ms each -> 0 ms.
  Resolution scale: 0 ms (already cbuffer-routed since 9d2b49d).

The shadergen GenerateVRAMWriteFragmentShader function is left
intact in gpu_hw_shadergen.cpp - the other three backends still
call it through their runtime shader-cache paths. Only D3D12
bypasses it now.

Test:
  - Build: D3D12 backend links without unresolved symbols.
  - Runtime: every VRAM upload that previously worked must still
    work. Concrete test: any game's pre-rendered backdrop streaming
    (FF7 entering new screens, MGS pre-rendered cutscene backdrops),
    any FMV playback (Sony intro bookend, in-game cutscenes), and
    any sprite-page reload (fighting-game character select). If
    the embedded blob were wrong, framebuffer uploads would land
    at wrong VRAM offsets and produce visible texture garble
    within seconds of any of those activities.
  - PGXP toggle: flip the "PGXP CPU mode" core option in RetroArch.
    First flip rebuilds PSOs picking blob[1] vs blob[0]; subsequent
    same-direction flips hit the PSO cache.
  - Resolution scale toggle 1x <-> 4x: no PSO recompile (blob is
    scale-independent post-9d2b49d); next CPU->VRAM upload pushes
    a different u_resolution_scale through the cbuffer.

Next per the same template now that the toolchain is trusted and
the pattern is established: vram_fill_ps (8 variants on PGXP x
WRAPPED x INTERLACED, no scale-refactor needed - body doesn't
reference RESOLUTION_SCALE for D3D12 mode), vram_update_depth_ps
(2 MSAA variants - first MSAA texture-binding variant exercise),
vram_read_ps (M MSAA variants - on top of the e64fc28-line
cbuffer routing already done).
2026-05-18 23:04:29 +02:00

113 lines
4.4 KiB
HLSL

// Pre-baked VRAM write pixel shader for the D3D12 backend.
//
// Equivalent to GPU_HW_ShaderGen::GenerateVRAMWriteFragmentShader(false)
// in src/core/gpu_hw_shadergen.cpp. Used by GPU_HW_D3D12::GetVRAMWritePipeline
// for the CPU->VRAM upload path - this is hit on every game's
// framebuffer streaming (pre-rendered backgrounds in FF7, FMV decode
// staging, sprite-page reloads in fighting games, etc., so it runs
// many times per frame in practice).
//
// Variant axis: PGXP_DEPTH = 0 or 1. PGXP-depth disabled / enabled -
// the runtime m_pgxp_depth_buffer toggle. Two .inc blobs:
// vram_write_ps_pgxp0.inc (PGXP_DEPTH = 0)
// vram_write_ps_pgxp1.inc (PGXP_DEPTH = 1)
//
// use_ssbo: the shadergen path takes a `bool use_ssbo` parameter, but
// the D3D12 caller always passes `false` (HLSL has no direct SSBO
// equivalent; the shadergen's use_ssbo=true branch is GLSL/Vulkan-only).
// So the D3D12 pre-bake only covers the texture-buffer path
// (Buffer<uint4> samp0), and the use_ssbo dimension collapses to a
// single value here.
//
// RESOLUTION_SCALE / VRAM_SIZE derive at runtime from u_resolution_scale
// in the cbuffer below (post-9d2b49d refactor). Same DXBC serves
// every resolution-scale value.
cbuffer UBOBlock : register(b0)
{
// 44-byte cbuffer. C++ side is GPU_HW::VRAMWriteUBOData (gpu_hw.h);
// the field order here MUST match the struct member order there.
uint2 u_base_coords;
uint2 u_end_coords;
uint2 u_size;
uint u_buffer_base_offset;
uint u_mask_or_bits;
float u_depth_value;
uint u_resolution_scale;
uint u_pad0;
};
// VRAM_SIZE / RESOLUTION_SCALE derive from u_resolution_scale - mirrors
// the WriteCBufferResolutionScaleAliases helper's #define output in
// gpu_hw_shadergen.cpp.
#define RESOLUTION_SCALE u_resolution_scale
#define VRAM_SIZE (uint2(1024u, 512u) * u_resolution_scale)
// Texture-buffer source for the CPU upload data. PSX VRAM writes are
// 16-bit RGBA5551 packed into the texel buffer; the shader does
// per-pixel address arithmetic + RGBA5551 -> RGBA8 conversion below.
// `Buffer<uint4>` here matches what shadergen.cpp's DeclareTextureBuffer
// emits for HLSL when is_int=true / is_unsigned=true.
Buffer<uint4> samp0 : register(t0);
// RGBA5551 -> RGBA8 unpack. PSX 16-bit colour layout (bits 0-4 R,
// 5-9 G, 10-14 B, 15 alpha/mask). Identical to the helper inlined by
// WriteCommonFunctions in the shadergen path.
float4 RGBA5551ToRGBA8(uint v)
{
uint r = (v & 31u);
uint g = ((v >> 5) & 31u);
uint b = ((v >> 10) & 31u);
uint a = ((v >> 15) & 1u);
return float4(float(r) / 31.0, float(g) / 31.0, float(b) / 31.0, float(a));
}
void main(
in float2 v_tex0 : TEXCOORD0,
in float4 v_pos : SV_Position,
out float o_depth : SV_Depth,
out float4 o_col0 : SV_Target0)
{
// Map upscaled framebuffer pixel back to native VRAM coord. The
// runtime sets the viewport in upscaled VRAM space; the divide by
// RESOLUTION_SCALE collapses N upscaled pixels into one native one
// so the texture-buffer lookup uses native VRAM addressing.
uint2 coords = uint2(uint(v_pos.x) / RESOLUTION_SCALE,
uint(v_pos.y) / RESOLUTION_SCALE);
// Discard out-of-range pixels. Same wrap-around-rect logic as
// vram_copy: (coord < base) && (coord >= end) means the pixel sits
// outside both halves of a wrapped rect.
if ((coords.x < u_base_coords.x && coords.x >= u_end_coords.x) ||
(coords.y < u_base_coords.y && coords.y >= u_end_coords.y))
{
discard;
}
// Offset from the start of the rect, with native-VRAM wrap-around
// handling on each axis. (VRAM_SIZE.x / RESOLUTION_SCALE) is just
// 1024 in native coords; written this way to match the shadergen
// body byte-for-byte so the optimization opportunities are
// identical.
uint2 offset;
offset.x = (coords.x < u_base_coords.x)
? ((VRAM_SIZE.x / RESOLUTION_SCALE) - u_base_coords.x + coords.x)
: (coords.x - u_base_coords.x);
offset.y = (coords.y < u_base_coords.y)
? ((VRAM_SIZE.y / RESOLUTION_SCALE) - u_base_coords.y + coords.y)
: (coords.y - u_base_coords.y);
// Linearise (offset.x, offset.y) into a 1-D buffer index, fetch the
// 16-bit texel from the upload buffer, OR in the mask bit, unpack
// to RGBA8.
uint buffer_offset = u_buffer_base_offset + (offset.y * u_size.x) + offset.x;
uint value = samp0.Load(int(buffer_offset)).r | u_mask_or_bits;
o_col0 = RGBA5551ToRGBA8(value);
#if PGXP_DEPTH
o_depth = 1.0;
#else
o_depth = (o_col0.a == 1.0) ? u_depth_value : 0.0;
#endif
}