From d6e8c1f5d02db17bd5d84e52dea6a9c5c12d4237 Mon Sep 17 00:00:00 2001 From: donkopunchstania Date: Sun, 11 Jan 2009 22:25:57 +0000 Subject: [PATCH] added an option to copy textures from the EFB to system memory. this is slower but shouldn't cause the compatibility issues the previous method of copying them directly to a GL texture has. the new method can be tested by disabling "copy EFB to texture" in the hacks section. the new method still needs some tweaks. fixed some off-by-one errors in the EFB to texture copy and the scissor box. also fixed a couple asserts I was getting in the volume directory stuff. git-svn-id: https://dolphin-emu.googlecode.com/svn/trunk@1853 8ced0084-cf51-0410-be5f-012b33b47a6e --- Source/Core/DiscIO/Src/VolumeDirectory.cpp | 5 +- .../Plugin_VideoOGL/Plugin_VideoOGL.vcproj | 8 + .../Plugins/Plugin_VideoOGL/Src/BPStructs.cpp | 22 +- Source/Plugins/Plugin_VideoOGL/Src/Config.cpp | 10 +- Source/Plugins/Plugin_VideoOGL/Src/Config.h | 5 +- .../Plugin_VideoOGL/Src/GUI/ConfigDlg.cpp | 53 +- .../Plugin_VideoOGL/Src/GUI/ConfigDlg.h | 8 +- .../Plugins/Plugin_VideoOGL/Src/OS/Win32.cpp | 6 +- Source/Plugins/Plugin_VideoOGL/Src/Render.cpp | 6 +- .../Src/TextureConversionShader.cpp | 660 ++++++++++++++++++ .../Src/TextureConversionShader.h | 41 ++ .../Plugin_VideoOGL/Src/TextureConverter.cpp | 147 +++- .../Plugin_VideoOGL/Src/TextureConverter.h | 3 + .../Plugin_VideoOGL/Src/TextureMngr.cpp | 9 +- .../Plugins/Plugin_VideoOGL/Src/TextureMngr.h | 2 + 15 files changed, 928 insertions(+), 57 deletions(-) create mode 100644 Source/Plugins/Plugin_VideoOGL/Src/TextureConversionShader.cpp create mode 100644 Source/Plugins/Plugin_VideoOGL/Src/TextureConversionShader.h diff --git a/Source/Core/DiscIO/Src/VolumeDirectory.cpp b/Source/Core/DiscIO/Src/VolumeDirectory.cpp index ff7f883636..e30513adeb 100644 --- a/Source/Core/DiscIO/Src/VolumeDirectory.cpp +++ b/Source/Core/DiscIO/Src/VolumeDirectory.cpp @@ -332,7 +332,7 @@ void CVolumeDirectory::BuildFST() } // overflow check - _dbg_assert_(DVDINTERFACE, nameOffset == m_fstSize); + _dbg_assert_(DVDINTERFACE, nameOffset == m_totalNameSize); // write FST size and location _dbg_assert_(DVDINTERFACE, m_diskHeader); @@ -344,6 +344,9 @@ void CVolumeDirectory::BuildFST() void CVolumeDirectory::WriteToBuffer(u64 _SrcStartAddress, u64 _SrcLength, u8* _Src, u64& _Address, u64& _Length, u8*& _pBuffer) const { + if(_Length == 0) + return; + _dbg_assert_(DVDINTERFACE, _Address >= _SrcStartAddress); u64 srcOffset = _Address - _SrcStartAddress; diff --git a/Source/Plugins/Plugin_VideoOGL/Plugin_VideoOGL.vcproj b/Source/Plugins/Plugin_VideoOGL/Plugin_VideoOGL.vcproj index e1bae12743..165737a46e 100644 --- a/Source/Plugins/Plugin_VideoOGL/Plugin_VideoOGL.vcproj +++ b/Source/Plugins/Plugin_VideoOGL/Plugin_VideoOGL.vcproj @@ -1013,6 +1013,14 @@ RelativePath=".\Src\stdafx.h" > + + + + diff --git a/Source/Plugins/Plugin_VideoOGL/Src/BPStructs.cpp b/Source/Plugins/Plugin_VideoOGL/Src/BPStructs.cpp index aba8f05d0e..1bfca5b8d0 100644 --- a/Source/Plugins/Plugin_VideoOGL/Src/BPStructs.cpp +++ b/Source/Plugins/Plugin_VideoOGL/Src/BPStructs.cpp @@ -30,6 +30,7 @@ #include "OpcodeDecoding.h" #include "TextureMngr.h" #include "TextureDecoder.h" +#include "TextureConverter.h" #include "VertexShaderManager.h" #include "PixelShaderManager.h" #include "XFB.h" @@ -431,8 +432,8 @@ void BPWritten(int addr, int changes, int newval) TRectangle rc = { (int)(bpmem.copyTexSrcXY.x), (int)(bpmem.copyTexSrcXY.y), - (int)((bpmem.copyTexSrcXY.x + bpmem.copyTexSrcWH.x)), - (int)((bpmem.copyTexSrcXY.y + bpmem.copyTexSrcWH.y)) + (int)((bpmem.copyTexSrcXY.x + bpmem.copyTexSrcWH.x + 1)), + (int)((bpmem.copyTexSrcXY.y + bpmem.copyTexSrcWH.y + 1)) }; float MValueX = OpenGL_GetXmax(); float MValueY = OpenGL_GetYmax(); @@ -449,16 +450,21 @@ void BPWritten(int addr, int changes, int newval) PE_copy.Hex = bpmem.triggerEFBCopy; if (PE_copy.copy_to_xfb == 0) { - if(g_Config.bEFBToTextureDisable) { + // EFB to texture + // for some reason it sets bpmem.zcontrol.pixel_format to PIXELFMT_Z24 every time a zbuffer format is given as a dest to GXSetTexCopyDst + if (g_Config.bEFBCopyDisable) { glViewport(rc.left,rc.bottom,rc.right,rc.top); glScissor(rc.left,rc.bottom,rc.right,rc.top); } - else - // EFB to texture - // for some reason it sets bpmem.zcontrol.pixel_format to PIXELFMT_Z24 every time a zbuffer format is given as a dest to GXSetTexCopyDst - TextureMngr::CopyRenderTargetToTexture(bpmem.copyTexDest<<5, bpmem.zcontrol.pixel_format==PIXELFMT_Z24, PE_copy.intensity_fmt>0, + else if (g_Config.bEFBToTextureEnable) { + TextureMngr::CopyRenderTargetToTexture(bpmem.copyTexDest<<5, bpmem.zcontrol.pixel_format==PIXELFMT_Z24, PE_copy.intensity_fmt>0, (PE_copy.target_pixel_format/2)+((PE_copy.target_pixel_format&1)*8), PE_copy.half_scale>0, &rc); - } + } + else { + TextureConverter::EncodeToRam(bpmem.copyTexDest<<5, bpmem.zcontrol.pixel_format==PIXELFMT_Z24, PE_copy.intensity_fmt>0, + (PE_copy.target_pixel_format/2)+((PE_copy.target_pixel_format&1)*8), PE_copy.half_scale>0, rc); + } + } else { // EFB to XFB if (g_Config.bUseXFB) diff --git a/Source/Plugins/Plugin_VideoOGL/Src/Config.cpp b/Source/Plugins/Plugin_VideoOGL/Src/Config.cpp index b30f6b7a26..c393ed3349 100644 --- a/Source/Plugins/Plugin_VideoOGL/Src/Config.cpp +++ b/Source/Plugins/Plugin_VideoOGL/Src/Config.cpp @@ -78,10 +78,11 @@ void Config::Load() iniFile.Get("Enhancements", "ForceFiltering", &bForceFiltering, 0); iniFile.Get("Enhancements", "MaxAnisotropy", &iMaxAnisotropy, 3); // NOTE - this is x in (1 << x) - iniFile.Get("Hacks", "EFBToTextureDisable", &bEFBToTextureDisable, 0); - iniFile.Get("Hacks", "EFBToTextureDisableHotKey", &bEFBToTextureDisableHotKey, 0); + iniFile.Get("Hacks", "EFBCopyDisable", &bEFBCopyDisable, 0); + iniFile.Get("Hacks", "EFBCopyDisableHotKey", &bEFBCopyDisableHotKey, 0); iniFile.Get("Hacks", "ProjectionHax1", &bProjectionHax1, 0); iniFile.Get("Hacks", "ProjectionHax2", &bProjectionHax2, 0); + iniFile.Get("Hacks", "EFBToTextureEnable", &bEFBToTextureEnable, 1); } void Config::Save() @@ -116,10 +117,11 @@ void Config::Save() iniFile.Set("Enhancements", "ForceFiltering", bForceFiltering); iniFile.Set("Enhancements", "MaxAnisotropy", iMaxAnisotropy); - iniFile.Set("Hacks", "EFBToTextureDisable", bEFBToTextureDisable); - iniFile.Set("Hacks", "EFBToTextureDisableHotKey", bEFBToTextureDisableHotKey); + iniFile.Set("Hacks", "EFBCopyDisable", bEFBCopyDisable); + iniFile.Set("Hacks", "EFBCopyDisableHotKey", bEFBCopyDisableHotKey); iniFile.Set("Hacks", "ProjectionHax1", bProjectionHax1); iniFile.Set("Hacks", "ProjectionHax2", bProjectionHax2); + iniFile.Set("Hacks", "EFBToTextureEnable", bEFBToTextureEnable); iniFile.Save(FULL_CONFIG_DIR "gfx_opengl.ini"); } diff --git a/Source/Plugins/Plugin_VideoOGL/Src/Config.h b/Source/Plugins/Plugin_VideoOGL/Src/Config.h index 614d0f045f..68641ca8fd 100644 --- a/Source/Plugins/Plugin_VideoOGL/Src/Config.h +++ b/Source/Plugins/Plugin_VideoOGL/Src/Config.h @@ -64,10 +64,11 @@ struct Config bool bDumpTextures; // Hacks - bool bEFBToTextureDisable; - bool bEFBToTextureDisableHotKey; + bool bEFBCopyDisable; + bool bEFBCopyDisableHotKey; bool bProjectionHax1; bool bProjectionHax2; + bool bEFBToTextureEnable; int iLog; // CONF_ bits int iSaveTargetId; diff --git a/Source/Plugins/Plugin_VideoOGL/Src/GUI/ConfigDlg.cpp b/Source/Plugins/Plugin_VideoOGL/Src/GUI/ConfigDlg.cpp index 8af144aa34..12a594fa17 100644 --- a/Source/Plugins/Plugin_VideoOGL/Src/GUI/ConfigDlg.cpp +++ b/Source/Plugins/Plugin_VideoOGL/Src/GUI/ConfigDlg.cpp @@ -49,11 +49,12 @@ BEGIN_EVENT_TABLE(ConfigDialog,wxDialog) EVT_CHECKBOX(ID_DUMPTEXTURES, ConfigDialog::AdvancedSettingsChanged) EVT_CHECKBOX(ID_DISABLELIGHTING, ConfigDialog::AdvancedSettingsChanged) EVT_CHECKBOX(ID_DISABLETEXTURING, ConfigDialog::AdvancedSettingsChanged) - EVT_CHECKBOX(ID_EFBTOTEXTUREDISABLE, ConfigDialog::AdvancedSettingsChanged) - EVT_CHECKBOX(ID_EFBTOTEXTUREDISABLEHOTKEY, ConfigDialog::AdvancedSettingsChanged) + EVT_CHECKBOX(ID_EFBCOPYDISABLE, ConfigDialog::AdvancedSettingsChanged) + EVT_CHECKBOX(ID_EFBCOPYDISABLEHOTKEY, ConfigDialog::AdvancedSettingsChanged) EVT_CHECKBOX(ID_PROJECTIONHACK1,ConfigDialog::AdvancedSettingsChanged) EVT_CHECKBOX(ID_PROJECTIONHACK2,ConfigDialog::AdvancedSettingsChanged) EVT_CHECKBOX(ID_SAFETEXTURECACHE,ConfigDialog::AdvancedSettingsChanged) + EVT_CHECKBOX(ID_EFBTOTEXTUREENABLE, ConfigDialog::AdvancedSettingsChanged) EVT_DIRPICKER_CHANGED(ID_TEXTUREPATH, ConfigDialog::TexturePathChange) END_EVENT_TABLE() @@ -226,26 +227,31 @@ void ConfigDialog::CreateGUIControls() // Hacks sbHacks = new wxStaticBoxSizer(wxVERTICAL, m_PageAdvanced, wxT("Hacks")); - m_EFBToTextureDisable = new wxCheckBox(m_PageAdvanced, - ID_EFBTOTEXTUREDISABLE, wxT("Disable copy EFB to texture"), wxDefaultPosition, wxDefaultSize, 0, wxDefaultValidator); - m_EFBToTextureDisable->SetToolTip(wxT("Do not copy the Embedded Framebuffer (EFB)" - " to the\nTexture. This may result in a speed increase.")); - m_EFBToTextureDisable->Enable(true); - m_EFBToTextureDisable->SetValue(g_Config.bEFBToTextureDisable); - m_EFBToTextureDisableHotKey = new wxCheckBox(m_PageAdvanced, - ID_EFBTOTEXTUREDISABLEHOTKEY, wxT("With hotkey E"), wxDefaultPosition, wxDefaultSize, 0, wxDefaultValidator); - m_EFBToTextureDisableHotKey->SetToolTip(wxT("Use the E key to turn this option on and off")); + m_EFBCopyDisable = new wxCheckBox(m_PageAdvanced, + ID_EFBCOPYDISABLE, wxT("Disable copy EFB"), wxDefaultPosition, wxDefaultSize, 0, wxDefaultValidator); + m_EFBCopyDisable->SetToolTip(wxT("Do not copy the Embedded Framebuffer (EFB)." + " This may result in a speed increase.")); + m_EFBCopyDisable->Enable(true); + m_EFBCopyDisable->SetValue(g_Config.bEFBCopyDisable); + m_EFBCopyDisableHotKey = new wxCheckBox(m_PageAdvanced, + ID_EFBCOPYDISABLEHOTKEY, wxT("With hotkey E"), wxDefaultPosition, wxDefaultSize, 0, wxDefaultValidator); + m_EFBCopyDisableHotKey->SetToolTip(wxT("Use the E key to turn this option on and off")); #ifndef _WIN32 // JPeterson set the hot key to be Win32-specific - m_EFBToTextureDisableHotKey->Enable(false); + m_EFBCopyDisableHotKey->Enable(false); #endif - m_EFBToTextureDisableHotKey->SetValue(g_Config.bEFBToTextureDisableHotKey); + m_EFBCopyDisableHotKey->SetValue(g_Config.bEFBCopyDisableHotKey); m_SafeTextureCache = new wxCheckBox(m_PageAdvanced, ID_SAFETEXTURECACHE, wxT("Safe texture cache"), wxDefaultPosition, wxDefaultSize, 0, wxDefaultValidator); m_SafeTextureCache->SetToolTip(wxT("This is useful to prevent Metroid Prime from crashing, but can cause problems in other games.")); m_SafeTextureCache->Enable(true); m_SafeTextureCache->SetValue(g_Config.bSafeTextureCache); + m_EFBToTextureEnable = new wxCheckBox(m_PageAdvanced, ID_EFBTOTEXTUREENABLE, wxT("Copy EFB to texture"), wxDefaultPosition, wxDefaultSize, 0, wxDefaultValidator); + m_EFBToTextureEnable->SetToolTip(wxT("This is faster than copying the EFB to RAM, but may cause missing/corrupt textures or effects.")); + m_EFBToTextureEnable->Enable(true); + m_EFBToTextureEnable->SetValue(g_Config.bEFBToTextureEnable); + m_ProjectionHax1 = new wxCheckBox(m_PageAdvanced, ID_PROJECTIONHACK1, wxT("Projection before R945"), wxDefaultPosition, wxDefaultSize, 0, wxDefaultValidator); m_ProjectionHax1->SetToolTip(wxT("This may reveal otherwise invisible graphics" " in\ngames like Mario Galaxy or Ikaruga.")); @@ -281,11 +287,12 @@ void ConfigDialog::CreateGUIControls() sAdvanced->Add(sbUtilities, 0, wxEXPAND|wxALL, 5); sHacks = new wxGridBagSizer(0, 0); - sHacks->Add(m_EFBToTextureDisable, wxGBPosition(0, 0), wxGBSpan(1, 1), wxALL, 5); - sHacks->Add(m_EFBToTextureDisableHotKey, wxGBPosition(0, 1), wxGBSpan(1, 1), wxALL, 5); + sHacks->Add(m_EFBCopyDisable, wxGBPosition(0, 0), wxGBSpan(1, 1), wxALL, 5); + sHacks->Add(m_EFBCopyDisableHotKey, wxGBPosition(0, 1), wxGBSpan(1, 1), wxALL, 5); sHacks->Add(m_ProjectionHax1, wxGBPosition(1, 0), wxGBSpan(1, 1), wxALL, 5); sHacks->Add(m_ProjectionHax2, wxGBPosition(2, 0), wxGBSpan(1, 2), wxALL, 5); sHacks->Add(m_SafeTextureCache, wxGBPosition(3, 0), wxGBSpan(1, 1), wxALL, 5); + sHacks->Add(m_EFBToTextureEnable, wxGBPosition(1, 1), wxGBSpan(1, 1), wxALL, 5); sbHacks->Add(sHacks); sAdvanced->Add(sbHacks, 0, wxEXPAND|wxALL, 5); m_PageAdvanced->SetSizer(sAdvanced); @@ -421,11 +428,11 @@ void ConfigDialog::AdvancedSettingsChanged(wxCommandEvent& event) break; case ID_TEXTUREPATH: break; - case ID_EFBTOTEXTUREDISABLE: - g_Config.bEFBToTextureDisable = m_EFBToTextureDisable->IsChecked(); + case ID_EFBCOPYDISABLE: + g_Config.bEFBCopyDisable = m_EFBCopyDisable->IsChecked(); break; - case ID_EFBTOTEXTUREDISABLEHOTKEY: - g_Config.bEFBToTextureDisableHotKey = m_EFBToTextureDisableHotKey->IsChecked(); + case ID_EFBCOPYDISABLEHOTKEY: + g_Config.bEFBCopyDisableHotKey = m_EFBCopyDisableHotKey->IsChecked(); break; case ID_PROJECTIONHACK1: g_Config.bProjectionHax1 = m_ProjectionHax1->IsChecked(); @@ -436,6 +443,14 @@ void ConfigDialog::AdvancedSettingsChanged(wxCommandEvent& event) case ID_SAFETEXTURECACHE: g_Config.bSafeTextureCache = m_SafeTextureCache->IsChecked(); break; + case ID_EFBTOTEXTUREENABLE: + { + bool wasEnabled = g_Config.bEFBToTextureEnable; + g_Config.bEFBToTextureEnable = m_EFBToTextureEnable->IsChecked(); + if(wasEnabled && !g_Config.bEFBToTextureEnable) + TextureMngr::ClearRenderTargets(); + } + break; default: break; } diff --git a/Source/Plugins/Plugin_VideoOGL/Src/GUI/ConfigDlg.h b/Source/Plugins/Plugin_VideoOGL/Src/GUI/ConfigDlg.h index e061898ed3..eeaad8a8b7 100644 --- a/Source/Plugins/Plugin_VideoOGL/Src/GUI/ConfigDlg.h +++ b/Source/Plugins/Plugin_VideoOGL/Src/GUI/ConfigDlg.h @@ -97,10 +97,11 @@ class ConfigDialog : public wxDialog wxCheckBox *m_DisableTexturing; wxCheckBox *m_DumpTextures; wxDirPickerCtrl *m_TexturePath; - wxCheckBox *m_EFBToTextureDisable, *m_EFBToTextureDisableHotKey; + wxCheckBox *m_EFBCopyDisable, *m_EFBCopyDisableHotKey; wxCheckBox *m_ProjectionHax1; wxCheckBox *m_ProjectionHax2; wxCheckBox *m_SafeTextureCache; + wxCheckBox *m_EFBToTextureEnable; enum { @@ -143,9 +144,10 @@ class ConfigDialog : public wxDialog ID_DUMPTEXTURES, ID_TEXTUREPATH, - ID_EFBTOTEXTUREDISABLE, ID_EFBTOTEXTUREDISABLEHOTKEY, + ID_EFBCOPYDISABLE, ID_EFBCOPYDISABLEHOTKEY, ID_PROJECTIONHACK1, - ID_PROJECTIONHACK2 + ID_PROJECTIONHACK2, + ID_EFBTOTEXTUREENABLE }; void OnClose(wxCloseEvent& event); diff --git a/Source/Plugins/Plugin_VideoOGL/Src/OS/Win32.cpp b/Source/Plugins/Plugin_VideoOGL/Src/OS/Win32.cpp index 175b0e3d59..d9a0682454 100644 --- a/Source/Plugins/Plugin_VideoOGL/Src/OS/Win32.cpp +++ b/Source/Plugins/Plugin_VideoOGL/Src/OS/Win32.cpp @@ -173,11 +173,11 @@ namespace EmuWindow hypotheticalScene->sendMessage(KEYDOWN...); */ case 'E': // EFB hotkey - if(g_Config.bEFBToTextureDisableHotKey) + if(g_Config.bEFBCopyDisableHotKey) { - g_Config.bEFBToTextureDisable = !g_Config.bEFBToTextureDisable; + g_Config.bEFBCopyDisable = !g_Config.bEFBCopyDisable; Renderer::AddMessage(StringFromFormat("Copy EFB was turned %s", - g_Config.bEFBToTextureDisable ? "off" : "on").c_str(), 5000); + g_Config.bEFBCopyDisable ? "off" : "on").c_str(), 5000); } break; } diff --git a/Source/Plugins/Plugin_VideoOGL/Src/Render.cpp b/Source/Plugins/Plugin_VideoOGL/Src/Render.cpp index 06895fc1d2..1b1d9bf1ef 100644 --- a/Source/Plugins/Plugin_VideoOGL/Src/Render.cpp +++ b/Source/Plugins/Plugin_VideoOGL/Src/Render.cpp @@ -571,6 +571,8 @@ void Renderer::SetColorMask() // bpmem.scissorTL.x, y = 342x342 // bpmem.scissorBR.x, y = 981x821 // 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() { int xoff = bpmem.scissorOffset.x * 2 - 342; @@ -585,11 +587,11 @@ bool Renderer::SetScissorRect() rc_top *= MValueY; if (rc_top < 0) rc_top = 0; - float rc_right = bpmem.scissorBR.x - xoff - 342; // right = 640 + float rc_right = bpmem.scissorBR.x - xoff - 341; // right = 640 rc_right *= MValueX; if (rc_right > 640 * MValueX) rc_right = 640 * MValueX; - float rc_bottom = bpmem.scissorBR.y - yoff - 342; // bottom = 480 + float rc_bottom = bpmem.scissorBR.y - yoff - 341; // bottom = 480 rc_bottom *= MValueY; if (rc_bottom > 480 * MValueY) rc_bottom = 480 * MValueY; diff --git a/Source/Plugins/Plugin_VideoOGL/Src/TextureConversionShader.cpp b/Source/Plugins/Plugin_VideoOGL/Src/TextureConversionShader.cpp new file mode 100644 index 0000000000..b6424759b0 --- /dev/null +++ b/Source/Plugins/Plugin_VideoOGL/Src/TextureConversionShader.cpp @@ -0,0 +1,660 @@ +// Copyright (C) 2003-2008 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 "TextureConversionShader.h" +#include "TextureDecoder.h" +#include "PixelShaderManager.h" +#include "PixelShaderGen.h" +#include "BPMemory.h" + +#include +#include +#include "Common.h" + +#define WRITE p+=sprintf + +static char text[16384]; + +namespace TextureConversionShader +{ + +u16 GetBlockWidthInTexels(u32 format) +{ + switch (format) { + case GX_TF_I4: return 8; + case GX_TF_I8: return 8; + case GX_TF_IA4: return 8; + case GX_TF_IA8: return 4; + case GX_TF_RGB565: return 4; + case GX_TF_RGB5A3: return 4; + case GX_TF_RGBA8: return 4; + case GX_CTF_R4: return 8; + case GX_CTF_RA4: return 8; + case GX_CTF_RA8: return 4; + case GX_CTF_A8: return 8; + case GX_CTF_R8: return 8; + case GX_CTF_G8: return 8; + case GX_CTF_B8: return 8; + case GX_CTF_RG8: return 4; + case GX_CTF_GB8: return 4; + case GX_TF_Z8: return 8; + case GX_TF_Z16: return 4; + case GX_TF_Z24X8: return 4; + case GX_CTF_Z4: return 8; + case GX_CTF_Z8M: return 8; + case GX_CTF_Z8L: return 8; + case GX_CTF_Z16L: return 4; + default: return 8; + } +} + +u16 GetBlockHeightInTexels(u32 format) +{ + switch (format) { + case GX_TF_I4: return 8; + case GX_TF_I8: return 4; + case GX_TF_IA4: return 4; + case GX_TF_IA8: return 4; + case GX_TF_RGB565: return 4; + case GX_TF_RGB5A3: return 4; + case GX_TF_RGBA8: return 4; + case GX_CTF_R4: return 8; + case GX_CTF_RA4: return 4; + case GX_CTF_RA8: return 4; + case GX_CTF_A8: return 4; + case GX_CTF_R8: return 4; + case GX_CTF_G8: return 4; + case GX_CTF_B8: return 4; + case GX_CTF_RG8: return 4; + case GX_CTF_GB8: return 4; + case GX_TF_Z8: return 4; + case GX_TF_Z16: return 4; + case GX_TF_Z24X8: return 4; + case GX_CTF_Z4: return 8; + case GX_CTF_Z8M: return 4; + case GX_CTF_Z8L: return 4; + case GX_CTF_Z16L: return 4; + default: return 8; + } +} + +u16 GetEncodedSampleCount(u32 format) +{ + switch (format) { + case GX_TF_I4: return 8; + case GX_TF_I8: return 4; + case GX_TF_IA4: return 4; + case GX_TF_IA8: return 2; + case GX_TF_RGB565: return 2; + case GX_TF_RGB5A3: return 2; + case GX_TF_RGBA8: return 1; + case GX_CTF_R4: return 8; + case GX_CTF_RA4: return 4; + case GX_CTF_RA8: return 2; + case GX_CTF_A8: return 4; + case GX_CTF_R8: return 4; + case GX_CTF_G8: return 4; + case GX_CTF_B8: return 4; + case GX_CTF_RG8: return 2; + case GX_CTF_GB8: return 2; + case GX_TF_Z8: return 4; + case GX_TF_Z16: return 2; + case GX_TF_Z24X8: return 1; + case GX_CTF_Z4: return 8; + case GX_CTF_Z8M: return 4; + case GX_CTF_Z8L: return 4; + case GX_CTF_Z16L: return 2; + default: return 1; + } +} + +// block dimensions : block width, block height, samples, pixelStride +// texture dims : width, height, x offset, y offset +void WriteSwizzler(char*& p) +{ + WRITE(p, "uniform float4 blkDims : register(c%d);\n", C_COLORMATRIX); + WRITE(p, "uniform float4 textureDims : register(c%d);\n", C_COLORMATRIX + 1); + + WRITE(p, + "uniform samplerRECT samp0 : register(s0);\n" + "void main(\n" + " out float4 ocol0 : COLOR0,\n" + " in float2 uv0 : TEXCOORD0)\n" + "{\n" + + " float2 uv1 = floor(uv0);\n" + " uv1.x = uv1.x * blkDims.z;\n" + + " float bw = blkDims.x;\n" + " float bh = blkDims.y;\n" + + " float xl = floor(uv1.x / bw);\n" + " float xib = uv1.x - (xl * bw);\n" + " float yl = floor(uv1.y / bh);\n" + " float yb = yl * bh;\n" + " float yoff = uv1.y - yb;\n" + " float xp = uv1.x + (yoff * textureDims.x);\n" + " float xel = floor(xp / bw);\n" + " float xb = floor(xel / bh);\n" + " float xoff = xel - (xb * bh);\n" + + " float2 sampleUv;\n" + " sampleUv.x = xib + (xb * bw);\n" + " sampleUv.y = yb + xoff;\n" + " sampleUv = sampleUv * blkDims.w;\n" + " sampleUv.y = textureDims.y - sampleUv.y;\n" + + " sampleUv.x = sampleUv.x + textureDims.z;\n" + " sampleUv.y = sampleUv.y + textureDims.w;\n"); +} + +void WriteSampleColor(char*& p, const char* colorComp, const char* dest) +{ + WRITE(p, " %s = texRECT(samp0, sampleUv).%s;\n", dest, colorComp); +} + +void WriteColorToIntensity(char*& p, const char* src, const char* dest) +{ + WRITE(p, " %s = (0.257f * %s.r) + (0.504f * %s.g) + (0.098f * %s.b) + 0.0625f;\n", dest, src, src, src); +} + +void WriteIncrementSampleX(char*& p) +{ + WRITE(p, " sampleUv.x = sampleUv.x + blkDims.w;\n"); +} + +void WriteToBitDepth(char*& p, u8 depth, const char* src, const char* dest) +{ + float result = pow(2.0f, depth) - 1.0f; + WRITE(p, " %s = floor(%s * %ff);\n", dest, src, result); +} + +void WriteI8Encoder(char* p) +{ + WriteSwizzler(p); + WRITE(p, " float3 texSample;\n"); + + WriteSampleColor(p, "rgb", "texSample"); + WriteColorToIntensity(p, "texSample", "ocol0.b"); + WriteIncrementSampleX(p); + + WriteSampleColor(p, "rgb", "texSample"); + WriteColorToIntensity(p, "texSample", "ocol0.g"); + WriteIncrementSampleX(p); + + WriteSampleColor(p, "rgb", "texSample"); + WriteColorToIntensity(p, "texSample", "ocol0.r"); + WriteIncrementSampleX(p); + + WriteSampleColor(p, "rgb", "texSample"); + WriteColorToIntensity(p, "texSample", "ocol0.a"); + + WRITE(p, "}\n"); +} + +void WriteI4Encoder(char* p) +{ + WriteSwizzler(p); + WRITE(p, " float3 texSample;\n"); + WRITE(p, " float4 color0;\n"); + WRITE(p, " float4 color1;\n"); + + WriteSampleColor(p, "rgb", "texSample"); + WriteColorToIntensity(p, "texSample", "color0.b"); + WriteIncrementSampleX(p); + + WriteSampleColor(p, "rgb", "texSample"); + WriteColorToIntensity(p, "texSample", "color1.b"); + WriteIncrementSampleX(p); + + WriteSampleColor(p, "rgb", "texSample"); + WriteColorToIntensity(p, "texSample", "color0.g"); + WriteIncrementSampleX(p); + + WriteSampleColor(p, "rgb", "texSample"); + WriteColorToIntensity(p, "texSample", "color1.g"); + WriteIncrementSampleX(p); + + WriteSampleColor(p, "rgb", "texSample"); + WriteColorToIntensity(p, "texSample", "color0.r"); + WriteIncrementSampleX(p); + + WriteSampleColor(p, "rgb", "texSample"); + WriteColorToIntensity(p, "texSample", "color1.r"); + WriteIncrementSampleX(p); + + WriteSampleColor(p, "rgb", "texSample"); + WriteColorToIntensity(p, "texSample", "color0.a"); + WriteIncrementSampleX(p); + + WriteSampleColor(p, "rgb", "texSample"); + WriteColorToIntensity(p, "texSample", "color1.a"); + + WriteToBitDepth(p, 4, "color0", "color0"); + WriteToBitDepth(p, 4, "color1", "color1"); + + WRITE(p, " ocol0 = (color0 * 16.0f + color1) / 255.0f;\n"); + WRITE(p, "}\n"); +} + +void WriteIA8Encoder(char* p) +{ + WriteSwizzler(p); + WRITE(p, " float4 texSample;\n"); + + WriteSampleColor(p, "rgba", "texSample"); + WRITE(p, " ocol0.b = texSample.a;\n"); + WriteColorToIntensity(p, "texSample", "ocol0.g"); + WriteIncrementSampleX(p); + + WriteSampleColor(p, "rgba", "texSample"); + WRITE(p, " ocol0.r = texSample.a;\n"); + WriteColorToIntensity(p, "texSample", "ocol0.a"); + + WRITE(p, "}\n"); +} + +void WriteIA4Encoder(char* p) +{ + WriteSwizzler(p); + WRITE(p, " float4 texSample;\n"); + WRITE(p, " float4 color0;\n"); + WRITE(p, " float4 color1;\n"); + + WriteSampleColor(p, "rgba", "texSample"); + WRITE(p, " color0.b = texSample.a;\n"); + WriteColorToIntensity(p, "texSample", "color1.b"); + WriteIncrementSampleX(p); + + WriteSampleColor(p, "rgba", "texSample"); + WRITE(p, " color0.g = texSample.a;\n"); + WriteColorToIntensity(p, "texSample", "color1.g"); + WriteIncrementSampleX(p); + + WriteSampleColor(p, "rgba", "texSample"); + WRITE(p, " color0.r = texSample.a;\n"); + WriteColorToIntensity(p, "texSample", "color1.r"); + WriteIncrementSampleX(p); + + WriteSampleColor(p, "rgba", "texSample"); + WRITE(p, " color0.a = texSample.a;\n"); + WriteColorToIntensity(p, "texSample", "color1.a"); + + WriteToBitDepth(p, 4, "color0", "color0"); + WriteToBitDepth(p, 4, "color1", "color1"); + + WRITE(p, " ocol0 = (color0 * 16.0f + color1) / 255.0f;\n"); + WRITE(p, "}\n"); +} + +void WriteRGB5X5Encoder(char* p, bool g6Bit) +{ + s32 gBits; + float rShift; + char* msbString; + if(g6Bit) + { + gBits = 6; + rShift = 8.0f; + msbString = ""; + } + else + { + gBits = 5; + rShift = 4.0f; + msbString = " + 128.0f"; + } + + WriteSwizzler(p); + + WRITE(p, " float3 texSample;\n"); + WRITE(p, " float gInt;\n"); + WRITE(p, " float gUpper;\n"); + WRITE(p, " float gLower;\n"); + + WriteSampleColor(p, "rgb", "texSample"); + WriteToBitDepth(p, gBits, "texSample.g", "gInt"); + WRITE(p, " gUpper = floor(gInt / 8.0f);\n"); + WRITE(p, " gLower = gInt - gUpper * 8.0f;\n"); + + WriteToBitDepth(p, 5, "texSample.r", "ocol0.b"); + WRITE(p, " ocol0.b = ocol0.b * %f + gUpper%s;\n", rShift, msbString); + WriteToBitDepth(p, 5, "texSample.b", "ocol0.g"); + WRITE(p, " ocol0.g = ocol0.g + gLower * 32.0f;\n"); + + WriteIncrementSampleX(p); + + WriteSampleColor(p, "rgb", "texSample"); + WriteToBitDepth(p, gBits, "texSample.g", "gInt"); + WRITE(p, " gUpper = floor(gInt / 8.0f);\n"); + WRITE(p, " gLower = gInt - gUpper * 8.0f;\n"); + + WriteToBitDepth(p, 5, "texSample.r", "ocol0.r"); + WRITE(p, " ocol0.r = ocol0.r * %f + gUpper%s;\n", rShift, msbString); + WriteToBitDepth(p, 5, "texSample.b", "ocol0.a"); + WRITE(p, " ocol0.a = ocol0.a + gLower * 32.0f;\n"); + + WRITE(p, " ocol0 = ocol0 / 255.0f;\n"); + WRITE(p, "}\n"); +} + +void WriteRGBA4443Encoder(char* p) +{ + WriteSwizzler(p); + + WRITE(p, " float4 texSample;\n"); + WRITE(p, " float4 color0;\n"); + WRITE(p, " float4 color1;\n"); + + WriteSampleColor(p, "rgba", "texSample"); + WriteToBitDepth(p, 3, "texSample.a", "color0.b"); + WriteToBitDepth(p, 4, "texSample.r", "color1.b"); + WriteToBitDepth(p, 4, "texSample.g", "color0.g"); + WriteToBitDepth(p, 4, "texSample.b", "color1.g"); + + WriteIncrementSampleX(p); + + WriteSampleColor(p, "rgba", "texSample"); + WriteToBitDepth(p, 3, "texSample.a", "color0.r"); + WriteToBitDepth(p, 4, "texSample.r", "color1.r"); + WriteToBitDepth(p, 4, "texSample.g", "color0.a"); + WriteToBitDepth(p, 4, "texSample.b", "color1.a"); + + WRITE(p, " ocol0 = (color0 * 16.0f + color1) / 255.0f;\n"); + WRITE(p, "}\n"); +} + +// block dimensions : block width, block height, samples, pixelStride +// texture dims : width, height, x offset, y offset +void WriteRGBA8Encoder(char* p, bool fromDepth) +{ + WRITE(p, "uniform float4 blkDims : register(c%d);\n", C_COLORMATRIX); + WRITE(p, "uniform float4 textureDims : register(c%d);\n", C_COLORMATRIX + 1); + + // Swizzling for RGBA8 format + WRITE(p, + "uniform samplerRECT samp0 : register(s0);\n" + "void main(\n" + " out float4 ocol0 : COLOR0,\n" + " in float2 uv0 : TEXCOORD0)\n" + "{\n" + " float2 uv1 = floor(uv0);\n" + + " float bw = blkDims.x;\n" + " float bh = blkDims.y;\n" + + " float yl = floor(uv1.y / bh);\n" + " float yb = yl * bh;\n" + " float yoff = uv1.y - yb;\n" + " float xp = uv1.x + (yoff * textureDims.x);\n" + " float xel = floor(xp / 2);\n" + " float xb = floor(xel / bh);\n" + " float xoff = xel - (xb * bh);\n" + + " float x2 = uv1.x * 2;\n" + " float xl = floor(x2 / bw);\n" + " float xib = x2 - (xl * bw);\n" + " float halfxb = floor(xb / 2);\n" + + " float2 sampleUv;\n" + " sampleUv.x = xib + (halfxb * bw);\n" + " sampleUv.y = yb + xoff;\n" + " sampleUv = sampleUv * blkDims.w;\n" + " sampleUv.y = textureDims.y - sampleUv.y;\n" + + " sampleUv.x = sampleUv.x + textureDims.z;\n" + " sampleUv.y = sampleUv.y + textureDims.w;\n"); + + WRITE(p, " float cl1 = xb - (halfxb * 2);\n"); + WRITE(p, " float cl0 = 1.0f - cl1;\n"); + + WRITE(p, " float4 texSample;\n"); + WRITE(p, " float4 color0;\n"); + WRITE(p, " float4 color1;\n"); + + WriteSampleColor(p, "rgba", "texSample"); + if(fromDepth) + WRITE(p, " color0.b = 1.0f;\n"); + else + WRITE(p, " color0.b = texSample.a;\n"); + WRITE(p, " color0.g = texSample.r;\n"); + WRITE(p, " color1.b = texSample.g;\n"); + WRITE(p, " color1.g = texSample.b;\n"); + + WriteIncrementSampleX(p); + + WriteSampleColor(p, "rgba", "texSample"); + if(fromDepth) + WRITE(p, " color0.r = 1.0f;\n"); + else + WRITE(p, " color0.r = texSample.a;\n"); + WRITE(p, " color0.a = texSample.r;\n"); + WRITE(p, " color1.r = texSample.g;\n"); + WRITE(p, " color1.a = texSample.b;\n"); + + WRITE(p, " ocol0 = (cl0 * color0) + (cl1 * color1);\n"); + + WRITE(p, "}\n"); +} + +void WriteC4Encoder(char* p, const char* comp) +{ + WriteSwizzler(p); + WRITE(p, " float4 color0;\n"); + WRITE(p, " float4 color1;\n"); + + WriteSampleColor(p, comp, "color0.b"); + WriteIncrementSampleX(p); + + WriteSampleColor(p, comp, "color1.b"); + WriteIncrementSampleX(p); + + WriteSampleColor(p, comp, "color0.g"); + WriteIncrementSampleX(p); + + WriteSampleColor(p, comp, "color1.g"); + WriteIncrementSampleX(p); + + WriteSampleColor(p, comp, "color0.r"); + WriteIncrementSampleX(p); + + WriteSampleColor(p, comp, "color1.r"); + WriteIncrementSampleX(p); + + WriteSampleColor(p, comp, "color0.a"); + WriteIncrementSampleX(p); + + WriteSampleColor(p, comp, "color1.a"); + + WriteToBitDepth(p, 4, "color0", "color0"); + WriteToBitDepth(p, 4, "color1", "color1"); + + WRITE(p, " ocol0 = (color0 * 16.0f + color1) / 255.0f;\n"); + WRITE(p, "}\n"); +} + +void WriteC8Encoder(char* p, const char* comp) +{ + WriteSwizzler(p); + + WriteSampleColor(p, comp, "ocol0.b"); + WriteIncrementSampleX(p); + + WriteSampleColor(p, comp, "ocol0.g"); + WriteIncrementSampleX(p); + + WriteSampleColor(p, comp, "ocol0.r"); + WriteIncrementSampleX(p); + + WriteSampleColor(p, comp, "ocol0.a"); + + WRITE(p, "}\n"); +} + +void WriteCC4Encoder(char* p, const char* comp) +{ + WriteSwizzler(p); + WRITE(p, " float2 texSample;\n"); + WRITE(p, " float4 color0;\n"); + WRITE(p, " float4 color1;\n"); + + WriteSampleColor(p, comp, "texSample"); + WRITE(p, " color0.b = texSample.x;\n"); + WRITE(p, " color1.b = texSample.y;\n"); + WriteIncrementSampleX(p); + + WriteSampleColor(p, comp, "texSample"); + WRITE(p, " color0.g = texSample.x;\n"); + WRITE(p, " color1.g = texSample.y;\n"); + WriteIncrementSampleX(p); + + WriteSampleColor(p, comp, "texSample"); + WRITE(p, " color0.r = texSample.x;\n"); + WRITE(p, " color1.r = texSample.y;\n"); + WriteIncrementSampleX(p); + + WriteSampleColor(p, comp, "texSample"); + WRITE(p, " color0.a = texSample.x;\n"); + WRITE(p, " color1.a = texSample.y;\n"); + + WriteToBitDepth(p, 4, "color0", "color0"); + WriteToBitDepth(p, 4, "color1", "color1"); + + WRITE(p, " ocol0 = (color0 * 16.0f + color1) / 255.0f;\n"); + WRITE(p, "}\n"); +} + +void WriteCC8Encoder(char* p, const char* comp) +{ + WriteSwizzler(p); + + WriteSampleColor(p, comp, "ocol0.bg"); + WriteIncrementSampleX(p); + + WriteSampleColor(p, comp, "ocol0.ra"); + + WRITE(p, "}\n"); +} + +char *GenerateEncodingShader(u32 format) +{ + text[sizeof(text) - 1] = 0x7C; // canary + + char *p = text; + + switch(format) + { + case GX_TF_I4: + WriteI4Encoder(p); + break; + case GX_TF_I8: + WriteI8Encoder(p); + break; + case GX_TF_IA4: + WriteIA4Encoder(p); + break; + case GX_TF_IA8: + WriteIA8Encoder(p); + break; + case GX_TF_RGB565: + WriteRGB5X5Encoder(p, true); + break; + case GX_TF_RGB5A3: + if(bpmem.zcontrol.pixel_format == PIXELFMT_RGBA6_Z24) + WriteRGBA4443Encoder(p); + else + WriteRGB5X5Encoder(p, false); + break; + case GX_TF_RGBA8: + WriteRGBA8Encoder(p, false); + break; + case GX_CTF_R4: + WriteC4Encoder(p, "r"); + break; + case GX_CTF_RA4: + WriteCC4Encoder(p, "ar"); + break; + case GX_CTF_RA8: + WriteCC8Encoder(p, "ar"); + break; + case GX_CTF_A8: + WriteC8Encoder(p, "a"); + break; + case GX_CTF_R8: + WriteC8Encoder(p, "r"); + break; + case GX_CTF_G8: + WriteC8Encoder(p, "g"); + break; + case GX_CTF_B8: + WriteC8Encoder(p, "b"); + break; + case GX_CTF_RG8: + WriteCC8Encoder(p, "rg"); + break; + case GX_CTF_GB8: + WriteCC8Encoder(p, "gb"); + break; + case GX_TF_Z8: + WriteC8Encoder(p, "b"); + break; + case GX_TF_Z16: + // byte order is reversed + WriteCC8Encoder(p, "gb"); + break; + case GX_TF_Z24X8: + WriteRGBA8Encoder(p, true); + break; + case GX_CTF_Z4: + WriteC4Encoder(p, "b"); + break; + case GX_CTF_Z8M: + WriteC8Encoder(p, "g"); + break; + case GX_CTF_Z8L: + WriteC8Encoder(p, "r"); + break; + case GX_CTF_Z16L: + // byte order is reversed + WriteCC8Encoder(p, "rg"); + break; + default: + PanicAlert("Unknown texture copy format: 0x%x\n", format); + break; + + } + + if (text[sizeof(text) - 1] != 0x7C) + PanicAlert("TextureConversionShader generator - buffer too small, canary has been eaten!"); + + return text; +} + +void SetShaderParameters(u32 width, u32 height, u32 offsetX, u32 offsetY, float pixelStride, u32 format) +{ + u16 blkW = GetBlockWidthInTexels(format); + u16 blkH = GetBlockHeightInTexels(format); + u16 samples = GetEncodedSampleCount(format); + + SetPSConstant4f(C_COLORMATRIX, (float)blkW, (float)blkH, (float)samples, pixelStride); + SetPSConstant4f(C_COLORMATRIX + 1, (float)width, (float)(height - 1), (float)offsetX, (float)offsetY); + +} + + +} \ No newline at end of file diff --git a/Source/Plugins/Plugin_VideoOGL/Src/TextureConversionShader.h b/Source/Plugins/Plugin_VideoOGL/Src/TextureConversionShader.h new file mode 100644 index 0000000000..f898ffda45 --- /dev/null +++ b/Source/Plugins/Plugin_VideoOGL/Src/TextureConversionShader.h @@ -0,0 +1,41 @@ +// Copyright (C) 2003-2008 Dolphin Project. + +// This program is free software: you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation, version 2.0. + +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License 2.0 for more details. + +// A copy of the GPL 2.0 should have been included with the program. +// If not, see http://www.gnu.org/licenses/ + +// Official SVN repository and contact information can be found at +// http://code.google.com/p/dolphin-emu/ + +#ifndef _TEXTURECONVERSIONSHADER_H +#define _TEXTURECONVERSIONSHADER_H + +#include "Common.h" +#include "TextureDecoder.h" +#include + +namespace TextureConversionShader +{ + +u16 GetBlockWidthInTexels(u32 format); + +u16 GetBlockHeightInTexels(u32 format); + +u16 GetEncodedSampleCount(u32 format); + +char *GenerateEncodingShader(u32 format); + +void SetShaderParameters(u32 width, u32 height, u32 offsetX, u32 offsetY, float pixelStride, u32 format); + + +} + +#endif \ No newline at end of file diff --git a/Source/Plugins/Plugin_VideoOGL/Src/TextureConverter.cpp b/Source/Plugins/Plugin_VideoOGL/Src/TextureConverter.cpp index 4a7b45b406..42f27833ee 100644 --- a/Source/Plugins/Plugin_VideoOGL/Src/TextureConverter.cpp +++ b/Source/Plugins/Plugin_VideoOGL/Src/TextureConverter.cpp @@ -16,9 +16,12 @@ // http://code.google.com/p/dolphin-emu/ #include "TextureConverter.h" +#include "TextureConversionShader.h" #include "PixelShaderCache.h" #include "VertexShaderManager.h" #include "Globals.h" +#include "Config.h" +#include "ImageWrite.h" #include "GLUtil.h" #include "Render.h" @@ -35,6 +38,11 @@ const int renderBufferHeight = 1024; static FRAGMENTSHADER s_rgbToYuyvProgram; static FRAGMENTSHADER s_yuyvToRgbProgram; +// todo - store shaders in a smarter way - there are not 64 different copy texture formats +const u32 NUM_ENCODING_PROGRAMS = 64; +static FRAGMENTSHADER s_encodingPrograms[NUM_ENCODING_PROGRAMS]; + + void CreateRgbToYuyvProgram() { // output is BGRA because that is slightly faster than RGBA @@ -50,10 +58,12 @@ void CreateRgbToYuyvProgram() " float y0 = (0.257f * c0.r) + (0.504f * c0.g) + (0.098f * c0.b) + 0.0625f;\n" " float u0 =-(0.148f * c0.r) - (0.291f * c0.g) + (0.439f * c0.b) + 0.5f;\n" + " float v0 = (0.439f * c0.r) - (0.368f * c0.g) - (0.071f * c0.b) + 0.5f;\n" " float y1 = (0.257f * c1.r) + (0.504f * c1.g) + (0.098f * c1.b) + 0.0625f;\n" - " float v1 = (0.439f * c1.r) - (0.368f * c1.g) - (0.071f * c1.b) + 0.5f;\n" + " float u1 =-(0.148f * c1.r) - (0.291f * c1.g) + (0.439f * c1.b) + 0.5f;\n" + " float v1 = (0.439f * c1.r) - (0.368f * c1.g) - (0.071f * c1.b) + 0.5f;\n" - " ocol0 = float4(y1, u0, y0, v1);\n" + " ocol0 = float4(y1, (u0 + u1) / 2, y0, (v0 + v1) / 2);\n" "}\n"; if (!PixelShaderCache::CompilePixelShader(s_rgbToYuyvProgram, FProgram)) { @@ -88,6 +98,39 @@ void CreateYuyvToRgbProgram() } } +FRAGMENTSHADER& GetOrCreateEncodingShader(u32 format) +{ + if(format > NUM_ENCODING_PROGRAMS) + { + PanicAlert("Unknown texture copy format: 0x%x\n", format); + return s_encodingPrograms[0]; + } + + // todo - this does not handle the case that an application is using RGB555/4443 + // and switches EFB formats between a format that does and does not support alpha + if(s_encodingPrograms[format].glprogid == 0) + { + char* shader = TextureConversionShader::GenerateEncodingShader(format); + +#if defined(_DEBUG) || defined(DEBUGFAST) + if (g_Config.iLog & CONF_SAVESHADERS && shader) { + static int counter = 0; + char szTemp[MAX_PATH]; + sprintf(szTemp, "%s/enc_%04i.txt", g_Config.texDumpPath, counter++); + + SaveData(szTemp, shader); + } +#endif + + if (!PixelShaderCache::CompilePixelShader(s_encodingPrograms[format], shader)) { + const char* error = cgGetLastListing(g_cgcontext); + ERROR_LOG("Failed to create encoding fragment program\n"); + } + } + + return s_encodingPrograms[format]; +} + void Init() { glGenFramebuffersEXT( 1, &s_frameBuffer); @@ -114,16 +157,14 @@ void Shutdown() glDeleteFramebuffersEXT(1, &s_frameBuffer); } + void EncodeToRam(GLuint srcTexture, const TRectangle& sourceRc, - u8* destAddr, int dstWidth, int dstHeight) + u8* destAddr, int dstWidth, int dstHeight, bool linearFilter, FRAGMENTSHADER& shader) { Renderer::SetRenderMode(Renderer::RM_Normal); Renderer::ResetGLState(); - - float dstFormatFactor = 0.5f; - float dstFmtWidth = dstWidth * dstFormatFactor; - + // switch to texture converter frame buffer // attach render buffer as color destination Renderer::SetFramebuffer(s_frameBuffer); @@ -134,18 +175,28 @@ void EncodeToRam(GLuint srcTexture, const TRectangle& sourceRc, // set source texture glActiveTexture(GL_TEXTURE0); glBindTexture(GL_TEXTURE_RECTANGLE_ARB, srcTexture); - glTexParameteri(GL_TEXTURE_RECTANGLE_ARB, GL_TEXTURE_MAG_FILTER, GL_NEAREST); - glTexParameteri(GL_TEXTURE_RECTANGLE_ARB, GL_TEXTURE_MIN_FILTER, GL_NEAREST); + + if(linearFilter) + { + glTexParameteri(GL_TEXTURE_RECTANGLE_ARB, GL_TEXTURE_MAG_FILTER, GL_LINEAR); + glTexParameteri(GL_TEXTURE_RECTANGLE_ARB, GL_TEXTURE_MIN_FILTER, GL_LINEAR); + } + else + { + glTexParameteri(GL_TEXTURE_RECTANGLE_ARB, GL_TEXTURE_MAG_FILTER, GL_NEAREST); + glTexParameteri(GL_TEXTURE_RECTANGLE_ARB, GL_TEXTURE_MIN_FILTER, GL_NEAREST); + } + // TextureMngr::EnableTexRECT(0); for (int i = 1; i < 8; ++i) TextureMngr::DisableStage(i); GL_REPORT_ERRORD(); - glViewport(0, 0, (GLsizei)dstFmtWidth, (GLsizei)dstHeight); + glViewport(0, 0, (GLsizei)dstWidth, (GLsizei)dstHeight); glEnable(GL_FRAGMENT_PROGRAM_ARB); - glBindProgramARB( GL_FRAGMENT_PROGRAM_ARB, s_rgbToYuyvProgram.glprogid); + glBindProgramARB( GL_FRAGMENT_PROGRAM_ARB, shader.glprogid); glBegin(GL_QUADS); glTexCoord2f((float)sourceRc.left, (float)sourceRc.top); glVertex2f(-1,-1); @@ -155,8 +206,8 @@ void EncodeToRam(GLuint srcTexture, const TRectangle& sourceRc, glEnd(); GL_REPORT_ERRORD(); - // TODO: this is real slow. try using a pixel buffer object. - glReadPixels(0, 0, (GLsizei)dstFmtWidth, (GLsizei)dstHeight, GL_BGRA, GL_UNSIGNED_BYTE, destAddr); + // TODO: make this less slow. + glReadPixels(0, 0, (GLsizei)dstWidth, (GLsizei)dstHeight, GL_BGRA, GL_UNSIGNED_BYTE, destAddr); GL_REPORT_ERRORD(); Renderer::SetFramebuffer(0); @@ -170,6 +221,74 @@ void EncodeToRam(GLuint srcTexture, const TRectangle& sourceRc, GL_REPORT_ERRORD(); } +void EncodeToRam(u32 address, bool bFromZBuffer, bool bIsIntensityFmt, u32 copyfmt, bool bScaleByHalf, const TRectangle& 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& fs = GetOrCreateEncodingShader(format); + + if(fs.glprogid == 0) + return; + + u8* ptr = Memory_GetPtr(address); + + u32 target = bFromZBuffer?Renderer::GetZBufferTarget():Renderer::GetRenderTarget(); + + s32 width = source.right - source.left; + s32 height = source.bottom - source.top; + + if(bScaleByHalf) + { + width /= 2; + height /= 2; + } + + u16 blkW = TextureConversionShader::GetBlockWidthInTexels(format) - 1; + u16 blkH = TextureConversionShader::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); + + u32 top = Renderer::GetTargetHeight() - (source.top + expandedHeight); + + TextureConversionShader::SetShaderParameters(expandedWidth, expandedHeight, source.left, top, bScaleByHalf?2.0f:1.0f, format); + + TRectangle scaledSource; + scaledSource.top = 0; + scaledSource.bottom = expandedHeight; + scaledSource.left = 0; + scaledSource.right = expandedWidth / samples; + + EncodeToRam(target, scaledSource, ptr, expandedWidth / samples, expandedHeight, bScaleByHalf, fs); + + if (bFromZBuffer ) + Renderer::SetZBufferRender(); // notify for future settings +} + +void EncodeToRam(GLuint srcTexture, const TRectangle& sourceRc, + u8* destAddr, int dstWidth, int dstHeight) +{ + EncodeToRam(srcTexture, sourceRc, destAddr, dstWidth / 2, dstHeight, false, s_rgbToYuyvProgram); +} + + void DecodeToTexture(u8* srcAddr, int srcWidth, int srcHeight, GLuint destTexture) { Renderer::SetRenderMode(Renderer::RM_Normal); @@ -190,7 +309,7 @@ void DecodeToTexture(u8* srcAddr, int srcWidth, int srcHeight, GLuint destTextur glActiveTexture(GL_TEXTURE0); glBindTexture(GL_TEXTURE_RECTANGLE_ARB, s_srcTexture); - // TODO: this is slow. try using a pixel buffer object. + // TODO: make this less slow. glTexImage2D(GL_TEXTURE_RECTANGLE_ARB, 0, GL_RGBA8, (GLsizei)srcFmtWidth, (GLsizei)srcHeight, 0, GL_BGRA, GL_UNSIGNED_BYTE, srcAddr); TextureMngr::EnableTexRECT(0); diff --git a/Source/Plugins/Plugin_VideoOGL/Src/TextureConverter.h b/Source/Plugins/Plugin_VideoOGL/Src/TextureConverter.h index 32f3b38f2d..43e23c2364 100644 --- a/Source/Plugins/Plugin_VideoOGL/Src/TextureConverter.h +++ b/Source/Plugins/Plugin_VideoOGL/Src/TextureConverter.h @@ -29,6 +29,9 @@ namespace TextureConverter void Init(); void Shutdown(); +void EncodeToRam(u32 address, bool bFromZBuffer, bool bIsIntensityFmt, + u32 copyfmt, bool bScaleByHalf, const TRectangle& source); + void EncodeToRam(GLuint srcTexture, const TRectangle& sourceRc, u8* destAddr, int dstWidth, int dstHeight); diff --git a/Source/Plugins/Plugin_VideoOGL/Src/TextureMngr.cpp b/Source/Plugins/Plugin_VideoOGL/Src/TextureMngr.cpp index 2e22aaba6f..7e88d41627 100644 --- a/Source/Plugins/Plugin_VideoOGL/Src/TextureMngr.cpp +++ b/Source/Plugins/Plugin_VideoOGL/Src/TextureMngr.cpp @@ -627,7 +627,7 @@ void TextureMngr::CopyRenderTargetToTexture(u32 address, bool bFromZBuffer, bool GL_REPORT_ERRORD(); //SaveTexture("frame.tga", GL_TEXTURE_RECTANGLE_ARB, entry.texture, entry.w, entry.h); - //SaveTexture("tex.tga", GL_TEXTURE_RECTANGLE_ARB, Renderer::GetZBufferTarget(), Renderer::GetTargetWidth(), Renderer::GetTargetHeight()); + //SaveTexture("tex.tga", GL_TEXTURE_RECTANGLE_ARB, bFromZBuffer?Renderer::GetZBufferTarget():Renderer::GetRenderTarget(), Renderer::GetTargetWidth(), Renderer::GetTargetHeight()); } void TextureMngr::EnableTex2D(int stage) @@ -670,3 +670,10 @@ void TextureMngr::DisableStage(int stage) glDisable(GL_TEXTURE_RECTANGLE_ARB); } } + +void TextureMngr::ClearRenderTargets() +{ + TexCache::iterator iter = textures.begin(); + for (; iter!=textures.end(); iter++) + iter->second.isRenderTarget = false; +} diff --git a/Source/Plugins/Plugin_VideoOGL/Src/TextureMngr.h b/Source/Plugins/Plugin_VideoOGL/Src/TextureMngr.h index 6bfa2ec669..273a720901 100644 --- a/Source/Plugins/Plugin_VideoOGL/Src/TextureMngr.h +++ b/Source/Plugins/Plugin_VideoOGL/Src/TextureMngr.h @@ -79,6 +79,8 @@ public: static void EnableTex2D(int stage); static void EnableTexRECT(int stage); static void DisableStage(int stage); // sets active texture + + static void ClearRenderTargets(); // sets render target value of all textures to false }; bool SaveTexture(const char* filename, u32 textarget, u32 tex, int width, int height);