2025-05-07 19:21:51 +02:00
|
|
|
/*
|
|
|
|
|
Crafter®.Graphics
|
|
|
|
|
Copyright (C) 2025 Catcrafts®
|
|
|
|
|
Catcrafts.net
|
|
|
|
|
|
|
|
|
|
This library is free software; you can redistribute it and/or
|
|
|
|
|
modify it under the terms of the GNU Lesser General Public
|
|
|
|
|
License as published by the Free Software Foundation; either
|
|
|
|
|
version 3.0 of the License, or (at your option) any later version.
|
|
|
|
|
|
|
|
|
|
This library is distributed in the hope that it will be useful,
|
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
|
Lesser General Public License for more details.
|
|
|
|
|
|
|
|
|
|
You should have received a copy of the GNU Lesser General Public
|
|
|
|
|
License along with this library; if not, write to the Free Software
|
|
|
|
|
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
|
|
|
|
|
*/
|
|
|
|
|
|
2025-04-27 00:05:21 +02:00
|
|
|
module;
|
|
|
|
|
|
|
|
|
|
#include <cstdint>
|
|
|
|
|
#include <vulkan/vulkan.h>
|
2025-06-13 23:59:36 +02:00
|
|
|
#include "../../lib/VulkanInitializers.hpp"
|
2025-04-27 00:05:21 +02:00
|
|
|
|
|
|
|
|
export module Crafter.Graphics:VulkanBuffer;
|
|
|
|
|
import :VulkanDevice;
|
|
|
|
|
|
|
|
|
|
namespace Crafter {
|
2025-06-14 14:58:02 +02:00
|
|
|
/**
|
|
|
|
|
* @brief VkBuffer holder.
|
|
|
|
|
* Stores a value and handles buffer mapping and lifetime management.
|
|
|
|
|
* @tparam T The value to store.
|
|
|
|
|
*/
|
2025-04-27 01:57:25 +02:00
|
|
|
export template <typename T>
|
2025-04-27 00:05:21 +02:00
|
|
|
class Buffer {
|
|
|
|
|
public:
|
|
|
|
|
T* value;
|
|
|
|
|
VkDescriptorBufferInfo descriptor;
|
2025-06-14 14:58:02 +02:00
|
|
|
public:
|
|
|
|
|
/**
|
|
|
|
|
* @brief Creates and initializes a Vulkan buffer with the specified usage and memory properties.
|
|
|
|
|
*
|
|
|
|
|
* This constructor allocates a buffer capable of holding `count` elements of type T.
|
|
|
|
|
* The buffer usage and memory property flags control how the buffer will be used
|
|
|
|
|
* and how its memory is managed.
|
|
|
|
|
*
|
|
|
|
|
* @param usageFlags Vulkan buffer usage flags that define allowed operations on the buffer
|
|
|
|
|
* (e.g., vertex buffer, index buffer, uniform buffer).
|
|
|
|
|
* @param memoryPropertyFlags Vulkan memory property flags specifying the desired memory
|
|
|
|
|
* properties (e.g., device local, host visible).
|
|
|
|
|
* @param count Number of elements to allocate space for in the buffer. The total buffer size
|
|
|
|
|
* will be `count * sizeof(T)`. Must be above 0.
|
|
|
|
|
*/
|
2025-04-27 01:57:25 +02:00
|
|
|
Buffer(VkBufferUsageFlags usageFlags, VkMemoryPropertyFlags memoryPropertyFlags, std::uint32_t count = 1) {
|
2025-04-27 00:05:21 +02:00
|
|
|
VkBufferCreateInfo bufferCreateInfo = vks::initializers::bufferCreateInfo(usageFlags, sizeof(T)*count);
|
|
|
|
|
VulkanDevice::CHECK_VK_RESULT(vkCreateBuffer(VulkanDevice::device, &bufferCreateInfo, nullptr, &buffer));
|
|
|
|
|
|
|
|
|
|
// Create the memory backing up the buffer handle
|
|
|
|
|
VkMemoryRequirements memReqs;
|
|
|
|
|
VkMemoryAllocateInfo memAlloc = vks::initializers::memoryAllocateInfo();
|
|
|
|
|
vkGetBufferMemoryRequirements(VulkanDevice::device, buffer, &memReqs);
|
|
|
|
|
memAlloc.allocationSize = memReqs.size;
|
|
|
|
|
// Find a memory type index that fits the properties of the buffer
|
|
|
|
|
memAlloc.memoryTypeIndex = VulkanDevice::GetMemoryType(memReqs.memoryTypeBits, memoryPropertyFlags);
|
|
|
|
|
// If the buffer has VK_BUFFER_USAGE_SHADER_DEVICE_ADDRESS_BIT set we also need to enable the appropriate flag during allocation
|
|
|
|
|
VkMemoryAllocateFlagsInfoKHR allocFlagsInfo{};
|
|
|
|
|
if (usageFlags & VK_BUFFER_USAGE_SHADER_DEVICE_ADDRESS_BIT) {
|
|
|
|
|
allocFlagsInfo.sType = VK_STRUCTURE_TYPE_MEMORY_ALLOCATE_FLAGS_INFO_KHR;
|
|
|
|
|
allocFlagsInfo.flags = VK_MEMORY_ALLOCATE_DEVICE_ADDRESS_BIT_KHR;
|
|
|
|
|
memAlloc.pNext = &allocFlagsInfo;
|
|
|
|
|
}
|
|
|
|
|
VulkanDevice::CHECK_VK_RESULT(vkAllocateMemory(VulkanDevice::device, &memAlloc, nullptr, &memory));
|
|
|
|
|
|
|
|
|
|
alignment = memReqs.alignment;
|
|
|
|
|
usageFlags = usageFlags;
|
|
|
|
|
memoryPropertyFlags = memoryPropertyFlags;
|
|
|
|
|
|
|
|
|
|
descriptor.offset = 0;
|
|
|
|
|
descriptor.buffer = buffer;
|
|
|
|
|
descriptor.range = sizeof(T)*count;
|
|
|
|
|
|
|
|
|
|
VulkanDevice::CHECK_VK_RESULT(vkBindBufferMemory(VulkanDevice::device, buffer, memory, 0));
|
|
|
|
|
VulkanDevice::CHECK_VK_RESULT(vkMapMemory(VulkanDevice::device, memory, 0, sizeof(T)*count, 0, reinterpret_cast<void**>(&value)));
|
|
|
|
|
}
|
2025-06-14 14:58:02 +02:00
|
|
|
/**
|
|
|
|
|
* @brief Frees all resources assosiacted with the buffer.
|
|
|
|
|
*/
|
2025-05-03 06:51:33 +02:00
|
|
|
~Buffer() {
|
|
|
|
|
vkUnmapMemory(VulkanDevice::device, memory);
|
|
|
|
|
vkDestroyBuffer(VulkanDevice::device, buffer, nullptr);
|
|
|
|
|
vkFreeMemory(VulkanDevice::device, memory, nullptr);
|
|
|
|
|
}
|
2025-04-27 00:05:21 +02:00
|
|
|
private:
|
|
|
|
|
VkDeviceSize alignment = 0;
|
|
|
|
|
VkMemoryPropertyFlags memoryPropertyFlags;
|
|
|
|
|
VkBufferUsageFlags usageFlags;
|
2025-06-14 14:58:02 +02:00
|
|
|
VkBuffer buffer = VK_NULL_HANDLE;
|
|
|
|
|
VkDeviceMemory memory = VK_NULL_HANDLE;
|
2025-04-27 00:05:21 +02:00
|
|
|
};
|
|
|
|
|
}
|