载入中...
搜索中...
未找到
PathSceneCapture.cpp
浏览该文件的文档.
2
3#include "common/Diagnostic.h"
4#include "common/Profile.h"
6#include "graphics/Light.h"
9
10#include <array>
11#include <bit>
12#include <unordered_map>
13#include <vector>
14
15namespace eve::graphics {
16namespace {
17struct TextureKey {
18 Texture* texture;
19 std::array<uint32_t, 9> parameters;
20 bool operator==(const TextureKey&) const = default;
21};
22struct TextureHash {
23 size_t operator()(const TextureKey& key) const {
24 auto hash = std::hash<Texture*>{}(key.texture);
25 for (auto value : key.parameters) hash ^= size_t(value) + size_t(0x9e3779b9u) + (hash << 6) + (hash >> 2);
26 return hash;
27 }
28};
29struct GeometryKey {
30 Mesh* mesh;
31 std::array<uint32_t, 16> material;
32 bool operator==(const GeometryKey&) const = default;
33};
34struct GeometryHash {
35 size_t operator()(const GeometryKey& key) const {
36 auto hash = std::hash<Mesh*>{}(key.mesh);
37 for (auto value : key.material) hash ^= size_t(value) + size_t(0x9e3779b9u) + (hash << 6) + (hash >> 2);
38 return hash;
39 }
40};
41GeometryKey geometryKey(Mesh* mesh, const PathMaterial& material) {
42 const auto bits = [](float value) { return std::bit_cast<uint32_t>(value == 0.f ? 0.f : value); };
43 return {mesh,
44 {bits(material.baseColor.x), bits(material.baseColor.y), bits(material.baseColor.z),
45 bits(material.emission.x), bits(material.emission.y), bits(material.emission.z), bits(material.roughness),
46 bits(material.metallic), material.baseColorTexture, bits(material.opacity), bits(material.alphaCutoff),
47 uint32_t(material.alphaMode), material.metallicRoughnessTexture, material.emissionTexture,
48 material.normalTexture, bits(material.normalScale)}};
49}
50} // namespace
52 EV_PROFILE_MODULE("graphics", "Path.WorldCapture");
53 const auto unsupported = [](const char* message) {
56 };
57 std::vector<PathGeometry> geometry;
58 std::vector<PathMaterial> materials;
59 std::vector<PathInstance> instances;
60 std::vector<PathLight> lights;
61 std::vector<PathColorTexture> textures;
62 std::unordered_map<TextureKey, uint32_t, TextureHash> textureIndices;
63 std::unordered_map<Material*, PathMaterial> materialValues;
64 const auto registerTexture = [&](const PathColorTexture& input) {
65 const auto bits = [](float value) { return std::bit_cast<uint32_t>(value == 0.f ? 0.f : value); };
66 TextureKey key{input.sourceTexture,
67 {uint32_t(input.srgbDecode), bits(input.offset.x), bits(input.offset.y), bits(input.scale.x),
68 bits(input.scale.y), bits(input.rotation), uint32_t(input.filter), uint32_t(input.addressU),
69 uint32_t(input.addressV)}};
70 auto [entry, inserted] = textureIndices.try_emplace(key, uint32_t(textures.size()));
71 if (inserted) textures.push_back(input);
72 return entry->second;
73 };
74 const auto registerBasicTexture = [&](Texture* texture, bool srgbDecode) {
75 if (!texture) return UINT32_MAX;
78 input.srgbDecode = srgbDecode;
79 return registerTexture(input);
80 };
81 // Keys live only during capture. Equal material values share geometry even
82 // when authored through distinct Material objects or legacy component fields.
83 std::unordered_map<GeometryKey, uint32_t, GeometryHash> geometryIndices;
84 // Scope: Renderable3D descendants with Transform3D+MeshRenderer, and Light3D
85 // descendants with Data. Read only; no structural mutation/events/RNG/time.
86 // Phase: explicit preparation before frame recording. Service: IRayTracing.
87 auto renderables = ecs::View<Renderable3D, Renderable3D::Transform3D, Renderable3D::MeshRenderer>(world);
88 for (auto it = renderables.begin(); it != renderables.end(); ++it) {
89 auto [transform, renderer] = *it;
90 if (!renderer->visible) continue;
91 if (renderer->instances || renderer->isHair)
92 return unsupported("Path world requires supported instance and hair geometry");
93 const int count = renderer->partCount > 0 ? renderer->partCount : 1;
95 return unsupported("Path world part count exceeds renderer storage");
96 for (int part = 0; part < count; ++part) {
97 auto* mesh =
98 renderer->partCount > 0
99 ? renderer->parts[part].mesh
100 : (renderer->lodCount > 0 && renderer->lodMeshes[0] ? renderer->lodMeshes[0] : renderer->mesh);
101 if (!mesh) continue;
102 auto* material = renderer->partCount > 0 && renderer->parts[part].material ? renderer->parts[part].material
103 : renderer->material;
105 if (material && materialValues.contains(material)) {
106 value = materialValues.at(material);
107 } else if (material) {
108 if (material->virtualTextureMode() != MaterialVirtualTextureMode::Conventional ||
109 material->getShader() || material->getHeightTexture() ||
110 (material->surfaceMode() == SurfaceMode::Transparent &&
111 material->blendMode() != BlendMode::Alpha) ||
112 material->getShadingModel() != "pbr")
113 return unsupported("Path world material conversion is unavailable for this surface");
114 value.baseColor = {material->getTintR(), material->getTintG(), material->getTintB()};
115 value.opacity = material->getTintA();
116 value.alphaCutoff = material->getAlphaCutoff();
117 value.alphaMode = material->surfaceMode() == SurfaceMode::Masked ? PathAlphaMode::Masked
120 value.roughness = material->getRoughness();
121 value.metallic = material->getMetallic();
122 if (material->hasPbrSurface()) {
123 auto pbr = material->pbrSurface();
124 value.emission =
125 glm::vec3(pbr.emissive[0], pbr.emissive[1], pbr.emissive[2]) * pbr.emissiveStrength;
126 pbr.emissive = {0.f, 0.f, 0.f};
127 pbr.emissiveStrength = 1.f;
128 value.normalScale = pbr.normalScale;
129 pbr.normalScale = 1.f;
132 auto& binding = pbr.textures[size_t(slot)];
133 if (binding.texture) {
134 if (binding.texcoord != 0)
135 return unsupported(
136 "Path world material requires UV0; additional UV sets are not captured yet");
138 input.sourceTexture = binding.texture;
139 input.srgbDecode = binding.srgbDecode;
140 input.offset = {binding.offset[0], binding.offset[1]};
141 input.scale = {binding.scale[0], binding.scale[1]};
142 input.rotation = binding.rotation;
143 input.filter =
145 const auto address = [](uint32_t wrap) {
146 return wrap == 10497
149 };
150 input.addressU = address(binding.wrapS);
151 input.addressV = address(binding.wrapT);
152 auto index = registerTexture(input);
153 if (slot == PbrTextureSlot::BaseColor) value.baseColorTexture = index;
154 if (slot == PbrTextureSlot::MetallicRoughness) value.metallicRoughnessTexture = index;
155 if (slot == PbrTextureSlot::Emissive) value.emissionTexture = index;
156 if (slot == PbrTextureSlot::Normal) value.normalTexture = index;
157 }
158 binding = {};
159 }
160 // Defaulted value comparison also covers newly added extension fields:
161 // an unimplemented non-default extension must never disappear silently.
162 if (!(pbr == PbrSurface{})) return unsupported("Path world PBR extension is not yet supported");
163 } else {
164 value.baseColorTexture = registerBasicTexture(material->getAlbedoTexture(), true);
165 value.normalTexture = registerBasicTexture(material->getNormalTexture(), false);
166 }
167 materialValues.emplace(material, value);
168 } else {
169 if (renderer->heightTexture || renderer->shader || !renderer->receiveLight)
170 return unsupported("Path world legacy material conversion is unavailable for this surface");
171 value.baseColor = {renderer->r, renderer->g, renderer->b};
172 value.opacity = renderer->a;
174 value.roughness = renderer->roughness;
175 value.metallic = renderer->metallic;
176 value.baseColorTexture = registerBasicTexture(renderer->texture, true);
177 value.normalTexture = registerBasicTexture(renderer->normalTexture, false);
178 }
179 const auto [entry, inserted] =
180 geometryIndices.try_emplace(geometryKey(mesh, value), uint32_t(geometry.size()));
181 if (inserted) {
184 input.material = uint32_t(materials.size());
185 geometry.push_back(input);
186 materials.push_back(value);
187 }
188 instances.push_back({entry->second, detail::modelFromTransform(*transform)});
189 }
190 }
191 auto lightView = ecs::View<Light3D, Light3D::Data>(world);
192 for (auto it = lightView.begin(); it != lightView.end(); ++it) {
193 auto [light] = *it;
194 if (!light->enabled) continue;
196 (light->type != "point" && light->type != "dir" && light->type != "spot"))
197 return unsupported("Path world light conversion requires point, directional or spot surface lights");
200 if (light->type == "spot") {
202 value.beamDirection = {light->dx, light->dy, light->dz};
204 value.cosInner = std::min(value.cosInner, 1.f);
205 }
206 value.position = {light->x, light->y, light->z};
207 value.directionToLight = {light->dx, light->dy, light->dz};
208 value.strength = glm::vec3(light->r, light->g, light->b) * light->intensity;
209 lights.push_back(value);
210 }
211 return provider.preparePathScene(&graphics, {geometry, instances, materials, lights, textures});
212}
213} // namespace eve::graphics
Stable, structured diagnostics shared by engine modules.
SQObjectPtr key
Definition ECS.cpp:155
EvpackChunkInput input
Definition Evpack.cpp:170
std::string message
std::vector< eve::ProcgenProbeDesc > lights
graphics::PbrSurface pbr
OceanMeshSettings mesh
std::shared_ptr< OceanRenderer > renderer
eve::action::ActionVfxBinding binding
std::array< uint32_t, 16 > material
std::array< uint32_t, 9 > parameters
Texture * texture
#define EV_PROFILE_MODULE(module, name)
Profile the enclosing scope, tagged with a module for grouping.
Definition Profile.h:158
Object::Ptr value
std::string hash
std::string slot
std::string geometry
Graphics * graphics
Light3D::Data * light
Definition Shadow.cpp:147
int count
uint32_t index
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 failure(Status status)
Construct a failed result from a structured status.
Definition Result.h:196
Optional hardware ray-tracing backend (Vulkan KHR RT).
Definition IRayTracing.h:28
GPU texture created via Graphics::newTexture. Owns GPU resources through an opaque backend handle.
Definition Texture.h:15
glm::mat4 modelFromTransform(const Renderable3D::Transform3D &t)
Resolve the value-owned scene world matrix or the local TRS on the render thread.
卡牌游戏 UI 工具模块:工厂 + 脚本绑定入口。 功能参考 ycarowr/UiCard:扇形手牌布局、抽牌/洗牌、悬浮放大、拖拽到落牌区、 敌方手牌(背面/偷看)、费用不足置灰,以及可实时调节的布局...
Definition Animation.h:25
void lightSpotCosines(float angleDeg, float softness, float &cosOuter, float &cosInner)
Convert spot angle/softness into GPU cosines (outer < inner). Shared by Light2D and Light3D flashligh...
Definition Light.h:27
Result< PathSceneToken > preparePathWorld(ecs::Table &world, Graphics &graphics, IRayTracing &provider)
Prepare an unculled path scene from the supplied ECS world.
float spotSoftness
Spot penumbra in [0, 1] (ignored for point/dir).
Definition Light.h:209
float spotAngleDeg
Spot outer half-angle in degrees (ignored for point/dir).
Definition Light.h:207
bool volumetricOnly
When true with volumetric, skip mesh/PBR surface lighting and only contribute to volumetric shafts / ...
Definition Light.h:232
Base-level color or material-data texture from linear pixels or an owned engine RGBA8 texture....
Definition PathTracing.h:55
Indexed geometry; spans are borrowed only during scene preparation. @ownership Caller owns all spans ...
Definition PathTracing.h:75
World-space analytic light copied during scene preparation.
Linear-light surface parameters for path tracing.
Definition PathTracing.h:20
Owning material parameter snapshot; texture pointers remain borrowed. Base color/metallic/roughness c...
Definition PbrSurface.h:240