Skip to content

Commit 5e33cf0

Browse files
committed
WIP
1 parent ffcf4db commit 5e33cf0

2 files changed

Lines changed: 75 additions & 71 deletions

File tree

extensions/pl_dear_imgui_ext.cpp

Lines changed: 60 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,6 @@ Index of this file:
3434
#include "pl_graphics_ext.h"
3535
#include "pl_shader_ext.h"
3636
#include "pl_starter_ext.h"
37-
#include "pl_stage_ext.h"
3837

3938
#include <float.h>
4039

@@ -47,7 +46,6 @@ static const plDataRegistryI* gptDataRegistry = nullptr;
4746
static const plMemoryI* gptMemory = nullptr;
4847
static const plShaderI* gptShader = nullptr;
4948
static const plStarterI* gptStarter = nullptr;
50-
static const plStageI* gptStage = nullptr;
5149
static const plIOI* gptIO = nullptr;
5250

5351
//-----------------------------------------------------------------------------
@@ -95,6 +93,14 @@ struct plImGuiViewportData
9593
~plImGuiViewportData() { }
9694
};
9795

96+
struct plImGuiTextureUpload
97+
{
98+
plBufferHandle tStagingBuffer;
99+
plBufferImageCopy tBufferImageCopy;
100+
plTextureHandle tTexture;
101+
size_t szUploadSize;
102+
};
103+
98104
struct plImGuiGraphicsData
99105
{
100106
// gfx stuff
@@ -116,6 +122,8 @@ struct plImGuiGraphicsData
116122
plBindGroupLayoutHandle tTextureDescriptorSetLayout;
117123
plBindGroupLayoutHandle tSamplerDescriptorSetLayout;
118124
plBindGroupHandle tSamplerDescriptorSet;
125+
126+
plImGuiTextureUpload* sbtTextureUploads;
119127

120128
// temporary dynamic block
121129
plDynamicDataBlock tDynamicDataBlock;
@@ -313,7 +321,6 @@ pl_dear_imgui_cleanup(void)
313321
gptGfx->cleanup_bind_group_pool(bd->ptBindGroupPool);
314322
gptGfx->cleanup_command_pool(bd->ptTextureCommandPool);
315323
}
316-
gptStage->cleanup();
317324

318325
// Only the main viewport has renderer data because multi-viewport is intentionally unsupported.
319326
ImGuiViewport* main_viewport = ImGui::GetMainViewport();
@@ -327,6 +334,7 @@ pl_dear_imgui_cleanup(void)
327334
ImGuiBackendFlags_RendererHasTextures |
328335
ImGuiBackendFlags_RendererHasViewports);
329336
platform_io.ClearRendererHandlers();
337+
pl_sb_free(bd->sbtTextureUploads);
330338
IM_DELETE(bd);
331339

