scene/tables/instances.cpp
| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | // SPDX-License-Identifier: BSD-3-Clause | ||
| 2 | #include "chrono_batch_render/scene/tables/instances.h" | ||
| 3 | |||
| 4 | #include <algorithm> | ||
| 5 | |||
| 6 | #include "chrono_batch_render/device/commands.h" | ||
| 7 | #include "chrono_batch_render/device/queue.h" | ||
| 8 | #include "chrono_batch_render/scene/kernels.h" | ||
| 9 | #include "chrono_batch_render/scene/records/render_tables.h" | ||
| 10 | #include "chrono_batch_render/scene/schema/columns.h" | ||
| 11 | #include "chrono_batch_render/scene/tables/kernels/instances.h" | ||
| 12 | #include "chrono_batch_render/stage/publish/change_set.h" | ||
| 13 | #include "chrono_batch_render/util/profiler.h" | ||
| 14 | |||
| 15 | namespace chrono::batch_render { | ||
| 16 | |||
| 17 | namespace { | ||
| 18 | |||
| 19 | constexpr ColumnRef kActivation[] = {kSphereRadius, | ||
| 20 | kCubeSize, | ||
| 21 | kPlaneWidth, | ||
| 22 | kPlaneLength, | ||
| 23 | kPlaneAxis, | ||
| 24 | kCylinderRadius, | ||
| 25 | kCylinderHeight, | ||
| 26 | kCylinderAxis, | ||
| 27 | kConeRadius, | ||
| 28 | kConeHeight, | ||
| 29 | kConeAxis, | ||
| 30 | kCapsuleRadius, | ||
| 31 | kCapsuleHeight, | ||
| 32 | kCapsuleAxis, | ||
| 33 | kSphereLightRadius, | ||
| 34 | kSphereLightTreatAsPoint, | ||
| 35 | kRectLightWidth, | ||
| 36 | kRectLightHeight, | ||
| 37 | kDiskLightRadius, | ||
| 38 | kCylinderLightLength, | ||
| 39 | kCylinderLightRadius, | ||
| 40 | kCylinderLightTreatAsLine, | ||
| 41 | kLightAPIVisibleInPrimaryRay, | ||
| 42 | kPointInstancerProtoIndices, | ||
| 43 | kPointInstancerIds, | ||
| 44 | kPointInstancerInvisibleIds, | ||
| 45 | kGprimDoubleSided, | ||
| 46 | kGprimOrientation}; | ||
| 47 | |||
| 48 | constexpr ColumnRef kPlacement[] = {kPointBasedPoints, kPointInstancerOrientations, | ||
| 49 | kPointInstancerScales, kRenderProductCamera}; | ||
| 50 | |||
| 51 | constexpr ColumnRef kLayoutInputs[] = {kPointInstancerPrototypes, kPointInstancerPositions, | ||
| 52 | kPrototypeAPIPrototype}; | ||
| 53 | |||
| 54 | 2744 | const std::vector<ColumnRef>& InputColumns() { | |
| 55 | 5 | static const std::vector<ColumnRef> inputs = [] { | |
| 56 | 5 | std::vector<ColumnRef> columns(std::begin(kPlacement), std::end(kPlacement)); | |
| 57 | 5 | columns.insert(columns.end(), std::begin(kActivation), std::end(kActivation)); | |
| 58 | 5 | columns.insert(columns.end(), std::begin(kLayoutInputs), std::end(kLayoutInputs)); | |
| 59 | 5 | return columns; | |
| 60 | 2744 | }(); | |
| 61 | 2744 | return inputs; | |
| 62 | } | ||
| 63 | |||
| 64 | } | ||
| 65 | |||
| 66 | 379 | InstanceTable::InstanceTable(Structure& structure, const RenderTables& tables, | |
| 67 | 379 | bvh::Builder& builder, uint32_t maxRefits) | |
| 68 | 2274 | : m_structure(structure), m_tables(tables), m_builder(builder), m_maxRefits(maxRefits) { | |
| 69 | 379 | device::Device& device = structure.GetColumnStore().GetDevice(); | |
| 70 | 379 | const BvhBox unit = {{-1, -1, -1}, {1, 1, 1}}; | |
| 71 | 758 | m_unitBoxes = device.CreateBuffer( | |
| 72 | 379 | {.usage = device::BufferUsage::AccelBuildInput, .label = "unit box"}, std::span(&unit, 1)); | |
| 73 | 379 | m_unitBox = builder.CreateBlas(device::AccelBoxes{.boxes = m_unitBoxes->Slice(0), .count = 1}); | |
| 74 | 379 | device::Queue& queue = device.GetQueue(); | |
| 75 | 379 | device::CommandEncoder encoder = queue.CreateEncoder(); | |
| 76 | 1137 | const Ref<bvh::Blas> boxes[] = {m_unitBox}; | |
| 77 | 379 | builder.Build(encoder, boxes); | |
| 78 | 379 | queue.Submit(encoder.Finish()); | |
| 79 | 1895 | } | |
| 80 | |||
| 81 | 2744 | std::span<const ColumnRef> InstanceTable::Inputs() const { | |
| 82 | 2744 | return InputColumns(); | |
| 83 | } | ||
| 84 | |||
| 85 | 790 | void InstanceTable::Run(const ChangeSet& changes, TableVersions& versions) { | |
| 86 | 790 | CHRONO_BATCH_RENDER_ZONE("Instances/Run"); | |
| 87 | 790 | ColumnStore& store = m_structure.GetColumnStore(); | |
| 88 | 790 | const uint64_t seen = store.WriteSequence(); | |
| 89 | 790 | bool relaid = changes.resync || !m_tlas || m_structure.CompositionVersion() != m_compositions; | |
| 90 | 490 | bool restacked = false; | |
| 91 | 490 | if (!relaid && | |
| 92 | 490 | (std::ranges::any_of(kLayoutInputs, | |
| 93 | 1465 | [&](ColumnRef column) { return store.LastWrite(column) > m_seen; }) || | |
| 94 | 484 | store.LastWrite(kPointInstancerInvisibleIds) > m_seen)) { | |
| 95 | 8 | std::vector<Instancer> fresh = GatherInstancers(); | |
| 96 | 8 | if (fresh != m_instancers) { | |
| 97 | 5 | const bool counts = | |
| 98 | 5 | std::ranges::equal(fresh, m_instancers, [](const Instancer& a, const Instancer& b) { | |
| 99 | 4 | return a.prim == b.prim && a.world == b.world && a.native == b.native && | |
| 100 | 4 | a.roots == b.roots; | |
| 101 | }); | ||
| 102 | 5 | m_instancers = std::move(fresh); | |
| 103 | 5 | relaid = !counts || !Restack(); | |
| 104 | 5 | restacked = !relaid; | |
| 105 | 3 | } else if (store.LastWrite(kPointInstancerInvisibleIds) > m_seen) { | |
| 106 | 2 | relaid = !Restack(); | |
| 107 | 2 | restacked = !relaid; | |
| 108 | } | ||
| 109 | 8 | } | |
| 110 | 790 | std::set<uint32_t> structural; | |
| 111 | 790 | if (!relaid && (changes.structure || !changes.apis.empty())) { | |
| 112 | 191 | if (std::optional<std::set<uint32_t>> touched = Apply(changes)) { | |
| 113 | 181 | structural = std::move(*touched); | |
| 114 | } else { | ||
| 115 | relaid = true; | ||
| 116 | 191 | } | |
| 117 | } | ||
| 118 | 790 | if (relaid) { | |
| 119 | 311 | Relayout(); | |
| 120 | } | ||
| 121 | 790 | if (relaid || restacked) { | |
| 122 | 634 | versions.Record(TableId::Instances, std::vector<uint32_t>{kGlobalWorld}); | |
| 123 | } | ||
| 124 | 790 | const uint32_t tlases = m_regions.Size(); | |
| 125 | 790 | if (restacked) { | |
| 126 | 12 | for (uint32_t tlas = 0; tlas < tlases; ++tlas) { | |
| 127 | 6 | structural.insert(tlas); | |
| 128 | } | ||
| 129 | } | ||
| 130 | 790 | std::set<uint32_t> touched = structural; | |
| 131 | 790 | const bool global = | |
| 132 | 790 | relaid || store.LastWrite(kRenderProductCamera) > m_seen || changes.Global(); | |
| 133 | 1582 | for (const uint32_t world : global ? std::vector<uint32_t>{kGlobalWorld} : changes.worlds) { | |
| 134 | 1584 | touched.merge(Tlases(world)); | |
| 135 | ✗ | } | |
| 136 | 790 | const std::vector<uint32_t> dirty(touched.begin(), touched.end()); | |
| 137 | 1079 | const auto written = [&](std::span<const ColumnRef> columns) { | |
| 138 | 289 | return std::ranges::any_of( | |
| 139 | 5406 | columns, [&](ColumnRef column) { return store.LastWrite(column) > m_seen; }); | |
| 140 | 790 | }; | |
| 141 | 1404 | const bool activated = changes.Wrote(TableId::Imageables) || changes.Wrote(TableId::Meshes) || | |
| 142 | 1611 | changes.Wrote(TableId::Points) || changes.Wrote(TableId::Curves) || | |
| 143 | 260 | written(kActivation); | |
| 144 | 645 | const bool placed = relaid || restacked || activated || !structural.empty() || | |
| 145 | 247 | changes.Wrote(TableId::Transforms) || changes.Wrote(TableId::MeshPoints) || | |
| 146 | 53 | changes.Wrote(TableId::PointsMoved) || | |
| 147 | 838 | changes.Wrote(TableId::CurvesMoved) || written(kPlacement) || | |
| 148 | 13 | written(kLayoutInputs); | |
| 149 | 790 | m_seen = seen; | |
| 150 | |||
| 151 | 790 | m_stats = {}; | |
| 152 | 790 | m_stats.tlases = tlases; | |
| 153 | 790 | m_stats.instances = 0; | |
| 154 | 1757 | for (const uint32_t count : m_regions.Counts()) { | |
| 155 | 967 | m_stats.instances += count; | |
| 156 | } | ||
| 157 | 790 | m_stats.relaid = relaid; | |
| 158 | 790 | if (dirty.empty()) { | |
| 159 | return; | ||
| 160 | } | ||
| 161 | |||
| 162 | 790 | std::vector<uint32_t> built; | |
| 163 | 790 | std::vector<uint32_t> refit; | |
| 164 | 790 | uint32_t widest = 0; | |
| 165 | 1704 | for (const uint32_t tlas : dirty) { | |
| 166 | 914 | const uint32_t count = m_regions.Counts()[tlas]; | |
| 167 | 914 | widest = std::max(widest, m_regions.Capacity(tlas)); | |
| 168 | 914 | m_stats.written += count; | |
| 169 | 914 | if (!placed) { | |
| 170 | 13 | continue; | |
| 171 | } | ||
| 172 | 901 | if (relaid || activated || structural.contains(tlas) || m_refits[tlas] >= m_maxRefits) { | |
| 173 | 733 | built.push_back(tlas); | |
| 174 | 733 | m_refits[tlas] = 0; | |
| 175 | } else { | ||
| 176 | 168 | refit.push_back(tlas); | |
| 177 | 168 | ++m_refits[tlas]; | |
| 178 | } | ||
| 179 | } | ||
| 180 | 790 | m_stats.built = uint32_t(built.size()); | |
| 181 | 790 | if (!built.empty()) { | |
| 182 | 619 | std::vector<uint32_t> rebuilt; | |
| 183 | 1352 | for (const uint32_t tlas : built) { | |
| 184 | 860 | rebuilt.push_back(TlasWorld(tlas, m_worlds)); | |
| 185 | } | ||
| 186 | 619 | versions.Record(TableId::Slots, rebuilt); | |
| 187 | 619 | } | |
| 188 | 790 | m_stats.refit = uint32_t(refit.size()); | |
| 189 | |||
| 190 | 790 | device::Device& device = store.GetDevice(); | |
| 191 | 790 | static const profiler::ZoneId zone = profiler::ZoneId::Named("Instances/Write"); | |
| 192 | 790 | const Ref<device::Buffer> dirtyBuffer = device.CreateBuffer({.label = "dirty TLASes"}, dirty); | |
| 193 | 790 | InstanceParams params{}; | |
| 194 | 1580 | params.scene.address = store.DeviceData(); | |
| 195 | 790 | params.layout.address = m_layout->Address(); | |
| 196 | 790 | params.instances.address = m_records.Address(); | |
| 197 | 790 | params.shapes.address = m_shapes.Address(); | |
| 198 | 790 | params.tlases.address = dirtyBuffer->Address(); | |
| 199 | 790 | params.hits.address = m_hits.Address(); | |
| 200 | 790 | params.origins.address = m_renderOrigins.Address(); | |
| 201 | 790 | params.transforms = m_tables.transforms; | |
| 202 | 790 | params.imageables = m_tables.imageables; | |
| 203 | 790 | params.meshes = m_tables.meshes; | |
| 204 | 790 | params.points = m_tables.points; | |
| 205 | 790 | params.curves = m_tables.curves; | |
| 206 | 790 | params.slotWorlds.address = m_slotWorlds.Address(); | |
| 207 | 790 | params.unitBox = m_unitBox->Handle(); | |
| 208 | |||
| 209 | 790 | device::Queue& queue = device.GetQueue(); | |
| 210 | 790 | device::CommandEncoder encoder = queue.CreateEncoder(); | |
| 211 | 790 | OriginParams origins{}; | |
| 212 | 790 | origins.scene.address = store.DeviceData(); | |
| 213 | 790 | origins.layout.address = m_layout->Address(); | |
| 214 | 790 | origins.products.address = m_productBuffer->Address(); | |
| 215 | 790 | origins.origins.address = m_renderOrigins.Address(); | |
| 216 | 790 | origins.viewers.address = m_viewers->Address(); | |
| 217 | 790 | origins.transforms = m_tables.transforms; | |
| 218 | 790 | origins.productCount = uint32_t(m_products.size()); | |
| 219 | 790 | origins.tlasCount = tlases; | |
| 220 | 790 | if (!m_products.empty()) { | |
| 221 | 1016 | encoder.Dispatch(Kernels().Entry("GatherViewers"), origins, | |
| 222 | {.x = kViewerGroups, .y = origins.productCount}); | ||
| 223 | } | ||
| 224 | 790 | encoder.Dispatch(Kernels().Entry("PlaceOrigins"), origins, | |
| 225 | 790 | {.x = device::GroupCount(tlases, kInstanceGroup)}); | |
| 226 | 790 | if (widest > 0) { | |
| 227 | 790 | const device::ZoneScope scope(encoder, zone); | |
| 228 | 790 | encoder.Dispatch( | |
| 229 | 790 | Kernels().Entry("WriteInstances"), params, | |
| 230 | 790 | {.x = device::GroupCount(widest, kInstanceGroup), .y = uint32_t(dirty.size())}); | |
| 231 | 790 | } | |
| 232 | 790 | const device::BufferRange counts = | |
| 233 | 790 | LayoutWords(m_header.instanceCounts.address, 0, m_regions.Size()); | |
| 234 | 790 | if (!built.empty()) { | |
| 235 | 619 | m_builder.Build(encoder, *m_tlas, *m_records.Buffer(), counts, built); | |
| 236 | } | ||
| 237 | 790 | if (refit.empty()) { | |
| 238 | 632 | queue.Submit(encoder.Finish()); | |
| 239 | 632 | return; | |
| 240 | } | ||
| 241 | 158 | m_builder.SubmitRefit(std::move(encoder), *m_tlas, *m_records.Buffer(), counts, refit); | |
| 242 | 3792 | } | |
| 243 | |||
| 244 | 1450 | std::set<uint32_t> InstanceTable::Tlases(uint32_t world) const { | |
| 245 | 1450 | std::set<uint32_t> tlases; | |
| 246 | 1450 | if (world == kGlobalWorld) { | |
| 247 | 3013 | for (uint32_t tlas = 0; tlas < m_regions.Size(); ++tlas) { | |
| 248 | 1615 | tlases.insert(tlas); | |
| 249 | } | ||
| 250 | 1398 | return tlases; | |
| 251 | } | ||
| 252 | 52 | if (world < m_worlds) { | |
| 253 | 52 | tlases.insert(world); | |
| 254 | } | ||
| 255 | 156 | for (const auto* map : {&m_shownIn, &m_watch}) { | |
| 256 | 104 | if (const auto found = map->find(world); found != map->end()) { | |
| 257 | 1 | tlases.insert(found->second.begin(), found->second.end()); | |
| 258 | } | ||
| 259 | } | ||
| 260 | 52 | return tlases; | |
| 261 | ✗ | } | |
| 262 | |||
| 263 | 790 | void InstanceTable::Describe(RenderTables& tables) const { | |
| 264 | 790 | tables.instances.address = m_layout ? m_layout->Address() : 0; | |
| 265 | 790 | tables.origins.address = m_renderOrigins.Address(); | |
| 266 | 790 | tables.slotWorlds.address = m_slotWorlds.Address(); | |
| 267 | 790 | } | |
| 268 | |||
| 269 | 978 | void InstanceTable::Hold(std::vector<Ref<const RefCounted>>& held) const { | |
| 270 | 8802 | HoldBuffers(held, {m_layout, m_records.Buffer(), m_shapes.Buffer(), m_hits.Buffer(), | |
| 271 | 978 | m_slotWorlds.Buffer(), m_instancing, m_renderOrigins.Buffer()}); | |
| 272 | 978 | if (m_tlas) { | |
| 273 | 977 | held.emplace_back(m_tlas); | |
| 274 | } | ||
| 275 | 978 | if (m_unitBox) { | |
| 276 | 978 | held.emplace_back(m_unitBox); | |
| 277 | } | ||
| 278 | 1956 | } | |
| 279 | |||
| 280 | 353 | void InstanceTable::Prepare() { | |
| 281 | 353 | Kernels(); | |
| 282 | 353 | } | |
| 283 | |||
| 284 | 2441 | device::Program& InstanceTable::Kernels() { | |
| 285 | 2441 | if (!m_program) { | |
| 286 | 1137 | m_program = m_structure.GetColumnStore().GetDevice().CreateProgram( | |
| 287 | {.modules = {"chrono_batch_render.scene.tables.kernels.instances"}, | ||
| 288 | .library = &kSceneKernels, | ||
| 289 | .entries = {"WriteInstances", "GatherViewers", "PlaceOrigins"}, | ||
| 290 | 758 | .label = "instances"}); | |
| 291 | } | ||
| 292 | 2441 | return *m_program; | |
| 293 | 758 | } | |
| 294 | |||
| 295 | } | ||
| 296 |