chrono_batch_render/
Coverage:
low: ≥ 0%
medium: ≥ 75.0%
high: ≥ 90.0%
Lines:
34 / 36
94.4%
Branches:
60 / 79
75.9%
Functions:
-
-%

device/commands.h
Line Branch Exec Source
1 // SPDX-License-Identifier: BSD-3-Clause
2 #pragma once
3
4 #include <cstddef>
5 #include <cstdint>
6 #include <memory>
7 #include <optional>
8 #include <span>
9 #include <type_traits>
10 #include <variant>
11 #include <vector>
12
13 #include "chrono_batch_render/device/accel.h"
14 #include "chrono_batch_render/device/buffer.h"
15 #include "chrono_batch_render/device/program.h"
16 #include "chrono_batch_render/device/ref.h"
17 #include "chrono_batch_render/device/texture.h"
18 #include "chrono_batch_render/export.h"
19 #include "chrono_batch_render/util/profiler.h"
20
21 namespace chrono::batch_render::device {
22
23 class Queue;
24 using profiler::ZoneId;
25
26 struct CopyCommand {
27 Buffer* src;
28 uint64_t srcOffset;
29 Buffer* dst;
30 uint64_t dstOffset;
31 uint64_t bytes;
32 };
33
34 struct TextureCopyCommand {
35 Buffer* src;
36 uint64_t srcOffset;
37 Texture* dst;
38 uint32_t level;
39 };
40
41 struct FillCommand {
42 Buffer* dst;
43 uint64_t offset;
44 uint64_t bytes;
45 uint32_t value;
46 };
47
48 struct UpdateCommand {
49 Buffer* dst;
50 uint64_t offset;
51 uint64_t bytes;
52 size_t data;
53 };
54
55 struct DispatchCommand {
56 const Kernel* kernel;
57 Size3 groups;
58 size_t data;
59 uint32_t paramBytes;
60 Buffer* counts = nullptr;
61 uint64_t countsOffset = 0;
62 };
63
64 struct BuildAccelCommand {
65 Accel* dst;
66 AccelInput input;
67 Buffer* scratch;
68 uint64_t scratchOffset;
69 bool update = false;
70 };
71
72 struct BarrierCommand {};
73
74 struct ZoneCommand {
75 uint32_t index;
76 bool begin;
77 };
78
79 using Command = std::variant<CopyCommand, TextureCopyCommand, FillCommand, UpdateCommand,
80 DispatchCommand, BuildAccelCommand, BarrierCommand, ZoneCommand>;
81
82 template <class... Lambdas> struct Overloaded : Lambdas... {
83 using Lambdas::operator()...;
84 };
85
86 struct FinishDesc {
87 bool reusable = false;
88 };
89
90 class CHRONO_BATCH_RENDER_API CommandBuffer {
91 public:
92 CommandBuffer(CommandBuffer&& other) noexcept;
93 CommandBuffer& operator=(CommandBuffer&& other) noexcept;
94 ~CommandBuffer();
95
96 115543 bool Empty() const {
97
4/4
✓ Branch 0 taken 7684 times.
✓ Branch 1 taken 107328 times.
✓ Branch 2 taken 471 times.
✓ Branch 3 taken 55 times.
115543 return m_commands.empty();
98 }
99
100 80101 bool Reusable() const {
101
4/8
<unknown function 100>:
✓ Branch 0 taken 80095 times.
✗ Branch 0 not taken.
✓ Branch 1 taken 4 times.
✗ Branch 1 not taken.
<unknown function 96>:
✗ Branch 0 not taken.
✓ Branch 1 taken 1 time.
✗ Branch 2 not taken.
✓ Branch 3 taken 1 time.
80101 return m_reusable;
102 }
103
104 160209 std::span<const Command> Commands() const {
105 160209 return m_commands;
106 }
107
108 49196 std::span<const ZoneId> Zones() const {
109
1/1
✓ Branch 1 taken 1 time.
49196 return m_zones;
110 }
111
112 ✗ std::span<const std::byte> Data() const {
113 ✗ return m_data;
114 }
115
116 442601 template <class DataCommand> const std::byte* Data(const DataCommand& command) const {
117 442601 return m_data.data() + command.data;
118 }
119
120 private:
121 friend class CommandEncoder;
122 friend class Queue;
123
124 CommandBuffer(Queue& queue, bool own);
125 void Release();
126 void Close();
127 void HoldResources();
128
129 Queue* m_queue;
130 Ref<Device> m_device;
131 std::vector<Command> m_commands;
132 std::vector<std::byte> m_data;
133 std::vector<ZoneId> m_zones;
134 bool m_open = false;
135 bool m_reusable = false;
136 std::vector<Ref<Resource>> m_held;
137 mutable uint64_t m_submission = 0;
138
139
2/2
✓ Branch 2 taken 127290 times.
✓ Branch 3 taken 111262 times.
238556 struct Recorded {
140 std::shared_ptr<void> timestamps;
141 std::shared_ptr<void> native;
142 };
143
144 mutable Recorded m_recorded;
145 };
146
147
16/31
<unknown function 147>:
✓ Branch 1 taken 1 time.
✓ Branch 2 taken 1783 times.
✓ Branch 2 taken 193 times.
✗ Branch 3 not taken.
✗ Branch 3 not taken.
✓ Branch 5 taken 3 times.
✓ Branch 6 taken 234 times.
✗ Branch 7 not taken.
✓ Branch 8 taken 1 time.
✗ Branch 9 not taken.
✓ Branch 9 taken 94 times.
✓ Branch 10 taken 8 times.
✗ Branch 11 not taken.
✓ Branch 13 taken 94 times.
✗ Branch 14 not taken.
✗ Branch 25 not taken.
✗ Branch 26 not taken.
<unknown function 96>:
✓ Branch 2 taken 15 times.
✗ Branch 3 not taken.
✓ Branch 8 taken 26 times.
✗ Branch 9 not taken.
✓ Branch 11 taken 1 time.
✗ Branch 12 not taken.
✓ Branch 14 taken 7 times.
✗ Branch 15 not taken.
✓ Branch 21 taken 1 time.
✗ Branch 22 not taken.
✗ Branch 27 not taken.
✓ Branch 28 taken 1 time.
✓ Branch 34 taken 1 time.
✗ Branch 35 not taken.
66522 class CHRONO_BATCH_RENDER_API CommandEncoder {
148 public:
149 void Copy(BufferRange src, BufferRange dst);
150
151 void Copy(BufferRange src, Texture& dst, uint32_t level);
152
153 void Fill(BufferRange dst, uint32_t value = 0);
154
155 void Update(BufferRange dst, const void* data);
156
157 template <class Params>
158 232582 void Dispatch(Kernel& kernel, const Params& params, Size3 groups,
159 std::optional<BufferRange> counts = std::nullopt) {
160 static_assert(std::is_trivially_copyable_v<Params>, "parameters are copied as bytes");
161
21/21
<unknown function 147>:
✓ Branch 1 taken 44115 times.
✓ Branch 4 taken 2215 times.
✓ Branch 7 taken 2849 times.
✓ Branch 10 taken 1600 times.
✓ Branch 13 taken 424 times.
✓ Branch 15 taken 8 times.
✓ Branch 16 taken 207 times.
✓ Branch 18 taken 8 times.
✓ Branch 19 taken 112 times.
✓ Branch 21 taken 8 times.
✓ Branch 22 taken 112 times.
✓ Branch 24 taken 8 times.
✓ Branch 27 taken 8 times.
<unknown function 158>:
✓ Branch 1 taken 1423 times.
✓ Branch 4 taken 1423 times.
✓ Branch 7 taken 1233 times.
✓ Branch 10 taken 1217 times.
✓ Branch 13 taken 1217 times.
✓ Branch 16 taken 77888 times.
✓ Branch 19 taken 77888 times.
✓ Branch 22 taken 1217 times.
232582 Dispatch(kernel, &params, sizeof(Params), groups, counts);
162 215180 }
163
164 void Dispatch(Kernel& kernel, Size3 groups, std::optional<BufferRange> counts = std::nullopt) {
165 Dispatch(kernel, nullptr, 0, groups, counts);
166 }
167
168 void BuildAccel(Accel& dst, const AccelInput& input, BufferRange scratch, bool update = false);
169
170 void Dispatch(Kernel& kernel, const void* params, size_t bytes, Size3 groups,
171 std::optional<BufferRange> counts = std::nullopt);
172
173 void Concurrent(bool concurrent);
174
175 void Barrier();
176
177 void BeginZone(ZoneId zone);
178
179 void EndZone();
180
181 CommandBuffer Finish(const FinishDesc& desc = {});
182
183 static constexpr uint64_t kMaxUpdateBytes = 65536;
184
185 private:
186 friend class Queue;
187
188 CommandEncoder(Queue& queue, bool own);
189
190 void Append(const CommandBuffer& commands);
191 void Require(const Resource& resource, const char* what) const;
192 size_t Store(const void* data, size_t bytes, size_t alignment);
193 void Record(const Command& command);
194
195 CommandBuffer m_recording;
196 std::vector<uint32_t> m_open;
197 bool m_concurrent = false;
198 bool m_barrier = false;
199 bool m_own;
200 };
201
202 class CHRONO_BATCH_RENDER_API ZoneScope {
203 public:
204 44741 ZoneScope(CommandEncoder& encoder, ZoneId zone) : m_encoder(encoder) {
205
12/12
<unknown function 147>:
✓ Branch 1 taken 39318 times.
✓ Branch 2 taken 15 times.
✓ Branch 4 taken 466 times.
✓ Branch 5 taken 784 times.
✓ Branch 7 taken 1205 times.
✓ Branch 10 taken 7 times.
✓ Branch 13 taken 273 times.
<unknown function 158>:
✓ Branch 1 taken 1376 times.
✓ Branch 4 taken 623 times.
✓ Branch 7 taken 47 times.
✓ Branch 10 taken 586 times.
<unknown function 96>:
✓ Branch 1 taken 1 time.
44741 encoder.BeginZone(zone);
206 44701 }
207
208 ~ZoneScope();
209 ZoneScope(const ZoneScope&) = delete;
210 ZoneScope& operator=(const ZoneScope&) = delete;
211
212 private:
213 CommandEncoder& m_encoder;
214 };
215
216 }
217