From 6cacfad010b05cd9d79056a81c4dc7d19ca57273 Mon Sep 17 00:00:00 2001 From: Jules Blok Date: Thu, 30 Oct 2014 02:08:15 +0100 Subject: [PATCH] GeometryShader: Transform the projection within the geometry shader. Reduces the amount of data transferred through uniforms. The shearing transformation is reduced to a single multiplication/addition for optimization. --- Source/Core/VideoCommon/ConstantManager.h | 2 +- Source/Core/VideoCommon/GeometryShaderGen.cpp | 12 ++++++----- Source/Core/VideoCommon/ShaderGenCommon.h | 4 ++-- .../Core/VideoCommon/VertexShaderManager.cpp | 20 ++++--------------- 4 files changed, 14 insertions(+), 24 deletions(-) diff --git a/Source/Core/VideoCommon/ConstantManager.h b/Source/Core/VideoCommon/ConstantManager.h index 1c3ad55778..e31806bbd1 100644 --- a/Source/Core/VideoCommon/ConstantManager.h +++ b/Source/Core/VideoCommon/ConstantManager.h @@ -43,6 +43,6 @@ struct VertexShaderConstants float4 normalmatrices[32]; float4 posttransformmatrices[64]; float4 depthparams; - float4 stereoprojection[8]; + float4 stereooffset; }; diff --git a/Source/Core/VideoCommon/GeometryShaderGen.cpp b/Source/Core/VideoCommon/GeometryShaderGen.cpp index f016acd6f4..6a39e2ca1d 100644 --- a/Source/Core/VideoCommon/GeometryShaderGen.cpp +++ b/Source/Core/VideoCommon/GeometryShaderGen.cpp @@ -70,16 +70,18 @@ static inline void GenerateGeometryShader(T& out, u32 components, API_TYPE ApiTy out.Write("flat out int layer;\n"); out.Write("void main()\n{\n"); - if (!g_ActiveConfig.backend_info.bSupportsGSInstancing) + if (g_ActiveConfig.backend_info.bSupportsGSInstancing) + out.Write("\tlayer = gl_InvocationID;\n"); + else out.Write("\tfor (layer = 0; layer < %d; ++layer) {\n", g_ActiveConfig.bStereo ? 2 : 1); + out.Write("\tgl_Layer = layer;\n"); + out.Write("\tvec4 stereoproj = "I_PROJECTION"[0];\n"); + out.Write("\tstereoproj[2] += "I_STEREOOFFSET"[layer] * stereoproj[0];\n"); out.Write("\tfor (int i = 0; i < gl_in.length(); ++i) {\n"); out.Write("\t\to = v[i];\n"); - if (g_ActiveConfig.backend_info.bSupportsGSInstancing) - out.Write("\t\tlayer = gl_InvocationID;\n"); if (g_ActiveConfig.bStereo) - out.Write("\t\to.pos = float4(dot(" I_STEREOPROJECTION"[layer * 4 + 0], v[i].rawpos), dot(" I_STEREOPROJECTION"[layer * 4 + 1], v[i].rawpos), dot(" I_STEREOPROJECTION"[layer * 4 + 2], v[i].rawpos), dot(" I_STEREOPROJECTION"[layer * 4 + 3], v[i].rawpos)); \n"); + out.Write("\t\to.pos = float4(dot(stereoproj, v[i].rawpos), dot(" I_PROJECTION"[1], v[i].rawpos), dot(" I_PROJECTION"[2], v[i].rawpos), dot(" I_PROJECTION"[3], v[i].rawpos)); \n"); out.Write("\t\tgl_Position = o.pos;\n"); - out.Write("\t\tgl_Layer = layer;\n"); out.Write("\t\tEmitVertex();\n"); out.Write("\t}\n"); out.Write("\tEndPrimitive();\n"); diff --git a/Source/Core/VideoCommon/ShaderGenCommon.h b/Source/Core/VideoCommon/ShaderGenCommon.h index 11a5ccafa8..5e9e24e7fa 100644 --- a/Source/Core/VideoCommon/ShaderGenCommon.h +++ b/Source/Core/VideoCommon/ShaderGenCommon.h @@ -239,7 +239,7 @@ private: #define I_NORMALMATRICES "cnmtx" #define I_POSTTRANSFORMMATRICES "cpostmtx" #define I_DEPTHPARAMS "cDepth" // farZ, zRange -#define I_STEREOPROJECTION "csproj" +#define I_STEREOOFFSET "csoffset" static const char s_shader_uniforms[] = "\tfloat4 " I_POSNORMALMATRIX"[6];\n" @@ -251,4 +251,4 @@ static const char s_shader_uniforms[] = "\tfloat4 " I_NORMALMATRICES"[32];\n" "\tfloat4 " I_POSTTRANSFORMMATRICES"[64];\n" "\tfloat4 " I_DEPTHPARAMS";\n" - "\tfloat4 " I_STEREOPROJECTION"[8];\n"; + "\tfloat4 " I_STEREOOFFSET";\n"; diff --git a/Source/Core/VideoCommon/VertexShaderManager.cpp b/Source/Core/VideoCommon/VertexShaderManager.cpp index e40eb92152..5d65a49186 100644 --- a/Source/Core/VideoCommon/VertexShaderManager.cpp +++ b/Source/Core/VideoCommon/VertexShaderManager.cpp @@ -515,25 +515,13 @@ void VertexShaderManager::SetConstants() if (g_ActiveConfig.bStereo && xfmem.projection.type == GX_PERSPECTIVE) { - Matrix44 projMtx; - Matrix44::Set(projMtx, g_fProjectionMatrix); - - Matrix44 leftShearMtx, rightShearMtx; - Matrix44::Shear(leftShearMtx, g_ActiveConfig.iStereoSeparation / (200.0f * g_ActiveConfig.iStereoFocalLength)); - Matrix44::Shear(rightShearMtx, -g_ActiveConfig.iStereoSeparation / (200.0f * g_ActiveConfig.iStereoFocalLength)); - - Matrix44 leftProjMtx, rightProjMtx, leftCorrectedMtx, rightCorrectedMtx; - Matrix44::Multiply(projMtx, leftShearMtx, leftProjMtx); - Matrix44::Multiply(s_viewportCorrection, leftProjMtx, leftCorrectedMtx); - Matrix44::Multiply(projMtx, rightShearMtx, rightProjMtx); - Matrix44::Multiply(s_viewportCorrection, rightProjMtx, rightCorrectedMtx); - memcpy(constants.stereoprojection, leftCorrectedMtx.data, 4*16); - memcpy(constants.stereoprojection + 4, rightCorrectedMtx.data, 4*16); + float offset = g_ActiveConfig.iStereoSeparation / (200.0f * g_ActiveConfig.iStereoFocalLength); + constants.stereooffset[0] = offset; + constants.stereooffset[1] = -offset; } else { - memcpy(constants.stereoprojection, constants.projection, 4 * 16); - memcpy(constants.stereoprojection + 4, constants.projection, 4 * 16); + constants.stereooffset[0] = constants.stereooffset[1] = 0; } dirty = true;