diff --git a/Source/Native/OpenGL/GLIndexBuffer.cpp b/Source/Native/OpenGL/GLIndexBuffer.cpp index 8adbcc51..0df2642b 100644 --- a/Source/Native/OpenGL/GLIndexBuffer.cpp +++ b/Source/Native/OpenGL/GLIndexBuffer.cpp @@ -25,11 +25,18 @@ GLIndexBuffer::~GLIndexBuffer() { - if (Device && mBuffer != 0) + Finalize(); +} + +void GLIndexBuffer::Finalize() +{ + if (Device) { Device->mIndexBuffers.erase(ItBuffer); Device = nullptr; - glDeleteBuffers(1, &mBuffer); + + if (mBuffer != 0) + glDeleteBuffers(1, &mBuffer); mBuffer = 0; } } diff --git a/Source/Native/OpenGL/GLIndexBuffer.h b/Source/Native/OpenGL/GLIndexBuffer.h index 64ea71c8..dce8825f 100644 --- a/Source/Native/OpenGL/GLIndexBuffer.h +++ b/Source/Native/OpenGL/GLIndexBuffer.h @@ -31,6 +31,8 @@ class GLIndexBuffer : public IndexBuffer public: ~GLIndexBuffer(); + void Finalize(); + GLuint GetBuffer(); GLRenderDevice* Device = nullptr; diff --git a/Source/Native/OpenGL/GLRenderDevice.cpp b/Source/Native/OpenGL/GLRenderDevice.cpp index 9c6d988a..55fd0653 100644 --- a/Source/Native/OpenGL/GLRenderDevice.cpp +++ b/Source/Native/OpenGL/GLRenderDevice.cpp @@ -87,7 +87,7 @@ GLRenderDevice::~GLRenderDevice() for (GLIndexBuffer* buffer : mIndexBuffers) mDeleteList.IndexBuffers.push_back(buffer); for (GLVertexBuffer* buffer : mSharedVertexBuffers[0]->VertexBuffers) mDeleteList.VertexBuffers.push_back(buffer); for (GLVertexBuffer* buffer : mSharedVertexBuffers[1]->VertexBuffers) mDeleteList.VertexBuffers.push_back(buffer); - ProcessDeleteList(); + ProcessDeleteList(true); glDeleteBuffers(1, &mStreamVertexBuffer); glDeleteVertexArrays(1, &mStreamVAO); @@ -965,13 +965,22 @@ void GLRenderDevice::DeleteObject(GLTexture* texture) delete texture; } -void GLRenderDevice::ProcessDeleteList() +void GLRenderDevice::ProcessDeleteList(bool finalize) { std::unique_lock lock(GLRenderDevice::GetMutex()); - for (auto buffer : mDeleteList.IndexBuffers) delete buffer; - for (auto buffer : mDeleteList.VertexBuffers) delete buffer; - for (auto texture : mDeleteList.Textures) delete texture; + if (!finalize) + { + for (auto buffer : mDeleteList.IndexBuffers) delete buffer; + for (auto buffer : mDeleteList.VertexBuffers) delete buffer; + for (auto texture : mDeleteList.Textures) delete texture; + } + else + { + for (auto buffer : mDeleteList.IndexBuffers) buffer->Finalize(); + for (auto buffer : mDeleteList.VertexBuffers) buffer->Finalize(); + for (auto texture : mDeleteList.Textures) texture->Finalize(); + } mDeleteList.IndexBuffers.clear(); mDeleteList.VertexBuffers.clear(); diff --git a/Source/Native/OpenGL/GLRenderDevice.h b/Source/Native/OpenGL/GLRenderDevice.h index 6d22a90b..d9ab4bd6 100644 --- a/Source/Native/OpenGL/GLRenderDevice.h +++ b/Source/Native/OpenGL/GLRenderDevice.h @@ -106,7 +106,7 @@ public: static void DeleteObject(GLIndexBuffer* buffer); static void DeleteObject(GLTexture* texture); - void ProcessDeleteList(); + void ProcessDeleteList(bool finalize = false); std::unique_ptr Context; diff --git a/Source/Native/OpenGL/GLTexture.cpp b/Source/Native/OpenGL/GLTexture.cpp index c3c65582..9ce12fb1 100644 --- a/Source/Native/OpenGL/GLTexture.cpp +++ b/Source/Native/OpenGL/GLTexture.cpp @@ -29,6 +29,11 @@ GLTexture::GLTexture() } GLTexture::~GLTexture() +{ + Finalize(); +} + +void GLTexture::Finalize() { if (Device) Invalidate(); diff --git a/Source/Native/OpenGL/GLTexture.h b/Source/Native/OpenGL/GLTexture.h index 6c47e3a5..b34c24ce 100644 --- a/Source/Native/OpenGL/GLTexture.h +++ b/Source/Native/OpenGL/GLTexture.h @@ -32,6 +32,8 @@ public: GLTexture(); ~GLTexture(); + void Finalize(); + void Set2DImage(int width, int height) override; void SetCubeImage(int size) override; diff --git a/Source/Native/OpenGL/GLVertexBuffer.cpp b/Source/Native/OpenGL/GLVertexBuffer.cpp index b59b9408..7b7511ed 100644 --- a/Source/Native/OpenGL/GLVertexBuffer.cpp +++ b/Source/Native/OpenGL/GLVertexBuffer.cpp @@ -72,6 +72,11 @@ void GLSharedVertexBuffer::SetupWorldVAO() ///////////////////////////////////////////////////////////////////////////// GLVertexBuffer::~GLVertexBuffer() +{ + Finalize(); +} + +void GLVertexBuffer::Finalize() { if (Device) { diff --git a/Source/Native/OpenGL/GLVertexBuffer.h b/Source/Native/OpenGL/GLVertexBuffer.h index ea16d6f4..ed50eb67 100644 --- a/Source/Native/OpenGL/GLVertexBuffer.h +++ b/Source/Native/OpenGL/GLVertexBuffer.h @@ -56,6 +56,8 @@ class GLVertexBuffer : public VertexBuffer public: ~GLVertexBuffer(); + void Finalize(); + VertexFormat Format = VertexFormat::Flat; GLRenderDevice* Device = nullptr;