载入中...
搜索中...
未找到
EcsScriptPack.cpp
浏览该文件的文档.
2#include "gpgpu/GpuBuffer.h"
3
4#include "common/ECS.h"
5#include "common/Exception.h"
6#include "common/Value.h"
7
8#include <simplesquirrel/simplesquirrel.hpp>
9#include <squirrel.h>
10
11#include <algorithm>
12#include <limits>
13#include <string>
14#include <vector>
15
16namespace eve::gpgpu {
17namespace {
18
19bool readStringField(HSQUIRRELVM vm, SQInteger idx, std::string &out) {
20 if (sq_gettype(vm, idx) != OT_STRING) return false;
21 const SQChar *s = nullptr;
22 if (SQ_FAILED(sq_getstring(vm, idx, &s)) || !s) return false;
23 out = s;
24 return true;
25}
26
27bool getInstanceSlot(HSQUIRRELVM vm, HSQOBJECT instance, const std::string &name,
28 HSQOBJECT &out) {
29 const SQInteger top = sq_gettop(vm);
30 sq_pushobject(vm, instance);
31 sq_pushstring(vm, name.c_str(), -1);
32 if (SQ_FAILED(sq_get(vm, -2))) {
33 sq_settop(vm, top);
34 return false;
35 }
36 sq_getstackobj(vm, -1, &out);
37 sq_addref(vm, &out);
38 sq_settop(vm, top);
39 return true;
40}
41
54bool getNumberField(const ssq::Object& comp, const std::string& name, float& out) {
55 Result<Value> field = readScriptComponentField(comp, name);
56 if (!field.ok() || !field.value().isNumeric()) return false;
57 out = float(field.value().asDouble());
58 return true;
59}
60
62bool setNumberField(const ssq::Object& comp, const std::string& name, float value) {
63 return writeScriptComponentField(comp, name, Value::number(double(value))).ok();
64}
65
66std::vector<std::string> collectFieldNames(ssq::Object fieldsObj) {
67 std::vector<std::string> names;
68 HSQUIRRELVM vm = fieldsObj.getHandle();
69 if (!vm) return names;
70 HSQOBJECT arr = fieldsObj.getRaw();
71 if (arr._type != OT_ARRAY) return names;
72
73 const SQInteger top = sq_gettop(vm);
74 sq_pushobject(vm, arr);
75 const SQInteger len = sq_getsize(vm, -1);
76 names.reserve(size_t(std::max<SQInteger>(0, len)));
77 for (SQInteger i = 0; i < len; ++i) {
78 sq_pushinteger(vm, i);
79 if (SQ_FAILED(sq_get(vm, -2))) continue;
80 std::string name;
81 if (readStringField(vm, -1, name)) names.push_back(name);
82 sq_pop(vm, 1);
83 }
84 sq_settop(vm, top);
85 return names;
86}
87
88int entityArrayLen(HSQUIRRELVM vm, HSQOBJECT arr) {
89 const SQInteger top = sq_gettop(vm);
90 sq_pushobject(vm, arr);
91 const SQInteger len = sq_getsize(vm, -1);
92 sq_settop(vm, top);
93 return int(std::max<SQInteger>(0, len));
94}
95
96} // namespace
97
98int packScriptEntityFloats(ssq::Object entitiesObj, const std::string &slot,
99 ssq::Object fieldsObj, GpuBuffer *buf) {
100 return packScriptEntityFloatsRange(entitiesObj, slot, fieldsObj, buf, 0, std::numeric_limits<int>::max());
101}
102
103int packScriptEntityFloatsRange(ssq::Object entitiesObj, const std::string &slot, ssq::Object fieldsObj, GpuBuffer *buf,
104 int firstEntity, int entityCount) {
105 if (!buf) throw Exception("packScriptEntityFloats: buffer is null");
106 if (slot.empty()) throw Exception("packScriptEntityFloats: empty slot");
107
108 HSQUIRRELVM vm = entitiesObj.getHandle();
109 if (!vm) return 0;
110 HSQOBJECT ents = entitiesObj.getRaw();
111 if (ents._type != OT_ARRAY) return 0;
112
113 const std::vector<std::string> fields = collectFieldNames(fieldsObj);
114 if (fields.empty()) return 0;
115
116 const int total = entityArrayLen(vm, ents);
117 const int first = std::clamp(firstEntity, 0, total);
118 const int n = std::clamp(entityCount, 0, total - first);
119 if (n <= 0) return 0;
120
121 const int floatsPer = int(fields.size());
122 std::vector<float> tmp(size_t(n) * size_t(floatsPer), 0.f);
123
124 const SQInteger top = sq_gettop(vm);
125 sq_pushobject(vm, ents);
126 for (int i = 0; i < n; ++i) {
127 sq_pushinteger(vm, first + i);
128 if (SQ_FAILED(sq_get(vm, -2))) {
129 sq_settop(vm, top);
130 throw Exception("packScriptEntityFloats: bad entity index %d", first + i);
131 }
132 if (sq_gettype(vm, -1) != OT_INSTANCE) {
133 sq_settop(vm, top);
134 throw Exception("packScriptEntityFloats: entity[%d] is not an instance", first + i);
135 }
136 HSQOBJECT entity{};
137 sq_getstackobj(vm, -1, &entity);
138
139 HSQOBJECT comp{};
140 sq_resetobject(&comp);
141 if (!getInstanceSlot(vm, entity, slot, comp) || comp._type != OT_INSTANCE) {
142 if (comp._type != OT_NULL) sq_release(vm, &comp);
143 sq_settop(vm, top);
144 throw Exception("packScriptEntityFloats: missing component slot '%s'", slot.c_str());
145 }
146 // `getInstanceSlot` 已经 addref 过一次;把这一份引用交给 `ssq::Object`(它的析构一定会
147 // `sq_release`),下面就不再手工释放——否则就是双重释放(见 ECS.cpp §9.64.3 的坑)。
148 ssq::Object compObj(vm);
149 compObj.getRaw() = comp;
150
151 for (int f = 0; f < floatsPer; ++f) {
152 float v = 0.f;
153 if (!getNumberField(compObj, fields[size_t(f)], v)) {
154 sq_settop(vm, top);
155 throw Exception("packScriptEntityFloats: missing field '%s.%s'", slot.c_str(),
156 fields[size_t(f)].c_str());
157 }
158 tmp[size_t(i) * size_t(floatsPer) + size_t(f)] = v;
159 }
160 sq_pop(vm, 1); // entity
161 }
162 sq_settop(vm, top);
163
164 const int needBytes = (first + n) * floatsPer * int(sizeof(float));
165 if (buf->getSize() < needBytes)
166 throw Exception("packScriptEntityFloats: buffer too small (%d < %d)", buf->getSize(),
167 needBytes);
168 buf->writeFloat32s(tmp.data(), n * floatsPer, first * floatsPer);
169 return n;
170}
171
172int unpackScriptEntityFloats(ssq::Object entitiesObj, const std::string &slot,
173 ssq::Object fieldsObj, GpuBuffer *buf, int entityCount) {
174 return unpackScriptEntityFloatsRange(entitiesObj, slot, fieldsObj, buf, 0, entityCount);
175}
176
177int unpackScriptEntityFloatsRange(ssq::Object entitiesObj, const std::string &slot, ssq::Object fieldsObj,
178 GpuBuffer *buf, int firstEntity, int entityCount) {
179 if (!buf) throw Exception("unpackScriptEntityFloats: buffer is null");
180 if (slot.empty()) throw Exception("unpackScriptEntityFloats: empty slot");
181 if (entityCount <= 0) return 0;
182
183 HSQUIRRELVM vm = entitiesObj.getHandle();
184 if (!vm) return 0;
185 HSQOBJECT ents = entitiesObj.getRaw();
186 if (ents._type != OT_ARRAY) return 0;
187
188 const std::vector<std::string> fields = collectFieldNames(fieldsObj);
189 if (fields.empty()) return 0;
190
191 const int nArr = entityArrayLen(vm, ents);
192 const int first = std::clamp(firstEntity, 0, nArr);
193 const int n = std::clamp(entityCount, 0, nArr - first);
194 if (n <= 0) return 0;
195
196 const int floatsPer = int(fields.size());
197 std::vector<float> tmp(size_t(n) * size_t(floatsPer), 0.f);
198 const int needBytes = (first + n) * floatsPer * int(sizeof(float));
199 if (buf->getSize() < needBytes)
200 throw Exception("unpackScriptEntityFloats: buffer too small (%d < %d)", buf->getSize(), needBytes);
201 buf->readFloat32s(tmp.data(), n * floatsPer, first * floatsPer);
202
203 const SQInteger top = sq_gettop(vm);
204 sq_pushobject(vm, ents);
205 for (int i = 0; i < n; ++i) {
206 sq_pushinteger(vm, first + i);
207 if (SQ_FAILED(sq_get(vm, -2))) {
208 sq_settop(vm, top);
209 throw Exception("unpackScriptEntityFloats: bad entity index %d", first + i);
210 }
211 HSQOBJECT entity{};
212 sq_getstackobj(vm, -1, &entity);
213
214 HSQOBJECT comp{};
215 sq_resetobject(&comp);
216 if (!getInstanceSlot(vm, entity, slot, comp) || comp._type != OT_INSTANCE) {
217 if (comp._type != OT_NULL) sq_release(vm, &comp);
218 sq_settop(vm, top);
219 throw Exception("unpackScriptEntityFloats: missing component slot '%s'", slot.c_str());
220 }
221 // 归属同 pack:addref 交给 `ssq::Object` 的析构释放,不再手工 `sq_release`。
222 ssq::Object compObj(vm);
223 compObj.getRaw() = comp;
224
225 for (int f = 0; f < floatsPer; ++f) {
226 const float v = tmp[size_t(i) * size_t(floatsPer) + size_t(f)];
227 if (!setNumberField(compObj, fields[size_t(f)], v)) {
228 sq_settop(vm, top);
229 throw Exception("unpackScriptEntityFloats: cannot set '%s.%s'", slot.c_str(),
230 fields[size_t(f)].c_str());
231 }
232 }
233 sq_pop(vm, 1);
234 }
235 sq_settop(vm, top);
236 return n;
237}
238
239} // namespace eve::gpgpu
const std::string & s
struct SQVM * HSQUIRRELVM
HSQUIRRELVM vm
Definition ECS.cpp:38
std::string name
Definition ECS.cpp:154
std::vector< std::string > fields
Definition ECS.cpp:42
glm::vec3 n
Definition Grass.cpp:63
float v
std::int32_t first
int idx
float f
Object::Ptr value
const EntityFact * entity
std::string slot
ScriptStructure out
Owning, renderer-independent dynamic values.
EVENGINE_API_FOUNDATION public API.
Definition Exception.h:13
bool ok() const noexcept
Whether this result represents a non-failure outcome.
Definition Result.h:514
static Value number(double value)
Compatibility factory for a Double value.
Definition Value.h:117
Backend-agnostic GPU buffer for compute (storage) or CPU staging transfers. Squirrel-owned; derived c...
Definition GpuBuffer.h:20
virtual void writeFloat32s(const float *data, int count, int startIndex=0)=0
Bulk float upload/download (one transfer). startIndex is in floats.
virtual int getSize() const =0
Byte length of the owned buffer.
virtual void readFloat32s(float *out, int count, int startIndex=0) const =0
Reads float 32 s.
double asDouble(double fallback=0.0) const
As double.
Definition Json.cpp:378
SQInteger top
Definition Snapshot.cpp:115
const Value * field(const Value &value, const char *key)
Borrow an optional object field.
Fluids module — interactive surface fluid simulation.
Definition Fluids.h:35
int unpackScriptEntityFloatsRange(ssq::Object entitiesObj, const std::string &slot, ssq::Object fieldsObj, GpuBuffer *buf, int firstEntity, int entityCount)
Unpack a contiguous buffer range into the matching stable ECS view range.
int packScriptEntityFloats(ssq::Object entitiesObj, const std::string &slot, ssq::Object fieldsObj, GpuBuffer *buf)
Pack script ECS entities' component number fields into a GpuBuffer (AoS).
int unpackScriptEntityFloats(ssq::Object entitiesObj, const std::string &slot, ssq::Object fieldsObj, GpuBuffer *buf, int entityCount)
Inverse of packScriptEntityFloats: write buffer rows back into component fields.
int packScriptEntityFloatsRange(ssq::Object entitiesObj, const std::string &slot, ssq::Object fieldsObj, GpuBuffer *buf, int firstEntity, int entityCount)
Pack a contiguous entity range into the matching range of an existing buffer.
EVENGINE_API_FOUNDATION Result< Value > readScriptComponentField(const ssq::Object &instance, const std::string &field)
读一个脚本组件实例的字段,**不关心这个字段存在哪**。
EVENGINE_API_FOUNDATION Result< void > writeScriptComponentField(const ssq::Object &instance, const std::string &field, const Value &value)
写一个脚本组件实例的字段,归属规则见 readScriptComponentField。