imgui_impl_dx12.cpp 53 KB

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