From 736d054b8f1282dc0d9bcd1cc1daf51ffa485bbb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Henrik=20Rydg=C3=A5rd?= Date: Tue, 19 May 2026 15:41:35 +0200 Subject: [PATCH] Remove the u_proj_though matrix from the uniform buffer --- GPU/Common/ShaderUniforms.cpp | 14 ++++---------- GPU/Common/ShaderUniforms.h | 6 +++--- GPU/Common/VertexShaderGenerator.cpp | 9 +++++++-- GPU/GLES/ShaderManagerGLES.cpp | 17 +++++++---------- GPU/GLES/ShaderManagerGLES.h | 2 +- 5 files changed, 22 insertions(+), 26 deletions(-) diff --git a/GPU/Common/ShaderUniforms.cpp b/GPU/Common/ShaderUniforms.cpp index 75e9fd1344..e8b290d2ac 100644 --- a/GPU/Common/ShaderUniforms.cpp +++ b/GPU/Common/ShaderUniforms.cpp @@ -151,16 +151,10 @@ void BaseUpdateUniforms(UB_VS_FS_Base *ub, uint64_t dirtyUniforms, bool flipView } if (dirtyUniforms & DIRTY_PROJTHROUGHMATRIX) { - Matrix4x4 proj_through; - proj_through.setOrthoVulkan(0.0f, gstate_c.curRTWidth, 0, gstate_c.curRTHeight, 0, 1); - - // Negative RT offsets come from split framebuffers (Killzone) - if (gstate_c.curRTOffsetX < 0 || gstate_c.curRTOffsetY < 0) { - proj_through.wx += 2.0f * (float)gstate_c.curRTOffsetX / (float)gstate_c.curRTWidth; - proj_through.wy += 2.0f * (float)gstate_c.curRTOffsetY / (float)gstate_c.curRTHeight; - } - - CopyMatrix4x4(ub->proj_through, proj_through.getReadPtr()); + ub->xywh[0] = (float)gstate_c.curRTOffsetX; + ub->xywh[1] = (float)gstate_c.curRTOffsetY; + ub->xywh[2] = (float)gstate_c.curRTWidth; + ub->xywh[3] = (float)gstate_c.curRTHeight; ub->rotation = useBufferedRendering ? 0 : (float)g_display.rotation; } diff --git a/GPU/Common/ShaderUniforms.h b/GPU/Common/ShaderUniforms.h index a63b12e2e0..a9f37551e3 100644 --- a/GPU/Common/ShaderUniforms.h +++ b/GPU/Common/ShaderUniforms.h @@ -21,7 +21,7 @@ enum : uint64_t { // Every line here is a 4-float. struct alignas(16) UB_VS_FS_Base { float proj[16]; - float proj_through[16]; + float xywh[4]; // later, we could invert w and h here to avoid division. float view[12]; float world[12]; float tex[12]; @@ -43,11 +43,11 @@ struct alignas(16) UB_VS_FS_Base { // VR stuff is to go here, later. For normal drawing, we can then get away // with just uploading the first 448 bytes of the struct (up to and including fogCoef). }; -static_assert(sizeof(UB_VS_FS_Base) == 480, "UB_VS_FS_Base should be 480 bytes"); +static_assert(sizeof(UB_VS_FS_Base) == 432, "UB_VS_FS_Base should be 432 bytes"); static const char * const ub_baseStr = R"( mat4 u_proj; - mat4 u_proj_through; + vec4 u_xywh; mat3x4 u_view; mat3x4 u_world; mat3x4 u_texmtx; diff --git a/GPU/Common/VertexShaderGenerator.cpp b/GPU/Common/VertexShaderGenerator.cpp index 1272b89110..6461739721 100644 --- a/GPU/Common/VertexShaderGenerator.cpp +++ b/GPU/Common/VertexShaderGenerator.cpp @@ -410,7 +410,7 @@ bool GenerateVertexShader(const VShaderID &id, char *buffer, const ShaderLanguag } if (isModeThrough) { - WRITE(p, "uniform mat4 u_proj_through;\n"); + WRITE(p, "uniform vec4 u_xywh;\n"); *uniformMask |= DIRTY_PROJTHROUGHMATRIX; } else if (useHWTransform) { if (gstate_c.Use(GPU_USE_VIRTUAL_REALITY)) { @@ -746,7 +746,12 @@ bool GenerateVertexShader(const VShaderID &id, char *buffer, const ShaderLanguag WRITE(p, " %sv_fogdepth = fog;\n", compat.vsOutPrefix); if (isModeThrough) { // The proj_through matrix already has the rotation, if needed. - WRITE(p, " vec4 outPos = mul(u_proj_through, vec4(position.xyz, 1.0));\n"); + // NOTE: In through mode, we can ignore W, it's always 1.0. However, + // this transform will later be applied in all modes. + WRITE(p, " vec4 outPos;\n"); + WRITE(p, " outPos.xy = ((position.xy - u_xywh.xy * position.w) / u_xywh.zw) * 2.0 - 1.0;\n"); + WRITE(p, " outPos.zw = position.zw;\n"); + // WRITE(p, " vec4 outPos = mul(u_proj_through, vec4(position.xyz, 1.0));\n"); } else { // The viewport is used in this case, so need to compensate for that. if (gstate_c.Use(GPU_ROUND_DEPTH_TO_16BIT)) { diff --git a/GPU/GLES/ShaderManagerGLES.cpp b/GPU/GLES/ShaderManagerGLES.cpp index 1d1dcfd35e..87d744aaa1 100644 --- a/GPU/GLES/ShaderManagerGLES.cpp +++ b/GPU/GLES/ShaderManagerGLES.cpp @@ -105,7 +105,7 @@ LinkedShader::LinkedShader(GLRenderManager *render, VShaderID VSID, Shader *vs, queries.push_back({ &u_proj, "u_proj" }); queries.push_back({ &u_proj_lens, "u_proj_lens" }); - queries.push_back({ &u_proj_through, "u_proj_through" }); + queries.push_back({ &u_xywh, "u_xywh" }); queries.push_back({ &u_texenv, "u_texenv" }); queries.push_back({ &u_fogcolor, "u_fogcolor" }); queries.push_back({ &u_fogcoef, "u_fogcoef" }); @@ -443,15 +443,12 @@ void LinkedShader::UpdateUniforms(const ShaderID &vsid, bool useBufferedRenderin render_->SetUniformM4x4(&u_proj, flippedMatrix.m); } if (dirty & DIRTY_PROJTHROUGHMATRIX) { - Matrix4x4 proj_through; - proj_through.setOrthoVulkan(0.0f, gstate_c.curRTWidth, 0.0f, gstate_c.curRTHeight, 0.0f, 1.0f); - - // Negative RT offsets come from split framebuffers (Killzone) - if (gstate_c.curRTOffsetX < 0 || gstate_c.curRTOffsetY < 0) { - proj_through.wx += 2.0f * (float)gstate_c.curRTOffsetX / (float)gstate_c.curRTWidth; - proj_through.wy += 2.0f * (float)gstate_c.curRTOffsetY / (float)gstate_c.curRTHeight; - } - render_->SetUniformM4x4(&u_proj_through, proj_through.getReadPtr()); + float xywh[4]; + xywh[0] = (float)gstate_c.curRTOffsetX; + xywh[1] = (float)gstate_c.curRTOffsetY; + xywh[2] = (float)gstate_c.curRTWidth; + xywh[3] = (float)gstate_c.curRTHeight; + SetFloatUniform4(render_, &u_xywh, xywh); } if (dirty & DIRTY_TEXENV) { SetColorUniform3(render_, &u_texenv, gstate.texenvcolor); diff --git a/GPU/GLES/ShaderManagerGLES.h b/GPU/GLES/ShaderManagerGLES.h index 35fc2054e8..5082042ad2 100644 --- a/GPU/GLES/ShaderManagerGLES.h +++ b/GPU/GLES/ShaderManagerGLES.h @@ -60,7 +60,7 @@ public: int u_tex; int u_proj; int u_proj_lens; - int u_proj_through; + int u_xywh; int u_texenv; int u_view; int u_texmtx;