material/codegen.cpp
| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | // SPDX-License-Identifier: BSD-3-Clause | ||
| 2 | #include "chrono_batch_render/material/codegen.h" | ||
| 3 | |||
| 4 | #include <algorithm> | ||
| 5 | #include <cstddef> | ||
| 6 | #include <string_view> | ||
| 7 | |||
| 8 | #include <fmt/format.h> | ||
| 9 | |||
| 10 | #include "chrono_batch_render/util/text.h" | ||
| 11 | |||
| 12 | extern "C" { | ||
| 13 | extern const unsigned char chrono_batch_render_material_dispatch[]; | ||
| 14 | extern const std::size_t chrono_batch_render_material_dispatch_size; | ||
| 15 | extern const unsigned char chrono_batch_render_material_definition[]; | ||
| 16 | extern const std::size_t chrono_batch_render_material_definition_size; | ||
| 17 | extern const unsigned char chrono_batch_render_material_surface[]; | ||
| 18 | extern const std::size_t chrono_batch_render_material_surface_size; | ||
| 19 | extern const unsigned char chrono_batch_render_material_standard[]; | ||
| 20 | extern const std::size_t chrono_batch_render_material_standard_size; | ||
| 21 | extern const unsigned char chrono_batch_render_material_registered[]; | ||
| 22 | extern const std::size_t chrono_batch_render_material_registered_size; | ||
| 23 | } | ||
| 24 | |||
| 25 | namespace chrono::batch_render::material { | ||
| 26 | |||
| 27 | namespace { | ||
| 28 | |||
| 29 | 327 | std::string_view Embedded(const unsigned char* bytes, std::size_t size) { | |
| 30 | 327 | return {reinterpret_cast<const char*>(bytes), size}; | |
| 31 | } | ||
| 32 | |||
| 33 | 430 | std::string Cases(const MaterialDefinitionSet& set, std::string_view standard, | |
| 34 | std::string_view surface) { | ||
| 35 | 430 | std::string cases; | |
| 36 | 470 | for (const MaterialDefinition& definition : set.Definitions()) { | |
| 37 | 40 | const std::string_view call = definition.surface ? surface : standard; | |
| 38 | 40 | cases += fmt::format(" }} else if (context.definition == {}) {{\n {}\n", | |
| 39 | 120 | definition.id, fmt::format(fmt::runtime(call), definition.type)); | |
| 40 | } | ||
| 41 | 430 | return cases; | |
| 42 | ✗ | } | |
| 43 | |||
| 44 | constexpr std::string_view kPlanetaryDefinitions[] = {"Hapke", "LommelSeeliger", "LunarLambert", | ||
| 45 | "OrenNayar"}; | ||
| 46 | |||
| 47 | 430 | std::string PlanetaryCases(bool planetary, std::string_view call) { | |
| 48 | 430 | std::string cases; | |
| 49 | 430 | if (!planetary) { | |
| 50 | return cases; | ||
| 51 | } | ||
| 52 | 50 | for (const std::string_view name : kPlanetaryDefinitions) { | |
| 53 | 40 | cases += | |
| 54 | 80 | fmt::format(" }} else if (context.definition == kDefinition{0}) {{\n {1}\n", | |
| 55 | 120 | name, fmt::format(fmt::runtime(call), name)); | |
| 56 | } | ||
| 57 | 10 | return cases; | |
| 58 | ✗ | } | |
| 59 | |||
| 60 | 215 | std::string Dispatch(const MaterialDefinitionSet& set) { | |
| 61 | 215 | std::string imports; | |
| 62 | 215 | std::string linked; | |
| 63 | 235 | for (const MaterialDefinition& definition : set.Definitions()) { | |
| 64 | 40 | imports += fmt::format("import {};\n", definition.module); | |
| 65 | 40 | linked += fmt::format(" || definition == {}", definition.id); | |
| 66 | } | ||
| 67 | 215 | const bool planetary = set.Planetary(); | |
| 68 | 215 | return FillTemplate( | |
| 69 | Embedded(chrono_batch_render_material_dispatch, chrono_batch_render_material_dispatch_size), | ||
| 70 | {{"imports", imports}, | ||
| 71 | {"planetaryImport", | ||
| 72 | 215 | planetary ? "__exported import " | |
| 73 | "chrono_batch_render.render.kernels.material.definitions.planetary;\n" | ||
| 74 | : ""}, | ||
| 75 | {"planetaryLinked", | ||
| 76 | 215 | planetary ? " || (definition >= kDefinitionHapke && definition <= kDefinitionOrenNayar)" | |
| 77 | : ""}, | ||
| 78 | {"planetaryShade", | ||
| 79 | 215 | PlanetaryCases(planetary, "{}Definition<Spectrum>.Shade(context, closure);")}, | |
| 80 | {"planetaryAlbedo", | ||
| 81 | 430 | PlanetaryCases(planetary, "return {}Definition<Spectrum>.Albedo(context);")}, | |
| 82 | {"linked", linked}, | ||
| 83 | 430 | {"shadeCases", Cases(set, "StandardDefinition<{}, Spectrum>.Shade(context, closure);", | |
| 84 | "{}<Spectrum>.Shade(context, closure);")}, | ||
| 85 | 430 | {"albedoCases", Cases(set, "return StandardDefinition<{}, Spectrum>.Albedo(context);", | |
| 86 | 860 | "return {}<Spectrum>.Albedo(context);")}}); | |
| 87 | 215 | } | |
| 88 | |||
| 89 | 27 | std::string Expression(const ShadingGraph& graph, const GraphLayout& layout, uint32_t node, | |
| 90 | uint32_t input) { | ||
| 91 | 27 | const InputBinding& binding = graph.nodes[node].inputs[input]; | |
| 92 | 27 | const MaterialNodeInput& declared = graph.nodes[node].node->inputs[input]; | |
| 93 | 27 | const bool spectral = declared.type == MaterialInputType::Spectral; | |
| 94 | 27 | if (binding.kind == BindingKind::Pattern) { | |
| 95 | 3 | return spectral ? fmt::format("SpectralValue.Rgb(r{})", binding.source) | |
| 96 | 3 | : fmt::format("r{}", binding.source); | |
| 97 | } | ||
| 98 | 24 | const std::string cell = fmt::format("p.n{}_{}", node, declared.name); | |
| 99 | 24 | if (binding.kind == BindingKind::Texture) { | |
| 100 | 2 | const std::string sampled = | |
| 101 | fmt::format("texels.texels[{0}], texels.Handle({0}), &p.textures[{0}]", | ||
| 102 | 2 | layout.textures[node][input]); | |
| 103 | 2 | if (declared.type == MaterialInputType::Float) { | |
| 104 | ✗ | return fmt::format("TexturedFloat({}, {})", cell, sampled); | |
| 105 | } | ||
| 106 | 2 | const std::string color = fmt::format("TexturedColor({}, {})", cell, sampled); | |
| 107 | 2 | return spectral ? fmt::format("SpectralValue.Rgb({})", color) : color; | |
| 108 | 4 | } | |
| 109 | 22 | if (spectral) { | |
| 110 | 1 | return fmt::format("({0}_spectrum != kNoSpectrum ? SpectralValue.Library({0}_spectrum) : " | |
| 111 | "SpectralValue.Rgb({0}.Vector()))", | ||
| 112 | 1 | cell); | |
| 113 | } | ||
| 114 | 21 | switch (declared.type) { | |
| 115 | 4 | case MaterialInputType::Bool: | |
| 116 | 4 | return cell + " != 0"; | |
| 117 | 7 | case MaterialInputType::Float2: | |
| 118 | 7 | case MaterialInputType::Float3: | |
| 119 | 7 | case MaterialInputType::Float4: | |
| 120 | 7 | return cell + ".Vector()"; | |
| 121 | 10 | default: | |
| 122 | 10 | return cell; | |
| 123 | } | ||
| 124 | 24 | } | |
| 125 | |||
| 126 | 3 | std::string Call(const ShadingGraph& graph, const GraphLayout& layout, uint32_t node) { | |
| 127 | 3 | const MaterialNodeDefinition& definition = *graph.nodes[node].node; | |
| 128 | 3 | std::vector<std::string> arguments; | |
| 129 | 8 | for (const MaterialNodeParameter& parameter : definition.parameters) { | |
| 130 | 5 | switch (parameter.source) { | |
| 131 | 1 | case MaterialNodeParameter::Source::Context: | |
| 132 | 1 | arguments.emplace_back("context"); | |
| 133 | break; | ||
| 134 | ✗ | case MaterialNodeParameter::Source::Point: | |
| 135 | ✗ | arguments.emplace_back("context.point"); | |
| 136 | break; | ||
| 137 | 4 | case MaterialNodeParameter::Source::Input: | |
| 138 | 8 | arguments.push_back(Expression(graph, layout, node, parameter.input)); | |
| 139 | 4 | break; | |
| 140 | } | ||
| 141 | } | ||
| 142 | 3 | return fmt::format(" const {} r{} = {}({});\n", InputInfo(definition.result).slangType, | |
| 143 | 6 | node, definition.name, Join(arguments)); | |
| 144 | 3 | } | |
| 145 | |||
| 146 | 14 | std::string ParamsFields(const ShadingGraph& graph, const GraphLayout& layout) { | |
| 147 | 14 | std::string fields; | |
| 148 | 14 | if (layout.textureCount > 0) { | |
| 149 | 4 | fields += fmt::format(" TextureInput textures[{}];\n", layout.textureCount); | |
| 150 | } | ||
| 151 | 14 | if (layout.mapped) { | |
| 152 | 2 | fields += " MappingRecord mapping;\n"; | |
| 153 | } | ||
| 154 | 14 | if (layout.detailed) { | |
| 155 | ✗ | fields += fmt::format(" DetailRecord detail;\n TextureInput details[{}];\n", | |
| 156 | ✗ | kDetailTextures); | |
| 157 | } | ||
| 158 | 31 | for (uint32_t node = 0; node < graph.nodes.size(); ++node) { | |
| 159 | 44 | for (const MaterialNodeInput& input : graph.nodes[node].node->inputs) { | |
| 160 | 27 | fields += | |
| 161 | 54 | fmt::format(" {} n{}_{};\n", InputInfo(input.type).cellType, node, input.name); | |
| 162 | 27 | if (input.type == MaterialInputType::Spectral) { | |
| 163 | 2 | fields += fmt::format(" uint n{}_{}_spectrum;\n", node, input.name); | |
| 164 | } | ||
| 165 | } | ||
| 166 | } | ||
| 167 | 14 | return fields; | |
| 168 | ✗ | } | |
| 169 | |||
| 170 | 14 | std::string AddImports(const ShadingGraph& graph, MaterialDefinition& module) { | |
| 171 | 14 | std::string imports; | |
| 172 | 31 | for (const ShadingNode& node : graph.nodes) { | |
| 173 | 17 | const MaterialNodeDefinition& definition = *node.node; | |
| 174 | 17 | const std::string file = ModulePath(definition.module); | |
| 175 | 17 | if (module.files.contains(file)) { | |
| 176 | 3 | continue; | |
| 177 | } | ||
| 178 | 28 | imports += fmt::format("import {};\n", definition.module); | |
| 179 | 14 | if (definition.source.empty()) { | |
| 180 | ✗ | continue; | |
| 181 | } | ||
| 182 | 14 | module.files.emplace(file, definition.source); | |
| 183 | 14 | module.origins.emplace_back(file, definition.file); | |
| 184 | 14 | module.origins.emplace_back(definition.module + ".slang", definition.file); | |
| 185 | 17 | } | |
| 186 | 14 | return imports; | |
| 187 | ✗ | } | |
| 188 | |||
| 189 | 14 | std::string Body(const ShadingGraph& graph, const GraphLayout& layout) { | |
| 190 | 14 | const auto surface = uint32_t(graph.nodes.size() - 1); | |
| 191 | 14 | const MaterialNodeDefinition& top = *graph.nodes[surface].node; | |
| 192 | 14 | std::string body; | |
| 193 | 14 | if (layout.detailed) { | |
| 194 | ✗ | body += " SurfacePoint point = context.point;\n" | |
| 195 | " const float3 overlay = MapNodeDetail(context.materials, &p.mapping, " | ||
| 196 | "&p.detail, &p.details[0], point);\n" | ||
| 197 | 14 | " context.point = point;\n"; | |
| 198 | } | ||
| 199 | 14 | if (layout.textureCount > 0) { | |
| 200 | 2 | body += fmt::format(" const SurfaceTexels<{0}> texels = SampleNodeTextures<{0}>(" | |
| 201 | "context, &p.mapping, &p.textures[0]);\n", | ||
| 202 | 4 | layout.textureCount); | |
| 203 | } | ||
| 204 | 17 | for (uint32_t node = 0; node < surface; ++node) { | |
| 205 | 6 | body += Call(graph, layout, node); | |
| 206 | } | ||
| 207 | 28 | body += fmt::format(" {} node;\n", top.name); | |
| 208 | 37 | for (uint32_t input = 0; input < top.inputs.size(); ++input) { | |
| 209 | 23 | const std::string value = Expression(graph, layout, surface, input); | |
| 210 | 23 | const bool overlaid = layout.detailed && top.detailAlbedo == input && | |
| 211 | ✗ | top.inputs[input].type == MaterialInputType::Spectral; | |
| 212 | 46 | body += fmt::format(" node.{} = {};\n", top.inputs[input].name, | |
| 213 | 69 | overlaid ? fmt::format("DetailedAlbedo({}, overlay)", value) : value); | |
| 214 | 23 | } | |
| 215 | 14 | return body; | |
| 216 | ✗ | } | |
| 217 | |||
| 218 | } | ||
| 219 | |||
| 220 | 2 | bool Texturable(MaterialInputType type) { | |
| 221 | 2 | return type == MaterialInputType::Float || type == MaterialInputType::Float3 || | |
| 222 | 2 | type == MaterialInputType::Spectral; | |
| 223 | } | ||
| 224 | |||
| 225 | 29 | GraphLayout LayOut(const ShadingGraph& graph) { | |
| 226 | 29 | GraphLayout layout; | |
| 227 | 64 | for (const ShadingNode& node : graph.nodes) { | |
| 228 | 35 | std::vector<uint32_t>& textures = layout.textures.emplace_back(); | |
| 229 | 90 | for (const InputBinding& binding : node.inputs) { | |
| 230 | 55 | textures.push_back(binding.kind == BindingKind::Texture ? layout.textureCount++ | |
| 231 | : kNoTexture); | ||
| 232 | } | ||
| 233 | } | ||
| 234 | 29 | uint32_t offset = 0; | |
| 235 | 29 | layout.detailed = !graph.nodes.empty() && graph.nodes.back().node->detail; | |
| 236 | 29 | layout.mapped = layout.textureCount > 0 || layout.detailed; | |
| 237 | 29 | if (layout.mapped) { | |
| 238 | 4 | layout.mapping = layout.textureCount * kTextureInputBytes; | |
| 239 | 4 | offset = layout.mapping + kMappingBytes; | |
| 240 | } | ||
| 241 | 29 | if (layout.detailed) { | |
| 242 | ✗ | layout.detail = offset; | |
| 243 | ✗ | layout.detailTextures = offset + kDetailBytes; | |
| 244 | ✗ | offset = layout.detailTextures + kDetailTextures * kTextureInputBytes; | |
| 245 | } | ||
| 246 | 64 | for (const ShadingNode& node : graph.nodes) { | |
| 247 | 35 | std::vector<uint32_t>& cells = layout.cells.emplace_back(); | |
| 248 | 90 | for (const MaterialNodeInput& input : node.node->inputs) { | |
| 249 | 55 | cells.push_back(offset); | |
| 250 | 55 | offset += InputInfo(input.type).bytes; | |
| 251 | } | ||
| 252 | } | ||
| 253 | 29 | layout.bytes = offset; | |
| 254 | 29 | return layout; | |
| 255 | ✗ | } | |
| 256 | |||
| 257 | 15 | graph::ShapeKey MaterialDefinitionKey(const ShadingGraph& graph) { | |
| 258 | 15 | graph::ShapeWriter writer; | |
| 259 | 15 | writer.Add(uint32_t(graph.nodes.size())); | |
| 260 | 33 | for (const ShadingNode& node : graph.nodes) { | |
| 261 | 18 | writer.Add(node.node->module); | |
| 262 | 18 | writer.Add(node.node->name); | |
| 263 | 18 | writer.Add(uint32_t(node.inputs.size())); | |
| 264 | 46 | for (const InputBinding& binding : node.inputs) { | |
| 265 | 28 | writer.Add(uint32_t(binding.kind)); | |
| 266 | 28 | if (binding.kind == BindingKind::Pattern) { | |
| 267 | 3 | writer.Add(binding.source); | |
| 268 | } | ||
| 269 | } | ||
| 270 | } | ||
| 271 | 15 | return std::move(writer).Finish(); | |
| 272 | 15 | } | |
| 273 | |||
| 274 | 14 | MaterialDefinition GenerateMaterialDefinition(uint32_t id, const ShadingGraph& graph) { | |
| 275 | 14 | const GraphLayout layout = LayOut(graph); | |
| 276 | 14 | const MaterialNodeDefinition& top = *graph.nodes.back().node; | |
| 277 | 14 | MaterialDefinition module{.id = id, | |
| 278 | .type = fmt::format("Definition{}", id), | ||
| 279 | .module = | ||
| 280 | fmt::format("chrono_batch_render.material.generated.d{}", id), | ||
| 281 | 14 | .words = layout.bytes / uint32_t(sizeof(uint32_t)), | |
| 282 | 14 | .surface = top.role == MaterialNodeRole::Surface}; | |
| 283 | 14 | const std::string& type = module.type; | |
| 284 | 14 | const std::string imports = AddImports(graph, module); | |
| 285 | 14 | const std::string fields = ParamsFields(graph, layout); | |
| 286 | 14 | const std::string body = Body(graph, layout); | |
| 287 | 14 | const std::string definition = | |
| 288 | 3 | FillTemplate(module.surface ? Embedded(chrono_batch_render_material_surface, | |
| 289 | chrono_batch_render_material_surface_size) | ||
| 290 | 11 | : Embedded(chrono_batch_render_material_standard, | |
| 291 | chrono_batch_render_material_standard_size), | ||
| 292 | 28 | {{"type", type}, {"body", body}}); | |
| 293 | 14 | module.source = FillTemplate(Embedded(chrono_batch_render_material_definition, | |
| 294 | chrono_batch_render_material_definition_size), | ||
| 295 | 14 | {{"node", top.name}, | |
| 296 | {"imports", imports}, | ||
| 297 | {"type", type}, | ||
| 298 | {"fields", fields}, | ||
| 299 | 14 | {"definition", definition}}); | |
| 300 | 14 | return module; | |
| 301 | 28 | } | |
| 302 | |||
| 303 | std::map<std::string, std::string, std::less<>> | ||
| 304 | 351 | ClosurePrimitiveSources(std::span<const std::shared_ptr<const ClosurePrimitive>> primitives) { | |
| 305 | 351 | std::map<std::string, std::string, std::less<>> sources; | |
| 306 | 351 | if (primitives.empty()) { | |
| 307 | return sources; | ||
| 308 | } | ||
| 309 | 84 | std::string imports; | |
| 310 | 84 | std::string kinds; | |
| 311 | 84 | std::string glossy; | |
| 312 | 84 | std::string estimate; | |
| 313 | 84 | std::string evaluate; | |
| 314 | 84 | std::string pdf; | |
| 315 | 84 | std::string direction; | |
| 316 | 169 | for (const std::shared_ptr<const ClosurePrimitive>& primitive : primitives) { | |
| 317 | 170 | sources[ModulePath(primitive->module)] = primitive->source; | |
| 318 | 170 | imports += fmt::format("import {};\n", primitive->module); | |
| 319 | 170 | kinds += fmt::format("static const uint kLobe{} = {};\n", primitive->name, primitive->kind); | |
| 320 | 510 | const auto add = [&](std::string& cases, std::string_view call) { | |
| 321 | 850 | cases += fmt::format(" case {}:\n return {}.{};\n", primitive->kind, | |
| 322 | 850 | primitive->name, call); | |
| 323 | 425 | }; | |
| 324 | 85 | add(glossy, "Glossy()"); | |
| 325 | 85 | add(estimate, "Estimate<Spectrum>(tint, alpha, params.values, outgoing)"); | |
| 326 | 85 | add(evaluate, "Evaluate<Spectrum>(tint, alpha, params.values, outgoing, incoming)"); | |
| 327 | 85 | add(pdf, "Pdf(alpha, params.values, outgoing, incoming)"); | |
| 328 | 85 | add(direction, "Direction(alpha, params.values, outgoing, u)"); | |
| 329 | } | ||
| 330 | 252 | sources[kRegisteredFile] = FillTemplate(Embedded(chrono_batch_render_material_registered, | |
| 331 | chrono_batch_render_material_registered_size), | ||
| 332 | {{"imports", imports}, | ||
| 333 | 84 | {"firstLobe", std::to_string(kFirstPrimitiveLobe)}, | |
| 334 | {"kinds", kinds}, | ||
| 335 | {"glossy", glossy}, | ||
| 336 | {"estimate", estimate}, | ||
| 337 | {"evaluate", evaluate}, | ||
| 338 | {"pdf", pdf}, | ||
| 339 | 84 | {"direction", direction}}); | |
| 340 | 84 | return sources; | |
| 341 | 84 | } | |
| 342 | |||
| 343 | std::map<std::string, std::string, std::less<>> | ||
| 344 | 215 | MaterialDispatchSources(const MaterialDefinitionSet& set) { | |
| 345 | 215 | std::map<std::string, std::string, std::less<>> sources(set.Files().begin(), set.Files().end()); | |
| 346 | 215 | sources[kDispatchFile] = Dispatch(set); | |
| 347 | 215 | if (set.Planetary()) { | |
| 348 | 5 | sources[kLobesFile] = "static const bool kPhotometricLobes = true;\n"; | |
| 349 | } | ||
| 350 | 235 | for (const MaterialDefinition& definition : set.Definitions()) { | |
| 351 | 40 | sources[ModulePath(definition.module)] = definition.source; | |
| 352 | 39 | for (const auto& [path, source] : definition.files) { | |
| 353 | 38 | sources[path] = source; | |
| 354 | } | ||
| 355 | } | ||
| 356 | 215 | return sources; | |
| 357 | ✗ | } | |
| 358 | |||
| 359 | 669 | MaterialDefinitionSet::MaterialDefinitionSet(std::vector<MaterialDefinition> definitions, | |
| 360 | std::map<std::string, std::string, std::less<>> files, | ||
| 361 | 669 | bool planetary) | |
| 362 | 669 | : m_definitions(std::move(definitions)), m_files(std::move(files)), m_planetary(planetary), | |
| 363 | 669 | m_hash(kFnvBasis) { | |
| 364 | 1322 | m_hash = Fnv1a(planetary ? "planetary" : "", m_hash); | |
| 365 | 811 | for (const auto& [path, source] : m_files) { | |
| 366 | 142 | m_hash = Fnv1a(path, m_hash); | |
| 367 | 142 | m_hash = Fnv1a(source, m_hash); | |
| 368 | } | ||
| 369 | 669 | std::ranges::sort(m_definitions, {}, &MaterialDefinition::id); | |
| 370 | 690 | for (const MaterialDefinition& definition : m_definitions) { | |
| 371 | 21 | m_hash = Fnv1a(std::to_string(definition.id), m_hash); | |
| 372 | 21 | m_hash = Fnv1a(definition.type, m_hash); | |
| 373 | 21 | m_hash = Fnv1a(definition.source, m_hash); | |
| 374 | 41 | for (const auto& [path, source] : definition.files) { | |
| 375 | 20 | m_hash = Fnv1a(path, m_hash); | |
| 376 | 20 | m_hash = Fnv1a(source, m_hash); | |
| 377 | } | ||
| 378 | } | ||
| 379 | 669 | } | |
| 380 | |||
| 381 | 8 | bool MaterialDefinitionSet::Contains(uint32_t id) const { | |
| 382 | 8 | if (id >= kFirstPlanetaryDefinition && id < kFirstGeneratedDefinition) { | |
| 383 | 3 | return m_planetary; | |
| 384 | } | ||
| 385 | 8 | return id < kFirstGeneratedDefinition || | |
| 386 | 3 | std::ranges::binary_search(m_definitions, id, {}, &MaterialDefinition::id); | |
| 387 | } | ||
| 388 | |||
| 389 | } | ||
| 390 |