Skip to content

Commit 5c5c9b3

Browse files
committed
WIP
1 parent db0212c commit 5c5c9b3

8 files changed

Lines changed: 135 additions & 179 deletions

File tree

extensions/pl_dear_imgui_ext.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,7 @@ pl_dear_imgui_new_frame(plDevice *ptDevice, plRenderPassHandle tMainRenderPass)
111111
}
112112

113113
void
114-
pl_dear_imgui_render(plRenderEncoder *ptRenderEncoder, plCommandBuffer *ptCommandBuffer)
114+
pl_dear_imgui_render(plCommandBuffer *ptCommandBuffer)
115115
{
116116
ImGui::Render();
117117
ImDrawData *main_draw_data = ImGui::GetDrawData();
@@ -120,7 +120,7 @@ pl_dear_imgui_render(plRenderEncoder *ptRenderEncoder, plCommandBuffer *ptComman
120120
#elif defined(PL_VULKAN_BACKEND)
121121
ImGui_ImplVulkan_RenderDrawData(main_draw_data, gptGfx->get_vulkan_command_buffer(ptCommandBuffer));
122122
#elif defined(PL_METAL_BACKEND)
123-
ImGui_ImplMetal_RenderDrawData(main_draw_data, gptGfx->get_metal_command_buffer(ptCommandBuffer), gptGfx->get_metal_command_encoder(ptRenderEncoder));
123+
ImGui_ImplMetal_RenderDrawData(main_draw_data, gptGfx->get_metal_command_buffer(ptCommandBuffer), gptGfx->get_metal_command_encoder(ptCommandBuffer));
124124
#else
125125
#endif
126126

extensions/pl_dear_imgui_ext.h

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,6 @@ Index of this file:
4141
// external (pl_graphics_ext.h)
4242
typedef union plRenderPassHandle plRenderPassHandle;
4343
typedef union plBindGroupHandle plBindGroupHandle;
44-
typedef struct _plRenderEncoder plRenderEncoder;
4544
typedef struct _plSwapchain plSwapchain;
4645
typedef struct _plCommandBuffer plCommandBuffer;
4746
typedef struct _plDevice plDevice;
@@ -60,7 +59,7 @@ PL_API void pl_dear_imgui_cleanup (void);
6059

6160
// per frame
6261
PL_API void pl_dear_imgui_new_frame(plDevice*, plRenderPassHandle tMainRenderPass);
63-
PL_API void pl_dear_imgui_render (plRenderEncoder*, plCommandBuffer*);
62+
PL_API void pl_dear_imgui_render (plCommandBuffer*);
6463

6564
// for using texture in Dear ImGui specifically
6665
PL_API ImTextureID pl_dear_imgui_get_texture_id_from_bindgroup(plDevice*, plBindGroupHandle);
@@ -77,7 +76,7 @@ typedef struct _plDearImGuiI
7776

7877
// per frame
7978
void (*new_frame)(plDevice*, plRenderPassHandle tMainRenderPass);
80-
void (*render) (plRenderEncoder*, plCommandBuffer*);
79+
void (*render) (plCommandBuffer*);
8180

8281
ImTextureID (*get_texture_id_from_bindgroup)(plDevice*, plBindGroupHandle);
8382
} plDearImGuiI;

extensions/pl_graphics_cpu.c

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -764,16 +764,6 @@ pl_graphics_end_compute_pass(plComputeEncoder* ptEncoder)
764764
pl__return_compute_encoder(ptEncoder);
765765
}
766766

767-
void
768-
pl_graphics_begin_blit_pass(plCommandBuffer* ptCmdBuffer)
769-
{
770-
}
771-
772-
void
773-
pl_graphics_end_blit_pass(plCommandBuffer* ptCmdBuffer)
774-
{
775-
}
776-
777767
void
778768
pl_graphics_dispatch(plComputeEncoder* ptEncoder, uint32_t uDispatchCount, const plDispatch *atDispatches)
779769
{

extensions/pl_graphics_metal.m

Lines changed: 19 additions & 72 deletions
Original file line numberDiff line numberDiff line change
@@ -287,14 +287,6 @@
287287

288288
#define PL__ALIGN_UP(num, align) (((num) + ((align)-1)) & ~((align)-1))
289289

290-
static inline void
291-
pl__metal_barrier_in_encoder(id<MTL4CommandEncoder> encoder)
292-
{
293-
[encoder barrierAfterEncoderStages:MTLStageDispatch | MTLStageBlit
294-
beforeEncoderStages:MTLStageDispatch | MTLStageBlit
295-
visibilityOptions:MTL4VisibilityOptionDevice];
296-
}
297-
298290
//-----------------------------------------------------------------------------
299291
// [SECTION] public api implementation
300292
//-----------------------------------------------------------------------------
@@ -581,8 +573,6 @@
581573
destinationLevel:ptRegion->uMipLevel
582574
destinationOrigin:tOrigin];
583575
}
584-
585-
pl__metal_barrier_in_encoder(ptDevice->tComputeEncoder);
586576
}
587577
}
588578

