BsVulkanCommandBuffer.cpp 41 KB

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