vulkan_context.cpp 81 KB

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