1 /* Copyright (c) 2017-2018 Hans-Kristian Arntzen 2 * 3 * Permission is hereby granted, free of charge, to any person obtaining 4 * a copy of this software and associated documentation files (the 5 * "Software"), to deal in the Software without restriction, including 6 * without limitation the rights to use, copy, modify, merge, publish, 7 * distribute, sublicense, and/or sell copies of the Software, and to 8 * permit persons to whom the Software is furnished to do so, subject to 9 * the following conditions: 10 * 11 * The above copyright notice and this permission notice shall be 12 * included in all copies or substantial portions of the Software. 13 * 14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 15 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 16 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. 17 * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY 18 * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, 19 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE 20 * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 21 */ 22 23 #pragma once 24 25 #include "vulkan.hpp" 26 #include "intrusive.hpp" 27 #include <vector> 28 29 namespace Vulkan 30 { 31 class Device; 32 class Buffer; 33 34 struct BufferBlockAllocation 35 { 36 uint8_t *host; 37 VkDeviceSize offset; 38 }; 39 40 struct BufferBlock 41 { 42 ~BufferBlock(); 43 Util::IntrusivePtr<Buffer> gpu; 44 Util::IntrusivePtr<Buffer> cpu; 45 VkDeviceSize offset = 0; 46 VkDeviceSize alignment = 0; 47 VkDeviceSize size = 0; 48 uint8_t *mapped = nullptr; 49 allocateVulkan::BufferBlock50 BufferBlockAllocation allocate(VkDeviceSize allocate_size) 51 { 52 auto aligned_offset = (offset + alignment - 1) & ~(alignment - 1); 53 if (aligned_offset + allocate_size <= size) 54 { 55 auto *ret = mapped + aligned_offset; 56 offset = aligned_offset + allocate_size; 57 return { ret, aligned_offset }; 58 } 59 else 60 return { nullptr, 0 }; 61 } 62 }; 63 64 class BufferPool 65 { 66 public: 67 ~BufferPool(); 68 void init(Device *device, VkDeviceSize block_size, VkDeviceSize alignment, VkBufferUsageFlags usage, bool need_device_local); 69 void reset(); 70 get_block_size() const71 VkDeviceSize get_block_size() const 72 { 73 return block_size; 74 } 75 76 BufferBlock request_block(VkDeviceSize minimum_size); 77 void recycle_block(BufferBlock &&block); 78 79 private: 80 Device *device = nullptr; 81 VkDeviceSize block_size = 0; 82 VkDeviceSize alignment = 0; 83 VkBufferUsageFlags usage = 0; 84 std::vector<BufferBlock> blocks; 85 BufferBlock allocate_block(VkDeviceSize size); 86 bool need_device_local = false; 87 }; 88 }