main.cpp 19 KB

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