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 <type_traits>
7#include <unordered_map>
8#include <vector>
9
10namespace unravel
11{
12
14{
15 uint32_t total_batches = 0;
16 uint32_t total_instances = 0;
17 uint32_t draw_calls_saved = 0;
18 float collection_time_ms = 0.0f;
19 float preparation_time_ms = 0.0f;
20 float submission_time_ms = 0.0f;
22 float average_batch_size = 0.0f;
23 float batching_efficiency = 0.0f;
24 uint32_t split_batches = 0;
25
26 void reset();
28 auto to_string() const -> std::string;
29};
30
32{
33 uint16_t view_id = 0;
34 math::vec3 camera_position = math::vec3(0.0f);
35 bool enable_distance_sorting = false;
36 uint32_t max_instances_per_batch = 1024;
37 bool enable_profiling = true;
38};
39
40template<typename Key>
42{
43 Key key;
45 float camera_distance = 0.0f;
46 bool is_split_batch = false;
47
48 batch_group_t() = default;
49 explicit batch_group_t(const Key& key);
50
51 void add_instance(const batch_instance& instance);
52 void calculate_camera_distance(const math::vec3& camera_pos);
53 auto is_valid() const -> bool;
54 auto get_gpu_memory_size() const -> size_t;
55};
56
58
59namespace batch_collector_detail
60{
61inline auto static_mesh_batching_enabled() -> bool&
62{
63 static bool enabled = true;
64 return enabled;
65}
66} // namespace batch_collector_detail
67
68template<typename Key>
70{
71public:
72 using batch_map_t = std::unordered_map<Key, batch_group_t<Key>>;
73 using batch_list_t = std::vector<batch_group_t<Key>*>;
74
76
77 static auto is_static_mesh_batching_enabled() -> bool
78 {
79 return batch_collector_detail::static_mesh_batching_enabled();
80 }
81
83 {
84 batch_collector_detail::static_mesh_batching_enabled() = enabled;
85 }
86
87 ~batch_collector_t() = default;
88
91 batch_collector_t(batch_collector_t&&) noexcept = default;
92 auto operator=(batch_collector_t&&) noexcept -> batch_collector_t& = default;
93
94 void collect_renderable(const Key& key, const batch_instance& instance);
95 void collect_renderable(const Key& key, const math::mat4& world_transform);
96 void prepare_batches(const submit_context& context);
97 auto get_prepared_batches() const -> const batch_list_t&;
98 void clear();
99 auto get_stats() const -> const batch_stats&;
100 auto get_batch_count() const -> size_t;
101 auto get_instance_count() const -> size_t;
102 auto has_batches() const -> bool;
103 void set_max_instances_per_batch(uint32_t max_instances);
104 void set_profiling_enabled(bool enabled);
105
106private:
107 batch_map_t batch_groups_;
108 batch_list_t prepared_batches_;
109 batch_stats stats_;
110 uint32_t max_instances_per_batch_ = 1024;
111 bool profiling_enabled_ = true;
112
113 void sort_batches(const submit_context& context);
114 void split_large_batches(const submit_context& context);
115 void calculate_camera_distances(const math::vec3& camera_pos);
116 void update_statistics();
117 auto get_or_create_batch_group(const Key& key) -> batch_group_t<Key>&;
118};
119
122
123} // namespace unravel
124
125#include "batch_collector.hpp"
std::vector< batch_group_t< Key > * > batch_list_t
static auto is_static_mesh_batching_enabled() -> bool
batch_collector_t(const batch_collector_t &)=delete
std::unordered_map< Key, batch_group_t< Key > > batch_map_t
batch_collector_t(batch_collector_t &&) noexcept=default
auto operator=(const batch_collector_t &) -> batch_collector_t &=delete
static void set_static_mesh_batching_enabled(bool enabled)
Collection of batch instances with utilities.
Definition bbox.cpp:5
Hash specialization for batch_key to enable use in std::unordered_map.
auto static_mesh_batching_enabled() -> bool &
batch_instance_collection instances
Instance data for a single object in a batch.
Batch key structure for grouping compatible draw calls.
Definition batch_key.h:28
auto to_string() const -> std::string
Batch key for shadow depth passes — geometry-first, optional cutout bucket.
Definition batch_key.h:119
bool enabled