Skip to content

Commit 627aa83

Browse files
committed
all done
1 parent 0abaca4 commit 627aa83

10 files changed

Lines changed: 195 additions & 79 deletions

extensions/pl_dear_imgui_ext.cpp

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -117,9 +117,7 @@ pl_dear_imgui_new_frame(plDevice *ptDevice)
117117
#ifdef PL_CPU_BACKEND
118118
#elif defined(PL_VULKAN_BACKEND)
119119
ImGui_ImplVulkan_NewFrame();
120-
#elif defined(PL_METAL_BACKEND)
121-
ImGui_ImplMetal_NewFrame(gptGfx->get_metal_render_pass_descriptor(ptDevice, tMainRenderPass));
122-
#else
120+
123121
#endif
124122

125123
ImGui::NewFrame();
@@ -128,6 +126,10 @@ pl_dear_imgui_new_frame(plDevice *ptDevice)
128126
void
129127
pl_dear_imgui_render(plCommandBuffer *ptCommandBuffer)
130128
{
129+
#ifdef PL_METAL_BACKEND
130+
ImGui_ImplMetal_NewFrame(gptGfx->get_metal_render_pass_descriptor(ptCommandBuffer));
131+
#endif
132+
131133
ImGui::Render();
132134
ImDrawData *main_draw_data = ImGui::GetDrawData();
133135

extensions/pl_graphics_ext.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1074,6 +1074,7 @@ pl_load_graphics_ext(plApiRegistryI* ptApiRegistry, bool bReload)
10741074
.get_metal_command_encoder = pl_graphics_get_metal_command_encoder,
10751075
.get_metal_texture = pl_graphics_get_metal_texture,
10761076
.get_metal_bind_group_texture = pl_graphics_get_metal_bind_group_texture,
1077+
.get_metal_render_pass_descriptor = pl_graphics_get_metal_render_pass_descriptor,
10771078
#endif
10781079
};
10791080
pl_set_api(ptApiRegistry, plGraphicsI, &tApi);

extensions/pl_graphics_ext.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -296,6 +296,7 @@ typedef enum VkFormat VkFormat;
296296
#endif
297297

298298
#ifdef PL_GRAPHICS_EXPOSE_METAL
299+
@class MTL4RenderPassDescriptor;
299300
@protocol MTLDevice, MTL4CommandBuffer, MTL4RenderCommandEncoder, MTLTexture;
300301
#endif
301302

@@ -769,6 +770,7 @@ typedef struct _plGraphicsI
769770
id<MTLDevice> (*get_metal_device)(plDevice*);
770771
plTextureHandle (*get_metal_bind_group_texture)(plDevice*, plBindGroupHandle);
771772
id<MTLTexture> (*get_metal_texture)(plDevice*, plTextureHandle);
773+
MTL4RenderPassDescriptor* (*get_metal_render_pass_descriptor)(plCommandBuffer*);
772774
id<MTL4CommandBuffer> (*get_metal_command_buffer)(plCommandBuffer*);
773775
id<MTL4RenderCommandEncoder> (*get_metal_command_encoder)(plCommandBuffer*);
774776
#endif

extensions/pl_graphics_metal.m

Lines changed: 83 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,7 @@
8686
uint64_t uHeap;
8787
MTLTextureDescriptor* ptTextureDescriptor;
8888
bool bOriginalView;
89+
bool bSwapchain;
8990
} plMetalTexture;
9091

9192
typedef struct _plMetalSampler
@@ -143,7 +144,8 @@
143144
plTempAllocator tTempAllocator;
144145
CAMetalLayer* pMetalLayer;
145146

146-
bool bEncoderActive;
147+
bool bComputeEncoderActive;
148+
bool bRenderEncoderActive;
147149
} plGraphics;
148150

149151
typedef struct _plDevice
@@ -211,6 +213,7 @@
211213

212214
// render encoder
213215
id<MTL4RenderCommandEncoder> tRenderEncoder;
216+
MTL4RenderPassDescriptor* ptRenderPassDescriptor;
214217

