refactor(cleanup): expand vulkan abstraction

This commit is contained in:
PancakeTAS 2025-11-30 12:53:40 +01:00
parent 3879dd9b3d
commit 18b01050eb
10 changed files with 45 additions and 11 deletions

View file

@ -14,17 +14,21 @@ namespace vk {
/// create a buffer
/// @param vk the vulkan instance
/// @param data initial data uploaded to the buffer
/// @param usage usage flags for the buffer
/// @throws ls::vulkan_error on failure
template<typename T>
Buffer(const vk::Vulkan& vk, const T& data)
: Buffer(vk, reinterpret_cast<const void*>(&data), sizeof(T)) {}
Buffer(const vk::Vulkan& vk, const T& data,
VkBufferUsageFlags usage = VK_BUFFER_USAGE_UNIFORM_BUFFER_BIT)
: Buffer(vk, reinterpret_cast<const void*>(&data), sizeof(T), usage) {}
/// create a buffer
/// @param vk the vulkan instance
/// @param data initial data uploaded to the buffer
/// @param size size of the buffer in bytes
/// @param usage usage flags for the buffer
/// @throws ls::vulkan_error on failure
Buffer(const vk::Vulkan& vk, const void* data, size_t size);
Buffer(const vk::Vulkan& vk, const void* data, size_t size,
VkBufferUsageFlags usage = VK_BUFFER_USAGE_UNIFORM_BUFFER_BIT);
/// get the buffer handle
/// @return the buffer handle

View file

@ -29,6 +29,10 @@ namespace vk {
const std::vector<ls::R<const vk::Image>>& storageImages,
const std::vector<ls::R<const vk::Sampler>>& samplers,
const std::vector<ls::R<const vk::Buffer>>& buffers);
/// get the underlying descriptor set handle
/// @return the handle
[[nodiscard]] const auto& handle() const { return *this->descriptorSet; }
private:
ls::owned_ptr<VkDescriptorSet> descriptorSet;
};

View file

@ -27,6 +27,10 @@ namespace vk {
/// @returns true if the fence signaled, false if it timed out
/// @throws ls::vulkan_error on failure
[[nodiscard]] bool wait(const vk::Vulkan& vk, uint64_t timeout = UINT64_MAX) const;
/// get the fence handle
/// @return the fence handle
[[nodiscard]] VkFence handle() const { return *this->fence; }
private:
ls::owned_ptr<VkFence> fence;
};

View file

@ -32,9 +32,15 @@ namespace vk {
/// get the image view handle
/// @return the image view handle
[[nodiscard]] const auto& imageview() const { return this->view.get(); }
/// get the extent of the image
/// @return the extent of the image
[[nodiscard]] VkExtent2D getExtent() const { return this->extent; }
private:
ls::owned_ptr<VkImage> image;
ls::owned_ptr<VkDeviceMemory> memory;
ls::owned_ptr<VkImageView> view;
VkExtent2D extent{};
};
}

View file

@ -27,11 +27,18 @@ namespace vk {
/// get the descriptor set layout
/// @returns the descriptor set layout
[[nodiscard]] const auto& descriptorlayout() const { return *this->descriptorLayout; }
/// get the pipeline layout
/// @returns the pipeline layout
[[nodiscard]] const auto& pipelinelayout() const { return *this->pipelineLayout; }
/// get the pipeline
/// @returns the pipeline
[[nodiscard]] const auto& pipeline() const { return *this->pipeline_; }
private:
ls::owned_ptr<VkShaderModule> shaderModule;
ls::owned_ptr<VkDescriptorSetLayout> descriptorLayout;
ls::owned_ptr<VkPipelineLayout> pipelineLayout;
ls::owned_ptr<VkPipeline> pipeline;
ls::owned_ptr<VkPipeline> pipeline_;
};
}

View file

@ -54,6 +54,9 @@ namespace vk {
[[nodiscard]] std::optional<uint32_t> findMemoryTypeIndex(
std::bitset<32> validTypes, bool hostVisibility) const;
/// get the vulkan instance
/// @return the instance handle
[[nodiscard]] const auto& inst() const { return this->instance.get(); }
/// get the vulkan device
/// @return the device handle
[[nodiscard]] const auto& dev() const { return this->device.get(); }
@ -63,6 +66,9 @@ namespace vk {
/// get the descriptor pool
/// @return the descriptor pool handle
[[nodiscard]] const auto& descpool() const { return this->descPool.get(); }
/// get the compute queue
/// @return the compute queue handle
[[nodiscard]] const auto& queue() const { return this->computeQueue; }
/// check if fp16 is supported
/// @return true if fp16 is supported

View file

@ -15,13 +15,14 @@ using namespace vk;
namespace {
/// create a buffer
ls::owned_ptr<VkBuffer> createBuffer(const vk::Vulkan& vk, size_t size) {
ls::owned_ptr<VkBuffer> createBuffer(const vk::Vulkan& vk, size_t size,
VkBufferUsageFlags usage) {
VkBuffer handle{};
const VkBufferCreateInfo bufferInfo{
.sType = VK_STRUCTURE_TYPE_BUFFER_CREATE_INFO,
.size = size,
.usage = VK_BUFFER_USAGE_UNIFORM_BUFFER_BIT,
.usage = usage,
.sharingMode = VK_SHARING_MODE_EXCLUSIVE
};
auto res = vkCreateBuffer(vk.dev(), &bufferInfo, nullptr, &handle);
@ -88,8 +89,8 @@ namespace {
}
}
Buffer::Buffer(const vk::Vulkan& vk, const void* data, size_t size) :
buffer(createBuffer(vk, size)),
Buffer::Buffer(const vk::Vulkan& vk, const void* data, size_t size, VkBufferUsageFlags usage) :
buffer(createBuffer(vk, size, usage)),
memory(allocateMemory(vk, *this->buffer)),
size(size) {
copyDataToBuffer(vk, *this->memory, data, size);

View file

@ -10,6 +10,7 @@
#include <cstddef>
#include <cstdint>
#include <vector>
#include <vulkan/vulkan_core.h>
using namespace vk;

View file

@ -58,7 +58,7 @@ namespace {
auto mti = vk.findMemoryTypeIndex(
reqs.memoryTypeBits,
true
false
);
if (!mti.has_value())
throw ls::vulkan_error("no suitable memory type found for image");
@ -168,5 +168,6 @@ Image::Image(const vk::Vulkan& vk,
view(createImageView(vk,
*this->image,
format
)) {
)),
extent(extent) {
}

View file

@ -158,7 +158,7 @@ Shader::Shader(const vk::Vulkan& vk, const std::vector<uint8_t>& code,
pipelineLayout(createPipelineLayout(vk,
*this->descriptorLayout
)),
pipeline(createComputePipeline(vk,
pipeline_(createComputePipeline(vk,
*this->shaderModule,
*this->pipelineLayout
)) {