Skip to content

Commit

Permalink
feat: add dedicated staging uncached allocator back
Browse files Browse the repository at this point in the history
  • Loading branch information
jonathan hoffstadt authored and hoffstadt committed Jan 23, 2025
1 parent 38be5cc commit 36cda69
Show file tree
Hide file tree
Showing 7 changed files with 157 additions and 21 deletions.
7 changes: 5 additions & 2 deletions extensions/pl_debug_ext.c
Original file line number Diff line number Diff line change
Expand Up @@ -231,12 +231,13 @@ pl__show_memory_allocations(bool* bValue)

size_t szOriginalAllocationCount = 0;
plAllocationEntry* atAllocations = gptMemory->get_allocations(&szOriginalAllocationCount);

plUiClipper tClipper = {(uint32_t)szOriginalAllocationCount};
while(gptUI->step_clipper(&tClipper))
{
for(uint32_t i = tClipper.uDisplayStart; i < tClipper.uDisplayEnd; i++)
{
size_t szUnused = 0;
atAllocations = gptMemory->get_allocations(&szUnused);
plAllocationEntry tEntry = atAllocations[i];
strncpy(pcFile, tEntry.pcFile, 1024);
gptUI->text("%i", i);
Expand Down Expand Up @@ -878,6 +879,7 @@ pl__show_device_memory(bool* bValue)
const plDeviceMemoryAllocatorI atAllocators[] = {
*gptGpuAllocators->get_local_buddy_allocator(gptDebugCtx->ptDevice),
*gptGpuAllocators->get_local_dedicated_allocator(gptDebugCtx->ptDevice),
*gptGpuAllocators->get_staging_uncached_buddy_allocator(gptDebugCtx->ptDevice),
*gptGpuAllocators->get_staging_uncached_allocator(gptDebugCtx->ptDevice),
*gptGpuAllocators->get_staging_cached_allocator(gptDebugCtx->ptDevice)
};
Expand All @@ -886,13 +888,14 @@ pl__show_device_memory(bool* bValue)
"Device Memory: Local Buddy",
"Device Memory: Local Dedicated",
"Device Memory: Staging Uncached Buddy",
"Device Memory: Staging Uncached",
"Device Memory: Staging Cached"
};

gptUI->push_theme_color(PL_UI_COLOR_BUTTON, tButtonColor);
gptUI->push_theme_color(PL_UI_COLOR_BUTTON_ACTIVE, tButtonColor);
gptUI->push_theme_color(PL_UI_COLOR_BUTTON_HOVERED, tButtonColor);
for(uint32_t uAllocatorIndex = 0; uAllocatorIndex < 4; uAllocatorIndex++)
for(uint32_t uAllocatorIndex = 0; uAllocatorIndex < 5; uAllocatorIndex++)
{
uint32_t uBlockCount = 0;
uint32_t uRangeCount = 0;
Expand Down
128 changes: 121 additions & 7 deletions extensions/pl_gpu_allocators_ext.c
Original file line number Diff line number Diff line change
Expand Up @@ -552,6 +552,75 @@ pl_free_buddy(struct plDeviceMemoryAllocatorO* ptInst, plDeviceMemoryAllocation*
strncpy(ptNode->acName, "not used", PL_MAX_NAME_LENGTH);
}

static plDeviceMemoryAllocation
pl_allocate_staging_uncached(
struct plDeviceMemoryAllocatorO* ptInst, uint32_t uTypeFilter,
uint64_t ulSize, uint64_t ulAlignment, const char* pcName)
{
plDeviceAllocatorData* ptData = (plDeviceAllocatorData*)ptInst;

plDeviceMemoryAllocation tBlock = gptGfx->allocate_memory(ptData->ptDevice, ulSize,
PL_MEMORY_GPU_CPU, uTypeFilter, pcName);

plDeviceMemoryAllocation tAllocation = {
.pHostMapped = tBlock.pHostMapped,
.uHandle = tBlock.uHandle,
.ulOffset = 0,
.ulSize = ulSize,
.ptAllocator = ptData->ptAllocator,
.tMemoryMode = PL_MEMORY_GPU_CPU
};

uint32_t uBlockIndex = pl_sb_size(ptData->sbtBlocks);
if(pl_sb_size(ptData->sbtFreeBlockIndices) > 0)
uBlockIndex = pl_sb_pop(ptData->sbtFreeBlockIndices);
else
pl_sb_add(ptData->sbtBlocks);

plDeviceAllocationRange tRange = {
.ulOffset = 0,
.ulTotalSize = ulSize,
.ulUsedSize = ulSize,
.ulBlockIndex = uBlockIndex
};
pl_sprintf(tRange.acName, "%s", pcName);

pl_sb_push(ptData->sbtNodes, tRange);
ptData->sbtBlocks[uBlockIndex] = tBlock;
return tAllocation;
}

static void
pl_free_staging_uncached(struct plDeviceMemoryAllocatorO* ptInst, plDeviceMemoryAllocation* ptAllocation)
{
plDeviceAllocatorData* ptData = (plDeviceAllocatorData*)ptInst;

uint32_t uBlockIndex = 0;
uint32_t uNodeIndex = 0;
for(uint32_t i = 0; i < pl_sb_size(ptData->sbtNodes); i++)
{
plDeviceAllocationRange* ptNode = &ptData->sbtNodes[i];
plDeviceMemoryAllocation* ptBlock = &ptData->sbtBlocks[ptNode->ulBlockIndex];

if(ptBlock->uHandle == ptAllocation->uHandle)
{
uNodeIndex = i;
uBlockIndex = (uint32_t)ptNode->ulBlockIndex;
ptBlock->ulSize = 0;
break;
}
}
pl_sb_del_swap(ptData->sbtNodes, uNodeIndex);
pl_sb_push(ptData->sbtFreeBlockIndices, uBlockIndex);

gptGfx->free_memory(ptData->ptDevice, &ptData->sbtBlocks[uBlockIndex]);

ptAllocation->pHostMapped = NULL;
ptAllocation->uHandle = 0;
ptAllocation->ulOffset = 0;
ptAllocation->ulSize = 0;
}

static plDeviceMemoryAllocation
pl_allocate_staging_uncached_buddy(
struct plDeviceMemoryAllocatorO* ptInst, uint32_t uTypeFilter,
Expand Down Expand Up @@ -763,6 +832,39 @@ pl_get_staging_uncached_allocator(plDevice* ptDevice)

ptAllocatorData = &gtAllocatorData;
ptAllocator = &gtAllocator;
}
}
ptAllocator->allocate = pl_allocate_staging_uncached;
ptAllocator->free = pl_free_staging_uncached;
return ptAllocator;
}

