Skip to content

Commit ca5f9d5

Browse files
committed
WIP
1 parent 5e33cf0 commit ca5f9d5

3 files changed

Lines changed: 77 additions & 15 deletions

File tree

extensions/pl_dear_imgui_ext.cpp

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,18 @@ static const plShaderI* gptShader = nullptr;
4848
static const plStarterI* gptStarter = nullptr;
4949
static const plIOI* gptIO = nullptr;
5050

51+
#define PL_ALLOC(x) gptMemory->tracked_realloc(NULL, (x), __FILE__, __LINE__)
52+
#define PL_REALLOC(x, y) gptMemory->tracked_realloc((x), (y), __FILE__, __LINE__)
53+
#define PL_FREE(x) gptMemory->tracked_realloc((x), 0, __FILE__, __LINE__)
54+
55+
#ifndef PL_DS_ALLOC
56+
#define PL_DS_ALLOC(x) gptMemory->tracked_realloc(NULL, (x), __FILE__, __LINE__)
57+
#define PL_DS_ALLOC_INDIRECT(x, FILE, LINE) gptMemory->tracked_realloc(NULL, (x), FILE, LINE)
58+
#define PL_DS_FREE(x) gptMemory->tracked_realloc((x), 0, __FILE__, __LINE__)
59+
#endif
60+
61+
#include "pl_ds.h"
62+
5163
//-----------------------------------------------------------------------------
5264
// [SECTION] structs
5365
//-----------------------------------------------------------------------------
@@ -295,7 +307,7 @@ pl_dear_imgui_initialize(plImGuiGraphicsInitInfo* ptInfo)
295307
}
296308

297309
plBindGroupPoolDesc tDesc = {};
298-
tDesc.szSampledTextureBindings = 20;
310+
tDesc.szSampledTextureBindings = 5;
299311
tDesc.szSamplerBindings = 2;
300312
bd->ptBindGroupPool = gptGfx->create_bind_group_pool(bd->ptDevice, &tDesc);
301313

@@ -1062,7 +1074,7 @@ pl__dear_imgui_update_texture(ImTextureData* tex)
10621074
tUpload.tBufferImageCopy.iImageOffsetX = upload_x;
10631075
tUpload.tBufferImageCopy.iImageOffsetY = upload_y;
10641076
tUpload.tBufferImageCopy.szBufferOffset = upload_x * tex->BytesPerPixel + stage_pitch * upload_y;
1065-
tUpload.tBufferImageCopy.uBufferRowLength = stage_pitch;
1077+
tUpload.tBufferImageCopy.uBufferRowLength = tex->Width;
10661078
tUpload.szUploadSize = upload_size;
10671079
pl_sb_push(bd->sbtTextureUploads, tUpload);
10681080
tex->SetStatus(ImTextureStatus_OK);

extensions/pl_graphics_ext.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1207,8 +1207,8 @@ typedef struct _plBufferImageCopy
12071207
uint32_t uMipLevel;
12081208
uint32_t uBaseArrayLayer;
12091209
uint32_t uLayerCount;
1210-
uint32_t uBufferRowLength;
1211-
uint32_t uBufferImageHeight;
1210+
uint32_t uBufferRowLength; // texels
1211+
uint32_t uBufferImageHeight; // texels
12121212
} plBufferImageCopy;
12131213

12141214
typedef struct _plImageCopy

extensions/pl_graphics_metal.m

Lines changed: 61 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -61,14 +61,21 @@
6161
plCommandBuffer* ptCommandBufferFreeList;
6262
} plCommandPool;
6363

64+
typedef struct _plBindGroupFreeSlot
65+
{
66+
uint32_t uSize;
67+
uint32_t uOffset;
68+
} plBindGroupFreeSlot;
69+
6470
typedef struct _plBindGroupPool
6571
{
66-
plDevice* ptDevice;
67-
id<MTLHeap> tDescriptorHeap;
68-
size_t szHeapSize;
69-
plBindGroupPoolDesc tDesc;
70-
plMetalBuffer tArgumentBuffer;
71-
size_t szCurrentArgumentOffset;
72+
plDevice* ptDevice;
73+
id<MTLHeap> tDescriptorHeap;
74+
size_t szHeapSize;
75+
plBindGroupPoolDesc tDesc;
76+
plMetalBuffer tArgumentBuffer;
77+
size_t szCurrentArgumentOffset;
78+
plBindGroupFreeSlot* sbtFreeSlots;
7279
} plBindGroupPool;
7380

7481
typedef struct _plFrameContext
@@ -109,6 +116,7 @@
109116
uint32_t uOffset;
110117
uint32_t uSize;
111118
plTextureHandle tFirstTexture; // for use with imgui for now (temp)
119+
plBindGroupPool* ptPool;
112120
} plMetalBindGroup;
113121

114122
typedef struct _plMetalShader
@@ -285,8 +293,8 @@
285293
const NSUInteger uCopyHeight = ptRegion->uImageHeight ? ptRegion->uImageHeight : 1;
286294
const NSUInteger uCopyDepth = ptRegion->uImageDepth ? ptRegion->uImageDepth : 1;
287295

