From b20176b74f28c05ec62ac8bbaeb80ff0ae6bb119 Mon Sep 17 00:00:00 2001 From: Ryan Houdek Date: Wed, 30 Nov 2011 21:00:21 -0600 Subject: [PATCH 01/83] Add in GLSL setting again. PS and VS making. Untested and won't work for now. Add in program shader cache files. Readd NativeVertexFormat stuffs. Add in PS and VS cache things. SetShaders in places. Fixed EFB cache index computations in OpenGL renderer. The previous computation was very likely to go out of array bounds, which could result in crashes on EFB access. Also, the cache size was rounded down instead of up. This is a problem since EFB_HEIGHT (528) is not a multiple of EFB_CACHE_RECT_SIZE (64). --- .../Core/VideoCommon/Src/PixelShaderGen.cpp | 188 ++++-- Source/Core/VideoCommon/Src/PixelShaderGen.h | 13 + .../Core/VideoCommon/Src/VertexShaderGen.cpp | 191 ++++-- Source/Core/VideoCommon/Src/VertexShaderGen.h | 11 +- Source/Core/VideoCommon/Src/VideoCommon.h | 6 + Source/Core/VideoCommon/Src/VideoConfig.cpp | 4 + Source/Core/VideoCommon/Src/VideoConfig.h | 2 + Source/Plugins/Plugin_VideoDX11/Src/main.cpp | 1 + Source/Plugins/Plugin_VideoDX9/Src/main.cpp | 1 + Source/Plugins/Plugin_VideoOGL/CMakeLists.txt | 1 + .../Src/NativeVertexFormat.cpp | 18 +- .../Plugin_VideoOGL/Src/PixelShaderCache.cpp | 578 ++++++++++-------- .../Plugin_VideoOGL/Src/PixelShaderCache.h | 10 + .../Src/ProgramShaderCache.cpp | 150 +++++ .../Plugin_VideoOGL/Src/ProgramShaderCache.h | 108 ++++ Source/Plugins/Plugin_VideoOGL/Src/Render.cpp | 28 +- .../Plugin_VideoOGL/Src/VertexManager.cpp | 27 +- .../Plugin_VideoOGL/Src/VertexShaderCache.cpp | 414 ++++++++----- .../Plugin_VideoOGL/Src/VertexShaderCache.h | 10 + Source/Plugins/Plugin_VideoOGL/Src/main.cpp | 1 + 20 files changed, 1243 insertions(+), 519 deletions(-) create mode 100644 Source/Plugins/Plugin_VideoOGL/Src/ProgramShaderCache.cpp create mode 100644 Source/Plugins/Plugin_VideoOGL/Src/ProgramShaderCache.h diff --git a/Source/Core/VideoCommon/Src/PixelShaderGen.cpp b/Source/Core/VideoCommon/Src/PixelShaderGen.cpp index 1726855d54..4cdf562182 100644 --- a/Source/Core/VideoCommon/Src/PixelShaderGen.cpp +++ b/Source/Core/VideoCommon/Src/PixelShaderGen.cpp @@ -500,6 +500,15 @@ static void BuildSwapModeTable() } } +const char* WriteRegister(API_TYPE ApiType, const char *prefix, const u32 num) +{ + if(ApiType == API_GLSL) + return ""; // Nothing to do here + static char result[64]; + sprintf(result, " : register(%s%d)", prefix, num); + return result; +} + const char *GeneratePixelShaderCode(DSTALPHA_MODE dstAlphaMode, API_TYPE ApiType, u32 components) { setlocale(LC_NUMERIC, "C"); // Reset locale for compilation @@ -524,6 +533,21 @@ const char *GeneratePixelShaderCode(DSTALPHA_MODE dstAlphaMode, API_TYPE ApiType } } DepthTextureEnable = (bpmem.ztex2.op != ZTEXTURE_DISABLE && !bpmem.zcontrol.zcomploc && bpmem.zmode.testenable && bpmem.zmode.updateenable) || g_ActiveConfig.bEnablePerPixelDepth ; + + if(ApiType == API_GLSL) + { + // A few required defines and ones that will make our lives a lot easier + WRITE(p, "#version 120\n"); + // Silly differences + WRITE(p, "#define float2 vec2\n"); + WRITE(p, "#define float3 vec3\n"); + WRITE(p, "#define float4 vec4\n"); + + // cg to glsl function translation + WRITE(p, "#define frac(x) fract(x)\n"); + WRITE(p, "#define saturate(x) clamp(x, 0.0f, 1.0f)\n"); + WRITE(p, "#define lerp(x, y, z) mix(x, y, z)\n"); + } // Declare samplers if(ApiType != API_D3D11) @@ -538,7 +562,7 @@ const char *GeneratePixelShaderCode(DSTALPHA_MODE dstAlphaMode, API_TYPE ApiType bool bfirst = true; for (int i = 0; i < 8; ++i) { - WRITE(p, "%s samp%d : register(s%d)", bfirst?"":",", i, i); + WRITE(p, "%s samp%d %s", bfirst?"":",", i, WriteRegister(ApiType, "s", i)); bfirst = false; } WRITE(p, ";\n"); @@ -555,67 +579,107 @@ const char *GeneratePixelShaderCode(DSTALPHA_MODE dstAlphaMode, API_TYPE ApiType } WRITE(p, "\n"); - - WRITE(p, "uniform float4 "I_COLORS"[4] : register(c%d);\n", C_COLORS); - WRITE(p, "uniform float4 "I_KCOLORS"[4] : register(c%d);\n", C_KCOLORS); - WRITE(p, "uniform float4 "I_ALPHA"[1] : register(c%d);\n", C_ALPHA); - WRITE(p, "uniform float4 "I_TEXDIMS"[8] : register(c%d);\n", C_TEXDIMS); - WRITE(p, "uniform float4 "I_ZBIAS"[2] : register(c%d);\n", C_ZBIAS); - WRITE(p, "uniform float4 "I_INDTEXSCALE"[2] : register(c%d);\n", C_INDTEXSCALE); - WRITE(p, "uniform float4 "I_INDTEXMTX"[6] : register(c%d);\n", C_INDTEXMTX); - WRITE(p, "uniform float4 "I_FOG"[3] : register(c%d);\n", C_FOG); + + WRITE(p, "uniform float4 "I_COLORS"[4] %s;\n", WriteRegister(ApiType, "c", C_COLORS)); + WRITE(p, "uniform float4 "I_KCOLORS"[4] %s;\n", WriteRegister(ApiType, "c", C_KCOLORS)); + WRITE(p, "uniform float4 "I_ALPHA"[1] %s;\n", WriteRegister(ApiType, "c", C_ALPHA)); + WRITE(p, "uniform float4 "I_TEXDIMS"[8] %s;\n", WriteRegister(ApiType, "c", C_TEXDIMS)); + WRITE(p, "uniform float4 "I_ZBIAS"[2] %s;\n", WriteRegister(ApiType, "c", C_ZBIAS)); + WRITE(p, "uniform float4 "I_INDTEXSCALE"[2] %s;\n", WriteRegister(ApiType, "c", C_INDTEXSCALE)); + WRITE(p, "uniform float4 "I_INDTEXMTX"[6] %s;\n", WriteRegister(ApiType, "c", C_INDTEXMTX)); + WRITE(p, "uniform float4 "I_FOG"[3] %s;\n", WriteRegister(ApiType, "c", C_FOG)); if(g_ActiveConfig.bEnablePixelLighting && g_ActiveConfig.backend_info.bSupportsPixelLighting) { - WRITE(p,"typedef struct { float4 col; float4 cosatt; float4 distatt; float4 pos; float4 dir; } Light;\n"); - WRITE(p,"typedef struct { Light lights[8]; } s_"I_PLIGHTS";\n"); - WRITE(p, "uniform s_"I_PLIGHTS" "I_PLIGHTS" : register(c%d);\n", C_PLIGHTS); - WRITE(p, "typedef struct { float4 C0, C1, C2, C3; } s_"I_PMATERIALS";\n"); - WRITE(p, "uniform s_"I_PMATERIALS" "I_PMATERIALS" : register(c%d);\n", C_PMATERIALS); + WRITE(p, "uniform float4 "I_PLIGHTS"[40] %s;\n", WriteRegister(ApiType, "c", C_PLIGHTS)); + WRITE(p, "uniform float4 "I_PMATERIALS"[4] %s;\n", WriteRegister(ApiType, "c", C_PMATERIALS)); } - WRITE(p, "void main(\n"); - if(ApiType != API_D3D11) - { - WRITE(p, " out float4 ocol0 : COLOR0,%s%s\n in float4 rawpos : %s,\n", - dstAlphaMode == DSTALPHA_DUAL_SOURCE_BLEND ? "\n out float4 ocol1 : COLOR1," : "", - DepthTextureEnable ? "\n out float depth : DEPTH," : "", - ApiType & API_OPENGL ? "WPOS" : ApiType & API_D3D9_SM20 ? "POSITION" : "VPOS"); - } - else - { - WRITE(p, " out float4 ocol0 : SV_Target0,%s%s\n in float4 rawpos : SV_Position,\n", - dstAlphaMode == DSTALPHA_DUAL_SOURCE_BLEND ? "\n out float4 ocol1 : SV_Target1," : "", - DepthTextureEnable ? "\n out float depth : SV_Depth," : ""); - } - - WRITE(p, " in float4 colors_0 : COLOR0,\n"); - WRITE(p, " in float4 colors_1 : COLOR1"); - - // compute window position if needed because binding semantic WPOS is not widely supported - if (numTexgen < 7) - { - for (int i = 0; i < numTexgen; ++i) - WRITE(p, ",\n in float3 uv%d : TEXCOORD%d", i, i); - WRITE(p, ",\n in float4 clipPos : TEXCOORD%d", numTexgen); - if(g_ActiveConfig.bEnablePixelLighting && g_ActiveConfig.backend_info.bSupportsPixelLighting) - WRITE(p, ",\n in float4 Normal : TEXCOORD%d", numTexgen + 1); - } - else - { - // wpos is in w of first 4 texcoords - if(g_ActiveConfig.bEnablePixelLighting && g_ActiveConfig.backend_info.bSupportsPixelLighting) + if(ApiType != API_GLSL) + { + WRITE(p, "void main(\n"); + if(ApiType != API_D3D11) { - for (int i = 0; i < 8; ++i) - WRITE(p, ",\n in float4 uv%d : TEXCOORD%d", i, i); + WRITE(p, " out float4 ocol0 : COLOR0,%s%s\n in float4 rawpos : %s,\n", + dstAlphaMode == DSTALPHA_DUAL_SOURCE_BLEND ? "\n out float4 ocol1 : COLOR1," : "", + DepthTextureEnable ? "\n out float depth : DEPTH," : "", + ApiType & API_OPENGL ? "WPOS" : ApiType & API_D3D9_SM20 ? "POSITION" : "VPOS"); } else { - for (unsigned int i = 0; i < xfregs.numTexGen.numTexGens; ++i) - WRITE(p, ",\n in float%d uv%d : TEXCOORD%d", i < 4 ? 4 : 3 , i, i); + WRITE(p, " out float4 ocol0 : SV_Target0,%s%s\n in float4 rawpos : SV_Position,\n", + dstAlphaMode == DSTALPHA_DUAL_SOURCE_BLEND ? "\n out float4 ocol1 : SV_Target1," : "", + DepthTextureEnable ? "\n out float depth : SV_Depth," : ""); } + + WRITE(p, " in float4 colors_0 : COLOR0,\n"); + WRITE(p, " in float4 colors_1 : COLOR1"); + + // compute window position if needed because binding semantic WPOS is not widely supported + if (numTexgen < 7) + { + for (int i = 0; i < numTexgen; ++i) + WRITE(p, ",\n in float3 uv%d : TEXCOORD%d", i, i); + WRITE(p, ",\n in float4 clipPos : TEXCOORD%d", numTexgen); + if(g_ActiveConfig.bEnablePixelLighting && g_ActiveConfig.backend_info.bSupportsPixelLighting) + WRITE(p, ",\n in float4 Normal : TEXCOORD%d", numTexgen + 1); + } + else + { + // wpos is in w of first 4 texcoords + if(g_ActiveConfig.bEnablePixelLighting && g_ActiveConfig.backend_info.bSupportsPixelLighting) + { + for (int i = 0; i < 8; ++i) + WRITE(p, ",\n in float4 uv%d : TEXCOORD%d", i, i); + } + else + { + for (unsigned int i = 0; i < xfregs.numTexGen.numTexGens; ++i) + WRITE(p, ",\n in float%d uv%d : TEXCOORD%d", i < 4 ? 4 : 3 , i, i); + } + } + WRITE(p, " ) {\n"); + } + else + { + // GLSL doesn't do main arguments + // Once we switch to GLSL 1.3 we will bind a lot of these. + + WRITE(p, " float4 ocol0;\n"); + if(dstAlphaMode == DSTALPHA_DUAL_SOURCE_BLEND) + WRITE(p, " float4 ocol1;\n"); // Will be supported later + if(DepthTextureEnable) + WRITE(p, " float depth;\n"); // TODO: Passed to Vertex Shader right? + WRITE(p, " float4 rawpos = gl_FragCoord;\n"); + + WRITE(p, " float4 colors_0 = gl_Color;\n"); + WRITE(p, " float4 colors_1 = gl_SecondaryColor;\n"); + + // compute window position if needed because binding semantic WPOS is not widely supported + if (numTexgen < 7) + { + for (int i = 0; i < numTexgen; ++i) + WRITE(p, " float3 uv%d = gl_TexCoord[%d].xyz;\n", i, i); + WRITE(p, " float4 clipPos = gl_TexCoord[%d];\n", numTexgen); + if(g_ActiveConfig.bEnablePixelLighting && g_ActiveConfig.backend_info.bSupportsPixelLighting) + WRITE(p, " float4 Normal = gl_TexCoord[%d];\n", numTexgen + 1); + } + else + { + // wpos is in w of first 4 texcoords + if(g_ActiveConfig.bEnablePixelLighting && g_ActiveConfig.backend_info.bSupportsPixelLighting) + { + for (int i = 0; i < 8; ++i) + WRITE(p, " float4 uv%d = gl_TexCoord[%d];\n", i, i); + } + else + { + for (unsigned int i = 0; i < xfregs.numTexGen.numTexGens; ++i) + WRITE(p, " float%d uv%d = gl_TexCoord[%d]%s;\n", i < 4 ? 4 : 3 , i, i, i < 4 ? ".xyz" : ""); + } + } + WRITE(p, "void main()\n{\n"); } - WRITE(p, " ) {\n"); char* pmainstart = p; int Pretest = AlphaPreTest(); @@ -624,8 +688,10 @@ const char *GeneratePixelShaderCode(DSTALPHA_MODE dstAlphaMode, API_TYPE ApiType if (!Pretest) { // alpha test will always fail, so restart the shader and just make it an empty function - WRITE(p, "ocol0 = 0;\n"); + WRITE(p, "ocol0 = float4(0);\n"); WRITE(p, "discard;\n"); + if(ApiType == API_GLSL) + WRITE(p, "gl_FragData[0] = ocol0;\n"); if(ApiType != API_D3D11) WRITE(p, "return;\n"); } @@ -733,6 +799,15 @@ const char *GeneratePixelShaderCode(DSTALPHA_MODE dstAlphaMode, API_TYPE ApiType WRITE(p, "depth = 1.f;\n"); if(dstAlphaMode == DSTALPHA_DUAL_SOURCE_BLEND) WRITE(p, "ocol1 = 0;\n"); + if(ApiType == API_GLSL) + { + // Once we switch to GLSL 1.3 and bind variables, we won't need to do this + WRITE(p, "gl_FragData[0] = ocol0;\n"); + if(DepthTextureEnable) + WRITE(p, "gl_FragDepth = depth;\n"); + if(dstAlphaMode == DSTALPHA_DUAL_SOURCE_BLEND) + ; // TODO: Will do this later + } WRITE(p, "discard;\n"); if(ApiType != API_D3D11) WRITE(p, "return;\n"); @@ -781,6 +856,13 @@ const char *GeneratePixelShaderCode(DSTALPHA_MODE dstAlphaMode, API_TYPE ApiType // ...and the alpha from ocol0 will be written to the framebuffer. WRITE(p, " ocol0.a = "I_ALPHA"[0].a;\n"); } + + if(ApiType == API_GLSL) + { + if(DepthTextureEnable) + WRITE(p, "gl_FragDepth = depth;\n"); + WRITE(p, "gl_FragData[0] = ocol0;\n"); + } } WRITE(p, "}\n"); if (text[sizeof(text) - 1] != 0x7C) @@ -1242,4 +1324,4 @@ static void WriteFog(char *&p) WRITE(p, " prev.rgb = lerp(prev.rgb,"I_FOG"[0].rgb,fog);\n"); -} \ No newline at end of file +} diff --git a/Source/Core/VideoCommon/Src/PixelShaderGen.h b/Source/Core/VideoCommon/Src/PixelShaderGen.h index 31242a916e..589fc927bc 100644 --- a/Source/Core/VideoCommon/Src/PixelShaderGen.h +++ b/Source/Core/VideoCommon/Src/PixelShaderGen.h @@ -47,6 +47,19 @@ #define PIXELSHADERUID_MAX_VALUES 70 #define PIXELSHADERUID_MAX_VALUES_SAFE 120 +// Annoying sure, can be removed once we get up to GLSL ~1.3 +const s_svar PSVar_Loc[] = { {I_COLORS, C_COLORS, 4 }, + {I_KCOLORS, C_KCOLORS, 4 }, + {I_ALPHA, C_ALPHA, 1 }, + {I_TEXDIMS, C_TEXDIMS, 8 }, + {I_ZBIAS , C_ZBIAS, 2 }, + {I_INDTEXSCALE , C_INDTEXSCALE, 2 }, + {I_INDTEXMTX, C_INDTEXMTX, 6 }, + {I_FOG, C_FOG, 3 }, + {I_PLIGHTS, C_PLIGHTS, 40 }, + {I_PMATERIALS, C_PMATERIALS, 4 }, + }; + // DO NOT make anything in this class virtual. template class _PIXELSHADERUID diff --git a/Source/Core/VideoCommon/Src/VertexShaderGen.cpp b/Source/Core/VideoCommon/Src/VertexShaderGen.cpp index 5076f9f044..fcdc0e8976 100644 --- a/Source/Core/VideoCommon/Src/VertexShaderGen.cpp +++ b/Source/Core/VideoCommon/Src/VertexShaderGen.cpp @@ -130,7 +130,7 @@ static char text[16384]; #define WRITE p+=sprintf -char* GenerateVSOutputStruct(char* p, u32 components, API_TYPE api_type) +char* GenerateVSOutputStruct(char* p, u32 components, API_TYPE ApiType) { WRITE(p, "struct VS_OUTPUT {\n"); WRITE(p, " float4 pos : POSITION;\n"); @@ -161,7 +161,10 @@ char* GenerateVSOutputStruct(char* p, u32 components, API_TYPE api_type) return p; } -const char *GenerateVertexShaderCode(u32 components, API_TYPE api_type) +extern const char* WriteRegister(API_TYPE ApiType, const char *prefix, const u32 num); + + +const char *GenerateVertexShaderCode(u32 components, API_TYPE ApiType) { setlocale(LC_NUMERIC, "C"); // Reset locale for compilation text[sizeof(text) - 1] = 0x7C; // canary @@ -169,7 +172,7 @@ const char *GenerateVertexShaderCode(u32 components, API_TYPE api_type) _assert_(bpmem.genMode.numtexgens == xfregs.numTexGen.numTexGens); _assert_(bpmem.genMode.numcolchans == xfregs.numChan.numColorChans); - bool is_d3d = (api_type & API_D3D9 || api_type == API_D3D11); + bool is_d3d = (ApiType & API_D3D9 || ApiType == API_D3D11); u32 lightMask = 0; if (xfregs.numChan.numColorChans > 0) lightMask |= xfregs.color[0].GetFullLightMask() | xfregs.alpha[0].GetFullLightMask(); @@ -178,78 +181,109 @@ const char *GenerateVertexShaderCode(u32 components, API_TYPE api_type) char *p = text; WRITE(p, "//Vertex Shader: comp:%x, \n", components); - WRITE(p, "typedef struct { float4 T0, T1, T2; float4 N0, N1, N2; } s_"I_POSNORMALMATRIX";\n" - "typedef struct { float4 t; } FLT4;\n" - "typedef struct { FLT4 T[24]; } s_"I_TEXMATRICES";\n" - "typedef struct { FLT4 T[64]; } s_"I_TRANSFORMMATRICES";\n" - "typedef struct { FLT4 T[32]; } s_"I_NORMALMATRICES";\n" - "typedef struct { FLT4 T[64]; } s_"I_POSTTRANSFORMMATRICES";\n" - "typedef struct { float4 col; float4 cosatt; float4 distatt; float4 pos; float4 dir; } Light;\n" - "typedef struct { Light lights[8]; } s_"I_LIGHTS";\n" - "typedef struct { float4 C0, C1, C2, C3; } s_"I_MATERIALS";\n" - "typedef struct { float4 T0, T1, T2, T3; } s_"I_PROJECTION";\n" - ); - - p = GenerateVSOutputStruct(p, components, api_type); - - // uniforms - - WRITE(p, "uniform s_"I_TRANSFORMMATRICES" "I_TRANSFORMMATRICES" : register(c%d);\n", C_TRANSFORMMATRICES); - WRITE(p, "uniform s_"I_TEXMATRICES" "I_TEXMATRICES" : register(c%d);\n", C_TEXMATRICES); // also using tex matrices - WRITE(p, "uniform s_"I_NORMALMATRICES" "I_NORMALMATRICES" : register(c%d);\n", C_NORMALMATRICES); - WRITE(p, "uniform s_"I_POSNORMALMATRIX" "I_POSNORMALMATRIX" : register(c%d);\n", C_POSNORMALMATRIX); - WRITE(p, "uniform s_"I_POSTTRANSFORMMATRICES" "I_POSTTRANSFORMMATRICES" : register(c%d);\n", C_POSTTRANSFORMMATRICES); - WRITE(p, "uniform s_"I_LIGHTS" "I_LIGHTS" : register(c%d);\n", C_LIGHTS); - WRITE(p, "uniform s_"I_MATERIALS" "I_MATERIALS" : register(c%d);\n", C_MATERIALS); - WRITE(p, "uniform s_"I_PROJECTION" "I_PROJECTION" : register(c%d);\n", C_PROJECTION); - WRITE(p, "uniform float4 "I_DEPTHPARAMS" : register(c%d);\n", C_DEPTHPARAMS); - - WRITE(p, "VS_OUTPUT main(\n"); - // inputs - if (components & VB_HAS_NRM0) - WRITE(p, " float3 rawnorm0 : NORMAL0,\n"); - if (components & VB_HAS_NRM1) { - if (is_d3d) - WRITE(p, " float3 rawnorm1 : NORMAL1,\n"); - else - WRITE(p, " float3 rawnorm1 : ATTR%d,\n", SHADER_NORM1_ATTRIB); + if(ApiType == API_GLSL) + { + // A few required defines and ones that will make our lives a lot easier + WRITE(p, "#version 120\n"); + // Silly differences + WRITE(p, "#define float2 vec2\n"); + WRITE(p, "#define float3 vec3\n"); + WRITE(p, "#define float4 vec4\n"); + + // cg to glsl function translation + WRITE(p, "#define frac(x) fract(x)\n"); + WRITE(p, "#define saturate(x) clamp(x, 0.0f, 1.0f)\n"); + WRITE(p, "#define lerp(x, y, z) mix(x, y, z)\n"); } - if (components & VB_HAS_NRM2) { - if (is_d3d) - WRITE(p, " float3 rawnorm2 : NORMAL2,\n"); - else - WRITE(p, " float3 rawnorm2 : ATTR%d,\n", SHADER_NORM2_ATTRIB); - } - if (components & VB_HAS_COL0) - WRITE(p, " float4 color0 : COLOR0,\n"); - if (components & VB_HAS_COL1) - WRITE(p, " float4 color1 : COLOR1,\n"); - for (int i = 0; i < 8; ++i) { - u32 hastexmtx = (components & (VB_HAS_TEXMTXIDX0< class _VERTEXSHADERUID { diff --git a/Source/Core/VideoCommon/Src/VideoCommon.h b/Source/Core/VideoCommon/Src/VideoCommon.h index 9f6dd59cd3..f87cb7e9b1 100644 --- a/Source/Core/VideoCommon/Src/VideoCommon.h +++ b/Source/Core/VideoCommon/Src/VideoCommon.h @@ -152,5 +152,11 @@ inline unsigned int GetPow2(unsigned int val) ++ret; return ret; } +struct s_svar +{ + const char *name; + const unsigned int reg; + const unsigned int size; +}; #endif // _VIDEOCOMMON_H diff --git a/Source/Core/VideoCommon/Src/VideoConfig.cpp b/Source/Core/VideoCommon/Src/VideoConfig.cpp index b6ba76f45e..57685dec09 100644 --- a/Source/Core/VideoCommon/Src/VideoConfig.cpp +++ b/Source/Core/VideoCommon/Src/VideoConfig.cpp @@ -151,6 +151,7 @@ void VideoConfig::GameIniLoad(const char *ini_file) iniFile.GetIfExists("Video_Settings", "DisableFog", &bDisableFog); iniFile.GetIfExists("Video_Settings", "EnableOpenCL", &bEnableOpenCL); iniFile.GetIfExists("Video_Settings", "OMPDecoder", &bOMPDecoder); + iniFile.GetIfExists("Video_Settings", "UseGLSL", &bUseGLSL); iniFile.GetIfExists("Video_Enhancements", "ForceFiltering", &bForceFiltering); iniFile.GetIfExists("Video_Enhancements", "MaxAnisotropy", &iMaxAnisotropy); // NOTE - this is x in (1 << x) @@ -183,6 +184,7 @@ void VideoConfig::VerifyValidity() if (!backend_info.bSupports3DVision) b3DVision = false; if (!backend_info.bSupportsFormatReinterpretation) bEFBEmulateFormatChanges = false; if (!backend_info.bSupportsPixelLighting) bEnablePixelLighting = false; + if (!backend_info.bSupportsGLSL) bUseGLSL = false; } void VideoConfig::Save(const char *ini_file) @@ -230,6 +232,7 @@ void VideoConfig::Save(const char *ini_file) iniFile.Set("Settings", "DisableTexturing", bDisableTexturing); iniFile.Set("Settings", "DstAlphaPass", bDstAlphaPass); iniFile.Set("Settings", "DisableFog", bDisableFog); + iniFile.Set("Settings", "bUseGLSL", bUseGLSL); iniFile.Set("Settings", "EnableOpenCL", bEnableOpenCL); iniFile.Set("Settings", "OMPDecoder", bOMPDecoder); @@ -296,6 +299,7 @@ void VideoConfig::GameIniSave(const char* default_ini, const char* game_ini) SET_IF_DIFFERS("Video_Settings", "DisableFog", bDisableFog); SET_IF_DIFFERS("Video_Settings", "EnableOpenCL", bEnableOpenCL); SET_IF_DIFFERS("Video_Settings", "OMPDecoder", bOMPDecoder); + SET_IF_DIFFERS("Video_Settings", "UseGLSL", bUseGLSL); SET_IF_DIFFERS("Video_Enhancements", "ForceFiltering", bForceFiltering); SET_IF_DIFFERS("Video_Enhancements", "MaxAnisotropy", iMaxAnisotropy); // NOTE - this is x in (1 << x) diff --git a/Source/Core/VideoCommon/Src/VideoConfig.h b/Source/Core/VideoCommon/Src/VideoConfig.h index 3bd833cc73..63df7e0ded 100644 --- a/Source/Core/VideoCommon/Src/VideoConfig.h +++ b/Source/Core/VideoCommon/Src/VideoConfig.h @@ -106,6 +106,7 @@ struct VideoConfig bool bDisableTexturing; bool bDstAlphaPass; bool bDisableFog; + bool bUseGLSL; // Utility bool bDumpTextures; @@ -166,6 +167,7 @@ struct VideoConfig bool bSupportsDualSourceBlend; // only supported by D3D11 and OpenGL bool bSupportsFormatReinterpretation; bool bSupportsPixelLighting; + bool bSupportsGLSL; } backend_info; }; diff --git a/Source/Plugins/Plugin_VideoDX11/Src/main.cpp b/Source/Plugins/Plugin_VideoDX11/Src/main.cpp index ba93890e2b..dc494682d8 100644 --- a/Source/Plugins/Plugin_VideoDX11/Src/main.cpp +++ b/Source/Plugins/Plugin_VideoDX11/Src/main.cpp @@ -94,6 +94,7 @@ void InitBackendInfo() g_Config.backend_info.bSupportsDualSourceBlend = true; g_Config.backend_info.bSupportsFormatReinterpretation = true; g_Config.backend_info.bSupportsPixelLighting = true; + g_Config.backend_info.bSupportsGLSL = false; IDXGIFactory* factory; IDXGIAdapter* ad; diff --git a/Source/Plugins/Plugin_VideoDX9/Src/main.cpp b/Source/Plugins/Plugin_VideoDX9/Src/main.cpp index 1d733f7dc3..1a98fb5c44 100644 --- a/Source/Plugins/Plugin_VideoDX9/Src/main.cpp +++ b/Source/Plugins/Plugin_VideoDX9/Src/main.cpp @@ -96,6 +96,7 @@ void InitBackendInfo() g_Config.backend_info.bSupports3DVision = true; g_Config.backend_info.bSupportsDualSourceBlend = false; g_Config.backend_info.bSupportsFormatReinterpretation = true; + g_Config.backend_info.bSupportsGLSL = false; g_Config.backend_info.bSupportsPixelLighting = C_PLIGHTS + 40 <= maxConstants && C_PMATERIALS + 4 <= maxConstants; diff --git a/Source/Plugins/Plugin_VideoOGL/CMakeLists.txt b/Source/Plugins/Plugin_VideoOGL/CMakeLists.txt index c57106645f..6a49269c6c 100644 --- a/Source/Plugins/Plugin_VideoOGL/CMakeLists.txt +++ b/Source/Plugins/Plugin_VideoOGL/CMakeLists.txt @@ -4,6 +4,7 @@ set(SRCS Src/FramebufferManager.cpp Src/NativeVertexFormat.cpp Src/PixelShaderCache.cpp Src/PostProcessing.cpp + Src/ProgramShaderCache.cpp Src/RasterFont.cpp Src/Render.cpp Src/TextureCache.cpp diff --git a/Source/Plugins/Plugin_VideoOGL/Src/NativeVertexFormat.cpp b/Source/Plugins/Plugin_VideoOGL/Src/NativeVertexFormat.cpp index 9fa3281b19..c83c976fb2 100644 --- a/Source/Plugins/Plugin_VideoOGL/Src/NativeVertexFormat.cpp +++ b/Source/Plugins/Plugin_VideoOGL/Src/NativeVertexFormat.cpp @@ -19,6 +19,7 @@ #include "x64Emitter.h" #include "ABI.h" #include "MemoryUtil.h" +#include "ProgramShaderCache.h" #include "VertexShaderGen.h" #include "CPMemory.h" @@ -191,8 +192,15 @@ void GLVertexFormat::SetupVertexPointers() { if (vtx_decl.num_normals >= 1) { glNormalPointer(VarToGL(vtx_decl.normal_gl_type), vtx_decl.stride, (void *)(VertexManager::s_pBaseBufferPointer + vtx_decl.normal_offset[0])); if (vtx_decl.num_normals == 3) { - glVertexAttribPointer(SHADER_NORM1_ATTRIB, vtx_decl.normal_gl_size, VarToGL(vtx_decl.normal_gl_type), GL_TRUE, vtx_decl.stride, (void *)(VertexManager::s_pBaseBufferPointer + vtx_decl.normal_offset[1])); - glVertexAttribPointer(SHADER_NORM2_ATTRIB, vtx_decl.normal_gl_size, VarToGL(vtx_decl.normal_gl_type), GL_TRUE, vtx_decl.stride, (void *)(VertexManager::s_pBaseBufferPointer + vtx_decl.normal_offset[2])); + GLint varLocation1 = SHADER_NORM1_ATTRIB, varLocation2 = SHADER_NORM2_ATTRIB; + if(g_ActiveConfig.bUseGLSL){ + varLocation1 = OGL::ProgramShaderCache::GetAttr(0); + varLocation2 = OGL::ProgramShaderCache::GetAttr(1); + } + if(varLocation1 > 0) + glVertexAttribPointer(varLocation1, vtx_decl.normal_gl_size, VarToGL(vtx_decl.normal_gl_type), GL_TRUE, vtx_decl.stride, (void *)(VertexManager::s_pBaseBufferPointer + vtx_decl.normal_offset[1])); + if(varLocation2 > 0) + glVertexAttribPointer(varLocation2, vtx_decl.normal_gl_size, VarToGL(vtx_decl.normal_gl_type), GL_TRUE, vtx_decl.stride, (void *)(VertexManager::s_pBaseBufferPointer + vtx_decl.normal_offset[2])); } } @@ -216,7 +224,11 @@ void GLVertexFormat::SetupVertexPointers() { } if (vtx_decl.posmtx_offset != -1) { - glVertexAttribPointer(SHADER_POSMTX_ATTRIB, 4, GL_UNSIGNED_BYTE, GL_FALSE, vtx_decl.stride, (void *)(VertexManager::s_pBaseBufferPointer + vtx_decl.posmtx_offset)); + GLint varLocation = SHADER_POSMTX_ATTRIB; + if(g_ActiveConfig.bUseGLSL) + varLocation = OGL::ProgramShaderCache::GetAttr(2); + if(varLocation > 0) + glVertexAttribPointer(varLocation, 4, GL_UNSIGNED_BYTE, GL_FALSE, vtx_decl.stride, (void *)(VertexManager::s_pBaseBufferPointer + vtx_decl.posmtx_offset)); } #endif } diff --git a/Source/Plugins/Plugin_VideoOGL/Src/PixelShaderCache.cpp b/Source/Plugins/Plugin_VideoOGL/Src/PixelShaderCache.cpp index 050b378d2f..6c6725bb13 100644 --- a/Source/Plugins/Plugin_VideoOGL/Src/PixelShaderCache.cpp +++ b/Source/Plugins/Plugin_VideoOGL/Src/PixelShaderCache.cpp @@ -27,6 +27,7 @@ #include "Common.h" #include "Render.h" #include "VertexShaderGen.h" +#include "ProgramShaderCache.h" #include "PixelShaderCache.h" #include "PixelShaderManager.h" #include "FileUtil.h" @@ -47,326 +48,427 @@ bool PixelShaderCache::ShaderEnabled; PixelShaderCache::PSCacheEntry* PixelShaderCache::last_entry = NULL; PIXELSHADERUID PixelShaderCache::last_uid; +void(*pSetPSConstant4f)(unsigned int, float, float, float, float); +void(*pSetPSConstant4fv)(unsigned int, const float*); +void(*pSetMultiPSConstant4fv)(unsigned int, unsigned int, const float*); + GLuint PixelShaderCache::GetDepthMatrixProgram() { - return s_DepthMatrixProgram; + return s_DepthMatrixProgram; } GLuint PixelShaderCache::GetColorMatrixProgram() { - return s_ColorMatrixProgram; + return s_ColorMatrixProgram; } void PixelShaderCache::Init() { - glEnable(GL_FRAGMENT_PROGRAM_ARB); - ShaderEnabled = true; - CurrentShader = 0; - last_entry = NULL; - GL_REPORT_ERRORD(); + glEnable(GL_FRAGMENT_PROGRAM_ARB); + ShaderEnabled = true; + CurrentShader = 0; + last_entry = NULL; + GL_REPORT_ERRORD(); - s_displayCompileAlert = true; + s_displayCompileAlert = true; - glGetProgramivARB(GL_FRAGMENT_PROGRAM_ARB, GL_MAX_PROGRAM_NATIVE_ALU_INSTRUCTIONS_ARB, (GLint *)&s_nMaxPixelInstructions); - - if(s_nMaxPixelInstructions == 0) // Some combination of drivers and hardware returns zero for some reason. - s_nMaxPixelInstructions = 4096; - if (strstr((const char*)glGetString(GL_VENDOR), "Humper") != NULL) s_nMaxPixelInstructions = 4096; + if(g_ActiveConfig.bUseGLSL) + { + pSetPSConstant4f = SetGLSLPSConstant4f; + pSetPSConstant4fv = SetGLSLPSConstant4fv; + pSetMultiPSConstant4fv = SetMultiGLSLPSConstant4fv; + } + else + { + pSetPSConstant4f = SetCGPSConstant4f; + pSetPSConstant4fv = SetCGPSConstant4fv; + pSetMultiPSConstant4fv = SetMultiCGPSConstant4fv; + } + + glGetProgramivARB(GL_FRAGMENT_PROGRAM_ARB, GL_MAX_PROGRAM_NATIVE_ALU_INSTRUCTIONS_ARB, (GLint *)&s_nMaxPixelInstructions); + + if(s_nMaxPixelInstructions == 0) // Some combination of drivers and hardware returns zero for some reason. + s_nMaxPixelInstructions = 4096; + if (strstr((const char*)glGetString(GL_VENDOR), "Humper") != NULL) s_nMaxPixelInstructions = 4096; #if CG_VERSION_NUM == 2100 - if (strstr((const char*)glGetString(GL_VENDOR), "ATI") != NULL) - { - s_nMaxPixelInstructions = 4096; - } + if (strstr((const char*)glGetString(GL_VENDOR), "ATI") != NULL) + { + s_nMaxPixelInstructions = 4096; + } #endif - int maxinst, maxattribs; - glGetProgramivARB(GL_FRAGMENT_PROGRAM_ARB, GL_MAX_PROGRAM_NATIVE_INSTRUCTIONS_ARB, (GLint *)&maxinst); - glGetProgramivARB(GL_FRAGMENT_PROGRAM_ARB, GL_MAX_PROGRAM_NATIVE_ATTRIBS_ARB, (GLint *)&maxattribs); - INFO_LOG(VIDEO, "pixel max_alu=%d, max_inst=%d, max_attrib=%d", s_nMaxPixelInstructions, maxinst, maxattribs); - - char pmatrixprog[2048]; - sprintf(pmatrixprog, "!!ARBfp1.0" - "TEMP R0;\n" - "TEMP R1;\n" - "PARAM K0 = { 0.5, 0.5, 0.5, 0.5};\n" - "TEX R0, fragment.texcoord[0], texture[0], RECT;\n" - "MUL R0, R0, program.env[%d];\n" - "ADD R0, R0, K0;\n" - "FLR R0, R0;\n" - "MUL R0, R0, program.env[%d];\n" - "DP4 R1.x, R0, program.env[%d];\n" - "DP4 R1.y, R0, program.env[%d];\n" - "DP4 R1.z, R0, program.env[%d];\n" - "DP4 R1.w, R0, program.env[%d];\n" - "ADD result.color, R1, program.env[%d];\n" - "END\n",C_COLORMATRIX+5,C_COLORMATRIX+6, C_COLORMATRIX, C_COLORMATRIX+1, C_COLORMATRIX+2, C_COLORMATRIX+3, C_COLORMATRIX+4); - glGenProgramsARB(1, &s_ColorMatrixProgram); - SetCurrentShader(s_ColorMatrixProgram); - glProgramStringARB(GL_FRAGMENT_PROGRAM_ARB, GL_PROGRAM_FORMAT_ASCII_ARB, (GLsizei)strlen(pmatrixprog), pmatrixprog); + int maxinst, maxattribs; + glGetProgramivARB(GL_FRAGMENT_PROGRAM_ARB, GL_MAX_PROGRAM_NATIVE_INSTRUCTIONS_ARB, (GLint *)&maxinst); + glGetProgramivARB(GL_FRAGMENT_PROGRAM_ARB, GL_MAX_PROGRAM_NATIVE_ATTRIBS_ARB, (GLint *)&maxattribs); + INFO_LOG(VIDEO, "pixel max_alu=%d, max_inst=%d, max_attrib=%d", s_nMaxPixelInstructions, maxinst, maxattribs); - GLenum err = GL_REPORT_ERROR(); - if (err != GL_NO_ERROR) { - ERROR_LOG(VIDEO, "Failed to create color matrix fragment program"); - glDeleteProgramsARB(1, &s_ColorMatrixProgram); - s_ColorMatrixProgram = 0; - } + char pmatrixprog[2048]; + sprintf(pmatrixprog, "!!ARBfp1.0" + "TEMP R0;\n" + "TEMP R1;\n" + "PARAM K0 = { 0.5, 0.5, 0.5, 0.5};\n" + "TEX R0, fragment.texcoord[0], texture[0], RECT;\n" + "MUL R0, R0, program.env[%d];\n" + "ADD R0, R0, K0;\n" + "FLR R0, R0;\n" + "MUL R0, R0, program.env[%d];\n" + "DP4 R1.x, R0, program.env[%d];\n" + "DP4 R1.y, R0, program.env[%d];\n" + "DP4 R1.z, R0, program.env[%d];\n" + "DP4 R1.w, R0, program.env[%d];\n" + "ADD result.color, R1, program.env[%d];\n" + "END\n",C_COLORMATRIX+5,C_COLORMATRIX+6, C_COLORMATRIX, C_COLORMATRIX+1, C_COLORMATRIX+2, C_COLORMATRIX+3, C_COLORMATRIX+4); + glGenProgramsARB(1, &s_ColorMatrixProgram); + SetCurrentShader(s_ColorMatrixProgram); + glProgramStringARB(GL_FRAGMENT_PROGRAM_ARB, GL_PROGRAM_FORMAT_ASCII_ARB, (GLsizei)strlen(pmatrixprog), pmatrixprog); - sprintf(pmatrixprog, "!!ARBfp1.0\n" - "TEMP R0;\n" - "TEMP R1;\n" - "TEMP R2;\n" - //16777215/16777216*256, 1/255, 256, 0 - "PARAM K0 = { 255.99998474121, 0.003921568627451, 256.0, 0.0};\n" - "PARAM K1 = { 15.0, 0.066666666666, 0.0, 0.0};\n" - //sample the depth value - "TEX R2, fragment.texcoord[0], texture[0], RECT;\n" + GLenum err = GL_REPORT_ERROR(); + if (err != GL_NO_ERROR) { + ERROR_LOG(VIDEO, "Failed to create color matrix fragment program"); + glDeleteProgramsARB(1, &s_ColorMatrixProgram); + s_ColorMatrixProgram = 0; + } - //scale from [0*16777216..1*16777216] to - //[0*16777215..1*16777215], multiply by 256 - "MUL R0, R2.x, K0.x;\n" // *16777215/16777216*256 + sprintf(pmatrixprog, "!!ARBfp1.0\n" + "TEMP R0;\n" + "TEMP R1;\n" + "TEMP R2;\n" + //16777215/16777216*256, 1/255, 256, 0 + "PARAM K0 = { 255.99998474121, 0.003921568627451, 256.0, 0.0};\n" + "PARAM K1 = { 15.0, 0.066666666666, 0.0, 0.0};\n" + //sample the depth value + "TEX R2, fragment.texcoord[0], texture[0], RECT;\n" - //It is easy to get bad results due to low precision - //here, for example converting like this: - //MUL R0,R0,{ 65536, 256, 1, 16777216 } - //FRC R0,R0 - //gives {?, 128/255, 254/255, ?} for depth value 254/255 - //on some gpus + //scale from [0*16777216..1*16777216] to + //[0*16777215..1*16777215], multiply by 256 + "MUL R0, R2.x, K0.x;\n" // *16777215/16777216*256 - "FLR R0.x,R0;\n" //bits 31..24 + //It is easy to get bad results due to low precision + //here, for example converting like this: + //MUL R0,R0,{ 65536, 256, 1, 16777216 } + //FRC R0,R0 + //gives {?, 128/255, 254/255, ?} for depth value 254/255 + //on some gpus - "SUB R0.yzw,R0,R0.x;\n" //subtract bits 31..24 from rest - "MUL R0.yzw,R0,K0.z;\n" // *256 - "FLR R0.y,R0;\n" //bits 23..16 + "FLR R0.x,R0;\n" //bits 31..24 - "SUB R0.zw,R0,R0.y;\n" //subtract bits 23..16 from rest - "MUL R0.zw,R0,K0.z;\n" // *256 - "FLR R0.z,R0;\n" //bits 15..8 + "SUB R0.yzw,R0,R0.x;\n" //subtract bits 31..24 from rest + "MUL R0.yzw,R0,K0.z;\n" // *256 + "FLR R0.y,R0;\n" //bits 23..16 - "MOV R0.w,R0.x;\n" //duplicate bit 31..24 - - "MUL R0,R0,K0.y;\n" // /255 + "SUB R0.zw,R0,R0.y;\n" //subtract bits 23..16 from rest + "MUL R0.zw,R0,K0.z;\n" // *256 + "FLR R0.z,R0;\n" //bits 15..8 - "MUL R0.w,R0,K1.x;\n" // *15 - "FLR R0.w,R0;\n" //bits 31..28 - "MUL R0.w,R0,K1.y;\n" // /15 + "MOV R0.w,R0.x;\n" //duplicate bit 31..24 - "DP4 R1.x, R0, program.env[%d];\n" - "DP4 R1.y, R0, program.env[%d];\n" - "DP4 R1.z, R0, program.env[%d];\n" - "DP4 R1.w, R0, program.env[%d];\n" - "ADD result.color, R1, program.env[%d];\n" - "END\n", C_COLORMATRIX, C_COLORMATRIX+1, C_COLORMATRIX+2, C_COLORMATRIX+3, C_COLORMATRIX+4); - glGenProgramsARB(1, &s_DepthMatrixProgram); - SetCurrentShader(s_DepthMatrixProgram); - glProgramStringARB(GL_FRAGMENT_PROGRAM_ARB, GL_PROGRAM_FORMAT_ASCII_ARB, (GLsizei)strlen(pmatrixprog), pmatrixprog); + "MUL R0,R0,K0.y;\n" // /255 + + "MUL R0.w,R0,K1.x;\n" // *15 + "FLR R0.w,R0;\n" //bits 31..28 + "MUL R0.w,R0,K1.y;\n" // /15 + + "DP4 R1.x, R0, program.env[%d];\n" + "DP4 R1.y, R0, program.env[%d];\n" + "DP4 R1.z, R0, program.env[%d];\n" + "DP4 R1.w, R0, program.env[%d];\n" + "ADD result.color, R1, program.env[%d];\n" + "END\n", C_COLORMATRIX, C_COLORMATRIX+1, C_COLORMATRIX+2, C_COLORMATRIX+3, C_COLORMATRIX+4); + glGenProgramsARB(1, &s_DepthMatrixProgram); + SetCurrentShader(s_DepthMatrixProgram); + glProgramStringARB(GL_FRAGMENT_PROGRAM_ARB, GL_PROGRAM_FORMAT_ASCII_ARB, (GLsizei)strlen(pmatrixprog), pmatrixprog); + + err = GL_REPORT_ERROR(); + if (err != GL_NO_ERROR) { + ERROR_LOG(VIDEO, "Failed to create depth matrix fragment program"); + glDeleteProgramsARB(1, &s_DepthMatrixProgram); + s_DepthMatrixProgram = 0; + } - err = GL_REPORT_ERROR(); - if (err != GL_NO_ERROR) { - ERROR_LOG(VIDEO, "Failed to create depth matrix fragment program"); - glDeleteProgramsARB(1, &s_DepthMatrixProgram); - s_DepthMatrixProgram = 0; - } - } void PixelShaderCache::Shutdown() { - glDeleteProgramsARB(1, &s_ColorMatrixProgram); - s_ColorMatrixProgram = 0; + glDeleteProgramsARB(1, &s_ColorMatrixProgram); + s_ColorMatrixProgram = 0; glDeleteProgramsARB(1, &s_DepthMatrixProgram); - s_DepthMatrixProgram = 0; - PSCache::iterator iter = PixelShaders.begin(); - for (; iter != PixelShaders.end(); iter++) - iter->second.Destroy(); - PixelShaders.clear(); + s_DepthMatrixProgram = 0; + PSCache::iterator iter = PixelShaders.begin(); + for (; iter != PixelShaders.end(); iter++) + iter->second.Destroy(); + PixelShaders.clear(); } FRAGMENTSHADER* PixelShaderCache::SetShader(DSTALPHA_MODE dstAlphaMode, u32 components) { - PIXELSHADERUID uid; - GetPixelShaderId(&uid, dstAlphaMode, components); - - // Check if the shader is already set - if (last_entry) - { - if (uid == last_uid) - { - GFX_DEBUGGER_PAUSE_AT(NEXT_PIXEL_SHADER_CHANGE, true); - ValidatePixelShaderIDs(API_OPENGL, last_entry->safe_uid, last_entry->shader.strprog, dstAlphaMode, components); - return &last_entry->shader; - } - } + PIXELSHADERUID uid; + GetPixelShaderId(&uid, dstAlphaMode, components); - last_uid = uid; + // Check if the shader is already set + if (last_entry) + { + if (uid == last_uid) + { + GFX_DEBUGGER_PAUSE_AT(NEXT_PIXEL_SHADER_CHANGE, true); + ValidatePixelShaderIDs(API_OPENGL, last_entry->safe_uid, last_entry->shader.strprog, dstAlphaMode, components); + return &last_entry->shader; + } + } - PSCache::iterator iter = PixelShaders.find(uid); - if (iter != PixelShaders.end()) - { - PSCacheEntry &entry = iter->second; - last_entry = &entry; + last_uid = uid; - GFX_DEBUGGER_PAUSE_AT(NEXT_PIXEL_SHADER_CHANGE, true); - ValidatePixelShaderIDs(API_OPENGL, entry.safe_uid, entry.shader.strprog, dstAlphaMode, components); - return &last_entry->shader; - } + PSCache::iterator iter = PixelShaders.find(uid); + if (iter != PixelShaders.end()) + { + PSCacheEntry &entry = iter->second; + last_entry = &entry; - // Make an entry in the table - PSCacheEntry& newentry = PixelShaders[uid]; - last_entry = &newentry; - const char *code = GeneratePixelShaderCode(dstAlphaMode, API_OPENGL, components); + GFX_DEBUGGER_PAUSE_AT(NEXT_PIXEL_SHADER_CHANGE, true); + ValidatePixelShaderIDs(API_OPENGL, entry.safe_uid, entry.shader.strprog, dstAlphaMode, components); + return &last_entry->shader; + } - if (g_ActiveConfig.bEnableShaderDebugging && code) - { - GetSafePixelShaderId(&newentry.safe_uid, dstAlphaMode, components); - newentry.shader.strprog = code; - } + // Make an entry in the table + PSCacheEntry& newentry = PixelShaders[uid]; + last_entry = &newentry; + const char *code = GeneratePixelShaderCode(dstAlphaMode, API_OPENGL, components); + + if (g_ActiveConfig.bEnableShaderDebugging && code) + { + GetSafePixelShaderId(&newentry.safe_uid, dstAlphaMode, components); + newentry.shader.strprog = code; + } #if defined(_DEBUG) || defined(DEBUGFAST) - if (g_ActiveConfig.iLog & CONF_SAVESHADERS && code) { - static int counter = 0; - char szTemp[MAX_PATH]; - sprintf(szTemp, "%sps_%04i.txt", File::GetUserPath(D_DUMP_IDX).c_str(), counter++); - - SaveData(szTemp, code); - } + if (g_ActiveConfig.iLog & CONF_SAVESHADERS && code) { + static int counter = 0; + char szTemp[MAX_PATH]; + sprintf(szTemp, "%sps_%04i.txt", File::GetUserPath(D_DUMP_IDX).c_str(), counter++); + + SaveData(szTemp, code); + } #endif - if (!code || !CompilePixelShader(newentry.shader, code)) { - GFX_DEBUGGER_PAUSE_AT(NEXT_ERROR, true); - return NULL; - } - - INCSTAT(stats.numPixelShadersCreated); - SETSTAT(stats.numPixelShadersAlive, PixelShaders.size()); - GFX_DEBUGGER_PAUSE_AT(NEXT_PIXEL_SHADER_CHANGE, true); - return &last_entry->shader; + if (!code || !CompilePixelShader(newentry.shader, code)) { + GFX_DEBUGGER_PAUSE_AT(NEXT_ERROR, true); + return NULL; + } + + INCSTAT(stats.numPixelShadersCreated); + SETSTAT(stats.numPixelShadersAlive, PixelShaders.size()); + GFX_DEBUGGER_PAUSE_AT(NEXT_PIXEL_SHADER_CHANGE, true); + return &last_entry->shader; } bool PixelShaderCache::CompilePixelShader(FRAGMENTSHADER& ps, const char* pstrprogram) { - GLenum err = GL_REPORT_ERROR(); - if (err != GL_NO_ERROR) - { - ERROR_LOG(VIDEO, "glError %08x before PS!", err); - } + GLenum err = GL_REPORT_ERROR(); + if (err != GL_NO_ERROR) + { + ERROR_LOG(VIDEO, "glError %08x before PS!", err); + } #if defined HAVE_CG && HAVE_CG - char stropt[128]; - sprintf(stropt, "MaxLocalParams=32,NumInstructionSlots=%d", s_nMaxPixelInstructions); - const char *opts[] = {"-profileopts", stropt, "-O2", "-q", NULL}; - CGprogram tempprog = cgCreateProgram(g_cgcontext, CG_SOURCE, pstrprogram, g_cgfProf, "main", opts); + char stropt[128]; + sprintf(stropt, "MaxLocalParams=32,NumInstructionSlots=%d", s_nMaxPixelInstructions); + const char *opts[] = {"-profileopts", stropt, "-O2", "-q", NULL}; + CGprogram tempprog = cgCreateProgram(g_cgcontext, CG_SOURCE, pstrprogram, g_cgfProf, "main", opts); - // handle errors - if (!cgIsProgram(tempprog)) - { - cgDestroyProgram(tempprog); + // handle errors + if (!cgIsProgram(tempprog)) + { + cgDestroyProgram(tempprog); - static int num_failures = 0; - char szTemp[MAX_PATH]; - sprintf(szTemp, "%sbad_ps_%04i.txt", File::GetUserPath(D_DUMP_IDX).c_str(), num_failures++); - std::ofstream file(szTemp); - file << pstrprogram; - file.close(); + static int num_failures = 0; + char szTemp[MAX_PATH]; + sprintf(szTemp, "%sbad_ps_%04i.txt", File::GetUserPath(D_DUMP_IDX).c_str(), num_failures++); + std::ofstream file(szTemp); + file << pstrprogram; + file.close(); - PanicAlert("Failed to compile pixel 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 (%d):\n%s", - szTemp, - g_cgfProf, - cgGetLastListing(g_cgcontext)); + PanicAlert("Failed to compile pixel 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 (%d):\n%s", + szTemp, + g_cgfProf, + cgGetLastListing(g_cgcontext)); - return false; - } + return false; + } - // handle warnings - if (cgGetError() != CG_NO_ERROR) - { - WARN_LOG(VIDEO, "Warnings on compile ps %s:", cgGetLastListing(g_cgcontext)); - WARN_LOG(VIDEO, "%s", pstrprogram); - } + // handle warnings + if (cgGetError() != CG_NO_ERROR) + { + WARN_LOG(VIDEO, "Warnings on compile ps %s:", cgGetLastListing(g_cgcontext)); + WARN_LOG(VIDEO, "%s", pstrprogram); + } - // This looks evil - we modify the program through the const char * we got from cgGetProgramString! - // It SHOULD not have any nasty side effects though - but you never know... - char *pcompiledprog = (char*)cgGetProgramString(tempprog, CG_COMPILED_PROGRAM); - char *plocal = strstr(pcompiledprog, "program.local"); - while (plocal != NULL) - { - const char *penv = " program.env"; - memcpy(plocal, penv, 13); - plocal = strstr(plocal+13, "program.local"); - } + // This looks evil - we modify the program through the const char * we got from cgGetProgramString! + // It SHOULD not have any nasty side effects though - but you never know... + char *pcompiledprog = (char*)cgGetProgramString(tempprog, CG_COMPILED_PROGRAM); + char *plocal = strstr(pcompiledprog, "program.local"); + while (plocal != NULL) + { + const char *penv = " program.env"; + memcpy(plocal, penv, 13); + plocal = strstr(plocal+13, "program.local"); + } - glGenProgramsARB(1, &ps.glprogid); - SetCurrentShader(ps.glprogid); - glProgramStringARB(GL_FRAGMENT_PROGRAM_ARB, GL_PROGRAM_FORMAT_ASCII_ARB, (GLsizei)strlen(pcompiledprog), pcompiledprog); + glGenProgramsARB(1, &ps.glprogid); + SetCurrentShader(ps.glprogid); + glProgramStringARB(GL_FRAGMENT_PROGRAM_ARB, GL_PROGRAM_FORMAT_ASCII_ARB, (GLsizei)strlen(pcompiledprog), pcompiledprog); - err = GL_REPORT_ERROR(); - if (err != GL_NO_ERROR) - { - GLint error_pos, native_limit; - glGetIntegerv(GL_PROGRAM_ERROR_POSITION_ARB, &error_pos); - glGetProgramivARB(GL_FRAGMENT_PROGRAM_ARB, GL_PROGRAM_UNDER_NATIVE_LIMITS_ARB, &native_limit); - // Error occur - if (error_pos != -1) { - const char *program_error = (const char *)glGetString(GL_PROGRAM_ERROR_STRING_ARB); - char line[256]; - strncpy(line, (const char *)pcompiledprog + error_pos, 255); - line[255] = 0; - ERROR_LOG(VIDEO, "Error at %i: %s", error_pos, program_error); - ERROR_LOG(VIDEO, "Line dump: \n%s", line); - } else if (native_limit != -1) { - ERROR_LOG(VIDEO, "Hit limit? %i", native_limit); - // TODO - } - ERROR_LOG(VIDEO, "%s", pstrprogram); - ERROR_LOG(VIDEO, "%s", pcompiledprog); - } + err = GL_REPORT_ERROR(); + if (err != GL_NO_ERROR) + { + GLint error_pos, native_limit; + glGetIntegerv(GL_PROGRAM_ERROR_POSITION_ARB, &error_pos); + glGetProgramivARB(GL_FRAGMENT_PROGRAM_ARB, GL_PROGRAM_UNDER_NATIVE_LIMITS_ARB, &native_limit); + // Error occur + if (error_pos != -1) { + const char *program_error = (const char *)glGetString(GL_PROGRAM_ERROR_STRING_ARB); + char line[256]; + strncpy(line, (const char *)pcompiledprog + error_pos, 255); + line[255] = 0; + ERROR_LOG(VIDEO, "Error at %i: %s", error_pos, program_error); + ERROR_LOG(VIDEO, "Line dump: \n%s", line); + } else if (native_limit != -1) { + ERROR_LOG(VIDEO, "Hit limit? %i", native_limit); + // TODO + } + ERROR_LOG(VIDEO, "%s", pstrprogram); + ERROR_LOG(VIDEO, "%s", pcompiledprog); + } - cgDestroyProgram(tempprog); + cgDestroyProgram(tempprog); #endif - return true; + return true; } //Disable Fragment programs and reset the selected Program void PixelShaderCache::DisableShader() { - if(ShaderEnabled) - { - glDisable(GL_FRAGMENT_PROGRAM_ARB); - ShaderEnabled = false; - } + if(ShaderEnabled) + { + glDisable(GL_FRAGMENT_PROGRAM_ARB); + ShaderEnabled = false; + } } //bind a program if is diferent from the binded oone void PixelShaderCache::SetCurrentShader(GLuint Shader) { - if(!ShaderEnabled) - { - glEnable(GL_FRAGMENT_PROGRAM_ARB); - ShaderEnabled = true; - } - if(CurrentShader != Shader) - { - if(Shader != 0) - CurrentShader = Shader; - glBindProgramARB(GL_FRAGMENT_PROGRAM_ARB, CurrentShader); - } + if(!ShaderEnabled) + { + glEnable(GL_FRAGMENT_PROGRAM_ARB); + ShaderEnabled = true; + } + if(CurrentShader != Shader) + { + if(Shader != 0) + CurrentShader = Shader; + glBindProgramARB(GL_FRAGMENT_PROGRAM_ARB, CurrentShader); + } +} +// GLSL Specific +void PixelShaderCache::SetPSSampler(const char * name, unsigned int Tex) +{ + PROGRAMSHADER tmp = ProgramShaderCache::GetShaderProgram(); + for(int a = 0; a < NUM_UNIFORMS; ++a) + if(!strcmp(name, UniformNames[a])) + { + if(tmp.UniformLocations[a] == -1) + return; + else + { + glUniform1i(tmp.UniformLocations[a], Tex); + return; + } + } +} +void SetPSConstant4fvByName(const char * name, unsigned int offset, const float *f, const unsigned int count = 1) +{ + PROGRAMSHADER tmp = ProgramShaderCache::GetShaderProgram(); + for(int a = 0; a < NUM_UNIFORMS; ++a) + if(!strcmp(name, UniformNames[a])) + { + if(tmp.UniformLocations[a] == -1) + return; + else + { + glUniform4fv(tmp.UniformLocations[a] + offset, count, f); + return; + } + } +} +void SetGLSLPSConstant4f(unsigned int const_number, float f1, float f2, float f3, float f4) +{ + float f[4] = { f1, f2, f3, f4 }; + for( unsigned int a = 0; a < 10; ++a) + { + if( const_number >= PSVar_Loc[a].reg && const_number < (PSVar_Loc[a].reg + PSVar_Loc[a].size)) + { + unsigned int offset = const_number - PSVar_Loc[a].reg; + SetPSConstant4fvByName(PSVar_Loc[a].name, offset, f); + return; + } + } } +void SetGLSLPSConstant4fv(unsigned int const_number, const float *f) +{ + for( unsigned int a = 0; a < 10; ++a) + { + if( const_number >= PSVar_Loc[a].reg && const_number < (PSVar_Loc[a].reg + PSVar_Loc[a].size)) + { + unsigned int offset = const_number - PSVar_Loc[a].reg; + SetPSConstant4fvByName(PSVar_Loc[a].name, offset, f); + return; + } + } +} + +void SetMultiGLSLPSConstant4fv(unsigned int const_number, unsigned int count, const float *f) +{ + for( unsigned int a = 0; a < 10; ++a) + { + if( const_number >= PSVar_Loc[a].reg && const_number < (PSVar_Loc[a].reg + PSVar_Loc[a].size)) + { + unsigned int offset = const_number - PSVar_Loc[a].reg; + SetPSConstant4fvByName(PSVar_Loc[a].name, offset, f, count); + return; + } + } +} +// CG Specific +void SetCGPSConstant4f(unsigned int const_number, float f1, float f2, float f3, float f4) +{ + float f[4] = { f1, f2, f3, f4 }; + glProgramEnvParameter4fvARB(GL_FRAGMENT_PROGRAM_ARB, const_number, f); +} + +void SetCGPSConstant4fv(unsigned int const_number, const float *f) +{ + glProgramEnvParameter4fvARB(GL_FRAGMENT_PROGRAM_ARB, const_number, f); +} + +void SetMultiCGPSConstant4fv(unsigned int const_number, unsigned int count, const float *f) +{ + for (unsigned int i = 0; i < count; i++,f+=4) + glProgramEnvParameter4fvARB(GL_FRAGMENT_PROGRAM_ARB, const_number + i, f); +} +// Renderer functions void Renderer::SetPSConstant4f(unsigned int const_number, float f1, float f2, float f3, float f4) { - float f[4] = { f1, f2, f3, f4 }; - glProgramEnvParameter4fvARB(GL_FRAGMENT_PROGRAM_ARB, const_number, f); + pSetPSConstant4f(const_number, f1, f2, f3, f4); } void Renderer::SetPSConstant4fv(unsigned int const_number, const float *f) { - glProgramEnvParameter4fvARB(GL_FRAGMENT_PROGRAM_ARB, const_number, f); + pSetPSConstant4fv(const_number, f); } void Renderer::SetMultiPSConstant4fv(unsigned int const_number, unsigned int count, const float *f) { - for (unsigned int i = 0; i < count; i++,f+=4) - glProgramEnvParameter4fvARB(GL_FRAGMENT_PROGRAM_ARB, const_number + i, f); + pSetMultiPSConstant4fv(const_number, count, f); } - } // namespace OGL diff --git a/Source/Plugins/Plugin_VideoOGL/Src/PixelShaderCache.h b/Source/Plugins/Plugin_VideoOGL/Src/PixelShaderCache.h index f528eeb35b..9d61d7229a 100644 --- a/Source/Plugins/Plugin_VideoOGL/Src/PixelShaderCache.h +++ b/Source/Plugins/Plugin_VideoOGL/Src/PixelShaderCache.h @@ -73,6 +73,8 @@ class PixelShaderCache public: static void Init(); static void Shutdown(); + // This is a GLSL only function + static void SetPSSampler(const char * name, unsigned int Tex); static FRAGMENTSHADER* SetShader(DSTALPHA_MODE dstAlphaMode, u32 components); static bool CompilePixelShader(FRAGMENTSHADER& ps, const char* pstrprogram); @@ -85,7 +87,15 @@ public: static void DisableShader(); }; +// GLSL Specific +void SetGLSLPSConstant4f(unsigned int const_number, float f1, float f2, float f3, float f4); +void SetGLSLPSConstant4fv(unsigned int const_number, const float *f); +void SetMultiGLSLPSConstant4fv(unsigned int const_number, unsigned int count, const float *f); +//CG Specific +void SetCGPSConstant4f(unsigned int const_number, float f1, float f2, float f3, float f4); +void SetCGPSConstant4fv(unsigned int const_number, const float *f); +void SetMultiCGPSConstant4fv(unsigned int const_number, unsigned int count, const float *f); } // namespace OGL #endif // _PIXELSHADERCACHE_H_ diff --git a/Source/Plugins/Plugin_VideoOGL/Src/ProgramShaderCache.cpp b/Source/Plugins/Plugin_VideoOGL/Src/ProgramShaderCache.cpp new file mode 100644 index 0000000000..fc056aed63 --- /dev/null +++ b/Source/Plugins/Plugin_VideoOGL/Src/ProgramShaderCache.cpp @@ -0,0 +1,150 @@ +// Copyright (C) 2003 Dolphin Project. + +// This program is free software: you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation, version 2.0. + +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License 2.0 for more details. + +// A copy of the GPL 2.0 should have been included with the program. +// If not, see http://www.gnu.org/licenses/ + +// Official SVN repository and contact information can be found at +// http://code.google.com/p/dolphin-emu/ +#include "ProgramShaderCache.h" + +namespace OGL +{ + GLuint ProgramShaderCache::CurrentFShader = 0, ProgramShaderCache::CurrentVShader = 0, ProgramShaderCache::CurrentProgram = 0; + ProgramShaderCache::PCache ProgramShaderCache::pshaders; + + std::pair ProgramShaderCache::CurrentShaderProgram; + const char *UniformNames[NUM_UNIFORMS] = { + // SAMPLERS + "samp0","samp1","samp2","samp3","samp4","samp5","samp6","samp7", + // PIXEL SHADER UNIFORMS + I_COLORS, + I_KCOLORS, + I_ALPHA, + I_TEXDIMS, + I_ZBIAS , + I_INDTEXSCALE , + I_INDTEXMTX, + I_FOG, + I_PLIGHTS, + I_PMATERIALS, + // VERTEX SHADER UNIFORMS + I_POSNORMALMATRIX, + I_PROJECTION , + I_MATERIALS, + I_LIGHTS, + I_TEXMATRICES, + I_TRANSFORMMATRICES , + I_NORMALMATRICES , + I_POSTTRANSFORMMATRICES, + I_DEPTHPARAMS, + }; + + void ProgramShaderCache::SetBothShaders(GLuint PS, GLuint VS) + { + PROGRAMUID uid; + CurrentFShader = PS; + CurrentVShader = VS; + + + GetProgramShaderId(&uid, CurrentVShader, CurrentFShader); + + if(uid.uid.id == 0) + { + CurrentProgram = 0; + glUseProgram(0); + return; + } + + // Fragment shaders can survive without Vertex Shaders + // We have a valid fragment shader, let's create our program + std::pair ShaderPair = std::make_pair(uid.uid.psid, uid.uid.vsid); + PCache::iterator iter = pshaders.find(ShaderPair); + if (iter != pshaders.end()) + { + PCacheEntry &entry = iter->second; + glUseProgram(entry.program.glprogid); + CurrentShaderProgram = ShaderPair; + CurrentProgram = entry.program.glprogid; + return; + } + PCacheEntry entry; + entry.program.vsid = CurrentVShader; + entry.program.psid = CurrentFShader; + entry.program.glprogid = glCreateProgram(); + + // Right, the program is created now + // Let's attach everything + if(entry.program.vsid != 0) // attaching zero vertex shader makes it freak out + glAttachShader(entry.program.glprogid, entry.program.vsid); + glAttachShader(entry.program.glprogid, entry.program.psid); + glLinkProgram(entry.program.glprogid); + //checkForGLError("linking program"); + glUseProgram(entry.program.glprogid); + //checkForGLError("using program"); + + // We cache our uniform locations for now + // Once we move up to a newer version of GLSL, ~1.30 + // We can remove this + + //For some reason this fails on my hardware + //glGetUniformIndices(entry.program.glprogid, NUM_UNIFORMS, UniformNames, entry.program.UniformLocations); + //Got to do it this crappy way. + for(int a = 0; a < NUM_UNIFORMS; ++a) + entry.program.UniformLocations[a] = glGetUniformLocation(entry.program.glprogid, UniformNames[a]); + + // Need to get some attribute locations + if(uid.uid.vsid != 0) // We have no vertex Shader + { + entry.program.attrLoc[0] = glGetAttribLocation(entry.program.glprogid, "rawnorm1"); + entry.program.attrLoc[1] = glGetAttribLocation(entry.program.glprogid, "rawnorm2"); + entry.program.attrLoc[2] = glGetAttribLocation(entry.program.glprogid, "fposmtx"); + if(entry.program.attrLoc[0] > 0) + glEnableVertexAttribArray(entry.program.attrLoc[0]); + if(entry.program.attrLoc[1] > 0) + glEnableVertexAttribArray(entry.program.attrLoc[1]); + if(entry.program.attrLoc[2] > 0) + glEnableVertexAttribArray(entry.program.attrLoc[2]); + } + else + entry.program.attrLoc[0] = entry.program.attrLoc[1] = entry.program.attrLoc[2] = 0; + + + pshaders[ShaderPair] = entry; + CurrentShaderProgram = ShaderPair; + CurrentProgram = entry.program.glprogid; + } + + GLuint ProgramShaderCache::GetCurrentProgram(void) { return CurrentProgram; } + + GLint ProgramShaderCache::GetAttr(int num) + { + return pshaders[CurrentShaderProgram].program.attrLoc[num]; + } + PROGRAMSHADER ProgramShaderCache::GetShaderProgram() + { + return pshaders[CurrentShaderProgram].program; + } + + void ProgramShaderCache::Shutdown(void) + { + PCache::iterator iter = pshaders.begin(); + for (; iter != pshaders.end(); iter++) + iter->second.Destroy(); + pshaders.clear(); + } +} + +void GetProgramShaderId(PROGRAMUID *uid, GLuint _v, GLuint _p) +{ + uid->uid.vsid = _v; + uid->uid.psid = _p; +} diff --git a/Source/Plugins/Plugin_VideoOGL/Src/ProgramShaderCache.h b/Source/Plugins/Plugin_VideoOGL/Src/ProgramShaderCache.h new file mode 100644 index 0000000000..53abe440f5 --- /dev/null +++ b/Source/Plugins/Plugin_VideoOGL/Src/ProgramShaderCache.h @@ -0,0 +1,108 @@ +// Copyright (C) 2003 Dolphin Project. + +// This program is free software: you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation, version 2.0. + +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License 2.0 for more details. + +// A copy of the GPL 2.0 should have been included with the program. +// If not, see http://www.gnu.org/licenses/ + +// Official SVN repository and contact information can be found at +// http://code.google.com/p/dolphin-emu/ + +#ifndef _ProgramShaderCache_H_ +#define _ProgramShaderCache_H_ + +#include "GLUtil.h" + +#include "VertexShaderCache.h" +#include "PixelShaderCache.h" +#include "PixelShaderGen.h" +#include "VertexShaderGen.h" + + union PID + { + struct { + GLuint vsid, psid; + }; + u64 id; + }; + +class PROGRAMUID +{ +public: + + PID uid; + + PROGRAMUID() + { + uid.id = 0; + } + + PROGRAMUID(const PROGRAMUID& r) + { + uid.id = r.uid.id; + } + PROGRAMUID(GLuint _v, GLuint _p) + { + uid.vsid = _v; + uid.psid = _p; + } + + int GetNumValues() const + { + return uid.id; + } +}; +void GetProgramShaderId(PROGRAMUID *uid, GLuint _v, GLuint _p); + +namespace OGL +{ +#define NUM_UNIFORMS 27 +extern const char *UniformNames[NUM_UNIFORMS]; + + +struct PROGRAMSHADER +{ + PROGRAMSHADER() : glprogid(0), vsid(0), psid(0){} + GLuint glprogid; // opengl program id + GLuint vsid, psid; + GLint attrLoc[3]; + GLint UniformLocations[NUM_UNIFORMS]; +}; + + +class ProgramShaderCache +{ + struct PCacheEntry + { + PROGRAMSHADER program; + int frameCount; + PCacheEntry() : frameCount(0) {} + void Destroy() { + glDeleteProgram(program.glprogid); + program.glprogid = 0; + } + }; + typedef std::map, PCacheEntry> PCache; + + static PCache pshaders; + static GLuint CurrentFShader, CurrentVShader, CurrentProgram; + static std::pair CurrentShaderProgram; +public: + static PROGRAMSHADER GetShaderProgram(); + static GLint GetAttr(int num); + static void SetBothShaders(GLuint PS, GLuint VS); + static GLuint GetCurrentProgram(void); + static void Shutdown(void); + +}; + +} // namespace OGL + +#endif diff --git a/Source/Plugins/Plugin_VideoOGL/Src/Render.cpp b/Source/Plugins/Plugin_VideoOGL/Src/Render.cpp index 792c14960e..1dae8d0752 100644 --- a/Source/Plugins/Plugin_VideoOGL/Src/Render.cpp +++ b/Source/Plugins/Plugin_VideoOGL/Src/Render.cpp @@ -45,6 +45,7 @@ #include "DLCache.h" #include "PixelShaderCache.h" #include "PixelShaderManager.h" +#include "ProgramShaderCache.h" #include "VertexShaderCache.h" #include "VertexShaderManager.h" #include "VertexLoaderManager.h" @@ -125,8 +126,8 @@ static std::thread scrshotThread; // EFB cache related const u32 EFB_CACHE_RECT_SIZE = 64; // Cache 64x64 blocks. -const u32 EFB_CACHE_WIDTH = EFB_WIDTH / EFB_CACHE_RECT_SIZE; -const u32 EFB_CACHE_HEIGHT = EFB_HEIGHT / EFB_CACHE_RECT_SIZE; +const u32 EFB_CACHE_WIDTH = (EFB_WIDTH + EFB_CACHE_RECT_SIZE - 1) / EFB_CACHE_RECT_SIZE; // round up +const u32 EFB_CACHE_HEIGHT = (EFB_HEIGHT + EFB_CACHE_RECT_SIZE - 1) / EFB_CACHE_RECT_SIZE; static bool s_efbCacheValid[2][EFB_CACHE_WIDTH * EFB_CACHE_HEIGHT]; static std::vector s_efbCache[2][EFB_CACHE_WIDTH * EFB_CACHE_HEIGHT]; // 2 for PEEK_Z and PEEK_COLOR @@ -698,7 +699,8 @@ u32 Renderer::AccessEFB(EFBAccessType type, u32 x, u32 y, u32 poke_data) if (!g_ActiveConfig.bEFBAccessEnable) return 0; - u32 cacheRectIdx = ((x / EFB_CACHE_RECT_SIZE) << 16) | (y / EFB_CACHE_RECT_SIZE); + u32 cacheRectIdx = (y / EFB_CACHE_RECT_SIZE) * EFB_CACHE_WIDTH + + (x / EFB_CACHE_RECT_SIZE); // Get the rectangular target region containing the EFB pixel EFBRectangle efbPixelRc; @@ -1426,8 +1428,13 @@ void Renderer::ResetAPIState() { // Gets us to a reasonably sane state where it's possible to do things like // image copies with textured quads, etc. - VertexShaderCache::DisableShader(); - PixelShaderCache::DisableShader(); + if(g_ActiveConfig.bUseGLSL) + ProgramShaderCache::SetBothShaders(0, 0); + else + { + VertexShaderCache::DisableShader(); + PixelShaderCache::DisableShader(); + } glDisable(GL_SCISSOR_TEST); glDisable(GL_DEPTH_TEST); glDisable(GL_CULL_FACE); @@ -1452,9 +1459,14 @@ void Renderer::RestoreAPIState() if (g_ActiveConfig.bWireFrame) glPolygonMode(GL_FRONT_AND_BACK, GL_LINE); - - VertexShaderCache::SetCurrentShader(0); - PixelShaderCache::SetCurrentShader(0); + + if(g_ActiveConfig.bUseGLSL) + ProgramShaderCache::SetBothShaders(0, 0); + else + { + VertexShaderCache::SetCurrentShader(0); + PixelShaderCache::SetCurrentShader(0); + } } void Renderer::SetGenerationMode() diff --git a/Source/Plugins/Plugin_VideoOGL/Src/VertexManager.cpp b/Source/Plugins/Plugin_VideoOGL/Src/VertexManager.cpp index 5144c328d9..2c97b904b2 100644 --- a/Source/Plugins/Plugin_VideoOGL/Src/VertexManager.cpp +++ b/Source/Plugins/Plugin_VideoOGL/Src/VertexManager.cpp @@ -33,6 +33,7 @@ #include "PixelShaderManager.h" #include "VertexShaderCache.h" #include "VertexShaderManager.h" +#include "ProgramShaderCache.h" #include "VertexShaderGen.h" #include "VertexLoader.h" #include "VertexManager.h" @@ -176,10 +177,6 @@ void VertexManager::vFlush() } } - // set global constants - VertexShaderManager::SetConstants(); - PixelShaderManager::SetConstants(); - bool useDstAlpha = !g_ActiveConfig.bDstAlphaPass && bpmem.dstalpha.enable && bpmem.blendmode.alphaupdate && bpmem.zcontrol.pixel_format == PIXELFMT_RGBA6_Z24; @@ -212,8 +209,18 @@ void VertexManager::vFlush() FRAGMENTSHADER* ps = PixelShaderCache::SetShader(DSTALPHA_NONE,g_nativeVertexFmt->m_components); #endif VERTEXSHADER* vs = VertexShaderCache::SetShader(g_nativeVertexFmt->m_components); - if (ps) PixelShaderCache::SetCurrentShader(ps->glprogid); // Lego Star Wars crashes here. - if (vs) VertexShaderCache::SetCurrentShader(vs->glprogid); + + if(g_ActiveConfig.bUseGLSL) + ProgramShaderCache::SetBothShaders(ps->glprogid, vs->glprogid); + else + { + if (ps) PixelShaderCache::SetCurrentShader(ps->glprogid); // Lego Star Wars crashes here. + if (vs) VertexShaderCache::SetCurrentShader(vs->glprogid); + } + + // set global constants + VertexShaderManager::SetConstants(); + PixelShaderManager::SetConstants(); Draw(); @@ -221,7 +228,13 @@ void VertexManager::vFlush() if (useDstAlpha && !dualSourcePossible) { ps = PixelShaderCache::SetShader(DSTALPHA_ALPHA_PASS,g_nativeVertexFmt->m_components); - if (ps) PixelShaderCache::SetCurrentShader(ps->glprogid); + if(g_ActiveConfig.bUseGLSL) + { + ProgramShaderCache::SetBothShaders(ps->glprogid, 0); + PixelShaderManager::SetConstants(); // Need to set these again + } + else + if (ps) PixelShaderCache::SetCurrentShader(ps->glprogid); // only update alpha glColorMask(GL_FALSE, GL_FALSE, GL_FALSE, GL_TRUE); diff --git a/Source/Plugins/Plugin_VideoOGL/Src/VertexShaderCache.cpp b/Source/Plugins/Plugin_VideoOGL/Src/VertexShaderCache.cpp index dbbb7ee29b..a7b1c68e7a 100644 --- a/Source/Plugins/Plugin_VideoOGL/Src/VertexShaderCache.cpp +++ b/Source/Plugins/Plugin_VideoOGL/Src/VertexShaderCache.cpp @@ -26,6 +26,7 @@ #include "Render.h" #include "VertexShaderGen.h" #include "VertexShaderManager.h" +#include "ProgramShaderCache.h" #include "VertexShaderCache.h" #include "VertexManager.h" #include "VertexLoader.h" @@ -45,194 +46,293 @@ VertexShaderCache::VSCacheEntry* VertexShaderCache::last_entry = NULL; VERTEXSHADERUID VertexShaderCache::last_uid; static int s_nMaxVertexInstructions; - +void (*pSetVSConstant4f)(unsigned int, float, float, float, float); +void (*pSetVSConstant4fv)(unsigned int, const float*); +void (*pSetMultiVSConstant4fv)(unsigned int, unsigned int, const float*); +void (*pSetMultiVSConstant3fv)(unsigned int, unsigned int, const float*); void VertexShaderCache::Init() { - glEnable(GL_VERTEX_PROGRAM_ARB); - ShaderEnabled = true; - CurrentShader = 0; - last_entry = NULL; + glEnable(GL_VERTEX_PROGRAM_ARB); + ShaderEnabled = true; + CurrentShader = 0; + last_entry = NULL; - glGetProgramivARB(GL_VERTEX_PROGRAM_ARB, GL_MAX_PROGRAM_NATIVE_INSTRUCTIONS_ARB, (GLint *)&s_nMaxVertexInstructions); - if (strstr((const char*)glGetString(GL_VENDOR), "Humper") != NULL) s_nMaxVertexInstructions = 4096; + if(g_ActiveConfig.bUseGLSL) + { + pSetVSConstant4f = SetGLSLVSConstant4f; + pSetVSConstant4fv = SetGLSLVSConstant4fv; + pSetMultiVSConstant4fv = SetMultiGLSLVSConstant4fv; + pSetMultiVSConstant3fv = SetMultiGLSLVSConstant3fv; + } + else + { + pSetVSConstant4f = SetCGVSConstant4f; + pSetVSConstant4fv = SetCGVSConstant4fv; + pSetMultiVSConstant4fv = SetMultiCGVSConstant4fv; + pSetMultiVSConstant3fv = SetMultiCGVSConstant3fv; + } + + glGetProgramivARB(GL_VERTEX_PROGRAM_ARB, GL_MAX_PROGRAM_NATIVE_INSTRUCTIONS_ARB, (GLint *)&s_nMaxVertexInstructions); + if (strstr((const char*)glGetString(GL_VENDOR), "Humper") != NULL) s_nMaxVertexInstructions = 4096; #if CG_VERSION_NUM == 2100 - if (strstr((const char*)glGetString(GL_VENDOR), "ATI") != NULL) - { - s_nMaxVertexInstructions = 4096; - } + if (strstr((const char*)glGetString(GL_VENDOR), "ATI") != NULL) + { + s_nMaxVertexInstructions = 4096; + } #endif } void VertexShaderCache::Shutdown() { - for (VSCache::iterator iter = vshaders.begin(); iter != vshaders.end(); ++iter) - iter->second.Destroy(); - vshaders.clear(); + for (VSCache::iterator iter = vshaders.begin(); iter != vshaders.end(); ++iter) + iter->second.Destroy(); + vshaders.clear(); } VERTEXSHADER* VertexShaderCache::SetShader(u32 components) { - VERTEXSHADERUID uid; - GetVertexShaderId(&uid, components); - if (last_entry) - { - if (uid == last_uid) - { - GFX_DEBUGGER_PAUSE_AT(NEXT_VERTEX_SHADER_CHANGE, true); - ValidateVertexShaderIDs(API_OPENGL, vshaders[uid].safe_uid, vshaders[uid].shader.strprog, components); - return &last_entry->shader; - } - } + VERTEXSHADERUID uid; + GetVertexShaderId(&uid, components); + if (last_entry) + { + if (uid == last_uid) + { + GFX_DEBUGGER_PAUSE_AT(NEXT_VERTEX_SHADER_CHANGE, true); + ValidateVertexShaderIDs(API_OPENGL, vshaders[uid].safe_uid, vshaders[uid].shader.strprog, components); + return &last_entry->shader; + } + } - last_uid = uid; + last_uid = uid; - VSCache::iterator iter = vshaders.find(uid); - if (iter != vshaders.end()) - { - VSCacheEntry &entry = iter->second; - last_entry = &entry; + VSCache::iterator iter = vshaders.find(uid); + if (iter != vshaders.end()) + { + VSCacheEntry &entry = iter->second; + last_entry = &entry; - GFX_DEBUGGER_PAUSE_AT(NEXT_VERTEX_SHADER_CHANGE, true); - ValidateVertexShaderIDs(API_OPENGL, entry.safe_uid, entry.shader.strprog, components); - return &last_entry->shader; - } + GFX_DEBUGGER_PAUSE_AT(NEXT_VERTEX_SHADER_CHANGE, true); + ValidateVertexShaderIDs(API_OPENGL, entry.safe_uid, entry.shader.strprog, components); + return &last_entry->shader; + } - // Make an entry in the table - VSCacheEntry& entry = vshaders[uid]; - last_entry = &entry; - const char *code = GenerateVertexShaderCode(components, API_OPENGL); - GetSafeVertexShaderId(&entry.safe_uid, components); + // Make an entry in the table + VSCacheEntry& entry = vshaders[uid]; + last_entry = &entry; + const char *code = GenerateVertexShaderCode(components, API_OPENGL); + GetSafeVertexShaderId(&entry.safe_uid, components); #if defined(_DEBUG) || defined(DEBUGFAST) - if (g_ActiveConfig.iLog & CONF_SAVESHADERS && code) { - static int counter = 0; - char szTemp[MAX_PATH]; - sprintf(szTemp, "%svs_%04i.txt", File::GetUserPath(D_DUMP_IDX).c_str(), counter++); + if (g_ActiveConfig.iLog & CONF_SAVESHADERS && code) { + static int counter = 0; + char szTemp[MAX_PATH]; + sprintf(szTemp, "%svs_%04i.txt", File::GetUserPath(D_DUMP_IDX).c_str(), counter++); - SaveData(szTemp, code); - } + SaveData(szTemp, code); + } #endif - if (!code || !VertexShaderCache::CompileVertexShader(entry.shader, code)) { - GFX_DEBUGGER_PAUSE_AT(NEXT_ERROR, true); - return NULL; - } + if (!code || !VertexShaderCache::CompileVertexShader(entry.shader, code)) { + GFX_DEBUGGER_PAUSE_AT(NEXT_ERROR, true); + return NULL; + } - INCSTAT(stats.numVertexShadersCreated); - SETSTAT(stats.numVertexShadersAlive, vshaders.size()); - GFX_DEBUGGER_PAUSE_AT(NEXT_VERTEX_SHADER_CHANGE, true); - return &last_entry->shader; + INCSTAT(stats.numVertexShadersCreated); + SETSTAT(stats.numVertexShadersAlive, vshaders.size()); + GFX_DEBUGGER_PAUSE_AT(NEXT_VERTEX_SHADER_CHANGE, true); + return &last_entry->shader; } bool VertexShaderCache::CompileVertexShader(VERTEXSHADER& vs, const char* pstrprogram) { - // Reset GL error before compiling shaders. Yeah, we need to investigate the causes of these. - GLenum err = GL_REPORT_ERROR(); - if (err != GL_NO_ERROR) - { - ERROR_LOG(VIDEO, "glError %08x before VS!", err); - } + // Reset GL error before compiling shaders. Yeah, we need to investigate the causes of these. + GLenum err = GL_REPORT_ERROR(); + if (err != GL_NO_ERROR) + { + ERROR_LOG(VIDEO, "glError %08x before VS!", err); + } #if defined HAVE_CG && HAVE_CG - char stropt[64]; - sprintf(stropt, "MaxLocalParams=256,MaxInstructions=%d", s_nMaxVertexInstructions); - const char *opts[] = {"-profileopts", stropt, "-O2", "-q", NULL}; - CGprogram tempprog = cgCreateProgram(g_cgcontext, CG_SOURCE, pstrprogram, g_cgvProf, "main", opts); - if (!cgIsProgram(tempprog)) { - static int num_failures = 0; - char szTemp[MAX_PATH]; - sprintf(szTemp, "%sbad_vs_%04i.txt", File::GetUserPath(D_DUMP_IDX).c_str(), num_failures++); - std::ofstream file(szTemp); - file << pstrprogram; - file.close(); + char stropt[64]; + sprintf(stropt, "MaxLocalParams=256,MaxInstructions=%d", s_nMaxVertexInstructions); + const char *opts[] = {"-profileopts", stropt, "-O2", "-q", NULL}; + CGprogram tempprog = cgCreateProgram(g_cgcontext, CG_SOURCE, pstrprogram, g_cgvProf, "main", opts); + if (!cgIsProgram(tempprog)) { + static int num_failures = 0; + char szTemp[MAX_PATH]; + sprintf(szTemp, "%sbad_vs_%04i.txt", File::GetUserPath(D_DUMP_IDX).c_str(), num_failures++); + std::ofstream file(szTemp); + file << pstrprogram; + file.close(); - PanicAlert("Failed to compile vertex 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 (%d):\n%s", - szTemp, - g_cgfProf, - cgGetLastListing(g_cgcontext)); + PanicAlert("Failed to compile vertex 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 (%d):\n%s", + szTemp, + g_cgfProf, + cgGetLastListing(g_cgcontext)); cgDestroyProgram(tempprog); - ERROR_LOG(VIDEO, "Failed to load vs %s:", cgGetLastListing(g_cgcontext)); - ERROR_LOG(VIDEO, "%s", pstrprogram); - return false; - } + ERROR_LOG(VIDEO, "Failed to load vs %s:", cgGetLastListing(g_cgcontext)); + ERROR_LOG(VIDEO, "%s", pstrprogram); + return false; + } - if (cgGetError() != CG_NO_ERROR) - { - WARN_LOG(VIDEO, "Failed to load vs %s:", cgGetLastListing(g_cgcontext)); - WARN_LOG(VIDEO, "%s", pstrprogram); - } + if (cgGetError() != CG_NO_ERROR) + { + WARN_LOG(VIDEO, "Failed to load vs %s:", cgGetLastListing(g_cgcontext)); + WARN_LOG(VIDEO, "%s", pstrprogram); + } - // This looks evil - we modify the program through the const char * we got from cgGetProgramString! - // It SHOULD not have any nasty side effects though - but you never know... - char *pcompiledprog = (char*)cgGetProgramString(tempprog, CG_COMPILED_PROGRAM); - char *plocal = strstr(pcompiledprog, "program.local"); - while (plocal != NULL) { - const char* penv = " program.env"; - memcpy(plocal, penv, 13); - plocal = strstr(plocal + 13, "program.local"); - } - glGenProgramsARB(1, &vs.glprogid); - SetCurrentShader(vs.glprogid); + // This looks evil - we modify the program through the const char * we got from cgGetProgramString! + // It SHOULD not have any nasty side effects though - but you never know... + char *pcompiledprog = (char*)cgGetProgramString(tempprog, CG_COMPILED_PROGRAM); + char *plocal = strstr(pcompiledprog, "program.local"); + while (plocal != NULL) { + const char* penv = " program.env"; + memcpy(plocal, penv, 13); + plocal = strstr(plocal + 13, "program.local"); + } + glGenProgramsARB(1, &vs.glprogid); + SetCurrentShader(vs.glprogid); - glProgramStringARB(GL_VERTEX_PROGRAM_ARB, GL_PROGRAM_FORMAT_ASCII_ARB, (GLsizei)strlen(pcompiledprog), pcompiledprog); - err = GL_REPORT_ERROR(); - if (err != GL_NO_ERROR) { - ERROR_LOG(VIDEO, "%s", pstrprogram); - ERROR_LOG(VIDEO, "%s", pcompiledprog); - } + glProgramStringARB(GL_VERTEX_PROGRAM_ARB, GL_PROGRAM_FORMAT_ASCII_ARB, (GLsizei)strlen(pcompiledprog), pcompiledprog); + err = GL_REPORT_ERROR(); + if (err != GL_NO_ERROR) { + ERROR_LOG(VIDEO, "%s", pstrprogram); + ERROR_LOG(VIDEO, "%s", pcompiledprog); + } - cgDestroyProgram(tempprog); + cgDestroyProgram(tempprog); #endif - if (g_ActiveConfig.bEnableShaderDebugging) - vs.strprog = pstrprogram; + if (g_ActiveConfig.bEnableShaderDebugging) + vs.strprog = pstrprogram; - return true; + return true; } void VertexShaderCache::DisableShader() { - if (ShaderEnabled) - { - glDisable(GL_VERTEX_PROGRAM_ARB); - ShaderEnabled = false; - } + if (ShaderEnabled) + { + glDisable(GL_VERTEX_PROGRAM_ARB); + ShaderEnabled = false; + } } void VertexShaderCache::SetCurrentShader(GLuint Shader) { - if (!ShaderEnabled) - { - glEnable(GL_VERTEX_PROGRAM_ARB); - ShaderEnabled= true; - } - if (CurrentShader != Shader) - { - if(Shader != 0) - CurrentShader = Shader; - glBindProgramARB(GL_VERTEX_PROGRAM_ARB, CurrentShader); - } + if (!ShaderEnabled) + { + glEnable(GL_VERTEX_PROGRAM_ARB); + ShaderEnabled= true; + } + if (CurrentShader != Shader) + { + if(Shader != 0) + CurrentShader = Shader; + glBindProgramARB(GL_VERTEX_PROGRAM_ARB, CurrentShader); + } } - -void Renderer::SetVSConstant4f(unsigned int const_number, float f1, float f2, float f3, float f4) +// GLSL Specific +void SetVSConstant4fvByName(const char * name, unsigned int offset, const float *f, const unsigned int count = 1) { - glProgramEnvParameter4fARB(GL_VERTEX_PROGRAM_ARB, const_number, f1, f2, f3, f4); + PROGRAMSHADER tmp = ProgramShaderCache::GetShaderProgram(); + for(int a = 0; a < NUM_UNIFORMS; ++a) + if(!strcmp(name, UniformNames[a])) + { + if(tmp.UniformLocations[a] == -1) + return; + else + { + glUniform4fv(tmp.UniformLocations[a] + offset, count, f); + return; + } + } } - -void Renderer::SetVSConstant4fv(unsigned int const_number, const float *f) +void SetGLSLVSConstant4f(unsigned int const_number, float f1, float f2, float f3, float f4) { - glProgramEnvParameter4fvARB(GL_VERTEX_PROGRAM_ARB, const_number, f); + float buf[4]; + buf[0] = f1; + buf[1] = f2; + buf[2] = f3; + buf[3] = f4; + for( unsigned int a = 0; a < 9; ++a) + { + if( const_number >= VSVar_Loc[a].reg && const_number < ( VSVar_Loc[a].reg + VSVar_Loc[a].size)) + { + unsigned int offset = const_number - VSVar_Loc[a].reg; + SetVSConstant4fvByName(VSVar_Loc[a].name, offset, buf); + return; + } + } } -void Renderer::SetMultiVSConstant4fv(unsigned int const_number, unsigned int count, const float *f) +void SetGLSLVSConstant4fv(unsigned int const_number, const float *f) +{ + for( unsigned int a = 0; a < 9; ++a) + { + if( const_number >= VSVar_Loc[a].reg && const_number < ( VSVar_Loc[a].reg + VSVar_Loc[a].size)) + { + unsigned int offset = const_number - VSVar_Loc[a].reg; + SetVSConstant4fvByName(VSVar_Loc[a].name, offset, f); + return; + } + } +} + +void SetMultiGLSLVSConstant4fv(unsigned int const_number, unsigned int count, const float *f) +{ + for( unsigned int a = 0; a < 9; ++a) + { + if( const_number >= VSVar_Loc[a].reg && const_number < ( VSVar_Loc[a].reg + VSVar_Loc[a].size)) + { + unsigned int offset = const_number - VSVar_Loc[a].reg; + SetVSConstant4fvByName(VSVar_Loc[a].name, offset, f, count); + return; + } + } +} + +void SetMultiGLSLVSConstant3fv(unsigned int const_number, unsigned int count, const float *f) +{ + float buf[4 * C_VENVCONST_END]; + for (unsigned int i = 0; i < count; i++) + { + buf[4*i ] = *f++; + buf[4*i+1] = *f++; + buf[4*i+2] = *f++; + buf[4*i+3] = 0.f; + } + for( unsigned int a = 0; a < 9; ++a) + { + if( const_number >= VSVar_Loc[a].reg && const_number < ( VSVar_Loc[a].reg + VSVar_Loc[a].size)) + { + unsigned int offset = const_number - VSVar_Loc[a].reg; + SetVSConstant4fvByName(VSVar_Loc[a].name, offset, buf, count); + return; + } + } +} + +// CG Specific +void SetCGVSConstant4f(unsigned int const_number, float f1, float f2, float f3, float f4) +{ + glProgramEnvParameter4fARB(GL_VERTEX_PROGRAM_ARB, const_number, f1, f2, f3, f4); +} + +void SetCGVSConstant4fv(unsigned int const_number, const float *f) +{ + glProgramEnvParameter4fvARB(GL_VERTEX_PROGRAM_ARB, const_number, f); +} + +void SetMultiCGVSConstant4fv(unsigned int const_number, unsigned int count, const float *f) { if(GLEW_EXT_gpu_program_parameters) { - glProgramEnvParameters4fvEXT(GL_VERTEX_PROGRAM_ARB, const_number, count, f); + glProgramEnvParameters4fvEXT(GL_VERTEX_PROGRAM_ARB, const_number, count, f); } else { @@ -241,32 +341,54 @@ void Renderer::SetMultiVSConstant4fv(unsigned int const_number, unsigned int cou } } -void Renderer::SetMultiVSConstant3fv(unsigned int const_number, unsigned int count, const float *f) +void SetMultiCGVSConstant3fv(unsigned int const_number, unsigned int count, const float *f) { - if(GLEW_EXT_gpu_program_parameters) - { + if(GLEW_EXT_gpu_program_parameters) + { float buf[4 * C_VENVCONST_END]; for (unsigned int i = 0; i < count; i++) - { - buf[4*i ] = *f++; - buf[4*i+1] = *f++; - buf[4*i+2] = *f++; - buf[4*i+3] = 0.f; - } + { + buf[4*i ] = *f++; + buf[4*i+1] = *f++; + buf[4*i+2] = *f++; + buf[4*i+3] = 0.f; + } glProgramEnvParameters4fvEXT(GL_VERTEX_PROGRAM_ARB, const_number, count, buf); } else { for (unsigned int i = 0; i < count; i++) - { - float buf[4]; - buf[0] = *f++; - buf[1] = *f++; - buf[2] = *f++; - buf[3] = 0.f; - glProgramEnvParameter4fvARB(GL_VERTEX_PROGRAM_ARB, const_number + i, buf); - } + { + float buf[4]; + buf[0] = *f++; + buf[1] = *f++; + buf[2] = *f++; + buf[3] = 0.f; + glProgramEnvParameter4fvARB(GL_VERTEX_PROGRAM_ARB, const_number + i, buf); + } } } +// Renderer Functions +void Renderer::SetVSConstant4f(unsigned int const_number, float f1, float f2, float f3, float f4) +{ + pSetVSConstant4f(const_number, f1, f2, f3, f4); +} + +void Renderer::SetVSConstant4fv(unsigned int const_number, const float *f) +{ + pSetVSConstant4fv(const_number, f); +} + +void Renderer::SetMultiVSConstant4fv(unsigned int const_number, unsigned int count, const float *f) +{ + pSetMultiVSConstant4fv(const_number, count, f); +} + +void Renderer::SetMultiVSConstant3fv(unsigned int const_number, unsigned int count, const float *f) +{ + pSetMultiVSConstant3fv(const_number, count, f); +} + + } // namespace OGL diff --git a/Source/Plugins/Plugin_VideoOGL/Src/VertexShaderCache.h b/Source/Plugins/Plugin_VideoOGL/Src/VertexShaderCache.h index 6f4cbe25c2..e398a801ff 100644 --- a/Source/Plugins/Plugin_VideoOGL/Src/VertexShaderCache.h +++ b/Source/Plugins/Plugin_VideoOGL/Src/VertexShaderCache.h @@ -70,7 +70,17 @@ public: static void DisableShader(); }; +// GLSL Specific +void SetGLSLVSConstant4f(unsigned int const_number, float f1, float f2, float f3, float f4); +void SetGLSLVSConstant4fv(unsigned int const_number, const float *f); +void SetMultiGLSLVSConstant4fv(unsigned int const_number, unsigned int count, const float *f); +void SetMultiGLSLVSConstant3fv(unsigned int const_number, unsigned int count, const float *f); +// CG Specific +void SetCGVSConstant4f(unsigned int const_number, float f1, float f2, float f3, float f4); +void SetCGVSConstant4fv(unsigned int const_number, const float *f); +void SetMultiCGVSConstant4fv(unsigned int const_number, unsigned int count, const float *f); +void SetMultiCGVSConstant3fv(unsigned int const_number, unsigned int count, const float *f); } // namespace OGL #endif // _VERTEXSHADERCACHE_H_ diff --git a/Source/Plugins/Plugin_VideoOGL/Src/main.cpp b/Source/Plugins/Plugin_VideoOGL/Src/main.cpp index a25f9cd839..0d1c3fc35a 100644 --- a/Source/Plugins/Plugin_VideoOGL/Src/main.cpp +++ b/Source/Plugins/Plugin_VideoOGL/Src/main.cpp @@ -135,6 +135,7 @@ void InitBackendInfo() g_Config.backend_info.bSupportsDualSourceBlend = false; // supported, but broken g_Config.backend_info.bSupportsFormatReinterpretation = false; g_Config.backend_info.bSupportsPixelLighting = true; + g_Config.backend_info.bSupportsGLSL = true; // aamodes const char* caamodes[] = {"None", "2x", "4x", "8x", "8x CSAA", "8xQ CSAA", "16x CSAA", "16xQ CSAA"}; From f8eb45637ff64957b118127457d77e02959ca4f1 Mon Sep 17 00:00:00 2001 From: Ryan Houdek Date: Thu, 1 Dec 2011 00:33:12 -0600 Subject: [PATCH 02/83] Now CG plays nice with this new stuff. --- .../VideoCommon/Src/LightingShaderGen.cpp | 34 +++++++++---------- .../Core/VideoCommon/Src/VertexShaderGen.cpp | 32 +++++++++-------- .../Plugin_VideoOGL/Src/PixelShaderCache.cpp | 11 +++--- .../Plugin_VideoOGL/Src/VertexShaderCache.cpp | 11 +++--- 4 files changed, 42 insertions(+), 46 deletions(-) diff --git a/Source/Core/VideoCommon/Src/LightingShaderGen.cpp b/Source/Core/VideoCommon/Src/LightingShaderGen.cpp index a506acadfc..5933a7d28f 100644 --- a/Source/Core/VideoCommon/Src/LightingShaderGen.cpp +++ b/Source/Core/VideoCommon/Src/LightingShaderGen.cpp @@ -47,13 +47,13 @@ char *GenerateLightShader(char *p, int index, const LitChannel& chan, const char // atten disabled switch (chan.diffusefunc) { case LIGHTDIF_NONE: - WRITE(p, "lacc.%s += %s.lights[%d].col.%s;\n", swizzle, lightsName, index, swizzle); + WRITE(p, "lacc.%s += %s[%d].%s;\n", swizzle, lightsName, index * 5, swizzle); break; case LIGHTDIF_SIGN: case LIGHTDIF_CLAMP: - WRITE(p, "ldir = normalize(%s.lights[%d].pos.xyz - pos.xyz);\n", lightsName, index); - WRITE(p, "lacc.%s += %sdot(ldir, _norm0)) * %s.lights[%d].col.%s;\n", - swizzle, chan.diffusefunc != LIGHTDIF_SIGN ? "max(0.0f," :"(", lightsName, index, swizzle); + WRITE(p, "ldir = normalize(%s[%d + 3].xyz - pos.xyz);\n", lightsName, index * 5); + WRITE(p, "lacc.%s += %sdot(ldir, _norm0)) * %s[%d].%s;\n", + swizzle, chan.diffusefunc != LIGHTDIF_SIGN ? "max(0.0f," :"(", lightsName, index * 5, swizzle); break; default: _assert_(0); } @@ -62,32 +62,32 @@ char *GenerateLightShader(char *p, int index, const LitChannel& chan, const char if (chan.attnfunc == 3) { // spot - WRITE(p, "ldir = %s.lights[%d].pos.xyz - pos.xyz;\n", lightsName, index); + WRITE(p, "ldir = %s[%d + 3].xyz - pos.xyz;\n", lightsName, index * 5); WRITE(p, "dist2 = dot(ldir, ldir);\n" "dist = sqrt(dist2);\n" "ldir = ldir / dist;\n" - "attn = max(0.0f, dot(ldir, %s.lights[%d].dir.xyz));\n", lightsName, index); - WRITE(p, "attn = max(0.0f, dot(%s.lights[%d].cosatt.xyz, float3(1.0f, attn, attn*attn))) / dot(%s.lights[%d].distatt.xyz, float3(1.0f,dist,dist2));\n", lightsName, index, lightsName, index); + "attn = max(0.0f, dot(ldir, %s[%d + 4].xyz));\n", lightsName, index * 5); + WRITE(p, "attn = max(0.0f, dot(%s[%d + 1].xyz, float3(1.0f, attn, attn*attn))) / dot(%s[%d + 2].xyz, float3(1.0f,dist,dist2));\n", lightsName, index * 5, lightsName, index * 5); } else if (chan.attnfunc == 1) { // specular - WRITE(p, "ldir = normalize(%s.lights[%d].pos.xyz);\n", lightsName, index); - WRITE(p, "attn = (dot(_norm0,ldir) >= 0.0f) ? max(0.0f, dot(_norm0, %s.lights[%d].dir.xyz)) : 0.0f;\n", lightsName, index); - WRITE(p, "attn = max(0.0f, dot(%s.lights[%d].cosatt.xyz, float3(1,attn,attn*attn))) / dot(%s.lights[%d].distatt.xyz, float3(1,attn,attn*attn));\n", lightsName, index, lightsName, index); + WRITE(p, "ldir = normalize(%s[%d + 3].xyz);\n", lightsName, index * 5); + WRITE(p, "attn = (dot(_norm0,ldir) >= 0.0f) ? max(0.0f, dot(_norm0, %s[%d + 4].xyz)) : 0.0f;\n", lightsName, index * 5); + WRITE(p, "attn = max(0.0f, dot(%s[%d + 1].xyz, float3(1,attn,attn*attn))) / dot(%s[%d + 2].xyz, float3(1,attn,attn*attn));\n", lightsName, index * 5, lightsName, index * 5); } switch (chan.diffusefunc) { case LIGHTDIF_NONE: - WRITE(p, "lacc.%s += attn * %s.lights[%d].col.%s;\n", swizzle, lightsName, index, swizzle); + WRITE(p, "lacc.%s += attn * %s[%d].%s;\n", swizzle, lightsName, index * 5, swizzle); break; case LIGHTDIF_SIGN: case LIGHTDIF_CLAMP: - WRITE(p, "lacc.%s += attn * %sdot(ldir, _norm0)) * %s.lights[%d].col.%s;\n", + WRITE(p, "lacc.%s += attn * %sdot(ldir, _norm0)) * %s[%d].%s;\n", swizzle, chan.diffusefunc != LIGHTDIF_SIGN ? "max(0.0f," :"(", lightsName, - index, + index * 5, swizzle); break; default: _assert_(0); @@ -120,7 +120,7 @@ char *GenerateLightingShader(char *p, int components, const char* materialsName, WRITE(p, "mat = float4(1.0f, 1.0f, 1.0f, 1.0f);\n"); } else // from color - WRITE(p, "mat = %s.C%d;\n", materialsName, j+2); + WRITE(p, "mat = %s[%d];\n", materialsName, j+2); if (color.enablelighting) { if (color.ambsource) { // from vertex @@ -132,7 +132,7 @@ char *GenerateLightingShader(char *p, int components, const char* materialsName, WRITE(p, "lacc = float4(0.0f, 0.0f, 0.0f, 0.0f);\n"); } else // from color - WRITE(p, "lacc = %s.C%d;\n", materialsName, j); + WRITE(p, "lacc = %s[%d];\n", materialsName, j); } else { @@ -149,7 +149,7 @@ char *GenerateLightingShader(char *p, int components, const char* materialsName, else WRITE(p, "mat.w = 1.0f;\n"); } else // from color - WRITE(p, "mat.w = %s.C%d.w;\n", materialsName, j+2); + WRITE(p, "mat.w = %s[%d].w;\n", materialsName, j+2); } if (alpha.enablelighting) @@ -163,7 +163,7 @@ char *GenerateLightingShader(char *p, int components, const char* materialsName, WRITE(p, "lacc.w = 0.0f;\n"); } else // from color - WRITE(p, "lacc.w = %s.C%d.w;\n", materialsName, j); + WRITE(p, "lacc.w = %s[%d].w;\n", materialsName, j); } else { diff --git a/Source/Core/VideoCommon/Src/VertexShaderGen.cpp b/Source/Core/VideoCommon/Src/VertexShaderGen.cpp index fcdc0e8976..43ffdeedd8 100644 --- a/Source/Core/VideoCommon/Src/VertexShaderGen.cpp +++ b/Source/Core/VideoCommon/Src/VertexShaderGen.cpp @@ -292,11 +292,11 @@ const char *GenerateVertexShaderCode(u32 components, API_TYPE ApiType) WRITE(p, "int posmtx = fposmtx;\n"); } - WRITE(p, "float4 pos = float4(dot("I_TRANSFORMMATRICES".T[posmtx].t, rawpos), dot("I_TRANSFORMMATRICES".T[posmtx+1].t, rawpos), dot("I_TRANSFORMMATRICES".T[posmtx+2].t, rawpos), 1);\n"); + WRITE(p, "float4 pos = float4(dot("I_TRANSFORMMATRICES"[posmtx], rawpos), dot("I_TRANSFORMMATRICES"[posmtx+1], rawpos), dot("I_TRANSFORMMATRICES"[posmtx+2], rawpos), 1);\n"); if (components & VB_HAS_NRMALL) { WRITE(p, "int normidx = posmtx >= 32 ? (posmtx-32) : posmtx;\n"); - WRITE(p, "float3 N0 = "I_NORMALMATRICES".T[normidx].t.xyz, N1 = "I_NORMALMATRICES".T[normidx+1].t.xyz, N2 = "I_NORMALMATRICES".T[normidx+2].t.xyz;\n"); + WRITE(p, "float3 N0 = "I_NORMALMATRICES"[normidx].xyz, N1 = "I_NORMALMATRICES"[normidx+1].xyz, N2 = "I_NORMALMATRICES"[normidx+2].xyz;\n"); } if (components & VB_HAS_NRM0) @@ -308,13 +308,13 @@ const char *GenerateVertexShaderCode(u32 components, API_TYPE ApiType) } else { - WRITE(p, "float4 pos = float4(dot("I_POSNORMALMATRIX".T0, rawpos), dot("I_POSNORMALMATRIX".T1, rawpos), dot("I_POSNORMALMATRIX".T2, rawpos), 1.0f);\n"); + WRITE(p, "float4 pos = float4(dot("I_POSNORMALMATRIX"[0], rawpos), dot("I_POSNORMALMATRIX"[1], rawpos), dot("I_POSNORMALMATRIX"[2], rawpos), 1.0f);\n"); if (components & VB_HAS_NRM0) - WRITE(p, "float3 _norm0 = normalize(float3(dot("I_POSNORMALMATRIX".N0.xyz, rawnorm0), dot("I_POSNORMALMATRIX".N1.xyz, rawnorm0), dot("I_POSNORMALMATRIX".N2.xyz, rawnorm0)));\n"); + WRITE(p, "float3 _norm0 = normalize(float3(dot("I_POSNORMALMATRIX"[3].xyz, rawnorm0), dot("I_POSNORMALMATRIX"[4].xyz, rawnorm0), dot("I_POSNORMALMATRIX"[5].xyz, rawnorm0)));\n"); if (components & VB_HAS_NRM1) - WRITE(p, "float3 _norm1 = float3(dot("I_POSNORMALMATRIX".N0.xyz, rawnorm1), dot("I_POSNORMALMATRIX".N1.xyz, rawnorm1), dot("I_POSNORMALMATRIX".N2.xyz, rawnorm1));\n"); + WRITE(p, "float3 _norm1 = float3(dot("I_POSNORMALMATRIX"[3].xyz, rawnorm1), dot("I_POSNORMALMATRIX"[4].xyz, rawnorm1), dot("I_POSNORMALMATRIX"[5].xyz, rawnorm1));\n"); if (components & VB_HAS_NRM2) - WRITE(p, "float3 _norm2 = float3(dot("I_POSNORMALMATRIX".N0.xyz, rawnorm2), dot("I_POSNORMALMATRIX".N1.xyz, rawnorm2), dot("I_POSNORMALMATRIX".N2.xyz, rawnorm2));\n"); + WRITE(p, "float3 _norm2 = float3(dot("I_POSNORMALMATRIX".[3].xyz, rawnorm2), dot("I_POSNORMALMATRIX"[4].xyz, rawnorm2), dot("I_POSNORMALMATRIX"[5].xyz, rawnorm2));\n"); } if (!(components & VB_HAS_NRM0)) @@ -322,7 +322,7 @@ const char *GenerateVertexShaderCode(u32 components, API_TYPE ApiType) - WRITE(p, "o.pos = float4(dot("I_PROJECTION".T0, pos), dot("I_PROJECTION".T1, pos), dot("I_PROJECTION".T2, pos), dot("I_PROJECTION".T3, pos));\n"); + WRITE(p, "o.pos = float4(dot("I_PROJECTION"[0], pos), dot("I_PROJECTION"[1], pos), dot("I_PROJECTION"[2], pos), dot("I_PROJECTION"[3], pos));\n"); WRITE(p, "float4 mat, lacc;\n" "float3 ldir, h;\n" @@ -421,18 +421,20 @@ const char *GenerateVertexShaderCode(u32 components, API_TYPE ApiType) break; case XF_TEXGEN_REGULAR: default: - if (components & (VB_HAS_TEXMTXIDX0< Date: Fri, 2 Dec 2011 18:26:15 -0600 Subject: [PATCH 03/83] meh --- .../Plugin_VideoOGL/Src/PixelShaderCache.cpp | 166 ++++++++++-------- .../Plugin_VideoOGL/Src/PixelShaderCache.h | 2 + .../Plugin_VideoOGL/Src/VertexShaderCache.cpp | 131 +++++++------- .../Plugin_VideoOGL/Src/VertexShaderCache.h | 2 + 4 files changed, 164 insertions(+), 137 deletions(-) diff --git a/Source/Plugins/Plugin_VideoOGL/Src/PixelShaderCache.cpp b/Source/Plugins/Plugin_VideoOGL/Src/PixelShaderCache.cpp index d6acf10588..c4162b54a0 100644 --- a/Source/Plugins/Plugin_VideoOGL/Src/PixelShaderCache.cpp +++ b/Source/Plugins/Plugin_VideoOGL/Src/PixelShaderCache.cpp @@ -51,6 +51,7 @@ PIXELSHADERUID PixelShaderCache::last_uid; void(*pSetPSConstant4f)(unsigned int, float, float, float, float); void(*pSetPSConstant4fv)(unsigned int, const float*); void(*pSetMultiPSConstant4fv)(unsigned int, unsigned int, const float*); +bool (*pCompilePixelShader)(FRAGMENTSHADER&, const char*); GLuint PixelShaderCache::GetDepthMatrixProgram() { @@ -77,12 +78,14 @@ void PixelShaderCache::Init() pSetPSConstant4f = SetGLSLPSConstant4f; pSetPSConstant4fv = SetGLSLPSConstant4fv; pSetMultiPSConstant4fv = SetMultiGLSLPSConstant4fv; + pCompilePixelShader = CompileGLSLPixelShader; } else { pSetPSConstant4f = SetCGPSConstant4f; pSetPSConstant4fv = SetCGPSConstant4fv; pSetMultiPSConstant4fv = SetMultiCGPSConstant4fv; + pCompilePixelShader = CompileCGPixelShader; } glGetProgramivARB(GL_FRAGMENT_PROGRAM_ARB, GL_MAX_PROGRAM_NATIVE_ALU_INSTRUCTIONS_ARB, (GLint *)&s_nMaxPixelInstructions); @@ -262,83 +265,7 @@ FRAGMENTSHADER* PixelShaderCache::SetShader(DSTALPHA_MODE dstAlphaMode, u32 comp bool PixelShaderCache::CompilePixelShader(FRAGMENTSHADER& ps, const char* pstrprogram) { - GLenum err = GL_REPORT_ERROR(); - if (err != GL_NO_ERROR) - { - ERROR_LOG(VIDEO, "glError %08x before PS!", err); - } - -#if defined HAVE_CG && HAVE_CG - CGprogram tempprog = cgCreateProgram(g_cgcontext, CG_SOURCE, pstrprogram, g_cgfProf, "main", NULL); - - // handle errors - if (!cgIsProgram(tempprog)) - { - cgDestroyProgram(tempprog); - - static int num_failures = 0; - char szTemp[MAX_PATH]; - sprintf(szTemp, "bad_ps_%04i.txt", num_failures++); - std::ofstream file(szTemp); - file << pstrprogram; - file.close(); - - PanicAlert("Failed to compile pixel shader %d!\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 (%d):\n%s", - num_failures - 1, szTemp, - g_cgfProf, - cgGetLastListing(g_cgcontext)); - - return false; - } - - // handle warnings - if (cgGetError() != CG_NO_ERROR) - { - WARN_LOG(VIDEO, "Warnings on compile ps %s:", cgGetLastListing(g_cgcontext)); - WARN_LOG(VIDEO, "%s", pstrprogram); - } - - // This looks evil - we modify the program through the const char * we got from cgGetProgramString! - // It SHOULD not have any nasty side effects though - but you never know... - char *pcompiledprog = (char*)cgGetProgramString(tempprog, CG_COMPILED_PROGRAM); - char *plocal = strstr(pcompiledprog, "program.local"); - while (plocal != NULL) - { - const char *penv = " program.env"; - memcpy(plocal, penv, 13); - plocal = strstr(plocal+13, "program.local"); - } - - glGenProgramsARB(1, &ps.glprogid); - SetCurrentShader(ps.glprogid); - glProgramStringARB(GL_FRAGMENT_PROGRAM_ARB, GL_PROGRAM_FORMAT_ASCII_ARB, (GLsizei)strlen(pcompiledprog), pcompiledprog); - - err = GL_REPORT_ERROR(); - if (err != GL_NO_ERROR) - { - GLint error_pos, native_limit; - glGetIntegerv(GL_PROGRAM_ERROR_POSITION_ARB, &error_pos); - glGetProgramivARB(GL_FRAGMENT_PROGRAM_ARB, GL_PROGRAM_UNDER_NATIVE_LIMITS_ARB, &native_limit); - // Error occur - if (error_pos != -1) { - const char *program_error = (const char *)glGetString(GL_PROGRAM_ERROR_STRING_ARB); - char line[256]; - strncpy(line, (const char *)pcompiledprog + error_pos, 255); - line[255] = 0; - ERROR_LOG(VIDEO, "Error at %i: %s", error_pos, program_error); - ERROR_LOG(VIDEO, "Line dump: \n%s", line); - } else if (native_limit != -1) { - ERROR_LOG(VIDEO, "Hit limit? %i", native_limit); - // TODO - } - ERROR_LOG(VIDEO, "%s", pstrprogram); - ERROR_LOG(VIDEO, "%s", pcompiledprog); - } - - cgDestroyProgram(tempprog); -#endif - - return true; + return pCompilePixelShader(ps, pstrprogram); } //Disable Fragment programs and reset the selected Program @@ -367,6 +294,10 @@ void PixelShaderCache::SetCurrentShader(GLuint Shader) } } // GLSL Specific +bool CompileGLSLPixelShader(FRAGMENTSHADER& ps, const char* pstrprogram) +{ + return false; +} void PixelShaderCache::SetPSSampler(const char * name, unsigned int Tex) { PROGRAMSHADER tmp = ProgramShaderCache::GetShaderProgram(); @@ -437,6 +368,87 @@ void SetMultiGLSLPSConstant4fv(unsigned int const_number, unsigned int count, co } } // CG Specific + +bool CompileCGPixelShader(FRAGMENTSHADER& ps, const char* pstrprogram) +{ + GLenum err = GL_REPORT_ERROR(); + if (err != GL_NO_ERROR) + { + ERROR_LOG(VIDEO, "glError %08x before PS!", err); + } + +#if defined HAVE_CG && HAVE_CG + CGprogram tempprog = cgCreateProgram(g_cgcontext, CG_SOURCE, pstrprogram, g_cgfProf, "main", NULL); + + // handle errors + if (!cgIsProgram(tempprog)) + { + cgDestroyProgram(tempprog); + + static int num_failures = 0; + char szTemp[MAX_PATH]; + sprintf(szTemp, "bad_ps_%04i.txt", num_failures++); + std::ofstream file(szTemp); + file << pstrprogram; + file.close(); + + PanicAlert("Failed to compile pixel shader %d!\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 (%d):\n%s", + num_failures - 1, szTemp, + g_cgfProf, + cgGetLastListing(g_cgcontext)); + + return false; + } + + // handle warnings + if (cgGetError() != CG_NO_ERROR) + { + WARN_LOG(VIDEO, "Warnings on compile ps %s:", cgGetLastListing(g_cgcontext)); + WARN_LOG(VIDEO, "%s", pstrprogram); + } + + // This looks evil - we modify the program through the const char * we got from cgGetProgramString! + // It SHOULD not have any nasty side effects though - but you never know... + char *pcompiledprog = (char*)cgGetProgramString(tempprog, CG_COMPILED_PROGRAM); + char *plocal = strstr(pcompiledprog, "program.local"); + while (plocal != NULL) + { + const char *penv = " program.env"; + memcpy(plocal, penv, 13); + plocal = strstr(plocal+13, "program.local"); + } + + glGenProgramsARB(1, &ps.glprogid); + PixelShaderCache::SetCurrentShader(ps.glprogid); + glProgramStringARB(GL_FRAGMENT_PROGRAM_ARB, GL_PROGRAM_FORMAT_ASCII_ARB, (GLsizei)strlen(pcompiledprog), pcompiledprog); + + err = GL_REPORT_ERROR(); + if (err != GL_NO_ERROR) + { + GLint error_pos, native_limit; + glGetIntegerv(GL_PROGRAM_ERROR_POSITION_ARB, &error_pos); + glGetProgramivARB(GL_FRAGMENT_PROGRAM_ARB, GL_PROGRAM_UNDER_NATIVE_LIMITS_ARB, &native_limit); + // Error occur + if (error_pos != -1) { + const char *program_error = (const char *)glGetString(GL_PROGRAM_ERROR_STRING_ARB); + char line[256]; + strncpy(line, (const char *)pcompiledprog + error_pos, 255); + line[255] = 0; + ERROR_LOG(VIDEO, "Error at %i: %s", error_pos, program_error); + ERROR_LOG(VIDEO, "Line dump: \n%s", line); + } else if (native_limit != -1) { + ERROR_LOG(VIDEO, "Hit limit? %i", native_limit); + // TODO + } + ERROR_LOG(VIDEO, "%s", pstrprogram); + ERROR_LOG(VIDEO, "%s", pcompiledprog); + } + + cgDestroyProgram(tempprog); +#endif + + return true; +} void SetCGPSConstant4f(unsigned int const_number, float f1, float f2, float f3, float f4) { float f[4] = { f1, f2, f3, f4 }; diff --git a/Source/Plugins/Plugin_VideoOGL/Src/PixelShaderCache.h b/Source/Plugins/Plugin_VideoOGL/Src/PixelShaderCache.h index 9d61d7229a..429af249f1 100644 --- a/Source/Plugins/Plugin_VideoOGL/Src/PixelShaderCache.h +++ b/Source/Plugins/Plugin_VideoOGL/Src/PixelShaderCache.h @@ -91,11 +91,13 @@ public: void SetGLSLPSConstant4f(unsigned int const_number, float f1, float f2, float f3, float f4); void SetGLSLPSConstant4fv(unsigned int const_number, const float *f); void SetMultiGLSLPSConstant4fv(unsigned int const_number, unsigned int count, const float *f); +bool CompileGLSLPixelShader(FRAGMENTSHADER& ps, const char* pstrprogram); //CG Specific void SetCGPSConstant4f(unsigned int const_number, float f1, float f2, float f3, float f4); void SetCGPSConstant4fv(unsigned int const_number, const float *f); void SetMultiCGPSConstant4fv(unsigned int const_number, unsigned int count, const float *f); +bool CompileCGPixelShader(FRAGMENTSHADER& ps, const char* pstrprogram); } // namespace OGL #endif // _PIXELSHADERCACHE_H_ diff --git a/Source/Plugins/Plugin_VideoOGL/Src/VertexShaderCache.cpp b/Source/Plugins/Plugin_VideoOGL/Src/VertexShaderCache.cpp index 82db873e85..eb4f56522e 100644 --- a/Source/Plugins/Plugin_VideoOGL/Src/VertexShaderCache.cpp +++ b/Source/Plugins/Plugin_VideoOGL/Src/VertexShaderCache.cpp @@ -50,6 +50,7 @@ void (*pSetVSConstant4f)(unsigned int, float, float, float, float); void (*pSetVSConstant4fv)(unsigned int, const float*); void (*pSetMultiVSConstant4fv)(unsigned int, unsigned int, const float*); void (*pSetMultiVSConstant3fv)(unsigned int, unsigned int, const float*); +bool (*pCompileVertexShader)(VERTEXSHADER&, const char*); void VertexShaderCache::Init() { @@ -64,6 +65,7 @@ void VertexShaderCache::Init() pSetVSConstant4fv = SetGLSLVSConstant4fv; pSetMultiVSConstant4fv = SetMultiGLSLVSConstant4fv; pSetMultiVSConstant3fv = SetMultiGLSLVSConstant3fv; + pCompileVertexShader = CompileGLSLVertexShader; } else { @@ -71,6 +73,7 @@ void VertexShaderCache::Init() pSetVSConstant4fv = SetCGVSConstant4fv; pSetMultiVSConstant4fv = SetMultiCGVSConstant4fv; pSetMultiVSConstant3fv = SetMultiCGVSConstant3fv; + pCompileVertexShader = CompileGLSLVertexShader; } glGetProgramivARB(GL_VERTEX_PROGRAM_ARB, GL_MAX_PROGRAM_NATIVE_INSTRUCTIONS_ARB, (GLint *)&s_nMaxVertexInstructions); @@ -147,66 +150,7 @@ VERTEXSHADER* VertexShaderCache::SetShader(u32 components) bool VertexShaderCache::CompileVertexShader(VERTEXSHADER& vs, const char* pstrprogram) { - // Reset GL error before compiling shaders. Yeah, we need to investigate the causes of these. - GLenum err = GL_REPORT_ERROR(); - if (err != GL_NO_ERROR) - { - ERROR_LOG(VIDEO, "glError %08x before VS!", err); - } - -#if defined HAVE_CG && HAVE_CG - CGprogram tempprog = cgCreateProgram(g_cgcontext, CG_SOURCE, pstrprogram, g_cgvProf, "main", NULL); - if (!cgIsProgram(tempprog)) { - static int num_failures = 0; - char szTemp[MAX_PATH]; - sprintf(szTemp, "bad_vs_%04i.txt", num_failures++); - std::ofstream file(szTemp); - file << pstrprogram; - file.close(); - - PanicAlert("Failed to compile vertex shader %d!\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 (%d):\n%s", - num_failures - 1, szTemp, - g_cgfProf, - cgGetLastListing(g_cgcontext)); - - cgDestroyProgram(tempprog); - ERROR_LOG(VIDEO, "Failed to load vs %s:", cgGetLastListing(g_cgcontext)); - ERROR_LOG(VIDEO, "%s", pstrprogram); - return false; - } - - if (cgGetError() != CG_NO_ERROR) - { - WARN_LOG(VIDEO, "Failed to load vs %s:", cgGetLastListing(g_cgcontext)); - WARN_LOG(VIDEO, "%s", pstrprogram); - } - - // This looks evil - we modify the program through the const char * we got from cgGetProgramString! - // It SHOULD not have any nasty side effects though - but you never know... - char *pcompiledprog = (char*)cgGetProgramString(tempprog, CG_COMPILED_PROGRAM); - char *plocal = strstr(pcompiledprog, "program.local"); - while (plocal != NULL) { - const char* penv = " program.env"; - memcpy(plocal, penv, 13); - plocal = strstr(plocal + 13, "program.local"); - } - glGenProgramsARB(1, &vs.glprogid); - SetCurrentShader(vs.glprogid); - - glProgramStringARB(GL_VERTEX_PROGRAM_ARB, GL_PROGRAM_FORMAT_ASCII_ARB, (GLsizei)strlen(pcompiledprog), pcompiledprog); - err = GL_REPORT_ERROR(); - if (err != GL_NO_ERROR) { - ERROR_LOG(VIDEO, "%s", pstrprogram); - ERROR_LOG(VIDEO, "%s", pcompiledprog); - } - - cgDestroyProgram(tempprog); -#endif - - if (g_ActiveConfig.bEnableShaderDebugging) - vs.strprog = pstrprogram; - - return true; + return pCompileVertexShader(vs, pstrprogram); } void VertexShaderCache::DisableShader() @@ -234,6 +178,10 @@ void VertexShaderCache::SetCurrentShader(GLuint Shader) } } // GLSL Specific +bool CompileGLSLVertexShader(VERTEXSHADER& vs, const char* pstrprogram) +{ + return false; +} void SetVSConstant4fvByName(const char * name, unsigned int offset, const float *f, const unsigned int count = 1) { PROGRAMSHADER tmp = ProgramShaderCache::GetShaderProgram(); @@ -315,6 +263,69 @@ void SetMultiGLSLVSConstant3fv(unsigned int const_number, unsigned int count, co } // CG Specific +bool CompileCGVertexShader(VERTEXSHADER& vs, const char* pstrprogram) +{ + // Reset GL error before compiling shaders. Yeah, we need to investigate the causes of these. + GLenum err = GL_REPORT_ERROR(); + if (err != GL_NO_ERROR) + { + ERROR_LOG(VIDEO, "glError %08x before VS!", err); + } + +#if defined HAVE_CG && HAVE_CG + CGprogram tempprog = cgCreateProgram(g_cgcontext, CG_SOURCE, pstrprogram, g_cgvProf, "main", NULL); + if (!cgIsProgram(tempprog)) { + static int num_failures = 0; + char szTemp[MAX_PATH]; + sprintf(szTemp, "bad_vs_%04i.txt", num_failures++); + std::ofstream file(szTemp); + file << pstrprogram; + file.close(); + + PanicAlert("Failed to compile vertex shader %d!\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 (%d):\n%s", + num_failures - 1, szTemp, + g_cgfProf, + cgGetLastListing(g_cgcontext)); + + cgDestroyProgram(tempprog); + ERROR_LOG(VIDEO, "Failed to load vs %s:", cgGetLastListing(g_cgcontext)); + ERROR_LOG(VIDEO, "%s", pstrprogram); + return false; + } + + if (cgGetError() != CG_NO_ERROR) + { + WARN_LOG(VIDEO, "Failed to load vs %s:", cgGetLastListing(g_cgcontext)); + WARN_LOG(VIDEO, "%s", pstrprogram); + } + + // This looks evil - we modify the program through the const char * we got from cgGetProgramString! + // It SHOULD not have any nasty side effects though - but you never know... + char *pcompiledprog = (char*)cgGetProgramString(tempprog, CG_COMPILED_PROGRAM); + char *plocal = strstr(pcompiledprog, "program.local"); + while (plocal != NULL) { + const char* penv = " program.env"; + memcpy(plocal, penv, 13); + plocal = strstr(plocal + 13, "program.local"); + } + glGenProgramsARB(1, &vs.glprogid); + VertexShaderCache::SetCurrentShader(vs.glprogid); + + glProgramStringARB(GL_VERTEX_PROGRAM_ARB, GL_PROGRAM_FORMAT_ASCII_ARB, (GLsizei)strlen(pcompiledprog), pcompiledprog); + err = GL_REPORT_ERROR(); + if (err != GL_NO_ERROR) { + ERROR_LOG(VIDEO, "%s", pstrprogram); + ERROR_LOG(VIDEO, "%s", pcompiledprog); + } + + cgDestroyProgram(tempprog); +#endif + + if (g_ActiveConfig.bEnableShaderDebugging) + vs.strprog = pstrprogram; + + return true; +} void SetCGVSConstant4f(unsigned int const_number, float f1, float f2, float f3, float f4) { glProgramEnvParameter4fARB(GL_VERTEX_PROGRAM_ARB, const_number, f1, f2, f3, f4); diff --git a/Source/Plugins/Plugin_VideoOGL/Src/VertexShaderCache.h b/Source/Plugins/Plugin_VideoOGL/Src/VertexShaderCache.h index e398a801ff..fc08cc4efd 100644 --- a/Source/Plugins/Plugin_VideoOGL/Src/VertexShaderCache.h +++ b/Source/Plugins/Plugin_VideoOGL/Src/VertexShaderCache.h @@ -75,12 +75,14 @@ void SetGLSLVSConstant4f(unsigned int const_number, float f1, float f2, float f3 void SetGLSLVSConstant4fv(unsigned int const_number, const float *f); void SetMultiGLSLVSConstant4fv(unsigned int const_number, unsigned int count, const float *f); void SetMultiGLSLVSConstant3fv(unsigned int const_number, unsigned int count, const float *f); +bool CompileGLSLVertexShader(VERTEXSHADER& vs, const char* pstrprogram); // CG Specific void SetCGVSConstant4f(unsigned int const_number, float f1, float f2, float f3, float f4); void SetCGVSConstant4fv(unsigned int const_number, const float *f); void SetMultiCGVSConstant4fv(unsigned int const_number, unsigned int count, const float *f); void SetMultiCGVSConstant3fv(unsigned int const_number, unsigned int count, const float *f); +bool CompileCGVertexShader(VERTEXSHADER& vs, const char* pstrprogram); } // namespace OGL #endif // _VERTEXSHADERCACHE_H_ From 6882e00d5e39b2cfa92975a51f717a19bc9319c9 Mon Sep 17 00:00:00 2001 From: Ryan Houdek Date: Fri, 2 Dec 2011 18:31:06 -0600 Subject: [PATCH 04/83] Compile --- .../Plugin_VideoOGL/Src/PixelShaderCache.cpp | 31 ++++++++++++++++++- .../Plugin_VideoOGL/Src/VertexShaderCache.cpp | 31 ++++++++++++++++++- 2 files changed, 60 insertions(+), 2 deletions(-) diff --git a/Source/Plugins/Plugin_VideoOGL/Src/PixelShaderCache.cpp b/Source/Plugins/Plugin_VideoOGL/Src/PixelShaderCache.cpp index c4162b54a0..36a8f054f5 100644 --- a/Source/Plugins/Plugin_VideoOGL/Src/PixelShaderCache.cpp +++ b/Source/Plugins/Plugin_VideoOGL/Src/PixelShaderCache.cpp @@ -296,7 +296,36 @@ void PixelShaderCache::SetCurrentShader(GLuint Shader) // GLSL Specific bool CompileGLSLPixelShader(FRAGMENTSHADER& ps, const char* pstrprogram) { - return false; + GLuint result = glCreateShader(GL_FRAGMENT_SHADER); + + glShaderSource(result, 1, &pstrprogram, NULL); + glCompileShader(result); + GLsizei length = 0; + + glGetShaderiv(result, GL_INFO_LOG_LENGTH, &length); + if (length > 0) + { + GLsizei charsWritten; + GLchar* infoLog = new GLchar[length]; + glGetShaderInfoLog(result, length, &charsWritten, infoLog); + WARN_LOG(VIDEO, "Shader info log:\n%s", infoLog); + delete[] infoLog; + } + + GLint compileStatus; + glGetShaderiv(result, GL_COMPILE_STATUS, &compileStatus); + if (compileStatus != GL_TRUE) + { + // Compile failed + ERROR_LOG(VIDEO, "Shader compilation failed; see info log"); + // Don't try to use this shader + glDeleteShader(result); + return false; + } + + (void)GL_REPORT_ERROR(); + ps.glprogid = result; + return true; } void PixelShaderCache::SetPSSampler(const char * name, unsigned int Tex) { diff --git a/Source/Plugins/Plugin_VideoOGL/Src/VertexShaderCache.cpp b/Source/Plugins/Plugin_VideoOGL/Src/VertexShaderCache.cpp index eb4f56522e..f58e9eb786 100644 --- a/Source/Plugins/Plugin_VideoOGL/Src/VertexShaderCache.cpp +++ b/Source/Plugins/Plugin_VideoOGL/Src/VertexShaderCache.cpp @@ -180,7 +180,36 @@ void VertexShaderCache::SetCurrentShader(GLuint Shader) // GLSL Specific bool CompileGLSLVertexShader(VERTEXSHADER& vs, const char* pstrprogram) { - return false; + GLuint result = glCreateShader(GL_VERTEX_SHADER); + + glShaderSource(result, 1, &pstrprogram, NULL); + glCompileShader(result); + GLsizei length = 0; + + glGetShaderiv(result, GL_INFO_LOG_LENGTH, &length); + if (length > 0) + { + GLsizei charsWritten; + GLchar* infoLog = new GLchar[length]; + glGetShaderInfoLog(result, length, &charsWritten, infoLog); + WARN_LOG(VIDEO, "Shader info log:\n%s", infoLog); + delete[] infoLog; + } + + GLint compileStatus; + glGetShaderiv(result, GL_COMPILE_STATUS, &compileStatus); + if (compileStatus != GL_TRUE) + { + // Compile failed + ERROR_LOG(VIDEO, "Shader compilation failed; see info log"); + // Don't try to use this shader + glDeleteShader(result); + return false; + } + + (void)GL_REPORT_ERROR(); + vs.glprogid = result; + return true; } void SetVSConstant4fvByName(const char * name, unsigned int offset, const float *f, const unsigned int count = 1) { From 8a18a110b7bda3c244a8478010b71f951cfffdc6 Mon Sep 17 00:00:00 2001 From: Ryan Houdek Date: Fri, 2 Dec 2011 18:46:07 -0600 Subject: [PATCH 05/83] mah --- .../Plugin_VideoOGL/Src/PixelShaderCache.cpp | 8 +++++++- .../Plugin_VideoOGL/Src/PixelShaderCache.h | 9 +++++++-- .../Plugin_VideoOGL/Src/VertexShaderCache.cpp | 10 ++++++++-- .../Plugin_VideoOGL/Src/VertexShaderCache.h | 15 +++++++++++---- 4 files changed, 33 insertions(+), 9 deletions(-) diff --git a/Source/Plugins/Plugin_VideoOGL/Src/PixelShaderCache.cpp b/Source/Plugins/Plugin_VideoOGL/Src/PixelShaderCache.cpp index 36a8f054f5..32e6fb3374 100644 --- a/Source/Plugins/Plugin_VideoOGL/Src/PixelShaderCache.cpp +++ b/Source/Plugins/Plugin_VideoOGL/Src/PixelShaderCache.cpp @@ -20,6 +20,7 @@ #include "GLUtil.h" #include +#include #include "Statistics.h" #include "VideoConfig.h" @@ -234,7 +235,8 @@ FRAGMENTSHADER* PixelShaderCache::SetShader(DSTALPHA_MODE dstAlphaMode, u32 comp // Make an entry in the table PSCacheEntry& newentry = PixelShaders[uid]; last_entry = &newentry; - const char *code = GeneratePixelShaderCode(dstAlphaMode, API_OPENGL, components); + newentry.shader.bGLSL = g_ActiveConfig.bUseGLSL; + const char *code = GeneratePixelShaderCode(dstAlphaMode, g_ActiveConfig.bUseGLSL ? API_GLSL : API_OPENGL, components); if (g_ActiveConfig.bEnableShaderDebugging && code) { @@ -271,6 +273,8 @@ bool PixelShaderCache::CompilePixelShader(FRAGMENTSHADER& ps, const char* pstrpr //Disable Fragment programs and reset the selected Program void PixelShaderCache::DisableShader() { + if(g_ActiveConfig.bUseGLSL) + assert(true); if(ShaderEnabled) { glDisable(GL_FRAGMENT_PROGRAM_ARB); @@ -281,6 +285,8 @@ void PixelShaderCache::DisableShader() //bind a program if is diferent from the binded oone void PixelShaderCache::SetCurrentShader(GLuint Shader) { + if(g_ActiveConfig.bUseGLSL) + assert(true); if(!ShaderEnabled) { glEnable(GL_FRAGMENT_PROGRAM_ARB); diff --git a/Source/Plugins/Plugin_VideoOGL/Src/PixelShaderCache.h b/Source/Plugins/Plugin_VideoOGL/Src/PixelShaderCache.h index 429af249f1..d5e2cae6bc 100644 --- a/Source/Plugins/Plugin_VideoOGL/Src/PixelShaderCache.h +++ b/Source/Plugins/Plugin_VideoOGL/Src/PixelShaderCache.h @@ -29,16 +29,21 @@ namespace OGL struct FRAGMENTSHADER { - FRAGMENTSHADER() : glprogid(0) { } + FRAGMENTSHADER() : glprogid(0), bGLSL(0) { } void Destroy() { if (glprogid) { - glDeleteProgramsARB(1, &glprogid); + if(bGLSL) + glDeleteShader(glprogid); + else + glDeleteProgramsARB(1, &glprogid); glprogid = 0; } } GLuint glprogid; // opengl program id + + bool bGLSL; std::string strprog; }; diff --git a/Source/Plugins/Plugin_VideoOGL/Src/VertexShaderCache.cpp b/Source/Plugins/Plugin_VideoOGL/Src/VertexShaderCache.cpp index f58e9eb786..ea7a97196b 100644 --- a/Source/Plugins/Plugin_VideoOGL/Src/VertexShaderCache.cpp +++ b/Source/Plugins/Plugin_VideoOGL/Src/VertexShaderCache.cpp @@ -16,6 +16,7 @@ // http://code.google.com/p/dolphin-emu/ #include +#include #include "Globals.h" #include "VideoConfig.h" @@ -73,7 +74,7 @@ void VertexShaderCache::Init() pSetVSConstant4fv = SetCGVSConstant4fv; pSetMultiVSConstant4fv = SetMultiCGVSConstant4fv; pSetMultiVSConstant3fv = SetMultiCGVSConstant3fv; - pCompileVertexShader = CompileGLSLVertexShader; + pCompileVertexShader = CompileCGVertexShader; } glGetProgramivARB(GL_VERTEX_PROGRAM_ARB, GL_MAX_PROGRAM_NATIVE_INSTRUCTIONS_ARB, (GLint *)&s_nMaxVertexInstructions); @@ -124,7 +125,8 @@ VERTEXSHADER* VertexShaderCache::SetShader(u32 components) // Make an entry in the table VSCacheEntry& entry = vshaders[uid]; last_entry = &entry; - const char *code = GenerateVertexShaderCode(components, API_OPENGL); + entry.shader.bGLSL = g_ActiveConfig.bUseGLSL; + const char *code = GenerateVertexShaderCode(components, g_ActiveConfig.bUseGLSL ? API_GLSL : API_OPENGL); GetSafeVertexShaderId(&entry.safe_uid, components); #if defined(_DEBUG) || defined(DEBUGFAST) @@ -155,6 +157,8 @@ bool VertexShaderCache::CompileVertexShader(VERTEXSHADER& vs, const char* pstrpr void VertexShaderCache::DisableShader() { + if(g_ActiveConfig.bUseGLSL) + assert(true); if (ShaderEnabled) { glDisable(GL_VERTEX_PROGRAM_ARB); @@ -165,6 +169,8 @@ void VertexShaderCache::DisableShader() void VertexShaderCache::SetCurrentShader(GLuint Shader) { + if(g_ActiveConfig.bUseGLSL) + assert(true); if (!ShaderEnabled) { glEnable(GL_VERTEX_PROGRAM_ARB); diff --git a/Source/Plugins/Plugin_VideoOGL/Src/VertexShaderCache.h b/Source/Plugins/Plugin_VideoOGL/Src/VertexShaderCache.h index fc08cc4efd..c3a6146b53 100644 --- a/Source/Plugins/Plugin_VideoOGL/Src/VertexShaderCache.h +++ b/Source/Plugins/Plugin_VideoOGL/Src/VertexShaderCache.h @@ -29,9 +29,18 @@ namespace OGL struct VERTEXSHADER { - VERTEXSHADER() : glprogid(0) {} + VERTEXSHADER() : glprogid(0), bGLSL(0) {} + void Destroy() + { + if(bGLSL) + glDeleteShader(glprogid); + else + glDeleteProgramsARB(1, &glprogid); + glprogid = 0; + } GLuint glprogid; // opengl program id + bool bGLSL; std::string strprog; }; @@ -43,9 +52,7 @@ class VertexShaderCache VERTEXSHADERUIDSAFE safe_uid; VSCacheEntry() {} void Destroy() { - // printf("Destroying vs %i\n", shader.glprogid); - glDeleteProgramsARB(1, &shader.glprogid); - shader.glprogid = 0; + shader.Destroy(); } }; From ae6ac5b4399a5032cf185b7f695896859799af99 Mon Sep 17 00:00:00 2001 From: Ryan Houdek Date: Fri, 2 Dec 2011 19:04:37 -0600 Subject: [PATCH 06/83] moe --- Source/Core/Core/Src/PowerPC/Jit64/Jit.cpp | 8 - .../Src/TextureConversionShader.cpp | 150 +++++++++++++----- 2 files changed, 106 insertions(+), 52 deletions(-) diff --git a/Source/Core/Core/Src/PowerPC/Jit64/Jit.cpp b/Source/Core/Core/Src/PowerPC/Jit64/Jit.cpp index f9fd3c4635..099d7db8d2 100644 --- a/Source/Core/Core/Src/PowerPC/Jit64/Jit.cpp +++ b/Source/Core/Core/Src/PowerPC/Jit64/Jit.cpp @@ -585,14 +585,6 @@ const u8* Jit64::DoJit(u32 em_address, PPCAnalyst::CodeBuffer *code_buf, JitBloc } } -#if defined(_DEBUG) || defined(DEBUGFAST) - if (gpr.SanityCheck() || fpr.SanityCheck()) - { - char ppcInst[256]; - DisassembleGekko(ops[i].inst.hex, em_address, ppcInst, 256); - NOTICE_LOG(DYNA_REC, "Unflushed reg: %s", ppcInst); - } -#endif if (js.skipnext) { js.skipnext = false; diff --git a/Source/Core/VideoCommon/Src/TextureConversionShader.cpp b/Source/Core/VideoCommon/Src/TextureConversionShader.cpp index c921a1a9fd..ce4a5ca1f4 100644 --- a/Source/Core/VideoCommon/Src/TextureConversionShader.cpp +++ b/Source/Core/VideoCommon/Src/TextureConversionShader.cpp @@ -65,21 +65,35 @@ u16 GetEncodedSampleCount(u32 format) default: return 1; } } +const char* WriteRegister(API_TYPE ApiType, const char *prefix, const u32 num) +{ + if(ApiType == API_GLSL) + return ""; // Once we switch to GLSL 1.3 we can do something here + static char result[64]; + sprintf(result, " : register(%s%d)", prefix, num); + return result; +} // block dimensions : widthStride, heightStride // texture dims : width, height, x offset, y offset void WriteSwizzler(char*& p, u32 format, API_TYPE ApiType) { - WRITE(p, "uniform float4 blkDims : register(c%d);\n", C_COLORMATRIX); - WRITE(p, "uniform float4 textureDims : register(c%d);\n", C_COLORMATRIX + 1); + // [0] left, top, right, bottom of source rectangle within source texture + // [1] width and height of destination texture in pixels + // Two were merged for GLSL + WRITE(p, "uniform float4 "I_COLORS"[2] %s;\n", WriteRegister(ApiType, "c", C_COLORS)); float blkW = (float)TexDecoder_GetBlockWidthInTexels(format); float blkH = (float)TexDecoder_GetBlockHeightInTexels(format); float samples = (float)GetEncodedSampleCount(format); - if(ApiType == API_OPENGL) + if (ApiType == API_OPENGL) { WRITE(p,"uniform samplerRECT samp0 : register(s0);\n"); } + else if (ApiType == API_GLSL) + { + WRITE(p, "uniform sampler2DRect samp0;\n"); + } else if (ApiType & API_D3D9) { WRITE(p,"uniform sampler samp0 : register(s0);\n"); @@ -91,14 +105,24 @@ void WriteSwizzler(char*& p, u32 format, API_TYPE ApiType) } - WRITE(p,"void main(\n"); - if(ApiType != API_D3D11) + if(ApiType == API_GLSL) { - WRITE(p," out float4 ocol0 : COLOR0,\n"); + WRITE(p, " float4 ocol0;\n"); + WRITE(p, " float2 uv0 = gl_TexCoord[0].xy;\n"); + WRITE(p, "void main()\n"); } else { - WRITE(p," out float4 ocol0 : SV_Target,\n"); + WRITE(p,"void main(\n"); + if(ApiType != API_D3D11) + { + WRITE(p," out float4 ocol0 : COLOR0,\n"); + } + else + { + WRITE(p," out float4 ocol0 : SV_Target,\n"); + } + WRITE(p," in float2 uv0 : TEXCOORD0)\n"); } WRITE(p," in float2 uv0 : TEXCOORD0)\n" @@ -113,7 +137,7 @@ void WriteSwizzler(char*& p, u32 format, API_TYPE ApiType) WRITE(p, " float yl = floor(uv1.y / %f);\n", blkH); WRITE(p, " float yb = yl * %f;\n", blkH); WRITE(p, " float yoff = uv1.y - yb;\n"); - WRITE(p, " float xp = uv1.x + (yoff * textureDims.x);\n"); + WRITE(p, " float xp = uv1.x + (yoff * "I_COLORS"[1].x);\n"); WRITE(p, " float xel = floor(xp / %f);\n", blkW); WRITE(p, " float xb = floor(xel / %f);\n", blkH); WRITE(p, " float xoff = xel - (xb * %f);\n", blkH); @@ -121,26 +145,28 @@ void WriteSwizzler(char*& p, u32 format, API_TYPE ApiType) WRITE(p, " sampleUv.x = xib + (xb * %f);\n", blkW); WRITE(p, " sampleUv.y = yb + xoff;\n"); - WRITE(p, " sampleUv = sampleUv * blkDims.xy;\n"); + WRITE(p, " sampleUv = sampleUv * "I_COLORS"[0].xy;\n"); if(ApiType == API_OPENGL) - WRITE(p," sampleUv.y = textureDims.y - sampleUv.y;\n"); + WRITE(p," sampleUv.y = "I_COLORS"[1].y - sampleUv.y;\n"); - WRITE(p, " sampleUv = sampleUv + textureDims.zw;\n"); + WRITE(p, " sampleUv = sampleUv + "I_COLORS"[1].zw;\n"); if(ApiType != API_OPENGL) { WRITE(p, " sampleUv = sampleUv + float2(0.0f,1.0f);\n");// still to determine the reason for this - WRITE(p, " sampleUv = sampleUv / blkDims.zw;\n"); + WRITE(p, " sampleUv = sampleUv / "I_COLORS"[0].zw;\n"); } } // block dimensions : widthStride, heightStride // texture dims : width, height, x offset, y offset void Write32BitSwizzler(char*& p, u32 format, API_TYPE ApiType) -{ - WRITE(p, "uniform float4 blkDims : register(c%d);\n", C_COLORMATRIX); - WRITE(p, "uniform float4 textureDims : register(c%d);\n", C_COLORMATRIX + 1); +{ + // [0] left, top, right, bottom of source rectangle within source texture + // [1] width and height of destination texture in pixels + // Two were merged for GLSL + WRITE(p, "uniform float4 "I_COLORS"[2] %s;\n", WriteRegister(ApiType, "c", C_COLORS)); float blkW = (float)TexDecoder_GetBlockWidthInTexels(format); float blkH = (float)TexDecoder_GetBlockHeightInTexels(format); @@ -150,6 +176,10 @@ void Write32BitSwizzler(char*& p, u32 format, API_TYPE ApiType) { WRITE(p,"uniform samplerRECT samp0 : register(s0);\n"); } + else if (ApiType == API_GLSL) + { + WRITE(p, "uniform sampler2DRect samp0;\n"); + } else if (ApiType & API_D3D9) { WRITE(p,"uniform sampler samp0 : register(s0);\n"); @@ -160,17 +190,27 @@ void Write32BitSwizzler(char*& p, u32 format, API_TYPE ApiType) WRITE(p, "Texture2D Tex0 : register(t0);\n"); } - - WRITE(p,"void main(\n"); - if(ApiType != API_D3D11) + if(ApiType == API_GLSL) { - WRITE(p," out float4 ocol0 : COLOR0,\n"); + WRITE(p, " float4 ocol0;\n"); + WRITE(p, " float2 uv0 = gl_TexCoord[0].xy;\n"); + WRITE(p, "void main()\n"); } else { - WRITE(p," out float4 ocol0 : SV_Target,\n"); + WRITE(p,"void main(\n"); + if(ApiType != API_D3D11) + { + WRITE(p," out float4 ocol0 : COLOR0,\n"); + } + else + { + WRITE(p," out float4 ocol0 : SV_Target,\n"); + } + WRITE(p," in float2 uv0 : TEXCOORD0)\n"); } + WRITE(p," in float2 uv0 : TEXCOORD0)\n" "{\n" " float2 sampleUv;\n" @@ -179,7 +219,7 @@ void Write32BitSwizzler(char*& p, u32 format, API_TYPE ApiType) WRITE(p, " float yl = floor(uv1.y / %f);\n", blkH); WRITE(p, " float yb = yl * %f;\n", blkH); WRITE(p, " float yoff = uv1.y - yb;\n"); - WRITE(p, " float xp = uv1.x + (yoff * textureDims.x);\n"); + WRITE(p, " float xp = uv1.x + (yoff * "I_COLORS"[1].x);\n"); WRITE(p, " float xel = floor(xp / 2);\n"); WRITE(p, " float xb = floor(xel / %f);\n", blkH); WRITE(p, " float xoff = xel - (xb * %f);\n", blkH); @@ -192,17 +232,17 @@ void Write32BitSwizzler(char*& p, u32 format, API_TYPE ApiType) WRITE(p, " sampleUv.x = xib + (halfxb * %f);\n", blkW); WRITE(p, " sampleUv.y = yb + xoff;\n"); - WRITE(p, " sampleUv = sampleUv * blkDims.xy;\n"); + WRITE(p, " sampleUv = sampleUv * "I_COLORS"[0].xy;\n"); if(ApiType == API_OPENGL) - WRITE(p," sampleUv.y = textureDims.y - sampleUv.y;\n"); + WRITE(p," sampleUv.y = "I_COLORS"[1].y - sampleUv.y;\n"); - WRITE(p, " sampleUv = sampleUv + textureDims.zw;\n"); + WRITE(p, " sampleUv = sampleUv + "I_COLORS"[1].zw;\n"); if(ApiType != API_OPENGL) { WRITE(p, " sampleUv = sampleUv + float2(0.0f,1.0f);\n");// still to determine the reason for this - WRITE(p, " sampleUv = sampleUv / blkDims.zw;\n"); + WRITE(p, " sampleUv = sampleUv / "I_COLORS"[0].zw;\n"); } } @@ -213,15 +253,17 @@ void WriteSampleColor(char*& p, const char* colorComp, const char* dest, API_TYP texSampleOpName = "tex2D"; else if (ApiType == API_D3D11) texSampleOpName = "tex0.Sample"; + else if (ApiType == API_GLSL) + texSampleOpName = "texture2DRect"; else texSampleOpName = "texRECT"; // the increment of sampleUv.x is delayed, so we perform it here. see WriteIncrementSampleX. const char* texSampleIncrementUnit; if(ApiType != API_OPENGL) - texSampleIncrementUnit = "blkDims.x / blkDims.z"; + texSampleIncrementUnit = I_COLORS"[0].x / "I_COLORS"[0].z"; else - texSampleIncrementUnit = "blkDims.x"; + texSampleIncrementUnit = I_COLORS"[0].x"; WRITE(p, " %s = %s(samp0, sampleUv + float2(%d * (%s), 0)).%s;\n", dest, texSampleOpName, s_incrementSampleXCount, texSampleIncrementUnit, colorComp); @@ -263,8 +305,10 @@ void WriteToBitDepth(char*& p, u8 depth, const char* src, const char* dest) WRITE(p, " %s = floor(%s * %ff);\n", dest, src, result); } -void WriteEncoderEnd(char* p) +void WriteEncoderEnd(char* p, API_TYPE ApiType) { + if(ApiType == API_GLSL) + WRITE(p, "gl_FragData[0] = ocol0;\n"); WRITE(p, "}\n"); IntensityConstantAdded = false; s_incrementSampleXCount = 0; @@ -292,7 +336,7 @@ void WriteI8Encoder(char* p, API_TYPE ApiType) WRITE(p, " ocol0.rgba += IntensityConst.aaaa;\n"); // see WriteColorToIntensity - WriteEncoderEnd(p); + WriteEncoderEnd(p, ApiType); } void WriteI4Encoder(char* p, API_TYPE ApiType) @@ -340,7 +384,7 @@ void WriteI4Encoder(char* p, API_TYPE ApiType) WriteToBitDepth(p, 4, "color1", "color1"); WRITE(p, " ocol0 = (color0 * 16.0f + color1) / 255.0f;\n"); - WriteEncoderEnd(p); + WriteEncoderEnd(p, ApiType); } void WriteIA8Encoder(char* p,API_TYPE ApiType) @@ -359,7 +403,7 @@ void WriteIA8Encoder(char* p,API_TYPE ApiType) WRITE(p, " ocol0.ga += IntensityConst.aa;\n"); - WriteEncoderEnd(p); + WriteEncoderEnd(p, ApiType); } void WriteIA4Encoder(char* p,API_TYPE ApiType) @@ -394,7 +438,7 @@ void WriteIA4Encoder(char* p,API_TYPE ApiType) WriteToBitDepth(p, 4, "color1", "color1"); WRITE(p, " ocol0 = (color0 * 16.0f + color1) / 255.0f;\n"); - WriteEncoderEnd(p); + WriteEncoderEnd(p, ApiType); } void WriteRGB565Encoder(char* p,API_TYPE ApiType) @@ -419,7 +463,7 @@ void WriteRGB565Encoder(char* p,API_TYPE ApiType) WRITE(p, " ocol0.ga = ocol0.ga + gLower * 32.0f;\n"); WRITE(p, " ocol0 = ocol0 / 255.0f;\n"); - WriteEncoderEnd(p); + WriteEncoderEnd(p, ApiType); } void WriteRGB5A3Encoder(char* p,API_TYPE ApiType) @@ -486,7 +530,7 @@ void WriteRGB5A3Encoder(char* p,API_TYPE ApiType) WRITE(p, "}\n"); WRITE(p, " ocol0 = ocol0 / 255.0f;\n"); - WriteEncoderEnd(p); + WriteEncoderEnd(p, ApiType); } void WriteRGBA4443Encoder(char* p,API_TYPE ApiType) @@ -512,7 +556,7 @@ void WriteRGBA4443Encoder(char* p,API_TYPE ApiType) WriteToBitDepth(p, 4, "texSample.b", "color1.a"); WRITE(p, " ocol0 = (color0 * 16.0f + color1) / 255.0f;\n"); - WriteEncoderEnd(p); + WriteEncoderEnd(p, ApiType); } void WriteRGBA8Encoder(char* p,API_TYPE ApiType) @@ -542,7 +586,7 @@ void WriteRGBA8Encoder(char* p,API_TYPE ApiType) WRITE(p, " ocol0 = (cl0 * color0) + (cl1 * color1);\n"); - WriteEncoderEnd(p); + WriteEncoderEnd(p, ApiType); } void WriteC4Encoder(char* p, const char* comp,API_TYPE ApiType) @@ -578,7 +622,7 @@ void WriteC4Encoder(char* p, const char* comp,API_TYPE ApiType) WriteToBitDepth(p, 4, "color1", "color1"); WRITE(p, " ocol0 = (color0 * 16.0f + color1) / 255.0f;\n"); - WriteEncoderEnd(p); + WriteEncoderEnd(p, ApiType); } void WriteC8Encoder(char* p, const char* comp,API_TYPE ApiType) @@ -596,7 +640,7 @@ void WriteC8Encoder(char* p, const char* comp,API_TYPE ApiType) WriteSampleColor(p, comp, "ocol0.a",ApiType); - WriteEncoderEnd(p); + WriteEncoderEnd(p, ApiType); } void WriteCC4Encoder(char* p, const char* comp,API_TYPE ApiType) @@ -629,7 +673,7 @@ void WriteCC4Encoder(char* p, const char* comp,API_TYPE ApiType) WriteToBitDepth(p, 4, "color1", "color1"); WRITE(p, " ocol0 = (color0 * 16.0f + color1) / 255.0f;\n"); - WriteEncoderEnd(p); + WriteEncoderEnd(p, ApiType); } void WriteCC8Encoder(char* p, const char* comp, API_TYPE ApiType) @@ -641,7 +685,7 @@ void WriteCC8Encoder(char* p, const char* comp, API_TYPE ApiType) WriteSampleColor(p, comp, "ocol0.ra",ApiType); - WriteEncoderEnd(p); + WriteEncoderEnd(p, ApiType); } void WriteZ8Encoder(char* p, const char* multiplier,API_TYPE ApiType) @@ -665,7 +709,7 @@ void WriteZ8Encoder(char* p, const char* multiplier,API_TYPE ApiType) WriteSampleColor(p, "b", "depth",ApiType); WRITE(p, "ocol0.a = frac(depth * %s);\n", multiplier); - WriteEncoderEnd(p); + WriteEncoderEnd(p, ApiType); } void WriteZ16Encoder(char* p,API_TYPE ApiType) @@ -699,7 +743,7 @@ void WriteZ16Encoder(char* p,API_TYPE ApiType) WRITE(p, " ocol0.r = expanded.g / 255;\n"); WRITE(p, " ocol0.a = expanded.r / 255;\n"); - WriteEncoderEnd(p); + WriteEncoderEnd(p, ApiType); } void WriteZ16LEncoder(char* p,API_TYPE ApiType) @@ -737,7 +781,7 @@ void WriteZ16LEncoder(char* p,API_TYPE ApiType) WRITE(p, " ocol0.r = expanded.b;\n"); WRITE(p, " ocol0.a = expanded.g;\n"); - WriteEncoderEnd(p); + WriteEncoderEnd(p, ApiType); } void WriteZ24Encoder(char* p, API_TYPE ApiType) @@ -780,7 +824,7 @@ void WriteZ24Encoder(char* p, API_TYPE ApiType) WRITE(p, " ocol0.a = expanded1.r / 255;\n"); WRITE(p, " }\n"); - WriteEncoderEnd(p); + WriteEncoderEnd(p, ApiType); } const char *GenerateEncodingShader(u32 format,API_TYPE ApiType) @@ -790,6 +834,24 @@ const char *GenerateEncodingShader(u32 format,API_TYPE ApiType) char *p = text; + if(ApiType == API_GLSL) + { + // A few required defines and ones that will make our lives a lot easier + WRITE(p, "#version 120\n"); + // Silly differences + WRITE(p, "#define float2 vec2\n"); + WRITE(p, "#define float3 vec3\n"); + WRITE(p, "#define float4 vec4\n"); + + // cg to glsl function translation + WRITE(p, "#define frac(x) fract(x)\n"); + WRITE(p, "#define saturate(x) clamp(x, 0.0f, 1.0f)\n"); + WRITE(p, "#define lerp(x, y, z) mix(x, y, z)\n"); + + // We require this here + WRITE(p, "#ifdef GL_ARB_texture_rectangle\n #extension GL_ARB_texture_rectangle : require\n#endif\n"); + } + switch(format) { case GX_TF_I4: From 804938e9fc4afefac1e04b2eeae4c158b7b34d00 Mon Sep 17 00:00:00 2001 From: Ryan Houdek Date: Fri, 2 Dec 2011 20:17:26 -0600 Subject: [PATCH 07/83] More stuff --- .../Core/VideoCommon/Src/PixelShaderGen.cpp | 2 +- .../Core/VideoCommon/Src/VertexShaderGen.cpp | 20 +-- Source/Core/VideoCommon/Src/VideoConfig.cpp | 4 +- .../Plugin_VideoOGL/Src/PixelShaderCache.cpp | 10 +- .../Plugin_VideoOGL/Src/TextureConverter.cpp | 132 ++++++++++++------ .../Plugin_VideoOGL/Src/VertexShaderCache.cpp | 10 +- 6 files changed, 126 insertions(+), 52 deletions(-) diff --git a/Source/Core/VideoCommon/Src/PixelShaderGen.cpp b/Source/Core/VideoCommon/Src/PixelShaderGen.cpp index 4cdf562182..5d7fa9790e 100644 --- a/Source/Core/VideoCommon/Src/PixelShaderGen.cpp +++ b/Source/Core/VideoCommon/Src/PixelShaderGen.cpp @@ -1183,7 +1183,7 @@ void SampleTexture(char *&p, const char *destination, const char *texcoords, con if (ApiType == API_D3D11) WRITE(p, "%s=Tex%d.Sample(samp%d,%s.xy * "I_TEXDIMS"[%d].xy).%s;\n", destination, texmap,texmap, texcoords, texmap, texswap); else - WRITE(p, "%s=tex2D(samp%d,%s.xy * "I_TEXDIMS"[%d].xy).%s;\n", destination, texmap, texcoords, texmap, texswap); + WRITE(p, "%s=%s(samp%d,%s.xy * "I_TEXDIMS"[%d].xy).%s;\n", destination, ApiType == API_GLSL ? "texture2D" : "tex2D", texmap, texcoords, texmap, texswap); } static const char *tevAlphaFuncsTable[] = diff --git a/Source/Core/VideoCommon/Src/VertexShaderGen.cpp b/Source/Core/VideoCommon/Src/VertexShaderGen.cpp index 43ffdeedd8..ac35a4aa35 100644 --- a/Source/Core/VideoCommon/Src/VertexShaderGen.cpp +++ b/Source/Core/VideoCommon/Src/VertexShaderGen.cpp @@ -132,30 +132,32 @@ static char text[16384]; char* GenerateVSOutputStruct(char* p, u32 components, API_TYPE ApiType) { + // This turned a bit ugly with GLSL + // Will be less ugly with GLSL 1.3...hopefully WRITE(p, "struct VS_OUTPUT {\n"); - WRITE(p, " float4 pos : POSITION;\n"); - WRITE(p, " float4 colors_0 : COLOR0;\n"); - WRITE(p, " float4 colors_1 : COLOR1;\n"); + WRITE(p, " float4 pos %s POSITION;\n", ApiType == API_GLSL ? ";//" : ":"); + WRITE(p, " float4 colors_0 %s COLOR0;\n", ApiType == API_GLSL ? ";//" : ":"); + WRITE(p, " float4 colors_1 %s COLOR1;\n", ApiType == API_GLSL ? ";//" : ":"); if (xfregs.numTexGen.numTexGens < 7) { for (unsigned int i = 0; i < xfregs.numTexGen.numTexGens; ++i) - WRITE(p, " float3 tex%d : TEXCOORD%d;\n", i, i); - WRITE(p, " float4 clipPos : TEXCOORD%d;\n", xfregs.numTexGen.numTexGens); + WRITE(p, " float3 tex%d %s TEXCOORD%d;\n", i, ApiType == API_GLSL ? ";//" : ":", i); + WRITE(p, " float4 clipPos %s TEXCOORD%d;\n", ApiType == API_GLSL ? ";//" : ":", xfregs.numTexGen.numTexGens); if(g_ActiveConfig.bEnablePixelLighting && g_ActiveConfig.backend_info.bSupportsPixelLighting) - WRITE(p, " float4 Normal : TEXCOORD%d;\n", xfregs.numTexGen.numTexGens + 1); + WRITE(p, " float4 Normal %s TEXCOORD%d;\n", ApiType == API_GLSL ? ";//" : ":", xfregs.numTexGen.numTexGens + 1); } else { // clip position is in w of first 4 texcoords if(g_ActiveConfig.bEnablePixelLighting && g_ActiveConfig.backend_info.bSupportsPixelLighting) { for (int i = 0; i < 8; ++i) - WRITE(p, " float4 tex%d : TEXCOORD%d;\n", i, i); + WRITE(p, " float4 tex%d %s TEXCOORD%d;\n", i, ApiType == API_GLSL ? ";//" : ":", i); } else { for (unsigned int i = 0; i < xfregs.numTexGen.numTexGens; ++i) - WRITE(p, " float%d tex%d : TEXCOORD%d;\n", i < 4 ? 4 : 3 , i, i); + WRITE(p, " float%d tex%d %s TEXCOORD%d;\n", i < 4 ? 4 : 3 , i, ApiType == API_GLSL ? ";//" : ":", i); } - } + } WRITE(p, "};\n"); return p; diff --git a/Source/Core/VideoCommon/Src/VideoConfig.cpp b/Source/Core/VideoCommon/Src/VideoConfig.cpp index 57685dec09..69c1c4ce1c 100644 --- a/Source/Core/VideoCommon/Src/VideoConfig.cpp +++ b/Source/Core/VideoCommon/Src/VideoConfig.cpp @@ -43,6 +43,7 @@ VideoConfig::VideoConfig() backend_info.APIType = API_NONE; backend_info.bUseRGBATextures = false; backend_info.bSupports3DVision = false; + backend_info.bSupportsGLSL = false; } void VideoConfig::Load(const char *ini_file) @@ -92,6 +93,7 @@ void VideoConfig::Load(const char *ini_file) iniFile.Get("Settings", "DisableLighting", &bDisableLighting, 0); iniFile.Get("Settings", "DisableTexturing", &bDisableTexturing, 0); iniFile.Get("Settings", "DisableFog", &bDisableFog, 0); + iniFile.Get("Settings", "UseGLSL", &bUseGLSL, 0); iniFile.Get("Settings", "EnableOpenCL", &bEnableOpenCL, false); iniFile.Get("Settings", "OMPDecoder", &bOMPDecoder, false); @@ -232,7 +234,7 @@ void VideoConfig::Save(const char *ini_file) iniFile.Set("Settings", "DisableTexturing", bDisableTexturing); iniFile.Set("Settings", "DstAlphaPass", bDstAlphaPass); iniFile.Set("Settings", "DisableFog", bDisableFog); - iniFile.Set("Settings", "bUseGLSL", bUseGLSL); + iniFile.Set("Settings", "UseGLSL", bUseGLSL); iniFile.Set("Settings", "EnableOpenCL", bEnableOpenCL); iniFile.Set("Settings", "OMPDecoder", bOMPDecoder); diff --git a/Source/Plugins/Plugin_VideoOGL/Src/PixelShaderCache.cpp b/Source/Plugins/Plugin_VideoOGL/Src/PixelShaderCache.cpp index 32e6fb3374..601445988f 100644 --- a/Source/Plugins/Plugin_VideoOGL/Src/PixelShaderCache.cpp +++ b/Source/Plugins/Plugin_VideoOGL/Src/PixelShaderCache.cpp @@ -314,7 +314,15 @@ bool CompileGLSLPixelShader(FRAGMENTSHADER& ps, const char* pstrprogram) GLsizei charsWritten; GLchar* infoLog = new GLchar[length]; glGetShaderInfoLog(result, length, &charsWritten, infoLog); - WARN_LOG(VIDEO, "Shader info log:\n%s", infoLog); + WARN_LOG(VIDEO, "PS Shader info log:\n%s", infoLog); + char szTemp[MAX_PATH]; + sprintf(szTemp, "ps_%d.txt", result); + FILE *fp = fopen(szTemp, "wb"); + fwrite(pstrprogram, strlen(pstrprogram), 1, fp); + fclose(fp); + + if(strstr(infoLog, "warning") != NULL || strstr(infoLog, "error") != NULL) + exit(0); delete[] infoLog; } diff --git a/Source/Plugins/Plugin_VideoOGL/Src/TextureConverter.cpp b/Source/Plugins/Plugin_VideoOGL/Src/TextureConverter.cpp index c715775194..588c593b13 100644 --- a/Source/Plugins/Plugin_VideoOGL/Src/TextureConverter.cpp +++ b/Source/Plugins/Plugin_VideoOGL/Src/TextureConverter.cpp @@ -60,51 +60,105 @@ static FRAGMENTSHADER s_encodingPrograms[NUM_ENCODING_PROGRAMS]; void CreateRgbToYuyvProgram() { // Output is BGRA because that is slightly faster than RGBA. - const char *FProgram = - "uniform samplerRECT samp0 : register(s0);\n" - "void main(\n" - " out float4 ocol0 : COLOR0,\n" - " in float2 uv0 : TEXCOORD0)\n" - "{\n" - " float2 uv1 = float2(uv0.x + 1.0f, uv0.y);\n" - " float3 c0 = texRECT(samp0, uv0).rgb;\n" - " float3 c1 = texRECT(samp0, uv1).rgb;\n" - " float3 y_const = float3(0.257f,0.504f,0.098f);\n" - " float3 u_const = float3(-0.148f,-0.291f,0.439f);\n" - " float3 v_const = float3(0.439f,-0.368f,-0.071f);\n" - " float4 const3 = float4(0.0625f,0.5f,0.0625f,0.5f);\n" - " float3 c01 = (c0 + c1) * 0.5f;\n" - " ocol0 = float4(dot(c1,y_const),dot(c01,u_const),dot(c0,y_const),dot(c01, v_const)) + const3;\n" - "}\n"; - - if (!PixelShaderCache::CompilePixelShader(s_rgbToYuyvProgram, FProgram)) - ERROR_LOG(VIDEO, "Failed to create RGB to YUYV fragment program."); + if(g_ActiveConfig.bUseGLSL) + { + const char *FProgram = + "#version 120\n" + "#ifdef GL_ARB_texture_rectangle\n" + "#extension GL_ARB_texture_rectangle : require\n" + "#endif\n" + "uniform sampler2DRect samp0;\n" + "void main()\n" + "{\n" + " vec2 uv1 = vec2(gl_TexCoord[0].x + 1.0f, gl_TexCoord[0].y);\n" + " vec3 c0 = texture2DRect(samp0, gl_TexCoord[0].xy).rgb;\n" + " vec3 c1 = texture2DRect(samp0, uv1).rgb;\n" + " vec3 y_const = vec3(0.257f,0.504f,0.098f);\n" + " vec3 u_const = vec3(-0.148f,-0.291f,0.439f);\n" + " vec3 v_const = vec3(0.439f,-0.368f,-0.071f);\n" + " vec4 const3 = vec4(0.0625f,0.5f,0.0625f,0.5f);\n" + " vec3 c01 = (c0 + c1) * 0.5f;\n" + " gl_FragData[0] = vec4(dot(c1,y_const),dot(c01,u_const),dot(c0,y_const),dot(c01, v_const)) + const3;\n" + "}\n"; + if (!PixelShaderCache::CompilePixelShader(s_rgbToYuyvProgram, FProgram)) + ERROR_LOG(VIDEO, "Failed to create RGB to YUYV fragment program."); + } + else + { + const char *FProgram = + "uniform samplerRECT samp0 : register(s0);\n" + "void main(\n" + " out float4 ocol0 : COLOR0,\n" + " in float2 uv0 : TEXCOORD0)\n" + "{\n" + " float2 uv1 = float2(uv0.x + 1.0f, uv0.y);\n" + " float3 c0 = texRECT(samp0, uv0).rgb;\n" + " float3 c1 = texRECT(samp0, uv1).rgb;\n" + " float3 y_const = float3(0.257f,0.504f,0.098f);\n" + " float3 u_const = float3(-0.148f,-0.291f,0.439f);\n" + " float3 v_const = float3(0.439f,-0.368f,-0.071f);\n" + " float4 const3 = float4(0.0625f,0.5f,0.0625f,0.5f);\n" + " float3 c01 = (c0 + c1) * 0.5f;\n" + " ocol0 = float4(dot(c1,y_const),dot(c01,u_const),dot(c0,y_const),dot(c01, v_const)) + const3;\n" + "}\n"; + if (!PixelShaderCache::CompilePixelShader(s_rgbToYuyvProgram, FProgram)) + ERROR_LOG(VIDEO, "Failed to create RGB to YUYV fragment program."); + } } void CreateYuyvToRgbProgram() { - const char *FProgram = - "uniform samplerRECT samp0 : register(s0);\n" - "void main(\n" - " out float4 ocol0 : COLOR0,\n" - " in float2 uv0 : TEXCOORD0)\n" - "{\n" - " float4 c0 = texRECT(samp0, uv0).rgba;\n" + if(g_ActiveConfig.bUseGLSL) + { + const char *FProgram = + "#version 120\n" + "#ifdef GL_ARB_texture_rectangle\n" + "#extension GL_ARB_texture_rectangle : require\n" + "#endif\n" + "uniform sampler2DRect samp0;\n" + "void main()\n" + "{\n" + " vec4 c0 = texture2DRect(samp0, gl_TexCoord[0].xy).rgba;\n" - " float f = step(0.5, frac(uv0.x));\n" - " float y = lerp(c0.b, c0.r, f);\n" - " float yComp = 1.164f * (y - 0.0625f);\n" - " float uComp = c0.g - 0.5f;\n" - " float vComp = c0.a - 0.5f;\n" + " float f = step(0.5, fract(gl_TexCoord[0].x));\n" + " float y = mix(c0.b, c0.r, f);\n" + " float yComp = 1.164f * (y - 0.0625f);\n" + " float uComp = c0.g - 0.5f;\n" + " float vComp = c0.a - 0.5f;\n" - " ocol0 = float4(yComp + (1.596f * vComp),\n" - " yComp - (0.813f * vComp) - (0.391f * uComp),\n" - " yComp + (2.018f * uComp),\n" - " 1.0f);\n" - "}\n"; + " gl_FragData[0] = vec4(yComp + (1.596f * vComp),\n" + " yComp - (0.813f * vComp) - (0.391f * uComp),\n" + " yComp + (2.018f * uComp),\n" + " 1.0f);\n" + "}\n"; + if (!PixelShaderCache::CompilePixelShader(s_yuyvToRgbProgram, FProgram)) + ERROR_LOG(VIDEO, "Failed to create YUYV to RGB fragment program."); + } + else + { + const char *FProgram = + "uniform samplerRECT samp0 : register(s0);\n" + "void main(\n" + " out float4 ocol0 : COLOR0,\n" + " in float2 uv0 : TEXCOORD0)\n" + "{\n" + " float4 c0 = texRECT(samp0, uv0).rgba;\n" - if (!PixelShaderCache::CompilePixelShader(s_yuyvToRgbProgram, FProgram)) - ERROR_LOG(VIDEO, "Failed to create YUYV to RGB fragment program."); + " float f = step(0.5, frac(uv0.x));\n" + " float y = lerp(c0.b, c0.r, f);\n" + " float yComp = 1.164f * (y - 0.0625f);\n" + " float uComp = c0.g - 0.5f;\n" + " float vComp = c0.a - 0.5f;\n" + + " ocol0 = float4(yComp + (1.596f * vComp),\n" + " yComp - (0.813f * vComp) - (0.391f * uComp),\n" + " yComp + (2.018f * uComp),\n" + " 1.0f);\n" + "}\n"; + + if (!PixelShaderCache::CompilePixelShader(s_yuyvToRgbProgram, FProgram)) + ERROR_LOG(VIDEO, "Failed to create YUYV to RGB fragment program."); + } } FRAGMENTSHADER &GetOrCreateEncodingShader(u32 format) @@ -117,7 +171,7 @@ FRAGMENTSHADER &GetOrCreateEncodingShader(u32 format) if (s_encodingPrograms[format].glprogid == 0) { - const char* shader = TextureConversionShader::GenerateEncodingShader(format,API_OPENGL); + const char* shader = TextureConversionShader::GenerateEncodingShader(format, g_ActiveConfig.bUseGLSL ? API_GLSL : API_OPENGL); #if defined(_DEBUG) || defined(DEBUGFAST) if (g_ActiveConfig.iLog & CONF_SAVESHADERS && shader) diff --git a/Source/Plugins/Plugin_VideoOGL/Src/VertexShaderCache.cpp b/Source/Plugins/Plugin_VideoOGL/Src/VertexShaderCache.cpp index ea7a97196b..c543dfb86e 100644 --- a/Source/Plugins/Plugin_VideoOGL/Src/VertexShaderCache.cpp +++ b/Source/Plugins/Plugin_VideoOGL/Src/VertexShaderCache.cpp @@ -198,7 +198,15 @@ bool CompileGLSLVertexShader(VERTEXSHADER& vs, const char* pstrprogram) GLsizei charsWritten; GLchar* infoLog = new GLchar[length]; glGetShaderInfoLog(result, length, &charsWritten, infoLog); - WARN_LOG(VIDEO, "Shader info log:\n%s", infoLog); + WARN_LOG(VIDEO, "VS Shader info log:\n%s", infoLog); + char szTemp[MAX_PATH]; + sprintf(szTemp, "vs_%d.txt", result); + FILE *fp = fopen(szTemp, "wb"); + fwrite(pstrprogram, strlen(pstrprogram), 1, fp); + fclose(fp); + + if(strstr(infoLog, "warning") != NULL || strstr(infoLog, "error") != NULL) + exit(0); delete[] infoLog; } From 164b56ff73c9e850edbac8546a9d4530621082b5 Mon Sep 17 00:00:00 2001 From: Ryan Houdek Date: Fri, 2 Dec 2011 20:20:53 -0600 Subject: [PATCH 08/83] This is the terrible bit that can't be removed until we use UBOs in the GLSL shaders. --- .../VideoCommon/Src/PixelShaderManager.cpp | 36 +++++++++---------- .../VideoCommon/Src/VertexShaderManager.cpp | 22 ++++++------ 2 files changed, 29 insertions(+), 29 deletions(-) diff --git a/Source/Core/VideoCommon/Src/PixelShaderManager.cpp b/Source/Core/VideoCommon/Src/PixelShaderManager.cpp index 2521f80500..4f6cd8da2d 100644 --- a/Source/Core/VideoCommon/Src/PixelShaderManager.cpp +++ b/Source/Core/VideoCommon/Src/PixelShaderManager.cpp @@ -87,35 +87,35 @@ void PixelShaderManager::SetConstants() { for (int i = 0; i < 2; ++i) { - if (s_nColorsChanged[i]) + if (s_nColorsChanged[i] || g_ActiveConfig.bUseGLSL) { int baseind = i ? C_KCOLORS : C_COLORS; for (int j = 0; j < 4; ++j) { - if (s_nColorsChanged[i] & (1 << j)) + if (s_nColorsChanged[i] & (1 << j) || g_ActiveConfig.bUseGLSL) SetPSConstant4fv(baseind+j, &lastRGBAfull[i][j][0]); } s_nColorsChanged[i] = 0; } } - if (s_nTexDimsChanged) + if (s_nTexDimsChanged || g_ActiveConfig.bUseGLSL) { for (int i = 0; i < 8; ++i) { - if (s_nTexDimsChanged & (1<>8)&0xff)/255.0f, 0, ((lastAlpha>>16)&0xff)/255.0f); s_bAlphaChanged = false; } - if (s_bZTextureTypeChanged) + if (s_bZTextureTypeChanged || g_ActiveConfig.bUseGLSL) { float ftemp[4]; switch (bpmem.ztex2.type) @@ -137,7 +137,7 @@ void PixelShaderManager::SetConstants() s_bZTextureTypeChanged = false; } - if (s_bZBiasChanged || s_bDepthRangeChanged) + if (s_bZBiasChanged || s_bDepthRangeChanged || g_ActiveConfig.bUseGLSL) { // reversed gxsetviewport(xorig, yorig, width, height, nearz, farz) // [0] = width/2 @@ -153,12 +153,12 @@ void PixelShaderManager::SetConstants() } // indirect incoming texture scales - if (s_nIndTexScaleChanged) + if (s_nIndTexScaleChanged || g_ActiveConfig.bUseGLSL) { // set as two sets of vec4s, each containing S and T of two ind stages. float f[8]; - if (s_nIndTexScaleChanged & 0x03) + if (s_nIndTexScaleChanged & 0x03 || g_ActiveConfig.bUseGLSL) { for (u32 i = 0; i < 2; ++i) { @@ -169,7 +169,7 @@ void PixelShaderManager::SetConstants() SetPSConstant4fv(C_INDTEXSCALE, f); } - if (s_nIndTexScaleChanged & 0x0c) { + if (s_nIndTexScaleChanged & 0x0c || g_ActiveConfig.bUseGLSL) { for (u32 i = 2; i < 4; ++i) { f[2 * i] = bpmem.texscale[1].getScaleS(i & 1); f[2 * i + 1] = bpmem.texscale[1].getScaleT(i & 1); @@ -181,11 +181,11 @@ void PixelShaderManager::SetConstants() s_nIndTexScaleChanged = 0; } - if (s_nIndTexMtxChanged) + if (s_nIndTexMtxChanged || g_ActiveConfig.bUseGLSL) { for (int i = 0; i < 3; ++i) { - if (s_nIndTexMtxChanged & (1 << i)) + if (s_nIndTexMtxChanged & (1 << i) || g_ActiveConfig.bUseGLSL) { int scale = ((u32)bpmem.indmtx[i].col0.s0 << 0) | ((u32)bpmem.indmtx[i].col1.s1 << 2) | @@ -215,13 +215,13 @@ void PixelShaderManager::SetConstants() s_nIndTexMtxChanged = 0; } - if (s_bFogColorChanged) + if (s_bFogColorChanged || g_ActiveConfig.bUseGLSL) { SetPSConstant4f(C_FOG, bpmem.fog.color.r / 255.0f, bpmem.fog.color.g / 255.0f, bpmem.fog.color.b / 255.0f, 0); s_bFogColorChanged = false; } - if (s_bFogParamChanged) + if (s_bFogParamChanged || g_ActiveConfig.bUseGLSL) { if(!g_ActiveConfig.bDisableFog) { @@ -237,7 +237,7 @@ void PixelShaderManager::SetConstants() s_bFogParamChanged = false; } - if (s_bFogRangeAdjustChanged) + if (s_bFogRangeAdjustChanged || g_ActiveConfig.bUseGLSL) { if(!g_ActiveConfig.bDisableFog && bpmem.fogRange.Base.Enabled == 1) { @@ -261,7 +261,7 @@ void PixelShaderManager::SetConstants() if (g_ActiveConfig.bEnablePixelLighting && g_ActiveConfig.backend_info.bSupportsPixelLighting) // config check added because the code in here was crashing for me inside SetPSConstant4f { - if (nLightsChanged[0] >= 0) + if (nLightsChanged[0] >= 0 || g_ActiveConfig.bUseGLSL) { // lights don't have a 1 to 1 mapping, the color component needs to be converted to 4 floats int istart = nLightsChanged[0] / 0x10; @@ -297,14 +297,14 @@ void PixelShaderManager::SetConstants() nLightsChanged[0] = nLightsChanged[1] = -1; } - if (nMaterialsChanged) + if (nMaterialsChanged || g_ActiveConfig.bUseGLSL) { float GC_ALIGNED16(material[4]); float NormalizationCoef = 1 / 255.0f; for (int i = 0; i < 4; ++i) { - if (nMaterialsChanged & (1 << i)) + if (nMaterialsChanged & (1 << i) || g_ActiveConfig.bUseGLSL) { u32 data = *(xfregs.ambColor + i); diff --git a/Source/Core/VideoCommon/Src/VertexShaderManager.cpp b/Source/Core/VideoCommon/Src/VertexShaderManager.cpp index a18829fde2..8fd53d128d 100644 --- a/Source/Core/VideoCommon/Src/VertexShaderManager.cpp +++ b/Source/Core/VideoCommon/Src/VertexShaderManager.cpp @@ -173,7 +173,7 @@ void VertexShaderManager::Dirty() // TODO: A cleaner way to control the matricies without making a mess in the parameters field void VertexShaderManager::SetConstants() { - if (nTransformMatricesChanged[0] >= 0) + if (nTransformMatricesChanged[0] >= 0 || g_ActiveConfig.bUseGLSL) { int startn = nTransformMatricesChanged[0] / 4; int endn = (nTransformMatricesChanged[1] + 3) / 4; @@ -181,7 +181,7 @@ void VertexShaderManager::SetConstants() SetMultiVSConstant4fv(C_TRANSFORMMATRICES + startn, endn - startn, pstart); nTransformMatricesChanged[0] = nTransformMatricesChanged[1] = -1; } - if (nNormalMatricesChanged[0] >= 0) + if (nNormalMatricesChanged[0] >= 0 || g_ActiveConfig.bUseGLSL) { int startn = nNormalMatricesChanged[0] / 3; int endn = (nNormalMatricesChanged[1] + 2) / 3; @@ -190,7 +190,7 @@ void VertexShaderManager::SetConstants() nNormalMatricesChanged[0] = nNormalMatricesChanged[1] = -1; } - if (nPostTransformMatricesChanged[0] >= 0) + if (nPostTransformMatricesChanged[0] >= 0 || g_ActiveConfig.bUseGLSL) { int startn = nPostTransformMatricesChanged[0] / 4; int endn = (nPostTransformMatricesChanged[1] + 3 ) / 4; @@ -198,7 +198,7 @@ void VertexShaderManager::SetConstants() SetMultiVSConstant4fv(C_POSTTRANSFORMMATRICES + startn, endn - startn, pstart); } - if (nLightsChanged[0] >= 0) + if (nLightsChanged[0] >= 0 || g_ActiveConfig.bUseGLSL) { // lights don't have a 1 to 1 mapping, the color component needs to be converted to 4 floats int istart = nLightsChanged[0] / 0x10; @@ -234,14 +234,14 @@ void VertexShaderManager::SetConstants() nLightsChanged[0] = nLightsChanged[1] = -1; } - if (nMaterialsChanged) + if (nMaterialsChanged || g_ActiveConfig.bUseGLSL) { float GC_ALIGNED16(material[4]); float NormalizationCoef = 1 / 255.0f; for (int i = 0; i < 4; ++i) { - if (nMaterialsChanged & (1 << i)) + if (nMaterialsChanged & (1 << i) || g_ActiveConfig.bUseGLSL) { u32 data = *(xfregs.ambColor + i); @@ -257,7 +257,7 @@ void VertexShaderManager::SetConstants() nMaterialsChanged = 0; } - if (bPosNormalMatrixChanged) + if (bPosNormalMatrixChanged || g_ActiveConfig.bUseGLSL) { bPosNormalMatrixChanged = false; @@ -268,7 +268,7 @@ void VertexShaderManager::SetConstants() SetMultiVSConstant3fv(C_POSNORMALMATRIX + 3, 3, norm); } - if (bTexMatricesChanged[0]) + if (bTexMatricesChanged[0] || g_ActiveConfig.bUseGLSL) { bTexMatricesChanged[0] = false; const float *fptrs[] = @@ -283,7 +283,7 @@ void VertexShaderManager::SetConstants() } } - if (bTexMatricesChanged[1]) + if (bTexMatricesChanged[1] || g_ActiveConfig.bUseGLSL) { bTexMatricesChanged[1] = false; const float *fptrs[] = { @@ -297,7 +297,7 @@ void VertexShaderManager::SetConstants() } } - if (bViewportChanged) + if (bViewportChanged || g_ActiveConfig.bUseGLSL) { bViewportChanged = false; SetVSConstant4f(C_DEPTHPARAMS,xfregs.viewport.farZ / 16777216.0f,xfregs.viewport.zRange / 16777216.0f,0.0f,0.0f); @@ -306,7 +306,7 @@ void VertexShaderManager::SetConstants() bProjectionChanged = true; } - if (bProjectionChanged) + if (bProjectionChanged || g_ActiveConfig.bUseGLSL) { bProjectionChanged = false; From 33c24f0a15161de23c26c61e2fc6efeb73def6df Mon Sep 17 00:00:00 2001 From: Ryan Houdek Date: Wed, 7 Dec 2011 22:04:34 -0600 Subject: [PATCH 09/83] Almost there. --- .../Core/VideoCommon/Src/PixelShaderGen.cpp | 2 +- .../Core/VideoCommon/Src/VertexShaderGen.cpp | 2 +- .../Plugin_VideoOGL/Src/PixelShaderCache.cpp | 218 +++++++++++------- .../Plugin_VideoOGL/Src/TextureCache.cpp | 9 +- .../Plugin_VideoOGL/Src/VertexManager.cpp | 29 ++- .../Plugin_VideoOGL/Src/VertexShaderCache.cpp | 3 +- 6 files changed, 175 insertions(+), 88 deletions(-) diff --git a/Source/Core/VideoCommon/Src/PixelShaderGen.cpp b/Source/Core/VideoCommon/Src/PixelShaderGen.cpp index 5d7fa9790e..d3d59cde63 100644 --- a/Source/Core/VideoCommon/Src/PixelShaderGen.cpp +++ b/Source/Core/VideoCommon/Src/PixelShaderGen.cpp @@ -1263,7 +1263,7 @@ static bool WriteAlphaTest(char *&p, API_TYPE ApiType,DSTALPHA_MODE dstAlphaMode compindex = bpmem.alphaFunc.comp1 % 8; WRITE(p, tevAlphaFuncsTable[compindex],alphaRef[1]);//lookup the second component from the alpha function table - WRITE(p, ")){ocol0 = 0;%s%s discard;%s}\n", + WRITE(p, ")){ocol0 = float4(0.0);%s%s discard;%s}\n", dstAlphaMode == DSTALPHA_DUAL_SOURCE_BLEND ? "ocol1 = 0;" : "", DepthTextureEnable ? "depth = 1.f;" : "", (ApiType != API_D3D11) ? "return;" : ""); diff --git a/Source/Core/VideoCommon/Src/VertexShaderGen.cpp b/Source/Core/VideoCommon/Src/VertexShaderGen.cpp index ac35a4aa35..9a6a220979 100644 --- a/Source/Core/VideoCommon/Src/VertexShaderGen.cpp +++ b/Source/Core/VideoCommon/Src/VertexShaderGen.cpp @@ -291,7 +291,7 @@ const char *GenerateVertexShaderCode(u32 components, API_TYPE ApiType) } else { - WRITE(p, "int posmtx = fposmtx;\n"); + WRITE(p, "int posmtx = int(fposmtx);\n"); } WRITE(p, "float4 pos = float4(dot("I_TRANSFORMMATRICES"[posmtx], rawpos), dot("I_TRANSFORMMATRICES"[posmtx+1], rawpos), dot("I_TRANSFORMMATRICES"[posmtx+2], rawpos), 1);\n"); diff --git a/Source/Plugins/Plugin_VideoOGL/Src/PixelShaderCache.cpp b/Source/Plugins/Plugin_VideoOGL/Src/PixelShaderCache.cpp index 601445988f..6c67252dde 100644 --- a/Source/Plugins/Plugin_VideoOGL/Src/PixelShaderCache.cpp +++ b/Source/Plugins/Plugin_VideoOGL/Src/PixelShaderCache.cpp @@ -38,8 +38,8 @@ namespace OGL { static int s_nMaxPixelInstructions; -static GLuint s_ColorMatrixProgram = 0; -static GLuint s_DepthMatrixProgram = 0; +static FRAGMENTSHADER s_ColorMatrixProgram; +static FRAGMENTSHADER s_DepthMatrixProgram; PixelShaderCache::PSCache PixelShaderCache::PixelShaders; PIXELSHADERUID PixelShaderCache::s_curuid; bool PixelShaderCache::s_displayCompileAlert; @@ -56,12 +56,12 @@ bool (*pCompilePixelShader)(FRAGMENTSHADER&, const char*); GLuint PixelShaderCache::GetDepthMatrixProgram() { - return s_DepthMatrixProgram; + return s_DepthMatrixProgram.glprogid; } GLuint PixelShaderCache::GetColorMatrixProgram() { - return s_ColorMatrixProgram; + return s_ColorMatrixProgram.glprogid; } void PixelShaderCache::Init() @@ -105,98 +105,154 @@ void PixelShaderCache::Init() glGetProgramivARB(GL_FRAGMENT_PROGRAM_ARB, GL_MAX_PROGRAM_NATIVE_INSTRUCTIONS_ARB, (GLint *)&maxinst); glGetProgramivARB(GL_FRAGMENT_PROGRAM_ARB, GL_MAX_PROGRAM_NATIVE_ATTRIBS_ARB, (GLint *)&maxattribs); INFO_LOG(VIDEO, "pixel max_alu=%d, max_inst=%d, max_attrib=%d", s_nMaxPixelInstructions, maxinst, maxattribs); + if(g_ActiveConfig.bUseGLSL) + { + char pmatrixprog[2048]; + sprintf(pmatrixprog, "#extension GL_ARB_texture_rectangle : enable\n" + "uniform sampler2DRect samp0;\n" + "uniform vec4 "I_COLORS"[7];\n" + "void main(){\n" + "vec4 Temp0, Temp1;\n" + "vec4 K0 = vec4(0.5, 0.5, 0.5, 0.5);\n" + "Temp0 = texture2DRect(samp0, gl_TexCoord[0].xy);\n" + "Temp0 = Temp0 * "I_COLORS"[%d];\n" + "Temp0 = Temp0 + K0;\n" + "Temp0 = floor(Temp0);\n" + "Temp0 = Temp0 * "I_COLORS"[%d];\n" + "Temp1.x = dot(Temp0, "I_COLORS"[%d]);\n" + "Temp1.y = dot(Temp0, "I_COLORS"[%d]);\n" + "Temp1.z = dot(Temp0, "I_COLORS"[%d]);\n" + "Temp1.w = dot(Temp0, "I_COLORS"[%d]);\n" + "gl_FragData[0] = Temp1 + "I_COLORS"[%d];\n" + "}\n", C_COLORS+5, C_COLORS+6, C_COLORS, C_COLORS+1, C_COLORS+2, C_COLORS+3, C_COLORS+4); + if(!pCompilePixelShader(s_ColorMatrixProgram, pmatrixprog)) + { + ERROR_LOG(VIDEO, "Failed to create color matrix fragment program"); + s_ColorMatrixProgram.Destroy(); + } + sprintf(pmatrixprog, "#extension GL_ARB_texture_rectangle : enable\n" + "uniform sampler2DRect samp0;\n" + "uniform vec4 "I_COLORS"[5];\n" + "void main(){\n" + "vec4 R0, R1, R2;\n" + "vec4 K0 = vec4(255.99998474121, 0.003921568627451, 256.0, 0.0);\n" + "vec4 K1 = vec4(15.0, 0.066666666666, 0.0, 0.0);\n" + "R2 = texture2DRect(samp0, gl_TexCoord[0].xy);\n" + "R0.x = R2.x * K0.x;\n" + "R0.x = floor(R0).x;\n" + "R0.yzw = (R0 - R0.x).yzw;\n" + "R0.yzw = (R0 * K0.z).yzw;\n" + "R0.y = floor(R0).y;\n" + "R0.zw = (R0 - R0.y).zw;\n" + "R0.zw = (R0 * K0.z).zw;\n" + "R0.z = floor(R0).z;\n" + "R0.w = R0.x;\n" + "R0 = R0 * K0.y;\n" + "R0.w = (R0 * K1.x).w;\n" + "R0.w = floor(R0).w;\n" + "R0.w = (R0 * K1.y).w;\n" + "R1.x = dot(R0, "I_COLORS"[%d]);\n" + "R1.y = dot(R0, "I_COLORS"[%d]);\n" + "R1.z = dot(R0, "I_COLORS"[%d]);\n" + "R1.w = dot(R0, "I_COLORS"[%d]);\n" + "gl_FragData[0] = R1 * "I_COLORS"[%d];\n" + "}\n", C_COLORS, C_COLORS+1, C_COLORS+2, C_COLORS+3, C_COLORS+4); + if(!pCompilePixelShader(s_DepthMatrixProgram, pmatrixprog)) + { + ERROR_LOG(VIDEO, "Failed to create depth matrix fragment program"); + s_DepthMatrixProgram.Destroy(); + } + } + else + { + char pmatrixprog[2048]; + sprintf(pmatrixprog, "!!ARBfp1.0" + "TEMP R0;\n" + "TEMP R1;\n" + "PARAM K0 = { 0.5, 0.5, 0.5, 0.5};\n" + "TEX R0, fragment.texcoord[0], texture[0], RECT;\n" + "MUL R0, R0, program.env[%d];\n" + "ADD R0, R0, K0;\n" + "FLR R0, R0;\n" + "MUL R0, R0, program.env[%d];\n" + "DP4 R1.x, R0, program.env[%d];\n" + "DP4 R1.y, R0, program.env[%d];\n" + "DP4 R1.z, R0, program.env[%d];\n" + "DP4 R1.w, R0, program.env[%d];\n" + "ADD result.color, R1, program.env[%d];\n" + "END\n",C_COLORS+5,C_COLORS+6, C_COLORS, C_COLORS+1, C_COLORS+2, C_COLORS+3, C_COLORS+4); + glGenProgramsARB(1, &s_ColorMatrixProgram.glprogid); + SetCurrentShader(s_ColorMatrixProgram.glprogid); + glProgramStringARB(GL_FRAGMENT_PROGRAM_ARB, GL_PROGRAM_FORMAT_ASCII_ARB, (GLsizei)strlen(pmatrixprog), pmatrixprog); - char pmatrixprog[2048]; - sprintf(pmatrixprog, "!!ARBfp1.0" - "TEMP R0;\n" - "TEMP R1;\n" - "PARAM K0 = { 0.5, 0.5, 0.5, 0.5};\n" - "TEX R0, fragment.texcoord[0], texture[0], RECT;\n" - "MUL R0, R0, program.env[%d];\n" - "ADD R0, R0, K0;\n" - "FLR R0, R0;\n" - "MUL R0, R0, program.env[%d];\n" - "DP4 R1.x, R0, program.env[%d];\n" - "DP4 R1.y, R0, program.env[%d];\n" - "DP4 R1.z, R0, program.env[%d];\n" - "DP4 R1.w, R0, program.env[%d];\n" - "ADD result.color, R1, program.env[%d];\n" - "END\n",C_COLORMATRIX+5,C_COLORMATRIX+6, C_COLORMATRIX, C_COLORMATRIX+1, C_COLORMATRIX+2, C_COLORMATRIX+3, C_COLORMATRIX+4); - glGenProgramsARB(1, &s_ColorMatrixProgram); - SetCurrentShader(s_ColorMatrixProgram); - glProgramStringARB(GL_FRAGMENT_PROGRAM_ARB, GL_PROGRAM_FORMAT_ASCII_ARB, (GLsizei)strlen(pmatrixprog), pmatrixprog); + GLenum err = GL_REPORT_ERROR(); + if (err != GL_NO_ERROR) { + ERROR_LOG(VIDEO, "Failed to create color matrix fragment program"); + s_ColorMatrixProgram.Destroy(); + } - GLenum err = GL_REPORT_ERROR(); - if (err != GL_NO_ERROR) { - ERROR_LOG(VIDEO, "Failed to create color matrix fragment program"); - glDeleteProgramsARB(1, &s_ColorMatrixProgram); - s_ColorMatrixProgram = 0; - } + sprintf(pmatrixprog, "!!ARBfp1.0\n" + "TEMP R0;\n" + "TEMP R1;\n" + "TEMP R2;\n" + //16777215/16777216*256, 1/255, 256, 0 + "PARAM K0 = { 255.99998474121, 0.003921568627451, 256.0, 0.0};\n" + "PARAM K1 = { 15.0, 0.066666666666, 0.0, 0.0};\n" + //sample the depth value + "TEX R2, fragment.texcoord[0], texture[0], RECT;\n" - sprintf(pmatrixprog, "!!ARBfp1.0\n" - "TEMP R0;\n" - "TEMP R1;\n" - "TEMP R2;\n" - //16777215/16777216*256, 1/255, 256, 0 - "PARAM K0 = { 255.99998474121, 0.003921568627451, 256.0, 0.0};\n" - "PARAM K1 = { 15.0, 0.066666666666, 0.0, 0.0};\n" - //sample the depth value - "TEX R2, fragment.texcoord[0], texture[0], RECT;\n" + //scale from [0*16777216..1*16777216] to + //[0*16777215..1*16777215], multiply by 256 + "MUL R0, R2.x, K0.x;\n" // *16777215/16777216*256 - //scale from [0*16777216..1*16777216] to - //[0*16777215..1*16777215], multiply by 256 - "MUL R0, R2.x, K0.x;\n" // *16777215/16777216*256 + //It is easy to get bad results due to low precision + //here, for example converting like this: + //MUL R0,R0,{ 65536, 256, 1, 16777216 } + //FRC R0,R0 + //gives {?, 128/255, 254/255, ?} for depth value 254/255 + //on some gpus - //It is easy to get bad results due to low precision - //here, for example converting like this: - //MUL R0,R0,{ 65536, 256, 1, 16777216 } - //FRC R0,R0 - //gives {?, 128/255, 254/255, ?} for depth value 254/255 - //on some gpus + "FLR R0.x,R0;\n" //bits 31..24 - "FLR R0.x,R0;\n" //bits 31..24 + "SUB R0.yzw,R0,R0.x;\n" //subtract bits 31..24 from rest + "MUL R0.yzw,R0,K0.z;\n" // *256 + "FLR R0.y,R0;\n" //bits 23..16 - "SUB R0.yzw,R0,R0.x;\n" //subtract bits 31..24 from rest - "MUL R0.yzw,R0,K0.z;\n" // *256 - "FLR R0.y,R0;\n" //bits 23..16 + "SUB R0.zw,R0,R0.y;\n" //subtract bits 23..16 from rest + "MUL R0.zw,R0,K0.z;\n" // *256 + "FLR R0.z,R0;\n" //bits 15..8 - "SUB R0.zw,R0,R0.y;\n" //subtract bits 23..16 from rest - "MUL R0.zw,R0,K0.z;\n" // *256 - "FLR R0.z,R0;\n" //bits 15..8 + "MOV R0.w,R0.x;\n" //duplicate bit 31..24 - "MOV R0.w,R0.x;\n" //duplicate bit 31..24 + "MUL R0,R0,K0.y;\n" // /255 - "MUL R0,R0,K0.y;\n" // /255 + "MUL R0.w,R0,K1.x;\n" // *15 + "FLR R0.w,R0;\n" //bits 31..28 + "MUL R0.w,R0,K1.y;\n" // /15 - "MUL R0.w,R0,K1.x;\n" // *15 - "FLR R0.w,R0;\n" //bits 31..28 - "MUL R0.w,R0,K1.y;\n" // /15 + "DP4 R1.x, R0, program.env[%d];\n" + "DP4 R1.y, R0, program.env[%d];\n" + "DP4 R1.z, R0, program.env[%d];\n" + "DP4 R1.w, R0, program.env[%d];\n" + "ADD result.color, R1, program.env[%d];\n" + "END\n", C_COLORS, C_COLORS+1, C_COLORS+2, C_COLORS+3, C_COLORS+4); + glGenProgramsARB(1, &s_DepthMatrixProgram.glprogid); + SetCurrentShader(s_DepthMatrixProgram.glprogid); + glProgramStringARB(GL_FRAGMENT_PROGRAM_ARB, GL_PROGRAM_FORMAT_ASCII_ARB, (GLsizei)strlen(pmatrixprog), pmatrixprog); - "DP4 R1.x, R0, program.env[%d];\n" - "DP4 R1.y, R0, program.env[%d];\n" - "DP4 R1.z, R0, program.env[%d];\n" - "DP4 R1.w, R0, program.env[%d];\n" - "ADD result.color, R1, program.env[%d];\n" - "END\n", C_COLORMATRIX, C_COLORMATRIX+1, C_COLORMATRIX+2, C_COLORMATRIX+3, C_COLORMATRIX+4); - glGenProgramsARB(1, &s_DepthMatrixProgram); - SetCurrentShader(s_DepthMatrixProgram); - glProgramStringARB(GL_FRAGMENT_PROGRAM_ARB, GL_PROGRAM_FORMAT_ASCII_ARB, (GLsizei)strlen(pmatrixprog), pmatrixprog); - - err = GL_REPORT_ERROR(); - if (err != GL_NO_ERROR) { - ERROR_LOG(VIDEO, "Failed to create depth matrix fragment program"); - glDeleteProgramsARB(1, &s_DepthMatrixProgram); - s_DepthMatrixProgram = 0; - } + err = GL_REPORT_ERROR(); + if (err != GL_NO_ERROR) { + ERROR_LOG(VIDEO, "Failed to create depth matrix fragment program"); + s_DepthMatrixProgram.Destroy(); + } + } } void PixelShaderCache::Shutdown() { - glDeleteProgramsARB(1, &s_ColorMatrixProgram); - s_ColorMatrixProgram = 0; - glDeleteProgramsARB(1, &s_DepthMatrixProgram); - s_DepthMatrixProgram = 0; + s_ColorMatrixProgram.Destroy(); + s_DepthMatrixProgram.Destroy(); PSCache::iterator iter = PixelShaders.begin(); for (; iter != PixelShaders.end(); iter++) iter->second.Destroy(); @@ -235,7 +291,6 @@ FRAGMENTSHADER* PixelShaderCache::SetShader(DSTALPHA_MODE dstAlphaMode, u32 comp // Make an entry in the table PSCacheEntry& newentry = PixelShaders[uid]; last_entry = &newentry; - newentry.shader.bGLSL = g_ActiveConfig.bUseGLSL; const char *code = GeneratePixelShaderCode(dstAlphaMode, g_ActiveConfig.bUseGLSL ? API_GLSL : API_OPENGL, components); if (g_ActiveConfig.bEnableShaderDebugging && code) @@ -320,7 +375,6 @@ bool CompileGLSLPixelShader(FRAGMENTSHADER& ps, const char* pstrprogram) FILE *fp = fopen(szTemp, "wb"); fwrite(pstrprogram, strlen(pstrprogram), 1, fp); fclose(fp); - if(strstr(infoLog, "warning") != NULL || strstr(infoLog, "error") != NULL) exit(0); delete[] infoLog; @@ -339,6 +393,7 @@ bool CompileGLSLPixelShader(FRAGMENTSHADER& ps, const char* pstrprogram) (void)GL_REPORT_ERROR(); ps.glprogid = result; + ps.bGLSL = true; return true; } void PixelShaderCache::SetPSSampler(const char * name, unsigned int Tex) @@ -462,6 +517,7 @@ bool CompileCGPixelShader(FRAGMENTSHADER& ps, const char* pstrprogram) } glGenProgramsARB(1, &ps.glprogid); + ps.bGLSL = false; PixelShaderCache::SetCurrentShader(ps.glprogid); glProgramStringARB(GL_FRAGMENT_PROGRAM_ARB, GL_PROGRAM_FORMAT_ASCII_ARB, (GLsizei)strlen(pcompiledprog), pcompiledprog); diff --git a/Source/Plugins/Plugin_VideoOGL/Src/TextureCache.cpp b/Source/Plugins/Plugin_VideoOGL/Src/TextureCache.cpp index 4a9431a563..d3a1283627 100644 --- a/Source/Plugins/Plugin_VideoOGL/Src/TextureCache.cpp +++ b/Source/Plugins/Plugin_VideoOGL/Src/TextureCache.cpp @@ -42,6 +42,7 @@ #include "ImageWrite.h" #include "MemoryUtil.h" #include "PixelShaderCache.h" +#include "ProgramShaderCache.h" #include "PixelShaderManager.h" #include "Render.h" #include "Statistics.h" @@ -296,7 +297,13 @@ void TextureCache::TCacheEntry::FromRenderTarget(u32 dstAddr, unsigned int dstFo glViewport(0, 0, virtualW, virtualH); - PixelShaderCache::SetCurrentShader((srcFormat == PIXELFMT_Z24) ? PixelShaderCache::GetDepthMatrixProgram() : PixelShaderCache::GetColorMatrixProgram()); + if(g_ActiveConfig.bUseGLSL) + { + ProgramShaderCache::SetBothShaders((srcFormat == PIXELFMT_Z24) ? PixelShaderCache::GetDepthMatrixProgram() : PixelShaderCache::GetColorMatrixProgram(), 0); + PixelShaderCache::SetPSSampler("samp0", 0); + } + else + PixelShaderCache::SetCurrentShader((srcFormat == PIXELFMT_Z24) ? PixelShaderCache::GetDepthMatrixProgram() : PixelShaderCache::GetColorMatrixProgram()); PixelShaderManager::SetColorMatrix(colmat); // set transformation GL_REPORT_ERRORD(); diff --git a/Source/Plugins/Plugin_VideoOGL/Src/VertexManager.cpp b/Source/Plugins/Plugin_VideoOGL/Src/VertexManager.cpp index 2c97b904b2..ebca599fe8 100644 --- a/Source/Plugins/Plugin_VideoOGL/Src/VertexManager.cpp +++ b/Source/Plugins/Plugin_VideoOGL/Src/VertexManager.cpp @@ -129,9 +129,6 @@ void VertexManager::vFlush() //glBufferData(GL_ARRAY_BUFFER, s_pCurBufferPointer - LocalVBuffer, LocalVBuffer, GL_STREAM_DRAW); GL_REPORT_ERRORD(); - // setup the pointers - if (g_nativeVertexFmt) - g_nativeVertexFmt->SetupVertexPointers(); GL_REPORT_ERRORD(); u32 usedtextures = 0; @@ -221,6 +218,21 @@ void VertexManager::vFlush() // set global constants VertexShaderManager::SetConstants(); PixelShaderManager::SetConstants(); + + // setup the pointers + if (g_nativeVertexFmt) + g_nativeVertexFmt->SetupVertexPointers(); + GL_REPORT_ERRORD(); + if(g_ActiveConfig.bUseGLSL) + for (int i = 0; i < 8; i++) + { + if (usedtextures & (1 << i)) + { + char tmp[16]; + sprintf(tmp, "samp%d", i); // Bake this in to something so we don't have to sprintf? + PixelShaderCache::SetPSSampler(tmp, i); + } + } Draw(); @@ -232,6 +244,17 @@ void VertexManager::vFlush() { ProgramShaderCache::SetBothShaders(ps->glprogid, 0); PixelShaderManager::SetConstants(); // Need to set these again + if (g_nativeVertexFmt) + g_nativeVertexFmt->SetupVertexPointers(); + for (int i = 0; i < 8; i++) + { + if (usedtextures & (1 << i)) + { + char tmp[16]; + sprintf(tmp, "samp%d", i); // Bake this in to something so we don't have to sprintf? + PixelShaderCache::SetPSSampler(tmp, i); + } + } } else if (ps) PixelShaderCache::SetCurrentShader(ps->glprogid); diff --git a/Source/Plugins/Plugin_VideoOGL/Src/VertexShaderCache.cpp b/Source/Plugins/Plugin_VideoOGL/Src/VertexShaderCache.cpp index c543dfb86e..34c8020a00 100644 --- a/Source/Plugins/Plugin_VideoOGL/Src/VertexShaderCache.cpp +++ b/Source/Plugins/Plugin_VideoOGL/Src/VertexShaderCache.cpp @@ -125,7 +125,6 @@ VERTEXSHADER* VertexShaderCache::SetShader(u32 components) // Make an entry in the table VSCacheEntry& entry = vshaders[uid]; last_entry = &entry; - entry.shader.bGLSL = g_ActiveConfig.bUseGLSL; const char *code = GenerateVertexShaderCode(components, g_ActiveConfig.bUseGLSL ? API_GLSL : API_OPENGL); GetSafeVertexShaderId(&entry.safe_uid, components); @@ -223,6 +222,7 @@ bool CompileGLSLVertexShader(VERTEXSHADER& vs, const char* pstrprogram) (void)GL_REPORT_ERROR(); vs.glprogid = result; + vs.bGLSL = true; return true; } void SetVSConstant4fvByName(const char * name, unsigned int offset, const float *f, const unsigned int count = 1) @@ -352,6 +352,7 @@ bool CompileCGVertexShader(VERTEXSHADER& vs, const char* pstrprogram) plocal = strstr(plocal + 13, "program.local"); } glGenProgramsARB(1, &vs.glprogid); + vs.bGLSL = false; VertexShaderCache::SetCurrentShader(vs.glprogid); glProgramStringARB(GL_VERTEX_PROGRAM_ARB, GL_PROGRAM_FORMAT_ASCII_ARB, (GLsizei)strlen(pcompiledprog), pcompiledprog); From ed18b82d2f9b36293f6b3ffaaeeaf958cf024144 Mon Sep 17 00:00:00 2001 From: Ryan Houdek Date: Wed, 7 Dec 2011 22:11:41 -0600 Subject: [PATCH 10/83] This lets us see stuff with GLSL shaders. Just need to take care of some compile errors now. --- Source/Plugins/Plugin_VideoOGL/Src/PixelShaderCache.cpp | 2 +- Source/Plugins/Plugin_VideoOGL/Src/VertexShaderCache.cpp | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/Source/Plugins/Plugin_VideoOGL/Src/PixelShaderCache.cpp b/Source/Plugins/Plugin_VideoOGL/Src/PixelShaderCache.cpp index 6c67252dde..b3e8b09b18 100644 --- a/Source/Plugins/Plugin_VideoOGL/Src/PixelShaderCache.cpp +++ b/Source/Plugins/Plugin_VideoOGL/Src/PixelShaderCache.cpp @@ -66,7 +66,6 @@ GLuint PixelShaderCache::GetColorMatrixProgram() void PixelShaderCache::Init() { - glEnable(GL_FRAGMENT_PROGRAM_ARB); ShaderEnabled = true; CurrentShader = 0; last_entry = NULL; @@ -87,6 +86,7 @@ void PixelShaderCache::Init() pSetPSConstant4fv = SetCGPSConstant4fv; pSetMultiPSConstant4fv = SetMultiCGPSConstant4fv; pCompilePixelShader = CompileCGPixelShader; + glEnable(GL_FRAGMENT_PROGRAM_ARB); } glGetProgramivARB(GL_FRAGMENT_PROGRAM_ARB, GL_MAX_PROGRAM_NATIVE_ALU_INSTRUCTIONS_ARB, (GLint *)&s_nMaxPixelInstructions); diff --git a/Source/Plugins/Plugin_VideoOGL/Src/VertexShaderCache.cpp b/Source/Plugins/Plugin_VideoOGL/Src/VertexShaderCache.cpp index 34c8020a00..e51e70d9eb 100644 --- a/Source/Plugins/Plugin_VideoOGL/Src/VertexShaderCache.cpp +++ b/Source/Plugins/Plugin_VideoOGL/Src/VertexShaderCache.cpp @@ -55,7 +55,6 @@ bool (*pCompileVertexShader)(VERTEXSHADER&, const char*); void VertexShaderCache::Init() { - glEnable(GL_VERTEX_PROGRAM_ARB); ShaderEnabled = true; CurrentShader = 0; last_entry = NULL; @@ -75,6 +74,7 @@ void VertexShaderCache::Init() pSetMultiVSConstant4fv = SetMultiCGVSConstant4fv; pSetMultiVSConstant3fv = SetMultiCGVSConstant3fv; pCompileVertexShader = CompileCGVertexShader; + glEnable(GL_VERTEX_PROGRAM_ARB); } glGetProgramivARB(GL_VERTEX_PROGRAM_ARB, GL_MAX_PROGRAM_NATIVE_INSTRUCTIONS_ARB, (GLint *)&s_nMaxVertexInstructions); From bf4ef054d3cf2889a7753d86ce3b582ce5349e4c Mon Sep 17 00:00:00 2001 From: Ryan Houdek Date: Wed, 7 Dec 2011 22:47:13 -0600 Subject: [PATCH 11/83] Shader Compile fixes. Played SMS for two shines. --- Source/Core/VideoCommon/Src/PixelShaderGen.cpp | 2 +- .../Core/VideoCommon/Src/TextureConversionShader.cpp | 10 ++++------ 2 files changed, 5 insertions(+), 7 deletions(-) diff --git a/Source/Core/VideoCommon/Src/PixelShaderGen.cpp b/Source/Core/VideoCommon/Src/PixelShaderGen.cpp index d3d59cde63..dcfa2da05d 100644 --- a/Source/Core/VideoCommon/Src/PixelShaderGen.cpp +++ b/Source/Core/VideoCommon/Src/PixelShaderGen.cpp @@ -750,7 +750,7 @@ const char *GeneratePixelShaderCode(DSTALPHA_MODE dstAlphaMode, API_TYPE ApiType // optional perspective divides if (xfregs.texMtxInfo[i].projection == XF_TEXPROJ_STQ) { - WRITE(p, "if (uv%d.z)", i); + WRITE(p, "if (uv%d.z != 0.0f)", i); WRITE(p, " uv%d.xy = uv%d.xy / uv%d.z;\n", i, i, i); } diff --git a/Source/Core/VideoCommon/Src/TextureConversionShader.cpp b/Source/Core/VideoCommon/Src/TextureConversionShader.cpp index ce4a5ca1f4..caa4ff8f72 100644 --- a/Source/Core/VideoCommon/Src/TextureConversionShader.cpp +++ b/Source/Core/VideoCommon/Src/TextureConversionShader.cpp @@ -125,8 +125,7 @@ void WriteSwizzler(char*& p, u32 format, API_TYPE ApiType) WRITE(p," in float2 uv0 : TEXCOORD0)\n"); } - WRITE(p," in float2 uv0 : TEXCOORD0)\n" - "{\n" + WRITE(p, "{\n" " float2 sampleUv;\n" " float2 uv1 = floor(uv0);\n"); @@ -448,10 +447,9 @@ void WriteRGB565Encoder(char* p,API_TYPE ApiType) WriteSampleColor(p, "rgb", "float3 texSample0",ApiType); WriteIncrementSampleX(p,ApiType); WriteSampleColor(p, "rgb", "float3 texSample1",ApiType); - - WRITE(p, " float2 texRs = {texSample0.r, texSample1.r};\n"); - WRITE(p, " float2 texGs = {texSample0.g, texSample1.g};\n"); - WRITE(p, " float2 texBs = {texSample0.b, texSample1.b};\n"); + WRITE(p, " float2 texRs = float2(texSample0.r, texSample1.r);\n"); + WRITE(p, " float2 texGs = float2(texSample0.g, texSample1.g);\n"); + WRITE(p, " float2 texBs = float2(texSample0.b, texSample1.b);\n"); WriteToBitDepth(p, 6, "texGs", "float2 gInt"); WRITE(p, " float2 gUpper = floor(gInt / 8.0f);\n"); From b01c97368942b8efb2ad1888b36d11d818d2c4fe Mon Sep 17 00:00:00 2001 From: Ryan Houdek Date: Wed, 7 Dec 2011 23:01:14 -0600 Subject: [PATCH 12/83] Missed one --- Source/Core/VideoCommon/Src/TextureConversionShader.cpp | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/Source/Core/VideoCommon/Src/TextureConversionShader.cpp b/Source/Core/VideoCommon/Src/TextureConversionShader.cpp index caa4ff8f72..bf45484850 100644 --- a/Source/Core/VideoCommon/Src/TextureConversionShader.cpp +++ b/Source/Core/VideoCommon/Src/TextureConversionShader.cpp @@ -210,8 +210,7 @@ void Write32BitSwizzler(char*& p, u32 format, API_TYPE ApiType) } - WRITE(p," in float2 uv0 : TEXCOORD0)\n" - "{\n" + WRITE(p, "{\n" " float2 sampleUv;\n" " float2 uv1 = floor(uv0);\n"); From 4c136c4efc40400e1f94f8dad39c71217cf4c477 Mon Sep 17 00:00:00 2001 From: Ryan Houdek Date: Wed, 7 Dec 2011 23:20:55 -0600 Subject: [PATCH 13/83] Missed a spot. Most games work now, Still have a problem with viewtiful joe. Destination Alpha pass doesn't work yet, going to use Dual source blending on that. --- .../Plugin_VideoOGL/Src/TextureConverter.cpp | 45 +++++++++++++------ 1 file changed, 32 insertions(+), 13 deletions(-) diff --git a/Source/Plugins/Plugin_VideoOGL/Src/TextureConverter.cpp b/Source/Plugins/Plugin_VideoOGL/Src/TextureConverter.cpp index 588c593b13..b5e67b9f19 100644 --- a/Source/Plugins/Plugin_VideoOGL/Src/TextureConverter.cpp +++ b/Source/Plugins/Plugin_VideoOGL/Src/TextureConverter.cpp @@ -24,6 +24,7 @@ #include "TextureConversionShader.h" #include "TextureCache.h" #include "PixelShaderCache.h" +#include "ProgramShaderCache.h" #include "VertexShaderManager.h" #include "FramebufferManager.h" #include "Globals.h" @@ -228,7 +229,7 @@ void Shutdown() s_texConvFrameBuffer = 0; } -void EncodeToRamUsingShader(FRAGMENTSHADER& shader, GLuint srcTexture, const TargetRectangle& sourceRc, +void EncodeToRamUsingShader(GLuint srcTexture, const TargetRectangle& sourceRc, u8* destAddr, int dstWidth, int dstHeight, int readStride, bool toTexture, bool linearFilter) { @@ -265,8 +266,6 @@ void EncodeToRamUsingShader(FRAGMENTSHADER& shader, GLuint srcTexture, const Tar glViewport(0, 0, (GLsizei)dstWidth, (GLsizei)dstHeight); - PixelShaderCache::SetCurrentShader(shader.glprogid); - // Draw... glBegin(GL_QUADS); glTexCoord2f((float)sourceRc.left, (float)sourceRc.top); glVertex2f(-1,-1); @@ -347,13 +346,6 @@ void EncodeToRam(u32 address, bool bFromZBuffer, bool bIsIntensityFmt, u32 copyf s32 expandedHeight = (height + blkH) & (~blkH); float sampleStride = bScaleByHalf ? 2.f : 1.f; - TextureConversionShader::SetShaderParameters( - (float)expandedWidth, - (float)Renderer::EFBToScaledY(expandedHeight), // TODO: Why do we scale this? - (float)Renderer::EFBToScaledX(source.left), - (float)Renderer::EFBToScaledY(EFB_HEIGHT - source.top - expandedHeight), - Renderer::EFBToScaledXf(sampleStride), - Renderer::EFBToScaledYf(sampleStride)); TargetRectangle scaledSource; scaledSource.top = 0; @@ -366,7 +358,23 @@ void EncodeToRam(u32 address, bool bFromZBuffer, bool bIsIntensityFmt, u32 copyf int readStride = (expandedWidth * cacheBytes) / TexDecoder_GetBlockWidthInTexels(format); g_renderer->ResetAPIState(); - EncodeToRamUsingShader(texconv_shader, source_texture, scaledSource, dest_ptr, expandedWidth / samples, expandedHeight, readStride, true, bScaleByHalf > 0); + + if(g_ActiveConfig.bUseGLSL) + ProgramShaderCache::SetBothShaders(texconv_shader.glprogid, 0); + else + PixelShaderCache::SetCurrentShader(texconv_shader.glprogid); + + TextureConversionShader::SetShaderParameters( + (float)expandedWidth, + (float)Renderer::EFBToScaledY(expandedHeight), // TODO: Why do we scale this? + (float)Renderer::EFBToScaledX(source.left), + (float)Renderer::EFBToScaledY(EFB_HEIGHT - source.top - expandedHeight), + Renderer::EFBToScaledXf(sampleStride), + Renderer::EFBToScaledYf(sampleStride)); + + EncodeToRamUsingShader(source_texture, scaledSource, dest_ptr, expandedWidth / samples, expandedHeight, readStride, true, bScaleByHalf > 0); + + FramebufferManager::SetFramebuffer(0); VertexShaderManager::SetViewportChanged(); glBindTexture(GL_TEXTURE_RECTANGLE_ARB, 0); @@ -411,6 +419,11 @@ u64 EncodeToRamFromTexture(u32 address,GLuint source_texture, bool bFromZBuffer, s32 expandedWidth = (width + blkW) & (~blkW); s32 expandedHeight = (height + blkH) & (~blkH); + if(g_ActiveConfig.bUseGLSL) + ProgramShaderCache::SetBothShaders(texconv_shader.glprogid, 0); + else + PixelShaderCache::SetCurrentShader(texconv_shader.glprogid); + float sampleStride = bScaleByHalf ? 2.f : 1.f; TextureConversionShader::SetShaderParameters((float)expandedWidth, (float)Renderer::EFBToScaledY(expandedHeight), // TODO: Why do we scale this? @@ -430,7 +443,7 @@ u64 EncodeToRamFromTexture(u32 address,GLuint source_texture, bool bFromZBuffer, int readStride = (expandedWidth * cacheBytes) / TexDecoder_GetBlockWidthInTexels(format); - EncodeToRamUsingShader(texconv_shader, source_texture, scaledSource, + EncodeToRamUsingShader(source_texture, scaledSource, dest_ptr, expandedWidth / samples, expandedHeight, readStride, true, bScaleByHalf > 0 && !bFromZBuffer); @@ -451,7 +464,13 @@ u64 EncodeToRamFromTexture(u32 address,GLuint source_texture, bool bFromZBuffer, void EncodeToRamYUYV(GLuint srcTexture, const TargetRectangle& sourceRc, u8* destAddr, int dstWidth, int dstHeight) { g_renderer->ResetAPIState(); - EncodeToRamUsingShader(s_rgbToYuyvProgram, srcTexture, sourceRc, destAddr, dstWidth / 2, dstHeight, 0, false, false); + + if(g_ActiveConfig.bUseGLSL) + ProgramShaderCache::SetBothShaders(s_rgbToYuyvProgram.glprogid, 0); + else + PixelShaderCache::SetCurrentShader(s_rgbToYuyvProgram.glprogid); + + EncodeToRamUsingShader(srcTexture, sourceRc, destAddr, dstWidth / 2, dstHeight, 0, false, false); FramebufferManager::SetFramebuffer(0); VertexShaderManager::SetViewportChanged(); glBindTexture(GL_TEXTURE_RECTANGLE_ARB, 0); From f77d54ff52c19ae7b61e2bc7e59d5de1a6e22722 Mon Sep 17 00:00:00 2001 From: Ryan Houdek Date: Wed, 7 Dec 2011 23:23:00 -0600 Subject: [PATCH 14/83] Welp, just fixed that problem. --- Source/Plugins/Plugin_VideoOGL/Src/TextureConverter.cpp | 2 ++ 1 file changed, 2 insertions(+) diff --git a/Source/Plugins/Plugin_VideoOGL/Src/TextureConverter.cpp b/Source/Plugins/Plugin_VideoOGL/Src/TextureConverter.cpp index b5e67b9f19..e1439d621c 100644 --- a/Source/Plugins/Plugin_VideoOGL/Src/TextureConverter.cpp +++ b/Source/Plugins/Plugin_VideoOGL/Src/TextureConverter.cpp @@ -263,6 +263,8 @@ void EncodeToRamUsingShader(GLuint srcTexture, const TargetRectangle& sourceRc, } GL_REPORT_ERRORD(); + if(g_ActiveConfig.bUseGLSL) + PixelShaderCache::SetPSSampler("samp0", 0); glViewport(0, 0, (GLsizei)dstWidth, (GLsizei)dstHeight); From 1201988fe4e94720f32f2165ee03d5debf957cd7 Mon Sep 17 00:00:00 2001 From: Ryan Houdek Date: Thu, 8 Dec 2011 01:51:08 -0600 Subject: [PATCH 15/83] Add support for GL_ARB_shading_language_420pack so we don't have to binding sampler locations. Also add support for GL_ARB_separate_shader_objects which doesn't currently work for some reason....investigating. --- .../Core/VideoCommon/Src/PixelShaderGen.cpp | 93 ++++++--- .../Src/TextureConversionShader.cpp | 13 +- .../Core/VideoCommon/Src/VertexShaderGen.cpp | 34 ++-- Source/Core/VideoCommon/Src/VideoConfig.h | 2 + .../Plugin_VideoOGL/Src/PixelShaderCache.cpp | 179 +++++++++++++----- .../Plugin_VideoOGL/Src/PixelShaderCache.h | 2 + .../Plugin_VideoOGL/Src/VertexShaderCache.cpp | 20 ++ 7 files changed, 252 insertions(+), 91 deletions(-) diff --git a/Source/Core/VideoCommon/Src/PixelShaderGen.cpp b/Source/Core/VideoCommon/Src/PixelShaderGen.cpp index dcfa2da05d..3d328ed70c 100644 --- a/Source/Core/VideoCommon/Src/PixelShaderGen.cpp +++ b/Source/Core/VideoCommon/Src/PixelShaderGen.cpp @@ -508,6 +508,22 @@ const char* WriteRegister(API_TYPE ApiType, const char *prefix, const u32 num) sprintf(result, " : register(%s%d)", prefix, num); return result; } +const char* WriteBinding(API_TYPE ApiType, const u32 num) +{ + if(ApiType != API_GLSL || !g_ActiveConfig.backend_info.bSupportsGLSLBinding) + return ""; + static char result[64]; + sprintf(result, "layout(binding = %d) ", num); + return result; +} +const char* WriteLocation(API_TYPE ApiType, const u32 num) +{ + if(ApiType != API_GLSL || !g_ActiveConfig.backend_info.bSupportsGLSLLocation) + return ""; + static char result[64]; + sprintf(result, "layout(location = %d) ", num); + return result; +} const char *GeneratePixelShaderCode(DSTALPHA_MODE dstAlphaMode, API_TYPE ApiType, u32 components) { @@ -537,7 +553,16 @@ const char *GeneratePixelShaderCode(DSTALPHA_MODE dstAlphaMode, API_TYPE ApiType if(ApiType == API_GLSL) { // A few required defines and ones that will make our lives a lot easier - WRITE(p, "#version 120\n"); + if (g_ActiveConfig.backend_info.bSupportsGLSLBinding || g_ActiveConfig.backend_info.bSupportsGLSLLocation) + { + WRITE(p, "#version 330 compatibility\n"); + if (g_ActiveConfig.backend_info.bSupportsGLSLBinding) + WRITE(p, "#extension GL_ARB_shading_language_420pack : enable\n"); + if (g_ActiveConfig.backend_info.bSupportsGLSLLocation) + WRITE(p, "#extension GL_ARB_separate_shader_objects : enable\n"); + } + else + WRITE(p, "#version 120\n"); // Silly differences WRITE(p, "#define float2 vec2\n"); WRITE(p, "#define float3 vec3\n"); @@ -547,52 +572,58 @@ const char *GeneratePixelShaderCode(DSTALPHA_MODE dstAlphaMode, API_TYPE ApiType WRITE(p, "#define frac(x) fract(x)\n"); WRITE(p, "#define saturate(x) clamp(x, 0.0f, 1.0f)\n"); WRITE(p, "#define lerp(x, y, z) mix(x, y, z)\n"); - } - // Declare samplers - - if(ApiType != API_D3D11) - { - WRITE(p, "uniform sampler2D "); + + for (int i = 0; i < 8; ++i) + WRITE(p, "%suniform sampler2D samp%d;\n", WriteBinding(ApiType, i), i); } else { - WRITE(p, "sampler "); - } + // Declare samplers - bool bfirst = true; - for (int i = 0; i < 8; ++i) - { - WRITE(p, "%s samp%d %s", bfirst?"":",", i, WriteRegister(ApiType, "s", i)); - bfirst = false; - } - WRITE(p, ";\n"); - if(ApiType == API_D3D11) - { - WRITE(p, "Texture2D "); - bfirst = true; + if(ApiType != API_D3D11) + { + WRITE(p, "uniform sampler2D "); + } + else + { + WRITE(p, "sampler "); + } + + bool bfirst = true; for (int i = 0; i < 8; ++i) { - WRITE(p, "%s Tex%d : register(t%d)", bfirst?"":",", i, i); + WRITE(p, "%s samp%d %s", bfirst?"":",", i, WriteRegister(ApiType, "s", i)); bfirst = false; } WRITE(p, ";\n"); + if(ApiType == API_D3D11) + { + WRITE(p, "Texture2D "); + bfirst = true; + for (int i = 0; i < 8; ++i) + { + WRITE(p, "%s Tex%d : register(t%d)", bfirst?"":",", i, i); + bfirst = false; + } + WRITE(p, ";\n"); + } } WRITE(p, "\n"); - WRITE(p, "uniform float4 "I_COLORS"[4] %s;\n", WriteRegister(ApiType, "c", C_COLORS)); - WRITE(p, "uniform float4 "I_KCOLORS"[4] %s;\n", WriteRegister(ApiType, "c", C_KCOLORS)); - WRITE(p, "uniform float4 "I_ALPHA"[1] %s;\n", WriteRegister(ApiType, "c", C_ALPHA)); - WRITE(p, "uniform float4 "I_TEXDIMS"[8] %s;\n", WriteRegister(ApiType, "c", C_TEXDIMS)); - WRITE(p, "uniform float4 "I_ZBIAS"[2] %s;\n", WriteRegister(ApiType, "c", C_ZBIAS)); - WRITE(p, "uniform float4 "I_INDTEXSCALE"[2] %s;\n", WriteRegister(ApiType, "c", C_INDTEXSCALE)); - WRITE(p, "uniform float4 "I_INDTEXMTX"[6] %s;\n", WriteRegister(ApiType, "c", C_INDTEXMTX)); - WRITE(p, "uniform float4 "I_FOG"[3] %s;\n", WriteRegister(ApiType, "c", C_FOG)); + WRITE(p, "%suniform float4 "I_COLORS"[4] %s;\n", WriteLocation(ApiType, C_COLORS), WriteRegister(ApiType, "c", C_COLORS)); + WRITE(p, "%suniform float4 "I_KCOLORS"[4] %s;\n", WriteLocation(ApiType, C_KCOLORS), WriteRegister(ApiType, "c", C_KCOLORS)); + WRITE(p, "%suniform float4 "I_ALPHA"[1] %s;\n", WriteLocation(ApiType, C_ALPHA), WriteRegister(ApiType, "c", C_ALPHA)); + WRITE(p, "%suniform float4 "I_TEXDIMS"[8] %s;\n", WriteLocation(ApiType, C_TEXDIMS), WriteRegister(ApiType, "c", C_TEXDIMS)); + WRITE(p, "%suniform float4 "I_ZBIAS"[2] %s;\n", WriteLocation(ApiType, C_ZBIAS), WriteRegister(ApiType, "c", C_ZBIAS)); + WRITE(p, "%suniform float4 "I_INDTEXSCALE"[2] %s;\n", WriteLocation(ApiType, C_INDTEXSCALE), WriteRegister(ApiType, "c", C_INDTEXSCALE)); + WRITE(p, "%suniform float4 "I_INDTEXMTX"[6] %s;\n", WriteLocation(ApiType, C_INDTEXMTX), WriteRegister(ApiType, "c", C_INDTEXMTX)); + WRITE(p, "%suniform float4 "I_FOG"[3] %s;\n", WriteLocation(ApiType, C_FOG), WriteRegister(ApiType, "c", C_FOG)); if(g_ActiveConfig.bEnablePixelLighting && g_ActiveConfig.backend_info.bSupportsPixelLighting) { - WRITE(p, "uniform float4 "I_PLIGHTS"[40] %s;\n", WriteRegister(ApiType, "c", C_PLIGHTS)); - WRITE(p, "uniform float4 "I_PMATERIALS"[4] %s;\n", WriteRegister(ApiType, "c", C_PMATERIALS)); + WRITE(p, "%suniform float4 "I_PLIGHTS"[40] %s;\n", WriteLocation(ApiType, C_PLIGHTS), WriteRegister(ApiType, "c", C_PLIGHTS)); + WRITE(p, "%suniform float4 "I_PMATERIALS"[4] %s;\n", WriteLocation(ApiType, C_PMATERIALS), WriteRegister(ApiType, "c", C_PMATERIALS)); } if(ApiType != API_GLSL) diff --git a/Source/Core/VideoCommon/Src/TextureConversionShader.cpp b/Source/Core/VideoCommon/Src/TextureConversionShader.cpp index bf45484850..1be760a608 100644 --- a/Source/Core/VideoCommon/Src/TextureConversionShader.cpp +++ b/Source/Core/VideoCommon/Src/TextureConversionShader.cpp @@ -26,6 +26,7 @@ #include "PixelShaderGen.h" #include "BPMemory.h" #include "RenderBase.h" +#include "VideoConfig.h" #define WRITE p+=sprintf @@ -92,6 +93,8 @@ void WriteSwizzler(char*& p, u32 format, API_TYPE ApiType) } else if (ApiType == API_GLSL) { + if (g_ActiveConfig.backend_info.bSupportsGLSLBinding) + WRITE(p, "layout(binding = 0) "); WRITE(p, "uniform sampler2DRect samp0;\n"); } else if (ApiType & API_D3D9) @@ -177,6 +180,8 @@ void Write32BitSwizzler(char*& p, u32 format, API_TYPE ApiType) } else if (ApiType == API_GLSL) { + if (g_ActiveConfig.backend_info.bSupportsGLSLBinding) + WRITE(p, "layout(binding = 0) "); WRITE(p, "uniform sampler2DRect samp0;\n"); } else if (ApiType & API_D3D9) @@ -834,7 +839,13 @@ const char *GenerateEncodingShader(u32 format,API_TYPE ApiType) if(ApiType == API_GLSL) { // A few required defines and ones that will make our lives a lot easier - WRITE(p, "#version 120\n"); + if (g_ActiveConfig.backend_info.bSupportsGLSLBinding) + { + WRITE(p, "#version 330 compatibility\n"); + WRITE(p, "#extension GL_ARB_shading_language_420pack : enable\n"); + } + else + WRITE(p, "#version 120\n"); // Silly differences WRITE(p, "#define float2 vec2\n"); WRITE(p, "#define float3 vec3\n"); diff --git a/Source/Core/VideoCommon/Src/VertexShaderGen.cpp b/Source/Core/VideoCommon/Src/VertexShaderGen.cpp index 9a6a220979..78feff6541 100644 --- a/Source/Core/VideoCommon/Src/VertexShaderGen.cpp +++ b/Source/Core/VideoCommon/Src/VertexShaderGen.cpp @@ -164,7 +164,8 @@ char* GenerateVSOutputStruct(char* p, u32 components, API_TYPE ApiType) } extern const char* WriteRegister(API_TYPE ApiType, const char *prefix, const u32 num); - +extern const char* WriteBinding(API_TYPE ApiType, const u32 num); +extern const char* WriteLocation(API_TYPE ApiType, const u32 num); const char *GenerateVertexShaderCode(u32 components, API_TYPE ApiType) { @@ -187,7 +188,16 @@ const char *GenerateVertexShaderCode(u32 components, API_TYPE ApiType) if(ApiType == API_GLSL) { // A few required defines and ones that will make our lives a lot easier - WRITE(p, "#version 120\n"); + if (g_ActiveConfig.backend_info.bSupportsGLSLBinding || g_ActiveConfig.backend_info.bSupportsGLSLLocation) + { + WRITE(p, "#version 330 compatibility\n"); + if (g_ActiveConfig.backend_info.bSupportsGLSLBinding) + WRITE(p, "#extension GL_ARB_shading_language_420pack : enable\n"); + if (g_ActiveConfig.backend_info.bSupportsGLSLLocation) + WRITE(p, "#extension GL_ARB_separate_shader_objects : enable\n"); + } + else + WRITE(p, "#version 120\n"); // Silly differences WRITE(p, "#define float2 vec2\n"); WRITE(p, "#define float3 vec3\n"); @@ -199,15 +209,15 @@ const char *GenerateVertexShaderCode(u32 components, API_TYPE ApiType) WRITE(p, "#define lerp(x, y, z) mix(x, y, z)\n"); } // uniforms - WRITE(p, "uniform float4 "I_TRANSFORMMATRICES"[64] %s;\n", WriteRegister(ApiType, "c", C_TRANSFORMMATRICES)); - WRITE(p, "uniform float4 "I_TEXMATRICES"[24] %s;\n", WriteRegister(ApiType, "c", C_TEXMATRICES)); // also using tex matrices - WRITE(p, "uniform float4 "I_NORMALMATRICES"[32] %s;\n", WriteRegister(ApiType, "c", C_NORMALMATRICES)); - WRITE(p, "uniform float4 "I_POSNORMALMATRIX"[6] %s;\n", WriteRegister(ApiType, "c", C_POSNORMALMATRIX)); - WRITE(p, "uniform float4 "I_POSTTRANSFORMMATRICES"[64] %s;\n", WriteRegister(ApiType, "c", C_POSTTRANSFORMMATRICES)); - WRITE(p, "uniform float4 "I_LIGHTS"[40] %s;\n", WriteRegister(ApiType, "c", C_LIGHTS)); - WRITE(p, "uniform float4 "I_MATERIALS"[4] %s;\n", WriteRegister(ApiType, "c", C_MATERIALS)); - WRITE(p, "uniform float4 "I_PROJECTION"[4] %s;\n", WriteRegister(ApiType, "c", C_PROJECTION)); - WRITE(p, "uniform float4 "I_DEPTHPARAMS" %s;\n", WriteRegister(ApiType, "c", C_DEPTHPARAMS)); + WRITE(p, "%suniform float4 "I_TRANSFORMMATRICES"[64] %s;\n", WriteLocation(ApiType, C_TRANSFORMMATRICES), WriteRegister(ApiType, "c", C_TRANSFORMMATRICES)); + WRITE(p, "%suniform float4 "I_TEXMATRICES"[24] %s;\n", WriteLocation(ApiType, C_TEXMATRICES), WriteRegister(ApiType, "c", C_TEXMATRICES)); // also using tex matrices + WRITE(p, "%suniform float4 "I_NORMALMATRICES"[32] %s;\n", WriteLocation(ApiType, C_NORMALMATRICES), WriteRegister(ApiType, "c", C_NORMALMATRICES)); + WRITE(p, "%suniform float4 "I_POSNORMALMATRIX"[6] %s;\n", WriteLocation(ApiType, C_POSNORMALMATRIX), WriteRegister(ApiType, "c", C_POSNORMALMATRIX)); + WRITE(p, "%suniform float4 "I_POSTTRANSFORMMATRICES"[64] %s;\n", WriteLocation(ApiType, C_POSTTRANSFORMMATRICES), WriteRegister(ApiType, "c", C_POSTTRANSFORMMATRICES)); + WRITE(p, "%suniform float4 "I_LIGHTS"[40] %s;\n", WriteLocation(ApiType, C_LIGHTS), WriteRegister(ApiType, "c", C_LIGHTS)); + WRITE(p, "%suniform float4 "I_MATERIALS"[4] %s;\n", WriteLocation(ApiType, C_MATERIALS), WriteRegister(ApiType, "c", C_MATERIALS)); + WRITE(p, "%suniform float4 "I_PROJECTION"[4] %s;\n", WriteLocation(ApiType, C_PROJECTION), WriteRegister(ApiType, "c", C_PROJECTION)); + WRITE(p, "%suniform float4 "I_DEPTHPARAMS" %s;\n", WriteLocation(ApiType, C_DEPTHPARAMS), WriteRegister(ApiType, "c", C_DEPTHPARAMS)); p = GenerateVSOutputStruct(p, components, ApiType); @@ -403,7 +413,7 @@ const char *GenerateVertexShaderCode(u32 components, API_TYPE ApiType) if (components & (VB_HAS_NRM1|VB_HAS_NRM2)) { // transform the light dir into tangent space - WRITE(p, "ldir = normalize("I_LIGHTS".lights[%d].pos.xyz - pos.xyz);\n", texinfo.embosslightshift); + WRITE(p, "ldir = normalize("I_LIGHTS"[%d + 3].xyz - pos.xyz);\n", texinfo.embosslightshift); WRITE(p, "o.tex%d.xyz = o.tex%d.xyz + float3(dot(ldir, _norm1), dot(ldir, _norm2), 0.0f);\n", i, texinfo.embosssourceshift); } else diff --git a/Source/Core/VideoCommon/Src/VideoConfig.h b/Source/Core/VideoCommon/Src/VideoConfig.h index 63df7e0ded..414086ed15 100644 --- a/Source/Core/VideoCommon/Src/VideoConfig.h +++ b/Source/Core/VideoCommon/Src/VideoConfig.h @@ -168,6 +168,8 @@ struct VideoConfig bool bSupportsFormatReinterpretation; bool bSupportsPixelLighting; bool bSupportsGLSL; + bool bSupportsGLSLBinding; + bool bSupportsGLSLLocation; } backend_info; }; diff --git a/Source/Plugins/Plugin_VideoOGL/Src/PixelShaderCache.cpp b/Source/Plugins/Plugin_VideoOGL/Src/PixelShaderCache.cpp index b3e8b09b18..5ee839676c 100644 --- a/Source/Plugins/Plugin_VideoOGL/Src/PixelShaderCache.cpp +++ b/Source/Plugins/Plugin_VideoOGL/Src/PixelShaderCache.cpp @@ -63,7 +63,6 @@ GLuint PixelShaderCache::GetColorMatrixProgram() { return s_ColorMatrixProgram.glprogid; } - void PixelShaderCache::Init() { ShaderEnabled = true; @@ -79,6 +78,12 @@ void PixelShaderCache::Init() pSetPSConstant4fv = SetGLSLPSConstant4fv; pSetMultiPSConstant4fv = SetMultiGLSLPSConstant4fv; pCompilePixelShader = CompileGLSLPixelShader; + // Should this be set here? + if (strstr((const char*)glGetString(GL_EXTENSIONS), "GL_ARB_shading_language_420pack") != NULL) + g_ActiveConfig.backend_info.bSupportsGLSLBinding = true; + // This bit doesn't quite work yet, always set to false + //if (strstr((const char*)glGetString(GL_EXTENSIONS), "GL_ARB_separate_shader_objects") != NULL) + g_ActiveConfig.backend_info.bSupportsGLSLLocation = false; } else { @@ -108,56 +113,118 @@ void PixelShaderCache::Init() if(g_ActiveConfig.bUseGLSL) { char pmatrixprog[2048]; - sprintf(pmatrixprog, "#extension GL_ARB_texture_rectangle : enable\n" - "uniform sampler2DRect samp0;\n" - "uniform vec4 "I_COLORS"[7];\n" - "void main(){\n" - "vec4 Temp0, Temp1;\n" - "vec4 K0 = vec4(0.5, 0.5, 0.5, 0.5);\n" - "Temp0 = texture2DRect(samp0, gl_TexCoord[0].xy);\n" - "Temp0 = Temp0 * "I_COLORS"[%d];\n" - "Temp0 = Temp0 + K0;\n" - "Temp0 = floor(Temp0);\n" - "Temp0 = Temp0 * "I_COLORS"[%d];\n" - "Temp1.x = dot(Temp0, "I_COLORS"[%d]);\n" - "Temp1.y = dot(Temp0, "I_COLORS"[%d]);\n" - "Temp1.z = dot(Temp0, "I_COLORS"[%d]);\n" - "Temp1.w = dot(Temp0, "I_COLORS"[%d]);\n" - "gl_FragData[0] = Temp1 + "I_COLORS"[%d];\n" - "}\n", C_COLORS+5, C_COLORS+6, C_COLORS, C_COLORS+1, C_COLORS+2, C_COLORS+3, C_COLORS+4); - if(!pCompilePixelShader(s_ColorMatrixProgram, pmatrixprog)) + if (g_ActiveConfig.backend_info.bSupportsGLSLBinding) + { + sprintf(pmatrixprog, "#version 330 compatibility\n" + "#extension GL_ARB_texture_rectangle : enable\n" + "#extension GL_ARB_shading_language_420pack : enable\n" + "layout(binding = 0) uniform sampler2DRect samp0;\n" + "uniform vec4 "I_COLORS"[7];\n" + "void main(){\n" + "vec4 Temp0, Temp1;\n" + "vec4 K0 = vec4(0.5, 0.5, 0.5, 0.5);\n" + "Temp0 = texture2DRect(samp0, gl_TexCoord[0].xy);\n" + "Temp0 = Temp0 * "I_COLORS"[%d];\n" + "Temp0 = Temp0 + K0;\n" + "Temp0 = floor(Temp0);\n" + "Temp0 = Temp0 * "I_COLORS"[%d];\n" + "Temp1.x = dot(Temp0, "I_COLORS"[%d]);\n" + "Temp1.y = dot(Temp0, "I_COLORS"[%d]);\n" + "Temp1.z = dot(Temp0, "I_COLORS"[%d]);\n" + "Temp1.w = dot(Temp0, "I_COLORS"[%d]);\n" + "gl_FragData[0] = Temp1 + "I_COLORS"[%d];\n" + "}\n", C_COLORS+5, C_COLORS+6, C_COLORS, C_COLORS+1, C_COLORS+2, C_COLORS+3, C_COLORS+4); + } + else + { + sprintf(pmatrixprog, "#version 120\n" + "#extension GL_ARB_texture_rectangle : enable\n" + "uniform sampler2DRect samp0;\n" + "uniform vec4 "I_COLORS"[7];\n" + "void main(){\n" + "vec4 Temp0, Temp1;\n" + "vec4 K0 = vec4(0.5, 0.5, 0.5, 0.5);\n" + "Temp0 = texture2DRect(samp0, gl_TexCoord[0].xy);\n" + "Temp0 = Temp0 * "I_COLORS"[%d];\n" + "Temp0 = Temp0 + K0;\n" + "Temp0 = floor(Temp0);\n" + "Temp0 = Temp0 * "I_COLORS"[%d];\n" + "Temp1.x = dot(Temp0, "I_COLORS"[%d]);\n" + "Temp1.y = dot(Temp0, "I_COLORS"[%d]);\n" + "Temp1.z = dot(Temp0, "I_COLORS"[%d]);\n" + "Temp1.w = dot(Temp0, "I_COLORS"[%d]);\n" + "gl_FragData[0] = Temp1 + "I_COLORS"[%d];\n" + "}\n", C_COLORS+5, C_COLORS+6, C_COLORS, C_COLORS+1, C_COLORS+2, C_COLORS+3, C_COLORS+4); + } + if (!pCompilePixelShader(s_ColorMatrixProgram, pmatrixprog)) { ERROR_LOG(VIDEO, "Failed to create color matrix fragment program"); s_ColorMatrixProgram.Destroy(); } - sprintf(pmatrixprog, "#extension GL_ARB_texture_rectangle : enable\n" - "uniform sampler2DRect samp0;\n" - "uniform vec4 "I_COLORS"[5];\n" - "void main(){\n" - "vec4 R0, R1, R2;\n" - "vec4 K0 = vec4(255.99998474121, 0.003921568627451, 256.0, 0.0);\n" - "vec4 K1 = vec4(15.0, 0.066666666666, 0.0, 0.0);\n" - "R2 = texture2DRect(samp0, gl_TexCoord[0].xy);\n" - "R0.x = R2.x * K0.x;\n" - "R0.x = floor(R0).x;\n" - "R0.yzw = (R0 - R0.x).yzw;\n" - "R0.yzw = (R0 * K0.z).yzw;\n" - "R0.y = floor(R0).y;\n" - "R0.zw = (R0 - R0.y).zw;\n" - "R0.zw = (R0 * K0.z).zw;\n" - "R0.z = floor(R0).z;\n" - "R0.w = R0.x;\n" - "R0 = R0 * K0.y;\n" - "R0.w = (R0 * K1.x).w;\n" - "R0.w = floor(R0).w;\n" - "R0.w = (R0 * K1.y).w;\n" - "R1.x = dot(R0, "I_COLORS"[%d]);\n" - "R1.y = dot(R0, "I_COLORS"[%d]);\n" - "R1.z = dot(R0, "I_COLORS"[%d]);\n" - "R1.w = dot(R0, "I_COLORS"[%d]);\n" - "gl_FragData[0] = R1 * "I_COLORS"[%d];\n" - "}\n", C_COLORS, C_COLORS+1, C_COLORS+2, C_COLORS+3, C_COLORS+4); - if(!pCompilePixelShader(s_DepthMatrixProgram, pmatrixprog)) + if (g_ActiveConfig.backend_info.bSupportsGLSLBinding) + { + sprintf(pmatrixprog, "#version 330 compatibility\n" + "#extension GL_ARB_texture_rectangle : enable\n" + "#extension GL_ARB_shading_language_420pack : enable\n" + "layout(binding = 0) uniform sampler2DRect samp0;\n" + "uniform vec4 "I_COLORS"[5];\n" + "void main(){\n" + "vec4 R0, R1, R2;\n" + "vec4 K0 = vec4(255.99998474121, 0.003921568627451, 256.0, 0.0);\n" + "vec4 K1 = vec4(15.0, 0.066666666666, 0.0, 0.0);\n" + "R2 = texture2DRect(samp0, gl_TexCoord[0].xy);\n" + "R0.x = R2.x * K0.x;\n" + "R0.x = floor(R0).x;\n" + "R0.yzw = (R0 - R0.x).yzw;\n" + "R0.yzw = (R0 * K0.z).yzw;\n" + "R0.y = floor(R0).y;\n" + "R0.zw = (R0 - R0.y).zw;\n" + "R0.zw = (R0 * K0.z).zw;\n" + "R0.z = floor(R0).z;\n" + "R0.w = R0.x;\n" + "R0 = R0 * K0.y;\n" + "R0.w = (R0 * K1.x).w;\n" + "R0.w = floor(R0).w;\n" + "R0.w = (R0 * K1.y).w;\n" + "R1.x = dot(R0, "I_COLORS"[%d]);\n" + "R1.y = dot(R0, "I_COLORS"[%d]);\n" + "R1.z = dot(R0, "I_COLORS"[%d]);\n" + "R1.w = dot(R0, "I_COLORS"[%d]);\n" + "gl_FragData[0] = R1 * "I_COLORS"[%d];\n" + "}\n", C_COLORS, C_COLORS+1, C_COLORS+2, C_COLORS+3, C_COLORS+4); + } + else + { + sprintf(pmatrixprog, "#version 120\n" + "#extension GL_ARB_texture_rectangle : enable\n" + "uniform sampler2DRect samp0;\n" + "uniform vec4 "I_COLORS"[5];\n" + "void main(){\n" + "vec4 R0, R1, R2;\n" + "vec4 K0 = vec4(255.99998474121, 0.003921568627451, 256.0, 0.0);\n" + "vec4 K1 = vec4(15.0, 0.066666666666, 0.0, 0.0);\n" + "R2 = texture2DRect(samp0, gl_TexCoord[0].xy);\n" + "R0.x = R2.x * K0.x;\n" + "R0.x = floor(R0).x;\n" + "R0.yzw = (R0 - R0.x).yzw;\n" + "R0.yzw = (R0 * K0.z).yzw;\n" + "R0.y = floor(R0).y;\n" + "R0.zw = (R0 - R0.y).zw;\n" + "R0.zw = (R0 * K0.z).zw;\n" + "R0.z = floor(R0).z;\n" + "R0.w = R0.x;\n" + "R0 = R0 * K0.y;\n" + "R0.w = (R0 * K1.x).w;\n" + "R0.w = floor(R0).w;\n" + "R0.w = (R0 * K1.y).w;\n" + "R1.x = dot(R0, "I_COLORS"[%d]);\n" + "R1.y = dot(R0, "I_COLORS"[%d]);\n" + "R1.z = dot(R0, "I_COLORS"[%d]);\n" + "R1.w = dot(R0, "I_COLORS"[%d]);\n" + "gl_FragData[0] = R1 * "I_COLORS"[%d];\n" + "}\n", C_COLORS, C_COLORS+1, C_COLORS+2, C_COLORS+3, C_COLORS+4); + } + if (!pCompilePixelShader(s_DepthMatrixProgram, pmatrixprog)) { ERROR_LOG(VIDEO, "Failed to create depth matrix fragment program"); s_DepthMatrixProgram.Destroy(); @@ -398,6 +465,8 @@ bool CompileGLSLPixelShader(FRAGMENTSHADER& ps, const char* pstrprogram) } void PixelShaderCache::SetPSSampler(const char * name, unsigned int Tex) { + if(g_ActiveConfig.backend_info.bSupportsGLSLBinding) + return; PROGRAMSHADER tmp = ProgramShaderCache::GetShaderProgram(); for(int a = 0; a < NUM_UNIFORMS; ++a) if(!strcmp(name, UniformNames[a])) @@ -429,6 +498,12 @@ void SetPSConstant4fvByName(const char * name, unsigned int offset, const float void SetGLSLPSConstant4f(unsigned int const_number, float f1, float f2, float f3, float f4) { float f[4] = { f1, f2, f3, f4 }; + + if(g_ActiveConfig.backend_info.bSupportsGLSLLocation) + { + glUniform4fv(const_number, 1, f); + return; + } for( unsigned int a = 0; a < 10; ++a) { if( const_number >= PSVar_Loc[a].reg && const_number < (PSVar_Loc[a].reg + PSVar_Loc[a].size)) @@ -442,6 +517,11 @@ void SetGLSLPSConstant4f(unsigned int const_number, float f1, float f2, float f3 void SetGLSLPSConstant4fv(unsigned int const_number, const float *f) { + if(g_ActiveConfig.backend_info.bSupportsGLSLLocation) + { + glUniform4fv(const_number, 1, f); + return; + } for( unsigned int a = 0; a < 10; ++a) { if( const_number >= PSVar_Loc[a].reg && const_number < (PSVar_Loc[a].reg + PSVar_Loc[a].size)) @@ -455,6 +535,11 @@ void SetGLSLPSConstant4fv(unsigned int const_number, const float *f) void SetMultiGLSLPSConstant4fv(unsigned int const_number, unsigned int count, const float *f) { + if(g_ActiveConfig.backend_info.bSupportsGLSLLocation) + { + glUniform4fv(const_number, count, f); + return; + } for( unsigned int a = 0; a < 10; ++a) { if( const_number >= PSVar_Loc[a].reg && const_number < (PSVar_Loc[a].reg + PSVar_Loc[a].size)) diff --git a/Source/Plugins/Plugin_VideoOGL/Src/PixelShaderCache.h b/Source/Plugins/Plugin_VideoOGL/Src/PixelShaderCache.h index d5e2cae6bc..6547b95234 100644 --- a/Source/Plugins/Plugin_VideoOGL/Src/PixelShaderCache.h +++ b/Source/Plugins/Plugin_VideoOGL/Src/PixelShaderCache.h @@ -88,6 +88,8 @@ public: static GLuint GetDepthMatrixProgram(); + static bool SupportsBinding(); + static void SetCurrentShader(GLuint Shader); static void DisableShader(); diff --git a/Source/Plugins/Plugin_VideoOGL/Src/VertexShaderCache.cpp b/Source/Plugins/Plugin_VideoOGL/Src/VertexShaderCache.cpp index e51e70d9eb..143340277e 100644 --- a/Source/Plugins/Plugin_VideoOGL/Src/VertexShaderCache.cpp +++ b/Source/Plugins/Plugin_VideoOGL/Src/VertexShaderCache.cpp @@ -247,6 +247,11 @@ void SetGLSLVSConstant4f(unsigned int const_number, float f1, float f2, float f3 buf[1] = f2; buf[2] = f3; buf[3] = f4; + if(g_ActiveConfig.backend_info.bSupportsGLSLLocation) + { + glUniform4fv(const_number, 1, buf); + return; + } for( unsigned int a = 0; a < 9; ++a) { if( const_number >= VSVar_Loc[a].reg && const_number < ( VSVar_Loc[a].reg + VSVar_Loc[a].size)) @@ -260,6 +265,11 @@ void SetGLSLVSConstant4f(unsigned int const_number, float f1, float f2, float f3 void SetGLSLVSConstant4fv(unsigned int const_number, const float *f) { + if(g_ActiveConfig.backend_info.bSupportsGLSLLocation) + { + glUniform4fv(const_number, 1, f); + return; + } for( unsigned int a = 0; a < 9; ++a) { if( const_number >= VSVar_Loc[a].reg && const_number < ( VSVar_Loc[a].reg + VSVar_Loc[a].size)) @@ -273,6 +283,11 @@ void SetGLSLVSConstant4fv(unsigned int const_number, const float *f) void SetMultiGLSLVSConstant4fv(unsigned int const_number, unsigned int count, const float *f) { + if(g_ActiveConfig.backend_info.bSupportsGLSLLocation) + { + glUniform4fv(const_number, count, f); + return; + } for( unsigned int a = 0; a < 9; ++a) { if( const_number >= VSVar_Loc[a].reg && const_number < ( VSVar_Loc[a].reg + VSVar_Loc[a].size)) @@ -294,6 +309,11 @@ void SetMultiGLSLVSConstant3fv(unsigned int const_number, unsigned int count, co buf[4*i+2] = *f++; buf[4*i+3] = 0.f; } + if(g_ActiveConfig.backend_info.bSupportsGLSLLocation) + { + glUniform4fv(const_number, count, buf); + return; + } for( unsigned int a = 0; a < 9; ++a) { if( const_number >= VSVar_Loc[a].reg && const_number < ( VSVar_Loc[a].reg + VSVar_Loc[a].size)) From cecc3c3873c6aaabea514b3a5eff380e5a32856f Mon Sep 17 00:00:00 2001 From: Ryan Houdek Date: Thu, 8 Dec 2011 03:20:31 -0600 Subject: [PATCH 16/83] Bit of cleanup. Clean up my massive hack in the ShaderManagerFiles. Almost feature parity with Nvidia CG now I think. Just need to do Alpha test with Dual source blending now. --- .../Core/VideoCommon/Src/PixelShaderGen.cpp | 3 +- .../VideoCommon/Src/PixelShaderManager.cpp | 38 ++++++++++--------- .../Src/TextureConversionShader.cpp | 10 ++--- .../Core/VideoCommon/Src/VertexShaderGen.cpp | 12 ++++-- .../VideoCommon/Src/VertexShaderManager.cpp | 24 ++++++------ .../Plugin_VideoOGL/Src/PixelShaderCache.cpp | 5 ++- 6 files changed, 52 insertions(+), 40 deletions(-) diff --git a/Source/Core/VideoCommon/Src/PixelShaderGen.cpp b/Source/Core/VideoCommon/Src/PixelShaderGen.cpp index 3d328ed70c..584254e371 100644 --- a/Source/Core/VideoCommon/Src/PixelShaderGen.cpp +++ b/Source/Core/VideoCommon/Src/PixelShaderGen.cpp @@ -891,7 +891,8 @@ const char *GeneratePixelShaderCode(DSTALPHA_MODE dstAlphaMode, API_TYPE ApiType if(ApiType == API_GLSL) { if(DepthTextureEnable) - WRITE(p, "gl_FragDepth = depth;\n"); + WRITE(p, "gl_FragDepth = depth;\n"); + WRITE(p, "gl_FragData[0] = ocol0;\n"); } } diff --git a/Source/Core/VideoCommon/Src/PixelShaderManager.cpp b/Source/Core/VideoCommon/Src/PixelShaderManager.cpp index 4f6cd8da2d..3d4ddd2f35 100644 --- a/Source/Core/VideoCommon/Src/PixelShaderManager.cpp +++ b/Source/Core/VideoCommon/Src/PixelShaderManager.cpp @@ -85,37 +85,39 @@ void PixelShaderManager::Shutdown() void PixelShaderManager::SetConstants() { + if (g_ActiveConfig.bUseGLSL) + Dirty(); for (int i = 0; i < 2; ++i) { - if (s_nColorsChanged[i] || g_ActiveConfig.bUseGLSL) + if (s_nColorsChanged[i]) { int baseind = i ? C_KCOLORS : C_COLORS; for (int j = 0; j < 4; ++j) { - if (s_nColorsChanged[i] & (1 << j) || g_ActiveConfig.bUseGLSL) + if (s_nColorsChanged[i] & (1 << j)) SetPSConstant4fv(baseind+j, &lastRGBAfull[i][j][0]); } s_nColorsChanged[i] = 0; } } - if (s_nTexDimsChanged || g_ActiveConfig.bUseGLSL) + if (s_nTexDimsChanged) { for (int i = 0; i < 8; ++i) { - if (s_nTexDimsChanged & (1<>8)&0xff)/255.0f, 0, ((lastAlpha>>16)&0xff)/255.0f); s_bAlphaChanged = false; } - if (s_bZTextureTypeChanged || g_ActiveConfig.bUseGLSL) + if (s_bZTextureTypeChanged) { float ftemp[4]; switch (bpmem.ztex2.type) @@ -137,7 +139,7 @@ void PixelShaderManager::SetConstants() s_bZTextureTypeChanged = false; } - if (s_bZBiasChanged || s_bDepthRangeChanged || g_ActiveConfig.bUseGLSL) + if (s_bZBiasChanged || s_bDepthRangeChanged) { // reversed gxsetviewport(xorig, yorig, width, height, nearz, farz) // [0] = width/2 @@ -153,12 +155,12 @@ void PixelShaderManager::SetConstants() } // indirect incoming texture scales - if (s_nIndTexScaleChanged || g_ActiveConfig.bUseGLSL) + if (s_nIndTexScaleChanged) { // set as two sets of vec4s, each containing S and T of two ind stages. float f[8]; - if (s_nIndTexScaleChanged & 0x03 || g_ActiveConfig.bUseGLSL) + if (s_nIndTexScaleChanged & 0x03) { for (u32 i = 0; i < 2; ++i) { @@ -169,7 +171,7 @@ void PixelShaderManager::SetConstants() SetPSConstant4fv(C_INDTEXSCALE, f); } - if (s_nIndTexScaleChanged & 0x0c || g_ActiveConfig.bUseGLSL) { + if (s_nIndTexScaleChanged & 0x0c) { for (u32 i = 2; i < 4; ++i) { f[2 * i] = bpmem.texscale[1].getScaleS(i & 1); f[2 * i + 1] = bpmem.texscale[1].getScaleT(i & 1); @@ -181,11 +183,11 @@ void PixelShaderManager::SetConstants() s_nIndTexScaleChanged = 0; } - if (s_nIndTexMtxChanged || g_ActiveConfig.bUseGLSL) + if (s_nIndTexMtxChanged) { for (int i = 0; i < 3; ++i) { - if (s_nIndTexMtxChanged & (1 << i) || g_ActiveConfig.bUseGLSL) + if (s_nIndTexMtxChanged & (1 << i)) { int scale = ((u32)bpmem.indmtx[i].col0.s0 << 0) | ((u32)bpmem.indmtx[i].col1.s1 << 2) | @@ -215,13 +217,13 @@ void PixelShaderManager::SetConstants() s_nIndTexMtxChanged = 0; } - if (s_bFogColorChanged || g_ActiveConfig.bUseGLSL) + if (s_bFogColorChanged) { SetPSConstant4f(C_FOG, bpmem.fog.color.r / 255.0f, bpmem.fog.color.g / 255.0f, bpmem.fog.color.b / 255.0f, 0); s_bFogColorChanged = false; } - if (s_bFogParamChanged || g_ActiveConfig.bUseGLSL) + if (s_bFogParamChanged) { if(!g_ActiveConfig.bDisableFog) { @@ -237,7 +239,7 @@ void PixelShaderManager::SetConstants() s_bFogParamChanged = false; } - if (s_bFogRangeAdjustChanged || g_ActiveConfig.bUseGLSL) + if (s_bFogRangeAdjustChanged) { if(!g_ActiveConfig.bDisableFog && bpmem.fogRange.Base.Enabled == 1) { @@ -261,7 +263,7 @@ void PixelShaderManager::SetConstants() if (g_ActiveConfig.bEnablePixelLighting && g_ActiveConfig.backend_info.bSupportsPixelLighting) // config check added because the code in here was crashing for me inside SetPSConstant4f { - if (nLightsChanged[0] >= 0 || g_ActiveConfig.bUseGLSL) + if (nLightsChanged[0] >= 0) { // lights don't have a 1 to 1 mapping, the color component needs to be converted to 4 floats int istart = nLightsChanged[0] / 0x10; @@ -297,14 +299,14 @@ void PixelShaderManager::SetConstants() nLightsChanged[0] = nLightsChanged[1] = -1; } - if (nMaterialsChanged || g_ActiveConfig.bUseGLSL) + if (nMaterialsChanged) { float GC_ALIGNED16(material[4]); float NormalizationCoef = 1 / 255.0f; for (int i = 0; i < 4; ++i) { - if (nMaterialsChanged & (1 << i) || g_ActiveConfig.bUseGLSL) + if (nMaterialsChanged & (1 << i)) { u32 data = *(xfregs.ambColor + i); diff --git a/Source/Core/VideoCommon/Src/TextureConversionShader.cpp b/Source/Core/VideoCommon/Src/TextureConversionShader.cpp index 1be760a608..550fef2740 100644 --- a/Source/Core/VideoCommon/Src/TextureConversionShader.cpp +++ b/Source/Core/VideoCommon/Src/TextureConversionShader.cpp @@ -149,12 +149,12 @@ void WriteSwizzler(char*& p, u32 format, API_TYPE ApiType) WRITE(p, " sampleUv = sampleUv * "I_COLORS"[0].xy;\n"); - if(ApiType == API_OPENGL) + if(ApiType == API_OPENGL || ApiType == API_GLSL) WRITE(p," sampleUv.y = "I_COLORS"[1].y - sampleUv.y;\n"); WRITE(p, " sampleUv = sampleUv + "I_COLORS"[1].zw;\n"); - if(ApiType != API_OPENGL) + if(ApiType != API_OPENGL && ApiType != API_GLSL) { WRITE(p, " sampleUv = sampleUv + float2(0.0f,1.0f);\n");// still to determine the reason for this WRITE(p, " sampleUv = sampleUv / "I_COLORS"[0].zw;\n"); @@ -237,12 +237,12 @@ void Write32BitSwizzler(char*& p, u32 format, API_TYPE ApiType) WRITE(p, " sampleUv.y = yb + xoff;\n"); WRITE(p, " sampleUv = sampleUv * "I_COLORS"[0].xy;\n"); - if(ApiType == API_OPENGL) + if(ApiType == API_OPENGL || ApiType == API_GLSL) WRITE(p," sampleUv.y = "I_COLORS"[1].y - sampleUv.y;\n"); WRITE(p, " sampleUv = sampleUv + "I_COLORS"[1].zw;\n"); - if(ApiType != API_OPENGL) + if(ApiType != API_OPENGL && ApiType != API_GLSL) { WRITE(p, " sampleUv = sampleUv + float2(0.0f,1.0f);\n");// still to determine the reason for this WRITE(p, " sampleUv = sampleUv / "I_COLORS"[0].zw;\n"); @@ -263,7 +263,7 @@ void WriteSampleColor(char*& p, const char* colorComp, const char* dest, API_TYP // the increment of sampleUv.x is delayed, so we perform it here. see WriteIncrementSampleX. const char* texSampleIncrementUnit; - if(ApiType != API_OPENGL) + if(ApiType != API_OPENGL || ApiType != API_GLSL) texSampleIncrementUnit = I_COLORS"[0].x / "I_COLORS"[0].z"; else texSampleIncrementUnit = I_COLORS"[0].x"; diff --git a/Source/Core/VideoCommon/Src/VertexShaderGen.cpp b/Source/Core/VideoCommon/Src/VertexShaderGen.cpp index 78feff6541..2aceb5cda7 100644 --- a/Source/Core/VideoCommon/Src/VertexShaderGen.cpp +++ b/Source/Core/VideoCommon/Src/VertexShaderGen.cpp @@ -195,9 +195,15 @@ const char *GenerateVertexShaderCode(u32 components, API_TYPE ApiType) WRITE(p, "#extension GL_ARB_shading_language_420pack : enable\n"); if (g_ActiveConfig.backend_info.bSupportsGLSLLocation) WRITE(p, "#extension GL_ARB_separate_shader_objects : enable\n"); + WRITE(p, "#define ATTRIN in\n"); + WRITE(p, "#define ATTROUT out\n"); } else + { WRITE(p, "#version 120\n"); + WRITE(p, "#define ATTRIN attribute\n"); + WRITE(p, "#define ATTROUT attribute\n"); + } // Silly differences WRITE(p, "#define float2 vec2\n"); WRITE(p, "#define float3 vec3\n"); @@ -227,11 +233,11 @@ const char *GenerateVertexShaderCode(u32 components, API_TYPE ApiType) WRITE(p, " float3 rawnorm0 = gl_Normal; // NORMAL0,\n"); if (components & VB_HAS_POSMTXIDX) - WRITE(p, "attribute float fposmtx; // ATTR%d,\n", SHADER_POSMTX_ATTRIB); + WRITE(p, "ATTRIN float fposmtx; // ATTR%d,\n", SHADER_POSMTX_ATTRIB); if (components & VB_HAS_NRM1) - WRITE(p, "attribute float3 rawnorm1; // ATTR%d,\n", SHADER_NORM1_ATTRIB); + WRITE(p, "ATTRIN float3 rawnorm1; // ATTR%d,\n", SHADER_NORM1_ATTRIB); if (components & VB_HAS_NRM2) - WRITE(p, "attribute float3 rawnorm2; // ATTR%d,\n", SHADER_NORM2_ATTRIB); + WRITE(p, "ATTRIN float3 rawnorm2; // ATTR%d,\n", SHADER_NORM2_ATTRIB); if (components & VB_HAS_COL0) WRITE(p, " float4 color0 = gl_Color; // COLOR0,\n"); diff --git a/Source/Core/VideoCommon/Src/VertexShaderManager.cpp b/Source/Core/VideoCommon/Src/VertexShaderManager.cpp index 8fd53d128d..2b962c5a6b 100644 --- a/Source/Core/VideoCommon/Src/VertexShaderManager.cpp +++ b/Source/Core/VideoCommon/Src/VertexShaderManager.cpp @@ -173,7 +173,9 @@ void VertexShaderManager::Dirty() // TODO: A cleaner way to control the matricies without making a mess in the parameters field void VertexShaderManager::SetConstants() { - if (nTransformMatricesChanged[0] >= 0 || g_ActiveConfig.bUseGLSL) + if (g_ActiveConfig.bUseGLSL) + Dirty(); + if (nTransformMatricesChanged[0] >= 0) { int startn = nTransformMatricesChanged[0] / 4; int endn = (nTransformMatricesChanged[1] + 3) / 4; @@ -181,7 +183,7 @@ void VertexShaderManager::SetConstants() SetMultiVSConstant4fv(C_TRANSFORMMATRICES + startn, endn - startn, pstart); nTransformMatricesChanged[0] = nTransformMatricesChanged[1] = -1; } - if (nNormalMatricesChanged[0] >= 0 || g_ActiveConfig.bUseGLSL) + if (nNormalMatricesChanged[0] >= 0) { int startn = nNormalMatricesChanged[0] / 3; int endn = (nNormalMatricesChanged[1] + 2) / 3; @@ -190,7 +192,7 @@ void VertexShaderManager::SetConstants() nNormalMatricesChanged[0] = nNormalMatricesChanged[1] = -1; } - if (nPostTransformMatricesChanged[0] >= 0 || g_ActiveConfig.bUseGLSL) + if (nPostTransformMatricesChanged[0] >= 0) { int startn = nPostTransformMatricesChanged[0] / 4; int endn = (nPostTransformMatricesChanged[1] + 3 ) / 4; @@ -198,7 +200,7 @@ void VertexShaderManager::SetConstants() SetMultiVSConstant4fv(C_POSTTRANSFORMMATRICES + startn, endn - startn, pstart); } - if (nLightsChanged[0] >= 0 || g_ActiveConfig.bUseGLSL) + if (nLightsChanged[0] >= 0) { // lights don't have a 1 to 1 mapping, the color component needs to be converted to 4 floats int istart = nLightsChanged[0] / 0x10; @@ -234,14 +236,14 @@ void VertexShaderManager::SetConstants() nLightsChanged[0] = nLightsChanged[1] = -1; } - if (nMaterialsChanged || g_ActiveConfig.bUseGLSL) + if (nMaterialsChanged) { float GC_ALIGNED16(material[4]); float NormalizationCoef = 1 / 255.0f; for (int i = 0; i < 4; ++i) { - if (nMaterialsChanged & (1 << i) || g_ActiveConfig.bUseGLSL) + if (nMaterialsChanged & (1 << i)) { u32 data = *(xfregs.ambColor + i); @@ -257,7 +259,7 @@ void VertexShaderManager::SetConstants() nMaterialsChanged = 0; } - if (bPosNormalMatrixChanged || g_ActiveConfig.bUseGLSL) + if (bPosNormalMatrixChanged) { bPosNormalMatrixChanged = false; @@ -268,7 +270,7 @@ void VertexShaderManager::SetConstants() SetMultiVSConstant3fv(C_POSNORMALMATRIX + 3, 3, norm); } - if (bTexMatricesChanged[0] || g_ActiveConfig.bUseGLSL) + if (bTexMatricesChanged[0]) { bTexMatricesChanged[0] = false; const float *fptrs[] = @@ -283,7 +285,7 @@ void VertexShaderManager::SetConstants() } } - if (bTexMatricesChanged[1] || g_ActiveConfig.bUseGLSL) + if (bTexMatricesChanged[1]) { bTexMatricesChanged[1] = false; const float *fptrs[] = { @@ -297,7 +299,7 @@ void VertexShaderManager::SetConstants() } } - if (bViewportChanged || g_ActiveConfig.bUseGLSL) + if (bViewportChanged) { bViewportChanged = false; SetVSConstant4f(C_DEPTHPARAMS,xfregs.viewport.farZ / 16777216.0f,xfregs.viewport.zRange / 16777216.0f,0.0f,0.0f); @@ -306,7 +308,7 @@ void VertexShaderManager::SetConstants() bProjectionChanged = true; } - if (bProjectionChanged || g_ActiveConfig.bUseGLSL) + if (bProjectionChanged) { bProjectionChanged = false; diff --git a/Source/Plugins/Plugin_VideoOGL/Src/PixelShaderCache.cpp b/Source/Plugins/Plugin_VideoOGL/Src/PixelShaderCache.cpp index 5ee839676c..f4f9a46e72 100644 --- a/Source/Plugins/Plugin_VideoOGL/Src/PixelShaderCache.cpp +++ b/Source/Plugins/Plugin_VideoOGL/Src/PixelShaderCache.cpp @@ -80,10 +80,11 @@ void PixelShaderCache::Init() pCompilePixelShader = CompileGLSLPixelShader; // Should this be set here? if (strstr((const char*)glGetString(GL_EXTENSIONS), "GL_ARB_shading_language_420pack") != NULL) - g_ActiveConfig.backend_info.bSupportsGLSLBinding = true; + g_Config.backend_info.bSupportsGLSLBinding = true; // This bit doesn't quite work yet, always set to false //if (strstr((const char*)glGetString(GL_EXTENSIONS), "GL_ARB_separate_shader_objects") != NULL) - g_ActiveConfig.backend_info.bSupportsGLSLLocation = false; + g_Config.backend_info.bSupportsGLSLLocation = false; + UpdateActiveConfig(); } else { From 62b9a779c152df45a723d4fbd8c1d75d8d6fc8e3 Mon Sep 17 00:00:00 2001 From: Ryan Houdek Date: Thu, 8 Dec 2011 04:11:30 -0600 Subject: [PATCH 17/83] Playing through SSBM story made me find this. --- Source/Core/VideoCommon/Src/VertexShaderGen.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Source/Core/VideoCommon/Src/VertexShaderGen.cpp b/Source/Core/VideoCommon/Src/VertexShaderGen.cpp index 2aceb5cda7..7463f5d5fc 100644 --- a/Source/Core/VideoCommon/Src/VertexShaderGen.cpp +++ b/Source/Core/VideoCommon/Src/VertexShaderGen.cpp @@ -332,7 +332,7 @@ const char *GenerateVertexShaderCode(u32 components, API_TYPE ApiType) if (components & VB_HAS_NRM1) WRITE(p, "float3 _norm1 = float3(dot("I_POSNORMALMATRIX"[3].xyz, rawnorm1), dot("I_POSNORMALMATRIX"[4].xyz, rawnorm1), dot("I_POSNORMALMATRIX"[5].xyz, rawnorm1));\n"); if (components & VB_HAS_NRM2) - WRITE(p, "float3 _norm2 = float3(dot("I_POSNORMALMATRIX".[3].xyz, rawnorm2), dot("I_POSNORMALMATRIX"[4].xyz, rawnorm2), dot("I_POSNORMALMATRIX"[5].xyz, rawnorm2));\n"); + WRITE(p, "float3 _norm2 = float3(dot("I_POSNORMALMATRIX"[3].xyz, rawnorm2), dot("I_POSNORMALMATRIX"[4].xyz, rawnorm2), dot("I_POSNORMALMATRIX"[5].xyz, rawnorm2));\n"); } if (!(components & VB_HAS_NRM0)) From 0ccba2b581bedc564aa8052d6545e946768d4c70 Mon Sep 17 00:00:00 2001 From: Ryan Houdek Date: Thu, 8 Dec 2011 05:09:48 -0600 Subject: [PATCH 18/83] Support Dual Source Blending in OGL plugin with GLSL. --- .../Core/VideoCommon/Src/PixelShaderGen.cpp | 23 +++++++++++++------ .../Plugin_VideoOGL/Src/VertexManager.cpp | 8 ++----- 2 files changed, 18 insertions(+), 13 deletions(-) diff --git a/Source/Core/VideoCommon/Src/PixelShaderGen.cpp b/Source/Core/VideoCommon/Src/PixelShaderGen.cpp index 584254e371..4c402e0a9f 100644 --- a/Source/Core/VideoCommon/Src/PixelShaderGen.cpp +++ b/Source/Core/VideoCommon/Src/PixelShaderGen.cpp @@ -675,10 +675,18 @@ const char *GeneratePixelShaderCode(DSTALPHA_MODE dstAlphaMode, API_TYPE ApiType { // GLSL doesn't do main arguments // Once we switch to GLSL 1.3 we will bind a lot of these. - - WRITE(p, " float4 ocol0;\n"); + + if(dstAlphaMode == DSTALPHA_DUAL_SOURCE_BLEND) - WRITE(p, " float4 ocol1;\n"); // Will be supported later + { + // This won't get hit unless we support GL 3.3 + WRITE(p, " layout(location = 0) out float4 ocol0;\n"); + WRITE(p, " layout(location = 0, index = 1) out float4 ocol1;\n"); // Will be supported later + } + else + { + WRITE(p, " float4 ocol0;\n"); + } if(DepthTextureEnable) WRITE(p, " float depth;\n"); // TODO: Passed to Vertex Shader right? WRITE(p, " float4 rawpos = gl_FragCoord;\n"); @@ -721,7 +729,7 @@ const char *GeneratePixelShaderCode(DSTALPHA_MODE dstAlphaMode, API_TYPE ApiType // alpha test will always fail, so restart the shader and just make it an empty function WRITE(p, "ocol0 = float4(0);\n"); WRITE(p, "discard;\n"); - if(ApiType == API_GLSL) + if(ApiType == API_GLSL && dstAlphaMode != DSTALPHA_DUAL_SOURCE_BLEND) WRITE(p, "gl_FragData[0] = ocol0;\n"); if(ApiType != API_D3D11) WRITE(p, "return;\n"); @@ -833,7 +841,8 @@ const char *GeneratePixelShaderCode(DSTALPHA_MODE dstAlphaMode, API_TYPE ApiType if(ApiType == API_GLSL) { // Once we switch to GLSL 1.3 and bind variables, we won't need to do this - WRITE(p, "gl_FragData[0] = ocol0;\n"); + if (dstAlphaMode != DSTALPHA_DUAL_SOURCE_BLEND) + WRITE(p, "gl_FragData[0] = ocol0;\n"); if(DepthTextureEnable) WRITE(p, "gl_FragDepth = depth;\n"); if(dstAlphaMode == DSTALPHA_DUAL_SOURCE_BLEND) @@ -892,8 +901,8 @@ const char *GeneratePixelShaderCode(DSTALPHA_MODE dstAlphaMode, API_TYPE ApiType { if(DepthTextureEnable) WRITE(p, "gl_FragDepth = depth;\n"); - - WRITE(p, "gl_FragData[0] = ocol0;\n"); + if (dstAlphaMode != DSTALPHA_DUAL_SOURCE_BLEND) + WRITE(p, "gl_FragData[0] = ocol0;\n"); } } WRITE(p, "}\n"); diff --git a/Source/Plugins/Plugin_VideoOGL/Src/VertexManager.cpp b/Source/Plugins/Plugin_VideoOGL/Src/VertexManager.cpp index ebca599fe8..e2723c0c98 100644 --- a/Source/Plugins/Plugin_VideoOGL/Src/VertexManager.cpp +++ b/Source/Plugins/Plugin_VideoOGL/Src/VertexManager.cpp @@ -177,8 +177,8 @@ void VertexManager::vFlush() bool useDstAlpha = !g_ActiveConfig.bDstAlphaPass && bpmem.dstalpha.enable && bpmem.blendmode.alphaupdate && bpmem.zcontrol.pixel_format == PIXELFMT_RGBA6_Z24; -#ifdef USE_DUAL_SOURCE_BLEND - bool dualSourcePossible = GLEW_ARB_blend_func_extended; + // Makes sure we can actually do Dual source blending + bool dualSourcePossible = g_ActiveConfig.bUseGLSL && g_ActiveConfig.backend_info.bSupportsGLSLBinding; // finally bind FRAGMENTSHADER* ps; @@ -201,10 +201,6 @@ void VertexManager::vFlush() { ps = PixelShaderCache::SetShader(DSTALPHA_NONE,g_nativeVertexFmt->m_components); } -#else - bool dualSourcePossible = false; - FRAGMENTSHADER* ps = PixelShaderCache::SetShader(DSTALPHA_NONE,g_nativeVertexFmt->m_components); -#endif VERTEXSHADER* vs = VertexShaderCache::SetShader(g_nativeVertexFmt->m_components); if(g_ActiveConfig.bUseGLSL) From c89c484dd0269c44d88a63e3796ad6e34d927d35 Mon Sep 17 00:00:00 2001 From: Ryan Houdek Date: Thu, 8 Dec 2011 05:32:17 -0600 Subject: [PATCH 19/83] Few compiler errors that got exposed once I got Dual Source Blending working. Seems it isn't working quite 100% either. Good chance I missed something anyway. --- .../Core/VideoCommon/Src/PixelShaderGen.cpp | 24 +++++++++---------- 1 file changed, 11 insertions(+), 13 deletions(-) diff --git a/Source/Core/VideoCommon/Src/PixelShaderGen.cpp b/Source/Core/VideoCommon/Src/PixelShaderGen.cpp index 4c402e0a9f..9071747526 100644 --- a/Source/Core/VideoCommon/Src/PixelShaderGen.cpp +++ b/Source/Core/VideoCommon/Src/PixelShaderGen.cpp @@ -833,20 +833,18 @@ const char *GeneratePixelShaderCode(DSTALPHA_MODE dstAlphaMode, API_TYPE ApiType { // alpha test will always fail, so restart the shader and just make it an empty function p = pmainstart; - WRITE(p, "ocol0 = 0;\n"); + WRITE(p, "ocol0 = vec4(0.0f);\n"); if(DepthTextureEnable) WRITE(p, "depth = 1.f;\n"); if(dstAlphaMode == DSTALPHA_DUAL_SOURCE_BLEND) - WRITE(p, "ocol1 = 0;\n"); + WRITE(p, "ocol1 = vec4(0.0f);\n"); if(ApiType == API_GLSL) { - // Once we switch to GLSL 1.3 and bind variables, we won't need to do this - if (dstAlphaMode != DSTALPHA_DUAL_SOURCE_BLEND) - WRITE(p, "gl_FragData[0] = ocol0;\n"); - if(DepthTextureEnable) - WRITE(p, "gl_FragDepth = depth;\n"); - if(dstAlphaMode == DSTALPHA_DUAL_SOURCE_BLEND) - ; // TODO: Will do this later + // Once we switch to GLSL 1.3 and bind variables, we won't need to do this + if (dstAlphaMode != DSTALPHA_DUAL_SOURCE_BLEND) + WRITE(p, "gl_FragData[0] = ocol0;\n"); + if(DepthTextureEnable) + WRITE(p, "gl_FragDepth = depth;\n"); } WRITE(p, "discard;\n"); if(ApiType != API_D3D11) @@ -1011,10 +1009,10 @@ static void WriteStage(char *&p, int n, API_TYPE ApiType) WRITE(p, "float2 indtevtrans%d = "I_INDTEXMTX"[%d].ww * uv%d.xy * indtevcrd%d.yy;\n", n, mtxidx, texcoord, n); } else - WRITE(p, "float2 indtevtrans%d = 0;\n", n); + WRITE(p, "float2 indtevtrans%d = float2(0.0f);\n", n); } else - WRITE(p, "float2 indtevtrans%d = 0;\n", n); + WRITE(p, "float2 indtevtrans%d = float2(0.0f);\n", n); // --------- // Wrapping @@ -1304,8 +1302,8 @@ static bool WriteAlphaTest(char *&p, API_TYPE ApiType,DSTALPHA_MODE dstAlphaMode compindex = bpmem.alphaFunc.comp1 % 8; WRITE(p, tevAlphaFuncsTable[compindex],alphaRef[1]);//lookup the second component from the alpha function table - WRITE(p, ")){ocol0 = float4(0.0);%s%s discard;%s}\n", - dstAlphaMode == DSTALPHA_DUAL_SOURCE_BLEND ? "ocol1 = 0;" : "", + WRITE(p, ")){ocol0 = float4(0.0f);%s%s discard;%s}\n", + dstAlphaMode == DSTALPHA_DUAL_SOURCE_BLEND ? "ocol1 = vec4(0.0f);" : "", DepthTextureEnable ? "depth = 1.f;" : "", (ApiType != API_D3D11) ? "return;" : ""); return true; From 9119399547b44f9c9068fa52812a9e90ab367d8d Mon Sep 17 00:00:00 2001 From: Ryan Houdek Date: Fri, 9 Dec 2011 16:13:04 -0600 Subject: [PATCH 20/83] Put Vertex Uniforms in to the correct places to get ready for UBOs. --- Source/Core/VideoCommon/Src/VertexShaderGen.cpp | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/Source/Core/VideoCommon/Src/VertexShaderGen.cpp b/Source/Core/VideoCommon/Src/VertexShaderGen.cpp index 7463f5d5fc..a66f3685e3 100644 --- a/Source/Core/VideoCommon/Src/VertexShaderGen.cpp +++ b/Source/Core/VideoCommon/Src/VertexShaderGen.cpp @@ -215,14 +215,14 @@ const char *GenerateVertexShaderCode(u32 components, API_TYPE ApiType) WRITE(p, "#define lerp(x, y, z) mix(x, y, z)\n"); } // uniforms - WRITE(p, "%suniform float4 "I_TRANSFORMMATRICES"[64] %s;\n", WriteLocation(ApiType, C_TRANSFORMMATRICES), WriteRegister(ApiType, "c", C_TRANSFORMMATRICES)); - WRITE(p, "%suniform float4 "I_TEXMATRICES"[24] %s;\n", WriteLocation(ApiType, C_TEXMATRICES), WriteRegister(ApiType, "c", C_TEXMATRICES)); // also using tex matrices - WRITE(p, "%suniform float4 "I_NORMALMATRICES"[32] %s;\n", WriteLocation(ApiType, C_NORMALMATRICES), WriteRegister(ApiType, "c", C_NORMALMATRICES)); WRITE(p, "%suniform float4 "I_POSNORMALMATRIX"[6] %s;\n", WriteLocation(ApiType, C_POSNORMALMATRIX), WriteRegister(ApiType, "c", C_POSNORMALMATRIX)); - WRITE(p, "%suniform float4 "I_POSTTRANSFORMMATRICES"[64] %s;\n", WriteLocation(ApiType, C_POSTTRANSFORMMATRICES), WriteRegister(ApiType, "c", C_POSTTRANSFORMMATRICES)); - WRITE(p, "%suniform float4 "I_LIGHTS"[40] %s;\n", WriteLocation(ApiType, C_LIGHTS), WriteRegister(ApiType, "c", C_LIGHTS)); - WRITE(p, "%suniform float4 "I_MATERIALS"[4] %s;\n", WriteLocation(ApiType, C_MATERIALS), WriteRegister(ApiType, "c", C_MATERIALS)); WRITE(p, "%suniform float4 "I_PROJECTION"[4] %s;\n", WriteLocation(ApiType, C_PROJECTION), WriteRegister(ApiType, "c", C_PROJECTION)); + WRITE(p, "%suniform float4 "I_MATERIALS"[4] %s;\n", WriteLocation(ApiType, C_MATERIALS), WriteRegister(ApiType, "c", C_MATERIALS)); + WRITE(p, "%suniform float4 "I_LIGHTS"[40] %s;\n", WriteLocation(ApiType, C_LIGHTS), WriteRegister(ApiType, "c", C_LIGHTS)); + WRITE(p, "%suniform float4 "I_TEXMATRICES"[24] %s;\n", WriteLocation(ApiType, C_TEXMATRICES), WriteRegister(ApiType, "c", C_TEXMATRICES)); // also using tex matrices + WRITE(p, "%suniform float4 "I_TRANSFORMMATRICES"[64] %s;\n", WriteLocation(ApiType, C_TRANSFORMMATRICES), WriteRegister(ApiType, "c", C_TRANSFORMMATRICES)); + WRITE(p, "%suniform float4 "I_NORMALMATRICES"[32] %s;\n", WriteLocation(ApiType, C_NORMALMATRICES), WriteRegister(ApiType, "c", C_NORMALMATRICES)); + WRITE(p, "%suniform float4 "I_POSTTRANSFORMMATRICES"[64] %s;\n", WriteLocation(ApiType, C_POSTTRANSFORMMATRICES), WriteRegister(ApiType, "c", C_POSTTRANSFORMMATRICES)); WRITE(p, "%suniform float4 "I_DEPTHPARAMS" %s;\n", WriteLocation(ApiType, C_DEPTHPARAMS), WriteRegister(ApiType, "c", C_DEPTHPARAMS)); p = GenerateVSOutputStruct(p, components, ApiType); From 87c3c37ba71e297ceb037fd9c00117ed26b935ab Mon Sep 17 00:00:00 2001 From: LPFaint99 Date: Fri, 9 Dec 2011 14:28:59 -0800 Subject: [PATCH 21/83] add ProgramShaderCache.* to visual studio project files --- Source/Plugins/Plugin_VideoOGL/Plugin_VideoOGL.vcxproj | 2 ++ .../Plugins/Plugin_VideoOGL/Plugin_VideoOGL.vcxproj.filters | 6 ++++++ 2 files changed, 8 insertions(+) diff --git a/Source/Plugins/Plugin_VideoOGL/Plugin_VideoOGL.vcxproj b/Source/Plugins/Plugin_VideoOGL/Plugin_VideoOGL.vcxproj index f782fd2c93..f70f8b9d0e 100644 --- a/Source/Plugins/Plugin_VideoOGL/Plugin_VideoOGL.vcxproj +++ b/Source/Plugins/Plugin_VideoOGL/Plugin_VideoOGL.vcxproj @@ -202,6 +202,7 @@ + @@ -224,6 +225,7 @@ + diff --git a/Source/Plugins/Plugin_VideoOGL/Plugin_VideoOGL.vcxproj.filters b/Source/Plugins/Plugin_VideoOGL/Plugin_VideoOGL.vcxproj.filters index ed581ff963..9e7816de46 100644 --- a/Source/Plugins/Plugin_VideoOGL/Plugin_VideoOGL.vcxproj.filters +++ b/Source/Plugins/Plugin_VideoOGL/Plugin_VideoOGL.vcxproj.filters @@ -27,6 +27,9 @@ Render + + Render + Render @@ -63,6 +66,9 @@ Render + + Render + Render From 8e5bb59cb692bae1bf4a94797a7c153e9bbd2d3d Mon Sep 17 00:00:00 2001 From: Ryan Houdek Date: Fri, 9 Dec 2011 17:30:05 -0600 Subject: [PATCH 22/83] Add in UBOs, doesn't work yet. Still debugging here. --- .../Core/VideoCommon/Src/PixelShaderGen.cpp | 33 +++++++------ .../VideoCommon/Src/PixelShaderManager.cpp | 2 +- .../Core/VideoCommon/Src/VertexShaderGen.cpp | 25 ++++++---- Source/Core/VideoCommon/Src/VertexShaderGen.h | 2 +- .../VideoCommon/Src/VertexShaderManager.cpp | 2 +- Source/Core/VideoCommon/Src/VideoConfig.h | 2 + .../Plugin_VideoOGL/Src/PixelShaderCache.cpp | 48 +++++++++++++------ .../Src/ProgramShaderCache.cpp | 39 ++++++++++++--- .../Plugin_VideoOGL/Src/ProgramShaderCache.h | 8 +++- .../Plugin_VideoOGL/Src/VertexManager.cpp | 3 +- .../Plugin_VideoOGL/Src/VertexShaderCache.cpp | 20 ++++++++ Source/Plugins/Plugin_VideoOGL/Src/main.cpp | 3 ++ 12 files changed, 138 insertions(+), 49 deletions(-) diff --git a/Source/Core/VideoCommon/Src/PixelShaderGen.cpp b/Source/Core/VideoCommon/Src/PixelShaderGen.cpp index 9071747526..8d62858ecc 100644 --- a/Source/Core/VideoCommon/Src/PixelShaderGen.cpp +++ b/Source/Core/VideoCommon/Src/PixelShaderGen.cpp @@ -610,21 +610,24 @@ const char *GeneratePixelShaderCode(DSTALPHA_MODE dstAlphaMode, API_TYPE ApiType } WRITE(p, "\n"); - - WRITE(p, "%suniform float4 "I_COLORS"[4] %s;\n", WriteLocation(ApiType, C_COLORS), WriteRegister(ApiType, "c", C_COLORS)); - WRITE(p, "%suniform float4 "I_KCOLORS"[4] %s;\n", WriteLocation(ApiType, C_KCOLORS), WriteRegister(ApiType, "c", C_KCOLORS)); - WRITE(p, "%suniform float4 "I_ALPHA"[1] %s;\n", WriteLocation(ApiType, C_ALPHA), WriteRegister(ApiType, "c", C_ALPHA)); - WRITE(p, "%suniform float4 "I_TEXDIMS"[8] %s;\n", WriteLocation(ApiType, C_TEXDIMS), WriteRegister(ApiType, "c", C_TEXDIMS)); - WRITE(p, "%suniform float4 "I_ZBIAS"[2] %s;\n", WriteLocation(ApiType, C_ZBIAS), WriteRegister(ApiType, "c", C_ZBIAS)); - WRITE(p, "%suniform float4 "I_INDTEXSCALE"[2] %s;\n", WriteLocation(ApiType, C_INDTEXSCALE), WriteRegister(ApiType, "c", C_INDTEXSCALE)); - WRITE(p, "%suniform float4 "I_INDTEXMTX"[6] %s;\n", WriteLocation(ApiType, C_INDTEXMTX), WriteRegister(ApiType, "c", C_INDTEXMTX)); - WRITE(p, "%suniform float4 "I_FOG"[3] %s;\n", WriteLocation(ApiType, C_FOG), WriteRegister(ApiType, "c", C_FOG)); - - if(g_ActiveConfig.bEnablePixelLighting && g_ActiveConfig.backend_info.bSupportsPixelLighting) - { - WRITE(p, "%suniform float4 "I_PLIGHTS"[40] %s;\n", WriteLocation(ApiType, C_PLIGHTS), WriteRegister(ApiType, "c", C_PLIGHTS)); - WRITE(p, "%suniform float4 "I_PMATERIALS"[4] %s;\n", WriteLocation(ApiType, C_PMATERIALS), WriteRegister(ApiType, "c", C_PMATERIALS)); - } + if(ApiType == API_GLSL && g_ActiveConfig.backend_info.bSupportsGLSLUBO) + WRITE(p, "layout(std140, binding = 0) uniform PSBlock {\n"); + + WRITE(p, "%suniform float4 "I_COLORS"[4] %s;\n", WriteLocation(ApiType, C_COLORS), WriteRegister(ApiType, "c", C_COLORS)); + WRITE(p, "%suniform float4 "I_KCOLORS"[4] %s;\n", WriteLocation(ApiType, C_KCOLORS), WriteRegister(ApiType, "c", C_KCOLORS)); + WRITE(p, "%suniform float4 "I_ALPHA"[1] %s;\n", WriteLocation(ApiType, C_ALPHA), WriteRegister(ApiType, "c", C_ALPHA)); + WRITE(p, "%suniform float4 "I_TEXDIMS"[8] %s;\n", WriteLocation(ApiType, C_TEXDIMS), WriteRegister(ApiType, "c", C_TEXDIMS)); + WRITE(p, "%suniform float4 "I_ZBIAS"[2] %s;\n", WriteLocation(ApiType, C_ZBIAS), WriteRegister(ApiType, "c", C_ZBIAS)); + WRITE(p, "%suniform float4 "I_INDTEXSCALE"[2] %s;\n", WriteLocation(ApiType, C_INDTEXSCALE), WriteRegister(ApiType, "c", C_INDTEXSCALE)); + WRITE(p, "%suniform float4 "I_INDTEXMTX"[6] %s;\n", WriteLocation(ApiType, C_INDTEXMTX), WriteRegister(ApiType, "c", C_INDTEXMTX)); + WRITE(p, "%suniform float4 "I_FOG"[3] %s;\n", WriteLocation(ApiType, C_FOG), WriteRegister(ApiType, "c", C_FOG)); + + // Compiler will optimize these out by itself. + WRITE(p, "%suniform float4 "I_PLIGHTS"[40] %s;\n", WriteLocation(ApiType, C_PLIGHTS), WriteRegister(ApiType, "c", C_PLIGHTS)); + WRITE(p, "%suniform float4 "I_PMATERIALS"[4] %s;\n", WriteLocation(ApiType, C_PMATERIALS), WriteRegister(ApiType, "c", C_PMATERIALS)); + + if(ApiType == API_GLSL && g_ActiveConfig.backend_info.bSupportsGLSLUBO) + WRITE(p, "};\n"); if(ApiType != API_GLSL) { diff --git a/Source/Core/VideoCommon/Src/PixelShaderManager.cpp b/Source/Core/VideoCommon/Src/PixelShaderManager.cpp index 3d4ddd2f35..e9eabb4040 100644 --- a/Source/Core/VideoCommon/Src/PixelShaderManager.cpp +++ b/Source/Core/VideoCommon/Src/PixelShaderManager.cpp @@ -85,7 +85,7 @@ void PixelShaderManager::Shutdown() void PixelShaderManager::SetConstants() { - if (g_ActiveConfig.bUseGLSL) + if (g_ActiveConfig.bUseGLSL && !g_ActiveConfig.backend_info.bSupportsGLSLUBO) Dirty(); for (int i = 0; i < 2; ++i) { diff --git a/Source/Core/VideoCommon/Src/VertexShaderGen.cpp b/Source/Core/VideoCommon/Src/VertexShaderGen.cpp index a66f3685e3..f2559d929c 100644 --- a/Source/Core/VideoCommon/Src/VertexShaderGen.cpp +++ b/Source/Core/VideoCommon/Src/VertexShaderGen.cpp @@ -214,16 +214,23 @@ const char *GenerateVertexShaderCode(u32 components, API_TYPE ApiType) WRITE(p, "#define saturate(x) clamp(x, 0.0f, 1.0f)\n"); WRITE(p, "#define lerp(x, y, z) mix(x, y, z)\n"); } + // uniforms - WRITE(p, "%suniform float4 "I_POSNORMALMATRIX"[6] %s;\n", WriteLocation(ApiType, C_POSNORMALMATRIX), WriteRegister(ApiType, "c", C_POSNORMALMATRIX)); - WRITE(p, "%suniform float4 "I_PROJECTION"[4] %s;\n", WriteLocation(ApiType, C_PROJECTION), WriteRegister(ApiType, "c", C_PROJECTION)); - WRITE(p, "%suniform float4 "I_MATERIALS"[4] %s;\n", WriteLocation(ApiType, C_MATERIALS), WriteRegister(ApiType, "c", C_MATERIALS)); - WRITE(p, "%suniform float4 "I_LIGHTS"[40] %s;\n", WriteLocation(ApiType, C_LIGHTS), WriteRegister(ApiType, "c", C_LIGHTS)); - WRITE(p, "%suniform float4 "I_TEXMATRICES"[24] %s;\n", WriteLocation(ApiType, C_TEXMATRICES), WriteRegister(ApiType, "c", C_TEXMATRICES)); // also using tex matrices - WRITE(p, "%suniform float4 "I_TRANSFORMMATRICES"[64] %s;\n", WriteLocation(ApiType, C_TRANSFORMMATRICES), WriteRegister(ApiType, "c", C_TRANSFORMMATRICES)); - WRITE(p, "%suniform float4 "I_NORMALMATRICES"[32] %s;\n", WriteLocation(ApiType, C_NORMALMATRICES), WriteRegister(ApiType, "c", C_NORMALMATRICES)); - WRITE(p, "%suniform float4 "I_POSTTRANSFORMMATRICES"[64] %s;\n", WriteLocation(ApiType, C_POSTTRANSFORMMATRICES), WriteRegister(ApiType, "c", C_POSTTRANSFORMMATRICES)); - WRITE(p, "%suniform float4 "I_DEPTHPARAMS" %s;\n", WriteLocation(ApiType, C_DEPTHPARAMS), WriteRegister(ApiType, "c", C_DEPTHPARAMS)); + if(ApiType == API_GLSL && g_ActiveConfig.backend_info.bSupportsGLSLUBO) + WRITE(p, "layout(std140, binding = 1) uniform VSBlock {\n"); + + WRITE(p, "%suniform float4 "I_POSNORMALMATRIX"[6] %s;\n", WriteLocation(ApiType, C_POSNORMALMATRIX), WriteRegister(ApiType, "c", C_POSNORMALMATRIX)); + WRITE(p, "%suniform float4 "I_PROJECTION"[4] %s;\n", WriteLocation(ApiType, C_PROJECTION), WriteRegister(ApiType, "c", C_PROJECTION)); + WRITE(p, "%suniform float4 "I_MATERIALS"[4] %s;\n", WriteLocation(ApiType, C_MATERIALS), WriteRegister(ApiType, "c", C_MATERIALS)); + WRITE(p, "%suniform float4 "I_LIGHTS"[40] %s;\n", WriteLocation(ApiType, C_LIGHTS), WriteRegister(ApiType, "c", C_LIGHTS)); + WRITE(p, "%suniform float4 "I_TEXMATRICES"[24] %s;\n", WriteLocation(ApiType, C_TEXMATRICES), WriteRegister(ApiType, "c", C_TEXMATRICES)); // also using tex matrices + WRITE(p, "%suniform float4 "I_TRANSFORMMATRICES"[64] %s;\n", WriteLocation(ApiType, C_TRANSFORMMATRICES), WriteRegister(ApiType, "c", C_TRANSFORMMATRICES)); + WRITE(p, "%suniform float4 "I_NORMALMATRICES"[32] %s;\n", WriteLocation(ApiType, C_NORMALMATRICES), WriteRegister(ApiType, "c", C_NORMALMATRICES)); + WRITE(p, "%suniform float4 "I_POSTTRANSFORMMATRICES"[64] %s;\n", WriteLocation(ApiType, C_POSTTRANSFORMMATRICES), WriteRegister(ApiType, "c", C_POSTTRANSFORMMATRICES)); + WRITE(p, "%suniform float4 "I_DEPTHPARAMS" %s;\n", WriteLocation(ApiType, C_DEPTHPARAMS), WriteRegister(ApiType, "c", C_DEPTHPARAMS)); + + if(ApiType == API_GLSL && g_ActiveConfig.backend_info.bSupportsGLSLUBO) + WRITE(p, "};\n"); p = GenerateVSOutputStruct(p, components, ApiType); diff --git a/Source/Core/VideoCommon/Src/VertexShaderGen.h b/Source/Core/VideoCommon/Src/VertexShaderGen.h index 0d4eeeb1c5..a3364c2982 100644 --- a/Source/Core/VideoCommon/Src/VertexShaderGen.h +++ b/Source/Core/VideoCommon/Src/VertexShaderGen.h @@ -46,7 +46,7 @@ #define C_NORMALMATRICES (C_TRANSFORMMATRICES + 64) #define C_POSTTRANSFORMMATRICES (C_NORMALMATRICES + 32) #define C_DEPTHPARAMS (C_POSTTRANSFORMMATRICES + 64) -#define C_VENVCONST_END (C_DEPTHPARAMS + 4) +#define C_VENVCONST_END (C_DEPTHPARAMS + 1) const s_svar VSVar_Loc[] = { {I_POSNORMALMATRIX, C_POSNORMALMATRIX, 6 }, {I_PROJECTION , C_PROJECTION, 4 }, {I_MATERIALS, C_MATERIALS, 4 }, diff --git a/Source/Core/VideoCommon/Src/VertexShaderManager.cpp b/Source/Core/VideoCommon/Src/VertexShaderManager.cpp index 2b962c5a6b..5a12392ed2 100644 --- a/Source/Core/VideoCommon/Src/VertexShaderManager.cpp +++ b/Source/Core/VideoCommon/Src/VertexShaderManager.cpp @@ -173,7 +173,7 @@ void VertexShaderManager::Dirty() // TODO: A cleaner way to control the matricies without making a mess in the parameters field void VertexShaderManager::SetConstants() { - if (g_ActiveConfig.bUseGLSL) + if (g_ActiveConfig.bUseGLSL && !g_ActiveConfig.backend_info.bSupportsGLSLUBO) Dirty(); if (nTransformMatricesChanged[0] >= 0) { diff --git a/Source/Core/VideoCommon/Src/VideoConfig.h b/Source/Core/VideoCommon/Src/VideoConfig.h index 414086ed15..a8d3a9af27 100644 --- a/Source/Core/VideoCommon/Src/VideoConfig.h +++ b/Source/Core/VideoCommon/Src/VideoConfig.h @@ -167,9 +167,11 @@ struct VideoConfig bool bSupportsDualSourceBlend; // only supported by D3D11 and OpenGL bool bSupportsFormatReinterpretation; bool bSupportsPixelLighting; + bool bSupportsGLSL; bool bSupportsGLSLBinding; bool bSupportsGLSLLocation; + bool bSupportsGLSLUBO; } backend_info; }; diff --git a/Source/Plugins/Plugin_VideoOGL/Src/PixelShaderCache.cpp b/Source/Plugins/Plugin_VideoOGL/Src/PixelShaderCache.cpp index f4f9a46e72..978357e18f 100644 --- a/Source/Plugins/Plugin_VideoOGL/Src/PixelShaderCache.cpp +++ b/Source/Plugins/Plugin_VideoOGL/Src/PixelShaderCache.cpp @@ -82,8 +82,13 @@ void PixelShaderCache::Init() if (strstr((const char*)glGetString(GL_EXTENSIONS), "GL_ARB_shading_language_420pack") != NULL) g_Config.backend_info.bSupportsGLSLBinding = true; // This bit doesn't quite work yet, always set to false + // TODO: Probably just drop this entirely in favour of UBOS //if (strstr((const char*)glGetString(GL_EXTENSIONS), "GL_ARB_separate_shader_objects") != NULL) g_Config.backend_info.bSupportsGLSLLocation = false; + + if (strstr((const char*)glGetString(GL_EXTENSIONS), "GL_ARB_uniform_buffer_object") != NULL) + g_Config.backend_info.bSupportsGLSLUBO = true; + UpdateActiveConfig(); } else @@ -466,11 +471,11 @@ bool CompileGLSLPixelShader(FRAGMENTSHADER& ps, const char* pstrprogram) } void PixelShaderCache::SetPSSampler(const char * name, unsigned int Tex) { - if(g_ActiveConfig.backend_info.bSupportsGLSLBinding) + if (g_ActiveConfig.backend_info.bSupportsGLSLBinding) return; PROGRAMSHADER tmp = ProgramShaderCache::GetShaderProgram(); - for(int a = 0; a < NUM_UNIFORMS; ++a) - if(!strcmp(name, UniformNames[a])) + for (int a = 0; a < NUM_UNIFORMS; ++a) + if (!strcmp(name, UniformNames[a])) { if(tmp.UniformLocations[a] == -1) return; @@ -484,8 +489,8 @@ void PixelShaderCache::SetPSSampler(const char * name, unsigned int Tex) void SetPSConstant4fvByName(const char * name, unsigned int offset, const float *f, const unsigned int count = 1) { PROGRAMSHADER tmp = ProgramShaderCache::GetShaderProgram(); - for(int a = 0; a < NUM_UNIFORMS; ++a) - if(!strcmp(name, UniformNames[a])) + for (int a = 0; a < NUM_UNIFORMS; ++a) + if (!strcmp(name, UniformNames[a])) { if(tmp.UniformLocations[a] == -1) return; @@ -500,14 +505,19 @@ void SetGLSLPSConstant4f(unsigned int const_number, float f1, float f2, float f3 { float f[4] = { f1, f2, f3, f4 }; - if(g_ActiveConfig.backend_info.bSupportsGLSLLocation) + if (g_ActiveConfig.backend_info.bSupportsGLSLLocation) { glUniform4fv(const_number, 1, f); return; } - for( unsigned int a = 0; a < 10; ++a) + if (g_ActiveConfig.backend_info.bSupportsGLSLUBO) + { + ProgramShaderCache::SetUniformObjects(0, const_number, f); + return; + } + for (unsigned int a = 0; a < 10; ++a) { - if( const_number >= PSVar_Loc[a].reg && const_number < (PSVar_Loc[a].reg + PSVar_Loc[a].size)) + if ( const_number >= PSVar_Loc[a].reg && const_number < (PSVar_Loc[a].reg + PSVar_Loc[a].size)) { unsigned int offset = const_number - PSVar_Loc[a].reg; SetPSConstant4fvByName(PSVar_Loc[a].name, offset, f); @@ -518,14 +528,19 @@ void SetGLSLPSConstant4f(unsigned int const_number, float f1, float f2, float f3 void SetGLSLPSConstant4fv(unsigned int const_number, const float *f) { - if(g_ActiveConfig.backend_info.bSupportsGLSLLocation) + if (g_ActiveConfig.backend_info.bSupportsGLSLLocation) { glUniform4fv(const_number, 1, f); return; } - for( unsigned int a = 0; a < 10; ++a) + if (g_ActiveConfig.backend_info.bSupportsGLSLUBO) + { + ProgramShaderCache::SetUniformObjects(0, const_number, f); + return; + } + for (unsigned int a = 0; a < 10; ++a) { - if( const_number >= PSVar_Loc[a].reg && const_number < (PSVar_Loc[a].reg + PSVar_Loc[a].size)) + if ( const_number >= PSVar_Loc[a].reg && const_number < (PSVar_Loc[a].reg + PSVar_Loc[a].size)) { unsigned int offset = const_number - PSVar_Loc[a].reg; SetPSConstant4fvByName(PSVar_Loc[a].name, offset, f); @@ -536,14 +551,19 @@ void SetGLSLPSConstant4fv(unsigned int const_number, const float *f) void SetMultiGLSLPSConstant4fv(unsigned int const_number, unsigned int count, const float *f) { - if(g_ActiveConfig.backend_info.bSupportsGLSLLocation) + if (g_ActiveConfig.backend_info.bSupportsGLSLLocation) { glUniform4fv(const_number, count, f); return; } - for( unsigned int a = 0; a < 10; ++a) + if (g_ActiveConfig.backend_info.bSupportsGLSLUBO) + { + ProgramShaderCache::SetUniformObjects(0, const_number, f, count); + return; + } + for (unsigned int a = 0; a < 10; ++a) { - if( const_number >= PSVar_Loc[a].reg && const_number < (PSVar_Loc[a].reg + PSVar_Loc[a].size)) + if (const_number >= PSVar_Loc[a].reg && const_number < (PSVar_Loc[a].reg + PSVar_Loc[a].size)) { unsigned int offset = const_number - PSVar_Loc[a].reg; SetPSConstant4fvByName(PSVar_Loc[a].name, offset, f, count); diff --git a/Source/Plugins/Plugin_VideoOGL/Src/ProgramShaderCache.cpp b/Source/Plugins/Plugin_VideoOGL/Src/ProgramShaderCache.cpp index fc056aed63..776280103e 100644 --- a/Source/Plugins/Plugin_VideoOGL/Src/ProgramShaderCache.cpp +++ b/Source/Plugins/Plugin_VideoOGL/Src/ProgramShaderCache.cpp @@ -14,12 +14,15 @@ // Official SVN repository and contact information can be found at // http://code.google.com/p/dolphin-emu/ + #include "ProgramShaderCache.h" +#include namespace OGL { GLuint ProgramShaderCache::CurrentFShader = 0, ProgramShaderCache::CurrentVShader = 0, ProgramShaderCache::CurrentProgram = 0; ProgramShaderCache::PCache ProgramShaderCache::pshaders; + GLuint ProgramShaderCache::UBOBuffers[2]; std::pair ProgramShaderCache::CurrentShaderProgram; const char *UniformNames[NUM_UNIFORMS] = { @@ -85,12 +88,14 @@ namespace OGL // Let's attach everything if(entry.program.vsid != 0) // attaching zero vertex shader makes it freak out glAttachShader(entry.program.glprogid, entry.program.vsid); + glAttachShader(entry.program.glprogid, entry.program.psid); + glLinkProgram(entry.program.glprogid); - //checkForGLError("linking program"); - glUseProgram(entry.program.glprogid); - //checkForGLError("using program"); + glUseProgram(entry.program.glprogid); + + // We cache our uniform locations for now // Once we move up to a newer version of GLSL, ~1.30 // We can remove this @@ -122,18 +127,40 @@ namespace OGL CurrentShaderProgram = ShaderPair; CurrentProgram = entry.program.glprogid; } - + void ProgramShaderCache::SetUniformObjects(int Buffer, unsigned int offset, const float *f, unsigned int count) + { + assert(Buffer > 1); + glBindBuffer(GL_UNIFORM_BUFFER, UBOBuffers[Buffer]); + // glBufferSubData expects data in bytes, so multiply count by four + // Expects the offset in bytes as well, so multiply by *4 *4 since we are passing in a vec4 location + glBufferSubData(GL_UNIFORM_BUFFER, offset * 4 * 4, count * 4, (void*)&f[0]); + } GLuint ProgramShaderCache::GetCurrentProgram(void) { return CurrentProgram; } GLint ProgramShaderCache::GetAttr(int num) { return pshaders[CurrentShaderProgram].program.attrLoc[num]; } - PROGRAMSHADER ProgramShaderCache::GetShaderProgram() + PROGRAMSHADER ProgramShaderCache::GetShaderProgram(void) { return pshaders[CurrentShaderProgram].program; } - + void ProgramShaderCache::Init(void) + { + glGenBuffers(2, UBOBuffers); + glBindBuffer(GL_UNIFORM_BUFFER, UBOBuffers[0]); + // We multiply by *4*4 because we need to get down to basic machine units. + // So multiply by four to get how many floats we have from vec4s + // Then once more to get bytes + glBufferData(GL_UNIFORM_BUFFER, C_PENVCONST_END * 4 * 4, NULL, GL_DYNAMIC_DRAW); + // Now bind the buffer to the index point + // We know PS is 0 since we have it statically set in the shader + glBindBufferBase(GL_UNIFORM_BUFFER, 0, UBOBuffers[0]); + // Repeat for VS shader + glBindBuffer(GL_UNIFORM_BUFFER, UBOBuffers[1]); + glBufferData(GL_UNIFORM_BUFFER, C_VENVCONST_END * 4 * 4, NULL, GL_DYNAMIC_DRAW); + glBindBufferBase(GL_UNIFORM_BUFFER, 1, UBOBuffers[1]); + } void ProgramShaderCache::Shutdown(void) { PCache::iterator iter = pshaders.begin(); diff --git a/Source/Plugins/Plugin_VideoOGL/Src/ProgramShaderCache.h b/Source/Plugins/Plugin_VideoOGL/Src/ProgramShaderCache.h index 53abe440f5..a5ff097af6 100644 --- a/Source/Plugins/Plugin_VideoOGL/Src/ProgramShaderCache.h +++ b/Source/Plugins/Plugin_VideoOGL/Src/ProgramShaderCache.h @@ -94,11 +94,17 @@ class ProgramShaderCache static PCache pshaders; static GLuint CurrentFShader, CurrentVShader, CurrentProgram; static std::pair CurrentShaderProgram; + + // For UBOS + static GLuint UBOBuffers[2]; // PS is 0, VS is 1 public: - static PROGRAMSHADER GetShaderProgram(); + static PROGRAMSHADER GetShaderProgram(void); static GLint GetAttr(int num); static void SetBothShaders(GLuint PS, GLuint VS); static GLuint GetCurrentProgram(void); + static void SetUniformObjects(int Buffer, unsigned int offset, const float *f, unsigned int count = 1); + + static void Init(void); static void Shutdown(void); }; diff --git a/Source/Plugins/Plugin_VideoOGL/Src/VertexManager.cpp b/Source/Plugins/Plugin_VideoOGL/Src/VertexManager.cpp index e2723c0c98..fff8263569 100644 --- a/Source/Plugins/Plugin_VideoOGL/Src/VertexManager.cpp +++ b/Source/Plugins/Plugin_VideoOGL/Src/VertexManager.cpp @@ -239,7 +239,8 @@ void VertexManager::vFlush() if(g_ActiveConfig.bUseGLSL) { ProgramShaderCache::SetBothShaders(ps->glprogid, 0); - PixelShaderManager::SetConstants(); // Need to set these again + if(!g_ActiveConfig.backend_info.bSupportsGLSLUBO) + PixelShaderManager::SetConstants(); // Need to set these again, if we don't support UBO if (g_nativeVertexFmt) g_nativeVertexFmt->SetupVertexPointers(); for (int i = 0; i < 8; i++) diff --git a/Source/Plugins/Plugin_VideoOGL/Src/VertexShaderCache.cpp b/Source/Plugins/Plugin_VideoOGL/Src/VertexShaderCache.cpp index 143340277e..dfa3293a46 100644 --- a/Source/Plugins/Plugin_VideoOGL/Src/VertexShaderCache.cpp +++ b/Source/Plugins/Plugin_VideoOGL/Src/VertexShaderCache.cpp @@ -252,6 +252,11 @@ void SetGLSLVSConstant4f(unsigned int const_number, float f1, float f2, float f3 glUniform4fv(const_number, 1, buf); return; } + if (g_ActiveConfig.backend_info.bSupportsGLSLUBO) + { + ProgramShaderCache::SetUniformObjects(1, const_number, buf); + return; + } for( unsigned int a = 0; a < 9; ++a) { if( const_number >= VSVar_Loc[a].reg && const_number < ( VSVar_Loc[a].reg + VSVar_Loc[a].size)) @@ -270,6 +275,11 @@ void SetGLSLVSConstant4fv(unsigned int const_number, const float *f) glUniform4fv(const_number, 1, f); return; } + if (g_ActiveConfig.backend_info.bSupportsGLSLUBO) + { + ProgramShaderCache::SetUniformObjects(1, const_number, f); + return; + } for( unsigned int a = 0; a < 9; ++a) { if( const_number >= VSVar_Loc[a].reg && const_number < ( VSVar_Loc[a].reg + VSVar_Loc[a].size)) @@ -288,6 +298,11 @@ void SetMultiGLSLVSConstant4fv(unsigned int const_number, unsigned int count, co glUniform4fv(const_number, count, f); return; } + if (g_ActiveConfig.backend_info.bSupportsGLSLUBO) + { + ProgramShaderCache::SetUniformObjects(1, const_number, f, count); + return; + } for( unsigned int a = 0; a < 9; ++a) { if( const_number >= VSVar_Loc[a].reg && const_number < ( VSVar_Loc[a].reg + VSVar_Loc[a].size)) @@ -314,6 +329,11 @@ void SetMultiGLSLVSConstant3fv(unsigned int const_number, unsigned int count, co glUniform4fv(const_number, count, buf); return; } + if (g_ActiveConfig.backend_info.bSupportsGLSLUBO) + { + ProgramShaderCache::SetUniformObjects(1, const_number, buf, count); + return; + } for( unsigned int a = 0; a < 9; ++a) { if( const_number >= VSVar_Loc[a].reg && const_number < ( VSVar_Loc[a].reg + VSVar_Loc[a].size)) diff --git a/Source/Plugins/Plugin_VideoOGL/Src/main.cpp b/Source/Plugins/Plugin_VideoOGL/Src/main.cpp index 0d1c3fc35a..c4e2a8f0ea 100644 --- a/Source/Plugins/Plugin_VideoOGL/Src/main.cpp +++ b/Source/Plugins/Plugin_VideoOGL/Src/main.cpp @@ -83,6 +83,7 @@ Make AA apply instantly during gameplay if possible #include "PixelShaderManager.h" #include "VertexShaderCache.h" #include "VertexShaderManager.h" +#include "ProgramShaderCache.h" #include "CommandProcessor.h" #include "PixelEngine.h" #include "TextureConverter.h" @@ -199,6 +200,7 @@ void VideoBackend::Video_Prepare() VertexShaderManager::Init(); PixelShaderCache::Init(); PixelShaderManager::Init(); + ProgramShaderCache::Init(); PostProcessing::Init(); GL_REPORT_ERRORD(); VertexLoaderManager::Init(); @@ -226,6 +228,7 @@ void VideoBackend::Shutdown() // And need to be called from the video thread TextureConverter::Shutdown(); VertexLoaderManager::Shutdown(); + ProgramShaderCache::Shutdown(); VertexShaderCache::Shutdown(); VertexShaderManager::Shutdown(); PixelShaderManager::Shutdown(); From e8087aa1a9f3471be92af06d6f35917eb835642c Mon Sep 17 00:00:00 2001 From: Ryan Houdek Date: Fri, 9 Dec 2011 19:14:02 -0600 Subject: [PATCH 23/83] Remove bSupportsGLSLLocation since it won't work how I expect it. --- .../Core/VideoCommon/Src/PixelShaderGen.cpp | 34 +++++++------------ .../Core/VideoCommon/Src/VertexShaderGen.cpp | 25 +++++++------- Source/Core/VideoCommon/Src/VideoConfig.h | 1 - .../Plugin_VideoOGL/Src/PixelShaderCache.cpp | 20 ----------- .../Src/ProgramShaderCache.cpp | 2 +- .../Plugin_VideoOGL/Src/VertexShaderCache.cpp | 20 ----------- 6 files changed, 26 insertions(+), 76 deletions(-) diff --git a/Source/Core/VideoCommon/Src/PixelShaderGen.cpp b/Source/Core/VideoCommon/Src/PixelShaderGen.cpp index 8d62858ecc..cd7ecd3461 100644 --- a/Source/Core/VideoCommon/Src/PixelShaderGen.cpp +++ b/Source/Core/VideoCommon/Src/PixelShaderGen.cpp @@ -516,14 +516,6 @@ const char* WriteBinding(API_TYPE ApiType, const u32 num) sprintf(result, "layout(binding = %d) ", num); return result; } -const char* WriteLocation(API_TYPE ApiType, const u32 num) -{ - if(ApiType != API_GLSL || !g_ActiveConfig.backend_info.bSupportsGLSLLocation) - return ""; - static char result[64]; - sprintf(result, "layout(location = %d) ", num); - return result; -} const char *GeneratePixelShaderCode(DSTALPHA_MODE dstAlphaMode, API_TYPE ApiType, u32 components) { @@ -553,13 +545,13 @@ const char *GeneratePixelShaderCode(DSTALPHA_MODE dstAlphaMode, API_TYPE ApiType if(ApiType == API_GLSL) { // A few required defines and ones that will make our lives a lot easier - if (g_ActiveConfig.backend_info.bSupportsGLSLBinding || g_ActiveConfig.backend_info.bSupportsGLSLLocation) + if (g_ActiveConfig.backend_info.bSupportsGLSLBinding || g_ActiveConfig.backend_info.bSupportsGLSLUBO) { WRITE(p, "#version 330 compatibility\n"); if (g_ActiveConfig.backend_info.bSupportsGLSLBinding) WRITE(p, "#extension GL_ARB_shading_language_420pack : enable\n"); - if (g_ActiveConfig.backend_info.bSupportsGLSLLocation) - WRITE(p, "#extension GL_ARB_separate_shader_objects : enable\n"); + if (g_ActiveConfig.backend_info.bSupportsGLSLUBO) + WRITE(p, "#extension GL_ARB_uniform_buffer_object : enable\n"); } else WRITE(p, "#version 120\n"); @@ -613,18 +605,18 @@ const char *GeneratePixelShaderCode(DSTALPHA_MODE dstAlphaMode, API_TYPE ApiType if(ApiType == API_GLSL && g_ActiveConfig.backend_info.bSupportsGLSLUBO) WRITE(p, "layout(std140, binding = 0) uniform PSBlock {\n"); - WRITE(p, "%suniform float4 "I_COLORS"[4] %s;\n", WriteLocation(ApiType, C_COLORS), WriteRegister(ApiType, "c", C_COLORS)); - WRITE(p, "%suniform float4 "I_KCOLORS"[4] %s;\n", WriteLocation(ApiType, C_KCOLORS), WriteRegister(ApiType, "c", C_KCOLORS)); - WRITE(p, "%suniform float4 "I_ALPHA"[1] %s;\n", WriteLocation(ApiType, C_ALPHA), WriteRegister(ApiType, "c", C_ALPHA)); - WRITE(p, "%suniform float4 "I_TEXDIMS"[8] %s;\n", WriteLocation(ApiType, C_TEXDIMS), WriteRegister(ApiType, "c", C_TEXDIMS)); - WRITE(p, "%suniform float4 "I_ZBIAS"[2] %s;\n", WriteLocation(ApiType, C_ZBIAS), WriteRegister(ApiType, "c", C_ZBIAS)); - WRITE(p, "%suniform float4 "I_INDTEXSCALE"[2] %s;\n", WriteLocation(ApiType, C_INDTEXSCALE), WriteRegister(ApiType, "c", C_INDTEXSCALE)); - WRITE(p, "%suniform float4 "I_INDTEXMTX"[6] %s;\n", WriteLocation(ApiType, C_INDTEXMTX), WriteRegister(ApiType, "c", C_INDTEXMTX)); - WRITE(p, "%suniform float4 "I_FOG"[3] %s;\n", WriteLocation(ApiType, C_FOG), WriteRegister(ApiType, "c", C_FOG)); + WRITE(p, "uniform float4 "I_COLORS"[4] %s;\n", WriteRegister(ApiType, "c", C_COLORS)); + WRITE(p, "uniform float4 "I_KCOLORS"[4] %s;\n", WriteRegister(ApiType, "c", C_KCOLORS)); + WRITE(p, "uniform float4 "I_ALPHA"[1] %s;\n", WriteRegister(ApiType, "c", C_ALPHA)); + WRITE(p, "uniform float4 "I_TEXDIMS"[8] %s;\n", WriteRegister(ApiType, "c", C_TEXDIMS)); + WRITE(p, "uniform float4 "I_ZBIAS"[2] %s;\n", WriteRegister(ApiType, "c", C_ZBIAS)); + WRITE(p, "uniform float4 "I_INDTEXSCALE"[2] %s;\n", WriteRegister(ApiType, "c", C_INDTEXSCALE)); + WRITE(p, "uniform float4 "I_INDTEXMTX"[6] %s;\n", WriteRegister(ApiType, "c", C_INDTEXMTX)); + WRITE(p, "uniform float4 "I_FOG"[3] %s;\n", WriteRegister(ApiType, "c", C_FOG)); // Compiler will optimize these out by itself. - WRITE(p, "%suniform float4 "I_PLIGHTS"[40] %s;\n", WriteLocation(ApiType, C_PLIGHTS), WriteRegister(ApiType, "c", C_PLIGHTS)); - WRITE(p, "%suniform float4 "I_PMATERIALS"[4] %s;\n", WriteLocation(ApiType, C_PMATERIALS), WriteRegister(ApiType, "c", C_PMATERIALS)); + WRITE(p, "uniform float4 "I_PLIGHTS"[40] %s;\n", WriteRegister(ApiType, "c", C_PLIGHTS)); + WRITE(p, "uniform float4 "I_PMATERIALS"[4] %s;\n", WriteRegister(ApiType, "c", C_PMATERIALS)); if(ApiType == API_GLSL && g_ActiveConfig.backend_info.bSupportsGLSLUBO) WRITE(p, "};\n"); diff --git a/Source/Core/VideoCommon/Src/VertexShaderGen.cpp b/Source/Core/VideoCommon/Src/VertexShaderGen.cpp index f2559d929c..770208552b 100644 --- a/Source/Core/VideoCommon/Src/VertexShaderGen.cpp +++ b/Source/Core/VideoCommon/Src/VertexShaderGen.cpp @@ -165,7 +165,6 @@ char* GenerateVSOutputStruct(char* p, u32 components, API_TYPE ApiType) extern const char* WriteRegister(API_TYPE ApiType, const char *prefix, const u32 num); extern const char* WriteBinding(API_TYPE ApiType, const u32 num); -extern const char* WriteLocation(API_TYPE ApiType, const u32 num); const char *GenerateVertexShaderCode(u32 components, API_TYPE ApiType) { @@ -188,13 +187,13 @@ const char *GenerateVertexShaderCode(u32 components, API_TYPE ApiType) if(ApiType == API_GLSL) { // A few required defines and ones that will make our lives a lot easier - if (g_ActiveConfig.backend_info.bSupportsGLSLBinding || g_ActiveConfig.backend_info.bSupportsGLSLLocation) + if (g_ActiveConfig.backend_info.bSupportsGLSLBinding || g_ActiveConfig.backend_info.bSupportsGLSLUBO) { WRITE(p, "#version 330 compatibility\n"); if (g_ActiveConfig.backend_info.bSupportsGLSLBinding) WRITE(p, "#extension GL_ARB_shading_language_420pack : enable\n"); - if (g_ActiveConfig.backend_info.bSupportsGLSLLocation) - WRITE(p, "#extension GL_ARB_separate_shader_objects : enable\n"); + if (g_ActiveConfig.backend_info.bSupportsGLSLUBO) + WRITE(p, "#extension GL_ARB_uniform_buffer_object : enable\n"); WRITE(p, "#define ATTRIN in\n"); WRITE(p, "#define ATTROUT out\n"); } @@ -219,15 +218,15 @@ const char *GenerateVertexShaderCode(u32 components, API_TYPE ApiType) if(ApiType == API_GLSL && g_ActiveConfig.backend_info.bSupportsGLSLUBO) WRITE(p, "layout(std140, binding = 1) uniform VSBlock {\n"); - WRITE(p, "%suniform float4 "I_POSNORMALMATRIX"[6] %s;\n", WriteLocation(ApiType, C_POSNORMALMATRIX), WriteRegister(ApiType, "c", C_POSNORMALMATRIX)); - WRITE(p, "%suniform float4 "I_PROJECTION"[4] %s;\n", WriteLocation(ApiType, C_PROJECTION), WriteRegister(ApiType, "c", C_PROJECTION)); - WRITE(p, "%suniform float4 "I_MATERIALS"[4] %s;\n", WriteLocation(ApiType, C_MATERIALS), WriteRegister(ApiType, "c", C_MATERIALS)); - WRITE(p, "%suniform float4 "I_LIGHTS"[40] %s;\n", WriteLocation(ApiType, C_LIGHTS), WriteRegister(ApiType, "c", C_LIGHTS)); - WRITE(p, "%suniform float4 "I_TEXMATRICES"[24] %s;\n", WriteLocation(ApiType, C_TEXMATRICES), WriteRegister(ApiType, "c", C_TEXMATRICES)); // also using tex matrices - WRITE(p, "%suniform float4 "I_TRANSFORMMATRICES"[64] %s;\n", WriteLocation(ApiType, C_TRANSFORMMATRICES), WriteRegister(ApiType, "c", C_TRANSFORMMATRICES)); - WRITE(p, "%suniform float4 "I_NORMALMATRICES"[32] %s;\n", WriteLocation(ApiType, C_NORMALMATRICES), WriteRegister(ApiType, "c", C_NORMALMATRICES)); - WRITE(p, "%suniform float4 "I_POSTTRANSFORMMATRICES"[64] %s;\n", WriteLocation(ApiType, C_POSTTRANSFORMMATRICES), WriteRegister(ApiType, "c", C_POSTTRANSFORMMATRICES)); - WRITE(p, "%suniform float4 "I_DEPTHPARAMS" %s;\n", WriteLocation(ApiType, C_DEPTHPARAMS), WriteRegister(ApiType, "c", C_DEPTHPARAMS)); + WRITE(p, "uniform float4 "I_POSNORMALMATRIX"[6] %s;\n", WriteRegister(ApiType, "c", C_POSNORMALMATRIX)); + WRITE(p, "uniform float4 "I_PROJECTION"[4] %s;\n", WriteRegister(ApiType, "c", C_PROJECTION)); + WRITE(p, "uniform float4 "I_MATERIALS"[4] %s;\n", WriteRegister(ApiType, "c", C_MATERIALS)); + WRITE(p, "uniform float4 "I_LIGHTS"[40] %s;\n", WriteRegister(ApiType, "c", C_LIGHTS)); + WRITE(p, "uniform float4 "I_TEXMATRICES"[24] %s;\n", WriteRegister(ApiType, "c", C_TEXMATRICES)); // also using tex matrices + WRITE(p, "uniform float4 "I_TRANSFORMMATRICES"[64] %s;\n", WriteRegister(ApiType, "c", C_TRANSFORMMATRICES)); + WRITE(p, "uniform float4 "I_NORMALMATRICES"[32] %s;\n", WriteRegister(ApiType, "c", C_NORMALMATRICES)); + WRITE(p, "uniform float4 "I_POSTTRANSFORMMATRICES"[64] %s;\n", WriteRegister(ApiType, "c", C_POSTTRANSFORMMATRICES)); + WRITE(p, "uniform float4 "I_DEPTHPARAMS" %s;\n", WriteRegister(ApiType, "c", C_DEPTHPARAMS)); if(ApiType == API_GLSL && g_ActiveConfig.backend_info.bSupportsGLSLUBO) WRITE(p, "};\n"); diff --git a/Source/Core/VideoCommon/Src/VideoConfig.h b/Source/Core/VideoCommon/Src/VideoConfig.h index a8d3a9af27..84c71c0497 100644 --- a/Source/Core/VideoCommon/Src/VideoConfig.h +++ b/Source/Core/VideoCommon/Src/VideoConfig.h @@ -170,7 +170,6 @@ struct VideoConfig bool bSupportsGLSL; bool bSupportsGLSLBinding; - bool bSupportsGLSLLocation; bool bSupportsGLSLUBO; } backend_info; }; diff --git a/Source/Plugins/Plugin_VideoOGL/Src/PixelShaderCache.cpp b/Source/Plugins/Plugin_VideoOGL/Src/PixelShaderCache.cpp index 978357e18f..7e18e54c7b 100644 --- a/Source/Plugins/Plugin_VideoOGL/Src/PixelShaderCache.cpp +++ b/Source/Plugins/Plugin_VideoOGL/Src/PixelShaderCache.cpp @@ -81,11 +81,6 @@ void PixelShaderCache::Init() // Should this be set here? if (strstr((const char*)glGetString(GL_EXTENSIONS), "GL_ARB_shading_language_420pack") != NULL) g_Config.backend_info.bSupportsGLSLBinding = true; - // This bit doesn't quite work yet, always set to false - // TODO: Probably just drop this entirely in favour of UBOS - //if (strstr((const char*)glGetString(GL_EXTENSIONS), "GL_ARB_separate_shader_objects") != NULL) - g_Config.backend_info.bSupportsGLSLLocation = false; - if (strstr((const char*)glGetString(GL_EXTENSIONS), "GL_ARB_uniform_buffer_object") != NULL) g_Config.backend_info.bSupportsGLSLUBO = true; @@ -505,11 +500,6 @@ void SetGLSLPSConstant4f(unsigned int const_number, float f1, float f2, float f3 { float f[4] = { f1, f2, f3, f4 }; - if (g_ActiveConfig.backend_info.bSupportsGLSLLocation) - { - glUniform4fv(const_number, 1, f); - return; - } if (g_ActiveConfig.backend_info.bSupportsGLSLUBO) { ProgramShaderCache::SetUniformObjects(0, const_number, f); @@ -528,11 +518,6 @@ void SetGLSLPSConstant4f(unsigned int const_number, float f1, float f2, float f3 void SetGLSLPSConstant4fv(unsigned int const_number, const float *f) { - if (g_ActiveConfig.backend_info.bSupportsGLSLLocation) - { - glUniform4fv(const_number, 1, f); - return; - } if (g_ActiveConfig.backend_info.bSupportsGLSLUBO) { ProgramShaderCache::SetUniformObjects(0, const_number, f); @@ -551,11 +536,6 @@ void SetGLSLPSConstant4fv(unsigned int const_number, const float *f) void SetMultiGLSLPSConstant4fv(unsigned int const_number, unsigned int count, const float *f) { - if (g_ActiveConfig.backend_info.bSupportsGLSLLocation) - { - glUniform4fv(const_number, count, f); - return; - } if (g_ActiveConfig.backend_info.bSupportsGLSLUBO) { ProgramShaderCache::SetUniformObjects(0, const_number, f, count); diff --git a/Source/Plugins/Plugin_VideoOGL/Src/ProgramShaderCache.cpp b/Source/Plugins/Plugin_VideoOGL/Src/ProgramShaderCache.cpp index 776280103e..c7c8624069 100644 --- a/Source/Plugins/Plugin_VideoOGL/Src/ProgramShaderCache.cpp +++ b/Source/Plugins/Plugin_VideoOGL/Src/ProgramShaderCache.cpp @@ -92,7 +92,7 @@ namespace OGL glAttachShader(entry.program.glprogid, entry.program.psid); glLinkProgram(entry.program.glprogid); - + glUseProgram(entry.program.glprogid); diff --git a/Source/Plugins/Plugin_VideoOGL/Src/VertexShaderCache.cpp b/Source/Plugins/Plugin_VideoOGL/Src/VertexShaderCache.cpp index dfa3293a46..d0fb9ec189 100644 --- a/Source/Plugins/Plugin_VideoOGL/Src/VertexShaderCache.cpp +++ b/Source/Plugins/Plugin_VideoOGL/Src/VertexShaderCache.cpp @@ -247,11 +247,6 @@ void SetGLSLVSConstant4f(unsigned int const_number, float f1, float f2, float f3 buf[1] = f2; buf[2] = f3; buf[3] = f4; - if(g_ActiveConfig.backend_info.bSupportsGLSLLocation) - { - glUniform4fv(const_number, 1, buf); - return; - } if (g_ActiveConfig.backend_info.bSupportsGLSLUBO) { ProgramShaderCache::SetUniformObjects(1, const_number, buf); @@ -270,11 +265,6 @@ void SetGLSLVSConstant4f(unsigned int const_number, float f1, float f2, float f3 void SetGLSLVSConstant4fv(unsigned int const_number, const float *f) { - if(g_ActiveConfig.backend_info.bSupportsGLSLLocation) - { - glUniform4fv(const_number, 1, f); - return; - } if (g_ActiveConfig.backend_info.bSupportsGLSLUBO) { ProgramShaderCache::SetUniformObjects(1, const_number, f); @@ -293,11 +283,6 @@ void SetGLSLVSConstant4fv(unsigned int const_number, const float *f) void SetMultiGLSLVSConstant4fv(unsigned int const_number, unsigned int count, const float *f) { - if(g_ActiveConfig.backend_info.bSupportsGLSLLocation) - { - glUniform4fv(const_number, count, f); - return; - } if (g_ActiveConfig.backend_info.bSupportsGLSLUBO) { ProgramShaderCache::SetUniformObjects(1, const_number, f, count); @@ -324,11 +309,6 @@ void SetMultiGLSLVSConstant3fv(unsigned int const_number, unsigned int count, co buf[4*i+2] = *f++; buf[4*i+3] = 0.f; } - if(g_ActiveConfig.backend_info.bSupportsGLSLLocation) - { - glUniform4fv(const_number, count, buf); - return; - } if (g_ActiveConfig.backend_info.bSupportsGLSLUBO) { ProgramShaderCache::SetUniformObjects(1, const_number, buf, count); From 54a90d08cec61253634c06e7dfc494c19f2cc253 Mon Sep 17 00:00:00 2001 From: Ryan Houdek Date: Fri, 9 Dec 2011 21:15:15 -0600 Subject: [PATCH 24/83] Fix one error. --- .../Core/VideoCommon/Src/PixelShaderGen.cpp | 28 ++++++++++++------- .../Core/VideoCommon/Src/VertexShaderGen.cpp | 19 +++++++------ .../Plugin_VideoOGL/Src/PixelShaderCache.cpp | 6 ++-- .../Src/ProgramShaderCache.cpp | 1 - .../Plugin_VideoOGL/Src/VertexShaderCache.cpp | 8 +++--- 5 files changed, 35 insertions(+), 27 deletions(-) diff --git a/Source/Core/VideoCommon/Src/PixelShaderGen.cpp b/Source/Core/VideoCommon/Src/PixelShaderGen.cpp index cd7ecd3461..e88a8ca11f 100644 --- a/Source/Core/VideoCommon/Src/PixelShaderGen.cpp +++ b/Source/Core/VideoCommon/Src/PixelShaderGen.cpp @@ -516,6 +516,14 @@ const char* WriteBinding(API_TYPE ApiType, const u32 num) sprintf(result, "layout(binding = %d) ", num); return result; } +const char *WriteLocation(API_TYPE ApiType) +{ + static char result[64]; + if(ApiType == API_GLSL && g_ActiveConfig.backend_info.bSupportsGLSLUBO) + return ""; + sprintf(result, "uniform "); + return result; +} const char *GeneratePixelShaderCode(DSTALPHA_MODE dstAlphaMode, API_TYPE ApiType, u32 components) { @@ -605,18 +613,18 @@ const char *GeneratePixelShaderCode(DSTALPHA_MODE dstAlphaMode, API_TYPE ApiType if(ApiType == API_GLSL && g_ActiveConfig.backend_info.bSupportsGLSLUBO) WRITE(p, "layout(std140, binding = 0) uniform PSBlock {\n"); - WRITE(p, "uniform float4 "I_COLORS"[4] %s;\n", WriteRegister(ApiType, "c", C_COLORS)); - WRITE(p, "uniform float4 "I_KCOLORS"[4] %s;\n", WriteRegister(ApiType, "c", C_KCOLORS)); - WRITE(p, "uniform float4 "I_ALPHA"[1] %s;\n", WriteRegister(ApiType, "c", C_ALPHA)); - WRITE(p, "uniform float4 "I_TEXDIMS"[8] %s;\n", WriteRegister(ApiType, "c", C_TEXDIMS)); - WRITE(p, "uniform float4 "I_ZBIAS"[2] %s;\n", WriteRegister(ApiType, "c", C_ZBIAS)); - WRITE(p, "uniform float4 "I_INDTEXSCALE"[2] %s;\n", WriteRegister(ApiType, "c", C_INDTEXSCALE)); - WRITE(p, "uniform float4 "I_INDTEXMTX"[6] %s;\n", WriteRegister(ApiType, "c", C_INDTEXMTX)); - WRITE(p, "uniform float4 "I_FOG"[3] %s;\n", WriteRegister(ApiType, "c", C_FOG)); + WRITE(p, "%sfloat4 "I_COLORS"[4] %s;\n", WriteLocation(ApiType), WriteRegister(ApiType, "c", C_COLORS)); + WRITE(p, "%sfloat4 "I_KCOLORS"[4] %s;\n", WriteLocation(ApiType), WriteRegister(ApiType, "c", C_KCOLORS)); + WRITE(p, "%sfloat4 "I_ALPHA"[1] %s;\n", WriteLocation(ApiType), WriteRegister(ApiType, "c", C_ALPHA)); + WRITE(p, "%sfloat4 "I_TEXDIMS"[8] %s;\n", WriteLocation(ApiType), WriteRegister(ApiType, "c", C_TEXDIMS)); + WRITE(p, "%sfloat4 "I_ZBIAS"[2] %s;\n", WriteLocation(ApiType), WriteRegister(ApiType, "c", C_ZBIAS)); + WRITE(p, "%sfloat4 "I_INDTEXSCALE"[2] %s;\n", WriteLocation(ApiType), WriteRegister(ApiType, "c", C_INDTEXSCALE)); + WRITE(p, "%sfloat4 "I_INDTEXMTX"[6] %s;\n", WriteLocation(ApiType), WriteRegister(ApiType, "c", C_INDTEXMTX)); + WRITE(p, "%sfloat4 "I_FOG"[3] %s;\n", WriteLocation(ApiType), WriteRegister(ApiType, "c", C_FOG)); // Compiler will optimize these out by itself. - WRITE(p, "uniform float4 "I_PLIGHTS"[40] %s;\n", WriteRegister(ApiType, "c", C_PLIGHTS)); - WRITE(p, "uniform float4 "I_PMATERIALS"[4] %s;\n", WriteRegister(ApiType, "c", C_PMATERIALS)); + WRITE(p, "%sfloat4 "I_PLIGHTS"[40] %s;\n", WriteLocation(ApiType), WriteRegister(ApiType, "c", C_PLIGHTS)); + WRITE(p, "%sfloat4 "I_PMATERIALS"[4] %s;\n", WriteLocation(ApiType), WriteRegister(ApiType, "c", C_PMATERIALS)); if(ApiType == API_GLSL && g_ActiveConfig.backend_info.bSupportsGLSLUBO) WRITE(p, "};\n"); diff --git a/Source/Core/VideoCommon/Src/VertexShaderGen.cpp b/Source/Core/VideoCommon/Src/VertexShaderGen.cpp index 770208552b..c04a2ba1f1 100644 --- a/Source/Core/VideoCommon/Src/VertexShaderGen.cpp +++ b/Source/Core/VideoCommon/Src/VertexShaderGen.cpp @@ -165,6 +165,7 @@ char* GenerateVSOutputStruct(char* p, u32 components, API_TYPE ApiType) extern const char* WriteRegister(API_TYPE ApiType, const char *prefix, const u32 num); extern const char* WriteBinding(API_TYPE ApiType, const u32 num); +const char *WriteLocation(API_TYPE ApiType); const char *GenerateVertexShaderCode(u32 components, API_TYPE ApiType) { @@ -218,15 +219,15 @@ const char *GenerateVertexShaderCode(u32 components, API_TYPE ApiType) if(ApiType == API_GLSL && g_ActiveConfig.backend_info.bSupportsGLSLUBO) WRITE(p, "layout(std140, binding = 1) uniform VSBlock {\n"); - WRITE(p, "uniform float4 "I_POSNORMALMATRIX"[6] %s;\n", WriteRegister(ApiType, "c", C_POSNORMALMATRIX)); - WRITE(p, "uniform float4 "I_PROJECTION"[4] %s;\n", WriteRegister(ApiType, "c", C_PROJECTION)); - WRITE(p, "uniform float4 "I_MATERIALS"[4] %s;\n", WriteRegister(ApiType, "c", C_MATERIALS)); - WRITE(p, "uniform float4 "I_LIGHTS"[40] %s;\n", WriteRegister(ApiType, "c", C_LIGHTS)); - WRITE(p, "uniform float4 "I_TEXMATRICES"[24] %s;\n", WriteRegister(ApiType, "c", C_TEXMATRICES)); // also using tex matrices - WRITE(p, "uniform float4 "I_TRANSFORMMATRICES"[64] %s;\n", WriteRegister(ApiType, "c", C_TRANSFORMMATRICES)); - WRITE(p, "uniform float4 "I_NORMALMATRICES"[32] %s;\n", WriteRegister(ApiType, "c", C_NORMALMATRICES)); - WRITE(p, "uniform float4 "I_POSTTRANSFORMMATRICES"[64] %s;\n", WriteRegister(ApiType, "c", C_POSTTRANSFORMMATRICES)); - WRITE(p, "uniform float4 "I_DEPTHPARAMS" %s;\n", WriteRegister(ApiType, "c", C_DEPTHPARAMS)); + WRITE(p, "%sfloat4 "I_POSNORMALMATRIX"[6] %s;\n", WriteLocation(ApiType), WriteRegister(ApiType, "c", C_POSNORMALMATRIX)); + WRITE(p, "%sfloat4 "I_PROJECTION"[4] %s;\n", WriteLocation(ApiType), WriteRegister(ApiType, "c", C_PROJECTION)); + WRITE(p, "%sfloat4 "I_MATERIALS"[4] %s;\n", WriteLocation(ApiType), WriteRegister(ApiType, "c", C_MATERIALS)); + WRITE(p, "%sfloat4 "I_LIGHTS"[40] %s;\n", WriteLocation(ApiType), WriteRegister(ApiType, "c", C_LIGHTS)); + WRITE(p, "%sfloat4 "I_TEXMATRICES"[24] %s;\n", WriteLocation(ApiType), WriteRegister(ApiType, "c", C_TEXMATRICES)); // also using tex matrices + WRITE(p, "%sfloat4 "I_TRANSFORMMATRICES"[64] %s;\n", WriteLocation(ApiType), WriteRegister(ApiType, "c", C_TRANSFORMMATRICES)); + WRITE(p, "%sfloat4 "I_NORMALMATRICES"[32] %s;\n", WriteLocation(ApiType), WriteRegister(ApiType, "c", C_NORMALMATRICES)); + WRITE(p, "%sfloat4 "I_POSTTRANSFORMMATRICES"[64] %s;\n", WriteLocation(ApiType), WriteRegister(ApiType, "c", C_POSTTRANSFORMMATRICES)); + WRITE(p, "%sfloat4 "I_DEPTHPARAMS" %s;\n", WriteLocation(ApiType), WriteRegister(ApiType, "c", C_DEPTHPARAMS)); if(ApiType == API_GLSL && g_ActiveConfig.backend_info.bSupportsGLSLUBO) WRITE(p, "};\n"); diff --git a/Source/Plugins/Plugin_VideoOGL/Src/PixelShaderCache.cpp b/Source/Plugins/Plugin_VideoOGL/Src/PixelShaderCache.cpp index 7e18e54c7b..50382cc0dd 100644 --- a/Source/Plugins/Plugin_VideoOGL/Src/PixelShaderCache.cpp +++ b/Source/Plugins/Plugin_VideoOGL/Src/PixelShaderCache.cpp @@ -503,7 +503,7 @@ void SetGLSLPSConstant4f(unsigned int const_number, float f1, float f2, float f3 if (g_ActiveConfig.backend_info.bSupportsGLSLUBO) { ProgramShaderCache::SetUniformObjects(0, const_number, f); - return; + //return; } for (unsigned int a = 0; a < 10; ++a) { @@ -521,7 +521,7 @@ void SetGLSLPSConstant4fv(unsigned int const_number, const float *f) if (g_ActiveConfig.backend_info.bSupportsGLSLUBO) { ProgramShaderCache::SetUniformObjects(0, const_number, f); - return; + //return; } for (unsigned int a = 0; a < 10; ++a) { @@ -539,7 +539,7 @@ void SetMultiGLSLPSConstant4fv(unsigned int const_number, unsigned int count, co if (g_ActiveConfig.backend_info.bSupportsGLSLUBO) { ProgramShaderCache::SetUniformObjects(0, const_number, f, count); - return; + //return; } for (unsigned int a = 0; a < 10; ++a) { diff --git a/Source/Plugins/Plugin_VideoOGL/Src/ProgramShaderCache.cpp b/Source/Plugins/Plugin_VideoOGL/Src/ProgramShaderCache.cpp index c7c8624069..9fd3479e1d 100644 --- a/Source/Plugins/Plugin_VideoOGL/Src/ProgramShaderCache.cpp +++ b/Source/Plugins/Plugin_VideoOGL/Src/ProgramShaderCache.cpp @@ -94,7 +94,6 @@ namespace OGL glLinkProgram(entry.program.glprogid); glUseProgram(entry.program.glprogid); - // We cache our uniform locations for now // Once we move up to a newer version of GLSL, ~1.30 diff --git a/Source/Plugins/Plugin_VideoOGL/Src/VertexShaderCache.cpp b/Source/Plugins/Plugin_VideoOGL/Src/VertexShaderCache.cpp index d0fb9ec189..e22a2f3be2 100644 --- a/Source/Plugins/Plugin_VideoOGL/Src/VertexShaderCache.cpp +++ b/Source/Plugins/Plugin_VideoOGL/Src/VertexShaderCache.cpp @@ -250,7 +250,7 @@ void SetGLSLVSConstant4f(unsigned int const_number, float f1, float f2, float f3 if (g_ActiveConfig.backend_info.bSupportsGLSLUBO) { ProgramShaderCache::SetUniformObjects(1, const_number, buf); - return; + //return; } for( unsigned int a = 0; a < 9; ++a) { @@ -268,7 +268,7 @@ void SetGLSLVSConstant4fv(unsigned int const_number, const float *f) if (g_ActiveConfig.backend_info.bSupportsGLSLUBO) { ProgramShaderCache::SetUniformObjects(1, const_number, f); - return; + //return; } for( unsigned int a = 0; a < 9; ++a) { @@ -286,7 +286,7 @@ void SetMultiGLSLVSConstant4fv(unsigned int const_number, unsigned int count, co if (g_ActiveConfig.backend_info.bSupportsGLSLUBO) { ProgramShaderCache::SetUniformObjects(1, const_number, f, count); - return; + //return; } for( unsigned int a = 0; a < 9; ++a) { @@ -312,7 +312,7 @@ void SetMultiGLSLVSConstant3fv(unsigned int const_number, unsigned int count, co if (g_ActiveConfig.backend_info.bSupportsGLSLUBO) { ProgramShaderCache::SetUniformObjects(1, const_number, buf, count); - return; + //return; } for( unsigned int a = 0; a < 9; ++a) { From 126dfa073b70e91aa9482fac1052f71d7be1bcc2 Mon Sep 17 00:00:00 2001 From: Ryan Houdek Date: Sat, 10 Dec 2011 01:56:37 -0600 Subject: [PATCH 25/83] Firin ma lazer --- .../Core/VideoCommon/Src/PixelShaderGen.cpp | 2 +- .../Core/VideoCommon/Src/VertexShaderGen.cpp | 2 +- .../Src/ProgramShaderCache.cpp | 22 ++++++++++++++----- 3 files changed, 18 insertions(+), 8 deletions(-) diff --git a/Source/Core/VideoCommon/Src/PixelShaderGen.cpp b/Source/Core/VideoCommon/Src/PixelShaderGen.cpp index e88a8ca11f..2a5ab11ee6 100644 --- a/Source/Core/VideoCommon/Src/PixelShaderGen.cpp +++ b/Source/Core/VideoCommon/Src/PixelShaderGen.cpp @@ -611,7 +611,7 @@ const char *GeneratePixelShaderCode(DSTALPHA_MODE dstAlphaMode, API_TYPE ApiType WRITE(p, "\n"); if(ApiType == API_GLSL && g_ActiveConfig.backend_info.bSupportsGLSLUBO) - WRITE(p, "layout(std140, binding = 0) uniform PSBlock {\n"); + WRITE(p, "layout(std140, binding = 4) uniform PSBlock {\n"); WRITE(p, "%sfloat4 "I_COLORS"[4] %s;\n", WriteLocation(ApiType), WriteRegister(ApiType, "c", C_COLORS)); WRITE(p, "%sfloat4 "I_KCOLORS"[4] %s;\n", WriteLocation(ApiType), WriteRegister(ApiType, "c", C_KCOLORS)); diff --git a/Source/Core/VideoCommon/Src/VertexShaderGen.cpp b/Source/Core/VideoCommon/Src/VertexShaderGen.cpp index c04a2ba1f1..caefa15bc4 100644 --- a/Source/Core/VideoCommon/Src/VertexShaderGen.cpp +++ b/Source/Core/VideoCommon/Src/VertexShaderGen.cpp @@ -217,7 +217,7 @@ const char *GenerateVertexShaderCode(u32 components, API_TYPE ApiType) // uniforms if(ApiType == API_GLSL && g_ActiveConfig.backend_info.bSupportsGLSLUBO) - WRITE(p, "layout(std140, binding = 1) uniform VSBlock {\n"); + WRITE(p, "layout(std140, binding = 5) uniform VSBlock {\n"); WRITE(p, "%sfloat4 "I_POSNORMALMATRIX"[6] %s;\n", WriteLocation(ApiType), WriteRegister(ApiType, "c", C_POSNORMALMATRIX)); WRITE(p, "%sfloat4 "I_PROJECTION"[4] %s;\n", WriteLocation(ApiType), WriteRegister(ApiType, "c", C_PROJECTION)); diff --git a/Source/Plugins/Plugin_VideoOGL/Src/ProgramShaderCache.cpp b/Source/Plugins/Plugin_VideoOGL/Src/ProgramShaderCache.cpp index 9fd3479e1d..b2bb6b6351 100644 --- a/Source/Plugins/Plugin_VideoOGL/Src/ProgramShaderCache.cpp +++ b/Source/Plugins/Plugin_VideoOGL/Src/ProgramShaderCache.cpp @@ -94,7 +94,15 @@ namespace OGL glLinkProgram(entry.program.glprogid); glUseProgram(entry.program.glprogid); - + + GLint Info = -1; + GLuint Indice = 1; + //glGetIntegeri_v(GL_UNIFORM_BLOCK_DATA_SIZE, 4, &Info); + //glGetIntegerv(GL_UNIFORM_BLOCK_DATA_SIZE, &Info); + glGetActiveUniformsiv(entry.program.glprogid, 1, &Indice, + GL_UNIFORM_SIZE, &Info); + printf("Minimum size: %d\n", Info); + // We cache our uniform locations for now // Once we move up to a newer version of GLSL, ~1.30 // We can remove this @@ -132,7 +140,8 @@ namespace OGL glBindBuffer(GL_UNIFORM_BUFFER, UBOBuffers[Buffer]); // glBufferSubData expects data in bytes, so multiply count by four // Expects the offset in bytes as well, so multiply by *4 *4 since we are passing in a vec4 location - glBufferSubData(GL_UNIFORM_BUFFER, offset * 4 * 4, count * 4, (void*)&f[0]); + glBufferSubData(GL_UNIFORM_BUFFER_EXT, offset * 4 * 4, count * 4 * 4, f); + glBindBuffer(GL_UNIFORM_BUFFER, 0); } GLuint ProgramShaderCache::GetCurrentProgram(void) { return CurrentProgram; } @@ -151,14 +160,15 @@ namespace OGL // We multiply by *4*4 because we need to get down to basic machine units. // So multiply by four to get how many floats we have from vec4s // Then once more to get bytes - glBufferData(GL_UNIFORM_BUFFER, C_PENVCONST_END * 4 * 4, NULL, GL_DYNAMIC_DRAW); + glBufferData(GL_UNIFORM_BUFFER, 1024 *1024 *1024, NULL, GL_DYNAMIC_DRAW); // Now bind the buffer to the index point // We know PS is 0 since we have it statically set in the shader - glBindBufferBase(GL_UNIFORM_BUFFER, 0, UBOBuffers[0]); + glBindBufferRange(GL_UNIFORM_BUFFER, 4, UBOBuffers[0], 0, (C_PENVCONST_END * 4 * 4) - (C_PENVCONST_END * 4 * 4 % 256) + 256); // Repeat for VS shader glBindBuffer(GL_UNIFORM_BUFFER, UBOBuffers[1]); - glBufferData(GL_UNIFORM_BUFFER, C_VENVCONST_END * 4 * 4, NULL, GL_DYNAMIC_DRAW); - glBindBufferBase(GL_UNIFORM_BUFFER, 1, UBOBuffers[1]); + glBufferData(GL_UNIFORM_BUFFER, 1024*1024*1024, NULL, GL_DYNAMIC_DRAW); + glBindBufferRange(GL_UNIFORM_BUFFER, 5, UBOBuffers[1], 0, (C_VENVCONST_END * 4 * 4) - (C_VENVCONST_END * 4 * 4 % 256) + 256); + glBindBuffer(GL_UNIFORM_BUFFER, 0); } void ProgramShaderCache::Shutdown(void) { From 24336171f1f9803a4b98aff7227c5018b7981a55 Mon Sep 17 00:00:00 2001 From: Ryan Houdek Date: Sat, 10 Dec 2011 01:57:27 -0600 Subject: [PATCH 26/83] Firin ma lazer --- Source/Plugins/Plugin_VideoOGL/Src/ProgramShaderCache.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Source/Plugins/Plugin_VideoOGL/Src/ProgramShaderCache.cpp b/Source/Plugins/Plugin_VideoOGL/Src/ProgramShaderCache.cpp index b2bb6b6351..322950edf0 100644 --- a/Source/Plugins/Plugin_VideoOGL/Src/ProgramShaderCache.cpp +++ b/Source/Plugins/Plugin_VideoOGL/Src/ProgramShaderCache.cpp @@ -140,7 +140,7 @@ namespace OGL glBindBuffer(GL_UNIFORM_BUFFER, UBOBuffers[Buffer]); // glBufferSubData expects data in bytes, so multiply count by four // Expects the offset in bytes as well, so multiply by *4 *4 since we are passing in a vec4 location - glBufferSubData(GL_UNIFORM_BUFFER_EXT, offset * 4 * 4, count * 4 * 4, f); + glBufferSubData(GL_UNIFORM_BUFFER, offset * 4 * 4, count * 4 * 4, f); glBindBuffer(GL_UNIFORM_BUFFER, 0); } GLuint ProgramShaderCache::GetCurrentProgram(void) { return CurrentProgram; } From 49664bff61d26d8120daa31b2e0afb2090e7474a Mon Sep 17 00:00:00 2001 From: Ryan Houdek Date: Sat, 10 Dec 2011 02:02:22 -0600 Subject: [PATCH 27/83] More for Billiard <3 --- .../Src/ProgramShaderCache.cpp | 25 ++++++++++--------- 1 file changed, 13 insertions(+), 12 deletions(-) diff --git a/Source/Plugins/Plugin_VideoOGL/Src/ProgramShaderCache.cpp b/Source/Plugins/Plugin_VideoOGL/Src/ProgramShaderCache.cpp index 322950edf0..e174cf2641 100644 --- a/Source/Plugins/Plugin_VideoOGL/Src/ProgramShaderCache.cpp +++ b/Source/Plugins/Plugin_VideoOGL/Src/ProgramShaderCache.cpp @@ -17,7 +17,13 @@ #include "ProgramShaderCache.h" #include - +static GLenum checkForGLError(const char * situation) +{ + GLenum error = glGetError(); + if (error != GL_NO_ERROR) + printf("Error: %d -- %s\n", error, situation); + return error; +} namespace OGL { GLuint ProgramShaderCache::CurrentFShader = 0, ProgramShaderCache::CurrentVShader = 0, ProgramShaderCache::CurrentProgram = 0; @@ -94,14 +100,8 @@ namespace OGL glLinkProgram(entry.program.glprogid); glUseProgram(entry.program.glprogid); + checkForGLError("User"); - GLint Info = -1; - GLuint Indice = 1; - //glGetIntegeri_v(GL_UNIFORM_BLOCK_DATA_SIZE, 4, &Info); - //glGetIntegerv(GL_UNIFORM_BLOCK_DATA_SIZE, &Info); - glGetActiveUniformsiv(entry.program.glprogid, 1, &Indice, - GL_UNIFORM_SIZE, &Info); - printf("Minimum size: %d\n", Info); // We cache our uniform locations for now // Once we move up to a newer version of GLSL, ~1.30 @@ -160,15 +160,16 @@ namespace OGL // We multiply by *4*4 because we need to get down to basic machine units. // So multiply by four to get how many floats we have from vec4s // Then once more to get bytes - glBufferData(GL_UNIFORM_BUFFER, 1024 *1024 *1024, NULL, GL_DYNAMIC_DRAW); + glBufferData(GL_UNIFORM_BUFFER, 1024 *1024, NULL, GL_DYNAMIC_DRAW); // Now bind the buffer to the index point // We know PS is 0 since we have it statically set in the shader - glBindBufferRange(GL_UNIFORM_BUFFER, 4, UBOBuffers[0], 0, (C_PENVCONST_END * 4 * 4) - (C_PENVCONST_END * 4 * 4 % 256) + 256); + glBindBufferBase(GL_UNIFORM_BUFFER, 4, UBOBuffers[0]); // Repeat for VS shader glBindBuffer(GL_UNIFORM_BUFFER, UBOBuffers[1]); - glBufferData(GL_UNIFORM_BUFFER, 1024*1024*1024, NULL, GL_DYNAMIC_DRAW); - glBindBufferRange(GL_UNIFORM_BUFFER, 5, UBOBuffers[1], 0, (C_VENVCONST_END * 4 * 4) - (C_VENVCONST_END * 4 * 4 % 256) + 256); + glBufferData(GL_UNIFORM_BUFFER, 1024*1024, NULL, GL_DYNAMIC_DRAW); + glBindBufferBase(GL_UNIFORM_BUFFER, 5, UBOBuffers[1]); glBindBuffer(GL_UNIFORM_BUFFER, 0); + checkForGLError("Init"); } void ProgramShaderCache::Shutdown(void) { From 7ab38cff68e411bf104ebc55373ca6f2b2ad9c66 Mon Sep 17 00:00:00 2001 From: Ryan Houdek Date: Sat, 10 Dec 2011 07:38:30 -0600 Subject: [PATCH 28/83] UBO works for Pixel Shaders if Binding for UBO is zero, otherwise fails. Probably why Vertex shader UBO is failing. Too tired to investigate right now. --- .../Core/VideoCommon/Src/PixelShaderGen.cpp | 14 +- Source/Core/VideoCommon/Src/PixelShaderGen.h | 4 +- .../Core/VideoCommon/Src/VertexShaderGen.cpp | 33 +++-- Source/Core/VideoCommon/Src/VertexShaderGen.h | 2 +- .../VideoCommon/Src/VertexShaderManager.cpp | 2 +- .../Plugin_VideoOGL/Src/PixelShaderCache.cpp | 13 +- .../Src/ProgramShaderCache.cpp | 56 +++++--- .../Plugin_VideoOGL/Src/TextureConverter.cpp | 133 ++++++++++++------ .../Plugin_VideoOGL/Src/VertexShaderCache.cpp | 7 +- 9 files changed, 176 insertions(+), 88 deletions(-) diff --git a/Source/Core/VideoCommon/Src/PixelShaderGen.cpp b/Source/Core/VideoCommon/Src/PixelShaderGen.cpp index 2a5ab11ee6..b963912443 100644 --- a/Source/Core/VideoCommon/Src/PixelShaderGen.cpp +++ b/Source/Core/VideoCommon/Src/PixelShaderGen.cpp @@ -502,11 +502,11 @@ static void BuildSwapModeTable() const char* WriteRegister(API_TYPE ApiType, const char *prefix, const u32 num) { - if(ApiType == API_GLSL) - return ""; // Nothing to do here - static char result[64]; - sprintf(result, " : register(%s%d)", prefix, num); - return result; + if(ApiType == API_GLSL) + return ""; // Nothing to do here + static char result[64]; + sprintf(result, " : register(%s%d)", prefix, num); + return result; } const char* WriteBinding(API_TYPE ApiType, const u32 num) { @@ -518,9 +518,9 @@ const char* WriteBinding(API_TYPE ApiType, const u32 num) } const char *WriteLocation(API_TYPE ApiType) { - static char result[64]; if(ApiType == API_GLSL && g_ActiveConfig.backend_info.bSupportsGLSLUBO) return ""; + static char result[64]; sprintf(result, "uniform "); return result; } @@ -611,7 +611,7 @@ const char *GeneratePixelShaderCode(DSTALPHA_MODE dstAlphaMode, API_TYPE ApiType WRITE(p, "\n"); if(ApiType == API_GLSL && g_ActiveConfig.backend_info.bSupportsGLSLUBO) - WRITE(p, "layout(std140, binding = 4) uniform PSBlock {\n"); + WRITE(p, "layout(std140, binding = 0) uniform PSBlock {\n"); WRITE(p, "%sfloat4 "I_COLORS"[4] %s;\n", WriteLocation(ApiType), WriteRegister(ApiType, "c", C_COLORS)); WRITE(p, "%sfloat4 "I_KCOLORS"[4] %s;\n", WriteLocation(ApiType), WriteRegister(ApiType, "c", C_KCOLORS)); diff --git a/Source/Core/VideoCommon/Src/PixelShaderGen.h b/Source/Core/VideoCommon/Src/PixelShaderGen.h index 589fc927bc..b32b7408c3 100644 --- a/Source/Core/VideoCommon/Src/PixelShaderGen.h +++ b/Source/Core/VideoCommon/Src/PixelShaderGen.h @@ -28,8 +28,8 @@ #define I_INDTEXSCALE "cindscale" #define I_INDTEXMTX "cindmtx" #define I_FOG "cfog" -#define I_PLIGHTS "cLights" -#define I_PMATERIALS "cmtrl" +#define I_PLIGHTS "cPLights" +#define I_PMATERIALS "cPmtrl" #define C_COLORMATRIX 0 // 0 #define C_COLORS 0 // 0 diff --git a/Source/Core/VideoCommon/Src/VertexShaderGen.cpp b/Source/Core/VideoCommon/Src/VertexShaderGen.cpp index caefa15bc4..c693a7b06b 100644 --- a/Source/Core/VideoCommon/Src/VertexShaderGen.cpp +++ b/Source/Core/VideoCommon/Src/VertexShaderGen.cpp @@ -166,6 +166,12 @@ char* GenerateVSOutputStruct(char* p, u32 components, API_TYPE ApiType) extern const char* WriteRegister(API_TYPE ApiType, const char *prefix, const u32 num); extern const char* WriteBinding(API_TYPE ApiType, const u32 num); const char *WriteLocation(API_TYPE ApiType); +const char *WriteLocation2(API_TYPE ApiType) +{ + static char result[64]; + sprintf(result, "uniform "); + return result; +} const char *GenerateVertexShaderCode(u32 components, API_TYPE ApiType) { @@ -216,21 +222,22 @@ const char *GenerateVertexShaderCode(u32 components, API_TYPE ApiType) } // uniforms - if(ApiType == API_GLSL && g_ActiveConfig.backend_info.bSupportsGLSLUBO) - WRITE(p, "layout(std140, binding = 5) uniform VSBlock {\n"); + //if(ApiType == API_GLSL && g_ActiveConfig.backend_info.bSupportsGLSLUBO) + //WRITE(p, "layout(std140, binding = 2) uniform VSBlock {\n"); - WRITE(p, "%sfloat4 "I_POSNORMALMATRIX"[6] %s;\n", WriteLocation(ApiType), WriteRegister(ApiType, "c", C_POSNORMALMATRIX)); - WRITE(p, "%sfloat4 "I_PROJECTION"[4] %s;\n", WriteLocation(ApiType), WriteRegister(ApiType, "c", C_PROJECTION)); - WRITE(p, "%sfloat4 "I_MATERIALS"[4] %s;\n", WriteLocation(ApiType), WriteRegister(ApiType, "c", C_MATERIALS)); - WRITE(p, "%sfloat4 "I_LIGHTS"[40] %s;\n", WriteLocation(ApiType), WriteRegister(ApiType, "c", C_LIGHTS)); - WRITE(p, "%sfloat4 "I_TEXMATRICES"[24] %s;\n", WriteLocation(ApiType), WriteRegister(ApiType, "c", C_TEXMATRICES)); // also using tex matrices - WRITE(p, "%sfloat4 "I_TRANSFORMMATRICES"[64] %s;\n", WriteLocation(ApiType), WriteRegister(ApiType, "c", C_TRANSFORMMATRICES)); - WRITE(p, "%sfloat4 "I_NORMALMATRICES"[32] %s;\n", WriteLocation(ApiType), WriteRegister(ApiType, "c", C_NORMALMATRICES)); - WRITE(p, "%sfloat4 "I_POSTTRANSFORMMATRICES"[64] %s;\n", WriteLocation(ApiType), WriteRegister(ApiType, "c", C_POSTTRANSFORMMATRICES)); - WRITE(p, "%sfloat4 "I_DEPTHPARAMS" %s;\n", WriteLocation(ApiType), WriteRegister(ApiType, "c", C_DEPTHPARAMS)); + WRITE(p, "%sfloat4 "I_POSNORMALMATRIX"[6] %s;\n", WriteLocation2(ApiType), WriteRegister(ApiType, "c", C_POSNORMALMATRIX)); + //if(ApiType == API_GLSL && g_ActiveConfig.backend_info.bSupportsGLSLUBO) + //WRITE(p, "};\n"); + WRITE(p, "%sfloat4 "I_PROJECTION"[4] %s;\n", WriteLocation2(ApiType), WriteRegister(ApiType, "c", C_PROJECTION)); + WRITE(p, "%sfloat4 "I_MATERIALS"[4] %s;\n", WriteLocation2(ApiType), WriteRegister(ApiType, "c", C_MATERIALS)); + WRITE(p, "%sfloat4 "I_LIGHTS"[40] %s;\n", WriteLocation2(ApiType), WriteRegister(ApiType, "c", C_LIGHTS)); + WRITE(p, "%sfloat4 "I_TEXMATRICES"[24] %s;\n", WriteLocation2(ApiType), WriteRegister(ApiType, "c", C_TEXMATRICES)); // also using tex matrices + WRITE(p, "%sfloat4 "I_TRANSFORMMATRICES"[64] %s;\n", WriteLocation2(ApiType), WriteRegister(ApiType, "c", C_TRANSFORMMATRICES)); + WRITE(p, "%sfloat4 "I_NORMALMATRICES"[32] %s;\n", WriteLocation2(ApiType), WriteRegister(ApiType, "c", C_NORMALMATRICES)); + WRITE(p, "%sfloat4 "I_POSTTRANSFORMMATRICES"[64] %s;\n", WriteLocation2(ApiType), WriteRegister(ApiType, "c", C_POSTTRANSFORMMATRICES)); + WRITE(p, "%sfloat4 "I_DEPTHPARAMS" %s;\n", WriteLocation2(ApiType), WriteRegister(ApiType, "c", C_DEPTHPARAMS)); + - if(ApiType == API_GLSL && g_ActiveConfig.backend_info.bSupportsGLSLUBO) - WRITE(p, "};\n"); p = GenerateVSOutputStruct(p, components, ApiType); diff --git a/Source/Core/VideoCommon/Src/VertexShaderGen.h b/Source/Core/VideoCommon/Src/VertexShaderGen.h index a3364c2982..c711aa7876 100644 --- a/Source/Core/VideoCommon/Src/VertexShaderGen.h +++ b/Source/Core/VideoCommon/Src/VertexShaderGen.h @@ -55,7 +55,7 @@ const s_svar VSVar_Loc[] = { {I_POSNORMALMATRIX, C_POSNORMALMATRIX, 6 }, {I_TRANSFORMMATRICES , C_TRANSFORMMATRICES, 64 }, {I_NORMALMATRICES , C_NORMALMATRICES, 32 }, {I_POSTTRANSFORMMATRICES, C_POSTTRANSFORMMATRICES, 64 }, - {I_DEPTHPARAMS, C_DEPTHPARAMS, 4 }, + {I_DEPTHPARAMS, C_DEPTHPARAMS, 1 }, }; template class _VERTEXSHADERUID diff --git a/Source/Core/VideoCommon/Src/VertexShaderManager.cpp b/Source/Core/VideoCommon/Src/VertexShaderManager.cpp index 5a12392ed2..939e751f55 100644 --- a/Source/Core/VideoCommon/Src/VertexShaderManager.cpp +++ b/Source/Core/VideoCommon/Src/VertexShaderManager.cpp @@ -173,7 +173,7 @@ void VertexShaderManager::Dirty() // TODO: A cleaner way to control the matricies without making a mess in the parameters field void VertexShaderManager::SetConstants() { - if (g_ActiveConfig.bUseGLSL && !g_ActiveConfig.backend_info.bSupportsGLSLUBO) + //if (g_ActiveConfig.bUseGLSL && !g_ActiveConfig.backend_info.bSupportsGLSLUBO) Dirty(); if (nTransformMatricesChanged[0] >= 0) { diff --git a/Source/Plugins/Plugin_VideoOGL/Src/PixelShaderCache.cpp b/Source/Plugins/Plugin_VideoOGL/Src/PixelShaderCache.cpp index 50382cc0dd..c957ccd894 100644 --- a/Source/Plugins/Plugin_VideoOGL/Src/PixelShaderCache.cpp +++ b/Source/Plugins/Plugin_VideoOGL/Src/PixelShaderCache.cpp @@ -496,14 +496,15 @@ void SetPSConstant4fvByName(const char * name, unsigned int offset, const float } } } + void SetGLSLPSConstant4f(unsigned int const_number, float f1, float f2, float f3, float f4) { float f[4] = { f1, f2, f3, f4 }; if (g_ActiveConfig.backend_info.bSupportsGLSLUBO) { - ProgramShaderCache::SetUniformObjects(0, const_number, f); - //return; + ProgramShaderCache::SetUniformObjects(0, const_number, f); + //return; } for (unsigned int a = 0; a < 10; ++a) { @@ -520,8 +521,8 @@ void SetGLSLPSConstant4fv(unsigned int const_number, const float *f) { if (g_ActiveConfig.backend_info.bSupportsGLSLUBO) { - ProgramShaderCache::SetUniformObjects(0, const_number, f); - //return; + ProgramShaderCache::SetUniformObjects(0, const_number, f); + //return; } for (unsigned int a = 0; a < 10; ++a) { @@ -538,8 +539,8 @@ void SetMultiGLSLPSConstant4fv(unsigned int const_number, unsigned int count, co { if (g_ActiveConfig.backend_info.bSupportsGLSLUBO) { - ProgramShaderCache::SetUniformObjects(0, const_number, f, count); - //return; + ProgramShaderCache::SetUniformObjects(0, const_number, f, count); + //return; } for (unsigned int a = 0; a < 10; ++a) { diff --git a/Source/Plugins/Plugin_VideoOGL/Src/ProgramShaderCache.cpp b/Source/Plugins/Plugin_VideoOGL/Src/ProgramShaderCache.cpp index e174cf2641..4b1f8b28d2 100644 --- a/Source/Plugins/Plugin_VideoOGL/Src/ProgramShaderCache.cpp +++ b/Source/Plugins/Plugin_VideoOGL/Src/ProgramShaderCache.cpp @@ -17,12 +17,15 @@ #include "ProgramShaderCache.h" #include -static GLenum checkForGLError(const char * situation) +GLuint GLERR(const char *function) { - GLenum error = glGetError(); - if (error != GL_NO_ERROR) - printf("Error: %d -- %s\n", error, situation); - return error; + GLint err = glGetError(); + if (err != GL_NO_ERROR) + { + printf( "(%s) OpenGL error 0x%x - %s\n", + function, err, gluErrorString(err)); + } + return err; } namespace OGL { @@ -98,10 +101,19 @@ namespace OGL glAttachShader(entry.program.glprogid, entry.program.psid); glLinkProgram(entry.program.glprogid); + GLsizei length = 0; + + glGetProgramiv(entry.program.glprogid, GL_INFO_LOG_LENGTH, &length); + if (length > 0) + { + GLsizei charsWritten; + GLchar* infoLog = new GLchar[length]; + glGetProgramInfoLog(entry.program.glprogid, length, &charsWritten, infoLog); + printf("Program info log:\n%s", infoLog); + delete[] infoLog; + } glUseProgram(entry.program.glprogid); - checkForGLError("User"); - // We cache our uniform locations for now // Once we move up to a newer version of GLSL, ~1.30 @@ -136,12 +148,22 @@ namespace OGL } void ProgramShaderCache::SetUniformObjects(int Buffer, unsigned int offset, const float *f, unsigned int count) { + GLERR(""); assert(Buffer > 1); - glBindBuffer(GL_UNIFORM_BUFFER, UBOBuffers[Buffer]); + static int _Buffer = -1; + if(_Buffer != Buffer) + { + _Buffer = Buffer; + GLERR("bind"); + glBindBuffer(GL_UNIFORM_BUFFER, UBOBuffers[_Buffer]); + } + // Query for the offsets of each block variable + // glBufferSubData expects data in bytes, so multiply count by four // Expects the offset in bytes as well, so multiply by *4 *4 since we are passing in a vec4 location glBufferSubData(GL_UNIFORM_BUFFER, offset * 4 * 4, count * 4 * 4, f); - glBindBuffer(GL_UNIFORM_BUFFER, 0); + GLERR("sub"); + } GLuint ProgramShaderCache::GetCurrentProgram(void) { return CurrentProgram; } @@ -156,20 +178,22 @@ namespace OGL void ProgramShaderCache::Init(void) { glGenBuffers(2, UBOBuffers); + + glBindBuffer(GL_UNIFORM_BUFFER, UBOBuffers[0]); // We multiply by *4*4 because we need to get down to basic machine units. // So multiply by four to get how many floats we have from vec4s // Then once more to get bytes - glBufferData(GL_UNIFORM_BUFFER, 1024 *1024, NULL, GL_DYNAMIC_DRAW); + glBufferData(GL_UNIFORM_BUFFER, C_PENVCONST_END * 4 * 4, NULL, GL_DYNAMIC_DRAW); // Now bind the buffer to the index point // We know PS is 0 since we have it statically set in the shader - glBindBufferBase(GL_UNIFORM_BUFFER, 4, UBOBuffers[0]); + glBindBufferBase(GL_UNIFORM_BUFFER, 0, UBOBuffers[0]); + // Repeat for VS shader - glBindBuffer(GL_UNIFORM_BUFFER, UBOBuffers[1]); - glBufferData(GL_UNIFORM_BUFFER, 1024*1024, NULL, GL_DYNAMIC_DRAW); - glBindBufferBase(GL_UNIFORM_BUFFER, 5, UBOBuffers[1]); - glBindBuffer(GL_UNIFORM_BUFFER, 0); - checkForGLError("Init"); + //glBindBuffer(GL_UNIFORM_BUFFER, UBOBuffers[1]); + //glBufferData(GL_UNIFORM_BUFFER, C_PENVCONST_END * 4 * 4, NULL, GL_DYNAMIC_DRAW); + //glBindBufferBase(GL_UNIFORM_BUFFER, 2, UBOBuffers[1]); + GLERR("init"); } void ProgramShaderCache::Shutdown(void) { diff --git a/Source/Plugins/Plugin_VideoOGL/Src/TextureConverter.cpp b/Source/Plugins/Plugin_VideoOGL/Src/TextureConverter.cpp index e1439d621c..6e2c46c95c 100644 --- a/Source/Plugins/Plugin_VideoOGL/Src/TextureConverter.cpp +++ b/Source/Plugins/Plugin_VideoOGL/Src/TextureConverter.cpp @@ -63,26 +63,49 @@ void CreateRgbToYuyvProgram() // Output is BGRA because that is slightly faster than RGBA. if(g_ActiveConfig.bUseGLSL) { - const char *FProgram = - "#version 120\n" - "#ifdef GL_ARB_texture_rectangle\n" - "#extension GL_ARB_texture_rectangle : require\n" - "#endif\n" - "uniform sampler2DRect samp0;\n" - "void main()\n" - "{\n" - " vec2 uv1 = vec2(gl_TexCoord[0].x + 1.0f, gl_TexCoord[0].y);\n" - " vec3 c0 = texture2DRect(samp0, gl_TexCoord[0].xy).rgb;\n" - " vec3 c1 = texture2DRect(samp0, uv1).rgb;\n" - " vec3 y_const = vec3(0.257f,0.504f,0.098f);\n" - " vec3 u_const = vec3(-0.148f,-0.291f,0.439f);\n" - " vec3 v_const = vec3(0.439f,-0.368f,-0.071f);\n" - " vec4 const3 = vec4(0.0625f,0.5f,0.0625f,0.5f);\n" - " vec3 c01 = (c0 + c1) * 0.5f;\n" - " gl_FragData[0] = vec4(dot(c1,y_const),dot(c01,u_const),dot(c0,y_const),dot(c01, v_const)) + const3;\n" - "}\n"; - if (!PixelShaderCache::CompilePixelShader(s_rgbToYuyvProgram, FProgram)) - ERROR_LOG(VIDEO, "Failed to create RGB to YUYV fragment program."); + if(g_ActiveConfig.backend_info.bSupportsGLSLBinding) + { + const char *FProgram = + "#version 330 compatibility\n" + "#extension GL_ARB_texture_rectangle : enable\n" + "#extension GL_ARB_shading_language_420pack : enable\n" + "layout(binding = 0) uniform sampler2DRect samp0;\n" + "void main()\n" + "{\n" + " vec2 uv1 = vec2(gl_TexCoord[0].x + 1.0f, gl_TexCoord[0].y);\n" + " vec3 c0 = texture2DRect(samp0, gl_TexCoord[0].xy).rgb;\n" + " vec3 c1 = texture2DRect(samp0, uv1).rgb;\n" + " vec3 y_const = vec3(0.257f,0.504f,0.098f);\n" + " vec3 u_const = vec3(-0.148f,-0.291f,0.439f);\n" + " vec3 v_const = vec3(0.439f,-0.368f,-0.071f);\n" + " vec4 const3 = vec4(0.0625f,0.5f,0.0625f,0.5f);\n" + " vec3 c01 = (c0 + c1) * 0.5f;\n" + " gl_FragData[0] = vec4(dot(c1,y_const),dot(c01,u_const),dot(c0,y_const),dot(c01, v_const)) + const3;\n" + "}\n"; + if (!PixelShaderCache::CompilePixelShader(s_rgbToYuyvProgram, FProgram)) + ERROR_LOG(VIDEO, "Failed to create RGB to YUYV fragment program."); + } + else + { + const char *FProgram = + "#version 120\n" + "#extension GL_ARB_texture_rectangle : enable\n" + "uniform sampler2DRect samp0;\n" + "void main()\n" + "{\n" + " vec2 uv1 = vec2(gl_TexCoord[0].x + 1.0f, gl_TexCoord[0].y);\n" + " vec3 c0 = texture2DRect(samp0, gl_TexCoord[0].xy).rgb;\n" + " vec3 c1 = texture2DRect(samp0, uv1).rgb;\n" + " vec3 y_const = vec3(0.257f,0.504f,0.098f);\n" + " vec3 u_const = vec3(-0.148f,-0.291f,0.439f);\n" + " vec3 v_const = vec3(0.439f,-0.368f,-0.071f);\n" + " vec4 const3 = vec4(0.0625f,0.5f,0.0625f,0.5f);\n" + " vec3 c01 = (c0 + c1) * 0.5f;\n" + " gl_FragData[0] = vec4(dot(c1,y_const),dot(c01,u_const),dot(c0,y_const),dot(c01, v_const)) + const3;\n" + "}\n"; + if (!PixelShaderCache::CompilePixelShader(s_rgbToYuyvProgram, FProgram)) + ERROR_LOG(VIDEO, "Failed to create RGB to YUYV fragment program."); + } } else { @@ -111,29 +134,57 @@ void CreateYuyvToRgbProgram() { if(g_ActiveConfig.bUseGLSL) { - const char *FProgram = - "#version 120\n" - "#ifdef GL_ARB_texture_rectangle\n" - "#extension GL_ARB_texture_rectangle : require\n" - "#endif\n" - "uniform sampler2DRect samp0;\n" - "void main()\n" - "{\n" - " vec4 c0 = texture2DRect(samp0, gl_TexCoord[0].xy).rgba;\n" + if(g_ActiveConfig.backend_info.bSupportsGLSLBinding) + { + const char *FProgram = + "#version 330 compatibility\n" + "#extension GL_ARB_texture_rectangle : enable\n" + "#extension GL_ARB_shading_language_420pack : enable\n" + "layout(binding = 0) uniform sampler2DRect samp0;\n" + "void main()\n" + "{\n" + " vec4 c0 = texture2DRect(samp0, gl_TexCoord[0].xy).rgba;\n" - " float f = step(0.5, fract(gl_TexCoord[0].x));\n" - " float y = mix(c0.b, c0.r, f);\n" - " float yComp = 1.164f * (y - 0.0625f);\n" - " float uComp = c0.g - 0.5f;\n" - " float vComp = c0.a - 0.5f;\n" + " float f = step(0.5, fract(gl_TexCoord[0].x));\n" + " float y = mix(c0.b, c0.r, f);\n" + " float yComp = 1.164f * (y - 0.0625f);\n" + " float uComp = c0.g - 0.5f;\n" + " float vComp = c0.a - 0.5f;\n" - " gl_FragData[0] = vec4(yComp + (1.596f * vComp),\n" - " yComp - (0.813f * vComp) - (0.391f * uComp),\n" - " yComp + (2.018f * uComp),\n" - " 1.0f);\n" - "}\n"; - if (!PixelShaderCache::CompilePixelShader(s_yuyvToRgbProgram, FProgram)) - ERROR_LOG(VIDEO, "Failed to create YUYV to RGB fragment program."); + " gl_FragData[0] = vec4(yComp + (1.596f * vComp),\n" + " yComp - (0.813f * vComp) - (0.391f * uComp),\n" + " yComp + (2.018f * uComp),\n" + " 1.0f);\n" + "}\n"; + if (!PixelShaderCache::CompilePixelShader(s_yuyvToRgbProgram, FProgram)) + ERROR_LOG(VIDEO, "Failed to create YUYV to RGB fragment program."); + } + else + { + const char *FProgram = + "#version 120\n" + "#ifdef GL_ARB_texture_rectangle\n" + "#extension GL_ARB_texture_rectangle : require\n" + "#endif\n" + "uniform sampler2DRect samp0;\n" + "void main()\n" + "{\n" + " vec4 c0 = texture2DRect(samp0, gl_TexCoord[0].xy).rgba;\n" + + " float f = step(0.5, fract(gl_TexCoord[0].x));\n" + " float y = mix(c0.b, c0.r, f);\n" + " float yComp = 1.164f * (y - 0.0625f);\n" + " float uComp = c0.g - 0.5f;\n" + " float vComp = c0.a - 0.5f;\n" + + " gl_FragData[0] = vec4(yComp + (1.596f * vComp),\n" + " yComp - (0.813f * vComp) - (0.391f * uComp),\n" + " yComp + (2.018f * uComp),\n" + " 1.0f);\n" + "}\n"; + if (!PixelShaderCache::CompilePixelShader(s_yuyvToRgbProgram, FProgram)) + ERROR_LOG(VIDEO, "Failed to create YUYV to RGB fragment program."); + } } else { diff --git a/Source/Plugins/Plugin_VideoOGL/Src/VertexShaderCache.cpp b/Source/Plugins/Plugin_VideoOGL/Src/VertexShaderCache.cpp index e22a2f3be2..0550e99b18 100644 --- a/Source/Plugins/Plugin_VideoOGL/Src/VertexShaderCache.cpp +++ b/Source/Plugins/Plugin_VideoOGL/Src/VertexShaderCache.cpp @@ -240,6 +240,7 @@ void SetVSConstant4fvByName(const char * name, unsigned int offset, const float } } } +#define MAX_UNIFORM 0 void SetGLSLVSConstant4f(unsigned int const_number, float f1, float f2, float f3, float f4) { float buf[4]; @@ -249,7 +250,8 @@ void SetGLSLVSConstant4f(unsigned int const_number, float f1, float f2, float f3 buf[3] = f4; if (g_ActiveConfig.backend_info.bSupportsGLSLUBO) { - ProgramShaderCache::SetUniformObjects(1, const_number, buf); + if(const_number < MAX_UNIFORM) + ProgramShaderCache::SetUniformObjects(1, const_number, buf); //return; } for( unsigned int a = 0; a < 9; ++a) @@ -267,6 +269,7 @@ void SetGLSLVSConstant4fv(unsigned int const_number, const float *f) { if (g_ActiveConfig.backend_info.bSupportsGLSLUBO) { + if(const_number < MAX_UNIFORM) ProgramShaderCache::SetUniformObjects(1, const_number, f); //return; } @@ -285,6 +288,7 @@ void SetMultiGLSLVSConstant4fv(unsigned int const_number, unsigned int count, co { if (g_ActiveConfig.backend_info.bSupportsGLSLUBO) { + if(const_number < MAX_UNIFORM) ProgramShaderCache::SetUniformObjects(1, const_number, f, count); //return; } @@ -311,6 +315,7 @@ void SetMultiGLSLVSConstant3fv(unsigned int const_number, unsigned int count, co } if (g_ActiveConfig.backend_info.bSupportsGLSLUBO) { + if(const_number < MAX_UNIFORM) ProgramShaderCache::SetUniformObjects(1, const_number, buf, count); //return; } From 5925feb6e05219be8abb43576fa7e0b639c049f7 Mon Sep 17 00:00:00 2001 From: Ryan Houdek Date: Sat, 10 Dec 2011 08:07:13 -0600 Subject: [PATCH 29/83] yay, UBOs work 100% now. --- .../Core/VideoCommon/Src/PixelShaderGen.cpp | 4 +- .../Core/VideoCommon/Src/VertexShaderGen.cpp | 33 +++++------- .../VideoCommon/Src/VertexShaderManager.cpp | 2 +- .../Plugin_VideoOGL/Src/PixelShaderCache.cpp | 35 +++++++------ .../Src/ProgramShaderCache.cpp | 40 ++++----------- .../Plugin_VideoOGL/Src/VertexShaderCache.cpp | 51 ++++++++----------- 6 files changed, 66 insertions(+), 99 deletions(-) diff --git a/Source/Core/VideoCommon/Src/PixelShaderGen.cpp b/Source/Core/VideoCommon/Src/PixelShaderGen.cpp index b963912443..977de656d6 100644 --- a/Source/Core/VideoCommon/Src/PixelShaderGen.cpp +++ b/Source/Core/VideoCommon/Src/PixelShaderGen.cpp @@ -518,7 +518,7 @@ const char* WriteBinding(API_TYPE ApiType, const u32 num) } const char *WriteLocation(API_TYPE ApiType) { - if(ApiType == API_GLSL && g_ActiveConfig.backend_info.bSupportsGLSLUBO) + if(ApiType == API_GLSL && !g_ActiveConfig.backend_info.bSupportsGLSLUBO) return ""; static char result[64]; sprintf(result, "uniform "); @@ -611,7 +611,7 @@ const char *GeneratePixelShaderCode(DSTALPHA_MODE dstAlphaMode, API_TYPE ApiType WRITE(p, "\n"); if(ApiType == API_GLSL && g_ActiveConfig.backend_info.bSupportsGLSLUBO) - WRITE(p, "layout(std140, binding = 0) uniform PSBlock {\n"); + WRITE(p, "layout(std140, binding = 1) uniform PSBlock {\n"); WRITE(p, "%sfloat4 "I_COLORS"[4] %s;\n", WriteLocation(ApiType), WriteRegister(ApiType, "c", C_COLORS)); WRITE(p, "%sfloat4 "I_KCOLORS"[4] %s;\n", WriteLocation(ApiType), WriteRegister(ApiType, "c", C_KCOLORS)); diff --git a/Source/Core/VideoCommon/Src/VertexShaderGen.cpp b/Source/Core/VideoCommon/Src/VertexShaderGen.cpp index c693a7b06b..8b945af530 100644 --- a/Source/Core/VideoCommon/Src/VertexShaderGen.cpp +++ b/Source/Core/VideoCommon/Src/VertexShaderGen.cpp @@ -166,12 +166,6 @@ char* GenerateVSOutputStruct(char* p, u32 components, API_TYPE ApiType) extern const char* WriteRegister(API_TYPE ApiType, const char *prefix, const u32 num); extern const char* WriteBinding(API_TYPE ApiType, const u32 num); const char *WriteLocation(API_TYPE ApiType); -const char *WriteLocation2(API_TYPE ApiType) -{ - static char result[64]; - sprintf(result, "uniform "); - return result; -} const char *GenerateVertexShaderCode(u32 components, API_TYPE ApiType) { @@ -222,20 +216,21 @@ const char *GenerateVertexShaderCode(u32 components, API_TYPE ApiType) } // uniforms - //if(ApiType == API_GLSL && g_ActiveConfig.backend_info.bSupportsGLSLUBO) - //WRITE(p, "layout(std140, binding = 2) uniform VSBlock {\n"); + if(ApiType == API_GLSL && g_ActiveConfig.backend_info.bSupportsGLSLUBO) + WRITE(p, "layout(std140, binding = 2) uniform VSBlock {\n"); - WRITE(p, "%sfloat4 "I_POSNORMALMATRIX"[6] %s;\n", WriteLocation2(ApiType), WriteRegister(ApiType, "c", C_POSNORMALMATRIX)); - //if(ApiType == API_GLSL && g_ActiveConfig.backend_info.bSupportsGLSLUBO) - //WRITE(p, "};\n"); - WRITE(p, "%sfloat4 "I_PROJECTION"[4] %s;\n", WriteLocation2(ApiType), WriteRegister(ApiType, "c", C_PROJECTION)); - WRITE(p, "%sfloat4 "I_MATERIALS"[4] %s;\n", WriteLocation2(ApiType), WriteRegister(ApiType, "c", C_MATERIALS)); - WRITE(p, "%sfloat4 "I_LIGHTS"[40] %s;\n", WriteLocation2(ApiType), WriteRegister(ApiType, "c", C_LIGHTS)); - WRITE(p, "%sfloat4 "I_TEXMATRICES"[24] %s;\n", WriteLocation2(ApiType), WriteRegister(ApiType, "c", C_TEXMATRICES)); // also using tex matrices - WRITE(p, "%sfloat4 "I_TRANSFORMMATRICES"[64] %s;\n", WriteLocation2(ApiType), WriteRegister(ApiType, "c", C_TRANSFORMMATRICES)); - WRITE(p, "%sfloat4 "I_NORMALMATRICES"[32] %s;\n", WriteLocation2(ApiType), WriteRegister(ApiType, "c", C_NORMALMATRICES)); - WRITE(p, "%sfloat4 "I_POSTTRANSFORMMATRICES"[64] %s;\n", WriteLocation2(ApiType), WriteRegister(ApiType, "c", C_POSTTRANSFORMMATRICES)); - WRITE(p, "%sfloat4 "I_DEPTHPARAMS" %s;\n", WriteLocation2(ApiType), WriteRegister(ApiType, "c", C_DEPTHPARAMS)); + WRITE(p, "%sfloat4 "I_POSNORMALMATRIX"[6] %s;\n", WriteLocation(ApiType), WriteRegister(ApiType, "c", C_POSNORMALMATRIX)); + WRITE(p, "%sfloat4 "I_PROJECTION"[4] %s;\n", WriteLocation(ApiType), WriteRegister(ApiType, "c", C_PROJECTION)); + WRITE(p, "%sfloat4 "I_MATERIALS"[4] %s;\n", WriteLocation(ApiType), WriteRegister(ApiType, "c", C_MATERIALS)); + WRITE(p, "%sfloat4 "I_LIGHTS"[40] %s;\n", WriteLocation(ApiType), WriteRegister(ApiType, "c", C_LIGHTS)); + WRITE(p, "%sfloat4 "I_TEXMATRICES"[24] %s;\n", WriteLocation(ApiType), WriteRegister(ApiType, "c", C_TEXMATRICES)); // also using tex matrices + WRITE(p, "%sfloat4 "I_TRANSFORMMATRICES"[64] %s;\n", WriteLocation(ApiType),WriteRegister(ApiType, "c", C_TRANSFORMMATRICES)); + WRITE(p, "%sfloat4 "I_NORMALMATRICES"[32] %s;\n", WriteLocation(ApiType), WriteRegister(ApiType, "c", C_NORMALMATRICES)); + WRITE(p, "%sfloat4 "I_POSTTRANSFORMMATRICES"[64] %s;\n", WriteLocation(ApiType), WriteRegister(ApiType, "c", C_POSTTRANSFORMMATRICES)); + WRITE(p, "%sfloat4 "I_DEPTHPARAMS" %s;\n", WriteLocation(ApiType), WriteRegister(ApiType, "c", C_DEPTHPARAMS)); + + if(ApiType == API_GLSL && g_ActiveConfig.backend_info.bSupportsGLSLUBO) + WRITE(p, "};\n"); diff --git a/Source/Core/VideoCommon/Src/VertexShaderManager.cpp b/Source/Core/VideoCommon/Src/VertexShaderManager.cpp index 939e751f55..5a12392ed2 100644 --- a/Source/Core/VideoCommon/Src/VertexShaderManager.cpp +++ b/Source/Core/VideoCommon/Src/VertexShaderManager.cpp @@ -173,7 +173,7 @@ void VertexShaderManager::Dirty() // TODO: A cleaner way to control the matricies without making a mess in the parameters field void VertexShaderManager::SetConstants() { - //if (g_ActiveConfig.bUseGLSL && !g_ActiveConfig.backend_info.bSupportsGLSLUBO) + if (g_ActiveConfig.bUseGLSL && !g_ActiveConfig.backend_info.bSupportsGLSLUBO) Dirty(); if (nTransformMatricesChanged[0] >= 0) { diff --git a/Source/Plugins/Plugin_VideoOGL/Src/PixelShaderCache.cpp b/Source/Plugins/Plugin_VideoOGL/Src/PixelShaderCache.cpp index c957ccd894..4172237bb2 100644 --- a/Source/Plugins/Plugin_VideoOGL/Src/PixelShaderCache.cpp +++ b/Source/Plugins/Plugin_VideoOGL/Src/PixelShaderCache.cpp @@ -429,24 +429,6 @@ bool CompileGLSLPixelShader(FRAGMENTSHADER& ps, const char* pstrprogram) glShaderSource(result, 1, &pstrprogram, NULL); glCompileShader(result); - GLsizei length = 0; - - glGetShaderiv(result, GL_INFO_LOG_LENGTH, &length); - if (length > 0) - { - GLsizei charsWritten; - GLchar* infoLog = new GLchar[length]; - glGetShaderInfoLog(result, length, &charsWritten, infoLog); - WARN_LOG(VIDEO, "PS Shader info log:\n%s", infoLog); - char szTemp[MAX_PATH]; - sprintf(szTemp, "ps_%d.txt", result); - FILE *fp = fopen(szTemp, "wb"); - fwrite(pstrprogram, strlen(pstrprogram), 1, fp); - fclose(fp); - if(strstr(infoLog, "warning") != NULL || strstr(infoLog, "error") != NULL) - exit(0); - delete[] infoLog; - } GLint compileStatus; glGetShaderiv(result, GL_COMPILE_STATUS, &compileStatus); @@ -454,6 +436,23 @@ bool CompileGLSLPixelShader(FRAGMENTSHADER& ps, const char* pstrprogram) { // Compile failed ERROR_LOG(VIDEO, "Shader compilation failed; see info log"); + + GLsizei length = 0; + glGetShaderiv(result, GL_INFO_LOG_LENGTH, &length); + if (length > 0) + { + GLsizei charsWritten; + GLchar* infoLog = new GLchar[length]; + glGetShaderInfoLog(result, length, &charsWritten, infoLog); + WARN_LOG(VIDEO, "VS Shader info log:\n%s", infoLog); + char szTemp[MAX_PATH]; + sprintf(szTemp, "vs_%d.txt", result); + FILE *fp = fopen(szTemp, "wb"); + fwrite(pstrprogram, strlen(pstrprogram), 1, fp); + fclose(fp); + + delete[] infoLog; + } // Don't try to use this shader glDeleteShader(result); return false; diff --git a/Source/Plugins/Plugin_VideoOGL/Src/ProgramShaderCache.cpp b/Source/Plugins/Plugin_VideoOGL/Src/ProgramShaderCache.cpp index 4b1f8b28d2..073e8b430c 100644 --- a/Source/Plugins/Plugin_VideoOGL/Src/ProgramShaderCache.cpp +++ b/Source/Plugins/Plugin_VideoOGL/Src/ProgramShaderCache.cpp @@ -17,16 +17,6 @@ #include "ProgramShaderCache.h" #include -GLuint GLERR(const char *function) -{ - GLint err = glGetError(); - if (err != GL_NO_ERROR) - { - printf( "(%s) OpenGL error 0x%x - %s\n", - function, err, gluErrorString(err)); - } - return err; -} namespace OGL { GLuint ProgramShaderCache::CurrentFShader = 0, ProgramShaderCache::CurrentVShader = 0, ProgramShaderCache::CurrentProgram = 0; @@ -101,19 +91,14 @@ namespace OGL glAttachShader(entry.program.glprogid, entry.program.psid); glLinkProgram(entry.program.glprogid); - GLsizei length = 0; - - glGetProgramiv(entry.program.glprogid, GL_INFO_LOG_LENGTH, &length); - if (length > 0) - { - GLsizei charsWritten; - GLchar* infoLog = new GLchar[length]; - glGetProgramInfoLog(entry.program.glprogid, length, &charsWritten, infoLog); - printf("Program info log:\n%s", infoLog); - delete[] infoLog; - } glUseProgram(entry.program.glprogid); + + // Dunno why this is needed when I have the binding + // points statically set in the shader source + // Driver Bug? Nvidia GTX 570, 290.xx Driver, Linux x64 + glUniformBlockBinding( entry.program.glprogid, 0, 1 ); + glUniformBlockBinding( entry.program.glprogid, 1, 2 ); // We cache our uniform locations for now // Once we move up to a newer version of GLSL, ~1.30 @@ -148,13 +133,11 @@ namespace OGL } void ProgramShaderCache::SetUniformObjects(int Buffer, unsigned int offset, const float *f, unsigned int count) { - GLERR(""); assert(Buffer > 1); static int _Buffer = -1; if(_Buffer != Buffer) { _Buffer = Buffer; - GLERR("bind"); glBindBuffer(GL_UNIFORM_BUFFER, UBOBuffers[_Buffer]); } // Query for the offsets of each block variable @@ -162,7 +145,6 @@ namespace OGL // glBufferSubData expects data in bytes, so multiply count by four // Expects the offset in bytes as well, so multiply by *4 *4 since we are passing in a vec4 location glBufferSubData(GL_UNIFORM_BUFFER, offset * 4 * 4, count * 4 * 4, f); - GLERR("sub"); } GLuint ProgramShaderCache::GetCurrentProgram(void) { return CurrentProgram; } @@ -179,7 +161,6 @@ namespace OGL { glGenBuffers(2, UBOBuffers); - glBindBuffer(GL_UNIFORM_BUFFER, UBOBuffers[0]); // We multiply by *4*4 because we need to get down to basic machine units. // So multiply by four to get how many floats we have from vec4s @@ -187,13 +168,12 @@ namespace OGL glBufferData(GL_UNIFORM_BUFFER, C_PENVCONST_END * 4 * 4, NULL, GL_DYNAMIC_DRAW); // Now bind the buffer to the index point // We know PS is 0 since we have it statically set in the shader - glBindBufferBase(GL_UNIFORM_BUFFER, 0, UBOBuffers[0]); + glBindBufferBase(GL_UNIFORM_BUFFER, 1, UBOBuffers[0]); // Repeat for VS shader - //glBindBuffer(GL_UNIFORM_BUFFER, UBOBuffers[1]); - //glBufferData(GL_UNIFORM_BUFFER, C_PENVCONST_END * 4 * 4, NULL, GL_DYNAMIC_DRAW); - //glBindBufferBase(GL_UNIFORM_BUFFER, 2, UBOBuffers[1]); - GLERR("init"); + glBindBuffer(GL_UNIFORM_BUFFER, UBOBuffers[1]); + glBufferData(GL_UNIFORM_BUFFER, 1024*1024, NULL, GL_DYNAMIC_DRAW); + glBindBufferBase(GL_UNIFORM_BUFFER, 2, UBOBuffers[1]); } void ProgramShaderCache::Shutdown(void) { diff --git a/Source/Plugins/Plugin_VideoOGL/Src/VertexShaderCache.cpp b/Source/Plugins/Plugin_VideoOGL/Src/VertexShaderCache.cpp index 0550e99b18..97dfc9a3bb 100644 --- a/Source/Plugins/Plugin_VideoOGL/Src/VertexShaderCache.cpp +++ b/Source/Plugins/Plugin_VideoOGL/Src/VertexShaderCache.cpp @@ -189,25 +189,6 @@ bool CompileGLSLVertexShader(VERTEXSHADER& vs, const char* pstrprogram) glShaderSource(result, 1, &pstrprogram, NULL); glCompileShader(result); - GLsizei length = 0; - - glGetShaderiv(result, GL_INFO_LOG_LENGTH, &length); - if (length > 0) - { - GLsizei charsWritten; - GLchar* infoLog = new GLchar[length]; - glGetShaderInfoLog(result, length, &charsWritten, infoLog); - WARN_LOG(VIDEO, "VS Shader info log:\n%s", infoLog); - char szTemp[MAX_PATH]; - sprintf(szTemp, "vs_%d.txt", result); - FILE *fp = fopen(szTemp, "wb"); - fwrite(pstrprogram, strlen(pstrprogram), 1, fp); - fclose(fp); - - if(strstr(infoLog, "warning") != NULL || strstr(infoLog, "error") != NULL) - exit(0); - delete[] infoLog; - } GLint compileStatus; glGetShaderiv(result, GL_COMPILE_STATUS, &compileStatus); @@ -215,6 +196,23 @@ bool CompileGLSLVertexShader(VERTEXSHADER& vs, const char* pstrprogram) { // Compile failed ERROR_LOG(VIDEO, "Shader compilation failed; see info log"); + + GLsizei length = 0; + glGetShaderiv(result, GL_INFO_LOG_LENGTH, &length); + if (length > 0) + { + GLsizei charsWritten; + GLchar* infoLog = new GLchar[length]; + glGetShaderInfoLog(result, length, &charsWritten, infoLog); + WARN_LOG(VIDEO, "VS Shader info log:\n%s", infoLog); + char szTemp[MAX_PATH]; + sprintf(szTemp, "vs_%d.txt", result); + FILE *fp = fopen(szTemp, "wb"); + fwrite(pstrprogram, strlen(pstrprogram), 1, fp); + fclose(fp); + + delete[] infoLog; + } // Don't try to use this shader glDeleteShader(result); return false; @@ -240,7 +238,6 @@ void SetVSConstant4fvByName(const char * name, unsigned int offset, const float } } } -#define MAX_UNIFORM 0 void SetGLSLVSConstant4f(unsigned int const_number, float f1, float f2, float f3, float f4) { float buf[4]; @@ -250,9 +247,8 @@ void SetGLSLVSConstant4f(unsigned int const_number, float f1, float f2, float f3 buf[3] = f4; if (g_ActiveConfig.backend_info.bSupportsGLSLUBO) { - if(const_number < MAX_UNIFORM) - ProgramShaderCache::SetUniformObjects(1, const_number, buf); - //return; + ProgramShaderCache::SetUniformObjects(1, const_number, buf); + return; } for( unsigned int a = 0; a < 9; ++a) { @@ -269,9 +265,8 @@ void SetGLSLVSConstant4fv(unsigned int const_number, const float *f) { if (g_ActiveConfig.backend_info.bSupportsGLSLUBO) { - if(const_number < MAX_UNIFORM) ProgramShaderCache::SetUniformObjects(1, const_number, f); - //return; + return; } for( unsigned int a = 0; a < 9; ++a) { @@ -288,9 +283,8 @@ void SetMultiGLSLVSConstant4fv(unsigned int const_number, unsigned int count, co { if (g_ActiveConfig.backend_info.bSupportsGLSLUBO) { - if(const_number < MAX_UNIFORM) ProgramShaderCache::SetUniformObjects(1, const_number, f, count); - //return; + return; } for( unsigned int a = 0; a < 9; ++a) { @@ -315,9 +309,8 @@ void SetMultiGLSLVSConstant3fv(unsigned int const_number, unsigned int count, co } if (g_ActiveConfig.backend_info.bSupportsGLSLUBO) { - if(const_number < MAX_UNIFORM) ProgramShaderCache::SetUniformObjects(1, const_number, buf, count); - //return; + return; } for( unsigned int a = 0; a < 9; ++a) { From c72a244809ee9e8a645bd1f200138af727d0474b Mon Sep 17 00:00:00 2001 From: Ryan Houdek Date: Sat, 10 Dec 2011 14:35:37 -0600 Subject: [PATCH 30/83] Make sure to support everything even if GPU doesn't. --- Source/Core/VideoCommon/Src/PixelShaderGen.cpp | 5 +++-- Source/Core/VideoCommon/Src/VertexShaderGen.cpp | 2 +- Source/Plugins/Plugin_VideoOGL/Src/ProgramShaderCache.cpp | 8 ++++++-- 3 files changed, 10 insertions(+), 5 deletions(-) diff --git a/Source/Core/VideoCommon/Src/PixelShaderGen.cpp b/Source/Core/VideoCommon/Src/PixelShaderGen.cpp index 977de656d6..940a34f537 100644 --- a/Source/Core/VideoCommon/Src/PixelShaderGen.cpp +++ b/Source/Core/VideoCommon/Src/PixelShaderGen.cpp @@ -518,7 +518,7 @@ const char* WriteBinding(API_TYPE ApiType, const u32 num) } const char *WriteLocation(API_TYPE ApiType) { - if(ApiType == API_GLSL && !g_ActiveConfig.backend_info.bSupportsGLSLUBO) + if(ApiType == API_GLSL && g_ActiveConfig.backend_info.bSupportsGLSLUBO) return ""; static char result[64]; sprintf(result, "uniform "); @@ -572,6 +572,7 @@ const char *GeneratePixelShaderCode(DSTALPHA_MODE dstAlphaMode, API_TYPE ApiType WRITE(p, "#define frac(x) fract(x)\n"); WRITE(p, "#define saturate(x) clamp(x, 0.0f, 1.0f)\n"); WRITE(p, "#define lerp(x, y, z) mix(x, y, z)\n"); + for (int i = 0; i < 8; ++i) WRITE(p, "%suniform sampler2D samp%d;\n", WriteBinding(ApiType, i), i); @@ -611,7 +612,7 @@ const char *GeneratePixelShaderCode(DSTALPHA_MODE dstAlphaMode, API_TYPE ApiType WRITE(p, "\n"); if(ApiType == API_GLSL && g_ActiveConfig.backend_info.bSupportsGLSLUBO) - WRITE(p, "layout(std140, binding = 1) uniform PSBlock {\n"); + WRITE(p, "layout(std140%s) uniform PSBlock {\n", g_ActiveConfig.backend_info.bSupportsGLSLBinding ? ", binding = 1" : ""); WRITE(p, "%sfloat4 "I_COLORS"[4] %s;\n", WriteLocation(ApiType), WriteRegister(ApiType, "c", C_COLORS)); WRITE(p, "%sfloat4 "I_KCOLORS"[4] %s;\n", WriteLocation(ApiType), WriteRegister(ApiType, "c", C_KCOLORS)); diff --git a/Source/Core/VideoCommon/Src/VertexShaderGen.cpp b/Source/Core/VideoCommon/Src/VertexShaderGen.cpp index 8b945af530..67700dd647 100644 --- a/Source/Core/VideoCommon/Src/VertexShaderGen.cpp +++ b/Source/Core/VideoCommon/Src/VertexShaderGen.cpp @@ -217,7 +217,7 @@ const char *GenerateVertexShaderCode(u32 components, API_TYPE ApiType) // uniforms if(ApiType == API_GLSL && g_ActiveConfig.backend_info.bSupportsGLSLUBO) - WRITE(p, "layout(std140, binding = 2) uniform VSBlock {\n"); + WRITE(p, "layout(std140%s) uniform VSBlock {\n", g_ActiveConfig.backend_info.bSupportsGLSLBinding ? ", binding = 2" : ""); WRITE(p, "%sfloat4 "I_POSNORMALMATRIX"[6] %s;\n", WriteLocation(ApiType), WriteRegister(ApiType, "c", C_POSNORMALMATRIX)); WRITE(p, "%sfloat4 "I_PROJECTION"[4] %s;\n", WriteLocation(ApiType), WriteRegister(ApiType, "c", C_PROJECTION)); diff --git a/Source/Plugins/Plugin_VideoOGL/Src/ProgramShaderCache.cpp b/Source/Plugins/Plugin_VideoOGL/Src/ProgramShaderCache.cpp index 073e8b430c..37923d2f47 100644 --- a/Source/Plugins/Plugin_VideoOGL/Src/ProgramShaderCache.cpp +++ b/Source/Plugins/Plugin_VideoOGL/Src/ProgramShaderCache.cpp @@ -96,9 +96,13 @@ namespace OGL // Dunno why this is needed when I have the binding // points statically set in the shader source + // We should only need these two functions when we don't support binding but do support UBO // Driver Bug? Nvidia GTX 570, 290.xx Driver, Linux x64 - glUniformBlockBinding( entry.program.glprogid, 0, 1 ); - glUniformBlockBinding( entry.program.glprogid, 1, 2 ); + //if(!g_ActiveConfig.backend_info.bSupportsGLSLBinding) + { + glUniformBlockBinding( entry.program.glprogid, 0, 1 ); + glUniformBlockBinding( entry.program.glprogid, 1, 2 ); + } // We cache our uniform locations for now // Once we move up to a newer version of GLSL, ~1.30 From 97c3c156e61b9e8c4882ead53647b554f606dd16 Mon Sep 17 00:00:00 2001 From: Ryan Houdek Date: Sat, 10 Dec 2011 15:40:10 -0600 Subject: [PATCH 31/83] Use UBOs in every shader. I had missed a few. Only cache Uniform locations if we aren't using UBOs. --- .../Src/TextureConversionShader.cpp | 29 ++++++++-- .../Plugin_VideoOGL/Src/PixelShaderCache.cpp | 54 +++++++++++++------ .../Src/ProgramShaderCache.cpp | 8 +-- .../Plugin_VideoOGL/Src/TextureConverter.cpp | 6 ++- 4 files changed, 73 insertions(+), 24 deletions(-) diff --git a/Source/Core/VideoCommon/Src/TextureConversionShader.cpp b/Source/Core/VideoCommon/Src/TextureConversionShader.cpp index 550fef2740..262b2cee87 100644 --- a/Source/Core/VideoCommon/Src/TextureConversionShader.cpp +++ b/Source/Core/VideoCommon/Src/TextureConversionShader.cpp @@ -74,6 +74,14 @@ const char* WriteRegister(API_TYPE ApiType, const char *prefix, const u32 num) sprintf(result, " : register(%s%d)", prefix, num); return result; } +const char *WriteLocation(API_TYPE ApiType) +{ + if(ApiType == API_GLSL && g_ActiveConfig.backend_info.bSupportsGLSLUBO) + return ""; + static char result[64]; + sprintf(result, "uniform "); + return result; +} // block dimensions : widthStride, heightStride // texture dims : width, height, x offset, y offset @@ -82,7 +90,13 @@ void WriteSwizzler(char*& p, u32 format, API_TYPE ApiType) // [0] left, top, right, bottom of source rectangle within source texture // [1] width and height of destination texture in pixels // Two were merged for GLSL - WRITE(p, "uniform float4 "I_COLORS"[2] %s;\n", WriteRegister(ApiType, "c", C_COLORS)); + if(ApiType == API_GLSL && g_ActiveConfig.backend_info.bSupportsGLSLUBO) + WRITE(p, "layout(std140%s) uniform PSBlock {\n", g_ActiveConfig.backend_info.bSupportsGLSLBinding ? ", binding = 1" : ""); + + WRITE(p, "%sfloat4 "I_COLORS"[2] %s;\n", WriteLocation(ApiType), WriteRegister(ApiType, "c", C_COLORS)); + + if(ApiType == API_GLSL && g_ActiveConfig.backend_info.bSupportsGLSLUBO) + WRITE(p, "};\n"); float blkW = (float)TexDecoder_GetBlockWidthInTexels(format); float blkH = (float)TexDecoder_GetBlockHeightInTexels(format); @@ -168,7 +182,11 @@ void Write32BitSwizzler(char*& p, u32 format, API_TYPE ApiType) // [0] left, top, right, bottom of source rectangle within source texture // [1] width and height of destination texture in pixels // Two were merged for GLSL - WRITE(p, "uniform float4 "I_COLORS"[2] %s;\n", WriteRegister(ApiType, "c", C_COLORS)); + if(ApiType == API_GLSL && g_ActiveConfig.backend_info.bSupportsGLSLUBO) + WRITE(p, "layout(std140%s) uniform PSBlock {\n", g_ActiveConfig.backend_info.bSupportsGLSLBinding ? ", binding = 1" : ""); + WRITE(p, "%sfloat4 "I_COLORS"[2] %s;\n", WriteLocation(ApiType), WriteRegister(ApiType, "c", C_COLORS)); + if(ApiType == API_GLSL && g_ActiveConfig.backend_info.bSupportsGLSLUBO) + WRITE(p, "};\n"); float blkW = (float)TexDecoder_GetBlockWidthInTexels(format); float blkH = (float)TexDecoder_GetBlockHeightInTexels(format); @@ -839,10 +857,13 @@ const char *GenerateEncodingShader(u32 format,API_TYPE ApiType) if(ApiType == API_GLSL) { // A few required defines and ones that will make our lives a lot easier - if (g_ActiveConfig.backend_info.bSupportsGLSLBinding) + if (g_ActiveConfig.backend_info.bSupportsGLSLBinding || g_ActiveConfig.backend_info.bSupportsGLSLUBO) { WRITE(p, "#version 330 compatibility\n"); - WRITE(p, "#extension GL_ARB_shading_language_420pack : enable\n"); + if (g_ActiveConfig.backend_info.bSupportsGLSLBinding) + WRITE(p, "#extension GL_ARB_shading_language_420pack : enable\n"); + if (g_ActiveConfig.backend_info.bSupportsGLSLUBO) + WRITE(p, "#extension GL_ARB_uniform_buffer_object : enable\n"); } else WRITE(p, "#version 120\n"); diff --git a/Source/Plugins/Plugin_VideoOGL/Src/PixelShaderCache.cpp b/Source/Plugins/Plugin_VideoOGL/Src/PixelShaderCache.cpp index 4172237bb2..47df4bb6d7 100644 --- a/Source/Plugins/Plugin_VideoOGL/Src/PixelShaderCache.cpp +++ b/Source/Plugins/Plugin_VideoOGL/Src/PixelShaderCache.cpp @@ -120,7 +120,9 @@ void PixelShaderCache::Init() "#extension GL_ARB_texture_rectangle : enable\n" "#extension GL_ARB_shading_language_420pack : enable\n" "layout(binding = 0) uniform sampler2DRect samp0;\n" - "uniform vec4 "I_COLORS"[7];\n" + "%s\n" + "%svec4 "I_COLORS"[7];\n" + "%s\n" "void main(){\n" "vec4 Temp0, Temp1;\n" "vec4 K0 = vec4(0.5, 0.5, 0.5, 0.5);\n" @@ -134,14 +136,20 @@ void PixelShaderCache::Init() "Temp1.z = dot(Temp0, "I_COLORS"[%d]);\n" "Temp1.w = dot(Temp0, "I_COLORS"[%d]);\n" "gl_FragData[0] = Temp1 + "I_COLORS"[%d];\n" - "}\n", C_COLORS+5, C_COLORS+6, C_COLORS, C_COLORS+1, C_COLORS+2, C_COLORS+3, C_COLORS+4); + "}\n", + g_ActiveConfig.backend_info.bSupportsGLSLUBO ? "layout(std140, binding = 1) uniform PSBlock {" : "", + g_ActiveConfig.backend_info.bSupportsGLSLUBO ? "" : "uniform ", + g_ActiveConfig.backend_info.bSupportsGLSLUBO ? "};" : "", + C_COLORS+5, C_COLORS+6, C_COLORS, C_COLORS+1, C_COLORS+2, C_COLORS+3, C_COLORS+4); } else { sprintf(pmatrixprog, "#version 120\n" "#extension GL_ARB_texture_rectangle : enable\n" "uniform sampler2DRect samp0;\n" - "uniform vec4 "I_COLORS"[7];\n" + "%s\n" + "%svec4 "I_COLORS"[7];\n" + "%s\n" "void main(){\n" "vec4 Temp0, Temp1;\n" "vec4 K0 = vec4(0.5, 0.5, 0.5, 0.5);\n" @@ -155,9 +163,13 @@ void PixelShaderCache::Init() "Temp1.z = dot(Temp0, "I_COLORS"[%d]);\n" "Temp1.w = dot(Temp0, "I_COLORS"[%d]);\n" "gl_FragData[0] = Temp1 + "I_COLORS"[%d];\n" - "}\n", C_COLORS+5, C_COLORS+6, C_COLORS, C_COLORS+1, C_COLORS+2, C_COLORS+3, C_COLORS+4); + "}\n", + g_ActiveConfig.backend_info.bSupportsGLSLUBO ? "layout(std140) uniform PSBlock {" : "", + g_ActiveConfig.backend_info.bSupportsGLSLUBO ? "" : "uniform ", + g_ActiveConfig.backend_info.bSupportsGLSLUBO ? "};" : "", + C_COLORS+5, C_COLORS+6, C_COLORS, C_COLORS+1, C_COLORS+2, C_COLORS+3, C_COLORS+4); } - if (!pCompilePixelShader(s_ColorMatrixProgram, pmatrixprog)) + if (!PixelShaderCache::CompilePixelShader(s_ColorMatrixProgram, pmatrixprog)) { ERROR_LOG(VIDEO, "Failed to create color matrix fragment program"); s_ColorMatrixProgram.Destroy(); @@ -168,7 +180,9 @@ void PixelShaderCache::Init() "#extension GL_ARB_texture_rectangle : enable\n" "#extension GL_ARB_shading_language_420pack : enable\n" "layout(binding = 0) uniform sampler2DRect samp0;\n" - "uniform vec4 "I_COLORS"[5];\n" + "%s\n" + "%svec4 "I_COLORS"[5];\n" + "%s\n" "void main(){\n" "vec4 R0, R1, R2;\n" "vec4 K0 = vec4(255.99998474121, 0.003921568627451, 256.0, 0.0);\n" @@ -192,14 +206,20 @@ void PixelShaderCache::Init() "R1.z = dot(R0, "I_COLORS"[%d]);\n" "R1.w = dot(R0, "I_COLORS"[%d]);\n" "gl_FragData[0] = R1 * "I_COLORS"[%d];\n" - "}\n", C_COLORS, C_COLORS+1, C_COLORS+2, C_COLORS+3, C_COLORS+4); + "}\n", + g_ActiveConfig.backend_info.bSupportsGLSLUBO ? "layout(std140, binding = 1) uniform PSBlock {" : "", + g_ActiveConfig.backend_info.bSupportsGLSLUBO ? "" : "uniform ", + g_ActiveConfig.backend_info.bSupportsGLSLUBO ? "};" : "", + C_COLORS, C_COLORS+1, C_COLORS+2, C_COLORS+3, C_COLORS+4); } else { sprintf(pmatrixprog, "#version 120\n" "#extension GL_ARB_texture_rectangle : enable\n" "uniform sampler2DRect samp0;\n" - "uniform vec4 "I_COLORS"[5];\n" + "%s\n" + "%svec4 "I_COLORS"[5];\n" + "%s\n" "void main(){\n" "vec4 R0, R1, R2;\n" "vec4 K0 = vec4(255.99998474121, 0.003921568627451, 256.0, 0.0);\n" @@ -223,9 +243,13 @@ void PixelShaderCache::Init() "R1.z = dot(R0, "I_COLORS"[%d]);\n" "R1.w = dot(R0, "I_COLORS"[%d]);\n" "gl_FragData[0] = R1 * "I_COLORS"[%d];\n" - "}\n", C_COLORS, C_COLORS+1, C_COLORS+2, C_COLORS+3, C_COLORS+4); + "}\n", + g_ActiveConfig.backend_info.bSupportsGLSLUBO ? "layout(std140) uniform PSBlock {" : "", + g_ActiveConfig.backend_info.bSupportsGLSLUBO ? "" : "uniform ", + g_ActiveConfig.backend_info.bSupportsGLSLUBO ? "};" : "", + C_COLORS, C_COLORS+1, C_COLORS+2, C_COLORS+3, C_COLORS+4); } - if (!pCompilePixelShader(s_DepthMatrixProgram, pmatrixprog)) + if (!PixelShaderCache::CompilePixelShader(s_DepthMatrixProgram, pmatrixprog)) { ERROR_LOG(VIDEO, "Failed to create depth matrix fragment program"); s_DepthMatrixProgram.Destroy(); @@ -444,9 +468,9 @@ bool CompileGLSLPixelShader(FRAGMENTSHADER& ps, const char* pstrprogram) GLsizei charsWritten; GLchar* infoLog = new GLchar[length]; glGetShaderInfoLog(result, length, &charsWritten, infoLog); - WARN_LOG(VIDEO, "VS Shader info log:\n%s", infoLog); + WARN_LOG(VIDEO, "PS Shader info log:\n%s", infoLog); char szTemp[MAX_PATH]; - sprintf(szTemp, "vs_%d.txt", result); + sprintf(szTemp, "ps_%d.txt", result); FILE *fp = fopen(szTemp, "wb"); fwrite(pstrprogram, strlen(pstrprogram), 1, fp); fclose(fp); @@ -503,7 +527,7 @@ void SetGLSLPSConstant4f(unsigned int const_number, float f1, float f2, float f3 if (g_ActiveConfig.backend_info.bSupportsGLSLUBO) { ProgramShaderCache::SetUniformObjects(0, const_number, f); - //return; + return; } for (unsigned int a = 0; a < 10; ++a) { @@ -521,7 +545,7 @@ void SetGLSLPSConstant4fv(unsigned int const_number, const float *f) if (g_ActiveConfig.backend_info.bSupportsGLSLUBO) { ProgramShaderCache::SetUniformObjects(0, const_number, f); - //return; + return; } for (unsigned int a = 0; a < 10; ++a) { @@ -539,7 +563,7 @@ void SetMultiGLSLPSConstant4fv(unsigned int const_number, unsigned int count, co if (g_ActiveConfig.backend_info.bSupportsGLSLUBO) { ProgramShaderCache::SetUniformObjects(0, const_number, f, count); - //return; + return; } for (unsigned int a = 0; a < 10; ++a) { diff --git a/Source/Plugins/Plugin_VideoOGL/Src/ProgramShaderCache.cpp b/Source/Plugins/Plugin_VideoOGL/Src/ProgramShaderCache.cpp index 37923d2f47..562843ebb1 100644 --- a/Source/Plugins/Plugin_VideoOGL/Src/ProgramShaderCache.cpp +++ b/Source/Plugins/Plugin_VideoOGL/Src/ProgramShaderCache.cpp @@ -111,8 +111,9 @@ namespace OGL //For some reason this fails on my hardware //glGetUniformIndices(entry.program.glprogid, NUM_UNIFORMS, UniformNames, entry.program.UniformLocations); //Got to do it this crappy way. - for(int a = 0; a < NUM_UNIFORMS; ++a) - entry.program.UniformLocations[a] = glGetUniformLocation(entry.program.glprogid, UniformNames[a]); + if(!g_ActiveConfig.backend_info.bSupportsGLSLUBO) + for(int a = 0; a < NUM_UNIFORMS; ++a) + entry.program.UniformLocations[a] = glGetUniformLocation(entry.program.glprogid, UniformNames[a]); // Need to get some attribute locations if(uid.uid.vsid != 0) // We have no vertex Shader @@ -176,7 +177,7 @@ namespace OGL // Repeat for VS shader glBindBuffer(GL_UNIFORM_BUFFER, UBOBuffers[1]); - glBufferData(GL_UNIFORM_BUFFER, 1024*1024, NULL, GL_DYNAMIC_DRAW); + glBufferData(GL_UNIFORM_BUFFER, 1024 * 1024, NULL, GL_DYNAMIC_DRAW); glBindBufferBase(GL_UNIFORM_BUFFER, 2, UBOBuffers[1]); } void ProgramShaderCache::Shutdown(void) @@ -185,6 +186,7 @@ namespace OGL for (; iter != pshaders.end(); iter++) iter->second.Destroy(); pshaders.clear(); + glDeleteBuffers(2, UBOBuffers); } } diff --git a/Source/Plugins/Plugin_VideoOGL/Src/TextureConverter.cpp b/Source/Plugins/Plugin_VideoOGL/Src/TextureConverter.cpp index 6e2c46c95c..f89ca38b05 100644 --- a/Source/Plugins/Plugin_VideoOGL/Src/TextureConverter.cpp +++ b/Source/Plugins/Plugin_VideoOGL/Src/TextureConverter.cpp @@ -579,8 +579,10 @@ void DecodeToTexture(u32 xfbAddr, int srcWidth, int srcHeight, GLuint destTextur } glViewport(0, 0, srcWidth, srcHeight); - - PixelShaderCache::SetCurrentShader(s_yuyvToRgbProgram.glprogid); + if(g_ActiveConfig.bUseGLSL) + ProgramShaderCache::SetBothShaders(s_yuyvToRgbProgram.glprogid, 0); + else + PixelShaderCache::SetCurrentShader(s_yuyvToRgbProgram.glprogid); GL_REPORT_ERRORD(); From c678172f3212b90ac394359b2be732e76a2bc39c Mon Sep 17 00:00:00 2001 From: Ryan Houdek Date: Sat, 10 Dec 2011 15:52:20 -0600 Subject: [PATCH 32/83] Make sure our UBO buffers are always aligned correctly. --- .../Plugins/Plugin_VideoOGL/Src/ProgramShaderCache.cpp | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/Source/Plugins/Plugin_VideoOGL/Src/ProgramShaderCache.cpp b/Source/Plugins/Plugin_VideoOGL/Src/ProgramShaderCache.cpp index 562843ebb1..c62016a853 100644 --- a/Source/Plugins/Plugin_VideoOGL/Src/ProgramShaderCache.cpp +++ b/Source/Plugins/Plugin_VideoOGL/Src/ProgramShaderCache.cpp @@ -164,20 +164,26 @@ namespace OGL } void ProgramShaderCache::Init(void) { + // We have to get the UBO alignment here because + // if we generate a buffer that isn't aligned + // then the UBO will fail. + GLint Align; + glGetIntegerv(GL_UNIFORM_BUFFER_OFFSET_ALIGNMENT, &Align); + glGenBuffers(2, UBOBuffers); glBindBuffer(GL_UNIFORM_BUFFER, UBOBuffers[0]); // We multiply by *4*4 because we need to get down to basic machine units. // So multiply by four to get how many floats we have from vec4s // Then once more to get bytes - glBufferData(GL_UNIFORM_BUFFER, C_PENVCONST_END * 4 * 4, NULL, GL_DYNAMIC_DRAW); + glBufferData(GL_UNIFORM_BUFFER, (C_PENVCONST_END * 4 * 4) - (C_PENVCONST_END * 4 * 4 % Align) + Align, NULL, GL_DYNAMIC_DRAW); // Now bind the buffer to the index point // We know PS is 0 since we have it statically set in the shader glBindBufferBase(GL_UNIFORM_BUFFER, 1, UBOBuffers[0]); // Repeat for VS shader glBindBuffer(GL_UNIFORM_BUFFER, UBOBuffers[1]); - glBufferData(GL_UNIFORM_BUFFER, 1024 * 1024, NULL, GL_DYNAMIC_DRAW); + glBufferData(GL_UNIFORM_BUFFER, (C_VENVCONST_END * 4 * 4) - (C_VENVCONST_END * 4 * 4 % Align) + Align, NULL, GL_DYNAMIC_DRAW); glBindBufferBase(GL_UNIFORM_BUFFER, 2, UBOBuffers[1]); } void ProgramShaderCache::Shutdown(void) From 2907ffd72c45334974c96df3e947d90b70668893 Mon Sep 17 00:00:00 2001 From: Ryan Houdek Date: Sat, 10 Dec 2011 15:58:44 -0600 Subject: [PATCH 33/83] Make this pretty --- Source/Plugins/Plugin_VideoOGL/Src/ProgramShaderCache.cpp | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/Source/Plugins/Plugin_VideoOGL/Src/ProgramShaderCache.cpp b/Source/Plugins/Plugin_VideoOGL/Src/ProgramShaderCache.cpp index c62016a853..d66fd9ec19 100644 --- a/Source/Plugins/Plugin_VideoOGL/Src/ProgramShaderCache.cpp +++ b/Source/Plugins/Plugin_VideoOGL/Src/ProgramShaderCache.cpp @@ -16,6 +16,8 @@ // http://code.google.com/p/dolphin-emu/ #include "ProgramShaderCache.h" +#include "MathUtil.h" + #include namespace OGL { @@ -176,14 +178,14 @@ namespace OGL // We multiply by *4*4 because we need to get down to basic machine units. // So multiply by four to get how many floats we have from vec4s // Then once more to get bytes - glBufferData(GL_UNIFORM_BUFFER, (C_PENVCONST_END * 4 * 4) - (C_PENVCONST_END * 4 * 4 % Align) + Align, NULL, GL_DYNAMIC_DRAW); + glBufferData(GL_UNIFORM_BUFFER, ROUND_UP(C_PENVCONST_END * 4 * 4, Align), NULL, GL_DYNAMIC_DRAW); // Now bind the buffer to the index point // We know PS is 0 since we have it statically set in the shader glBindBufferBase(GL_UNIFORM_BUFFER, 1, UBOBuffers[0]); // Repeat for VS shader glBindBuffer(GL_UNIFORM_BUFFER, UBOBuffers[1]); - glBufferData(GL_UNIFORM_BUFFER, (C_VENVCONST_END * 4 * 4) - (C_VENVCONST_END * 4 * 4 % Align) + Align, NULL, GL_DYNAMIC_DRAW); + glBufferData(GL_UNIFORM_BUFFER, ROUND_UP(C_VENVCONST_END * 4 * 4, Align), NULL, GL_DYNAMIC_DRAW); glBindBufferBase(GL_UNIFORM_BUFFER, 2, UBOBuffers[1]); } void ProgramShaderCache::Shutdown(void) From 1724385c8c52769c350d626b9b35b3bfd788522a Mon Sep 17 00:00:00 2001 From: Ryan Houdek Date: Sun, 11 Dec 2011 03:10:03 -0600 Subject: [PATCH 34/83] Actually have Dual Source blending work for people. Forgot about this change. --- Source/Plugins/Plugin_VideoOGL/Src/GLUtil.h | 7 ++++++- Source/Plugins/Plugin_VideoOGL/Src/Render.cpp | 4 ++-- 2 files changed, 8 insertions(+), 3 deletions(-) diff --git a/Source/Plugins/Plugin_VideoOGL/Src/GLUtil.h b/Source/Plugins/Plugin_VideoOGL/Src/GLUtil.h index 23c31a1bb7..7d309dee38 100644 --- a/Source/Plugins/Plugin_VideoOGL/Src/GLUtil.h +++ b/Source/Plugins/Plugin_VideoOGL/Src/GLUtil.h @@ -124,6 +124,11 @@ extern CGprofile g_cgvProf, g_cgfProf; // XXX: Dual-source blending in OpenGL does not work correctly yet. To make it // work, we may need to use glBindFragDataLocation. To use that, we need to // use GLSL shaders across the whole pipeline. Yikes! -//#define USE_DUAL_SOURCE_BLEND +#define USE_DUAL_SOURCE_BLEND + +// Can't get defined on my system for some reason... +#ifndef GL_ONE_MINUS_SRC1_ALPHA +#define GL_ONE_MINUS_SRC1_ALPHA 0x88FB +#endif #endif // _GLINIT_H_ diff --git a/Source/Plugins/Plugin_VideoOGL/Src/Render.cpp b/Source/Plugins/Plugin_VideoOGL/Src/Render.cpp index 1dae8d0752..86452adebd 100644 --- a/Source/Plugins/Plugin_VideoOGL/Src/Render.cpp +++ b/Source/Plugins/Plugin_VideoOGL/Src/Render.cpp @@ -940,8 +940,8 @@ void Renderer::SetBlendMode(bool forceUpdate) #ifdef USE_DUAL_SOURCE_BLEND bool useDstAlpha = !g_ActiveConfig.bDstAlphaPass && bpmem.dstalpha.enable && bpmem.blendmode.alphaupdate - && bpmem.zcontrol.pixel_format == PIXELFMT_RGBA6_Z24; - bool useDualSource = useDstAlpha && GLEW_ARB_blend_func_extended; + && bpmem.zcontrol.pixel_format == PIXELFMT_RGBA6_Z24 && g_ActiveConfig.bUseGLSL; + bool useDualSource = useDstAlpha; #endif if (changes & 1) From 014c474024e28e77d2bb72e0af7acb31850fba64 Mon Sep 17 00:00:00 2001 From: Pierre Bourdon Date: Sun, 11 Dec 2011 11:08:18 +0100 Subject: [PATCH 35/83] 8 spaces indentation -> tabs --- Source/Core/VideoCommon/Src/PixelShaderGen.h | 20 +- .../Src/TextureConversionShader.cpp | 22 +- Source/Core/VideoCommon/Src/VertexShaderGen.h | 18 +- Source/Core/VideoCommon/Src/VideoCommon.h | 6 +- .../Plugin_VideoOGL/Src/PixelShaderCache.cpp | 234 ++++++++--------- .../Src/ProgramShaderCache.cpp | 238 ++++++++--------- .../Plugin_VideoOGL/Src/ProgramShaderCache.h | 108 ++++---- .../Plugin_VideoOGL/Src/TextureCache.cpp | 8 +- .../Plugin_VideoOGL/Src/TextureConverter.cpp | 2 +- .../Plugin_VideoOGL/Src/VertexShaderCache.cpp | 246 +++++++++--------- Source/Plugins/Plugin_VideoOGL/Src/main.cpp | 34 +-- 11 files changed, 468 insertions(+), 468 deletions(-) diff --git a/Source/Core/VideoCommon/Src/PixelShaderGen.h b/Source/Core/VideoCommon/Src/PixelShaderGen.h index b32b7408c3..3228d50b99 100644 --- a/Source/Core/VideoCommon/Src/PixelShaderGen.h +++ b/Source/Core/VideoCommon/Src/PixelShaderGen.h @@ -49,16 +49,16 @@ // Annoying sure, can be removed once we get up to GLSL ~1.3 const s_svar PSVar_Loc[] = { {I_COLORS, C_COLORS, 4 }, - {I_KCOLORS, C_KCOLORS, 4 }, - {I_ALPHA, C_ALPHA, 1 }, - {I_TEXDIMS, C_TEXDIMS, 8 }, - {I_ZBIAS , C_ZBIAS, 2 }, - {I_INDTEXSCALE , C_INDTEXSCALE, 2 }, - {I_INDTEXMTX, C_INDTEXMTX, 6 }, - {I_FOG, C_FOG, 3 }, - {I_PLIGHTS, C_PLIGHTS, 40 }, - {I_PMATERIALS, C_PMATERIALS, 4 }, - }; + {I_KCOLORS, C_KCOLORS, 4 }, + {I_ALPHA, C_ALPHA, 1 }, + {I_TEXDIMS, C_TEXDIMS, 8 }, + {I_ZBIAS , C_ZBIAS, 2 }, + {I_INDTEXSCALE , C_INDTEXSCALE, 2 }, + {I_INDTEXMTX, C_INDTEXMTX, 6 }, + {I_FOG, C_FOG, 3 }, + {I_PLIGHTS, C_PLIGHTS, 40 }, + {I_PMATERIALS, C_PMATERIALS, 4 }, + }; // DO NOT make anything in this class virtual. template diff --git a/Source/Core/VideoCommon/Src/TextureConversionShader.cpp b/Source/Core/VideoCommon/Src/TextureConversionShader.cpp index 262b2cee87..2f21c816b7 100644 --- a/Source/Core/VideoCommon/Src/TextureConversionShader.cpp +++ b/Source/Core/VideoCommon/Src/TextureConversionShader.cpp @@ -68,11 +68,11 @@ u16 GetEncodedSampleCount(u32 format) } const char* WriteRegister(API_TYPE ApiType, const char *prefix, const u32 num) { - if(ApiType == API_GLSL) - return ""; // Once we switch to GLSL 1.3 we can do something here - static char result[64]; - sprintf(result, " : register(%s%d)", prefix, num); - return result; + if(ApiType == API_GLSL) + return ""; // Once we switch to GLSL 1.3 we can do something here + static char result[64]; + sprintf(result, " : register(%s%d)", prefix, num); + return result; } const char *WriteLocation(API_TYPE ApiType) { @@ -821,13 +821,13 @@ void WriteZ24Encoder(char* p, API_TYPE ApiType) for (int i = 0; i < 2; i++) { - WRITE(p, " depth%i *= 16777215.0f;\n", i); + WRITE(p, " depth%i *= 16777215.0f;\n", i); - WRITE(p, " expanded%i.r = floor(depth%i / (256 * 256));\n", i, i); - WRITE(p, " depth%i -= expanded%i.r * 256 * 256;\n", i, i); - WRITE(p, " expanded%i.g = floor(depth%i / 256);\n", i, i); - WRITE(p, " depth%i -= expanded%i.g * 256;\n", i, i); - WRITE(p, " expanded%i.b = depth%i;\n", i, i); + WRITE(p, " expanded%i.r = floor(depth%i / (256 * 256));\n", i, i); + WRITE(p, " depth%i -= expanded%i.r * 256 * 256;\n", i, i); + WRITE(p, " expanded%i.g = floor(depth%i / 256);\n", i, i); + WRITE(p, " depth%i -= expanded%i.g * 256;\n", i, i); + WRITE(p, " expanded%i.b = depth%i;\n", i, i); } WRITE(p, " if(cl > 0.5f) {\n"); diff --git a/Source/Core/VideoCommon/Src/VertexShaderGen.h b/Source/Core/VideoCommon/Src/VertexShaderGen.h index c711aa7876..5cfb9f801d 100644 --- a/Source/Core/VideoCommon/Src/VertexShaderGen.h +++ b/Source/Core/VideoCommon/Src/VertexShaderGen.h @@ -48,15 +48,15 @@ #define C_DEPTHPARAMS (C_POSTTRANSFORMMATRICES + 64) #define C_VENVCONST_END (C_DEPTHPARAMS + 1) const s_svar VSVar_Loc[] = { {I_POSNORMALMATRIX, C_POSNORMALMATRIX, 6 }, - {I_PROJECTION , C_PROJECTION, 4 }, - {I_MATERIALS, C_MATERIALS, 4 }, - {I_LIGHTS, C_LIGHTS, 40 }, - {I_TEXMATRICES, C_TEXMATRICES, 24 }, - {I_TRANSFORMMATRICES , C_TRANSFORMMATRICES, 64 }, - {I_NORMALMATRICES , C_NORMALMATRICES, 32 }, - {I_POSTTRANSFORMMATRICES, C_POSTTRANSFORMMATRICES, 64 }, - {I_DEPTHPARAMS, C_DEPTHPARAMS, 1 }, - }; + {I_PROJECTION , C_PROJECTION, 4 }, + {I_MATERIALS, C_MATERIALS, 4 }, + {I_LIGHTS, C_LIGHTS, 40 }, + {I_TEXMATRICES, C_TEXMATRICES, 24 }, + {I_TRANSFORMMATRICES , C_TRANSFORMMATRICES, 64 }, + {I_NORMALMATRICES , C_NORMALMATRICES, 32 }, + {I_POSTTRANSFORMMATRICES, C_POSTTRANSFORMMATRICES, 64 }, + {I_DEPTHPARAMS, C_DEPTHPARAMS, 1 }, + }; template class _VERTEXSHADERUID { diff --git a/Source/Core/VideoCommon/Src/VideoCommon.h b/Source/Core/VideoCommon/Src/VideoCommon.h index f87cb7e9b1..9d9148ec7b 100644 --- a/Source/Core/VideoCommon/Src/VideoCommon.h +++ b/Source/Core/VideoCommon/Src/VideoCommon.h @@ -154,9 +154,9 @@ inline unsigned int GetPow2(unsigned int val) } struct s_svar { - const char *name; - const unsigned int reg; - const unsigned int size; + const char *name; + const unsigned int reg; + const unsigned int size; }; #endif // _VIDEOCOMMON_H diff --git a/Source/Plugins/Plugin_VideoOGL/Src/PixelShaderCache.cpp b/Source/Plugins/Plugin_VideoOGL/Src/PixelShaderCache.cpp index 47df4bb6d7..db41f34024 100644 --- a/Source/Plugins/Plugin_VideoOGL/Src/PixelShaderCache.cpp +++ b/Source/Plugins/Plugin_VideoOGL/Src/PixelShaderCache.cpp @@ -74,12 +74,12 @@ void PixelShaderCache::Init() if(g_ActiveConfig.bUseGLSL) { - pSetPSConstant4f = SetGLSLPSConstant4f; - pSetPSConstant4fv = SetGLSLPSConstant4fv; - pSetMultiPSConstant4fv = SetMultiGLSLPSConstant4fv; - pCompilePixelShader = CompileGLSLPixelShader; - // Should this be set here? - if (strstr((const char*)glGetString(GL_EXTENSIONS), "GL_ARB_shading_language_420pack") != NULL) + pSetPSConstant4f = SetGLSLPSConstant4f; + pSetPSConstant4fv = SetGLSLPSConstant4fv; + pSetMultiPSConstant4fv = SetMultiGLSLPSConstant4fv; + pCompilePixelShader = CompileGLSLPixelShader; + // Should this be set here? + if (strstr((const char*)glGetString(GL_EXTENSIONS), "GL_ARB_shading_language_420pack") != NULL) g_Config.backend_info.bSupportsGLSLBinding = true; if (strstr((const char*)glGetString(GL_EXTENSIONS), "GL_ARB_uniform_buffer_object") != NULL) g_Config.backend_info.bSupportsGLSLUBO = true; @@ -88,22 +88,22 @@ void PixelShaderCache::Init() } else { - pSetPSConstant4f = SetCGPSConstant4f; - pSetPSConstant4fv = SetCGPSConstant4fv; - pSetMultiPSConstant4fv = SetMultiCGPSConstant4fv; - pCompilePixelShader = CompileCGPixelShader; - glEnable(GL_FRAGMENT_PROGRAM_ARB); + pSetPSConstant4f = SetCGPSConstant4f; + pSetPSConstant4fv = SetCGPSConstant4fv; + pSetMultiPSConstant4fv = SetMultiCGPSConstant4fv; + pCompilePixelShader = CompileCGPixelShader; + glEnable(GL_FRAGMENT_PROGRAM_ARB); } glGetProgramivARB(GL_FRAGMENT_PROGRAM_ARB, GL_MAX_PROGRAM_NATIVE_ALU_INSTRUCTIONS_ARB, (GLint *)&s_nMaxPixelInstructions); if(s_nMaxPixelInstructions == 0) // Some combination of drivers and hardware returns zero for some reason. - s_nMaxPixelInstructions = 4096; + s_nMaxPixelInstructions = 4096; if (strstr((const char*)glGetString(GL_VENDOR), "Humper") != NULL) s_nMaxPixelInstructions = 4096; #if CG_VERSION_NUM == 2100 if (strstr((const char*)glGetString(GL_VENDOR), "ATI") != NULL) { - s_nMaxPixelInstructions = 4096; + s_nMaxPixelInstructions = 4096; } #endif @@ -347,7 +347,7 @@ void PixelShaderCache::Shutdown() s_DepthMatrixProgram.Destroy(); PSCache::iterator iter = PixelShaders.begin(); for (; iter != PixelShaders.end(); iter++) - iter->second.Destroy(); + iter->second.Destroy(); PixelShaders.clear(); } @@ -359,12 +359,12 @@ FRAGMENTSHADER* PixelShaderCache::SetShader(DSTALPHA_MODE dstAlphaMode, u32 comp // Check if the shader is already set if (last_entry) { - if (uid == last_uid) - { - GFX_DEBUGGER_PAUSE_AT(NEXT_PIXEL_SHADER_CHANGE, true); - ValidatePixelShaderIDs(API_OPENGL, last_entry->safe_uid, last_entry->shader.strprog, dstAlphaMode, components); - return &last_entry->shader; - } + if (uid == last_uid) + { + GFX_DEBUGGER_PAUSE_AT(NEXT_PIXEL_SHADER_CHANGE, true); + ValidatePixelShaderIDs(API_OPENGL, last_entry->safe_uid, last_entry->shader.strprog, dstAlphaMode, components); + return &last_entry->shader; + } } last_uid = uid; @@ -372,12 +372,12 @@ FRAGMENTSHADER* PixelShaderCache::SetShader(DSTALPHA_MODE dstAlphaMode, u32 comp PSCache::iterator iter = PixelShaders.find(uid); if (iter != PixelShaders.end()) { - PSCacheEntry &entry = iter->second; - last_entry = &entry; + PSCacheEntry &entry = iter->second; + last_entry = &entry; - GFX_DEBUGGER_PAUSE_AT(NEXT_PIXEL_SHADER_CHANGE, true); - ValidatePixelShaderIDs(API_OPENGL, entry.safe_uid, entry.shader.strprog, dstAlphaMode, components); - return &last_entry->shader; + GFX_DEBUGGER_PAUSE_AT(NEXT_PIXEL_SHADER_CHANGE, true); + ValidatePixelShaderIDs(API_OPENGL, entry.safe_uid, entry.shader.strprog, dstAlphaMode, components); + return &last_entry->shader; } // Make an entry in the table @@ -387,23 +387,23 @@ FRAGMENTSHADER* PixelShaderCache::SetShader(DSTALPHA_MODE dstAlphaMode, u32 comp if (g_ActiveConfig.bEnableShaderDebugging && code) { - GetSafePixelShaderId(&newentry.safe_uid, dstAlphaMode, components); - newentry.shader.strprog = code; + GetSafePixelShaderId(&newentry.safe_uid, dstAlphaMode, components); + newentry.shader.strprog = code; } #if defined(_DEBUG) || defined(DEBUGFAST) if (g_ActiveConfig.iLog & CONF_SAVESHADERS && code) { - static int counter = 0; - char szTemp[MAX_PATH]; - sprintf(szTemp, "%sps_%04i.txt", File::GetUserPath(D_DUMP_IDX).c_str(), counter++); + static int counter = 0; + char szTemp[MAX_PATH]; + sprintf(szTemp, "%sps_%04i.txt", File::GetUserPath(D_DUMP_IDX).c_str(), counter++); - SaveData(szTemp, code); + SaveData(szTemp, code); } #endif if (!code || !CompilePixelShader(newentry.shader, code)) { - GFX_DEBUGGER_PAUSE_AT(NEXT_ERROR, true); - return NULL; + GFX_DEBUGGER_PAUSE_AT(NEXT_ERROR, true); + return NULL; } INCSTAT(stats.numPixelShadersCreated); @@ -424,8 +424,8 @@ void PixelShaderCache::DisableShader() assert(true); if(ShaderEnabled) { - glDisable(GL_FRAGMENT_PROGRAM_ARB); - ShaderEnabled = false; + glDisable(GL_FRAGMENT_PROGRAM_ARB); + ShaderEnabled = false; } } @@ -436,14 +436,14 @@ void PixelShaderCache::SetCurrentShader(GLuint Shader) assert(true); if(!ShaderEnabled) { - glEnable(GL_FRAGMENT_PROGRAM_ARB); - ShaderEnabled = true; + glEnable(GL_FRAGMENT_PROGRAM_ARB); + ShaderEnabled = true; } if(CurrentShader != Shader) { - if(Shader != 0) - CurrentShader = Shader; - glBindProgramARB(GL_FRAGMENT_PROGRAM_ARB, CurrentShader); + if(Shader != 0) + CurrentShader = Shader; + glBindProgramARB(GL_FRAGMENT_PROGRAM_ARB, CurrentShader); } } // GLSL Specific @@ -493,31 +493,31 @@ void PixelShaderCache::SetPSSampler(const char * name, unsigned int Tex) return; PROGRAMSHADER tmp = ProgramShaderCache::GetShaderProgram(); for (int a = 0; a < NUM_UNIFORMS; ++a) - if (!strcmp(name, UniformNames[a])) - { - if(tmp.UniformLocations[a] == -1) - return; - else - { - glUniform1i(tmp.UniformLocations[a], Tex); - return; - } - } + if (!strcmp(name, UniformNames[a])) + { + if(tmp.UniformLocations[a] == -1) + return; + else + { + glUniform1i(tmp.UniformLocations[a], Tex); + return; + } + } } void SetPSConstant4fvByName(const char * name, unsigned int offset, const float *f, const unsigned int count = 1) { PROGRAMSHADER tmp = ProgramShaderCache::GetShaderProgram(); for (int a = 0; a < NUM_UNIFORMS; ++a) - if (!strcmp(name, UniformNames[a])) - { - if(tmp.UniformLocations[a] == -1) - return; - else - { - glUniform4fv(tmp.UniformLocations[a] + offset, count, f); - return; - } - } + if (!strcmp(name, UniformNames[a])) + { + if(tmp.UniformLocations[a] == -1) + return; + else + { + glUniform4fv(tmp.UniformLocations[a] + offset, count, f); + return; + } + } } void SetGLSLPSConstant4f(unsigned int const_number, float f1, float f2, float f3, float f4) @@ -531,12 +531,12 @@ void SetGLSLPSConstant4f(unsigned int const_number, float f1, float f2, float f3 } for (unsigned int a = 0; a < 10; ++a) { - if ( const_number >= PSVar_Loc[a].reg && const_number < (PSVar_Loc[a].reg + PSVar_Loc[a].size)) - { - unsigned int offset = const_number - PSVar_Loc[a].reg; - SetPSConstant4fvByName(PSVar_Loc[a].name, offset, f); - return; - } + if ( const_number >= PSVar_Loc[a].reg && const_number < (PSVar_Loc[a].reg + PSVar_Loc[a].size)) + { + unsigned int offset = const_number - PSVar_Loc[a].reg; + SetPSConstant4fvByName(PSVar_Loc[a].name, offset, f); + return; + } } } @@ -549,12 +549,12 @@ void SetGLSLPSConstant4fv(unsigned int const_number, const float *f) } for (unsigned int a = 0; a < 10; ++a) { - if ( const_number >= PSVar_Loc[a].reg && const_number < (PSVar_Loc[a].reg + PSVar_Loc[a].size)) - { - unsigned int offset = const_number - PSVar_Loc[a].reg; - SetPSConstant4fvByName(PSVar_Loc[a].name, offset, f); - return; - } + if ( const_number >= PSVar_Loc[a].reg && const_number < (PSVar_Loc[a].reg + PSVar_Loc[a].size)) + { + unsigned int offset = const_number - PSVar_Loc[a].reg; + SetPSConstant4fvByName(PSVar_Loc[a].name, offset, f); + return; + } } } @@ -567,12 +567,12 @@ void SetMultiGLSLPSConstant4fv(unsigned int const_number, unsigned int count, co } for (unsigned int a = 0; a < 10; ++a) { - if (const_number >= PSVar_Loc[a].reg && const_number < (PSVar_Loc[a].reg + PSVar_Loc[a].size)) - { - unsigned int offset = const_number - PSVar_Loc[a].reg; - SetPSConstant4fvByName(PSVar_Loc[a].name, offset, f, count); - return; - } + if (const_number >= PSVar_Loc[a].reg && const_number < (PSVar_Loc[a].reg + PSVar_Loc[a].size)) + { + unsigned int offset = const_number - PSVar_Loc[a].reg; + SetPSConstant4fvByName(PSVar_Loc[a].name, offset, f, count); + return; + } } } // CG Specific @@ -582,7 +582,7 @@ bool CompileCGPixelShader(FRAGMENTSHADER& ps, const char* pstrprogram) GLenum err = GL_REPORT_ERROR(); if (err != GL_NO_ERROR) { - ERROR_LOG(VIDEO, "glError %08x before PS!", err); + ERROR_LOG(VIDEO, "glError %08x before PS!", err); } #if defined HAVE_CG && HAVE_CG @@ -591,28 +591,28 @@ bool CompileCGPixelShader(FRAGMENTSHADER& ps, const char* pstrprogram) // handle errors if (!cgIsProgram(tempprog)) { - cgDestroyProgram(tempprog); + cgDestroyProgram(tempprog); - static int num_failures = 0; - char szTemp[MAX_PATH]; - sprintf(szTemp, "bad_ps_%04i.txt", num_failures++); - std::ofstream file(szTemp); - file << pstrprogram; - file.close(); + static int num_failures = 0; + char szTemp[MAX_PATH]; + sprintf(szTemp, "bad_ps_%04i.txt", num_failures++); + std::ofstream file(szTemp); + file << pstrprogram; + file.close(); - PanicAlert("Failed to compile pixel shader %d!\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 (%d):\n%s", - num_failures - 1, szTemp, - g_cgfProf, - cgGetLastListing(g_cgcontext)); + PanicAlert("Failed to compile pixel shader %d!\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 (%d):\n%s", + num_failures - 1, szTemp, + g_cgfProf, + cgGetLastListing(g_cgcontext)); - return false; + return false; } // handle warnings if (cgGetError() != CG_NO_ERROR) { - WARN_LOG(VIDEO, "Warnings on compile ps %s:", cgGetLastListing(g_cgcontext)); - WARN_LOG(VIDEO, "%s", pstrprogram); + WARN_LOG(VIDEO, "Warnings on compile ps %s:", cgGetLastListing(g_cgcontext)); + WARN_LOG(VIDEO, "%s", pstrprogram); } // This looks evil - we modify the program through the const char * we got from cgGetProgramString! @@ -621,9 +621,9 @@ bool CompileCGPixelShader(FRAGMENTSHADER& ps, const char* pstrprogram) char *plocal = strstr(pcompiledprog, "program.local"); while (plocal != NULL) { - const char *penv = " program.env"; - memcpy(plocal, penv, 13); - plocal = strstr(plocal+13, "program.local"); + const char *penv = " program.env"; + memcpy(plocal, penv, 13); + plocal = strstr(plocal+13, "program.local"); } glGenProgramsARB(1, &ps.glprogid); @@ -634,23 +634,23 @@ bool CompileCGPixelShader(FRAGMENTSHADER& ps, const char* pstrprogram) err = GL_REPORT_ERROR(); if (err != GL_NO_ERROR) { - GLint error_pos, native_limit; - glGetIntegerv(GL_PROGRAM_ERROR_POSITION_ARB, &error_pos); - glGetProgramivARB(GL_FRAGMENT_PROGRAM_ARB, GL_PROGRAM_UNDER_NATIVE_LIMITS_ARB, &native_limit); - // Error occur - if (error_pos != -1) { - const char *program_error = (const char *)glGetString(GL_PROGRAM_ERROR_STRING_ARB); - char line[256]; - strncpy(line, (const char *)pcompiledprog + error_pos, 255); - line[255] = 0; - ERROR_LOG(VIDEO, "Error at %i: %s", error_pos, program_error); - ERROR_LOG(VIDEO, "Line dump: \n%s", line); - } else if (native_limit != -1) { - ERROR_LOG(VIDEO, "Hit limit? %i", native_limit); - // TODO - } - ERROR_LOG(VIDEO, "%s", pstrprogram); - ERROR_LOG(VIDEO, "%s", pcompiledprog); + GLint error_pos, native_limit; + glGetIntegerv(GL_PROGRAM_ERROR_POSITION_ARB, &error_pos); + glGetProgramivARB(GL_FRAGMENT_PROGRAM_ARB, GL_PROGRAM_UNDER_NATIVE_LIMITS_ARB, &native_limit); + // Error occur + if (error_pos != -1) { + const char *program_error = (const char *)glGetString(GL_PROGRAM_ERROR_STRING_ARB); + char line[256]; + strncpy(line, (const char *)pcompiledprog + error_pos, 255); + line[255] = 0; + ERROR_LOG(VIDEO, "Error at %i: %s", error_pos, program_error); + ERROR_LOG(VIDEO, "Line dump: \n%s", line); + } else if (native_limit != -1) { + ERROR_LOG(VIDEO, "Hit limit? %i", native_limit); + // TODO + } + ERROR_LOG(VIDEO, "%s", pstrprogram); + ERROR_LOG(VIDEO, "%s", pcompiledprog); } cgDestroyProgram(tempprog); @@ -672,21 +672,21 @@ void SetCGPSConstant4fv(unsigned int const_number, const float *f) void SetMultiCGPSConstant4fv(unsigned int const_number, unsigned int count, const float *f) { for (unsigned int i = 0; i < count; i++,f+=4) - glProgramEnvParameter4fvARB(GL_FRAGMENT_PROGRAM_ARB, const_number + i, f); + glProgramEnvParameter4fvARB(GL_FRAGMENT_PROGRAM_ARB, const_number + i, f); } // Renderer functions void Renderer::SetPSConstant4f(unsigned int const_number, float f1, float f2, float f3, float f4) { - pSetPSConstant4f(const_number, f1, f2, f3, f4); + pSetPSConstant4f(const_number, f1, f2, f3, f4); } void Renderer::SetPSConstant4fv(unsigned int const_number, const float *f) { - pSetPSConstant4fv(const_number, f); + pSetPSConstant4fv(const_number, f); } void Renderer::SetMultiPSConstant4fv(unsigned int const_number, unsigned int count, const float *f) { - pSetMultiPSConstant4fv(const_number, count, f); + pSetMultiPSConstant4fv(const_number, count, f); } } // namespace OGL diff --git a/Source/Plugins/Plugin_VideoOGL/Src/ProgramShaderCache.cpp b/Source/Plugins/Plugin_VideoOGL/Src/ProgramShaderCache.cpp index d66fd9ec19..15a5ff42a8 100644 --- a/Source/Plugins/Plugin_VideoOGL/Src/ProgramShaderCache.cpp +++ b/Source/Plugins/Plugin_VideoOGL/Src/ProgramShaderCache.cpp @@ -21,123 +21,123 @@ #include namespace OGL { - GLuint ProgramShaderCache::CurrentFShader = 0, ProgramShaderCache::CurrentVShader = 0, ProgramShaderCache::CurrentProgram = 0; - ProgramShaderCache::PCache ProgramShaderCache::pshaders; - GLuint ProgramShaderCache::UBOBuffers[2]; + GLuint ProgramShaderCache::CurrentFShader = 0, ProgramShaderCache::CurrentVShader = 0, ProgramShaderCache::CurrentProgram = 0; + ProgramShaderCache::PCache ProgramShaderCache::pshaders; + GLuint ProgramShaderCache::UBOBuffers[2]; - std::pair ProgramShaderCache::CurrentShaderProgram; - const char *UniformNames[NUM_UNIFORMS] = { - // SAMPLERS - "samp0","samp1","samp2","samp3","samp4","samp5","samp6","samp7", - // PIXEL SHADER UNIFORMS - I_COLORS, - I_KCOLORS, - I_ALPHA, - I_TEXDIMS, - I_ZBIAS , - I_INDTEXSCALE , - I_INDTEXMTX, - I_FOG, - I_PLIGHTS, - I_PMATERIALS, - // VERTEX SHADER UNIFORMS - I_POSNORMALMATRIX, - I_PROJECTION , - I_MATERIALS, - I_LIGHTS, - I_TEXMATRICES, - I_TRANSFORMMATRICES , - I_NORMALMATRICES , - I_POSTTRANSFORMMATRICES, - I_DEPTHPARAMS, - }; + std::pair ProgramShaderCache::CurrentShaderProgram; + const char *UniformNames[NUM_UNIFORMS] = { + // SAMPLERS + "samp0","samp1","samp2","samp3","samp4","samp5","samp6","samp7", + // PIXEL SHADER UNIFORMS + I_COLORS, + I_KCOLORS, + I_ALPHA, + I_TEXDIMS, + I_ZBIAS , + I_INDTEXSCALE , + I_INDTEXMTX, + I_FOG, + I_PLIGHTS, + I_PMATERIALS, + // VERTEX SHADER UNIFORMS + I_POSNORMALMATRIX, + I_PROJECTION , + I_MATERIALS, + I_LIGHTS, + I_TEXMATRICES, + I_TRANSFORMMATRICES , + I_NORMALMATRICES , + I_POSTTRANSFORMMATRICES, + I_DEPTHPARAMS, + }; - void ProgramShaderCache::SetBothShaders(GLuint PS, GLuint VS) - { - PROGRAMUID uid; - CurrentFShader = PS; - CurrentVShader = VS; + void ProgramShaderCache::SetBothShaders(GLuint PS, GLuint VS) + { + PROGRAMUID uid; + CurrentFShader = PS; + CurrentVShader = VS; - GetProgramShaderId(&uid, CurrentVShader, CurrentFShader); + GetProgramShaderId(&uid, CurrentVShader, CurrentFShader); - if(uid.uid.id == 0) - { - CurrentProgram = 0; - glUseProgram(0); - return; - } + if(uid.uid.id == 0) + { + CurrentProgram = 0; + glUseProgram(0); + return; + } - // Fragment shaders can survive without Vertex Shaders - // We have a valid fragment shader, let's create our program - std::pair ShaderPair = std::make_pair(uid.uid.psid, uid.uid.vsid); - PCache::iterator iter = pshaders.find(ShaderPair); - if (iter != pshaders.end()) - { - PCacheEntry &entry = iter->second; - glUseProgram(entry.program.glprogid); - CurrentShaderProgram = ShaderPair; - CurrentProgram = entry.program.glprogid; - return; - } - PCacheEntry entry; - entry.program.vsid = CurrentVShader; - entry.program.psid = CurrentFShader; - entry.program.glprogid = glCreateProgram(); + // Fragment shaders can survive without Vertex Shaders + // We have a valid fragment shader, let's create our program + std::pair ShaderPair = std::make_pair(uid.uid.psid, uid.uid.vsid); + PCache::iterator iter = pshaders.find(ShaderPair); + if (iter != pshaders.end()) + { + PCacheEntry &entry = iter->second; + glUseProgram(entry.program.glprogid); + CurrentShaderProgram = ShaderPair; + CurrentProgram = entry.program.glprogid; + return; + } + PCacheEntry entry; + entry.program.vsid = CurrentVShader; + entry.program.psid = CurrentFShader; + entry.program.glprogid = glCreateProgram(); - // Right, the program is created now - // Let's attach everything - if(entry.program.vsid != 0) // attaching zero vertex shader makes it freak out - glAttachShader(entry.program.glprogid, entry.program.vsid); - - glAttachShader(entry.program.glprogid, entry.program.psid); - - glLinkProgram(entry.program.glprogid); - - glUseProgram(entry.program.glprogid); + // Right, the program is created now + // Let's attach everything + if(entry.program.vsid != 0) // attaching zero vertex shader makes it freak out + glAttachShader(entry.program.glprogid, entry.program.vsid); + + glAttachShader(entry.program.glprogid, entry.program.psid); + + glLinkProgram(entry.program.glprogid); + + glUseProgram(entry.program.glprogid); - // Dunno why this is needed when I have the binding - // points statically set in the shader source - // We should only need these two functions when we don't support binding but do support UBO - // Driver Bug? Nvidia GTX 570, 290.xx Driver, Linux x64 - //if(!g_ActiveConfig.backend_info.bSupportsGLSLBinding) - { + // Dunno why this is needed when I have the binding + // points statically set in the shader source + // We should only need these two functions when we don't support binding but do support UBO + // Driver Bug? Nvidia GTX 570, 290.xx Driver, Linux x64 + //if(!g_ActiveConfig.backend_info.bSupportsGLSLBinding) + { glUniformBlockBinding( entry.program.glprogid, 0, 1 ); glUniformBlockBinding( entry.program.glprogid, 1, 2 ); } - // We cache our uniform locations for now - // Once we move up to a newer version of GLSL, ~1.30 - // We can remove this - - //For some reason this fails on my hardware - //glGetUniformIndices(entry.program.glprogid, NUM_UNIFORMS, UniformNames, entry.program.UniformLocations); - //Got to do it this crappy way. - if(!g_ActiveConfig.backend_info.bSupportsGLSLUBO) + // We cache our uniform locations for now + // Once we move up to a newer version of GLSL, ~1.30 + // We can remove this + + //For some reason this fails on my hardware + //glGetUniformIndices(entry.program.glprogid, NUM_UNIFORMS, UniformNames, entry.program.UniformLocations); + //Got to do it this crappy way. + if(!g_ActiveConfig.backend_info.bSupportsGLSLUBO) for(int a = 0; a < NUM_UNIFORMS; ++a) entry.program.UniformLocations[a] = glGetUniformLocation(entry.program.glprogid, UniformNames[a]); - // Need to get some attribute locations - if(uid.uid.vsid != 0) // We have no vertex Shader - { - entry.program.attrLoc[0] = glGetAttribLocation(entry.program.glprogid, "rawnorm1"); - entry.program.attrLoc[1] = glGetAttribLocation(entry.program.glprogid, "rawnorm2"); - entry.program.attrLoc[2] = glGetAttribLocation(entry.program.glprogid, "fposmtx"); - if(entry.program.attrLoc[0] > 0) - glEnableVertexAttribArray(entry.program.attrLoc[0]); - if(entry.program.attrLoc[1] > 0) - glEnableVertexAttribArray(entry.program.attrLoc[1]); - if(entry.program.attrLoc[2] > 0) - glEnableVertexAttribArray(entry.program.attrLoc[2]); - } - else - entry.program.attrLoc[0] = entry.program.attrLoc[1] = entry.program.attrLoc[2] = 0; + // Need to get some attribute locations + if(uid.uid.vsid != 0) // We have no vertex Shader + { + entry.program.attrLoc[0] = glGetAttribLocation(entry.program.glprogid, "rawnorm1"); + entry.program.attrLoc[1] = glGetAttribLocation(entry.program.glprogid, "rawnorm2"); + entry.program.attrLoc[2] = glGetAttribLocation(entry.program.glprogid, "fposmtx"); + if(entry.program.attrLoc[0] > 0) + glEnableVertexAttribArray(entry.program.attrLoc[0]); + if(entry.program.attrLoc[1] > 0) + glEnableVertexAttribArray(entry.program.attrLoc[1]); + if(entry.program.attrLoc[2] > 0) + glEnableVertexAttribArray(entry.program.attrLoc[2]); + } + else + entry.program.attrLoc[0] = entry.program.attrLoc[1] = entry.program.attrLoc[2] = 0; - pshaders[ShaderPair] = entry; - CurrentShaderProgram = ShaderPair; - CurrentProgram = entry.program.glprogid; - } + pshaders[ShaderPair] = entry; + CurrentShaderProgram = ShaderPair; + CurrentProgram = entry.program.glprogid; + } void ProgramShaderCache::SetUniformObjects(int Buffer, unsigned int offset, const float *f, unsigned int count) { assert(Buffer > 1); @@ -154,16 +154,16 @@ namespace OGL glBufferSubData(GL_UNIFORM_BUFFER, offset * 4 * 4, count * 4 * 4, f); } - GLuint ProgramShaderCache::GetCurrentProgram(void) { return CurrentProgram; } + GLuint ProgramShaderCache::GetCurrentProgram(void) { return CurrentProgram; } - GLint ProgramShaderCache::GetAttr(int num) - { - return pshaders[CurrentShaderProgram].program.attrLoc[num]; - } - PROGRAMSHADER ProgramShaderCache::GetShaderProgram(void) - { - return pshaders[CurrentShaderProgram].program; - } + GLint ProgramShaderCache::GetAttr(int num) + { + return pshaders[CurrentShaderProgram].program.attrLoc[num]; + } + PROGRAMSHADER ProgramShaderCache::GetShaderProgram(void) + { + return pshaders[CurrentShaderProgram].program; + } void ProgramShaderCache::Init(void) { // We have to get the UBO alignment here because @@ -188,18 +188,18 @@ namespace OGL glBufferData(GL_UNIFORM_BUFFER, ROUND_UP(C_VENVCONST_END * 4 * 4, Align), NULL, GL_DYNAMIC_DRAW); glBindBufferBase(GL_UNIFORM_BUFFER, 2, UBOBuffers[1]); } - void ProgramShaderCache::Shutdown(void) - { - PCache::iterator iter = pshaders.begin(); - for (; iter != pshaders.end(); iter++) - iter->second.Destroy(); - pshaders.clear(); - glDeleteBuffers(2, UBOBuffers); - } + void ProgramShaderCache::Shutdown(void) + { + PCache::iterator iter = pshaders.begin(); + for (; iter != pshaders.end(); iter++) + iter->second.Destroy(); + pshaders.clear(); + glDeleteBuffers(2, UBOBuffers); + } } void GetProgramShaderId(PROGRAMUID *uid, GLuint _v, GLuint _p) { - uid->uid.vsid = _v; - uid->uid.psid = _p; + uid->uid.vsid = _v; + uid->uid.psid = _p; } diff --git a/Source/Plugins/Plugin_VideoOGL/Src/ProgramShaderCache.h b/Source/Plugins/Plugin_VideoOGL/Src/ProgramShaderCache.h index a5ff097af6..1c114551b8 100644 --- a/Source/Plugins/Plugin_VideoOGL/Src/ProgramShaderCache.h +++ b/Source/Plugins/Plugin_VideoOGL/Src/ProgramShaderCache.h @@ -25,39 +25,39 @@ #include "PixelShaderGen.h" #include "VertexShaderGen.h" - union PID - { - struct { - GLuint vsid, psid; - }; - u64 id; - }; + union PID + { + struct { + GLuint vsid, psid; + }; + u64 id; + }; class PROGRAMUID { public: - PID uid; + PID uid; - PROGRAMUID() - { - uid.id = 0; - } + PROGRAMUID() + { + uid.id = 0; + } - PROGRAMUID(const PROGRAMUID& r) - { - uid.id = r.uid.id; - } - PROGRAMUID(GLuint _v, GLuint _p) - { - uid.vsid = _v; - uid.psid = _p; - } + PROGRAMUID(const PROGRAMUID& r) + { + uid.id = r.uid.id; + } + PROGRAMUID(GLuint _v, GLuint _p) + { + uid.vsid = _v; + uid.psid = _p; + } - int GetNumValues() const - { - return uid.id; - } + int GetNumValues() const + { + return uid.id; + } }; void GetProgramShaderId(PROGRAMUID *uid, GLuint _v, GLuint _p); @@ -69,43 +69,43 @@ extern const char *UniformNames[NUM_UNIFORMS]; struct PROGRAMSHADER { - PROGRAMSHADER() : glprogid(0), vsid(0), psid(0){} - GLuint glprogid; // opengl program id - GLuint vsid, psid; - GLint attrLoc[3]; - GLint UniformLocations[NUM_UNIFORMS]; + PROGRAMSHADER() : glprogid(0), vsid(0), psid(0){} + GLuint glprogid; // opengl program id + GLuint vsid, psid; + GLint attrLoc[3]; + GLint UniformLocations[NUM_UNIFORMS]; }; class ProgramShaderCache { - struct PCacheEntry - { - PROGRAMSHADER program; - int frameCount; - PCacheEntry() : frameCount(0) {} - void Destroy() { - glDeleteProgram(program.glprogid); - program.glprogid = 0; - } - }; - typedef std::map, PCacheEntry> PCache; + struct PCacheEntry + { + PROGRAMSHADER program; + int frameCount; + PCacheEntry() : frameCount(0) {} + void Destroy() { + glDeleteProgram(program.glprogid); + program.glprogid = 0; + } + }; + typedef std::map, PCacheEntry> PCache; - static PCache pshaders; - static GLuint CurrentFShader, CurrentVShader, CurrentProgram; - static std::pair CurrentShaderProgram; + static PCache pshaders; + static GLuint CurrentFShader, CurrentVShader, CurrentProgram; + static std::pair CurrentShaderProgram; - // For UBOS - static GLuint UBOBuffers[2]; // PS is 0, VS is 1 + // For UBOS + static GLuint UBOBuffers[2]; // PS is 0, VS is 1 public: - static PROGRAMSHADER GetShaderProgram(void); - static GLint GetAttr(int num); - static void SetBothShaders(GLuint PS, GLuint VS); - static GLuint GetCurrentProgram(void); - static void SetUniformObjects(int Buffer, unsigned int offset, const float *f, unsigned int count = 1); - - static void Init(void); - static void Shutdown(void); + static PROGRAMSHADER GetShaderProgram(void); + static GLint GetAttr(int num); + static void SetBothShaders(GLuint PS, GLuint VS); + static GLuint GetCurrentProgram(void); + static void SetUniformObjects(int Buffer, unsigned int offset, const float *f, unsigned int count = 1); + + static void Init(void); + static void Shutdown(void); }; diff --git a/Source/Plugins/Plugin_VideoOGL/Src/TextureCache.cpp b/Source/Plugins/Plugin_VideoOGL/Src/TextureCache.cpp index d3a1283627..2fc02d867a 100644 --- a/Source/Plugins/Plugin_VideoOGL/Src/TextureCache.cpp +++ b/Source/Plugins/Plugin_VideoOGL/Src/TextureCache.cpp @@ -208,7 +208,7 @@ void TextureCache::TCacheEntry::Load(unsigned int width, unsigned int height, if (pcfmt != PC_TEX_FMT_DXT1) { if (expanded_width != width) - glPixelStorei(GL_UNPACK_ROW_LENGTH, expanded_width); + glPixelStorei(GL_UNPACK_ROW_LENGTH, expanded_width); if (bHaveMipMaps && autogen_mips) { @@ -222,7 +222,7 @@ void TextureCache::TCacheEntry::Load(unsigned int width, unsigned int height, } if (expanded_width != width) - glPixelStorei(GL_UNPACK_ROW_LENGTH, 0); + glPixelStorei(GL_UNPACK_ROW_LENGTH, 0); } else { @@ -385,8 +385,8 @@ TextureCache::~TextureCache() { if (s_TempFramebuffer) { - glDeleteFramebuffersEXT(1, (GLuint*)&s_TempFramebuffer); - s_TempFramebuffer = 0; + glDeleteFramebuffersEXT(1, (GLuint*)&s_TempFramebuffer); + s_TempFramebuffer = 0; } } diff --git a/Source/Plugins/Plugin_VideoOGL/Src/TextureConverter.cpp b/Source/Plugins/Plugin_VideoOGL/Src/TextureConverter.cpp index f89ca38b05..ff4ecab624 100644 --- a/Source/Plugins/Plugin_VideoOGL/Src/TextureConverter.cpp +++ b/Source/Plugins/Plugin_VideoOGL/Src/TextureConverter.cpp @@ -407,7 +407,7 @@ void EncodeToRam(u32 address, bool bFromZBuffer, bool bIsIntensityFmt, u32 copyf scaledSource.right = expandedWidth / samples; int cacheBytes = 32; if ((format & 0x0f) == 6) - cacheBytes = 64; + cacheBytes = 64; int readStride = (expandedWidth * cacheBytes) / TexDecoder_GetBlockWidthInTexels(format); g_renderer->ResetAPIState(); diff --git a/Source/Plugins/Plugin_VideoOGL/Src/VertexShaderCache.cpp b/Source/Plugins/Plugin_VideoOGL/Src/VertexShaderCache.cpp index 97dfc9a3bb..b665aeae98 100644 --- a/Source/Plugins/Plugin_VideoOGL/Src/VertexShaderCache.cpp +++ b/Source/Plugins/Plugin_VideoOGL/Src/VertexShaderCache.cpp @@ -61,20 +61,20 @@ void VertexShaderCache::Init() if(g_ActiveConfig.bUseGLSL) { - pSetVSConstant4f = SetGLSLVSConstant4f; - pSetVSConstant4fv = SetGLSLVSConstant4fv; - pSetMultiVSConstant4fv = SetMultiGLSLVSConstant4fv; - pSetMultiVSConstant3fv = SetMultiGLSLVSConstant3fv; - pCompileVertexShader = CompileGLSLVertexShader; + pSetVSConstant4f = SetGLSLVSConstant4f; + pSetVSConstant4fv = SetGLSLVSConstant4fv; + pSetMultiVSConstant4fv = SetMultiGLSLVSConstant4fv; + pSetMultiVSConstant3fv = SetMultiGLSLVSConstant3fv; + pCompileVertexShader = CompileGLSLVertexShader; } else { - pSetVSConstant4f = SetCGVSConstant4f; - pSetVSConstant4fv = SetCGVSConstant4fv; - pSetMultiVSConstant4fv = SetMultiCGVSConstant4fv; - pSetMultiVSConstant3fv = SetMultiCGVSConstant3fv; - pCompileVertexShader = CompileCGVertexShader; - glEnable(GL_VERTEX_PROGRAM_ARB); + pSetVSConstant4f = SetCGVSConstant4f; + pSetVSConstant4fv = SetCGVSConstant4fv; + pSetMultiVSConstant4fv = SetMultiCGVSConstant4fv; + pSetMultiVSConstant3fv = SetMultiCGVSConstant3fv; + pCompileVertexShader = CompileCGVertexShader; + glEnable(GL_VERTEX_PROGRAM_ARB); } glGetProgramivARB(GL_VERTEX_PROGRAM_ARB, GL_MAX_PROGRAM_NATIVE_INSTRUCTIONS_ARB, (GLint *)&s_nMaxVertexInstructions); @@ -82,7 +82,7 @@ void VertexShaderCache::Init() #if CG_VERSION_NUM == 2100 if (strstr((const char*)glGetString(GL_VENDOR), "ATI") != NULL) { - s_nMaxVertexInstructions = 4096; + s_nMaxVertexInstructions = 4096; } #endif } @@ -90,7 +90,7 @@ void VertexShaderCache::Init() void VertexShaderCache::Shutdown() { for (VSCache::iterator iter = vshaders.begin(); iter != vshaders.end(); ++iter) - iter->second.Destroy(); + iter->second.Destroy(); vshaders.clear(); } @@ -101,12 +101,12 @@ VERTEXSHADER* VertexShaderCache::SetShader(u32 components) GetVertexShaderId(&uid, components); if (last_entry) { - if (uid == last_uid) - { - GFX_DEBUGGER_PAUSE_AT(NEXT_VERTEX_SHADER_CHANGE, true); - ValidateVertexShaderIDs(API_OPENGL, vshaders[uid].safe_uid, vshaders[uid].shader.strprog, components); - return &last_entry->shader; - } + if (uid == last_uid) + { + GFX_DEBUGGER_PAUSE_AT(NEXT_VERTEX_SHADER_CHANGE, true); + ValidateVertexShaderIDs(API_OPENGL, vshaders[uid].safe_uid, vshaders[uid].shader.strprog, components); + return &last_entry->shader; + } } last_uid = uid; @@ -114,12 +114,12 @@ VERTEXSHADER* VertexShaderCache::SetShader(u32 components) VSCache::iterator iter = vshaders.find(uid); if (iter != vshaders.end()) { - VSCacheEntry &entry = iter->second; - last_entry = &entry; + VSCacheEntry &entry = iter->second; + last_entry = &entry; - GFX_DEBUGGER_PAUSE_AT(NEXT_VERTEX_SHADER_CHANGE, true); - ValidateVertexShaderIDs(API_OPENGL, entry.safe_uid, entry.shader.strprog, components); - return &last_entry->shader; + GFX_DEBUGGER_PAUSE_AT(NEXT_VERTEX_SHADER_CHANGE, true); + ValidateVertexShaderIDs(API_OPENGL, entry.safe_uid, entry.shader.strprog, components); + return &last_entry->shader; } // Make an entry in the table @@ -130,17 +130,17 @@ VERTEXSHADER* VertexShaderCache::SetShader(u32 components) #if defined(_DEBUG) || defined(DEBUGFAST) if (g_ActiveConfig.iLog & CONF_SAVESHADERS && code) { - static int counter = 0; - char szTemp[MAX_PATH]; - sprintf(szTemp, "%svs_%04i.txt", File::GetUserPath(D_DUMP_IDX).c_str(), counter++); + static int counter = 0; + char szTemp[MAX_PATH]; + sprintf(szTemp, "%svs_%04i.txt", File::GetUserPath(D_DUMP_IDX).c_str(), counter++); - SaveData(szTemp, code); + SaveData(szTemp, code); } #endif if (!code || !VertexShaderCache::CompileVertexShader(entry.shader, code)) { - GFX_DEBUGGER_PAUSE_AT(NEXT_ERROR, true); - return NULL; + GFX_DEBUGGER_PAUSE_AT(NEXT_ERROR, true); + return NULL; } INCSTAT(stats.numVertexShadersCreated); @@ -160,8 +160,8 @@ void VertexShaderCache::DisableShader() assert(true); if (ShaderEnabled) { - glDisable(GL_VERTEX_PROGRAM_ARB); - ShaderEnabled = false; + glDisable(GL_VERTEX_PROGRAM_ARB); + ShaderEnabled = false; } } @@ -172,14 +172,14 @@ void VertexShaderCache::SetCurrentShader(GLuint Shader) assert(true); if (!ShaderEnabled) { - glEnable(GL_VERTEX_PROGRAM_ARB); - ShaderEnabled= true; + glEnable(GL_VERTEX_PROGRAM_ARB); + ShaderEnabled= true; } if (CurrentShader != Shader) { - if(Shader != 0) - CurrentShader = Shader; - glBindProgramARB(GL_VERTEX_PROGRAM_ARB, CurrentShader); + if(Shader != 0) + CurrentShader = Shader; + glBindProgramARB(GL_VERTEX_PROGRAM_ARB, CurrentShader); } } // GLSL Specific @@ -227,16 +227,16 @@ void SetVSConstant4fvByName(const char * name, unsigned int offset, const float { PROGRAMSHADER tmp = ProgramShaderCache::GetShaderProgram(); for(int a = 0; a < NUM_UNIFORMS; ++a) - if(!strcmp(name, UniformNames[a])) - { - if(tmp.UniformLocations[a] == -1) - return; - else - { - glUniform4fv(tmp.UniformLocations[a] + offset, count, f); - return; - } - } + if(!strcmp(name, UniformNames[a])) + { + if(tmp.UniformLocations[a] == -1) + return; + else + { + glUniform4fv(tmp.UniformLocations[a] + offset, count, f); + return; + } + } } void SetGLSLVSConstant4f(unsigned int const_number, float f1, float f2, float f3, float f4) { @@ -252,12 +252,12 @@ void SetGLSLVSConstant4f(unsigned int const_number, float f1, float f2, float f3 } for( unsigned int a = 0; a < 9; ++a) { - if( const_number >= VSVar_Loc[a].reg && const_number < ( VSVar_Loc[a].reg + VSVar_Loc[a].size)) - { - unsigned int offset = const_number - VSVar_Loc[a].reg; - SetVSConstant4fvByName(VSVar_Loc[a].name, offset, buf); - return; - } + if( const_number >= VSVar_Loc[a].reg && const_number < ( VSVar_Loc[a].reg + VSVar_Loc[a].size)) + { + unsigned int offset = const_number - VSVar_Loc[a].reg; + SetVSConstant4fvByName(VSVar_Loc[a].name, offset, buf); + return; + } } } @@ -270,12 +270,12 @@ void SetGLSLVSConstant4fv(unsigned int const_number, const float *f) } for( unsigned int a = 0; a < 9; ++a) { - if( const_number >= VSVar_Loc[a].reg && const_number < ( VSVar_Loc[a].reg + VSVar_Loc[a].size)) - { - unsigned int offset = const_number - VSVar_Loc[a].reg; - SetVSConstant4fvByName(VSVar_Loc[a].name, offset, f); - return; - } + if( const_number >= VSVar_Loc[a].reg && const_number < ( VSVar_Loc[a].reg + VSVar_Loc[a].size)) + { + unsigned int offset = const_number - VSVar_Loc[a].reg; + SetVSConstant4fvByName(VSVar_Loc[a].name, offset, f); + return; + } } } @@ -288,12 +288,12 @@ void SetMultiGLSLVSConstant4fv(unsigned int const_number, unsigned int count, co } for( unsigned int a = 0; a < 9; ++a) { - if( const_number >= VSVar_Loc[a].reg && const_number < ( VSVar_Loc[a].reg + VSVar_Loc[a].size)) - { - unsigned int offset = const_number - VSVar_Loc[a].reg; - SetVSConstant4fvByName(VSVar_Loc[a].name, offset, f, count); - return; - } + if( const_number >= VSVar_Loc[a].reg && const_number < ( VSVar_Loc[a].reg + VSVar_Loc[a].size)) + { + unsigned int offset = const_number - VSVar_Loc[a].reg; + SetVSConstant4fvByName(VSVar_Loc[a].name, offset, f, count); + return; + } } } @@ -302,10 +302,10 @@ void SetMultiGLSLVSConstant3fv(unsigned int const_number, unsigned int count, co float buf[4 * C_VENVCONST_END]; for (unsigned int i = 0; i < count; i++) { - buf[4*i ] = *f++; - buf[4*i+1] = *f++; - buf[4*i+2] = *f++; - buf[4*i+3] = 0.f; + buf[4*i ] = *f++; + buf[4*i+1] = *f++; + buf[4*i+2] = *f++; + buf[4*i+3] = 0.f; } if (g_ActiveConfig.backend_info.bSupportsGLSLUBO) { @@ -314,12 +314,12 @@ void SetMultiGLSLVSConstant3fv(unsigned int const_number, unsigned int count, co } for( unsigned int a = 0; a < 9; ++a) { - if( const_number >= VSVar_Loc[a].reg && const_number < ( VSVar_Loc[a].reg + VSVar_Loc[a].size)) - { - unsigned int offset = const_number - VSVar_Loc[a].reg; - SetVSConstant4fvByName(VSVar_Loc[a].name, offset, buf, count); - return; - } + if( const_number >= VSVar_Loc[a].reg && const_number < ( VSVar_Loc[a].reg + VSVar_Loc[a].size)) + { + unsigned int offset = const_number - VSVar_Loc[a].reg; + SetVSConstant4fvByName(VSVar_Loc[a].name, offset, buf, count); + return; + } } } @@ -330,34 +330,34 @@ bool CompileCGVertexShader(VERTEXSHADER& vs, const char* pstrprogram) GLenum err = GL_REPORT_ERROR(); if (err != GL_NO_ERROR) { - ERROR_LOG(VIDEO, "glError %08x before VS!", err); + ERROR_LOG(VIDEO, "glError %08x before VS!", err); } #if defined HAVE_CG && HAVE_CG CGprogram tempprog = cgCreateProgram(g_cgcontext, CG_SOURCE, pstrprogram, g_cgvProf, "main", NULL); if (!cgIsProgram(tempprog)) { - static int num_failures = 0; - char szTemp[MAX_PATH]; - sprintf(szTemp, "bad_vs_%04i.txt", num_failures++); - std::ofstream file(szTemp); - file << pstrprogram; - file.close(); + static int num_failures = 0; + char szTemp[MAX_PATH]; + sprintf(szTemp, "bad_vs_%04i.txt", num_failures++); + std::ofstream file(szTemp); + file << pstrprogram; + file.close(); - PanicAlert("Failed to compile vertex shader %d!\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 (%d):\n%s", - num_failures - 1, szTemp, - g_cgfProf, - cgGetLastListing(g_cgcontext)); + PanicAlert("Failed to compile vertex shader %d!\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 (%d):\n%s", + num_failures - 1, szTemp, + g_cgfProf, + cgGetLastListing(g_cgcontext)); - cgDestroyProgram(tempprog); - ERROR_LOG(VIDEO, "Failed to load vs %s:", cgGetLastListing(g_cgcontext)); - ERROR_LOG(VIDEO, "%s", pstrprogram); - return false; + cgDestroyProgram(tempprog); + ERROR_LOG(VIDEO, "Failed to load vs %s:", cgGetLastListing(g_cgcontext)); + ERROR_LOG(VIDEO, "%s", pstrprogram); + return false; } if (cgGetError() != CG_NO_ERROR) { - WARN_LOG(VIDEO, "Failed to load vs %s:", cgGetLastListing(g_cgcontext)); - WARN_LOG(VIDEO, "%s", pstrprogram); + WARN_LOG(VIDEO, "Failed to load vs %s:", cgGetLastListing(g_cgcontext)); + WARN_LOG(VIDEO, "%s", pstrprogram); } // This looks evil - we modify the program through the const char * we got from cgGetProgramString! @@ -365,9 +365,9 @@ bool CompileCGVertexShader(VERTEXSHADER& vs, const char* pstrprogram) char *pcompiledprog = (char*)cgGetProgramString(tempprog, CG_COMPILED_PROGRAM); char *plocal = strstr(pcompiledprog, "program.local"); while (plocal != NULL) { - const char* penv = " program.env"; - memcpy(plocal, penv, 13); - plocal = strstr(plocal + 13, "program.local"); + const char* penv = " program.env"; + memcpy(plocal, penv, 13); + plocal = strstr(plocal + 13, "program.local"); } glGenProgramsARB(1, &vs.glprogid); vs.bGLSL = false; @@ -376,15 +376,15 @@ bool CompileCGVertexShader(VERTEXSHADER& vs, const char* pstrprogram) glProgramStringARB(GL_VERTEX_PROGRAM_ARB, GL_PROGRAM_FORMAT_ASCII_ARB, (GLsizei)strlen(pcompiledprog), pcompiledprog); err = GL_REPORT_ERROR(); if (err != GL_NO_ERROR) { - ERROR_LOG(VIDEO, "%s", pstrprogram); - ERROR_LOG(VIDEO, "%s", pcompiledprog); + ERROR_LOG(VIDEO, "%s", pstrprogram); + ERROR_LOG(VIDEO, "%s", pcompiledprog); } cgDestroyProgram(tempprog); #endif if (g_ActiveConfig.bEnableShaderDebugging) - vs.strprog = pstrprogram; + vs.strprog = pstrprogram; return true; } @@ -402,12 +402,12 @@ void SetMultiCGVSConstant4fv(unsigned int const_number, unsigned int count, cons { if(GLEW_EXT_gpu_program_parameters) { - glProgramEnvParameters4fvEXT(GL_VERTEX_PROGRAM_ARB, const_number, count, f); + glProgramEnvParameters4fvEXT(GL_VERTEX_PROGRAM_ARB, const_number, count, f); } else { - for (unsigned int i = 0; i < count; i++,f+=4) - glProgramEnvParameter4fvARB(GL_VERTEX_PROGRAM_ARB, const_number + i, f); + for (unsigned int i = 0; i < count; i++,f+=4) + glProgramEnvParameter4fvARB(GL_VERTEX_PROGRAM_ARB, const_number + i, f); } } @@ -415,49 +415,49 @@ void SetMultiCGVSConstant3fv(unsigned int const_number, unsigned int count, cons { if(GLEW_EXT_gpu_program_parameters) { - float buf[4 * C_VENVCONST_END]; - for (unsigned int i = 0; i < count; i++) - { - buf[4*i ] = *f++; - buf[4*i+1] = *f++; - buf[4*i+2] = *f++; - buf[4*i+3] = 0.f; - } - glProgramEnvParameters4fvEXT(GL_VERTEX_PROGRAM_ARB, const_number, count, buf); + float buf[4 * C_VENVCONST_END]; + for (unsigned int i = 0; i < count; i++) + { + buf[4*i ] = *f++; + buf[4*i+1] = *f++; + buf[4*i+2] = *f++; + buf[4*i+3] = 0.f; + } + glProgramEnvParameters4fvEXT(GL_VERTEX_PROGRAM_ARB, const_number, count, buf); } else { - for (unsigned int i = 0; i < count; i++) - { - float buf[4]; - buf[0] = *f++; - buf[1] = *f++; - buf[2] = *f++; - buf[3] = 0.f; - glProgramEnvParameter4fvARB(GL_VERTEX_PROGRAM_ARB, const_number + i, buf); - } + for (unsigned int i = 0; i < count; i++) + { + float buf[4]; + buf[0] = *f++; + buf[1] = *f++; + buf[2] = *f++; + buf[3] = 0.f; + glProgramEnvParameter4fvARB(GL_VERTEX_PROGRAM_ARB, const_number + i, buf); + } } } // Renderer Functions void Renderer::SetVSConstant4f(unsigned int const_number, float f1, float f2, float f3, float f4) { - pSetVSConstant4f(const_number, f1, f2, f3, f4); + pSetVSConstant4f(const_number, f1, f2, f3, f4); } void Renderer::SetVSConstant4fv(unsigned int const_number, const float *f) { - pSetVSConstant4fv(const_number, f); + pSetVSConstant4fv(const_number, f); } void Renderer::SetMultiVSConstant4fv(unsigned int const_number, unsigned int count, const float *f) { - pSetMultiVSConstant4fv(const_number, count, f); + pSetMultiVSConstant4fv(const_number, count, f); } void Renderer::SetMultiVSConstant3fv(unsigned int const_number, unsigned int count, const float *f) { - pSetMultiVSConstant3fv(const_number, count, f); + pSetMultiVSConstant3fv(const_number, count, f); } diff --git a/Source/Plugins/Plugin_VideoOGL/Src/main.cpp b/Source/Plugins/Plugin_VideoOGL/Src/main.cpp index c4e2a8f0ea..8b25d5c47c 100644 --- a/Source/Plugins/Plugin_VideoOGL/Src/main.cpp +++ b/Source/Plugins/Plugin_VideoOGL/Src/main.cpp @@ -109,23 +109,23 @@ std::string VideoBackend::GetName() void GetShaders(std::vector &shaders) { - shaders.clear(); - if (File::IsDirectory(File::GetUserPath(D_SHADERS_IDX))) - { - File::FSTEntry entry; - File::ScanDirectoryTree(File::GetUserPath(D_SHADERS_IDX), entry); - for (u32 i = 0; i < entry.children.size(); i++) - { - std::string name = entry.children[i].virtualName.c_str(); - if (!strcasecmp(name.substr(name.size() - 4).c_str(), ".txt")) - name = name.substr(0, name.size() - 4); - shaders.push_back(name); - } - } - else - { - File::CreateDir(File::GetUserPath(D_SHADERS_IDX).c_str()); - } + shaders.clear(); + if (File::IsDirectory(File::GetUserPath(D_SHADERS_IDX))) + { + File::FSTEntry entry; + File::ScanDirectoryTree(File::GetUserPath(D_SHADERS_IDX), entry); + for (u32 i = 0; i < entry.children.size(); i++) + { + std::string name = entry.children[i].virtualName.c_str(); + if (!strcasecmp(name.substr(name.size() - 4).c_str(), ".txt")) + name = name.substr(0, name.size() - 4); + shaders.push_back(name); + } + } + else + { + File::CreateDir(File::GetUserPath(D_SHADERS_IDX).c_str()); + } } void InitBackendInfo() From df283a56a0bc09a455e50ada1aa7e9116a276b44 Mon Sep 17 00:00:00 2001 From: Pierre Bourdon Date: Sun, 11 Dec 2011 11:14:02 +0100 Subject: [PATCH 36/83] More coding style fixes because I suck at sed --- Source/Core/VideoCommon/Src/PixelShaderGen.h | 20 +- .../Src/TextureConversionShader.cpp | 2 +- Source/Core/VideoCommon/Src/VertexShaderGen.h | 18 +- .../Plugin_VideoOGL/Src/PixelShaderCache.cpp | 18 +- .../Src/ProgramShaderCache.cpp | 198 +++++++++--------- .../Plugin_VideoOGL/Src/ProgramShaderCache.h | 32 +-- .../Plugin_VideoOGL/Src/TextureConverter.cpp | 2 +- Source/Plugins/Plugin_VideoOGL/Src/main.cpp | 20 +- 8 files changed, 155 insertions(+), 155 deletions(-) diff --git a/Source/Core/VideoCommon/Src/PixelShaderGen.h b/Source/Core/VideoCommon/Src/PixelShaderGen.h index 3228d50b99..1a70a3664b 100644 --- a/Source/Core/VideoCommon/Src/PixelShaderGen.h +++ b/Source/Core/VideoCommon/Src/PixelShaderGen.h @@ -49,16 +49,16 @@ // Annoying sure, can be removed once we get up to GLSL ~1.3 const s_svar PSVar_Loc[] = { {I_COLORS, C_COLORS, 4 }, - {I_KCOLORS, C_KCOLORS, 4 }, - {I_ALPHA, C_ALPHA, 1 }, - {I_TEXDIMS, C_TEXDIMS, 8 }, - {I_ZBIAS , C_ZBIAS, 2 }, - {I_INDTEXSCALE , C_INDTEXSCALE, 2 }, - {I_INDTEXMTX, C_INDTEXMTX, 6 }, - {I_FOG, C_FOG, 3 }, - {I_PLIGHTS, C_PLIGHTS, 40 }, - {I_PMATERIALS, C_PMATERIALS, 4 }, - }; + {I_KCOLORS, C_KCOLORS, 4 }, + {I_ALPHA, C_ALPHA, 1 }, + {I_TEXDIMS, C_TEXDIMS, 8 }, + {I_ZBIAS , C_ZBIAS, 2 }, + {I_INDTEXSCALE , C_INDTEXSCALE, 2 }, + {I_INDTEXMTX, C_INDTEXMTX, 6 }, + {I_FOG, C_FOG, 3 }, + {I_PLIGHTS, C_PLIGHTS, 40 }, + {I_PMATERIALS, C_PMATERIALS, 4 }, + }; // DO NOT make anything in this class virtual. template diff --git a/Source/Core/VideoCommon/Src/TextureConversionShader.cpp b/Source/Core/VideoCommon/Src/TextureConversionShader.cpp index 2f21c816b7..2d3b642593 100644 --- a/Source/Core/VideoCommon/Src/TextureConversionShader.cpp +++ b/Source/Core/VideoCommon/Src/TextureConversionShader.cpp @@ -69,7 +69,7 @@ u16 GetEncodedSampleCount(u32 format) const char* WriteRegister(API_TYPE ApiType, const char *prefix, const u32 num) { if(ApiType == API_GLSL) - return ""; // Once we switch to GLSL 1.3 we can do something here + return ""; // Once we switch to GLSL 1.3 we can do something here static char result[64]; sprintf(result, " : register(%s%d)", prefix, num); return result; diff --git a/Source/Core/VideoCommon/Src/VertexShaderGen.h b/Source/Core/VideoCommon/Src/VertexShaderGen.h index 5cfb9f801d..afd49c65d4 100644 --- a/Source/Core/VideoCommon/Src/VertexShaderGen.h +++ b/Source/Core/VideoCommon/Src/VertexShaderGen.h @@ -48,15 +48,15 @@ #define C_DEPTHPARAMS (C_POSTTRANSFORMMATRICES + 64) #define C_VENVCONST_END (C_DEPTHPARAMS + 1) const s_svar VSVar_Loc[] = { {I_POSNORMALMATRIX, C_POSNORMALMATRIX, 6 }, - {I_PROJECTION , C_PROJECTION, 4 }, - {I_MATERIALS, C_MATERIALS, 4 }, - {I_LIGHTS, C_LIGHTS, 40 }, - {I_TEXMATRICES, C_TEXMATRICES, 24 }, - {I_TRANSFORMMATRICES , C_TRANSFORMMATRICES, 64 }, - {I_NORMALMATRICES , C_NORMALMATRICES, 32 }, - {I_POSTTRANSFORMMATRICES, C_POSTTRANSFORMMATRICES, 64 }, - {I_DEPTHPARAMS, C_DEPTHPARAMS, 1 }, - }; + {I_PROJECTION , C_PROJECTION, 4 }, + {I_MATERIALS, C_MATERIALS, 4 }, + {I_LIGHTS, C_LIGHTS, 40 }, + {I_TEXMATRICES, C_TEXMATRICES, 24 }, + {I_TRANSFORMMATRICES , C_TRANSFORMMATRICES, 64 }, + {I_NORMALMATRICES , C_NORMALMATRICES, 32 }, + {I_POSTTRANSFORMMATRICES, C_POSTTRANSFORMMATRICES, 64 }, + {I_DEPTHPARAMS, C_DEPTHPARAMS, 1 }, + }; template class _VERTEXSHADERUID { diff --git a/Source/Plugins/Plugin_VideoOGL/Src/PixelShaderCache.cpp b/Source/Plugins/Plugin_VideoOGL/Src/PixelShaderCache.cpp index db41f34024..452ff506c3 100644 --- a/Source/Plugins/Plugin_VideoOGL/Src/PixelShaderCache.cpp +++ b/Source/Plugins/Plugin_VideoOGL/Src/PixelShaderCache.cpp @@ -496,11 +496,11 @@ void PixelShaderCache::SetPSSampler(const char * name, unsigned int Tex) if (!strcmp(name, UniformNames[a])) { if(tmp.UniformLocations[a] == -1) - return; + return; else { - glUniform1i(tmp.UniformLocations[a], Tex); - return; + glUniform1i(tmp.UniformLocations[a], Tex); + return; } } } @@ -511,11 +511,11 @@ void SetPSConstant4fvByName(const char * name, unsigned int offset, const float if (!strcmp(name, UniformNames[a])) { if(tmp.UniformLocations[a] == -1) - return; + return; else { - glUniform4fv(tmp.UniformLocations[a] + offset, count, f); - return; + glUniform4fv(tmp.UniformLocations[a] + offset, count, f); + return; } } } @@ -601,9 +601,9 @@ bool CompileCGPixelShader(FRAGMENTSHADER& ps, const char* pstrprogram) file.close(); PanicAlert("Failed to compile pixel shader %d!\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 (%d):\n%s", - num_failures - 1, szTemp, - g_cgfProf, - cgGetLastListing(g_cgcontext)); + num_failures - 1, szTemp, + g_cgfProf, + cgGetLastListing(g_cgcontext)); return false; } diff --git a/Source/Plugins/Plugin_VideoOGL/Src/ProgramShaderCache.cpp b/Source/Plugins/Plugin_VideoOGL/Src/ProgramShaderCache.cpp index 15a5ff42a8..e43dfa9798 100644 --- a/Source/Plugins/Plugin_VideoOGL/Src/ProgramShaderCache.cpp +++ b/Source/Plugins/Plugin_VideoOGL/Src/ProgramShaderCache.cpp @@ -27,116 +27,116 @@ namespace OGL std::pair ProgramShaderCache::CurrentShaderProgram; const char *UniformNames[NUM_UNIFORMS] = { - // SAMPLERS - "samp0","samp1","samp2","samp3","samp4","samp5","samp6","samp7", - // PIXEL SHADER UNIFORMS - I_COLORS, - I_KCOLORS, - I_ALPHA, - I_TEXDIMS, - I_ZBIAS , - I_INDTEXSCALE , - I_INDTEXMTX, - I_FOG, - I_PLIGHTS, - I_PMATERIALS, - // VERTEX SHADER UNIFORMS - I_POSNORMALMATRIX, - I_PROJECTION , - I_MATERIALS, - I_LIGHTS, - I_TEXMATRICES, - I_TRANSFORMMATRICES , - I_NORMALMATRICES , - I_POSTTRANSFORMMATRICES, - I_DEPTHPARAMS, - }; + // SAMPLERS + "samp0","samp1","samp2","samp3","samp4","samp5","samp6","samp7", + // PIXEL SHADER UNIFORMS + I_COLORS, + I_KCOLORS, + I_ALPHA, + I_TEXDIMS, + I_ZBIAS , + I_INDTEXSCALE , + I_INDTEXMTX, + I_FOG, + I_PLIGHTS, + I_PMATERIALS, + // VERTEX SHADER UNIFORMS + I_POSNORMALMATRIX, + I_PROJECTION , + I_MATERIALS, + I_LIGHTS, + I_TEXMATRICES, + I_TRANSFORMMATRICES , + I_NORMALMATRICES , + I_POSTTRANSFORMMATRICES, + I_DEPTHPARAMS, + }; void ProgramShaderCache::SetBothShaders(GLuint PS, GLuint VS) { - PROGRAMUID uid; - CurrentFShader = PS; - CurrentVShader = VS; + PROGRAMUID uid; + CurrentFShader = PS; + CurrentVShader = VS; - GetProgramShaderId(&uid, CurrentVShader, CurrentFShader); + GetProgramShaderId(&uid, CurrentVShader, CurrentFShader); - if(uid.uid.id == 0) - { - CurrentProgram = 0; - glUseProgram(0); - return; - } + if(uid.uid.id == 0) + { + CurrentProgram = 0; + glUseProgram(0); + return; + } - // Fragment shaders can survive without Vertex Shaders - // We have a valid fragment shader, let's create our program - std::pair ShaderPair = std::make_pair(uid.uid.psid, uid.uid.vsid); - PCache::iterator iter = pshaders.find(ShaderPair); - if (iter != pshaders.end()) - { - PCacheEntry &entry = iter->second; - glUseProgram(entry.program.glprogid); - CurrentShaderProgram = ShaderPair; - CurrentProgram = entry.program.glprogid; - return; - } - PCacheEntry entry; - entry.program.vsid = CurrentVShader; - entry.program.psid = CurrentFShader; - entry.program.glprogid = glCreateProgram(); + // Fragment shaders can survive without Vertex Shaders + // We have a valid fragment shader, let's create our program + std::pair ShaderPair = std::make_pair(uid.uid.psid, uid.uid.vsid); + PCache::iterator iter = pshaders.find(ShaderPair); + if (iter != pshaders.end()) + { + PCacheEntry &entry = iter->second; + glUseProgram(entry.program.glprogid); + CurrentShaderProgram = ShaderPair; + CurrentProgram = entry.program.glprogid; + return; + } + PCacheEntry entry; + entry.program.vsid = CurrentVShader; + entry.program.psid = CurrentFShader; + entry.program.glprogid = glCreateProgram(); - // Right, the program is created now - // Let's attach everything - if(entry.program.vsid != 0) // attaching zero vertex shader makes it freak out - glAttachShader(entry.program.glprogid, entry.program.vsid); - - glAttachShader(entry.program.glprogid, entry.program.psid); - - glLinkProgram(entry.program.glprogid); - - glUseProgram(entry.program.glprogid); + // Right, the program is created now + // Let's attach everything + if(entry.program.vsid != 0) // attaching zero vertex shader makes it freak out + glAttachShader(entry.program.glprogid, entry.program.vsid); + + glAttachShader(entry.program.glprogid, entry.program.psid); + + glLinkProgram(entry.program.glprogid); + + glUseProgram(entry.program.glprogid); - // Dunno why this is needed when I have the binding - // points statically set in the shader source - // We should only need these two functions when we don't support binding but do support UBO - // Driver Bug? Nvidia GTX 570, 290.xx Driver, Linux x64 - //if(!g_ActiveConfig.backend_info.bSupportsGLSLBinding) - { + // Dunno why this is needed when I have the binding + // points statically set in the shader source + // We should only need these two functions when we don't support binding but do support UBO + // Driver Bug? Nvidia GTX 570, 290.xx Driver, Linux x64 + //if(!g_ActiveConfig.backend_info.bSupportsGLSLBinding) + { glUniformBlockBinding( entry.program.glprogid, 0, 1 ); glUniformBlockBinding( entry.program.glprogid, 1, 2 ); } - // We cache our uniform locations for now - // Once we move up to a newer version of GLSL, ~1.30 - // We can remove this - - //For some reason this fails on my hardware - //glGetUniformIndices(entry.program.glprogid, NUM_UNIFORMS, UniformNames, entry.program.UniformLocations); - //Got to do it this crappy way. - if(!g_ActiveConfig.backend_info.bSupportsGLSLUBO) + // We cache our uniform locations for now + // Once we move up to a newer version of GLSL, ~1.30 + // We can remove this + + //For some reason this fails on my hardware + //glGetUniformIndices(entry.program.glprogid, NUM_UNIFORMS, UniformNames, entry.program.UniformLocations); + //Got to do it this crappy way. + if(!g_ActiveConfig.backend_info.bSupportsGLSLUBO) for(int a = 0; a < NUM_UNIFORMS; ++a) entry.program.UniformLocations[a] = glGetUniformLocation(entry.program.glprogid, UniformNames[a]); - // Need to get some attribute locations - if(uid.uid.vsid != 0) // We have no vertex Shader - { - entry.program.attrLoc[0] = glGetAttribLocation(entry.program.glprogid, "rawnorm1"); - entry.program.attrLoc[1] = glGetAttribLocation(entry.program.glprogid, "rawnorm2"); - entry.program.attrLoc[2] = glGetAttribLocation(entry.program.glprogid, "fposmtx"); - if(entry.program.attrLoc[0] > 0) - glEnableVertexAttribArray(entry.program.attrLoc[0]); - if(entry.program.attrLoc[1] > 0) - glEnableVertexAttribArray(entry.program.attrLoc[1]); - if(entry.program.attrLoc[2] > 0) - glEnableVertexAttribArray(entry.program.attrLoc[2]); - } - else - entry.program.attrLoc[0] = entry.program.attrLoc[1] = entry.program.attrLoc[2] = 0; + // Need to get some attribute locations + if(uid.uid.vsid != 0) // We have no vertex Shader + { + entry.program.attrLoc[0] = glGetAttribLocation(entry.program.glprogid, "rawnorm1"); + entry.program.attrLoc[1] = glGetAttribLocation(entry.program.glprogid, "rawnorm2"); + entry.program.attrLoc[2] = glGetAttribLocation(entry.program.glprogid, "fposmtx"); + if(entry.program.attrLoc[0] > 0) + glEnableVertexAttribArray(entry.program.attrLoc[0]); + if(entry.program.attrLoc[1] > 0) + glEnableVertexAttribArray(entry.program.attrLoc[1]); + if(entry.program.attrLoc[2] > 0) + glEnableVertexAttribArray(entry.program.attrLoc[2]); + } + else + entry.program.attrLoc[0] = entry.program.attrLoc[1] = entry.program.attrLoc[2] = 0; - pshaders[ShaderPair] = entry; - CurrentShaderProgram = ShaderPair; - CurrentProgram = entry.program.glprogid; + pshaders[ShaderPair] = entry; + CurrentShaderProgram = ShaderPair; + CurrentProgram = entry.program.glprogid; } void ProgramShaderCache::SetUniformObjects(int Buffer, unsigned int offset, const float *f, unsigned int count) { @@ -158,11 +158,11 @@ namespace OGL GLint ProgramShaderCache::GetAttr(int num) { - return pshaders[CurrentShaderProgram].program.attrLoc[num]; + return pshaders[CurrentShaderProgram].program.attrLoc[num]; } PROGRAMSHADER ProgramShaderCache::GetShaderProgram(void) { - return pshaders[CurrentShaderProgram].program; + return pshaders[CurrentShaderProgram].program; } void ProgramShaderCache::Init(void) { @@ -190,11 +190,11 @@ namespace OGL } void ProgramShaderCache::Shutdown(void) { - PCache::iterator iter = pshaders.begin(); - for (; iter != pshaders.end(); iter++) - iter->second.Destroy(); - pshaders.clear(); - glDeleteBuffers(2, UBOBuffers); + PCache::iterator iter = pshaders.begin(); + for (; iter != pshaders.end(); iter++) + iter->second.Destroy(); + pshaders.clear(); + glDeleteBuffers(2, UBOBuffers); } } diff --git a/Source/Plugins/Plugin_VideoOGL/Src/ProgramShaderCache.h b/Source/Plugins/Plugin_VideoOGL/Src/ProgramShaderCache.h index 1c114551b8..4c356f52d5 100644 --- a/Source/Plugins/Plugin_VideoOGL/Src/ProgramShaderCache.h +++ b/Source/Plugins/Plugin_VideoOGL/Src/ProgramShaderCache.h @@ -27,10 +27,10 @@ union PID { - struct { - GLuint vsid, psid; - }; - u64 id; + struct { + GLuint vsid, psid; + }; + u64 id; }; class PROGRAMUID @@ -41,22 +41,22 @@ public: PROGRAMUID() { - uid.id = 0; + uid.id = 0; } PROGRAMUID(const PROGRAMUID& r) { - uid.id = r.uid.id; + uid.id = r.uid.id; } PROGRAMUID(GLuint _v, GLuint _p) { - uid.vsid = _v; - uid.psid = _p; + uid.vsid = _v; + uid.psid = _p; } int GetNumValues() const { - return uid.id; + return uid.id; } }; void GetProgramShaderId(PROGRAMUID *uid, GLuint _v, GLuint _p); @@ -81,13 +81,13 @@ class ProgramShaderCache { struct PCacheEntry { - PROGRAMSHADER program; - int frameCount; - PCacheEntry() : frameCount(0) {} - void Destroy() { - glDeleteProgram(program.glprogid); - program.glprogid = 0; - } + PROGRAMSHADER program; + int frameCount; + PCacheEntry() : frameCount(0) {} + void Destroy() { + glDeleteProgram(program.glprogid); + program.glprogid = 0; + } }; typedef std::map, PCacheEntry> PCache; diff --git a/Source/Plugins/Plugin_VideoOGL/Src/TextureConverter.cpp b/Source/Plugins/Plugin_VideoOGL/Src/TextureConverter.cpp index ff4ecab624..213d92098a 100644 --- a/Source/Plugins/Plugin_VideoOGL/Src/TextureConverter.cpp +++ b/Source/Plugins/Plugin_VideoOGL/Src/TextureConverter.cpp @@ -281,7 +281,7 @@ void Shutdown() } void EncodeToRamUsingShader(GLuint srcTexture, const TargetRectangle& sourceRc, - u8* destAddr, int dstWidth, int dstHeight, int readStride, + u8* destAddr, int dstWidth, int dstHeight, int readStride, bool toTexture, bool linearFilter) { diff --git a/Source/Plugins/Plugin_VideoOGL/Src/main.cpp b/Source/Plugins/Plugin_VideoOGL/Src/main.cpp index 8b25d5c47c..c7a4c0d42f 100644 --- a/Source/Plugins/Plugin_VideoOGL/Src/main.cpp +++ b/Source/Plugins/Plugin_VideoOGL/Src/main.cpp @@ -112,19 +112,19 @@ void GetShaders(std::vector &shaders) shaders.clear(); if (File::IsDirectory(File::GetUserPath(D_SHADERS_IDX))) { - File::FSTEntry entry; - File::ScanDirectoryTree(File::GetUserPath(D_SHADERS_IDX), entry); - for (u32 i = 0; i < entry.children.size(); i++) - { - std::string name = entry.children[i].virtualName.c_str(); - if (!strcasecmp(name.substr(name.size() - 4).c_str(), ".txt")) - name = name.substr(0, name.size() - 4); - shaders.push_back(name); - } + File::FSTEntry entry; + File::ScanDirectoryTree(File::GetUserPath(D_SHADERS_IDX), entry); + for (u32 i = 0; i < entry.children.size(); i++) + { + std::string name = entry.children[i].virtualName.c_str(); + if (!strcasecmp(name.substr(name.size() - 4).c_str(), ".txt")) + name = name.substr(0, name.size() - 4); + shaders.push_back(name); + } } else { - File::CreateDir(File::GetUserPath(D_SHADERS_IDX).c_str()); + File::CreateDir(File::GetUserPath(D_SHADERS_IDX).c_str()); } } From fbef258dab883492d306fc29823229616a090b41 Mon Sep 17 00:00:00 2001 From: Ryan Houdek Date: Sun, 11 Dec 2011 04:19:11 -0600 Subject: [PATCH 37/83] Disable UBO buffer generation if hardware doesn't support it. --- .../Src/ProgramShaderCache.cpp | 52 ++++++++++--------- 1 file changed, 28 insertions(+), 24 deletions(-) diff --git a/Source/Plugins/Plugin_VideoOGL/Src/ProgramShaderCache.cpp b/Source/Plugins/Plugin_VideoOGL/Src/ProgramShaderCache.cpp index e43dfa9798..adaa4babc3 100644 --- a/Source/Plugins/Plugin_VideoOGL/Src/ProgramShaderCache.cpp +++ b/Source/Plugins/Plugin_VideoOGL/Src/ProgramShaderCache.cpp @@ -102,9 +102,9 @@ namespace OGL // Driver Bug? Nvidia GTX 570, 290.xx Driver, Linux x64 //if(!g_ActiveConfig.backend_info.bSupportsGLSLBinding) { - glUniformBlockBinding( entry.program.glprogid, 0, 1 ); - glUniformBlockBinding( entry.program.glprogid, 1, 2 ); - } + glUniformBlockBinding( entry.program.glprogid, 0, 1 ); + glUniformBlockBinding( entry.program.glprogid, 1, 2 ); + } // We cache our uniform locations for now // Once we move up to a newer version of GLSL, ~1.30 @@ -114,8 +114,8 @@ namespace OGL //glGetUniformIndices(entry.program.glprogid, NUM_UNIFORMS, UniformNames, entry.program.UniformLocations); //Got to do it this crappy way. if(!g_ActiveConfig.backend_info.bSupportsGLSLUBO) - for(int a = 0; a < NUM_UNIFORMS; ++a) - entry.program.UniformLocations[a] = glGetUniformLocation(entry.program.glprogid, UniformNames[a]); + for(int a = 0; a < NUM_UNIFORMS; ++a) + entry.program.UniformLocations[a] = glGetUniformLocation(entry.program.glprogid, UniformNames[a]); // Need to get some attribute locations if(uid.uid.vsid != 0) // We have no vertex Shader @@ -138,22 +138,22 @@ namespace OGL CurrentShaderProgram = ShaderPair; CurrentProgram = entry.program.glprogid; } - void ProgramShaderCache::SetUniformObjects(int Buffer, unsigned int offset, const float *f, unsigned int count) + void ProgramShaderCache::SetUniformObjects(int Buffer, unsigned int offset, const float *f, unsigned int count) + { + assert(Buffer > 1); + static int _Buffer = -1; + if(_Buffer != Buffer) { - assert(Buffer > 1); - static int _Buffer = -1; - if(_Buffer != Buffer) - { - _Buffer = Buffer; - glBindBuffer(GL_UNIFORM_BUFFER, UBOBuffers[_Buffer]); - } - // Query for the offsets of each block variable - - // glBufferSubData expects data in bytes, so multiply count by four - // Expects the offset in bytes as well, so multiply by *4 *4 since we are passing in a vec4 location - glBufferSubData(GL_UNIFORM_BUFFER, offset * 4 * 4, count * 4 * 4, f); - + _Buffer = Buffer; + glBindBuffer(GL_UNIFORM_BUFFER, UBOBuffers[_Buffer]); } + // Query for the offsets of each block variable + + // glBufferSubData expects data in bytes, so multiply count by four + // Expects the offset in bytes as well, so multiply by *4 *4 since we are passing in a vec4 location + glBufferSubData(GL_UNIFORM_BUFFER, offset * 4 * 4, count * 4 * 4, f); + + } GLuint ProgramShaderCache::GetCurrentProgram(void) { return CurrentProgram; } GLint ProgramShaderCache::GetAttr(int num) @@ -164,11 +164,13 @@ namespace OGL { return pshaders[CurrentShaderProgram].program; } - void ProgramShaderCache::Init(void) + void ProgramShaderCache::Init(void) + { + // We have to get the UBO alignment here because + // if we generate a buffer that isn't aligned + // then the UBO will fail. + if (g_ActiveConfig.backend_info.bSupportsGLSLUBO) { - // We have to get the UBO alignment here because - // if we generate a buffer that isn't aligned - // then the UBO will fail. GLint Align; glGetIntegerv(GL_UNIFORM_BUFFER_OFFSET_ALIGNMENT, &Align); @@ -188,13 +190,15 @@ namespace OGL glBufferData(GL_UNIFORM_BUFFER, ROUND_UP(C_VENVCONST_END * 4 * 4, Align), NULL, GL_DYNAMIC_DRAW); glBindBufferBase(GL_UNIFORM_BUFFER, 2, UBOBuffers[1]); } + } void ProgramShaderCache::Shutdown(void) { PCache::iterator iter = pshaders.begin(); for (; iter != pshaders.end(); iter++) iter->second.Destroy(); pshaders.clear(); - glDeleteBuffers(2, UBOBuffers); + if (g_ActiveConfig.backend_info.bSupportsGLSLUBO) + glDeleteBuffers(2, UBOBuffers); } } From 031c523fbab44d92fbd01993c1d44a0e8713139e Mon Sep 17 00:00:00 2001 From: Jordan Woyak Date: Sun, 11 Dec 2011 04:11:57 -0600 Subject: [PATCH 38/83] have separate variables/functions for VS/PS ubo stuff, array was confusing. --- .../Plugin_VideoOGL/Src/PixelShaderCache.cpp | 14 ++--- .../Src/ProgramShaderCache.cpp | 57 ++++++++++--------- .../Plugin_VideoOGL/Src/ProgramShaderCache.h | 7 ++- .../Plugin_VideoOGL/Src/VertexShaderCache.cpp | 15 ++--- 4 files changed, 47 insertions(+), 46 deletions(-) diff --git a/Source/Plugins/Plugin_VideoOGL/Src/PixelShaderCache.cpp b/Source/Plugins/Plugin_VideoOGL/Src/PixelShaderCache.cpp index 452ff506c3..726dbe7a66 100644 --- a/Source/Plugins/Plugin_VideoOGL/Src/PixelShaderCache.cpp +++ b/Source/Plugins/Plugin_VideoOGL/Src/PixelShaderCache.cpp @@ -522,12 +522,12 @@ void SetPSConstant4fvByName(const char * name, unsigned int offset, const float void SetGLSLPSConstant4f(unsigned int const_number, float f1, float f2, float f3, float f4) { - float f[4] = { f1, f2, f3, f4 }; + float const f[4] = {f1, f2, f3, f4}; if (g_ActiveConfig.backend_info.bSupportsGLSLUBO) { - ProgramShaderCache::SetUniformObjects(0, const_number, f); - return; + ProgramShaderCache::SetMultiPSConstant4fv(const_number, f, 1); + return; } for (unsigned int a = 0; a < 10; ++a) { @@ -544,8 +544,8 @@ void SetGLSLPSConstant4fv(unsigned int const_number, const float *f) { if (g_ActiveConfig.backend_info.bSupportsGLSLUBO) { - ProgramShaderCache::SetUniformObjects(0, const_number, f); - return; + ProgramShaderCache::SetMultiPSConstant4fv(const_number, f, 1); + return; } for (unsigned int a = 0; a < 10; ++a) { @@ -562,8 +562,8 @@ void SetMultiGLSLPSConstant4fv(unsigned int const_number, unsigned int count, co { if (g_ActiveConfig.backend_info.bSupportsGLSLUBO) { - ProgramShaderCache::SetUniformObjects(0, const_number, f, count); - return; + ProgramShaderCache::SetMultiPSConstant4fv(const_number, f, count); + return; } for (unsigned int a = 0; a < 10; ++a) { diff --git a/Source/Plugins/Plugin_VideoOGL/Src/ProgramShaderCache.cpp b/Source/Plugins/Plugin_VideoOGL/Src/ProgramShaderCache.cpp index adaa4babc3..3be1e76aff 100644 --- a/Source/Plugins/Plugin_VideoOGL/Src/ProgramShaderCache.cpp +++ b/Source/Plugins/Plugin_VideoOGL/Src/ProgramShaderCache.cpp @@ -23,7 +23,8 @@ namespace OGL { GLuint ProgramShaderCache::CurrentFShader = 0, ProgramShaderCache::CurrentVShader = 0, ProgramShaderCache::CurrentProgram = 0; ProgramShaderCache::PCache ProgramShaderCache::pshaders; - GLuint ProgramShaderCache::UBOBuffers[2]; + GLuint ProgramShaderCache::s_ps_ubo; + GLuint ProgramShaderCache::s_vs_ubo; std::pair ProgramShaderCache::CurrentShaderProgram; const char *UniformNames[NUM_UNIFORMS] = { @@ -138,22 +139,21 @@ namespace OGL CurrentShaderProgram = ShaderPair; CurrentProgram = entry.program.glprogid; } - void ProgramShaderCache::SetUniformObjects(int Buffer, unsigned int offset, const float *f, unsigned int count) - { - assert(Buffer > 1); - static int _Buffer = -1; - if(_Buffer != Buffer) + + void ProgramShaderCache::SetMultiPSConstant4fv(unsigned int offset, const float *f, unsigned int count) { - _Buffer = Buffer; - glBindBuffer(GL_UNIFORM_BUFFER, UBOBuffers[_Buffer]); + glBindBuffer(GL_UNIFORM_BUFFER, s_ps_ubo); + glBufferSubData(GL_UNIFORM_BUFFER, offset * sizeof(float) * 4, count * sizeof(float) * 4, f); + glBindBuffer(GL_UNIFORM_BUFFER, 0); } - // Query for the offsets of each block variable - - // glBufferSubData expects data in bytes, so multiply count by four - // Expects the offset in bytes as well, so multiply by *4 *4 since we are passing in a vec4 location - glBufferSubData(GL_UNIFORM_BUFFER, offset * 4 * 4, count * 4 * 4, f); - + + void ProgramShaderCache::SetMultiVSConstant4fv(unsigned int offset, const float *f, unsigned int count) + { + glBindBuffer(GL_UNIFORM_BUFFER, s_vs_ubo); + glBufferSubData(GL_UNIFORM_BUFFER, offset * sizeof(float) * 4, count * sizeof(float) * 4, f); + glBindBuffer(GL_UNIFORM_BUFFER, 0); } + GLuint ProgramShaderCache::GetCurrentProgram(void) { return CurrentProgram; } GLint ProgramShaderCache::GetAttr(int num) @@ -173,32 +173,35 @@ namespace OGL { GLint Align; glGetIntegerv(GL_UNIFORM_BUFFER_OFFSET_ALIGNMENT, &Align); - - glGenBuffers(2, UBOBuffers); - - glBindBuffer(GL_UNIFORM_BUFFER, UBOBuffers[0]); + // We multiply by *4*4 because we need to get down to basic machine units. // So multiply by four to get how many floats we have from vec4s // Then once more to get bytes - glBufferData(GL_UNIFORM_BUFFER, ROUND_UP(C_PENVCONST_END * 4 * 4, Align), NULL, GL_DYNAMIC_DRAW); + glGenBuffers(1, &s_ps_ubo); + glBindBuffer(GL_UNIFORM_BUFFER, s_ps_ubo); + glBufferData(GL_UNIFORM_BUFFER, ROUND_UP(C_PENVCONST_END * sizeof(float) * 4, Align), NULL, GL_DYNAMIC_DRAW); + glGenBuffers(1, &s_vs_ubo); + glBindBuffer(GL_UNIFORM_BUFFER, s_vs_ubo); + glBufferData(GL_UNIFORM_BUFFER, ROUND_UP(C_VENVCONST_END * sizeof(float) * 4, Align), NULL, GL_DYNAMIC_DRAW); + + glBindBuffer(GL_UNIFORM_BUFFER, 0); // Now bind the buffer to the index point // We know PS is 0 since we have it statically set in the shader - glBindBufferBase(GL_UNIFORM_BUFFER, 1, UBOBuffers[0]); - // Repeat for VS shader - glBindBuffer(GL_UNIFORM_BUFFER, UBOBuffers[1]); - glBufferData(GL_UNIFORM_BUFFER, ROUND_UP(C_VENVCONST_END * 4 * 4, Align), NULL, GL_DYNAMIC_DRAW); - glBindBufferBase(GL_UNIFORM_BUFFER, 2, UBOBuffers[1]); + glBindBufferBase(GL_UNIFORM_BUFFER, 1, s_ps_ubo); + glBindBufferBase(GL_UNIFORM_BUFFER, 2, s_vs_ubo); } } + void ProgramShaderCache::Shutdown(void) { PCache::iterator iter = pshaders.begin(); - for (; iter != pshaders.end(); iter++) + for (; iter != pshaders.end(); ++iter) iter->second.Destroy(); pshaders.clear(); - if (g_ActiveConfig.backend_info.bSupportsGLSLUBO) - glDeleteBuffers(2, UBOBuffers); + + glDeleteBuffers(1, &s_ps_ubo); + glDeleteBuffers(1, &s_ps_ubo); } } diff --git a/Source/Plugins/Plugin_VideoOGL/Src/ProgramShaderCache.h b/Source/Plugins/Plugin_VideoOGL/Src/ProgramShaderCache.h index 4c356f52d5..03bb58ce55 100644 --- a/Source/Plugins/Plugin_VideoOGL/Src/ProgramShaderCache.h +++ b/Source/Plugins/Plugin_VideoOGL/Src/ProgramShaderCache.h @@ -95,14 +95,15 @@ class ProgramShaderCache static GLuint CurrentFShader, CurrentVShader, CurrentProgram; static std::pair CurrentShaderProgram; - // For UBOS - static GLuint UBOBuffers[2]; // PS is 0, VS is 1 + static GLuint s_ps_ubo, s_vs_ubo; public: static PROGRAMSHADER GetShaderProgram(void); static GLint GetAttr(int num); static void SetBothShaders(GLuint PS, GLuint VS); static GLuint GetCurrentProgram(void); - static void SetUniformObjects(int Buffer, unsigned int offset, const float *f, unsigned int count = 1); + + static void SetMultiPSConstant4fv(unsigned int offset, const float *f, unsigned int count); + static void SetMultiVSConstant4fv(unsigned int offset, const float *f, unsigned int count); static void Init(void); static void Shutdown(void); diff --git a/Source/Plugins/Plugin_VideoOGL/Src/VertexShaderCache.cpp b/Source/Plugins/Plugin_VideoOGL/Src/VertexShaderCache.cpp index b665aeae98..a88867095a 100644 --- a/Source/Plugins/Plugin_VideoOGL/Src/VertexShaderCache.cpp +++ b/Source/Plugins/Plugin_VideoOGL/Src/VertexShaderCache.cpp @@ -240,14 +240,11 @@ void SetVSConstant4fvByName(const char * name, unsigned int offset, const float } void SetGLSLVSConstant4f(unsigned int const_number, float f1, float f2, float f3, float f4) { - float buf[4]; - buf[0] = f1; - buf[1] = f2; - buf[2] = f3; - buf[3] = f4; + float const buf[4] = {f1, f2, f3, f4}; + if (g_ActiveConfig.backend_info.bSupportsGLSLUBO) { - ProgramShaderCache::SetUniformObjects(1, const_number, buf); + ProgramShaderCache::SetMultiVSConstant4fv(const_number, buf, 1); return; } for( unsigned int a = 0; a < 9; ++a) @@ -265,7 +262,7 @@ void SetGLSLVSConstant4fv(unsigned int const_number, const float *f) { if (g_ActiveConfig.backend_info.bSupportsGLSLUBO) { - ProgramShaderCache::SetUniformObjects(1, const_number, f); + ProgramShaderCache::SetMultiVSConstant4fv(const_number, f, 1); return; } for( unsigned int a = 0; a < 9; ++a) @@ -283,7 +280,7 @@ void SetMultiGLSLVSConstant4fv(unsigned int const_number, unsigned int count, co { if (g_ActiveConfig.backend_info.bSupportsGLSLUBO) { - ProgramShaderCache::SetUniformObjects(1, const_number, f, count); + ProgramShaderCache::SetMultiVSConstant4fv(const_number, f, count); return; } for( unsigned int a = 0; a < 9; ++a) @@ -309,7 +306,7 @@ void SetMultiGLSLVSConstant3fv(unsigned int const_number, unsigned int count, co } if (g_ActiveConfig.backend_info.bSupportsGLSLUBO) { - ProgramShaderCache::SetUniformObjects(1, const_number, buf, count); + ProgramShaderCache::SetMultiVSConstant4fv(const_number, buf, count); return; } for( unsigned int a = 0; a < 9; ++a) From bcb2abbcf18018b339faea58a7d8e65114986ca0 Mon Sep 17 00:00:00 2001 From: Jordan Woyak Date: Sun, 11 Dec 2011 04:32:57 -0600 Subject: [PATCH 39/83] fix stupid indentation --- .../Src/ProgramShaderCache.cpp | 24 +++++++++---------- 1 file changed, 12 insertions(+), 12 deletions(-) diff --git a/Source/Plugins/Plugin_VideoOGL/Src/ProgramShaderCache.cpp b/Source/Plugins/Plugin_VideoOGL/Src/ProgramShaderCache.cpp index 3be1e76aff..dd4fd6923f 100644 --- a/Source/Plugins/Plugin_VideoOGL/Src/ProgramShaderCache.cpp +++ b/Source/Plugins/Plugin_VideoOGL/Src/ProgramShaderCache.cpp @@ -140,18 +140,18 @@ namespace OGL CurrentProgram = entry.program.glprogid; } - void ProgramShaderCache::SetMultiPSConstant4fv(unsigned int offset, const float *f, unsigned int count) - { - glBindBuffer(GL_UNIFORM_BUFFER, s_ps_ubo); - glBufferSubData(GL_UNIFORM_BUFFER, offset * sizeof(float) * 4, count * sizeof(float) * 4, f); - glBindBuffer(GL_UNIFORM_BUFFER, 0); - } - - void ProgramShaderCache::SetMultiVSConstant4fv(unsigned int offset, const float *f, unsigned int count) - { - glBindBuffer(GL_UNIFORM_BUFFER, s_vs_ubo); - glBufferSubData(GL_UNIFORM_BUFFER, offset * sizeof(float) * 4, count * sizeof(float) * 4, f); - glBindBuffer(GL_UNIFORM_BUFFER, 0); + void ProgramShaderCache::SetMultiPSConstant4fv(unsigned int offset, const float *f, unsigned int count) + { + glBindBuffer(GL_UNIFORM_BUFFER, s_ps_ubo); + glBufferSubData(GL_UNIFORM_BUFFER, offset * sizeof(float) * 4, count * sizeof(float) * 4, f); + glBindBuffer(GL_UNIFORM_BUFFER, 0); + } + + void ProgramShaderCache::SetMultiVSConstant4fv(unsigned int offset, const float *f, unsigned int count) + { + glBindBuffer(GL_UNIFORM_BUFFER, s_vs_ubo); + glBufferSubData(GL_UNIFORM_BUFFER, offset * sizeof(float) * 4, count * sizeof(float) * 4, f); + glBindBuffer(GL_UNIFORM_BUFFER, 0); } GLuint ProgramShaderCache::GetCurrentProgram(void) { return CurrentProgram; } From 16b58a8825cae61d9f48aebfaa29182f0c687538 Mon Sep 17 00:00:00 2001 From: Ryan Houdek Date: Sun, 11 Dec 2011 05:15:08 -0600 Subject: [PATCH 40/83] Show a bit of information when using GLSL shaders. --- .../Plugin_VideoOGL/Src/PixelShaderCache.cpp | 27 +++++++++++-------- 1 file changed, 16 insertions(+), 11 deletions(-) diff --git a/Source/Plugins/Plugin_VideoOGL/Src/PixelShaderCache.cpp b/Source/Plugins/Plugin_VideoOGL/Src/PixelShaderCache.cpp index 726dbe7a66..9556c12995 100644 --- a/Source/Plugins/Plugin_VideoOGL/Src/PixelShaderCache.cpp +++ b/Source/Plugins/Plugin_VideoOGL/Src/PixelShaderCache.cpp @@ -31,6 +31,8 @@ #include "ProgramShaderCache.h" #include "PixelShaderCache.h" #include "PixelShaderManager.h" +#include "OnScreenDisplay.h" +#include "StringUtil.h" #include "FileUtil.h" #include "Debugger.h" @@ -74,25 +76,28 @@ void PixelShaderCache::Init() if(g_ActiveConfig.bUseGLSL) { - pSetPSConstant4f = SetGLSLPSConstant4f; - pSetPSConstant4fv = SetGLSLPSConstant4fv; - pSetMultiPSConstant4fv = SetMultiGLSLPSConstant4fv; - pCompilePixelShader = CompileGLSLPixelShader; - // Should this be set here? - if (strstr((const char*)glGetString(GL_EXTENSIONS), "GL_ARB_shading_language_420pack") != NULL) + pSetPSConstant4f = SetGLSLPSConstant4f; + pSetPSConstant4fv = SetGLSLPSConstant4fv; + pSetMultiPSConstant4fv = SetMultiGLSLPSConstant4fv; + pCompilePixelShader = CompileGLSLPixelShader; + // Should this be set here? + if (strstr((const char*)glGetString(GL_EXTENSIONS), "GL_ARB_shading_language_420pack") != NULL) g_Config.backend_info.bSupportsGLSLBinding = true; if (strstr((const char*)glGetString(GL_EXTENSIONS), "GL_ARB_uniform_buffer_object") != NULL) g_Config.backend_info.bSupportsGLSLUBO = true; UpdateActiveConfig(); + OSD::AddMessage(StringFromFormat("Using GLSL. Supports Binding: %s UBOs: %s", + g_ActiveConfig.backend_info.bSupportsGLSLBinding ? "True" : "False", + g_ActiveConfig.backend_info.bSupportsGLSLUBO ? "True" : "False").c_str(), 5000); } else { - pSetPSConstant4f = SetCGPSConstant4f; - pSetPSConstant4fv = SetCGPSConstant4fv; - pSetMultiPSConstant4fv = SetMultiCGPSConstant4fv; - pCompilePixelShader = CompileCGPixelShader; - glEnable(GL_FRAGMENT_PROGRAM_ARB); + pSetPSConstant4f = SetCGPSConstant4f; + pSetPSConstant4fv = SetCGPSConstant4fv; + pSetMultiPSConstant4fv = SetMultiCGPSConstant4fv; + pCompilePixelShader = CompileCGPixelShader; + glEnable(GL_FRAGMENT_PROGRAM_ARB); } glGetProgramivARB(GL_FRAGMENT_PROGRAM_ARB, GL_MAX_PROGRAM_NATIVE_ALU_INSTRUCTIONS_ARB, (GLint *)&s_nMaxPixelInstructions); From 5ae1f674f56851466b57ad9986a835e85eb3909c Mon Sep 17 00:00:00 2001 From: Jordan Woyak Date: Sun, 11 Dec 2011 05:13:05 -0600 Subject: [PATCH 41/83] make use of glMapBuffer to set ubo data --- .../Src/ProgramShaderCache.cpp | 56 ++++++++++++++++--- .../Plugin_VideoOGL/Src/ProgramShaderCache.h | 3 + .../Plugin_VideoOGL/Src/VertexManager.cpp | 3 + 3 files changed, 55 insertions(+), 7 deletions(-) diff --git a/Source/Plugins/Plugin_VideoOGL/Src/ProgramShaderCache.cpp b/Source/Plugins/Plugin_VideoOGL/Src/ProgramShaderCache.cpp index dd4fd6923f..df01055e65 100644 --- a/Source/Plugins/Plugin_VideoOGL/Src/ProgramShaderCache.cpp +++ b/Source/Plugins/Plugin_VideoOGL/Src/ProgramShaderCache.cpp @@ -25,6 +25,8 @@ namespace OGL ProgramShaderCache::PCache ProgramShaderCache::pshaders; GLuint ProgramShaderCache::s_ps_ubo; GLuint ProgramShaderCache::s_vs_ubo; + float* ProgramShaderCache::s_ps_mapped_data; + float* ProgramShaderCache::s_vs_mapped_data; std::pair ProgramShaderCache::CurrentShaderProgram; const char *UniformNames[NUM_UNIFORMS] = { @@ -142,18 +144,55 @@ namespace OGL void ProgramShaderCache::SetMultiPSConstant4fv(unsigned int offset, const float *f, unsigned int count) { - glBindBuffer(GL_UNIFORM_BUFFER, s_ps_ubo); - glBufferSubData(GL_UNIFORM_BUFFER, offset * sizeof(float) * 4, count * sizeof(float) * 4, f); - glBindBuffer(GL_UNIFORM_BUFFER, 0); + if (!s_ps_mapped_data) + { + glBindBuffer(GL_UNIFORM_BUFFER, s_ps_ubo); + s_ps_mapped_data = reinterpret_cast(glMapBuffer(GL_UNIFORM_BUFFER, GL_WRITE_ONLY)); + glBindBuffer(GL_UNIFORM_BUFFER, 0); + + if (!s_ps_mapped_data) + PanicAlert("glMapBuffer"); + } + + std::copy(f, f + count * 4, s_ps_mapped_data + offset * 4); } void ProgramShaderCache::SetMultiVSConstant4fv(unsigned int offset, const float *f, unsigned int count) { - glBindBuffer(GL_UNIFORM_BUFFER, s_vs_ubo); - glBufferSubData(GL_UNIFORM_BUFFER, offset * sizeof(float) * 4, count * sizeof(float) * 4, f); + if (!s_vs_mapped_data) + { + glBindBuffer(GL_UNIFORM_BUFFER, s_vs_ubo); + s_vs_mapped_data = reinterpret_cast(glMapBuffer(GL_UNIFORM_BUFFER, GL_WRITE_ONLY)); + glBindBuffer(GL_UNIFORM_BUFFER, 0); + + if (!s_vs_mapped_data) + PanicAlert("glMapBuffer"); + } + + std::copy(f, f + count * 4, s_vs_mapped_data + offset * 4); + } + + void ProgramShaderCache::FlushConstants() + { + if (s_ps_mapped_data) + { + glBindBuffer(GL_UNIFORM_BUFFER, s_ps_ubo); + if (!glUnmapBuffer(GL_UNIFORM_BUFFER)) + PanicAlert("glUnmapBuffer"); + s_ps_mapped_data = NULL; + } + + if (s_vs_mapped_data) + { + glBindBuffer(GL_UNIFORM_BUFFER, s_vs_ubo); + if (!glUnmapBuffer(GL_UNIFORM_BUFFER)) + PanicAlert("glUnmapBuffer"); + s_vs_mapped_data = NULL; + } + glBindBuffer(GL_UNIFORM_BUFFER, 0); } - + GLuint ProgramShaderCache::GetCurrentProgram(void) { return CurrentProgram; } GLint ProgramShaderCache::GetAttr(int num) @@ -200,8 +239,11 @@ namespace OGL iter->second.Destroy(); pshaders.clear(); + // "A buffer object's mapped data store is automatically unmapped when the buffer object is deleted" glDeleteBuffers(1, &s_ps_ubo); - glDeleteBuffers(1, &s_ps_ubo); + glDeleteBuffers(1, &s_vs_ubo); + s_ps_ubo = s_vs_ubo = 0; + s_ps_mapped_data = s_vs_mapped_data = NULL; } } diff --git a/Source/Plugins/Plugin_VideoOGL/Src/ProgramShaderCache.h b/Source/Plugins/Plugin_VideoOGL/Src/ProgramShaderCache.h index 03bb58ce55..56026a5c4d 100644 --- a/Source/Plugins/Plugin_VideoOGL/Src/ProgramShaderCache.h +++ b/Source/Plugins/Plugin_VideoOGL/Src/ProgramShaderCache.h @@ -96,6 +96,7 @@ class ProgramShaderCache static std::pair CurrentShaderProgram; static GLuint s_ps_ubo, s_vs_ubo; + static float *s_ps_mapped_data, *s_vs_mapped_data; public: static PROGRAMSHADER GetShaderProgram(void); static GLint GetAttr(int num); @@ -104,6 +105,8 @@ public: static void SetMultiPSConstant4fv(unsigned int offset, const float *f, unsigned int count); static void SetMultiVSConstant4fv(unsigned int offset, const float *f, unsigned int count); + + static void FlushConstants(); static void Init(void); static void Shutdown(void); diff --git a/Source/Plugins/Plugin_VideoOGL/Src/VertexManager.cpp b/Source/Plugins/Plugin_VideoOGL/Src/VertexManager.cpp index fff8263569..7d7bc651a0 100644 --- a/Source/Plugins/Plugin_VideoOGL/Src/VertexManager.cpp +++ b/Source/Plugins/Plugin_VideoOGL/Src/VertexManager.cpp @@ -214,6 +214,9 @@ void VertexManager::vFlush() // set global constants VertexShaderManager::SetConstants(); PixelShaderManager::SetConstants(); + + if(g_ActiveConfig.bUseGLSL && g_ActiveConfig.backend_info.bSupportsGLSLUBO) + ProgramShaderCache::FlushConstants(); // setup the pointers if (g_nativeVertexFmt) From a613012d084e4913c3eb3eafe6e24adafe99dfaa Mon Sep 17 00:00:00 2001 From: Jordan Woyak Date: Sun, 11 Dec 2011 06:02:04 -0600 Subject: [PATCH 42/83] try combining vs/ps ubo --- .../Src/ProgramShaderCache.cpp | 80 ++++++++----------- .../Plugin_VideoOGL/Src/ProgramShaderCache.h | 8 +- 2 files changed, 38 insertions(+), 50 deletions(-) diff --git a/Source/Plugins/Plugin_VideoOGL/Src/ProgramShaderCache.cpp b/Source/Plugins/Plugin_VideoOGL/Src/ProgramShaderCache.cpp index df01055e65..bff25a5a65 100644 --- a/Source/Plugins/Plugin_VideoOGL/Src/ProgramShaderCache.cpp +++ b/Source/Plugins/Plugin_VideoOGL/Src/ProgramShaderCache.cpp @@ -23,10 +23,9 @@ namespace OGL { GLuint ProgramShaderCache::CurrentFShader = 0, ProgramShaderCache::CurrentVShader = 0, ProgramShaderCache::CurrentProgram = 0; ProgramShaderCache::PCache ProgramShaderCache::pshaders; - GLuint ProgramShaderCache::s_ps_ubo; - GLuint ProgramShaderCache::s_vs_ubo; - float* ProgramShaderCache::s_ps_mapped_data; - float* ProgramShaderCache::s_vs_mapped_data; + GLuint ProgramShaderCache::s_ps_vs_ubo; + float* ProgramShaderCache::s_ps_vs_mapped_data; + GLintptr ProgramShaderCache::s_vs_data_offset; std::pair ProgramShaderCache::CurrentShaderProgram; const char *UniformNames[NUM_UNIFORMS] = { @@ -142,55 +141,40 @@ namespace OGL CurrentProgram = entry.program.glprogid; } - void ProgramShaderCache::SetMultiPSConstant4fv(unsigned int offset, const float *f, unsigned int count) + void ProgramShaderCache::MapBuffer() { - if (!s_ps_mapped_data) + if (!s_ps_vs_mapped_data) { - glBindBuffer(GL_UNIFORM_BUFFER, s_ps_ubo); - s_ps_mapped_data = reinterpret_cast(glMapBuffer(GL_UNIFORM_BUFFER, GL_WRITE_ONLY)); - glBindBuffer(GL_UNIFORM_BUFFER, 0); + s_ps_vs_mapped_data = reinterpret_cast(glMapBuffer(GL_UNIFORM_BUFFER, GL_WRITE_ONLY)); - if (!s_ps_mapped_data) + if (!s_ps_vs_mapped_data) PanicAlert("glMapBuffer"); } + } - std::copy(f, f + count * 4, s_ps_mapped_data + offset * 4); + void ProgramShaderCache::SetMultiPSConstant4fv(unsigned int offset, const float *f, unsigned int count) + { + MapBuffer(); + + std::copy(f, f + count * 4, s_ps_vs_mapped_data + offset * 4); } void ProgramShaderCache::SetMultiVSConstant4fv(unsigned int offset, const float *f, unsigned int count) { - if (!s_vs_mapped_data) - { - glBindBuffer(GL_UNIFORM_BUFFER, s_vs_ubo); - s_vs_mapped_data = reinterpret_cast(glMapBuffer(GL_UNIFORM_BUFFER, GL_WRITE_ONLY)); - glBindBuffer(GL_UNIFORM_BUFFER, 0); + MapBuffer(); - if (!s_vs_mapped_data) - PanicAlert("glMapBuffer"); - } - - std::copy(f, f + count * 4, s_vs_mapped_data + offset * 4); + // TODO: division = hax + std::copy(f, f + count * 4, s_ps_vs_mapped_data + (s_vs_data_offset / sizeof(float)) + offset * 4); } void ProgramShaderCache::FlushConstants() { - if (s_ps_mapped_data) + if (s_ps_vs_mapped_data) { - glBindBuffer(GL_UNIFORM_BUFFER, s_ps_ubo); if (!glUnmapBuffer(GL_UNIFORM_BUFFER)) PanicAlert("glUnmapBuffer"); - s_ps_mapped_data = NULL; + s_ps_vs_mapped_data = NULL; } - - if (s_vs_mapped_data) - { - glBindBuffer(GL_UNIFORM_BUFFER, s_vs_ubo); - if (!glUnmapBuffer(GL_UNIFORM_BUFFER)) - PanicAlert("glUnmapBuffer"); - s_vs_mapped_data = NULL; - } - - glBindBuffer(GL_UNIFORM_BUFFER, 0); } GLuint ProgramShaderCache::GetCurrentProgram(void) { return CurrentProgram; } @@ -212,23 +196,23 @@ namespace OGL { GLint Align; glGetIntegerv(GL_UNIFORM_BUFFER_OFFSET_ALIGNMENT, &Align); + + GLintptr const ps_data_size = ROUND_UP(C_PENVCONST_END * sizeof(float) * 4, Align); + GLintptr const vs_data_size = ROUND_UP(C_VENVCONST_END * sizeof(float) * 4, Align); + s_vs_data_offset = ps_data_size; // We multiply by *4*4 because we need to get down to basic machine units. // So multiply by four to get how many floats we have from vec4s // Then once more to get bytes - glGenBuffers(1, &s_ps_ubo); - glBindBuffer(GL_UNIFORM_BUFFER, s_ps_ubo); - glBufferData(GL_UNIFORM_BUFFER, ROUND_UP(C_PENVCONST_END * sizeof(float) * 4, Align), NULL, GL_DYNAMIC_DRAW); - glGenBuffers(1, &s_vs_ubo); - glBindBuffer(GL_UNIFORM_BUFFER, s_vs_ubo); - glBufferData(GL_UNIFORM_BUFFER, ROUND_UP(C_VENVCONST_END * sizeof(float) * 4, Align), NULL, GL_DYNAMIC_DRAW); - - glBindBuffer(GL_UNIFORM_BUFFER, 0); + glGenBuffers(1, &s_ps_vs_ubo); + glBindBuffer(GL_UNIFORM_BUFFER, s_ps_vs_ubo); + glBufferData(GL_UNIFORM_BUFFER, ps_data_size + vs_data_size, NULL, GL_DYNAMIC_DRAW); + // Now bind the buffer to the index point // We know PS is 0 since we have it statically set in the shader // Repeat for VS shader - glBindBufferBase(GL_UNIFORM_BUFFER, 1, s_ps_ubo); - glBindBufferBase(GL_UNIFORM_BUFFER, 2, s_vs_ubo); + glBindBufferRange(GL_UNIFORM_BUFFER, 1, s_ps_vs_ubo, 0, ps_data_size); + glBindBufferRange(GL_UNIFORM_BUFFER, 2, s_ps_vs_ubo, s_vs_data_offset, vs_data_size); } } @@ -239,11 +223,11 @@ namespace OGL iter->second.Destroy(); pshaders.clear(); + glBindBuffer(GL_UNIFORM_BUFFER, 0); // "A buffer object's mapped data store is automatically unmapped when the buffer object is deleted" - glDeleteBuffers(1, &s_ps_ubo); - glDeleteBuffers(1, &s_vs_ubo); - s_ps_ubo = s_vs_ubo = 0; - s_ps_mapped_data = s_vs_mapped_data = NULL; + glDeleteBuffers(1, &s_ps_vs_ubo); + s_ps_vs_ubo = 0; + s_ps_vs_mapped_data = NULL; } } diff --git a/Source/Plugins/Plugin_VideoOGL/Src/ProgramShaderCache.h b/Source/Plugins/Plugin_VideoOGL/Src/ProgramShaderCache.h index 56026a5c4d..20a33f184b 100644 --- a/Source/Plugins/Plugin_VideoOGL/Src/ProgramShaderCache.h +++ b/Source/Plugins/Plugin_VideoOGL/Src/ProgramShaderCache.h @@ -95,8 +95,12 @@ class ProgramShaderCache static GLuint CurrentFShader, CurrentVShader, CurrentProgram; static std::pair CurrentShaderProgram; - static GLuint s_ps_ubo, s_vs_ubo; - static float *s_ps_mapped_data, *s_vs_mapped_data; + static GLuint s_ps_vs_ubo; + static float* s_ps_vs_mapped_data; + static GLintptr s_vs_data_offset; + + static void MapBuffer(); + public: static PROGRAMSHADER GetShaderProgram(void); static GLint GetAttr(int num); From c908e1173d9a1d54645249f8a942d18203cd7244 Mon Sep 17 00:00:00 2001 From: Jordan Woyak Date: Sun, 11 Dec 2011 06:11:38 -0600 Subject: [PATCH 43/83] glMapBuffer was slow, go back to glBufferSubData, single combined ps/vs ubo now --- .../Src/ProgramShaderCache.cpp | 37 +++---------------- .../Plugin_VideoOGL/Src/ProgramShaderCache.h | 5 --- .../Plugin_VideoOGL/Src/VertexManager.cpp | 3 -- 3 files changed, 5 insertions(+), 40 deletions(-) diff --git a/Source/Plugins/Plugin_VideoOGL/Src/ProgramShaderCache.cpp b/Source/Plugins/Plugin_VideoOGL/Src/ProgramShaderCache.cpp index bff25a5a65..10c15aae36 100644 --- a/Source/Plugins/Plugin_VideoOGL/Src/ProgramShaderCache.cpp +++ b/Source/Plugins/Plugin_VideoOGL/Src/ProgramShaderCache.cpp @@ -24,7 +24,6 @@ namespace OGL GLuint ProgramShaderCache::CurrentFShader = 0, ProgramShaderCache::CurrentVShader = 0, ProgramShaderCache::CurrentProgram = 0; ProgramShaderCache::PCache ProgramShaderCache::pshaders; GLuint ProgramShaderCache::s_ps_vs_ubo; - float* ProgramShaderCache::s_ps_vs_mapped_data; GLintptr ProgramShaderCache::s_vs_data_offset; std::pair ProgramShaderCache::CurrentShaderProgram; @@ -141,42 +140,18 @@ namespace OGL CurrentProgram = entry.program.glprogid; } - void ProgramShaderCache::MapBuffer() - { - if (!s_ps_vs_mapped_data) - { - s_ps_vs_mapped_data = reinterpret_cast(glMapBuffer(GL_UNIFORM_BUFFER, GL_WRITE_ONLY)); - - if (!s_ps_vs_mapped_data) - PanicAlert("glMapBuffer"); - } - } - void ProgramShaderCache::SetMultiPSConstant4fv(unsigned int offset, const float *f, unsigned int count) { - MapBuffer(); - - std::copy(f, f + count * 4, s_ps_vs_mapped_data + offset * 4); + glBufferSubData(GL_UNIFORM_BUFFER, offset * sizeof(float) * 4, + count * sizeof(float) * 4, f); } void ProgramShaderCache::SetMultiVSConstant4fv(unsigned int offset, const float *f, unsigned int count) { - MapBuffer(); - - // TODO: division = hax - std::copy(f, f + count * 4, s_ps_vs_mapped_data + (s_vs_data_offset / sizeof(float)) + offset * 4); + glBufferSubData(GL_UNIFORM_BUFFER, s_vs_data_offset + offset * sizeof(float) * 4, + count * sizeof(float) * 4, f); } - - void ProgramShaderCache::FlushConstants() - { - if (s_ps_vs_mapped_data) - { - if (!glUnmapBuffer(GL_UNIFORM_BUFFER)) - PanicAlert("glUnmapBuffer"); - s_ps_vs_mapped_data = NULL; - } - } - + GLuint ProgramShaderCache::GetCurrentProgram(void) { return CurrentProgram; } GLint ProgramShaderCache::GetAttr(int num) @@ -224,10 +199,8 @@ namespace OGL pshaders.clear(); glBindBuffer(GL_UNIFORM_BUFFER, 0); - // "A buffer object's mapped data store is automatically unmapped when the buffer object is deleted" glDeleteBuffers(1, &s_ps_vs_ubo); s_ps_vs_ubo = 0; - s_ps_vs_mapped_data = NULL; } } diff --git a/Source/Plugins/Plugin_VideoOGL/Src/ProgramShaderCache.h b/Source/Plugins/Plugin_VideoOGL/Src/ProgramShaderCache.h index 20a33f184b..26d5735ce6 100644 --- a/Source/Plugins/Plugin_VideoOGL/Src/ProgramShaderCache.h +++ b/Source/Plugins/Plugin_VideoOGL/Src/ProgramShaderCache.h @@ -96,10 +96,7 @@ class ProgramShaderCache static std::pair CurrentShaderProgram; static GLuint s_ps_vs_ubo; - static float* s_ps_vs_mapped_data; static GLintptr s_vs_data_offset; - - static void MapBuffer(); public: static PROGRAMSHADER GetShaderProgram(void); @@ -109,8 +106,6 @@ public: static void SetMultiPSConstant4fv(unsigned int offset, const float *f, unsigned int count); static void SetMultiVSConstant4fv(unsigned int offset, const float *f, unsigned int count); - - static void FlushConstants(); static void Init(void); static void Shutdown(void); diff --git a/Source/Plugins/Plugin_VideoOGL/Src/VertexManager.cpp b/Source/Plugins/Plugin_VideoOGL/Src/VertexManager.cpp index 7d7bc651a0..fff8263569 100644 --- a/Source/Plugins/Plugin_VideoOGL/Src/VertexManager.cpp +++ b/Source/Plugins/Plugin_VideoOGL/Src/VertexManager.cpp @@ -214,9 +214,6 @@ void VertexManager::vFlush() // set global constants VertexShaderManager::SetConstants(); PixelShaderManager::SetConstants(); - - if(g_ActiveConfig.bUseGLSL && g_ActiveConfig.backend_info.bSupportsGLSLUBO) - ProgramShaderCache::FlushConstants(); // setup the pointers if (g_nativeVertexFmt) From ad13f2d23d8dd809e5f5246a7ea45bbcb0b3fc70 Mon Sep 17 00:00:00 2001 From: Ryan Houdek Date: Sun, 11 Dec 2011 06:18:01 -0600 Subject: [PATCH 44/83] Instead of querying the vertex attribute location. Let's bind it to where CG expects it to be as well. Was causing problems when we were trying to activate the components below and they weren't available. This fixes cubivore with GLSL shader. Also any other game that uses 3 normal pointers. --- .../Plugin_VideoOGL/Src/NativeVertexFormat.cpp | 17 +++-------------- .../Plugin_VideoOGL/Src/ProgramShaderCache.cpp | 18 +++--------------- .../Plugin_VideoOGL/Src/ProgramShaderCache.h | 2 -- 3 files changed, 6 insertions(+), 31 deletions(-) diff --git a/Source/Plugins/Plugin_VideoOGL/Src/NativeVertexFormat.cpp b/Source/Plugins/Plugin_VideoOGL/Src/NativeVertexFormat.cpp index c83c976fb2..d42b2a7222 100644 --- a/Source/Plugins/Plugin_VideoOGL/Src/NativeVertexFormat.cpp +++ b/Source/Plugins/Plugin_VideoOGL/Src/NativeVertexFormat.cpp @@ -192,15 +192,8 @@ void GLVertexFormat::SetupVertexPointers() { if (vtx_decl.num_normals >= 1) { glNormalPointer(VarToGL(vtx_decl.normal_gl_type), vtx_decl.stride, (void *)(VertexManager::s_pBaseBufferPointer + vtx_decl.normal_offset[0])); if (vtx_decl.num_normals == 3) { - GLint varLocation1 = SHADER_NORM1_ATTRIB, varLocation2 = SHADER_NORM2_ATTRIB; - if(g_ActiveConfig.bUseGLSL){ - varLocation1 = OGL::ProgramShaderCache::GetAttr(0); - varLocation2 = OGL::ProgramShaderCache::GetAttr(1); - } - if(varLocation1 > 0) - glVertexAttribPointer(varLocation1, vtx_decl.normal_gl_size, VarToGL(vtx_decl.normal_gl_type), GL_TRUE, vtx_decl.stride, (void *)(VertexManager::s_pBaseBufferPointer + vtx_decl.normal_offset[1])); - if(varLocation2 > 0) - glVertexAttribPointer(varLocation2, vtx_decl.normal_gl_size, VarToGL(vtx_decl.normal_gl_type), GL_TRUE, vtx_decl.stride, (void *)(VertexManager::s_pBaseBufferPointer + vtx_decl.normal_offset[2])); + glVertexAttribPointer(SHADER_NORM1_ATTRIB, vtx_decl.normal_gl_size, VarToGL(vtx_decl.normal_gl_type), GL_TRUE, vtx_decl.stride, (void *)(VertexManager::s_pBaseBufferPointer + vtx_decl.normal_offset[1])); + glVertexAttribPointer(SHADER_NORM2_ATTRIB, vtx_decl.normal_gl_size, VarToGL(vtx_decl.normal_gl_type), GL_TRUE, vtx_decl.stride, (void *)(VertexManager::s_pBaseBufferPointer + vtx_decl.normal_offset[2])); } } @@ -224,11 +217,7 @@ void GLVertexFormat::SetupVertexPointers() { } if (vtx_decl.posmtx_offset != -1) { - GLint varLocation = SHADER_POSMTX_ATTRIB; - if(g_ActiveConfig.bUseGLSL) - varLocation = OGL::ProgramShaderCache::GetAttr(2); - if(varLocation > 0) - glVertexAttribPointer(varLocation, 4, GL_UNSIGNED_BYTE, GL_FALSE, vtx_decl.stride, (void *)(VertexManager::s_pBaseBufferPointer + vtx_decl.posmtx_offset)); + glVertexAttribPointer(SHADER_POSMTX_ATTRIB, 4, GL_UNSIGNED_BYTE, GL_FALSE, vtx_decl.stride, (void *)(VertexManager::s_pBaseBufferPointer + vtx_decl.posmtx_offset)); } #endif } diff --git a/Source/Plugins/Plugin_VideoOGL/Src/ProgramShaderCache.cpp b/Source/Plugins/Plugin_VideoOGL/Src/ProgramShaderCache.cpp index 10c15aae36..2f6aa691de 100644 --- a/Source/Plugins/Plugin_VideoOGL/Src/ProgramShaderCache.cpp +++ b/Source/Plugins/Plugin_VideoOGL/Src/ProgramShaderCache.cpp @@ -121,18 +121,10 @@ namespace OGL // Need to get some attribute locations if(uid.uid.vsid != 0) // We have no vertex Shader { - entry.program.attrLoc[0] = glGetAttribLocation(entry.program.glprogid, "rawnorm1"); - entry.program.attrLoc[1] = glGetAttribLocation(entry.program.glprogid, "rawnorm2"); - entry.program.attrLoc[2] = glGetAttribLocation(entry.program.glprogid, "fposmtx"); - if(entry.program.attrLoc[0] > 0) - glEnableVertexAttribArray(entry.program.attrLoc[0]); - if(entry.program.attrLoc[1] > 0) - glEnableVertexAttribArray(entry.program.attrLoc[1]); - if(entry.program.attrLoc[2] > 0) - glEnableVertexAttribArray(entry.program.attrLoc[2]); + glBindAttribLocation(entry.program.glprogid, SHADER_NORM1_ATTRIB, "rawnorm1"); + glBindAttribLocation(entry.program.glprogid, SHADER_NORM2_ATTRIB, "rawnorm2"); + glBindAttribLocation(entry.program.glprogid, SHADER_POSMTX_ATTRIB, "fposmtx"); } - else - entry.program.attrLoc[0] = entry.program.attrLoc[1] = entry.program.attrLoc[2] = 0; pshaders[ShaderPair] = entry; @@ -154,10 +146,6 @@ namespace OGL GLuint ProgramShaderCache::GetCurrentProgram(void) { return CurrentProgram; } - GLint ProgramShaderCache::GetAttr(int num) - { - return pshaders[CurrentShaderProgram].program.attrLoc[num]; - } PROGRAMSHADER ProgramShaderCache::GetShaderProgram(void) { return pshaders[CurrentShaderProgram].program; diff --git a/Source/Plugins/Plugin_VideoOGL/Src/ProgramShaderCache.h b/Source/Plugins/Plugin_VideoOGL/Src/ProgramShaderCache.h index 26d5735ce6..4830f28849 100644 --- a/Source/Plugins/Plugin_VideoOGL/Src/ProgramShaderCache.h +++ b/Source/Plugins/Plugin_VideoOGL/Src/ProgramShaderCache.h @@ -72,7 +72,6 @@ struct PROGRAMSHADER PROGRAMSHADER() : glprogid(0), vsid(0), psid(0){} GLuint glprogid; // opengl program id GLuint vsid, psid; - GLint attrLoc[3]; GLint UniformLocations[NUM_UNIFORMS]; }; @@ -100,7 +99,6 @@ class ProgramShaderCache public: static PROGRAMSHADER GetShaderProgram(void); - static GLint GetAttr(int num); static void SetBothShaders(GLuint PS, GLuint VS); static GLuint GetCurrentProgram(void); From e85a3d68b02b61f607637dddffd575ab99ba2898 Mon Sep 17 00:00:00 2001 From: Ryan Houdek Date: Sun, 11 Dec 2011 07:02:13 -0600 Subject: [PATCH 45/83] Looks like we make use of fmod, make a GLSL function for it! --- Source/Core/VideoCommon/Src/PixelShaderGen.cpp | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/Source/Core/VideoCommon/Src/PixelShaderGen.cpp b/Source/Core/VideoCommon/Src/PixelShaderGen.cpp index 940a34f537..5f66dfb6e9 100644 --- a/Source/Core/VideoCommon/Src/PixelShaderGen.cpp +++ b/Source/Core/VideoCommon/Src/PixelShaderGen.cpp @@ -573,6 +573,15 @@ const char *GeneratePixelShaderCode(DSTALPHA_MODE dstAlphaMode, API_TYPE ApiType WRITE(p, "#define saturate(x) clamp(x, 0.0f, 1.0f)\n"); WRITE(p, "#define lerp(x, y, z) mix(x, y, z)\n"); + // A function here + // Fmod implementation gleaned from Nvidia + // At http://http.developer.nvidia.com/Cg/fmod.html + WRITE(p, "float fmod( float x, float y )\n"); + WRITE(p, "{\n"); + WRITE(p, "float z = fract( abs( x / y) ) * abs( y );\n"); + WRITE(p, "return (x < 0) ? -z : z;\n"); + WRITE(p, "}\n"); + for (int i = 0; i < 8; ++i) WRITE(p, "%suniform sampler2D samp%d;\n", WriteBinding(ApiType, i), i); From 9ff48ac6eb92a7a9d46705c672097ac29a8c4e69 Mon Sep 17 00:00:00 2001 From: Ryan Houdek Date: Thu, 15 Dec 2011 15:48:21 -0600 Subject: [PATCH 46/83] Make sure not to try and bind UBO locations when it isn't supported --- Source/Plugins/Plugin_VideoOGL/Src/ProgramShaderCache.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Source/Plugins/Plugin_VideoOGL/Src/ProgramShaderCache.cpp b/Source/Plugins/Plugin_VideoOGL/Src/ProgramShaderCache.cpp index 2f6aa691de..5d00ce2529 100644 --- a/Source/Plugins/Plugin_VideoOGL/Src/ProgramShaderCache.cpp +++ b/Source/Plugins/Plugin_VideoOGL/Src/ProgramShaderCache.cpp @@ -101,7 +101,7 @@ namespace OGL // points statically set in the shader source // We should only need these two functions when we don't support binding but do support UBO // Driver Bug? Nvidia GTX 570, 290.xx Driver, Linux x64 - //if(!g_ActiveConfig.backend_info.bSupportsGLSLBinding) + if (!g_ActiveConfig.backend_info.bSupportsGLSLUBO) { glUniformBlockBinding( entry.program.glprogid, 0, 1 ); glUniformBlockBinding( entry.program.glprogid, 1, 2 ); From 800e1c9e098adb030180bda9e12420a352ad8750 Mon Sep 17 00:00:00 2001 From: Ryan Houdek Date: Fri, 16 Dec 2011 00:00:08 -0600 Subject: [PATCH 47/83] Let compiling work on OSX. --- Source/Plugins/Plugin_VideoOGL/SConscript | 1 + 1 file changed, 1 insertion(+) diff --git a/Source/Plugins/Plugin_VideoOGL/SConscript b/Source/Plugins/Plugin_VideoOGL/SConscript index 4b601fca83..46c07d6266 100644 --- a/Source/Plugins/Plugin_VideoOGL/SConscript +++ b/Source/Plugins/Plugin_VideoOGL/SConscript @@ -10,6 +10,7 @@ files = [ 'Src/NativeVertexFormat.cpp', 'Src/PixelShaderCache.cpp', 'Src/PostProcessing.cpp', + 'Src/ProgramShaderCache.cpp', 'Src/RasterFont.cpp', 'Src/Render.cpp', 'Src/TextureCache.cpp', From fe6fb553896ff47cfdebeae9db29d212b6938082 Mon Sep 17 00:00:00 2001 From: Ryan Houdek Date: Fri, 16 Dec 2011 23:18:24 -0600 Subject: [PATCH 48/83] Add a GUI option to use GLSL shaders. Also fix a small typo. --- Source/Core/DolphinWX/Src/VideoConfigDiag.cpp | 12 ++++++++++++ .../Plugin_VideoOGL/Src/ProgramShaderCache.cpp | 2 +- 2 files changed, 13 insertions(+), 1 deletion(-) diff --git a/Source/Core/DolphinWX/Src/VideoConfigDiag.cpp b/Source/Core/DolphinWX/Src/VideoConfigDiag.cpp index 45e239a835..618831ee81 100644 --- a/Source/Core/DolphinWX/Src/VideoConfigDiag.cpp +++ b/Source/Core/DolphinWX/Src/VideoConfigDiag.cpp @@ -118,6 +118,7 @@ wxString use_ffv1_desc = wxTRANSLATE("Encode frame dumps using the FFV1 codec.\n #endif wxString free_look_desc = wxTRANSLATE("This feature allows you to change the game's camera.\nHold the right mouse button and move the mouse to pan the camera around. Hold SHIFT and press one of the WASD keys to move the camera by a certain step distance (SHIFT+0 to move faster and SHIFT+9 to move slower). Press SHIFT+R to reset the camera.\n\nIf unsure, leave this unchecked."); wxString crop_desc = wxTRANSLATE("Crop the picture from 4:3 to 5:4 or from 16:9 to 16:10.\n\nIf unsure, leave this unchecked."); +wxString GLSL_desc = wxTRANSLATE("Uses GLSL shaders in place of Nvidia CG Shaders\n\nIf unsure, leave this unchecked."); wxString opencl_desc = wxTRANSLATE("[EXPERIMENTAL]\nAims to speed up emulation by offloading texture decoding to the GPU using the OpenCL framework.\nHowever, right now it's known to cause texture defects in various games. Also it's slower than regular CPU texture decoding in most cases.\n\nIf unsure, leave this unchecked."); wxString dlc_desc = wxTRANSLATE("[EXPERIMENTAL]\nSpeeds up emulation a bit by caching display lists.\nPossibly causes issues though.\n\nIf unsure, leave this unchecked."); wxString omp_desc = wxTRANSLATE("Use multiple threads to decode textures.\nMight result in a speedup (especially on CPUs with more than two cores).\n\nIf unsure, leave this unchecked."); @@ -578,6 +579,17 @@ VideoConfigDiag::VideoConfigDiag(wxWindow* parent, const std::string &title, con szr_misc->Add(cb_prog_scan); } + // GLSL Option + { + if(strstr(choice_backend->GetString(choice_backend->GetSelection()).ToAscii(), "OpenGL") != NULL) + { + wxCheckBox* const cb_GLSL = CreateCheckBox(page_advanced, _("Use GLSL Shaders"), wxGetTranslation(GLSL_desc), vconfig.bUseGLSL); + szr_misc->Add(cb_GLSL); + if (Core::GetState() != Core::CORE_UNINITIALIZED) + cb_GLSL->Disable(); + } + } + wxStaticBoxSizer* const group_misc = new wxStaticBoxSizer(wxVERTICAL, page_advanced, _("Misc")); szr_advanced->Add(group_misc, 0, wxEXPAND | wxALL, 5); diff --git a/Source/Plugins/Plugin_VideoOGL/Src/ProgramShaderCache.cpp b/Source/Plugins/Plugin_VideoOGL/Src/ProgramShaderCache.cpp index 5d00ce2529..98efbb000b 100644 --- a/Source/Plugins/Plugin_VideoOGL/Src/ProgramShaderCache.cpp +++ b/Source/Plugins/Plugin_VideoOGL/Src/ProgramShaderCache.cpp @@ -101,7 +101,7 @@ namespace OGL // points statically set in the shader source // We should only need these two functions when we don't support binding but do support UBO // Driver Bug? Nvidia GTX 570, 290.xx Driver, Linux x64 - if (!g_ActiveConfig.backend_info.bSupportsGLSLUBO) + if (g_ActiveConfig.backend_info.bSupportsGLSLUBO) { glUniformBlockBinding( entry.program.glprogid, 0, 1 ); glUniformBlockBinding( entry.program.glprogid, 1, 2 ); From 73c3f198f4188629eb3bcac123f8516135a86a4c Mon Sep 17 00:00:00 2001 From: Ryan Houdek Date: Fri, 16 Dec 2011 23:37:22 -0600 Subject: [PATCH 49/83] Only delete this buffer if we support it. --- .../Plugins/Plugin_VideoOGL/Src/ProgramShaderCache.cpp | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/Source/Plugins/Plugin_VideoOGL/Src/ProgramShaderCache.cpp b/Source/Plugins/Plugin_VideoOGL/Src/ProgramShaderCache.cpp index 98efbb000b..c90f151421 100644 --- a/Source/Plugins/Plugin_VideoOGL/Src/ProgramShaderCache.cpp +++ b/Source/Plugins/Plugin_VideoOGL/Src/ProgramShaderCache.cpp @@ -186,9 +186,12 @@ namespace OGL iter->second.Destroy(); pshaders.clear(); - glBindBuffer(GL_UNIFORM_BUFFER, 0); - glDeleteBuffers(1, &s_ps_vs_ubo); - s_ps_vs_ubo = 0; + if (g_ActiveConfig.backend_info.bSupportsGLSLUBO) + { + glBindBuffer(GL_UNIFORM_BUFFER, 0); + glDeleteBuffers(1, &s_ps_vs_ubo); + s_ps_vs_ubo = 0; + } } } From b837ae25fc9db2ae2fa9708038b1db8733a13020 Mon Sep 17 00:00:00 2001 From: Ryan Houdek Date: Sat, 17 Dec 2011 01:06:55 -0600 Subject: [PATCH 50/83] Set Sampler values at program make time instead of every frame. Fix an issue when The user had UBO support but not Binding support. --- .../Plugin_VideoOGL/Src/PixelShaderCache.cpp | 217 ++++++------------ .../Plugin_VideoOGL/Src/PixelShaderCache.h | 4 - .../Src/ProgramShaderCache.cpp | 10 +- .../Plugin_VideoOGL/Src/TextureCache.cpp | 3 - .../Plugin_VideoOGL/Src/TextureConverter.cpp | 2 - .../Plugin_VideoOGL/Src/VertexManager.cpp | 19 -- 6 files changed, 79 insertions(+), 176 deletions(-) diff --git a/Source/Plugins/Plugin_VideoOGL/Src/PixelShaderCache.cpp b/Source/Plugins/Plugin_VideoOGL/Src/PixelShaderCache.cpp index 9556c12995..6b171a3f03 100644 --- a/Source/Plugins/Plugin_VideoOGL/Src/PixelShaderCache.cpp +++ b/Source/Plugins/Plugin_VideoOGL/Src/PixelShaderCache.cpp @@ -119,141 +119,81 @@ void PixelShaderCache::Init() if(g_ActiveConfig.bUseGLSL) { char pmatrixprog[2048]; - if (g_ActiveConfig.backend_info.bSupportsGLSLBinding) - { - sprintf(pmatrixprog, "#version 330 compatibility\n" - "#extension GL_ARB_texture_rectangle : enable\n" - "#extension GL_ARB_shading_language_420pack : enable\n" - "layout(binding = 0) uniform sampler2DRect samp0;\n" - "%s\n" - "%svec4 "I_COLORS"[7];\n" - "%s\n" - "void main(){\n" - "vec4 Temp0, Temp1;\n" - "vec4 K0 = vec4(0.5, 0.5, 0.5, 0.5);\n" - "Temp0 = texture2DRect(samp0, gl_TexCoord[0].xy);\n" - "Temp0 = Temp0 * "I_COLORS"[%d];\n" - "Temp0 = Temp0 + K0;\n" - "Temp0 = floor(Temp0);\n" - "Temp0 = Temp0 * "I_COLORS"[%d];\n" - "Temp1.x = dot(Temp0, "I_COLORS"[%d]);\n" - "Temp1.y = dot(Temp0, "I_COLORS"[%d]);\n" - "Temp1.z = dot(Temp0, "I_COLORS"[%d]);\n" - "Temp1.w = dot(Temp0, "I_COLORS"[%d]);\n" - "gl_FragData[0] = Temp1 + "I_COLORS"[%d];\n" - "}\n", - g_ActiveConfig.backend_info.bSupportsGLSLUBO ? "layout(std140, binding = 1) uniform PSBlock {" : "", - g_ActiveConfig.backend_info.bSupportsGLSLUBO ? "" : "uniform ", - g_ActiveConfig.backend_info.bSupportsGLSLUBO ? "};" : "", - C_COLORS+5, C_COLORS+6, C_COLORS, C_COLORS+1, C_COLORS+2, C_COLORS+3, C_COLORS+4); - } - else - { - sprintf(pmatrixprog, "#version 120\n" - "#extension GL_ARB_texture_rectangle : enable\n" - "uniform sampler2DRect samp0;\n" - "%s\n" - "%svec4 "I_COLORS"[7];\n" - "%s\n" - "void main(){\n" - "vec4 Temp0, Temp1;\n" - "vec4 K0 = vec4(0.5, 0.5, 0.5, 0.5);\n" - "Temp0 = texture2DRect(samp0, gl_TexCoord[0].xy);\n" - "Temp0 = Temp0 * "I_COLORS"[%d];\n" - "Temp0 = Temp0 + K0;\n" - "Temp0 = floor(Temp0);\n" - "Temp0 = Temp0 * "I_COLORS"[%d];\n" - "Temp1.x = dot(Temp0, "I_COLORS"[%d]);\n" - "Temp1.y = dot(Temp0, "I_COLORS"[%d]);\n" - "Temp1.z = dot(Temp0, "I_COLORS"[%d]);\n" - "Temp1.w = dot(Temp0, "I_COLORS"[%d]);\n" - "gl_FragData[0] = Temp1 + "I_COLORS"[%d];\n" - "}\n", - g_ActiveConfig.backend_info.bSupportsGLSLUBO ? "layout(std140) uniform PSBlock {" : "", - g_ActiveConfig.backend_info.bSupportsGLSLUBO ? "" : "uniform ", - g_ActiveConfig.backend_info.bSupportsGLSLUBO ? "};" : "", - C_COLORS+5, C_COLORS+6, C_COLORS, C_COLORS+1, C_COLORS+2, C_COLORS+3, C_COLORS+4); - } + sprintf(pmatrixprog, "#version %s\n" + "#extension GL_ARB_texture_rectangle : enable\n" + "%s\n" + "%suniform sampler2DRect samp0;\n" + "%s\n" + "%svec4 "I_COLORS"[7];\n" + "%s\n" + "void main(){\n" + "vec4 Temp0, Temp1;\n" + "vec4 K0 = vec4(0.5, 0.5, 0.5, 0.5);\n" + "Temp0 = texture2DRect(samp0, gl_TexCoord[0].xy);\n" + "Temp0 = Temp0 * "I_COLORS"[%d];\n" + "Temp0 = Temp0 + K0;\n" + "Temp0 = floor(Temp0);\n" + "Temp0 = Temp0 * "I_COLORS"[%d];\n" + "Temp1.x = dot(Temp0, "I_COLORS"[%d]);\n" + "Temp1.y = dot(Temp0, "I_COLORS"[%d]);\n" + "Temp1.z = dot(Temp0, "I_COLORS"[%d]);\n" + "Temp1.w = dot(Temp0, "I_COLORS"[%d]);\n" + "gl_FragData[0] = Temp1 + "I_COLORS"[%d];\n" + "}\n", + (g_ActiveConfig.backend_info.bSupportsGLSLUBO || g_ActiveConfig.backend_info.bSupportsGLSLBinding) ? "330 compatibility" : "120", + g_ActiveConfig.backend_info.bSupportsGLSLBinding ? "#extension GL_ARB_shading_language_420pack : enable" : "", + g_ActiveConfig.backend_info.bSupportsGLSLBinding ? "layout(binding = 0) " : "", + g_ActiveConfig.backend_info.bSupportsGLSLUBO ? "layout(std140) uniform PSBlock {" : "", + g_ActiveConfig.backend_info.bSupportsGLSLUBO ? "" : "uniform ", + g_ActiveConfig.backend_info.bSupportsGLSLUBO ? "};" : "", + C_COLORS+5, C_COLORS+6, C_COLORS, C_COLORS+1, C_COLORS+2, C_COLORS+3, C_COLORS+4); + + if (!PixelShaderCache::CompilePixelShader(s_ColorMatrixProgram, pmatrixprog)) { ERROR_LOG(VIDEO, "Failed to create color matrix fragment program"); s_ColorMatrixProgram.Destroy(); } - if (g_ActiveConfig.backend_info.bSupportsGLSLBinding) - { - sprintf(pmatrixprog, "#version 330 compatibility\n" - "#extension GL_ARB_texture_rectangle : enable\n" - "#extension GL_ARB_shading_language_420pack : enable\n" - "layout(binding = 0) uniform sampler2DRect samp0;\n" - "%s\n" - "%svec4 "I_COLORS"[5];\n" - "%s\n" - "void main(){\n" - "vec4 R0, R1, R2;\n" - "vec4 K0 = vec4(255.99998474121, 0.003921568627451, 256.0, 0.0);\n" - "vec4 K1 = vec4(15.0, 0.066666666666, 0.0, 0.0);\n" - "R2 = texture2DRect(samp0, gl_TexCoord[0].xy);\n" - "R0.x = R2.x * K0.x;\n" - "R0.x = floor(R0).x;\n" - "R0.yzw = (R0 - R0.x).yzw;\n" - "R0.yzw = (R0 * K0.z).yzw;\n" - "R0.y = floor(R0).y;\n" - "R0.zw = (R0 - R0.y).zw;\n" - "R0.zw = (R0 * K0.z).zw;\n" - "R0.z = floor(R0).z;\n" - "R0.w = R0.x;\n" - "R0 = R0 * K0.y;\n" - "R0.w = (R0 * K1.x).w;\n" - "R0.w = floor(R0).w;\n" - "R0.w = (R0 * K1.y).w;\n" - "R1.x = dot(R0, "I_COLORS"[%d]);\n" - "R1.y = dot(R0, "I_COLORS"[%d]);\n" - "R1.z = dot(R0, "I_COLORS"[%d]);\n" - "R1.w = dot(R0, "I_COLORS"[%d]);\n" - "gl_FragData[0] = R1 * "I_COLORS"[%d];\n" - "}\n", - g_ActiveConfig.backend_info.bSupportsGLSLUBO ? "layout(std140, binding = 1) uniform PSBlock {" : "", - g_ActiveConfig.backend_info.bSupportsGLSLUBO ? "" : "uniform ", - g_ActiveConfig.backend_info.bSupportsGLSLUBO ? "};" : "", - C_COLORS, C_COLORS+1, C_COLORS+2, C_COLORS+3, C_COLORS+4); - } - else - { - sprintf(pmatrixprog, "#version 120\n" - "#extension GL_ARB_texture_rectangle : enable\n" - "uniform sampler2DRect samp0;\n" - "%s\n" - "%svec4 "I_COLORS"[5];\n" - "%s\n" - "void main(){\n" - "vec4 R0, R1, R2;\n" - "vec4 K0 = vec4(255.99998474121, 0.003921568627451, 256.0, 0.0);\n" - "vec4 K1 = vec4(15.0, 0.066666666666, 0.0, 0.0);\n" - "R2 = texture2DRect(samp0, gl_TexCoord[0].xy);\n" - "R0.x = R2.x * K0.x;\n" - "R0.x = floor(R0).x;\n" - "R0.yzw = (R0 - R0.x).yzw;\n" - "R0.yzw = (R0 * K0.z).yzw;\n" - "R0.y = floor(R0).y;\n" - "R0.zw = (R0 - R0.y).zw;\n" - "R0.zw = (R0 * K0.z).zw;\n" - "R0.z = floor(R0).z;\n" - "R0.w = R0.x;\n" - "R0 = R0 * K0.y;\n" - "R0.w = (R0 * K1.x).w;\n" - "R0.w = floor(R0).w;\n" - "R0.w = (R0 * K1.y).w;\n" - "R1.x = dot(R0, "I_COLORS"[%d]);\n" - "R1.y = dot(R0, "I_COLORS"[%d]);\n" - "R1.z = dot(R0, "I_COLORS"[%d]);\n" - "R1.w = dot(R0, "I_COLORS"[%d]);\n" - "gl_FragData[0] = R1 * "I_COLORS"[%d];\n" - "}\n", - g_ActiveConfig.backend_info.bSupportsGLSLUBO ? "layout(std140) uniform PSBlock {" : "", - g_ActiveConfig.backend_info.bSupportsGLSLUBO ? "" : "uniform ", - g_ActiveConfig.backend_info.bSupportsGLSLUBO ? "};" : "", - C_COLORS, C_COLORS+1, C_COLORS+2, C_COLORS+3, C_COLORS+4); - } + + sprintf(pmatrixprog, "#version %s\n" + "#extension GL_ARB_texture_rectangle : enable\n" + "%s\n" + "%suniform sampler2DRect samp0;\n" + "%s\n" + "%svec4 "I_COLORS"[5];\n" + "%s\n" + "void main(){\n" + "vec4 R0, R1, R2;\n" + "vec4 K0 = vec4(255.99998474121, 0.003921568627451, 256.0, 0.0);\n" + "vec4 K1 = vec4(15.0, 0.066666666666, 0.0, 0.0);\n" + "R2 = texture2DRect(samp0, gl_TexCoord[0].xy);\n" + "R0.x = R2.x * K0.x;\n" + "R0.x = floor(R0).x;\n" + "R0.yzw = (R0 - R0.x).yzw;\n" + "R0.yzw = (R0 * K0.z).yzw;\n" + "R0.y = floor(R0).y;\n" + "R0.zw = (R0 - R0.y).zw;\n" + "R0.zw = (R0 * K0.z).zw;\n" + "R0.z = floor(R0).z;\n" + "R0.w = R0.x;\n" + "R0 = R0 * K0.y;\n" + "R0.w = (R0 * K1.x).w;\n" + "R0.w = floor(R0).w;\n" + "R0.w = (R0 * K1.y).w;\n" + "R1.x = dot(R0, "I_COLORS"[%d]);\n" + "R1.y = dot(R0, "I_COLORS"[%d]);\n" + "R1.z = dot(R0, "I_COLORS"[%d]);\n" + "R1.w = dot(R0, "I_COLORS"[%d]);\n" + "gl_FragData[0] = R1 * "I_COLORS"[%d];\n" + "}\n", + (g_ActiveConfig.backend_info.bSupportsGLSLUBO || g_ActiveConfig.backend_info.bSupportsGLSLBinding) ? "330 compatibility" : "120", + g_ActiveConfig.backend_info.bSupportsGLSLBinding ? "#extension GL_ARB_shading_language_420pack : enable" : "", + g_ActiveConfig.backend_info.bSupportsGLSLBinding ? "layout(binding = 0) " : "", + g_ActiveConfig.backend_info.bSupportsGLSLUBO ? "layout(std140) uniform PSBlock {" : "", + g_ActiveConfig.backend_info.bSupportsGLSLUBO ? "" : "uniform ", + g_ActiveConfig.backend_info.bSupportsGLSLUBO ? "};" : "", + C_COLORS, C_COLORS+1, C_COLORS+2, C_COLORS+3, C_COLORS+4); + if (!PixelShaderCache::CompilePixelShader(s_DepthMatrixProgram, pmatrixprog)) { ERROR_LOG(VIDEO, "Failed to create depth matrix fragment program"); @@ -492,23 +432,6 @@ bool CompileGLSLPixelShader(FRAGMENTSHADER& ps, const char* pstrprogram) ps.bGLSL = true; return true; } -void PixelShaderCache::SetPSSampler(const char * name, unsigned int Tex) -{ - if (g_ActiveConfig.backend_info.bSupportsGLSLBinding) - return; - PROGRAMSHADER tmp = ProgramShaderCache::GetShaderProgram(); - for (int a = 0; a < NUM_UNIFORMS; ++a) - if (!strcmp(name, UniformNames[a])) - { - if(tmp.UniformLocations[a] == -1) - return; - else - { - glUniform1i(tmp.UniformLocations[a], Tex); - return; - } - } -} void SetPSConstant4fvByName(const char * name, unsigned int offset, const float *f, const unsigned int count = 1) { PROGRAMSHADER tmp = ProgramShaderCache::GetShaderProgram(); diff --git a/Source/Plugins/Plugin_VideoOGL/Src/PixelShaderCache.h b/Source/Plugins/Plugin_VideoOGL/Src/PixelShaderCache.h index 6547b95234..b0328f336c 100644 --- a/Source/Plugins/Plugin_VideoOGL/Src/PixelShaderCache.h +++ b/Source/Plugins/Plugin_VideoOGL/Src/PixelShaderCache.h @@ -78,8 +78,6 @@ class PixelShaderCache public: static void Init(); static void Shutdown(); - // This is a GLSL only function - static void SetPSSampler(const char * name, unsigned int Tex); static FRAGMENTSHADER* SetShader(DSTALPHA_MODE dstAlphaMode, u32 components); static bool CompilePixelShader(FRAGMENTSHADER& ps, const char* pstrprogram); @@ -88,8 +86,6 @@ public: static GLuint GetDepthMatrixProgram(); - static bool SupportsBinding(); - static void SetCurrentShader(GLuint Shader); static void DisableShader(); diff --git a/Source/Plugins/Plugin_VideoOGL/Src/ProgramShaderCache.cpp b/Source/Plugins/Plugin_VideoOGL/Src/ProgramShaderCache.cpp index c90f151421..14542bf68f 100644 --- a/Source/Plugins/Plugin_VideoOGL/Src/ProgramShaderCache.cpp +++ b/Source/Plugins/Plugin_VideoOGL/Src/ProgramShaderCache.cpp @@ -114,9 +114,17 @@ namespace OGL //For some reason this fails on my hardware //glGetUniformIndices(entry.program.glprogid, NUM_UNIFORMS, UniformNames, entry.program.UniformLocations); //Got to do it this crappy way. - if(!g_ActiveConfig.backend_info.bSupportsGLSLUBO) + if (!g_ActiveConfig.backend_info.bSupportsGLSLUBO) for(int a = 0; a < NUM_UNIFORMS; ++a) entry.program.UniformLocations[a] = glGetUniformLocation(entry.program.glprogid, UniformNames[a]); + else if (!g_ActiveConfig.backend_info.bSupportsGLSLBinding) + for(int a = 0; a < 8; ++a) + { + // Still need to get sampler locations since we aren't binding them statically in the shaders + entry.program.UniformLocations[a] = glGetUniformLocation(entry.program.glprogid, UniformNames[a]); + if(entry.program.UniformLocations[a] != -1) + glUniform1i(entry.program.UniformLocations[a], a); + } // Need to get some attribute locations if(uid.uid.vsid != 0) // We have no vertex Shader diff --git a/Source/Plugins/Plugin_VideoOGL/Src/TextureCache.cpp b/Source/Plugins/Plugin_VideoOGL/Src/TextureCache.cpp index 2fc02d867a..1a4b8bb55f 100644 --- a/Source/Plugins/Plugin_VideoOGL/Src/TextureCache.cpp +++ b/Source/Plugins/Plugin_VideoOGL/Src/TextureCache.cpp @@ -298,10 +298,7 @@ void TextureCache::TCacheEntry::FromRenderTarget(u32 dstAddr, unsigned int dstFo glViewport(0, 0, virtualW, virtualH); if(g_ActiveConfig.bUseGLSL) - { ProgramShaderCache::SetBothShaders((srcFormat == PIXELFMT_Z24) ? PixelShaderCache::GetDepthMatrixProgram() : PixelShaderCache::GetColorMatrixProgram(), 0); - PixelShaderCache::SetPSSampler("samp0", 0); - } else PixelShaderCache::SetCurrentShader((srcFormat == PIXELFMT_Z24) ? PixelShaderCache::GetDepthMatrixProgram() : PixelShaderCache::GetColorMatrixProgram()); PixelShaderManager::SetColorMatrix(colmat); // set transformation diff --git a/Source/Plugins/Plugin_VideoOGL/Src/TextureConverter.cpp b/Source/Plugins/Plugin_VideoOGL/Src/TextureConverter.cpp index 213d92098a..0b0cae3aa3 100644 --- a/Source/Plugins/Plugin_VideoOGL/Src/TextureConverter.cpp +++ b/Source/Plugins/Plugin_VideoOGL/Src/TextureConverter.cpp @@ -314,8 +314,6 @@ void EncodeToRamUsingShader(GLuint srcTexture, const TargetRectangle& sourceRc, } GL_REPORT_ERRORD(); - if(g_ActiveConfig.bUseGLSL) - PixelShaderCache::SetPSSampler("samp0", 0); glViewport(0, 0, (GLsizei)dstWidth, (GLsizei)dstHeight); diff --git a/Source/Plugins/Plugin_VideoOGL/Src/VertexManager.cpp b/Source/Plugins/Plugin_VideoOGL/Src/VertexManager.cpp index fff8263569..cb8e010794 100644 --- a/Source/Plugins/Plugin_VideoOGL/Src/VertexManager.cpp +++ b/Source/Plugins/Plugin_VideoOGL/Src/VertexManager.cpp @@ -219,16 +219,6 @@ void VertexManager::vFlush() if (g_nativeVertexFmt) g_nativeVertexFmt->SetupVertexPointers(); GL_REPORT_ERRORD(); - if(g_ActiveConfig.bUseGLSL) - for (int i = 0; i < 8; i++) - { - if (usedtextures & (1 << i)) - { - char tmp[16]; - sprintf(tmp, "samp%d", i); // Bake this in to something so we don't have to sprintf? - PixelShaderCache::SetPSSampler(tmp, i); - } - } Draw(); @@ -243,15 +233,6 @@ void VertexManager::vFlush() PixelShaderManager::SetConstants(); // Need to set these again, if we don't support UBO if (g_nativeVertexFmt) g_nativeVertexFmt->SetupVertexPointers(); - for (int i = 0; i < 8; i++) - { - if (usedtextures & (1 << i)) - { - char tmp[16]; - sprintf(tmp, "samp%d", i); // Bake this in to something so we don't have to sprintf? - PixelShaderCache::SetPSSampler(tmp, i); - } - } } else if (ps) PixelShaderCache::SetCurrentShader(ps->glprogid); From 4342efe71231bbf7155f6ed3df1b117a137f3f0d Mon Sep 17 00:00:00 2001 From: Ryan Houdek Date: Sat, 17 Dec 2011 01:17:11 -0600 Subject: [PATCH 51/83] Some shader programs don't come with Vertex shaders attached, don't try to bind a Vertex Uniform block then. --- Source/Plugins/Plugin_VideoOGL/Src/ProgramShaderCache.cpp | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/Source/Plugins/Plugin_VideoOGL/Src/ProgramShaderCache.cpp b/Source/Plugins/Plugin_VideoOGL/Src/ProgramShaderCache.cpp index 14542bf68f..7ae733bcf1 100644 --- a/Source/Plugins/Plugin_VideoOGL/Src/ProgramShaderCache.cpp +++ b/Source/Plugins/Plugin_VideoOGL/Src/ProgramShaderCache.cpp @@ -104,7 +104,8 @@ namespace OGL if (g_ActiveConfig.backend_info.bSupportsGLSLUBO) { glUniformBlockBinding( entry.program.glprogid, 0, 1 ); - glUniformBlockBinding( entry.program.glprogid, 1, 2 ); + if(uid.uid.vsid != 0) // Some things have no vertex shader + glUniformBlockBinding( entry.program.glprogid, 1, 2 ); } // We cache our uniform locations for now From 636d6a915d889ca2397d56c54659ec2b8c823248 Mon Sep 17 00:00:00 2001 From: Ryan Houdek Date: Sun, 18 Dec 2011 01:51:15 -0600 Subject: [PATCH 52/83] Remove this silliness --- Source/Plugins/Plugin_VideoOGL/Src/GLUtil.h | 10 ---------- Source/Plugins/Plugin_VideoOGL/Src/Render.cpp | 10 ---------- 2 files changed, 20 deletions(-) diff --git a/Source/Plugins/Plugin_VideoOGL/Src/GLUtil.h b/Source/Plugins/Plugin_VideoOGL/Src/GLUtil.h index 7d309dee38..b81d051a0f 100644 --- a/Source/Plugins/Plugin_VideoOGL/Src/GLUtil.h +++ b/Source/Plugins/Plugin_VideoOGL/Src/GLUtil.h @@ -121,14 +121,4 @@ extern CGcontext g_cgcontext; extern CGprofile g_cgvProf, g_cgfProf; #endif -// XXX: Dual-source blending in OpenGL does not work correctly yet. To make it -// work, we may need to use glBindFragDataLocation. To use that, we need to -// use GLSL shaders across the whole pipeline. Yikes! -#define USE_DUAL_SOURCE_BLEND - -// Can't get defined on my system for some reason... -#ifndef GL_ONE_MINUS_SRC1_ALPHA -#define GL_ONE_MINUS_SRC1_ALPHA 0x88FB -#endif - #endif // _GLINIT_H_ diff --git a/Source/Plugins/Plugin_VideoOGL/Src/Render.cpp b/Source/Plugins/Plugin_VideoOGL/Src/Render.cpp index 86452adebd..78b422eed2 100644 --- a/Source/Plugins/Plugin_VideoOGL/Src/Render.cpp +++ b/Source/Plugins/Plugin_VideoOGL/Src/Render.cpp @@ -938,11 +938,9 @@ void Renderer::SetBlendMode(bool forceUpdate) u32 changes = forceUpdate ? 0xFFFFFFFF : newval ^ s_blendMode; -#ifdef USE_DUAL_SOURCE_BLEND bool useDstAlpha = !g_ActiveConfig.bDstAlphaPass && bpmem.dstalpha.enable && bpmem.blendmode.alphaupdate && bpmem.zcontrol.pixel_format == PIXELFMT_RGBA6_Z24 && g_ActiveConfig.bUseGLSL; bool useDualSource = useDstAlpha; -#endif if (changes & 1) // blend enable change @@ -950,19 +948,14 @@ void Renderer::SetBlendMode(bool forceUpdate) if (changes & 4) { -#ifdef USE_DUAL_SOURCE_BLEND // subtract enable change GLenum equation = newval & 4 ? GL_FUNC_REVERSE_SUBTRACT : GL_FUNC_ADD; GLenum equationAlpha = useDualSource ? GL_FUNC_ADD : equation; glBlendEquationSeparate(equation, equationAlpha); -#else - glBlendEquation(newval & 4 ? GL_FUNC_REVERSE_SUBTRACT : GL_FUNC_ADD); -#endif } if (changes & 0x1F8) { -#ifdef USE_DUAL_SOURCE_BLEND GLenum srcFactor = glSrcFactors[(newval >> 3) & 7]; GLenum srcFactorAlpha = srcFactor; GLenum dstFactor = glDestFactors[(newval >> 6) & 7]; @@ -985,9 +978,6 @@ void Renderer::SetBlendMode(bool forceUpdate) // blend RGB change glBlendFuncSeparate(srcFactor, dstFactor, srcFactorAlpha, dstFactorAlpha); -#else - glBlendFunc(glSrcFactors[(newval >> 3) & 7], glDestFactors[(newval >> 6) & 7]); -#endif } s_blendMode = newval; From 3513dd7115d95331e294d03299b806227b2ee458 Mon Sep 17 00:00:00 2001 From: Ryan Houdek Date: Sun, 18 Dec 2011 21:06:28 -0600 Subject: [PATCH 53/83] Looky, OSX found an issue for me. --- Source/Core/VideoCommon/Src/PixelShaderGen.cpp | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/Source/Core/VideoCommon/Src/PixelShaderGen.cpp b/Source/Core/VideoCommon/Src/PixelShaderGen.cpp index 5f66dfb6e9..c376f55e6f 100644 --- a/Source/Core/VideoCommon/Src/PixelShaderGen.cpp +++ b/Source/Core/VideoCommon/Src/PixelShaderGen.cpp @@ -721,13 +721,13 @@ const char *GeneratePixelShaderCode(DSTALPHA_MODE dstAlphaMode, API_TYPE ApiType // wpos is in w of first 4 texcoords if(g_ActiveConfig.bEnablePixelLighting && g_ActiveConfig.backend_info.bSupportsPixelLighting) { - for (int i = 0; i < 8; ++i) - WRITE(p, " float4 uv%d = gl_TexCoord[%d];\n", i, i); + for (int i = 0; i < 8; ++i) + WRITE(p, " float4 uv%d = gl_TexCoord[%d];\n", i, i); } else { - for (unsigned int i = 0; i < xfregs.numTexGen.numTexGens; ++i) - WRITE(p, " float%d uv%d = gl_TexCoord[%d]%s;\n", i < 4 ? 4 : 3 , i, i, i < 4 ? ".xyz" : ""); + for (unsigned int i = 0; i < xfregs.numTexGen.numTexGens; ++i) + WRITE(p, " float%d uv%d = gl_TexCoord[%d]%s;\n", i < 4 ? 4 : 3 , i, i, i < 4 ? "" : ".xyz"); } } WRITE(p, "void main()\n{\n"); From 768a683f04cb17b5c668a8b55d7ab084686e845d Mon Sep 17 00:00:00 2001 From: Ryan Houdek Date: Sun, 18 Dec 2011 23:31:00 -0600 Subject: [PATCH 54/83] This thing fails when we don't support binding. Not 100% sure why I need to set this multiple times. --- Source/Plugins/Plugin_VideoOGL/Src/ProgramShaderCache.cpp | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/Source/Plugins/Plugin_VideoOGL/Src/ProgramShaderCache.cpp b/Source/Plugins/Plugin_VideoOGL/Src/ProgramShaderCache.cpp index 7ae733bcf1..b83e4cf0ac 100644 --- a/Source/Plugins/Plugin_VideoOGL/Src/ProgramShaderCache.cpp +++ b/Source/Plugins/Plugin_VideoOGL/Src/ProgramShaderCache.cpp @@ -77,6 +77,13 @@ namespace OGL { PCacheEntry &entry = iter->second; glUseProgram(entry.program.glprogid); + if (!g_ActiveConfig.backend_info.bSupportsGLSLBinding) + for(int a = 0; a < 8; ++a) + { + // Why do we need to set this again here when we don't support binding? + if(entry.program.UniformLocations[a] != -1) + glUniform1i(entry.program.UniformLocations[a], a); + } CurrentShaderProgram = ShaderPair; CurrentProgram = entry.program.glprogid; return; From 9ce17a43ec44430449034034335b6c9f96aab42d Mon Sep 17 00:00:00 2001 From: Ryan Houdek Date: Mon, 19 Dec 2011 00:15:07 -0600 Subject: [PATCH 55/83] There we go, actually found the issue. --- .../Plugins/Plugin_VideoOGL/Src/ProgramShaderCache.cpp | 10 +--------- 1 file changed, 1 insertion(+), 9 deletions(-) diff --git a/Source/Plugins/Plugin_VideoOGL/Src/ProgramShaderCache.cpp b/Source/Plugins/Plugin_VideoOGL/Src/ProgramShaderCache.cpp index b83e4cf0ac..d38a4607e4 100644 --- a/Source/Plugins/Plugin_VideoOGL/Src/ProgramShaderCache.cpp +++ b/Source/Plugins/Plugin_VideoOGL/Src/ProgramShaderCache.cpp @@ -77,13 +77,6 @@ namespace OGL { PCacheEntry &entry = iter->second; glUseProgram(entry.program.glprogid); - if (!g_ActiveConfig.backend_info.bSupportsGLSLBinding) - for(int a = 0; a < 8; ++a) - { - // Why do we need to set this again here when we don't support binding? - if(entry.program.UniformLocations[a] != -1) - glUniform1i(entry.program.UniformLocations[a], a); - } CurrentShaderProgram = ShaderPair; CurrentProgram = entry.program.glprogid; return; @@ -125,11 +118,10 @@ namespace OGL if (!g_ActiveConfig.backend_info.bSupportsGLSLUBO) for(int a = 0; a < NUM_UNIFORMS; ++a) entry.program.UniformLocations[a] = glGetUniformLocation(entry.program.glprogid, UniformNames[a]); - else if (!g_ActiveConfig.backend_info.bSupportsGLSLBinding) + if (!g_ActiveConfig.backend_info.bSupportsGLSLBinding) for(int a = 0; a < 8; ++a) { // Still need to get sampler locations since we aren't binding them statically in the shaders - entry.program.UniformLocations[a] = glGetUniformLocation(entry.program.glprogid, UniformNames[a]); if(entry.program.UniformLocations[a] != -1) glUniform1i(entry.program.UniformLocations[a], a); } From f6b33cf0be7420c9dc09010ecd743d7d9a7004b5 Mon Sep 17 00:00:00 2001 From: Ryan Houdek Date: Mon, 19 Dec 2011 19:10:05 -0600 Subject: [PATCH 56/83] Add a define back so we work without GLEW 1.6, Also I missed a line when I was fixing binding sampler locations last night when I was tired. --- Source/Plugins/Plugin_VideoOGL/Src/GLUtil.h | 5 +++++ Source/Plugins/Plugin_VideoOGL/Src/ProgramShaderCache.cpp | 3 ++- 2 files changed, 7 insertions(+), 1 deletion(-) diff --git a/Source/Plugins/Plugin_VideoOGL/Src/GLUtil.h b/Source/Plugins/Plugin_VideoOGL/Src/GLUtil.h index b81d051a0f..bfde884ccd 100644 --- a/Source/Plugins/Plugin_VideoOGL/Src/GLUtil.h +++ b/Source/Plugins/Plugin_VideoOGL/Src/GLUtil.h @@ -121,4 +121,9 @@ extern CGcontext g_cgcontext; extern CGprofile g_cgvProf, g_cgfProf; #endif +// Isn't defined if we aren't using GLEW 1.6 +#ifndef GL_ONE_MINUS_SRC1_ALPHA +#define GL_ONE_MINUS_SRC1_ALPHA 0x88FB +#endif + #endif // _GLINIT_H_ diff --git a/Source/Plugins/Plugin_VideoOGL/Src/ProgramShaderCache.cpp b/Source/Plugins/Plugin_VideoOGL/Src/ProgramShaderCache.cpp index d38a4607e4..dc4b4e9822 100644 --- a/Source/Plugins/Plugin_VideoOGL/Src/ProgramShaderCache.cpp +++ b/Source/Plugins/Plugin_VideoOGL/Src/ProgramShaderCache.cpp @@ -116,12 +116,13 @@ namespace OGL //glGetUniformIndices(entry.program.glprogid, NUM_UNIFORMS, UniformNames, entry.program.UniformLocations); //Got to do it this crappy way. if (!g_ActiveConfig.backend_info.bSupportsGLSLUBO) - for(int a = 0; a < NUM_UNIFORMS; ++a) + for(int a = 8; a < NUM_UNIFORMS; ++a) entry.program.UniformLocations[a] = glGetUniformLocation(entry.program.glprogid, UniformNames[a]); if (!g_ActiveConfig.backend_info.bSupportsGLSLBinding) for(int a = 0; a < 8; ++a) { // Still need to get sampler locations since we aren't binding them statically in the shaders + entry.program.UniformLocations[a] = glGetUniformLocation(entry.program.glprogid, UniformNames[a]); if(entry.program.UniformLocations[a] != -1) glUniform1i(entry.program.UniformLocations[a], a); } From dc134b9bacefb7eb2ba8d5b9a66f1c0c2b423238 Mon Sep 17 00:00:00 2001 From: Ryan Houdek Date: Tue, 20 Dec 2011 21:55:11 -0600 Subject: [PATCH 57/83] Move the GLSL extension checks over here so it is nicer. --- .../Plugins/Plugin_VideoOGL/Src/PixelShaderCache.cpp | 10 ---------- Source/Plugins/Plugin_VideoOGL/Src/Render.cpp | 11 +++++++++++ 2 files changed, 11 insertions(+), 10 deletions(-) diff --git a/Source/Plugins/Plugin_VideoOGL/Src/PixelShaderCache.cpp b/Source/Plugins/Plugin_VideoOGL/Src/PixelShaderCache.cpp index 6b171a3f03..9c7463442a 100644 --- a/Source/Plugins/Plugin_VideoOGL/Src/PixelShaderCache.cpp +++ b/Source/Plugins/Plugin_VideoOGL/Src/PixelShaderCache.cpp @@ -80,16 +80,6 @@ void PixelShaderCache::Init() pSetPSConstant4fv = SetGLSLPSConstant4fv; pSetMultiPSConstant4fv = SetMultiGLSLPSConstant4fv; pCompilePixelShader = CompileGLSLPixelShader; - // Should this be set here? - if (strstr((const char*)glGetString(GL_EXTENSIONS), "GL_ARB_shading_language_420pack") != NULL) - g_Config.backend_info.bSupportsGLSLBinding = true; - if (strstr((const char*)glGetString(GL_EXTENSIONS), "GL_ARB_uniform_buffer_object") != NULL) - g_Config.backend_info.bSupportsGLSLUBO = true; - - UpdateActiveConfig(); - OSD::AddMessage(StringFromFormat("Using GLSL. Supports Binding: %s UBOs: %s", - g_ActiveConfig.backend_info.bSupportsGLSLBinding ? "True" : "False", - g_ActiveConfig.backend_info.bSupportsGLSLUBO ? "True" : "False").c_str(), 5000); } else { diff --git a/Source/Plugins/Plugin_VideoOGL/Src/Render.cpp b/Source/Plugins/Plugin_VideoOGL/Src/Render.cpp index 78b422eed2..2e17429910 100644 --- a/Source/Plugins/Plugin_VideoOGL/Src/Render.cpp +++ b/Source/Plugins/Plugin_VideoOGL/Src/Render.cpp @@ -312,6 +312,17 @@ Renderer::Renderer() s_bHaveFramebufferBlit = strstr(ptoken, "GL_EXT_framebuffer_blit") != NULL; s_bHaveCoverageMSAA = strstr(ptoken, "GL_NV_framebuffer_multisample_coverage") != NULL; + + if (glewIsSupported("GL_ARB_shading_language_420pack")) + g_Config.backend_info.bSupportsGLSLBinding = true; + if (glewIsSupported("GL_ARB_uniform_buffer_object")) + g_Config.backend_info.bSupportsGLSLUBO = true; + + UpdateActiveConfig(); + OSD::AddMessage(StringFromFormat("Using GLSL. Supports Binding: %s UBOs: %s", + g_ActiveConfig.backend_info.bSupportsGLSLBinding ? "True" : "False", + g_ActiveConfig.backend_info.bSupportsGLSLUBO ? "True" : "False").c_str(), 5000); + s_LastMultisampleMode = g_ActiveConfig.iMultisampleMode; s_MSAASamples = GetNumMSAASamples(s_LastMultisampleMode); s_MSAACoverageSamples = GetNumMSAACoverageSamples(s_LastMultisampleMode); From 31a9b6c322b89ff64f5a1fe758259e3845879e20 Mon Sep 17 00:00:00 2001 From: Ryan Houdek Date: Tue, 20 Dec 2011 23:07:42 -0600 Subject: [PATCH 58/83] Go back to using glGetString with GL_EXTENSIONS because glew 1.7 is required to check for newer 4.2 extensions that way. --- Source/Plugins/Plugin_VideoOGL/Src/Render.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Source/Plugins/Plugin_VideoOGL/Src/Render.cpp b/Source/Plugins/Plugin_VideoOGL/Src/Render.cpp index 2e17429910..bb935e09b7 100644 --- a/Source/Plugins/Plugin_VideoOGL/Src/Render.cpp +++ b/Source/Plugins/Plugin_VideoOGL/Src/Render.cpp @@ -313,9 +313,9 @@ Renderer::Renderer() s_bHaveCoverageMSAA = strstr(ptoken, "GL_NV_framebuffer_multisample_coverage") != NULL; - if (glewIsSupported("GL_ARB_shading_language_420pack")) + if (strstr((const char*)glGetString(GL_EXTENSIONS), "GL_ARB_shading_language_420pack") != NULL) g_Config.backend_info.bSupportsGLSLBinding = true; - if (glewIsSupported("GL_ARB_uniform_buffer_object")) + if (strstr((const char*)glGetString(GL_EXTENSIONS), "GL_ARB_uniform_buffer_object") != NULL) g_Config.backend_info.bSupportsGLSLUBO = true; UpdateActiveConfig(); From a10656b1b2a9c464db122460fd57038c313ee5ef Mon Sep 17 00:00:00 2001 From: Ryan Houdek Date: Wed, 21 Dec 2011 00:15:48 -0600 Subject: [PATCH 59/83] Implement Program shaders cache. Seems to reduce a small amount of stuttering when F-Zero starts. Did it because I can :| --- Source/Core/VideoCommon/Src/VideoConfig.h | 1 + .../Src/ProgramShaderCache.cpp | 112 ++++++++++++------ .../Plugin_VideoOGL/Src/ProgramShaderCache.h | 59 ++++++++- Source/Plugins/Plugin_VideoOGL/Src/Render.cpp | 9 +- 4 files changed, 139 insertions(+), 42 deletions(-) diff --git a/Source/Core/VideoCommon/Src/VideoConfig.h b/Source/Core/VideoCommon/Src/VideoConfig.h index 84c71c0497..2e2b08319d 100644 --- a/Source/Core/VideoCommon/Src/VideoConfig.h +++ b/Source/Core/VideoCommon/Src/VideoConfig.h @@ -171,6 +171,7 @@ struct VideoConfig bool bSupportsGLSL; bool bSupportsGLSLBinding; bool bSupportsGLSLUBO; + bool bSupportsGLSLCache; } backend_info; }; diff --git a/Source/Plugins/Plugin_VideoOGL/Src/ProgramShaderCache.cpp b/Source/Plugins/Plugin_VideoOGL/Src/ProgramShaderCache.cpp index dc4b4e9822..70655294c7 100644 --- a/Source/Plugins/Plugin_VideoOGL/Src/ProgramShaderCache.cpp +++ b/Source/Plugins/Plugin_VideoOGL/Src/ProgramShaderCache.cpp @@ -26,6 +26,9 @@ namespace OGL GLuint ProgramShaderCache::s_ps_vs_ubo; GLintptr ProgramShaderCache::s_vs_data_offset; + LinearDiskCache g_program_disk_cache; + GLenum ProgramFormat; + std::pair ProgramShaderCache::CurrentShaderProgram; const char *UniformNames[NUM_UNIFORMS] = { // SAMPLERS @@ -53,6 +56,48 @@ namespace OGL I_DEPTHPARAMS, }; + void ProgramShaderCache::SetProgramVariables(PCacheEntry &entry, const PROGRAMUID &uid) + { + // Dunno why this is needed when I have the binding + // points statically set in the shader source + // We should only need these two functions when we don't support binding but do support UBO + // Driver Bug? Nvidia GTX 570, 290.xx Driver, Linux x64 + if (g_ActiveConfig.backend_info.bSupportsGLSLUBO) + { + glUniformBlockBinding( entry.program.glprogid, 0, 1 ); + if(uid.uid.vsid != 0) // Some things have no vertex shader + glUniformBlockBinding( entry.program.glprogid, 1, 2 ); + } + + // We cache our uniform locations for now + // Once we move up to a newer version of GLSL, ~1.30 + // We can remove this + + //For some reason this fails on my hardware + //glGetUniformIndices(entry.program.glprogid, NUM_UNIFORMS, UniformNames, entry.program.UniformLocations); + //Got to do it this crappy way. + if (!g_ActiveConfig.backend_info.bSupportsGLSLUBO) + for(int a = 8; a < NUM_UNIFORMS; ++a) + entry.program.UniformLocations[a] = glGetUniformLocation(entry.program.glprogid, UniformNames[a]); + if (!g_ActiveConfig.backend_info.bSupportsGLSLBinding) + for(int a = 0; a < 8; ++a) + { + // Still need to get sampler locations since we aren't binding them statically in the shaders + entry.program.UniformLocations[a] = glGetUniformLocation(entry.program.glprogid, UniformNames[a]); + if(entry.program.UniformLocations[a] != -1) + glUniform1i(entry.program.UniformLocations[a], a); + } + + // Need to get some attribute locations + if(uid.uid.vsid != 0) // We have no vertex Shader + { + glBindAttribLocation(entry.program.glprogid, SHADER_NORM1_ATTRIB, "rawnorm1"); + glBindAttribLocation(entry.program.glprogid, SHADER_NORM2_ATTRIB, "rawnorm2"); + glBindAttribLocation(entry.program.glprogid, SHADER_POSMTX_ATTRIB, "fposmtx"); + } + } + + void ProgramShaderCache::SetBothShaders(GLuint PS, GLuint VS) { PROGRAMUID uid; @@ -92,49 +137,19 @@ namespace OGL glAttachShader(entry.program.glprogid, entry.program.vsid); glAttachShader(entry.program.glprogid, entry.program.psid); + + + glProgramParameteri(entry.program.glprogid, GL_PROGRAM_BINARY_RETRIEVABLE_HINT, GL_TRUE); glLinkProgram(entry.program.glprogid); glUseProgram(entry.program.glprogid); - // Dunno why this is needed when I have the binding - // points statically set in the shader source - // We should only need these two functions when we don't support binding but do support UBO - // Driver Bug? Nvidia GTX 570, 290.xx Driver, Linux x64 - if (g_ActiveConfig.backend_info.bSupportsGLSLUBO) - { - glUniformBlockBinding( entry.program.glprogid, 0, 1 ); - if(uid.uid.vsid != 0) // Some things have no vertex shader - glUniformBlockBinding( entry.program.glprogid, 1, 2 ); - } - - // We cache our uniform locations for now - // Once we move up to a newer version of GLSL, ~1.30 - // We can remove this - - //For some reason this fails on my hardware - //glGetUniformIndices(entry.program.glprogid, NUM_UNIFORMS, UniformNames, entry.program.UniformLocations); - //Got to do it this crappy way. - if (!g_ActiveConfig.backend_info.bSupportsGLSLUBO) - for(int a = 8; a < NUM_UNIFORMS; ++a) - entry.program.UniformLocations[a] = glGetUniformLocation(entry.program.glprogid, UniformNames[a]); - if (!g_ActiveConfig.backend_info.bSupportsGLSLBinding) - for(int a = 0; a < 8; ++a) - { - // Still need to get sampler locations since we aren't binding them statically in the shaders - entry.program.UniformLocations[a] = glGetUniformLocation(entry.program.glprogid, UniformNames[a]); - if(entry.program.UniformLocations[a] != -1) - glUniform1i(entry.program.UniformLocations[a], a); - } - - // Need to get some attribute locations - if(uid.uid.vsid != 0) // We have no vertex Shader - { - glBindAttribLocation(entry.program.glprogid, SHADER_NORM1_ATTRIB, "rawnorm1"); - glBindAttribLocation(entry.program.glprogid, SHADER_NORM2_ATTRIB, "rawnorm2"); - glBindAttribLocation(entry.program.glprogid, SHADER_POSMTX_ATTRIB, "fposmtx"); - } + SetProgramVariables(entry, uid); + // Add it to our cache + if (g_ActiveConfig.backend_info.bSupportsGLSLCache) + g_program_disk_cache.Append(uid, entry.Data(), entry.Size()); pshaders[ShaderPair] = entry; CurrentShaderProgram = ShaderPair; @@ -186,10 +201,33 @@ namespace OGL glBindBufferRange(GL_UNIFORM_BUFFER, 1, s_ps_vs_ubo, 0, ps_data_size); glBindBufferRange(GL_UNIFORM_BUFFER, 2, s_ps_vs_ubo, s_vs_data_offset, vs_data_size); } + + // Read our shader cache, only if supported + if (g_ActiveConfig.backend_info.bSupportsGLSLCache) + { + char cache_filename[MAX_PATH]; + sprintf(cache_filename, "%sogl-%s-shaders.cache", File::GetUserPath(D_SHADERCACHE_IDX).c_str(), + SConfig::GetInstance().m_LocalCoreStartupParameter.m_strUniqueID.c_str()); + ProgramShaderCacheInserter inserter; + g_program_disk_cache.OpenAndRead(cache_filename, inserter); + + GLint Supported; + glGetIntegerv(GL_NUM_PROGRAM_BINARY_FORMATS, &Supported); + + GLint Formats[Supported]; + glGetIntegerv(GL_PROGRAM_BINARY_FORMATS, Formats); + ProgramFormat = (GLenum)Formats[0]; // We don't really care about format + } } void ProgramShaderCache::Shutdown(void) { + if (g_ActiveConfig.backend_info.bSupportsGLSLCache) + { + g_program_disk_cache.Sync(); + g_program_disk_cache.Close(); + } + PCache::iterator iter = pshaders.begin(); for (; iter != pshaders.end(); ++iter) iter->second.Destroy(); diff --git a/Source/Plugins/Plugin_VideoOGL/Src/ProgramShaderCache.h b/Source/Plugins/Plugin_VideoOGL/Src/ProgramShaderCache.h index 4830f28849..f2987965dc 100644 --- a/Source/Plugins/Plugin_VideoOGL/Src/ProgramShaderCache.h +++ b/Source/Plugins/Plugin_VideoOGL/Src/ProgramShaderCache.h @@ -25,6 +25,9 @@ #include "PixelShaderGen.h" #include "VertexShaderGen.h" +#include "LinearDiskCache.h" +#include "ConfigManager.h" + union PID { struct { @@ -65,14 +68,28 @@ namespace OGL { #define NUM_UNIFORMS 27 extern const char *UniformNames[NUM_UNIFORMS]; - +extern GLenum ProgramFormat; struct PROGRAMSHADER { - PROGRAMSHADER() : glprogid(0), vsid(0), psid(0){} + PROGRAMSHADER() : glprogid(0), vsid(0), psid(0), binaryLength(0){} GLuint glprogid; // opengl program id GLuint vsid, psid; GLint UniformLocations[NUM_UNIFORMS]; + GLint binaryLength; + u8 *Data() + { + glGetProgramiv(glprogid, GL_PROGRAM_BINARY_LENGTH, &binaryLength); + u8* binary = (u8*)malloc(binaryLength); + glGetProgramBinary(glprogid, binaryLength, NULL, &ProgramFormat, binary); + return binary; + } + GLint Size() + { + if(!binaryLength) + glGetProgramiv(glprogid, GL_PROGRAM_BINARY_LENGTH, &binaryLength); + return binaryLength; + } }; @@ -87,7 +104,44 @@ class ProgramShaderCache glDeleteProgram(program.glprogid); program.glprogid = 0; } + u8* Data() + { + return program.Data(); + } + GLint Size() + { + return program.Size(); + } }; + + class ProgramShaderCacheInserter : public LinearDiskCacheReader + { + public: + void Read(const PROGRAMUID &key, const u8 *value, u32 value_size) + { + PCacheEntry entry; + + // The two shaders might not even exist anymore + // But it is fine, no need to worry about that + entry.program.vsid = key.uid.vsid; + entry.program.psid = key.uid.psid; + + entry.program.glprogid = glCreateProgram(); + + glProgramBinary(entry.program.glprogid, ProgramFormat, value, value_size); + + GLint success; + glGetProgramiv(entry.program.glprogid, GL_LINK_STATUS, &success); + + if (success) + { + pshaders[std::make_pair(key.uid.psid, key.uid.vsid)] = entry; + glUseProgram(entry.program.glprogid); + SetProgramVariables(entry, key); + } + } + }; + typedef std::map, PCacheEntry> PCache; static PCache pshaders; @@ -96,6 +150,7 @@ class ProgramShaderCache static GLuint s_ps_vs_ubo; static GLintptr s_vs_data_offset; + static void SetProgramVariables(PCacheEntry &entry, const PROGRAMUID &uid); public: static PROGRAMSHADER GetShaderProgram(void); diff --git a/Source/Plugins/Plugin_VideoOGL/Src/Render.cpp b/Source/Plugins/Plugin_VideoOGL/Src/Render.cpp index bb935e09b7..5583619d38 100644 --- a/Source/Plugins/Plugin_VideoOGL/Src/Render.cpp +++ b/Source/Plugins/Plugin_VideoOGL/Src/Render.cpp @@ -312,16 +312,19 @@ Renderer::Renderer() s_bHaveFramebufferBlit = strstr(ptoken, "GL_EXT_framebuffer_blit") != NULL; s_bHaveCoverageMSAA = strstr(ptoken, "GL_NV_framebuffer_multisample_coverage") != NULL; - + // TODO: Switch over to using glew once 1.6/1.7 becomes more mainstream, seems most people are stuck in 1.5 if (strstr((const char*)glGetString(GL_EXTENSIONS), "GL_ARB_shading_language_420pack") != NULL) g_Config.backend_info.bSupportsGLSLBinding = true; if (strstr((const char*)glGetString(GL_EXTENSIONS), "GL_ARB_uniform_buffer_object") != NULL) g_Config.backend_info.bSupportsGLSLUBO = true; + if (strstr((const char*)glGetString(GL_EXTENSIONS), "GL_ARB_get_program_binary") != NULL) + g_Config.backend_info.bSupportsGLSLCache = true; UpdateActiveConfig(); - OSD::AddMessage(StringFromFormat("Using GLSL. Supports Binding: %s UBOs: %s", + OSD::AddMessage(StringFromFormat("Using GLSL. Supports Binding: %s UBOs: %s Cache: %s", g_ActiveConfig.backend_info.bSupportsGLSLBinding ? "True" : "False", - g_ActiveConfig.backend_info.bSupportsGLSLUBO ? "True" : "False").c_str(), 5000); + g_ActiveConfig.backend_info.bSupportsGLSLUBO ? "True" : "False", + g_ActiveConfig.backend_info.bSupportsGLSLCache ? "True" : "False").c_str(), 5000); s_LastMultisampleMode = g_ActiveConfig.iMultisampleMode; s_MSAASamples = GetNumMSAASamples(s_LastMultisampleMode); From 8e0172374c172b507d3fb7a3f829c101c9dd6230 Mon Sep 17 00:00:00 2001 From: Ryan Houdek Date: Wed, 21 Dec 2011 01:29:29 -0600 Subject: [PATCH 60/83] Give OSX users more of a chance of supporting Single pass DSB in the future. --- .../Core/VideoCommon/Src/PixelShaderGen.cpp | 5 ++- .../Core/VideoCommon/Src/VertexShaderGen.cpp | 32 +++++++++++++------ Source/Core/VideoCommon/Src/VideoConfig.h | 1 + .../Src/ProgramShaderCache.cpp | 2 +- Source/Plugins/Plugin_VideoOGL/Src/Render.cpp | 2 ++ .../Plugin_VideoOGL/Src/VertexManager.cpp | 2 +- 6 files changed, 32 insertions(+), 12 deletions(-) diff --git a/Source/Core/VideoCommon/Src/PixelShaderGen.cpp b/Source/Core/VideoCommon/Src/PixelShaderGen.cpp index c376f55e6f..ef27629246 100644 --- a/Source/Core/VideoCommon/Src/PixelShaderGen.cpp +++ b/Source/Core/VideoCommon/Src/PixelShaderGen.cpp @@ -563,6 +563,9 @@ const char *GeneratePixelShaderCode(DSTALPHA_MODE dstAlphaMode, API_TYPE ApiType } else WRITE(p, "#version 120\n"); + + if(g_ActiveConfig.backend_info.bSupportsGLSLATTRBind) + WRITE(p, "#extension GL_ARB_explicit_attrib_location : enable\n"); // Silly differences WRITE(p, "#define float2 vec2\n"); WRITE(p, "#define float3 vec3\n"); @@ -621,7 +624,7 @@ const char *GeneratePixelShaderCode(DSTALPHA_MODE dstAlphaMode, API_TYPE ApiType WRITE(p, "\n"); if(ApiType == API_GLSL && g_ActiveConfig.backend_info.bSupportsGLSLUBO) - WRITE(p, "layout(std140%s) uniform PSBlock {\n", g_ActiveConfig.backend_info.bSupportsGLSLBinding ? ", binding = 1" : ""); + WRITE(p, "layout(std140) uniform PSBlock {\n"); WRITE(p, "%sfloat4 "I_COLORS"[4] %s;\n", WriteLocation(ApiType), WriteRegister(ApiType, "c", C_COLORS)); WRITE(p, "%sfloat4 "I_KCOLORS"[4] %s;\n", WriteLocation(ApiType), WriteRegister(ApiType, "c", C_KCOLORS)); diff --git a/Source/Core/VideoCommon/Src/VertexShaderGen.cpp b/Source/Core/VideoCommon/Src/VertexShaderGen.cpp index 67700dd647..7585597d43 100644 --- a/Source/Core/VideoCommon/Src/VertexShaderGen.cpp +++ b/Source/Core/VideoCommon/Src/VertexShaderGen.cpp @@ -204,6 +204,8 @@ const char *GenerateVertexShaderCode(u32 components, API_TYPE ApiType) WRITE(p, "#define ATTRIN attribute\n"); WRITE(p, "#define ATTROUT attribute\n"); } + if(g_ActiveConfig.backend_info.bSupportsGLSLATTRBind) + WRITE(p, "#extension GL_ARB_explicit_attrib_location : enable\n"); // Silly differences WRITE(p, "#define float2 vec2\n"); WRITE(p, "#define float3 vec3\n"); @@ -217,7 +219,7 @@ const char *GenerateVertexShaderCode(u32 components, API_TYPE ApiType) // uniforms if(ApiType == API_GLSL && g_ActiveConfig.backend_info.bSupportsGLSLUBO) - WRITE(p, "layout(std140%s) uniform VSBlock {\n", g_ActiveConfig.backend_info.bSupportsGLSLBinding ? ", binding = 2" : ""); + WRITE(p, "layout(std140) uniform VSBlock {\n"); WRITE(p, "%sfloat4 "I_POSNORMALMATRIX"[6] %s;\n", WriteLocation(ApiType), WriteRegister(ApiType, "c", C_POSNORMALMATRIX)); WRITE(p, "%sfloat4 "I_PROJECTION"[4] %s;\n", WriteLocation(ApiType), WriteRegister(ApiType, "c", C_PROJECTION)); @@ -241,21 +243,33 @@ const char *GenerateVertexShaderCode(u32 components, API_TYPE ApiType) if (components & VB_HAS_NRM0) WRITE(p, " float3 rawnorm0 = gl_Normal; // NORMAL0,\n"); - if (components & VB_HAS_POSMTXIDX) + if(g_ActiveConfig.backend_info.bSupportsGLSLATTRBind) + { + if (components & VB_HAS_POSMTXIDX) + WRITE(p, "layout(location = %d) ATTRIN float fposmtx;\n", SHADER_POSMTX_ATTRIB); + if (components & VB_HAS_NRM1) + WRITE(p, "layout(location = %d) ATTRIN float3 rawnorm1;\n", SHADER_NORM1_ATTRIB); + if (components & VB_HAS_NRM2) + WRITE(p, "layout(location = %d) ATTRIN float3 rawnorm2;\n", SHADER_NORM2_ATTRIB); + } + else + { + if (components & VB_HAS_POSMTXIDX) WRITE(p, "ATTRIN float fposmtx; // ATTR%d,\n", SHADER_POSMTX_ATTRIB); - if (components & VB_HAS_NRM1) + if (components & VB_HAS_NRM1) WRITE(p, "ATTRIN float3 rawnorm1; // ATTR%d,\n", SHADER_NORM1_ATTRIB); - if (components & VB_HAS_NRM2) + if (components & VB_HAS_NRM2) WRITE(p, "ATTRIN float3 rawnorm2; // ATTR%d,\n", SHADER_NORM2_ATTRIB); + } if (components & VB_HAS_COL0) - WRITE(p, " float4 color0 = gl_Color; // COLOR0,\n"); + WRITE(p, " float4 color0 = gl_Color; // COLOR0,\n"); if (components & VB_HAS_COL1) - WRITE(p, " float4 color1 = gl_SecondaryColor; // COLOR1,\n"); + WRITE(p, " float4 color1 = gl_SecondaryColor; // COLOR1,\n"); for (int i = 0; i < 8; ++i) { - u32 hastexmtx = (components & (VB_HAS_TEXMTXIDX0< Date: Wed, 21 Dec 2011 22:33:33 +0000 Subject: [PATCH 61/83] Missed a if, don't want people crashing now do we? --- Source/Plugins/Plugin_VideoOGL/Src/ProgramShaderCache.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Source/Plugins/Plugin_VideoOGL/Src/ProgramShaderCache.cpp b/Source/Plugins/Plugin_VideoOGL/Src/ProgramShaderCache.cpp index 5c143c010c..0429b89e30 100644 --- a/Source/Plugins/Plugin_VideoOGL/Src/ProgramShaderCache.cpp +++ b/Source/Plugins/Plugin_VideoOGL/Src/ProgramShaderCache.cpp @@ -138,8 +138,8 @@ namespace OGL glAttachShader(entry.program.glprogid, entry.program.psid); - - glProgramParameteri(entry.program.glprogid, GL_PROGRAM_BINARY_RETRIEVABLE_HINT, GL_TRUE); + if (g_ActiveConfig.backend_info.bSupportsGLSLCache) + glProgramParameteri(entry.program.glprogid, GL_PROGRAM_BINARY_RETRIEVABLE_HINT, GL_TRUE); glLinkProgram(entry.program.glprogid); From 9bc6b83fd0bd7f54b8cbefe762eaf59e9ec522b9 Mon Sep 17 00:00:00 2001 From: Ryan Houdek Date: Sat, 24 Dec 2011 00:24:13 -0600 Subject: [PATCH 62/83] If CG isn't available, still compile and fallback on GLSL --- CMakeLists.txt | 6 ++++++ Source/Plugins/Plugin_VideoOGL/CMakeLists.txt | 2 +- Source/Plugins/Plugin_VideoOGL/Src/GLUtil.h | 3 +-- Source/Plugins/Plugin_VideoOGL/Src/Render.cpp | 5 +++++ 4 files changed, 13 insertions(+), 3 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index e870304dc4..a22e017107 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -372,6 +372,12 @@ else() check_lib(CG Cg Cg/cg.h) check_lib(CGGL CgGL Cg/cgGL.h) + if(NOT CG_FOUND) + message("CG not found, Building without") + add_definitions(-DHAVE_CG=0) + else() + add_definitions(-DHAVE_CG=1) + endif() endif() if(NOT ${CMAKE_SYSTEM_NAME} MATCHES "Darwin") diff --git a/Source/Plugins/Plugin_VideoOGL/CMakeLists.txt b/Source/Plugins/Plugin_VideoOGL/CMakeLists.txt index 6a49269c6c..86f48d2fc3 100644 --- a/Source/Plugins/Plugin_VideoOGL/CMakeLists.txt +++ b/Source/Plugins/Plugin_VideoOGL/CMakeLists.txt @@ -23,7 +23,7 @@ if(wxWidgets_FOUND) set(LIBS ${LIBS} ${wxWidgets_LIBRARIES}) endif(wxWidgets_FOUND) -if(APPLE OR WIN32 OR ${CMAKE_SYSTEM_NAME} MATCHES "Linux") +if(HAVE_CG AND (APPLE OR WIN32 OR ${CMAKE_SYSTEM_NAME} MATCHES "Linux")) set(LIBS ${LIBS} Cg CgGL) endif() diff --git a/Source/Plugins/Plugin_VideoOGL/Src/GLUtil.h b/Source/Plugins/Plugin_VideoOGL/Src/GLUtil.h index bfde884ccd..db7cf822e4 100644 --- a/Source/Plugins/Plugin_VideoOGL/Src/GLUtil.h +++ b/Source/Plugins/Plugin_VideoOGL/Src/GLUtil.h @@ -113,10 +113,9 @@ bool OpenGL_ReportFBOError(const char *function, const char *file, int line); #define GL_REPORT_PROGRAM_ERROR() (void)0 #endif -#if defined __APPLE__ || defined __linux__ || defined _WIN32 +#if HAVE_CG && (defined __APPLE__ || defined __linux__ || defined _WIN32) #include #include -#define HAVE_CG 1 extern CGcontext g_cgcontext; extern CGprofile g_cgvProf, g_cgfProf; #endif diff --git a/Source/Plugins/Plugin_VideoOGL/Src/Render.cpp b/Source/Plugins/Plugin_VideoOGL/Src/Render.cpp index bbb80bcbbd..6d1d4dd23b 100644 --- a/Source/Plugins/Plugin_VideoOGL/Src/Render.cpp +++ b/Source/Plugins/Plugin_VideoOGL/Src/Render.cpp @@ -435,6 +435,11 @@ Renderer::Renderer() cgGLSetOptimalOptions(g_cgfProf); } } +#else + // If we don't have Nvidia CG, we HAVE to use GLSL + g_Config.bUseGLSL = true; + + UpdateActiveConfig(); #endif // HAVE_CG int nenvvertparams, nenvfragparams, naddrregisters[2]; From b90fa37c608737ffff653b0d7ff6ac80fb533541 Mon Sep 17 00:00:00 2001 From: Ryan Houdek Date: Sat, 24 Dec 2011 00:37:13 -0600 Subject: [PATCH 63/83] If user doesn't compile with glew 1.6, cut out program binaries so everything still compiles. --- .../Plugins/Plugin_VideoOGL/Src/ProgramShaderCache.cpp | 6 ++++-- .../Plugins/Plugin_VideoOGL/Src/ProgramShaderCache.h | 10 ++++++++++ 2 files changed, 14 insertions(+), 2 deletions(-) diff --git a/Source/Plugins/Plugin_VideoOGL/Src/ProgramShaderCache.cpp b/Source/Plugins/Plugin_VideoOGL/Src/ProgramShaderCache.cpp index 0429b89e30..628cbae865 100644 --- a/Source/Plugins/Plugin_VideoOGL/Src/ProgramShaderCache.cpp +++ b/Source/Plugins/Plugin_VideoOGL/Src/ProgramShaderCache.cpp @@ -137,9 +137,10 @@ namespace OGL glAttachShader(entry.program.glprogid, entry.program.vsid); glAttachShader(entry.program.glprogid, entry.program.psid); - + #ifdef GLEW_VERSION_4_0 if (g_ActiveConfig.backend_info.bSupportsGLSLCache) glProgramParameteri(entry.program.glprogid, GL_PROGRAM_BINARY_RETRIEVABLE_HINT, GL_TRUE); + #endif glLinkProgram(entry.program.glprogid); @@ -201,7 +202,7 @@ namespace OGL glBindBufferRange(GL_UNIFORM_BUFFER, 1, s_ps_vs_ubo, 0, ps_data_size); glBindBufferRange(GL_UNIFORM_BUFFER, 2, s_ps_vs_ubo, s_vs_data_offset, vs_data_size); } - + #ifdef GLEW_VERSION_4_0 // Read our shader cache, only if supported if (g_ActiveConfig.backend_info.bSupportsGLSLCache) { @@ -218,6 +219,7 @@ namespace OGL glGetIntegerv(GL_PROGRAM_BINARY_FORMATS, Formats); ProgramFormat = (GLenum)Formats[0]; // We don't really care about format } + #endif } void ProgramShaderCache::Shutdown(void) diff --git a/Source/Plugins/Plugin_VideoOGL/Src/ProgramShaderCache.h b/Source/Plugins/Plugin_VideoOGL/Src/ProgramShaderCache.h index f2987965dc..51638cc1e4 100644 --- a/Source/Plugins/Plugin_VideoOGL/Src/ProgramShaderCache.h +++ b/Source/Plugins/Plugin_VideoOGL/Src/ProgramShaderCache.h @@ -79,16 +79,24 @@ struct PROGRAMSHADER GLint binaryLength; u8 *Data() { + #ifdef GLEW_VERSION_4_0 glGetProgramiv(glprogid, GL_PROGRAM_BINARY_LENGTH, &binaryLength); u8* binary = (u8*)malloc(binaryLength); glGetProgramBinary(glprogid, binaryLength, NULL, &ProgramFormat, binary); return binary; + #else + return NULL; + #endif } GLint Size() { + #ifdef GLEW_VERSION_4_0 if(!binaryLength) glGetProgramiv(glprogid, GL_PROGRAM_BINARY_LENGTH, &binaryLength); return binaryLength; + #else + return 0; + #endif } }; @@ -119,6 +127,7 @@ class ProgramShaderCache public: void Read(const PROGRAMUID &key, const u8 *value, u32 value_size) { + #ifdef GLEW_VERSION_4_0 PCacheEntry entry; // The two shaders might not even exist anymore @@ -139,6 +148,7 @@ class ProgramShaderCache glUseProgram(entry.program.glprogid); SetProgramVariables(entry, key); } + #endif } }; From aa2032af2cc444fc0387d5aebcdd4b34b39d5277 Mon Sep 17 00:00:00 2001 From: Ryan Houdek Date: Sat, 24 Dec 2011 02:19:30 -0600 Subject: [PATCH 64/83] Write all shaders to disk on emulator stop instead of constantly. Also change pair from u64 to u32. --- .../Plugin_VideoOGL/Src/ProgramShaderCache.cpp | 13 +++++++------ .../Plugin_VideoOGL/Src/ProgramShaderCache.h | 5 +++-- 2 files changed, 10 insertions(+), 8 deletions(-) diff --git a/Source/Plugins/Plugin_VideoOGL/Src/ProgramShaderCache.cpp b/Source/Plugins/Plugin_VideoOGL/Src/ProgramShaderCache.cpp index 628cbae865..6514e1e775 100644 --- a/Source/Plugins/Plugin_VideoOGL/Src/ProgramShaderCache.cpp +++ b/Source/Plugins/Plugin_VideoOGL/Src/ProgramShaderCache.cpp @@ -29,7 +29,7 @@ namespace OGL LinearDiskCache g_program_disk_cache; GLenum ProgramFormat; - std::pair ProgramShaderCache::CurrentShaderProgram; + std::pair ProgramShaderCache::CurrentShaderProgram; const char *UniformNames[NUM_UNIFORMS] = { // SAMPLERS "samp0","samp1","samp2","samp3","samp4","samp5","samp6","samp7", @@ -116,7 +116,7 @@ namespace OGL // Fragment shaders can survive without Vertex Shaders // We have a valid fragment shader, let's create our program - std::pair ShaderPair = std::make_pair(uid.uid.psid, uid.uid.vsid); + std::pair ShaderPair = std::make_pair(uid.uid.psid, uid.uid.vsid); PCache::iterator iter = pshaders.find(ShaderPair); if (iter != pshaders.end()) { @@ -129,6 +129,7 @@ namespace OGL PCacheEntry entry; entry.program.vsid = CurrentVShader; entry.program.psid = CurrentFShader; + entry.program.uid = uid; entry.program.glprogid = glCreateProgram(); // Right, the program is created now @@ -148,10 +149,6 @@ namespace OGL SetProgramVariables(entry, uid); - // Add it to our cache - if (g_ActiveConfig.backend_info.bSupportsGLSLCache) - g_program_disk_cache.Append(uid, entry.Data(), entry.Size()); - pshaders[ShaderPair] = entry; CurrentShaderProgram = ShaderPair; CurrentProgram = entry.program.glprogid; @@ -226,6 +223,10 @@ namespace OGL { if (g_ActiveConfig.backend_info.bSupportsGLSLCache) { + PCache::iterator iter = pshaders.begin(); + for (; iter != pshaders.end(); ++iter) + g_program_disk_cache.Append(iter->second.program.uid, iter->second.program.Data(), iter->second.program.Size()); + g_program_disk_cache.Sync(); g_program_disk_cache.Close(); } diff --git a/Source/Plugins/Plugin_VideoOGL/Src/ProgramShaderCache.h b/Source/Plugins/Plugin_VideoOGL/Src/ProgramShaderCache.h index 51638cc1e4..59bcc8e295 100644 --- a/Source/Plugins/Plugin_VideoOGL/Src/ProgramShaderCache.h +++ b/Source/Plugins/Plugin_VideoOGL/Src/ProgramShaderCache.h @@ -75,6 +75,7 @@ struct PROGRAMSHADER PROGRAMSHADER() : glprogid(0), vsid(0), psid(0), binaryLength(0){} GLuint glprogid; // opengl program id GLuint vsid, psid; + PROGRAMUID uid; GLint UniformLocations[NUM_UNIFORMS]; GLint binaryLength; u8 *Data() @@ -152,11 +153,11 @@ class ProgramShaderCache } }; - typedef std::map, PCacheEntry> PCache; + typedef std::map, PCacheEntry> PCache; static PCache pshaders; static GLuint CurrentFShader, CurrentVShader, CurrentProgram; - static std::pair CurrentShaderProgram; + static std::pair CurrentShaderProgram; static GLuint s_ps_vs_ubo; static GLintptr s_vs_data_offset; From 8349cc2551ca89006120cdf4c5fd296edee25445 Mon Sep 17 00:00:00 2001 From: Ryan Houdek Date: Sat, 24 Dec 2011 08:58:51 -0600 Subject: [PATCH 65/83] Only use explicit attribute locations when we are supporting GLSL > 1.2 since we need in/out instead of attribute variable types. This was brought to my attention from MESA. MESA supports GL_ARB_explicit_attrib_location, but yets to support GLSL 1.3, so basically useless extension to MESA right now? --- Source/Plugins/Plugin_VideoOGL/Src/Render.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Source/Plugins/Plugin_VideoOGL/Src/Render.cpp b/Source/Plugins/Plugin_VideoOGL/Src/Render.cpp index 6d1d4dd23b..fa270f051e 100644 --- a/Source/Plugins/Plugin_VideoOGL/Src/Render.cpp +++ b/Source/Plugins/Plugin_VideoOGL/Src/Render.cpp @@ -315,10 +315,10 @@ Renderer::Renderer() // TODO: Switch over to using glew once 1.6/1.7 becomes more mainstream, seems most people are stuck in 1.5 if (strstr((const char*)glGetString(GL_EXTENSIONS), "GL_ARB_shading_language_420pack") != NULL) g_Config.backend_info.bSupportsGLSLBinding = true; - if (strstr((const char*)glGetString(GL_EXTENSIONS), "GL_ARB_explicit_attrib_location") != NULL) - g_Config.backend_info.bSupportsGLSLATTRBind = true; if (strstr((const char*)glGetString(GL_EXTENSIONS), "GL_ARB_uniform_buffer_object") != NULL) g_Config.backend_info.bSupportsGLSLUBO = true; + if ((g_Config.backend_info.bSupportsGLSLBinding || g_Config.backend_info.bSupportsGLSLUBO) && strstr((const char*)glGetString(GL_EXTENSIONS), "GL_ARB_explicit_attrib_location") != NULL) + g_Config.backend_info.bSupportsGLSLATTRBind = true; if (strstr((const char*)glGetString(GL_EXTENSIONS), "GL_ARB_get_program_binary") != NULL) g_Config.backend_info.bSupportsGLSLCache = true; From b607695103ce9d2b40e3e60980b88fb388f5d121 Mon Sep 17 00:00:00 2001 From: Ryan Houdek Date: Sun, 25 Dec 2011 12:21:31 -0600 Subject: [PATCH 66/83] Add in the Windows fix. --- Source/Plugins/Plugin_VideoOGL/Src/ProgramShaderCache.cpp | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/Source/Plugins/Plugin_VideoOGL/Src/ProgramShaderCache.cpp b/Source/Plugins/Plugin_VideoOGL/Src/ProgramShaderCache.cpp index 6514e1e775..a422f80079 100644 --- a/Source/Plugins/Plugin_VideoOGL/Src/ProgramShaderCache.cpp +++ b/Source/Plugins/Plugin_VideoOGL/Src/ProgramShaderCache.cpp @@ -212,9 +212,10 @@ namespace OGL GLint Supported; glGetIntegerv(GL_NUM_PROGRAM_BINARY_FORMATS, &Supported); - GLint Formats[Supported]; + GLint *Formats = new GLint[Supported]; glGetIntegerv(GL_PROGRAM_BINARY_FORMATS, Formats); ProgramFormat = (GLenum)Formats[0]; // We don't really care about format + delete[] Formats; } #endif } From 8266a564863e530169101e35a8e82fc346b61a59 Mon Sep 17 00:00:00 2001 From: Ryan Houdek Date: Sun, 25 Dec 2011 12:44:31 -0600 Subject: [PATCH 67/83] Missed a line here --- CMakeLists.txt | 2 ++ 1 file changed, 2 insertions(+) diff --git a/CMakeLists.txt b/CMakeLists.txt index a22e017107..1eac2b2cd3 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -375,8 +375,10 @@ else() if(NOT CG_FOUND) message("CG not found, Building without") add_definitions(-DHAVE_CG=0) + set(HAVE_CG 0) else() add_definitions(-DHAVE_CG=1) + set(HAVE_CG 1) endif() endif() From f59063c8e7ec48992cdb0e7b2952ffc801daeefd Mon Sep 17 00:00:00 2001 From: Shawn Hoffman Date: Sun, 25 Dec 2011 22:00:24 -0500 Subject: [PATCH 68/83] default to GLSL instead of Cg --- Source/Core/VideoCommon/Src/VideoConfig.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Source/Core/VideoCommon/Src/VideoConfig.cpp b/Source/Core/VideoCommon/Src/VideoConfig.cpp index 69c1c4ce1c..21c71f5dfc 100644 --- a/Source/Core/VideoCommon/Src/VideoConfig.cpp +++ b/Source/Core/VideoCommon/Src/VideoConfig.cpp @@ -93,7 +93,7 @@ void VideoConfig::Load(const char *ini_file) iniFile.Get("Settings", "DisableLighting", &bDisableLighting, 0); iniFile.Get("Settings", "DisableTexturing", &bDisableTexturing, 0); iniFile.Get("Settings", "DisableFog", &bDisableFog, 0); - iniFile.Get("Settings", "UseGLSL", &bUseGLSL, 0); + iniFile.Get("Settings", "UseGLSL", &bUseGLSL, true); iniFile.Get("Settings", "EnableOpenCL", &bEnableOpenCL, false); iniFile.Get("Settings", "OMPDecoder", &bOMPDecoder, false); From 4bc14c347338f951b3b87de23a3fa19d7118a629 Mon Sep 17 00:00:00 2001 From: Shawn Hoffman Date: Mon, 26 Dec 2011 00:15:54 -0500 Subject: [PATCH 69/83] fix formatting uglies introduced in glsl-master branch --- Source/Core/DolphinWX/Src/VideoConfigDiag.cpp | 2 +- Source/Core/DolphinWX/Src/VideoConfigDiag.h | 2 +- .../Core/VideoCommon/Src/PixelShaderGen.cpp | 89 +-- .../Src/TextureConversionShader.cpp | 315 +++++------ .../Core/VideoCommon/Src/VertexShaderGen.cpp | 129 ++--- .../Plugin_VideoOGL/Src/PixelShaderCache.cpp | 532 +++++++++--------- .../Plugin_VideoOGL/Src/PixelShaderCache.h | 2 +- .../Src/ProgramShaderCache.cpp | 439 ++++++++------- .../Plugin_VideoOGL/Src/ProgramShaderCache.h | 116 ++-- Source/Plugins/Plugin_VideoOGL/Src/Render.cpp | 4 +- .../Plugin_VideoOGL/Src/TextureCache.cpp | 10 +- .../Plugin_VideoOGL/Src/TextureConverter.cpp | 18 +- .../Plugin_VideoOGL/Src/VertexManager.cpp | 6 +- .../Plugin_VideoOGL/Src/VertexShaderCache.cpp | 457 ++++++++------- .../Plugin_VideoOGL/Src/VertexShaderCache.h | 2 +- 15 files changed, 1069 insertions(+), 1054 deletions(-) diff --git a/Source/Core/DolphinWX/Src/VideoConfigDiag.cpp b/Source/Core/DolphinWX/Src/VideoConfigDiag.cpp index 618831ee81..2fc8ea5ba4 100644 --- a/Source/Core/DolphinWX/Src/VideoConfigDiag.cpp +++ b/Source/Core/DolphinWX/Src/VideoConfigDiag.cpp @@ -581,7 +581,7 @@ VideoConfigDiag::VideoConfigDiag(wxWindow* parent, const std::string &title, con // GLSL Option { - if(strstr(choice_backend->GetString(choice_backend->GetSelection()).ToAscii(), "OpenGL") != NULL) + if (strstr(choice_backend->GetString(choice_backend->GetSelection()).ToAscii(), "OpenGL") != NULL) { wxCheckBox* const cb_GLSL = CreateCheckBox(page_advanced, _("Use GLSL Shaders"), wxGetTranslation(GLSL_desc), vconfig.bUseGLSL); szr_misc->Add(cb_GLSL); diff --git a/Source/Core/DolphinWX/Src/VideoConfigDiag.h b/Source/Core/DolphinWX/Src/VideoConfigDiag.h index 5765524f03..ebcc3b9454 100644 --- a/Source/Core/DolphinWX/Src/VideoConfigDiag.h +++ b/Source/Core/DolphinWX/Src/VideoConfigDiag.h @@ -112,7 +112,7 @@ protected: void Event_ProgressiveScan(wxCommandEvent &ev) { SConfig::GetInstance().m_SYSCONF->SetData("IPL.PGS", ev.GetInt()); - SConfig::GetInstance().m_LocalCoreStartupParameter.bProgressive = ev.GetInt(); + SConfig::GetInstance().m_LocalCoreStartupParameter.bProgressive = !!ev.GetInt(); ev.Skip(); } diff --git a/Source/Core/VideoCommon/Src/PixelShaderGen.cpp b/Source/Core/VideoCommon/Src/PixelShaderGen.cpp index ef27629246..7eb3999fbc 100644 --- a/Source/Core/VideoCommon/Src/PixelShaderGen.cpp +++ b/Source/Core/VideoCommon/Src/PixelShaderGen.cpp @@ -502,23 +502,25 @@ static void BuildSwapModeTable() const char* WriteRegister(API_TYPE ApiType, const char *prefix, const u32 num) { - if(ApiType == API_GLSL) + if (ApiType == API_GLSL) return ""; // Nothing to do here static char result[64]; sprintf(result, " : register(%s%d)", prefix, num); return result; } + const char* WriteBinding(API_TYPE ApiType, const u32 num) { - if(ApiType != API_GLSL || !g_ActiveConfig.backend_info.bSupportsGLSLBinding) + if (ApiType != API_GLSL || !g_ActiveConfig.backend_info.bSupportsGLSLBinding) return ""; static char result[64]; sprintf(result, "layout(binding = %d) ", num); return result; } + const char *WriteLocation(API_TYPE ApiType) { - if(ApiType == API_GLSL && g_ActiveConfig.backend_info.bSupportsGLSLUBO) + if (ApiType == API_GLSL && g_ActiveConfig.backend_info.bSupportsGLSLUBO) return ""; static char result[64]; sprintf(result, "uniform "); @@ -550,7 +552,7 @@ const char *GeneratePixelShaderCode(DSTALPHA_MODE dstAlphaMode, API_TYPE ApiType } DepthTextureEnable = (bpmem.ztex2.op != ZTEXTURE_DISABLE && !bpmem.zcontrol.zcomploc && bpmem.zmode.testenable && bpmem.zmode.updateenable) || g_ActiveConfig.bEnablePerPixelDepth ; - if(ApiType == API_GLSL) + if (ApiType == API_GLSL) { // A few required defines and ones that will make our lives a lot easier if (g_ActiveConfig.backend_info.bSupportsGLSLBinding || g_ActiveConfig.backend_info.bSupportsGLSLUBO) @@ -564,7 +566,7 @@ const char *GeneratePixelShaderCode(DSTALPHA_MODE dstAlphaMode, API_TYPE ApiType else WRITE(p, "#version 120\n"); - if(g_ActiveConfig.backend_info.bSupportsGLSLATTRBind) + if (g_ActiveConfig.backend_info.bSupportsGLSLATTRBind) WRITE(p, "#extension GL_ARB_explicit_attrib_location : enable\n"); // Silly differences WRITE(p, "#define float2 vec2\n"); @@ -593,7 +595,7 @@ const char *GeneratePixelShaderCode(DSTALPHA_MODE dstAlphaMode, API_TYPE ApiType { // Declare samplers - if(ApiType != API_D3D11) + if (ApiType != API_D3D11) { WRITE(p, "uniform sampler2D "); } @@ -609,7 +611,7 @@ const char *GeneratePixelShaderCode(DSTALPHA_MODE dstAlphaMode, API_TYPE ApiType bfirst = false; } WRITE(p, ";\n"); - if(ApiType == API_D3D11) + if (ApiType == API_D3D11) { WRITE(p, "Texture2D "); bfirst = true; @@ -623,7 +625,7 @@ const char *GeneratePixelShaderCode(DSTALPHA_MODE dstAlphaMode, API_TYPE ApiType } WRITE(p, "\n"); - if(ApiType == API_GLSL && g_ActiveConfig.backend_info.bSupportsGLSLUBO) + if (ApiType == API_GLSL && g_ActiveConfig.backend_info.bSupportsGLSLUBO) WRITE(p, "layout(std140) uniform PSBlock {\n"); WRITE(p, "%sfloat4 "I_COLORS"[4] %s;\n", WriteLocation(ApiType), WriteRegister(ApiType, "c", C_COLORS)); @@ -639,13 +641,13 @@ const char *GeneratePixelShaderCode(DSTALPHA_MODE dstAlphaMode, API_TYPE ApiType WRITE(p, "%sfloat4 "I_PLIGHTS"[40] %s;\n", WriteLocation(ApiType), WriteRegister(ApiType, "c", C_PLIGHTS)); WRITE(p, "%sfloat4 "I_PMATERIALS"[4] %s;\n", WriteLocation(ApiType), WriteRegister(ApiType, "c", C_PMATERIALS)); - if(ApiType == API_GLSL && g_ActiveConfig.backend_info.bSupportsGLSLUBO) + if (ApiType == API_GLSL && g_ActiveConfig.backend_info.bSupportsGLSLUBO) WRITE(p, "};\n"); - if(ApiType != API_GLSL) + if (ApiType != API_GLSL) { WRITE(p, "void main(\n"); - if(ApiType != API_D3D11) + if (ApiType != API_D3D11) { WRITE(p, " out float4 ocol0 : COLOR0,%s%s\n in float4 rawpos : %s,\n", dstAlphaMode == DSTALPHA_DUAL_SOURCE_BLEND ? "\n out float4 ocol1 : COLOR1," : "", @@ -693,7 +695,7 @@ const char *GeneratePixelShaderCode(DSTALPHA_MODE dstAlphaMode, API_TYPE ApiType // Once we switch to GLSL 1.3 we will bind a lot of these. - if(dstAlphaMode == DSTALPHA_DUAL_SOURCE_BLEND) + if (dstAlphaMode == DSTALPHA_DUAL_SOURCE_BLEND) { // This won't get hit unless we support GL 3.3 WRITE(p, " layout(location = 0) out float4 ocol0;\n"); @@ -703,7 +705,7 @@ const char *GeneratePixelShaderCode(DSTALPHA_MODE dstAlphaMode, API_TYPE ApiType { WRITE(p, " float4 ocol0;\n"); } - if(DepthTextureEnable) + if (DepthTextureEnable) WRITE(p, " float depth;\n"); // TODO: Passed to Vertex Shader right? WRITE(p, " float4 rawpos = gl_FragCoord;\n"); @@ -716,13 +718,13 @@ const char *GeneratePixelShaderCode(DSTALPHA_MODE dstAlphaMode, API_TYPE ApiType for (int i = 0; i < numTexgen; ++i) WRITE(p, " float3 uv%d = gl_TexCoord[%d].xyz;\n", i, i); WRITE(p, " float4 clipPos = gl_TexCoord[%d];\n", numTexgen); - if(g_ActiveConfig.bEnablePixelLighting && g_ActiveConfig.backend_info.bSupportsPixelLighting) + if (g_ActiveConfig.bEnablePixelLighting && g_ActiveConfig.backend_info.bSupportsPixelLighting) WRITE(p, " float4 Normal = gl_TexCoord[%d];\n", numTexgen + 1); } else { // wpos is in w of first 4 texcoords - if(g_ActiveConfig.bEnablePixelLighting && g_ActiveConfig.backend_info.bSupportsPixelLighting) + if (g_ActiveConfig.bEnablePixelLighting && g_ActiveConfig.backend_info.bSupportsPixelLighting) { for (int i = 0; i < 8; ++i) WRITE(p, " float4 uv%d = gl_TexCoord[%d];\n", i, i); @@ -767,7 +769,7 @@ const char *GeneratePixelShaderCode(DSTALPHA_MODE dstAlphaMode, API_TYPE ApiType " float4 cc2=float4(0.0f,0.0f,0.0f,0.0f), cprev=float4(0.0f,0.0f,0.0f,0.0f);\n" " float4 crastemp=float4(0.0f,0.0f,0.0f,0.0f),ckonsttemp=float4(0.0f,0.0f,0.0f,0.0f);\n\n"); - if(g_ActiveConfig.bEnablePixelLighting && g_ActiveConfig.backend_info.bSupportsPixelLighting) + if (g_ActiveConfig.bEnablePixelLighting && g_ActiveConfig.backend_info.bSupportsPixelLighting) { if (xfregs.numTexGen.numTexGens < 7) { @@ -814,7 +816,7 @@ const char *GeneratePixelShaderCode(DSTALPHA_MODE dstAlphaMode, API_TYPE ApiType } // indirect texture map lookup - for(u32 i = 0; i < bpmem.genMode.numindstages; ++i) + for (u32 i = 0; i < bpmem.genMode.numindstages; ++i) { if (nIndirectStagesUsed & (1< 7 || ka > 7) + if (kc > 7 || ka > 7) { WRITE(p, "ckonsttemp = frac(konsttemp * (255.0f/256.0f)) * (256.0f/255.0f);\n"); } @@ -1110,9 +1112,9 @@ static void WriteStage(char *&p, int n, API_TYPE ApiType) } if(cc.a == TEVCOLORARG_CPREV || cc.a == TEVCOLORARG_APREV - || cc.b == TEVCOLORARG_CPREV || cc.b == TEVCOLORARG_APREV - || cc.c == TEVCOLORARG_CPREV || cc.c == TEVCOLORARG_APREV - || ac.a == TEVALPHAARG_APREV || ac.b == TEVALPHAARG_APREV || ac.c == TEVALPHAARG_APREV) + || cc.b == TEVCOLORARG_CPREV || cc.b == TEVCOLORARG_APREV + || cc.c == TEVCOLORARG_CPREV || cc.c == TEVCOLORARG_APREV + || ac.a == TEVALPHAARG_APREV || ac.b == TEVALPHAARG_APREV || ac.c == TEVALPHAARG_APREV) WRITE(p, "cprev = frac(prev * (255.0f/256.0f)) * (256.0f/255.0f);\n"); @@ -1150,7 +1152,7 @@ static void WriteStage(char *&p, int n, API_TYPE ApiType) if (cc.shift > TEVSCALE_1) WRITE(p, "%s*(", tevScaleTable[cc.shift]); - if(!(cc.d == TEVCOLORARG_ZERO && cc.op == TEVOP_ADD)) + if (!(cc.d == TEVCOLORARG_ZERO && cc.op == TEVOP_ADD)) WRITE(p, "%s%s", tevCInputTable[cc.d], tevOpTable[cc.op]); if (cc.a == cc.b) @@ -1197,7 +1199,7 @@ static void WriteStage(char *&p, int n, API_TYPE ApiType) if (ac.shift > TEVSCALE_1) WRITE(p, "%s*(", tevScaleTable[ac.shift]); - if(!(ac.d == TEVALPHAARG_ZERO && ac.op == TEVOP_ADD)) + if (!(ac.d == TEVALPHAARG_ZERO && ac.op == TEVOP_ADD)) WRITE(p, "%s.a%s", tevAInputTable[ac.d], tevOpTable[ac.op]); if (ac.a == ac.b) @@ -1213,7 +1215,7 @@ static void WriteStage(char *&p, int n, API_TYPE ApiType) WRITE(p, "%s",tevBiasTable[ac.bias]); - if (ac.shift>0) + if (ac.shift > 0) WRITE(p, ")"); } @@ -1303,7 +1305,7 @@ static bool WriteAlphaTest(char *&p, API_TYPE ApiType,DSTALPHA_MODE dstAlphaMode }; int Pretest = AlphaPreTest(); - if(Pretest >= 0) + if (Pretest >= 0) { return Pretest != 0; } @@ -1339,7 +1341,8 @@ static const char *tevFogFuncsTable[] = static void WriteFog(char *&p) { - if(bpmem.fog.c_proj_fsel.fsel == 0)return;//no Fog + if (bpmem.fog.c_proj_fsel.fsel == 0) + return; // no Fog if (bpmem.fog.c_proj_fsel.proj == 0) { @@ -1357,7 +1360,7 @@ static void WriteFog(char *&p) // x_adjust = sqrt((x-center)^2 + k^2)/k // ze *= x_adjust //this is complitly teorical as the real hard seems to use a table intead of calculate the values. - if(bpmem.fogRange.Base.Enabled) + if (bpmem.fogRange.Base.Enabled) { WRITE (p, " float x_adjust = (2.0f * (clipPos.x / "I_FOG"[2].y)) - 1.0f - "I_FOG"[2].x;\n"); WRITE (p, " x_adjust = sqrt(x_adjust * x_adjust + "I_FOG"[2].z * "I_FOG"[2].z) / "I_FOG"[2].z;\n"); @@ -1366,17 +1369,15 @@ static void WriteFog(char *&p) WRITE (p, " float fog = saturate(ze - "I_FOG"[1].z);\n"); - if(bpmem.fog.c_proj_fsel.fsel > 3) + if (bpmem.fog.c_proj_fsel.fsel > 3) { WRITE(p, "%s", tevFogFuncsTable[bpmem.fog.c_proj_fsel.fsel]); } else { - if(bpmem.fog.c_proj_fsel.fsel != 2) + if (bpmem.fog.c_proj_fsel.fsel != 2) WARN_LOG(VIDEO, "Unknown Fog Type! %08x", bpmem.fog.c_proj_fsel.fsel); } WRITE(p, " prev.rgb = lerp(prev.rgb,"I_FOG"[0].rgb,fog);\n"); - - } diff --git a/Source/Core/VideoCommon/Src/TextureConversionShader.cpp b/Source/Core/VideoCommon/Src/TextureConversionShader.cpp index 2d3b642593..3fc83ddb7c 100644 --- a/Source/Core/VideoCommon/Src/TextureConversionShader.cpp +++ b/Source/Core/VideoCommon/Src/TextureConversionShader.cpp @@ -66,17 +66,19 @@ u16 GetEncodedSampleCount(u32 format) default: return 1; } } + const char* WriteRegister(API_TYPE ApiType, const char *prefix, const u32 num) { - if(ApiType == API_GLSL) + if (ApiType == API_GLSL) return ""; // Once we switch to GLSL 1.3 we can do something here static char result[64]; sprintf(result, " : register(%s%d)", prefix, num); return result; } + const char *WriteLocation(API_TYPE ApiType) { - if(ApiType == API_GLSL && g_ActiveConfig.backend_info.bSupportsGLSLUBO) + if (ApiType == API_GLSL && g_ActiveConfig.backend_info.bSupportsGLSLUBO) return ""; static char result[64]; sprintf(result, "uniform "); @@ -90,12 +92,12 @@ void WriteSwizzler(char*& p, u32 format, API_TYPE ApiType) // [0] left, top, right, bottom of source rectangle within source texture // [1] width and height of destination texture in pixels // Two were merged for GLSL - if(ApiType == API_GLSL && g_ActiveConfig.backend_info.bSupportsGLSLUBO) + if (ApiType == API_GLSL && g_ActiveConfig.backend_info.bSupportsGLSLUBO) WRITE(p, "layout(std140%s) uniform PSBlock {\n", g_ActiveConfig.backend_info.bSupportsGLSLBinding ? ", binding = 1" : ""); WRITE(p, "%sfloat4 "I_COLORS"[2] %s;\n", WriteLocation(ApiType), WriteRegister(ApiType, "c", C_COLORS)); - if(ApiType == API_GLSL && g_ActiveConfig.backend_info.bSupportsGLSLUBO) + if (ApiType == API_GLSL && g_ActiveConfig.backend_info.bSupportsGLSLUBO) WRITE(p, "};\n"); float blkW = (float)TexDecoder_GetBlockWidthInTexels(format); @@ -122,7 +124,7 @@ void WriteSwizzler(char*& p, u32 format, API_TYPE ApiType) } - if(ApiType == API_GLSL) + if (ApiType == API_GLSL) { WRITE(p, " float4 ocol0;\n"); WRITE(p, " float2 uv0 = gl_TexCoord[0].xy;\n"); @@ -131,7 +133,7 @@ void WriteSwizzler(char*& p, u32 format, API_TYPE ApiType) else { WRITE(p,"void main(\n"); - if(ApiType != API_D3D11) + if (ApiType != API_D3D11) { WRITE(p," out float4 ocol0 : COLOR0,\n"); } @@ -163,12 +165,12 @@ void WriteSwizzler(char*& p, u32 format, API_TYPE ApiType) WRITE(p, " sampleUv = sampleUv * "I_COLORS"[0].xy;\n"); - if(ApiType == API_OPENGL || ApiType == API_GLSL) + if (ApiType == API_OPENGL || ApiType == API_GLSL) WRITE(p," sampleUv.y = "I_COLORS"[1].y - sampleUv.y;\n"); WRITE(p, " sampleUv = sampleUv + "I_COLORS"[1].zw;\n"); - if(ApiType != API_OPENGL && ApiType != API_GLSL) + if (ApiType != API_OPENGL && ApiType != API_GLSL) { WRITE(p, " sampleUv = sampleUv + float2(0.0f,1.0f);\n");// still to determine the reason for this WRITE(p, " sampleUv = sampleUv / "I_COLORS"[0].zw;\n"); @@ -182,17 +184,17 @@ void Write32BitSwizzler(char*& p, u32 format, API_TYPE ApiType) // [0] left, top, right, bottom of source rectangle within source texture // [1] width and height of destination texture in pixels // Two were merged for GLSL - if(ApiType == API_GLSL && g_ActiveConfig.backend_info.bSupportsGLSLUBO) + if (ApiType == API_GLSL && g_ActiveConfig.backend_info.bSupportsGLSLUBO) WRITE(p, "layout(std140%s) uniform PSBlock {\n", g_ActiveConfig.backend_info.bSupportsGLSLBinding ? ", binding = 1" : ""); WRITE(p, "%sfloat4 "I_COLORS"[2] %s;\n", WriteLocation(ApiType), WriteRegister(ApiType, "c", C_COLORS)); - if(ApiType == API_GLSL && g_ActiveConfig.backend_info.bSupportsGLSLUBO) + if (ApiType == API_GLSL && g_ActiveConfig.backend_info.bSupportsGLSLUBO) WRITE(p, "};\n"); float blkW = (float)TexDecoder_GetBlockWidthInTexels(format); float blkH = (float)TexDecoder_GetBlockHeightInTexels(format); // 32 bit textures (RGBA8 and Z24) are store in 2 cache line increments - if(ApiType == API_OPENGL) + if (ApiType == API_OPENGL) { WRITE(p,"uniform samplerRECT samp0 : register(s0);\n"); } @@ -212,7 +214,7 @@ void Write32BitSwizzler(char*& p, u32 format, API_TYPE ApiType) WRITE(p, "Texture2D Tex0 : register(t0);\n"); } - if(ApiType == API_GLSL) + if (ApiType == API_GLSL) { WRITE(p, " float4 ocol0;\n"); WRITE(p, " float2 uv0 = gl_TexCoord[0].xy;\n"); @@ -249,18 +251,17 @@ void Write32BitSwizzler(char*& p, u32 format, API_TYPE ApiType) WRITE(p, " float xl = floor(x2 / %f);\n", blkW); WRITE(p, " float xib = x2 - (xl * %f);\n", blkW); WRITE(p, " float halfxb = floor(xb / 2);\n"); - WRITE(p, " sampleUv.x = xib + (halfxb * %f);\n", blkW); WRITE(p, " sampleUv.y = yb + xoff;\n"); WRITE(p, " sampleUv = sampleUv * "I_COLORS"[0].xy;\n"); - if(ApiType == API_OPENGL || ApiType == API_GLSL) + if (ApiType == API_OPENGL || ApiType == API_GLSL) WRITE(p," sampleUv.y = "I_COLORS"[1].y - sampleUv.y;\n"); WRITE(p, " sampleUv = sampleUv + "I_COLORS"[1].zw;\n"); - if(ApiType != API_OPENGL && ApiType != API_GLSL) + if (ApiType != API_OPENGL && ApiType != API_GLSL) { WRITE(p, " sampleUv = sampleUv + float2(0.0f,1.0f);\n");// still to determine the reason for this WRITE(p, " sampleUv = sampleUv / "I_COLORS"[0].zw;\n"); @@ -281,7 +282,7 @@ void WriteSampleColor(char*& p, const char* colorComp, const char* dest, API_TYP // the increment of sampleUv.x is delayed, so we perform it here. see WriteIncrementSampleX. const char* texSampleIncrementUnit; - if(ApiType != API_OPENGL || ApiType != API_GLSL) + if (ApiType != API_OPENGL || ApiType != API_GLSL) texSampleIncrementUnit = I_COLORS"[0].x / "I_COLORS"[0].z"; else texSampleIncrementUnit = I_COLORS"[0].x"; @@ -292,7 +293,7 @@ void WriteSampleColor(char*& p, const char* colorComp, const char* dest, API_TYP void WriteColorToIntensity(char*& p, const char* src, const char* dest) { - if(!IntensityConstantAdded) + if (!IntensityConstantAdded) { WRITE(p, " float4 IntensityConst = float4(0.257f,0.504f,0.098f,0.0625f);\n"); IntensityConstantAdded = true; @@ -328,7 +329,7 @@ void WriteToBitDepth(char*& p, u8 depth, const char* src, const char* dest) void WriteEncoderEnd(char* p, API_TYPE ApiType) { - if(ApiType == API_GLSL) + if (ApiType == API_GLSL) WRITE(p, "gl_FragData[0] = ocol0;\n"); WRITE(p, "}\n"); IntensityConstantAdded = false; @@ -337,22 +338,22 @@ void WriteEncoderEnd(char* p, API_TYPE ApiType) void WriteI8Encoder(char* p, API_TYPE ApiType) { - WriteSwizzler(p, GX_TF_I8,ApiType); + WriteSwizzler(p, GX_TF_I8, ApiType); WRITE(p, " float3 texSample;\n"); - WriteSampleColor(p, "rgb", "texSample",ApiType); + WriteSampleColor(p, "rgb", "texSample", ApiType); WriteColorToIntensity(p, "texSample", "ocol0.b"); - WriteIncrementSampleX(p,ApiType); + WriteIncrementSampleX(p, ApiType); - WriteSampleColor(p, "rgb", "texSample",ApiType); + WriteSampleColor(p, "rgb", "texSample", ApiType); WriteColorToIntensity(p, "texSample", "ocol0.g"); - WriteIncrementSampleX(p,ApiType); + WriteIncrementSampleX(p, ApiType); - WriteSampleColor(p, "rgb", "texSample",ApiType); + WriteSampleColor(p, "rgb", "texSample", ApiType); WriteColorToIntensity(p, "texSample", "ocol0.r"); - WriteIncrementSampleX(p,ApiType); + WriteIncrementSampleX(p, ApiType); - WriteSampleColor(p, "rgb", "texSample",ApiType); + WriteSampleColor(p, "rgb", "texSample", ApiType); WriteColorToIntensity(p, "texSample", "ocol0.a"); WRITE(p, " ocol0.rgba += IntensityConst.aaaa;\n"); // see WriteColorToIntensity @@ -362,40 +363,40 @@ void WriteI8Encoder(char* p, API_TYPE ApiType) void WriteI4Encoder(char* p, API_TYPE ApiType) { - WriteSwizzler(p, GX_TF_I4,ApiType); + WriteSwizzler(p, GX_TF_I4, ApiType); WRITE(p, " float3 texSample;\n"); WRITE(p, " float4 color0;\n"); WRITE(p, " float4 color1;\n"); - WriteSampleColor(p, "rgb", "texSample",ApiType); + WriteSampleColor(p, "rgb", "texSample", ApiType); WriteColorToIntensity(p, "texSample", "color0.b"); - WriteIncrementSampleX(p,ApiType); + WriteIncrementSampleX(p, ApiType); - WriteSampleColor(p, "rgb", "texSample",ApiType); + WriteSampleColor(p, "rgb", "texSample", ApiType); WriteColorToIntensity(p, "texSample", "color1.b"); - WriteIncrementSampleX(p,ApiType); + WriteIncrementSampleX(p, ApiType); - WriteSampleColor(p, "rgb", "texSample",ApiType); + WriteSampleColor(p, "rgb", "texSample", ApiType); WriteColorToIntensity(p, "texSample", "color0.g"); - WriteIncrementSampleX(p,ApiType); + WriteIncrementSampleX(p, ApiType); - WriteSampleColor(p, "rgb", "texSample",ApiType); + WriteSampleColor(p, "rgb", "texSample", ApiType); WriteColorToIntensity(p, "texSample", "color1.g"); - WriteIncrementSampleX(p,ApiType); + WriteIncrementSampleX(p, ApiType); - WriteSampleColor(p, "rgb", "texSample",ApiType); + WriteSampleColor(p, "rgb", "texSample", ApiType); WriteColorToIntensity(p, "texSample", "color0.r"); - WriteIncrementSampleX(p,ApiType); + WriteIncrementSampleX(p, ApiType); - WriteSampleColor(p, "rgb", "texSample",ApiType); + WriteSampleColor(p, "rgb", "texSample", ApiType); WriteColorToIntensity(p, "texSample", "color1.r"); - WriteIncrementSampleX(p,ApiType); + WriteIncrementSampleX(p, ApiType); - WriteSampleColor(p, "rgb", "texSample",ApiType); + WriteSampleColor(p, "rgb", "texSample", ApiType); WriteColorToIntensity(p, "texSample", "color0.a"); - WriteIncrementSampleX(p,ApiType); + WriteIncrementSampleX(p, ApiType); - WriteSampleColor(p, "rgb", "texSample",ApiType); + WriteSampleColor(p, "rgb", "texSample", ApiType); WriteColorToIntensity(p, "texSample", "color1.a"); WRITE(p, " color0.rgba += IntensityConst.aaaa;\n"); @@ -410,15 +411,15 @@ void WriteI4Encoder(char* p, API_TYPE ApiType) void WriteIA8Encoder(char* p,API_TYPE ApiType) { - WriteSwizzler(p, GX_TF_IA8,ApiType); + WriteSwizzler(p, GX_TF_IA8, ApiType); WRITE(p, " float4 texSample;\n"); - WriteSampleColor(p, "rgba", "texSample",ApiType); + WriteSampleColor(p, "rgba", "texSample", ApiType); WRITE(p, " ocol0.b = texSample.a;\n"); WriteColorToIntensity(p, "texSample", "ocol0.g"); - WriteIncrementSampleX(p,ApiType); + WriteIncrementSampleX(p, ApiType); - WriteSampleColor(p, "rgba", "texSample",ApiType); + WriteSampleColor(p, "rgba", "texSample", ApiType); WRITE(p, " ocol0.r = texSample.a;\n"); WriteColorToIntensity(p, "texSample", "ocol0.a"); @@ -429,27 +430,27 @@ void WriteIA8Encoder(char* p,API_TYPE ApiType) void WriteIA4Encoder(char* p,API_TYPE ApiType) { - WriteSwizzler(p, GX_TF_IA4,ApiType); + WriteSwizzler(p, GX_TF_IA4, ApiType); WRITE(p, " float4 texSample;\n"); WRITE(p, " float4 color0;\n"); WRITE(p, " float4 color1;\n"); - WriteSampleColor(p, "rgba", "texSample",ApiType); + WriteSampleColor(p, "rgba", "texSample", ApiType); WRITE(p, " color0.b = texSample.a;\n"); WriteColorToIntensity(p, "texSample", "color1.b"); - WriteIncrementSampleX(p,ApiType); + WriteIncrementSampleX(p, ApiType); - WriteSampleColor(p, "rgba", "texSample",ApiType); + WriteSampleColor(p, "rgba", "texSample", ApiType); WRITE(p, " color0.g = texSample.a;\n"); WriteColorToIntensity(p, "texSample", "color1.g"); - WriteIncrementSampleX(p,ApiType); + WriteIncrementSampleX(p, ApiType); - WriteSampleColor(p, "rgba", "texSample",ApiType); + WriteSampleColor(p, "rgba", "texSample", ApiType); WRITE(p, " color0.r = texSample.a;\n"); WriteColorToIntensity(p, "texSample", "color1.r"); - WriteIncrementSampleX(p,ApiType); + WriteIncrementSampleX(p, ApiType); - WriteSampleColor(p, "rgba", "texSample",ApiType); + WriteSampleColor(p, "rgba", "texSample", ApiType); WRITE(p, " color0.a = texSample.a;\n"); WriteColorToIntensity(p, "texSample", "color1.a"); @@ -464,11 +465,11 @@ void WriteIA4Encoder(char* p,API_TYPE ApiType) void WriteRGB565Encoder(char* p,API_TYPE ApiType) { - WriteSwizzler(p, GX_TF_RGB565,ApiType); + WriteSwizzler(p, GX_TF_RGB565, ApiType); - WriteSampleColor(p, "rgb", "float3 texSample0",ApiType); - WriteIncrementSampleX(p,ApiType); - WriteSampleColor(p, "rgb", "float3 texSample1",ApiType); + WriteSampleColor(p, "rgb", "float3 texSample0", ApiType); + WriteIncrementSampleX(p, ApiType); + WriteSampleColor(p, "rgb", "float3 texSample1", ApiType); WRITE(p, " float2 texRs = float2(texSample0.r, texSample1.r);\n"); WRITE(p, " float2 texGs = float2(texSample0.g, texSample1.g);\n"); WRITE(p, " float2 texBs = float2(texSample0.b, texSample1.b);\n"); @@ -488,14 +489,14 @@ void WriteRGB565Encoder(char* p,API_TYPE ApiType) void WriteRGB5A3Encoder(char* p,API_TYPE ApiType) { - WriteSwizzler(p, GX_TF_RGB5A3,ApiType); + WriteSwizzler(p, GX_TF_RGB5A3, ApiType); WRITE(p, " float4 texSample;\n"); WRITE(p, " float color0;\n"); WRITE(p, " float gUpper;\n"); WRITE(p, " float gLower;\n"); - WriteSampleColor(p, "rgba", "texSample",ApiType); + WriteSampleColor(p, "rgba", "texSample", ApiType); // 0.8784 = 224 / 255 which is the maximum alpha value that can be represented in 3 bits WRITE(p, "if(texSample.a > 0.878f) {\n"); @@ -522,9 +523,9 @@ void WriteRGB5A3Encoder(char* p,API_TYPE ApiType) WRITE(p, "}\n"); - WriteIncrementSampleX(p,ApiType); + WriteIncrementSampleX(p, ApiType); - WriteSampleColor(p, "rgba", "texSample",ApiType); + WriteSampleColor(p, "rgba", "texSample", ApiType); WRITE(p, "if(texSample.a > 0.878f) {\n"); @@ -555,21 +556,21 @@ void WriteRGB5A3Encoder(char* p,API_TYPE ApiType) void WriteRGBA4443Encoder(char* p,API_TYPE ApiType) { - WriteSwizzler(p, GX_TF_RGB5A3,ApiType); + WriteSwizzler(p, GX_TF_RGB5A3, ApiType); WRITE(p, " float4 texSample;\n"); WRITE(p, " float4 color0;\n"); WRITE(p, " float4 color1;\n"); - WriteSampleColor(p, "rgba", "texSample",ApiType); + WriteSampleColor(p, "rgba", "texSample", ApiType); WriteToBitDepth(p, 3, "texSample.a", "color0.b"); WriteToBitDepth(p, 4, "texSample.r", "color1.b"); WriteToBitDepth(p, 4, "texSample.g", "color0.g"); WriteToBitDepth(p, 4, "texSample.b", "color1.g"); - WriteIncrementSampleX(p,ApiType); + WriteIncrementSampleX(p, ApiType); - WriteSampleColor(p, "rgba", "texSample",ApiType); + WriteSampleColor(p, "rgba", "texSample", ApiType); WriteToBitDepth(p, 3, "texSample.a", "color0.r"); WriteToBitDepth(p, 4, "texSample.r", "color1.r"); WriteToBitDepth(p, 4, "texSample.g", "color0.a"); @@ -581,7 +582,7 @@ void WriteRGBA4443Encoder(char* p,API_TYPE ApiType) void WriteRGBA8Encoder(char* p,API_TYPE ApiType) { - Write32BitSwizzler(p, GX_TF_RGBA8,ApiType); + Write32BitSwizzler(p, GX_TF_RGBA8, ApiType); WRITE(p, " float cl1 = xb - (halfxb * 2);\n"); WRITE(p, " float cl0 = 1.0f - cl1;\n"); @@ -590,15 +591,15 @@ void WriteRGBA8Encoder(char* p,API_TYPE ApiType) WRITE(p, " float4 color0;\n"); WRITE(p, " float4 color1;\n"); - WriteSampleColor(p, "rgba", "texSample",ApiType); + WriteSampleColor(p, "rgba", "texSample", ApiType); WRITE(p, " color0.b = texSample.a;\n"); WRITE(p, " color0.g = texSample.r;\n"); WRITE(p, " color1.b = texSample.g;\n"); WRITE(p, " color1.g = texSample.b;\n"); - WriteIncrementSampleX(p,ApiType); + WriteIncrementSampleX(p, ApiType); - WriteSampleColor(p, "rgba", "texSample",ApiType); + WriteSampleColor(p, "rgba", "texSample", ApiType); WRITE(p, " color0.r = texSample.a;\n"); WRITE(p, " color0.a = texSample.r;\n"); WRITE(p, " color1.r = texSample.g;\n"); @@ -611,32 +612,32 @@ void WriteRGBA8Encoder(char* p,API_TYPE ApiType) void WriteC4Encoder(char* p, const char* comp,API_TYPE ApiType) { - WriteSwizzler(p, GX_CTF_R4,ApiType); + WriteSwizzler(p, GX_CTF_R4, ApiType); WRITE(p, " float4 color0;\n"); WRITE(p, " float4 color1;\n"); - WriteSampleColor(p, comp, "color0.b",ApiType); - WriteIncrementSampleX(p,ApiType); + WriteSampleColor(p, comp, "color0.b", ApiType); + WriteIncrementSampleX(p, ApiType); - WriteSampleColor(p, comp, "color1.b",ApiType); - WriteIncrementSampleX(p,ApiType); + WriteSampleColor(p, comp, "color1.b", ApiType); + WriteIncrementSampleX(p, ApiType); - WriteSampleColor(p, comp, "color0.g",ApiType); - WriteIncrementSampleX(p,ApiType); + WriteSampleColor(p, comp, "color0.g", ApiType); + WriteIncrementSampleX(p, ApiType); - WriteSampleColor(p, comp, "color1.g",ApiType); - WriteIncrementSampleX(p,ApiType); + WriteSampleColor(p, comp, "color1.g", ApiType); + WriteIncrementSampleX(p, ApiType); - WriteSampleColor(p, comp, "color0.r",ApiType); - WriteIncrementSampleX(p,ApiType); + WriteSampleColor(p, comp, "color0.r", ApiType); + WriteIncrementSampleX(p, ApiType); - WriteSampleColor(p, comp, "color1.r",ApiType); - WriteIncrementSampleX(p,ApiType); + WriteSampleColor(p, comp, "color1.r", ApiType); + WriteIncrementSampleX(p, ApiType); - WriteSampleColor(p, comp, "color0.a",ApiType); - WriteIncrementSampleX(p,ApiType); + WriteSampleColor(p, comp, "color0.a", ApiType); + WriteIncrementSampleX(p, ApiType); - WriteSampleColor(p, comp, "color1.a",ApiType); + WriteSampleColor(p, comp, "color1.a", ApiType); WriteToBitDepth(p, 4, "color0", "color0"); WriteToBitDepth(p, 4, "color1", "color1"); @@ -647,45 +648,45 @@ void WriteC4Encoder(char* p, const char* comp,API_TYPE ApiType) void WriteC8Encoder(char* p, const char* comp,API_TYPE ApiType) { - WriteSwizzler(p, GX_CTF_R8,ApiType); + WriteSwizzler(p, GX_CTF_R8, ApiType); - WriteSampleColor(p, comp, "ocol0.b",ApiType); - WriteIncrementSampleX(p,ApiType); + WriteSampleColor(p, comp, "ocol0.b", ApiType); + WriteIncrementSampleX(p, ApiType); - WriteSampleColor(p, comp, "ocol0.g",ApiType); - WriteIncrementSampleX(p,ApiType); + WriteSampleColor(p, comp, "ocol0.g", ApiType); + WriteIncrementSampleX(p, ApiType); - WriteSampleColor(p, comp, "ocol0.r",ApiType); - WriteIncrementSampleX(p,ApiType); + WriteSampleColor(p, comp, "ocol0.r", ApiType); + WriteIncrementSampleX(p, ApiType); - WriteSampleColor(p, comp, "ocol0.a",ApiType); + WriteSampleColor(p, comp, "ocol0.a", ApiType); WriteEncoderEnd(p, ApiType); } void WriteCC4Encoder(char* p, const char* comp,API_TYPE ApiType) { - WriteSwizzler(p, GX_CTF_RA4,ApiType); + WriteSwizzler(p, GX_CTF_RA4, ApiType); WRITE(p, " float2 texSample;\n"); WRITE(p, " float4 color0;\n"); WRITE(p, " float4 color1;\n"); - WriteSampleColor(p, comp, "texSample",ApiType); + WriteSampleColor(p, comp, "texSample", ApiType); WRITE(p, " color0.b = texSample.x;\n"); WRITE(p, " color1.b = texSample.y;\n"); - WriteIncrementSampleX(p,ApiType); + WriteIncrementSampleX(p, ApiType); - WriteSampleColor(p, comp, "texSample",ApiType); + WriteSampleColor(p, comp, "texSample", ApiType); WRITE(p, " color0.g = texSample.x;\n"); WRITE(p, " color1.g = texSample.y;\n"); - WriteIncrementSampleX(p,ApiType); + WriteIncrementSampleX(p, ApiType); - WriteSampleColor(p, comp, "texSample",ApiType); + WriteSampleColor(p, comp, "texSample", ApiType); WRITE(p, " color0.r = texSample.x;\n"); WRITE(p, " color1.r = texSample.y;\n"); - WriteIncrementSampleX(p,ApiType); + WriteIncrementSampleX(p, ApiType); - WriteSampleColor(p, comp, "texSample",ApiType); + WriteSampleColor(p, comp, "texSample", ApiType); WRITE(p, " color0.a = texSample.x;\n"); WRITE(p, " color1.a = texSample.y;\n"); @@ -698,35 +699,35 @@ void WriteCC4Encoder(char* p, const char* comp,API_TYPE ApiType) void WriteCC8Encoder(char* p, const char* comp, API_TYPE ApiType) { - WriteSwizzler(p, GX_CTF_RA8,ApiType); + WriteSwizzler(p, GX_CTF_RA8, ApiType); - WriteSampleColor(p, comp, "ocol0.bg",ApiType); - WriteIncrementSampleX(p,ApiType); + WriteSampleColor(p, comp, "ocol0.bg", ApiType); + WriteIncrementSampleX(p, ApiType); - WriteSampleColor(p, comp, "ocol0.ra",ApiType); + WriteSampleColor(p, comp, "ocol0.ra", ApiType); WriteEncoderEnd(p, ApiType); } void WriteZ8Encoder(char* p, const char* multiplier,API_TYPE ApiType) { - WriteSwizzler(p, GX_CTF_Z8M,ApiType); + WriteSwizzler(p, GX_CTF_Z8M, ApiType); WRITE(p, " float depth;\n"); - WriteSampleColor(p, "b", "depth",ApiType); + WriteSampleColor(p, "b", "depth", ApiType); WRITE(p, "ocol0.b = frac(depth * %s);\n", multiplier); - WriteIncrementSampleX(p,ApiType); + WriteIncrementSampleX(p, ApiType); - WriteSampleColor(p, "b", "depth",ApiType); + WriteSampleColor(p, "b", "depth", ApiType); WRITE(p, "ocol0.g = frac(depth * %s);\n", multiplier); - WriteIncrementSampleX(p,ApiType); + WriteIncrementSampleX(p, ApiType); - WriteSampleColor(p, "b", "depth",ApiType); + WriteSampleColor(p, "b", "depth", ApiType); WRITE(p, "ocol0.r = frac(depth * %s);\n", multiplier); - WriteIncrementSampleX(p,ApiType); + WriteIncrementSampleX(p, ApiType); - WriteSampleColor(p, "b", "depth",ApiType); + WriteSampleColor(p, "b", "depth", ApiType); WRITE(p, "ocol0.a = frac(depth * %s);\n", multiplier); WriteEncoderEnd(p, ApiType); @@ -734,14 +735,14 @@ void WriteZ8Encoder(char* p, const char* multiplier,API_TYPE ApiType) void WriteZ16Encoder(char* p,API_TYPE ApiType) { - WriteSwizzler(p, GX_TF_Z16,ApiType); + WriteSwizzler(p, GX_TF_Z16, ApiType); WRITE(p, " float depth;\n"); WRITE(p, " float3 expanded;\n"); // byte order is reversed - WriteSampleColor(p, "b", "depth",ApiType); + WriteSampleColor(p, "b", "depth", ApiType); WRITE(p, " depth *= 16777215.0f;\n"); WRITE(p, " expanded.r = floor(depth / (256 * 256));\n"); @@ -751,9 +752,9 @@ void WriteZ16Encoder(char* p,API_TYPE ApiType) WRITE(p, " ocol0.b = expanded.g / 255;\n"); WRITE(p, " ocol0.g = expanded.r / 255;\n"); - WriteIncrementSampleX(p,ApiType); + WriteIncrementSampleX(p, ApiType); - WriteSampleColor(p, "b", "depth",ApiType); + WriteSampleColor(p, "b", "depth", ApiType); WRITE(p, " depth *= 16777215.0f;\n"); WRITE(p, " expanded.r = floor(depth / (256 * 256));\n"); @@ -768,14 +769,14 @@ void WriteZ16Encoder(char* p,API_TYPE ApiType) void WriteZ16LEncoder(char* p,API_TYPE ApiType) { - WriteSwizzler(p, GX_CTF_Z16L,ApiType); + WriteSwizzler(p, GX_CTF_Z16L, ApiType); WRITE(p, " float depth;\n"); WRITE(p, " float3 expanded;\n"); // byte order is reversed - WriteSampleColor(p, "b", "depth",ApiType); + WriteSampleColor(p, "b", "depth", ApiType); WRITE(p, " depth *= 16777215.0f;\n"); WRITE(p, " expanded.r = floor(depth / (256 * 256));\n"); @@ -787,9 +788,9 @@ void WriteZ16LEncoder(char* p,API_TYPE ApiType) WRITE(p, " ocol0.b = expanded.b / 255;\n"); WRITE(p, " ocol0.g = expanded.g / 255;\n"); - WriteIncrementSampleX(p,ApiType); + WriteIncrementSampleX(p, ApiType); - WriteSampleColor(p, "b", "depth",ApiType); + WriteSampleColor(p, "b", "depth", ApiType); WRITE(p, " depth *= 16777215.0f;\n"); WRITE(p, " expanded.r = floor(depth / (256 * 256));\n"); @@ -806,7 +807,7 @@ void WriteZ16LEncoder(char* p,API_TYPE ApiType) void WriteZ24Encoder(char* p, API_TYPE ApiType) { - Write32BitSwizzler(p, GX_TF_Z24X8,ApiType); + Write32BitSwizzler(p, GX_TF_Z24X8, ApiType); WRITE(p, " float cl = xb - (halfxb * 2);\n"); @@ -815,19 +816,19 @@ void WriteZ24Encoder(char* p, API_TYPE ApiType) WRITE(p, " float3 expanded0;\n"); WRITE(p, " float3 expanded1;\n"); - WriteSampleColor(p, "b", "depth0",ApiType); - WriteIncrementSampleX(p,ApiType); - WriteSampleColor(p, "b", "depth1",ApiType); + WriteSampleColor(p, "b", "depth0", ApiType); + WriteIncrementSampleX(p, ApiType); + WriteSampleColor(p, "b", "depth1", ApiType); for (int i = 0; i < 2; i++) { - WRITE(p, " depth%i *= 16777215.0f;\n", i); + WRITE(p, " depth%i *= 16777215.0f;\n", i); - WRITE(p, " expanded%i.r = floor(depth%i / (256 * 256));\n", i, i); - WRITE(p, " depth%i -= expanded%i.r * 256 * 256;\n", i, i); - WRITE(p, " expanded%i.g = floor(depth%i / 256);\n", i, i); - WRITE(p, " depth%i -= expanded%i.g * 256;\n", i, i); - WRITE(p, " expanded%i.b = depth%i;\n", i, i); + WRITE(p, " expanded%i.r = floor(depth%i / (256 * 256));\n", i, i); + WRITE(p, " depth%i -= expanded%i.r * 256 * 256;\n", i, i); + WRITE(p, " expanded%i.g = floor(depth%i / 256);\n", i, i); + WRITE(p, " depth%i -= expanded%i.g * 256;\n", i, i); + WRITE(p, " expanded%i.b = depth%i;\n", i, i); } WRITE(p, " if(cl > 0.5f) {\n"); @@ -854,7 +855,7 @@ const char *GenerateEncodingShader(u32 format,API_TYPE ApiType) char *p = text; - if(ApiType == API_GLSL) + if (ApiType == API_GLSL) { // A few required defines and ones that will make our lives a lot easier if (g_ActiveConfig.backend_info.bSupportsGLSLBinding || g_ActiveConfig.backend_info.bSupportsGLSLUBO) @@ -881,76 +882,76 @@ const char *GenerateEncodingShader(u32 format,API_TYPE ApiType) WRITE(p, "#ifdef GL_ARB_texture_rectangle\n #extension GL_ARB_texture_rectangle : require\n#endif\n"); } - switch(format) + switch (format) { case GX_TF_I4: - WriteI4Encoder(p,ApiType); + WriteI4Encoder(p, ApiType); break; case GX_TF_I8: - WriteI8Encoder(p,ApiType); + WriteI8Encoder(p, ApiType); break; case GX_TF_IA4: - WriteIA4Encoder(p,ApiType); + WriteIA4Encoder(p, ApiType); break; case GX_TF_IA8: - WriteIA8Encoder(p,ApiType); + WriteIA8Encoder(p, ApiType); break; case GX_TF_RGB565: - WriteRGB565Encoder(p,ApiType); + WriteRGB565Encoder(p, ApiType); break; case GX_TF_RGB5A3: - WriteRGB5A3Encoder(p,ApiType); + WriteRGB5A3Encoder(p, ApiType); break; case GX_TF_RGBA8: - WriteRGBA8Encoder(p,ApiType); + WriteRGBA8Encoder(p, ApiType); break; case GX_CTF_R4: - WriteC4Encoder(p, "r",ApiType); + WriteC4Encoder(p, "r", ApiType); break; case GX_CTF_RA4: - WriteCC4Encoder(p, "ar",ApiType); + WriteCC4Encoder(p, "ar", ApiType); break; case GX_CTF_RA8: - WriteCC8Encoder(p, "ar",ApiType); + WriteCC8Encoder(p, "ar", ApiType); break; case GX_CTF_A8: - WriteC8Encoder(p, "a",ApiType); + WriteC8Encoder(p, "a", ApiType); break; case GX_CTF_R8: - WriteC8Encoder(p, "r",ApiType); + WriteC8Encoder(p, "r", ApiType); break; case GX_CTF_G8: - WriteC8Encoder(p, "g",ApiType); + WriteC8Encoder(p, "g", ApiType); break; case GX_CTF_B8: - WriteC8Encoder(p, "b",ApiType); + WriteC8Encoder(p, "b", ApiType); break; case GX_CTF_RG8: - WriteCC8Encoder(p, "rg",ApiType); + WriteCC8Encoder(p, "rg", ApiType); break; case GX_CTF_GB8: - WriteCC8Encoder(p, "gb",ApiType); + WriteCC8Encoder(p, "gb", ApiType); break; case GX_TF_Z8: - WriteC8Encoder(p, "b",ApiType); + WriteC8Encoder(p, "b", ApiType); break; case GX_TF_Z16: - WriteZ16Encoder(p,ApiType); + WriteZ16Encoder(p, ApiType); break; case GX_TF_Z24X8: - WriteZ24Encoder(p,ApiType); + WriteZ24Encoder(p, ApiType); break; case GX_CTF_Z4: - WriteC4Encoder(p, "b",ApiType); + WriteC4Encoder(p, "b", ApiType); break; case GX_CTF_Z8M: - WriteZ8Encoder(p, "256.0f",ApiType); + WriteZ8Encoder(p, "256.0f", ApiType); break; case GX_CTF_Z8L: - WriteZ8Encoder(p, "65536.0f" ,ApiType); + WriteZ8Encoder(p, "65536.0f" , ApiType); break; case GX_CTF_Z16L: - WriteZ16LEncoder(p,ApiType); + WriteZ16LEncoder(p, ApiType); break; default: PanicAlert("Unknown texture copy format: 0x%x\n", format); diff --git a/Source/Core/VideoCommon/Src/VertexShaderGen.cpp b/Source/Core/VideoCommon/Src/VertexShaderGen.cpp index 7585597d43..9f945f1b95 100644 --- a/Source/Core/VideoCommon/Src/VertexShaderGen.cpp +++ b/Source/Core/VideoCommon/Src/VertexShaderGen.cpp @@ -185,65 +185,64 @@ const char *GenerateVertexShaderCode(u32 components, API_TYPE ApiType) char *p = text; WRITE(p, "//Vertex Shader: comp:%x, \n", components); - if(ApiType == API_GLSL) + if (ApiType == API_GLSL) { - // A few required defines and ones that will make our lives a lot easier - if (g_ActiveConfig.backend_info.bSupportsGLSLBinding || g_ActiveConfig.backend_info.bSupportsGLSLUBO) - { - WRITE(p, "#version 330 compatibility\n"); - if (g_ActiveConfig.backend_info.bSupportsGLSLBinding) - WRITE(p, "#extension GL_ARB_shading_language_420pack : enable\n"); - if (g_ActiveConfig.backend_info.bSupportsGLSLUBO) - WRITE(p, "#extension GL_ARB_uniform_buffer_object : enable\n"); - WRITE(p, "#define ATTRIN in\n"); - WRITE(p, "#define ATTROUT out\n"); - } - else - { - WRITE(p, "#version 120\n"); - WRITE(p, "#define ATTRIN attribute\n"); - WRITE(p, "#define ATTROUT attribute\n"); - } - if(g_ActiveConfig.backend_info.bSupportsGLSLATTRBind) - WRITE(p, "#extension GL_ARB_explicit_attrib_location : enable\n"); - // Silly differences - WRITE(p, "#define float2 vec2\n"); - WRITE(p, "#define float3 vec3\n"); - WRITE(p, "#define float4 vec4\n"); + // A few required defines and ones that will make our lives a lot easier + if (g_ActiveConfig.backend_info.bSupportsGLSLBinding || g_ActiveConfig.backend_info.bSupportsGLSLUBO) + { + WRITE(p, "#version 330 compatibility\n"); + if (g_ActiveConfig.backend_info.bSupportsGLSLBinding) + WRITE(p, "#extension GL_ARB_shading_language_420pack : enable\n"); + if (g_ActiveConfig.backend_info.bSupportsGLSLUBO) + WRITE(p, "#extension GL_ARB_uniform_buffer_object : enable\n"); + WRITE(p, "#define ATTRIN in\n"); + WRITE(p, "#define ATTROUT out\n"); + } + else + { + WRITE(p, "#version 120\n"); + WRITE(p, "#define ATTRIN attribute\n"); + WRITE(p, "#define ATTROUT attribute\n"); + } + if (g_ActiveConfig.backend_info.bSupportsGLSLATTRBind) + WRITE(p, "#extension GL_ARB_explicit_attrib_location : enable\n"); + // Silly differences + WRITE(p, "#define float2 vec2\n"); + WRITE(p, "#define float3 vec3\n"); + WRITE(p, "#define float4 vec4\n"); - // cg to glsl function translation - WRITE(p, "#define frac(x) fract(x)\n"); - WRITE(p, "#define saturate(x) clamp(x, 0.0f, 1.0f)\n"); - WRITE(p, "#define lerp(x, y, z) mix(x, y, z)\n"); + // cg to glsl function translation + WRITE(p, "#define frac(x) fract(x)\n"); + WRITE(p, "#define saturate(x) clamp(x, 0.0f, 1.0f)\n"); + WRITE(p, "#define lerp(x, y, z) mix(x, y, z)\n"); } // uniforms - if(ApiType == API_GLSL && g_ActiveConfig.backend_info.bSupportsGLSLUBO) + if (ApiType == API_GLSL && g_ActiveConfig.backend_info.bSupportsGLSLUBO) WRITE(p, "layout(std140) uniform VSBlock {\n"); - WRITE(p, "%sfloat4 "I_POSNORMALMATRIX"[6] %s;\n", WriteLocation(ApiType), WriteRegister(ApiType, "c", C_POSNORMALMATRIX)); - WRITE(p, "%sfloat4 "I_PROJECTION"[4] %s;\n", WriteLocation(ApiType), WriteRegister(ApiType, "c", C_PROJECTION)); - WRITE(p, "%sfloat4 "I_MATERIALS"[4] %s;\n", WriteLocation(ApiType), WriteRegister(ApiType, "c", C_MATERIALS)); - WRITE(p, "%sfloat4 "I_LIGHTS"[40] %s;\n", WriteLocation(ApiType), WriteRegister(ApiType, "c", C_LIGHTS)); - WRITE(p, "%sfloat4 "I_TEXMATRICES"[24] %s;\n", WriteLocation(ApiType), WriteRegister(ApiType, "c", C_TEXMATRICES)); // also using tex matrices - WRITE(p, "%sfloat4 "I_TRANSFORMMATRICES"[64] %s;\n", WriteLocation(ApiType),WriteRegister(ApiType, "c", C_TRANSFORMMATRICES)); - WRITE(p, "%sfloat4 "I_NORMALMATRICES"[32] %s;\n", WriteLocation(ApiType), WriteRegister(ApiType, "c", C_NORMALMATRICES)); - WRITE(p, "%sfloat4 "I_POSTTRANSFORMMATRICES"[64] %s;\n", WriteLocation(ApiType), WriteRegister(ApiType, "c", C_POSTTRANSFORMMATRICES)); - WRITE(p, "%sfloat4 "I_DEPTHPARAMS" %s;\n", WriteLocation(ApiType), WriteRegister(ApiType, "c", C_DEPTHPARAMS)); + WRITE(p, "%sfloat4 "I_POSNORMALMATRIX"[6] %s;\n", WriteLocation(ApiType), WriteRegister(ApiType, "c", C_POSNORMALMATRIX)); + WRITE(p, "%sfloat4 "I_PROJECTION"[4] %s;\n", WriteLocation(ApiType), WriteRegister(ApiType, "c", C_PROJECTION)); + WRITE(p, "%sfloat4 "I_MATERIALS"[4] %s;\n", WriteLocation(ApiType), WriteRegister(ApiType, "c", C_MATERIALS)); + WRITE(p, "%sfloat4 "I_LIGHTS"[40] %s;\n", WriteLocation(ApiType), WriteRegister(ApiType, "c", C_LIGHTS)); + WRITE(p, "%sfloat4 "I_TEXMATRICES"[24] %s;\n", WriteLocation(ApiType), WriteRegister(ApiType, "c", C_TEXMATRICES)); // also using tex matrices + WRITE(p, "%sfloat4 "I_TRANSFORMMATRICES"[64] %s;\n", WriteLocation(ApiType),WriteRegister(ApiType, "c", C_TRANSFORMMATRICES)); + WRITE(p, "%sfloat4 "I_NORMALMATRICES"[32] %s;\n", WriteLocation(ApiType), WriteRegister(ApiType, "c", C_NORMALMATRICES)); + WRITE(p, "%sfloat4 "I_POSTTRANSFORMMATRICES"[64] %s;\n", WriteLocation(ApiType), WriteRegister(ApiType, "c", C_POSTTRANSFORMMATRICES)); + WRITE(p, "%sfloat4 "I_DEPTHPARAMS" %s;\n", WriteLocation(ApiType), WriteRegister(ApiType, "c", C_DEPTHPARAMS)); - if(ApiType == API_GLSL && g_ActiveConfig.backend_info.bSupportsGLSLUBO) + if (ApiType == API_GLSL && g_ActiveConfig.backend_info.bSupportsGLSLUBO) WRITE(p, "};\n"); - p = GenerateVSOutputStruct(p, components, ApiType); - if(ApiType == API_GLSL) + if(ApiType == API_GLSL) { if (components & VB_HAS_NRM0) - WRITE(p, " float3 rawnorm0 = gl_Normal; // NORMAL0,\n"); + WRITE(p, " float3 rawnorm0 = gl_Normal; // NORMAL0,\n"); - if(g_ActiveConfig.backend_info.bSupportsGLSLATTRBind) + if (g_ActiveConfig.backend_info.bSupportsGLSLATTRBind) { if (components & VB_HAS_POSMTXIDX) WRITE(p, "layout(location = %d) ATTRIN float fposmtx;\n", SHADER_POSMTX_ATTRIB); @@ -277,7 +276,6 @@ const char *GenerateVertexShaderCode(u32 components, API_TYPE ApiType) } else { - WRITE(p, "VS_OUTPUT main(\n"); // inputs @@ -306,9 +304,7 @@ const char *GenerateVertexShaderCode(u32 components, API_TYPE ApiType) } if (components & VB_HAS_POSMTXIDX) { if (is_d3d) - { WRITE(p, " float4 blend_indices : BLENDINDICES,\n"); - } else WRITE(p, " float fposmtx : ATTR%d,\n", SHADER_POSMTX_ATTRIB); } @@ -369,7 +365,7 @@ const char *GenerateVertexShaderCode(u32 components, API_TYPE ApiType) "float3 ldir, h;\n" "float dist, dist2, attn;\n"); - if(xfregs.numChan.numColorChans == 0) + if (xfregs.numChan.numColorChans == 0) { if (components & VB_HAS_COL0) WRITE(p, "o.colors_0 = color0;\n"); @@ -380,7 +376,7 @@ const char *GenerateVertexShaderCode(u32 components, API_TYPE ApiType) // TODO: This probably isn't necessary if pixel lighting is enabled. p = GenerateLightingShader(p, components, I_MATERIALS, I_LIGHTS, "color", "o.colors_"); - if(xfregs.numChan.numColorChans < 2) + if (xfregs.numChan.numColorChans < 2) { if (components & VB_HAS_COL1) WRITE(p, "o.colors_1 = color1;\n"); @@ -570,30 +566,30 @@ const char *GenerateVertexShaderCode(u32 components, API_TYPE ApiType) //seems to get rather complicated } - if(ApiType == API_GLSL) + if (ApiType == API_GLSL) { // Bit ugly here // Will look better when we bind uniforms in GLSL 1.3 // clipPos/w needs to be done in pixel shader, not here if (xfregs.numTexGen.numTexGens < 7) { - for (unsigned int i = 0; i < xfregs.numTexGen.numTexGens; ++i) - WRITE(p, " gl_TexCoord[%d].xyz = o.tex%d;\n", i, i); - WRITE(p, " gl_TexCoord[%d] = o.clipPos;\n", xfregs.numTexGen.numTexGens); - if(g_ActiveConfig.bEnablePixelLighting && g_ActiveConfig.backend_info.bSupportsPixelLighting) - WRITE(p, " gl_TexCoord[%d] = o.Normal;\n", xfregs.numTexGen.numTexGens + 1); + for (unsigned int i = 0; i < xfregs.numTexGen.numTexGens; ++i) + WRITE(p, " gl_TexCoord[%d].xyz = o.tex%d;\n", i, i); + WRITE(p, " gl_TexCoord[%d] = o.clipPos;\n", xfregs.numTexGen.numTexGens); + if(g_ActiveConfig.bEnablePixelLighting && g_ActiveConfig.backend_info.bSupportsPixelLighting) + WRITE(p, " gl_TexCoord[%d] = o.Normal;\n", xfregs.numTexGen.numTexGens + 1); } else { - // clip position is in w of first 4 texcoords - if(g_ActiveConfig.bEnablePixelLighting && g_ActiveConfig.backend_info.bSupportsPixelLighting) - { - for (int i = 0; i < 8; ++i) - WRITE(p, " gl_TexCoord[%d] = o.tex%d;\n", i, i); - } - else - { - for (unsigned int i = 0; i < xfregs.numTexGen.numTexGens; ++i) - WRITE(p, " gl_TexCoord[%d]%s = o.tex%d;\n", i, i < 4 ? ".xyzw" : ".xyz" , i); - } + // clip position is in w of first 4 texcoords + if (g_ActiveConfig.bEnablePixelLighting && g_ActiveConfig.backend_info.bSupportsPixelLighting) + { + for (int i = 0; i < 8; ++i) + WRITE(p, " gl_TexCoord[%d] = o.tex%d;\n", i, i); + } + else + { + for (unsigned int i = 0; i < xfregs.numTexGen.numTexGens; ++i) + WRITE(p, " gl_TexCoord[%d]%s = o.tex%d;\n", i, i < 4 ? ".xyzw" : ".xyz" , i); + } } WRITE(p, "gl_FrontColor = o.colors_0;\n"); WRITE(p, "gl_FrontSecondaryColor = o.colors_1;\n"); @@ -601,8 +597,7 @@ const char *GenerateVertexShaderCode(u32 components, API_TYPE ApiType) WRITE(p, "}\n"); } else - WRITE(p, "return o;\n}\n"); - + WRITE(p, "return o;\n}\n"); if (text[sizeof(text) - 1] != 0x7C) PanicAlert("VertexShader generator - buffer too small, canary has been eaten!"); diff --git a/Source/Plugins/Plugin_VideoOGL/Src/PixelShaderCache.cpp b/Source/Plugins/Plugin_VideoOGL/Src/PixelShaderCache.cpp index 9c7463442a..345aa79115 100644 --- a/Source/Plugins/Plugin_VideoOGL/Src/PixelShaderCache.cpp +++ b/Source/Plugins/Plugin_VideoOGL/Src/PixelShaderCache.cpp @@ -20,7 +20,6 @@ #include "GLUtil.h" #include -#include #include "Statistics.h" #include "VideoConfig.h" @@ -51,62 +50,63 @@ bool PixelShaderCache::ShaderEnabled; PixelShaderCache::PSCacheEntry* PixelShaderCache::last_entry = NULL; PIXELSHADERUID PixelShaderCache::last_uid; -void(*pSetPSConstant4f)(unsigned int, float, float, float, float); -void(*pSetPSConstant4fv)(unsigned int, const float*); -void(*pSetMultiPSConstant4fv)(unsigned int, unsigned int, const float*); +void (*pSetPSConstant4f)(unsigned int, float, float, float, float); +void (*pSetPSConstant4fv)(unsigned int, const float*); +void (*pSetMultiPSConstant4fv)(unsigned int, unsigned int, const float*); bool (*pCompilePixelShader)(FRAGMENTSHADER&, const char*); GLuint PixelShaderCache::GetDepthMatrixProgram() { - return s_DepthMatrixProgram.glprogid; + return s_DepthMatrixProgram.glprogid; } GLuint PixelShaderCache::GetColorMatrixProgram() { - return s_ColorMatrixProgram.glprogid; + return s_ColorMatrixProgram.glprogid; } + void PixelShaderCache::Init() { - ShaderEnabled = true; - CurrentShader = 0; - last_entry = NULL; - GL_REPORT_ERRORD(); + ShaderEnabled = true; + CurrentShader = 0; + last_entry = NULL; + GL_REPORT_ERRORD(); - s_displayCompileAlert = true; + s_displayCompileAlert = true; - if(g_ActiveConfig.bUseGLSL) - { + if (g_ActiveConfig.bUseGLSL) + { pSetPSConstant4f = SetGLSLPSConstant4f; pSetPSConstant4fv = SetGLSLPSConstant4fv; pSetMultiPSConstant4fv = SetMultiGLSLPSConstant4fv; pCompilePixelShader = CompileGLSLPixelShader; - } - else - { + } + else + { pSetPSConstant4f = SetCGPSConstant4f; pSetPSConstant4fv = SetCGPSConstant4fv; pSetMultiPSConstant4fv = SetMultiCGPSConstant4fv; pCompilePixelShader = CompileCGPixelShader; glEnable(GL_FRAGMENT_PROGRAM_ARB); - } + } - glGetProgramivARB(GL_FRAGMENT_PROGRAM_ARB, GL_MAX_PROGRAM_NATIVE_ALU_INSTRUCTIONS_ARB, (GLint *)&s_nMaxPixelInstructions); + glGetProgramivARB(GL_FRAGMENT_PROGRAM_ARB, GL_MAX_PROGRAM_NATIVE_ALU_INSTRUCTIONS_ARB, (GLint *)&s_nMaxPixelInstructions); - if(s_nMaxPixelInstructions == 0) // Some combination of drivers and hardware returns zero for some reason. - s_nMaxPixelInstructions = 4096; - if (strstr((const char*)glGetString(GL_VENDOR), "Humper") != NULL) s_nMaxPixelInstructions = 4096; + if (s_nMaxPixelInstructions == 0) // Some combination of drivers and hardware returns zero for some reason. + s_nMaxPixelInstructions = 4096; + if (strstr((const char*)glGetString(GL_VENDOR), "Humper") != NULL) s_nMaxPixelInstructions = 4096; #if CG_VERSION_NUM == 2100 - if (strstr((const char*)glGetString(GL_VENDOR), "ATI") != NULL) - { - s_nMaxPixelInstructions = 4096; - } + if (strstr((const char*)glGetString(GL_VENDOR), "ATI") != NULL) + { + s_nMaxPixelInstructions = 4096; + } #endif - int maxinst, maxattribs; - glGetProgramivARB(GL_FRAGMENT_PROGRAM_ARB, GL_MAX_PROGRAM_NATIVE_INSTRUCTIONS_ARB, (GLint *)&maxinst); - glGetProgramivARB(GL_FRAGMENT_PROGRAM_ARB, GL_MAX_PROGRAM_NATIVE_ATTRIBS_ARB, (GLint *)&maxattribs); - INFO_LOG(VIDEO, "pixel max_alu=%d, max_inst=%d, max_attrib=%d", s_nMaxPixelInstructions, maxinst, maxattribs); - if(g_ActiveConfig.bUseGLSL) + int maxinst, maxattribs; + glGetProgramivARB(GL_FRAGMENT_PROGRAM_ARB, GL_MAX_PROGRAM_NATIVE_INSTRUCTIONS_ARB, (GLint *)&maxinst); + glGetProgramivARB(GL_FRAGMENT_PROGRAM_ARB, GL_MAX_PROGRAM_NATIVE_ATTRIBS_ARB, (GLint *)&maxattribs); + INFO_LOG(VIDEO, "pixel max_alu=%d, max_inst=%d, max_attrib=%d", s_nMaxPixelInstructions, maxinst, maxattribs); + if (g_ActiveConfig.bUseGLSL) { char pmatrixprog[2048]; sprintf(pmatrixprog, "#version %s\n" @@ -137,14 +137,14 @@ void PixelShaderCache::Init() g_ActiveConfig.backend_info.bSupportsGLSLUBO ? "" : "uniform ", g_ActiveConfig.backend_info.bSupportsGLSLUBO ? "};" : "", C_COLORS+5, C_COLORS+6, C_COLORS, C_COLORS+1, C_COLORS+2, C_COLORS+3, C_COLORS+4); - - + + if (!PixelShaderCache::CompilePixelShader(s_ColorMatrixProgram, pmatrixprog)) { ERROR_LOG(VIDEO, "Failed to create color matrix fragment program"); s_ColorMatrixProgram.Destroy(); } - + sprintf(pmatrixprog, "#version %s\n" "#extension GL_ARB_texture_rectangle : enable\n" "%s\n" @@ -183,7 +183,7 @@ void PixelShaderCache::Init() g_ActiveConfig.backend_info.bSupportsGLSLUBO ? "" : "uniform ", g_ActiveConfig.backend_info.bSupportsGLSLUBO ? "};" : "", C_COLORS, C_COLORS+1, C_COLORS+2, C_COLORS+3, C_COLORS+4); - + if (!PixelShaderCache::CompilePixelShader(s_DepthMatrixProgram, pmatrixprog)) { ERROR_LOG(VIDEO, "Failed to create depth matrix fragment program"); @@ -194,20 +194,20 @@ void PixelShaderCache::Init() { char pmatrixprog[2048]; sprintf(pmatrixprog, "!!ARBfp1.0" - "TEMP R0;\n" - "TEMP R1;\n" - "PARAM K0 = { 0.5, 0.5, 0.5, 0.5};\n" - "TEX R0, fragment.texcoord[0], texture[0], RECT;\n" - "MUL R0, R0, program.env[%d];\n" - "ADD R0, R0, K0;\n" - "FLR R0, R0;\n" - "MUL R0, R0, program.env[%d];\n" - "DP4 R1.x, R0, program.env[%d];\n" - "DP4 R1.y, R0, program.env[%d];\n" - "DP4 R1.z, R0, program.env[%d];\n" - "DP4 R1.w, R0, program.env[%d];\n" - "ADD result.color, R1, program.env[%d];\n" - "END\n",C_COLORS+5,C_COLORS+6, C_COLORS, C_COLORS+1, C_COLORS+2, C_COLORS+3, C_COLORS+4); + "TEMP R0;\n" + "TEMP R1;\n" + "PARAM K0 = { 0.5, 0.5, 0.5, 0.5};\n" + "TEX R0, fragment.texcoord[0], texture[0], RECT;\n" + "MUL R0, R0, program.env[%d];\n" + "ADD R0, R0, K0;\n" + "FLR R0, R0;\n" + "MUL R0, R0, program.env[%d];\n" + "DP4 R1.x, R0, program.env[%d];\n" + "DP4 R1.y, R0, program.env[%d];\n" + "DP4 R1.z, R0, program.env[%d];\n" + "DP4 R1.w, R0, program.env[%d];\n" + "ADD result.color, R1, program.env[%d];\n" + "END\n",C_COLORS+5,C_COLORS+6, C_COLORS, C_COLORS+1, C_COLORS+2, C_COLORS+3, C_COLORS+4); glGenProgramsARB(1, &s_ColorMatrixProgram.glprogid); SetCurrentShader(s_ColorMatrixProgram.glprogid); glProgramStringARB(GL_FRAGMENT_PROGRAM_ARB, GL_PROGRAM_FORMAT_ASCII_ARB, (GLsizei)strlen(pmatrixprog), pmatrixprog); @@ -219,50 +219,50 @@ void PixelShaderCache::Init() } sprintf(pmatrixprog, "!!ARBfp1.0\n" - "TEMP R0;\n" - "TEMP R1;\n" - "TEMP R2;\n" - //16777215/16777216*256, 1/255, 256, 0 - "PARAM K0 = { 255.99998474121, 0.003921568627451, 256.0, 0.0};\n" - "PARAM K1 = { 15.0, 0.066666666666, 0.0, 0.0};\n" - //sample the depth value - "TEX R2, fragment.texcoord[0], texture[0], RECT;\n" + "TEMP R0;\n" + "TEMP R1;\n" + "TEMP R2;\n" + //16777215/16777216*256, 1/255, 256, 0 + "PARAM K0 = { 255.99998474121, 0.003921568627451, 256.0, 0.0};\n" + "PARAM K1 = { 15.0, 0.066666666666, 0.0, 0.0};\n" + //sample the depth value + "TEX R2, fragment.texcoord[0], texture[0], RECT;\n" - //scale from [0*16777216..1*16777216] to - //[0*16777215..1*16777215], multiply by 256 - "MUL R0, R2.x, K0.x;\n" // *16777215/16777216*256 + //scale from [0*16777216..1*16777216] to + //[0*16777215..1*16777215], multiply by 256 + "MUL R0, R2.x, K0.x;\n" // *16777215/16777216*256 - //It is easy to get bad results due to low precision - //here, for example converting like this: - //MUL R0,R0,{ 65536, 256, 1, 16777216 } - //FRC R0,R0 - //gives {?, 128/255, 254/255, ?} for depth value 254/255 - //on some gpus + //It is easy to get bad results due to low precision + //here, for example converting like this: + //MUL R0,R0,{ 65536, 256, 1, 16777216 } + //FRC R0,R0 + //gives {?, 128/255, 254/255, ?} for depth value 254/255 + //on some gpus - "FLR R0.x,R0;\n" //bits 31..24 + "FLR R0.x,R0;\n" //bits 31..24 - "SUB R0.yzw,R0,R0.x;\n" //subtract bits 31..24 from rest - "MUL R0.yzw,R0,K0.z;\n" // *256 - "FLR R0.y,R0;\n" //bits 23..16 + "SUB R0.yzw,R0,R0.x;\n" //subtract bits 31..24 from rest + "MUL R0.yzw,R0,K0.z;\n" // *256 + "FLR R0.y,R0;\n" //bits 23..16 - "SUB R0.zw,R0,R0.y;\n" //subtract bits 23..16 from rest - "MUL R0.zw,R0,K0.z;\n" // *256 - "FLR R0.z,R0;\n" //bits 15..8 + "SUB R0.zw,R0,R0.y;\n" //subtract bits 23..16 from rest + "MUL R0.zw,R0,K0.z;\n" // *256 + "FLR R0.z,R0;\n" //bits 15..8 - "MOV R0.w,R0.x;\n" //duplicate bit 31..24 + "MOV R0.w,R0.x;\n" //duplicate bit 31..24 - "MUL R0,R0,K0.y;\n" // /255 + "MUL R0,R0,K0.y;\n" // /255 - "MUL R0.w,R0,K1.x;\n" // *15 - "FLR R0.w,R0;\n" //bits 31..28 - "MUL R0.w,R0,K1.y;\n" // /15 + "MUL R0.w,R0,K1.x;\n" // *15 + "FLR R0.w,R0;\n" //bits 31..28 + "MUL R0.w,R0,K1.y;\n" // /15 - "DP4 R1.x, R0, program.env[%d];\n" - "DP4 R1.y, R0, program.env[%d];\n" - "DP4 R1.z, R0, program.env[%d];\n" - "DP4 R1.w, R0, program.env[%d];\n" - "ADD result.color, R1, program.env[%d];\n" - "END\n", C_COLORS, C_COLORS+1, C_COLORS+2, C_COLORS+3, C_COLORS+4); + "DP4 R1.x, R0, program.env[%d];\n" + "DP4 R1.y, R0, program.env[%d];\n" + "DP4 R1.z, R0, program.env[%d];\n" + "DP4 R1.w, R0, program.env[%d];\n" + "ADD result.color, R1, program.env[%d];\n" + "END\n", C_COLORS, C_COLORS+1, C_COLORS+2, C_COLORS+3, C_COLORS+4); glGenProgramsARB(1, &s_DepthMatrixProgram.glprogid); SetCurrentShader(s_DepthMatrixProgram.glprogid); glProgramStringARB(GL_FRAGMENT_PROGRAM_ARB, GL_PROGRAM_FORMAT_ASCII_ARB, (GLsizei)strlen(pmatrixprog), pmatrixprog); @@ -280,71 +280,71 @@ void PixelShaderCache::Shutdown() { s_ColorMatrixProgram.Destroy(); s_DepthMatrixProgram.Destroy(); - PSCache::iterator iter = PixelShaders.begin(); - for (; iter != PixelShaders.end(); iter++) - iter->second.Destroy(); - PixelShaders.clear(); + PSCache::iterator iter = PixelShaders.begin(); + for (; iter != PixelShaders.end(); iter++) + iter->second.Destroy(); + PixelShaders.clear(); } FRAGMENTSHADER* PixelShaderCache::SetShader(DSTALPHA_MODE dstAlphaMode, u32 components) { - PIXELSHADERUID uid; - GetPixelShaderId(&uid, dstAlphaMode, components); + PIXELSHADERUID uid; + GetPixelShaderId(&uid, dstAlphaMode, components); - // Check if the shader is already set - if (last_entry) - { - if (uid == last_uid) + // Check if the shader is already set + if (last_entry) { - GFX_DEBUGGER_PAUSE_AT(NEXT_PIXEL_SHADER_CHANGE, true); - ValidatePixelShaderIDs(API_OPENGL, last_entry->safe_uid, last_entry->shader.strprog, dstAlphaMode, components); - return &last_entry->shader; + if (uid == last_uid) + { + GFX_DEBUGGER_PAUSE_AT(NEXT_PIXEL_SHADER_CHANGE, true); + ValidatePixelShaderIDs(API_OPENGL, last_entry->safe_uid, last_entry->shader.strprog, dstAlphaMode, components); + return &last_entry->shader; + } } - } - last_uid = uid; + last_uid = uid; - PSCache::iterator iter = PixelShaders.find(uid); - if (iter != PixelShaders.end()) - { - PSCacheEntry &entry = iter->second; - last_entry = &entry; + PSCache::iterator iter = PixelShaders.find(uid); + if (iter != PixelShaders.end()) + { + PSCacheEntry &entry = iter->second; + last_entry = &entry; - GFX_DEBUGGER_PAUSE_AT(NEXT_PIXEL_SHADER_CHANGE, true); - ValidatePixelShaderIDs(API_OPENGL, entry.safe_uid, entry.shader.strprog, dstAlphaMode, components); - return &last_entry->shader; - } + GFX_DEBUGGER_PAUSE_AT(NEXT_PIXEL_SHADER_CHANGE, true); + ValidatePixelShaderIDs(API_OPENGL, entry.safe_uid, entry.shader.strprog, dstAlphaMode, components); + return &last_entry->shader; + } - // Make an entry in the table - PSCacheEntry& newentry = PixelShaders[uid]; - last_entry = &newentry; - const char *code = GeneratePixelShaderCode(dstAlphaMode, g_ActiveConfig.bUseGLSL ? API_GLSL : API_OPENGL, components); + // Make an entry in the table + PSCacheEntry& newentry = PixelShaders[uid]; + last_entry = &newentry; + const char *code = GeneratePixelShaderCode(dstAlphaMode, g_ActiveConfig.bUseGLSL ? API_GLSL : API_OPENGL, components); - if (g_ActiveConfig.bEnableShaderDebugging && code) - { - GetSafePixelShaderId(&newentry.safe_uid, dstAlphaMode, components); - newentry.shader.strprog = code; - } + if (g_ActiveConfig.bEnableShaderDebugging && code) + { + GetSafePixelShaderId(&newentry.safe_uid, dstAlphaMode, components); + newentry.shader.strprog = code; + } #if defined(_DEBUG) || defined(DEBUGFAST) - if (g_ActiveConfig.iLog & CONF_SAVESHADERS && code) { - static int counter = 0; - char szTemp[MAX_PATH]; - sprintf(szTemp, "%sps_%04i.txt", File::GetUserPath(D_DUMP_IDX).c_str(), counter++); + if (g_ActiveConfig.iLog & CONF_SAVESHADERS && code) { + static int counter = 0; + char szTemp[MAX_PATH]; + sprintf(szTemp, "%sps_%04i.txt", File::GetUserPath(D_DUMP_IDX).c_str(), counter++); - SaveData(szTemp, code); - } + SaveData(szTemp, code); + } #endif - if (!code || !CompilePixelShader(newentry.shader, code)) { - GFX_DEBUGGER_PAUSE_AT(NEXT_ERROR, true); - return NULL; - } + if (!code || !CompilePixelShader(newentry.shader, code)) { + GFX_DEBUGGER_PAUSE_AT(NEXT_ERROR, true); + return NULL; + } - INCSTAT(stats.numPixelShadersCreated); - SETSTAT(stats.numPixelShadersAlive, PixelShaders.size()); - GFX_DEBUGGER_PAUSE_AT(NEXT_PIXEL_SHADER_CHANGE, true); - return &last_entry->shader; + INCSTAT(stats.numPixelShadersCreated); + SETSTAT(stats.numPixelShadersAlive, PixelShaders.size()); + GFX_DEBUGGER_PAUSE_AT(NEXT_PIXEL_SHADER_CHANGE, true); + return &last_entry->shader; } bool PixelShaderCache::CompilePixelShader(FRAGMENTSHADER& ps, const char* pstrprogram) @@ -352,35 +352,32 @@ bool PixelShaderCache::CompilePixelShader(FRAGMENTSHADER& ps, const char* pstrpr return pCompilePixelShader(ps, pstrprogram); } -//Disable Fragment programs and reset the selected Program +// Disable Fragment programs and reset the selected Program void PixelShaderCache::DisableShader() { - if(g_ActiveConfig.bUseGLSL) - assert(true); - if(ShaderEnabled) - { - glDisable(GL_FRAGMENT_PROGRAM_ARB); - ShaderEnabled = false; - } + if (ShaderEnabled) + { + glDisable(GL_FRAGMENT_PROGRAM_ARB); + ShaderEnabled = false; + } } -//bind a program if is diferent from the binded oone +// bind a program if is different from the binded oone void PixelShaderCache::SetCurrentShader(GLuint Shader) { - if(g_ActiveConfig.bUseGLSL) - assert(true); - if(!ShaderEnabled) - { - glEnable(GL_FRAGMENT_PROGRAM_ARB); - ShaderEnabled = true; - } - if(CurrentShader != Shader) - { - if(Shader != 0) - CurrentShader = Shader; - glBindProgramARB(GL_FRAGMENT_PROGRAM_ARB, CurrentShader); - } + if (!ShaderEnabled) + { + glEnable(GL_FRAGMENT_PROGRAM_ARB); + ShaderEnabled = true; + } + if (CurrentShader != Shader) + { + if (Shader != 0) + CurrentShader = Shader; + glBindProgramARB(GL_FRAGMENT_PROGRAM_ARB, CurrentShader); + } } + // GLSL Specific bool CompileGLSLPixelShader(FRAGMENTSHADER& ps, const char* pstrprogram) { @@ -404,12 +401,12 @@ bool CompileGLSLPixelShader(FRAGMENTSHADER& ps, const char* pstrprogram) GLchar* infoLog = new GLchar[length]; glGetShaderInfoLog(result, length, &charsWritten, infoLog); WARN_LOG(VIDEO, "PS Shader info log:\n%s", infoLog); - char szTemp[MAX_PATH]; - sprintf(szTemp, "ps_%d.txt", result); - FILE *fp = fopen(szTemp, "wb"); - fwrite(pstrprogram, strlen(pstrprogram), 1, fp); - fclose(fp); - + char szTemp[MAX_PATH]; + sprintf(szTemp, "ps_%d.txt", result); + FILE *fp = fopen(szTemp, "wb"); + fwrite(pstrprogram, strlen(pstrprogram), 1, fp); + fclose(fp); + delete[] infoLog; } // Don't try to use this shader @@ -422,40 +419,43 @@ bool CompileGLSLPixelShader(FRAGMENTSHADER& ps, const char* pstrprogram) ps.bGLSL = true; return true; } + void SetPSConstant4fvByName(const char * name, unsigned int offset, const float *f, const unsigned int count = 1) { - PROGRAMSHADER tmp = ProgramShaderCache::GetShaderProgram(); - for (int a = 0; a < NUM_UNIFORMS; ++a) - if (!strcmp(name, UniformNames[a])) + PROGRAMSHADER tmp = ProgramShaderCache::GetShaderProgram(); + for (int a = 0; a < NUM_UNIFORMS; ++a) { - if(tmp.UniformLocations[a] == -1) - return; - else - { - glUniform4fv(tmp.UniformLocations[a] + offset, count, f); - return; - } + if (!strcmp(name, UniformNames[a])) + { + if (tmp.UniformLocations[a] == -1) + return; + else + { + glUniform4fv(tmp.UniformLocations[a] + offset, count, f); + return; + } + } } } void SetGLSLPSConstant4f(unsigned int const_number, float f1, float f2, float f3, float f4) { - float const f[4] = {f1, f2, f3, f4}; - + float const f[4] = {f1, f2, f3, f4}; + if (g_ActiveConfig.backend_info.bSupportsGLSLUBO) { ProgramShaderCache::SetMultiPSConstant4fv(const_number, f, 1); return; } - for (unsigned int a = 0; a < 10; ++a) - { - if ( const_number >= PSVar_Loc[a].reg && const_number < (PSVar_Loc[a].reg + PSVar_Loc[a].size)) + for (unsigned int a = 0; a < 10; ++a) { - unsigned int offset = const_number - PSVar_Loc[a].reg; - SetPSConstant4fvByName(PSVar_Loc[a].name, offset, f); - return; + if (const_number >= PSVar_Loc[a].reg && const_number < (PSVar_Loc[a].reg + PSVar_Loc[a].size)) + { + unsigned int offset = const_number - PSVar_Loc[a].reg; + SetPSConstant4fvByName(PSVar_Loc[a].name, offset, f); + return; + } } - } } void SetGLSLPSConstant4fv(unsigned int const_number, const float *f) @@ -465,15 +465,15 @@ void SetGLSLPSConstant4fv(unsigned int const_number, const float *f) ProgramShaderCache::SetMultiPSConstant4fv(const_number, f, 1); return; } - for (unsigned int a = 0; a < 10; ++a) - { - if ( const_number >= PSVar_Loc[a].reg && const_number < (PSVar_Loc[a].reg + PSVar_Loc[a].size)) + for (unsigned int a = 0; a < 10; ++a) { - unsigned int offset = const_number - PSVar_Loc[a].reg; - SetPSConstant4fvByName(PSVar_Loc[a].name, offset, f); - return; + if (const_number >= PSVar_Loc[a].reg && const_number < (PSVar_Loc[a].reg + PSVar_Loc[a].size)) + { + unsigned int offset = const_number - PSVar_Loc[a].reg; + SetPSConstant4fvByName(PSVar_Loc[a].name, offset, f); + return; + } } - } } void SetMultiGLSLPSConstant4fv(unsigned int const_number, unsigned int count, const float *f) @@ -483,115 +483,117 @@ void SetMultiGLSLPSConstant4fv(unsigned int const_number, unsigned int count, co ProgramShaderCache::SetMultiPSConstant4fv(const_number, f, count); return; } - for (unsigned int a = 0; a < 10; ++a) - { - if (const_number >= PSVar_Loc[a].reg && const_number < (PSVar_Loc[a].reg + PSVar_Loc[a].size)) + for (unsigned int a = 0; a < 10; ++a) { - unsigned int offset = const_number - PSVar_Loc[a].reg; - SetPSConstant4fvByName(PSVar_Loc[a].name, offset, f, count); - return; + if (const_number >= PSVar_Loc[a].reg && const_number < (PSVar_Loc[a].reg + PSVar_Loc[a].size)) + { + unsigned int offset = const_number - PSVar_Loc[a].reg; + SetPSConstant4fvByName(PSVar_Loc[a].name, offset, f, count); + return; + } } - } } -// CG Specific +// CG Specific bool CompileCGPixelShader(FRAGMENTSHADER& ps, const char* pstrprogram) { - GLenum err = GL_REPORT_ERROR(); - if (err != GL_NO_ERROR) - { - ERROR_LOG(VIDEO, "glError %08x before PS!", err); - } + GLenum err = GL_REPORT_ERROR(); + if (err != GL_NO_ERROR) + { + ERROR_LOG(VIDEO, "glError %08x before PS!", err); + } #if defined HAVE_CG && HAVE_CG - CGprogram tempprog = cgCreateProgram(g_cgcontext, CG_SOURCE, pstrprogram, g_cgfProf, "main", NULL); + CGprogram tempprog = cgCreateProgram(g_cgcontext, CG_SOURCE, pstrprogram, g_cgfProf, "main", NULL); - // handle errors - if (!cgIsProgram(tempprog)) - { - cgDestroyProgram(tempprog); + // handle errors + if (!cgIsProgram(tempprog)) + { + cgDestroyProgram(tempprog); - static int num_failures = 0; - char szTemp[MAX_PATH]; - sprintf(szTemp, "bad_ps_%04i.txt", num_failures++); - std::ofstream file(szTemp); - file << pstrprogram; - file.close(); + static int num_failures = 0; + char szTemp[MAX_PATH]; + sprintf(szTemp, "bad_ps_%04i.txt", num_failures++); + std::ofstream file(szTemp); + file << pstrprogram; + file.close(); - PanicAlert("Failed to compile pixel shader %d!\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 (%d):\n%s", - num_failures - 1, szTemp, - g_cgfProf, - cgGetLastListing(g_cgcontext)); + PanicAlert("Failed to compile pixel shader %d!\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 (%d):\n%s", + num_failures - 1, szTemp, + g_cgfProf, + cgGetLastListing(g_cgcontext)); - return false; - } - - // handle warnings - if (cgGetError() != CG_NO_ERROR) - { - WARN_LOG(VIDEO, "Warnings on compile ps %s:", cgGetLastListing(g_cgcontext)); - WARN_LOG(VIDEO, "%s", pstrprogram); - } - - // This looks evil - we modify the program through the const char * we got from cgGetProgramString! - // It SHOULD not have any nasty side effects though - but you never know... - char *pcompiledprog = (char*)cgGetProgramString(tempprog, CG_COMPILED_PROGRAM); - char *plocal = strstr(pcompiledprog, "program.local"); - while (plocal != NULL) - { - const char *penv = " program.env"; - memcpy(plocal, penv, 13); - plocal = strstr(plocal+13, "program.local"); - } - - glGenProgramsARB(1, &ps.glprogid); - ps.bGLSL = false; - PixelShaderCache::SetCurrentShader(ps.glprogid); - glProgramStringARB(GL_FRAGMENT_PROGRAM_ARB, GL_PROGRAM_FORMAT_ASCII_ARB, (GLsizei)strlen(pcompiledprog), pcompiledprog); - - err = GL_REPORT_ERROR(); - if (err != GL_NO_ERROR) - { - GLint error_pos, native_limit; - glGetIntegerv(GL_PROGRAM_ERROR_POSITION_ARB, &error_pos); - glGetProgramivARB(GL_FRAGMENT_PROGRAM_ARB, GL_PROGRAM_UNDER_NATIVE_LIMITS_ARB, &native_limit); - // Error occur - if (error_pos != -1) { - const char *program_error = (const char *)glGetString(GL_PROGRAM_ERROR_STRING_ARB); - char line[256]; - strncpy(line, (const char *)pcompiledprog + error_pos, 255); - line[255] = 0; - ERROR_LOG(VIDEO, "Error at %i: %s", error_pos, program_error); - ERROR_LOG(VIDEO, "Line dump: \n%s", line); - } else if (native_limit != -1) { - ERROR_LOG(VIDEO, "Hit limit? %i", native_limit); - // TODO + return false; } - ERROR_LOG(VIDEO, "%s", pstrprogram); - ERROR_LOG(VIDEO, "%s", pcompiledprog); - } - cgDestroyProgram(tempprog); + // handle warnings + if (cgGetError() != CG_NO_ERROR) + { + WARN_LOG(VIDEO, "Warnings on compile ps %s:", cgGetLastListing(g_cgcontext)); + WARN_LOG(VIDEO, "%s", pstrprogram); + } + + // This looks evil - we modify the program through the const char * we got from cgGetProgramString! + // It SHOULD not have any nasty side effects though - but you never know... + char *pcompiledprog = (char*)cgGetProgramString(tempprog, CG_COMPILED_PROGRAM); + char *plocal = strstr(pcompiledprog, "program.local"); + while (plocal != NULL) + { + const char *penv = " program.env"; + memcpy(plocal, penv, 13); + plocal = strstr(plocal+13, "program.local"); + } + + glGenProgramsARB(1, &ps.glprogid); + ps.bGLSL = false; + PixelShaderCache::SetCurrentShader(ps.glprogid); + glProgramStringARB(GL_FRAGMENT_PROGRAM_ARB, GL_PROGRAM_FORMAT_ASCII_ARB, (GLsizei)strlen(pcompiledprog), pcompiledprog); + + err = GL_REPORT_ERROR(); + if (err != GL_NO_ERROR) + { + GLint error_pos, native_limit; + glGetIntegerv(GL_PROGRAM_ERROR_POSITION_ARB, &error_pos); + glGetProgramivARB(GL_FRAGMENT_PROGRAM_ARB, GL_PROGRAM_UNDER_NATIVE_LIMITS_ARB, &native_limit); + // Error occur + if (error_pos != -1) { + const char *program_error = (const char *)glGetString(GL_PROGRAM_ERROR_STRING_ARB); + char line[256]; + strncpy(line, (const char *)pcompiledprog + error_pos, 255); + line[255] = 0; + ERROR_LOG(VIDEO, "Error at %i: %s", error_pos, program_error); + ERROR_LOG(VIDEO, "Line dump: \n%s", line); + } else if (native_limit != -1) { + ERROR_LOG(VIDEO, "Hit limit? %i", native_limit); + // TODO + } + ERROR_LOG(VIDEO, "%s", pstrprogram); + ERROR_LOG(VIDEO, "%s", pcompiledprog); + } + + cgDestroyProgram(tempprog); #endif - return true; + return true; } + void SetCGPSConstant4f(unsigned int const_number, float f1, float f2, float f3, float f4) { - float f[4] = { f1, f2, f3, f4 }; - glProgramEnvParameter4fvARB(GL_FRAGMENT_PROGRAM_ARB, const_number, f); + float f[4] = { f1, f2, f3, f4 }; + glProgramEnvParameter4fvARB(GL_FRAGMENT_PROGRAM_ARB, const_number, f); } void SetCGPSConstant4fv(unsigned int const_number, const float *f) { - glProgramEnvParameter4fvARB(GL_FRAGMENT_PROGRAM_ARB, const_number, f); + glProgramEnvParameter4fvARB(GL_FRAGMENT_PROGRAM_ARB, const_number, f); } void SetMultiCGPSConstant4fv(unsigned int const_number, unsigned int count, const float *f) { - for (unsigned int i = 0; i < count; i++,f+=4) - glProgramEnvParameter4fvARB(GL_FRAGMENT_PROGRAM_ARB, const_number + i, f); + for (unsigned int i = 0; i < count; i++,f+=4) + glProgramEnvParameter4fvARB(GL_FRAGMENT_PROGRAM_ARB, const_number + i, f); } + // Renderer functions void Renderer::SetPSConstant4f(unsigned int const_number, float f1, float f2, float f3, float f4) { diff --git a/Source/Plugins/Plugin_VideoOGL/Src/PixelShaderCache.h b/Source/Plugins/Plugin_VideoOGL/Src/PixelShaderCache.h index b0328f336c..c9a6ca6791 100644 --- a/Source/Plugins/Plugin_VideoOGL/Src/PixelShaderCache.h +++ b/Source/Plugins/Plugin_VideoOGL/Src/PixelShaderCache.h @@ -34,7 +34,7 @@ struct FRAGMENTSHADER { if (glprogid) { - if(bGLSL) + if (bGLSL) glDeleteShader(glprogid); else glDeleteProgramsARB(1, &glprogid); diff --git a/Source/Plugins/Plugin_VideoOGL/Src/ProgramShaderCache.cpp b/Source/Plugins/Plugin_VideoOGL/Src/ProgramShaderCache.cpp index a422f80079..008a4a679e 100644 --- a/Source/Plugins/Plugin_VideoOGL/Src/ProgramShaderCache.cpp +++ b/Source/Plugins/Plugin_VideoOGL/Src/ProgramShaderCache.cpp @@ -18,234 +18,247 @@ #include "ProgramShaderCache.h" #include "MathUtil.h" -#include namespace OGL { - GLuint ProgramShaderCache::CurrentFShader = 0, ProgramShaderCache::CurrentVShader = 0, ProgramShaderCache::CurrentProgram = 0; - ProgramShaderCache::PCache ProgramShaderCache::pshaders; - GLuint ProgramShaderCache::s_ps_vs_ubo; - GLintptr ProgramShaderCache::s_vs_data_offset; - LinearDiskCache g_program_disk_cache; - GLenum ProgramFormat; +GLuint ProgramShaderCache::CurrentFShader = 0, ProgramShaderCache::CurrentVShader = 0, ProgramShaderCache::CurrentProgram = 0; +ProgramShaderCache::PCache ProgramShaderCache::pshaders; +GLuint ProgramShaderCache::s_ps_vs_ubo; +GLintptr ProgramShaderCache::s_vs_data_offset; - std::pair ProgramShaderCache::CurrentShaderProgram; - const char *UniformNames[NUM_UNIFORMS] = { - // SAMPLERS - "samp0","samp1","samp2","samp3","samp4","samp5","samp6","samp7", - // PIXEL SHADER UNIFORMS - I_COLORS, - I_KCOLORS, - I_ALPHA, - I_TEXDIMS, - I_ZBIAS , - I_INDTEXSCALE , - I_INDTEXMTX, - I_FOG, - I_PLIGHTS, - I_PMATERIALS, - // VERTEX SHADER UNIFORMS - I_POSNORMALMATRIX, - I_PROJECTION , - I_MATERIALS, - I_LIGHTS, - I_TEXMATRICES, - I_TRANSFORMMATRICES , - I_NORMALMATRICES , - I_POSTTRANSFORMMATRICES, - I_DEPTHPARAMS, - }; +LinearDiskCache g_program_disk_cache; +GLenum ProgramFormat; - void ProgramShaderCache::SetProgramVariables(PCacheEntry &entry, const PROGRAMUID &uid) +std::pair ProgramShaderCache::CurrentShaderProgram; +const char *UniformNames[NUM_UNIFORMS] = +{ + // SAMPLERS + "samp0","samp1","samp2","samp3","samp4","samp5","samp6","samp7", + // PIXEL SHADER UNIFORMS + I_COLORS, + I_KCOLORS, + I_ALPHA, + I_TEXDIMS, + I_ZBIAS , + I_INDTEXSCALE , + I_INDTEXMTX, + I_FOG, + I_PLIGHTS, + I_PMATERIALS, + // VERTEX SHADER UNIFORMS + I_POSNORMALMATRIX, + I_PROJECTION , + I_MATERIALS, + I_LIGHTS, + I_TEXMATRICES, + I_TRANSFORMMATRICES , + I_NORMALMATRICES , + I_POSTTRANSFORMMATRICES, + I_DEPTHPARAMS, +}; + +void ProgramShaderCache::SetProgramVariables(PCacheEntry &entry, const PROGRAMUID &uid) +{ + // Dunno why this is needed when I have the binding + // points statically set in the shader source + // We should only need these two functions when we don't support binding but do support UBO + // Driver Bug? Nvidia GTX 570, 290.xx Driver, Linux x64 + if (g_ActiveConfig.backend_info.bSupportsGLSLUBO) { - // Dunno why this is needed when I have the binding - // points statically set in the shader source - // We should only need these two functions when we don't support binding but do support UBO - // Driver Bug? Nvidia GTX 570, 290.xx Driver, Linux x64 - if (g_ActiveConfig.backend_info.bSupportsGLSLUBO) - { - glUniformBlockBinding( entry.program.glprogid, 0, 1 ); - if(uid.uid.vsid != 0) // Some things have no vertex shader - glUniformBlockBinding( entry.program.glprogid, 1, 2 ); - } - - // We cache our uniform locations for now - // Once we move up to a newer version of GLSL, ~1.30 - // We can remove this - - //For some reason this fails on my hardware - //glGetUniformIndices(entry.program.glprogid, NUM_UNIFORMS, UniformNames, entry.program.UniformLocations); - //Got to do it this crappy way. - if (!g_ActiveConfig.backend_info.bSupportsGLSLUBO) - for(int a = 8; a < NUM_UNIFORMS; ++a) - entry.program.UniformLocations[a] = glGetUniformLocation(entry.program.glprogid, UniformNames[a]); - if (!g_ActiveConfig.backend_info.bSupportsGLSLBinding) - for(int a = 0; a < 8; ++a) - { - // Still need to get sampler locations since we aren't binding them statically in the shaders - entry.program.UniformLocations[a] = glGetUniformLocation(entry.program.glprogid, UniformNames[a]); - if(entry.program.UniformLocations[a] != -1) - glUniform1i(entry.program.UniformLocations[a], a); - } + glUniformBlockBinding(entry.program.glprogid, 0, 1); + // Some things have no vertex shader + if (uid.uid.vsid != 0) + glUniformBlockBinding(entry.program.glprogid, 1, 2); + } - // Need to get some attribute locations - if(uid.uid.vsid != 0 && !g_ActiveConfig.backend_info.bSupportsGLSLATTRBind) // We have no vertex Shader + // We cache our uniform locations for now + // Once we move up to a newer version of GLSL, ~1.30 + // We can remove this + + // (Sonicadvance): For some reason this fails on my hardware + //glGetUniformIndices(entry.program.glprogid, NUM_UNIFORMS, UniformNames, entry.program.UniformLocations); + // Got to do it this crappy way. + if (!g_ActiveConfig.backend_info.bSupportsGLSLUBO) + for (int a = 8; a < NUM_UNIFORMS; ++a) + entry.program.UniformLocations[a] = glGetUniformLocation(entry.program.glprogid, UniformNames[a]); + + if (!g_ActiveConfig.backend_info.bSupportsGLSLBinding) + { + for (int a = 0; a < 8; ++a) { - glBindAttribLocation(entry.program.glprogid, SHADER_NORM1_ATTRIB, "rawnorm1"); - glBindAttribLocation(entry.program.glprogid, SHADER_NORM2_ATTRIB, "rawnorm2"); - glBindAttribLocation(entry.program.glprogid, SHADER_POSMTX_ATTRIB, "fposmtx"); + // Still need to get sampler locations since we aren't binding them statically in the shaders + entry.program.UniformLocations[a] = glGetUniformLocation(entry.program.glprogid, UniformNames[a]); + if (entry.program.UniformLocations[a] != -1) + glUniform1i(entry.program.UniformLocations[a], a); } } - - void ProgramShaderCache::SetBothShaders(GLuint PS, GLuint VS) + // Need to get some attribute locations + if (uid.uid.vsid != 0 && !g_ActiveConfig.backend_info.bSupportsGLSLATTRBind) { - PROGRAMUID uid; - CurrentFShader = PS; - CurrentVShader = VS; - - - GetProgramShaderId(&uid, CurrentVShader, CurrentFShader); - - if(uid.uid.id == 0) - { - CurrentProgram = 0; - glUseProgram(0); - return; - } - - // Fragment shaders can survive without Vertex Shaders - // We have a valid fragment shader, let's create our program - std::pair ShaderPair = std::make_pair(uid.uid.psid, uid.uid.vsid); - PCache::iterator iter = pshaders.find(ShaderPair); - if (iter != pshaders.end()) - { - PCacheEntry &entry = iter->second; - glUseProgram(entry.program.glprogid); - CurrentShaderProgram = ShaderPair; - CurrentProgram = entry.program.glprogid; - return; - } - PCacheEntry entry; - entry.program.vsid = CurrentVShader; - entry.program.psid = CurrentFShader; - entry.program.uid = uid; - entry.program.glprogid = glCreateProgram(); - - // Right, the program is created now - // Let's attach everything - if(entry.program.vsid != 0) // attaching zero vertex shader makes it freak out - glAttachShader(entry.program.glprogid, entry.program.vsid); - - glAttachShader(entry.program.glprogid, entry.program.psid); - #ifdef GLEW_VERSION_4_0 - if (g_ActiveConfig.backend_info.bSupportsGLSLCache) - glProgramParameteri(entry.program.glprogid, GL_PROGRAM_BINARY_RETRIEVABLE_HINT, GL_TRUE); - #endif - - glLinkProgram(entry.program.glprogid); - - glUseProgram(entry.program.glprogid); - - SetProgramVariables(entry, uid); - - pshaders[ShaderPair] = entry; - CurrentShaderProgram = ShaderPair; - CurrentProgram = entry.program.glprogid; - } - - void ProgramShaderCache::SetMultiPSConstant4fv(unsigned int offset, const float *f, unsigned int count) - { - glBufferSubData(GL_UNIFORM_BUFFER, offset * sizeof(float) * 4, - count * sizeof(float) * 4, f); - } - - void ProgramShaderCache::SetMultiVSConstant4fv(unsigned int offset, const float *f, unsigned int count) - { - glBufferSubData(GL_UNIFORM_BUFFER, s_vs_data_offset + offset * sizeof(float) * 4, - count * sizeof(float) * 4, f); - } - - GLuint ProgramShaderCache::GetCurrentProgram(void) { return CurrentProgram; } - - PROGRAMSHADER ProgramShaderCache::GetShaderProgram(void) - { - return pshaders[CurrentShaderProgram].program; - } - void ProgramShaderCache::Init(void) - { - // We have to get the UBO alignment here because - // if we generate a buffer that isn't aligned - // then the UBO will fail. - if (g_ActiveConfig.backend_info.bSupportsGLSLUBO) - { - GLint Align; - glGetIntegerv(GL_UNIFORM_BUFFER_OFFSET_ALIGNMENT, &Align); - - GLintptr const ps_data_size = ROUND_UP(C_PENVCONST_END * sizeof(float) * 4, Align); - GLintptr const vs_data_size = ROUND_UP(C_VENVCONST_END * sizeof(float) * 4, Align); - s_vs_data_offset = ps_data_size; - - // We multiply by *4*4 because we need to get down to basic machine units. - // So multiply by four to get how many floats we have from vec4s - // Then once more to get bytes - glGenBuffers(1, &s_ps_vs_ubo); - glBindBuffer(GL_UNIFORM_BUFFER, s_ps_vs_ubo); - glBufferData(GL_UNIFORM_BUFFER, ps_data_size + vs_data_size, NULL, GL_DYNAMIC_DRAW); - - // Now bind the buffer to the index point - // We know PS is 0 since we have it statically set in the shader - // Repeat for VS shader - glBindBufferRange(GL_UNIFORM_BUFFER, 1, s_ps_vs_ubo, 0, ps_data_size); - glBindBufferRange(GL_UNIFORM_BUFFER, 2, s_ps_vs_ubo, s_vs_data_offset, vs_data_size); - } - #ifdef GLEW_VERSION_4_0 - // Read our shader cache, only if supported - if (g_ActiveConfig.backend_info.bSupportsGLSLCache) - { - char cache_filename[MAX_PATH]; - sprintf(cache_filename, "%sogl-%s-shaders.cache", File::GetUserPath(D_SHADERCACHE_IDX).c_str(), - SConfig::GetInstance().m_LocalCoreStartupParameter.m_strUniqueID.c_str()); - ProgramShaderCacheInserter inserter; - g_program_disk_cache.OpenAndRead(cache_filename, inserter); - - GLint Supported; - glGetIntegerv(GL_NUM_PROGRAM_BINARY_FORMATS, &Supported); - - GLint *Formats = new GLint[Supported]; - glGetIntegerv(GL_PROGRAM_BINARY_FORMATS, Formats); - ProgramFormat = (GLenum)Formats[0]; // We don't really care about format - delete[] Formats; - } - #endif - } - - void ProgramShaderCache::Shutdown(void) - { - if (g_ActiveConfig.backend_info.bSupportsGLSLCache) - { - PCache::iterator iter = pshaders.begin(); - for (; iter != pshaders.end(); ++iter) - g_program_disk_cache.Append(iter->second.program.uid, iter->second.program.Data(), iter->second.program.Size()); - - g_program_disk_cache.Sync(); - g_program_disk_cache.Close(); - } - - PCache::iterator iter = pshaders.begin(); - for (; iter != pshaders.end(); ++iter) - iter->second.Destroy(); - pshaders.clear(); - - if (g_ActiveConfig.backend_info.bSupportsGLSLUBO) - { - glBindBuffer(GL_UNIFORM_BUFFER, 0); - glDeleteBuffers(1, &s_ps_vs_ubo); - s_ps_vs_ubo = 0; - } + // We have no vertex Shader + glBindAttribLocation(entry.program.glprogid, SHADER_NORM1_ATTRIB, "rawnorm1"); + glBindAttribLocation(entry.program.glprogid, SHADER_NORM2_ATTRIB, "rawnorm2"); + glBindAttribLocation(entry.program.glprogid, SHADER_POSMTX_ATTRIB, "fposmtx"); } } +void ProgramShaderCache::SetBothShaders(GLuint PS, GLuint VS) +{ + PROGRAMUID uid; + CurrentFShader = PS; + CurrentVShader = VS; + + GetProgramShaderId(&uid, CurrentVShader, CurrentFShader); + + if (uid.uid.id == 0) + { + CurrentProgram = 0; + glUseProgram(0); + return; + } + + // Fragment shaders can survive without Vertex Shaders + // We have a valid fragment shader, let's create our program + std::pair ShaderPair = std::make_pair(uid.uid.psid, uid.uid.vsid); + PCache::iterator iter = pshaders.find(ShaderPair); + if (iter != pshaders.end()) + { + PCacheEntry &entry = iter->second; + glUseProgram(entry.program.glprogid); + CurrentShaderProgram = ShaderPair; + CurrentProgram = entry.program.glprogid; + return; + } + + PCacheEntry entry; + entry.program.vsid = CurrentVShader; + entry.program.psid = CurrentFShader; + entry.program.uid = uid; + entry.program.glprogid = glCreateProgram(); + + // Right, the program is created now + // Let's attach everything + if (entry.program.vsid != 0) // attaching zero vertex shader makes it freak out + glAttachShader(entry.program.glprogid, entry.program.vsid); + + glAttachShader(entry.program.glprogid, entry.program.psid); + +#ifdef GLEW_VERSION_4_0 + if (g_ActiveConfig.backend_info.bSupportsGLSLCache) + glProgramParameteri(entry.program.glprogid, GL_PROGRAM_BINARY_RETRIEVABLE_HINT, GL_TRUE); +#endif + + glLinkProgram(entry.program.glprogid); + + glUseProgram(entry.program.glprogid); + + SetProgramVariables(entry, uid); + + pshaders[ShaderPair] = entry; + CurrentShaderProgram = ShaderPair; + CurrentProgram = entry.program.glprogid; +} + +void ProgramShaderCache::SetMultiPSConstant4fv(unsigned int offset, const float *f, unsigned int count) +{ + glBufferSubData(GL_UNIFORM_BUFFER, offset * sizeof(float) * 4, + count * sizeof(float) * 4, f); +} + +void ProgramShaderCache::SetMultiVSConstant4fv(unsigned int offset, const float *f, unsigned int count) +{ + glBufferSubData(GL_UNIFORM_BUFFER, s_vs_data_offset + offset * sizeof(float) * 4, + count * sizeof(float) * 4, f); +} + +GLuint ProgramShaderCache::GetCurrentProgram(void) +{ + return CurrentProgram; +} + +PROGRAMSHADER ProgramShaderCache::GetShaderProgram(void) +{ + return pshaders[CurrentShaderProgram].program; +} + +void ProgramShaderCache::Init(void) +{ + // We have to get the UBO alignment here because + // if we generate a buffer that isn't aligned + // then the UBO will fail. + if (g_ActiveConfig.backend_info.bSupportsGLSLUBO) + { + GLint Align; + glGetIntegerv(GL_UNIFORM_BUFFER_OFFSET_ALIGNMENT, &Align); + + GLintptr const ps_data_size = ROUND_UP(C_PENVCONST_END * sizeof(float) * 4, Align); + GLintptr const vs_data_size = ROUND_UP(C_VENVCONST_END * sizeof(float) * 4, Align); + s_vs_data_offset = ps_data_size; + + // We multiply by *4*4 because we need to get down to basic machine units. + // So multiply by four to get how many floats we have from vec4s + // Then once more to get bytes + glGenBuffers(1, &s_ps_vs_ubo); + glBindBuffer(GL_UNIFORM_BUFFER, s_ps_vs_ubo); + glBufferData(GL_UNIFORM_BUFFER, ps_data_size + vs_data_size, NULL, GL_DYNAMIC_DRAW); + + // Now bind the buffer to the index point + // We know PS is 0 since we have it statically set in the shader + // Repeat for VS shader + glBindBufferRange(GL_UNIFORM_BUFFER, 1, s_ps_vs_ubo, 0, ps_data_size); + glBindBufferRange(GL_UNIFORM_BUFFER, 2, s_ps_vs_ubo, s_vs_data_offset, vs_data_size); + } + +#ifdef GLEW_VERSION_4_0 + // Read our shader cache, only if supported + if (g_ActiveConfig.backend_info.bSupportsGLSLCache) + { + char cache_filename[MAX_PATH]; + sprintf(cache_filename, "%sogl-%s-shaders.cache", File::GetUserPath(D_SHADERCACHE_IDX).c_str(), + SConfig::GetInstance().m_LocalCoreStartupParameter.m_strUniqueID.c_str()); + ProgramShaderCacheInserter inserter; + g_program_disk_cache.OpenAndRead(cache_filename, inserter); + + GLint Supported; + glGetIntegerv(GL_NUM_PROGRAM_BINARY_FORMATS, &Supported); + + GLint *Formats = new GLint[Supported]; + glGetIntegerv(GL_PROGRAM_BINARY_FORMATS, Formats); + // We don't really care about format + ProgramFormat = (GLenum)Formats[0]; + delete[] Formats; + } +#endif +} + +void ProgramShaderCache::Shutdown(void) +{ + if (g_ActiveConfig.backend_info.bSupportsGLSLCache) + { + PCache::iterator iter = pshaders.begin(); + for (; iter != pshaders.end(); ++iter) + g_program_disk_cache.Append(iter->second.program.uid, iter->second.program.Data(), iter->second.program.Size()); + + g_program_disk_cache.Sync(); + g_program_disk_cache.Close(); + } + + PCache::iterator iter = pshaders.begin(); + for (; iter != pshaders.end(); ++iter) + iter->second.Destroy(); + pshaders.clear(); + + if (g_ActiveConfig.backend_info.bSupportsGLSLUBO) + { + glBindBuffer(GL_UNIFORM_BUFFER, 0); + glDeleteBuffers(1, &s_ps_vs_ubo); + s_ps_vs_ubo = 0; + } +} + +} // namespace OGL + void GetProgramShaderId(PROGRAMUID *uid, GLuint _v, GLuint _p) { uid->uid.vsid = _v; diff --git a/Source/Plugins/Plugin_VideoOGL/Src/ProgramShaderCache.h b/Source/Plugins/Plugin_VideoOGL/Src/ProgramShaderCache.h index 59bcc8e295..e34ca1c086 100644 --- a/Source/Plugins/Plugin_VideoOGL/Src/ProgramShaderCache.h +++ b/Source/Plugins/Plugin_VideoOGL/Src/ProgramShaderCache.h @@ -15,8 +15,7 @@ // Official SVN repository and contact information can be found at // http://code.google.com/p/dolphin-emu/ -#ifndef _ProgramShaderCache_H_ -#define _ProgramShaderCache_H_ +#pragma once #include "GLUtil.h" @@ -28,18 +27,18 @@ #include "LinearDiskCache.h" #include "ConfigManager.h" - union PID +union PID +{ + struct { - struct { GLuint vsid, psid; - }; - u64 id; }; + u64 id; +}; class PROGRAMUID { public: - PID uid; PROGRAMUID() @@ -51,57 +50,63 @@ public: { uid.id = r.uid.id; } + PROGRAMUID(GLuint _v, GLuint _p) { uid.vsid = _v; uid.psid = _p; } - int GetNumValues() const + u64 GetNumValues() const { return uid.id; } }; + void GetProgramShaderId(PROGRAMUID *uid, GLuint _v, GLuint _p); namespace OGL { -#define NUM_UNIFORMS 27 + +const int NUM_UNIFORMS = 27; extern const char *UniformNames[NUM_UNIFORMS]; extern GLenum ProgramFormat; struct PROGRAMSHADER { - PROGRAMSHADER() : glprogid(0), vsid(0), psid(0), binaryLength(0){} - GLuint glprogid; // opengl program id + PROGRAMSHADER() : glprogid(0), vsid(0), psid(0), binaryLength(0) {} + // opengl program id + GLuint glprogid; GLuint vsid, psid; PROGRAMUID uid; GLint UniformLocations[NUM_UNIFORMS]; GLint binaryLength; + + // TODO at first glance looks bad - malloc/no free/pointer not saved in instance... u8 *Data() { - #ifdef GLEW_VERSION_4_0 +#ifdef GLEW_VERSION_4_0 glGetProgramiv(glprogid, GL_PROGRAM_BINARY_LENGTH, &binaryLength); - u8* binary = (u8*)malloc(binaryLength); - glGetProgramBinary(glprogid, binaryLength, NULL, &ProgramFormat, binary); - return binary; - #else - return NULL; - #endif + u8* binary = (u8*)malloc(binaryLength); + glGetProgramBinary(glprogid, binaryLength, NULL, &ProgramFormat, binary); + return binary; +#else + return NULL; +#endif } + GLint Size() { - #ifdef GLEW_VERSION_4_0 - if(!binaryLength) +#ifdef GLEW_VERSION_4_0 + if (!binaryLength) glGetProgramiv(glprogid, GL_PROGRAM_BINARY_LENGTH, &binaryLength); return binaryLength; - #else +#else return 0; - #endif +#endif } }; - class ProgramShaderCache { struct PCacheEntry @@ -109,50 +114,54 @@ class ProgramShaderCache PROGRAMSHADER program; int frameCount; PCacheEntry() : frameCount(0) {} - void Destroy() { + + void Destroy() + { glDeleteProgram(program.glprogid); program.glprogid = 0; } + u8* Data() { return program.Data(); } + GLint Size() { return program.Size(); } }; - + class ProgramShaderCacheInserter : public LinearDiskCacheReader { - public: - void Read(const PROGRAMUID &key, const u8 *value, u32 value_size) + public: + void Read(const PROGRAMUID &key, const u8 *value, u32 value_size) + { +#ifdef GLEW_VERSION_4_0 + PCacheEntry entry; + + // The two shaders might not even exist anymore + // But it is fine, no need to worry about that + entry.program.vsid = key.uid.vsid; + entry.program.psid = key.uid.psid; + + entry.program.glprogid = glCreateProgram(); + + glProgramBinary(entry.program.glprogid, ProgramFormat, value, value_size); + + GLint success; + glGetProgramiv(entry.program.glprogid, GL_LINK_STATUS, &success); + + if (success) { - #ifdef GLEW_VERSION_4_0 - PCacheEntry entry; - - // The two shaders might not even exist anymore - // But it is fine, no need to worry about that - entry.program.vsid = key.uid.vsid; - entry.program.psid = key.uid.psid; - - entry.program.glprogid = glCreateProgram(); - - glProgramBinary(entry.program.glprogid, ProgramFormat, value, value_size); - - GLint success; - glGetProgramiv(entry.program.glprogid, GL_LINK_STATUS, &success); - - if (success) - { - pshaders[std::make_pair(key.uid.psid, key.uid.vsid)] = entry; - glUseProgram(entry.program.glprogid); - SetProgramVariables(entry, key); - } - #endif + pshaders[std::make_pair(key.uid.psid, key.uid.vsid)] = entry; + glUseProgram(entry.program.glprogid); + SetProgramVariables(entry, key); } +#endif + } }; - + typedef std::map, PCacheEntry> PCache; static PCache pshaders; @@ -162,7 +171,7 @@ class ProgramShaderCache static GLuint s_ps_vs_ubo; static GLintptr s_vs_data_offset; static void SetProgramVariables(PCacheEntry &entry, const PROGRAMUID &uid); - + public: static PROGRAMSHADER GetShaderProgram(void); static void SetBothShaders(GLuint PS, GLuint VS); @@ -170,12 +179,9 @@ public: static void SetMultiPSConstant4fv(unsigned int offset, const float *f, unsigned int count); static void SetMultiVSConstant4fv(unsigned int offset, const float *f, unsigned int count); - + static void Init(void); static void Shutdown(void); - }; } // namespace OGL - -#endif diff --git a/Source/Plugins/Plugin_VideoOGL/Src/Render.cpp b/Source/Plugins/Plugin_VideoOGL/Src/Render.cpp index fa270f051e..a54a8a83b3 100644 --- a/Source/Plugins/Plugin_VideoOGL/Src/Render.cpp +++ b/Source/Plugins/Plugin_VideoOGL/Src/Render.cpp @@ -1439,7 +1439,7 @@ void Renderer::ResetAPIState() { // Gets us to a reasonably sane state where it's possible to do things like // image copies with textured quads, etc. - if(g_ActiveConfig.bUseGLSL) + if (g_ActiveConfig.bUseGLSL) ProgramShaderCache::SetBothShaders(0, 0); else { @@ -1471,7 +1471,7 @@ void Renderer::RestoreAPIState() if (g_ActiveConfig.bWireFrame) glPolygonMode(GL_FRONT_AND_BACK, GL_LINE); - if(g_ActiveConfig.bUseGLSL) + if (g_ActiveConfig.bUseGLSL) ProgramShaderCache::SetBothShaders(0, 0); else { diff --git a/Source/Plugins/Plugin_VideoOGL/Src/TextureCache.cpp b/Source/Plugins/Plugin_VideoOGL/Src/TextureCache.cpp index 1a4b8bb55f..61cedd0f9a 100644 --- a/Source/Plugins/Plugin_VideoOGL/Src/TextureCache.cpp +++ b/Source/Plugins/Plugin_VideoOGL/Src/TextureCache.cpp @@ -208,7 +208,7 @@ void TextureCache::TCacheEntry::Load(unsigned int width, unsigned int height, if (pcfmt != PC_TEX_FMT_DXT1) { if (expanded_width != width) - glPixelStorei(GL_UNPACK_ROW_LENGTH, expanded_width); + glPixelStorei(GL_UNPACK_ROW_LENGTH, expanded_width); if (bHaveMipMaps && autogen_mips) { @@ -222,7 +222,7 @@ void TextureCache::TCacheEntry::Load(unsigned int width, unsigned int height, } if (expanded_width != width) - glPixelStorei(GL_UNPACK_ROW_LENGTH, 0); + glPixelStorei(GL_UNPACK_ROW_LENGTH, 0); } else { @@ -297,7 +297,7 @@ void TextureCache::TCacheEntry::FromRenderTarget(u32 dstAddr, unsigned int dstFo glViewport(0, 0, virtualW, virtualH); - if(g_ActiveConfig.bUseGLSL) + if (g_ActiveConfig.bUseGLSL) ProgramShaderCache::SetBothShaders((srcFormat == PIXELFMT_Z24) ? PixelShaderCache::GetDepthMatrixProgram() : PixelShaderCache::GetColorMatrixProgram(), 0); else PixelShaderCache::SetCurrentShader((srcFormat == PIXELFMT_Z24) ? PixelShaderCache::GetDepthMatrixProgram() : PixelShaderCache::GetColorMatrixProgram()); @@ -382,8 +382,8 @@ TextureCache::~TextureCache() { if (s_TempFramebuffer) { - glDeleteFramebuffersEXT(1, (GLuint*)&s_TempFramebuffer); - s_TempFramebuffer = 0; + glDeleteFramebuffersEXT(1, (GLuint*)&s_TempFramebuffer); + s_TempFramebuffer = 0; } } diff --git a/Source/Plugins/Plugin_VideoOGL/Src/TextureConverter.cpp b/Source/Plugins/Plugin_VideoOGL/Src/TextureConverter.cpp index 0b0cae3aa3..b4dc0a2e80 100644 --- a/Source/Plugins/Plugin_VideoOGL/Src/TextureConverter.cpp +++ b/Source/Plugins/Plugin_VideoOGL/Src/TextureConverter.cpp @@ -61,9 +61,9 @@ static FRAGMENTSHADER s_encodingPrograms[NUM_ENCODING_PROGRAMS]; void CreateRgbToYuyvProgram() { // Output is BGRA because that is slightly faster than RGBA. - if(g_ActiveConfig.bUseGLSL) + if (g_ActiveConfig.bUseGLSL) { - if(g_ActiveConfig.backend_info.bSupportsGLSLBinding) + if (g_ActiveConfig.backend_info.bSupportsGLSLBinding) { const char *FProgram = "#version 330 compatibility\n" @@ -132,9 +132,9 @@ void CreateRgbToYuyvProgram() void CreateYuyvToRgbProgram() { - if(g_ActiveConfig.bUseGLSL) + if (g_ActiveConfig.bUseGLSL) { - if(g_ActiveConfig.backend_info.bSupportsGLSLBinding) + if (g_ActiveConfig.backend_info.bSupportsGLSLBinding) { const char *FProgram = "#version 330 compatibility\n" @@ -410,7 +410,7 @@ void EncodeToRam(u32 address, bool bFromZBuffer, bool bIsIntensityFmt, u32 copyf int readStride = (expandedWidth * cacheBytes) / TexDecoder_GetBlockWidthInTexels(format); g_renderer->ResetAPIState(); - if(g_ActiveConfig.bUseGLSL) + if (g_ActiveConfig.bUseGLSL) ProgramShaderCache::SetBothShaders(texconv_shader.glprogid, 0); else PixelShaderCache::SetCurrentShader(texconv_shader.glprogid); @@ -470,7 +470,7 @@ u64 EncodeToRamFromTexture(u32 address,GLuint source_texture, bool bFromZBuffer, s32 expandedWidth = (width + blkW) & (~blkW); s32 expandedHeight = (height + blkH) & (~blkH); - if(g_ActiveConfig.bUseGLSL) + if (g_ActiveConfig.bUseGLSL) ProgramShaderCache::SetBothShaders(texconv_shader.glprogid, 0); else PixelShaderCache::SetCurrentShader(texconv_shader.glprogid); @@ -516,7 +516,7 @@ void EncodeToRamYUYV(GLuint srcTexture, const TargetRectangle& sourceRc, u8* des { g_renderer->ResetAPIState(); - if(g_ActiveConfig.bUseGLSL) + if (g_ActiveConfig.bUseGLSL) ProgramShaderCache::SetBothShaders(s_rgbToYuyvProgram.glprogid, 0); else PixelShaderCache::SetCurrentShader(s_rgbToYuyvProgram.glprogid); @@ -563,7 +563,7 @@ void DecodeToTexture(u32 xfbAddr, int srcWidth, int srcHeight, GLuint destTextur glBindTexture(GL_TEXTURE_RECTANGLE_ARB, s_srcTexture); // TODO: make this less slow. (How?) - if((GLsizei)s_srcTextureWidth == (GLsizei)srcFmtWidth && (GLsizei)s_srcTextureHeight == (GLsizei)srcHeight) + if ((GLsizei)s_srcTextureWidth == (GLsizei)srcFmtWidth && (GLsizei)s_srcTextureHeight == (GLsizei)srcHeight) { glTexSubImage2D(GL_TEXTURE_RECTANGLE_ARB, 0,0,0,s_srcTextureWidth, s_srcTextureHeight, GL_BGRA, GL_UNSIGNED_BYTE, srcAddr); @@ -577,7 +577,7 @@ void DecodeToTexture(u32 xfbAddr, int srcWidth, int srcHeight, GLuint destTextur } glViewport(0, 0, srcWidth, srcHeight); - if(g_ActiveConfig.bUseGLSL) + if (g_ActiveConfig.bUseGLSL) ProgramShaderCache::SetBothShaders(s_yuyvToRgbProgram.glprogid, 0); else PixelShaderCache::SetCurrentShader(s_yuyvToRgbProgram.glprogid); diff --git a/Source/Plugins/Plugin_VideoOGL/Src/VertexManager.cpp b/Source/Plugins/Plugin_VideoOGL/Src/VertexManager.cpp index 1660e39ed4..3913785407 100644 --- a/Source/Plugins/Plugin_VideoOGL/Src/VertexManager.cpp +++ b/Source/Plugins/Plugin_VideoOGL/Src/VertexManager.cpp @@ -203,7 +203,7 @@ void VertexManager::vFlush() } VERTEXSHADER* vs = VertexShaderCache::SetShader(g_nativeVertexFmt->m_components); - if(g_ActiveConfig.bUseGLSL) + if (g_ActiveConfig.bUseGLSL) ProgramShaderCache::SetBothShaders(ps->glprogid, vs->glprogid); else { @@ -226,10 +226,10 @@ void VertexManager::vFlush() if (useDstAlpha && !dualSourcePossible) { ps = PixelShaderCache::SetShader(DSTALPHA_ALPHA_PASS,g_nativeVertexFmt->m_components); - if(g_ActiveConfig.bUseGLSL) + if (g_ActiveConfig.bUseGLSL) { ProgramShaderCache::SetBothShaders(ps->glprogid, 0); - if(!g_ActiveConfig.backend_info.bSupportsGLSLUBO) + if (!g_ActiveConfig.backend_info.bSupportsGLSLUBO) PixelShaderManager::SetConstants(); // Need to set these again, if we don't support UBO if (g_nativeVertexFmt) g_nativeVertexFmt->SetupVertexPointers(); diff --git a/Source/Plugins/Plugin_VideoOGL/Src/VertexShaderCache.cpp b/Source/Plugins/Plugin_VideoOGL/Src/VertexShaderCache.cpp index a88867095a..59fcd6582e 100644 --- a/Source/Plugins/Plugin_VideoOGL/Src/VertexShaderCache.cpp +++ b/Source/Plugins/Plugin_VideoOGL/Src/VertexShaderCache.cpp @@ -16,7 +16,6 @@ // http://code.google.com/p/dolphin-emu/ #include -#include #include "Globals.h" #include "VideoConfig.h" @@ -55,98 +54,97 @@ bool (*pCompileVertexShader)(VERTEXSHADER&, const char*); void VertexShaderCache::Init() { - ShaderEnabled = true; - CurrentShader = 0; - last_entry = NULL; + ShaderEnabled = true; + CurrentShader = 0; + last_entry = NULL; - if(g_ActiveConfig.bUseGLSL) - { - pSetVSConstant4f = SetGLSLVSConstant4f; - pSetVSConstant4fv = SetGLSLVSConstant4fv; - pSetMultiVSConstant4fv = SetMultiGLSLVSConstant4fv; - pSetMultiVSConstant3fv = SetMultiGLSLVSConstant3fv; - pCompileVertexShader = CompileGLSLVertexShader; - } - else - { - pSetVSConstant4f = SetCGVSConstant4f; - pSetVSConstant4fv = SetCGVSConstant4fv; - pSetMultiVSConstant4fv = SetMultiCGVSConstant4fv; - pSetMultiVSConstant3fv = SetMultiCGVSConstant3fv; - pCompileVertexShader = CompileCGVertexShader; - glEnable(GL_VERTEX_PROGRAM_ARB); - } + if (g_ActiveConfig.bUseGLSL) + { + pSetVSConstant4f = SetGLSLVSConstant4f; + pSetVSConstant4fv = SetGLSLVSConstant4fv; + pSetMultiVSConstant4fv = SetMultiGLSLVSConstant4fv; + pSetMultiVSConstant3fv = SetMultiGLSLVSConstant3fv; + pCompileVertexShader = CompileGLSLVertexShader; + } + else + { + pSetVSConstant4f = SetCGVSConstant4f; + pSetVSConstant4fv = SetCGVSConstant4fv; + pSetMultiVSConstant4fv = SetMultiCGVSConstant4fv; + pSetMultiVSConstant3fv = SetMultiCGVSConstant3fv; + pCompileVertexShader = CompileCGVertexShader; + glEnable(GL_VERTEX_PROGRAM_ARB); + } - glGetProgramivARB(GL_VERTEX_PROGRAM_ARB, GL_MAX_PROGRAM_NATIVE_INSTRUCTIONS_ARB, (GLint *)&s_nMaxVertexInstructions); - if (strstr((const char*)glGetString(GL_VENDOR), "Humper") != NULL) s_nMaxVertexInstructions = 4096; + glGetProgramivARB(GL_VERTEX_PROGRAM_ARB, GL_MAX_PROGRAM_NATIVE_INSTRUCTIONS_ARB, (GLint *)&s_nMaxVertexInstructions); + if (strstr((const char*)glGetString(GL_VENDOR), "Humper") != NULL) s_nMaxVertexInstructions = 4096; #if CG_VERSION_NUM == 2100 - if (strstr((const char*)glGetString(GL_VENDOR), "ATI") != NULL) - { - s_nMaxVertexInstructions = 4096; - } + if (strstr((const char*)glGetString(GL_VENDOR), "ATI") != NULL) + { + s_nMaxVertexInstructions = 4096; + } #endif } void VertexShaderCache::Shutdown() { - for (VSCache::iterator iter = vshaders.begin(); iter != vshaders.end(); ++iter) - iter->second.Destroy(); - vshaders.clear(); + for (VSCache::iterator iter = vshaders.begin(); iter != vshaders.end(); ++iter) + iter->second.Destroy(); + vshaders.clear(); } - VERTEXSHADER* VertexShaderCache::SetShader(u32 components) { - VERTEXSHADERUID uid; - GetVertexShaderId(&uid, components); - if (last_entry) - { - if (uid == last_uid) + VERTEXSHADERUID uid; + GetVertexShaderId(&uid, components); + if (last_entry) { - GFX_DEBUGGER_PAUSE_AT(NEXT_VERTEX_SHADER_CHANGE, true); - ValidateVertexShaderIDs(API_OPENGL, vshaders[uid].safe_uid, vshaders[uid].shader.strprog, components); - return &last_entry->shader; + if (uid == last_uid) + { + GFX_DEBUGGER_PAUSE_AT(NEXT_VERTEX_SHADER_CHANGE, true); + ValidateVertexShaderIDs(API_OPENGL, vshaders[uid].safe_uid, vshaders[uid].shader.strprog, components); + return &last_entry->shader; + } } - } - last_uid = uid; + last_uid = uid; - VSCache::iterator iter = vshaders.find(uid); - if (iter != vshaders.end()) - { - VSCacheEntry &entry = iter->second; + VSCache::iterator iter = vshaders.find(uid); + if (iter != vshaders.end()) + { + VSCacheEntry &entry = iter->second; + last_entry = &entry; + + GFX_DEBUGGER_PAUSE_AT(NEXT_VERTEX_SHADER_CHANGE, true); + ValidateVertexShaderIDs(API_OPENGL, entry.safe_uid, entry.shader.strprog, components); + return &last_entry->shader; + } + + // Make an entry in the table + VSCacheEntry& entry = vshaders[uid]; last_entry = &entry; - - GFX_DEBUGGER_PAUSE_AT(NEXT_VERTEX_SHADER_CHANGE, true); - ValidateVertexShaderIDs(API_OPENGL, entry.safe_uid, entry.shader.strprog, components); - return &last_entry->shader; - } - - // Make an entry in the table - VSCacheEntry& entry = vshaders[uid]; - last_entry = &entry; - const char *code = GenerateVertexShaderCode(components, g_ActiveConfig.bUseGLSL ? API_GLSL : API_OPENGL); - GetSafeVertexShaderId(&entry.safe_uid, components); + const char *code = GenerateVertexShaderCode(components, g_ActiveConfig.bUseGLSL ? API_GLSL : API_OPENGL); + GetSafeVertexShaderId(&entry.safe_uid, components); #if defined(_DEBUG) || defined(DEBUGFAST) - if (g_ActiveConfig.iLog & CONF_SAVESHADERS && code) { - static int counter = 0; - char szTemp[MAX_PATH]; - sprintf(szTemp, "%svs_%04i.txt", File::GetUserPath(D_DUMP_IDX).c_str(), counter++); + if (g_ActiveConfig.iLog & CONF_SAVESHADERS && code) { + static int counter = 0; + char szTemp[MAX_PATH]; + sprintf(szTemp, "%svs_%04i.txt", File::GetUserPath(D_DUMP_IDX).c_str(), counter++); - SaveData(szTemp, code); - } + SaveData(szTemp, code); + } #endif - if (!code || !VertexShaderCache::CompileVertexShader(entry.shader, code)) { - GFX_DEBUGGER_PAUSE_AT(NEXT_ERROR, true); - return NULL; - } + if (!code || !VertexShaderCache::CompileVertexShader(entry.shader, code)) { + GFX_DEBUGGER_PAUSE_AT(NEXT_ERROR, true); + return NULL; + } - INCSTAT(stats.numVertexShadersCreated); - SETSTAT(stats.numVertexShadersAlive, vshaders.size()); - GFX_DEBUGGER_PAUSE_AT(NEXT_VERTEX_SHADER_CHANGE, true); - return &last_entry->shader; + INCSTAT(stats.numVertexShadersCreated); + SETSTAT(stats.numVertexShadersAlive, vshaders.size()); + GFX_DEBUGGER_PAUSE_AT(NEXT_VERTEX_SHADER_CHANGE, true); + return &last_entry->shader; } bool VertexShaderCache::CompileVertexShader(VERTEXSHADER& vs, const char* pstrprogram) @@ -156,32 +154,28 @@ bool VertexShaderCache::CompileVertexShader(VERTEXSHADER& vs, const char* pstrpr void VertexShaderCache::DisableShader() { - if(g_ActiveConfig.bUseGLSL) - assert(true); - if (ShaderEnabled) - { - glDisable(GL_VERTEX_PROGRAM_ARB); - ShaderEnabled = false; - } + if (ShaderEnabled) + { + glDisable(GL_VERTEX_PROGRAM_ARB); + ShaderEnabled = false; + } } - void VertexShaderCache::SetCurrentShader(GLuint Shader) { - if(g_ActiveConfig.bUseGLSL) - assert(true); - if (!ShaderEnabled) - { - glEnable(GL_VERTEX_PROGRAM_ARB); - ShaderEnabled= true; - } - if (CurrentShader != Shader) - { - if(Shader != 0) - CurrentShader = Shader; - glBindProgramARB(GL_VERTEX_PROGRAM_ARB, CurrentShader); - } + if (!ShaderEnabled) + { + glEnable(GL_VERTEX_PROGRAM_ARB); + ShaderEnabled= true; + } + if (CurrentShader != Shader) + { + if (Shader != 0) + CurrentShader = Shader; + glBindProgramARB(GL_VERTEX_PROGRAM_ARB, CurrentShader); + } } + // GLSL Specific bool CompileGLSLVertexShader(VERTEXSHADER& vs, const char* pstrprogram) { @@ -205,12 +199,12 @@ bool CompileGLSLVertexShader(VERTEXSHADER& vs, const char* pstrprogram) GLchar* infoLog = new GLchar[length]; glGetShaderInfoLog(result, length, &charsWritten, infoLog); WARN_LOG(VIDEO, "VS Shader info log:\n%s", infoLog); - char szTemp[MAX_PATH]; - sprintf(szTemp, "vs_%d.txt", result); - FILE *fp = fopen(szTemp, "wb"); - fwrite(pstrprogram, strlen(pstrprogram), 1, fp); - fclose(fp); - + char szTemp[MAX_PATH]; + sprintf(szTemp, "vs_%d.txt", result); + FILE *fp = fopen(szTemp, "wb"); + fwrite(pstrprogram, strlen(pstrprogram), 1, fp); + fclose(fp); + delete[] infoLog; } // Don't try to use this shader @@ -223,39 +217,43 @@ bool CompileGLSLVertexShader(VERTEXSHADER& vs, const char* pstrprogram) vs.bGLSL = true; return true; } + void SetVSConstant4fvByName(const char * name, unsigned int offset, const float *f, const unsigned int count = 1) { - PROGRAMSHADER tmp = ProgramShaderCache::GetShaderProgram(); - for(int a = 0; a < NUM_UNIFORMS; ++a) - if(!strcmp(name, UniformNames[a])) + PROGRAMSHADER tmp = ProgramShaderCache::GetShaderProgram(); + for (int a = 0; a < NUM_UNIFORMS; ++a) { - if(tmp.UniformLocations[a] == -1) - return; - else - { - glUniform4fv(tmp.UniformLocations[a] + offset, count, f); - return; - } + if (!strcmp(name, UniformNames[a])) + { + if (tmp.UniformLocations[a] == -1) + return; + else + { + glUniform4fv(tmp.UniformLocations[a] + offset, count, f); + return; + } + } } } + void SetGLSLVSConstant4f(unsigned int const_number, float f1, float f2, float f3, float f4) { - float const buf[4] = {f1, f2, f3, f4}; - + float const buf[4] = {f1, f2, f3, f4}; + if (g_ActiveConfig.backend_info.bSupportsGLSLUBO) { ProgramShaderCache::SetMultiVSConstant4fv(const_number, buf, 1); return; } - for( unsigned int a = 0; a < 9; ++a) - { - if( const_number >= VSVar_Loc[a].reg && const_number < ( VSVar_Loc[a].reg + VSVar_Loc[a].size)) + for (unsigned int a = 0; a < 9; ++a) { - unsigned int offset = const_number - VSVar_Loc[a].reg; - SetVSConstant4fvByName(VSVar_Loc[a].name, offset, buf); - return; + if (const_number >= VSVar_Loc[a].reg && const_number < ( VSVar_Loc[a].reg + VSVar_Loc[a].size)) + { + unsigned int offset = const_number - VSVar_Loc[a].reg; + SetVSConstant4fvByName(VSVar_Loc[a].name, offset, buf); + return; + } } - } } void SetGLSLVSConstant4fv(unsigned int const_number, const float *f) @@ -265,15 +263,15 @@ void SetGLSLVSConstant4fv(unsigned int const_number, const float *f) ProgramShaderCache::SetMultiVSConstant4fv(const_number, f, 1); return; } - for( unsigned int a = 0; a < 9; ++a) - { - if( const_number >= VSVar_Loc[a].reg && const_number < ( VSVar_Loc[a].reg + VSVar_Loc[a].size)) + for (unsigned int a = 0; a < 9; ++a) { - unsigned int offset = const_number - VSVar_Loc[a].reg; - SetVSConstant4fvByName(VSVar_Loc[a].name, offset, f); - return; + if (const_number >= VSVar_Loc[a].reg && const_number < ( VSVar_Loc[a].reg + VSVar_Loc[a].size)) + { + unsigned int offset = const_number - VSVar_Loc[a].reg; + SetVSConstant4fvByName(VSVar_Loc[a].name, offset, f); + return; + } } - } } void SetMultiGLSLVSConstant4fv(unsigned int const_number, unsigned int count, const float *f) @@ -283,157 +281,157 @@ void SetMultiGLSLVSConstant4fv(unsigned int const_number, unsigned int count, co ProgramShaderCache::SetMultiVSConstant4fv(const_number, f, count); return; } - for( unsigned int a = 0; a < 9; ++a) - { - if( const_number >= VSVar_Loc[a].reg && const_number < ( VSVar_Loc[a].reg + VSVar_Loc[a].size)) + for (unsigned int a = 0; a < 9; ++a) { - unsigned int offset = const_number - VSVar_Loc[a].reg; - SetVSConstant4fvByName(VSVar_Loc[a].name, offset, f, count); - return; + if (const_number >= VSVar_Loc[a].reg && const_number < ( VSVar_Loc[a].reg + VSVar_Loc[a].size)) + { + unsigned int offset = const_number - VSVar_Loc[a].reg; + SetVSConstant4fvByName(VSVar_Loc[a].name, offset, f, count); + return; + } } - } } void SetMultiGLSLVSConstant3fv(unsigned int const_number, unsigned int count, const float *f) { - float buf[4 * C_VENVCONST_END]; - for (unsigned int i = 0; i < count; i++) - { - buf[4*i ] = *f++; - buf[4*i+1] = *f++; - buf[4*i+2] = *f++; - buf[4*i+3] = 0.f; - } + float buf[4 * C_VENVCONST_END]; + for (unsigned int i = 0; i < count; i++) + { + buf[4*i ] = *f++; + buf[4*i+1] = *f++; + buf[4*i+2] = *f++; + buf[4*i+3] = 0.f; + } if (g_ActiveConfig.backend_info.bSupportsGLSLUBO) { ProgramShaderCache::SetMultiVSConstant4fv(const_number, buf, count); return; } - for( unsigned int a = 0; a < 9; ++a) - { - if( const_number >= VSVar_Loc[a].reg && const_number < ( VSVar_Loc[a].reg + VSVar_Loc[a].size)) + for (unsigned int a = 0; a < 9; ++a) { - unsigned int offset = const_number - VSVar_Loc[a].reg; - SetVSConstant4fvByName(VSVar_Loc[a].name, offset, buf, count); - return; + if (const_number >= VSVar_Loc[a].reg && const_number < ( VSVar_Loc[a].reg + VSVar_Loc[a].size)) + { + unsigned int offset = const_number - VSVar_Loc[a].reg; + SetVSConstant4fvByName(VSVar_Loc[a].name, offset, buf, count); + return; + } } - } } // CG Specific bool CompileCGVertexShader(VERTEXSHADER& vs, const char* pstrprogram) { - // Reset GL error before compiling shaders. Yeah, we need to investigate the causes of these. - GLenum err = GL_REPORT_ERROR(); - if (err != GL_NO_ERROR) - { - ERROR_LOG(VIDEO, "glError %08x before VS!", err); - } + // Reset GL error before compiling shaders. Yeah, we need to investigate the causes of these. + GLenum err = GL_REPORT_ERROR(); + if (err != GL_NO_ERROR) + { + ERROR_LOG(VIDEO, "glError %08x before VS!", err); + } #if defined HAVE_CG && HAVE_CG - CGprogram tempprog = cgCreateProgram(g_cgcontext, CG_SOURCE, pstrprogram, g_cgvProf, "main", NULL); - if (!cgIsProgram(tempprog)) { - static int num_failures = 0; - char szTemp[MAX_PATH]; - sprintf(szTemp, "bad_vs_%04i.txt", num_failures++); - std::ofstream file(szTemp); - file << pstrprogram; - file.close(); + CGprogram tempprog = cgCreateProgram(g_cgcontext, CG_SOURCE, pstrprogram, g_cgvProf, "main", NULL); + if (!cgIsProgram(tempprog)) { + static int num_failures = 0; + char szTemp[MAX_PATH]; + sprintf(szTemp, "bad_vs_%04i.txt", num_failures++); + std::ofstream file(szTemp); + file << pstrprogram; + file.close(); - PanicAlert("Failed to compile vertex shader %d!\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 (%d):\n%s", - num_failures - 1, szTemp, - g_cgfProf, - cgGetLastListing(g_cgcontext)); + PanicAlert("Failed to compile vertex shader %d!\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 (%d):\n%s", + num_failures - 1, szTemp, + g_cgfProf, + cgGetLastListing(g_cgcontext)); + + cgDestroyProgram(tempprog); + ERROR_LOG(VIDEO, "Failed to load vs %s:", cgGetLastListing(g_cgcontext)); + ERROR_LOG(VIDEO, "%s", pstrprogram); + return false; + } + + if (cgGetError() != CG_NO_ERROR) + { + WARN_LOG(VIDEO, "Failed to load vs %s:", cgGetLastListing(g_cgcontext)); + WARN_LOG(VIDEO, "%s", pstrprogram); + } + + // This looks evil - we modify the program through the const char * we got from cgGetProgramString! + // It SHOULD not have any nasty side effects though - but you never know... + char *pcompiledprog = (char*)cgGetProgramString(tempprog, CG_COMPILED_PROGRAM); + char *plocal = strstr(pcompiledprog, "program.local"); + while (plocal != NULL) { + const char* penv = " program.env"; + memcpy(plocal, penv, 13); + plocal = strstr(plocal + 13, "program.local"); + } + glGenProgramsARB(1, &vs.glprogid); + vs.bGLSL = false; + VertexShaderCache::SetCurrentShader(vs.glprogid); + + glProgramStringARB(GL_VERTEX_PROGRAM_ARB, GL_PROGRAM_FORMAT_ASCII_ARB, (GLsizei)strlen(pcompiledprog), pcompiledprog); + err = GL_REPORT_ERROR(); + if (err != GL_NO_ERROR) { + ERROR_LOG(VIDEO, "%s", pstrprogram); + ERROR_LOG(VIDEO, "%s", pcompiledprog); + } cgDestroyProgram(tempprog); - ERROR_LOG(VIDEO, "Failed to load vs %s:", cgGetLastListing(g_cgcontext)); - ERROR_LOG(VIDEO, "%s", pstrprogram); - return false; - } - - if (cgGetError() != CG_NO_ERROR) - { - WARN_LOG(VIDEO, "Failed to load vs %s:", cgGetLastListing(g_cgcontext)); - WARN_LOG(VIDEO, "%s", pstrprogram); - } - - // This looks evil - we modify the program through the const char * we got from cgGetProgramString! - // It SHOULD not have any nasty side effects though - but you never know... - char *pcompiledprog = (char*)cgGetProgramString(tempprog, CG_COMPILED_PROGRAM); - char *plocal = strstr(pcompiledprog, "program.local"); - while (plocal != NULL) { - const char* penv = " program.env"; - memcpy(plocal, penv, 13); - plocal = strstr(plocal + 13, "program.local"); - } - glGenProgramsARB(1, &vs.glprogid); - vs.bGLSL = false; - VertexShaderCache::SetCurrentShader(vs.glprogid); - - glProgramStringARB(GL_VERTEX_PROGRAM_ARB, GL_PROGRAM_FORMAT_ASCII_ARB, (GLsizei)strlen(pcompiledprog), pcompiledprog); - err = GL_REPORT_ERROR(); - if (err != GL_NO_ERROR) { - ERROR_LOG(VIDEO, "%s", pstrprogram); - ERROR_LOG(VIDEO, "%s", pcompiledprog); - } - - cgDestroyProgram(tempprog); #endif - if (g_ActiveConfig.bEnableShaderDebugging) - vs.strprog = pstrprogram; + if (g_ActiveConfig.bEnableShaderDebugging) + vs.strprog = pstrprogram; - return true; + return true; } void SetCGVSConstant4f(unsigned int const_number, float f1, float f2, float f3, float f4) { - glProgramEnvParameter4fARB(GL_VERTEX_PROGRAM_ARB, const_number, f1, f2, f3, f4); + glProgramEnvParameter4fARB(GL_VERTEX_PROGRAM_ARB, const_number, f1, f2, f3, f4); } void SetCGVSConstant4fv(unsigned int const_number, const float *f) { - glProgramEnvParameter4fvARB(GL_VERTEX_PROGRAM_ARB, const_number, f); + glProgramEnvParameter4fvARB(GL_VERTEX_PROGRAM_ARB, const_number, f); } void SetMultiCGVSConstant4fv(unsigned int const_number, unsigned int count, const float *f) { - if(GLEW_EXT_gpu_program_parameters) - { - glProgramEnvParameters4fvEXT(GL_VERTEX_PROGRAM_ARB, const_number, count, f); - } - else - { - for (unsigned int i = 0; i < count; i++,f+=4) - glProgramEnvParameter4fvARB(GL_VERTEX_PROGRAM_ARB, const_number + i, f); - } + if (GLEW_EXT_gpu_program_parameters) + { + glProgramEnvParameters4fvEXT(GL_VERTEX_PROGRAM_ARB, const_number, count, f); + } + else + { + for (unsigned int i = 0; i < count; i++,f+=4) + glProgramEnvParameter4fvARB(GL_VERTEX_PROGRAM_ARB, const_number + i, f); + } } void SetMultiCGVSConstant3fv(unsigned int const_number, unsigned int count, const float *f) { - if(GLEW_EXT_gpu_program_parameters) - { - float buf[4 * C_VENVCONST_END]; - for (unsigned int i = 0; i < count; i++) + if (GLEW_EXT_gpu_program_parameters) { - buf[4*i ] = *f++; - buf[4*i+1] = *f++; - buf[4*i+2] = *f++; - buf[4*i+3] = 0.f; + float buf[4 * C_VENVCONST_END]; + for (unsigned int i = 0; i < count; i++) + { + buf[4*i ] = *f++; + buf[4*i+1] = *f++; + buf[4*i+2] = *f++; + buf[4*i+3] = 0.f; + } + glProgramEnvParameters4fvEXT(GL_VERTEX_PROGRAM_ARB, const_number, count, buf); } - glProgramEnvParameters4fvEXT(GL_VERTEX_PROGRAM_ARB, const_number, count, buf); - } - else - { - for (unsigned int i = 0; i < count; i++) + else { - float buf[4]; - buf[0] = *f++; - buf[1] = *f++; - buf[2] = *f++; - buf[3] = 0.f; - glProgramEnvParameter4fvARB(GL_VERTEX_PROGRAM_ARB, const_number + i, buf); + for (unsigned int i = 0; i < count; i++) + { + float buf[4]; + buf[0] = *f++; + buf[1] = *f++; + buf[2] = *f++; + buf[3] = 0.f; + glProgramEnvParameter4fvARB(GL_VERTEX_PROGRAM_ARB, const_number + i, buf); + } } - } } // Renderer Functions @@ -457,5 +455,4 @@ void Renderer::SetMultiVSConstant3fv(unsigned int const_number, unsigned int cou pSetMultiVSConstant3fv(const_number, count, f); } - } // namespace OGL diff --git a/Source/Plugins/Plugin_VideoOGL/Src/VertexShaderCache.h b/Source/Plugins/Plugin_VideoOGL/Src/VertexShaderCache.h index c3a6146b53..8922311613 100644 --- a/Source/Plugins/Plugin_VideoOGL/Src/VertexShaderCache.h +++ b/Source/Plugins/Plugin_VideoOGL/Src/VertexShaderCache.h @@ -32,7 +32,7 @@ struct VERTEXSHADER VERTEXSHADER() : glprogid(0), bGLSL(0) {} void Destroy() { - if(bGLSL) + if (bGLSL) glDeleteShader(glprogid); else glDeleteProgramsARB(1, &glprogid); From 16f9d8e06dfd872141a055dd422a1666da34cd2a Mon Sep 17 00:00:00 2001 From: Shawn Hoffman Date: Mon, 26 Dec 2011 00:43:47 -0500 Subject: [PATCH 70/83] Checking GLEW_VERSION_4_0 is superfluous since we check GL_ARB_get_program_binary, and it's a runtime variable anyways. --- .../Plugins/Plugin_VideoOGL/Src/ProgramShaderCache.cpp | 4 ---- .../Plugins/Plugin_VideoOGL/Src/ProgramShaderCache.h | 10 ---------- 2 files changed, 14 deletions(-) diff --git a/Source/Plugins/Plugin_VideoOGL/Src/ProgramShaderCache.cpp b/Source/Plugins/Plugin_VideoOGL/Src/ProgramShaderCache.cpp index 008a4a679e..d25830566e 100644 --- a/Source/Plugins/Plugin_VideoOGL/Src/ProgramShaderCache.cpp +++ b/Source/Plugins/Plugin_VideoOGL/Src/ProgramShaderCache.cpp @@ -144,10 +144,8 @@ void ProgramShaderCache::SetBothShaders(GLuint PS, GLuint VS) glAttachShader(entry.program.glprogid, entry.program.psid); -#ifdef GLEW_VERSION_4_0 if (g_ActiveConfig.backend_info.bSupportsGLSLCache) glProgramParameteri(entry.program.glprogid, GL_PROGRAM_BINARY_RETRIEVABLE_HINT, GL_TRUE); -#endif glLinkProgram(entry.program.glprogid); @@ -210,7 +208,6 @@ void ProgramShaderCache::Init(void) glBindBufferRange(GL_UNIFORM_BUFFER, 2, s_ps_vs_ubo, s_vs_data_offset, vs_data_size); } -#ifdef GLEW_VERSION_4_0 // Read our shader cache, only if supported if (g_ActiveConfig.backend_info.bSupportsGLSLCache) { @@ -229,7 +226,6 @@ void ProgramShaderCache::Init(void) ProgramFormat = (GLenum)Formats[0]; delete[] Formats; } -#endif } void ProgramShaderCache::Shutdown(void) diff --git a/Source/Plugins/Plugin_VideoOGL/Src/ProgramShaderCache.h b/Source/Plugins/Plugin_VideoOGL/Src/ProgramShaderCache.h index e34ca1c086..34930ec027 100644 --- a/Source/Plugins/Plugin_VideoOGL/Src/ProgramShaderCache.h +++ b/Source/Plugins/Plugin_VideoOGL/Src/ProgramShaderCache.h @@ -85,25 +85,17 @@ struct PROGRAMSHADER // TODO at first glance looks bad - malloc/no free/pointer not saved in instance... u8 *Data() { -#ifdef GLEW_VERSION_4_0 glGetProgramiv(glprogid, GL_PROGRAM_BINARY_LENGTH, &binaryLength); u8* binary = (u8*)malloc(binaryLength); glGetProgramBinary(glprogid, binaryLength, NULL, &ProgramFormat, binary); return binary; -#else - return NULL; -#endif } GLint Size() { -#ifdef GLEW_VERSION_4_0 if (!binaryLength) glGetProgramiv(glprogid, GL_PROGRAM_BINARY_LENGTH, &binaryLength); return binaryLength; -#else - return 0; -#endif } }; @@ -137,7 +129,6 @@ class ProgramShaderCache public: void Read(const PROGRAMUID &key, const u8 *value, u32 value_size) { -#ifdef GLEW_VERSION_4_0 PCacheEntry entry; // The two shaders might not even exist anymore @@ -158,7 +149,6 @@ class ProgramShaderCache glUseProgram(entry.program.glprogid); SetProgramVariables(entry, key); } -#endif } }; From 5f296d0be7ef76f28bf6b0025c19c5a553177635 Mon Sep 17 00:00:00 2001 From: Shawn Hoffman Date: Mon, 26 Dec 2011 02:58:52 -0500 Subject: [PATCH 71/83] refactor ProgramShaderCache::PCacheEntry --- .../Plugin_VideoOGL/Src/PixelShaderCache.cpp | 2 +- .../Src/ProgramShaderCache.cpp | 81 +++---- .../Plugin_VideoOGL/Src/ProgramShaderCache.h | 209 ++++++++---------- .../Plugin_VideoOGL/Src/VertexShaderCache.cpp | 2 +- 4 files changed, 127 insertions(+), 167 deletions(-) diff --git a/Source/Plugins/Plugin_VideoOGL/Src/PixelShaderCache.cpp b/Source/Plugins/Plugin_VideoOGL/Src/PixelShaderCache.cpp index 345aa79115..026d3ec037 100644 --- a/Source/Plugins/Plugin_VideoOGL/Src/PixelShaderCache.cpp +++ b/Source/Plugins/Plugin_VideoOGL/Src/PixelShaderCache.cpp @@ -422,7 +422,7 @@ bool CompileGLSLPixelShader(FRAGMENTSHADER& ps, const char* pstrprogram) void SetPSConstant4fvByName(const char * name, unsigned int offset, const float *f, const unsigned int count = 1) { - PROGRAMSHADER tmp = ProgramShaderCache::GetShaderProgram(); + ProgramShaderCache::PCacheEntry tmp = ProgramShaderCache::GetShaderProgram(); for (int a = 0; a < NUM_UNIFORMS; ++a) { if (!strcmp(name, UniformNames[a])) diff --git a/Source/Plugins/Plugin_VideoOGL/Src/ProgramShaderCache.cpp b/Source/Plugins/Plugin_VideoOGL/Src/ProgramShaderCache.cpp index d25830566e..387996a957 100644 --- a/Source/Plugins/Plugin_VideoOGL/Src/ProgramShaderCache.cpp +++ b/Source/Plugins/Plugin_VideoOGL/Src/ProgramShaderCache.cpp @@ -26,7 +26,7 @@ ProgramShaderCache::PCache ProgramShaderCache::pshaders; GLuint ProgramShaderCache::s_ps_vs_ubo; GLintptr ProgramShaderCache::s_vs_data_offset; -LinearDiskCache g_program_disk_cache; +LinearDiskCache g_program_disk_cache; GLenum ProgramFormat; std::pair ProgramShaderCache::CurrentShaderProgram; @@ -57,7 +57,7 @@ const char *UniformNames[NUM_UNIFORMS] = I_DEPTHPARAMS, }; -void ProgramShaderCache::SetProgramVariables(PCacheEntry &entry, const PROGRAMUID &uid) +void ProgramShaderCache::SetProgramVariables(PCacheEntry &entry) { // Dunno why this is needed when I have the binding // points statically set in the shader source @@ -65,10 +65,10 @@ void ProgramShaderCache::SetProgramVariables(PCacheEntry &entry, const PROGRAMUI // Driver Bug? Nvidia GTX 570, 290.xx Driver, Linux x64 if (g_ActiveConfig.backend_info.bSupportsGLSLUBO) { - glUniformBlockBinding(entry.program.glprogid, 0, 1); + glUniformBlockBinding(entry.prog_id, 0, 1); // Some things have no vertex shader - if (uid.uid.vsid != 0) - glUniformBlockBinding(entry.program.glprogid, 1, 2); + if (entry.vsid != 0) + glUniformBlockBinding(entry.prog_id, 1, 2); } // We cache our uniform locations for now @@ -76,42 +76,39 @@ void ProgramShaderCache::SetProgramVariables(PCacheEntry &entry, const PROGRAMUI // We can remove this // (Sonicadvance): For some reason this fails on my hardware - //glGetUniformIndices(entry.program.glprogid, NUM_UNIFORMS, UniformNames, entry.program.UniformLocations); + //glGetUniformIndices(entry.prog_id, NUM_UNIFORMS, UniformNames, entry.UniformLocations); // Got to do it this crappy way. if (!g_ActiveConfig.backend_info.bSupportsGLSLUBO) for (int a = 8; a < NUM_UNIFORMS; ++a) - entry.program.UniformLocations[a] = glGetUniformLocation(entry.program.glprogid, UniformNames[a]); + entry.UniformLocations[a] = glGetUniformLocation(entry.prog_id, UniformNames[a]); if (!g_ActiveConfig.backend_info.bSupportsGLSLBinding) { for (int a = 0; a < 8; ++a) { // Still need to get sampler locations since we aren't binding them statically in the shaders - entry.program.UniformLocations[a] = glGetUniformLocation(entry.program.glprogid, UniformNames[a]); - if (entry.program.UniformLocations[a] != -1) - glUniform1i(entry.program.UniformLocations[a], a); + entry.UniformLocations[a] = glGetUniformLocation(entry.prog_id, UniformNames[a]); + if (entry.UniformLocations[a] != -1) + glUniform1i(entry.UniformLocations[a], a); } } // Need to get some attribute locations - if (uid.uid.vsid != 0 && !g_ActiveConfig.backend_info.bSupportsGLSLATTRBind) + if (entry.vsid != 0 && !g_ActiveConfig.backend_info.bSupportsGLSLATTRBind) { // We have no vertex Shader - glBindAttribLocation(entry.program.glprogid, SHADER_NORM1_ATTRIB, "rawnorm1"); - glBindAttribLocation(entry.program.glprogid, SHADER_NORM2_ATTRIB, "rawnorm2"); - glBindAttribLocation(entry.program.glprogid, SHADER_POSMTX_ATTRIB, "fposmtx"); + glBindAttribLocation(entry.prog_id, SHADER_NORM1_ATTRIB, "rawnorm1"); + glBindAttribLocation(entry.prog_id, SHADER_NORM2_ATTRIB, "rawnorm2"); + glBindAttribLocation(entry.prog_id, SHADER_POSMTX_ATTRIB, "fposmtx"); } } void ProgramShaderCache::SetBothShaders(GLuint PS, GLuint VS) { - PROGRAMUID uid; CurrentFShader = PS; CurrentVShader = VS; - GetProgramShaderId(&uid, CurrentVShader, CurrentFShader); - - if (uid.uid.id == 0) + if (CurrentFShader == 0 && CurrentVShader == 0) { CurrentProgram = 0; glUseProgram(0); @@ -120,42 +117,39 @@ void ProgramShaderCache::SetBothShaders(GLuint PS, GLuint VS) // Fragment shaders can survive without Vertex Shaders // We have a valid fragment shader, let's create our program - std::pair ShaderPair = std::make_pair(uid.uid.psid, uid.uid.vsid); + std::pair ShaderPair = std::make_pair(CurrentFShader, CurrentVShader); PCache::iterator iter = pshaders.find(ShaderPair); if (iter != pshaders.end()) { PCacheEntry &entry = iter->second; - glUseProgram(entry.program.glprogid); + glUseProgram(entry.prog_id); CurrentShaderProgram = ShaderPair; - CurrentProgram = entry.program.glprogid; + CurrentProgram = entry.prog_id; return; } PCacheEntry entry; - entry.program.vsid = CurrentVShader; - entry.program.psid = CurrentFShader; - entry.program.uid = uid; - entry.program.glprogid = glCreateProgram(); + entry.Create(CurrentFShader, CurrentVShader); // Right, the program is created now // Let's attach everything - if (entry.program.vsid != 0) // attaching zero vertex shader makes it freak out - glAttachShader(entry.program.glprogid, entry.program.vsid); + if (entry.vsid != 0) // attaching zero vertex shader makes it freak out + glAttachShader(entry.prog_id, entry.vsid); - glAttachShader(entry.program.glprogid, entry.program.psid); + glAttachShader(entry.prog_id, entry.psid); if (g_ActiveConfig.backend_info.bSupportsGLSLCache) - glProgramParameteri(entry.program.glprogid, GL_PROGRAM_BINARY_RETRIEVABLE_HINT, GL_TRUE); + glProgramParameteri(entry.prog_id, GL_PROGRAM_BINARY_RETRIEVABLE_HINT, GL_TRUE); - glLinkProgram(entry.program.glprogid); + glLinkProgram(entry.prog_id); - glUseProgram(entry.program.glprogid); + glUseProgram(entry.prog_id); - SetProgramVariables(entry, uid); + SetProgramVariables(entry); pshaders[ShaderPair] = entry; CurrentShaderProgram = ShaderPair; - CurrentProgram = entry.program.glprogid; + CurrentProgram = entry.prog_id; } void ProgramShaderCache::SetMultiPSConstant4fv(unsigned int offset, const float *f, unsigned int count) @@ -175,9 +169,9 @@ GLuint ProgramShaderCache::GetCurrentProgram(void) return CurrentProgram; } -PROGRAMSHADER ProgramShaderCache::GetShaderProgram(void) +ProgramShaderCache::PCacheEntry ProgramShaderCache::GetShaderProgram(void) { - return pshaders[CurrentShaderProgram].program; + return pshaders[CurrentShaderProgram]; } void ProgramShaderCache::Init(void) @@ -216,15 +210,6 @@ void ProgramShaderCache::Init(void) SConfig::GetInstance().m_LocalCoreStartupParameter.m_strUniqueID.c_str()); ProgramShaderCacheInserter inserter; g_program_disk_cache.OpenAndRead(cache_filename, inserter); - - GLint Supported; - glGetIntegerv(GL_NUM_PROGRAM_BINARY_FORMATS, &Supported); - - GLint *Formats = new GLint[Supported]; - glGetIntegerv(GL_PROGRAM_BINARY_FORMATS, Formats); - // We don't really care about format - ProgramFormat = (GLenum)Formats[0]; - delete[] Formats; } } @@ -234,7 +219,7 @@ void ProgramShaderCache::Shutdown(void) { PCache::iterator iter = pshaders.begin(); for (; iter != pshaders.end(); ++iter) - g_program_disk_cache.Append(iter->second.program.uid, iter->second.program.Data(), iter->second.program.Size()); + g_program_disk_cache.Append(iter->second.uid, iter->second.GetProgram(), iter->second.Size()); g_program_disk_cache.Sync(); g_program_disk_cache.Close(); @@ -254,9 +239,3 @@ void ProgramShaderCache::Shutdown(void) } } // namespace OGL - -void GetProgramShaderId(PROGRAMUID *uid, GLuint _v, GLuint _p) -{ - uid->uid.vsid = _v; - uid->uid.psid = _p; -} diff --git a/Source/Plugins/Plugin_VideoOGL/Src/ProgramShaderCache.h b/Source/Plugins/Plugin_VideoOGL/Src/ProgramShaderCache.h index 34930ec027..d10cb202a0 100644 --- a/Source/Plugins/Plugin_VideoOGL/Src/ProgramShaderCache.h +++ b/Source/Plugins/Plugin_VideoOGL/Src/ProgramShaderCache.h @@ -27,143 +27,89 @@ #include "LinearDiskCache.h" #include "ConfigManager.h" -union PID -{ - struct - { - GLuint vsid, psid; - }; - u64 id; -}; - -class PROGRAMUID -{ -public: - PID uid; - - PROGRAMUID() - { - uid.id = 0; - } - - PROGRAMUID(const PROGRAMUID& r) - { - uid.id = r.uid.id; - } - - PROGRAMUID(GLuint _v, GLuint _p) - { - uid.vsid = _v; - uid.psid = _p; - } - - u64 GetNumValues() const - { - return uid.id; - } -}; - -void GetProgramShaderId(PROGRAMUID *uid, GLuint _v, GLuint _p); - namespace OGL { const int NUM_UNIFORMS = 27; extern const char *UniformNames[NUM_UNIFORMS]; -extern GLenum ProgramFormat; - -struct PROGRAMSHADER -{ - PROGRAMSHADER() : glprogid(0), vsid(0), psid(0), binaryLength(0) {} - // opengl program id - GLuint glprogid; - GLuint vsid, psid; - PROGRAMUID uid; - GLint UniformLocations[NUM_UNIFORMS]; - GLint binaryLength; - - // TODO at first glance looks bad - malloc/no free/pointer not saved in instance... - u8 *Data() - { - glGetProgramiv(glprogid, GL_PROGRAM_BINARY_LENGTH, &binaryLength); - u8* binary = (u8*)malloc(binaryLength); - glGetProgramBinary(glprogid, binaryLength, NULL, &ProgramFormat, binary); - return binary; - } - - GLint Size() - { - if (!binaryLength) - glGetProgramiv(glprogid, GL_PROGRAM_BINARY_LENGTH, &binaryLength); - return binaryLength; - } -}; class ProgramShaderCache { +public: + typedef std::pair ShaderUID; + struct PCacheEntry { - PROGRAMSHADER program; - int frameCount; - PCacheEntry() : frameCount(0) {} + GLuint prog_id; + GLenum prog_format; + u8 *binary; + GLint binary_size; + GLuint vsid, psid; + ShaderUID uid; + GLint UniformLocations[NUM_UNIFORMS]; + + PCacheEntry() : prog_id(0), vsid(0), psid(0), binary(NULL), binary_size(0) { } + + ~PCacheEntry() + { + FreeProgram(); + } + + void Create(const GLuint pix_id, const GLuint vert_id) + { + psid = pix_id; + vsid = vert_id; + uid = std::make_pair(psid, vsid); + prog_id = glCreateProgram(); + } void Destroy() { - glDeleteProgram(program.glprogid); - program.glprogid = 0; + glDeleteProgram(prog_id); + prog_id = 0; } - u8* Data() + void UpdateSize() { - return program.Data(); + if (binary_size == 0) + glGetProgramiv(prog_id, GL_PROGRAM_BINARY_LENGTH, &binary_size); + } + + // No idea how necessary this is + void SetProgramFormat() + { + GLint Supported; + glGetIntegerv(GL_NUM_PROGRAM_BINARY_FORMATS, &Supported); + + GLint *Formats = new GLint[Supported]; + glGetIntegerv(GL_PROGRAM_BINARY_FORMATS, Formats); + // We don't really care about format + prog_format = (GLenum)Formats[0]; + delete[] Formats; + } + + u8 *GetProgram() + { + UpdateSize(); + FreeProgram(); + binary = new u8[binary_size]; + glGetProgramBinary(prog_id, binary_size, NULL, &prog_format, binary); + return binary; + } + + void FreeProgram() + { + delete [] binary; } GLint Size() { - return program.Size(); + UpdateSize(); + return binary_size; } }; - class ProgramShaderCacheInserter : public LinearDiskCacheReader - { - public: - void Read(const PROGRAMUID &key, const u8 *value, u32 value_size) - { - PCacheEntry entry; - - // The two shaders might not even exist anymore - // But it is fine, no need to worry about that - entry.program.vsid = key.uid.vsid; - entry.program.psid = key.uid.psid; - - entry.program.glprogid = glCreateProgram(); - - glProgramBinary(entry.program.glprogid, ProgramFormat, value, value_size); - - GLint success; - glGetProgramiv(entry.program.glprogid, GL_LINK_STATUS, &success); - - if (success) - { - pshaders[std::make_pair(key.uid.psid, key.uid.vsid)] = entry; - glUseProgram(entry.program.glprogid); - SetProgramVariables(entry, key); - } - } - }; - - typedef std::map, PCacheEntry> PCache; - - static PCache pshaders; - static GLuint CurrentFShader, CurrentVShader, CurrentProgram; - static std::pair CurrentShaderProgram; - - static GLuint s_ps_vs_ubo; - static GLintptr s_vs_data_offset; - static void SetProgramVariables(PCacheEntry &entry, const PROGRAMUID &uid); - -public: - static PROGRAMSHADER GetShaderProgram(void); + static PCacheEntry GetShaderProgram(void); static void SetBothShaders(GLuint PS, GLuint VS); static GLuint GetCurrentProgram(void); @@ -172,6 +118,41 @@ public: static void Init(void); static void Shutdown(void); + +private: + class ProgramShaderCacheInserter : public LinearDiskCacheReader + { + public: + void Read(const ShaderUID &key, const u8 *value, u32 value_size) + { + // The two shaders might not even exist anymore + // But it is fine, no need to worry about that + PCacheEntry entry; + entry.Create(key.first, key.second); + + glProgramBinary(entry.prog_id, entry.prog_format, value, value_size); + + GLint success; + glGetProgramiv(entry.prog_id, GL_LINK_STATUS, &success); + + if (success) + { + pshaders[key] = entry; + glUseProgram(entry.prog_id); + SetProgramVariables(entry); + } + } + }; + + typedef std::map PCache; + + static PCache pshaders; + static GLuint CurrentFShader, CurrentVShader, CurrentProgram; + static ShaderUID CurrentShaderProgram; + + static GLuint s_ps_vs_ubo; + static GLintptr s_vs_data_offset; + static void SetProgramVariables(PCacheEntry &entry); }; } // namespace OGL diff --git a/Source/Plugins/Plugin_VideoOGL/Src/VertexShaderCache.cpp b/Source/Plugins/Plugin_VideoOGL/Src/VertexShaderCache.cpp index 59fcd6582e..f396e90078 100644 --- a/Source/Plugins/Plugin_VideoOGL/Src/VertexShaderCache.cpp +++ b/Source/Plugins/Plugin_VideoOGL/Src/VertexShaderCache.cpp @@ -220,7 +220,7 @@ bool CompileGLSLVertexShader(VERTEXSHADER& vs, const char* pstrprogram) void SetVSConstant4fvByName(const char * name, unsigned int offset, const float *f, const unsigned int count = 1) { - PROGRAMSHADER tmp = ProgramShaderCache::GetShaderProgram(); + ProgramShaderCache::PCacheEntry tmp = ProgramShaderCache::GetShaderProgram(); for (int a = 0; a < NUM_UNIFORMS; ++a) { if (!strcmp(name, UniformNames[a])) From 3773d29b6d2d4cd1f427bbbe8aac7d079b89e483 Mon Sep 17 00:00:00 2001 From: Ryan Houdek Date: Mon, 26 Dec 2011 03:54:44 -0600 Subject: [PATCH 72/83] Remove some warnings in ProgramShadercache, Was using wrong variable for checking dual source blending. --- Source/Plugins/Plugin_VideoOGL/Src/ProgramShaderCache.h | 2 +- Source/Plugins/Plugin_VideoOGL/Src/Render.cpp | 4 ++-- Source/Plugins/Plugin_VideoOGL/Src/VertexManager.cpp | 2 +- 3 files changed, 4 insertions(+), 4 deletions(-) diff --git a/Source/Plugins/Plugin_VideoOGL/Src/ProgramShaderCache.h b/Source/Plugins/Plugin_VideoOGL/Src/ProgramShaderCache.h index d10cb202a0..d0743326dd 100644 --- a/Source/Plugins/Plugin_VideoOGL/Src/ProgramShaderCache.h +++ b/Source/Plugins/Plugin_VideoOGL/Src/ProgramShaderCache.h @@ -48,7 +48,7 @@ public: ShaderUID uid; GLint UniformLocations[NUM_UNIFORMS]; - PCacheEntry() : prog_id(0), vsid(0), psid(0), binary(NULL), binary_size(0) { } + PCacheEntry() : prog_id(0), binary(NULL), binary_size(0), vsid(0), psid(0) { } ~PCacheEntry() { diff --git a/Source/Plugins/Plugin_VideoOGL/Src/Render.cpp b/Source/Plugins/Plugin_VideoOGL/Src/Render.cpp index a54a8a83b3..ca641ff480 100644 --- a/Source/Plugins/Plugin_VideoOGL/Src/Render.cpp +++ b/Source/Plugins/Plugin_VideoOGL/Src/Render.cpp @@ -960,8 +960,8 @@ void Renderer::SetBlendMode(bool forceUpdate) u32 changes = forceUpdate ? 0xFFFFFFFF : newval ^ s_blendMode; bool useDstAlpha = !g_ActiveConfig.bDstAlphaPass && bpmem.dstalpha.enable && bpmem.blendmode.alphaupdate - && bpmem.zcontrol.pixel_format == PIXELFMT_RGBA6_Z24 && g_ActiveConfig.bUseGLSL; - bool useDualSource = useDstAlpha; + && bpmem.zcontrol.pixel_format == PIXELFMT_RGBA6_Z24; + bool useDualSource = g_ActiveConfig.bUseGLSL && g_ActiveConfig.backend_info.bSupportsGLSLBind; if (changes & 1) // blend enable change diff --git a/Source/Plugins/Plugin_VideoOGL/Src/VertexManager.cpp b/Source/Plugins/Plugin_VideoOGL/Src/VertexManager.cpp index 3913785407..35e783337a 100644 --- a/Source/Plugins/Plugin_VideoOGL/Src/VertexManager.cpp +++ b/Source/Plugins/Plugin_VideoOGL/Src/VertexManager.cpp @@ -178,7 +178,7 @@ void VertexManager::vFlush() && bpmem.zcontrol.pixel_format == PIXELFMT_RGBA6_Z24; // Makes sure we can actually do Dual source blending - bool dualSourcePossible = g_ActiveConfig.bUseGLSL && g_ActiveConfig.backend_info.bSupportsGLSLATTRBind; + bool dualSourcePossible = g_ActiveConfig.bUseGLSL && g_ActiveConfig.backend_info.bSupportsGLSLBind; // finally bind FRAGMENTSHADER* ps; From 6f729f8cbfc704d562a737c85d2270bd1225ee2c Mon Sep 17 00:00:00 2001 From: Ryan Houdek Date: Mon, 26 Dec 2011 04:09:36 -0600 Subject: [PATCH 73/83] Woops, better not forget the ing --- Source/Plugins/Plugin_VideoOGL/Src/Render.cpp | 2 +- Source/Plugins/Plugin_VideoOGL/Src/VertexManager.cpp | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/Source/Plugins/Plugin_VideoOGL/Src/Render.cpp b/Source/Plugins/Plugin_VideoOGL/Src/Render.cpp index ca641ff480..6bf2a5cf65 100644 --- a/Source/Plugins/Plugin_VideoOGL/Src/Render.cpp +++ b/Source/Plugins/Plugin_VideoOGL/Src/Render.cpp @@ -961,7 +961,7 @@ void Renderer::SetBlendMode(bool forceUpdate) bool useDstAlpha = !g_ActiveConfig.bDstAlphaPass && bpmem.dstalpha.enable && bpmem.blendmode.alphaupdate && bpmem.zcontrol.pixel_format == PIXELFMT_RGBA6_Z24; - bool useDualSource = g_ActiveConfig.bUseGLSL && g_ActiveConfig.backend_info.bSupportsGLSLBind; + bool useDualSource = useDstAlpha && g_ActiveConfig.bUseGLSL && g_ActiveConfig.backend_info.bSupportsGLSLBinding; if (changes & 1) // blend enable change diff --git a/Source/Plugins/Plugin_VideoOGL/Src/VertexManager.cpp b/Source/Plugins/Plugin_VideoOGL/Src/VertexManager.cpp index 35e783337a..f931f50c78 100644 --- a/Source/Plugins/Plugin_VideoOGL/Src/VertexManager.cpp +++ b/Source/Plugins/Plugin_VideoOGL/Src/VertexManager.cpp @@ -178,7 +178,7 @@ void VertexManager::vFlush() && bpmem.zcontrol.pixel_format == PIXELFMT_RGBA6_Z24; // Makes sure we can actually do Dual source blending - bool dualSourcePossible = g_ActiveConfig.bUseGLSL && g_ActiveConfig.backend_info.bSupportsGLSLBind; + bool dualSourcePossible = g_ActiveConfig.bUseGLSL && g_ActiveConfig.backend_info.bSupportsGLSLBinding; // finally bind FRAGMENTSHADER* ps; From c8d0c8e2172900ee36c6cb25553bb07d982aaad7 Mon Sep 17 00:00:00 2001 From: Shawn Hoffman Date: Mon, 26 Dec 2011 10:27:18 -0500 Subject: [PATCH 74/83] oops, forgot to make sure the gl program is actually free'd. --- Source/Plugins/Plugin_VideoOGL/Src/ProgramShaderCache.cpp | 3 +++ Source/Plugins/Plugin_VideoOGL/Src/ProgramShaderCache.h | 1 + 2 files changed, 4 insertions(+) diff --git a/Source/Plugins/Plugin_VideoOGL/Src/ProgramShaderCache.cpp b/Source/Plugins/Plugin_VideoOGL/Src/ProgramShaderCache.cpp index 387996a957..eeb9614070 100644 --- a/Source/Plugins/Plugin_VideoOGL/Src/ProgramShaderCache.cpp +++ b/Source/Plugins/Plugin_VideoOGL/Src/ProgramShaderCache.cpp @@ -219,7 +219,10 @@ void ProgramShaderCache::Shutdown(void) { PCache::iterator iter = pshaders.begin(); for (; iter != pshaders.end(); ++iter) + { g_program_disk_cache.Append(iter->second.uid, iter->second.GetProgram(), iter->second.Size()); + iter->second.FreeProgram(); + } g_program_disk_cache.Sync(); g_program_disk_cache.Close(); diff --git a/Source/Plugins/Plugin_VideoOGL/Src/ProgramShaderCache.h b/Source/Plugins/Plugin_VideoOGL/Src/ProgramShaderCache.h index d0743326dd..6089f205e7 100644 --- a/Source/Plugins/Plugin_VideoOGL/Src/ProgramShaderCache.h +++ b/Source/Plugins/Plugin_VideoOGL/Src/ProgramShaderCache.h @@ -100,6 +100,7 @@ public: void FreeProgram() { delete [] binary; + binary = NULL; } GLint Size() From 88d20f1a2b681a9e0f4214496ef274a0375c3013 Mon Sep 17 00:00:00 2001 From: Ryan Houdek Date: Tue, 27 Dec 2011 12:35:35 -0600 Subject: [PATCH 75/83] Had a few vec4 and float4(0) calls floating around. --- Source/Core/VideoCommon/Src/PixelShaderGen.cpp | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/Source/Core/VideoCommon/Src/PixelShaderGen.cpp b/Source/Core/VideoCommon/Src/PixelShaderGen.cpp index 7eb3999fbc..277cd2e3e4 100644 --- a/Source/Core/VideoCommon/Src/PixelShaderGen.cpp +++ b/Source/Core/VideoCommon/Src/PixelShaderGen.cpp @@ -745,7 +745,7 @@ const char *GeneratePixelShaderCode(DSTALPHA_MODE dstAlphaMode, API_TYPE ApiType if (!Pretest) { // alpha test will always fail, so restart the shader and just make it an empty function - WRITE(p, "ocol0 = float4(0);\n"); + WRITE(p, "ocol0 = float4(0.0f,0.0f,0.0f,0.0f);\n"); WRITE(p, "discard;\n"); if(ApiType == API_GLSL && dstAlphaMode != DSTALPHA_DUAL_SOURCE_BLEND) WRITE(p, "gl_FragData[0] = ocol0;\n"); @@ -851,11 +851,11 @@ const char *GeneratePixelShaderCode(DSTALPHA_MODE dstAlphaMode, API_TYPE ApiType { // alpha test will always fail, so restart the shader and just make it an empty function p = pmainstart; - WRITE(p, "ocol0 = vec4(0.0f);\n"); + WRITE(p, "ocol0 = float4(0.0f,0.0f,0.0f,0.0f);\n"); if (DepthTextureEnable) WRITE(p, "depth = 1.f;\n"); if (dstAlphaMode == DSTALPHA_DUAL_SOURCE_BLEND) - WRITE(p, "ocol1 = vec4(0.0f);\n"); + WRITE(p, "ocol1 = float4(0.0f,0.0f,0.0f,0.0f);\n"); if (ApiType == API_GLSL) { // Once we switch to GLSL 1.3 and bind variables, we won't need to do this @@ -1320,8 +1320,8 @@ static bool WriteAlphaTest(char *&p, API_TYPE ApiType,DSTALPHA_MODE dstAlphaMode compindex = bpmem.alphaFunc.comp1 % 8; WRITE(p, tevAlphaFuncsTable[compindex],alphaRef[1]);//lookup the second component from the alpha function table - WRITE(p, ")){ocol0 = float4(0.0f);%s%s discard;%s}\n", - dstAlphaMode == DSTALPHA_DUAL_SOURCE_BLEND ? "ocol1 = vec4(0.0f);" : "", + WRITE(p, ")){ocol0 = float4(0.0f,0.0f,0.0f,0.0f);%s%s discard;%s}\n", + dstAlphaMode == DSTALPHA_DUAL_SOURCE_BLEND ? "ocol1 = float4(0.0f,0.0f,0.0f,0.0f);" : "", DepthTextureEnable ? "depth = 1.f;" : "", (ApiType != API_D3D11) ? "return;" : ""); return true; From fbbea9bb9b30b8957f2c9229660b6cbfc801933f Mon Sep 17 00:00:00 2001 From: Ryan Houdek Date: Thu, 29 Dec 2011 00:01:50 -0600 Subject: [PATCH 76/83] These checks aren't needed when using CG. --- Source/Plugins/Plugin_VideoOGL/Src/Render.cpp | 36 ++++++++++--------- 1 file changed, 19 insertions(+), 17 deletions(-) diff --git a/Source/Plugins/Plugin_VideoOGL/Src/Render.cpp b/Source/Plugins/Plugin_VideoOGL/Src/Render.cpp index 6bf2a5cf65..494744a447 100644 --- a/Source/Plugins/Plugin_VideoOGL/Src/Render.cpp +++ b/Source/Plugins/Plugin_VideoOGL/Src/Render.cpp @@ -311,22 +311,24 @@ Renderer::Renderer() s_bHaveFramebufferBlit = strstr(ptoken, "GL_EXT_framebuffer_blit") != NULL; s_bHaveCoverageMSAA = strstr(ptoken, "GL_NV_framebuffer_multisample_coverage") != NULL; - - // TODO: Switch over to using glew once 1.6/1.7 becomes more mainstream, seems most people are stuck in 1.5 - if (strstr((const char*)glGetString(GL_EXTENSIONS), "GL_ARB_shading_language_420pack") != NULL) - g_Config.backend_info.bSupportsGLSLBinding = true; - if (strstr((const char*)glGetString(GL_EXTENSIONS), "GL_ARB_uniform_buffer_object") != NULL) - g_Config.backend_info.bSupportsGLSLUBO = true; - if ((g_Config.backend_info.bSupportsGLSLBinding || g_Config.backend_info.bSupportsGLSLUBO) && strstr((const char*)glGetString(GL_EXTENSIONS), "GL_ARB_explicit_attrib_location") != NULL) - g_Config.backend_info.bSupportsGLSLATTRBind = true; - if (strstr((const char*)glGetString(GL_EXTENSIONS), "GL_ARB_get_program_binary") != NULL) - g_Config.backend_info.bSupportsGLSLCache = true; - - UpdateActiveConfig(); - OSD::AddMessage(StringFromFormat("Using GLSL. Supports Binding: %s UBOs: %s Cache: %s", - g_ActiveConfig.backend_info.bSupportsGLSLBinding ? "True" : "False", - g_ActiveConfig.backend_info.bSupportsGLSLUBO ? "True" : "False", - g_ActiveConfig.backend_info.bSupportsGLSLCache ? "True" : "False").c_str(), 5000); + if(g_ActiveConfig.bUseGLSL) + { + // TODO: Switch over to using glew once 1.6/1.7 becomes more mainstream, seems most people are stuck in 1.5 + if (strstr((const char*)glGetString(GL_EXTENSIONS), "GL_ARB_shading_language_420pack") != NULL) + g_Config.backend_info.bSupportsGLSLBinding = true; + if (strstr((const char*)glGetString(GL_EXTENSIONS), "GL_ARB_uniform_buffer_object") != NULL) + g_Config.backend_info.bSupportsGLSLUBO = true; + if ((g_Config.backend_info.bSupportsGLSLBinding || g_Config.backend_info.bSupportsGLSLUBO) && strstr((const char*)glGetString(GL_EXTENSIONS), "GL_ARB_explicit_attrib_location") != NULL) + g_Config.backend_info.bSupportsGLSLATTRBind = true; + if (strstr((const char*)glGetString(GL_EXTENSIONS), "GL_ARB_get_program_binary") != NULL) + g_Config.backend_info.bSupportsGLSLCache = true; + + UpdateActiveConfig(); + OSD::AddMessage(StringFromFormat("Using GLSL. Supports Binding: %s UBOs: %s Cache: %s", + g_ActiveConfig.backend_info.bSupportsGLSLBinding ? "True" : "False", + g_ActiveConfig.backend_info.bSupportsGLSLUBO ? "True" : "False", + g_ActiveConfig.backend_info.bSupportsGLSLCache ? "True" : "False").c_str(), 5000); + } s_LastMultisampleMode = g_ActiveConfig.iMultisampleMode; s_MSAASamples = GetNumMSAASamples(s_LastMultisampleMode); @@ -438,8 +440,8 @@ Renderer::Renderer() #else // If we don't have Nvidia CG, we HAVE to use GLSL g_Config.bUseGLSL = true; - UpdateActiveConfig(); + INFO_LOG(VIDEO, "CG not found, switching to GLSL"); #endif // HAVE_CG int nenvvertparams, nenvfragparams, naddrregisters[2]; From 8a48b42e4c79e1d55e28c4d2f67ed72e39057a14 Mon Sep 17 00:00:00 2001 From: Ryan Houdek Date: Thu, 29 Dec 2011 00:32:06 -0600 Subject: [PATCH 77/83] Add support for Dual source blending to older ATI cards that don't support 420pack but do support GL_ARB_blend_func_extended. This is more proper as well anyways. --- .../Core/VideoCommon/Src/PixelShaderGen.cpp | 15 ++++-- Source/Core/VideoCommon/Src/VideoConfig.h | 1 + .../Plugin_VideoOGL/Src/PixelShaderCache.cpp | 1 - .../Src/ProgramShaderCache.cpp | 8 +++ Source/Plugins/Plugin_VideoOGL/Src/Render.cpp | 51 +++++++++++-------- .../Plugin_VideoOGL/Src/VertexManager.cpp | 2 +- .../Plugin_VideoOGL/Src/VertexShaderCache.cpp | 2 - 7 files changed, 53 insertions(+), 27 deletions(-) diff --git a/Source/Core/VideoCommon/Src/PixelShaderGen.cpp b/Source/Core/VideoCommon/Src/PixelShaderGen.cpp index 277cd2e3e4..55a453c9b6 100644 --- a/Source/Core/VideoCommon/Src/PixelShaderGen.cpp +++ b/Source/Core/VideoCommon/Src/PixelShaderGen.cpp @@ -568,6 +568,8 @@ const char *GeneratePixelShaderCode(DSTALPHA_MODE dstAlphaMode, API_TYPE ApiType if (g_ActiveConfig.backend_info.bSupportsGLSLATTRBind) WRITE(p, "#extension GL_ARB_explicit_attrib_location : enable\n"); + if (g_ActiveConfig.backend_info.bSupportsGLSLBlend) + WRITE(p, "#extension GL_ARB_blend_func_extended : enable\n"); // Silly differences WRITE(p, "#define float2 vec2\n"); WRITE(p, "#define float3 vec3\n"); @@ -693,13 +695,20 @@ const char *GeneratePixelShaderCode(DSTALPHA_MODE dstAlphaMode, API_TYPE ApiType { // GLSL doesn't do main arguments // Once we switch to GLSL 1.3 we will bind a lot of these. - if (dstAlphaMode == DSTALPHA_DUAL_SOURCE_BLEND) { // This won't get hit unless we support GL 3.3 - WRITE(p, " layout(location = 0) out float4 ocol0;\n"); - WRITE(p, " layout(location = 0, index = 1) out float4 ocol1;\n"); // Will be supported later + if (g_ActiveConfig.backend_info.bSupportsGLSLBinding) + { + WRITE(p, " layout(location = 0) out float4 ocol0;\n"); + WRITE(p, " layout(location = 0, index = 1) out float4 ocol1;\n"); + } + else + { + WRITE(p, " out float4 ocol0;\n"); + WRITE(p, " out float4 ocol1;\n"); + } } else { diff --git a/Source/Core/VideoCommon/Src/VideoConfig.h b/Source/Core/VideoCommon/Src/VideoConfig.h index b8a7168dfe..d51d99d411 100644 --- a/Source/Core/VideoCommon/Src/VideoConfig.h +++ b/Source/Core/VideoCommon/Src/VideoConfig.h @@ -170,6 +170,7 @@ struct VideoConfig bool bSupportsGLSL; bool bSupportsGLSLBinding; + bool bSupportsGLSLBlend; bool bSupportsGLSLUBO; bool bSupportsGLSLATTRBind; bool bSupportsGLSLCache; diff --git a/Source/Plugins/Plugin_VideoOGL/Src/PixelShaderCache.cpp b/Source/Plugins/Plugin_VideoOGL/Src/PixelShaderCache.cpp index 026d3ec037..202f7dc58e 100644 --- a/Source/Plugins/Plugin_VideoOGL/Src/PixelShaderCache.cpp +++ b/Source/Plugins/Plugin_VideoOGL/Src/PixelShaderCache.cpp @@ -406,7 +406,6 @@ bool CompileGLSLPixelShader(FRAGMENTSHADER& ps, const char* pstrprogram) FILE *fp = fopen(szTemp, "wb"); fwrite(pstrprogram, strlen(pstrprogram), 1, fp); fclose(fp); - delete[] infoLog; } // Don't try to use this shader diff --git a/Source/Plugins/Plugin_VideoOGL/Src/ProgramShaderCache.cpp b/Source/Plugins/Plugin_VideoOGL/Src/ProgramShaderCache.cpp index eeb9614070..728e4e8a01 100644 --- a/Source/Plugins/Plugin_VideoOGL/Src/ProgramShaderCache.cpp +++ b/Source/Plugins/Plugin_VideoOGL/Src/ProgramShaderCache.cpp @@ -91,6 +91,14 @@ void ProgramShaderCache::SetProgramVariables(PCacheEntry &entry) if (entry.UniformLocations[a] != -1) glUniform1i(entry.UniformLocations[a], a); } + if (g_ActiveConfig.backend_info.bSupportsGLSLBlend) + { + // So we don't support binding, but we do support extended blending + // So we need to set a few more things here. + // Bind our out locations + glBindFragDataLocationIndexed(entry.prog_id, 0, 0, "ocol0"); + glBindFragDataLocationIndexed(entry.prog_id, 0, 1, "ocol1"); + } } // Need to get some attribute locations diff --git a/Source/Plugins/Plugin_VideoOGL/Src/Render.cpp b/Source/Plugins/Plugin_VideoOGL/Src/Render.cpp index 494744a447..5054bf7390 100644 --- a/Source/Plugins/Plugin_VideoOGL/Src/Render.cpp +++ b/Source/Plugins/Plugin_VideoOGL/Src/Render.cpp @@ -316,6 +316,8 @@ Renderer::Renderer() // TODO: Switch over to using glew once 1.6/1.7 becomes more mainstream, seems most people are stuck in 1.5 if (strstr((const char*)glGetString(GL_EXTENSIONS), "GL_ARB_shading_language_420pack") != NULL) g_Config.backend_info.bSupportsGLSLBinding = true; + if (glewIsSupported("GL_ARB_blend_func_extended")) + g_Config.backend_info.bSupportsGLSLBlend = true; if (strstr((const char*)glGetString(GL_EXTENSIONS), "GL_ARB_uniform_buffer_object") != NULL) g_Config.backend_info.bSupportsGLSLUBO = true; if ((g_Config.backend_info.bSupportsGLSLBinding || g_Config.backend_info.bSupportsGLSLUBO) && strstr((const char*)glGetString(GL_EXTENSIONS), "GL_ARB_explicit_attrib_location") != NULL) @@ -963,7 +965,7 @@ void Renderer::SetBlendMode(bool forceUpdate) bool useDstAlpha = !g_ActiveConfig.bDstAlphaPass && bpmem.dstalpha.enable && bpmem.blendmode.alphaupdate && bpmem.zcontrol.pixel_format == PIXELFMT_RGBA6_Z24; - bool useDualSource = useDstAlpha && g_ActiveConfig.bUseGLSL && g_ActiveConfig.backend_info.bSupportsGLSLBinding; + bool useDualSource = useDstAlpha && g_ActiveConfig.bUseGLSL && g_ActiveConfig.backend_info.bSupportsGLSLBlend; if (changes & 1) // blend enable change @@ -974,33 +976,42 @@ void Renderer::SetBlendMode(bool forceUpdate) // subtract enable change GLenum equation = newval & 4 ? GL_FUNC_REVERSE_SUBTRACT : GL_FUNC_ADD; GLenum equationAlpha = useDualSource ? GL_FUNC_ADD : equation; - glBlendEquationSeparate(equation, equationAlpha); + + if (g_ActiveConfig.backend_info.bSupportsGLSLBlend) + glBlendEquationSeparate(equation, equationAlpha); + else + glBlendEquation(newval & 4 ? GL_FUNC_REVERSE_SUBTRACT : GL_FUNC_ADD); } if (changes & 0x1F8) { - GLenum srcFactor = glSrcFactors[(newval >> 3) & 7]; - GLenum srcFactorAlpha = srcFactor; - GLenum dstFactor = glDestFactors[(newval >> 6) & 7]; - GLenum dstFactorAlpha = dstFactor; - if (useDualSource) + if (g_ActiveConfig.backend_info.bSupportsGLSLBlend) { - srcFactorAlpha = GL_ONE; - dstFactorAlpha = GL_ZERO; + GLenum srcFactor = glSrcFactors[(newval >> 3) & 7]; + GLenum srcFactorAlpha = srcFactor; + GLenum dstFactor = glDestFactors[(newval >> 6) & 7]; + GLenum dstFactorAlpha = dstFactor; + if (useDualSource) + { + srcFactorAlpha = GL_ONE; + dstFactorAlpha = GL_ZERO; - if (srcFactor == GL_SRC_ALPHA) - srcFactor = GL_SRC1_ALPHA; - else if (srcFactor == GL_ONE_MINUS_SRC_ALPHA) - srcFactor = GL_ONE_MINUS_SRC1_ALPHA; + if (srcFactor == GL_SRC_ALPHA) + srcFactor = GL_SRC1_ALPHA; + else if (srcFactor == GL_ONE_MINUS_SRC_ALPHA) + srcFactor = GL_ONE_MINUS_SRC1_ALPHA; - if (dstFactor == GL_SRC_ALPHA) - dstFactor = GL_SRC1_ALPHA; - else if (dstFactor == GL_ONE_MINUS_SRC_ALPHA) - dstFactor = GL_ONE_MINUS_SRC1_ALPHA; + if (dstFactor == GL_SRC_ALPHA) + dstFactor = GL_SRC1_ALPHA; + else if (dstFactor == GL_ONE_MINUS_SRC_ALPHA) + dstFactor = GL_ONE_MINUS_SRC1_ALPHA; + } + + // blend RGB change + glBlendFuncSeparate(srcFactor, dstFactor, srcFactorAlpha, dstFactorAlpha); } - - // blend RGB change - glBlendFuncSeparate(srcFactor, dstFactor, srcFactorAlpha, dstFactorAlpha); + else + glBlendFunc(glSrcFactors[(newval >> 3) & 7], glDestFactors[(newval >> 6) & 7]); } s_blendMode = newval; diff --git a/Source/Plugins/Plugin_VideoOGL/Src/VertexManager.cpp b/Source/Plugins/Plugin_VideoOGL/Src/VertexManager.cpp index f931f50c78..5a76c34d56 100644 --- a/Source/Plugins/Plugin_VideoOGL/Src/VertexManager.cpp +++ b/Source/Plugins/Plugin_VideoOGL/Src/VertexManager.cpp @@ -178,7 +178,7 @@ void VertexManager::vFlush() && bpmem.zcontrol.pixel_format == PIXELFMT_RGBA6_Z24; // Makes sure we can actually do Dual source blending - bool dualSourcePossible = g_ActiveConfig.bUseGLSL && g_ActiveConfig.backend_info.bSupportsGLSLBinding; + bool dualSourcePossible = g_ActiveConfig.bUseGLSL && g_ActiveConfig.backend_info.bSupportsGLSLBlend; // finally bind FRAGMENTSHADER* ps; diff --git a/Source/Plugins/Plugin_VideoOGL/Src/VertexShaderCache.cpp b/Source/Plugins/Plugin_VideoOGL/Src/VertexShaderCache.cpp index f396e90078..810cd4c54a 100644 --- a/Source/Plugins/Plugin_VideoOGL/Src/VertexShaderCache.cpp +++ b/Source/Plugins/Plugin_VideoOGL/Src/VertexShaderCache.cpp @@ -204,14 +204,12 @@ bool CompileGLSLVertexShader(VERTEXSHADER& vs, const char* pstrprogram) FILE *fp = fopen(szTemp, "wb"); fwrite(pstrprogram, strlen(pstrprogram), 1, fp); fclose(fp); - delete[] infoLog; } // Don't try to use this shader glDeleteShader(result); return false; } - (void)GL_REPORT_ERROR(); vs.glprogid = result; vs.bGLSL = true; From 6a5b56d25f4340bb4341ae5782e934d397750a4b Mon Sep 17 00:00:00 2001 From: Ryan Houdek Date: Thu, 29 Dec 2011 01:35:50 -0600 Subject: [PATCH 78/83] Get the program binary type correctly or else ATI makes massive (~400MB) shader caches. Also, don't need the line in the PixelShaderGen. --- .../Core/VideoCommon/Src/PixelShaderGen.cpp | 2 -- .../Src/ProgramShaderCache.cpp | 11 ++++++++ .../Plugin_VideoOGL/Src/ProgramShaderCache.h | 25 ++++++------------- 3 files changed, 19 insertions(+), 19 deletions(-) diff --git a/Source/Core/VideoCommon/Src/PixelShaderGen.cpp b/Source/Core/VideoCommon/Src/PixelShaderGen.cpp index 55a453c9b6..0b47a072ca 100644 --- a/Source/Core/VideoCommon/Src/PixelShaderGen.cpp +++ b/Source/Core/VideoCommon/Src/PixelShaderGen.cpp @@ -568,8 +568,6 @@ const char *GeneratePixelShaderCode(DSTALPHA_MODE dstAlphaMode, API_TYPE ApiType if (g_ActiveConfig.backend_info.bSupportsGLSLATTRBind) WRITE(p, "#extension GL_ARB_explicit_attrib_location : enable\n"); - if (g_ActiveConfig.backend_info.bSupportsGLSLBlend) - WRITE(p, "#extension GL_ARB_blend_func_extended : enable\n"); // Silly differences WRITE(p, "#define float2 vec2\n"); WRITE(p, "#define float3 vec3\n"); diff --git a/Source/Plugins/Plugin_VideoOGL/Src/ProgramShaderCache.cpp b/Source/Plugins/Plugin_VideoOGL/Src/ProgramShaderCache.cpp index 728e4e8a01..57470959d2 100644 --- a/Source/Plugins/Plugin_VideoOGL/Src/ProgramShaderCache.cpp +++ b/Source/Plugins/Plugin_VideoOGL/Src/ProgramShaderCache.cpp @@ -25,6 +25,7 @@ GLuint ProgramShaderCache::CurrentFShader = 0, ProgramShaderCache::CurrentVShade ProgramShaderCache::PCache ProgramShaderCache::pshaders; GLuint ProgramShaderCache::s_ps_vs_ubo; GLintptr ProgramShaderCache::s_vs_data_offset; +GLenum ProgramShaderCache::prog_format; LinearDiskCache g_program_disk_cache; GLenum ProgramFormat; @@ -213,6 +214,16 @@ void ProgramShaderCache::Init(void) // Read our shader cache, only if supported if (g_ActiveConfig.backend_info.bSupportsGLSLCache) { + GLint Supported; + glGetIntegerv(GL_NUM_PROGRAM_BINARY_FORMATS, &Supported); + + GLint *Formats = new GLint[Supported]; + glGetIntegerv(GL_PROGRAM_BINARY_FORMATS, Formats); + // We don't really care about format + // We just need the correct data type + prog_format = (GLenum)Formats[0]; + delete[] Formats; + char cache_filename[MAX_PATH]; sprintf(cache_filename, "%sogl-%s-shaders.cache", File::GetUserPath(D_SHADERCACHE_IDX).c_str(), SConfig::GetInstance().m_LocalCoreStartupParameter.m_strUniqueID.c_str()); diff --git a/Source/Plugins/Plugin_VideoOGL/Src/ProgramShaderCache.h b/Source/Plugins/Plugin_VideoOGL/Src/ProgramShaderCache.h index 6089f205e7..6ea72c05dd 100644 --- a/Source/Plugins/Plugin_VideoOGL/Src/ProgramShaderCache.h +++ b/Source/Plugins/Plugin_VideoOGL/Src/ProgramShaderCache.h @@ -41,7 +41,6 @@ public: struct PCacheEntry { GLuint prog_id; - GLenum prog_format; u8 *binary; GLint binary_size; GLuint vsid, psid; @@ -75,25 +74,15 @@ public: glGetProgramiv(prog_id, GL_PROGRAM_BINARY_LENGTH, &binary_size); } - // No idea how necessary this is - void SetProgramFormat() - { - GLint Supported; - glGetIntegerv(GL_NUM_PROGRAM_BINARY_FORMATS, &Supported); - - GLint *Formats = new GLint[Supported]; - glGetIntegerv(GL_PROGRAM_BINARY_FORMATS, Formats); - // We don't really care about format - prog_format = (GLenum)Formats[0]; - delete[] Formats; - } - u8 *GetProgram() { UpdateSize(); FreeProgram(); binary = new u8[binary_size]; - glGetProgramBinary(prog_id, binary_size, NULL, &prog_format, binary); + GLenum _form; + glGetProgramBinary(prog_id, binary_size, NULL, &_form, binary); + if (_form != prog_format) + ERROR_LOG(VIDEO, "Returned format not the same as expected! %d vs %d", _form, prog_format); return binary; } @@ -130,8 +119,7 @@ private: // But it is fine, no need to worry about that PCacheEntry entry; entry.Create(key.first, key.second); - - glProgramBinary(entry.prog_id, entry.prog_format, value, value_size); + glProgramBinary(entry.prog_id, prog_format, value, value_size); GLint success; glGetProgramiv(entry.prog_id, GL_LINK_STATUS, &success); @@ -153,6 +141,9 @@ private: static GLuint s_ps_vs_ubo; static GLintptr s_vs_data_offset; + + static GLenum prog_format; + static void SetProgramVariables(PCacheEntry &entry); }; From bd5cf8868610a7fe5060a85d62da33a28eb9a22b Mon Sep 17 00:00:00 2001 From: Shawn Hoffman Date: Thu, 29 Dec 2011 16:25:03 +0100 Subject: [PATCH 79/83] let us try normal c++ static init instead... --- .../Src/ProgramShaderCache.cpp | 13 ++-------- .../Plugin_VideoOGL/Src/ProgramShaderCache.h | 26 +++++++++++++------ 2 files changed, 20 insertions(+), 19 deletions(-) diff --git a/Source/Plugins/Plugin_VideoOGL/Src/ProgramShaderCache.cpp b/Source/Plugins/Plugin_VideoOGL/Src/ProgramShaderCache.cpp index 57470959d2..b178157a89 100644 --- a/Source/Plugins/Plugin_VideoOGL/Src/ProgramShaderCache.cpp +++ b/Source/Plugins/Plugin_VideoOGL/Src/ProgramShaderCache.cpp @@ -25,11 +25,12 @@ GLuint ProgramShaderCache::CurrentFShader = 0, ProgramShaderCache::CurrentVShade ProgramShaderCache::PCache ProgramShaderCache::pshaders; GLuint ProgramShaderCache::s_ps_vs_ubo; GLintptr ProgramShaderCache::s_vs_data_offset; -GLenum ProgramShaderCache::prog_format; LinearDiskCache g_program_disk_cache; GLenum ProgramFormat; +GLuint ProgramShaderCache::PCacheEntry::prog_format = ProgramShaderCache::PCacheEntry::SetProgramFormat(); + std::pair ProgramShaderCache::CurrentShaderProgram; const char *UniformNames[NUM_UNIFORMS] = { @@ -214,16 +215,6 @@ void ProgramShaderCache::Init(void) // Read our shader cache, only if supported if (g_ActiveConfig.backend_info.bSupportsGLSLCache) { - GLint Supported; - glGetIntegerv(GL_NUM_PROGRAM_BINARY_FORMATS, &Supported); - - GLint *Formats = new GLint[Supported]; - glGetIntegerv(GL_PROGRAM_BINARY_FORMATS, Formats); - // We don't really care about format - // We just need the correct data type - prog_format = (GLenum)Formats[0]; - delete[] Formats; - char cache_filename[MAX_PATH]; sprintf(cache_filename, "%sogl-%s-shaders.cache", File::GetUserPath(D_SHADERCACHE_IDX).c_str(), SConfig::GetInstance().m_LocalCoreStartupParameter.m_strUniqueID.c_str()); diff --git a/Source/Plugins/Plugin_VideoOGL/Src/ProgramShaderCache.h b/Source/Plugins/Plugin_VideoOGL/Src/ProgramShaderCache.h index 6ea72c05dd..07e77e3eae 100644 --- a/Source/Plugins/Plugin_VideoOGL/Src/ProgramShaderCache.h +++ b/Source/Plugins/Plugin_VideoOGL/Src/ProgramShaderCache.h @@ -41,6 +41,7 @@ public: struct PCacheEntry { GLuint prog_id; + static GLenum prog_format; u8 *binary; GLint binary_size; GLuint vsid, psid; @@ -74,15 +75,26 @@ public: glGetProgramiv(prog_id, GL_PROGRAM_BINARY_LENGTH, &binary_size); } + // No idea how necessary this is + static GLenum SetProgramFormat() + { + GLint Supported; + glGetIntegerv(GL_NUM_PROGRAM_BINARY_FORMATS, &Supported); + + GLint *Formats = new GLint[Supported]; + glGetIntegerv(GL_PROGRAM_BINARY_FORMATS, Formats); + // We don't really care about format + GLenum prog_format = (GLenum)Formats[0]; + delete[] Formats; + return prog_format; + } + u8 *GetProgram() { UpdateSize(); FreeProgram(); binary = new u8[binary_size]; - GLenum _form; - glGetProgramBinary(prog_id, binary_size, NULL, &_form, binary); - if (_form != prog_format) - ERROR_LOG(VIDEO, "Returned format not the same as expected! %d vs %d", _form, prog_format); + glGetProgramBinary(prog_id, binary_size, NULL, &prog_format, binary); return binary; } @@ -119,7 +131,8 @@ private: // But it is fine, no need to worry about that PCacheEntry entry; entry.Create(key.first, key.second); - glProgramBinary(entry.prog_id, prog_format, value, value_size); + + glProgramBinary(entry.prog_id, entry.prog_format, value, value_size); GLint success; glGetProgramiv(entry.prog_id, GL_LINK_STATUS, &success); @@ -141,9 +154,6 @@ private: static GLuint s_ps_vs_ubo; static GLintptr s_vs_data_offset; - - static GLenum prog_format; - static void SetProgramVariables(PCacheEntry &entry); }; From 576955c145e0eb2a7965684b9ba3f950790ccbc1 Mon Sep 17 00:00:00 2001 From: Ryan Houdek Date: Wed, 11 Jan 2012 01:18:54 -0600 Subject: [PATCH 80/83] Change 'stuff' --- .../Core/VideoCommon/Src/PixelShaderGen.cpp | 40 ++++++++++++++----- .../Core/VideoCommon/Src/VertexShaderGen.cpp | 34 +++++++++++++--- 2 files changed, 59 insertions(+), 15 deletions(-) diff --git a/Source/Core/VideoCommon/Src/PixelShaderGen.cpp b/Source/Core/VideoCommon/Src/PixelShaderGen.cpp index 0b47a072ca..8ba8fb0ce8 100644 --- a/Source/Core/VideoCommon/Src/PixelShaderGen.cpp +++ b/Source/Core/VideoCommon/Src/PixelShaderGen.cpp @@ -562,9 +562,15 @@ const char *GeneratePixelShaderCode(DSTALPHA_MODE dstAlphaMode, API_TYPE ApiType WRITE(p, "#extension GL_ARB_shading_language_420pack : enable\n"); if (g_ActiveConfig.backend_info.bSupportsGLSLUBO) WRITE(p, "#extension GL_ARB_uniform_buffer_object : enable\n"); - } - else - WRITE(p, "#version 120\n"); + WRITE(p, "#define ATTRIN in\n"); + WRITE(p, "#define ATTROUT out\n"); + } + else + { + WRITE(p, "#version 120\n"); + WRITE(p, "#define ATTRIN attribute\n"); + WRITE(p, "#define ATTROUT attribute\n"); + } if (g_ActiveConfig.backend_info.bSupportsGLSLATTRBind) WRITE(p, "#extension GL_ARB_explicit_attrib_location : enable\n"); @@ -720,13 +726,21 @@ const char *GeneratePixelShaderCode(DSTALPHA_MODE dstAlphaMode, API_TYPE ApiType WRITE(p, " float4 colors_1 = gl_SecondaryColor;\n"); // compute window position if needed because binding semantic WPOS is not widely supported - if (numTexgen < 7) + // Let's set up attributes + if (xfregs.numTexGen.numTexGens < 7) { - for (int i = 0; i < numTexgen; ++i) - WRITE(p, " float3 uv%d = gl_TexCoord[%d].xyz;\n", i, i); - WRITE(p, " float4 clipPos = gl_TexCoord[%d];\n", numTexgen); + for (int i = 0; i < 8; ++i) + { + WRITE(p, "ATTRIN float3 uv%d_2;\n", i); + WRITE(p, " float3 uv%d = uv%d_2;\n", i, i); + } + WRITE(p, "ATTRIN float4 clipPos_2;\n"); + WRITE(p, " float4 clipPos = clipPos_2;\n"); if (g_ActiveConfig.bEnablePixelLighting && g_ActiveConfig.backend_info.bSupportsPixelLighting) - WRITE(p, " float4 Normal = gl_TexCoord[%d];\n", numTexgen + 1); + { + WRITE(p, "ATTRIN float4 Normal_2;\n"); + WRITE(p, " float4 Normal = Normal_2;\n"); + } } else { @@ -734,12 +748,18 @@ const char *GeneratePixelShaderCode(DSTALPHA_MODE dstAlphaMode, API_TYPE ApiType if (g_ActiveConfig.bEnablePixelLighting && g_ActiveConfig.backend_info.bSupportsPixelLighting) { for (int i = 0; i < 8; ++i) - WRITE(p, " float4 uv%d = gl_TexCoord[%d];\n", i, i); + { + WRITE(p, "ATTRIN float4 uv%d_2;\n", i); + WRITE(p, " float4 uv%d = uv%d_2;\n", i, i); + } } else { for (unsigned int i = 0; i < xfregs.numTexGen.numTexGens; ++i) - WRITE(p, " float%d uv%d = gl_TexCoord[%d]%s;\n", i < 4 ? 4 : 3 , i, i, i < 4 ? "" : ".xyz"); + { + WRITE(p, "ATTRIN float%d uv%d_2;\n", i < 4 ? 4 : 3 , i); + WRITE(p, " float%d uv%d = uv%d_2;\n", i < 4 ? 4 : 3 , i, i); + } } } WRITE(p, "void main()\n{\n"); diff --git a/Source/Core/VideoCommon/Src/VertexShaderGen.cpp b/Source/Core/VideoCommon/Src/VertexShaderGen.cpp index 9f945f1b95..003e498e06 100644 --- a/Source/Core/VideoCommon/Src/VertexShaderGen.cpp +++ b/Source/Core/VideoCommon/Src/VertexShaderGen.cpp @@ -270,6 +270,30 @@ const char *GenerateVertexShaderCode(u32 components, API_TYPE ApiType) if ((components & (VB_HAS_UV0< Date: Wed, 11 Jan 2012 04:10:43 -0600 Subject: [PATCH 81/83] More "stuff" for SS, also a small fix in the program shader cache cache setup." --- .../Core/VideoCommon/Src/PixelShaderGen.cpp | 18 +++++++++++------- .../Core/VideoCommon/Src/VertexShaderGen.cpp | 19 ++++++++++++------- .../Plugin_VideoOGL/Src/ProgramShaderCache.h | 2 ++ 3 files changed, 25 insertions(+), 14 deletions(-) diff --git a/Source/Core/VideoCommon/Src/PixelShaderGen.cpp b/Source/Core/VideoCommon/Src/PixelShaderGen.cpp index 8ba8fb0ce8..e7dc8b73db 100644 --- a/Source/Core/VideoCommon/Src/PixelShaderGen.cpp +++ b/Source/Core/VideoCommon/Src/PixelShaderGen.cpp @@ -564,12 +564,16 @@ const char *GeneratePixelShaderCode(DSTALPHA_MODE dstAlphaMode, API_TYPE ApiType WRITE(p, "#extension GL_ARB_uniform_buffer_object : enable\n"); WRITE(p, "#define ATTRIN in\n"); WRITE(p, "#define ATTROUT out\n"); + WRITE(p, "#define VARYIN in\n"); + WRITE(p, "#define VARYOUT out\n"); } else { WRITE(p, "#version 120\n"); WRITE(p, "#define ATTRIN attribute\n"); - WRITE(p, "#define ATTROUT attribute\n"); + WRITE(p, "#define ATTROUT attribute\n"); // Can't really be used, but provide it anyway + WRITE(p, "#define VARYIN varying\n"); + WRITE(p, "#define VARYOUT varying\n"); } if (g_ActiveConfig.backend_info.bSupportsGLSLATTRBind) @@ -720,7 +724,7 @@ const char *GeneratePixelShaderCode(DSTALPHA_MODE dstAlphaMode, API_TYPE ApiType } if (DepthTextureEnable) WRITE(p, " float depth;\n"); // TODO: Passed to Vertex Shader right? - WRITE(p, " float4 rawpos = gl_FragCoord;\n"); + WRITE(p, "VARYIN float4 rawpos;\n"); WRITE(p, " float4 colors_0 = gl_Color;\n"); WRITE(p, " float4 colors_1 = gl_SecondaryColor;\n"); @@ -731,14 +735,14 @@ const char *GeneratePixelShaderCode(DSTALPHA_MODE dstAlphaMode, API_TYPE ApiType { for (int i = 0; i < 8; ++i) { - WRITE(p, "ATTRIN float3 uv%d_2;\n", i); + WRITE(p, "VARYIN float3 uv%d_2;\n", i); WRITE(p, " float3 uv%d = uv%d_2;\n", i, i); } - WRITE(p, "ATTRIN float4 clipPos_2;\n"); + WRITE(p, "VARYIN float4 clipPos_2;\n"); WRITE(p, " float4 clipPos = clipPos_2;\n"); if (g_ActiveConfig.bEnablePixelLighting && g_ActiveConfig.backend_info.bSupportsPixelLighting) { - WRITE(p, "ATTRIN float4 Normal_2;\n"); + WRITE(p, "VARYIN float4 Normal_2;\n"); WRITE(p, " float4 Normal = Normal_2;\n"); } } @@ -749,7 +753,7 @@ const char *GeneratePixelShaderCode(DSTALPHA_MODE dstAlphaMode, API_TYPE ApiType { for (int i = 0; i < 8; ++i) { - WRITE(p, "ATTRIN float4 uv%d_2;\n", i); + WRITE(p, "VARYIN float4 uv%d_2;\n", i); WRITE(p, " float4 uv%d = uv%d_2;\n", i, i); } } @@ -757,7 +761,7 @@ const char *GeneratePixelShaderCode(DSTALPHA_MODE dstAlphaMode, API_TYPE ApiType { for (unsigned int i = 0; i < xfregs.numTexGen.numTexGens; ++i) { - WRITE(p, "ATTRIN float%d uv%d_2;\n", i < 4 ? 4 : 3 , i); + WRITE(p, "VARYIN float%d uv%d_2;\n", i < 4 ? 4 : 3 , i); WRITE(p, " float%d uv%d = uv%d_2;\n", i < 4 ? 4 : 3 , i, i); } } diff --git a/Source/Core/VideoCommon/Src/VertexShaderGen.cpp b/Source/Core/VideoCommon/Src/VertexShaderGen.cpp index 003e498e06..a616d00674 100644 --- a/Source/Core/VideoCommon/Src/VertexShaderGen.cpp +++ b/Source/Core/VideoCommon/Src/VertexShaderGen.cpp @@ -197,12 +197,16 @@ const char *GenerateVertexShaderCode(u32 components, API_TYPE ApiType) WRITE(p, "#extension GL_ARB_uniform_buffer_object : enable\n"); WRITE(p, "#define ATTRIN in\n"); WRITE(p, "#define ATTROUT out\n"); + WRITE(p, "#define VARYIN in\n"); + WRITE(p, "#define VARYOUT out\n"); } else { WRITE(p, "#version 120\n"); WRITE(p, "#define ATTRIN attribute\n"); - WRITE(p, "#define ATTROUT attribute\n"); + WRITE(p, "#define ATTROUT attribute\n"); // Can't really be used, but provide it anyway + WRITE(p, "#define VARYIN varying\n"); + WRITE(p, "#define VARYOUT varying\n"); } if (g_ActiveConfig.backend_info.bSupportsGLSLATTRBind) WRITE(p, "#extension GL_ARB_explicit_attrib_location : enable\n"); @@ -275,10 +279,10 @@ const char *GenerateVertexShaderCode(u32 components, API_TYPE ApiType) if (xfregs.numTexGen.numTexGens < 7) { for (int i = 0; i < 8; ++i) - WRITE(p, "ATTROUT float3 uv%d_2;\n", i); - WRITE(p, "ATTROUT float4 clipPos_2;\n"); + WRITE(p, "VARYOUT float3 uv%d_2;\n", i); + WRITE(p, "VARYOUT float4 clipPos_2;\n"); if (g_ActiveConfig.bEnablePixelLighting && g_ActiveConfig.backend_info.bSupportsPixelLighting) - WRITE(p, "ATTROUT float4 Normal_2;\n"); + WRITE(p, "VARYOUT float4 Normal_2;\n"); } else { @@ -286,17 +290,18 @@ const char *GenerateVertexShaderCode(u32 components, API_TYPE ApiType) if (g_ActiveConfig.bEnablePixelLighting && g_ActiveConfig.backend_info.bSupportsPixelLighting) { for (int i = 0; i < 8; ++i) - WRITE(p, "ATTROUT float4 uv%d_2;\n", i); + WRITE(p, "VARYOUT float4 uv%d_2;\n", i); } else { for (unsigned int i = 0; i < xfregs.numTexGen.numTexGens; ++i) - WRITE(p, "ATTROUT float%d uv%d_2;\n", i < 4 ? 4 : 3 , i); + WRITE(p, "VARYOUT float%d uv%d_2;\n", i < 4 ? 4 : 3 , i); } } - WRITE(p, " float4 rawpos = gl_Vertex;\n") ; + WRITE(p, "VARYOUT float4 rawpos;\n") ; WRITE(p, "void main()\n{\n"); + WRITE(p, "rawpos = gl_Vertex;\n"); } else { diff --git a/Source/Plugins/Plugin_VideoOGL/Src/ProgramShaderCache.h b/Source/Plugins/Plugin_VideoOGL/Src/ProgramShaderCache.h index 07e77e3eae..d4a12aac43 100644 --- a/Source/Plugins/Plugin_VideoOGL/Src/ProgramShaderCache.h +++ b/Source/Plugins/Plugin_VideoOGL/Src/ProgramShaderCache.h @@ -78,6 +78,8 @@ public: // No idea how necessary this is static GLenum SetProgramFormat() { + if(!g_ActiveConfig.backend_info.bSupportsGLSLCache) + return 0; GLint Supported; glGetIntegerv(GL_NUM_PROGRAM_BINARY_FORMATS, &Supported); From d355ad70554ff2655b1e12a83663cd43f8483320 Mon Sep 17 00:00:00 2001 From: Ryan Houdek Date: Wed, 11 Jan 2012 16:00:30 -0600 Subject: [PATCH 82/83] More 'stuff' --- Source/Core/VideoCommon/Src/PixelShaderGen.cpp | 8 +++++--- Source/Core/VideoCommon/Src/VertexShaderGen.cpp | 9 +++++---- 2 files changed, 10 insertions(+), 7 deletions(-) diff --git a/Source/Core/VideoCommon/Src/PixelShaderGen.cpp b/Source/Core/VideoCommon/Src/PixelShaderGen.cpp index e7dc8b73db..6ce7fdd417 100644 --- a/Source/Core/VideoCommon/Src/PixelShaderGen.cpp +++ b/Source/Core/VideoCommon/Src/PixelShaderGen.cpp @@ -724,10 +724,12 @@ const char *GeneratePixelShaderCode(DSTALPHA_MODE dstAlphaMode, API_TYPE ApiType } if (DepthTextureEnable) WRITE(p, " float depth;\n"); // TODO: Passed to Vertex Shader right? - WRITE(p, "VARYIN float4 rawpos;\n"); + WRITE(p, " float4 rawpos = gl_FragCoord;\n"); - WRITE(p, " float4 colors_0 = gl_Color;\n"); - WRITE(p, " float4 colors_1 = gl_SecondaryColor;\n"); + WRITE(p, "VARYIN float4 colors_02;\n"); + WRITE(p, "VARYIN float4 colors_12;\n"); + WRITE(p, " float4 colors_0 = colors_02;\n"); + WRITE(p, " float4 colors_1 = colors_12;\n"); // compute window position if needed because binding semantic WPOS is not widely supported // Let's set up attributes diff --git a/Source/Core/VideoCommon/Src/VertexShaderGen.cpp b/Source/Core/VideoCommon/Src/VertexShaderGen.cpp index a616d00674..e05e299a3a 100644 --- a/Source/Core/VideoCommon/Src/VertexShaderGen.cpp +++ b/Source/Core/VideoCommon/Src/VertexShaderGen.cpp @@ -298,10 +298,11 @@ const char *GenerateVertexShaderCode(u32 components, API_TYPE ApiType) WRITE(p, "VARYOUT float%d uv%d_2;\n", i < 4 ? 4 : 3 , i); } } - WRITE(p, "VARYOUT float4 rawpos;\n") ; + WRITE(p, " float4 rawpos = gl_Vertex;\n"); + WRITE(p, "VARYOUT float4 colors_02;\n"); + WRITE(p, "VARYOUT float4 colors_12;\n"); WRITE(p, "void main()\n{\n"); - WRITE(p, "rawpos = gl_Vertex;\n"); } else { @@ -620,8 +621,8 @@ const char *GenerateVertexShaderCode(u32 components, API_TYPE ApiType) WRITE(p, " uv%d_2%s = o.tex%d;\n", i, i < 4 ? ".xyzw" : ".xyz" , i); } } - WRITE(p, "gl_FrontColor = o.colors_0;\n"); - WRITE(p, "gl_FrontSecondaryColor = o.colors_1;\n"); + WRITE(p, "colors_02 = o.colors_0;\n"); + WRITE(p, "colors_12 = o.colors_1;\n"); WRITE(p, "gl_Position = o.pos;\n"); WRITE(p, "}\n"); } From f57f654b5997e7525f64a6241b84c6a7b13b21cf Mon Sep 17 00:00:00 2001 From: Shawn Hoffman Date: Wed, 18 Jan 2012 10:22:03 -0800 Subject: [PATCH 83/83] set ProgramShaderCache program format correctly. --- Source/Plugins/Plugin_VideoOGL/Src/ProgramShaderCache.cpp | 5 ++++- Source/Plugins/Plugin_VideoOGL/Src/ProgramShaderCache.h | 2 -- 2 files changed, 4 insertions(+), 3 deletions(-) diff --git a/Source/Plugins/Plugin_VideoOGL/Src/ProgramShaderCache.cpp b/Source/Plugins/Plugin_VideoOGL/Src/ProgramShaderCache.cpp index b178157a89..8f3378dca8 100644 --- a/Source/Plugins/Plugin_VideoOGL/Src/ProgramShaderCache.cpp +++ b/Source/Plugins/Plugin_VideoOGL/Src/ProgramShaderCache.cpp @@ -29,7 +29,7 @@ GLintptr ProgramShaderCache::s_vs_data_offset; LinearDiskCache g_program_disk_cache; GLenum ProgramFormat; -GLuint ProgramShaderCache::PCacheEntry::prog_format = ProgramShaderCache::PCacheEntry::SetProgramFormat(); +GLuint ProgramShaderCache::PCacheEntry::prog_format = 0; std::pair ProgramShaderCache::CurrentShaderProgram; const char *UniformNames[NUM_UNIFORMS] = @@ -215,9 +215,12 @@ void ProgramShaderCache::Init(void) // Read our shader cache, only if supported if (g_ActiveConfig.backend_info.bSupportsGLSLCache) { + PCacheEntry::prog_format = PCacheEntry::SetProgramFormat(); + char cache_filename[MAX_PATH]; sprintf(cache_filename, "%sogl-%s-shaders.cache", File::GetUserPath(D_SHADERCACHE_IDX).c_str(), SConfig::GetInstance().m_LocalCoreStartupParameter.m_strUniqueID.c_str()); + ProgramShaderCacheInserter inserter; g_program_disk_cache.OpenAndRead(cache_filename, inserter); } diff --git a/Source/Plugins/Plugin_VideoOGL/Src/ProgramShaderCache.h b/Source/Plugins/Plugin_VideoOGL/Src/ProgramShaderCache.h index d4a12aac43..07e77e3eae 100644 --- a/Source/Plugins/Plugin_VideoOGL/Src/ProgramShaderCache.h +++ b/Source/Plugins/Plugin_VideoOGL/Src/ProgramShaderCache.h @@ -78,8 +78,6 @@ public: // No idea how necessary this is static GLenum SetProgramFormat() { - if(!g_ActiveConfig.backend_info.bSupportsGLSLCache) - return 0; GLint Supported; glGetIntegerv(GL_NUM_PROGRAM_BINARY_FORMATS, &Supported);