Skip to content

Commit 3260ea6

Browse files
committed
Examples: Win32+DX12: Tweaks.
1 parent 8be0723 commit 3260ea6

File tree

3 files changed

+24
-18
lines changed

3 files changed

+24
-18
lines changed

backends/imgui_impl_dx12.cpp

+3
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,9 @@
66
// [X] Renderer: Large meshes support (64k+ vertices) with 16-bit indices.
77
// [X] Renderer: Expose selected render state for draw callbacks to use. Access in '(ImGui_ImplXXXX_RenderState*)GetPlatformIO().Renderer_RenderState'.
88

9+
// The aim of imgui_impl_dx12.h/.cpp is to be usable in your engine without any modification.
10+
// IF YOU FEEL YOU NEED TO MAKE ANY CHANGE TO THIS CODE, please share them and your feedback at https://github.com/ocornut/imgui/
11+
912
// You can use unmodified imgui_impl_* files in your project. See examples/ folder for examples of using this.
1013
// Prefer including the entire imgui/ repository into your project (either as a copy or as a submodule), and only build the backends you need.
1114
// Learn about Dear ImGui:

backends/imgui_impl_dx12.h

+3
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,9 @@
66
// [X] Renderer: Large meshes support (64k+ vertices) with 16-bit indices.
77
// [X] Renderer: Expose selected render state for draw callbacks to use. Access in '(ImGui_ImplXXXX_RenderState*)GetPlatformIO().Renderer_RenderState'.
88

9+
// The aim of imgui_impl_dx12.h/.cpp is to be usable in your engine without any modification.
10+
// IF YOU FEEL YOU NEED TO MAKE ANY CHANGE TO THIS CODE, please share them and your feedback at https://github.com/ocornut/imgui/
11+
912
// You can use unmodified imgui_impl_* files in your project. See examples/ folder for examples of using this.
1013
// Prefer including the entire imgui/ repository into your project (either as a copy or as a submodule), and only build the backends you need.
1114
// Learn about Dear ImGui:

examples/example_win32_directx12/main.cpp

+18-18
Original file line numberDiff line numberDiff line change
@@ -22,20 +22,20 @@
2222
#pragma comment(lib, "dxguid.lib")
2323
#endif
2424

25-
#include "imgui_internal.h"
25+
// Config for example app
26+
static const int APP_NUM_FRAMES_IN_FLIGHT = 3;
27+
static const int APP_NUM_BACK_BUFFERS = 3;
2628

2729
struct FrameContext
2830
{
29-
ID3D12CommandAllocator* CommandAllocator;
30-
UINT64 FenceValue;
31+
ID3D12CommandAllocator* CommandAllocator;
32+
UINT64 FenceValue;
3133
};
3234

3335
// Data
34-
static int const NUM_FRAMES_IN_FLIGHT = 3;
35-
static FrameContext g_frameContext[NUM_FRAMES_IN_FLIGHT] = {};
36+
static FrameContext g_frameContext[APP_NUM_FRAMES_IN_FLIGHT] = {};
3637
static UINT g_frameIndex = 0;
3738

38-
static int const NUM_BACK_BUFFERS = 3;
3939
static ID3D12Device* g_pd3dDevice = nullptr;
4040
static ID3D12DescriptorHeap* g_pd3dRtvDescHeap = nullptr;
4141
static ID3D12DescriptorHeap* g_pd3dSrvDescHeap = nullptr;
@@ -47,8 +47,8 @@ static UINT64 g_fenceLastSignaledValue = 0;
4747
static IDXGISwapChain3* g_pSwapChain = nullptr;
4848
static bool g_SwapChainOccluded = false;
4949
static HANDLE g_hSwapChainWaitableObject = nullptr;
50-
static ID3D12Resource* g_mainRenderTargetResource[NUM_BACK_BUFFERS] = {};
51-
static D3D12_CPU_DESCRIPTOR_HANDLE g_mainRenderTargetDescriptor[NUM_BACK_BUFFERS] = {};
50+
static ID3D12Resource* g_mainRenderTargetResource[APP_NUM_BACK_BUFFERS] = {};
51+
static D3D12_CPU_DESCRIPTOR_HANDLE g_mainRenderTargetDescriptor[APP_NUM_BACK_BUFFERS] = {};
5252

