2
0

main.cpp 19 KB

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