2
0

ComputeSystemVKWithAllocator.cpp 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. // Jolt Physics Library (https://github.com/jrouwe/JoltPhysics)
  2. // SPDX-FileCopyrightText: 2025 Jorrit Rouwe
  3. // SPDX-License-Identifier: MIT
  4. #include <Jolt/Jolt.h>
  5. #ifdef JPH_USE_VK
  6. #include <Jolt/Compute/VK/ComputeSystemVKWithAllocator.h>
  7. #include <Jolt/Compute/VK/ComputeShaderVK.h>
  8. #include <Jolt/Compute/VK/ComputeBufferVK.h>
  9. #include <Jolt/Compute/VK/ComputeQueueVK.h>
  10. JPH_NAMESPACE_BEGIN
  11. JPH_IMPLEMENT_RTTI_VIRTUAL(ComputeSystemVKWithAllocator)
  12. {
  13. JPH_ADD_BASE_CLASS(ComputeSystemVKWithAllocator, ComputeSystemVK)
  14. }
  15. bool ComputeSystemVKWithAllocator::InitializeMemory()
  16. {
  17. // Get memory properties
  18. vkGetPhysicalDeviceMemoryProperties(mPhysicalDevice, &mMemoryProperties);
  19. return true;
  20. }
  21. void ComputeSystemVKWithAllocator::ShutdownMemory()
  22. {
  23. // Free all memory
  24. for (const MemoryCache::value_type &mc : mMemoryCache)
  25. for (const Memory &m : mc.second)
  26. if (m.mOffset == 0)
  27. FreeMemory(*m.mMemory);
  28. mMemoryCache.clear();
  29. }
  30. uint32 ComputeSystemVKWithAllocator::FindMemoryType(uint32 inTypeFilter, VkMemoryPropertyFlags inProperties) const
  31. {
  32. for (uint32 i = 0; i < mMemoryProperties.memoryTypeCount; i++)
  33. if ((inTypeFilter & (1 << i))
  34. && (mMemoryProperties.memoryTypes[i].propertyFlags & inProperties) == inProperties)
  35. return i;
  36. JPH_ASSERT(false, "Failed to find memory type!");
  37. return 0;
  38. }
  39. void ComputeSystemVKWithAllocator::AllocateMemory(VkDeviceSize inSize, uint32 inMemoryTypeBits, VkMemoryPropertyFlags inProperties, MemoryVK &ioMemory)
  40. {
  41. JPH_ASSERT(ioMemory.mMemory == VK_NULL_HANDLE);
  42. ioMemory.mSize = inSize;
  43. ioMemory.mProperties = inProperties;
  44. VkMemoryAllocateInfo alloc_info = {};
  45. alloc_info.sType = VK_STRUCTURE_TYPE_MEMORY_ALLOCATE_INFO;
  46. alloc_info.allocationSize = inSize;
  47. alloc_info.memoryTypeIndex = FindMemoryType(inMemoryTypeBits, inProperties);
  48. vkAllocateMemory(mDevice, &alloc_info, nullptr, &ioMemory.mMemory);
  49. }
  50. void ComputeSystemVKWithAllocator::FreeMemory(MemoryVK &ioMemory)
  51. {
  52. vkFreeMemory(mDevice, ioMemory.mMemory, nullptr);
  53. ioMemory.mMemory = VK_NULL_HANDLE;
  54. }
  55. bool ComputeSystemVKWithAllocator::CreateBuffer(VkDeviceSize inSize, VkBufferUsageFlags inUsage, VkMemoryPropertyFlags inProperties, BufferVK &outBuffer)
  56. {
  57. // Create a new buffer
  58. outBuffer.mSize = inSize;
  59. VkBufferCreateInfo create_info = {};
  60. create_info.sType = VK_STRUCTURE_TYPE_BUFFER_CREATE_INFO;
  61. create_info.size = inSize;
  62. create_info.usage = inUsage;
  63. create_info.sharingMode = VK_SHARING_MODE_EXCLUSIVE;
  64. if (VKFailed(vkCreateBuffer(mDevice, &create_info, nullptr, &outBuffer.mBuffer)))
  65. {
  66. outBuffer.mBuffer = VK_NULL_HANDLE;
  67. return false;
  68. }
  69. VkMemoryRequirements mem_requirements;
  70. vkGetBufferMemoryRequirements(mDevice, outBuffer.mBuffer, &mem_requirements);
  71. if (mem_requirements.size > cMaxAllocSize)
  72. {
  73. // Allocate block directly
  74. Ref<MemoryVK> memory_vk = new MemoryVK();
  75. memory_vk->mBufferSize = mem_requirements.size;
  76. AllocateMemory(mem_requirements.size, mem_requirements.memoryTypeBits, inProperties, *memory_vk);
  77. outBuffer.mMemory = memory_vk;
  78. outBuffer.mOffset = 0;
  79. }
  80. else
  81. {
  82. // Round allocation to the next power of 2 so that we can use a simple block based allocator
  83. VkDeviceSize buffer_size = max(VkDeviceSize(GetNextPowerOf2(uint32(mem_requirements.size))), cMinAllocSize);
  84. // Ensure that we have memory available from the right pool
  85. Array<Memory> &mem_array = mMemoryCache[{ buffer_size, inProperties }];
  86. if (mem_array.empty())
  87. {
  88. // Allocate a bigger block
  89. Ref<MemoryVK> memory_vk = new MemoryVK();
  90. memory_vk->mBufferSize = buffer_size;
  91. AllocateMemory(cBlockSize, mem_requirements.memoryTypeBits, inProperties, *memory_vk);
  92. // Divide into sub blocks
  93. for (VkDeviceSize offset = 0; offset < cBlockSize; offset += buffer_size)
  94. mem_array.push_back({ memory_vk, offset });
  95. }
  96. // Claim memory from the pool
  97. Memory &memory = mem_array.back();
  98. outBuffer.mMemory = memory.mMemory;
  99. outBuffer.mOffset = memory.mOffset;
  100. mem_array.pop_back();
  101. }
  102. // Bind the memory to the buffer
  103. vkBindBufferMemory(mDevice, outBuffer.mBuffer, outBuffer.mMemory->mMemory, outBuffer.mOffset);
  104. return true;
  105. }
  106. void ComputeSystemVKWithAllocator::FreeBuffer(BufferVK &ioBuffer)
  107. {
  108. if (ioBuffer.mBuffer != VK_NULL_HANDLE)
  109. {
  110. // Destroy the buffer
  111. vkDestroyBuffer(mDevice, ioBuffer.mBuffer, nullptr);
  112. ioBuffer.mBuffer = VK_NULL_HANDLE;
  113. // Hand the memory back to the cache
  114. VkDeviceSize buffer_size = ioBuffer.mMemory->mBufferSize;
  115. if (buffer_size > cMaxAllocSize)
  116. FreeMemory(*ioBuffer.mMemory);
  117. else
  118. mMemoryCache[{ buffer_size, ioBuffer.mMemory->mProperties }].push_back({ ioBuffer.mMemory, ioBuffer.mOffset });
  119. ioBuffer = BufferVK();
  120. }
  121. }
  122. void *ComputeSystemVKWithAllocator::MapBuffer(BufferVK& ioBuffer)
  123. {
  124. if (++ioBuffer.mMemory->mMappedCount == 1
  125. && VKFailed(vkMapMemory(mDevice, ioBuffer.mMemory->mMemory, 0, VK_WHOLE_SIZE, 0, &ioBuffer.mMemory->mMappedPtr)))
  126. {
  127. ioBuffer.mMemory->mMappedCount = 0;
  128. return nullptr;
  129. }
  130. return static_cast<uint8 *>(ioBuffer.mMemory->mMappedPtr) + ioBuffer.mOffset;
  131. }
  132. void ComputeSystemVKWithAllocator::UnmapBuffer(BufferVK& ioBuffer)
  133. {
  134. JPH_ASSERT(ioBuffer.mMemory->mMappedCount > 0);
  135. if (--ioBuffer.mMemory->mMappedCount == 0)
  136. {
  137. vkUnmapMemory(mDevice, ioBuffer.mMemory->mMemory);
  138. ioBuffer.mMemory->mMappedPtr = nullptr;
  139. }
  140. }
  141. JPH_NAMESPACE_END
  142. #endif // JPH_USE_VK