From ceabf90bdabd49b401b6f035ce5eb71178fc682a Mon Sep 17 00:00:00 2001 From: Jordan Woyak Date: Fri, 28 May 2010 23:14:16 +0000 Subject: [PATCH] Improve iterator usage. git-svn-id: https://dolphin-emu.googlecode.com/svn/trunk@5528 8ced0084-cf51-0410-be5f-012b33b47a6e --- Source/Core/Common/Src/LogManager.cpp | 23 +- Source/Core/Common/Src/SymbolDB.cpp | 6 +- Source/Core/Core/Src/PowerPC/PPCAnalyst.cpp | 6 +- Source/Core/Core/Src/PowerPC/PPCSymbolDB.cpp | 14 +- Source/Core/Core/Src/PowerPC/SignatureDB.cpp | 8 +- .../DebuggerWX/Src/CodeWindowFunctions.cpp | 2 +- Source/Core/DiscIO/Src/NANDContentLoader.cpp | 2 +- Source/Core/DolphinWX/Src/PatchAddEdit.cpp | 4 +- Source/Core/VideoCommon/Src/VideoCommon.h | 6 - .../Plugins/Plugin_DSP_LLE/Src/DSPSymbols.cpp | 2 +- .../Src/Debugger/DSPDebugWindow.cpp | 2 +- .../Src/FramebufferManager.cpp | 1044 ++++++++------- .../Plugin_VideoDX9/Src/PixelShaderCache.cpp | 2 +- .../Plugin_VideoDX9/Src/TextureCache.cpp | 6 +- .../Src/W32Util/DialogManager.cpp | 4 +- .../Src/W32Util/PropertySheet.cpp | 4 +- .../Plugins/Plugin_VideoOGL/Src/DLCache.cpp | 1124 ++++++++--------- .../Src/FramebufferManager.cpp | 12 +- .../Plugin_VideoOGL/Src/PixelShaderCache.cpp | 2 +- .../Plugin_VideoOGL/Src/TextureMngr.cpp | 10 +- .../Plugin_VideoOGL/Src/VertexShaderCache.cpp | 2 +- 21 files changed, 1132 insertions(+), 1153 deletions(-) diff --git a/Source/Core/Common/Src/LogManager.cpp b/Source/Core/Common/Src/LogManager.cpp index 5303dab13d..559de0b7f5 100644 --- a/Source/Core/Common/Src/LogManager.cpp +++ b/Source/Core/Common/Src/LogManager.cpp @@ -15,6 +15,8 @@ // Official SVN repository and contact information can be found at // http://code.google.com/p/dolphin-emu/ +#include + #include "LogManager.h" #include "ConsoleListener.h" #include "Timer.h" @@ -160,28 +162,19 @@ void LogContainer::addListener(LogListener *listener) { } void LogContainer::removeListener(LogListener *listener) { - std::vector::iterator i; - for(i = listeners.begin(); i != listeners.end(); i++) { - if ((*i) == listener) { - listeners.erase(i); - break; - } - } + std::vector::iterator i = std::find(listeners.begin(), listeners.end(), listener); + if (listeners.end() != i) + listeners.erase(i); } bool LogContainer::isListener(LogListener *listener) const { - std::vector::const_iterator i; - for (i = listeners.begin(); i != listeners.end(); i++) { - if ((*i) == listener) { - return true; - } - } - return false; + std::vector::const_iterator i = std::find(listeners.begin(), listeners.end(), listener); + return listeners.end() != i; } void LogContainer::trigger(LogTypes::LOG_LEVELS level, const char *msg) { std::vector::const_iterator i; - for (i = listeners.begin(); i != listeners.end(); i++) { + for (i = listeners.begin(); i != listeners.end(); ++i) { (*i)->Log(level, msg); } } diff --git a/Source/Core/Common/Src/SymbolDB.cpp b/Source/Core/Common/Src/SymbolDB.cpp index 2494e95e10..4fd814ea71 100644 --- a/Source/Core/Common/Src/SymbolDB.cpp +++ b/Source/Core/Common/Src/SymbolDB.cpp @@ -20,7 +20,7 @@ void SymbolDB::List() { - for (XFuncMap::iterator iter = functions.begin(); iter != functions.end(); iter++) + for (XFuncMap::iterator iter = functions.begin(); iter != functions.end(); ++iter) { DEBUG_LOG(HLE,"%s @ %08x: %i bytes (hash %08x) : %i calls", iter->second.name.c_str(), iter->second.address, iter->second.size, iter->second.hash,iter->second.numCalls); } @@ -37,7 +37,7 @@ void SymbolDB::Clear(const char *prefix) void SymbolDB::Index() { int i = 0; - for (XFuncMap::iterator iter = functions.begin(); iter != functions.end(); iter++) + for (XFuncMap::iterator iter = functions.begin(); iter != functions.end(); ++iter) { iter->second.index = i++; } @@ -45,7 +45,7 @@ void SymbolDB::Index() Symbol *SymbolDB::GetSymbolFromName(const char *name) { - for (XFuncMap::iterator iter = functions.begin(); iter != functions.end(); iter++) + for (XFuncMap::iterator iter = functions.begin(); iter != functions.end(); ++iter) { if (!strcmp(iter->second.name.c_str(), name)) return &iter->second; diff --git a/Source/Core/Core/Src/PowerPC/PPCAnalyst.cpp b/Source/Core/Core/Src/PowerPC/PPCAnalyst.cpp index 9f55b72202..0e5238c181 100644 --- a/Source/Core/Core/Src/PowerPC/PPCAnalyst.cpp +++ b/Source/Core/Core/Src/PowerPC/PPCAnalyst.cpp @@ -587,10 +587,10 @@ void FindFunctionsAfterBLR(PPCSymbolDB *func_db) { vector funcAddrs; - for (PPCSymbolDB::XFuncMap::iterator iter = func_db->GetIterator(); iter != func_db->End(); iter++) + for (PPCSymbolDB::XFuncMap::iterator iter = func_db->GetIterator(); iter != func_db->End(); ++iter) funcAddrs.push_back(iter->second.address + iter->second.size); - for (vector::iterator iter = funcAddrs.begin(); iter != funcAddrs.end(); iter++) + for (vector::iterator iter = funcAddrs.begin(); iter != funcAddrs.end(); ++iter) { u32 location = *iter; while (true) @@ -622,7 +622,7 @@ void FindFunctions(u32 startAddr, u32 endAddr, PPCSymbolDB *func_db) int numLeafs = 0, numNice = 0, numUnNice = 0; int numTimer = 0, numRFI = 0, numStraightLeaf = 0; int leafSize = 0, niceSize = 0, unniceSize = 0; - for (PPCSymbolDB::XFuncMap::iterator iter = func_db->GetIterator(); iter != func_db->End(); iter++) + for (PPCSymbolDB::XFuncMap::iterator iter = func_db->GetIterator(); iter != func_db->End(); ++iter) { if (iter->second.address == 4) { diff --git a/Source/Core/Core/Src/PowerPC/PPCSymbolDB.cpp b/Source/Core/Core/Src/PowerPC/PPCSymbolDB.cpp index e080140c5c..b22fc70425 100644 --- a/Source/Core/Core/Src/PowerPC/PPCSymbolDB.cpp +++ b/Source/Core/Core/Src/PowerPC/PPCSymbolDB.cpp @@ -103,7 +103,7 @@ Symbol *PPCSymbolDB::GetSymbolFromAddr(u32 addr) return &it->second; else { - for (XFuncMap::iterator iter = functions.begin(); iter != functions.end(); iter++) + for (XFuncMap::iterator iter = functions.begin(); iter != functions.end(); ++iter) { if (addr >= iter->second.address && addr < iter->second.address + iter->second.size) return &iter->second; @@ -123,12 +123,12 @@ const char *PPCSymbolDB::GetDescription(u32 addr) void PPCSymbolDB::FillInCallers() { - for (XFuncMap::iterator iter = functions.begin(); iter != functions.end(); iter++) + for (XFuncMap::iterator iter = functions.begin(); iter != functions.end(); ++iter) { iter->second.callers.clear(); } - for (XFuncMap::iterator iter = functions.begin(); iter != functions.end(); iter++) + for (XFuncMap::iterator iter = functions.begin(); iter != functions.end(); ++iter) { Symbol &f = iter->second; for (size_t i = 0; i < f.calls.size(); i++) @@ -158,7 +158,7 @@ void PPCSymbolDB::PrintCalls(u32 funcAddr) const { const Symbol &f = iter->second; INFO_LOG(HLE, "The function %s at %08x calls:", f.name.c_str(), f.address); - for (std::vector::const_iterator fiter = f.calls.begin(); fiter!=f.calls.end(); fiter++) + for (std::vector::const_iterator fiter = f.calls.begin(); fiter!=f.calls.end(); ++fiter) { XFuncMap::const_iterator n = functions.find(fiter->function); if (n != functions.end()) @@ -180,7 +180,7 @@ void PPCSymbolDB::PrintCallers(u32 funcAddr) const { const Symbol &f = iter->second; INFO_LOG(CONSOLE,"The function %s at %08x is called by:",f.name.c_str(),f.address); - for (std::vector::const_iterator fiter = f.callers.begin(); fiter != f.callers.end(); fiter++) + for (std::vector::const_iterator fiter = f.callers.begin(); fiter != f.callers.end(); ++fiter) { XFuncMap::const_iterator n = functions.find(fiter->function); if (n != functions.end()) @@ -306,7 +306,7 @@ bool PPCSymbolDB::SaveMap(const char *filename, bool WithCodes) const { fprintf(f,"%08x %08x %08x %i %s\n", rSymbol.address, rSymbol.size, rSymbol.address, 0, rSymbol.name.c_str()); - itr++; + ++itr; } // Save a code file @@ -315,7 +315,7 @@ bool PPCSymbolDB::SaveMap(const char *filename, bool WithCodes) const // Get the current and next address LastAddress = rSymbol.address; LastSymbolName = rSymbol.name; - itr++; + ++itr; /* To make nice straight lines we fill out the name with spaces, we also cut off all names longer than 25 letters */ diff --git a/Source/Core/Core/Src/PowerPC/SignatureDB.cpp b/Source/Core/Core/Src/PowerPC/SignatureDB.cpp index ea3c043cf6..c0b28efa56 100644 --- a/Source/Core/Core/Src/PowerPC/SignatureDB.cpp +++ b/Source/Core/Core/Src/PowerPC/SignatureDB.cpp @@ -68,7 +68,7 @@ bool SignatureDB::Save(const char *filename) } int fcount = (int)database.size(); fwrite(&fcount, 4, 1, f); - for (FuncDB::const_iterator iter = database.begin(); iter != database.end(); iter++) + for (FuncDB::const_iterator iter = database.begin(); iter != database.end(); ++iter) { FuncDesc temp; memset(&temp, 0, sizeof(temp)); @@ -99,7 +99,7 @@ u32 SignatureDB::Add(u32 startAddr, u32 size, const char *name) void SignatureDB::List() { - for (FuncDB::iterator iter = database.begin(); iter != database.end(); iter++) + for (FuncDB::iterator iter = database.begin(); iter != database.end(); ++iter) { INFO_LOG(HLE,"%s : %i bytes, hash = %08x",iter->second.name.c_str(), iter->second.size, iter->first); } @@ -113,7 +113,7 @@ void SignatureDB::Clear() void SignatureDB::Apply(PPCSymbolDB *symbol_db) { - for (FuncDB::const_iterator iter = database.begin(); iter != database.end(); iter++) + for (FuncDB::const_iterator iter = database.begin(); iter != database.end(); ++iter) { u32 hash = iter->first; Symbol *function = symbol_db->GetSymbolFromHash(hash); @@ -138,7 +138,7 @@ void SignatureDB::Apply(PPCSymbolDB *symbol_db) void SignatureDB::Initialize(PPCSymbolDB *symbol_db, const char *prefix) { std::string prefix_str(prefix); - for (PPCSymbolDB::XFuncMap::const_iterator iter = symbol_db->GetConstIterator(); iter != symbol_db->End(); iter++) + for (PPCSymbolDB::XFuncMap::const_iterator iter = symbol_db->GetConstIterator(); iter != symbol_db->End(); ++iter) { if ((iter->second.name.substr(0, prefix_str.size()) == prefix_str) || prefix_str.empty()) { diff --git a/Source/Core/DebuggerWX/Src/CodeWindowFunctions.cpp b/Source/Core/DebuggerWX/Src/CodeWindowFunctions.cpp index cb0895dd89..f8c1310aee 100644 --- a/Source/Core/DebuggerWX/Src/CodeWindowFunctions.cpp +++ b/Source/Core/DebuggerWX/Src/CodeWindowFunctions.cpp @@ -383,7 +383,7 @@ void CCodeWindow::NotifyMapLoaded() //symbols->Show(false); // hide it for faster filling symbols->Freeze(); // HyperIris: wx style fast filling symbols->Clear(); - for (PPCSymbolDB::XFuncMap::iterator iter = g_symbolDB.GetIterator(); iter != g_symbolDB.End(); iter++) + for (PPCSymbolDB::XFuncMap::iterator iter = g_symbolDB.GetIterator(); iter != g_symbolDB.End(); ++iter) { int idx = symbols->Append(wxString::FromAscii(iter->second.name.c_str())); symbols->SetClientData(idx, (void*)&iter->second); diff --git a/Source/Core/DiscIO/Src/NANDContentLoader.cpp b/Source/Core/DiscIO/Src/NANDContentLoader.cpp index 4b29ef594a..3e7f6573ff 100644 --- a/Source/Core/DiscIO/Src/NANDContentLoader.cpp +++ b/Source/Core/DiscIO/Src/NANDContentLoader.cpp @@ -357,7 +357,7 @@ CNANDContentManager::~CNANDContentManager() while (itr != m_Map.end()) { delete itr->second; - itr++; + ++itr; } m_Map.clear(); } diff --git a/Source/Core/DolphinWX/Src/PatchAddEdit.cpp b/Source/Core/DolphinWX/Src/PatchAddEdit.cpp index 8ba0dff694..a8a769218a 100644 --- a/Source/Core/DolphinWX/Src/PatchAddEdit.cpp +++ b/Source/Core/DolphinWX/Src/PatchAddEdit.cpp @@ -155,7 +155,7 @@ void CPatchAddEdit::AddRemoveEntry(wxCommandEvent& event) SaveEntryData(itCurEntry); PatchEngine::PatchEntry peEmptyEntry(PatchEngine::PATCH_8BIT, 0x00000000, 0x00000000); - itCurEntry++; + ++itCurEntry; currentItem++; itCurEntry = tempEntries.insert(itCurEntry, peEmptyEntry); @@ -172,7 +172,7 @@ void CPatchAddEdit::AddRemoveEntry(wxCommandEvent& event) if (itCurEntry != tempEntries.begin()) { - itCurEntry--; + --itCurEntry; currentItem--; } else diff --git a/Source/Core/VideoCommon/Src/VideoCommon.h b/Source/Core/VideoCommon/Src/VideoCommon.h index 8922d75b95..9e800f887e 100644 --- a/Source/Core/VideoCommon/Src/VideoCommon.h +++ b/Source/Core/VideoCommon/Src/VideoCommon.h @@ -142,12 +142,6 @@ struct TargetRectangle : public MathUtil::Rectangle #define LOG_VTX() -#ifdef _WIN32 -#define ERASE_THROUGH_ITERATOR(container, iterator) iterator = container.erase(iterator) -#else -#define ERASE_THROUGH_ITERATOR(container, iterator) container.erase(iterator++) -#endif - bool IsD3D(); #endif // _VIDEOCOMMON_H diff --git a/Source/Plugins/Plugin_DSP_LLE/Src/DSPSymbols.cpp b/Source/Plugins/Plugin_DSP_LLE/Src/DSPSymbols.cpp index cd3770b1ca..e67c48ba98 100644 --- a/Source/Plugins/Plugin_DSP_LLE/Src/DSPSymbols.cpp +++ b/Source/Plugins/Plugin_DSP_LLE/Src/DSPSymbols.cpp @@ -75,7 +75,7 @@ Symbol *DSPSymbolDB::GetSymbolFromAddr(u32 addr) return &it->second; else { - for (XFuncMap::iterator iter = functions.begin(); iter != functions.end(); iter++) + for (XFuncMap::iterator iter = functions.begin(); iter != functions.end(); ++iter) { if (addr >= iter->second.address && addr < iter->second.address + iter->second.size) return &iter->second; diff --git a/Source/Plugins/Plugin_DSP_LLE/Src/Debugger/DSPDebugWindow.cpp b/Source/Plugins/Plugin_DSP_LLE/Src/Debugger/DSPDebugWindow.cpp index 962a0ec4b7..5c3d67d9c1 100644 --- a/Source/Plugins/Plugin_DSP_LLE/Src/Debugger/DSPDebugWindow.cpp +++ b/Source/Plugins/Plugin_DSP_LLE/Src/Debugger/DSPDebugWindow.cpp @@ -207,7 +207,7 @@ void DSPDebuggerLLE::UpdateSymbolMap() m_SymbolList->Freeze(); // HyperIris: wx style fast filling m_SymbolList->Clear(); for (SymbolDB::XFuncMap::iterator iter = DSPSymbols::g_dsp_symbol_db.GetIterator(); - iter != DSPSymbols::g_dsp_symbol_db.End(); iter++) + iter != DSPSymbols::g_dsp_symbol_db.End(); ++iter) { int idx = m_SymbolList->Append(wxString::FromAscii(iter->second.name.c_str())); m_SymbolList->SetClientData(idx, (void*)&iter->second); diff --git a/Source/Plugins/Plugin_VideoDX9/Src/FramebufferManager.cpp b/Source/Plugins/Plugin_VideoDX9/Src/FramebufferManager.cpp index bace776f7b..b182cf450e 100644 --- a/Source/Plugins/Plugin_VideoDX9/Src/FramebufferManager.cpp +++ b/Source/Plugins/Plugin_VideoDX9/Src/FramebufferManager.cpp @@ -1,524 +1,522 @@ -// 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 "D3DBase.h" -#include "Render.h" -#include "FramebufferManager.h" -#include "VideoConfig.h" -#include "PixelShaderCache.h" -#include "VertexShaderCache.h" -#include "TextureConverter.h" - -#undef CHECK -#define CHECK(hr,Message) if (FAILED(hr)) { PanicAlert(__FUNCTION__ " FAIL: %s" ,Message); } - -FramebufferManager FBManager; - -LPDIRECT3DSURFACE9 FramebufferManager::GetEFBColorRTSurface() -{ - return s_efb_color_surface; -} - -LPDIRECT3DSURFACE9 FramebufferManager::GetEFBDepthRTSurface() -{ - return s_efb_depth_surface; -} - -LPDIRECT3DSURFACE9 FramebufferManager::GetEFBColorOffScreenRTSurface() -{ - return s_efb_color_OffScreenReadBuffer; -} -LPDIRECT3DSURFACE9 FramebufferManager::GetEFBDepthOffScreenRTSurface() -{ - return s_efb_depth_OffScreenReadBuffer; -} - -LPDIRECT3DSURFACE9 FramebufferManager::GetEFBColorReadSurface() -{ - return s_efb_color_ReadBuffer; -} - -LPDIRECT3DSURFACE9 FramebufferManager::GetEFBDepthReadSurface() -{ - return s_efb_depth_ReadBuffer; -} - -D3DFORMAT FramebufferManager::GetEFBDepthRTSurfaceFormat(){return s_efb_depth_surface_Format;} -D3DFORMAT FramebufferManager::GetEFBDepthReadSurfaceFormat(){return s_efb_depth_ReadBuffer_Format;} -D3DFORMAT FramebufferManager::GetEFBColorRTSurfaceFormat(){return s_efb_color_surface_Format;} - - -LPDIRECT3DTEXTURE9 FramebufferManager::GetEFBColorTexture(const EFBRectangle& sourceRc) -{ - return s_efb_color_texture; -} - - -LPDIRECT3DTEXTURE9 FramebufferManager::GetEFBDepthTexture(const EFBRectangle &sourceRc) -{ - return s_efb_depth_texture; -} - - - -void FramebufferManager::Create() -{ - // Simplest possible setup to start with. - int target_width = Renderer::GetFullTargetWidth(); - int target_height = Renderer::GetFullTargetHeight(); - - s_efb_color_surface_Format = D3DFMT_A8R8G8B8; - //get the framebuffer texture - HRESULT hr = D3D::dev->CreateTexture(target_width, target_height, 1, D3DUSAGE_RENDERTARGET, s_efb_color_surface_Format, - D3DPOOL_DEFAULT, &s_efb_color_texture, NULL); - if(s_efb_color_texture) - { - hr = s_efb_color_texture->GetSurfaceLevel(0,&s_efb_color_surface); - } - CHECK(hr,"Create Color Texture"); - hr = D3D::dev->CreateTexture(1, 1, 1, D3DUSAGE_RENDERTARGET, s_efb_color_surface_Format, - D3DPOOL_DEFAULT, &s_efb_colorRead_texture, NULL); - CHECK(hr,"Create Color Read Texture"); - if(s_efb_colorRead_texture) - { - s_efb_colorRead_texture->GetSurfaceLevel(0,&s_efb_color_ReadBuffer); - } - //create an offscreen surface that we can lock to retrieve the data - hr = D3D::dev->CreateOffscreenPlainSurface(1, 1, s_efb_color_surface_Format, D3DPOOL_SYSTEMMEM, &s_efb_color_OffScreenReadBuffer, NULL ); - CHECK(hr,"Create Color offScreen Surface"); - - //Select Zbuffer format supported by hadware. - if (g_ActiveConfig.bEFBAccessEnable) - { - D3DFORMAT *DepthTexFormats = new D3DFORMAT[5]; - DepthTexFormats[0] = FOURCC_INTZ; - DepthTexFormats[1] = FOURCC_DF24; - DepthTexFormats[2] = FOURCC_RAWZ; - DepthTexFormats[3] = FOURCC_DF16; - DepthTexFormats[4] = D3DFMT_D24X8; - - for(int i = 0;i<5;i++) - { - s_efb_depth_surface_Format = DepthTexFormats[i]; - //get the framebuffer Depth texture - hr = D3D::dev->CreateTexture(target_width, target_height, 1, D3DUSAGE_DEPTHSTENCIL, s_efb_depth_surface_Format, - D3DPOOL_DEFAULT, &s_efb_depth_texture, NULL); - if (!FAILED(hr)) break; - } - CHECK(hr,"Depth Color Texture"); - //get the Surface - if(s_efb_depth_texture) - { - s_efb_depth_texture->GetSurfaceLevel(0,&s_efb_depth_surface); - } - //create a 4x4 pixel texture to work as a buffer for peeking - if(s_efb_depth_surface_Format == FOURCC_RAWZ || s_efb_depth_surface_Format == D3DFMT_D24X8) - { - DepthTexFormats[0] = D3DFMT_A8R8G8B8; - } - else - { - DepthTexFormats[0] = D3DFMT_R32F; - } - DepthTexFormats[1] = D3DFMT_A8R8G8B8; - - for(int i = 0;i<2;i++) - { - s_efb_depth_ReadBuffer_Format = DepthTexFormats[i]; - //get the framebuffer Depth texture - hr = D3D::dev->CreateTexture(4, 4, 1, D3DUSAGE_RENDERTARGET, s_efb_depth_ReadBuffer_Format, - D3DPOOL_DEFAULT, &s_efb_depthRead_texture, NULL); - if (!FAILED(hr)) break; - } - - CHECK(hr,"Create Depth Read texture"); - if(s_efb_depthRead_texture) - { - s_efb_depthRead_texture->GetSurfaceLevel(0,&s_efb_depth_ReadBuffer); - } - //create an offscreen surface that we can lock to retrieve the data - hr = D3D::dev->CreateOffscreenPlainSurface(4, 4, s_efb_depth_ReadBuffer_Format, D3DPOOL_SYSTEMMEM, &s_efb_depth_OffScreenReadBuffer, NULL ); - CHECK(hr,"Create Depth offScreen Surface"); - delete [] DepthTexFormats; - } - else - { - s_efb_depth_surface_Format = D3DFMT_D24X8; - hr = D3D::dev->CreateDepthStencilSurface(target_width, target_height, s_efb_depth_surface_Format, - D3DMULTISAMPLE_NONE, 0, FALSE, &s_efb_depth_surface, NULL); - CHECK(hr,"CreateDepthStencilSurface"); - } -} - -void FramebufferManager::Destroy() -{ - if (s_efb_depth_surface) - s_efb_depth_surface->Release(); - s_efb_depth_surface=NULL; - - if (s_efb_color_surface) - s_efb_color_surface->Release(); - s_efb_color_surface=NULL; - - if (s_efb_color_ReadBuffer) - s_efb_color_ReadBuffer->Release(); - s_efb_color_ReadBuffer=NULL; - - if (s_efb_depth_ReadBuffer) - s_efb_depth_ReadBuffer->Release(); - s_efb_depth_ReadBuffer=NULL; - - if (s_efb_color_OffScreenReadBuffer) - s_efb_color_OffScreenReadBuffer->Release(); - s_efb_color_OffScreenReadBuffer=NULL; - - if (s_efb_depth_OffScreenReadBuffer) - s_efb_depth_OffScreenReadBuffer->Release(); - s_efb_depth_OffScreenReadBuffer=NULL; - - if (s_efb_color_texture) - s_efb_color_texture->Release(); - s_efb_color_texture=NULL; - - if (s_efb_colorRead_texture) - s_efb_colorRead_texture->Release(); - s_efb_colorRead_texture=NULL; - - if (s_efb_depth_texture) - s_efb_depth_texture->Release(); - s_efb_depth_texture=NULL; - - if (s_efb_depthRead_texture) - s_efb_depthRead_texture->Release(); - s_efb_depthRead_texture=NULL; - - for (VirtualXFBListType::iterator it = m_virtualXFBList.begin(); it != m_virtualXFBList.end(); ++it) - { - if(it->xfbSource.texture) - it->xfbSource.texture->Release(); - } - m_virtualXFBList.clear(); - if(m_realXFBSource.texture) - m_realXFBSource.texture->Release(); - m_realXFBSource.texture = NULL; - -} - -void FramebufferManager::CopyToXFB(u32 xfbAddr, u32 fbWidth, u32 fbHeight, const EFBRectangle& sourceRc) -{ - if (g_ActiveConfig.bUseRealXFB) - copyToRealXFB(xfbAddr, fbWidth, fbHeight, sourceRc); - else - copyToVirtualXFB(xfbAddr, fbWidth, fbHeight, sourceRc); -} - -const XFBSource** FramebufferManager::GetXFBSource(u32 xfbAddr, u32 fbWidth, u32 fbHeight, u32 &xfbCount) -{ - if (g_ActiveConfig.bUseRealXFB) - return getRealXFBSource(xfbAddr, fbWidth, fbHeight, xfbCount); - else - return getVirtualXFBSource(xfbAddr, fbWidth, fbHeight, xfbCount); -} - -FramebufferManager::VirtualXFBListType::iterator FramebufferManager::findVirtualXFB(u32 xfbAddr, u32 width, u32 height) -{ - u32 srcLower = xfbAddr; - u32 srcUpper = xfbAddr + 2 * width * height; - - VirtualXFBListType::iterator it; - for (it = m_virtualXFBList.begin(); it != m_virtualXFBList.end(); ++it) - { - u32 dstLower = it->xfbAddr; - u32 dstUpper = it->xfbAddr + 2 * it->xfbWidth * it->xfbHeight; - - if (dstLower >= srcLower && dstUpper <= srcUpper) - return it; - } - - // That address is not in the Virtual XFB list. - return m_virtualXFBList.end(); -} - -void FramebufferManager::replaceVirtualXFB() -{ - VirtualXFBListType::iterator it = m_virtualXFBList.begin(); - - s32 srcLower = it->xfbAddr; - s32 srcUpper = it->xfbAddr + 2 * it->xfbWidth * it->xfbHeight; - s32 lineSize = 2 * it->xfbWidth; - - it++; - - while (it != m_virtualXFBList.end()) - { - s32 dstLower = it->xfbAddr; - s32 dstUpper = it->xfbAddr + 2 * it->xfbWidth * it->xfbHeight; - - if (dstLower >= srcLower && dstUpper <= srcUpper) - { - // invalidate the data - it->xfbAddr = 0; - it->xfbHeight = 0; - it->xfbWidth = 0; - } - else if (addrRangesOverlap(srcLower, srcUpper, dstLower, dstUpper)) - { - s32 upperOverlap = (srcUpper - dstLower) / lineSize; - s32 lowerOverlap = (dstUpper - srcLower) / lineSize; - - if (upperOverlap > 0 && lowerOverlap < 0) - { - it->xfbAddr += lineSize * upperOverlap; - it->xfbHeight -= upperOverlap; - } - else if (lowerOverlap > 0) - { - it->xfbHeight -= lowerOverlap; - } - } - - it++; - } -} - -void FramebufferManager::copyToRealXFB(u32 xfbAddr, u32 fbWidth, u32 fbHeight, const EFBRectangle& sourceRc) -{ - u8* xfb_in_ram = Memory_GetPtr(xfbAddr); - if (!xfb_in_ram) - { - WARN_LOG(VIDEO, "Tried to copy to invalid XFB address"); - return; - } - - TargetRectangle targetRc = Renderer::ConvertEFBRectangle(sourceRc); - TextureConverter::EncodeToRamYUYV(GetEFBColorTexture(sourceRc), targetRc, xfb_in_ram, fbWidth, fbHeight); -} - -void FramebufferManager::copyToVirtualXFB(u32 xfbAddr, u32 fbWidth, u32 fbHeight, const EFBRectangle& sourceRc) -{ - LPDIRECT3DTEXTURE9 xfbTexture; - HRESULT hr = 0; - - - VirtualXFBListType::iterator it = findVirtualXFB(xfbAddr, fbWidth, fbHeight); - - if (it == m_virtualXFBList.end() && (int)m_virtualXFBList.size() >= MAX_VIRTUAL_XFB) - { - // replace the last virtual XFB - it--; - } - - float SuperSampleCompensation = 1.0f; - float scaleX = Renderer::GetXFBScaleX(); - float scaleY = Renderer::GetXFBScaleY(); - if(g_ActiveConfig.iMultisampleMode > 0 && g_ActiveConfig.iMultisampleMode < 4) - { - switch (g_ActiveConfig.iMultisampleMode) - { - case 1: - break; - case 2: - SuperSampleCompensation = 0.5f; - break; - case 3: - SuperSampleCompensation = 1.0f/3.0f; - break; - default: - break; - }; - } - - scaleX *= SuperSampleCompensation ; - scaleY *= SuperSampleCompensation; - TargetRectangle targetSource,efbSource; - efbSource = Renderer::ConvertEFBRectangle(sourceRc); - targetSource.top = (sourceRc.top *scaleY); - targetSource.bottom = (sourceRc.bottom *scaleY); - targetSource.left = (sourceRc.left *scaleX); - targetSource.right = (sourceRc.right * scaleX); - int target_width = targetSource.right - targetSource.left; - int target_height = targetSource.bottom - targetSource.top; - if (it != m_virtualXFBList.end()) - { - // Overwrite an existing Virtual XFB. - - it->xfbAddr = xfbAddr; - it->xfbWidth = fbWidth; - it->xfbHeight = fbHeight; - - it->xfbSource.srcAddr = xfbAddr; - it->xfbSource.srcWidth = fbWidth; - it->xfbSource.srcHeight = fbHeight; - - if(it->xfbSource.texWidth != target_width || it->xfbSource.texHeight != target_height || !(it->xfbSource.texture)) - { - if(it->xfbSource.texture) - it->xfbSource.texture->Release(); - it->xfbSource.texture = NULL; - hr = D3D::dev->CreateTexture(target_width, target_height, 1, D3DUSAGE_RENDERTARGET, s_efb_color_surface_Format, - D3DPOOL_DEFAULT, &(it->xfbSource.texture), NULL); - - } - - xfbTexture = it->xfbSource.texture; - - it->xfbSource.texWidth = target_width; - it->xfbSource.texHeight = target_height; - - // Move this Virtual XFB to the front of the list. - m_virtualXFBList.splice(m_virtualXFBList.begin(), m_virtualXFBList, it); - - // Keep stale XFB data from being used - replaceVirtualXFB(); - } - else - { - // Create a new Virtual XFB and place it at the front of the list. - - D3D::dev->CreateTexture(target_width, target_height, 1, D3DUSAGE_RENDERTARGET, s_efb_color_surface_Format, - D3DPOOL_DEFAULT, &xfbTexture, NULL); - VirtualXFB newVirt; - - newVirt.xfbAddr = xfbAddr; - newVirt.xfbWidth = fbWidth; - newVirt.xfbHeight = fbHeight; - - newVirt.xfbSource.texture = xfbTexture; - newVirt.xfbSource.texWidth = target_width; - newVirt.xfbSource.texHeight = target_height; - - // Add the new Virtual XFB to the list - - if ((int)m_virtualXFBList.size() >= MAX_VIRTUAL_XFB) - { - // List overflowed; delete the oldest. - m_virtualXFBList.back().xfbSource.texture->Release(); - m_virtualXFBList.pop_back(); - } - - m_virtualXFBList.push_front(newVirt); - } - - // Copy EFB to XFB texture - - if(!xfbTexture) - return; - LPDIRECT3DTEXTURE9 read_texture = GetEFBColorTexture(sourceRc); - - Renderer::ResetAPIState(); // reset any game specific settings - LPDIRECT3DSURFACE9 Rendersurf = NULL; - - xfbTexture->GetSurfaceLevel(0,&Rendersurf); - D3D::dev->SetDepthStencilSurface(NULL); - D3D::dev->SetRenderTarget(0, Rendersurf); - - D3DVIEWPORT9 vp; - - vp.X = 0; - vp.Y = 0; - vp.Width = target_width; - vp.Height = target_height; - vp.MinZ = 0.0f; - vp.MaxZ = 1.0f; - D3D::dev->SetViewport(&vp); - RECT sourcerect; - sourcerect.bottom = efbSource.bottom; - sourcerect.left = efbSource.left; - sourcerect.right = efbSource.right; - sourcerect.top = efbSource.top; - - D3D::ChangeSamplerState(0, D3DSAMP_MINFILTER, D3DTEXF_LINEAR); - D3D::ChangeSamplerState(0, D3DSAMP_MAGFILTER, D3DTEXF_LINEAR); - - - int SSAAMode = ( g_ActiveConfig.iMultisampleMode > 3 )? 0 : g_ActiveConfig.iMultisampleMode; - D3D::drawShadedTexQuad( - read_texture, - &sourcerect, - Renderer::GetFullTargetWidth() , - Renderer::GetFullTargetHeight(), - target_width, - target_height, - PixelShaderCache::GetColorCopyProgram(SSAAMode), - (SSAAMode != 0)? VertexShaderCache::GetFSAAVertexShader() : VertexShaderCache::GetSimpleVertexShader()); - - - D3D::RefreshSamplerState(0, D3DSAMP_MINFILTER); - D3D::RefreshSamplerState(0, D3DSAMP_MAGFILTER); - D3D::SetTexture(0,NULL); - D3D::dev->SetRenderTarget(0, GetEFBColorRTSurface()); - D3D::dev->SetDepthStencilSurface(GetEFBDepthRTSurface()); - Renderer::RestoreAPIState(); - Rendersurf->Release(); - -} - -const XFBSource** FramebufferManager::getRealXFBSource(u32 xfbAddr, u32 fbWidth, u32 fbHeight, u32 &xfbCount) -{ - xfbCount = 1; - - m_realXFBSource.texWidth = fbWidth; - m_realXFBSource.texHeight = fbHeight; - - m_realXFBSource.srcAddr = xfbAddr; - m_realXFBSource.srcWidth = fbWidth; - m_realXFBSource.srcHeight = fbHeight; - - if (!m_realXFBSource.texture) - { - D3D::dev->CreateTexture(fbWidth, fbHeight, 1, D3DUSAGE_RENDERTARGET, s_efb_color_surface_Format, - D3DPOOL_DEFAULT, &m_realXFBSource.texture, NULL); - } - - // Decode YUYV data from GameCube RAM - TextureConverter::DecodeToTexture(xfbAddr, fbWidth, fbHeight, m_realXFBSource.texture); - - m_overlappingXFBArray[0] = &m_realXFBSource; - - return &m_overlappingXFBArray[0]; -} - -const XFBSource** FramebufferManager::getVirtualXFBSource(u32 xfbAddr, u32 fbWidth, u32 fbHeight, u32 &xfbCount) -{ - xfbCount = 0; - - if (m_virtualXFBList.size() == 0) - { - // No Virtual XFBs available. - return NULL; - } - - u32 srcLower = xfbAddr; - u32 srcUpper = xfbAddr + 2 * fbWidth * fbHeight; - - VirtualXFBListType::iterator it; - for (it = m_virtualXFBList.end(); it != m_virtualXFBList.begin();) - { - --it; - - u32 dstLower = it->xfbAddr; - u32 dstUpper = it->xfbAddr + 2 * it->xfbWidth * it->xfbHeight; - - if (addrRangesOverlap(srcLower, srcUpper, dstLower, dstUpper)) - { - m_overlappingXFBArray[xfbCount] = &(it->xfbSource); - xfbCount++; - } - } - - return &m_overlappingXFBArray[0]; +// 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 "D3DBase.h" +#include "Render.h" +#include "FramebufferManager.h" +#include "VideoConfig.h" +#include "PixelShaderCache.h" +#include "VertexShaderCache.h" +#include "TextureConverter.h" + +#undef CHECK +#define CHECK(hr,Message) if (FAILED(hr)) { PanicAlert(__FUNCTION__ " FAIL: %s" ,Message); } + +FramebufferManager FBManager; + +LPDIRECT3DSURFACE9 FramebufferManager::GetEFBColorRTSurface() +{ + return s_efb_color_surface; +} + +LPDIRECT3DSURFACE9 FramebufferManager::GetEFBDepthRTSurface() +{ + return s_efb_depth_surface; +} + +LPDIRECT3DSURFACE9 FramebufferManager::GetEFBColorOffScreenRTSurface() +{ + return s_efb_color_OffScreenReadBuffer; +} +LPDIRECT3DSURFACE9 FramebufferManager::GetEFBDepthOffScreenRTSurface() +{ + return s_efb_depth_OffScreenReadBuffer; +} + +LPDIRECT3DSURFACE9 FramebufferManager::GetEFBColorReadSurface() +{ + return s_efb_color_ReadBuffer; +} + +LPDIRECT3DSURFACE9 FramebufferManager::GetEFBDepthReadSurface() +{ + return s_efb_depth_ReadBuffer; +} + +D3DFORMAT FramebufferManager::GetEFBDepthRTSurfaceFormat(){return s_efb_depth_surface_Format;} +D3DFORMAT FramebufferManager::GetEFBDepthReadSurfaceFormat(){return s_efb_depth_ReadBuffer_Format;} +D3DFORMAT FramebufferManager::GetEFBColorRTSurfaceFormat(){return s_efb_color_surface_Format;} + + +LPDIRECT3DTEXTURE9 FramebufferManager::GetEFBColorTexture(const EFBRectangle& sourceRc) +{ + return s_efb_color_texture; +} + + +LPDIRECT3DTEXTURE9 FramebufferManager::GetEFBDepthTexture(const EFBRectangle &sourceRc) +{ + return s_efb_depth_texture; +} + + + +void FramebufferManager::Create() +{ + // Simplest possible setup to start with. + int target_width = Renderer::GetFullTargetWidth(); + int target_height = Renderer::GetFullTargetHeight(); + + s_efb_color_surface_Format = D3DFMT_A8R8G8B8; + //get the framebuffer texture + HRESULT hr = D3D::dev->CreateTexture(target_width, target_height, 1, D3DUSAGE_RENDERTARGET, s_efb_color_surface_Format, + D3DPOOL_DEFAULT, &s_efb_color_texture, NULL); + if(s_efb_color_texture) + { + hr = s_efb_color_texture->GetSurfaceLevel(0,&s_efb_color_surface); + } + CHECK(hr,"Create Color Texture"); + hr = D3D::dev->CreateTexture(1, 1, 1, D3DUSAGE_RENDERTARGET, s_efb_color_surface_Format, + D3DPOOL_DEFAULT, &s_efb_colorRead_texture, NULL); + CHECK(hr,"Create Color Read Texture"); + if(s_efb_colorRead_texture) + { + s_efb_colorRead_texture->GetSurfaceLevel(0,&s_efb_color_ReadBuffer); + } + //create an offscreen surface that we can lock to retrieve the data + hr = D3D::dev->CreateOffscreenPlainSurface(1, 1, s_efb_color_surface_Format, D3DPOOL_SYSTEMMEM, &s_efb_color_OffScreenReadBuffer, NULL ); + CHECK(hr,"Create Color offScreen Surface"); + + //Select Zbuffer format supported by hadware. + if (g_ActiveConfig.bEFBAccessEnable) + { + D3DFORMAT *DepthTexFormats = new D3DFORMAT[5]; + DepthTexFormats[0] = FOURCC_INTZ; + DepthTexFormats[1] = FOURCC_DF24; + DepthTexFormats[2] = FOURCC_RAWZ; + DepthTexFormats[3] = FOURCC_DF16; + DepthTexFormats[4] = D3DFMT_D24X8; + + for(int i = 0;i<5;i++) + { + s_efb_depth_surface_Format = DepthTexFormats[i]; + //get the framebuffer Depth texture + hr = D3D::dev->CreateTexture(target_width, target_height, 1, D3DUSAGE_DEPTHSTENCIL, s_efb_depth_surface_Format, + D3DPOOL_DEFAULT, &s_efb_depth_texture, NULL); + if (!FAILED(hr)) break; + } + CHECK(hr,"Depth Color Texture"); + //get the Surface + if(s_efb_depth_texture) + { + s_efb_depth_texture->GetSurfaceLevel(0,&s_efb_depth_surface); + } + //create a 4x4 pixel texture to work as a buffer for peeking + if(s_efb_depth_surface_Format == FOURCC_RAWZ || s_efb_depth_surface_Format == D3DFMT_D24X8) + { + DepthTexFormats[0] = D3DFMT_A8R8G8B8; + } + else + { + DepthTexFormats[0] = D3DFMT_R32F; + } + DepthTexFormats[1] = D3DFMT_A8R8G8B8; + + for(int i = 0;i<2;i++) + { + s_efb_depth_ReadBuffer_Format = DepthTexFormats[i]; + //get the framebuffer Depth texture + hr = D3D::dev->CreateTexture(4, 4, 1, D3DUSAGE_RENDERTARGET, s_efb_depth_ReadBuffer_Format, + D3DPOOL_DEFAULT, &s_efb_depthRead_texture, NULL); + if (!FAILED(hr)) break; + } + + CHECK(hr,"Create Depth Read texture"); + if(s_efb_depthRead_texture) + { + s_efb_depthRead_texture->GetSurfaceLevel(0,&s_efb_depth_ReadBuffer); + } + //create an offscreen surface that we can lock to retrieve the data + hr = D3D::dev->CreateOffscreenPlainSurface(4, 4, s_efb_depth_ReadBuffer_Format, D3DPOOL_SYSTEMMEM, &s_efb_depth_OffScreenReadBuffer, NULL ); + CHECK(hr,"Create Depth offScreen Surface"); + delete [] DepthTexFormats; + } + else + { + s_efb_depth_surface_Format = D3DFMT_D24X8; + hr = D3D::dev->CreateDepthStencilSurface(target_width, target_height, s_efb_depth_surface_Format, + D3DMULTISAMPLE_NONE, 0, FALSE, &s_efb_depth_surface, NULL); + CHECK(hr,"CreateDepthStencilSurface"); + } +} + +void FramebufferManager::Destroy() +{ + if (s_efb_depth_surface) + s_efb_depth_surface->Release(); + s_efb_depth_surface=NULL; + + if (s_efb_color_surface) + s_efb_color_surface->Release(); + s_efb_color_surface=NULL; + + if (s_efb_color_ReadBuffer) + s_efb_color_ReadBuffer->Release(); + s_efb_color_ReadBuffer=NULL; + + if (s_efb_depth_ReadBuffer) + s_efb_depth_ReadBuffer->Release(); + s_efb_depth_ReadBuffer=NULL; + + if (s_efb_color_OffScreenReadBuffer) + s_efb_color_OffScreenReadBuffer->Release(); + s_efb_color_OffScreenReadBuffer=NULL; + + if (s_efb_depth_OffScreenReadBuffer) + s_efb_depth_OffScreenReadBuffer->Release(); + s_efb_depth_OffScreenReadBuffer=NULL; + + if (s_efb_color_texture) + s_efb_color_texture->Release(); + s_efb_color_texture=NULL; + + if (s_efb_colorRead_texture) + s_efb_colorRead_texture->Release(); + s_efb_colorRead_texture=NULL; + + if (s_efb_depth_texture) + s_efb_depth_texture->Release(); + s_efb_depth_texture=NULL; + + if (s_efb_depthRead_texture) + s_efb_depthRead_texture->Release(); + s_efb_depthRead_texture=NULL; + + for (VirtualXFBListType::iterator it = m_virtualXFBList.begin(); it != m_virtualXFBList.end(); ++it) + { + if(it->xfbSource.texture) + it->xfbSource.texture->Release(); + } + m_virtualXFBList.clear(); + if(m_realXFBSource.texture) + m_realXFBSource.texture->Release(); + m_realXFBSource.texture = NULL; + +} + +void FramebufferManager::CopyToXFB(u32 xfbAddr, u32 fbWidth, u32 fbHeight, const EFBRectangle& sourceRc) +{ + if (g_ActiveConfig.bUseRealXFB) + copyToRealXFB(xfbAddr, fbWidth, fbHeight, sourceRc); + else + copyToVirtualXFB(xfbAddr, fbWidth, fbHeight, sourceRc); +} + +const XFBSource** FramebufferManager::GetXFBSource(u32 xfbAddr, u32 fbWidth, u32 fbHeight, u32 &xfbCount) +{ + if (g_ActiveConfig.bUseRealXFB) + return getRealXFBSource(xfbAddr, fbWidth, fbHeight, xfbCount); + else + return getVirtualXFBSource(xfbAddr, fbWidth, fbHeight, xfbCount); +} + +FramebufferManager::VirtualXFBListType::iterator FramebufferManager::findVirtualXFB(u32 xfbAddr, u32 width, u32 height) +{ + u32 srcLower = xfbAddr; + u32 srcUpper = xfbAddr + 2 * width * height; + + VirtualXFBListType::iterator it; + for (it = m_virtualXFBList.begin(); it != m_virtualXFBList.end(); ++it) + { + u32 dstLower = it->xfbAddr; + u32 dstUpper = it->xfbAddr + 2 * it->xfbWidth * it->xfbHeight; + + if (dstLower >= srcLower && dstUpper <= srcUpper) + return it; + } + + // That address is not in the Virtual XFB list. + return m_virtualXFBList.end(); +} + +void FramebufferManager::replaceVirtualXFB() +{ + VirtualXFBListType::iterator it = m_virtualXFBList.begin(); + + s32 srcLower = it->xfbAddr; + s32 srcUpper = it->xfbAddr + 2 * it->xfbWidth * it->xfbHeight; + s32 lineSize = 2 * it->xfbWidth; + + ++it; + + while (it != m_virtualXFBList.end()) + { + s32 dstLower = it->xfbAddr; + s32 dstUpper = it->xfbAddr + 2 * it->xfbWidth * it->xfbHeight; + + if (dstLower >= srcLower && dstUpper <= srcUpper) + { + // invalidate the data + it->xfbAddr = 0; + it->xfbHeight = 0; + it->xfbWidth = 0; + } + else if (addrRangesOverlap(srcLower, srcUpper, dstLower, dstUpper)) + { + s32 upperOverlap = (srcUpper - dstLower) / lineSize; + s32 lowerOverlap = (dstUpper - srcLower) / lineSize; + + if (upperOverlap > 0 && lowerOverlap < 0) + { + it->xfbAddr += lineSize * upperOverlap; + it->xfbHeight -= upperOverlap; + } + else if (lowerOverlap > 0) + { + it->xfbHeight -= lowerOverlap; + } + } + + ++it; + } +} + +void FramebufferManager::copyToRealXFB(u32 xfbAddr, u32 fbWidth, u32 fbHeight, const EFBRectangle& sourceRc) +{ + u8* xfb_in_ram = Memory_GetPtr(xfbAddr); + if (!xfb_in_ram) + { + WARN_LOG(VIDEO, "Tried to copy to invalid XFB address"); + return; + } + + TargetRectangle targetRc = Renderer::ConvertEFBRectangle(sourceRc); + TextureConverter::EncodeToRamYUYV(GetEFBColorTexture(sourceRc), targetRc, xfb_in_ram, fbWidth, fbHeight); +} + +void FramebufferManager::copyToVirtualXFB(u32 xfbAddr, u32 fbWidth, u32 fbHeight, const EFBRectangle& sourceRc) +{ + LPDIRECT3DTEXTURE9 xfbTexture; + HRESULT hr = 0; + + + VirtualXFBListType::iterator it = findVirtualXFB(xfbAddr, fbWidth, fbHeight); + + if (it == m_virtualXFBList.end() && (int)m_virtualXFBList.size() >= MAX_VIRTUAL_XFB) + { + // replace the last virtual XFB + --it; + } + + float SuperSampleCompensation = 1.0f; + float scaleX = Renderer::GetXFBScaleX(); + float scaleY = Renderer::GetXFBScaleY(); + if(g_ActiveConfig.iMultisampleMode > 0 && g_ActiveConfig.iMultisampleMode < 4) + { + switch (g_ActiveConfig.iMultisampleMode) + { + case 1: + break; + case 2: + SuperSampleCompensation = 0.5f; + break; + case 3: + SuperSampleCompensation = 1.0f/3.0f; + break; + default: + break; + }; + } + + scaleX *= SuperSampleCompensation ; + scaleY *= SuperSampleCompensation; + TargetRectangle targetSource,efbSource; + efbSource = Renderer::ConvertEFBRectangle(sourceRc); + targetSource.top = (sourceRc.top *scaleY); + targetSource.bottom = (sourceRc.bottom *scaleY); + targetSource.left = (sourceRc.left *scaleX); + targetSource.right = (sourceRc.right * scaleX); + int target_width = targetSource.right - targetSource.left; + int target_height = targetSource.bottom - targetSource.top; + if (it != m_virtualXFBList.end()) + { + // Overwrite an existing Virtual XFB. + + it->xfbAddr = xfbAddr; + it->xfbWidth = fbWidth; + it->xfbHeight = fbHeight; + + it->xfbSource.srcAddr = xfbAddr; + it->xfbSource.srcWidth = fbWidth; + it->xfbSource.srcHeight = fbHeight; + + if(it->xfbSource.texWidth != target_width || it->xfbSource.texHeight != target_height || !(it->xfbSource.texture)) + { + if(it->xfbSource.texture) + it->xfbSource.texture->Release(); + it->xfbSource.texture = NULL; + hr = D3D::dev->CreateTexture(target_width, target_height, 1, D3DUSAGE_RENDERTARGET, s_efb_color_surface_Format, + D3DPOOL_DEFAULT, &(it->xfbSource.texture), NULL); + + } + + xfbTexture = it->xfbSource.texture; + + it->xfbSource.texWidth = target_width; + it->xfbSource.texHeight = target_height; + + // Move this Virtual XFB to the front of the list. + m_virtualXFBList.splice(m_virtualXFBList.begin(), m_virtualXFBList, it); + + // Keep stale XFB data from being used + replaceVirtualXFB(); + } + else + { + // Create a new Virtual XFB and place it at the front of the list. + + D3D::dev->CreateTexture(target_width, target_height, 1, D3DUSAGE_RENDERTARGET, s_efb_color_surface_Format, + D3DPOOL_DEFAULT, &xfbTexture, NULL); + VirtualXFB newVirt; + + newVirt.xfbAddr = xfbAddr; + newVirt.xfbWidth = fbWidth; + newVirt.xfbHeight = fbHeight; + + newVirt.xfbSource.texture = xfbTexture; + newVirt.xfbSource.texWidth = target_width; + newVirt.xfbSource.texHeight = target_height; + + // Add the new Virtual XFB to the list + + if ((int)m_virtualXFBList.size() >= MAX_VIRTUAL_XFB) + { + // List overflowed; delete the oldest. + m_virtualXFBList.back().xfbSource.texture->Release(); + m_virtualXFBList.pop_back(); + } + + m_virtualXFBList.push_front(newVirt); + } + + // Copy EFB to XFB texture + + if(!xfbTexture) + return; + LPDIRECT3DTEXTURE9 read_texture = GetEFBColorTexture(sourceRc); + + Renderer::ResetAPIState(); // reset any game specific settings + LPDIRECT3DSURFACE9 Rendersurf = NULL; + + xfbTexture->GetSurfaceLevel(0,&Rendersurf); + D3D::dev->SetDepthStencilSurface(NULL); + D3D::dev->SetRenderTarget(0, Rendersurf); + + D3DVIEWPORT9 vp; + + vp.X = 0; + vp.Y = 0; + vp.Width = target_width; + vp.Height = target_height; + vp.MinZ = 0.0f; + vp.MaxZ = 1.0f; + D3D::dev->SetViewport(&vp); + RECT sourcerect; + sourcerect.bottom = efbSource.bottom; + sourcerect.left = efbSource.left; + sourcerect.right = efbSource.right; + sourcerect.top = efbSource.top; + + D3D::ChangeSamplerState(0, D3DSAMP_MINFILTER, D3DTEXF_LINEAR); + D3D::ChangeSamplerState(0, D3DSAMP_MAGFILTER, D3DTEXF_LINEAR); + + + int SSAAMode = ( g_ActiveConfig.iMultisampleMode > 3 )? 0 : g_ActiveConfig.iMultisampleMode; + D3D::drawShadedTexQuad( + read_texture, + &sourcerect, + Renderer::GetFullTargetWidth() , + Renderer::GetFullTargetHeight(), + target_width, + target_height, + PixelShaderCache::GetColorCopyProgram(SSAAMode), + (SSAAMode != 0)? VertexShaderCache::GetFSAAVertexShader() : VertexShaderCache::GetSimpleVertexShader()); + + + D3D::RefreshSamplerState(0, D3DSAMP_MINFILTER); + D3D::RefreshSamplerState(0, D3DSAMP_MAGFILTER); + D3D::SetTexture(0,NULL); + D3D::dev->SetRenderTarget(0, GetEFBColorRTSurface()); + D3D::dev->SetDepthStencilSurface(GetEFBDepthRTSurface()); + Renderer::RestoreAPIState(); + Rendersurf->Release(); + +} + +const XFBSource** FramebufferManager::getRealXFBSource(u32 xfbAddr, u32 fbWidth, u32 fbHeight, u32 &xfbCount) +{ + xfbCount = 1; + + m_realXFBSource.texWidth = fbWidth; + m_realXFBSource.texHeight = fbHeight; + + m_realXFBSource.srcAddr = xfbAddr; + m_realXFBSource.srcWidth = fbWidth; + m_realXFBSource.srcHeight = fbHeight; + + if (!m_realXFBSource.texture) + { + D3D::dev->CreateTexture(fbWidth, fbHeight, 1, D3DUSAGE_RENDERTARGET, s_efb_color_surface_Format, + D3DPOOL_DEFAULT, &m_realXFBSource.texture, NULL); + } + + // Decode YUYV data from GameCube RAM + TextureConverter::DecodeToTexture(xfbAddr, fbWidth, fbHeight, m_realXFBSource.texture); + + m_overlappingXFBArray[0] = &m_realXFBSource; + + return &m_overlappingXFBArray[0]; +} + +const XFBSource** FramebufferManager::getVirtualXFBSource(u32 xfbAddr, u32 fbWidth, u32 fbHeight, u32 &xfbCount) +{ + xfbCount = 0; + + if (m_virtualXFBList.size() == 0) + { + // No Virtual XFBs available. + return NULL; + } + + u32 srcLower = xfbAddr; + u32 srcUpper = xfbAddr + 2 * fbWidth * fbHeight; + + VirtualXFBListType::reverse_iterator it; + for (it = m_virtualXFBList.rbegin(); it != m_virtualXFBList.rend(); ++it) + { + u32 dstLower = it->xfbAddr; + u32 dstUpper = it->xfbAddr + 2 * it->xfbWidth * it->xfbHeight; + + if (addrRangesOverlap(srcLower, srcUpper, dstLower, dstUpper)) + { + m_overlappingXFBArray[xfbCount] = &(it->xfbSource); + xfbCount++; + } + } + + return &m_overlappingXFBArray[0]; } \ No newline at end of file diff --git a/Source/Plugins/Plugin_VideoDX9/Src/PixelShaderCache.cpp b/Source/Plugins/Plugin_VideoDX9/Src/PixelShaderCache.cpp index 5a8447260b..ec6a755bc3 100644 --- a/Source/Plugins/Plugin_VideoDX9/Src/PixelShaderCache.cpp +++ b/Source/Plugins/Plugin_VideoDX9/Src/PixelShaderCache.cpp @@ -314,7 +314,7 @@ void PixelShaderCache::Init() void PixelShaderCache::Clear() { PSCache::iterator iter = PixelShaders.begin(); - for (; iter != PixelShaders.end(); iter++) + for (; iter != PixelShaders.end(); ++iter) iter->second.Destroy(); PixelShaders.clear(); diff --git a/Source/Plugins/Plugin_VideoDX9/Src/TextureCache.cpp b/Source/Plugins/Plugin_VideoDX9/Src/TextureCache.cpp index 894293bcd2..4e9ae42a66 100644 --- a/Source/Plugins/Plugin_VideoDX9/Src/TextureCache.cpp +++ b/Source/Plugins/Plugin_VideoDX9/Src/TextureCache.cpp @@ -72,7 +72,7 @@ void TextureCache::Init() void TextureCache::Invalidate(bool shutdown) { - for (TexCache::iterator iter = textures.begin(); iter != textures.end(); iter++) + for (TexCache::iterator iter = textures.begin(); iter != textures.end(); ++iter) iter->second.Destroy(shutdown); textures.clear(); HiresTextures::Shutdown(); @@ -86,7 +86,7 @@ void TextureCache::InvalidateRange(u32 start_address, u32 size) if (iter->second.IntersectsMemoryRange(start_address, size)) { iter->second.Destroy(false); - ERASE_THROUGH_ITERATOR(textures, iter); + textures.erase(iter++); } else { ++iter; @@ -131,7 +131,7 @@ void TextureCache::Cleanup() } else { - iter++; + ++iter; } } } diff --git a/Source/Plugins/Plugin_VideoDX9/Src/W32Util/DialogManager.cpp b/Source/Plugins/Plugin_VideoDX9/Src/W32Util/DialogManager.cpp index 86c41d5cda..46be5c0dfa 100644 --- a/Source/Plugins/Plugin_VideoDX9/Src/W32Util/DialogManager.cpp +++ b/Source/Plugins/Plugin_VideoDX9/Src/W32Util/DialogManager.cpp @@ -13,7 +13,7 @@ void DialogManager::AddDlg(HWND hDialog) bool DialogManager::IsDialogMessage(LPMSG message) { WindowList::iterator iter; - for (iter=dialogs.begin(); iter!=dialogs.end(); iter++) + for (iter=dialogs.begin(); iter!=dialogs.end(); ++iter) { if (::IsDialogMessage(*iter,message)) return true; @@ -24,6 +24,6 @@ bool DialogManager::IsDialogMessage(LPMSG message) void DialogManager::EnableAll(BOOL enable) { WindowList::iterator iter; - for (iter=dialogs.begin(); iter!=dialogs.end(); iter++) + for (iter=dialogs.begin(); iter!=dialogs.end(); ++iter) EnableWindow(*iter,enable); } diff --git a/Source/Plugins/Plugin_VideoDX9/Src/W32Util/PropertySheet.cpp b/Source/Plugins/Plugin_VideoDX9/Src/W32Util/PropertySheet.cpp index 7ece37ccde..a3f493c123 100644 --- a/Source/Plugins/Plugin_VideoDX9/Src/W32Util/PropertySheet.cpp +++ b/Source/Plugins/Plugin_VideoDX9/Src/W32Util/PropertySheet.cpp @@ -70,7 +70,7 @@ namespace W32Util page.hInstance = hInstance; int i=0; - for (DlgList::iterator iter = list.begin(); iter != list.end(); iter++, i++) + for (DlgList::iterator iter = list.begin(); iter != list.end(); ++iter, ++i) { if (wizard) { @@ -146,7 +146,7 @@ namespace W32Util PropertySheet(&sheet); if (!floating) { - for (DlgList::iterator iter = list.begin(); iter != list.end(); iter++) + for (DlgList::iterator iter = list.begin(); iter != list.end(); ++iter) { delete iter->tab; } diff --git a/Source/Plugins/Plugin_VideoOGL/Src/DLCache.cpp b/Source/Plugins/Plugin_VideoOGL/Src/DLCache.cpp index 9ee0da3529..8b6b35dc4f 100644 --- a/Source/Plugins/Plugin_VideoOGL/Src/DLCache.cpp +++ b/Source/Plugins/Plugin_VideoOGL/Src/DLCache.cpp @@ -1,564 +1,560 @@ -// Copyright (C) 2003-2009 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/ - -// TODO: Handle cache-is-full condition :p - -#include - -#include "Common.h" -#include "VideoCommon.h" -#include "Hash.h" -#include "MemoryUtil.h" -#include "DataReader.h" -#include "Statistics.h" -#include "OpcodeDecoding.h" // For the GX_ constants. - -#include "XFMemory.h" -#include "CPMemory.h" -#include "BPMemory.h" - -#include "VertexManager.h" -#include "VertexLoaderManager.h" - -#include "x64Emitter.h" -#include "ABI.h" - -#include "DLCache.h" - -#define DL_CODE_CACHE_SIZE (1024*1024*16) -#define DL_STATIC_DATA_SIZE (1024*1024*4) -extern int frameCount; - -using namespace Gen; - -namespace DLCache -{ - -// Currently just recompiles the DLs themselves, doesn't bother with the vertex data. -// The speed boost is pretty small. The real big boost will come when we also store -// vertex arrays in the cached DLs. - -enum DisplayListPass { - DLPASS_ANALYZE, - DLPASS_COMPILE, - DLPASS_RUN, -}; - -struct VDataHashRegion -{ - u32 hash; - u32 start_address; - int size; -}; - -struct CachedDisplayList -{ - CachedDisplayList() - : uncachable(false), - pass(DLPASS_ANALYZE), - next_check(1) - { - frame_count = frameCount; - } - - bool uncachable; // if set, this DL will always be interpreted. This gets set if hash ever changes. - - int pass; - u32 dl_hash; - - int check; - int next_check; - - u32 vdata_hash; - - std::vector hash_regions; - - int frame_count; - - // ... Something containing cached vertex buffers here ... - - // Compile the commands themselves down to native code. - const u8 *compiled_code; -}; - -// We want to allow caching DLs that start at the same address but have different lengths, -// so the size has to be in the ID. -inline u64 CreateMapId(u32 address, u32 size) -{ - return ((u64)address << 32) | size; -} - -typedef std::map DLMap; - -static DLMap dl_map; -static u8 *dlcode_cache; -static u8 *static_data_buffer; -static u8 *static_data_ptr; - -static Gen::XEmitter emitter; - -// Everything gets free'd when the cache is cleared. -u8 *AllocStaticData(int size) -{ - u8 *cur_ptr = static_data_ptr; - static_data_ptr += (size + 3) & ~3; - return cur_ptr; -} - -// First pass - analyze -bool AnalyzeAndRunDisplayList(u32 address, int size, CachedDisplayList *dl) -{ - int num_xf_reg = 0; - int num_cp_reg = 0; - //int num_bp_reg = 0; // unused? - int num_index_xf = 0; - //int num_draw_call = 0; // unused? - - u8 *old_datareader = g_pVideoData; - g_pVideoData = Memory_GetPtr(address); - - u8 *end = g_pVideoData + size; - while (g_pVideoData < end) - { - // Yet another reimplementation of the DL reading... - int cmd_byte = DataReadU8(); - switch (cmd_byte) - { - case GX_NOP: - break; - - case GX_LOAD_CP_REG: //0x08 - { - // Execute - u8 sub_cmd = DataReadU8(); - u32 value = DataReadU32(); - LoadCPReg(sub_cmd, value); - INCSTAT(stats.thisFrame.numCPLoads); - - // Analyze - num_cp_reg++; - } - break; - - case GX_LOAD_XF_REG: - { - // Execute - u32 Cmd2 = DataReadU32(); - int transfer_size = ((Cmd2 >> 16) & 15) + 1; - u32 xf_address = Cmd2 & 0xFFFF; - // TODO - speed this up. pshufb? - u32 data_buffer[16]; - for (int i = 0; i < transfer_size; i++) - data_buffer[i] = DataReadU32(); - LoadXFReg(transfer_size, xf_address, data_buffer); - INCSTAT(stats.thisFrame.numXFLoads); - - // Analyze - num_xf_reg++; - } - break; - - case GX_LOAD_INDX_A: //used for position matrices - { - u32 value = DataReadU32(); - // Execute - LoadIndexedXF(value, 0xC); - // Analyze - num_index_xf++; - } - break; - case GX_LOAD_INDX_B: //used for normal matrices - { - u32 value = DataReadU32(); - // Execute - LoadIndexedXF(value, 0xD); - // Analyze - num_index_xf++; - } - break; - case GX_LOAD_INDX_C: //used for postmatrices - { - u32 value = DataReadU32(); - // Execute - LoadIndexedXF(value, 0xE); - // Analyze - num_index_xf++; - } - break; - case GX_LOAD_INDX_D: //used for lights - { - u32 value = DataReadU32(); - // Execute - LoadIndexedXF(value, 0xF); - // Analyze - num_index_xf++; - } - break; - - case GX_CMD_CALL_DL: - PanicAlert("Seeing DL call inside DL."); - break; - - case GX_CMD_UNKNOWN_METRICS: - // zelda 4 swords calls it and checks the metrics registers after that - break; - - case GX_CMD_INVL_VC:// Invalidate (vertex cache?) - DEBUG_LOG(VIDEO, "Invalidate (vertex cache?)"); - break; - - case GX_LOAD_BP_REG: //0x61 - { - u32 bp_cmd = DataReadU32(); - // Execute - LoadBPReg(bp_cmd); - INCSTAT(stats.thisFrame.numBPLoads); - - // Analyze - } - break; - - // draw primitives - default: - if (cmd_byte & 0x80) - { - // load vertices (use computed vertex size from FifoCommandRunnable above) - - // Execute - u16 numVertices = DataReadU16(); - - VertexLoaderManager::RunVertices( - cmd_byte & GX_VAT_MASK, // Vertex loader index (0 - 7) - (cmd_byte & GX_PRIMITIVE_MASK) >> GX_PRIMITIVE_SHIFT, - numVertices); - - // Analyze - } - else - { - ERROR_LOG(VIDEO, "DLCache::CompileAndRun: Illegal command %02x", cmd_byte); - break; - } - break; - } - } - - g_pVideoData = old_datareader; - return true; -} - -// The only sensible way to detect changes to vertex data is to convert several times -// and hash the output. - -// Second pass - compile -// Since some commands can affect the size of other commands, we really have no choice -// but to compile as we go, interpreting the list. We can't compile and then execute, we must -// compile AND execute at the same time. The second time the display list gets called, we already -// have the compiled code so we don't have to interpret anymore, we just run it. -bool CompileAndRunDisplayList(u32 address, int size, CachedDisplayList *dl) -{ - VertexManager::Flush(); - - u8 *old_datareader = g_pVideoData; - g_pVideoData = Memory_GetPtr(address); - - u8 *end = g_pVideoData + size; - - emitter.AlignCode4(); - dl->compiled_code = emitter.GetCodePtr(); - emitter.ABI_EmitPrologue(4); - - while (g_pVideoData < end) - { - // Yet another reimplementation of the DL reading... - int cmd_byte = DataReadU8(); - switch (cmd_byte) - { - case GX_NOP: - // Execute - // Compile - break; - - case GX_LOAD_CP_REG: //0x08 - { - // Execute - u8 sub_cmd = DataReadU8(); - u32 value = DataReadU32(); - LoadCPReg(sub_cmd, value); - INCSTAT(stats.thisFrame.numCPLoads); - - // Compile - emitter.ABI_CallFunctionCC((void *)&LoadCPReg, sub_cmd, value); - } - break; - - case GX_LOAD_XF_REG: - { - // Execute - u32 Cmd2 = DataReadU32(); - int transfer_size = ((Cmd2 >> 16) & 15) + 1; - u32 xf_address = Cmd2 & 0xFFFF; - // TODO - speed this up. pshufb? - u8 *real_data_buffer = AllocStaticData(4 * transfer_size); - u32 *data_buffer = (u32 *)real_data_buffer; - for (int i = 0; i < transfer_size; i++) - data_buffer[i] = DataReadU32(); - LoadXFReg(transfer_size, xf_address, data_buffer); - INCSTAT(stats.thisFrame.numXFLoads); - - // Compile - emitter.ABI_CallFunctionCCP((void *)&LoadXFReg, transfer_size, address, data_buffer); - } - break; - - case GX_LOAD_INDX_A: //used for position matrices - { - u32 value = DataReadU32(); - // Execute - LoadIndexedXF(value, 0xC); - // Compile - emitter.ABI_CallFunctionCC((void *)&LoadIndexedXF, value, 0xC); - } - break; - case GX_LOAD_INDX_B: //used for normal matrices - { - u32 value = DataReadU32(); - // Execute - LoadIndexedXF(value, 0xD); - // Compile - emitter.ABI_CallFunctionCC((void *)&LoadIndexedXF, value, 0xD); - } - break; - case GX_LOAD_INDX_C: //used for postmatrices - { - u32 value = DataReadU32(); - // Execute - LoadIndexedXF(value, 0xE); - // Compile - emitter.ABI_CallFunctionCC((void *)&LoadIndexedXF, value, 0xE); - } - break; - case GX_LOAD_INDX_D: //used for lights - { - u32 value = DataReadU32(); - // Execute - LoadIndexedXF(value, 0xF); - // Compile - emitter.ABI_CallFunctionCC((void *)&LoadIndexedXF, value, 0xF); - } - break; - - case GX_CMD_CALL_DL: - PanicAlert("Seeing DL call inside DL."); - break; - - case GX_CMD_UNKNOWN_METRICS: - // zelda 4 swords calls it and checks the metrics registers after that - break; - - case GX_CMD_INVL_VC:// Invalidate (vertex cache?) - DEBUG_LOG(VIDEO, "Invalidate (vertex cache?)"); - break; - - case GX_LOAD_BP_REG: //0x61 - { - u32 bp_cmd = DataReadU32(); - // Execute - LoadBPReg(bp_cmd); - INCSTAT(stats.thisFrame.numBPLoads); - // Compile - emitter.ABI_CallFunctionC((void *)&LoadBPReg, bp_cmd); - } - break; - - // draw primitives - default: - if (cmd_byte & 0x80) - { - // load vertices (use computed vertex size from FifoCommandRunnable above) - - // Execute - u16 numVertices = DataReadU16(); - - u64 pre_draw_video_data = (u64)g_pVideoData; - - VertexLoaderManager::RunVertices( - cmd_byte & GX_VAT_MASK, // Vertex loader index (0 - 7) - (cmd_byte & GX_PRIMITIVE_MASK) >> GX_PRIMITIVE_SHIFT, - numVertices); - - // Compile -#ifdef _M_X64 - emitter.MOV(64, R(RAX), Imm64(pre_draw_video_data)); - emitter.MOV(64, M(&g_pVideoData), R(RAX)); -#else - emitter.MOV(32, R(EAX), Imm32((u32)pre_draw_video_data)); - emitter.MOV(32, M(&g_pVideoData), R(EAX)); -#endif - emitter.ABI_CallFunctionCCC( - (void *)&VertexLoaderManager::RunVertices, - cmd_byte & GX_VAT_MASK, // Vertex loader index (0 - 7) - (cmd_byte & GX_PRIMITIVE_MASK) >> GX_PRIMITIVE_SHIFT, - numVertices); - } - else - { - ERROR_LOG(VIDEO, "DLCache::CompileAndRun: Illegal command %02x", cmd_byte); - break; - } - break; - } - } - - emitter.ABI_EmitEpilogue(4); - - g_pVideoData = old_datareader; - return true; -} - -// This one's pretty expensive. We should check if we can get away with only -// hashing the entire DL the first 3 frames or something. -u32 ComputeDLHash(u32 address, u32 size) -{ - u8 *ptr = Memory_GetPtr(address); - return HashFletcher(ptr, size & ~1); -} - -void Init() -{ - dlcode_cache = (u8 *)AllocateExecutableMemory(DL_CODE_CACHE_SIZE, false); // Don't need low memory. - static_data_buffer = (u8 *)AllocateMemoryPages(DL_STATIC_DATA_SIZE); - static_data_ptr = static_data_buffer; - emitter.SetCodePtr(dlcode_cache); -} - -void Shutdown() -{ - Clear(); - FreeMemoryPages(dlcode_cache, DL_CODE_CACHE_SIZE); - FreeMemoryPages(static_data_buffer, DL_STATIC_DATA_SIZE); - dlcode_cache = NULL; -} - -void Clear() -{ - dl_map.clear(); - - // Reset the cache pointers. - emitter.SetCodePtr(dlcode_cache); - static_data_ptr = static_data_buffer; -} - -void ProgressiveCleanup() -{ - DLMap::iterator iter = dl_map.begin(); - while (iter != dl_map.end()) { - CachedDisplayList &entry = iter->second; - int limit = iter->second.uncachable ? 1200 : 400; - if (entry.frame_count < frameCount - limit) { - // entry.Destroy(); -#ifdef _WIN32 - iter = dl_map.erase(iter); -#else - dl_map.erase(iter++); // (this is gcc standard!) -#endif - } - else - iter++; - } -} - -} // namespace - -// NOTE - outside the namespace on purpose. -bool HandleDisplayList(u32 address, u32 size) -{ - // Disable display list caching since the benefit isn't much to write home about - // right now... - return false; - - u64 dl_id = DLCache::CreateMapId(address, size); - DLCache::DLMap::iterator iter = DLCache::dl_map.find(dl_id); - - stats.numDListsAlive = (int)DLCache::dl_map.size(); - if (iter != DLCache::dl_map.end()) - { - DLCache::CachedDisplayList &dl = iter->second; - if (dl.uncachable) - { - // We haven't compiled it - let's return false so it gets - // interpreted. - return false; - } - - // Got one! And it's been compiled too, so let's run the compiled code! - switch (dl.pass) - { - case DLCache::DLPASS_ANALYZE: - PanicAlert("DLPASS_ANALYZE - should have been done the first pass"); - break; - case DLCache::DLPASS_COMPILE: - // First, check that the hash is the same as the last time. - if (dl.dl_hash != HashAdler32(Memory_GetPtr(address), size)) - { - // PanicAlert("uncachable %08x", address); - dl.uncachable = true; - return false; - } - DLCache::CompileAndRunDisplayList(address, size, &dl); - dl.pass = DLCache::DLPASS_RUN; - break; - case DLCache::DLPASS_RUN: - { - // Every N draws, check hash - dl.check--; - if (dl.check <= 0) - { - if (dl.dl_hash != HashAdler32(Memory_GetPtr(address), size)) - { - dl.uncachable = true; - return false; - } - dl.check = dl.next_check; - dl.next_check *= 2; - if (dl.next_check > 1024) - dl.next_check = 1024; - } - u8 *old_datareader = g_pVideoData; - ((void (*)())(void*)(dl.compiled_code))(); - g_pVideoData = old_datareader; - break; - } - } - return true; - } - - DLCache::CachedDisplayList dl; - - if (DLCache::AnalyzeAndRunDisplayList(address, size, &dl)) { - dl.dl_hash = HashAdler32(Memory_GetPtr(address), size); - dl.pass = DLCache::DLPASS_COMPILE; - dl.check = 1; - dl.next_check = 1; - DLCache::dl_map[dl_id] = dl; - return true; - } else { - dl.uncachable = true; - DLCache::dl_map[dl_id] = dl; - return true; // don't also interpret the list. - } -} +// Copyright (C) 2003-2009 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/ + +// TODO: Handle cache-is-full condition :p + +#include + +#include "Common.h" +#include "VideoCommon.h" +#include "Hash.h" +#include "MemoryUtil.h" +#include "DataReader.h" +#include "Statistics.h" +#include "OpcodeDecoding.h" // For the GX_ constants. + +#include "XFMemory.h" +#include "CPMemory.h" +#include "BPMemory.h" + +#include "VertexManager.h" +#include "VertexLoaderManager.h" + +#include "x64Emitter.h" +#include "ABI.h" + +#include "DLCache.h" + +#define DL_CODE_CACHE_SIZE (1024*1024*16) +#define DL_STATIC_DATA_SIZE (1024*1024*4) +extern int frameCount; + +using namespace Gen; + +namespace DLCache +{ + +// Currently just recompiles the DLs themselves, doesn't bother with the vertex data. +// The speed boost is pretty small. The real big boost will come when we also store +// vertex arrays in the cached DLs. + +enum DisplayListPass { + DLPASS_ANALYZE, + DLPASS_COMPILE, + DLPASS_RUN, +}; + +struct VDataHashRegion +{ + u32 hash; + u32 start_address; + int size; +}; + +struct CachedDisplayList +{ + CachedDisplayList() + : uncachable(false), + pass(DLPASS_ANALYZE), + next_check(1) + { + frame_count = frameCount; + } + + bool uncachable; // if set, this DL will always be interpreted. This gets set if hash ever changes. + + int pass; + u32 dl_hash; + + int check; + int next_check; + + u32 vdata_hash; + + std::vector hash_regions; + + int frame_count; + + // ... Something containing cached vertex buffers here ... + + // Compile the commands themselves down to native code. + const u8 *compiled_code; +}; + +// We want to allow caching DLs that start at the same address but have different lengths, +// so the size has to be in the ID. +inline u64 CreateMapId(u32 address, u32 size) +{ + return ((u64)address << 32) | size; +} + +typedef std::map DLMap; + +static DLMap dl_map; +static u8 *dlcode_cache; +static u8 *static_data_buffer; +static u8 *static_data_ptr; + +static Gen::XEmitter emitter; + +// Everything gets free'd when the cache is cleared. +u8 *AllocStaticData(int size) +{ + u8 *cur_ptr = static_data_ptr; + static_data_ptr += (size + 3) & ~3; + return cur_ptr; +} + +// First pass - analyze +bool AnalyzeAndRunDisplayList(u32 address, int size, CachedDisplayList *dl) +{ + int num_xf_reg = 0; + int num_cp_reg = 0; + //int num_bp_reg = 0; // unused? + int num_index_xf = 0; + //int num_draw_call = 0; // unused? + + u8 *old_datareader = g_pVideoData; + g_pVideoData = Memory_GetPtr(address); + + u8 *end = g_pVideoData + size; + while (g_pVideoData < end) + { + // Yet another reimplementation of the DL reading... + int cmd_byte = DataReadU8(); + switch (cmd_byte) + { + case GX_NOP: + break; + + case GX_LOAD_CP_REG: //0x08 + { + // Execute + u8 sub_cmd = DataReadU8(); + u32 value = DataReadU32(); + LoadCPReg(sub_cmd, value); + INCSTAT(stats.thisFrame.numCPLoads); + + // Analyze + num_cp_reg++; + } + break; + + case GX_LOAD_XF_REG: + { + // Execute + u32 Cmd2 = DataReadU32(); + int transfer_size = ((Cmd2 >> 16) & 15) + 1; + u32 xf_address = Cmd2 & 0xFFFF; + // TODO - speed this up. pshufb? + u32 data_buffer[16]; + for (int i = 0; i < transfer_size; i++) + data_buffer[i] = DataReadU32(); + LoadXFReg(transfer_size, xf_address, data_buffer); + INCSTAT(stats.thisFrame.numXFLoads); + + // Analyze + num_xf_reg++; + } + break; + + case GX_LOAD_INDX_A: //used for position matrices + { + u32 value = DataReadU32(); + // Execute + LoadIndexedXF(value, 0xC); + // Analyze + num_index_xf++; + } + break; + case GX_LOAD_INDX_B: //used for normal matrices + { + u32 value = DataReadU32(); + // Execute + LoadIndexedXF(value, 0xD); + // Analyze + num_index_xf++; + } + break; + case GX_LOAD_INDX_C: //used for postmatrices + { + u32 value = DataReadU32(); + // Execute + LoadIndexedXF(value, 0xE); + // Analyze + num_index_xf++; + } + break; + case GX_LOAD_INDX_D: //used for lights + { + u32 value = DataReadU32(); + // Execute + LoadIndexedXF(value, 0xF); + // Analyze + num_index_xf++; + } + break; + + case GX_CMD_CALL_DL: + PanicAlert("Seeing DL call inside DL."); + break; + + case GX_CMD_UNKNOWN_METRICS: + // zelda 4 swords calls it and checks the metrics registers after that + break; + + case GX_CMD_INVL_VC:// Invalidate (vertex cache?) + DEBUG_LOG(VIDEO, "Invalidate (vertex cache?)"); + break; + + case GX_LOAD_BP_REG: //0x61 + { + u32 bp_cmd = DataReadU32(); + // Execute + LoadBPReg(bp_cmd); + INCSTAT(stats.thisFrame.numBPLoads); + + // Analyze + } + break; + + // draw primitives + default: + if (cmd_byte & 0x80) + { + // load vertices (use computed vertex size from FifoCommandRunnable above) + + // Execute + u16 numVertices = DataReadU16(); + + VertexLoaderManager::RunVertices( + cmd_byte & GX_VAT_MASK, // Vertex loader index (0 - 7) + (cmd_byte & GX_PRIMITIVE_MASK) >> GX_PRIMITIVE_SHIFT, + numVertices); + + // Analyze + } + else + { + ERROR_LOG(VIDEO, "DLCache::CompileAndRun: Illegal command %02x", cmd_byte); + break; + } + break; + } + } + + g_pVideoData = old_datareader; + return true; +} + +// The only sensible way to detect changes to vertex data is to convert several times +// and hash the output. + +// Second pass - compile +// Since some commands can affect the size of other commands, we really have no choice +// but to compile as we go, interpreting the list. We can't compile and then execute, we must +// compile AND execute at the same time. The second time the display list gets called, we already +// have the compiled code so we don't have to interpret anymore, we just run it. +bool CompileAndRunDisplayList(u32 address, int size, CachedDisplayList *dl) +{ + VertexManager::Flush(); + + u8 *old_datareader = g_pVideoData; + g_pVideoData = Memory_GetPtr(address); + + u8 *end = g_pVideoData + size; + + emitter.AlignCode4(); + dl->compiled_code = emitter.GetCodePtr(); + emitter.ABI_EmitPrologue(4); + + while (g_pVideoData < end) + { + // Yet another reimplementation of the DL reading... + int cmd_byte = DataReadU8(); + switch (cmd_byte) + { + case GX_NOP: + // Execute + // Compile + break; + + case GX_LOAD_CP_REG: //0x08 + { + // Execute + u8 sub_cmd = DataReadU8(); + u32 value = DataReadU32(); + LoadCPReg(sub_cmd, value); + INCSTAT(stats.thisFrame.numCPLoads); + + // Compile + emitter.ABI_CallFunctionCC((void *)&LoadCPReg, sub_cmd, value); + } + break; + + case GX_LOAD_XF_REG: + { + // Execute + u32 Cmd2 = DataReadU32(); + int transfer_size = ((Cmd2 >> 16) & 15) + 1; + u32 xf_address = Cmd2 & 0xFFFF; + // TODO - speed this up. pshufb? + u8 *real_data_buffer = AllocStaticData(4 * transfer_size); + u32 *data_buffer = (u32 *)real_data_buffer; + for (int i = 0; i < transfer_size; i++) + data_buffer[i] = DataReadU32(); + LoadXFReg(transfer_size, xf_address, data_buffer); + INCSTAT(stats.thisFrame.numXFLoads); + + // Compile + emitter.ABI_CallFunctionCCP((void *)&LoadXFReg, transfer_size, address, data_buffer); + } + break; + + case GX_LOAD_INDX_A: //used for position matrices + { + u32 value = DataReadU32(); + // Execute + LoadIndexedXF(value, 0xC); + // Compile + emitter.ABI_CallFunctionCC((void *)&LoadIndexedXF, value, 0xC); + } + break; + case GX_LOAD_INDX_B: //used for normal matrices + { + u32 value = DataReadU32(); + // Execute + LoadIndexedXF(value, 0xD); + // Compile + emitter.ABI_CallFunctionCC((void *)&LoadIndexedXF, value, 0xD); + } + break; + case GX_LOAD_INDX_C: //used for postmatrices + { + u32 value = DataReadU32(); + // Execute + LoadIndexedXF(value, 0xE); + // Compile + emitter.ABI_CallFunctionCC((void *)&LoadIndexedXF, value, 0xE); + } + break; + case GX_LOAD_INDX_D: //used for lights + { + u32 value = DataReadU32(); + // Execute + LoadIndexedXF(value, 0xF); + // Compile + emitter.ABI_CallFunctionCC((void *)&LoadIndexedXF, value, 0xF); + } + break; + + case GX_CMD_CALL_DL: + PanicAlert("Seeing DL call inside DL."); + break; + + case GX_CMD_UNKNOWN_METRICS: + // zelda 4 swords calls it and checks the metrics registers after that + break; + + case GX_CMD_INVL_VC:// Invalidate (vertex cache?) + DEBUG_LOG(VIDEO, "Invalidate (vertex cache?)"); + break; + + case GX_LOAD_BP_REG: //0x61 + { + u32 bp_cmd = DataReadU32(); + // Execute + LoadBPReg(bp_cmd); + INCSTAT(stats.thisFrame.numBPLoads); + // Compile + emitter.ABI_CallFunctionC((void *)&LoadBPReg, bp_cmd); + } + break; + + // draw primitives + default: + if (cmd_byte & 0x80) + { + // load vertices (use computed vertex size from FifoCommandRunnable above) + + // Execute + u16 numVertices = DataReadU16(); + + u64 pre_draw_video_data = (u64)g_pVideoData; + + VertexLoaderManager::RunVertices( + cmd_byte & GX_VAT_MASK, // Vertex loader index (0 - 7) + (cmd_byte & GX_PRIMITIVE_MASK) >> GX_PRIMITIVE_SHIFT, + numVertices); + + // Compile +#ifdef _M_X64 + emitter.MOV(64, R(RAX), Imm64(pre_draw_video_data)); + emitter.MOV(64, M(&g_pVideoData), R(RAX)); +#else + emitter.MOV(32, R(EAX), Imm32((u32)pre_draw_video_data)); + emitter.MOV(32, M(&g_pVideoData), R(EAX)); +#endif + emitter.ABI_CallFunctionCCC( + (void *)&VertexLoaderManager::RunVertices, + cmd_byte & GX_VAT_MASK, // Vertex loader index (0 - 7) + (cmd_byte & GX_PRIMITIVE_MASK) >> GX_PRIMITIVE_SHIFT, + numVertices); + } + else + { + ERROR_LOG(VIDEO, "DLCache::CompileAndRun: Illegal command %02x", cmd_byte); + break; + } + break; + } + } + + emitter.ABI_EmitEpilogue(4); + + g_pVideoData = old_datareader; + return true; +} + +// This one's pretty expensive. We should check if we can get away with only +// hashing the entire DL the first 3 frames or something. +u32 ComputeDLHash(u32 address, u32 size) +{ + u8 *ptr = Memory_GetPtr(address); + return HashFletcher(ptr, size & ~1); +} + +void Init() +{ + dlcode_cache = (u8 *)AllocateExecutableMemory(DL_CODE_CACHE_SIZE, false); // Don't need low memory. + static_data_buffer = (u8 *)AllocateMemoryPages(DL_STATIC_DATA_SIZE); + static_data_ptr = static_data_buffer; + emitter.SetCodePtr(dlcode_cache); +} + +void Shutdown() +{ + Clear(); + FreeMemoryPages(dlcode_cache, DL_CODE_CACHE_SIZE); + FreeMemoryPages(static_data_buffer, DL_STATIC_DATA_SIZE); + dlcode_cache = NULL; +} + +void Clear() +{ + dl_map.clear(); + + // Reset the cache pointers. + emitter.SetCodePtr(dlcode_cache); + static_data_ptr = static_data_buffer; +} + +void ProgressiveCleanup() +{ + DLMap::iterator iter = dl_map.begin(); + while (iter != dl_map.end()) { + CachedDisplayList &entry = iter->second; + int limit = iter->second.uncachable ? 1200 : 400; + if (entry.frame_count < frameCount - limit) { + // entry.Destroy(); + dl_map.erase(iter++); // (this is gcc standard!) + } + else + ++iter; + } +} + +} // namespace + +// NOTE - outside the namespace on purpose. +bool HandleDisplayList(u32 address, u32 size) +{ + // Disable display list caching since the benefit isn't much to write home about + // right now... + return false; + + u64 dl_id = DLCache::CreateMapId(address, size); + DLCache::DLMap::iterator iter = DLCache::dl_map.find(dl_id); + + stats.numDListsAlive = (int)DLCache::dl_map.size(); + if (iter != DLCache::dl_map.end()) + { + DLCache::CachedDisplayList &dl = iter->second; + if (dl.uncachable) + { + // We haven't compiled it - let's return false so it gets + // interpreted. + return false; + } + + // Got one! And it's been compiled too, so let's run the compiled code! + switch (dl.pass) + { + case DLCache::DLPASS_ANALYZE: + PanicAlert("DLPASS_ANALYZE - should have been done the first pass"); + break; + case DLCache::DLPASS_COMPILE: + // First, check that the hash is the same as the last time. + if (dl.dl_hash != HashAdler32(Memory_GetPtr(address), size)) + { + // PanicAlert("uncachable %08x", address); + dl.uncachable = true; + return false; + } + DLCache::CompileAndRunDisplayList(address, size, &dl); + dl.pass = DLCache::DLPASS_RUN; + break; + case DLCache::DLPASS_RUN: + { + // Every N draws, check hash + dl.check--; + if (dl.check <= 0) + { + if (dl.dl_hash != HashAdler32(Memory_GetPtr(address), size)) + { + dl.uncachable = true; + return false; + } + dl.check = dl.next_check; + dl.next_check *= 2; + if (dl.next_check > 1024) + dl.next_check = 1024; + } + u8 *old_datareader = g_pVideoData; + ((void (*)())(void*)(dl.compiled_code))(); + g_pVideoData = old_datareader; + break; + } + } + return true; + } + + DLCache::CachedDisplayList dl; + + if (DLCache::AnalyzeAndRunDisplayList(address, size, &dl)) { + dl.dl_hash = HashAdler32(Memory_GetPtr(address), size); + dl.pass = DLCache::DLPASS_COMPILE; + dl.check = 1; + dl.next_check = 1; + DLCache::dl_map[dl_id] = dl; + return true; + } else { + dl.uncachable = true; + DLCache::dl_map[dl_id] = dl; + return true; // don't also interpret the list. + } +} diff --git a/Source/Plugins/Plugin_VideoOGL/Src/FramebufferManager.cpp b/Source/Plugins/Plugin_VideoOGL/Src/FramebufferManager.cpp index 35245c55ca..b671abfb2c 100644 --- a/Source/Plugins/Plugin_VideoOGL/Src/FramebufferManager.cpp +++ b/Source/Plugins/Plugin_VideoOGL/Src/FramebufferManager.cpp @@ -302,7 +302,7 @@ void FramebufferManager::replaceVirtualXFB() s32 srcUpper = it->xfbAddr + 2 * it->xfbWidth * it->xfbHeight; s32 lineSize = 2 * it->xfbWidth; - it++; + ++it; while (it != m_virtualXFBList.end()) { @@ -332,7 +332,7 @@ void FramebufferManager::replaceVirtualXFB() } } - it++; + ++it; } } @@ -357,7 +357,7 @@ void FramebufferManager::copyToVirtualXFB(u32 xfbAddr, u32 fbWidth, u32 fbHeight if (it == m_virtualXFBList.end() && (int)m_virtualXFBList.size() >= MAX_VIRTUAL_XFB) { // replace the last virtual XFB - it--; + --it; } if (it != m_virtualXFBList.end()) @@ -524,11 +524,9 @@ const XFBSource** FramebufferManager::getVirtualXFBSource(u32 xfbAddr, u32 fbWid u32 srcLower = xfbAddr; u32 srcUpper = xfbAddr + 2 * fbWidth * fbHeight; - VirtualXFBListType::iterator it; - for (it = m_virtualXFBList.end(); it != m_virtualXFBList.begin();) + VirtualXFBListType::reverse_iterator it; + for (it = m_virtualXFBList.rbegin(); it != m_virtualXFBList.rend(); ++it) { - --it; - u32 dstLower = it->xfbAddr; u32 dstUpper = it->xfbAddr + 2 * it->xfbWidth * it->xfbHeight; diff --git a/Source/Plugins/Plugin_VideoOGL/Src/PixelShaderCache.cpp b/Source/Plugins/Plugin_VideoOGL/Src/PixelShaderCache.cpp index 570c5bcf0f..2b9e090088 100644 --- a/Source/Plugins/Plugin_VideoOGL/Src/PixelShaderCache.cpp +++ b/Source/Plugins/Plugin_VideoOGL/Src/PixelShaderCache.cpp @@ -160,7 +160,7 @@ void PixelShaderCache::Shutdown() glDeleteProgramsARB(1, &s_DepthMatrixProgram); s_DepthMatrixProgram = 0; PSCache::iterator iter = pshaders.begin(); - for (; iter != pshaders.end(); iter++) + for (; iter != pshaders.end(); ++iter) iter->second.Destroy(); pshaders.clear(); } diff --git a/Source/Plugins/Plugin_VideoOGL/Src/TextureMngr.cpp b/Source/Plugins/Plugin_VideoOGL/Src/TextureMngr.cpp index c9d1f62f69..f614005623 100644 --- a/Source/Plugins/Plugin_VideoOGL/Src/TextureMngr.cpp +++ b/Source/Plugins/Plugin_VideoOGL/Src/TextureMngr.cpp @@ -199,15 +199,15 @@ void TextureMngr::ProgressiveCleanup() { if (!iter->second.isRenderTarget) { iter->second.Destroy(false); - ERASE_THROUGH_ITERATOR(textures, iter); + textures.erase(iter++); } else { iter->second.Destroy(false); - ERASE_THROUGH_ITERATOR(textures, iter); + textures.erase(iter++); } } else - iter++; + ++iter; } } @@ -219,7 +219,7 @@ void TextureMngr::InvalidateRange(u32 start_address, u32 size) if (iter->second.IntersectsMemoryRange(start_address, size)) { iter->second.Destroy(false); - ERASE_THROUGH_ITERATOR(textures, iter); + textures.erase(iter++); } else { ++iter; @@ -820,6 +820,6 @@ void TextureMngr::DisableStage(int stage) void TextureMngr::ClearRenderTargets() { - for (TexCache::iterator iter = textures.begin(); iter != textures.end(); iter++) + for (TexCache::iterator iter = textures.begin(); iter != textures.end(); ++iter) iter->second.isRenderTarget = false; } diff --git a/Source/Plugins/Plugin_VideoOGL/Src/VertexShaderCache.cpp b/Source/Plugins/Plugin_VideoOGL/Src/VertexShaderCache.cpp index c01ee7eb00..285a560567 100644 --- a/Source/Plugins/Plugin_VideoOGL/Src/VertexShaderCache.cpp +++ b/Source/Plugins/Plugin_VideoOGL/Src/VertexShaderCache.cpp @@ -116,7 +116,7 @@ void VertexShaderCache::Init() void VertexShaderCache::Shutdown() { - for (VSCache::iterator iter = vshaders.begin(); iter != vshaders.end(); iter++) + for (VSCache::iterator iter = vshaders.begin(); iter != vshaders.end(); ++iter) iter->second.Destroy(); vshaders.clear(); }