Estella 0.113.0
A fast 2D and 3D game engine powered by WebAssembly and ECS
Loading...
Searching...
No Matches
ImmediateDraw.hpp
Go to the documentation of this file.
1// SPDX-License-Identifier: Apache-2.0
2// SPDX-FileCopyrightText: Copyright (c) 2024-present ESEngine Team
19#pragma once
20
21#include "../../core/Types.hpp"
22#include "../../math/Math.hpp"
26#include "./BatchVertex.hpp"
27
28#include <glm/glm.hpp>
29#include <string>
30#include <vector>
31#include <span>
32
33namespace esengine {
34
35class GfxDevice;
36class RenderContext;
37
38namespace resource {
39 class ResourceManager;
40}
41
61public:
68 ImmediateDraw(GfxDevice& device, RenderContext& context,
69 resource::ResourceManager& resource_manager);
71
72 ImmediateDraw(const ImmediateDraw&) = delete;
74
75 // =========================================================================
76 // Lifecycle
77 // =========================================================================
78
79 void init();
80
81 void shutdown();
82
83 // =========================================================================
84 // Frame Management
85 // =========================================================================
86
88 void begin(const glm::mat4& viewProjection, i32 viewportWidth = 0, i32 viewportHeight = 0);
89
91 void end();
92
94 void deferEnd();
95
97 void flushDeferred();
98
104 void flushDeferred(ShaderHandle program);
105
107 bool hasDeferred() const { return deferredGeometry_; }
108
110 const glm::mat4& viewProjection() const { return viewProjection_; }
111
117 void flush();
118
119 // =========================================================================
120 // Line Drawing
121 // =========================================================================
122
123 void line(const glm::vec2& from, const glm::vec2& to,
124 const glm::vec4& color, f32 thickness = 1.0f);
125
133 void line3D(const glm::vec3& from, const glm::vec3& to,
134 const glm::vec4& color, f32 thickness = 1.0f);
135
137 void line3DScreen(const glm::vec3& from, const glm::vec3& to,
138 const glm::vec4& color, f32 thickness = 1.0f);
139
146 void line3DScreenOffset(const glm::vec3& anchor,
147 const glm::vec2& fromOffset,
148 const glm::vec2& toOffset,
149 const glm::vec4& color, f32 thickness = 1.0f);
150
151 void polyline(std::span<const glm::vec2> vertices, const glm::vec4& color,
152 f32 thickness = 1.0f, bool closed = false);
153
154 // =========================================================================
155 // Rectangle Drawing
156 // =========================================================================
157
158 void rect(const glm::vec2& position, const glm::vec2& size,
159 const glm::vec4& color, bool filled = true);
160
161 void rectOutline(const glm::vec2& position, const glm::vec2& size,
162 const glm::vec4& color, f32 thickness = 1.0f);
163
164 // =========================================================================
165 // Circle Drawing
166 // =========================================================================
167
168 void circle(const glm::vec2& center, f32 radius,
169 const glm::vec4& color, bool filled = true, i32 segments = 32);
170
171 void circleOutline(const glm::vec2& center, f32 radius,
172 const glm::vec4& color, f32 thickness = 1.0f, i32 segments = 32);
173
174 // =========================================================================
175 // Polygon Drawing
176 // =========================================================================
177
178 void polygon(std::span<const glm::vec2> vertices, const glm::vec4& color);
179
188 void triangles(const f32* positions, u32 vertexCount, const f32* colors, const u32* indices, u32 indexCount,
189 const glm::vec4& tint, const f32 xform[6]);
190
197 u32 createShape(const f32* positions, u32 vertexCount, const f32* colors, const u32* indices, u32 indexCount);
198
200 void destroyMesh(u32 id);
201
206 void drawMesh(u32 id, const glm::vec4& tint, const f32 xform[6]);
207
214 void drawMeshMany(u32 id, const void* records, u32 count);
215
216 u32 meshCount() const { return liveMeshes_; }
217
218 // =========================================================================
219 // Texture Drawing
220 // =========================================================================
221
222 void texture(const glm::vec2& position, const glm::vec2& size,
223 u32 textureId, const glm::vec4& tint = glm::vec4(1.0f));
224
225 void textureRotated(const glm::vec2& position, const glm::vec2& size,
226 f32 rotation, u32 textureId,
227 const glm::vec4& tint = glm::vec4(1.0f));
228
229 // =========================================================================
230 // Configuration
231 // =========================================================================
232
233 void setLayer(i32 layer) { currentLayer_ = layer; }
234 i32 getLayer() const { return currentLayer_; }
235 void setDepth(f32 depth) { currentDepth_ = depth; }
236 f32 getDepth() const { return currentDepth_; }
237
239 void setBlendMode(BlendMode mode);
241 void setDepthTest(bool enabled);
243 void setDepthState(bool test, bool write);
244 BlendMode blendMode() const { return current_desc_.blend; }
245 bool depthTest() const { return current_desc_.depthTest; }
246 bool depthWrite() const { return current_desc_.depthWrite; }
252 void setDither(bool on, bool phase = false) { dither_ = on; ditherPhase_ = phase; }
253 bool dither() const { return dither_; }
254 bool ditherPhase() const { return ditherPhase_; }
256 void deferBegin() { recording_ = true; }
257
258 // =========================================================================
259 // Statistics
260 // =========================================================================
261
262 u32 getDrawCallCount() const { return drawCallCount_; }
263 u32 getPrimitiveCount() const { return primitiveCount_; }
264
265private:
267 void emitQuad(const glm::vec2& center, const glm::vec2& size, f32 rotation,
268 const glm::vec4& color, u32 textureId,
269 const glm::vec2& uvOffset = glm::vec2(0.0f),
270 const glm::vec2& uvScale = glm::vec2(1.0f));
272 void emitTriangle(const glm::vec2& p0, const glm::vec2& p1, const glm::vec2& p2,
273 const glm::vec4& color);
275 void useTexture(u32 textureId);
277 void flushRun();
279 void uploadInstances();
281 void drawRun(PipelineHandle pipeline, u32 mesh, u32 first, u32 count);
283 PipelineHandle instancedPipeline(const PipelineDesc& state, bool display);
284 resource::ShaderHandle compileVariant(const std::vector<std::string>& features);
285
286 GfxDevice& device_;
287 RenderContext& context_;
288 resource::ResourceManager& resource_manager_;
289
293 resource::ShaderHandle batch_shader_ref_;
294 ShaderHandle batch_shader_ = ShaderHandle::Invalid;
295 PipelineDesc base_desc_{};
296 PipelineDesc current_desc_{};
297 bool dither_ = false;
298 bool ditherPhase_ = false;
299 std::vector<BatchVertex> ditherScratch_;
301 u32 appendBatch(const BatchVertex* vertices, usize count);
303 u32 white_texture_id_ = 0;
304 u32 currentTexture_ = 0;
305 bool pendingGeometry_ = false;
307 std::vector<BatchVertex> triangleVertices_;
308 std::vector<u32> triangleIndices_;
309 bool deferredGeometry_ = false;
310 struct DeferredBatch {
311 PipelineHandle pipeline;
312 u32 texture;
313 u32 firstIndex;
314 u32 indexCount;
316 u32 mesh = 0;
317 u32 firstInstance = 0;
318 u32 instanceCount = 0;
319 };
320 std::vector<DeferredBatch> deferredBatches_;
321 u32 recordedIndices_ = 0;
322 bool recording_ = false;
323
324 struct ResidentMesh {
327 u32 indexCount = 0;
328 bool live = false;
329 };
331 struct InstanceRecord {
332 f32 xform[6];
333 u32 tint;
334 };
335 std::vector<ResidentMesh> meshes_;
336 std::vector<u32> freeMeshes_;
337 std::vector<u32> doomedMeshes_;
338 u32 liveMeshes_ = 0;
339 std::vector<InstanceRecord> instances_;
340 u32 instancesUploaded_ = 0;
341 BufferHandle instanceVbo_ = BufferHandle::Invalid;
342 u32 instanceCapacity_ = 0;
345 resource::ShaderHandle instancedShader_;
346 resource::ShaderHandle instancedDisplayShader_;
347 bool instancedLinear_ = false;
348 bool instancedCompiled_ = false;
349 bool instancedDisplayCompiled_ = false;
350 struct Run {
351 u32 mesh = 0;
352 u32 first = 0;
353 u32 count = 0;
354 };
355 Run pendingRun_;
356
359 glm::vec3 viewForward_{0.0f, 0.0f, -1.0f};
361 glm::mat4 viewProjection_{1.0f};
362 glm::mat4 inverseViewProjection_{1.0f};
363 f32 viewportWidth_ = 1.0f;
364 f32 viewportHeight_ = 1.0f;
365
366 i32 currentLayer_ = 0;
367 f32 currentDepth_ = 0.0f;
368 u32 primitiveCount_ = 0;
369 u32 drawCallCount_ = 0;
370 bool initialized_ = false;
371 bool inFrame_ = false;
372};
373
374} // namespace esengine
Type-safe resource handle system.
Math utilities and GLM configuration for ESEngine.
Immutable GPU pipeline state — the backend-agnostic unit of "how to draw".
Core type definitions and utilities for ESEngine.
Abstract graphics device interface.
Definition GfxDevice.hpp:75
void polygon(std::span< const glm::vec2 > vertices, const glm::vec4 &color)
void line3DScreenOffset(const glm::vec3 &anchor, const glm::vec2 &fromOffset, const glm::vec2 &toOffset, const glm::vec4 &color, f32 thickness=1.0f)
A framebuffer-sized line attached to one world-space anchor.
void rect(const glm::vec2 &position, const glm::vec2 &size, const glm::vec4 &color, bool filled=true)
void drawMeshMany(u32 id, const void *records, u32 count)
count copies of a mesh in one call: drawMesh for each, without a call a copy.
void line3D(const glm::vec3 &from, const glm::vec3 &to, const glm::vec4 &color, f32 thickness=1.0f)
A line between two points in SPACE, widened across the view.
void drawMesh(u32 id, const glm::vec4 &tint, const f32 xform[6])
void texture(const glm::vec2 &position, const glm::vec2 &size, u32 textureId, const glm::vec4 &tint=glm::vec4(1.0f))
void setDepthState(bool test, bool write)
ImmediateDraw(GfxDevice &device, RenderContext &context, resource::ResourceManager &resource_manager)
Constructs an immediate draw instance.
void circleOutline(const glm::vec2 &center, f32 radius, const glm::vec4 &color, f32 thickness=1.0f, i32 segments=32)
const glm::mat4 & viewProjection() const
void setDepthTest(bool enabled)
void end()
Ends the frame and submits all draw commands.
u32 createShape(const f32 *positions, u32 vertexCount, const f32 *colors, const u32 *indices, u32 indexCount)
Puts a shape on the GPU once, for drawMesh to draw any number of times without sending its vertices a...
void begin(const glm::mat4 &viewProjection, i32 viewportWidth=0, i32 viewportHeight=0)
Begins a new draw frame.
void triangles(const f32 *positions, u32 vertexCount, const f32 *colors, const u32 *indices, u32 indexCount, const glm::vec4 &tint, const f32 xform[6])
Triangles with a colour at every vertex, in one call: a shape tessellated once and drawn as often as ...
void setBlendMode(BlendMode mode)
void rectOutline(const glm::vec2 &position, const glm::vec2 &size, const glm::vec4 &color, f32 thickness=1.0f)
void line(const glm::vec2 &from, const glm::vec2 &to, const glm::vec4 &color, f32 thickness=1.0f)
ImmediateDraw & operator=(const ImmediateDraw &)=delete
void flush()
Flushes pending draw commands without ending the frame.
void textureRotated(const glm::vec2 &position, const glm::vec2 &size, f32 rotation, u32 textureId, const glm::vec4 &tint=glm::vec4(1.0f))
void polyline(std::span< const glm::vec2 > vertices, const glm::vec4 &color, f32 thickness=1.0f, bool closed=false)
BlendMode blendMode() const
void line3DScreen(const glm::vec3 &from, const glm::vec3 &to, const glm::vec4 &color, f32 thickness=1.0f)
void circle(const glm::vec2 &center, f32 radius, const glm::vec4 &color, bool filled=true, i32 segments=32)
void setDither(bool on, bool phase=false)
Draw what follows dithered: one half of a checkerboard (phase 0 or 1), at full colour; the two halves...
ImmediateDraw(const ImmediateDraw &)=delete
Central manager for GPU resources.
Handle< esengine::Shader > ShaderHandle
Handle to a shader resource.
Definition Handle.hpp:214
std::int32_t i32
Signed 32-bit integer.
Definition Types.hpp:54
std::size_t usize
Size type for indexing and sizes.
Definition Types.hpp:64
PipelineHandle
Opaque handle to a cached pipeline; PipelineHandle{0} is the null/invalid handle.
std::uint32_t u32
Unsigned 32-bit integer.
Definition Types.hpp:45
BlendMode
Predefined blend modes for rendering.
Definition BlendMode.hpp:22
float f32
32-bit floating point
Definition Types.hpp:59
Immutable description of a draw pipeline. Equality keys the pipeline cache, so two draws with the sam...