Skip to content

Commit

Permalink
Pursche/gpu vector improvements (#42)
Browse files Browse the repository at this point in the history
* Start refactoring GPUVector using an Engine test

* Add support for uploading GPUVector to GPU

Improve automated tests for GPUVector
  • Loading branch information
Pursche authored Dec 19, 2024
1 parent 58eec2c commit c67a364
Show file tree
Hide file tree
Showing 12 changed files with 981 additions and 250 deletions.
1 change: 1 addition & 0 deletions Premake/ProjectUtil.lua
Original file line number Diff line number Diff line change
Expand Up @@ -397,6 +397,7 @@ Solution.Util.GetFilesForCpp = function(basePath)
(basePath .. "/**.hpp"),
(basePath .. "/**.c"),
(basePath .. "/**.cpp"),
(basePath .. "/**.natvis"),
}

return files
Expand Down
55 changes: 26 additions & 29 deletions Source/Base/Base/Memory/BufferRangeAllocator.cpp
Original file line number Diff line number Diff line change
@@ -1,18 +1,23 @@
#include "BufferRangeAllocator.h"
#include "Base/Util/DebugHandler.h"

void BufferRangeAllocator::Init(size_t bufferOffset, size_t bufferSize)
void BufferRangeAllocator::Init(size_t bufferOffset, size_t bufferSize, size_t freeSize)
{
_freeFrames.reserve(64);
_freeFrames.clear();

if (freeSize == std::numeric_limits<size_t>::max())
{
freeSize = bufferSize;
}

BufferRangeFrame& frame = _freeFrames.emplace_back();
frame.offset = bufferOffset;
frame.size = bufferSize;
frame.size = freeSize;

_currentOffset = bufferOffset;
_currentSize = bufferSize;
_allocatedBytes = 0;
_allocatedBytes = bufferOffset;
}

void BufferRangeAllocator::Reset()
Expand All @@ -32,7 +37,7 @@ bool BufferRangeAllocator::Allocate(size_t size, BufferRangeFrame& frame)
return false;

size_t index = std::numeric_limits<size_t>().max();
//size_t spaceLeft = std::numeric_limits<size_t>().max();
bool wasLastFrame = false;

for (size_t i = 0; i < _freeFrames.size(); i++)
{
Expand All @@ -41,15 +46,8 @@ bool BufferRangeAllocator::Allocate(size_t size, BufferRangeFrame& frame)
if (size <= freeFrame.size)
{
index = i;
wasLastFrame = i == _freeFrames.size() - 1;
break;

// Best Fit (Use later when we know implementation works
/*size_t space = freeFrame.size - size;
if (space < spaceLeft)
{
spaceLeft = space;
index = i;
}*/
}
}

Expand All @@ -59,14 +57,18 @@ bool BufferRangeAllocator::Allocate(size_t size, BufferRangeFrame& frame)

frame.offset = freeFrame.offset;
frame.size = size;
frame.wasHole = !wasLastFrame;

freeFrame.offset += size;
freeFrame.size -= size;

if (freeFrame.size == 0)
_freeFrames.erase(_freeFrames.begin() + index);

_allocatedBytes += size;
if (wasLastFrame)
{
_allocatedBytes += size;
}

return true;
}
Expand All @@ -80,7 +82,7 @@ bool BufferRangeAllocator::Allocate(size_t size, size_t alignment, BufferRangeFr
return false;

size_t index = std::numeric_limits<size_t>().max();
//size_t spaceLeft = std::numeric_limits<size_t>().max();
bool wasLastFrame = false;

for (size_t i = 0; i < _freeFrames.size(); i++)
{
Expand All @@ -92,15 +94,8 @@ bool BufferRangeAllocator::Allocate(size_t size, size_t alignment, BufferRangeFr
if (alignedSize <= freeFrame.size)
{
index = i;
wasLastFrame = i == _freeFrames.size() - 1;
break;

// Best Fit (Use later when we know implementation works
/*size_t space = freeFrame.size - size;
if (space < spaceLeft)
{
spaceLeft = space;
index = i;
}*/
}
}

Expand All @@ -125,7 +120,10 @@ bool BufferRangeAllocator::Allocate(size_t size, size_t alignment, BufferRangeFr
//NC_LOG_CRITICAL("Did not allocate aligned");
}

_allocatedBytes += size;
if (wasLastFrame)
{
_allocatedBytes += size;
}

return true;
}
Expand All @@ -138,13 +136,9 @@ bool BufferRangeAllocator::Free(const BufferRangeFrame& frame)
BufferRangeFrame& newFreeFrame = _freeFrames.emplace_back();
newFreeFrame = frame;

// Validate if 'frame' overlaps with any free frame

// Check if we can merge 'frame' with any free frame

// Reclaim alignment as well
TryMergeFrames();

_allocatedBytes -= frame.size;
//_allocatedBytes -= frame.size; // This looks very sus to me so I disabled it for now... If we're using this to for example loop over data it would be bad

return true;
}
Expand Down Expand Up @@ -183,4 +177,7 @@ void BufferRangeAllocator::TryMergeFrames()
_freeFrames.erase(_freeFrames.begin() + i + 1);
}
}

// Sort the free frames by offset
std::sort(_freeFrames.begin(), _freeFrames.end(), [](const BufferRangeFrame& a, const BufferRangeFrame& b) { return a.offset < b.offset; });
}
14 changes: 8 additions & 6 deletions Source/Base/Base/Memory/BufferRangeAllocator.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,27 +7,29 @@ struct BufferRangeFrame
{
size_t offset = 0;
size_t size = 0;
bool wasHole = false;
};

class BufferRangeAllocator
{
public:
BufferRangeAllocator() { }

void Init(size_t bufferOffset, size_t bufferSize);
void Init(size_t bufferOffset, size_t bufferSize, size_t freeSize = std::numeric_limits<size_t>::max());
void Reset();

bool Allocate(size_t size, BufferRangeFrame& frame);
bool Allocate(size_t size, size_t alignment, BufferRangeFrame& frame);
bool Free(const BufferRangeFrame& frame);
bool Grow(size_t newSize);

size_t Offset() { return _currentOffset; }
size_t Size() { return _currentSize; }
size_t AllocatedBytes() { return _allocatedBytes; }
private:
inline size_t Offset() const { return _currentOffset; }
inline size_t Size() const { return _currentSize; }
inline size_t AllocatedBytes() const { return _allocatedBytes; }

void TryMergeFrames();

std::vector<BufferRangeFrame>& GetFreeFrames() { return _freeFrames; }

private:
size_t _currentOffset = 0;
size_t _currentSize = 0;
Expand Down
2 changes: 1 addition & 1 deletion Source/Engine-Tests/Engine-Tests.lua
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
if BuildSettings:Get("Build UnitTests") == false then return end

local mod = Solution.Util.CreateModuleTable("Engine-Tests", { "base", "fileformat", "imgui", "catch2", "catch2-withmain" })
local mod = Solution.Util.CreateModuleTable("Engine-Tests", { "base", "fileformat", "imgui", "renderer", "catch2", "catch2-withmain" })

Solution.Util.CreateConsoleApp(mod.Name, Solution.Projects.Current.BinDir, mod.Dependencies, function()
local defines = { "_SILENCE_ALL_CXX17_DEPRECATION_WARNINGS", "_SILENCE_ALL_MS_EXT_DEPRECATION_WARNINGS" }
Expand Down
Loading

0 comments on commit c67a364

Please sign in to comment.