载入中...
搜索中...
未找到
FrameCapturePreview.cpp
浏览该文件的文档.
2#include <Poco/Checksum.h>
3#include <Poco/DeflatingStream.h>
4#include <algorithm>
5#include <cmath>
6#include <sstream>
7namespace eve::dev {
8namespace {
9template <class T>
10Result<T> invalid(std::string message) {
12}
13void be32(std::vector<uint8_t>& out, uint32_t value) {
14 for (int shift = 24; shift >= 0; shift -= 8) out.push_back(static_cast<uint8_t>(value >> shift));
15}
16void chunk(std::vector<uint8_t>& out, const char* type, const std::vector<uint8_t>& bytes) {
17 be32(out, static_cast<uint32_t>(bytes.size()));
18 const size_t start = out.size();
19 out.insert(out.end(), type, type + 4);
20 out.insert(out.end(), bytes.begin(), bytes.end());
21 Poco::Checksum checksum(Poco::Checksum::TYPE_CRC32);
22 checksum.update(reinterpret_cast<const char*>(out.data() + start), static_cast<unsigned>(out.size() - start));
23 be32(out, checksum.checksum());
24}
25uint8_t unorm(double value) {
26 return std::isfinite(value) ? static_cast<uint8_t>(std::lround(std::clamp(value, 0.0, 1.0) * 255.0)) : 255;
27}
28} // namespace
30 if (!framePixelBytes(b.format) || !b.bytes || !b.width || !b.height ||
31 uint64_t(b.width) * b.height * framePixelBytes(b.format) != b.bytes->size() || !o.maxWidth || !o.maxHeight ||
32 o.maxWidth > 2048 || o.maxHeight > 2048 || !std::isfinite(o.rangeMin) || !std::isfinite(o.rangeMax) ||
33 o.rangeMax <= o.rangeMin || !std::isfinite(o.exposure) || std::abs(o.exposure) > 20 || o.channel < 0 ||
34 o.channel > 3)
35 return invalid<FramePreview>("InvalidPreviewParametersOrStorage");
37 out.view = o.view;
38 if (out.view == "auto") {
39 if (b.format == FramePixelFormat::Depth32Float || b.attachment == FrameAttachment::Depth)
40 out.view = "scalar";
41 else if (b.attachment == FrameAttachment::Normal)
42 out.view = "normalWorld";
43 else if (b.attachment == FrameAttachment::Velocity)
44 out.view = "velocity";
45 else
46 out.view = "rawChannels";
47 }
48 if (out.view != "linearDepth" && out.view != "scalar" && out.view != "rawChannels" && out.view != "normalWorld" &&
49 out.view != "velocity" && out.view != "hdrExposure")
50 return invalid<FramePreview>("UnsupportedView");
51 if ((out.view == "normalWorld" && b.attachment != FrameAttachment::Normal) ||
52 (out.view == "velocity" && b.attachment != FrameAttachment::Velocity))
53 return invalid<FramePreview>("UnsupportedView");
54 if ((out.view == "linearDepth" || (out.view == "scalar" && b.format == FramePixelFormat::Depth32Float)) &&
55 o.channel != 0)
56 return invalid<FramePreview>("UnsupportedChannel");
57 if (out.view == "hdrExposure" && b.attachment != FrameAttachment::Final &&
58 b.attachment != FrameAttachment::Albedo && b.attachment != FrameAttachment::Emissive)
59 return invalid<FramePreview>("UnsupportedView");
60 const double scale = (std::min)({1.0, double(o.maxWidth) / b.width, double(o.maxHeight) / b.height});
61 out.width = (std::max)(1u, static_cast<uint32_t>(b.width * scale));
62 out.height = (std::max)(1u, static_cast<uint32_t>(b.height * scale));
63 std::vector<uint8_t> scanlines((size_t(out.width) * 4 + 1) * out.height);
64 for (uint32_t y = 0; y < out.height; ++y) {
65 const size_t row = size_t(y) * (size_t(out.width) * 4 + 1);
66 for (uint32_t x = 0; x < out.width; ++x) {
67 auto pixel = frameCapturePixel(b, uint32_t(uint64_t(x) * b.width / out.width),
68 uint32_t(uint64_t(y) * b.height / out.height));
69 if (!pixel) return Result<FramePreview>::failure(pixel.status());
70 auto values = pixel.value().decoded;
71 if (out.view == "linearDepth") {
72 auto depth =
73 frameCaptureLinearDepth(b, uint32_t(uint64_t(x) * b.width / out.width),
74 uint32_t(uint64_t(y) * b.height / out.height), o.inverseProjection);
75 if (!depth) return Result<FramePreview>::failure(depth.status());
76 values[0] = depth.value();
77 }
78 if (out.view == "normalWorld")
79 for (size_t c = 0; c < 3; ++c) values[c] = values[c] * 0.5 + 0.5;
80 if (out.view == "velocity") values = {values[1] * 0.5 + 0.5, values[2] * 0.5 + 0.5, 0.5, 1};
81 if (out.view == "rawChannels") {
82 for (size_t c = 0; c < 4; ++c) {
83 values[c] = b.format == FramePixelFormat::Depth32Float ? pixel.value().stored[0]
84 : b.format == FramePixelFormat::Rgba16Float ? pixel.value().stored[c]
85 : pixel.value().stored[c] / 255.0;
86 }
87 }
88 if (out.view == "scalar" || out.view == "linearDepth") {
89 const double value = (values[static_cast<size_t>(o.channel)] - o.rangeMin) / (o.rangeMax - o.rangeMin);
90 values = {value, value, value, 1};
91 }
92 if (out.view == "hdrExposure")
93 for (size_t c = 0; c < 3; ++c)
94 values[c] = 1.0 - std::exp(-(std::max)(0.0, values[c]) * std::exp2(o.exposure));
95 const size_t offset = row + 1 + size_t(x) * 4;
96 for (size_t c = 0; c < 3; ++c) scanlines[offset + c] = unorm(values[c]);
97 scanlines[offset + 3] = 255; // Packed metadata alpha never becomes preview transparency.
98 }
99 }
100 std::ostringstream compressed;
101 Poco::DeflatingOutputStream zlib(compressed, Poco::DeflatingStreamBuf::STREAM_ZLIB);
102 zlib.write(reinterpret_cast<const char*>(scanlines.data()), static_cast<std::streamsize>(scanlines.size()));
103 zlib.close();
104 const auto data = compressed.str();
105 out.png = {137, 80, 78, 71, 13, 10, 26, 10};
106 std::vector<uint8_t> header;
107 be32(header, out.width);
108 be32(header, out.height);
109 header.insert(header.end(), {8, 6, 0, 0, 0});
110 chunk(out.png, "IHDR", header);
111 chunk(out.png, "IDAT", std::vector<uint8_t>(data.begin(), data.end()));
112 chunk(out.png, "IEND", {});
113 if (((out.png.size() + 2) / 3) * 4 > o.maxBytes) return invalid<FramePreview>("ImageTooLarge");
114 return Result<FramePreview>::success(std::move(out));
115}
116} // namespace eve::dev
double y
double x
Duration start
std::string message
std::int32_t c
size_t offset
std::array< float, 3 > scale
std::uint64_t bytes
MeleePoint3 b
Definition MeleeHit.cpp:41
std::set< std::string > values
Object::Ptr value
ScriptStructure out
std::uint32_t depth
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
Result< FramePreview > frameCapturePreview(const FrameCaptureBuffer &b, const FramePreviewOptions &o)
Status invalid(const char *message)
constexpr size_t framePixelBytes(FramePixelFormat format)
Packed CPU bytes per pixel; pure value query, callable on any thread.
Result< double > frameCaptureLinearDepth(const FrameCaptureBuffer &b, uint32_t x, uint32_t y, const std::array< float, 16 > &matrix)
Reconstruct positive view depth using this snapshot's projection. @thread Any thread with CPU leases.
Result< FramePixel > frameCapturePixel(const FrameCaptureBuffer &b, uint32_t x, uint32_t y)
Read CPU snapshot only; no callback, rendering, or GPU wait. @thread Any thread with a snapshot lease...
Immutable CPU attachment with producer-provided decoding metadata.
Explicit CPU preview transformation, separate from costly GPU capture.
std::array< float, 16 > inverseProjection
Encoded preview; width and height may differ from the original attachment.