Skip to content

Commit

Permalink
refac: simplify graphics_ext device allocator interface
Browse files Browse the repository at this point in the history
  • Loading branch information
jonathan hoffstadt committed Apr 23, 2024
1 parent a80ef59 commit 865c297
Show file tree
Hide file tree
Showing 6 changed files with 144 additions and 124 deletions.
11 changes: 8 additions & 3 deletions apps/app.c
Original file line number Diff line number Diff line change
Expand Up @@ -532,9 +532,6 @@ pl_app_update(plAppData* ptAppData)
}
if(pl_collapsing_header("Tools"))
{
if(pl_button("resize"))
ptAppData->bResize = true;
pl_checkbox("Always Resize", &ptAppData->bAlwaysResize);
pl_checkbox("Device Memory Analyzer", &ptAppData->tDebugInfo.bShowDeviceMemoryAnalyzer);
pl_checkbox("Memory Allocations", &ptAppData->tDebugInfo.bShowMemoryAllocations);
pl_checkbox("Profiling", &ptAppData->tDebugInfo.bShowProfiling);
Expand All @@ -544,6 +541,14 @@ pl_app_update(plAppData* ptAppData)
pl_end_collapsing_header();
}

if(pl_collapsing_header("Debug"))
{
if(pl_button("resize"))
ptAppData->bResize = true;
pl_checkbox("Always Resize", &ptAppData->bAlwaysResize);
pl_end_collapsing_header();
}

