diff --git a/src/common/d3d12/embedded_shaders.cpp b/src/common/d3d12/embedded_shaders.cpp index c241ef91..3abf683d 100644 --- a/src/common/d3d12/embedded_shaders.cpp +++ b/src/common/d3d12/embedded_shaders.cpp @@ -94,4 +94,29 @@ namespace D3D12::EmbeddedShaders { #include "embedded_dxbc/display_ps_d1i2c1m16.inc" #include "embedded_dxbc/display_ps_d1i2c1m32.inc" +#include "embedded_dxbc/batch_untextured_ps_t0d0_none_p0.inc" +#include "embedded_dxbc/batch_untextured_ps_t0d0_none_p1.inc" +#include "embedded_dxbc/batch_untextured_ps_t0d0_centroid_p0.inc" +#include "embedded_dxbc/batch_untextured_ps_t0d0_centroid_p1.inc" +#include "embedded_dxbc/batch_untextured_ps_t0d0_sample_p0.inc" +#include "embedded_dxbc/batch_untextured_ps_t0d0_sample_p1.inc" +#include "embedded_dxbc/batch_untextured_ps_t0d1_none_p0.inc" +#include "embedded_dxbc/batch_untextured_ps_t0d1_none_p1.inc" +#include "embedded_dxbc/batch_untextured_ps_t0d1_centroid_p0.inc" +#include "embedded_dxbc/batch_untextured_ps_t0d1_centroid_p1.inc" +#include "embedded_dxbc/batch_untextured_ps_t0d1_sample_p0.inc" +#include "embedded_dxbc/batch_untextured_ps_t0d1_sample_p1.inc" +#include "embedded_dxbc/batch_untextured_ps_t1d0_none_p0.inc" +#include "embedded_dxbc/batch_untextured_ps_t1d0_none_p1.inc" +#include "embedded_dxbc/batch_untextured_ps_t1d0_centroid_p0.inc" +#include "embedded_dxbc/batch_untextured_ps_t1d0_centroid_p1.inc" +#include "embedded_dxbc/batch_untextured_ps_t1d0_sample_p0.inc" +#include "embedded_dxbc/batch_untextured_ps_t1d0_sample_p1.inc" +#include "embedded_dxbc/batch_untextured_ps_t1d1_none_p0.inc" +#include "embedded_dxbc/batch_untextured_ps_t1d1_none_p1.inc" +#include "embedded_dxbc/batch_untextured_ps_t1d1_centroid_p0.inc" +#include "embedded_dxbc/batch_untextured_ps_t1d1_centroid_p1.inc" +#include "embedded_dxbc/batch_untextured_ps_t1d1_sample_p0.inc" +#include "embedded_dxbc/batch_untextured_ps_t1d1_sample_p1.inc" + } // namespace D3D12::EmbeddedShaders diff --git a/src/common/d3d12/embedded_shaders.h b/src/common/d3d12/embedded_shaders.h index d0e33b7e..2e855cc8 100644 --- a/src/common/d3d12/embedded_shaders.h +++ b/src/common/d3d12/embedded_shaders.h @@ -320,4 +320,90 @@ extern const size_t k_display_ps_d1i2c1m16_size_bytes; extern const uint8_t k_display_ps_d1i2c1m32[]; extern const size_t k_display_ps_d1i2c1m32_size_bytes; +// Batch fragment shader, untextured slice. Equivalent to +// GPU_HW_ShaderGen::GenerateBatchFragmentShader invoked with +// texture_mode == GPUTextureMode::Disabled. Used by +// GPU_HW_D3D12::GetBatchPipeline's untextured branch in place of +// the runtime shadergen + D3DCompile path for the same slice. First +// batch FS template to be pre-baked; the four textured filter +// templates (Nearest / Bilinear / JINC2 / xBR) land in subsequent +// commits. +// +// Variant axes (5, all `-D` to fxc): +// +// * TRANSPARENCY (0/1): drives premultiply-alpha formula +// choice and the output-arm structure. +// * USE_DUAL_SOURCE (0/1): controls o_col1 declaration. +// * INTERP_CENTROID: emits `centroid` qualifier on inputs. +// * INTERP_SAMPLE: emits `sample` qualifier (wins over +// INTERP_CENTROID if both set; we never +// emit a variant where both are 1). +// * NOPERSP (0/1): adds `noperspective` to v_col0. +// +// 2 (transparency) x 2 (dual) x 3 (interp: none/centroid/sample) x +// 2 (persp) = 24 blobs. MSAA cardinality does not multiply this +// slice (untextured FS has no LOAD_TEXTURE_MS unroll); the +// interpolation qualifier captures the relevant MSAA contribution +// via INTERP_CENTROID/INTERP_SAMPLE. +// +// Runtime selection at the GetBatchPipeline call site: +// transparency_idx = (render_mode != BatchRenderMode::TransparencyDisabled) ? 1 : 0 +// dual_idx = use_dual_source ? 1 : 0 (mirror of +// shadergen formula: m_supports_dual_source_blend && +// ((transparency != Disabled && transparency != OnlyOpaque) || +// m_texture_filter != Nearest)) +// interp_idx = m_per_sample_shading ? 2 : +// (m_multisamples > 1 ? 1 : 0) +// persp_idx = m_disable_color_perspective ? 1 : 0 +// +// Source: data/shaders/d3d12/batch_untextured.ps.hlsl +extern const uint8_t k_batch_untextured_ps_t0d0_none_p0[]; +extern const size_t k_batch_untextured_ps_t0d0_none_p0_size_bytes; +extern const uint8_t k_batch_untextured_ps_t0d0_none_p1[]; +extern const size_t k_batch_untextured_ps_t0d0_none_p1_size_bytes; +extern const uint8_t k_batch_untextured_ps_t0d0_centroid_p0[]; +extern const size_t k_batch_untextured_ps_t0d0_centroid_p0_size_bytes; +extern const uint8_t k_batch_untextured_ps_t0d0_centroid_p1[]; +extern const size_t k_batch_untextured_ps_t0d0_centroid_p1_size_bytes; +extern const uint8_t k_batch_untextured_ps_t0d0_sample_p0[]; +extern const size_t k_batch_untextured_ps_t0d0_sample_p0_size_bytes; +extern const uint8_t k_batch_untextured_ps_t0d0_sample_p1[]; +extern const size_t k_batch_untextured_ps_t0d0_sample_p1_size_bytes; +extern const uint8_t k_batch_untextured_ps_t0d1_none_p0[]; +extern const size_t k_batch_untextured_ps_t0d1_none_p0_size_bytes; +extern const uint8_t k_batch_untextured_ps_t0d1_none_p1[]; +extern const size_t k_batch_untextured_ps_t0d1_none_p1_size_bytes; +extern const uint8_t k_batch_untextured_ps_t0d1_centroid_p0[]; +extern const size_t k_batch_untextured_ps_t0d1_centroid_p0_size_bytes; +extern const uint8_t k_batch_untextured_ps_t0d1_centroid_p1[]; +extern const size_t k_batch_untextured_ps_t0d1_centroid_p1_size_bytes; +extern const uint8_t k_batch_untextured_ps_t0d1_sample_p0[]; +extern const size_t k_batch_untextured_ps_t0d1_sample_p0_size_bytes; +extern const uint8_t k_batch_untextured_ps_t0d1_sample_p1[]; +extern const size_t k_batch_untextured_ps_t0d1_sample_p1_size_bytes; +extern const uint8_t k_batch_untextured_ps_t1d0_none_p0[]; +extern const size_t k_batch_untextured_ps_t1d0_none_p0_size_bytes; +extern const uint8_t k_batch_untextured_ps_t1d0_none_p1[]; +extern const size_t k_batch_untextured_ps_t1d0_none_p1_size_bytes; +extern const uint8_t k_batch_untextured_ps_t1d0_centroid_p0[]; +extern const size_t k_batch_untextured_ps_t1d0_centroid_p0_size_bytes; +extern const uint8_t k_batch_untextured_ps_t1d0_centroid_p1[]; +extern const size_t k_batch_untextured_ps_t1d0_centroid_p1_size_bytes; +extern const uint8_t k_batch_untextured_ps_t1d0_sample_p0[]; +extern const size_t k_batch_untextured_ps_t1d0_sample_p0_size_bytes; +extern const uint8_t k_batch_untextured_ps_t1d0_sample_p1[]; +extern const size_t k_batch_untextured_ps_t1d0_sample_p1_size_bytes; +extern const uint8_t k_batch_untextured_ps_t1d1_none_p0[]; +extern const size_t k_batch_untextured_ps_t1d1_none_p0_size_bytes; +extern const uint8_t k_batch_untextured_ps_t1d1_none_p1[]; +extern const size_t k_batch_untextured_ps_t1d1_none_p1_size_bytes; +extern const uint8_t k_batch_untextured_ps_t1d1_centroid_p0[]; +extern const size_t k_batch_untextured_ps_t1d1_centroid_p0_size_bytes; +extern const uint8_t k_batch_untextured_ps_t1d1_centroid_p1[]; +extern const size_t k_batch_untextured_ps_t1d1_centroid_p1_size_bytes; +extern const uint8_t k_batch_untextured_ps_t1d1_sample_p0[]; +extern const size_t k_batch_untextured_ps_t1d1_sample_p0_size_bytes; +extern const uint8_t k_batch_untextured_ps_t1d1_sample_p1[]; +extern const size_t k_batch_untextured_ps_t1d1_sample_p1_size_bytes; + } // namespace D3D12::EmbeddedShaders diff --git a/src/core/gpu_hw_d3d12.cpp b/src/core/gpu_hw_d3d12.cpp index 3a81ba4b..00df227e 100644 --- a/src/core/gpu_hw_d3d12.cpp +++ b/src/core/gpu_hw_d3d12.cpp @@ -892,9 +892,20 @@ bool GPU_HW_D3D12::CompilePipelines() if (!IsBatchShaderReachable(static_cast(render_mode), texture_mode, dual_source)) continue; - ComPtr blob = GetBatchFragmentShader(cur_filter, render_mode, texture_mode); - if (!blob) - return false; + // Pre-baked variants skip the GetBatchFragmentShader cache- + // warm step - the bytecode is statically linked, no + // D3DCompile to run. Currently the untextured slice is the + // only pre-baked template (commits 9e6f933 + this one); the + // four textured filter templates are still runtime-compiled + // and land in subsequent commits. The progress.Increment + // below still ticks for visual continuity so the + // user-facing progress bar reads the same as pre-pre-bake. + if (static_cast(texture_mode) != GPUTextureMode::Disabled) + { + ComPtr blob = GetBatchFragmentShader(cur_filter, render_mode, texture_mode); + if (!blob) + return false; + } progress.Increment(); } } @@ -1133,6 +1144,139 @@ void GPU_HW_D3D12::ShaderCompileThreadEntryPoint() } } +namespace { + +// Untextured batch FS pre-baked variant picker. Returns the matching +// DXBC blob from the 24-entry table at +// src/common/d3d12/embedded_dxbc/batch_untextured_ps_*.inc, indexed +// by the 5 axes the shadergen used to bake at compile time: +// +// transparency (BatchRenderMode != TransparencyDisabled) +// use_dual_source (BatchRenderMode != Disabled/OnlyOpaque OR +// filter != Nearest, capability-gated by +// m_supports_dual_source_blend) +// interp (per_sample_shading > MSAA > none; mirror of +// ShaderGen::GetInterpolationQualifier) +// noperspective (m_disable_color_perspective) +// +// The session-level inputs (filter, supports_dual_source_blend, +// multisamples, per_sample_shading, disable_color_perspective) are +// fixed for the lifetime of the GetBatchPipeline call - they only +// change via UpdateHWSettings which triggers full PSO rebuild. The +// per-call input (render_mode) is the BatchRenderMode enum value +// the draw is rendering with. +static D3D12_SHADER_BYTECODE PickBatchUntexturedFSBytecode( + uint8_t render_mode, GPUTextureFilter filter, bool supports_dual_source_blend, + uint32_t multisamples, bool per_sample_shading, bool disable_color_perspective) +{ + const bool transparency = + (render_mode != static_cast(BatchRenderMode::TransparencyDisabled)); + const bool use_dual_source = + supports_dual_source_blend && + ((render_mode != static_cast(BatchRenderMode::TransparencyDisabled) && + render_mode != static_cast(BatchRenderMode::OnlyOpaque)) || + filter != GPUTextureFilter::Nearest); + // 0 = none, 1 = centroid, 2 = sample. Mirror of + // ShaderGen::GetInterpolationQualifier: sample wins over centroid + // when per-sample-shading is enabled. + const unsigned interp_idx = + per_sample_shading ? 2u : ((multisamples > 1u) ? 1u : 0u); + const unsigned persp_idx = disable_color_perspective ? 1u : 0u; + + // 4-D table: [transparency][dual_source][interp][persp]. Nested + // brace-init keeps the structural correspondence with the regen + // tool's TEMPLATE_VARIANTS table visible to readers - the + // outermost-to-innermost iteration order matches the + // alphabetical .inc filenames. Each entry is a + // D3D12_SHADER_BYTECODE { pShaderBytecode, BytecodeLength } + // aggregate; the extern declarations in + // src/common/d3d12/embedded_shaders.h supply both halves. + static const D3D12_SHADER_BYTECODE k_blobs[2][2][3][2] = { + // transparency = 0 + { + // dual = 0 + { + // interp = none + {{D3D12::EmbeddedShaders::k_batch_untextured_ps_t0d0_none_p0, + D3D12::EmbeddedShaders::k_batch_untextured_ps_t0d0_none_p0_size_bytes}, + {D3D12::EmbeddedShaders::k_batch_untextured_ps_t0d0_none_p1, + D3D12::EmbeddedShaders::k_batch_untextured_ps_t0d0_none_p1_size_bytes}}, + // interp = centroid + {{D3D12::EmbeddedShaders::k_batch_untextured_ps_t0d0_centroid_p0, + D3D12::EmbeddedShaders::k_batch_untextured_ps_t0d0_centroid_p0_size_bytes}, + {D3D12::EmbeddedShaders::k_batch_untextured_ps_t0d0_centroid_p1, + D3D12::EmbeddedShaders::k_batch_untextured_ps_t0d0_centroid_p1_size_bytes}}, + // interp = sample + {{D3D12::EmbeddedShaders::k_batch_untextured_ps_t0d0_sample_p0, + D3D12::EmbeddedShaders::k_batch_untextured_ps_t0d0_sample_p0_size_bytes}, + {D3D12::EmbeddedShaders::k_batch_untextured_ps_t0d0_sample_p1, + D3D12::EmbeddedShaders::k_batch_untextured_ps_t0d0_sample_p1_size_bytes}}, + }, + // dual = 1 + { + // interp = none + {{D3D12::EmbeddedShaders::k_batch_untextured_ps_t0d1_none_p0, + D3D12::EmbeddedShaders::k_batch_untextured_ps_t0d1_none_p0_size_bytes}, + {D3D12::EmbeddedShaders::k_batch_untextured_ps_t0d1_none_p1, + D3D12::EmbeddedShaders::k_batch_untextured_ps_t0d1_none_p1_size_bytes}}, + // interp = centroid + {{D3D12::EmbeddedShaders::k_batch_untextured_ps_t0d1_centroid_p0, + D3D12::EmbeddedShaders::k_batch_untextured_ps_t0d1_centroid_p0_size_bytes}, + {D3D12::EmbeddedShaders::k_batch_untextured_ps_t0d1_centroid_p1, + D3D12::EmbeddedShaders::k_batch_untextured_ps_t0d1_centroid_p1_size_bytes}}, + // interp = sample + {{D3D12::EmbeddedShaders::k_batch_untextured_ps_t0d1_sample_p0, + D3D12::EmbeddedShaders::k_batch_untextured_ps_t0d1_sample_p0_size_bytes}, + {D3D12::EmbeddedShaders::k_batch_untextured_ps_t0d1_sample_p1, + D3D12::EmbeddedShaders::k_batch_untextured_ps_t0d1_sample_p1_size_bytes}}, + }, + }, + // transparency = 1 + { + // dual = 0 + { + // interp = none + {{D3D12::EmbeddedShaders::k_batch_untextured_ps_t1d0_none_p0, + D3D12::EmbeddedShaders::k_batch_untextured_ps_t1d0_none_p0_size_bytes}, + {D3D12::EmbeddedShaders::k_batch_untextured_ps_t1d0_none_p1, + D3D12::EmbeddedShaders::k_batch_untextured_ps_t1d0_none_p1_size_bytes}}, + // interp = centroid + {{D3D12::EmbeddedShaders::k_batch_untextured_ps_t1d0_centroid_p0, + D3D12::EmbeddedShaders::k_batch_untextured_ps_t1d0_centroid_p0_size_bytes}, + {D3D12::EmbeddedShaders::k_batch_untextured_ps_t1d0_centroid_p1, + D3D12::EmbeddedShaders::k_batch_untextured_ps_t1d0_centroid_p1_size_bytes}}, + // interp = sample + {{D3D12::EmbeddedShaders::k_batch_untextured_ps_t1d0_sample_p0, + D3D12::EmbeddedShaders::k_batch_untextured_ps_t1d0_sample_p0_size_bytes}, + {D3D12::EmbeddedShaders::k_batch_untextured_ps_t1d0_sample_p1, + D3D12::EmbeddedShaders::k_batch_untextured_ps_t1d0_sample_p1_size_bytes}}, + }, + // dual = 1 + { + // interp = none + {{D3D12::EmbeddedShaders::k_batch_untextured_ps_t1d1_none_p0, + D3D12::EmbeddedShaders::k_batch_untextured_ps_t1d1_none_p0_size_bytes}, + {D3D12::EmbeddedShaders::k_batch_untextured_ps_t1d1_none_p1, + D3D12::EmbeddedShaders::k_batch_untextured_ps_t1d1_none_p1_size_bytes}}, + // interp = centroid + {{D3D12::EmbeddedShaders::k_batch_untextured_ps_t1d1_centroid_p0, + D3D12::EmbeddedShaders::k_batch_untextured_ps_t1d1_centroid_p0_size_bytes}, + {D3D12::EmbeddedShaders::k_batch_untextured_ps_t1d1_centroid_p1, + D3D12::EmbeddedShaders::k_batch_untextured_ps_t1d1_centroid_p1_size_bytes}}, + // interp = sample + {{D3D12::EmbeddedShaders::k_batch_untextured_ps_t1d1_sample_p0, + D3D12::EmbeddedShaders::k_batch_untextured_ps_t1d1_sample_p0_size_bytes}, + {D3D12::EmbeddedShaders::k_batch_untextured_ps_t1d1_sample_p1, + D3D12::EmbeddedShaders::k_batch_untextured_ps_t1d1_sample_p1_size_bytes}}, + }, + }, + }; + + return k_blobs[transparency ? 1 : 0][use_dual_source ? 1 : 0][interp_idx][persp_idx]; +} + +} // anonymous namespace + GPU_HW_D3D12::ComPtr GPU_HW_D3D12::GetBatchFragmentShader(GPUTextureFilter filter, uint8_t render_mode, uint8_t texture_mode) { // Reserved_*Direct16Bit shader-source dedup is applied at the @@ -1269,21 +1413,42 @@ GPU_HW_D3D12::ComPtr GPU_HW_D3D12::GetBatchPipeline(GPUText return ret; } - // Slow path. Compile the PSO WITHOUT m_batch_shader_mutex held. - // GetBatchFragmentShader is internally thread-safe and so is - // m_shader_cache.GetPipelineState (via the per-instance mutexes - // added inside D3D12::ShaderCache). The actual D3DCompile and - // CreateGraphicsPipelineState calls run lock-free, so the worker - // compiling slot A doesn't block the main thread compiling - // slot B. Two threads racing to compile the SAME slot is - // wasteful but harmless - both produce equivalent PSOs and the - // double-check below picks the winner. - ComPtr fs_blob = GetBatchFragmentShader(filter, render_mode, lookup_mode); - if (!fs_blob) - return {}; - + // Compile WITHOUT m_batch_shader_mutex held. The shader cache + // itself is thread-safe (it has its own internal locking + // that does NOT span D3DCompile), so multiple threads can compile + // different shaders here in parallel. Two threads compiling the + // SAME shader is harmless - both produce identical DXBC and the + // cache dedups internally via its own double-check. + // + // Untextured slice (lookup_mode == GPUTextureMode::Disabled) takes + // the pre-baked fast path - PickBatchUntexturedFSBytecode picks + // the right .inc-supplied DXBC blob from the 24-variant table. + // Textured slices fall through to GetBatchFragmentShader which + // does the shadergen + D3DCompile dance and caches via + // m_batch_fragment_shader_blobs. The matrix slot for untextured + // stays empty across all sessions; nothing reads from it on this + // path. As subsequent commits land the 4 textured filter + // templates (Nearest / Bilinear / JINC2 / xBR), the textured + // branch shrinks until every batch FS variant is pre-baked. const bool textured = (static_cast(lookup_mode) != GPUTextureMode::Disabled); + D3D12_SHADER_BYTECODE fs_bc; + ComPtr fs_blob; // null on the pre-baked path; held for ref-life otherwise + if (textured) + { + fs_blob = GetBatchFragmentShader(filter, render_mode, lookup_mode); + if (!fs_blob) + return {}; + fs_bc.pShaderBytecode = fs_blob->GetBufferPointer(); + fs_bc.BytecodeLength = fs_blob->GetBufferSize(); + } + else + { + fs_bc = PickBatchUntexturedFSBytecode(render_mode, filter, m_supports_dual_source_blend, + m_multisamples, m_per_sample_shading, + m_disable_color_perspective); + } + D3D12::GraphicsPipelineBuilder gpbuilder; gpbuilder.SetRootSignature(m_batch_root_signature.Get()); gpbuilder.SetRenderTarget(0, m_vram_texture.GetFormat()); @@ -1310,7 +1475,7 @@ GPU_HW_D3D12::ComPtr GPU_HW_D3D12::GetBatchPipeline(GPUText gpbuilder.SetPrimitiveTopologyType(D3D12_PRIMITIVE_TOPOLOGY_TYPE_TRIANGLE); gpbuilder.SetVertexShader(m_batch_vertex_shader_blobs[static_cast(textured)].Get()); - gpbuilder.SetPixelShader(fs_blob.Get()); + gpbuilder.SetPixelShader(fs_bc); gpbuilder.SetRasterizationState(D3D12_FILL_MODE_SOLID, D3D12_CULL_MODE_NONE, false); gpbuilder.SetDepthState(true, true,