Skip to content

Commit

Permalink
WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
hoffstadt committed Jan 17, 2025
1 parent f8b1903 commit 2b2385d
Show file tree
Hide file tree
Showing 7 changed files with 81 additions and 25 deletions.
3 changes: 2 additions & 1 deletion extensions/pl_draw_ext.c
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ Index of this file:
#include "pl.h"
#define PL_MATH_INCLUDE_FUNCTIONS
#include "pl_draw_ext.h"
#include "pl_ds.h"
#include "pl_memory.h"
#include "pl_string.h"

Expand Down Expand Up @@ -140,6 +139,8 @@ static unsigned char* ptrDOut_ = NULL;
static const plFileI* gptFile = NULL;
#endif

#include "pl_ds.h"

//-----------------------------------------------------------------------------
// [SECTION] internal api
//-----------------------------------------------------------------------------
Expand Down
1 change: 1 addition & 0 deletions extensions/pl_graphics_ext.h
Original file line number Diff line number Diff line change
Expand Up @@ -634,6 +634,7 @@ typedef struct _plBindGroup

// [INTERNAL]
uint16_t _uGeneration;
plTextureHandle* _sbtTextures;
} plBindGroup;

//-------------------------------shaders---------------------------------------
Expand Down
3 changes: 2 additions & 1 deletion extensions/pl_graphics_internal.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
#define PL_GRAPHICS_INTERNAL_EXT_H

#include "pl.h"
#include "pl_ds.h"
#include "pl_log_ext.h"
#include "pl_threads_ext.h"
#include "pl_graphics_ext.h"
Expand Down Expand Up @@ -34,6 +33,8 @@
static plIO* gptIO = NULL;
#endif

#include "pl_ds.h"

//-----------------------------------------------------------------------------
// [SECTION] global data
//-----------------------------------------------------------------------------
Expand Down
89 changes: 68 additions & 21 deletions extensions/pl_graphics_metal.m
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,6 @@
plSamplerHandle atSamplerBindings[PL_MAX_TEXTURES_PER_BIND_GROUP];
uint64_t uHeapUsageMask;
uint32_t uOffset;
plTextureHandle* sbtTextures;
} plMetalBindGroup;

typedef struct _plMetalShader
Expand Down Expand Up @@ -549,6 +548,44 @@
}
}

void
pl_copy_texture(plBlitEncoder* ptEncoder, plTextureHandle tSrcHandle, plTextureHandle tDstHandle, uint32_t uRegionCount, const plImageCopy* ptRegions)
{
plCommandBuffer* ptCmdBuffer = ptEncoder->ptCommandBuffer;
plDevice* ptDevice = ptCmdBuffer->ptDevice;
const plMetalTexture* ptMetalSrcTexture = &ptDevice->sbtTexturesHot[tSrcHandle.uIndex];
const plMetalTexture* ptMetalDstTexture = &ptDevice->sbtTexturesHot[tDstHandle.uIndex];

for(uint32_t i = 0; i < uRegionCount; i++)
{

MTLOrigin tSrcOrigin;
tSrcOrigin.x = ptRegions[i].iSourceOffsetX;
tSrcOrigin.y = ptRegions[i].iSourceOffsetY;
tSrcOrigin.z = ptRegions[i].iSourceOffsetZ;

MTLOrigin tDestOrigin;
tDestOrigin.x = ptRegions[i].iDestinationOffsetX;
tDestOrigin.y = ptRegions[i].iDestinationOffsetY;
tDestOrigin.z = ptRegions[i].iDestinationOffsetZ;

MTLSize tSourceSize;
tSourceSize.width = ptRegions[i].uSourceExtentX;
tSourceSize.height = ptRegions[i].uSourceExtentY;
tSourceSize.depth = ptRegions[i].uSourceExtentZ;

[ptEncoder->tEncoder copyFromTexture:ptMetalSrcTexture->tTexture
sourceSlice:ptRegions[i].uSourceBaseArrayLayer
sourceLevel:ptRegions[i].uSourceMipLevel
sourceOrigin:tSrcOrigin
sourceSize:tSourceSize
toTexture:ptMetalDstTexture->tTexture
destinationSlice:ptRegions[i].uDestinationBaseArrayLayer
destinationLevel:ptRegions[i].uDestinationMipLevel
destinationOrigin:tDestOrigin];
}
}

