Skip to content

Commit 288561c

Browse files
committed
WIP
1 parent a469a55 commit 288561c

File tree

8 files changed

+67
-89
lines changed

8 files changed

+67
-89
lines changed

extensions/pl_graphics_ext.c

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -231,7 +231,9 @@ pl_queue_buffer_for_deletion(plDevice* ptDevice, plBufferHandle tHandle)
231231
plFrameGarbage* ptGarbage = pl__get_frame_garbage(ptDevice);
232232
pl_sb_push(ptGarbage->sbtBuffers, tHandle);
233233
pl_sb_push(ptGarbage->sbtMemory, ptDevice->sbtBuffersCold[tHandle.uIndex].tMemoryAllocation);
234+
ptDevice->sbtBuffersCold[tHandle.uIndex].tMemoryAllocation._uFrameBoundaryValueForDeletion = pl__get_frame_resources(ptDevice)->uNextValue;
234235
ptDevice->sbtBuffersCold[tHandle.uIndex]._uGeneration++;
236+
ptDevice->sbtBuffersCold[tHandle.uIndex]._uFrameBoundaryValueForDeletion = pl__get_frame_resources(ptDevice)->uNextValue;
235237
// pl_log_trace_f(gptLog, uLogChannelGraphics, "Queue buffer %u for deletion frame %llu", tHandle.uIndex, gptIO->ulFrameCount);
236238
pl_log_debug_f(gptLog, uLogChannelGraphics, "queue buffer %s for deletion (%u)", ptDevice->sbtBuffersCold[tHandle.uIndex].tDesc.pcDebugName, tHandle.uIndex);
237239
}
@@ -250,9 +252,11 @@ pl_queue_texture_for_deletion(plDevice* ptDevice, plTextureHandle tHandle)
250252
pl_sb_push(ptGarbage->sbtTextures, tHandle);
251253
if(ptDevice->sbtTexturesHot[tHandle.uIndex].bOriginalView)
252254
{
255+
ptDevice->sbtTexturesCold[tHandle.uIndex].tMemoryAllocation._uFrameBoundaryValueForDeletion = pl__get_frame_resources(ptDevice)->uNextValue;
253256
pl_sb_push(ptGarbage->sbtMemory, ptDevice->sbtTexturesCold[tHandle.uIndex].tMemoryAllocation);
254257
}
255258
ptDevice->sbtTexturesCold[tHandle.uIndex]._uGeneration++;
259+
ptDevice->sbtTexturesCold[tHandle.uIndex]._uFrameBoundaryValueForDeletion = pl__get_frame_resources(ptDevice)->uNextValue;
256260
pl_log_debug_f(gptLog, uLogChannelGraphics, "queue texture %s for deletion (%u)", ptDevice->sbtTexturesCold[tHandle.uIndex].tDesc.pcDebugName, tHandle.uIndex);
257261
}
258262
else

extensions/pl_graphics_ext.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -568,6 +568,9 @@ typedef struct _plDeviceMemoryAllocation
568568
uint64_t ulSize; // actual size of allocation
569569
char* pHostMapped; // host mapped memory if host visible
570570
plDeviceMemoryAllocatorI* ptAllocator; // if allocated from user allocator, this should be set so cleanup can delegate
571+
572+
// [INTERNAL]
573+
uint64_t _uFrameBoundaryValueForDeletion;
571574
} plDeviceMemoryAllocation;
572575

573576
typedef struct _plDeviceMemoryAllocatorI
@@ -636,6 +639,7 @@ typedef struct _plTexture
636639

637640
// [INTERNAL]
638641
uint16_t _uGeneration;
642+
uint64_t _uFrameBoundaryValueForDeletion;
639643
} plTexture;
640644

641645
typedef struct _plBufferDesc
@@ -653,6 +657,7 @@ typedef struct _plBuffer
653657

654658
// [INTERNAL]
655659
uint16_t _uGeneration;
660+
uint64_t _uFrameBoundaryValueForDeletion;
656661
} plBuffer;
657662

658663
//-----------------------------bind groups-------------------------------------

extensions/pl_graphics_metal.m

Lines changed: 46 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,9 @@
116116
{
117117
uint32_t uCurrentBufferIndex;
118118
plMetalDynamicBuffer* sbtDynamicBuffers;
119-
dispatch_semaphore_t tFrameBoundarySemaphore;
119+
id<MTLSharedEvent> tFrameBoundaryEvent;
120+
uint64_t uNextValue;
121+
uint64_t uLastValue;
120122
} plFrameContext;
121123