5353
// Forward declarations of helper functions
5454
bool CreateDeviceD3D(HWND hWnd);
@@ -249,7 +249,7 @@ bool CreateDeviceD3D(HWND hWnd)
249249
DXGI_SWAP_CHAIN_DESC1 sd;
250250
{
251251
ZeroMemory(&sd, sizeof(sd));
252-
sd.BufferCount = NUM_BACK_BUFFERS;
252+
sd.BufferCount = APP_NUM_BACK_BUFFERS;
253253
sd.Width = 0;
254254
sd.Height = 0;
255255
sd.Format = DXGI_FORMAT_R8G8B8A8_UNORM;
@@ -292,15 +292,15 @@ bool CreateDeviceD3D(HWND hWnd)
292292
{
293293
D3D12_DESCRIPTOR_HEAP_DESC desc = {};
294294
desc.Type = D3D12_DESCRIPTOR_HEAP_TYPE_RTV;
295-
desc.NumDescriptors = NUM_BACK_BUFFERS;
295+
desc.NumDescriptors = APP_NUM_BACK_BUFFERS;
296296
desc.Flags = D3D12_DESCRIPTOR_HEAP_FLAG_NONE;
297297
desc.NodeMask = 1;
298298
if (g_pd3dDevice->CreateDescriptorHeap(&desc, IID_PPV_ARGS(&g_pd3dRtvDescHeap)) != S_OK)
299299
return false;
300300

301301
SIZE_T rtvDescriptorSize = g_pd3dDevice->GetDescriptorHandleIncrementSize(D3D12_DESCRIPTOR_HEAP_TYPE_RTV);
302302
D3D12_CPU_DESCRIPTOR_HANDLE rtvHandle = g_pd3dRtvDescHeap->GetCPUDescriptorHandleForHeapStart();
303-
for (UINT i = 0; i < NUM_BACK_BUFFERS; i++)
303+
for (UINT i = 0; i < APP_NUM_BACK_BUFFERS; i++)
304304
{
305305
g_mainRenderTargetDescriptor[i] = rtvHandle;
306306
rtvHandle.ptr += rtvDescriptorSize;
@@ -325,7 +325,7 @@ bool CreateDeviceD3D(HWND hWnd)
325325
return false;
326326
}
327327

328-
for (UINT i = 0; i < NUM_FRAMES_IN_FLIGHT; i++)
328+
for (UINT i = 0; i < APP_NUM_FRAMES_IN_FLIGHT; i++)
329329
if (g_pd3dDevice->CreateCommandAllocator(D3D12_COMMAND_LIST_TYPE_DIRECT, IID_PPV_ARGS(&g_frameContext[i].CommandAllocator)) != S_OK)
330330
return false;
331331

@@ -351,7 +351,7 @@ bool CreateDeviceD3D(HWND hWnd)
351351
return false;
352352
swapChain1->Release();
353353
dxgiFactory->Release();
354-
g_pSwapChain->SetMaximumFrameLatency(NUM_BACK_BUFFERS);
354+
g_pSwapChain->SetMaximumFrameLatency(APP_NUM_BACK_BUFFERS);
355355
g_hSwapChainWaitableObject = g_pSwapChain->GetFrameLatencyWaitableObject();
356356
}
357357

@@ -364,7 +364,7 @@ void CleanupDeviceD3D()
364364
CleanupRenderTarget();
365365
if (g_pSwapChain) { g_pSwapChain->SetFullscreenState(false, nullptr); g_pSwapChain->Release(); g_pSwapChain = nullptr; }
366366
if (g_hSwapChainWaitableObject != nullptr) { CloseHandle(g_hSwapChainWaitableObject); }
367-
for (UINT i = 0; i < NUM_FRAMES_IN_FLIGHT; i++)
367+
for (UINT i = 0; i < APP_NUM_FRAMES_IN_FLIGHT; i++)
368368
if (g_frameContext[i].CommandAllocator) { g_frameContext[i].CommandAllocator->Release(); g_frameContext[i].CommandAllocator = nullptr; }
369369
if (g_pd3dCommandQueue) { g_pd3dCommandQueue->Release(); g_pd3dCommandQueue = nullptr; }
370370
if (g_pd3dCommandList) { g_pd3dCommandList->Release(); g_pd3dCommandList = nullptr; }
@@ -386,7 +386,7 @@ void CleanupDeviceD3D()
386386

387387
void CreateRenderTarget()
388388
{
389-
for (UINT i = 0; i < NUM_BACK_BUFFERS; i++)
389+
for (UINT i = 0; i < APP_NUM_BACK_BUFFERS; i++)
390390
{
391391
ID3D12Resource* pBackBuffer = nullptr;
392392
g_pSwapChain->GetBuffer(i, IID_PPV_ARGS(&pBackBuffer));
@@ -399,13 +399,13 @@ void CleanupRenderTarget()
399399
{
400400
WaitForLastSubmittedFrame();
401401

402-
for (UINT i = 0; i < NUM_BACK_BUFFERS; i++)
402+
for (UINT i = 0; i < APP_NUM_BACK_BUFFERS; i++)
403403
if (g_mainRenderTargetResource[i]) { g_mainRenderTargetResource[i]->Release(); g_mainRenderTargetResource[i] = nullptr; }
404404
}
405405

406406
void WaitForLastSubmittedFrame()
407407
{
408-
FrameContext* frameCtx = &g_frameContext[g_frameIndex % NUM_FRAMES_IN_FLIGHT];
408+
FrameContext* frameCtx = &g_frameContext[g_frameIndex % APP_NUM_FRAMES_IN_FLIGHT];
409409

410410
UINT64 fenceValue = frameCtx->FenceValue;
411411
if (fenceValue == 0)
@@ -427,7 +427,7 @@ FrameContext* WaitForNextFrameResources()
427427
HANDLE waitableObjects[] = { g_hSwapChainWaitableObject, nullptr };
428428
DWORD numWaitableObjects = 1;
429429

430-
FrameContext* frameCtx = &g_frameContext[nextFrameIndex % NUM_FRAMES_IN_FLIGHT];
430+
FrameContext* frameCtx = &g_frameContext[nextFrameIndex % APP_NUM_FRAMES_IN_FLIGHT];
431431
UINT64 fenceValue = frameCtx->FenceValue;
432432
if (fenceValue != 0) // means no fence was signaled
433433
{

0 commit comments

Comments
 (0)