Skip to content

A fast 2D and 3D game engine — TypeScript SDK, C++/WebAssembly core, visual editor. Ship one project to web, desktop, WeChat MiniGames, playable ads, and native Android / iOS.

fix: implement particle rendering in standard collect path

esenginec7dc190655cf
diff --git a/src/esengine/renderer/DrawCommand.hpp b/src/esengine/renderer/DrawCommand.hpp
index 12c4c8f8b..50a93d236 100644
--- a/src/esengine/renderer/DrawCommand.hpp
+++ b/src/esengine/renderer/DrawCommand.hpp
@@ -16,6 +16,7 @@ static constexpr u32 MAX_CMD_TEXTURE_SLOTS = 8;
static constexpr u16 CMD_STATE_SCISSOR = 0x01;
static constexpr u16 CMD_STATE_STENCIL_WRITE = 0x02;
static constexpr u16 CMD_STATE_STENCIL_TEST = 0x04;
+static constexpr u16 CMD_STATE_CUSTOM_DRAW = 0x08;
struct DrawCommand {
u64 sort_key = 0;
diff --git a/src/esengine/renderer/DrawList.cpp b/src/esengine/renderer/DrawList.cpp
index 96f3f4b95..9a8633d7e 100644
--- a/src/esengine/renderer/DrawList.cpp
+++ b/src/esengine/renderer/DrawList.cpp
@@ -69,7 +69,8 @@ void DrawList::finalize() {
void DrawList::execute(StateTracker& state, TransientBufferPool& buffers,
const glm::mat4& viewProjection,
- FrameCapture* capture) {
+ FrameCapture* capture,
+ const CustomDrawFn& customDraw) {
u32 lastShader = 0;
for (u32 i = 0; i < merged_draw_calls_; ++i) {
const auto& cmd = commands_[i];
@@ -103,13 +104,19 @@ void DrawList::execute(StateTracker& state, TransientBufferPool& buffers,
state.bindTexture(slot, cmd.texture_ids[slot]);
}
- buffers.bindLayout(cmd.layout_id);
+ bool isCustom = (cmd.state_flags & CMD_STATE_CUSTOM_DRAW) != 0;
- glDrawElements(GL_TRIANGLES,
- static_cast<GLsizei>(cmd.index_count),
- GL_UNSIGNED_SHORT,
- reinterpret_cast<void*>(
- static_cast<uintptr_t>(cmd.index_offset) * sizeof(u16)));
+ if (isCustom && customDraw) {
+ customDraw(cmd, state, buffers);
+ } else {
+ buffers.bindLayout(cmd.layout_id);
+
+ glDrawElements(GL_TRIANGLES,
+ static_cast<GLsizei>(cmd.index_count),
+ GL_UNSIGNED_SHORT,
+ reinterpret_cast<void*>(
+ static_cast<uintptr_t>(cmd.index_offset) * sizeof(u16)));
+ }
if (capture && capture->isCapturing()) {
capture->recordDrawCall(
diff --git a/src/esengine/renderer/DrawList.hpp b/src/esengine/renderer/DrawList.hpp
index 162477f73..52b037373 100644
--- a/src/esengine/renderer/DrawList.hpp
+++ b/src/esengine/renderer/DrawList.hpp
@@ -8,9 +8,15 @@
#include <glm/glm.hpp>
#include <vector>
+#include <functional>
namespace esengine {
+struct RenderFrameContext;
+
+using CustomDrawFn = std::function<void(const DrawCommand& cmd, StateTracker& state,
+ TransientBufferPool& buffers)>;
+
class DrawList {
public:
void clear();
@@ -20,7 +26,8 @@ public:
void execute(StateTracker& state, TransientBufferPool& buffers,
const glm::mat4& viewProjection,
- FrameCapture* capture = nullptr);
+ FrameCapture* capture = nullptr,
+ const CustomDrawFn& customDraw = nullptr);
u32 commandCount() const { return static_cast<u32>(commands_.size()); }
u32 mergedDrawCallCount() const { return merged_draw_calls_; }
diff --git a/src/esengine/renderer/RenderFrame.cpp b/src/esengine/renderer/RenderFrame.cpp
index 7419fe905..6f945fd92 100644
--- a/src/esengine/renderer/RenderFrame.cpp
+++ b/src/esengine/renderer/RenderFrame.cpp
@@ -181,7 +181,20 @@ void RenderFrame::flush() {
pool_.upload();
draw_list_.finalize();
- draw_list_.execute(state_tracker_, pool_, view_projection_, &frame_capture_);
+
+ auto ctx = makeContext();
+
+ auto customDrawFn = [this, &ctx](const DrawCommand& cmd, StateTracker& state,
+ TransientBufferPool& buffers) {
+ for (auto& plugin : plugins_) {
+ if (plugin->needsCustomDraw() && plugin->handlesType(cmd.type)) {
+ plugin->customDraw(cmd, state, buffers, ctx);
+ return;
+ }
+ }
+ };
+
+ draw_list_.execute(state_tracker_, pool_, view_projection_, &frame_capture_, customDrawFn);
stats_.draw_calls = draw_list_.mergedDrawCallCount();
for (u32 i = 0; i < draw_list_.commandCount(); ++i) {
@@ -359,10 +372,8 @@ void RenderFrame::buildClipState() {
}
}
-void RenderFrame::collectAll(ecs::Registry& registry, u32 skipFlags) {
- buildClipState();
-
- RenderFrameContext ctx{
+RenderFrameContext RenderFrame::makeContext() {
+ return {
context_,
resource_manager_,
context_.getWhiteTextureId(),
@@ -370,6 +381,12 @@ void RenderFrame::collectAll(ecs::Registry& registry, u32 skipFlags) {
current_stage_,
view_projection_
};
+}
+
+void RenderFrame::collectAll(ecs::Registry& registry, u32 skipFlags) {
+ buildClipState();
+
+ auto ctx = makeContext();
for (auto& plugin : plugins_) {
if (skipFlags != 0 && (skipFlags & plugin->skipFlag()) != 0) continue;
diff --git a/src/esengine/renderer/RenderFrame.hpp b/src/esengine/renderer/RenderFrame.hpp
index 86f13bf52..dca215e80 100644
--- a/src/esengine/renderer/RenderFrame.hpp
+++ b/src/esengine/renderer/RenderFrame.hpp
@@ -157,6 +157,7 @@ private:
ClipState clip_state_;
std::vector<std::unique_ptr<RenderTypePlugin>> plugins_;
+ RenderFrameContext makeContext();
void buildClipState();
u32 initBatchShader();
};
diff --git a/src/esengine/renderer/RenderTypePlugin.hpp b/src/esengine/renderer/RenderTypePlugin.hpp
index bc106afc6..87aea4947 100644
--- a/src/esengine/renderer/RenderTypePlugin.hpp
+++ b/src/esengine/renderer/RenderTypePlugin.hpp
@@ -46,6 +46,7 @@ public:
) = 0;
virtual bool needsCustomDraw() const { return false; }
+ virtual bool handlesType(RenderType type) const { (void)type; return false; }
virtual void customDraw(const DrawCommand& cmd,
StateTracker& state,
TransientBufferPool& buffers,
diff --git a/src/esengine/renderer/plugins/ParticlePlugin.cpp b/src/esengine/renderer/plugins/ParticlePlugin.cpp
index 9f36fa2ee..a7dd19b95 100644
--- a/src/esengine/renderer/plugins/ParticlePlugin.cpp
+++ b/src/esengine/renderer/plugins/ParticlePlugin.cpp
@@ -1,23 +1,25 @@
#include "ParticlePlugin.hpp"
#include "../RenderContext.hpp"
#include "../Texture.hpp"
+#include "../BatchVertex.hpp"
#include "../../ecs/components/Transform.hpp"
#include "../../ecs/components/ParticleEmitter.hpp"
#include "../../particle/ParticleSystem.hpp"
#include "../../particle/Particle.hpp"
#include <cmath>
-#include <algorithm>
namespace esengine {
+static constexpr u16 QUAD_INDICES[6] = { 0, 1, 2, 0, 2, 3 };
+
void ParticlePlugin::init(RenderFrameContext& ctx) {
batch_shader_id_ = ctx.batch_shader_id;
}
void ParticlePlugin::collect(
ecs::Registry& registry,
- const Frustum& frustum,
+ const Frustum&,
const ClipState& clips,
TransientBufferPool& buffers,
DrawList& draw_list,
@@ -25,9 +27,6 @@ void ParticlePlugin::collect(
) {
if (!particle_system_) return;
- entries_.clear();
- batches_.clear();
-
auto emitterView = registry.view<ecs::Transform, ecs::ParticleEmitter>();
for (auto entity : emitterView) {
@@ -38,7 +37,7 @@ void ParticlePlugin::collect(
transform.ensureDecomposed();
const auto* state = particle_system_->getState(entity);
- if (!state) continue;
+ if (!state || state->pool.aliveCount() == 0) continue;
u32 textureId = ctx.white_texture_id;
if (emitter.texture.isValid()) {
@@ -58,72 +57,114 @@ void ParticlePlugin::collect(
glm::vec3 emitterWorldPos = transform.worldPosition;
f32 emitterAngle = 0.0f;
glm::vec2 emitterScale(transform.worldScale);
+ f32 cosA = 1.0f, sinA = 0.0f;
if (isLocalSpace) {
const auto& rot = transform.worldRotation;
emitterAngle = 2.0f * std::atan2(rot.z, rot.w);
+ if (std::abs(emitterAngle) > 0.001f) {
+ cosA = std::cos(emitterAngle);
+ sinA = std::sin(emitterAngle);
+ }
}
- ParticleBatchData batch;
- batch.texture_id = textureId;
- batch.blend_mode = static_cast<BlendMode>(emitter.blendMode);
- batch.material_id = emitter.material;
- batch.first_entry = static_cast<u32>(entries_.size());
- batch.entity = entity;
+ BlendMode blendMode = static_cast<BlendMode>(emitter.blendMode);
+
+ u32 particleCount = state->pool.aliveCount();
+ u32 vertCount = particleCount * 4;
+ u32 idxCount = particleCount * 6;
+
+ u32 vertByteSize = vertCount * sizeof(BatchVertex);
+ u32 vertByteOffset = buffers.allocVertices(vertByteSize);
+ u32 idxOffset = buffers.allocIndices(idxCount);
+
+ auto* verts = reinterpret_cast<BatchVertex*>(buffers.vertexData() + vertByteOffset);
+ u16 baseVertex = static_cast<u16>(vertByteOffset / sizeof(BatchVertex));
- u32 particleCount = 0;
+ u32 pi = 0;
state->pool.forEachAlive([&](const particle::Particle& p) {
- ParticleRenderEntry entry;
+ glm::vec2 worldPos;
+ glm::vec2 size;
if (isLocalSpace) {
- glm::vec2 worldPos = glm::vec2(emitterWorldPos) +
- glm::vec2(p.position.x * emitterScale.x, p.position.y * emitterScale.y);
+ glm::vec2 rel = p.position * emitterScale;
if (std::abs(emitterAngle) > 0.001f) {
- f32 cosA = std::cos(emitterAngle);
- f32 sinA = std::sin(emitterAngle);
- glm::vec2 rel = p.position * emitterScale;
worldPos = glm::vec2(emitterWorldPos) +
glm::vec2(rel.x * cosA - rel.y * sinA,
rel.x * sinA + rel.y * cosA);
+ } else {
+ worldPos = glm::vec2(emitterWorldPos) + rel;
}
- entry.position = worldPos;
- entry.size = glm::vec2(p.size) * emitterScale;
+ size = glm::vec2(p.size) * emitterScale;
} else {
- entry.position = p.position;
- entry.size = glm::vec2(p.size);
+ worldPos = p.position;
+ size = glm::vec2(p.size);
}
- entry.rotation = p.rotation;
- entry.color = p.color;
+ u32 color = packColor(p.color);
+ f32 halfW = size.x * 0.5f;
+ f32 halfH = size.y * 0.5f;
+ f32 cosR = std::cos(p.rotation);
+ f32 sinR = std::sin(p.rotation);
+
+ glm::vec2 corners[4] = {
+ { -halfW, -halfH },
+ { halfW, -halfH },
+ { halfW, halfH },
+ { -halfW, halfH },
+ };
+
+ f32 u0, v0, u1, v1;
if (cols > 1 || rows > 1) {
i32 col = p.sprite_frame % cols;
i32 row = p.sprite_frame / cols;
- entry.uv_offset = glm::vec2(static_cast<f32>(col) * uvScaleX,
- static_cast<f32>(row) * uvScaleY);
- entry.uv_scale = glm::vec2(uvScaleX, uvScaleY);
+ u0 = static_cast<f32>(col) * uvScaleX;
+ v0 = static_cast<f32>(row) * uvScaleY;
+ u1 = u0 + uvScaleX;
+ v1 = v0 + uvScaleY;
+ } else {
+ u0 = 0.0f; v0 = 0.0f; u1 = 1.0f; v1 = 1.0f;
}
- entries_.push_back(entry);
- particleCount++;
- });
-
- batch.entry_count = particleCount;
- if (particleCount == 0) continue;
+ u32 vi = pi * 4;
+ for (u32 c = 0; c < 4; ++c) {
+ f32 rx = corners[c].x * cosR - corners[c].y * sinR;
+ f32 ry = corners[c].x * sinR + corners[c].y * cosR;
+ verts[vi + c].position = worldPos + glm::vec2(rx, ry);
+ verts[vi + c].color = color;
+ }
+ verts[vi + 0].texCoord = { u0, v1 };
+ verts[vi + 1].texCoord = { u1, v1 };
+ verts[vi + 2].texCoord = { u1, v0 };
+ verts[vi + 3].texCoord = { u0, v0 };
+
+ u32 ii = pi * 6;
+ u16 bv = baseVertex + static_cast<u16>(vi);
+ for (u32 q = 0; q < 6; ++q) {
+ buffers.writeIndices(idxOffset + ii + q, &QUAD_INDICES[q], 1);
+ }
+ // Patch base vertex into indices
+ u16 patched[6];
+ for (u32 q = 0; q < 6; ++q) {
+ patched[q] = bv + QUAD_INDICES[q];
+ }
+ buffers.writeIndices(idxOffset + ii, patched, 6);
- u32 batchIndex = static_cast<u32>(batches_.size());
- batches_.push_back(batch);
+ ++pi;
+ });
DrawCommand cmd{};
- cmd.sort_key = DrawCommand::buildSortKey(ctx.current_stage, emitter.layer, batch_shader_id_, batch.blend_mode, 0, textureId, emitterWorldPos.z);
- cmd.index_offset = batchIndex;
- cmd.index_count = particleCount;
- cmd.vertex_byte_offset = 0;
+ cmd.sort_key = DrawCommand::buildSortKey(ctx.current_stage, emitter.layer, batch_shader_id_, blendMode, 0, textureId, emitterWorldPos.z);
+ cmd.index_offset = idxOffset;
+ cmd.index_count = idxCount;
+ cmd.vertex_byte_offset = vertByteOffset;
cmd.shader_id = batch_shader_id_;
- cmd.blend_mode = batch.blend_mode;
+ cmd.blend_mode = blendMode;
cmd.layout_id = LayoutId::Batch;
cmd.texture_count = 1;
cmd.texture_ids[0] = textureId;
cmd.entity = entity;
+ cmd.entity_count = 1;
cmd.type = RenderType::Particle;
cmd.layer = emitter.layer;
@@ -133,18 +174,4 @@ void ParticlePlugin::collect(
}
}
-void ParticlePlugin::customDraw(
- const DrawCommand& cmd,
- StateTracker& state,
- TransientBufferPool& buffers,
- RenderFrameContext& ctx
-) {
- u32 batchIndex = cmd.index_offset;
- if (batchIndex >= batches_.size()) return;
-
- (void)state;
- (void)buffers;
- (void)ctx;
-}
-
} // namespace esengine
diff --git a/src/esengine/renderer/plugins/ParticlePlugin.hpp b/src/esengine/renderer/plugins/ParticlePlugin.hpp
index d0f979749..051dd79ac 100644
--- a/src/esengine/renderer/plugins/ParticlePlugin.hpp
+++ b/src/esengine/renderer/plugins/ParticlePlugin.hpp
@@ -24,35 +24,9 @@ public:
RenderFrameContext& ctx
) override;
- bool needsCustomDraw() const override { return true; }
- void customDraw(const DrawCommand& cmd,
- StateTracker& state,
- TransientBufferPool& buffers,
- RenderFrameContext& ctx) override;
-
private:
- struct ParticleRenderEntry {
- glm::vec2 position;
- glm::vec2 size;
- f32 rotation;
- glm::vec4 color;
- glm::vec2 uv_offset{0.0f, 0.0f};
- glm::vec2 uv_scale{1.0f, 1.0f};
- };
-
- struct ParticleBatchData {
- u32 texture_id = 0;
- BlendMode blend_mode = BlendMode::Normal;
- u32 material_id = 0;
- u32 first_entry = 0;
- u32 entry_count = 0;
- Entity entity = static_cast<Entity>(0);
- };
-
particle::ParticleSystem* particle_system_ = nullptr;
u32 batch_shader_id_ = 0;
- std::vector<ParticleRenderEntry> entries_;
- std::vector<ParticleBatchData> batches_;
};
} // namespace esengine
diff --git a/src/esengine/renderer/plugins/ShapePlugin.cpp b/src/esengine/renderer/plugins/ShapePlugin.cpp
index 06a83e250..7ef5591cd 100644
--- a/src/esengine/renderer/plugins/ShapePlugin.cpp
+++ b/src/esengine/renderer/plugins/ShapePlugin.cpp
@@ -113,6 +113,7 @@ void ShapePlugin::collect(
cmd.entity = entity;
cmd.type = RenderType::Shape;
cmd.layer = shape.layer;
+ cmd.state_flags |= CMD_STATE_CUSTOM_DRAW;
clips.applyTo(entity, cmd);
diff --git a/src/esengine/renderer/plugins/ShapePlugin.hpp b/src/esengine/renderer/plugins/ShapePlugin.hpp
index 42f93f08f..02d450bf7 100644
--- a/src/esengine/renderer/plugins/ShapePlugin.hpp
+++ b/src/esengine/renderer/plugins/ShapePlugin.hpp
@@ -21,6 +21,7 @@ public:
) override;
bool needsCustomDraw() const override { return true; }
+ bool handlesType(RenderType type) const override { return type == RenderType::Shape; }
void customDraw(const DrawCommand& cmd,
StateTracker& state,
TransientBufferPool& buffers,