imgui_impl_dx11.cpp 37 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784
  1. // dear imgui: Renderer Backend for DirectX11
  2. // This needs to be used along with a Platform Backend (e.g. Win32)
  3. // Implemented features:
  4. // [X] Renderer: User texture binding. Use 'ID3D11ShaderResourceView*' 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. // You can use unmodified imgui_impl_* files in your project. See examples/ folder for examples of using this.
  9. // Prefer including the entire imgui/ repository into your project (either as a copy or as a submodule), and only build the backends you need.
  10. // Learn about Dear ImGui:
  11. // - FAQ https://dearimgui.com/faq
  12. // - Getting Started https://dearimgui.com/getting-started
  13. // - Documentation https://dearimgui.com/docs (same as your local docs/ folder).
  14. // - Introduction, links and more at the top of imgui.cpp
  15. // CHANGELOG
  16. // (minor and older changes stripped away, please see git history for details)
  17. // 2025-XX-XX: Platform: Added support for multiple windows via the ImGuiPlatformIO interface.
  18. // 2025-02-24: [Docking] Added undocumented ImGui_ImplDX11_SetSwapChainDescs() to configure swap chain creation for secondary viewports.
  19. // 2025-01-06: DirectX11: Expose VertexConstantBuffer in ImGui_ImplDX11_RenderState. Reset projection matrix in ImDrawCallback_ResetRenderState handler.
  20. // 2024-10-07: DirectX11: Changed default texture sampler to Clamp instead of Repeat/Wrap.
  21. // 2024-10-07: DirectX11: Expose selected render state in ImGui_ImplDX11_RenderState, which you can access in 'void* platform_io.Renderer_RenderState' during draw callbacks.
  22. // 2022-10-11: Using 'nullptr' instead of 'NULL' as per our switch to C++11.
  23. // 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).
  24. // 2021-05-19: DirectX11: Replaced direct access to ImDrawCmd::TextureId with a call to ImDrawCmd::GetTexID(). (will become a requirement)
  25. // 2021-02-18: DirectX11: Change blending equation to preserve alpha in output buffer.
  26. // 2019-08-01: DirectX11: Fixed code querying the Geometry Shader state (would generally error with Debug layer enabled).
  27. // 2019-07-21: DirectX11: Backup, clear and restore Geometry Shader is any is bound when calling ImGui_ImplDX11_RenderDrawData. Clearing Hull/Domain/Compute shaders without backup/restore.
  28. // 2019-05-29: DirectX11: Added support for large mesh (64K+ vertices), enable ImGuiBackendFlags_RendererHasVtxOffset flag.
  29. // 2019-04-30: DirectX11: Added support for special ImDrawCallback_ResetRenderState callback to reset render state.
  30. // 2018-12-03: Misc: Added #pragma comment statement to automatically link with d3dcompiler.lib when using D3DCompile().
  31. // 2018-11-30: Misc: Setting up io.BackendRendererName so it can be displayed in the About Window.
  32. // 2018-08-01: DirectX11: Querying for IDXGIFactory instead of IDXGIFactory1 to increase compatibility.
  33. // 2018-07-13: DirectX11: Fixed unreleased resources in Init and Shutdown functions.
  34. // 2018-06-08: Misc: Extracted imgui_impl_dx11.cpp/.h away from the old combined DX11+Win32 example.
  35. // 2018-06-08: DirectX11: Use draw_data->DisplayPos and draw_data->DisplaySize to setup projection matrix and clipping rectangle.
  36. // 2018-02-16: Misc: Obsoleted the io.RenderDrawListsFn callback and exposed ImGui_ImplDX11_RenderDrawData() in the .h file so you can call it yourself.
  37. // 2018-02-06: Misc: Removed call to ImGui::Shutdown() which is not available from 1.60 WIP, user needs to call CreateContext/DestroyContext themselves.
  38. // 2016-05-07: DirectX11: Disabling depth-write.
  39. #include "imgui.h"
  40. #ifndef IMGUI_DISABLE
  41. #include "imgui_impl_dx11.h"
  42. // DirectX
  43. #include <stdio.h>
  44. #include <d3d11.h>
  45. #include <d3dcompiler.h>
  46. #ifdef _MSC_VER
  47. #pragma comment(lib, "d3dcompiler") // Automatically link with d3dcompiler.lib as we are using D3DCompile() below.
  48. #endif
  49. // DirectX11 data
  50. struct ImGui_ImplDX11_Data
  51. {
  52. ID3D11Device* pd3dDevice;
  53. ID3D11DeviceContext* pd3dDeviceContext;
  54. IDXGIFactory* pFactory;
  55. ID3D11Buffer* pVB;
  56. ID3D11Buffer* pIB;
  57. ID3D11VertexShader* pVertexShader;
  58. ID3D11InputLayout* pInputLayout;
  59. ID3D11Buffer* pVertexConstantBuffer;
  60. ID3D11PixelShader* pPixelShader;
  61. ID3D11SamplerState* pFontSampler;
  62. ID3D11ShaderResourceView* pFontTextureView;
  63. ID3D11RasterizerState* pRasterizerState;
  64. ID3D11BlendState* pBlendState;
  65. ID3D11DepthStencilState* pDepthStencilState;
  66. int VertexBufferSize;
  67. int IndexBufferSize;
  68. ImVector<DXGI_SWAP_CHAIN_DESC> SwapChainDescsForViewports;
  69. ImGui_ImplDX11_Data() { memset((void*)this, 0, sizeof(*this)); VertexBufferSize = 5000; IndexBufferSize = 10000; }
  70. };
  71. struct VERTEX_CONSTANT_BUFFER_DX11
  72. {
  73. float mvp[4][4];
  74. };
  75. // Backend data stored in io.BackendRendererUserData to allow support for multiple Dear ImGui contexts
  76. // It is STRONGLY preferred that you use docking branch with multi-viewports (== single Dear ImGui context + multiple windows) instead of multiple Dear ImGui contexts.
  77. static ImGui_ImplDX11_Data* ImGui_ImplDX11_GetBackendData()
  78. {
  79. return ImGui::GetCurrentContext() ? (ImGui_ImplDX11_Data*)ImGui::GetIO().BackendRendererUserData : nullptr;
  80. }
  81. // Forward Declarations
  82. static void ImGui_ImplDX11_InitMultiViewportSupport();
  83. static void ImGui_ImplDX11_ShutdownMultiViewportSupport();
  84. // Functions
  85. static void ImGui_ImplDX11_SetupRenderState(ImDrawData* draw_data, ID3D11DeviceContext* device_ctx)
  86. {
  87. ImGui_ImplDX11_Data* bd = ImGui_ImplDX11_GetBackendData();
  88. // Setup viewport
  89. D3D11_VIEWPORT vp = {};
  90. vp.Width = draw_data->DisplaySize.x;
  91. vp.Height = draw_data->DisplaySize.y;
  92. vp.MinDepth = 0.0f;
  93. vp.MaxDepth = 1.0f;
  94. vp.TopLeftX = vp.TopLeftY = 0;
  95. device_ctx->RSSetViewports(1, &vp);
  96. // Setup orthographic projection matrix into our constant buffer
  97. // Our visible imgui space lies from draw_data->DisplayPos (top left) to draw_data->DisplayPos+data_data->DisplaySize (bottom right). DisplayPos is (0,0) for single viewport apps.
  98. D3D11_MAPPED_SUBRESOURCE mapped_resource;
  99. if (device_ctx->Map(bd->pVertexConstantBuffer, 0, D3D11_MAP_WRITE_DISCARD, 0, &mapped_resource) == S_OK)
  100. {
  101. VERTEX_CONSTANT_BUFFER_DX11* constant_buffer = (VERTEX_CONSTANT_BUFFER_DX11*)mapped_resource.pData;
  102. float L = draw_data->DisplayPos.x;
  103. float R = draw_data->DisplayPos.x + draw_data->DisplaySize.x;
  104. float T = draw_data->DisplayPos.y;
  105. float B = draw_data->DisplayPos.y + draw_data->DisplaySize.y;
  106. float mvp[4][4] =
  107. {
  108. { 2.0f/(R-L), 0.0f, 0.0f, 0.0f },
  109. { 0.0f, 2.0f/(T-B), 0.0f, 0.0f },
  110. { 0.0f, 0.0f, 0.5f, 0.0f },
  111. { (R+L)/(L-R), (T+B)/(B-T), 0.5f, 1.0f },
  112. };
  113. memcpy(&constant_buffer->mvp, mvp, sizeof(mvp));
  114. device_ctx->Unmap(bd->pVertexConstantBuffer, 0);
  115. }
  116. // Setup shader and vertex buffers
  117. unsigned int stride = sizeof(ImDrawVert);
  118. unsigned int offset = 0;
  119. device_ctx->IASetInputLayout(bd->pInputLayout);
  120. device_ctx->IASetVertexBuffers(0, 1, &bd->pVB, &stride, &offset);
  121. device_ctx->IASetIndexBuffer(bd->pIB, sizeof(ImDrawIdx) == 2 ? DXGI_FORMAT_R16_UINT : DXGI_FORMAT_R32_UINT, 0);
  122. device_ctx->IASetPrimitiveTopology(D3D11_PRIMITIVE_TOPOLOGY_TRIANGLELIST);
  123. device_ctx->VSSetShader(bd->pVertexShader, nullptr, 0);
  124. device_ctx->VSSetConstantBuffers(0, 1, &bd->pVertexConstantBuffer);
  125. device_ctx->PSSetShader(bd->pPixelShader, nullptr, 0);
  126. device_ctx->PSSetSamplers(0, 1, &bd->pFontSampler);
  127. device_ctx->GSSetShader(nullptr, nullptr, 0);
  128. device_ctx->HSSetShader(nullptr, nullptr, 0); // In theory we should backup and restore this as well.. very infrequently used..
  129. device_ctx->DSSetShader(nullptr, nullptr, 0); // In theory we should backup and restore this as well.. very infrequently used..
  130. device_ctx->CSSetShader(nullptr, nullptr, 0); // In theory we should backup and restore this as well.. very infrequently used..
  131. // Setup render state
  132. const float blend_factor[4] = { 0.f, 0.f, 0.f, 0.f };
  133. device_ctx->OMSetBlendState(bd->pBlendState, blend_factor, 0xffffffff);
  134. device_ctx->OMSetDepthStencilState(bd->pDepthStencilState, 0);
  135. device_ctx->RSSetState(bd->pRasterizerState);
  136. }
  137. // Render function
  138. void ImGui_ImplDX11_RenderDrawData(ImDrawData* draw_data)
  139. {
  140. // Avoid rendering when minimized
  141. if (draw_data->DisplaySize.x <= 0.0f || draw_data->DisplaySize.y <= 0.0f)
  142. return;
  143. ImGui_ImplDX11_Data* bd = ImGui_ImplDX11_GetBackendData();
  144. ID3D11DeviceContext* device = bd->pd3dDeviceContext;
  145. // Create and grow vertex/index buffers if needed
  146. if (!bd->pVB || bd->VertexBufferSize < draw_data->TotalVtxCount)
  147. {
  148. if (bd->pVB) { bd->pVB->Release(); bd->pVB = nullptr; }
  149. bd->VertexBufferSize = draw_data->TotalVtxCount + 5000;
  150. D3D11_BUFFER_DESC desc = {};
  151. desc.Usage = D3D11_USAGE_DYNAMIC;
  152. desc.ByteWidth = bd->VertexBufferSize * sizeof(ImDrawVert);
  153. desc.BindFlags = D3D11_BIND_VERTEX_BUFFER;
  154. desc.CPUAccessFlags = D3D11_CPU_ACCESS_WRITE;
  155. desc.MiscFlags = 0;
  156. if (bd->pd3dDevice->CreateBuffer(&desc, nullptr, &bd->pVB) < 0)
  157. return;
  158. }
  159. if (!bd->pIB || bd->IndexBufferSize < draw_data->TotalIdxCount)
  160. {
  161. if (bd->pIB) { bd->pIB->Release(); bd->pIB = nullptr; }
  162. bd->IndexBufferSize = draw_data->TotalIdxCount + 10000;
  163. D3D11_BUFFER_DESC desc = {};
  164. desc.Usage = D3D11_USAGE_DYNAMIC;
  165. desc.ByteWidth = bd->IndexBufferSize * sizeof(ImDrawIdx);
  166. desc.BindFlags = D3D11_BIND_INDEX_BUFFER;
  167. desc.CPUAccessFlags = D3D11_CPU_ACCESS_WRITE;
  168. if (bd->pd3dDevice->CreateBuffer(&desc, nullptr, &bd->pIB) < 0)
  169. return;
  170. }
  171. // Upload vertex/index data into a single contiguous GPU buffer
  172. D3D11_MAPPED_SUBRESOURCE vtx_resource, idx_resource;
  173. if (device->Map(bd->pVB, 0, D3D11_MAP_WRITE_DISCARD, 0, &vtx_resource) != S_OK)
  174. return;
  175. if (device->Map(bd->pIB, 0, D3D11_MAP_WRITE_DISCARD, 0, &idx_resource) != S_OK)
  176. return;
  177. ImDrawVert* vtx_dst = (ImDrawVert*)vtx_resource.pData;
  178. ImDrawIdx* idx_dst = (ImDrawIdx*)idx_resource.pData;
  179. for (int n = 0; n < draw_data->CmdListsCount; n++)
  180. {
  181. const ImDrawList* draw_list = draw_data->CmdLists[n];
  182. memcpy(vtx_dst, draw_list->VtxBuffer.Data, draw_list->VtxBuffer.Size * sizeof(ImDrawVert));
  183. memcpy(idx_dst, draw_list->IdxBuffer.Data, draw_list->IdxBuffer.Size * sizeof(ImDrawIdx));
  184. vtx_dst += draw_list->VtxBuffer.Size;
  185. idx_dst += draw_list->IdxBuffer.Size;
  186. }
  187. device->Unmap(bd->pVB, 0);
  188. device->Unmap(bd->pIB, 0);
  189. // Backup DX state that will be modified to restore it afterwards (unfortunately this is very ugly looking and verbose. Close your eyes!)
  190. struct BACKUP_DX11_STATE
  191. {
  192. UINT ScissorRectsCount, ViewportsCount;
  193. D3D11_RECT ScissorRects[D3D11_VIEWPORT_AND_SCISSORRECT_OBJECT_COUNT_PER_PIPELINE];
  194. D3D11_VIEWPORT Viewports[D3D11_VIEWPORT_AND_SCISSORRECT_OBJECT_COUNT_PER_PIPELINE];
  195. ID3D11RasterizerState* RS;
  196. ID3D11BlendState* BlendState;
  197. FLOAT BlendFactor[4];
  198. UINT SampleMask;
  199. UINT StencilRef;
  200. ID3D11DepthStencilState* DepthStencilState;
  201. ID3D11ShaderResourceView* PSShaderResource;
  202. ID3D11SamplerState* PSSampler;
  203. ID3D11PixelShader* PS;
  204. ID3D11VertexShader* VS;
  205. ID3D11GeometryShader* GS;
  206. UINT PSInstancesCount, VSInstancesCount, GSInstancesCount;
  207. ID3D11ClassInstance *PSInstances[256], *VSInstances[256], *GSInstances[256]; // 256 is max according to PSSetShader documentation
  208. D3D11_PRIMITIVE_TOPOLOGY PrimitiveTopology;
  209. ID3D11Buffer* IndexBuffer, *VertexBuffer, *VSConstantBuffer;
  210. UINT IndexBufferOffset, VertexBufferStride, VertexBufferOffset;
  211. DXGI_FORMAT IndexBufferFormat;
  212. ID3D11InputLayout* InputLayout;
  213. };
  214. BACKUP_DX11_STATE old = {};
  215. old.ScissorRectsCount = old.ViewportsCount = D3D11_VIEWPORT_AND_SCISSORRECT_OBJECT_COUNT_PER_PIPELINE;
  216. device->RSGetScissorRects(&old.ScissorRectsCount, old.ScissorRects);
  217. device->RSGetViewports(&old.ViewportsCount, old.Viewports);
  218. device->RSGetState(&old.RS);
  219. device->OMGetBlendState(&old.BlendState, old.BlendFactor, &old.SampleMask);
  220. device->OMGetDepthStencilState(&old.DepthStencilState, &old.StencilRef);
  221. device->PSGetShaderResources(0, 1, &old.PSShaderResource);
  222. device->PSGetSamplers(0, 1, &old.PSSampler);
  223. old.PSInstancesCount = old.VSInstancesCount = old.GSInstancesCount = 256;
  224. device->PSGetShader(&old.PS, old.PSInstances, &old.PSInstancesCount);
  225. device->VSGetShader(&old.VS, old.VSInstances, &old.VSInstancesCount);
  226. device->VSGetConstantBuffers(0, 1, &old.VSConstantBuffer);
  227. device->GSGetShader(&old.GS, old.GSInstances, &old.GSInstancesCount);
  228. device->IAGetPrimitiveTopology(&old.PrimitiveTopology);
  229. device->IAGetIndexBuffer(&old.IndexBuffer, &old.IndexBufferFormat, &old.IndexBufferOffset);
  230. device->IAGetVertexBuffers(0, 1, &old.VertexBuffer, &old.VertexBufferStride, &old.VertexBufferOffset);
  231. device->IAGetInputLayout(&old.InputLayout);
  232. // Setup desired DX state
  233. ImGui_ImplDX11_SetupRenderState(draw_data, device);
  234. // Setup render state structure (for callbacks and custom texture bindings)
  235. ImGuiPlatformIO& platform_io = ImGui::GetPlatformIO();
  236. ImGui_ImplDX11_RenderState render_state;
  237. render_state.Device = bd->pd3dDevice;
  238. render_state.DeviceContext = bd->pd3dDeviceContext;
  239. render_state.SamplerDefault = bd->pFontSampler;
  240. render_state.VertexConstantBuffer = bd->pVertexConstantBuffer;
  241. platform_io.Renderer_RenderState = &render_state;
  242. // Render command lists
  243. // (Because we merged all buffers into a single one, we maintain our own offset into them)
  244. int global_idx_offset = 0;
  245. int global_vtx_offset = 0;
  246. ImVec2 clip_off = draw_data->DisplayPos;
  247. for (int n = 0; n < draw_data->CmdListsCount; n++)
  248. {
  249. const ImDrawList* draw_list = draw_data->CmdLists[n];
  250. for (int cmd_i = 0; cmd_i < draw_list->CmdBuffer.Size; cmd_i++)
  251. {
  252. const ImDrawCmd* pcmd = &draw_list->CmdBuffer[cmd_i];
  253. if (pcmd->UserCallback != nullptr)
  254. {
  255. // User callback, registered via ImDrawList::AddCallback()
  256. // (ImDrawCallback_ResetRenderState is a special callback value used by the user to request the renderer to reset render state.)
  257. if (pcmd->UserCallback == ImDrawCallback_ResetRenderState)
  258. ImGui_ImplDX11_SetupRenderState(draw_data, device);
  259. else
  260. pcmd->UserCallback(draw_list, pcmd);
  261. }
  262. else
  263. {
  264. // Project scissor/clipping rectangles into framebuffer space
  265. ImVec2 clip_min(pcmd->ClipRect.x - clip_off.x, pcmd->ClipRect.y - clip_off.y);
  266. ImVec2 clip_max(pcmd->ClipRect.z - clip_off.x, pcmd->ClipRect.w - clip_off.y);
  267. if (clip_max.x <= clip_min.x || clip_max.y <= clip_min.y)
  268. continue;
  269. // Apply scissor/clipping rectangle
  270. const D3D11_RECT r = { (LONG)clip_min.x, (LONG)clip_min.y, (LONG)clip_max.x, (LONG)clip_max.y };
  271. device->RSSetScissorRects(1, &r);
  272. // Bind texture, Draw
  273. ID3D11ShaderResourceView* texture_srv = (ID3D11ShaderResourceView*)pcmd->GetTexID();
  274. device->PSSetShaderResources(0, 1, &texture_srv);
  275. device->DrawIndexed(pcmd->ElemCount, pcmd->IdxOffset + global_idx_offset, pcmd->VtxOffset + global_vtx_offset);
  276. }
  277. }
  278. global_idx_offset += draw_list->IdxBuffer.Size;
  279. global_vtx_offset += draw_list->VtxBuffer.Size;
  280. }
  281. platform_io.Renderer_RenderState = nullptr;
  282. // Restore modified DX state
  283. device->RSSetScissorRects(old.ScissorRectsCount, old.ScissorRects);
  284. device->RSSetViewports(old.ViewportsCount, old.Viewports);
  285. device->RSSetState(old.RS); if (old.RS) old.RS->Release();
  286. device->OMSetBlendState(old.BlendState, old.BlendFactor, old.SampleMask); if (old.BlendState) old.BlendState->Release();
  287. device->OMSetDepthStencilState(old.DepthStencilState, old.StencilRef); if (old.DepthStencilState) old.DepthStencilState->Release();
  288. device->PSSetShaderResources(0, 1, &old.PSShaderResource); if (old.PSShaderResource) old.PSShaderResource->Release();
  289. device->PSSetSamplers(0, 1, &old.PSSampler); if (old.PSSampler) old.PSSampler->Release();
  290. device->PSSetShader(old.PS, old.PSInstances, old.PSInstancesCount); if (old.PS) old.PS->Release();
  291. for (UINT i = 0; i < old.PSInstancesCount; i++) if (old.PSInstances[i]) old.PSInstances[i]->Release();
  292. device->VSSetShader(old.VS, old.VSInstances, old.VSInstancesCount); if (old.VS) old.VS->Release();
  293. device->VSSetConstantBuffers(0, 1, &old.VSConstantBuffer); if (old.VSConstantBuffer) old.VSConstantBuffer->Release();
  294. device->GSSetShader(old.GS, old.GSInstances, old.GSInstancesCount); if (old.GS) old.GS->Release();
  295. for (UINT i = 0; i < old.VSInstancesCount; i++) if (old.VSInstances[i]) old.VSInstances[i]->Release();
  296. device->IASetPrimitiveTopology(old.PrimitiveTopology);
  297. device->IASetIndexBuffer(old.IndexBuffer, old.IndexBufferFormat, old.IndexBufferOffset); if (old.IndexBuffer) old.IndexBuffer->Release();
  298. device->IASetVertexBuffers(0, 1, &old.VertexBuffer, &old.VertexBufferStride, &old.VertexBufferOffset); if (old.VertexBuffer) old.VertexBuffer->Release();
  299. device->IASetInputLayout(old.InputLayout); if (old.InputLayout) old.InputLayout->Release();
  300. }
  301. static void ImGui_ImplDX11_CreateFontsTexture()
  302. {
  303. // Build texture atlas
  304. ImGuiIO& io = ImGui::GetIO();
  305. ImGui_ImplDX11_Data* bd = ImGui_ImplDX11_GetBackendData();
  306. unsigned char* pixels;
  307. int width, height;
  308. io.Fonts->GetTexDataAsRGBA32(&pixels, &width, &height);
  309. // Upload texture to graphics system
  310. {
  311. D3D11_TEXTURE2D_DESC desc;
  312. ZeroMemory(&desc, sizeof(desc));
  313. desc.Width = width;
  314. desc.Height = height;
  315. desc.MipLevels = 1;
  316. desc.ArraySize = 1;
  317. desc.Format = DXGI_FORMAT_R8G8B8A8_UNORM;
  318. desc.SampleDesc.Count = 1;
  319. desc.Usage = D3D11_USAGE_DEFAULT;
  320. desc.BindFlags = D3D11_BIND_SHADER_RESOURCE;
  321. desc.CPUAccessFlags = 0;
  322. ID3D11Texture2D* pTexture = nullptr;
  323. D3D11_SUBRESOURCE_DATA subResource;
  324. subResource.pSysMem = pixels;
  325. subResource.SysMemPitch = desc.Width * 4;
  326. subResource.SysMemSlicePitch = 0;
  327. bd->pd3dDevice->CreateTexture2D(&desc, &subResource, &pTexture);
  328. IM_ASSERT(pTexture != nullptr);
  329. // Create texture view
  330. D3D11_SHADER_RESOURCE_VIEW_DESC srvDesc;
  331. ZeroMemory(&srvDesc, sizeof(srvDesc));
  332. srvDesc.Format = DXGI_FORMAT_R8G8B8A8_UNORM;
  333. srvDesc.ViewDimension = D3D11_SRV_DIMENSION_TEXTURE2D;
  334. srvDesc.Texture2D.MipLevels = desc.MipLevels;
  335. srvDesc.Texture2D.MostDetailedMip = 0;
  336. bd->pd3dDevice->CreateShaderResourceView(pTexture, &srvDesc, &bd->pFontTextureView);
  337. pTexture->Release();
  338. }
  339. // Store our identifier
  340. io.Fonts->SetTexID((ImTextureID)bd->pFontTextureView);
  341. }
  342. static void ImGui_ImplDX11_DestroyFontsTexture()
  343. {
  344. ImGui_ImplDX11_Data* bd = ImGui_ImplDX11_GetBackendData();
  345. if (bd->pFontTextureView)
  346. {
  347. bd->pFontTextureView->Release();
  348. bd->pFontTextureView = nullptr;
  349. ImGui::GetIO().Fonts->SetTexID(0); // We copied data->pFontTextureView to io.Fonts->TexID so let's clear that as well.
  350. }
  351. }
  352. bool ImGui_ImplDX11_CreateDeviceObjects()
  353. {
  354. ImGui_ImplDX11_Data* bd = ImGui_ImplDX11_GetBackendData();
  355. if (!bd->pd3dDevice)
  356. return false;
  357. if (bd->pFontSampler)
  358. ImGui_ImplDX11_InvalidateDeviceObjects();
  359. // By using D3DCompile() from <d3dcompiler.h> / d3dcompiler.lib, we introduce a dependency to a given version of d3dcompiler_XX.dll (see D3DCOMPILER_DLL_A)
  360. // If you would like to use this DX11 sample code but remove this dependency you can:
  361. // 1) compile once, save the compiled shader blobs into a file or source code and pass them to CreateVertexShader()/CreatePixelShader() [preferred solution]
  362. // 2) use code to detect any version of the DLL and grab a pointer to D3DCompile from the DLL.
  363. // See https://github.com/ocornut/imgui/pull/638 for sources and details.
  364. // Create the vertex shader
  365. {
  366. static const char* vertexShader =
  367. "cbuffer vertexBuffer : register(b0) \
  368. {\
  369. float4x4 ProjectionMatrix; \
  370. };\
  371. struct VS_INPUT\
  372. {\
  373. float2 pos : POSITION;\
  374. float4 col : COLOR0;\
  375. float2 uv : TEXCOORD0;\
  376. };\
  377. \
  378. struct PS_INPUT\
  379. {\
  380. float4 pos : SV_POSITION;\
  381. float4 col : COLOR0;\
  382. float2 uv : TEXCOORD0;\
  383. };\
  384. \
  385. PS_INPUT main(VS_INPUT input)\
  386. {\
  387. PS_INPUT output;\
  388. output.pos = mul( ProjectionMatrix, float4(input.pos.xy, 0.f, 1.f));\
  389. output.col = input.col;\
  390. output.uv = input.uv;\
  391. return output;\
  392. }";
  393. ID3DBlob* vertexShaderBlob;
  394. if (FAILED(D3DCompile(vertexShader, strlen(vertexShader), nullptr, nullptr, nullptr, "main", "vs_4_0", 0, 0, &vertexShaderBlob, nullptr)))
  395. return false; // NB: Pass ID3DBlob* pErrorBlob to D3DCompile() to get error showing in (const char*)pErrorBlob->GetBufferPointer(). Make sure to Release() the blob!
  396. if (bd->pd3dDevice->CreateVertexShader(vertexShaderBlob->GetBufferPointer(), vertexShaderBlob->GetBufferSize(), nullptr, &bd->pVertexShader) != S_OK)
  397. {
  398. vertexShaderBlob->Release();
  399. return false;
  400. }
  401. // Create the input layout
  402. D3D11_INPUT_ELEMENT_DESC local_layout[] =
  403. {
  404. { "POSITION", 0, DXGI_FORMAT_R32G32_FLOAT, 0, (UINT)offsetof(ImDrawVert, pos), D3D11_INPUT_PER_VERTEX_DATA, 0 },
  405. { "TEXCOORD", 0, DXGI_FORMAT_R32G32_FLOAT, 0, (UINT)offsetof(ImDrawVert, uv), D3D11_INPUT_PER_VERTEX_DATA, 0 },
  406. { "COLOR", 0, DXGI_FORMAT_R8G8B8A8_UNORM, 0, (UINT)offsetof(ImDrawVert, col), D3D11_INPUT_PER_VERTEX_DATA, 0 },
  407. };
  408. if (bd->pd3dDevice->CreateInputLayout(local_layout, 3, vertexShaderBlob->GetBufferPointer(), vertexShaderBlob->GetBufferSize(), &bd->pInputLayout) != S_OK)
  409. {
  410. vertexShaderBlob->Release();
  411. return false;
  412. }
  413. vertexShaderBlob->Release();
  414. // Create the constant buffer
  415. {
  416. D3D11_BUFFER_DESC desc = {};
  417. desc.ByteWidth = sizeof(VERTEX_CONSTANT_BUFFER_DX11);
  418. desc.Usage = D3D11_USAGE_DYNAMIC;
  419. desc.BindFlags = D3D11_BIND_CONSTANT_BUFFER;
  420. desc.CPUAccessFlags = D3D11_CPU_ACCESS_WRITE;
  421. desc.MiscFlags = 0;
  422. bd->pd3dDevice->CreateBuffer(&desc, nullptr, &bd->pVertexConstantBuffer);
  423. }
  424. }
  425. // Create the pixel shader
  426. {
  427. static const char* pixelShader =
  428. "struct PS_INPUT\
  429. {\
  430. float4 pos : SV_POSITION;\
  431. float4 col : COLOR0;\
  432. float2 uv : TEXCOORD0;\
  433. };\
  434. sampler sampler0;\
  435. Texture2D texture0;\
  436. \
  437. float4 main(PS_INPUT input) : SV_Target\
  438. {\
  439. float4 out_col = input.col * texture0.Sample(sampler0, input.uv); \
  440. return out_col; \
  441. }";
  442. ID3DBlob* pixelShaderBlob;
  443. if (FAILED(D3DCompile(pixelShader, strlen(pixelShader), nullptr, nullptr, nullptr, "main", "ps_4_0", 0, 0, &pixelShaderBlob, nullptr)))
  444. return false; // NB: Pass ID3DBlob* pErrorBlob to D3DCompile() to get error showing in (const char*)pErrorBlob->GetBufferPointer(). Make sure to Release() the blob!
  445. if (bd->pd3dDevice->CreatePixelShader(pixelShaderBlob->GetBufferPointer(), pixelShaderBlob->GetBufferSize(), nullptr, &bd->pPixelShader) != S_OK)
  446. {
  447. pixelShaderBlob->Release();
  448. return false;
  449. }
  450. pixelShaderBlob->Release();
  451. }
  452. // Create the blending setup
  453. {
  454. D3D11_BLEND_DESC desc;
  455. ZeroMemory(&desc, sizeof(desc));
  456. desc.AlphaToCoverageEnable = false;
  457. desc.RenderTarget[0].BlendEnable = true;
  458. desc.RenderTarget[0].SrcBlend = D3D11_BLEND_SRC_ALPHA;
  459. desc.RenderTarget[0].DestBlend = D3D11_BLEND_INV_SRC_ALPHA;
  460. desc.RenderTarget[0].BlendOp = D3D11_BLEND_OP_ADD;
  461. desc.RenderTarget[0].SrcBlendAlpha = D3D11_BLEND_ONE;
  462. desc.RenderTarget[0].DestBlendAlpha = D3D11_BLEND_INV_SRC_ALPHA;
  463. desc.RenderTarget[0].BlendOpAlpha = D3D11_BLEND_OP_ADD;
  464. desc.RenderTarget[0].RenderTargetWriteMask = D3D11_COLOR_WRITE_ENABLE_ALL;
  465. bd->pd3dDevice->CreateBlendState(&desc, &bd->pBlendState);
  466. }
  467. // Create the rasterizer state
  468. {
  469. D3D11_RASTERIZER_DESC desc;
  470. ZeroMemory(&desc, sizeof(desc));
  471. desc.FillMode = D3D11_FILL_SOLID;
  472. desc.CullMode = D3D11_CULL_NONE;
  473. desc.ScissorEnable = true;
  474. desc.DepthClipEnable = true;
  475. bd->pd3dDevice->CreateRasterizerState(&desc, &bd->pRasterizerState);
  476. }
  477. // Create depth-stencil State
  478. {
  479. D3D11_DEPTH_STENCIL_DESC desc;
  480. ZeroMemory(&desc, sizeof(desc));
  481. desc.DepthEnable = false;
  482. desc.DepthWriteMask = D3D11_DEPTH_WRITE_MASK_ALL;
  483. desc.DepthFunc = D3D11_COMPARISON_ALWAYS;
  484. desc.StencilEnable = false;
  485. desc.FrontFace.StencilFailOp = desc.FrontFace.StencilDepthFailOp = desc.FrontFace.StencilPassOp = D3D11_STENCIL_OP_KEEP;
  486. desc.FrontFace.StencilFunc = D3D11_COMPARISON_ALWAYS;
  487. desc.BackFace = desc.FrontFace;
  488. bd->pd3dDevice->CreateDepthStencilState(&desc, &bd->pDepthStencilState);
  489. }
  490. // Create texture sampler
  491. // (Bilinear sampling is required by default. Set 'io.Fonts->Flags |= ImFontAtlasFlags_NoBakedLines' or 'style.AntiAliasedLinesUseTex = false' to allow point/nearest sampling)
  492. {
  493. D3D11_SAMPLER_DESC desc;
  494. ZeroMemory(&desc, sizeof(desc));
  495. desc.Filter = D3D11_FILTER_MIN_MAG_MIP_LINEAR;
  496. desc.AddressU = D3D11_TEXTURE_ADDRESS_CLAMP;
  497. desc.AddressV = D3D11_TEXTURE_ADDRESS_CLAMP;
  498. desc.AddressW = D3D11_TEXTURE_ADDRESS_CLAMP;
  499. desc.MipLODBias = 0.f;
  500. desc.ComparisonFunc = D3D11_COMPARISON_ALWAYS;
  501. desc.MinLOD = 0.f;
  502. desc.MaxLOD = 0.f;
  503. bd->pd3dDevice->CreateSamplerState(&desc, &bd->pFontSampler);
  504. }
  505. ImGui_ImplDX11_CreateFontsTexture();
  506. return true;
  507. }
  508. void ImGui_ImplDX11_InvalidateDeviceObjects()
  509. {
  510. ImGui_ImplDX11_Data* bd = ImGui_ImplDX11_GetBackendData();
  511. if (!bd->pd3dDevice)
  512. return;
  513. ImGui_ImplDX11_DestroyFontsTexture();
  514. if (bd->pFontSampler) { bd->pFontSampler->Release(); bd->pFontSampler = nullptr; }
  515. if (bd->pIB) { bd->pIB->Release(); bd->pIB = nullptr; }
  516. if (bd->pVB) { bd->pVB->Release(); bd->pVB = nullptr; }
  517. if (bd->pBlendState) { bd->pBlendState->Release(); bd->pBlendState = nullptr; }
  518. if (bd->pDepthStencilState) { bd->pDepthStencilState->Release(); bd->pDepthStencilState = nullptr; }
  519. if (bd->pRasterizerState) { bd->pRasterizerState->Release(); bd->pRasterizerState = nullptr; }
  520. if (bd->pPixelShader) { bd->pPixelShader->Release(); bd->pPixelShader = nullptr; }
  521. if (bd->pVertexConstantBuffer) { bd->pVertexConstantBuffer->Release(); bd->pVertexConstantBuffer = nullptr; }
  522. if (bd->pInputLayout) { bd->pInputLayout->Release(); bd->pInputLayout = nullptr; }
  523. if (bd->pVertexShader) { bd->pVertexShader->Release(); bd->pVertexShader = nullptr; }
  524. }
  525. bool ImGui_ImplDX11_Init(ID3D11Device* device, ID3D11DeviceContext* device_context)
  526. {
  527. ImGuiIO& io = ImGui::GetIO();
  528. IMGUI_CHECKVERSION();
  529. IM_ASSERT(io.BackendRendererUserData == nullptr && "Already initialized a renderer backend!");
  530. // Setup backend capabilities flags
  531. ImGui_ImplDX11_Data* bd = IM_NEW(ImGui_ImplDX11_Data)();
  532. io.BackendRendererUserData = (void*)bd;
  533. io.BackendRendererName = "imgui_impl_dx11";
  534. io.BackendFlags |= ImGuiBackendFlags_RendererHasVtxOffset; // We can honor the ImDrawCmd::VtxOffset field, allowing for large meshes.
  535. io.BackendFlags |= ImGuiBackendFlags_RendererHasViewports; // We can create multi-viewports on the Renderer side (optional)
  536. // Get factory from device
  537. IDXGIDevice* pDXGIDevice = nullptr;
  538. IDXGIAdapter* pDXGIAdapter = nullptr;
  539. IDXGIFactory* pFactory = nullptr;
  540. if (device->QueryInterface(IID_PPV_ARGS(&pDXGIDevice)) == S_OK)
  541. if (pDXGIDevice->GetParent(IID_PPV_ARGS(&pDXGIAdapter)) == S_OK)
  542. if (pDXGIAdapter->GetParent(IID_PPV_ARGS(&pFactory)) == S_OK)
  543. {
  544. bd->pd3dDevice = device;
  545. bd->pd3dDeviceContext = device_context;
  546. bd->pFactory = pFactory;
  547. }
  548. if (pDXGIDevice) pDXGIDevice->Release();
  549. if (pDXGIAdapter) pDXGIAdapter->Release();
  550. bd->pd3dDevice->AddRef();
  551. bd->pd3dDeviceContext->AddRef();
  552. ImGui_ImplDX11_InitMultiViewportSupport();
  553. return true;
  554. }
  555. void ImGui_ImplDX11_Shutdown()
  556. {
  557. ImGui_ImplDX11_Data* bd = ImGui_ImplDX11_GetBackendData();
  558. IM_ASSERT(bd != nullptr && "No renderer backend to shutdown, or already shutdown?");
  559. ImGuiIO& io = ImGui::GetIO();
  560. ImGui_ImplDX11_ShutdownMultiViewportSupport();
  561. ImGui_ImplDX11_InvalidateDeviceObjects();
  562. if (bd->pFactory) { bd->pFactory->Release(); }
  563. if (bd->pd3dDevice) { bd->pd3dDevice->Release(); }
  564. if (bd->pd3dDeviceContext) { bd->pd3dDeviceContext->Release(); }
  565. io.BackendRendererName = nullptr;
  566. io.BackendRendererUserData = nullptr;
  567. io.BackendFlags &= ~(ImGuiBackendFlags_RendererHasVtxOffset | ImGuiBackendFlags_RendererHasViewports);
  568. IM_DELETE(bd);
  569. }
  570. void ImGui_ImplDX11_NewFrame()
  571. {
  572. ImGui_ImplDX11_Data* bd = ImGui_ImplDX11_GetBackendData();
  573. IM_ASSERT(bd != nullptr && "Context or backend not initialized! Did you call ImGui_ImplDX11_Init()?");
  574. if (!bd->pFontSampler)
  575. ImGui_ImplDX11_CreateDeviceObjects();
  576. }
  577. //--------------------------------------------------------------------------------------------------------
  578. // MULTI-VIEWPORT / PLATFORM INTERFACE SUPPORT
  579. // This is an _advanced_ and _optional_ feature, allowing the backend to create and handle multiple viewports simultaneously.
  580. // 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..
  581. //--------------------------------------------------------------------------------------------------------
  582. // Helper structure we store in the void* RendererUserData field of each ImGuiViewport to easily retrieve our backend data.
  583. struct ImGui_ImplDX11_ViewportData
  584. {
  585. IDXGISwapChain* SwapChain;
  586. ID3D11RenderTargetView* RTView;
  587. ImGui_ImplDX11_ViewportData() { SwapChain = nullptr; RTView = nullptr; }
  588. ~ImGui_ImplDX11_ViewportData() { IM_ASSERT(SwapChain == nullptr && RTView == nullptr); }
  589. };
  590. // Multi-Viewports: configure templates used when creating swapchains for secondary viewports. Will try them in order.
  591. // This is intentionally not declared in the .h file yet, so you will need to copy this declaration:
  592. void ImGui_ImplDX11_SetSwapChainDescs(const DXGI_SWAP_CHAIN_DESC* desc_templates, int desc_templates_count);
  593. void ImGui_ImplDX11_SetSwapChainDescs(const DXGI_SWAP_CHAIN_DESC* desc_templates, int desc_templates_count)
  594. {
  595. ImGui_ImplDX11_Data* bd = ImGui_ImplDX11_GetBackendData();
  596. bd->SwapChainDescsForViewports.resize(desc_templates_count);
  597. memcpy(bd->SwapChainDescsForViewports.Data, desc_templates, sizeof(DXGI_SWAP_CHAIN_DESC));
  598. }
  599. static void ImGui_ImplDX11_CreateWindow(ImGuiViewport* viewport)
  600. {
  601. ImGui_ImplDX11_Data* bd = ImGui_ImplDX11_GetBackendData();
  602. ImGui_ImplDX11_ViewportData* vd = IM_NEW(ImGui_ImplDX11_ViewportData)();
  603. viewport->RendererUserData = vd;
  604. // PlatformHandleRaw should always be a HWND, whereas PlatformHandle might be a higher-level handle (e.g. GLFWWindow*, SDL's WindowID).
  605. // Some backends will leave PlatformHandleRaw == 0, in which case we assume PlatformHandle will contain the HWND.
  606. HWND hwnd = viewport->PlatformHandleRaw ? (HWND)viewport->PlatformHandleRaw : (HWND)viewport->PlatformHandle;
  607. IM_ASSERT(hwnd != 0);
  608. IM_ASSERT(vd->SwapChain == nullptr && vd->RTView == nullptr);
  609. // Create swap chain
  610. HRESULT hr = DXGI_ERROR_UNSUPPORTED;
  611. for (const DXGI_SWAP_CHAIN_DESC& sd_template : bd->SwapChainDescsForViewports)
  612. {
  613. IM_ASSERT(sd_template.BufferDesc.Width == 0 && sd_template.BufferDesc.Height == 0 && sd_template.OutputWindow == nullptr);
  614. DXGI_SWAP_CHAIN_DESC sd = sd_template;
  615. sd.BufferDesc.Width = (UINT)viewport->Size.x;
  616. sd.BufferDesc.Height = (UINT)viewport->Size.y;
  617. sd.OutputWindow = hwnd;
  618. hr = bd->pFactory->CreateSwapChain(bd->pd3dDevice, &sd, &vd->SwapChain);
  619. if (SUCCEEDED(hr))
  620. break;
  621. }
  622. IM_ASSERT(SUCCEEDED(hr));
  623. // Create the render target
  624. if (vd->SwapChain != nullptr)
  625. {
  626. ID3D11Texture2D* pBackBuffer;
  627. vd->SwapChain->GetBuffer(0, IID_PPV_ARGS(&pBackBuffer));
  628. bd->pd3dDevice->CreateRenderTargetView(pBackBuffer, nullptr, &vd->RTView);
  629. pBackBuffer->Release();
  630. }
  631. }
  632. static void ImGui_ImplDX11_DestroyWindow(ImGuiViewport* viewport)
  633. {
  634. // The main viewport (owned by the application) will always have RendererUserData == nullptr since we didn't create the data for it.
  635. if (ImGui_ImplDX11_ViewportData* vd = (ImGui_ImplDX11_ViewportData*)viewport->RendererUserData)
  636. {
  637. if (vd->SwapChain)
  638. vd->SwapChain->Release();
  639. vd->SwapChain = nullptr;
  640. if (vd->RTView)
  641. vd->RTView->Release();
  642. vd->RTView = nullptr;
  643. IM_DELETE(vd);
  644. }
  645. viewport->RendererUserData = nullptr;
  646. }
  647. static void ImGui_ImplDX11_SetWindowSize(ImGuiViewport* viewport, ImVec2 size)
  648. {
  649. ImGui_ImplDX11_Data* bd = ImGui_ImplDX11_GetBackendData();
  650. ImGui_ImplDX11_ViewportData* vd = (ImGui_ImplDX11_ViewportData*)viewport->RendererUserData;
  651. if (vd->RTView)
  652. {
  653. vd->RTView->Release();
  654. vd->RTView = nullptr;
  655. }
  656. if (vd->SwapChain)
  657. {
  658. ID3D11Texture2D* pBackBuffer = nullptr;
  659. vd->SwapChain->ResizeBuffers(0, (UINT)size.x, (UINT)size.y, DXGI_FORMAT_UNKNOWN, 0);
  660. vd->SwapChain->GetBuffer(0, IID_PPV_ARGS(&pBackBuffer));
  661. if (pBackBuffer == nullptr) { fprintf(stderr, "ImGui_ImplDX11_SetWindowSize() failed creating buffers.\n"); return; }
  662. bd->pd3dDevice->CreateRenderTargetView(pBackBuffer, nullptr, &vd->RTView);
  663. pBackBuffer->Release();
  664. }
  665. }
  666. static void ImGui_ImplDX11_RenderWindow(ImGuiViewport* viewport, void*)
  667. {
  668. ImGui_ImplDX11_Data* bd = ImGui_ImplDX11_GetBackendData();
  669. ImGui_ImplDX11_ViewportData* vd = (ImGui_ImplDX11_ViewportData*)viewport->RendererUserData;
  670. ImVec4 clear_color = ImVec4(0.0f, 0.0f, 0.0f, 1.0f);
  671. bd->pd3dDeviceContext->OMSetRenderTargets(1, &vd->RTView, nullptr);
  672. if (!(viewport->Flags & ImGuiViewportFlags_NoRendererClear))
  673. bd->pd3dDeviceContext->ClearRenderTargetView(vd->RTView, (float*)&clear_color);
  674. ImGui_ImplDX11_RenderDrawData(viewport->DrawData);
  675. }
  676. static void ImGui_ImplDX11_SwapBuffers(ImGuiViewport* viewport, void*)
  677. {
  678. ImGui_ImplDX11_ViewportData* vd = (ImGui_ImplDX11_ViewportData*)viewport->RendererUserData;
  679. vd->SwapChain->Present(0, 0); // Present without vsync
  680. }
  681. static void ImGui_ImplDX11_InitMultiViewportSupport()
  682. {
  683. ImGuiPlatformIO& platform_io = ImGui::GetPlatformIO();
  684. platform_io.Renderer_CreateWindow = ImGui_ImplDX11_CreateWindow;
  685. platform_io.Renderer_DestroyWindow = ImGui_ImplDX11_DestroyWindow;
  686. platform_io.Renderer_SetWindowSize = ImGui_ImplDX11_SetWindowSize;
  687. platform_io.Renderer_RenderWindow = ImGui_ImplDX11_RenderWindow;
  688. platform_io.Renderer_SwapBuffers = ImGui_ImplDX11_SwapBuffers;
  689. // Default swapchain format
  690. DXGI_SWAP_CHAIN_DESC sd;
  691. ZeroMemory(&sd, sizeof(sd));
  692. sd.BufferDesc.Format = DXGI_FORMAT_R8G8B8A8_UNORM;
  693. sd.SampleDesc.Count = 1;
  694. sd.SampleDesc.Quality = 0;
  695. sd.BufferUsage = DXGI_USAGE_RENDER_TARGET_OUTPUT;
  696. sd.BufferCount = 1;
  697. sd.Windowed = TRUE;
  698. sd.SwapEffect = DXGI_SWAP_EFFECT_DISCARD;
  699. sd.Flags = 0;
  700. ImGui_ImplDX11_SetSwapChainDescs(&sd, 1);
  701. }
  702. static void ImGui_ImplDX11_ShutdownMultiViewportSupport()
  703. {
  704. ImGui::DestroyPlatformWindows();
  705. }
  706. //-----------------------------------------------------------------------------
  707. #endif // #ifndef IMGUI_DISABLE