nuklear_sdl_vulkan.h 72 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654
  1. /*
  2. * Nuklear - 1.32.0 - public domain
  3. * no warranty 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_SDL_VULKAN_H_
  14. #define NK_SDL_VULKAN_H_
  15. unsigned char nuklearshaders_nuklear_vert_spv[] = {
  16. 0x03, 0x02, 0x23, 0x07, 0x00, 0x00, 0x01, 0x00, 0x0b, 0x00, 0x0d, 0x00,
  17. 0x45, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x11, 0x00, 0x02, 0x00,
  18. 0x01, 0x00, 0x00, 0x00, 0x0b, 0x00, 0x06, 0x00, 0x01, 0x00, 0x00, 0x00,
  19. 0x47, 0x4c, 0x53, 0x4c, 0x2e, 0x73, 0x74, 0x64, 0x2e, 0x34, 0x35, 0x30,
  20. 0x00, 0x00, 0x00, 0x00, 0x0e, 0x00, 0x03, 0x00, 0x00, 0x00, 0x00, 0x00,
  21. 0x01, 0x00, 0x00, 0x00, 0x0f, 0x00, 0x0b, 0x00, 0x00, 0x00, 0x00, 0x00,
  22. 0x04, 0x00, 0x00, 0x00, 0x6d, 0x61, 0x69, 0x6e, 0x00, 0x00, 0x00, 0x00,
  23. 0x0a, 0x00, 0x00, 0x00, 0x16, 0x00, 0x00, 0x00, 0x27, 0x00, 0x00, 0x00,
  24. 0x2a, 0x00, 0x00, 0x00, 0x42, 0x00, 0x00, 0x00, 0x43, 0x00, 0x00, 0x00,
  25. 0x03, 0x00, 0x03, 0x00, 0x02, 0x00, 0x00, 0x00, 0xc2, 0x01, 0x00, 0x00,
  26. 0x04, 0x00, 0x09, 0x00, 0x47, 0x4c, 0x5f, 0x41, 0x52, 0x42, 0x5f, 0x73,
  27. 0x65, 0x70, 0x61, 0x72, 0x61, 0x74, 0x65, 0x5f, 0x73, 0x68, 0x61, 0x64,
  28. 0x65, 0x72, 0x5f, 0x6f, 0x62, 0x6a, 0x65, 0x63, 0x74, 0x73, 0x00, 0x00,
  29. 0x04, 0x00, 0x0a, 0x00, 0x47, 0x4c, 0x5f, 0x47, 0x4f, 0x4f, 0x47, 0x4c,
  30. 0x45, 0x5f, 0x63, 0x70, 0x70, 0x5f, 0x73, 0x74, 0x79, 0x6c, 0x65, 0x5f,
  31. 0x6c, 0x69, 0x6e, 0x65, 0x5f, 0x64, 0x69, 0x72, 0x65, 0x63, 0x74, 0x69,
  32. 0x76, 0x65, 0x00, 0x00, 0x04, 0x00, 0x08, 0x00, 0x47, 0x4c, 0x5f, 0x47,
  33. 0x4f, 0x4f, 0x47, 0x4c, 0x45, 0x5f, 0x69, 0x6e, 0x63, 0x6c, 0x75, 0x64,
  34. 0x65, 0x5f, 0x64, 0x69, 0x72, 0x65, 0x63, 0x74, 0x69, 0x76, 0x65, 0x00,
  35. 0x05, 0x00, 0x04, 0x00, 0x04, 0x00, 0x00, 0x00, 0x6d, 0x61, 0x69, 0x6e,
  36. 0x00, 0x00, 0x00, 0x00, 0x05, 0x00, 0x06, 0x00, 0x08, 0x00, 0x00, 0x00,
  37. 0x67, 0x6c, 0x5f, 0x50, 0x65, 0x72, 0x56, 0x65, 0x72, 0x74, 0x65, 0x78,
  38. 0x00, 0x00, 0x00, 0x00, 0x06, 0x00, 0x06, 0x00, 0x08, 0x00, 0x00, 0x00,
  39. 0x00, 0x00, 0x00, 0x00, 0x67, 0x6c, 0x5f, 0x50, 0x6f, 0x73, 0x69, 0x74,
  40. 0x69, 0x6f, 0x6e, 0x00, 0x05, 0x00, 0x03, 0x00, 0x0a, 0x00, 0x00, 0x00,
  41. 0x00, 0x00, 0x00, 0x00, 0x05, 0x00, 0x07, 0x00, 0x0e, 0x00, 0x00, 0x00,
  42. 0x55, 0x6e, 0x69, 0x66, 0x6f, 0x72, 0x6d, 0x42, 0x75, 0x66, 0x66, 0x65,
  43. 0x72, 0x4f, 0x62, 0x6a, 0x65, 0x63, 0x74, 0x00, 0x06, 0x00, 0x06, 0x00,
  44. 0x0e, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x70, 0x72, 0x6f, 0x6a,
  45. 0x65, 0x63, 0x74, 0x69, 0x6f, 0x6e, 0x00, 0x00, 0x05, 0x00, 0x03, 0x00,
  46. 0x10, 0x00, 0x00, 0x00, 0x75, 0x62, 0x6f, 0x00, 0x05, 0x00, 0x05, 0x00,
  47. 0x16, 0x00, 0x00, 0x00, 0x70, 0x6f, 0x73, 0x69, 0x74, 0x69, 0x6f, 0x6e,
  48. 0x00, 0x00, 0x00, 0x00, 0x05, 0x00, 0x05, 0x00, 0x27, 0x00, 0x00, 0x00,
  49. 0x66, 0x72, 0x61, 0x67, 0x43, 0x6f, 0x6c, 0x6f, 0x72, 0x00, 0x00, 0x00,
  50. 0x05, 0x00, 0x04, 0x00, 0x2a, 0x00, 0x00, 0x00, 0x63, 0x6f, 0x6c, 0x6f,
  51. 0x72, 0x00, 0x00, 0x00, 0x05, 0x00, 0x04, 0x00, 0x42, 0x00, 0x00, 0x00,
  52. 0x66, 0x72, 0x61, 0x67, 0x55, 0x76, 0x00, 0x00, 0x05, 0x00, 0x03, 0x00,
  53. 0x43, 0x00, 0x00, 0x00, 0x75, 0x76, 0x00, 0x00, 0x48, 0x00, 0x05, 0x00,
  54. 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x0b, 0x00, 0x00, 0x00,
  55. 0x00, 0x00, 0x00, 0x00, 0x47, 0x00, 0x03, 0x00, 0x08, 0x00, 0x00, 0x00,
  56. 0x02, 0x00, 0x00, 0x00, 0x48, 0x00, 0x04, 0x00, 0x0e, 0x00, 0x00, 0x00,
  57. 0x00, 0x00, 0x00, 0x00, 0x05, 0x00, 0x00, 0x00, 0x48, 0x00, 0x05, 0x00,
  58. 0x0e, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x23, 0x00, 0x00, 0x00,
  59. 0x00, 0x00, 0x00, 0x00, 0x48, 0x00, 0x05, 0x00, 0x0e, 0x00, 0x00, 0x00,
  60. 0x00, 0x00, 0x00, 0x00, 0x07, 0x00, 0x00, 0x00, 0x10, 0x00, 0x00, 0x00,
  61. 0x47, 0x00, 0x03, 0x00, 0x0e, 0x00, 0x00, 0x00, 0x02, 0x00, 0x00, 0x00,
  62. 0x47, 0x00, 0x04, 0x00, 0x10, 0x00, 0x00, 0x00, 0x22, 0x00, 0x00, 0x00,
  63. 0x00, 0x00, 0x00, 0x00, 0x47, 0x00, 0x04, 0x00, 0x10, 0x00, 0x00, 0x00,
  64. 0x21, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x47, 0x00, 0x04, 0x00,
  65. 0x16, 0x00, 0x00, 0x00, 0x1e, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  66. 0x47, 0x00, 0x04, 0x00, 0x27, 0x00, 0x00, 0x00, 0x1e, 0x00, 0x00, 0x00,
  67. 0x00, 0x00, 0x00, 0x00, 0x47, 0x00, 0x04, 0x00, 0x2a, 0x00, 0x00, 0x00,
  68. 0x1e, 0x00, 0x00, 0x00, 0x02, 0x00, 0x00, 0x00, 0x47, 0x00, 0x04, 0x00,
  69. 0x42, 0x00, 0x00, 0x00, 0x1e, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00,
  70. 0x47, 0x00, 0x04, 0x00, 0x43, 0x00, 0x00, 0x00, 0x1e, 0x00, 0x00, 0x00,
  71. 0x01, 0x00, 0x00, 0x00, 0x13, 0x00, 0x02, 0x00, 0x02, 0x00, 0x00, 0x00,
  72. 0x21, 0x00, 0x03, 0x00, 0x03, 0x00, 0x00, 0x00, 0x02, 0x00, 0x00, 0x00,
  73. 0x16, 0x00, 0x03, 0x00, 0x06, 0x00, 0x00, 0x00, 0x20, 0x00, 0x00, 0x00,
  74. 0x17, 0x00, 0x04, 0x00, 0x07, 0x00, 0x00, 0x00, 0x06, 0x00, 0x00, 0x00,
  75. 0x04, 0x00, 0x00, 0x00, 0x1e, 0x00, 0x03, 0x00, 0x08, 0x00, 0x00, 0x00,
  76. 0x07, 0x00, 0x00, 0x00, 0x20, 0x00, 0x04, 0x00, 0x09, 0x00, 0x00, 0x00,
  77. 0x03, 0x00, 0x00, 0x00, 0x08, 0x00, 0x00, 0x00, 0x3b, 0x00, 0x04, 0x00,
  78. 0x09, 0x00, 0x00, 0x00, 0x0a, 0x00, 0x00, 0x00, 0x03, 0x00, 0x00, 0x00,
  79. 0x15, 0x00, 0x04, 0x00, 0x0b, 0x00, 0x00, 0x00, 0x20, 0x00, 0x00, 0x00,
  80. 0x01, 0x00, 0x00, 0x00, 0x2b, 0x00, 0x04, 0x00, 0x0b, 0x00, 0x00, 0x00,
  81. 0x0c, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x18, 0x00, 0x04, 0x00,
  82. 0x0d, 0x00, 0x00, 0x00, 0x07, 0x00, 0x00, 0x00, 0x04, 0x00, 0x00, 0x00,
  83. 0x1e, 0x00, 0x03, 0x00, 0x0e, 0x00, 0x00, 0x00, 0x0d, 0x00, 0x00, 0x00,
  84. 0x20, 0x00, 0x04, 0x00, 0x0f, 0x00, 0x00, 0x00, 0x02, 0x00, 0x00, 0x00,
  85. 0x0e, 0x00, 0x00, 0x00, 0x3b, 0x00, 0x04, 0x00, 0x0f, 0x00, 0x00, 0x00,
  86. 0x10, 0x00, 0x00, 0x00, 0x02, 0x00, 0x00, 0x00, 0x20, 0x00, 0x04, 0x00,
  87. 0x11, 0x00, 0x00, 0x00, 0x02, 0x00, 0x00, 0x00, 0x0d, 0x00, 0x00, 0x00,
  88. 0x17, 0x00, 0x04, 0x00, 0x14, 0x00, 0x00, 0x00, 0x06, 0x00, 0x00, 0x00,
  89. 0x02, 0x00, 0x00, 0x00, 0x20, 0x00, 0x04, 0x00, 0x15, 0x00, 0x00, 0x00,
  90. 0x01, 0x00, 0x00, 0x00, 0x14, 0x00, 0x00, 0x00, 0x3b, 0x00, 0x04, 0x00,
  91. 0x15, 0x00, 0x00, 0x00, 0x16, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00,
  92. 0x2b, 0x00, 0x04, 0x00, 0x06, 0x00, 0x00, 0x00, 0x18, 0x00, 0x00, 0x00,
  93. 0x00, 0x00, 0x00, 0x00, 0x2b, 0x00, 0x04, 0x00, 0x06, 0x00, 0x00, 0x00,
  94. 0x19, 0x00, 0x00, 0x00, 0x00, 0x00, 0x80, 0x3f, 0x20, 0x00, 0x04, 0x00,
  95. 0x1e, 0x00, 0x00, 0x00, 0x03, 0x00, 0x00, 0x00, 0x07, 0x00, 0x00, 0x00,
  96. 0x15, 0x00, 0x04, 0x00, 0x20, 0x00, 0x00, 0x00, 0x20, 0x00, 0x00, 0x00,
  97. 0x00, 0x00, 0x00, 0x00, 0x2b, 0x00, 0x04, 0x00, 0x20, 0x00, 0x00, 0x00,
  98. 0x21, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x20, 0x00, 0x04, 0x00,
  99. 0x22, 0x00, 0x00, 0x00, 0x03, 0x00, 0x00, 0x00, 0x06, 0x00, 0x00, 0x00,
  100. 0x3b, 0x00, 0x04, 0x00, 0x1e, 0x00, 0x00, 0x00, 0x27, 0x00, 0x00, 0x00,
  101. 0x03, 0x00, 0x00, 0x00, 0x17, 0x00, 0x04, 0x00, 0x28, 0x00, 0x00, 0x00,
  102. 0x20, 0x00, 0x00, 0x00, 0x04, 0x00, 0x00, 0x00, 0x20, 0x00, 0x04, 0x00,
  103. 0x29, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x28, 0x00, 0x00, 0x00,
  104. 0x3b, 0x00, 0x04, 0x00, 0x29, 0x00, 0x00, 0x00, 0x2a, 0x00, 0x00, 0x00,
  105. 0x01, 0x00, 0x00, 0x00, 0x2b, 0x00, 0x04, 0x00, 0x20, 0x00, 0x00, 0x00,
  106. 0x2b, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x20, 0x00, 0x04, 0x00,
  107. 0x2c, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x20, 0x00, 0x00, 0x00,
  108. 0x2b, 0x00, 0x04, 0x00, 0x06, 0x00, 0x00, 0x00, 0x30, 0x00, 0x00, 0x00,
  109. 0x00, 0x00, 0x7f, 0x43, 0x2b, 0x00, 0x04, 0x00, 0x20, 0x00, 0x00, 0x00,
  110. 0x36, 0x00, 0x00, 0x00, 0x02, 0x00, 0x00, 0x00, 0x2b, 0x00, 0x04, 0x00,
  111. 0x20, 0x00, 0x00, 0x00, 0x3b, 0x00, 0x00, 0x00, 0x03, 0x00, 0x00, 0x00,
  112. 0x20, 0x00, 0x04, 0x00, 0x41, 0x00, 0x00, 0x00, 0x03, 0x00, 0x00, 0x00,
  113. 0x14, 0x00, 0x00, 0x00, 0x3b, 0x00, 0x04, 0x00, 0x41, 0x00, 0x00, 0x00,
  114. 0x42, 0x00, 0x00, 0x00, 0x03, 0x00, 0x00, 0x00, 0x3b, 0x00, 0x04, 0x00,
  115. 0x15, 0x00, 0x00, 0x00, 0x43, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00,
  116. 0x36, 0x00, 0x05, 0x00, 0x02, 0x00, 0x00, 0x00, 0x04, 0x00, 0x00, 0x00,
  117. 0x00, 0x00, 0x00, 0x00, 0x03, 0x00, 0x00, 0x00, 0xf8, 0x00, 0x02, 0x00,
  118. 0x05, 0x00, 0x00, 0x00, 0x41, 0x00, 0x05, 0x00, 0x11, 0x00, 0x00, 0x00,
  119. 0x12, 0x00, 0x00, 0x00, 0x10, 0x00, 0x00, 0x00, 0x0c, 0x00, 0x00, 0x00,
  120. 0x3d, 0x00, 0x04, 0x00, 0x0d, 0x00, 0x00, 0x00, 0x13, 0x00, 0x00, 0x00,
  121. 0x12, 0x00, 0x00, 0x00, 0x3d, 0x00, 0x04, 0x00, 0x14, 0x00, 0x00, 0x00,
  122. 0x17, 0x00, 0x00, 0x00, 0x16, 0x00, 0x00, 0x00, 0x51, 0x00, 0x05, 0x00,
  123. 0x06, 0x00, 0x00, 0x00, 0x1a, 0x00, 0x00, 0x00, 0x17, 0x00, 0x00, 0x00,
  124. 0x00, 0x00, 0x00, 0x00, 0x51, 0x00, 0x05, 0x00, 0x06, 0x00, 0x00, 0x00,
  125. 0x1b, 0x00, 0x00, 0x00, 0x17, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00,
  126. 0x50, 0x00, 0x07, 0x00, 0x07, 0x00, 0x00, 0x00, 0x1c, 0x00, 0x00, 0x00,
  127. 0x1a, 0x00, 0x00, 0x00, 0x1b, 0x00, 0x00, 0x00, 0x18, 0x00, 0x00, 0x00,
  128. 0x19, 0x00, 0x00, 0x00, 0x91, 0x00, 0x05, 0x00, 0x07, 0x00, 0x00, 0x00,
  129. 0x1d, 0x00, 0x00, 0x00, 0x13, 0x00, 0x00, 0x00, 0x1c, 0x00, 0x00, 0x00,
  130. 0x41, 0x00, 0x05, 0x00, 0x1e, 0x00, 0x00, 0x00, 0x1f, 0x00, 0x00, 0x00,
  131. 0x0a, 0x00, 0x00, 0x00, 0x0c, 0x00, 0x00, 0x00, 0x3e, 0x00, 0x03, 0x00,
  132. 0x1f, 0x00, 0x00, 0x00, 0x1d, 0x00, 0x00, 0x00, 0x41, 0x00, 0x06, 0x00,
  133. 0x22, 0x00, 0x00, 0x00, 0x23, 0x00, 0x00, 0x00, 0x0a, 0x00, 0x00, 0x00,
  134. 0x0c, 0x00, 0x00, 0x00, 0x21, 0x00, 0x00, 0x00, 0x3d, 0x00, 0x04, 0x00,
  135. 0x06, 0x00, 0x00, 0x00, 0x24, 0x00, 0x00, 0x00, 0x23, 0x00, 0x00, 0x00,
  136. 0x7f, 0x00, 0x04, 0x00, 0x06, 0x00, 0x00, 0x00, 0x25, 0x00, 0x00, 0x00,
  137. 0x24, 0x00, 0x00, 0x00, 0x41, 0x00, 0x06, 0x00, 0x22, 0x00, 0x00, 0x00,
  138. 0x26, 0x00, 0x00, 0x00, 0x0a, 0x00, 0x00, 0x00, 0x0c, 0x00, 0x00, 0x00,
  139. 0x21, 0x00, 0x00, 0x00, 0x3e, 0x00, 0x03, 0x00, 0x26, 0x00, 0x00, 0x00,
  140. 0x25, 0x00, 0x00, 0x00, 0x41, 0x00, 0x05, 0x00, 0x2c, 0x00, 0x00, 0x00,
  141. 0x2d, 0x00, 0x00, 0x00, 0x2a, 0x00, 0x00, 0x00, 0x2b, 0x00, 0x00, 0x00,
  142. 0x3d, 0x00, 0x04, 0x00, 0x20, 0x00, 0x00, 0x00, 0x2e, 0x00, 0x00, 0x00,
  143. 0x2d, 0x00, 0x00, 0x00, 0x70, 0x00, 0x04, 0x00, 0x06, 0x00, 0x00, 0x00,
  144. 0x2f, 0x00, 0x00, 0x00, 0x2e, 0x00, 0x00, 0x00, 0x88, 0x00, 0x05, 0x00,
  145. 0x06, 0x00, 0x00, 0x00, 0x31, 0x00, 0x00, 0x00, 0x2f, 0x00, 0x00, 0x00,
  146. 0x30, 0x00, 0x00, 0x00, 0x41, 0x00, 0x05, 0x00, 0x2c, 0x00, 0x00, 0x00,
  147. 0x32, 0x00, 0x00, 0x00, 0x2a, 0x00, 0x00, 0x00, 0x21, 0x00, 0x00, 0x00,
  148. 0x3d, 0x00, 0x04, 0x00, 0x20, 0x00, 0x00, 0x00, 0x33, 0x00, 0x00, 0x00,
  149. 0x32, 0x00, 0x00, 0x00, 0x70, 0x00, 0x04, 0x00, 0x06, 0x00, 0x00, 0x00,
  150. 0x34, 0x00, 0x00, 0x00, 0x33, 0x00, 0x00, 0x00, 0x88, 0x00, 0x05, 0x00,
  151. 0x06, 0x00, 0x00, 0x00, 0x35, 0x00, 0x00, 0x00, 0x34, 0x00, 0x00, 0x00,
  152. 0x30, 0x00, 0x00, 0x00, 0x41, 0x00, 0x05, 0x00, 0x2c, 0x00, 0x00, 0x00,
  153. 0x37, 0x00, 0x00, 0x00, 0x2a, 0x00, 0x00, 0x00, 0x36, 0x00, 0x00, 0x00,
  154. 0x3d, 0x00, 0x04, 0x00, 0x20, 0x00, 0x00, 0x00, 0x38, 0x00, 0x00, 0x00,
  155. 0x37, 0x00, 0x00, 0x00, 0x70, 0x00, 0x04, 0x00, 0x06, 0x00, 0x00, 0x00,
  156. 0x39, 0x00, 0x00, 0x00, 0x38, 0x00, 0x00, 0x00, 0x88, 0x00, 0x05, 0x00,
  157. 0x06, 0x00, 0x00, 0x00, 0x3a, 0x00, 0x00, 0x00, 0x39, 0x00, 0x00, 0x00,
  158. 0x30, 0x00, 0x00, 0x00, 0x41, 0x00, 0x05, 0x00, 0x2c, 0x00, 0x00, 0x00,
  159. 0x3c, 0x00, 0x00, 0x00, 0x2a, 0x00, 0x00, 0x00, 0x3b, 0x00, 0x00, 0x00,
  160. 0x3d, 0x00, 0x04, 0x00, 0x20, 0x00, 0x00, 0x00, 0x3d, 0x00, 0x00, 0x00,
  161. 0x3c, 0x00, 0x00, 0x00, 0x70, 0x00, 0x04, 0x00, 0x06, 0x00, 0x00, 0x00,
  162. 0x3e, 0x00, 0x00, 0x00, 0x3d, 0x00, 0x00, 0x00, 0x88, 0x00, 0x05, 0x00,
  163. 0x06, 0x00, 0x00, 0x00, 0x3f, 0x00, 0x00, 0x00, 0x3e, 0x00, 0x00, 0x00,
  164. 0x30, 0x00, 0x00, 0x00, 0x50, 0x00, 0x07, 0x00, 0x07, 0x00, 0x00, 0x00,
  165. 0x40, 0x00, 0x00, 0x00, 0x31, 0x00, 0x00, 0x00, 0x35, 0x00, 0x00, 0x00,
  166. 0x3a, 0x00, 0x00, 0x00, 0x3f, 0x00, 0x00, 0x00, 0x3e, 0x00, 0x03, 0x00,
  167. 0x27, 0x00, 0x00, 0x00, 0x40, 0x00, 0x00, 0x00, 0x3d, 0x00, 0x04, 0x00,
  168. 0x14, 0x00, 0x00, 0x00, 0x44, 0x00, 0x00, 0x00, 0x43, 0x00, 0x00, 0x00,
  169. 0x3e, 0x00, 0x03, 0x00, 0x42, 0x00, 0x00, 0x00, 0x44, 0x00, 0x00, 0x00,
  170. 0xfd, 0x00, 0x01, 0x00, 0x38, 0x00, 0x01, 0x00
  171. };
  172. unsigned int nuklearshaders_nuklear_vert_spv_len = 1856;
  173. unsigned char nuklearshaders_nuklear_frag_spv[] = {
  174. 0x03, 0x02, 0x23, 0x07, 0x00, 0x00, 0x01, 0x00, 0x0b, 0x00, 0x0d, 0x00,
  175. 0x1b, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x11, 0x00, 0x02, 0x00,
  176. 0x01, 0x00, 0x00, 0x00, 0x0b, 0x00, 0x06, 0x00, 0x01, 0x00, 0x00, 0x00,
  177. 0x47, 0x4c, 0x53, 0x4c, 0x2e, 0x73, 0x74, 0x64, 0x2e, 0x34, 0x35, 0x30,
  178. 0x00, 0x00, 0x00, 0x00, 0x0e, 0x00, 0x03, 0x00, 0x00, 0x00, 0x00, 0x00,
  179. 0x01, 0x00, 0x00, 0x00, 0x0f, 0x00, 0x08, 0x00, 0x04, 0x00, 0x00, 0x00,
  180. 0x04, 0x00, 0x00, 0x00, 0x6d, 0x61, 0x69, 0x6e, 0x00, 0x00, 0x00, 0x00,
  181. 0x11, 0x00, 0x00, 0x00, 0x15, 0x00, 0x00, 0x00, 0x17, 0x00, 0x00, 0x00,
  182. 0x10, 0x00, 0x03, 0x00, 0x04, 0x00, 0x00, 0x00, 0x07, 0x00, 0x00, 0x00,
  183. 0x03, 0x00, 0x03, 0x00, 0x02, 0x00, 0x00, 0x00, 0xc2, 0x01, 0x00, 0x00,
  184. 0x04, 0x00, 0x09, 0x00, 0x47, 0x4c, 0x5f, 0x41, 0x52, 0x42, 0x5f, 0x73,
  185. 0x65, 0x70, 0x61, 0x72, 0x61, 0x74, 0x65, 0x5f, 0x73, 0x68, 0x61, 0x64,
  186. 0x65, 0x72, 0x5f, 0x6f, 0x62, 0x6a, 0x65, 0x63, 0x74, 0x73, 0x00, 0x00,
  187. 0x04, 0x00, 0x0a, 0x00, 0x47, 0x4c, 0x5f, 0x47, 0x4f, 0x4f, 0x47, 0x4c,
  188. 0x45, 0x5f, 0x63, 0x70, 0x70, 0x5f, 0x73, 0x74, 0x79, 0x6c, 0x65, 0x5f,
  189. 0x6c, 0x69, 0x6e, 0x65, 0x5f, 0x64, 0x69, 0x72, 0x65, 0x63, 0x74, 0x69,
  190. 0x76, 0x65, 0x00, 0x00, 0x04, 0x00, 0x08, 0x00, 0x47, 0x4c, 0x5f, 0x47,
  191. 0x4f, 0x4f, 0x47, 0x4c, 0x45, 0x5f, 0x69, 0x6e, 0x63, 0x6c, 0x75, 0x64,
  192. 0x65, 0x5f, 0x64, 0x69, 0x72, 0x65, 0x63, 0x74, 0x69, 0x76, 0x65, 0x00,
  193. 0x05, 0x00, 0x04, 0x00, 0x04, 0x00, 0x00, 0x00, 0x6d, 0x61, 0x69, 0x6e,
  194. 0x00, 0x00, 0x00, 0x00, 0x05, 0x00, 0x05, 0x00, 0x09, 0x00, 0x00, 0x00,
  195. 0x74, 0x65, 0x78, 0x43, 0x6f, 0x6c, 0x6f, 0x72, 0x00, 0x00, 0x00, 0x00,
  196. 0x05, 0x00, 0x06, 0x00, 0x0d, 0x00, 0x00, 0x00, 0x63, 0x75, 0x72, 0x72,
  197. 0x65, 0x6e, 0x74, 0x54, 0x65, 0x78, 0x74, 0x75, 0x72, 0x65, 0x00, 0x00,
  198. 0x05, 0x00, 0x04, 0x00, 0x11, 0x00, 0x00, 0x00, 0x66, 0x72, 0x61, 0x67,
  199. 0x55, 0x76, 0x00, 0x00, 0x05, 0x00, 0x05, 0x00, 0x15, 0x00, 0x00, 0x00,
  200. 0x6f, 0x75, 0x74, 0x43, 0x6f, 0x6c, 0x6f, 0x72, 0x00, 0x00, 0x00, 0x00,
  201. 0x05, 0x00, 0x05, 0x00, 0x17, 0x00, 0x00, 0x00, 0x66, 0x72, 0x61, 0x67,
  202. 0x43, 0x6f, 0x6c, 0x6f, 0x72, 0x00, 0x00, 0x00, 0x47, 0x00, 0x04, 0x00,
  203. 0x0d, 0x00, 0x00, 0x00, 0x22, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00,
  204. 0x47, 0x00, 0x04, 0x00, 0x0d, 0x00, 0x00, 0x00, 0x21, 0x00, 0x00, 0x00,
  205. 0x00, 0x00, 0x00, 0x00, 0x47, 0x00, 0x04, 0x00, 0x11, 0x00, 0x00, 0x00,
  206. 0x1e, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x47, 0x00, 0x04, 0x00,
  207. 0x15, 0x00, 0x00, 0x00, 0x1e, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  208. 0x47, 0x00, 0x04, 0x00, 0x17, 0x00, 0x00, 0x00, 0x1e, 0x00, 0x00, 0x00,
  209. 0x00, 0x00, 0x00, 0x00, 0x13, 0x00, 0x02, 0x00, 0x02, 0x00, 0x00, 0x00,
  210. 0x21, 0x00, 0x03, 0x00, 0x03, 0x00, 0x00, 0x00, 0x02, 0x00, 0x00, 0x00,
  211. 0x16, 0x00, 0x03, 0x00, 0x06, 0x00, 0x00, 0x00, 0x20, 0x00, 0x00, 0x00,
  212. 0x17, 0x00, 0x04, 0x00, 0x07, 0x00, 0x00, 0x00, 0x06, 0x00, 0x00, 0x00,
  213. 0x04, 0x00, 0x00, 0x00, 0x20, 0x00, 0x04, 0x00, 0x08, 0x00, 0x00, 0x00,
  214. 0x07, 0x00, 0x00, 0x00, 0x07, 0x00, 0x00, 0x00, 0x19, 0x00, 0x09, 0x00,
  215. 0x0a, 0x00, 0x00, 0x00, 0x06, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00,
  216. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  217. 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x1b, 0x00, 0x03, 0x00,
  218. 0x0b, 0x00, 0x00, 0x00, 0x0a, 0x00, 0x00, 0x00, 0x20, 0x00, 0x04, 0x00,
  219. 0x0c, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x0b, 0x00, 0x00, 0x00,
  220. 0x3b, 0x00, 0x04, 0x00, 0x0c, 0x00, 0x00, 0x00, 0x0d, 0x00, 0x00, 0x00,
  221. 0x00, 0x00, 0x00, 0x00, 0x17, 0x00, 0x04, 0x00, 0x0f, 0x00, 0x00, 0x00,
  222. 0x06, 0x00, 0x00, 0x00, 0x02, 0x00, 0x00, 0x00, 0x20, 0x00, 0x04, 0x00,
  223. 0x10, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x0f, 0x00, 0x00, 0x00,
  224. 0x3b, 0x00, 0x04, 0x00, 0x10, 0x00, 0x00, 0x00, 0x11, 0x00, 0x00, 0x00,
  225. 0x01, 0x00, 0x00, 0x00, 0x20, 0x00, 0x04, 0x00, 0x14, 0x00, 0x00, 0x00,
  226. 0x03, 0x00, 0x00, 0x00, 0x07, 0x00, 0x00, 0x00, 0x3b, 0x00, 0x04, 0x00,
  227. 0x14, 0x00, 0x00, 0x00, 0x15, 0x00, 0x00, 0x00, 0x03, 0x00, 0x00, 0x00,
  228. 0x20, 0x00, 0x04, 0x00, 0x16, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00,
  229. 0x07, 0x00, 0x00, 0x00, 0x3b, 0x00, 0x04, 0x00, 0x16, 0x00, 0x00, 0x00,
  230. 0x17, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x36, 0x00, 0x05, 0x00,
  231. 0x02, 0x00, 0x00, 0x00, 0x04, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  232. 0x03, 0x00, 0x00, 0x00, 0xf8, 0x00, 0x02, 0x00, 0x05, 0x00, 0x00, 0x00,
  233. 0x3b, 0x00, 0x04, 0x00, 0x08, 0x00, 0x00, 0x00, 0x09, 0x00, 0x00, 0x00,
  234. 0x07, 0x00, 0x00, 0x00, 0x3d, 0x00, 0x04, 0x00, 0x0b, 0x00, 0x00, 0x00,
  235. 0x0e, 0x00, 0x00, 0x00, 0x0d, 0x00, 0x00, 0x00, 0x3d, 0x00, 0x04, 0x00,
  236. 0x0f, 0x00, 0x00, 0x00, 0x12, 0x00, 0x00, 0x00, 0x11, 0x00, 0x00, 0x00,
  237. 0x57, 0x00, 0x05, 0x00, 0x07, 0x00, 0x00, 0x00, 0x13, 0x00, 0x00, 0x00,
  238. 0x0e, 0x00, 0x00, 0x00, 0x12, 0x00, 0x00, 0x00, 0x3e, 0x00, 0x03, 0x00,
  239. 0x09, 0x00, 0x00, 0x00, 0x13, 0x00, 0x00, 0x00, 0x3d, 0x00, 0x04, 0x00,
  240. 0x07, 0x00, 0x00, 0x00, 0x18, 0x00, 0x00, 0x00, 0x17, 0x00, 0x00, 0x00,
  241. 0x3d, 0x00, 0x04, 0x00, 0x07, 0x00, 0x00, 0x00, 0x19, 0x00, 0x00, 0x00,
  242. 0x09, 0x00, 0x00, 0x00, 0x85, 0x00, 0x05, 0x00, 0x07, 0x00, 0x00, 0x00,
  243. 0x1a, 0x00, 0x00, 0x00, 0x18, 0x00, 0x00, 0x00, 0x19, 0x00, 0x00, 0x00,
  244. 0x3e, 0x00, 0x03, 0x00, 0x15, 0x00, 0x00, 0x00, 0x1a, 0x00, 0x00, 0x00,
  245. 0xfd, 0x00, 0x01, 0x00, 0x38, 0x00, 0x01, 0x00
  246. };
  247. unsigned int nuklearshaders_nuklear_frag_spv_len = 860;
  248. #include <assert.h>
  249. #include <stddef.h>
  250. #include <string.h>
  251. #include <SDL2/SDL.h>
  252. enum nk_sdl_init_state { NK_SDL_DEFAULT = 0 };
  253. NK_API struct nk_context *
  254. nk_sdl_init(SDL_Window *win, VkDevice logical_device,
  255. VkPhysicalDevice physical_device,
  256. uint32_t graphics_queue_family_index, VkImageView *image_views,
  257. uint32_t image_views_len, VkFormat color_format,
  258. enum nk_sdl_init_state init_state, VkDeviceSize max_vertex_buffer,
  259. VkDeviceSize max_element_buffer);
  260. NK_API void nk_sdl_shutdown(void);
  261. NK_API void nk_sdl_font_stash_begin(struct nk_font_atlas **atlas);
  262. NK_API void nk_sdl_font_stash_end(VkQueue graphics_queue);
  263. NK_API int nk_sdl_handle_event(SDL_Event *evt);
  264. NK_API VkSemaphore nk_sdl_render(VkQueue graphics_queue, uint32_t buffer_index,
  265. VkSemaphore wait_semaphore,
  266. enum nk_anti_aliasing AA);
  267. NK_API void nk_sdl_resize(uint32_t framebuffer_width,
  268. uint32_t framebuffer_height);
  269. NK_API void nk_sdl_device_destroy(void);
  270. NK_API void
  271. nk_sdl_device_create(VkDevice logical_device, VkPhysicalDevice physical_device,
  272. uint32_t graphics_queue_family_index,
  273. VkImageView *image_views, uint32_t image_views_len,
  274. VkFormat color_format, VkDeviceSize max_vertex_buffer,
  275. VkDeviceSize max_element_buffer,
  276. uint32_t framebuffer_width, uint32_t framebuffer_height);
  277. NK_API void nk_sdl_handle_grab(void);
  278. #endif
  279. /*
  280. * ==============================================================
  281. *
  282. * IMPLEMENTATION
  283. *
  284. * ===============================================================
  285. */
  286. #ifdef NK_SDL_VULKAN_IMPLEMENTATION
  287. #undef NK_SDL_VULKAN_IMPLEMENTATION
  288. #include <stdlib.h>
  289. #ifndef NK_SDL_TEXT_MAX
  290. #define NK_SDL_TEXT_MAX 256
  291. #endif
  292. #ifndef NK_SDL_MAX_TEXTURES
  293. #define NK_SDL_MAX_TEXTURES 256
  294. #endif
  295. #ifndef NK_SDL_MAX_KEYS
  296. #define NK_SDL_MAX_KEYS 32
  297. #endif
  298. #define VK_COLOR_COMPONENT_MASK_RGBA \
  299. VK_COLOR_COMPONENT_R_BIT | VK_COLOR_COMPONENT_G_BIT | \
  300. VK_COLOR_COMPONENT_B_BIT | VK_COLOR_COMPONENT_A_BIT
  301. struct nk_sdl_vertex {
  302. float position[2];
  303. float uv[2];
  304. nk_byte col[4];
  305. };
  306. struct nk_vulkan_texture_descriptor_set {
  307. VkImageView image_view;
  308. VkDescriptorSet descriptor_set;
  309. };
  310. struct nk_sdl_device {
  311. struct nk_buffer cmds;
  312. struct nk_draw_null_texture tex_null;
  313. int max_vertex_buffer;
  314. int max_element_buffer;
  315. VkDevice logical_device;
  316. VkPhysicalDevice physical_device;
  317. VkImageView *image_views;
  318. uint32_t image_views_len;
  319. VkFormat color_format;
  320. VkFramebuffer *framebuffers;
  321. uint32_t framebuffers_len;
  322. VkCommandBuffer *command_buffers;
  323. uint32_t command_buffers_len;
  324. VkSampler sampler;
  325. VkCommandPool command_pool;
  326. VkSemaphore render_completed;
  327. VkBuffer vertex_buffer;
  328. VkDeviceMemory vertex_memory;
  329. void *mapped_vertex;
  330. VkBuffer index_buffer;
  331. VkDeviceMemory index_memory;
  332. void *mapped_index;
  333. VkBuffer uniform_buffer;
  334. VkDeviceMemory uniform_memory;
  335. void *mapped_uniform;
  336. VkRenderPass render_pass;
  337. VkDescriptorPool descriptor_pool;
  338. VkDescriptorSetLayout uniform_descriptor_set_layout;
  339. VkDescriptorSet uniform_descriptor_set;
  340. VkDescriptorSetLayout texture_descriptor_set_layout;
  341. struct nk_vulkan_texture_descriptor_set *texture_descriptor_sets;
  342. uint32_t texture_descriptor_sets_len;
  343. VkPipelineLayout pipeline_layout;
  344. VkPipeline pipeline;
  345. VkImage font_image;
  346. VkImageView font_image_view;
  347. VkDeviceMemory font_memory;
  348. };
  349. static struct nk_sdl {
  350. SDL_Window *win;
  351. int width, height;
  352. int display_width, display_height;
  353. struct nk_sdl_device vulkan;
  354. struct nk_context ctx;
  355. struct nk_font_atlas atlas;
  356. unsigned int text[NK_SDL_TEXT_MAX];
  357. int text_len;
  358. struct nk_vec2 scroll;
  359. uint64_t delta_time_milliseconds_last;
  360. } sdl;
  361. struct Mat4f {
  362. float m[16];
  363. };
  364. NK_INTERN uint32_t nk_sdl_find_memory_index(VkPhysicalDevice physical_device,
  365. uint32_t type_filter,
  366. VkMemoryPropertyFlags properties) {
  367. VkPhysicalDeviceMemoryProperties mem_properties;
  368. uint32_t i;
  369. vkGetPhysicalDeviceMemoryProperties(physical_device, &mem_properties);
  370. for (i = 0; i < mem_properties.memoryTypeCount; i++) {
  371. if ((type_filter & (1 << i)) &&
  372. (mem_properties.memoryTypes[i].propertyFlags & properties) ==
  373. properties) {
  374. return i;
  375. }
  376. }
  377. assert(0);
  378. return 0;
  379. }
  380. NK_INTERN void nk_sdl_create_sampler(struct nk_sdl_device *dev) {
  381. VkResult result;
  382. VkSamplerCreateInfo sampler_info;
  383. memset(&sampler_info, 0, sizeof(VkSamplerCreateInfo));
  384. sampler_info.sType = VK_STRUCTURE_TYPE_SAMPLER_CREATE_INFO;
  385. sampler_info.pNext = NULL;
  386. sampler_info.maxAnisotropy = 1.0;
  387. sampler_info.magFilter = VK_FILTER_LINEAR;
  388. sampler_info.minFilter = VK_FILTER_LINEAR;
  389. sampler_info.mipmapMode = VK_SAMPLER_MIPMAP_MODE_LINEAR;
  390. sampler_info.addressModeU = VK_SAMPLER_ADDRESS_MODE_REPEAT;
  391. sampler_info.addressModeV = VK_SAMPLER_ADDRESS_MODE_REPEAT;
  392. sampler_info.addressModeW = VK_SAMPLER_ADDRESS_MODE_REPEAT;
  393. sampler_info.mipLodBias = 0.0f;
  394. sampler_info.compareEnable = VK_FALSE;
  395. sampler_info.compareOp = VK_COMPARE_OP_ALWAYS;
  396. sampler_info.minLod = 0.0f;
  397. sampler_info.maxLod = 0.0f;
  398. sampler_info.borderColor = VK_BORDER_COLOR_FLOAT_OPAQUE_BLACK;
  399. result = vkCreateSampler(dev->logical_device, &sampler_info, NULL,
  400. &dev->sampler);
  401. NK_ASSERT(result == VK_SUCCESS);
  402. }
  403. NK_INTERN void
  404. nk_sdl_create_command_pool(struct nk_sdl_device *dev,
  405. uint32_t graphics_queue_family_index) {
  406. VkResult result;
  407. VkCommandPoolCreateInfo pool_info;
  408. memset(&pool_info, 0, sizeof(VkCommandPoolCreateInfo));
  409. pool_info.sType = VK_STRUCTURE_TYPE_COMMAND_POOL_CREATE_INFO;
  410. pool_info.queueFamilyIndex = graphics_queue_family_index;
  411. pool_info.flags = VK_COMMAND_POOL_CREATE_RESET_COMMAND_BUFFER_BIT;
  412. result = vkCreateCommandPool(dev->logical_device, &pool_info, NULL,
  413. &dev->command_pool);
  414. NK_ASSERT(result == VK_SUCCESS);
  415. }
  416. NK_INTERN void nk_sdl_create_command_buffers(struct nk_sdl_device *dev) {
  417. VkResult result;
  418. VkCommandBufferAllocateInfo allocate_info;
  419. memset(&allocate_info, 0, sizeof(VkCommandBufferAllocateInfo));
  420. dev->command_buffers = (VkCommandBuffer *)malloc(dev->image_views_len *
  421. sizeof(VkCommandBuffer));
  422. dev->command_buffers_len = dev->image_views_len;
  423. allocate_info.sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_ALLOCATE_INFO;
  424. allocate_info.commandPool = dev->command_pool;
  425. allocate_info.level = VK_COMMAND_BUFFER_LEVEL_PRIMARY;
  426. allocate_info.commandBufferCount = dev->command_buffers_len;
  427. result = vkAllocateCommandBuffers(dev->logical_device, &allocate_info,
  428. dev->command_buffers);
  429. NK_ASSERT(result == VK_SUCCESS);
  430. }
  431. NK_INTERN void nk_sdl_create_semaphore(struct nk_sdl_device *dev) {
  432. VkResult result;
  433. VkSemaphoreCreateInfo semaphore_info;
  434. memset(&semaphore_info, 0, sizeof(VkSemaphoreCreateInfo));
  435. semaphore_info.sType = VK_STRUCTURE_TYPE_SEMAPHORE_CREATE_INFO;
  436. result = (vkCreateSemaphore(dev->logical_device, &semaphore_info, NULL,
  437. &dev->render_completed));
  438. NK_ASSERT(result == VK_SUCCESS);
  439. }
  440. NK_INTERN void nk_sdl_create_buffer_and_memory(struct nk_sdl_device *dev,
  441. VkBuffer *buffer,
  442. VkBufferUsageFlags usage,
  443. VkDeviceMemory *memory,
  444. VkDeviceSize size) {
  445. VkMemoryRequirements mem_reqs;
  446. VkResult result;
  447. VkBufferCreateInfo buffer_info;
  448. VkMemoryAllocateInfo alloc_info;
  449. memset(&buffer_info, 0, sizeof(VkBufferCreateInfo));
  450. buffer_info.sType = VK_STRUCTURE_TYPE_BUFFER_CREATE_INFO;
  451. buffer_info.size = size;
  452. buffer_info.usage = usage;
  453. buffer_info.sharingMode = VK_SHARING_MODE_EXCLUSIVE;
  454. result = vkCreateBuffer(dev->logical_device, &buffer_info, NULL, buffer);
  455. NK_ASSERT(result == VK_SUCCESS);
  456. vkGetBufferMemoryRequirements(dev->logical_device, *buffer, &mem_reqs);
  457. memset(&alloc_info, 0, sizeof(VkMemoryAllocateInfo));
  458. alloc_info.sType = VK_STRUCTURE_TYPE_MEMORY_ALLOCATE_INFO;
  459. alloc_info.allocationSize = mem_reqs.size;
  460. alloc_info.memoryTypeIndex =
  461. nk_sdl_find_memory_index(dev->physical_device, mem_reqs.memoryTypeBits,
  462. VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT |
  463. VK_MEMORY_PROPERTY_HOST_COHERENT_BIT);
  464. result = vkAllocateMemory(dev->logical_device, &alloc_info, NULL, memory);
  465. NK_ASSERT(result == VK_SUCCESS);
  466. result = vkBindBufferMemory(dev->logical_device, *buffer, *memory, 0);
  467. NK_ASSERT(result == VK_SUCCESS);
  468. }
  469. NK_INTERN void nk_sdl_create_render_pass(struct nk_sdl_device *dev) {
  470. VkAttachmentDescription attachment;
  471. VkAttachmentReference color_reference;
  472. VkSubpassDependency subpass_dependency;
  473. VkSubpassDescription subpass_description;
  474. VkRenderPassCreateInfo render_pass_info;
  475. VkResult result;
  476. memset(&attachment, 0, sizeof(VkAttachmentDescription));
  477. attachment.format = dev->color_format;
  478. attachment.samples = VK_SAMPLE_COUNT_1_BIT;
  479. attachment.loadOp = VK_ATTACHMENT_LOAD_OP_CLEAR;
  480. attachment.storeOp = VK_ATTACHMENT_STORE_OP_STORE;
  481. attachment.stencilLoadOp = VK_ATTACHMENT_LOAD_OP_DONT_CARE;
  482. attachment.stencilStoreOp = VK_ATTACHMENT_STORE_OP_DONT_CARE;
  483. attachment.initialLayout = VK_IMAGE_LAYOUT_UNDEFINED;
  484. attachment.finalLayout = VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL;
  485. memset(&color_reference, 0, sizeof(VkAttachmentReference));
  486. color_reference.attachment = 0;
  487. color_reference.layout = VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL;
  488. memset(&subpass_dependency, 0, sizeof(VkSubpassDependency));
  489. subpass_dependency.srcSubpass = VK_SUBPASS_EXTERNAL;
  490. subpass_dependency.srcAccessMask = 0;
  491. subpass_dependency.srcStageMask =
  492. VK_PIPELINE_STAGE_COLOR_ATTACHMENT_OUTPUT_BIT;
  493. subpass_dependency.dstSubpass = 0;
  494. subpass_dependency.dstAccessMask = VK_ACCESS_COLOR_ATTACHMENT_READ_BIT |
  495. VK_ACCESS_COLOR_ATTACHMENT_WRITE_BIT;
  496. subpass_dependency.dstStageMask =
  497. VK_PIPELINE_STAGE_COLOR_ATTACHMENT_OUTPUT_BIT;
  498. memset(&subpass_description, 0, sizeof(VkSubpassDescription));
  499. subpass_description.pipelineBindPoint = VK_PIPELINE_BIND_POINT_GRAPHICS;
  500. subpass_description.colorAttachmentCount = 1;
  501. subpass_description.pColorAttachments = &color_reference;
  502. memset(&render_pass_info, 0, sizeof(VkRenderPassCreateInfo));
  503. render_pass_info.sType = VK_STRUCTURE_TYPE_RENDER_PASS_CREATE_INFO;
  504. render_pass_info.attachmentCount = 1;
  505. render_pass_info.pAttachments = &attachment;
  506. render_pass_info.subpassCount = 1;
  507. render_pass_info.pSubpasses = &subpass_description;
  508. render_pass_info.dependencyCount = 1;
  509. render_pass_info.pDependencies = &subpass_dependency;
  510. result = vkCreateRenderPass(dev->logical_device, &render_pass_info, NULL,
  511. &dev->render_pass);
  512. NK_ASSERT(result == VK_SUCCESS);
  513. }
  514. NK_INTERN void nk_sdl_create_framebuffers(struct nk_sdl_device *dev,
  515. uint32_t framebuffer_width,
  516. uint32_t framebuffer_height) {
  517. VkFramebufferCreateInfo framebuffer_create_info;
  518. uint32_t i;
  519. VkResult result;
  520. dev->framebuffers =
  521. (VkFramebuffer *)malloc(dev->image_views_len * sizeof(VkFramebuffer));
  522. memset(&framebuffer_create_info, 0, sizeof(VkFramebufferCreateInfo));
  523. framebuffer_create_info.sType = VK_STRUCTURE_TYPE_FRAMEBUFFER_CREATE_INFO;
  524. framebuffer_create_info.renderPass = dev->render_pass;
  525. framebuffer_create_info.attachmentCount = 1;
  526. framebuffer_create_info.width = framebuffer_width;
  527. framebuffer_create_info.height = framebuffer_height;
  528. framebuffer_create_info.layers = 1;
  529. for (i = 0; i < dev->image_views_len; i++) {
  530. framebuffer_create_info.pAttachments = &dev->image_views[i];
  531. result =
  532. vkCreateFramebuffer(dev->logical_device, &framebuffer_create_info,
  533. NULL, &dev->framebuffers[i]);
  534. NK_ASSERT(result == VK_SUCCESS);
  535. }
  536. dev->framebuffers_len = dev->image_views_len;
  537. }
  538. NK_INTERN void nk_sdl_create_descriptor_pool(struct nk_sdl_device *dev) {
  539. VkDescriptorPoolSize pool_sizes[2];
  540. VkDescriptorPoolCreateInfo pool_info;
  541. VkResult result;
  542. memset(&pool_sizes, 0, sizeof(VkDescriptorPoolSize) * 2);
  543. pool_sizes[0].type = VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER;
  544. pool_sizes[0].descriptorCount = 1;
  545. pool_sizes[1].type = VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER;
  546. pool_sizes[1].descriptorCount = NK_SDL_MAX_TEXTURES;
  547. memset(&pool_info, 0, sizeof(VkDescriptorPoolCreateInfo));
  548. pool_info.sType = VK_STRUCTURE_TYPE_DESCRIPTOR_POOL_CREATE_INFO;
  549. pool_info.poolSizeCount = 2;
  550. pool_info.pPoolSizes = pool_sizes;
  551. pool_info.maxSets = 1 + NK_SDL_MAX_TEXTURES;
  552. result = vkCreateDescriptorPool(dev->logical_device, &pool_info, NULL,
  553. &dev->descriptor_pool);
  554. NK_ASSERT(result == VK_SUCCESS);
  555. }
  556. NK_INTERN void
  557. nk_sdl_create_uniform_descriptor_set_layout(struct nk_sdl_device *dev) {
  558. VkDescriptorSetLayoutBinding binding;
  559. VkDescriptorSetLayoutCreateInfo descriptor_set_info;
  560. VkResult result;
  561. memset(&binding, 0, sizeof(VkDescriptorSetLayoutBinding));
  562. binding.binding = 0;
  563. binding.descriptorType = VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER;
  564. binding.descriptorCount = 1;
  565. binding.stageFlags = VK_SHADER_STAGE_VERTEX_BIT;
  566. memset(&descriptor_set_info, 0, sizeof(VkDescriptorSetLayoutCreateInfo));
  567. descriptor_set_info.sType =
  568. VK_STRUCTURE_TYPE_DESCRIPTOR_SET_LAYOUT_CREATE_INFO;
  569. descriptor_set_info.bindingCount = 1;
  570. descriptor_set_info.pBindings = &binding;
  571. result =
  572. vkCreateDescriptorSetLayout(dev->logical_device, &descriptor_set_info,
  573. NULL, &dev->uniform_descriptor_set_layout);
  574. NK_ASSERT(result == VK_SUCCESS);
  575. }
  576. NK_INTERN void
  577. nk_sdl_create_and_update_uniform_descriptor_set(struct nk_sdl_device *dev) {
  578. VkDescriptorSetAllocateInfo allocate_info;
  579. VkDescriptorBufferInfo buffer_info;
  580. VkWriteDescriptorSet descriptor_write;
  581. VkResult result;
  582. memset(&allocate_info, 0, sizeof(VkDescriptorSetAllocateInfo));
  583. allocate_info.sType = VK_STRUCTURE_TYPE_DESCRIPTOR_SET_ALLOCATE_INFO;
  584. allocate_info.descriptorPool = dev->descriptor_pool;
  585. allocate_info.descriptorSetCount = 1;
  586. allocate_info.pSetLayouts = &dev->uniform_descriptor_set_layout;
  587. result = vkAllocateDescriptorSets(dev->logical_device, &allocate_info,
  588. &dev->uniform_descriptor_set);
  589. NK_ASSERT(result == VK_SUCCESS);
  590. memset(&buffer_info, 0, sizeof(VkDescriptorBufferInfo));
  591. buffer_info.buffer = dev->uniform_buffer;
  592. buffer_info.offset = 0;
  593. buffer_info.range = sizeof(struct Mat4f);
  594. memset(&descriptor_write, 0, sizeof(VkWriteDescriptorSet));
  595. descriptor_write.sType = VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET;
  596. descriptor_write.dstSet = dev->uniform_descriptor_set;
  597. descriptor_write.dstBinding = 0;
  598. descriptor_write.dstArrayElement = 0;
  599. descriptor_write.descriptorType = VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER;
  600. descriptor_write.descriptorCount = 1;
  601. descriptor_write.pBufferInfo = &buffer_info;
  602. vkUpdateDescriptorSets(dev->logical_device, 1, &descriptor_write, 0, NULL);
  603. }
  604. NK_INTERN void
  605. nk_sdl_create_texture_descriptor_set_layout(struct nk_sdl_device *dev) {
  606. VkDescriptorSetLayoutBinding binding;
  607. VkDescriptorSetLayoutCreateInfo descriptor_set_info;
  608. VkResult result;
  609. memset(&binding, 0, sizeof(VkDescriptorSetLayoutBinding));
  610. binding.binding = 0;
  611. binding.descriptorType = VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER;
  612. binding.descriptorCount = 1;
  613. binding.stageFlags = VK_SHADER_STAGE_FRAGMENT_BIT;
  614. memset(&descriptor_set_info, 0, sizeof(VkDescriptorSetLayoutCreateInfo));
  615. descriptor_set_info.sType =
  616. VK_STRUCTURE_TYPE_DESCRIPTOR_SET_LAYOUT_CREATE_INFO;
  617. descriptor_set_info.bindingCount = 1;
  618. descriptor_set_info.pBindings = &binding;
  619. result =
  620. vkCreateDescriptorSetLayout(dev->logical_device, &descriptor_set_info,
  621. NULL, &dev->texture_descriptor_set_layout);
  622. NK_ASSERT(result == VK_SUCCESS);
  623. }
  624. NK_INTERN void
  625. nk_sdl_create_texture_descriptor_sets(struct nk_sdl_device *dev) {
  626. VkDescriptorSetLayout *descriptor_set_layouts;
  627. VkDescriptorSet *descriptor_sets;
  628. VkDescriptorSetAllocateInfo allocate_info;
  629. VkResult result;
  630. int i;
  631. descriptor_set_layouts = (VkDescriptorSetLayout *)malloc(
  632. NK_SDL_MAX_TEXTURES * sizeof(VkDescriptorSetLayout));
  633. descriptor_sets = (VkDescriptorSet *)malloc(NK_SDL_MAX_TEXTURES *
  634. sizeof(VkDescriptorSet));
  635. dev->texture_descriptor_sets =
  636. (struct nk_vulkan_texture_descriptor_set *)malloc(
  637. NK_SDL_MAX_TEXTURES *
  638. sizeof(struct nk_vulkan_texture_descriptor_set));
  639. dev->texture_descriptor_sets_len = 0;
  640. for (i = 0; i < NK_SDL_MAX_TEXTURES; i++) {
  641. descriptor_set_layouts[i] = dev->texture_descriptor_set_layout;
  642. descriptor_sets[i] = dev->texture_descriptor_sets[i].descriptor_set;
  643. }
  644. memset(&allocate_info, 0, sizeof(VkDescriptorSetAllocateInfo));
  645. allocate_info.sType = VK_STRUCTURE_TYPE_DESCRIPTOR_SET_ALLOCATE_INFO;
  646. allocate_info.descriptorPool = dev->descriptor_pool;
  647. allocate_info.descriptorSetCount = NK_SDL_MAX_TEXTURES;
  648. allocate_info.pSetLayouts = descriptor_set_layouts;
  649. result = vkAllocateDescriptorSets(dev->logical_device, &allocate_info,
  650. descriptor_sets);
  651. NK_ASSERT(result == VK_SUCCESS);
  652. for (i = 0; i < NK_SDL_MAX_TEXTURES; i++) {
  653. dev->texture_descriptor_sets[i].descriptor_set = descriptor_sets[i];
  654. }
  655. free(descriptor_set_layouts);
  656. free(descriptor_sets);
  657. }
  658. NK_INTERN void nk_sdl_create_pipeline_layout(struct nk_sdl_device *dev) {
  659. VkPipelineLayoutCreateInfo pipeline_layout_info;
  660. VkDescriptorSetLayout descriptor_set_layouts[2];
  661. VkResult result;
  662. descriptor_set_layouts[0] = dev->uniform_descriptor_set_layout;
  663. descriptor_set_layouts[1] = dev->texture_descriptor_set_layout;
  664. memset(&pipeline_layout_info, 0, sizeof(VkPipelineLayoutCreateInfo));
  665. pipeline_layout_info.sType = VK_STRUCTURE_TYPE_PIPELINE_LAYOUT_CREATE_INFO;
  666. pipeline_layout_info.setLayoutCount = 2;
  667. pipeline_layout_info.pSetLayouts = descriptor_set_layouts;
  668. result = (vkCreatePipelineLayout(dev->logical_device, &pipeline_layout_info,
  669. NULL, &dev->pipeline_layout));
  670. NK_ASSERT(result == VK_SUCCESS);
  671. }
  672. NK_INTERN VkPipelineShaderStageCreateInfo
  673. nk_sdl_create_shader(struct nk_sdl_device *dev, unsigned char *spv_shader,
  674. uint32_t size, VkShaderStageFlagBits stage_bit) {
  675. VkShaderModuleCreateInfo create_info;
  676. VkPipelineShaderStageCreateInfo shader_info;
  677. VkShaderModule module = NULL;
  678. VkResult result;
  679. memset(&create_info, 0, sizeof(VkShaderModuleCreateInfo));
  680. create_info.sType = VK_STRUCTURE_TYPE_SHADER_MODULE_CREATE_INFO;
  681. create_info.codeSize = size;
  682. create_info.pCode = (const uint32_t *)spv_shader;
  683. result =
  684. vkCreateShaderModule(dev->logical_device, &create_info, NULL, &module);
  685. NK_ASSERT(result == VK_SUCCESS);
  686. memset(&shader_info, 0, sizeof(VkPipelineShaderStageCreateInfo));
  687. shader_info.sType = VK_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO;
  688. shader_info.stage = stage_bit;
  689. shader_info.module = module;
  690. shader_info.pName = "main";
  691. return shader_info;
  692. }
  693. NK_INTERN void nk_sdl_create_pipeline(struct nk_sdl_device *dev) {
  694. VkPipelineInputAssemblyStateCreateInfo input_assembly_state;
  695. VkPipelineRasterizationStateCreateInfo rasterization_state;
  696. VkPipelineColorBlendAttachmentState attachment_state = {
  697. VK_TRUE,
  698. VK_BLEND_FACTOR_SRC_ALPHA,
  699. VK_BLEND_FACTOR_ONE_MINUS_SRC_ALPHA,
  700. VK_BLEND_OP_ADD,
  701. VK_BLEND_FACTOR_SRC_ALPHA,
  702. VK_BLEND_FACTOR_ONE,
  703. VK_BLEND_OP_ADD,
  704. VK_COLOR_COMPONENT_MASK_RGBA,
  705. };
  706. VkPipelineColorBlendStateCreateInfo color_blend_state;
  707. VkPipelineViewportStateCreateInfo viewport_state;
  708. VkPipelineMultisampleStateCreateInfo multisample_state;
  709. VkDynamicState dynamic_states[2] = {VK_DYNAMIC_STATE_VIEWPORT,
  710. VK_DYNAMIC_STATE_SCISSOR};
  711. VkPipelineDynamicStateCreateInfo dynamic_state;
  712. VkPipelineShaderStageCreateInfo shader_stages[2];
  713. VkVertexInputBindingDescription vertex_input_info;
  714. VkVertexInputAttributeDescription vertex_attribute_description[3];
  715. VkPipelineVertexInputStateCreateInfo vertex_input;
  716. VkGraphicsPipelineCreateInfo pipeline_info;
  717. VkResult result;
  718. memset(&input_assembly_state, 0,
  719. sizeof(VkPipelineInputAssemblyStateCreateInfo));
  720. input_assembly_state.sType =
  721. VK_STRUCTURE_TYPE_PIPELINE_INPUT_ASSEMBLY_STATE_CREATE_INFO;
  722. input_assembly_state.topology = VK_PRIMITIVE_TOPOLOGY_TRIANGLE_LIST;
  723. input_assembly_state.primitiveRestartEnable = VK_FALSE;
  724. memset(&rasterization_state, 0,
  725. sizeof(VkPipelineRasterizationStateCreateInfo));
  726. rasterization_state.sType =
  727. VK_STRUCTURE_TYPE_PIPELINE_RASTERIZATION_STATE_CREATE_INFO;
  728. rasterization_state.polygonMode = VK_POLYGON_MODE_FILL;
  729. rasterization_state.cullMode = VK_CULL_MODE_NONE;
  730. rasterization_state.frontFace = VK_FRONT_FACE_COUNTER_CLOCKWISE;
  731. rasterization_state.lineWidth = 1.0f;
  732. memset(&color_blend_state, 0, sizeof(VkPipelineColorBlendStateCreateInfo));
  733. color_blend_state.sType =
  734. VK_STRUCTURE_TYPE_PIPELINE_COLOR_BLEND_STATE_CREATE_INFO;
  735. color_blend_state.attachmentCount = 1;
  736. color_blend_state.pAttachments = &attachment_state;
  737. memset(&viewport_state, 0, sizeof(VkPipelineViewportStateCreateInfo));
  738. viewport_state.sType =
  739. VK_STRUCTURE_TYPE_PIPELINE_VIEWPORT_STATE_CREATE_INFO;
  740. viewport_state.viewportCount = 1;
  741. viewport_state.scissorCount = 1;
  742. memset(&multisample_state, 0, sizeof(VkPipelineMultisampleStateCreateInfo));
  743. multisample_state.sType =
  744. VK_STRUCTURE_TYPE_PIPELINE_MULTISAMPLE_STATE_CREATE_INFO;
  745. multisample_state.rasterizationSamples = VK_SAMPLE_COUNT_1_BIT;
  746. memset(&dynamic_state, 0, sizeof(VkPipelineDynamicStateCreateInfo));
  747. dynamic_state.sType = VK_STRUCTURE_TYPE_PIPELINE_DYNAMIC_STATE_CREATE_INFO;
  748. dynamic_state.pDynamicStates = dynamic_states;
  749. dynamic_state.dynamicStateCount = 2;
  750. shader_stages[0] = nk_sdl_create_shader(
  751. dev, nuklearshaders_nuklear_vert_spv,
  752. nuklearshaders_nuklear_vert_spv_len, VK_SHADER_STAGE_VERTEX_BIT);
  753. shader_stages[1] = nk_sdl_create_shader(
  754. dev, nuklearshaders_nuklear_frag_spv,
  755. nuklearshaders_nuklear_frag_spv_len, VK_SHADER_STAGE_FRAGMENT_BIT);
  756. memset(&vertex_input_info, 0, sizeof(VkVertexInputBindingDescription));
  757. vertex_input_info.binding = 0;
  758. vertex_input_info.stride = sizeof(struct nk_sdl_vertex);
  759. vertex_input_info.inputRate = VK_VERTEX_INPUT_RATE_VERTEX;
  760. memset(&vertex_attribute_description, 0,
  761. sizeof(VkVertexInputAttributeDescription) * 3);
  762. vertex_attribute_description[0].location = 0;
  763. vertex_attribute_description[0].format = VK_FORMAT_R32G32_SFLOAT;
  764. vertex_attribute_description[0].offset =
  765. NK_OFFSETOF(struct nk_sdl_vertex, position);
  766. vertex_attribute_description[1].location = 1;
  767. vertex_attribute_description[1].format = VK_FORMAT_R32G32_SFLOAT;
  768. vertex_attribute_description[1].offset =
  769. NK_OFFSETOF(struct nk_sdl_vertex, uv);
  770. vertex_attribute_description[2].location = 2;
  771. vertex_attribute_description[2].format = VK_FORMAT_R8G8B8A8_UINT;
  772. vertex_attribute_description[2].offset =
  773. NK_OFFSETOF(struct nk_sdl_vertex, col);
  774. memset(&vertex_input, 0, sizeof(VkPipelineVertexInputStateCreateInfo));
  775. vertex_input.sType =
  776. VK_STRUCTURE_TYPE_PIPELINE_VERTEX_INPUT_STATE_CREATE_INFO;
  777. vertex_input.vertexBindingDescriptionCount = 1;
  778. vertex_input.pVertexBindingDescriptions = &vertex_input_info;
  779. vertex_input.vertexAttributeDescriptionCount = 3;
  780. vertex_input.pVertexAttributeDescriptions = vertex_attribute_description;
  781. memset(&pipeline_info, 0, sizeof(VkGraphicsPipelineCreateInfo));
  782. pipeline_info.sType = VK_STRUCTURE_TYPE_GRAPHICS_PIPELINE_CREATE_INFO;
  783. pipeline_info.flags = 0;
  784. pipeline_info.stageCount = 2;
  785. pipeline_info.pStages = shader_stages;
  786. pipeline_info.pVertexInputState = &vertex_input;
  787. pipeline_info.pInputAssemblyState = &input_assembly_state;
  788. pipeline_info.pViewportState = &viewport_state;
  789. pipeline_info.pRasterizationState = &rasterization_state;
  790. pipeline_info.pMultisampleState = &multisample_state;
  791. pipeline_info.pColorBlendState = &color_blend_state;
  792. pipeline_info.pDynamicState = &dynamic_state;
  793. pipeline_info.layout = dev->pipeline_layout;
  794. pipeline_info.renderPass = dev->render_pass;
  795. pipeline_info.basePipelineIndex = -1;
  796. pipeline_info.basePipelineHandle = NULL;
  797. result = vkCreateGraphicsPipelines(dev->logical_device, NULL, 1,
  798. &pipeline_info, NULL, &dev->pipeline);
  799. NK_ASSERT(result == VK_SUCCESS);
  800. vkDestroyShaderModule(dev->logical_device, shader_stages[0].module, NULL);
  801. vkDestroyShaderModule(dev->logical_device, shader_stages[1].module, NULL);
  802. }
  803. NK_INTERN void nk_sdl_create_render_resources(struct nk_sdl_device *dev,
  804. uint32_t framebuffer_width,
  805. uint32_t framebuffer_height) {
  806. nk_sdl_create_render_pass(dev);
  807. nk_sdl_create_framebuffers(dev, framebuffer_width, framebuffer_height);
  808. nk_sdl_create_descriptor_pool(dev);
  809. nk_sdl_create_uniform_descriptor_set_layout(dev);
  810. nk_sdl_create_and_update_uniform_descriptor_set(dev);
  811. nk_sdl_create_texture_descriptor_set_layout(dev);
  812. nk_sdl_create_texture_descriptor_sets(dev);
  813. nk_sdl_create_pipeline_layout(dev);
  814. nk_sdl_create_pipeline(dev);
  815. }
  816. NK_API void
  817. nk_sdl_device_create(VkDevice logical_device, VkPhysicalDevice physical_device,
  818. uint32_t graphics_queue_family_index,
  819. VkImageView *image_views, uint32_t image_views_len,
  820. VkFormat color_format, VkDeviceSize max_vertex_buffer,
  821. VkDeviceSize max_element_buffer,
  822. uint32_t framebuffer_width, uint32_t framebuffer_height) {
  823. struct nk_sdl_device *dev = &sdl.vulkan;
  824. dev->max_vertex_buffer = max_vertex_buffer;
  825. dev->max_element_buffer = max_element_buffer;
  826. nk_buffer_init_default(&dev->cmds);
  827. dev->logical_device = logical_device;
  828. dev->physical_device = physical_device;
  829. dev->image_views = image_views;
  830. dev->image_views_len = image_views_len;
  831. dev->color_format = color_format;
  832. dev->framebuffers = NULL;
  833. dev->framebuffers_len = 0;
  834. nk_sdl_create_sampler(dev);
  835. nk_sdl_create_command_pool(dev, graphics_queue_family_index);
  836. nk_sdl_create_command_buffers(dev);
  837. nk_sdl_create_semaphore(dev);
  838. nk_sdl_create_buffer_and_memory(dev, &dev->vertex_buffer,
  839. VK_BUFFER_USAGE_VERTEX_BUFFER_BIT,
  840. &dev->vertex_memory, max_vertex_buffer);
  841. nk_sdl_create_buffer_and_memory(dev, &dev->index_buffer,
  842. VK_BUFFER_USAGE_INDEX_BUFFER_BIT,
  843. &dev->index_memory, max_element_buffer);
  844. nk_sdl_create_buffer_and_memory(dev, &dev->uniform_buffer,
  845. VK_BUFFER_USAGE_UNIFORM_BUFFER_BIT,
  846. &dev->uniform_memory, sizeof(struct Mat4f));
  847. vkMapMemory(dev->logical_device, dev->vertex_memory, 0, max_vertex_buffer,
  848. 0, &dev->mapped_vertex);
  849. vkMapMemory(dev->logical_device, dev->index_memory, 0, max_element_buffer,
  850. 0, &dev->mapped_index);
  851. vkMapMemory(dev->logical_device, dev->uniform_memory, 0,
  852. sizeof(struct Mat4f), 0, &dev->mapped_uniform);
  853. nk_sdl_create_render_resources(dev, framebuffer_width, framebuffer_height);
  854. }
  855. NK_INTERN void nk_sdl_device_upload_atlas(VkQueue graphics_queue,
  856. const void *image, int width,
  857. int height) {
  858. struct nk_sdl_device *dev = &sdl.vulkan;
  859. VkImageCreateInfo image_info;
  860. VkResult result;
  861. VkMemoryRequirements mem_reqs;
  862. VkMemoryAllocateInfo alloc_info;
  863. VkBufferCreateInfo buffer_info;
  864. uint8_t *data = 0;
  865. VkCommandBufferBeginInfo begin_info;
  866. VkCommandBuffer command_buffer;
  867. VkImageMemoryBarrier image_memory_barrier;
  868. VkBufferImageCopy buffer_copy_region;
  869. VkImageMemoryBarrier image_shader_memory_barrier;
  870. VkFence fence;
  871. VkFenceCreateInfo fence_create;
  872. VkSubmitInfo submit_info;
  873. VkImageViewCreateInfo image_view_info;
  874. struct {
  875. VkDeviceMemory memory;
  876. VkBuffer buffer;
  877. } staging_buffer;
  878. memset(&image_info, 0, sizeof(VkImageCreateInfo));
  879. image_info.sType = VK_STRUCTURE_TYPE_IMAGE_CREATE_INFO;
  880. image_info.imageType = VK_IMAGE_TYPE_2D;
  881. image_info.format = VK_FORMAT_R8G8B8A8_UNORM;
  882. image_info.extent.width = (uint32_t)width;
  883. image_info.extent.height = (uint32_t)height;
  884. image_info.extent.depth = 1;
  885. image_info.mipLevels = 1;
  886. image_info.arrayLayers = 1;
  887. image_info.samples = VK_SAMPLE_COUNT_1_BIT;
  888. image_info.tiling = VK_IMAGE_TILING_OPTIMAL;
  889. image_info.usage =
  890. VK_IMAGE_USAGE_TRANSFER_DST_BIT | VK_IMAGE_USAGE_SAMPLED_BIT;
  891. image_info.sharingMode = VK_SHARING_MODE_EXCLUSIVE;
  892. image_info.initialLayout = VK_IMAGE_LAYOUT_UNDEFINED;
  893. result =
  894. vkCreateImage(dev->logical_device, &image_info, NULL, &dev->font_image);
  895. NK_ASSERT(result == VK_SUCCESS);
  896. vkGetImageMemoryRequirements(dev->logical_device, dev->font_image,
  897. &mem_reqs);
  898. memset(&alloc_info, 0, sizeof(VkMemoryAllocateInfo));
  899. alloc_info.sType = VK_STRUCTURE_TYPE_MEMORY_ALLOCATE_INFO;
  900. alloc_info.allocationSize = mem_reqs.size;
  901. alloc_info.memoryTypeIndex =
  902. nk_sdl_find_memory_index(dev->physical_device, mem_reqs.memoryTypeBits,
  903. VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT);
  904. result = vkAllocateMemory(dev->logical_device, &alloc_info, NULL,
  905. &dev->font_memory);
  906. NK_ASSERT(result == VK_SUCCESS);
  907. result = vkBindImageMemory(dev->logical_device, dev->font_image,
  908. dev->font_memory, 0);
  909. NK_ASSERT(result == VK_SUCCESS);
  910. memset(&buffer_info, 0, sizeof(VkBufferCreateInfo));
  911. buffer_info.sType = VK_STRUCTURE_TYPE_BUFFER_CREATE_INFO;
  912. buffer_info.size = alloc_info.allocationSize;
  913. buffer_info.usage = VK_BUFFER_USAGE_TRANSFER_SRC_BIT;
  914. buffer_info.sharingMode = VK_SHARING_MODE_EXCLUSIVE;
  915. result = vkCreateBuffer(dev->logical_device, &buffer_info, NULL,
  916. &staging_buffer.buffer);
  917. NK_ASSERT(result == VK_SUCCESS);
  918. vkGetBufferMemoryRequirements(dev->logical_device, staging_buffer.buffer,
  919. &mem_reqs);
  920. alloc_info.allocationSize = mem_reqs.size;
  921. alloc_info.memoryTypeIndex =
  922. nk_sdl_find_memory_index(dev->physical_device, mem_reqs.memoryTypeBits,
  923. VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT |
  924. VK_MEMORY_PROPERTY_HOST_COHERENT_BIT);
  925. result = vkAllocateMemory(dev->logical_device, &alloc_info, NULL,
  926. &staging_buffer.memory);
  927. NK_ASSERT(result == VK_SUCCESS);
  928. result = vkBindBufferMemory(dev->logical_device, staging_buffer.buffer,
  929. staging_buffer.memory, 0);
  930. NK_ASSERT(result == VK_SUCCESS);
  931. result = vkMapMemory(dev->logical_device, staging_buffer.memory, 0,
  932. alloc_info.allocationSize, 0, (void **)&data);
  933. NK_ASSERT(result == VK_SUCCESS);
  934. memcpy(data, image, width * height * 4);
  935. vkUnmapMemory(dev->logical_device, staging_buffer.memory);
  936. memset(&begin_info, 0, sizeof(VkCommandBufferBeginInfo));
  937. begin_info.sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_BEGIN_INFO;
  938. NK_ASSERT(dev->command_buffers_len > 0);
  939. /*
  940. use the same command buffer as for render as we are regenerating the
  941. buffer during render anyway
  942. */
  943. command_buffer = dev->command_buffers[0];
  944. result = vkBeginCommandBuffer(command_buffer, &begin_info);
  945. NK_ASSERT(result == VK_SUCCESS);
  946. memset(&image_memory_barrier, 0, sizeof(VkImageMemoryBarrier));
  947. image_memory_barrier.sType = VK_STRUCTURE_TYPE_IMAGE_MEMORY_BARRIER;
  948. image_memory_barrier.image = dev->font_image;
  949. image_memory_barrier.srcQueueFamilyIndex = VK_QUEUE_FAMILY_IGNORED;
  950. image_memory_barrier.dstQueueFamilyIndex = VK_QUEUE_FAMILY_IGNORED;
  951. image_memory_barrier.oldLayout = VK_IMAGE_LAYOUT_UNDEFINED;
  952. image_memory_barrier.newLayout = VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL;
  953. image_memory_barrier.subresourceRange.aspectMask =
  954. VK_IMAGE_ASPECT_COLOR_BIT;
  955. image_memory_barrier.subresourceRange.levelCount = 1;
  956. image_memory_barrier.subresourceRange.layerCount = 1;
  957. image_memory_barrier.dstAccessMask = VK_ACCESS_TRANSFER_WRITE_BIT;
  958. vkCmdPipelineBarrier(command_buffer, VK_PIPELINE_STAGE_TOP_OF_PIPE_BIT,
  959. VK_PIPELINE_STAGE_TRANSFER_BIT, 0, 0, NULL, 0, NULL, 1,
  960. &image_memory_barrier);
  961. memset(&buffer_copy_region, 0, sizeof(VkBufferImageCopy));
  962. buffer_copy_region.imageSubresource.aspectMask = VK_IMAGE_ASPECT_COLOR_BIT;
  963. buffer_copy_region.imageSubresource.layerCount = 1;
  964. buffer_copy_region.imageExtent.width = (uint32_t)width;
  965. buffer_copy_region.imageExtent.height = (uint32_t)height;
  966. buffer_copy_region.imageExtent.depth = 1;
  967. vkCmdCopyBufferToImage(
  968. command_buffer, staging_buffer.buffer, dev->font_image,
  969. VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL, 1, &buffer_copy_region);
  970. memset(&image_shader_memory_barrier, 0, sizeof(VkImageMemoryBarrier));
  971. image_shader_memory_barrier.sType = VK_STRUCTURE_TYPE_IMAGE_MEMORY_BARRIER;
  972. image_shader_memory_barrier.image = dev->font_image;
  973. image_shader_memory_barrier.srcQueueFamilyIndex = VK_QUEUE_FAMILY_IGNORED;
  974. image_shader_memory_barrier.dstQueueFamilyIndex = VK_QUEUE_FAMILY_IGNORED;
  975. image_shader_memory_barrier.oldLayout =
  976. VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL;
  977. image_shader_memory_barrier.newLayout =
  978. VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL;
  979. image_shader_memory_barrier.subresourceRange.aspectMask =
  980. VK_IMAGE_ASPECT_COLOR_BIT;
  981. image_shader_memory_barrier.subresourceRange.levelCount = 1;
  982. image_shader_memory_barrier.subresourceRange.layerCount = 1;
  983. image_shader_memory_barrier.srcAccessMask = VK_ACCESS_TRANSFER_WRITE_BIT,
  984. image_shader_memory_barrier.dstAccessMask = VK_ACCESS_SHADER_READ_BIT,
  985. vkCmdPipelineBarrier(command_buffer, VK_PIPELINE_STAGE_TRANSFER_BIT,
  986. VK_PIPELINE_STAGE_FRAGMENT_SHADER_BIT, 0, 0, NULL, 0,
  987. NULL, 1, &image_shader_memory_barrier);
  988. result = vkEndCommandBuffer(command_buffer);
  989. NK_ASSERT(result == VK_SUCCESS);
  990. memset(&fence_create, 0, sizeof(VkFenceCreateInfo));
  991. fence_create.sType = VK_STRUCTURE_TYPE_FENCE_CREATE_INFO;
  992. result = vkCreateFence(dev->logical_device, &fence_create, NULL, &fence);
  993. NK_ASSERT(result == VK_SUCCESS);
  994. memset(&submit_info, 0, sizeof(VkSubmitInfo));
  995. submit_info.sType = VK_STRUCTURE_TYPE_SUBMIT_INFO;
  996. submit_info.commandBufferCount = 1;
  997. submit_info.pCommandBuffers = &command_buffer;
  998. result = vkQueueSubmit(graphics_queue, 1, &submit_info, fence);
  999. NK_ASSERT(result == VK_SUCCESS);
  1000. result =
  1001. vkWaitForFences(dev->logical_device, 1, &fence, VK_TRUE, UINT64_MAX);
  1002. NK_ASSERT(result == VK_SUCCESS);
  1003. vkDestroyFence(dev->logical_device, fence, NULL);
  1004. vkFreeMemory(dev->logical_device, staging_buffer.memory, NULL);
  1005. vkDestroyBuffer(dev->logical_device, staging_buffer.buffer, NULL);
  1006. memset(&image_view_info, 0, sizeof(VkImageViewCreateInfo));
  1007. image_view_info.sType = VK_STRUCTURE_TYPE_IMAGE_VIEW_CREATE_INFO;
  1008. image_view_info.image = dev->font_image;
  1009. image_view_info.viewType = VK_IMAGE_VIEW_TYPE_2D;
  1010. image_view_info.format = image_info.format;
  1011. image_view_info.subresourceRange.aspectMask = VK_IMAGE_ASPECT_COLOR_BIT;
  1012. image_view_info.subresourceRange.layerCount = 1;
  1013. image_view_info.subresourceRange.levelCount = 1;
  1014. result = vkCreateImageView(dev->logical_device, &image_view_info, NULL,
  1015. &dev->font_image_view);
  1016. NK_ASSERT(result == VK_SUCCESS);
  1017. }
  1018. NK_INTERN void nk_sdl_destroy_render_resources(struct nk_sdl_device *dev) {
  1019. uint32_t i;
  1020. vkDestroyPipeline(dev->logical_device, dev->pipeline, NULL);
  1021. vkDestroyPipelineLayout(dev->logical_device, dev->pipeline_layout, NULL);
  1022. vkDestroyDescriptorSetLayout(dev->logical_device,
  1023. dev->texture_descriptor_set_layout, NULL);
  1024. vkDestroyDescriptorSetLayout(dev->logical_device,
  1025. dev->uniform_descriptor_set_layout, NULL);
  1026. vkDestroyDescriptorPool(dev->logical_device, dev->descriptor_pool, NULL);
  1027. for (i = 0; i < dev->framebuffers_len; i++) {
  1028. vkDestroyFramebuffer(dev->logical_device, dev->framebuffers[i], NULL);
  1029. }
  1030. free(dev->framebuffers);
  1031. dev->framebuffers_len = 0;
  1032. free(dev->texture_descriptor_sets);
  1033. dev->texture_descriptor_sets_len = 0;
  1034. vkDestroyRenderPass(dev->logical_device, dev->render_pass, NULL);
  1035. }
  1036. NK_API void nk_sdl_resize(uint32_t framebuffer_width,
  1037. uint32_t framebuffer_height) {
  1038. struct nk_sdl_device *dev = &sdl.vulkan;
  1039. SDL_GetWindowSize(sdl.win, &sdl.width, &sdl.height);
  1040. sdl.display_width = framebuffer_width;
  1041. sdl.display_height = framebuffer_height;
  1042. nk_sdl_destroy_render_resources(dev);
  1043. nk_sdl_create_render_resources(dev, sdl.display_width, sdl.display_height);
  1044. }
  1045. NK_API void nk_sdl_device_destroy(void) {
  1046. struct nk_sdl_device *dev = &sdl.vulkan;
  1047. vkDeviceWaitIdle(dev->logical_device);
  1048. nk_sdl_destroy_render_resources(dev);
  1049. vkFreeCommandBuffers(dev->logical_device, dev->command_pool,
  1050. dev->command_buffers_len, dev->command_buffers);
  1051. vkDestroyCommandPool(dev->logical_device, dev->command_pool, NULL);
  1052. vkDestroySemaphore(dev->logical_device, dev->render_completed, NULL);
  1053. vkUnmapMemory(dev->logical_device, dev->vertex_memory);
  1054. vkUnmapMemory(dev->logical_device, dev->index_memory);
  1055. vkUnmapMemory(dev->logical_device, dev->uniform_memory);
  1056. vkFreeMemory(dev->logical_device, dev->vertex_memory, NULL);
  1057. vkFreeMemory(dev->logical_device, dev->index_memory, NULL);
  1058. vkFreeMemory(dev->logical_device, dev->uniform_memory, NULL);
  1059. vkDestroyBuffer(dev->logical_device, dev->vertex_buffer, NULL);
  1060. vkDestroyBuffer(dev->logical_device, dev->index_buffer, NULL);
  1061. vkDestroyBuffer(dev->logical_device, dev->uniform_buffer, NULL);
  1062. vkDestroySampler(dev->logical_device, dev->sampler, NULL);
  1063. vkFreeMemory(dev->logical_device, dev->font_memory, NULL);
  1064. vkDestroyImage(dev->logical_device, dev->font_image, NULL);
  1065. vkDestroyImageView(dev->logical_device, dev->font_image_view, NULL);
  1066. free(dev->command_buffers);
  1067. nk_buffer_free(&dev->cmds);
  1068. }
  1069. NK_API
  1070. void nk_sdl_shutdown(void) {
  1071. nk_font_atlas_clear(&sdl.atlas);
  1072. nk_free(&sdl.ctx);
  1073. nk_sdl_device_destroy();
  1074. memset(&sdl, 0, sizeof(sdl));
  1075. }
  1076. NK_API void nk_sdl_font_stash_begin(struct nk_font_atlas **atlas) {
  1077. nk_font_atlas_init_default(&sdl.atlas);
  1078. nk_font_atlas_begin(&sdl.atlas);
  1079. *atlas = &sdl.atlas;
  1080. }
  1081. NK_API void nk_sdl_font_stash_end(VkQueue graphics_queue) {
  1082. struct nk_sdl_device *dev = &sdl.vulkan;
  1083. const void *image;
  1084. int w, h;
  1085. image = nk_font_atlas_bake(&sdl.atlas, &w, &h, NK_FONT_ATLAS_RGBA32);
  1086. nk_sdl_device_upload_atlas(graphics_queue, image, w, h);
  1087. nk_font_atlas_end(&sdl.atlas, nk_handle_ptr(dev->font_image_view),
  1088. &dev->tex_null);
  1089. if (sdl.atlas.default_font) {
  1090. nk_style_set_font(&sdl.ctx, &sdl.atlas.default_font->handle);
  1091. }
  1092. }
  1093. NK_API void nk_sdl_handle_grab(void) {
  1094. struct nk_context *ctx = &sdl.ctx;
  1095. if (ctx->input.mouse.grab) {
  1096. SDL_SetRelativeMouseMode(SDL_TRUE);
  1097. } else if (ctx->input.mouse.ungrab) {
  1098. /* better support for older SDL by setting mode first; causes an extra
  1099. * mouse motion event */
  1100. SDL_SetRelativeMouseMode(SDL_FALSE);
  1101. SDL_WarpMouseInWindow(sdl.win, (int)ctx->input.mouse.prev.x,
  1102. (int)ctx->input.mouse.prev.y);
  1103. } else if (ctx->input.mouse.grabbed) {
  1104. ctx->input.mouse.pos.x = ctx->input.mouse.prev.x;
  1105. ctx->input.mouse.pos.y = ctx->input.mouse.prev.y;
  1106. }
  1107. }
  1108. NK_API int nk_sdl_handle_event(SDL_Event *evt) {
  1109. struct nk_context *ctx = &sdl.ctx;
  1110. int ctrl_down = SDL_GetModState() & (KMOD_LCTRL | KMOD_RCTRL);
  1111. switch (evt->type) {
  1112. case SDL_KEYUP: /* KEYUP & KEYDOWN share same routine */
  1113. case SDL_KEYDOWN: {
  1114. int down = evt->type == SDL_KEYDOWN;
  1115. switch (evt->key.keysym.sym) {
  1116. case SDLK_RSHIFT: /* RSHIFT & LSHIFT share same routine */
  1117. case SDLK_LSHIFT:
  1118. nk_input_key(ctx, NK_KEY_SHIFT, down);
  1119. break;
  1120. case SDLK_DELETE:
  1121. nk_input_key(ctx, NK_KEY_DEL, down);
  1122. break;
  1123. case SDLK_RETURN:
  1124. case SDLK_KP_ENTER:
  1125. nk_input_key(ctx, NK_KEY_ENTER, down);
  1126. break;
  1127. case SDLK_TAB:
  1128. nk_input_key(ctx, NK_KEY_TAB, down);
  1129. break;
  1130. case SDLK_BACKSPACE:
  1131. nk_input_key(ctx, NK_KEY_BACKSPACE, down);
  1132. break;
  1133. case SDLK_HOME:
  1134. nk_input_key(ctx, NK_KEY_TEXT_START, down);
  1135. nk_input_key(ctx, NK_KEY_SCROLL_START, down);
  1136. break;
  1137. case SDLK_END:
  1138. nk_input_key(ctx, NK_KEY_TEXT_END, down);
  1139. nk_input_key(ctx, NK_KEY_SCROLL_END, down);
  1140. break;
  1141. case SDLK_PAGEDOWN:
  1142. nk_input_key(ctx, NK_KEY_SCROLL_DOWN, down);
  1143. break;
  1144. case SDLK_PAGEUP:
  1145. nk_input_key(ctx, NK_KEY_SCROLL_UP, down);
  1146. break;
  1147. case SDLK_z:
  1148. nk_input_key(ctx, NK_KEY_TEXT_UNDO,
  1149. down && ctrl_down);
  1150. break;
  1151. case SDLK_r:
  1152. nk_input_key(ctx, NK_KEY_TEXT_REDO,
  1153. down && ctrl_down);
  1154. break;
  1155. case SDLK_c:
  1156. nk_input_key(ctx, NK_KEY_COPY, down && ctrl_down);
  1157. break;
  1158. case SDLK_v:
  1159. nk_input_key(ctx, NK_KEY_PASTE, down && ctrl_down);
  1160. break;
  1161. case SDLK_x:
  1162. nk_input_key(ctx, NK_KEY_CUT, down && ctrl_down);
  1163. break;
  1164. case SDLK_b:
  1165. nk_input_key(ctx, NK_KEY_TEXT_LINE_START,
  1166. down && ctrl_down);
  1167. break;
  1168. case SDLK_e:
  1169. nk_input_key(ctx, NK_KEY_TEXT_LINE_END,
  1170. down && ctrl_down);
  1171. break;
  1172. case SDLK_UP:
  1173. nk_input_key(ctx, NK_KEY_UP, down);
  1174. break;
  1175. case SDLK_DOWN:
  1176. nk_input_key(ctx, NK_KEY_DOWN, down);
  1177. break;
  1178. case SDLK_a:
  1179. if(ctrl_down)
  1180. nk_input_key(ctx,NK_KEY_TEXT_SELECT_ALL, down);
  1181. break;
  1182. case SDLK_LEFT:
  1183. if (ctrl_down)
  1184. nk_input_key(ctx, NK_KEY_TEXT_WORD_LEFT, down);
  1185. else
  1186. nk_input_key(ctx, NK_KEY_LEFT, down);
  1187. break;
  1188. case SDLK_RIGHT:
  1189. if (ctrl_down)
  1190. nk_input_key(ctx, NK_KEY_TEXT_WORD_RIGHT, down);
  1191. else
  1192. nk_input_key(ctx, NK_KEY_RIGHT, down);
  1193. break;
  1194. }
  1195. return 1;
  1196. }
  1197. case SDL_MOUSEBUTTONUP: /* MOUSEBUTTONUP & MOUSEBUTTONDOWN share same
  1198. routine */
  1199. case SDL_MOUSEBUTTONDOWN: {
  1200. int down = evt->type == SDL_MOUSEBUTTONDOWN;
  1201. const int x = evt->button.x, y = evt->button.y;
  1202. switch (evt->button.button) {
  1203. case SDL_BUTTON_LEFT:
  1204. if (evt->button.clicks > 1)
  1205. nk_input_button(ctx, NK_BUTTON_DOUBLE, x, y, down);
  1206. nk_input_button(ctx, NK_BUTTON_LEFT, x, y, down);
  1207. break;
  1208. case SDL_BUTTON_MIDDLE:
  1209. nk_input_button(ctx, NK_BUTTON_MIDDLE, x, y, down);
  1210. break;
  1211. case SDL_BUTTON_RIGHT:
  1212. nk_input_button(ctx, NK_BUTTON_RIGHT, x, y, down);
  1213. break;
  1214. }
  1215. return 1;
  1216. }
  1217. case SDL_MOUSEMOTION: {
  1218. if (ctx->input.mouse.grabbed) {
  1219. int x = (int)ctx->input.mouse.prev.x,
  1220. y = (int)ctx->input.mouse.prev.y;
  1221. nk_input_motion(ctx, x + evt->motion.xrel, y + evt->motion.yrel);
  1222. } else
  1223. nk_input_motion(ctx, evt->motion.x, evt->motion.y);
  1224. return 1;
  1225. }
  1226. case SDL_TEXTINPUT: {
  1227. nk_glyph glyph;
  1228. memcpy(glyph, evt->text.text, NK_UTF_SIZE);
  1229. nk_input_glyph(ctx, glyph);
  1230. return 1;
  1231. }
  1232. case SDL_MOUSEWHEEL:
  1233. nk_input_scroll(ctx,nk_vec2(evt->wheel.preciseX, evt->wheel.preciseY));
  1234. return 1;
  1235. }
  1236. return 0;
  1237. }
  1238. NK_INTERN void update_texture_descriptor_set(
  1239. struct nk_sdl_device *dev,
  1240. struct nk_vulkan_texture_descriptor_set *texture_descriptor_set,
  1241. VkImageView image_view) {
  1242. VkDescriptorImageInfo descriptor_image_info;
  1243. VkWriteDescriptorSet descriptor_write;
  1244. texture_descriptor_set->image_view = image_view;
  1245. memset(&descriptor_image_info, 0, sizeof(VkDescriptorImageInfo));
  1246. descriptor_image_info.sampler = dev->sampler;
  1247. descriptor_image_info.imageView = texture_descriptor_set->image_view;
  1248. descriptor_image_info.imageLayout =
  1249. VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL;
  1250. memset(&descriptor_write, 0, sizeof(VkWriteDescriptorSet));
  1251. descriptor_write.sType = VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET;
  1252. descriptor_write.dstSet = texture_descriptor_set->descriptor_set;
  1253. descriptor_write.dstBinding = 0;
  1254. descriptor_write.dstArrayElement = 0;
  1255. descriptor_write.descriptorType = VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER;
  1256. descriptor_write.descriptorCount = 1;
  1257. descriptor_write.pImageInfo = &descriptor_image_info;
  1258. vkUpdateDescriptorSets(dev->logical_device, 1, &descriptor_write, 0, NULL);
  1259. }
  1260. NK_API
  1261. VkSemaphore nk_sdl_render(VkQueue graphics_queue, uint32_t buffer_index,
  1262. VkSemaphore wait_semaphore,
  1263. enum nk_anti_aliasing AA) {
  1264. struct nk_sdl_device *dev = &sdl.vulkan;
  1265. struct nk_buffer vbuf, ebuf;
  1266. struct Mat4f projection = {
  1267. {2.0f, 0.0f, 0.0f, 0.0f, 0.0f, -2.0f, 0.0f, 0.0f, 0.0f, 0.0f, -1.0f,
  1268. 0.0f, -1.0f, 1.0f, 0.0f, 1.0f},
  1269. };
  1270. VkCommandBufferBeginInfo begin_info;
  1271. VkClearValue clear_value = {{{0.0f, 0.0f, 0.0f, 0.0f}}};
  1272. VkRenderPassBeginInfo render_pass_begin_nfo;
  1273. VkCommandBuffer command_buffer;
  1274. VkResult result;
  1275. VkViewport viewport;
  1276. VkDeviceSize doffset = 0;
  1277. VkImageView current_texture = NULL;
  1278. uint32_t index_offset = 0;
  1279. VkRect2D scissor;
  1280. uint32_t wait_semaphore_count;
  1281. VkSemaphore *wait_semaphores;
  1282. VkPipelineStageFlags wait_stage =
  1283. VK_PIPELINE_STAGE_COLOR_ATTACHMENT_OUTPUT_BIT;
  1284. VkSubmitInfo submit_info;
  1285. uint64_t time_now;
  1286. time_now = SDL_GetTicks64();
  1287. sdl.ctx.delta_time_seconds =
  1288. (float)(time_now - sdl.delta_time_milliseconds_last) / 1000.0f;
  1289. sdl.delta_time_milliseconds_last = time_now;
  1290. projection.m[0] /= sdl.display_width;
  1291. projection.m[5] /= sdl.display_height;
  1292. memcpy(dev->mapped_uniform, &projection, sizeof(projection));
  1293. memset(&begin_info, 0, sizeof(VkCommandBufferBeginInfo));
  1294. begin_info.sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_BEGIN_INFO;
  1295. memset(&render_pass_begin_nfo, 0, sizeof(VkRenderPassBeginInfo));
  1296. render_pass_begin_nfo.sType = VK_STRUCTURE_TYPE_RENDER_PASS_BEGIN_INFO;
  1297. render_pass_begin_nfo.renderPass = dev->render_pass;
  1298. render_pass_begin_nfo.renderArea.extent.width = (uint32_t)sdl.display_width;
  1299. render_pass_begin_nfo.renderArea.extent.height =
  1300. (uint32_t)sdl.display_height;
  1301. render_pass_begin_nfo.clearValueCount = 1;
  1302. render_pass_begin_nfo.pClearValues = &clear_value;
  1303. render_pass_begin_nfo.framebuffer = dev->framebuffers[buffer_index];
  1304. command_buffer = dev->command_buffers[buffer_index];
  1305. result = vkBeginCommandBuffer(command_buffer, &begin_info);
  1306. NK_ASSERT(result == VK_SUCCESS);
  1307. vkCmdBeginRenderPass(command_buffer, &render_pass_begin_nfo,
  1308. VK_SUBPASS_CONTENTS_INLINE);
  1309. memset(&viewport, 0, sizeof(VkViewport));
  1310. viewport.width = (float)sdl.width;
  1311. viewport.height = (float)sdl.height;
  1312. viewport.maxDepth = 1.0f;
  1313. vkCmdSetViewport(command_buffer, 0, 1, &viewport);
  1314. vkCmdBindPipeline(command_buffer, VK_PIPELINE_BIND_POINT_GRAPHICS,
  1315. dev->pipeline);
  1316. vkCmdBindDescriptorSets(command_buffer, VK_PIPELINE_BIND_POINT_GRAPHICS,
  1317. dev->pipeline_layout, 0, 1,
  1318. &dev->uniform_descriptor_set, 0, NULL);
  1319. {
  1320. /* convert from command queue into draw list and draw to screen */
  1321. const struct nk_draw_command *cmd;
  1322. /* load draw vertices & elements directly into vertex + element buffer
  1323. */
  1324. {
  1325. /* fill convert configuration */
  1326. struct nk_convert_config config;
  1327. static const struct nk_draw_vertex_layout_element vertex_layout[] =
  1328. {{NK_VERTEX_POSITION, NK_FORMAT_FLOAT,
  1329. NK_OFFSETOF(struct nk_sdl_vertex, position)},
  1330. {NK_VERTEX_TEXCOORD, NK_FORMAT_FLOAT,
  1331. NK_OFFSETOF(struct nk_sdl_vertex, uv)},
  1332. {NK_VERTEX_COLOR, NK_FORMAT_R8G8B8A8,
  1333. NK_OFFSETOF(struct nk_sdl_vertex, col)},
  1334. {NK_VERTEX_LAYOUT_END}};
  1335. NK_MEMSET(&config, 0, sizeof(config));
  1336. config.vertex_layout = vertex_layout;
  1337. config.vertex_size = sizeof(struct nk_sdl_vertex);
  1338. config.vertex_alignment = NK_ALIGNOF(struct nk_sdl_vertex);
  1339. config.tex_null = dev->tex_null;
  1340. config.circle_segment_count = 22;
  1341. config.curve_segment_count = 22;
  1342. config.arc_segment_count = 22;
  1343. config.global_alpha = 1.0f;
  1344. config.shape_AA = AA;
  1345. config.line_AA = AA;
  1346. /* setup buffers to load vertices and elements */
  1347. nk_buffer_init_fixed(&vbuf, dev->mapped_vertex,
  1348. (size_t)dev->max_vertex_buffer);
  1349. nk_buffer_init_fixed(&ebuf, dev->mapped_index,
  1350. (size_t)dev->max_element_buffer);
  1351. nk_convert(&sdl.ctx, &dev->cmds, &vbuf, &ebuf, &config);
  1352. }
  1353. /* iterate over and execute each draw command */
  1354. vkCmdBindVertexBuffers(command_buffer, 0, 1, &dev->vertex_buffer,
  1355. &doffset);
  1356. vkCmdBindIndexBuffer(command_buffer, dev->index_buffer, 0,
  1357. VK_INDEX_TYPE_UINT16);
  1358. nk_draw_foreach(cmd, &sdl.ctx, &dev->cmds) {
  1359. if (!cmd->texture.ptr) {
  1360. continue;
  1361. }
  1362. if (cmd->texture.ptr && cmd->texture.ptr != current_texture) {
  1363. int found = 0;
  1364. uint32_t i;
  1365. for (i = 0; i < dev->texture_descriptor_sets_len; i++) {
  1366. if (dev->texture_descriptor_sets[i].image_view ==
  1367. cmd->texture.ptr) {
  1368. found = 1;
  1369. break;
  1370. }
  1371. }
  1372. if (!found) {
  1373. update_texture_descriptor_set(
  1374. dev, &dev->texture_descriptor_sets[i],
  1375. (VkImageView)cmd->texture.ptr);
  1376. dev->texture_descriptor_sets_len++;
  1377. }
  1378. vkCmdBindDescriptorSets(
  1379. command_buffer, VK_PIPELINE_BIND_POINT_GRAPHICS,
  1380. dev->pipeline_layout, 1, 1,
  1381. &dev->texture_descriptor_sets[i].descriptor_set, 0, NULL);
  1382. }
  1383. if (!cmd->elem_count)
  1384. continue;
  1385. scissor.offset.x = (int32_t)(NK_MAX(cmd->clip_rect.x, 0.f));
  1386. scissor.offset.y = (int32_t)(NK_MAX(cmd->clip_rect.y, 0.f));
  1387. scissor.extent.width = (uint32_t)(cmd->clip_rect.w);
  1388. scissor.extent.height = (uint32_t)(cmd->clip_rect.h);
  1389. vkCmdSetScissor(command_buffer, 0, 1, &scissor);
  1390. vkCmdDrawIndexed(command_buffer, cmd->elem_count, 1, index_offset,
  1391. 0, 0);
  1392. index_offset += cmd->elem_count;
  1393. }
  1394. nk_clear(&sdl.ctx);
  1395. }
  1396. vkCmdEndRenderPass(command_buffer);
  1397. result = vkEndCommandBuffer(command_buffer);
  1398. NK_ASSERT(result == VK_SUCCESS);
  1399. if (wait_semaphore) {
  1400. wait_semaphore_count = 1;
  1401. wait_semaphores = &wait_semaphore;
  1402. } else {
  1403. wait_semaphore_count = 0;
  1404. wait_semaphores = NULL;
  1405. }
  1406. memset(&submit_info, 0, sizeof(VkSubmitInfo));
  1407. submit_info.sType = VK_STRUCTURE_TYPE_SUBMIT_INFO;
  1408. submit_info.commandBufferCount = 1;
  1409. submit_info.pCommandBuffers = &command_buffer;
  1410. submit_info.pWaitDstStageMask = &wait_stage;
  1411. submit_info.waitSemaphoreCount = wait_semaphore_count;
  1412. submit_info.pWaitSemaphores = wait_semaphores;
  1413. submit_info.signalSemaphoreCount = 1;
  1414. submit_info.pSignalSemaphores = &dev->render_completed;
  1415. result = vkQueueSubmit(graphics_queue, 1, &submit_info, NULL);
  1416. NK_ASSERT(result == VK_SUCCESS);
  1417. return dev->render_completed;
  1418. }
  1419. NK_INTERN void nk_sdl_clipboard_paste(nk_handle usr,
  1420. struct nk_text_edit *edit) {
  1421. const char *text = SDL_GetClipboardText();
  1422. if (text) {
  1423. nk_textedit_paste(edit, text, nk_strlen(text));
  1424. SDL_free((void *)text);
  1425. }
  1426. (void)usr;
  1427. }
  1428. NK_INTERN void nk_sdl_clipboard_copy(nk_handle usr, const char *text, int len) {
  1429. char *str = 0;
  1430. (void)usr;
  1431. if (!len)
  1432. return;
  1433. str = (char *)malloc((size_t)len + 1);
  1434. if (!str)
  1435. return;
  1436. memcpy(str, text, (size_t)len);
  1437. str[len] = '\0';
  1438. SDL_SetClipboardText(str);
  1439. free(str);
  1440. }
  1441. NK_API struct nk_context *
  1442. nk_sdl_init(SDL_Window *win, VkDevice logical_device,
  1443. VkPhysicalDevice physical_device,
  1444. uint32_t graphics_queue_family_index, VkImageView *image_views,
  1445. uint32_t image_views_len, VkFormat color_format,
  1446. enum nk_sdl_init_state init_state, VkDeviceSize max_vertex_buffer,
  1447. VkDeviceSize max_element_buffer) {
  1448. (void)init_state;
  1449. memset(&sdl, 0, sizeof(struct nk_sdl));
  1450. sdl.win = win;
  1451. nk_init_default(&sdl.ctx, 0);
  1452. sdl.ctx.clip.copy = nk_sdl_clipboard_copy;
  1453. sdl.ctx.clip.paste = nk_sdl_clipboard_paste;
  1454. sdl.ctx.clip.userdata = nk_handle_ptr(0);
  1455. SDL_GetWindowSize(win, &sdl.width, &sdl.height);
  1456. SDL_Vulkan_GetDrawableSize(win, &sdl.display_width, &sdl.display_height);
  1457. nk_sdl_device_create(logical_device, physical_device,
  1458. graphics_queue_family_index, image_views,
  1459. image_views_len, color_format, max_vertex_buffer,
  1460. max_element_buffer, (uint32_t)sdl.display_width,
  1461. (uint32_t)sdl.display_height);
  1462. return &sdl.ctx;
  1463. }
  1464. #endif