vulkan_context.cpp 114 KB

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