Skip to content

Commit

Permalink
Revert "#6185: Add my personal debugging code only to have it stored …
Browse files Browse the repository at this point in the history
…in git history"

This reverts commit eac000e.
  • Loading branch information
codereader committed Jan 1, 2023
1 parent eac000e commit 9722a07
Showing 1 changed file with 5 additions and 133 deletions.
138 changes: 5 additions & 133 deletions libs/render/ContinuousBuffer.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
#include <stack>
#include <limits>
#include <vector>
#include <fstream>
#include "igeometrystore.h"
#include "itextstream.h"

Expand All @@ -21,18 +20,6 @@ struct BufferTransaction
std::size_t numChangedElements;
};

template<typename Element>
inline std::string formatElement(const Element& element)
{
return fmt::format("{0:0000} ", element);
}

template<>
inline std::string formatElement(const RenderVertex& vertex)
{
return "vertex";
}

}

/**
Expand Down Expand Up @@ -193,11 +180,6 @@ class ContinuousBuffer
std::copy(elements.begin(), elements.end(), _buffer.begin() + slot.Offset);
slot.Used = numElements;

if (slot.Offset + 0 + numElements > _buffer.size())
{
throw std::logic_error("Modified chunk would extend beyond buffer limits");
}

_unsyncedModifications.emplace_back(ModifiedMemoryChunk{ handle, 0, numElements });
}

Expand All @@ -214,33 +196,22 @@ class ContinuousBuffer
std::copy(elements.begin(), elements.end(), _buffer.begin() + slot.Offset + elementOffset);
slot.Used = std::max(slot.Used, elementOffset + numElements);

if (slot.Offset + elementOffset + numElements > _buffer.size())
{
throw std::logic_error("Modified chunk would extend beyond buffer limits");
}

_unsyncedModifications.emplace_back(ModifiedMemoryChunk{ handle, elementOffset, numElements });
}

// Returns true if the size of this slot actually changed
// Returns true if the size of this size actually changed
bool resizeData(Handle handle, std::size_t elementCount)
{
auto& slot = _slots[handle];

if (elementCount > slot.Size)
{
throw std::logic_error("Cannot resize to a larger amount than allocated in GeometryStore::Buffer::resizeData");
throw std::logic_error("Cannot resize to a large amount than allocated in GeometryStore::Buffer::resizeData");
}

if (slot.Used == elementCount) return false; // no size change

slot.Used = elementCount;

if (slot.Offset + 0 + elementCount > _buffer.size())
{
throw std::logic_error("Modified chunk would extend beyond buffer limits");
}

_unsyncedModifications.emplace_back(ModifiedMemoryChunk{ handle, 0, elementCount });
return true;
}
Expand All @@ -253,14 +224,6 @@ class ContinuousBuffer

_allocatedElements -= releasedSlot.Size;

for (const auto& modification : _unsyncedModifications)
{
if (modification.handle == handle)
{
int i = 6;
}
}

// Check if the slot can merge with an adjacent one
Handle slotIndexToMerge = std::numeric_limits<Handle>::max();
if (findLeftFreeSlot(releasedSlot, slotIndexToMerge))
Expand Down Expand Up @@ -301,14 +264,6 @@ class ContinuousBuffer
{
for (const auto& transaction : transactions)
{
auto slotIndex = getHandle(transaction.slot);
const auto& slot = _slots.at(slotIndex);

if (slot.Offset + transaction.offset + transaction.numChangedElements > _buffer.size())
{
throw std::logic_error("Modified chunk would extend beyond buffer limits");
}

_unsyncedModifications.emplace_back(ModifiedMemoryChunk{
getHandle(transaction.slot), transaction.offset, transaction.numChangedElements });
}
Expand Down Expand Up @@ -358,15 +313,11 @@ class ContinuousBuffer
buffer->resize(currentBufferSize);
_lastSyncedBufferSize = currentBufferSize;

rMessage() << getElementName() << "Syncing entire buffer" << std::endl;

// Re-upload everything
buffer->bind();
dumpSlots(buffer, "_before");
buffer->setData(0, reinterpret_cast<unsigned char*>(_buffer.data()),
_buffer.size() * sizeof(ElementType));
verifyBufferData(buffer, "After copying everything");
dumpSlots(buffer, "_after");
buffer->unbind();
}
else
Expand All @@ -384,30 +335,17 @@ class ContinuousBuffer

minimumOffset = std::min(slot.Offset + modifiedChunk.offset, minimumOffset);
maximumOffset = std::max(slot.Offset + modifiedChunk.offset + modifiedChunk.numElements, maximumOffset);

if (slot.Offset + modifiedChunk.offset + modifiedChunk.numElements > _buffer.size())
{
//throw std::logic_error("Modified chunk cannot extend beyond buffer limits");
}

elementsToCopy += modifiedChunk.numElements;
}

// FIX: The maximum offset must not exceed the buffer limits
//maximumOffset = std::min(_buffer.size(), maximumOffset);

// Copy the data in one single operation or in multiple, depending on the effort
if (elementsToCopy > 0)
{
buffer->bind();

dumpSlots(buffer, "_before");

// Less than a couple of operations will be copied piece by piece
if (_unsyncedModifications.size() < 100)
{
rMessage() << getElementName() << "Syncing < 100 modifications (" << elementsToCopy << " elements)" << std::endl;

for (auto modifiedChunk : _unsyncedModifications)
{
auto& slot = _slots[modifiedChunk.handle];
Expand All @@ -419,15 +357,12 @@ class ContinuousBuffer
}
else // copy everything in between minimum and maximum in one operation
{
rMessage() << getElementName() << "Syncing range modification (" << elementsToCopy << " elements, range = [" << minimumOffset << "," << maximumOffset << "])" << std::endl;

buffer->setData(minimumOffset * sizeof(ElementType),
reinterpret_cast<unsigned char*>(_buffer.data() + minimumOffset),
(maximumOffset - minimumOffset) * sizeof(ElementType));
}

verifyBufferData(buffer, "After copying elements");
dumpSlots(buffer, "_after");

buffer->unbind();
}
Expand All @@ -437,19 +372,12 @@ class ContinuousBuffer
}

private:
const char* getElementName()
{
return std::is_same_v<ElementType, unsigned int> ? "[Indices]: " : "[Vertices]: ";
}

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

for (auto slotIndex = 0; slotIndex < _slots.size(); ++slotIndex)
for (const auto& slot : _slots)
{
const auto& slot = _slots.at(slotIndex);

if (!slot.Occupied) continue;

auto bufferData = buffer->getData(slot.Offset * sizeof(ElementType), slot.Used * sizeof(ElementType));
Expand All @@ -460,69 +388,13 @@ class ContinuousBuffer
{
if (_buffer.at(slot.Offset + i) != bufferElements[i])
{
rMessage() << getElementName() << "Buffer data corruption in slot index " << slotIndex << " (" << eventString << ")" << std::endl;
dumpSlots(buffer, "_corruption");
rMessage() << "Buffer data corruption (" << eventString << ")" << std::endl;
return;
}
}
}
}

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

std::ofstream local(std::string("c:/temp/local_buffer") + suffix + ".txt");
std::ofstream fbo(std::string("c:/temp/frame_buffer") + suffix + ".txt");

std::map<std::size_t, const SlotInfo*> _sortedSlots;

for (auto slotIndex = 0; slotIndex < _slots.size(); ++slotIndex)
{
const auto& slot = _slots.at(slotIndex);
_sortedSlots[slot.Offset] = &slot;
}

for (const auto& [slotIndex, slotPtr] : _sortedSlots)
{
const auto& slot = *slotPtr;

auto slotInfo = fmt::format("Slot {0:0000} (OFS: {1:00000} S: {2:00000} U: {3:00000})", slotIndex, slot.Offset, slot.Size, slot.Used);

local << slotInfo;
fbo << slotInfo;

if (!slot.Occupied)
{
local << "-\n";
fbo << "-\n";
continue;
}

// Dump data
auto bufferData = buffer->getData(slot.Offset * sizeof(ElementType), slot.Used * sizeof(ElementType));
auto bufferElements = reinterpret_cast<const ElementType*>(bufferData.data());

local << "[";
fbo << "[";

for (auto i = 0; i < slot.Used; ++i)
{
local << detail::formatElement(_buffer.at(slot.Offset + i));
fbo << detail::formatElement(bufferElements[i]);
}

local << "]";
fbo << "]";

local << "\n";
fbo << "\n";
}

local.close();
fbo.close();
}

bool findLeftFreeSlot(const SlotInfo& slotToTouch, Handle& found)
{
auto numSlots = _slots.size();
Expand Down

0 comments on commit 9722a07

Please sign in to comment.