vulkan_context.cpp 80 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193
  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. uint32_t vulkan_patch = VK_VERSION_PATCH(api_version);
  248. print_line("Vulkan API " + itos(vulkan_major) + "." + itos(vulkan_minor) + "." + itos(vulkan_patch));
  249. } else {
  250. // according to the documentation this shouldn't fail with anything except a memory allocation error
  251. // in which case we're in deep trouble anyway
  252. ERR_FAIL_V(ERR_CANT_CREATE);
  253. }
  254. } else {
  255. print_line("vkEnumerateInstanceVersion not available, assuming Vulkan 1.0");
  256. }
  257. // we don't go above 1.2
  258. if ((vulkan_major > 1) || (vulkan_major == 1 && vulkan_minor > 2)) {
  259. vulkan_major = 1;
  260. vulkan_minor = 2;
  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 = NULL;
  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. }
  478. // check extended properties
  479. PFN_vkGetPhysicalDeviceProperties2 device_properties_func = (PFN_vkGetPhysicalDeviceProperties2)vkGetInstanceProcAddr(inst, "vkGetPhysicalDeviceProperties2");
  480. if (device_properties_func == nullptr) {
  481. // In Vulkan 1.0 might be accessible under its original extension name
  482. device_properties_func = (PFN_vkGetPhysicalDeviceProperties2)vkGetInstanceProcAddr(inst, "vkGetPhysicalDeviceProperties2KHR");
  483. }
  484. if (device_properties_func != nullptr) {
  485. VkPhysicalDeviceMultiviewProperties multiviewProperties;
  486. VkPhysicalDeviceSubgroupProperties subgroupProperties;
  487. VkPhysicalDeviceProperties2 physicalDeviceProperties;
  488. subgroupProperties.sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_SUBGROUP_PROPERTIES;
  489. subgroupProperties.pNext = nullptr;
  490. physicalDeviceProperties.sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_PROPERTIES_2;
  491. if (multiview_capabilities.is_supported) {
  492. multiviewProperties.sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_MULTIVIEW_PROPERTIES;
  493. multiviewProperties.pNext = &subgroupProperties;
  494. physicalDeviceProperties.pNext = &multiviewProperties;
  495. } else {
  496. physicalDeviceProperties.pNext = &subgroupProperties;
  497. }
  498. device_properties_func(gpu, &physicalDeviceProperties);
  499. subgroup_capabilities.size = subgroupProperties.subgroupSize;
  500. subgroup_capabilities.supportedStages = subgroupProperties.supportedStages;
  501. subgroup_capabilities.supportedOperations = subgroupProperties.supportedOperations;
  502. // Note: quadOperationsInAllStages will be true if:
  503. // - supportedStages has VK_SHADER_STAGE_ALL_GRAPHICS + VK_SHADER_STAGE_COMPUTE_BIT
  504. // - supportedOperations has VK_SUBGROUP_FEATURE_QUAD_BIT
  505. subgroup_capabilities.quadOperationsInAllStages = subgroupProperties.quadOperationsInAllStages;
  506. if (multiview_capabilities.is_supported) {
  507. multiview_capabilities.max_view_count = multiviewProperties.maxMultiviewViewCount;
  508. multiview_capabilities.max_instance_count = multiviewProperties.maxMultiviewInstanceIndex;
  509. #ifdef DEBUG_ENABLED
  510. print_line("- Vulkan multiview supported:");
  511. print_line(" max view count: " + itos(multiview_capabilities.max_view_count));
  512. print_line(" max instances: " + itos(multiview_capabilities.max_instance_count));
  513. } else {
  514. print_line("- Vulkan multiview not supported");
  515. #endif
  516. }
  517. #ifdef DEBUG_ENABLED
  518. print_line("- Vulkan subgroup:");
  519. print_line(" size: " + itos(subgroup_capabilities.size));
  520. print_line(" stages: " + subgroup_capabilities.supported_stages_desc());
  521. print_line(" supported ops: " + subgroup_capabilities.supported_operations_desc());
  522. if (subgroup_capabilities.quadOperationsInAllStages) {
  523. print_line(" quad operations in all stages");
  524. }
  525. } else {
  526. print_line("- Couldn't call vkGetPhysicalDeviceProperties2");
  527. #endif
  528. }
  529. return OK;
  530. }
  531. Error VulkanContext::_create_physical_device() {
  532. /* obtain version */
  533. _obtain_vulkan_version();
  534. /* initialise extensions */
  535. {
  536. Error err = _initialize_extensions();
  537. if (err != OK) {
  538. return err;
  539. }
  540. }
  541. CharString cs = ProjectSettings::get_singleton()->get("application/config/name").operator String().utf8();
  542. String name = "GodotEngine " + String(VERSION_FULL_NAME);
  543. CharString namecs = name.utf8();
  544. const VkApplicationInfo app = {
  545. /*sType*/ VK_STRUCTURE_TYPE_APPLICATION_INFO,
  546. /*pNext*/ nullptr,
  547. /*pApplicationName*/ cs.get_data(),
  548. /*applicationVersion*/ 0,
  549. /*pEngineName*/ namecs.get_data(),
  550. /*engineVersion*/ 0,
  551. /*apiVersion*/ VK_MAKE_VERSION(vulkan_major, vulkan_minor, 0)
  552. };
  553. VkInstanceCreateInfo inst_info{};
  554. inst_info.sType = VK_STRUCTURE_TYPE_INSTANCE_CREATE_INFO;
  555. inst_info.pApplicationInfo = &app;
  556. inst_info.enabledExtensionCount = enabled_extension_count;
  557. inst_info.ppEnabledExtensionNames = (const char *const *)extension_names;
  558. if (_use_validation_layers()) {
  559. _get_preferred_validation_layers(&inst_info.enabledLayerCount, &inst_info.ppEnabledLayerNames);
  560. }
  561. /*
  562. * This is info for a temp callback to use during CreateInstance.
  563. * After the instance is created, we use the instance-based
  564. * function to register the final callback.
  565. */
  566. VkDebugUtilsMessengerCreateInfoEXT dbg_messenger_create_info;
  567. VkDebugReportCallbackCreateInfoEXT dbg_report_callback_create_info{};
  568. if (enabled_debug_utils) {
  569. // VK_EXT_debug_utils style
  570. dbg_messenger_create_info.sType = VK_STRUCTURE_TYPE_DEBUG_UTILS_MESSENGER_CREATE_INFO_EXT;
  571. dbg_messenger_create_info.pNext = nullptr;
  572. dbg_messenger_create_info.flags = 0;
  573. dbg_messenger_create_info.messageSeverity =
  574. VK_DEBUG_UTILS_MESSAGE_SEVERITY_WARNING_BIT_EXT | VK_DEBUG_UTILS_MESSAGE_SEVERITY_ERROR_BIT_EXT;
  575. dbg_messenger_create_info.messageType = VK_DEBUG_UTILS_MESSAGE_TYPE_GENERAL_BIT_EXT |
  576. VK_DEBUG_UTILS_MESSAGE_TYPE_VALIDATION_BIT_EXT |
  577. VK_DEBUG_UTILS_MESSAGE_TYPE_PERFORMANCE_BIT_EXT;
  578. dbg_messenger_create_info.pfnUserCallback = _debug_messenger_callback;
  579. dbg_messenger_create_info.pUserData = this;
  580. inst_info.pNext = &dbg_messenger_create_info;
  581. } else if (enabled_debug_report) {
  582. dbg_report_callback_create_info.sType = VK_STRUCTURE_TYPE_DEBUG_REPORT_CALLBACK_CREATE_INFO_EXT;
  583. dbg_report_callback_create_info.flags = VK_DEBUG_REPORT_INFORMATION_BIT_EXT |
  584. VK_DEBUG_REPORT_WARNING_BIT_EXT |
  585. VK_DEBUG_REPORT_PERFORMANCE_WARNING_BIT_EXT |
  586. VK_DEBUG_REPORT_ERROR_BIT_EXT |
  587. VK_DEBUG_REPORT_DEBUG_BIT_EXT;
  588. dbg_report_callback_create_info.pfnCallback = _debug_report_callback;
  589. dbg_report_callback_create_info.pUserData = this;
  590. inst_info.pNext = &dbg_report_callback_create_info;
  591. }
  592. uint32_t gpu_count;
  593. VkResult err = vkCreateInstance(&inst_info, nullptr, &inst);
  594. ERR_FAIL_COND_V_MSG(err == VK_ERROR_INCOMPATIBLE_DRIVER, ERR_CANT_CREATE,
  595. "Cannot find a compatible Vulkan installable client driver (ICD).\n\n"
  596. "vkCreateInstance Failure");
  597. ERR_FAIL_COND_V_MSG(err == VK_ERROR_EXTENSION_NOT_PRESENT, ERR_CANT_CREATE,
  598. "Cannot find a specified extension library.\n"
  599. "Make sure your layers path is set appropriately.\n"
  600. "vkCreateInstance Failure");
  601. ERR_FAIL_COND_V_MSG(err, ERR_CANT_CREATE,
  602. "vkCreateInstance failed.\n\n"
  603. "Do you have a compatible Vulkan installable client driver (ICD) installed?\n"
  604. "Please look at the Getting Started guide for additional information.\n"
  605. "vkCreateInstance Failure");
  606. inst_initialized = true;
  607. /* Make initial call to query gpu_count, then second call for gpu info*/
  608. err = vkEnumeratePhysicalDevices(inst, &gpu_count, nullptr);
  609. ERR_FAIL_COND_V(err, ERR_CANT_CREATE);
  610. ERR_FAIL_COND_V_MSG(gpu_count == 0, ERR_CANT_CREATE,
  611. "vkEnumeratePhysicalDevices reported zero accessible devices.\n\n"
  612. "Do you have a compatible Vulkan installable client driver (ICD) installed?\n"
  613. "vkEnumeratePhysicalDevices Failure");
  614. VkPhysicalDevice *physical_devices = (VkPhysicalDevice *)malloc(sizeof(VkPhysicalDevice) * gpu_count);
  615. err = vkEnumeratePhysicalDevices(inst, &gpu_count, physical_devices);
  616. if (err) {
  617. free(physical_devices);
  618. ERR_FAIL_V(ERR_CANT_CREATE);
  619. }
  620. // TODO: At least on Linux Laptops integrated GPUs fail with Vulkan in many instances.
  621. // The device should really be a preference, but for now choosing a discrete GPU over the
  622. // integrated one is better than the default.
  623. // Default to first device
  624. uint32_t device_index = 0;
  625. for (uint32_t i = 0; i < gpu_count; ++i) {
  626. VkPhysicalDeviceProperties props;
  627. vkGetPhysicalDeviceProperties(physical_devices[i], &props);
  628. if (props.deviceType == VkPhysicalDeviceType::VK_PHYSICAL_DEVICE_TYPE_DISCRETE_GPU) {
  629. // Prefer discrete GPU.
  630. device_index = i;
  631. break;
  632. }
  633. }
  634. gpu = physical_devices[device_index];
  635. free(physical_devices);
  636. /* Look for device extensions */
  637. uint32_t device_extension_count = 0;
  638. VkBool32 swapchainExtFound = 0;
  639. enabled_extension_count = 0;
  640. memset(extension_names, 0, sizeof(extension_names));
  641. /* Get identifier properties */
  642. vkGetPhysicalDeviceProperties(gpu, &gpu_props);
  643. static const struct {
  644. uint32_t id;
  645. const char *name;
  646. } vendor_names[] = {
  647. { 0x1002, "AMD" },
  648. { 0x1010, "ImgTec" },
  649. { 0x10DE, "NVIDIA" },
  650. { 0x13B5, "ARM" },
  651. { 0x5143, "Qualcomm" },
  652. { 0x8086, "INTEL" },
  653. { 0, nullptr },
  654. };
  655. device_name = gpu_props.deviceName;
  656. pipeline_cache_id = String::hex_encode_buffer(gpu_props.pipelineCacheUUID, VK_UUID_SIZE);
  657. pipeline_cache_id += "-driver-" + itos(gpu_props.driverVersion);
  658. {
  659. device_vendor = "Unknown";
  660. uint32_t vendor_idx = 0;
  661. while (vendor_names[vendor_idx].name != nullptr) {
  662. if (gpu_props.vendorID == vendor_names[vendor_idx].id) {
  663. device_vendor = vendor_names[vendor_idx].name;
  664. break;
  665. }
  666. vendor_idx++;
  667. }
  668. }
  669. #ifdef DEBUG_ENABLED
  670. print_line("Using Vulkan Device #" + itos(device_index) + ": " + device_vendor + " - " + device_name);
  671. #endif
  672. device_api_version = gpu_props.apiVersion;
  673. err = vkEnumerateDeviceExtensionProperties(gpu, nullptr, &device_extension_count, nullptr);
  674. ERR_FAIL_COND_V(err, ERR_CANT_CREATE);
  675. if (device_extension_count > 0) {
  676. VkExtensionProperties *device_extensions = (VkExtensionProperties *)malloc(sizeof(VkExtensionProperties) * device_extension_count);
  677. err = vkEnumerateDeviceExtensionProperties(gpu, nullptr, &device_extension_count, device_extensions);
  678. if (err) {
  679. free(device_extensions);
  680. ERR_FAIL_V(ERR_CANT_CREATE);
  681. }
  682. for (uint32_t i = 0; i < device_extension_count; i++) {
  683. if (!strcmp(VK_KHR_SWAPCHAIN_EXTENSION_NAME, device_extensions[i].extensionName)) {
  684. swapchainExtFound = 1;
  685. extension_names[enabled_extension_count++] = VK_KHR_SWAPCHAIN_EXTENSION_NAME;
  686. }
  687. if (!strcmp(VK_KHR_MULTIVIEW_EXTENSION_NAME, device_extensions[i].extensionName)) {
  688. // if multiview is supported, enable it
  689. extension_names[enabled_extension_count++] = VK_KHR_MULTIVIEW_EXTENSION_NAME;
  690. }
  691. if (enabled_extension_count >= MAX_EXTENSIONS) {
  692. free(device_extensions);
  693. ERR_FAIL_V_MSG(ERR_BUG, "Enabled extension count reaches MAX_EXTENSIONS, BUG");
  694. }
  695. }
  696. if (VK_KHR_incremental_present_enabled) {
  697. // Even though the user "enabled" the extension via the command
  698. // line, we must make sure that it's enumerated for use with the
  699. // device. Therefore, disable it here, and re-enable it again if
  700. // enumerated.
  701. VK_KHR_incremental_present_enabled = false;
  702. for (uint32_t i = 0; i < device_extension_count; i++) {
  703. if (!strcmp(VK_KHR_INCREMENTAL_PRESENT_EXTENSION_NAME, device_extensions[i].extensionName)) {
  704. extension_names[enabled_extension_count++] = VK_KHR_INCREMENTAL_PRESENT_EXTENSION_NAME;
  705. VK_KHR_incremental_present_enabled = true;
  706. }
  707. if (enabled_extension_count >= MAX_EXTENSIONS) {
  708. free(device_extensions);
  709. ERR_FAIL_V_MSG(ERR_BUG, "Enabled extension count reaches MAX_EXTENSIONS, BUG");
  710. }
  711. }
  712. }
  713. if (VK_GOOGLE_display_timing_enabled) {
  714. // Even though the user "enabled" the extension via the command
  715. // line, we must make sure that it's enumerated for use with the
  716. // device. Therefore, disable it here, and re-enable it again if
  717. // enumerated.
  718. VK_GOOGLE_display_timing_enabled = false;
  719. for (uint32_t i = 0; i < device_extension_count; i++) {
  720. if (!strcmp(VK_GOOGLE_DISPLAY_TIMING_EXTENSION_NAME, device_extensions[i].extensionName)) {
  721. extension_names[enabled_extension_count++] = VK_GOOGLE_DISPLAY_TIMING_EXTENSION_NAME;
  722. VK_GOOGLE_display_timing_enabled = true;
  723. }
  724. if (enabled_extension_count >= MAX_EXTENSIONS) {
  725. free(device_extensions);
  726. ERR_FAIL_V_MSG(ERR_BUG, "Enabled extension count reaches MAX_EXTENSIONS, BUG");
  727. }
  728. }
  729. }
  730. free(device_extensions);
  731. }
  732. ERR_FAIL_COND_V_MSG(!swapchainExtFound, ERR_CANT_CREATE,
  733. "vkEnumerateDeviceExtensionProperties failed to find the " VK_KHR_SWAPCHAIN_EXTENSION_NAME
  734. " extension.\n\nDo you have a compatible Vulkan installable client driver (ICD) installed?\n"
  735. "vkCreateInstance Failure");
  736. if (enabled_debug_utils) {
  737. // Setup VK_EXT_debug_utils function pointers always (we use them for
  738. // debug labels and names).
  739. CreateDebugUtilsMessengerEXT =
  740. (PFN_vkCreateDebugUtilsMessengerEXT)vkGetInstanceProcAddr(inst, "vkCreateDebugUtilsMessengerEXT");
  741. DestroyDebugUtilsMessengerEXT =
  742. (PFN_vkDestroyDebugUtilsMessengerEXT)vkGetInstanceProcAddr(inst, "vkDestroyDebugUtilsMessengerEXT");
  743. SubmitDebugUtilsMessageEXT =
  744. (PFN_vkSubmitDebugUtilsMessageEXT)vkGetInstanceProcAddr(inst, "vkSubmitDebugUtilsMessageEXT");
  745. CmdBeginDebugUtilsLabelEXT =
  746. (PFN_vkCmdBeginDebugUtilsLabelEXT)vkGetInstanceProcAddr(inst, "vkCmdBeginDebugUtilsLabelEXT");
  747. CmdEndDebugUtilsLabelEXT =
  748. (PFN_vkCmdEndDebugUtilsLabelEXT)vkGetInstanceProcAddr(inst, "vkCmdEndDebugUtilsLabelEXT");
  749. CmdInsertDebugUtilsLabelEXT =
  750. (PFN_vkCmdInsertDebugUtilsLabelEXT)vkGetInstanceProcAddr(inst, "vkCmdInsertDebugUtilsLabelEXT");
  751. SetDebugUtilsObjectNameEXT =
  752. (PFN_vkSetDebugUtilsObjectNameEXT)vkGetInstanceProcAddr(inst, "vkSetDebugUtilsObjectNameEXT");
  753. if (nullptr == CreateDebugUtilsMessengerEXT || nullptr == DestroyDebugUtilsMessengerEXT ||
  754. nullptr == SubmitDebugUtilsMessageEXT || nullptr == CmdBeginDebugUtilsLabelEXT ||
  755. nullptr == CmdEndDebugUtilsLabelEXT || nullptr == CmdInsertDebugUtilsLabelEXT ||
  756. nullptr == SetDebugUtilsObjectNameEXT) {
  757. ERR_FAIL_V_MSG(ERR_CANT_CREATE,
  758. "GetProcAddr: Failed to init VK_EXT_debug_utils\n"
  759. "GetProcAddr: Failure");
  760. }
  761. err = CreateDebugUtilsMessengerEXT(inst, &dbg_messenger_create_info, nullptr, &dbg_messenger);
  762. switch (err) {
  763. case VK_SUCCESS:
  764. break;
  765. case VK_ERROR_OUT_OF_HOST_MEMORY:
  766. ERR_FAIL_V_MSG(ERR_CANT_CREATE,
  767. "CreateDebugUtilsMessengerEXT: out of host memory\n"
  768. "CreateDebugUtilsMessengerEXT Failure");
  769. break;
  770. default:
  771. ERR_FAIL_V_MSG(ERR_CANT_CREATE,
  772. "CreateDebugUtilsMessengerEXT: unknown failure\n"
  773. "CreateDebugUtilsMessengerEXT Failure");
  774. ERR_FAIL_V(ERR_CANT_CREATE);
  775. break;
  776. }
  777. } else if (enabled_debug_report) {
  778. CreateDebugReportCallbackEXT = (PFN_vkCreateDebugReportCallbackEXT)vkGetInstanceProcAddr(inst, "vkCreateDebugReportCallbackEXT");
  779. DebugReportMessageEXT = (PFN_vkDebugReportMessageEXT)vkGetInstanceProcAddr(inst, "vkDebugReportMessageEXT");
  780. DestroyDebugReportCallbackEXT = (PFN_vkDestroyDebugReportCallbackEXT)vkGetInstanceProcAddr(inst, "vkDestroyDebugReportCallbackEXT");
  781. if (nullptr == CreateDebugReportCallbackEXT || nullptr == DebugReportMessageEXT || nullptr == DestroyDebugReportCallbackEXT) {
  782. ERR_FAIL_V_MSG(ERR_CANT_CREATE,
  783. "GetProcAddr: Failed to init VK_EXT_debug_report\n"
  784. "GetProcAddr: Failure");
  785. }
  786. err = CreateDebugReportCallbackEXT(inst, &dbg_report_callback_create_info, nullptr, &dbg_debug_report);
  787. switch (err) {
  788. case VK_SUCCESS:
  789. break;
  790. case VK_ERROR_OUT_OF_HOST_MEMORY:
  791. ERR_FAIL_V_MSG(ERR_CANT_CREATE,
  792. "CreateDebugReportCallbackEXT: out of host memory\n"
  793. "CreateDebugReportCallbackEXT Failure");
  794. break;
  795. default:
  796. ERR_FAIL_V_MSG(ERR_CANT_CREATE,
  797. "CreateDebugReportCallbackEXT: unknown failure\n"
  798. "CreateDebugReportCallbackEXT Failure");
  799. ERR_FAIL_V(ERR_CANT_CREATE);
  800. break;
  801. }
  802. }
  803. /* Call with nullptr data to get count */
  804. vkGetPhysicalDeviceQueueFamilyProperties(gpu, &queue_family_count, nullptr);
  805. ERR_FAIL_COND_V(queue_family_count == 0, ERR_CANT_CREATE);
  806. queue_props = (VkQueueFamilyProperties *)malloc(queue_family_count * sizeof(VkQueueFamilyProperties));
  807. vkGetPhysicalDeviceQueueFamilyProperties(gpu, &queue_family_count, queue_props);
  808. // Query fine-grained feature support for this device.
  809. // If app has specific feature requirements it should check supported
  810. // features based on this query
  811. vkGetPhysicalDeviceFeatures(gpu, &physical_device_features);
  812. physical_device_features.robustBufferAccess = false; //turn off robust buffer access, which can hamper performance on some hardware
  813. #define GET_INSTANCE_PROC_ADDR(inst, entrypoint) \
  814. { \
  815. fp##entrypoint = (PFN_vk##entrypoint)vkGetInstanceProcAddr(inst, "vk" #entrypoint); \
  816. ERR_FAIL_COND_V_MSG(fp##entrypoint == nullptr, ERR_CANT_CREATE, \
  817. "vkGetInstanceProcAddr failed to find vk" #entrypoint); \
  818. }
  819. GET_INSTANCE_PROC_ADDR(inst, GetPhysicalDeviceSurfaceSupportKHR);
  820. GET_INSTANCE_PROC_ADDR(inst, GetPhysicalDeviceSurfaceCapabilitiesKHR);
  821. GET_INSTANCE_PROC_ADDR(inst, GetPhysicalDeviceSurfaceFormatsKHR);
  822. GET_INSTANCE_PROC_ADDR(inst, GetPhysicalDeviceSurfacePresentModesKHR);
  823. GET_INSTANCE_PROC_ADDR(inst, GetSwapchainImagesKHR);
  824. // get info about what our vulkan driver is capable off
  825. {
  826. Error res = _check_capabilities();
  827. if (res != OK) {
  828. return res;
  829. }
  830. }
  831. return OK;
  832. }
  833. Error VulkanContext::_create_device() {
  834. VkResult err;
  835. float queue_priorities[1] = { 0.0 };
  836. VkDeviceQueueCreateInfo queues[2];
  837. queues[0].sType = VK_STRUCTURE_TYPE_DEVICE_QUEUE_CREATE_INFO;
  838. queues[0].pNext = nullptr;
  839. queues[0].queueFamilyIndex = graphics_queue_family_index;
  840. queues[0].queueCount = 1;
  841. queues[0].pQueuePriorities = queue_priorities;
  842. queues[0].flags = 0;
  843. VkDeviceCreateInfo sdevice = {
  844. /*sType*/ VK_STRUCTURE_TYPE_DEVICE_CREATE_INFO,
  845. /*pNext*/ nullptr,
  846. /*flags*/ 0,
  847. /*queueCreateInfoCount*/ 1,
  848. /*pQueueCreateInfos*/ queues,
  849. /*enabledLayerCount*/ 0,
  850. /*ppEnabledLayerNames*/ nullptr,
  851. /*enabledExtensionCount*/ enabled_extension_count,
  852. /*ppEnabledExtensionNames*/ (const char *const *)extension_names,
  853. /*pEnabledFeatures*/ &physical_device_features, // If specific features are required, pass them in here
  854. };
  855. if (separate_present_queue) {
  856. queues[1].sType = VK_STRUCTURE_TYPE_DEVICE_QUEUE_CREATE_INFO;
  857. queues[1].pNext = nullptr;
  858. queues[1].queueFamilyIndex = present_queue_family_index;
  859. queues[1].queueCount = 1;
  860. queues[1].pQueuePriorities = queue_priorities;
  861. queues[1].flags = 0;
  862. sdevice.queueCreateInfoCount = 2;
  863. }
  864. #ifdef VK_VERSION_1_2
  865. VkPhysicalDeviceVulkan11Features vulkan11features;
  866. vulkan11features.sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_VULKAN_1_1_FEATURES;
  867. vulkan11features.pNext = nullptr;
  868. // !BAS! Need to figure out which ones of these we want enabled...
  869. vulkan11features.storageBuffer16BitAccess = 0;
  870. vulkan11features.uniformAndStorageBuffer16BitAccess = 0;
  871. vulkan11features.storagePushConstant16 = 0;
  872. vulkan11features.storageInputOutput16 = 0;
  873. vulkan11features.multiview = multiview_capabilities.is_supported;
  874. vulkan11features.multiviewGeometryShader = multiview_capabilities.geometry_shader_is_supported;
  875. vulkan11features.multiviewTessellationShader = multiview_capabilities.tessellation_shader_is_supported;
  876. vulkan11features.variablePointersStorageBuffer = 0;
  877. vulkan11features.variablePointers = 0;
  878. vulkan11features.protectedMemory = 0;
  879. vulkan11features.samplerYcbcrConversion = 0;
  880. vulkan11features.shaderDrawParameters = 0;
  881. sdevice.pNext = &vulkan11features;
  882. #elif VK_VERSION_1_1
  883. VkPhysicalDeviceMultiviewFeatures multiview_features;
  884. multiview_features.sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_MULTIVIEW_FEATURES;
  885. multiview_features.pNext = nullptr;
  886. multiview_features.multiview = multiview_capabilities.is_supported;
  887. multiview_features.multiviewGeometryShader = multiview_capabilities.geometry_shader_is_supported;
  888. multiview_features.multiviewTessellationShader = multiview_capabilities.tessellation_shader_is_supported;
  889. sdevice.pNext = &multiview_features;
  890. #endif
  891. err = vkCreateDevice(gpu, &sdevice, nullptr, &device);
  892. ERR_FAIL_COND_V(err, ERR_CANT_CREATE);
  893. return OK;
  894. }
  895. Error VulkanContext::_initialize_queues(VkSurfaceKHR p_surface) {
  896. // Iterate over each queue to learn whether it supports presenting:
  897. VkBool32 *supportsPresent = (VkBool32 *)malloc(queue_family_count * sizeof(VkBool32));
  898. for (uint32_t i = 0; i < queue_family_count; i++) {
  899. fpGetPhysicalDeviceSurfaceSupportKHR(gpu, i, p_surface, &supportsPresent[i]);
  900. }
  901. // Search for a graphics and a present queue in the array of queue
  902. // families, try to find one that supports both
  903. uint32_t graphicsQueueFamilyIndex = UINT32_MAX;
  904. uint32_t presentQueueFamilyIndex = UINT32_MAX;
  905. for (uint32_t i = 0; i < queue_family_count; i++) {
  906. if ((queue_props[i].queueFlags & VK_QUEUE_GRAPHICS_BIT) != 0) {
  907. if (graphicsQueueFamilyIndex == UINT32_MAX) {
  908. graphicsQueueFamilyIndex = i;
  909. }
  910. if (supportsPresent[i] == VK_TRUE) {
  911. graphicsQueueFamilyIndex = i;
  912. presentQueueFamilyIndex = i;
  913. break;
  914. }
  915. }
  916. }
  917. if (presentQueueFamilyIndex == UINT32_MAX) {
  918. // If didn't find a queue that supports both graphics and present, then
  919. // find a separate present queue.
  920. for (uint32_t i = 0; i < queue_family_count; ++i) {
  921. if (supportsPresent[i] == VK_TRUE) {
  922. presentQueueFamilyIndex = i;
  923. break;
  924. }
  925. }
  926. }
  927. free(supportsPresent);
  928. // Generate error if could not find both a graphics and a present queue
  929. ERR_FAIL_COND_V_MSG(graphicsQueueFamilyIndex == UINT32_MAX || presentQueueFamilyIndex == UINT32_MAX, ERR_CANT_CREATE,
  930. "Could not find both graphics and present queues\n");
  931. graphics_queue_family_index = graphicsQueueFamilyIndex;
  932. present_queue_family_index = presentQueueFamilyIndex;
  933. separate_present_queue = (graphics_queue_family_index != present_queue_family_index);
  934. _create_device();
  935. static PFN_vkGetDeviceProcAddr g_gdpa = nullptr;
  936. #define GET_DEVICE_PROC_ADDR(dev, entrypoint) \
  937. { \
  938. if (!g_gdpa) \
  939. g_gdpa = (PFN_vkGetDeviceProcAddr)vkGetInstanceProcAddr(inst, "vkGetDeviceProcAddr"); \
  940. fp##entrypoint = (PFN_vk##entrypoint)g_gdpa(dev, "vk" #entrypoint); \
  941. ERR_FAIL_COND_V_MSG(fp##entrypoint == nullptr, ERR_CANT_CREATE, \
  942. "vkGetDeviceProcAddr failed to find vk" #entrypoint); \
  943. }
  944. GET_DEVICE_PROC_ADDR(device, CreateSwapchainKHR);
  945. GET_DEVICE_PROC_ADDR(device, DestroySwapchainKHR);
  946. GET_DEVICE_PROC_ADDR(device, GetSwapchainImagesKHR);
  947. GET_DEVICE_PROC_ADDR(device, AcquireNextImageKHR);
  948. GET_DEVICE_PROC_ADDR(device, QueuePresentKHR);
  949. if (VK_GOOGLE_display_timing_enabled) {
  950. GET_DEVICE_PROC_ADDR(device, GetRefreshCycleDurationGOOGLE);
  951. GET_DEVICE_PROC_ADDR(device, GetPastPresentationTimingGOOGLE);
  952. }
  953. vkGetDeviceQueue(device, graphics_queue_family_index, 0, &graphics_queue);
  954. if (!separate_present_queue) {
  955. present_queue = graphics_queue;
  956. } else {
  957. vkGetDeviceQueue(device, present_queue_family_index, 0, &present_queue);
  958. }
  959. // Get the list of VkFormat's that are supported:
  960. uint32_t formatCount;
  961. VkResult err = fpGetPhysicalDeviceSurfaceFormatsKHR(gpu, p_surface, &formatCount, nullptr);
  962. ERR_FAIL_COND_V(err, ERR_CANT_CREATE);
  963. VkSurfaceFormatKHR *surfFormats = (VkSurfaceFormatKHR *)malloc(formatCount * sizeof(VkSurfaceFormatKHR));
  964. err = fpGetPhysicalDeviceSurfaceFormatsKHR(gpu, p_surface, &formatCount, surfFormats);
  965. if (err) {
  966. free(surfFormats);
  967. ERR_FAIL_V(ERR_CANT_CREATE);
  968. }
  969. // If the format list includes just one entry of VK_FORMAT_UNDEFINED,
  970. // the surface has no preferred format. Otherwise, at least one
  971. // supported format will be returned.
  972. if (formatCount == 1 && surfFormats[0].format == VK_FORMAT_UNDEFINED) {
  973. format = VK_FORMAT_B8G8R8A8_UNORM;
  974. color_space = surfFormats[0].colorSpace;
  975. } else {
  976. // These should be ordered with the ones we want to use on top and fallback modes further down
  977. // we want an 32bit RGBA unsigned normalised buffer or similar
  978. const VkFormat allowed_formats[] = {
  979. VK_FORMAT_B8G8R8A8_UNORM,
  980. VK_FORMAT_R8G8B8A8_UNORM
  981. };
  982. uint32_t allowed_formats_count = sizeof(allowed_formats) / sizeof(VkFormat);
  983. if (formatCount < 1) {
  984. free(surfFormats);
  985. ERR_FAIL_V_MSG(ERR_CANT_CREATE, "formatCount less than 1");
  986. }
  987. // Find the first format that we support
  988. format = VK_FORMAT_UNDEFINED;
  989. for (uint32_t af = 0; af < allowed_formats_count && format == VK_FORMAT_UNDEFINED; af++) {
  990. for (uint32_t sf = 0; sf < formatCount && format == VK_FORMAT_UNDEFINED; sf++) {
  991. if (surfFormats[sf].format == allowed_formats[af]) {
  992. format = surfFormats[sf].format;
  993. color_space = surfFormats[sf].colorSpace;
  994. }
  995. }
  996. }
  997. if (format == VK_FORMAT_UNDEFINED) {
  998. free(surfFormats);
  999. ERR_FAIL_V_MSG(ERR_CANT_CREATE, "No usable surface format found.");
  1000. }
  1001. }
  1002. free(surfFormats);
  1003. Error serr = _create_semaphores();
  1004. if (serr) {
  1005. return serr;
  1006. }
  1007. queues_initialized = true;
  1008. return OK;
  1009. }
  1010. Error VulkanContext::_create_semaphores() {
  1011. VkResult err;
  1012. // Create semaphores to synchronize acquiring presentable buffers before
  1013. // rendering and waiting for drawing to be complete before presenting
  1014. VkSemaphoreCreateInfo semaphoreCreateInfo = {
  1015. /*sType*/ VK_STRUCTURE_TYPE_SEMAPHORE_CREATE_INFO,
  1016. /*pNext*/ nullptr,
  1017. /*flags*/ 0,
  1018. };
  1019. // Create fences that we can use to throttle if we get too far
  1020. // ahead of the image presents
  1021. VkFenceCreateInfo fence_ci = {
  1022. /*sType*/ VK_STRUCTURE_TYPE_FENCE_CREATE_INFO,
  1023. /*pNext*/ nullptr,
  1024. /*flags*/ VK_FENCE_CREATE_SIGNALED_BIT
  1025. };
  1026. for (uint32_t i = 0; i < FRAME_LAG; i++) {
  1027. err = vkCreateFence(device, &fence_ci, nullptr, &fences[i]);
  1028. ERR_FAIL_COND_V(err, ERR_CANT_CREATE);
  1029. err = vkCreateSemaphore(device, &semaphoreCreateInfo, nullptr, &draw_complete_semaphores[i]);
  1030. ERR_FAIL_COND_V(err, ERR_CANT_CREATE);
  1031. if (separate_present_queue) {
  1032. err = vkCreateSemaphore(device, &semaphoreCreateInfo, nullptr, &image_ownership_semaphores[i]);
  1033. ERR_FAIL_COND_V(err, ERR_CANT_CREATE);
  1034. }
  1035. }
  1036. frame_index = 0;
  1037. // Get Memory information and properties
  1038. vkGetPhysicalDeviceMemoryProperties(gpu, &memory_properties);
  1039. return OK;
  1040. }
  1041. bool VulkanContext::_use_validation_layers() {
  1042. return Engine::get_singleton()->is_validation_layers_enabled();
  1043. }
  1044. Error VulkanContext::_window_create(DisplayServer::WindowID p_window_id, VkSurfaceKHR p_surface, int p_width, int p_height) {
  1045. ERR_FAIL_COND_V(windows.has(p_window_id), ERR_INVALID_PARAMETER);
  1046. if (!queues_initialized) {
  1047. // We use a single GPU, but we need a surface to initialize the
  1048. // queues, so this process must be deferred until a surface
  1049. // is created.
  1050. Error err = _initialize_queues(p_surface);
  1051. ERR_FAIL_COND_V(err != OK, ERR_CANT_CREATE);
  1052. } else {
  1053. // make sure any of the surfaces supports present (validation layer complains if this is not done).
  1054. bool any_supports_present = false;
  1055. for (uint32_t i = 0; i < queue_family_count; i++) {
  1056. VkBool32 supports;
  1057. fpGetPhysicalDeviceSurfaceSupportKHR(gpu, i, p_surface, &supports);
  1058. if (supports) {
  1059. any_supports_present = true;
  1060. break;
  1061. }
  1062. }
  1063. ERR_FAIL_COND_V_MSG(!any_supports_present, ERR_CANT_CREATE, "Surface passed for sub-window creation does not support presenting");
  1064. }
  1065. Window window;
  1066. window.surface = p_surface;
  1067. window.width = p_width;
  1068. window.height = p_height;
  1069. Error err = _update_swap_chain(&window);
  1070. ERR_FAIL_COND_V(err != OK, ERR_CANT_CREATE);
  1071. VkSemaphoreCreateInfo semaphoreCreateInfo = {
  1072. /*sType*/ VK_STRUCTURE_TYPE_SEMAPHORE_CREATE_INFO,
  1073. /*pNext*/ nullptr,
  1074. /*flags*/ 0,
  1075. };
  1076. for (uint32_t i = 0; i < FRAME_LAG; i++) {
  1077. VkResult vkerr = vkCreateSemaphore(device, &semaphoreCreateInfo, nullptr, &window.image_acquired_semaphores[i]);
  1078. ERR_FAIL_COND_V(vkerr, ERR_CANT_CREATE);
  1079. }
  1080. windows[p_window_id] = window;
  1081. return OK;
  1082. }
  1083. void VulkanContext::window_resize(DisplayServer::WindowID p_window, int p_width, int p_height) {
  1084. ERR_FAIL_COND(!windows.has(p_window));
  1085. windows[p_window].width = p_width;
  1086. windows[p_window].height = p_height;
  1087. _update_swap_chain(&windows[p_window]);
  1088. }
  1089. int VulkanContext::window_get_width(DisplayServer::WindowID p_window) {
  1090. ERR_FAIL_COND_V(!windows.has(p_window), -1);
  1091. return windows[p_window].width;
  1092. }
  1093. int VulkanContext::window_get_height(DisplayServer::WindowID p_window) {
  1094. ERR_FAIL_COND_V(!windows.has(p_window), -1);
  1095. return windows[p_window].height;
  1096. }
  1097. VkRenderPass VulkanContext::window_get_render_pass(DisplayServer::WindowID p_window) {
  1098. ERR_FAIL_COND_V(!windows.has(p_window), VK_NULL_HANDLE);
  1099. Window *w = &windows[p_window];
  1100. //vulkan use of currentbuffer
  1101. return w->render_pass;
  1102. }
  1103. VkFramebuffer VulkanContext::window_get_framebuffer(DisplayServer::WindowID p_window) {
  1104. ERR_FAIL_COND_V(!windows.has(p_window), VK_NULL_HANDLE);
  1105. ERR_FAIL_COND_V(!buffers_prepared, VK_NULL_HANDLE);
  1106. Window *w = &windows[p_window];
  1107. //vulkan use of currentbuffer
  1108. return w->swapchain_image_resources[w->current_buffer].framebuffer;
  1109. }
  1110. void VulkanContext::window_destroy(DisplayServer::WindowID p_window_id) {
  1111. ERR_FAIL_COND(!windows.has(p_window_id));
  1112. _clean_up_swap_chain(&windows[p_window_id]);
  1113. for (uint32_t i = 0; i < FRAME_LAG; i++) {
  1114. vkDestroySemaphore(device, windows[p_window_id].image_acquired_semaphores[i], nullptr);
  1115. }
  1116. vkDestroySurfaceKHR(inst, windows[p_window_id].surface, nullptr);
  1117. windows.erase(p_window_id);
  1118. }
  1119. Error VulkanContext::_clean_up_swap_chain(Window *window) {
  1120. if (!window->swapchain) {
  1121. return OK;
  1122. }
  1123. vkDeviceWaitIdle(device);
  1124. //this destroys images associated it seems
  1125. fpDestroySwapchainKHR(device, window->swapchain, nullptr);
  1126. window->swapchain = VK_NULL_HANDLE;
  1127. vkDestroyRenderPass(device, window->render_pass, nullptr);
  1128. if (window->swapchain_image_resources) {
  1129. for (uint32_t i = 0; i < swapchainImageCount; i++) {
  1130. vkDestroyImageView(device, window->swapchain_image_resources[i].view, nullptr);
  1131. vkDestroyFramebuffer(device, window->swapchain_image_resources[i].framebuffer, nullptr);
  1132. }
  1133. free(window->swapchain_image_resources);
  1134. window->swapchain_image_resources = nullptr;
  1135. }
  1136. if (separate_present_queue) {
  1137. vkDestroyCommandPool(device, window->present_cmd_pool, nullptr);
  1138. }
  1139. return OK;
  1140. }
  1141. Error VulkanContext::_update_swap_chain(Window *window) {
  1142. VkResult err;
  1143. if (window->swapchain) {
  1144. _clean_up_swap_chain(window);
  1145. }
  1146. // Check the surface capabilities and formats
  1147. VkSurfaceCapabilitiesKHR surfCapabilities;
  1148. err = fpGetPhysicalDeviceSurfaceCapabilitiesKHR(gpu, window->surface, &surfCapabilities);
  1149. ERR_FAIL_COND_V(err, ERR_CANT_CREATE);
  1150. uint32_t presentModeCount;
  1151. err = fpGetPhysicalDeviceSurfacePresentModesKHR(gpu, window->surface, &presentModeCount, nullptr);
  1152. ERR_FAIL_COND_V(err, ERR_CANT_CREATE);
  1153. VkPresentModeKHR *presentModes = (VkPresentModeKHR *)malloc(presentModeCount * sizeof(VkPresentModeKHR));
  1154. ERR_FAIL_COND_V(!presentModes, ERR_CANT_CREATE);
  1155. err = fpGetPhysicalDeviceSurfacePresentModesKHR(gpu, window->surface, &presentModeCount, presentModes);
  1156. if (err) {
  1157. free(presentModes);
  1158. ERR_FAIL_V(ERR_CANT_CREATE);
  1159. }
  1160. VkExtent2D swapchainExtent;
  1161. // width and height are either both 0xFFFFFFFF, or both not 0xFFFFFFFF.
  1162. if (surfCapabilities.currentExtent.width == 0xFFFFFFFF) {
  1163. // If the surface size is undefined, the size is set to the size
  1164. // of the images requested, which must fit within the minimum and
  1165. // maximum values.
  1166. swapchainExtent.width = window->width;
  1167. swapchainExtent.height = window->height;
  1168. if (swapchainExtent.width < surfCapabilities.minImageExtent.width) {
  1169. swapchainExtent.width = surfCapabilities.minImageExtent.width;
  1170. } else if (swapchainExtent.width > surfCapabilities.maxImageExtent.width) {
  1171. swapchainExtent.width = surfCapabilities.maxImageExtent.width;
  1172. }
  1173. if (swapchainExtent.height < surfCapabilities.minImageExtent.height) {
  1174. swapchainExtent.height = surfCapabilities.minImageExtent.height;
  1175. } else if (swapchainExtent.height > surfCapabilities.maxImageExtent.height) {
  1176. swapchainExtent.height = surfCapabilities.maxImageExtent.height;
  1177. }
  1178. } else {
  1179. // If the surface size is defined, the swap chain size must match
  1180. swapchainExtent = surfCapabilities.currentExtent;
  1181. window->width = surfCapabilities.currentExtent.width;
  1182. window->height = surfCapabilities.currentExtent.height;
  1183. }
  1184. if (window->width == 0 || window->height == 0) {
  1185. free(presentModes);
  1186. //likely window minimized, no swapchain created
  1187. return OK;
  1188. }
  1189. // The FIFO present mode is guaranteed by the spec to be supported
  1190. // and to have no tearing. It's a great default present mode to use.
  1191. VkPresentModeKHR swapchainPresentMode = VK_PRESENT_MODE_FIFO_KHR;
  1192. // There are times when you may wish to use another present mode. The
  1193. // following code shows how to select them, and the comments provide some
  1194. // reasons you may wish to use them.
  1195. //
  1196. // It should be noted that Vulkan 1.0 doesn't provide a method for
  1197. // synchronizing rendering with the presentation engine's display. There
  1198. // is a method provided for throttling rendering with the display, but
  1199. // there are some presentation engines for which this method will not work.
  1200. // If an application doesn't throttle its rendering, and if it renders much
  1201. // faster than the refresh rate of the display, this can waste power on
  1202. // mobile devices. That is because power is being spent rendering images
  1203. // that may never be seen.
  1204. // VK_PRESENT_MODE_IMMEDIATE_KHR is for applications that don't care about
  1205. // tearing, or have some way of synchronizing their rendering with the
  1206. // display.
  1207. // VK_PRESENT_MODE_MAILBOX_KHR may be useful for applications that
  1208. // generally render a new presentable image every refresh cycle, but are
  1209. // occasionally early. In this case, the application wants the new image
  1210. // to be displayed instead of the previously-queued-for-presentation image
  1211. // that has not yet been displayed.
  1212. // VK_PRESENT_MODE_FIFO_RELAXED_KHR is for applications that generally
  1213. // render a new presentable image every refresh cycle, but are occasionally
  1214. // late. In this case (perhaps because of stuttering/latency concerns),
  1215. // the application wants the late image to be immediately displayed, even
  1216. // though that may mean some tearing.
  1217. if (window->presentMode != swapchainPresentMode) {
  1218. for (size_t i = 0; i < presentModeCount; ++i) {
  1219. if (presentModes[i] == window->presentMode) {
  1220. swapchainPresentMode = window->presentMode;
  1221. break;
  1222. }
  1223. }
  1224. }
  1225. free(presentModes);
  1226. ERR_FAIL_COND_V_MSG(swapchainPresentMode != window->presentMode, ERR_CANT_CREATE, "Present mode specified is not supported\n");
  1227. // Determine the number of VkImages to use in the swap chain.
  1228. // Application desires to acquire 3 images at a time for triple
  1229. // buffering
  1230. uint32_t desiredNumOfSwapchainImages = 3;
  1231. if (desiredNumOfSwapchainImages < surfCapabilities.minImageCount) {
  1232. desiredNumOfSwapchainImages = surfCapabilities.minImageCount;
  1233. }
  1234. // If maxImageCount is 0, we can ask for as many images as we want;
  1235. // otherwise we're limited to maxImageCount
  1236. if ((surfCapabilities.maxImageCount > 0) && (desiredNumOfSwapchainImages > surfCapabilities.maxImageCount)) {
  1237. // Application must settle for fewer images than desired:
  1238. desiredNumOfSwapchainImages = surfCapabilities.maxImageCount;
  1239. }
  1240. VkSurfaceTransformFlagsKHR preTransform;
  1241. if (surfCapabilities.supportedTransforms & VK_SURFACE_TRANSFORM_IDENTITY_BIT_KHR) {
  1242. preTransform = VK_SURFACE_TRANSFORM_IDENTITY_BIT_KHR;
  1243. } else {
  1244. preTransform = surfCapabilities.currentTransform;
  1245. }
  1246. // Find a supported composite alpha mode - one of these is guaranteed to be set
  1247. VkCompositeAlphaFlagBitsKHR compositeAlpha = VK_COMPOSITE_ALPHA_OPAQUE_BIT_KHR;
  1248. VkCompositeAlphaFlagBitsKHR compositeAlphaFlags[4] = {
  1249. VK_COMPOSITE_ALPHA_OPAQUE_BIT_KHR,
  1250. VK_COMPOSITE_ALPHA_PRE_MULTIPLIED_BIT_KHR,
  1251. VK_COMPOSITE_ALPHA_POST_MULTIPLIED_BIT_KHR,
  1252. VK_COMPOSITE_ALPHA_INHERIT_BIT_KHR,
  1253. };
  1254. for (uint32_t i = 0; i < ARRAY_SIZE(compositeAlphaFlags); i++) {
  1255. if (surfCapabilities.supportedCompositeAlpha & compositeAlphaFlags[i]) {
  1256. compositeAlpha = compositeAlphaFlags[i];
  1257. break;
  1258. }
  1259. }
  1260. VkSwapchainCreateInfoKHR swapchain_ci = {
  1261. /*sType*/ VK_STRUCTURE_TYPE_SWAPCHAIN_CREATE_INFO_KHR,
  1262. /*pNext*/ nullptr,
  1263. /*flags*/ 0,
  1264. /*surface*/ window->surface,
  1265. /*minImageCount*/ desiredNumOfSwapchainImages,
  1266. /*imageFormat*/ format,
  1267. /*imageColorSpace*/ color_space,
  1268. /*imageExtent*/ {
  1269. /*width*/ swapchainExtent.width,
  1270. /*height*/ swapchainExtent.height,
  1271. },
  1272. /*imageArrayLayers*/ 1,
  1273. /*imageUsage*/ VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT,
  1274. /*imageSharingMode*/ VK_SHARING_MODE_EXCLUSIVE,
  1275. /*queueFamilyIndexCount*/ 0,
  1276. /*pQueueFamilyIndices*/ nullptr,
  1277. /*preTransform*/ (VkSurfaceTransformFlagBitsKHR)preTransform,
  1278. /*compositeAlpha*/ compositeAlpha,
  1279. /*presentMode*/ swapchainPresentMode,
  1280. /*clipped*/ true,
  1281. /*oldSwapchain*/ VK_NULL_HANDLE,
  1282. };
  1283. err = fpCreateSwapchainKHR(device, &swapchain_ci, nullptr, &window->swapchain);
  1284. ERR_FAIL_COND_V(err, ERR_CANT_CREATE);
  1285. uint32_t sp_image_count;
  1286. err = fpGetSwapchainImagesKHR(device, window->swapchain, &sp_image_count, nullptr);
  1287. ERR_FAIL_COND_V(err, ERR_CANT_CREATE);
  1288. if (swapchainImageCount == 0) {
  1289. //assign here for the first time.
  1290. swapchainImageCount = sp_image_count;
  1291. } else {
  1292. ERR_FAIL_COND_V(swapchainImageCount != sp_image_count, ERR_BUG);
  1293. }
  1294. VkImage *swapchainImages = (VkImage *)malloc(swapchainImageCount * sizeof(VkImage));
  1295. ERR_FAIL_COND_V(!swapchainImages, ERR_CANT_CREATE);
  1296. err = fpGetSwapchainImagesKHR(device, window->swapchain, &swapchainImageCount, swapchainImages);
  1297. if (err) {
  1298. free(swapchainImages);
  1299. ERR_FAIL_V(ERR_CANT_CREATE);
  1300. }
  1301. window->swapchain_image_resources =
  1302. (SwapchainImageResources *)malloc(sizeof(SwapchainImageResources) * swapchainImageCount);
  1303. if (!window->swapchain_image_resources) {
  1304. free(swapchainImages);
  1305. ERR_FAIL_V(ERR_CANT_CREATE);
  1306. }
  1307. for (uint32_t i = 0; i < swapchainImageCount; i++) {
  1308. VkImageViewCreateInfo color_image_view = {
  1309. /*sType*/ VK_STRUCTURE_TYPE_IMAGE_VIEW_CREATE_INFO,
  1310. /*pNext*/ nullptr,
  1311. /*flags*/ 0,
  1312. /*image*/ swapchainImages[i],
  1313. /*viewType*/ VK_IMAGE_VIEW_TYPE_2D,
  1314. /*format*/ format,
  1315. /*components*/ {
  1316. /*r*/ VK_COMPONENT_SWIZZLE_R,
  1317. /*g*/ VK_COMPONENT_SWIZZLE_G,
  1318. /*b*/ VK_COMPONENT_SWIZZLE_B,
  1319. /*a*/ VK_COMPONENT_SWIZZLE_A,
  1320. },
  1321. /*subresourceRange*/ { /*aspectMask*/ VK_IMAGE_ASPECT_COLOR_BIT,
  1322. /*baseMipLevel*/ 0,
  1323. /*levelCount*/ 1,
  1324. /*baseArrayLayer*/ 0,
  1325. /*layerCount*/ 1 },
  1326. };
  1327. window->swapchain_image_resources[i].image = swapchainImages[i];
  1328. color_image_view.image = window->swapchain_image_resources[i].image;
  1329. err = vkCreateImageView(device, &color_image_view, nullptr, &window->swapchain_image_resources[i].view);
  1330. if (err) {
  1331. free(swapchainImages);
  1332. ERR_FAIL_V(ERR_CANT_CREATE);
  1333. }
  1334. }
  1335. free(swapchainImages);
  1336. /******** FRAMEBUFFER ************/
  1337. {
  1338. const VkAttachmentDescription attachment = {
  1339. /*flags*/ 0,
  1340. /*format*/ format,
  1341. /*samples*/ VK_SAMPLE_COUNT_1_BIT,
  1342. /*loadOp*/ VK_ATTACHMENT_LOAD_OP_CLEAR,
  1343. /*storeOp*/ VK_ATTACHMENT_STORE_OP_STORE,
  1344. /*stencilLoadOp*/ VK_ATTACHMENT_LOAD_OP_DONT_CARE,
  1345. /*stencilStoreOp*/ VK_ATTACHMENT_STORE_OP_DONT_CARE,
  1346. /*initialLayout*/ VK_IMAGE_LAYOUT_UNDEFINED,
  1347. /*finalLayout*/ VK_IMAGE_LAYOUT_PRESENT_SRC_KHR,
  1348. };
  1349. const VkAttachmentReference color_reference = {
  1350. /*attachment*/ 0,
  1351. /*layout*/ VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL,
  1352. };
  1353. const VkSubpassDescription subpass = {
  1354. /*flags*/ 0,
  1355. /*pipelineBindPoint*/ VK_PIPELINE_BIND_POINT_GRAPHICS,
  1356. /*inputAttachmentCount*/ 0,
  1357. /*pInputAttachments*/ nullptr,
  1358. /*colorAttachmentCount*/ 1,
  1359. /*pColorAttachments*/ &color_reference,
  1360. /*pResolveAttachments*/ nullptr,
  1361. /*pDepthStencilAttachment*/ nullptr,
  1362. /*preserveAttachmentCount*/ 0,
  1363. /*pPreserveAttachments*/ nullptr,
  1364. };
  1365. const VkRenderPassCreateInfo rp_info = {
  1366. /*sTyp*/ VK_STRUCTURE_TYPE_RENDER_PASS_CREATE_INFO,
  1367. /*pNext*/ nullptr,
  1368. /*flags*/ 0,
  1369. /*attachmentCount*/ 1,
  1370. /*pAttachments*/ &attachment,
  1371. /*subpassCount*/ 1,
  1372. /*pSubpasses*/ &subpass,
  1373. /*dependencyCount*/ 0,
  1374. /*pDependencies*/ nullptr,
  1375. };
  1376. err = vkCreateRenderPass(device, &rp_info, nullptr, &window->render_pass);
  1377. ERR_FAIL_COND_V(err, ERR_CANT_CREATE);
  1378. for (uint32_t i = 0; i < swapchainImageCount; i++) {
  1379. const VkFramebufferCreateInfo fb_info = {
  1380. /*sType*/ VK_STRUCTURE_TYPE_FRAMEBUFFER_CREATE_INFO,
  1381. /*pNext*/ nullptr,
  1382. /*flags*/ 0,
  1383. /*renderPass*/ window->render_pass,
  1384. /*attachmentCount*/ 1,
  1385. /*pAttachments*/ &window->swapchain_image_resources[i].view,
  1386. /*width*/ (uint32_t)window->width,
  1387. /*height*/ (uint32_t)window->height,
  1388. /*layers*/ 1,
  1389. };
  1390. err = vkCreateFramebuffer(device, &fb_info, nullptr, &window->swapchain_image_resources[i].framebuffer);
  1391. ERR_FAIL_COND_V(err, ERR_CANT_CREATE);
  1392. }
  1393. }
  1394. /******** SEPARATE PRESENT QUEUE ************/
  1395. if (separate_present_queue) {
  1396. const VkCommandPoolCreateInfo present_cmd_pool_info = {
  1397. /*sType*/ VK_STRUCTURE_TYPE_COMMAND_POOL_CREATE_INFO,
  1398. /*pNext*/ nullptr,
  1399. /*flags*/ 0,
  1400. /*queueFamilyIndex*/ present_queue_family_index,
  1401. };
  1402. err = vkCreateCommandPool(device, &present_cmd_pool_info, nullptr, &window->present_cmd_pool);
  1403. ERR_FAIL_COND_V(err, ERR_CANT_CREATE);
  1404. const VkCommandBufferAllocateInfo present_cmd_info = {
  1405. /*sType*/ VK_STRUCTURE_TYPE_COMMAND_BUFFER_ALLOCATE_INFO,
  1406. /*pNext*/ nullptr,
  1407. /*commandPool*/ window->present_cmd_pool,
  1408. /*level*/ VK_COMMAND_BUFFER_LEVEL_PRIMARY,
  1409. /*commandBufferCount*/ 1,
  1410. };
  1411. for (uint32_t i = 0; i < swapchainImageCount; i++) {
  1412. err = vkAllocateCommandBuffers(device, &present_cmd_info,
  1413. &window->swapchain_image_resources[i].graphics_to_present_cmd);
  1414. ERR_FAIL_COND_V(err, ERR_CANT_CREATE);
  1415. const VkCommandBufferBeginInfo cmd_buf_info = {
  1416. /*sType*/ VK_STRUCTURE_TYPE_COMMAND_BUFFER_BEGIN_INFO,
  1417. /*pNext*/ nullptr,
  1418. /*flags*/ VK_COMMAND_BUFFER_USAGE_SIMULTANEOUS_USE_BIT,
  1419. /*pInheritanceInfo*/ nullptr,
  1420. };
  1421. err = vkBeginCommandBuffer(window->swapchain_image_resources[i].graphics_to_present_cmd, &cmd_buf_info);
  1422. ERR_FAIL_COND_V(err, ERR_CANT_CREATE);
  1423. VkImageMemoryBarrier image_ownership_barrier = {
  1424. /*sType*/ VK_STRUCTURE_TYPE_IMAGE_MEMORY_BARRIER,
  1425. /*pNext*/ nullptr,
  1426. /*srcAccessMask*/ 0,
  1427. /*dstAccessMask*/ VK_ACCESS_COLOR_ATTACHMENT_WRITE_BIT,
  1428. /*oldLayout*/ VK_IMAGE_LAYOUT_PRESENT_SRC_KHR,
  1429. /*newLayout*/ VK_IMAGE_LAYOUT_PRESENT_SRC_KHR,
  1430. /*srcQueueFamilyIndex*/ graphics_queue_family_index,
  1431. /*dstQueueFamilyIndex*/ present_queue_family_index,
  1432. /*image*/ window->swapchain_image_resources[i].image,
  1433. /*subresourceRange*/ { VK_IMAGE_ASPECT_COLOR_BIT, 0, 1, 0, 1 }
  1434. };
  1435. vkCmdPipelineBarrier(window->swapchain_image_resources[i].graphics_to_present_cmd, VK_PIPELINE_STAGE_COLOR_ATTACHMENT_OUTPUT_BIT,
  1436. VK_PIPELINE_STAGE_COLOR_ATTACHMENT_OUTPUT_BIT, 0, 0, nullptr, 0, nullptr, 1, &image_ownership_barrier);
  1437. err = vkEndCommandBuffer(window->swapchain_image_resources[i].graphics_to_present_cmd);
  1438. ERR_FAIL_COND_V(err, ERR_CANT_CREATE);
  1439. }
  1440. }
  1441. //reset current buffer
  1442. window->current_buffer = 0;
  1443. return OK;
  1444. }
  1445. Error VulkanContext::initialize() {
  1446. Error err = _create_physical_device();
  1447. if (err) {
  1448. return err;
  1449. }
  1450. device_initialized = true;
  1451. return OK;
  1452. }
  1453. void VulkanContext::set_setup_buffer(const VkCommandBuffer &pCommandBuffer) {
  1454. command_buffer_queue.write[0] = pCommandBuffer;
  1455. }
  1456. void VulkanContext::append_command_buffer(const VkCommandBuffer &pCommandBuffer) {
  1457. if (command_buffer_queue.size() <= command_buffer_count) {
  1458. command_buffer_queue.resize(command_buffer_count + 1);
  1459. }
  1460. command_buffer_queue.write[command_buffer_count] = pCommandBuffer;
  1461. command_buffer_count++;
  1462. }
  1463. void VulkanContext::flush(bool p_flush_setup, bool p_flush_pending) {
  1464. // ensure everything else pending is executed
  1465. vkDeviceWaitIdle(device);
  1466. //flush the pending setup buffer
  1467. if (p_flush_setup && command_buffer_queue[0]) {
  1468. //use a fence to wait for everything done
  1469. VkSubmitInfo submit_info;
  1470. submit_info.sType = VK_STRUCTURE_TYPE_SUBMIT_INFO;
  1471. submit_info.pNext = nullptr;
  1472. submit_info.pWaitDstStageMask = nullptr;
  1473. submit_info.waitSemaphoreCount = 0;
  1474. submit_info.pWaitSemaphores = nullptr;
  1475. submit_info.commandBufferCount = 1;
  1476. submit_info.pCommandBuffers = command_buffer_queue.ptr();
  1477. submit_info.signalSemaphoreCount = 0;
  1478. submit_info.pSignalSemaphores = nullptr;
  1479. VkResult err = vkQueueSubmit(graphics_queue, 1, &submit_info, VK_NULL_HANDLE);
  1480. command_buffer_queue.write[0] = nullptr;
  1481. ERR_FAIL_COND(err);
  1482. vkDeviceWaitIdle(device);
  1483. }
  1484. if (p_flush_pending && command_buffer_count > 1) {
  1485. //use a fence to wait for everything done
  1486. VkSubmitInfo submit_info;
  1487. submit_info.sType = VK_STRUCTURE_TYPE_SUBMIT_INFO;
  1488. submit_info.pNext = nullptr;
  1489. submit_info.pWaitDstStageMask = nullptr;
  1490. submit_info.waitSemaphoreCount = 0;
  1491. submit_info.pWaitSemaphores = nullptr;
  1492. submit_info.commandBufferCount = command_buffer_count - 1;
  1493. submit_info.pCommandBuffers = command_buffer_queue.ptr() + 1;
  1494. submit_info.signalSemaphoreCount = 0;
  1495. submit_info.pSignalSemaphores = nullptr;
  1496. VkResult err = vkQueueSubmit(graphics_queue, 1, &submit_info, VK_NULL_HANDLE);
  1497. ERR_FAIL_COND(err);
  1498. vkDeviceWaitIdle(device);
  1499. command_buffer_count = 1;
  1500. }
  1501. }
  1502. Error VulkanContext::prepare_buffers() {
  1503. if (!queues_initialized) {
  1504. return OK;
  1505. }
  1506. VkResult err;
  1507. // Ensure no more than FRAME_LAG renderings are outstanding
  1508. vkWaitForFences(device, 1, &fences[frame_index], VK_TRUE, UINT64_MAX);
  1509. vkResetFences(device, 1, &fences[frame_index]);
  1510. for (Map<int, Window>::Element *E = windows.front(); E; E = E->next()) {
  1511. Window *w = &E->get();
  1512. w->semaphore_acquired = false;
  1513. if (w->swapchain == VK_NULL_HANDLE) {
  1514. continue;
  1515. }
  1516. do {
  1517. // Get the index of the next available swapchain image:
  1518. err =
  1519. fpAcquireNextImageKHR(device, w->swapchain, UINT64_MAX,
  1520. w->image_acquired_semaphores[frame_index], VK_NULL_HANDLE, &w->current_buffer);
  1521. if (err == VK_ERROR_OUT_OF_DATE_KHR) {
  1522. // swapchain is out of date (e.g. the window was resized) and
  1523. // must be recreated:
  1524. print_verbose("Vulkan: Early out of date swapchain, recreating.");
  1525. //resize_notify();
  1526. _update_swap_chain(w);
  1527. } else if (err == VK_SUBOPTIMAL_KHR) {
  1528. // swapchain is not as optimal as it could be, but the platform's
  1529. // presentation engine will still present the image correctly.
  1530. print_verbose("Vulkan: Early suboptimal swapchain.");
  1531. break;
  1532. } else if (err != VK_SUCCESS) {
  1533. ERR_BREAK_MSG(err != VK_SUCCESS, "Vulkan: Did not create swapchain successfully.");
  1534. } else {
  1535. w->semaphore_acquired = true;
  1536. }
  1537. } while (err != VK_SUCCESS);
  1538. }
  1539. buffers_prepared = true;
  1540. return OK;
  1541. }
  1542. Error VulkanContext::swap_buffers() {
  1543. if (!queues_initialized) {
  1544. return OK;
  1545. }
  1546. // print_line("swapbuffers?");
  1547. VkResult err;
  1548. #if 0
  1549. if (VK_GOOGLE_display_timing_enabled) {
  1550. // Look at what happened to previous presents, and make appropriate
  1551. // adjustments in timing:
  1552. DemoUpdateTargetIPD(demo);
  1553. // Note: a real application would position its geometry to that it's in
  1554. // the correct location for when the next image is presented. It might
  1555. // also wait, so that there's less latency between any input and when
  1556. // the next image is rendered/presented. This demo program is so
  1557. // simple that it doesn't do either of those.
  1558. }
  1559. #endif
  1560. // Wait for the image acquired semaphore to be signalled to ensure
  1561. // that the image won't be rendered to until the presentation
  1562. // engine has fully released ownership to the application, and it is
  1563. // okay to render to the image.
  1564. const VkCommandBuffer *commands_ptr = nullptr;
  1565. uint32_t commands_to_submit = 0;
  1566. if (command_buffer_queue[0] == nullptr) {
  1567. //no setup command, but commands to submit, submit from the first and skip command
  1568. if (command_buffer_count > 1) {
  1569. commands_ptr = command_buffer_queue.ptr() + 1;
  1570. commands_to_submit = command_buffer_count - 1;
  1571. }
  1572. } else {
  1573. commands_ptr = command_buffer_queue.ptr();
  1574. commands_to_submit = command_buffer_count;
  1575. }
  1576. VkSemaphore *semaphores_to_acquire = (VkSemaphore *)alloca(windows.size() * sizeof(VkSemaphore));
  1577. uint32_t semaphores_to_acquire_count = 0;
  1578. for (Map<int, Window>::Element *E = windows.front(); E; E = E->next()) {
  1579. Window *w = &E->get();
  1580. if (w->semaphore_acquired) {
  1581. semaphores_to_acquire[semaphores_to_acquire_count++] = w->image_acquired_semaphores[frame_index];
  1582. }
  1583. }
  1584. VkPipelineStageFlags pipe_stage_flags;
  1585. VkSubmitInfo submit_info;
  1586. submit_info.sType = VK_STRUCTURE_TYPE_SUBMIT_INFO;
  1587. submit_info.pNext = nullptr;
  1588. submit_info.pWaitDstStageMask = &pipe_stage_flags;
  1589. pipe_stage_flags = VK_PIPELINE_STAGE_COLOR_ATTACHMENT_OUTPUT_BIT;
  1590. submit_info.waitSemaphoreCount = semaphores_to_acquire_count;
  1591. submit_info.pWaitSemaphores = semaphores_to_acquire;
  1592. submit_info.commandBufferCount = commands_to_submit;
  1593. submit_info.pCommandBuffers = commands_ptr;
  1594. submit_info.signalSemaphoreCount = 1;
  1595. submit_info.pSignalSemaphores = &draw_complete_semaphores[frame_index];
  1596. err = vkQueueSubmit(graphics_queue, 1, &submit_info, fences[frame_index]);
  1597. ERR_FAIL_COND_V(err, ERR_CANT_CREATE);
  1598. command_buffer_queue.write[0] = nullptr;
  1599. command_buffer_count = 1;
  1600. if (separate_present_queue) {
  1601. // If we are using separate queues, change image ownership to the
  1602. // present queue before presenting, waiting for the draw complete
  1603. // semaphore and signalling the ownership released semaphore when finished
  1604. VkFence nullFence = VK_NULL_HANDLE;
  1605. pipe_stage_flags = VK_PIPELINE_STAGE_COLOR_ATTACHMENT_OUTPUT_BIT;
  1606. submit_info.waitSemaphoreCount = 1;
  1607. submit_info.pWaitSemaphores = &draw_complete_semaphores[frame_index];
  1608. submit_info.commandBufferCount = 0;
  1609. VkCommandBuffer *cmdbufptr = (VkCommandBuffer *)alloca(sizeof(VkCommandBuffer *) * windows.size());
  1610. submit_info.pCommandBuffers = cmdbufptr;
  1611. for (Map<int, Window>::Element *E = windows.front(); E; E = E->next()) {
  1612. Window *w = &E->get();
  1613. if (w->swapchain == VK_NULL_HANDLE) {
  1614. continue;
  1615. }
  1616. cmdbufptr[submit_info.commandBufferCount] = w->swapchain_image_resources[w->current_buffer].graphics_to_present_cmd;
  1617. submit_info.commandBufferCount++;
  1618. }
  1619. submit_info.signalSemaphoreCount = 1;
  1620. submit_info.pSignalSemaphores = &image_ownership_semaphores[frame_index];
  1621. err = vkQueueSubmit(present_queue, 1, &submit_info, nullFence);
  1622. ERR_FAIL_COND_V(err, ERR_CANT_CREATE);
  1623. }
  1624. // If we are using separate queues, we have to wait for image ownership,
  1625. // otherwise wait for draw complete
  1626. VkPresentInfoKHR present = {
  1627. /*sType*/ VK_STRUCTURE_TYPE_PRESENT_INFO_KHR,
  1628. /*pNext*/ nullptr,
  1629. /*waitSemaphoreCount*/ 1,
  1630. /*pWaitSemaphores*/ (separate_present_queue) ? &image_ownership_semaphores[frame_index] : &draw_complete_semaphores[frame_index],
  1631. /*swapchainCount*/ 0,
  1632. /*pSwapchain*/ nullptr,
  1633. /*pImageIndices*/ nullptr,
  1634. /*pResults*/ nullptr,
  1635. };
  1636. VkSwapchainKHR *pSwapchains = (VkSwapchainKHR *)alloca(sizeof(VkSwapchainKHR *) * windows.size());
  1637. uint32_t *pImageIndices = (uint32_t *)alloca(sizeof(uint32_t *) * windows.size());
  1638. present.pSwapchains = pSwapchains;
  1639. present.pImageIndices = pImageIndices;
  1640. for (Map<int, Window>::Element *E = windows.front(); E; E = E->next()) {
  1641. Window *w = &E->get();
  1642. if (w->swapchain == VK_NULL_HANDLE) {
  1643. continue;
  1644. }
  1645. pSwapchains[present.swapchainCount] = w->swapchain;
  1646. pImageIndices[present.swapchainCount] = w->current_buffer;
  1647. present.swapchainCount++;
  1648. }
  1649. #if 0
  1650. if (VK_KHR_incremental_present_enabled) {
  1651. // If using VK_KHR_incremental_present, we provide a hint of the region
  1652. // that contains changed content relative to the previously-presented
  1653. // image. The implementation can use this hint in order to save
  1654. // work/power (by only copying the region in the hint). The
  1655. // implementation is free to ignore the hint though, and so we must
  1656. // ensure that the entire image has the correctly-drawn content.
  1657. uint32_t eighthOfWidth = width / 8;
  1658. uint32_t eighthOfHeight = height / 8;
  1659. VkRectLayerKHR rect = {
  1660. /*offset.x*/ eighthOfWidth,
  1661. /*offset.y*/ eighthOfHeight,
  1662. /*extent.width*/ eighthOfWidth * 6,
  1663. /*extent.height*/ eighthOfHeight * 6,
  1664. /*layer*/ 0,
  1665. };
  1666. VkPresentRegionKHR region = {
  1667. /*rectangleCount*/ 1,
  1668. /*pRectangles*/ &rect,
  1669. };
  1670. VkPresentRegionsKHR regions = {
  1671. /*sType*/ VK_STRUCTURE_TYPE_PRESENT_REGIONS_KHR,
  1672. /*pNext*/ present.pNext,
  1673. /*swapchainCount*/ present.swapchainCount,
  1674. /*pRegions*/ &region,
  1675. };
  1676. present.pNext = &regions;
  1677. }
  1678. #endif
  1679. #if 0
  1680. if (VK_GOOGLE_display_timing_enabled) {
  1681. VkPresentTimeGOOGLE ptime;
  1682. if (prev_desired_present_time == 0) {
  1683. // This must be the first present for this swapchain.
  1684. //
  1685. // We don't know where we are relative to the presentation engine's
  1686. // display's refresh cycle. We also don't know how long rendering
  1687. // takes. Let's make a grossly-simplified assumption that the
  1688. // desiredPresentTime should be half way between now and
  1689. // now+target_IPD. We will adjust over time.
  1690. uint64_t curtime = getTimeInNanoseconds();
  1691. if (curtime == 0) {
  1692. // Since we didn't find out the current time, don't give a
  1693. // desiredPresentTime:
  1694. ptime.desiredPresentTime = 0;
  1695. } else {
  1696. ptime.desiredPresentTime = curtime + (target_IPD >> 1);
  1697. }
  1698. } else {
  1699. ptime.desiredPresentTime = (prev_desired_present_time + target_IPD);
  1700. }
  1701. ptime.presentID = next_present_id++;
  1702. prev_desired_present_time = ptime.desiredPresentTime;
  1703. VkPresentTimesInfoGOOGLE present_time = {
  1704. /*sType*/ VK_STRUCTURE_TYPE_PRESENT_TIMES_INFO_GOOGLE,
  1705. /*pNext*/ present.pNext,
  1706. /*swapchainCount*/ present.swapchainCount,
  1707. /*pTimes*/ &ptime,
  1708. };
  1709. if (VK_GOOGLE_display_timing_enabled) {
  1710. present.pNext = &present_time;
  1711. }
  1712. }
  1713. #endif
  1714. static int total_frames = 0;
  1715. total_frames++;
  1716. // print_line("current buffer: " + itos(current_buffer));
  1717. err = fpQueuePresentKHR(present_queue, &present);
  1718. frame_index += 1;
  1719. frame_index %= FRAME_LAG;
  1720. if (err == VK_ERROR_OUT_OF_DATE_KHR) {
  1721. // swapchain is out of date (e.g. the window was resized) and
  1722. // must be recreated:
  1723. print_verbose("Vulkan: Swapchain is out of date, recreating.");
  1724. resize_notify();
  1725. } else if (err == VK_SUBOPTIMAL_KHR) {
  1726. // swapchain is not as optimal as it could be, but the platform's
  1727. // presentation engine will still present the image correctly.
  1728. print_verbose("Vulkan: Swapchain is suboptimal.");
  1729. } else {
  1730. ERR_FAIL_COND_V(err, ERR_CANT_CREATE);
  1731. }
  1732. buffers_prepared = false;
  1733. return OK;
  1734. }
  1735. void VulkanContext::resize_notify() {
  1736. }
  1737. VkDevice VulkanContext::get_device() {
  1738. return device;
  1739. }
  1740. VkPhysicalDevice VulkanContext::get_physical_device() {
  1741. return gpu;
  1742. }
  1743. int VulkanContext::get_swapchain_image_count() const {
  1744. return swapchainImageCount;
  1745. }
  1746. uint32_t VulkanContext::get_graphics_queue() const {
  1747. return graphics_queue_family_index;
  1748. }
  1749. VkFormat VulkanContext::get_screen_format() const {
  1750. return format;
  1751. }
  1752. VkPhysicalDeviceLimits VulkanContext::get_device_limits() const {
  1753. return gpu_props.limits;
  1754. }
  1755. RID VulkanContext::local_device_create() {
  1756. LocalDevice ld;
  1757. { //create device
  1758. VkResult err;
  1759. float queue_priorities[1] = { 0.0 };
  1760. VkDeviceQueueCreateInfo queues[2];
  1761. queues[0].sType = VK_STRUCTURE_TYPE_DEVICE_QUEUE_CREATE_INFO;
  1762. queues[0].pNext = nullptr;
  1763. queues[0].queueFamilyIndex = graphics_queue_family_index;
  1764. queues[0].queueCount = 1;
  1765. queues[0].pQueuePriorities = queue_priorities;
  1766. queues[0].flags = 0;
  1767. VkDeviceCreateInfo sdevice = {
  1768. /*sType =*/VK_STRUCTURE_TYPE_DEVICE_CREATE_INFO,
  1769. /*pNext */ nullptr,
  1770. /*flags */ 0,
  1771. /*queueCreateInfoCount */ 1,
  1772. /*pQueueCreateInfos */ queues,
  1773. /*enabledLayerCount */ 0,
  1774. /*ppEnabledLayerNames */ nullptr,
  1775. /*enabledExtensionCount */ enabled_extension_count,
  1776. /*ppEnabledExtensionNames */ (const char *const *)extension_names,
  1777. /*pEnabledFeatures */ &physical_device_features, // If specific features are required, pass them in here
  1778. };
  1779. err = vkCreateDevice(gpu, &sdevice, nullptr, &ld.device);
  1780. ERR_FAIL_COND_V(err, RID());
  1781. }
  1782. { //create graphics queue
  1783. vkGetDeviceQueue(ld.device, graphics_queue_family_index, 0, &ld.queue);
  1784. }
  1785. return local_device_owner.make_rid(ld);
  1786. }
  1787. VkDevice VulkanContext::local_device_get_vk_device(RID p_local_device) {
  1788. LocalDevice *ld = local_device_owner.getornull(p_local_device);
  1789. return ld->device;
  1790. }
  1791. void VulkanContext::local_device_push_command_buffers(RID p_local_device, const VkCommandBuffer *p_buffers, int p_count) {
  1792. LocalDevice *ld = local_device_owner.getornull(p_local_device);
  1793. ERR_FAIL_COND(ld->waiting);
  1794. VkSubmitInfo submit_info;
  1795. submit_info.sType = VK_STRUCTURE_TYPE_SUBMIT_INFO;
  1796. submit_info.pNext = nullptr;
  1797. submit_info.pWaitDstStageMask = nullptr;
  1798. submit_info.waitSemaphoreCount = 0;
  1799. submit_info.pWaitSemaphores = nullptr;
  1800. submit_info.commandBufferCount = p_count;
  1801. submit_info.pCommandBuffers = p_buffers;
  1802. submit_info.signalSemaphoreCount = 0;
  1803. submit_info.pSignalSemaphores = nullptr;
  1804. VkResult err = vkQueueSubmit(ld->queue, 1, &submit_info, VK_NULL_HANDLE);
  1805. if (err == VK_ERROR_OUT_OF_HOST_MEMORY) {
  1806. print_line("Vulkan: Out of host memory!");
  1807. }
  1808. if (err == VK_ERROR_OUT_OF_DEVICE_MEMORY) {
  1809. print_line("Vulkan: Out of device memory!");
  1810. }
  1811. if (err == VK_ERROR_DEVICE_LOST) {
  1812. print_line("Vulkan: Device lost!");
  1813. }
  1814. ERR_FAIL_COND(err);
  1815. ld->waiting = true;
  1816. }
  1817. void VulkanContext::local_device_sync(RID p_local_device) {
  1818. LocalDevice *ld = local_device_owner.getornull(p_local_device);
  1819. ERR_FAIL_COND(!ld->waiting);
  1820. vkDeviceWaitIdle(ld->device);
  1821. ld->waiting = false;
  1822. }
  1823. void VulkanContext::local_device_free(RID p_local_device) {
  1824. LocalDevice *ld = local_device_owner.getornull(p_local_device);
  1825. vkDestroyDevice(ld->device, nullptr);
  1826. local_device_owner.free(p_local_device);
  1827. }
  1828. void VulkanContext::command_begin_label(VkCommandBuffer p_command_buffer, String p_label_name, const Color p_color) {
  1829. if (!enabled_debug_utils) {
  1830. return;
  1831. }
  1832. CharString cs = p_label_name.utf8().get_data();
  1833. VkDebugUtilsLabelEXT label;
  1834. label.sType = VK_STRUCTURE_TYPE_DEBUG_UTILS_LABEL_EXT;
  1835. label.pNext = nullptr;
  1836. label.pLabelName = cs.get_data();
  1837. label.color[0] = p_color[0];
  1838. label.color[1] = p_color[1];
  1839. label.color[2] = p_color[2];
  1840. label.color[3] = p_color[3];
  1841. CmdBeginDebugUtilsLabelEXT(p_command_buffer, &label);
  1842. }
  1843. void VulkanContext::command_insert_label(VkCommandBuffer p_command_buffer, String p_label_name, const Color p_color) {
  1844. if (!enabled_debug_utils) {
  1845. return;
  1846. }
  1847. CharString cs = p_label_name.utf8().get_data();
  1848. VkDebugUtilsLabelEXT label;
  1849. label.sType = VK_STRUCTURE_TYPE_DEBUG_UTILS_LABEL_EXT;
  1850. label.pNext = nullptr;
  1851. label.pLabelName = cs.get_data();
  1852. label.color[0] = p_color[0];
  1853. label.color[1] = p_color[1];
  1854. label.color[2] = p_color[2];
  1855. label.color[3] = p_color[3];
  1856. CmdInsertDebugUtilsLabelEXT(p_command_buffer, &label);
  1857. }
  1858. void VulkanContext::command_end_label(VkCommandBuffer p_command_buffer) {
  1859. if (!enabled_debug_utils) {
  1860. return;
  1861. }
  1862. CmdEndDebugUtilsLabelEXT(p_command_buffer);
  1863. }
  1864. void VulkanContext::set_object_name(VkObjectType p_object_type, uint64_t p_object_handle, String p_object_name) {
  1865. if (!enabled_debug_utils) {
  1866. return;
  1867. }
  1868. CharString obj_data = p_object_name.utf8();
  1869. VkDebugUtilsObjectNameInfoEXT name_info;
  1870. name_info.sType = VK_STRUCTURE_TYPE_DEBUG_UTILS_OBJECT_NAME_INFO_EXT;
  1871. name_info.pNext = nullptr;
  1872. name_info.objectType = p_object_type;
  1873. name_info.objectHandle = p_object_handle;
  1874. name_info.pObjectName = obj_data.get_data();
  1875. SetDebugUtilsObjectNameEXT(device, &name_info);
  1876. }
  1877. String VulkanContext::get_device_vendor_name() const {
  1878. return device_vendor;
  1879. }
  1880. String VulkanContext::get_device_name() const {
  1881. return device_name;
  1882. }
  1883. String VulkanContext::get_device_pipeline_cache_uuid() const {
  1884. return pipeline_cache_id;
  1885. }
  1886. VulkanContext::VulkanContext() {
  1887. command_buffer_queue.resize(1); // First one is always the setup command.
  1888. command_buffer_queue.write[0] = nullptr;
  1889. }
  1890. VulkanContext::~VulkanContext() {
  1891. if (queue_props) {
  1892. free(queue_props);
  1893. }
  1894. if (device_initialized) {
  1895. for (uint32_t i = 0; i < FRAME_LAG; i++) {
  1896. vkDestroyFence(device, fences[i], nullptr);
  1897. vkDestroySemaphore(device, draw_complete_semaphores[i], nullptr);
  1898. if (separate_present_queue) {
  1899. vkDestroySemaphore(device, image_ownership_semaphores[i], nullptr);
  1900. }
  1901. }
  1902. if (inst_initialized && enabled_debug_utils) {
  1903. DestroyDebugUtilsMessengerEXT(inst, dbg_messenger, nullptr);
  1904. }
  1905. if (inst_initialized && dbg_debug_report != VK_NULL_HANDLE) {
  1906. DestroyDebugReportCallbackEXT(inst, dbg_debug_report, nullptr);
  1907. }
  1908. vkDestroyDevice(device, nullptr);
  1909. }
  1910. if (inst_initialized) {
  1911. vkDestroyInstance(inst, nullptr);
  1912. }
  1913. }