material/codegen.h
| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | // SPDX-License-Identifier: BSD-3-Clause | ||
| 2 | #pragma once | ||
| 3 | |||
| 4 | #include <cstdint> | ||
| 5 | #include <functional> | ||
| 6 | #include <map> | ||
| 7 | #include <memory> | ||
| 8 | #include <span> | ||
| 9 | #include <string> | ||
| 10 | #include <utility> | ||
| 11 | #include <vector> | ||
| 12 | |||
| 13 | #include "chrono_batch_render/graph/graph.h" | ||
| 14 | #include "chrono_batch_render/material/node.h" | ||
| 15 | |||
| 16 | namespace chrono::batch_render::material { | ||
| 17 | |||
| 18 | inline constexpr uint32_t kFirstPlanetaryDefinition = 4; | ||
| 19 | inline constexpr uint32_t kFirstGeneratedDefinition = 8; | ||
| 20 | |||
| 21 | struct MaterialDefinition { | ||
| 22 | uint32_t id = 0; | ||
| 23 | std::string type; | ||
| 24 | std::string module; | ||
| 25 | std::string source; | ||
| 26 | std::map<std::string, std::string, std::less<>> files; | ||
| 27 | std::vector<std::pair<std::string, std::string>> origins; | ||
| 28 | uint32_t words = 0; | ||
| 29 | bool surface = false; | ||
| 30 | }; | ||
| 31 | |||
| 32 | class MaterialDefinitionSet { | ||
| 33 | public: | ||
| 34 | explicit MaterialDefinitionSet(std::vector<MaterialDefinition> definitions = {}, | ||
| 35 | std::map<std::string, std::string, std::less<>> files = {}, | ||
| 36 | bool planetary = false); | ||
| 37 | |||
| 38 | 1036 | std::span<const MaterialDefinition> Definitions() const { | |
| 39 | 1036 | return m_definitions; | |
| 40 | } | ||
| 41 | |||
| 42 | const std::map<std::string, std::string, std::less<>>& Files() const { | ||
| 43 | 215 | return m_files; | |
| 44 | } | ||
| 45 | |||
| 46 | 3975 | uint64_t Hash() const { | |
| 47 | 3975 | return m_hash; | |
| 48 | } | ||
| 49 | |||
| 50 | 545 | bool Planetary() const { | |
| 51 | 545 | return m_planetary; | |
| 52 | } | ||
| 53 | |||
| 54 | bool Contains(uint32_t id) const; | ||
| 55 | |||
| 56 | private: | ||
| 57 | std::vector<MaterialDefinition> m_definitions; | ||
| 58 | std::map<std::string, std::string, std::less<>> m_files; | ||
| 59 | bool m_planetary = false; | ||
| 60 | uint64_t m_hash = 0; | ||
| 61 | }; | ||
| 62 | |||
| 63 | using MaterialDefinitionSetRef = std::shared_ptr<const MaterialDefinitionSet>; | ||
| 64 | |||
| 65 | inline constexpr const char* kDispatchFile = | ||
| 66 | "chrono_batch_render/render/kernels/material/dispatch.slang"; | ||
| 67 | |||
| 68 | inline constexpr const char* kLobesFile = "chrono_batch_render/render/kernels/material/lobes.slang"; | ||
| 69 | |||
| 70 | inline constexpr uint32_t kTextureInputBytes = 48; | ||
| 71 | inline constexpr uint32_t kMappingBytes = 64; | ||
| 72 | inline constexpr uint32_t kDetailBytes = 80; | ||
| 73 | inline constexpr uint32_t kDetailTextures = 3; | ||
| 74 | inline constexpr uint32_t kNoTexture = 0xFFFFFFFF; | ||
| 75 | |||
| 76 | enum class BindingKind : uint8_t { Value, Texture, Pattern }; | ||
| 77 | |||
| 78 | 28 | struct InputBinding { | |
| 79 | BindingKind kind = BindingKind::Value; | ||
| 80 | uint32_t source = 0; | ||
| 81 | }; | ||
| 82 | |||
| 83 | 22 | struct ShadingNode { | |
| 84 | std::shared_ptr<const MaterialNodeDefinition> node; | ||
| 85 | std::vector<InputBinding> inputs; | ||
| 86 | }; | ||
| 87 | |||
| 88 | 15 | struct ShadingGraph { | |
| 89 | std::vector<ShadingNode> nodes; | ||
| 90 | }; | ||
| 91 | |||
| 92 | 29 | struct GraphLayout { | |
| 93 | std::vector<std::vector<uint32_t>> cells; | ||
| 94 | std::vector<std::vector<uint32_t>> textures; | ||
| 95 | uint32_t textureCount = 0; | ||
| 96 | bool mapped = false; | ||
| 97 | uint32_t mapping = 0; | ||
| 98 | bool detailed = false; | ||
| 99 | uint32_t detail = 0; | ||
| 100 | uint32_t detailTextures = 0; | ||
| 101 | uint32_t bytes = 0; | ||
| 102 | }; | ||
| 103 | |||
| 104 | bool Texturable(MaterialInputType type); | ||
| 105 | |||
| 106 | GraphLayout LayOut(const ShadingGraph& graph); | ||
| 107 | |||
| 108 | graph::ShapeKey MaterialDefinitionKey(const ShadingGraph& graph); | ||
| 109 | |||
| 110 | MaterialDefinition GenerateMaterialDefinition(uint32_t id, const ShadingGraph& graph); | ||
| 111 | |||
| 112 | inline constexpr const char* kRegisteredFile = | ||
| 113 | "chrono_batch_render/render/kernels/material/registered.slang"; | ||
| 114 | |||
| 115 | std::map<std::string, std::string, std::less<>> | ||
| 116 | ClosurePrimitiveSources(std::span<const std::shared_ptr<const ClosurePrimitive>> primitives); | ||
| 117 | |||
| 118 | std::map<std::string, std::string, std::less<>> | ||
| 119 | MaterialDispatchSources(const MaterialDefinitionSet& set); | ||
| 120 | |||
| 121 | } | ||
| 122 |