载入中...
搜索中...
未找到
ActionGuideExport.cpp
浏览该文件的文档.
2
3#include "common/Diagnostic.h"
6
7#include <fstream>
8#include <iomanip>
9#include <sstream>
10#include <stdexcept>
11#include <utility>
12
13namespace eve::dev {
14namespace {
15
16template <class T>
17Result<T> fail(DiagnosticCode code, std::string message, std::string path = {}) {
18 return Result<T>::failure(Diagnostic::error(code, std::move(message), std::move(path)));
19}
20
21void writeBytes(const std::filesystem::path& path, const char* data, size_t size) {
22 std::ofstream stream(path, std::ios::binary | std::ios::trunc);
23 stream.write(data, static_cast<std::streamsize>(size));
24 if (!stream) throw std::runtime_error("failed writing " + path.generic_string());
25}
26
27std::string padShotFolder(size_t index1Based) {
28 std::ostringstream oss;
29 oss << "shots/" << std::setw(4) << std::setfill('0') << index1Based;
30 return oss.str();
31}
32
33Value canvasValue(const ActionGuideCanvas& canvas) {
34 return Value::object(
35 {{"width", Value(static_cast<std::int64_t>(canvas.width))},
36 {"height", Value(static_cast<std::int64_t>(canvas.height))},
37 {"pixelsPerUnit", Value(canvas.pixelsPerUnit)},
38 {"backgroundRgba", Value::array({Value(canvas.backgroundRgba[0]), Value(canvas.backgroundRgba[1]),
39 Value(canvas.backgroundRgba[2]), Value(canvas.backgroundRgba[3])})},
40 {"transparent", Value(canvas.transparent)}});
41}
42
43Value cameraValue(const ActionGuideCamera& camera) {
44 return Value::object(
45 {{"projection",
46 Value(camera.projection == ActionGuideProjection::Perspective ? "perspective" : "orthographic")},
47 {"fovYDegrees", Value(camera.fovYDegrees)},
48 {"orthoSize", Value(camera.orthoSize)},
49 {"nearPlane", Value(camera.nearPlane)},
50 {"farPlane", Value(camera.farPlane)},
51 {"eye", Value::array({Value(camera.eye[0]), Value(camera.eye[1]), Value(camera.eye[2])})},
52 {"target", Value::array({Value(camera.target[0]), Value(camera.target[1]), Value(camera.target[2])})},
53 {"up", Value::array({Value(camera.up[0]), Value(camera.up[1]), Value(camera.up[2])})}});
54}
55
56Result<std::vector<uint8_t>> encodeRgbaPng(const ActionGuideAovBuffer& aov) {
57 if (!aov.rgba8 || aov.width == 0 || aov.height == 0)
58 return fail<std::vector<uint8_t>>(DiagnosticCode::InvalidArgument, "AOV buffer is empty", aov.aov);
59 if (uint64_t(aov.width) * aov.height * 4 != aov.rgba8->size())
60 return fail<std::vector<uint8_t>>(DiagnosticCode::InvalidArgument, "AOV RGBA size mismatch", aov.aov);
64 buffer.width = aov.width;
65 buffer.height = aov.height;
66 buffer.bytes = aov.rgba8;
68 options.view = "rawChannels";
69 options.maxWidth = aov.width;
70 options.maxHeight = aov.height;
71 options.maxBytes = size_t(aov.width) * aov.height * 4 + 64u * 1024u;
72 auto preview = frameCapturePreview(buffer, options);
73 if (!preview) return Result<std::vector<uint8_t>>::failure(preview.status());
74 return Result<std::vector<uint8_t>>::success(std::move(preview.value().png));
75}
76
77} // namespace
78
80 if (aov == "silhouette" || aov == "guide.silhouette") return Result<std::string>::success("silhouette");
81 if (aov == "depth" || aov == "guide.depth") return Result<std::string>::success("depth");
82 if (aov == "bone" || aov == "guide.bone") return Result<std::string>::success("bone");
83 if (aov == "partId" || aov == "guide.partId") return Result<std::string>::success("partId");
84 if (aov == "normal" || aov == "guide.normal") return Result<std::string>::success("normal");
85 if (aov == "final" || aov == "whitebox_final") return Result<std::string>::success("whitebox_final");
86 return fail<std::string>(DiagnosticCode::InvalidArgument, "unsupported guide AOV name", std::string(aov));
87}
88
90 const std::filesystem::path& root) {
92 return fail<Value>(DiagnosticCode::InvalidArgument, "capture is not ready for export");
93 if (snapshot.shots.empty())
94 return fail<Value>(DiagnosticCode::InvalidArgument, "ready capture has no shot artifacts");
95 try {
96 std::filesystem::create_directories(root);
97 } catch (const std::exception& e) {
98 return fail<Value>(DiagnosticCode::Failed, std::string("create export root: ") + e.what());
99 }
100
101 Value::Array shotIndex;
102 Value::Array files;
103
104 try {
105 for (size_t i = 0; i < snapshot.shots.size(); ++i) {
106 const auto& shot = snapshot.shots[i];
107 const auto folder = padShotFolder(i + 1);
108 const auto shotDir = root / folder;
109 std::filesystem::create_directories(shotDir);
110
111 Value::Array aovNames;
112 Value::Object shotFiles;
113 for (const auto& aov : shot.aovs) {
114 auto stem = actionGuideAovFileStem(aov.aov);
115 if (!stem) return Result<Value>::failure(stem.status());
116 auto png = encodeRgbaPng(aov);
117 if (!png) return Result<Value>::failure(png.status());
118 const auto fileName = stem.value() + ".png";
119 const auto relPath = folder + "/" + fileName;
120 const auto tmpPath = shotDir / (fileName + ".tmp");
121 const auto dstPath = shotDir / fileName;
122 writeBytes(tmpPath, reinterpret_cast<const char*>(png.value().data()), png.value().size());
123 std::filesystem::rename(tmpPath, dstPath);
124 aovNames.emplace_back(aov.aov);
125 shotFiles.emplace(stem.value(), Value(relPath));
126 files.emplace_back(relPath);
127 }
128
130 for (const auto& tag : shot.tags) tags.emplace_back(tag);
131
132 Value::Object meta;
133 meta.emplace("shotId", Value(shot.shotId));
134 meta.emplace("timeSeconds", Value(shot.timeSeconds));
135 if (shot.sampleIndex) meta.emplace("sampleIndex", Value(*shot.sampleIndex));
136 meta.emplace("view", Value(shot.view));
137 meta.emplace("aovs", Value(std::move(aovNames)));
138 meta.emplace("tags", Value(std::move(tags)));
139 meta.emplace("canvas", canvasValue(snapshot.pack.canvas));
140 meta.emplace("camera", cameraValue(snapshot.pack.camera));
141 meta.emplace("pixelsPerUnit", Value(snapshot.pack.canvas.pixelsPerUnit));
142 meta.emplace("depthEncoding", Value("normalized-gray8-near-far"));
143 meta.emplace("files", Value(std::move(shotFiles)));
144
145 const auto metaJson = Value(std::move(meta)).toJson();
146 if (!metaJson) return Result<Value>::failure(metaJson.status());
147 const auto metaTmp = shotDir / "meta.json.tmp";
148 const auto metaDst = shotDir / "meta.json";
149 writeBytes(metaTmp, metaJson.value().data(), metaJson.value().size());
150 std::filesystem::rename(metaTmp, metaDst);
151 files.emplace_back(folder + "/meta.json");
152
153 shotIndex.emplace_back(Value::object({{"shotId", Value(shot.shotId)}, {"folder", Value(folder)}}));
154 }
155
156 Value manifest = Value::object({{"schemaId", Value("eve.action.guide-pack.artifact")},
157 {"schemaVersion", Value(static_cast<std::int64_t>(1))},
158 {"revision", Value(static_cast<std::int64_t>(snapshot.revision))},
159 {"backend", Value(snapshot.backend)},
160 {"token", Value(snapshot.token)},
161 {"shotCount", Value(static_cast<std::int64_t>(snapshot.shots.size()))},
162 {"shots", Value(std::move(shotIndex))}});
163 const auto manifestJson = manifest.toJson();
164 if (!manifestJson) return Result<Value>::failure(manifestJson.status());
165 const auto manifestTmp = root / "manifest.json.tmp";
166 const auto manifestDst = root / "manifest.json";
167 writeBytes(manifestTmp, manifestJson.value().data(), manifestJson.value().size());
168 std::filesystem::rename(manifestTmp, manifestDst);
169
171 Value::object({{"exportRoot", Value(root.generic_string())},
172 {"manifest", Value("manifest.json")},
173 {"files", Value(std::move(files))},
174 {"shotCount", Value(static_cast<std::int64_t>(snapshot.shots.size()))},
175 {"revision", Value(static_cast<std::int64_t>(snapshot.revision))},
176 {"backend", Value(snapshot.backend)}}));
177 } catch (const std::exception& e) {
178 return fail<Value>(DiagnosticCode::Failed, std::string("guide export failed: ") + e.what());
179 }
180}
181
182} // namespace eve::dev
Write a Ready guide-capture snapshot to the guide-pack artifact directory layout.
int root
Definition AnimSmr.cpp:119
Stable, structured diagnostics shared by engine modules.
std::string message
DiagnosticCode code
float camera[2]
Snapshot snapshot
std::unordered_set< std::string > tags
const std::string * tag
std::unique_ptr< gpgpu::GpuBuffer > buffer
Definition OnnxGpgpu.cpp:26
std::string path
Definition PlayHost.cpp:111
const SquirrelValueOptions & options
float size
Definition TreeMesh.cpp:158
static Diagnostic error(DiagnosticCode code, std::string message, std::string path={})
Construct an error diagnostic with the standard error severity.
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
The canonical owning dynamic value used by data-facing protocols.
Definition Value.h:31
Result< std::string > toJson() const
Serialize this value as deterministic compact JSON.
Definition Value.cpp:187
std::map< std::string, Value > Object
Definition Value.h:34
std::vector< Value > Array
Definition Value.h:33
static Value array(Array value)
Compatibility factory for an array value.
Definition Value.cpp:128
static Value object(Object value)
Compatibility factory for an object value.
Definition Value.cpp:129
Result< Value > writeActionGuideArtifactPack(const ActionGuideCaptureSnapshot &snapshot, const std::filesystem::path &root)
Encode Ready silhouette/depth(/bone) buffers as PNG + manifest under root.
Result< FramePreview > frameCapturePreview(const FrameCaptureBuffer &b, const FramePreviewOptions &o)
Result< std::string > actionGuideAovFileStem(std::string_view aov)
Map pack AOV name to export file stem (silhouette → silhouette.png).
DiagnosticCode
Stable machine-readable diagnostic codes.
Definition Diagnostic.h:47
One lease-owned CPU AOV buffer (always RGBA8 for PNG export). @ownership rgba8 is immutable shared st...
std::shared_ptr< const std::vector< std::uint8_t > > rgba8
Offscreen guide camera; never mutates the live gameplay camera.
Output canvas contract for guide images and landmark pixels.
std::array< double, 4 > backgroundRgba
Immutable CPU snapshot for one guide-capture job. @lifetime Bytes remain valid until releaseCapture; ...
Immutable CPU attachment with producer-provided decoding metadata.
FrameAttachment attachment
Explicit CPU preview transformation, separate from costly GPU capture.