载入中...
搜索中...
未找到
PointCompute.cpp
浏览该文件的文档.
2
3#include "common/Module.h"
4#include "common/config.h"
6#include "gpgpu/Gpgpu.h"
7#include "gpgpu/GpuBuffer.h"
8#include "gpgpu/Sequence.h"
9
10#include <algorithm>
11#include <cmath>
12#include <cstdlib>
13#include <exception>
14#include <limits>
15#include <memory>
16#include <string>
17#include <vector>
18
19namespace eve::procgen {
20namespace {
21
22constexpr int kPointFloats = 11;
23constexpr int kWorkgroupSize = 64;
24constexpr int kMaxTransforms = 4;
25constexpr int kRotationFloats = kMaxTransforms * 2;
26
27bool forcedFailure(const char* stage) {
28 const char* requested = std::getenv("EVENGINE_POINT_COMPUTE_FAIL");
29 return requested && std::string(requested) == stage;
30}
31
32const char* transformKernel() {
33#ifdef EVENGINE_WEBGPU
34 return R"(
35struct Data { values: array<f32> };
36struct Push { data: array<vec4f, 8> };
37@group(0) @binding(0) var<storage, read_write> points: Data;
38@group(0) @binding(8) var<uniform> push: Push;
39fn parameter(index: u32) -> f32 {
40 let value = push.data[index / 4u];
41 switch index % 4u {
42 case 0u: { return value.x; }
43 case 1u: { return value.y; }
44 case 2u: { return value.z; }
45 default: { return value.w; }
46 }
47}
48@compute @workgroup_size(64)
49fn main(@builtin(global_invocation_id) gid: vec3u) {
50 if (gid.x >= u32(parameter(0u))) { return; }
51 let base = gid.x * 11u;
52 for (var operation = 0u; operation < min(u32(parameter(1u)), 4u); operation++) {
53 let offset = 2u + operation * 7u;
54 let sx = parameter(offset + 3u);
55 let sy = parameter(offset + 4u);
56 let sz = parameter(offset + 5u);
57 let yaw = parameter(offset + 6u);
58 let rotation = u32(parameter(0u)) * 11u + operation * 2u;
59 let c = points.values[rotation];
60 let s = points.values[rotation + 1u];
61 let x = points.values[base] * sx;
62 let z = points.values[base + 2u] * sz;
63 points.values[base] = x * c - z * s + parameter(offset);
64 points.values[base + 1u] = points.values[base + 1u] * sy + parameter(offset + 1u);
65 points.values[base + 2u] = x * s + z * c + parameter(offset + 2u);
66 var nx = points.values[base + 3u] / select(1.0, sx, abs(sx) > 0.000001);
67 var ny = points.values[base + 4u] / select(1.0, sy, abs(sy) > 0.000001);
68 var nz = points.values[base + 5u] / select(1.0, sz, abs(sz) > 0.000001);
69 let rotated = vec3f(nx * c - nz * s, ny, nx * s + nz * c);
70 let normal = select(rotated, normalize(rotated), dot(rotated, rotated) > 0.0);
71 points.values[base + 3u] = normal.x;
72 points.values[base + 4u] = normal.y;
73 points.values[base + 5u] = normal.z;
74 points.values[base + 6u] += yaw;
75 points.values[base + 7u] *= sx;
76 points.values[base + 8u] *= sy;
77 points.values[base + 9u] *= sz;
78 }
79}
80)";
81#else
82 return R"(#version 450
83layout(local_size_x = 64) in;
84layout(set = 0, binding = 0) buffer Data { float values[]; } points;
85layout(push_constant) uniform Push { float data[32]; } push;
86void main() {
87 uint i = gl_GlobalInvocationID.x;
88 if (i >= uint(push.data[0])) return;
89 uint base = i * 11u;
90 for (int operation = 0; operation < min(int(push.data[1]), 4); ++operation) {
91 int offset = 2 + operation * 7;
92 float sx = push.data[offset + 3], sy = push.data[offset + 4], sz = push.data[offset + 5];
93 float yaw = push.data[offset + 6];
94 uint rotation = uint(push.data[0]) * 11u + uint(operation) * 2u;
95 float c = points.values[rotation], s = points.values[rotation + 1u];
96 float x = points.values[base] * sx;
97 float z = points.values[base + 2u] * sz;
98 points.values[base] = x * c - z * s + push.data[offset];
99 points.values[base + 1u] = points.values[base + 1u] * sy + push.data[offset + 1];
100 points.values[base + 2u] = x * s + z * c + push.data[offset + 2];
101 float nx = points.values[base + 3u] / (abs(sx) > 0.000001 ? sx : 1.0);
102 float ny = points.values[base + 4u] / (abs(sy) > 0.000001 ? sy : 1.0);
103 float nz = points.values[base + 5u] / (abs(sz) > 0.000001 ? sz : 1.0);
104 vec3 normal = vec3(nx * c - nz * s, ny, nx * s + nz * c);
105 float normalLength = length(normal);
106 if (normalLength > 0.0) normal /= normalLength;
107 points.values[base + 3u] = normal.x;
108 points.values[base + 4u] = normal.y;
109 points.values[base + 5u] = normal.z;
110 points.values[base + 6u] += yaw;
111 points.values[base + 7u] *= sx;
112 points.values[base + 8u] *= sy;
113 points.values[base + 9u] *= sz;
114 }
115}
116)";
117#endif
118}
119
120} // namespace
121
123 std::vector<float> packed;
124 std::unique_ptr<eve::gpgpu::GpuBuffer> storage;
125 std::unique_ptr<eve::gpgpu::GpuBuffer> staging;
126 std::unique_ptr<eve::gpgpu::ComputeShader> shader;
127 std::unique_ptr<eve::gpgpu::Sequence> sequence;
129 uint64_t uploadCount = 0;
130 uint64_t dispatchCount = 0;
131 uint64_t readbackCount = 0;
132 uint64_t bufferReuseCount = 0;
133 uint64_t peakBufferBytes = 0;
135
136 void reset() {
137 sequence.reset();
138 shader.reset();
139 staging.reset();
140 storage.reset();
141 capacityBytes = 0;
142 }
143};
144
145PointCompute::PointCompute() : impl_(std::make_unique<Impl>()) {}
147
148bool PointCompute::transform(const PointSet& input, PointSet& output, float translateX, float translateY,
149 float translateZ, float yawDegrees, float scaleX, float scaleY, float scaleZ) {
150 return transformChain(input, output, {{translateX, translateY, translateZ, yawDegrees, scaleX, scaleY, scaleZ}});
151}
152
153bool PointCompute::transformChain(const PointSet& input, PointSet& output, const std::vector<Transform>& transforms) {
154 error_.clear();
155 if (transforms.empty() || transforms.size() > kMaxTransforms) {
156 error_ = "transform chain must contain one to four operations";
157 return false;
158 }
159 if (input.empty()) {
160 output = input;
161 return true;
162 }
163 constexpr size_t bytesPerPoint = size_t(kPointFloats) * sizeof(float);
164 if (size_t(input.getCount()) >
165 (size_t(std::numeric_limits<int>::max()) - kRotationFloats * sizeof(float)) / bytesPerPoint) {
166 error_ = "point data exceeds GPU buffer address range";
167 return false;
168 }
169 try {
170 if (forcedFailure("compile")) {
171 error_ = "forced shader compilation failure";
172 return false;
173 }
174 auto* gpu = eve::ModuleManager::getInstance<eve::gpgpu::Gpgpu>("Gpgpu");
175 if (!gpu) gpu = eve::gpgpu::Gpgpu::create();
176 if (!gpu || !gpu->isAvailable()) {
177 error_ = "compute device unavailable";
178 return false;
179 }
180
181 const size_t rotationOffset = size_t(input.getCount()) * kPointFloats;
182 impl_->packed.resize(rotationOffset + kRotationFloats);
183 std::vector<float>& packed = impl_->packed;
184 for (int index = 0; index < input.getCount(); ++index) {
185 const ProcgenPoint& point = input.points()[size_t(index)];
186 float* values = packed.data() + size_t(index) * kPointFloats;
187 values[0] = point.x;
188 values[1] = point.y;
189 values[2] = point.z;
190 values[3] = point.normalX;
191 values[4] = point.normalY;
192 values[5] = point.normalZ;
193 values[6] = point.yaw;
194 values[7] = point.scaleX;
195 values[8] = point.scaleY;
196 values[9] = point.scaleZ;
197 values[10] = point.density;
198 }
199 // Match the CPU transform's rotation coefficients, independent of GPU
200 // transcendental precision, and calculate them once per operation.
201 for (size_t index = 0; index < transforms.size(); ++index) {
202 const float radians = transforms[index].yaw * 0.017453292519943295f;
203 packed[rotationOffset + index * 2] = std::cos(radians);
204 packed[rotationOffset + index * 2 + 1] = std::sin(radians);
205 }
206 const int byteSize = int(packed.size() * sizeof(float));
207 if (!impl_->shader) impl_->shader.reset(gpu->newShader(transformKernel()));
208 if (!impl_->sequence) impl_->sequence.reset(gpu->newSequence());
209 const bool reusedBuffers = byteSize <= impl_->capacityBytes;
210 if (byteSize > impl_->capacityBytes) {
211 if (forcedFailure("allocation")) {
212 error_ = "forced GPU allocation failure";
213 return false;
214 }
215 int capacity = std::max(byteSize, impl_->capacityBytes + impl_->capacityBytes / 2);
216 impl_->storage.reset(gpu->newBuffer(capacity, "storage"));
217 impl_->staging.reset(gpu->newBuffer(capacity, "staging"));
218 impl_->capacityBytes = capacity;
219 impl_->peakBufferBytes = std::max(impl_->peakBufferBytes, uint64_t(capacity));
220 }
221 if (!impl_->storage || !impl_->staging || !impl_->shader || !impl_->sequence ||
222 !impl_->sequence->isAvailable()) {
223 error_ = "compute resources unavailable";
224 return false;
225 }
226 impl_->shader->bindBuffer(0, impl_->storage.get());
227 impl_->shader->setFloat(0, float(input.getCount()));
228 impl_->shader->setFloat(1, float(transforms.size()));
229 for (size_t index = 0; index < transforms.size(); ++index) {
230 const Transform& transform = transforms[index];
231 const int offset = 2 + int(index) * 7;
232 impl_->shader->setFloat(offset, transform.x);
233 impl_->shader->setFloat(offset + 1, transform.y);
234 impl_->shader->setFloat(offset + 2, transform.z);
235 impl_->shader->setFloat(offset + 3, transform.scaleX);
236 impl_->shader->setFloat(offset + 4, transform.scaleY);
237 impl_->shader->setFloat(offset + 5, transform.scaleZ);
238 impl_->shader->setFloat(offset + 6, transform.yaw);
239 }
240 impl_->sequence->begin();
241 impl_->sequence->recordUpload(impl_->storage.get(), packed.data(), uint64_t(byteSize));
242 impl_->sequence->recordDispatch(impl_->shader.get(), (input.getCount() + kWorkgroupSize - 1) / kWorkgroupSize);
243 impl_->sequence->recordDownload(impl_->storage.get(), impl_->staging.get(), uint64_t(byteSize));
244 if (forcedFailure("submit")) {
245 error_ = "forced GPU submission failure";
246 impl_->reset();
247 return false;
248 }
249 impl_->sequence->submit();
250 if (forcedFailure("readback")) {
251 error_ = "forced GPU readback failure";
252 impl_->reset();
253 return false;
254 }
255 impl_->staging->downloadBytes(packed.data(), uint64_t(byteSize));
256 ++impl_->uploadCount;
257 ++impl_->dispatchCount;
258 ++impl_->readbackCount;
259 if (reusedBuffers) ++impl_->bufferReuseCount;
260 impl_->lastFusedTransformCount = int(transforms.size());
261
262 output = input;
263 for (int index = 0; index < output.getCount(); ++index) {
264 ProcgenPoint& point = output.mutablePoint(size_t(index));
265 const float* values = packed.data() + size_t(index) * kPointFloats;
266 point.x = values[0];
267 point.y = values[1];
268 point.z = values[2];
269 point.normalX = values[3];
270 point.normalY = values[4];
271 point.normalZ = values[5];
272 point.yaw = values[6];
273 point.scaleX = values[7];
274 point.scaleY = values[8];
275 point.scaleZ = values[9];
276 point.density = values[10];
277 }
278 return true;
279 } catch (const std::exception& exception) {
280 error_ = exception.what();
281 impl_->reset();
282 } catch (...) {
283 error_ = "unknown compute failure";
284 impl_->reset();
285 }
286 return false;
287}
288
289uint64_t PointCompute::getUploadCount() const { return impl_->uploadCount; }
290uint64_t PointCompute::getDispatchCount() const { return impl_->dispatchCount; }
291uint64_t PointCompute::getReadbackCount() const { return impl_->readbackCount; }
292uint64_t PointCompute::getBufferReuseCount() const { return impl_->bufferReuseCount; }
293uint64_t PointCompute::getPeakBufferBytes() const { return impl_->peakBufferBytes; }
294int PointCompute::getLastFusedTransformCount() const { return impl_->lastFusedTransformCount; }
295
296} // namespace eve::procgen
std::string output
EvpackChunkInput input
Definition Evpack.cpp:170
std::uint32_t capacity
size_t offset
std::shared_ptr< OceanGpuSpectrum > gpu
std::set< std::string > values
uint32_t index
glm::vec3 point
uint64_t getReadbackCount() const
Return successful GPU-to-host readback count for this executor.
bool transformChain(const PointSet &input, PointSet &output, const std::vector< Transform > &transforms)
Execute one to four transforms in one upload, dispatch and readback.
int getLastFusedTransformCount() const
Return logical transform count in the most recent successful dispatch.
uint64_t getBufferReuseCount() const
Return executions that reused existing GPU buffers.
uint64_t getDispatchCount() const
Return successful compute dispatch count for this executor.
uint64_t getPeakBufferBytes() const
Return peak bytes reserved in each reusable GPU point buffer.
bool transform(const PointSet &input, PointSet &output, float translateX, float translateY, float translateZ, float yawDegrees, float scaleX, float scaleY, float scaleZ)
Transform attributed points through the active Vulkan or WebGPU compute backend.
uint64_t getUploadCount() const
Return successful host-to-GPU upload count for this executor.
Script-friendly collection of attributed 3D samples.
Definition PointSet.h:59
std::unique_ptr< eve::gpgpu::Sequence > sequence
std::unique_ptr< eve::gpgpu::ComputeShader > shader
std::unique_ptr< eve::gpgpu::GpuBuffer > storage
std::unique_ptr< eve::gpgpu::GpuBuffer > staging
One transform operation in a fused point-compute dispatch.
One deterministic sample used by script-first procedural pipelines.
Definition PointSet.h:17