@@ -626,8 +616,6 @@
626616
destinationSlice:ptRegions[i].uDestinationBaseArrayLayer
627617
destinationLevel:ptRegions[i].uDestinationMipLevel
628618
destinationOrigin:tDestOrigin];
629-
630-
pl__metal_barrier_in_encoder(ptDevice->tComputeEncoder);
631619
}
632620
}
633621

@@ -690,8 +678,6 @@
690678
destinationBytesPerRow:uBytesPerRow
691679
destinationBytesPerImage:uBytesPerImage];
692680
}
693-
694-
pl__metal_barrier_in_encoder(ptDevice->tComputeEncoder);
695681
}
696682
}
697683

@@ -706,7 +692,6 @@
706692

707693
plDevice* ptDevice = ptCmdBuffer->ptDevice;
708694
[ptDevice->tComputeEncoder copyFromBuffer:ptDevice->sbtBuffersHot[tSource.uIndex].tBuffer sourceOffset:uSourceOffset toBuffer:ptDevice->sbtBuffersHot[tDestination.uIndex].tBuffer destinationOffset:uDestinationOffset size:szSize];
709-
pl__metal_barrier_in_encoder(ptDevice->tComputeEncoder);
710695
}
711696

712697
plTimelineSemaphore*
@@ -840,8 +825,6 @@
840825
return;
841826

842827
[ptDevice->tComputeEncoder generateMipmapsForTexture:ptDevice->sbtTexturesHot[tTexture.uIndex].tTexture];
843-
844-
pl__metal_barrier_in_encoder(ptDevice->tComputeEncoder);
845828
}
846829

847830
void
@@ -2168,8 +2151,10 @@
21682151
// Add a producer queue barrier that blocks any dispatch stages in subsequent passes
21692152
// in the queue, not counting this one, from running until the blit stages in all
21702153
// previous passes finish running, including this one.
2171-
[ptDevice->tRenderEncoder barrierAfterStages:MTLStageAll
2172-
beforeQueueStages:MTLStageAll
2154+
2155+
// producer queue barrier
2156+
[ptDevice->tRenderEncoder barrierAfterStages:MTLStageFragment
2157+
beforeQueueStages:MTLStageVertex
21732158
visibilityOptions:MTL4VisibilityOptionDevice];
21742159

21752160
[ptDevice->tRenderEncoder endEncoding];
@@ -2181,9 +2166,9 @@
21812166
// Add a consumer queue barrier that blocks any dispatch stages in subsequent passes
21822167
// in the queue, including this one, from running until blit stages in all
21832168
// previous passes finish running, not counting this one.
2184-
[ptDevice->tRenderEncoder barrierAfterQueueStages:MTLStageAll
2185-
beforeStages:MTLStageAll
2186-
visibilityOptions:MTL4VisibilityOptionDevice];
2169+
// [ptDevice->tRenderEncoder barrierAfterQueueStages:MTLStageAll
2170+
// beforeStages:MTLStageAll
2171+
// visibilityOptions:MTL4VisibilityOptionDevice];
21872172
}
21882173

21892174
void
@@ -2210,9 +2195,9 @@
22102195
// Add a consumer queue barrier that blocks any dispatch stages in subsequent passes
22112196
// in the queue, including this one, from running until blit stages in all
22122197
// previous passes finish running, not counting this one.
2213-
[ptDevice->tRenderEncoder barrierAfterQueueStages:MTLStageAll
2214-
beforeStages:MTLStageAll
2215-
visibilityOptions:MTL4VisibilityOptionDevice];
2198+
// [ptDevice->tRenderEncoder barrierAfterQueueStages:MTLStageAll
2199+
// beforeStages:MTLStageAll
2200+
// visibilityOptions:MTL4VisibilityOptionDevice];
22162201
}
22172202

