载入中...
搜索中...
未找到
ShaderAsset.cpp
浏览该文件的文档.
1#include "asset/ShaderAsset.h"
2#include <cmath>
3#include <limits>
4#include <set>
7
8// ShaderAsset.cpp
9namespace eve::asset {
10namespace {
11Result<std::vector<std::uint32_t>> stage(const Value& value, std::uint32_t model, const ShaderAssetLimits& limits) {
12 const auto* input = value.getIf<Value::Array>();
13 if (!input || input->size() < 5 || input->size() > limits.maximumStageWords)
14 return Result<std::vector<std::uint32_t>>::failure(
15 Diagnostic::error(DiagnosticCode::InvalidArgument, "Shader stage size is invalid", {}, {}, "asset.shader"));
16 std::vector<std::uint32_t> words;
17 words.reserve(input->size());
18 for (const auto& word : *input) {
19 if (!word.isInt64() || word.asInt() < 0 || word.asInt() > std::numeric_limits<std::uint32_t>::max())
20 return Result<std::vector<std::uint32_t>>::failure(
21 Diagnostic::error(DiagnosticCode::InvalidArgument, "Shader stage requires unsigned 32-bit words", {},
22 {}, "asset.shader"));
23 words.push_back(static_cast<std::uint32_t>(word.asInt()));
24 }
25 if (words[0] != 0x07230203 || words[1] < 0x00010000 || words[1] > 0x00010600 || (words[1] & 0xff) != 0 ||
26 words[3] == 0 || words[4] != 0)
27 return Result<std::vector<std::uint32_t>>::failure(
28 Diagnostic::error(DiagnosticCode::InvalidArgument, "Unsupported SPIR-V header", {}, {}, "asset.shader"));
29 bool main = false;
30 for (std::size_t cursor = 5; cursor < words.size();) {
31 const auto count = words[cursor] >> 16;
32 const auto opcode = words[cursor] & 0xffff;
33 if (count == 0 || count > words.size() - cursor)
34 return Result<std::vector<std::uint32_t>>::failure(Diagnostic::error(
35 DiagnosticCode::InvalidArgument, "Truncated SPIR-V instruction", {}, {}, "asset.shader"));
36 if (opcode == 15) {
37 if (count < 4)
38 return Result<std::vector<std::uint32_t>>::failure(Diagnostic::error(
39 DiagnosticCode::InvalidArgument, "Invalid SPIR-V entry point", {}, {}, "asset.shader"));
40 if (count >= 5 && words[cursor + 3] == 0x6e69616d && words[cursor + 4] == 0) {
41 if (main || words[cursor + 1] != model || words[cursor + 2] == 0 || words[cursor + 2] >= words[3])
42 return Result<std::vector<std::uint32_t>>::failure(Diagnostic::error(
43 DiagnosticCode::InvalidArgument, "Incompatible main entry point", {}, {}, "asset.shader"));
44 main = true;
45 }
46 }
47 cursor += count;
48 }
49 if (!main)
50 return Result<std::vector<std::uint32_t>>::failure(Diagnostic::error(
51 DiagnosticCode::InvalidArgument, "SPIR-V main entry point is missing", {}, {}, "asset.shader"));
52 return Result<std::vector<std::uint32_t>>::success(std::move(words));
53}
54} // namespace
55
57 const auto* fields = definition.getIf<Value::Object>();
58 if (!fields || fields->size() != 7)
60 "Shader definition requires exactly seven known fields",
61 {}, {}, "asset.shader"));
62 for (const auto name : {"schema", "schemaVersion", "interface", "format", "vertex", "fragment", "parameters"})
63 if (!fields->contains(name))
65 DiagnosticCode::InvalidArgument, "Shader field is missing or unknown", {}, {}, "asset.shader"));
66 if (!fields->at("schema").isString() || fields->at("schema").asString() != "eve.shader" ||
67 !fields->at("schemaVersion").isInt64() || fields->at("schemaVersion").asInt() != 1)
69 DiagnosticCode::UnknownVersion, "Unsupported shader schema/version", {}, {}, "asset.shader"));
70 if (!fields->at("format").isString() || fields->at("format").asString() != "spirv-1.6" ||
71 !fields->at("interface").isString())
73 DiagnosticCode::InvalidArgument, "Unsupported shader format/interface", {}, {}, "asset.shader"));
75 const auto& interface = fields->at("interface").asString();
76 if (interface == "sprite2d")
78 else if (interface != "mesh3d")
80 Diagnostic::error(DiagnosticCode::InvalidArgument, "Unsupported shader interface", {}, {}, "asset.shader"));
81 const auto* parameters = fields->at("parameters").getIf<Value::Array>();
82 if (!parameters || parameters->size() > 32)
84 DiagnosticCode::InvalidArgument, "Invalid shader parameter count", {}, {}, "asset.shader"));
85 std::set<std::string> names;
86 std::size_t slots = 0;
87 for (const auto& parameter : *parameters) {
88 const auto* object = parameter.getIf<Value::Object>();
89 if (!object || object->size() != 2 || !object->contains("name") || !object->contains("default") ||
90 !object->at("name").isString())
92 Diagnostic::error(DiagnosticCode::InvalidArgument, "Invalid parameter fields", {}, {}, "asset.shader"));
93 const auto& name = object->at("name").asString();
94 const auto* defaults = object->at("default").getIf<Value::Array>();
95 if (name.empty() || name.size() > 128 || name.find('\0') != std::string::npos || !names.insert(name).second ||
96 !defaults || defaults->empty() || defaults->size() > 4 || (slots += defaults->size()) > 32)
98 "Invalid parameter name or push-constant capacity",
99 {}, {}, "asset.shader"));
101 for (const auto& value : *defaults) {
102 if (!value.isDouble() && !value.isInt64())
104 Diagnostic::error(DiagnosticCode::InvalidArgument, "Non-numeric default", {}, {}, "asset.shader"));
105 const double number = value.isDouble() ? value.asDouble() : static_cast<double>(value.asInt());
106 if (!std::isfinite(number) || std::abs(number) > std::numeric_limits<float>::max())
108 DiagnosticCode::InvalidArgument, "Non-finite float default", {}, {}, "asset.shader"));
109 output.defaults.push_back(static_cast<float>(number));
110 }
111 result.parameters.push_back(std::move(output));
112 }
113 auto vertex = stage(fields->at("vertex"), 0, limits);
114 if (!vertex) return Result<ShaderAsset>::failure(vertex.status());
115 auto fragment = stage(fields->at("fragment"), 4, limits);
116 if (!fragment) return Result<ShaderAsset>::failure(fragment.status());
117 result.vertex = std::move(vertex).takeValue();
118 result.fragment = std::move(fragment).takeValue();
119 return Result<ShaderAsset>::success(std::move(result));
120}
121} // namespace eve::asset
122
123// ShaderAssetLoader.cpp
124namespace eve::asset {
126 const EvpackCapabilities& capabilities) {
127 if (capabilities.graphics != "vulkan")
129 Diagnostic::error(DiagnosticCode::Unsupported, "eve.shader/1 requires Vulkan", {}, {}, "asset.shader"));
130 auto payload = reader.read(asset, "eve.shader/1", capabilities, 8 * 1024 * 1024);
131 if (!payload) return Result<ShaderAsset>::failure(payload.status());
132 if (payload.value().chunks.size() != 1 || payload.value().chunks.front().kind != EvpackChunkKind::Definition)
134 "eve.shader/1 requires one self-contained definition", {},
135 {}, "asset.shader"));
137 limits.maximumBytes = 8 * 1024 * 1024;
138 limits.maximumValues = 600000;
139 auto definition = decodeRuntimeDefinition(payload.value().chunks.front().bytes, limits);
140 if (!definition) return Result<ShaderAsset>::failure(definition.status());
141 return decodeShaderAsset(definition.value());
142}
143} // namespace eve::asset
Value::Object payload
std::string output
std::size_t cursor
std::vector< Value > defaults
Definition ECS.cpp:49
std::string name
Definition ECS.cpp:154
std::vector< std::string > fields
Definition ECS.cpp:42
Typed capability-aware runtime access to admitted packs.
EvpackChunkInput input
Definition Evpack.cpp:170
Poco::JSON::Object::Ptr result
std::array< uint32_t, 9 > parameters
Object::Ptr value
std::string parameter
glm::mat4 model
double number
JSON-free versioned runtime metadata codec.
LocalPageCacheEntry slots[ShadowConfig::kLocalSlots]
Definition Shadow.cpp:159
int count
Json object
const AssetImportLimits & limits
std::size_t at
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
The canonical owning dynamic value used by data-facing protocols.
Definition Value.h:31
std::map< std::string, Value > Object
Definition Value.h:34
std::vector< Value > Array
Definition Value.h:33
Immutable reader retaining an admitted pack by shared ownership.
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.
std::string word
int main(int argc, char **argv)
Definition main.cpp:46
Result< ShaderAsset > loadShaderAsset(const EvpackResourceReader &reader, const AssetRef &asset, const EvpackCapabilities &capabilities)
Read and decode one capability-selected eve.shader/1 from an admitted runtime package.
Result< Value > decodeRuntimeDefinition(std::span< const std::uint8_t > bytes, const RuntimeDefinitionLimits &limits)
Decode a bounded EVDEF\0\1 metadata tree without a JSON parser.
Result< ShaderAsset > decodeShaderAsset(const Value &definition, const ShaderAssetLimits &limits)
Decode strict eve.shader/1 metadata with SPIR-V 1.0 through 1.6 stages.
Actual runtime device capabilities used for variant selection.
Definition Evpack.h:92
Structural and allocation limits for untrusted EVDEF metadata.
Admission bounds checked before copying stage words.
Definition ShaderAsset.h:34
One sequential float/vector push-constant parameter, owned by the definition.
Definition ShaderAsset.h:20
Owning CPU program snapshot; decoding does not prove GPU/descriptor compatibility.
Definition ShaderAsset.h:26
ShaderAssetInterface interface
Definition ShaderAsset.h:27