main.cpp 18 KB

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