Skip to content

Commit

Permalink
#5912: Extend IGeometryStore interface by resizeData() method to be a…
Browse files Browse the repository at this point in the history
…ble to trim the stored data.
  • Loading branch information
codereader committed Mar 4, 2022
1 parent d0dc716 commit ac5ba82
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 0 deletions.
5 changes: 5 additions & 0 deletions include/igeometrystore.h
Expand Up @@ -53,6 +53,11 @@ class IGeometryStore
virtual void updateSubData(Slot slot, std::size_t vertexOffset, const std::vector<ArbitraryMeshVertex>& vertices,
std::size_t indexOffset, const std::vector<unsigned int>& indices) = 0;

/**
* Called in case the stored data in the given slot should just be cut off at the end.
*/
virtual void resizeData(Slot slot, std::size_t vertexSize, std::size_t indexSize) = 0;

virtual void deallocateSlot(Slot slot) = 0;

// The render parameters suitable for rendering surfaces using gl(Multi)DrawElements
Expand Down
12 changes: 12 additions & 0 deletions radiantcore/rendersystem/backend/GeometryStore.h
Expand Up @@ -141,6 +141,18 @@ class GeometryStore :
});
}

void resizeData(Slot slot, std::size_t vertexSize, std::size_t indexSize) override
{
auto& current = getCurrentBuffer();

current.vertices.resizeData(GetVertexSlot(slot), vertexSize);
current.indices.resizeData(GetIndexSlot(slot), indexSize);

_transactionLog.emplace_back(detail::BufferTransaction{
slot, detail::BufferTransaction::Type::Update
});
}

void deallocateSlot(Slot slot) override
{
auto& current = getCurrentBuffer();
Expand Down
30 changes: 30 additions & 0 deletions test/ContinuousBuffer.cpp
Expand Up @@ -208,6 +208,36 @@ TEST(ContinuousBufferTest, ReplaceSubDataOverflow)
EXPECT_THROW(buffer.setSubData(handle, 4, five), std::logic_error);
}

TEST(ContinuousBufferTest, ResizeData)
{
auto eight = std::vector<int>({ 0,1,2,3,4,5,6,7 });

render::ContinuousBuffer<int> buffer(24);

auto handle = buffer.allocate(eight.size());
buffer.setData(handle, eight);

EXPECT_EQ(buffer.getNumUsedElements(handle), eight.size());

buffer.resizeData(handle, 5);
EXPECT_TRUE(checkData(buffer, handle, eight)) << "Data should not have changed";
EXPECT_EQ(buffer.getNumUsedElements(handle), 5) << "Used Element Count should be 5 now";
}

TEST(ContinuousBufferTest, ResizeDataOverflow)
{
auto eight = std::vector<int>({ 0,1,2,3,4,5,6,7 });

render::ContinuousBuffer<int> buffer(24);

auto handle = buffer.allocate(eight.size());
buffer.setData(handle, eight);

EXPECT_EQ(buffer.getNumUsedElements(handle), eight.size());

EXPECT_THROW(buffer.resizeData(handle, eight.size() + 1), std::logic_error);
}

TEST(ContinuousBufferTest, BufferGrowth)
{
auto sixteen = std::vector<int>({ 0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15 });
Expand Down

0 comments on commit ac5ba82

Please sign in to comment.