buffer
This commit is contained in:
parent
05c19c3f94
commit
86d7adb07f
9 changed files with 84 additions and 108 deletions
54
Crafter.Graphics-VulkanBuffer.cppm
Normal file
54
Crafter.Graphics-VulkanBuffer.cppm
Normal file
|
|
@ -0,0 +1,54 @@
|
|||
module;
|
||||
|
||||
#include <cstdint>
|
||||
#include <vulkan/vulkan.h>
|
||||
#include "VulkanInitializers.hpp"
|
||||
|
||||
export module Crafter.Graphics:VulkanBuffer;
|
||||
import :VulkanDevice;
|
||||
|
||||
namespace Crafter {
|
||||
export template <typename T, std::uint32_t count>
|
||||
class Buffer {
|
||||
public:
|
||||
T* value;
|
||||
VkDescriptorBufferInfo descriptor;
|
||||
Buffer(VkBufferUsageFlags usageFlags, VkMemoryPropertyFlags memoryPropertyFlags) {
|
||||
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)));
|
||||
}
|
||||
private:
|
||||
VkBuffer buffer = VK_NULL_HANDLE;
|
||||
VkDeviceMemory memory = VK_NULL_HANDLE;
|
||||
VkDeviceSize alignment = 0;
|
||||
VkMemoryPropertyFlags memoryPropertyFlags;
|
||||
VkBufferUsageFlags usageFlags;
|
||||
};
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue