diff options
Diffstat (limited to 'Source/Plugins/Plugin_VideoOGL')
14 files changed, 269 insertions, 325 deletions
diff --git a/Source/Plugins/Plugin_VideoOGL/CMakeLists.txt b/Source/Plugins/Plugin_VideoOGL/CMakeLists.txt index c57106645f..b506087106 100644 --- a/Source/Plugins/Plugin_VideoOGL/CMakeLists.txt +++ b/Source/Plugins/Plugin_VideoOGL/CMakeLists.txt @@ -22,8 +22,10 @@ if(wxWidgets_FOUND) set(LIBS ${LIBS} ${wxWidgets_LIBRARIES}) endif(wxWidgets_FOUND) -if(APPLE OR WIN32 OR ${CMAKE_SYSTEM_NAME} MATCHES "Linux") +if(WIN32 OR ${CMAKE_SYSTEM_NAME} MATCHES "Linux") set(LIBS ${LIBS} Cg CgGL) +elseif(${CMAKE_SYSTEM_NAME} MATCHES "Darwin") + list(APPEND LIBS "${CMAKE_SOURCE_DIR}/Externals/Cg/Cg.framework") endif() if(NOT ${CMAKE_SYSTEM_NAME} MATCHES "Darwin") diff --git a/Source/Plugins/Plugin_VideoOGL/Plugin_VideoOGL.vcxproj b/Source/Plugins/Plugin_VideoOGL/Plugin_VideoOGL.vcxproj index d12f779d53..f782fd2c93 100644 --- a/Source/Plugins/Plugin_VideoOGL/Plugin_VideoOGL.vcxproj +++ b/Source/Plugins/Plugin_VideoOGL/Plugin_VideoOGL.vcxproj @@ -46,7 +46,7 @@ <ConfigurationType>StaticLibrary</ConfigurationType> <UseDebugLibraries>false</UseDebugLibraries> <CharacterSet>Unicode</CharacterSet> - <WholeProgramOptimization>true</WholeProgramOptimization> + <WholeProgramOptimization>false</WholeProgramOptimization> </PropertyGroup> <PropertyGroup Condition="'$(Configuration)|$(Platform)'=='DebugFast|Win32'" Label="Configuration"> <ConfigurationType>StaticLibrary</ConfigurationType> @@ -57,7 +57,6 @@ <ConfigurationType>StaticLibrary</ConfigurationType> <UseDebugLibraries>false</UseDebugLibraries> <CharacterSet>Unicode</CharacterSet> - <WholeProgramOptimization>true</WholeProgramOptimization> </PropertyGroup> <PropertyGroup Condition="'$(Configuration)|$(Platform)'=='DebugFast|x64'" Label="Configuration"> <ConfigurationType>StaticLibrary</ConfigurationType> diff --git a/Source/Plugins/Plugin_VideoOGL/Src/GLUtil.cpp b/Source/Plugins/Plugin_VideoOGL/Src/GLUtil.cpp index bedcaa6245..cbdb702885 100644 --- a/Source/Plugins/Plugin_VideoOGL/Src/GLUtil.cpp +++ b/Source/Plugins/Plugin_VideoOGL/Src/GLUtil.cpp @@ -73,8 +73,9 @@ void OpenGL_SetWindowText(const char *text) #elif defined(__APPLE__) [GLWin.cocoaWin setTitle: [NSString stringWithUTF8String: text]]; #elif defined(_WIN32) - // TODO convert text to unicode and change SetWindowTextA to SetWindowText - SetWindowTextA(EmuWindow::GetWnd(), text); + TCHAR temp[512]; + swprintf_s(temp, sizeof(temp)/sizeof(TCHAR), _T("%hs"), text); + EmuWindow::SetWindowText(temp); #elif defined(HAVE_X11) && HAVE_X11 // Tell X to ask the window manager to set the window title. // (X itself doesn't provide window title functionality.) diff --git a/Source/Plugins/Plugin_VideoOGL/Src/PixelShaderCache.cpp b/Source/Plugins/Plugin_VideoOGL/Src/PixelShaderCache.cpp index fb4c226f77..61e41008b3 100644 --- a/Source/Plugins/Plugin_VideoOGL/Src/PixelShaderCache.cpp +++ b/Source/Plugins/Plugin_VideoOGL/Src/PixelShaderCache.cpp @@ -44,7 +44,8 @@ bool PixelShaderCache::s_displayCompileAlert; GLuint PixelShaderCache::CurrentShader; bool PixelShaderCache::ShaderEnabled; -static FRAGMENTSHADER* pShaderLast = NULL; +PixelShaderCache::PSCacheEntry* PixelShaderCache::last_entry = NULL; +PIXELSHADERUID PixelShaderCache::last_uid; GLuint PixelShaderCache::GetDepthMatrixProgram() { @@ -61,10 +62,9 @@ void PixelShaderCache::Init() glEnable(GL_FRAGMENT_PROGRAM_ARB); ShaderEnabled = true; CurrentShader = 0; + last_entry = NULL; GL_REPORT_ERRORD(); - memset(&last_pixel_shader_uid, 0xFF, sizeof(last_pixel_shader_uid)); - s_displayCompileAlert = true; glGetProgramivARB(GL_FRAGMENT_PROGRAM_ARB, GL_MAX_PROGRAM_NATIVE_ALU_INSTRUCTIONS_ARB, (GLint *)&s_nMaxPixelInstructions); @@ -184,38 +184,43 @@ void PixelShaderCache::Shutdown() FRAGMENTSHADER* PixelShaderCache::SetShader(DSTALPHA_MODE dstAlphaMode, u32 components) { PIXELSHADERUID uid; - GetPixelShaderId(&uid, dstAlphaMode); - + GetPixelShaderId(&uid, dstAlphaMode, components); + // Check if the shader is already set - if (uid == last_pixel_shader_uid && PixelShaders[uid].frameCount == frameCount) + if (last_entry) { - GFX_DEBUGGER_PAUSE_AT(NEXT_PIXEL_SHADER_CHANGE, true); - return pShaderLast; + 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; + } } - memcpy(&last_pixel_shader_uid, &uid, sizeof(PIXELSHADERUID)); + last_uid = uid; PSCache::iterator iter = PixelShaders.find(uid); - if (iter != PixelShaders.end()) { - iter->second.frameCount = frameCount; PSCacheEntry &entry = iter->second; - if (&entry.shader != pShaderLast) - { - pShaderLast = &entry.shader; - } + last_entry = &entry; GFX_DEBUGGER_PAUSE_AT(NEXT_PIXEL_SHADER_CHANGE, true); - return pShaderLast; + 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]; - newentry.frameCount = frameCount; - pShaderLast = &newentry.shader; + 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; @@ -234,7 +239,7 @@ FRAGMENTSHADER* PixelShaderCache::SetShader(DSTALPHA_MODE dstAlphaMode, u32 comp INCSTAT(stats.numPixelShadersCreated); SETSTAT(stats.numPixelShadersAlive, PixelShaders.size()); GFX_DEBUGGER_PAUSE_AT(NEXT_PIXEL_SHADER_CHANGE, true); - return pShaderLast; + return &last_entry->shader; } bool PixelShaderCache::CompilePixelShader(FRAGMENTSHADER& ps, const char* pstrprogram) @@ -247,7 +252,7 @@ bool PixelShaderCache::CompilePixelShader(FRAGMENTSHADER& ps, const char* pstrpr #if defined HAVE_CG && HAVE_CG char stropt[128]; - sprintf(stropt, "MaxLocalParams=32,NumInstructionSlots=%d", s_nMaxPixelInstructions); + sprintf(stropt, "MaxLocalParams=224,NumInstructionSlots=%d", s_nMaxPixelInstructions); const char *opts[] = {"-profileopts", stropt, "-O2", "-q", NULL}; CGprogram tempprog = cgCreateProgram(g_cgcontext, CG_SOURCE, pstrprogram, g_cgfProf, "main", opts); @@ -318,9 +323,6 @@ bool PixelShaderCache::CompilePixelShader(FRAGMENTSHADER& ps, const char* pstrpr cgDestroyProgram(tempprog); #endif -#if defined(_DEBUG) || defined(DEBUGFAST) - ps.strprog = pstrprogram; -#endif return true; } diff --git a/Source/Plugins/Plugin_VideoOGL/Src/PixelShaderCache.h b/Source/Plugins/Plugin_VideoOGL/Src/PixelShaderCache.h index f404b206a2..f528eeb35b 100644 --- a/Source/Plugins/Plugin_VideoOGL/Src/PixelShaderCache.h +++ b/Source/Plugins/Plugin_VideoOGL/Src/PixelShaderCache.h @@ -39,9 +39,7 @@ struct FRAGMENTSHADER } } GLuint glprogid; // opengl program id -#if defined(_DEBUG) || defined(DEBUGFAST) std::string strprog; -#endif }; class PixelShaderCache @@ -49,13 +47,13 @@ class PixelShaderCache struct PSCacheEntry { FRAGMENTSHADER shader; - int frameCount; - PSCacheEntry() : frameCount(0) {} + PSCacheEntry() {} ~PSCacheEntry() {} void Destroy() { shader.Destroy(); } + PIXELSHADERUIDSAFE safe_uid; }; typedef std::map<PIXELSHADERUID, PSCacheEntry> PSCache; @@ -67,6 +65,8 @@ class PixelShaderCache static bool s_displayCompileAlert; static GLuint CurrentShader; + static PSCacheEntry* last_entry; + static PIXELSHADERUID last_uid; static bool ShaderEnabled; diff --git a/Source/Plugins/Plugin_VideoOGL/Src/Render.cpp b/Source/Plugins/Plugin_VideoOGL/Src/Render.cpp index 0c9d904cad..9883d35fbb 100644 --- a/Source/Plugins/Plugin_VideoOGL/Src/Render.cpp +++ b/Source/Plugins/Plugin_VideoOGL/Src/Render.cpp @@ -60,6 +60,7 @@ #include "Core.h" #include "Movie.h" #include "Host.h" +#include "BPFunctions.h" #include "main.h" // Local #ifdef _WIN32 @@ -109,12 +110,6 @@ int s_fps=0; RasterFont* s_pfont = NULL; -#if defined _WIN32 || defined HAVE_LIBAV -static bool s_bAVIDumping = false; -#else -static File::IOFile f_pFrameDump; -#endif - // 1 for no MSAA. Use s_MSAASamples > 1 to check for MSAA. static int s_MSAASamples = 1; static int s_MSAACoverageSamples = 0; @@ -128,6 +123,12 @@ static u32 s_blendMode; static std::thread scrshotThread; #endif +// EFB cache related +const u32 EFB_CACHE_RECT_SIZE = 64; // Cache 64x64 blocks. +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<u32> s_efbCache[2][EFB_CACHE_WIDTH * EFB_CACHE_HEIGHT]; // 2 for PEEK_Z and PEEK_COLOR static const GLenum glSrcFactors[8] = { @@ -249,9 +250,6 @@ Renderer::Renderer() s_fps=0; s_blendMode = 0; -#if defined _WIN32 || defined HAVE_LIBAV - s_bAVIDumping = false; -#endif #if defined HAVE_CG && HAVE_CG g_cgcontext = cgCreateContext(); @@ -515,19 +513,6 @@ Renderer::~Renderer() #endif delete g_framebuffer_manager; - -#if defined _WIN32 || defined HAVE_LIBAV - if(s_bAVIDumping) - { - AVIDump::Stop(); - s_bLastFrameDumped = false; - s_bAVIDumping = false; - } -#else - if (f_pFrameDump.IsOpen()) - f_pFrameDump.Close(); - s_bLastFrameDumped = false; -#endif } // Create On-Screen-Messages @@ -610,7 +595,7 @@ void Renderer::RenderText(const char *text, int left, int top, u32 color) { const int nBackbufferWidth = (int)OpenGL_GetBackbufferWidth(); const int nBackbufferHeight = (int)OpenGL_GetBackbufferHeight(); - + glColor4f(((color>>16) & 0xff)/255.0f, ((color>> 8) & 0xff)/255.0f, ((color>> 0) & 0xff)/255.0f, ((color>>24) & 0xFF)/255.0f); @@ -642,49 +627,9 @@ TargetRectangle Renderer::ConvertEFBRectangle(const EFBRectangle& rc) // Renderer::GetTargetHeight() = the fixed ini file setting // donkopunchstania - it appears scissorBR is the bottom right pixel inside the scissor box // therefore the width and height are (scissorBR + 1) - scissorTL -bool Renderer::SetScissorRect() +void Renderer::SetScissorRect(const TargetRectangle& rc) { - MathUtil::Rectangle<float> rc; - GetScissorRect(rc); - - if (rc.left < 0) rc.left = 0; - if (rc.top < 0) rc.top = 0; - if (rc.right > EFB_WIDTH) rc.right = EFB_WIDTH; - if (rc.bottom > EFB_HEIGHT) rc.bottom = EFB_HEIGHT; - - if (rc.left > rc.right) - { - int temp = rc.right; - rc.right = rc.left; - rc.left = temp; - } - if (rc.top > rc.bottom) - { - int temp = rc.bottom; - rc.bottom = rc.top; - rc.top = temp; - } - - // Check that the coordinates are good - if (rc.right != rc.left && rc.bottom != rc.top) - { - glScissor( - EFBToScaledX(rc.left), // x = 0 for example - EFBToScaledY(EFB_HEIGHT - rc.bottom), // y = 0 for example - EFBToScaledX(rc.right - rc.left), // width = 640 for example - EFBToScaledY(rc.bottom - rc.top)); // height = 480 for example - return true; - } - else - { - glScissor( - 0, - 0, - Renderer::GetTargetWidth(), - Renderer::GetTargetHeight() - ); - } - return false; + glScissor(rc.left, rc.bottom, rc.GetWidth(), rc.GetHeight()); } void Renderer::SetColorMask() @@ -698,6 +643,42 @@ void Renderer::SetColorMask() glColorMask(ColorMask, ColorMask, ColorMask, AlphaMask); } +void ClearEFBCache() +{ + for (u32 i = 0; i < EFB_CACHE_WIDTH * EFB_CACHE_HEIGHT; ++i) + s_efbCacheValid[0][i] = false; + + for (u32 i = 0; i < EFB_CACHE_WIDTH * EFB_CACHE_HEIGHT; ++i) + s_efbCacheValid[1][i] = false; +} + +void Renderer::UpdateEFBCache(EFBAccessType type, u32 cacheRectIdx, const EFBRectangle& efbPixelRc, const TargetRectangle& targetPixelRc, const u32* data) +{ + u32 cacheType = (type == PEEK_Z ? 0 : 1); + + if (!s_efbCache[cacheType][cacheRectIdx].size()) + s_efbCache[cacheType][cacheRectIdx].resize(EFB_CACHE_RECT_SIZE * EFB_CACHE_RECT_SIZE); + + u32 targetPixelRcWidth = targetPixelRc.right - targetPixelRc.left; + + for (u32 yCache = 0; yCache < EFB_CACHE_RECT_SIZE; ++yCache) + { + u32 yEFB = efbPixelRc.top + yCache; + u32 yPixel = (EFBToScaledY(EFB_HEIGHT - yEFB) + EFBToScaledY(EFB_HEIGHT - yEFB - 1)) / 2; + u32 yData = yPixel - targetPixelRc.bottom; + + for (u32 xCache = 0; xCache < EFB_CACHE_RECT_SIZE; ++xCache) + { + u32 xEFB = efbPixelRc.left + xCache; + u32 xPixel = (EFBToScaledX(xEFB) + EFBToScaledX(xEFB + 1)) / 2; + u32 xData = xPixel - targetPixelRc.left; + s_efbCache[cacheType][cacheRectIdx][yCache * EFB_CACHE_RECT_SIZE + xCache] = data[yData * targetPixelRcWidth + xData]; + } + } + + s_efbCacheValid[cacheType][cacheRectIdx] = true; +} + // This function allows the CPU to directly access the EFB. // There are EFB peeks (which will read the color or depth of a pixel) // and EFB pokes (which will change the color or depth of a pixel). @@ -717,34 +698,50 @@ u32 Renderer::AccessEFB(EFBAccessType type, u32 x, u32 y, u32 poke_data) if (!g_ActiveConfig.bEFBAccessEnable) return 0; - // Get the rectangular target region covered by the EFB pixel + 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; - efbPixelRc.left = x; - efbPixelRc.top = y; - efbPixelRc.right = x + 1; - efbPixelRc.bottom = y + 1; + efbPixelRc.left = (x / EFB_CACHE_RECT_SIZE) * EFB_CACHE_RECT_SIZE; + efbPixelRc.top = (y / EFB_CACHE_RECT_SIZE) * EFB_CACHE_RECT_SIZE; + efbPixelRc.right = efbPixelRc.left + EFB_CACHE_RECT_SIZE; + efbPixelRc.bottom = efbPixelRc.top + EFB_CACHE_RECT_SIZE; TargetRectangle targetPixelRc = ConvertEFBRectangle(efbPixelRc); + u32 targetPixelRcWidth = targetPixelRc.right - targetPixelRc.left; + u32 targetPixelRcHeight = targetPixelRc.top - targetPixelRc.bottom; // TODO (FIX) : currently, AA path is broken/offset and doesn't return the correct pixel switch (type) { case PEEK_Z: { - if (s_MSAASamples > 1) + u32 z; + + if (!s_efbCacheValid[0][cacheRectIdx]) { - // Resolve our rectangle. - FramebufferManager::GetEFBDepthTexture(efbPixelRc); - glBindFramebufferEXT(GL_READ_FRAMEBUFFER_EXT, FramebufferManager::GetResolvedFramebuffer()); - } + if (s_MSAASamples > 1) + { + // Resolve our rectangle. + FramebufferManager::GetEFBDepthTexture(efbPixelRc); + glBindFramebufferEXT(GL_READ_FRAMEBUFFER_EXT, FramebufferManager::GetResolvedFramebuffer()); + } + + u32* depthMap = new u32[targetPixelRcWidth * targetPixelRcHeight]; - // Sample from the center of the target region. - int srcX = (targetPixelRc.left + targetPixelRc.right) / 2; - int srcY = (targetPixelRc.top + targetPixelRc.bottom) / 2; + glReadPixels(targetPixelRc.left, targetPixelRc.bottom, targetPixelRcWidth, targetPixelRcHeight, + GL_DEPTH_COMPONENT, GL_UNSIGNED_INT, depthMap); + GL_REPORT_ERRORD(); - u32 z = 0; - glReadPixels(srcX, srcY, 1, 1, GL_DEPTH_COMPONENT, GL_UNSIGNED_INT, &z); - GL_REPORT_ERRORD(); + UpdateEFBCache(type, cacheRectIdx, efbPixelRc, targetPixelRc, depthMap); + + delete[] depthMap; + } + + u32 xRect = x % EFB_CACHE_RECT_SIZE; + u32 yRect = y % EFB_CACHE_RECT_SIZE; + z = s_efbCache[0][cacheRectIdx][yRect * EFB_CACHE_RECT_SIZE + xRect]; // Scale the 32-bit value returned by glReadPixels to a 24-bit // value (GC uses a 24-bit Z-buffer). @@ -769,21 +766,31 @@ u32 Renderer::AccessEFB(EFBAccessType type, u32 x, u32 y, u32 poke_data) // determine if we're aiming at an enemy (0x80 / 0x88) or not (0x70) // Wind Waker is also using it for the pictograph to determine the color of each pixel - if (s_MSAASamples > 1) + u32 color; + + if (!s_efbCacheValid[1][cacheRectIdx]) { - // Resolve our rectangle. - FramebufferManager::GetEFBColorTexture(efbPixelRc); - glBindFramebufferEXT(GL_READ_FRAMEBUFFER_EXT, FramebufferManager::GetResolvedFramebuffer()); - } + if (s_MSAASamples > 1) + { + // Resolve our rectangle. + FramebufferManager::GetEFBColorTexture(efbPixelRc); + glBindFramebufferEXT(GL_READ_FRAMEBUFFER_EXT, FramebufferManager::GetResolvedFramebuffer()); + } + + u32* colorMap = new u32[targetPixelRcWidth * targetPixelRcHeight]; + + glReadPixels(targetPixelRc.left, targetPixelRc.bottom, targetPixelRcWidth, targetPixelRcHeight, + GL_BGRA, GL_UNSIGNED_INT_8_8_8_8_REV, colorMap); + GL_REPORT_ERRORD(); - // Sample from the center of the target region. - int srcX = (targetPixelRc.left + targetPixelRc.right) / 2; - int srcY = (targetPixelRc.top + targetPixelRc.bottom) / 2; + UpdateEFBCache(type, cacheRectIdx, efbPixelRc, targetPixelRc, colorMap); + + delete[] colorMap; + } - // Read back pixel in BGRA format, then byteswap to get GameCube's ARGB Format. - u32 color = 0; - glReadPixels(srcX, srcY, 1, 1, GL_BGRA, GL_UNSIGNED_INT_8_8_8_8_REV, &color); - GL_REPORT_ERRORD(); + u32 xRect = x % EFB_CACHE_RECT_SIZE; + u32 yRect = y % EFB_CACHE_RECT_SIZE; + color = s_efbCache[1][cacheRectIdx][yRect * EFB_CACHE_RECT_SIZE + xRect]; // check what to do with the alpha channel (GX_PokeAlphaRead) PixelEngine::UPEAlphaReadReg alpha_read_mode; @@ -865,41 +872,35 @@ void Renderer::ClearScreen(const EFBRectangle& rc, bool colorEnable, bool alphaE { ResetAPIState(); - GLenum ColorMask = GL_FALSE, AlphaMask = GL_FALSE; - if (colorEnable) ColorMask = GL_TRUE; - if (alphaEnable) AlphaMask = GL_TRUE; - glColorMask(ColorMask, ColorMask, ColorMask, AlphaMask); + // color + GLboolean const + color_mask = colorEnable ? GL_TRUE : GL_FALSE, + alpha_mask = alphaEnable ? GL_TRUE : GL_FALSE; + glColorMask(color_mask, color_mask, color_mask, alpha_mask); - if (zEnable) - { - glEnable(GL_DEPTH_TEST); - glDepthMask(GL_TRUE); - glDepthFunc(GL_ALWAYS); - } - else - { - glDisable(GL_DEPTH_TEST); - glDepthMask(GL_FALSE); - glDepthFunc(GL_NEVER); - } + glClearColor( + float((color >> 16) & 0xFF) / 255.0f, + float((color >> 8) & 0xFF) / 255.0f, + float((color >> 0) & 0xFF) / 255.0f, + float((color >> 24) & 0xFF) / 255.0f); + + // depth + glDepthMask(zEnable ? GL_TRUE : GL_FALSE); + + glClearDepth(float(z & 0xFFFFFF) / float(0xFFFFFF)); + + // Update rect for clearing the picture + glEnable(GL_SCISSOR_TEST); + + TargetRectangle const targetRc = ConvertEFBRectangle(rc); + glScissor(targetRc.left, targetRc.bottom, targetRc.GetWidth(), targetRc.GetHeight()); - // Update viewport for clearing the picture - TargetRectangle targetRc = ConvertEFBRectangle(rc); - glViewport(targetRc.left, targetRc.bottom, targetRc.GetWidth(), targetRc.GetHeight()); - glDepthRange(0.0, (float)(z & 0xFFFFFF) / float(0xFFFFFF)); - - glColor4f((float)((color >> 16) & 0xFF) / 255.0f, - (float)((color >> 8) & 0xFF) / 255.0f, - (float)(color & 0xFF) / 255.0f, - (float)((color >> 24) & 0xFF) / 255.0f); - glBegin(GL_QUADS); - glVertex3f(-1.f, -1.f, 1.f); - glVertex3f(-1.f, 1.f, 1.f); - glVertex3f( 1.f, 1.f, 1.f); - glVertex3f( 1.f, -1.f, 1.f); - glEnd(); + // glColorMask/glDepthMask/glScissor affect glClear (glViewport does not) + glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); RestoreAPIState(); + + ClearEFBCache(); } void Renderer::ReinterpretPixelData(unsigned int convtype) @@ -986,15 +987,14 @@ void Renderer::SetBlendMode(bool forceUpdate) // This function has the final picture. We adjust the aspect ratio here. void Renderer::Swap(u32 xfbAddr, FieldType field, u32 fbWidth, u32 fbHeight,const EFBRectangle& rc,float Gamma) { - static u8 *data = NULL; static int w = 0, h = 0; if (g_bSkipCurrentFrame || (!XFBWrited && (!g_ActiveConfig.bUseXFB || !g_ActiveConfig.bUseRealXFB)) || !fbWidth || !fbHeight) { - if (g_ActiveConfig.bDumpFrames && data) + if (g_ActiveConfig.bDumpFrames && frame_data) #ifdef _WIN32 - AVIDump::AddFrame((char *) data); + AVIDump::AddFrame(frame_data); #elif defined HAVE_LIBAV - AVIDump::AddFrame(data, w, h); + AVIDump::AddFrame((u8*)frame_data, w, h); #endif Core::Callback_VideoCopiedToXFB(false); return; @@ -1008,12 +1008,14 @@ void Renderer::Swap(u32 xfbAddr, FieldType field, u32 fbWidth, u32 fbHeight,cons const XFBSourceBase* const* xfbSourceList = FramebufferManager::GetXFBSource(xfbAddr, fbWidth, fbHeight, xfbCount); if ((!xfbSourceList || xfbCount == 0) && g_ActiveConfig.bUseXFB && !g_ActiveConfig.bUseRealXFB) { - if (g_ActiveConfig.bDumpFrames && data) - #ifdef _WIN32 - AVIDump::AddFrame((char *) data); - #elif defined HAVE_LIBAV - AVIDump::AddFrame(data, w, h); - #endif + if (g_ActiveConfig.bDumpFrames && frame_data) + { +#ifdef _WIN32 + AVIDump::AddFrame(frame_data); +#elif defined HAVE_LIBAV + AVIDump::AddFrame((u8*)frame_data, w, h); +#endif + } Core::Callback_VideoCopiedToXFB(false); return; } @@ -1172,26 +1174,26 @@ void Renderer::Swap(u32 xfbAddr, FieldType field, u32 fbWidth, u32 fbHeight,cons if (g_ActiveConfig.bDumpFrames) { std::lock_guard<std::mutex> lk(s_criticalScreenshot); - if (!data || w != dst_rect.GetWidth() || + if (!frame_data || w != dst_rect.GetWidth() || h != dst_rect.GetHeight()) { - if (data) delete[] data; + if (frame_data) delete[] frame_data; w = dst_rect.GetWidth(); h = dst_rect.GetHeight(); - data = new u8[3 * w * h]; + frame_data = new char[3 * w * h]; } glPixelStorei(GL_PACK_ALIGNMENT, 1); - glReadPixels(dst_rect.left, dst_rect.bottom, w, h, GL_BGR, GL_UNSIGNED_BYTE, data); + glReadPixels(dst_rect.left, dst_rect.bottom, w, h, GL_BGR, GL_UNSIGNED_BYTE, frame_data); if (GL_REPORT_ERROR() == GL_NO_ERROR && w > 0 && h > 0) { - if (!s_bLastFrameDumped) + if (!bLastFrameDumped) { #ifdef _WIN32 - s_bAVIDumping = AVIDump::Start(EmuWindow::GetParentWnd(), w, h); + bAVIDumping = AVIDump::Start(EmuWindow::GetParentWnd(), w, h); #else - s_bAVIDumping = AVIDump::Start(w, h); + bAVIDumping = AVIDump::Start(w, h); #endif - if (!s_bAVIDumping) + if (!bAVIDumping) OSD::AddMessage("AVIDump Start failed", 2000); else { @@ -1200,36 +1202,36 @@ void Renderer::Swap(u32 xfbAddr, FieldType field, u32 fbWidth, u32 fbHeight,cons File::GetUserPath(D_DUMPFRAMES_IDX).c_str(), w, h).c_str(), 2000); } } - if (s_bAVIDumping) + if (bAVIDumping) { #ifdef _WIN32 - AVIDump::AddFrame((char *) data); + AVIDump::AddFrame(frame_data); #else - FlipImageData(data, w, h); - AVIDump::AddFrame(data, w, h); + FlipImageData((u8*)frame_data, w, h); + AVIDump::AddFrame((u8*)frame_data, w, h); #endif } - s_bLastFrameDumped = true; + bLastFrameDumped = true; } else NOTICE_LOG(VIDEO, "Error reading framebuffer"); } else { - if (s_bLastFrameDumped && s_bAVIDumping) + if (bLastFrameDumped && bAVIDumping) { - if (data) + if (frame_data) { - delete[] data; - data = NULL; + delete[] frame_data; + frame_data = NULL; w = h = 0; } AVIDump::Stop(); - s_bAVIDumping = false; + bAVIDumping = false; OSD::AddMessage("Stop dumping frames", 2000); } - s_bLastFrameDumped = false; + bLastFrameDumped = false; } #else if (g_ActiveConfig.bDumpFrames) @@ -1238,16 +1240,16 @@ void Renderer::Swap(u32 xfbAddr, FieldType field, u32 fbWidth, u32 fbHeight,cons std::string movie_file_name; w = dst_rect.GetWidth(); h = dst_rect.GetHeight(); - data = new u8[3 * w * h]; + frame_data = new char[3 * w * h]; glPixelStorei(GL_PACK_ALIGNMENT, 1); - glReadPixels(dst_rect.left, dst_rect.bottom, w, h, GL_BGR, GL_UNSIGNED_BYTE, data); + glReadPixels(dst_rect.left, dst_rect.bottom, w, h, GL_BGR, GL_UNSIGNED_BYTE, frame_data); if (GL_REPORT_ERROR() == GL_NO_ERROR) { - if (!s_bLastFrameDumped) + if (!bLastFrameDumped) { movie_file_name = File::GetUserPath(D_DUMPFRAMES_IDX) + "framedump.raw"; - f_pFrameDump.Open(movie_file_name, "wb"); - if (!f_pFrameDump) + pFrameDump.Open(movie_file_name, "wb"); + if (!pFrameDump) OSD::AddMessage("Error opening framedump.raw for writing.", 2000); else { @@ -1256,22 +1258,22 @@ void Renderer::Swap(u32 xfbAddr, FieldType field, u32 fbWidth, u32 fbHeight,cons OSD::AddMessage(msg, 2000); } } - if (f_pFrameDump) + if (pFrameDump) { - FlipImageData(data, w, h); - f_pFrameDump.WriteBytes(data, w * 3 * h); - f_pFrameDump.Flush(); + FlipImageData((u8*)frame_data, w, h); + pFrameDump.WriteBytes(frame_data, w * 3 * h); + pFrameDump.Flush(); } - s_bLastFrameDumped = true; + bLastFrameDumped = true; } - delete[] data; + delete[] frame_data; } else { - if (s_bLastFrameDumped) - f_pFrameDump.Close(); - s_bLastFrameDumped = false; + if (bLastFrameDumped) + pFrameDump.Close(); + bLastFrameDumped = false; } #endif @@ -1392,8 +1394,7 @@ void Renderer::Swap(u32 xfbAddr, FieldType field, u32 fbWidth, u32 fbHeight,cons g_Config.iSaveTargetId = 0; // reload textures if these settings changed - if (g_Config.bSafeTextureCache != g_ActiveConfig.bSafeTextureCache || - g_Config.bUseNativeMips != g_ActiveConfig.bUseNativeMips) + if (g_Config.bUseNativeMips != g_ActiveConfig.bUseNativeMips) TextureCache::Invalidate(false); if (g_Config.bCopyEFBToTexture != g_ActiveConfig.bCopyEFBToTexture) @@ -1407,6 +1408,9 @@ void Renderer::Swap(u32 xfbAddr, FieldType field, u32 fbWidth, u32 fbHeight,cons // GetTargetWidth(), GetTargetHeight()); Core::Callback_VideoCopiedToXFB(XFBWrited || (g_ActiveConfig.bUseXFB && g_ActiveConfig.bUseRealXFB)); XFBWrited = false; + + // Invalidate EFB cache + ClearEFBCache(); } // ALWAYS call RestoreAPIState for each ResetAPIState call you're doing @@ -1422,9 +1426,6 @@ void Renderer::ResetAPIState() glDisable(GL_BLEND); glDepthMask(GL_FALSE); glColorMask(GL_TRUE, GL_TRUE, GL_TRUE, GL_TRUE); - - // make sure to disable wireframe when drawing the clear quad - glPolygonMode(GL_FRONT_AND_BACK, GL_FILL); } void Renderer::RestoreAPIState() @@ -1432,14 +1433,13 @@ void Renderer::RestoreAPIState() // Gets us back into a more game-like state. glEnable(GL_SCISSOR_TEST); SetGenerationMode(); - SetScissorRect(); + BPFunctions::SetScissor(); SetColorMask(); SetDepthMode(); SetBlendMode(true); VertexShaderManager::SetViewportChanged(); - if (g_ActiveConfig.bWireFrame) - glPolygonMode(GL_FRONT_AND_BACK, GL_LINE); + glPolygonMode(GL_FRONT_AND_BACK, g_ActiveConfig.bWireFrame ? GL_LINE : GL_FILL); VertexShaderCache::SetCurrentShader(0); PixelShaderCache::SetCurrentShader(0); diff --git a/Source/Plugins/Plugin_VideoOGL/Src/Render.h b/Source/Plugins/Plugin_VideoOGL/Src/Render.h index 5805c524ae..9cf1573b56 100644 --- a/Source/Plugins/Plugin_VideoOGL/Src/Render.h +++ b/Source/Plugins/Plugin_VideoOGL/Src/Render.h @@ -7,6 +7,8 @@ namespace OGL { +void ClearEFBCache(); + class Renderer : public ::Renderer { public: @@ -15,7 +17,7 @@ public: void SetColorMask(); void SetBlendMode(bool forceUpdate); - bool SetScissorRect(); + void SetScissorRect(const TargetRectangle& rc); void SetGenerationMode(); void SetDepthMode(); void SetLogicOpMode(); @@ -57,6 +59,9 @@ public: void SetVSConstant4fv(unsigned int const_number, const float *f); void SetMultiVSConstant3fv(unsigned int const_number, unsigned int count, const float *f); void SetMultiVSConstant4fv(unsigned int const_number, unsigned int count, const float *f); + +private: + void UpdateEFBCache(EFBAccessType type, u32 cacheRectIdx, const EFBRectangle& efbPixelRc, const TargetRectangle& targetPixelRc, const u32* data); }; } diff --git a/Source/Plugins/Plugin_VideoOGL/Src/TextureCache.cpp b/Source/Plugins/Plugin_VideoOGL/Src/TextureCache.cpp index 4a9431a563..f78eb11905 100644 --- a/Source/Plugins/Plugin_VideoOGL/Src/TextureCache.cpp +++ b/Source/Plugins/Plugin_VideoOGL/Src/TextureCache.cpp @@ -39,6 +39,7 @@ #include "Globals.h" #include "Hash.h" #include "HiresTextures.h" +#include "HW/Memmap.h" #include "ImageWrite.h" #include "MemoryUtil.h" #include "PixelShaderCache.h" @@ -124,7 +125,7 @@ bool TextureCache::TCacheEntry::Save(const char filename[]) std::string tga_filename(filename); tga_filename.replace(tga_filename.size() - 3, 3, "tga"); - return SaveTexture(tga_filename.c_str(), GL_TEXTURE_2D, texture, realW, realH); + return SaveTexture(tga_filename.c_str(), GL_TEXTURE_2D, texture, virtual_width, virtual_height); } TextureCache::TCacheEntryBase* TextureCache::CreateTexture(unsigned int width, @@ -278,7 +279,7 @@ void TextureCache::TCacheEntry::FromRenderTarget(u32 dstAddr, unsigned int dstFo GL_REPORT_ERRORD(); - if (false == isDynamic || g_ActiveConfig.bCopyEFBToTexture) + if (type != TCET_EC_DYNAMIC || g_ActiveConfig.bCopyEFBToTexture) { if (s_TempFramebuffer == 0) glGenFramebuffersEXT(1, (GLuint*)&s_TempFramebuffer); @@ -294,7 +295,7 @@ void TextureCache::TCacheEntry::FromRenderTarget(u32 dstAddr, unsigned int dstFo glEnable(GL_TEXTURE_RECTANGLE_ARB); glBindTexture(GL_TEXTURE_RECTANGLE_ARB, read_texture); - glViewport(0, 0, virtualW, virtualH); + glViewport(0, 0, virtual_width, virtual_height); PixelShaderCache::SetCurrentShader((srcFormat == PIXELFMT_Z24) ? PixelShaderCache::GetDepthMatrixProgram() : PixelShaderCache::GetColorMatrixProgram()); PixelShaderManager::SetColorMatrix(colmat); // set transformation @@ -317,7 +318,7 @@ void TextureCache::TCacheEntry::FromRenderTarget(u32 dstAddr, unsigned int dstFo if (false == g_ActiveConfig.bCopyEFBToTexture) { - hash = TextureConverter::EncodeToRamFromTexture( + int encoded_size = TextureConverter::EncodeToRamFromTexture( addr, read_texture, srcFormat == PIXELFMT_Z24, @@ -325,6 +326,15 @@ void TextureCache::TCacheEntry::FromRenderTarget(u32 dstAddr, unsigned int dstFo dstFormat, scaleByHalf, srcRect); + + u8* dst = Memory::GetPointer(addr); + hash = GetHash64(dst,encoded_size,g_ActiveConfig.iSafeTextureCache_ColorSamples); + + // Mark texture entries in destination address range dynamic unless caching is enabled and the texture entry is up to date + if (!g_ActiveConfig.bEFBCopyCacheEnable) + TextureCache::MakeRangeDynamic(addr,encoded_size); + else if (!TextureCache::Find(addr, hash)) + TextureCache::MakeRangeDynamic(addr,encoded_size); } FramebufferManager::SetFramebuffer(0); @@ -337,7 +347,7 @@ void TextureCache::TCacheEntry::FromRenderTarget(u32 dstAddr, unsigned int dstFo { static int count = 0; SaveTexture(StringFromFormat("%sefb_frame_%i.tga", File::GetUserPath(D_DUMPTEXTURES_IDX).c_str(), - count++).c_str(), GL_TEXTURE_2D, texture, realW, realH); + count++).c_str(), GL_TEXTURE_2D, texture, virtual_width, virtual_height); } } diff --git a/Source/Plugins/Plugin_VideoOGL/Src/TextureConverter.cpp b/Source/Plugins/Plugin_VideoOGL/Src/TextureConverter.cpp index c715775194..c73d3abdbf 100644 --- a/Source/Plugins/Plugin_VideoOGL/Src/TextureConverter.cpp +++ b/Source/Plugins/Plugin_VideoOGL/Src/TextureConverter.cpp @@ -250,78 +250,7 @@ void EncodeToRamUsingShader(FRAGMENTSHADER& shader, GLuint srcTexture, const Tar } -void EncodeToRam(u32 address, bool bFromZBuffer, bool bIsIntensityFmt, u32 copyfmt, int bScaleByHalf, const EFBRectangle& source) -{ - u32 format = copyfmt; - - if (bFromZBuffer) - { - format |= _GX_TF_ZTF; - if (copyfmt == 11) - format = GX_TF_Z16; - else if (format < GX_TF_Z8 || format > GX_TF_Z24X8) - format |= _GX_TF_CTF; - } - else - if (copyfmt > GX_TF_RGBA8 || (copyfmt < GX_TF_RGB565 && !bIsIntensityFmt)) - format |= _GX_TF_CTF; - - FRAGMENTSHADER& texconv_shader = GetOrCreateEncodingShader(format); - if (texconv_shader.glprogid == 0) - return; - - u8 *dest_ptr = Memory::GetPointer(address); - - GLuint source_texture = bFromZBuffer ? FramebufferManager::ResolveAndGetDepthTarget(source) : FramebufferManager::ResolveAndGetRenderTarget(source); - - int width = (source.right - source.left) >> bScaleByHalf; - int height = (source.bottom - source.top) >> bScaleByHalf; - - int size_in_bytes = TexDecoder_GetTextureSizeInBytes(width, height, format); - - // Invalidate any existing texture covering this memory range. - // TODO - don't delete the texture if it already exists, just replace the contents. - TextureCache::InvalidateRange(address, size_in_bytes); - - u16 blkW = TexDecoder_GetBlockWidthInTexels(format) - 1; - u16 blkH = TexDecoder_GetBlockHeightInTexels(format) - 1; - u16 samples = TextureConversionShader::GetEncodedSampleCount(format); - - // only copy on cache line boundaries - // extra pixels are copied but not displayed in the resulting texture - s32 expandedWidth = (width + blkW) & (~blkW); - 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; - scaledSource.bottom = expandedHeight; - scaledSource.left = 0; - scaledSource.right = expandedWidth / samples; - int cacheBytes = 32; - if ((format & 0x0f) == 6) - cacheBytes = 64; - - 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); - FramebufferManager::SetFramebuffer(0); - VertexShaderManager::SetViewportChanged(); - glBindTexture(GL_TEXTURE_RECTANGLE_ARB, 0); - TextureCache::DisableStage(0); - g_renderer->RestoreAPIState(); - GL_REPORT_ERRORD(); -} - -u64 EncodeToRamFromTexture(u32 address,GLuint source_texture, bool bFromZBuffer, bool bIsIntensityFmt, u32 copyfmt, int bScaleByHalf, const EFBRectangle& source) +int EncodeToRamFromTexture(u32 address,GLuint source_texture, bool bFromZBuffer, bool bIsIntensityFmt, u32 copyfmt, int bScaleByHalf, const EFBRectangle& source) { u32 format = copyfmt; @@ -379,19 +308,8 @@ u64 EncodeToRamFromTexture(u32 address,GLuint source_texture, bool bFromZBuffer, EncodeToRamUsingShader(texconv_shader, source_texture, scaledSource, dest_ptr, expandedWidth / samples, expandedHeight, readStride, true, bScaleByHalf > 0 && !bFromZBuffer); + return size_in_bytes; // TODO: D3D11 is calculating this value differently! - u64 hash = GetHash64(dest_ptr, size_in_bytes, - g_ActiveConfig.iSafeTextureCache_ColorSamples); - if (g_ActiveConfig.bEFBCopyCacheEnable) - { - // If the texture in RAM is already in the texture cache, - // do not copy it again as it has not changed. - if (TextureCache::Find(address, hash)) - return hash; - } - - TextureCache::MakeRangeDynamic(address,size_in_bytes); - return hash; } void EncodeToRamYUYV(GLuint srcTexture, const TargetRectangle& sourceRc, u8* destAddr, int dstWidth, int dstHeight) diff --git a/Source/Plugins/Plugin_VideoOGL/Src/TextureConverter.h b/Source/Plugins/Plugin_VideoOGL/Src/TextureConverter.h index 719ac1b7fc..4113778603 100644 --- a/Source/Plugins/Plugin_VideoOGL/Src/TextureConverter.h +++ b/Source/Plugins/Plugin_VideoOGL/Src/TextureConverter.h @@ -32,15 +32,13 @@ namespace TextureConverter void Init(); void Shutdown(); -void EncodeToRam(u32 address, bool bFromZBuffer, bool bIsIntensityFmt, - u32 copyfmt, int bScaleByHalf, const EFBRectangle& source); - void EncodeToRamYUYV(GLuint srcTexture, const TargetRectangle& sourceRc, u8* destAddr, int dstWidth, int dstHeight); void DecodeToTexture(u32 xfbAddr, int srcWidth, int srcHeight, GLuint destTexture); -u64 EncodeToRamFromTexture(u32 address, GLuint source_texture, bool bFromZBuffer, bool bIsIntensityFmt, u32 copyfmt, int bScaleByHalf, const EFBRectangle& source); +// returns size of the encoded data (in bytes) +int EncodeToRamFromTexture(u32 address, GLuint source_texture, bool bFromZBuffer, bool bIsIntensityFmt, u32 copyfmt, int bScaleByHalf, const EFBRectangle& source); } diff --git a/Source/Plugins/Plugin_VideoOGL/Src/VertexManager.cpp b/Source/Plugins/Plugin_VideoOGL/Src/VertexManager.cpp index a101d6c2d6..5117f6ddb6 100644 --- a/Source/Plugins/Plugin_VideoOGL/Src/VertexManager.cpp +++ b/Source/Plugins/Plugin_VideoOGL/Src/VertexManager.cpp @@ -155,12 +155,13 @@ void VertexManager::vFlush() tex.texImage0[i&3].format, tex.texTlut[i&3].tmem_offset<<9, tex.texTlut[i&3].tlut_format, (tex.texMode0[i&3].min_filter & 3) && (tex.texMode0[i&3].min_filter != 8) && g_ActiveConfig.bUseNativeMips, - (tex.texMode1[i&3].max_lod >> 4)); + tex.texMode1[i&3].max_lod >> 4, + tex.texImage1[i&3].image_type); if (tentry) { // 0s are probably for no manual wrapping needed. - PixelShaderManager::SetTexDims(i, tentry->realW, tentry->realH, 0, 0); + PixelShaderManager::SetTexDims(i, tentry->native_width, tentry->native_height, tentry->virtual_width, tentry->virtual_height, 0, 0, API_OPENGL); if (g_ActiveConfig.iLog & CONF_SAVETEXTURES) { @@ -178,7 +179,7 @@ void VertexManager::vFlush() // set global constants VertexShaderManager::SetConstants(); - PixelShaderManager::SetConstants(); + PixelShaderManager::SetConstants(API_OPENGL); bool useDstAlpha = !g_ActiveConfig.bDstAlphaPass && bpmem.dstalpha.enable && bpmem.blendmode.alphaupdate && bpmem.zcontrol.pixel_format == PIXELFMT_RGBA6_Z24; @@ -268,6 +269,8 @@ void VertexManager::vFlush() #endif g_Config.iSaveTargetId++; + ClearEFBCache(); + GL_REPORT_ERRORD(); } diff --git a/Source/Plugins/Plugin_VideoOGL/Src/VertexShaderCache.cpp b/Source/Plugins/Plugin_VideoOGL/Src/VertexShaderCache.cpp index 96f2d5a2ae..dbbb7ee29b 100644 --- a/Source/Plugins/Plugin_VideoOGL/Src/VertexShaderCache.cpp +++ b/Source/Plugins/Plugin_VideoOGL/Src/VertexShaderCache.cpp @@ -41,7 +41,9 @@ VertexShaderCache::VSCache VertexShaderCache::vshaders; GLuint VertexShaderCache::CurrentShader; bool VertexShaderCache::ShaderEnabled; -static VERTEXSHADER *pShaderLast = NULL; +VertexShaderCache::VSCacheEntry* VertexShaderCache::last_entry = NULL; +VERTEXSHADERUID VertexShaderCache::last_uid; + static int s_nMaxVertexInstructions; @@ -50,7 +52,7 @@ void VertexShaderCache::Init() glEnable(GL_VERTEX_PROGRAM_ARB); ShaderEnabled = true; CurrentShader = 0; - memset(&last_vertex_shader_uid, 0xFF, sizeof(last_vertex_shader_uid)); + 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; @@ -74,31 +76,34 @@ VERTEXSHADER* VertexShaderCache::SetShader(u32 components) { VERTEXSHADERUID uid; GetVertexShaderId(&uid, components); - if (uid == last_vertex_shader_uid && vshaders[uid].frameCount == frameCount) + if (last_entry) { - GFX_DEBUGGER_PAUSE_AT(NEXT_VERTEX_SHADER_CHANGE, true); - return pShaderLast; + 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; + } } - memcpy(&last_vertex_shader_uid, &uid, sizeof(VERTEXSHADERUID)); + + last_uid = uid; VSCache::iterator iter = vshaders.find(uid); if (iter != vshaders.end()) { - iter->second.frameCount = frameCount; VSCacheEntry &entry = iter->second; - if (&entry.shader != pShaderLast) { - pShaderLast = &entry.shader; - } + last_entry = &entry; GFX_DEBUGGER_PAUSE_AT(NEXT_VERTEX_SHADER_CHANGE, true); - return pShaderLast; + ValidateVertexShaderIDs(API_OPENGL, entry.safe_uid, entry.shader.strprog, components); + return &last_entry->shader; } // Make an entry in the table VSCacheEntry& entry = vshaders[uid]; - entry.frameCount = frameCount; - pShaderLast = &entry.shader; + 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) { @@ -118,7 +123,7 @@ VERTEXSHADER* VertexShaderCache::SetShader(u32 components) INCSTAT(stats.numVertexShadersCreated); SETSTAT(stats.numVertexShadersAlive, vshaders.size()); GFX_DEBUGGER_PAUSE_AT(NEXT_VERTEX_SHADER_CHANGE, true); - return pShaderLast; + return &last_entry->shader; } bool VertexShaderCache::CompileVertexShader(VERTEXSHADER& vs, const char* pstrprogram) @@ -182,9 +187,8 @@ bool VertexShaderCache::CompileVertexShader(VERTEXSHADER& vs, const char* pstrpr cgDestroyProgram(tempprog); #endif -#if defined(_DEBUG) || defined(DEBUGFAST) - vs.strprog = pstrprogram; -#endif + if (g_ActiveConfig.bEnableShaderDebugging) + vs.strprog = pstrprogram; return true; } diff --git a/Source/Plugins/Plugin_VideoOGL/Src/VertexShaderCache.h b/Source/Plugins/Plugin_VideoOGL/Src/VertexShaderCache.h index e311fde77b..6f4cbe25c2 100644 --- a/Source/Plugins/Plugin_VideoOGL/Src/VertexShaderCache.h +++ b/Source/Plugins/Plugin_VideoOGL/Src/VertexShaderCache.h @@ -32,9 +32,7 @@ struct VERTEXSHADER VERTEXSHADER() : glprogid(0) {} GLuint glprogid; // opengl program id -#if defined(_DEBUG) || defined(DEBUGFAST) std::string strprog; -#endif }; class VertexShaderCache @@ -42,8 +40,8 @@ class VertexShaderCache struct VSCacheEntry { VERTEXSHADER shader; - int frameCount; - VSCacheEntry() : frameCount(0) {} + VERTEXSHADERUIDSAFE safe_uid; + VSCacheEntry() {} void Destroy() { // printf("Destroying vs %i\n", shader.glprogid); glDeleteProgramsARB(1, &shader.glprogid); @@ -55,6 +53,9 @@ class VertexShaderCache static VSCache vshaders; + static VSCacheEntry* last_entry; + static VERTEXSHADERUID last_uid; + static GLuint CurrentShader; static bool ShaderEnabled; diff --git a/Source/Plugins/Plugin_VideoOGL/Src/main.cpp b/Source/Plugins/Plugin_VideoOGL/Src/main.cpp index a25f9cd839..251512fe6b 100644 --- a/Source/Plugins/Plugin_VideoOGL/Src/main.cpp +++ b/Source/Plugins/Plugin_VideoOGL/Src/main.cpp @@ -155,6 +155,7 @@ void VideoBackend::ShowConfig(void *_hParent) bool VideoBackend::Initialize(void *&window_handle) { + InitializeShared(); InitBackendInfo(); frameCount = 0; |
