minecraftvulkan/engine/xe_descriptors.hpp

105 lines
2.7 KiB
C++
Raw Normal View History

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