215218
id<MTL4ArgumentTable> tArgumentTable;
216219
id<MTLResidencySet> tResidencySet;
@@ -1626,6 +1629,7 @@
16261629
ptTexture->tView.uMips = 1;
16271630
ptTexture->tView.uLayerCount = 1;
16281631
ptTexture->tView.tTexture = tHandle;
1632+
ptTexture->tView.tTexture = tHandle;
16291633
ptTexture->tView.pcDebugName = "swapchain dummy image view";
16301634
}
16311635
ptSwap->tInfo.uWidth = (uint32_t)gptIO->tMainViewportSize.x;
@@ -1763,6 +1767,7 @@
17631767

17641768
plTextureHandle tTexture = ptSwapchain->sbtSwapchainTextureViews[gptGraphics->uCurrentFrameIndex];
17651769
ptDevice->sbtTexturesHot[tTexture.uIndex].tTexture = ptSwapchain->tCurrentDrawable.texture;
1770+
ptDevice->sbtTexturesHot[tTexture.uIndex].bSwapchain = true;
17661771

17671772
PL_PROFILE_END_SAMPLE_API(gptProfile, 0);
17681773
return true;
@@ -1878,57 +1883,72 @@
18781883
pl_graphics_begin_render_pass(plCommandBuffer* ptCmdBuffer, plRenderInfo tInfo, const plPassResources* ptResources)
18791884
{
18801885
plDevice* ptDevice = ptCmdBuffer->ptDevice;
1881-
gptGraphics->bEncoderActive = true;
1886+
gptGraphics->bRenderEncoderActive = true;
18821887

1883-
MTL4RenderPassDescriptor* ptRenderPassDescriptor = [MTL4RenderPassDescriptor new];
1888+
ptDevice->ptRenderPassDescriptor = [MTL4RenderPassDescriptor new];
18841889

18851890
for(uint32_t i = 0; i < PL_MAX_RENDER_TARGETS; i++)
18861891
{
18871892
if(!pl_graphics_is_texture_valid(ptDevice, tInfo.atColorAttachments[i].tTexture))
18881893
break;
18891894

1890-
ptRenderPassDescriptor.colorAttachments[i].texture = ptDevice->sbtTexturesHot[tInfo.atColorAttachments[i].tTexture.uIndex].tTexture;
1891-
ptRenderPassDescriptor.colorAttachments[i].loadAction = pl__metal_load_op(tInfo.atColorAttachments[i].tLoadOp);
1892-
ptRenderPassDescriptor.colorAttachments[i].storeAction = pl__metal_store_op(tInfo.atColorAttachments[i].tStoreOp);
1893-
ptRenderPassDescriptor.colorAttachments[i].slice = ptDevice->sbtTexturesCold[tInfo.atColorAttachments[i].tTexture.uIndex].tView.uBaseLayer;
1895+
ptDevice->ptRenderPassDescriptor.colorAttachments[i].texture = ptDevice->sbtTexturesHot[tInfo.atColorAttachments[i].tTexture.uIndex].tTexture;
1896+
ptDevice->ptRenderPassDescriptor.colorAttachments[i].loadAction = pl__metal_load_op(tInfo.atColorAttachments[i].tLoadOp);
1897+
ptDevice->ptRenderPassDescriptor.colorAttachments[i].storeAction = pl__metal_store_op(tInfo.atColorAttachments[i].tStoreOp);
1898+
ptDevice->ptRenderPassDescriptor.colorAttachments[i].slice = ptDevice->sbtTexturesCold[tInfo.atColorAttachments[i].tTexture.uIndex].tView.uBaseLayer;
18941899

1895-
ptRenderPassDescriptor.colorAttachments[i].clearColor = MTLClearColorMake(
1900+
ptDevice->ptRenderPassDescriptor.colorAttachments[i].clearColor = MTLClearColorMake(
18961901
tInfo.atColorAttachments[i].tClearColor.r,
18971902
tInfo.atColorAttachments[i].tClearColor.g,
18981903
tInfo.atColorAttachments[i].tClearColor.b,
18991904
tInfo.atColorAttachments[i].tClearColor.a);
19001905

19011906
if(tInfo.atColorAttachments[i].tResolveTexture.uIndex > 0)
19021907
{
1903-
ptRenderPassDescriptor.colorAttachments[i].resolveTexture = ptDevice->sbtTexturesHot[tInfo.atColorAttachments[i].tResolveTexture.uIndex].tTexture;
1904-
ptRenderPassDescriptor.colorAttachments[i].storeAction = MTLStoreActionMultisampleResolve;
1908+
ptDevice->ptRenderPassDescriptor.colorAttachments[i].resolveTexture = ptDevice->sbtTexturesHot[tInfo.atColorAttachments[i].tResolveTexture.uIndex].tTexture;
1909+
ptDevice->ptRenderPassDescriptor.colorAttachments[i].storeAction = MTLStoreActionMultisampleResolve;
19051910

19061911
}
19071912
}
19081913

19091914
if(pl_graphics_is_texture_valid(ptDevice, tInfo.tDepthAttachment.tTexture))
19101915
{
1911-
ptRenderPassDescriptor.depthAttachment.loadAction = pl__metal_load_op(tInfo.tDepthAttachment.tLoadOp);
1912-
ptRenderPassDescriptor.depthAttachment.storeAction = pl__metal_store_op(tInfo.tDepthAttachment.tStoreOp);
1913-
ptRenderPassDescriptor.depthAttachment.clearDepth = tInfo.tDepthAttachment.fClearZ;
1914-
ptRenderPassDescriptor.depthAttachment.texture = ptDevice->sbtTexturesHot[tInfo.tDepthAttachment.tTexture.uIndex].tTexture;
1915-
ptRenderPassDescriptor.depthAttachment.slice = ptDevice->sbtTexturesCold[tInfo.tDepthAttachment.tTexture.uIndex].tView.uBaseLayer;
1916+
ptDevice->ptRenderPassDescriptor.depthAttachment.loadAction = pl__metal_load_op(tInfo.tDepthAttachment.tLoadOp);
1917+
ptDevice->ptRenderPassDescriptor.depthAttachment.storeAction = pl__metal_store_op(tInfo.tDepthAttachment.tStoreOp);
1918+
ptDevice->ptRenderPassDescriptor.depthAttachment.clearDepth = tInfo.tDepthAttachment.fClearZ;
1919+
ptDevice->ptRenderPassDescriptor.depthAttachment.texture = ptDevice->sbtTexturesHot[tInfo.tDepthAttachment.tTexture.uIndex].tTexture;
1920+
ptDevice->ptRenderPassDescriptor.depthAttachment.slice = ptDevice->sbtTexturesCold[tInfo.tDepthAttachment.tTexture.uIndex].tView.uBaseLayer;
19161921
}
19171922

19181923
if(pl_graphics_is_texture_valid(ptDevice, tInfo.tStencilAttachment.tTexture))
19191924
{
1920-
ptRenderPassDescriptor.stencilAttachment.loadAction = pl__metal_load_op(tInfo.tStencilAttachment.tLoadOp);
1921-
ptRenderPassDescriptor.stencilAttachment.storeAction = pl__metal_store_op(tInfo.tStencilAttachment.tStoreOp);
1922-
ptRenderPassDescriptor.stencilAttachment.clearStencil = tInfo.tStencilAttachment.uClearStencil;
1923-
ptRenderPassDescriptor.stencilAttachment.texture = ptDevice->sbtTexturesHot[tInfo.tStencilAttachment.tTexture.uIndex].tTexture;
1924-
ptRenderPassDescriptor.stencilAttachment.slice = ptDevice->sbtTexturesCold[tInfo.tStencilAttachment.tTexture.uIndex].tView.uBaseLayer;
1925+
ptDevice->ptRenderPassDescriptor.stencilAttachment.loadAction = pl__metal_load_op(tInfo.tStencilAttachment.tLoadOp);
1926+
ptDevice->ptRenderPassDescriptor.stencilAttachment.storeAction = pl__metal_store_op(tInfo.tStencilAttachment.tStoreOp);
1927+
ptDevice->ptRenderPassDescriptor.stencilAttachment.clearStencil = tInfo.tStencilAttachment.uClearStencil;
1928+
ptDevice->ptRenderPassDescriptor.stencilAttachment.texture = ptDevice->sbtTexturesHot[tInfo.tStencilAttachment.tTexture.uIndex].tTexture;
1929+
ptDevice->ptRenderPassDescriptor.stencilAttachment.slice = ptDevice->sbtTexturesCold[tInfo.tStencilAttachment.tTexture.uIndex].tView.uBaseLayer;
19251930
}
19261931

1927-
ptDevice->tRenderEncoder = [ptCmdBuffer->tCmdBuffer4 renderCommandEncoderWithDescriptor:ptRenderPassDescriptor];
1932+
ptDevice->tRenderEncoder = [ptCmdBuffer->tCmdBuffer4 renderCommandEncoderWithDescriptor:ptDevice->ptRenderPassDescriptor];
19281933
// ptDevice->tRenderEncoder.label = [NSString stringWithUTF8String:ptRenderPass->tDesc.pcDebugName];
19291934

1930-
[ptRenderPassDescriptor release];
1931-
ptRenderPassDescriptor = nil;
1935+
for(uint32_t i = 0; i < PL_MAX_RENDER_TARGETS; i++)
1936+
{
1937+
if(!pl_graphics_is_texture_valid(ptDevice, tInfo.atColorAttachments[i].tTexture))
1938+
break;
1939+
1940+
ptDevice->ptRenderPassDescriptor.colorAttachments[i].loadAction = MTLLoadActionLoad;
1941+
}
1942+
1943+
if(pl_graphics_is_texture_valid(ptDevice, tInfo.tDepthAttachment.tTexture))
1944+
{
1945+
ptDevice->ptRenderPassDescriptor.depthAttachment.loadAction = MTLLoadActionLoad;
1946+
}
1947+
1948+
if(pl_graphics_is_texture_valid(ptDevice, tInfo.tStencilAttachment.tTexture))
1949+
{
1950+
ptDevice->ptRenderPassDescriptor.stencilAttachment.loadAction = MTLLoadActionLoad;
1951+
}
19321952
}
19331953

19341954
void
@@ -1937,7 +1957,9 @@
19371957
plDevice* ptDevice = ptCmdBuffer->ptDevice;
19381958
[ptDevice->tRenderEncoder endEncoding];
19391959
ptDevice->tRenderEncoder = nil;
1940-
gptGraphics->bEncoderActive = false;
1960+
gptGraphics->bRenderEncoderActive = false;
1961+
[ptDevice->ptRenderPassDescriptor release];
1962+
ptDevice->ptRenderPassDescriptor = nil;
19411963
}
19421964

