Skip to content

Commit

Permalink
use buffer allocator for restrided vertex buffers
Browse files Browse the repository at this point in the history
  • Loading branch information
SamoZ256 committed Aug 13, 2024
1 parent 27925a4 commit bba2bbc
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 31 deletions.
29 changes: 7 additions & 22 deletions src/Cafe/HW/Latte/Renderer/Metal/MetalMemoryManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -70,17 +70,6 @@ MetalBufferAllocation MetalBufferAllocator::GetBufferAllocation(size_t size, siz

MetalVertexBufferCache::~MetalVertexBufferCache()
{
for (uint32 i = 0; i < LATTE_MAX_VERTEX_BUFFERS; i++)
{
auto vertexBufferRange = m_bufferRanges[i];
if (vertexBufferRange.offset != INVALID_OFFSET)
{
if (vertexBufferRange.restrideInfo->buffer)
{
vertexBufferRange.restrideInfo->buffer->release();
}
}
}
}

MetalRestridedBufferRange MetalVertexBufferCache::RestrideBufferIfNeeded(MTL::Buffer* bufferCache, uint32 bufferIndex, size_t stride)
Expand All @@ -94,17 +83,12 @@ MetalRestridedBufferRange MetalVertexBufferCache::RestrideBufferIfNeeded(MTL::Bu
return {bufferCache, vertexBufferRange.offset};
}

auto buffer = m_bufferAllocator->GetBuffer(restrideInfo.allocation.bufferIndex);
if (restrideInfo.memoryInvalidated || stride != restrideInfo.lastStride)
{
size_t newStride = Align(stride, 4);
size_t newSize = vertexBufferRange.size / stride * newStride;
if (!restrideInfo.buffer || newSize != restrideInfo.buffer->length())
{
if (restrideInfo.buffer)
restrideInfo.buffer->release();
// TODO: use one big buffer for all restrided buffers
restrideInfo.buffer = m_mtlr->GetDevice()->newBuffer(newSize, MTL::StorageModeShared);
}
restrideInfo.allocation = m_bufferAllocator->GetBufferAllocation(newSize, 4);

//uint8* oldPtr = (uint8*)bufferCache->contents() + vertexBufferRange.offset;
//uint8* newPtr = (uint8*)restrideInfo.buffer->contents();
Expand All @@ -120,8 +104,8 @@ MetalRestridedBufferRange MetalVertexBufferCache::RestrideBufferIfNeeded(MTL::Bu
auto renderCommandEncoder = static_cast<MTL::RenderCommandEncoder*>(m_mtlr->GetCommandEncoder());

renderCommandEncoder->setRenderPipelineState(m_restrideBufferPipeline->GetRenderPipelineState());
MTL::Buffer* buffers[] = {bufferCache, restrideInfo.buffer};
size_t offsets[] = {vertexBufferRange.offset, 0};
MTL::Buffer* buffers[] = {bufferCache, buffer};
size_t offsets[] = {vertexBufferRange.offset, restrideInfo.allocation.bufferOffset};
renderCommandEncoder->setVertexBuffers(buffers, offsets, NS::Range(0, 2));

struct
Expand All @@ -133,7 +117,8 @@ MetalRestridedBufferRange MetalVertexBufferCache::RestrideBufferIfNeeded(MTL::Bu

renderCommandEncoder->drawPrimitives(MTL::PrimitiveTypePoint, NS::UInteger(0), vertexBufferRange.size / stride);

MTL::Resource* barrierBuffers[] = {restrideInfo.buffer};
// TODO: restride in one call?
MTL::Resource* barrierBuffers[] = {buffer};
renderCommandEncoder->memoryBarrier(barrierBuffers, 1, MTL::RenderStageVertex, MTL::RenderStageVertex);
}
else
Expand All @@ -146,7 +131,7 @@ MetalRestridedBufferRange MetalVertexBufferCache::RestrideBufferIfNeeded(MTL::Bu
restrideInfo.lastStride = newStride;
}

return {restrideInfo.buffer, 0};
return {buffer, restrideInfo.allocation.bufferOffset};
}

void MetalVertexBufferCache::MemoryRangeChanged(size_t offset, size_t size)
Expand Down
18 changes: 10 additions & 8 deletions src/Cafe/HW/Latte/Renderer/Metal/MetalMemoryManager.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,12 @@ struct MetalBufferAllocation
{
void* data;
uint32 bufferIndex;
size_t bufferOffset;
size_t bufferOffset = INVALID_OFFSET;

bool IsValid() const
{
return bufferOffset != INVALID_OFFSET;
}
};

struct MetalBufferRange
Expand Down Expand Up @@ -62,7 +67,7 @@ struct MetalRestrideInfo
{
bool memoryInvalidated = true;
size_t lastStride = 0;
MTL::Buffer* buffer = nullptr;
MetalBufferAllocation allocation{};
};

struct MetalVertexBufferRange
Expand All @@ -77,7 +82,7 @@ class MetalVertexBufferCache
public:
friend class MetalMemoryManager;

MetalVertexBufferCache(class MetalRenderer* metalRenderer) : m_mtlr{metalRenderer} {}
MetalVertexBufferCache(class MetalRenderer* metalRenderer, MetalBufferAllocator* bufferAllocator) : m_mtlr{metalRenderer}, m_bufferAllocator{bufferAllocator} {}
~MetalVertexBufferCache();

void SetRestrideBufferPipeline(class MetalHybridComputePipeline* restrideBufferPipeline)
Expand All @@ -93,17 +98,14 @@ class MetalVertexBufferCache
void UntrackVertexBuffer(uint32 bufferIndex)
{
auto& range = m_bufferRanges[bufferIndex];
if (range.restrideInfo->buffer)
{
range.restrideInfo->buffer->release();
}
range.offset = INVALID_OFFSET;
}

MetalRestridedBufferRange RestrideBufferIfNeeded(MTL::Buffer* bufferCache, uint32 bufferIndex, size_t stride);

private:
class MetalRenderer* m_mtlr;
MetalBufferAllocator* m_bufferAllocator;

class MetalHybridComputePipeline* m_restrideBufferPipeline = nullptr;

Expand All @@ -115,7 +117,7 @@ class MetalVertexBufferCache
class MetalMemoryManager
{
public:
MetalMemoryManager(class MetalRenderer* metalRenderer) : m_mtlr{metalRenderer}, m_bufferAllocator(metalRenderer), m_vertexBufferCache(metalRenderer) {}
MetalMemoryManager(class MetalRenderer* metalRenderer) : m_mtlr{metalRenderer}, m_bufferAllocator(metalRenderer), m_vertexBufferCache(metalRenderer, &m_bufferAllocator) {}
~MetalMemoryManager();

// Pipelines
Expand Down
2 changes: 1 addition & 1 deletion src/Cafe/HW/Latte/Renderer/Metal/MetalRenderer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -795,7 +795,7 @@ void MetalRenderer::draw_execute(uint32 baseVertex, uint32 baseInstance, uint32
}

// Render pipeline state
MTL::RenderPipelineState* renderPipelineState = m_pipelineCache->GetPipelineState(fetchShader, vertexShader, pixelShader, m_state.m_activeFBO, LatteGPUState.contextNew);
MTL::RenderPipelineState* renderPipelineState = m_pipelineCache->GetPipelineState(fetchShader, vertexShader, pixelShader, m_state.m_lastUsedFBO, LatteGPUState.contextNew);
renderCommandEncoder->setRenderPipelineState(renderPipelineState);

// Uniform buffers, textures and samplers
Expand Down

0 comments on commit bba2bbc

Please sign in to comment.