From 07d947c8348022984219c56ae9cf8ee2ffc05ad3 Mon Sep 17 00:00:00 2001 From: NeoBrainX Date: Wed, 29 Dec 2010 13:07:00 +0000 Subject: [PATCH] DX9/DX11: Fixing some maybe possible crashes if a game was started, the config dialog opened and the game closed again. Due to other issues this still happens quite often though... Various warning fixes. git-svn-id: https://dolphin-emu.googlecode.com/svn/trunk@6684 8ced0084-cf51-0410-be5f-012b33b47a6e --- Source/Core/AudioCommon/Src/Mixer.cpp | 2 +- Source/Core/AudioCommon/Src/XAudio2Stream.cpp | 2 +- .../Plugins/Plugin_VideoDX11/Src/D3DBase.cpp | 19 +++++++++++++++++++ .../Plugins/Plugin_VideoDX11/Src/D3DBlob.cpp | 2 +- Source/Plugins/Plugin_VideoDX11/Src/main.cpp | 9 +++------ .../Plugins/Plugin_VideoDX9/Src/D3DBase.cpp | 19 +++++++++++++++++-- Source/Plugins/Plugin_VideoDX9/Src/main.cpp | 8 ++------ 7 files changed, 44 insertions(+), 17 deletions(-) diff --git a/Source/Core/AudioCommon/Src/Mixer.cpp b/Source/Core/AudioCommon/Src/Mixer.cpp index acccede47f..ddeec84b6f 100644 --- a/Source/Core/AudioCommon/Src/Mixer.cpp +++ b/Source/Core/AudioCommon/Src/Mixer.cpp @@ -158,7 +158,7 @@ void CMixer::PushSamples(short *samples, unsigned int num_samples) if (m_sampleRate == 32000) Common::AtomicAdd(m_numSamples, num_samples); else if (m_sampleRate == 48000) - Common::AtomicAdd(m_numSamples, num_samples * 1.5); + Common::AtomicAdd(m_numSamples, num_samples * 3 / 2); else PanicAlert("Mixer: Unsupported sample rate."); diff --git a/Source/Core/AudioCommon/Src/XAudio2Stream.cpp b/Source/Core/AudioCommon/Src/XAudio2Stream.cpp index aea7dfc1fe..326cd4e318 100644 --- a/Source/Core/AudioCommon/Src/XAudio2Stream.cpp +++ b/Source/Core/AudioCommon/Src/XAudio2Stream.cpp @@ -158,7 +158,7 @@ bool XAudio2::Start() void XAudio2::SetVolume(int volume) { //linear 1- .01 - m_volume = (float)volume / 100.0; + m_volume = (float)volume / 100.f; if (pMasteringVoice) pMasteringVoice->SetVolume(m_volume); diff --git a/Source/Plugins/Plugin_VideoDX11/Src/D3DBase.cpp b/Source/Plugins/Plugin_VideoDX11/Src/D3DBase.cpp index fd83308a67..6e95178182 100644 --- a/Source/Plugins/Plugin_VideoDX11/Src/D3DBase.cpp +++ b/Source/Plugins/Plugin_VideoDX11/Src/D3DBase.cpp @@ -29,14 +29,17 @@ D3DX11COMPILEFROMMEMORYTYPE PD3DX11CompileFromMemory = NULL; D3DX11FILTERTEXTURETYPE PD3DX11FilterTexture = NULL; D3DX11SAVETEXTURETOFILEATYPE PD3DX11SaveTextureToFileA = NULL; D3DX11SAVETEXTURETOFILEWTYPE PD3DX11SaveTextureToFileW = NULL; +int d3dx_dll_ref = 0; CREATEDXGIFACTORY PCreateDXGIFactory = NULL; HINSTANCE hDXGIDll = NULL; +int dxgi_dll_ref = 0; typedef HRESULT (WINAPI* D3D11CREATEDEVICEANDSWAPCHAIN)(IDXGIAdapter*, D3D_DRIVER_TYPE, HMODULE, UINT, CONST D3D_FEATURE_LEVEL*, UINT, UINT, CONST DXGI_SWAP_CHAIN_DESC*, IDXGISwapChain**, ID3D11Device**, D3D_FEATURE_LEVEL*, ID3D11DeviceContext**); D3D11CREATEDEVICE PD3D11CreateDevice = NULL; D3D11CREATEDEVICEANDSWAPCHAIN PD3D11CreateDeviceAndSwapChain = NULL; HINSTANCE hD3DDll = NULL; +int d3d_dll_ref = 0; namespace D3D { @@ -65,11 +68,14 @@ bool bFrameInProgress = false; HRESULT LoadDXGI() { + if (dxgi_dll_ref++ > 0) return S_OK; + if (hDXGIDll) return S_OK; hDXGIDll = LoadLibraryA("dxgi.dll"); if (!hDXGIDll) { MessageBoxA(NULL, "Failed to load dxgi.dll", "Critical error", MB_OK | MB_ICONERROR); + --dxgi_dll_ref; return E_FAIL; } PCreateDXGIFactory = (CREATEDXGIFACTORY)GetProcAddress(hDXGIDll, "CreateDXGIFactory"); @@ -80,11 +86,14 @@ HRESULT LoadDXGI() HRESULT LoadD3D() { + if (d3d_dll_ref++ > 0) return S_OK; + if (hD3DDll) return S_OK; hD3DDll = LoadLibraryA("d3d11.dll"); if (!hD3DDll) { MessageBoxA(NULL, "Failed to load d3d11.dll", "Critical error", MB_OK | MB_ICONERROR); + --d3d_dll_ref; return E_FAIL; } PD3D11CreateDevice = (D3D11CREATEDEVICE)GetProcAddress(hD3DDll, "D3D11CreateDevice"); @@ -98,6 +107,7 @@ HRESULT LoadD3D() HRESULT LoadD3DX() { + if (d3dx_dll_ref++ > 0) return S_OK; if (hD3DXDll) return S_OK; // try to load D3DX11 first to check whether we have proper runtime support @@ -135,6 +145,9 @@ HRESULT LoadD3DX() void UnloadDXGI() { + if (!dxgi_dll_ref) return; + if (--dxgi_dll_ref != 0) return; + if(hDXGIDll) FreeLibrary(hDXGIDll); hDXGIDll = NULL; PCreateDXGIFactory = NULL; @@ -142,6 +155,9 @@ void UnloadDXGI() void UnloadD3DX() { + if (!d3dx_dll_ref) return; + if (--d3dx_dll_ref != 0) return; + if(hD3DXDll) FreeLibrary(hD3DXDll); hD3DXDll = NULL; PD3DX11FilterTexture = NULL; @@ -151,6 +167,9 @@ void UnloadD3DX() void UnloadD3D() { + if (!d3d_dll_ref) return; + if (--d3d_dll_ref != 0) return; + if(hD3DDll) FreeLibrary(hD3DDll); hD3DDll = NULL; PD3D11CreateDevice = NULL; diff --git a/Source/Plugins/Plugin_VideoDX11/Src/D3DBlob.cpp b/Source/Plugins/Plugin_VideoDX11/Src/D3DBlob.cpp index 8a100b7bef..94655911fd 100644 --- a/Source/Plugins/Plugin_VideoDX11/Src/D3DBlob.cpp +++ b/Source/Plugins/Plugin_VideoDX11/Src/D3DBlob.cpp @@ -27,7 +27,7 @@ D3DBlob::D3DBlob(ID3D10Blob* d3dblob) : ref(1) { blob = d3dblob; data = (u8*)blob->GetBufferPointer(); - size = blob->GetBufferSize(); + size = (unsigned int)blob->GetBufferSize(); d3dblob->AddRef(); } diff --git a/Source/Plugins/Plugin_VideoDX11/Src/main.cpp b/Source/Plugins/Plugin_VideoDX11/Src/main.cpp index 08a98e651b..ad98dec91c 100644 --- a/Source/Plugins/Plugin_VideoDX11/Src/main.cpp +++ b/Source/Plugins/Plugin_VideoDX11/Src/main.cpp @@ -167,7 +167,7 @@ void DllConfig(void *_hParent) if (SUCCEEDED(hr)) hr = D3D::LoadD3D(); if (FAILED(hr)) { - if (!s_PluginInitialized) D3D::UnloadDXGI(); + D3D::UnloadDXGI(); return; } @@ -212,11 +212,8 @@ void DllConfig(void *_hParent) diag->ShowModal(); diag->Destroy(); - if (!s_PluginInitialized) - { - D3D::UnloadDXGI(); - D3D::UnloadD3D(); - } + D3D::UnloadDXGI(); + D3D::UnloadD3D(); #endif } diff --git a/Source/Plugins/Plugin_VideoDX9/Src/D3DBase.cpp b/Source/Plugins/Plugin_VideoDX9/Src/D3DBase.cpp index a3d6d2ec73..0f0c84266a 100644 --- a/Source/Plugins/Plugin_VideoDX9/Src/D3DBase.cpp +++ b/Source/Plugins/Plugin_VideoDX9/Src/D3DBase.cpp @@ -26,10 +26,12 @@ HINSTANCE hD3DXDll = NULL; D3DXSAVESURFACETOFILEATYPE PD3DXSaveSurfaceToFileA = NULL; D3DXSAVETEXTURETOFILEATYPE PD3DXSaveTextureToFileA = NULL; D3DXCOMPILESHADERTYPE PD3DXCompileShader = NULL; +int d3dx_dll_ref = 0; typedef IDirect3D9* (WINAPI* DIRECT3DCREATE9)(UINT); DIRECT3DCREATE9 PDirect3DCreate9 = NULL; HINSTANCE hD3DDll = NULL; +int d3d_dll_ref = 0; namespace D3D { @@ -93,6 +95,8 @@ bool IsATIDevice() HRESULT Init() { + if (d3d_dll_ref++ > 0) return S_OK; + hD3DDll = LoadLibraryA("d3d9.dll"); if (!hD3DDll) { @@ -105,15 +109,20 @@ HRESULT Init() // Create the D3D object, which is needed to create the D3DDevice. D3D = PDirect3DCreate9(D3D_SDK_VERSION); if (!D3D) + { + --d3d_dll_ref; return E_FAIL; + } Enumerate(); return S_OK; } void Shutdown() { - if(D3D) - D3D->Release(); + if (!d3d_dll_ref) return; + if (--d3d_dll_ref != 0) return; + + if (D3D) D3D->Release(); D3D = NULL; if (hD3DDll) FreeLibrary(hD3DDll); @@ -278,6 +287,8 @@ void Enumerate() // we're first trying to load the dll Dolphin was compiled with, otherwise the most up-to-date one HRESULT LoadD3DX9() { + if (d3dx_dll_ref++ > 0) return S_OK; + HRESULT hr = E_FAIL; hD3DXDll = LoadLibraryA(StringFromFormat("d3dx9_%d.dll", D3DX_SDK_VERSION).c_str()); if (hD3DXDll != NULL) @@ -326,6 +337,7 @@ HRESULT LoadD3DX9() return S_OK; fail: + --d3dx_dll_ref; FreeLibrary(hD3DXDll); PD3DXCompileShader = NULL; PD3DXSaveSurfaceToFileA = NULL; @@ -335,6 +347,9 @@ fail: void UnloadD3DX9() { + if (!d3dx_dll_ref) return; + if (--d3dx_dll_ref != 0) return; + FreeLibrary(hD3DXDll); PD3DXCompileShader = NULL; PD3DXSaveSurfaceToFileA = NULL; diff --git a/Source/Plugins/Plugin_VideoDX9/Src/main.cpp b/Source/Plugins/Plugin_VideoDX9/Src/main.cpp index c3d034d776..1fe17c4231 100644 --- a/Source/Plugins/Plugin_VideoDX9/Src/main.cpp +++ b/Source/Plugins/Plugin_VideoDX9/Src/main.cpp @@ -165,10 +165,7 @@ void DllConfig(void *_hParent) { #if defined(HAVE_WX) && HAVE_WX InitBackendInfo(); - - // If not initialized, only init D3D so we can enumerate resolutions. - if (!s_PluginInitialized) - D3D::Init(); + D3D::Init(); // adapters g_Config.backend_info.Adapters.clear(); @@ -190,8 +187,7 @@ void DllConfig(void *_hParent) diag->ShowModal(); diag->Destroy(); - if (!s_PluginInitialized) - D3D::Shutdown(); + D3D::Shutdown(); #endif }