| 1 |
// dear imgui: Renderer Backend for DirectX12 |
| 2 |
// This needs to be used along with a Platform Backend (e.g. Win32) |
| 3 |
|
| 4 |
// Implemented features: |
| 5 |
// [X] Renderer: User texture binding. Use 'D3D12_GPU_DESCRIPTOR_HANDLE' as texture identifier. Read the FAQ about ImTextureID/ImTextureRef! |
| 6 |
// [X] Renderer: Large meshes support (64k+ vertices) even with 16-bit indices (ImGuiBackendFlags_RendererHasVtxOffset). |
| 7 |
// [X] Renderer: Texture updates support for dynamic font atlas (ImGuiBackendFlags_RendererHasTextures). |
| 8 |
// [X] Renderer: Expose selected render state for draw callbacks to use. Access in '(ImGui_ImplXXXX_RenderState*)GetPlatformIO().Renderer_RenderState'. |
| 9 |
// [X] Renderer: Multi-viewport support (multiple windows). Enable with 'io.ConfigFlags |= ImGuiConfigFlags_ViewportsEnable'. |
| 10 |
// FIXME: The transition from removing a viewport and moving the window in an existing hosted viewport tends to flicker. |
| 11 |
|
| 12 |
// The aim of imgui_impl_dx12.h/.cpp is to be usable in your engine without any modification. |
| 13 |
// IF YOU FEEL YOU NEED TO MAKE ANY CHANGE TO THIS CODE, please share them and your feedback at https://github.com/ocornut/imgui/ |
| 14 |
|
| 15 |
// You can use unmodified imgui_impl_* files in your project. See examples/ folder for examples of using this. |
| 16 |
// Prefer including the entire imgui/ repository into your project (either as a copy or as a submodule), and only build the backends you need. |
| 17 |
// Learn about Dear ImGui: |
| 18 |
// - FAQ https://dearimgui.com/faq |
| 19 |
// - Getting Started https://dearimgui.com/getting-started |
| 20 |
// - Documentation https://dearimgui.com/docs (same as your local docs/ folder). |
| 21 |
// - Introduction, links and more at the top of imgui.cpp |
| 22 |
|
| 23 |
// CHANGELOG |
| 24 |
// (minor and older changes stripped away, please see git history for details) |
| 25 |
// 2025-XX-XX: Platform: Added support for multiple windows via the ImGuiPlatformIO interface. |
| 26 |
// 2025-06-19: Fixed build on MinGW. (#8702, #4594) |
| 27 |
// 2025-06-11: DirectX12: Added support for ImGuiBackendFlags_RendererHasTextures, for dynamic font atlas. |
| 28 |
// 2025-05-07: DirectX12: Honor draw_data->FramebufferScale to allow for custom backends and experiment using it (consistently with other renderer backends, even though in normal condition it is not set under Windows). |
| 29 |
// 2025-02-24: DirectX12: Fixed an issue where ImGui_ImplDX12_Init() signature change from 2024-11-15 combined with change from 2025-01-15 made legacy ImGui_ImplDX12_Init() crash. (#8429) |
| 30 |
// 2025-01-15: DirectX12: Texture upload use the command queue provided in ImGui_ImplDX12_InitInfo instead of creating its own. |
| 31 |
// 2024-12-09: DirectX12: Let user specifies the DepthStencilView format by setting ImGui_ImplDX12_InitInfo::DSVFormat. |
| 32 |
// 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). |
| 33 |
// 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. |
| 34 |
// 2024-10-23: DirectX12: Unmap() call specify written range. The range is informational and may be used by debug tools. |
| 35 |
// 2024-10-07: DirectX12: Changed default texture sampler to Clamp instead of Repeat/Wrap. |
| 36 |
// 2024-10-07: DirectX12: Expose selected render state in ImGui_ImplDX12_RenderState, which you can access in 'void* platform_io.Renderer_RenderState' during draw callbacks. |
| 37 |
// 2024-10-07: DirectX12: Compiling with '#define ImTextureID=ImU64' is unnecessary now that dear imgui defaults ImTextureID to u64 instead of void*. |
| 38 |
// 2022-10-11: Using 'nullptr' instead of 'NULL' as per our switch to C++11. |
| 39 |
// 2021-06-29: Reorganized backend to pull data from a single structure to facilitate usage with multiple-contexts (all g_XXXX access changed to bd->XXXX). |
| 40 |
// 2021-05-19: DirectX12: Replaced direct access to ImDrawCmd::TextureId with a call to ImDrawCmd::GetTexID(). (will become a requirement) |
| 41 |
// 2021-02-18: DirectX12: Change blending equation to preserve alpha in output buffer. |
| 42 |
// 2021-01-11: DirectX12: Improve Windows 7 compatibility (for D3D12On7) by loading d3d12.dll dynamically. |
| 43 |
// 2020-09-16: DirectX12: Avoid rendering calls with zero-sized scissor rectangle since it generates a validation layer warning. |
| 44 |
// 2020-09-08: DirectX12: Clarified support for building on 32-bit systems by redefining ImTextureID. |
| 45 |
// 2019-10-18: DirectX12: *BREAKING CHANGE* Added extra ID3D12DescriptorHeap parameter to ImGui_ImplDX12_Init() function. |
| 46 |
// 2019-05-29: DirectX12: Added support for large mesh (64K+ vertices), enable ImGuiBackendFlags_RendererHasVtxOffset flag. |
| 47 |
// 2019-04-30: DirectX12: Added support for special ImDrawCallback_ResetRenderState callback to reset render state. |
| 48 |
// 2019-03-29: Misc: Various minor tidying up. |
| 49 |
// 2018-12-03: Misc: Added #pragma comment statement to automatically link with d3dcompiler.lib when using D3DCompile(). |
| 50 |
// 2018-11-30: Misc: Setting up io.BackendRendererName so it can be displayed in the About Window. |
| 51 |
// 2018-06-12: DirectX12: Moved the ID3D12GraphicsCommandList* parameter from NewFrame() to RenderDrawData(). |
| 52 |
// 2018-06-08: Misc: Extracted imgui_impl_dx12.cpp/.h away from the old combined DX12+Win32 example. |
| 53 |
// 2018-06-08: DirectX12: Use draw_data->DisplayPos and draw_data->DisplaySize to setup projection matrix and clipping rectangle (to ease support for future multi-viewport). |
| 54 |
// 2018-02-22: Merged into master with all Win32 code synchronized to other examples. |
| 55 |
|
| 56 |
#include "imgui.h" |
| 57 |
#ifndef IMGUI_DISABLE |
| 58 |
#include "imgui_impl_dx12.h" |
| 59 |
|
| 60 |
// DirectX |
| 61 |
#include <d3d12.h> |
| 62 |
#include <dxgi1_4.h> |
| 63 |
#include <d3dcompiler.h> |
| 64 |
#ifdef _MSC_VER |
| 65 |
#pragma comment(lib, "d3dcompiler") // Automatically link with d3dcompiler.lib as we are using D3DCompile() below. |
| 66 |
#endif |
| 67 |
|
| 68 |
// Clang/GCC warnings with -Weverything |
| 69 |
#if defined(__clang__) |
| 70 |
#pragma clang diagnostic ignored "-Wold-style-cast" // warning: use of old-style cast // yes, they are more terse. |
| 71 |
#pragma clang diagnostic ignored "-Wsign-conversion" // warning: implicit conversion changes signedness |
| 72 |
#endif |
| 73 |
|
| 74 |
// MinGW workaround, see #4594 |
| 75 |
typedef decltype(D3D12SerializeRootSignature) *_PFN_D3D12_SERIALIZE_ROOT_SIGNATURE; |
| 76 |
|
| 77 |
// DirectX12 data |
| 78 |
struct ImGui_ImplDX12_RenderBuffers; |
| 79 |
|
| 80 |
struct ImGui_ImplDX12_Texture |
| 81 |
{ |
| 82 |
ID3D12Resource* pTextureResource; |
| 83 |
D3D12_CPU_DESCRIPTOR_HANDLE hFontSrvCpuDescHandle; |
| 84 |
D3D12_GPU_DESCRIPTOR_HANDLE hFontSrvGpuDescHandle; |
| 85 |
|
| 86 |
ImGui_ImplDX12_Texture() { memset((void*)this, 0, sizeof(*this)); } |
| 87 |
}; |
| 88 |
|
| 89 |
struct ImGui_ImplDX12_Data |
| 90 |
{ |
| 91 |
ImGui_ImplDX12_InitInfo InitInfo; |
| 92 |
ID3D12Device* pd3dDevice; |
| 93 |
ID3D12RootSignature* pRootSignature; |
| 94 |
ID3D12PipelineState* pPipelineState; |
| 95 |
ID3D12CommandQueue* pCommandQueue; |
| 96 |
bool commandQueueOwned; |
| 97 |
DXGI_FORMAT RTVFormat; |
| 98 |
DXGI_FORMAT DSVFormat; |
| 99 |
ID3D12DescriptorHeap* pd3dSrvDescHeap; |
| 100 |
UINT numFramesInFlight; |
| 101 |
ImGui_ImplDX12_Texture FontTexture; |
| 102 |
bool LegacySingleDescriptorUsed; |
| 103 |
|
| 104 |
ImGui_ImplDX12_Data() { memset((void*)this, 0, sizeof(*this)); } |
| 105 |
}; |
| 106 |
|
| 107 |
// Backend data stored in io.BackendRendererUserData to allow support for multiple Dear ImGui contexts |
| 108 |
// It is STRONGLY preferred that you use docking branch with multi-viewports (== single Dear ImGui context + multiple windows) instead of multiple Dear ImGui contexts. |
| 109 |
static ImGui_ImplDX12_Data* ImGui_ImplDX12_GetBackendData() |
| 110 |
{ |
| 111 |
return ImGui::GetCurrentContext() ? (ImGui_ImplDX12_Data*)ImGui::GetIO().BackendRendererUserData : nullptr; |
| 112 |
} |
| 113 |
|
| 114 |
// Buffers used during the rendering of a frame |
| 115 |
struct ImGui_ImplDX12_RenderBuffers |
| 116 |
{ |
| 117 |
ID3D12Resource* IndexBuffer; |
| 118 |
ID3D12Resource* VertexBuffer; |
| 119 |
int IndexBufferSize; |
| 120 |
int VertexBufferSize; |
| 121 |
}; |
| 122 |
|
| 123 |
// Buffers used for secondary viewports created by the multi-viewports systems |
| 124 |
struct ImGui_ImplDX12_FrameContext |
| 125 |
{ |
| 126 |
ID3D12CommandAllocator* CommandAllocator; |
| 127 |
ID3D12Resource* RenderTarget; |
| 128 |
D3D12_CPU_DESCRIPTOR_HANDLE RenderTargetCpuDescriptors; |
| 129 |
}; |
| 130 |
|
| 131 |
// Helper structure we store in the void* RendererUserData field of each ImGuiViewport to easily retrieve our backend data. |
| 132 |
// Main viewport created by application will only use the Resources field. |
| 133 |
// Secondary viewports created by this backend will use all the fields (including Window fields), |
| 134 |
struct ImGui_ImplDX12_ViewportData |
| 135 |
{ |
| 136 |
// Window |
| 137 |
ID3D12CommandQueue* CommandQueue; |
| 138 |
ID3D12GraphicsCommandList* CommandList; |
| 139 |
ID3D12DescriptorHeap* RtvDescHeap; |
| 140 |
IDXGISwapChain3* SwapChain; |
| 141 |
ID3D12Fence* Fence; |
| 142 |
UINT64 FenceSignaledValue; |
| 143 |
HANDLE FenceEvent; |
| 144 |
UINT NumFramesInFlight; |
| 145 |
ImGui_ImplDX12_FrameContext* FrameCtx; |
| 146 |
|
| 147 |
// Render buffers |
| 148 |
UINT FrameIndex; |
| 149 |
ImGui_ImplDX12_RenderBuffers* FrameRenderBuffers; |
| 150 |
|
| 151 |
ImGui_ImplDX12_ViewportData(UINT num_frames_in_flight) |
| 152 |
{ |
| 153 |
CommandQueue = nullptr; |
| 154 |
CommandList = nullptr; |
| 155 |
RtvDescHeap = nullptr; |
| 156 |
SwapChain = nullptr; |
| 157 |
Fence = nullptr; |
| 158 |
FenceSignaledValue = 0; |
| 159 |
FenceEvent = nullptr; |
| 160 |
NumFramesInFlight = num_frames_in_flight; |
| 161 |
FrameCtx = new ImGui_ImplDX12_FrameContext[NumFramesInFlight]; |
| 162 |
FrameIndex = UINT_MAX; |
| 163 |
FrameRenderBuffers = new ImGui_ImplDX12_RenderBuffers[NumFramesInFlight]; |
| 164 |
|
| 165 |
for (UINT i = 0; i < NumFramesInFlight; ++i) |
| 166 |
{ |
| 167 |
FrameCtx[i].CommandAllocator = nullptr; |
| 168 |
FrameCtx[i].RenderTarget = nullptr; |
| 169 |
|
| 170 |
// Create buffers with a default size (they will later be grown as needed) |
| 171 |
FrameRenderBuffers[i].IndexBuffer = nullptr; |
| 172 |
FrameRenderBuffers[i].VertexBuffer = nullptr; |
| 173 |
FrameRenderBuffers[i].VertexBufferSize = 5000; |
| 174 |
FrameRenderBuffers[i].IndexBufferSize = 10000; |
| 175 |
} |
| 176 |
} |
| 177 |
~ImGui_ImplDX12_ViewportData() |
| 178 |
{ |
| 179 |
IM_ASSERT(CommandQueue == nullptr && CommandList == nullptr); |
| 180 |
IM_ASSERT(RtvDescHeap == nullptr); |
| 181 |
IM_ASSERT(SwapChain == nullptr); |
| 182 |
IM_ASSERT(Fence == nullptr); |
| 183 |
IM_ASSERT(FenceEvent == nullptr); |
| 184 |
|
| 185 |
for (UINT i = 0; i < NumFramesInFlight; ++i) |
| 186 |
{ |
| 187 |
IM_ASSERT(FrameCtx[i].CommandAllocator == nullptr && FrameCtx[i].RenderTarget == nullptr); |
| 188 |
IM_ASSERT(FrameRenderBuffers[i].IndexBuffer == nullptr && FrameRenderBuffers[i].VertexBuffer == nullptr); |
| 189 |
} |
| 190 |
|
| 191 |
delete[] FrameCtx; FrameCtx = nullptr; |
| 192 |
delete[] FrameRenderBuffers; FrameRenderBuffers = nullptr; |
| 193 |
} |
| 194 |
}; |
| 195 |
|
| 196 |
struct VERTEX_CONSTANT_BUFFER_DX12 |
| 197 |
{ |
| 198 |
float mvp[4][4]; |
| 199 |
}; |
| 200 |
|
| 201 |
// Forward Declarations |
| 202 |
static void ImGui_ImplDX12_InitPlatformInterface(); |
| 203 |
static void ImGui_ImplDX12_ShutdownPlatformInterface(); |
| 204 |
|
| 205 |
// Functions |
| 206 |
static void ImGui_ImplDX12_SetupRenderState(ImDrawData* draw_data, ID3D12GraphicsCommandList* command_list, ImGui_ImplDX12_RenderBuffers* fr) |
| 207 |
{ |
| 208 |
ImGui_ImplDX12_Data* bd = ImGui_ImplDX12_GetBackendData(); |
| 209 |
|
| 210 |
// Setup orthographic projection matrix into our constant buffer |
| 211 |
// Our visible imgui space lies from draw_data->DisplayPos (top left) to draw_data->DisplayPos+data_data->DisplaySize (bottom right). |
| 212 |
VERTEX_CONSTANT_BUFFER_DX12 vertex_constant_buffer; |
| 213 |
{ |
| 214 |
float L = draw_data->DisplayPos.x; |
| 215 |
float R = draw_data->DisplayPos.x + draw_data->DisplaySize.x; |
| 216 |
float T = draw_data->DisplayPos.y; |
| 217 |
float B = draw_data->DisplayPos.y + draw_data->DisplaySize.y; |
| 218 |
float mvp[4][4] = |
| 219 |
{ |
| 220 |
{ 2.0f/(R-L), 0.0f, 0.0f, 0.0f }, |
| 221 |
{ 0.0f, 2.0f/(T-B), 0.0f, 0.0f }, |
| 222 |
{ 0.0f, 0.0f, 0.5f, 0.0f }, |
| 223 |
{ (R+L)/(L-R), (T+B)/(B-T), 0.5f, 1.0f }, |
| 224 |
}; |
| 225 |
memcpy(&vertex_constant_buffer.mvp, mvp, sizeof(mvp)); |
| 226 |
} |
| 227 |
|
| 228 |
// Setup viewport |
| 229 |
D3D12_VIEWPORT vp = {}; |
| 230 |
vp.Width = draw_data->DisplaySize.x * draw_data->FramebufferScale.x; |
| 231 |
vp.Height = draw_data->DisplaySize.y * draw_data->FramebufferScale.y; |
| 232 |
vp.MinDepth = 0.0f; |
| 233 |
vp.MaxDepth = 1.0f; |
| 234 |
vp.TopLeftX = vp.TopLeftY = 0.0f; |
| 235 |
command_list->RSSetViewports(1, &vp); |
| 236 |
|
| 237 |
// Bind shader and vertex buffers |
| 238 |
unsigned int stride = sizeof(ImDrawVert); |
| 239 |
unsigned int offset = 0; |
| 240 |
D3D12_VERTEX_BUFFER_VIEW vbv = {}; |
| 241 |
vbv.BufferLocation = fr->VertexBuffer->GetGPUVirtualAddress() + offset; |
| 242 |
vbv.SizeInBytes = fr->VertexBufferSize * stride; |
| 243 |
vbv.StrideInBytes = stride; |
| 244 |
command_list->IASetVertexBuffers(0, 1, &vbv); |
| 245 |
D3D12_INDEX_BUFFER_VIEW ibv = {}; |
| 246 |
ibv.BufferLocation = fr->IndexBuffer->GetGPUVirtualAddress(); |
| 247 |
ibv.SizeInBytes = fr->IndexBufferSize * sizeof(ImDrawIdx); |
| 248 |
ibv.Format = sizeof(ImDrawIdx) == 2 ? DXGI_FORMAT_R16_UINT : DXGI_FORMAT_R32_UINT; |
| 249 |
command_list->IASetIndexBuffer(&ibv); |
| 250 |
command_list->IASetPrimitiveTopology(D3D_PRIMITIVE_TOPOLOGY_TRIANGLELIST); |
| 251 |
command_list->SetPipelineState(bd->pPipelineState); |
| 252 |
command_list->SetGraphicsRootSignature(bd->pRootSignature); |
| 253 |
command_list->SetGraphicsRoot32BitConstants(0, 16, &vertex_constant_buffer, 0); |
| 254 |
|
| 255 |
// Setup blend factor |
| 256 |
const float blend_factor[4] = { 0.f, 0.f, 0.f, 0.f }; |
| 257 |
command_list->OMSetBlendFactor(blend_factor); |
| 258 |
} |
| 259 |
|
| 260 |
template<typename T> |
| 261 |
static inline void SafeRelease(T*& res) |
| 262 |
{ |
| 263 |
if (res) |
| 264 |
res->Release(); |
| 265 |
res = nullptr; |
| 266 |
} |
| 267 |
|
| 268 |
// Render function |
| 269 |
void ImGui_ImplDX12_RenderDrawData(ImDrawData* draw_data, ID3D12GraphicsCommandList* command_list) |
| 270 |
{ |
| 271 |
// Avoid rendering when minimized |
| 272 |
if (draw_data->DisplaySize.x <= 0.0f || draw_data->DisplaySize.y <= 0.0f) |
| 273 |
return; |
| 274 |
|
| 275 |
// Catch up with texture updates. Most of the times, the list will have 1 element with an OK status, aka nothing to do. |
| 276 |
// (This almost always points to ImGui::GetPlatformIO().Textures[] but is part of ImDrawData to allow overriding or disabling texture updates). |
| 277 |
if (draw_data->Textures != nullptr) |
| 278 |
for (ImTextureData* tex : *draw_data->Textures) |
| 279 |
if (tex->Status != ImTextureStatus_OK) |
| 280 |
ImGui_ImplDX12_UpdateTexture(tex); |
| 281 |
|
| 282 |
// FIXME: We are assuming that this only gets called once per frame! |
| 283 |
ImGui_ImplDX12_Data* bd = ImGui_ImplDX12_GetBackendData(); |
| 284 |
ImGui_ImplDX12_ViewportData* vd = (ImGui_ImplDX12_ViewportData*)draw_data->OwnerViewport->RendererUserData; |
| 285 |
vd->FrameIndex++; |
| 286 |
ImGui_ImplDX12_RenderBuffers* fr = &vd->FrameRenderBuffers[vd->FrameIndex % bd->numFramesInFlight]; |
| 287 |
|
| 288 |
// Create and grow vertex/index buffers if needed |
| 289 |
if (fr->VertexBuffer == nullptr || fr->VertexBufferSize < draw_data->TotalVtxCount) |
| 290 |
{ |
| 291 |
SafeRelease(fr->VertexBuffer); |
| 292 |
fr->VertexBufferSize = draw_data->TotalVtxCount + 5000; |
| 293 |
D3D12_HEAP_PROPERTIES props = {}; |
| 294 |
props.Type = D3D12_HEAP_TYPE_UPLOAD; |
| 295 |
props.CPUPageProperty = D3D12_CPU_PAGE_PROPERTY_UNKNOWN; |
| 296 |
props.MemoryPoolPreference = D3D12_MEMORY_POOL_UNKNOWN; |
| 297 |
D3D12_RESOURCE_DESC desc = {}; |
| 298 |
desc.Dimension = D3D12_RESOURCE_DIMENSION_BUFFER; |
| 299 |
desc.Width = fr->VertexBufferSize * sizeof(ImDrawVert); |
| 300 |
desc.Height = 1; |
| 301 |
desc.DepthOrArraySize = 1; |
| 302 |
desc.MipLevels = 1; |
| 303 |
desc.Format = DXGI_FORMAT_UNKNOWN; |
| 304 |
desc.SampleDesc.Count = 1; |
| 305 |
desc.Layout = D3D12_TEXTURE_LAYOUT_ROW_MAJOR; |
| 306 |
desc.Flags = D3D12_RESOURCE_FLAG_NONE; |
| 307 |
if (bd->pd3dDevice->CreateCommittedResource(&props, D3D12_HEAP_FLAG_NONE, &desc, D3D12_RESOURCE_STATE_GENERIC_READ, nullptr, IID_PPV_ARGS(&fr->VertexBuffer)) < 0) |
| 308 |
return; |
| 309 |
} |
| 310 |
if (fr->IndexBuffer == nullptr || fr->IndexBufferSize < draw_data->TotalIdxCount) |
| 311 |
{ |
| 312 |
SafeRelease(fr->IndexBuffer); |
| 313 |
fr->IndexBufferSize = draw_data->TotalIdxCount + 10000; |
| 314 |
D3D12_HEAP_PROPERTIES props = {}; |
| 315 |
props.Type = D3D12_HEAP_TYPE_UPLOAD; |
| 316 |
props.CPUPageProperty = D3D12_CPU_PAGE_PROPERTY_UNKNOWN; |
| 317 |
props.MemoryPoolPreference = D3D12_MEMORY_POOL_UNKNOWN; |
| 318 |
D3D12_RESOURCE_DESC desc = {}; |
| 319 |
desc.Dimension = D3D12_RESOURCE_DIMENSION_BUFFER; |
| 320 |
desc.Width = fr->IndexBufferSize * sizeof(ImDrawIdx); |
| 321 |
desc.Height = 1; |
| 322 |
desc.DepthOrArraySize = 1; |
| 323 |
desc.MipLevels = 1; |
| 324 |
desc.Format = DXGI_FORMAT_UNKNOWN; |
| 325 |
desc.SampleDesc.Count = 1; |
| 326 |
desc.Layout = D3D12_TEXTURE_LAYOUT_ROW_MAJOR; |
| 327 |
desc.Flags = D3D12_RESOURCE_FLAG_NONE; |
| 328 |
if (bd->pd3dDevice->CreateCommittedResource(&props, D3D12_HEAP_FLAG_NONE, &desc, D3D12_RESOURCE_STATE_GENERIC_READ, nullptr, IID_PPV_ARGS(&fr->IndexBuffer)) < 0) |
| 329 |
return; |
| 330 |
} |
| 331 |
|
| 332 |
// Upload vertex/index data into a single contiguous GPU buffer |
| 333 |
// During Map() we specify a null read range (as per DX12 API, this is informational and for tooling only) |
| 334 |
void* vtx_resource, *idx_resource; |
| 335 |
D3D12_RANGE range = { 0, 0 }; |
| 336 |
if (fr->VertexBuffer->Map(0, &range, &vtx_resource) != S_OK) |
| 337 |
return; |
| 338 |
if (fr->IndexBuffer->Map(0, &range, &idx_resource) != S_OK) |
| 339 |
return; |
| 340 |
ImDrawVert* vtx_dst = (ImDrawVert*)vtx_resource; |
| 341 |
ImDrawIdx* idx_dst = (ImDrawIdx*)idx_resource; |
| 342 |
for (const ImDrawList* draw_list : draw_data->CmdLists) |
| 343 |
{ |
| 344 |
memcpy(vtx_dst, draw_list->VtxBuffer.Data, draw_list->VtxBuffer.Size * sizeof(ImDrawVert)); |
| 345 |
memcpy(idx_dst, draw_list->IdxBuffer.Data, draw_list->IdxBuffer.Size * sizeof(ImDrawIdx)); |
| 346 |
vtx_dst += draw_list->VtxBuffer.Size; |
| 347 |
idx_dst += draw_list->IdxBuffer.Size; |
| 348 |
} |
| 349 |
|
| 350 |
// During Unmap() we specify the written range (as per DX12 API, this is informational and for tooling only) |
| 351 |
range.End = (SIZE_T)((intptr_t)vtx_dst - (intptr_t)vtx_resource); |
| 352 |
IM_ASSERT(range.End == draw_data->TotalVtxCount * sizeof(ImDrawVert)); |
| 353 |
fr->VertexBuffer->Unmap(0, &range); |
| 354 |
range.End = (SIZE_T)((intptr_t)idx_dst - (intptr_t)idx_resource); |
| 355 |
IM_ASSERT(range.End == draw_data->TotalIdxCount * sizeof(ImDrawIdx)); |
| 356 |
fr->IndexBuffer->Unmap(0, &range); |
| 357 |
|
| 358 |
// Setup desired DX state |
| 359 |
ImGui_ImplDX12_SetupRenderState(draw_data, command_list, fr); |
| 360 |
|
| 361 |
// Setup render state structure (for callbacks and custom texture bindings) |
| 362 |
ImGuiPlatformIO& platform_io = ImGui::GetPlatformIO(); |
| 363 |
ImGui_ImplDX12_RenderState render_state; |
| 364 |
render_state.Device = bd->pd3dDevice; |
| 365 |
render_state.CommandList = command_list; |
| 366 |
platform_io.Renderer_RenderState = &render_state; |
| 367 |
|
| 368 |
// Render command lists |
| 369 |
// (Because we merged all buffers into a single one, we maintain our own offset into them) |
| 370 |
int global_vtx_offset = 0; |
| 371 |
int global_idx_offset = 0; |
| 372 |
ImVec2 clip_off = draw_data->DisplayPos; |
| 373 |
ImVec2 clip_scale = draw_data->FramebufferScale; |
| 374 |
for (const ImDrawList* draw_list : draw_data->CmdLists) |
| 375 |
{ |
| 376 |
for (int cmd_i = 0; cmd_i < draw_list->CmdBuffer.Size; cmd_i++) |
| 377 |
{ |
| 378 |
const ImDrawCmd* pcmd = &draw_list->CmdBuffer[cmd_i]; |
| 379 |
if (pcmd->UserCallback != nullptr) |
| 380 |
{ |
| 381 |
// User callback, registered via ImDrawList::AddCallback() |
| 382 |
// (ImDrawCallback_ResetRenderState is a special callback value used by the user to request the renderer to reset render state.) |
| 383 |
if (pcmd->UserCallback == ImDrawCallback_ResetRenderState) |
| 384 |
ImGui_ImplDX12_SetupRenderState(draw_data, command_list, fr); |
| 385 |
else |
| 386 |
pcmd->UserCallback(draw_list, pcmd); |
| 387 |
} |
| 388 |
else |
| 389 |
{ |
| 390 |
// Project scissor/clipping rectangles into framebuffer space |
| 391 |
ImVec2 clip_min((pcmd->ClipRect.x - clip_off.x) * clip_scale.x, (pcmd->ClipRect.y - clip_off.y) * clip_scale.y); |
| 392 |
ImVec2 clip_max((pcmd->ClipRect.z - clip_off.x) * clip_scale.x, (pcmd->ClipRect.w - clip_off.y) * clip_scale.y); |
| 393 |
if (clip_max.x <= clip_min.x || clip_max.y <= clip_min.y) |
| 394 |
continue; |
| 395 |
|
| 396 |
// Apply scissor/clipping rectangle |
| 397 |
const D3D12_RECT r = { (LONG)clip_min.x, (LONG)clip_min.y, (LONG)clip_max.x, (LONG)clip_max.y }; |
| 398 |
command_list->RSSetScissorRects(1, &r); |
| 399 |
|
| 400 |
// Bind texture, Draw |
| 401 |
D3D12_GPU_DESCRIPTOR_HANDLE texture_handle = {}; |
| 402 |
texture_handle.ptr = (UINT64)pcmd->GetTexID(); |
| 403 |
command_list->SetGraphicsRootDescriptorTable(1, texture_handle); |
| 404 |
command_list->DrawIndexedInstanced(pcmd->ElemCount, 1, pcmd->IdxOffset + global_idx_offset, pcmd->VtxOffset + global_vtx_offset, 0); |
| 405 |
} |
| 406 |
} |
| 407 |
global_idx_offset += draw_list->IdxBuffer.Size; |
| 408 |
global_vtx_offset += draw_list->VtxBuffer.Size; |
| 409 |
} |
| 410 |
platform_io.Renderer_RenderState = nullptr; |
| 411 |
} |
| 412 |
|
| 413 |
static void ImGui_ImplDX12_DestroyTexture(ImTextureData* tex) |
| 414 |
{ |
| 415 |
ImGui_ImplDX12_Texture* backend_tex = (ImGui_ImplDX12_Texture*)tex->BackendUserData; |
| 416 |
if (backend_tex == nullptr) |
| 417 |
return; |
| 418 |
IM_ASSERT(backend_tex->hFontSrvGpuDescHandle.ptr == (UINT64)tex->TexID); |
| 419 |
ImGui_ImplDX12_Data* bd = ImGui_ImplDX12_GetBackendData(); |
| 420 |
bd->InitInfo.SrvDescriptorFreeFn(&bd->InitInfo, backend_tex->hFontSrvCpuDescHandle, backend_tex->hFontSrvGpuDescHandle); |
| 421 |
SafeRelease(backend_tex->pTextureResource); |
| 422 |
backend_tex->hFontSrvCpuDescHandle.ptr = 0; |
| 423 |
backend_tex->hFontSrvGpuDescHandle.ptr = 0; |
| 424 |
IM_DELETE(backend_tex); |
| 425 |
|
| 426 |
// Clear identifiers and mark as destroyed (in order to allow e.g. calling InvalidateDeviceObjects while running) |
| 427 |
tex->SetTexID(ImTextureID_Invalid); |
| 428 |
tex->SetStatus(ImTextureStatus_Destroyed); |
| 429 |
tex->BackendUserData = nullptr; |
| 430 |
} |
| 431 |
|
| 432 |
void ImGui_ImplDX12_UpdateTexture(ImTextureData* tex) |
| 433 |
{ |
| 434 |
ImGui_ImplDX12_Data* bd = ImGui_ImplDX12_GetBackendData(); |
| 435 |
bool need_barrier_before_copy = true; // Do we need a resource barrier before we copy new data in? |
| 436 |
|
| 437 |
if (tex->Status == ImTextureStatus_WantCreate) |
| 438 |
{ |
| 439 |
// Create and upload new texture to graphics system |
| 440 |
//IMGUI_DEBUG_LOG("UpdateTexture #%03d: WantCreate %dx%d\n", tex->UniqueID, tex->Width, tex->Height); |
| 441 |
IM_ASSERT(tex->TexID == ImTextureID_Invalid && tex->BackendUserData == nullptr); |
| 442 |
IM_ASSERT(tex->Format == ImTextureFormat_RGBA32); |
| 443 |
ImGui_ImplDX12_Texture* backend_tex = IM_NEW(ImGui_ImplDX12_Texture)(); |
| 444 |
bd->InitInfo.SrvDescriptorAllocFn(&bd->InitInfo, &backend_tex->hFontSrvCpuDescHandle, &backend_tex->hFontSrvGpuDescHandle); // Allocate a desctriptor handle |
| 445 |
|
| 446 |
D3D12_HEAP_PROPERTIES props = {}; |
| 447 |
props.Type = D3D12_HEAP_TYPE_DEFAULT; |
| 448 |
props.CPUPageProperty = D3D12_CPU_PAGE_PROPERTY_UNKNOWN; |
| 449 |
props.MemoryPoolPreference = D3D12_MEMORY_POOL_UNKNOWN; |
| 450 |
|
| 451 |
D3D12_RESOURCE_DESC desc; |
| 452 |
ZeroMemory(&desc, sizeof(desc)); |
| 453 |
desc.Dimension = D3D12_RESOURCE_DIMENSION_TEXTURE2D; |
| 454 |
desc.Alignment = 0; |
| 455 |
desc.Width = tex->Width; |
| 456 |
desc.Height = tex->Height; |
| 457 |
desc.DepthOrArraySize = 1; |
| 458 |
desc.MipLevels = 1; |
| 459 |
desc.Format = DXGI_FORMAT_R8G8B8A8_UNORM; |
| 460 |
desc.SampleDesc.Count = 1; |
| 461 |
desc.SampleDesc.Quality = 0; |
| 462 |
desc.Layout = D3D12_TEXTURE_LAYOUT_UNKNOWN; |
| 463 |
desc.Flags = D3D12_RESOURCE_FLAG_NONE; |
| 464 |
|
| 465 |
ID3D12Resource* pTexture = nullptr; |
| 466 |
bd->pd3dDevice->CreateCommittedResource(&props, D3D12_HEAP_FLAG_NONE, &desc, |
| 467 |
D3D12_RESOURCE_STATE_COPY_DEST, nullptr, IID_PPV_ARGS(&pTexture)); |
| 468 |
|
| 469 |
// Create SRV |
| 470 |
D3D12_SHADER_RESOURCE_VIEW_DESC srvDesc; |
| 471 |
ZeroMemory(&srvDesc, sizeof(srvDesc)); |
| 472 |
srvDesc.Format = DXGI_FORMAT_R8G8B8A8_UNORM; |
| 473 |
srvDesc.ViewDimension = D3D12_SRV_DIMENSION_TEXTURE2D; |
| 474 |
srvDesc.Texture2D.MipLevels = desc.MipLevels; |
| 475 |
srvDesc.Texture2D.MostDetailedMip = 0; |
| 476 |
srvDesc.Shader4ComponentMapping = D3D12_DEFAULT_SHADER_4_COMPONENT_MAPPING; |
| 477 |
bd->pd3dDevice->CreateShaderResourceView(pTexture, &srvDesc, backend_tex->hFontSrvCpuDescHandle); |
| 478 |
SafeRelease(backend_tex->pTextureResource); |
| 479 |
backend_tex->pTextureResource = pTexture; |
| 480 |
|
| 481 |
// Store identifiers |
| 482 |
tex->SetTexID((ImTextureID)backend_tex->hFontSrvGpuDescHandle.ptr); |
| 483 |
tex->BackendUserData = backend_tex; |
| 484 |
need_barrier_before_copy = false; // Because this is a newly-created texture it will be in D3D12_RESOURCE_STATE_COMMON and thus we don't need a barrier |
| 485 |
// We don't set tex->Status to ImTextureStatus_OK to let the code fallthrough below. |
| 486 |
} |
| 487 |
|
| 488 |
if (tex->Status == ImTextureStatus_WantCreate || tex->Status == ImTextureStatus_WantUpdates) |
| 489 |
{ |
| 490 |
ImGui_ImplDX12_Texture* backend_tex = (ImGui_ImplDX12_Texture*)tex->BackendUserData; |
| 491 |
IM_ASSERT(tex->Format == ImTextureFormat_RGBA32); |
| 492 |
|
| 493 |
// We could use the smaller rect on _WantCreate but using the full rect allows us to clear the texture. |
| 494 |
// FIXME-OPT: Uploading single box even when using ImTextureStatus_WantUpdates. Could use tex->Updates[] |
| 495 |
// - Copy all blocks contiguously in upload buffer. |
| 496 |
// - Barrier before copy, submit all CopyTextureRegion(), barrier after copy. |
| 497 |
const int upload_x = (tex->Status == ImTextureStatus_WantCreate) ? 0 : tex->UpdateRect.x; |
| 498 |
const int upload_y = (tex->Status == ImTextureStatus_WantCreate) ? 0 : tex->UpdateRect.y; |
| 499 |
const int upload_w = (tex->Status == ImTextureStatus_WantCreate) ? tex->Width : tex->UpdateRect.w; |
| 500 |
const int upload_h = (tex->Status == ImTextureStatus_WantCreate) ? tex->Height : tex->UpdateRect.h; |
| 501 |
|
| 502 |
// Update full texture or selected blocks. We only ever write to textures regions which have never been used before! |
| 503 |
// This backend choose to use tex->UpdateRect but you can use tex->Updates[] to upload individual regions. |
| 504 |
UINT upload_pitch_src = upload_w * tex->BytesPerPixel; |
| 505 |
UINT upload_pitch_dst = (upload_pitch_src + D3D12_TEXTURE_DATA_PITCH_ALIGNMENT - 1u) & ~(D3D12_TEXTURE_DATA_PITCH_ALIGNMENT - 1u); |
| 506 |
UINT upload_size = upload_pitch_dst * upload_h; |
| 507 |
|
| 508 |
D3D12_RESOURCE_DESC desc; |
| 509 |
ZeroMemory(&desc, sizeof(desc)); |
| 510 |
desc.Dimension = D3D12_RESOURCE_DIMENSION_BUFFER; |
| 511 |
desc.Alignment = 0; |
| 512 |
desc.Width = upload_size; |
| 513 |
desc.Height = 1; |
| 514 |
desc.DepthOrArraySize = 1; |
| 515 |
desc.MipLevels = 1; |
| 516 |
desc.Format = DXGI_FORMAT_UNKNOWN; |
| 517 |
desc.SampleDesc.Count = 1; |
| 518 |
desc.SampleDesc.Quality = 0; |
| 519 |
desc.Layout = D3D12_TEXTURE_LAYOUT_ROW_MAJOR; |
| 520 |
desc.Flags = D3D12_RESOURCE_FLAG_NONE; |
| 521 |
|
| 522 |
D3D12_HEAP_PROPERTIES props; |
| 523 |
memset(&props, 0, sizeof(D3D12_HEAP_PROPERTIES)); |
| 524 |
props.Type = D3D12_HEAP_TYPE_UPLOAD; |
| 525 |
props.CPUPageProperty = D3D12_CPU_PAGE_PROPERTY_UNKNOWN; |
| 526 |
props.MemoryPoolPreference = D3D12_MEMORY_POOL_UNKNOWN; |
| 527 |
|
| 528 |
// FIXME-OPT: Can upload buffer be reused? |
| 529 |
ID3D12Resource* uploadBuffer = nullptr; |
| 530 |
HRESULT hr = bd->pd3dDevice->CreateCommittedResource(&props, D3D12_HEAP_FLAG_NONE, &desc, |
| 531 |
D3D12_RESOURCE_STATE_GENERIC_READ, nullptr, IID_PPV_ARGS(&uploadBuffer)); |
| 532 |
IM_ASSERT(SUCCEEDED(hr)); |
| 533 |
|
| 534 |
// Create temporary command list and execute immediately |
| 535 |
ID3D12Fence* fence = nullptr; |
| 536 |
hr = bd->pd3dDevice->CreateFence(0, D3D12_FENCE_FLAG_NONE, IID_PPV_ARGS(&fence)); |
| 537 |
IM_ASSERT(SUCCEEDED(hr)); |
| 538 |
|
| 539 |
HANDLE event = ::CreateEvent(0, 0, 0, 0); |
| 540 |
IM_ASSERT(event != nullptr); |
| 541 |
|
| 542 |
// FIXME-OPT: Create once and reuse? |
| 543 |
ID3D12CommandAllocator* cmdAlloc = nullptr; |
| 544 |
hr = bd->pd3dDevice->CreateCommandAllocator(D3D12_COMMAND_LIST_TYPE_DIRECT, IID_PPV_ARGS(&cmdAlloc)); |
| 545 |
IM_ASSERT(SUCCEEDED(hr)); |
| 546 |
|
| 547 |
// FIXME-OPT: Can be use the one from user? (pass ID3D12GraphicsCommandList* to ImGui_ImplDX12_UpdateTextures) |
| 548 |
ID3D12GraphicsCommandList* cmdList = nullptr; |
| 549 |
hr = bd->pd3dDevice->CreateCommandList(0, D3D12_COMMAND_LIST_TYPE_DIRECT, cmdAlloc, nullptr, IID_PPV_ARGS(&cmdList)); |
| 550 |
IM_ASSERT(SUCCEEDED(hr)); |
| 551 |
|
| 552 |
// Copy to upload buffer |
| 553 |
void* mapped = nullptr; |
| 554 |
D3D12_RANGE range = { 0, upload_size }; |
| 555 |
hr = uploadBuffer->Map(0, &range, &mapped); |
| 556 |
IM_ASSERT(SUCCEEDED(hr)); |
| 557 |
for (int y = 0; y < upload_h; y++) |
| 558 |
memcpy((void*)((uintptr_t)mapped + y * upload_pitch_dst), tex->GetPixelsAt(upload_x, upload_y + y), upload_pitch_src); |
| 559 |
uploadBuffer->Unmap(0, &range); |
| 560 |
|
| 561 |
if (need_barrier_before_copy) |
| 562 |
{ |
| 563 |
D3D12_RESOURCE_BARRIER barrier = {}; |
| 564 |
barrier.Type = D3D12_RESOURCE_BARRIER_TYPE_TRANSITION; |
| 565 |
barrier.Flags = D3D12_RESOURCE_BARRIER_FLAG_NONE; |
| 566 |
barrier.Transition.pResource = backend_tex->pTextureResource; |
| 567 |
barrier.Transition.Subresource = D3D12_RESOURCE_BARRIER_ALL_SUBRESOURCES; |
| 568 |
barrier.Transition.StateBefore = D3D12_RESOURCE_STATE_PIXEL_SHADER_RESOURCE; |
| 569 |
barrier.Transition.StateAfter = D3D12_RESOURCE_STATE_COPY_DEST; |
| 570 |
cmdList->ResourceBarrier(1, &barrier); |
| 571 |
} |
| 572 |
|
| 573 |
D3D12_TEXTURE_COPY_LOCATION srcLocation = {}; |
| 574 |
D3D12_TEXTURE_COPY_LOCATION dstLocation = {}; |
| 575 |
{ |
| 576 |
srcLocation.pResource = uploadBuffer; |
| 577 |
srcLocation.Type = D3D12_TEXTURE_COPY_TYPE_PLACED_FOOTPRINT; |
| 578 |
srcLocation.PlacedFootprint.Footprint.Format = DXGI_FORMAT_R8G8B8A8_UNORM; |
| 579 |
srcLocation.PlacedFootprint.Footprint.Width = upload_w; |
| 580 |
srcLocation.PlacedFootprint.Footprint.Height = upload_h; |
| 581 |
srcLocation.PlacedFootprint.Footprint.Depth = 1; |
| 582 |
srcLocation.PlacedFootprint.Footprint.RowPitch = upload_pitch_dst; |
| 583 |
dstLocation.pResource = backend_tex->pTextureResource; |
| 584 |
dstLocation.Type = D3D12_TEXTURE_COPY_TYPE_SUBRESOURCE_INDEX; |
| 585 |
dstLocation.SubresourceIndex = 0; |
| 586 |
} |
| 587 |
cmdList->CopyTextureRegion(&dstLocation, upload_x, upload_y, 0, &srcLocation, nullptr); |
| 588 |
|
| 589 |
{ |
| 590 |
D3D12_RESOURCE_BARRIER barrier = {}; |
| 591 |
barrier.Type = D3D12_RESOURCE_BARRIER_TYPE_TRANSITION; |
| 592 |
barrier.Flags = D3D12_RESOURCE_BARRIER_FLAG_NONE; |
| 593 |
barrier.Transition.pResource = backend_tex->pTextureResource; |
| 594 |
barrier.Transition.Subresource = D3D12_RESOURCE_BARRIER_ALL_SUBRESOURCES; |
| 595 |
barrier.Transition.StateBefore = D3D12_RESOURCE_STATE_COPY_DEST; |
| 596 |
barrier.Transition.StateAfter = D3D12_RESOURCE_STATE_PIXEL_SHADER_RESOURCE; |
| 597 |
cmdList->ResourceBarrier(1, &barrier); |
| 598 |
} |
| 599 |
|
| 600 |
hr = cmdList->Close(); |
| 601 |
IM_ASSERT(SUCCEEDED(hr)); |
| 602 |
|
| 603 |
ID3D12CommandQueue* cmdQueue = bd->pCommandQueue; |
| 604 |
cmdQueue->ExecuteCommandLists(1, (ID3D12CommandList* const*)&cmdList); |
| 605 |
hr = cmdQueue->Signal(fence, 1); |
| 606 |
IM_ASSERT(SUCCEEDED(hr)); |
| 607 |
|
| 608 |
// FIXME-OPT: Suboptimal? |
| 609 |
// - To remove this may need to create NumFramesInFlight x ImGui_ImplDX12_FrameContext in backend data (mimick docking version) |
| 610 |
// - Store per-frame in flight: upload buffer? |
| 611 |
// - Where do cmdList and cmdAlloc fit? |
| 612 |
fence->SetEventOnCompletion(1, event); |
| 613 |
::WaitForSingleObject(event, INFINITE); |
| 614 |
|
| 615 |
cmdList->Release(); |
| 616 |
cmdAlloc->Release(); |
| 617 |
::CloseHandle(event); |
| 618 |
fence->Release(); |
| 619 |
uploadBuffer->Release(); |
| 620 |
tex->SetStatus(ImTextureStatus_OK); |
| 621 |
} |
| 622 |
|
| 623 |
if (tex->Status == ImTextureStatus_WantDestroy && tex->UnusedFrames >= (int)bd->numFramesInFlight) |
| 624 |
ImGui_ImplDX12_DestroyTexture(tex); |
| 625 |
} |
| 626 |
|
| 627 |
bool ImGui_ImplDX12_CreateDeviceObjects() |
| 628 |
{ |
| 629 |
ImGui_ImplDX12_Data* bd = ImGui_ImplDX12_GetBackendData(); |
| 630 |
if (!bd || !bd->pd3dDevice) |
| 631 |
return false; |
| 632 |
if (bd->pPipelineState) |
| 633 |
ImGui_ImplDX12_InvalidateDeviceObjects(); |
| 634 |
|
| 635 |
// Create the root signature |
| 636 |
{ |
| 637 |
D3D12_DESCRIPTOR_RANGE descRange = {}; |
| 638 |
descRange.RangeType = D3D12_DESCRIPTOR_RANGE_TYPE_SRV; |
| 639 |
descRange.NumDescriptors = 1; |
| 640 |
descRange.BaseShaderRegister = 0; |
| 641 |
descRange.RegisterSpace = 0; |
| 642 |
descRange.OffsetInDescriptorsFromTableStart = 0; |
| 643 |
|
| 644 |
D3D12_ROOT_PARAMETER param[2] = {}; |
| 645 |
|
| 646 |
param[0].ParameterType = D3D12_ROOT_PARAMETER_TYPE_32BIT_CONSTANTS; |
| 647 |
param[0].Constants.ShaderRegister = 0; |
| 648 |
param[0].Constants.RegisterSpace = 0; |
| 649 |
param[0].Constants.Num32BitValues = 16; |
| 650 |
param[0].ShaderVisibility = D3D12_SHADER_VISIBILITY_VERTEX; |
| 651 |
|
| 652 |
param[1].ParameterType = D3D12_ROOT_PARAMETER_TYPE_DESCRIPTOR_TABLE; |
| 653 |
param[1].DescriptorTable.NumDescriptorRanges = 1; |
| 654 |
param[1].DescriptorTable.pDescriptorRanges = &descRange; |
| 655 |
param[1].ShaderVisibility = D3D12_SHADER_VISIBILITY_PIXEL; |
| 656 |
|
| 657 |
// Bilinear sampling is required by default. Set 'io.Fonts->Flags |= ImFontAtlasFlags_NoBakedLines' or 'style.AntiAliasedLinesUseTex = false' to allow point/nearest sampling. |
| 658 |
D3D12_STATIC_SAMPLER_DESC staticSampler = {}; |
| 659 |
staticSampler.Filter = D3D12_FILTER_MIN_MAG_MIP_LINEAR; |
| 660 |
staticSampler.AddressU = D3D12_TEXTURE_ADDRESS_MODE_CLAMP; |
| 661 |
staticSampler.AddressV = D3D12_TEXTURE_ADDRESS_MODE_CLAMP; |
| 662 |
staticSampler.AddressW = D3D12_TEXTURE_ADDRESS_MODE_CLAMP; |
| 663 |
staticSampler.MipLODBias = 0.f; |
| 664 |
staticSampler.MaxAnisotropy = 0; |
| 665 |
staticSampler.ComparisonFunc = D3D12_COMPARISON_FUNC_ALWAYS; |
| 666 |
staticSampler.BorderColor = D3D12_STATIC_BORDER_COLOR_TRANSPARENT_BLACK; |
| 667 |
staticSampler.MinLOD = 0.f; |
| 668 |
staticSampler.MaxLOD = D3D12_FLOAT32_MAX; |
| 669 |
staticSampler.ShaderRegister = 0; |
| 670 |
staticSampler.RegisterSpace = 0; |
| 671 |
staticSampler.ShaderVisibility = D3D12_SHADER_VISIBILITY_PIXEL; |
| 672 |
|
| 673 |
D3D12_ROOT_SIGNATURE_DESC desc = {}; |
| 674 |
desc.NumParameters = _countof(param); |
| 675 |
desc.pParameters = param; |
| 676 |
desc.NumStaticSamplers = 1; |
| 677 |
desc.pStaticSamplers = &staticSampler; |
| 678 |
desc.Flags = |
| 679 |
D3D12_ROOT_SIGNATURE_FLAG_ALLOW_INPUT_ASSEMBLER_INPUT_LAYOUT | |
| 680 |
D3D12_ROOT_SIGNATURE_FLAG_DENY_HULL_SHADER_ROOT_ACCESS | |
| 681 |
D3D12_ROOT_SIGNATURE_FLAG_DENY_DOMAIN_SHADER_ROOT_ACCESS | |
| 682 |
D3D12_ROOT_SIGNATURE_FLAG_DENY_GEOMETRY_SHADER_ROOT_ACCESS; |
| 683 |
|
| 684 |
// Load d3d12.dll and D3D12SerializeRootSignature() function address dynamically to facilitate using with D3D12On7. |
| 685 |
// See if any version of d3d12.dll is already loaded in the process. If so, give preference to that. |
| 686 |
static HINSTANCE d3d12_dll = ::GetModuleHandleA("d3d12.dll"); |
| 687 |
if (d3d12_dll == nullptr) |
| 688 |
{ |
| 689 |
// Attempt to load d3d12.dll from local directories. This will only succeed if |
| 690 |
// (1) the current OS is Windows 7, and |
| 691 |
// (2) there exists a version of d3d12.dll for Windows 7 (D3D12On7) in one of the following directories. |
| 692 |
// See https://github.com/ocornut/imgui/pull/3696 for details. |
| 693 |
const char* localD3d12Paths[] = { ".\\d3d12.dll", ".\\d3d12on7\\d3d12.dll", ".\\12on7\\d3d12.dll" }; // A. current directory, B. used by some games, C. used in Microsoft D3D12On7 sample |
| 694 |
for (int i = 0; i < IM_ARRAYSIZE(localD3d12Paths); i++) |
| 695 |
if ((d3d12_dll = ::LoadLibraryA(localD3d12Paths[i])) != nullptr) |
| 696 |
break; |
| 697 |
|
| 698 |
// If failed, we are on Windows >= 10. |
| 699 |
if (d3d12_dll == nullptr) |
| 700 |
d3d12_dll = ::LoadLibraryA("d3d12.dll"); |
| 701 |
|
| 702 |
if (d3d12_dll == nullptr) |
| 703 |
return false; |
| 704 |
} |
| 705 |
|
| 706 |
_PFN_D3D12_SERIALIZE_ROOT_SIGNATURE D3D12SerializeRootSignatureFn = (_PFN_D3D12_SERIALIZE_ROOT_SIGNATURE)(void*)::GetProcAddress(d3d12_dll, "D3D12SerializeRootSignature"); |
| 707 |
if (D3D12SerializeRootSignatureFn == nullptr) |
| 708 |
return false; |
| 709 |
|
| 710 |
ID3DBlob* blob = nullptr; |
| 711 |
if (D3D12SerializeRootSignatureFn(&desc, D3D_ROOT_SIGNATURE_VERSION_1, &blob, nullptr) != S_OK) |
| 712 |
return false; |
| 713 |
|
| 714 |
bd->pd3dDevice->CreateRootSignature(0, blob->GetBufferPointer(), blob->GetBufferSize(), IID_PPV_ARGS(&bd->pRootSignature)); |
| 715 |
blob->Release(); |
| 716 |
} |
| 717 |
|
| 718 |
// By using D3DCompile() from <d3dcompiler.h> / d3dcompiler.lib, we introduce a dependency to a given version of d3dcompiler_XX.dll (see D3DCOMPILER_DLL_A) |
| 719 |
// If you would like to use this DX12 sample code but remove this dependency you can: |
| 720 |
// 1) compile once, save the compiled shader blobs into a file or source code and assign them to psoDesc.VS/PS [preferred solution] |
| 721 |
// 2) use code to detect any version of the DLL and grab a pointer to D3DCompile from the DLL. |
| 722 |
// See https://github.com/ocornut/imgui/pull/638 for sources and details. |
| 723 |
|
| 724 |
D3D12_GRAPHICS_PIPELINE_STATE_DESC psoDesc = {}; |
| 725 |
psoDesc.NodeMask = 1; |
| 726 |
psoDesc.PrimitiveTopologyType = D3D12_PRIMITIVE_TOPOLOGY_TYPE_TRIANGLE; |
| 727 |
psoDesc.pRootSignature = bd->pRootSignature; |
| 728 |
psoDesc.SampleMask = UINT_MAX; |
| 729 |
psoDesc.NumRenderTargets = 1; |
| 730 |
psoDesc.RTVFormats[0] = bd->RTVFormat; |
| 731 |
psoDesc.DSVFormat = bd->DSVFormat; |
| 732 |
psoDesc.SampleDesc.Count = 1; |
| 733 |
psoDesc.Flags = D3D12_PIPELINE_STATE_FLAG_NONE; |
| 734 |
|
| 735 |
ID3DBlob* vertexShaderBlob; |
| 736 |
ID3DBlob* pixelShaderBlob; |
| 737 |
|
| 738 |
// Create the vertex shader |
| 739 |
{ |
| 740 |
static const char* vertexShader = |
| 741 |
"cbuffer vertexBuffer : register(b0) \ |
| 742 |
{\ |
| 743 |
float4x4 ProjectionMatrix; \ |
| 744 |
};\ |
| 745 |
struct VS_INPUT\ |
| 746 |
{\ |
| 747 |
float2 pos : POSITION;\ |
| 748 |
float4 col : COLOR0;\ |
| 749 |
float2 uv : TEXCOORD0;\ |
| 750 |
};\ |
| 751 |
\ |
| 752 |
struct PS_INPUT\ |
| 753 |
{\ |
| 754 |
float4 pos : SV_POSITION;\ |
| 755 |
float4 col : COLOR0;\ |
| 756 |
float2 uv : TEXCOORD0;\ |
| 757 |
};\ |
| 758 |
\ |
| 759 |
PS_INPUT main(VS_INPUT input)\ |
| 760 |
{\ |
| 761 |
PS_INPUT output;\ |
| 762 |
output.pos = mul( ProjectionMatrix, float4(input.pos.xy, 0.f, 1.f));\ |
| 763 |
output.col = input.col;\ |
| 764 |
output.uv = input.uv;\ |
| 765 |
return output;\ |
| 766 |
}"; |
| 767 |
|
| 768 |
if (FAILED(D3DCompile(vertexShader, strlen(vertexShader), nullptr, nullptr, nullptr, "main", "vs_5_0", 0, 0, &vertexShaderBlob, nullptr))) |
| 769 |
return false; // NB: Pass ID3DBlob* pErrorBlob to D3DCompile() to get error showing in (const char*)pErrorBlob->GetBufferPointer(). Make sure to Release() the blob! |
| 770 |
psoDesc.VS = { vertexShaderBlob->GetBufferPointer(), vertexShaderBlob->GetBufferSize() }; |
| 771 |
|
| 772 |
// Create the input layout |
| 773 |
static D3D12_INPUT_ELEMENT_DESC local_layout[] = |
| 774 |
{ |
| 775 |
{ "POSITION", 0, DXGI_FORMAT_R32G32_FLOAT, 0, (UINT)offsetof(ImDrawVert, pos), D3D12_INPUT_CLASSIFICATION_PER_VERTEX_DATA, 0 }, |
| 776 |
{ "TEXCOORD", 0, DXGI_FORMAT_R32G32_FLOAT, 0, (UINT)offsetof(ImDrawVert, uv), D3D12_INPUT_CLASSIFICATION_PER_VERTEX_DATA, 0 }, |
| 777 |
{ "COLOR", 0, DXGI_FORMAT_R8G8B8A8_UNORM, 0, (UINT)offsetof(ImDrawVert, col), D3D12_INPUT_CLASSIFICATION_PER_VERTEX_DATA, 0 }, |
| 778 |
}; |
| 779 |
psoDesc.InputLayout = { local_layout, 3 }; |
| 780 |
} |
| 781 |
|
| 782 |
// Create the pixel shader |
| 783 |
{ |
| 784 |
static const char* pixelShader = |
| 785 |
"struct PS_INPUT\ |
| 786 |
{\ |
| 787 |
float4 pos : SV_POSITION;\ |
| 788 |
float4 col : COLOR0;\ |
| 789 |
float2 uv : TEXCOORD0;\ |
| 790 |
};\ |
| 791 |
SamplerState sampler0 : register(s0);\ |
| 792 |
Texture2D texture0 : register(t0);\ |
| 793 |
\ |
| 794 |
float4 main(PS_INPUT input) : SV_Target\ |
| 795 |
{\ |
| 796 |
float4 out_col = input.col * texture0.Sample(sampler0, input.uv); \ |
| 797 |
return out_col; \ |
| 798 |
}"; |
| 799 |
|
| 800 |
if (FAILED(D3DCompile(pixelShader, strlen(pixelShader), nullptr, nullptr, nullptr, "main", "ps_5_0", 0, 0, &pixelShaderBlob, nullptr))) |
| 801 |
{ |
| 802 |
vertexShaderBlob->Release(); |
| 803 |
return false; // NB: Pass ID3DBlob* pErrorBlob to D3DCompile() to get error showing in (const char*)pErrorBlob->GetBufferPointer(). Make sure to Release() the blob! |
| 804 |
} |
| 805 |
psoDesc.PS = { pixelShaderBlob->GetBufferPointer(), pixelShaderBlob->GetBufferSize() }; |
| 806 |
} |
| 807 |
|
| 808 |
// Create the blending setup |
| 809 |
{ |
| 810 |
D3D12_BLEND_DESC& desc = psoDesc.BlendState; |
| 811 |
desc.AlphaToCoverageEnable = false; |
| 812 |
desc.RenderTarget[0].BlendEnable = true; |
| 813 |
desc.RenderTarget[0].SrcBlend = D3D12_BLEND_SRC_ALPHA; |
| 814 |
desc.RenderTarget[0].DestBlend = D3D12_BLEND_INV_SRC_ALPHA; |
| 815 |
desc.RenderTarget[0].BlendOp = D3D12_BLEND_OP_ADD; |
| 816 |
desc.RenderTarget[0].SrcBlendAlpha = D3D12_BLEND_ONE; |
| 817 |
desc.RenderTarget[0].DestBlendAlpha = D3D12_BLEND_INV_SRC_ALPHA; |
| 818 |
desc.RenderTarget[0].BlendOpAlpha = D3D12_BLEND_OP_ADD; |
| 819 |
desc.RenderTarget[0].RenderTargetWriteMask = D3D12_COLOR_WRITE_ENABLE_ALL; |
| 820 |
} |
| 821 |
|
| 822 |
// Create the rasterizer state |
| 823 |
{ |
| 824 |
D3D12_RASTERIZER_DESC& desc = psoDesc.RasterizerState; |
| 825 |
desc.FillMode = D3D12_FILL_MODE_SOLID; |
| 826 |
desc.CullMode = D3D12_CULL_MODE_NONE; |
| 827 |
desc.FrontCounterClockwise = FALSE; |
| 828 |
desc.DepthBias = D3D12_DEFAULT_DEPTH_BIAS; |
| 829 |
desc.DepthBiasClamp = D3D12_DEFAULT_DEPTH_BIAS_CLAMP; |
| 830 |
desc.SlopeScaledDepthBias = D3D12_DEFAULT_SLOPE_SCALED_DEPTH_BIAS; |
| 831 |
desc.DepthClipEnable = true; |
| 832 |
desc.MultisampleEnable = FALSE; |
| 833 |
desc.AntialiasedLineEnable = FALSE; |
| 834 |
desc.ForcedSampleCount = 0; |
| 835 |
desc.ConservativeRaster = D3D12_CONSERVATIVE_RASTERIZATION_MODE_OFF; |
| 836 |
} |
| 837 |
|
| 838 |
// Create depth-stencil State |
| 839 |
{ |
| 840 |
D3D12_DEPTH_STENCIL_DESC& desc = psoDesc.DepthStencilState; |
| 841 |
desc.DepthEnable = false; |
| 842 |
desc.DepthWriteMask = D3D12_DEPTH_WRITE_MASK_ALL; |
| 843 |
desc.DepthFunc = D3D12_COMPARISON_FUNC_ALWAYS; |
| 844 |
desc.StencilEnable = false; |
| 845 |
desc.FrontFace.StencilFailOp = desc.FrontFace.StencilDepthFailOp = desc.FrontFace.StencilPassOp = D3D12_STENCIL_OP_KEEP; |
| 846 |
desc.FrontFace.StencilFunc = D3D12_COMPARISON_FUNC_ALWAYS; |
| 847 |
desc.BackFace = desc.FrontFace; |
| 848 |
} |
| 849 |
|
| 850 |
HRESULT result_pipeline_state = bd->pd3dDevice->CreateGraphicsPipelineState(&psoDesc, IID_PPV_ARGS(&bd->pPipelineState)); |
| 851 |
vertexShaderBlob->Release(); |
| 852 |
pixelShaderBlob->Release(); |
| 853 |
if (result_pipeline_state != S_OK) |
| 854 |
return false; |
| 855 |
|
| 856 |
return true; |
| 857 |
} |
| 858 |
|
| 859 |
static void ImGui_ImplDX12_DestroyRenderBuffers(ImGui_ImplDX12_RenderBuffers* render_buffers) |
| 860 |
{ |
| 861 |
SafeRelease(render_buffers->IndexBuffer); |
| 862 |
SafeRelease(render_buffers->VertexBuffer); |
| 863 |
render_buffers->IndexBufferSize = render_buffers->VertexBufferSize = 0; |
| 864 |
} |
| 865 |
|
| 866 |
void ImGui_ImplDX12_InvalidateDeviceObjects() |
| 867 |
{ |
| 868 |
ImGui_ImplDX12_Data* bd = ImGui_ImplDX12_GetBackendData(); |
| 869 |
if (!bd || !bd->pd3dDevice) |
| 870 |
return; |
| 871 |
|
| 872 |
if (bd->commandQueueOwned) |
| 873 |
SafeRelease(bd->pCommandQueue); |
| 874 |
bd->commandQueueOwned = false; |
| 875 |
SafeRelease(bd->pRootSignature); |
| 876 |
SafeRelease(bd->pPipelineState); |
| 877 |
|
| 878 |
// Destroy all textures |
| 879 |
for (ImTextureData* tex : ImGui::GetPlatformIO().Textures) |
| 880 |
if (tex->RefCount == 1) |
| 881 |
ImGui_ImplDX12_DestroyTexture(tex); |
| 882 |
} |
| 883 |
|
| 884 |
#ifndef IMGUI_DISABLE_OBSOLETE_FUNCTIONS |
| 885 |
static void ImGui_ImplDX12_InitLegacySingleDescriptorMode(ImGui_ImplDX12_InitInfo* init_info) |
| 886 |
{ |
| 887 |
// Wrap legacy behavior of passing space for a single descriptor |
| 888 |
IM_ASSERT(init_info->LegacySingleSrvCpuDescriptor.ptr != 0 && init_info->LegacySingleSrvGpuDescriptor.ptr != 0); |
| 889 |
init_info->SrvDescriptorAllocFn = [](ImGui_ImplDX12_InitInfo*, D3D12_CPU_DESCRIPTOR_HANDLE* out_cpu_handle, D3D12_GPU_DESCRIPTOR_HANDLE* out_gpu_handle) |
| 890 |
{ |
| 891 |
ImGui_ImplDX12_Data* bd = ImGui_ImplDX12_GetBackendData(); |
| 892 |
IM_ASSERT(bd->LegacySingleDescriptorUsed == false && "Only 1 simultaneous texture allowed with legacy ImGui_ImplDX12_Init() signature!"); |
| 893 |
*out_cpu_handle = bd->InitInfo.LegacySingleSrvCpuDescriptor; |
| 894 |
*out_gpu_handle = bd->InitInfo.LegacySingleSrvGpuDescriptor; |
| 895 |
bd->LegacySingleDescriptorUsed = true; |
| 896 |
}; |
| 897 |
init_info->SrvDescriptorFreeFn = [](ImGui_ImplDX12_InitInfo*, D3D12_CPU_DESCRIPTOR_HANDLE, D3D12_GPU_DESCRIPTOR_HANDLE) |
| 898 |
{ |
| 899 |
ImGui_ImplDX12_Data* bd = ImGui_ImplDX12_GetBackendData(); |
| 900 |
IM_ASSERT(bd->LegacySingleDescriptorUsed == true); |
| 901 |
bd->LegacySingleDescriptorUsed = false; |
| 902 |
}; |
| 903 |
} |
| 904 |
#endif |
| 905 |
|
| 906 |
bool ImGui_ImplDX12_Init(ImGui_ImplDX12_InitInfo* init_info) |
| 907 |
{ |
| 908 |
ImGuiIO& io = ImGui::GetIO(); |
| 909 |
IMGUI_CHECKVERSION(); |
| 910 |
IM_ASSERT(io.BackendRendererUserData == nullptr && "Already initialized a renderer backend!"); |
| 911 |
|
| 912 |
// Setup backend capabilities flags |
| 913 |
ImGui_ImplDX12_Data* bd = IM_NEW(ImGui_ImplDX12_Data)(); |
| 914 |
bd->InitInfo = *init_info; // Deep copy |
| 915 |
init_info = &bd->InitInfo; |
| 916 |
|
| 917 |
bd->pd3dDevice = init_info->Device; |
| 918 |
IM_ASSERT(init_info->CommandQueue != NULL); |
| 919 |
bd->pCommandQueue = init_info->CommandQueue; |
| 920 |
bd->RTVFormat = init_info->RTVFormat; |
| 921 |
bd->DSVFormat = init_info->DSVFormat; |
| 922 |
bd->numFramesInFlight = init_info->NumFramesInFlight; |
| 923 |
bd->pd3dSrvDescHeap = init_info->SrvDescriptorHeap; |
| 924 |
|
| 925 |
io.BackendRendererUserData = (void*)bd; |
| 926 |
io.BackendRendererName = "imgui_impl_dx12"; |
| 927 |
io.BackendFlags |= ImGuiBackendFlags_RendererHasVtxOffset; // We can honor the ImDrawCmd::VtxOffset field, allowing for large meshes. |
| 928 |
io.BackendFlags |= ImGuiBackendFlags_RendererHasTextures; // We can honor ImGuiPlatformIO::Textures[] requests during render. |
| 929 |
io.BackendFlags |= ImGuiBackendFlags_RendererHasViewports; // We can create multi-viewports on the Renderer side (optional) |
| 930 |
|
| 931 |
if (io.ConfigFlags & ImGuiConfigFlags_ViewportsEnable) |
| 932 |
ImGui_ImplDX12_InitPlatformInterface(); |
| 933 |
|
| 934 |
// Create a dummy ImGui_ImplDX12_ViewportData holder for the main viewport, |
| 935 |
// Since this is created and managed by the application, we will only use the ->Resources[] fields. |
| 936 |
ImGuiViewport* main_viewport = ImGui::GetMainViewport(); |
| 937 |
main_viewport->RendererUserData = IM_NEW(ImGui_ImplDX12_ViewportData)(bd->numFramesInFlight); |
| 938 |
|
| 939 |
#ifndef IMGUI_DISABLE_OBSOLETE_FUNCTIONS |
| 940 |
if (init_info->SrvDescriptorAllocFn == nullptr) |
| 941 |
ImGui_ImplDX12_InitLegacySingleDescriptorMode(init_info); |
| 942 |
#endif |
| 943 |
IM_ASSERT(init_info->SrvDescriptorAllocFn != nullptr && init_info->SrvDescriptorFreeFn != nullptr); |
| 944 |
|
| 945 |
return true; |
| 946 |
} |
| 947 |
|
| 948 |
#ifndef IMGUI_DISABLE_OBSOLETE_FUNCTIONS |
| 949 |
// Legacy initialization API Obsoleted in 1.91.5 |
| 950 |
// font_srv_cpu_desc_handle and font_srv_gpu_desc_handle are handles to a single SRV descriptor to use for the internal font texture, they must be in 'srv_descriptor_heap' |
| 951 |
bool ImGui_ImplDX12_Init(ID3D12Device* device, int num_frames_in_flight, DXGI_FORMAT rtv_format, ID3D12DescriptorHeap* srv_descriptor_heap, D3D12_CPU_DESCRIPTOR_HANDLE font_srv_cpu_desc_handle, D3D12_GPU_DESCRIPTOR_HANDLE font_srv_gpu_desc_handle) |
| 952 |
{ |
| 953 |
ImGui_ImplDX12_InitInfo init_info; |
| 954 |
init_info.Device = device; |
| 955 |
init_info.NumFramesInFlight = num_frames_in_flight; |
| 956 |
init_info.RTVFormat = rtv_format; |
| 957 |
init_info.SrvDescriptorHeap = srv_descriptor_heap; |
| 958 |
init_info.LegacySingleSrvCpuDescriptor = font_srv_cpu_desc_handle; |
| 959 |
init_info.LegacySingleSrvGpuDescriptor = font_srv_gpu_desc_handle; |
| 960 |
|
| 961 |
D3D12_COMMAND_QUEUE_DESC queueDesc = {}; |
| 962 |
queueDesc.Type = D3D12_COMMAND_LIST_TYPE_DIRECT; |
| 963 |
queueDesc.Flags = D3D12_COMMAND_QUEUE_FLAG_NONE; |
| 964 |
queueDesc.NodeMask = 1; |
| 965 |
HRESULT hr = device->CreateCommandQueue(&queueDesc, IID_PPV_ARGS(&init_info.CommandQueue)); |
| 966 |
IM_ASSERT(SUCCEEDED(hr)); |
| 967 |
|
| 968 |
bool ret = ImGui_ImplDX12_Init(&init_info); |
| 969 |
ImGui_ImplDX12_Data* bd = ImGui_ImplDX12_GetBackendData(); |
| 970 |
bd->commandQueueOwned = true; |
| 971 |
ImGuiIO& io = ImGui::GetIO(); |
| 972 |
io.BackendFlags &= ~ImGuiBackendFlags_RendererHasTextures; // Using legacy ImGui_ImplDX12_Init() call with 1 SRV descriptor we cannot support multiple textures. |
| 973 |
|
| 974 |
return ret; |
| 975 |
} |
| 976 |
#endif |
| 977 |
|
| 978 |
void ImGui_ImplDX12_Shutdown() |
| 979 |
{ |
| 980 |
ImGui_ImplDX12_Data* bd = ImGui_ImplDX12_GetBackendData(); |
| 981 |
IM_ASSERT(bd != nullptr && "No renderer backend to shutdown, or already shutdown?"); |
| 982 |
ImGuiIO& io = ImGui::GetIO(); |
| 983 |
|
| 984 |
// Manually delete main viewport render resources in-case we haven't initialized for viewports |
| 985 |
ImGuiViewport* main_viewport = ImGui::GetMainViewport(); |
| 986 |
if (ImGui_ImplDX12_ViewportData* vd = (ImGui_ImplDX12_ViewportData*)main_viewport->RendererUserData) |
| 987 |
{ |
| 988 |
// We could just call ImGui_ImplDX12_DestroyWindow(main_viewport) as a convenience but that would be misleading since we only use data->Resources[] |
| 989 |
for (UINT i = 0; i < bd->numFramesInFlight; i++) |
| 990 |
ImGui_ImplDX12_DestroyRenderBuffers(&vd->FrameRenderBuffers[i]); |
| 991 |
IM_DELETE(vd); |
| 992 |
main_viewport->RendererUserData = nullptr; |
| 993 |
} |
| 994 |
|
| 995 |
// Clean up windows and device objects |
| 996 |
ImGui_ImplDX12_ShutdownPlatformInterface(); |
| 997 |
ImGui_ImplDX12_InvalidateDeviceObjects(); |
| 998 |
|
| 999 |
io.BackendRendererName = nullptr; |
| 1000 |
io.BackendRendererUserData = nullptr; |
| 1001 |
io.BackendFlags &= ~(ImGuiBackendFlags_RendererHasVtxOffset | ImGuiBackendFlags_RendererHasTextures | ImGuiBackendFlags_RendererHasViewports); |
| 1002 |
IM_DELETE(bd); |
| 1003 |
} |
| 1004 |
|
| 1005 |
void ImGui_ImplDX12_NewFrame() |
| 1006 |
{ |
| 1007 |
ImGui_ImplDX12_Data* bd = ImGui_ImplDX12_GetBackendData(); |
| 1008 |
IM_ASSERT(bd != nullptr && "Context or backend not initialized! Did you call ImGui_ImplDX12_Init()?"); |
| 1009 |
|
| 1010 |
if (!bd->pPipelineState) |
| 1011 |
if (!ImGui_ImplDX12_CreateDeviceObjects()) |
| 1012 |
IM_ASSERT(0 && "ImGui_ImplDX12_CreateDeviceObjects() failed!"); |
| 1013 |
} |
| 1014 |
|
| 1015 |
//-------------------------------------------------------------------------------------------------------- |
| 1016 |
// MULTI-VIEWPORT / PLATFORM INTERFACE SUPPORT |
| 1017 |
// This is an _advanced_ and _optional_ feature, allowing the backend to create and handle multiple viewports simultaneously. |
| 1018 |
// If you are new to dear imgui or creating a new binding for dear imgui, it is recommended that you completely ignore this section first.. |
| 1019 |
//-------------------------------------------------------------------------------------------------------- |
| 1020 |
|
| 1021 |
static void ImGui_ImplDX12_CreateWindow(ImGuiViewport* viewport) |
| 1022 |
{ |
| 1023 |
ImGui_ImplDX12_Data* bd = ImGui_ImplDX12_GetBackendData(); |
| 1024 |
ImGui_ImplDX12_ViewportData* vd = IM_NEW(ImGui_ImplDX12_ViewportData)(bd->numFramesInFlight); |
| 1025 |
viewport->RendererUserData = vd; |
| 1026 |
|
| 1027 |
// PlatformHandleRaw should always be a HWND, whereas PlatformHandle might be a higher-level handle (e.g. GLFWWindow*, SDL's WindowID). |
| 1028 |
// Some backends will leave PlatformHandleRaw == 0, in which case we assume PlatformHandle will contain the HWND. |
| 1029 |
HWND hwnd = viewport->PlatformHandleRaw ? (HWND)viewport->PlatformHandleRaw : (HWND)viewport->PlatformHandle; |
| 1030 |
IM_ASSERT(hwnd != 0); |
| 1031 |
|
| 1032 |
vd->FrameIndex = UINT_MAX; |
| 1033 |
|
| 1034 |
// Create command queue. |
| 1035 |
D3D12_COMMAND_QUEUE_DESC queue_desc = {}; |
| 1036 |
queue_desc.Flags = D3D12_COMMAND_QUEUE_FLAG_NONE; |
| 1037 |
queue_desc.Type = D3D12_COMMAND_LIST_TYPE_DIRECT; |
| 1038 |
|
| 1039 |
HRESULT res = S_OK; |
| 1040 |
res = bd->pd3dDevice->CreateCommandQueue(&queue_desc, IID_PPV_ARGS(&vd->CommandQueue)); |
| 1041 |
IM_ASSERT(res == S_OK); |
| 1042 |
|
| 1043 |
// Create command allocator. |
| 1044 |
for (UINT i = 0; i < bd->numFramesInFlight; ++i) |
| 1045 |
{ |
| 1046 |
res = bd->pd3dDevice->CreateCommandAllocator(D3D12_COMMAND_LIST_TYPE_DIRECT, IID_PPV_ARGS(&vd->FrameCtx[i].CommandAllocator)); |
| 1047 |
IM_ASSERT(res == S_OK); |
| 1048 |
} |
| 1049 |
|
| 1050 |
// Create command list. |
| 1051 |
res = bd->pd3dDevice->CreateCommandList(0, D3D12_COMMAND_LIST_TYPE_DIRECT, vd->FrameCtx[0].CommandAllocator, nullptr, IID_PPV_ARGS(&vd->CommandList)); |
| 1052 |
IM_ASSERT(res == S_OK); |
| 1053 |
vd->CommandList->Close(); |
| 1054 |
|
| 1055 |
// Create fence. |
| 1056 |
res = bd->pd3dDevice->CreateFence(0, D3D12_FENCE_FLAG_NONE, IID_PPV_ARGS(&vd->Fence)); |
| 1057 |
IM_ASSERT(res == S_OK); |
| 1058 |
|
| 1059 |
vd->FenceEvent = CreateEvent(nullptr, FALSE, FALSE, nullptr); |
| 1060 |
IM_ASSERT(vd->FenceEvent != nullptr); |
| 1061 |
|
| 1062 |
// Create swap chain |
| 1063 |
// FIXME-VIEWPORT: May want to copy/inherit swap chain settings from the user/application. |
| 1064 |
DXGI_SWAP_CHAIN_DESC1 sd1; |
| 1065 |
ZeroMemory(&sd1, sizeof(sd1)); |
| 1066 |
sd1.BufferCount = bd->numFramesInFlight; |
| 1067 |
sd1.Width = (UINT)viewport->Size.x; |
| 1068 |
sd1.Height = (UINT)viewport->Size.y; |
| 1069 |
sd1.Format = bd->RTVFormat; |
| 1070 |
sd1.BufferUsage = DXGI_USAGE_RENDER_TARGET_OUTPUT; |
| 1071 |
sd1.SampleDesc.Count = 1; |
| 1072 |
sd1.SampleDesc.Quality = 0; |
| 1073 |
sd1.SwapEffect = DXGI_SWAP_EFFECT_FLIP_DISCARD; |
| 1074 |
sd1.AlphaMode = DXGI_ALPHA_MODE_UNSPECIFIED; |
| 1075 |
sd1.Scaling = DXGI_SCALING_NONE; |
| 1076 |
sd1.Stereo = FALSE; |
| 1077 |
|
| 1078 |
IDXGIFactory4* dxgi_factory = nullptr; |
| 1079 |
res = ::CreateDXGIFactory1(IID_PPV_ARGS(&dxgi_factory)); |
| 1080 |
IM_ASSERT(res == S_OK); |
| 1081 |
|
| 1082 |
IDXGISwapChain1* swap_chain = nullptr; |
| 1083 |
res = dxgi_factory->CreateSwapChainForHwnd(vd->CommandQueue, hwnd, &sd1, nullptr, nullptr, &swap_chain); |
| 1084 |
IM_ASSERT(res == S_OK); |
| 1085 |
|
| 1086 |
dxgi_factory->Release(); |
| 1087 |
|
| 1088 |
// Or swapChain.As(&mSwapChain) |
| 1089 |
IM_ASSERT(vd->SwapChain == nullptr); |
| 1090 |
swap_chain->QueryInterface(IID_PPV_ARGS(&vd->SwapChain)); |
| 1091 |
swap_chain->Release(); |
| 1092 |
|
| 1093 |
// Create the render targets |
| 1094 |
if (vd->SwapChain) |
| 1095 |
{ |
| 1096 |
D3D12_DESCRIPTOR_HEAP_DESC desc = {}; |
| 1097 |
desc.Type = D3D12_DESCRIPTOR_HEAP_TYPE_RTV; |
| 1098 |
desc.NumDescriptors = bd->numFramesInFlight; |
| 1099 |
desc.Flags = D3D12_DESCRIPTOR_HEAP_FLAG_NONE; |
| 1100 |
desc.NodeMask = 1; |
| 1101 |
|
| 1102 |
HRESULT hr = bd->pd3dDevice->CreateDescriptorHeap(&desc, IID_PPV_ARGS(&vd->RtvDescHeap)); |
| 1103 |
IM_ASSERT(hr == S_OK); |
| 1104 |
|
| 1105 |
SIZE_T rtv_descriptor_size = bd->pd3dDevice->GetDescriptorHandleIncrementSize(D3D12_DESCRIPTOR_HEAP_TYPE_RTV); |
| 1106 |
D3D12_CPU_DESCRIPTOR_HANDLE rtv_handle = vd->RtvDescHeap->GetCPUDescriptorHandleForHeapStart(); |
| 1107 |
for (UINT i = 0; i < bd->numFramesInFlight; i++) |
| 1108 |
{ |
| 1109 |
vd->FrameCtx[i].RenderTargetCpuDescriptors = rtv_handle; |
| 1110 |
rtv_handle.ptr += rtv_descriptor_size; |
| 1111 |
} |
| 1112 |
|
| 1113 |
ID3D12Resource* back_buffer; |
| 1114 |
for (UINT i = 0; i < bd->numFramesInFlight; i++) |
| 1115 |
{ |
| 1116 |
IM_ASSERT(vd->FrameCtx[i].RenderTarget == nullptr); |
| 1117 |
vd->SwapChain->GetBuffer(i, IID_PPV_ARGS(&back_buffer)); |
| 1118 |
bd->pd3dDevice->CreateRenderTargetView(back_buffer, nullptr, vd->FrameCtx[i].RenderTargetCpuDescriptors); |
| 1119 |
vd->FrameCtx[i].RenderTarget = back_buffer; |
| 1120 |
} |
| 1121 |
} |
| 1122 |
|
| 1123 |
for (UINT i = 0; i < bd->numFramesInFlight; i++) |
| 1124 |
ImGui_ImplDX12_DestroyRenderBuffers(&vd->FrameRenderBuffers[i]); |
| 1125 |
} |
| 1126 |
|
| 1127 |
static void ImGui_WaitForPendingOperations(ImGui_ImplDX12_ViewportData* vd) |
| 1128 |
{ |
| 1129 |
HRESULT hr = S_FALSE; |
| 1130 |
if (vd && vd->CommandQueue && vd->Fence && vd->FenceEvent) |
| 1131 |
{ |
| 1132 |
hr = vd->CommandQueue->Signal(vd->Fence, ++vd->FenceSignaledValue); |
| 1133 |
IM_ASSERT(hr == S_OK); |
| 1134 |
::WaitForSingleObject(vd->FenceEvent, 0); // Reset any forgotten waits |
| 1135 |
hr = vd->Fence->SetEventOnCompletion(vd->FenceSignaledValue, vd->FenceEvent); |
| 1136 |
IM_ASSERT(hr == S_OK); |
| 1137 |
::WaitForSingleObject(vd->FenceEvent, INFINITE); |
| 1138 |
} |
| 1139 |
} |
| 1140 |
|
| 1141 |
static void ImGui_ImplDX12_DestroyWindow(ImGuiViewport* viewport) |
| 1142 |
{ |
| 1143 |
// The main viewport (owned by the application) will always have RendererUserData == 0 since we didn't create the data for it. |
| 1144 |
ImGui_ImplDX12_Data* bd = ImGui_ImplDX12_GetBackendData(); |
| 1145 |
if (ImGui_ImplDX12_ViewportData* vd = (ImGui_ImplDX12_ViewportData*)viewport->RendererUserData) |
| 1146 |
{ |
| 1147 |
ImGui_WaitForPendingOperations(vd); |
| 1148 |
|
| 1149 |
SafeRelease(vd->CommandQueue); |
| 1150 |
SafeRelease(vd->CommandList); |
| 1151 |
SafeRelease(vd->SwapChain); |
| 1152 |
SafeRelease(vd->RtvDescHeap); |
| 1153 |
SafeRelease(vd->Fence); |
| 1154 |
::CloseHandle(vd->FenceEvent); |
| 1155 |
vd->FenceEvent = nullptr; |
| 1156 |
|
| 1157 |
for (UINT i = 0; i < bd->numFramesInFlight; i++) |
| 1158 |
{ |
| 1159 |
SafeRelease(vd->FrameCtx[i].RenderTarget); |
| 1160 |
SafeRelease(vd->FrameCtx[i].CommandAllocator); |
| 1161 |
ImGui_ImplDX12_DestroyRenderBuffers(&vd->FrameRenderBuffers[i]); |
| 1162 |
} |
| 1163 |
IM_DELETE(vd); |
| 1164 |
} |
| 1165 |
viewport->RendererUserData = nullptr; |
| 1166 |
} |
| 1167 |
|
| 1168 |
static void ImGui_ImplDX12_SetWindowSize(ImGuiViewport* viewport, ImVec2 size) |
| 1169 |
{ |
| 1170 |
ImGui_ImplDX12_Data* bd = ImGui_ImplDX12_GetBackendData(); |
| 1171 |
ImGui_ImplDX12_ViewportData* vd = (ImGui_ImplDX12_ViewportData*)viewport->RendererUserData; |
| 1172 |
|
| 1173 |
ImGui_WaitForPendingOperations(vd); |
| 1174 |
|
| 1175 |
for (UINT i = 0; i < bd->numFramesInFlight; i++) |
| 1176 |
SafeRelease(vd->FrameCtx[i].RenderTarget); |
| 1177 |
|
| 1178 |
if (vd->SwapChain) |
| 1179 |
{ |
| 1180 |
ID3D12Resource* back_buffer = nullptr; |
| 1181 |
vd->SwapChain->ResizeBuffers(0, (UINT)size.x, (UINT)size.y, DXGI_FORMAT_UNKNOWN, 0); |
| 1182 |
for (UINT i = 0; i < bd->numFramesInFlight; i++) |
| 1183 |
{ |
| 1184 |
vd->SwapChain->GetBuffer(i, IID_PPV_ARGS(&back_buffer)); |
| 1185 |
bd->pd3dDevice->CreateRenderTargetView(back_buffer, nullptr, vd->FrameCtx[i].RenderTargetCpuDescriptors); |
| 1186 |
vd->FrameCtx[i].RenderTarget = back_buffer; |
| 1187 |
} |
| 1188 |
} |
| 1189 |
} |
| 1190 |
|
| 1191 |
static void ImGui_ImplDX12_RenderWindow(ImGuiViewport* viewport, void*) |
| 1192 |
{ |
| 1193 |
ImGui_ImplDX12_Data* bd = ImGui_ImplDX12_GetBackendData(); |
| 1194 |
ImGui_ImplDX12_ViewportData* vd = (ImGui_ImplDX12_ViewportData*)viewport->RendererUserData; |
| 1195 |
|
| 1196 |
ImGui_ImplDX12_FrameContext* frame_context = &vd->FrameCtx[vd->FrameIndex % bd->numFramesInFlight]; |
| 1197 |
UINT back_buffer_idx = vd->SwapChain->GetCurrentBackBufferIndex(); |
| 1198 |
|
| 1199 |
const ImVec4 clear_color = ImVec4(0.0f, 0.0f, 0.0f, 1.0f); |
| 1200 |
D3D12_RESOURCE_BARRIER barrier = {}; |
| 1201 |
barrier.Type = D3D12_RESOURCE_BARRIER_TYPE_TRANSITION; |
| 1202 |
barrier.Flags = D3D12_RESOURCE_BARRIER_FLAG_NONE; |
| 1203 |
barrier.Transition.pResource = vd->FrameCtx[back_buffer_idx].RenderTarget; |
| 1204 |
barrier.Transition.Subresource = D3D12_RESOURCE_BARRIER_ALL_SUBRESOURCES; |
| 1205 |
barrier.Transition.StateBefore = D3D12_RESOURCE_STATE_PRESENT; |
| 1206 |
barrier.Transition.StateAfter = D3D12_RESOURCE_STATE_RENDER_TARGET; |
| 1207 |
|
| 1208 |
// Draw |
| 1209 |
ID3D12GraphicsCommandList* cmd_list = vd->CommandList; |
| 1210 |
|
| 1211 |
frame_context->CommandAllocator->Reset(); |
| 1212 |
cmd_list->Reset(frame_context->CommandAllocator, nullptr); |
| 1213 |
cmd_list->ResourceBarrier(1, &barrier); |
| 1214 |
cmd_list->OMSetRenderTargets(1, &vd->FrameCtx[back_buffer_idx].RenderTargetCpuDescriptors, FALSE, nullptr); |
| 1215 |
if (!(viewport->Flags & ImGuiViewportFlags_NoRendererClear)) |
| 1216 |
cmd_list->ClearRenderTargetView(vd->FrameCtx[back_buffer_idx].RenderTargetCpuDescriptors, (const float*)&clear_color, 0, nullptr); |
| 1217 |
cmd_list->SetDescriptorHeaps(1, &bd->pd3dSrvDescHeap); |
| 1218 |
|
| 1219 |
ImGui_ImplDX12_RenderDrawData(viewport->DrawData, cmd_list); |
| 1220 |
|
| 1221 |
barrier.Transition.StateBefore = D3D12_RESOURCE_STATE_RENDER_TARGET; |
| 1222 |
barrier.Transition.StateAfter = D3D12_RESOURCE_STATE_PRESENT; |
| 1223 |
cmd_list->ResourceBarrier(1, &barrier); |
| 1224 |
cmd_list->Close(); |
| 1225 |
|
| 1226 |
vd->CommandQueue->Wait(vd->Fence, vd->FenceSignaledValue); |
| 1227 |
vd->CommandQueue->ExecuteCommandLists(1, (ID3D12CommandList* const*)&cmd_list); |
| 1228 |
vd->CommandQueue->Signal(vd->Fence, ++vd->FenceSignaledValue); |
| 1229 |
} |
| 1230 |
|
| 1231 |
static void ImGui_ImplDX12_SwapBuffers(ImGuiViewport* viewport, void*) |
| 1232 |
{ |
| 1233 |
ImGui_ImplDX12_ViewportData* vd = (ImGui_ImplDX12_ViewportData*)viewport->RendererUserData; |
| 1234 |
|
| 1235 |
vd->SwapChain->Present(0, 0); |
| 1236 |
while (vd->Fence->GetCompletedValue() < vd->FenceSignaledValue) |
| 1237 |
::SwitchToThread(); |
| 1238 |
} |
| 1239 |
|
| 1240 |
void ImGui_ImplDX12_InitPlatformInterface() |
| 1241 |
{ |
| 1242 |
ImGuiPlatformIO& platform_io = ImGui::GetPlatformIO(); |
| 1243 |
platform_io.Renderer_CreateWindow = ImGui_ImplDX12_CreateWindow; |
| 1244 |
platform_io.Renderer_DestroyWindow = ImGui_ImplDX12_DestroyWindow; |
| 1245 |
platform_io.Renderer_SetWindowSize = ImGui_ImplDX12_SetWindowSize; |
| 1246 |
platform_io.Renderer_RenderWindow = ImGui_ImplDX12_RenderWindow; |
| 1247 |
platform_io.Renderer_SwapBuffers = ImGui_ImplDX12_SwapBuffers; |
| 1248 |
} |
| 1249 |
|
| 1250 |
void ImGui_ImplDX12_ShutdownPlatformInterface() |
| 1251 |
{ |
| 1252 |
ImGui::DestroyPlatformWindows(); |
| 1253 |
} |
| 1254 |
|
| 1255 |
//----------------------------------------------------------------------------- |
| 1256 |
|
| 1257 |
#endif // #ifndef IMGUI_DISABLE |