PixelBullet  0.0.1
A C++ game engine
Loading...
Searching...
No Matches
render_stage.h
1#pragma once
2
3#include "pixelbullet/core/color.h"
4#include "pixelbullet/graphics/images/image_depth.h"
5#include "pixelbullet/graphics/renderpass/framebuffers.h"
6#include "pixelbullet/graphics/renderpass/renderpass.h"
7#include "pixelbullet/graphics/renderpass/swapchain.h"
8
9#include <glm/glm.hpp>
10
11#include <map>
12
13namespace pixelbullet
14{
15class Graphics;
16class RenderTarget2D;
17class Window;
18
23{
24public:
25 enum class Type
26 {
27 RenderTarget,
28 Depth,
30 };
31
41 Attachment(uint32_t binding, std::string name, Type type, bool multisampled = false, VkFormat format = VK_FORMAT_R8G8B8A8_UNORM,
42 const Color& clearColour = Color::black)
43 : binding_(binding)
44 , name_(std::move(name))
45 , type_(type)
46 , multisampled_(multisampled)
47 , format_(format)
48 , clear_color_(clearColour)
49 {
50 }
51
52 uint32_t GetBinding() const
53 {
54 return binding_;
55 }
56 const std::string& GetName() const
57 {
58 return name_;
59 }
60 Type GetType() const
61 {
62 return type_;
63 }
64 bool IsMultisampled() const
65 {
66 return multisampled_;
67 }
68 VkFormat GetFormat() const
69 {
70 return format_;
71 }
72 const Color& GetClearColour() const
73 {
74 return clear_color_;
75 }
76
77private:
78 uint32_t binding_;
79 std::string name_;
80 Type type_;
81 bool multisampled_;
82 VkFormat format_;
83 Color clear_color_;
84};
85
87{
88public:
89 SubpassType(uint32_t binding, std::vector<uint32_t> attachmentBindings)
90 : binding_(binding)
91 , attachment_bindings_(std::move(attachmentBindings))
92 {
93 }
94
95 uint32_t GetBinding() const
96 {
97 return binding_;
98 }
99 const std::vector<uint32_t>& GetAttachmentBindings() const
100 {
101 return attachment_bindings_;
102 }
103
104private:
105 uint32_t binding_;
106 std::vector<uint32_t> attachment_bindings_;
107};
108
110{
111public:
112 explicit RenderArea(const glm::uvec2& extent = {}, const glm::ivec2& offset = {})
113 : extent_(extent)
114 , offset_(offset)
115 {
116 }
117
118 bool operator==(const RenderArea& rhs) const
119 {
120 return extent_ == rhs.extent_ && offset_ == rhs.offset_;
121 }
122
123 bool operator!=(const RenderArea& rhs) const
124 {
125 return !(*this == rhs);
126 }
127
128 const glm::uvec2& GetExtent() const
129 {
130 return extent_;
131 }
132 void SetExtent(const glm::uvec2& extent)
133 {
134 this->extent_ = extent;
135 }
136
137 const glm::ivec2& GetOffset() const
138 {
139 return offset_;
140 }
141 void SetOffset(const glm::ivec2& offset)
142 {
143 this->offset_ = offset;
144 }
145
150 float GetAspectRatio() const
151 {
152 return aspect_ratio_;
153 }
154 void SetAspectRatio(float aspectRatio)
155 {
156 this->aspect_ratio_ = aspectRatio;
157 }
158
159private:
160 glm::uvec2 extent_;
161 glm::ivec2 offset_;
162 float aspect_ratio_ = 1.0f;
163};
164
166{
167public:
168 Viewport() = default;
169
170 explicit Viewport(const glm::uvec2& size)
171 : size_(size)
172 {
173 }
174
175 const glm::vec2& GetScale() const
176 {
177 return scale_;
178 }
179 void SetScale(const glm::vec2& scale)
180 {
181 this->scale_ = scale;
182 }
183
184 const std::optional<glm::uvec2>& GetSize() const
185 {
186 return size_;
187 }
188 void SetSize(const std::optional<glm::uvec2>& size)
189 {
190 this->size_ = size;
191 }
192
193 const glm::ivec2& GetOffset() const
194 {
195 return offset_;
196 }
197 void SetOffset(const glm::ivec2& offset)
198 {
199 this->offset_ = offset;
200 }
201
202private:
203 glm::vec2 scale_{ 1.0f, 1.0f };
204 std::optional<glm::uvec2> size_;
205 glm::ivec2 offset_;
206};
207
209{
210 friend class Graphics;
211
212public:
213 explicit RenderStage(std::vector<Attachment> images = {}, std::vector<SubpassType> subpasses = {},
214 const Viewport& viewport = Viewport());
215
216 void Update(const Graphics& graphics);
217 void Rebuild(const Graphics& graphics, const Swapchain& swapchain);
218
219 std::optional<Attachment> GetAttachment(const std::string& name) const;
220 std::optional<Attachment> GetAttachment(uint32_t binding) const;
221
222 const Descriptor* GetDescriptor(const std::string& name) const;
223 const Descriptor* GetDescriptor(uint32_t binding) const;
224 const VkFramebuffer& GetActiveFramebuffer(uint32_t activeSwapchainImage) const;
225
226 const std::vector<Attachment>& GetAttachments() const
227 {
228 return attachments_;
229 }
230 const std::vector<SubpassType>& GetSubpasses() const
231 {
232 return subpasses_;
233 }
234
235 Viewport& GetViewport()
236 {
237 return viewport_;
238 }
239 void SetViewport(const Viewport& viewport)
240 {
241 this->viewport_ = viewport;
242 }
243
249 {
250 return render_area_;
251 }
252
257 bool IsOutOfDate() const
258 {
259 return out_of_date_;
260 }
261
262 const Renderpass* GetRenderpass() const
263 {
264 return renderpass_.get();
265 }
266 const ImageDepth* GetDepthStencil() const
267 {
268 return depth_stencil_.get();
269 }
270 const Framebuffers* GetFramebuffers() const
271 {
272 return framebuffers_.get();
273 }
274 const std::vector<VkClearValue>& GetClearValues() const
275 {
276 return clear_values_;
277 }
278 uint32_t GetAttachmentCount(uint32_t subpass) const
279 {
280 return subpass_attachment_count_[subpass];
281 }
282 bool HasDepth() const
283 {
284 return depth_attachment_.has_value();
285 }
286 bool HasSwapchain() const
287 {
288 return swapchain_attachment_.has_value();
289 }
290 bool IsMultisampled(uint32_t subpass) const
291 {
292 return subpass_multisampled_[subpass];
293 }
294
295private:
296 std::vector<Attachment> attachments_;
297 std::vector<SubpassType> subpasses_;
298
299 Viewport viewport_;
300
301 std::unique_ptr<Renderpass> renderpass_;
302 std::unique_ptr<ImageDepth> depth_stencil_;
303 std::unique_ptr<Framebuffers> framebuffers_;
304
305 std::map<std::string, const Descriptor*> descriptors_;
306 std::map<uint32_t, const Descriptor*> descriptors_by_binding_;
307
308 std::vector<VkClearValue> clear_values_;
309 std::vector<uint32_t> subpass_attachment_count_;
310 std::optional<Attachment> depth_attachment_;
311 std::optional<Attachment> swapchain_attachment_;
312 std::vector<bool> subpass_multisampled_;
313
314 RenderArea render_area_;
315 bool out_of_date_ = false;
316};
317} // namespace pixelbullet
Class that represents an attachment in a renderpass.
Definition render_stage.h:23
Attachment(uint32_t binding, std::string name, Type type, bool multisampled=false, VkFormat format=VK_FORMAT_R8G8B8A8_UNORM, const Color &clearColour=Color::black)
Definition render_stage.h:41
A polished Color class storing RGBA values in a glm::vec4.
Definition color.h:23
Definition descriptor.h:75
Module that manages the Vulkan instance_, Surface, Window and the renderpass structure.
Definition graphics.h:35
Definition render_stage.h:110
float GetAspectRatio() const
Definition render_stage.h:150
Definition render_stage.h:209
const RenderArea & GetRenderArea() const
Definition render_stage.h:248
bool IsOutOfDate() const
Definition render_stage.h:257
Definition renderpass.h:15
Definition render_stage.h:87
Definition swapchain.h:14
Definition render_stage.h:166