minecraftvulkan/engine/xe_descriptors.hpp
2022-09-26 18:03:07 -04:00

106 lines
2.8 KiB
C++

#pragma once
#include "xe_device.hpp"
// std
#include <memory>
#include <unordered_map>
#include <vector>
namespace xe {
class DescriptorSetLayout {
public:
class Builder {
public:
Builder(Device &xeDevice) : xeDevice{xeDevice} {}
Builder &addBinding(
uint32_t binding,
VkDescriptorType descriptorType,
VkShaderStageFlags stageFlags,
VkSampler *sampler,
uint32_t count);
std::unique_ptr<DescriptorSetLayout> build() const;
private:
Device &xeDevice;
std::unordered_map<uint32_t, VkDescriptorSetLayoutBinding> bindings{};
};
DescriptorSetLayout(
Device &xeDevice, std::unordered_map<uint32_t, VkDescriptorSetLayoutBinding> bindings);
~DescriptorSetLayout();
DescriptorSetLayout(const DescriptorSetLayout &) = delete;
DescriptorSetLayout &operator=(const DescriptorSetLayout &) = delete;
VkDescriptorSetLayout getDescriptorSetLayout() const { return descriptorSetLayout; }
private:
Device &xeDevice;
VkDescriptorSetLayout descriptorSetLayout;
std::unordered_map<uint32_t, VkDescriptorSetLayoutBinding> bindings;
friend class DescriptorWriter;
};
class DescriptorPool {
public:
class Builder {
public:
Builder(Device &xeDevice) : xeDevice{xeDevice} {}
Builder &addPoolSize(VkDescriptorType descriptorType, uint32_t count);
Builder &setPoolFlags(VkDescriptorPoolCreateFlags flags);
Builder &setMaxSets(uint32_t count);
std::unique_ptr<DescriptorPool> build() const;
private:
Device &xeDevice;
std::vector<VkDescriptorPoolSize> poolSizes{};
uint32_t maxSets = 1000;
VkDescriptorPoolCreateFlags poolFlags = 0;
};
DescriptorPool(
Device &xeDevice,
uint32_t maxSets,
VkDescriptorPoolCreateFlags poolFlags,
const std::vector<VkDescriptorPoolSize> &poolSizes);
~DescriptorPool();
DescriptorPool(const DescriptorPool &) = delete;
DescriptorPool &operator=(const DescriptorPool &) = delete;
bool allocateDescriptor(
const VkDescriptorSetLayout descriptorSetLayout, VkDescriptorSet &descriptor) const;
void freeDescriptors(std::vector<VkDescriptorSet> &descriptors) const;
void resetPool();
private:
Device &xeDevice;
VkDescriptorPool descriptorPool;
friend class DescriptorWriter;
};
class DescriptorWriter {
public:
DescriptorWriter(DescriptorSetLayout &setLayout, DescriptorPool &pool);
DescriptorWriter &writeBuffer(uint32_t binding, VkDescriptorBufferInfo *bufferInfo);
DescriptorWriter &writeImage(uint32_t binding, VkDescriptorImageInfo *imageInfo);
DescriptorWriter &writeImageArray(uint32_t binding, std::vector<VkDescriptorImageInfo> *imageInfos);
bool build(VkDescriptorSet &set);
void overwrite(VkDescriptorSet &set);
private:
DescriptorSetLayout &setLayout;
DescriptorPool &pool;
std::vector<VkWriteDescriptorSet> writes;
};
}