122124
typedef struct _plMetalTexture
@@ -1767,8 +1769,11 @@
17671769
for(uint32_t i = 0; i < gptGraphics->uFramesInFlight; i++)
17681770
{
17691771
plFrameContext tFrame = {
1770-
.tFrameBoundarySemaphore = dispatch_semaphore_create(1),
1772+
.uNextValue = 0
17711773
};
1774+
1775+
tFrame.tFrameBoundaryEvent = [ptDevice->tDevice newSharedEvent];
1776+
17721777
pl_sb_resize(tFrame.sbtDynamicBuffers, 1);
17731778
static char atNameBuffer[PL_MAX_NAME_LENGTH] = {0};
17741779
pl_sprintf(atNameBuffer, "D-BUF-F%d-0", (int)i);
@@ -1852,17 +1857,6 @@
18521857
ptSwap->tInfo.tSampleCount = pl_min(ptInit->tSampleCount, ptSwap->ptDevice->tInfo.tMaxSampleCount);
18531858
if(ptSwap->tInfo.tSampleCount == 0)
18541859
ptSwap->tInfo.tSampleCount = 1;
1855-
1856-
if(bMSAAChange)
1857-
{
1858-
// uint32_t uNextFrameIndex = (gptGraphics->uCurrentFrameIndex + 1) % gptGraphics->uFramesInFlight;
1859-
// plFrameContext* ptFrame1 = &ptSwap->ptDevice->sbtFrames[uNextFrameIndex];
1860-
plFrameContext* ptFrame0 = pl__get_frame_resources(ptSwap->ptDevice);
1861-
// dispatch_semaphore_wait(ptFrame->tFrameBoundarySemaphore, DISPATCH_TIME_FOREVER);
1862-
1863-
dispatch_semaphore_signal(ptFrame0->tFrameBoundarySemaphore);
1864-
// // dispatch_semaphore_signal(ptFrame1->tFrameBoundarySemaphore);
1865-
}
18661860
}
18671861

18681862
static plCommandPool*
@@ -1940,20 +1934,15 @@
19401934

19411935
// Wait until the inflight command buffer has completed its work
19421936
// gptGraphics->tSwapchain.uCurrentImageIndex = gptGraphics->uCurrentFrameIndex;
1943-
gptGraphics->uCurrentFrameIndex = (gptGraphics->uCurrentFrameIndex + 1) % gptGraphics->uFramesInFlight;
19441937
plFrameContext* ptFrame = pl__get_frame_resources(ptDevice);
1938+
[ptFrame->tFrameBoundaryEvent waitUntilSignaledValue:ptFrame->uNextValue timeoutMS:10000];
19451939

1946-
dispatch_semaphore_wait(ptFrame->tFrameBoundarySemaphore, DISPATCH_TIME_FOREVER);
1947-
1948-
static bool bFirstRun = true;
1949-
if(bFirstRun == false)
1940+
if(ptFrame->uNextValue >= ptFrame->uLastValue + 2)
19501941
{
19511942
pl__garbage_collect(ptDevice);
1943+
ptFrame->uLastValue = ptFrame->uNextValue;
19521944
}
1953-
else
1954-
{
1955-
bFirstRun = false;
1956-
}
1945+
19571946

19581947
pl_end_cpu_sample(gptProfile, 0);
19591948
}
@@ -2036,21 +2025,9 @@
20362025
}
20372026

20382027
ptFrame->uCurrentBufferIndex = 0;
2039-
2040-
__block dispatch_semaphore_t semaphore = ptFrame->tFrameBoundarySemaphore;
2041-
[ptCommandBuffer->tCmdBuffer addCompletedHandler:^(id<MTLCommandBuffer> commandBuffer) {
2042-
2043-
if(commandBuffer.status == MTLCommandBufferStatusError)
2044-
{
2045-
NSLog(@"PRESENT: %@s", commandBuffer.error);
2046-
}
2047-
// GPU work is complete
2048-
// Signal the semaphore to start the CPU work
2049-
dispatch_semaphore_signal(semaphore);
2050-
2051-
}];
2052-
2028+
[ptCommandBuffer->tCmdBuffer encodeSignalEvent:ptFrame->tFrameBoundaryEvent value:++ptFrame->uNextValue];
20532029
[ptCommandBuffer->tCmdBuffer commit];
2030+
gptGraphics->uCurrentFrameIndex = (gptGraphics->uCurrentFrameIndex + 1) % gptGraphics->uFramesInFlight;
20542031

