scene/tables/materials.h
| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | // SPDX-License-Identifier: BSD-3-Clause | ||
| 2 | #pragma once | ||
| 3 | |||
| 4 | #include <cstdint> | ||
| 5 | #include <map> | ||
| 6 | #include <memory> | ||
| 7 | #include <set> | ||
| 8 | #include <span> | ||
| 9 | #include <string> | ||
| 10 | #include <string_view> | ||
| 11 | #include <utility> | ||
| 12 | #include <vector> | ||
| 13 | |||
| 14 | #include "chrono_batch_render/device/program.h" | ||
| 15 | #include "chrono_batch_render/device/ref.h" | ||
| 16 | #include "chrono_batch_render/graph/library.h" | ||
| 17 | #include "chrono_batch_render/material/codegen.h" | ||
| 18 | #include "chrono_batch_render/material/node.h" | ||
| 19 | #include "chrono_batch_render/scene/records/materials.h" | ||
| 20 | #include "chrono_batch_render/scene/tables/connections.h" | ||
| 21 | #include "chrono_batch_render/scene/tables/pack.h" | ||
| 22 | #include "chrono_batch_render/scene/tables/storage.h" | ||
| 23 | #include "chrono_batch_render/scene/tables/table.h" | ||
| 24 | #include "chrono_batch_render/stage/structure/structure.h" | ||
| 25 | |||
| 26 | namespace chrono::batch_render { | ||
| 27 | |||
| 28 | class TextureTable; | ||
| 29 | |||
| 30 | class MaterialTable : public RenderTable { | ||
| 31 | public: | ||
| 32 | 353 | MaterialTable(Structure& structure, const TextureTable& textures) | |
| 33 | 1412 | : m_structure(structure), m_textures(textures) {} | |
| 34 | |||
| 35 | 928 | std::string_view Name() const override { | |
| 36 | 928 | return "materials"; | |
| 37 | } | ||
| 38 | |||
| 39 | std::span<const ColumnRef> Inputs() const override; | ||
| 40 | |||
| 41 | 3096 | std::span<const TableId> TableInputs() const override { | |
| 42 | 3096 | static constexpr TableId kTables[] = {TableId::Textures}; | |
| 43 | 3096 | return kTables; | |
| 44 | } | ||
| 45 | |||
| 46 | void Prepare() override; | ||
| 47 | |||
| 48 | 8553 | bool Pending() const override { | |
| 49 | 8553 | return m_nodeVersion != material::MaterialNodeVersion(); | |
| 50 | } | ||
| 51 | |||
| 52 | void Run(const ChangeSet& changes, TableVersions& versions) override; | ||
| 53 | void Describe(RenderTables& tables) const override; | ||
| 54 | |||
| 55 | 928 | void Hold(std::vector<Ref<const RefCounted>>& held) const override { | |
| 56 | 5568 | HoldBuffers(held, {m_records.Buffer(), m_words.Buffer(), m_checkers.Buffer(), | |
| 57 | m_temperatures.Buffer(), m_radios.Buffer()}); | ||
| 58 | 1856 | } | |
| 59 | |||
| 60 | 1 | const MaterialRecord& Record(uint32_t prim) const { | |
| 61 | 31 | return m_records.Row(prim); | |
| 62 | } | ||
| 63 | |||
| 64 | 7 | uint32_t Resolves() const { | |
| 65 | 7 | return m_resolves; | |
| 66 | } | ||
| 67 | |||
| 68 | 149 | const material::MaterialDefinitionSetRef& Definitions() const { | |
| 69 | 3662 | return m_definitions; | |
| 70 | } | ||
| 71 | |||
| 72 | private: | ||
| 73 | using Connection = ConnectionCache::Connection; | ||
| 74 | |||
| 75 | struct Binding { | ||
| 76 | uint32_t texture = kNoPrim; | ||
| 77 | uint32_t low = 0; | ||
| 78 | uint32_t high = 0; | ||
| 79 | }; | ||
| 80 | |||
| 81 | struct Carry { | ||
| 82 | uint32_t from = 0; | ||
| 83 | uint32_t to = 0; | ||
| 84 | uint32_t words = 0; | ||
| 85 | }; | ||
| 86 | |||
| 87 | struct RadarLink { | ||
| 88 | uint32_t material = kNoPrim; | ||
| 89 | uint32_t row = 0; | ||
| 90 | }; | ||
| 91 | |||
| 92 | struct Reached { | ||
| 93 | uint32_t prim = kNoPrim; | ||
| 94 | std::shared_ptr<const material::MaterialNodeDefinition> node; | ||
| 95 | std::vector<material::InputBinding> inputs; | ||
| 96 | std::vector<Connection> textures; | ||
| 97 | std::vector<uint32_t> spectra; | ||
| 98 | }; | ||
| 99 | |||
| 100 | void Resolve(const ChangeSet& changes); | ||
| 101 | void ResolveMaterial(uint32_t material); | ||
| 102 | void ResolvePreview(uint32_t material, uint32_t shader); | ||
| 103 | void ResolveNode(uint32_t material, uint32_t shader, | ||
| 104 | std::shared_ptr<const material::MaterialNodeDefinition> surface); | ||
| 105 | std::vector<Reached> | ||
| 106 | ReachNodes(uint32_t shader, std::shared_ptr<const material::MaterialNodeDefinition> surface); | ||
| 107 | std::vector<uint32_t> OrderPatterns(std::vector<Reached>& reached) const; | ||
| 108 | bool BindTexture(uint32_t shader, const Connection& connection, std::string_view input, | ||
| 109 | bool color, uint32_t slot, TextureChannel& channel); | ||
| 110 | void CarryPrevious(const std::vector<MaterialRecord>& before); | ||
| 111 | void Define(uint32_t material, uint32_t definition, uint32_t words); | ||
| 112 | void WarnOutsideVisible(); | ||
| 113 | void ResolveRadios(); | ||
| 114 | void WriteRadios(); | ||
| 115 | void ResolvePlacement(uint32_t placement, uint32_t mapping); | ||
| 116 | uint32_t PackDetail(uint32_t shader, uint32_t detail, uint32_t textures); | ||
| 117 | bool ResolvePlanetary(uint32_t material, uint32_t shader); | ||
| 118 | bool Restructured(const ChangeSet& changes) const; | ||
| 119 | bool Changed(SchemaId schema) const; | ||
| 120 | std::span<const Connection> Connections(uint32_t prim); | ||
| 121 | void CopyCell(ColumnRef column, uint32_t prim, uint32_t destination); | ||
| 122 | void BuildEntries(); | ||
| 123 | device::Program& Kernels(); | ||
| 124 | |||
| 125 | Structure& m_structure; | ||
| 126 | const TextureTable& m_textures; | ||
| 127 | Ref<device::Program> m_program; | ||
| 128 | RecordTable<MaterialRecord> m_records{"material records"}; | ||
| 129 | DeviceTable<uint32_t> m_words{"material words"}; | ||
| 130 | PackPlan m_pack{"material"}; | ||
| 131 | std::vector<Binding> m_bindings; | ||
| 132 | std::vector<Carry> m_carries; | ||
| 133 | ConnectionCache m_connections{m_structure, {SchemaId::Connectable}}; | ||
| 134 | std::vector<bool> m_touched; | ||
| 135 | uint64_t m_writes = 0; | ||
| 136 | std::vector<RadarLink> m_radarLinks; | ||
| 137 | Ref<device::Buffer> m_carried; | ||
| 138 | graph::DefinitionLibrary<material::MaterialDefinition> m_library{ | ||
| 139 | material::kFirstGeneratedDefinition}; | ||
| 140 | std::vector<uint32_t> m_generations; | ||
| 141 | std::set<uint32_t> m_spectral; | ||
| 142 | std::set<std::pair<uint64_t, uint64_t>> m_warned; | ||
| 143 | mutable std::vector<ColumnRef> m_inputs; | ||
| 144 | mutable uint32_t m_inputSchemas = 0; | ||
| 145 | uint64_t m_nodeVersion = material::MaterialNodeVersion(); | ||
| 146 | uint32_t m_wordCount = 0; | ||
| 147 | uint32_t m_resolves = 0; | ||
| 148 | material::MaterialDefinitionSetRef m_definitions = | ||
| 149 | std::make_shared<const material::MaterialDefinitionSet>(); | ||
| 150 | DeviceTable<CheckerRecord> m_checkers{"checkerboard records"}; | ||
| 151 | DeviceTable<float> m_temperatures{"temperatures"}; | ||
| 152 | uint32_t m_temperaturePrimvar = 0; | ||
| 153 | RecordTable<RadioRecord> m_radios{"radio materials"}; | ||
| 154 | }; | ||
| 155 | |||
| 156 | } | ||
| 157 |