22182203
void
@@ -2226,9 +2211,9 @@
22262211
// Add a producer queue barrier that blocks any dispatch stages in subsequent passes
22272212
// in the queue, not counting this one, from running until the blit stages in all
22282213
// previous passes finish running, including this one.
2229-
[ptDevice->tRenderEncoder barrierAfterStages:MTLStageAll
2230-
beforeQueueStages:MTLStageAll
2231-
visibilityOptions:MTL4VisibilityOptionDevice];
2214+
// [ptDevice->tRenderEncoder barrierAfterStages:MTLStageAll
2215+
// beforeQueueStages:MTLStageAll
2216+
// visibilityOptions:MTL4VisibilityOptionDevice];
22322217

22332218
[ptDevice->tRenderEncoder endEncoding];
22342219
ptDevice->tRenderEncoder = nil;
@@ -2307,43 +2292,6 @@
23072292
ptCmdBuffer->ptPool->ptCommandBufferFreeList = ptCmdBuffer;
23082293
}
23092294

2310-
void
2311-
pl_graphics_begin_blit_pass(plCommandBuffer* ptCmdBuffer)
2312-
{
2313-
PL_ASSERT(gptGraphics->bEncoderActive == false);
2314-
gptGraphics->bEncoderActive = true;
2315-
plDevice* ptDevice = ptCmdBuffer->ptDevice;
2316-
2317-
ptDevice->tComputeEncoder = [ptCmdBuffer->tCmdBuffer4 computeCommandEncoder];
2318-
2319-
// Add a consumer queue barrier that blocks any dispatch stages in subsequent passes
2320-
// in the queue, including this one, from running until blit stages in all
2321-
// previous passes finish running, not counting this one.
2322-
[ptDevice->tComputeEncoder barrierAfterQueueStages:MTLStageAll
2323-
beforeStages:MTLStageAll
2324-
visibilityOptions:MTL4VisibilityOptionDevice];
2325-
}
2326-
2327-
void
2328-
pl_graphics_end_blit_pass(plCommandBuffer* ptCmdBuffer)
2329-
{
2330-
plDevice* ptDevice = ptCmdBuffer->ptDevice;
2331-
2332-
// Add a producer queue barrier that blocks any dispatch stages in subsequent passes
2333-
// in the queue, not counting this one, from running until the blit stages in all
2334-
// previous passes finish running, including this one.
2335-
[ptDevice->tComputeEncoder barrierAfterStages:MTLStageAll
2336-
beforeQueueStages:MTLStageAll
2337-
visibilityOptions:MTL4VisibilityOptionDevice];
2338-
2339-
// if(ptEncoder->bActive)
2340-
// {
2341-
[ptDevice->tComputeEncoder endEncoding];
2342-
// }
2343-
gptGraphics->bEncoderActive = false;
2344-
ptDevice->tComputeEncoder = nil;
2345-
}
2346-
23472295
void
23482296
pl_graphics_begin_compute_pass(plCommandBuffer* ptCmdBuffer, const plPassResources* ptResources)
23492297
{
@@ -2353,9 +2301,9 @@
23532301
// Add a consumer queue barrier that blocks any dispatch stages in subsequent passes
23542302
// in the queue, including this one, from running until blit stages in all
23552303
// previous passes finish running, not counting this one.
2356-
[ptDevice->tComputeEncoder barrierAfterQueueStages:MTLStageAll
2357-
beforeStages:MTLStageAll
2358-
visibilityOptions:MTL4VisibilityOptionDevice];
2304+
// [ptDevice->tComputeEncoder barrierAfterQueueStages:MTLStageAll
2305+
// beforeStages:MTLStageAll
2306+
// visibilityOptions:MTL4VisibilityOptionDevice];
23592307
}
23602308

23612309
void
@@ -2366,9 +2314,9 @@
23662314
// Add a producer queue barrier that blocks any dispatch stages in subsequent passes
23672315
// in the queue, not counting this one, from running until the blit stages in all
23682316
// previous passes finish running, including this one.
2369-
[ptDevice->tComputeEncoder barrierAfterStages:MTLStageAll
2370-
beforeQueueStages:MTLStageAll
2371-
visibilityOptions:MTL4VisibilityOptionDevice];
2317+
// [ptDevice->tComputeEncoder barrierAfterStages:MTLStageAll
2318+
// beforeQueueStages:MTLStageAll
2319+
// visibilityOptions:MTL4VisibilityOptionDevice];
23722320

