Toast Engine
Loading...
Searching...
No Matches
debris_simulation.hpp
Go to the documentation of this file.
1
7
8#pragma once
9
10#include "debris.hpp"
11
12#include <array>
13#include <cstdint>
14#include <deque>
15#include <glm/glm.hpp>
16#include <memory>
17#include <mutex>
18#include <span>
20
21namespace renderer {
23}
24
25namespace physics {
26
27class TOAST_API DebrisSimulation : public renderer::AsyncComputeTask {
28public:
29 struct GpuParticle {
30 glm::vec3 position {0.0f};
31 uint32_t packed = 0;
32 glm::vec4 rotation {0.0f, 0.0f, 0.0f, 1.0f};
33 glm::vec3 velocity {0.0f};
34 uint32_t albedo_roughness = 0;
35 };
36
37 struct GpuMotion {
38 glm::vec3 angular_velocity {0.0f};
39 float age = 0.0f;
40 uint32_t rest_steps = 0;
41 std::array<uint32_t, 3> pad {};
42 };
43
44 struct GpuSpawn {
45 glm::vec3 position {0.0f};
46 uint32_t slot = 0;
47 glm::vec4 rotation {0.0f, 0.0f, 0.0f, 1.0f};
48 glm::vec3 velocity {0.0f};
49 uint32_t albedo_roughness = 0;
50 glm::vec3 angular_velocity {0.0f};
51 uint32_t packed = 0;
52 };
53
54 struct GpuVolume {
55 glm::mat4 world_to_voxel {1.0f};
56 glm::vec4 bounds {0.0f};
57 uint32_t record_index = 0;
58 std::array<uint32_t, 3> pad {};
59 };
60
61 // just in case somebody touches this
62 static_assert(sizeof(GpuParticle) == 48, "GpuParticle is mirrored by debris.slang");
63 static_assert(sizeof(GpuMotion) == 32, "GpuMotion is mirrored by debris.slang");
64 static_assert(sizeof(GpuSpawn) == 64, "GpuSpawn is mirrored by debris.slang");
65 static_assert(sizeof(GpuVolume) == 96, "GpuVolume is mirrored by debris.slang");
66
67 static constexpr uint32_t k_max_spawns_per_step = 4096;
68 static constexpr uint32_t k_max_volumes = 512;
69 static constexpr uint32_t k_voxels_per_particle = 8;
70
71 explicit DebrisSimulation(uint32_t capacity);
72 ~DebrisSimulation() override;
73
74 DebrisSimulation(const DebrisSimulation&) = delete;
75 auto operator=(const DebrisSimulation&) -> DebrisSimulation& = delete;
76 DebrisSimulation(DebrisSimulation&&) = delete;
77 auto operator=(DebrisSimulation&&) -> DebrisSimulation& = delete;
78
79 [[nodiscard]]
80 static auto get() -> DebrisSimulation*;
81 void spawn(std::span<const DebrisSpawn> spawns);
82 void step(uint32_t steps, double fixed_delta, std::shared_ptr<const renderer::VoxelCollisionWorld> world);
83 void setDisplayAlpha(float alpha, double fixed_delta);
84 void clear();
85
86 [[nodiscard]]
87 auto capacity() const -> uint32_t {
88 return m_capacity;
89 }
90
91 [[nodiscard]]
92 auto visible() const -> bool;
93
94 [[nodiscard]]
95 auto extrapolation(uint64_t step) const -> float; // render thread
96
97 [[nodiscard]]
98 auto frameDelta() const -> float; // render thread
99
100 [[nodiscard]]
101 auto particlesId() const -> BufferId {
102 return m_particles;
103 }
104
105 [[nodiscard]]
106 auto drawListId() const -> BufferId {
107 return m_draw_list;
108 }
109
110 [[nodiscard]]
111 auto drawArgsId() const -> BufferId {
112 return m_draw_args;
113 }
114
115private:
116 struct Params {
117 glm::vec3 gravity {0.0f};
118 float dt = 0.0f;
119 float lifetime = 0.0f;
120 float restitution = 0.0f;
121 float friction = 0.0f;
122 float voxel_size = 0.0f;
123 uint32_t capacity = 0;
124 uint32_t spawn_count = 0;
125 uint32_t volume_count = 0;
126 uint32_t substeps = 1;
127 uint32_t clear = 0;
128 std::array<uint32_t, 3> pad {};
129 };
130
131 static_assert(sizeof(Params) == 64, "Params is mirrored by debris.slang");
132
133 static constexpr uint64_t k_step_history = 16;
134
135 uint32_t m_capacity = 0;
136 uint32_t m_cursor = 0;
137
138 BufferId m_particles = 0;
139 BufferId m_motion = 0;
140 BufferId m_draw_list = 0;
141 BufferId m_draw_args = 0;
142 BufferId m_spawns = 0;
143 BufferId m_volumes = 0;
144
145 std::deque<GpuSpawn> m_pending;
146 bool m_clear_pending = false;
147
148 std::shared_ptr<const renderer::VoxelCollisionWorld> m_world;
149
150 mutable std::mutex m_time_mutex;
151 double m_sim_time = 0.0;
152 double m_display_time = 0.0;
153 double m_frame_delta = 0.0;
154 std::array<double, k_step_history> m_step_times {};
155 bool m_visible = false;
156};
157
158}
Definition async_compute_task.hpp:33
Small voxel debris clusters.
Definition debris_simulation.hpp:37
Definition debris_simulation.hpp:29
Definition debris_simulation.hpp:44
Definition debris_simulation.hpp:54
Definition voxel_gpu_storage.hpp:148