BsVulkanCommandBuffer.cpp 46 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504
  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. #if BS_PLATFORM == BS_PLATFORM_WIN32
  16. #include "Win32/BsWin32RenderWindow.h"
  17. #else
  18. static_assert(false, "Other platforms go here");
  19. #endif
  20. namespace bs
  21. {
  22. VulkanSemaphore::VulkanSemaphore(VulkanResourceManager* owner)
  23. :VulkanResource(owner, true)
  24. {
  25. VkSemaphoreCreateInfo semaphoreCI;
  26. semaphoreCI.sType = VK_STRUCTURE_TYPE_SEMAPHORE_CREATE_INFO;
  27. semaphoreCI.pNext = nullptr;
  28. semaphoreCI.flags = 0;
  29. VkResult result = vkCreateSemaphore(owner->getDevice().getLogical(), &semaphoreCI, gVulkanAllocator, &mSemaphore);
  30. assert(result == VK_SUCCESS);
  31. }
  32. VulkanSemaphore::~VulkanSemaphore()
  33. {
  34. vkDestroySemaphore(mOwner->getDevice().getLogical(), mSemaphore, gVulkanAllocator);
  35. }
  36. VulkanCmdBufferPool::VulkanCmdBufferPool(VulkanDevice& device)
  37. :mDevice(device), mNextId(1)
  38. {
  39. for (UINT32 i = 0; i < GQT_COUNT; i++)
  40. {
  41. UINT32 familyIdx = device.getQueueFamily((GpuQueueType)i);
  42. if (familyIdx == (UINT32)-1)
  43. continue;
  44. VkCommandPoolCreateInfo poolCI;
  45. poolCI.sType = VK_STRUCTURE_TYPE_COMMAND_POOL_CREATE_INFO;
  46. poolCI.pNext = nullptr;
  47. poolCI.flags = VK_COMMAND_POOL_CREATE_RESET_COMMAND_BUFFER_BIT;
  48. poolCI.queueFamilyIndex = familyIdx;
  49. PoolInfo& poolInfo = mPools[familyIdx];
  50. poolInfo.queueFamily = familyIdx;
  51. memset(poolInfo.buffers, 0, sizeof(poolInfo.buffers));
  52. vkCreateCommandPool(device.getLogical(), &poolCI, gVulkanAllocator, &poolInfo.pool);
  53. }
  54. }
  55. VulkanCmdBufferPool::~VulkanCmdBufferPool()
  56. {
  57. // Note: Shutdown should be the only place command buffers are destroyed at, as the system relies on the fact that
  58. // they won't be destroyed during normal operation.
  59. for(auto& entry : mPools)
  60. {
  61. PoolInfo& poolInfo = entry.second;
  62. for (UINT32 i = 0; i < BS_MAX_VULKAN_CB_PER_QUEUE_FAMILY; i++)
  63. {
  64. VulkanCmdBuffer* buffer = poolInfo.buffers[i];
  65. if (buffer == nullptr)
  66. break;
  67. bs_delete(buffer);
  68. }
  69. vkDestroyCommandPool(mDevice.getLogical(), poolInfo.pool, gVulkanAllocator);
  70. }
  71. }
  72. VulkanCmdBuffer* VulkanCmdBufferPool::getBuffer(UINT32 queueFamily, bool secondary)
  73. {
  74. auto iterFind = mPools.find(queueFamily);
  75. if (iterFind == mPools.end())
  76. return nullptr;
  77. VulkanCmdBuffer** buffers = iterFind->second.buffers;
  78. UINT32 i = 0;
  79. for(; i < BS_MAX_VULKAN_CB_PER_QUEUE_FAMILY; i++)
  80. {
  81. if (buffers[i] == nullptr)
  82. break;
  83. if(buffers[i]->mState == VulkanCmdBuffer::State::Ready)
  84. {
  85. buffers[i]->begin();
  86. return buffers[i];
  87. }
  88. }
  89. assert(i < BS_MAX_VULKAN_CB_PER_QUEUE_FAMILY &&
  90. "Too many command buffers allocated. Increment BS_MAX_VULKAN_CB_PER_QUEUE_FAMILY to a higher value. ");
  91. buffers[i] = createBuffer(queueFamily, secondary);
  92. buffers[i]->begin();
  93. return buffers[i];
  94. }
  95. VulkanCmdBuffer* VulkanCmdBufferPool::createBuffer(UINT32 queueFamily, bool secondary)
  96. {
  97. auto iterFind = mPools.find(queueFamily);
  98. if (iterFind == mPools.end())
  99. return nullptr;
  100. const PoolInfo& poolInfo = iterFind->second;
  101. return bs_new<VulkanCmdBuffer>(mDevice, mNextId++, poolInfo.pool, poolInfo.queueFamily, secondary);
  102. }
  103. VulkanCmdBuffer::VulkanCmdBuffer(VulkanDevice& device, UINT32 id, VkCommandPool pool, UINT32 queueFamily, bool secondary)
  104. : mId(id), mQueueFamily(queueFamily), mState(State::Ready), mDevice(device), mPool(pool), mSemaphore(nullptr)
  105. , mFenceCounter(0), mFramebuffer(nullptr), mRenderTargetWidth(0)
  106. , mRenderTargetHeight(0), mRenderTargetDepthReadOnly(false), mRenderTargetLoadMask(RT_NONE), mGlobalQueueIdx(-1)
  107. , mViewport(0.0f, 0.0f, 1.0f, 1.0f), mScissor(0, 0, 0, 0), mStencilRef(0), mDrawOp(DOT_TRIANGLE_LIST)
  108. , mNumBoundDescriptorSets(0), mGfxPipelineRequiresBind(true), mCmpPipelineRequiresBind(true)
  109. , mViewportRequiresBind(true), mStencilRefRequiresBind(true), mScissorRequiresBind(true), mClearValues()
  110. , mClearMask(), mVertexBuffersTemp(), mVertexBufferOffsetsTemp()
  111. {
  112. UINT32 maxBoundDescriptorSets = device.getDeviceProperties().limits.maxBoundDescriptorSets;
  113. mDescriptorSetsTemp = (VkDescriptorSet*)bs_alloc(sizeof(VkDescriptorSet) * maxBoundDescriptorSets);
  114. VkCommandBufferAllocateInfo cmdBufferAllocInfo;
  115. cmdBufferAllocInfo.sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_ALLOCATE_INFO;
  116. cmdBufferAllocInfo.pNext = nullptr;
  117. cmdBufferAllocInfo.commandPool = pool;
  118. cmdBufferAllocInfo.level = secondary ? VK_COMMAND_BUFFER_LEVEL_SECONDARY : VK_COMMAND_BUFFER_LEVEL_PRIMARY;
  119. cmdBufferAllocInfo.commandBufferCount = 1;
  120. VkResult result = vkAllocateCommandBuffers(mDevice.getLogical(), &cmdBufferAllocInfo, &mCmdBuffer);
  121. assert(result == VK_SUCCESS);
  122. VkFenceCreateInfo fenceCI;
  123. fenceCI.sType = VK_STRUCTURE_TYPE_FENCE_CREATE_INFO;
  124. fenceCI.pNext = nullptr;
  125. fenceCI.flags = 0;
  126. result = vkCreateFence(mDevice.getLogical(), &fenceCI, gVulkanAllocator, &mFence);
  127. assert(result == VK_SUCCESS);
  128. }
  129. VulkanCmdBuffer::~VulkanCmdBuffer()
  130. {
  131. VkDevice device = mDevice.getLogical();
  132. if(mState == State::Submitted)
  133. {
  134. // Wait 1s
  135. UINT64 waitTime = 1000 * 1000 * 1000;
  136. VkResult result = vkWaitForFences(device, 1, &mFence, true, waitTime);
  137. assert(result == VK_SUCCESS || result == VK_TIMEOUT);
  138. if (result == VK_TIMEOUT)
  139. LOGWRN("Freeing a command buffer before done executing because fence wait expired!");
  140. // Resources have been marked as used, make sure to notify them we're done with them
  141. refreshFenceStatus();
  142. }
  143. else if(mState != State::Ready)
  144. {
  145. // Notify any resources that they are no longer bound
  146. for (auto& entry : mResources)
  147. {
  148. ResourceUseHandle& useHandle = entry.second;
  149. assert(useHandle.used);
  150. entry.first->notifyUnbound();
  151. }
  152. for (auto& entry : mImages)
  153. {
  154. UINT32 imageInfoIdx = entry.second;
  155. ImageInfo& imageInfo = mImageInfos[imageInfoIdx];
  156. ResourceUseHandle& useHandle = imageInfo.useHandle;
  157. assert(useHandle.used);
  158. entry.first->notifyUnbound();
  159. }
  160. for (auto& entry : mBuffers)
  161. {
  162. ResourceUseHandle& useHandle = entry.second.useHandle;
  163. assert(useHandle.used);
  164. entry.first->notifyUnbound();
  165. }
  166. }
  167. if (mSemaphore != nullptr)
  168. mSemaphore->destroy();
  169. vkDestroyFence(device, mFence, gVulkanAllocator);
  170. vkFreeCommandBuffers(device, mPool, 1, &mCmdBuffer);
  171. bs_free(mDescriptorSetsTemp);
  172. }
  173. UINT32 VulkanCmdBuffer::getDeviceIdx() const
  174. {
  175. return mDevice.getIndex();
  176. }
  177. void VulkanCmdBuffer::begin()
  178. {
  179. assert(mState == State::Ready);
  180. VkCommandBufferBeginInfo beginInfo;
  181. beginInfo.sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_BEGIN_INFO;
  182. beginInfo.pNext = nullptr;
  183. beginInfo.flags = VK_COMMAND_BUFFER_USAGE_ONE_TIME_SUBMIT_BIT;
  184. beginInfo.pInheritanceInfo = nullptr;
  185. VkResult result = vkBeginCommandBuffer(mCmdBuffer, &beginInfo);
  186. assert(result == VK_SUCCESS);
  187. mState = State::Recording;
  188. }
  189. void VulkanCmdBuffer::end()
  190. {
  191. assert(mState == State::Recording);
  192. // If a clear is queued, execute the render pass with no additional instructions
  193. if (mClearMask)
  194. executeClearPass();
  195. VkResult result = vkEndCommandBuffer(mCmdBuffer);
  196. assert(result == VK_SUCCESS);
  197. mState = State::RecordingDone;
  198. }
  199. void VulkanCmdBuffer::beginRenderPass()
  200. {
  201. assert(mState == State::Recording);
  202. if (mFramebuffer == nullptr)
  203. {
  204. LOGWRN("Attempting to begin a render pass but no render target is bound to the command buffer.");
  205. return;
  206. }
  207. if(mClearMask != CLEAR_NONE)
  208. {
  209. // If a previous clear is queued, but it doesn't match the rendered area, need to execute a separate pass
  210. // just for it
  211. Rect2I rtArea(0, 0, mRenderTargetWidth, mRenderTargetHeight);
  212. if (mClearArea != rtArea)
  213. executeClearPass();
  214. }
  215. executeLayoutTransitions();
  216. // Check if any frame-buffer attachments are also used as shader inputs, in which case we make them read-only
  217. RenderSurfaceMask readMask = RT_NONE;
  218. UINT32 numColorAttachments = mFramebuffer->getNumColorAttachments();
  219. for(UINT32 i = 0; i < numColorAttachments; i++)
  220. {
  221. VulkanImage* image = mFramebuffer->getColorAttachment(i).image;
  222. UINT32 imageInfoIdx = mImages[image];
  223. ImageInfo& imageInfo = mImageInfos[imageInfoIdx];
  224. bool readOnly = imageInfo.isShaderInput;
  225. if(readOnly)
  226. readMask.set((RenderSurfaceMaskBits)(1 << i));
  227. }
  228. if(mFramebuffer->hasDepthAttachment())
  229. {
  230. VulkanImage* image = mFramebuffer->getDepthStencilAttachment().image;
  231. UINT32 imageInfoIdx = mImages[image];
  232. ImageInfo& imageInfo = mImageInfos[imageInfoIdx];
  233. bool readOnly = imageInfo.isShaderInput;
  234. if (readOnly)
  235. readMask.set(RT_DEPTH);
  236. }
  237. VkRenderPassBeginInfo renderPassBeginInfo;
  238. renderPassBeginInfo.sType = VK_STRUCTURE_TYPE_RENDER_PASS_BEGIN_INFO;
  239. renderPassBeginInfo.pNext = nullptr;
  240. renderPassBeginInfo.framebuffer = mFramebuffer->getFramebuffer(mRenderTargetLoadMask, readMask, mClearMask);
  241. renderPassBeginInfo.renderPass = mFramebuffer->getRenderPass(mRenderTargetLoadMask, readMask, mClearMask);
  242. renderPassBeginInfo.renderArea.offset.x = 0;
  243. renderPassBeginInfo.renderArea.offset.y = 0;
  244. renderPassBeginInfo.renderArea.extent.width = mRenderTargetWidth;
  245. renderPassBeginInfo.renderArea.extent.height = mRenderTargetHeight;
  246. renderPassBeginInfo.clearValueCount = mFramebuffer->getNumAttachments();
  247. renderPassBeginInfo.pClearValues = mClearValues.data();
  248. vkCmdBeginRenderPass(mCmdBuffer, &renderPassBeginInfo, VK_SUBPASS_CONTENTS_INLINE);
  249. mClearMask = CLEAR_NONE;
  250. mState = State::RecordingRenderPass;
  251. }
  252. void VulkanCmdBuffer::endRenderPass()
  253. {
  254. assert(mState == State::RecordingRenderPass);
  255. vkCmdEndRenderPass(mCmdBuffer);
  256. mState = State::Recording;
  257. }
  258. VulkanSemaphore* VulkanCmdBuffer::allocateSemaphore()
  259. {
  260. if (mSemaphore != nullptr)
  261. mSemaphore->destroy();
  262. mSemaphore = mDevice.getResourceManager().create<VulkanSemaphore>();
  263. return mSemaphore;
  264. }
  265. void VulkanCmdBuffer::submit(VulkanQueue* queue, UINT32 queueIdx, UINT32 syncMask)
  266. {
  267. assert(isReadyForSubmit());
  268. // Issue pipeline barriers for queue transitions (need to happen on original queue first, then on new queue)
  269. for (auto& entry : mBuffers)
  270. {
  271. VulkanBuffer* resource = static_cast<VulkanBuffer*>(entry.first);
  272. if (!resource->isExclusive())
  273. continue;
  274. UINT32 currentQueueFamily = resource->getQueueFamily();
  275. if (currentQueueFamily != -1 && currentQueueFamily != mQueueFamily)
  276. {
  277. Vector<VkBufferMemoryBarrier>& barriers = mTransitionInfoTemp[currentQueueFamily].bufferBarriers;
  278. barriers.push_back(VkBufferMemoryBarrier());
  279. VkBufferMemoryBarrier& barrier = barriers.back();
  280. barrier.sType = VK_STRUCTURE_TYPE_BUFFER_MEMORY_BARRIER;
  281. barrier.pNext = nullptr;
  282. barrier.srcAccessMask = entry.second.accessFlags;
  283. barrier.dstAccessMask = entry.second.accessFlags;
  284. barrier.srcQueueFamilyIndex = currentQueueFamily;
  285. barrier.dstQueueFamilyIndex = mQueueFamily;
  286. barrier.buffer = resource->getHandle();
  287. barrier.offset = 0;
  288. barrier.size = VK_WHOLE_SIZE;
  289. }
  290. }
  291. for (auto& entry : mImages)
  292. {
  293. VulkanImage* resource = static_cast<VulkanImage*>(entry.first);
  294. ImageInfo& imageInfo = mImageInfos[entry.second];
  295. UINT32 currentQueueFamily = resource->getQueueFamily();
  296. bool queueMismatch = resource->isExclusive() && currentQueueFamily != -1 && currentQueueFamily != mQueueFamily;
  297. if (queueMismatch || imageInfo.currentLayout != imageInfo.requiredLayout)
  298. {
  299. Vector<VkImageMemoryBarrier>& barriers = mTransitionInfoTemp[currentQueueFamily].imageBarriers;
  300. barriers.push_back(VkImageMemoryBarrier());
  301. VkImageMemoryBarrier& barrier = barriers.back();
  302. barrier.sType = VK_STRUCTURE_TYPE_IMAGE_MEMORY_BARRIER;
  303. barrier.pNext = nullptr;
  304. barrier.srcAccessMask = imageInfo.accessFlags;
  305. barrier.dstAccessMask = imageInfo.accessFlags;
  306. barrier.srcQueueFamilyIndex = currentQueueFamily;
  307. barrier.dstQueueFamilyIndex = mQueueFamily;
  308. barrier.oldLayout = imageInfo.currentLayout;
  309. barrier.newLayout = imageInfo.requiredLayout;
  310. barrier.image = resource->getHandle();
  311. barrier.subresourceRange = imageInfo.range;
  312. imageInfo.currentLayout = imageInfo.requiredLayout;
  313. }
  314. resource->setLayout(imageInfo.finalLayout);
  315. }
  316. VulkanDevice& device = queue->getDevice();
  317. for (auto& entry : mTransitionInfoTemp)
  318. {
  319. bool empty = entry.second.imageBarriers.size() == 0 && entry.second.bufferBarriers.size() == 0;
  320. if (empty)
  321. continue;
  322. UINT32 entryQueueFamily = entry.first;
  323. // No queue transition needed for entries on this queue (this entry is most likely an image layout transition)
  324. if (entryQueueFamily == mQueueFamily)
  325. continue;
  326. VulkanCmdBuffer* cmdBuffer = device.getCmdBufferPool().getBuffer(entryQueueFamily, false);
  327. VkCommandBuffer vkCmdBuffer = cmdBuffer->getHandle();
  328. TransitionInfo& barriers = entry.second;
  329. UINT32 numImgBarriers = (UINT32)barriers.imageBarriers.size();
  330. UINT32 numBufferBarriers = (UINT32)barriers.bufferBarriers.size();
  331. vkCmdPipelineBarrier(vkCmdBuffer,
  332. VK_PIPELINE_STAGE_TOP_OF_PIPE_BIT, // Note: VK_PIPELINE_STAGE_BOTTOM_OF_PIPE_BIT might be more correct here, according to the spec
  333. 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.
  334. 0, 0, nullptr,
  335. numBufferBarriers, barriers.bufferBarriers.data(),
  336. numImgBarriers, barriers.imageBarriers.data());
  337. // Find an appropriate queue to execute on
  338. UINT32 otherQueueIdx = 0;
  339. VulkanQueue* otherQueue = nullptr;
  340. GpuQueueType otherQueueType = GQT_GRAPHICS;
  341. for (UINT32 i = 0; i < GQT_COUNT; i++)
  342. {
  343. otherQueueType = (GpuQueueType)i;
  344. if (device.getQueueFamily(otherQueueType) != entryQueueFamily)
  345. continue;
  346. UINT32 numQueues = device.getNumQueues(otherQueueType);
  347. for (UINT32 j = 0; j < numQueues; j++)
  348. {
  349. // Try to find a queue not currently executing
  350. VulkanQueue* curQueue = device.getQueue(otherQueueType, j);
  351. if (!curQueue->isExecuting())
  352. {
  353. otherQueue = curQueue;
  354. otherQueueIdx = j;
  355. }
  356. }
  357. // Can't find empty one, use the first one then
  358. if (otherQueue == nullptr)
  359. {
  360. otherQueue = device.getQueue(otherQueueType, 0);
  361. otherQueueIdx = 0;
  362. }
  363. break;
  364. }
  365. syncMask |= CommandSyncMask::getGlobalQueueMask(otherQueueType, otherQueueIdx);
  366. cmdBuffer->end();
  367. cmdBuffer->submit(otherQueue, otherQueueIdx, 0);
  368. // If there are any layout transitions, reset them as we don't need them for the second pipeline barrier
  369. for (auto& barrierEntry : barriers.imageBarriers)
  370. barrierEntry.oldLayout = barrierEntry.newLayout;
  371. }
  372. UINT32 deviceIdx = device.getIndex();
  373. VulkanCommandBufferManager& cbm = static_cast<VulkanCommandBufferManager&>(CommandBufferManager::instance());
  374. UINT32 numSemaphores;
  375. cbm.getSyncSemaphores(deviceIdx, syncMask, mSemaphoresTemp, numSemaphores);
  376. // Issue second part of transition pipeline barriers (on this queue)
  377. for (auto& entry : mTransitionInfoTemp)
  378. {
  379. bool empty = entry.second.imageBarriers.size() == 0 && entry.second.bufferBarriers.size() == 0;
  380. if (empty)
  381. continue;
  382. VulkanCmdBuffer* cmdBuffer = device.getCmdBufferPool().getBuffer(mQueueFamily, false);
  383. VkCommandBuffer vkCmdBuffer = cmdBuffer->getHandle();
  384. TransitionInfo& barriers = entry.second;
  385. UINT32 numImgBarriers = (UINT32)barriers.imageBarriers.size();
  386. UINT32 numBufferBarriers = (UINT32)barriers.bufferBarriers.size();
  387. vkCmdPipelineBarrier(vkCmdBuffer,
  388. VK_PIPELINE_STAGE_TOP_OF_PIPE_BIT, // Note: VK_PIPELINE_STAGE_BOTTOM_OF_PIPE_BIT might be more correct here, according to the spec
  389. VK_PIPELINE_STAGE_TOP_OF_PIPE_BIT,
  390. 0, 0, nullptr,
  391. numBufferBarriers, barriers.bufferBarriers.data(),
  392. numImgBarriers, barriers.imageBarriers.data());
  393. cmdBuffer->end();
  394. queue->submit(cmdBuffer, mSemaphoresTemp, numSemaphores);
  395. numSemaphores = 0; // Semaphores are only needed the first time, since we're adding the buffers on the same queue
  396. }
  397. queue->submit(this, mSemaphoresTemp, numSemaphores);
  398. mGlobalQueueIdx = CommandSyncMask::getGlobalQueueIdx(queue->getType(), queueIdx);
  399. for (auto& entry : mResources)
  400. {
  401. ResourceUseHandle& useHandle = entry.second;
  402. assert(!useHandle.used);
  403. useHandle.used = true;
  404. entry.first->notifyUsed(mGlobalQueueIdx, mQueueFamily, useHandle.flags);
  405. }
  406. for (auto& entry : mImages)
  407. {
  408. UINT32 imageInfoIdx = entry.second;
  409. ImageInfo& imageInfo = mImageInfos[imageInfoIdx];
  410. ResourceUseHandle& useHandle = imageInfo.useHandle;
  411. assert(!useHandle.used);
  412. useHandle.used = true;
  413. entry.first->notifyUsed(mGlobalQueueIdx, mQueueFamily, useHandle.flags);
  414. }
  415. for (auto& entry : mBuffers)
  416. {
  417. ResourceUseHandle& useHandle = entry.second.useHandle;
  418. assert(!useHandle.used);
  419. useHandle.used = true;
  420. entry.first->notifyUsed(mGlobalQueueIdx, mQueueFamily, useHandle.flags);
  421. }
  422. // Note: Uncommented for debugging only, prevents any device concurrency issues.
  423. // vkQueueWaitIdle(queue->getHandle());
  424. // Clear vectors but don't clear the actual map, as we want to re-use the memory since we expect queue family
  425. // indices to be the same
  426. for (auto& entry : mTransitionInfoTemp)
  427. {
  428. entry.second.imageBarriers.clear();
  429. entry.second.bufferBarriers.clear();
  430. }
  431. mGraphicsPipeline = nullptr;
  432. mComputePipeline = nullptr;
  433. mGfxPipelineRequiresBind = true;
  434. mCmpPipelineRequiresBind = true;
  435. mFramebuffer = nullptr;
  436. mDescriptorSetsBindState = DescriptorSetBindFlag::Graphics | DescriptorSetBindFlag::Compute;
  437. mQueuedLayoutTransitions.clear();
  438. }
  439. void VulkanCmdBuffer::refreshFenceStatus()
  440. {
  441. VkResult result = vkGetFenceStatus(mDevice.getLogical(), mFence);
  442. assert(result == VK_SUCCESS || result == VK_NOT_READY);
  443. bool signaled = result == VK_SUCCESS;
  444. if (mState == State::Submitted)
  445. {
  446. if(signaled)
  447. {
  448. mState = State::Ready;
  449. vkResetCommandBuffer(mCmdBuffer, VK_COMMAND_BUFFER_RESET_RELEASE_RESOURCES_BIT); // Note: Maybe better not to release resources?
  450. result = vkResetFences(mDevice.getLogical(), 1, &mFence);
  451. assert(result == VK_SUCCESS);
  452. mFenceCounter++;
  453. for (auto& entry : mResources)
  454. {
  455. ResourceUseHandle& useHandle = entry.second;
  456. assert(useHandle.used);
  457. entry.first->notifyDone(mGlobalQueueIdx, useHandle.flags);
  458. }
  459. for (auto& entry : mImages)
  460. {
  461. UINT32 imageInfoIdx = entry.second;
  462. ImageInfo& imageInfo = mImageInfos[imageInfoIdx];
  463. ResourceUseHandle& useHandle = imageInfo.useHandle;
  464. assert(useHandle.used);
  465. entry.first->notifyDone(mGlobalQueueIdx, useHandle.flags);
  466. }
  467. for (auto& entry : mBuffers)
  468. {
  469. ResourceUseHandle& useHandle = entry.second.useHandle;
  470. assert(useHandle.used);
  471. entry.first->notifyDone(mGlobalQueueIdx, useHandle.flags);
  472. }
  473. mResources.clear();
  474. mImages.clear();
  475. mBuffers.clear();
  476. mImageInfos.clear();
  477. }
  478. }
  479. else
  480. assert(!signaled); // We reset the fence along with mState so this shouldn't be possible
  481. }
  482. void VulkanCmdBuffer::setRenderTarget(const SPtr<RenderTargetCore>& rt, bool readOnlyDepthStencil,
  483. RenderSurfaceMask loadMask)
  484. {
  485. assert(mState != State::Submitted);
  486. VulkanFramebuffer* oldFramebuffer = mFramebuffer;
  487. if(rt == nullptr)
  488. {
  489. mFramebuffer = nullptr;
  490. mRenderTargetWidth = 0;
  491. mRenderTargetHeight = 0;
  492. mRenderTargetDepthReadOnly = false;
  493. mRenderTargetLoadMask = RT_NONE;
  494. }
  495. else
  496. {
  497. if (rt->getProperties().isWindow())
  498. {
  499. Win32RenderWindowCore* window = static_cast<Win32RenderWindowCore*>(rt.get());
  500. window->acquireBackBuffer();
  501. }
  502. rt->getCustomAttribute("FB", &mFramebuffer);
  503. mRenderTargetWidth = rt->getProperties().getWidth();
  504. mRenderTargetHeight = rt->getProperties().getHeight();
  505. mRenderTargetDepthReadOnly = readOnlyDepthStencil;
  506. mRenderTargetLoadMask = loadMask;
  507. }
  508. // If anything changed
  509. if(oldFramebuffer != mFramebuffer)
  510. {
  511. if (isInRenderPass())
  512. endRenderPass();
  513. else
  514. {
  515. // If a clear is queued for previous FB, execute the render pass with no additional instructions
  516. if (mClearMask)
  517. executeClearPass();
  518. }
  519. // Reset flags that signal image usage
  520. for (auto& entry : mImages)
  521. {
  522. UINT32 imageInfoIdx = entry.second;
  523. ImageInfo& imageInfo = mImageInfos[imageInfoIdx];
  524. imageInfo.isFBAttachment = false;
  525. imageInfo.isShaderInput = false;
  526. }
  527. setGpuParams(nullptr);
  528. if(mFramebuffer != nullptr)
  529. registerResource(mFramebuffer, VulkanUseFlag::Write);
  530. mGfxPipelineRequiresBind = true;
  531. }
  532. }
  533. void VulkanCmdBuffer::clearViewport(const Rect2I& area, UINT32 buffers, const Color& color, float depth, UINT16 stencil,
  534. UINT8 targetMask)
  535. {
  536. if (buffers == 0 || mFramebuffer == nullptr)
  537. return;
  538. // Add clear command if currently in render pass
  539. if (isInRenderPass())
  540. {
  541. VkClearAttachment attachments[BS_MAX_MULTIPLE_RENDER_TARGETS + 1];
  542. UINT32 baseLayer = 0;
  543. UINT32 attachmentIdx = 0;
  544. if ((buffers & FBT_COLOR) != 0)
  545. {
  546. UINT32 numColorAttachments = mFramebuffer->getNumColorAttachments();
  547. for (UINT32 i = 0; i < numColorAttachments; i++)
  548. {
  549. const VulkanFramebufferAttachment& attachment = mFramebuffer->getColorAttachment(i);
  550. if (((1 << attachment.index) & targetMask) == 0)
  551. continue;
  552. attachments[attachmentIdx].aspectMask = VK_IMAGE_ASPECT_COLOR_BIT;
  553. attachments[attachmentIdx].colorAttachment = i;
  554. VkClearColorValue& colorValue = attachments[attachmentIdx].clearValue.color;
  555. colorValue.float32[0] = color.r;
  556. colorValue.float32[1] = color.g;
  557. colorValue.float32[2] = color.b;
  558. colorValue.float32[3] = color.a;
  559. UINT32 curBaseLayer = attachment.baseLayer;
  560. if (attachmentIdx == 0)
  561. baseLayer = curBaseLayer;
  562. else
  563. {
  564. if (baseLayer != curBaseLayer)
  565. {
  566. // Note: This could be supported relatively easily: we would need to issue multiple separate
  567. // clear commands for such framebuffers.
  568. LOGERR("Attempting to clear a texture that has multiple multi-layer surfaces with mismatching "
  569. "starting layers. This is currently not supported.");
  570. }
  571. }
  572. attachmentIdx++;
  573. }
  574. }
  575. if ((buffers & FBT_DEPTH) != 0 || (buffers & FBT_STENCIL) != 0)
  576. {
  577. if (mFramebuffer->hasDepthAttachment())
  578. {
  579. attachments[attachmentIdx].aspectMask = 0;
  580. if ((buffers & FBT_DEPTH) != 0)
  581. {
  582. attachments[attachmentIdx].aspectMask |= VK_IMAGE_ASPECT_DEPTH_BIT;
  583. attachments[attachmentIdx].clearValue.depthStencil.depth = depth;
  584. }
  585. if ((buffers & FBT_STENCIL) != 0)
  586. {
  587. attachments[attachmentIdx].aspectMask |= VK_IMAGE_ASPECT_STENCIL_BIT;
  588. attachments[attachmentIdx].clearValue.depthStencil.stencil = stencil;
  589. }
  590. attachments[attachmentIdx].colorAttachment = 0;
  591. UINT32 curBaseLayer = mFramebuffer->getDepthStencilAttachment().baseLayer;
  592. if (attachmentIdx == 0)
  593. baseLayer = curBaseLayer;
  594. else
  595. {
  596. if (baseLayer != curBaseLayer)
  597. {
  598. // Note: This could be supported relatively easily: we would need to issue multiple separate
  599. // clear commands for such framebuffers.
  600. LOGERR("Attempting to clear a texture that has multiple multi-layer surfaces with mismatching "
  601. "starting layers. This is currently not supported.");
  602. }
  603. }
  604. attachmentIdx++;
  605. }
  606. }
  607. UINT32 numAttachments = attachmentIdx;
  608. if (numAttachments == 0)
  609. return;
  610. VkClearRect clearRect;
  611. clearRect.baseArrayLayer = baseLayer;
  612. clearRect.layerCount = mFramebuffer->getNumLayers();
  613. clearRect.rect.offset.x = area.x;
  614. clearRect.rect.offset.y = area.y;
  615. clearRect.rect.extent.width = area.width;
  616. clearRect.rect.extent.height = area.height;
  617. vkCmdClearAttachments(mCmdBuffer, numAttachments, attachments, 1, &clearRect);
  618. }
  619. // Otherwise we use a render pass that performs a clear on begin
  620. else
  621. {
  622. UINT32 attachmentIdx = 0;
  623. ClearMask clearMask;
  624. std::array<VkClearValue, BS_MAX_MULTIPLE_RENDER_TARGETS + 1> clearValues;
  625. if ((buffers & FBT_COLOR) != 0)
  626. {
  627. UINT32 numColorAttachments = mFramebuffer->getNumColorAttachments();
  628. for (UINT32 i = 0; i < numColorAttachments; i++)
  629. {
  630. const VulkanFramebufferAttachment& attachment = mFramebuffer->getColorAttachment(i);
  631. if (((1 << attachment.index) & targetMask) == 0)
  632. continue;
  633. clearMask |= (ClearMaskBits)(1 << attachment.index);
  634. VkClearColorValue& colorValue = clearValues[attachmentIdx].color;
  635. colorValue.float32[0] = color.r;
  636. colorValue.float32[1] = color.g;
  637. colorValue.float32[2] = color.b;
  638. colorValue.float32[3] = color.a;
  639. attachmentIdx++;
  640. }
  641. }
  642. if ((buffers & FBT_DEPTH) != 0 || (buffers & FBT_STENCIL) != 0)
  643. {
  644. if (mFramebuffer->hasDepthAttachment())
  645. {
  646. if ((buffers & FBT_DEPTH) != 0)
  647. {
  648. clearValues[attachmentIdx].depthStencil.depth = depth;
  649. clearMask |= CLEAR_DEPTH;
  650. }
  651. if ((buffers & FBT_STENCIL) != 0)
  652. {
  653. clearValues[attachmentIdx].depthStencil.stencil = stencil;
  654. clearMask |= CLEAR_STENCIL;
  655. }
  656. attachmentIdx++;
  657. }
  658. }
  659. UINT32 numAttachments = attachmentIdx;
  660. if (numAttachments == 0)
  661. return;
  662. // Some previous clear operation is already queued, execute it first
  663. bool previousClearNeedsToFinish = (mClearMask & clearMask) != CLEAR_NONE;
  664. if(previousClearNeedsToFinish)
  665. executeClearPass();
  666. mClearMask = clearMask;
  667. mClearValues = clearValues;
  668. mClearArea = area;
  669. }
  670. }
  671. void VulkanCmdBuffer::clearRenderTarget(UINT32 buffers, const Color& color, float depth, UINT16 stencil, UINT8 targetMask)
  672. {
  673. Rect2I area(0, 0, mRenderTargetWidth, mRenderTargetHeight);
  674. clearViewport(area, buffers, color, depth, stencil, targetMask);
  675. }
  676. void VulkanCmdBuffer::clearViewport(UINT32 buffers, const Color& color, float depth, UINT16 stencil, UINT8 targetMask)
  677. {
  678. Rect2I area;
  679. area.x = (UINT32)(mViewport.x * mRenderTargetWidth);
  680. area.y = (UINT32)(mViewport.y * mRenderTargetHeight);
  681. area.width = (UINT32)(mViewport.width * mRenderTargetWidth);
  682. area.height = (UINT32)(mViewport.height * mRenderTargetHeight);
  683. clearViewport(area, buffers, color, depth, stencil, targetMask);
  684. }
  685. void VulkanCmdBuffer::setPipelineState(const SPtr<GraphicsPipelineStateCore>& state)
  686. {
  687. if (mGraphicsPipeline == state)
  688. return;
  689. mGraphicsPipeline = std::static_pointer_cast<VulkanGraphicsPipelineStateCore>(state);
  690. mGfxPipelineRequiresBind = true;
  691. }
  692. void VulkanCmdBuffer::setPipelineState(const SPtr<ComputePipelineStateCore>& state)
  693. {
  694. if (mComputePipeline == state)
  695. return;
  696. mComputePipeline = std::static_pointer_cast<VulkanComputePipelineStateCore>(state);
  697. mCmpPipelineRequiresBind = true;
  698. }
  699. void VulkanCmdBuffer::setGpuParams(const SPtr<GpuParamsCore>& gpuParams)
  700. {
  701. SPtr<VulkanGpuParams> vulkanGpuParams = std::static_pointer_cast<VulkanGpuParams>(gpuParams);
  702. if(vulkanGpuParams != nullptr)
  703. {
  704. mNumBoundDescriptorSets = vulkanGpuParams->getNumSets();
  705. vulkanGpuParams->prepareForBind(*this, mDescriptorSetsTemp);
  706. }
  707. else
  708. {
  709. mNumBoundDescriptorSets = 0;
  710. }
  711. mDescriptorSetsBindState = DescriptorSetBindFlag::Graphics | DescriptorSetBindFlag::Compute;
  712. }
  713. void VulkanCmdBuffer::setViewport(const Rect2& area)
  714. {
  715. if (mViewport == area)
  716. return;
  717. mViewport = area;
  718. mViewportRequiresBind = true;
  719. }
  720. void VulkanCmdBuffer::setScissorRect(const Rect2I& value)
  721. {
  722. if (mScissor == value)
  723. return;
  724. mScissor = value;
  725. mScissorRequiresBind = true;
  726. }
  727. void VulkanCmdBuffer::setStencilRef(UINT32 value)
  728. {
  729. if (mStencilRef == value)
  730. return;
  731. mStencilRef = value;
  732. mStencilRefRequiresBind = true;
  733. }
  734. void VulkanCmdBuffer::setDrawOp(DrawOperationType drawOp)
  735. {
  736. if (mDrawOp == drawOp)
  737. return;
  738. mDrawOp = drawOp;
  739. mGfxPipelineRequiresBind = true;
  740. }
  741. void VulkanCmdBuffer::setVertexBuffers(UINT32 index, SPtr<VertexBufferCore>* buffers, UINT32 numBuffers)
  742. {
  743. if (numBuffers == 0)
  744. return;
  745. for(UINT32 i = 0; i < numBuffers; i++)
  746. {
  747. VulkanVertexBufferCore* vertexBuffer = static_cast<VulkanVertexBufferCore*>(buffers[i].get());
  748. if (vertexBuffer != nullptr)
  749. {
  750. VulkanBuffer* resource = vertexBuffer->getResource(mDevice.getIndex());
  751. if (resource != nullptr)
  752. {
  753. mVertexBuffersTemp[i] = resource->getHandle();
  754. registerResource(resource, VulkanUseFlag::Read);
  755. }
  756. else
  757. mVertexBuffersTemp[i] = VK_NULL_HANDLE;
  758. }
  759. else
  760. mVertexBuffersTemp[i] = VK_NULL_HANDLE;
  761. }
  762. vkCmdBindVertexBuffers(mCmdBuffer, index, numBuffers, mVertexBuffersTemp, mVertexBufferOffsetsTemp);
  763. }
  764. void VulkanCmdBuffer::setIndexBuffer(const SPtr<IndexBufferCore>& buffer)
  765. {
  766. VulkanIndexBufferCore* indexBuffer = static_cast<VulkanIndexBufferCore*>(buffer.get());
  767. VkBuffer vkBuffer = VK_NULL_HANDLE;
  768. VkIndexType indexType = VK_INDEX_TYPE_UINT32;
  769. if (indexBuffer != nullptr)
  770. {
  771. VulkanBuffer* resource = indexBuffer->getResource(mDevice.getIndex());
  772. if (resource != nullptr)
  773. {
  774. vkBuffer = resource->getHandle();
  775. indexType = VulkanUtility::getIndexType(buffer->getProperties().getType());
  776. registerResource(resource, VulkanUseFlag::Read);
  777. }
  778. }
  779. vkCmdBindIndexBuffer(mCmdBuffer, vkBuffer, 0, indexType);
  780. }
  781. void VulkanCmdBuffer::setVertexDeclaration(const SPtr<VertexDeclarationCore>& decl)
  782. {
  783. if (mVertexDecl == decl)
  784. return;
  785. mVertexDecl = decl;
  786. mGfxPipelineRequiresBind = true;
  787. }
  788. bool VulkanCmdBuffer::isReadyForRender()
  789. {
  790. if (mGraphicsPipeline == nullptr)
  791. return false;
  792. SPtr<VertexDeclarationCore> inputDecl = mGraphicsPipeline->getInputDeclaration();
  793. if (inputDecl == nullptr)
  794. return false;
  795. return mFramebuffer != nullptr && mVertexDecl != nullptr;
  796. }
  797. bool VulkanCmdBuffer::bindGraphicsPipeline()
  798. {
  799. SPtr<VertexDeclarationCore> inputDecl = mGraphicsPipeline->getInputDeclaration();
  800. SPtr<VulkanVertexInput> vertexInput = VulkanVertexInputManager::instance().getVertexInfo(mVertexDecl, inputDecl);
  801. VulkanPipeline* pipeline = mGraphicsPipeline->getPipeline(mDevice.getIndex(), mFramebuffer,
  802. mRenderTargetDepthReadOnly, mDrawOp, vertexInput);
  803. if (pipeline == nullptr)
  804. return false;
  805. // Check that pipeline matches the read-only state of any framebuffer attachments
  806. UINT32 numColorAttachments = mFramebuffer->getNumColorAttachments();
  807. for (UINT32 i = 0; i < numColorAttachments; i++)
  808. {
  809. VulkanImage* image = mFramebuffer->getColorAttachment(i).image;
  810. UINT32 imageInfoIdx = mImages[image];
  811. ImageInfo& imageInfo = mImageInfos[imageInfoIdx];
  812. if (imageInfo.isShaderInput && !pipeline->isColorReadOnly(i))
  813. {
  814. LOGWRN("Framebuffer attachment also used as a shader input, but color writes aren't disabled. This will"
  815. " result in undefined behavior.");
  816. }
  817. }
  818. if (mFramebuffer->hasDepthAttachment())
  819. {
  820. VulkanImage* image = mFramebuffer->getDepthStencilAttachment().image;
  821. UINT32 imageInfoIdx = mImages[image];
  822. ImageInfo& imageInfo = mImageInfos[imageInfoIdx];
  823. if (imageInfo.isShaderInput && !pipeline->isDepthStencilReadOnly())
  824. {
  825. LOGWRN("Framebuffer attachment also used as a shader input, but depth/stencil writes aren't disabled. "
  826. "This will result in undefined behavior.");
  827. }
  828. }
  829. mGraphicsPipeline->registerPipelineResources(this);
  830. registerResource(pipeline, VulkanUseFlag::Read);
  831. vkCmdBindPipeline(mCmdBuffer, VK_PIPELINE_BIND_POINT_GRAPHICS, pipeline->getHandle());
  832. bindDynamicStates(true);
  833. mGfxPipelineRequiresBind = false;
  834. return true;
  835. }
  836. void VulkanCmdBuffer::bindDynamicStates(bool forceAll)
  837. {
  838. if (mViewportRequiresBind || forceAll)
  839. {
  840. VkViewport viewport;
  841. viewport.x = mViewport.x * mRenderTargetWidth;
  842. viewport.y = mViewport.y * mRenderTargetHeight;
  843. viewport.width = mViewport.width * mRenderTargetWidth;
  844. viewport.height = mViewport.height * mRenderTargetHeight;
  845. viewport.minDepth = 0.0f;
  846. viewport.maxDepth = 1.0f;
  847. vkCmdSetViewport(mCmdBuffer, 0, 1, &viewport);
  848. mViewportRequiresBind = false;
  849. }
  850. if(mStencilRefRequiresBind || forceAll)
  851. {
  852. vkCmdSetStencilReference(mCmdBuffer, VK_STENCIL_FRONT_AND_BACK, mStencilRef);
  853. mStencilRefRequiresBind = false;
  854. }
  855. if(mScissorRequiresBind || forceAll)
  856. {
  857. VkRect2D scissorRect;
  858. if(mGraphicsPipeline->isScissorEnabled())
  859. {
  860. scissorRect.offset.x = mScissor.x;
  861. scissorRect.offset.y = mScissor.y;
  862. scissorRect.extent.width = mScissor.width;
  863. scissorRect.extent.height = mScissor.height;
  864. }
  865. else
  866. {
  867. scissorRect.offset.x = 0;
  868. scissorRect.offset.y = 0;
  869. scissorRect.extent.width = mRenderTargetWidth;
  870. scissorRect.extent.height = mRenderTargetHeight;
  871. }
  872. vkCmdSetScissor(mCmdBuffer, 0, 1, &scissorRect);
  873. mScissorRequiresBind = false;
  874. }
  875. }
  876. void VulkanCmdBuffer::executeLayoutTransitions()
  877. {
  878. auto createLayoutTransitionBarrier = [&](VulkanImage* image, ImageInfo& imageInfo)
  879. {
  880. mLayoutTransitionBarriersTemp.push_back(VkImageMemoryBarrier());
  881. VkImageMemoryBarrier& barrier = mLayoutTransitionBarriersTemp.back();
  882. barrier.sType = VK_STRUCTURE_TYPE_IMAGE_MEMORY_BARRIER;
  883. barrier.pNext = nullptr;
  884. barrier.srcAccessMask = image->getAccessFlags(imageInfo.currentLayout);
  885. barrier.dstAccessMask = imageInfo.accessFlags;
  886. barrier.srcQueueFamilyIndex = mQueueFamily;
  887. barrier.dstQueueFamilyIndex = mQueueFamily;
  888. barrier.oldLayout = imageInfo.currentLayout;
  889. barrier.newLayout = imageInfo.requiredLayout;
  890. barrier.image = image->getHandle();
  891. barrier.subresourceRange = imageInfo.range;
  892. imageInfo.currentLayout = imageInfo.requiredLayout;
  893. };
  894. // Note: These layout transitions will contain transitions for offscreen framebuffer attachments (while they
  895. // transition to shader read-only layout). This can be avoided, since they're immediately used by the render pass
  896. // as color attachments, making the layout change redundant.
  897. for (auto& entry : mQueuedLayoutTransitions)
  898. {
  899. UINT32 imageInfoIdx = entry.second;
  900. ImageInfo& imageInfo = mImageInfos[imageInfoIdx];
  901. createLayoutTransitionBarrier(entry.first, imageInfo);
  902. }
  903. mQueuedLayoutTransitions.clear();
  904. vkCmdPipelineBarrier(mCmdBuffer,
  905. VK_PIPELINE_STAGE_TOP_OF_PIPE_BIT, // Note: VK_PIPELINE_STAGE_BOTTOM_OF_PIPE_BIT might be more correct here, according to the spec
  906. VK_PIPELINE_STAGE_TOP_OF_PIPE_BIT,
  907. 0, 0, nullptr,
  908. 0, nullptr,
  909. (UINT32)mLayoutTransitionBarriersTemp.size(), mLayoutTransitionBarriersTemp.data());
  910. mLayoutTransitionBarriersTemp.clear();
  911. }
  912. void VulkanCmdBuffer::executeClearPass()
  913. {
  914. assert(mState == State::Recording);
  915. executeLayoutTransitions();
  916. VkRenderPassBeginInfo renderPassBeginInfo;
  917. renderPassBeginInfo.sType = VK_STRUCTURE_TYPE_RENDER_PASS_BEGIN_INFO;
  918. renderPassBeginInfo.pNext = nullptr;
  919. renderPassBeginInfo.framebuffer = mFramebuffer->getFramebuffer(RT_NONE, RT_NONE, mClearMask);
  920. renderPassBeginInfo.renderPass = mFramebuffer->getRenderPass(RT_NONE, RT_NONE, mClearMask);
  921. renderPassBeginInfo.renderArea.offset.x = mClearArea.x;
  922. renderPassBeginInfo.renderArea.offset.y = mClearArea.y;
  923. renderPassBeginInfo.renderArea.extent.width = mClearArea.width;
  924. renderPassBeginInfo.renderArea.extent.height = mClearArea.height;
  925. renderPassBeginInfo.clearValueCount = mFramebuffer->getNumAttachments();
  926. renderPassBeginInfo.pClearValues = mClearValues.data();
  927. vkCmdBeginRenderPass(mCmdBuffer, &renderPassBeginInfo, VK_SUBPASS_CONTENTS_INLINE);
  928. vkCmdEndRenderPass(mCmdBuffer);
  929. mClearMask = CLEAR_NONE;
  930. }
  931. void VulkanCmdBuffer::draw(UINT32 vertexOffset, UINT32 vertexCount, UINT32 instanceCount)
  932. {
  933. if (!isReadyForRender())
  934. return;
  935. if (!isInRenderPass())
  936. beginRenderPass();
  937. if (mGfxPipelineRequiresBind)
  938. {
  939. if (!bindGraphicsPipeline())
  940. return;
  941. }
  942. else
  943. bindDynamicStates(false);
  944. if (mDescriptorSetsBindState.isSet(DescriptorSetBindFlag::Graphics))
  945. {
  946. if (mNumBoundDescriptorSets > 0)
  947. {
  948. UINT32 deviceIdx = mDevice.getIndex();
  949. VkPipelineLayout pipelineLayout = mGraphicsPipeline->getPipelineLayout(deviceIdx);
  950. vkCmdBindDescriptorSets(mCmdBuffer, VK_PIPELINE_BIND_POINT_GRAPHICS, pipelineLayout, 0,
  951. mNumBoundDescriptorSets, mDescriptorSetsTemp, 0, nullptr);
  952. }
  953. mDescriptorSetsBindState.unset(DescriptorSetBindFlag::Graphics);
  954. }
  955. vkCmdDraw(mCmdBuffer, vertexCount, instanceCount, vertexOffset, 0);
  956. }
  957. void VulkanCmdBuffer::drawIndexed(UINT32 startIndex, UINT32 indexCount, UINT32 vertexOffset, UINT32 instanceCount)
  958. {
  959. if (!isReadyForRender())
  960. return;
  961. if (!isInRenderPass())
  962. beginRenderPass();
  963. if (mGfxPipelineRequiresBind)
  964. {
  965. if (!bindGraphicsPipeline())
  966. return;
  967. }
  968. else
  969. bindDynamicStates(false);
  970. if (mDescriptorSetsBindState.isSet(DescriptorSetBindFlag::Graphics))
  971. {
  972. if (mNumBoundDescriptorSets > 0)
  973. {
  974. UINT32 deviceIdx = mDevice.getIndex();
  975. VkPipelineLayout pipelineLayout = mGraphicsPipeline->getPipelineLayout(deviceIdx);
  976. vkCmdBindDescriptorSets(mCmdBuffer, VK_PIPELINE_BIND_POINT_GRAPHICS, pipelineLayout, 0,
  977. mNumBoundDescriptorSets, mDescriptorSetsTemp, 0, nullptr);
  978. }
  979. mDescriptorSetsBindState.unset(DescriptorSetBindFlag::Graphics);
  980. }
  981. vkCmdDrawIndexed(mCmdBuffer, indexCount, instanceCount, startIndex, vertexOffset, 0);
  982. }
  983. void VulkanCmdBuffer::dispatch(UINT32 numGroupsX, UINT32 numGroupsY, UINT32 numGroupsZ)
  984. {
  985. if (mComputePipeline == nullptr)
  986. return;
  987. if (isInRenderPass())
  988. endRenderPass();
  989. UINT32 deviceIdx = mDevice.getIndex();
  990. if(mCmpPipelineRequiresBind)
  991. {
  992. VulkanPipeline* pipeline = mComputePipeline->getPipeline(deviceIdx);
  993. if (pipeline == nullptr)
  994. return;
  995. registerResource(pipeline, VulkanUseFlag::Read);
  996. mComputePipeline->registerPipelineResources(this);
  997. vkCmdBindPipeline(mCmdBuffer, VK_PIPELINE_BIND_POINT_COMPUTE, pipeline->getHandle());
  998. mCmpPipelineRequiresBind = false;
  999. }
  1000. if(mDescriptorSetsBindState.isSet(DescriptorSetBindFlag::Compute))
  1001. {
  1002. if (mNumBoundDescriptorSets > 0)
  1003. {
  1004. VkPipelineLayout pipelineLayout = mComputePipeline->getPipelineLayout(deviceIdx);
  1005. vkCmdBindDescriptorSets(mCmdBuffer, VK_PIPELINE_BIND_POINT_COMPUTE, pipelineLayout, 0,
  1006. mNumBoundDescriptorSets, mDescriptorSetsTemp, 0, nullptr);
  1007. }
  1008. mDescriptorSetsBindState.unset(DescriptorSetBindFlag::Compute);
  1009. }
  1010. vkCmdDispatch(mCmdBuffer, numGroupsX, numGroupsY, numGroupsZ);
  1011. }
  1012. void VulkanCmdBuffer::registerResource(VulkanResource* res, VulkanUseFlags flags)
  1013. {
  1014. auto insertResult = mResources.insert(std::make_pair(res, ResourceUseHandle()));
  1015. if(insertResult.second) // New element
  1016. {
  1017. ResourceUseHandle& useHandle = insertResult.first->second;
  1018. useHandle.used = false;
  1019. useHandle.flags = flags;
  1020. res->notifyBound();
  1021. }
  1022. else // Existing element
  1023. {
  1024. ResourceUseHandle& useHandle = insertResult.first->second;
  1025. assert(!useHandle.used);
  1026. useHandle.flags |= flags;
  1027. }
  1028. }
  1029. void VulkanCmdBuffer::registerResource(VulkanImage* res, VkAccessFlags accessFlags, VkImageLayout currentLayout,
  1030. VkImageLayout newLayout, VulkanUseFlags flags, bool isFBAttachment)
  1031. {
  1032. // Note: I currently always perform pipeline barriers (layout transitions and similar), over the entire image.
  1033. // In the case of render and storage images, the case is often that only a specific subresource requires
  1034. // it. However this makes grouping and tracking of current image layouts much more difficult.
  1035. // If this is ever requires we'll need to track image layout per-subresource instead per-image, and we
  1036. // might also need a smart way to group layout transitions for multiple sub-resources on the same image.
  1037. VkImageSubresourceRange range = res->getRange();
  1038. UINT32 nextImageInfoIdx = (UINT32)mImageInfos.size();
  1039. auto insertResult = mImages.insert(std::make_pair(res, nextImageInfoIdx));
  1040. if (insertResult.second) // New element
  1041. {
  1042. UINT32 imageInfoIdx = insertResult.first->second;
  1043. mImageInfos.push_back(ImageInfo());
  1044. ImageInfo& imageInfo = mImageInfos[imageInfoIdx];
  1045. imageInfo.accessFlags = accessFlags;
  1046. imageInfo.currentLayout = currentLayout;
  1047. imageInfo.requiredLayout = newLayout;
  1048. imageInfo.finalLayout = newLayout;
  1049. imageInfo.range = range;
  1050. imageInfo.isFBAttachment = isFBAttachment;
  1051. imageInfo.isShaderInput = !isFBAttachment;
  1052. imageInfo.useHandle.used = false;
  1053. imageInfo.useHandle.flags = flags;
  1054. res->notifyBound();
  1055. if (imageInfo.currentLayout != imageInfo.requiredLayout)
  1056. mQueuedLayoutTransitions[res] = imageInfoIdx;
  1057. }
  1058. else // Existing element
  1059. {
  1060. UINT32 imageInfoIdx = insertResult.first->second;
  1061. ImageInfo& imageInfo = mImageInfos[imageInfoIdx];
  1062. assert(!imageInfo.useHandle.used);
  1063. imageInfo.useHandle.flags |= flags;
  1064. imageInfo.accessFlags |= accessFlags;
  1065. // Check if the same image is used with different layouts, in which case we need to transfer to the general
  1066. // layout
  1067. if (imageInfo.requiredLayout != newLayout)
  1068. imageInfo.requiredLayout = VK_IMAGE_LAYOUT_GENERAL;
  1069. // If attached to FB, then the final layout is set by the FB (provided as layout param here), otherwise its
  1070. // the same as required layout
  1071. if(!isFBAttachment && !imageInfo.isFBAttachment)
  1072. imageInfo.finalLayout = imageInfo.requiredLayout;
  1073. else
  1074. {
  1075. if (isFBAttachment)
  1076. imageInfo.finalLayout = newLayout;
  1077. }
  1078. if (imageInfo.currentLayout != imageInfo.requiredLayout)
  1079. mQueuedLayoutTransitions[res] = imageInfoIdx;
  1080. // If a FB attachment was just bound as a shader input, we might need to restart the render pass with a FB
  1081. // attachment that supports read-only attachments using the GENERAL layout
  1082. bool requiresReadOnlyFB = false;
  1083. if (isFBAttachment)
  1084. {
  1085. if (!imageInfo.isFBAttachment)
  1086. {
  1087. imageInfo.isFBAttachment = true;
  1088. requiresReadOnlyFB = imageInfo.isShaderInput;
  1089. }
  1090. }
  1091. else
  1092. {
  1093. if (!imageInfo.isShaderInput)
  1094. {
  1095. imageInfo.isShaderInput = true;
  1096. requiresReadOnlyFB = imageInfo.isFBAttachment;
  1097. }
  1098. }
  1099. // If we need to switch frame-buffers, end current render pass
  1100. if (requiresReadOnlyFB && isInRenderPass())
  1101. endRenderPass();
  1102. }
  1103. // Register any sub-resources
  1104. for(UINT32 i = 0; i < range.layerCount; i++)
  1105. {
  1106. for(UINT32 j = 0; j < range.levelCount; j++)
  1107. {
  1108. UINT32 layer = range.baseArrayLayer + i;
  1109. UINT32 mipLevel = range.baseMipLevel + j;
  1110. registerResource(res->getSubresource(layer, mipLevel), flags);
  1111. }
  1112. }
  1113. }
  1114. void VulkanCmdBuffer::registerResource(VulkanBuffer* res, VkAccessFlags accessFlags, VulkanUseFlags flags)
  1115. {
  1116. auto insertResult = mBuffers.insert(std::make_pair(res, BufferInfo()));
  1117. if (insertResult.second) // New element
  1118. {
  1119. BufferInfo& bufferInfo = insertResult.first->second;
  1120. bufferInfo.accessFlags = accessFlags;
  1121. bufferInfo.useHandle.used = false;
  1122. bufferInfo.useHandle.flags = flags;
  1123. res->notifyBound();
  1124. }
  1125. else // Existing element
  1126. {
  1127. BufferInfo& bufferInfo = insertResult.first->second;
  1128. assert(!bufferInfo.useHandle.used);
  1129. bufferInfo.useHandle.flags |= flags;
  1130. bufferInfo.accessFlags |= accessFlags;
  1131. }
  1132. }
  1133. void VulkanCmdBuffer::registerResource(VulkanFramebuffer* res, VulkanUseFlags flags)
  1134. {
  1135. auto insertResult = mResources.insert(std::make_pair(res, ResourceUseHandle()));
  1136. if (insertResult.second) // New element
  1137. {
  1138. ResourceUseHandle& useHandle = insertResult.first->second;
  1139. useHandle.used = false;
  1140. useHandle.flags = flags;
  1141. res->notifyBound();
  1142. }
  1143. else // Existing element
  1144. {
  1145. ResourceUseHandle& useHandle = insertResult.first->second;
  1146. assert(!useHandle.used);
  1147. useHandle.flags |= flags;
  1148. }
  1149. // Register any sub-resources
  1150. UINT32 numColorAttachments = res->getNumColorAttachments();
  1151. for (UINT32 i = 0; i < numColorAttachments; i++)
  1152. {
  1153. const VulkanFramebufferAttachment& attachment = res->getColorAttachment(i);
  1154. VkAccessFlags accessMask;
  1155. if (attachment.finalLayout == VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL)
  1156. accessMask = VK_ACCESS_SHADER_READ_BIT;
  1157. else
  1158. {
  1159. assert(attachment.finalLayout == VK_IMAGE_LAYOUT_PRESENT_SRC_KHR);
  1160. accessMask = VK_ACCESS_MEMORY_READ_BIT;
  1161. }
  1162. registerResource(attachment.image, accessMask, attachment.image->getLayout(), attachment.finalLayout,
  1163. VulkanUseFlag::Write, true);
  1164. }
  1165. if(res->hasDepthAttachment())
  1166. {
  1167. const VulkanFramebufferAttachment& attachment = res->getDepthStencilAttachment();
  1168. VkAccessFlags accessMask = VK_ACCESS_DEPTH_STENCIL_ATTACHMENT_WRITE_BIT | VK_ACCESS_DEPTH_STENCIL_ATTACHMENT_READ_BIT;
  1169. registerResource(attachment.image, accessMask, attachment.image->getLayout(), attachment.finalLayout,
  1170. VulkanUseFlag::Write, true);
  1171. }
  1172. }
  1173. VulkanCommandBuffer::VulkanCommandBuffer(VulkanDevice& device, GpuQueueType type, UINT32 deviceIdx,
  1174. UINT32 queueIdx, bool secondary)
  1175. : CommandBuffer(type, deviceIdx, queueIdx, secondary), mBuffer(nullptr)
  1176. , mDevice(device), mQueue(nullptr), mIdMask(0)
  1177. {
  1178. UINT32 numQueues = device.getNumQueues(mType);
  1179. if (numQueues == 0) // Fall back to graphics queue
  1180. {
  1181. mType = GQT_GRAPHICS;
  1182. numQueues = device.getNumQueues(GQT_GRAPHICS);
  1183. }
  1184. mQueue = device.getQueue(mType, mQueueIdx % numQueues);
  1185. mIdMask = device.getQueueMask(mType, mQueueIdx);
  1186. acquireNewBuffer();
  1187. }
  1188. void VulkanCommandBuffer::acquireNewBuffer()
  1189. {
  1190. VulkanCmdBufferPool& pool = mDevice.getCmdBufferPool();
  1191. if (mBuffer != nullptr)
  1192. assert(mBuffer->isSubmitted());
  1193. UINT32 queueFamily = mDevice.getQueueFamily(mType);
  1194. mBuffer = pool.getBuffer(queueFamily, mIsSecondary);
  1195. }
  1196. void VulkanCommandBuffer::submit(UINT32 syncMask)
  1197. {
  1198. // Ignore myself
  1199. syncMask &= ~mIdMask;
  1200. if (mBuffer->isInRenderPass())
  1201. mBuffer->endRenderPass();
  1202. if (mBuffer->isRecording())
  1203. mBuffer->end();
  1204. if (!mBuffer->isReadyForSubmit()) // Possibly nothing was recorded in the buffer
  1205. return;
  1206. mBuffer->submit(mQueue, mQueueIdx, syncMask);
  1207. acquireNewBuffer();
  1208. gVulkanCBManager().refreshStates(mDeviceIdx);
  1209. }
  1210. }