SleakEngine 1.0.0
C++23 multi-backend game engine
Loading...
Searching...
No Matches
RenderCommands.hpp
Go to the documentation of this file.
1#ifndef _RENDERCOMMANDS_H
2#define _RENDERCOMMANDS_H
3
4#include <mutex>
5#include <Memory/RefPtr.hpp>
8#include "BufferBase.hpp"
10
11#define RENDER_COMMAND(Type) void Execute(RenderContext* context) override; \
12 CommandType GetType() const override { return CommandType::Type;}
13
14namespace Sleak {
15 class Texture;
16
17 namespace RenderEngine {
18
19 class BufferBase;
20 class RenderContext;
21 class Shader;
22 } // close RenderEngine temporarily
23
24 class Material;
25
26 namespace RenderEngine {
27
28 /// Discriminator for RenderCommandBase subclasses, used for sorting and batching.
48
49 /// One recorded draw plus the state it needs to replay into a command list.
51 public:
52 virtual ~RenderCommandBase() = default;
53 /// Replays this command into the main forward/deferred pass.
54 virtual void Execute(RenderContext* context) = 0;
55 virtual CommandType GetType() const = 0;
56 virtual void ExecuteShadow(RenderContext* context) { /* no-op for non-draw commands */ }
57 virtual bool IsSkinned() const { return false; }
58
59 void SetOwnerObjectID(uint32_t id) { m_ownerObjectID = id; }
60 uint32_t GetOwnerObjectID() const { return m_ownerObjectID; }
61
62 // Shadow-caster cull: distant draws render to the camera but skip the
63 // shadow pass (their shadows are imperceptible). Default true.
64 void SetCastsShadow(bool v) { m_castsShadow = v; }
65 bool CastsShadow() const { return m_castsShadow; }
66
67 private:
68 uint32_t m_ownerObjectID = 0;
69 bool m_castsShadow = true;
70 };
71
72 /// Non-indexed draw command with its vertex and constant buffers.
74 public:
76 RefPtr<BufferBase> vertexBuffer,
77 List<RefPtr<BufferBase>> constantBuffers,
78 uint32_t vertexCount,
79 uint32_t startVertexLocation = 0
80 );
81
83 void ExecuteShadow(RenderContext* context) override;
84 bool IsSkinned() const override;
85
86 private:
87 RefPtr<BufferBase> m_vertexBuffer;
88 List<RefPtr<BufferBase>> m_constantBuffers;
89 uint32_t m_vertexCount;
90 uint32_t m_startVertexLocation;
91 };
92
93 /// Indexed draw command with its vertex, index, and constant buffers.
95 public:
97 RefPtr<BufferBase> indexBuffer,
98 List<RefPtr<BufferBase>> constantBuffers,
99 uint32_t indexCount,
100 uint32_t startIndexLocation = 0,
101 int32_t baseVertexLocation = 0
102 );
103
105 void ExecuteShadow(RenderContext* context) override;
106 bool IsSkinned() const override;
107
108 private:
109 RefPtr<BufferBase> m_vertexBuffer;
110 RefPtr<BufferBase> m_indexBuffer;
111 List<RefPtr<BufferBase>> m_constantBuffers;
112 uint32_t m_indexCount;
113 uint32_t m_startIndexLocation;
114 int32_t m_baseVertexLocation;
115 };
116
117 /// Constant buffer write, captured by value into inline storage or a heap fallback for larger payloads.
119 public:
120 UpdateConstantBufferCommand(RefPtr<BufferBase> buffer, void* Data, uint16_t Size);
122 if (m_heapData) free(m_heapData);
123 }
124
126
127 /// Returns a pointer to the captured payload, inline or heap-backed.
128 void* GetData() const
129 {
130 return m_heapData ? m_heapData
131 : const_cast<void*>(static_cast<const void*>(m_inlineData));
132 }
133
134 private:
135 RefPtr<BufferBase> constantBuffer;
136 // Inline storage avoids malloc/free for small buffers (≤256 bytes).
137 // TransformBuffer=128B, MaterialBuffer=128B — both fit inline.
138 static constexpr uint16_t INLINE_CAPACITY = 256;
139 alignas(16) uint8_t m_inlineData[INLINE_CAPACITY];
140 void* m_heapData = nullptr;
141 uint16_t Size;
142 };
143
144 /// Binds a constant buffer to a pipeline slot.
146 public:
148
150
151 RefPtr<BufferBase> GetBuffer() const { return constantBuffer; }
152 int GetSlot() const { return slot; }
153
154 private:
155 RefPtr<BufferBase> constantBuffer;
156 int slot;
157 };
158
159 /// Binds a texture for subsequent draws.
161 public:
163
165
166 private:
167 RefPtr<Texture> texture;
168 };
169
170 /// Binds a shader program for subsequent draws.
172 public:
174
176
177 private:
178 RefPtr<Shader> shader;
179 };
180
181 /// Switches the rasterizer fill mode.
183 public:
185
187
188 private:
189 RenderMode mode;
190 };
191
192 /// Switches the cull-face mode.
194 public:
196
198
199 private:
200 RenderFace face;
201 };
202
203 /// Binds a material's shader and textures for subsequent draws.
205 public:
207
208 void Execute(RenderContext* context) override;
209 CommandType GetType() const override { return CommandType::BindMaterial; }
210
211 ::Sleak::Material* GetMaterial() const { return m_material; }
212
213 private:
214 ::Sleak::Material* m_material;
215 };
216
217 /// Wraps an arbitrary callback as a queueable render command.
219 public:
220 using ExecuteFunction = std::function<void(RenderContext*)>;
221
222 CustomCommand(ExecuteFunction executeFunction);
223
224 void Execute(RenderContext* context) override;
225
226 CommandType GetType() const override { return CommandType::CustomCommand; }
227 private:
228 ExecuteFunction m_executeFunction;
229 };
230 }
231}
232
233#endif
#define RENDER_COMMAND(Type)
Implements a dynamic array-like list for storing and managing a collection of elements.
Definition List.hpp:20
BindConstantBufferCommand(RefPtr< BufferBase > buffer, int slot)
void Execute(RenderContext *context) override
Binds the material either through the deferred PBR path or the plain forward path,...
::Sleak::Material * GetMaterial() const
BindMaterialCommand(::Sleak::Material *material)
BindShaderCommand(RefPtr< Shader > shader)
BindTextureCommand(RefPtr< Texture > texture)
Backend-agnostic GPU buffer: vertex, index, constant, or resource view target.
Wraps an arbitrary callback as a queueable render command.
CommandType GetType() const override
std::function< void(RenderContext *)> ExecuteFunction
CustomCommand(ExecuteFunction executeFunction)
void Execute(RenderContext *context) override
Replays this command into the main forward/deferred pass.
void ExecuteShadow(RenderContext *context) override
DrawCommand(RefPtr< BufferBase > vertexBuffer, List< RefPtr< BufferBase > > constantBuffers, uint32_t vertexCount, uint32_t startVertexLocation=0)
DrawIndexedCommand(RefPtr< BufferBase > vertexBuffer, RefPtr< BufferBase > indexBuffer, List< RefPtr< BufferBase > > constantBuffers, uint32_t indexCount, uint32_t startIndexLocation=0, int32_t baseVertexLocation=0)
void ExecuteShadow(RenderContext *context) override
One recorded draw plus the state it needs to replay into a command list.
virtual CommandType GetType() const =0
virtual void Execute(RenderContext *context)=0
Replays this command into the main forward/deferred pass.
virtual void ExecuteShadow(RenderContext *context)
Abstract interface for high-level graphics command execution and resource management.
void * GetData() const
Returns a pointer to the captured payload, inline or heap-backed.
UpdateConstantBufferCommand(RefPtr< BufferBase > buffer, void *Data, uint16_t Size)
Copies the payload into inline storage, falling back to a heap allocation past INLINE_CAPACITY.
CommandType
Discriminator for RenderCommandBase subclasses, used for sorting and batching.
RenderMode
Rasterizer fill style for a draw.
RenderFace
Which triangle winding gets culled.
Root namespace for everything the engine exposes.
Definition Camera.hpp:10