if(pl_collapsing_header("User Interface"))
{
pl_checkbox("UI Debug", &ptAppData->bShowUiDebug);
Expand Down
12 changes: 6 additions & 6 deletions extensions/pl_debug_ext.c
Original file line number Diff line number Diff line change
Expand Up @@ -823,10 +823,10 @@ pl__show_device_memory(bool* bValue)
pl_text("Host Memory: %llu bytes", (double)ptDevice->ptGraphics->szHostMemoryInUse);

const plDeviceMemoryAllocatorI atAllocators[] = {
*gptGpuAllocators->create_local_buddy_allocator(ptDevice),
*gptGpuAllocators->create_local_dedicated_allocator(ptDevice),
*gptGpuAllocators->create_staging_uncached_allocator(ptDevice),
*gptGpuAllocators->create_staging_uncached_allocator(ptDevice)
*gptGpuAllocators->get_local_buddy_allocator(ptDevice),
*gptGpuAllocators->get_local_dedicated_allocator(ptDevice),
*gptGpuAllocators->get_staging_uncached_allocator(ptDevice),
*gptGpuAllocators->get_staging_uncached_allocator(ptDevice)
};

const char* apcAllocatorNames[] = {
Expand All @@ -843,8 +843,8 @@ pl__show_device_memory(bool* bValue)
{
uint32_t uBlockCount = 0;
uint32_t uRangeCount = 0;
plDeviceAllocationBlock* sbtBlocks = atAllocators[uAllocatorIndex].blocks(atAllocators[uAllocatorIndex].ptInst, &uBlockCount);
plDeviceAllocationRange* sbtRanges = atAllocators[uAllocatorIndex].ranges(atAllocators[uAllocatorIndex].ptInst, &uRangeCount);
plDeviceAllocationBlock* sbtBlocks = gptGpuAllocators->get_blocks(&atAllocators[uAllocatorIndex], &uBlockCount);
plDeviceAllocationRange* sbtRanges = gptGpuAllocators->get_ranges(&atAllocators[uAllocatorIndex], &uRangeCount);
if(uBlockCount > 0)
{

Expand Down
36 changes: 16 additions & 20 deletions extensions/pl_gpu_allocators_ext.c
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,9 @@ typedef struct _plDeviceAllocatorData
} plDeviceAllocatorData;

static plDeviceAllocationBlock*
pl_get_allocator_blocks(struct plDeviceMemoryAllocatorO* ptInst, uint32_t* puSizeOut)
pl_get_allocator_blocks(const plDeviceMemoryAllocatorI* ptAllocator, uint32_t* puSizeOut)
{
plDeviceAllocatorData* ptData = (plDeviceAllocatorData*)ptInst;
plDeviceAllocatorData* ptData = (plDeviceAllocatorData*)ptAllocator->ptInst;

if(puSizeOut)
{
Expand All @@ -53,9 +53,9 @@ pl_get_allocator_blocks(struct plDeviceMemoryAllocatorO* ptInst, uint32_t* puSiz
}

static plDeviceAllocationRange*
pl_get_allocator_ranges(struct plDeviceMemoryAllocatorO* ptInst, uint32_t* puSizeOut)
pl_get_allocator_ranges(const plDeviceMemoryAllocatorI* ptAllocator, uint32_t* puSizeOut)
{
plDeviceAllocatorData* ptData = (plDeviceAllocatorData*)ptInst;
plDeviceAllocatorData* ptData = (plDeviceAllocatorData*)ptAllocator->ptInst;

if(puSizeOut)
{
Expand Down Expand Up @@ -598,22 +598,20 @@ pl_free_staging_uncached(struct plDeviceMemoryAllocatorO* ptInst, plDeviceMemory
}

static plDeviceMemoryAllocatorI*
pl_create_local_dedicated_allocator(plDevice* ptDevice)
pl_get_local_dedicated_allocator(plDevice* ptDevice)
{
static plDeviceAllocatorData tAllocatorData = {0};
static plDeviceMemoryAllocatorI tAllocator = {0};
tAllocatorData.ptDevice = ptDevice;
tAllocatorData.ptAllocator = &tAllocator;
tAllocator.allocate = pl_allocate_dedicated;
tAllocator.free = pl_free_dedicated;
tAllocator.blocks = pl_get_allocator_blocks;
tAllocator.ranges = pl_get_allocator_ranges;
tAllocator.ptInst = (struct plDeviceMemoryAllocatorO*)&tAllocatorData;
return &tAllocator;
}

static plDeviceMemoryAllocatorI*
pl_create_local_buddy_allocator(plDevice* ptDevice)
pl_get_local_buddy_allocator(plDevice* ptDevice)
{
static plDeviceAllocatorData tAllocatorData = {0};
static plDeviceMemoryAllocatorI tAllocator = {0};
Expand All @@ -628,31 +626,27 @@ pl_create_local_buddy_allocator(plDevice* ptDevice)
tAllocatorData.ptAllocator = &tAllocator;
tAllocator.allocate = pl_allocate_buddy;
tAllocator.free = pl_free_buddy;
tAllocator.blocks = pl_get_allocator_blocks;
tAllocator.ranges = pl_get_allocator_ranges;
tAllocator.ptInst = (struct plDeviceMemoryAllocatorO*)&tAllocatorData;
return &tAllocator;
}

static plDeviceMemoryAllocatorI*
pl_create_staging_uncached_allocator(plDevice* ptDevice)
pl_get_staging_uncached_allocator(plDevice* ptDevice)
{
static plDeviceAllocatorData tAllocatorData = {0};
static plDeviceMemoryAllocatorI tAllocator = {0};
tAllocatorData.ptDevice = ptDevice;
tAllocatorData.ptAllocator = &tAllocator;
tAllocator.allocate = pl_allocate_staging_uncached;
tAllocator.free = pl_free_staging_uncached;
tAllocator.blocks = pl_get_allocator_blocks;
tAllocator.ranges = pl_get_allocator_ranges;
tAllocator.ptInst = (struct plDeviceMemoryAllocatorO*)&tAllocatorData;
return &tAllocator;
}

static void
pl_cleanup_allocators(plDevice* ptDevice)
{
plDeviceMemoryAllocatorI* ptAllocator = pl_create_local_buddy_allocator(ptDevice);
plDeviceMemoryAllocatorI* ptAllocator = pl_get_local_buddy_allocator(ptDevice);
plDeviceAllocatorData* ptAllocatorData = (plDeviceAllocatorData*)ptAllocator->ptInst;

for(uint32_t i = 0; i < pl_sb_size(ptAllocatorData->sbtBlocks); i++)
Expand All @@ -664,7 +658,7 @@ pl_cleanup_allocators(plDevice* ptDevice)
pl_sb_free(ptAllocatorData->sbtNodes);
pl_sb_free(ptAllocatorData->sbtFreeBlockIndices);

ptAllocator = pl_create_local_dedicated_allocator(ptDevice);
ptAllocator = pl_get_local_dedicated_allocator(ptDevice);
ptAllocatorData = (plDeviceAllocatorData*)ptAllocator->ptInst;
for(uint32_t i = 0; i < pl_sb_size(ptAllocatorData->sbtBlocks); i++)
{
Expand All @@ -675,7 +669,7 @@ pl_cleanup_allocators(plDevice* ptDevice)
pl_sb_free(ptAllocatorData->sbtNodes);
pl_sb_free(ptAllocatorData->sbtFreeBlockIndices);

ptAllocator = pl_create_staging_uncached_allocator(ptDevice);
ptAllocator = pl_get_staging_uncached_allocator(ptDevice);
ptAllocatorData = (plDeviceAllocatorData*)ptAllocator->ptInst;
for(uint32_t i = 0; i < pl_sb_size(ptAllocatorData->sbtBlocks); i++)
{
Expand All @@ -695,10 +689,12 @@ const plGPUAllocatorsI*
pl_load_gpu_allocators_api(void)
{
static const plGPUAllocatorsI tApi = {
.create_local_dedicated_allocator = pl_create_local_dedicated_allocator,
.create_local_buddy_allocator = pl_create_local_buddy_allocator,
.create_staging_uncached_allocator = pl_create_staging_uncached_allocator,
.cleanup_allocators = 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_blocks = pl_get_allocator_blocks,
.get_ranges = pl_get_allocator_ranges,
.cleanup_allocators = pl_cleanup_allocators
};
return &tApi;
}
Expand Down
49 changes: 43 additions & 6 deletions extensions/pl_gpu_allocators_ext.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
/*
Index of this file:
// [SECTION] header mess
// [SECTION] includes
// [SECTION] defines
// [SECTION] forward declarations
// [SECTION] APIs
Expand All @@ -19,8 +20,14 @@ Index of this file:
#ifndef PL_GPU_ALLOCATORS_EXT_H
#define PL_GPU_ALLOCATORS_EXT_H

#define PL_GPU_ALLOCATORS_EXT_VERSION "0.9.0"
#define PL_GPU_ALLOCATORS_EXT_VERSION_NUM 000900
#define PL_GPU_ALLOCATORS_EXT_VERSION "0.10.0"
#define PL_GPU_ALLOCATORS_EXT_VERSION_NUM 010000

//-----------------------------------------------------------------------------
// [SECTION] includes
//-----------------------------------------------------------------------------

#include <stdint.h>

//-----------------------------------------------------------------------------
// [SECTION] defines
Expand All @@ -34,10 +41,19 @@ Index of this file:
#define PL_DEVICE_LOCAL_LEVELS 8
#endif

#ifndef PL_MAX_NAME_LENGTH
#define PL_MAX_NAME_LENGTH 1024
#endif

//-----------------------------------------------------------------------------
// [SECTION] forward declarations
//-----------------------------------------------------------------------------

// basic types
typedef struct _plDeviceAllocationRange plDeviceAllocationRange;
typedef struct _plDeviceAllocationBlock plDeviceAllocationBlock;

// external (pl_graphics_ext.h)
typedef struct _plDeviceMemoryAllocatorI plDeviceMemoryAllocatorI;
typedef struct _plDevice plDevice;

Expand All @@ -60,11 +76,32 @@ const plGPUAllocatorsI* pl_load_gpu_allocators_api(void);

typedef struct _plGPUAllocatorsI
{
plDeviceMemoryAllocatorI* (*create_local_dedicated_allocator) (plDevice* ptDevice);
plDeviceMemoryAllocatorI* (*create_local_buddy_allocator) (plDevice* ptDevice);
plDeviceMemoryAllocatorI* (*create_staging_uncached_allocator)(plDevice* ptDevice);
// allocators
plDeviceMemoryAllocatorI* (*get_local_dedicated_allocator) (plDevice*);
plDeviceMemoryAllocatorI* (*get_local_buddy_allocator) (plDevice*);
plDeviceMemoryAllocatorI* (*get_staging_uncached_allocator)(plDevice*);

void (*cleanup_allocators)(plDevice*);

// for debug viewing
plDeviceAllocationBlock* (*get_blocks)(const plDeviceMemoryAllocatorI*, uint32_t* puSizeOut);
plDeviceAllocationRange* (*get_ranges)(const plDeviceMemoryAllocatorI*, uint32_t* puSizeOut);

void (*cleanup_allocators)(plDevice* ptDevice);
} plGPUAllocatorsI;

//-----------------------------------------------------------------------------
// [SECTION] structs
//-----------------------------------------------------------------------------

typedef struct _plDeviceAllocationRange
{
char acName[PL_MAX_NAME_LENGTH];
uint64_t ulOffset;
uint64_t ulUsedSize;
uint64_t ulTotalSize;
uint64_t ulBlockIndex;
uint32_t uNodeIndex;
uint32_t uNextNode;
} plDeviceAllocationRange;

#endif // PL_GPU_ALLOCATORS_EXT_H
Loading

0 comments on commit 865c297

Please sign in to comment.