descriptor set updates

This commit is contained in:
PancakeTAS 2025-06-29 19:04:56 +02:00
parent d0bd00d412
commit 736fa9b74f
No known key found for this signature in database
2 changed files with 78 additions and 1 deletions

View file

@ -1,12 +1,16 @@
#ifndef DESCRIPTORSET_HPP #ifndef DESCRIPTORSET_HPP
#define DESCRIPTORSET_HPP #define DESCRIPTORSET_HPP
#include "core/buffer.hpp"
#include "core/commandbuffer.hpp" #include "core/commandbuffer.hpp"
#include "core/descriptorpool.hpp" #include "core/descriptorpool.hpp"
#include "core/image.hpp"
#include "core/pipeline.hpp" #include "core/pipeline.hpp"
#include "core/sampler.hpp"
#include "core/shadermodule.hpp" #include "core/shadermodule.hpp"
#include "device.hpp" #include "device.hpp"
#include <variant>
#include <vulkan/vulkan_core.h> #include <vulkan/vulkan_core.h>
#include <memory> #include <memory>
@ -33,13 +37,26 @@ namespace Vulkan::Core {
DescriptorSet(const Device& device, DescriptorSet(const Device& device,
DescriptorPool pool, const ShaderModule& shaderModule); DescriptorPool pool, const ShaderModule& shaderModule);
using ResourcePair = std::pair<VkDescriptorType, std::variant<Image, Sampler, Buffer>>;
///
/// Update the descriptor set with resources.
///
/// @param device Vulkan device
/// @param resources Resources to update the descriptor set with
///
/// @throws std::invalid_argument if the device or resources are invalid.
///
void update(const Device& device,
const std::vector<std::vector<ResourcePair>>& resources) const;
/// ///
/// Bind a descriptor set to a command buffer. /// Bind a descriptor set to a command buffer.
/// ///
/// @param commandBuffer Command buffer to bind the descriptor set to. /// @param commandBuffer Command buffer to bind the descriptor set to.
/// @param pipeline Pipeline to bind the descriptor set to. /// @param pipeline Pipeline to bind the descriptor set to.
/// ///
/// @throws std::invalid_argument if the command buffer is invalid. /// @throws std::invalid_argument if the command buffer or pipeline is invalid.
/// ///
void bind(const CommandBuffer& commandBuffer, const Pipeline& pipeline) const; void bind(const CommandBuffer& commandBuffer, const Pipeline& pipeline) const;

View file

@ -30,6 +30,66 @@ DescriptorSet::DescriptorSet(const Device& device,
); );
} }
void DescriptorSet::update(const Device& device,
const std::vector<std::vector<ResourcePair>>& resources) const {
if (!device)
throw std::invalid_argument("Invalid Vulkan device");
std::vector<VkWriteDescriptorSet> writeDescriptorSets;
uint32_t bindingIndex = 0;
for (const auto& list : resources) {
for (const auto& [type, resource] : list) {
VkWriteDescriptorSet writeDesc{
.sType = VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET,
.dstSet = this->handle(),
.dstBinding = bindingIndex++,
.descriptorCount = 1,
.descriptorType = type,
};
if (std::holds_alternative<Image>(resource)) {
const auto& image = std::get<Image>(resource);
if (!image)
throw std::invalid_argument("Invalid image resource");
const VkDescriptorImageInfo imageInfo{
.imageView = image.getView(),
.imageLayout = VK_IMAGE_LAYOUT_GENERAL
};
writeDesc.pImageInfo = &imageInfo;
} else if (std::holds_alternative<Sampler>(resource)) {
const auto& sampler = std::get<Sampler>(resource);
if (!sampler)
throw std::invalid_argument("Invalid sampler resource");
const VkDescriptorImageInfo imageInfo{
.sampler = sampler.handle()
};
writeDesc.pImageInfo = &imageInfo;
} else if (std::holds_alternative<Buffer>(resource)) {
const auto& buffer = std::get<Buffer>(resource);
if (!buffer)
throw std::invalid_argument("Invalid buffer resource");
const VkDescriptorBufferInfo bufferInfo{
.buffer = buffer.handle(),
.range = buffer.getSize()
};
writeDesc.pBufferInfo = &bufferInfo;
} else {
throw std::invalid_argument("Unsupported resource type");
}
writeDescriptorSets.push_back(writeDesc);
}
}
vkUpdateDescriptorSets(device.handle(),
static_cast<uint32_t>(writeDescriptorSets.size()),
writeDescriptorSets.data(),
0, nullptr);
}
void DescriptorSet::bind(const CommandBuffer& commandBuffer, const Pipeline& pipeline) const { void DescriptorSet::bind(const CommandBuffer& commandBuffer, const Pipeline& pipeline) const {
if (!commandBuffer) if (!commandBuffer)
throw std::invalid_argument("Invalid command buffer"); throw std::invalid_argument("Invalid command buffer");