diff --git a/Source/Plugins/Plugin_VideoDX11/Src/D3DBase.cpp b/Source/Plugins/Plugin_VideoDX11/Src/D3DBase.cpp index cd55ac2b11..1aaed516df 100644 --- a/Source/Plugins/Plugin_VideoDX11/Src/D3DBase.cpp +++ b/Source/Plugins/Plugin_VideoDX11/Src/D3DBase.cpp @@ -24,11 +24,6 @@ #include "XFStructs.h" #include "StringUtil.h" -#include -#include -#include -using namespace std; - HINSTANCE hD3DXDll = NULL; D3DX11COMPILEFROMMEMORYTYPE PD3DX11CompileFromMemory = NULL; D3DX11FILTERTEXTURETYPE PD3DX11FilterTexture = NULL; @@ -167,22 +162,22 @@ HRESULT Create(HWND wnd) return E_FAIL; } SetDebugObjectName((ID3D11DeviceChild*)context, "device context"); - factory->Release(); - output->Release(); - adapter->Release(); + SAFE_RELEASE(factory); + SAFE_RELEASE(output); + SAFE_RELEASE(adapter); ID3D11Texture2D* buf; hr = swapchain->GetBuffer(0, IID_ID3D11Texture2D, (void**)&buf); if (FAILED(hr)) { MessageBox(wnd, _T("Failed to get swapchain buffer"), _T("Dolphin Direct3D 11 plugin"), MB_OK | MB_ICONERROR); - device->Release(); - context->Release(); - swapchain->Release(); + SAFE_RELEASE(device); + SAFE_RELEASE(context); + SAFE_RELEASE(swapchain); return E_FAIL; } backbuf = new D3DTexture2D(buf, D3D11_BIND_RENDER_TARGET); - buf->Release(); + SAFE_RELEASE(buf); CHECK(backbuf!=NULL, "Create back buffer texture"); SetDebugObjectName((ID3D11DeviceChild*)backbuf->GetTex(), "backbuffer texture"); SetDebugObjectName((ID3D11DeviceChild*)backbuf->GetRTV(), "backbuffer render target view"); @@ -255,8 +250,10 @@ unsigned int GetMaxTextureSize() case D3D_FEATURE_LEVEL_9_2: case D3D_FEATURE_LEVEL_9_1: - default: return 2048; + + default: + return 0; } } @@ -278,13 +275,13 @@ void Reset() if (FAILED(hr)) { MessageBox(hWnd, _T("Failed to get swapchain buffer"), _T("Dolphin Direct3D 11 plugin"), MB_OK | MB_ICONERROR); - device->Release(); - context->Release(); - swapchain->Release(); + SAFE_RELEASE(device); + SAFE_RELEASE(context); + SAFE_RELEASE(swapchain); return; } backbuf = new D3DTexture2D(buf, D3D11_BIND_RENDER_TARGET); - buf->Release(); + SAFE_RELEASE(buf); CHECK(backbuf!=NULL, "Create back buffer texture"); SetDebugObjectName((ID3D11DeviceChild*)backbuf->GetTex(), "backbuffer texture"); SetDebugObjectName((ID3D11DeviceChild*)backbuf->GetRTV(), "backbuffer render target view"); diff --git a/Source/Plugins/Plugin_VideoDX11/Src/D3DTexture.cpp b/Source/Plugins/Plugin_VideoDX11/Src/D3DTexture.cpp index 1ad71a802c..5003a9116b 100644 --- a/Source/Plugins/Plugin_VideoDX11/Src/D3DTexture.cpp +++ b/Source/Plugins/Plugin_VideoDX11/Src/D3DTexture.cpp @@ -66,7 +66,7 @@ D3DTexture2D* D3DTexture2D::Create(unsigned int width, unsigned int height, D3D1 } D3DTexture2D* ret = new D3DTexture2D(pTexture, bind); - pTexture->Release(); + SAFE_RELEASE(pTexture); return ret; } @@ -77,7 +77,7 @@ void D3DTexture2D::AddRef() UINT D3DTexture2D::Release() { - ref--; + --ref; if (ref == 0) { delete this; @@ -106,8 +106,8 @@ D3DTexture2D::D3DTexture2D(ID3D11Texture2D* texptr, D3D11_BIND_FLAG bind, D3DTexture2D::~D3DTexture2D() { - if (srv) srv->Release(); - if (rtv) rtv->Release(); - if (dsv) dsv->Release(); - tex->Release(); + SAFE_RELEASE(srv); + SAFE_RELEASE(rtv); + SAFE_RELEASE(dsv); + SAFE_RELEASE(tex); } diff --git a/Source/Plugins/Plugin_VideoDX11/Src/NativeVertexFormat.cpp b/Source/Plugins/Plugin_VideoDX11/Src/NativeVertexFormat.cpp index 192a10c983..0ef85c2840 100644 --- a/Source/Plugins/Plugin_VideoDX11/Src/NativeVertexFormat.cpp +++ b/Source/Plugins/Plugin_VideoDX11/Src/NativeVertexFormat.cpp @@ -28,9 +28,6 @@ #include "CPMemory.h" #include "NativeVertexFormat.h" -#include -using std::string; - class D3DVertexFormat : public NativeVertexFormat { D3D11_INPUT_ELEMENT_DESC m_elems[32]; diff --git a/Source/Plugins/Plugin_VideoDX9/Plugin_VideoDX9.vcproj b/Source/Plugins/Plugin_VideoDX9/Plugin_VideoDX9.vcproj index 71aab207a7..bc1f1f10da 100644 --- a/Source/Plugins/Plugin_VideoDX9/Plugin_VideoDX9.vcproj +++ b/Source/Plugins/Plugin_VideoDX9/Plugin_VideoDX9.vcproj @@ -93,13 +93,12 @@ = 24; --num) + { + hD3DXDll = LoadLibraryA(StringFromFormat("d3dx9_%d.dll", num).c_str()); + if (hD3DXDll != NULL) + { + NOTICE_LOG(VIDEO, "Successfully loaded %s. If you're having trouble, try updating your DX runtime first.", StringFromFormat("d3dx9_%d.dll", num).c_str()); + hr = S_OK; + break; + } + } + if (FAILED(hr)) + { + MessageBoxA(NULL, "Failed to load any D3DX9 dll, update your DX9 runtime, please", "Critical error", MB_OK | MB_ICONERROR); + return hr; + } + } + PD3DXCompileShader = (D3DXCOMPILESHADERTYPE)GetProcAddress(hD3DXDll, "D3DXCompileShader"); + if (PD3DXCompileShader == NULL) + { + MessageBoxA(NULL, "GetProcAddress failed for D3DXCompileShader!", "Critical error", MB_OK | MB_ICONERROR); + goto fail; + } + + PD3DXSaveSurfaceToFileA = (D3DXSAVESURFACETOFILEATYPE)GetProcAddress(hD3DXDll, "D3DXSaveSurfaceToFileA"); + if (PD3DXSaveSurfaceToFileA == NULL) + { + MessageBoxA(NULL, "GetProcAddress failed for D3DXSaveSurfaceToFileA!", "Critical error", MB_OK | MB_ICONERROR); + goto fail; + } + + PD3DXSaveTextureToFileA = (D3DXSAVETEXTURETOFILEATYPE)GetProcAddress(hD3DXDll, "D3DXSaveTextureToFileA"); + if (PD3DXSaveTextureToFileA == NULL) + { + MessageBoxA(NULL, "GetProcAddress failed for D3DXSaveTextureToFileA!", "Critical error", MB_OK | MB_ICONERROR); + goto fail; + } + return S_OK; + +fail: + FreeLibrary(hD3DXDll); + PD3DXCompileShader = NULL; + PD3DXSaveSurfaceToFileA = NULL; + PD3DXSaveTextureToFileA = NULL; + return E_FAIL; +} + +void UnloadD3DX9() +{ + FreeLibrary(hD3DXDll); + PD3DXCompileShader = NULL; + PD3DXSaveSurfaceToFileA = NULL; + PD3DXSaveTextureToFileA = NULL; +} + HRESULT Create(int adapter, HWND wnd, int _resolution, int aa_mode, bool auto_depth) { hWnd = wnd; @@ -257,7 +332,11 @@ HRESULT Create(int adapter, HWND wnd, int _resolution, int aa_mode, bool auto_de resolution = _resolution; auto_depth_stencil = auto_depth; cur_adapter = adapter; - D3DPRESENT_PARAMETERS d3dpp; + D3DPRESENT_PARAMETERS d3dpp; + + HRESULT hr = LoadD3DX9(); + if (FAILED(hr)) return hr; + InitPP(adapter, resolution, aa_mode, &d3dpp); if (FAILED(D3D->CreateDevice( @@ -303,6 +382,8 @@ HRESULT Create(int adapter, HWND wnd, int _resolution, int aa_mode, bool auto_de void Close() { + UnloadD3DX9(); + if (back_buffer_z) back_buffer_z->Release(); back_buffer_z = NULL; diff --git a/Source/Plugins/Plugin_VideoDX9/Src/D3DBase.h b/Source/Plugins/Plugin_VideoDX9/Src/D3DBase.h index 3472ea7c78..533978a46c 100644 --- a/Source/Plugins/Plugin_VideoDX9/Src/D3DBase.h +++ b/Source/Plugins/Plugin_VideoDX9/Src/D3DBase.h @@ -141,4 +141,19 @@ int GetNumAdapters(); } // namespace + +// Used to not require the SDK and runtime versions to match: +// Linking with d3dx9.lib makes the most recent d3dx9_xx.dll of the +// compiler's SDK an actually unnecessary requirement. +// Add any d3dx9 functions which you want to use here and load them in LoadD3DX9() +typedef HRESULT (WINAPI* D3DXSAVESURFACETOFILEATYPE)(LPCSTR, D3DXIMAGE_FILEFORMAT, LPDIRECT3DSURFACE9, CONST PALETTEENTRY*, CONST RECT*); +typedef HRESULT (WINAPI* D3DXSAVETEXTURETOFILEATYPE)(LPCSTR, D3DXIMAGE_FILEFORMAT, LPDIRECT3DBASETEXTURE9, CONST PALETTEENTRY*); +typedef HRESULT (WINAPI* D3DXCOMPILESHADERTYPE)(LPCSTR, UINT, CONST D3DXMACRO*, LPD3DXINCLUDE, LPCSTR, LPCSTR, DWORD, LPD3DXBUFFER*, LPD3DXBUFFER*, LPD3DXCONSTANTTABLE*); + +extern D3DXSAVESURFACETOFILEATYPE PD3DXSaveSurfaceToFileA; +extern D3DXSAVETEXTURETOFILEATYPE PD3DXSaveTextureToFileA; +extern D3DXCOMPILESHADERTYPE PD3DXCompileShader; + + + #endif diff --git a/Source/Plugins/Plugin_VideoDX9/Src/D3DShader.cpp b/Source/Plugins/Plugin_VideoDX9/Src/D3DShader.cpp index dcbee559b8..c93afda6fc 100644 --- a/Source/Plugins/Plugin_VideoDX9/Src/D3DShader.cpp +++ b/Source/Plugins/Plugin_VideoDX9/Src/D3DShader.cpp @@ -40,7 +40,7 @@ bool CompileVertexShader(const char *code, int len, u8 **bytecode, int *bytecode //try to compile LPD3DXBUFFER shaderBuffer = 0; LPD3DXBUFFER errorBuffer = 0; - HRESULT hr = D3DXCompileShader(code, len, 0, 0, "main", D3D::VertexShaderVersionString(), + HRESULT hr = PD3DXCompileShader(code, len, 0, 0, "main", D3D::VertexShaderVersionString(), 0, &shaderBuffer, &errorBuffer, 0); if (FAILED(hr)) { @@ -89,7 +89,7 @@ bool CompilePixelShader(const char *code, int len, u8 **bytecode, int *bytecodel // Someone: // For some reason, I had this kind of errors : "Shader uses texture addressing operations // in a dependency chain that is too complex for the target shader model (ps_2_0) to handle." - HRESULT hr = D3DXCompileShader(code, len, 0, 0, "main", D3D::PixelShaderVersionString(), + HRESULT hr = PD3DXCompileShader(code, len, 0, 0, "main", D3D::PixelShaderVersionString(), 0, &shaderBuffer, &errorBuffer, 0); if (FAILED(hr)) diff --git a/Source/Plugins/Plugin_VideoDX9/Src/Render.cpp b/Source/Plugins/Plugin_VideoDX9/Src/Render.cpp index 15dc7bde67..4c941c9768 100644 --- a/Source/Plugins/Plugin_VideoDX9/Src/Render.cpp +++ b/Source/Plugins/Plugin_VideoDX9/Src/Render.cpp @@ -1116,7 +1116,7 @@ void Renderer::Swap(u32 xfbAddr, FieldType field, u32 fbWidth, u32 fbHeight,cons { PanicAlert("Error dumping surface data."); } - hr = D3DXSaveSurfaceToFileA(s_sScreenshotName, D3DXIFF_PNG, ScreenShootMEMSurface, NULL, dst_rect.AsRECT()); + hr = PD3DXSaveSurfaceToFileA(s_sScreenshotName, D3DXIFF_PNG, ScreenShootMEMSurface, NULL, dst_rect.AsRECT()); if(FAILED(hr)) { PanicAlert("Error saving screen."); diff --git a/Source/Plugins/Plugin_VideoDX9/Src/TextureCache.cpp b/Source/Plugins/Plugin_VideoDX9/Src/TextureCache.cpp index 113ab8e64c..4da1000df6 100644 --- a/Source/Plugins/Plugin_VideoDX9/Src/TextureCache.cpp +++ b/Source/Plugins/Plugin_VideoDX9/Src/TextureCache.cpp @@ -388,7 +388,7 @@ TextureCache::TCacheEntry *TextureCache::Load(int stage, u32 address, int width, sprintf(szTemp, "%s/%s_%08x_%i.png", szDir, uniqueId, texHash, tex_format); if (!File::Exists(szTemp)) - D3DXSaveTextureToFileA(szTemp,D3DXIFF_PNG,entry.texture,0); + PD3DXSaveTextureToFileA(szTemp,D3DXIFF_PNG,entry.texture,0); } INCSTAT(stats.numTexturesCreated);