19431965
void
@@ -2015,6 +2037,7 @@
20152037
{
20162038
plDevice* ptDevice = ptCmdBuffer->ptDevice;
20172039
ptDevice->tComputeEncoder = [ptCmdBuffer->tCmdBuffer4 computeCommandEncoder];
2040+
gptGraphics->bComputeEncoderActive = true;
20182041
}
20192042

20202043
void
@@ -2024,13 +2047,14 @@
20242047
[ptDevice->tComputeEncoder endEncoding];
20252048
// [ptDevice->tComputeEncoder release];
20262049
ptDevice->tComputeEncoder = nil;
2050+
gptGraphics->bComputeEncoderActive = false;
20272051
}
20282052

20292053
void
20302054
pl_graphics_intra_pass_barrier(plCommandBuffer* ptCmdBuffer, plPipelineStageFlags tSrcStages, plPipelineStageFlags tDstStages, plBarrierScope tScope)
20312055
{
20322056
plDevice* ptDevice = ptCmdBuffer->ptDevice;
2033-
PL_ASSERT(gptGraphics->bEncoderActive);
2057+
PL_ASSERT(gptGraphics->bRenderEncoderActive || gptGraphics->bComputeEncoderActive);
20342058
PL_ASSERT(tSrcStages != 0);
20352059
PL_ASSERT(tDstStages != 0);
20362060

@@ -2050,14 +2074,36 @@
20502074
if(tDstStages & PL_PIPELINE_STAGE_VERTEX) tDstStage |= MTLStageVertex;
20512075
if(tDstStages & PL_PIPELINE_STAGE_FRAGMENT) tDstStage |= MTLStageFragment;
20522076

2053-
[ptDevice->tComputeEncoder barrierAfterEncoderStages:tSrcStage beforeEncoderStages:tDstStage visibilityOptions:tOptions];
2077+
if(gptGraphics->bComputeEncoderActive)
2078+
[ptDevice->tComputeEncoder barrierAfterEncoderStages:tSrcStage beforeEncoderStages:tDstStage visibilityOptions:tOptions];
2079+
else if(gptGraphics->bRenderEncoderActive)
2080+
{
2081+
if(tSrcStage & MTLStageFragment)
2082+
{
2083+
[ptDevice->tRenderEncoder barrierAfterStages:tSrcStage beforeQueueStages:tDstStage visibilityOptions:tOptions];
2084+
[ptDevice->tRenderEncoder endEncoding];
2085+
ptDevice->tRenderEncoder = nil;
2086+
ptDevice->tRenderEncoder = [ptCmdBuffer->tCmdBuffer4 renderCommandEncoderWithDescriptor:ptDevice->ptRenderPassDescriptor];
2087+
[ptDevice->tRenderEncoder barrierAfterQueueStages:tSrcStage beforeStages:tDstStage visibilityOptions:tOptions];
2088+
2089+
}
2090+
else
2091+
[ptDevice->tComputeEncoder barrierAfterEncoderStages:tSrcStage beforeEncoderStages:tDstStage visibilityOptions:tOptions];
2092+
}
2093+
}
2094+
2095+
MTL4RenderPassDescriptor*
2096+
pl_graphics_get_metal_render_pass_descriptor(plCommandBuffer* ptCmdBuffer)
2097+
{
2098+
plDevice* ptDevice = ptCmdBuffer->ptDevice;
2099+
return ptDevice->ptRenderPassDescriptor;
20542100
}
20552101

