BsVulkanHardwareBuffer.cpp 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612
  1. //********************************** Banshee Engine (www.banshee3d.com) **************************************************//
  2. //**************** Copyright (c) 2016 Marko Pintera ([email protected]). All rights reserved. **********************//
  3. #include "BsVulkanHardwareBuffer.h"
  4. #include "BsVulkanRenderAPI.h"
  5. #include "BsVulkanDevice.h"
  6. #include "BsVulkanUtility.h"
  7. #include "Managers/BsVulkanCommandBufferManager.h"
  8. #include "BsVulkanCommandBuffer.h"
  9. #include "BsVulkanTexture.h"
  10. namespace bs { namespace ct
  11. {
  12. VulkanBuffer::VulkanBuffer(VulkanResourceManager* owner, VkBuffer buffer, VkBufferView view, VkDeviceMemory memory,
  13. UINT32 rowPitch, UINT32 slicePitch)
  14. : VulkanResource(owner, false), mBuffer(buffer), mView(view), mMemory(memory), mRowPitch(rowPitch)
  15. {
  16. if (rowPitch != 0)
  17. mSliceHeight = slicePitch / rowPitch;
  18. else
  19. mSliceHeight = 0;
  20. }
  21. VulkanBuffer::~VulkanBuffer()
  22. {
  23. VulkanDevice& device = mOwner->getDevice();
  24. if (mView != VK_NULL_HANDLE)
  25. vkDestroyBufferView(device.getLogical(), mView, gVulkanAllocator);
  26. vkDestroyBuffer(device.getLogical(), mBuffer, gVulkanAllocator);
  27. device.freeMemory(mMemory);
  28. }
  29. UINT8* VulkanBuffer::map(VkDeviceSize offset, VkDeviceSize length) const
  30. {
  31. VulkanDevice& device = mOwner->getDevice();
  32. UINT8* data;
  33. VkResult result = vkMapMemory(device.getLogical(), mMemory, offset, length, 0, (void**)&data);
  34. assert(result == VK_SUCCESS);
  35. return data;
  36. }
  37. void VulkanBuffer::unmap()
  38. {
  39. VulkanDevice& device = mOwner->getDevice();
  40. vkUnmapMemory(device.getLogical(), mMemory);
  41. }
  42. void VulkanBuffer::copy(VulkanCmdBuffer* cb, VulkanBuffer* destination, VkDeviceSize srcOffset,
  43. VkDeviceSize dstOffset, VkDeviceSize length)
  44. {
  45. VkBufferCopy region;
  46. region.size = length;
  47. region.srcOffset = srcOffset;
  48. region.dstOffset = dstOffset;
  49. vkCmdCopyBuffer(cb->getHandle(), mBuffer, destination->getHandle(), 1, &region);
  50. }
  51. void VulkanBuffer::copy(VulkanCmdBuffer* cb, VulkanImage* destination, const VkExtent3D& extent,
  52. const VkImageSubresourceLayers& range, VkImageLayout layout)
  53. {
  54. VkBufferImageCopy region;
  55. region.bufferRowLength = mRowPitch;
  56. region.bufferImageHeight = mSliceHeight;
  57. region.bufferOffset = 0;
  58. region.imageOffset.x = 0;
  59. region.imageOffset.y = 0;
  60. region.imageOffset.z = 0;
  61. region.imageExtent = extent;
  62. region.imageSubresource = range;
  63. vkCmdCopyBufferToImage(cb->getHandle(), mBuffer, destination->getHandle(), layout, 1, &region);
  64. }
  65. void VulkanBuffer::update(VulkanCmdBuffer* cb, UINT8* data, VkDeviceSize offset, VkDeviceSize length)
  66. {
  67. vkCmdUpdateBuffer(cb->getHandle(), mBuffer, offset, length, (uint32_t*)data);
  68. }
  69. VulkanHardwareBuffer::VulkanHardwareBuffer(BufferType type, GpuBufferFormat format, GpuBufferUsage usage,
  70. UINT32 size, GpuDeviceFlags deviceMask)
  71. : HardwareBuffer(size), mBuffers(), mStagingBuffer(nullptr), mStagingMemory(nullptr), mMappedDeviceIdx(-1)
  72. , mMappedGlobalQueueIdx(-1), mMappedOffset(0), mMappedSize(0), mMappedLockOptions(GBL_WRITE_ONLY)
  73. , mDirectlyMappable((usage & GBU_DYNAMIC) != 0), mSupportsGPUWrites(type == BT_STORAGE), mRequiresView(false)
  74. , mIsMapped(false)
  75. {
  76. VkBufferUsageFlags usageFlags = 0;
  77. switch(type)
  78. {
  79. case BT_VERTEX:
  80. usageFlags = VK_BUFFER_USAGE_TRANSFER_DST_BIT | VK_BUFFER_USAGE_VERTEX_BUFFER_BIT;
  81. break;
  82. case BT_INDEX:
  83. usageFlags = VK_BUFFER_USAGE_TRANSFER_DST_BIT | VK_BUFFER_USAGE_INDEX_BUFFER_BIT;
  84. break;
  85. case BT_UNIFORM:
  86. usageFlags = VK_BUFFER_USAGE_TRANSFER_DST_BIT | VK_BUFFER_USAGE_UNIFORM_BUFFER_BIT;
  87. break;
  88. case BT_GENERIC:
  89. usageFlags = VK_BUFFER_USAGE_TRANSFER_DST_BIT | VK_BUFFER_USAGE_UNIFORM_TEXEL_BUFFER_BIT;
  90. mRequiresView = true;
  91. break;
  92. case BT_STORAGE:
  93. usageFlags = VK_BUFFER_USAGE_TRANSFER_DST_BIT | VK_BUFFER_USAGE_UNIFORM_TEXEL_BUFFER_BIT |
  94. VK_BUFFER_USAGE_STORAGE_TEXEL_BUFFER_BIT;
  95. mRequiresView = true;
  96. break;
  97. case BT_STRUCTURED:
  98. usageFlags = VK_BUFFER_USAGE_TRANSFER_DST_BIT | VK_BUFFER_USAGE_STORAGE_BUFFER_BIT;
  99. break;
  100. }
  101. mBufferCI.sType = VK_STRUCTURE_TYPE_BUFFER_CREATE_INFO;
  102. mBufferCI.pNext = nullptr;
  103. mBufferCI.flags = 0;
  104. mBufferCI.sharingMode = VK_SHARING_MODE_EXCLUSIVE;
  105. mBufferCI.usage = usageFlags;
  106. mBufferCI.queueFamilyIndexCount = 0;
  107. mBufferCI.pQueueFamilyIndices = nullptr;
  108. mViewCI.sType = VK_STRUCTURE_TYPE_BUFFER_VIEW_CREATE_INFO;
  109. mViewCI.pNext = nullptr;
  110. mViewCI.flags = 0;
  111. mViewCI.format = VulkanUtility::getBufferFormat(format);
  112. mViewCI.offset = 0;
  113. mViewCI.range = VK_WHOLE_SIZE;
  114. VulkanRenderAPI& rapi = static_cast<VulkanRenderAPI&>(RenderAPI::instance());
  115. VulkanDevice* devices[BS_MAX_DEVICES];
  116. VulkanUtility::getDevices(rapi, deviceMask, devices);
  117. // Allocate buffers per-device
  118. for (UINT32 i = 0; i < BS_MAX_DEVICES; i++)
  119. {
  120. if (devices[i] == nullptr)
  121. continue;
  122. mBuffers[i] = createBuffer(*devices[i], size, false, true);
  123. }
  124. }
  125. VulkanHardwareBuffer::~VulkanHardwareBuffer()
  126. {
  127. for (UINT32 i = 0; i < BS_MAX_DEVICES; i++)
  128. {
  129. if (mBuffers[i] == nullptr)
  130. continue;
  131. mBuffers[i]->destroy();
  132. }
  133. assert(mStagingBuffer == nullptr);
  134. }
  135. VulkanBuffer* VulkanHardwareBuffer::createBuffer(VulkanDevice& device, UINT32 size, bool staging, bool readable)
  136. {
  137. VkBufferUsageFlags usage = mBufferCI.usage;
  138. if (staging)
  139. {
  140. mBufferCI.usage = VK_BUFFER_USAGE_TRANSFER_SRC_BIT;
  141. // Staging buffers are used as a destination for reads
  142. if (readable)
  143. mBufferCI.usage |= VK_BUFFER_USAGE_TRANSFER_DST_BIT;
  144. }
  145. else if(readable) // Non-staging readable
  146. mBufferCI.usage |= VK_BUFFER_USAGE_TRANSFER_SRC_BIT;
  147. mBufferCI.size = size;
  148. VkMemoryPropertyFlags flags = (mDirectlyMappable || staging) ?
  149. (VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT | VK_MEMORY_PROPERTY_HOST_COHERENT_BIT) : // Note: Try using cached memory
  150. VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT;
  151. VkDevice vkDevice = device.getLogical();
  152. VkBuffer buffer;
  153. VkResult result = vkCreateBuffer(vkDevice, &mBufferCI, gVulkanAllocator, &buffer);
  154. assert(result == VK_SUCCESS);
  155. VkMemoryRequirements memReqs;
  156. vkGetBufferMemoryRequirements(vkDevice, buffer, &memReqs);
  157. VkDeviceMemory memory = device.allocateMemory(memReqs, flags);
  158. result = vkBindBufferMemory(vkDevice, buffer, memory, 0);
  159. assert(result == VK_SUCCESS);
  160. VkBufferView view;
  161. if (mRequiresView && !staging)
  162. {
  163. mViewCI.buffer = buffer;
  164. result = vkCreateBufferView(vkDevice, &mViewCI, gVulkanAllocator, &view);
  165. assert(result == VK_SUCCESS);
  166. }
  167. else
  168. view = VK_NULL_HANDLE;
  169. mBufferCI.usage = usage; // Restore original usage
  170. return device.getResourceManager().create<VulkanBuffer>(buffer, view, memory);
  171. }
  172. void* VulkanHardwareBuffer::map(UINT32 offset, UINT32 length, GpuLockOptions options, UINT32 deviceIdx, UINT32 queueIdx)
  173. {
  174. if ((offset + length) > mSize)
  175. {
  176. LOGERR("Provided offset(" + toString(offset) + ") + length(" + toString(length) + ") "
  177. "is larger than the buffer " + toString(mSize) + ".");
  178. return nullptr;
  179. }
  180. if (length == 0)
  181. return nullptr;
  182. VulkanBuffer* buffer = mBuffers[deviceIdx];
  183. if (buffer == nullptr)
  184. return nullptr;
  185. mIsMapped = true;
  186. mMappedDeviceIdx = deviceIdx;
  187. mMappedGlobalQueueIdx = queueIdx;
  188. mMappedOffset = offset;
  189. mMappedSize = length;
  190. mMappedLockOptions = options;
  191. VulkanRenderAPI& rapi = static_cast<VulkanRenderAPI&>(RenderAPI::instance());
  192. VulkanDevice& device = *rapi._getDevice(deviceIdx);
  193. VulkanCommandBufferManager& cbManager = gVulkanCBManager();
  194. GpuQueueType queueType;
  195. UINT32 localQueueIdx = CommandSyncMask::getQueueIdxAndType(queueIdx, queueType);
  196. VkAccessFlags accessFlags;
  197. if (options == GBL_READ_ONLY)
  198. accessFlags = VK_ACCESS_HOST_READ_BIT;
  199. else if (options == GBL_READ_WRITE)
  200. accessFlags = VK_ACCESS_HOST_READ_BIT | VK_ACCESS_HOST_WRITE_BIT;
  201. else
  202. accessFlags = VK_ACCESS_HOST_WRITE_BIT;
  203. // If memory is host visible try mapping it directly
  204. if(mDirectlyMappable)
  205. {
  206. // Check is the GPU currently reading or writing from the buffer
  207. UINT32 useMask = buffer->getUseInfo(VulkanUseFlag::Read | VulkanUseFlag::Write);
  208. // Note: Even if GPU isn't currently using the buffer, but the buffer supports GPU writes, we consider it as
  209. // being used because the write could have completed yet still not visible, so we need to issue a pipeline
  210. // barrier below.
  211. bool isUsedOnGPU = useMask != 0 || mSupportsGPUWrites;
  212. // We're safe to map directly since GPU isn't using the buffer
  213. if (!isUsedOnGPU)
  214. {
  215. // If some CB has an operation queued that will be using the current contents of the buffer, create a new
  216. // buffer so we don't modify the previous use of the buffer
  217. if(buffer->isBound())
  218. {
  219. VulkanBuffer* newBuffer = createBuffer(device, mSize, false, true);
  220. // Copy contents of the current buffer to the new one, unless caller explicitly specifies he doesn't
  221. // care about the current contents
  222. if (options != GBL_WRITE_ONLY_DISCARD)
  223. {
  224. UINT8* src = buffer->map(offset, length);
  225. UINT8* dst = newBuffer->map(offset, length);
  226. memcpy(dst, src, length);
  227. buffer->unmap();
  228. newBuffer->unmap();
  229. }
  230. buffer->destroy();
  231. buffer = newBuffer;
  232. mBuffers[deviceIdx] = buffer;
  233. }
  234. return buffer->map(offset, length);
  235. }
  236. // Caller guarantees he won't touch the same data as the GPU, so just map even though the GPU is using the buffer
  237. if (options == GBL_WRITE_ONLY_NO_OVERWRITE)
  238. return buffer->map(offset, length);
  239. // Caller doesn't care about buffer contents, so just discard the existing buffer and create a new one
  240. if (options == GBL_WRITE_ONLY_DISCARD)
  241. {
  242. buffer->destroy();
  243. buffer = createBuffer(device, mSize, false, true);
  244. mBuffers[deviceIdx] = buffer;
  245. return buffer->map(offset, length);
  246. }
  247. // We need to read the buffer contents
  248. if(options == GBL_READ_ONLY || options == GBL_READ_WRITE)
  249. {
  250. // We need to wait until (potential) read/write operations complete
  251. VulkanTransferBuffer* transferCB = cbManager.getTransferBuffer(deviceIdx, queueType, localQueueIdx);
  252. // Ensure flush() will wait for all queues currently using to the buffer (if any) to finish
  253. // If only reading, wait for all writes to complete, otherwise wait on both writes and reads
  254. if (options == GBL_READ_ONLY)
  255. useMask = buffer->getUseInfo(VulkanUseFlag::Write);
  256. else
  257. useMask = buffer->getUseInfo(VulkanUseFlag::Read | VulkanUseFlag::Write);
  258. transferCB->appendMask(useMask);
  259. // Make any writes visible before mapping
  260. if (mSupportsGPUWrites)
  261. {
  262. // Issue a barrier so :
  263. // - If reading: the device makes the written memory available for read (read-after-write hazard)
  264. // - If writing: ensures our writes properly overlap with GPU writes (write-after-write hazard)
  265. transferCB->memoryBarrier(buffer->getHandle(),
  266. VK_ACCESS_SHADER_WRITE_BIT,
  267. accessFlags,
  268. // Last stages that could have written to the buffer:
  269. VK_PIPELINE_STAGE_FRAGMENT_SHADER_BIT | VK_PIPELINE_STAGE_COMPUTE_SHADER_BIT,
  270. VK_PIPELINE_STAGE_HOST_BIT
  271. );
  272. }
  273. // Submit the command buffer and wait until it finishes
  274. transferCB->flush(true);
  275. // If writing and some CB has an operation queued that will be using the current contents of the buffer,
  276. // create a new buffer so we don't modify the previous use of the buffer
  277. if (options == GBL_READ_WRITE && buffer->isBound())
  278. {
  279. VulkanBuffer* newBuffer = createBuffer(device, mSize, false, true);
  280. // Copy contents of the current buffer to the new one
  281. UINT8* src = buffer->map(offset, length);
  282. UINT8* dst = newBuffer->map(offset, length);
  283. memcpy(dst, src, length);
  284. buffer->unmap();
  285. newBuffer->unmap();
  286. buffer->destroy();
  287. buffer = newBuffer;
  288. mBuffers[deviceIdx] = buffer;
  289. }
  290. return buffer->map(offset, length);
  291. }
  292. // Otherwise, we're doing write only, in which case it's best to use the staging buffer to avoid waiting
  293. // and blocking, so fall through
  294. }
  295. // Can't use direct mapping, so use a staging buffer or memory
  296. // We might need to copy the current contents of the buffer to the staging buffer. Even if the user doesn't plan on
  297. // reading, it is still required as we will eventually copy all of the contents back to the original buffer,
  298. // and we can't write potentially uninitialized data. The only exception is when the caller specifies the buffer
  299. // contents should be discarded in which he guarantees he will overwrite the entire locked area with his own
  300. // contents.
  301. bool needRead = options != GBL_WRITE_ONLY_DISCARD_RANGE && options != GBL_WRITE_ONLY_DISCARD;
  302. // See if we can use the cheaper staging memory, rather than a staging buffer
  303. if(!needRead && offset % 4 == 0 && length % 4 == 0 && length <= 65536)
  304. {
  305. mStagingMemory = (UINT8*)bs_alloc(length);
  306. return mStagingMemory;
  307. }
  308. // Create a staging buffer
  309. mStagingBuffer = createBuffer(device, length, true, needRead);
  310. if (needRead)
  311. {
  312. VulkanTransferBuffer* transferCB = cbManager.getTransferBuffer(deviceIdx, queueType, localQueueIdx);
  313. // Similar to above, if buffer supports GPU writes or is currently being written to, we need to wait on any
  314. // potential writes to complete
  315. UINT32 writeUseMask = buffer->getUseInfo(VulkanUseFlag::Write);
  316. if(mSupportsGPUWrites || writeUseMask != 0)
  317. {
  318. // Ensure flush() will wait for all queues currently writing to the buffer (if any) to finish
  319. transferCB->appendMask(writeUseMask);
  320. }
  321. // Queue copy command
  322. buffer->copy(transferCB->getCB(), mStagingBuffer, offset, 0, length);
  323. // Ensure data written to the staging buffer is visible
  324. transferCB->memoryBarrier(mStagingBuffer->getHandle(),
  325. VK_ACCESS_TRANSFER_WRITE_BIT,
  326. accessFlags,
  327. VK_PIPELINE_STAGE_TRANSFER_BIT,
  328. VK_PIPELINE_STAGE_HOST_BIT
  329. );
  330. // Submit the command buffer and wait until it finishes
  331. transferCB->flush(true);
  332. assert(!buffer->isUsed());
  333. }
  334. return mStagingBuffer->map(0, length);
  335. }
  336. void VulkanHardwareBuffer::unmap()
  337. {
  338. // Possibly map() failed with some error
  339. if (!mIsMapped)
  340. return;
  341. // Note: If we did any writes they need to be made visible to the GPU. However there is no need to execute
  342. // a pipeline barrier because (as per spec) host writes are implicitly visible to the device.
  343. if(mStagingMemory == nullptr && mStagingBuffer == nullptr) // We directly mapped the buffer
  344. {
  345. mBuffers[mMappedDeviceIdx]->unmap();
  346. }
  347. else
  348. {
  349. if(mStagingBuffer != nullptr)
  350. mStagingBuffer->unmap();
  351. bool isWrite = mMappedLockOptions != GBL_READ_ONLY;
  352. // We the caller wrote anything to the staging buffer, we need to upload it back to the main buffer
  353. if(isWrite)
  354. {
  355. VulkanRenderAPI& rapi = static_cast<VulkanRenderAPI&>(RenderAPI::instance());
  356. VulkanDevice& device = *rapi._getDevice(mMappedDeviceIdx);
  357. VulkanCommandBufferManager& cbManager = gVulkanCBManager();
  358. GpuQueueType queueType;
  359. UINT32 localQueueIdx = CommandSyncMask::getQueueIdxAndType(mMappedGlobalQueueIdx, queueType);
  360. VulkanBuffer* buffer = mBuffers[mMappedDeviceIdx];
  361. VulkanTransferBuffer* transferCB = cbManager.getTransferBuffer(mMappedDeviceIdx, queueType, localQueueIdx);
  362. // If the buffer is used in any way on the GPU, we need to wait for that use to finish before
  363. // we issue our copy
  364. UINT32 useMask = buffer->getUseInfo(VulkanUseFlag::Read | VulkanUseFlag::Write);
  365. bool isNormalWrite = false;
  366. if(useMask != 0) // Buffer is currently used on the GPU
  367. {
  368. // Try to avoid the wait by checking for special write conditions
  369. // Caller guarantees he won't touch the same data as the GPU, so just copy
  370. if (mMappedLockOptions == GBL_WRITE_ONLY_NO_OVERWRITE)
  371. {
  372. // Fall through to copy()
  373. }
  374. // Caller doesn't care about buffer contents, so just discard the existing buffer and create a new one
  375. else if (mMappedLockOptions == GBL_WRITE_ONLY_DISCARD)
  376. {
  377. buffer->destroy();
  378. buffer = createBuffer(device, mSize, false, true);
  379. mBuffers[mMappedDeviceIdx] = buffer;
  380. }
  381. else // Otherwise we have no choice but to issue a dependency between the queues
  382. {
  383. transferCB->appendMask(useMask);
  384. isNormalWrite = true;
  385. }
  386. }
  387. else
  388. isNormalWrite = true;
  389. // Check if the buffer will still be bound somewhere after the CBs using it finish
  390. if (isNormalWrite)
  391. {
  392. UINT32 useCount = buffer->getUseCount();
  393. UINT32 boundCount = buffer->getBoundCount();
  394. bool isBoundWithoutUse = boundCount > useCount;
  395. // If buffer is queued for some operation on a CB, then we need to make a copy of the buffer to
  396. // avoid modifying its use in the previous operation
  397. if (isBoundWithoutUse)
  398. {
  399. VulkanBuffer* newBuffer = createBuffer(device, mSize, false, true);
  400. // Avoid copying original contents if the staging buffer completely covers it
  401. if (mMappedOffset > 0 || mMappedSize != mSize)
  402. {
  403. buffer->copy(transferCB->getCB(), newBuffer, 0, 0, mSize);
  404. transferCB->getCB()->registerResource(buffer, VK_ACCESS_TRANSFER_READ_BIT, VulkanUseFlag::Read);
  405. }
  406. buffer->destroy();
  407. buffer = newBuffer;
  408. mBuffers[mMappedDeviceIdx] = buffer;
  409. }
  410. }
  411. // Queue copy/update command
  412. if (mStagingBuffer != nullptr)
  413. {
  414. mStagingBuffer->copy(transferCB->getCB(), buffer, 0, mMappedOffset, mMappedSize);
  415. transferCB->getCB()->registerResource(mStagingBuffer, VK_ACCESS_TRANSFER_READ_BIT, VulkanUseFlag::Read);
  416. }
  417. else // Staging memory
  418. {
  419. buffer->update(transferCB->getCB(), mStagingMemory, mMappedOffset, mMappedSize);
  420. }
  421. transferCB->getCB()->registerResource(buffer, VK_ACCESS_TRANSFER_WRITE_BIT, VulkanUseFlag::Write);
  422. // We don't actually flush the transfer buffer here since it's an expensive operation, but it's instead
  423. // done automatically before next "normal" command buffer submission.
  424. }
  425. if (mStagingBuffer != nullptr)
  426. {
  427. mStagingBuffer->destroy();
  428. mStagingBuffer = nullptr;
  429. }
  430. if(mStagingMemory != nullptr)
  431. {
  432. bs_free(mStagingMemory);
  433. mStagingMemory = nullptr;
  434. }
  435. }
  436. mIsMapped = false;
  437. }
  438. void VulkanHardwareBuffer::copyData(HardwareBuffer& srcBuffer, UINT32 srcOffset,
  439. UINT32 dstOffset, UINT32 length, bool discardWholeBuffer, const SPtr<CommandBuffer>& commandBuffer)
  440. {
  441. if ((dstOffset + length) > mSize)
  442. {
  443. LOGERR("Provided offset(" + toString(dstOffset) + ") + length(" + toString(length) + ") "
  444. "is larger than the destination buffer " + toString(mSize) + ". Copy operation aborted.");
  445. return;
  446. }
  447. if ((srcOffset + length) > srcBuffer.getSize())
  448. {
  449. LOGERR("Provided offset(" + toString(srcOffset) + ") + length(" + toString(length) + ") "
  450. "is larger than the source buffer " + toString(srcBuffer.getSize()) + ". Copy operation aborted.");
  451. return;
  452. }
  453. VulkanHardwareBuffer& vkSource = static_cast<VulkanHardwareBuffer&>(srcBuffer);
  454. VulkanRenderAPI& rapi = static_cast<VulkanRenderAPI&>(RenderAPI::instance());
  455. VulkanCmdBuffer* vkCB;
  456. if (commandBuffer != nullptr)
  457. vkCB = static_cast<VulkanCommandBuffer*>(commandBuffer.get())->getInternal();
  458. else
  459. vkCB = rapi._getMainCommandBuffer()->getInternal();
  460. UINT32 deviceIdx = vkCB->getDeviceIdx();
  461. VulkanBuffer* src = vkSource.mBuffers[deviceIdx];
  462. VulkanBuffer* dst = mBuffers[deviceIdx];
  463. if (src == nullptr || dst == nullptr)
  464. return;
  465. if (vkCB->isInRenderPass())
  466. vkCB->endRenderPass();
  467. src->copy(vkCB, dst, srcOffset, dstOffset, length);
  468. // Notify the command buffer that these resources are being used on it
  469. vkCB->registerResource(src, VK_ACCESS_TRANSFER_READ_BIT, VulkanUseFlag::Read);
  470. vkCB->registerResource(dst, VK_ACCESS_TRANSFER_WRITE_BIT, VulkanUseFlag::Write);
  471. }
  472. void VulkanHardwareBuffer::readData(UINT32 offset, UINT32 length, void* dest, UINT32 deviceIdx, UINT32 queueIdx)
  473. {
  474. void* lockedData = lock(offset, length, GBL_READ_ONLY, deviceIdx, queueIdx);
  475. memcpy(dest, lockedData, length);
  476. unlock();
  477. }
  478. void VulkanHardwareBuffer::writeData(UINT32 offset, UINT32 length, const void* source, BufferWriteType writeFlags,
  479. UINT32 queueIdx)
  480. {
  481. GpuLockOptions lockOptions = GBL_WRITE_ONLY_DISCARD_RANGE;
  482. if (writeFlags == BTW_NO_OVERWRITE)
  483. lockOptions = GBL_WRITE_ONLY_NO_OVERWRITE;
  484. else if (writeFlags == BWT_DISCARD)
  485. lockOptions = GBL_WRITE_ONLY_DISCARD;
  486. // Write to every device
  487. for (UINT32 i = 0; i < BS_MAX_DEVICES; i++)
  488. {
  489. if (mBuffers[i] == nullptr)
  490. continue;
  491. void* lockedData = lock(offset, length, lockOptions, i, queueIdx);
  492. memcpy(lockedData, source, length);
  493. unlock();
  494. }
  495. }
  496. }}