chrono_batch_render/
Coverage:
low: ≥ 0%
medium: ≥ 75.0%
high: ≥ 90.0%
Lines:
18 / 18
100.0%
Branches:
53 / 80
66.2%
Functions:
5 / 5, 2 excl
100.0%

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
9/15
✓ Branch 1 taken 353 times.
✓ Branch 6 taken 353 times.
✓ Branch 10 taken 353 times.
✓ Branch 15 taken 353 times.
✓ Branch 19 taken 353 times.
✓ Branch 22 taken 353 times.
✓ Branch 25 taken 353 times.
✓ Branch 29 taken 353 times.
✓ Branch 33 taken 353 times.
✗ Branch 39 not taken.
✗ Branch 40 not taken.
✗ Branch 46 not taken.
✗ Branch 47 not taken.
✗ Branch 56 not taken.
✗ Branch 57 not taken.
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
4/8
✓ Branch 0 taken 4640 times.
✓ Branch 1 taken 928 times.
✓ Branch 2 taken 4635 times.
✓ Branch 3 taken 5 times.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
✗ Branch 6 not taken.
✗ Branch 7 not taken.
5568 HoldBuffers(held, {m_records.Buffer(), m_words.Buffer(), m_checkers.Buffer(),
57 m_temperatures.Buffer(), m_radios.Buffer()});
58 1856 }
59
60
1/2
✓ Branch 0 taken 1 time.
✗ Branch 1 not taken.
1 const MaterialRecord& Record(uint32_t prim) const {
61
31/47
✓ Branch 0 taken 1 time.
✗ Branch 1 not taken.
✓ Branch 3 taken 1 time.
✓ Branch 5 taken 1 time.
✗ Branch 6 not taken.
✓ Branch 8 taken 1 time.
✓ Branch 10 taken 1 time.
✗ Branch 11 not taken.
✓ Branch 13 taken 1 time.
✓ Branch 15 taken 1 time.
✗ Branch 16 not taken.
✓ Branch 18 taken 1 time.
✓ Branch 20 taken 1 time.
✗ Branch 21 not taken.
✓ Branch 23 taken 1 time.
✓ Branch 25 taken 1 time.
✗ Branch 26 not taken.
✓ Branch 28 taken 1 time.
✓ Branch 30 taken 1 time.
✗ Branch 31 not taken.
✓ Branch 33 taken 1 time.
✓ Branch 35 taken 1 time.
✗ Branch 36 not taken.
✓ Branch 38 taken 1 time.
✓ Branch 40 taken 1 time.
✗ Branch 41 not taken.
✓ Branch 43 taken 1 time.
✓ Branch 45 taken 1 time.
✗ Branch 46 not taken.
✓ Branch 48 taken 1 time.
✓ Branch 50 taken 1 time.
✗ Branch 51 not taken.
✓ Branch 53 taken 1 time.
✓ Branch 55 taken 1 time.
✗ Branch 56 not taken.
✓ Branch 57 taken 1 time.
✗ Branch 58 not taken.
✓ Branch 60 taken 1 time.
✓ Branch 62 taken 1 time.
✗ Branch 63 not taken.
✓ Branch 65 taken 1 time.
✓ Branch 67 taken 1 time.
✗ Branch 68 not taken.
✓ Branch 70 taken 1 time.
✓ Branch 72 taken 1 time.
✗ Branch 73 not taken.
✓ Branch 75 taken 1 time.
31 return m_records.Row(prim);
62 }
63
64 7 uint32_t Resolves() const {
65
7/7
✓ Branch 1 taken 1 time.
✓ Branch 4 taken 1 time.
✓ Branch 7 taken 1 time.
✓ Branch 10 taken 1 time.
✓ Branch 13 taken 1 time.
✓ Branch 16 taken 1 time.
✓ Branch 19 taken 1 time.
7 return m_resolves;
66 }
67
68 149 const material::MaterialDefinitionSetRef& Definitions() const {
69
1/1
✓ Branch 2 taken 149 times.
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