Skip to content

Commit 100075f

Browse files
committed
Backends: DirectX12: Texture upload use the command queue provided in ImGui_ImplDX12_InitInfo instead of creating its own.
+ minor tweaks to faciliate branch merging.
1 parent c59a226 commit 100075f

File tree

3 files changed

+28
-29
lines changed

3 files changed

+28
-29
lines changed

backends/imgui_impl_dx12.cpp

+24-29
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919

2020
// CHANGELOG
2121
// (minor and older changes stripped away, please see git history for details)
22+
// 2025-01-15: DirectX12: Texture upload use the command queue provided in ImGui_ImplDX12_InitInfo instead of creating its own.
2223
// 2024-12-09: DirectX12: Let user specifies the DepthStencilView format by setting ImGui_ImplDX12_InitInfo::DSVFormat.
2324
// 2024-11-15: DirectX12: *BREAKING CHANGE* Changed ImGui_ImplDX12_Init() signature to take a ImGui_ImplDX12_InitInfo struct. Legacy ImGui_ImplDX12_Init() signature is still supported (will obsolete).
2425
// 2024-11-15: DirectX12: *BREAKING CHANGE* User is now required to pass function pointers to allocate/free SRV Descriptors. We provide convenience legacy fields to pass a single descriptor, matching the old API, but upcoming features will want multiple.
@@ -64,6 +65,8 @@ struct ImGui_ImplDX12_Texture
6465
ID3D12Resource* pTextureResource;
6566
D3D12_CPU_DESCRIPTOR_HANDLE hFontSrvCpuDescHandle;
6667
D3D12_GPU_DESCRIPTOR_HANDLE hFontSrvGpuDescHandle;
68+
69+
ImGui_ImplDX12_Texture() { memset((void*)this, 0, sizeof(*this)); }
6770
};
6871

