@@ -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;
4746static const plMemoryI* gptMemory = nullptr ;
4847static const plShaderI* gptShader = nullptr ;
4948static const plStarterI* gptStarter = nullptr ;
50- static const plStageI* gptStage = nullptr ;
5149static 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+
98104struct 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)
338346void
339347pl_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)
0 commit comments