diff --git a/extensions/pl_debug_ext.c b/extensions/pl_debug_ext.c index b0b21a7..d989335 100644 --- a/extensions/pl_debug_ext.c +++ b/extensions/pl_debug_ext.c @@ -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); @@ -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) }; @@ -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; diff --git a/extensions/pl_gpu_allocators_ext.c b/extensions/pl_gpu_allocators_ext.c index e2825c4..a845a76 100644 --- a/extensions/pl_gpu_allocators_ext.c +++ b/extensions/pl_gpu_allocators_ext.c @@ -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, @@ -763,6 +832,39 @@ pl_get_staging_uncached_allocator(plDevice* ptDevice) ptAllocatorData = >AllocatorData; ptAllocator = >Allocator; + } + } + 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", >Allocator); + gptDataRegistry->set_data("StagingUncachedAllocatorBuddyData", >AllocatorData); + gtAllocatorData.ptDevice = ptDevice; + gtAllocatorData.ptAllocator = >Allocator; + gtAllocator.ptInst = (struct plDeviceMemoryAllocatorO*)>AllocatorData; + + ptAllocatorData = >AllocatorData; + ptAllocator = >Allocator; if(ptAllocatorData->auFreeList[0] == 0) { @@ -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); } //----------------------------------------------------------------------------- @@ -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); diff --git a/extensions/pl_gpu_allocators_ext.h b/extensions/pl_gpu_allocators_ext.h index 1587be9..be47f89 100644 --- a/extensions/pl_gpu_allocators_ext.h +++ b/extensions/pl_gpu_allocators_ext.h @@ -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 @@ -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*); diff --git a/extensions/pl_renderer_ext.c b/extensions/pl_renderer_ext.c index 6a3179b..c0830f4 100644 --- a/extensions/pl_renderer_ext.c +++ b/extensions/pl_renderer_ext.c @@ -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); diff --git a/extensions/pl_renderer_internal.c b/extensions/pl_renderer_internal.c index e45f268..eafc71b 100644 --- a/extensions/pl_renderer_internal.c +++ b/extensions/pl_renderer_internal.c @@ -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, diff --git a/extensions/pl_renderer_internal.h b/extensions/pl_renderer_internal.h index f56844f..7d05494 100644 --- a/extensions/pl_renderer_internal.h +++ b/extensions/pl_renderer_internal.h @@ -515,6 +515,7 @@ typedef struct _plRefRendererData plDeviceMemoryAllocatorI* ptLocalDedicatedAllocator; plDeviceMemoryAllocatorI* ptLocalBuddyAllocator; plDeviceMemoryAllocatorI* ptStagingUnCachedAllocator; + plDeviceMemoryAllocatorI* ptStagingUnCachedBuddyAllocator; plDeviceMemoryAllocatorI* ptStagingCachedAllocator; // default textures & samplers & bindgroups diff --git a/sandbox/pl_ecs_tools.c b/sandbox/pl_ecs_tools.c index 0faf262..c629392 100644 --- a/sandbox/pl_ecs_tools.c +++ b/sandbox/pl_ecs_tools.c @@ -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); @@ -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); @@ -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))