Skip to content

Commit

Permalink
WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
jonathan hoffstadt committed Feb 25, 2025
1 parent d6f5f09 commit a897e9f
Show file tree
Hide file tree
Showing 5 changed files with 66 additions and 25 deletions.
41 changes: 35 additions & 6 deletions extensions/pl_draw_backend_ext.c
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ Index of this file:
#include "pl_draw_backend_ext.h"
#include "pl_draw_ext.h"
#include "pl_stats_ext.h"
#include "pl_log_ext.h"

#ifdef PL_UNITY_BUILD
#include "pl_unity_ext.inc"
Expand All @@ -48,6 +49,9 @@ Index of this file:
static const plStatsI* gptStats = NULL;
static const plDrawI* gptDraw = NULL;
static const plShaderI* gptShader = NULL;
static const plLogI* gptLog = NULL;
static const plIOI* gptIOI = NULL;
static plIO* gptIO = NULL;
#endif

#include "pl_ds.h"
Expand Down Expand Up @@ -101,6 +105,7 @@ typedef struct _plDrawBackendContext
//-----------------------------------------------------------------------------

static plDrawBackendContext* gptDrawBackendCtx = NULL;
static uint64_t uLogChannelDrawBackend = UINT64_MAX;

//-----------------------------------------------------------------------------
// [SECTION] internal api
Expand Down Expand Up @@ -673,8 +678,10 @@ pl_submit_2d_drawlist(plDrawList2D* ptDrawlist, plRenderEncoder* ptEncoder, floa

const plBufferDesc tBufferDesc = {
.tUsage = PL_BUFFER_USAGE_VERTEX | PL_BUFFER_USAGE_STAGING,
.szByteSize = pl_max(ptBufferInfo->uVertexBufferSize * 2, uVtxBufSzNeeded + uAvailableVertexBufferSpace)
.szByteSize = pl_max(ptBufferInfo->uVertexBufferSize * 2, uVtxBufSzNeeded + uAvailableVertexBufferSpace),
.pcDebugName = "2D Draw Vertex Buffer"
};
pl_log_debug_f(gptLog, uLogChannelDrawBackend, "Grow \"%s\" from %u to %u on frame %llu", tBufferDesc.pcDebugName, ptBufferInfo->uVertexBufferSize, (uint32_t)tBufferDesc.szByteSize, gptIO->ulFrameCount);
ptBufferInfo->uVertexBufferSize = (uint32_t)tBufferDesc.szByteSize;
ptBufferInfo->uVertexBufferOffset = 0;

Expand All @@ -701,8 +708,10 @@ pl_submit_2d_drawlist(plDrawList2D* ptDrawlist, plRenderEncoder* ptEncoder, floa

const plBufferDesc tBufferDesc = {
.tUsage = PL_BUFFER_USAGE_INDEX | PL_BUFFER_USAGE_STAGING,
.szByteSize = pl_max(gptDrawBackendCtx->auIndexBufferSize[uFrameIdx] * 2, uIdxBufSzNeeded + uAvailableIndexBufferSpace)
.szByteSize = pl_max(gptDrawBackendCtx->auIndexBufferSize[uFrameIdx] * 2, uIdxBufSzNeeded + uAvailableIndexBufferSpace),
.pcDebugName = "Draw Index Buffer"
};
pl_log_debug_f(gptLog, uLogChannelDrawBackend, "(2D) Grow \"%s\" from %u to %u on frame %llu", tBufferDesc.pcDebugName, gptDrawBackendCtx->auIndexBufferSize[uFrameIdx], (uint32_t)tBufferDesc.szByteSize, gptIO->ulFrameCount);
gptDrawBackendCtx->auIndexBufferSize[uFrameIdx] = (uint32_t)tBufferDesc.szByteSize;

gptDrawBackendCtx->atIndexBuffer[uFrameIdx] = pl__create_staging_buffer(&tBufferDesc, "draw idx buffer", uFrameIdx);
Expand Down Expand Up @@ -859,8 +868,10 @@ pl_submit_3d_drawlist(plDrawList3D* ptDrawlist, plRenderEncoder* ptEncoder, floa

const plBufferDesc tBufferDesc = {
.tUsage = PL_BUFFER_USAGE_VERTEX | PL_BUFFER_USAGE_STAGING,
.szByteSize = pl_max(ptBufferInfo->uVertexBufferSize * 2, uVtxBufSzNeeded + uAvailableVertexBufferSpace)
.szByteSize = pl_max(ptBufferInfo->uVertexBufferSize * 2, uVtxBufSzNeeded + uAvailableVertexBufferSpace),
.pcDebugName = "3D Draw Vertex Buffer"
};
pl_log_debug_f(gptLog, uLogChannelDrawBackend, "Grow \"%s\" from %u to %u on frame %llu", tBufferDesc.pcDebugName, ptBufferInfo->uVertexBufferSize, (uint32_t)tBufferDesc.szByteSize, gptIO->ulFrameCount);
ptBufferInfo->uVertexBufferSize = (uint32_t)tBufferDesc.szByteSize;

ptBufferInfo->tVertexBuffer = pl__create_staging_buffer(&tBufferDesc, "3d draw vtx buffer", uFrameIdx);
Expand All @@ -885,8 +896,10 @@ pl_submit_3d_drawlist(plDrawList3D* ptDrawlist, plRenderEncoder* ptEncoder, floa

const plBufferDesc tBufferDesc = {
.tUsage = PL_BUFFER_USAGE_INDEX | PL_BUFFER_USAGE_STAGING,
.szByteSize = pl_max(gptDrawBackendCtx->auIndexBufferSize[uFrameIdx] * 2, uIdxBufSzNeeded + uAvailableIndexBufferSpace)
.szByteSize = pl_max(gptDrawBackendCtx->auIndexBufferSize[uFrameIdx] * 2, uIdxBufSzNeeded + uAvailableIndexBufferSpace),
.pcDebugName = "Draw Index Buffer"
};
pl_log_debug_f(gptLog, uLogChannelDrawBackend, "(3D) Grow \"%s\" from %u to %u on frame %llu", tBufferDesc.pcDebugName, gptDrawBackendCtx->auIndexBufferSize[uFrameIdx], (uint32_t)tBufferDesc.szByteSize, gptIO->ulFrameCount);
gptDrawBackendCtx->auIndexBufferSize[uFrameIdx] = (uint32_t)tBufferDesc.szByteSize;

gptDrawBackendCtx->atIndexBuffer[uFrameIdx] = pl__create_staging_buffer(&tBufferDesc, "3d draw idx buffer", uFrameIdx);
Expand Down Expand Up @@ -945,8 +958,10 @@ pl_submit_3d_drawlist(plDrawList3D* ptDrawlist, plRenderEncoder* ptEncoder, floa

const plBufferDesc tBufferDesc = {
.tUsage = PL_BUFFER_USAGE_VERTEX | PL_BUFFER_USAGE_STAGING,
.szByteSize = pl_max(ptBufferInfo->uVertexBufferSize * 2, uVtxBufSzNeeded + uAvailableVertexBufferSpace)
.szByteSize = pl_max(ptBufferInfo->uVertexBufferSize * 2, uVtxBufSzNeeded + uAvailableVertexBufferSpace),
.pcDebugName = "3D Lines Vertex Buffer"
};
pl_log_debug_f(gptLog, uLogChannelDrawBackend, "Grow \"%s\" from %u to %u on frame %llu", tBufferDesc.pcDebugName, ptBufferInfo->uVertexBufferSize, (uint32_t)tBufferDesc.szByteSize, gptIO->ulFrameCount);
ptBufferInfo->uVertexBufferSize = (uint32_t)tBufferDesc.szByteSize;

ptBufferInfo->tVertexBuffer = pl__create_staging_buffer(&tBufferDesc, "draw vtx buffer", uFrameIdx);
Expand All @@ -972,8 +987,10 @@ pl_submit_3d_drawlist(plDrawList3D* ptDrawlist, plRenderEncoder* ptEncoder, floa

const plBufferDesc tBufferDesc = {
.tUsage = PL_BUFFER_USAGE_INDEX | PL_BUFFER_USAGE_STAGING,
.szByteSize = pl_max(gptDrawBackendCtx->auIndexBufferSize[uFrameIdx] * 2, uIdxBufSzNeeded + uAvailableIndexBufferSpace)
.szByteSize = pl_max(gptDrawBackendCtx->auIndexBufferSize[uFrameIdx] * 2, uIdxBufSzNeeded + uAvailableIndexBufferSpace),
.pcDebugName = "Draw Index Buffer"
};
pl_log_debug_f(gptLog, uLogChannelDrawBackend, "(3D) Grow \"%s\" from %u to %u on frame %llu", tBufferDesc.pcDebugName, gptDrawBackendCtx->auIndexBufferSize[uFrameIdx], (uint32_t)tBufferDesc.szByteSize, gptIO->ulFrameCount);
gptDrawBackendCtx->auIndexBufferSize[uFrameIdx] = (uint32_t)tBufferDesc.szByteSize;

gptDrawBackendCtx->atIndexBuffer[uFrameIdx] = pl__create_staging_buffer(&tBufferDesc, "draw idx buffer", uFrameIdx);
Expand Down Expand Up @@ -1070,15 +1087,27 @@ pl_load_draw_backend_ext(plApiRegistryI* ptApiRegistry, bool bReload)
gptGfx = pl_get_api_latest(ptApiRegistry, plGraphicsI);
gptDraw = pl_get_api_latest(ptApiRegistry, plDrawI);
gptShader = pl_get_api_latest(ptApiRegistry, plShaderI);
gptLog = pl_get_api_latest(ptApiRegistry, plLogI);
gptIOI = pl_get_api_latest(ptApiRegistry, plIOI);
gptIO = gptIOI->get_io();
const plDataRegistryI* ptDataRegistry = pl_get_api_latest(ptApiRegistry, plDataRegistryI);

if(bReload)
{
gptDrawBackendCtx = ptDataRegistry->get_data("plDrawBackendContext");
uLogChannelDrawBackend = gptLog->get_channel_id("Draw Backend");
}
else // first load
{
static plDrawBackendContext tCtx = {0};
gptDrawBackendCtx = &tCtx;
ptDataRegistry->set_data("plDrawBackendContext", gptDrawBackendCtx);

plLogExtChannelInit tLogInit = {
.tType = PL_LOG_CHANNEL_TYPE_BUFFER | PL_LOG_CHANNEL_TYPE_CONSOLE,
.uEntryCount = 256
};
uLogChannelDrawBackend = gptLog->add_channel("Draw Backend", tLogInit);
}
}

Expand Down
2 changes: 1 addition & 1 deletion extensions/pl_graphics_ext.c
Original file line number Diff line number Diff line change
Expand Up @@ -185,7 +185,7 @@ pl_queue_buffer_for_deletion(plDevice* ptDevice, plBufferHandle tHandle)
pl_sb_push(ptGarbage->sbtBuffers, tHandle);
pl_sb_push(ptGarbage->sbtMemory, ptDevice->sbtBuffersCold[tHandle.uIndex].tMemoryAllocation);
ptDevice->sbtBuffersCold[tHandle.uIndex]._uGeneration++;
pl_log_trace_f(gptLog, uLogChannelGraphics, "Queue buffer %u for deletion", tHandle.uIndex);
pl_log_debug_f(gptLog, uLogChannelGraphics, "Queue buffer %u for deletion frame %llu", tHandle.uIndex, gptIO->ulFrameCount);
}

static void
Expand Down
32 changes: 20 additions & 12 deletions extensions/pl_graphics_metal.m
Original file line number Diff line number Diff line change
Expand Up @@ -1477,11 +1477,11 @@

// setup logging
plLogExtChannelInit tLogInit = {
.tType = PL_LOG_CHANNEL_TYPE_CYCLIC_BUFFER,
.tType = PL_LOG_CHANNEL_TYPE_BUFFER | PL_LOG_CHANNEL_TYPE_CONSOLE,
.uEntryCount = 1024
};
uLogChannelGraphics = gptLog->add_channel("Graphics", tLogInit);
uint32_t uLogLevel = PL_LOG_LEVEL_INFO;
uint32_t uLogLevel = PL_LOG_LEVEL_DEBUG;
gptLog->set_level(uLogChannelGraphics, uLogLevel);

return true;
Expand Down Expand Up @@ -1605,7 +1605,7 @@
for(uint32_t i = 0; i < gptGraphics->uFramesInFlight; i++)
{
plFrameContext tFrame = {
.tFrameBoundarySemaphore = dispatch_semaphore_create(gptGraphics->uFramesInFlight),
.tFrameBoundarySemaphore = dispatch_semaphore_create(1),
};
pl_sb_resize(tFrame.sbtDynamicBuffers, 1);
static char atNameBuffer[PL_MAX_NAME_LENGTH] = {0};
Expand Down Expand Up @@ -1680,6 +1680,9 @@
static void
pl_recreate_swapchain(plSwapchain* ptSwap, const plSwapchainInit* ptInit)
{

bool bMSAAChange = ptSwap->tInfo.tSampleCount != ptInit->tSampleCount;

gptGraphics->uCurrentFrameIndex = 0;
ptSwap->tInfo.bVSync = ptInit->bVSync;
ptSwap->tInfo.uWidth = ptInit->uWidth;
Expand All @@ -1688,12 +1691,16 @@
if(ptSwap->tInfo.tSampleCount == 0)
ptSwap->tInfo.tSampleCount = 1;

uint32_t uNextFrameIndex = (gptGraphics->uCurrentFrameIndex + 1) % gptGraphics->uFramesInFlight;
plFrameContext* ptFrame1 = &ptSwap->ptDevice->sbtFrames[uNextFrameIndex];
plFrameContext* ptFrame0 = pl__get_frame_resources(ptSwap->ptDevice);
// dispatch_semaphore_wait(ptFrame->tFrameBoundarySemaphore, DISPATCH_TIME_FOREVER);
dispatch_semaphore_signal(ptFrame0->tFrameBoundarySemaphore);
dispatch_semaphore_signal(ptFrame1->tFrameBoundarySemaphore);
if(bMSAAChange)
{
uint32_t uNextFrameIndex = (gptGraphics->uCurrentFrameIndex + 1) % gptGraphics->uFramesInFlight;
plFrameContext* ptFrame1 = &ptSwap->ptDevice->sbtFrames[uNextFrameIndex];
plFrameContext* ptFrame0 = pl__get_frame_resources(ptSwap->ptDevice);
// dispatch_semaphore_wait(ptFrame->tFrameBoundarySemaphore, DISPATCH_TIME_FOREVER);

dispatch_semaphore_signal(ptFrame0->tFrameBoundarySemaphore);
dispatch_semaphore_signal(ptFrame1->tFrameBoundarySemaphore);
}
}

static plCommandPool*
Expand Down Expand Up @@ -1754,9 +1761,9 @@
ptCommandBuffer->tCmdBuffer = [ptPool->tCmdQueue commandBufferWithDescriptor:ptCmdBufferDescriptor];

// [ptCmdBufferDescriptor release];
// char blah[32] = {0};
// pl_sprintf(blah, "%u", gptGraphics->uCurrentFrameIndex);
// tCmdBuffer.label = [NSString stringWithUTF8String:blah];
char blah[32] = {0};
pl_sprintf(blah, "%llu", gptIO->ulFrameCount);
ptCommandBuffer->tCmdBuffer .label = [NSString stringWithUTF8String:blah];

// [ptCmdBufferDescriptor release];
ptCommandBuffer->ptDevice = ptPool->ptDevice;
Expand Down Expand Up @@ -3240,6 +3247,7 @@
[ptDevice->sbtBuffersHot[iBufferIndex].tBuffer release];
ptDevice->sbtBuffersHot[iBufferIndex].tBuffer = nil;
pl_sb_push(ptDevice->sbtBufferFreeIndices, iBufferIndex);
pl_log_debug_f(gptLog, uLogChannelGraphics, "Delete buffer %u for deletion frame %llu", iBufferIndex, gptIO->ulFrameCount);
}

for(uint32_t i = 0; i < pl_sb_size(ptGarbage->sbtMemory); i++)
Expand Down
4 changes: 2 additions & 2 deletions extensions/pl_graphics_vulkan.c
Original file line number Diff line number Diff line change
Expand Up @@ -2392,11 +2392,11 @@ pl_initialize_graphics(const plGraphicsInit* ptDesc)

// setup logging
plLogExtChannelInit tLogInit = {
.tType = PL_LOG_CHANNEL_TYPE_CYCLIC_BUFFER,
.tType = PL_LOG_CHANNEL_TYPE_BUFFER | PL_LOG_CHANNEL_TYPE_CONSOLE,
.uEntryCount = 1024
};
uLogChannelGraphics = gptLog->add_channel("Graphics", tLogInit);
uint32_t uLogLevel = PL_LOG_LEVEL_ALL;
uint32_t uLogLevel = PL_LOG_LEVEL_DEBUG;
gptLog->set_level(uLogChannelGraphics, uLogLevel);

// save context for hot-reloads
Expand Down
12 changes: 8 additions & 4 deletions extensions/pl_renderer_ext.c
Original file line number Diff line number Diff line change
Expand Up @@ -225,15 +225,17 @@ pl_refr_initialize(plWindow* ptWindow)
// create staging buffers
const plBufferDesc tStagingBufferDesc = {
.tUsage = PL_BUFFER_USAGE_STAGING,
.szByteSize = 268435456
.szByteSize = 268435456,
.pcDebugName = "Renderer Staging Buffer"
};
for(uint32_t i = 0; i < gptGfx->get_frames_in_flight(); i++)
gptData->tStagingBufferHandle[i] = pl__refr_create_staging_buffer(&tStagingBufferDesc, "staging", i);

// create caching staging buffer
const plBufferDesc tStagingCachedBufferDesc = {
.tUsage = PL_BUFFER_USAGE_STAGING,
.szByteSize = 268435456
.szByteSize = 268435456,
.pcDebugName = "Renderer Cached Staging Buffer"
};
gptData->tCachedStagingBuffer = pl__refr_create_cached_staging_buffer(&tStagingBufferDesc, "cached staging", 0);

Expand Down Expand Up @@ -536,7 +538,8 @@ pl_refr_initialize(plWindow* ptWindow)
const uint32_t auFullQuadIndexBuffer[] = {0, 1, 2, 0, 2, 3};
const plBufferDesc tFullQuadIndexBufferDesc = {
.tUsage = PL_BUFFER_USAGE_INDEX,
.szByteSize = sizeof(uint32_t) * 6
.szByteSize = sizeof(uint32_t) * 6,
.pcDebugName = "Renderer Quad Index Buffer"
};
gptData->tFullQuadIndexBuffer = pl__refr_create_local_buffer(&tFullQuadIndexBufferDesc, "full quad index buffer", 0, auFullQuadIndexBuffer);

Expand All @@ -548,7 +551,8 @@ pl_refr_initialize(plWindow* ptWindow)
};
const plBufferDesc tFullQuadVertexBufferDesc = {
.tUsage = PL_BUFFER_USAGE_VERTEX,
.szByteSize = sizeof(float) * 16
.szByteSize = sizeof(float) * 16,
.pcDebugName = "Renderer Quad Vertex Buffer"
};
gptData->tFullQuadVertexBuffer = pl__refr_create_local_buffer(&tFullQuadVertexBufferDesc, "full quad vertex buffer", 0, afFullQuadVertexBuffer);

Expand Down

0 comments on commit a897e9f

Please # to comment.