Add geometry shader generator for stereo 3D.

This commit is contained in:
Jules Blok 2014-10-16 18:52:32 +02:00
parent 6670cacddc
commit b005f61a2e
13 changed files with 209 additions and 25 deletions

View File

@ -73,7 +73,7 @@ FramebufferManager::FramebufferManager(int targetWidth, int targetHeight, int ms
m_efbDepth = glObj[1];
m_efbColorSwap = glObj[2];
m_EFBLayers = 1;
m_EFBLayers = (g_ActiveConfig.bStereo) ? 2 : 1;
// OpenGL MSAA textures are a different kind of texture type and must be allocated
// with a different function, so we create them separately.

View File

@ -36,6 +36,7 @@ ProgramShaderCache::PCacheEntry* ProgramShaderCache::last_entry;
SHADERUID ProgramShaderCache::last_uid;
UidChecker<PixelShaderUid,PixelShaderCode> ProgramShaderCache::pixel_uid_checker;
UidChecker<VertexShaderUid,VertexShaderCode> ProgramShaderCache::vertex_uid_checker;
UidChecker<GeometryShaderUid,ShaderCode> ProgramShaderCache::geometry_uid_checker;
static char s_glsl_header[1024] = "";
@ -196,13 +197,17 @@ SHADER* ProgramShaderCache::SetShader(DSTALPHA_MODE dstAlphaMode, u32 components
VertexShaderCode vcode;
PixelShaderCode pcode;
ShaderCode gcode;
GenerateVertexShaderCode(vcode, components, API_OPENGL);
GeneratePixelShaderCode(pcode, dstAlphaMode, API_OPENGL, components);
if (g_ActiveConfig.bStereo)
GenerateGeometryShaderCode(gcode, components, API_OPENGL);
if (g_ActiveConfig.bEnableShaderDebugging)
{
newentry.shader.strvprog = vcode.GetBuffer();
newentry.shader.strpprog = pcode.GetBuffer();
newentry.shader.strgprog = gcode.GetBuffer();
}
#if defined(_DEBUG) || defined(DEBUGFAST)
@ -214,10 +219,16 @@ SHADER* ProgramShaderCache::SetShader(DSTALPHA_MODE dstAlphaMode, u32 components
filename = StringFromFormat("%sps_%04i.txt", File::GetUserPath(D_DUMP_IDX).c_str(), counter++);
SaveData(filename, pcode.GetBuffer());
if (g_ActiveConfig.bStereo)
{
filename = StringFromFormat("%sgs_%04i.txt", File::GetUserPath(D_DUMP_IDX).c_str(), counter++);
SaveData(filename, gcode.GetBuffer());
}
}
#endif
if (!CompileShader(newentry.shader, vcode.GetBuffer(), pcode.GetBuffer()))
if (!CompileShader(newentry.shader, vcode.GetBuffer(), pcode.GetBuffer(), gcode.GetBuffer()))
{
GFX_DEBUGGER_PAUSE_AT(NEXT_ERROR, true);
return nullptr;
@ -231,15 +242,21 @@ SHADER* ProgramShaderCache::SetShader(DSTALPHA_MODE dstAlphaMode, u32 components
return &last_entry->shader;
}
bool ProgramShaderCache::CompileShader(SHADER& shader, const char* vcode, const char* pcode)
bool ProgramShaderCache::CompileShader(SHADER& shader, const char* vcode, const char* pcode, const char* gcode)
{
GLuint vsid = CompileSingleShader(GL_VERTEX_SHADER, vcode);
GLuint psid = CompileSingleShader(GL_FRAGMENT_SHADER, pcode);
// Optional geometry shader
GLuint gsid = 0;
if (gcode)
gsid = CompileSingleShader(GL_GEOMETRY_SHADER, gcode);
if (!vsid || !psid)
{
glDeleteShader(vsid);
glDeleteShader(psid);
glDeleteShader(gsid);
return false;
}
@ -247,6 +264,7 @@ bool ProgramShaderCache::CompileShader(SHADER& shader, const char* vcode, const
glAttachShader(pid, vsid);
glAttachShader(pid, psid);
glAttachShader(pid, gsid);
if (g_ogl_config.bSupportsGLSLCache)
glProgramParameteri(pid, GL_PROGRAM_BINARY_RETRIEVABLE_HINT, GL_TRUE);
@ -258,6 +276,7 @@ bool ProgramShaderCache::CompileShader(SHADER& shader, const char* vcode, const
// original shaders aren't needed any more
glDeleteShader(vsid);
glDeleteShader(psid);
glDeleteShader(gsid);
GLint linkStatus;
glGetProgramiv(pid, GL_LINK_STATUS, &linkStatus);
@ -273,7 +292,10 @@ bool ProgramShaderCache::CompileShader(SHADER& shader, const char* vcode, const
std::string filename = StringFromFormat("%sbad_p_%d.txt", File::GetUserPath(D_DUMP_IDX).c_str(), num_failures++);
std::ofstream file;
OpenFStream(file, filename, std::ios_base::out);
file << s_glsl_header << vcode << s_glsl_header << pcode << infoLog;
file << s_glsl_header << vcode << s_glsl_header << pcode;
if (gcode)
file << s_glsl_header << gcode;
file << infoLog;
file.close();
if (linkStatus != GL_TRUE)
@ -324,11 +346,11 @@ GLuint ProgramShaderCache::CompileSingleShader(GLuint type, const char* code)
GLsizei charsWritten;
GLchar* infoLog = new GLchar[length];
glGetShaderInfoLog(result, length, &charsWritten, infoLog);
ERROR_LOG(VIDEO, "%s Shader info log:\n%s", type==GL_VERTEX_SHADER ? "VS" : "PS", infoLog);
ERROR_LOG(VIDEO, "%s Shader info log:\n%s", type==GL_VERTEX_SHADER ? "VS" : type==GL_FRAGMENT_SHADER ? "PS" : "GS", infoLog);
std::string filename = StringFromFormat("%sbad_%s_%04i.txt",
File::GetUserPath(D_DUMP_IDX).c_str(),
type==GL_VERTEX_SHADER ? "vs" : "ps",
type==GL_VERTEX_SHADER ? "vs" : type==GL_FRAGMENT_SHADER ? "ps" : "gs",
num_failures++);
std::ofstream file;
OpenFStream(file, filename, std::ios_base::out);
@ -338,7 +360,7 @@ GLuint ProgramShaderCache::CompileSingleShader(GLuint type, const char* code)
if (compileStatus != GL_TRUE)
{
PanicAlert("Failed to compile %s shader!\nThis usually happens when trying to use Dolphin with an outdated GPU or integrated GPU like the Intel GMA series.\n\nIf you're sure this is Dolphin's error anyway, post the contents of %s along with this error message at the forums.\n\nDebug info (%s, %s, %s):\n%s",
type == GL_VERTEX_SHADER ? "vertex" : "pixel",
type == GL_VERTEX_SHADER ? "vertex" : type==GL_FRAGMENT_SHADER ? "pixel" : "geometry",
filename.c_str(),
g_ogl_config.gl_vendor,
g_ogl_config.gl_renderer,
@ -365,6 +387,8 @@ void ProgramShaderCache::GetShaderId(SHADERUID* uid, DSTALPHA_MODE dstAlphaMode,
{
GetPixelShaderUid(uid->puid, dstAlphaMode, API_OPENGL, components);
GetVertexShaderUid(uid->vuid, components, API_OPENGL);
if (g_ActiveConfig.bStereo)
GetGeometryShaderUid(uid->guid, components, API_OPENGL);
if (g_ActiveConfig.bEnableShaderDebugging)
{
@ -375,6 +399,10 @@ void ProgramShaderCache::GetShaderId(SHADERUID* uid, DSTALPHA_MODE dstAlphaMode,
VertexShaderCode vcode;
GenerateVertexShaderCode(vcode, components, API_OPENGL);
vertex_uid_checker.AddToIndexAndCheck(vcode, uid->vuid, "Vertex", "v");
ShaderCode gcode;
GenerateGeometryShaderCode(gcode, components, API_OPENGL);
geometry_uid_checker.AddToIndexAndCheck(gcode, uid->guid, "Geometry", "g");
}
}
@ -486,6 +514,7 @@ void ProgramShaderCache::CreateHeader()
"%s\n" // sample shading
"%s\n" // Sampler binding
"%s\n" // storage buffer
"%s\n" // shader5
// Precision defines for GLSL ES
"%s\n"
@ -518,6 +547,7 @@ void ProgramShaderCache::CreateHeader()
, (g_ogl_config.bSupportSampleShading) ? "#extension GL_ARB_sample_shading : enable" : ""
, g_ActiveConfig.backend_info.bSupportsBindingLayout ? "#define SAMPLER_BINDING(x) layout(binding = x)" : "#define SAMPLER_BINDING(x)"
, g_ActiveConfig.backend_info.bSupportsBBox ? "#extension GL_ARB_shader_storage_buffer_object : enable" : ""
, (g_ogl_config.bSupportGSInvocation) ? "#extension GL_ARB_gpu_shader5 : enable" : ""
, v>=GLSLES_300 ? "precision highp float;" : ""
, v>=GLSLES_300 ? "precision highp int;" : ""

View File

@ -9,6 +9,7 @@
#include "VideoBackends/OGL/GLUtil.h"
#include "VideoCommon/PixelShaderGen.h"
#include "VideoCommon/VertexShaderGen.h"
#include "VideoCommon/GeometryShaderGen.h"
namespace OGL
{
@ -18,10 +19,11 @@ class SHADERUID
public:
VertexShaderUid vuid;
PixelShaderUid puid;
GeometryShaderUid guid;
SHADERUID() {}
SHADERUID(const SHADERUID& r) : vuid(r.vuid), puid(r.puid) {}
SHADERUID(const SHADERUID& r) : vuid(r.vuid), puid(r.puid), guid(r.guid) {}
bool operator <(const SHADERUID& r) const
{
@ -34,12 +36,18 @@ public:
if (vuid < r.vuid)
return true;
if (r.vuid < vuid)
return false;
if (guid < r.guid)
return true;
return false;
}
bool operator ==(const SHADERUID& r) const
{
return puid == r.puid && vuid == r.vuid;
return puid == r.puid && vuid == r.vuid && guid == r.guid;
}
};
@ -54,7 +62,7 @@ struct SHADER
}
GLuint glprogid; // opengl program id
std::string strvprog, strpprog;
std::string strvprog, strpprog, strgprog;
void SetProgramVariables();
void SetProgramBindings();
@ -83,7 +91,7 @@ public:
static SHADER* SetShader(DSTALPHA_MODE dstAlphaMode, u32 components);
static void GetShaderId(SHADERUID *uid, DSTALPHA_MODE dstAlphaMode, u32 components);
static bool CompileShader(SHADER &shader, const char* vcode, const char* pcode);
static bool CompileShader(SHADER &shader, const char* vcode, const char* pcode, const char* gcode = nullptr);
static GLuint CompileSingleShader(GLuint type, const char *code);
static void UploadConstants();
@ -104,6 +112,7 @@ private:
static UidChecker<PixelShaderUid,PixelShaderCode> pixel_uid_checker;
static UidChecker<VertexShaderUid,VertexShaderCode> vertex_uid_checker;
static UidChecker<GeometryShaderUid,ShaderCode> geometry_uid_checker;
static u32 s_ubo_buffer_size;
static s32 s_ubo_align;

View File

@ -491,6 +491,7 @@ Renderer::Renderer()
g_ogl_config.bSupportSampleShading = GLExtensions::Supports("GL_ARB_sample_shading");
g_ogl_config.bSupportOGL31 = GLExtensions::Version() >= 310;
g_ogl_config.bSupportViewportFloat = GLExtensions::Supports("GL_ARB_viewport_array");
g_ogl_config.bSupportGSInvocation = GLExtensions::Supports("GL_ARB_gpu_shader5");
if (GLInterface->GetMode() == GLInterfaceMode::MODE_OPENGLES3)
{

View File

@ -30,6 +30,7 @@ struct VideoConfig
GLSL_VERSION eSupportedGLSLVersion;
bool bSupportOGL31;
bool bSupportViewportFloat;
bool bSupportGSInvocation;
const char* gl_vendor;
const char* gl_renderer;

View File

@ -38,7 +38,8 @@ set(SRCS BoundingBox.cpp
VideoConfig.cpp
VideoState.cpp
XFMemory.cpp
XFStructs.cpp)
XFStructs.cpp
GeometryShaderGen.cpp)
set(LIBS core png)
if(_M_X86)

View File

@ -0,0 +1,109 @@
// Copyright 2013 Dolphin Emulator Project
// Licensed under GPLv2
// Refer to the license.txt file included.
#include <cmath>
#include <locale.h>
#ifdef __APPLE__
#include <xlocale.h>
#endif
#include "VideoCommon/GeometryShaderGen.h"
#include "VideoCommon/VideoConfig.h"
#include "VideoCommon/VertexShaderGen.h"
#include "VideoCommon/LightingShaderGen.h"
static char text[16384];
template<class T>
static inline void GenerateGeometryShader(T& out, u32 components, API_TYPE ApiType)
{
// Non-uid template parameters will write to the dummy data (=> gets optimized out)
geometry_shader_uid_data dummy_data;
geometry_shader_uid_data* uid_data = out.template GetUidData<geometry_shader_uid_data>();
if (uid_data == nullptr)
uid_data = &dummy_data;
out.SetBuffer(text);
const bool is_writing_shadercode = (out.GetBuffer() != nullptr);
#ifndef ANDROID
locale_t locale;
locale_t old_locale;
if (is_writing_shadercode)
{
locale = newlocale(LC_NUMERIC_MASK, "C", nullptr); // New locale for compilation
old_locale = uselocale(locale); // Apply the locale for this thread
}
#endif
if (is_writing_shadercode)
text[sizeof(text) - 1] = 0x7C; // canary
out.Write("//Geometry Shader for 3D stereoscopy\n");
if (ApiType == API_OPENGL)
{
// Insert layout parameters
out.Write("layout(triangles, invocations = %d) in;\n", g_ActiveConfig.bStereo ? 2 : 1);
out.Write("layout(triangle_strip, max_vertices = 3) out;\n");
}
out.Write("%s", s_lighting_struct);
// uniforms
/*if (ApiType == API_OPENGL)
out.Write("layout(std140%s) uniform VSBlock {\n", g_ActiveConfig.backend_info.bSupportsBindingLayout ? ", binding = 2" : "");
else
out.Write("cbuffer VSBlock {\n");
out.Write(
"\tfloat4 " I_POSNORMALMATRIX"[6];\n"
"\tfloat4 " I_PROJECTION"[4];\n"
"\tint4 " I_MATERIALS"[4];\n"
"\tLight " I_LIGHTS"[8];\n"
"\tfloat4 " I_TEXMATRICES"[24];\n"
"\tfloat4 " I_TRANSFORMMATRICES"[64];\n"
"\tfloat4 " I_NORMALMATRICES"[32];\n"
"\tfloat4 " I_POSTTRANSFORMMATRICES"[64];\n"
"\tfloat4 " I_DEPTHPARAMS";\n"
"};\n");*/
ShaderCode code;
char buf[16384];
code.SetBuffer(buf);
GenerateVSOutputStructForGS(code, ApiType);
out.Write(code.GetBuffer());
out.Write("in VS_OUTPUT vertices[];\n");
out.Write("out VS_OUTPUT frag;\n");
out.Write("void main()\n{\n");
out.Write("\tfor (int i = 0; i < gl_in.length(); ++i) {\n");
out.Write("\t\tfrag = vertices[i];\n");
out.Write("\t\tgl_Position = gl_in[i].gl_Position;\n");
out.Write("\t\tgl_Layer = gl_InvocationID;\n");
out.Write("\t\tEmitVertex();\n");
out.Write("\t}\n");
out.Write("\tEndPrimitive();\n");
out.Write("}\n");
if (is_writing_shadercode)
{
if (text[sizeof(text) - 1] != 0x7C)
PanicAlert("GeometryShader generator - buffer too small, canary has been eaten!");
#ifndef ANDROID
uselocale(old_locale); // restore locale
freelocale(locale);
#endif
}
}
void GetGeometryShaderUid(GeometryShaderUid& object, u32 components, API_TYPE ApiType)
{
GenerateGeometryShader<GeometryShaderUid>(object, components, ApiType);
}
void GenerateGeometryShaderCode(ShaderCode& object, u32 components, API_TYPE ApiType)
{
GenerateGeometryShader<ShaderCode>(object, components, ApiType);
}

View File

@ -0,0 +1,25 @@
// Copyright 2013 Dolphin Emulator Project
// Licensed under GPLv2
// Refer to the license.txt file included.
#pragma once
#include "VideoCommon/ShaderGenCommon.h"
#include "VideoCommon/VideoCommon.h"
#pragma pack(1)
struct geometry_shader_uid_data
{
u32 NumValues() const { return sizeof(geometry_shader_uid_data); }
u32 components : 23;
u32 stereo : 1;
};
#pragma pack()
typedef ShaderUid<geometry_shader_uid_data> GeometryShaderUid;
void GenerateGeometryShaderCode(ShaderCode& object, u32 components, API_TYPE ApiType);
void GetGeometryShaderUid(GeometryShaderUid& object, u32 components, API_TYPE ApiType);

View File

@ -489,7 +489,7 @@ void VertexShaderManager::SetConstants()
PRIM_LOG("Projection: %f %f %f %f %f %f\n", rawProjection[0], rawProjection[1], rawProjection[2], rawProjection[3], rawProjection[4], rawProjection[5]);
if ((g_ActiveConfig.bFreeLook || g_ActiveConfig.bAnaglyphStereo ) && xfmem.projection.type == GX_PERSPECTIVE)
if (g_ActiveConfig.bFreeLook && xfmem.projection.type == GX_PERSPECTIVE)
{
Matrix44 mtxA;
Matrix44 mtxB;

View File

@ -60,6 +60,7 @@
<ClCompile Include="PostProcessing.cpp" />
<ClCompile Include="RenderBase.cpp" />
<ClCompile Include="Statistics.cpp" />
<ClCompile Include="GeometryShaderGen.cpp" />
<ClCompile Include="TextureCacheBase.cpp" />
<ClCompile Include="TextureConversionShader.cpp" />
<ClCompile Include="VertexLoader.cpp" />
@ -110,6 +111,7 @@
<ClInclude Include="RenderBase.h" />
<ClInclude Include="ShaderGenCommon.h" />
<ClInclude Include="Statistics.h" />
<ClInclude Include="GeometryShaderGen.h" />
<ClInclude Include="TextureCacheBase.h" />
<ClInclude Include="TextureConversionShader.h" />
<ClInclude Include="TextureDecoder.h" />

View File

@ -143,6 +143,9 @@
<ClCompile Include="BoundingBox.cpp">
<Filter>Util</Filter>
</ClCompile>
<ClCompile Include="GeometryShaderGen.cpp">
<Filter>Shader Generators</Filter>
</ClCompile>
</ItemGroup>
<ItemGroup>
<ClInclude Include="CommandProcessor.h" />
@ -275,6 +278,9 @@
<ClInclude Include="BoundingBox.h">
<Filter>Util</Filter>
</ClInclude>
<ClInclude Include="GeometryShaderGen.h">
<Filter>Shader Generators</Filter>
</ClInclude>
</ItemGroup>
<ItemGroup>
<Text Include="CMakeLists.txt" />

View File

@ -66,9 +66,9 @@ void VideoConfig::Load(const std::string& ini_file)
settings->Get("DumpEFBTarget", &bDumpEFBTarget, 0);
settings->Get("FreeLook", &bFreeLook, 0);
settings->Get("UseFFV1", &bUseFFV1, 0);
settings->Get("AnaglyphStereo", &bAnaglyphStereo, false);
settings->Get("AnaglyphStereoSeparation", &iAnaglyphStereoSeparation, 200);
settings->Get("AnaglyphFocalAngle", &iAnaglyphFocalAngle, 0);
settings->Get("Stereo", &bStereo, false);
settings->Get("StereoSeparation", &iStereoSeparation, 200);
settings->Get("StereoFocalAngle", &iStereoFocalAngle, 0);
settings->Get("EnablePixelLighting", &bEnablePixelLighting, 0);
settings->Get("FastDepthCalc", &bFastDepthCalc, true);
settings->Get("MSAA", &iMultisampleMode, 0);
@ -140,9 +140,9 @@ void VideoConfig::GameIniLoad()
CHECK_SETTING("Video_Settings", "UseRealXFB", bUseRealXFB);
CHECK_SETTING("Video_Settings", "SafeTextureCacheColorSamples", iSafeTextureCache_ColorSamples);
CHECK_SETTING("Video_Settings", "HiresTextures", bHiresTextures);
CHECK_SETTING("Video_Settings", "AnaglyphStereo", bAnaglyphStereo);
CHECK_SETTING("Video_Settings", "AnaglyphStereoSeparation", iAnaglyphStereoSeparation);
CHECK_SETTING("Video_Settings", "AnaglyphFocalAngle", iAnaglyphFocalAngle);
CHECK_SETTING("Video_Settings", "Stereo", bStereo);
CHECK_SETTING("Video_Settings", "StereoSeparation", iStereoSeparation);
CHECK_SETTING("Video_Settings", "StereoFocalAngle", iStereoFocalAngle);
CHECK_SETTING("Video_Settings", "EnablePixelLighting", bEnablePixelLighting);
CHECK_SETTING("Video_Settings", "FastDepthCalc", bFastDepthCalc);
CHECK_SETTING("Video_Settings", "MSAA", iMultisampleMode);
@ -230,9 +230,9 @@ void VideoConfig::Save(const std::string& ini_file)
settings->Set("DumpEFBTarget", bDumpEFBTarget);
settings->Set("FreeLook", bFreeLook);
settings->Set("UseFFV1", bUseFFV1);
settings->Set("AnaglyphStereo", bAnaglyphStereo);
settings->Set("AnaglyphStereoSeparation", iAnaglyphStereoSeparation);
settings->Set("AnaglyphFocalAngle", iAnaglyphFocalAngle);
settings->Set("Stereo", bStereo);
settings->Set("StereoSeparation", iStereoSeparation);
settings->Set("StereoFocalAngle", iStereoFocalAngle);
settings->Set("EnablePixelLighting", bEnablePixelLighting);
settings->Set("FastDepthCalc", bFastDepthCalc);
settings->Set("ShowEFBCopyRegions", bShowEFBCopyRegions);

View File

@ -92,9 +92,9 @@ struct VideoConfig final
bool bDumpEFBTarget;
bool bUseFFV1;
bool bFreeLook;
bool bAnaglyphStereo;
int iAnaglyphStereoSeparation;
int iAnaglyphFocalAngle;
bool bStereo;
int iStereoSeparation;
int iStereoFocalAngle;
bool bBorderlessFullscreen;
// Hacks