From 4cb69760297dc7c915e8383892c0984f27510d14 Mon Sep 17 00:00:00 2001 From: "Unknown W. Brackets" Date: Sat, 7 Aug 2021 22:22:11 -0700 Subject: [PATCH] GPU: Use an empty vertex buf for reinterpret. See #14552. --- Common/GPU/D3D11/thin3d_d3d11.cpp | 4 ++-- Common/GPU/D3D9/thin3d_d3d9.cpp | 2 +- Common/GPU/OpenGL/thin3d_gl.cpp | 2 +- Common/GPU/Vulkan/thin3d_vulkan.cpp | 2 +- Common/GPU/thin3d.h | 2 +- GPU/Common/FramebufferManagerCommon.cpp | 31 ++++++++++++++----------- GPU/Common/FramebufferManagerCommon.h | 1 + 7 files changed, 25 insertions(+), 19 deletions(-) diff --git a/Common/GPU/D3D11/thin3d_d3d11.cpp b/Common/GPU/D3D11/thin3d_d3d11.cpp index b9f829e6c7..114692228c 100644 --- a/Common/GPU/D3D11/thin3d_d3d11.cpp +++ b/Common/GPU/D3D11/thin3d_d3d11.cpp @@ -87,7 +87,7 @@ public: void BindTextures(int start, int count, Texture **textures) override; void BindSamplerStates(int start, int count, SamplerState **states) override; - void BindVertexBuffers(int start, int count, Buffer **buffers, int *offsets) override; + void BindVertexBuffers(int start, int count, Buffer **buffers, const int *offsets) override; void BindIndexBuffer(Buffer *indexBuffer, int offset) override; void BindPipeline(Pipeline *pipeline) override; @@ -1163,7 +1163,7 @@ void D3D11DrawContext::UpdateBuffer(Buffer *buffer, const uint8_t *data, size_t context_->UpdateSubresource(buf->buf, 0, &box, data, 0, 0); } -void D3D11DrawContext::BindVertexBuffers(int start, int count, Buffer **buffers, int *offsets) { +void D3D11DrawContext::BindVertexBuffers(int start, int count, Buffer **buffers, const int *offsets) { _assert_(start + count <= ARRAY_SIZE(nextVertexBuffers_)); // Lazy application for (int i = 0; i < count; i++) { diff --git a/Common/GPU/D3D9/thin3d_d3d9.cpp b/Common/GPU/D3D9/thin3d_d3d9.cpp index 39a8b9d5b7..449b74b05c 100644 --- a/Common/GPU/D3D9/thin3d_d3d9.cpp +++ b/Common/GPU/D3D9/thin3d_d3d9.cpp @@ -533,7 +533,7 @@ public: s->Apply(device_, start + i); } } - void BindVertexBuffers(int start, int count, Buffer **buffers, int *offsets) override { + void BindVertexBuffers(int start, int count, Buffer **buffers, const int *offsets) override { _assert_(start + count <= ARRAY_SIZE(curVBuffers_)); for (int i = 0; i < count; i++) { curVBuffers_[i + start] = (D3D9Buffer *)buffers[i]; diff --git a/Common/GPU/OpenGL/thin3d_gl.cpp b/Common/GPU/OpenGL/thin3d_gl.cpp index d26f1ab8af..1c7a19792d 100644 --- a/Common/GPU/OpenGL/thin3d_gl.cpp +++ b/Common/GPU/OpenGL/thin3d_gl.cpp @@ -399,7 +399,7 @@ public: void BindTextures(int start, int count, Texture **textures) override; void BindPipeline(Pipeline *pipeline) override; - void BindVertexBuffers(int start, int count, Buffer **buffers, int *offsets) override { + void BindVertexBuffers(int start, int count, Buffer **buffers, const int *offsets) override { _assert_(start + count <= ARRAY_SIZE(curVBuffers_)); for (int i = 0; i < count; i++) { curVBuffers_[i + start] = (OpenGLBuffer *)buffers[i]; diff --git a/Common/GPU/Vulkan/thin3d_vulkan.cpp b/Common/GPU/Vulkan/thin3d_vulkan.cpp index f68132f5e4..5946ef0e14 100644 --- a/Common/GPU/Vulkan/thin3d_vulkan.cpp +++ b/Common/GPU/Vulkan/thin3d_vulkan.cpp @@ -415,7 +415,7 @@ public: } // TODO: Make VKBuffers proper buffers, and do a proper binding model. This is just silly. - void BindVertexBuffers(int start, int count, Buffer **buffers, int *offsets) override { + void BindVertexBuffers(int start, int count, Buffer **buffers, const int *offsets) override { _assert_(start + count <= ARRAY_SIZE(curVBuffers_)); for (int i = 0; i < count; i++) { curVBuffers_[i + start] = (VKBuffer *)buffers[i]; diff --git a/Common/GPU/thin3d.h b/Common/GPU/thin3d.h index 853ed27542..f3454df2e7 100644 --- a/Common/GPU/thin3d.h +++ b/Common/GPU/thin3d.h @@ -643,7 +643,7 @@ public: virtual void BindSamplerStates(int start, int count, SamplerState **state) = 0; virtual void BindTextures(int start, int count, Texture **textures) = 0; - virtual void BindVertexBuffers(int start, int count, Buffer **buffers, int *offsets) = 0; + virtual void BindVertexBuffers(int start, int count, Buffer **buffers, const int *offsets) = 0; virtual void BindIndexBuffer(Buffer *indexBuffer, int offset) = 0; // Only supports a single dynamic uniform buffer, for maximum compatibility with the old APIs and ease of emulation. diff --git a/GPU/Common/FramebufferManagerCommon.cpp b/GPU/Common/FramebufferManagerCommon.cpp index 2690d94274..91b712086b 100644 --- a/GPU/Common/FramebufferManagerCommon.cpp +++ b/GPU/Common/FramebufferManagerCommon.cpp @@ -603,6 +603,10 @@ void FramebufferManagerCommon::ReinterpretFramebuffer(VirtualFramebuffer *vfb, G reinterpretSampler_ = draw_->CreateSamplerState(samplerDesc); } + if (!reinterpretVBuf_) { + reinterpretVBuf_ = draw_->CreateBuffer(12 * 3, Draw::BufferUsageFlag::DYNAMIC | Draw::BufferUsageFlag::VERTEXDATA); + } + // See if we need to create a new pipeline. Draw::Pipeline *pipeline = reinterpretFromTo_[(int)oldFormat][(int)newFormat]; @@ -650,7 +654,9 @@ void FramebufferManagerCommon::ReinterpretFramebuffer(VirtualFramebuffer *vfb, G draw_->SetScissorRect(0, 0, vfb->renderWidth, vfb->renderHeight); Draw::Viewport vp = Draw::Viewport{ 0.0f, 0.0f, (float)vfb->renderWidth, (float)vfb->renderHeight, 0.0f, 1.0f }; draw_->SetViewports(1, &vp); - // No vertex buffer - generate vertices in shader. TODO: Switch to a vertex buffer for GLES2/D3D9 compat. + // Vertex buffer not used - vertices generated in shader. + // TODO: Switch to a vertex buffer for GLES2/D3D9 compat. + draw_->BindVertexBuffers(0, 1, &reinterpretVBuf_, nullptr); draw_->Draw(3, 0); draw_->InvalidateCachedState(); @@ -2318,24 +2324,23 @@ std::vector FramebufferManagerCommon::GetFramebufferList() { return list; } +template +static void DoRelease(T *&obj) { + if (obj) + obj->Release(); + obj = nullptr; +} + void FramebufferManagerCommon::DeviceLost() { DestroyAllFBOs(); for (int i = 0; i < 3; i++) { for (int j = 0; j < 3; j++) { - if (reinterpretFromTo_[i][j]) { - reinterpretFromTo_[i][j]->Release(); - reinterpretFromTo_[i][j] = nullptr; - } + DoRelease(reinterpretFromTo_[i][j]); } } - if (reinterpretSampler_) { - reinterpretSampler_->Release(); - reinterpretSampler_ = nullptr; - } - if (reinterpretVS_) { - reinterpretVS_->Release(); - reinterpretVS_ = nullptr; - } + DoRelease(reinterpretVBuf_); + DoRelease(reinterpretSampler_); + DoRelease(reinterpretVS_); presentation_->DeviceLost(); draw_ = nullptr; } diff --git a/GPU/Common/FramebufferManagerCommon.h b/GPU/Common/FramebufferManagerCommon.h index 0b843b5bc4..1182c73e32 100644 --- a/GPU/Common/FramebufferManagerCommon.h +++ b/GPU/Common/FramebufferManagerCommon.h @@ -445,4 +445,5 @@ protected: Draw::Pipeline *reinterpretFromTo_[3][3]{}; Draw::ShaderModule *reinterpretVS_ = nullptr; Draw::SamplerState *reinterpretSampler_ = nullptr; + Draw::Buffer *reinterpretVBuf_ = nullptr; };