vulkan_context.cpp 93 KB

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