载入中...
搜索中...
未找到
EvpackGraphicsLoader.cpp
浏览该文件的文档.
2#include <algorithm>
3#include <cmath>
4#include <limits>
5#include <new>
7namespace eve::asset_graphics {
8namespace {}
10 const asset::EvpackCapabilities& capabilities,
12 std::uint32_t texcoordSet, bool preserveAllTexcoords) const {
13 auto payload = reader_.read(asset, "eve.mesh/3", capabilities, limits.maximumDecodedBytes);
14 if (!payload && payload.error()->code() == DiagnosticCode::TypeMismatch)
15 payload = reader_.read(asset, "eve.mesh/2", capabilities, limits.maximumDecodedBytes);
16 if (!payload && payload.error()->code() == DiagnosticCode::TypeMismatch)
17 payload = reader_.read(asset, "eve.mesh/1", capabilities, limits.maximumDecodedBytes);
19 const asset::RuntimeAssetChunk* bulk = nullptr;
20 for (const auto& chunk : payload.value().chunks) {
21 if (chunk.kind != asset::EvpackChunkKind::Bulk) continue;
22 if (bulk)
24 DiagnosticCode::ParseError, "eve.mesh must contain exactly one bulk chunk", {}, {}, "asset.graphics"));
25 bulk = &chunk;
26 }
27 if (!bulk)
29 Diagnostic::error(DiagnosticCode::NotFound, "eve.mesh bulk chunk is missing", {}, {}, "asset.graphics"));
30 auto staging = asset::decodeCanonicalMesh(
31 bulk->bytes, {limits.maximumVertices, limits.maximumIndices, limits.maximumDecodedBytes});
32 if (!staging) return Result<LoadedGraphicsMesh>::failure(staging.status());
33 std::array<float, 6> localBounds;
34 for (unsigned axis = 0; axis < 3; ++axis) {
35 localBounds[axis] = std::numeric_limits<float>::max();
36 localBounds[axis + 3] = std::numeric_limits<float>::lowest();
37 }
38 for (std::size_t i = 0; i < staging.value().positions.size(); ++i) {
39 const auto axis = i % 3;
40 localBounds[axis] = std::min(localBounds[axis], staging.value().positions[i]);
41 localBounds[axis + 3] = std::max(localBounds[axis + 3], staging.value().positions[i]);
42 }
43 if (staging.value().positions.size() / 3 > size_t(std::numeric_limits<int>::max()) ||
44 staging.value().indices.size() > size_t(std::numeric_limits<int>::max()))
46 DiagnosticCode::InvalidArgument, "mesh exceeds backend count range", {}, {}, "asset.graphics"));
47 const auto uv = staging.value().texcoords.find(texcoordSet);
48 if (uv == staging.value().texcoords.end() && (texcoordSet != 0 || !staging.value().texcoords.empty()))
50 DiagnosticCode::NotFound, "requested UV set is absent; no remapping performed", {}, {}, "asset.graphics"));
51 auto uploaded = staging.value().colors.empty()
52 ? factory_.uploadMesh(staging.value().positions.data(),
53 staging.value().normals.empty() ? nullptr : staging.value().normals.data(),
54 uv == staging.value().texcoords.end() ? nullptr : uv->second.data(),
55 static_cast<int>(staging.value().positions.size() / 3), staging.value().indices.data(),
56 static_cast<int>(staging.value().indices.size()))
57 : factory_.uploadMeshColored(staging.value().positions.data(),
58 staging.value().normals.empty() ? nullptr : staging.value().normals.data(),
59 uv == staging.value().texcoords.end() ? nullptr : uv->second.data(),
60 staging.value().colors.data(),
61 static_cast<int>(staging.value().positions.size() / 3), staging.value().indices.data(),
62 static_cast<int>(staging.value().indices.size()));
63 if (!uploaded) return Result<LoadedGraphicsMesh>::failure(uploaded.status());
64 if (const auto authored = staging.value().attributes.find("TANGENT");
65 authored != staging.value().attributes.end()) {
66 const auto count = staging.value().positions.size() / 3;
67 const auto& a = authored->second;
68 auto attach = [&]() -> Result<void> {
69 try {
70 if (a.components != 4 || a.values.size() != count * 4 || staging.value().normals.size() != count * 3)
72 Diagnostic::error(DiagnosticCode::InvalidArgument, "invalid canonical tangent frame"));
73 std::vector<float> tangents(count * 3), bitangents(count * 3);
74 for (size_t i = 0; i < count; ++i) {
75 const auto& normals = staging.value().normals;
76 const float w = a.values[i * 4 + 3];
77 if (w != 1.f && w != -1.f)
79 "tangent handedness must be signed unit"));
80 for (unsigned c = 0; c < 3; ++c) {
81 tangents[i * 3 + c] = a.values[i * 4 + c];
82 const unsigned j = (c + 1) % 3, k = (c + 2) % 3;
83 bitangents[i * 3 + c] =
84 (normals[i * 3 + j] * a.values[i * 4 + k] - normals[i * 3 + k] * a.values[i * 4 + j]) * w;
85 if (!std::isfinite(bitangents[i * 3 + c]))
87 Diagnostic::error(DiagnosticCode::InvalidArgument, "nonfinite canonical bitangent"));
88 }
89 }
90 return factory_.setMeshTangentFrame(uploaded.value(), tangents, bitangents);
91 } catch (const std::bad_alloc&) {
93 Diagnostic::error(DiagnosticCode::Failed, "canonical tangent allocation failed"));
94 }
95 };
96 auto attached = attach();
97 if (!attached) {
98 auto released = factory_.releaseMesh(uploaded.value());
99 if (!released) return Result<LoadedGraphicsMesh>::failure(released.status());
100 return Result<LoadedGraphicsMesh>::failure(attached.status());
101 }
102 }
103 if (preserveAllTexcoords)
104 for (const auto& [set, values] : staging.value().texcoords) {
105 if (set == texcoordSet) continue;
106 auto attached = factory_.setMeshTexcoords(uploaded.value(), set, values);
107 if (!attached) {
108 auto released = factory_.releaseMesh(uploaded.value());
109 if (!released) return Result<LoadedGraphicsMesh>::failure(released.status());
110 return Result<LoadedGraphicsMesh>::failure(attached.status());
111 }
112 }
113 std::vector<uint32_t> available;
114 for (const auto& [set, values] : staging.value().texcoords) available.push_back(set);
115 return Result<LoadedGraphicsMesh>::success({asset, uploaded.value(), std::move(payload).takeValue().variant,
116 texcoordSet, std::move(available),
117 std::move(staging.value().attributes), localBounds});
118}
119
120} // namespace eve::asset_graphics
Value::Object payload
float w
Definition AnimClip.cpp:738
std::string variant
float uv
Transactional graphics consumers for canonical runtime assets.
std::vector< float > normals
std::vector< float > positions
std::int32_t c
JointPoint2D axis
MeleePoint3 a
Definition MeleeHit.cpp:40
std::set< std::string > values
int count
const AssetImportLimits & limits
Stable asset identity backed by PersistentId.
Definition ResourceRef.h:53
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< RuntimeAssetPayload > read(const AssetRef &asset, std::string_view expectedType, const EvpackCapabilities &capabilities, std::uint64_t maximumDecodedBytes) const
Resolve, type-check, variant-select and decode a canonical runtime asset.
Result< LoadedGraphicsMesh > loadMesh(const AssetRef &asset, const asset::EvpackCapabilities &capabilities, const GraphicsAssetLoadLimits &limits={}, std::uint32_t texcoordSet=0, bool preserveAllTexcoords=false) const
Validate and upload one canonical mesh v3 (or compatibility v1/v2) atomically.
virtual Result< void > setMeshTangentFrame(Mesh *, std::span< const float >, std::span< const float >)
Attach authored XYZ tangent and bitangent streams before mesh publication.
virtual Result< void > releaseMesh(Mesh *mesh)=0
Release a mesh previously returned by this factory.
virtual Result< void > setMeshTexcoords(Mesh *, std::uint32_t, std::span< const float >)
Attach an owning UV stream to an uploaded mesh before publication. @thread Graphics thread; input is ...
virtual Result< Mesh * > uploadMesh(const float *posXYZ, const float *nrmXYZ, const float *uvST, int vertexCount, const std::uint32_t *indices, int indexCount)=0
Upload validated, tightly packed mesh arrays.
virtual Result< Mesh * > uploadMeshColored(const float *, const float *, const float *, const float *, int, const std::uint32_t *, int)
Upload a mesh with a tightly packed linear RGBA vertex-color stream.
Result< CanonicalMeshData > decodeCanonicalMesh(std::span< const std::uint8_t > bytes, const CanonicalMeshLimits &limits)
Decode canonical EVMESH binary v1, v2 or v3 into independently owned arrays.
@ TypeMismatch
A stable reference resolved to a different canonical domain type.
Actual runtime device capabilities used for variant selection.
Definition Evpack.h:92
One decoded runtime chunk with its admitted semantic metadata.
std::vector< std::uint8_t > bytes
Bounds applied before allocating canonical mesh staging arrays.