imgui_impl_dx12.cpp 34 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823
  1. // ImGui Win32 + DirectX12 binding
  2. // FIXME: 64-bit only for now! (Because sizeof(ImTextureId) == sizeof(void*))
  3. // Implemented features:
  4. // [X] User texture binding. Use 'D3D12_GPU_DESCRIPTOR_HANDLE' as ImTextureID. Read the FAQ about ImTextureID in imgui.cpp.
  5. // You can copy and use unmodified imgui_impl_* files in your project. See main.cpp for an example of using this.
  6. // If you use this binding you'll need to call 4 functions: ImGui_ImplXXXX_Init(), ImGui_ImplXXXX_NewFrame(), ImGui::Render() and ImGui_ImplXXXX_Shutdown().
  7. // If you are new to ImGui, see examples/README.txt and documentation at the top of imgui.cpp.
  8. // https://github.com/ocornut/imgui
  9. // CHANGELOG
  10. // (minor and older changes stripped away, please see git history for details)
  11. // 2018-03-20: Misc: Setup io.BackendFlags ImGuiBackendFlags_HasMouseCursors and ImGuiBackendFlags_HasSetMousePos flags + honor ImGuiConfigFlags_NoMouseCursorChange flag.
  12. // 2018-02-22: Merged into master with all Win32 code synchronized to other examples.
  13. #include "imgui.h"
  14. #include "imgui_impl_dx12.h"
  15. // DirectX
  16. #include <d3d12.h>
  17. #include <d3dcompiler.h>
  18. // Win32 data
  19. static HWND g_hWnd = 0;
  20. static INT64 g_Time = 0;
  21. static INT64 g_TicksPerSecond = 0;
  22. static ImGuiMouseCursor g_LastMouseCursor = ImGuiMouseCursor_COUNT;
  23. // DirectX data
  24. static ID3D12Device* g_pd3dDevice = NULL;
  25. static ID3D12GraphicsCommandList* g_pd3dCommandList = NULL;
  26. static ID3D10Blob* g_pVertexShaderBlob = NULL;
  27. static ID3D10Blob* g_pPixelShaderBlob = NULL;
  28. static ID3D12RootSignature* g_pRootSignature = NULL;
  29. static ID3D12PipelineState* g_pPipelineState = NULL;
  30. static DXGI_FORMAT g_RTVFormat = DXGI_FORMAT_UNKNOWN;
  31. static ID3D12Resource* g_pFontTextureResource = NULL;
  32. static D3D12_CPU_DESCRIPTOR_HANDLE g_hFontSrvCpuDescHandle = {};
  33. static D3D12_GPU_DESCRIPTOR_HANDLE g_hFontSrvGpuDescHandle = {};
  34. struct FrameResources
  35. {
  36. ID3D12Resource* IB;
  37. ID3D12Resource* VB;
  38. int VertexBufferSize;
  39. int IndexBufferSize;
  40. };
  41. static FrameResources* g_pFrameResources = NULL;
  42. static UINT g_numFramesInFlight = 0;
  43. static UINT g_frameIndex = UINT_MAX;
  44. struct VERTEX_CONSTANT_BUFFER
  45. {
  46. float mvp[4][4];
  47. };
  48. // Render function
  49. // (this used to be set in io.RenderDrawListsFn and called by ImGui::Render(), but you can now call this directly from your main loop)
  50. void ImGui_ImplDX12_RenderDrawData(ImDrawData* draw_data)
  51. {
  52. // NOTE: I'm assuming that this only get's called once per frame!
  53. // If not, we can't just re-allocate the IB or VB, we'll have to do a proper allocator.
  54. g_frameIndex = g_frameIndex + 1;
  55. FrameResources* frameResources = &g_pFrameResources[g_frameIndex % g_numFramesInFlight];
  56. ID3D12Resource* g_pVB = frameResources->VB;
  57. ID3D12Resource* g_pIB = frameResources->IB;
  58. int g_VertexBufferSize = frameResources->VertexBufferSize;
  59. int g_IndexBufferSize = frameResources->IndexBufferSize;
  60. ID3D12GraphicsCommandList* ctx = g_pd3dCommandList;
  61. // Create and grow vertex/index buffers if needed
  62. if (!g_pVB || g_VertexBufferSize < draw_data->TotalVtxCount)
  63. {
  64. if (g_pVB) { g_pVB->Release(); g_pVB = NULL; }
  65. g_VertexBufferSize = draw_data->TotalVtxCount + 5000;
  66. D3D12_HEAP_PROPERTIES props;
  67. memset(&props, 0, sizeof(D3D12_HEAP_PROPERTIES));
  68. props.Type = D3D12_HEAP_TYPE_UPLOAD;
  69. props.CPUPageProperty = D3D12_CPU_PAGE_PROPERTY_UNKNOWN;
  70. props.MemoryPoolPreference = D3D12_MEMORY_POOL_UNKNOWN;
  71. D3D12_RESOURCE_DESC desc;
  72. memset(&desc, 0, sizeof(D3D12_RESOURCE_DESC));
  73. desc.Dimension = D3D12_RESOURCE_DIMENSION_BUFFER;
  74. desc.Width = g_VertexBufferSize * sizeof(ImDrawVert);
  75. desc.Height = 1;
  76. desc.DepthOrArraySize = 1;
  77. desc.MipLevels = 1;
  78. desc.Format = DXGI_FORMAT_UNKNOWN;
  79. desc.SampleDesc.Count = 1;
  80. desc.Layout = D3D12_TEXTURE_LAYOUT_ROW_MAJOR;
  81. desc.Flags = D3D12_RESOURCE_FLAG_NONE;
  82. if (g_pd3dDevice->CreateCommittedResource(&props, D3D12_HEAP_FLAG_NONE,
  83. &desc, D3D12_RESOURCE_STATE_GENERIC_READ, NULL, IID_PPV_ARGS(&g_pVB)) < 0)
  84. return;
  85. frameResources->VB = g_pVB;
  86. frameResources->VertexBufferSize = g_VertexBufferSize;
  87. }
  88. if (!g_pIB || g_IndexBufferSize < draw_data->TotalIdxCount)
  89. {
  90. if (g_pIB) { g_pIB->Release(); g_pIB = NULL; }
  91. g_IndexBufferSize = draw_data->TotalIdxCount + 10000;
  92. D3D12_HEAP_PROPERTIES props;
  93. memset(&props, 0, sizeof(D3D12_HEAP_PROPERTIES));
  94. props.Type = D3D12_HEAP_TYPE_UPLOAD;
  95. props.CPUPageProperty = D3D12_CPU_PAGE_PROPERTY_UNKNOWN;
  96. props.MemoryPoolPreference = D3D12_MEMORY_POOL_UNKNOWN;
  97. D3D12_RESOURCE_DESC desc;
  98. memset(&desc, 0, sizeof(D3D12_RESOURCE_DESC));
  99. desc.Dimension = D3D12_RESOURCE_DIMENSION_BUFFER;
  100. desc.Width = g_IndexBufferSize * sizeof(ImDrawIdx);
  101. desc.Height = 1;
  102. desc.DepthOrArraySize = 1;
  103. desc.MipLevels = 1;
  104. desc.Format = DXGI_FORMAT_UNKNOWN;
  105. desc.SampleDesc.Count = 1;
  106. desc.Layout = D3D12_TEXTURE_LAYOUT_ROW_MAJOR;
  107. desc.Flags = D3D12_RESOURCE_FLAG_NONE;
  108. if (g_pd3dDevice->CreateCommittedResource(&props, D3D12_HEAP_FLAG_NONE,
  109. &desc, D3D12_RESOURCE_STATE_GENERIC_READ, NULL, IID_PPV_ARGS(&g_pIB)) < 0)
  110. return;
  111. frameResources->IB = g_pIB;
  112. frameResources->IndexBufferSize = g_IndexBufferSize;
  113. }
  114. // Copy and convert all vertices into a single contiguous buffer
  115. void* vtx_resource, *idx_resource;
  116. D3D12_RANGE range;
  117. memset(&range, 0, sizeof(D3D12_RANGE));
  118. if (g_pVB->Map(0, &range, &vtx_resource) != S_OK)
  119. return;
  120. if (g_pIB->Map(0, &range, &idx_resource) != S_OK)
  121. return;
  122. ImDrawVert* vtx_dst = (ImDrawVert*)vtx_resource;
  123. ImDrawIdx* idx_dst = (ImDrawIdx*)idx_resource;
  124. for (int n = 0; n < draw_data->CmdListsCount; n++)
  125. {
  126. const ImDrawList* cmd_list = draw_data->CmdLists[n];
  127. memcpy(vtx_dst, cmd_list->VtxBuffer.Data, cmd_list->VtxBuffer.Size * sizeof(ImDrawVert));
  128. memcpy(idx_dst, cmd_list->IdxBuffer.Data, cmd_list->IdxBuffer.Size * sizeof(ImDrawIdx));
  129. vtx_dst += cmd_list->VtxBuffer.Size;
  130. idx_dst += cmd_list->IdxBuffer.Size;
  131. }
  132. g_pVB->Unmap(0, &range);
  133. g_pIB->Unmap(0, &range);
  134. // Setup orthographic projection matrix into our constant buffer
  135. VERTEX_CONSTANT_BUFFER vertex_constant_buffer;
  136. {
  137. VERTEX_CONSTANT_BUFFER* constant_buffer = &vertex_constant_buffer;
  138. float L = 0.0f;
  139. float R = ImGui::GetIO().DisplaySize.x;
  140. float B = ImGui::GetIO().DisplaySize.y;
  141. float T = 0.0f;
  142. float mvp[4][4] =
  143. {
  144. { 2.0f/(R-L), 0.0f, 0.0f, 0.0f },
  145. { 0.0f, 2.0f/(T-B), 0.0f, 0.0f },
  146. { 0.0f, 0.0f, 0.5f, 0.0f },
  147. { (R+L)/(L-R), (T+B)/(B-T), 0.5f, 1.0f },
  148. };
  149. memcpy(&constant_buffer->mvp, mvp, sizeof(mvp));
  150. }
  151. // Setup viewport
  152. D3D12_VIEWPORT vp;
  153. memset(&vp, 0, sizeof(D3D12_VIEWPORT));
  154. vp.Width = ImGui::GetIO().DisplaySize.x;
  155. vp.Height = ImGui::GetIO().DisplaySize.y;
  156. vp.MinDepth = 0.0f;
  157. vp.MaxDepth = 1.0f;
  158. vp.TopLeftX = vp.TopLeftY = 0.0f;
  159. ctx->RSSetViewports(1, &vp);
  160. // Bind shader and vertex buffers
  161. unsigned int stride = sizeof(ImDrawVert);
  162. unsigned int offset = 0;
  163. D3D12_VERTEX_BUFFER_VIEW vbv;
  164. memset(&vbv, 0, sizeof(D3D12_VERTEX_BUFFER_VIEW));
  165. vbv.BufferLocation = g_pVB->GetGPUVirtualAddress() + offset;
  166. vbv.SizeInBytes = g_VertexBufferSize * stride;
  167. vbv.StrideInBytes = stride;
  168. ctx->IASetVertexBuffers(0, 1, &vbv);
  169. D3D12_INDEX_BUFFER_VIEW ibv;
  170. memset(&ibv, 0, sizeof(D3D12_INDEX_BUFFER_VIEW));
  171. ibv.BufferLocation = g_pIB->GetGPUVirtualAddress();
  172. ibv.SizeInBytes = g_IndexBufferSize * sizeof(ImDrawIdx);
  173. ibv.Format = sizeof(ImDrawIdx) == 2 ? DXGI_FORMAT_R16_UINT : DXGI_FORMAT_R32_UINT;
  174. ctx->IASetIndexBuffer(&ibv);
  175. ctx->IASetPrimitiveTopology(D3D_PRIMITIVE_TOPOLOGY_TRIANGLELIST);
  176. ctx->SetPipelineState(g_pPipelineState);
  177. ctx->SetGraphicsRootSignature(g_pRootSignature);
  178. ctx->SetGraphicsRoot32BitConstants(0, 16, &vertex_constant_buffer, 0);
  179. // Setup render state
  180. const float blend_factor[4] = { 0.f, 0.f, 0.f, 0.f };
  181. ctx->OMSetBlendFactor(blend_factor);
  182. // Render command lists
  183. int vtx_offset = 0;
  184. int idx_offset = 0;
  185. for (int n = 0; n < draw_data->CmdListsCount; n++)
  186. {
  187. const ImDrawList* cmd_list = draw_data->CmdLists[n];
  188. for (int cmd_i = 0; cmd_i < cmd_list->CmdBuffer.Size; cmd_i++)
  189. {
  190. const ImDrawCmd* pcmd = &cmd_list->CmdBuffer[cmd_i];
  191. if (pcmd->UserCallback)
  192. {
  193. pcmd->UserCallback(cmd_list, pcmd);
  194. }
  195. else
  196. {
  197. const D3D12_RECT r = { (LONG)pcmd->ClipRect.x, (LONG)pcmd->ClipRect.y, (LONG)pcmd->ClipRect.z, (LONG)pcmd->ClipRect.w };
  198. ctx->SetGraphicsRootDescriptorTable(1, *(D3D12_GPU_DESCRIPTOR_HANDLE*)&pcmd->TextureId);
  199. ctx->RSSetScissorRects(1, &r);
  200. ctx->DrawIndexedInstanced(pcmd->ElemCount, 1, idx_offset, vtx_offset, 0);
  201. }
  202. idx_offset += pcmd->ElemCount;
  203. }
  204. vtx_offset += cmd_list->VtxBuffer.Size;
  205. }
  206. }
  207. static bool ImGui_ImplWin32_UpdateMouseCursor()
  208. {
  209. ImGuiIO& io = ImGui::GetIO();
  210. if (io.ConfigFlags & ImGuiConfigFlags_NoMouseCursorChange)
  211. return false;
  212. ImGuiMouseCursor imgui_cursor = io.MouseDrawCursor ? ImGuiMouseCursor_None : ImGui::GetMouseCursor();
  213. if (imgui_cursor == ImGuiMouseCursor_None)
  214. {
  215. // Hide OS mouse cursor if imgui is drawing it or if it wants no cursor
  216. ::SetCursor(NULL);
  217. }
  218. else
  219. {
  220. // Hardware cursor type
  221. LPTSTR win32_cursor = IDC_ARROW;
  222. switch (imgui_cursor)
  223. {
  224. case ImGuiMouseCursor_Arrow: win32_cursor = IDC_ARROW; break;
  225. case ImGuiMouseCursor_TextInput: win32_cursor = IDC_IBEAM; break;
  226. case ImGuiMouseCursor_ResizeAll: win32_cursor = IDC_SIZEALL; break;
  227. case ImGuiMouseCursor_ResizeEW: win32_cursor = IDC_SIZEWE; break;
  228. case ImGuiMouseCursor_ResizeNS: win32_cursor = IDC_SIZENS; break;
  229. case ImGuiMouseCursor_ResizeNESW: win32_cursor = IDC_SIZENESW; break;
  230. case ImGuiMouseCursor_ResizeNWSE: win32_cursor = IDC_SIZENWSE; break;
  231. }
  232. ::SetCursor(::LoadCursor(NULL, win32_cursor));
  233. }
  234. return true;
  235. }
  236. // Allow compilation with old Windows SDK. MinGW doesn't have default _WIN32_WINNT/WINVER versions.
  237. #ifndef WM_MOUSEHWHEEL
  238. #define WM_MOUSEHWHEEL 0x020E
  239. #endif
  240. // Process Win32 mouse/keyboard inputs.
  241. // You can read the io.WantCaptureMouse, io.WantCaptureKeyboard flags to tell if dear imgui wants to use your inputs.
  242. // - When io.WantCaptureMouse is true, do not dispatch mouse input data to your main application.
  243. // - When io.WantCaptureKeyboard is true, do not dispatch keyboard input data to your main application.
  244. // Generally you may always pass all inputs to dear imgui, and hide them from your application based on those two flags.
  245. // PS: In this Win32 handler, we use the capture API (GetCapture/SetCapture/ReleaseCapture) to be able to read mouse coordinations when dragging mouse outside of our window bounds.
  246. // PS: We treat DBLCLK messages as regular mouse down messages, so this code will work on windows classes that have the CS_DBLCLKS flag set. Our own example app code doesn't set this flag.
  247. IMGUI_API LRESULT ImGui_ImplWin32_WndProcHandler(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
  248. {
  249. if (ImGui::GetCurrentContext() == NULL)
  250. return 0;
  251. ImGuiIO& io = ImGui::GetIO();
  252. switch (msg)
  253. {
  254. case WM_LBUTTONDOWN: case WM_LBUTTONDBLCLK:
  255. case WM_RBUTTONDOWN: case WM_RBUTTONDBLCLK:
  256. case WM_MBUTTONDOWN: case WM_MBUTTONDBLCLK:
  257. {
  258. int button = 0;
  259. if (msg == WM_LBUTTONDOWN || msg == WM_LBUTTONDBLCLK) button = 0;
  260. if (msg == WM_RBUTTONDOWN || msg == WM_RBUTTONDBLCLK) button = 1;
  261. if (msg == WM_MBUTTONDOWN || msg == WM_MBUTTONDBLCLK) button = 2;
  262. if (!ImGui::IsAnyMouseDown() && ::GetCapture() == NULL)
  263. ::SetCapture(hwnd);
  264. io.MouseDown[button] = true;
  265. return 0;
  266. }
  267. case WM_LBUTTONUP:
  268. case WM_RBUTTONUP:
  269. case WM_MBUTTONUP:
  270. {
  271. int button = 0;
  272. if (msg == WM_LBUTTONUP) button = 0;
  273. if (msg == WM_RBUTTONUP) button = 1;
  274. if (msg == WM_MBUTTONUP) button = 2;
  275. io.MouseDown[button] = false;
  276. if (!ImGui::IsAnyMouseDown() && ::GetCapture() == hwnd)
  277. ::ReleaseCapture();
  278. return 0;
  279. }
  280. case WM_MOUSEWHEEL:
  281. io.MouseWheel += GET_WHEEL_DELTA_WPARAM(wParam) > 0 ? +1.0f : -1.0f;
  282. return 0;
  283. case WM_MOUSEHWHEEL:
  284. io.MouseWheelH += GET_WHEEL_DELTA_WPARAM(wParam) > 0 ? +1.0f : -1.0f;
  285. return 0;
  286. case WM_MOUSEMOVE:
  287. io.MousePos.x = (signed short)(lParam);
  288. io.MousePos.y = (signed short)(lParam >> 16);
  289. return 0;
  290. case WM_KEYDOWN:
  291. case WM_SYSKEYDOWN:
  292. if (wParam < 256)
  293. io.KeysDown[wParam] = 1;
  294. return 0;
  295. case WM_KEYUP:
  296. case WM_SYSKEYUP:
  297. if (wParam < 256)
  298. io.KeysDown[wParam] = 0;
  299. return 0;
  300. case WM_CHAR:
  301. // You can also use ToAscii()+GetKeyboardState() to retrieve characters.
  302. if (wParam > 0 && wParam < 0x10000)
  303. io.AddInputCharacter((unsigned short)wParam);
  304. return 0;
  305. case WM_SETCURSOR:
  306. if (LOWORD(lParam) == HTCLIENT && ImGui_ImplWin32_UpdateMouseCursor())
  307. return 1;
  308. return 0;
  309. }
  310. return 0;
  311. }
  312. static void ImGui_ImplDX12_CreateFontsTexture()
  313. {
  314. // Build texture atlas
  315. ImGuiIO& io = ImGui::GetIO();
  316. unsigned char* pixels;
  317. int width, height;
  318. io.Fonts->GetTexDataAsRGBA32(&pixels, &width, &height);
  319. // Upload texture to graphics system
  320. {
  321. D3D12_HEAP_PROPERTIES props;
  322. memset(&props, 0, sizeof(D3D12_HEAP_PROPERTIES));
  323. props.Type = D3D12_HEAP_TYPE_DEFAULT;
  324. props.CPUPageProperty = D3D12_CPU_PAGE_PROPERTY_UNKNOWN;
  325. props.MemoryPoolPreference = D3D12_MEMORY_POOL_UNKNOWN;
  326. D3D12_RESOURCE_DESC desc;
  327. ZeroMemory(&desc, sizeof(desc));
  328. desc.Dimension = D3D12_RESOURCE_DIMENSION_TEXTURE2D;
  329. desc.Alignment = 0;
  330. desc.Width = width;
  331. desc.Height = height;
  332. desc.DepthOrArraySize = 1;
  333. desc.MipLevels = 1;
  334. desc.Format = DXGI_FORMAT_R8G8B8A8_UNORM;
  335. desc.SampleDesc.Count = 1;
  336. desc.SampleDesc.Quality = 0;
  337. desc.Layout = D3D12_TEXTURE_LAYOUT_UNKNOWN;
  338. desc.Flags = D3D12_RESOURCE_FLAG_NONE;
  339. ID3D12Resource* pTexture = NULL;
  340. g_pd3dDevice->CreateCommittedResource(&props, D3D12_HEAP_FLAG_NONE, &desc,
  341. D3D12_RESOURCE_STATE_COPY_DEST, NULL, IID_PPV_ARGS(&pTexture));
  342. UINT uploadPitch = (width * 4 + D3D12_TEXTURE_DATA_PITCH_ALIGNMENT - 1u) & ~(D3D12_TEXTURE_DATA_PITCH_ALIGNMENT - 1u);
  343. UINT uploadSize = height * uploadPitch;
  344. desc.Dimension = D3D12_RESOURCE_DIMENSION_BUFFER;
  345. desc.Alignment = 0;
  346. desc.Width = uploadSize;
  347. desc.Height = 1;
  348. desc.DepthOrArraySize = 1;
  349. desc.MipLevels = 1;
  350. desc.Format = DXGI_FORMAT_UNKNOWN;
  351. desc.SampleDesc.Count = 1;
  352. desc.SampleDesc.Quality = 0;
  353. desc.Layout = D3D12_TEXTURE_LAYOUT_ROW_MAJOR;
  354. desc.Flags = D3D12_RESOURCE_FLAG_NONE;
  355. props.Type = D3D12_HEAP_TYPE_UPLOAD;
  356. props.CPUPageProperty = D3D12_CPU_PAGE_PROPERTY_UNKNOWN;
  357. props.MemoryPoolPreference = D3D12_MEMORY_POOL_UNKNOWN;
  358. ID3D12Resource* uploadBuffer = NULL;
  359. HRESULT hr = g_pd3dDevice->CreateCommittedResource(&props, D3D12_HEAP_FLAG_NONE, &desc,
  360. D3D12_RESOURCE_STATE_GENERIC_READ, NULL, IID_PPV_ARGS(&uploadBuffer));
  361. IM_ASSERT(SUCCEEDED(hr));
  362. void* mapped = NULL;
  363. D3D12_RANGE range = { 0, uploadSize };
  364. hr = uploadBuffer->Map(0, &range, &mapped);
  365. IM_ASSERT(SUCCEEDED(hr));
  366. for (int y = 0; y < height; y++)
  367. memcpy((void*) ((uintptr_t) mapped + y * uploadPitch), pixels + y * width * 4, width * 4);
  368. uploadBuffer->Unmap(0, &range);
  369. D3D12_TEXTURE_COPY_LOCATION srcLocation = {};
  370. srcLocation.pResource = uploadBuffer;
  371. srcLocation.Type = D3D12_TEXTURE_COPY_TYPE_PLACED_FOOTPRINT;
  372. srcLocation.PlacedFootprint.Footprint.Format = DXGI_FORMAT_R8G8B8A8_UNORM;
  373. srcLocation.PlacedFootprint.Footprint.Width = width;
  374. srcLocation.PlacedFootprint.Footprint.Height = height;
  375. srcLocation.PlacedFootprint.Footprint.Depth = 1;
  376. srcLocation.PlacedFootprint.Footprint.RowPitch = uploadPitch;
  377. D3D12_TEXTURE_COPY_LOCATION dstLocation = {};
  378. dstLocation.pResource = pTexture;
  379. dstLocation.Type = D3D12_TEXTURE_COPY_TYPE_SUBRESOURCE_INDEX;
  380. dstLocation.SubresourceIndex = 0;
  381. D3D12_RESOURCE_BARRIER barrier = {};
  382. barrier.Type = D3D12_RESOURCE_BARRIER_TYPE_TRANSITION;
  383. barrier.Flags = D3D12_RESOURCE_BARRIER_FLAG_NONE;
  384. barrier.Transition.pResource = pTexture;
  385. barrier.Transition.Subresource = D3D12_RESOURCE_BARRIER_ALL_SUBRESOURCES;
  386. barrier.Transition.StateBefore = D3D12_RESOURCE_STATE_COPY_DEST;
  387. barrier.Transition.StateAfter = D3D12_RESOURCE_STATE_PIXEL_SHADER_RESOURCE;
  388. ID3D12Fence* fence = NULL;
  389. hr = g_pd3dDevice->CreateFence(0, D3D12_FENCE_FLAG_NONE, IID_PPV_ARGS(&fence));
  390. IM_ASSERT(SUCCEEDED(hr));
  391. HANDLE event = CreateEvent(0, 0, 0, 0);
  392. IM_ASSERT(event != NULL);
  393. D3D12_COMMAND_QUEUE_DESC queueDesc = {};
  394. queueDesc.Type = D3D12_COMMAND_LIST_TYPE_DIRECT;
  395. queueDesc.Flags = D3D12_COMMAND_QUEUE_FLAG_NONE;
  396. queueDesc.NodeMask = 1;
  397. ID3D12CommandQueue* cmdQueue = NULL;
  398. hr = g_pd3dDevice->CreateCommandQueue(&queueDesc, IID_PPV_ARGS(&cmdQueue));
  399. IM_ASSERT(SUCCEEDED(hr));
  400. ID3D12CommandAllocator* cmdAlloc = NULL;
  401. hr = g_pd3dDevice->CreateCommandAllocator(D3D12_COMMAND_LIST_TYPE_DIRECT, IID_PPV_ARGS(&cmdAlloc));
  402. IM_ASSERT(SUCCEEDED(hr));
  403. ID3D12GraphicsCommandList* cmdList = NULL;
  404. hr = g_pd3dDevice->CreateCommandList(0, D3D12_COMMAND_LIST_TYPE_DIRECT, cmdAlloc, NULL, IID_PPV_ARGS(&cmdList));
  405. IM_ASSERT(SUCCEEDED(hr));
  406. cmdList->CopyTextureRegion(&dstLocation, 0, 0, 0, &srcLocation, NULL);
  407. cmdList->ResourceBarrier(1, &barrier);
  408. hr = cmdList->Close();
  409. IM_ASSERT(SUCCEEDED(hr));
  410. cmdQueue->ExecuteCommandLists(1, (ID3D12CommandList* const*) &cmdList);
  411. hr = cmdQueue->Signal(fence, 1);
  412. IM_ASSERT(SUCCEEDED(hr));
  413. fence->SetEventOnCompletion(1, event);
  414. WaitForSingleObject(event, INFINITE);
  415. cmdList->Release();
  416. cmdAlloc->Release();
  417. cmdQueue->Release();
  418. CloseHandle(event);
  419. fence->Release();
  420. uploadBuffer->Release();
  421. // Create texture view
  422. D3D12_SHADER_RESOURCE_VIEW_DESC srvDesc;
  423. ZeroMemory(&srvDesc, sizeof(srvDesc));
  424. srvDesc.Format = DXGI_FORMAT_R8G8B8A8_UNORM;
  425. srvDesc.ViewDimension = D3D12_SRV_DIMENSION_TEXTURE2D;
  426. srvDesc.Texture2D.MipLevels = desc.MipLevels;
  427. srvDesc.Texture2D.MostDetailedMip = 0;
  428. srvDesc.Shader4ComponentMapping = D3D12_DEFAULT_SHADER_4_COMPONENT_MAPPING;
  429. g_pd3dDevice->CreateShaderResourceView(pTexture, &srvDesc, g_hFontSrvCpuDescHandle);
  430. if (g_pFontTextureResource != NULL)
  431. g_pFontTextureResource->Release();
  432. g_pFontTextureResource = pTexture;
  433. }
  434. // Store our identifier
  435. static_assert(sizeof(ImTextureID) >= sizeof(g_hFontSrvGpuDescHandle.ptr), "Can't pack descriptor handle into TexID");
  436. io.Fonts->TexID = (void *)g_hFontSrvGpuDescHandle.ptr;
  437. }
  438. bool ImGui_ImplDX12_CreateDeviceObjects()
  439. {
  440. if (!g_pd3dDevice)
  441. return false;
  442. if (g_pPipelineState)
  443. ImGui_ImplDX12_InvalidateDeviceObjects();
  444. // Create the root signature
  445. {
  446. D3D12_DESCRIPTOR_RANGE descRange = {};
  447. descRange.RangeType = D3D12_DESCRIPTOR_RANGE_TYPE_SRV;
  448. descRange.NumDescriptors = 1;
  449. descRange.BaseShaderRegister = 0;
  450. descRange.RegisterSpace = 0;
  451. descRange.OffsetInDescriptorsFromTableStart = 0;
  452. D3D12_ROOT_PARAMETER param[2] = {};
  453. param[0].ParameterType = D3D12_ROOT_PARAMETER_TYPE_32BIT_CONSTANTS;
  454. param[0].Constants.ShaderRegister = 0;
  455. param[0].Constants.RegisterSpace = 0;
  456. param[0].Constants.Num32BitValues = 16;
  457. param[0].ShaderVisibility = D3D12_SHADER_VISIBILITY_VERTEX;
  458. param[1].ParameterType = D3D12_ROOT_PARAMETER_TYPE_DESCRIPTOR_TABLE;
  459. param[1].DescriptorTable.NumDescriptorRanges = 1;
  460. param[1].DescriptorTable.pDescriptorRanges = &descRange;
  461. param[1].ShaderVisibility = D3D12_SHADER_VISIBILITY_PIXEL;
  462. D3D12_STATIC_SAMPLER_DESC staticSampler = {};
  463. staticSampler.Filter = D3D12_FILTER_MIN_MAG_MIP_LINEAR;
  464. staticSampler.AddressU = D3D12_TEXTURE_ADDRESS_MODE_WRAP;
  465. staticSampler.AddressV = D3D12_TEXTURE_ADDRESS_MODE_WRAP;
  466. staticSampler.AddressW = D3D12_TEXTURE_ADDRESS_MODE_WRAP;
  467. staticSampler.MipLODBias = 0.f;
  468. staticSampler.MaxAnisotropy = 0;
  469. staticSampler.ComparisonFunc = D3D12_COMPARISON_FUNC_ALWAYS;
  470. staticSampler.BorderColor = D3D12_STATIC_BORDER_COLOR_TRANSPARENT_BLACK;
  471. staticSampler.MinLOD = 0.f;
  472. staticSampler.MaxLOD = 0.f;
  473. staticSampler.ShaderRegister = 0;
  474. staticSampler.RegisterSpace = 0;
  475. staticSampler.ShaderVisibility = D3D12_SHADER_VISIBILITY_PIXEL;
  476. D3D12_ROOT_SIGNATURE_DESC desc = {};
  477. desc.NumParameters = _countof(param);
  478. desc.pParameters = param;
  479. desc.NumStaticSamplers = 1;
  480. desc.pStaticSamplers = &staticSampler;
  481. desc.Flags =
  482. D3D12_ROOT_SIGNATURE_FLAG_ALLOW_INPUT_ASSEMBLER_INPUT_LAYOUT |
  483. D3D12_ROOT_SIGNATURE_FLAG_DENY_HULL_SHADER_ROOT_ACCESS |
  484. D3D12_ROOT_SIGNATURE_FLAG_DENY_DOMAIN_SHADER_ROOT_ACCESS |
  485. D3D12_ROOT_SIGNATURE_FLAG_DENY_GEOMETRY_SHADER_ROOT_ACCESS;
  486. ID3DBlob* blob = NULL;
  487. if (D3D12SerializeRootSignature(&desc, D3D_ROOT_SIGNATURE_VERSION_1, &blob, NULL) != S_OK)
  488. return false;
  489. g_pd3dDevice->CreateRootSignature(0, blob->GetBufferPointer(), blob->GetBufferSize(), IID_PPV_ARGS(&g_pRootSignature));
  490. blob->Release();
  491. }
  492. // By using D3DCompile() from <d3dcompiler.h> / d3dcompiler.lib, we introduce a dependency to a given version of d3dcompiler_XX.dll (see D3DCOMPILER_DLL_A)
  493. // If you would like to use this DX12 sample code but remove this dependency you can:
  494. // 1) compile once, save the compiled shader blobs into a file or source code and pass them to CreateVertexShader()/CreatePixelShader() [preferred solution]
  495. // 2) use code to detect any version of the DLL and grab a pointer to D3DCompile from the DLL.
  496. // See https://github.com/ocornut/imgui/pull/638 for sources and details.
  497. D3D12_GRAPHICS_PIPELINE_STATE_DESC psoDesc;
  498. memset(&psoDesc, 0, sizeof(D3D12_GRAPHICS_PIPELINE_STATE_DESC));
  499. psoDesc.NodeMask = 1;
  500. psoDesc.PrimitiveTopologyType = D3D12_PRIMITIVE_TOPOLOGY_TYPE_TRIANGLE;
  501. psoDesc.pRootSignature = g_pRootSignature;
  502. psoDesc.SampleMask = UINT_MAX;
  503. psoDesc.NumRenderTargets = 1;
  504. psoDesc.RTVFormats[0] = g_RTVFormat;
  505. psoDesc.SampleDesc.Count = 1;
  506. psoDesc.Flags = D3D12_PIPELINE_STATE_FLAG_NONE;
  507. // Create the vertex shader
  508. {
  509. static const char* vertexShader =
  510. "cbuffer vertexBuffer : register(b0) \
  511. {\
  512. float4x4 ProjectionMatrix; \
  513. };\
  514. struct VS_INPUT\
  515. {\
  516. float2 pos : POSITION;\
  517. float4 col : COLOR0;\
  518. float2 uv : TEXCOORD0;\
  519. };\
  520. \
  521. struct PS_INPUT\
  522. {\
  523. float4 pos : SV_POSITION;\
  524. float4 col : COLOR0;\
  525. float2 uv : TEXCOORD0;\
  526. };\
  527. \
  528. PS_INPUT main(VS_INPUT input)\
  529. {\
  530. PS_INPUT output;\
  531. output.pos = mul( ProjectionMatrix, float4(input.pos.xy, 0.f, 1.f));\
  532. output.col = input.col;\
  533. output.uv = input.uv;\
  534. return output;\
  535. }";
  536. D3DCompile(vertexShader, strlen(vertexShader), NULL, NULL, NULL, "main", "vs_5_0", 0, 0, &g_pVertexShaderBlob, NULL);
  537. if (g_pVertexShaderBlob == NULL) // NB: Pass ID3D10Blob* pErrorBlob to D3DCompile() to get error showing in (const char*)pErrorBlob->GetBufferPointer(). Make sure to Release() the blob!
  538. return false;
  539. psoDesc.VS = { g_pVertexShaderBlob->GetBufferPointer(), g_pVertexShaderBlob->GetBufferSize() };
  540. // Create the input layout
  541. static D3D12_INPUT_ELEMENT_DESC local_layout[] = {
  542. { "POSITION", 0, DXGI_FORMAT_R32G32_FLOAT, 0, (size_t)(&((ImDrawVert*)0)->pos), D3D12_INPUT_CLASSIFICATION_PER_VERTEX_DATA, 0 },
  543. { "TEXCOORD", 0, DXGI_FORMAT_R32G32_FLOAT, 0, (size_t)(&((ImDrawVert*)0)->uv), D3D12_INPUT_CLASSIFICATION_PER_VERTEX_DATA, 0 },
  544. { "COLOR", 0, DXGI_FORMAT_R8G8B8A8_UNORM, 0, (size_t)(&((ImDrawVert*)0)->col), D3D12_INPUT_CLASSIFICATION_PER_VERTEX_DATA, 0 },
  545. };
  546. psoDesc.InputLayout = { local_layout, 3 };
  547. }
  548. // Create the pixel shader
  549. {
  550. static const char* pixelShader =
  551. "struct PS_INPUT\
  552. {\
  553. float4 pos : SV_POSITION;\
  554. float4 col : COLOR0;\
  555. float2 uv : TEXCOORD0;\
  556. };\
  557. SamplerState sampler0 : register(s0);\
  558. Texture2D texture0 : register(t0);\
  559. \
  560. float4 main(PS_INPUT input) : SV_Target\
  561. {\
  562. float4 out_col = input.col * texture0.Sample(sampler0, input.uv); \
  563. return out_col; \
  564. }";
  565. D3DCompile(pixelShader, strlen(pixelShader), NULL, NULL, NULL, "main", "ps_5_0", 0, 0, &g_pPixelShaderBlob, NULL);
  566. if (g_pPixelShaderBlob == NULL) // NB: Pass ID3D10Blob* pErrorBlob to D3DCompile() to get error showing in (const char*)pErrorBlob->GetBufferPointer(). Make sure to Release() the blob!
  567. return false;
  568. psoDesc.PS = { g_pPixelShaderBlob->GetBufferPointer(), g_pPixelShaderBlob->GetBufferSize() };
  569. }
  570. // Create the blending setup
  571. {
  572. D3D12_BLEND_DESC& desc = psoDesc.BlendState;
  573. desc.AlphaToCoverageEnable = false;
  574. desc.RenderTarget[0].BlendEnable = true;
  575. desc.RenderTarget[0].SrcBlend = D3D12_BLEND_SRC_ALPHA;
  576. desc.RenderTarget[0].DestBlend = D3D12_BLEND_INV_SRC_ALPHA;
  577. desc.RenderTarget[0].BlendOp = D3D12_BLEND_OP_ADD;
  578. desc.RenderTarget[0].SrcBlendAlpha = D3D12_BLEND_INV_SRC_ALPHA;
  579. desc.RenderTarget[0].DestBlendAlpha = D3D12_BLEND_ZERO;
  580. desc.RenderTarget[0].BlendOpAlpha = D3D12_BLEND_OP_ADD;
  581. desc.RenderTarget[0].RenderTargetWriteMask = D3D12_COLOR_WRITE_ENABLE_ALL;
  582. }
  583. // Create the rasterizer state
  584. {
  585. D3D12_RASTERIZER_DESC& desc = psoDesc.RasterizerState;
  586. desc.FillMode = D3D12_FILL_MODE_SOLID;
  587. desc.CullMode = D3D12_CULL_MODE_NONE;
  588. desc.FrontCounterClockwise = FALSE;
  589. desc.DepthBias = D3D12_DEFAULT_DEPTH_BIAS;
  590. desc.DepthBiasClamp = D3D12_DEFAULT_DEPTH_BIAS_CLAMP;
  591. desc.SlopeScaledDepthBias = D3D12_DEFAULT_SLOPE_SCALED_DEPTH_BIAS;
  592. desc.DepthClipEnable = true;
  593. desc.MultisampleEnable = FALSE;
  594. desc.AntialiasedLineEnable = FALSE;
  595. desc.ForcedSampleCount = 0;
  596. desc.ConservativeRaster = D3D12_CONSERVATIVE_RASTERIZATION_MODE_OFF;
  597. }
  598. // Create depth-stencil State
  599. {
  600. D3D12_DEPTH_STENCIL_DESC& desc = psoDesc.DepthStencilState;
  601. desc.DepthEnable = false;
  602. desc.DepthWriteMask = D3D12_DEPTH_WRITE_MASK_ALL;
  603. desc.DepthFunc = D3D12_COMPARISON_FUNC_ALWAYS;
  604. desc.StencilEnable = false;
  605. desc.FrontFace.StencilFailOp = desc.FrontFace.StencilDepthFailOp = desc.FrontFace.StencilPassOp = D3D12_STENCIL_OP_KEEP;
  606. desc.FrontFace.StencilFunc = D3D12_COMPARISON_FUNC_ALWAYS;
  607. desc.BackFace = desc.FrontFace;
  608. }
  609. if (g_pd3dDevice->CreateGraphicsPipelineState(&psoDesc, IID_PPV_ARGS(&g_pPipelineState)) != S_OK)
  610. return false;
  611. ImGui_ImplDX12_CreateFontsTexture();
  612. return true;
  613. }
  614. void ImGui_ImplDX12_InvalidateDeviceObjects()
  615. {
  616. if (!g_pd3dDevice)
  617. return;
  618. if (g_pVertexShaderBlob) { g_pVertexShaderBlob->Release(); g_pVertexShaderBlob = NULL; }
  619. if (g_pPixelShaderBlob) { g_pPixelShaderBlob->Release(); g_pPixelShaderBlob = NULL; }
  620. if (g_pRootSignature) { g_pRootSignature->Release(); g_pRootSignature = NULL; }
  621. if (g_pPipelineState) { g_pPipelineState->Release(); g_pPipelineState = NULL; }
  622. if (g_pFontTextureResource) { g_pFontTextureResource->Release(); g_pFontTextureResource = NULL; ImGui::GetIO().Fonts->TexID = NULL; } // We copied g_pFontTextureView to io.Fonts->TexID so let's clear that as well.
  623. for (UINT i = 0; i < g_numFramesInFlight; i++)
  624. {
  625. if (g_pFrameResources[i].IB) { g_pFrameResources[i].IB->Release(); g_pFrameResources[i].IB = NULL; }
  626. if (g_pFrameResources[i].VB) { g_pFrameResources[i].VB->Release(); g_pFrameResources[i].VB = NULL; }
  627. }
  628. }
  629. bool ImGui_ImplDX12_Init(void* hwnd, int num_frames_in_flight,
  630. ID3D12Device* device,
  631. DXGI_FORMAT rtv_format,
  632. D3D12_CPU_DESCRIPTOR_HANDLE font_srv_cpu_desc_handle,
  633. D3D12_GPU_DESCRIPTOR_HANDLE font_srv_gpu_desc_handle)
  634. {
  635. g_hWnd = (HWND)hwnd;
  636. g_pd3dDevice = device;
  637. g_RTVFormat = rtv_format;
  638. g_hFontSrvCpuDescHandle = font_srv_cpu_desc_handle;
  639. g_hFontSrvGpuDescHandle = font_srv_gpu_desc_handle;
  640. g_pFrameResources = new FrameResources [num_frames_in_flight];
  641. g_numFramesInFlight = num_frames_in_flight;
  642. g_frameIndex = UINT_MAX;
  643. for (int i = 0; i < num_frames_in_flight; i++)
  644. {
  645. g_pFrameResources[i].IB = NULL;
  646. g_pFrameResources[i].VB = NULL;
  647. g_pFrameResources[i].VertexBufferSize = 5000;
  648. g_pFrameResources[i].IndexBufferSize = 10000;
  649. }
  650. if (!QueryPerformanceFrequency((LARGE_INTEGER *)&g_TicksPerSecond))
  651. return false;
  652. if (!QueryPerformanceCounter((LARGE_INTEGER *)&g_Time))
  653. return false;
  654. // Setup back-end capabilities flags
  655. ImGuiIO& io = ImGui::GetIO();
  656. io.BackendFlags |= ImGuiBackendFlags_HasMouseCursors; // We can honor GetMouseCursor() values (optional)
  657. io.BackendFlags |= ImGuiBackendFlags_HasSetMousePos; // We can honor io.WantSetMousePos requests (optional, rarely used)
  658. // Keyboard mapping. ImGui will use those indices to peek into the io.KeysDown[] array that we will update during the application lifetime.
  659. io.KeyMap[ImGuiKey_Tab] = VK_TAB;
  660. io.KeyMap[ImGuiKey_LeftArrow] = VK_LEFT;
  661. io.KeyMap[ImGuiKey_RightArrow] = VK_RIGHT;
  662. io.KeyMap[ImGuiKey_UpArrow] = VK_UP;
  663. io.KeyMap[ImGuiKey_DownArrow] = VK_DOWN;
  664. io.KeyMap[ImGuiKey_PageUp] = VK_PRIOR;
  665. io.KeyMap[ImGuiKey_PageDown] = VK_NEXT;
  666. io.KeyMap[ImGuiKey_Home] = VK_HOME;
  667. io.KeyMap[ImGuiKey_End] = VK_END;
  668. io.KeyMap[ImGuiKey_Insert] = VK_INSERT;
  669. io.KeyMap[ImGuiKey_Delete] = VK_DELETE;
  670. io.KeyMap[ImGuiKey_Backspace] = VK_BACK;
  671. io.KeyMap[ImGuiKey_Space] = VK_SPACE;
  672. io.KeyMap[ImGuiKey_Enter] = VK_RETURN;
  673. io.KeyMap[ImGuiKey_Escape] = VK_ESCAPE;
  674. io.KeyMap[ImGuiKey_A] = 'A';
  675. io.KeyMap[ImGuiKey_C] = 'C';
  676. io.KeyMap[ImGuiKey_V] = 'V';
  677. io.KeyMap[ImGuiKey_X] = 'X';
  678. io.KeyMap[ImGuiKey_Y] = 'Y';
  679. io.KeyMap[ImGuiKey_Z] = 'Z';
  680. io.ImeWindowHandle = g_hWnd;
  681. return true;
  682. }
  683. void ImGui_ImplDX12_Shutdown()
  684. {
  685. ImGui_ImplDX12_InvalidateDeviceObjects();
  686. delete[] g_pFrameResources;
  687. g_pd3dDevice = NULL;
  688. g_hWnd = (HWND)0;
  689. g_pd3dCommandList = NULL;
  690. g_hFontSrvCpuDescHandle.ptr = 0;
  691. g_hFontSrvGpuDescHandle.ptr = 0;
  692. g_pFrameResources = NULL;
  693. g_numFramesInFlight = 0;
  694. g_frameIndex = UINT_MAX;
  695. }
  696. void ImGui_ImplDX12_NewFrame(ID3D12GraphicsCommandList* command_list)
  697. {
  698. if (!g_pPipelineState)
  699. ImGui_ImplDX12_CreateDeviceObjects();
  700. g_pd3dCommandList = command_list;
  701. ImGuiIO& io = ImGui::GetIO();
  702. // Setup display size (every frame to accommodate for window resizing)
  703. RECT rect;
  704. GetClientRect(g_hWnd, &rect);
  705. io.DisplaySize = ImVec2((float)(rect.right - rect.left), (float)(rect.bottom - rect.top));
  706. // Setup time step
  707. INT64 current_time;
  708. QueryPerformanceCounter((LARGE_INTEGER *)&current_time);
  709. io.DeltaTime = (float)(current_time - g_Time) / g_TicksPerSecond;
  710. g_Time = current_time;
  711. // Read keyboard modifiers inputs
  712. io.KeyCtrl = (GetKeyState(VK_CONTROL) & 0x8000) != 0;
  713. io.KeyShift = (GetKeyState(VK_SHIFT) & 0x8000) != 0;
  714. io.KeyAlt = (GetKeyState(VK_MENU) & 0x8000) != 0;
  715. io.KeySuper = false;
  716. // io.KeysDown : filled by WM_KEYDOWN/WM_KEYUP events
  717. // io.MousePos : filled by WM_MOUSEMOVE events
  718. // io.MouseDown : filled by WM_*BUTTON* events
  719. // io.MouseWheel : filled by WM_MOUSEWHEEL events
  720. // Set OS mouse position if requested (only used when ImGuiConfigFlags_NavEnableSetMousePos is enabled by user)
  721. if (io.WantSetMousePos)
  722. {
  723. POINT pos = { (int)io.MousePos.x, (int)io.MousePos.y };
  724. ClientToScreen(g_hWnd, &pos);
  725. SetCursorPos(pos.x, pos.y);
  726. }
  727. // Update OS mouse cursor with the cursor requested by imgui
  728. ImGuiMouseCursor mouse_cursor = io.MouseDrawCursor ? ImGuiMouseCursor_None : ImGui::GetMouseCursor();
  729. if (g_LastMouseCursor != mouse_cursor)
  730. {
  731. g_LastMouseCursor = mouse_cursor;
  732. ImGui_ImplWin32_UpdateMouseCursor();
  733. }
  734. // Start the frame. This call will update the io.WantCaptureMouse, io.WantCaptureKeyboard flag that you can use to dispatch inputs (or not) to your application.
  735. ImGui::NewFrame();
  736. }