vulkan_context.cpp 105 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751
  1. /**************************************************************************/
  2. /* vulkan_context.cpp */
  3. /**************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /**************************************************************************/
  8. /* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
  9. /* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
  10. /* */
  11. /* Permission is hereby granted, free of charge, to any person obtaining */
  12. /* a copy of this software and associated documentation files (the */
  13. /* "Software"), to deal in the Software without restriction, including */
  14. /* without limitation the rights to use, copy, modify, merge, publish, */
  15. /* distribute, sublicense, and/or sell copies of the Software, and to */
  16. /* permit persons to whom the Software is furnished to do so, subject to */
  17. /* the following conditions: */
  18. /* */
  19. /* The above copyright notice and this permission notice shall be */
  20. /* included in all copies or substantial portions of the Software. */
  21. /* */
  22. /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
  23. /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
  24. /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
  25. /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
  26. /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
  27. /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
  28. /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  29. /**************************************************************************/
  30. #include "vulkan_context.h"
  31. #include "core/config/engine.h"
  32. #include "core/config/project_settings.h"
  33. #include "core/string/ustring.h"
  34. #include "core/templates/local_vector.h"
  35. #include "core/version.h"
  36. #include "servers/rendering/rendering_device.h"
  37. #include "vk_enum_string_helper.h"
  38. #include <stdio.h>
  39. #include <stdlib.h>
  40. #include <string.h>
  41. #define ARRAY_SIZE(a) (sizeof(a) / sizeof(a[0]))
  42. #define APP_SHORT_NAME "GodotEngine"
  43. VulkanHooks *VulkanContext::vulkan_hooks = nullptr;
  44. Vector<VkAttachmentReference> VulkanContext::_convert_VkAttachmentReference2(uint32_t p_count, const VkAttachmentReference2 *p_refs) {
  45. Vector<VkAttachmentReference> att_refs;
  46. if (p_refs != nullptr) {
  47. for (uint32_t i = 0; i < p_count; i++) {
  48. // We lose aspectMask in this conversion but we don't use it currently.
  49. VkAttachmentReference ref = {
  50. p_refs[i].attachment, /* attachment */
  51. p_refs[i].layout /* layout */
  52. };
  53. att_refs.push_back(ref);
  54. }
  55. }
  56. return att_refs;
  57. }
  58. VkResult VulkanContext::vkCreateRenderPass2KHR(VkDevice p_device, const VkRenderPassCreateInfo2 *p_create_info, const VkAllocationCallbacks *p_allocator, VkRenderPass *p_render_pass) {
  59. if (is_device_extension_enabled(VK_KHR_CREATE_RENDERPASS_2_EXTENSION_NAME)) {
  60. if (fpCreateRenderPass2KHR == nullptr) {
  61. fpCreateRenderPass2KHR = (PFN_vkCreateRenderPass2KHR)vkGetDeviceProcAddr(p_device, "vkCreateRenderPass2KHR");
  62. }
  63. if (fpCreateRenderPass2KHR == nullptr) {
  64. return VK_ERROR_EXTENSION_NOT_PRESENT;
  65. } else {
  66. return (fpCreateRenderPass2KHR)(p_device, p_create_info, p_allocator, p_render_pass);
  67. }
  68. } else {
  69. // need to fall back on vkCreateRenderPass
  70. const void *next = p_create_info->pNext; // ATM we only support multiview which should work if supported.
  71. Vector<VkAttachmentDescription> attachments;
  72. for (uint32_t i = 0; i < p_create_info->attachmentCount; i++) {
  73. // Basically the old layout just misses type and next.
  74. VkAttachmentDescription att = {
  75. p_create_info->pAttachments[i].flags, /* flags */
  76. p_create_info->pAttachments[i].format, /* format */
  77. p_create_info->pAttachments[i].samples, /* samples */
  78. p_create_info->pAttachments[i].loadOp, /* loadOp */
  79. p_create_info->pAttachments[i].storeOp, /* storeOp */
  80. p_create_info->pAttachments[i].stencilLoadOp, /* stencilLoadOp */
  81. p_create_info->pAttachments[i].stencilStoreOp, /* stencilStoreOp */
  82. p_create_info->pAttachments[i].initialLayout, /* initialLayout */
  83. p_create_info->pAttachments[i].finalLayout /* finalLayout */
  84. };
  85. attachments.push_back(att);
  86. }
  87. Vector<Vector<VkAttachmentReference>> attachment_references;
  88. Vector<VkSubpassDescription> subpasses;
  89. for (uint32_t i = 0; i < p_create_info->subpassCount; i++) {
  90. // Here we need to do more, again it's just stripping out type and next
  91. // but we have VkAttachmentReference2 to convert to VkAttachmentReference.
  92. // Also viewmask is not supported but we don't use it outside of multiview.
  93. Vector<VkAttachmentReference> input_attachments = _convert_VkAttachmentReference2(p_create_info->pSubpasses[i].inputAttachmentCount, p_create_info->pSubpasses[i].pInputAttachments);
  94. Vector<VkAttachmentReference> color_attachments = _convert_VkAttachmentReference2(p_create_info->pSubpasses[i].colorAttachmentCount, p_create_info->pSubpasses[i].pColorAttachments);
  95. Vector<VkAttachmentReference> resolve_attachments = _convert_VkAttachmentReference2(p_create_info->pSubpasses[i].colorAttachmentCount, p_create_info->pSubpasses[i].pResolveAttachments);
  96. Vector<VkAttachmentReference> depth_attachments = _convert_VkAttachmentReference2(p_create_info->pSubpasses[i].colorAttachmentCount, p_create_info->pSubpasses[i].pDepthStencilAttachment);
  97. VkSubpassDescription subpass = {
  98. p_create_info->pSubpasses[i].flags, /* flags */
  99. p_create_info->pSubpasses[i].pipelineBindPoint, /* pipelineBindPoint */
  100. p_create_info->pSubpasses[i].inputAttachmentCount, /* inputAttachmentCount */
  101. input_attachments.size() == 0 ? nullptr : input_attachments.ptr(), /* pInputAttachments */
  102. p_create_info->pSubpasses[i].colorAttachmentCount, /* colorAttachmentCount */
  103. color_attachments.size() == 0 ? nullptr : color_attachments.ptr(), /* pColorAttachments */
  104. resolve_attachments.size() == 0 ? nullptr : resolve_attachments.ptr(), /* pResolveAttachments */
  105. depth_attachments.size() == 0 ? nullptr : depth_attachments.ptr(), /* pDepthStencilAttachment */
  106. p_create_info->pSubpasses[i].preserveAttachmentCount, /* preserveAttachmentCount */
  107. p_create_info->pSubpasses[i].pPreserveAttachments /* pPreserveAttachments */
  108. };
  109. attachment_references.push_back(input_attachments);
  110. attachment_references.push_back(color_attachments);
  111. attachment_references.push_back(resolve_attachments);
  112. attachment_references.push_back(depth_attachments);
  113. subpasses.push_back(subpass);
  114. }
  115. Vector<VkSubpassDependency> dependencies;
  116. for (uint32_t i = 0; i < p_create_info->dependencyCount; i++) {
  117. // We lose viewOffset here but again I don't believe we use this anywhere.
  118. VkSubpassDependency dep = {
  119. p_create_info->pDependencies[i].srcSubpass, /* srcSubpass */
  120. p_create_info->pDependencies[i].dstSubpass, /* dstSubpass */
  121. p_create_info->pDependencies[i].srcStageMask, /* srcStageMask */
  122. p_create_info->pDependencies[i].dstStageMask, /* dstStageMask */
  123. p_create_info->pDependencies[i].srcAccessMask, /* srcAccessMask */
  124. p_create_info->pDependencies[i].dstAccessMask, /* dstAccessMask */
  125. p_create_info->pDependencies[i].dependencyFlags, /* dependencyFlags */
  126. };
  127. dependencies.push_back(dep);
  128. }
  129. // CorrelatedViewMask is not supported in vkCreateRenderPass but we
  130. // currently only use this for multiview.
  131. // We'll need to look into this.
  132. VkRenderPassCreateInfo create_info = {
  133. VK_STRUCTURE_TYPE_RENDER_PASS_CREATE_INFO, /* sType */
  134. next, /* pNext*/
  135. p_create_info->flags, /* flags */
  136. (uint32_t)attachments.size(), /* attachmentCount */
  137. attachments.ptr(), /* pAttachments */
  138. (uint32_t)subpasses.size(), /* subpassCount */
  139. subpasses.ptr(), /* pSubpasses */
  140. (uint32_t)dependencies.size(), /* */
  141. dependencies.ptr(), /* */
  142. };
  143. return vkCreateRenderPass(device, &create_info, p_allocator, p_render_pass);
  144. }
  145. }
  146. VKAPI_ATTR VkBool32 VKAPI_CALL VulkanContext::_debug_messenger_callback(
  147. VkDebugUtilsMessageSeverityFlagBitsEXT messageSeverity,
  148. VkDebugUtilsMessageTypeFlagsEXT messageType,
  149. const VkDebugUtilsMessengerCallbackDataEXT *pCallbackData,
  150. void *pUserData) {
  151. // This error needs to be ignored because the AMD allocator will mix up memory types on IGP processors.
  152. if (strstr(pCallbackData->pMessage, "Mapping an image with layout") != nullptr &&
  153. strstr(pCallbackData->pMessage, "can result in undefined behavior if this memory is used by the device") != nullptr) {
  154. return VK_FALSE;
  155. }
  156. // This needs to be ignored because Validator is wrong here.
  157. if (strstr(pCallbackData->pMessage, "Invalid SPIR-V binary version 1.3") != nullptr) {
  158. return VK_FALSE;
  159. }
  160. // This needs to be ignored because Validator is wrong here.
  161. if (strstr(pCallbackData->pMessage, "Shader requires flag") != nullptr) {
  162. return VK_FALSE;
  163. }
  164. // This needs to be ignored because Validator is wrong here.
  165. if (strstr(pCallbackData->pMessage, "SPIR-V module not valid: Pointer operand") != nullptr &&
  166. strstr(pCallbackData->pMessage, "must be a memory object") != nullptr) {
  167. return VK_FALSE;
  168. }
  169. if (pCallbackData->pMessageIdName && strstr(pCallbackData->pMessageIdName, "UNASSIGNED-CoreValidation-DrawState-ClearCmdBeforeDraw") != nullptr) {
  170. return VK_FALSE;
  171. }
  172. String type_string;
  173. switch (messageType) {
  174. case (VK_DEBUG_UTILS_MESSAGE_TYPE_GENERAL_BIT_EXT):
  175. type_string = "GENERAL";
  176. break;
  177. case (VK_DEBUG_UTILS_MESSAGE_TYPE_VALIDATION_BIT_EXT):
  178. type_string = "VALIDATION";
  179. break;
  180. case (VK_DEBUG_UTILS_MESSAGE_TYPE_PERFORMANCE_BIT_EXT):
  181. type_string = "PERFORMANCE";
  182. break;
  183. case (VK_DEBUG_UTILS_MESSAGE_TYPE_VALIDATION_BIT_EXT & VK_DEBUG_UTILS_MESSAGE_TYPE_PERFORMANCE_BIT_EXT):
  184. type_string = "VALIDATION|PERFORMANCE";
  185. break;
  186. }
  187. String objects_string;
  188. if (pCallbackData->objectCount > 0) {
  189. objects_string = "\n\tObjects - " + String::num_int64(pCallbackData->objectCount);
  190. for (uint32_t object = 0; object < pCallbackData->objectCount; ++object) {
  191. objects_string +=
  192. "\n\t\tObject[" + String::num_int64(object) + "]" +
  193. " - " + string_VkObjectType(pCallbackData->pObjects[object].objectType) +
  194. ", Handle " + String::num_int64(pCallbackData->pObjects[object].objectHandle);
  195. if (nullptr != pCallbackData->pObjects[object].pObjectName && strlen(pCallbackData->pObjects[object].pObjectName) > 0) {
  196. objects_string += ", Name \"" + String(pCallbackData->pObjects[object].pObjectName) + "\"";
  197. }
  198. }
  199. }
  200. String labels_string;
  201. if (pCallbackData->cmdBufLabelCount > 0) {
  202. labels_string = "\n\tCommand Buffer Labels - " + String::num_int64(pCallbackData->cmdBufLabelCount);
  203. for (uint32_t cmd_buf_label = 0; cmd_buf_label < pCallbackData->cmdBufLabelCount; ++cmd_buf_label) {
  204. labels_string +=
  205. "\n\t\tLabel[" + String::num_int64(cmd_buf_label) + "]" +
  206. " - " + pCallbackData->pCmdBufLabels[cmd_buf_label].pLabelName +
  207. "{ ";
  208. for (int color_idx = 0; color_idx < 4; ++color_idx) {
  209. labels_string += String::num(pCallbackData->pCmdBufLabels[cmd_buf_label].color[color_idx]);
  210. if (color_idx < 3) {
  211. labels_string += ", ";
  212. }
  213. }
  214. labels_string += " }";
  215. }
  216. }
  217. String error_message(type_string +
  218. " - Message Id Number: " + String::num_int64(pCallbackData->messageIdNumber) +
  219. " | Message Id Name: " + pCallbackData->pMessageIdName +
  220. "\n\t" + pCallbackData->pMessage +
  221. objects_string + labels_string);
  222. // Convert VK severity to our own log macros.
  223. switch (messageSeverity) {
  224. case VK_DEBUG_UTILS_MESSAGE_SEVERITY_VERBOSE_BIT_EXT:
  225. print_verbose(error_message);
  226. break;
  227. case VK_DEBUG_UTILS_MESSAGE_SEVERITY_INFO_BIT_EXT:
  228. print_line(error_message);
  229. break;
  230. case VK_DEBUG_UTILS_MESSAGE_SEVERITY_WARNING_BIT_EXT:
  231. WARN_PRINT(error_message);
  232. break;
  233. case VK_DEBUG_UTILS_MESSAGE_SEVERITY_ERROR_BIT_EXT:
  234. ERR_PRINT(error_message);
  235. CRASH_COND_MSG(Engine::get_singleton()->is_abort_on_gpu_errors_enabled(),
  236. "Crashing, because abort on GPU errors is enabled.");
  237. break;
  238. case VK_DEBUG_UTILS_MESSAGE_SEVERITY_FLAG_BITS_MAX_ENUM_EXT:
  239. break; // Shouldn't happen, only handling to make compilers happy.
  240. }
  241. return VK_FALSE;
  242. }
  243. VKAPI_ATTR VkBool32 VKAPI_CALL VulkanContext::_debug_report_callback(
  244. VkDebugReportFlagsEXT flags,
  245. VkDebugReportObjectTypeEXT objectType,
  246. uint64_t object,
  247. size_t location,
  248. int32_t messageCode,
  249. const char *pLayerPrefix,
  250. const char *pMessage,
  251. void *pUserData) {
  252. String debugMessage = String("Vulkan Debug Report: object - ") +
  253. String::num_int64(object) + "\n" + pMessage;
  254. switch (flags) {
  255. case VK_DEBUG_REPORT_DEBUG_BIT_EXT:
  256. case VK_DEBUG_REPORT_INFORMATION_BIT_EXT:
  257. print_line(debugMessage);
  258. break;
  259. case VK_DEBUG_REPORT_WARNING_BIT_EXT:
  260. case VK_DEBUG_REPORT_PERFORMANCE_WARNING_BIT_EXT:
  261. WARN_PRINT(debugMessage);
  262. break;
  263. case VK_DEBUG_REPORT_ERROR_BIT_EXT:
  264. ERR_PRINT(debugMessage);
  265. break;
  266. }
  267. return VK_FALSE;
  268. }
  269. VkBool32 VulkanContext::_check_layers(uint32_t check_count, const char *const *check_names, uint32_t layer_count, VkLayerProperties *layers) {
  270. for (uint32_t i = 0; i < check_count; i++) {
  271. VkBool32 found = 0;
  272. for (uint32_t j = 0; j < layer_count; j++) {
  273. if (!strcmp(check_names[i], layers[j].layerName)) {
  274. found = 1;
  275. break;
  276. }
  277. }
  278. if (!found) {
  279. WARN_PRINT("Can't find layer: " + String(check_names[i]));
  280. return 0;
  281. }
  282. }
  283. return 1;
  284. }
  285. Error VulkanContext::_get_preferred_validation_layers(uint32_t *count, const char *const **names) {
  286. static const LocalVector<LocalVector<const char *>> instance_validation_layers_alt{
  287. // Preferred set of validation layers.
  288. { "VK_LAYER_KHRONOS_validation" },
  289. // Alternative (deprecated, removed in SDK 1.1.126.0) set of validation layers.
  290. { "VK_LAYER_LUNARG_standard_validation" },
  291. // Alternative (deprecated, removed in SDK 1.1.121.1) set of validation layers.
  292. { "VK_LAYER_GOOGLE_threading", "VK_LAYER_LUNARG_parameter_validation", "VK_LAYER_LUNARG_object_tracker", "VK_LAYER_LUNARG_core_validation", "VK_LAYER_GOOGLE_unique_objects" }
  293. };
  294. // Clear out-arguments.
  295. *count = 0;
  296. if (names != nullptr) {
  297. *names = nullptr;
  298. }
  299. VkResult err;
  300. uint32_t instance_layer_count;
  301. err = vkEnumerateInstanceLayerProperties(&instance_layer_count, nullptr);
  302. if (err) {
  303. ERR_FAIL_V(ERR_CANT_CREATE);
  304. }
  305. if (instance_layer_count < 1) {
  306. return OK;
  307. }
  308. VkLayerProperties *instance_layers = (VkLayerProperties *)malloc(sizeof(VkLayerProperties) * instance_layer_count);
  309. err = vkEnumerateInstanceLayerProperties(&instance_layer_count, instance_layers);
  310. if (err) {
  311. free(instance_layers);
  312. ERR_FAIL_V(ERR_CANT_CREATE);
  313. }
  314. for (const LocalVector<const char *> &layer : instance_validation_layers_alt) {
  315. if (_check_layers(layer.size(), layer.ptr(), instance_layer_count, instance_layers)) {
  316. *count = layer.size();
  317. if (names != nullptr) {
  318. *names = layer.ptr();
  319. }
  320. break;
  321. }
  322. }
  323. free(instance_layers);
  324. return OK;
  325. }
  326. typedef VkResult(VKAPI_PTR *_vkEnumerateInstanceVersion)(uint32_t *);
  327. Error VulkanContext::_obtain_vulkan_version() {
  328. // https://www.khronos.org/registry/vulkan/specs/1.2-extensions/man/html/VkApplicationInfo.html#_description
  329. // For Vulkan 1.0 vkEnumerateInstanceVersion is not available, including not in the loader we compile against on Android.
  330. _vkEnumerateInstanceVersion func = (_vkEnumerateInstanceVersion)vkGetInstanceProcAddr(nullptr, "vkEnumerateInstanceVersion");
  331. if (func != nullptr) {
  332. uint32_t api_version;
  333. VkResult res = func(&api_version);
  334. if (res == VK_SUCCESS) {
  335. instance_api_version = api_version;
  336. } else {
  337. // According to the documentation this shouldn't fail with anything except a memory allocation error
  338. // in which case we're in deep trouble anyway.
  339. ERR_FAIL_V(ERR_CANT_CREATE);
  340. }
  341. } else {
  342. print_line("vkEnumerateInstanceVersion not available, assuming Vulkan 1.0.");
  343. instance_api_version = VK_API_VERSION_1_0;
  344. }
  345. return OK;
  346. }
  347. bool VulkanContext::instance_extensions_initialized = false;
  348. HashMap<CharString, bool> VulkanContext::requested_instance_extensions;
  349. void VulkanContext::register_requested_instance_extension(const CharString &extension_name, bool p_required) {
  350. ERR_FAIL_COND_MSG(instance_extensions_initialized, "You can only registered extensions before the Vulkan instance is created");
  351. ERR_FAIL_COND(requested_instance_extensions.has(extension_name));
  352. requested_instance_extensions[extension_name] = p_required;
  353. }
  354. Error VulkanContext::_initialize_instance_extensions() {
  355. enabled_instance_extension_names.clear();
  356. // Make sure our core extensions are here
  357. register_requested_instance_extension(VK_KHR_SURFACE_EXTENSION_NAME, true);
  358. register_requested_instance_extension(_get_platform_surface_extension(), true);
  359. if (_use_validation_layers()) {
  360. register_requested_instance_extension(VK_EXT_DEBUG_REPORT_EXTENSION_NAME, false);
  361. }
  362. // This extension allows us to use the properties2 features to query additional device capabilities
  363. register_requested_instance_extension(VK_KHR_GET_PHYSICAL_DEVICE_PROPERTIES_2_EXTENSION_NAME, false);
  364. // Only enable debug utils in verbose mode or DEV_ENABLED.
  365. // End users would get spammed with messages of varying verbosity due to the
  366. // mess that thirdparty layers/extensions and drivers seem to leave in their
  367. // wake, making the Windows registry a bottomless pit of broken layer JSON.
  368. #ifdef DEV_ENABLED
  369. bool want_debug_utils = true;
  370. #else
  371. bool want_debug_utils = OS::get_singleton()->is_stdout_verbose();
  372. #endif
  373. if (want_debug_utils) {
  374. register_requested_instance_extension(VK_EXT_DEBUG_UTILS_EXTENSION_NAME, false);
  375. }
  376. // Load instance extensions that are available...
  377. uint32_t instance_extension_count = 0;
  378. VkResult err = vkEnumerateInstanceExtensionProperties(nullptr, &instance_extension_count, nullptr);
  379. ERR_FAIL_COND_V(err != VK_SUCCESS && err != VK_INCOMPLETE, ERR_CANT_CREATE);
  380. ERR_FAIL_COND_V_MSG(instance_extension_count == 0, ERR_CANT_CREATE, "No instance extensions found, is a driver installed?");
  381. VkExtensionProperties *instance_extensions = (VkExtensionProperties *)malloc(sizeof(VkExtensionProperties) * instance_extension_count);
  382. err = vkEnumerateInstanceExtensionProperties(nullptr, &instance_extension_count, instance_extensions);
  383. if (err != VK_SUCCESS && err != VK_INCOMPLETE) {
  384. free(instance_extensions);
  385. ERR_FAIL_V(ERR_CANT_CREATE);
  386. }
  387. #ifdef DEV_ENABLED
  388. for (uint32_t i = 0; i < instance_extension_count; i++) {
  389. print_verbose(String("VULKAN: Found instance extension ") + String(instance_extensions[i].extensionName));
  390. }
  391. #endif
  392. // Enable all extensions that are supported and requested
  393. for (uint32_t i = 0; i < instance_extension_count; i++) {
  394. CharString extension_name(instance_extensions[i].extensionName);
  395. if (requested_instance_extensions.has(extension_name)) {
  396. enabled_instance_extension_names.insert(extension_name);
  397. }
  398. }
  399. // Now check our requested extensions
  400. for (KeyValue<CharString, bool> &requested_extension : requested_instance_extensions) {
  401. if (!enabled_instance_extension_names.has(requested_extension.key)) {
  402. if (requested_extension.value) {
  403. free(instance_extensions);
  404. ERR_FAIL_V_MSG(ERR_BUG, String("Required extension ") + String(requested_extension.key) + String(" not found, is a driver installed?"));
  405. } else {
  406. print_verbose(String("Optional extension ") + String(requested_extension.key) + String(" not found"));
  407. }
  408. }
  409. }
  410. free(instance_extensions);
  411. instance_extensions_initialized = true;
  412. return OK;
  413. }
  414. bool VulkanContext::device_extensions_initialized = false;
  415. HashMap<CharString, bool> VulkanContext::requested_device_extensions;
  416. void VulkanContext::register_requested_device_extension(const CharString &extension_name, bool p_required) {
  417. ERR_FAIL_COND_MSG(device_extensions_initialized, "You can only registered extensions before the Vulkan instance is created");
  418. ERR_FAIL_COND(requested_device_extensions.has(extension_name));
  419. requested_device_extensions[extension_name] = p_required;
  420. }
  421. Error VulkanContext::_initialize_device_extensions() {
  422. // Look for device extensions.
  423. enabled_device_extension_names.clear();
  424. // Make sure our core extensions are here
  425. register_requested_device_extension(VK_KHR_SWAPCHAIN_EXTENSION_NAME, true);
  426. register_requested_device_extension(VK_KHR_MULTIVIEW_EXTENSION_NAME, false);
  427. register_requested_device_extension(VK_KHR_FRAGMENT_SHADING_RATE_EXTENSION_NAME, false);
  428. register_requested_device_extension(VK_KHR_CREATE_RENDERPASS_2_EXTENSION_NAME, false);
  429. register_requested_device_extension(VK_KHR_SHADER_FLOAT16_INT8_EXTENSION_NAME, false);
  430. register_requested_device_extension(VK_KHR_STORAGE_BUFFER_STORAGE_CLASS_EXTENSION_NAME, false);
  431. register_requested_device_extension(VK_KHR_16BIT_STORAGE_EXTENSION_NAME, false);
  432. register_requested_device_extension(VK_KHR_IMAGE_FORMAT_LIST_EXTENSION_NAME, false);
  433. register_requested_device_extension(VK_KHR_MAINTENANCE_2_EXTENSION_NAME, false);
  434. // TODO consider the following extensions:
  435. // - VK_KHR_spirv_1_4
  436. // - VK_KHR_swapchain_mutable_format
  437. // - VK_EXT_full_screen_exclusive
  438. // - VK_EXT_hdr_metadata
  439. // - VK_KHR_depth_stencil_resolve
  440. // Even though the user "enabled" the extension via the command
  441. // line, we must make sure that it's enumerated for use with the
  442. // device. Therefore, disable it here, and re-enable it again if
  443. // enumerated.
  444. if (VK_KHR_incremental_present_enabled) {
  445. register_requested_device_extension(VK_KHR_INCREMENTAL_PRESENT_EXTENSION_NAME, false);
  446. }
  447. if (VK_GOOGLE_display_timing_enabled) {
  448. register_requested_device_extension(VK_GOOGLE_DISPLAY_TIMING_EXTENSION_NAME, false);
  449. }
  450. // obtain available device extensions
  451. uint32_t device_extension_count = 0;
  452. VkResult err = vkEnumerateDeviceExtensionProperties(gpu, nullptr, &device_extension_count, nullptr);
  453. ERR_FAIL_COND_V(err, ERR_CANT_CREATE);
  454. ERR_FAIL_COND_V_MSG(device_extension_count == 0, ERR_CANT_CREATE,
  455. "vkEnumerateDeviceExtensionProperties failed to find any extensions\n\n"
  456. "Do you have a compatible Vulkan installable client driver (ICD) installed?\n"
  457. "vkCreateInstance Failure");
  458. VkExtensionProperties *device_extensions = (VkExtensionProperties *)malloc(sizeof(VkExtensionProperties) * device_extension_count);
  459. err = vkEnumerateDeviceExtensionProperties(gpu, nullptr, &device_extension_count, device_extensions);
  460. if (err) {
  461. free(device_extensions);
  462. ERR_FAIL_V(ERR_CANT_CREATE);
  463. }
  464. #ifdef DEV_ENABLED
  465. for (uint32_t i = 0; i < device_extension_count; i++) {
  466. print_verbose(String("VULKAN: Found device extension ") + String(device_extensions[i].extensionName));
  467. }
  468. #endif
  469. // Enable all extensions that are supported and requested
  470. for (uint32_t i = 0; i < device_extension_count; i++) {
  471. CharString extension_name(device_extensions[i].extensionName);
  472. if (requested_device_extensions.has(extension_name)) {
  473. enabled_device_extension_names.insert(extension_name);
  474. }
  475. }
  476. // Now check our requested extensions
  477. for (KeyValue<CharString, bool> &requested_extension : requested_device_extensions) {
  478. if (!enabled_device_extension_names.has(requested_extension.key)) {
  479. if (requested_extension.value) {
  480. free(device_extensions);
  481. ERR_FAIL_V_MSG(ERR_BUG,
  482. String("vkEnumerateDeviceExtensionProperties failed to find the ") + String(requested_extension.key) + String(" extension.\n\nDo you have a compatible Vulkan installable client driver (ICD) installed?\nvkCreateInstance Failure"));
  483. } else {
  484. print_verbose(String("Optional extension ") + String(requested_extension.key) + String(" not found"));
  485. }
  486. }
  487. }
  488. free(device_extensions);
  489. device_extensions_initialized = true;
  490. return OK;
  491. }
  492. uint32_t VulkanContext::SubgroupCapabilities::supported_stages_flags_rd() const {
  493. uint32_t flags = 0;
  494. if (supportedStages & VK_SHADER_STAGE_VERTEX_BIT) {
  495. flags += RenderingDevice::ShaderStage::SHADER_STAGE_VERTEX_BIT;
  496. }
  497. if (supportedStages & VK_SHADER_STAGE_TESSELLATION_CONTROL_BIT) {
  498. flags += RenderingDevice::ShaderStage::SHADER_STAGE_TESSELATION_CONTROL_BIT;
  499. }
  500. if (supportedStages & VK_SHADER_STAGE_TESSELLATION_EVALUATION_BIT) {
  501. flags += RenderingDevice::ShaderStage::SHADER_STAGE_TESSELATION_EVALUATION_BIT;
  502. }
  503. // if (supportedStages & VK_SHADER_STAGE_GEOMETRY_BIT) {
  504. // flags += RenderingDevice::ShaderStage::SHADER_STAGE_GEOMETRY_BIT;
  505. // }
  506. if (supportedStages & VK_SHADER_STAGE_FRAGMENT_BIT) {
  507. flags += RenderingDevice::ShaderStage::SHADER_STAGE_FRAGMENT_BIT;
  508. }
  509. if (supportedStages & VK_SHADER_STAGE_COMPUTE_BIT) {
  510. flags += RenderingDevice::ShaderStage::SHADER_STAGE_COMPUTE_BIT;
  511. }
  512. return flags;
  513. }
  514. String VulkanContext::SubgroupCapabilities::supported_stages_desc() const {
  515. String res;
  516. if (supportedStages & VK_SHADER_STAGE_VERTEX_BIT) {
  517. res += ", STAGE_VERTEX";
  518. }
  519. if (supportedStages & VK_SHADER_STAGE_TESSELLATION_CONTROL_BIT) {
  520. res += ", STAGE_TESSELLATION_CONTROL";
  521. }
  522. if (supportedStages & VK_SHADER_STAGE_TESSELLATION_EVALUATION_BIT) {
  523. res += ", STAGE_TESSELLATION_EVALUATION";
  524. }
  525. if (supportedStages & VK_SHADER_STAGE_GEOMETRY_BIT) {
  526. res += ", STAGE_GEOMETRY";
  527. }
  528. if (supportedStages & VK_SHADER_STAGE_FRAGMENT_BIT) {
  529. res += ", STAGE_FRAGMENT";
  530. }
  531. if (supportedStages & VK_SHADER_STAGE_COMPUTE_BIT) {
  532. res += ", STAGE_COMPUTE";
  533. }
  534. // These are not defined on Android GRMBL.
  535. if (supportedStages & 0x00000100 /* VK_SHADER_STAGE_RAYGEN_BIT_KHR */) {
  536. res += ", STAGE_RAYGEN_KHR";
  537. }
  538. if (supportedStages & 0x00000200 /* VK_SHADER_STAGE_ANY_HIT_BIT_KHR */) {
  539. res += ", STAGE_ANY_HIT_KHR";
  540. }
  541. if (supportedStages & 0x00000400 /* VK_SHADER_STAGE_CLOSEST_HIT_BIT_KHR */) {
  542. res += ", STAGE_CLOSEST_HIT_KHR";
  543. }
  544. if (supportedStages & 0x00000800 /* VK_SHADER_STAGE_MISS_BIT_KHR */) {
  545. res += ", STAGE_MISS_KHR";
  546. }
  547. if (supportedStages & 0x00001000 /* VK_SHADER_STAGE_INTERSECTION_BIT_KHR */) {
  548. res += ", STAGE_INTERSECTION_KHR";
  549. }
  550. if (supportedStages & 0x00002000 /* VK_SHADER_STAGE_CALLABLE_BIT_KHR */) {
  551. res += ", STAGE_CALLABLE_KHR";
  552. }
  553. if (supportedStages & 0x00000040 /* VK_SHADER_STAGE_TASK_BIT_NV */) {
  554. res += ", STAGE_TASK_NV";
  555. }
  556. if (supportedStages & 0x00000080 /* VK_SHADER_STAGE_MESH_BIT_NV */) {
  557. res += ", STAGE_MESH_NV";
  558. }
  559. return res.substr(2); // Remove first ", ".
  560. }
  561. uint32_t VulkanContext::SubgroupCapabilities::supported_operations_flags_rd() const {
  562. uint32_t flags = 0;
  563. if (supportedOperations & VK_SUBGROUP_FEATURE_BASIC_BIT) {
  564. flags += RenderingDevice::SubgroupOperations::SUBGROUP_BASIC_BIT;
  565. }
  566. if (supportedOperations & VK_SUBGROUP_FEATURE_VOTE_BIT) {
  567. flags += RenderingDevice::SubgroupOperations::SUBGROUP_VOTE_BIT;
  568. }
  569. if (supportedOperations & VK_SUBGROUP_FEATURE_ARITHMETIC_BIT) {
  570. flags += RenderingDevice::SubgroupOperations::SUBGROUP_ARITHMETIC_BIT;
  571. }
  572. if (supportedOperations & VK_SUBGROUP_FEATURE_BALLOT_BIT) {
  573. flags += RenderingDevice::SubgroupOperations::SUBGROUP_BALLOT_BIT;
  574. }
  575. if (supportedOperations & VK_SUBGROUP_FEATURE_SHUFFLE_BIT) {
  576. flags += RenderingDevice::SubgroupOperations::SUBGROUP_SHUFFLE_BIT;
  577. }
  578. if (supportedOperations & VK_SUBGROUP_FEATURE_SHUFFLE_RELATIVE_BIT) {
  579. flags += RenderingDevice::SubgroupOperations::SUBGROUP_SHUFFLE_RELATIVE_BIT;
  580. }
  581. if (supportedOperations & VK_SUBGROUP_FEATURE_CLUSTERED_BIT) {
  582. flags += RenderingDevice::SubgroupOperations::SUBGROUP_CLUSTERED_BIT;
  583. }
  584. if (supportedOperations & VK_SUBGROUP_FEATURE_QUAD_BIT) {
  585. flags += RenderingDevice::SubgroupOperations::SUBGROUP_QUAD_BIT;
  586. }
  587. return flags;
  588. }
  589. String VulkanContext::SubgroupCapabilities::supported_operations_desc() const {
  590. String res;
  591. if (supportedOperations & VK_SUBGROUP_FEATURE_BASIC_BIT) {
  592. res += ", FEATURE_BASIC";
  593. }
  594. if (supportedOperations & VK_SUBGROUP_FEATURE_VOTE_BIT) {
  595. res += ", FEATURE_VOTE";
  596. }
  597. if (supportedOperations & VK_SUBGROUP_FEATURE_ARITHMETIC_BIT) {
  598. res += ", FEATURE_ARITHMETIC";
  599. }
  600. if (supportedOperations & VK_SUBGROUP_FEATURE_BALLOT_BIT) {
  601. res += ", FEATURE_BALLOT";
  602. }
  603. if (supportedOperations & VK_SUBGROUP_FEATURE_SHUFFLE_BIT) {
  604. res += ", FEATURE_SHUFFLE";
  605. }
  606. if (supportedOperations & VK_SUBGROUP_FEATURE_SHUFFLE_RELATIVE_BIT) {
  607. res += ", FEATURE_SHUFFLE_RELATIVE";
  608. }
  609. if (supportedOperations & VK_SUBGROUP_FEATURE_CLUSTERED_BIT) {
  610. res += ", FEATURE_CLUSTERED";
  611. }
  612. if (supportedOperations & VK_SUBGROUP_FEATURE_QUAD_BIT) {
  613. res += ", FEATURE_QUAD";
  614. }
  615. if (supportedOperations & VK_SUBGROUP_FEATURE_PARTITIONED_BIT_NV) {
  616. res += ", FEATURE_PARTITIONED_NV";
  617. }
  618. return res.substr(2); // Remove first ", ".
  619. }
  620. Error VulkanContext::_check_capabilities() {
  621. // https://www.khronos.org/registry/vulkan/specs/1.2-extensions/man/html/VK_KHR_multiview.html
  622. // https://www.khronos.org/blog/vulkan-subgroup-tutorial
  623. // For Vulkan 1.0 vkGetPhysicalDeviceProperties2 is not available, including not in the loader we compile against on Android.
  624. // So we check if the functions are accessible by getting their function pointers and skipping if not
  625. // (note that the desktop loader does a better job here but the android loader doesn't.)
  626. // Assume not supported until proven otherwise.
  627. vrs_capabilities.pipeline_vrs_supported = false;
  628. vrs_capabilities.primitive_vrs_supported = false;
  629. vrs_capabilities.attachment_vrs_supported = false;
  630. vrs_capabilities.min_texel_size = Size2i();
  631. vrs_capabilities.max_texel_size = Size2i();
  632. vrs_capabilities.texel_size = Size2i();
  633. multiview_capabilities.is_supported = false;
  634. multiview_capabilities.geometry_shader_is_supported = false;
  635. multiview_capabilities.tessellation_shader_is_supported = false;
  636. multiview_capabilities.max_view_count = 0;
  637. multiview_capabilities.max_instance_count = 0;
  638. subgroup_capabilities.size = 0;
  639. subgroup_capabilities.supportedStages = 0;
  640. subgroup_capabilities.supportedOperations = 0;
  641. subgroup_capabilities.quadOperationsInAllStages = false;
  642. shader_capabilities.shader_float16_is_supported = false;
  643. shader_capabilities.shader_int8_is_supported = false;
  644. storage_buffer_capabilities.storage_buffer_16_bit_access_is_supported = false;
  645. storage_buffer_capabilities.uniform_and_storage_buffer_16_bit_access_is_supported = false;
  646. storage_buffer_capabilities.storage_push_constant_16_is_supported = false;
  647. storage_buffer_capabilities.storage_input_output_16 = false;
  648. if (is_instance_extension_enabled(VK_KHR_GET_PHYSICAL_DEVICE_PROPERTIES_2_EXTENSION_NAME)) {
  649. // Check for extended features.
  650. PFN_vkGetPhysicalDeviceFeatures2 vkGetPhysicalDeviceFeatures2_func = (PFN_vkGetPhysicalDeviceFeatures2)vkGetInstanceProcAddr(inst, "vkGetPhysicalDeviceFeatures2");
  651. if (vkGetPhysicalDeviceFeatures2_func == nullptr) {
  652. // In Vulkan 1.0 might be accessible under its original extension name.
  653. vkGetPhysicalDeviceFeatures2_func = (PFN_vkGetPhysicalDeviceFeatures2)vkGetInstanceProcAddr(inst, "vkGetPhysicalDeviceFeatures2KHR");
  654. }
  655. if (vkGetPhysicalDeviceFeatures2_func != nullptr) {
  656. // Check our extended features.
  657. void *next = nullptr;
  658. // We must check that the relative extension is present before assuming a
  659. // feature as enabled.
  660. // See also: https://github.com/godotengine/godot/issues/65409
  661. VkPhysicalDeviceVulkan12Features device_features_vk12 = {};
  662. VkPhysicalDeviceShaderFloat16Int8FeaturesKHR shader_features = {};
  663. VkPhysicalDeviceFragmentShadingRateFeaturesKHR vrs_features = {};
  664. VkPhysicalDevice16BitStorageFeaturesKHR storage_feature = {};
  665. VkPhysicalDeviceMultiviewFeatures multiview_features = {};
  666. if (device_api_version >= VK_API_VERSION_1_2) {
  667. device_features_vk12.sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_VULKAN_1_2_FEATURES;
  668. device_features_vk12.pNext = next;
  669. next = &device_features_vk12;
  670. } else {
  671. if (is_device_extension_enabled(VK_KHR_SHADER_FLOAT16_INT8_EXTENSION_NAME)) {
  672. shader_features = {
  673. /*sType*/ VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_SHADER_FLOAT16_INT8_FEATURES_KHR,
  674. /*pNext*/ next,
  675. /*shaderFloat16*/ false,
  676. /*shaderInt8*/ false,
  677. };
  678. next = &shader_features;
  679. }
  680. }
  681. if (is_device_extension_enabled(VK_KHR_FRAGMENT_SHADING_RATE_EXTENSION_NAME)) {
  682. vrs_features = {
  683. /*sType*/ VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_FRAGMENT_SHADING_RATE_FEATURES_KHR,
  684. /*pNext*/ next,
  685. /*pipelineFragmentShadingRate*/ false,
  686. /*primitiveFragmentShadingRate*/ false,
  687. /*attachmentFragmentShadingRate*/ false,
  688. };
  689. next = &vrs_features;
  690. }
  691. if (is_device_extension_enabled(VK_KHR_16BIT_STORAGE_EXTENSION_NAME)) {
  692. storage_feature = {
  693. /*sType*/ VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_16BIT_STORAGE_FEATURES_KHR,
  694. /*pNext*/ next,
  695. /*storageBuffer16BitAccess*/ false,
  696. /*uniformAndStorageBuffer16BitAccess*/ false,
  697. /*storagePushConstant16*/ false,
  698. /*storageInputOutput16*/ false,
  699. };
  700. next = &storage_feature;
  701. }
  702. if (is_device_extension_enabled(VK_KHR_MULTIVIEW_EXTENSION_NAME)) {
  703. multiview_features = {
  704. /*sType*/ VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_MULTIVIEW_FEATURES,
  705. /*pNext*/ next,
  706. /*multiview*/ false,
  707. /*multiviewGeometryShader*/ false,
  708. /*multiviewTessellationShader*/ false,
  709. };
  710. next = &multiview_features;
  711. }
  712. VkPhysicalDeviceFeatures2 device_features;
  713. device_features.sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_FEATURES_2;
  714. device_features.pNext = next;
  715. vkGetPhysicalDeviceFeatures2_func(gpu, &device_features);
  716. if (device_api_version >= VK_API_VERSION_1_2) {
  717. #ifdef MACOS_ENABLED
  718. ERR_FAIL_COND_V_MSG(!device_features_vk12.shaderSampledImageArrayNonUniformIndexing, ERR_CANT_CREATE, "Your GPU doesn't support shaderSampledImageArrayNonUniformIndexing which is required to use the Vulkan-based renderers in Godot.");
  719. #endif
  720. if (is_device_extension_enabled(VK_KHR_SHADER_FLOAT16_INT8_EXTENSION_NAME)) {
  721. shader_capabilities.shader_float16_is_supported = device_features_vk12.shaderFloat16;
  722. shader_capabilities.shader_int8_is_supported = device_features_vk12.shaderInt8;
  723. }
  724. } else {
  725. if (is_device_extension_enabled(VK_KHR_SHADER_FLOAT16_INT8_EXTENSION_NAME)) {
  726. shader_capabilities.shader_float16_is_supported = shader_features.shaderFloat16;
  727. shader_capabilities.shader_int8_is_supported = shader_features.shaderInt8;
  728. }
  729. }
  730. if (is_device_extension_enabled(VK_KHR_FRAGMENT_SHADING_RATE_EXTENSION_NAME)) {
  731. vrs_capabilities.pipeline_vrs_supported = vrs_features.pipelineFragmentShadingRate;
  732. vrs_capabilities.primitive_vrs_supported = vrs_features.primitiveFragmentShadingRate;
  733. vrs_capabilities.attachment_vrs_supported = vrs_features.attachmentFragmentShadingRate;
  734. }
  735. if (is_device_extension_enabled(VK_KHR_MULTIVIEW_EXTENSION_NAME)) {
  736. multiview_capabilities.is_supported = multiview_features.multiview;
  737. multiview_capabilities.geometry_shader_is_supported = multiview_features.multiviewGeometryShader;
  738. multiview_capabilities.tessellation_shader_is_supported = multiview_features.multiviewTessellationShader;
  739. }
  740. if (is_device_extension_enabled(VK_KHR_16BIT_STORAGE_EXTENSION_NAME)) {
  741. storage_buffer_capabilities.storage_buffer_16_bit_access_is_supported = storage_feature.storageBuffer16BitAccess;
  742. storage_buffer_capabilities.uniform_and_storage_buffer_16_bit_access_is_supported = storage_feature.uniformAndStorageBuffer16BitAccess;
  743. storage_buffer_capabilities.storage_push_constant_16_is_supported = storage_feature.storagePushConstant16;
  744. storage_buffer_capabilities.storage_input_output_16 = storage_feature.storageInputOutput16;
  745. }
  746. }
  747. // Check extended properties.
  748. PFN_vkGetPhysicalDeviceProperties2 device_properties_func = (PFN_vkGetPhysicalDeviceProperties2)vkGetInstanceProcAddr(inst, "vkGetPhysicalDeviceProperties2");
  749. if (device_properties_func == nullptr) {
  750. // In Vulkan 1.0 might be accessible under its original extension name.
  751. device_properties_func = (PFN_vkGetPhysicalDeviceProperties2)vkGetInstanceProcAddr(inst, "vkGetPhysicalDeviceProperties2KHR");
  752. }
  753. if (device_properties_func != nullptr) {
  754. VkPhysicalDeviceFragmentShadingRatePropertiesKHR vrsProperties{};
  755. VkPhysicalDeviceMultiviewProperties multiviewProperties{};
  756. VkPhysicalDeviceSubgroupProperties subgroupProperties{};
  757. VkPhysicalDeviceProperties2 physicalDeviceProperties{};
  758. void *nextptr = nullptr;
  759. if (device_api_version >= VK_API_VERSION_1_1) { // Vulkan 1.1 or higher
  760. subgroupProperties.sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_SUBGROUP_PROPERTIES;
  761. subgroupProperties.pNext = nextptr;
  762. nextptr = &subgroupProperties;
  763. }
  764. if (multiview_capabilities.is_supported) {
  765. multiviewProperties.sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_MULTIVIEW_PROPERTIES;
  766. multiviewProperties.pNext = nextptr;
  767. nextptr = &multiviewProperties;
  768. }
  769. if (vrs_capabilities.attachment_vrs_supported) {
  770. vrsProperties.sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_FRAGMENT_SHADING_RATE_PROPERTIES_KHR;
  771. vrsProperties.pNext = nextptr;
  772. nextptr = &vrsProperties;
  773. }
  774. physicalDeviceProperties.sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_PROPERTIES_2;
  775. physicalDeviceProperties.pNext = nextptr;
  776. device_properties_func(gpu, &physicalDeviceProperties);
  777. subgroup_capabilities.size = subgroupProperties.subgroupSize;
  778. subgroup_capabilities.supportedStages = subgroupProperties.supportedStages;
  779. subgroup_capabilities.supportedOperations = subgroupProperties.supportedOperations;
  780. // Note: quadOperationsInAllStages will be true if:
  781. // - supportedStages has VK_SHADER_STAGE_ALL_GRAPHICS + VK_SHADER_STAGE_COMPUTE_BIT.
  782. // - supportedOperations has VK_SUBGROUP_FEATURE_QUAD_BIT.
  783. subgroup_capabilities.quadOperationsInAllStages = subgroupProperties.quadOperationsInAllStages;
  784. if (vrs_capabilities.pipeline_vrs_supported || vrs_capabilities.primitive_vrs_supported || vrs_capabilities.attachment_vrs_supported) {
  785. print_verbose("- Vulkan Variable Rate Shading supported:");
  786. if (vrs_capabilities.pipeline_vrs_supported) {
  787. print_verbose(" Pipeline fragment shading rate");
  788. }
  789. if (vrs_capabilities.primitive_vrs_supported) {
  790. print_verbose(" Primitive fragment shading rate");
  791. }
  792. if (vrs_capabilities.attachment_vrs_supported) {
  793. // TODO expose these somehow to the end user.
  794. vrs_capabilities.min_texel_size.x = vrsProperties.minFragmentShadingRateAttachmentTexelSize.width;
  795. vrs_capabilities.min_texel_size.y = vrsProperties.minFragmentShadingRateAttachmentTexelSize.height;
  796. vrs_capabilities.max_texel_size.x = vrsProperties.maxFragmentShadingRateAttachmentTexelSize.width;
  797. vrs_capabilities.max_texel_size.y = vrsProperties.maxFragmentShadingRateAttachmentTexelSize.height;
  798. // We'll attempt to default to a texel size of 16x16
  799. vrs_capabilities.texel_size.x = CLAMP(16, vrs_capabilities.min_texel_size.x, vrs_capabilities.max_texel_size.x);
  800. vrs_capabilities.texel_size.y = CLAMP(16, vrs_capabilities.min_texel_size.y, vrs_capabilities.max_texel_size.y);
  801. print_verbose(String(" Attachment fragment shading rate") + String(", min texel size: (") + itos(vrs_capabilities.min_texel_size.x) + String(", ") + itos(vrs_capabilities.min_texel_size.y) + String(")") + String(", max texel size: (") + itos(vrs_capabilities.max_texel_size.x) + String(", ") + itos(vrs_capabilities.max_texel_size.y) + String(")"));
  802. }
  803. } else {
  804. print_verbose("- Vulkan Variable Rate Shading not supported");
  805. }
  806. if (multiview_capabilities.is_supported) {
  807. multiview_capabilities.max_view_count = multiviewProperties.maxMultiviewViewCount;
  808. multiview_capabilities.max_instance_count = multiviewProperties.maxMultiviewInstanceIndex;
  809. print_verbose("- Vulkan multiview supported:");
  810. print_verbose(" max view count: " + itos(multiview_capabilities.max_view_count));
  811. print_verbose(" max instances: " + itos(multiview_capabilities.max_instance_count));
  812. } else {
  813. print_verbose("- Vulkan multiview not supported");
  814. }
  815. print_verbose("- Vulkan subgroup:");
  816. print_verbose(" size: " + itos(subgroup_capabilities.size));
  817. print_verbose(" stages: " + subgroup_capabilities.supported_stages_desc());
  818. print_verbose(" supported ops: " + subgroup_capabilities.supported_operations_desc());
  819. if (subgroup_capabilities.quadOperationsInAllStages) {
  820. print_verbose(" quad operations in all stages");
  821. }
  822. } else {
  823. print_verbose("- Couldn't call vkGetPhysicalDeviceProperties2");
  824. }
  825. }
  826. return OK;
  827. }
  828. Error VulkanContext::_create_instance() {
  829. // Obtain Vulkan version.
  830. _obtain_vulkan_version();
  831. // Initialize extensions.
  832. {
  833. Error err = _initialize_instance_extensions();
  834. if (err != OK) {
  835. return err;
  836. }
  837. }
  838. int enabled_extension_count = 0;
  839. const char *enabled_extension_names[MAX_EXTENSIONS];
  840. ERR_FAIL_COND_V(enabled_instance_extension_names.size() > MAX_EXTENSIONS, ERR_CANT_CREATE);
  841. for (const CharString &extension_name : enabled_instance_extension_names) {
  842. enabled_extension_names[enabled_extension_count++] = extension_name.ptr();
  843. }
  844. // We'll set application version to the Vulkan version we're developing against, even if our instance is based on
  845. // an older Vulkan version, devices can still support newer versions of Vulkan.
  846. // The exception is when we're on Vulkan 1.0, we should not set this to anything but 1.0.
  847. // Note that this value is only used by validation layers to warn us about version issues.
  848. uint32_t application_api_version = instance_api_version == VK_API_VERSION_1_0 ? VK_API_VERSION_1_0 : VK_API_VERSION_1_2;
  849. CharString cs = GLOBAL_GET("application/config/name").operator String().utf8();
  850. const VkApplicationInfo app = {
  851. /*sType*/ VK_STRUCTURE_TYPE_APPLICATION_INFO,
  852. /*pNext*/ nullptr,
  853. /*pApplicationName*/ cs.get_data(),
  854. /*applicationVersion*/ 0, // It would be really nice if we store a version number in project settings, say "application/config/version"
  855. /*pEngineName*/ VERSION_NAME,
  856. /*engineVersion*/ VK_MAKE_VERSION(VERSION_MAJOR, VERSION_MINOR, VERSION_PATCH),
  857. /*apiVersion*/ application_api_version
  858. };
  859. VkInstanceCreateInfo inst_info{};
  860. inst_info.sType = VK_STRUCTURE_TYPE_INSTANCE_CREATE_INFO;
  861. inst_info.pApplicationInfo = &app;
  862. inst_info.enabledExtensionCount = enabled_extension_count;
  863. inst_info.ppEnabledExtensionNames = (const char *const *)enabled_extension_names;
  864. if (_use_validation_layers()) {
  865. _get_preferred_validation_layers(&inst_info.enabledLayerCount, &inst_info.ppEnabledLayerNames);
  866. }
  867. /*
  868. * This is info for a temp callback to use during CreateInstance.
  869. * After the instance is created, we use the instance-based
  870. * function to register the final callback.
  871. */
  872. VkDebugUtilsMessengerCreateInfoEXT dbg_messenger_create_info = {};
  873. VkDebugReportCallbackCreateInfoEXT dbg_report_callback_create_info = {};
  874. if (is_instance_extension_enabled(VK_EXT_DEBUG_UTILS_EXTENSION_NAME)) {
  875. // VK_EXT_debug_utils style.
  876. dbg_messenger_create_info.sType = VK_STRUCTURE_TYPE_DEBUG_UTILS_MESSENGER_CREATE_INFO_EXT;
  877. dbg_messenger_create_info.pNext = nullptr;
  878. dbg_messenger_create_info.flags = 0;
  879. dbg_messenger_create_info.messageSeverity =
  880. VK_DEBUG_UTILS_MESSAGE_SEVERITY_WARNING_BIT_EXT | VK_DEBUG_UTILS_MESSAGE_SEVERITY_ERROR_BIT_EXT;
  881. dbg_messenger_create_info.messageType = VK_DEBUG_UTILS_MESSAGE_TYPE_GENERAL_BIT_EXT |
  882. VK_DEBUG_UTILS_MESSAGE_TYPE_VALIDATION_BIT_EXT |
  883. VK_DEBUG_UTILS_MESSAGE_TYPE_PERFORMANCE_BIT_EXT;
  884. dbg_messenger_create_info.pfnUserCallback = _debug_messenger_callback;
  885. dbg_messenger_create_info.pUserData = this;
  886. inst_info.pNext = &dbg_messenger_create_info;
  887. } else if (is_instance_extension_enabled(VK_EXT_DEBUG_REPORT_EXTENSION_NAME)) {
  888. dbg_report_callback_create_info.sType = VK_STRUCTURE_TYPE_DEBUG_REPORT_CALLBACK_CREATE_INFO_EXT;
  889. dbg_report_callback_create_info.flags = VK_DEBUG_REPORT_INFORMATION_BIT_EXT |
  890. VK_DEBUG_REPORT_WARNING_BIT_EXT |
  891. VK_DEBUG_REPORT_PERFORMANCE_WARNING_BIT_EXT |
  892. VK_DEBUG_REPORT_ERROR_BIT_EXT |
  893. VK_DEBUG_REPORT_DEBUG_BIT_EXT;
  894. dbg_report_callback_create_info.pfnCallback = _debug_report_callback;
  895. dbg_report_callback_create_info.pUserData = this;
  896. inst_info.pNext = &dbg_report_callback_create_info;
  897. }
  898. VkResult err;
  899. if (vulkan_hooks) {
  900. if (!vulkan_hooks->create_vulkan_instance(&inst_info, &inst)) {
  901. return ERR_CANT_CREATE;
  902. }
  903. } else {
  904. err = vkCreateInstance(&inst_info, nullptr, &inst);
  905. ERR_FAIL_COND_V_MSG(err == VK_ERROR_INCOMPATIBLE_DRIVER, ERR_CANT_CREATE,
  906. "Cannot find a compatible Vulkan installable client driver (ICD).\n\n"
  907. "vkCreateInstance Failure");
  908. ERR_FAIL_COND_V_MSG(err == VK_ERROR_EXTENSION_NOT_PRESENT, ERR_CANT_CREATE,
  909. "Cannot find a specified extension library.\n"
  910. "Make sure your layers path is set appropriately.\n"
  911. "vkCreateInstance Failure");
  912. ERR_FAIL_COND_V_MSG(err, ERR_CANT_CREATE,
  913. "vkCreateInstance failed.\n\n"
  914. "Do you have a compatible Vulkan installable client driver (ICD) installed?\n"
  915. "Please look at the Getting Started guide for additional information.\n"
  916. "vkCreateInstance Failure");
  917. }
  918. inst_initialized = true;
  919. #ifdef USE_VOLK
  920. volkLoadInstance(inst);
  921. #endif
  922. if (is_instance_extension_enabled(VK_EXT_DEBUG_UTILS_EXTENSION_NAME)) {
  923. // Setup VK_EXT_debug_utils function pointers always (we use them for debug labels and names).
  924. CreateDebugUtilsMessengerEXT =
  925. (PFN_vkCreateDebugUtilsMessengerEXT)vkGetInstanceProcAddr(inst, "vkCreateDebugUtilsMessengerEXT");
  926. DestroyDebugUtilsMessengerEXT =
  927. (PFN_vkDestroyDebugUtilsMessengerEXT)vkGetInstanceProcAddr(inst, "vkDestroyDebugUtilsMessengerEXT");
  928. SubmitDebugUtilsMessageEXT =
  929. (PFN_vkSubmitDebugUtilsMessageEXT)vkGetInstanceProcAddr(inst, "vkSubmitDebugUtilsMessageEXT");
  930. CmdBeginDebugUtilsLabelEXT =
  931. (PFN_vkCmdBeginDebugUtilsLabelEXT)vkGetInstanceProcAddr(inst, "vkCmdBeginDebugUtilsLabelEXT");
  932. CmdEndDebugUtilsLabelEXT =
  933. (PFN_vkCmdEndDebugUtilsLabelEXT)vkGetInstanceProcAddr(inst, "vkCmdEndDebugUtilsLabelEXT");
  934. CmdInsertDebugUtilsLabelEXT =
  935. (PFN_vkCmdInsertDebugUtilsLabelEXT)vkGetInstanceProcAddr(inst, "vkCmdInsertDebugUtilsLabelEXT");
  936. SetDebugUtilsObjectNameEXT =
  937. (PFN_vkSetDebugUtilsObjectNameEXT)vkGetInstanceProcAddr(inst, "vkSetDebugUtilsObjectNameEXT");
  938. if (nullptr == CreateDebugUtilsMessengerEXT || nullptr == DestroyDebugUtilsMessengerEXT ||
  939. nullptr == SubmitDebugUtilsMessageEXT || nullptr == CmdBeginDebugUtilsLabelEXT ||
  940. nullptr == CmdEndDebugUtilsLabelEXT || nullptr == CmdInsertDebugUtilsLabelEXT ||
  941. nullptr == SetDebugUtilsObjectNameEXT) {
  942. ERR_FAIL_V_MSG(ERR_CANT_CREATE,
  943. "GetProcAddr: Failed to init VK_EXT_debug_utils\n"
  944. "GetProcAddr: Failure");
  945. }
  946. err = CreateDebugUtilsMessengerEXT(inst, &dbg_messenger_create_info, nullptr, &dbg_messenger);
  947. switch (err) {
  948. case VK_SUCCESS:
  949. break;
  950. case VK_ERROR_OUT_OF_HOST_MEMORY:
  951. ERR_FAIL_V_MSG(ERR_CANT_CREATE,
  952. "CreateDebugUtilsMessengerEXT: out of host memory\n"
  953. "CreateDebugUtilsMessengerEXT Failure");
  954. break;
  955. default:
  956. ERR_FAIL_V_MSG(ERR_CANT_CREATE,
  957. "CreateDebugUtilsMessengerEXT: unknown failure\n"
  958. "CreateDebugUtilsMessengerEXT Failure");
  959. ERR_FAIL_V(ERR_CANT_CREATE);
  960. break;
  961. }
  962. } else if (is_instance_extension_enabled(VK_EXT_DEBUG_REPORT_EXTENSION_NAME)) {
  963. CreateDebugReportCallbackEXT = (PFN_vkCreateDebugReportCallbackEXT)vkGetInstanceProcAddr(inst, "vkCreateDebugReportCallbackEXT");
  964. DebugReportMessageEXT = (PFN_vkDebugReportMessageEXT)vkGetInstanceProcAddr(inst, "vkDebugReportMessageEXT");
  965. DestroyDebugReportCallbackEXT = (PFN_vkDestroyDebugReportCallbackEXT)vkGetInstanceProcAddr(inst, "vkDestroyDebugReportCallbackEXT");
  966. if (nullptr == CreateDebugReportCallbackEXT || nullptr == DebugReportMessageEXT || nullptr == DestroyDebugReportCallbackEXT) {
  967. ERR_FAIL_V_MSG(ERR_CANT_CREATE,
  968. "GetProcAddr: Failed to init VK_EXT_debug_report\n"
  969. "GetProcAddr: Failure");
  970. }
  971. err = CreateDebugReportCallbackEXT(inst, &dbg_report_callback_create_info, nullptr, &dbg_debug_report);
  972. switch (err) {
  973. case VK_SUCCESS:
  974. break;
  975. case VK_ERROR_OUT_OF_HOST_MEMORY:
  976. ERR_FAIL_V_MSG(ERR_CANT_CREATE,
  977. "CreateDebugReportCallbackEXT: out of host memory\n"
  978. "CreateDebugReportCallbackEXT Failure");
  979. break;
  980. default:
  981. ERR_FAIL_V_MSG(ERR_CANT_CREATE,
  982. "CreateDebugReportCallbackEXT: unknown failure\n"
  983. "CreateDebugReportCallbackEXT Failure");
  984. ERR_FAIL_V(ERR_CANT_CREATE);
  985. break;
  986. }
  987. }
  988. return OK;
  989. }
  990. Error VulkanContext::_create_physical_device(VkSurfaceKHR p_surface) {
  991. // Make initial call to query gpu_count, then second call for gpu info.
  992. uint32_t gpu_count = 0;
  993. VkResult err = vkEnumeratePhysicalDevices(inst, &gpu_count, nullptr);
  994. ERR_FAIL_COND_V(err, ERR_CANT_CREATE);
  995. ERR_FAIL_COND_V_MSG(gpu_count == 0, ERR_CANT_CREATE,
  996. "vkEnumeratePhysicalDevices reported zero accessible devices.\n\n"
  997. "Do you have a compatible Vulkan installable client driver (ICD) installed?\n"
  998. "vkEnumeratePhysicalDevices Failure");
  999. VkPhysicalDevice *physical_devices = (VkPhysicalDevice *)malloc(sizeof(VkPhysicalDevice) * gpu_count);
  1000. err = vkEnumeratePhysicalDevices(inst, &gpu_count, physical_devices);
  1001. if (err) {
  1002. free(physical_devices);
  1003. ERR_FAIL_V(ERR_CANT_CREATE);
  1004. }
  1005. static const struct {
  1006. uint32_t id;
  1007. const char *name;
  1008. } vendor_names[] = {
  1009. { 0x1002, "AMD" },
  1010. { 0x1010, "ImgTec" },
  1011. { 0x106B, "Apple" },
  1012. { 0x10DE, "NVIDIA" },
  1013. { 0x13B5, "ARM" },
  1014. { 0x5143, "Qualcomm" },
  1015. { 0x8086, "Intel" },
  1016. { 0, nullptr },
  1017. };
  1018. int32_t device_index = -1;
  1019. if (vulkan_hooks) {
  1020. if (!vulkan_hooks->get_physical_device(&gpu)) {
  1021. return ERR_CANT_CREATE;
  1022. }
  1023. // Not really needed but nice to print the correct entry.
  1024. for (uint32_t i = 0; i < gpu_count; ++i) {
  1025. if (physical_devices[i] == gpu) {
  1026. device_index = i;
  1027. break;
  1028. }
  1029. }
  1030. } else {
  1031. // TODO: At least on Linux Laptops integrated GPUs fail with Vulkan in many instances.
  1032. // The device should really be a preference, but for now choosing a discrete GPU over the
  1033. // integrated one is better than the default.
  1034. int type_selected = -1;
  1035. print_verbose("Vulkan devices:");
  1036. for (uint32_t i = 0; i < gpu_count; ++i) {
  1037. VkPhysicalDeviceProperties props;
  1038. vkGetPhysicalDeviceProperties(physical_devices[i], &props);
  1039. bool present_supported = false;
  1040. uint32_t device_queue_family_count = 0;
  1041. vkGetPhysicalDeviceQueueFamilyProperties(physical_devices[i], &device_queue_family_count, nullptr);
  1042. VkQueueFamilyProperties *device_queue_props = (VkQueueFamilyProperties *)malloc(device_queue_family_count * sizeof(VkQueueFamilyProperties));
  1043. vkGetPhysicalDeviceQueueFamilyProperties(physical_devices[i], &device_queue_family_count, device_queue_props);
  1044. for (uint32_t j = 0; j < device_queue_family_count; j++) {
  1045. VkBool32 supports;
  1046. vkGetPhysicalDeviceSurfaceSupportKHR(physical_devices[i], j, p_surface, &supports);
  1047. if (supports && ((device_queue_props[j].queueFlags & VK_QUEUE_GRAPHICS_BIT) != 0)) {
  1048. present_supported = true;
  1049. } else {
  1050. continue;
  1051. }
  1052. }
  1053. String name = props.deviceName;
  1054. String vendor = "Unknown";
  1055. String dev_type;
  1056. switch (props.deviceType) {
  1057. case VkPhysicalDeviceType::VK_PHYSICAL_DEVICE_TYPE_DISCRETE_GPU: {
  1058. dev_type = "Discrete";
  1059. } break;
  1060. case VkPhysicalDeviceType::VK_PHYSICAL_DEVICE_TYPE_INTEGRATED_GPU: {
  1061. dev_type = "Integrated";
  1062. } break;
  1063. case VkPhysicalDeviceType::VK_PHYSICAL_DEVICE_TYPE_VIRTUAL_GPU: {
  1064. dev_type = "Virtual";
  1065. } break;
  1066. case VkPhysicalDeviceType::VK_PHYSICAL_DEVICE_TYPE_CPU: {
  1067. dev_type = "CPU";
  1068. } break;
  1069. default: {
  1070. dev_type = "Other";
  1071. } break;
  1072. }
  1073. uint32_t vendor_idx = 0;
  1074. while (vendor_names[vendor_idx].name != nullptr) {
  1075. if (props.vendorID == vendor_names[vendor_idx].id) {
  1076. vendor = vendor_names[vendor_idx].name;
  1077. break;
  1078. }
  1079. vendor_idx++;
  1080. }
  1081. free(device_queue_props);
  1082. print_verbose(" #" + itos(i) + ": " + vendor + " " + name + " - " + (present_supported ? "Supported" : "Unsupported") + ", " + dev_type);
  1083. if (present_supported) { // Select first supported device of preferred type: Discrete > Integrated > Virtual > CPU > Other.
  1084. switch (props.deviceType) {
  1085. case VkPhysicalDeviceType::VK_PHYSICAL_DEVICE_TYPE_DISCRETE_GPU: {
  1086. if (type_selected < 4) {
  1087. type_selected = 4;
  1088. device_index = i;
  1089. }
  1090. } break;
  1091. case VkPhysicalDeviceType::VK_PHYSICAL_DEVICE_TYPE_INTEGRATED_GPU: {
  1092. if (type_selected < 3) {
  1093. type_selected = 3;
  1094. device_index = i;
  1095. }
  1096. } break;
  1097. case VkPhysicalDeviceType::VK_PHYSICAL_DEVICE_TYPE_VIRTUAL_GPU: {
  1098. if (type_selected < 2) {
  1099. type_selected = 2;
  1100. device_index = i;
  1101. }
  1102. } break;
  1103. case VkPhysicalDeviceType::VK_PHYSICAL_DEVICE_TYPE_CPU: {
  1104. if (type_selected < 1) {
  1105. type_selected = 1;
  1106. device_index = i;
  1107. }
  1108. } break;
  1109. default: {
  1110. if (type_selected < 0) {
  1111. type_selected = 0;
  1112. device_index = i;
  1113. }
  1114. } break;
  1115. }
  1116. }
  1117. }
  1118. int32_t user_device_index = Engine::get_singleton()->get_gpu_index(); // Force user selected GPU.
  1119. if (user_device_index >= 0 && user_device_index < (int32_t)gpu_count) {
  1120. device_index = user_device_index;
  1121. }
  1122. ERR_FAIL_COND_V_MSG(device_index == -1, ERR_CANT_CREATE, "None of Vulkan devices supports both graphics and present queues.");
  1123. gpu = physical_devices[device_index];
  1124. }
  1125. free(physical_devices);
  1126. // Get identifier properties.
  1127. vkGetPhysicalDeviceProperties(gpu, &gpu_props);
  1128. device_name = gpu_props.deviceName;
  1129. device_type = gpu_props.deviceType;
  1130. pipeline_cache_id = String::hex_encode_buffer(gpu_props.pipelineCacheUUID, VK_UUID_SIZE);
  1131. pipeline_cache_id += "-driver-" + itos(gpu_props.driverVersion);
  1132. {
  1133. device_vendor = "Unknown";
  1134. uint32_t vendor_idx = 0;
  1135. while (vendor_names[vendor_idx].name != nullptr) {
  1136. if (gpu_props.vendorID == vendor_names[vendor_idx].id) {
  1137. device_vendor = vendor_names[vendor_idx].name;
  1138. break;
  1139. }
  1140. vendor_idx++;
  1141. }
  1142. }
  1143. // Get device version
  1144. device_api_version = gpu_props.apiVersion;
  1145. String rendering_method;
  1146. if (OS::get_singleton()->get_current_rendering_method() == "mobile") {
  1147. rendering_method = "Forward Mobile";
  1148. } else {
  1149. rendering_method = "Forward+";
  1150. }
  1151. // Output our device version
  1152. print_line(vformat("Vulkan API %s - %s - Using Vulkan Device #%d: %s - %s", get_device_api_version(), rendering_method, device_index, device_vendor, device_name));
  1153. {
  1154. Error _err = _initialize_device_extensions();
  1155. if (_err != OK) {
  1156. return _err;
  1157. }
  1158. }
  1159. // Call with nullptr data to get count.
  1160. vkGetPhysicalDeviceQueueFamilyProperties(gpu, &queue_family_count, nullptr);
  1161. ERR_FAIL_COND_V(queue_family_count == 0, ERR_CANT_CREATE);
  1162. queue_props = (VkQueueFamilyProperties *)malloc(queue_family_count * sizeof(VkQueueFamilyProperties));
  1163. vkGetPhysicalDeviceQueueFamilyProperties(gpu, &queue_family_count, queue_props);
  1164. // Query fine-grained feature support for this device.
  1165. // If app has specific feature requirements it should check supported
  1166. // features based on this query
  1167. vkGetPhysicalDeviceFeatures(gpu, &physical_device_features);
  1168. // Check required features
  1169. ERR_FAIL_COND_V_MSG(!physical_device_features.imageCubeArray, ERR_CANT_CREATE, "Your GPU doesn't support image cube arrays which are required to use the Vulkan-based renderers in Godot.");
  1170. ERR_FAIL_COND_V_MSG(!physical_device_features.independentBlend, ERR_CANT_CREATE, "Your GPU doesn't support independentBlend which is required to use the Vulkan-based renderers in Godot.");
  1171. physical_device_features.robustBufferAccess = false; // Turn off robust buffer access, which can hamper performance on some hardware.
  1172. #define GET_INSTANCE_PROC_ADDR(inst, entrypoint) \
  1173. { \
  1174. fp##entrypoint = (PFN_vk##entrypoint)vkGetInstanceProcAddr(inst, "vk" #entrypoint); \
  1175. ERR_FAIL_COND_V_MSG(fp##entrypoint == nullptr, ERR_CANT_CREATE, \
  1176. "vkGetInstanceProcAddr failed to find vk" #entrypoint); \
  1177. }
  1178. GET_INSTANCE_PROC_ADDR(inst, GetPhysicalDeviceSurfaceSupportKHR);
  1179. GET_INSTANCE_PROC_ADDR(inst, GetPhysicalDeviceSurfaceCapabilitiesKHR);
  1180. GET_INSTANCE_PROC_ADDR(inst, GetPhysicalDeviceSurfaceFormatsKHR);
  1181. GET_INSTANCE_PROC_ADDR(inst, GetPhysicalDeviceSurfacePresentModesKHR);
  1182. GET_INSTANCE_PROC_ADDR(inst, GetSwapchainImagesKHR);
  1183. // Gets capability info for current Vulkan driver.
  1184. {
  1185. Error res = _check_capabilities();
  1186. if (res != OK) {
  1187. return res;
  1188. }
  1189. }
  1190. device_initialized = true;
  1191. return OK;
  1192. }
  1193. Error VulkanContext::_create_device() {
  1194. VkResult err;
  1195. float queue_priorities[1] = { 0.0 };
  1196. VkDeviceQueueCreateInfo queues[2];
  1197. queues[0].sType = VK_STRUCTURE_TYPE_DEVICE_QUEUE_CREATE_INFO;
  1198. queues[0].pNext = nullptr;
  1199. queues[0].queueFamilyIndex = graphics_queue_family_index;
  1200. queues[0].queueCount = 1;
  1201. queues[0].pQueuePriorities = queue_priorities;
  1202. queues[0].flags = 0;
  1203. // Before we retrieved what is supported, here we tell Vulkan we want to enable these features using the same structs.
  1204. void *nextptr = nullptr;
  1205. VkPhysicalDeviceShaderFloat16Int8FeaturesKHR shader_features = {
  1206. /*sType*/ VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_SHADER_FLOAT16_INT8_FEATURES_KHR,
  1207. /*pNext*/ nextptr,
  1208. /*shaderFloat16*/ shader_capabilities.shader_float16_is_supported,
  1209. /*shaderInt8*/ shader_capabilities.shader_int8_is_supported,
  1210. };
  1211. nextptr = &shader_features;
  1212. VkPhysicalDeviceFragmentShadingRateFeaturesKHR vrs_features = {};
  1213. if (vrs_capabilities.pipeline_vrs_supported || vrs_capabilities.primitive_vrs_supported || vrs_capabilities.attachment_vrs_supported) {
  1214. // Insert into our chain to enable these features if they are available.
  1215. vrs_features.sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_FRAGMENT_SHADING_RATE_FEATURES_KHR;
  1216. vrs_features.pNext = nextptr;
  1217. vrs_features.pipelineFragmentShadingRate = vrs_capabilities.pipeline_vrs_supported;
  1218. vrs_features.primitiveFragmentShadingRate = vrs_capabilities.primitive_vrs_supported;
  1219. vrs_features.attachmentFragmentShadingRate = vrs_capabilities.attachment_vrs_supported;
  1220. nextptr = &vrs_features;
  1221. }
  1222. VkPhysicalDeviceVulkan11Features vulkan11features = {};
  1223. VkPhysicalDevice16BitStorageFeaturesKHR storage_feature = {};
  1224. VkPhysicalDeviceMultiviewFeatures multiview_features = {};
  1225. if (device_api_version >= VK_API_VERSION_1_2) {
  1226. // In Vulkan 1.2 and newer we use a newer struct to enable various features.
  1227. vulkan11features.sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_VULKAN_1_1_FEATURES;
  1228. vulkan11features.pNext = nextptr;
  1229. vulkan11features.storageBuffer16BitAccess = storage_buffer_capabilities.storage_buffer_16_bit_access_is_supported;
  1230. vulkan11features.uniformAndStorageBuffer16BitAccess = storage_buffer_capabilities.uniform_and_storage_buffer_16_bit_access_is_supported;
  1231. vulkan11features.storagePushConstant16 = storage_buffer_capabilities.storage_push_constant_16_is_supported;
  1232. vulkan11features.storageInputOutput16 = storage_buffer_capabilities.storage_input_output_16;
  1233. vulkan11features.multiview = multiview_capabilities.is_supported;
  1234. vulkan11features.multiviewGeometryShader = multiview_capabilities.geometry_shader_is_supported;
  1235. vulkan11features.multiviewTessellationShader = multiview_capabilities.tessellation_shader_is_supported;
  1236. vulkan11features.variablePointersStorageBuffer = 0;
  1237. vulkan11features.variablePointers = 0;
  1238. vulkan11features.protectedMemory = 0;
  1239. vulkan11features.samplerYcbcrConversion = 0;
  1240. vulkan11features.shaderDrawParameters = 0;
  1241. nextptr = &vulkan11features;
  1242. } else {
  1243. // On Vulkan 1.0 and 1.1 we use our older structs to initialize these features.
  1244. storage_feature.sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_16BIT_STORAGE_FEATURES_KHR;
  1245. storage_feature.pNext = nextptr;
  1246. storage_feature.storageBuffer16BitAccess = storage_buffer_capabilities.storage_buffer_16_bit_access_is_supported;
  1247. storage_feature.uniformAndStorageBuffer16BitAccess = storage_buffer_capabilities.uniform_and_storage_buffer_16_bit_access_is_supported;
  1248. storage_feature.storagePushConstant16 = storage_buffer_capabilities.storage_push_constant_16_is_supported;
  1249. storage_feature.storageInputOutput16 = storage_buffer_capabilities.storage_input_output_16;
  1250. nextptr = &storage_feature;
  1251. if (device_api_version >= VK_API_VERSION_1_1) { // any Vulkan 1.1.x version
  1252. multiview_features.sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_MULTIVIEW_FEATURES;
  1253. multiview_features.pNext = nextptr;
  1254. multiview_features.multiview = multiview_capabilities.is_supported;
  1255. multiview_features.multiviewGeometryShader = multiview_capabilities.geometry_shader_is_supported;
  1256. multiview_features.multiviewTessellationShader = multiview_capabilities.tessellation_shader_is_supported;
  1257. nextptr = &multiview_features;
  1258. }
  1259. }
  1260. uint32_t enabled_extension_count = 0;
  1261. const char *enabled_extension_names[MAX_EXTENSIONS];
  1262. ERR_FAIL_COND_V(enabled_device_extension_names.size() > MAX_EXTENSIONS, ERR_CANT_CREATE);
  1263. for (const CharString &extension_name : enabled_device_extension_names) {
  1264. enabled_extension_names[enabled_extension_count++] = extension_name.ptr();
  1265. }
  1266. VkDeviceCreateInfo sdevice = {
  1267. /*sType*/ VK_STRUCTURE_TYPE_DEVICE_CREATE_INFO,
  1268. /*pNext*/ nextptr,
  1269. /*flags*/ 0,
  1270. /*queueCreateInfoCount*/ 1,
  1271. /*pQueueCreateInfos*/ queues,
  1272. /*enabledLayerCount*/ 0,
  1273. /*ppEnabledLayerNames*/ nullptr,
  1274. /*enabledExtensionCount*/ enabled_extension_count,
  1275. /*ppEnabledExtensionNames*/ (const char *const *)enabled_extension_names,
  1276. /*pEnabledFeatures*/ &physical_device_features, // If specific features are required, pass them in here.
  1277. };
  1278. if (separate_present_queue) {
  1279. queues[1].sType = VK_STRUCTURE_TYPE_DEVICE_QUEUE_CREATE_INFO;
  1280. queues[1].pNext = nullptr;
  1281. queues[1].queueFamilyIndex = present_queue_family_index;
  1282. queues[1].queueCount = 1;
  1283. queues[1].pQueuePriorities = queue_priorities;
  1284. queues[1].flags = 0;
  1285. sdevice.queueCreateInfoCount = 2;
  1286. }
  1287. if (vulkan_hooks) {
  1288. if (!vulkan_hooks->create_vulkan_device(&sdevice, &device)) {
  1289. return ERR_CANT_CREATE;
  1290. }
  1291. } else {
  1292. err = vkCreateDevice(gpu, &sdevice, nullptr, &device);
  1293. ERR_FAIL_COND_V(err, ERR_CANT_CREATE);
  1294. }
  1295. return OK;
  1296. }
  1297. Error VulkanContext::_initialize_queues(VkSurfaceKHR p_surface) {
  1298. // Iterate over each queue to learn whether it supports presenting:
  1299. VkBool32 *supportsPresent = (VkBool32 *)malloc(queue_family_count * sizeof(VkBool32));
  1300. for (uint32_t i = 0; i < queue_family_count; i++) {
  1301. fpGetPhysicalDeviceSurfaceSupportKHR(gpu, i, p_surface, &supportsPresent[i]);
  1302. }
  1303. // Search for a graphics and a present queue in the array of queue
  1304. // families, try to find one that supports both.
  1305. uint32_t graphicsQueueFamilyIndex = UINT32_MAX;
  1306. uint32_t presentQueueFamilyIndex = UINT32_MAX;
  1307. for (uint32_t i = 0; i < queue_family_count; i++) {
  1308. if ((queue_props[i].queueFlags & VK_QUEUE_GRAPHICS_BIT) != 0) {
  1309. if (graphicsQueueFamilyIndex == UINT32_MAX) {
  1310. graphicsQueueFamilyIndex = i;
  1311. }
  1312. if (supportsPresent[i] == VK_TRUE) {
  1313. graphicsQueueFamilyIndex = i;
  1314. presentQueueFamilyIndex = i;
  1315. break;
  1316. }
  1317. }
  1318. }
  1319. if (presentQueueFamilyIndex == UINT32_MAX) {
  1320. // If didn't find a queue that supports both graphics and present, then
  1321. // find a separate present queue.
  1322. for (uint32_t i = 0; i < queue_family_count; ++i) {
  1323. if (supportsPresent[i] == VK_TRUE) {
  1324. presentQueueFamilyIndex = i;
  1325. break;
  1326. }
  1327. }
  1328. }
  1329. free(supportsPresent);
  1330. // Generate error if could not find both a graphics and a present queue.
  1331. ERR_FAIL_COND_V_MSG(graphicsQueueFamilyIndex == UINT32_MAX || presentQueueFamilyIndex == UINT32_MAX, ERR_CANT_CREATE,
  1332. "Could not find both graphics and present queues\n");
  1333. graphics_queue_family_index = graphicsQueueFamilyIndex;
  1334. present_queue_family_index = presentQueueFamilyIndex;
  1335. separate_present_queue = (graphics_queue_family_index != present_queue_family_index);
  1336. _create_device();
  1337. static PFN_vkGetDeviceProcAddr g_gdpa = nullptr;
  1338. #define GET_DEVICE_PROC_ADDR(dev, entrypoint) \
  1339. { \
  1340. if (!g_gdpa) \
  1341. g_gdpa = (PFN_vkGetDeviceProcAddr)vkGetInstanceProcAddr(inst, "vkGetDeviceProcAddr"); \
  1342. fp##entrypoint = (PFN_vk##entrypoint)g_gdpa(dev, "vk" #entrypoint); \
  1343. ERR_FAIL_COND_V_MSG(fp##entrypoint == nullptr, ERR_CANT_CREATE, \
  1344. "vkGetDeviceProcAddr failed to find vk" #entrypoint); \
  1345. }
  1346. GET_DEVICE_PROC_ADDR(device, CreateSwapchainKHR);
  1347. GET_DEVICE_PROC_ADDR(device, DestroySwapchainKHR);
  1348. GET_DEVICE_PROC_ADDR(device, GetSwapchainImagesKHR);
  1349. GET_DEVICE_PROC_ADDR(device, AcquireNextImageKHR);
  1350. GET_DEVICE_PROC_ADDR(device, QueuePresentKHR);
  1351. if (is_device_extension_enabled(VK_GOOGLE_DISPLAY_TIMING_EXTENSION_NAME)) {
  1352. GET_DEVICE_PROC_ADDR(device, GetRefreshCycleDurationGOOGLE);
  1353. GET_DEVICE_PROC_ADDR(device, GetPastPresentationTimingGOOGLE);
  1354. }
  1355. vkGetDeviceQueue(device, graphics_queue_family_index, 0, &graphics_queue);
  1356. if (!separate_present_queue) {
  1357. present_queue = graphics_queue;
  1358. } else {
  1359. vkGetDeviceQueue(device, present_queue_family_index, 0, &present_queue);
  1360. }
  1361. // Get the list of VkFormat's that are supported:
  1362. uint32_t formatCount;
  1363. VkResult err = fpGetPhysicalDeviceSurfaceFormatsKHR(gpu, p_surface, &formatCount, nullptr);
  1364. ERR_FAIL_COND_V(err, ERR_CANT_CREATE);
  1365. VkSurfaceFormatKHR *surfFormats = (VkSurfaceFormatKHR *)malloc(formatCount * sizeof(VkSurfaceFormatKHR));
  1366. err = fpGetPhysicalDeviceSurfaceFormatsKHR(gpu, p_surface, &formatCount, surfFormats);
  1367. if (err) {
  1368. free(surfFormats);
  1369. ERR_FAIL_V(ERR_CANT_CREATE);
  1370. }
  1371. // If the format list includes just one entry of VK_FORMAT_UNDEFINED,
  1372. // the surface has no preferred format. Otherwise, at least one
  1373. // supported format will be returned.
  1374. if (formatCount == 1 && surfFormats[0].format == VK_FORMAT_UNDEFINED) {
  1375. format = VK_FORMAT_B8G8R8A8_UNORM;
  1376. color_space = surfFormats[0].colorSpace;
  1377. } else {
  1378. // These should be ordered with the ones we want to use on top and fallback modes further down
  1379. // we want a 32bit RGBA unsigned normalized buffer or similar.
  1380. const VkFormat allowed_formats[] = {
  1381. VK_FORMAT_B8G8R8A8_UNORM,
  1382. VK_FORMAT_R8G8B8A8_UNORM
  1383. };
  1384. uint32_t allowed_formats_count = sizeof(allowed_formats) / sizeof(VkFormat);
  1385. if (formatCount < 1) {
  1386. free(surfFormats);
  1387. ERR_FAIL_V_MSG(ERR_CANT_CREATE, "formatCount less than 1");
  1388. }
  1389. // Find the first format that we support.
  1390. format = VK_FORMAT_UNDEFINED;
  1391. for (uint32_t af = 0; af < allowed_formats_count && format == VK_FORMAT_UNDEFINED; af++) {
  1392. for (uint32_t sf = 0; sf < formatCount && format == VK_FORMAT_UNDEFINED; sf++) {
  1393. if (surfFormats[sf].format == allowed_formats[af]) {
  1394. format = surfFormats[sf].format;
  1395. color_space = surfFormats[sf].colorSpace;
  1396. }
  1397. }
  1398. }
  1399. if (format == VK_FORMAT_UNDEFINED) {
  1400. free(surfFormats);
  1401. ERR_FAIL_V_MSG(ERR_CANT_CREATE, "No usable surface format found.");
  1402. }
  1403. }
  1404. free(surfFormats);
  1405. Error serr = _create_semaphores();
  1406. if (serr) {
  1407. return serr;
  1408. }
  1409. queues_initialized = true;
  1410. return OK;
  1411. }
  1412. Error VulkanContext::_create_semaphores() {
  1413. VkResult err;
  1414. // Create semaphores to synchronize acquiring presentable buffers before
  1415. // rendering and waiting for drawing to be complete before presenting.
  1416. VkSemaphoreCreateInfo semaphoreCreateInfo = {
  1417. /*sType*/ VK_STRUCTURE_TYPE_SEMAPHORE_CREATE_INFO,
  1418. /*pNext*/ nullptr,
  1419. /*flags*/ 0,
  1420. };
  1421. // Create fences that we can use to throttle if we get too far
  1422. // ahead of the image presents.
  1423. VkFenceCreateInfo fence_ci = {
  1424. /*sType*/ VK_STRUCTURE_TYPE_FENCE_CREATE_INFO,
  1425. /*pNext*/ nullptr,
  1426. /*flags*/ VK_FENCE_CREATE_SIGNALED_BIT
  1427. };
  1428. for (uint32_t i = 0; i < FRAME_LAG; i++) {
  1429. err = vkCreateFence(device, &fence_ci, nullptr, &fences[i]);
  1430. ERR_FAIL_COND_V(err, ERR_CANT_CREATE);
  1431. err = vkCreateSemaphore(device, &semaphoreCreateInfo, nullptr, &draw_complete_semaphores[i]);
  1432. ERR_FAIL_COND_V(err, ERR_CANT_CREATE);
  1433. if (separate_present_queue) {
  1434. err = vkCreateSemaphore(device, &semaphoreCreateInfo, nullptr, &image_ownership_semaphores[i]);
  1435. ERR_FAIL_COND_V(err, ERR_CANT_CREATE);
  1436. }
  1437. }
  1438. frame_index = 0;
  1439. // Get Memory information and properties.
  1440. vkGetPhysicalDeviceMemoryProperties(gpu, &memory_properties);
  1441. return OK;
  1442. }
  1443. bool VulkanContext::_use_validation_layers() {
  1444. return Engine::get_singleton()->is_validation_layers_enabled();
  1445. }
  1446. VkExtent2D VulkanContext::_compute_swapchain_extent(const VkSurfaceCapabilitiesKHR &p_surf_capabilities, int *p_window_width, int *p_window_height) const {
  1447. // Width and height are either both 0xFFFFFFFF, or both not 0xFFFFFFFF.
  1448. if (p_surf_capabilities.currentExtent.width == 0xFFFFFFFF) {
  1449. // If the surface size is undefined, the size is set to the size
  1450. // of the images requested, which must fit within the minimum and
  1451. // maximum values.
  1452. VkExtent2D extent = {};
  1453. extent.width = CLAMP((uint32_t)(*p_window_width), p_surf_capabilities.minImageExtent.width, p_surf_capabilities.maxImageExtent.width);
  1454. extent.height = CLAMP((uint32_t)(*p_window_height), p_surf_capabilities.minImageExtent.height, p_surf_capabilities.maxImageExtent.height);
  1455. return extent;
  1456. } else {
  1457. // If the surface size is defined, the swap chain size must match.
  1458. *p_window_width = p_surf_capabilities.currentExtent.width;
  1459. *p_window_height = p_surf_capabilities.currentExtent.height;
  1460. return p_surf_capabilities.currentExtent;
  1461. }
  1462. }
  1463. Error VulkanContext::_window_create(DisplayServer::WindowID p_window_id, DisplayServer::VSyncMode p_vsync_mode, VkSurfaceKHR p_surface, int p_width, int p_height) {
  1464. ERR_FAIL_COND_V(windows.has(p_window_id), ERR_INVALID_PARAMETER);
  1465. if (!device_initialized) {
  1466. Error err = _create_physical_device(p_surface);
  1467. ERR_FAIL_COND_V(err != OK, ERR_CANT_CREATE);
  1468. }
  1469. if (!queues_initialized) {
  1470. // We use a single GPU, but we need a surface to initialize the
  1471. // queues, so this process must be deferred until a surface
  1472. // is created.
  1473. Error err = _initialize_queues(p_surface);
  1474. ERR_FAIL_COND_V(err != OK, ERR_CANT_CREATE);
  1475. }
  1476. Window window;
  1477. window.surface = p_surface;
  1478. window.width = p_width;
  1479. window.height = p_height;
  1480. window.vsync_mode = p_vsync_mode;
  1481. Error err = _update_swap_chain(&window);
  1482. ERR_FAIL_COND_V(err != OK, ERR_CANT_CREATE);
  1483. VkSemaphoreCreateInfo semaphoreCreateInfo = {
  1484. /*sType*/ VK_STRUCTURE_TYPE_SEMAPHORE_CREATE_INFO,
  1485. /*pNext*/ nullptr,
  1486. /*flags*/ 0,
  1487. };
  1488. for (uint32_t i = 0; i < FRAME_LAG; i++) {
  1489. VkResult vkerr = vkCreateSemaphore(device, &semaphoreCreateInfo, nullptr, &window.image_acquired_semaphores[i]);
  1490. ERR_FAIL_COND_V(vkerr, ERR_CANT_CREATE);
  1491. }
  1492. windows[p_window_id] = window;
  1493. return OK;
  1494. }
  1495. void VulkanContext::window_resize(DisplayServer::WindowID p_window, int p_width, int p_height) {
  1496. ERR_FAIL_COND(!windows.has(p_window));
  1497. windows[p_window].width = p_width;
  1498. windows[p_window].height = p_height;
  1499. _update_swap_chain(&windows[p_window]);
  1500. }
  1501. int VulkanContext::window_get_width(DisplayServer::WindowID p_window) {
  1502. ERR_FAIL_COND_V(!windows.has(p_window), -1);
  1503. return windows[p_window].width;
  1504. }
  1505. int VulkanContext::window_get_height(DisplayServer::WindowID p_window) {
  1506. ERR_FAIL_COND_V(!windows.has(p_window), -1);
  1507. return windows[p_window].height;
  1508. }
  1509. bool VulkanContext::window_is_valid_swapchain(DisplayServer::WindowID p_window) {
  1510. ERR_FAIL_COND_V(!windows.has(p_window), false);
  1511. Window *w = &windows[p_window];
  1512. return w->swapchain_image_resources != VK_NULL_HANDLE;
  1513. }
  1514. VkRenderPass VulkanContext::window_get_render_pass(DisplayServer::WindowID p_window) {
  1515. ERR_FAIL_COND_V(!windows.has(p_window), VK_NULL_HANDLE);
  1516. Window *w = &windows[p_window];
  1517. // Vulkan use of currentbuffer.
  1518. return w->render_pass;
  1519. }
  1520. VkFramebuffer VulkanContext::window_get_framebuffer(DisplayServer::WindowID p_window) {
  1521. ERR_FAIL_COND_V(!windows.has(p_window), VK_NULL_HANDLE);
  1522. ERR_FAIL_COND_V(!buffers_prepared, VK_NULL_HANDLE);
  1523. Window *w = &windows[p_window];
  1524. // Vulkan use of currentbuffer.
  1525. if (w->swapchain_image_resources != VK_NULL_HANDLE) {
  1526. return w->swapchain_image_resources[w->current_buffer].framebuffer;
  1527. } else {
  1528. return VK_NULL_HANDLE;
  1529. }
  1530. }
  1531. void VulkanContext::window_destroy(DisplayServer::WindowID p_window_id) {
  1532. ERR_FAIL_COND(!windows.has(p_window_id));
  1533. _clean_up_swap_chain(&windows[p_window_id]);
  1534. for (uint32_t i = 0; i < FRAME_LAG; i++) {
  1535. vkDestroySemaphore(device, windows[p_window_id].image_acquired_semaphores[i], nullptr);
  1536. }
  1537. vkDestroySurfaceKHR(inst, windows[p_window_id].surface, nullptr);
  1538. windows.erase(p_window_id);
  1539. }
  1540. Error VulkanContext::_clean_up_swap_chain(Window *window) {
  1541. if (!window->swapchain) {
  1542. return OK;
  1543. }
  1544. vkDeviceWaitIdle(device);
  1545. // This destroys images associated it seems.
  1546. fpDestroySwapchainKHR(device, window->swapchain, nullptr);
  1547. window->swapchain = VK_NULL_HANDLE;
  1548. vkDestroyRenderPass(device, window->render_pass, nullptr);
  1549. if (window->swapchain_image_resources) {
  1550. for (uint32_t i = 0; i < swapchainImageCount; i++) {
  1551. vkDestroyImageView(device, window->swapchain_image_resources[i].view, nullptr);
  1552. vkDestroyFramebuffer(device, window->swapchain_image_resources[i].framebuffer, nullptr);
  1553. }
  1554. free(window->swapchain_image_resources);
  1555. window->swapchain_image_resources = nullptr;
  1556. }
  1557. if (separate_present_queue) {
  1558. vkDestroyCommandPool(device, window->present_cmd_pool, nullptr);
  1559. }
  1560. return OK;
  1561. }
  1562. Error VulkanContext::_update_swap_chain(Window *window) {
  1563. VkResult err;
  1564. if (window->swapchain) {
  1565. _clean_up_swap_chain(window);
  1566. }
  1567. // Check the surface capabilities and formats.
  1568. VkSurfaceCapabilitiesKHR surfCapabilities;
  1569. err = fpGetPhysicalDeviceSurfaceCapabilitiesKHR(gpu, window->surface, &surfCapabilities);
  1570. ERR_FAIL_COND_V(err, ERR_CANT_CREATE);
  1571. uint32_t presentModeCount;
  1572. err = fpGetPhysicalDeviceSurfacePresentModesKHR(gpu, window->surface, &presentModeCount, nullptr);
  1573. ERR_FAIL_COND_V(err, ERR_CANT_CREATE);
  1574. VkPresentModeKHR *presentModes = (VkPresentModeKHR *)malloc(presentModeCount * sizeof(VkPresentModeKHR));
  1575. ERR_FAIL_COND_V(!presentModes, ERR_CANT_CREATE);
  1576. err = fpGetPhysicalDeviceSurfacePresentModesKHR(gpu, window->surface, &presentModeCount, presentModes);
  1577. if (err) {
  1578. free(presentModes);
  1579. ERR_FAIL_V(ERR_CANT_CREATE);
  1580. }
  1581. VkExtent2D swapchainExtent = _compute_swapchain_extent(surfCapabilities, &window->width, &window->height);
  1582. if (window->width == 0 || window->height == 0) {
  1583. free(presentModes);
  1584. // Likely window minimized, no swapchain created.
  1585. return OK;
  1586. }
  1587. // The FIFO present mode is guaranteed by the spec to be supported
  1588. // and to have no tearing. It's a great default present mode to use.
  1589. // There are times when you may wish to use another present mode. The
  1590. // following code shows how to select them, and the comments provide some
  1591. // reasons you may wish to use them.
  1592. //
  1593. // It should be noted that Vulkan 1.0 doesn't provide a method for
  1594. // synchronizing rendering with the presentation engine's display. There
  1595. // is a method provided for throttling rendering with the display, but
  1596. // there are some presentation engines for which this method will not work.
  1597. // If an application doesn't throttle its rendering, and if it renders much
  1598. // faster than the refresh rate of the display, this can waste power on
  1599. // mobile devices. That is because power is being spent rendering images
  1600. // that may never be seen.
  1601. // VK_PRESENT_MODE_IMMEDIATE_KHR is for applications that don't care about
  1602. // tearing, or have some way of synchronizing their rendering with the
  1603. // display.
  1604. // VK_PRESENT_MODE_MAILBOX_KHR may be useful for applications that
  1605. // generally render a new presentable image every refresh cycle, but are
  1606. // occasionally early. In this case, the application wants the new image
  1607. // to be displayed instead of the previously-queued-for-presentation image
  1608. // that has not yet been displayed.
  1609. // VK_PRESENT_MODE_FIFO_RELAXED_KHR is for applications that generally
  1610. // render a new presentable image every refresh cycle, but are occasionally
  1611. // late. In this case (perhaps because of stuttering/latency concerns),
  1612. // the application wants the late image to be immediately displayed, even
  1613. // though that may mean some tearing.
  1614. VkPresentModeKHR requested_present_mode = VkPresentModeKHR::VK_PRESENT_MODE_FIFO_KHR;
  1615. switch (window->vsync_mode) {
  1616. case DisplayServer::VSYNC_MAILBOX:
  1617. requested_present_mode = VkPresentModeKHR::VK_PRESENT_MODE_MAILBOX_KHR;
  1618. break;
  1619. case DisplayServer::VSYNC_ADAPTIVE:
  1620. requested_present_mode = VkPresentModeKHR::VK_PRESENT_MODE_FIFO_RELAXED_KHR;
  1621. break;
  1622. case DisplayServer::VSYNC_ENABLED:
  1623. requested_present_mode = VkPresentModeKHR::VK_PRESENT_MODE_FIFO_KHR;
  1624. break;
  1625. case DisplayServer::VSYNC_DISABLED:
  1626. requested_present_mode = VkPresentModeKHR::VK_PRESENT_MODE_IMMEDIATE_KHR;
  1627. break;
  1628. }
  1629. // Check if the requested mode is available.
  1630. bool present_mode_available = false;
  1631. for (uint32_t i = 0; i < presentModeCount; i++) {
  1632. if (presentModes[i] == requested_present_mode) {
  1633. present_mode_available = true;
  1634. }
  1635. }
  1636. // Set the windows present mode if it is available, otherwise FIFO is used (guaranteed supported).
  1637. if (present_mode_available) {
  1638. if (window->presentMode != requested_present_mode) {
  1639. window->presentMode = requested_present_mode;
  1640. print_verbose("Using present mode: " + String(string_VkPresentModeKHR(window->presentMode)));
  1641. }
  1642. } else {
  1643. String present_mode_string;
  1644. switch (window->vsync_mode) {
  1645. case DisplayServer::VSYNC_MAILBOX:
  1646. present_mode_string = "Mailbox";
  1647. break;
  1648. case DisplayServer::VSYNC_ADAPTIVE:
  1649. present_mode_string = "Adaptive";
  1650. break;
  1651. case DisplayServer::VSYNC_ENABLED:
  1652. present_mode_string = "Enabled";
  1653. break;
  1654. case DisplayServer::VSYNC_DISABLED:
  1655. present_mode_string = "Disabled";
  1656. break;
  1657. }
  1658. WARN_PRINT(vformat("The requested V-Sync mode %s is not available. Falling back to V-Sync mode Enabled.", present_mode_string));
  1659. window->vsync_mode = DisplayServer::VSYNC_ENABLED; // Set to default.
  1660. }
  1661. free(presentModes);
  1662. // Determine the number of VkImages to use in the swap chain.
  1663. // Application desires to acquire 3 images at a time for triple
  1664. // buffering.
  1665. uint32_t desiredNumOfSwapchainImages = 3;
  1666. if (desiredNumOfSwapchainImages < surfCapabilities.minImageCount) {
  1667. desiredNumOfSwapchainImages = surfCapabilities.minImageCount;
  1668. }
  1669. // If maxImageCount is 0, we can ask for as many images as we want;
  1670. // otherwise we're limited to maxImageCount.
  1671. if ((surfCapabilities.maxImageCount > 0) && (desiredNumOfSwapchainImages > surfCapabilities.maxImageCount)) {
  1672. // Application must settle for fewer images than desired.
  1673. desiredNumOfSwapchainImages = surfCapabilities.maxImageCount;
  1674. }
  1675. VkSurfaceTransformFlagsKHR preTransform;
  1676. if (surfCapabilities.supportedTransforms & VK_SURFACE_TRANSFORM_IDENTITY_BIT_KHR) {
  1677. preTransform = VK_SURFACE_TRANSFORM_IDENTITY_BIT_KHR;
  1678. } else {
  1679. preTransform = surfCapabilities.currentTransform;
  1680. }
  1681. VkCompositeAlphaFlagBitsKHR compositeAlpha = VK_COMPOSITE_ALPHA_OPAQUE_BIT_KHR;
  1682. if (OS::get_singleton()->is_layered_allowed() || !(surfCapabilities.supportedCompositeAlpha & compositeAlpha)) {
  1683. // Find a supported composite alpha mode - one of these is guaranteed to be set.
  1684. VkCompositeAlphaFlagBitsKHR compositeAlphaFlags[4] = {
  1685. VK_COMPOSITE_ALPHA_PRE_MULTIPLIED_BIT_KHR,
  1686. VK_COMPOSITE_ALPHA_POST_MULTIPLIED_BIT_KHR,
  1687. VK_COMPOSITE_ALPHA_INHERIT_BIT_KHR,
  1688. VK_COMPOSITE_ALPHA_OPAQUE_BIT_KHR,
  1689. };
  1690. for (uint32_t i = 0; i < ARRAY_SIZE(compositeAlphaFlags); i++) {
  1691. if (surfCapabilities.supportedCompositeAlpha & compositeAlphaFlags[i]) {
  1692. compositeAlpha = compositeAlphaFlags[i];
  1693. break;
  1694. }
  1695. }
  1696. }
  1697. VkSwapchainCreateInfoKHR swapchain_ci = {
  1698. /*sType*/ VK_STRUCTURE_TYPE_SWAPCHAIN_CREATE_INFO_KHR,
  1699. /*pNext*/ nullptr,
  1700. /*flags*/ 0,
  1701. /*surface*/ window->surface,
  1702. /*minImageCount*/ desiredNumOfSwapchainImages,
  1703. /*imageFormat*/ format,
  1704. /*imageColorSpace*/ color_space,
  1705. /*imageExtent*/ {
  1706. /*width*/ swapchainExtent.width,
  1707. /*height*/ swapchainExtent.height,
  1708. },
  1709. /*imageArrayLayers*/ 1,
  1710. /*imageUsage*/ VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT,
  1711. /*imageSharingMode*/ VK_SHARING_MODE_EXCLUSIVE,
  1712. /*queueFamilyIndexCount*/ 0,
  1713. /*pQueueFamilyIndices*/ nullptr,
  1714. /*preTransform*/ (VkSurfaceTransformFlagBitsKHR)preTransform,
  1715. /*compositeAlpha*/ compositeAlpha,
  1716. /*presentMode*/ window->presentMode,
  1717. /*clipped*/ true,
  1718. /*oldSwapchain*/ VK_NULL_HANDLE,
  1719. };
  1720. err = fpCreateSwapchainKHR(device, &swapchain_ci, nullptr, &window->swapchain);
  1721. ERR_FAIL_COND_V(err, ERR_CANT_CREATE);
  1722. uint32_t sp_image_count;
  1723. err = fpGetSwapchainImagesKHR(device, window->swapchain, &sp_image_count, nullptr);
  1724. ERR_FAIL_COND_V(err, ERR_CANT_CREATE);
  1725. if (swapchainImageCount == 0) {
  1726. // Assign here for the first time.
  1727. swapchainImageCount = sp_image_count;
  1728. } else {
  1729. ERR_FAIL_COND_V(swapchainImageCount != sp_image_count, ERR_BUG);
  1730. }
  1731. VkImage *swapchainImages = (VkImage *)malloc(swapchainImageCount * sizeof(VkImage));
  1732. ERR_FAIL_COND_V(!swapchainImages, ERR_CANT_CREATE);
  1733. err = fpGetSwapchainImagesKHR(device, window->swapchain, &swapchainImageCount, swapchainImages);
  1734. if (err) {
  1735. free(swapchainImages);
  1736. ERR_FAIL_V(ERR_CANT_CREATE);
  1737. }
  1738. window->swapchain_image_resources =
  1739. (SwapchainImageResources *)malloc(sizeof(SwapchainImageResources) * swapchainImageCount);
  1740. if (!window->swapchain_image_resources) {
  1741. free(swapchainImages);
  1742. ERR_FAIL_V(ERR_CANT_CREATE);
  1743. }
  1744. for (uint32_t i = 0; i < swapchainImageCount; i++) {
  1745. VkImageViewCreateInfo color_image_view = {
  1746. /*sType*/ VK_STRUCTURE_TYPE_IMAGE_VIEW_CREATE_INFO,
  1747. /*pNext*/ nullptr,
  1748. /*flags*/ 0,
  1749. /*image*/ swapchainImages[i],
  1750. /*viewType*/ VK_IMAGE_VIEW_TYPE_2D,
  1751. /*format*/ format,
  1752. /*components*/ {
  1753. /*r*/ VK_COMPONENT_SWIZZLE_R,
  1754. /*g*/ VK_COMPONENT_SWIZZLE_G,
  1755. /*b*/ VK_COMPONENT_SWIZZLE_B,
  1756. /*a*/ VK_COMPONENT_SWIZZLE_A,
  1757. },
  1758. /*subresourceRange*/ { /*aspectMask*/ VK_IMAGE_ASPECT_COLOR_BIT,
  1759. /*baseMipLevel*/ 0,
  1760. /*levelCount*/ 1,
  1761. /*baseArrayLayer*/ 0,
  1762. /*layerCount*/ 1 },
  1763. };
  1764. window->swapchain_image_resources[i].image = swapchainImages[i];
  1765. color_image_view.image = window->swapchain_image_resources[i].image;
  1766. err = vkCreateImageView(device, &color_image_view, nullptr, &window->swapchain_image_resources[i].view);
  1767. if (err) {
  1768. free(swapchainImages);
  1769. ERR_FAIL_V(ERR_CANT_CREATE);
  1770. }
  1771. }
  1772. free(swapchainImages);
  1773. /******** FRAMEBUFFER ************/
  1774. {
  1775. const VkAttachmentDescription2KHR attachment = {
  1776. /*sType*/ VK_STRUCTURE_TYPE_ATTACHMENT_DESCRIPTION_2_KHR,
  1777. /*pNext*/ nullptr,
  1778. /*flags*/ 0,
  1779. /*format*/ format,
  1780. /*samples*/ VK_SAMPLE_COUNT_1_BIT,
  1781. /*loadOp*/ VK_ATTACHMENT_LOAD_OP_CLEAR,
  1782. /*storeOp*/ VK_ATTACHMENT_STORE_OP_STORE,
  1783. /*stencilLoadOp*/ VK_ATTACHMENT_LOAD_OP_DONT_CARE,
  1784. /*stencilStoreOp*/ VK_ATTACHMENT_STORE_OP_DONT_CARE,
  1785. /*initialLayout*/ VK_IMAGE_LAYOUT_UNDEFINED,
  1786. /*finalLayout*/ VK_IMAGE_LAYOUT_PRESENT_SRC_KHR,
  1787. };
  1788. const VkAttachmentReference2KHR color_reference = {
  1789. /*sType*/ VK_STRUCTURE_TYPE_ATTACHMENT_REFERENCE_2_KHR,
  1790. /*pNext*/ nullptr,
  1791. /*attachment*/ 0,
  1792. /*layout*/ VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL,
  1793. /*aspectMask*/ 0,
  1794. };
  1795. const VkSubpassDescription2KHR subpass = {
  1796. /*sType*/ VK_STRUCTURE_TYPE_SUBPASS_DESCRIPTION_2_KHR,
  1797. /*pNext*/ nullptr,
  1798. /*flags*/ 0,
  1799. /*pipelineBindPoint*/ VK_PIPELINE_BIND_POINT_GRAPHICS,
  1800. /*viewMask*/ 0,
  1801. /*inputAttachmentCount*/ 0,
  1802. /*pInputAttachments*/ nullptr,
  1803. /*colorAttachmentCount*/ 1,
  1804. /*pColorAttachments*/ &color_reference,
  1805. /*pResolveAttachments*/ nullptr,
  1806. /*pDepthStencilAttachment*/ nullptr,
  1807. /*preserveAttachmentCount*/ 0,
  1808. /*pPreserveAttachments*/ nullptr,
  1809. };
  1810. const VkRenderPassCreateInfo2KHR rp_info = {
  1811. /*sType*/ VK_STRUCTURE_TYPE_RENDER_PASS_CREATE_INFO_2_KHR,
  1812. /*pNext*/ nullptr,
  1813. /*flags*/ 0,
  1814. /*attachmentCount*/ 1,
  1815. /*pAttachments*/ &attachment,
  1816. /*subpassCount*/ 1,
  1817. /*pSubpasses*/ &subpass,
  1818. /*dependencyCount*/ 0,
  1819. /*pDependencies*/ nullptr,
  1820. /*correlatedViewMaskCount*/ 0,
  1821. /*pCorrelatedViewMasks*/ nullptr,
  1822. };
  1823. err = vkCreateRenderPass2KHR(device, &rp_info, nullptr, &window->render_pass);
  1824. ERR_FAIL_COND_V(err, ERR_CANT_CREATE);
  1825. for (uint32_t i = 0; i < swapchainImageCount; i++) {
  1826. const VkFramebufferCreateInfo fb_info = {
  1827. /*sType*/ VK_STRUCTURE_TYPE_FRAMEBUFFER_CREATE_INFO,
  1828. /*pNext*/ nullptr,
  1829. /*flags*/ 0,
  1830. /*renderPass*/ window->render_pass,
  1831. /*attachmentCount*/ 1,
  1832. /*pAttachments*/ &window->swapchain_image_resources[i].view,
  1833. /*width*/ (uint32_t)window->width,
  1834. /*height*/ (uint32_t)window->height,
  1835. /*layers*/ 1,
  1836. };
  1837. err = vkCreateFramebuffer(device, &fb_info, nullptr, &window->swapchain_image_resources[i].framebuffer);
  1838. ERR_FAIL_COND_V(err, ERR_CANT_CREATE);
  1839. }
  1840. }
  1841. /******** SEPARATE PRESENT QUEUE ************/
  1842. if (separate_present_queue) {
  1843. const VkCommandPoolCreateInfo present_cmd_pool_info = {
  1844. /*sType*/ VK_STRUCTURE_TYPE_COMMAND_POOL_CREATE_INFO,
  1845. /*pNext*/ nullptr,
  1846. /*flags*/ 0,
  1847. /*queueFamilyIndex*/ present_queue_family_index,
  1848. };
  1849. err = vkCreateCommandPool(device, &present_cmd_pool_info, nullptr, &window->present_cmd_pool);
  1850. ERR_FAIL_COND_V(err, ERR_CANT_CREATE);
  1851. const VkCommandBufferAllocateInfo present_cmd_info = {
  1852. /*sType*/ VK_STRUCTURE_TYPE_COMMAND_BUFFER_ALLOCATE_INFO,
  1853. /*pNext*/ nullptr,
  1854. /*commandPool*/ window->present_cmd_pool,
  1855. /*level*/ VK_COMMAND_BUFFER_LEVEL_PRIMARY,
  1856. /*commandBufferCount*/ 1,
  1857. };
  1858. for (uint32_t i = 0; i < swapchainImageCount; i++) {
  1859. err = vkAllocateCommandBuffers(device, &present_cmd_info,
  1860. &window->swapchain_image_resources[i].graphics_to_present_cmd);
  1861. ERR_FAIL_COND_V(err, ERR_CANT_CREATE);
  1862. const VkCommandBufferBeginInfo cmd_buf_info = {
  1863. /*sType*/ VK_STRUCTURE_TYPE_COMMAND_BUFFER_BEGIN_INFO,
  1864. /*pNext*/ nullptr,
  1865. /*flags*/ VK_COMMAND_BUFFER_USAGE_SIMULTANEOUS_USE_BIT,
  1866. /*pInheritanceInfo*/ nullptr,
  1867. };
  1868. err = vkBeginCommandBuffer(window->swapchain_image_resources[i].graphics_to_present_cmd, &cmd_buf_info);
  1869. ERR_FAIL_COND_V(err, ERR_CANT_CREATE);
  1870. VkImageMemoryBarrier image_ownership_barrier = {
  1871. /*sType*/ VK_STRUCTURE_TYPE_IMAGE_MEMORY_BARRIER,
  1872. /*pNext*/ nullptr,
  1873. /*srcAccessMask*/ 0,
  1874. /*dstAccessMask*/ VK_ACCESS_COLOR_ATTACHMENT_WRITE_BIT,
  1875. /*oldLayout*/ VK_IMAGE_LAYOUT_PRESENT_SRC_KHR,
  1876. /*newLayout*/ VK_IMAGE_LAYOUT_PRESENT_SRC_KHR,
  1877. /*srcQueueFamilyIndex*/ graphics_queue_family_index,
  1878. /*dstQueueFamilyIndex*/ present_queue_family_index,
  1879. /*image*/ window->swapchain_image_resources[i].image,
  1880. /*subresourceRange*/ { VK_IMAGE_ASPECT_COLOR_BIT, 0, 1, 0, 1 }
  1881. };
  1882. vkCmdPipelineBarrier(window->swapchain_image_resources[i].graphics_to_present_cmd, VK_PIPELINE_STAGE_COLOR_ATTACHMENT_OUTPUT_BIT,
  1883. VK_PIPELINE_STAGE_COLOR_ATTACHMENT_OUTPUT_BIT, 0, 0, nullptr, 0, nullptr, 1, &image_ownership_barrier);
  1884. err = vkEndCommandBuffer(window->swapchain_image_resources[i].graphics_to_present_cmd);
  1885. ERR_FAIL_COND_V(err, ERR_CANT_CREATE);
  1886. }
  1887. }
  1888. // Reset current buffer.
  1889. window->current_buffer = 0;
  1890. return OK;
  1891. }
  1892. Error VulkanContext::initialize() {
  1893. #ifdef USE_VOLK
  1894. if (volkInitialize() != VK_SUCCESS) {
  1895. return FAILED;
  1896. }
  1897. #endif
  1898. Error err = _create_instance();
  1899. if (err != OK) {
  1900. return err;
  1901. }
  1902. return OK;
  1903. }
  1904. void VulkanContext::set_setup_buffer(VkCommandBuffer p_command_buffer) {
  1905. command_buffer_queue.write[0] = p_command_buffer;
  1906. }
  1907. void VulkanContext::append_command_buffer(VkCommandBuffer p_command_buffer) {
  1908. if (command_buffer_queue.size() <= command_buffer_count) {
  1909. command_buffer_queue.resize(command_buffer_count + 1);
  1910. }
  1911. command_buffer_queue.write[command_buffer_count] = p_command_buffer;
  1912. command_buffer_count++;
  1913. }
  1914. void VulkanContext::flush(bool p_flush_setup, bool p_flush_pending) {
  1915. // Ensure everything else pending is executed.
  1916. vkDeviceWaitIdle(device);
  1917. // Flush the pending setup buffer.
  1918. bool setup_flushable = p_flush_setup && command_buffer_queue[0];
  1919. bool pending_flushable = p_flush_pending && command_buffer_count > 1;
  1920. if (setup_flushable) {
  1921. // Use a fence to wait for everything done.
  1922. VkSubmitInfo submit_info;
  1923. submit_info.sType = VK_STRUCTURE_TYPE_SUBMIT_INFO;
  1924. submit_info.pNext = nullptr;
  1925. submit_info.pWaitDstStageMask = nullptr;
  1926. submit_info.waitSemaphoreCount = 0;
  1927. submit_info.pWaitSemaphores = nullptr;
  1928. submit_info.commandBufferCount = 1;
  1929. submit_info.pCommandBuffers = command_buffer_queue.ptr();
  1930. submit_info.signalSemaphoreCount = pending_flushable ? 1 : 0;
  1931. submit_info.pSignalSemaphores = pending_flushable ? &draw_complete_semaphores[frame_index] : nullptr;
  1932. VkResult err = vkQueueSubmit(graphics_queue, 1, &submit_info, VK_NULL_HANDLE);
  1933. command_buffer_queue.write[0] = nullptr;
  1934. ERR_FAIL_COND(err);
  1935. }
  1936. if (pending_flushable) {
  1937. // Use a fence to wait for everything to finish.
  1938. VkSubmitInfo submit_info;
  1939. submit_info.sType = VK_STRUCTURE_TYPE_SUBMIT_INFO;
  1940. submit_info.pNext = nullptr;
  1941. VkPipelineStageFlags wait_stage_mask = VK_PIPELINE_STAGE_TOP_OF_PIPE_BIT;
  1942. submit_info.pWaitDstStageMask = setup_flushable ? &wait_stage_mask : nullptr;
  1943. submit_info.waitSemaphoreCount = setup_flushable ? 1 : 0;
  1944. submit_info.pWaitSemaphores = setup_flushable ? &draw_complete_semaphores[frame_index] : nullptr;
  1945. submit_info.commandBufferCount = command_buffer_count - 1;
  1946. submit_info.pCommandBuffers = command_buffer_queue.ptr() + 1;
  1947. submit_info.signalSemaphoreCount = 0;
  1948. submit_info.pSignalSemaphores = nullptr;
  1949. VkResult err = vkQueueSubmit(graphics_queue, 1, &submit_info, VK_NULL_HANDLE);
  1950. command_buffer_count = 1;
  1951. ERR_FAIL_COND(err);
  1952. }
  1953. vkDeviceWaitIdle(device);
  1954. }
  1955. Error VulkanContext::prepare_buffers() {
  1956. if (!queues_initialized) {
  1957. return OK;
  1958. }
  1959. VkResult err;
  1960. // Ensure no more than FRAME_LAG renderings are outstanding.
  1961. vkWaitForFences(device, 1, &fences[frame_index], VK_TRUE, UINT64_MAX);
  1962. vkResetFences(device, 1, &fences[frame_index]);
  1963. for (KeyValue<int, Window> &E : windows) {
  1964. Window *w = &E.value;
  1965. w->semaphore_acquired = false;
  1966. if (w->swapchain == VK_NULL_HANDLE) {
  1967. continue;
  1968. }
  1969. do {
  1970. // Get the index of the next available swapchain image.
  1971. err =
  1972. fpAcquireNextImageKHR(device, w->swapchain, UINT64_MAX,
  1973. w->image_acquired_semaphores[frame_index], VK_NULL_HANDLE, &w->current_buffer);
  1974. if (err == VK_ERROR_OUT_OF_DATE_KHR) {
  1975. // Swapchain is out of date (e.g. the window was resized) and
  1976. // must be recreated.
  1977. print_verbose("Vulkan: Early out of date swapchain, recreating.");
  1978. // resize_notify();
  1979. _update_swap_chain(w);
  1980. } else if (err == VK_SUBOPTIMAL_KHR) {
  1981. // Swapchain is not as optimal as it could be, but the platform's
  1982. // presentation engine will still present the image correctly.
  1983. print_verbose("Vulkan: Early suboptimal swapchain, recreating.");
  1984. _update_swap_chain(w);
  1985. break;
  1986. } else if (err != VK_SUCCESS) {
  1987. ERR_BREAK_MSG(err != VK_SUCCESS, "Vulkan: Did not create swapchain successfully. Error code: " + String(string_VkResult(err)));
  1988. } else {
  1989. w->semaphore_acquired = true;
  1990. }
  1991. } while (err != VK_SUCCESS);
  1992. }
  1993. buffers_prepared = true;
  1994. return OK;
  1995. }
  1996. Error VulkanContext::swap_buffers() {
  1997. if (!queues_initialized) {
  1998. return OK;
  1999. }
  2000. // print_line("swapbuffers?");
  2001. VkResult err;
  2002. #if 0
  2003. if (is_device_extension_enabled(VK_GOOGLE_DISPLAY_TIMING_EXTENSION_NAME)) {
  2004. // Look at what happened to previous presents, and make appropriate
  2005. // adjustments in timing.
  2006. DemoUpdateTargetIPD(demo);
  2007. // Note: a real application would position its geometry to that it's in
  2008. // the correct location for when the next image is presented. It might
  2009. // also wait, so that there's less latency between any input and when
  2010. // the next image is rendered/presented. This demo program is so
  2011. // simple that it doesn't do either of those.
  2012. }
  2013. #endif
  2014. // Wait for the image acquired semaphore to be signaled to ensure
  2015. // that the image won't be rendered to until the presentation
  2016. // engine has fully released ownership to the application, and it is
  2017. // okay to render to the image.
  2018. const VkCommandBuffer *commands_ptr = nullptr;
  2019. uint32_t commands_to_submit = 0;
  2020. if (command_buffer_queue[0] == nullptr) {
  2021. // No setup command, but commands to submit, submit from the first and skip command.
  2022. if (command_buffer_count > 1) {
  2023. commands_ptr = command_buffer_queue.ptr() + 1;
  2024. commands_to_submit = command_buffer_count - 1;
  2025. }
  2026. } else {
  2027. commands_ptr = command_buffer_queue.ptr();
  2028. commands_to_submit = command_buffer_count;
  2029. }
  2030. VkSemaphore *semaphores_to_acquire = (VkSemaphore *)alloca(windows.size() * sizeof(VkSemaphore));
  2031. VkPipelineStageFlags *pipe_stage_flags = (VkPipelineStageFlags *)alloca(windows.size() * sizeof(VkPipelineStageFlags));
  2032. uint32_t semaphores_to_acquire_count = 0;
  2033. for (KeyValue<int, Window> &E : windows) {
  2034. Window *w = &E.value;
  2035. if (w->semaphore_acquired) {
  2036. semaphores_to_acquire[semaphores_to_acquire_count] = w->image_acquired_semaphores[frame_index];
  2037. pipe_stage_flags[semaphores_to_acquire_count] = VK_PIPELINE_STAGE_COLOR_ATTACHMENT_OUTPUT_BIT;
  2038. semaphores_to_acquire_count++;
  2039. }
  2040. }
  2041. VkSubmitInfo submit_info;
  2042. submit_info.sType = VK_STRUCTURE_TYPE_SUBMIT_INFO;
  2043. submit_info.pNext = nullptr;
  2044. submit_info.waitSemaphoreCount = semaphores_to_acquire_count;
  2045. submit_info.pWaitSemaphores = semaphores_to_acquire;
  2046. submit_info.pWaitDstStageMask = pipe_stage_flags;
  2047. submit_info.commandBufferCount = commands_to_submit;
  2048. submit_info.pCommandBuffers = commands_ptr;
  2049. submit_info.signalSemaphoreCount = 1;
  2050. submit_info.pSignalSemaphores = &draw_complete_semaphores[frame_index];
  2051. err = vkQueueSubmit(graphics_queue, 1, &submit_info, fences[frame_index]);
  2052. ERR_FAIL_COND_V_MSG(err, ERR_CANT_CREATE, "Vulkan: Cannot submit graphics queue. Error code: " + String(string_VkResult(err)));
  2053. command_buffer_queue.write[0] = nullptr;
  2054. command_buffer_count = 1;
  2055. if (separate_present_queue) {
  2056. // If we are using separate queues, change image ownership to the
  2057. // present queue before presenting, waiting for the draw complete
  2058. // semaphore and signaling the ownership released semaphore when finished.
  2059. VkFence nullFence = VK_NULL_HANDLE;
  2060. pipe_stage_flags[0] = VK_PIPELINE_STAGE_COLOR_ATTACHMENT_OUTPUT_BIT;
  2061. submit_info.waitSemaphoreCount = 1;
  2062. submit_info.pWaitSemaphores = &draw_complete_semaphores[frame_index];
  2063. submit_info.commandBufferCount = 0;
  2064. VkCommandBuffer *cmdbufptr = (VkCommandBuffer *)alloca(sizeof(VkCommandBuffer *) * windows.size());
  2065. submit_info.pCommandBuffers = cmdbufptr;
  2066. for (KeyValue<int, Window> &E : windows) {
  2067. Window *w = &E.value;
  2068. if (w->swapchain == VK_NULL_HANDLE) {
  2069. continue;
  2070. }
  2071. cmdbufptr[submit_info.commandBufferCount] = w->swapchain_image_resources[w->current_buffer].graphics_to_present_cmd;
  2072. submit_info.commandBufferCount++;
  2073. }
  2074. submit_info.signalSemaphoreCount = 1;
  2075. submit_info.pSignalSemaphores = &image_ownership_semaphores[frame_index];
  2076. err = vkQueueSubmit(present_queue, 1, &submit_info, nullFence);
  2077. ERR_FAIL_COND_V_MSG(err, ERR_CANT_CREATE, "Vulkan: Cannot submit present queue. Error code: " + String(string_VkResult(err)));
  2078. }
  2079. // If we are using separate queues, we have to wait for image ownership,
  2080. // otherwise wait for draw complete.
  2081. VkPresentInfoKHR present = {
  2082. /*sType*/ VK_STRUCTURE_TYPE_PRESENT_INFO_KHR,
  2083. /*pNext*/ nullptr,
  2084. /*waitSemaphoreCount*/ 1,
  2085. /*pWaitSemaphores*/ (separate_present_queue) ? &image_ownership_semaphores[frame_index] : &draw_complete_semaphores[frame_index],
  2086. /*swapchainCount*/ 0,
  2087. /*pSwapchain*/ nullptr,
  2088. /*pImageIndices*/ nullptr,
  2089. /*pResults*/ nullptr,
  2090. };
  2091. VkSwapchainKHR *pSwapchains = (VkSwapchainKHR *)alloca(sizeof(VkSwapchainKHR *) * windows.size());
  2092. uint32_t *pImageIndices = (uint32_t *)alloca(sizeof(uint32_t *) * windows.size());
  2093. present.pSwapchains = pSwapchains;
  2094. present.pImageIndices = pImageIndices;
  2095. for (KeyValue<int, Window> &E : windows) {
  2096. Window *w = &E.value;
  2097. if (w->swapchain == VK_NULL_HANDLE) {
  2098. continue;
  2099. }
  2100. pSwapchains[present.swapchainCount] = w->swapchain;
  2101. pImageIndices[present.swapchainCount] = w->current_buffer;
  2102. present.swapchainCount++;
  2103. }
  2104. #if 0
  2105. if (is_device_extension_enabled(VK_KHR_incremental_present_enabled)) {
  2106. // If using VK_KHR_incremental_present, we provide a hint of the region
  2107. // that contains changed content relative to the previously-presented
  2108. // image. The implementation can use this hint in order to save
  2109. // work/power (by only copying the region in the hint). The
  2110. // implementation is free to ignore the hint though, and so we must
  2111. // ensure that the entire image has the correctly-drawn content.
  2112. uint32_t eighthOfWidth = width / 8;
  2113. uint32_t eighthOfHeight = height / 8;
  2114. VkRectLayerKHR rect = {
  2115. /*offset.x*/ eighthOfWidth,
  2116. /*offset.y*/ eighthOfHeight,
  2117. /*extent.width*/ eighthOfWidth * 6,
  2118. /*extent.height*/ eighthOfHeight * 6,
  2119. /*layer*/ 0,
  2120. };
  2121. VkPresentRegionKHR region = {
  2122. /*rectangleCount*/ 1,
  2123. /*pRectangles*/ &rect,
  2124. };
  2125. VkPresentRegionsKHR regions = {
  2126. /*sType*/ VK_STRUCTURE_TYPE_PRESENT_REGIONS_KHR,
  2127. /*pNext*/ present.pNext,
  2128. /*swapchainCount*/ present.swapchainCount,
  2129. /*pRegions*/ &region,
  2130. };
  2131. present.pNext = &regions;
  2132. }
  2133. #endif
  2134. #if 0
  2135. if (is_device_extension_enabled(VK_GOOGLE_DISPLAY_TIMING_EXTENSION_NAME)) {
  2136. VkPresentTimeGOOGLE ptime;
  2137. if (prev_desired_present_time == 0) {
  2138. // This must be the first present for this swapchain.
  2139. //
  2140. // We don't know where we are relative to the presentation engine's
  2141. // display's refresh cycle. We also don't know how long rendering
  2142. // takes. Let's make a grossly-simplified assumption that the
  2143. // desiredPresentTime should be half way between now and
  2144. // now+target_IPD. We will adjust over time.
  2145. uint64_t curtime = getTimeInNanoseconds();
  2146. if (curtime == 0) {
  2147. // Since we didn't find out the current time, don't give a
  2148. // desiredPresentTime.
  2149. ptime.desiredPresentTime = 0;
  2150. } else {
  2151. ptime.desiredPresentTime = curtime + (target_IPD >> 1);
  2152. }
  2153. } else {
  2154. ptime.desiredPresentTime = (prev_desired_present_time + target_IPD);
  2155. }
  2156. ptime.presentID = next_present_id++;
  2157. prev_desired_present_time = ptime.desiredPresentTime;
  2158. VkPresentTimesInfoGOOGLE present_time = {
  2159. /*sType*/ VK_STRUCTURE_TYPE_PRESENT_TIMES_INFO_GOOGLE,
  2160. /*pNext*/ present.pNext,
  2161. /*swapchainCount*/ present.swapchainCount,
  2162. /*pTimes*/ &ptime,
  2163. };
  2164. if (is_device_extension_enabled(VK_GOOGLE_DISPLAY_TIMING_EXTENSION_NAME)) {
  2165. present.pNext = &present_time;
  2166. }
  2167. }
  2168. #endif
  2169. // print_line("current buffer: " + itos(current_buffer));
  2170. err = fpQueuePresentKHR(present_queue, &present);
  2171. frame_index += 1;
  2172. frame_index %= FRAME_LAG;
  2173. if (err == VK_ERROR_OUT_OF_DATE_KHR) {
  2174. // Swapchain is out of date (e.g. the window was resized) and
  2175. // must be recreated.
  2176. print_verbose("Vulkan queue submit: Swapchain is out of date, recreating.");
  2177. resize_notify();
  2178. } else if (err == VK_SUBOPTIMAL_KHR) {
  2179. // Swapchain is not as optimal as it could be, but the platform's
  2180. // presentation engine will still present the image correctly.
  2181. print_verbose("Vulkan queue submit: Swapchain is suboptimal.");
  2182. } else {
  2183. ERR_FAIL_COND_V_MSG(err, ERR_CANT_CREATE, "Error code: " + String(string_VkResult(err)));
  2184. }
  2185. buffers_prepared = false;
  2186. return OK;
  2187. }
  2188. void VulkanContext::resize_notify() {
  2189. }
  2190. VkDevice VulkanContext::get_device() {
  2191. return device;
  2192. }
  2193. VkPhysicalDevice VulkanContext::get_physical_device() {
  2194. return gpu;
  2195. }
  2196. int VulkanContext::get_swapchain_image_count() const {
  2197. return swapchainImageCount;
  2198. }
  2199. VkQueue VulkanContext::get_graphics_queue() const {
  2200. return graphics_queue;
  2201. }
  2202. uint32_t VulkanContext::get_graphics_queue_family_index() const {
  2203. return graphics_queue_family_index;
  2204. }
  2205. VkFormat VulkanContext::get_screen_format() const {
  2206. return format;
  2207. }
  2208. VkPhysicalDeviceLimits VulkanContext::get_device_limits() const {
  2209. return gpu_props.limits;
  2210. }
  2211. RID VulkanContext::local_device_create() {
  2212. LocalDevice ld;
  2213. { // Create device.
  2214. VkResult err;
  2215. float queue_priorities[1] = { 0.0 };
  2216. VkDeviceQueueCreateInfo queues[2];
  2217. queues[0].sType = VK_STRUCTURE_TYPE_DEVICE_QUEUE_CREATE_INFO;
  2218. queues[0].pNext = nullptr;
  2219. queues[0].queueFamilyIndex = graphics_queue_family_index;
  2220. queues[0].queueCount = 1;
  2221. queues[0].pQueuePriorities = queue_priorities;
  2222. queues[0].flags = 0;
  2223. uint32_t enabled_extension_count = 0;
  2224. const char *enabled_extension_names[MAX_EXTENSIONS];
  2225. ERR_FAIL_COND_V(enabled_device_extension_names.size() > MAX_EXTENSIONS, RID());
  2226. for (const CharString &extension_name : enabled_device_extension_names) {
  2227. enabled_extension_names[enabled_extension_count++] = extension_name.ptr();
  2228. }
  2229. VkDeviceCreateInfo sdevice = {
  2230. /*sType =*/VK_STRUCTURE_TYPE_DEVICE_CREATE_INFO,
  2231. /*pNext */ nullptr,
  2232. /*flags */ 0,
  2233. /*queueCreateInfoCount */ 1,
  2234. /*pQueueCreateInfos */ queues,
  2235. /*enabledLayerCount */ 0,
  2236. /*ppEnabledLayerNames */ nullptr,
  2237. /*enabledExtensionCount */ enabled_extension_count,
  2238. /*ppEnabledExtensionNames */ (const char *const *)enabled_extension_names,
  2239. /*pEnabledFeatures */ &physical_device_features, // If specific features are required, pass them in here.
  2240. };
  2241. err = vkCreateDevice(gpu, &sdevice, nullptr, &ld.device);
  2242. ERR_FAIL_COND_V(err, RID());
  2243. }
  2244. { // Create graphics queue.
  2245. vkGetDeviceQueue(ld.device, graphics_queue_family_index, 0, &ld.queue);
  2246. }
  2247. return local_device_owner.make_rid(ld);
  2248. }
  2249. VkDevice VulkanContext::local_device_get_vk_device(RID p_local_device) {
  2250. LocalDevice *ld = local_device_owner.get_or_null(p_local_device);
  2251. return ld->device;
  2252. }
  2253. void VulkanContext::local_device_push_command_buffers(RID p_local_device, const VkCommandBuffer *p_buffers, int p_count) {
  2254. LocalDevice *ld = local_device_owner.get_or_null(p_local_device);
  2255. ERR_FAIL_COND(ld->waiting);
  2256. VkSubmitInfo submit_info;
  2257. submit_info.sType = VK_STRUCTURE_TYPE_SUBMIT_INFO;
  2258. submit_info.pNext = nullptr;
  2259. submit_info.pWaitDstStageMask = nullptr;
  2260. submit_info.waitSemaphoreCount = 0;
  2261. submit_info.pWaitSemaphores = nullptr;
  2262. submit_info.commandBufferCount = p_count;
  2263. submit_info.pCommandBuffers = p_buffers;
  2264. submit_info.signalSemaphoreCount = 0;
  2265. submit_info.pSignalSemaphores = nullptr;
  2266. VkResult err = vkQueueSubmit(ld->queue, 1, &submit_info, VK_NULL_HANDLE);
  2267. if (err == VK_ERROR_OUT_OF_HOST_MEMORY) {
  2268. print_line("Vulkan: Out of host memory!");
  2269. }
  2270. if (err == VK_ERROR_OUT_OF_DEVICE_MEMORY) {
  2271. print_line("Vulkan: Out of device memory!");
  2272. }
  2273. if (err == VK_ERROR_DEVICE_LOST) {
  2274. print_line("Vulkan: Device lost!");
  2275. }
  2276. ERR_FAIL_COND(err);
  2277. ld->waiting = true;
  2278. }
  2279. void VulkanContext::local_device_sync(RID p_local_device) {
  2280. LocalDevice *ld = local_device_owner.get_or_null(p_local_device);
  2281. ERR_FAIL_COND(!ld->waiting);
  2282. vkDeviceWaitIdle(ld->device);
  2283. ld->waiting = false;
  2284. }
  2285. void VulkanContext::local_device_free(RID p_local_device) {
  2286. LocalDevice *ld = local_device_owner.get_or_null(p_local_device);
  2287. vkDestroyDevice(ld->device, nullptr);
  2288. local_device_owner.free(p_local_device);
  2289. }
  2290. void VulkanContext::command_begin_label(VkCommandBuffer p_command_buffer, String p_label_name, const Color p_color) {
  2291. if (!is_instance_extension_enabled(VK_EXT_DEBUG_UTILS_EXTENSION_NAME)) {
  2292. return;
  2293. }
  2294. CharString cs = p_label_name.utf8();
  2295. VkDebugUtilsLabelEXT label;
  2296. label.sType = VK_STRUCTURE_TYPE_DEBUG_UTILS_LABEL_EXT;
  2297. label.pNext = nullptr;
  2298. label.pLabelName = cs.get_data();
  2299. label.color[0] = p_color[0];
  2300. label.color[1] = p_color[1];
  2301. label.color[2] = p_color[2];
  2302. label.color[3] = p_color[3];
  2303. CmdBeginDebugUtilsLabelEXT(p_command_buffer, &label);
  2304. }
  2305. void VulkanContext::command_insert_label(VkCommandBuffer p_command_buffer, String p_label_name, const Color p_color) {
  2306. if (!is_instance_extension_enabled(VK_EXT_DEBUG_UTILS_EXTENSION_NAME)) {
  2307. return;
  2308. }
  2309. CharString cs = p_label_name.utf8();
  2310. VkDebugUtilsLabelEXT label;
  2311. label.sType = VK_STRUCTURE_TYPE_DEBUG_UTILS_LABEL_EXT;
  2312. label.pNext = nullptr;
  2313. label.pLabelName = cs.get_data();
  2314. label.color[0] = p_color[0];
  2315. label.color[1] = p_color[1];
  2316. label.color[2] = p_color[2];
  2317. label.color[3] = p_color[3];
  2318. CmdInsertDebugUtilsLabelEXT(p_command_buffer, &label);
  2319. }
  2320. void VulkanContext::command_end_label(VkCommandBuffer p_command_buffer) {
  2321. if (!is_instance_extension_enabled(VK_EXT_DEBUG_UTILS_EXTENSION_NAME)) {
  2322. return;
  2323. }
  2324. CmdEndDebugUtilsLabelEXT(p_command_buffer);
  2325. }
  2326. void VulkanContext::set_object_name(VkObjectType p_object_type, uint64_t p_object_handle, String p_object_name) {
  2327. if (!is_instance_extension_enabled(VK_EXT_DEBUG_UTILS_EXTENSION_NAME)) {
  2328. return;
  2329. }
  2330. CharString obj_data = p_object_name.utf8();
  2331. VkDebugUtilsObjectNameInfoEXT name_info;
  2332. name_info.sType = VK_STRUCTURE_TYPE_DEBUG_UTILS_OBJECT_NAME_INFO_EXT;
  2333. name_info.pNext = nullptr;
  2334. name_info.objectType = p_object_type;
  2335. name_info.objectHandle = p_object_handle;
  2336. name_info.pObjectName = obj_data.get_data();
  2337. SetDebugUtilsObjectNameEXT(device, &name_info);
  2338. }
  2339. String VulkanContext::get_device_vendor_name() const {
  2340. return device_vendor;
  2341. }
  2342. String VulkanContext::get_device_name() const {
  2343. return device_name;
  2344. }
  2345. RenderingDevice::DeviceType VulkanContext::get_device_type() const {
  2346. return RenderingDevice::DeviceType(device_type);
  2347. }
  2348. String VulkanContext::get_device_api_version() const {
  2349. return vformat("%d.%d.%d", VK_API_VERSION_MAJOR(device_api_version), VK_API_VERSION_MINOR(device_api_version), VK_API_VERSION_PATCH(device_api_version));
  2350. }
  2351. String VulkanContext::get_device_pipeline_cache_uuid() const {
  2352. return pipeline_cache_id;
  2353. }
  2354. DisplayServer::VSyncMode VulkanContext::get_vsync_mode(DisplayServer::WindowID p_window) const {
  2355. ERR_FAIL_COND_V_MSG(!windows.has(p_window), DisplayServer::VSYNC_ENABLED, "Could not get V-Sync mode for window with WindowID " + itos(p_window) + " because it does not exist.");
  2356. return windows[p_window].vsync_mode;
  2357. }
  2358. void VulkanContext::set_vsync_mode(DisplayServer::WindowID p_window, DisplayServer::VSyncMode p_mode) {
  2359. ERR_FAIL_COND_MSG(!windows.has(p_window), "Could not set V-Sync mode for window with WindowID " + itos(p_window) + " because it does not exist.");
  2360. windows[p_window].vsync_mode = p_mode;
  2361. _update_swap_chain(&windows[p_window]);
  2362. }
  2363. VulkanContext::VulkanContext() {
  2364. command_buffer_queue.resize(1); // First one is always the setup command.
  2365. command_buffer_queue.write[0] = nullptr;
  2366. }
  2367. VulkanContext::~VulkanContext() {
  2368. if (queue_props) {
  2369. free(queue_props);
  2370. }
  2371. if (device_initialized) {
  2372. for (uint32_t i = 0; i < FRAME_LAG; i++) {
  2373. vkDestroyFence(device, fences[i], nullptr);
  2374. vkDestroySemaphore(device, draw_complete_semaphores[i], nullptr);
  2375. if (separate_present_queue) {
  2376. vkDestroySemaphore(device, image_ownership_semaphores[i], nullptr);
  2377. }
  2378. }
  2379. if (inst_initialized && is_instance_extension_enabled(VK_EXT_DEBUG_UTILS_EXTENSION_NAME)) {
  2380. DestroyDebugUtilsMessengerEXT(inst, dbg_messenger, nullptr);
  2381. }
  2382. if (inst_initialized && dbg_debug_report != VK_NULL_HANDLE) {
  2383. DestroyDebugReportCallbackEXT(inst, dbg_debug_report, nullptr);
  2384. }
  2385. vkDestroyDevice(device, nullptr);
  2386. }
  2387. if (inst_initialized) {
  2388. vkDestroyInstance(inst, nullptr);
  2389. }
  2390. }