main.cpp 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448
  1. // dear imgui: standalone example application for DirectX 12
  2. // If you are new to dear imgui, see examples/README.txt and documentation at the top of imgui.cpp.
  3. // FIXME: 64-bit only for now! (Because sizeof(ImTextureId) == sizeof(void*))
  4. #include "imgui.h"
  5. #include "imgui_impl_win32.h"
  6. #include "imgui_impl_dx12.h"
  7. #include <d3d12.h>
  8. #include <dxgi1_4.h>
  9. #include <tchar.h>
  10. #define DX12_ENABLE_DEBUG_LAYER 0
  11. struct FrameContext
  12. {
  13. ID3D12CommandAllocator* CommandAllocator;
  14. UINT64 FenceValue;
  15. };
  16. // Data
  17. static int const NUM_FRAMES_IN_FLIGHT = 3;
  18. static FrameContext g_frameContext[NUM_FRAMES_IN_FLIGHT] = {};
  19. static UINT g_frameIndex = 0;
  20. static int const NUM_BACK_BUFFERS = 3;
  21. static ID3D12Device* g_pd3dDevice = NULL;
  22. static ID3D12DescriptorHeap* g_pd3dRtvDescHeap = NULL;
  23. static ID3D12DescriptorHeap* g_pd3dSrvDescHeap = NULL;
  24. static ID3D12CommandQueue* g_pd3dCommandQueue = NULL;
  25. static ID3D12GraphicsCommandList* g_pd3dCommandList = NULL;
  26. static ID3D12Fence* g_fence = NULL;
  27. static HANDLE g_fenceEvent = NULL;
  28. static UINT64 g_fenceLastSignaledValue = 0;
  29. static IDXGISwapChain3* g_pSwapChain = NULL;
  30. static HANDLE g_hSwapChainWaitableObject = NULL;
  31. static ID3D12Resource* g_mainRenderTargetResource[NUM_BACK_BUFFERS] = {};
  32. static D3D12_CPU_DESCRIPTOR_HANDLE g_mainRenderTargetDescriptor[NUM_BACK_BUFFERS] = {};
  33. void CreateRenderTarget()
  34. {
  35. for (UINT i = 0; i < NUM_BACK_BUFFERS; i++)
  36. {
  37. ID3D12Resource* pBackBuffer = NULL;
  38. g_pSwapChain->GetBuffer(i, IID_PPV_ARGS(&pBackBuffer));
  39. g_pd3dDevice->CreateRenderTargetView(pBackBuffer, NULL, g_mainRenderTargetDescriptor[i]);
  40. g_mainRenderTargetResource[i] = pBackBuffer;
  41. }
  42. }
  43. void WaitForLastSubmittedFrame()
  44. {
  45. FrameContext* frameCtxt = &g_frameContext[g_frameIndex % NUM_FRAMES_IN_FLIGHT];
  46. UINT64 fenceValue = frameCtxt->FenceValue;
  47. if (fenceValue == 0)
  48. return; // No fence was signaled
  49. frameCtxt->FenceValue = 0;
  50. if (g_fence->GetCompletedValue() >= fenceValue)
  51. return;
  52. g_fence->SetEventOnCompletion(fenceValue, g_fenceEvent);
  53. WaitForSingleObject(g_fenceEvent, INFINITE);
  54. }
  55. FrameContext* WaitForNextFrameResources()
  56. {
  57. UINT nextFrameIndex = g_frameIndex + 1;
  58. g_frameIndex = nextFrameIndex;
  59. HANDLE waitableObjects[] = { g_hSwapChainWaitableObject, NULL };
  60. DWORD numWaitableObjects = 1;
  61. FrameContext* frameCtxt = &g_frameContext[nextFrameIndex % NUM_FRAMES_IN_FLIGHT];
  62. UINT64 fenceValue = frameCtxt->FenceValue;
  63. if (fenceValue != 0) // means no fence was signaled
  64. {
  65. frameCtxt->FenceValue = 0;
  66. g_fence->SetEventOnCompletion(fenceValue, g_fenceEvent);
  67. waitableObjects[1] = g_fenceEvent;
  68. numWaitableObjects = 2;
  69. }
  70. WaitForMultipleObjects(numWaitableObjects, waitableObjects, TRUE, INFINITE);
  71. return frameCtxt;
  72. }
  73. void ResizeSwapChain(HWND hWnd, int width, int height)
  74. {
  75. DXGI_SWAP_CHAIN_DESC1 sd;
  76. g_pSwapChain->GetDesc1(&sd);
  77. sd.Width = width;
  78. sd.Height = height;
  79. IDXGIFactory4* dxgiFactory = NULL;
  80. g_pSwapChain->GetParent(IID_PPV_ARGS(&dxgiFactory));
  81. g_pSwapChain->Release();
  82. CloseHandle(g_hSwapChainWaitableObject);
  83. IDXGISwapChain1* swapChain1 = NULL;
  84. dxgiFactory->CreateSwapChainForHwnd(g_pd3dCommandQueue, hWnd, &sd, NULL, NULL, &swapChain1);
  85. swapChain1->QueryInterface(IID_PPV_ARGS(&g_pSwapChain));
  86. swapChain1->Release();
  87. dxgiFactory->Release();
  88. g_pSwapChain->SetMaximumFrameLatency(NUM_BACK_BUFFERS);
  89. g_hSwapChainWaitableObject = g_pSwapChain->GetFrameLatencyWaitableObject();
  90. assert(g_hSwapChainWaitableObject != NULL);
  91. }
  92. void CleanupRenderTarget()
  93. {
  94. WaitForLastSubmittedFrame();
  95. for (UINT i = 0; i < NUM_BACK_BUFFERS; i++)
  96. if (g_mainRenderTargetResource[i]) { g_mainRenderTargetResource[i]->Release(); g_mainRenderTargetResource[i] = NULL; }
  97. }
  98. HRESULT CreateDeviceD3D(HWND hWnd)
  99. {
  100. // Setup swap chain
  101. DXGI_SWAP_CHAIN_DESC1 sd;
  102. {
  103. ZeroMemory(&sd, sizeof(sd));
  104. sd.BufferCount = NUM_BACK_BUFFERS;
  105. sd.Width = 0;
  106. sd.Height = 0;
  107. sd.Format = DXGI_FORMAT_R8G8B8A8_UNORM;
  108. sd.Flags = DXGI_SWAP_CHAIN_FLAG_FRAME_LATENCY_WAITABLE_OBJECT;
  109. sd.BufferUsage = DXGI_USAGE_RENDER_TARGET_OUTPUT;
  110. sd.SampleDesc.Count = 1;
  111. sd.SampleDesc.Quality = 0;
  112. sd.SwapEffect = DXGI_SWAP_EFFECT_FLIP_DISCARD;
  113. sd.AlphaMode = DXGI_ALPHA_MODE_UNSPECIFIED;
  114. sd.Scaling = DXGI_SCALING_STRETCH;
  115. sd.Stereo = FALSE;
  116. }
  117. if (DX12_ENABLE_DEBUG_LAYER)
  118. {
  119. ID3D12Debug* dx12Debug = NULL;
  120. if (SUCCEEDED(D3D12GetDebugInterface(IID_PPV_ARGS(&dx12Debug))))
  121. {
  122. dx12Debug->EnableDebugLayer();
  123. dx12Debug->Release();
  124. }
  125. }
  126. D3D_FEATURE_LEVEL featureLevel = D3D_FEATURE_LEVEL_11_0;
  127. if (D3D12CreateDevice(NULL, featureLevel, IID_PPV_ARGS(&g_pd3dDevice)) != S_OK)
  128. return E_FAIL;
  129. {
  130. D3D12_DESCRIPTOR_HEAP_DESC desc = {};
  131. desc.Type = D3D12_DESCRIPTOR_HEAP_TYPE_RTV;
  132. desc.NumDescriptors = NUM_BACK_BUFFERS;
  133. desc.Flags = D3D12_DESCRIPTOR_HEAP_FLAG_NONE;
  134. desc.NodeMask = 1;
  135. if (g_pd3dDevice->CreateDescriptorHeap(&desc, IID_PPV_ARGS(&g_pd3dRtvDescHeap)) != S_OK)
  136. return E_FAIL;
  137. SIZE_T rtvDescriptorSize = g_pd3dDevice->GetDescriptorHandleIncrementSize(D3D12_DESCRIPTOR_HEAP_TYPE_RTV);
  138. D3D12_CPU_DESCRIPTOR_HANDLE rtvHandle = g_pd3dRtvDescHeap->GetCPUDescriptorHandleForHeapStart();
  139. for (UINT i = 0; i < NUM_BACK_BUFFERS; i++)
  140. {
  141. g_mainRenderTargetDescriptor[i] = rtvHandle;
  142. rtvHandle.ptr += rtvDescriptorSize;
  143. }
  144. }
  145. {
  146. D3D12_DESCRIPTOR_HEAP_DESC desc = {};
  147. desc.Type = D3D12_DESCRIPTOR_HEAP_TYPE_CBV_SRV_UAV;
  148. desc.NumDescriptors = 1;
  149. desc.Flags = D3D12_DESCRIPTOR_HEAP_FLAG_SHADER_VISIBLE;
  150. if (g_pd3dDevice->CreateDescriptorHeap(&desc, IID_PPV_ARGS(&g_pd3dSrvDescHeap)) != S_OK)
  151. return E_FAIL;
  152. }
  153. {
  154. D3D12_COMMAND_QUEUE_DESC desc = {};
  155. desc.Type = D3D12_COMMAND_LIST_TYPE_DIRECT;
  156. desc.Flags = D3D12_COMMAND_QUEUE_FLAG_NONE;
  157. desc.NodeMask = 1;
  158. if (g_pd3dDevice->CreateCommandQueue(&desc, IID_PPV_ARGS(&g_pd3dCommandQueue)) != S_OK)
  159. return E_FAIL;
  160. }
  161. for (UINT i = 0; i < NUM_FRAMES_IN_FLIGHT; i++)
  162. if (g_pd3dDevice->CreateCommandAllocator(D3D12_COMMAND_LIST_TYPE_DIRECT, IID_PPV_ARGS(&g_frameContext[i].CommandAllocator)) != S_OK)
  163. return E_FAIL;
  164. if (g_pd3dDevice->CreateCommandList(0, D3D12_COMMAND_LIST_TYPE_DIRECT, g_frameContext[0].CommandAllocator, NULL, IID_PPV_ARGS(&g_pd3dCommandList)) != S_OK ||
  165. g_pd3dCommandList->Close() != S_OK)
  166. return E_FAIL;
  167. if (g_pd3dDevice->CreateFence(0, D3D12_FENCE_FLAG_NONE, IID_PPV_ARGS(&g_fence)) != S_OK)
  168. return E_FAIL;
  169. g_fenceEvent = CreateEvent(NULL, FALSE, FALSE, NULL);
  170. if (g_fenceEvent == NULL)
  171. return E_FAIL;
  172. {
  173. IDXGIFactory4* dxgiFactory = NULL;
  174. IDXGISwapChain1* swapChain1 = NULL;
  175. if (CreateDXGIFactory1(IID_PPV_ARGS(&dxgiFactory)) != S_OK ||
  176. dxgiFactory->CreateSwapChainForHwnd(g_pd3dCommandQueue, hWnd, &sd, NULL, NULL, &swapChain1) != S_OK ||
  177. swapChain1->QueryInterface(IID_PPV_ARGS(&g_pSwapChain)) != S_OK)
  178. return E_FAIL;
  179. swapChain1->Release();
  180. dxgiFactory->Release();
  181. g_pSwapChain->SetMaximumFrameLatency(NUM_BACK_BUFFERS);
  182. g_hSwapChainWaitableObject = g_pSwapChain->GetFrameLatencyWaitableObject();
  183. }
  184. CreateRenderTarget();
  185. return S_OK;
  186. }
  187. void CleanupDeviceD3D()
  188. {
  189. CleanupRenderTarget();
  190. if (g_pSwapChain) { g_pSwapChain->Release(); g_pSwapChain = NULL; }
  191. if (g_hSwapChainWaitableObject != NULL) { CloseHandle(g_hSwapChainWaitableObject); }
  192. for (UINT i = 0; i < NUM_FRAMES_IN_FLIGHT; i++)
  193. if (g_frameContext[i].CommandAllocator) { g_frameContext[i].CommandAllocator->Release(); g_frameContext[i].CommandAllocator = NULL; }
  194. if (g_pd3dCommandQueue) { g_pd3dCommandQueue->Release(); g_pd3dCommandQueue = NULL; }
  195. if (g_pd3dCommandList) { g_pd3dCommandList->Release(); g_pd3dCommandList = NULL; }
  196. if (g_pd3dRtvDescHeap) { g_pd3dRtvDescHeap->Release(); g_pd3dRtvDescHeap = NULL; }
  197. if (g_pd3dSrvDescHeap) { g_pd3dSrvDescHeap->Release(); g_pd3dSrvDescHeap = NULL; }
  198. if (g_fence) { g_fence->Release(); g_fence = NULL; }
  199. if (g_fenceEvent) { CloseHandle(g_fenceEvent); g_fenceEvent = NULL; }
  200. if (g_pd3dDevice) { g_pd3dDevice->Release(); g_pd3dDevice = NULL; }
  201. }
  202. extern LRESULT ImGui_ImplWin32_WndProcHandler(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam);
  203. LRESULT WINAPI WndProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
  204. {
  205. if (ImGui_ImplWin32_WndProcHandler(hWnd, msg, wParam, lParam))
  206. return true;
  207. switch (msg)
  208. {
  209. case WM_SIZE:
  210. if (g_pd3dDevice != NULL && wParam != SIZE_MINIMIZED)
  211. {
  212. ImGui_ImplDX12_InvalidateDeviceObjects();
  213. CleanupRenderTarget();
  214. ResizeSwapChain(hWnd, (UINT)LOWORD(lParam), (UINT)HIWORD(lParam));
  215. CreateRenderTarget();
  216. ImGui_ImplDX12_CreateDeviceObjects();
  217. }
  218. return 0;
  219. case WM_SYSCOMMAND:
  220. if ((wParam & 0xfff0) == SC_KEYMENU) // Disable ALT application menu
  221. return 0;
  222. break;
  223. case WM_DESTROY:
  224. PostQuitMessage(0);
  225. return 0;
  226. }
  227. return DefWindowProc(hWnd, msg, wParam, lParam);
  228. }
  229. int main(int, char**)
  230. {
  231. // Create application window
  232. WNDCLASSEX wc = { sizeof(WNDCLASSEX), CS_CLASSDC, WndProc, 0L, 0L, GetModuleHandle(NULL), NULL, NULL, NULL, NULL, _T("ImGui Example"), NULL };
  233. RegisterClassEx(&wc);
  234. HWND hwnd = CreateWindow(_T("ImGui Example"), _T("Dear ImGui DirectX12 Example"), WS_OVERLAPPEDWINDOW, 100, 100, 1280, 800, NULL, NULL, wc.hInstance, NULL);
  235. // Initialize Direct3D
  236. if (CreateDeviceD3D(hwnd) < 0)
  237. {
  238. CleanupDeviceD3D();
  239. UnregisterClass(_T("ImGui Example"), wc.hInstance);
  240. return 1;
  241. }
  242. // Show the window
  243. ShowWindow(hwnd, SW_SHOWDEFAULT);
  244. UpdateWindow(hwnd);
  245. // Setup Dear ImGui context
  246. IMGUI_CHECKVERSION();
  247. ImGui::CreateContext();
  248. ImGuiIO& io = ImGui::GetIO(); (void)io;
  249. io.ConfigFlags |= ImGuiConfigFlags_NavEnableKeyboard; // Enable Keyboard Controls
  250. //io.ConfigFlags |= ImGuiConfigFlags_ViewportsEnable; // Enable Multi-Viewport / Platform Windows (FIXME: Currently broken in DX12 back-end, need some work!)
  251. //io.ConfigFlags |= ImGuiConfigFlags_ViewportsNoTaskBarIcons;
  252. //io.ConfigFlags |= ImGuiConfigFlags_ViewportsNoMerge;
  253. // Setup Dear ImGui style
  254. ImGui::StyleColorsDark();
  255. //ImGui::StyleColorsClassic();
  256. // When viewports are enabled we tweak WindowRounding/WindowBg so platform windows can look identical to regular ones.
  257. ImGuiStyle& style = ImGui::GetStyle();
  258. if (io.ConfigFlags & ImGuiConfigFlags_ViewportsEnable)
  259. {
  260. style.WindowRounding = 0.0f;
  261. style.Colors[ImGuiCol_WindowBg].w = 1.0f;
  262. }
  263. // Setup Platform/Renderer bindings
  264. ImGui_ImplWin32_Init(hwnd);
  265. ImGui_ImplDX12_Init(g_pd3dDevice, NUM_FRAMES_IN_FLIGHT,
  266. DXGI_FORMAT_R8G8B8A8_UNORM,
  267. g_pd3dSrvDescHeap->GetCPUDescriptorHandleForHeapStart(),
  268. g_pd3dSrvDescHeap->GetGPUDescriptorHandleForHeapStart());
  269. // Load Fonts
  270. // - If no fonts are loaded, dear imgui will use the default font. You can also load multiple fonts and use ImGui::PushFont()/PopFont() to select them.
  271. // - AddFontFromFileTTF() will return the ImFont* so you can store it if you need to select the font among multiple.
  272. // - If the file cannot be loaded, the function will return NULL. Please handle those errors in your application (e.g. use an assertion, or display an error and quit).
  273. // - The fonts will be rasterized at a given size (w/ oversampling) and stored into a texture when calling ImFontAtlas::Build()/GetTexDataAsXXXX(), which ImGui_ImplXXXX_NewFrame below will call.
  274. // - Read 'misc/fonts/README.txt' for more instructions and details.
  275. // - Remember that in C/C++ if you want to include a backslash \ in a string literal you need to write a double backslash \\ !
  276. //io.Fonts->AddFontDefault();
  277. //io.Fonts->AddFontFromFileTTF("../../misc/fonts/Roboto-Medium.ttf", 16.0f);
  278. //io.Fonts->AddFontFromFileTTF("../../misc/fonts/Cousine-Regular.ttf", 15.0f);
  279. //io.Fonts->AddFontFromFileTTF("../../misc/fonts/DroidSans.ttf", 16.0f);
  280. //io.Fonts->AddFontFromFileTTF("../../misc/fonts/ProggyTiny.ttf", 10.0f);
  281. //ImFont* font = io.Fonts->AddFontFromFileTTF("c:\\Windows\\Fonts\\ArialUni.ttf", 18.0f, NULL, io.Fonts->GetGlyphRangesJapanese());
  282. //IM_ASSERT(font != NULL);
  283. bool show_demo_window = true;
  284. bool show_another_window = false;
  285. ImVec4 clear_color = ImVec4(0.45f, 0.55f, 0.60f, 1.00f);
  286. // Main loop
  287. MSG msg;
  288. ZeroMemory(&msg, sizeof(msg));
  289. while (msg.message != WM_QUIT)
  290. {
  291. // Poll and handle messages (inputs, window resize, etc.)
  292. // You can read the io.WantCaptureMouse, io.WantCaptureKeyboard flags to tell if dear imgui wants to use your inputs.
  293. // - When io.WantCaptureMouse is true, do not dispatch mouse input data to your main application.
  294. // - When io.WantCaptureKeyboard is true, do not dispatch keyboard input data to your main application.
  295. // Generally you may always pass all inputs to dear imgui, and hide them from your application based on those two flags.
  296. if (PeekMessage(&msg, NULL, 0U, 0U, PM_REMOVE))
  297. {
  298. TranslateMessage(&msg);
  299. DispatchMessage(&msg);
  300. continue;
  301. }
  302. // Start the Dear ImGui frame
  303. ImGui_ImplDX12_NewFrame();
  304. ImGui_ImplWin32_NewFrame();
  305. ImGui::NewFrame();
  306. // 1. Show the big demo window (Most of the sample code is in ImGui::ShowDemoWindow()! You can browse its code to learn more about Dear ImGui!).
  307. if (show_demo_window)
  308. ImGui::ShowDemoWindow(&show_demo_window);
  309. // 2. Show a simple window that we create ourselves. We use a Begin/End pair to created a named window.
  310. {
  311. static float f = 0.0f;
  312. static int counter = 0;
  313. ImGui::Begin("Hello, world!"); // Create a window called "Hello, world!" and append into it.
  314. ImGui::Text("This is some useful text."); // Display some text (you can use a format strings too)
  315. ImGui::Checkbox("Demo Window", &show_demo_window); // Edit bools storing our window open/close state
  316. ImGui::Checkbox("Another Window", &show_another_window);
  317. ImGui::SliderFloat("float", &f, 0.0f, 1.0f); // Edit 1 float using a slider from 0.0f to 1.0f
  318. ImGui::ColorEdit3("clear color", (float*)&clear_color); // Edit 3 floats representing a color
  319. if (ImGui::Button("Button")) // Buttons return true when clicked (most widgets return true when edited/activated)
  320. counter++;
  321. ImGui::SameLine();
  322. ImGui::Text("counter = %d", counter);
  323. ImGui::Text("Application average %.3f ms/frame (%.1f FPS)", 1000.0f / ImGui::GetIO().Framerate, ImGui::GetIO().Framerate);
  324. ImGui::End();
  325. }
  326. // 3. Show another simple window.
  327. if (show_another_window)
  328. {
  329. ImGui::Begin("Another Window", &show_another_window); // Pass a pointer to our bool variable (the window will have a closing button that will clear the bool when clicked)
  330. ImGui::Text("Hello from another window!");
  331. if (ImGui::Button("Close Me"))
  332. show_another_window = false;
  333. ImGui::End();
  334. }
  335. // Rendering
  336. FrameContext* frameCtxt = WaitForNextFrameResources();
  337. UINT backBufferIdx = g_pSwapChain->GetCurrentBackBufferIndex();
  338. frameCtxt->CommandAllocator->Reset();
  339. D3D12_RESOURCE_BARRIER barrier = {};
  340. barrier.Type = D3D12_RESOURCE_BARRIER_TYPE_TRANSITION;
  341. barrier.Flags = D3D12_RESOURCE_BARRIER_FLAG_NONE;
  342. barrier.Transition.pResource = g_mainRenderTargetResource[backBufferIdx];
  343. barrier.Transition.Subresource = D3D12_RESOURCE_BARRIER_ALL_SUBRESOURCES;
  344. barrier.Transition.StateBefore = D3D12_RESOURCE_STATE_PRESENT;
  345. barrier.Transition.StateAfter = D3D12_RESOURCE_STATE_RENDER_TARGET;
  346. g_pd3dCommandList->Reset(frameCtxt->CommandAllocator, NULL);
  347. g_pd3dCommandList->ResourceBarrier(1, &barrier);
  348. g_pd3dCommandList->ClearRenderTargetView(g_mainRenderTargetDescriptor[backBufferIdx], (float*)&clear_color, 0, NULL);
  349. g_pd3dCommandList->OMSetRenderTargets(1, &g_mainRenderTargetDescriptor[backBufferIdx], FALSE, NULL);
  350. g_pd3dCommandList->SetDescriptorHeaps(1, &g_pd3dSrvDescHeap);
  351. ImGui::Render();
  352. ImGui_ImplDX12_RenderDrawData(ImGui::GetDrawData(), g_pd3dCommandList);
  353. barrier.Transition.StateBefore = D3D12_RESOURCE_STATE_RENDER_TARGET;
  354. barrier.Transition.StateAfter = D3D12_RESOURCE_STATE_PRESENT;
  355. g_pd3dCommandList->ResourceBarrier(1, &barrier);
  356. g_pd3dCommandList->Close();
  357. g_pd3dCommandQueue->ExecuteCommandLists(1, (ID3D12CommandList* const*)&g_pd3dCommandList);
  358. // Update and Render additional Platform Windows
  359. if (io.ConfigFlags & ImGuiConfigFlags_ViewportsEnable)
  360. {
  361. ImGui::UpdatePlatformWindows();
  362. ImGui::RenderPlatformWindowsDefault(NULL, (void*)g_pd3dCommandList);
  363. }
  364. g_pSwapChain->Present(1, 0); // Present with vsync
  365. //g_pSwapChain->Present(0, 0); // Present without vsync
  366. UINT64 fenceValue = g_fenceLastSignaledValue + 1;
  367. g_pd3dCommandQueue->Signal(g_fence, fenceValue);
  368. g_fenceLastSignaledValue = fenceValue;
  369. frameCtxt->FenceValue = fenceValue;
  370. }
  371. WaitForLastSubmittedFrame();
  372. ImGui_ImplDX12_Shutdown();
  373. ImGui_ImplWin32_Shutdown();
  374. ImGui::DestroyContext();
  375. CleanupDeviceD3D();
  376. DestroyWindow(hwnd);
  377. UnregisterClass(_T("ImGui Example"), wc.hInstance);
  378. return 0;
  379. }