Skip to content

Commit

Permalink
WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
hoffstadt committed Dec 16, 2024
1 parent e65ada9 commit c0889ea
Show file tree
Hide file tree
Showing 15 changed files with 259 additions and 109 deletions.
2 changes: 1 addition & 1 deletion examples/example_2.c
Original file line number Diff line number Diff line change
Expand Up @@ -440,7 +440,7 @@ pl_app_update(plAppData* ptAppData)
gptGfx->begin_command_recording(ptCommandBuffer, &tBeginInfo);

// begin main renderpass (directly to swapchain)
plRenderEncoder* ptEncoder = gptGfx->begin_render_pass(ptCommandBuffer, ptAppData->tMainRenderPass);
plRenderEncoder* ptEncoder = gptGfx->begin_render_pass(ptCommandBuffer, ptAppData->tMainRenderPass, NULL);

// submit drawlists
plIO* ptIO = gptIO->get_io();
Expand Down
2 changes: 1 addition & 1 deletion examples/example_3.c
Original file line number Diff line number Diff line change
Expand Up @@ -524,7 +524,7 @@ pl_app_update(plAppData* ptAppData)
gptGfx->begin_command_recording(ptCommandBuffer, &tBeginInfo);

// begin main renderpass (directly to swapchain)
plRenderEncoder* ptEncoder = gptGfx->begin_render_pass(ptCommandBuffer, ptAppData->tMainRenderPass);
plRenderEncoder* ptEncoder = gptGfx->begin_render_pass(ptCommandBuffer, ptAppData->tMainRenderPass, NULL);

// submits UI drawlist/layers
plIO* ptIO = gptIO->get_io();
Expand Down
2 changes: 1 addition & 1 deletion examples/example_4.c
Original file line number Diff line number Diff line change
Expand Up @@ -400,7 +400,7 @@ pl_app_update(plAppData* ptAppData)
gptGfx->begin_command_recording(ptCommandBuffer, &tBeginInfo);

// begin main renderpass (directly to swapchain)
plRenderEncoder* ptEncoder = gptGfx->begin_render_pass(ptCommandBuffer, ptAppData->tMainRenderPass);
plRenderEncoder* ptEncoder = gptGfx->begin_render_pass(ptCommandBuffer, ptAppData->tMainRenderPass, NULL);

// submit nonindexed draw using basic API
gptGfx->bind_shader(ptEncoder, ptAppData->tShader);
Expand Down
2 changes: 1 addition & 1 deletion examples/example_5.c
Original file line number Diff line number Diff line change
Expand Up @@ -476,7 +476,7 @@ pl_app_update(plAppData* ptAppData)
gptGfx->begin_command_recording(ptCommandBuffer, &tBeginInfo);

// begin main renderpass (directly to swapchain)
plRenderEncoder* ptEncoder = gptGfx->begin_render_pass(ptCommandBuffer, ptAppData->tMainRenderPass);
plRenderEncoder* ptEncoder = gptGfx->begin_render_pass(ptCommandBuffer, ptAppData->tMainRenderPass, NULL);

// submit nonindexed draw using basic API
gptGfx->bind_shader(ptEncoder, ptAppData->tShader);
Expand Down
2 changes: 1 addition & 1 deletion examples/example_6.c
Original file line number Diff line number Diff line change
Expand Up @@ -628,7 +628,7 @@ pl_app_update(plAppData* ptAppData)
gptGfx->begin_command_recording(ptCommandBuffer, &tBeginInfo);

// begin main renderpass (directly to swapchain)
plRenderEncoder* ptEncoder = gptGfx->begin_render_pass(ptCommandBuffer, ptAppData->tMainRenderPass);
plRenderEncoder* ptEncoder = gptGfx->begin_render_pass(ptCommandBuffer, ptAppData->tMainRenderPass, NULL);

// submit nonindexed draw using basic API
gptGfx->bind_shader(ptEncoder, ptAppData->tShader);
Expand Down
2 changes: 1 addition & 1 deletion examples/example_8.c
Original file line number Diff line number Diff line change
Expand Up @@ -561,7 +561,7 @@ pl_app_update(plAppData* ptAppData)
gptGfx->begin_command_recording(ptCommandBuffer, &tBeginInfo0);