332340
pl__dear_imgui_platform_cleanup();
@@ -338,11 +346,34 @@ pl_dear_imgui_cleanup(void)
338346
void
339347
pl_dear_imgui_new_frame(plDevice *ptDevice)
340348
{
341-
gptStage->flush();
342349

343350
plImGuiGraphicsData* bd = (plImGuiGraphicsData*)ImGui::GetIO().BackendRendererUserData;
344351
IM_ASSERT(bd != nullptr && "Dear ImGui renderer backend is not initialized!");
345352

353+
if(pl_sb_size(bd->sbtTextureUploads) > 0)
354+
{
355+
plCommandBuffer* ptCmdBuffer = gptGfx->request_command_buffer(bd->ptTextureCommandPool, "imgui texture upload");
356+
gptGfx->begin_command_recording(ptCmdBuffer);
357+
gptGfx->begin_compute_pass(ptCmdBuffer, NULL);
358+
for(uint32_t i = 0; i < pl_sb_size(bd->sbtTextureUploads); i++)
359+
{
360+
gptGfx->copy_buffer_to_texture(ptCmdBuffer, bd->sbtTextureUploads[i].tStagingBuffer,
361+
bd->sbtTextureUploads[i].tTexture, 1, &bd->sbtTextureUploads[i].tBufferImageCopy);
362+
}
363+
364+
gptGfx->end_compute_pass(ptCmdBuffer);
365+
gptGfx->end_command_recording(ptCmdBuffer);
366+
gptGfx->submit_command_buffer(ptCmdBuffer, nullptr);
367+
gptGfx->wait_on_command_buffer(ptCmdBuffer);
368+
gptGfx->return_command_buffer(ptCmdBuffer);
369+
370+
for(uint32_t i = 0; i < pl_sb_size(bd->sbtTextureUploads); i++)
371+
{
372+
gptStarter->return_staging_buffer(&bd->sbtTextureUploads[i].tStagingBuffer);
373+
}
374+
pl_sb_reset(bd->sbtTextureUploads);
375+
}
376+
346377
plDevice* ptFrameDevice = ptDevice ? ptDevice : bd->ptDevice;
347378
bd->tDynamicDataBlock = gptGfx->allocate_dynamic_data_block(ptFrameDevice);
348379

@@ -999,41 +1030,41 @@ pl__dear_imgui_update_texture(ImTextureData* tex)
9991030
{
10001031
plImGuiTexture* backend_tex = (plImGuiTexture*)tex->BackendUserData;
10011032

1033+
plImGuiTextureUpload tUpload = {};
1034+
tUpload.tTexture = backend_tex->tTexture;
1035+
10021036
// Update full texture or selected blocks. We only ever write to textures regions which have never been used before!
10031037
// This backend choose to use tex->UpdateRect but you can use tex->Updates[] to upload individual regions.
10041038
// We could use the smaller rect on _WantCreate but using the full rect allows us to clear the texture.
1005-
// const int upload_x = (tex->Status == ImTextureStatus_WantCreate) ? 0 : tex->UpdateRect.x;
1006-
// const int upload_y = (tex->Status == ImTextureStatus_WantCreate) ? 0 : tex->UpdateRect.y;
1007-
// const int upload_w = (tex->Status == ImTextureStatus_WantCreate) ? tex->Width : tex->UpdateRect.w;
1008-
// const int upload_h = (tex->Status == ImTextureStatus_WantCreate) ? tex->Height : tex->UpdateRect.h;
1009-
1010-
const int upload_x = 0;
1011-
const int upload_y = 0;
1012-
const int upload_w = tex->Width;
1013-
const int upload_h = tex->Height;
1039+
const int upload_x = (tex->Status == ImTextureStatus_WantCreate) ? 0 : tex->UpdateRect.x;
1040+
const int upload_y = (tex->Status == ImTextureStatus_WantCreate) ? 0 : tex->UpdateRect.y;
1041+
const int upload_w = (tex->Status == ImTextureStatus_WantCreate) ? tex->Width : tex->UpdateRect.w;
1042+
const int upload_h = (tex->Status == ImTextureStatus_WantCreate) ? tex->Height : tex->UpdateRect.h;
10141043

10151044
// Create the Upload Buffer:
1016-
static plBufferHandle tStagingBuffer = {};
10171045
int upload_pitch = upload_w * tex->BytesPerPixel;
10181046
int upload_size = upload_h * upload_pitch;
1019-
gptStarter->get_staging_buffer(upload_size, &tStagingBuffer, "imgui texture stage");
1047+
int stage_pitch = tex->Width * tex->BytesPerPixel;
1048+
1049+
// full size of texture
1050+
gptStarter->get_staging_buffer(tex->BytesPerPixel * tex->Width * tex->Height, &tUpload.tStagingBuffer, "imgui texture stage");
10201051

1021-
plBuffer* ptStagingBuffer = gptGfx->get_buffer(ptDevice, tStagingBuffer);
1022-
// for (int y = 0; y < upload_h; y++)
1023-
// memcpy(ptStagingBuffer->tMemoryAllocation.pHostMapped + upload_pitch * y, tex->GetPixelsAt(upload_x, upload_y + y), (size_t)upload_pitch);
1024-
memcpy(ptStagingBuffer->tMemoryAllocation.pHostMapped, tex->GetPixelsAt(upload_x, upload_y), (size_t)upload_size);
1052+
plBuffer* ptStagingBuffer = gptGfx->get_buffer(ptDevice, tUpload.tStagingBuffer);
1053+
char* pcStageLocation = ptStagingBuffer->tMemoryAllocation.pHostMapped + upload_x * tex->BytesPerPixel + stage_pitch * upload_y;
1054+
for (int y = 0; y < upload_h; y++)
1055+
memcpy(pcStageLocation + stage_pitch * y, tex->GetPixelsAt(upload_x, upload_y + y), (size_t)upload_pitch);
10251056

10261057
// copy buffer to image
1027-
plBufferImageCopy tBufferImageCopy = {};
1028-
tBufferImageCopy.uImageWidth = (uint32_t)upload_w;
1029-
tBufferImageCopy.uImageHeight = (uint32_t)upload_h;
1030-
tBufferImageCopy.uImageDepth = 1;
1031-
tBufferImageCopy.uLayerCount = 1;
1032-
tBufferImageCopy.iImageOffsetX = upload_x;
1033-
tBufferImageCopy.iImageOffsetY = upload_y;
1034-
gptStage->stage_texture_upload(backend_tex->tTexture, &tBufferImageCopy, ptStagingBuffer->tMemoryAllocation.pHostMapped, upload_size, false);
1035-
1036-
// gptStarter->return_staging_buffer(&tStagingBuffer);
1058+
tUpload.tBufferImageCopy.uImageWidth = (uint32_t)upload_w;
1059+
tUpload.tBufferImageCopy.uImageHeight = (uint32_t)upload_h;
1060+
tUpload.tBufferImageCopy.uImageDepth = 1;
1061+
tUpload.tBufferImageCopy.uLayerCount = 1;
1062+
tUpload.tBufferImageCopy.iImageOffsetX = upload_x;
1063+
tUpload.tBufferImageCopy.iImageOffsetY = upload_y;
1064+
tUpload.tBufferImageCopy.szBufferOffset = upload_x * tex->BytesPerPixel + stage_pitch * upload_y;
1065+
tUpload.tBufferImageCopy.uBufferRowLength = stage_pitch;
1066+
tUpload.szUploadSize = upload_size;
1067+
pl_sb_push(bd->sbtTextureUploads, tUpload);
10371068
tex->SetStatus(ImTextureStatus_OK);
10381069
}
10391070

@@ -1171,7 +1202,6 @@ pl_load_dear_imgui_ext(const plApiRegistryI *ptApiRegistry, bool bReload)
11711202
gptMemory = pl_get_api_latest(ptApiRegistry, plMemoryI);
11721203
gptShader = pl_get_api_latest(ptApiRegistry, plShaderI);
11731204
gptStarter = pl_get_api_latest(ptApiRegistry, plStarterI);
1174-
gptStage = pl_get_api_latest(ptApiRegistry, plStageI);
11751205
gptIO = pl_get_api_latest(ptApiRegistry, plIOI);
11761206

11771207
if (bReload)

extensions/pl_graphics_metal.m

Lines changed: 15 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -265,54 +265,33 @@
265265
//-----------------------------------------------------------------------------
266266

267267
void
268-
pl_graphics_copy_buffer_to_texture(
269-
plCommandBuffer* ptCmdBuffer,
270-
plBufferHandle tBufferHandle,
271-
plTextureHandle tTextureHandle,
272-
uint32_t uRegionCount,
273-
const plBufferImageCopy* ptRegions)
268+
pl_graphics_copy_buffer_to_texture( plCommandBuffer* ptCmdBuffer, plBufferHandle tBufferHandle, plTextureHandle tTextureHandle, uint32_t uRegionCount, const plBufferImageCopy* ptRegions)
274269
{
275270
plDevice* ptDevice = ptCmdBuffer->ptDevice;
276271

277-
plMetalBuffer* ptBuffer =
278-
&ptDevice->sbtBuffersHot[tBufferHandle.uIndex];
272+
plMetalBuffer* ptBuffer = &ptDevice->sbtBuffersHot[tBufferHandle.uIndex];
273+
plMetalTexture* ptTexture = &ptDevice->sbtTexturesHot[tTextureHandle.uIndex];
279274

280-
plMetalTexture* ptTexture =
281-
&ptDevice->sbtTexturesHot[tTextureHandle.uIndex];
282-
283-
plTexture* ptColdTexture =
284-
pl_graphics_get_texture(ptDevice, tTextureHandle);
275+
plTexture* ptColdTexture = pl_graphics_get_texture(ptDevice, tTextureHandle);
285276

286277
// This assumes an uncompressed pixel format.
287-
const NSUInteger uFormatStride =
288-
pl__format_stride(ptColdTexture->tDesc.eFormat);
278+
const NSUInteger uFormatStride = pl__format_stride(ptColdTexture->tDesc.eFormat);
289279

290280
for(uint32_t i = 0; i < uRegionCount; i++)
291281
{
292282
const plBufferImageCopy* ptRegion = &ptRegions[i];
293283

294284
const NSUInteger uCopyWidth = ptRegion->uImageWidth;
295-
const NSUInteger uCopyHeight =
296-
ptRegion->uImageHeight ? ptRegion->uImageHeight : 1;
297-
const NSUInteger uCopyDepth =
298-
ptRegion->uImageDepth ? ptRegion->uImageDepth : 1;
285+
const NSUInteger uCopyHeight = ptRegion->uImageHeight ? ptRegion->uImageHeight : 1;
286+
const NSUInteger uCopyDepth = ptRegion->uImageDepth ? ptRegion->uImageDepth : 1;
299287

300-
const NSUInteger uBufferRowLength =
301-
ptRegion->uBufferRowLength ?
302-
ptRegion->uBufferRowLength :
303-
uCopyWidth;
288+
const NSUInteger uBufferRowLength = ptRegion->uBufferRowLength ? ptRegion->uBufferRowLength : uCopyWidth * uFormatStride;
289+
const NSUInteger uBufferImageHeight = ptRegion->uBufferImageHeight ? ptRegion->uBufferImageHeight : uCopyHeight * uFormatStride;
304290

305-
const NSUInteger uBufferImageHeight =
306-
ptRegion->uBufferImageHeight ?
307-
ptRegion->uBufferImageHeight :
308-
uCopyHeight;
309-
310-
const NSUInteger uBytesPerRow =
311-
uBufferRowLength * uFormatStride;
291+
const NSUInteger uBytesPerRow = uBufferRowLength;
312292

313293
// Distance between depth slices within one source image.
314-
const NSUInteger uBytesPerImage =
315-
uBytesPerRow * uBufferImageHeight;
294+
const NSUInteger uBytesPerImage = uBytesPerRow * uBufferImageHeight;
316295

317296
const MTLOrigin tDestinationOrigin = {
318297
.x = (NSUInteger)ptRegion->iImageOffsetX,
@@ -326,21 +305,16 @@
326305
.depth = uCopyDepth
327306
};
328307

329-
const uint32_t uLayerCount =
330-
ptRegion->uLayerCount ? ptRegion->uLayerCount : 1;
308+
const uint32_t uLayerCount = ptRegion->uLayerCount ? ptRegion->uLayerCount : 1;
331309

332310
// One array layer contains all depth slices described by sourceSize.
333-
const NSUInteger uBytesPerArrayLayer =
334-
uBytesPerImage * uCopyDepth;
311+
const NSUInteger uBytesPerArrayLayer = uBytesPerImage * uCopyDepth;
335312

336313
for(uint32_t uLayer = 0; uLayer < uLayerCount; uLayer++)
337314
{
338-
const NSUInteger uSourceOffset =
339-
ptRegion->szBufferOffset +
340-
(NSUInteger)uLayer * uBytesPerArrayLayer;
315+
const NSUInteger uSourceOffset = ptRegion->szBufferOffset + (NSUInteger)uLayer * uBytesPerArrayLayer;
341316

342-
const NSUInteger uDestinationSlice =
343-
ptRegion->uBaseArrayLayer + uLayer;
317+
const NSUInteger uDestinationSlice = ptRegion->uBaseArrayLayer + uLayer;
344318

345319
[ptDevice->tComputeEncoder
346320
copyFromBuffer:ptBuffer->tBuffer

0 commit comments

Comments
 (0)