SleakEngine 0.1.0
C++23 multi-backend game engine
Loading...
Searching...
No Matches
RenderCommands.cpp
Go to the documentation of this file.
9
10namespace Sleak {
11namespace RenderEngine {
12
14 List<RefPtr<BufferBase>> constantBuffers,
15 uint32_t vertexCount, uint32_t vertexLocation)
16 : m_vertexBuffer(buffer),
17 m_constantBuffers(constantBuffers),
18 m_vertexCount(vertexCount),
19 m_startVertexLocation(vertexLocation) {}
20
22 context->BindVertexBuffer(m_vertexBuffer, 0);
23
24 for (const auto& b : m_constantBuffers)
25 if (b) context->BindConstantBuffer(b, b->GetSlot());
26
27 context->Draw(m_vertexCount, m_startVertexLocation);
28}
29
31 context->BindVertexBuffer(m_vertexBuffer, 0);
32 for (const auto& b : m_constantBuffers) {
33 if (b && b->GetSlot() == 0) {
34 context->BindConstantBuffer(b, 0);
35 break;
36 }
37 }
38 context->Draw(m_vertexCount, m_startVertexLocation);
39}
40
42 for (const auto& b : m_constantBuffers) {
43 if (b && b->GetSlot() == 3) return true;
44 }
45 return false;
46}
47
49 RefPtr<BufferBase> indexBuffer,
50 List<RefPtr<BufferBase>> constantBuffers,
51 uint32_t indexCount,
52 uint32_t startIndexLocation,
53 int32_t baseVertexLocation)
54 : m_vertexBuffer(vertexBuffer),
55 m_indexBuffer(indexBuffer),
56 m_constantBuffers(constantBuffers),
57 m_indexCount(indexCount),
58 m_startIndexLocation(startIndexLocation),
59 m_baseVertexLocation(baseVertexLocation) {}
60
61/// Binds buffers, routes bone data through the skinned pipeline when present, then draws.
63 context->BindVertexBuffer(m_vertexBuffer, 0);
64 context->BindIndexBuffer(m_indexBuffer, 0);
65
66 // Detect if this is a skinned mesh (has bone buffer at slot 3)
67 bool hasBones = false;
68 for (const auto& b : m_constantBuffers) {
69 if (b && b->GetSlot() == 3) { hasBones = true; break; }
70 }
71
72 // Switch to skinned pipeline before binding any constants
73 // (Vulkan pipeline change invalidates push constants)
74 if (hasBones) context->BeginSkinnedPass();
75
76 for (const auto& b : m_constantBuffers) {
77 if (!b) continue;
78 if (b->GetSlot() == 3) {
79 context->BindBoneBuffer(b);
80 } else {
81 context->BindConstantBuffer(b, b->GetSlot());
82 }
83 }
84
85 context->DrawIndexed(m_indexCount, m_startIndexLocation,
86 m_baseVertexLocation);
87
88 // Switch back to default pipeline for subsequent non-skinned draws
89 if (hasBones) context->EndSkinnedPass();
90}
91
93 context->BindVertexBuffer(m_vertexBuffer, 0);
94 context->BindIndexBuffer(m_indexBuffer, 0);
95 for (const auto& b : m_constantBuffers) {
96 if (b && b->GetSlot() == 0) {
97 context->BindConstantBuffer(b, 0);
98 break;
99 }
100 }
101 context->DrawIndexed(m_indexCount, m_startIndexLocation,
102 m_baseVertexLocation);
103}
104
106 for (const auto& b : m_constantBuffers) {
107 if (b && b->GetSlot() == 3) return true;
108 }
109 return false;
110}
111
112/// Copies the payload into inline storage, falling back to a heap allocation past INLINE_CAPACITY.
114 void* Data,
115 uint16_t Size) :
116 constantBuffer(buffer),
117 Size(Size) {
118 if (Size <= INLINE_CAPACITY) {
119 // Fast path: no heap allocation for small buffers
120 memcpy(m_inlineData, Data, Size);
121 } else {
122 // Fallback for large buffers
123 m_heapData = malloc(Size);
124 if (m_heapData) {
125 memcpy(m_heapData, Data, Size);
126 } else {
127 SLEAK_ERROR("Failed to allocate memory at UpdateConstantBufferCommand!");
128 }
129 }
130}
131
133 void* data = m_heapData ? m_heapData : static_cast<void*>(m_inlineData);
134 constantBuffer->Update(data, Size);
135}
136
138 constantBuffer(buffer), slot(slot) {}
139
141 context->BindConstantBuffer(constantBuffer, slot);
142}
143
145
147 context->SetRenderMode(mode);
148}
149
151
153 context->SetRenderFace(face);
154}
155
157 : m_material(material) {}
158
159/// Binds the material either through the deferred PBR path or the plain forward path, per active pass.
161 if (m_material) {
162 if (context->IsInGeometryPass()) {
163 // PBR deferred geometry pass: bind pipeline + all 6 PBR textures +
164 // material UBO as a single Vulkan descriptor set update.
165 context->BindPBRMaterial(m_material);
166 } else {
167 m_material->Bind();
168 // Bind diffuse texture through RenderContext (needed for Vulkan
169 // descriptor set switching — OpenGL already binds via Texture::Bind())
170 auto* diffuse = m_material->GetDiffuseTexture();
171 if (diffuse) context->BindTextureRaw(diffuse, 0);
172 }
173 }
174}
175
177 : m_executeFunction(function) {}
178
180 if (m_executeFunction)
181 m_executeFunction(context);
182}
183
184}
185
186}
#define SLEAK_ERROR(...)
Definition Logger.hpp:22
Implements a dynamic array-like list for storing and managing a collection of elements.
Definition List.hpp:20
void Execute(RenderContext *context) override
Replays this command into the main forward/deferred pass.
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,...
BindMaterialCommand(::Sleak::Material *material)
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)
void Execute(RenderContext *context) override
Replays this command into the main forward/deferred pass.
DrawIndexedCommand(RefPtr< BufferBase > vertexBuffer, RefPtr< BufferBase > indexBuffer, List< RefPtr< BufferBase > > constantBuffers, uint32_t indexCount, uint32_t startIndexLocation=0, int32_t baseVertexLocation=0)
void Execute(RenderContext *context) override
Binds buffers, routes bone data through the skinned pipeline when present, then draws.
void ExecuteShadow(RenderContext *context) override
Abstract interface for high-level graphics command execution and resource management.
virtual void BindBoneBuffer(RefPtr< BufferBase > buffer)
virtual void Draw(uint32_t vertexCount)=0
Non-indexed draw from the currently bound vertex buffer.
virtual void SetRenderMode(RenderMode mode)=0
virtual void BindVertexBuffer(RefPtr< BufferBase > buffer, uint32_t slot=0)=0
virtual void BindIndexBuffer(RefPtr< BufferBase > buffer, uint32_t slot=0)=0
virtual void BindTextureRaw(Sleak::Texture *texture, uint32_t slot=0)
virtual void BindPBRMaterial(Sleak::Material *material)
virtual void SetRenderFace(RenderFace face)=0
virtual void BindConstantBuffer(RefPtr< BufferBase > buffer, uint32_t slot=0)=0
virtual void DrawIndexed(uint32_t indexCount)=0
Indexed draw from the currently bound vertex/index buffers.
void Execute(RenderContext *context) override
Replays this command into the main forward/deferred pass.
void Execute(RenderContext *context) override
Replays this command into the main forward/deferred pass.
void Execute(RenderContext *context) override
Replays this command into the main forward/deferred pass.
UpdateConstantBufferCommand(RefPtr< BufferBase > buffer, void *Data, uint16_t Size)
Copies the payload into inline storage, falling back to a heap allocation past INLINE_CAPACITY.
Backend-facing rendering layer shared by the four graphics backends.
RenderMode
Rasterizer fill style for a draw.
RenderFace
Which triangle winding gets culled.
Root namespace for everything the engine exposes.
Definition Camera.hpp:10