|
116 | 116 | { |
117 | 117 | uint32_t uCurrentBufferIndex; |
118 | 118 | plMetalDynamicBuffer* sbtDynamicBuffers; |
119 | | - dispatch_semaphore_t tFrameBoundarySemaphore; |
| 119 | + id<MTLSharedEvent> tFrameBoundaryEvent; |
| 120 | + uint64_t uNextValue; |
| 121 | + uint64_t uLastValue; |
120 | 122 | } plFrameContext; |
121 | 123 |
|
122 | 124 | typedef struct _plMetalTexture |
|
1767 | 1769 | for(uint32_t i = 0; i < gptGraphics->uFramesInFlight; i++) |
1768 | 1770 | { |
1769 | 1771 | plFrameContext tFrame = { |
1770 | | - .tFrameBoundarySemaphore = dispatch_semaphore_create(1), |
| 1772 | + .uNextValue = 0 |
1771 | 1773 | }; |
| 1774 | + |
| 1775 | + tFrame.tFrameBoundaryEvent = [ptDevice->tDevice newSharedEvent]; |
| 1776 | + |
1772 | 1777 | pl_sb_resize(tFrame.sbtDynamicBuffers, 1); |
1773 | 1778 | static char atNameBuffer[PL_MAX_NAME_LENGTH] = {0}; |
1774 | 1779 | pl_sprintf(atNameBuffer, "D-BUF-F%d-0", (int)i); |
|
1852 | 1857 | ptSwap->tInfo.tSampleCount = pl_min(ptInit->tSampleCount, ptSwap->ptDevice->tInfo.tMaxSampleCount); |
1853 | 1858 | if(ptSwap->tInfo.tSampleCount == 0) |
1854 | 1859 | 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 | | - } |
1866 | 1860 | } |
1867 | 1861 |
|
1868 | 1862 | static plCommandPool* |
|
1940 | 1934 |
|
1941 | 1935 | // Wait until the inflight command buffer has completed its work |
1942 | 1936 | // gptGraphics->tSwapchain.uCurrentImageIndex = gptGraphics->uCurrentFrameIndex; |
1943 | | - gptGraphics->uCurrentFrameIndex = (gptGraphics->uCurrentFrameIndex + 1) % gptGraphics->uFramesInFlight; |
1944 | 1937 | plFrameContext* ptFrame = pl__get_frame_resources(ptDevice); |
| 1938 | + [ptFrame->tFrameBoundaryEvent waitUntilSignaledValue:ptFrame->uNextValue timeoutMS:10000]; |
1945 | 1939 |
|
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) |
1950 | 1941 | { |
1951 | 1942 | pl__garbage_collect(ptDevice); |
| 1943 | + ptFrame->uLastValue = ptFrame->uNextValue; |
1952 | 1944 | } |
1953 | | - else |
1954 | | - { |
1955 | | - bFirstRun = false; |
1956 | | - } |
| 1945 | + |
1957 | 1946 |
|
1958 | 1947 | pl_end_cpu_sample(gptProfile, 0); |
1959 | 1948 | } |
|
2036 | 2025 | } |
2037 | 2026 |
|
2038 | 2027 | 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]; |
2053 | 2029 | [ptCommandBuffer->tCmdBuffer commit]; |
| 2030 | + gptGraphics->uCurrentFrameIndex = (gptGraphics->uCurrentFrameIndex + 1) % gptGraphics->uFramesInFlight; |
2054 | 2031 |
|
2055 | 2032 | return true; |
2056 | 2033 | } |
|
3430 | 3407 | pl_sb_push(ptDevice->sbtSamplerFreeIndices, iResourceIndex); |
3431 | 3408 | } |
3432 | 3409 |
|
| 3410 | + uint64_t uSignaledValue = ptFrame->tFrameBoundaryEvent.signaledValue; |
3433 | 3411 | for(uint32_t i = 0; i < pl_sb_size(ptGarbage->sbtTextures); i++) |
3434 | 3412 | { |
3435 | 3413 | 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 | + } |
3440 | 3423 | } |
3441 | 3424 |
|
3442 | 3425 | for(uint32_t i = 0; i < pl_sb_size(ptGarbage->sbtBuffers); i++) |
3443 | 3426 | { |
3444 | 3427 | 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 | + } |
3449 | 3437 | } |
3450 | 3438 |
|
3451 | 3439 | for(uint32_t i = 0; i < pl_sb_size(ptGarbage->sbtMemory); i++) |
3452 | 3440 | { |
3453 | | - if(ptGarbage->sbtMemory[i].ptAllocator) |
| 3441 | + if( uSignaledValue > ptGarbage->sbtMemory[i]._uFrameBoundaryValueForDeletion) |
3454 | 3442 | { |
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--; |
3460 | 3453 | } |
3461 | 3454 | } |
3462 | 3455 |
|
3463 | | - pl_sb_reset(ptGarbage->sbtTextures); |
| 3456 | + // pl_sb_reset(ptGarbage->sbtTextures); |
3464 | 3457 | pl_sb_reset(ptGarbage->sbtShaders); |
3465 | 3458 | pl_sb_reset(ptGarbage->sbtComputeShaders); |
3466 | 3459 | pl_sb_reset(ptGarbage->sbtRenderPasses); |
3467 | 3460 | 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); |
3470 | 3463 | pl_sb_reset(ptGarbage->sbtBindGroups); |
3471 | 3464 | pl_end_cpu_sample(gptProfile, 0); |
3472 | 3465 | } |
|
0 commit comments