// begin offscreen renderpass
plRenderEncoder* ptEncoder0 = gptGfx->begin_render_pass(ptCommandBuffer, ptAppData->tMainRenderPass);
plRenderEncoder* ptEncoder0 = gptGfx->begin_render_pass(ptCommandBuffer, ptAppData->tMainRenderPass, NULL);

const plMat4 tMVP = pl_mul_mat4(&ptCamera->tProjMat, &ptCamera->tViewMat);
gptDrawBackend->submit_3d_drawlist(ptAppData->pt3dDrawlist,
Expand Down
4 changes: 2 additions & 2 deletions examples/example_9.c
Original file line number Diff line number Diff line change
Expand Up @@ -625,7 +625,7 @@ pl_app_update(plAppData* ptAppData)
gptGfx->begin_command_recording(ptCommandBuffer, &tBeginInfo0);

// begin offscreen renderpass
plRenderEncoder* ptEncoder0 = gptGfx->begin_render_pass(ptCommandBuffer, ptAppData->tOffscreenRenderPass);
plRenderEncoder* ptEncoder0 = gptGfx->begin_render_pass(ptCommandBuffer, ptAppData->tOffscreenRenderPass, NULL);

const plMat4 tMVP = pl_mul_mat4(&ptCamera->tProjMat, &ptCamera->tViewMat);
gptDrawBackend->submit_3d_drawlist(ptAppData->pt3dDrawlist,
Expand Down Expand Up @@ -659,7 +659,7 @@ pl_app_update(plAppData* ptAppData)
gptGfx->begin_command_recording(ptCommandBuffer, &tBeginInfo1);

// begin main renderpass (directly to swapchain)
plRenderEncoder* ptEncoder1 = gptGfx->begin_render_pass(ptCommandBuffer, ptAppData->tMainRenderPass);
plRenderEncoder* ptEncoder1 = gptGfx->begin_render_pass(ptCommandBuffer, ptAppData->tMainRenderPass, NULL);

// submit drawlists
gptDrawBackend->submit_2d_drawlist(ptAppData->ptAppDrawlist, ptEncoder1, ptIO->tMainViewportSize.x, ptIO->tMainViewportSize.y, 1);
Expand Down
1 change: 1 addition & 0 deletions extensions/pl_graphics_ext.c
Original file line number Diff line number Diff line change
Expand Up @@ -770,6 +770,7 @@ pl_load_graphics_ext(plApiRegistryI* ptApiRegistry, bool bReload)
.cleanup_bind_group_pool = pl_cleanup_bind_group_pool,
.reset_bind_group_pool = pl_reset_bind_group_pool,
.pipeline_barrier_blit = pl_pipeline_barrier_blit,
.pipeline_barrier_render = pl_pipeline_barrier_render,
.pipeline_barrier_compute = pl_pipeline_barrier_compute
};
pl_set_api(ptApiRegistry, plGraphicsI, &tApi);
Expand Down
5 changes: 3 additions & 2 deletions extensions/pl_graphics_ext.h
Original file line number Diff line number Diff line change
Expand Up @@ -259,8 +259,8 @@ typedef struct _plGraphicsI
void (*submit_command_buffer) (plCommandBuffer*, const plSubmitInfo*);

// render encoder
plRenderEncoder* (*begin_render_pass) (plCommandBuffer*, plRenderPassHandle); // do not store
void (*next_subpass) (plRenderEncoder*);
plRenderEncoder* (*begin_render_pass) (plCommandBuffer*, plRenderPassHandle, const plPassResources*); // do not store
void (*next_subpass) (plRenderEncoder*, const plPassResources*);
void (*end_render_pass) (plRenderEncoder*);
plRenderPassHandle (*get_encoder_render_pass) (plRenderEncoder*);
uint32_t (*get_render_encoder_subpass)(plRenderEncoder*);
Expand Down Expand Up @@ -304,6 +304,7 @@ typedef struct _plGraphicsI
// global barriers
void (*pipeline_barrier_blit) (plBlitEncoder*, plStageFlags beforeStages, plAccessFlags beforeAccesses, plStageFlags afterStages, plAccessFlags afterAccesses);
void (*pipeline_barrier_compute)(plComputeEncoder*, plStageFlags beforeStages, plAccessFlags beforeAccesses, plStageFlags afterStages, plAccessFlags afterAccesses);
void (*pipeline_barrier_render) (plRenderEncoder*, plStageFlags beforeStages, plAccessFlags beforeAccesses, plStageFlags afterStages, plAccessFlags afterAccesses);

//-----------------------------------------------------------------------------

Expand Down
163 changes: 143 additions & 20 deletions extensions/pl_graphics_metal.m
Original file line number Diff line number Diff line change
Expand Up @@ -122,10 +122,10 @@

typedef struct _plMetalTexture
{
id<MTLTexture> tTexture;
uint64_t uHeap;
id<MTLTexture> tTexture;
uint64_t uHeap;
MTLTextureDescriptor* ptTextureDescriptor;
bool bOriginalView;
bool bOriginalView;
} plMetalTexture;

typedef struct _plMetalSampler
Expand All @@ -135,20 +135,20 @@

typedef struct _plTimelineSemaphore
{
plDevice* ptDevice;
id<MTLEvent> tEvent;
id<MTLSharedEvent> tSharedEvent;
plDevice* ptDevice;
id<MTLEvent> tEvent;
id<MTLSharedEvent> tSharedEvent;
plTimelineSemaphore* ptNext;
} plTimelineSemaphore;

typedef struct _plMetalBindGroup
{
id<MTLBuffer> tShaderArgumentBuffer;
id<MTLBuffer> tShaderArgumentBuffer;
plBindGroupLayout tLayout;
plSamplerHandle atSamplerBindings[PL_MAX_TEXTURES_PER_BIND_GROUP];
uint64_t uHeapUsageMask;
uint32_t uOffset;
plTextureHandle* sbtTextures;
plSamplerHandle atSamplerBindings[PL_MAX_TEXTURES_PER_BIND_GROUP];
uint64_t uHeapUsageMask;
uint32_t uOffset;
plTextureHandle* sbtTextures;
} plMetalBindGroup;

typedef struct _plMetalShader
Expand Down Expand Up @@ -918,7 +918,12 @@
plMetalTexture* ptMetalTexture = &ptDevice->sbtTexturesHot[ptUpdate->tTexture.uIndex];
MTLResourceID* pptDestination = (MTLResourceID*)&pulDescriptorStart[ptUpdate->uSlot + ptUpdate->uIndex];
*pptDestination = ptMetalTexture->tTexture.gpuResourceID;
pl_sb_push(ptMetalBindGroup->sbtTextures, ptUpdate->tTexture);

int iCommonBits = ptTexture->tDesc.tUsage & (PL_TEXTURE_USAGE_DEPTH_STENCIL_ATTACHMENT | PL_TEXTURE_USAGE_COLOR_ATTACHMENT |PL_TEXTURE_USAGE_INPUT_ATTACHMENT | PL_TEXTURE_USAGE_STORAGE);
if(iCommonBits)
{
pl_sb_push(ptMetalBindGroup->sbtTextures, ptUpdate->tTexture);
}

ptMetalBindGroup->uHeapUsageMask |= (1ULL << ptMetalTexture->uHeap);
}
Expand Down Expand Up @@ -1819,7 +1824,7 @@
}

