imgui-platform-kit 2.0.0
Cross-platform Dear ImGui application framework
Loading...
Searching...
No Matches
win32_dx12_user_interface.h
Go to the documentation of this file.
1#pragma once
2
11#if defined(_WIN32)
12
13#include "imgui.h"
14#include "imgui_impl_win32.h"
15#include "imgui_impl_dx12.h"
16
17#include <d3d12.h>
18#include <dxgi1_4.h>
19#include <string>
20#include <tchar.h>
21#include <stdexcept>
22#include <iostream>
23#include <vector>
24#include <filesystem>
25
26#include "implot.h"
27#include "implot_internal.h"
28
29#include <imgui-node-editor/imgui_node_editor.h>
30
31
32#ifdef _DEBUG
33#define DX12_ENABLE_DEBUG_LAYER
34#endif
35
36#ifdef DX12_ENABLE_DEBUG_LAYER
37#include <dxgidebug.h>
38#pragma comment(lib, "dxguid.lib")
39#endif
40
43
44namespace imgui_kit
45{
53 {
54 ID3D12Resource* texture;
55 D3D12_CPU_DESCRIPTOR_HANDLE srv_cpu_handle{};
56 D3D12_GPU_DESCRIPTOR_HANDLE srv_gpu_handle{};
58
60 : texture(nullptr)
61 ,parameters()
62 {
63 srv_cpu_handle.ptr = 0;
64 srv_gpu_handle.ptr = 0;
65 }
66
68 : texture(nullptr), parameters(std::move(parameters))
69 {
70 srv_cpu_handle.ptr = 0;
71 srv_gpu_handle.ptr = 0;
72 }
73
74 void release()
75 {
76 if (texture)
77 texture->Release();
78 texture = nullptr;
79 }
80
82 {
83 if (texture)
84 texture->Release();
85 }
86 };
87
107 {
108 private:
109 UserInterfaceParameters parameters;
110 DX12BackgroundImageTexture backgroundImageTexture;
111 HWND windowHandle;
112 WNDCLASSEXW windowClass;
113 std::vector<std::shared_ptr<UserInterfaceWindow>> windows;
114 bool shutdownRequest;
115 public:
123 ~UserInterface() = default;
124
139 void render();
144 void shutdown();
149 [[nodiscard]] bool isShutdownRequested() const;
156 template<typename T, typename... Args>
157 void addWindow(Args&&... args)
158 {
159 auto window = std::make_shared<T>(std::forward<Args>(args)...);
160 windows.push_back(std::move(window));
161 }
162 private:
163 void loadIcon() const;
164 void loadFont();
165 void loadBackgroundImage();
166 void renderWindows() const;
167 void renderBackgroundImage() const;
168 void updateLastRenderedFrameDimensions();
169 };
170}
171
172// Dear ImGui stuff
176struct FrameContext
177{
178 ID3D12CommandAllocator* CommandAllocator;
179 UINT64 FenceValue;
180};
181
182// Data
183inline int constexpr NUM_FRAMES_IN_FLIGHT = 3;
184inline FrameContext g_frameContext[NUM_FRAMES_IN_FLIGHT] = {};
185inline UINT g_frameIndex = 0;
186inline int constexpr NUM_BACK_BUFFERS = 3;
187inline int constexpr SRV_HEAP_SIZE = 64;
188inline ID3D12Device* g_pd3dDevice = nullptr;
189inline ID3D12DescriptorHeap* g_pd3dRtvDescHeap = nullptr;
190inline ID3D12DescriptorHeap* g_pd3dSrvDescHeap = nullptr;
191inline ID3D12CommandQueue* g_pd3dCommandQueue = nullptr;
192inline ID3D12GraphicsCommandList* g_pd3dCommandList = nullptr;
193inline ID3D12Fence* g_fence = nullptr;
194inline HANDLE g_fenceEvent = nullptr;
195inline UINT64 g_fenceLastSignaledValue = 0;
196inline IDXGISwapChain3* g_pSwapChain = nullptr;
197inline HANDLE g_hSwapChainWaitableObject = nullptr;
198inline ID3D12Resource* g_mainRenderTargetResource[NUM_BACK_BUFFERS] = {};
199inline D3D12_CPU_DESCRIPTOR_HANDLE g_mainRenderTargetDescriptor[NUM_BACK_BUFFERS] = {};
200inline UINT g_SrvDescriptorSize = 0;
201inline std::vector<UINT> g_SrvFreeSlots; // slot 0 reserved for background image
202
203// Forward declarations of helper functions
204bool CreateDeviceD3D(HWND hWnd);
205void CleanupDeviceD3D();
206void CreateRenderTarget();
207void CleanupRenderTarget();
208void WaitForLastSubmittedFrame();
209FrameContext* WaitForNextFrameResources();
210LRESULT WINAPI WndProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam);
211bool LoadTextureFromFile(const char* filename, ID3D12Device* d3d_device, D3D12_CPU_DESCRIPTOR_HANDLE srv_cpu_handle, ID3D12Resource** out_tex_resource, int* out_width, int* out_height);
212std::wstring StringToWString(const std::string& str);
213float GetDpiScale(HWND hWnd);
214void ImGui_ImplDX12_SrvDescAlloc(ImGui_ImplDX12_InitInfo* info, D3D12_CPU_DESCRIPTOR_HANDLE* out_cpu, D3D12_GPU_DESCRIPTOR_HANDLE* out_gpu);
215void ImGui_ImplDX12_SrvDescFree(ImGui_ImplDX12_InitInfo* info, D3D12_CPU_DESCRIPTOR_HANDLE cpu, D3D12_GPU_DESCRIPTOR_HANDLE gpu);
217
218#endif
Main application object: owns the platform window, the rendering backend and the registered UserInter...
Definition win32_dx12_user_interface.h:107
void addWindow(Args &&... args)
Constructs a window of type T and registers it for rendering.
Definition win32_dx12_user_interface.h:157
void shutdown()
Destroys the rendering backend, the ImGui contexts and the platform window.
bool isShutdownRequested() const
Returns true once the user has requested to close the window.
void initialize()
Creates the platform window, sets up the rendering backend and the ImGui/ImPlot/node-editor contexts,...
UserInterface(UserInterfaceParameters parameters)
Constructs the interface with the given parameters.
void render()
Renders one frame: processes platform events and calls render() on every registered window.
UserInterface()
Constructs the interface with default UserInterfaceParameters.
Definition colour_palette.h:11
Background image drawn behind all windows.
Definition user_interface_parameters.h:167
D3D12 texture holding the background image and its parameters.
Definition win32_dx12_user_interface.h:53
ID3D12Resource * texture
Definition win32_dx12_user_interface.h:54
BackgroundImageParameters parameters
Definition win32_dx12_user_interface.h:57
DX12BackgroundImageTexture()
Definition win32_dx12_user_interface.h:59
D3D12_GPU_DESCRIPTOR_HANDLE srv_gpu_handle
Definition win32_dx12_user_interface.h:56
DX12BackgroundImageTexture(BackgroundImageParameters parameters)
Definition win32_dx12_user_interface.h:67
void release()
Definition win32_dx12_user_interface.h:74
D3D12_CPU_DESCRIPTOR_HANDLE srv_cpu_handle
Definition win32_dx12_user_interface.h:55
~DX12BackgroundImageTexture()
Definition win32_dx12_user_interface.h:81
Aggregates all configuration passed to imgui_kit::UserInterface.
Definition user_interface_parameters.h:189
Configuration structures used to set up an imgui_kit::UserInterface.
Abstract base class for user-defined windows and global window flags.