stage/views/poses.cpp
| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | // SPDX-License-Identifier: BSD-3-Clause | ||
| 2 | #include "chrono_batch_render/stage/views/poses.h" | ||
| 3 | |||
| 4 | #include <utility> | ||
| 5 | |||
| 6 | #include "chrono_batch_render/device/device.h" | ||
| 7 | #include "chrono_batch_render/device/queue.h" | ||
| 8 | #include "chrono_batch_render/scene/schema/columns.h" | ||
| 9 | #include "chrono_batch_render/stage/columns/column_store.h" | ||
| 10 | #include "chrono_batch_render/stage/columns/kernels/cells.h" | ||
| 11 | #include "chrono_batch_render/stage/views/layout.h" | ||
| 12 | #include "chrono_batch_render/stage/views/staged.h" | ||
| 13 | #include "chrono_batch_render/stage/views/view_state.h" | ||
| 14 | #include "chrono_batch_render/stage/views/world_poses.h" | ||
| 15 | #include "chrono_batch_render/util/profiler.h" | ||
| 16 | |||
| 17 | namespace chrono::batch_render { | ||
| 18 | |||
| 19 | namespace { | ||
| 20 | |||
| 21 | 83233 | uint32_t SourceRows(const DLTensor& tensor, int64_t width, const char* what) { | |
| 22 | 83233 | const bool lanes = tensor.ndim == 1 && tensor.dtype.lanes == width; | |
| 23 | 166466 | const bool wide = | |
| 24 | 83233 | tensor.ndim >= 2 && tensor.shape[tensor.ndim - 1] == width && tensor.dtype.lanes == 1; | |
| 25 | 83233 | if (!wide && !lanes) { | |
| 26 | ✗ | throw Error("{} are written from [..., {}] tensors", what, width); | |
| 27 | } | ||
| 28 | 83233 | if (tensor.dtype.code != kDLFloat || (tensor.dtype.bits != 32 && tensor.dtype.bits != 64)) { | |
| 29 | ✗ | throw Error("{} are float32 or float64, not {}-bit code {}", what, tensor.dtype.bits, | |
| 30 | ✗ | tensor.dtype.code); | |
| 31 | } | ||
| 32 | 83233 | if (tensor.strides != nullptr) { | |
| 33 | 1138 | int64_t expected = 1; | |
| 34 | 3414 | for (int dim = tensor.ndim - 1; dim >= 0; --dim) { | |
| 35 | 2276 | if (tensor.shape[dim] != 1 && tensor.strides[dim] != expected) { | |
| 36 | ✗ | throw UnsupportedError("{} are written from contiguous tensors only", what); | |
| 37 | } | ||
| 38 | 2276 | expected *= tensor.shape[dim]; | |
| 39 | } | ||
| 40 | } | ||
| 41 | uint64_t rows = 1; | ||
| 42 | 332932 | for (int dim = 0; dim < tensor.ndim - (lanes ? 0 : 1); ++dim) { | |
| 43 | 83233 | rows *= uint64_t(tensor.shape[dim]); | |
| 44 | } | ||
| 45 | 83233 | return uint32_t(rows); | |
| 46 | } | ||
| 47 | |||
| 48 | 137025 | void CheckOwn(device::Device& device, const device::Tensor& tensor, const char* what) { | |
| 49 | 137025 | if (&tensor.GetDevice() != &device) { | |
| 50 | ✗ | throw Error("{} are on {}, not the scene's {}; Device::Copy or Device::Import moves them", | |
| 51 | ✗ | what, tensor.GetDevice().Adapter().alias, device.Adapter().alias); | |
| 52 | } | ||
| 53 | 137025 | const Span<const int64_t> shape = tensor.Shape(); | |
| 54 | 137025 | const Span<const int64_t> strides = tensor.Strides(); | |
| 55 | 137025 | int64_t packed = 1; | |
| 56 | 411072 | for (size_t axis = shape.size(); axis-- > 0;) { | |
| 57 | 274047 | if (!strides.empty() && shape[axis] > 1 && strides[axis] != packed) { | |
| 58 | ✗ | throw UnsupportedError("{} are written from contiguous tensors only", what); | |
| 59 | } | ||
| 60 | 274047 | packed *= shape[axis]; | |
| 61 | } | ||
| 62 | 137025 | } | |
| 63 | |||
| 64 | 137023 | uint32_t TensorRows(device::Device& device, const device::Tensor& tensor, int64_t width, | |
| 65 | const char* what) { | ||
| 66 | 137023 | const Span<const int64_t> shape = tensor.Shape(); | |
| 67 | 137023 | if (shape.size() < 2 || shape.back() != width) { | |
| 68 | 1 | throw Error("{} are written from [..., {}] tensors", what, width); | |
| 69 | } | ||
| 70 | 137022 | if (tensor.Type() != device::DType::Float32 && tensor.Type() != device::DType::Float64) { | |
| 71 | ✗ | throw Error("{} are float32 or float64", what); | |
| 72 | } | ||
| 73 | 137022 | CheckOwn(device, tensor, what); | |
| 74 | uint64_t rows = 1; | ||
| 75 | 274044 | for (size_t dim = 0; dim + 1 < shape.size(); ++dim) { | |
| 76 | 137022 | rows *= uint64_t(shape[dim]); | |
| 77 | } | ||
| 78 | 137022 | return uint32_t(rows); | |
| 79 | } | ||
| 80 | |||
| 81 | } | ||
| 82 | |||
| 83 | 35466 | Poses::Poses(std::shared_ptr<ViewState> view) | |
| 84 | 141864 | : m_translate(view, kXformableTranslate), m_orient(std::move(view), kXformableOrient) {} | |
| 85 | |||
| 86 | 2217 | device::Operation Poses::Write(const DLTensor& source, Stream producer, QuaternionOrder order, | |
| 87 | const DLTensor* rows, const DLTensor* mask) { | ||
| 88 | 2217 | SourceRows(source, kPoseWords, "Poses"); | |
| 89 | 2217 | device::Device& device = m_translate.GetColumnStore().GetDevice(); | |
| 90 | 2217 | const StoreLock* store = m_translate.GetColumnStore().Lock(); | |
| 91 | 2217 | std::optional<StoreLock::Guard> lock = | |
| 92 | 2217 | store ? store->TryLock() : std::optional<StoreLock::Guard>(std::in_place); | |
| 93 | 2217 | if (!lock) { | |
| 94 | 1071 | std::optional<StagedTensor> values; | |
| 95 | 1071 | std::optional<StagedTensor> stagedRows; | |
| 96 | 1071 | std::optional<StagedTensor> stagedMask; | |
| 97 | 3213 | if (store->CanDefer() && StageOptional(device, &source, producer, values) && | |
| 98 | 3213 | StageOptional(device, rows, producer, stagedRows) && | |
| 99 | 1071 | StageOptional(device, mask, producer, stagedMask)) { | |
| 100 | 2142 | store->Defer([poses = *this, values = std::move(*values), order, | |
| 101 | stagedRows = std::move(stagedRows), | ||
| 102 | stagedMask = std::move(stagedMask)]() mutable { | ||
| 103 | 1071 | DLTensor rowView; | |
| 104 | 1071 | DLTensor maskView; | |
| 105 | 1071 | const DLTensor written = values.View(); | |
| 106 | 1071 | poses.Write(written, values.Producer(), order, ViewOptional(stagedRows, rowView), | |
| 107 | 1071 | ViewOptional(stagedMask, maskView)); | |
| 108 | 1071 | }); | |
| 109 | 1071 | return {}; | |
| 110 | } | ||
| 111 | ✗ | lock = store->Lock(); | |
| 112 | 1071 | } | |
| 113 | 1147 | return Write(device.Import(source, producer, "pose staging"), producer, order, | |
| 114 | 2294 | rows ? device.Import(*rows, producer, "pose rows") : device::Tensor(), | |
| 115 | 2296 | mask ? device.Import(*mask, producer, "pose mask") : device::Tensor()); | |
| 116 | 2216 | } | |
| 117 | |||
| 118 | 40508 | device::Operation Poses::Write(const DLTensor& translation, const DLTensor& rotation, | |
| 119 | Stream producer, QuaternionOrder order, const DLTensor* rows, | ||
| 120 | const DLTensor* mask) { | ||
| 121 | 40508 | if (SourceRows(translation, 3, "Translations") != SourceRows(rotation, 4, "Rotations") || | |
| 122 | 40508 | translation.dtype.bits != rotation.dtype.bits) { | |
| 123 | ✗ | throw Error("Translations and rotations need as many rows and the same precision"); | |
| 124 | } | ||
| 125 | 40508 | device::Device& device = m_translate.GetColumnStore().GetDevice(); | |
| 126 | 40508 | const StoreLock* store = m_translate.GetColumnStore().Lock(); | |
| 127 | 40508 | std::optional<StoreLock::Guard> lock = | |
| 128 | 40508 | store ? store->TryLock() : std::optional<StoreLock::Guard>(std::in_place); | |
| 129 | 40508 | if (!lock) { | |
| 130 | 6565 | std::optional<StagedTensor> translations; | |
| 131 | 6565 | std::optional<StagedTensor> rotations; | |
| 132 | 6565 | std::optional<StagedTensor> stagedRows; | |
| 133 | 6565 | std::optional<StagedTensor> stagedMask; | |
| 134 | 19651 | if (store->CanDefer() && StageOptional(device, &translation, producer, translations) && | |
| 135 | 13086 | StageOptional(device, &rotation, producer, rotations) && | |
| 136 | 19651 | StageOptional(device, rows, producer, stagedRows) && | |
| 137 | 6543 | StageOptional(device, mask, producer, stagedMask)) { | |
| 138 | 13086 | store->Defer([poses = *this, translations = std::move(*translations), | |
| 139 | 6543 | rotations = std::move(*rotations), order, | |
| 140 | stagedRows = std::move(stagedRows), | ||
| 141 | stagedMask = std::move(stagedMask)]() mutable { | ||
| 142 | 6543 | DLTensor rowView; | |
| 143 | 6543 | DLTensor maskView; | |
| 144 | 6543 | const DLTensor moved = translations.View(); | |
| 145 | 6543 | const DLTensor turned = rotations.View(); | |
| 146 | 6543 | poses.Write(moved, turned, translations.Producer(), order, | |
| 147 | 6543 | ViewOptional(stagedRows, rowView), ViewOptional(stagedMask, maskView)); | |
| 148 | 6543 | }); | |
| 149 | 6543 | return {}; | |
| 150 | } | ||
| 151 | 22 | lock = store->Lock(); | |
| 152 | 6565 | } | |
| 153 | 33965 | return Write(device.Import(translation, producer, "translation staging"), | |
| 154 | 67930 | device.Import(rotation, producer, "rotation staging"), producer, order, | |
| 155 | 67931 | rows ? device.Import(*rows, producer, "pose rows") : device::Tensor(), | |
| 156 | 67930 | mask ? device.Import(*mask, producer, "pose mask") : device::Tensor()); | |
| 157 | 40508 | } | |
| 158 | |||
| 159 | 1147 | device::Operation Poses::Write(const device::Tensor& source, Stream producer, QuaternionOrder order, | |
| 160 | const device::Tensor& rows, const device::Tensor& mask) { | ||
| 161 | 1147 | return Dispatch(source, nullptr, producer, order, rows, mask); | |
| 162 | } | ||
| 163 | |||
| 164 | 33966 | device::Operation Poses::Write(const device::Tensor& translation, const device::Tensor& rotation, | |
| 165 | Stream producer, QuaternionOrder order, const device::Tensor& rows, | ||
| 166 | const device::Tensor& mask) { | ||
| 167 | 33966 | device::Device& device = m_translate.GetColumnStore().GetDevice(); | |
| 168 | 33966 | if (TensorRows(device, translation, 3, "Translations") != | |
| 169 | 33965 | TensorRows(device, rotation, 4, "Rotations") || | |
| 170 | 33965 | translation.Type() != rotation.Type()) { | |
| 171 | ✗ | throw Error("Translations and rotations need as many rows and the same precision"); | |
| 172 | } | ||
| 173 | 33965 | return Dispatch(translation, &rotation, producer, order, rows, mask); | |
| 174 | } | ||
| 175 | |||
| 176 | 35126 | void Poses::CheckSources(const device::Tensor& values, const device::Tensor* rotations, | |
| 177 | const device::Tensor& rows) const { | ||
| 178 | 35126 | device::Device& device = m_translate.GetColumnStore().GetDevice(); | |
| 179 | 35126 | const uint32_t width = rotations ? 3 : kPoseWords; | |
| 180 | 35126 | const uint32_t sourceRows = TensorRows(device, values, width, "Poses"); | |
| 181 | 35126 | if (rotations && (TensorRows(device, *rotations, 4, "Rotations") != sourceRows || | |
| 182 | 33966 | rotations->Type() != values.Type())) { | |
| 183 | ✗ | throw Error("Translations and rotations need as many rows and the same precision"); | |
| 184 | } | ||
| 185 | 35126 | if (!rows.Empty()) { | |
| 186 | 3 | const bool indices = rows.Shape().size() == 1 && (rows.Type() == device::DType::Int32 || | |
| 187 | ✗ | rows.Type() == device::DType::UInt32); | |
| 188 | 3 | if (!indices || uint32_t(rows.Shape()[0]) != Size()) { | |
| 189 | ✗ | throw Error("Pose rows are a contiguous [{}] int32 or uint32 tensor, a source row per " | |
| 190 | "prim", | ||
| 191 | ✗ | Size()); | |
| 192 | } | ||
| 193 | 3 | CheckOwn(device, rows, "Pose rows"); | |
| 194 | 35123 | } else if (sourceRows != Size()) { | |
| 195 | 1 | throw Error("A tensor of {} poses for {} prims", sourceRows, Size()); | |
| 196 | } | ||
| 197 | 35125 | } | |
| 198 | |||
| 199 | 35119 | device::Operation Poses::Dispatch(const device::Tensor& values, const device::Tensor* rotations, | |
| 200 | Stream producer, QuaternionOrder order, | ||
| 201 | const device::Tensor& rows, const device::Tensor& mask) { | ||
| 202 | 35119 | const auto lock = LockStore(m_translate.GetColumnStore().Lock()); | |
| 203 | 35119 | CHRONO_BATCH_RENDER_ZONE("Scene/Write"); | |
| 204 | 35119 | if (Size() == 0) { | |
| 205 | ✗ | return {}; | |
| 206 | } | ||
| 207 | 35119 | ColumnStore& store = m_translate.GetColumnStore(); | |
| 208 | 35119 | device::Device& device = store.GetDevice(); | |
| 209 | 35119 | CheckSources(values, rotations, rows); | |
| 210 | 35118 | const UntypedAttribute::Mask selected = m_translate.Upload(mask); | |
| 211 | 35118 | const bool doubles = values.Type() == device::DType::Float64; | |
| 212 | 35118 | store.Flush(); | |
| 213 | 35118 | device.GetQueue().Follow(producer); | |
| 214 | 35118 | PoseParams params{}; | |
| 215 | 35118 | params.translateStamps = store.Stamps(kXformableTranslate); | |
| 216 | 35118 | params.orientStamps = store.Stamps(kXformableOrient); | |
| 217 | 35118 | params.translate.address = store.Schema(SchemaId::Xformable).Pages(kXformableTranslate.index); | |
| 218 | 35118 | params.orient.address = store.Schema(SchemaId::Xformable).Pages(kXformableOrient.index); | |
| 219 | 35118 | const RowSource rowSource = m_translate.m_view->Source(SchemaId::Xformable); | |
| 220 | 35118 | params.rows.address = rowSource.rows; | |
| 221 | 35118 | params.firstRow = rowSource.firstRow; | |
| 222 | 35118 | params.values.address = values.Address(); | |
| 223 | 35118 | params.rotations.address = rotations ? rotations->Address() : 0; | |
| 224 | 35118 | params.sources.address = rows.Empty() ? 0 : rows.Address(); | |
| 225 | 35118 | params.mask.address = selected.address; | |
| 226 | 35118 | params.count = Size(); | |
| 227 | 35118 | params.doubles = doubles ? 1 : 0; | |
| 228 | 35118 | params.order = uint32_t(order); | |
| 229 | 35118 | store.Touch(kXformableTranslate); | |
| 230 | 35118 | store.Touch(kXformableOrient); | |
| 231 | 35118 | m_translate.StampWorlds(); | |
| 232 | 35118 | const device::Operation written = store.Dispatch("WritePoses", params, params.count); | |
| 233 | 35118 | store.Reassert(kXformableTranslate, m_translate.Rows().rows); | |
| 234 | 35118 | store.Reassert(kXformableOrient, m_translate.Rows().rows); | |
| 235 | 35118 | return written; | |
| 236 | 35120 | } | |
| 237 | |||
| 238 | 115 | device::Tensor Poses::Read(device::DType type, Stream consumer, QuaternionOrder order) const { | |
| 239 | 115 | const auto lock = LockStore(m_translate.GetColumnStore().Lock()); | |
| 240 | 115 | CHRONO_BATCH_RENDER_ZONE("Scene/Read"); | |
| 241 | 115 | if (type != device::DType::Float32 && type != device::DType::Float64) { | |
| 242 | ✗ | throw Error("Poses are gathered as float32 or float64"); | |
| 243 | } | ||
| 244 | 115 | ColumnStore& store = m_translate.GetColumnStore(); | |
| 245 | 115 | device::Device& device = store.GetDevice(); | |
| 246 | 115 | const bool doubles = type == device::DType::Float64; | |
| 247 | 115 | store.Flush(); | |
| 248 | 115 | const Ref<device::Buffer> values = device.CreateBuffer( | |
| 249 | 228 | {.bytes = std::max<uint64_t>(uint64_t(Size()) * kPoseWords * (doubles ? 8 : 4), 4), | |
| 250 | .usage = device::BufferUsage::Interop, | ||
| 251 | 115 | .label = "gathered poses"}); | |
| 252 | 115 | PoseParams params{}; | |
| 253 | 115 | params.translate.address = store.Schema(SchemaId::Xformable).Pages(kXformableTranslate.index); | |
| 254 | 115 | params.orient.address = store.Schema(SchemaId::Xformable).Pages(kXformableOrient.index); | |
| 255 | 115 | const RowSource rowSource = m_translate.m_view->Source(SchemaId::Xformable); | |
| 256 | 115 | params.rows.address = rowSource.rows; | |
| 257 | 115 | params.firstRow = rowSource.firstRow; | |
| 258 | 115 | params.values.address = values->Address(); | |
| 259 | 115 | params.count = Size(); | |
| 260 | 115 | params.doubles = doubles ? 1 : 0; | |
| 261 | 115 | params.order = uint32_t(order); | |
| 262 | 115 | store.Dispatch("ReadPoses", params, params.count).Wait(consumer); | |
| 263 | 460 | return device::Tensor(values, 0, type, {int64_t(Size()), int64_t(kPoseWords)}); | |
| 264 | 460 | } | |
| 265 | |||
| 266 | class Batch { | ||
| 267 | public: | ||
| 268 | 238 | explicit Batch(device::Queue& queue) : m_queue(&queue) { | |
| 269 | 231 | m_queue->BeginBatch(); | |
| 270 | 238 | } | |
| 271 | |||
| 272 | Batch(const Batch&) = delete; | ||
| 273 | Batch& operator=(const Batch&) = delete; | ||
| 274 | |||
| 275 | 238 | ~Batch() { | |
| 276 | 238 | if (m_queue != nullptr) { | |
| 277 | 231 | m_queue->EndBatch(); | |
| 278 | } | ||
| 279 | 238 | } | |
| 280 | |||
| 281 | 7 | device::Operation End() { | |
| 282 | 7 | return std::exchange(m_queue, nullptr)->EndBatch(); | |
| 283 | } | ||
| 284 | |||
| 285 | private: | ||
| 286 | device::Queue* m_queue = nullptr; | ||
| 287 | }; | ||
| 288 | |||
| 289 | 238 | static WorldPoseSource& WorldPoses(const ViewState& view) { | |
| 290 | 238 | WorldPoseSource* source = view.GetStructure().GetWorldPoses(); | |
| 291 | 238 | if (source == nullptr) { | |
| 292 | ✗ | throw UnsupportedError("World poses need a scene, which composes the hierarchy"); | |
| 293 | } | ||
| 294 | 238 | return *source; | |
| 295 | } | ||
| 296 | |||
| 297 | 7 | device::Operation Poses::WriteWorld(const device::Tensor& values, const device::Tensor* rotations, | |
| 298 | Stream producer, QuaternionOrder order, | ||
| 299 | const device::Tensor& rows, const device::Tensor& mask) { | ||
| 300 | 7 | const auto lock = LockStore(m_translate.GetColumnStore().Lock()); | |
| 301 | 7 | CHRONO_BATCH_RENDER_ZONE("Scene/WriteWorld"); | |
| 302 | 7 | CheckSources(values, rotations, rows); | |
| 303 | 7 | if (Size() == 0) { | |
| 304 | ✗ | return {}; | |
| 305 | } | ||
| 306 | 7 | device::Queue& queue = m_translate.GetColumnStore().GetDevice().GetQueue(); | |
| 307 | 7 | queue.Follow(producer); | |
| 308 | 7 | Batch batch(queue); | |
| 309 | 7 | const device::Tensor local = WorldPoses(*m_translate.m_view) | |
| 310 | 7 | .Localize(*m_translate.m_view, values, rotations, order, rows); | |
| 311 | 7 | Dispatch(local, nullptr, Stream::None(), QuaternionOrder::Xyzw, {}, mask); | |
| 312 | 7 | return batch.End(); | |
| 313 | 7 | } | |
| 314 | |||
| 315 | 230 | device::Tensor Poses::ReadWorld(device::DType type, Stream consumer, QuaternionOrder order) const { | |
| 316 | 230 | const auto lock = LockStore(m_translate.GetColumnStore().Lock()); | |
| 317 | 230 | CHRONO_BATCH_RENDER_ZONE("Scene/ReadWorld"); | |
| 318 | 230 | if (type != device::DType::Float32 && type != device::DType::Float64) { | |
| 319 | ✗ | throw Error("World poses are gathered as float32 or float64"); | |
| 320 | } | ||
| 321 | 230 | const Batch batch(m_translate.GetColumnStore().GetDevice().GetQueue()); | |
| 322 | 460 | return WorldPoses(*m_translate.m_view).Read(*m_translate.m_view, type, consumer, order); | |
| 323 | 230 | } | |
| 324 | |||
| 325 | 1 | device::Tensor Poses::ReadWorldScales(device::DType type, Stream consumer) const { | |
| 326 | 1 | const auto lock = LockStore(m_translate.GetColumnStore().Lock()); | |
| 327 | 1 | CHRONO_BATCH_RENDER_ZONE("Scene/ReadWorldScales"); | |
| 328 | 1 | if (type != device::DType::Float32 && type != device::DType::Float64) { | |
| 329 | ✗ | throw Error("World scales are gathered as float32 or float64"); | |
| 330 | } | ||
| 331 | 1 | const Batch batch(m_translate.GetColumnStore().GetDevice().GetQueue()); | |
| 332 | 2 | return WorldPoses(*m_translate.m_view).ReadScales(*m_translate.m_view, type, consumer); | |
| 333 | 1 | } | |
| 334 | |||
| 335 | } | ||
| 336 |