23732321
[ptDevice->tComputeEncoder endEncoding];
23742322
ptDevice->tComputeEncoder = nil;
@@ -2472,7 +2420,6 @@
24722420
MTLSize tGridSize = MTLSizeMake(ptDispatch->uGroupCountX, ptDispatch->uGroupCountY, ptDispatch->uGroupCountZ);
24732421
MTLSize tThreadsPerGroup = MTLSizeMake(ptDispatch->uThreadPerGroupX, ptDispatch->uThreadPerGroupY, ptDispatch->uThreadPerGroupZ);
24742422
[ptDevice->tComputeEncoder dispatchThreadgroups:tGridSize threadsPerThreadgroup:tThreadsPerGroup];
2475-
// pl__metal_barrier_in_encoder(ptDevice->tComputeEncoder);
24762423
}
24772424
}
24782425

extensions/pl_renderer_internal.c

Lines changed: 28 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -3371,65 +3371,43 @@ pl__renderer_create_environment_map_from_texture(plScene* ptScene, plEnvironment
33713371
plMipMapChain tChain = {0};
33723372
pl_mipmap_generate_mip_chain_cpu(&tMipDesc, &tChain);
33733373

3374+
ptCommandBuffer = gptGfx->request_command_buffer(ptCmdPool, "map to cube env");
3375+
gptGfx->begin_command_recording(ptCommandBuffer);
3376+
gptGfx->begin_compute_pass(ptCommandBuffer, NULL);
3377+
33743378
size_t uStageOffset = tChain.atLevels[0].szFaceStride * 6;
33753379
for(uint32_t i = 1; i < tChain.uMipCount; i++)
33763380
{
33773381
// copy memory to mapped staging buffer
3378-
// memcpy(&ptStagingBuffer->tMemoryAllocation.pHostMapped[uStageOffset], tChain.atLevels[i].pData, tChain.atLevels[i].szFaceStride);
3382+
memcpy(&ptStagingBuffer->tMemoryAllocation.pHostMapped[uStageOffset], tChain.atLevels[i].pData, tChain.atLevels[i].szFaceStride);
33793383

33803384

3381-
// const plBufferImageCopy tBufferImageCopy = {
3382-
// .uImageWidth = tChain.atLevels[i].uWidth,
3383-
// .uImageHeight = tChain.atLevels[i].uHeight,
3384-
// .uImageDepth = 1,
3385-
// .uLayerCount = 6,
3386-
// .szBufferOffset = uStageOffset,
3387-
// .uMipLevel = i,
3388-
// };
3389-
// uStageOffset += tChain.atLevels[i].szFaceStride * 6;
3390-
3391-
// gptGfx->copy_buffer_to_texture(ptBlitEncoder, tStagingBuffer, ptProbe->tRawOutputTexture, 1, &tBufferImageCopy);
3392-
for(uint32_t j = 0; j < 6; j++)
3393-
{
3394-
3395-
ptCommandBuffer = gptGfx->request_command_buffer(ptCmdPool, "map to cube env");
3396-
gptGfx->begin_command_recording(ptCommandBuffer);
3397-
gptGfx->begin_compute_pass(ptCommandBuffer, NULL);
3398-
3399-
// copy memory to mapped staging buffer
3400-
memcpy(&ptStagingBuffer->tMemoryAllocation.pHostMapped[uStageOffset], (uint8_t*)tChain.atLevels[i].pData + j * tChain.atLevels[i].szFaceStride, tChain.atLevels[i].szFaceStride);
3401-
3402-
3403-
const plBufferImageCopy tBufferImageCopy = {
3404-
.uImageWidth = tChain.atLevels[i].uWidth,
3405-
.uImageHeight = tChain.atLevels[i].uHeight,
3406-
.uImageDepth = 1,
3407-
.uLayerCount = 1,
3408-
.szBufferOffset = uStageOffset,
3409-
.uMipLevel = i,
3410-
.uBaseArrayLayer = j
3411-
};
3412-
uStageOffset += tChain.atLevels[i].szFaceStride;
3413-
3414-
gptGfx->copy_buffer_to_texture(ptCommandBuffer, tStagingBuffer, ptProbe->tRawOutputTexture, 1, &tBufferImageCopy);
3415-
3416-
gptGfx->end_compute_pass(ptCommandBuffer);
3417-
gptGfx->end_command_recording(ptCommandBuffer);
3418-
const plSubmitInfo tSubmitInfo = {
3419-
.uWaitSemaphoreCount = 1,
3420-
.atWaitSempahores = {tSemHandle},
3421-
.auWaitSemaphoreValues = {gptStarter->get_current_timeline_value()},
3422-
.uSignalSemaphoreCount = 1,
3423-
.atSignalSempahores = {tSemHandle},
3424-
.auSignalSemaphoreValues = {gptStarter->increment_current_timeline_value()}
3425-
};
3426-
gptGfx->submit_command_buffer(ptCommandBuffer, &tSubmitInfo);
3427-
gptGfx->wait_on_command_buffer(ptCommandBuffer);
3428-
gptGfx->return_command_buffer(ptCommandBuffer);
3429-
}
3385+
const plBufferImageCopy tBufferImageCopy = {
3386+
.uImageWidth = tChain.atLevels[i].uWidth,
3387+
.uImageHeight = tChain.atLevels[i].uHeight,
3388+
.uImageDepth = 1,
3389+
.uLayerCount = 6,
3390+
.szBufferOffset = uStageOffset,
3391+
.uMipLevel = i,
3392+
};
3393+
uStageOffset += tChain.atLevels[i].szFaceStride * 6;
34303394

3395+
gptGfx->copy_buffer_to_texture(ptCommandBuffer, tStagingBuffer, ptProbe->tRawOutputTexture, 1, &tBufferImageCopy);
34313396
}
34323397

3398+
gptGfx->end_compute_pass(ptCommandBuffer);
3399+
gptGfx->end_command_recording(ptCommandBuffer);
3400+
const plSubmitInfo tSubmitInfo = {
3401+
.uWaitSemaphoreCount = 1,
3402+
.atWaitSempahores = {tSemHandle},
3403+
.auWaitSemaphoreValues = {gptStarter->get_current_timeline_value()},
3404+
.uSignalSemaphoreCount = 1,
3405+
.atSignalSempahores = {tSemHandle},
3406+
.auSignalSemaphoreValues = {gptStarter->increment_current_timeline_value()}
3407+
};
3408+
gptGfx->submit_command_buffer(ptCommandBuffer, &tSubmitInfo);
3409+
gptGfx->wait_on_command_buffer(ptCommandBuffer);
3410+
gptGfx->return_command_buffer(ptCommandBuffer);
34333411

34343412
gptGfx->destroy_buffer(ptDevice, tStagingBuffer);
34353413
pl_mipmap_free_mip_chain(&tChain);

extensions/pl_starter_ext.c

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1378,9 +1378,6 @@ pl__starter_create_render_pass_with_msaa_and_depth(void)
13781378
.ptSwapchain = gptStarterCtx->ptSwapchain
13791379
};
13801380

