From 9f8cd68211744698e5468ea8d4865ffdb11b0fcc Mon Sep 17 00:00:00 2001 From: Magnus Norddahl Date: Mon, 27 May 2019 20:03:11 +0200 Subject: [PATCH] - fix use after delete --- .../polyrenderer/backend/poly_buffers.cpp | 10 ++-- .../polyrenderer/backend/poly_buffers.h | 22 ++++++--- .../polyrenderer/backend/poly_framebuffer.cpp | 4 +- .../polyrenderer/backend/poly_renderstate.cpp | 47 ++++++++++++++++++- .../polyrenderer/backend/poly_renderstate.h | 3 ++ 5 files changed, 71 insertions(+), 15 deletions(-) diff --git a/src/rendering/polyrenderer/backend/poly_buffers.cpp b/src/rendering/polyrenderer/backend/poly_buffers.cpp index e186b7a71..ad6a36857 100644 --- a/src/rendering/polyrenderer/backend/poly_buffers.cpp +++ b/src/rendering/polyrenderer/backend/poly_buffers.cpp @@ -76,14 +76,12 @@ void PolyBuffer::Unlock() void PolyVertexBuffer::SetFormat(int numBindingPoints, int numAttributes, size_t stride, const FVertexBufferAttribute *attrs) { - for (int j = 0; j < numAttributes; j++) - { - mOffsets[attrs[j].location] = attrs[j].offset; - } - mStride = stride; + VertexFormat = GetPolyFrameBuffer()->GetRenderState()->GetVertexFormat(numBindingPoints, numAttributes, stride, attrs); } -void PolyVertexBuffer::Load(PolyTriangleThreadData *thread, const void *vertices, int index) +///////////////////////////////////////////////////////////////////////////// + +void PolyVertexInputAssembly::Load(PolyTriangleThreadData *thread, const void *vertices, int index) { const uint8_t *vertex = static_cast(vertices) + mStride * index; const float *attrVertex = reinterpret_cast(vertex + mOffsets[VATTR_VERTEX]); diff --git a/src/rendering/polyrenderer/backend/poly_buffers.h b/src/rendering/polyrenderer/backend/poly_buffers.h index b4e183194..1d8c38e82 100644 --- a/src/rendering/polyrenderer/backend/poly_buffers.h +++ b/src/rendering/polyrenderer/backend/poly_buffers.h @@ -39,17 +39,27 @@ private: std::vector mData; }; -class PolyVertexBuffer : public IVertexBuffer, public PolyBuffer, public PolyInputAssembly +class PolyVertexInputAssembly : public PolyInputAssembly +{ +public: + size_t mOffsets[VATTR_MAX] = {}; + size_t mStride = 0; + + int NumBindingPoints; + size_t Stride; + std::vector Attrs; + int UseVertexData; + + void Load(PolyTriangleThreadData *thread, const void *vertices, int index) override; +}; + +class PolyVertexBuffer : public IVertexBuffer, public PolyBuffer { public: PolyVertexBuffer() { } void SetFormat(int numBindingPoints, int numAttributes, size_t stride, const FVertexBufferAttribute *attrs) override; - void Load(PolyTriangleThreadData *thread, const void *vertices, int index) override; - -private: - size_t mOffsets[VATTR_MAX] = {}; - size_t mStride = 0; + PolyVertexInputAssembly *VertexFormat = nullptr; }; class PolyIndexBuffer : public IIndexBuffer, public PolyBuffer diff --git a/src/rendering/polyrenderer/backend/poly_framebuffer.cpp b/src/rendering/polyrenderer/backend/poly_framebuffer.cpp index dce7b8499..d457aea49 100644 --- a/src/rendering/polyrenderer/backend/poly_framebuffer.cpp +++ b/src/rendering/polyrenderer/backend/poly_framebuffer.cpp @@ -84,13 +84,13 @@ void PolyFrameBuffer::InitializeState() uniformblockalignment = 1; maxuniformblock = 0x7fffffff; + mRenderState.reset(new PolyRenderState()); + mVertexData = new FFlatVertexBuffer(GetWidth(), GetHeight()); mSkyData = new FSkyVertexBuffer; mViewpoints = new GLViewpointBuffer; mLights = new FLightBuffer(); - mRenderState.reset(new PolyRenderState()); - CheckCanvas(); } diff --git a/src/rendering/polyrenderer/backend/poly_renderstate.cpp b/src/rendering/polyrenderer/backend/poly_renderstate.cpp index a21ebf108..cece607d1 100644 --- a/src/rendering/polyrenderer/backend/poly_renderstate.cpp +++ b/src/rendering/polyrenderer/backend/poly_renderstate.cpp @@ -201,7 +201,7 @@ void PolyRenderState::Apply() if (mVertexBuffer) PolyTriangleDrawer::SetVertexBuffer(fb->GetDrawCommands(), mVertexBuffer->Memory()); if (mIndexBuffer) PolyTriangleDrawer::SetIndexBuffer(fb->GetDrawCommands(), mIndexBuffer->Memory()); - PolyTriangleDrawer::SetInputAssembly(fb->GetDrawCommands(), static_cast(mVertexBuffer)); + PolyTriangleDrawer::SetInputAssembly(fb->GetDrawCommands(), static_cast(mVertexBuffer)->VertexFormat); PolyTriangleDrawer::SetRenderStyle(fb->GetDrawCommands(), mRenderStyle); PolyTriangleDrawer::PushStreamData(fb->GetDrawCommands(), mStreamData, constants); ApplyMatrices(); @@ -287,3 +287,48 @@ void PolyRenderState::Bind(PolyDataBuffer *buffer, uint32_t offset, uint32_t len PolyTriangleDrawer::SetViewpointUniforms(GetPolyFrameBuffer()->GetDrawCommands(), mViewpointUniforms); } } + +PolyVertexInputAssembly *PolyRenderState::GetVertexFormat(int numBindingPoints, int numAttributes, size_t stride, const FVertexBufferAttribute *attrs) +{ + for (size_t i = 0; i < mVertexFormats.size(); i++) + { + auto f = mVertexFormats[i].get(); + if (f->Attrs.size() == (size_t)numAttributes && f->NumBindingPoints == numBindingPoints && f->Stride == stride) + { + bool matches = true; + for (int j = 0; j < numAttributes; j++) + { + if (memcmp(&f->Attrs[j], &attrs[j], sizeof(FVertexBufferAttribute)) != 0) + { + matches = false; + break; + } + } + + if (matches) + return f; + } + } + + auto fmt = std::make_unique(); + fmt->NumBindingPoints = numBindingPoints; + fmt->Stride = stride; + fmt->UseVertexData = 0; + for (int j = 0; j < numAttributes; j++) + { + if (attrs[j].location == VATTR_COLOR) + fmt->UseVertexData |= 1; + else if (attrs[j].location == VATTR_NORMAL) + fmt->UseVertexData |= 2; + fmt->Attrs.push_back(attrs[j]); + } + + for (int j = 0; j < numAttributes; j++) + { + fmt->mOffsets[attrs[j].location] = attrs[j].offset; + } + fmt->mStride = stride; + + mVertexFormats.push_back(std::move(fmt)); + return mVertexFormats.back().get(); +} diff --git a/src/rendering/polyrenderer/backend/poly_renderstate.h b/src/rendering/polyrenderer/backend/poly_renderstate.h index 6052e910e..b0fee75ce 100644 --- a/src/rendering/polyrenderer/backend/poly_renderstate.h +++ b/src/rendering/polyrenderer/backend/poly_renderstate.h @@ -42,6 +42,8 @@ public: void Bind(PolyDataBuffer *buffer, uint32_t offset, uint32_t length); + PolyVertexInputAssembly *GetVertexFormat(int numBindingPoints, int numAttributes, size_t stride, const FVertexBufferAttribute *attrs); + private: void Apply(); void ApplyMaterial(); @@ -57,6 +59,7 @@ private: bool mFirstMatrixApply = true; HWViewpointUniforms *mViewpointUniforms = nullptr; + std::vector> mVertexFormats; bool mDepthClamp = true; };