From 4d54cad9bd5a8e84c21eba78aeb30c7697277b18 Mon Sep 17 00:00:00 2001 From: "Unknown W. Brackets" Date: Sun, 7 Sep 2014 10:38:49 -0700 Subject: [PATCH 1/6] d3d: Reduce D3DX leaks. --- GPU/Directx9/ShaderManagerDX9.cpp | 8 +++++++ GPU/Directx9/helper/global.cpp | 36 ++++++++++++++++++++----------- 2 files changed, 32 insertions(+), 12 deletions(-) diff --git a/GPU/Directx9/ShaderManagerDX9.cpp b/GPU/Directx9/ShaderManagerDX9.cpp index ea087efbc1..85bef42d7a 100644 --- a/GPU/Directx9/ShaderManagerDX9.cpp +++ b/GPU/Directx9/ShaderManagerDX9.cpp @@ -47,6 +47,8 @@ PSShader::PSShader(const char *code, bool useHWTransform) : failed_(false), useH if (!success) { failed_ = true; + if (shader) + shader->Release(); shader = NULL; } else { DEBUG_LOG(G3D, "Compiled shader:\n%s\n", (const char *)code); @@ -55,6 +57,8 @@ PSShader::PSShader(const char *code, bool useHWTransform) : failed_(false), useH PSShader::~PSShader() { pD3Ddevice->SetPixelShader(NULL); + if (constant) + constant->Release(); if (shader) shader->Release(); } @@ -70,6 +74,8 @@ VSShader::VSShader(const char *code, bool useHWTransform) : failed_(false), useH if (!success) { failed_ = true; + if (shader) + shader->Release(); shader = NULL; } else { DEBUG_LOG(G3D, "Compiled shader:\n%s\n", (const char *)code); @@ -78,6 +84,8 @@ VSShader::VSShader(const char *code, bool useHWTransform) : failed_(false), useH VSShader::~VSShader() { pD3Ddevice->SetVertexShader(NULL); + if (constant) + constant->Release(); if (shader) shader->Release(); } diff --git a/GPU/Directx9/helper/global.cpp b/GPU/Directx9/helper/global.cpp index 0308d0e6dc..3b5c197759 100644 --- a/GPU/Directx9/helper/global.cpp +++ b/GPU/Directx9/helper/global.cpp @@ -66,8 +66,6 @@ LPDIRECT3DVERTEXSHADER9 pFramebufferVertexShader = NULL; // Vertex Shader LPDIRECT3DPIXELSHADER9 pFramebufferPixelShader = NULL; // Pixel Shader bool CompilePixelShader(const char * code, LPDIRECT3DPIXELSHADER9 * pShader, LPD3DXCONSTANTTABLE * pShaderTable) { - LPD3DXCONSTANTTABLE shaderTable = *pShaderTable; - ID3DXBuffer* pShaderCode = NULL; ID3DXBuffer* pErrorMsg = NULL; @@ -85,9 +83,12 @@ bool CompilePixelShader(const char * code, LPDIRECT3DPIXELSHADER9 * pShader, LPD &pErrorMsg, pShaderTable); - if( FAILED(hr) ) - { + if (pErrorMsg) { OutputDebugStringA((CHAR*)pErrorMsg->GetBufferPointer()); + pErrorMsg->Release(); + } + + if (FAILED(hr)) { DebugBreak(); return false; } @@ -102,8 +103,6 @@ bool CompilePixelShader(const char * code, LPDIRECT3DPIXELSHADER9 * pShader, LPD } bool CompileVertexShader(const char * code, LPDIRECT3DVERTEXSHADER9 * pShader, LPD3DXCONSTANTTABLE * pShaderTable) { - LPD3DXCONSTANTTABLE shaderTable = *pShaderTable; - ID3DXBuffer* pShaderCode = NULL; ID3DXBuffer* pErrorMsg = NULL; @@ -121,9 +120,12 @@ bool CompileVertexShader(const char * code, LPDIRECT3DVERTEXSHADER9 * pShader, L &pErrorMsg, pShaderTable); - if( FAILED(hr) ) - { + if (pErrorMsg) { OutputDebugStringA((CHAR*)pErrorMsg->GetBufferPointer()); + pErrorMsg->Release(); + } + + if (FAILED(hr)) { DebugBreak(); return false; } @@ -154,9 +156,12 @@ void CompileShaders() { &pErrorMsg, NULL); - if( FAILED(hr) ) - { + if (pErrorMsg) { OutputDebugStringA((CHAR*)pErrorMsg->GetBufferPointer()); + pErrorMsg->Release(); + } + + if (FAILED(hr)) { DebugBreak(); } @@ -165,6 +170,10 @@ void CompileShaders() { &pFramebufferVertexShader ); pShaderCode->Release(); + if (pErrorMsg) { + OutputDebugStringA((CHAR*)pErrorMsg->GetBufferPointer()); + pErrorMsg->Release(); + } // Compile pixel shader. hr = dyn_D3DXCompileShader(pscode, @@ -178,9 +187,12 @@ void CompileShaders() { &pErrorMsg, NULL); - if( FAILED(hr) ) - { + if (pErrorMsg) { OutputDebugStringA((CHAR*)pErrorMsg->GetBufferPointer()); + pErrorMsg->Release(); + } + + if (FAILED(hr)) { DebugBreak(); } From 564ae9f184b6a44f36afae45efaff70727bd18ef Mon Sep 17 00:00:00 2001 From: "Unknown W. Brackets" Date: Sun, 7 Sep 2014 11:01:19 -0700 Subject: [PATCH 2/6] d3d: Report shader compilation failures. --- GPU/Directx9/ShaderManagerDX9.cpp | 33 +++++++++++++++++++++++++++++-- GPU/Directx9/helper/global.cpp | 18 +++++++++++------ GPU/Directx9/helper/global.h | 5 +++-- 3 files changed, 46 insertions(+), 10 deletions(-) diff --git a/GPU/Directx9/ShaderManagerDX9.cpp b/GPU/Directx9/ShaderManagerDX9.cpp index 85bef42d7a..abb63a8488 100644 --- a/GPU/Directx9/ShaderManagerDX9.cpp +++ b/GPU/Directx9/ShaderManagerDX9.cpp @@ -21,6 +21,7 @@ #include #include "helper/global.h" +#include "base/logging.h" #include "math/lin/matrix4x4.h" #include "util/text/utf8.h" @@ -42,8 +43,22 @@ PSShader::PSShader(const char *code, bool useHWTransform) : failed_(false), useH OutputDebugString(ConvertUTF8ToWString(code).c_str()); #endif bool success; + std::string errorMessage; - success = CompilePixelShader(code, &shader, &constant); + success = CompilePixelShader(code, &shader, &constant, errorMessage); + + if (!errorMessage.empty()) { + if (success) { + ERROR_LOG(G3D, "Warnings in shader compilation!"); + } else { + ERROR_LOG(G3D, "Error in shader compilation!"); + } + ERROR_LOG(G3D, "Messages: %s", errorMessage.c_str()); + ERROR_LOG(G3D, "Shader source:\n%s", code); + OutputDebugStringUTF8("Messages:\n"); + OutputDebugStringUTF8(errorMessage.c_str()); + Reporting::ReportMessage("D3D error in shader compilation: info: %s / code: %s", errorMessage.c_str(), code); + } if (!success) { failed_ = true; @@ -69,8 +84,22 @@ VSShader::VSShader(const char *code, bool useHWTransform) : failed_(false), useH OutputDebugString(ConvertUTF8ToWString(code).c_str()); #endif bool success; + std::string errorMessage; - success = CompileVertexShader(code, &shader, &constant); + success = CompileVertexShader(code, &shader, &constant, errorMessage); + + if (!errorMessage.empty()) { + if (success) { + ERROR_LOG(G3D, "Warnings in shader compilation!"); + } else { + ERROR_LOG(G3D, "Error in shader compilation!"); + } + ERROR_LOG(G3D, "Messages: %s", errorMessage.c_str()); + ERROR_LOG(G3D, "Shader source:\n%s", code); + OutputDebugStringUTF8("Messages:\n"); + OutputDebugStringUTF8(errorMessage.c_str()); + Reporting::ReportMessage("D3D error in shader compilation: info: %s / code: %s", errorMessage.c_str(), code); + } if (!success) { failed_ = true; diff --git a/GPU/Directx9/helper/global.cpp b/GPU/Directx9/helper/global.cpp index 3b5c197759..26fa9bf267 100644 --- a/GPU/Directx9/helper/global.cpp +++ b/GPU/Directx9/helper/global.cpp @@ -65,7 +65,7 @@ static const D3DVERTEXELEMENT9 SoftTransVertexElements[] = LPDIRECT3DVERTEXSHADER9 pFramebufferVertexShader = NULL; // Vertex Shader LPDIRECT3DPIXELSHADER9 pFramebufferPixelShader = NULL; // Pixel Shader -bool CompilePixelShader(const char * code, LPDIRECT3DPIXELSHADER9 * pShader, LPD3DXCONSTANTTABLE * pShaderTable) { +bool CompilePixelShader(const char *code, LPDIRECT3DPIXELSHADER9 *pShader, LPD3DXCONSTANTTABLE *pShaderTable, std::string &errorMessage) { ID3DXBuffer* pShaderCode = NULL; ID3DXBuffer* pErrorMsg = NULL; @@ -84,12 +84,15 @@ bool CompilePixelShader(const char * code, LPDIRECT3DPIXELSHADER9 * pShader, LPD pShaderTable); if (pErrorMsg) { - OutputDebugStringA((CHAR*)pErrorMsg->GetBufferPointer()); + errorMessage = (CHAR *)pErrorMsg->GetBufferPointer(); pErrorMsg->Release(); + } else { + errorMessage = ""; } if (FAILED(hr)) { - DebugBreak(); + if (pShaderCode) + pShaderCode->Release(); return false; } @@ -102,7 +105,7 @@ bool CompilePixelShader(const char * code, LPDIRECT3DPIXELSHADER9 * pShader, LPD return true; } -bool CompileVertexShader(const char * code, LPDIRECT3DVERTEXSHADER9 * pShader, LPD3DXCONSTANTTABLE * pShaderTable) { +bool CompileVertexShader(const char *code, LPDIRECT3DVERTEXSHADER9 *pShader, LPD3DXCONSTANTTABLE *pShaderTable, std::string &errorMessage) { ID3DXBuffer* pShaderCode = NULL; ID3DXBuffer* pErrorMsg = NULL; @@ -121,12 +124,15 @@ bool CompileVertexShader(const char * code, LPDIRECT3DVERTEXSHADER9 * pShader, L pShaderTable); if (pErrorMsg) { - OutputDebugStringA((CHAR*)pErrorMsg->GetBufferPointer()); + errorMessage = (CHAR *)pErrorMsg->GetBufferPointer(); pErrorMsg->Release(); + } else { + errorMessage = ""; } if (FAILED(hr)) { - DebugBreak(); + if (pShaderCode) + pShaderCode->Release(); return false; } diff --git a/GPU/Directx9/helper/global.h b/GPU/Directx9/helper/global.h index d863dddbfd..2dff7267bd 100644 --- a/GPU/Directx9/helper/global.h +++ b/GPU/Directx9/helper/global.h @@ -9,6 +9,7 @@ #define D3DFMT(x) x #endif +#include #include struct ID3DXConstantTable; @@ -24,8 +25,8 @@ extern IDirect3DVertexDeclaration9* pFramebufferVertexDecl; extern IDirect3DVertexDeclaration9* pSoftVertexDecl; void CompileShaders(); -bool CompilePixelShader(const char * code, LPDIRECT3DPIXELSHADER9 * pShader, ID3DXConstantTable **pShaderTable); -bool CompileVertexShader(const char * code, LPDIRECT3DVERTEXSHADER9 * pShader, ID3DXConstantTable **pShaderTable); +bool CompilePixelShader(const char *code, LPDIRECT3DPIXELSHADER9 *pShader, ID3DXConstantTable **pShaderTable, std::string &errorMessage); +bool CompileVertexShader(const char *code, LPDIRECT3DVERTEXSHADER9 *pShader, ID3DXConstantTable **pShaderTable, std::string &errorMessage); void DirectxInit(HWND window); #define D3DBLEND_UNK D3DSTENCILOP_FORCE_DWORD From 34b1d13c29bf3d5f1488e430681d35cf426d36ba Mon Sep 17 00:00:00 2001 From: "Unknown W. Brackets" Date: Sun, 7 Sep 2014 13:07:12 -0700 Subject: [PATCH 3/6] d3d: Release vertex decls on shutdown. --- GPU/Directx9/TransformPipelineDX9.cpp | 28 ++++++++++++++------------- GPU/Directx9/TransformPipelineDX9.h | 2 ++ 2 files changed, 17 insertions(+), 13 deletions(-) diff --git a/GPU/Directx9/TransformPipelineDX9.cpp b/GPU/Directx9/TransformPipelineDX9.cpp index 354b15d984..42f96f7bef 100644 --- a/GPU/Directx9/TransformPipelineDX9.cpp +++ b/GPU/Directx9/TransformPipelineDX9.cpp @@ -177,6 +177,10 @@ TransformDrawEngineDX9::~TransformDrawEngineDX9() { FreeMemoryPages(transformed, TRANSFORMED_VERTEX_BUFFER_SIZE); FreeMemoryPages(transformedExpanded, 3 * TRANSFORMED_VERTEX_BUFFER_SIZE); + for (auto decl = vertexDeclMap_.begin(); decl != vertexDeclMap_.end(); ++decl) { + decl->second->Release(); + } + delete [] quadIndices_; for (auto iter = decoderMap_.begin(); iter != decoderMap_.end(); iter++) { @@ -229,10 +233,6 @@ static void VertexAttribSetup(D3DVERTEXELEMENT9 * VertexElement, u8 fmt, u8 offs VertexElement->UsageIndex = usage_index; } -static IDirect3DVertexDeclaration9* pHardwareVertexDecl = NULL; -static std::map vertexDeclMap; -static D3DVERTEXELEMENT9 VertexElements[8]; - // TODO: Use VBO and get rid of the vertexData pointers - with that, we will supply only offsets static void LogDecFmtForDraw(const DecVtxFormat &decFmt) { // Vertices Elements orders @@ -269,12 +269,12 @@ static void LogDecFmtForDraw(const DecVtxFormat &decFmt) { //pD3Ddevice->SetRenderState(D3DRS_FILLMODE, D3DFILL_WIREFRAME); } -static void SetupDecFmtForDraw(LinkedShaderDX9 *program, const DecVtxFormat &decFmt, u32 pspFmt) { - auto vertexDeclCached = vertexDeclMap.find(pspFmt); +IDirect3DVertexDeclaration9 *TransformDrawEngineDX9::SetupDecFmtForDraw(LinkedShaderDX9 *program, const DecVtxFormat &decFmt, u32 pspFmt) { + auto vertexDeclCached = vertexDeclMap_.find(pspFmt); - if (vertexDeclCached==vertexDeclMap.end()) { - D3DVERTEXELEMENT9 * VertexElement = &VertexElements[0]; - int offset = 0; + if (vertexDeclCached == vertexDeclMap_.end()) { + D3DVERTEXELEMENT9 VertexElements[8]; + D3DVERTEXELEMENT9 *VertexElement = &VertexElements[0]; // Vertices Elements orders // WEIGHT @@ -320,7 +320,8 @@ static void SetupDecFmtForDraw(LinkedShaderDX9 *program, const DecVtxFormat &dec D3DVERTEXELEMENT9 end = D3DDECL_END(); memcpy(VertexElement, &end, sizeof(D3DVERTEXELEMENT9)); - // Create declaration + // Create declaration + IDirect3DVertexDeclaration9 *pHardwareVertexDecl; HRESULT hr = pD3Ddevice->CreateVertexDeclaration( VertexElements, &pHardwareVertexDecl ); if (FAILED(hr)) { // Log @@ -329,10 +330,11 @@ static void SetupDecFmtForDraw(LinkedShaderDX9 *program, const DecVtxFormat &dec } // Add it to map - vertexDeclMap[pspFmt] = pHardwareVertexDecl; + vertexDeclMap_[pspFmt] = pHardwareVertexDecl; + return pHardwareVertexDecl; } else { // Set it from map - pHardwareVertexDecl = vertexDeclCached->second; + return vertexDeclCached->second; } } @@ -1218,7 +1220,7 @@ rotateVBO: DEBUG_LOG(G3D, "Flush prim %i! %i verts in one go", prim, vertexCount); - SetupDecFmtForDraw(program, dec_->GetDecVtxFmt(), dec_->VertexType()); + IDirect3DVertexDeclaration9 *pHardwareVertexDecl = SetupDecFmtForDraw(program, dec_->GetDecVtxFmt(), dec_->VertexType()); if (pHardwareVertexDecl) { pD3Ddevice->SetVertexDeclaration(pHardwareVertexDecl); diff --git a/GPU/Directx9/TransformPipelineDX9.h b/GPU/Directx9/TransformPipelineDX9.h index 77d1ab0d93..05760e5e2a 100644 --- a/GPU/Directx9/TransformPipelineDX9.h +++ b/GPU/Directx9/TransformPipelineDX9.h @@ -142,6 +142,7 @@ private: void SoftwareTransformAndDraw(int prim, u8 *decoded, LinkedShaderDX9 *program, int vertexCount, u32 vertexType, void *inds, int indexType, const DecVtxFormat &decVtxFormat, int maxIndex); void ApplyDrawState(int prim); bool IsReallyAClear(int numVerts) const; + IDirect3DVertexDeclaration9 *SetupDecFmtForDraw(LinkedShaderDX9 *program, const DecVtxFormat &decFmt, u32 pspFmt); // Preprocessing for spline/bezier u32 NormalizeVertices(u8 *outPtr, u8 *bufPtr, const u8 *inPtr, VertexDecoderDX9 *dec, int lowerBound, int upperBound, u32 vertType); @@ -184,6 +185,7 @@ private: TransformedVertex *transformedExpanded; std::map vai_; + std::map vertexDeclMap_; // Fixed index buffer for easy quad generation from spline/bezier u16 *quadIndices_; From 858df90dff1f5c192d0860e24a6acaa8bd297f6b Mon Sep 17 00:00:00 2001 From: "Unknown W. Brackets" Date: Sun, 7 Sep 2014 13:15:39 -0700 Subject: [PATCH 4/6] d3d: Release shaders and fbos on shutdown. --- GPU/Directx9/helper/fbo.h | 1 + GPU/Directx9/helper/global.cpp | 14 ++++++++++++++ GPU/Directx9/helper/global.h | 1 + Windows/D3D9Base.cpp | 2 ++ 4 files changed, 18 insertions(+) diff --git a/GPU/Directx9/helper/fbo.h b/GPU/Directx9/helper/fbo.h index 4228bc2c5e..4fd9ae810c 100644 --- a/GPU/Directx9/helper/fbo.h +++ b/GPU/Directx9/helper/fbo.h @@ -41,5 +41,6 @@ void * fbo_get_rtt(FBO *fbo); // To get default depth and rt surface void fbo_init(); +void fbo_shutdown(); }; \ No newline at end of file diff --git a/GPU/Directx9/helper/global.cpp b/GPU/Directx9/helper/global.cpp index 26fa9bf267..e8fa5fa70e 100644 --- a/GPU/Directx9/helper/global.cpp +++ b/GPU/Directx9/helper/global.cpp @@ -214,6 +214,20 @@ void CompileShaders() { pD3Ddevice->CreateVertexDeclaration( SoftTransVertexElements, &pSoftVertexDecl ); } +void DestroyShaders() { + if (pFramebufferVertexShader) { + pFramebufferVertexShader->Release(); + } + if (pFramebufferPixelShader) { + pFramebufferPixelShader->Release(); + } + if (pFramebufferVertexDecl) { + pFramebufferVertexDecl->Release(); + } + if (pSoftVertexDecl) { + pSoftVertexDecl->Release(); + } +} bool useVsync = false; diff --git a/GPU/Directx9/helper/global.h b/GPU/Directx9/helper/global.h index 2dff7267bd..abb52aa4bc 100644 --- a/GPU/Directx9/helper/global.h +++ b/GPU/Directx9/helper/global.h @@ -27,6 +27,7 @@ extern IDirect3DVertexDeclaration9* pSoftVertexDecl; void CompileShaders(); bool CompilePixelShader(const char *code, LPDIRECT3DPIXELSHADER9 *pShader, ID3DXConstantTable **pShaderTable, std::string &errorMessage); bool CompileVertexShader(const char *code, LPDIRECT3DVERTEXSHADER9 *pShader, ID3DXConstantTable **pShaderTable, std::string &errorMessage); +void DestroyShaders(); void DirectxInit(HWND window); #define D3DBLEND_UNK D3DSTENCILOP_FORCE_DWORD diff --git a/Windows/D3D9Base.cpp b/Windows/D3D9Base.cpp index e8af6bef05..ab26271646 100644 --- a/Windows/D3D9Base.cpp +++ b/Windows/D3D9Base.cpp @@ -116,6 +116,8 @@ void D3D9_Resize(HWND window) { } void D3D9_Shutdown() { + DX9::DestroyShaders(); + DX9::fbo_shutdown(); device->EndScene(); device->Release(); d3d->Release(); From 9f3deddef63f58d557700b59d721b9ed0934c9b1 Mon Sep 17 00:00:00 2001 From: "Unknown W. Brackets" Date: Sun, 7 Sep 2014 13:31:33 -0700 Subject: [PATCH 5/6] d3d: Release on shutdown in headless. Could sync it up, just want to be able to run tests without major leaks. --- Windows/D3D9Base.cpp | 2 ++ headless/WindowsHeadlessHostDx9.cpp | 7 +++++++ 2 files changed, 9 insertions(+) diff --git a/Windows/D3D9Base.cpp b/Windows/D3D9Base.cpp index ab26271646..4823a1ebaf 100644 --- a/Windows/D3D9Base.cpp +++ b/Windows/D3D9Base.cpp @@ -121,5 +121,7 @@ void D3D9_Shutdown() { device->EndScene(); device->Release(); d3d->Release(); + DX9::pD3Ddevice = NULL; + DX9::pD3Ddevice = NULL; hWnd = NULL; } diff --git a/headless/WindowsHeadlessHostDx9.cpp b/headless/WindowsHeadlessHostDx9.cpp index 37598ea3de..2527cee166 100644 --- a/headless/WindowsHeadlessHostDx9.cpp +++ b/headless/WindowsHeadlessHostDx9.cpp @@ -25,6 +25,7 @@ #include "base/logging.h" #include "thin3d/d3dx9_loader.h" #include "GPU/Directx9/helper/global.h" +#include "GPU/Directx9/helper/fbo.h" #include "file/vfs.h" #include "file/zip_read.h" @@ -80,6 +81,12 @@ bool WindowsHeadlessHostDx9::InitGL(std::string *error_message) void WindowsHeadlessHostDx9::ShutdownGL() { + DX9::DestroyShaders(); + DX9::fbo_shutdown(); + DX9::pD3Ddevice->EndScene(); + DX9::pD3Ddevice->Release(); + DX9::pD3Ddevice = NULL; + hWnd = NULL; } bool WindowsHeadlessHostDx9::ResizeGL() From 17ca3b8b36ffbace38e179097e7d7b5dedada56c Mon Sep 17 00:00:00 2001 From: "Unknown W. Brackets" Date: Sun, 7 Sep 2014 14:07:34 -0700 Subject: [PATCH 6/6] Update native (reduce d3d9 leaks.) --- native | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/native b/native index 69bad8ba74..d5711f360d 160000 --- a/native +++ b/native @@ -1 +1 @@ -Subproject commit 69bad8ba74dda4745f4ad5d12fe0cfdbf4230f1a +Subproject commit d5711f360dc8789d65db9f80b9502a9ed26dbe99