2009-07-28 15:32:10 -06:00
// Copyright (C) 2003 Dolphin Project.
2008-12-07 22:25:12 -07:00
// 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 <tchar.h>
2009-01-15 19:58:34 -07:00
# include <windows.h>
2008-12-07 22:25:12 -07:00
# include <d3dx9.h>
# include "Common.h"
2009-09-04 11:29:16 -06:00
# include "Atomic.h"
2009-07-12 15:58:32 -06:00
# include "Thread.h"
# include "LogManager.h"
2009-09-19 21:29:43 -06:00
# include "debugger/debugger.h"
2008-12-07 22:25:12 -07:00
2009-09-02 00:33:41 -06:00
# if defined(HAVE_WX) && HAVE_WX
# include "Debugger/Debugger.h"
GFXDebuggerDX9 * m_DebuggerFrame = NULL ;
# endif // HAVE_WX
2008-12-07 22:25:12 -07:00
# include "svnrev.h"
# include "resource.h"
# include "main.h"
2009-09-13 03:23:30 -06:00
# include "VideoConfig.h"
2008-12-07 22:25:12 -07:00
# include "Fifo.h"
# include "OpcodeDecoding.h"
# include "TextureCache.h"
2009-06-22 03:31:30 -06:00
# include "BPStructs.h"
2008-12-07 22:25:12 -07:00
# include "VertexManager.h"
2008-12-25 13:07:13 -07:00
# include "VertexLoaderManager.h"
2009-03-07 16:20:14 -07:00
# include "VertexShaderManager.h"
# include "PixelShaderManager.h"
# include "VertexShaderCache.h"
# include "PixelShaderCache.h"
2009-10-10 15:19:39 -06:00
# include "CommandProcessor.h"
# include "PixelEngine.h"
2009-09-03 14:37:35 -06:00
# include "OnScreenDisplay.h"
2008-12-07 22:25:12 -07:00
# include "DlgSettings.h"
# include "D3DTexture.h"
# include "D3DUtil.h"
# include "W32Util/Misc.h"
# include "EmuWindow.h"
# include "VideoState.h"
# include "XFBConvert.h"
2009-09-19 21:29:43 -06:00
# include "render.h"
2008-12-07 22:25:12 -07:00
2009-09-13 02:21:35 -06:00
// Having to include this is TERRIBLY ugly. FIXME x100
# include "../../../Core/Core/Src/ConfigManager.h" // FIXME
2008-12-07 22:25:12 -07:00
# include "Utils.h"
HINSTANCE g_hInstance = NULL ;
SVideoInitialize g_VideoInitialize ;
2009-03-18 11:17:58 -06:00
PLUGIN_GLOBALS * globals = NULL ;
2009-09-13 11:46:33 -06:00
bool s_initialized ;
2008-12-07 22:25:12 -07:00
2009-09-04 11:29:16 -06:00
static u32 s_efbAccessRequested = FALSE ;
2009-09-19 21:29:43 -06:00
static bool s_swapRequested = false ;
static volatile struct
{
u32 xfbAddr ;
FieldType field ;
u32 fbWidth ;
u32 fbHeight ;
} s_beginFieldArgs ;
2009-09-04 11:29:16 -06:00
2009-07-02 11:45:09 -06:00
static volatile EFBAccessType s_AccessEFBType ;
2009-08-09 06:12:29 -06:00
bool HandleDisplayList ( u32 address , u32 size )
{
return false ;
}
2009-09-02 00:33:41 -06:00
2009-09-13 15:18:04 -06:00
bool IsD3D ( )
{
return true ;
}
2009-09-02 00:33:41 -06:00
// This is used for the functions right below here which use wxwidgets
# if defined(HAVE_WX) && HAVE_WX
# ifdef _WIN32
WXDLLIMPEXP_BASE void wxSetInstance ( HINSTANCE hInst ) ;
# endif
wxWindow * GetParentedWxWindow ( HWND Parent )
{
# ifdef _WIN32
wxSetInstance ( ( HINSTANCE ) g_hInstance ) ;
# endif
wxWindow * win = new wxWindow ( ) ;
# ifdef _WIN32
win - > SetHWND ( ( WXHWND ) Parent ) ;
win - > AdoptAttributesFromHWND ( ) ;
# endif
return win ;
}
# endif
# if defined(HAVE_WX) && HAVE_WX
2008-12-07 22:25:12 -07:00
void DllDebugger ( HWND _hParent , bool Show )
{
2009-09-02 00:33:41 -06:00
if ( ! m_DebuggerFrame )
m_DebuggerFrame = new GFXDebuggerDX9 ( GetParentedWxWindow ( _hParent ) ) ;
if ( Show )
m_DebuggerFrame - > Show ( ) ;
else
m_DebuggerFrame - > Hide ( ) ;
2008-12-07 22:25:12 -07:00
}
2009-09-02 00:33:41 -06:00
# else
void DllDebugger ( HWND _hParent , bool Show ) { }
# endif
# if defined(HAVE_WX) && HAVE_WX
class wxDLLApp : public wxApp
{
bool OnInit ( )
{
return true ;
}
} ;
IMPLEMENT_APP_NO_MAIN ( wxDLLApp )
WXDLLIMPEXP_BASE void wxSetInstance ( HINSTANCE hInst ) ;
# endif
2009-09-12 09:00:08 -06:00
BOOL APIENTRY DllMain ( HINSTANCE hinstDLL , DWORD dwReason , LPVOID lpvReserved )
2008-12-07 22:25:12 -07:00
{
switch ( dwReason )
{
case DLL_PROCESS_ATTACH :
2009-09-02 00:33:41 -06:00
{
# if defined(HAVE_WX) && HAVE_WX
// Use wxInitialize() if you don't want GUI instead of the following 12 lines
wxSetInstance ( ( HINSTANCE ) hinstDLL ) ;
int argc = 0 ;
char * * argv = NULL ;
wxEntryStart ( argc , argv ) ;
if ( ! wxTheApp | | ! wxTheApp - > CallOnInit ( ) )
return FALSE ;
# endif
}
2008-12-07 22:25:12 -07:00
break ;
case DLL_PROCESS_DETACH :
2009-09-02 00:33:41 -06:00
# if defined(HAVE_WX) && HAVE_WX
// This causes a "stop hang", if the gfx config dialog has been opened.
// Old comment: "Use wxUninitialize() if you don't want GUI"
wxEntryCleanup ( ) ;
# endif
2008-12-07 22:25:12 -07:00
break ;
default :
break ;
}
g_hInstance = hinstDLL ;
return TRUE ;
}
2008-12-11 08:12:17 -07:00
unsigned int Callback_PeekMessages ( )
2008-12-07 22:25:12 -07:00
{
//TODO: peek message
MSG msg ;
while ( PeekMessage ( & msg , 0 , 0 , 0 , PM_REMOVE ) )
{
if ( msg . message = = WM_QUIT )
return FALSE ;
TranslateMessage ( & msg ) ;
DispatchMessage ( & msg ) ;
}
return TRUE ;
}
void UpdateFPSDisplay ( const char * text )
{
char temp [ 512 ] ;
sprintf_s ( temp , 512 , " SVN R%i: DX9: %s " , SVN_REV , text ) ;
2009-09-02 00:33:41 -06:00
SetWindowTextA ( EmuWindow : : GetWnd ( ) , temp ) ;
2008-12-07 22:25:12 -07:00
}
2009-01-14 23:48:15 -07:00
void GetDllInfo ( PLUGIN_INFO * _PluginInfo )
2008-12-07 22:25:12 -07:00
{
_PluginInfo - > Version = 0x0100 ;
_PluginInfo - > Type = PLUGIN_TYPE_VIDEO ;
2009-01-14 23:48:15 -07:00
# ifdef DEBUGFAST
2008-12-25 13:07:13 -07:00
sprintf_s ( _PluginInfo - > Name , 100 , " Dolphin Direct3D9 (DebugFast) " ) ;
2008-12-07 22:25:12 -07:00
# else
# ifndef _DEBUG
2008-12-25 13:07:13 -07:00
sprintf_s ( _PluginInfo - > Name , 100 , " Dolphin Direct3D9 " ) ;
2008-12-07 22:25:12 -07:00
# else
2008-12-25 13:07:13 -07:00
sprintf_s ( _PluginInfo - > Name , 100 , " Dolphin Direct3D9 (Debug) " ) ;
2008-12-07 22:25:12 -07:00
# endif
# endif
}
2009-01-08 05:12:15 -07:00
void SetDllGlobals ( PLUGIN_GLOBALS * _pPluginGlobals ) {
2009-03-18 11:17:58 -06:00
globals = _pPluginGlobals ;
LogManager : : SetInstance ( ( LogManager * ) globals - > logManager ) ;
2009-01-08 05:12:15 -07:00
}
2009-01-14 23:48:15 -07:00
void DllAbout ( HWND _hParent )
2008-12-07 22:25:12 -07:00
{
2009-09-02 00:33:41 -06:00
DialogBoxA ( g_hInstance , ( LPCSTR ) IDD_ABOUT , _hParent , ( DLGPROC ) AboutProc ) ;
2008-12-07 22:25:12 -07:00
}
void DllConfig ( HWND _hParent )
{
2009-09-12 09:00:08 -06:00
// If not initialized, only init D3D so we can enumerate resolutions.
2009-09-13 11:46:33 -06:00
if ( ! s_initialized ) D3D : : Init ( ) ;
2009-09-12 09:00:08 -06:00
DlgSettings_Show ( g_hInstance , _hParent ) ;
2009-09-13 11:46:33 -06:00
if ( ! s_initialized ) D3D : : Shutdown ( ) ;
2008-12-07 22:25:12 -07:00
}
2009-01-14 23:48:15 -07:00
void Initialize ( void * init )
2008-12-07 22:25:12 -07:00
{
2009-01-14 23:48:15 -07:00
SVideoInitialize * _pVideoInitialize = ( SVideoInitialize * ) init ;
2008-12-07 22:25:12 -07:00
frameCount = 0 ;
g_VideoInitialize = * _pVideoInitialize ;
2009-09-13 11:46:33 -06:00
g_Config . Load ( FULL_CONFIG_DIR " gfx_dx9.ini " ) ;
g_Config . GameIniLoad ( globals - > game_ini ) ;
UpdateProjectionHack ( g_Config . iPhackvalue ) ; // DX9 projection hack could be disabled by commenting out this line
// create the window
2009-10-28 16:40:18 -06:00
/*if (!g_Config.RenderToMainframe || g_VideoInitialize.pWindowHandle == NULL) // ignore parent for this plugin
2009-09-13 11:46:33 -06:00
g_VideoInitialize . pWindowHandle = ( void * ) EmuWindow : : Create ( NULL , g_hInstance , _T ( " Loading - Please wait. " ) ) ;
2009-10-28 16:40:18 -06:00
else */
2009-09-13 11:46:33 -06:00
g_VideoInitialize . pWindowHandle = ( void * ) EmuWindow : : Create ( ( HWND ) g_VideoInitialize . pWindowHandle , g_hInstance , _T ( " Loading - Please wait. " ) ) ;
if ( g_VideoInitialize . pWindowHandle = = NULL )
{
ERROR_LOG ( VIDEO , " An error has occurred while trying to create the window. " ) ;
return ;
}
EmuWindow : : Show ( ) ;
g_VideoInitialize . pPeekMessages = Callback_PeekMessages ;
g_VideoInitialize . pUpdateFPSDisplay = UpdateFPSDisplay ;
if ( FAILED ( D3D : : Init ( ) ) )
{
MessageBox ( GetActiveWindow ( ) , _T ( " Unable to initialize Direct3D. Please make sure that you have the latest version of DirectX 9.0c correctly installed. " ) , _T ( " Fatal Error " ) , MB_OK ) ;
return ;
}
InitXFBConvTables ( ) ;
OSD : : AddMessage ( " Dolphin Direct3D9 Video Plugin. " , 5000 ) ;
2008-12-07 22:25:12 -07:00
_pVideoInitialize - > pPeekMessages = g_VideoInitialize . pPeekMessages ;
_pVideoInitialize - > pUpdateFPSDisplay = g_VideoInitialize . pUpdateFPSDisplay ;
_pVideoInitialize - > pWindowHandle = g_VideoInitialize . pWindowHandle ;
2009-09-13 11:46:33 -06:00
s_initialized = true ;
2009-08-07 19:39:56 -06:00
}
2009-02-20 15:04:52 -07:00
2009-09-13 11:46:33 -06:00
void Video_Prepare ( )
2008-12-07 22:25:12 -07:00
{
2009-09-03 14:37:35 -06:00
Renderer : : Init ( ) ;
2008-12-07 22:25:12 -07:00
TextureCache : : Init ( ) ;
2009-06-22 03:31:30 -06:00
BPInit ( ) ;
2008-12-07 22:25:12 -07:00
VertexManager : : Init ( ) ;
Fifo_Init ( ) ;
2008-12-25 13:07:13 -07:00
VertexLoaderManager : : Init ( ) ;
2008-12-07 22:25:12 -07:00
OpcodeDecoder_Init ( ) ;
2009-03-07 16:20:14 -07:00
VertexShaderCache : : Init ( ) ;
VertexShaderManager : : Init ( ) ;
PixelShaderCache : : Init ( ) ;
PixelShaderManager : : Init ( ) ;
2009-10-10 15:19:39 -06:00
CommandProcessor : : Init ( ) ;
PixelEngine : : Init ( ) ;
2008-12-07 22:25:12 -07:00
}
2009-09-13 11:46:33 -06:00
void Shutdown ( )
2008-12-07 22:25:12 -07:00
{
2009-09-13 11:46:33 -06:00
s_efbAccessRequested = FALSE ;
2008-12-07 22:25:12 -07:00
Fifo_Shutdown ( ) ;
VertexManager : : Shutdown ( ) ;
2009-03-07 16:20:14 -07:00
VertexLoaderManager : : Shutdown ( ) ;
VertexShaderCache : : Shutdown ( ) ;
2009-09-13 11:46:33 -06:00
VertexShaderManager : : Shutdown ( ) ;
2009-03-07 16:20:14 -07:00
PixelShaderCache : : Shutdown ( ) ;
PixelShaderManager : : Shutdown ( ) ;
2008-12-07 22:25:12 -07:00
TextureCache : : Shutdown ( ) ;
2009-09-13 11:46:33 -06:00
OpcodeDecoder_Shutdown ( ) ;
2008-12-07 22:25:12 -07:00
Renderer : : Shutdown ( ) ;
2009-09-13 11:46:33 -06:00
D3D : : Shutdown ( ) ;
EmuWindow : : Close ( ) ;
s_initialized = false ;
2008-12-07 22:25:12 -07:00
}
2009-09-12 09:00:08 -06:00
void DoState ( unsigned char * * ptr , int mode ) {
// Clear texture cache because it might have written to RAM
TextureCache : : Invalidate ( false ) ;
2009-09-15 13:53:22 -06:00
// No need to clear shader caches.
2009-09-12 09:00:08 -06:00
PointerWrap p ( ptr , mode ) ;
VideoCommon_DoState ( p ) ;
}
void Video_EnterLoop ( )
{
Fifo_EnterLoop ( g_VideoInitialize ) ;
}
void Video_ExitLoop ( )
{
Fifo_ExitLoop ( ) ;
}
void Video_SetRendering ( bool bEnabled ) {
Fifo_SetRendering ( bEnabled ) ;
}
2009-09-19 21:29:43 -06:00
// Run from the graphics thread
2009-07-10 20:34:16 -06:00
void VideoFifo_CheckSwapRequest ( )
{
2009-09-19 21:29:43 -06:00
// CPU swap, not finished, seems to be working fine for dual-core for now
2009-09-20 07:00:25 -06:00
return ;
2009-09-19 21:29:43 -06:00
if ( s_swapRequested )
{
// Flip the backbuffer to front buffer now
s_swapRequested = false ;
2009-09-20 07:00:25 -06:00
//if (s_beginFieldArgs.field == FIELD_PROGRESSIVE || s_beginFieldArgs.field == FIELD_LOWER)
2009-09-19 21:29:43 -06:00
{
Renderer : : Swap ( 0 , FIELD_PROGRESSIVE , 0 , 0 ) ; // The swap function is not finished
// so it is ok to pass dummy parameters for now
}
}
2009-07-10 20:34:16 -06:00
}
2009-09-19 21:29:43 -06:00
// Run from the graphics thread
2009-07-10 20:34:16 -06:00
void VideoFifo_CheckSwapRequestAt ( u32 xfbAddr , u32 fbWidth , u32 fbHeight )
{
2009-08-31 10:10:36 -06:00
// CPU swap unimplemented
2009-07-11 10:52:34 -06:00
}
2009-09-19 21:29:43 -06:00
// Run from the CPU thread (from VideoInterface.cpp)
2009-07-10 20:34:16 -06:00
void Video_BeginField ( u32 xfbAddr , FieldType field , u32 fbWidth , u32 fbHeight )
2008-12-07 22:25:12 -07:00
{
2009-09-19 21:29:43 -06:00
s_beginFieldArgs . xfbAddr = xfbAddr ;
s_beginFieldArgs . field = field ;
s_beginFieldArgs . fbWidth = fbWidth ;
s_beginFieldArgs . fbHeight = fbHeight ;
s_swapRequested = true ;
2008-12-07 22:25:12 -07:00
2009-09-19 21:29:43 -06:00
if ( s_initialized )
{
2008-12-07 22:25:12 -07:00
2009-09-19 21:29:43 -06:00
// Make sure previous swap request has made it to the screen
2009-10-23 09:26:35 -06:00
if ( g_VideoInitialize . bOnThread )
2009-09-19 21:29:43 -06:00
{
// It seems to be working fine in this way for now without using AtomicLoadAcquire
// ector, please check here
//while (Common::AtomicLoadAcquire(s_swapRequested))
// Common::YieldCPU();
}
else
VideoFifo_CheckSwapRequest ( ) ;
2008-12-07 22:25:12 -07:00
2009-09-19 21:29:43 -06:00
}
DEBUGGER_LOG_AT ( NEXT_XFB_CMD , { printf ( " Begin Field: %08x, %d x %d \n " , xfbAddr , fbWidth , fbHeight ) ; } ) ;
2008-12-07 22:25:12 -07:00
}
2009-07-15 16:20:59 -06:00
void Video_EndField ( )
{
}
2009-02-20 15:04:52 -07:00
void Video_AddMessage ( const char * pstr , u32 milliseconds )
{
2009-09-03 14:37:35 -06:00
OSD : : AddMessage ( pstr , milliseconds ) ;
2009-02-20 15:04:52 -07:00
}
2008-12-07 22:25:12 -07:00
2009-02-26 20:56:34 -07:00
HRESULT ScreenShot ( const char * File )
2008-12-07 22:25:12 -07:00
{
if ( D3D : : dev = = NULL )
return S_FALSE ;
D3DDISPLAYMODE DisplayMode ;
if ( FAILED ( D3D : : dev - > GetDisplayMode ( 0 , & DisplayMode ) ) )
return S_FALSE ;
LPDIRECT3DSURFACE9 surf ;
if ( FAILED ( D3D : : dev - > CreateOffscreenPlainSurface ( DisplayMode . Width , DisplayMode . Height , D3DFMT_A8R8G8B8 , D3DPOOL_SYSTEMMEM , & surf , NULL ) ) )
return S_FALSE ;
if ( FAILED ( D3D : : dev - > GetFrontBufferData ( 0 , surf ) ) )
{
2009-01-14 23:48:15 -07:00
surf - > Release ( ) ;
2008-12-07 22:25:12 -07:00
return S_FALSE ;
}
RECT rect ;
: : GetWindowRect ( EmuWindow : : GetWnd ( ) , & rect ) ;
2009-09-02 00:33:41 -06:00
if ( FAILED ( D3DXSaveSurfaceToFileA ( File , D3DXIFF_PNG , surf , NULL , & rect ) ) )
2008-12-07 22:25:12 -07:00
{
surf - > Release ( ) ;
return S_FALSE ;
}
surf - > Release ( ) ;
return S_OK ;
}
2009-02-26 20:56:34 -07:00
void Video_Screenshot ( const char * _szFilename )
2008-12-07 22:25:12 -07:00
{
2009-03-01 03:53:23 -07:00
if ( ScreenShot ( _szFilename ) ! = S_OK )
2009-02-26 20:56:34 -07:00
PanicAlert ( " Error while capturing screen " ) ;
2009-02-27 04:45:07 -07:00
else {
std : : string message = " Saved " ;
message + = _szFilename ;
2009-09-03 14:37:35 -06:00
OSD : : AddMessage ( message . c_str ( ) , 2000 ) ;
2009-02-27 04:45:07 -07:00
}
2008-12-07 22:25:12 -07:00
}
2009-07-02 11:45:09 -06:00
2009-09-04 11:29:16 -06:00
static struct
2009-07-02 11:45:09 -06:00
{
2009-09-04 11:29:16 -06:00
EFBAccessType type ;
u32 x ;
u32 y ;
} s_accessEFBArgs ;
2009-07-02 11:45:09 -06:00
2009-09-04 11:29:16 -06:00
static u32 s_AccessEFBResult = 0 ;
2009-07-02 11:45:09 -06:00
2009-09-04 11:29:16 -06:00
void VideoFifo_CheckEFBAccess ( )
{
if ( Common : : AtomicLoadAcquire ( s_efbAccessRequested ) )
{
s_AccessEFBResult = Renderer : : AccessEFB ( s_accessEFBArgs . type , s_accessEFBArgs . x , s_accessEFBArgs . y ) ;
2009-07-02 11:45:09 -06:00
2009-09-04 11:29:16 -06:00
Common : : AtomicStoreRelease ( s_efbAccessRequested , FALSE ) ;
2009-07-02 11:45:09 -06:00
}
}
u32 Video_AccessEFB ( EFBAccessType type , u32 x , u32 y )
{
2009-10-07 13:54:56 -06:00
if ( ! g_ActiveConfig . bEFBAccessEnable )
return 0 ;
2009-09-04 11:29:16 -06:00
s_accessEFBArgs . type = type ;
s_accessEFBArgs . x = x ;
s_accessEFBArgs . y = y ;
2009-07-02 11:45:09 -06:00
2009-09-04 11:29:16 -06:00
Common : : AtomicStoreRelease ( s_efbAccessRequested , TRUE ) ;
2009-07-02 11:45:09 -06:00
2009-10-23 09:26:35 -06:00
if ( g_VideoInitialize . bOnThread )
2009-09-04 11:29:16 -06:00
{
while ( Common : : AtomicLoadAcquire ( s_efbAccessRequested ) )
Common : : YieldCPU ( ) ;
}
2009-07-02 11:45:09 -06:00
else
2009-08-31 10:10:36 -06:00
VideoFifo_CheckEFBAccess ( ) ;
2009-09-04 11:29:16 -06:00
return s_AccessEFBResult ;
2009-07-02 11:45:09 -06:00
}
2009-10-10 15:19:39 -06:00
void Video_CommandProcessorRead16 ( u16 & _rReturnValue , const u32 _Address )
{
CommandProcessor : : Read16 ( _rReturnValue , _Address ) ;
}
void Video_CommandProcessorWrite16 ( const u16 _Data , const u32 _Address )
{
CommandProcessor : : Write16 ( _Data , _Address ) ;
}
void Video_PixelEngineRead16 ( u16 & _rReturnValue , const u32 _Address )
{
PixelEngine : : Read16 ( _rReturnValue , _Address ) ;
}
void Video_PixelEngineWrite16 ( const u16 _Data , const u32 _Address )
{
PixelEngine : : Write16 ( _Data , _Address ) ;
}
void Video_PixelEngineWrite32 ( const u32 _Data , const u32 _Address )
{
PixelEngine : : Write32 ( _Data , _Address ) ;
}
inline void Video_GatherPipeBursted ( void )
{
CommandProcessor : : GatherPipeBursted ( ) ;
}
void Video_WaitForFrameFinish ( void )
{
CommandProcessor : : WaitForFrameFinish ( ) ;
}