载入中...
搜索中...
未找到
PbrVariantPlan.cpp
浏览该文件的文档.
2
3#include <bit>
4#include <tuple>
5#include <utility>
6#include <vector>
7
8namespace eve::graphics::webgpu {
9namespace {
10using SamplerKey = std::tuple<std::uint32_t, std::uint32_t, std::uint32_t, std::uint32_t>;
11
12SamplerKey samplerKey(const PbrTextureBinding& binding) {
13 return {binding.wrapS, binding.wrapT, binding.minFilter, binding.magFilter};
14}
15
16std::uint32_t addSampler(std::vector<std::pair<SamplerKey, std::uint32_t>>& samplers,
17 const SamplerKey& key, std::uint32_t resource) {
18 for (const auto& [existing, representative] : samplers)
19 if (existing == key) return representative;
20 samplers.emplace_back(key, resource);
21 return resource;
22}
23} // namespace
24
25Result<PbrVariantPlan> planPbrVariant(const PbrSurface& surface, bool environment, bool shadows,
26 const PbrVariantLimits& limits) {
27 auto valid = validatePbrSurface(surface);
28 if (!valid) return Result<PbrVariantPlan>::failure(valid.status());
29
30 PbrVariantPlan plan;
31 for (std::uint32_t slot = 0; slot < std::uint32_t(PbrTextureSlot::Count); ++slot)
32 if (surface.textures[slot].texture) plan.canonicalTextureMask |= 1u << slot;
33
34 if (surface.vegetationDetail.value > 0.f) {
35 // The TVE detail stage always evaluates albedo and normal through neutral
36 // fallbacks. Its packed mask is reachable only when authored.
37 plan.detailTextureMask = 0b011u;
38 if (surface.vegetationDetail.textures[2].texture) plan.detailTextureMask |= 0b100u;
39 }
40 if (environment) plan.fragmentFlags |= PbrEnvironment;
41 if (shadows) plan.fragmentFlags |= PbrShadows;
45
52 }
53
54 plan.fragmentTextures = std::popcount(plan.canonicalTextureMask) + std::popcount(plan.detailTextureMask) +
55 std::popcount(plan.fragmentFlags);
56 std::vector<std::pair<SamplerKey, std::uint32_t>> fragmentSamplers;
57 for (std::uint32_t slot = 0; slot < std::uint32_t(PbrTextureSlot::Count); ++slot)
58 if ((plan.canonicalTextureMask & (1u << slot)) != 0u) {
60 addSampler(fragmentSamplers, samplerKey(surface.textures[slot]), slot);
61 }
62 for (std::uint32_t slot = 0; slot < surface.vegetationDetail.textures.size(); ++slot)
63 if ((plan.detailTextureMask & (1u << slot)) != 0u) {
64 const auto resource = std::uint32_t(PbrTextureSlot::Count) + slot;
66 addSampler(fragmentSamplers, samplerKey(surface.vegetationDetail.textures[slot]), resource);
67 }
68 // These fixed-function resources currently have distinct backend sampler
69 // contracts; a later layout may merge them only after proving equivalence.
70 plan.fragmentSamplers = std::uint32_t(fragmentSamplers.size()) + std::popcount(plan.fragmentFlags);
71 plan.vertexTextures = std::popcount(plan.vertexFlags);
73
74 if (plan.fragmentTextures > limits.sampledTexturesPerStage ||
75 plan.fragmentSamplers > limits.samplersPerStage || plan.vertexTextures > limits.sampledTexturesPerStage ||
76 plan.vertexSamplers > limits.samplersPerStage)
78 DiagnosticCode::Unsupported, "WebGPU PBR material exceeds per-stage texture or sampler limits"));
80}
81
82} // namespace eve::graphics::webgpu
std::uint32_t key
float u
Definition Grass.cpp:233
bool valid
eve::action::ActionVfxBinding binding
std::string resource
const AssetImportLimits & limits
static Diagnostic error(DiagnosticCode code, std::string message, std::string path={}, DiagnosticDetails details={}, std::string source={})
Construct an error diagnostic with the standard error severity.
Definition Diagnostic.h:125
Move-only operation result carrying either a value or Status.
Definition Result.h:155
static Result success(T value)
Construct a successful result owning value.
Definition Result.h:164
static Result failure(Status status)
Construct a failed result from a structured status.
Definition Result.h:175
Result< PbrVariantPlan > planPbrVariant(const PbrSurface &surface, bool environment, bool shadows, const PbrVariantLimits &limits)
Plan only resources reachable for an immutable PBR draw snapshot.
Result< void > validatePbrSurface(const PbrSurface &s)
Validate finite material factors and sampler enums without changing the input.
Definition PbrSurface.cpp:4
Owning material parameter snapshot; texture pointers remain borrowed. Base color/metallic/roughness c...
Definition PbrSurface.h:218
PbrVegetationMotion vegetationMotion
Definition PbrSurface.h:259
PbrVegetationExtras vegetationExtras
Definition PbrSurface.h:256
std::array< PbrTextureBinding, std::size_t(PbrTextureSlot::Count)> textures
Definition PbrSurface.h:219
PbrVegetationVertex vegetationVertex
Definition PbrSurface.h:258
PbrVegetationColors vegetationColors
Definition PbrSurface.h:257
PbrVegetationDetail vegetationDetail
Definition PbrSurface.h:255
PbrVegetationAlpha vegetationAlpha
Definition PbrSurface.h:260
std::array< PbrTextureBinding, 3 > textures
Definition PbrSurface.h:104
PbrVegetationMotionMode mode
Definition PbrSurface.h:187
PbrVegetationDeformationSource source
Definition PbrSurface.h:159
Device resource limits relevant to one extended PBR shader variant.
Stable resource-layout key and checked stage counts for a PBR snapshot.
std::array< std::uint32_t, 11 > canonicalSamplerRepresentatives
std::array< std::uint32_t, 3 > detailSamplerRepresentatives