vulkan_context.cpp 98 KB

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