20562102
void
20572103
pl_graphics_consumer_barrier(plCommandBuffer* ptCmdBuffer, plPipelineStageFlags tSrcStages, plPipelineStageFlags tDstStages, plBarrierScope tScope)
20582104
{
20592105
plDevice* ptDevice = ptCmdBuffer->ptDevice;
2060-
PL_ASSERT(gptGraphics->bEncoderActive);
2106+
PL_ASSERT(gptGraphics->bRenderEncoderActive || gptGraphics->bComputeEncoderActive);
20612107
PL_ASSERT(tSrcStages != 0);
20622108
PL_ASSERT(tDstStages != 0);
20632109

@@ -2077,14 +2123,17 @@
20772123
if(tDstStages & PL_PIPELINE_STAGE_VERTEX) tDstStage |= MTLStageVertex;
20782124
if(tDstStages & PL_PIPELINE_STAGE_FRAGMENT) tDstStage |= MTLStageFragment;
20792125

2080-
[ptDevice->tRenderEncoder barrierAfterQueueStages:tSrcStage beforeStages:tDstStage visibilityOptions:tOptions];
2126+
if(gptGraphics->bComputeEncoderActive)
2127+
[ptDevice->tComputeEncoder barrierAfterQueueStages:tSrcStage beforeStages:tDstStage visibilityOptions:tOptions];
2128+
else if(gptGraphics->bRenderEncoderActive)
2129+
[ptDevice->tRenderEncoder barrierAfterQueueStages:tSrcStage beforeStages:tDstStage visibilityOptions:tOptions];
20812130
}
20822131

