Estella 0.113.0
A fast 2D and 3D game engine powered by WebAssembly and ECS
Loading...
Searching...
No Matches
MeshPlugin.hpp
Go to the documentation of this file.
1// SPDX-License-Identifier: Apache-2.0
2// SPDX-FileCopyrightText: Copyright (c) 2024-present ESEngine Team
3#pragma once
4
5#include <array>
10
11#include <glm/glm.hpp>
12#include <glm/gtc/quaternion.hpp>
13
14#include <unordered_map>
15#include <vector>
16
17namespace esengine {
18
19class Mesh;
20struct MaterialRecord;
21class MaterialStore;
22namespace ecs {
23struct MeshRenderer;
24struct Transform;
25} // namespace ecs
26
35public:
36 void init(RenderFrameContext& ctx) override;
39 bool castsShadows() const override { return true; }
40 bool drawsMotion() const override { return true; }
41 bool castInputs(RenderFrameContext& ctx, ecs::Registry& registry, u64& key) override;
42 bool castHeldInputs(u64& key) const override;
43 u32 castMoverCount() const override { return static_cast<u32>(movers_.size()); }
44 void collect(RenderCollectContext& ctx) override;
46 const Entity* entities, u32 count, bool shadowPasses) override;
50 u32 count, bool shadowPasses, bool residentGeometry);
52 u32 count, bool shadowPasses) override;
53 const char* collectScope() const override { return "render.collect.mesh"; }
54 bool clusterInspect(u32 entityId, u32& triangles, u32& clusters) const override;
55
56private:
58 u32 compiled_this_frame_ = 0;
60 u32 last_compiled_variant_ = 0;
61 std::vector<BatchVertex> scratch_;
65 std::array<u32, MESH_VARIANTS> mesh_programs_{};
66 std::array<bool, MESH_VARIANTS> mesh_compiled_{};
70 std::array<resource::ShaderHandle, MESH_VARIANTS> mesh_shaders_{};
72 std::vector<glm::mat4> pose_scratch_;
73 u32 meshProgram(RenderFrameContext& ctx, u32 variant);
80 u32 previewProgram(RenderFrameContext& ctx, u32 variant);
81 void scheduleSkins(RenderFrameContext& ctx, ecs::Registry& registry, SkinCache& cache) override;
82
85 struct Pose {
86 glm::mat4 previous{1.0f};
87 glm::mat4 current{1.0f};
90 std::vector<glm::mat4> bonesPrevious;
91 std::vector<glm::mat4> bonesCurrent;
92 u64 frame = 0;
94 glm::dvec3 origin{0.0};
95 };
96 std::unordered_map<u64, Pose> poses_;
97 void trackPose(Pose& pose, const RenderFrameContext& ctx, const glm::mat4& model,
98 const std::vector<glm::mat4>* bones = nullptr);
99 static u64 poseKey(const RenderFrameContext& ctx, Entity entity) {
100 return ctx.motion_key * 0x9E3779B97F4A7C15ull ^ static_cast<u64>(entity.id());
101 }
110 struct PlacedInstance {
111 glm::vec3 at{0.0f};
112 glm::vec3 centre{0.0f};
113 glm::vec3 half{0.0f};
114 f32 radius = 0.0f;
115 f32 grow = 0.0f;
116 };
117 struct MeshProxy {
118 Entity entity = INVALID_ENTITY;
120 u32 builtAt = 0;
122 u32 presentAt = 0;
123 u32 epoch = 0;
125 u8 present = 0;
127 bool kept = false;
128 bool depthTested = false;
129 f32 bend = 0.0f;
130 u64 meshRevision = 0;
131 u64 materialStore = 0;
132 u64 materialRevision = 0;
133 const Mesh* resident = nullptr;
134 const MaterialRecord* material = nullptr;
135 glm::vec3 position{0.0f};
136 glm::quat rotation{1.0f, 0.0f, 0.0f, 0.0f};
137 glm::vec3 scale{1.0f};
138 glm::vec3 boxCentre{0.0f};
139 glm::vec3 boxHalf{0.0f};
140 glm::mat4 model{1.0f};
142 glm::mat3 normalRows{1.0f};
144 MeshProgramFacts facts;
147 Pose* pose = nullptr;
148 u64 poseKey = 0;
149 u32 poseGeneration = 0;
150 bool placed = false;
151 const Mesh* coarse = nullptr;
152 u64 coarseRevision = 0;
153 std::vector<PlacedInstance> instances;
154 std::vector<u8> records;
155 };
163 struct ClusterCut {
166 u32 capacity = 0;
167 u32 indexCount = 0;
168 u32 clusters = 0;
169 u64 signature = 0;
170 const Mesh* mesh = nullptr;
171 u64 meshRevision = 0;
172 u64 deviceGeneration = 0;
173 lod::ClusterEye eye;
175 std::vector<u32> ids;
176 bool picked = false;
178 u64 usedAt = 0;
179 };
180 std::unordered_map<u32, ClusterCut> cluster_cuts_;
181 std::vector<u32> cluster_picked_, cluster_indices_;
182 u64 cluster_serial_ = 0;
185 f32 cluster_scale_ = 0.0f;
186 bool cluster_flat_ = false;
187 u32 cluster_meshes_ = 0, cluster_triangles_ = 0, cluster_source_triangles_ = 0, cluster_rebuilt_ = 0, cluster_drawn_ = 0;
189 u32 waiting_materials_ = 0;
191 bool compile_in_place_ = false;
195 const ClusterCut* clusterCut(RenderCollectContext& collect, Entity entity, const Mesh& mesh, const glm::vec3& position,
196 const glm::quat& rotation, const glm::vec3& scale);
197
199 u32 pose_generation_ = 0;
201 Pose* poseOf(const RenderFrameContext& ctx, Entity entity, MeshProxy* kept, bool make);
203 std::vector<u32> proxy_slot_;
204 std::vector<MeshProxy> proxies_;
207 u32 proxy_epoch_ = 1;
208 u64 proxy_registry_ = 0;
209 glm::dvec3 proxy_origin_{0.0};
210 const MaterialStore* proxy_materials_ = nullptr;
212 u32 proxies_kept_ = 0, proxies_built_ = 0;
213 u32 proxies_stale_total_ = 0;
214 bool warned_stale_ = false;
219 MeshProxy* retain(RenderCollectContext& collect, Entity entity, const ecs::MeshRenderer& mesh,
220 ecs::Transform& transform, u32 now, u32 rulesChangedAt);
221 void deriveProxy(RenderCollectContext& collect, MeshProxy& proxy, Entity entity, const ecs::MeshRenderer& mesh,
222 ecs::Transform& transform, u8 present, u32 now);
224 u64 cast_registry_ = 0;
225 u32 cast_tick_ = 0;
226 u32 cast_activity_ = 0;
227 u64 cast_serial_ = 0;
228 u64 cast_held_serial_ = 0;
229 u64 cast_held_key_ = 0;
230 bool cast_held_vouched_ = false;
231 u32 cast_look_ = 0;
232 std::vector<u64> cast_changed_at_;
233 std::vector<u32> mover_at_;
234 std::vector<Entity> movers_;
235 bool isMover(Entity entity) const {
236 const u32 index = entity.index();
237 return index < mover_at_.size() && mover_at_[index] != 0 && movers_[mover_at_[index] - 1] == entity;
238 }
239 void addMover(Entity entity);
240 bool dropStillMovers();
241 void clearMovers();
243 u32 programs_built_ = 0;
244 bool warned_material_ = false;
245 bool warned_bones_ = false;
246 bool warned_lod_ = false;
247};
248
249} // namespace esengine
Which clusters of a mesh's hierarchy one eye draws.
Type-safe resource handle system.
Maps a material handle to its resolved record. Owned by RenderContext so it is reachable from both th...
Scene-level 2D mesh renderer (MeshRenderer component).
bool castsShadows() const override
u32 castMoverCount() const override
const char * collectScope() const override
What this plugin's collect is called in a frame profile.
void collect(RenderCollectContext &ctx) override
bool castInputs(RenderFrameContext &ctx, ecs::Registry &registry, u64 &key) override
For a plugin that casts: one number for everything its depth collect reads apart from the shadow view...
bool clusterInspect(u32 entityId, u32 &triangles, u32 &clusters) const override
What the last frames drew of the Cluster LOD mesh of the entity entityId.
RenderPrewarmResult prewarm(RenderFrameContext &ctx, ecs::Registry &registry, const Entity *entities, u32 count, bool shadowPasses) override
bool drawsMotion() const override
RenderPrewarmResult prewarmFacts(RenderFrameContext &ctx, const MeshProgramFacts *facts, u32 count, bool shadowPasses, bool residentGeometry)
Ready programs from facts already gathered — the door a PREPARED cell comes through,...
RenderPrewarmResult prewarmDocument(RenderFrameContext &ctx, const MeshDocumentRecord *rows, u32 count, bool shadowPasses) override
The same readiness, asked of a PREPARED cell's own description.
void init(RenderFrameContext &ctx) override
bool castHeldInputs(u64 &key) const override
Buffers, the layout that describes them, and the bounds culling reads.
Definition Mesh.hpp:85
Central container for all entities and components.
Definition Registry.hpp:73
std::uint64_t u64
Unsigned 64-bit integer.
Definition Types.hpp:47
constexpr Entity INVALID_ENTITY
Invalid entity sentinel value.
Definition Types.hpp:174
std::uint32_t u32
Unsigned 32-bit integer.
Definition Types.hpp:45
std::uint8_t u8
Unsigned 8-bit integer.
Definition Types.hpp:41
static EstellaContext & ctx()
float f32
32-bit floating point
Definition Types.hpp:59
Entity identifier with packed index + generation.
Definition Types.hpp:135
A material's resolved render state (P0) plus its packed std140 constants (P1).
What a renderable's program key is made of, with no say in where the facts came from — a live entity'...
What readying a set of renderables' programs found. Counts, not JSON: this crosses no boundary and pa...
Unified transform component.
Definition Transform.hpp:45