PixelBullet  0.0.1
A C++ game engine
Loading...
Searching...
No Matches
DescriptorsHandler.hpp
1#pragma once
2
3#include "PixelBullet/Graphics/Buffers/PushHandler.hpp"
4#include "PixelBullet/Graphics/Buffers/StorageHandler.hpp"
5#include "PixelBullet/Graphics/Buffers/UniformHandler.hpp"
6#include "PixelBullet/Graphics/Descriptors/DescriptorSet.hpp"
7#include "PixelBullet/Graphics/Pipelines/Shader.hpp"
8
9#include <iomanip>
10#include <memory>
11
12namespace PixelBullet
13{
18 {
19 public:
20 DescriptorsHandler() = default;
21 explicit DescriptorsHandler(const Pipeline& pipeline);
22
23 template <typename T>
24 void Push(const std::string& descriptorName, const T& descriptor,
25 const std::optional<OffsetSize>& offsetSize = std::nullopt)
26 {
27 if (!m_Shader)
28 {
29 return;
30 }
31
32 auto it = m_Descriptors.find(descriptorName);
33
34 if (it != m_Descriptors.end())
35 {
36 if (it->second.m_Descriptor == std::to_address(descriptor) && it->second.m_OffsetSize == offsetSize)
37 {
38 return;
39 }
40
41 m_Descriptors.erase(it);
42 }
43
44 if (!std::to_address(descriptor))
45 {
46 return;
47 }
48
49 auto location = m_Shader->GetDescriptorLocation(descriptorName);
50
51 if (!location)
52 {
53#ifdef PB_DEBUG
54 if (m_Shader->ReportedNotFound(descriptorName, true))
55 {
56 Log::Error("Could not find descriptor '{}' in shader '{}'", descriptorName,
57 m_Shader->GetName().c_str());
58 }
59#endif
60 return;
61 }
62
63 auto descriptorType = m_Shader->GetDescriptorType(*location);
64
65 if (!descriptorType)
66 {
67#ifdef PB_DEBUG
68 if (m_Shader->ReportedNotFound(descriptorName, true))
69 {
70 Log::Error("Could not find descriptor '{}' at location {} in shader '{}'", descriptorName,
71 *location, m_Shader->GetName().c_str());
72 }
73#endif
74 return;
75 }
76
77 auto writeDescriptor =
78 std::to_address(descriptor)->GetWriteDescriptor(*location, *descriptorType, offsetSize);
79 m_Descriptors.emplace(descriptorName, DescriptorValue{ std::to_address(descriptor),
80 std::move(writeDescriptor), offsetSize, *location });
81 m_Changed = true;
82 }
83
84 template <typename T>
85 void Push(const std::string& descriptorName, const T& descriptor, WriteDescriptorSet writeDescriptorSet)
86 {
87 if (!m_Shader)
88 {
89 return;
90 }
91
92 if (auto it = m_Descriptors.find(descriptorName); it != m_Descriptors.end())
93 {
94 m_Descriptors.erase(it);
95 }
96
97 auto location = m_Shader->GetDescriptorLocation(descriptorName);
98
99 m_Descriptors.emplace(
100 descriptorName,
101 DescriptorValue{ std::to_address(descriptor), std::move(writeDescriptorSet), std::nullopt, *location });
102 m_Changed = true;
103 }
104
105 void Push(const std::string& descriptorName, UniformHandler& uniformHandler,
106 const std::optional<OffsetSize>& offsetSize = std::nullopt);
107 void Push(const std::string& descriptorName, StorageHandler& storageHandler,
108 const std::optional<OffsetSize>& offsetSize = std::nullopt);
109 void Push(const std::string& descriptorName, PushHandler& pushHandler,
110 const std::optional<OffsetSize>& offsetSize = std::nullopt);
111
112 bool Update(const Pipeline& pipeline);
113
114 void BindDescriptor(const CommandBuffer& commandBuffer, const Pipeline& pipeline);
115
116 const DescriptorSet* GetDescriptorSet() const
117 {
118 return m_DescriptorSet.get();
119 }
120
121 private:
122 class DescriptorValue
123 {
124 public:
125 const Descriptor* m_Descriptor;
126 WriteDescriptorSet m_WriteDescriptor;
127 std::optional<OffsetSize> m_OffsetSize;
128 uint32_t m_Location;
129 };
130
131 const Shader* m_Shader = nullptr;
132 bool m_PushDescriptors = false;
133 std::unique_ptr<DescriptorSet> m_DescriptorSet;
134
135 std::map<std::string, DescriptorValue> m_Descriptors;
136 std::vector<VkWriteDescriptorSet> m_WriteDescriptorSets;
137 bool m_Changed = false;
138 };
139} // namespace PixelBullet
Class that represents a command buffer.
Definition CommandBuffer.hpp:15
Definition DescriptorSet.hpp:9
Definition Descriptor.hpp:75
Class that handles a descriptor set.
Definition DescriptorsHandler.hpp:18
Class that is used to represent a pipeline.
Definition Pipeline.hpp:12
Class that handles a pipeline push constant.
Definition PushHandler.hpp:14
Class that loads and processes a shader, and provides reflection.
Definition Shader.hpp:22
Definition StorageHandler.hpp:10
Class that handles a uniform buffer.
Definition UniformHandler.hpp:13
Definition Descriptor.hpp:45