载入中...
搜索中...
未找到
FrameCaptureImage.cpp
浏览该文件的文档.
2#include <cmath>
3#include <cstring>
4#include <limits>
5namespace eve {
6namespace {
7template <class T>
8Result<T> invalid(std::string message) {
10}
11} // namespace
13 if (!framePixelBytes(b.format) || !b.bytes || x >= b.width || y >= b.height ||
14 uint64_t(b.width) * b.height * framePixelBytes(b.format) != b.bytes->size())
15 return invalid<FramePixel>("PixelOutOfBoundsOrUnavailable");
17 const auto* src = b.bytes->data() + (size_t(y) * b.width + x) * framePixelBytes(b.format);
18 p.labels = {"r", "g", "b", "a"};
19 if (b.format == FramePixelFormat::Depth32Float) {
20 float depth;
21 std::memcpy(&depth, src, sizeof(depth));
22 p.channels = 1;
23 p.stored[0] = p.decoded[0] = depth;
24 p.labels[0] = "deviceDepth";
25 } else if (b.format == FramePixelFormat::Rgba16Float) {
26 for (size_t i = 0; i < 4; ++i) {
27 uint16_t bits;
28 std::memcpy(&bits, src + i * sizeof(bits), sizeof(bits));
29 const int exponent = (bits >> 10) & 31;
30 const int fraction = bits & 1023;
31 const double value = exponent == 0 ? std::ldexp(double(fraction), -24)
32 : exponent == 31 ? (fraction ? std::numeric_limits<double>::quiet_NaN()
33 : std::numeric_limits<double>::infinity())
34 : std::ldexp(double(1024 + fraction), exponent - 25);
35 p.stored[i] = p.decoded[i] = bits & 0x8000 ? -value : value;
36 }
37 } else {
38 for (size_t i = 0; i < 4; ++i) {
39 const auto c = b.format == FramePixelFormat::Bgra8 && i % 2 == 0 ? 2 - i : i;
40 p.stored[i] = src[c];
41 p.decoded[i] = src[c] / 255.0;
42 }
43 if (b.attachment == FrameAttachment::Normal) {
44 p.labels = {"worldNormalX", "worldNormalY", "worldNormalZ", "legacyPbr"};
45 for (size_t i = 0; i < 3; ++i) p.decoded[i] = p.decoded[i] * 2.0 - 1.0;
46 } else if (b.attachment == FrameAttachment::Velocity || b.attachment == FrameAttachment::Depth) {
47 p.labels = {"linearDepth01", "motionNdcX", "motionNdcY", "unused"};
48 p.decoded[1] = p.decoded[1] * 2.0 - 1.0;
49 p.decoded[2] = p.decoded[2] * 2.0 - 1.0;
50 } else if (b.attachment == FrameAttachment::PbrParams) {
51 p.labels = {"metallic", "roughness", "occlusion", "specularFactor"};
52 } else if (b.attachment == FrameAttachment::Albedo) {
53 p.labels[3] = "linearDepth01";
54 }
55 }
56 return Result<FramePixel>::success(std::move(p));
57}
59 const std::array<float, 16>& matrix) {
60 if (b.attachment != FrameAttachment::HwDepth || b.format != FramePixelFormat::Depth32Float)
61 return invalid<double>("UnsupportedView");
62 auto pixel = frameCapturePixel(b, x, y);
63 if (!pixel) return Result<double>::failure(pixel.status());
64 const double nx = (double(x) + 0.5) / b.width * 2 - 1;
65 const double ny = (double(y) + 0.5) / b.height * 2 - 1;
66 const double z = pixel.value().stored[0];
67 const double vz = matrix[2] * nx + matrix[6] * ny + matrix[10] * z + matrix[14];
68 const double vw = matrix[3] * nx + matrix[7] * ny + matrix[11] * z + matrix[15];
69 if (!std::isfinite(vz) || !std::isfinite(vw) || std::abs(vw) < 1e-12)
70 return invalid<double>("InvalidDepthProjection");
71 return Result<double>::success(std::abs(vz / vw));
72}
73} // namespace eve
double z
double y
double x
glm::vec4 p[6]
std::string message
std::int32_t c
MeleePoint3 b
Definition MeleeHit.cpp:41
Object::Ptr value
glm::dmat4 matrix
std::uint32_t depth
float fraction
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
Status invalid(const char *message)
Build metadata (engine git commit, build time, third-party version).
Definition Build.cpp:16
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.
One pixel with native storage and decoded attachment semantics.
std::array< std::string_view, 4 > labels