diff --git a/src/core/gpu_hw.cpp b/src/core/gpu_hw.cpp index 50ed0b75..1845f294 100644 --- a/src/core/gpu_hw.cpp +++ b/src/core/gpu_hw.cpp @@ -95,6 +95,7 @@ bool GPU_HW::Initialize(HostDisplay* host_display) m_batch_ubo_data.u_resolution_scale = m_resolution_scale; m_batch_ubo_data.u_true_color = m_true_color ? 1u : 0u; m_batch_ubo_data.u_scaled_dithering = m_scaled_dithering ? 1u : 0u; + m_batch_ubo_data.u_pgxp_depth = m_pgxp_depth_buffer ? 1u : 0u; UpdateSoftwareRenderer(false); @@ -135,6 +136,7 @@ void GPU_HW::Reset(bool clear_vram) m_batch_ubo_data.u_resolution_scale = m_resolution_scale; m_batch_ubo_data.u_true_color = m_true_color ? 1u : 0u; m_batch_ubo_data.u_scaled_dithering = m_scaled_dithering ? 1u : 0u; + m_batch_ubo_data.u_pgxp_depth = m_pgxp_depth_buffer ? 1u : 0u; m_batch_ubo_dirty = true; m_current_depth = 1; @@ -343,6 +345,16 @@ void GPU_HW::UpdateHWSettings(bool* framebuffer_changed, bool* shaders_changed, m_batch.use_depth_buffer = false; if (m_pgxp_depth_buffer) ClearDepthBuffer(); + // The VS reads u_pgxp_depth from the batch UBO to select between + // a_pos.z and a_pos.w as the depth source. The dirty bit is + // already set in the bulk push above (line 336), so this update + // rides the existing upload at the next FlushRender. The FS + // still recompiles on this flip because PGXP_DEPTH gates + // SV_Depth in the FS entry-point signature - shader_source_changed + // upstream of this block has already triggered the rebuild path, + // and the new shadergen state will pick up the fresh + // m_pgxp_depth value when it generates the new FS source. + m_batch_ubo_data.u_pgxp_depth = m_pgxp_depth_buffer ? 1u : 0u; } UpdateSoftwareRenderer(true); diff --git a/src/core/gpu_hw.h b/src/core/gpu_hw.h index 44733710..276333b7 100644 --- a/src/core/gpu_hw.h +++ b/src/core/gpu_hw.h @@ -146,15 +146,25 @@ protected: // updated on its own cadence (field LSB per frame, interlacing // on/off on display-mode changes). uint32_t u_interlacing; + // u_pgxp_depth: per-session PGXP-depth-buffer mode toggle (0 = off, + // 1 = on). Used to live as a compile-time PGXP_DEPTH macro in the + // batch VS source, selecting between a_pos.z (legacy mask-Z) and + // a_pos.w (PGXP-computed Z) as the depth source. Now a runtime + // ternary on the cbuffer field. The FS-side PGXP_DEPTH usage + // (gating SV_Depth declaration) is NOT yet routed - it still + // affects the FS entry-point signature - so toggling + // m_pgxp_depth_buffer mid-session still triggers a FS / PSO + // matrix rebuild via shader_source_changed. VS, however, stays + // bound across the flip. + uint32_t u_pgxp_depth; // Reserved cbuffer slots. The batch UBO has been progressively // growing as compile-time #defines move to runtime cbuffer // branches (RESOLUTION_SCALE, TRUE_COLOR, DITHERING_SCALED, - // DITHERING, INTERLACING). Pre-allocating the remaining slots - // in the 4th 16-byte row matches the layout HLSL would have - // padded to anyway, and lets follow-up commits route additional - // axes (PGXP_DEPTH, UV_LIMITS, etc.) into a named slot without - // changing the cbuffer's external size or alignment. - uint32_t u_pad0; + // DITHERING, INTERLACING, PGXP_DEPTH for VS). Pre-allocating + // the remaining slots in the 4th 16-byte row matches the layout + // HLSL would have padded to anyway, and lets follow-up commits + // route additional axes (UV_LIMITS, etc.) into a named slot + // without changing the cbuffer's external size or alignment. uint32_t u_pad1; uint32_t u_pad2; }; diff --git a/src/core/gpu_hw_shadergen.cpp b/src/core/gpu_hw_shadergen.cpp index 418fd302..ae8fbab6 100644 --- a/src/core/gpu_hw_shadergen.cpp +++ b/src/core/gpu_hw_shadergen.cpp @@ -85,7 +85,7 @@ void GPU_HW_ShaderGen::WriteBatchUniformBuffer(std::stringstream& ss) "float u_dst_alpha_factor", "uint u_interlaced_displayed_field", "bool u_set_mask_while_drawing", "uint u_resolution_scale", "uint u_true_color", "uint u_scaled_dithering", "uint u_dithering", - "uint u_interlacing", "uint u_pad0", "uint u_pad1", "uint u_pad2"}, + "uint u_interlacing", "uint u_pgxp_depth", "uint u_pad1", "uint u_pad2"}, false); // Alias the historical compile-time constants to their cbuffer- @@ -120,7 +120,22 @@ std::string GPU_HW_ShaderGen::GenerateBatchVertexShader(bool textured) WriteHeader(ss); DefineMacro(ss, "TEXTURED", textured); DefineMacro(ss, "UV_LIMITS", m_uv_limits); - DefineMacro(ss, "PGXP_DEPTH", m_pgxp_depth); + // PGXP_DEPTH used to live as a compile-time #define driving the + // pos_z source selection in the VS body. It is now routed through + // the batch UBO (u_pgxp_depth) and the former #if/#else block is + // a uniform-control-flow ternary. Toggling PGXP-depth mode mid- + // session is a single 4-byte cbuffer write picked up on the next + // FlushRender on the VS side - the VS source is now invariant + // under the m_pgxp_depth member. + // The FS still captures m_pgxp_depth: it gates SV_Depth declaration + // in the FS entry-point signature (DeclareFragmentEntryPoint's + // `depth_output` parameter), which is a signature change that + // can't be cbuffer-routed without always-emitting SV_Depth and + // taking the early-Z hit unconditionally. Routing the FS side is + // a future commit pending perf measurement. Until then, PGXP + // toggle still triggers a FS / PSO matrix rebuild via the + // shader_source_changed path - it just doesn't trigger a VS + // rebuild anymore. // Order matters: WriteBatchUniformBuffer must emit before // WriteCommonFunctions so the #define aliases for RESOLUTION_SCALE @@ -181,18 +196,21 @@ std::string GPU_HW_ShaderGen::GenerateBatchVertexShader(bool textured) float pos_x = ((a_pos.x + vertex_offset) / 512.0) - 1.0; float pos_y = ((a_pos.y + vertex_offset) / -256.0) + 1.0; -#if PGXP_DEPTH - // Ignore mask Z when using PGXP depth. - float pos_z = a_pos.w; - float pos_w = a_pos.w; -#else - float pos_z = a_pos.z; - float pos_w = a_pos.w; -#endif - #if API_OPENGL || API_OPENGL_ES pos_y += POS_EPSILON; +#endif + // PGXP-depth mode (u_pgxp_depth != 0) ignores mask Z and uses + // a_pos.w as the depth source; the legacy path reads a_pos.z. + // u_pgxp_depth is a cbuffer scalar so this is a uniform-control- + // flow select - the driver collapses it to a single conditional + // move at compile time. Was a compile-time #if PGXP_DEPTH / + // #else / #endif gate (m_pgxp_depth captured at shadergen ctor); + // now a runtime branch on the cbuffer field. + float pos_z = (u_pgxp_depth != 0u) ? a_pos.w : a_pos.z; + float pos_w = a_pos.w; + +#if API_OPENGL || API_OPENGL_ES // 0..1 to -1..1 depth range. pos_z = (pos_z * 2.0) - 1.0; #endif