static plDeviceMemoryAllocatorI*
pl_get_staging_uncached_allocator_buddy(plDevice* ptDevice)
{
static plDeviceAllocatorData* ptAllocatorData = NULL;
static plDeviceMemoryAllocatorI* ptAllocator = NULL;

if(ptAllocator == NULL)
{
ptAllocator = gptDataRegistry->get_data("StagingUncachedAllocatorBuddy");

if(ptAllocator)
ptAllocatorData = gptDataRegistry->get_data("StagingUncachedAllocatorBuddyData");

else
{

static plDeviceMemoryAllocatorI gtAllocator = {0};
static plDeviceAllocatorData gtAllocatorData = {0};
gptDataRegistry->set_data("StagingUncachedAllocatorBuddy", &gtAllocator);
gptDataRegistry->set_data("StagingUncachedAllocatorBuddyData", &gtAllocatorData);
gtAllocatorData.ptDevice = ptDevice;
gtAllocatorData.ptAllocator = &gtAllocator;
gtAllocator.ptInst = (struct plDeviceMemoryAllocatorO*)&gtAllocatorData;

ptAllocatorData = &gtAllocatorData;
ptAllocator = &gtAllocator;

if(ptAllocatorData->auFreeList[0] == 0)
{
Expand Down Expand Up @@ -856,6 +958,17 @@ pl_cleanup_allocators(plDevice* ptDevice)
pl_sb_free(ptAllocatorData->sbtBlocks);
pl_sb_free(ptAllocatorData->sbtNodes);
pl_sb_free(ptAllocatorData->sbtFreeBlockIndices);

ptAllocator = pl_get_staging_uncached_allocator_buddy(ptDevice);
ptAllocatorData = (plDeviceAllocatorData*)ptAllocator->ptInst;
for(uint32_t i = 0; i < pl_sb_size(ptAllocatorData->sbtBlocks); i++)
{
if(ptAllocatorData->sbtBlocks[i].uHandle)
gptGfx->free_memory(ptDevice, &ptAllocatorData->sbtBlocks[i]);
}
pl_sb_free(ptAllocatorData->sbtBlocks);
pl_sb_free(ptAllocatorData->sbtNodes);
pl_sb_free(ptAllocatorData->sbtFreeBlockIndices);
}

//-----------------------------------------------------------------------------
Expand All @@ -866,13 +979,14 @@ PL_EXPORT void
pl_load_gpu_allocators_ext(plApiRegistryI* ptApiRegistry, bool bReload)
{
const plGPUAllocatorsI tApi = {
.get_local_dedicated_allocator = pl_get_local_dedicated_allocator,
.get_local_buddy_allocator = pl_get_local_buddy_allocator,
.get_staging_uncached_allocator = pl_get_staging_uncached_allocator,
.get_staging_cached_allocator = pl_get_staging_cached_allocator,
.get_blocks = pl_get_allocator_blocks,
.get_ranges = pl_get_allocator_ranges,
.cleanup = pl_cleanup_allocators
.get_local_dedicated_allocator = pl_get_local_dedicated_allocator,
.get_local_buddy_allocator = pl_get_local_buddy_allocator,
.get_staging_uncached_allocator = pl_get_staging_uncached_allocator,
.get_staging_cached_allocator = pl_get_staging_cached_allocator,
.get_staging_uncached_buddy_allocator = pl_get_staging_uncached_allocator_buddy,
.get_blocks = pl_get_allocator_blocks,
.get_ranges = pl_get_allocator_ranges,
.cleanup = pl_cleanup_allocators
};
pl_set_api(ptApiRegistry, plGPUAllocatorsI, &tApi);

Expand Down
11 changes: 6 additions & 5 deletions extensions/pl_gpu_allocators_ext.h
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ typedef struct _plDevice plDevice;
// [SECTION] APIs
//-----------------------------------------------------------------------------

#define plGPUAllocatorsI_version (plVersion){1, 0, 0}
#define plGPUAllocatorsI_version (plVersion){1, 1, 0}

//-----------------------------------------------------------------------------
// [SECTION] public api structs
Expand All @@ -50,10 +50,11 @@ typedef struct _plDevice plDevice;
typedef struct _plGPUAllocatorsI
{
// allocators
plDeviceMemoryAllocatorI* (*get_local_dedicated_allocator) (plDevice*);
plDeviceMemoryAllocatorI* (*get_local_buddy_allocator) (plDevice*);
plDeviceMemoryAllocatorI* (*get_staging_uncached_allocator)(plDevice*);
plDeviceMemoryAllocatorI* (*get_staging_cached_allocator) (plDevice*);
plDeviceMemoryAllocatorI* (*get_local_dedicated_allocator) (plDevice*);
plDeviceMemoryAllocatorI* (*get_local_buddy_allocator) (plDevice*);
plDeviceMemoryAllocatorI* (*get_staging_uncached_allocator) (plDevice*);
plDeviceMemoryAllocatorI* (*get_staging_uncached_buddy_allocator)(plDevice*);
plDeviceMemoryAllocatorI* (*get_staging_cached_allocator) (plDevice*);

void (*cleanup)(plDevice*);

Expand Down
11 changes: 6 additions & 5 deletions extensions/pl_renderer_ext.c
Original file line number Diff line number Diff line change
Expand Up @@ -180,15 +180,16 @@ pl_refr_initialize(plWindow* ptWindow)
}

// load gpu allocators
gptData->ptLocalBuddyAllocator = gptGpuAllocators->get_local_buddy_allocator(gptData->ptDevice);
gptData->ptLocalDedicatedAllocator = gptGpuAllocators->get_local_dedicated_allocator(gptData->ptDevice);
gptData->ptStagingUnCachedAllocator = gptGpuAllocators->get_staging_uncached_allocator(gptData->ptDevice);
gptData->ptStagingCachedAllocator = gptGpuAllocators->get_staging_cached_allocator(gptData->ptDevice);
gptData->ptLocalBuddyAllocator = gptGpuAllocators->get_local_buddy_allocator(gptData->ptDevice);
gptData->ptLocalDedicatedAllocator = gptGpuAllocators->get_local_dedicated_allocator(gptData->ptDevice);
gptData->ptStagingUnCachedAllocator = gptGpuAllocators->get_staging_uncached_allocator(gptData->ptDevice);
gptData->ptStagingUnCachedBuddyAllocator = gptGpuAllocators->get_staging_uncached_buddy_allocator(gptData->ptDevice);
gptData->ptStagingCachedAllocator = gptGpuAllocators->get_staging_cached_allocator(gptData->ptDevice);

// create staging buffers
const plBufferDesc tStagingBufferDesc = {
.tUsage = PL_BUFFER_USAGE_STAGING,
.szByteSize = 268435456 / 2
.szByteSize = 268435456
};
for(uint32_t i = 0; i < gptGfx->get_frames_in_flight(); i++)
gptData->tStagingBufferHandle[i] = pl__refr_create_staging_buffer(&tStagingBufferDesc, "staging", i);
Expand Down
11 changes: 10 additions & 1 deletion extensions/pl_renderer_internal.c
Original file line number Diff line number Diff line change
Expand Up @@ -300,8 +300,17 @@ pl__refr_create_staging_buffer(const plBufferDesc* ptDesc, const char* pcName, u
plBuffer* ptBuffer = NULL;
const plBufferHandle tHandle = gptGfx->create_buffer(ptDevice, ptDesc, &ptBuffer);

// PL_DEVICE_BUDDY_BLOCK_SIZE

plDeviceMemoryAllocatorI* ptAllocator = NULL;

if(ptBuffer->tMemoryRequirements.ulSize > PL_DEVICE_BUDDY_BLOCK_SIZE)
ptAllocator = gptData->ptStagingUnCachedAllocator;
else
ptAllocator = gptData->ptStagingUnCachedBuddyAllocator;

// allocate memory
const plDeviceMemoryAllocation tAllocation = gptData->ptStagingUnCachedAllocator->allocate(gptData->ptStagingUnCachedAllocator->ptInst,
const plDeviceMemoryAllocation tAllocation = ptAllocator->allocate(ptAllocator->ptInst,
ptBuffer->tMemoryRequirements.uMemoryTypeBits,
ptBuffer->tMemoryRequirements.ulSize,
ptBuffer->tMemoryRequirements.ulAlignment,
Expand Down
1 change: 1 addition & 0 deletions extensions/pl_renderer_internal.h
Original file line number Diff line number Diff line change
Expand Up @@ -515,6 +515,7 @@ typedef struct _plRefRendererData
plDeviceMemoryAllocatorI* ptLocalDedicatedAllocator;
plDeviceMemoryAllocatorI* ptLocalBuddyAllocator;
plDeviceMemoryAllocatorI* ptStagingUnCachedAllocator;
plDeviceMemoryAllocatorI* ptStagingUnCachedBuddyAllocator;
plDeviceMemoryAllocatorI* ptStagingCachedAllocator;

// default textures & samplers & bindgroups
Expand Down
9 changes: 8 additions & 1 deletion sandbox/pl_ecs_tools.c
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,8 @@ pl_show_ecs_window(plEntity* ptSelectedEntity, plComponentLibrary* ptLibrary, bo
plInverseKinematicsComponent* ptIKComp = gptEcs->get_component(ptLibrary, PL_COMPONENT_TYPE_INVERSE_KINEMATICS, *ptSelectedEntity);
plLightComponent* ptLightComp = gptEcs->get_component(ptLibrary, PL_COMPONENT_TYPE_LIGHT, *ptSelectedEntity);
plEnvironmentProbeComponent* ptProbeComp = gptEcs->get_component(ptLibrary, PL_COMPONENT_TYPE_ENVIRONMENT_PROBE, *ptSelectedEntity);
plHumanoidComponent* ptHumanComp = gptEcs->get_component(ptLibrary, PL_COMPONENT_TYPE_HUMANOID, *ptSelectedEntity);


gptUi->text("Entity: %u, %u", ptSelectedEntity->uIndex, ptSelectedEntity->uGeneration);

Expand All @@ -107,6 +109,11 @@ pl_show_ecs_window(plEntity* ptSelectedEntity, plComponentLibrary* ptLibrary, bo
gptUi->end_collapsing_header();
}

if(ptHumanComp && gptUi->begin_collapsing_header("Humanoid", 0))
{
gptUi->end_collapsing_header();
}

if(ptProbeComp && gptUi->begin_collapsing_header("Environment Probe", 0))
{
gptUi->input_float3("Position", ptProbeComp->tPosition.d, NULL, 0);
Expand Down Expand Up @@ -331,7 +338,7 @@ pl_show_ecs_window(plEntity* ptSelectedEntity, plComponentLibrary* ptLibrary, bo
// int iResolution = (int)ptLight->uShadowResolution;
gptUi->input_float4("Cascade Splits", ptLightComp->afCascadeSplits, NULL, 0);
}

gptUi->end_collapsing_header();
}

if(ptMaterialComp && gptUi->begin_collapsing_header("Material", 0))
Expand Down

0 comments on commit 36cda69

Please sign in to comment.