imgui_impl_dx12.cpp 53 KB

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