288-
const NSUInteger uBufferRowLength = ptRegion->uBufferRowLength ? ptRegion->uBufferRowLength : uCopyWidth * uFormatStride;
289-
const NSUInteger uBufferImageHeight = ptRegion->uBufferImageHeight ? ptRegion->uBufferImageHeight : uCopyHeight * uFormatStride;
296+
const NSUInteger uBufferRowLength = ptRegion->uBufferRowLength ? ptRegion->uBufferRowLength * uFormatStride : uCopyWidth * uFormatStride;
297+
const NSUInteger uBufferImageHeight = ptRegion->uBufferImageHeight ? ptRegion->uBufferImageHeight * uFormatStride : uCopyHeight * uFormatStride;
290298

291299
const NSUInteger uBytesPerRow = uBufferRowLength;
292300

@@ -766,14 +774,45 @@
766774

767775
plMetalBindGroup tMetalBindGroup = {
768776
.tLayout = *ptLayout,
777+
.ptPool = ptDesc->ptPool
769778
};
770779

771780
tMetalBindGroup.tShaderArgumentBuffer = ptDesc->ptPool->tArgumentBuffer.tBuffer;
772-
tMetalBindGroup.uOffset = ptDesc->ptPool->szCurrentArgumentOffset;
781+
782+
// get new
783+
if(ptDesc->ptPool->szCurrentArgumentOffset + argumentBufferLength <= ptDesc->ptPool->szHeapSize)
784+
{
785+
tMetalBindGroup.uOffset = ptDesc->ptPool->szCurrentArgumentOffset;
786+
PL_ASSERT(ptDesc->ptPool->szCurrentArgumentOffset + argumentBufferLength <= ptDesc->ptPool->szHeapSize);
787+
ptDesc->ptPool->szCurrentArgumentOffset += argumentBufferLength;
788+
}
789+
else // full so lets recycle
790+
{
791+
// check for free slot
792+
uint32_t uBestSlot = UINT32_MAX;
793+
uint32_t uSlotCount = pl_sb_size(ptDesc->ptPool->sbtFreeSlots);
794+
for(uint32_t i = 0; i < uSlotCount; i++)
795+
{
796+
if(ptDesc->ptPool->sbtFreeSlots[i].uSize == argumentBufferLength)
797+
{
798+
uBestSlot = i;
799+
break;
800+
}
801+
else if(ptDesc->ptPool->sbtFreeSlots[i].uSize > argumentBufferLength)
802+
{
803+
if(uBestSlot == UINT32_MAX)
804+
uBestSlot = i;
805+
else if(ptDesc->ptPool->sbtFreeSlots[i].uSize < ptDesc->ptPool->sbtFreeSlots[uBestSlot].uSize)
806+
uBestSlot = i;
807+
}
808+
}
809+
810+
PL_ASSERT(uBestSlot != UINT32_MAX);
811+
tMetalBindGroup.uOffset = ptDesc->ptPool->sbtFreeSlots[uBestSlot].uOffset;
812+
pl_sb_del_swap(ptDesc->ptPool->sbtFreeSlots, uBestSlot);
813+
}
773814
tMetalBindGroup.uSize = argumentBufferLength;
774815

775-
PL_ASSERT(ptDesc->ptPool->szCurrentArgumentOffset + argumentBufferLength <= ptDesc->ptPool->szHeapSize);
776-
ptDesc->ptPool->szCurrentArgumentOffset += argumentBufferLength;
777816
[tMetalBindGroup.tShaderArgumentBuffer retain];
778817
if(ptDesc->pcDebugName)
779818
tMetalBindGroup.tShaderArgumentBuffer.label = [NSString stringWithUTF8String:ptDesc->pcDebugName];
@@ -897,6 +936,7 @@
897936
void
898937
pl_graphics_cleanup_bind_group_pool(plBindGroupPool* ptPool)
899938
{
939+
pl_sb_free(ptPool->sbtFreeSlots);
900940
PL_FREE(ptPool);
901941
}
902942

@@ -3096,6 +3136,11 @@
30963136
const uint16_t iBindGroupIndex = ptGarbage->sbtBindGroups[i].uIndex;
30973137
plBindGroup* ptResource = &ptDevice->sbtBindGroupsCold[iBindGroupIndex];
30983138
plMetalBindGroup* ptMetalResource = &ptDevice->sbtBindGroupsHot[iBindGroupIndex];
3139+
plBindGroupFreeSlot tFreeSlot = {
3140+
.uSize = ptMetalResource->uSize,
3141+
.uOffset = ptMetalResource->uOffset
3142+
};
3143+
pl_sb_push(ptMetalResource->ptPool->sbtFreeSlots, tFreeSlot);
30993144
[ptMetalResource->tShaderArgumentBuffer release];
31003145
ptMetalResource->tShaderArgumentBuffer = nil;
31013146
pl_sb_push(ptDevice->sbtBindGroupFreeIndices, iBindGroupIndex);
@@ -3322,6 +3367,11 @@
33223367
pl_sb_push(ptDevice->sbtBindGroupFreeIndices, tHandle.uIndex);
33233368

33243369
plMetalBindGroup* ptMetalResource = &ptDevice->sbtBindGroupsHot[tHandle.uIndex];
3370+
plBindGroupFreeSlot tFreeSlot = {
3371+
.uSize = ptMetalResource->uSize,
3372+
.uOffset = ptMetalResource->uOffset
3373+
};
3374+
pl_sb_push(ptMetalResource->ptPool->sbtFreeSlots, tFreeSlot);
33253375
plBindGroup* ptResource = &ptDevice->sbtBindGroupsCold[tHandle.uIndex];
33263376
[ptMetalResource->tShaderArgumentBuffer release];
33273377
ptMetalResource->tShaderArgumentBuffer = nil;

0 commit comments

Comments
 (0)