From 382e1c22db91d863b55d6223d4893cb629dab810 Mon Sep 17 00:00:00 2001 From: Jules Blok Date: Sun, 14 Dec 2014 22:27:09 +0100 Subject: [PATCH] GeometryShaderGen: Support multiple primitive types. And make more stereoscopy code optional. --- .../Core/VideoBackends/D3D/VertexManager.cpp | 5 -- .../VideoBackends/OGL/ProgramShaderCache.cpp | 2 +- Source/Core/VideoCommon/BPStructs.cpp | 1 - Source/Core/VideoCommon/GeometryShaderGen.cpp | 79 ++++++++++++------- 4 files changed, 53 insertions(+), 34 deletions(-) diff --git a/Source/Core/VideoBackends/D3D/VertexManager.cpp b/Source/Core/VideoBackends/D3D/VertexManager.cpp index 0bb9735994..6ba3bc3391 100644 --- a/Source/Core/VideoBackends/D3D/VertexManager.cpp +++ b/Source/Core/VideoBackends/D3D/VertexManager.cpp @@ -12,7 +12,6 @@ #include "VideoBackends/D3D/VertexShaderCache.h" #include "VideoCommon/BoundingBox.h" -#include "VideoCommon/BPMemory.h" #include "VideoCommon/Debugger.h" #include "VideoCommon/IndexGenerator.h" #include "VideoCommon/MainBase.h" @@ -122,10 +121,6 @@ void VertexManager::PrepareDrawBuffers(u32 stride) ADDSTAT(stats.thisFrame.bytesIndexStreamed, indexBufferSize); } -static const float LINE_PT_TEX_OFFSETS[8] = { - 0.f, 0.0625f, 0.125f, 0.25f, 0.5f, 1.f, 1.f, 1.f -}; - void VertexManager::Draw(u32 stride) { u32 components = VertexLoaderManager::GetCurrentVertexFormat()->m_components; diff --git a/Source/Core/VideoBackends/OGL/ProgramShaderCache.cpp b/Source/Core/VideoBackends/OGL/ProgramShaderCache.cpp index 8a159ea9a6..d2ec0d049b 100644 --- a/Source/Core/VideoBackends/OGL/ProgramShaderCache.cpp +++ b/Source/Core/VideoBackends/OGL/ProgramShaderCache.cpp @@ -398,7 +398,7 @@ void ProgramShaderCache::GetShaderId(SHADERUID* uid, DSTALPHA_MODE dstAlphaMode, { GetPixelShaderUid(uid->puid, dstAlphaMode, API_OPENGL, components); GetVertexShaderUid(uid->vuid, components, API_OPENGL); - GetGeometryShaderUid(uid->guid, components, API_OPENGL); + GetGeometryShaderUid(uid->guid, primitive_type, API_OPENGL); if (g_ActiveConfig.bEnableShaderDebugging) { diff --git a/Source/Core/VideoCommon/BPStructs.cpp b/Source/Core/VideoCommon/BPStructs.cpp index fdf3e15ff2..6a32b7f245 100644 --- a/Source/Core/VideoCommon/BPStructs.cpp +++ b/Source/Core/VideoCommon/BPStructs.cpp @@ -129,7 +129,6 @@ static void BPWritten(const BPCmd& bp) GeometryShaderManager::SetViewportChanged(); return; case BPMEM_LINEPTWIDTH: // Line Width - SetLineWidth(); GeometryShaderManager::SetLinePtWidthChanged(); return; case BPMEM_ZMODE: // Depth Control diff --git a/Source/Core/VideoCommon/GeometryShaderGen.cpp b/Source/Core/VideoCommon/GeometryShaderGen.cpp index d0e9eb7144..ca4ef96a43 100644 --- a/Source/Core/VideoCommon/GeometryShaderGen.cpp +++ b/Source/Core/VideoCommon/GeometryShaderGen.cpp @@ -6,11 +6,26 @@ #include "VideoCommon/GeometryShaderGen.h" #include "VideoCommon/LightingShaderGen.h" +#include "VideoCommon/VertexManagerBase.h" #include "VideoCommon/VertexShaderGen.h" #include "VideoCommon/VideoConfig.h" static char text[16384]; +static const char* primitives_ogl[] = +{ + "points", + "lines", + "triangles" +}; + +static const char* primitives_d3d[] = +{ + "point", + "line", + "triangle" +}; + template static inline void GenerateGeometryShader(T& out, u32 primitive_type, API_TYPE ApiType) { @@ -27,15 +42,18 @@ static inline void GenerateGeometryShader(T& out, u32 primitive_type, API_TYPE A text[sizeof(text) - 1] = 0x7C; // canary uid_data->primitive_type = primitive_type; + const unsigned int vertex_in = primitive_type + 1; uid_data->stereo = g_ActiveConfig.iStereoMode > 0; + const unsigned int vertex_out = primitive_type == PRIMITIVE_TRIANGLES ? 3 : 4; + if (ApiType == API_OPENGL) { // Insert layout parameters if (g_ActiveConfig.backend_info.bSupportsGSInstancing) - out.Write("layout(triangles, invocations = %d) in;\n", g_ActiveConfig.iStereoMode > 0 ? 2 : 1); + out.Write("layout(%s, invocations = %d) in;\n", primitives_ogl[primitive_type], g_ActiveConfig.iStereoMode > 0 ? 2 : 1); else - out.Write("layout(triangles) in;\n"); - out.Write("layout(triangle_strip, max_vertices = %d) out;\n", g_ActiveConfig.backend_info.bSupportsGSInstancing ? 3 : 6); + out.Write("layout(%s) in;\n", primitives_ogl[primitive_type]); + out.Write("layout(triangle_strip, max_vertices = %d) out;\n", g_ActiveConfig.backend_info.bSupportsGSInstancing ? vertex_out : vertex_out * 2); } out.Write("%s", s_lighting_struct); @@ -62,9 +80,11 @@ static inline void GenerateGeometryShader(T& out, u32 primitive_type, API_TYPE A if (g_ActiveConfig.backend_info.bSupportsGSInstancing) out.Write("#define InstanceID gl_InvocationID\n"); - out.Write("centroid in VS_OUTPUT o[3];\n"); + out.Write("centroid in VS_OUTPUT o[%d];\n", vertex_in); out.Write("centroid out VS_OUTPUT vs;\n"); - out.Write("flat out int layer;\n"); + + if (g_ActiveConfig.iStereoMode > 0) + out.Write("flat out int layer;\n"); out.Write("void main()\n{\n"); } @@ -72,18 +92,21 @@ static inline void GenerateGeometryShader(T& out, u32 primitive_type, API_TYPE A { out.Write("struct GS_OUTPUT {\n"); out.Write("\tVS_OUTPUT vs;\n"); - out.Write("\tuint layer : SV_RenderTargetArrayIndex;\n"); + + if (g_ActiveConfig.iStereoMode > 0) + out.Write("\tuint layer : SV_RenderTargetArrayIndex;\n"); + out.Write("};\n"); + out.Write("[maxvertexcount(%d)]\n", vertex_out); if (g_ActiveConfig.backend_info.bSupportsGSInstancing) { - out.Write("[maxvertexcount(3)]\n[instance(%d)]\n", g_ActiveConfig.iStereoMode > 0 ? 2 : 1); - out.Write("void main(triangle VS_OUTPUT o[3], inout TriangleStream Output, in uint InstanceID : SV_GSInstanceID)\n{\n"); + out.Write("[instance(%d)]\n", g_ActiveConfig.iStereoMode > 0 ? 2 : 1); + out.Write("void main(%s VS_OUTPUT o[%d], inout TriangleStream Output, in uint InstanceID : SV_GSInstanceID)\n{\n", primitives_d3d[primitive_type], vertex_in); } else { - out.Write("[maxvertexcount(6)]\n"); - out.Write("void main(triangle VS_OUTPUT o[3], inout TriangleStream Output)\n{\n"); + out.Write("void main(%s VS_OUTPUT o[%d], inout TriangleStream Output)\n{\n", primitives_d3d[primitive_type], vertex_in); } out.Write("\tGS_OUTPUT gs;\n"); @@ -91,29 +114,32 @@ static inline void GenerateGeometryShader(T& out, u32 primitive_type, API_TYPE A out.Write("\tVS_OUTPUT f;\n"); - // If the GPU supports invocation we don't need a for loop and can simply use the - // invocation identifier to determine which layer we're rendering. - if (g_ActiveConfig.backend_info.bSupportsGSInstancing) - out.Write("\tint eye = InstanceID;\n"); - else - out.Write("\tfor (int eye = 0; eye < %d; ++eye) {\n", g_ActiveConfig.iStereoMode > 0 ? 2 : 1); - - out.Write("\tfor (int i = 0; i < 3; ++i) {\n"); - - // Select the output layer - if (ApiType == API_OPENGL) + if (g_ActiveConfig.iStereoMode > 0) { - out.Write("\t\tgl_Layer = eye;\n"); - out.Write("\t\tlayer = eye;\n"); + // If the GPU supports invocation we don't need a for loop and can simply use the + // invocation identifier to determine which layer we're rendering. + if (g_ActiveConfig.backend_info.bSupportsGSInstancing) + out.Write("\tint eye = InstanceID;\n"); + else + out.Write("\tfor (int eye = 0; eye < %d; ++eye) {\n", g_ActiveConfig.iStereoMode > 0 ? 2 : 1); } - else - out.Write("\t\tgs.layer = eye;\n"); + + out.Write("\tfor (int i = 0; i < %d; ++i) {\n", vertex_in); out.Write("\t\tf = o[i];\n"); out.Write("\t\tfloat4 pos = o[i].pos;\n"); if (g_ActiveConfig.iStereoMode > 0) { + // Select the output layer + if (ApiType == API_OPENGL) + { + out.Write("\t\tgl_Layer = eye;\n"); + out.Write("\t\tlayer = eye;\n"); + } + else + out.Write("\t\tgs.layer = eye;\n"); + // For stereoscopy add a small horizontal offset in Normalized Device Coordinates proportional // to the depth of the vertex. We retrieve the depth value from the w-component of the projected // vertex which contains the negated z-component of the original vertex. @@ -123,10 +149,9 @@ static inline void GenerateGeometryShader(T& out, u32 primitive_type, API_TYPE A // This formula is based on page 13 of the "Nvidia 3D Vision Automatic, Best Practices Guide" out.Write("\t\tf.clipPos.x = o[i].clipPos.x + " I_STEREOPARAMS"[eye] * (o[i].clipPos.w - " I_STEREOPARAMS"[2]);\n"); out.Write("\t\tpos.x = o[i].pos.x + " I_STEREOPARAMS"[eye] * (o[i].pos.w - " I_STEREOPARAMS"[2]);\n"); + out.Write("\t\tf.pos.x = pos.x;\n"); } - out.Write("\t\tf.pos.x = pos.x;\n"); - if (ApiType == API_OPENGL) out.Write("\t\tgl_Position = pos;\n");