static void
pl_copy_texture_to_buffer(plBlitEncoder* ptEncoder, plTextureHandle tTextureHandle, plBufferHandle tBufferHandle, uint32_t uRegionCount, const plBufferImageCopy* ptRegions)
{
Expand Down Expand Up @@ -900,6 +937,8 @@
plBindGroup* ptBindGroup = pl__get_bind_group(ptDevice, tHandle);
const char* pcDescriptorStart = ptMetalBindGroup->tShaderArgumentBuffer.contents;

pl_sb_reset(ptBindGroup->_sbtTextures);

uint64_t* pulDescriptorStart = (uint64_t*)&pcDescriptorStart[ptMetalBindGroup->uOffset];

for(uint32_t i = 0; i < ptData->uBufferCount; i++)
Expand All @@ -922,7 +961,7 @@
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);
pl_sb_push(ptBindGroup->_sbtTextures, ptUpdate->tTexture);
}

ptMetalBindGroup->uHeapUsageMask |= (1ULL << ptMetalTexture->uHeap);
Expand Down Expand Up @@ -2167,9 +2206,10 @@

for(uint32_t i = 0; i < uCount; i++)
{
plMetalBindGroup* ptBindGroup = &ptDevice->sbtBindGroupsHot[atBindGroups[i].uIndex];
plBindGroup* ptBindGroup = &ptDevice->sbtBindGroupsCold[atBindGroups[i].uIndex];
plMetalBindGroup* ptMetalBindGroup = &ptDevice->sbtBindGroupsHot[atBindGroups[i].uIndex];

ptEncoder->uHeapUsageMask |= ptBindGroup->uHeapUsageMask;
ptEncoder->uHeapUsageMask |= ptMetalBindGroup->uHeapUsageMask;

for(uint64_t k = 0; k < 64; k++)
{
Expand All @@ -2179,10 +2219,10 @@
}
}

const uint32_t uTextureCount = pl_sb_size(ptBindGroup->sbtTextures);
const uint32_t uTextureCount = pl_sb_size(ptBindGroup->_sbtTextures);
for(uint32_t k = 0; k < uTextureCount; k++)
{
const plTextureHandle tTextureHandle = ptBindGroup->sbtTextures[k];
const plTextureHandle tTextureHandle = ptBindGroup->_sbtTextures[k];
plTexture* ptTexture = pl__get_texture(ptDevice, tTextureHandle);
if(ptTexture->tDesc.tUsage & PL_TEXTURE_USAGE_STORAGE)
{
Expand All @@ -2194,8 +2234,8 @@
}
}

[ptEncoder->tEncoder setBuffer:ptBindGroup->tShaderArgumentBuffer
offset:ptBindGroup->uOffset
[ptEncoder->tEncoder setBuffer:ptMetalBindGroup->tShaderArgumentBuffer
offset:ptMetalBindGroup->uOffset
atIndex:uFirst + i];
}
}
Expand All @@ -2215,9 +2255,10 @@

for(uint32_t i = 0; i < uCount; i++)
{
plMetalBindGroup* ptBindGroup = &ptDevice->sbtBindGroupsHot[atBindGroups[i].uIndex];
plBindGroup* ptBindGroup = &ptDevice->sbtBindGroupsCold[atBindGroups[i].uIndex];
plMetalBindGroup* ptMetalBindGroup = &ptDevice->sbtBindGroupsHot[atBindGroups[i].uIndex];

ptEncoder->uHeapUsageMask |= ptBindGroup->uHeapUsageMask;
ptEncoder->uHeapUsageMask |= ptMetalBindGroup->uHeapUsageMask;

for(uint64_t k = 0; k < 64; k++)
{
Expand All @@ -2227,16 +2268,16 @@
}
}

const uint32_t uTextureCount = pl_sb_size(ptBindGroup->sbtTextures);
const uint32_t uTextureCount = pl_sb_size(ptBindGroup->_sbtTextures);
for(uint32_t k = 0; k < uTextureCount; k++)
{
const plTextureHandle tTextureHandle = ptBindGroup->sbtTextures[k];
const plTextureHandle tTextureHandle = ptBindGroup->_sbtTextures[k];
plTexture* ptTexture = pl__get_texture(ptDevice, tTextureHandle);
[ptEncoder->tEncoder useResource:ptDevice->sbtTexturesHot[tTextureHandle.uIndex].tTexture usage:MTLResourceUsageRead stages:MTLRenderStageVertex | MTLRenderStageFragment];
}