6972
struct ImGui_ImplDX12_Data
@@ -180,8 +183,7 @@ void ImGui_ImplDX12_RenderDrawData(ImDrawData* draw_data, ID3D12GraphicsCommandL
180183
if (draw_data->DisplaySize.x <= 0.0f || draw_data->DisplaySize.y <= 0.0f)
181184
return;
182185

183-
// FIXME: I'm assuming that this only gets called once per frame!
184-
// If not, we can't just re-allocate the IB or VB, we'll have to do a proper allocator.
186+
// FIXME: We are assuming that this only gets called once per frame!
185187
ImGui_ImplDX12_Data* bd = ImGui_ImplDX12_GetBackendData();
186188
bd->frameIndex = bd->frameIndex + 1;
187189
ImGui_ImplDX12_RenderBuffers* fr = &bd->pFrameResources[bd->frameIndex % bd->numFramesInFlight];
@@ -352,11 +354,11 @@ static void ImGui_ImplDX12_CreateFontsTexture()
352354
bd->pd3dDevice->CreateCommittedResource(&props, D3D12_HEAP_FLAG_NONE, &desc,
353355
D3D12_RESOURCE_STATE_COPY_DEST, nullptr, IID_PPV_ARGS(&pTexture));
354356

355-
UINT uploadPitch = (width * 4 + D3D12_TEXTURE_DATA_PITCH_ALIGNMENT - 1u) & ~(D3D12_TEXTURE_DATA_PITCH_ALIGNMENT - 1u);
356-
UINT uploadSize = height * uploadPitch;
357+
UINT upload_pitch = (width * 4 + D3D12_TEXTURE_DATA_PITCH_ALIGNMENT - 1u) & ~(D3D12_TEXTURE_DATA_PITCH_ALIGNMENT - 1u);
358+
UINT upload_size = height * upload_pitch;
357359
desc.Dimension = D3D12_RESOURCE_DIMENSION_BUFFER;
358360
desc.Alignment = 0;
359-
desc.Width = uploadSize;
361+
desc.Width = upload_size;
360362
desc.Height = 1;
361363
desc.DepthOrArraySize = 1;
362364
desc.MipLevels = 1;
@@ -376,26 +378,28 @@ static void ImGui_ImplDX12_CreateFontsTexture()
376378
IM_ASSERT(SUCCEEDED(hr));
377379

378380
void* mapped = nullptr;
379-
D3D12_RANGE range = { 0, uploadSize };
381+
D3D12_RANGE range = { 0, upload_size };
380382
hr = uploadBuffer->Map(0, &range, &mapped);
381383
IM_ASSERT(SUCCEEDED(hr));
382384
for (int y = 0; y < height; y++)
383-
memcpy((void*) ((uintptr_t) mapped + y * uploadPitch), pixels + y * width * 4, width * 4);
385+
memcpy((void*) ((uintptr_t) mapped + y * upload_pitch), pixels + y * width * 4, width * 4);
384386
uploadBuffer->Unmap(0, &range);
385387

386388
D3D12_TEXTURE_COPY_LOCATION srcLocation = {};
387-
srcLocation.pResource = uploadBuffer;
388-
srcLocation.Type = D3D12_TEXTURE_COPY_TYPE_PLACED_FOOTPRINT;
389-
srcLocation.PlacedFootprint.Footprint.Format = DXGI_FORMAT_R8G8B8A8_UNORM;
390-
srcLocation.PlacedFootprint.Footprint.Width = width;
391-
srcLocation.PlacedFootprint.Footprint.Height = height;
392-
srcLocation.PlacedFootprint.Footprint.Depth = 1;
393-
srcLocation.PlacedFootprint.Footprint.RowPitch = uploadPitch;
394-
395389
D3D12_TEXTURE_COPY_LOCATION dstLocation = {};
396-
dstLocation.pResource = pTexture;
397-
dstLocation.Type = D3D12_TEXTURE_COPY_TYPE_SUBRESOURCE_INDEX;
398-
dstLocation.SubresourceIndex = 0;
390+
{
391+
srcLocation.pResource = uploadBuffer;
392+
srcLocation.Type = D3D12_TEXTURE_COPY_TYPE_PLACED_FOOTPRINT;
393+
srcLocation.PlacedFootprint.Footprint.Format = DXGI_FORMAT_R8G8B8A8_UNORM;
394+
srcLocation.PlacedFootprint.Footprint.Width = width;
395+
srcLocation.PlacedFootprint.Footprint.Height = height;
396+
srcLocation.PlacedFootprint.Footprint.Depth = 1;
397+
srcLocation.PlacedFootprint.Footprint.RowPitch = upload_pitch;
398+
399+
dstLocation.pResource = pTexture;
400+
dstLocation.Type = D3D12_TEXTURE_COPY_TYPE_SUBRESOURCE_INDEX;
401+
dstLocation.SubresourceIndex = 0;
402+
}
399403

400404
D3D12_RESOURCE_BARRIER barrier = {};
401405
barrier.Type = D3D12_RESOURCE_BARRIER_TYPE_TRANSITION;
@@ -412,15 +416,6 @@ static void ImGui_ImplDX12_CreateFontsTexture()
412416
HANDLE event = ::CreateEvent(0, 0, 0, 0);
413417
IM_ASSERT(event != nullptr);
414418

415-
D3D12_COMMAND_QUEUE_DESC queueDesc = {};
416-
queueDesc.Type = D3D12_COMMAND_LIST_TYPE_DIRECT;
417-
queueDesc.Flags = D3D12_COMMAND_QUEUE_FLAG_NONE;
418-
queueDesc.NodeMask = 1;
419-
420-
ID3D12CommandQueue* cmdQueue = nullptr;
421-
hr = bd->pd3dDevice->CreateCommandQueue(&queueDesc, IID_PPV_ARGS(&cmdQueue));
422-
IM_ASSERT(SUCCEEDED(hr));
423-
424419
ID3D12CommandAllocator* cmdAlloc = nullptr;
425420
hr = bd->pd3dDevice->CreateCommandAllocator(D3D12_COMMAND_LIST_TYPE_DIRECT, IID_PPV_ARGS(&cmdAlloc));
426421
IM_ASSERT(SUCCEEDED(hr));
@@ -435,6 +430,7 @@ static void ImGui_ImplDX12_CreateFontsTexture()
435430
hr = cmdList->Close();
436431
IM_ASSERT(SUCCEEDED(hr));
437432

433+
ID3D12CommandQueue* cmdQueue = bd->InitInfo.CommandQueue;
438434
cmdQueue->ExecuteCommandLists(1, (ID3D12CommandList* const*)&cmdList);
439435
hr = cmdQueue->Signal(fence, 1);
440436
IM_ASSERT(SUCCEEDED(hr));
@@ -444,7 +440,6 @@ static void ImGui_ImplDX12_CreateFontsTexture()
444440

445441
cmdList->Release();
446442
cmdAlloc->Release();
447-
cmdQueue->Release();
448443
::CloseHandle(event);
449444
fence->Release();
450445
uploadBuffer->Release();
@@ -707,11 +702,11 @@ void ImGui_ImplDX12_InvalidateDeviceObjects()
707702
if (!bd || !bd->pd3dDevice)
708703
return;
709704

710-
ImGuiIO& io = ImGui::GetIO();
711705
SafeRelease(bd->pRootSignature);
712706
SafeRelease(bd->pPipelineState);
713707

714708
// Free SRV descriptor used by texture
709+
ImGuiIO& io = ImGui::GetIO();
715710
ImGui_ImplDX12_Texture* font_tex = &bd->FontTexture;
716711
bd->InitInfo.SrvDescriptorFreeFn(&bd->InitInfo, font_tex->hFontSrvCpuDescHandle, font_tex->hFontSrvGpuDescHandle);
717712
SafeRelease(font_tex->pTextureResource);

docs/CHANGELOG.txt

+3
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,9 @@ Breaking changes:
4343

4444
Other changes:
4545

46+
- Backends: DirectX12: Texture upload use the command queue provided in
47+
ImGui_ImplDX12_InitInfo instead of creating its own.
48+
4649

4750
-----------------------------------------------------------------------
4851
VERSION 1.91.7 (Released 2025-01-14)

imgui.cpp

+1
Original file line numberDiff line numberDiff line change
@@ -5086,6 +5086,7 @@ void ImGui::UpdateHoveredWindowAndCaptureFlags()
50865086
}
50875087

50885088
// Called once a frame. Followed by SetCurrentFont() which sets up the remaining data.
5089+
// FIXME-VIEWPORT: the concept of a single ClipRectFullscreen is not ideal!
50895090
static void SetupDrawListSharedData()
50905091
{
50915092
ImGuiContext& g = *GImGui;

0 commit comments

Comments
 (0)