From 3a8ac14ce5d597a38637c03239453fe4c34bdfc8 Mon Sep 17 00:00:00 2001 From: Jonathan Hoffstadt Date: Sat, 2 Nov 2024 23:26:00 -0500 Subject: [PATCH] refac: inline some graphics ext. functions --- examples/example_2.c | 2 +- examples/example_3.c | 2 +- examples/example_6.c | 3 +- examples/example_8.c | 2 +- examples/example_9.c | 2 +- extensions/pl_draw_backend_ext.c | 12 +- extensions/pl_graphics_ext.c | 136 +++----------------- extensions/pl_graphics_ext.h | 200 ++++++++++++++++++++++++++++-- extensions/pl_graphics_internal.h | 22 ---- extensions/pl_graphics_metal.m | 82 ++++++------ extensions/pl_graphics_vulkan.c | 85 +++++-------- extensions/pl_renderer_ext.c | 72 ++++++----- 12 files changed, 332 insertions(+), 288 deletions(-) diff --git a/examples/example_2.c b/examples/example_2.c index 8c5fba9..a095435 100644 --- a/examples/example_2.c +++ b/examples/example_2.c @@ -79,7 +79,7 @@ const plWindowI* gptWindows = NULL; const plGraphicsI* gptGfx = NULL; const plDrawI* gptDraw = NULL; const plShaderI* gptShader = NULL; -const plDrawBackendI* gptDrawBackend = NULL; // not yet stable +const plDrawBackendI* gptDrawBackend = NULL; //----------------------------------------------------------------------------- // [SECTION] pl_app_load diff --git a/examples/example_3.c b/examples/example_3.c index cc77fa0..6555f10 100644 --- a/examples/example_3.c +++ b/examples/example_3.c @@ -80,7 +80,7 @@ const plGraphicsI* gptGfx = NULL; const plDrawI* gptDraw = NULL; const plUiI* gptUi = NULL; const plShaderI* gptShader = NULL; -const plDrawBackendI* gptDrawBackend = NULL; // not yet stable +const plDrawBackendI* gptDrawBackend = NULL; //----------------------------------------------------------------------------- // [SECTION] pl_app_load diff --git a/examples/example_6.c b/examples/example_6.c index 85d4e5a..f2c6d7e 100644 --- a/examples/example_6.c +++ b/examples/example_6.c @@ -627,7 +627,8 @@ pl_app_update(plAppData* ptAppData) gptGfx->bind_vertex_buffer(ptEncoder, ptAppData->tVertexBuffer); // retrieve dynamic binding data - plDynamicBinding tDynamicBinding = gptGfx->allocate_dynamic_data(ptAppData->ptDevice, sizeof(plVec4)); + plDynamicDataBlock tCurrentDynamicDataBlock = {0}; + plDynamicBinding tDynamicBinding = pl_allocate_dynamic_data(gptGfx, ptAppData->ptDevice, &tCurrentDynamicDataBlock); plVec4* tTintColor = (plVec4*)tDynamicBinding.pcData; tTintColor->r = 1.0f; tTintColor->g = 1.0f; diff --git a/examples/example_8.c b/examples/example_8.c index 8141283..1d3ba78 100644 --- a/examples/example_8.c +++ b/examples/example_8.c @@ -101,7 +101,7 @@ const plWindowI* gptWindows = NULL; const plGraphicsI* gptGfx = NULL; const plDrawI* gptDraw = NULL; const plShaderI* gptShader = NULL; -const plDrawBackendI* gptDrawBackend = NULL; // not yet stable +const plDrawBackendI* gptDrawBackend = NULL; //----------------------------------------------------------------------------- // [SECTION] helper function declarations diff --git a/examples/example_9.c b/examples/example_9.c index 0bf1efe..249de63 100644 --- a/examples/example_9.c +++ b/examples/example_9.c @@ -116,7 +116,7 @@ const plWindowI* gptWindows = NULL; const plGraphicsI* gptGfx = NULL; const plDrawI* gptDraw = NULL; const plShaderI* gptShader = NULL; -const plDrawBackendI* gptDrawBackend = NULL; // not yet stable +const plDrawBackendI* gptDrawBackend = NULL; //----------------------------------------------------------------------------- // [SECTION] helper function declarations diff --git a/extensions/pl_draw_backend_ext.c b/extensions/pl_draw_backend_ext.c index ba23417..44b5c3d 100644 --- a/extensions/pl_draw_backend_ext.c +++ b/extensions/pl_draw_backend_ext.c @@ -48,10 +48,12 @@ typedef struct _plDrawBackendContext uint32_t auIndexBufferSize[PL_MAX_FRAMES_IN_FLIGHT]; uint32_t auIndexBufferOffset[PL_MAX_FRAMES_IN_FLIGHT]; - plBufferInfo atBufferInfo[PL_MAX_FRAMES_IN_FLIGHT]; plBufferInfo at3DBufferInfo[PL_MAX_FRAMES_IN_FLIGHT]; plBufferInfo atLineBufferInfo[PL_MAX_FRAMES_IN_FLIGHT]; + + // dynamic buffer system + plDynamicDataBlock tCurrentDynamicDataBlock; } plDrawBackendContext; //----------------------------------------------------------------------------- @@ -69,7 +71,6 @@ static const plPipelineEntry* pl__get_3d_pipeline (plRenderPassHand static const plPipelineEntry* pl__get_2d_pipeline (plRenderPassHandle, uint32_t uMSAASampleCount, uint32_t uSubpassIndex); static plBindGroupHandle pl_create_bind_group_for_texture(plTextureHandle); - //----------------------------------------------------------------------------- // [SECTION] public api implementation //----------------------------------------------------------------------------- @@ -189,6 +190,7 @@ pl_new_draw_frame(void) *pd3dPipelineCount = pl_sb_size(gptDrawBackendCtx->sbt3dPipelineEntries); gptDraw->new_frame(); + gptDrawBackendCtx->tCurrentDynamicDataBlock = gptGfx->allocate_dynamic_data_block(gptDrawBackendCtx->ptDevice); // reset buffer offsets for(uint32_t i = 0; i < gptGfx->get_frames_in_flight(); i++) @@ -685,7 +687,7 @@ pl_submit_2d_drawlist(plDrawList2D* ptDrawlist, plRenderEncoder* ptEncoder, floa plVec2 uTranslate; } plDrawDynamicData; - plDynamicBinding tDynamicBinding = gptGfx->allocate_dynamic_data(ptDevice, sizeof(plDrawDynamicData)); + plDynamicBinding tDynamicBinding = pl_allocate_dynamic_data(gptGfx, ptDevice, &gptDrawBackendCtx->tCurrentDynamicDataBlock); plDrawDynamicData* ptDynamicData = (plDrawDynamicData*)tDynamicBinding.pcData; ptDynamicData->uScale.x = 2.0f / fWidth; @@ -850,7 +852,7 @@ pl_submit_3d_drawlist(plDrawList3D* ptDrawlist, plRenderEncoder* ptEncoder, floa char* pucMappedIndexBufferLocation = ptIndexBuffer->tMemoryAllocation.pHostMapped; memcpy(&pucMappedIndexBufferLocation[gptDrawBackendCtx->auIndexBufferOffset[uFrameIdx]], ptDrawlist->sbtSolidIndexBuffer, sizeof(uint32_t) * pl_sb_size(ptDrawlist->sbtSolidIndexBuffer)); - plDynamicBinding tSolidDynamicData = gptGfx->allocate_dynamic_data(gptDrawBackendCtx->ptDevice, sizeof(plMat4)); + plDynamicBinding tSolidDynamicData = pl_allocate_dynamic_data(gptGfx, gptDrawBackendCtx->ptDevice, &gptDrawBackendCtx->tCurrentDynamicDataBlock); plMat4* ptSolidDynamicData = (plMat4*)tSolidDynamicData.pcData; *ptSolidDynamicData = *ptMVP; @@ -944,7 +946,7 @@ pl_submit_3d_drawlist(plDrawList3D* ptDrawlist, plRenderEncoder* ptEncoder, floa int padding[3]; } plLineDynamiceData; - plDynamicBinding tLineDynamicData = gptGfx->allocate_dynamic_data(ptDevice, sizeof(plLineDynamiceData)); + plDynamicBinding tLineDynamicData = pl_allocate_dynamic_data(gptGfx, gptDrawBackendCtx->ptDevice, &gptDrawBackendCtx->tCurrentDynamicDataBlock); plLineDynamiceData* ptLineDynamicData = (plLineDynamiceData*)tLineDynamicData.pcData; ptLineDynamicData->tMVP = *ptMVP; ptLineDynamicData->fAspect = fAspectRatio; diff --git a/extensions/pl_graphics_ext.c b/extensions/pl_graphics_ext.c index f5ecc6c..43aa957 100644 --- a/extensions/pl_graphics_ext.c +++ b/extensions/pl_graphics_ext.c @@ -247,132 +247,31 @@ pl_queue_sampler_for_deletion(plDevice* ptDevice, plSamplerHandle tHandle) } static void -pl_drawstream_cleanup(plDrawStream* ptStream) +pl_draw_stream_cleanup(plDrawStream* ptStream) { - memset(&ptStream->_tCurrentDraw, 255, sizeof(plDrawStreamData)); - pl_sb_free(ptStream->_sbtStream); + memset(&ptStream->_tCurrentDraw, 255, sizeof(plDrawStreamData)); + PL_FREE(ptStream->_auStream); + ptStream->_uStreamCapacity = 0; + ptStream->_uStreamCount = 0; } static void -pl_drawstream_reset(plDrawStream* ptStream) +pl_draw_stream_reset(plDrawStream* ptStream, uint32_t uDrawCount) { memset(&ptStream->_tCurrentDraw, 255, sizeof(plDrawStreamData)); ptStream->_tCurrentDraw.uIndexBuffer = UINT32_MAX - 1; ptStream->_tCurrentDraw.uDynamicBufferOffset0 = 0; - pl_sb_reset(ptStream->_sbtStream); -} - -static void -pl_drawstream_draw(plDrawStream* ptStream, plDrawStreamData tDraw) -{ - - uint32_t uDirtyMask = PL_DRAW_STREAM_BIT_NONE; - - if(ptStream->_tCurrentDraw.uShaderVariant != tDraw.uShaderVariant) - { - ptStream->_tCurrentDraw.uShaderVariant = tDraw.uShaderVariant; - uDirtyMask |= PL_DRAW_STREAM_BIT_SHADER; - } - - if(ptStream->_tCurrentDraw.uDynamicBufferOffset0 != tDraw.uDynamicBufferOffset0) - { - ptStream->_tCurrentDraw.uDynamicBufferOffset0 = tDraw.uDynamicBufferOffset0; - uDirtyMask |= PL_DRAW_STREAM_BIT_DYNAMIC_OFFSET_0; - } - - if(ptStream->_tCurrentDraw.uBindGroup0 != tDraw.uBindGroup0) - { - ptStream->_tCurrentDraw.uBindGroup0 = tDraw.uBindGroup0; - uDirtyMask |= PL_DRAW_STREAM_BIT_BINDGROUP_0; - } - - if(ptStream->_tCurrentDraw.uBindGroup1 != tDraw.uBindGroup1) - { - ptStream->_tCurrentDraw.uBindGroup1 = tDraw.uBindGroup1; - uDirtyMask |= PL_DRAW_STREAM_BIT_BINDGROUP_1; - } - - if(ptStream->_tCurrentDraw.uBindGroup2 != tDraw.uBindGroup2) - { - ptStream->_tCurrentDraw.uBindGroup2 = tDraw.uBindGroup2; - uDirtyMask |= PL_DRAW_STREAM_BIT_BINDGROUP_2; - } + ptStream->_uStreamCount = 0; - if(ptStream->_tCurrentDraw.uDynamicBuffer0 != tDraw.uDynamicBuffer0) + if(uDrawCount * 13 > ptStream->_uStreamCapacity) { - ptStream->_tCurrentDraw.uDynamicBuffer0 = tDraw.uDynamicBuffer0; - uDirtyMask |= PL_DRAW_STREAM_BIT_DYNAMIC_BUFFER_0; + uint32_t* auOldStream = ptStream->_auStream; + uint32_t uNewCapacity = uDrawCount * 13; + ptStream->_auStream = PL_ALLOC(sizeof(uint32_t) * uNewCapacity); + memset(ptStream->_auStream, 0, sizeof(uint32_t) * uNewCapacity); + ptStream->_uStreamCapacity = uNewCapacity; + PL_FREE(auOldStream); } - - if(ptStream->_tCurrentDraw.uIndexOffset != tDraw.uIndexOffset) - { - ptStream->_tCurrentDraw.uIndexOffset = tDraw.uIndexOffset; - uDirtyMask |= PL_DRAW_STREAM_BIT_INDEX_OFFSET; - } - - if(ptStream->_tCurrentDraw.uVertexOffset != tDraw.uVertexOffset) - { - ptStream->_tCurrentDraw.uVertexOffset = tDraw.uVertexOffset; - uDirtyMask |= PL_DRAW_STREAM_BIT_VERTEX_OFFSET; - } - - if(ptStream->_tCurrentDraw.uIndexBuffer != tDraw.uIndexBuffer) - { - ptStream->_tCurrentDraw.uIndexBuffer = tDraw.uIndexBuffer; - uDirtyMask |= PL_DRAW_STREAM_BIT_INDEX_BUFFER; - } - - if(ptStream->_tCurrentDraw.uVertexBuffer0 != tDraw.uVertexBuffer0) - { - ptStream->_tCurrentDraw.uVertexBuffer0 = tDraw.uVertexBuffer0; - uDirtyMask |= PL_DRAW_STREAM_BIT_VERTEX_BUFFER_0; - } - - if(ptStream->_tCurrentDraw.uTriangleCount != tDraw.uTriangleCount) - { - ptStream->_tCurrentDraw.uTriangleCount = tDraw.uTriangleCount; - uDirtyMask |= PL_DRAW_STREAM_BIT_TRIANGLES; - } - - if(ptStream->_tCurrentDraw.uInstanceStart != tDraw.uInstanceStart) - { - ptStream->_tCurrentDraw.uInstanceStart = tDraw.uInstanceStart; - uDirtyMask |= PL_DRAW_STREAM_BIT_INSTANCE_START; - } - - if(ptStream->_tCurrentDraw.uInstanceCount != tDraw.uInstanceCount) - { - ptStream->_tCurrentDraw.uInstanceCount = tDraw.uInstanceCount; - uDirtyMask |= PL_DRAW_STREAM_BIT_INSTANCE_COUNT; - } - - pl_sb_push(ptStream->_sbtStream, uDirtyMask); - if(uDirtyMask & PL_DRAW_STREAM_BIT_SHADER) - pl_sb_push(ptStream->_sbtStream, ptStream->_tCurrentDraw.uShaderVariant); - if(uDirtyMask & PL_DRAW_STREAM_BIT_DYNAMIC_OFFSET_0) - pl_sb_push(ptStream->_sbtStream, ptStream->_tCurrentDraw.uDynamicBufferOffset0); - if(uDirtyMask & PL_DRAW_STREAM_BIT_BINDGROUP_0) - pl_sb_push(ptStream->_sbtStream, ptStream->_tCurrentDraw.uBindGroup0); - if(uDirtyMask & PL_DRAW_STREAM_BIT_BINDGROUP_1) - pl_sb_push(ptStream->_sbtStream, ptStream->_tCurrentDraw.uBindGroup1); - if(uDirtyMask & PL_DRAW_STREAM_BIT_BINDGROUP_2) - pl_sb_push(ptStream->_sbtStream, ptStream->_tCurrentDraw.uBindGroup2); - if(uDirtyMask & PL_DRAW_STREAM_BIT_DYNAMIC_BUFFER_0) - pl_sb_push(ptStream->_sbtStream, ptStream->_tCurrentDraw.uDynamicBuffer0); - if(uDirtyMask & PL_DRAW_STREAM_BIT_INDEX_OFFSET) - pl_sb_push(ptStream->_sbtStream, ptStream->_tCurrentDraw.uIndexOffset); - if(uDirtyMask & PL_DRAW_STREAM_BIT_VERTEX_OFFSET) - pl_sb_push(ptStream->_sbtStream, ptStream->_tCurrentDraw.uVertexOffset); - if(uDirtyMask & PL_DRAW_STREAM_BIT_INDEX_BUFFER) - pl_sb_push(ptStream->_sbtStream, ptStream->_tCurrentDraw.uIndexBuffer); - if(uDirtyMask & PL_DRAW_STREAM_BIT_VERTEX_BUFFER_0) - pl_sb_push(ptStream->_sbtStream, ptStream->_tCurrentDraw.uVertexBuffer0); - if(uDirtyMask & PL_DRAW_STREAM_BIT_TRIANGLES) - pl_sb_push(ptStream->_sbtStream, ptStream->_tCurrentDraw.uTriangleCount); - if(uDirtyMask & PL_DRAW_STREAM_BIT_INSTANCE_START) - pl_sb_push(ptStream->_sbtStream, ptStream->_tCurrentDraw.uInstanceStart); - if(uDirtyMask & PL_DRAW_STREAM_BIT_INSTANCE_COUNT) - pl_sb_push(ptStream->_sbtStream, ptStream->_tCurrentDraw.uInstanceCount); } static uint32_t @@ -815,9 +714,8 @@ pl_load_graphics_api(void) .signal_semaphore = pl_signal_semaphore, .wait_semaphore = pl_wait_semaphore, .get_semaphore_value = pl_get_semaphore_value, - .reset_draw_stream = pl_drawstream_reset, - .add_to_stream = pl_drawstream_draw, - .cleanup_draw_stream = pl_drawstream_cleanup, + .reset_draw_stream = pl_draw_stream_reset, + .cleanup_draw_stream = pl_draw_stream_cleanup, .create_semaphore = pl_create_semaphore, .create_buffer = pl_create_buffer, .create_shader = pl_create_shader, @@ -829,7 +727,7 @@ pl_load_graphics_api(void) .create_sampler = pl_create_sampler, .create_bind_group = pl_create_bind_group, .update_bind_group = pl_update_bind_group, - .allocate_dynamic_data = pl_allocate_dynamic_data, + .allocate_dynamic_data_block = pl_allocate_dynamic_data_block, .queue_buffer_for_deletion = pl_queue_buffer_for_deletion, .queue_texture_for_deletion = pl_queue_texture_for_deletion, .queue_bind_group_for_deletion = pl_queue_bind_group_for_deletion, diff --git a/extensions/pl_graphics_ext.h b/extensions/pl_graphics_ext.h index d3a6fd9..4185ac6 100644 --- a/extensions/pl_graphics_ext.h +++ b/extensions/pl_graphics_ext.h @@ -10,9 +10,11 @@ Index of this file: // [SECTION] apis // [SECTION] includes // [SECTION] forward declarations & basic types -// [SECTION] public api structs +// [SECTION] public api struct // [SECTION] structs // [SECTION] enums +// [SECTION] internal enums +// [SECTION] inline API implementations */ //----------------------------------------------------------------------------- @@ -107,6 +109,7 @@ typedef struct _plBindGroupPoolDesc plBindGroupPoolDesc; // de typedef struct _plBufferBinding plBufferBinding; // buffer binding for bind groups typedef struct _plSamplerBinding plSamplerBinding; // sampler binding for bind groups typedef struct _plDynamicBinding plDynamicBinding; // dynamic binding for bind groups +typedef struct _plDynamicDataBlock plDynamicDataBlock; // dynamic data block typedef struct _plTextureBinding plTextureBinding; // texture binding for bind groups typedef struct _plBindGroupUpdateBufferData plBindGroupUpdateBufferData; // buffer update data for bind groups typedef struct _plBindGroupUpdateSamplerData plBindGroupUpdateSamplerData; // sampler update data for bind groups @@ -198,7 +201,7 @@ typedef int plCommandPoolResetFlags; // -> enum _plCommandPoolResetFlags // Fla typedef struct _plWindow plWindow; // pl_os.h //----------------------------------------------------------------------------- -// [SECTION] public api structs +// [SECTION] public api struct //----------------------------------------------------------------------------- typedef struct _plGraphicsI @@ -262,10 +265,14 @@ typedef struct _plGraphicsI uint32_t (*get_render_encoder_subpass)(plRenderEncoder*); // render encoder: draw stream (preferred system) - void (*reset_draw_stream) (plDrawStream*); - void (*cleanup_draw_stream)(plDrawStream*); - void (*add_to_stream) (plDrawStream*, plDrawStreamData); - void (*draw_stream) (plRenderEncoder*, uint32_t areaCount, plDrawArea*); + // Notes: + // - call reset_draw_stream(...) with the maximum number of possible calls to + // pl_add_to_draw_stream before the next call to draw_stream so any memory + // allocations can happen before a hot loop where pl_add_to_draw_stream is called. + void (*reset_draw_stream) (plDrawStream*, uint32_t drawCount); + void (*cleanup_draw_stream) (plDrawStream*); + void (*draw_stream) (plRenderEncoder*, uint32_t areaCount, plDrawArea*); // decodes drawstream (does not reset draw stream) + // INLINED -> void pl_add_to_draw_stream(plDrawStream*, plDrawStreamData); // render encoder: direct (prefer draw stream system, this will be used for bindless mostly) void (*bind_graphics_bind_groups)(plRenderEncoder*, plShaderHandle, uint32_t first, uint32_t count, const plBindGroupHandle*, uint32_t dynamicCount, const plDynamicBinding*); @@ -350,10 +357,13 @@ typedef struct _plGraphicsI void (*queue_compute_shader_for_deletion)(plDevice*, plComputeShaderHandle); void (*destroy_compute_shader) (plDevice*, plComputeShaderHandle); + // dynamic data system + plDynamicDataBlock (*allocate_dynamic_data_block)(plDevice*); // do not store + // INLINED -> plDynamicBinding pl_allocate_dynamic_data(const plGraphicsI*, plDevice*, plDynamicDataBlock*) + // memory - plDynamicBinding (*allocate_dynamic_data)(plDevice*, size_t); - plDeviceMemoryAllocation(*allocate_memory) (plDevice*, size_t, plMemoryMode, uint32_t typeFilter, const char* debugName); - void (*free_memory) (plDevice*, plDeviceMemoryAllocation*); + plDeviceMemoryAllocation (*allocate_memory)(plDevice*, size_t, plMemoryMode, uint32_t typeFilter, const char* debugName); + void (*free_memory) (plDevice*, plDeviceMemoryAllocation*); } plGraphicsI; @@ -500,6 +510,17 @@ typedef struct _plDynamicBinding char* pcData; } plDynamicBinding; +typedef struct _plDynamicDataBlock +{ + // [INTERNAL] + uint32_t _uBufferHandle; + uint32_t _uByteSize; + uint32_t _uAlignment; + uint32_t _uBumpAmount; + char* _pcData; + uint32_t _uCurrentOffset; +} plDynamicDataBlock; + typedef struct _plBindGroupUpdateSamplerData { plSamplerHandle tSampler; @@ -863,7 +884,9 @@ typedef struct _plDrawStream { // [INTERNAL] plDrawStreamData _tCurrentDraw; - uint32_t* _sbtStream; + uint32_t _uStreamCount; + uint32_t _uStreamCapacity; + uint32_t* _auStream; } plDrawStream; typedef struct _plDraw @@ -1227,4 +1250,161 @@ enum _plDataType PL_DATA_TYPE_UNSIGNED_LONG4, }; +//----------------------------------------------------------------------------- +// [SECTION] internal enums (NOT FOR PUBLIC CONSUMPTION) +//----------------------------------------------------------------------------- + +enum plDrawStreamBits +{ + PL_DRAW_STREAM_BIT_NONE = 0, + PL_DRAW_STREAM_BIT_SHADER = 1 << 0, + PL_DRAW_STREAM_BIT_DYNAMIC_OFFSET_0 = 1 << 1, + PL_DRAW_STREAM_BIT_DYNAMIC_BUFFER_0 = 1 << 2, + PL_DRAW_STREAM_BIT_BINDGROUP_2 = 1 << 3, + PL_DRAW_STREAM_BIT_BINDGROUP_1 = 1 << 4, + PL_DRAW_STREAM_BIT_BINDGROUP_0 = 1 << 5, + PL_DRAW_STREAM_BIT_INDEX_OFFSET = 1 << 6, + PL_DRAW_STREAM_BIT_VERTEX_OFFSET = 1 << 7, + PL_DRAW_STREAM_BIT_INDEX_BUFFER = 1 << 8, + PL_DRAW_STREAM_BIT_VERTEX_BUFFER_0 = 1 << 9, + PL_DRAW_STREAM_BIT_TRIANGLES = 1 << 10, + PL_DRAW_STREAM_BIT_INSTANCE_START = 1 << 11, + PL_DRAW_STREAM_BIT_INSTANCE_COUNT = 1 << 12 +}; + +//----------------------------------------------------------------------------- +// [SECTION] inline API implementations +//----------------------------------------------------------------------------- + +static inline plDynamicBinding +pl_allocate_dynamic_data(const plGraphicsI* ptGfx, plDevice* ptDevice, plDynamicDataBlock* ptCurrentBlock) +{ + if(ptCurrentBlock->_uCurrentOffset + ptCurrentBlock->_uBumpAmount > ptCurrentBlock->_uByteSize) + { + *ptCurrentBlock = ptGfx->allocate_dynamic_data_block(ptDevice); + } + + const plDynamicBinding tBinding = { + .uBufferHandle = ptCurrentBlock->_uBufferHandle, + .uByteOffset = ptCurrentBlock->_uCurrentOffset, + .pcData = &ptCurrentBlock->_pcData[ptCurrentBlock->_uCurrentOffset] + }; + + ptCurrentBlock->_uCurrentOffset = (((size_t)ptCurrentBlock->_uCurrentOffset + ptCurrentBlock->_uBumpAmount + (ptCurrentBlock->_uAlignment - 1)) & ~(ptCurrentBlock->_uAlignment - 1)); + return tBinding; +} + +static inline void +pl_add_to_draw_stream(plDrawStream* ptStream, plDrawStreamData tDraw) +{ + + uint32_t uDirtyMask = PL_DRAW_STREAM_BIT_NONE; + + if(ptStream->_tCurrentDraw.uShaderVariant != tDraw.uShaderVariant) + { + ptStream->_tCurrentDraw.uShaderVariant = tDraw.uShaderVariant; + uDirtyMask |= PL_DRAW_STREAM_BIT_SHADER; + } + + if(ptStream->_tCurrentDraw.uDynamicBufferOffset0 != tDraw.uDynamicBufferOffset0) + { + ptStream->_tCurrentDraw.uDynamicBufferOffset0 = tDraw.uDynamicBufferOffset0; + uDirtyMask |= PL_DRAW_STREAM_BIT_DYNAMIC_OFFSET_0; + } + + if(ptStream->_tCurrentDraw.uBindGroup0 != tDraw.uBindGroup0) + { + ptStream->_tCurrentDraw.uBindGroup0 = tDraw.uBindGroup0; + uDirtyMask |= PL_DRAW_STREAM_BIT_BINDGROUP_0; + } + + if(ptStream->_tCurrentDraw.uBindGroup1 != tDraw.uBindGroup1) + { + ptStream->_tCurrentDraw.uBindGroup1 = tDraw.uBindGroup1; + uDirtyMask |= PL_DRAW_STREAM_BIT_BINDGROUP_1; + } + + if(ptStream->_tCurrentDraw.uBindGroup2 != tDraw.uBindGroup2) + { + ptStream->_tCurrentDraw.uBindGroup2 = tDraw.uBindGroup2; + uDirtyMask |= PL_DRAW_STREAM_BIT_BINDGROUP_2; + } + + if(ptStream->_tCurrentDraw.uDynamicBuffer0 != tDraw.uDynamicBuffer0) + { + ptStream->_tCurrentDraw.uDynamicBuffer0 = tDraw.uDynamicBuffer0; + uDirtyMask |= PL_DRAW_STREAM_BIT_DYNAMIC_BUFFER_0; + } + + if(ptStream->_tCurrentDraw.uIndexOffset != tDraw.uIndexOffset) + { + ptStream->_tCurrentDraw.uIndexOffset = tDraw.uIndexOffset; + uDirtyMask |= PL_DRAW_STREAM_BIT_INDEX_OFFSET; + } + + if(ptStream->_tCurrentDraw.uVertexOffset != tDraw.uVertexOffset) + { + ptStream->_tCurrentDraw.uVertexOffset = tDraw.uVertexOffset; + uDirtyMask |= PL_DRAW_STREAM_BIT_VERTEX_OFFSET; + } + + if(ptStream->_tCurrentDraw.uIndexBuffer != tDraw.uIndexBuffer) + { + ptStream->_tCurrentDraw.uIndexBuffer = tDraw.uIndexBuffer; + uDirtyMask |= PL_DRAW_STREAM_BIT_INDEX_BUFFER; + } + + if(ptStream->_tCurrentDraw.uVertexBuffer0 != tDraw.uVertexBuffer0) + { + ptStream->_tCurrentDraw.uVertexBuffer0 = tDraw.uVertexBuffer0; + uDirtyMask |= PL_DRAW_STREAM_BIT_VERTEX_BUFFER_0; + } + + if(ptStream->_tCurrentDraw.uTriangleCount != tDraw.uTriangleCount) + { + ptStream->_tCurrentDraw.uTriangleCount = tDraw.uTriangleCount; + uDirtyMask |= PL_DRAW_STREAM_BIT_TRIANGLES; + } + + if(ptStream->_tCurrentDraw.uInstanceStart != tDraw.uInstanceStart) + { + ptStream->_tCurrentDraw.uInstanceStart = tDraw.uInstanceStart; + uDirtyMask |= PL_DRAW_STREAM_BIT_INSTANCE_START; + } + + if(ptStream->_tCurrentDraw.uInstanceCount != tDraw.uInstanceCount) + { + ptStream->_tCurrentDraw.uInstanceCount = tDraw.uInstanceCount; + uDirtyMask |= PL_DRAW_STREAM_BIT_INSTANCE_COUNT; + } + + ptStream->_auStream[ptStream->_uStreamCount++] = uDirtyMask; + if(uDirtyMask & PL_DRAW_STREAM_BIT_SHADER) + ptStream->_auStream[ptStream->_uStreamCount++] = ptStream->_tCurrentDraw.uShaderVariant; + if(uDirtyMask & PL_DRAW_STREAM_BIT_DYNAMIC_OFFSET_0) + ptStream->_auStream[ptStream->_uStreamCount++] = ptStream->_tCurrentDraw.uDynamicBufferOffset0; + if(uDirtyMask & PL_DRAW_STREAM_BIT_BINDGROUP_0) + ptStream->_auStream[ptStream->_uStreamCount++] = ptStream->_tCurrentDraw.uBindGroup0; + if(uDirtyMask & PL_DRAW_STREAM_BIT_BINDGROUP_1) + ptStream->_auStream[ptStream->_uStreamCount++] = ptStream->_tCurrentDraw.uBindGroup1; + if(uDirtyMask & PL_DRAW_STREAM_BIT_BINDGROUP_2) + ptStream->_auStream[ptStream->_uStreamCount++] = ptStream->_tCurrentDraw.uBindGroup2; + if(uDirtyMask & PL_DRAW_STREAM_BIT_DYNAMIC_BUFFER_0) + ptStream->_auStream[ptStream->_uStreamCount++] = ptStream->_tCurrentDraw.uDynamicBuffer0; + if(uDirtyMask & PL_DRAW_STREAM_BIT_INDEX_OFFSET) + ptStream->_auStream[ptStream->_uStreamCount++] = ptStream->_tCurrentDraw.uIndexOffset; + if(uDirtyMask & PL_DRAW_STREAM_BIT_VERTEX_OFFSET) + ptStream->_auStream[ptStream->_uStreamCount++] = ptStream->_tCurrentDraw.uVertexOffset; + if(uDirtyMask & PL_DRAW_STREAM_BIT_INDEX_BUFFER) + ptStream->_auStream[ptStream->_uStreamCount++] = ptStream->_tCurrentDraw.uIndexBuffer; + if(uDirtyMask & PL_DRAW_STREAM_BIT_VERTEX_BUFFER_0) + ptStream->_auStream[ptStream->_uStreamCount++] = ptStream->_tCurrentDraw.uVertexBuffer0; + if(uDirtyMask & PL_DRAW_STREAM_BIT_TRIANGLES) + ptStream->_auStream[ptStream->_uStreamCount++] = ptStream->_tCurrentDraw.uTriangleCount; + if(uDirtyMask & PL_DRAW_STREAM_BIT_INSTANCE_START) + ptStream->_auStream[ptStream->_uStreamCount++] = ptStream->_tCurrentDraw.uInstanceStart; + if(uDirtyMask & PL_DRAW_STREAM_BIT_INSTANCE_COUNT) + ptStream->_auStream[ptStream->_uStreamCount++] = ptStream->_tCurrentDraw.uInstanceCount; +} + #endif // PL_GRAPHICS_EXT_H \ No newline at end of file diff --git a/extensions/pl_graphics_internal.h b/extensions/pl_graphics_internal.h index 1df2dae..5f2ecb4 100644 --- a/extensions/pl_graphics_internal.h +++ b/extensions/pl_graphics_internal.h @@ -95,26 +95,4 @@ static void pl__cleanup_common_swapchain(plSwapchain*); // misc. static void pl__garbage_collect(plDevice*); -//----------------------------------------------------------------------------- -// [SECTION] enums -//----------------------------------------------------------------------------- - -enum plDrawStreamBits -{ - PL_DRAW_STREAM_BIT_NONE = 0, - PL_DRAW_STREAM_BIT_SHADER = 1 << 0, - PL_DRAW_STREAM_BIT_DYNAMIC_OFFSET_0 = 1 << 1, - PL_DRAW_STREAM_BIT_DYNAMIC_BUFFER_0 = 1 << 2, - PL_DRAW_STREAM_BIT_BINDGROUP_2 = 1 << 3, - PL_DRAW_STREAM_BIT_BINDGROUP_1 = 1 << 4, - PL_DRAW_STREAM_BIT_BINDGROUP_0 = 1 << 5, - PL_DRAW_STREAM_BIT_INDEX_OFFSET = 1 << 6, - PL_DRAW_STREAM_BIT_VERTEX_OFFSET = 1 << 7, - PL_DRAW_STREAM_BIT_INDEX_BUFFER = 1 << 8, - PL_DRAW_STREAM_BIT_VERTEX_BUFFER_0 = 1 << 9, - PL_DRAW_STREAM_BIT_TRIANGLES = 1 << 10, - PL_DRAW_STREAM_BIT_INSTANCE_START = 1 << 11, - PL_DRAW_STREAM_BIT_INSTANCE_COUNT = 1 << 12 -}; - #endif // PL_GRAPHICS_INTERNAL_EXT_H \ No newline at end of file diff --git a/extensions/pl_graphics_metal.m b/extensions/pl_graphics_metal.m index 40c2eeb..ad7aa5a 100644 --- a/extensions/pl_graphics_metal.m +++ b/extensions/pl_graphics_metal.m @@ -67,7 +67,6 @@ typedef struct _plMetalDynamicBuffer { - uint32_t uByteOffset; uint32_t uHandle; plDeviceMemoryAllocation tMemory; id tBuffer; @@ -1091,55 +1090,44 @@ return tHandle; } -static plDynamicBinding -pl_allocate_dynamic_data(plDevice* ptDevice, size_t szSize) +static plDynamicDataBlock +pl_allocate_dynamic_data_block(plDevice* ptDevice) { plFrameContext* ptFrame = pl__get_frame_resources(ptDevice); - PL_ASSERT(szSize <= ptDevice->tInit.szDynamicDataMaxSize && "Dynamic data size too large"); plMetalDynamicBuffer* ptDynamicBuffer = NULL; // first call this frame - if(ptFrame->uCurrentBufferIndex == UINT32_MAX) + if(ptFrame->uCurrentBufferIndex != 0) { - ptFrame->uCurrentBufferIndex = 0; - ptDynamicBuffer = &ptFrame->sbtDynamicBuffers[0]; - ptDynamicBuffer->uByteOffset = 0; - } - ptDynamicBuffer = &ptFrame->sbtDynamicBuffers[ptFrame->uCurrentBufferIndex]; - - // check if current block has room - if(ptDynamicBuffer->uByteOffset + szSize > ptDevice->tInit.szDynamicBufferBlockSize) - { - ptFrame->uCurrentBufferIndex++; - - // check if we have available block - if(ptFrame->uCurrentBufferIndex + 1 > pl_sb_size(ptFrame->sbtDynamicBuffers)) // create new buffer + if(pl_sb_size(ptFrame->sbtDynamicBuffers) <= ptFrame->uCurrentBufferIndex) { - // dynamic buffer stuff pl_sb_add(ptFrame->sbtDynamicBuffers); ptDynamicBuffer = &ptFrame->sbtDynamicBuffers[ptFrame->uCurrentBufferIndex]; - ptDynamicBuffer->uByteOffset = 0; static char atNameBuffer[64] = {0}; pl_sprintf(atNameBuffer, "D-BUF-F%d-%d", (int)gptGraphics->uCurrentFrameIndex, (int)ptFrame->uCurrentBufferIndex); ptDynamicBuffer->tMemory = ptDevice->ptDynamicAllocator->allocate(ptDevice->ptDynamicAllocator->ptInst, 0, ptDevice->tInit.szDynamicBufferBlockSize, 0, atNameBuffer); ptDynamicBuffer->tBuffer = [(id)ptDynamicBuffer->tMemory.uHandle newBufferWithLength:ptDevice->tInit.szDynamicBufferBlockSize options:MTLResourceStorageModeShared offset:0]; ptDynamicBuffer->tBuffer.label = [NSString stringWithUTF8String:"buddy allocator"]; + gptGraphics->szHostMemoryInUse += ptDevice->tInit.szDynamicBufferBlockSize; } - - ptDynamicBuffer = &ptFrame->sbtDynamicBuffers[ptFrame->uCurrentBufferIndex]; - ptDynamicBuffer->uByteOffset = 0; } + + if(ptDynamicBuffer == NULL) + ptDynamicBuffer = &ptFrame->sbtDynamicBuffers[ptFrame->uCurrentBufferIndex]; - plDynamicBinding tDynamicBinding = { - .uBufferHandle = ptFrame->uCurrentBufferIndex, - .uByteOffset = ptDynamicBuffer->uByteOffset, - .pcData = &ptDynamicBuffer->tBuffer.contents[ptDynamicBuffer->uByteOffset] + plDynamicDataBlock tBlock = { + ._uBufferHandle = ptFrame->uCurrentBufferIndex, + ._uByteSize = ptDevice->tInit.szDynamicBufferBlockSize, + ._pcData = ptDynamicBuffer->tBuffer.contents, + ._uAlignment = 256, + ._uBumpAmount = ptDevice->tInit.szDynamicDataMaxSize, + ._uCurrentOffset = 0 }; - ptDynamicBuffer->uByteOffset = pl_align_up((size_t)ptDynamicBuffer->uByteOffset + ptDevice->tInit.szDynamicDataMaxSize, 256); - return tDynamicBinding; + ptFrame->uCurrentBufferIndex++; + return tBlock; } static plComputeShaderHandle @@ -1811,7 +1799,7 @@ } } - ptFrame->uCurrentBufferIndex = UINT32_MAX; + ptFrame->uCurrentBufferIndex = 0; __block dispatch_semaphore_t semaphore = ptFrame->tFrameBoundarySemaphore; [ptCommandBuffer->tCmdBuffer addCompletedHandler:^(id commandBuffer) { @@ -2200,7 +2188,7 @@ }; [ptEncoder->tEncoder setViewport:tViewport]; - const uint32_t uTokens = pl_sb_size(ptStream->_sbtStream); + const uint32_t uTokens = ptStream->_uStreamCount; uint32_t uCurrentStreamIndex = 0; uint32_t uTriangleCount = 0; uint32_t uIndexBuffer = 0; @@ -2214,13 +2202,13 @@ uint32_t uDynamicSlot = UINT32_MAX; while(uCurrentStreamIndex < uTokens) { - const uint32_t uDirtyMask = ptStream->_sbtStream[uCurrentStreamIndex]; + const uint32_t uDirtyMask = ptStream->_auStream[uCurrentStreamIndex]; uCurrentStreamIndex++; if(uDirtyMask & PL_DRAW_STREAM_BIT_SHADER) { - const plShader* ptShader= &ptDevice->sbtShadersCold[ptStream->_sbtStream[uCurrentStreamIndex]]; - plMetalShader* ptMetalShader = &ptDevice->sbtShadersHot[ptStream->_sbtStream[uCurrentStreamIndex]]; + const plShader* ptShader= &ptDevice->sbtShadersCold[ptStream->_auStream[uCurrentStreamIndex]]; + plMetalShader* ptMetalShader = &ptDevice->sbtShadersHot[ptStream->_auStream[uCurrentStreamIndex]]; [ptEncoder->tEncoder setCullMode:ptMetalShader->tCullMode]; [ptEncoder->tEncoder setFrontFacingWinding:MTLWindingCounterClockwise]; if(tCurrentDepthStencilState != ptMetalShader->tDepthStencilState) @@ -2238,13 +2226,13 @@ if(uDirtyMask & PL_DRAW_STREAM_BIT_DYNAMIC_OFFSET_0) { - uDynamicBufferOffset0 = ptStream->_sbtStream[uCurrentStreamIndex]; + uDynamicBufferOffset0 = ptStream->_auStream[uCurrentStreamIndex]; uCurrentStreamIndex++; } if(uDirtyMask & PL_DRAW_STREAM_BIT_BINDGROUP_0) { - plMetalBindGroup* ptMetalBindGroup = &ptDevice->sbtBindGroupsHot[ptStream->_sbtStream[uCurrentStreamIndex]]; + plMetalBindGroup* ptMetalBindGroup = &ptDevice->sbtBindGroupsHot[ptStream->_auStream[uCurrentStreamIndex]]; for(uint32 j = 0; j < ptMetalBindGroup->uHeapCount; j++) { @@ -2265,7 +2253,7 @@ if(uDirtyMask & PL_DRAW_STREAM_BIT_BINDGROUP_1) { - plMetalBindGroup* ptMetalBindGroup = &ptDevice->sbtBindGroupsHot[ptStream->_sbtStream[uCurrentStreamIndex]]; + plMetalBindGroup* ptMetalBindGroup = &ptDevice->sbtBindGroupsHot[ptStream->_auStream[uCurrentStreamIndex]]; for(uint32 j = 0; j < ptMetalBindGroup->uHeapCount; j++) { @@ -2286,7 +2274,7 @@ if(uDirtyMask & PL_DRAW_STREAM_BIT_BINDGROUP_2) { - plMetalBindGroup* ptMetalBindGroup = &ptDevice->sbtBindGroupsHot[ptStream->_sbtStream[uCurrentStreamIndex]]; + plMetalBindGroup* ptMetalBindGroup = &ptDevice->sbtBindGroupsHot[ptStream->_auStream[uCurrentStreamIndex]]; for(uint32 j = 0; j < ptMetalBindGroup->uHeapCount; j++) { @@ -2307,8 +2295,8 @@ if(uDirtyMask & PL_DRAW_STREAM_BIT_DYNAMIC_BUFFER_0) { - [ptEncoder->tEncoder setVertexBuffer:ptFrame->sbtDynamicBuffers[ptStream->_sbtStream[uCurrentStreamIndex]].tBuffer offset:0 atIndex:uDynamicSlot]; - [ptEncoder->tEncoder setFragmentBuffer:ptFrame->sbtDynamicBuffers[ptStream->_sbtStream[uCurrentStreamIndex]].tBuffer offset:0 atIndex:uDynamicSlot]; + [ptEncoder->tEncoder setVertexBuffer:ptFrame->sbtDynamicBuffers[ptStream->_auStream[uCurrentStreamIndex]].tBuffer offset:0 atIndex:uDynamicSlot]; + [ptEncoder->tEncoder setFragmentBuffer:ptFrame->sbtDynamicBuffers[ptStream->_auStream[uCurrentStreamIndex]].tBuffer offset:0 atIndex:uDynamicSlot]; uCurrentStreamIndex++; } @@ -2320,41 +2308,41 @@ if(uDirtyMask & PL_DRAW_STREAM_BIT_INDEX_OFFSET) { - uIndexBufferOffset = ptStream->_sbtStream[uCurrentStreamIndex]; + uIndexBufferOffset = ptStream->_auStream[uCurrentStreamIndex]; uCurrentStreamIndex++; } if(uDirtyMask & PL_DRAW_STREAM_BIT_VERTEX_OFFSET) { - uVertexBufferOffset = ptStream->_sbtStream[uCurrentStreamIndex]; + uVertexBufferOffset = ptStream->_auStream[uCurrentStreamIndex]; uCurrentStreamIndex++; } if(uDirtyMask & PL_DRAW_STREAM_BIT_INDEX_BUFFER) { - uIndexBuffer = ptStream->_sbtStream[uCurrentStreamIndex]; + uIndexBuffer = ptStream->_auStream[uCurrentStreamIndex]; uCurrentStreamIndex++; } if(uDirtyMask & PL_DRAW_STREAM_BIT_VERTEX_BUFFER_0) { - [ptEncoder->tEncoder setVertexBuffer:ptDevice->sbtBuffersHot[ptStream->_sbtStream[uCurrentStreamIndex]].tBuffer + [ptEncoder->tEncoder setVertexBuffer:ptDevice->sbtBuffersHot[ptStream->_auStream[uCurrentStreamIndex]].tBuffer offset:0 atIndex:4]; uCurrentStreamIndex++; } if(uDirtyMask & PL_DRAW_STREAM_BIT_TRIANGLES) { - uTriangleCount = ptStream->_sbtStream[uCurrentStreamIndex]; + uTriangleCount = ptStream->_auStream[uCurrentStreamIndex]; uCurrentStreamIndex++; } if(uDirtyMask & PL_DRAW_STREAM_BIT_INSTANCE_START) { - uInstanceStart = ptStream->_sbtStream[uCurrentStreamIndex]; + uInstanceStart = ptStream->_auStream[uCurrentStreamIndex]; uCurrentStreamIndex++; } if(uDirtyMask & PL_DRAW_STREAM_BIT_INSTANCE_COUNT) { - uInstanceCount = ptStream->_sbtStream[uCurrentStreamIndex]; + uInstanceCount = ptStream->_auStream[uCurrentStreamIndex]; uCurrentStreamIndex++; } diff --git a/extensions/pl_graphics_vulkan.c b/extensions/pl_graphics_vulkan.c index 04a70f6..5f83357 100644 --- a/extensions/pl_graphics_vulkan.c +++ b/extensions/pl_graphics_vulkan.c @@ -78,7 +78,6 @@ typedef struct _plRenderPassCommonData typedef struct _plVulkanDynamicBuffer { - uint32_t uByteOffset; uint32_t uHandle; VkBuffer tBuffer; plDeviceMemoryAllocation tMemory; @@ -492,32 +491,18 @@ pl_bind_buffer_to_memory(plDevice* ptDevice, plBufferHandle tHandle, const plDev ptVulkanBuffer->pcData = ptAllocation->pHostMapped; } -plDynamicBinding -pl_allocate_dynamic_data(plDevice* ptDevice, size_t szSize) +static plDynamicDataBlock +pl_allocate_dynamic_data_block(plDevice* ptDevice) { plFrameContext* ptFrame = pl__get_frame_resources(ptDevice); - PL_ASSERT(szSize <= ptDevice->tInit.szDynamicDataMaxSize && "Dynamic data size too large"); - plVulkanDynamicBuffer* ptDynamicBuffer = NULL; // first call this frame - if (ptFrame->uCurrentBufferIndex == UINT32_MAX) - { - ptFrame->uCurrentBufferIndex = 0; - ptFrame->sbtDynamicBuffers[0].uByteOffset = 0; - } - ptDynamicBuffer = &ptFrame->sbtDynamicBuffers[ptFrame->uCurrentBufferIndex]; - - // check if current block has room - if (ptDynamicBuffer->uByteOffset + szSize > ptDevice->tInit.szDynamicBufferBlockSize) + if(ptFrame->uCurrentBufferIndex != 0) { - ptFrame->uCurrentBufferIndex++; - - // check if we have available block - if (ptFrame->uCurrentBufferIndex + 1 > pl_sb_size(ptFrame->sbtDynamicBuffers)) // create new buffer + if(pl_sb_size(ptFrame->sbtDynamicBuffers) <= ptFrame->uCurrentBufferIndex) { - // dynamic buffer stuff pl_sb_add(ptFrame->sbtDynamicBuffers); ptDynamicBuffer = &ptFrame->sbtDynamicBuffers[ptFrame->uCurrentBufferIndex]; @@ -540,7 +525,6 @@ pl_allocate_dynamic_data(plDevice* ptDevice, size_t szSize) ptDynamicBuffer->uHandle = tStagingBuffer0.uIndex; ptDynamicBuffer->tBuffer = ptDevice->sbtBuffersHot[tStagingBuffer0.uIndex].tBuffer; ptDynamicBuffer->tMemory = ptStagingBuffer->tMemoryAllocation; - ptDynamicBuffer->uByteOffset = 0; // allocate descriptor set const VkDescriptorSetAllocateInfo tDynamicAllocInfo = { @@ -570,23 +554,23 @@ pl_allocate_dynamic_data(plDevice* ptDevice, size_t szSize) }; vkUpdateDescriptorSets(ptDevice->tLogicalDevice, 1, &tWrite0, 0, NULL); } - - ptDynamicBuffer = &ptFrame->sbtDynamicBuffers[ptFrame->uCurrentBufferIndex]; - ptDynamicBuffer->uByteOffset = 0; } + + if(ptDynamicBuffer == NULL) + ptDynamicBuffer = &ptFrame->sbtDynamicBuffers[ptFrame->uCurrentBufferIndex]; plVulkanBuffer* ptBuffer = &ptDevice->sbtBuffersHot[ptDynamicBuffer->uHandle]; - plDynamicBinding tDynamicBinding = { - .uBufferHandle = ptFrame->uCurrentBufferIndex, - .uByteOffset = ptDynamicBuffer->uByteOffset, - .pcData = &ptBuffer->pcData[ptDynamicBuffer->uByteOffset] + plDynamicDataBlock tBlock = { + .uBufferHandle = ptFrame->uCurrentBufferIndex, + .uByteSize = ptDevice->tInit.szDynamicBufferBlockSize, + .pcData = ptBuffer->pcData, + .uAlignment = ptDevice->tInfo.tLimits.uMinUniformBufferOffsetAlignment, + .uBumpAmount = ptDevice->tInit.szDynamicDataMaxSize, + .uCurrentOffset = 0 }; - ptDynamicBuffer->uByteOffset = (uint32_t)pl_align_up( - (size_t)ptDynamicBuffer->uByteOffset + ptDevice->tInit.szDynamicDataMaxSize, - ptDevice->tInfo.tLimits.uMinUniformBufferOffsetAlignment); - - return tDynamicBinding; + ptFrame->uCurrentBufferIndex++; + return tBlock; } void @@ -2024,7 +2008,7 @@ pl_draw_stream(plRenderEncoder* ptEncoder, uint32_t uAreaCount, plDrawArea *atAr plDrawStream* ptStream = ptArea->ptDrawStream; - const uint32_t uTokens = pl_sb_size(ptStream->_sbtStream); + const uint32_t uTokens = ptStream->_uStreamCount; uint32_t uCurrentStreamIndex = 0; uint32_t uTriangleCount = 0; uint32_t uIndexBuffer = 0; @@ -2041,13 +2025,13 @@ pl_draw_stream(plRenderEncoder* ptEncoder, uint32_t uAreaCount, plDrawArea *atAr while (uCurrentStreamIndex < uTokens) { - const uint32_t uDirtyMask = ptStream->_sbtStream[uCurrentStreamIndex]; + const uint32_t uDirtyMask = ptStream->_auStream[uCurrentStreamIndex]; uCurrentStreamIndex++; if (uDirtyMask & PL_DRAW_STREAM_BIT_SHADER) { - const plShader* ptShader = &ptDevice->sbtShadersCold[ptStream->_sbtStream[uCurrentStreamIndex]]; - ptVulkanShader = &ptDevice->sbtShadersHot[ptStream->_sbtStream[uCurrentStreamIndex]]; + const plShader* ptShader = &ptDevice->sbtShadersCold[ptStream->_auStream[uCurrentStreamIndex]]; + ptVulkanShader = &ptDevice->sbtShadersHot[ptStream->_auStream[uCurrentStreamIndex]]; vkCmdBindPipeline(ptCmdBuffer->tCmdBuffer, VK_PIPELINE_BIND_POINT_GRAPHICS, ptVulkanShader->tPipeline); pl__set_bind_group_count(&tBindGroupManagerData, ptShader->tDesc._uBindGroupLayoutCount); uCurrentStreamIndex++; @@ -2055,52 +2039,52 @@ pl_draw_stream(plRenderEncoder* ptEncoder, uint32_t uAreaCount, plDrawArea *atAr if (uDirtyMask & PL_DRAW_STREAM_BIT_DYNAMIC_OFFSET_0) { - uDynamicBufferOffset0 = ptStream->_sbtStream[uCurrentStreamIndex]; + uDynamicBufferOffset0 = ptStream->_auStream[uCurrentStreamIndex]; tBindGroupManagerData.auOffsets[0] = uDynamicBufferOffset0; uCurrentStreamIndex++; } if (uDirtyMask & PL_DRAW_STREAM_BIT_BINDGROUP_0) { - plVulkanBindGroup* ptBindGroup0 = &ptDevice->sbtBindGroupsHot[ptStream->_sbtStream[uCurrentStreamIndex]]; + plVulkanBindGroup* ptBindGroup0 = &ptDevice->sbtBindGroupsHot[ptStream->_auStream[uCurrentStreamIndex]]; pl__set_bind_group(&tBindGroupManagerData, 0, ptBindGroup0->tDescriptorSet); uCurrentStreamIndex++; } if (uDirtyMask & PL_DRAW_STREAM_BIT_BINDGROUP_1) { - plVulkanBindGroup* ptBindGroup1 = &ptDevice->sbtBindGroupsHot[ptStream->_sbtStream[uCurrentStreamIndex]]; + plVulkanBindGroup* ptBindGroup1 = &ptDevice->sbtBindGroupsHot[ptStream->_auStream[uCurrentStreamIndex]]; pl__set_bind_group(&tBindGroupManagerData, 1, ptBindGroup1->tDescriptorSet); uCurrentStreamIndex++; } if (uDirtyMask & PL_DRAW_STREAM_BIT_BINDGROUP_2) { - plVulkanBindGroup* ptBindGroup2 = &ptDevice->sbtBindGroupsHot[ptStream->_sbtStream[uCurrentStreamIndex]]; + plVulkanBindGroup* ptBindGroup2 = &ptDevice->sbtBindGroupsHot[ptStream->_auStream[uCurrentStreamIndex]]; pl__set_bind_group(&tBindGroupManagerData, 2, ptBindGroup2->tDescriptorSet); uCurrentStreamIndex++; } if (uDirtyMask & PL_DRAW_STREAM_BIT_DYNAMIC_BUFFER_0) { - ptVulkanDynamicBuffer = &ptCurrentFrame->sbtDynamicBuffers[ptStream->_sbtStream[uCurrentStreamIndex]]; + ptVulkanDynamicBuffer = &ptCurrentFrame->sbtDynamicBuffers[ptStream->_auStream[uCurrentStreamIndex]]; pl__set_dynamic_bind_group(&tBindGroupManagerData, ptVulkanDynamicBuffer->tDescriptorSet, uDynamicBufferOffset0); uCurrentStreamIndex++; } if (uDirtyMask & PL_DRAW_STREAM_BIT_INDEX_OFFSET) { - uIndexBufferOffset = ptStream->_sbtStream[uCurrentStreamIndex]; + uIndexBufferOffset = ptStream->_auStream[uCurrentStreamIndex]; uCurrentStreamIndex++; } if (uDirtyMask & PL_DRAW_STREAM_BIT_VERTEX_OFFSET) { - uVertexBufferOffset = ptStream->_sbtStream[uCurrentStreamIndex]; + uVertexBufferOffset = ptStream->_auStream[uCurrentStreamIndex]; uCurrentStreamIndex++; } if (uDirtyMask & PL_DRAW_STREAM_BIT_INDEX_BUFFER) { - uIndexBuffer = ptStream->_sbtStream[uCurrentStreamIndex]; + uIndexBuffer = ptStream->_auStream[uCurrentStreamIndex]; if (uIndexBuffer != UINT32_MAX) { plVulkanBuffer* ptIndexBuffer = &ptDevice->sbtBuffersHot[uIndexBuffer]; @@ -2110,25 +2094,25 @@ pl_draw_stream(plRenderEncoder* ptEncoder, uint32_t uAreaCount, plDrawArea *atAr } if (uDirtyMask & PL_DRAW_STREAM_BIT_VERTEX_BUFFER_0) { - plVulkanBuffer* ptVertexBuffer = &ptDevice->sbtBuffersHot[ptStream->_sbtStream[uCurrentStreamIndex]]; + plVulkanBuffer* ptVertexBuffer = &ptDevice->sbtBuffersHot[ptStream->_auStream[uCurrentStreamIndex]]; vkCmdBindVertexBuffers(ptCmdBuffer->tCmdBuffer, 0, 1, &ptVertexBuffer->tBuffer, &offsets); uCurrentStreamIndex++; } if (uDirtyMask & PL_DRAW_STREAM_BIT_TRIANGLES) { - uTriangleCount = ptStream->_sbtStream[uCurrentStreamIndex]; + uTriangleCount = ptStream->_auStream[uCurrentStreamIndex]; uCurrentStreamIndex++; } if (uDirtyMask & PL_DRAW_STREAM_BIT_INSTANCE_START) { - uInstanceStart = ptStream->_sbtStream[uCurrentStreamIndex]; + uInstanceStart = ptStream->_auStream[uCurrentStreamIndex]; uCurrentStreamIndex++; } if (uDirtyMask & PL_DRAW_STREAM_BIT_INSTANCE_COUNT) { - uInstanceCount = ptStream->_sbtStream[uCurrentStreamIndex]; + uInstanceCount = ptStream->_auStream[uCurrentStreamIndex]; uCurrentStreamIndex++; } @@ -2815,11 +2799,10 @@ pl_create_device(const plDeviceInit* ptInit) plDeviceMemoryAllocation tAllocation = ptDynamicAllocator->allocate(ptDynamicAllocator->ptInst, ptBuffer->tMemoryRequirements.uMemoryTypeBits, ptBuffer->tMemoryRequirements.ulSize, ptBuffer->tMemoryRequirements.ulAlignment, "dynamic buffer"); pl_bind_buffer_to_memory(ptDevice, tStagingBuffer0, &tAllocation); - tFrame.uCurrentBufferIndex = UINT32_MAX; + tFrame.uCurrentBufferIndex = 0; tFrame.sbtDynamicBuffers[0].uHandle = tStagingBuffer0.uIndex; tFrame.sbtDynamicBuffers[0].tBuffer = ptDevice->sbtBuffersHot[tStagingBuffer0.uIndex].tBuffer; tFrame.sbtDynamicBuffers[0].tMemory = tAllocation; - tFrame.sbtDynamicBuffers[0].uByteOffset = 0; // allocate descriptor sets const VkDescriptorSetAllocateInfo tDynamicAllocInfo = { @@ -2939,7 +2922,7 @@ pl_begin_frame(plDevice* ptDevice) pl_begin_profile_sample(0, __FUNCTION__); plFrameContext* ptCurrentFrame = pl__get_frame_resources(ptDevice); - ptCurrentFrame->uCurrentBufferIndex = UINT32_MAX; + ptCurrentFrame->uCurrentBufferIndex = 0; PL_VULKAN(vkWaitForFences(ptDevice->tLogicalDevice, 1, &ptCurrentFrame->tInFlight, VK_TRUE, UINT64_MAX)); pl__garbage_collect(ptDevice); diff --git a/extensions/pl_renderer_ext.c b/extensions/pl_renderer_ext.c index 060e630..99d37c5 100644 --- a/extensions/pl_renderer_ext.c +++ b/extensions/pl_renderer_ext.c @@ -282,6 +282,7 @@ typedef struct _plRefScene typedef struct _plRefRendererData { plDevice* ptDevice; + plDeviceInfo tDeviceInfo; plSwapchain* ptSwap; plSurface* ptSurface; plTempAllocator tTempAllocator; @@ -360,6 +361,9 @@ typedef struct _plRefRendererData // command pools plCommandPool* atCmdPools[PL_MAX_FRAMES_IN_FLIGHT]; + // dynamic buffer system + plDynamicDataBlock tCurrentDynamicDataBlock; + // graphics options bool bReloadSwapchain; bool bVSync; @@ -410,6 +414,14 @@ static plRefRendererData* gptData = NULL; // [SECTION] internal API //----------------------------------------------------------------------------- +// dynamic data system + +static inline plDynamicBinding +pl__allocate_dynamic_data(plDevice* ptDevice) +{ + return pl_allocate_dynamic_data(gptGfx, gptData->ptDevice, &gptData->tCurrentDynamicDataBlock); +} + // general helpers static void pl__add_drawable_skin_data_to_global_buffer(plRefScene*, uint32_t uDrawableIndex, plDrawable* atDrawables); static void pl__add_drawable_data_to_global_buffer(plRefScene*, uint32_t uDrawableIndex, plDrawable* atDrawables); @@ -464,7 +476,7 @@ pl_refr_initialize(plWindow* ptWindow) gptData->tPickedEntity.ulData = UINT64_MAX; // shader default values - gptData->tSkyboxShader = (plShaderHandle){UINT32_MAX, UINT32_MAX}; + gptData->tSkyboxShader = (plShaderHandle){UINT32_MAX, UINT32_MAX}; // initialize graphics plGraphicsInit tGraphicsDesc = { @@ -541,6 +553,8 @@ pl_refr_initialize(plWindow* ptWindow) gptData->aptBindGroupPools[i] = gptGfx->create_bind_group_pool(gptData->ptDevice, &tPoolDesc); } + gptData->tCurrentDynamicDataBlock = gptGfx->allocate_dynamic_data_block(gptData->ptDevice); + // load gpu allocators gptData->ptLocalBuddyAllocator = gptGpuAllocators->get_local_buddy_allocator(gptData->ptDevice); gptData->ptLocalDedicatedAllocator = gptGpuAllocators->get_local_dedicated_allocator(gptData->ptDevice); @@ -3306,7 +3320,7 @@ pl_refr_perform_skinning(plCommandBuffer* ptCommandBuffer, uint32_t uSceneHandle for(uint32_t i = 0; i < uSkinCount; i++) { - plDynamicBinding tDynamicBinding = gptGfx->allocate_dynamic_data(ptDevice, sizeof(SkinDynamicData)); + plDynamicBinding tDynamicBinding = pl__allocate_dynamic_data(ptDevice); SkinDynamicData* ptDynamicData = (SkinDynamicData*)tDynamicBinding.pcData; ptDynamicData->iSourceDataOffset = ptScene->sbtSkinData[i].iSourceDataOffset; ptDynamicData->iDestDataOffset = ptScene->sbtSkinData[i].iDestDataOffset; @@ -3572,8 +3586,6 @@ pl_refr_generate_cascaded_shadow_map(plCommandBuffer* ptCommandBuffer, uint32_t gptGfx->update_bind_group(gptData->ptDevice, tOpaqueBG1, &tBGData1); gptGfx->queue_bind_group_for_deletion(ptDevice, tOpaqueBG1); - gptGfx->reset_draw_stream(ptStream); - const uint32_t uOpaqueDrawableCount = pl_sb_size(ptScene->sbtOpaqueDrawables); const uint32_t uTransparentDrawableCount = pl_sb_size(ptScene->sbtTransparentDrawables); @@ -3607,6 +3619,8 @@ pl_refr_generate_cascaded_shadow_map(plCommandBuffer* ptCommandBuffer, uint32_t const uint32_t uVisibleOpaqueDrawCount = pl_sb_size(ptScene->sbtOpaqueDrawables); const uint32_t uVisibleTransparentDrawCount = pl_sb_size(ptScene->sbtTransparentDrawables); + gptGfx->reset_draw_stream(ptStream, uVisibleOpaqueDrawCount + uVisibleTransparentDrawCount); + plRenderEncoder* ptEncoder = gptGfx->begin_render_pass(ptCommandBuffer, ptView->tShadowData.atOpaqueRenderPasses[uCascade]); for(uint32_t i = 0; i < uVisibleOpaqueDrawCount; i++) @@ -3615,7 +3629,7 @@ pl_refr_generate_cascaded_shadow_map(plCommandBuffer* ptCommandBuffer, uint32_t plObjectComponent* ptObject = gptECS->get_component(&ptScene->tComponentLibrary, PL_COMPONENT_TYPE_OBJECT, tDrawable.tEntity); plTransformComponent* ptTransform = gptECS->get_component(&ptScene->tComponentLibrary, PL_COMPONENT_TYPE_TRANSFORM, ptObject->tTransform); - plDynamicBinding tDynamicBinding = gptGfx->allocate_dynamic_data(ptDevice, sizeof(plShadowDynamicData)); + plDynamicBinding tDynamicBinding = pl__allocate_dynamic_data(ptDevice); plShadowDynamicData* ptDynamicData = (plShadowDynamicData*)tDynamicBinding.pcData; ptDynamicData->iDataOffset = tDrawable.uDataOffset; @@ -3624,7 +3638,7 @@ pl_refr_generate_cascaded_shadow_map(plCommandBuffer* ptCommandBuffer, uint32_t ptDynamicData->iMaterialIndex = tDrawable.uMaterialIndex; ptDynamicData->iIndex = (int)uCascade; - gptGfx->add_to_stream(ptStream, (plDrawStreamData) + pl_add_to_draw_stream(ptStream, (plDrawStreamData) { .uShaderVariant = gptData->tShadowShader.uIndex, .uDynamicBuffer0 = tDynamicBinding.uBufferHandle, @@ -3647,7 +3661,7 @@ pl_refr_generate_cascaded_shadow_map(plCommandBuffer* ptCommandBuffer, uint32_t plObjectComponent* ptObject = gptECS->get_component(&ptScene->tComponentLibrary, PL_COMPONENT_TYPE_OBJECT, tDrawable.tEntity); plTransformComponent* ptTransform = gptECS->get_component(&ptScene->tComponentLibrary, PL_COMPONENT_TYPE_TRANSFORM, ptObject->tTransform); - plDynamicBinding tDynamicBinding = gptGfx->allocate_dynamic_data(ptDevice, sizeof(plShadowDynamicData)); + plDynamicBinding tDynamicBinding = pl__allocate_dynamic_data(ptDevice); plShadowDynamicData* ptDynamicData = (plShadowDynamicData*)tDynamicBinding.pcData; ptDynamicData->iDataOffset = tDrawable.uDataOffset; @@ -3656,7 +3670,7 @@ pl_refr_generate_cascaded_shadow_map(plCommandBuffer* ptCommandBuffer, uint32_t ptDynamicData->iMaterialIndex = tDrawable.uMaterialIndex; ptDynamicData->iIndex = (int)uCascade; - gptGfx->add_to_stream(ptStream, (plDrawStreamData) + pl_add_to_draw_stream(ptStream, (plDrawStreamData) { .uShaderVariant = tDrawable.tShadowShader.uIndex, .uDynamicBuffer0 = tDynamicBinding.uBufferHandle, @@ -3674,7 +3688,6 @@ pl_refr_generate_cascaded_shadow_map(plCommandBuffer* ptCommandBuffer, uint32_t } gptGfx->draw_stream(ptEncoder, 1, &tArea); - gptGfx->reset_draw_stream(ptStream); gptGfx->end_render_pass(ptEncoder); } @@ -3765,7 +3778,7 @@ pl_refr_post_process_scene(plCommandBuffer* ptCommandBuffer, uint32_t uSceneHand } plPostProcessOptions; - plDynamicBinding tDynamicBinding = gptGfx->allocate_dynamic_data(ptDevice, sizeof(plPostProcessOptions)); + plDynamicBinding tDynamicBinding = pl__allocate_dynamic_data(ptDevice); plPostProcessOptions* ptDynamicData = (plPostProcessOptions*)tDynamicBinding.pcData; const plVec4 tOutlineColor = (plVec4){(float)sin(gptIOI->get_io()->dTime * 3.0) * 0.25f + 0.75f, 0.0f, 0.0f, 1.0f}; @@ -4032,8 +4045,6 @@ pl_refr_render_scene(uint32_t uSceneHandle, uint32_t uViewHandle, plViewOptions gptGfx->return_command_buffer(ptCommandBuffer); } - gptGfx->reset_draw_stream(ptStream); - const plVec2 tDimensions = gptGfx->get_render_pass(ptDevice, ptView->tRenderPass)->tDesc.tDimensions; plDrawArea tArea = { @@ -4077,13 +4088,14 @@ pl_refr_render_scene(uint32_t uSceneHandle, uint32_t uViewHandle, plViewOptions } const uint32_t uVisibleOpaqueDrawCount = pl_sb_size(ptView->sbtVisibleOpaqueDrawables); + gptGfx->reset_draw_stream(ptStream, uVisibleOpaqueDrawCount); for(uint32_t i = 0; i < uVisibleOpaqueDrawCount; i++) { const plDrawable tDrawable = ptView->sbtVisibleOpaqueDrawables[i]; plObjectComponent* ptObject = gptECS->get_component(&ptScene->tComponentLibrary, PL_COMPONENT_TYPE_OBJECT, tDrawable.tEntity); plTransformComponent* ptTransform = gptECS->get_component(&ptScene->tComponentLibrary, PL_COMPONENT_TYPE_TRANSFORM, ptObject->tTransform); - plDynamicBinding tDynamicBinding = gptGfx->allocate_dynamic_data(ptDevice, sizeof(DynamicData)); + plDynamicBinding tDynamicBinding = pl__allocate_dynamic_data(ptDevice); DynamicData* ptDynamicData = (DynamicData*)tDynamicBinding.pcData; ptDynamicData->iDataOffset = tDrawable.uDataOffset; @@ -4091,7 +4103,7 @@ pl_refr_render_scene(uint32_t uSceneHandle, uint32_t uViewHandle, plViewOptions ptDynamicData->tModel = ptTransform->tWorld; ptDynamicData->iMaterialOffset = tDrawable.uMaterialIndex; - gptGfx->add_to_stream(ptStream, (plDrawStreamData) + pl_add_to_draw_stream(ptStream, (plDrawStreamData) { .uShaderVariant = tDrawable.tShader.uIndex, .uDynamicBuffer0 = tDynamicBinding.uBufferHandle, @@ -4109,8 +4121,7 @@ pl_refr_render_scene(uint32_t uSceneHandle, uint32_t uViewHandle, plViewOptions } gptGfx->draw_stream(ptEncoder, 1, &tArea); - gptGfx->reset_draw_stream(ptStream); - + //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~subpass 1 - lighting~~~~~~~~~~~~~~~~~~~~~~~~~~~~ gptGfx->next_subpass(ptEncoder); @@ -4197,9 +4208,10 @@ pl_refr_render_scene(uint32_t uSceneHandle, uint32_t uViewHandle, plViewOptions int iVertexOffset; int unused[2]; } plLightingDynamicData; - plDynamicBinding tLightingDynamicData = gptGfx->allocate_dynamic_data(ptDevice, sizeof(plLightingDynamicData)); - - gptGfx->add_to_stream(ptStream, (plDrawStreamData) + plDynamicBinding tLightingDynamicData = pl__allocate_dynamic_data(ptDevice); + + gptGfx->reset_draw_stream(ptStream, 1); + pl_add_to_draw_stream(ptStream, (plDrawStreamData) { .uShaderVariant = ptScene->tLightingShader.uIndex, .uDynamicBuffer0 = tLightingDynamicData.uBufferHandle, @@ -4215,8 +4227,7 @@ pl_refr_render_scene(uint32_t uSceneHandle, uint32_t uViewHandle, plViewOptions .uInstanceCount = 1 }); gptGfx->draw_stream(ptEncoder, 1, &tArea); - gptGfx->reset_draw_stream(ptStream); - + //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~subpass 2 - forward~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ gptGfx->next_subpass(ptEncoder); @@ -4224,11 +4235,12 @@ pl_refr_render_scene(uint32_t uSceneHandle, uint32_t uViewHandle, plViewOptions if(ptScene->tSkyboxTexture.uIndex != UINT32_MAX) { - plDynamicBinding tSkyboxDynamicData = gptGfx->allocate_dynamic_data(ptDevice, sizeof(plMat4)); + plDynamicBinding tSkyboxDynamicData = pl__allocate_dynamic_data(ptDevice); plMat4* ptSkyboxDynamicData = (plMat4*)tSkyboxDynamicData.pcData; *ptSkyboxDynamicData = pl_mat4_translate_vec3(ptCamera->tPos); - gptGfx->add_to_stream(ptStream, (plDrawStreamData) + gptGfx->reset_draw_stream(ptStream, 1); + pl_add_to_draw_stream(ptStream, (plDrawStreamData) { .uShaderVariant = gptData->tSkyboxShader.uIndex, .uDynamicBuffer0 = tSkyboxDynamicData.uBufferHandle, @@ -4245,7 +4257,7 @@ pl_refr_render_scene(uint32_t uSceneHandle, uint32_t uViewHandle, plViewOptions }); gptGfx->draw_stream(ptEncoder, 1, &tArea); } - gptGfx->reset_draw_stream(ptStream); + // transparent & complex material objects gptJob->wait_for_counter(ptTransparentCounter); @@ -4261,13 +4273,14 @@ pl_refr_render_scene(uint32_t uSceneHandle, uint32_t uViewHandle, plViewOptions } const uint32_t uVisibleTransparentDrawCount = pl_sb_size(ptView->sbtVisibleTransparentDrawables); + gptGfx->reset_draw_stream(ptStream, uVisibleTransparentDrawCount); for(uint32_t i = 0; i < uVisibleTransparentDrawCount; i++) { const plDrawable tDrawable = ptView->sbtVisibleTransparentDrawables[i]; plObjectComponent* ptObject = gptECS->get_component(&ptScene->tComponentLibrary, PL_COMPONENT_TYPE_OBJECT, tDrawable.tEntity); plTransformComponent* ptTransform = gptECS->get_component(&ptScene->tComponentLibrary, PL_COMPONENT_TYPE_TRANSFORM, ptObject->tTransform); - plDynamicBinding tDynamicBinding = gptGfx->allocate_dynamic_data(ptDevice, sizeof(DynamicData)); + plDynamicBinding tDynamicBinding = pl__allocate_dynamic_data(ptDevice); DynamicData* ptDynamicData = (DynamicData*)tDynamicBinding.pcData; ptDynamicData->iDataOffset = tDrawable.uDataOffset; @@ -4275,7 +4288,7 @@ pl_refr_render_scene(uint32_t uSceneHandle, uint32_t uViewHandle, plViewOptions ptDynamicData->tModel = ptTransform->tWorld; ptDynamicData->iMaterialOffset = tDrawable.uMaterialIndex; - gptGfx->add_to_stream(ptStream, (plDrawStreamData) + pl_add_to_draw_stream(ptStream, (plDrawStreamData) { .uShaderVariant = tDrawable.tShader.uIndex, .uDynamicBuffer0 = tDynamicBinding.uBufferHandle, @@ -4446,7 +4459,7 @@ pl_refr_render_scene(uint32_t uSceneHandle, uint32_t uViewHandle, plViewOptions plObjectComponent* ptObject = gptECS->get_component(&ptScene->tComponentLibrary, PL_COMPONENT_TYPE_OBJECT, tDrawable.tEntity); plTransformComponent* ptTransform = gptECS->get_component(&ptScene->tComponentLibrary, PL_COMPONENT_TYPE_TRANSFORM, ptObject->tTransform); - plDynamicBinding tDynamicBinding = gptGfx->allocate_dynamic_data(ptDevice, sizeof(plPickDynamicData)); + plDynamicBinding tDynamicBinding = pl__allocate_dynamic_data(ptDevice); plPickDynamicData* ptDynamicData = (plPickDynamicData*)tDynamicBinding.pcData; ptDynamicData->tColor = (plVec4){ @@ -4474,7 +4487,7 @@ pl_refr_render_scene(uint32_t uSceneHandle, uint32_t uViewHandle, plViewOptions plObjectComponent* ptObject = gptECS->get_component(&ptScene->tComponentLibrary, PL_COMPONENT_TYPE_OBJECT, tDrawable.tEntity); plTransformComponent* ptTransform = gptECS->get_component(&ptScene->tComponentLibrary, PL_COMPONENT_TYPE_TRANSFORM, ptObject->tTransform); - plDynamicBinding tDynamicBinding = gptGfx->allocate_dynamic_data(ptDevice, sizeof(plPickDynamicData)); + plDynamicBinding tDynamicBinding = pl__allocate_dynamic_data(ptDevice); plPickDynamicData* ptDynamicData = (plPickDynamicData*)tDynamicBinding.pcData; const uint32_t uId = tDrawable.tEntity.uIndex; ptDynamicData->tColor = (plVec4){ @@ -4676,7 +4689,7 @@ pl_refr_render_scene(uint32_t uSceneHandle, uint32_t uViewHandle, plViewOptions // submit nonindexed draw using basic API gptGfx->bind_compute_shader(ptComputeEncoder, gptData->tJFAShader); - plDynamicBinding tDynamicBinding = gptGfx->allocate_dynamic_data(gptData->ptDevice, sizeof(plVec4)); + plDynamicBinding tDynamicBinding = pl__allocate_dynamic_data(ptDevice); plVec4* ptJumpDistance = (plVec4*)tDynamicBinding.pcData; ptJumpDistance->x = fJumpDistance; @@ -4782,6 +4795,7 @@ pl_refr_begin_frame(void) gptGfx->begin_frame(gptData->ptDevice); gptGfx->reset_command_pool(gptData->atCmdPools[gptGfx->get_current_frame_index()], 0); gptGfx->reset_bind_group_pool(gptData->aptBindGroupPools[gptGfx->get_current_frame_index()]); + gptData->tCurrentDynamicDataBlock = gptGfx->allocate_dynamic_data_block(gptData->ptDevice); if(!gptGfx->acquire_swapchain_image(gptData->ptSwap)) {