20552032
return true;
20562033
}
@@ -3430,43 +3407,59 @@
34303407
pl_sb_push(ptDevice->sbtSamplerFreeIndices, iResourceIndex);
34313408
}
34323409

3410+
uint64_t uSignaledValue = ptFrame->tFrameBoundaryEvent.signaledValue;
34333411
for(uint32_t i = 0; i < pl_sb_size(ptGarbage->sbtTextures); i++)
34343412
{
34353413
const uint16_t uTextureIndex = ptGarbage->sbtTextures[i].uIndex;
3436-
plMetalTexture* ptMetalTexture = &ptDevice->sbtTexturesHot[uTextureIndex];
3437-
[ptMetalTexture->tTexture release];
3438-
ptMetalTexture->tTexture = nil;
3439-
pl_sb_push(ptDevice->sbtTextureFreeIndices, uTextureIndex);
3414+
if( uSignaledValue > ptDevice->sbtTexturesCold[uTextureIndex]._uFrameBoundaryValueForDeletion)
3415+
{
3416+
plMetalTexture* ptMetalTexture = &ptDevice->sbtTexturesHot[uTextureIndex];
3417+
[ptMetalTexture->tTexture release];
3418+
ptMetalTexture->tTexture = nil;
3419+
pl_sb_push(ptDevice->sbtTextureFreeIndices, uTextureIndex);
3420+
pl_sb_del(ptGarbage->sbtTextures, i);
3421+
i--;
3422+
}
34403423
}
34413424

34423425
for(uint32_t i = 0; i < pl_sb_size(ptGarbage->sbtBuffers); i++)
34433426
{
34443427
const uint16_t iBufferIndex = ptGarbage->sbtBuffers[i].uIndex;
3445-
[ptDevice->sbtBuffersHot[iBufferIndex].tBuffer release];
3446-
ptDevice->sbtBuffersHot[iBufferIndex].tBuffer = nil;
3447-
pl_sb_push(ptDevice->sbtBufferFreeIndices, iBufferIndex);
3448-
pl_log_debug_f(gptLog, uLogChannelGraphics, "Delete buffer %u for deletion frame %llu", iBufferIndex, gptIO->ulFrameCount);
3428+
if( uSignaledValue > ptDevice->sbtBuffersCold[iBufferIndex]._uFrameBoundaryValueForDeletion)
3429+
{
3430+
[ptDevice->sbtBuffersHot[iBufferIndex].tBuffer release];
3431+
ptDevice->sbtBuffersHot[iBufferIndex].tBuffer = nil;
3432+
pl_sb_push(ptDevice->sbtBufferFreeIndices, iBufferIndex);
3433+
pl_log_debug_f(gptLog, uLogChannelGraphics, "Delete buffer %u for deletion frame %llu", iBufferIndex, gptIO->ulFrameCount);
3434+
pl_sb_del(ptGarbage->sbtBuffers, i);
3435+
i--;
3436+
}
34493437
}
34503438

34513439
for(uint32_t i = 0; i < pl_sb_size(ptGarbage->sbtMemory); i++)
34523440
{
3453-
if(ptGarbage->sbtMemory[i].ptAllocator)
3441+
if( uSignaledValue > ptGarbage->sbtMemory[i]._uFrameBoundaryValueForDeletion)
34543442
{
3455-
ptGarbage->sbtMemory[i].ptAllocator->free(ptGarbage->sbtMemory[i].ptAllocator->ptInst, &ptGarbage->sbtMemory[i]);
3456-
}
3457-
else
3458-
{
3459-
pl_free_memory(ptDevice, &ptGarbage->sbtMemory[i]);
3443+
if(ptGarbage->sbtMemory[i].ptAllocator)
3444+
{
3445+
ptGarbage->sbtMemory[i].ptAllocator->free(ptGarbage->sbtMemory[i].ptAllocator->ptInst, &ptGarbage->sbtMemory[i]);
3446+
}
3447+
else
3448+
{
3449+
pl_free_memory(ptDevice, &ptGarbage->sbtMemory[i]);
3450+
}
3451+
pl_sb_del(ptGarbage->sbtMemory, i);
3452+
i--;
34603453
}
34613454
}
34623455

