BsVulkanCommandBuffer.cpp 67 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089
  1. //********************************** Banshee Engine (www.banshee3d.com) **************************************************//
  2. //**************** Copyright (c) 2016 Marko Pintera ([email protected]). All rights reserved. **********************//
  3. #include "BsVulkanCommandBuffer.h"
  4. #include "BsVulkanCommandBufferManager.h"
  5. #include "BsVulkanUtility.h"
  6. #include "BsVulkanDevice.h"
  7. #include "BsVulkanGpuParams.h"
  8. #include "BsVulkanQueue.h"
  9. #include "BsVulkanTexture.h"
  10. #include "BsVulkanIndexBuffer.h"
  11. #include "BsVulkanVertexBuffer.h"
  12. #include "BsVulkanHardwareBuffer.h"
  13. #include "BsVulkanFramebuffer.h"
  14. #include "BsVulkanVertexInputManager.h"
  15. #include "BsVulkanEventQuery.h"
  16. #include "BsVulkanQueryManager.h"
  17. #include "BsVulkanSwapChain.h"
  18. #if BS_PLATFORM == BS_PLATFORM_WIN32
  19. #include "Win32/BsWin32RenderWindow.h"
  20. #else
  21. static_assert(false, "Other platforms go here");
  22. #endif
  23. namespace bs { namespace ct
  24. {
  25. VulkanSemaphore::VulkanSemaphore(VulkanResourceManager* owner)
  26. :VulkanResource(owner, true)
  27. {
  28. VkSemaphoreCreateInfo semaphoreCI;
  29. semaphoreCI.sType = VK_STRUCTURE_TYPE_SEMAPHORE_CREATE_INFO;
  30. semaphoreCI.pNext = nullptr;
  31. semaphoreCI.flags = 0;
  32. VkResult result = vkCreateSemaphore(owner->getDevice().getLogical(), &semaphoreCI, gVulkanAllocator, &mSemaphore);
  33. assert(result == VK_SUCCESS);
  34. }
  35. VulkanSemaphore::~VulkanSemaphore()
  36. {
  37. vkDestroySemaphore(mOwner->getDevice().getLogical(), mSemaphore, gVulkanAllocator);
  38. }
  39. VulkanCmdBufferPool::VulkanCmdBufferPool(VulkanDevice& device)
  40. :mDevice(device), mNextId(1)
  41. {
  42. for (UINT32 i = 0; i < GQT_COUNT; i++)
  43. {
  44. UINT32 familyIdx = device.getQueueFamily((GpuQueueType)i);
  45. if (familyIdx == (UINT32)-1)
  46. continue;
  47. VkCommandPoolCreateInfo poolCI;
  48. poolCI.sType = VK_STRUCTURE_TYPE_COMMAND_POOL_CREATE_INFO;
  49. poolCI.pNext = nullptr;
  50. poolCI.flags = VK_COMMAND_POOL_CREATE_RESET_COMMAND_BUFFER_BIT;
  51. poolCI.queueFamilyIndex = familyIdx;
  52. PoolInfo& poolInfo = mPools[familyIdx];
  53. poolInfo.queueFamily = familyIdx;
  54. memset(poolInfo.buffers, 0, sizeof(poolInfo.buffers));
  55. vkCreateCommandPool(device.getLogical(), &poolCI, gVulkanAllocator, &poolInfo.pool);
  56. }
  57. }
  58. VulkanCmdBufferPool::~VulkanCmdBufferPool()
  59. {
  60. // Note: Shutdown should be the only place command buffers are destroyed at, as the system relies on the fact that
  61. // they won't be destroyed during normal operation.
  62. for(auto& entry : mPools)
  63. {
  64. PoolInfo& poolInfo = entry.second;
  65. for (UINT32 i = 0; i < BS_MAX_VULKAN_CB_PER_QUEUE_FAMILY; i++)
  66. {
  67. VulkanCmdBuffer* buffer = poolInfo.buffers[i];
  68. if (buffer == nullptr)
  69. break;
  70. bs_delete(buffer);
  71. }
  72. vkDestroyCommandPool(mDevice.getLogical(), poolInfo.pool, gVulkanAllocator);
  73. }
  74. }
  75. VulkanCmdBuffer* VulkanCmdBufferPool::getBuffer(UINT32 queueFamily, bool secondary)
  76. {
  77. auto iterFind = mPools.find(queueFamily);
  78. if (iterFind == mPools.end())
  79. return nullptr;
  80. VulkanCmdBuffer** buffers = iterFind->second.buffers;
  81. UINT32 i = 0;
  82. for(; i < BS_MAX_VULKAN_CB_PER_QUEUE_FAMILY; i++)
  83. {
  84. if (buffers[i] == nullptr)
  85. break;
  86. if(buffers[i]->mState == VulkanCmdBuffer::State::Ready)
  87. {
  88. buffers[i]->begin();
  89. return buffers[i];
  90. }
  91. }
  92. assert(i < BS_MAX_VULKAN_CB_PER_QUEUE_FAMILY &&
  93. "Too many command buffers allocated. Increment BS_MAX_VULKAN_CB_PER_QUEUE_FAMILY to a higher value. ");
  94. buffers[i] = createBuffer(queueFamily, secondary);
  95. buffers[i]->begin();
  96. return buffers[i];
  97. }
  98. VulkanCmdBuffer* VulkanCmdBufferPool::createBuffer(UINT32 queueFamily, bool secondary)
  99. {
  100. auto iterFind = mPools.find(queueFamily);
  101. if (iterFind == mPools.end())
  102. return nullptr;
  103. const PoolInfo& poolInfo = iterFind->second;
  104. return bs_new<VulkanCmdBuffer>(mDevice, mNextId++, poolInfo.pool, poolInfo.queueFamily, secondary);
  105. }
  106. VulkanCmdBuffer::VulkanCmdBuffer(VulkanDevice& device, UINT32 id, VkCommandPool pool, UINT32 queueFamily, bool secondary)
  107. : mId(id), mQueueFamily(queueFamily), mState(State::Ready), mDevice(device), mPool(pool)
  108. , mIntraQueueSemaphore(nullptr), mInterQueueSemaphores(), mNumUsedInterQueueSemaphores(0)
  109. , mFramebuffer(nullptr), mRenderTargetWidth(0)
  110. , mRenderTargetHeight(0), mRenderTargetDepthReadOnly(false), mRenderTargetLoadMask(RT_NONE), mGlobalQueueIdx(-1)
  111. , mViewport(0.0f, 0.0f, 1.0f, 1.0f), mScissor(0, 0, 0, 0), mStencilRef(0), mDrawOp(DOT_TRIANGLE_LIST)
  112. , mNumBoundDescriptorSets(0), mGfxPipelineRequiresBind(true), mCmpPipelineRequiresBind(true)
  113. , mViewportRequiresBind(true), mStencilRefRequiresBind(true), mScissorRequiresBind(true), mBoundParamsDirty(false)
  114. , mClearValues(), mClearMask(), mSemaphoresTemp(BS_MAX_UNIQUE_QUEUES), mVertexBuffersTemp()
  115. , mVertexBufferOffsetsTemp()
  116. {
  117. UINT32 maxBoundDescriptorSets = device.getDeviceProperties().limits.maxBoundDescriptorSets;
  118. mDescriptorSetsTemp = (VkDescriptorSet*)bs_alloc(sizeof(VkDescriptorSet) * maxBoundDescriptorSets);
  119. VkCommandBufferAllocateInfo cmdBufferAllocInfo;
  120. cmdBufferAllocInfo.sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_ALLOCATE_INFO;
  121. cmdBufferAllocInfo.pNext = nullptr;
  122. cmdBufferAllocInfo.commandPool = pool;
  123. cmdBufferAllocInfo.level = secondary ? VK_COMMAND_BUFFER_LEVEL_SECONDARY : VK_COMMAND_BUFFER_LEVEL_PRIMARY;
  124. cmdBufferAllocInfo.commandBufferCount = 1;
  125. VkResult result = vkAllocateCommandBuffers(mDevice.getLogical(), &cmdBufferAllocInfo, &mCmdBuffer);
  126. assert(result == VK_SUCCESS);
  127. VkFenceCreateInfo fenceCI;
  128. fenceCI.sType = VK_STRUCTURE_TYPE_FENCE_CREATE_INFO;
  129. fenceCI.pNext = nullptr;
  130. fenceCI.flags = 0;
  131. result = vkCreateFence(mDevice.getLogical(), &fenceCI, gVulkanAllocator, &mFence);
  132. assert(result == VK_SUCCESS);
  133. }
  134. VulkanCmdBuffer::~VulkanCmdBuffer()
  135. {
  136. VkDevice device = mDevice.getLogical();
  137. if(mState == State::Submitted)
  138. {
  139. // Wait 1s
  140. UINT64 waitTime = 1000 * 1000 * 1000;
  141. VkResult result = vkWaitForFences(device, 1, &mFence, true, waitTime);
  142. assert(result == VK_SUCCESS || result == VK_TIMEOUT);
  143. if (result == VK_TIMEOUT)
  144. LOGWRN("Freeing a command buffer before done executing because fence wait expired!");
  145. // Resources have been marked as used, make sure to notify them we're done with them
  146. reset();
  147. }
  148. else if(mState != State::Ready)
  149. {
  150. // Notify any resources that they are no longer bound
  151. for (auto& entry : mResources)
  152. {
  153. ResourceUseHandle& useHandle = entry.second;
  154. assert(!useHandle.used);
  155. entry.first->notifyUnbound();
  156. }
  157. for (auto& entry : mImages)
  158. {
  159. UINT32 imageInfoIdx = entry.second;
  160. ImageInfo& imageInfo = mImageInfos[imageInfoIdx];
  161. ResourceUseHandle& useHandle = imageInfo.useHandle;
  162. assert(!useHandle.used);
  163. entry.first->notifyUnbound();
  164. }
  165. for (auto& entry : mBuffers)
  166. {
  167. ResourceUseHandle& useHandle = entry.second.useHandle;
  168. assert(!useHandle.used);
  169. entry.first->notifyUnbound();
  170. }
  171. }
  172. if (mIntraQueueSemaphore != nullptr)
  173. mIntraQueueSemaphore->destroy();
  174. for(UINT32 i = 0; i < BS_MAX_VULKAN_CB_DEPENDENCIES; i++)
  175. {
  176. if (mInterQueueSemaphores[i] != nullptr)
  177. mInterQueueSemaphores[i]->destroy();
  178. }
  179. vkDestroyFence(device, mFence, gVulkanAllocator);
  180. vkFreeCommandBuffers(device, mPool, 1, &mCmdBuffer);
  181. bs_free(mDescriptorSetsTemp);
  182. }
  183. UINT32 VulkanCmdBuffer::getDeviceIdx() const
  184. {
  185. return mDevice.getIndex();
  186. }
  187. void VulkanCmdBuffer::begin()
  188. {
  189. assert(mState == State::Ready);
  190. VkCommandBufferBeginInfo beginInfo;
  191. beginInfo.sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_BEGIN_INFO;
  192. beginInfo.pNext = nullptr;
  193. beginInfo.flags = VK_COMMAND_BUFFER_USAGE_ONE_TIME_SUBMIT_BIT;
  194. beginInfo.pInheritanceInfo = nullptr;
  195. VkResult result = vkBeginCommandBuffer(mCmdBuffer, &beginInfo);
  196. assert(result == VK_SUCCESS);
  197. mState = State::Recording;
  198. }
  199. void VulkanCmdBuffer::end()
  200. {
  201. assert(mState == State::Recording);
  202. // If a clear is queued, execute the render pass with no additional instructions
  203. if (mClearMask)
  204. executeClearPass();
  205. VkResult result = vkEndCommandBuffer(mCmdBuffer);
  206. assert(result == VK_SUCCESS);
  207. mState = State::RecordingDone;
  208. }
  209. void VulkanCmdBuffer::beginRenderPass()
  210. {
  211. assert(mState == State::Recording);
  212. if (mFramebuffer == nullptr)
  213. {
  214. LOGWRN("Attempting to begin a render pass but no render target is bound to the command buffer.");
  215. return;
  216. }
  217. if(mClearMask != CLEAR_NONE)
  218. {
  219. // If a previous clear is queued, but it doesn't match the rendered area, need to execute a separate pass
  220. // just for it
  221. Rect2I rtArea(0, 0, mRenderTargetWidth, mRenderTargetHeight);
  222. if (mClearArea != rtArea)
  223. executeClearPass();
  224. }
  225. executeLayoutTransitions();
  226. // Check if any frame-buffer attachments are also used as shader inputs, in which case we make them read-only
  227. RenderSurfaceMask readMask = RT_NONE;
  228. UINT32 numColorAttachments = mFramebuffer->getNumColorAttachments();
  229. for(UINT32 i = 0; i < numColorAttachments; i++)
  230. {
  231. const VulkanFramebufferAttachment& fbAttachment = mFramebuffer->getColorAttachment(i);
  232. ImageSubresourceInfo& subresourceInfo = findSubresourceInfo(fbAttachment.image, fbAttachment.surface.arraySlice,
  233. fbAttachment.surface.mipLevel);
  234. bool readOnly = subresourceInfo.isShaderInput;
  235. if(readOnly)
  236. readMask.set((RenderSurfaceMaskBits)(1 << i));
  237. }
  238. if(mFramebuffer->hasDepthAttachment())
  239. {
  240. const VulkanFramebufferAttachment& fbAttachment = mFramebuffer->getDepthStencilAttachment();
  241. ImageSubresourceInfo& subresourceInfo = findSubresourceInfo(fbAttachment.image, fbAttachment.surface.arraySlice,
  242. fbAttachment.surface.mipLevel);
  243. bool readOnly = subresourceInfo.isShaderInput;
  244. if (readOnly)
  245. readMask.set(RT_DEPTH);
  246. }
  247. // Reset flags that signal image usage (since those only matter for the render-pass' purposes), as well as barrier
  248. // requirements
  249. for (auto& entry : mSubresourceInfos)
  250. {
  251. entry.isFBAttachment = false;
  252. entry.isShaderInput = false;
  253. entry.needsBarrier = false;
  254. }
  255. for (auto& entry : mBuffers)
  256. entry.second.needsBarrier = false;
  257. VkRenderPassBeginInfo renderPassBeginInfo;
  258. renderPassBeginInfo.sType = VK_STRUCTURE_TYPE_RENDER_PASS_BEGIN_INFO;
  259. renderPassBeginInfo.pNext = nullptr;
  260. renderPassBeginInfo.framebuffer = mFramebuffer->getFramebuffer(mRenderTargetLoadMask, readMask, mClearMask);
  261. renderPassBeginInfo.renderPass = mFramebuffer->getRenderPass(mRenderTargetLoadMask, readMask, mClearMask);
  262. renderPassBeginInfo.renderArea.offset.x = 0;
  263. renderPassBeginInfo.renderArea.offset.y = 0;
  264. renderPassBeginInfo.renderArea.extent.width = mRenderTargetWidth;
  265. renderPassBeginInfo.renderArea.extent.height = mRenderTargetHeight;
  266. renderPassBeginInfo.clearValueCount = mFramebuffer->getNumClearEntries(mClearMask);
  267. renderPassBeginInfo.pClearValues = mClearValues.data();
  268. vkCmdBeginRenderPass(mCmdBuffer, &renderPassBeginInfo, VK_SUBPASS_CONTENTS_INLINE);
  269. mClearMask = CLEAR_NONE;
  270. mState = State::RecordingRenderPass;
  271. }
  272. void VulkanCmdBuffer::endRenderPass()
  273. {
  274. assert(mState == State::RecordingRenderPass);
  275. vkCmdEndRenderPass(mCmdBuffer);
  276. // Execute any queued events
  277. for(auto& entry : mQueuedEvents)
  278. vkCmdSetEvent(mCmdBuffer, entry->getHandle(), VK_PIPELINE_STAGE_ALL_COMMANDS_BIT);
  279. mQueuedEvents.clear();
  280. // Update any layout transitions that were performed by subpass dependencies, reset flags that signal image usage
  281. // and reset read-only state.
  282. // Note: It's okay reset these even those they might still be bound on the GPU, because these values only matter
  283. // for state transitions.
  284. for (auto& entry : mSubresourceInfos)
  285. {
  286. entry.isFBAttachment = false;
  287. entry.isShaderInput = false;
  288. entry.isReadOnly = true;
  289. }
  290. updateFinalLayouts();
  291. mState = State::Recording;
  292. }
  293. void VulkanCmdBuffer::allocateSemaphores(VkSemaphore* semaphores)
  294. {
  295. if (mIntraQueueSemaphore != nullptr)
  296. mIntraQueueSemaphore->destroy();
  297. mIntraQueueSemaphore = mDevice.getResourceManager().create<VulkanSemaphore>();
  298. semaphores[0] = mIntraQueueSemaphore->getHandle();
  299. for (UINT32 i = 0; i < BS_MAX_VULKAN_CB_DEPENDENCIES; i++)
  300. {
  301. if (mInterQueueSemaphores[i] != nullptr)
  302. mInterQueueSemaphores[i]->destroy();
  303. mInterQueueSemaphores[i] = mDevice.getResourceManager().create<VulkanSemaphore>();
  304. semaphores[i + 1] = mInterQueueSemaphores[i]->getHandle();
  305. }
  306. mNumUsedInterQueueSemaphores = 0;
  307. }
  308. VulkanSemaphore* VulkanCmdBuffer::requestInterQueueSemaphore() const
  309. {
  310. if (mNumUsedInterQueueSemaphores >= BS_MAX_VULKAN_CB_DEPENDENCIES)
  311. return nullptr;
  312. return mInterQueueSemaphores[mNumUsedInterQueueSemaphores++];
  313. }
  314. void VulkanCmdBuffer::submit(VulkanQueue* queue, UINT32 queueIdx, UINT32 syncMask)
  315. {
  316. assert(isReadyForSubmit());
  317. // Make sure to reset the CB fence before we submit it
  318. VkResult result = vkResetFences(mDevice.getLogical(), 1, &mFence);
  319. assert(result == VK_SUCCESS);
  320. // If there are any query resets needed, execute those first
  321. VulkanDevice& device = queue->getDevice();
  322. if(!mQueuedQueryResets.empty())
  323. {
  324. VulkanCmdBuffer* cmdBuffer = device.getCmdBufferPool().getBuffer(mQueueFamily, false);
  325. VkCommandBuffer vkCmdBuffer = cmdBuffer->getHandle();
  326. for (auto& entry : mQueuedQueryResets)
  327. entry->reset(vkCmdBuffer);
  328. cmdBuffer->end();
  329. queue->queueSubmit(cmdBuffer, nullptr, 0);
  330. mQueuedQueryResets.clear();
  331. }
  332. // Issue pipeline barriers for queue transitions (need to happen on original queue first, then on new queue)
  333. for (auto& entry : mBuffers)
  334. {
  335. VulkanBuffer* resource = static_cast<VulkanBuffer*>(entry.first);
  336. if (!resource->isExclusive())
  337. continue;
  338. UINT32 currentQueueFamily = resource->getQueueFamily();
  339. if (currentQueueFamily != -1 && currentQueueFamily != mQueueFamily)
  340. {
  341. Vector<VkBufferMemoryBarrier>& barriers = mTransitionInfoTemp[currentQueueFamily].bufferBarriers;
  342. barriers.push_back(VkBufferMemoryBarrier());
  343. VkBufferMemoryBarrier& barrier = barriers.back();
  344. barrier.sType = VK_STRUCTURE_TYPE_BUFFER_MEMORY_BARRIER;
  345. barrier.pNext = nullptr;
  346. barrier.srcAccessMask = entry.second.accessFlags;
  347. barrier.dstAccessMask = entry.second.accessFlags;
  348. barrier.srcQueueFamilyIndex = currentQueueFamily;
  349. barrier.dstQueueFamilyIndex = mQueueFamily;
  350. barrier.buffer = resource->getHandle();
  351. barrier.offset = 0;
  352. barrier.size = VK_WHOLE_SIZE;
  353. }
  354. }
  355. // For images issue queue transitions, as above. Also issue layout transitions to their inital layouts.
  356. Vector<VkImageMemoryBarrier>& localBarriers = mTransitionInfoTemp[mQueueFamily].imageBarriers;
  357. for (auto& entry : mImages)
  358. {
  359. VulkanImage* resource = static_cast<VulkanImage*>(entry.first);
  360. ImageInfo& imageInfo = mImageInfos[entry.second];
  361. UINT32 currentQueueFamily = resource->getQueueFamily();
  362. bool queueMismatch = resource->isExclusive() && currentQueueFamily != -1 && currentQueueFamily != mQueueFamily;
  363. ImageSubresourceInfo* subresourceInfos = &mSubresourceInfos[imageInfo.subresourceInfoIdx];
  364. if (queueMismatch)
  365. {
  366. Vector<VkImageMemoryBarrier>& barriers = mTransitionInfoTemp[currentQueueFamily].imageBarriers;
  367. for (UINT32 i = 0; i < imageInfo.numSubresourceInfos; i++)
  368. {
  369. ImageSubresourceInfo& subresourceInfo = subresourceInfos[i];
  370. UINT32 startIdx = (UINT32)barriers.size();
  371. resource->getBarriers(subresourceInfo.range, barriers);
  372. for(UINT32 j = startIdx; j < (UINT32)barriers.size(); j++)
  373. {
  374. VkImageMemoryBarrier& barrier = barriers[j];
  375. barrier.dstAccessMask = resource->getAccessFlags(barrier.oldLayout);
  376. barrier.newLayout = barrier.oldLayout;
  377. barrier.srcQueueFamilyIndex = currentQueueFamily;
  378. barrier.dstQueueFamilyIndex = mQueueFamily;
  379. }
  380. }
  381. }
  382. for (UINT32 i = 0; i < imageInfo.numSubresourceInfos; i++)
  383. {
  384. ImageSubresourceInfo& subresourceInfo = subresourceInfos[i];
  385. VkImageLayout initialLayout = subresourceInfo.initialLayout;
  386. if (initialLayout == VK_IMAGE_LAYOUT_UNDEFINED)
  387. continue;
  388. const VkImageSubresourceRange& range = subresourceInfo.range;
  389. UINT32 mipEnd = range.baseMipLevel + range.levelCount;
  390. UINT32 faceEnd = range.baseArrayLayer + range.layerCount;
  391. bool layoutMismatch = false;
  392. for (UINT32 mip = range.baseMipLevel; mip < mipEnd; mip++)
  393. {
  394. for (UINT32 face = range.baseArrayLayer; face < faceEnd; face++)
  395. {
  396. VulkanImageSubresource* subresource = resource->getSubresource(face, mip);
  397. if(subresource->getLayout() != initialLayout)
  398. {
  399. layoutMismatch = true;
  400. break;
  401. }
  402. }
  403. if (layoutMismatch)
  404. break;
  405. }
  406. if(layoutMismatch)
  407. {
  408. UINT32 startIdx = (UINT32)localBarriers.size();
  409. resource->getBarriers(subresourceInfo.range, localBarriers);
  410. for (UINT32 j = startIdx; j < (UINT32)localBarriers.size(); j++)
  411. {
  412. VkImageMemoryBarrier& barrier = localBarriers[j];
  413. barrier.dstAccessMask = resource->getAccessFlags(initialLayout, subresourceInfo.isInitialReadOnly);
  414. barrier.newLayout = initialLayout;
  415. }
  416. }
  417. for (UINT32 mip = range.baseMipLevel; mip < mipEnd; mip++)
  418. {
  419. for (UINT32 face = range.baseArrayLayer; face < faceEnd; face++)
  420. {
  421. VulkanImageSubresource* subresource = resource->getSubresource(face, mip);
  422. subresource->setLayout(subresourceInfo.finalLayout);
  423. }
  424. }
  425. }
  426. }
  427. for (auto& entry : mTransitionInfoTemp)
  428. {
  429. bool empty = entry.second.imageBarriers.size() == 0 && entry.second.bufferBarriers.size() == 0;
  430. if (empty)
  431. continue;
  432. UINT32 entryQueueFamily = entry.first;
  433. // No queue transition needed for entries on this queue (this entry is most likely an image layout transition)
  434. if (entryQueueFamily == -1 || entryQueueFamily == mQueueFamily)
  435. continue;
  436. VulkanCmdBuffer* cmdBuffer = device.getCmdBufferPool().getBuffer(entryQueueFamily, false);
  437. VkCommandBuffer vkCmdBuffer = cmdBuffer->getHandle();
  438. TransitionInfo& barriers = entry.second;
  439. UINT32 numImgBarriers = (UINT32)barriers.imageBarriers.size();
  440. UINT32 numBufferBarriers = (UINT32)barriers.bufferBarriers.size();
  441. vkCmdPipelineBarrier(vkCmdBuffer,
  442. VK_PIPELINE_STAGE_TOP_OF_PIPE_BIT, // Note: VK_PIPELINE_STAGE_BOTTOM_OF_PIPE_BIT might be more correct here, according to the spec
  443. VK_PIPELINE_STAGE_TOP_OF_PIPE_BIT, // The main idea is that the barrier executes before the semaphore triggers, no actual stage dependencies are needed.
  444. 0, 0, nullptr,
  445. numBufferBarriers, barriers.bufferBarriers.data(),
  446. numImgBarriers, barriers.imageBarriers.data());
  447. // Find an appropriate queue to execute on
  448. UINT32 otherQueueIdx = 0;
  449. VulkanQueue* otherQueue = nullptr;
  450. GpuQueueType otherQueueType = GQT_GRAPHICS;
  451. for (UINT32 i = 0; i < GQT_COUNT; i++)
  452. {
  453. otherQueueType = (GpuQueueType)i;
  454. if (device.getQueueFamily(otherQueueType) != entryQueueFamily)
  455. continue;
  456. UINT32 numQueues = device.getNumQueues(otherQueueType);
  457. for (UINT32 j = 0; j < numQueues; j++)
  458. {
  459. // Try to find a queue not currently executing
  460. VulkanQueue* curQueue = device.getQueue(otherQueueType, j);
  461. if (!curQueue->isExecuting())
  462. {
  463. otherQueue = curQueue;
  464. otherQueueIdx = j;
  465. }
  466. }
  467. // Can't find empty one, use the first one then
  468. if (otherQueue == nullptr)
  469. {
  470. otherQueue = device.getQueue(otherQueueType, 0);
  471. otherQueueIdx = 0;
  472. }
  473. break;
  474. }
  475. syncMask |= CommandSyncMask::getGlobalQueueMask(otherQueueType, otherQueueIdx);
  476. cmdBuffer->end();
  477. // Note: If I switch back to doing layout transitions here, I need to wait on present semaphore
  478. otherQueue->submit(cmdBuffer, nullptr, 0);
  479. }
  480. UINT32 deviceIdx = device.getIndex();
  481. VulkanCommandBufferManager& cbm = static_cast<VulkanCommandBufferManager&>(CommandBufferManager::instance());
  482. UINT32 numSemaphores;
  483. cbm.getSyncSemaphores(deviceIdx, syncMask, mSemaphoresTemp.data(), numSemaphores);
  484. // Wait on present (i.e. until the back buffer becomes available) for any swap chains
  485. for(auto& entry : mSwapChains)
  486. {
  487. const SwapChainSurface& surface = entry->getBackBuffer();
  488. if (surface.needsWait)
  489. {
  490. VulkanSemaphore* semaphore = entry->getBackBuffer().sync;
  491. if (numSemaphores >= (UINT32)mSemaphoresTemp.size())
  492. mSemaphoresTemp.push_back(semaphore);
  493. else
  494. mSemaphoresTemp[numSemaphores] = semaphore;
  495. numSemaphores++;
  496. entry->notifyBackBufferWaitIssued();
  497. }
  498. }
  499. // Issue second part of transition pipeline barriers (on this queue)
  500. for (auto& entry : mTransitionInfoTemp)
  501. {
  502. bool empty = entry.second.imageBarriers.size() == 0 && entry.second.bufferBarriers.size() == 0;
  503. if (empty)
  504. continue;
  505. VulkanCmdBuffer* cmdBuffer = device.getCmdBufferPool().getBuffer(mQueueFamily, false);
  506. VkCommandBuffer vkCmdBuffer = cmdBuffer->getHandle();
  507. TransitionInfo& barriers = entry.second;
  508. UINT32 numImgBarriers = (UINT32)barriers.imageBarriers.size();
  509. UINT32 numBufferBarriers = (UINT32)barriers.bufferBarriers.size();
  510. vkCmdPipelineBarrier(vkCmdBuffer,
  511. VK_PIPELINE_STAGE_TOP_OF_PIPE_BIT, // Note: VK_PIPELINE_STAGE_BOTTOM_OF_PIPE_BIT might be more correct here, according to the spec
  512. VK_PIPELINE_STAGE_TOP_OF_PIPE_BIT,
  513. 0, 0, nullptr,
  514. numBufferBarriers, barriers.bufferBarriers.data(),
  515. numImgBarriers, barriers.imageBarriers.data());
  516. cmdBuffer->end();
  517. queue->queueSubmit(cmdBuffer, mSemaphoresTemp.data(), numSemaphores);
  518. numSemaphores = 0; // Semaphores are only needed the first time, since we're adding the buffers on the same queue
  519. }
  520. queue->queueSubmit(this, mSemaphoresTemp.data(), numSemaphores);
  521. queue->submitQueued();
  522. mGlobalQueueIdx = CommandSyncMask::getGlobalQueueIdx(queue->getType(), queueIdx);
  523. for (auto& entry : mResources)
  524. {
  525. ResourceUseHandle& useHandle = entry.second;
  526. assert(!useHandle.used);
  527. useHandle.used = true;
  528. entry.first->notifyUsed(mGlobalQueueIdx, mQueueFamily, useHandle.flags);
  529. }
  530. for (auto& entry : mImages)
  531. {
  532. UINT32 imageInfoIdx = entry.second;
  533. ImageInfo& imageInfo = mImageInfos[imageInfoIdx];
  534. ResourceUseHandle& useHandle = imageInfo.useHandle;
  535. assert(!useHandle.used);
  536. useHandle.used = true;
  537. entry.first->notifyUsed(mGlobalQueueIdx, mQueueFamily, useHandle.flags);
  538. }
  539. for (auto& entry : mBuffers)
  540. {
  541. ResourceUseHandle& useHandle = entry.second.useHandle;
  542. assert(!useHandle.used);
  543. useHandle.used = true;
  544. entry.first->notifyUsed(mGlobalQueueIdx, mQueueFamily, useHandle.flags);
  545. }
  546. // Note: Uncomment for debugging only, prevents any device concurrency issues.
  547. // vkQueueWaitIdle(queue->getHandle());
  548. // Clear vectors but don't clear the actual map, as we want to re-use the memory since we expect queue family
  549. // indices to be the same
  550. for (auto& entry : mTransitionInfoTemp)
  551. {
  552. entry.second.imageBarriers.clear();
  553. entry.second.bufferBarriers.clear();
  554. }
  555. mGraphicsPipeline = nullptr;
  556. mComputePipeline = nullptr;
  557. mGfxPipelineRequiresBind = true;
  558. mCmpPipelineRequiresBind = true;
  559. mFramebuffer = nullptr;
  560. mDescriptorSetsBindState = DescriptorSetBindFlag::Graphics | DescriptorSetBindFlag::Compute;
  561. mQueuedLayoutTransitions.clear();
  562. mBoundParams = nullptr;
  563. mBoundParams = false;
  564. mSwapChains.clear();
  565. }
  566. bool VulkanCmdBuffer::checkFenceStatus(bool block) const
  567. {
  568. VkResult result = vkWaitForFences(mDevice.getLogical(), 1, &mFence, true, block ? 1'000'000'000 : 0);
  569. assert(result == VK_SUCCESS || result == VK_TIMEOUT);
  570. return result == VK_SUCCESS;
  571. }
  572. void VulkanCmdBuffer::reset()
  573. {
  574. bool wasSubmitted = mState == State::Submitted;
  575. mState = State::Ready;
  576. vkResetCommandBuffer(mCmdBuffer, VK_COMMAND_BUFFER_RESET_RELEASE_RESOURCES_BIT); // Note: Maybe better not to release resources?
  577. if (wasSubmitted)
  578. {
  579. for (auto& entry : mResources)
  580. {
  581. ResourceUseHandle& useHandle = entry.second;
  582. assert(useHandle.used);
  583. entry.first->notifyDone(mGlobalQueueIdx, useHandle.flags);
  584. }
  585. for (auto& entry : mImages)
  586. {
  587. UINT32 imageInfoIdx = entry.second;
  588. ImageInfo& imageInfo = mImageInfos[imageInfoIdx];
  589. ResourceUseHandle& useHandle = imageInfo.useHandle;
  590. assert(useHandle.used);
  591. entry.first->notifyDone(mGlobalQueueIdx, useHandle.flags);
  592. }
  593. for (auto& entry : mBuffers)
  594. {
  595. ResourceUseHandle& useHandle = entry.second.useHandle;
  596. assert(useHandle.used);
  597. entry.first->notifyDone(mGlobalQueueIdx, useHandle.flags);
  598. }
  599. }
  600. else
  601. {
  602. for (auto& entry : mResources)
  603. entry.first->notifyUnbound();
  604. for (auto& entry : mImages)
  605. entry.first->notifyUnbound();
  606. for (auto& entry : mBuffers)
  607. entry.first->notifyUnbound();
  608. }
  609. mResources.clear();
  610. mImages.clear();
  611. mBuffers.clear();
  612. mImageInfos.clear();
  613. mSubresourceInfos.clear();
  614. }
  615. void VulkanCmdBuffer::setRenderTarget(const SPtr<RenderTarget>& rt, bool readOnlyDepthStencil,
  616. RenderSurfaceMask loadMask)
  617. {
  618. assert(mState != State::Submitted);
  619. VulkanFramebuffer* newFB;
  620. if(rt != nullptr)
  621. {
  622. if (rt->getProperties().isWindow())
  623. {
  624. Win32RenderWindow* window = static_cast<Win32RenderWindow*>(rt.get());
  625. window->acquireBackBuffer();
  626. VulkanSwapChain* swapChain;
  627. rt->getCustomAttribute("SC", &swapChain);
  628. mSwapChains.insert(swapChain);
  629. }
  630. rt->getCustomAttribute("FB", &newFB);
  631. }
  632. else
  633. {
  634. newFB = nullptr;
  635. }
  636. if (mFramebuffer == newFB && mRenderTargetDepthReadOnly == readOnlyDepthStencil && mRenderTargetLoadMask == loadMask)
  637. return;
  638. if (isInRenderPass())
  639. endRenderPass();
  640. else
  641. {
  642. // If a clear is queued for previous FB, execute the render pass with no additional instructions
  643. if (mClearMask)
  644. executeClearPass();
  645. }
  646. if(newFB == nullptr)
  647. {
  648. mFramebuffer = nullptr;
  649. mRenderTargetWidth = 0;
  650. mRenderTargetHeight = 0;
  651. mRenderTargetDepthReadOnly = false;
  652. mRenderTargetLoadMask = RT_NONE;
  653. }
  654. else
  655. {
  656. mFramebuffer = newFB;
  657. mRenderTargetWidth = rt->getProperties().getWidth();
  658. mRenderTargetHeight = rt->getProperties().getHeight();
  659. mRenderTargetDepthReadOnly = readOnlyDepthStencil;
  660. mRenderTargetLoadMask = loadMask;
  661. }
  662. // Reset flags that signal image usage
  663. for (auto& entry : mSubresourceInfos)
  664. entry.isFBAttachment = false;
  665. setGpuParams(nullptr);
  666. if(mFramebuffer != nullptr)
  667. registerResource(mFramebuffer, loadMask, VulkanUseFlag::Write);
  668. mGfxPipelineRequiresBind = true;
  669. }
  670. void VulkanCmdBuffer::clearViewport(const Rect2I& area, UINT32 buffers, const Color& color, float depth, UINT16 stencil,
  671. UINT8 targetMask)
  672. {
  673. if (buffers == 0 || mFramebuffer == nullptr)
  674. return;
  675. // Add clear command if currently in render pass
  676. if (isInRenderPass())
  677. {
  678. VkClearAttachment attachments[BS_MAX_MULTIPLE_RENDER_TARGETS + 1];
  679. UINT32 baseLayer = 0;
  680. UINT32 attachmentIdx = 0;
  681. if ((buffers & FBT_COLOR) != 0)
  682. {
  683. UINT32 numColorAttachments = mFramebuffer->getNumColorAttachments();
  684. for (UINT32 i = 0; i < numColorAttachments; i++)
  685. {
  686. const VulkanFramebufferAttachment& attachment = mFramebuffer->getColorAttachment(i);
  687. if (((1 << attachment.index) & targetMask) == 0)
  688. continue;
  689. attachments[attachmentIdx].aspectMask = VK_IMAGE_ASPECT_COLOR_BIT;
  690. attachments[attachmentIdx].colorAttachment = i;
  691. VkClearColorValue& colorValue = attachments[attachmentIdx].clearValue.color;
  692. colorValue.float32[0] = color.r;
  693. colorValue.float32[1] = color.g;
  694. colorValue.float32[2] = color.b;
  695. colorValue.float32[3] = color.a;
  696. UINT32 curBaseLayer = attachment.baseLayer;
  697. if (attachmentIdx == 0)
  698. baseLayer = curBaseLayer;
  699. else
  700. {
  701. if (baseLayer != curBaseLayer)
  702. {
  703. // Note: This could be supported relatively easily: we would need to issue multiple separate
  704. // clear commands for such framebuffers.
  705. LOGERR("Attempting to clear a texture that has multiple multi-layer surfaces with mismatching "
  706. "starting layers. This is currently not supported.");
  707. }
  708. }
  709. attachmentIdx++;
  710. }
  711. }
  712. if ((buffers & FBT_DEPTH) != 0 || (buffers & FBT_STENCIL) != 0)
  713. {
  714. if (mFramebuffer->hasDepthAttachment())
  715. {
  716. attachments[attachmentIdx].aspectMask = 0;
  717. if ((buffers & FBT_DEPTH) != 0)
  718. {
  719. attachments[attachmentIdx].aspectMask |= VK_IMAGE_ASPECT_DEPTH_BIT;
  720. attachments[attachmentIdx].clearValue.depthStencil.depth = depth;
  721. }
  722. if ((buffers & FBT_STENCIL) != 0)
  723. {
  724. attachments[attachmentIdx].aspectMask |= VK_IMAGE_ASPECT_STENCIL_BIT;
  725. attachments[attachmentIdx].clearValue.depthStencil.stencil = stencil;
  726. }
  727. attachments[attachmentIdx].colorAttachment = 0;
  728. UINT32 curBaseLayer = mFramebuffer->getDepthStencilAttachment().baseLayer;
  729. if (attachmentIdx == 0)
  730. baseLayer = curBaseLayer;
  731. else
  732. {
  733. if (baseLayer != curBaseLayer)
  734. {
  735. // Note: This could be supported relatively easily: we would need to issue multiple separate
  736. // clear commands for such framebuffers.
  737. LOGERR("Attempting to clear a texture that has multiple multi-layer surfaces with mismatching "
  738. "starting layers. This is currently not supported.");
  739. }
  740. }
  741. attachmentIdx++;
  742. }
  743. }
  744. UINT32 numAttachments = attachmentIdx;
  745. if (numAttachments == 0)
  746. return;
  747. VkClearRect clearRect;
  748. clearRect.baseArrayLayer = baseLayer;
  749. clearRect.layerCount = mFramebuffer->getNumLayers();
  750. clearRect.rect.offset.x = area.x;
  751. clearRect.rect.offset.y = area.y;
  752. clearRect.rect.extent.width = area.width;
  753. clearRect.rect.extent.height = area.height;
  754. vkCmdClearAttachments(mCmdBuffer, numAttachments, attachments, 1, &clearRect);
  755. }
  756. // Otherwise we use a render pass that performs a clear on begin
  757. else
  758. {
  759. ClearMask clearMask;
  760. std::array<VkClearValue, BS_MAX_MULTIPLE_RENDER_TARGETS + 1> clearValues = mClearValues;
  761. UINT32 numColorAttachments = mFramebuffer->getNumColorAttachments();
  762. if ((buffers & FBT_COLOR) != 0)
  763. {
  764. for (UINT32 i = 0; i < numColorAttachments; i++)
  765. {
  766. const VulkanFramebufferAttachment& attachment = mFramebuffer->getColorAttachment(i);
  767. if (((1 << attachment.index) & targetMask) == 0)
  768. continue;
  769. clearMask |= (ClearMaskBits)(1 << attachment.index);
  770. VkClearColorValue& colorValue = clearValues[i].color;
  771. colorValue.float32[0] = color.r;
  772. colorValue.float32[1] = color.g;
  773. colorValue.float32[2] = color.b;
  774. colorValue.float32[3] = color.a;
  775. }
  776. }
  777. if ((buffers & FBT_DEPTH) != 0 || (buffers & FBT_STENCIL) != 0)
  778. {
  779. if (mFramebuffer->hasDepthAttachment())
  780. {
  781. UINT32 depthAttachmentIdx = numColorAttachments;
  782. if ((buffers & FBT_DEPTH) != 0)
  783. {
  784. clearValues[depthAttachmentIdx].depthStencil.depth = depth;
  785. clearMask |= CLEAR_DEPTH;
  786. }
  787. if ((buffers & FBT_STENCIL) != 0)
  788. {
  789. clearValues[depthAttachmentIdx].depthStencil.stencil = stencil;
  790. clearMask |= CLEAR_STENCIL;
  791. }
  792. }
  793. }
  794. if (!clearMask)
  795. return;
  796. // Some previous clear operation is already queued, execute it first
  797. bool previousClearNeedsToFinish = (mClearMask & clearMask) != CLEAR_NONE;
  798. if(previousClearNeedsToFinish)
  799. executeClearPass();
  800. mClearMask |= clearMask;
  801. mClearValues = clearValues;
  802. mClearArea = area;
  803. }
  804. }
  805. void VulkanCmdBuffer::clearRenderTarget(UINT32 buffers, const Color& color, float depth, UINT16 stencil, UINT8 targetMask)
  806. {
  807. Rect2I area(0, 0, mRenderTargetWidth, mRenderTargetHeight);
  808. clearViewport(area, buffers, color, depth, stencil, targetMask);
  809. }
  810. void VulkanCmdBuffer::clearViewport(UINT32 buffers, const Color& color, float depth, UINT16 stencil, UINT8 targetMask)
  811. {
  812. Rect2I area;
  813. area.x = (UINT32)(mViewport.x * mRenderTargetWidth);
  814. area.y = (UINT32)(mViewport.y * mRenderTargetHeight);
  815. area.width = (UINT32)(mViewport.width * mRenderTargetWidth);
  816. area.height = (UINT32)(mViewport.height * mRenderTargetHeight);
  817. clearViewport(area, buffers, color, depth, stencil, targetMask);
  818. }
  819. void VulkanCmdBuffer::setPipelineState(const SPtr<GraphicsPipelineState>& state)
  820. {
  821. if (mGraphicsPipeline == state)
  822. return;
  823. mGraphicsPipeline = std::static_pointer_cast<VulkanGraphicsPipelineState>(state);
  824. mGfxPipelineRequiresBind = true;
  825. }
  826. void VulkanCmdBuffer::setPipelineState(const SPtr<ComputePipelineState>& state)
  827. {
  828. if (mComputePipeline == state)
  829. return;
  830. mComputePipeline = std::static_pointer_cast<VulkanComputePipelineState>(state);
  831. mCmpPipelineRequiresBind = true;
  832. }
  833. void VulkanCmdBuffer::setGpuParams(const SPtr<GpuParams>& gpuParams)
  834. {
  835. // Note: We keep an internal reference to GPU params even though we shouldn't keep a reference to a core thread
  836. // object. But it should be fine since we expect the resource to be externally synchronized so it should never
  837. // be allowed to go out of scope on a non-core thread anyway.
  838. mBoundParams = std::static_pointer_cast<VulkanGpuParams>(gpuParams);
  839. if (mBoundParams != nullptr)
  840. mBoundParamsDirty = true;
  841. else
  842. {
  843. mNumBoundDescriptorSets = 0;
  844. mBoundParamsDirty = false;
  845. }
  846. mDescriptorSetsBindState = DescriptorSetBindFlag::Graphics | DescriptorSetBindFlag::Compute;
  847. }
  848. void VulkanCmdBuffer::setViewport(const Rect2& area)
  849. {
  850. if (mViewport == area)
  851. return;
  852. mViewport = area;
  853. mViewportRequiresBind = true;
  854. }
  855. void VulkanCmdBuffer::setScissorRect(const Rect2I& value)
  856. {
  857. if (mScissor == value)
  858. return;
  859. mScissor = value;
  860. mScissorRequiresBind = true;
  861. }
  862. void VulkanCmdBuffer::setStencilRef(UINT32 value)
  863. {
  864. if (mStencilRef == value)
  865. return;
  866. mStencilRef = value;
  867. mStencilRefRequiresBind = true;
  868. }
  869. void VulkanCmdBuffer::setDrawOp(DrawOperationType drawOp)
  870. {
  871. if (mDrawOp == drawOp)
  872. return;
  873. mDrawOp = drawOp;
  874. mGfxPipelineRequiresBind = true;
  875. }
  876. void VulkanCmdBuffer::setVertexBuffers(UINT32 index, SPtr<VertexBuffer>* buffers, UINT32 numBuffers)
  877. {
  878. if (numBuffers == 0)
  879. return;
  880. for(UINT32 i = 0; i < numBuffers; i++)
  881. {
  882. VulkanVertexBuffer* vertexBuffer = static_cast<VulkanVertexBuffer*>(buffers[i].get());
  883. if (vertexBuffer != nullptr)
  884. {
  885. VulkanBuffer* resource = vertexBuffer->getResource(mDevice.getIndex());
  886. if (resource != nullptr)
  887. {
  888. mVertexBuffersTemp[i] = resource->getHandle();
  889. registerResource(resource, VulkanUseFlag::Read);
  890. }
  891. else
  892. mVertexBuffersTemp[i] = VK_NULL_HANDLE;
  893. }
  894. else
  895. mVertexBuffersTemp[i] = VK_NULL_HANDLE;
  896. }
  897. vkCmdBindVertexBuffers(mCmdBuffer, index, numBuffers, mVertexBuffersTemp, mVertexBufferOffsetsTemp);
  898. }
  899. void VulkanCmdBuffer::setIndexBuffer(const SPtr<IndexBuffer>& buffer)
  900. {
  901. VulkanIndexBuffer* indexBuffer = static_cast<VulkanIndexBuffer*>(buffer.get());
  902. VkBuffer vkBuffer = VK_NULL_HANDLE;
  903. VkIndexType indexType = VK_INDEX_TYPE_UINT32;
  904. if (indexBuffer != nullptr)
  905. {
  906. VulkanBuffer* resource = indexBuffer->getResource(mDevice.getIndex());
  907. if (resource != nullptr)
  908. {
  909. vkBuffer = resource->getHandle();
  910. indexType = VulkanUtility::getIndexType(buffer->getProperties().getType());
  911. registerResource(resource, VulkanUseFlag::Read);
  912. }
  913. }
  914. vkCmdBindIndexBuffer(mCmdBuffer, vkBuffer, 0, indexType);
  915. }
  916. void VulkanCmdBuffer::setVertexDeclaration(const SPtr<VertexDeclaration>& decl)
  917. {
  918. if (mVertexDecl == decl)
  919. return;
  920. mVertexDecl = decl;
  921. mGfxPipelineRequiresBind = true;
  922. }
  923. bool VulkanCmdBuffer::isReadyForRender()
  924. {
  925. if (mGraphicsPipeline == nullptr)
  926. return false;
  927. SPtr<VertexDeclaration> inputDecl = mGraphicsPipeline->getInputDeclaration();
  928. if (inputDecl == nullptr)
  929. return false;
  930. return mFramebuffer != nullptr && mVertexDecl != nullptr;
  931. }
  932. bool VulkanCmdBuffer::bindGraphicsPipeline()
  933. {
  934. SPtr<VertexDeclaration> inputDecl = mGraphicsPipeline->getInputDeclaration();
  935. SPtr<VulkanVertexInput> vertexInput = VulkanVertexInputManager::instance().getVertexInfo(mVertexDecl, inputDecl);
  936. VulkanPipeline* pipeline = mGraphicsPipeline->getPipeline(mDevice.getIndex(), mFramebuffer,
  937. mRenderTargetDepthReadOnly, mDrawOp, vertexInput);
  938. if (pipeline == nullptr)
  939. return false;
  940. // Check that pipeline matches the read-only state of any framebuffer attachments
  941. UINT32 numColorAttachments = mFramebuffer->getNumColorAttachments();
  942. for (UINT32 i = 0; i < numColorAttachments; i++)
  943. {
  944. const VulkanFramebufferAttachment& fbAttachment = mFramebuffer->getColorAttachment(i);
  945. ImageSubresourceInfo& subresourceInfo = findSubresourceInfo(fbAttachment.image, fbAttachment.surface.arraySlice,
  946. fbAttachment.surface.mipLevel);
  947. if (subresourceInfo.isShaderInput && !pipeline->isColorReadOnly(i))
  948. {
  949. LOGWRN("Framebuffer attachment also used as a shader input, but color writes aren't disabled. This will"
  950. " result in undefined behavior.");
  951. }
  952. }
  953. if (mFramebuffer->hasDepthAttachment())
  954. {
  955. const VulkanFramebufferAttachment& fbAttachment = mFramebuffer->getDepthStencilAttachment();
  956. ImageSubresourceInfo& subresourceInfo = findSubresourceInfo(fbAttachment.image, fbAttachment.surface.arraySlice,
  957. fbAttachment.surface.mipLevel);
  958. if (subresourceInfo.isShaderInput && !pipeline->isDepthStencilReadOnly())
  959. {
  960. LOGWRN("Framebuffer attachment also used as a shader input, but depth/stencil writes aren't disabled. "
  961. "This will result in undefined behavior.");
  962. }
  963. }
  964. mGraphicsPipeline->registerPipelineResources(this);
  965. registerResource(pipeline, VulkanUseFlag::Read);
  966. vkCmdBindPipeline(mCmdBuffer, VK_PIPELINE_BIND_POINT_GRAPHICS, pipeline->getHandle());
  967. bindDynamicStates(true);
  968. mGfxPipelineRequiresBind = false;
  969. return true;
  970. }
  971. void VulkanCmdBuffer::bindDynamicStates(bool forceAll)
  972. {
  973. if (mViewportRequiresBind || forceAll)
  974. {
  975. VkViewport viewport;
  976. viewport.x = mViewport.x * mRenderTargetWidth;
  977. viewport.y = mViewport.y * mRenderTargetHeight;
  978. viewport.width = mViewport.width * mRenderTargetWidth;
  979. viewport.height = mViewport.height * mRenderTargetHeight;
  980. viewport.minDepth = 0.0f;
  981. viewport.maxDepth = 1.0f;
  982. vkCmdSetViewport(mCmdBuffer, 0, 1, &viewport);
  983. mViewportRequiresBind = false;
  984. }
  985. if(mStencilRefRequiresBind || forceAll)
  986. {
  987. vkCmdSetStencilReference(mCmdBuffer, VK_STENCIL_FRONT_AND_BACK, mStencilRef);
  988. mStencilRefRequiresBind = false;
  989. }
  990. if(mScissorRequiresBind || forceAll)
  991. {
  992. VkRect2D scissorRect;
  993. if(mGraphicsPipeline->isScissorEnabled())
  994. {
  995. scissorRect.offset.x = mScissor.x;
  996. scissorRect.offset.y = mScissor.y;
  997. scissorRect.extent.width = mScissor.width;
  998. scissorRect.extent.height = mScissor.height;
  999. }
  1000. else
  1001. {
  1002. scissorRect.offset.x = 0;
  1003. scissorRect.offset.y = 0;
  1004. scissorRect.extent.width = mRenderTargetWidth;
  1005. scissorRect.extent.height = mRenderTargetHeight;
  1006. }
  1007. vkCmdSetScissor(mCmdBuffer, 0, 1, &scissorRect);
  1008. mScissorRequiresBind = false;
  1009. }
  1010. }
  1011. void VulkanCmdBuffer::bindGpuParams()
  1012. {
  1013. if (mBoundParamsDirty)
  1014. {
  1015. if (mBoundParams != nullptr)
  1016. {
  1017. mNumBoundDescriptorSets = mBoundParams->getNumSets();
  1018. mBoundParams->prepareForBind(*this, mDescriptorSetsTemp);
  1019. }
  1020. else
  1021. mNumBoundDescriptorSets = 0;
  1022. mBoundParamsDirty = false;
  1023. }
  1024. else
  1025. {
  1026. mNumBoundDescriptorSets = 0;
  1027. }
  1028. }
  1029. void VulkanCmdBuffer::executeLayoutTransitions()
  1030. {
  1031. auto createLayoutTransitionBarrier = [&](VulkanImage* image, ImageInfo& imageInfo)
  1032. {
  1033. ImageSubresourceInfo* subresourceInfos = &mSubresourceInfos[imageInfo.subresourceInfoIdx];
  1034. for (UINT32 i = 0; i < imageInfo.numSubresourceInfos; i++)
  1035. {
  1036. ImageSubresourceInfo& subresourceInfo = subresourceInfos[i];
  1037. if (!subresourceInfo.hasTransitioned || subresourceInfo.currentLayout == subresourceInfo.requiredLayout)
  1038. continue;
  1039. mLayoutTransitionBarriersTemp.push_back(VkImageMemoryBarrier());
  1040. VkImageMemoryBarrier& barrier = mLayoutTransitionBarriersTemp.back();
  1041. barrier.sType = VK_STRUCTURE_TYPE_IMAGE_MEMORY_BARRIER;
  1042. barrier.pNext = nullptr;
  1043. barrier.srcAccessMask = image->getAccessFlags(subresourceInfo.currentLayout);
  1044. barrier.dstAccessMask = image->getAccessFlags(subresourceInfo.requiredLayout, subresourceInfo.isReadOnly);
  1045. barrier.srcQueueFamilyIndex = VK_QUEUE_FAMILY_IGNORED;
  1046. barrier.dstQueueFamilyIndex = VK_QUEUE_FAMILY_IGNORED;
  1047. barrier.oldLayout = subresourceInfo.currentLayout;
  1048. barrier.newLayout = subresourceInfo.requiredLayout;
  1049. barrier.image = image->getHandle();
  1050. barrier.subresourceRange = subresourceInfo.range;
  1051. subresourceInfo.currentLayout = subresourceInfo.requiredLayout;
  1052. subresourceInfo.isReadOnly = true;
  1053. subresourceInfo.hasTransitioned = true;
  1054. }
  1055. };
  1056. // Note: These layout transitions will contain transitions for offscreen framebuffer attachments (while they
  1057. // transition to shader read-only layout). This can be avoided, since they're immediately used by the render pass
  1058. // as color attachments, making the layout change redundant.
  1059. for (auto& entry : mQueuedLayoutTransitions)
  1060. {
  1061. UINT32 imageInfoIdx = entry.second;
  1062. ImageInfo& imageInfo = mImageInfos[imageInfoIdx];
  1063. createLayoutTransitionBarrier(entry.first, imageInfo);
  1064. }
  1065. vkCmdPipelineBarrier(mCmdBuffer,
  1066. VK_PIPELINE_STAGE_TOP_OF_PIPE_BIT, // Note: VK_PIPELINE_STAGE_BOTTOM_OF_PIPE_BIT might be more correct here, according to the spec
  1067. VK_PIPELINE_STAGE_TOP_OF_PIPE_BIT,
  1068. 0, 0, nullptr,
  1069. 0, nullptr,
  1070. (UINT32)mLayoutTransitionBarriersTemp.size(), mLayoutTransitionBarriersTemp.data());
  1071. mQueuedLayoutTransitions.clear();
  1072. mLayoutTransitionBarriersTemp.clear();
  1073. }
  1074. void VulkanCmdBuffer::updateFinalLayouts()
  1075. {
  1076. if (mFramebuffer == nullptr)
  1077. return;
  1078. UINT32 numColorAttachments = mFramebuffer->getNumColorAttachments();
  1079. for (UINT32 i = 0; i < numColorAttachments; i++)
  1080. {
  1081. const VulkanFramebufferAttachment& fbAttachment = mFramebuffer->getColorAttachment(i);
  1082. ImageSubresourceInfo& subresourceInfo = findSubresourceInfo(fbAttachment.image, fbAttachment.surface.arraySlice,
  1083. fbAttachment.surface.mipLevel);
  1084. subresourceInfo.currentLayout = subresourceInfo.finalLayout;
  1085. subresourceInfo.requiredLayout = subresourceInfo.finalLayout;
  1086. subresourceInfo.hasTransitioned = true;
  1087. }
  1088. if (mFramebuffer->hasDepthAttachment())
  1089. {
  1090. const VulkanFramebufferAttachment& fbAttachment = mFramebuffer->getDepthStencilAttachment();
  1091. ImageSubresourceInfo& subresourceInfo = findSubresourceInfo(fbAttachment.image, fbAttachment.surface.arraySlice,
  1092. fbAttachment.surface.mipLevel);
  1093. subresourceInfo.currentLayout = subresourceInfo.finalLayout;
  1094. subresourceInfo.requiredLayout = subresourceInfo.finalLayout;
  1095. subresourceInfo.hasTransitioned = true;
  1096. }
  1097. }
  1098. void VulkanCmdBuffer::executeClearPass()
  1099. {
  1100. assert(mState == State::Recording);
  1101. executeLayoutTransitions();
  1102. VkRenderPassBeginInfo renderPassBeginInfo;
  1103. renderPassBeginInfo.sType = VK_STRUCTURE_TYPE_RENDER_PASS_BEGIN_INFO;
  1104. renderPassBeginInfo.pNext = nullptr;
  1105. renderPassBeginInfo.framebuffer = mFramebuffer->getFramebuffer(RT_NONE, RT_NONE, mClearMask);
  1106. renderPassBeginInfo.renderPass = mFramebuffer->getRenderPass(RT_NONE, RT_NONE, mClearMask);
  1107. renderPassBeginInfo.renderArea.offset.x = mClearArea.x;
  1108. renderPassBeginInfo.renderArea.offset.y = mClearArea.y;
  1109. renderPassBeginInfo.renderArea.extent.width = mClearArea.width;
  1110. renderPassBeginInfo.renderArea.extent.height = mClearArea.height;
  1111. renderPassBeginInfo.clearValueCount = mFramebuffer->getNumClearEntries(mClearMask);
  1112. renderPassBeginInfo.pClearValues = mClearValues.data();
  1113. vkCmdBeginRenderPass(mCmdBuffer, &renderPassBeginInfo, VK_SUBPASS_CONTENTS_INLINE);
  1114. vkCmdEndRenderPass(mCmdBuffer);
  1115. updateFinalLayouts();
  1116. mClearMask = CLEAR_NONE;
  1117. }
  1118. void VulkanCmdBuffer::draw(UINT32 vertexOffset, UINT32 vertexCount, UINT32 instanceCount)
  1119. {
  1120. if (!isReadyForRender())
  1121. return;
  1122. // Note: Must begin render pass before binding GPU params as some GPU param related data gets cleared on begin, and
  1123. // we don't want to clear the currently used one
  1124. if (!isInRenderPass())
  1125. beginRenderPass();
  1126. bindGpuParams();
  1127. if (mGfxPipelineRequiresBind)
  1128. {
  1129. if (!bindGraphicsPipeline())
  1130. return;
  1131. }
  1132. else
  1133. bindDynamicStates(false);
  1134. if (mDescriptorSetsBindState.isSet(DescriptorSetBindFlag::Graphics))
  1135. {
  1136. if (mNumBoundDescriptorSets > 0)
  1137. {
  1138. UINT32 deviceIdx = mDevice.getIndex();
  1139. VkPipelineLayout pipelineLayout = mGraphicsPipeline->getPipelineLayout(deviceIdx);
  1140. vkCmdBindDescriptorSets(mCmdBuffer, VK_PIPELINE_BIND_POINT_GRAPHICS, pipelineLayout, 0,
  1141. mNumBoundDescriptorSets, mDescriptorSetsTemp, 0, nullptr);
  1142. }
  1143. mDescriptorSetsBindState.unset(DescriptorSetBindFlag::Graphics);
  1144. }
  1145. vkCmdDraw(mCmdBuffer, vertexCount, instanceCount, vertexOffset, 0);
  1146. }
  1147. void VulkanCmdBuffer::drawIndexed(UINT32 startIndex, UINT32 indexCount, UINT32 vertexOffset, UINT32 instanceCount)
  1148. {
  1149. if (!isReadyForRender())
  1150. return;
  1151. // Note: Must begin render pass before binding GPU params as some GPU param related data gets cleared on begin, and
  1152. // we don't want to clear the currently used one
  1153. if (!isInRenderPass())
  1154. beginRenderPass();
  1155. bindGpuParams();
  1156. if (mGfxPipelineRequiresBind)
  1157. {
  1158. if (!bindGraphicsPipeline())
  1159. return;
  1160. }
  1161. else
  1162. bindDynamicStates(false);
  1163. if (mDescriptorSetsBindState.isSet(DescriptorSetBindFlag::Graphics))
  1164. {
  1165. if (mNumBoundDescriptorSets > 0)
  1166. {
  1167. UINT32 deviceIdx = mDevice.getIndex();
  1168. VkPipelineLayout pipelineLayout = mGraphicsPipeline->getPipelineLayout(deviceIdx);
  1169. vkCmdBindDescriptorSets(mCmdBuffer, VK_PIPELINE_BIND_POINT_GRAPHICS, pipelineLayout, 0,
  1170. mNumBoundDescriptorSets, mDescriptorSetsTemp, 0, nullptr);
  1171. }
  1172. mDescriptorSetsBindState.unset(DescriptorSetBindFlag::Graphics);
  1173. }
  1174. vkCmdDrawIndexed(mCmdBuffer, indexCount, instanceCount, startIndex, vertexOffset, 0);
  1175. }
  1176. void VulkanCmdBuffer::dispatch(UINT32 numGroupsX, UINT32 numGroupsY, UINT32 numGroupsZ)
  1177. {
  1178. if (mComputePipeline == nullptr)
  1179. return;
  1180. bindGpuParams();
  1181. if (isInRenderPass())
  1182. endRenderPass();
  1183. UINT32 deviceIdx = mDevice.getIndex();
  1184. if(mCmpPipelineRequiresBind)
  1185. {
  1186. VulkanPipeline* pipeline = mComputePipeline->getPipeline(deviceIdx);
  1187. if (pipeline == nullptr)
  1188. return;
  1189. registerResource(pipeline, VulkanUseFlag::Read);
  1190. mComputePipeline->registerPipelineResources(this);
  1191. vkCmdBindPipeline(mCmdBuffer, VK_PIPELINE_BIND_POINT_COMPUTE, pipeline->getHandle());
  1192. mCmpPipelineRequiresBind = false;
  1193. }
  1194. if(mDescriptorSetsBindState.isSet(DescriptorSetBindFlag::Compute))
  1195. {
  1196. if (mNumBoundDescriptorSets > 0)
  1197. {
  1198. VkPipelineLayout pipelineLayout = mComputePipeline->getPipelineLayout(deviceIdx);
  1199. vkCmdBindDescriptorSets(mCmdBuffer, VK_PIPELINE_BIND_POINT_COMPUTE, pipelineLayout, 0,
  1200. mNumBoundDescriptorSets, mDescriptorSetsTemp, 0, nullptr);
  1201. }
  1202. mDescriptorSetsBindState.unset(DescriptorSetBindFlag::Compute);
  1203. }
  1204. vkCmdDispatch(mCmdBuffer, numGroupsX, numGroupsY, numGroupsZ);
  1205. }
  1206. void VulkanCmdBuffer::setEvent(VulkanEvent* event)
  1207. {
  1208. if(isInRenderPass())
  1209. mQueuedEvents.push_back(event);
  1210. else
  1211. vkCmdSetEvent(mCmdBuffer, event->getHandle(), VK_PIPELINE_STAGE_ALL_COMMANDS_BIT);
  1212. }
  1213. void VulkanCmdBuffer::resetQuery(VulkanQuery* query)
  1214. {
  1215. if (isInRenderPass())
  1216. mQueuedQueryResets.push_back(query);
  1217. else
  1218. query->reset(mCmdBuffer);
  1219. }
  1220. void VulkanCmdBuffer::memoryBarrier(VkBuffer buffer, VkAccessFlags srcAccessFlags, VkAccessFlags dstAccessFlags,
  1221. VkPipelineStageFlags srcStage, VkPipelineStageFlags dstStage)
  1222. {
  1223. VkBufferMemoryBarrier barrier;
  1224. barrier.sType = VK_STRUCTURE_TYPE_BUFFER_MEMORY_BARRIER;
  1225. barrier.pNext = nullptr;
  1226. barrier.srcAccessMask = srcAccessFlags;
  1227. barrier.dstAccessMask = dstAccessFlags;
  1228. barrier.srcQueueFamilyIndex = VK_QUEUE_FAMILY_IGNORED;
  1229. barrier.dstQueueFamilyIndex = VK_QUEUE_FAMILY_IGNORED;
  1230. barrier.buffer = buffer;
  1231. barrier.offset = 0;
  1232. barrier.size = VK_WHOLE_SIZE;
  1233. vkCmdPipelineBarrier(getHandle(),
  1234. srcStage,
  1235. dstStage,
  1236. 0, 0, nullptr,
  1237. 1, &barrier,
  1238. 0, nullptr);
  1239. }
  1240. void VulkanCmdBuffer::memoryBarrier(VkImage image, VkAccessFlags srcAccessFlags, VkAccessFlags dstAccessFlags,
  1241. VkPipelineStageFlags srcStage, VkPipelineStageFlags dstStage, VkImageLayout layout,
  1242. const VkImageSubresourceRange& range)
  1243. {
  1244. VkImageMemoryBarrier barrier;
  1245. barrier.sType = VK_STRUCTURE_TYPE_IMAGE_MEMORY_BARRIER;
  1246. barrier.pNext = nullptr;
  1247. barrier.srcAccessMask = srcAccessFlags;
  1248. barrier.dstAccessMask = dstAccessFlags;
  1249. barrier.srcQueueFamilyIndex = VK_QUEUE_FAMILY_IGNORED;
  1250. barrier.dstQueueFamilyIndex = VK_QUEUE_FAMILY_IGNORED;
  1251. barrier.image = image;
  1252. barrier.subresourceRange = range;
  1253. barrier.oldLayout = layout;
  1254. barrier.newLayout = layout;
  1255. vkCmdPipelineBarrier(getHandle(),
  1256. srcStage,
  1257. dstStage,
  1258. 0, 0, nullptr,
  1259. 0, nullptr,
  1260. 1, &barrier);
  1261. }
  1262. void VulkanCmdBuffer::setLayout(VkImage image, VkAccessFlags srcAccessFlags, VkAccessFlags dstAccessFlags,
  1263. VkImageLayout oldLayout, VkImageLayout newLayout, const VkImageSubresourceRange& range)
  1264. {
  1265. VkImageMemoryBarrier barrier;
  1266. barrier.sType = VK_STRUCTURE_TYPE_IMAGE_MEMORY_BARRIER;
  1267. barrier.pNext = nullptr;
  1268. barrier.srcAccessMask = srcAccessFlags;
  1269. barrier.dstAccessMask = dstAccessFlags;
  1270. barrier.srcQueueFamilyIndex = VK_QUEUE_FAMILY_IGNORED;
  1271. barrier.dstQueueFamilyIndex = VK_QUEUE_FAMILY_IGNORED;
  1272. barrier.oldLayout = oldLayout;
  1273. barrier.newLayout = newLayout;
  1274. barrier.image = image;
  1275. barrier.subresourceRange = range;
  1276. vkCmdPipelineBarrier(getHandle(),
  1277. VK_PIPELINE_STAGE_TOP_OF_PIPE_BIT,
  1278. VK_PIPELINE_STAGE_TOP_OF_PIPE_BIT,
  1279. 0, 0, nullptr,
  1280. 0, nullptr,
  1281. 1, &barrier);
  1282. }
  1283. void VulkanCmdBuffer::registerResource(VulkanResource* res, VulkanUseFlags flags)
  1284. {
  1285. auto insertResult = mResources.insert(std::make_pair(res, ResourceUseHandle()));
  1286. if(insertResult.second) // New element
  1287. {
  1288. ResourceUseHandle& useHandle = insertResult.first->second;
  1289. useHandle.used = false;
  1290. useHandle.flags = flags;
  1291. res->notifyBound();
  1292. }
  1293. else // Existing element
  1294. {
  1295. ResourceUseHandle& useHandle = insertResult.first->second;
  1296. assert(!useHandle.used);
  1297. useHandle.flags |= flags;
  1298. }
  1299. }
  1300. void VulkanCmdBuffer::registerResource(VulkanImage* res, const VkImageSubresourceRange& range, VulkanUseFlags flags)
  1301. {
  1302. VkImageLayout layout = res->getOptimalLayout();
  1303. registerResource(res, range, VK_IMAGE_LAYOUT_UNDEFINED, layout, flags, false);
  1304. }
  1305. void VulkanCmdBuffer::registerResource(VulkanImage* res, const VkImageSubresourceRange& range, VkImageLayout newLayout,
  1306. VkImageLayout finalLayout, VulkanUseFlags flags, bool isFBAttachment)
  1307. {
  1308. // Check if we're binding for shader use, or as a color attachment, or just for transfer purposes
  1309. bool isShaderBind = !isFBAttachment && newLayout != VK_IMAGE_LAYOUT_UNDEFINED;
  1310. // If binding it for write in a shader (not as color attachment or transfer op), we will need to issue a memory
  1311. // barrier if the image gets used again during this render pass, so remember this information.
  1312. bool needsBarrier = isShaderBind && flags.isSet(VulkanUseFlag::Write);
  1313. UINT32 nextImageInfoIdx = (UINT32)mImageInfos.size();
  1314. auto registerSubresourceInfo = [&](const VkImageSubresourceRange& subresourceRange)
  1315. {
  1316. mSubresourceInfos.push_back(ImageSubresourceInfo());
  1317. ImageSubresourceInfo& subresourceInfo = mSubresourceInfos.back();
  1318. subresourceInfo.currentLayout = newLayout;
  1319. subresourceInfo.initialLayout = newLayout;
  1320. subresourceInfo.requiredLayout = newLayout;
  1321. subresourceInfo.finalLayout = finalLayout;
  1322. subresourceInfo.range = subresourceRange;
  1323. subresourceInfo.isFBAttachment = isFBAttachment;
  1324. subresourceInfo.isShaderInput = !isFBAttachment;
  1325. subresourceInfo.hasTransitioned = false;
  1326. subresourceInfo.isReadOnly = !flags.isSet(VulkanUseFlag::Write);
  1327. subresourceInfo.isInitialReadOnly = subresourceInfo.isReadOnly;
  1328. subresourceInfo.needsBarrier = needsBarrier;
  1329. };
  1330. auto insertResult = mImages.insert(std::make_pair(res, nextImageInfoIdx));
  1331. if (insertResult.second) // New element
  1332. {
  1333. UINT32 imageInfoIdx = insertResult.first->second;
  1334. mImageInfos.push_back(ImageInfo());
  1335. ImageInfo& imageInfo = mImageInfos[imageInfoIdx];
  1336. imageInfo.subresourceInfoIdx = (UINT32)mSubresourceInfos.size();
  1337. imageInfo.numSubresourceInfos = 1;
  1338. imageInfo.useHandle.used = false;
  1339. imageInfo.useHandle.flags = flags;
  1340. registerSubresourceInfo(range);
  1341. res->notifyBound();
  1342. }
  1343. else // Existing element
  1344. {
  1345. UINT32 imageInfoIdx = insertResult.first->second;
  1346. ImageInfo& imageInfo = mImageInfos[imageInfoIdx];
  1347. assert(!imageInfo.useHandle.used);
  1348. imageInfo.useHandle.flags |= flags;
  1349. // See if there is an overlap between existing ranges and the new range. And if so break them up accordingly.
  1350. //// First test for the simplest and most common case (same range or no overlap) to avoid more complex
  1351. //// computations.
  1352. ImageSubresourceInfo* subresources = &mSubresourceInfos[imageInfo.subresourceInfoIdx];
  1353. bool foundRange = false;
  1354. for(UINT32 i = 0; i < imageInfo.numSubresourceInfos; i++)
  1355. {
  1356. if(VulkanUtility::rangeOverlaps(subresources[i].range, range))
  1357. {
  1358. if (subresources[i].range.layerCount == range.layerCount &&
  1359. subresources[i].range.levelCount == range.levelCount &&
  1360. subresources[i].range.baseArrayLayer == range.baseArrayLayer &&
  1361. subresources[i].range.baseMipLevel == range.baseMipLevel)
  1362. {
  1363. // Just update existing range
  1364. bool requiresReadOnlyFB = updateSubresourceInfo(res, imageInfoIdx, subresources[i], newLayout,
  1365. finalLayout, flags, isFBAttachment);
  1366. // If we need to switch frame-buffers, end current render pass
  1367. if (requiresReadOnlyFB && isInRenderPass())
  1368. endRenderPass();
  1369. foundRange = true;
  1370. break;
  1371. }
  1372. break;
  1373. }
  1374. }
  1375. //// We'll need to update subresource ranges or add new ones. The hope is that this code is trigger VERY rarely
  1376. //// (for just a few specific textures per frame).
  1377. if (!foundRange)
  1378. {
  1379. std::array<VkImageSubresourceRange, 5> tempCutRanges;
  1380. bs_frame_mark();
  1381. {
  1382. // We orphan previously allocated memory (we reset it after submit() anyway)
  1383. UINT32 newSubresourceIdx = (UINT32)mSubresourceInfos.size();
  1384. FrameVector<UINT32> cutOverlappingRanges;
  1385. for (UINT32 i = 0; i < imageInfo.numSubresourceInfos; i++)
  1386. {
  1387. UINT32 subresourceIdx = imageInfo.subresourceInfoIdx + i;
  1388. ImageSubresourceInfo& subresource = mSubresourceInfos[subresourceIdx];
  1389. if (!VulkanUtility::rangeOverlaps(subresource.range, range))
  1390. {
  1391. // Just copy as is
  1392. mSubresourceInfos.push_back(subresource);
  1393. }
  1394. else // Need to cut
  1395. {
  1396. UINT32 numCutRanges;
  1397. VulkanUtility::cutRange(subresource.range, range, tempCutRanges, numCutRanges);
  1398. for(UINT32 j = 0; j < numCutRanges; j++)
  1399. {
  1400. // Create a copy of the original subresource with the new range
  1401. ImageSubresourceInfo newInfo = subresource;
  1402. newInfo.range = tempCutRanges[j];
  1403. if(VulkanUtility::rangeOverlaps(tempCutRanges[j], range))
  1404. {
  1405. // Update overlapping sub-resource range with new data from this range
  1406. updateSubresourceInfo(res, imageInfoIdx, newInfo, newLayout, finalLayout, flags,
  1407. isFBAttachment);
  1408. // Keep track of the overlapping ranges for later
  1409. cutOverlappingRanges.push_back((UINT32)mSubresourceInfos.size());
  1410. }
  1411. mSubresourceInfos.push_back(newInfo);
  1412. }
  1413. }
  1414. }
  1415. // Our range doesn't overlap with any existing ranges, so just add it
  1416. if(cutOverlappingRanges.size() == 0)
  1417. {
  1418. registerSubresourceInfo(range);
  1419. }
  1420. else // Search if overlapping ranges fully cover the requested range, and insert non-covered regions
  1421. {
  1422. FrameQueue<VkImageSubresourceRange> sourceRanges;
  1423. sourceRanges.push(range);
  1424. for(auto& entry : cutOverlappingRanges)
  1425. {
  1426. VkImageSubresourceRange& overlappingRange = mSubresourceInfos[entry].range;
  1427. UINT32 numSourceRanges = (UINT32)sourceRanges.size();
  1428. for(UINT32 i = 0; i < numSourceRanges; i++)
  1429. {
  1430. VkImageSubresourceRange sourceRange = sourceRanges.front();
  1431. sourceRanges.pop();
  1432. UINT32 numCutRanges;
  1433. VulkanUtility::cutRange(sourceRange, overlappingRange, tempCutRanges, numCutRanges);
  1434. for(UINT32 j = 0; j < numCutRanges; j++)
  1435. {
  1436. // We only care about ranges outside of the ones we already covered
  1437. if(!VulkanUtility::rangeOverlaps(tempCutRanges[j], overlappingRange))
  1438. sourceRanges.push(tempCutRanges[j]);
  1439. }
  1440. }
  1441. }
  1442. // Any remaining range hasn't been covered yet
  1443. while(!sourceRanges.empty())
  1444. {
  1445. registerSubresourceInfo(sourceRanges.front());
  1446. sourceRanges.pop();
  1447. }
  1448. }
  1449. imageInfo.subresourceInfoIdx = newSubresourceIdx;
  1450. imageInfo.numSubresourceInfos = (UINT32)mSubresourceInfos.size() - newSubresourceIdx;
  1451. }
  1452. bs_frame_clear();
  1453. }
  1454. }
  1455. // Register any sub-resources
  1456. for(UINT32 i = 0; i < range.layerCount; i++)
  1457. {
  1458. for(UINT32 j = 0; j < range.levelCount; j++)
  1459. {
  1460. UINT32 layer = range.baseArrayLayer + i;
  1461. UINT32 mipLevel = range.baseMipLevel + j;
  1462. registerResource(res->getSubresource(layer, mipLevel), flags);
  1463. }
  1464. }
  1465. }
  1466. void VulkanCmdBuffer::registerResource(VulkanBuffer* res, VkAccessFlags accessFlags, VulkanUseFlags flags)
  1467. {
  1468. bool isShaderWrite = (accessFlags & VK_ACCESS_SHADER_WRITE_BIT) != 0;
  1469. auto insertResult = mBuffers.insert(std::make_pair(res, BufferInfo()));
  1470. if (insertResult.second) // New element
  1471. {
  1472. BufferInfo& bufferInfo = insertResult.first->second;
  1473. bufferInfo.accessFlags = accessFlags;
  1474. bufferInfo.useHandle.used = false;
  1475. bufferInfo.useHandle.flags = flags;
  1476. // Any writes done on storage buffers will need explicit memory barriers (if read during the same pass) so
  1477. // we remember this, in case this buffers gets used later in this pass.
  1478. bufferInfo.needsBarrier = isShaderWrite;
  1479. res->notifyBound();
  1480. }
  1481. else // Existing element
  1482. {
  1483. BufferInfo& bufferInfo = insertResult.first->second;
  1484. assert(!bufferInfo.useHandle.used);
  1485. bufferInfo.useHandle.flags |= flags;
  1486. bufferInfo.accessFlags |= accessFlags;
  1487. // If the buffer was written to previously in this pass, and is now being used by a shader we need to issue
  1488. // a barrier to make those writes visible.
  1489. bool isShaderRead = (accessFlags & VK_ACCESS_SHADER_READ_BIT) != 0;
  1490. if(bufferInfo.needsBarrier && (isShaderRead || isShaderWrite))
  1491. {
  1492. VkPipelineStageFlags stages =
  1493. VK_PIPELINE_STAGE_VERTEX_SHADER_BIT |
  1494. VK_PIPELINE_STAGE_TESSELLATION_CONTROL_SHADER_BIT |
  1495. VK_PIPELINE_STAGE_TESSELLATION_EVALUATION_SHADER_BIT |
  1496. VK_PIPELINE_STAGE_GEOMETRY_SHADER_BIT |
  1497. VK_PIPELINE_STAGE_FRAGMENT_SHADER_BIT |
  1498. VK_PIPELINE_STAGE_COMPUTE_SHADER_BIT;
  1499. VkBuffer buffer = res->getHandle();
  1500. memoryBarrier(buffer, VK_ACCESS_SHADER_WRITE_BIT, accessFlags, stages, stages);
  1501. bufferInfo.needsBarrier = isShaderWrite;
  1502. }
  1503. }
  1504. }
  1505. void VulkanCmdBuffer::registerResource(VulkanFramebuffer* res, RenderSurfaceMask loadMask, VulkanUseFlags flags)
  1506. {
  1507. auto insertResult = mResources.insert(std::make_pair(res, ResourceUseHandle()));
  1508. if (insertResult.second) // New element
  1509. {
  1510. ResourceUseHandle& useHandle = insertResult.first->second;
  1511. useHandle.used = false;
  1512. useHandle.flags = flags;
  1513. res->notifyBound();
  1514. }
  1515. else // Existing element
  1516. {
  1517. ResourceUseHandle& useHandle = insertResult.first->second;
  1518. assert(!useHandle.used);
  1519. useHandle.flags |= flags;
  1520. }
  1521. // Register any sub-resources
  1522. UINT32 numColorAttachments = res->getNumColorAttachments();
  1523. for (UINT32 i = 0; i < numColorAttachments; i++)
  1524. {
  1525. const VulkanFramebufferAttachment& attachment = res->getColorAttachment(i);
  1526. // If image is being loaded, we need to transfer it to correct layout, otherwise it doesn't matter
  1527. VkImageLayout layout;
  1528. if (loadMask.isSet((RenderSurfaceMaskBits)(1 << i)))
  1529. layout = VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL;
  1530. else
  1531. layout = VK_IMAGE_LAYOUT_UNDEFINED;
  1532. VkImageSubresourceRange range = attachment.image->getRange(attachment.surface);
  1533. registerResource(attachment.image, range, layout, attachment.finalLayout, VulkanUseFlag::Write, true);
  1534. }
  1535. if(res->hasDepthAttachment())
  1536. {
  1537. const VulkanFramebufferAttachment& attachment = res->getDepthStencilAttachment();
  1538. // If image is being loaded, we need to transfer it to correct layout, otherwise it doesn't matter
  1539. VkImageLayout layout;
  1540. if (loadMask.isSet(RT_DEPTH))
  1541. layout = VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL;
  1542. else
  1543. layout = VK_IMAGE_LAYOUT_UNDEFINED;
  1544. VkImageSubresourceRange range = attachment.image->getRange(attachment.surface);
  1545. registerResource(attachment.image, range, layout, attachment.finalLayout, VulkanUseFlag::Write, true);
  1546. }
  1547. }
  1548. bool VulkanCmdBuffer::updateSubresourceInfo(VulkanImage* image, UINT32 imageInfoIdx,
  1549. ImageSubresourceInfo& subresourceInfo, VkImageLayout newLayout, VkImageLayout finalLayout, VulkanUseFlags flags,
  1550. bool isFBAttachment)
  1551. {
  1552. subresourceInfo.isReadOnly &= !flags.isSet(VulkanUseFlag::Write);
  1553. // New layout is valid, check for transitions (UNDEFINED signifies the caller doesn't want a layout transition)
  1554. if (newLayout != VK_IMAGE_LAYOUT_UNDEFINED)
  1555. {
  1556. // If layout transition was requested by framebuffer bind, respect it because render-pass will only accept a
  1557. // specific layout (in certain cases), and we have no choice.
  1558. // In the case when a FB attachment is also bound for shader reads, this will override the layout required for
  1559. // shader read (GENERAL or DEPTH_READ_ONLY), but that is fine because those transitions are handled
  1560. // automatically by render-pass layout transitions.
  1561. // Any other texture (non FB attachment) will only even be bound in a single layout and we can keep the one it
  1562. // was originally registered with.
  1563. if (isFBAttachment)
  1564. subresourceInfo.requiredLayout = newLayout;
  1565. else if (!subresourceInfo.isFBAttachment) // Layout transition is not being done on a FB image
  1566. {
  1567. // Check if the image had a layout previously assigned, and if so check if multiple different layouts
  1568. // were requested. In that case we wish to transfer the image to GENERAL layout.
  1569. bool firstUseInRenderPass = !subresourceInfo.isShaderInput && !subresourceInfo.isFBAttachment;
  1570. if (firstUseInRenderPass || subresourceInfo.requiredLayout == VK_IMAGE_LAYOUT_UNDEFINED)
  1571. subresourceInfo.requiredLayout = newLayout;
  1572. else if (subresourceInfo.requiredLayout != newLayout)
  1573. subresourceInfo.requiredLayout = VK_IMAGE_LAYOUT_GENERAL;
  1574. }
  1575. }
  1576. // If attached to FB, then the final layout is set by the FB (provided as layout param here), otherwise its
  1577. // the same as required layout
  1578. if (!isFBAttachment && !subresourceInfo.isFBAttachment)
  1579. subresourceInfo.finalLayout = subresourceInfo.requiredLayout;
  1580. else
  1581. {
  1582. if (isFBAttachment)
  1583. subresourceInfo.finalLayout = finalLayout;
  1584. }
  1585. // If we haven't done a layout transition yet, we can just overwrite the previously written values, and the
  1586. // transition will be handled as the first thing in submit(), otherwise we queue a non-initial transition
  1587. // below.
  1588. if (!subresourceInfo.hasTransitioned)
  1589. {
  1590. subresourceInfo.initialLayout = subresourceInfo.requiredLayout;
  1591. subresourceInfo.currentLayout = subresourceInfo.requiredLayout;
  1592. subresourceInfo.isInitialReadOnly = subresourceInfo.isReadOnly;
  1593. }
  1594. else
  1595. {
  1596. if (subresourceInfo.currentLayout != subresourceInfo.requiredLayout)
  1597. mQueuedLayoutTransitions[image] = imageInfoIdx;
  1598. }
  1599. // If a FB attachment was just bound as a shader input, we might need to restart the render pass with a FB
  1600. // attachment that supports read-only attachments using the GENERAL layout
  1601. bool requiresReadOnlyFB = false;
  1602. if (isFBAttachment)
  1603. {
  1604. if (!subresourceInfo.isFBAttachment)
  1605. {
  1606. subresourceInfo.isFBAttachment = true;
  1607. requiresReadOnlyFB = subresourceInfo.isShaderInput;
  1608. }
  1609. }
  1610. else
  1611. {
  1612. if (!subresourceInfo.isShaderInput)
  1613. {
  1614. subresourceInfo.isShaderInput = true;
  1615. requiresReadOnlyFB = subresourceInfo.isFBAttachment;
  1616. }
  1617. }
  1618. // If we need to switch frame-buffers, end current render pass
  1619. if (requiresReadOnlyFB && isInRenderPass())
  1620. endRenderPass();
  1621. else
  1622. {
  1623. // Since we won't be ending the render pass, check if this same sub-resource was written earlier in the pass,
  1624. // in which case we need to issue a memory barrier so those writes are visible.
  1625. // Memory barrier only matters if image is bound for shader use (no need for color attachments or transfers)
  1626. bool isShaderBind = !isFBAttachment && newLayout != VK_IMAGE_LAYOUT_UNDEFINED;
  1627. if(subresourceInfo.needsBarrier && isShaderBind)
  1628. {
  1629. bool isWrite = flags.isSet(VulkanUseFlag::Write);
  1630. VkPipelineStageFlags stages =
  1631. VK_PIPELINE_STAGE_VERTEX_SHADER_BIT |
  1632. VK_PIPELINE_STAGE_TESSELLATION_CONTROL_SHADER_BIT |
  1633. VK_PIPELINE_STAGE_TESSELLATION_EVALUATION_SHADER_BIT |
  1634. VK_PIPELINE_STAGE_GEOMETRY_SHADER_BIT |
  1635. VK_PIPELINE_STAGE_FRAGMENT_SHADER_BIT |
  1636. VK_PIPELINE_STAGE_COMPUTE_SHADER_BIT;
  1637. memoryBarrier(image->getHandle(), VK_ACCESS_SHADER_WRITE_BIT,
  1638. image->getAccessFlags(subresourceInfo.requiredLayout, !isWrite),
  1639. stages, stages, subresourceInfo.requiredLayout, subresourceInfo.range);
  1640. subresourceInfo.needsBarrier = isWrite;
  1641. }
  1642. }
  1643. return requiresReadOnlyFB;
  1644. }
  1645. VulkanCmdBuffer::ImageSubresourceInfo& VulkanCmdBuffer::findSubresourceInfo(VulkanImage* image, UINT32 face, UINT32 mip)
  1646. {
  1647. UINT32 imageInfoIdx = mImages[image];
  1648. ImageInfo& imageInfo = mImageInfos[imageInfoIdx];
  1649. ImageSubresourceInfo* subresourceInfos = &mSubresourceInfos[imageInfo.subresourceInfoIdx];
  1650. for(UINT32 i = 0; i < imageInfo.numSubresourceInfos; i++)
  1651. {
  1652. ImageSubresourceInfo& entry = subresourceInfos[i];
  1653. if(face >= entry.range.baseArrayLayer && face < (entry.range.baseArrayLayer + entry.range.layerCount) &&
  1654. mip >= entry.range.baseMipLevel && mip < (entry.range.baseMipLevel + entry.range.levelCount))
  1655. {
  1656. return entry;
  1657. }
  1658. }
  1659. assert(false); // Caller should ensure the subresource actually exists, so this shouldn't happen
  1660. return subresourceInfos[0];
  1661. }
  1662. VulkanCommandBuffer::VulkanCommandBuffer(VulkanDevice& device, GpuQueueType type, UINT32 deviceIdx,
  1663. UINT32 queueIdx, bool secondary)
  1664. : CommandBuffer(type, deviceIdx, queueIdx, secondary), mBuffer(nullptr)
  1665. , mDevice(device), mQueue(nullptr), mIdMask(0)
  1666. {
  1667. UINT32 numQueues = device.getNumQueues(mType);
  1668. if (numQueues == 0) // Fall back to graphics queue
  1669. {
  1670. mType = GQT_GRAPHICS;
  1671. numQueues = device.getNumQueues(GQT_GRAPHICS);
  1672. }
  1673. mQueue = device.getQueue(mType, mQueueIdx % numQueues);
  1674. mIdMask = device.getQueueMask(mType, mQueueIdx);
  1675. acquireNewBuffer();
  1676. }
  1677. VulkanCommandBuffer::~VulkanCommandBuffer()
  1678. {
  1679. mBuffer->reset();
  1680. }
  1681. void VulkanCommandBuffer::acquireNewBuffer()
  1682. {
  1683. VulkanCmdBufferPool& pool = mDevice.getCmdBufferPool();
  1684. if (mBuffer != nullptr)
  1685. assert(mBuffer->isSubmitted());
  1686. UINT32 queueFamily = mDevice.getQueueFamily(mType);
  1687. mBuffer = pool.getBuffer(queueFamily, mIsSecondary);
  1688. }
  1689. void VulkanCommandBuffer::submit(UINT32 syncMask)
  1690. {
  1691. // Ignore myself
  1692. syncMask &= ~mIdMask;
  1693. if (mBuffer->isInRenderPass())
  1694. mBuffer->endRenderPass();
  1695. // Execute any queued layout transitions that weren't already handled by the render pass
  1696. mBuffer->executeLayoutTransitions();
  1697. if (mBuffer->isRecording())
  1698. mBuffer->end();
  1699. if (!mBuffer->isReadyForSubmit()) // Possibly nothing was recorded in the buffer
  1700. return;
  1701. mBuffer->submit(mQueue, mQueueIdx, syncMask);
  1702. acquireNewBuffer();
  1703. gVulkanCBManager().refreshStates(mDeviceIdx);
  1704. }
  1705. }}