1381-
plCommandBuffer* ptCommandBuffer = gptGfx->request_command_buffer(gptStarterCtx->atCmdPools[gptGfx->get_current_frame_index()], "starter 1");
1382-
gptGfx->begin_command_recording(ptCommandBuffer);
1383-
13841381
uint32_t uImageCount = 0;
13851382
plTextureHandle* atSwapchainImages = gptGfx->get_swapchain_images(gptStarterCtx->ptSwapchain, &uImageCount);
13861383
plRenderPassAttachments atMainAttachmentSets[16] = {0};

thirdparty/imgui/imgui_impl_metal.h

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -25,16 +25,16 @@
2525

2626
#ifdef __OBJC__
2727

28-
@class MTLRenderPassDescriptor;
29-
@protocol MTLDevice, MTLCommandBuffer, MTLRenderCommandEncoder;
28+
@class MTL4RenderPassDescriptor;
29+
@protocol MTLDevice, MTL4CommandBuffer, MTL4RenderCommandEncoder;
3030

3131
// Follow "Getting Started" link and check examples/ folder to learn about using backends!
3232
IMGUI_IMPL_API bool ImGui_ImplMetal_Init(id<MTLDevice> device);
3333
IMGUI_IMPL_API void ImGui_ImplMetal_Shutdown();
34-
IMGUI_IMPL_API void ImGui_ImplMetal_NewFrame(MTLRenderPassDescriptor* renderPassDescriptor);
34+
IMGUI_IMPL_API void ImGui_ImplMetal_NewFrame(MTL4RenderPassDescriptor* renderPassDescriptor);
3535
IMGUI_IMPL_API void ImGui_ImplMetal_RenderDrawData(ImDrawData* drawData,
36-
id<MTLCommandBuffer> commandBuffer,
37-
id<MTLRenderCommandEncoder> commandEncoder);
36+
id<MTL4CommandBuffer> commandBuffer,
37+
id<MTL4RenderCommandEncoder> commandEncoder);
3838

3939
// Called by Init/NewFrame/Shutdown
4040
IMGUI_IMPL_API bool ImGui_ImplMetal_CreateDeviceObjects(id<MTLDevice> device);

0 commit comments

Comments
 (0)