static void
pl_next_subpass(plRenderEncoder* ptEncoder)
pl_next_subpass(plRenderEncoder* ptEncoder, const plPassResources* ptResources)
{
plCommandBuffer* ptCmdBuffer = ptEncoder->ptCommandBuffer;
plDevice* ptDevice = ptCmdBuffer->ptDevice;
Expand All @@ -1835,15 +1840,109 @@
tNewRenderEncoder.label = @"subpass encoder";
[tNewRenderEncoder waitForFence:ptMetalRenderPass->tFence beforeStages:MTLRenderStageFragment | MTLRenderStageVertex];
ptEncoder->tEncoder = tNewRenderEncoder;

if(ptResources)
{
for(uint32_t i = 0; i < ptResources->uBufferCount; i++)
{
const plPassBufferResource* ptResource = &ptResources->atBuffers[i];
const plBuffer* ptBuffer = &ptCmdBuffer->ptDevice->sbtBuffersCold[ptResource->tHandle.uIndex];
const plMetalBuffer* ptMetalBuffer = &ptCmdBuffer->ptDevice->sbtBuffersHot[ptResource->tHandle.uIndex];

MTLResourceUsage tUsage = 0;
if(ptResource->tUsage & PL_PASS_RESOURCE_USAGE_READ)
tUsage |= MTLResourceUsageRead;
if(ptResource->tUsage & PL_PASS_RESOURCE_USAGE_WRITE)
tUsage |= MTLResourceUsageWrite;

if(!(ptEncoder->uHeapUsageMask & (1ULL << ptBuffer->tMemoryAllocation.uHandle)))
{
[ptEncoder->tEncoder useHeap:ptCmdBuffer->ptDevice->atHeaps[ptBuffer->tMemoryAllocation.uHandle] stages:MTLRenderStageVertex | MTLRenderStageFragment];
}

ptEncoder->uHeapUsageMask |= (1ULL << ptBuffer->tMemoryAllocation.uHandle);

[ptEncoder->tEncoder useResource:ptMetalBuffer->tBuffer usage:tUsage stages:pl__metal_stage_flags(ptResource->tStages)];
}

for(uint32_t i = 0; i < ptResources->uTextureCount; i++)
{
const plPassTextureResource* ptResource = &ptResources->atTextures[i];
const plTexture* ptTexture = &ptCmdBuffer->ptDevice->sbtTexturesCold[ptResource->tHandle.uIndex];
const plMetalTexture* ptMetalTexture = &ptCmdBuffer->ptDevice->sbtTexturesHot[ptResource->tHandle.uIndex];

MTLResourceUsage tUsage = 0;
if(ptResource->tUsage & PL_PASS_RESOURCE_USAGE_READ)
tUsage |= MTLResourceUsageRead;
if(ptResource->tUsage & PL_PASS_RESOURCE_USAGE_WRITE)
tUsage |= MTLResourceUsageWrite;

if(!(ptEncoder->uHeapUsageMask & (1ULL << ptTexture->tMemoryAllocation.uHandle)))
{
[ptEncoder->tEncoder useHeap:ptCmdBuffer->ptDevice->atHeaps[ptTexture->tMemoryAllocation.uHandle] stages:MTLRenderStageVertex | MTLRenderStageFragment];
}

ptEncoder->uHeapUsageMask |= (1ULL << ptTexture->tMemoryAllocation.uHandle);

[ptEncoder->tEncoder useResource:ptMetalTexture->tTexture usage:tUsage];
}
}
}