[ptEncoder->tEncoder setVertexBuffer:ptBindGroup->tShaderArgumentBuffer offset:ptBindGroup->uOffset atIndex:uFirst + i];
[ptEncoder->tEncoder setFragmentBuffer:ptBindGroup->tShaderArgumentBuffer offset:ptBindGroup->uOffset atIndex:uFirst + i];
[ptEncoder->tEncoder setVertexBuffer:ptMetalBindGroup->tShaderArgumentBuffer offset:ptMetalBindGroup->uOffset atIndex:uFirst + i];
[ptEncoder->tEncoder setFragmentBuffer:ptMetalBindGroup->tShaderArgumentBuffer offset:ptMetalBindGroup->uOffset atIndex:uFirst + i];
}
}

Expand Down Expand Up @@ -2438,6 +2479,7 @@
if(uDirtyMask & PL_DRAW_STREAM_BIT_BINDGROUP_0)
{
const plBindGroupHandle tBindGroupHandle = {.uData = ptStream->_auStream[uCurrentStreamIndex] };
plBindGroup* ptBindGroup = &ptDevice->sbtBindGroupsCold[tBindGroupHandle.uIndex];
plMetalBindGroup* ptMetalBindGroup = &ptDevice->sbtBindGroupsHot[tBindGroupHandle.uIndex];

ptEncoder->uHeapUsageMask |= ptMetalBindGroup->uHeapUsageMask;
Expand All @@ -2451,10 +2493,10 @@
}
}

const uint32_t uTextureCount = pl_sb_size(ptMetalBindGroup->sbtTextures);
const uint32_t uTextureCount = pl_sb_size(ptBindGroup->_sbtTextures);
for(uint32_t k = 0; k < uTextureCount; k++)
{
const plTextureHandle tTextureHandle = ptMetalBindGroup->sbtTextures[k];
const plTextureHandle tTextureHandle = ptBindGroup->_sbtTextures[k];
plTexture* ptTexture = pl__get_texture(ptDevice, tTextureHandle);
[ptEncoder->tEncoder useResource:ptDevice->sbtTexturesHot[tTextureHandle.uIndex].tTexture usage:MTLResourceUsageRead stages:MTLRenderStageVertex | MTLRenderStageFragment];
}
Expand All @@ -2468,6 +2510,7 @@
{
const plBindGroupHandle tBindGroupHandle = {.uData = ptStream->_auStream[uCurrentStreamIndex] };
plMetalBindGroup* ptMetalBindGroup = &ptDevice->sbtBindGroupsHot[tBindGroupHandle.uIndex];
plBindGroup* ptBindGroup = &ptDevice->sbtBindGroupsCold[tBindGroupHandle.uIndex];

ptEncoder->uHeapUsageMask |= ptMetalBindGroup->uHeapUsageMask;

Expand All @@ -2480,10 +2523,10 @@
}
}

const uint32_t uTextureCount = pl_sb_size(ptMetalBindGroup->sbtTextures);
const uint32_t uTextureCount = pl_sb_size(ptBindGroup->_sbtTextures);
for(uint32_t k = 0; k < uTextureCount; k++)
{
const plTextureHandle tTextureHandle = ptMetalBindGroup->sbtTextures[k];
const plTextureHandle tTextureHandle = ptBindGroup->_sbtTextures[k];
plTexture* ptTexture = pl__get_texture(ptDevice, tTextureHandle);
[ptEncoder->tEncoder useResource:ptDevice->sbtTexturesHot[tTextureHandle.uIndex].tTexture usage:MTLResourceUsageRead stages:MTLRenderStageVertex | MTLRenderStageFragment];
}
Expand All @@ -2497,6 +2540,7 @@
{
const plBindGroupHandle tBindGroupHandle = {.uData = ptStream->_auStream[uCurrentStreamIndex] };
plMetalBindGroup* ptMetalBindGroup = &ptDevice->sbtBindGroupsHot[tBindGroupHandle.uIndex];
plBindGroup* ptBindGroup = &ptDevice->sbtBindGroupsCold[tBindGroupHandle.uIndex];

ptEncoder->uHeapUsageMask |= ptMetalBindGroup->uHeapUsageMask;

Expand All @@ -2509,10 +2553,10 @@
}
}

