Buffer.cpp 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272
  1. /**
  2. * Copyright (c) 2006-2022 LOVE Development Team
  3. *
  4. * This software is provided 'as-is', without any express or implied
  5. * warranty. In no event will the authors be held liable for any damages
  6. * arising from the use of this software.
  7. *
  8. * Permission is granted to anyone to use this software for any purpose,
  9. * including commercial applications, and to alter it and redistribute it
  10. * freely, subject to the following restrictions:
  11. *
  12. * 1. The origin of this software must not be misrepresented; you must not
  13. * claim that you wrote the original software. If you use this software
  14. * in a product, an acknowledgment in the product documentation would be
  15. * appreciated but is not required.
  16. * 2. Altered source versions must be plainly marked as such, and must not be
  17. * misrepresented as being the original software.
  18. * 3. This notice may not be removed or altered from any source distribution.
  19. **/
  20. #include "Buffer.h"
  21. #include "Graphics.h"
  22. namespace love
  23. {
  24. namespace graphics
  25. {
  26. namespace vulkan
  27. {
  28. static VkBufferUsageFlags getUsageBit(BufferUsage mode)
  29. {
  30. switch (mode)
  31. {
  32. case BUFFERUSAGE_VERTEX: return VK_BUFFER_USAGE_VERTEX_BUFFER_BIT;
  33. case BUFFERUSAGE_INDEX: return VK_BUFFER_USAGE_INDEX_BUFFER_BIT;
  34. case BUFFERUSAGE_UNIFORM: return VK_BUFFER_USAGE_UNIFORM_BUFFER_BIT;
  35. case BUFFERUSAGE_TEXEL: return VK_BUFFER_USAGE_STORAGE_TEXEL_BUFFER_BIT;
  36. case BUFFERUSAGE_SHADER_STORAGE: return VK_BUFFER_USAGE_STORAGE_BUFFER_BIT;
  37. default:
  38. throw love::Exception("unsupported BufferUsage mode");
  39. }
  40. }
  41. static VkBufferUsageFlags getVulkanUsageFlags(BufferUsageFlags flags)
  42. {
  43. VkBufferUsageFlags vkFlags = 0;
  44. for (int i = 0; i < BUFFERUSAGE_MAX_ENUM; i++)
  45. {
  46. BufferUsageFlags flag = static_cast<BufferUsageFlags>(1u << i);
  47. if (flags & flag)
  48. vkFlags |= getUsageBit((BufferUsage)i);
  49. }
  50. return vkFlags;
  51. }
  52. Buffer::Buffer(love::graphics::Graphics *gfx, const Settings &settings, const std::vector<DataDeclaration> &format, const void *data, size_t size, size_t arraylength)
  53. : love::graphics::Buffer(gfx, settings, format, size, arraylength)
  54. , usageFlags(settings.usageFlags)
  55. , vgfx(dynamic_cast<Graphics*>(gfx))
  56. , zeroInitialize(settings.zeroInitialize)
  57. , initialData(data)
  58. {
  59. loadVolatile();
  60. }
  61. bool Buffer::loadVolatile()
  62. {
  63. allocator = vgfx->getVmaAllocator();
  64. VkBufferCreateInfo bufferInfo{};
  65. bufferInfo.sType = VK_STRUCTURE_TYPE_BUFFER_CREATE_INFO;
  66. bufferInfo.size = getSize();
  67. bufferInfo.usage = VK_BUFFER_USAGE_TRANSFER_DST_BIT | VK_BUFFER_USAGE_TRANSFER_SRC_BIT | getVulkanUsageFlags(usageFlags);
  68. VmaAllocationCreateInfo allocCreateInfo{};
  69. allocCreateInfo.usage = VMA_MEMORY_USAGE_AUTO;
  70. if (dataUsage == BUFFERDATAUSAGE_READBACK)
  71. allocCreateInfo.flags = VMA_ALLOCATION_CREATE_HOST_ACCESS_RANDOM_BIT | VMA_ALLOCATION_CREATE_MAPPED_BIT;
  72. else if ((bufferInfo.usage | VK_BUFFER_USAGE_STORAGE_TEXEL_BUFFER_BIT) || (bufferInfo.usage | VK_BUFFER_USAGE_STORAGE_BUFFER_BIT))
  73. allocCreateInfo.flags = VMA_ALLOCATION_CREATE_DEDICATED_MEMORY_BIT;
  74. auto result = vmaCreateBuffer(allocator, &bufferInfo, &allocCreateInfo, &buffer, &allocation, &allocInfo);
  75. if (result != VK_SUCCESS)
  76. throw love::Exception("failed to create buffer");
  77. if (zeroInitialize)
  78. vkCmdFillBuffer(vgfx->getCommandBufferForDataTransfer(), buffer, 0, VK_WHOLE_SIZE, 0);
  79. if (initialData)
  80. fill(0, size, initialData);
  81. if (usageFlags & BUFFERUSAGEFLAG_TEXEL)
  82. {
  83. VkBufferViewCreateInfo bufferViewInfo{};
  84. bufferViewInfo.buffer = buffer;
  85. bufferViewInfo.sType = VK_STRUCTURE_TYPE_BUFFER_VIEW_CREATE_INFO;
  86. bufferViewInfo.format = Vulkan::getVulkanVertexFormat(getDataMember(0).decl.format);
  87. bufferViewInfo.range = VK_WHOLE_SIZE;
  88. if (vkCreateBufferView(vgfx->getDevice(), &bufferViewInfo, nullptr, &bufferView) != VK_SUCCESS)
  89. throw love::Exception("failed to create texel buffer view");
  90. }
  91. VkMemoryPropertyFlags memoryProperties;
  92. vmaGetAllocationMemoryProperties(allocator, allocation, &memoryProperties);
  93. if (memoryProperties & VK_MEMORY_PROPERTY_HOST_COHERENT_BIT)
  94. coherent = true;
  95. else
  96. coherent = false;
  97. return true;
  98. }
  99. void Buffer::unloadVolatile()
  100. {
  101. if (buffer == VK_NULL_HANDLE)
  102. return;
  103. auto device = vgfx->getDevice();
  104. vgfx->queueCleanUp(
  105. [device=device, allocator=allocator, buffer=buffer, allocation=allocation, bufferView=bufferView](){
  106. vkDeviceWaitIdle(device);
  107. vmaDestroyBuffer(allocator, buffer, allocation);
  108. if (bufferView)
  109. vkDestroyBufferView(device, bufferView, nullptr);
  110. });
  111. buffer = VK_NULL_HANDLE;
  112. bufferView = VK_NULL_HANDLE;
  113. }
  114. Buffer::~Buffer()
  115. {
  116. unloadVolatile();
  117. }
  118. ptrdiff_t Buffer::getHandle() const
  119. {
  120. return (ptrdiff_t) buffer;
  121. }
  122. ptrdiff_t Buffer::getTexelBufferHandle() const
  123. {
  124. return (ptrdiff_t) bufferView;
  125. }
  126. void *Buffer::map(MapType map, size_t offset, size_t size)
  127. {
  128. if (size == 0)
  129. return nullptr;
  130. if (map == MAP_WRITE_INVALIDATE && (isImmutable() || dataUsage == BUFFERDATAUSAGE_READBACK))
  131. return nullptr;
  132. if (map == MAP_READ_ONLY && dataUsage != BUFFERDATAUSAGE_READBACK)
  133. return nullptr;
  134. mappedRange = Range(offset, size);
  135. if (!Range(0, getSize()).contains(mappedRange))
  136. return nullptr;
  137. if (dataUsage == BUFFERDATAUSAGE_READBACK)
  138. {
  139. if (!coherent)
  140. vmaInvalidateAllocation(allocator, allocation, offset, size);
  141. char *data = (char*)allocInfo.pMappedData;
  142. return (void*) (data + offset);
  143. }
  144. else
  145. {
  146. VkBufferCreateInfo bufferInfo{};
  147. bufferInfo.sType = VK_STRUCTURE_TYPE_BUFFER_CREATE_INFO;
  148. bufferInfo.size = size;
  149. bufferInfo.usage = VK_BUFFER_USAGE_TRANSFER_SRC_BIT;
  150. VmaAllocationCreateInfo allocInfo{};
  151. allocInfo.usage = VMA_MEMORY_USAGE_AUTO;
  152. allocInfo.flags = VMA_ALLOCATION_CREATE_HOST_ACCESS_RANDOM_BIT | VMA_ALLOCATION_CREATE_MAPPED_BIT;
  153. if (vmaCreateBuffer(allocator, &bufferInfo, &allocInfo, &stagingBuffer, &stagingAllocation, &stagingAllocInfo) != VK_SUCCESS)
  154. throw love::Exception("failed to create staging buffer");
  155. return stagingAllocInfo.pMappedData;
  156. }
  157. }
  158. bool Buffer::fill(size_t offset, size_t size, const void *data)
  159. {
  160. if (size == 0 || isImmutable() || dataUsage == BUFFERDATAUSAGE_READBACK)
  161. return false;
  162. if (!Range(0, getSize()).contains(Range(offset, size)))
  163. return false;
  164. VkBufferCreateInfo bufferInfo{};
  165. bufferInfo.sType = VK_STRUCTURE_TYPE_BUFFER_CREATE_INFO;
  166. bufferInfo.size = size;
  167. bufferInfo.usage = VK_BUFFER_USAGE_TRANSFER_SRC_BIT;
  168. VmaAllocationCreateInfo allocInfo{};
  169. allocInfo.usage = VMA_MEMORY_USAGE_AUTO;
  170. allocInfo.flags = VMA_ALLOCATION_CREATE_HOST_ACCESS_SEQUENTIAL_WRITE_BIT | VMA_ALLOCATION_CREATE_MAPPED_BIT;
  171. VkBuffer fillBuffer;
  172. VmaAllocation fillAllocation;
  173. VmaAllocationInfo fillAllocInfo;
  174. if (vmaCreateBuffer(allocator, &bufferInfo, &allocInfo, &fillBuffer, &fillAllocation, &fillAllocInfo) != VK_SUCCESS)
  175. throw love::Exception("failed to create fill buffer");
  176. memcpy(fillAllocInfo.pMappedData, data, size);
  177. VkMemoryPropertyFlags memoryProperties;
  178. vmaGetAllocationMemoryProperties(allocator, fillAllocation, &memoryProperties);
  179. if (~memoryProperties & VK_MEMORY_PROPERTY_HOST_COHERENT_BIT)
  180. vmaFlushAllocation(allocator, fillAllocation, 0, size);
  181. VkBufferCopy bufferCopy{};
  182. bufferCopy.srcOffset = 0;
  183. bufferCopy.dstOffset = offset;
  184. bufferCopy.size = size;
  185. vkCmdCopyBuffer(vgfx->getCommandBufferForDataTransfer(), fillBuffer, buffer, 1, &bufferCopy);
  186. vgfx->queueCleanUp([allocator = allocator, fillBuffer = fillBuffer, fillAllocation = fillAllocation]() {
  187. vmaDestroyBuffer(allocator, fillBuffer, fillAllocation);
  188. });
  189. return true;
  190. }
  191. void Buffer::unmap(size_t usedoffset, size_t usedsize)
  192. {
  193. if (dataUsage != BUFFERDATAUSAGE_READBACK)
  194. {
  195. VkBufferCopy bufferCopy{};
  196. bufferCopy.srcOffset = usedoffset - mappedRange.getOffset();
  197. bufferCopy.dstOffset = usedoffset;
  198. bufferCopy.size = usedsize;
  199. VkMemoryPropertyFlags memoryProperties;
  200. vmaGetAllocationMemoryProperties(allocator, stagingAllocation, &memoryProperties);
  201. if (~memoryProperties & VK_MEMORY_PROPERTY_HOST_COHERENT_BIT)
  202. vmaFlushAllocation(allocator, stagingAllocation, bufferCopy.srcOffset, usedsize);
  203. vkCmdCopyBuffer(vgfx->getCommandBufferForDataTransfer(), stagingBuffer, buffer, 1, &bufferCopy);
  204. vgfx->queueCleanUp([allocator = allocator, stagingBuffer = stagingBuffer, stagingAllocation = stagingAllocation]() {
  205. vmaDestroyBuffer(allocator, stagingBuffer, stagingAllocation);
  206. });
  207. }
  208. }
  209. void Buffer::copyTo(love::graphics::Buffer *dest, size_t sourceoffset, size_t destoffset, size_t size)
  210. {
  211. auto commandBuffer = vgfx->getCommandBufferForDataTransfer();
  212. VkBufferCopy bufferCopy{};
  213. bufferCopy.srcOffset = sourceoffset;
  214. bufferCopy.dstOffset = destoffset;
  215. bufferCopy.size = size;
  216. vkCmdCopyBuffer(commandBuffer, buffer, (VkBuffer) dest->getHandle(), 1, &bufferCopy);
  217. }
  218. } // vulkan
  219. } // graphics
  220. } // love