main.c 16 KB

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