vulkan_context.cpp 82 KB

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