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