const uint32_t uTextureCount = pl_sb_size(ptMetalBindGroup->sbtTextures);
const uint32_t uTextureCount = pl_sb_size(ptBindGroup->_sbtTextures);
for(uint32_t k = 0; k < uTextureCount; k++)
{
const plTextureHandle tTextureHandle = ptMetalBindGroup->sbtTextures[k];
const plTextureHandle tTextureHandle = ptBindGroup->_sbtTextures[k];
[ptEncoder->tEncoder useResource:ptDevice->sbtTexturesHot[tTextureHandle.uIndex].tTexture usage:MTLResourceUsageRead stages:MTLRenderStageVertex | MTLRenderStageFragment];
}

Expand Down Expand Up @@ -3143,8 +3187,9 @@
for(uint32_t i = 0; i < pl_sb_size(ptGarbage->sbtBindGroups); i++)
{
const uint16_t iBindGroupIndex = ptGarbage->sbtBindGroups[i].uIndex;
plBindGroup* ptResource = &ptDevice->sbtBindGroupsCold[iBindGroupIndex];
plMetalBindGroup* ptMetalResource = &ptDevice->sbtBindGroupsHot[iBindGroupIndex];
pl_sb_reset(ptMetalResource->sbtTextures);
pl_sb_reset(ptResource->_sbtTextures);
[ptMetalResource->tShaderArgumentBuffer release];
ptMetalResource->tShaderArgumentBuffer = nil;
pl_sb_push(ptDevice->sbtBindGroupFreeIndices, iBindGroupIndex);
Expand Down Expand Up @@ -3319,6 +3364,8 @@
pl_sb_push(ptDevice->sbtBindGroupFreeIndices, tHandle.uIndex);

plMetalBindGroup* ptMetalResource = &ptDevice->sbtBindGroupsHot[tHandle.uIndex];
plBindGroup* ptResource = &ptDevice->sbtBindGroupsCold[tHandle.uIndex];
pl_sb_free(ptResource->_sbtTextures);
[ptMetalResource->tShaderArgumentBuffer release];
ptMetalResource->tShaderArgumentBuffer = nil;
}
Expand Down
1 change: 0 additions & 1 deletion extensions/pl_graphics_vulkan.c
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ Index of this file:
//-----------------------------------------------------------------------------

#include "pl.h"
#include "pl_ds.h"
#include "pl_string.h"
#include "pl_memory.h"
#include "pl_graphics_internal.h"
Expand Down
6 changes: 6 additions & 0 deletions extensions/pl_shader_ext.c
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,12 @@ Index of this file:
#define PL_REALLOC(x, y) gptMemory->tracked_realloc((x), (y), __FILE__, __LINE__)
#define PL_FREE(x) gptMemory->tracked_realloc((x), 0, __FILE__, __LINE__)

#ifndef PL_DS_ALLOC
#define PL_DS_ALLOC(x) gptMemory->tracked_realloc(NULL, (x), __FILE__, __LINE__)
#define PL_DS_ALLOC_INDIRECT(x, FILE, LINE) gptMemory->tracked_realloc(NULL, (x), FILE, LINE)
#define PL_DS_FREE(x) gptMemory->tracked_realloc((x), 0, __FILE__, __LINE__)
#endif

static const plFileI* gptFile = NULL;
#endif

Expand Down
3 changes: 2 additions & 1 deletion extensions/pl_ui_internal.h
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,6 @@ Index of this file:

#define PL_MATH_INCLUDE_FUNCTIONS
#include "pl_math.h"
#include "pl_ds.h"

#ifdef PL_UNITY_BUILD
#include "pl_unity_ext.inc"
Expand All @@ -72,6 +71,8 @@ Index of this file:
static plIO* gptIO = NULL;
#endif

#include "pl_ds.h"

//-----------------------------------------------------------------------------
// [SECTION] helper macros
//-----------------------------------------------------------------------------
Expand Down

0 comments on commit 2b2385d

Please sign in to comment.