static plRenderEncoder*
pl_begin_render_pass(plCommandBuffer* ptCmdBuffer, plRenderPassHandle tPass)
pl_begin_render_pass(plCommandBuffer* ptCmdBuffer, plRenderPassHandle tPass, const plPassResources* ptResources)
{
plDevice* ptDevice = ptCmdBuffer->ptDevice;
plRenderEncoder* ptEncoder = pl__get_new_render_encoder();
ptEncoder->uHeapUsageMask = 0;

if(ptResources)
{
for(uint32_t i = 0; i < ptResources->uBufferCount; i++)
{
const plPassBufferResource* ptResource = &ptResources->atBuffers[i];
const plBuffer* ptBuffer = &ptCmdBuffer->ptDevice->sbtBuffersCold[ptResource->tHandle.uIndex];
const plMetalBuffer* ptMetalBuffer = &ptCmdBuffer->ptDevice->sbtBuffersHot[ptResource->tHandle.uIndex];

MTLResourceUsage tUsage = 0;
if(ptResource->tUsage & PL_PASS_RESOURCE_USAGE_READ)
tUsage |= MTLResourceUsageRead;
if(ptResource->tUsage & PL_PASS_RESOURCE_USAGE_WRITE)
tUsage |= MTLResourceUsageWrite;

if(!(ptEncoder->uHeapUsageMask & (1ULL << ptBuffer->tMemoryAllocation.uHandle)))
{
[ptEncoder->tEncoder useHeap:ptCmdBuffer->ptDevice->atHeaps[ptBuffer->tMemoryAllocation.uHandle] stages:MTLRenderStageVertex | MTLRenderStageFragment];
}

ptEncoder->uHeapUsageMask |= (1ULL << ptBuffer->tMemoryAllocation.uHandle);

[ptEncoder->tEncoder useResource:ptMetalBuffer->tBuffer usage:tUsage stages:pl__metal_stage_flags(ptResource->tStages)];
}

for(uint32_t i = 0; i < ptResources->uTextureCount; i++)
{
const plPassTextureResource* ptResource = &ptResources->atTextures[i];
const plTexture* ptTexture = &ptCmdBuffer->ptDevice->sbtTexturesCold[ptResource->tHandle.uIndex];
const plMetalTexture* ptMetalTexture = &ptCmdBuffer->ptDevice->sbtTexturesHot[ptResource->tHandle.uIndex];

MTLResourceUsage tUsage = 0;
if(ptResource->tUsage & PL_PASS_RESOURCE_USAGE_READ)
tUsage |= MTLResourceUsageRead;
if(ptResource->tUsage & PL_PASS_RESOURCE_USAGE_WRITE)
tUsage |= MTLResourceUsageWrite;

if(!(ptEncoder->uHeapUsageMask & (1ULL << ptTexture->tMemoryAllocation.uHandle)))
{
[ptEncoder->tEncoder useHeap:ptCmdBuffer->ptDevice->atHeaps[ptTexture->tMemoryAllocation.uHandle] stages:MTLRenderStageVertex | MTLRenderStageFragment];
}

ptEncoder->uHeapUsageMask |= (1ULL << ptTexture->tMemoryAllocation.uHandle);

[ptEncoder->tEncoder useResource:ptMetalTexture->tTexture usage:tUsage stages:pl__metal_stage_flags(ptResource->tStages)];
}
}

plRenderPass* ptRenderPass = pl_get_render_pass(ptDevice, tPass);
plMetalRenderPass* ptMetalRenderPass = &ptDevice->sbtRenderPassesHot[tPass.uIndex];
plRenderPassLayout* ptLayout = pl_get_render_pass_layout(ptDevice, ptRenderPass->tDesc.tLayout);
Expand Down Expand Up @@ -1957,11 +2056,19 @@
void
pl_pipeline_barrier_blit(plBlitEncoder* ptEncoder, plStageFlags beforeStages, plAccessFlags beforeAccesses, plStageFlags afterStages, plAccessFlags afterAccesses)
{

}

void
pl_pipeline_barrier_compute(plComputeEncoder* ptEncoder, plStageFlags beforeStages, plAccessFlags beforeAccesses, plStageFlags afterStages, plAccessFlags afterAccesses)
{
[ptEncoder->tEncoder memoryBarrierWithScope:MTLBarrierScopeBuffers | MTLBarrierScopeTextures];
}

void
pl_pipeline_barrier_render(plRenderEncoder* ptEncoder, plStageFlags beforeStages, plAccessFlags beforeAccesses, plStageFlags afterStages, plAccessFlags afterAccesses)
{
[ptEncoder->tEncoder memoryBarrierWithScope:MTLBarrierScopeRenderTargets | MTLBarrierScopeBuffers | MTLBarrierScopeTextures afterStages:pl__metal_stage_flags(beforeStages) beforeStages:pl__metal_stage_flags(afterStages)];
}

static plComputeEncoder*
Expand Down Expand Up @@ -2070,11 +2177,19 @@
}
}

