CeresEngine 0.2.0
A game development framework
Loading...
Searching...
No Matches
GraphicsCommandBuffer.hpp
Go to the documentation of this file.
1//
2// CeresEngine - A game development framework
3//
4// Created by Rogiel Sulzbach.
5// Copyright (c) 2018-2023 Rogiel Sulzbach. All rights reserved.
6//
7
8#pragma once
9
14
15#include "Shape.hpp"
16
18
22
27
28namespace CeresEngine {
29
33 Rect2 area;
34
35 // Compares the `GraphicsClipState` for equality. Two states are
37 [[nodiscard]] friend bool operator==(const GraphicsClipState& lhs, const GraphicsClipState& rhs) noexcept { return lhs.area == rhs.area; }
38
41 [[nodiscard]] friend bool operator!=(const GraphicsClipState& lhs, const GraphicsClipState& rhs) noexcept { return !(lhs == rhs); }
42 };
43
49 AffineTransform transform;
50
54
57 [[nodiscard]] friend bool operator==(const GraphicsState& lhs, const GraphicsState& rhs) noexcept {
58 return lhs.transform == rhs.transform && lhs.clipState == rhs.clipState;
59 }
60
63 [[nodiscard]] friend bool operator!=(const GraphicsState& lhs, const GraphicsState& rhs) noexcept { return !(lhs == rhs); }
64 };
65
69
72 Rect2 source = {0.0, 0.0, 1.0, 1.0};
73
76 Rect2 target = {0.0, 0.0, 1.0, 1.0};
77
80 Color tintColor = Color::white;
81
83
88
90
91 template<typename Allocator>
92 explicit TextureGraphicsCommand(std::allocator_arg_t, const Allocator& allocator) : TextureGraphicsCommand(allocator.get_allocator()) {}
94
95 template<typename Allocator>
96 explicit TextureGraphicsCommand(std::allocator_arg_t, const Allocator& allocator, const TextureGraphicsCommand& other)
97 : TextureGraphicsCommand(other, allocator.get_allocator()) {}
99 };
100
104
106
111
113
114 template<typename Allocator>
115 explicit ShapeGraphicsCommand(std::allocator_arg_t, const Allocator& allocator) : ShapeGraphicsCommand(allocator.get_allocator()) {}
116 explicit ShapeGraphicsCommand(AnyAllocatorReference allocator) : shape(allocator) {}
117
118 template<typename Allocator>
119 explicit ShapeGraphicsCommand(std::allocator_arg_t, const Allocator& allocator, const ShapeGraphicsCommand& other)
120 : ShapeGraphicsCommand(other, allocator.get_allocator()) {}
121 explicit ShapeGraphicsCommand(const ShapeGraphicsCommand& other, AnyAllocatorReference allocator) : shape(other.shape, allocator) {}
122 };
123
127
130
133
137
140
142
147
149
150 template<typename Allocator> explicit TextGraphicsCommand(std::allocator_arg_t, const Allocator& allocator) : TextGraphicsCommand(allocator.get_allocator()) {}
151 explicit TextGraphicsCommand(AnyAllocatorReference allocator) : layout(allocator) {}
152
153 template<typename Allocator>
154 explicit TextGraphicsCommand(std::allocator_arg_t, const Allocator& allocator, const TextGraphicsCommand& other)
155 : TextGraphicsCommand(other, allocator.get_allocator()) {}
157 : font(other.font), texture(other.texture), layout(other.layout, allocator), offset(other.offset), tintColor(other.tintColor) {}
158 };
159
163 Rect2 target = {0.0, 0.0, 1.0, 1.0};
164
166 Color color = Color::zero;
167
169
174
176
177 template<typename Allocator> ClearGraphicsCommand(std::allocator_arg_t, const Allocator& allocator) : ClearGraphicsCommand(allocator.get_allocator()) {}
179
180 template<typename Allocator>
181 explicit ClearGraphicsCommand(std::allocator_arg_t, const Allocator& allocator, const ClearGraphicsCommand& other)
182 : ClearGraphicsCommand(other, allocator.get_allocator()) {}
184 };
185
188 using Command = std::function<void(GPUCommandBuffer&, const GraphicsState&)>;
189
193
195
200
202
203 template<typename Allocator> explicit RawGraphicsCommand(std::allocator_arg_t, const Allocator& allocator) : RawGraphicsCommand(allocator.get_allocator()) {}
205
206 template<typename Allocator>
207 explicit RawGraphicsCommand(std::allocator_arg_t, const Allocator& allocator, const RawGraphicsCommand& other)
208 : RawGraphicsCommand(other, allocator.get_allocator()) {}
210 };
211
214
215 public:
218
219 private:
222
223 public:
224 GraphicsCommand() = default;
230
233
236
238
239 template<typename Allocator> explicit GraphicsCommand(std::allocator_arg_t, const Allocator& allocator) {}
240
241 template<typename Allocator>
242 explicit GraphicsCommand(std::allocator_arg_t, const Allocator& allocator, const GraphicsState& graphicsState, TextureGraphicsCommand&& command)
243 : GraphicsCommand(graphicsState, TextureGraphicsCommand(command, allocator.get_allocator())) {}
244
245 template<typename Allocator>
246 explicit GraphicsCommand(std::allocator_arg_t, const Allocator& allocator, const GraphicsState& graphicsState, ShapeGraphicsCommand&& command)
247 : GraphicsCommand(graphicsState, ShapeGraphicsCommand(command, allocator.get_allocator())) {}
248
249 template<typename Allocator>
250 explicit GraphicsCommand(std::allocator_arg_t, const Allocator& allocator, const GraphicsState& graphicsState, TextGraphicsCommand&& command)
251 : GraphicsCommand(graphicsState, TextGraphicsCommand(command, allocator.get_allocator())) {}
252
253 template<typename Allocator>
254 explicit GraphicsCommand(std::allocator_arg_t, const Allocator& allocator, const GraphicsState& graphicsState, ClearGraphicsCommand&& command)
255 : GraphicsCommand(graphicsState, ClearGraphicsCommand(command, allocator.get_allocator())) {}
256
257 template<typename Allocator>
258 explicit GraphicsCommand(std::allocator_arg_t, const Allocator& allocator, const GraphicsState& graphicsState, RawGraphicsCommand&& command)
259 : GraphicsCommand(graphicsState, RawGraphicsCommand(command, allocator.get_allocator())) {}
260
261 template<typename Allocator>
262 GraphicsCommand(std::allocator_arg_t, const Allocator& allocator, const GraphicsCommand& other) : GraphicsCommand(other, allocator.get_allocator()) {}
263
265 // : graphicsState(std::make_obj_using_allocator<GraphicsState>(allocator, other.graphicsState)),
266 : graphicsState(other.graphicsState), mData(other.mData.visit([&allocator](const auto& command) -> Variant {
267 using T = std::decay_t<decltype(command)>;
268 if constexpr(std::is_same_v<T, std::monostate>) {
269 return {};
270 } else {
271 return T(command, allocator.get_allocator());
272 }
273 })) {}
274
275 public:
276 template<typename T> [[nodiscard]] bool is() const noexcept { return mData.is<T>(); }
277 template<typename T> [[nodiscard]] T& as() { return mData.as<T>(); }
278 template<typename T> [[nodiscard]] const T& as() const { return mData.as<T>(); }
279
280 template<typename Visitor> decltype(auto) visit(Visitor&& visitor) const { return std::visit(std::forward<Visitor>(visitor), mData); }
281 };
282
285 public:
287
288 public:
289 template<typename Allocator>
290 explicit GraphicsCommandBuffer(std::allocator_arg_t, const Allocator& allocator) : GraphicsCommandBuffer(allocator.get_allocator()) {}
291 explicit GraphicsCommandBuffer(AnyAllocatorReference allocator) : commands(allocator) {}
292
294 explicit GraphicsCommandBuffer() noexcept = default;
295
296 public:
302 template<typename T> T& pushCommand(const GraphicsState& graphicsState) {
303 GraphicsCommand& command = commands.emplace_back(graphicsState, T());
304 return command.as<T>();
305 }
306
317 template<typename T> T& tryReuseCommand(const GraphicsState& graphicsState) {
318 if(commands.empty()) {
319 return pushCommand<T>(graphicsState);
320 }
321
322 GraphicsCommand& command = commands.emplace_back(graphicsState, T());
323 if(command.graphicsState == graphicsState || command.is<T>()) {
324 return command.as<T>();
325 }
326
327 return pushCommand<T>(graphicsState);
328 }
329 };
330
331} // namespace CeresEngine
332
#define CE_USES_ALLOCATOR(T)
Definition Macros.hpp:530
Definition GPUCommandBuffer.hpp:77
An object that encapsulates rendering context for a view.
Definition GraphicsCommandBuffer.hpp:284
GraphicsCommandBuffer() noexcept=default
Creates a new GraphicsCommandBuffer instance.
GraphicsCommandBuffer(std::allocator_arg_t, const Allocator &allocator)
Definition GraphicsCommandBuffer.hpp:290
Deque< GraphicsCommand, AnyAllocatorReference > commands
Definition GraphicsCommandBuffer.hpp:286
GraphicsCommandBuffer(AnyAllocatorReference allocator)
Definition GraphicsCommandBuffer.hpp:291
T & tryReuseCommand(const GraphicsState &graphicsState)
Tries to get a command from the command list.
Definition GraphicsCommandBuffer.hpp:317
Definition Optional.hpp:17
A type that describes a conjunction of shapes that can be filled and stroked.
Definition Shape.hpp:198
Definition Application.hpp:19
std::deque< T, ScopedAllocatorAdaptor< StdAllocator< T, RawAllocator > > > Deque
Deque (double-ended queue) is an indexed sequence container that allows fast insertion and deletion a...
Definition Deque.hpp:20
SPtr< Font > FontPtr
Definition Forward.hpp:13
foonathan::memory::any_allocator_reference AnyAllocatorReference
Definition Allocator.hpp:98
std::vector< T, ScopedAllocatorAdaptor< StdAllocator< T, RawAllocator > > > Vector
Vector is a sequence container that encapsulates dynamic size arrays.
Definition Vector.hpp:17
auto move(Vector3 position)
Moves a entity to the given position.
Definition Helpers.hpp:22
SPtr< Texture > TexturePtr
Definition Forward.hpp:17
@ Color
Attachment is used for color output.
DefaultAllocator & gDefaultAllocator()
constexpr size_t hash(const T &v)
Generates a hash for the provided type.
Definition Hash.hpp:25
Definition Span.hpp:668
Definition GraphicsCommandBuffer.hpp:160
ClearGraphicsCommand(const ClearGraphicsCommand &)=default
ClearGraphicsCommand(std::allocator_arg_t, const Allocator &allocator, const ClearGraphicsCommand &other)
Definition GraphicsCommandBuffer.hpp:181
ClearGraphicsCommand(AnyAllocatorReference allocator)
Definition GraphicsCommandBuffer.hpp:178
ClearGraphicsCommand & operator=(const ClearGraphicsCommand &)=default
ClearGraphicsCommand(const ClearGraphicsCommand &other, AnyAllocatorReference allocator)
Definition GraphicsCommandBuffer.hpp:183
ClearGraphicsCommand(ClearGraphicsCommand &&) noexcept=default
Rect2 target
The area of the target texture to be cleared.
Definition GraphicsCommandBuffer.hpp:163
Color color
The color to render the text with.
Definition GraphicsCommandBuffer.hpp:166
A structure that describes the clipping state of the graphics context.
Definition GraphicsCommandBuffer.hpp:31
Rect2 area
The area to clip the render output.
Definition GraphicsCommandBuffer.hpp:33
friend bool operator==(const GraphicsClipState &lhs, const GraphicsClipState &rhs) noexcept
considered equal if they both yield the same result when rendering.
Definition GraphicsCommandBuffer.hpp:37
friend bool operator!=(const GraphicsClipState &lhs, const GraphicsClipState &rhs) noexcept
Compares the GraphicsClipState for inequality.
Definition GraphicsCommandBuffer.hpp:41
Definition GraphicsCommandBuffer.hpp:212
bool is() const noexcept
Definition GraphicsCommandBuffer.hpp:276
GraphicsCommand(const GraphicsCommand &other, AnyAllocatorReference allocator)
Definition GraphicsCommandBuffer.hpp:264
GraphicsCommand & operator=(const GraphicsCommand &)=default
decltype(auto) visit(Visitor &&visitor) const
Definition GraphicsCommandBuffer.hpp:280
GraphicsCommand(const GraphicsState &graphicsState, RawGraphicsCommand &&command)
Definition GraphicsCommandBuffer.hpp:229
GraphicsCommand(std::allocator_arg_t, const Allocator &allocator, const GraphicsCommand &other)
Definition GraphicsCommandBuffer.hpp:262
GraphicsCommand(std::allocator_arg_t, const Allocator &allocator, const GraphicsState &graphicsState, TextGraphicsCommand &&command)
Definition GraphicsCommandBuffer.hpp:250
T & as()
Definition GraphicsCommandBuffer.hpp:277
GraphicsCommand(const GraphicsState &graphicsState, ClearGraphicsCommand &&command)
Definition GraphicsCommandBuffer.hpp:228
const T & as() const
Definition GraphicsCommandBuffer.hpp:278
GraphicsCommand(std::allocator_arg_t, const Allocator &allocator, const GraphicsState &graphicsState, ClearGraphicsCommand &&command)
Definition GraphicsCommandBuffer.hpp:254
GraphicsCommand(std::allocator_arg_t, const Allocator &allocator, const GraphicsState &graphicsState, ShapeGraphicsCommand &&command)
Definition GraphicsCommandBuffer.hpp:246
GraphicsCommand(const GraphicsCommand &)=default
GraphicsState graphicsState
Describes the rendering state the moment the command was recorded.
Definition GraphicsCommandBuffer.hpp:217
GraphicsCommand(std::allocator_arg_t, const Allocator &allocator, const GraphicsState &graphicsState, TextureGraphicsCommand &&command)
Definition GraphicsCommandBuffer.hpp:242
Variant mData
Definition GraphicsCommandBuffer.hpp:221
GraphicsCommand(const GraphicsState &graphicsState, TextGraphicsCommand &&command)
Definition GraphicsCommandBuffer.hpp:227
GraphicsCommand(const GraphicsState &graphicsState, TextureGraphicsCommand &&command)
Definition GraphicsCommandBuffer.hpp:225
GraphicsCommand(const GraphicsState &graphicsState, ShapeGraphicsCommand &&command)
Definition GraphicsCommandBuffer.hpp:226
GraphicsCommand(std::allocator_arg_t, const Allocator &allocator, const GraphicsState &graphicsState, RawGraphicsCommand &&command)
Definition GraphicsCommandBuffer.hpp:258
GraphicsCommand(GraphicsCommand &&) noexcept=default
Variant< std::monostate, TextureGraphicsCommand, ShapeGraphicsCommand, TextGraphicsCommand, ClearGraphicsCommand, RawGraphicsCommand > Variant
Definition GraphicsCommandBuffer.hpp:220
A structure that represents a snapshot of the graphics context state.
Definition GraphicsCommandBuffer.hpp:45
friend bool operator!=(const GraphicsState &lhs, const GraphicsState &rhs) noexcept
Compares the GraphicsState for inequality.
Definition GraphicsCommandBuffer.hpp:63
friend bool operator==(const GraphicsState &lhs, const GraphicsState &rhs) noexcept
Compares the GraphicsState for equality.
Definition GraphicsCommandBuffer.hpp:57
Optional< GraphicsClipState > clipState
Describes how and where the renderer should clip draw commands from views.
Definition GraphicsCommandBuffer.hpp:53
AffineTransform transform
A 2D transformation matrix that transforms a point from the view coordinate space into the window coo...
Definition GraphicsCommandBuffer.hpp:49
Definition GraphicsCommandBuffer.hpp:186
RawGraphicsCommand(const RawGraphicsCommand &)=default
std::function< void(GPUCommandBuffer &, const GraphicsState &)> Command
A function type that represents the command to be called before rendering the .
Definition GraphicsCommandBuffer.hpp:188
RawGraphicsCommand & operator=(const RawGraphicsCommand &)=default
RawGraphicsCommand(AnyAllocatorReference allocator)
Definition GraphicsCommandBuffer.hpp:204
RawGraphicsCommand(std::allocator_arg_t, const Allocator &allocator, const RawGraphicsCommand &other)
Definition GraphicsCommandBuffer.hpp:207
RawGraphicsCommand(const RawGraphicsCommand &other, AnyAllocatorReference allocator)
Definition GraphicsCommandBuffer.hpp:209
Command command
A command that will be executed by the renderer every time when rendering a frame.
Definition GraphicsCommandBuffer.hpp:192
RawGraphicsCommand(RawGraphicsCommand &&) noexcept=default
Definition GraphicsCommandBuffer.hpp:101
Shape shape
The shape to be rendered.
Definition GraphicsCommandBuffer.hpp:103
ShapeGraphicsCommand & operator=(const ShapeGraphicsCommand &)=default
ShapeGraphicsCommand(std::allocator_arg_t, const Allocator &allocator, const ShapeGraphicsCommand &other)
Definition GraphicsCommandBuffer.hpp:119
ShapeGraphicsCommand(AnyAllocatorReference allocator)
Definition GraphicsCommandBuffer.hpp:116
ShapeGraphicsCommand(ShapeGraphicsCommand &&) noexcept=default
ShapeGraphicsCommand(const ShapeGraphicsCommand &)=default
ShapeGraphicsCommand(const ShapeGraphicsCommand &other, AnyAllocatorReference allocator)
Definition GraphicsCommandBuffer.hpp:121
Definition GraphicsCommandBuffer.hpp:124
TextGraphicsCommand(std::allocator_arg_t, const Allocator &allocator, const TextGraphicsCommand &other)
Definition GraphicsCommandBuffer.hpp:154
FontPtr font
The font to draw the text with.
Definition GraphicsCommandBuffer.hpp:126
TexturePtr texture
A texture that contains the laidout text glyphs.
Definition GraphicsCommandBuffer.hpp:129
Vector2 offset
The offset to start rendering the text at.
Definition GraphicsCommandBuffer.hpp:136
Color tintColor
The color to render the text with.
Definition GraphicsCommandBuffer.hpp:139
TextGraphicsCommand(const TextGraphicsCommand &other, AnyAllocatorReference allocator)
Definition GraphicsCommandBuffer.hpp:156
TextGraphicsCommand(const TextGraphicsCommand &)=default
TextGraphicsCommand & operator=(const TextGraphicsCommand &)=default
TextGraphicsCommand(AnyAllocatorReference allocator)
Definition GraphicsCommandBuffer.hpp:151
TextGraphicsCommand(TextGraphicsCommand &&) noexcept=default
Vector< TextLayout, AnyAllocator > layout
A copy of the current text layout.
Definition GraphicsCommandBuffer.hpp:132
Definition GraphicsCommandBuffer.hpp:66
TextureGraphicsCommand(const TextureGraphicsCommand &other, AnyAllocatorReference allocator)
Definition GraphicsCommandBuffer.hpp:98
TextureGraphicsCommand(AnyAllocatorReference allocator)
Definition GraphicsCommandBuffer.hpp:93
TextureGraphicsCommand & operator=(const TextureGraphicsCommand &)=default
Rect2 source
The area of the source texture to be blit.
Definition GraphicsCommandBuffer.hpp:72
Color tintColor
A tint color to be applied to the blit texture.
Definition GraphicsCommandBuffer.hpp:80
TextureGraphicsCommand(std::allocator_arg_t, const Allocator &allocator, const TextureGraphicsCommand &other)
Definition GraphicsCommandBuffer.hpp:96
TextureGraphicsCommand(TextureGraphicsCommand &&) noexcept=default
TextureGraphicsCommand(const TextureGraphicsCommand &)=default
Rect2 target
The area of the target texture to be blit over.
Definition GraphicsCommandBuffer.hpp:76
TexturePtr texture
The texture to be copied into the view render area.
Definition GraphicsCommandBuffer.hpp:68