Estella 0.113.0
A fast 2D and 3D game engine powered by WebAssembly and ECS
Loading...
Searching...
No Matches
BatchBuilder.hpp
Go to the documentation of this file.
1// SPDX-License-Identifier: Apache-2.0
2// SPDX-FileCopyrightText: Copyright (c) 2024-present ESEngine Team
22#pragma once
23
24// =============================================================================
25// Includes
26// =============================================================================
27
28#include "../../core/Types.hpp"
29#include "./BatchVertex.hpp"
30#include "./BlendMode.hpp"
31#include "./ClipState.hpp"
32#include "./DrawCommand.hpp"
33#include "./DrawList.hpp"
34#include "./RenderItem.hpp"
37
38#include <glm/glm.hpp>
39
40namespace esengine {
41
42// =============================================================================
43// Shared quad geometry (single source of truth)
44// =============================================================================
45
47inline constexpr glm::vec4 BATCH_QUAD_POSITIONS[4] = {
48 { -0.5f, -0.5f, 0.0f, 1.0f },
49 { 0.5f, -0.5f, 0.0f, 1.0f },
50 { 0.5f, 0.5f, 0.0f, 1.0f },
51 { -0.5f, 0.5f, 0.0f, 1.0f },
52};
53
55inline constexpr glm::vec2 BATCH_QUAD_TEX_COORDS[4] = {
56 { 0.0f, 0.0f },
57 { 1.0f, 0.0f },
58 { 1.0f, 1.0f },
59 { 0.0f, 1.0f },
60};
61
63inline constexpr u32 BATCH_QUAD_INDICES[6] = { 0, 1, 2, 2, 3, 0 };
64
67inline glm::vec3 rotatePoint(const glm::vec3& center, f32 px, f32 py, f32 cosA, f32 sinA) {
68 f32 dx = px - center.x;
69 f32 dy = py - center.y;
70 return { center.x + dx * cosA - dy * sinA,
71 center.y + dx * sinA + dy * cosA,
72 center.z };
73}
74
75// =============================================================================
76// Batch submission
77// =============================================================================
78
83 // Where this draw sits INSIDE its sorting layer; higher draws on top. Outranks depth
84 // and y-sort alike, so it holds whatever ordering the layer was given. Clamped to
85 // -128..127; 0 says nothing, leaving a renderable without such a field as it was.
88 // The program of the scene's depth prepass for this draw; 0 keeps it out of one. @see DrawCommand::depth_shader_id.
92 // A second sampler this draw needs for itself (a mesh's normal map), bound to
93 // slot 1. Not the same thing as the Batch stream's extra slots, which are a
94 // MERGE product chosen per vertex.
96 // The frame's shadow map, on the slot the injected Lit header samples. Per-draw
97 // like the two above rather than a global unit: which draws receive a shadow is a
98 // question the collect answers, and the device has no "bind for everyone" seam.
100 // The frame environment's prefiltered reflection, one slot further on. Same
101 // terms as the shadow map: per-draw, and only where a mesh vertex source asked.
103 // How near the draw's anchor is to the viewer, larger = nearer —
104 // CameraView::viewDepth, not a world coordinate. Only that orders blended draws
105 // the way they composite once the camera has been turned off head-on.
106 f32 depth = 0.0f;
107 // World-space Y of the draw's anchor. Consumed only when the layer is y-sorted
108 // (DrawList::ySortMask); layers outside 0..31 cannot y-sort.
109 f32 y = 0.0f;
110 // Mask bit of the layer this draw belongs to for camera culling; 0 derives it from
111 // `layer`. UI sets it from its Canvas, whose `layer` is tree order, not membership.
115 // Material handle + the render state resolved from it (defaults when materialId == 0).
117 bool depthTest = false;
118 bool depthWrite = true;
119 u8 cull = 0;
122 // The transient stream this draw's geometry lives in. Only the Batch stream carries
123 // a per-vertex texIndex, so only it participates in the multi-texture merge.
125 // > 0 selects an instanced draw: indexCount indices drawn instanceCount times, with
126 // per-instance attributes based at the vertex byte offset (see DrawCommand).
128 // Bytes one instance record occupies. Needed to coalesce two such draws into
129 // one: the merge relocates their records so they sit next to each other.
131 // Instance records a compute kernel already wrote to the GPU (ParticleInstance layout): drawn from there instead
132 // of from the stream, so the draw's vertexByteOffset means nothing.
134 // Geometry the GPU already holds. Set together with a MeshInstance stream, whose
135 // offset is the draw's vertexByteOffset: the mesh supplies the vertices and the
136 // frame supplies only the transforms.
140
141 // A skinned draw's pose, already in the frame's matrix pool. Passed through
142 // like the buffers above: what deforms a draw is the draw's, not the batch's.
145
146 // The shapes this draw is blended towards: the block in the frame's pool
147 // (1-based, 0 = none) and the mesh's own delta texture behind it.
162};
163
166bool batchEntityVisible(const DrawList& drawList, const ClipState& clips,
167 Entity entity, i32 layer, u32 cullBit = 0);
168
176void pushBatchDraw(DrawList& drawList, const ClipState& clips,
177 u32 vertexByteOffset, u32 vertexCount, u32 indexOffset, u32 indexCount,
178 const BatchDrawKey& key);
179
187void pushBatchCommand(TransientBufferPool& pool, DrawList& drawList, const ClipState& clips,
188 u32 vertexByteOffset, u32 vertexCount, const u32* localIndices, u32 indexCount,
189 const BatchDrawKey& key);
190void pushBatchCommand(TransientBufferPool& pool, DrawList& drawList, const ClipState& clips,
191 u32 vertexByteOffset, u32 vertexCount, const u16* localIndices, u32 indexCount,
192 const BatchDrawKey& key);
193
201void appendIndexedDraw(TransientBufferPool& pool, DrawList& drawList, const ClipState& clips,
202 const void* verts, u32 vertexCount,
203 const u32* localIndices, u32 indexCount,
204 const BatchDrawKey& key);
205
211void appendIndexedBatch(TransientBufferPool& pool, DrawList& drawList, const ClipState& clips,
212 const BatchVertex* verts, u32 vertexCount,
213 const u32* localIndices, u32 indexCount,
214 const BatchDrawKey& key);
215
216void appendQuads(TransientBufferPool& pool, DrawList& drawList, const ClipState& clips,
217 const BatchVertex* quads, u32 quadCount, const BatchDrawKey& key);
218
219inline void appendQuad(TransientBufferPool& pool, DrawList& drawList, const ClipState& clips,
220 const BatchVertex quad[4], const BatchDrawKey& key) {
221 appendQuads(pool, drawList, clips, quad, 1, key);
222}
223
224} // namespace esengine
Blend mode enumeration for custom rendering.
Core type definitions and utilities for ESEngine.
void appendQuad(TransientBufferPool &pool, DrawList &drawList, const ClipState &clips, const BatchVertex quad[4], const BatchDrawKey &key)
constexpr glm::vec2 BATCH_QUAD_TEX_COORDS[4]
Unit-quad texture coordinates matching BATCH_QUAD_POSITIONS.
void appendQuads(TransientBufferPool &pool, DrawList &drawList, const ClipState &clips, const BatchVertex *quads, u32 quadCount, const BatchDrawKey &key)
std::int32_t i32
Signed 32-bit integer.
Definition Types.hpp:54
void appendIndexedDraw(TransientBufferPool &pool, DrawList &drawList, const ClipState &clips, const void *verts, u32 vertexCount, const u32 *localIndices, u32 indexCount, const BatchDrawKey &key)
Copies raw vertices into the key's stream, then pushes one DrawCommand spanning them.
void pushBatchDraw(DrawList &drawList, const ClipState &clips, u32 vertexByteOffset, u32 vertexCount, u32 indexOffset, u32 indexCount, const BatchDrawKey &key)
Atomic primitive: assemble + clip + push one DrawCommand for an index range ALREADY written into (or,...
constexpr Entity INVALID_ENTITY
Invalid entity sentinel value.
Definition Types.hpp:174
std::int16_t i16
Signed 16-bit integer.
Definition Types.hpp:52
constexpr glm::vec4 BATCH_QUAD_POSITIONS[4]
Canonical centered unit-quad corner positions (CCW).
std::uint32_t u32
Unsigned 32-bit integer.
Definition Types.hpp:45
BlendMode
Predefined blend modes for rendering.
Definition BlendMode.hpp:22
std::uint8_t u8
Unsigned 8-bit integer.
Definition Types.hpp:41
std::uint16_t u16
Unsigned 16-bit integer.
Definition Types.hpp:43
void pushBatchCommand(TransientBufferPool &pool, DrawList &drawList, const ClipState &clips, u32 vertexByteOffset, u32 vertexCount, const u32 *localIndices, u32 indexCount, const BatchDrawKey &key)
Pushes a DrawCommand for vertices ALREADY resident in the Batch stream at vertexByteOffset.
void appendIndexedBatch(TransientBufferPool &pool, DrawList &drawList, const ClipState &clips, const BatchVertex *verts, u32 vertexCount, const u32 *localIndices, u32 indexCount, const BatchDrawKey &key)
Copies verts into the Batch stream, then pushes one DrawCommand spanning them.
LayoutId
Identifies a transient vertex stream / vertex format. Each layout has its own VBO+EBO+VAO in Transien...
Definition GfxEnums.hpp:88
bool batchEntityVisible(const DrawList &drawList, const ClipState &clips, Entity entity, i32 layer, u32 cullBit)
glm::vec3 rotatePoint(const glm::vec3 &center, f32 px, f32 py, f32 cosA, f32 sinA)
Rotates a point around center in the XY plane, carrying the center's z through untouched — the rotati...
constexpr u32 BATCH_QUAD_INDICES[6]
Canonical two-triangle quad winding shared by every batch path.
GfxTopology
How a pipeline's indices assemble into primitives. Baked into the pipeline, as WebGPU bakes it,...
@ Triangles
Every three indices a triangle: all geometry the engine draws.
float f32
32-bit floating point
Definition Types.hpp:59
Non-geometry attributes of a batch draw — everything needed to build the sort key + DrawCommand.
i16 depthBias
Depth-buffer units toward the eye.
GfxTopology topology
How the indices assemble.
u8 cull
CullMode: 0 = none, 1 = back, 2 = front.
VertexLayoutHandle vertexLayout
One vertex of the shared batch stream.
Entity identifier with packed index + generation.
Definition Types.hpp:135