scene/tables/bounds.cpp
| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | // SPDX-License-Identifier: BSD-3-Clause | ||
| 2 | #include "chrono_batch_render/scene/tables/bounds.h" | ||
| 3 | |||
| 4 | #include <algorithm> | ||
| 5 | #include <bit> | ||
| 6 | #include <cmath> | ||
| 7 | #include <limits> | ||
| 8 | |||
| 9 | #include "chrono_batch_render/device/commands.h" | ||
| 10 | #include "chrono_batch_render/device/queue.h" | ||
| 11 | #include "chrono_batch_render/scene/kernels.h" | ||
| 12 | #include "chrono_batch_render/scene/records/render_tables.h" | ||
| 13 | #include "chrono_batch_render/scene/schema/columns.h" | ||
| 14 | #include "chrono_batch_render/scene/tables/kernels/bounds.h" | ||
| 15 | #include "chrono_batch_render/stage/publish/change_set.h" | ||
| 16 | #include "chrono_batch_render/util/error.h" | ||
| 17 | #include "chrono_batch_render/util/profiler.h" | ||
| 18 | |||
| 19 | namespace chrono::batch_render { | ||
| 20 | |||
| 21 | namespace { | ||
| 22 | |||
| 23 | constexpr ColumnRef kInputs[] = {kPointBasedPoints, | ||
| 24 | kSphereRadius, | ||
| 25 | kCubeSize, | ||
| 26 | kPlaneWidth, | ||
| 27 | kPlaneLength, | ||
| 28 | kPlaneAxis, | ||
| 29 | kCylinderRadius, | ||
| 30 | kCylinderHeight, | ||
| 31 | kCylinderAxis, | ||
| 32 | kConeRadius, | ||
| 33 | kConeHeight, | ||
| 34 | kConeAxis, | ||
| 35 | kCapsuleRadius, | ||
| 36 | kCapsuleHeight, | ||
| 37 | kCapsuleAxis, | ||
| 38 | kPointInstancerPrototypes, | ||
| 39 | kPointInstancerProtoIndices, | ||
| 40 | kPointInstancerPositions, | ||
| 41 | kPointInstancerOrientations, | ||
| 42 | kPointInstancerScales, | ||
| 43 | kPointInstancerIds, | ||
| 44 | kPointInstancerInvisibleIds, | ||
| 45 | kPrototypeAPIPrototype}; | ||
| 46 | |||
| 47 | 1470 | uint32_t Ordered(float value) { | |
| 48 | 1470 | const uint32_t bits = std::bit_cast<uint32_t>(value); | |
| 49 | 1470 | return (bits & 0x80000000u) != 0 ? ~bits : bits | 0x80000000u; | |
| 50 | } | ||
| 51 | |||
| 52 | } | ||
| 53 | |||
| 54 | 2589 | std::span<const ColumnRef> BoundsTable::Inputs() const { | |
| 55 | 2589 | return kInputs; | |
| 56 | } | ||
| 57 | |||
| 58 | 462 | void BoundsTable::Relayout() { | |
| 59 | 462 | const Prims& prims = m_structure.GetPrims(); | |
| 60 | 462 | device::Device& device = m_structure.GetColumnStore().GetDevice(); | |
| 61 | 462 | const uint32_t worlds = prims.WorldCount(); | |
| 62 | 893 | std::vector<uint32_t> origins(std::max(worlds, 1u), kNoRow); | |
| 63 | 11730 | for (uint32_t prim = 0; prim < prims.Size(); ++prim) { | |
| 64 | 11268 | if (prims.Alive(prim) && IsA(prims.Record(prim).schema, SchemaId::World)) { | |
| 65 | 57 | origins[prims.Record(prim).world] = prims.Row(prim, SchemaId::Xformable); | |
| 66 | } | ||
| 67 | } | ||
| 68 | 462 | m_origins = | |
| 69 | 462 | device.CreateBuffer({.label = "bounds world origins"}, std::span<const uint32_t>(origins)); | |
| 70 | 462 | if (worlds + 1 != m_boxes.Rows()) { | |
| 71 | 278 | m_boxes.Allocate(device, worlds + 1); | |
| 72 | 556 | m_extents = device.CreateBuffer( | |
| 73 | 278 | {.bytes = uint64_t(m_boxes.Rows()) * 6 * sizeof(uint32_t), .label = "world extents"}); | |
| 74 | } | ||
| 75 | 462 | m_primBounds.Reserve(device, prims.Size()); | |
| 76 | 462 | m_epoch = m_structure.Epoch(); | |
| 77 | 2220 | } | |
| 78 | |||
| 79 | 735 | void BoundsTable::Run(const ChangeSet& changes, TableVersions&) { | |
| 80 | 735 | CHRONO_BATCH_RENDER_ZONE("Bounds/Run"); | |
| 81 | 735 | ColumnStore& store = m_structure.GetColumnStore(); | |
| 82 | 735 | device::Device& device = store.GetDevice(); | |
| 83 | 735 | if (changes.resync || changes.structure || m_epoch != m_structure.Epoch()) { | |
| 84 | 462 | Relayout(); | |
| 85 | } | ||
| 86 | 735 | BoundsParams params{}; | |
| 87 | 735 | params.scene.address = store.DeviceData(); | |
| 88 | 735 | params.origins.address = m_origins->Address(); | |
| 89 | 735 | params.extents.address = m_extents->Address(); | |
| 90 | 735 | params.boxes.address = m_boxes.Address(); | |
| 91 | 735 | params.transforms = m_tables.transforms; | |
| 92 | 735 | params.imageables = m_tables.imageables; | |
| 93 | 735 | params.meshes = m_tables.meshes; | |
| 94 | 735 | params.points = m_tables.points; | |
| 95 | 735 | params.curves = m_tables.curves; | |
| 96 | 735 | params.primBounds.address = m_primBounds.Address(); | |
| 97 | 735 | params.primCount = m_structure.GetPrims().Size(); | |
| 98 | 735 | params.slotCount = m_boxes.Rows(); | |
| 99 | 735 | const uint64_t instancerBytes = std::max<uint64_t>(params.primCount, 1) * 6 * sizeof(uint32_t); | |
| 100 | 735 | if (!m_instancerExtents || m_instancerExtents->Bytes() < instancerBytes) { | |
| 101 | 584 | m_instancerExtents = device.CreateBuffer( | |
| 102 | 292 | {.bytes = std::bit_ceil(instancerBytes), .label = "instancer extents"}); | |
| 103 | } | ||
| 104 | 735 | params.instancerExtents.address = m_instancerExtents->Address(); | |
| 105 | 735 | const bool slots = m_tables.instances.address != 0; | |
| 106 | 735 | if (slots) { | |
| 107 | 735 | params.instances = m_tables.instances; | |
| 108 | 735 | params.instanceSlots = uint32_t(m_instances.InstancePrims().size()); | |
| 109 | 735 | params.tlasCount = uint32_t(m_instances.InstanceCounts().size()); | |
| 110 | } | ||
| 111 | |||
| 112 | 735 | const uint64_t half = uint64_t(m_boxes.Rows()) * 3 * sizeof(uint32_t); | |
| 113 | 735 | device::Queue& queue = device.GetQueue(); | |
| 114 | 735 | static const profiler::ZoneId zone = profiler::ZoneId::Named("Bounds/Compute"); | |
| 115 | 735 | device::CommandEncoder encoder = queue.CreateEncoder(); | |
| 116 | 735 | { | |
| 117 | 735 | const device::ZoneScope scope(encoder, zone); | |
| 118 | 735 | encoder.Fill(m_extents->Slice(0, half), Ordered(std::numeric_limits<float>::infinity())); | |
| 119 | 735 | encoder.Fill(m_extents->Slice(half), Ordered(-std::numeric_limits<float>::infinity())); | |
| 120 | 735 | encoder.Dispatch(Kernels().Entry("PrimBounds"), params, | |
| 121 | 735 | {.x = device::GroupCount(params.primCount, kBoundsGroup)}); | |
| 122 | 735 | if (slots && params.instanceSlots > 0) { | |
| 123 | 735 | const uint64_t lowers = uint64_t(params.primCount) * 3 * sizeof(uint32_t); | |
| 124 | 735 | encoder.Fill(m_instancerExtents->Slice(0, lowers), | |
| 125 | Ordered(std::numeric_limits<float>::infinity())); | ||
| 126 | 735 | encoder.Fill(m_instancerExtents->Slice(lowers, lowers), | |
| 127 | Ordered(-std::numeric_limits<float>::infinity())); | ||
| 128 | 735 | encoder.Barrier(); | |
| 129 | 735 | encoder.Dispatch(Kernels().Entry("SlotBounds"), params, | |
| 130 | 735 | {.x = device::GroupCount(params.instanceSlots, kBoundsGroup)}); | |
| 131 | 735 | encoder.Barrier(); | |
| 132 | 1470 | encoder.Dispatch(Kernels().Entry("InstancerBounds"), params, | |
| 133 | 735 | {.x = device::GroupCount(params.primCount, kBoundsGroup)}); | |
| 134 | } | ||
| 135 | 735 | encoder.Barrier(); | |
| 136 | 735 | encoder.Dispatch(Kernels().Entry("FinishBoxes"), params, | |
| 137 | 735 | {.x = device::GroupCount(params.slotCount, kBoundsGroup)}); | |
| 138 | 735 | } | |
| 139 | 735 | queue.Submit(encoder.Finish()); | |
| 140 | 1611 | } | |
| 141 | |||
| 142 | 100 | device::Download BoundsTable::ReadWorldsAsync() const { | |
| 143 | 100 | if (!m_boxes.Buffer()) { | |
| 144 | ✗ | return {}; | |
| 145 | } | ||
| 146 | 100 | return m_structure.GetColumnStore().GetDevice().GetQueue().ReadAsync( | |
| 147 | 200 | m_boxes.Buffer()->Slice(0, uint64_t(m_boxes.Rows()) * sizeof(BvhBox))); | |
| 148 | } | ||
| 149 | |||
| 150 | ✗ | device::Download BoundsTable::ReadWorldAsync(uint32_t world) const { | |
| 151 | ✗ | if (!m_boxes.Buffer()) { | |
| 152 | ✗ | return {}; | |
| 153 | } | ||
| 154 | ✗ | const uint32_t slot = WorldSlot(world); | |
| 155 | ✗ | if (slot >= m_boxes.Rows()) { | |
| 156 | ✗ | throw Error("The scene has no world {}", world); | |
| 157 | } | ||
| 158 | ✗ | return m_structure.GetColumnStore().GetDevice().GetQueue().ReadAsync( | |
| 159 | ✗ | m_boxes.Buffer()->Slice(uint64_t(slot) * sizeof(BvhBox), sizeof(BvhBox))); | |
| 160 | } | ||
| 161 | |||
| 162 | 114 | std::optional<BvhBox> BoundsTable::World(uint32_t world) const { | |
| 163 | 114 | if (!m_boxes.Buffer()) { | |
| 164 | ✗ | return std::nullopt; | |
| 165 | } | ||
| 166 | 114 | const uint32_t slot = WorldSlot(world); | |
| 167 | 228 | if (slot >= m_boxes.Rows()) { | |
| 168 | 1 | throw Error("The scene has no world {}", world); | |
| 169 | } | ||
| 170 | 113 | BvhBox box{}; | |
| 171 | 113 | m_structure.GetColumnStore().GetDevice().GetQueue().Read( | |
| 172 | 113 | m_boxes.Buffer()->Slice(uint64_t(slot) * sizeof(BvhBox), sizeof(BvhBox)), &box); | |
| 173 | 113 | if (box.lower[0] > box.upper[0]) { | |
| 174 | 1 | return std::nullopt; | |
| 175 | } | ||
| 176 | 112 | return box; | |
| 177 | } | ||
| 178 | |||
| 179 | 735 | void BoundsTable::Describe(RenderTables& tables) const { | |
| 180 | 735 | tables.primBounds.address = m_primBounds.Address(); | |
| 181 | 735 | } | |
| 182 | |||
| 183 | 353 | void BoundsTable::Prepare() { | |
| 184 | 353 | Kernels(); | |
| 185 | 353 | } | |
| 186 | |||
| 187 | 3293 | device::Program& BoundsTable::Kernels() { | |
| 188 | 3293 | if (!m_program) { | |
| 189 | 1059 | m_program = m_structure.GetColumnStore().GetDevice().CreateProgram( | |
| 190 | {.modules = {"chrono_batch_render.scene.tables.kernels.bounds"}, | ||
| 191 | .library = &kSceneKernels, | ||
| 192 | .entries = {"PrimBounds", "SlotBounds", "InstancerBounds", "FinishBoxes"}, | ||
| 193 | 706 | .label = "bounds"}); | |
| 194 | } | ||
| 195 | 3293 | return *m_program; | |
| 196 | 706 | } | |
| 197 | |||
| 198 | } | ||
| 199 |