Skip to content

Commit

Permalink
refac: inline some graphics ext. functions
Browse files Browse the repository at this point in the history
  • Loading branch information
hoffstadt committed Nov 3, 2024
1 parent c13c08e commit 3a8ac14
Show file tree
Hide file tree
Showing 12 changed files with 332 additions and 288 deletions.
2 changes: 1 addition & 1 deletion examples/example_2.c
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion examples/example_3.c
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
3 changes: 2 additions & 1 deletion examples/example_6.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
2 changes: 1 addition & 1 deletion examples/example_8.c
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion examples/example_9.c
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
12 changes: 7 additions & 5 deletions extensions/pl_draw_backend_ext.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;

//-----------------------------------------------------------------------------
Expand All @@ -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
//-----------------------------------------------------------------------------
Expand Down Expand Up @@ -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++)
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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;

Expand Down Expand Up @@ -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;
Expand Down
136 changes: 17 additions & 119 deletions extensions/pl_graphics_ext.c
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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,
Expand All @@ -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,
Expand Down
Loading

0 comments on commit 3a8ac14

Please sign in to comment.