for(uint32_t k = 0; k < pl_sb_size(ptBindGroup->sbtTextures); k++)
const uint32_t uTextureCount = pl_sb_size(ptBindGroup->sbtTextures);
for(uint32_t k = 0; k < uTextureCount; k++)
{
const plTextureHandle tTextureHandle = ptBindGroup->sbtTextures[k];
plTexture* ptTexture = pl__get_texture(ptDevice, tTextureHandle);
[ptEncoder->tEncoder useResource:ptDevice->sbtTexturesHot[tTextureHandle.uIndex].tTexture usage:MTLResourceUsageRead | MTLResourceUsageWrite];
if(ptTexture->tDesc.tUsage & PL_TEXTURE_USAGE_STORAGE)
{
[ptEncoder->tEncoder useResource:ptDevice->sbtTexturesHot[tTextureHandle.uIndex].tTexture usage:MTLResourceUsageRead | MTLResourceUsageWrite];
}
else
{
[ptEncoder->tEncoder useResource:ptDevice->sbtTexturesHot[tTextureHandle.uIndex].tTexture usage:MTLResourceUsageRead];
}
}

[ptEncoder->tEncoder setBuffer:ptBindGroup->tShaderArgumentBuffer
Expand Down Expand Up @@ -2110,7 +2225,8 @@
}
}

