Merged identical VertexManager code from DX9/DX11/OGL plugins into VideoCommon. Still...
[dolphin.git] / Source / Plugins / Plugin_VideoDX11 / Src / main.cpp
blob39cef47abc292f06d591a444ec202883aff73718
1 // Copyright (C) 2003 Dolphin Project.
3 // This program is free software: you can redistribute it and/or modify
4 // it under the terms of the GNU General Public License as published by
5 // the Free Software Foundation, version 2.0.
7 // This program is distributed in the hope that it will be useful,
8 // but WITHOUT ANY WARRANTY; without even the implied warranty of
9 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10 // GNU General Public License 2.0 for more details.
12 // A copy of the GPL 2.0 should have been included with the program.
13 // If not, see http://www.gnu.org/licenses/
15 // Official SVN repository and contact information can be found at
16 // http://code.google.com/p/dolphin-emu/
18 #include <tchar.h>
19 #include <windows.h>
20 #include <d3dx11.h>
22 #include <wx/wx.h>
23 #include <wx/notebook.h>
25 #include "Common.h"
26 #include "Atomic.h"
27 #include "Thread.h"
28 #include "LogManager.h"
30 #include "VideoConfig.h"
31 #include "Fifo.h"
32 #include "OpcodeDecoding.h"
33 #include "BPStructs.h"
34 #include "VertexLoaderManager.h"
35 #include "VertexShaderManager.h"
36 #include "PixelShaderManager.h"
37 #include "CommandProcessor.h"
38 #include "PixelEngine.h"
39 #include "OnScreenDisplay.h"
40 #include "VideoState.h"
41 #include "XFBConvert.h"
42 #include "Render.h"
44 #include "main.h"
45 #include "resource.h"
46 #include "DlgSettings.h"
47 #include "TextureCache.h"
48 #include "VertexManager.h"
49 #include "VertexShaderCache.h"
50 #include "PixelShaderCache.h"
51 #include "D3DTexture.h"
52 #include "D3DUtil.h"
53 #include "W32Util/Misc.h"
54 #include "EmuWindow.h"
55 #include "FramebufferManager.h"
56 #include "DLCache.h"
58 HINSTANCE g_hInstance = NULL;
59 SVideoInitialize g_VideoInitialize;
60 PLUGIN_GLOBALS* globals = NULL;
61 static bool s_PluginInitialized = false;
63 volatile u32 s_swapRequested = FALSE;
64 static u32 s_efbAccessRequested = FALSE;
65 static volatile u32 s_FifoShuttingDown = FALSE;
67 static volatile struct
69 u32 xfbAddr;
70 FieldType field;
71 u32 fbWidth;
72 u32 fbHeight;
73 } s_beginFieldArgs;
75 static volatile EFBAccessType s_AccessEFBType;
77 bool IsD3D()
79 return true;
82 // This is used for the functions right below here which use wxwidgets
83 #if defined(HAVE_WX) && HAVE_WX
84 WXDLLIMPEXP_BASE void wxSetInstance(HINSTANCE hInst);
85 #endif
87 void *DllDebugger(void *_hParent, bool Show)
89 return NULL;
92 #if defined(HAVE_WX) && HAVE_WX
93 class wxDLLApp : public wxApp
95 bool OnInit()
97 return true;
100 IMPLEMENT_APP_NO_MAIN(wxDLLApp)
101 WXDLLIMPEXP_BASE void wxSetInstance(HINSTANCE hInst);
102 #endif
104 BOOL APIENTRY DllMain(HINSTANCE hinstDLL, DWORD dwReason, LPVOID lpvReserved)
106 switch (dwReason)
108 case DLL_PROCESS_ATTACH:
110 #if defined(HAVE_WX) && HAVE_WX
111 wxSetInstance((HINSTANCE)hinstDLL);
112 wxInitialize();
113 #endif
115 break;
116 case DLL_PROCESS_DETACH:
117 #if defined(HAVE_WX) && HAVE_WX
118 wxUninitialize();
119 #endif
120 break;
123 g_hInstance = hinstDLL;
124 return TRUE;
127 unsigned int Callback_PeekMessages()
129 MSG msg;
130 while (PeekMessage(&msg, 0, 0, 0, PM_REMOVE))
132 if (msg.message == WM_QUIT)
133 return FALSE;
134 TranslateMessage(&msg);
135 DispatchMessage(&msg);
137 return TRUE;
141 void UpdateFPSDisplay(const char *text)
143 char temp[512];
144 sprintf_s(temp, 512, "SVN R%s: DX11: %s", svn_rev_str, text);
145 SetWindowTextA(EmuWindow::GetWnd(), temp);
148 void GetDllInfo(PLUGIN_INFO* _PluginInfo)
150 _PluginInfo->Version = 0x0100;
151 _PluginInfo->Type = PLUGIN_TYPE_VIDEO;
152 #ifdef DEBUGFAST
153 sprintf_s(_PluginInfo->Name, 100, "Dolphin Direct3D11 (DebugFast)");
154 #elif defined _DEBUG
155 sprintf_s(_PluginInfo->Name, 100, "Dolphin Direct3D11 (Debug)");
156 #else
157 sprintf_s(_PluginInfo->Name, 100, "Dolphin Direct3D11");
158 #endif
161 void SetDllGlobals(PLUGIN_GLOBALS* _pPluginGlobals)
163 globals = _pPluginGlobals;
164 LogManager::SetInstance((LogManager*)globals->logManager);
167 void DllAbout(HWND _hParent)
169 //DialogBox(g_hInstance,(LPCTSTR)IDD_ABOUT,_hParent,(DLGPROC)AboutProc);
172 void DllConfig(void *_hParent)
174 DlgSettings_Show(g_hInstance, (HWND)((wxWindow *)_hParent)->GetHandle());
177 void Initialize(void *init)
179 frameCount = 0;
180 SVideoInitialize *_pVideoInitialize = (SVideoInitialize*)init;
181 // Create a shortcut to _pVideoInitialize that can also update it
182 g_VideoInitialize = *(_pVideoInitialize);
183 InitXFBConvTables();
185 g_Config.Load((std::string(File::GetUserPath(D_CONFIG_IDX)) + "gfx_dx11.ini").c_str());
186 g_Config.GameIniLoad(globals->game_ini);
187 UpdateActiveConfig();
189 g_VideoInitialize.pWindowHandle = (void*)EmuWindow::Create((HWND)g_VideoInitialize.pWindowHandle, g_hInstance, _T("Loading - Please wait."));
190 if (g_VideoInitialize.pWindowHandle == NULL)
192 ERROR_LOG(VIDEO, "An error has occurred while trying to create the window.");
193 return;
196 g_VideoInitialize.pPeekMessages = &Callback_PeekMessages;
197 g_VideoInitialize.pUpdateFPSDisplay = &UpdateFPSDisplay;
199 _pVideoInitialize->pPeekMessages = g_VideoInitialize.pPeekMessages;
200 _pVideoInitialize->pUpdateFPSDisplay = g_VideoInitialize.pUpdateFPSDisplay;
202 // Now the window handle is written
203 _pVideoInitialize->pWindowHandle = g_VideoInitialize.pWindowHandle;
205 OSD::AddMessage("Dolphin Direct3D11 Video Plugin.", 5000);
206 s_PluginInitialized = true;
209 void Video_Prepare()
211 // Better be safe...
212 s_efbAccessRequested = FALSE;
213 s_FifoShuttingDown = FALSE;
214 s_swapRequested = FALSE;
216 // internal interfaces
217 Renderer::Init();
218 TextureCache::Init();
219 g_vertex_manager = new DX11::VertexManager;
220 VertexShaderCache::Init();
221 PixelShaderCache::Init();
222 D3D::InitUtils();
224 // VideoCommon
225 BPInit();
226 Fifo_Init();
227 VertexLoaderManager::Init();
228 OpcodeDecoder_Init();
229 VertexShaderManager::Init();
230 PixelShaderManager::Init();
231 CommandProcessor::Init();
232 PixelEngine::Init();
233 DLCache::Init();
235 // Tell the host that the window is ready
236 g_VideoInitialize.pCoreMessage(WM_USER_CREATE);
239 void Shutdown()
241 s_PluginInitialized = false;
243 s_efbAccessRequested = FALSE;
244 s_FifoShuttingDown = FALSE;
245 s_swapRequested = FALSE;
247 // VideoCommon
248 DLCache::Shutdown();
249 Fifo_Shutdown();
250 CommandProcessor::Shutdown();
251 PixelShaderManager::Shutdown();
252 VertexShaderManager::Shutdown();
253 OpcodeDecoder_Shutdown();
254 VertexLoaderManager::Shutdown();
256 // internal interfaces
257 D3D::ShutdownUtils();
258 PixelShaderCache::Shutdown();
259 VertexShaderCache::Shutdown();
260 delete g_vertex_manager;
261 TextureCache::Shutdown();
262 Renderer::Shutdown();
263 EmuWindow::Close();
265 s_PluginInitialized = false;
268 void DoState(unsigned char **ptr, int mode)
270 // Clear texture cache because it might have written to RAM
271 CommandProcessor::FifoCriticalEnter();
272 TextureCache::Invalidate(false);
273 CommandProcessor::FifoCriticalLeave();
274 // No need to clear shader caches
275 PointerWrap p(ptr, mode);
276 VideoCommon_DoState(p);
279 void EmuStateChange(PLUGIN_EMUSTATE newState)
281 Fifo_RunLoop((newState == PLUGIN_EMUSTATE_PLAY) ? true : false);
284 // Enter and exit the video loop
285 void Video_EnterLoop()
287 Fifo_EnterLoop(g_VideoInitialize);
290 void Video_ExitLoop()
292 Fifo_ExitLoop();
293 s_FifoShuttingDown = TRUE;
296 void Video_SetRendering(bool bEnabled)
298 Fifo_SetRendering(bEnabled);
301 // Run from the graphics thread (from Fifo.cpp)
302 void VideoFifo_CheckSwapRequest()
304 if(g_ActiveConfig.bUseXFB)
306 if (Common::AtomicLoadAcquire(s_swapRequested))
308 EFBRectangle rc;
309 Renderer::Swap(s_beginFieldArgs.xfbAddr, s_beginFieldArgs.field, s_beginFieldArgs.fbWidth, s_beginFieldArgs.fbHeight,rc);
310 Common::AtomicStoreRelease(s_swapRequested, FALSE);
315 // Run from the graphics thread (from Fifo.cpp)
316 void VideoFifo_CheckSwapRequestAt(u32 xfbAddr, u32 fbWidth, u32 fbHeight)
318 if (g_ActiveConfig.bUseXFB)
320 if(Common::AtomicLoadAcquire(s_swapRequested))
322 u32 aLower = xfbAddr;
323 u32 aUpper = xfbAddr + 2 * fbWidth * fbHeight;
324 u32 bLower = s_beginFieldArgs.xfbAddr;
325 u32 bUpper = s_beginFieldArgs.xfbAddr + 2 * s_beginFieldArgs.fbWidth * s_beginFieldArgs.fbHeight;
327 if (addrRangesOverlap(aLower, aUpper, bLower, bUpper))
328 VideoFifo_CheckSwapRequest();
333 // Run from the CPU thread (from VideoInterface.cpp)
334 void Video_BeginField(u32 xfbAddr, FieldType field, u32 fbWidth, u32 fbHeight)
336 if (s_PluginInitialized && g_ActiveConfig.bUseXFB)
338 if (g_VideoInitialize.bOnThread)
340 while (Common::AtomicLoadAcquire(s_swapRequested) && !s_FifoShuttingDown)
341 //Common::SleepCurrentThread(1);
342 Common::YieldCPU();
344 else
345 VideoFifo_CheckSwapRequest();
346 s_beginFieldArgs.xfbAddr = xfbAddr;
347 s_beginFieldArgs.field = field;
348 s_beginFieldArgs.fbWidth = fbWidth;
349 s_beginFieldArgs.fbHeight = fbHeight;
351 Common::AtomicStoreRelease(s_swapRequested, TRUE);
355 // Run from the CPU thread (from VideoInterface.cpp)
356 void Video_EndField()
360 void Video_AddMessage(const char* pstr, u32 milliseconds)
362 OSD::AddMessage(pstr, milliseconds);
365 // Screenshot
366 void Video_Screenshot(const char *_szFilename)
368 Renderer::SetScreenshot(_szFilename);
371 static struct
373 EFBAccessType type;
374 u32 x;
375 u32 y;
376 u32 Data;
377 } s_accessEFBArgs;
379 static u32 s_AccessEFBResult = 0;
381 void VideoFifo_CheckEFBAccess()
383 if (Common::AtomicLoadAcquire(s_efbAccessRequested))
385 s_AccessEFBResult = Renderer::AccessEFB(s_accessEFBArgs.type, s_accessEFBArgs.x, s_accessEFBArgs.y);
387 Common::AtomicStoreRelease(s_efbAccessRequested, FALSE);
391 u32 Video_AccessEFB(EFBAccessType type, u32 x, u32 y, u32 InputData)
393 if (s_PluginInitialized)
395 s_accessEFBArgs.type = type;
396 s_accessEFBArgs.x = x;
397 s_accessEFBArgs.y = y;
398 s_accessEFBArgs.Data = InputData;
400 Common::AtomicStoreRelease(s_efbAccessRequested, TRUE);
402 if (g_VideoInitialize.bOnThread)
404 while (Common::AtomicLoadAcquire(s_efbAccessRequested) && !s_FifoShuttingDown)
405 //Common::SleepCurrentThread(1);
406 Common::YieldCPU();
408 else
409 VideoFifo_CheckEFBAccess();
411 return s_AccessEFBResult;
414 return 0;
417 void VideoFifo_CheckAsyncRequest() {
418 VideoFifo_CheckSwapRequest();
419 VideoFifo_CheckEFBAccess();
422 void Video_CommandProcessorRead16(u16& _rReturnValue, const u32 _Address)
424 CommandProcessor::Read16(_rReturnValue, _Address);
427 void Video_CommandProcessorWrite16(const u16 _Data, const u32 _Address)
429 CommandProcessor::Write16(_Data, _Address);
432 void Video_PixelEngineRead16(u16& _rReturnValue, const u32 _Address)
434 PixelEngine::Read16(_rReturnValue, _Address);
437 void Video_PixelEngineWrite16(const u16 _Data, const u32 _Address)
439 PixelEngine::Write16(_Data, _Address);
442 void Video_PixelEngineWrite32(const u32 _Data, const u32 _Address)
444 PixelEngine::Write32(_Data, _Address);
447 void Video_GatherPipeBursted(void)
449 CommandProcessor::GatherPipeBursted();
452 void Video_WaitForFrameFinish(void)
454 CommandProcessor::WaitForFrameFinish();
457 bool Video_IsFifoBusy(void)
459 return CommandProcessor::isFifoBusy;
462 void Video_AbortFrame(void)
464 CommandProcessor::AbortFrame();