载入中...
搜索中...
未找到
EcsGpu.h
浏览该文件的文档.
1#pragma once
2
3// Bridge C++ ECS.hpp Views ↔ gpgpu::ShaderSystem storage buffers.
4// Component types must be trivially copyable and a multiple of sizeof(float)
5// (e.g. struct Position { float x, y; }).
6
8
9#include "ECS.hpp"
10
11#include <cstring>
12#include <type_traits>
13#include <vector>
14
15namespace eve::gpgpu {
16
17template <typename Comp>
18constexpr int componentFloatCount() {
19 static_assert(std::is_trivially_copyable_v<Comp>,
20 "EcsGpu component must be trivially copyable");
21 static_assert(sizeof(Comp) % sizeof(float) == 0,
22 "EcsGpu component size must be a multiple of float");
23 return int(sizeof(Comp) / sizeof(float));
24}
25
27template <typename Base, typename Comp>
29 int n = 0;
30 auto view = ecs::View<Base, Comp>();
31 for (auto it = view.begin(); it != view.end(); ++it)
32 ++n;
33 return n;
34}
35
40template <typename Base, typename Comp>
41int packViewComponent(ShaderSystem &sys, int binding) {
42 const int floatsPer = componentFloatCount<Comp>();
43 int n = 0;
44 {
45 auto view = ecs::View<Base, Comp>();
46 for (auto it = view.begin(); it != view.end(); ++it)
47 ++n;
48 }
49 if (n <= 0) return 0;
50
51 std::vector<float> tmp(size_t(n) * size_t(floatsPer));
52 int i = 0;
53 auto view = ecs::View<Base, Comp>();
54 for (auto it = view.begin(); it != view.end(); ++it) {
55 auto [comp] = *it;
56 std::memcpy(tmp.data() + size_t(i) * size_t(floatsPer), static_cast<const void *>(comp),
57 sizeof(Comp));
58 ++i;
59 }
60 sys.upload(binding, tmp.data(), n * floatsPer);
61 return n;
62}
63
68template <typename Base, typename Comp>
69void unpackViewComponent(ShaderSystem &sys, int binding, int count) {
70 if (count <= 0) return;
71 const int floatsPer = componentFloatCount<Comp>();
72 std::vector<float> tmp = sys.download(binding, count * floatsPer);
73 int i = 0;
74 auto view = ecs::View<Base, Comp>();
75 for (auto it = view.begin(); it != view.end() && i < count; ++it) {
76 auto [comp] = *it;
77 std::memcpy(static_cast<void *>(comp), tmp.data() + size_t(i) * size_t(floatsPer),
78 sizeof(Comp));
79 ++i;
80 }
81}
82
88template <typename Base, typename Comp>
89int runViewComponentSystem(ShaderSystem &sys, int binding, float dt) {
90 const int n = packViewComponent<Base, Comp>(sys, binding);
91 if (n <= 0) return 0;
92 sys.dispatch(n, dt);
93 unpackViewComponent<Base, Comp>(sys, binding, n);
94 return n;
95}
96
97} // namespace eve::gpgpu
glm::vec3 n
Definition Grass.cpp:64
glm::mat4 view
void upload(int binding, const float *data, int floatCount)
void dispatch(int entityCount, float dt=0.f)
void download(int binding, float *out, int floatCount) const
int countViewEntities()
Count live entities matching View<Base, Comp>.
Definition EcsGpu.h:28
int packViewComponent(ShaderSystem &sys, int binding)
Pack View<Base, Comp> into ShaderSystem binding as AoS floats. Returns entity count (0 if empty).
Definition EcsGpu.h:41
void unpackViewComponent(ShaderSystem &sys, int binding, int count)
Write ShaderSystem binding floats back into View<Base, Comp> (same order as pack)....
Definition EcsGpu.h:69
constexpr int componentFloatCount()
Definition EcsGpu.h:18
int runViewComponentSystem(ShaderSystem &sys, int binding, float dt)
Pack + dispatch + unpack a single-component GPU system. Additional bindings must be uploaded by the c...
Definition EcsGpu.h:89