20832132
void
20842133
pl_graphics_producer_barrier(plCommandBuffer* ptCmdBuffer, plPipelineStageFlags tSrcStages, plPipelineStageFlags tDstStages, plBarrierScope tScope)
20852134
{
20862135
plDevice* ptDevice = ptCmdBuffer->ptDevice;
2087-
PL_ASSERT(gptGraphics->bEncoderActive);
2136+
PL_ASSERT(gptGraphics->bRenderEncoderActive || gptGraphics->bComputeEncoderActive);
20882137
PL_ASSERT(tSrcStages != 0);
20892138
PL_ASSERT(tDstStages != 0);
20902139

@@ -2104,7 +2153,10 @@
21042153
if(tDstStages & PL_PIPELINE_STAGE_VERTEX) tDstStage |= MTLStageVertex;
21052154
if(tDstStages & PL_PIPELINE_STAGE_FRAGMENT) tDstStage |= MTLStageFragment;
21062155

2107-
[ptDevice->tRenderEncoder barrierAfterStages:tSrcStage beforeQueueStages:tDstStage visibilityOptions:tOptions];
2156+
if(gptGraphics->bComputeEncoderActive)
2157+
[ptDevice->tComputeEncoder barrierAfterStages:tSrcStage beforeQueueStages:tDstStage visibilityOptions:tOptions];
2158+
else if(gptGraphics->bRenderEncoderActive)
2159+
[ptDevice->tRenderEncoder barrierAfterStages:tSrcStage beforeQueueStages:tDstStage visibilityOptions:tOptions];
21082160
}
21092161

