render/instances.h
| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | // SPDX-License-Identifier: BSD-3-Clause | ||
| 2 | /// @file instances.h | ||
| 3 | /// @brief The device scene's instances: a row for each prim drawn, naming its transform node, | ||
| 4 | /// its geometry and its shape transform, kept dense on the device for kernels to read. | ||
| 5 | #pragma once | ||
| 6 | |||
| 7 | #include "ernie/device/column.h" | ||
| 8 | #include "ernie/device/staging.h" | ||
| 9 | #include "ernie/graph/graph.h" | ||
| 10 | #include "ernie/render/geometry.h" | ||
| 11 | #include "ernie/util/glm.h" | ||
| 12 | |||
| 13 | #include <array> | ||
| 14 | #include <cstdint> | ||
| 15 | #include <memory> | ||
| 16 | #include <optional> | ||
| 17 | #include <vector> | ||
| 18 | |||
| 19 | namespace ernie { | ||
| 20 | |||
| 21 | /// @brief A row a drawn prim, in device columns: `Nodes` its transform graph slot, `Geometries` its | ||
| 22 | /// geometry, `Shapes` the 3x4 row-major transform from that geometry to the prim, and `Colours` its | ||
| 23 | /// linear RGB albedo. Rows are kept dense by swap-removal, so a row is no stable id; a prim's row | ||
| 24 | /// is found with `Row`. | ||
| 25 | class InstanceTable { | ||
| 26 | public: | ||
| 27 | using Shape = std::array<glm::vec4, 3>; | ||
| 28 | |||
| 29 | /// @param arenas Where the columns live. | ||
| 30 | InstanceTable(DeviceContext& context, Arenas& arenas); | ||
| 31 | InstanceTable(const InstanceTable&) = delete; | ||
| 32 | InstanceTable& operator=(const InstanceTable&) = delete; | ||
| 33 | |||
| 34 | /// @brief Takes in what a geometry update placed and removed. | ||
| 35 | void Apply(const GeometryPool::Changes& changes); | ||
| 36 | /// @brief Adds the pass that uploads changed rows, before the passes that read the columns. | ||
| 37 | void AddUploads(graph::Graph& graph); | ||
| 38 | |||
| 39 | 77 | std::uint32_t Count() const { | |
| 40 | 77 | return static_cast<std::uint32_t>(prims_.size()); | |
| 41 | } | ||
| 42 | /// @return The prim's row, or none when it draws nothing. | ||
| 43 | std::optional<std::uint32_t> Row(PrimId prim) const; | ||
| 44 | 22 | const DeviceColumn& Nodes() const { | |
| 45 | 22 | return *nodes_; | |
| 46 | } | ||
| 47 | 22 | const DeviceColumn& Geometries() const { | |
| 48 | 22 | return *geometries_; | |
| 49 | } | ||
| 50 | 22 | const DeviceColumn& Shapes() const { | |
| 51 | 22 | return *shapes_; | |
| 52 | } | ||
| 53 | 27 | const DeviceColumn& Colours() const { | |
| 54 | 27 | return *colours_; | |
| 55 | } | ||
| 56 | |||
| 57 | private: | ||
| 58 | void Set(std::uint32_t row, const PrimGeometry& placed); | ||
| 59 | void Remove(PrimId prim); | ||
| 60 | /// Calls `f` with each host array kept a row an instance, so they grow and shrink together. | ||
| 61 | 26 | template <typename F> void ForEachHostColumn(F&& f) { | |
| 62 | 26 | f(prims_); | |
| 63 | 26 | f(hostNodes_); | |
| 64 | 26 | f(hostGeometries_); | |
| 65 | 26 | f(hostShapes_); | |
| 66 | 26 | f(hostColours_); | |
| 67 | 26 | } | |
| 68 | |||
| 69 | DeviceContext& context_; | ||
| 70 | Uploader uploader_; | ||
| 71 | // Host copies of the rows, by row. | ||
| 72 | std::vector<PrimId> prims_; | ||
| 73 | std::vector<std::uint32_t> hostNodes_; | ||
| 74 | std::vector<std::uint32_t> hostGeometries_; | ||
| 75 | std::vector<Shape> hostShapes_; | ||
| 76 | std::vector<glm::vec3> hostColours_; | ||
| 77 | /// Each prim's row by its id's slot. | ||
| 78 | std::vector<std::uint32_t> rows_; | ||
| 79 | std::vector<std::uint32_t> dirty_; | ||
| 80 | std::unique_ptr<DeviceColumn> nodes_; | ||
| 81 | std::unique_ptr<DeviceColumn> geometries_; | ||
| 82 | std::unique_ptr<DeviceColumn> shapes_; | ||
| 83 | std::unique_ptr<DeviceColumn> colours_; | ||
| 84 | }; | ||
| 85 | |||
| 86 | } // namespace ernie | ||
| 87 |