From 2fabe3e1c52a6a5608bfaaf767da8a978b97e253 Mon Sep 17 00:00:00 2001 From: Henrik Rydgard Date: Fri, 17 Feb 2017 19:22:41 +0100 Subject: [PATCH] Fix a number of resource leaks, both D3D11-only and general. Additionally, fix D3D11 BlitFramebuffer --- GPU/Common/FramebufferCommon.cpp | 4 +- GPU/D3D11/D3D11Util.cpp | 2 + GPU/D3D11/DrawEngineD3D11.cpp | 55 +++++++++++++-------------- GPU/D3D11/FramebufferManagerD3D11.cpp | 4 +- GPU/D3D11/FramebufferManagerD3D11.h | 7 ---- GPU/D3D11/ShaderManagerD3D11.cpp | 3 ++ GPU/Software/SoftGpu.cpp | 4 ++ UI/NativeApp.cpp | 2 + UI/TextureUtil.h | 4 +- ext/native/ui/ui_context.cpp | 1 + 10 files changed, 47 insertions(+), 39 deletions(-) diff --git a/GPU/Common/FramebufferCommon.cpp b/GPU/Common/FramebufferCommon.cpp index 602a5624af..a20d11828a 100644 --- a/GPU/Common/FramebufferCommon.cpp +++ b/GPU/Common/FramebufferCommon.cpp @@ -505,7 +505,7 @@ VirtualFramebuffer *FramebufferManagerCommon::DoSetRenderFrameBuffer(const Frame void FramebufferManagerCommon::DestroyFramebuf(VirtualFramebuffer *v) { textureCache_->NotifyFramebuffer(v->fb_address, v, NOTIFY_FB_DESTROYED); if (v->fbo) { - delete v->fbo; + v->fbo->Release(); v->fbo = nullptr; } @@ -1028,7 +1028,7 @@ void FramebufferManagerCommon::DecimateFBOs() { for (auto it = tempFBOs_.begin(); it != tempFBOs_.end(); ) { int age = frameLastFramebufUsed_ - it->second.last_frame_used; if (age > FBO_OLD_AGE) { - delete it->second.fbo; + it->second.fbo->Release(); tempFBOs_.erase(it++); } else { ++it; diff --git a/GPU/D3D11/D3D11Util.cpp b/GPU/D3D11/D3D11Util.cpp index 12069a17b8..af83807c70 100644 --- a/GPU/D3D11/D3D11Util.cpp +++ b/GPU/D3D11/D3D11Util.cpp @@ -116,6 +116,8 @@ void StockObjectsD3D11::Destroy() { rasterStateNoCull->Release(); samplerPoint2DWrap->Release(); samplerLinear2DWrap->Release(); + samplerPoint2DClamp->Release(); + samplerLinear2DClamp->Release(); } StockObjectsD3D11 stockD3D11; \ No newline at end of file diff --git a/GPU/D3D11/DrawEngineD3D11.cpp b/GPU/D3D11/DrawEngineD3D11.cpp index 89bb6f9103..e710f5fc01 100644 --- a/GPU/D3D11/DrawEngineD3D11.cpp +++ b/GPU/D3D11/DrawEngineD3D11.cpp @@ -100,16 +100,36 @@ DrawEngineD3D11::DrawEngineD3D11(Draw::DrawContext *draw, ID3D11Device *device, InitDeviceObjects(); - tessDataTransfer = new TessellationDataTransferD3D11(); - // Vertex pushing buffers. For uniforms we use short DISCARD buffers, but we could use // this kind of buffer there as well with D3D11.1. We might be able to use the same buffer // for both vertices and indices, and possibly all three data types. - pushVerts_ = new PushBufferD3D11(device, VERTEX_PUSH_SIZE, D3D11_BIND_VERTEX_BUFFER); - pushInds_ = new PushBufferD3D11(device, INDEX_PUSH_SIZE, D3D11_BIND_INDEX_BUFFER); } DrawEngineD3D11::~DrawEngineD3D11() { + DestroyDeviceObjects(); + FreeMemoryPages(decoded, DECODED_VERTEX_BUFFER_SIZE); + FreeMemoryPages(decIndex, DECODED_INDEX_BUFFER_SIZE); + FreeMemoryPages(splineBuffer, SPLINE_BUFFER_SIZE); + FreeMemoryPages(transformed, TRANSFORMED_VERTEX_BUFFER_SIZE); + FreeMemoryPages(transformedExpanded, 3 * TRANSFORMED_VERTEX_BUFFER_SIZE); +} + +void DrawEngineD3D11::InitDeviceObjects() { + pushVerts_ = new PushBufferD3D11(device_, VERTEX_PUSH_SIZE, D3D11_BIND_VERTEX_BUFFER); + pushInds_ = new PushBufferD3D11(device_, INDEX_PUSH_SIZE, D3D11_BIND_INDEX_BUFFER); + + tessDataTransfer = new TessellationDataTransferD3D11(); +} + +void DrawEngineD3D11::ClearTrackedVertexArrays() { + for (auto &vai : vai_) { + delete vai.second; + } + vai_.clear(); +} + +void DrawEngineD3D11::DestroyDeviceObjects() { + delete tessDataTransfer; delete pushVerts_; delete pushInds_; @@ -122,23 +142,9 @@ DrawEngineD3D11::~DrawEngineD3D11() { for (auto &blend : blendCache_) { blend.second->Release(); } - - DestroyDeviceObjects(); - FreeMemoryPages(decoded, DECODED_VERTEX_BUFFER_SIZE); - FreeMemoryPages(decIndex, DECODED_INDEX_BUFFER_SIZE); - FreeMemoryPages(splineBuffer, SPLINE_BUFFER_SIZE); - FreeMemoryPages(transformed, TRANSFORMED_VERTEX_BUFFER_SIZE); - FreeMemoryPages(transformedExpanded, 3 * TRANSFORMED_VERTEX_BUFFER_SIZE); - - delete tessDataTransfer; -} - -void DrawEngineD3D11::InitDeviceObjects() { - -} - -void DrawEngineD3D11::DestroyDeviceObjects() { - ClearTrackedVertexArrays(); + for (auto &raster : rasterCache_) { + raster.second->Release(); + } } struct DeclTypeInfo { @@ -508,13 +514,6 @@ ReliableHashType DrawEngineD3D11::ComputeHash() { return fullhash; } -void DrawEngineD3D11::ClearTrackedVertexArrays() { - for (auto vai = vai_.begin(); vai != vai_.end(); vai++) { - delete vai->second; - } - vai_.clear(); -} - void DrawEngineD3D11::BeginFrame() { pushVerts_->Reset(); pushInds_->Reset(); diff --git a/GPU/D3D11/FramebufferManagerD3D11.cpp b/GPU/D3D11/FramebufferManagerD3D11.cpp index 8a3b11aecd..fba6d212b1 100644 --- a/GPU/D3D11/FramebufferManagerD3D11.cpp +++ b/GPU/D3D11/FramebufferManagerD3D11.cpp @@ -124,7 +124,7 @@ FramebufferManagerD3D11::~FramebufferManagerD3D11() { // FBO cleanup for (auto it = tempFBOs_.begin(), end = tempFBOs_.end(); it != end; ++it) { - delete it->second.fbo; + it->second.fbo->Release(); } delete[] convBuf; @@ -568,6 +568,8 @@ void FramebufferManagerD3D11::SimpleBlit( draw_->BindFramebufferAsTexture(src, 0, Draw::FB_COLOR_BIT, 0); draw_->BindFramebufferAsRenderTarget(dest); Bind2DShader(); + D3D11_VIEWPORT vp{ 0.0f, 0.0f, (float)destW, (float)destH, 0.0f, 1.0f }; + context_->RSSetViewports(1, &vp); context_->RSSetState(stockD3D11.rasterStateNoCull); context_->OMSetBlendState(stockD3D11.blendStateDisabledWithColorMask[0xF], nullptr, 0xFFFFFFFF); context_->OMSetDepthStencilState(stockD3D11.depthStencilDisabled, 0); diff --git a/GPU/D3D11/FramebufferManagerD3D11.h b/GPU/D3D11/FramebufferManagerD3D11.h index a77bf53efa..bb91fd4165 100644 --- a/GPU/D3D11/FramebufferManagerD3D11.h +++ b/GPU/D3D11/FramebufferManagerD3D11.h @@ -141,13 +141,6 @@ private: bool resized_; - struct TempFBO { - Draw::Framebuffer *fbo; - int last_frame_used; - }; - - std::map tempFBOs_; - #if 0 AsyncPBO *pixelBufObj_; //this isn't that large u8 currentPBO_; diff --git a/GPU/D3D11/ShaderManagerD3D11.cpp b/GPU/D3D11/ShaderManagerD3D11.cpp index 66067539aa..7d21a2d553 100644 --- a/GPU/D3D11/ShaderManagerD3D11.cpp +++ b/GPU/D3D11/ShaderManagerD3D11.cpp @@ -107,6 +107,9 @@ ShaderManagerD3D11::ShaderManagerD3D11(ID3D11Device *device, ID3D11DeviceContext } ShaderManagerD3D11::~ShaderManagerD3D11() { + push_base->Release(); + push_lights->Release(); + push_bones->Release(); ClearShaders(); delete[] codeBuffer_; } diff --git a/GPU/Software/SoftGpu.cpp b/GPU/Software/SoftGpu.cpp index 809d5cb6c6..e4dae6d0c6 100644 --- a/GPU/Software/SoftGpu.cpp +++ b/GPU/Software/SoftGpu.cpp @@ -97,6 +97,10 @@ SoftGPU::SoftGPU(GraphicsContext *gfxCtx, Draw::DrawContext *draw) inputLayout, depth, blendstateOff, rasterNoCull, &vsTexColBufDesc }; texColor = draw_->CreateGraphicsPipeline(pipelineDesc); + inputLayout->Release(); + depth->Release(); + blendstateOff->Release(); + rasterNoCull->Release(); fb.data = Memory::GetPointer(0x44000000); // TODO: correct default address? depthbuf.data = Memory::GetPointer(0x44000000); // TODO: correct default address? diff --git a/UI/NativeApp.cpp b/UI/NativeApp.cpp index 976a17a65b..0116a68585 100644 --- a/UI/NativeApp.cpp +++ b/UI/NativeApp.cpp @@ -607,6 +607,8 @@ void NativeInitGraphics(GraphicsContext *graphicsContext) { colorPipeline = g_draw->CreateGraphicsPipeline(colorDesc); texColorPipeline = g_draw->CreateGraphicsPipeline(texColorDesc); + // Release these now, reference counting should ensure that they get completely released + // once we delete both pipelines. inputLayout->Release(); rasterNoCull->Release(); blendNormal->Release(); diff --git a/UI/TextureUtil.h b/UI/TextureUtil.h index 5d5d1d2d0f..b93cc82fde 100644 --- a/UI/TextureUtil.h +++ b/UI/TextureUtil.h @@ -17,10 +17,12 @@ public: register_gl_resource_holder(this); } ~ManagedTexture() { + if (texture_) + texture_->Release(); unregister_gl_resource_holder(this); } void GLLost() override { - delete texture_; + texture_->Release(); texture_ = nullptr; } void GLRestore() override { diff --git a/ext/native/ui/ui_context.cpp b/ext/native/ui/ui_context.cpp index bf7ba9f194..34601665dc 100644 --- a/ext/native/ui/ui_context.cpp +++ b/ext/native/ui/ui_context.cpp @@ -14,6 +14,7 @@ UIContext::UIContext() } UIContext::~UIContext() { + sampler_->Release(); delete fontStyle_; delete textDrawer_; }