Skip to content

Commit

Permalink
#6185: Verify uploaded geometry after syncing the buffer objects
Browse files Browse the repository at this point in the history
  • Loading branch information
codereader committed Jan 1, 2023
1 parent 916df97 commit 262b5c0
Show file tree
Hide file tree
Showing 5 changed files with 60 additions and 0 deletions.
3 changes: 3 additions & 0 deletions include/igeometrystore.h
Expand Up @@ -55,6 +55,9 @@ class IBufferObject
// Uploads the given data to the buffer, starting at the given offset
virtual void setData(std::size_t offset, const unsigned char* firstElement, std::size_t numBytes) = 0;

// Downloads the specified data chunk
virtual std::vector<unsigned char> getData(std::size_t offset, std::size_t numBytes) = 0;

// Re-allocates the memory of this buffer, does not transfer the data
// from the old internal buffer to the new one.
virtual void resize(std::size_t newSize) = 0;
Expand Down
27 changes: 27 additions & 0 deletions libs/render/ContinuousBuffer.h
Expand Up @@ -5,6 +5,7 @@
#include <limits>
#include <vector>
#include "igeometrystore.h"
#include "itextstream.h"

namespace render
{
Expand Down Expand Up @@ -316,6 +317,7 @@ class ContinuousBuffer
buffer->bind();
buffer->setData(0, reinterpret_cast<unsigned char*>(_buffer.data()),
_buffer.size() * sizeof(ElementType));
verifyBufferData(buffer, "After copying everything");
buffer->unbind();
}
else
Expand Down Expand Up @@ -360,6 +362,8 @@ class ContinuousBuffer
(maximumOffset - minimumOffset) * sizeof(ElementType));
}

verifyBufferData(buffer, "After copying elements");

buffer->unbind();
}
}
Expand All @@ -368,6 +372,29 @@ class ContinuousBuffer
}

private:
void verifyBufferData(const IBufferObject::Ptr& buffer, const char* eventString)
{
if (!std::is_same_v<ElementType, unsigned int>) return;

for (const auto& slot : _slots)
{
if (!slot.Occupied) continue;

auto bufferData = buffer->getData(slot.Offset * sizeof(ElementType), slot.Used * sizeof(ElementType));

auto bufferElements = reinterpret_cast<const ElementType*>(bufferData.data());

for (auto i = 0; i < slot.Used; ++i)
{
if (_buffer.at(slot.Offset + i) != bufferElements[i])
{
rMessage() << "Buffer data corruption (" << eventString << ")" << std::endl;
return;
}
}
}
}

bool findLeftFreeSlot(const SlotInfo& slotToTouch, Handle& found)
{
auto numSlots = _slots.size();
Expand Down
15 changes: 15 additions & 0 deletions libs/render/RenderVertex.h
Expand Up @@ -42,6 +42,21 @@ class RenderVertex
bitangent(bitangent_),
colour(colour_)
{}

bool operator==(const RenderVertex& other) const
{
return texcoord == other.texcoord &&
normal == other.normal &&
vertex == other.vertex &&
tangent == other.tangent &&
bitangent == other.bitangent &&
colour == other.colour;
}

bool operator!=(const RenderVertex& other) const
{
return !operator==(other);
}
};

}
10 changes: 10 additions & 0 deletions radiantcore/rendersystem/backend/BufferObjectProvider.h
Expand Up @@ -51,6 +51,16 @@ class BufferObjectProvider final :
glBufferSubData(_target, static_cast<GLintptr>(offset), static_cast<GLsizeiptr>(numBytes), firstElement);
}

std::vector<unsigned char> getData(std::size_t offset, std::size_t numBytes) override
{
std::vector<unsigned char> data;
data.resize(numBytes);

glGetBufferSubData(_target, static_cast<GLintptr>(offset), static_cast<GLsizeiptr>(numBytes), data.data());

return data;
}

// Re-allocates the memory of this buffer, does not transfer the data
// from the old internal buffer to the new one.
void resize(std::size_t newSize) override
Expand Down
5 changes: 5 additions & 0 deletions test/testutil/TestBufferObjectProvider.h
Expand Up @@ -29,6 +29,11 @@ class TestBufferObject final :
}
}

std::vector<unsigned char> getData(std::size_t offset, std::size_t numBytes) override
{
return {};
}

void resize(std::size_t newSize) override
{
buffer = std::vector<unsigned char>(newSize, '\0');
Expand Down

0 comments on commit 262b5c0

Please sign in to comment.