for(uint32_t k = 0; k < pl_sb_size(ptBindGroup->sbtTextures); k++)
const uint32_t uTextureCount = pl_sb_size(ptBindGroup->sbtTextures);
for(uint32_t k = 0; k < uTextureCount; k++)
{
const plTextureHandle tTextureHandle = ptBindGroup->sbtTextures[k];
plTexture* ptTexture = pl__get_texture(ptDevice, tTextureHandle);
Expand Down Expand Up @@ -2305,6 +2421,7 @@

ptEncoder->uHeapUsageMask |= ptMetalBindGroup->uHeapUsageMask;

// TODO: optimize to not go through all 64
for(uint64_t k = 0; k < 64; k++)
{
if(ptEncoder->uHeapUsageMask & (1ULL << k))
Expand All @@ -2313,7 +2430,8 @@
}
}

for(uint32_t k = 0; k < pl_sb_size(ptMetalBindGroup->sbtTextures); k++)
const uint32_t uTextureCount = pl_sb_size(ptMetalBindGroup->sbtTextures);
for(uint32_t k = 0; k < uTextureCount; k++)
{
const plTextureHandle tTextureHandle = ptMetalBindGroup->sbtTextures[k];
plTexture* ptTexture = pl__get_texture(ptDevice, tTextureHandle);
Expand All @@ -2332,6 +2450,7 @@

ptEncoder->uHeapUsageMask |= ptMetalBindGroup->uHeapUsageMask;

// TODO: optimize to not go through all 64
for(uint64_t k = 0; k < 64; k++)
{
if(ptEncoder->uHeapUsageMask & (1ULL << k))
Expand All @@ -2340,7 +2459,8 @@
}
}

for(uint32_t k = 0; k < pl_sb_size(ptMetalBindGroup->sbtTextures); k++)
const uint32_t uTextureCount = pl_sb_size(ptMetalBindGroup->sbtTextures);
for(uint32_t k = 0; k < uTextureCount; k++)
{
const plTextureHandle tTextureHandle = ptMetalBindGroup->sbtTextures[k];
plTexture* ptTexture = pl__get_texture(ptDevice, tTextureHandle);
Expand All @@ -2359,6 +2479,7 @@

ptEncoder->uHeapUsageMask |= ptMetalBindGroup->uHeapUsageMask;

// TODO: optimize to not go through all 64
for(uint64_t k = 0; k < 64; k++)
{
if(ptEncoder->uHeapUsageMask & (1ULL << k))
Expand All @@ -2367,7 +2488,8 @@
}
}

for(uint32_t k = 0; k < pl_sb_size(ptMetalBindGroup->sbtTextures); k++)
const uint32_t uTextureCount = pl_sb_size(ptMetalBindGroup->sbtTextures);
for(uint32_t k = 0; k < uTextureCount; k++)
{
const plTextureHandle tTextureHandle = ptMetalBindGroup->sbtTextures[k];
[ptEncoder->tEncoder useResource:ptDevice->sbtTexturesHot[tTextureHandle.uIndex].tTexture usage:MTLResourceUsageRead stages:MTLRenderStageVertex | MTLRenderStageFragment];
Expand Down Expand Up @@ -3019,6 +3141,7 @@
for(uint32_t i = 0; i < pl_sb_size(ptGarbage->sbtTextures); i++)
{
const uint16_t uTextureIndex = ptGarbage->sbtTextures[i].uIndex;
printf("Delete: %u\n", uTextureIndex);
plMetalTexture* ptMetalTexture = &ptDevice->sbtTexturesHot[uTextureIndex];
[ptMetalTexture->tTexture release];
ptMetalTexture->tTexture = nil;
Expand Down
Loading

0 comments on commit c0889ea

Please sign in to comment.