nuklear_d3d11.h 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632
  1. /*
  2. * Nuklear - 1.32.0 - public domain
  3. * no warrenty implied; use at your own risk.
  4. * authored from 2015-2016 by Micha Mettke
  5. */
  6. /*
  7. * ==============================================================
  8. *
  9. * API
  10. *
  11. * ===============================================================
  12. */
  13. #ifndef NK_D3D11_H_
  14. #define NK_D3D11_H_
  15. #define WIN32_LEAN_AND_MEAN
  16. #include <windows.h>
  17. typedef struct ID3D11Device ID3D11Device;
  18. typedef struct ID3D11DeviceContext ID3D11DeviceContext;
  19. NK_API struct nk_context *nk_d3d11_init(ID3D11Device *device, int width, int height, unsigned int max_vertex_buffer, unsigned int max_index_buffer);
  20. NK_API void nk_d3d11_font_stash_begin(struct nk_font_atlas **atlas);
  21. NK_API void nk_d3d11_font_stash_end(void);
  22. NK_API int nk_d3d11_handle_event(HWND hwnd, UINT msg, WPARAM wparam, LPARAM lparam);
  23. NK_API void nk_d3d11_render(ID3D11DeviceContext *context, enum nk_anti_aliasing);
  24. NK_API void nk_d3d11_resize(ID3D11DeviceContext *context, int width, int height);
  25. NK_API void nk_d3d11_shutdown(void);
  26. #endif
  27. /*
  28. * ==============================================================
  29. *
  30. * IMPLEMENTATION
  31. *
  32. * ===============================================================
  33. */
  34. #ifdef NK_D3D11_IMPLEMENTATION
  35. #define WIN32_LEAN_AND_MEAN
  36. #define COBJMACROS
  37. #include <d3d11.h>
  38. #include <stddef.h>
  39. #include <string.h>
  40. #include <float.h>
  41. #include <assert.h>
  42. #include "nuklear_d3d11_vertex_shader.h"
  43. #include "nuklear_d3d11_pixel_shader.h"
  44. struct nk_d3d11_vertex {
  45. float position[2];
  46. float uv[2];
  47. nk_byte col[4];
  48. };
  49. static struct
  50. {
  51. struct nk_context ctx;
  52. struct nk_font_atlas atlas;
  53. struct nk_buffer cmds;
  54. struct nk_draw_null_texture tex_null;
  55. unsigned int max_vertex_buffer;
  56. unsigned int max_index_buffer;
  57. D3D11_VIEWPORT viewport;
  58. ID3D11Device *device;
  59. ID3D11RasterizerState *rasterizer_state;
  60. ID3D11VertexShader *vertex_shader;
  61. ID3D11InputLayout *input_layout;
  62. ID3D11Buffer *const_buffer;
  63. ID3D11PixelShader *pixel_shader;
  64. ID3D11BlendState *blend_state;
  65. ID3D11Buffer *index_buffer;
  66. ID3D11Buffer *vertex_buffer;
  67. ID3D11ShaderResourceView *font_texture_view;
  68. ID3D11SamplerState *sampler_state;
  69. } d3d11;
  70. NK_API void
  71. nk_d3d11_render(ID3D11DeviceContext *context, enum nk_anti_aliasing AA)
  72. {
  73. const float blend_factor[4] = { 0.0f, 0.0f, 0.0f, 0.0f };
  74. const UINT stride = sizeof(struct nk_d3d11_vertex);
  75. const UINT offset = 0;
  76. #ifdef NK_UINT_DRAW_INDEX
  77. DXGI_FORMAT index_buffer_format = DXGI_FORMAT_R32_UINT;
  78. #else
  79. DXGI_FORMAT index_buffer_format = DXGI_FORMAT_R16_UINT;
  80. #endif
  81. ID3D11DeviceContext_IASetInputLayout(context, d3d11.input_layout);
  82. ID3D11DeviceContext_IASetVertexBuffers(context, 0, 1, &d3d11.vertex_buffer, &stride, &offset);
  83. ID3D11DeviceContext_IASetIndexBuffer(context, d3d11.index_buffer, index_buffer_format, 0);
  84. ID3D11DeviceContext_IASetPrimitiveTopology(context, D3D11_PRIMITIVE_TOPOLOGY_TRIANGLELIST);
  85. ID3D11DeviceContext_VSSetShader(context, d3d11.vertex_shader, NULL, 0);
  86. ID3D11DeviceContext_VSSetConstantBuffers(context, 0, 1, &d3d11.const_buffer);
  87. ID3D11DeviceContext_PSSetShader(context, d3d11.pixel_shader, NULL, 0);
  88. ID3D11DeviceContext_PSSetSamplers(context, 0, 1, &d3d11.sampler_state);
  89. ID3D11DeviceContext_OMSetBlendState(context, d3d11.blend_state, blend_factor, 0xffffffff);
  90. ID3D11DeviceContext_RSSetState(context, d3d11.rasterizer_state);
  91. ID3D11DeviceContext_RSSetViewports(context, 1, &d3d11.viewport);
  92. /* Convert from command queue into draw list and draw to screen */
  93. {/* load draw vertices & elements directly into vertex + element buffer */
  94. D3D11_MAPPED_SUBRESOURCE vertices;
  95. D3D11_MAPPED_SUBRESOURCE indices;
  96. const struct nk_draw_command *cmd;
  97. UINT offset = 0;
  98. HRESULT hr;
  99. hr = ID3D11DeviceContext_Map(context, (ID3D11Resource *)d3d11.vertex_buffer, 0, D3D11_MAP_WRITE_DISCARD, 0, &vertices);
  100. NK_ASSERT(SUCCEEDED(hr));
  101. hr = ID3D11DeviceContext_Map(context, (ID3D11Resource *)d3d11.index_buffer, 0, D3D11_MAP_WRITE_DISCARD, 0, &indices);
  102. NK_ASSERT(SUCCEEDED(hr));
  103. {/* fill converting configuration */
  104. struct nk_convert_config config;
  105. NK_STORAGE const struct nk_draw_vertex_layout_element vertex_layout[] = {
  106. {NK_VERTEX_POSITION, NK_FORMAT_FLOAT, NK_OFFSETOF(struct nk_d3d11_vertex, position)},
  107. {NK_VERTEX_TEXCOORD, NK_FORMAT_FLOAT, NK_OFFSETOF(struct nk_d3d11_vertex, uv)},
  108. {NK_VERTEX_COLOR, NK_FORMAT_R8G8B8A8, NK_OFFSETOF(struct nk_d3d11_vertex, col)},
  109. {NK_VERTEX_LAYOUT_END}
  110. };
  111. memset(&config, 0, sizeof(config));
  112. config.vertex_layout = vertex_layout;
  113. config.vertex_size = sizeof(struct nk_d3d11_vertex);
  114. config.vertex_alignment = NK_ALIGNOF(struct nk_d3d11_vertex);
  115. config.global_alpha = 1.0f;
  116. config.shape_AA = AA;
  117. config.line_AA = AA;
  118. config.circle_segment_count = 22;
  119. config.curve_segment_count = 22;
  120. config.arc_segment_count = 22;
  121. config.tex_null = d3d11.tex_null;
  122. {/* setup buffers to load vertices and elements */
  123. struct nk_buffer vbuf, ibuf;
  124. nk_buffer_init_fixed(&vbuf, vertices.pData, (size_t)d3d11.max_vertex_buffer);
  125. nk_buffer_init_fixed(&ibuf, indices.pData, (size_t)d3d11.max_index_buffer);
  126. nk_convert(&d3d11.ctx, &d3d11.cmds, &vbuf, &ibuf, &config);}
  127. }
  128. ID3D11DeviceContext_Unmap(context, (ID3D11Resource *)d3d11.vertex_buffer, 0);
  129. ID3D11DeviceContext_Unmap(context, (ID3D11Resource *)d3d11.index_buffer, 0);
  130. /* iterate over and execute each draw command */
  131. nk_draw_foreach(cmd, &d3d11.ctx, &d3d11.cmds)
  132. {
  133. D3D11_RECT scissor;
  134. ID3D11ShaderResourceView *texture_view = (ID3D11ShaderResourceView *)cmd->texture.ptr;
  135. if (!cmd->elem_count) continue;
  136. scissor.left = (LONG)cmd->clip_rect.x;
  137. scissor.right = (LONG)(cmd->clip_rect.x + cmd->clip_rect.w);
  138. scissor.top = (LONG)cmd->clip_rect.y;
  139. scissor.bottom = (LONG)(cmd->clip_rect.y + cmd->clip_rect.h);
  140. ID3D11DeviceContext_PSSetShaderResources(context, 0, 1, &texture_view);
  141. ID3D11DeviceContext_RSSetScissorRects(context, 1, &scissor);
  142. ID3D11DeviceContext_DrawIndexed(context, (UINT)cmd->elem_count, offset, 0);
  143. offset += cmd->elem_count;
  144. }
  145. nk_clear(&d3d11.ctx);
  146. nk_buffer_clear(&d3d11.cmds);}
  147. }
  148. static void
  149. nk_d3d11_get_projection_matrix(int width, int height, float *result)
  150. {
  151. const float L = 0.0f;
  152. const float R = (float)width;
  153. const float T = 0.0f;
  154. const float B = (float)height;
  155. float matrix[4][4] =
  156. {
  157. { 0.0f, 0.0f, 0.0f, 0.0f },
  158. { 0.0f, 0.0f, 0.0f, 0.0f },
  159. { 0.0f, 0.0f, 0.5f, 0.0f },
  160. { 0.0f, 0.0f, 0.5f, 1.0f },
  161. };
  162. matrix[0][0] = 2.0f / (R - L);
  163. matrix[1][1] = 2.0f / (T - B);
  164. matrix[3][0] = (R + L) / (L - R);
  165. matrix[3][1] = (T + B) / (B - T);
  166. memcpy(result, matrix, sizeof(matrix));
  167. }
  168. NK_API void
  169. nk_d3d11_resize(ID3D11DeviceContext *context, int width, int height)
  170. {
  171. D3D11_MAPPED_SUBRESOURCE mapped;
  172. if (SUCCEEDED(ID3D11DeviceContext_Map(context, (ID3D11Resource *)d3d11.const_buffer, 0, D3D11_MAP_WRITE_DISCARD, 0, &mapped)))
  173. {
  174. nk_d3d11_get_projection_matrix(width, height, (float *)mapped.pData);
  175. ID3D11DeviceContext_Unmap(context, (ID3D11Resource *)d3d11.const_buffer, 0);
  176. d3d11.viewport.Width = (float)width;
  177. d3d11.viewport.Height = (float)height;
  178. }
  179. }
  180. NK_API int
  181. nk_d3d11_handle_event(HWND wnd, UINT msg, WPARAM wparam, LPARAM lparam)
  182. {
  183. switch (msg)
  184. {
  185. case WM_KEYDOWN:
  186. case WM_KEYUP:
  187. case WM_SYSKEYDOWN:
  188. case WM_SYSKEYUP:
  189. {
  190. int down = !((lparam >> 31) & 1);
  191. int ctrl = GetKeyState(VK_CONTROL) & (1 << 15);
  192. switch (wparam)
  193. {
  194. case VK_SHIFT:
  195. case VK_LSHIFT:
  196. case VK_RSHIFT:
  197. nk_input_key(&d3d11.ctx, NK_KEY_SHIFT, down);
  198. return 1;
  199. case VK_DELETE:
  200. nk_input_key(&d3d11.ctx, NK_KEY_DEL, down);
  201. return 1;
  202. case VK_RETURN:
  203. nk_input_key(&d3d11.ctx, NK_KEY_ENTER, down);
  204. return 1;
  205. case VK_TAB:
  206. nk_input_key(&d3d11.ctx, NK_KEY_TAB, down);
  207. return 1;
  208. case VK_LEFT:
  209. if (ctrl)
  210. nk_input_key(&d3d11.ctx, NK_KEY_TEXT_WORD_LEFT, down);
  211. else
  212. nk_input_key(&d3d11.ctx, NK_KEY_LEFT, down);
  213. return 1;
  214. case VK_RIGHT:
  215. if (ctrl)
  216. nk_input_key(&d3d11.ctx, NK_KEY_TEXT_WORD_RIGHT, down);
  217. else
  218. nk_input_key(&d3d11.ctx, NK_KEY_RIGHT, down);
  219. return 1;
  220. case VK_BACK:
  221. nk_input_key(&d3d11.ctx, NK_KEY_BACKSPACE, down);
  222. return 1;
  223. case VK_HOME:
  224. nk_input_key(&d3d11.ctx, NK_KEY_TEXT_START, down);
  225. nk_input_key(&d3d11.ctx, NK_KEY_SCROLL_START, down);
  226. return 1;
  227. case VK_END:
  228. nk_input_key(&d3d11.ctx, NK_KEY_TEXT_END, down);
  229. nk_input_key(&d3d11.ctx, NK_KEY_SCROLL_END, down);
  230. return 1;
  231. case VK_NEXT:
  232. nk_input_key(&d3d11.ctx, NK_KEY_SCROLL_DOWN, down);
  233. return 1;
  234. case VK_PRIOR:
  235. nk_input_key(&d3d11.ctx, NK_KEY_SCROLL_UP, down);
  236. return 1;
  237. case 'C':
  238. if (ctrl) {
  239. nk_input_key(&d3d11.ctx, NK_KEY_COPY, down);
  240. return 1;
  241. }
  242. break;
  243. case 'V':
  244. if (ctrl) {
  245. nk_input_key(&d3d11.ctx, NK_KEY_PASTE, down);
  246. return 1;
  247. }
  248. break;
  249. case 'X':
  250. if (ctrl) {
  251. nk_input_key(&d3d11.ctx, NK_KEY_CUT, down);
  252. return 1;
  253. }
  254. break;
  255. case 'Z':
  256. if (ctrl) {
  257. nk_input_key(&d3d11.ctx, NK_KEY_TEXT_UNDO, down);
  258. return 1;
  259. }
  260. break;
  261. case 'R':
  262. if (ctrl) {
  263. nk_input_key(&d3d11.ctx, NK_KEY_TEXT_REDO, down);
  264. return 1;
  265. }
  266. break;
  267. }
  268. return 0;
  269. }
  270. case WM_CHAR:
  271. if (wparam >= 32)
  272. {
  273. nk_input_unicode(&d3d11.ctx, (nk_rune)wparam);
  274. return 1;
  275. }
  276. break;
  277. case WM_LBUTTONDOWN:
  278. nk_input_button(&d3d11.ctx, NK_BUTTON_LEFT, (short)LOWORD(lparam), (short)HIWORD(lparam), 1);
  279. SetCapture(wnd);
  280. return 1;
  281. case WM_LBUTTONUP:
  282. nk_input_button(&d3d11.ctx, NK_BUTTON_DOUBLE, (short)LOWORD(lparam), (short)HIWORD(lparam), 0);
  283. nk_input_button(&d3d11.ctx, NK_BUTTON_LEFT, (short)LOWORD(lparam), (short)HIWORD(lparam), 0);
  284. ReleaseCapture();
  285. return 1;
  286. case WM_RBUTTONDOWN:
  287. nk_input_button(&d3d11.ctx, NK_BUTTON_RIGHT, (short)LOWORD(lparam), (short)HIWORD(lparam), 1);
  288. SetCapture(wnd);
  289. return 1;
  290. case WM_RBUTTONUP:
  291. nk_input_button(&d3d11.ctx, NK_BUTTON_RIGHT, (short)LOWORD(lparam), (short)HIWORD(lparam), 0);
  292. ReleaseCapture();
  293. return 1;
  294. case WM_MBUTTONDOWN:
  295. nk_input_button(&d3d11.ctx, NK_BUTTON_MIDDLE, (short)LOWORD(lparam), (short)HIWORD(lparam), 1);
  296. SetCapture(wnd);
  297. return 1;
  298. case WM_MBUTTONUP:
  299. nk_input_button(&d3d11.ctx, NK_BUTTON_MIDDLE, (short)LOWORD(lparam), (short)HIWORD(lparam), 0);
  300. ReleaseCapture();
  301. return 1;
  302. case WM_MOUSEWHEEL:
  303. nk_input_scroll(&d3d11.ctx, nk_vec2(0,(float)(short)HIWORD(wparam) / WHEEL_DELTA));
  304. return 1;
  305. case WM_MOUSEMOVE:
  306. nk_input_motion(&d3d11.ctx, (short)LOWORD(lparam), (short)HIWORD(lparam));
  307. return 1;
  308. case WM_LBUTTONDBLCLK:
  309. nk_input_button(&d3d11.ctx, NK_BUTTON_DOUBLE, (short)LOWORD(lparam), (short)HIWORD(lparam), 1);
  310. return 1;
  311. }
  312. return 0;
  313. }
  314. static void
  315. nk_d3d11_clipboard_paste(nk_handle usr, struct nk_text_edit *edit)
  316. {
  317. (void)usr;
  318. if (IsClipboardFormatAvailable(CF_UNICODETEXT) && OpenClipboard(NULL))
  319. {
  320. HGLOBAL mem = GetClipboardData(CF_UNICODETEXT);
  321. if (mem)
  322. {
  323. SIZE_T size = GlobalSize(mem) - 1;
  324. if (size)
  325. {
  326. LPCWSTR wstr = (LPCWSTR)GlobalLock(mem);
  327. if (wstr)
  328. {
  329. int utf8size = WideCharToMultiByte(CP_UTF8, 0, wstr, size / sizeof(wchar_t), NULL, 0, NULL, NULL);
  330. if (utf8size)
  331. {
  332. char* utf8 = (char*)malloc(utf8size);
  333. if (utf8)
  334. {
  335. WideCharToMultiByte(CP_UTF8, 0, wstr, size / sizeof(wchar_t), utf8, utf8size, NULL, NULL);
  336. nk_textedit_paste(edit, utf8, utf8size);
  337. free(utf8);
  338. }
  339. }
  340. GlobalUnlock(mem);
  341. }
  342. }
  343. }
  344. CloseClipboard();
  345. }
  346. }
  347. static void
  348. nk_d3d11_clipboard_copy(nk_handle usr, const char *text, int len)
  349. {
  350. (void)usr;
  351. if (OpenClipboard(NULL))
  352. {
  353. int wsize = MultiByteToWideChar(CP_UTF8, 0, text, len, NULL, 0);
  354. if (wsize)
  355. {
  356. HGLOBAL mem = GlobalAlloc(GMEM_MOVEABLE, (wsize + 1) * sizeof(wchar_t));
  357. if (mem)
  358. {
  359. wchar_t* wstr = (wchar_t*)GlobalLock(mem);
  360. if (wstr)
  361. {
  362. MultiByteToWideChar(CP_UTF8, 0, text, len, wstr, wsize);
  363. wstr[wsize] = 0;
  364. GlobalUnlock(mem);
  365. SetClipboardData(CF_UNICODETEXT, mem);
  366. }
  367. }
  368. }
  369. CloseClipboard();
  370. }
  371. }
  372. NK_API struct nk_context*
  373. nk_d3d11_init(ID3D11Device *device, int width, int height, unsigned int max_vertex_buffer, unsigned int max_index_buffer)
  374. {
  375. HRESULT hr;
  376. d3d11.max_vertex_buffer = max_vertex_buffer;
  377. d3d11.max_index_buffer = max_index_buffer;
  378. d3d11.device = device;
  379. ID3D11Device_AddRef(device);
  380. nk_init_default(&d3d11.ctx, 0);
  381. d3d11.ctx.clip.copy = nk_d3d11_clipboard_copy;
  382. d3d11.ctx.clip.paste = nk_d3d11_clipboard_paste;
  383. d3d11.ctx.clip.userdata = nk_handle_ptr(0);
  384. nk_buffer_init_default(&d3d11.cmds);
  385. {/* rasterizer state */
  386. D3D11_RASTERIZER_DESC desc;
  387. memset(&desc, 0, sizeof(desc));
  388. desc.FillMode = D3D11_FILL_SOLID;
  389. desc.CullMode = D3D11_CULL_NONE;
  390. desc.FrontCounterClockwise = FALSE;
  391. desc.DepthBias = 0;
  392. desc.DepthBiasClamp = 0;
  393. desc.SlopeScaledDepthBias = 0.0f;
  394. desc.DepthClipEnable = TRUE;
  395. desc.ScissorEnable = TRUE;
  396. desc.MultisampleEnable = FALSE;
  397. desc.AntialiasedLineEnable = FALSE;
  398. hr = ID3D11Device_CreateRasterizerState(device,&desc, &d3d11.rasterizer_state);
  399. NK_ASSERT(SUCCEEDED(hr));}
  400. /* vertex shader */
  401. {hr = ID3D11Device_CreateVertexShader(device,nk_d3d11_vertex_shader, sizeof(nk_d3d11_vertex_shader), NULL, &d3d11.vertex_shader);
  402. NK_ASSERT(SUCCEEDED(hr));}
  403. /* input layout */
  404. {const D3D11_INPUT_ELEMENT_DESC layout[] = {
  405. { "POSITION", 0, DXGI_FORMAT_R32G32B32A32_FLOAT, 0, offsetof(struct nk_d3d11_vertex, position), D3D11_INPUT_PER_VERTEX_DATA, 0 },
  406. { "TEXCOORD", 0, DXGI_FORMAT_R32G32_FLOAT, 0, offsetof(struct nk_d3d11_vertex, uv), D3D11_INPUT_PER_VERTEX_DATA, 0 },
  407. { "COLOR", 0, DXGI_FORMAT_R8G8B8A8_UNORM, 0, offsetof(struct nk_d3d11_vertex, col), D3D11_INPUT_PER_VERTEX_DATA, 0 },
  408. };
  409. hr = ID3D11Device_CreateInputLayout(device,layout, ARRAYSIZE(layout), nk_d3d11_vertex_shader, sizeof(nk_d3d11_vertex_shader), &d3d11.input_layout);
  410. NK_ASSERT(SUCCEEDED(hr));}
  411. /* constant buffer */
  412. {float matrix[4*4];
  413. D3D11_BUFFER_DESC desc;
  414. memset(&desc, 0, sizeof(desc));
  415. desc.ByteWidth = sizeof(matrix);
  416. desc.Usage = D3D11_USAGE_DYNAMIC;
  417. desc.BindFlags = D3D11_BIND_CONSTANT_BUFFER;
  418. desc.CPUAccessFlags = D3D11_CPU_ACCESS_WRITE;
  419. desc.MiscFlags = 0;
  420. {D3D11_SUBRESOURCE_DATA data;
  421. data.pSysMem = matrix;
  422. data.SysMemPitch = 0;
  423. data.SysMemSlicePitch = 0;
  424. nk_d3d11_get_projection_matrix(width, height, matrix);
  425. hr = ID3D11Device_CreateBuffer(device, &desc, &data, &d3d11.const_buffer);
  426. NK_ASSERT(SUCCEEDED(hr));}}
  427. /* pixel shader */
  428. {hr = ID3D11Device_CreatePixelShader(device, nk_d3d11_pixel_shader, sizeof(nk_d3d11_pixel_shader), NULL, &d3d11.pixel_shader);
  429. NK_ASSERT(SUCCEEDED(hr));}
  430. {/* blend state */
  431. D3D11_BLEND_DESC desc;
  432. memset(&desc, 0, sizeof(desc));
  433. desc.AlphaToCoverageEnable = FALSE;
  434. desc.RenderTarget[0].BlendEnable = TRUE;
  435. desc.RenderTarget[0].SrcBlend = D3D11_BLEND_SRC_ALPHA;
  436. desc.RenderTarget[0].DestBlend = D3D11_BLEND_INV_SRC_ALPHA;
  437. desc.RenderTarget[0].BlendOp = D3D11_BLEND_OP_ADD;
  438. desc.RenderTarget[0].SrcBlendAlpha = D3D11_BLEND_INV_SRC_ALPHA;
  439. desc.RenderTarget[0].DestBlendAlpha = D3D11_BLEND_ZERO;
  440. desc.RenderTarget[0].BlendOpAlpha = D3D11_BLEND_OP_ADD;
  441. desc.RenderTarget[0].RenderTargetWriteMask = D3D11_COLOR_WRITE_ENABLE_ALL;
  442. hr = ID3D11Device_CreateBlendState(device, &desc, &d3d11.blend_state);
  443. NK_ASSERT(SUCCEEDED(hr));}
  444. /* vertex buffer */
  445. {D3D11_BUFFER_DESC desc;
  446. memset(&desc, 0, sizeof(desc));
  447. desc.Usage = D3D11_USAGE_DYNAMIC;
  448. desc.ByteWidth = max_vertex_buffer;
  449. desc.BindFlags = D3D11_BIND_VERTEX_BUFFER;
  450. desc.CPUAccessFlags = D3D11_CPU_ACCESS_WRITE;
  451. desc.MiscFlags = 0;
  452. hr = ID3D11Device_CreateBuffer(device, &desc, NULL, &d3d11.vertex_buffer);
  453. NK_ASSERT(SUCCEEDED(hr));}
  454. /* index buffer */
  455. {D3D11_BUFFER_DESC desc;
  456. memset(&desc, 0, sizeof(desc));
  457. desc.Usage = D3D11_USAGE_DYNAMIC;
  458. desc.ByteWidth = max_index_buffer;
  459. desc.BindFlags = D3D11_BIND_INDEX_BUFFER;
  460. desc.CPUAccessFlags = D3D11_CPU_ACCESS_WRITE;
  461. hr = ID3D11Device_CreateBuffer(device, &desc, NULL, &d3d11.index_buffer);
  462. NK_ASSERT(SUCCEEDED(hr));}
  463. /* sampler state */
  464. {D3D11_SAMPLER_DESC desc;
  465. memset(&desc, 0, sizeof(desc));
  466. desc.Filter = D3D11_FILTER_MIN_MAG_MIP_LINEAR;
  467. desc.AddressU = D3D11_TEXTURE_ADDRESS_CLAMP;
  468. desc.AddressV = D3D11_TEXTURE_ADDRESS_CLAMP;
  469. desc.AddressW = D3D11_TEXTURE_ADDRESS_CLAMP;
  470. desc.MipLODBias = 0.0f;
  471. desc.ComparisonFunc = D3D11_COMPARISON_ALWAYS;
  472. desc.MinLOD = 0.0f;
  473. desc.MaxLOD = FLT_MAX;
  474. hr = ID3D11Device_CreateSamplerState(device, &desc, &d3d11.sampler_state);
  475. NK_ASSERT(SUCCEEDED(hr));}
  476. /* viewport */
  477. {d3d11.viewport.TopLeftX = 0.0f;
  478. d3d11.viewport.TopLeftY = 0.0f;
  479. d3d11.viewport.Width = (float)width;
  480. d3d11.viewport.Height = (float)height;
  481. d3d11.viewport.MinDepth = 0.0f;
  482. d3d11.viewport.MaxDepth = 1.0f;}
  483. return &d3d11.ctx;
  484. }
  485. NK_API void
  486. nk_d3d11_font_stash_begin(struct nk_font_atlas **atlas)
  487. {
  488. nk_font_atlas_init_default(&d3d11.atlas);
  489. nk_font_atlas_begin(&d3d11.atlas);
  490. *atlas = &d3d11.atlas;
  491. }
  492. NK_API void
  493. nk_d3d11_font_stash_end(void)
  494. {
  495. const void *image; int w, h;
  496. image = nk_font_atlas_bake(&d3d11.atlas, &w, &h, NK_FONT_ATLAS_RGBA32);
  497. /* upload font to texture and create texture view */
  498. {ID3D11Texture2D *font_texture;
  499. HRESULT hr;
  500. D3D11_TEXTURE2D_DESC desc;
  501. memset(&desc, 0, sizeof(desc));
  502. desc.Width = (UINT)w;
  503. desc.Height = (UINT)h;
  504. desc.MipLevels = 1;
  505. desc.ArraySize = 1;
  506. desc.Format = DXGI_FORMAT_R8G8B8A8_UNORM;
  507. desc.SampleDesc.Count = 1;
  508. desc.SampleDesc.Quality = 0;
  509. desc.Usage = D3D11_USAGE_DEFAULT;
  510. desc.BindFlags = D3D11_BIND_SHADER_RESOURCE;
  511. desc.CPUAccessFlags = 0;
  512. {D3D11_SUBRESOURCE_DATA data;
  513. data.pSysMem = image;
  514. data.SysMemPitch = (UINT)(w * 4);
  515. data.SysMemSlicePitch = 0;
  516. hr = ID3D11Device_CreateTexture2D(d3d11.device, &desc, &data, &font_texture);
  517. assert(SUCCEEDED(hr));}
  518. {D3D11_SHADER_RESOURCE_VIEW_DESC srv;
  519. memset(&srv, 0, sizeof(srv));
  520. srv.Format = desc.Format;
  521. srv.ViewDimension = D3D11_SRV_DIMENSION_TEXTURE2D;
  522. srv.Texture2D.MipLevels = 1;
  523. srv.Texture2D.MostDetailedMip = 0;
  524. hr = ID3D11Device_CreateShaderResourceView(d3d11.device, (ID3D11Resource *)font_texture, &srv, &d3d11.font_texture_view);
  525. assert(SUCCEEDED(hr));}
  526. ID3D11Texture2D_Release(font_texture);}
  527. nk_font_atlas_end(&d3d11.atlas, nk_handle_ptr(d3d11.font_texture_view), &d3d11.tex_null);
  528. if (d3d11.atlas.default_font)
  529. nk_style_set_font(&d3d11.ctx, &d3d11.atlas.default_font->handle);
  530. }
  531. NK_API
  532. void nk_d3d11_shutdown(void)
  533. {
  534. nk_font_atlas_clear(&d3d11.atlas);
  535. nk_buffer_free(&d3d11.cmds);
  536. nk_free(&d3d11.ctx);
  537. ID3D11SamplerState_Release(d3d11.sampler_state);
  538. ID3D11ShaderResourceView_Release(d3d11.font_texture_view);
  539. ID3D11Buffer_Release(d3d11.vertex_buffer);
  540. ID3D11Buffer_Release(d3d11.index_buffer);
  541. ID3D11BlendState_Release(d3d11.blend_state);
  542. ID3D11PixelShader_Release(d3d11.pixel_shader);
  543. ID3D11Buffer_Release(d3d11.const_buffer);
  544. ID3D11VertexShader_Release(d3d11.vertex_shader);
  545. ID3D11InputLayout_Release(d3d11.input_layout);
  546. ID3D11RasterizerState_Release(d3d11.rasterizer_state);
  547. ID3D11Device_Release(d3d11.device);
  548. }
  549. #endif