3463-
pl_sb_reset(ptGarbage->sbtTextures);
3456+
// pl_sb_reset(ptGarbage->sbtTextures);
34643457
pl_sb_reset(ptGarbage->sbtShaders);
34653458
pl_sb_reset(ptGarbage->sbtComputeShaders);
34663459
pl_sb_reset(ptGarbage->sbtRenderPasses);
34673460
pl_sb_reset(ptGarbage->sbtRenderPassLayouts);
3468-
pl_sb_reset(ptGarbage->sbtMemory);
3469-
pl_sb_reset(ptGarbage->sbtBuffers);
3461+
// pl_sb_reset(ptGarbage->sbtMemory);
3462+
// pl_sb_reset(ptGarbage->sbtBuffers);
34703463
pl_sb_reset(ptGarbage->sbtBindGroups);
34713464
pl_end_cpu_sample(gptProfile, 0);
34723465
}

extensions/pl_graphics_vulkan.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -202,6 +202,7 @@ typedef struct _plFrameContext
202202
{
203203
VkFence tInFlight;
204204
VkFramebuffer* sbtRawFrameBuffers;
205+
uint64_t uNextValue;
205206

206207
// dynamic buffer stuff
207208
uint16_t uCurrentBufferIndex;

extensions/pl_renderer_ext.c

Lines changed: 9 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1401,33 +1401,15 @@ pl_renderer_resize_view(plView* ptView, plVec2 tDimensions)
14011401
plRenderPassAttachments atPickAttachmentSets[PL_MAX_FRAMES_IN_FLIGHT] = {0};
14021402

14031403

1404-
uint64_t uOldValue = gptGfx->get_semaphore_value(ptDevice, gptStarter->get_current_timeline_semaphore());
1405-
1406-
if(uOldValue >= gptStarter->get_current_timeline_value())
1407-
{
1408-
gptGfx->destroy_texture(ptDevice, ptView->tFinalTexture);
1409-
gptGfx->destroy_texture(ptDevice, ptView->atUVMaskTexture0);
1410-
gptGfx->destroy_texture(ptDevice, ptView->atUVMaskTexture1);
1411-
gptGfx->destroy_texture(ptDevice, ptView->tRawOutputTexture);
1412-
gptGfx->destroy_texture(ptDevice, ptView->tAlbedoTexture);
1413-
gptGfx->destroy_texture(ptDevice, ptView->tNormalTexture);
1414-
gptGfx->destroy_texture(ptDevice, ptView->tAOMetalRoughnessTexture);
1415-
gptGfx->destroy_texture(ptDevice, ptView->tDepthTexture);
1416-
gptGfx->destroy_texture(ptDevice, ptView->tPickTexture);
1417-
}
1418-
else
1419-
{
1420-
gptGfx->queue_texture_for_deletion(ptDevice, ptView->tFinalTexture);
1421-
gptGfx->queue_texture_for_deletion(ptDevice, ptView->atUVMaskTexture0);
1422-
gptGfx->queue_texture_for_deletion(ptDevice, ptView->atUVMaskTexture1);
1423-
gptGfx->queue_texture_for_deletion(ptDevice, ptView->tRawOutputTexture);
1424-
gptGfx->queue_texture_for_deletion(ptDevice, ptView->tAlbedoTexture);
1425-
gptGfx->queue_texture_for_deletion(ptDevice, ptView->tNormalTexture);
1426-
gptGfx->queue_texture_for_deletion(ptDevice, ptView->tAOMetalRoughnessTexture);
1427-
gptGfx->queue_texture_for_deletion(ptDevice, ptView->tDepthTexture);
1428-
gptGfx->queue_texture_for_deletion(ptDevice, ptView->tPickTexture);
1429-
}
1430-
1404+
gptGfx->queue_texture_for_deletion(ptDevice, ptView->tFinalTexture);
1405+
gptGfx->queue_texture_for_deletion(ptDevice, ptView->atUVMaskTexture0);
1406+
gptGfx->queue_texture_for_deletion(ptDevice, ptView->atUVMaskTexture1);
1407+
gptGfx->queue_texture_for_deletion(ptDevice, ptView->tRawOutputTexture);
1408+
gptGfx->queue_texture_for_deletion(ptDevice, ptView->tAlbedoTexture);
1409+
gptGfx->queue_texture_for_deletion(ptDevice, ptView->tNormalTexture);
1410+
gptGfx->queue_texture_for_deletion(ptDevice, ptView->tAOMetalRoughnessTexture);
1411+
gptGfx->queue_texture_for_deletion(ptDevice, ptView->tDepthTexture);
1412+
gptGfx->queue_texture_for_deletion(ptDevice, ptView->tPickTexture);
14311413
gptGfx->queue_bind_group_for_deletion(ptDevice, ptView->tLightingBindGroup);
14321414
ptView->tPickTexture = pl__renderer_create_texture(&tPickTextureDesc, "pick", 0, PL_TEXTURE_USAGE_SAMPLED);
14331415

@@ -3215,7 +3197,6 @@ pl_renderer_render_view(plView* ptView, plCamera* ptCamera, plCamera* ptCullCame
32153197
.atSignalSempahores = {gptStarter->get_current_timeline_semaphore()},
32163198
.auSignalSemaphoreValues = {gptStarter->increment_current_timeline_value()}
32173199
};
3218-
ptScene->aulStartTimelineValue[uFrameIdx] = tPostSubmitInfo.auSignalSemaphoreValues[0];
32193200

32203201
gptGfx->submit_command_buffer(ptPostCmdBuffer, &tPostSubmitInfo);
32213202
gptGfx->return_command_buffer(ptPostCmdBuffer);

extensions/pl_renderer_internal.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -337,7 +337,6 @@ typedef struct _plScene
337337
plShaderHandle tLightingShader;
338338
plShaderHandle tEnvLightingShader;
339339
uint64_t uLastSemValueForShadow;
340-
uint64_t aulStartTimelineValue[PL_MAX_FRAMES_IN_FLIGHT];
341340

342341
// skybox resources (optional)
343342
plDrawable tSkyboxDrawable;

extensions/pl_starter_ext.c

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,6 @@ typedef struct _plStarterContext
115115
plFont* ptDefaultFont;
116116
plTimelineSemaphore* aptSemaphores[PL_MAX_FRAMES_IN_FLIGHT];
117117
uint64_t aulNextTimelineValue[PL_MAX_FRAMES_IN_FLIGHT];
118-
uint64_t aulStartTimelineValue[PL_MAX_FRAMES_IN_FLIGHT];
119118
plCommandPool* atCmdPools[PL_MAX_FRAMES_IN_FLIGHT];
120119
plRenderPassHandle tRenderPass;
121120
plRenderPassLayoutHandle tRenderPassLayout;
@@ -365,10 +364,7 @@ pl_starter_resize(void)
365364

366365
if(gptStarterCtx->tFlags & PL_STARTER_FLAGS_DEPTH_BUFFER)
367366
{
368-
if(uOldValue >= gptStarterCtx->aulStartTimelineValue[gptGfx->get_current_frame_index()])
369-
gptGfx->destroy_texture(gptStarterCtx->ptDevice, gptStarterCtx->tDepthTexture);
370-
else
371-
gptGfx->queue_texture_for_deletion(gptStarterCtx->ptDevice, gptStarterCtx->tDepthTexture);
367+
gptGfx->queue_texture_for_deletion(gptStarterCtx->ptDevice, gptStarterCtx->tDepthTexture);
372368

373369
const plTextureDesc tDepthTextureDesc = {
374370
.tDimensions = {
@@ -684,7 +680,6 @@ pl_starter_end_main_pass(void)
684680
.atSignalSempahores = {gptStarterCtx->aptSemaphores[uCurrentFrameIndex]},
685681
.auSignalSemaphoreValues = {gptStarterCtx->aulNextTimelineValue[uCurrentFrameIndex]},
686682
};
687-
gptStarterCtx->aulStartTimelineValue[uCurrentFrameIndex] = tSubmitInfo.auSignalSemaphoreValues[0];
688683

689684
if(!gptGfx->present(ptCurrentCommandBuffer, &tSubmitInfo, &gptStarterCtx->ptSwapchain, 1))
690685
{

src/pl_main_macos.m

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -615,7 +615,7 @@ - (void)renderToMetalLayer:(nonnull CAMetalLayer *)layer
615615
gptIOCtx->tMainFramebufferScale.x = fCurrentScaleX;
616616
gptIOCtx->tMainFramebufferScale.y = fCurrentScaleY;
617617
pl_app_resize(gptMainWindow, gpUserData);
618-
return;
618+
// return;
619619
}
620620

621621
if(gtTime == 0.0)

0 commit comments

Comments
 (0)