main.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445
  1. /* nuklear - 1.32.0 - public domain */
  2. #define COBJMACROS
  3. #define WIN32_LEAN_AND_MEAN
  4. #include <windows.h>
  5. #include <combaseapi.h>
  6. #include <dxgi1_6.h>
  7. #include <d3d12.h>
  8. #include <stdio.h>
  9. #include <string.h>
  10. #include <limits.h>
  11. #include <time.h>
  12. #define WINDOW_WIDTH 800
  13. #define WINDOW_HEIGHT 600
  14. #define USER_TEXTURES 6
  15. #define MAX_VERTEX_BUFFER 512 * 1024
  16. #define MAX_INDEX_BUFFER 128 * 1024
  17. #define NK_INCLUDE_FIXED_TYPES
  18. #define NK_INCLUDE_STANDARD_IO
  19. #define NK_INCLUDE_STANDARD_VARARGS
  20. #define NK_INCLUDE_DEFAULT_ALLOCATOR
  21. #define NK_INCLUDE_VERTEX_BUFFER_OUTPUT
  22. #define NK_INCLUDE_FONT_BAKING
  23. #define NK_INCLUDE_DEFAULT_FONT
  24. #define NK_IMPLEMENTATION
  25. #define NK_D3D12_IMPLEMENTATION
  26. #include "../../nuklear.h"
  27. #include "nuklear_d3d12.h"
  28. /* ===============================================================
  29. *
  30. * EXAMPLE
  31. *
  32. * ===============================================================*/
  33. /* This are some code examples to provide a small overview of what can be
  34. * done with this library. To try out an example uncomment the defines */
  35. /*#define INCLUDE_ALL */
  36. /*#define INCLUDE_STYLE */
  37. /*#define INCLUDE_CALCULATOR */
  38. /*#define INCLUDE_CANVAS */
  39. /*#define INCLUDE_OVERVIEW */
  40. /*#define INCLUDE_NODE_EDITOR */
  41. #ifdef INCLUDE_ALL
  42. #define INCLUDE_STYLE
  43. #define INCLUDE_CALCULATOR
  44. #define INCLUDE_CANVAS
  45. #define INCLUDE_OVERVIEW
  46. #define INCLUDE_NODE_EDITOR
  47. #endif
  48. #ifdef INCLUDE_STYLE
  49. #include "../../demo/common/style.c"
  50. #endif
  51. #ifdef INCLUDE_CALCULATOR
  52. #include "../../demo/common/calculator.c"
  53. #endif
  54. #ifdef INCLUDE_CANVAS
  55. #include "../../demo/common/canvas.c"
  56. #endif
  57. #ifdef INCLUDE_OVERVIEW
  58. #include "../../demo/common/overview.c"
  59. #endif
  60. #ifdef INCLUDE_NODE_EDITOR
  61. #include "../../demo/common/node_editor.c"
  62. #endif
  63. /* ===============================================================
  64. *
  65. * DEMO
  66. *
  67. * ===============================================================*/
  68. /* DXGI & Window related device objects */
  69. static IDXGIFactory2 *dxgi_factory;
  70. static IDXGISwapChain1 *swap_chain;
  71. static ID3D12DescriptorHeap *rtv_descriptor_heap;
  72. static D3D12_CPU_DESCRIPTOR_HANDLE rtv_handles[2];
  73. static ID3D12Resource *rtv_buffers[2];
  74. static UINT rtv_desc_increment;
  75. static UINT rtv_index;
  76. /* DirectX common device objects */
  77. static ID3D12Device *device;
  78. static ID3D12CommandQueue *command_queue;
  79. static ID3D12Fence *queue_fence;
  80. static UINT64 fence_value;
  81. static ID3D12CommandAllocator *command_allocator;
  82. static ID3D12GraphicsCommandList *command_list;
  83. static void signal_and_wait()
  84. {
  85. HRESULT hr;
  86. /* Signal fence when execution finishes */
  87. hr = ID3D12CommandQueue_Signal(command_queue, queue_fence, ++fence_value);
  88. assert(SUCCEEDED(hr));
  89. /* Wait for queue to finish */
  90. while(ID3D12Fence_GetCompletedValue(queue_fence) != fence_value)
  91. {
  92. SwitchToThread(); /* Allow windows to do other work */
  93. }
  94. }
  95. static void execute_commands()
  96. {
  97. /* Prepare command list for execution */
  98. ID3D12GraphicsCommandList_Close(command_list);
  99. /* Execute on command queue */
  100. ID3D12CommandList* cmd_lists[] = { (ID3D12CommandList*)command_list};
  101. ID3D12CommandQueue_ExecuteCommandLists(command_queue, 1, cmd_lists);
  102. /* Wait for execution */
  103. signal_and_wait();
  104. /* Reset command allocator and list */
  105. ID3D12CommandAllocator_Reset(command_allocator);
  106. ID3D12GraphicsCommandList_Reset(command_list, command_allocator, NULL);
  107. }
  108. static void get_swap_chain_buffers()
  109. {
  110. HRESULT hr;
  111. D3D12_CPU_DESCRIPTOR_HANDLE descriptor_handle;
  112. /* Get resource objects from swap chain */
  113. hr = IDXGISwapChain1_GetBuffer(swap_chain, 0, &IID_ID3D12Resource, &rtv_buffers[0]);
  114. assert(SUCCEEDED(hr));
  115. hr = IDXGISwapChain1_GetBuffer(swap_chain, 1, &IID_ID3D12Resource, &rtv_buffers[1]);
  116. assert(SUCCEEDED(hr));
  117. /* Recreate render target views */
  118. ID3D12DescriptorHeap_GetCPUDescriptorHandleForHeapStart(rtv_descriptor_heap, &descriptor_handle);
  119. ID3D12Device_CreateRenderTargetView(device, rtv_buffers[0], NULL, descriptor_handle);
  120. rtv_handles[0] = descriptor_handle;
  121. descriptor_handle.ptr += rtv_desc_increment;
  122. ID3D12Device_CreateRenderTargetView(device, rtv_buffers[1], NULL, descriptor_handle);
  123. rtv_handles[1] = descriptor_handle;
  124. }
  125. static void
  126. set_swap_chain_size(int width, int height)
  127. {
  128. HRESULT hr;
  129. /* Wait for pending work */
  130. signal_and_wait();
  131. signal_and_wait(); /* Two times because we have two buffers in flight */
  132. /* Release all open refereces to the buffers */
  133. ID3D12Resource_Release(rtv_buffers[0]);
  134. ID3D12Resource_Release(rtv_buffers[1]);
  135. /* DXGI can now perform resizing */
  136. hr = IDXGISwapChain1_ResizeBuffers(swap_chain, 2, width, height, DXGI_FORMAT_UNKNOWN, DXGI_SWAP_CHAIN_FLAG_ALLOW_MODE_SWITCH);
  137. assert(SUCCEEDED(hr));
  138. /* Get references for the new resized buffers */
  139. get_swap_chain_buffers();
  140. /* Reset RTV index */
  141. rtv_index = 0;
  142. }
  143. static LRESULT CALLBACK
  144. WindowProc(HWND wnd, UINT msg, WPARAM wparam, LPARAM lparam)
  145. {
  146. switch (msg)
  147. {
  148. case WM_DESTROY:
  149. PostQuitMessage(0);
  150. return 0;
  151. case WM_SIZE:
  152. if (swap_chain)
  153. {
  154. int width = LOWORD(lparam);
  155. int height = HIWORD(lparam);
  156. set_swap_chain_size(width, height);
  157. nk_d3d12_resize(width, height);
  158. }
  159. break;
  160. }
  161. if (nk_d3d12_handle_event(wnd, msg, wparam, lparam))
  162. return 0;
  163. return DefWindowProcW(wnd, msg, wparam, lparam);
  164. }
  165. int main(void)
  166. {
  167. struct nk_context *ctx;
  168. struct nk_colorf bg;
  169. WNDCLASSW wc;
  170. RECT rect = { 0, 0, WINDOW_WIDTH, WINDOW_HEIGHT };
  171. DWORD style = WS_OVERLAPPEDWINDOW;
  172. DWORD exstyle = WS_EX_APPWINDOW;
  173. HWND wnd;
  174. int running = 1;
  175. HRESULT hr;
  176. D3D12_COMMAND_QUEUE_DESC command_queue_desc;
  177. DXGI_SWAP_CHAIN_DESC1 swap_chain_desc;
  178. D3D12_DESCRIPTOR_HEAP_DESC rtv_desc_heap_desc;
  179. /* Win32 */
  180. memset(&wc, 0, sizeof(wc));
  181. wc.style = CS_DBLCLKS;
  182. wc.lpfnWndProc = WindowProc;
  183. wc.hInstance = GetModuleHandleW(0);
  184. wc.hIcon = LoadIcon(NULL, IDI_APPLICATION);
  185. wc.hCursor = LoadCursor(NULL, IDC_ARROW);
  186. wc.lpszClassName = L"NuklearWindowClass";
  187. RegisterClassW(&wc);
  188. AdjustWindowRectEx(&rect, style, FALSE, exstyle);
  189. wnd = CreateWindowExW(exstyle, wc.lpszClassName, L"Nuklear Direct3D 12 Demo",
  190. style | WS_VISIBLE, CW_USEDEFAULT, CW_USEDEFAULT,
  191. rect.right - rect.left, rect.bottom - rect.top,
  192. NULL, NULL, wc.hInstance, NULL);
  193. /* D3D12 setup */
  194. /* Create default Device */
  195. hr = D3D12CreateDevice(NULL, D3D_FEATURE_LEVEL_11_0, &IID_ID3D12Device, &device);
  196. assert(SUCCEEDED(hr));
  197. /* Create a command queue */
  198. command_queue_desc.Type = D3D12_COMMAND_LIST_TYPE_DIRECT;
  199. command_queue_desc.Priority = D3D12_COMMAND_QUEUE_PRIORITY_NORMAL;
  200. command_queue_desc.Flags = D3D12_COMMAND_QUEUE_FLAG_NONE;
  201. command_queue_desc.NodeMask = 0;
  202. hr = ID3D12Device_CreateCommandQueue(device, &command_queue_desc, &IID_ID3D12CommandQueue, &command_queue);
  203. assert(SUCCEEDED(hr));
  204. /* Create a fence for command queue executions */
  205. fence_value = 0;
  206. hr = ID3D12Device_CreateFence(device, fence_value, D3D12_FENCE_FLAG_NONE, &IID_ID3D12Fence, &queue_fence);
  207. assert(SUCCEEDED(hr));
  208. /* Create a command allocator */
  209. hr = ID3D12Device_CreateCommandAllocator(device, D3D12_COMMAND_LIST_TYPE_DIRECT, &IID_ID3D12CommandAllocator, &command_allocator);
  210. assert(SUCCEEDED(hr));
  211. /* Create a command list that will use our allocator */
  212. hr = ID3D12Device_CreateCommandList(device, 0, D3D12_COMMAND_LIST_TYPE_DIRECT, command_allocator, NULL, &IID_ID3D12GraphicsCommandList1, &command_list);
  213. assert(SUCCEEDED(hr));
  214. /* DXGI Setup (Swap chain & resources) */
  215. /* Create a descriptor heap for the back buffers */
  216. rtv_desc_heap_desc.Type = D3D12_DESCRIPTOR_HEAP_TYPE_RTV;
  217. rtv_desc_heap_desc.NumDescriptors = 2;
  218. rtv_desc_heap_desc.Flags = D3D12_DESCRIPTOR_HEAP_FLAG_NONE;
  219. rtv_desc_heap_desc.NodeMask = 0;
  220. hr = ID3D12Device_CreateDescriptorHeap(device, &rtv_desc_heap_desc, &IID_ID3D12DescriptorHeap, &rtv_descriptor_heap);
  221. assert(SUCCEEDED(hr));
  222. /* Get descriptor increment */
  223. rtv_desc_increment = ID3D12Device_GetDescriptorHandleIncrementSize(device, D3D12_DESCRIPTOR_HEAP_TYPE_RTV);
  224. /* Get the DXGI factory */
  225. hr = CreateDXGIFactory1(&IID_IDXGIFactory2, &dxgi_factory);
  226. assert(SUCCEEDED(hr));
  227. /* Create the swap chain */
  228. swap_chain_desc.Width = WINDOW_WIDTH;
  229. swap_chain_desc.Height = WINDOW_HEIGHT;
  230. swap_chain_desc.Format = DXGI_FORMAT_R8G8B8A8_UNORM;
  231. swap_chain_desc.Stereo = 0;
  232. swap_chain_desc.SampleDesc.Count = 1;
  233. swap_chain_desc.SampleDesc.Quality = 0;
  234. swap_chain_desc.BufferUsage = DXGI_USAGE_RENDER_TARGET_OUTPUT;
  235. swap_chain_desc.BufferCount = 2;
  236. swap_chain_desc.Scaling = DXGI_SCALING_STRETCH;
  237. swap_chain_desc.SwapEffect = DXGI_SWAP_EFFECT_FLIP_SEQUENTIAL ;
  238. swap_chain_desc.AlphaMode = DXGI_ALPHA_MODE_IGNORE;
  239. swap_chain_desc.Flags = DXGI_SWAP_CHAIN_FLAG_ALLOW_MODE_SWITCH;
  240. hr = IDXGIFactory2_CreateSwapChainForHwnd(dxgi_factory, (IUnknown*)command_queue, wnd, &swap_chain_desc, NULL, NULL, &swap_chain);
  241. assert(SUCCEEDED(hr));
  242. get_swap_chain_buffers();
  243. /* GUI */
  244. ctx = nk_d3d12_init(device, WINDOW_WIDTH, WINDOW_HEIGHT, MAX_VERTEX_BUFFER, MAX_INDEX_BUFFER, USER_TEXTURES);
  245. /* Load Fonts: if none of these are loaded a default font will be used */
  246. /* Load Cursor: if you uncomment cursor loading please hide the cursor */
  247. {
  248. struct nk_font_atlas *atlas;
  249. nk_d3d12_font_stash_begin(&atlas);
  250. /*struct nk_font *droid = nk_font_atlas_add_from_file(atlas, "../../extra_font/DroidSans.ttf", 14, 0);*/
  251. /*struct nk_font *robot = nk_font_atlas_add_from_file(atlas, "../../extra_font/Roboto-Regular.ttf", 14, 0);*/
  252. /*struct nk_font *future = nk_font_atlas_add_from_file(atlas, "../../extra_font/kenvector_future_thin.ttf", 13, 0);*/
  253. /*struct nk_font *clean = nk_font_atlas_add_from_file(atlas, "../../extra_font/ProggyClean.ttf", 12, 0);*/
  254. /*struct nk_font *tiny = nk_font_atlas_add_from_file(atlas, "../../extra_font/ProggyTiny.ttf", 10, 0);*/
  255. /*struct nk_font *cousine = nk_font_atlas_add_from_file(atlas, "../../extra_font/Cousine-Regular.ttf", 13, 0);*/
  256. nk_d3d12_font_stash_end(command_list);
  257. /*nk_style_load_all_cursors(ctx, atlas->cursors);*/
  258. /*nk_style_set_font(ctx, &droid->handle)*/;
  259. }
  260. /* Execute the command list to make sure all texture (font) data has been uploaded */
  261. execute_commands();
  262. /* Now we can cleanup all resources consumed by font stashing that are no longer used */
  263. nk_d3d12_font_stash_cleanup();
  264. /* style.c */
  265. #ifdef INCLUDE_STYLE
  266. /* ease regression testing during Nuklear release process; not needed for anything else */
  267. #ifdef STYLE_WHITE
  268. set_style(ctx, THEME_WHITE);
  269. #elif defined(STYLE_RED)
  270. set_style(ctx, THEME_RED);
  271. #elif defined(STYLE_BLUE)
  272. set_style(ctx, THEME_BLUE);
  273. #elif defined(STYLE_DARK)
  274. set_style(ctx, THEME_DARK);
  275. #endif
  276. #endif
  277. bg.r = 0.10f, bg.g = 0.18f, bg.b = 0.24f, bg.a = 1.0f;
  278. while (running)
  279. {
  280. /* Input */
  281. MSG msg;
  282. nk_input_begin(ctx);
  283. while (PeekMessageW(&msg, NULL, 0, 0, PM_REMOVE))
  284. {
  285. if (msg.message == WM_QUIT)
  286. running = 0;
  287. TranslateMessage(&msg);
  288. DispatchMessageW(&msg);
  289. }
  290. nk_input_end(ctx);
  291. /* GUI */
  292. if (nk_begin(ctx, "Demo", nk_rect(50, 50, 230, 250),
  293. NK_WINDOW_BORDER|NK_WINDOW_MOVABLE|NK_WINDOW_SCALABLE|
  294. NK_WINDOW_MINIMIZABLE|NK_WINDOW_TITLE))
  295. {
  296. enum {EASY, HARD};
  297. static int op = EASY;
  298. static int property = 20;
  299. nk_layout_row_static(ctx, 30, 80, 1);
  300. if (nk_button_label(ctx, "button"))
  301. fprintf(stdout, "button pressed\n");
  302. nk_layout_row_dynamic(ctx, 30, 2);
  303. if (nk_option_label(ctx, "easy", op == EASY)) op = EASY;
  304. if (nk_option_label(ctx, "hard", op == HARD)) op = HARD;
  305. nk_layout_row_dynamic(ctx, 22, 1);
  306. nk_property_int(ctx, "Compression:", 0, &property, 100, 10, 1);
  307. nk_layout_row_dynamic(ctx, 20, 1);
  308. nk_label(ctx, "background:", NK_TEXT_LEFT);
  309. nk_layout_row_dynamic(ctx, 25, 1);
  310. if (nk_combo_begin_color(ctx, nk_rgb_cf(bg), nk_vec2(nk_widget_width(ctx),400))) {
  311. nk_layout_row_dynamic(ctx, 120, 1);
  312. bg = nk_color_picker(ctx, bg, NK_RGBA);
  313. nk_layout_row_dynamic(ctx, 25, 1);
  314. bg.r = nk_propertyf(ctx, "#R:", 0, bg.r, 1.0f, 0.01f,0.005f);
  315. bg.g = nk_propertyf(ctx, "#G:", 0, bg.g, 1.0f, 0.01f,0.005f);
  316. bg.b = nk_propertyf(ctx, "#B:", 0, bg.b, 1.0f, 0.01f,0.005f);
  317. bg.a = nk_propertyf(ctx, "#A:", 0, bg.a, 1.0f, 0.01f,0.005f);
  318. nk_combo_end(ctx);
  319. }
  320. }
  321. nk_end(ctx);
  322. /* -------------- EXAMPLES ---------------- */
  323. #ifdef INCLUDE_CALCULATOR
  324. calculator(ctx);
  325. #endif
  326. #ifdef INCLUDE_CANVAS
  327. canvas(ctx);
  328. #endif
  329. #ifdef INCLUDE_OVERVIEW
  330. overview(ctx);
  331. #endif
  332. #ifdef INCLUDE_NODE_EDITOR
  333. node_editor(ctx);
  334. #endif
  335. /* ----------------------------------------- */
  336. /* Set rtv resource state */
  337. D3D12_RESOURCE_BARRIER resource_barrier;
  338. resource_barrier.Type = D3D12_RESOURCE_BARRIER_TYPE_TRANSITION;
  339. resource_barrier.Transition.pResource = rtv_buffers[rtv_index];
  340. resource_barrier.Transition.Subresource = 0;
  341. resource_barrier.Transition.StateBefore = D3D12_RESOURCE_STATE_PRESENT;
  342. resource_barrier.Transition.StateAfter = D3D12_RESOURCE_STATE_RENDER_TARGET;
  343. resource_barrier.Flags = D3D12_RESOURCE_BARRIER_FLAG_NONE;
  344. ID3D12GraphicsCommandList_ResourceBarrier(command_list, 1, &resource_barrier);
  345. /* Clear and set the rtv */
  346. ID3D12GraphicsCommandList_ClearRenderTargetView(command_list, rtv_handles[rtv_index], &bg.r, 0, NULL);
  347. ID3D12GraphicsCommandList_OMSetRenderTargets(command_list, 1, &rtv_handles[rtv_index], FALSE, NULL);
  348. /* Draw */
  349. nk_d3d12_render(command_list, NK_ANTI_ALIASING_ON);
  350. /* Bring the rtv resource back to present state */
  351. resource_barrier.Type = D3D12_RESOURCE_BARRIER_TYPE_TRANSITION;
  352. resource_barrier.Transition.pResource = rtv_buffers[rtv_index];
  353. resource_barrier.Transition.Subresource = 0;
  354. resource_barrier.Transition.StateBefore = D3D12_RESOURCE_STATE_RENDER_TARGET;
  355. resource_barrier.Transition.StateAfter = D3D12_RESOURCE_STATE_PRESENT;
  356. resource_barrier.Flags = D3D12_RESOURCE_BARRIER_FLAG_NONE;
  357. ID3D12GraphicsCommandList_ResourceBarrier(command_list, 1, &resource_barrier);
  358. /* Execute command list and wait */
  359. execute_commands();
  360. /* Present frame */
  361. hr = IDXGISwapChain2_Present(swap_chain, 1, 0);
  362. rtv_index = (rtv_index + 1) % 2;
  363. if (hr == DXGI_ERROR_DEVICE_RESET || hr == DXGI_ERROR_DEVICE_REMOVED) {
  364. /* to recover from this, you'll need to recreate device and all the resources */
  365. MessageBoxW(NULL, L"D3D12 device is lost or removed!", L"Error", 0);
  366. break;
  367. } else if (hr == DXGI_STATUS_OCCLUDED) {
  368. /* window is not visible, so vsync won't work. Let's sleep a bit to reduce CPU usage */
  369. Sleep(10);
  370. }
  371. assert(SUCCEEDED(hr));
  372. }
  373. /* Nuklear shutdown */
  374. nk_d3d12_shutdown();
  375. /* D3D12 and DXGI shutdown */
  376. signal_and_wait();
  377. signal_and_wait(); /* Two times because we have two buffers in flight */
  378. ID3D12Resource_Release(rtv_buffers[0]);
  379. ID3D12Resource_Release(rtv_buffers[1]);
  380. ID3D12DescriptorHeap_Release(rtv_descriptor_heap);
  381. IDXGISwapChain1_Release(swap_chain);
  382. IDXGIFactory2_Release(dxgi_factory);
  383. ID3D12GraphicsCommandList_Release(command_list);
  384. ID3D12CommandAllocator_Release(command_allocator);
  385. ID3D12CommandQueue_Release(command_queue);
  386. ID3D12Fence_Release(queue_fence);
  387. ID3D12Device_Release(device);
  388. /* win32 shutdown */
  389. UnregisterClassW(wc.lpszClassName, wc.hInstance);
  390. return 0;
  391. }