Unravel Engine C++ Reference
Loading...
Searching...
No Matches
batch_collector.h
Go to the documentation of this file.
1#pragma once
2
3#include "batch_key.h"
4#include "batch_instance.h"
5
6#include <unordered_map>
7#include <vector>
8
9namespace unravel
10{
11
16{
18 uint32_t total_batches = 0;
19
21 uint32_t total_instances = 0;
22
24 uint32_t draw_calls_saved = 0;
25
27 float collection_time_ms = 0.0f;
28
30 float preparation_time_ms = 0.0f;
31
33 float submission_time_ms = 0.0f;
34
37
39 float average_batch_size = 0.0f;
40
42 float batching_efficiency = 0.0f;
43
45 uint32_t split_batches = 0;
46
50 void reset();
51
56
61 auto to_string() const -> std::string;
62};
63
68{
70 uint16_t view_id = 0;
71
73 math::vec3 camera_position = math::vec3(0.0f);
74
76 bool enable_distance_sorting = false;
77
79 uint32_t max_instances_per_batch = 1024;
80
82 bool enable_profiling = true;
83};
84
89{
92
95
97 float camera_distance = 0.0f;
98
100 bool is_split_batch = false;
101
105 batch_group() = default;
106
111 explicit batch_group(const batch_key& key);
112
117 void add_instance(const batch_instance& instance);
118
123 void calculate_camera_distance(const math::vec3& camera_pos);
124
129 auto is_valid() const -> bool;
130
135 auto get_gpu_memory_size() const -> size_t;
136};
137
142{
143public:
144 using batch_map_t = std::unordered_map<batch_key, batch_group>;
145 using batch_list_t = std::vector<batch_group*>;
146
151
156 static auto is_static_mesh_batching_enabled() -> bool;
157
162 static void set_static_mesh_batching_enabled(bool enabled);
163
167 ~batch_collector() = default;
168
169 // Non-copyable but movable
171 auto operator=(const batch_collector&) -> batch_collector& = delete;
172 batch_collector(batch_collector&&) noexcept = default;
173 auto operator=(batch_collector&&) noexcept -> batch_collector& = default;
174
180 void collect_renderable(const batch_key& key, const batch_instance& instance);
181
187 void collect_renderable(const batch_key& key, const math::mat4& world_transform);
188
193 void prepare_batches(const submit_context& context);
194
199 auto get_prepared_batches() const -> const batch_list_t&;
200
204 void clear();
205
210 auto get_stats() const -> const batch_stats&;
211
216 auto get_batch_count() const -> size_t;
217
222 auto get_instance_count() const -> size_t;
223
228 auto has_batches() const -> bool;
229
234 void set_max_instances_per_batch(uint32_t max_instances);
235
240 void set_profiling_enabled(bool enabled);
241
242private:
244 batch_map_t batch_groups_;
245
247 batch_list_t prepared_batches_;
248
250 batch_stats stats_;
251
253 uint32_t max_instances_per_batch_ = 1024;
254
256 bool profiling_enabled_ = true;
257
259 static bool enable_static_mesh_batching_;
260
265 void sort_batches(const submit_context& context);
266
271 void split_large_batches(const submit_context& context);
272
277 void calculate_camera_distances(const math::vec3& camera_pos);
278
282 void update_statistics();
283
289 auto get_or_create_batch_group(const batch_key& key) -> batch_group&;
290};
291
292} // namespace unravel
Core batch collection system for grouping compatible draw calls.
batch_collector(const batch_collector &)=delete
std::vector< batch_group * > batch_list_t
std::unordered_map< batch_key, batch_group > batch_map_t
~batch_collector()=default
Destructor.
batch_collector(batch_collector &&) noexcept=default
auto operator=(const batch_collector &) -> batch_collector &=delete
Collection of batch instances with utilities.
Definition bbox.cpp:5
Hash specialization for batch_key to enable use in std::unordered_map.
A group of instances that can be rendered together.
batch_instance_collection instances
Collection of instances in this batch.
batch_group()=default
Default constructor.
batch_key key
Key identifying this batch (mesh, material, LOD, submesh)
Instance data for a single object in a batch.
Batch key structure for grouping compatible draw calls.
Definition batch_key.h:22
Statistics for batch collection and rendering performance.
uint32_t total_instances
Total number of instances across all batches.
uint32_t split_batches
Number of batches that were split due to size limits.
float average_batch_size
Average number of instances per batch.
float batching_efficiency
Batching efficiency (instances / batches)
size_t instance_buffer_memory_used
Memory used for instance buffers (bytes)
float preparation_time_ms
Time spent preparing batches (milliseconds)
auto to_string() const -> std::string
Get string representation for debugging.
void calculate_derived_stats()
Calculate derived statistics (efficiency, averages)
uint32_t draw_calls_saved
Number of draw calls saved by batching (instances - batches)
void reset()
Reset all statistics to zero.
float submission_time_ms
Time spent submitting batches (milliseconds)
uint32_t total_batches
Total number of batches created.
float collection_time_ms
Time spent collecting instances (milliseconds)
Context information for batch submission.