Line data Source code
1 : /* Copyright (C) 2026 Wildfire Games. 2 : * This file is part of 0 A.D. 3 : * 4 : * 0 A.D. is free software: you can redistribute it and/or modify 5 : * it under the terms of the GNU General Public License as published by 6 : * the Free Software Foundation, either version 2 of the License, or 7 : * (at your option) any later version. 8 : * 9 : * 0 A.D. is distributed in the hope that it will be useful, 10 : * but WITHOUT ANY WARRANTY; without even the implied warranty of 11 : * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 : * GNU General Public License for more details. 13 : * 14 : * You should have received a copy of the GNU General Public License 15 : * along with 0 A.D. If not, see <http://www.gnu.org/licenses/>. 16 : */ 17 : 18 : #include "precompiled.h" 19 : 20 : #include "Buffer.h" 21 : 22 : #include "lib/debug.h" 23 : #include "ps/CLogger.h" 24 : #include "renderer/backend/vulkan/Device.h" 25 : #include "renderer/backend/vulkan/Utilities.h" 26 : 27 : #include <tuple> 28 : #include <utility> 29 : 30 : namespace Renderer 31 : { 32 : 33 : namespace Backend 34 : { 35 : 36 : namespace Vulkan 37 : { 38 : 39 0 : VkBufferUsageFlags ToVkBufferUsageFlags(const uint32_t usage) 40 : { 41 0 : VkBufferUsageFlags usageFlags = 0; 42 0 : if (usage & IBuffer::Usage::TRANSFER_SRC) 43 0 : usageFlags |= VK_BUFFER_USAGE_TRANSFER_SRC_BIT; 44 0 : if (usage & IBuffer::Usage::TRANSFER_DST) 45 0 : usageFlags |= VK_BUFFER_USAGE_TRANSFER_DST_BIT; 46 0 : if (usage & IBuffer::Usage::STORAGE) 47 0 : usageFlags |= VK_BUFFER_USAGE_STORAGE_BUFFER_BIT; 48 0 : return usageFlags; 49 : } 50 : 51 0 : std::tuple<VkBufferUsageFlags, VkMemoryPropertyFlags, VmaMemoryUsage> MakeCreationFlags( 52 : const IBuffer::Type type, const uint32_t usage) 53 : { 54 0 : const VkBufferUsageFlags commonFlags = ToVkBufferUsageFlags(usage); 55 : 56 0 : switch (type) 57 : { 58 0 : case IBuffer::Type::VERTEX: 59 0 : ENSURE(usage & (IBuffer::Usage::TRANSFER_DST | IBuffer::Usage::STORAGE)); 60 0 : return { 61 0 : commonFlags | VK_BUFFER_USAGE_VERTEX_BUFFER_BIT, 62 : VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT, 63 0 : VMA_MEMORY_USAGE_AUTO_PREFER_DEVICE}; 64 0 : case IBuffer::Type::INDEX: 65 0 : ENSURE(usage & (IBuffer::Usage::TRANSFER_DST | IBuffer::Usage::STORAGE)); 66 0 : return { 67 0 : commonFlags | VK_BUFFER_USAGE_INDEX_BUFFER_BIT, 68 : VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT, 69 0 : VMA_MEMORY_USAGE_AUTO_PREFER_DEVICE}; 70 0 : case IBuffer::Type::UPLOAD: 71 0 : ENSURE(usage & IBuffer::Usage::TRANSFER_SRC); 72 0 : ENSURE(!(usage & IBuffer::Usage::STORAGE)); 73 0 : return { 74 : commonFlags, 75 : VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT | VK_MEMORY_PROPERTY_HOST_COHERENT_BIT, 76 0 : VMA_MEMORY_USAGE_AUTO}; 77 0 : case IBuffer::Type::UNIFORM: 78 0 : ENSURE(usage & IBuffer::Usage::TRANSFER_DST); 79 0 : return { 80 0 : commonFlags | VK_BUFFER_USAGE_UNIFORM_BUFFER_BIT, 81 : VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT, 82 0 : VMA_MEMORY_USAGE_AUTO_PREFER_DEVICE}; 83 : } 84 : 85 0 : return {0, 0, VMA_MEMORY_USAGE_AUTO}; 86 : } 87 : 88 : // static 89 0 : std::unique_ptr<CBuffer> CBuffer::Create( 90 : CDevice* device, const char* name, const Type type, const uint32_t size, 91 : const uint32_t usage) 92 : { 93 0 : std::unique_ptr<CBuffer> buffer(new CBuffer()); 94 0 : buffer->m_Device = device; 95 0 : buffer->m_UID = device->GenerateNextDeviceObjectUID(); 96 0 : buffer->m_Type = type; 97 0 : buffer->m_Size = size; 98 0 : buffer->m_Usage = usage; 99 : 100 0 : const auto [usageFlags, memoryProperties, memoryUsage] = MakeCreationFlags(type, usage); 101 : // According to the Vulkan spec usage must not be 0. 102 0 : ENSURE(usageFlags != 0); 103 : 104 0 : VkBufferCreateInfo bufferCreateInfo{}; 105 0 : bufferCreateInfo.sType = VK_STRUCTURE_TYPE_BUFFER_CREATE_INFO; 106 0 : bufferCreateInfo.size = size; 107 0 : bufferCreateInfo.usage = usageFlags; 108 0 : bufferCreateInfo.sharingMode = VK_SHARING_MODE_EXCLUSIVE; 109 : 110 0 : VmaAllocationCreateInfo allocationCreateInfo{}; 111 0 : if (type == Type::UPLOAD) 112 0 : allocationCreateInfo.flags = VMA_ALLOCATION_CREATE_MAPPED_BIT | VMA_ALLOCATION_CREATE_HOST_ACCESS_SEQUENTIAL_WRITE_BIT; 113 : #ifndef NDEBUG 114 : allocationCreateInfo.flags |= VMA_ALLOCATION_CREATE_USER_DATA_COPY_STRING_BIT; 115 : allocationCreateInfo.pUserData = const_cast<char*>(name); 116 : #endif 117 0 : allocationCreateInfo.requiredFlags = memoryProperties; 118 0 : allocationCreateInfo.usage = memoryUsage; 119 0 : const VkResult createBufferResult = vmaCreateBuffer( 120 : device->GetVMAAllocator(), &bufferCreateInfo, &allocationCreateInfo, 121 0 : &buffer->m_Buffer, &buffer->m_Allocation, &buffer->m_AllocationInfo); 122 0 : if (createBufferResult != VK_SUCCESS) 123 : { 124 0 : LOGERROR("Failed to create VkBuffer: {} ({})", static_cast<int>(createBufferResult), 125 : Utilities::GetVkResultName(createBufferResult)); 126 0 : return nullptr; 127 : } 128 : 129 0 : device->SetObjectName(VK_OBJECT_TYPE_BUFFER, buffer->m_Buffer, name); 130 : 131 0 : return buffer; 132 0 : } 133 : 134 0 : CBuffer::CBuffer() = default; 135 : 136 0 : CBuffer::~CBuffer() 137 : { 138 0 : if (m_Allocation != VK_NULL_HANDLE) 139 0 : m_Device->ScheduleObjectToDestroy( 140 0 : VK_OBJECT_TYPE_BUFFER, m_Buffer, m_Allocation); 141 : 142 0 : m_Device->ScheduleBufferToDestroy(m_UID); 143 0 : } 144 : 145 0 : IDevice* CBuffer::GetDevice() 146 : { 147 0 : return m_Device; 148 : } 149 : 150 : } // namespace Vulkan 151 : 152 : } // namespace Backend 153 : 154 : } // namespace Renderer