nuklear_d3d11.h 21 KB

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