render/renderer_impl.h
| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | // SPDX-License-Identifier: BSD-3-Clause | ||
| 2 | #pragma once | ||
| 3 | |||
| 4 | #include <chrono> | ||
| 5 | |||
| 6 | #include <array> | ||
| 7 | #include <cstdint> | ||
| 8 | #include <list> | ||
| 9 | #include <map> | ||
| 10 | #include <memory> | ||
| 11 | #include <mutex> | ||
| 12 | #include <optional> | ||
| 13 | #include <set> | ||
| 14 | #include <span> | ||
| 15 | #include <string> | ||
| 16 | #include <tuple> | ||
| 17 | #include <vector> | ||
| 18 | |||
| 19 | #include "chrono_batch_render/algorithms/scan.h" | ||
| 20 | #include "chrono_batch_render/device/buffer.h" | ||
| 21 | #include "chrono_batch_render/device/program.h" | ||
| 22 | #include "chrono_batch_render/device/queue.h" | ||
| 23 | #include "chrono_batch_render/material/codegen.h" | ||
| 24 | #include "chrono_batch_render/overlay/overlay_impl.h" | ||
| 25 | #include "chrono_batch_render/render/kernels/render/render.h" | ||
| 26 | #include "chrono_batch_render/render/renderer.h" | ||
| 27 | #include "chrono_batch_render/render/sensor_pipelines.h" | ||
| 28 | #include "chrono_batch_render/scene/scene.h" | ||
| 29 | #include "chrono_batch_render/scene/tables/captures.h" | ||
| 30 | #include "chrono_batch_render/scene/tables/plans.h" | ||
| 31 | #include "chrono_batch_render/scene/tables/sensor_pipelines.h" | ||
| 32 | |||
| 33 | namespace chrono::batch_render { | ||
| 34 | |||
| 35 | class RendererImpl { | ||
| 36 | public: | ||
| 37 | explicit RendererImpl(Ref<Scene> scene, const RendererDesc& desc = {}); | ||
| 38 | |||
| 39 | RenderOutput Step(std::span<const std::string> products, Stream stream = Stream::None()); | ||
| 40 | |||
| 41 | struct OwnedVar { | ||
| 42 | uint32_t product = 0; | ||
| 43 | std::string renderVar; | ||
| 44 | device::Tensor tensor; | ||
| 45 | std::string channel; | ||
| 46 | std::string output; | ||
| 47 | }; | ||
| 48 | |||
| 49 | struct OwnedCloud { | ||
| 50 | uint32_t product = kNoPrim; | ||
| 51 | uint32_t slots = 0; | ||
| 52 | uint32_t index = 0; | ||
| 53 | std::string output; | ||
| 54 | bool world = false; | ||
| 55 | Ref<device::Buffer> cloud; | ||
| 56 | Ref<device::Buffer> records; | ||
| 57 | Ref<device::Buffer> flags; | ||
| 58 | Ref<device::Buffer> offsets; | ||
| 59 | }; | ||
| 60 | |||
| 61 | struct OwnedOverlay { | ||
| 62 | uint32_t product = 0; | ||
| 63 | device::Tensor tensor; | ||
| 64 | uint64_t depth = 0; | ||
| 65 | Ref<device::Buffer> ownDepth; | ||
| 66 | Ref<device::Buffer> primIds; | ||
| 67 | }; | ||
| 68 | |||
| 69 | struct OutputSet { | ||
| 70 | std::array<std::vector<Capture>, kRenderEntryCount> captures; | ||
| 71 | std::array<Ref<device::Buffer>, kRenderEntryCount> captureBuffers; | ||
| 72 | std::vector<OwnedVar> vars; | ||
| 73 | std::vector<OwnedOverlay> overlays; | ||
| 74 | std::vector<OwnedCloud> clouds; | ||
| 75 | std::map<uint32_t, uint64_t> rates; | ||
| 76 | std::vector<Ref<device::Buffer>> rateBuffers; | ||
| 77 | Ref<device::Buffer> arena; | ||
| 78 | bool outlined = false; | ||
| 79 | }; | ||
| 80 | |||
| 81 | struct SensorPipelineRun { | ||
| 82 | const PlannedSensorPipeline* planned = nullptr; | ||
| 83 | std::shared_ptr<const SensorPipelineInstance> instance; | ||
| 84 | std::shared_ptr<const CompiledSensorPipeline> compiled; | ||
| 85 | std::vector<uint32_t> products; | ||
| 86 | }; | ||
| 87 | |||
| 88 | 49 | struct SensorPipelineState { | |
| 89 | uint32_t definition = 0; | ||
| 90 | uint32_t captures = 0; | ||
| 91 | Ref<device::Buffer> state; | ||
| 92 | }; | ||
| 93 | |||
| 94 | struct StepPlan { | ||
| 95 | std::shared_ptr<const ProductPlan> plan; | ||
| 96 | std::optional<OutputSet> set; | ||
| 97 | }; | ||
| 98 | |||
| 99 | StepPlan& PlanProducts(std::span<const std::string> products); | ||
| 100 | |||
| 101 | OutputSet& AcquireSet(StepPlan& plan); | ||
| 102 | uint64_t AcquireCloud(OutputSet& set, uint32_t index, const std::string& path, | ||
| 103 | const ResolvedProduct& product, const ResolvedVar& var, uint64_t next); | ||
| 104 | device::Operation Trace(const ProductPlan& plan, OutputSet& set, const std::set<uint32_t>& due, | ||
| 105 | const Snapshot& snapshot, std::span<const SensorPipelineRun> runs); | ||
| 106 | std::vector<SensorPipelineRun> PrepareRuns(const ProductPlan& plan, | ||
| 107 | const std::set<uint32_t>& due); | ||
| 108 | void RecordRuns(device::CommandEncoder& encoder, const ProductPlan& plan, OutputSet& set, | ||
| 109 | std::span<const SensorPipelineRun> runs, const Snapshot& snapshot); | ||
| 110 | |||
| 111 | Ref<Scene> m_scene; | ||
| 112 | uint64_t m_maxPixelsPerLaunch; | ||
| 113 | Streaming m_streaming = Streaming::Blocking; | ||
| 114 | Ref<Overlays> m_overlays; | ||
| 115 | Ref<device::Program> CreateRenderProgram(const material::MaterialDefinitionSet& definitions, | ||
| 116 | std::vector<std::string> entries) const; | ||
| 117 | uint64_t ProgramHash(const material::MaterialDefinitionSet& definitions) const; | ||
| 118 | void UpdateRenderProgram(); | ||
| 119 | bool WaitsForKernels() const; | ||
| 120 | device::Kernel& RenderEntry(std::string_view name); | ||
| 121 | |||
| 122 | Ref<device::Program> m_renderProgram; | ||
| 123 | uint64_t m_renderHash = 0; | ||
| 124 | Ref<device::Program> m_pendingProgram; | ||
| 125 | material::MaterialDefinitionSetRef m_pendingDefinitions; | ||
| 126 | uint64_t m_pendingHash = 0; | ||
| 127 | std::chrono::steady_clock::time_point m_pendingStart; | ||
| 128 | std::set<std::string, std::less<>> m_usedEntries; | ||
| 129 | uint64_t m_failedHash = 0; | ||
| 130 | |||
| 131 | std::unique_ptr<algorithms::Scan> m_scan; | ||
| 132 | uint32_t m_scanCapacity = 0; | ||
| 133 | std::list<StepPlan> m_plans; | ||
| 134 | |||
| 135 | std::map<uint32_t, std::shared_ptr<const CompiledSensorPipeline>> m_pipelinePrograms; | ||
| 136 | std::set<uint32_t> m_pendingPipelines; | ||
| 137 | std::set<uint32_t> m_failedPipelines; | ||
| 138 | std::map<std::tuple<uint32_t, uint32_t, uint32_t, uint32_t>, SensorPipelineState> | ||
| 139 | m_pipelineStates; | ||
| 140 | |||
| 141 | CaptureSchedule m_captures; | ||
| 142 | double m_lastTime = 0; | ||
| 143 | uint64_t m_lastResets = 0; | ||
| 144 | |||
| 145 | std::mutex m_stepMutex; | ||
| 146 | std::optional<OverlayPass> m_overlayPass; | ||
| 147 | }; | ||
| 148 | |||
| 149 | } | ||
| 150 |