@@ -1358,13 +1358,36 @@ CGSH_OpenGL::BitmapPtr CGSH_OpenGL::FindOrCreateBitmap(const FramebufferPtr& fra
13581358 return sharedPtr;
13591359}
13601360
1361- CGSH_OpenGL::FramebufferPtr CGSH_OpenGL::FindFramebufferAtPtr (uint32 ptr, uint32 psm) const
1361+ CGSH_OpenGL::FramebufferPtr CGSH_OpenGL::FindSmallestFramebufferAtPtr (uint32 ptr, uint32 psm) const
13621362{
1363- auto framebufferIterator = std::find_if (m_framebuffers.begin (), m_framebuffers.end (),
1364- [ptr, psm](const FramebufferPtr& framebuffer) {
1365- return framebuffer->m_psm == psm && framebuffer->m_basePtr == ptr;
1366- });
1367- return (framebufferIterator != std::end (m_framebuffers)) ? *(framebufferIterator) : FramebufferPtr ();
1363+ uint32 smallestSize = 0 ;
1364+ FramebufferPtr framebufferPtr = nullptr ;
1365+
1366+ for (const auto & framebuffer : m_framebuffers)
1367+ {
1368+ if (framebuffer->m_psm != psm)
1369+ {
1370+ continue ;
1371+ }
1372+
1373+ // If its a exact match, we can be sure its the right framebuffer
1374+ if (framebuffer->m_basePtr == ptr)
1375+ {
1376+ return framebuffer;
1377+ }
1378+
1379+ if (framebuffer->m_basePtr <= ptr && (framebuffer->m_basePtr + 0x6200 ) >= ptr)
1380+ {
1381+ uint32 size = framebuffer->m_width * framebuffer->m_height ;
1382+ if (size < smallestSize || smallestSize == 0 )
1383+ {
1384+ smallestSize = size;
1385+ framebufferPtr = framebuffer;
1386+ }
1387+ }
1388+ }
1389+
1390+ return framebufferPtr;
13681391}
13691392
13701393// ///////////////////////////////////////////////////////////
@@ -1927,7 +1950,7 @@ void CGSH_OpenGL::SyncCLUT(const TEX0& tex0)
19271950 {
19281951 const uint32 ptr = tex0.GetCLUTPtr ();
19291952
1930- FramebufferPtr framebuffer = FindFramebufferAtPtr (ptr, PSMCT32);
1953+ FramebufferPtr framebuffer = FindSmallestFramebufferAtPtr (ptr, PSMCT32);
19311954 if (framebuffer)
19321955 {
19331956 WriteFramebufferToMemory (framebuffer, true );
0 commit comments