SleakEngine 1.0.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, m_startVertexLocation);
23
24 for (const auto& b : m_constantBuffers)
25 if (b) context->BindConstantBuffer(b, b->GetSlot());
26
27 context->Draw(m_vertexCount);
28}
29
31 context->BindVertexBuffer(m_vertexBuffer, m_startVertexLocation);
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);
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, m_startIndexLocation);
64 context->BindIndexBuffer(m_indexBuffer, m_startIndexLocation);
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);
86
87 // Switch back to default pipeline for subsequent non-skinned draws
88 if (hasBones) context->EndSkinnedPass();
89}
90
92 context->BindVertexBuffer(m_vertexBuffer, m_startIndexLocation);
93 context->BindIndexBuffer(m_indexBuffer, m_startIndexLocation);
94 for (const auto& b : m_constantBuffers) {
95 if (b && b->GetSlot() == 0) {
96 context->BindConstantBuffer(b, 0);
97 break;
98 }
99 }
100 context->DrawIndexed(m_indexCount);
101}
102
104 for (const auto& b : m_constantBuffers) {
105 if (b && b->GetSlot() == 3) return true;
106 }
107 return false;
108}
109
110/// Copies the payload into inline storage, falling back to a heap allocation past INLINE_CAPACITY.
112 void* Data,
113 uint16_t Size) :
114 constantBuffer(buffer),
115 Size(Size) {
116 if (Size <= INLINE_CAPACITY) {
117 // Fast path: no heap allocation for small buffers
118 memcpy(m_inlineData, Data, Size);
119 } else {
120 // Fallback for large buffers
121 m_heapData = malloc(Size);
122 if (m_heapData) {
123 memcpy(m_heapData, Data, Size);
124 } else {
125 SLEAK_ERROR("Failed to allocate memory at UpdateConstantBufferCommand!");
126 }
127 }
128}
129
131 void* data = m_heapData ? m_heapData : static_cast<void*>(m_inlineData);
132 constantBuffer->Update(data, Size);
133}
134
136 constantBuffer(buffer), slot(slot) {}
137
139 context->BindConstantBuffer(constantBuffer, slot);
140}
141
143
145 context->SetRenderMode(mode);
146}
147
149
151 context->SetRenderFace(face);
152}
153
155 : m_material(material) {}
156
157/// Binds the material either through the deferred PBR path or the plain forward path, per active pass.
159 if (m_material) {
160 if (context->IsInGeometryPass()) {
161 // PBR deferred geometry pass: bind pipeline + all 6 PBR textures +
162 // material UBO as a single Vulkan descriptor set update.
163 context->BindPBRMaterial(m_material);
164 } else {
165 m_material->Bind();
166 // Bind diffuse texture through RenderContext (needed for Vulkan
167 // descriptor set switching — OpenGL already binds via Texture::Bind())
168 auto* diffuse = m_material->GetDiffuseTexture();
169 if (diffuse) context->BindTextureRaw(diffuse, 0);
170 }
171 }
172}
173
175 : m_executeFunction(function) {}
176
178 if (m_executeFunction)
179 m_executeFunction(context);
180}
181
182}
183
184}
#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