From ec08914905ca1c4fe3b2456a9cc037a3f7eedd49 Mon Sep 17 00:00:00 2001 From: NeoBrainX Date: Wed, 10 Apr 2013 12:54:22 +0200 Subject: [PATCH] Move Shader UID mismatch checking to VideoCommon. --- .../Core/VideoCommon/Src/PixelShaderGen.cpp | 7 ++ Source/Core/VideoCommon/Src/PixelShaderGen.h | 1 - Source/Core/VideoCommon/Src/ShaderGenCommon.h | 47 ++++++++++++++ .../Core/VideoCommon/Src/VertexShaderGen.cpp | 7 ++ .../Src/ProgramShaderCache.cpp | 64 ------------------- 5 files changed, 61 insertions(+), 65 deletions(-) diff --git a/Source/Core/VideoCommon/Src/PixelShaderGen.cpp b/Source/Core/VideoCommon/Src/PixelShaderGen.cpp index 69c096f002..2d9121856c 100644 --- a/Source/Core/VideoCommon/Src/PixelShaderGen.cpp +++ b/Source/Core/VideoCommon/Src/PixelShaderGen.cpp @@ -1207,6 +1207,13 @@ static void WriteFog(T& out, pixel_shader_uid_data& uid_data) void GetPixelShaderUid(PixelShaderUid& object, DSTALPHA_MODE dstAlphaMode, API_TYPE ApiType, u32 components) { GeneratePixelShader(object, dstAlphaMode, ApiType, components); + + if (g_ActiveConfig.bEnableShaderDebugging) + { + PixelShaderCode code; + GeneratePixelShaderCode(code, dstAlphaMode, API_OPENGL, components); + CheckForUidMismatch(code, object); + } } void GeneratePixelShaderCode(PixelShaderCode& object, DSTALPHA_MODE dstAlphaMode, API_TYPE ApiType, u32 components) diff --git a/Source/Core/VideoCommon/Src/PixelShaderGen.h b/Source/Core/VideoCommon/Src/PixelShaderGen.h index 3c6db3fa8f..001ab53f0b 100644 --- a/Source/Core/VideoCommon/Src/PixelShaderGen.h +++ b/Source/Core/VideoCommon/Src/PixelShaderGen.h @@ -226,7 +226,6 @@ typedef ShaderUid PixelShaderUid; typedef ShaderCode PixelShaderCode; // TODO: Obsolete typedef ShaderConstantProfile PixelShaderConstantProfile; // TODO: Obsolete - void GeneratePixelShaderCode(PixelShaderCode& object, DSTALPHA_MODE dstAlphaMode, API_TYPE ApiType, u32 components); void GetPixelShaderUid(PixelShaderUid& object, DSTALPHA_MODE dstAlphaMode, API_TYPE ApiType, u32 components); void GetPixelShaderConstantProfile(PixelShaderConstantProfile& object, DSTALPHA_MODE dstAlphaMode, API_TYPE ApiType, u32 components); diff --git a/Source/Core/VideoCommon/Src/ShaderGenCommon.h b/Source/Core/VideoCommon/Src/ShaderGenCommon.h index 4d41cd62f1..07af93c295 100644 --- a/Source/Core/VideoCommon/Src/ShaderGenCommon.h +++ b/Source/Core/VideoCommon/Src/ShaderGenCommon.h @@ -22,6 +22,8 @@ #include #include #include +#include +#include #include "CommonTypes.h" #include "VideoCommon.h" @@ -166,4 +168,49 @@ struct LightingUidData } lit_chans[4]; }; +struct pixel_shader_uid_data; +struct vertex_shader_uid_data; + +typedef ShaderUid PixelShaderUid; +typedef ShaderUid VertexShaderUid; + +template +void CheckForUidMismatch(CodeT& new_code, const UidT& new_uid) +{ + static std::map s_shaders; + static std::vector s_uids; + + bool uid_is_indexed = std::find(s_uids.begin(), s_uids.end(), new_uid) != s_uids.end(); + if (!uid_is_indexed) + { + s_uids.push_back(new_uid); + s_shaders[new_uid] = new_code.GetBuffer(); + } + else + { + // uid is already in the index => check if there's a shader with the same uid but different code + auto& old_code = s_shaders[new_uid]; + if (strcmp(old_code.c_str(), new_code.GetBuffer()) != 0) + { + static int num_failures = 0; + + char szTemp[MAX_PATH]; + sprintf(szTemp, "%s%ssuid_mismatch_%04i.txt", File::GetUserPath(D_DUMP_IDX).c_str(), + (typeid(UidT) == typeid(PixelShaderUid)) ? "p" : (typeid(UidT) == typeid(VertexShaderUid)) ? "v" : "o", + ++num_failures); + + // TODO: Should also dump uids + std::ofstream file; + OpenFStream(file, szTemp, std::ios_base::out); + file << "Old shader code:\n" << old_code; + file << "\n\nNew shader code:\n" << new_code.GetBuffer(); + file.close(); + + // TODO: Make this more idiot-proof + ERROR_LOG(VIDEO, "%s shader uid mismatch!", + (typeid(UidT) == typeid(PixelShaderUid)) ? "Pixel" : (typeid(UidT) == typeid(VertexShaderUid)) ? "Vertex" : "Other"); + } + } +} + #endif // _SHADERGENCOMMON_H diff --git a/Source/Core/VideoCommon/Src/VertexShaderGen.cpp b/Source/Core/VideoCommon/Src/VertexShaderGen.cpp index 452ee7fd3f..7c1553890b 100644 --- a/Source/Core/VideoCommon/Src/VertexShaderGen.cpp +++ b/Source/Core/VideoCommon/Src/VertexShaderGen.cpp @@ -524,6 +524,13 @@ static void GenerateVertexShader(T& out, u32 components, API_TYPE api_type) void GetVertexShaderUid(VertexShaderUid& object, u32 components, API_TYPE api_type) { GenerateVertexShader(object, components, api_type); + + if (g_ActiveConfig.bEnableShaderDebugging) + { + VertexShaderCode code; + GenerateVertexShaderCode(code, components, API_OPENGL); + CheckForUidMismatch(code, object); + } } void GenerateVertexShaderCode(VertexShaderCode& object, u32 components, API_TYPE api_type) diff --git a/Source/Plugins/Plugin_VideoOGL/Src/ProgramShaderCache.cpp b/Source/Plugins/Plugin_VideoOGL/Src/ProgramShaderCache.cpp index 81621d2d38..0d571c29a9 100644 --- a/Source/Plugins/Plugin_VideoOGL/Src/ProgramShaderCache.cpp +++ b/Source/Plugins/Plugin_VideoOGL/Src/ProgramShaderCache.cpp @@ -23,9 +23,6 @@ #include "ImageWrite.h" #include "Render.h" -#include -#include - namespace OGL { @@ -356,73 +353,12 @@ GLuint ProgramShaderCache::CompileSingleShader (GLuint type, const char* code ) return result; } -template UidT GetPartialUid(const SHADERUID& uid); -template<> PixelShaderUid GetPartialUid(const SHADERUID& uid) { return uid.puid; } -template<> VertexShaderUid GetPartialUid(const SHADERUID& uid) { return uid.vuid; } - -template const std::string& GetShaderCode(const SHADER& shader); -template<> const std::string& GetShaderCode(const SHADER& shader) { return shader.strpprog; } -template<> const std::string& GetShaderCode(const SHADER& shader) { return shader.strvprog; } - -template -void CheckForUidMismatch(const ProgramShaderCache::PCache& cache, CodeT& new_code, const UidT& new_uid) -{ - static std::map s_shaders; - static std::vector s_uids; - - bool uid_is_indexed = std::find(s_uids.begin(), s_uids.end(), new_uid) != s_uids.end(); - if (!uid_is_indexed) - { - s_uids.push_back(new_uid); - s_shaders[new_uid] = new_code.GetBuffer(); - } - else - { - // uid is already in the index => check if there's a shader with the same uid but different code - auto& old_code = s_shaders[new_uid]; - if (strcmp(old_code.c_str(), new_code.GetBuffer()) != 0) - { - static int num_failures = 0; - - char szTemp[MAX_PATH]; - sprintf(szTemp, "%s%ssuid_mismatch_%04i.txt", File::GetUserPath(D_DUMP_IDX).c_str(), - (typeid(UidT) == typeid(PixelShaderUid)) ? "p" : (typeid(UidT) == typeid(VertexShaderUid)) ? "v" : "o", - ++num_failures); - - // TODO: Should also dump uids - std::ofstream file; - OpenFStream(file, szTemp, std::ios_base::out); - file << "Old shader code:\n" << old_code; - file << "\n\nNew shader code:\n" << new_code.GetBuffer(); - file.close(); - - // TODO: Make this more idiot-proof - ERROR_LOG(VIDEO, "%s shader uid mismatch!", - (typeid(UidT) == typeid(PixelShaderUid)) ? "Pixel" : (typeid(UidT) == typeid(VertexShaderUid)) ? "Vertex" : "Other"); - } - } -} - - void ProgramShaderCache::GetShaderId(SHADERUID* uid, DSTALPHA_MODE dstAlphaMode, u32 components) { GetPixelShaderUid(uid->puid, dstAlphaMode, API_OPENGL, components); GetVertexShaderUid(uid->vuid, components, API_OPENGL); - - if (g_ActiveConfig.bEnableShaderDebugging) - { - PixelShaderCode pcode; - VertexShaderCode vcode; - - GeneratePixelShaderCode(pcode, dstAlphaMode, API_OPENGL, components); - GenerateVertexShaderCode(vcode, components, API_OPENGL); - - CheckForUidMismatch(pshaders, pcode, uid->puid); - CheckForUidMismatch(pshaders, vcode, uid->vuid); - } } - ProgramShaderCache::PCacheEntry ProgramShaderCache::GetShaderProgram(void) { return *last_entry;