Estella 0.113.0
A fast 2D and 3D game engine powered by WebAssembly and ECS
Loading...
Searching...
No Matches
GpuParticles.hpp
Go to the documentation of this file.
1// SPDX-License-Identifier: Apache-2.0
2// SPDX-FileCopyrightText: Copyright (c) 2024-present ESEngine Team
24#pragma once
25
26#include "../../core/Types.hpp"
27
29#include "../rhi/GfxEnums.hpp"
30#include "../../math/Math.hpp"
31
32#include <unordered_map>
33#include <vector>
34
35namespace esengine {
36
37class GfxDevice;
38
39namespace ecs { struct ParticleEmitter; struct Transform; }
40namespace particle { class ParticleSystem; struct GpuStep; }
41
43public:
44 explicit GpuParticles(GfxDevice& device);
46
47 GpuParticles(const GpuParticles&) = delete;
49
51 bool available() const;
52
54 void beginFrame();
55
65 bool submit(Entity entity, const ecs::ParticleEmitter& emitter, const ecs::Transform& transform,
66 particle::ParticleSystem& system, const particle::GpuStep& step, const glm::vec4& eye,
67 BufferHandle& instances, BufferHandle& segments, u32& segmentCount);
68
70 bool wantsDepth() const { return wantsDepth_; }
71
73 void declareDepthCopy(rg::RenderGraph& graph, rg::ResourceId scene, u32 sceneW, u32 sceneH, const glm::mat4& clip);
74
77
78 void release();
79
81 u32 slotCount() const { return static_cast<u32>(slots_.size()); }
82 u32 lastDispatches() const { return lastDispatches_; }
84 u32 drawnSlots() const { return drawn_; }
85
86 void setDepthGridMax(u32 texels);
87 u32 depthGridWidth() const { return depthW_; }
88
89private:
90 struct Slot {
96 u32 capacity = 0;
97 u32 eventBytes = 0;
98 u32 trailPoints = 0;
99 u64 lastUsed = 0;
101 u32 readbackBytes = 0;
102 u32 collisionMode = 0;
103 particle::ParticleSystem* system = nullptr;
104 };
105 struct Job;
106
107 bool ensureKernel();
108 bool ensureEventKernels();
109 void ensureEmpties();
110 void pollReadbacks();
111 Slot* ensureSlot(Entity entity, u32 capacity);
112 void freeSlot(Slot& slot);
113 void run();
114
115 GfxDevice& device_;
121 u32 paramsCapacity_ = 0;
123 u32 controlCapacity_ = 0;
125 BufferHandle emptyEvents_ = BufferHandle::Invalid;
127 BufferHandle emptySegments_ = BufferHandle::Invalid;
129 u32 collidersCapacity_ = 0;
131 BufferHandle depthParams_ = BufferHandle::Invalid;
132 u32 depthW_ = 0, depthH_ = 0;
133 u32 depthGridMax_ = 640;
134 bool depthValid_ = false;
135 bool depthCopied_ = false;
136 bool wantsDepth_ = false;
137 bool wantsDepthNext_ = false;
138 glm::mat4 depthClip_{1.0f};
139 glm::mat4 depthWorld_{1.0f};
140 glm::vec3 depthOrigin_{0.0f};
141 bool failed_ = false;
142 u64 frame_ = 0;
143 u32 lastDispatches_ = 0;
144 u32 drawn_ = 0;
145 std::unordered_map<u32, Slot> slots_;
146 std::vector<Job> jobs_;
147 std::vector<Job*> order_;
148 std::vector<u8> placed_;
149 std::vector<i32> parent_;
150 std::vector<u8> staging_;
151 std::vector<u8> control_staging_;
152};
153
154} // namespace esengine
Backend-agnostic graphics enumerations.
Math utilities and GLM configuration for ESEngine.
Full-screen passes as a declared graph: what each one reads, what it writes, and at what size — the g...
Core type definitions and utilities for ESEngine.
Abstract graphics device interface.
Definition GfxDevice.hpp:75
void beginFrame()
A new frame: the previous frame's jobs are gone, and a slot nobody asked for in a while is given back...
GpuParticles(const GpuParticles &)=delete
GpuParticles & operator=(const GpuParticles &)=delete
bool submit(Entity entity, const ecs::ParticleEmitter &emitter, const ecs::Transform &transform, particle::ParticleSystem &system, const particle::GpuStep &step, const glm::vec4 &eye, BufferHandle &instances, BufferHandle &segments, u32 &segmentCount)
Queues the step an emitter needs and says where its particles are. The step is committed to the emitt...
bool wantsDepth() const
An emitter collided with the depth last frame, so the scene must draw into a target whose depth can b...
void setDepthGridMax(u32 texels)
rg::ResourceId declare(rg::RenderGraph &graph)
Adds the compute pass for what was submitted. The resource it writes must be named by the pass that d...
void declareDepthCopy(rg::RenderGraph &graph, rg::ResourceId scene, u32 sceneW, u32 sceneH, const glm::mat4 &clip)
Copies the scene's depth for next frame's kernel, once a frame. clip is the view that drew it,...
u32 slotCount() const
Slots live right now, for the stats.
GpuParticles(GfxDevice &device)
bool available() const
Whether the device can run the kernel at all.
u32 drawnSlots() const
Particle slots handed to a draw this frame, over every emitter and camera: what the GPU is asked to d...
std::uint64_t u64
Unsigned 64-bit integer.
Definition Types.hpp:47
ComputeHandle
A compute program (GfxDevice::createCompute); only a device that supportsCompute() issues one.
Definition GfxEnums.hpp:39
std::uint32_t u32
Unsigned 32-bit integer.
Definition Types.hpp:45
ReadbackHandle
An in-flight framebuffer readback (GfxDevice::requestReadback).
Definition GfxEnums.hpp:43
Entity identifier with packed index + generation.
Definition Types.hpp:135
Unified transform component.
Definition Transform.hpp:45
What one frame asks of a GPU-simulated emitter: how long to advance, how many to spawn and where in t...