载入中...
搜索中...
未找到
GraphicsCapabilities.cpp
浏览该文件的文档.
1// IRenderCapture capability provider.
2//
3// DevTools / MCP must not depend on the graphics module, so the graphics
4// module registers this implementation of the common render-capture
5// interface. The scene traversal for the entity-id mask and visible-entity
6// snapshot lives here too (graphics -> scene is a legal downward edge).
7
9
10#include "common/Capability.h"
13
14#include "common/ECS.h"
15#include "common/b64.h"
16#include "filesystem/FileData.h"
17#include "graphics/ClipSpace.h"
19#include "graphics/Graphics.h"
21#include "graphics/Mesh.h"
25#include "image/ImageData.h"
26#include "scene/SceneHost.h"
28
29#include <glm/gtc/matrix_transform.hpp>
30
31#include <algorithm>
32#include <cmath>
33#include <cstdio>
34#include <filesystem>
35#include <fstream>
36#include <string>
37#include <memory>
38#include <unordered_map>
39#include <unordered_set>
40#include <vector>
41
42namespace eve::graphics {
43namespace {
44
45constexpr float kPi = 3.14159265358979323846f;
46
47float degToRad(float d) { return d * kPi / 180.f; }
48
49std::string jsonEscape(const std::string &s) {
50 std::string out;
51 out.reserve(s.size() + 8);
52 for (char c : s) {
53 switch (c) {
54 case '"': out += "\\\""; break;
55 case '\\': out += "\\\\"; break;
56 case '\n': out += "\\n"; break;
57 case '\r': out += "\\r"; break;
58 case '\t': out += "\\t"; break;
59 default: out += c;
60 }
61 }
62 return out;
63}
64
65std::string num(float v) {
66 char buf[64];
67 std::snprintf(buf, sizeof(buf), "%g", static_cast<double>(v));
68 return buf;
69}
70
71std::string vec3Json(const glm::vec3 &v) {
72 return "[" + num(v.x) + "," + num(v.y) + "," + num(v.z) + "]";
73}
74
75class RenderCaptureImpl final : public eve::IRenderCapture {
76public:
77 Graphics *gfx() { return eve::ModuleManager::getInstance<Graphics>("Graphics"); }
78
79 Camera3D *camera() {
80 if (ecs::current()->getManager<Camera3D>() != nullptr) {
81 auto view = ecs::View<Camera3D, Camera3D::Data>();
82 for (auto it = view.begin(); it != view.end(); ++it) {
83 auto [data] = *it;
84 if (data->active && data->entity) return data->entity;
85 }
86 }
88 }
89
90 eve::RenderStatusInfo status() const override {
92 if (auto *g = const_cast<RenderCaptureImpl *>(this)->gfx()) {
93 info.width = g->getWidth();
94 info.height = g->getHeight();
95 info.pixelWidth = g->getPixelWidth();
96 info.pixelHeight = g->getPixelHeight();
97 info.had3DThisFrame = g->had3DThisFrame();
98 info.readbackEnabled = g->isScreenReadbackEnabled();
99 info.backend = g->getBackendName();
100 }
101 return info;
102 }
103
104 void setReadbackEnabled(bool enabled) override {
105 if (auto *g = gfx()) g->setScreenReadbackEnabled(enabled);
106 }
107
108 eve::Result<eve::CaptureImageInfo> savePng(const std::string &path) override {
109 auto *g = gfx();
110 if (!g) {
112 eve::Diagnostic::error(eve::DiagnosticCode::Unsupported, "Graphics module not available", "capture"));
113 }
114 g->setScreenReadbackEnabled(true);
115 image::ImageData *frame = nullptr;
116 try {
117 frame = g->newImageData();
118 } catch (...) {
121 "no presented frame yet: readback was just enabled and the "
122 "copy lands on the next present — call again after one frame "
123 "(screenshots need a running, presented game loop)",
124 "capture"));
125 }
126 if (!frame) {
129 "no presented frame (enable readback and render a frame first)", "capture"));
130 }
131 filesystem::FileData *png = nullptr;
132 try {
133 png = frame->encode(image::ImageData::FormatHandler::ENCODED_PNG, "frame.png", false);
134 } catch (...) {
135 delete frame;
137 eve::Diagnostic::error(eve::DiagnosticCode::Failed, "png encode failed", "capture"));
138 }
139 if (!png) {
140 delete frame;
142 eve::Diagnostic::error(eve::DiagnosticCode::Failed, "png encode returned null", "capture"));
143 }
144 std::error_code ec;
145 std::filesystem::create_directories(std::filesystem::path(path).parent_path(), ec);
146 std::ofstream out(path, std::ios::binary);
147 if (!out.good()) {
148 delete png;
149 delete frame;
151 eve::Diagnostic::error(eve::DiagnosticCode::Failed, "cannot open output file: " + path, "capture"));
152 }
153 out.write(static_cast<const char *>(png->getData()),
154 static_cast<std::streamsize>(png->getSize()));
155 const bool ok = out.good();
156 delete png;
157 delete frame;
158 if (!ok) {
160 eve::Diagnostic::error(eve::DiagnosticCode::Failed, "failed writing: " + path, "capture"));
161 }
163 info.width = g->getPixelWidth();
164 info.height = g->getPixelHeight();
166 }
167
168 std::string capturePngDataUrl() override {
169 auto *g = gfx();
170 if (!g) return {};
171 g->setScreenReadbackEnabled(true);
172 image::ImageData *frame = nullptr;
173 try {
174 frame = g->newImageData();
175 } catch (...) {
176 return {};
177 }
178 if (!frame) return {};
179 filesystem::FileData *png = nullptr;
180 try {
181 png = frame->encode(image::ImageData::FormatHandler::ENCODED_PNG, "frame.png", false);
182 } catch (...) {
183 delete frame;
184 return {};
185 }
186 if (!png) {
187 delete frame;
188 return {};
189 }
190 size_t len = 0;
191 char *b64 = eve::b64_encode(static_cast<const char *>(png->getData()), png->getSize(), 0, len);
192 std::string url = "data:image/png;base64," + std::string(b64 ? b64 : "", len);
193 delete[] b64;
194 delete png;
195 delete frame;
196 return url;
197 }
198
199 bool ensureCamera() override { return camera() != nullptr; }
200
201 bool setCameraPose(float ex, float ey, float ez, float tx, float ty, float tz,
202 float fovYDeg) override {
203 auto *cam = camera();
204 if (!cam) return false;
205 cam->setActive(true);
206 cam->setEye(ex, ey, ez);
207 cam->setTarget(tx, ty, tz);
208 if (fovYDeg > 0.f) cam->setFov(fovYDeg);
209 return true;
210 }
211
212 bool setCameraPoseYawPitch(float ex, float ey, float ez, float yawDeg, float pitchDeg,
213 float fovYDeg) override {
214 auto *cam = camera();
215 if (!cam) return false;
216 cam->setActive(true);
217 const float yaw = degToRad(yawDeg);
218 const float pitch = degToRad(pitchDeg);
219 const glm::vec3 dir(std::cos(pitch) * std::sin(yaw), std::sin(pitch),
220 std::cos(pitch) * std::cos(yaw));
221 cam->setEye(ex, ey, ez);
222 cam->setTarget(ex + dir.x * 100.f, ey + dir.y * 100.f, ez + dir.z * 100.f);
223 if (fovYDeg > 0.f) cam->setFov(fovYDeg);
224 return true;
225 }
226
227 std::string cameraPoseJson() override {
228 auto *cam = camera();
229 if (!cam) return "{\"error\":\"no camera\"}";
230 auto d = cam->data();
231 std::string out = "{\"eye\":" + vec3Json(glm::vec3(d->eyeX, d->eyeY, d->eyeZ));
232 out += ",\"target\":" + vec3Json(glm::vec3(d->targetX, d->targetY, d->targetZ));
233 out += ",\"up\":" + vec3Json(glm::vec3(d->upX, d->upY, d->upZ));
234 out += ",\"fov\":" + num(d->fovYDeg);
235 out += ",\"near\":" + num(d->nearZ);
236 out += ",\"far\":" + num(d->farZ);
237 if (auto *g = gfx()) {
238 out += ",\"viewportWidth\":" + std::to_string(g->getPixelWidth());
239 out += ",\"viewportHeight\":" + std::to_string(g->getPixelHeight());
240 }
241 out += "}";
242 return out;
243 }
244
245 eve::Result<eve::Renderable3DInfo> inspectRenderable3D(
246 std::uint32_t entityId, std::uint32_t generation) override {
247 ecs::EntityHandle handle{ecs::current(), std::type_index(typeid(Renderable3D)),
248 entityId, generation};
249 auto* renderable = dynamic_cast<Renderable3D*>(ecs::try_get(handle));
250 if (!renderable)
253 "Renderable3D handle is missing or stale", "entity"));
254 auto transform = renderable->transform();
255 auto renderer = renderable->meshRenderer();
257 info.entityId = renderable->id;
258 info.generation = renderable->generation;
259 info.x = transform->x;
260 info.y = transform->y;
261 info.z = transform->z;
262 info.tintR = renderer->r;
263 info.tintG = renderer->g;
264 info.tintB = renderer->b;
265 info.tintA = renderer->a;
266 info.metallic = renderer->metallic;
267 info.roughness = renderer->roughness;
268 info.parallaxScale = renderer->parallaxScale;
269 info.visible = renderer->visible;
270 info.receiveLight = renderer->receiveLight;
271 info.castShadow = renderer->castShadow;
272 info.receiveShadow = renderer->receiveShadow;
273 info.hasPackedMaterial = renderer->material != nullptr;
274 info.hasTexture = renderer->texture != nullptr || renderer->normalTexture != nullptr ||
275 renderer->heightTexture != nullptr;
276 info.hasShader = renderer->shader != nullptr;
278 }
279
280 std::string visibleEntitiesJson(float fovYDeg, bool *ok) override {
281 return visibleAt(nullptr, nullptr, fovYDeg, ok);
282 }
283
284 std::string visibleEntitiesJsonAt(float ex, float ey, float ez, float tx, float ty, float tz,
285 float fovYDeg, bool *ok) override {
286 const glm::vec3 eye(ex, ey, ez);
287 const glm::vec3 target(tx, ty, tz);
288 return visibleAt(&eye, &target, fovYDeg, ok);
289 }
290
291 std::string entityIdMaskJson(int *outWidth, int *outHeight, bool *ok) override {
292 auto *g = gfx();
293 if (ok) *ok = false;
294 if (!g) return "{\"error\":\"Graphics module not available\"}";
295 auto *cam = camera();
296 if (!cam) return "{\"error\":\"no camera\"}";
297 auto d = cam->data();
298 const glm::vec3 eye(d->eyeX, d->eyeY, d->eyeZ);
299 const glm::vec3 target(d->targetX, d->targetY, d->targetZ);
300 const glm::vec3 up(d->upX, d->upY, d->upZ);
301 const int vw = g->getPixelWidth() > 0 ? g->getPixelWidth() : g->getWidth();
302 const int vh = g->getPixelHeight() > 0 ? g->getPixelHeight() : g->getHeight();
303 if (outWidth) *outWidth = vw;
304 if (outHeight) *outHeight = vh;
305 if (vw <= 0 || vh <= 0) return "{\"error\":\"empty viewport\"}";
306 const glm::mat4 viewM = glm::lookAtRH(eye, target, up);
307 const glm::mat4 projM = cameraProjectionVulkanRH_ZO(
308 d->orthographic, degToRad(d->fovYDeg), d->orthoHeight,
309 float(vw) / float(vh), d->nearZ, d->farZ);
310 const glm::mat4 viewProj = projM * viewM;
311
312 std::vector<EntityIdDraw> draws;
313 int id = 1;
314 std::string entities;
316 if (ecs::current()->getManager<scene::SceneHost>() != nullptr) {
317 auto hostView = ecs::View<scene::SceneHost, scene::SceneHost::Meta,
318 scene::SceneHost::Tree>();
319 for (auto it = hostView.begin(); it != hostView.end(); ++it) {
320 auto [meta, tree] = *it;
321 if (!meta || !meta->entity || !tree) continue;
322 auto *host = meta->entity;
323 host->walkDepthFirst([&](scene::SceneHost *, int, scene::SceneNode &node) {
324 if (!node.visible || node.space != "3d") return;
325 const auto *l = host->findLink(&node, scene::findLinkKind("renderable3d"));
326 if (!l || !l->target) return;
327 auto *r = static_cast<Renderable3D *>(l->target);
328 auto mr = r->meshRenderer();
329 Mesh *mesh = mr->mesh;
330 if (!mesh || !mesh->gpuHandle) return;
331 EntityIdDraw draw;
332 draw.mesh = mesh;
333 draw.model = node.world;
334 const uint32_t packed = uint32_t(id);
335 draw.idColor = glm::vec4((packed & 255u) / 255.f,
336 ((packed >> 8) & 255u) / 255.f,
337 ((packed >> 16) & 255u) / 255.f, 1.f);
338 draws.push_back(draw);
339 if (!entities.empty()) entities += ",";
340 entities += "{\"id\":" + std::to_string(id) +
341 ",\"asset\":\"" + jsonEscape(node.name.empty() ? node.id : node.name) +
342 "\",\"node\":\"" + jsonEscape(node.id.empty() ? node.name : node.id) + "\"}";
343 ++id;
344 });
345 }
346 }
347 auto* capture = g->visibilityCapture();
348 if (!capture) return {};
349 image::ImageData* img = capture->renderEntityIdMask(draws, viewProj, vw, vh);
350 if (!img) return "{\"error\":\"id mask unsupported\"}";
351 delete img;
352 if (ok) *ok = true;
353 return "{\"camera\":" + vec3Json(eye) + ",\"viewportWidth\":" + std::to_string(vw) +
354 ",\"viewportHeight\":" + std::to_string(vh) + ",\"entities\":[" + entities +
355 "],\"count\":" + std::to_string(id - 1) + "}";
356 }
357
358 eve::Result<void> entityIdMaskPng(const std::string &path) override {
359 auto *g = gfx();
360 if (!g) {
362 eve::Diagnostic::error(eve::DiagnosticCode::Unsupported, "Graphics module not available", "id_mask"));
363 }
364 auto *cam = camera();
365 if (!cam) {
368 }
369 auto d = cam->data();
370 const glm::vec3 eye(d->eyeX, d->eyeY, d->eyeZ);
371 const glm::vec3 target(d->targetX, d->targetY, d->targetZ);
372 const glm::vec3 up(d->upX, d->upY, d->upZ);
373 const int vw = g->getPixelWidth() > 0 ? g->getPixelWidth() : g->getWidth();
374 const int vh = g->getPixelHeight() > 0 ? g->getPixelHeight() : g->getHeight();
375 if (vw <= 0 || vh <= 0) {
378 }
379 const glm::mat4 viewM = glm::lookAtRH(eye, target, up);
380 const glm::mat4 projM = cameraProjectionVulkanRH_ZO(
381 d->orthographic, degToRad(d->fovYDeg), d->orthoHeight,
382 float(vw) / float(vh), d->nearZ, d->farZ);
383 const glm::mat4 viewProj = projM * viewM;
384 std::vector<EntityIdDraw> draws;
385 int id = 1;
387 if (ecs::current()->getManager<scene::SceneHost>() != nullptr) {
388 auto hostView = ecs::View<scene::SceneHost, scene::SceneHost::Meta,
389 scene::SceneHost::Tree>();
390 for (auto it = hostView.begin(); it != hostView.end(); ++it) {
391 auto [meta, tree] = *it;
392 if (!meta || !meta->entity || !tree) continue;
393 auto *host = meta->entity;
394 host->walkDepthFirst([&](scene::SceneHost *, int, scene::SceneNode &node) {
395 if (!node.visible || node.space != "3d") return;
396 const auto *l = host->findLink(&node, scene::findLinkKind("renderable3d"));
397 if (!l || !l->target) return;
398 auto *r = static_cast<Renderable3D *>(l->target);
399 auto mr = r->meshRenderer();
400 Mesh *mesh = mr->mesh;
401 if (!mesh || !mesh->gpuHandle) return;
402 EntityIdDraw draw;
403 draw.mesh = mesh;
404 draw.model = node.world;
405 const uint32_t packed = uint32_t(id);
406 draw.idColor = glm::vec4((packed & 255u) / 255.f,
407 ((packed >> 8) & 255u) / 255.f,
408 ((packed >> 16) & 255u) / 255.f, 1.f);
409 draws.push_back(draw);
410 ++id;
411 });
412 }
413 }
414 auto* capture = g->visibilityCapture();
415 if (!capture) {
417 eve::Diagnostic::error(eve::DiagnosticCode::Unsupported, "id mask unsupported", "id_mask"));
418 }
419 image::ImageData* img = capture->renderEntityIdMask(draws, viewProj, vw, vh);
420 if (!img) {
422 eve::Diagnostic::error(eve::DiagnosticCode::Unsupported, "id mask unsupported", "id_mask"));
423 }
424 filesystem::FileData *png = nullptr;
425 try {
426 png = img->encode(image::ImageData::FormatHandler::ENCODED_PNG, "id.png", false);
427 } catch (...) {
428 delete img;
430 eve::Diagnostic::error(eve::DiagnosticCode::Failed, "png encode failed", "id_mask"));
431 }
432 bool ok = false;
433 if (png) {
434 std::error_code ec;
435 std::filesystem::create_directories(std::filesystem::path(path).parent_path(), ec);
436 std::ofstream out(path, std::ios::binary);
437 if (out.good()) {
438 out.write(static_cast<const char *>(png->getData()),
439 static_cast<std::streamsize>(png->getSize()));
440 ok = out.good();
441 }
442 }
443 delete png;
444 delete img;
445 if (!ok) {
447 eve::Diagnostic::error(eve::DiagnosticCode::Failed, "failed writing id mask", "id_mask"));
448 }
450 }
451
452 eve::Result<void> gbufferPng(const std::string &name, const std::string &path) override {
453 auto *g = gfx();
454 if (!g) {
456 eve::Diagnostic::error(eve::DiagnosticCode::Unsupported, "Graphics module not available", "gbuffer"));
457 }
458 image::ImageData *img = g->readGBufferToImageData(name);
459 if (!img) {
461 eve::DiagnosticCode::Unsupported, "G-buffer attachment unavailable: " + name, "gbuffer"));
462 }
463 filesystem::FileData *png = nullptr;
464 try {
465 png = img->encode(image::ImageData::FormatHandler::ENCODED_PNG, "buf.png", false);
466 } catch (...) {
467 delete img;
469 eve::Diagnostic::error(eve::DiagnosticCode::Failed, "png encode failed", "gbuffer"));
470 }
471 if (!png) {
472 delete img;
474 eve::Diagnostic::error(eve::DiagnosticCode::Failed, "png encode returned null", "gbuffer"));
475 }
476 std::error_code ec;
477 std::filesystem::create_directories(std::filesystem::path(path).parent_path(), ec);
478 std::ofstream out(path, std::ios::binary);
479 bool ok = false;
480 if (out.good()) {
481 out.write(static_cast<const char *>(png->getData()), static_cast<std::streamsize>(png->getSize()));
482 ok = out.good();
483 }
484 delete png;
485 delete img;
486 if (!ok) {
488 eve::Diagnostic::error(eve::DiagnosticCode::Failed, "failed writing gbuffer png", "gbuffer"));
489 }
491 }
492
493private:
494 std::string visibleAt(const glm::vec3 *eye, const glm::vec3 *target, float fovYDeg,
495 bool *ok) {
496 auto *g = gfx();
497 if (ok) *ok = false;
498 auto *cam = camera();
499
500 glm::vec3 camEye(0.f, 1.8f, 3.f);
501 glm::vec3 camTarget(0.f, 1.2f, 0.f);
502 float camFov = 60.f;
503 float camNear = 0.1f, camFar = 100.f;
504 if (cam) {
505 auto d = cam->data();
506 camEye = glm::vec3(d->eyeX, d->eyeY, d->eyeZ);
507 camTarget = glm::vec3(d->targetX, d->targetY, d->targetZ);
508 camFov = d->fovYDeg;
509 camNear = d->nearZ;
510 camFar = d->farZ;
511 }
512 if (eye && target) {
513 camEye = *eye;
514 camTarget = *target;
515 }
516 if (fovYDeg > 0.f) camFov = fovYDeg;
517
518 const int vw = g ? g->getPixelWidth() : 1280;
519 const int vh = g ? g->getPixelHeight() : 720;
520 const glm::vec3 up(0.f, 1.f, 0.f);
521 const glm::mat4 view = glm::lookAtRH(camEye, camTarget, up);
522 const glm::mat4 proj = perspectiveVulkanRH_ZO(degToRad(camFov), float(vw) / float(vh),
523 camNear, camFar);
524 const glm::mat4 viewProj = proj * view;
525
526 static const glm::vec3 kLocalCorners[8] = {
527 {-0.5f, -0.5f, -0.5f}, {0.5f, -0.5f, -0.5f}, {-0.5f, 0.5f, -0.5f},
528 {-0.5f, -0.5f, 0.5f}, {0.5f, 0.5f, -0.5f}, {0.5f, -0.5f, 0.5f},
529 {-0.5f, 0.5f, 0.5f}, {0.5f, 0.5f, 0.5f}};
530
531 std::string entities;
532 int count = 0;
534 if (ecs::current()->getManager<scene::SceneHost>() != nullptr) {
535 auto hostView = ecs::View<scene::SceneHost, scene::SceneHost::Meta,
536 scene::SceneHost::Tree>();
537 for (auto it = hostView.begin(); it != hostView.end(); ++it) {
538 auto [meta, tree] = *it;
539 if (!meta || !meta->entity || !tree) continue;
540 auto *host = meta->entity;
541 host->walkDepthFirst([&](scene::SceneHost *, int, scene::SceneNode &node) {
542 if (!node.visible || node.space != "3d") return;
543 float minX = 1e30f, minY = 1e30f, minZ = 1e30f;
544 float maxX = -1e30f, maxY = -1e30f, maxZ = -1e30f;
545 float scMinX = 1e30f, scMinY = 1e30f;
546 float scMaxX = -1e30f, scMaxY = -1e30f;
547 bool anyInFront = false;
548 for (const auto &c : kLocalCorners) {
549 const glm::vec4 w = node.world * glm::vec4(c, 1.f);
550 minX = std::min(minX, w.x);
551 minY = std::min(minY, w.y);
552 minZ = std::min(minZ, w.z);
553 maxX = std::max(maxX, w.x);
554 maxY = std::max(maxY, w.y);
555 maxZ = std::max(maxZ, w.z);
556 const glm::vec4 clip = viewProj * w;
557 if (clip.w <= 1e-6f) continue;
558 anyInFront = true;
559 const float ndcX = clip.x / clip.w;
560 const float ndcY = clip.y / clip.w;
561 const float sx = (ndcX * 0.5f + 0.5f) * float(vw);
562 const float sy = (ndcY * 0.5f + 0.5f) * float(vh);
563 scMinX = std::min(scMinX, sx);
564 scMinY = std::min(scMinY, sy);
565 scMaxX = std::max(scMaxX, sx);
566 scMaxY = std::max(scMaxY, sy);
567 }
568 if (!anyInFront) return;
569 if (scMaxX < 0.f || scMinX > float(vw) || scMaxY < 0.f ||
570 scMinY > float(vh))
571 return;
572 if (!entities.empty()) entities += ",";
573 entities += "{\"id\":\"" + jsonEscape(node.id.empty() ? node.name : node.id) +
574 "\",\"asset\":\"" +
575 jsonEscape(node.name.empty() ? node.id : node.name) +
576 "\",\"world_aabb\":{\"min\":" + vec3Json(glm::vec3(minX, minY, minZ)) +
577 ",\"max\":" + vec3Json(glm::vec3(maxX, maxY, maxZ)) +
578 "},\"screen_bbox\":{\"x\":" + num(scMinX) + ",\"y\":" + num(scMinY) +
579 ",\"w\":" + num(scMaxX - scMinX) + ",\"h\":" + num(scMaxY - scMinY) +
580 "}}";
581 ++count;
582 });
583 }
584 }
585 if (ok) *ok = true;
586 return "{\"camera\":" + vec3Json(camEye) + ",\"cameraTarget\":" + vec3Json(camTarget) +
587 ",\"fov\":" + num(camFov) + ",\"viewportWidth\":" + std::to_string(vw) +
588 ",\"viewportHeight\":" + std::to_string(vh) + ",\"entities\":[" + entities +
589 "],\"count\":" + std::to_string(count) + "}";
590 }
591};
592
593class ProcgenProbeSinkImpl final : public eve::IProcgenProbeSink {
594public:
595 struct Batch {
596 std::vector<std::unique_ptr<ReflectionProbeCapture>> reflections;
597 std::vector<eve::ProcgenProbeDesc> lights;
598 };
599
600 Result<int> replaceProbeBatch(const std::string& batchId,
601 const std::vector<eve::ProcgenProbeDesc>& probes) override {
602 Graphics* graphics = eve::ModuleManager::getInstance<Graphics>("Graphics");
603 if (batchId.empty() || !graphics)
605 "procedural probe publication requires a batch id and graphics provider"));
606 Batch candidate;
607 std::unordered_set<std::uint64_t> identities;
608 for (const auto& probe : probes) {
609 if (!probe.sourcePointId || !identities.insert(probe.sourcePointId).second ||
610 probe.resource.empty() || (probe.type != 0 && probe.type != 1) ||
611 !std::isfinite(probe.x) || !std::isfinite(probe.y) || !std::isfinite(probe.z) ||
612 !std::isfinite(probe.extentX) || !std::isfinite(probe.extentY) || !std::isfinite(probe.extentZ) ||
613 probe.extentX <= 0 || probe.extentY <= 0 || probe.extentZ <= 0 ||
614 probe.resolution < 16 || probe.resolution > 2048 ||
615 !std::isfinite(probe.clipDistance) || probe.clipDistance <= 0 ||
616 !std::isfinite(probe.irradianceR) || !std::isfinite(probe.irradianceG) ||
617 !std::isfinite(probe.irradianceB) || probe.irradianceR < 0 || probe.irradianceG < 0 ||
618 probe.irradianceB < 0)
620 "procedural probe publication requires valid unique probe descriptors"));
621 if (probe.hasSphericalHarmonics &&
622 !std::all_of(probe.sphericalHarmonics.begin(), probe.sphericalHarmonics.end(),
623 [](float coefficient) { return std::isfinite(coefficient); }))
625 "procedural light probe spherical harmonics must be finite"));
626 if (probe.type == 1) {
627 candidate.lights.push_back(probe);
628 continue;
629 }
630 auto capture = std::make_unique<ReflectionProbeCapture>(graphics);
631 capture->configure(probe.x, probe.y, probe.z, probe.resolution, 0.1F, probe.clipDistance);
632 capture->configureInfluence(probe.extentX, probe.extentY, probe.extentZ, 1, 1, 0);
633 capture->requestCapture();
634 candidate.reflections.push_back(std::move(capture));
635 }
636 ensureRegistry();
637 for (auto& probe : candidate.reflections) registry_->add(probe.get());
638 auto found = batches_.find(batchId);
639 if (found != batches_.end())
640 for (auto& probe : found->second.reflections) registry_->remove(probe.get());
641 DiffuseLightProbeRegistry::instance().replaceBatch(batchId, candidate.lights);
642 batches_.insert_or_assign(batchId, std::move(candidate));
643 return Result<int>::success(static_cast<int>(probes.size()));
644 }
645
646 Result<int> removeProbeBatch(const std::string& batchId) override {
647 auto found = batches_.find(batchId);
648 if (found == batches_.end())
650 "procedural probe batch was not published", batchId));
651 const int count = static_cast<int>(found->second.reflections.size() + found->second.lights.size());
652 if (registry_)
653 for (auto& probe : found->second.reflections) registry_->remove(probe.get());
655 batches_.erase(found);
657 }
658
659 Result<int> tickProbeBatches(int faceBudget, int filterBudget, int filterSamples) override {
660 if (faceBudget < 0 || filterBudget < 0 || filterSamples < 8 || filterSamples > 512)
662 "procedural probe tick requires nonnegative budgets and 8..512 filter samples"));
663 ensureRegistry();
664 return Result<int>::success(registry_->tick(faceBudget, filterBudget, filterSamples));
665 }
666 int probeCount(const std::string& batchId) const override {
667 auto found = batches_.find(batchId);
668 return found == batches_.end() ? 0 : static_cast<int>(found->second.reflections.size() + found->second.lights.size());
669 }
670 int reflectionProbeCount(const std::string& batchId) const override {
671 auto found = batches_.find(batchId);
672 return found == batches_.end() ? 0 : static_cast<int>(found->second.reflections.size());
673 }
674 int lightProbeCount(const std::string& batchId) const override {
675 auto found = batches_.find(batchId);
676 return found == batches_.end() ? 0 : static_cast<int>(found->second.lights.size());
677 }
678private:
679 void ensureRegistry() {
680 if (!registry_) registry_ = std::make_unique<ReflectionProbeRegistry>();
681 }
682 std::unique_ptr<ReflectionProbeRegistry> registry_;
683 std::unordered_map<std::string, Batch> batches_;
684};
685
686} // namespace
687
689 static RenderCaptureImpl impl;
690 static ProcgenProbeSinkImpl probeSink;
691 eve::cap::provide<eve::IRenderCapture>(&impl);
692 eve::cap::provide<eve::IProcgenProbeSink>(&probeSink);
693}
694
695} // namespace eve::graphics
LogicalId target
float w
Definition AnimClip.cpp:738
const std::string & s
float yaw
Definition BushMesh.cpp:37
void * impl
Generation generation
Definition ECS.cpp:40
std::string name
Definition ECS.cpp:154
tensor::Graph g
Definition GpuGraph.cpp:7
std::vector< eve::ProcgenProbeDesc > lights
std::vector< std::unique_ptr< ReflectionProbeCapture > > reflections
float camera[2]
wgpu::PopErrorScopeStatus status
glm::vec4 clip
Entity-ID / isolated visibility capture kept off Graphics.h.
double r
float v
HexVec3 up
std::int32_t second
std::int32_t c
std::string id
MeshVfxBatchedDraw draw
OceanMeshSettings mesh
std::shared_ptr< OceanRenderer > renderer
ssq::Object frame
OceanSprayView view
std::string path
Definition PlayHost.cpp:111
std::vector< ecs::EntityHandle > entities
PrimitiveHandle handle
std::set< std::string > identities
float d
Renderable3D::MeshRenderer * mr
glm::vec3 eye
glm::mat4 viewProj
glm::mat4 proj
const RoadNode * node
bool found
Graphics * graphics
ScriptStructure out
int count
V3 dir
Definition TreeMesh.cpp:151
UIHostHandle host
static Diagnostic error(DiagnosticCode code, std::string message, std::string path={})
Construct an error diagnostic with the standard error severity.
Optional graphics consumer for complete generated probe batches. Implementations own all runtime reso...
Frame capture + camera + visible-entity inspection (graphics).
Move-only operation result carrying either a value or Status.
Definition Result.h:176
static Result success(T value)
Construct a successful result owning value.
Definition Result.h:185
static Result failure(Status status)
Construct a failed result from a structured status.
Definition Result.h:196
static Camera3D * createCamera(ecs::Table &table= *ecs::current())
Create an active camera in the current ECS table.
void removeBatch(const std::string &batchId)
Remove every diffuse probe owned by one generated batch.
void replaceBatch(const std::string &batchId, const std::vector< eve::ProcgenProbeDesc > &probes)
Atomically replace the diffuse probes owned by one generated batch.
static DiffuseLightProbeRegistry & instance()
Return the process-wide graphics-owned registry.
static void updateAll()
Updates all.
卡牌游戏 UI 工具模块:工厂 + 脚本绑定入口。 功能参考 ycarowr/UiCard:扇形手牌布局、抽牌/洗牌、悬浮放大、拖拽到落牌区、 敌方手牌(背面/偷看)、费用不足置灰,以及可实时调节的布局...
Definition Animation.h:25
glm::mat4 cameraProjectionVulkanRH_ZO(bool orthographic, float fovyRad, float orthoHeight, float aspect, float zNear, float zFar)
Camera projection shared by rendering, picking, and diagnostic passes.
Definition ClipSpace.h:38
glm::mat4 perspectiveVulkanRH_ZO(float fovyRad, float aspect, float zNear, float zFar)
Right-handed, zero-to-one depth perspective for Vulkan swapchains.
Definition ClipSpace.h:22
void registerGraphicsCapabilities()
Registers graphics capabilities.
ForceVector add(ForceVector a, ForceVector b)
Definition ForceMath.h:13
int findLinkKind(const char *kind)
Finds link kind.
char * b64_encode(const char *src, size_t srclen, size_t linelen, size_t &dstlen)
Base64-encode data.
Definition b64.cpp:28
bool enabled
Pixel size reported by a successful frame/image capture.
Render surface / frame state snapshot (value type).
Generation-qualified snapshot of one live field-backed Renderable3D.
std::uint32_t entityId
glm::uvec4 info