21102162
void
@@ -3053,7 +3105,7 @@
30533105
for(uint32_t i = 0; i < pl_sb_size(ptGarbage->sbtTextures); i++)
30543106
{
30553107
const uint16_t uTextureIndex = ptGarbage->sbtTextures[i].uIndex;
3056-
if( uSignaledValue > ptDevice->sbtTexturesCold[uTextureIndex]._uFrameBoundaryValueForDeletion + 1)
3108+
if( uSignaledValue > ptDevice->sbtTexturesCold[uTextureIndex]._uFrameBoundaryValueForDeletion + 1 && !ptDevice->sbtTexturesHot[uTextureIndex].bSwapchain)
30573109
{
30583110
plMetalTexture* ptMetalTexture = &ptDevice->sbtTexturesHot[uTextureIndex];
30593111
[ptMetalTexture->tTexture release];

extensions/pl_graphics_vulkan.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4700,6 +4700,7 @@ pl_graphics_end_compute_pass(plCommandBuffer* ptCmdBuffer)
47004700
void
47014701
pl_graphics_intra_pass_barrier(plCommandBuffer* ptCmdBuffer, plPipelineStageFlags tSrcStages, plPipelineStageFlags tDstStages, plBarrierScope tScope)
47024702
{
4703+
#if 0
47034704
PL_ASSERT(gptGraphics->bEncoderActive);
47044705
PL_ASSERT(tSrcStages != 0);
47054706
PL_ASSERT(tDstStages != 0);
@@ -4881,6 +4882,7 @@ pl_graphics_intra_pass_barrier(plCommandBuffer* ptCmdBuffer, plPipelineStageFlag
48814882
};
48824883
vkCmdPipelineBarrier2(ptCmdBuffer->tCmdBuffer, &tDependencyInfo);
48834884
// vkCmdPipelineBarrier(ptCmdBuffer->tCmdBuffer, tSrcStageFlags, tDstStageFlags, 0, tScope == 0 ? 0 : 1, &tMemoryBarrier, 0, NULL, 0, NULL);
4885+
#endif
48844886
}
48854887

48864888
void

0 commit comments

Comments
 (0)