Pulp 1.88.0
Cross-platform audio plugin framework
Loading...
Searching...
No Matches
texture_atlas.hpp
Go to the documentation of this file.
1#pragma once
2
3#include <cstddef>
4#include <cstdint>
5#include <unordered_map>
6#include <vector>
7
8namespace pulp::render {
9
13public:
14 struct Region {
15 int x = 0, y = 0, w = 0, h = 0;
16 };
17
18 explicit AtlasPacker(int width, int height)
19 : width_(width), height_(height), shelf_y_(0), shelf_h_(0), shelf_x_(0) {}
20
22 bool allocate(int w, int h, Region& out);
23
24 void reset() { shelf_y_ = 0; shelf_h_ = 0; shelf_x_ = 0; }
25 int width() const { return width_; }
26 int height() const { return height_; }
27
30 std::size_t capacity() const {
31 if (width_ <= 0 || height_ <= 0) return 0;
32 return static_cast<std::size_t>(width_) *
33 static_cast<std::size_t>(height_);
34 }
35
42 std::size_t used_area() const {
43 if (width_ <= 0 || height_ <= 0 ||
44 (shelf_y_ <= 0 && shelf_x_ <= 0)) {
45 return 0;
46 }
47 const std::size_t completed =
48 static_cast<std::size_t>(shelf_y_) *
49 static_cast<std::size_t>(width_);
50 const std::size_t in_progress =
51 static_cast<std::size_t>(shelf_x_) *
52 static_cast<std::size_t>(shelf_h_);
53 return completed + in_progress;
54 }
55
58 float occupancy() const {
59 const std::size_t cap = capacity();
60 if (cap == 0) return 0.0f;
61 return static_cast<float>(used_area()) / static_cast<float>(cap);
62 }
63
64private:
65 int width_, height_;
66 int shelf_y_, shelf_h_, shelf_x_;
67};
68
72public:
73 explicit ImageAtlas(int size = 2048) : packer_(size, size) {}
74
75 struct Entry {
77 uint64_t ref_count = 1;
78 uint64_t last_used_frame = 0;
79 };
80
82 bool allocate(uint64_t key, int w, int h, AtlasPacker::Region& out);
83
84 void release(uint64_t key);
85
87 size_t evict_stale(uint64_t current_frame, uint64_t max_age = 120);
88
89 void mark_used(uint64_t key, uint64_t frame);
90
91 size_t entry_count() const { return entries_.size(); }
92
94 int width() const { return packer_.width(); }
95 int height() const { return packer_.height(); }
96 float occupancy() const { return packer_.occupancy(); }
97
98private:
99 AtlasPacker packer_;
100 std::unordered_map<uint64_t, Entry> entries_;
101};
102
106public:
107 struct Entry {
108 int row = 0;
109 uint64_t last_used = 0;
110 };
111
112 bool has(uint64_t key) const { return entries_.find(key) != entries_.end(); }
113
114 bool allocate(uint64_t key, int& row);
115
116 void mark_used(uint64_t key, uint64_t frame);
117
118 size_t evict_stale(uint64_t current_frame, uint64_t max_age = 120);
119
120 size_t entry_count() const { return entries_.size(); }
121
126 int row_capacity() const { return max_rows_; }
127 int rows_used() const { return next_row_; }
128 float occupancy() const {
129 if (max_rows_ <= 0) return 0.0f;
130 return static_cast<float>(next_row_) / static_cast<float>(max_rows_);
131 }
132
133private:
134 std::unordered_map<uint64_t, Entry> entries_;
135 int next_row_ = 0;
136 int max_rows_ = 512;
137};
138
143public:
144 struct Entry {
146 uint64_t last_used = 0;
147 };
148
149 explicit GlyphAtlas(int size = 1024) : packer_(size, size) {}
150
151 bool allocate(uint64_t glyph_key, int w, int h, AtlasPacker::Region& out);
152
153 void mark_used(uint64_t key, uint64_t frame);
154
155 size_t evict_stale(uint64_t current_frame, uint64_t max_age = 300);
156
157 size_t entry_count() const { return entries_.size(); }
158
160 int width() const { return packer_.width(); }
161 int height() const { return packer_.height(); }
162 float occupancy() const { return packer_.occupancy(); }
163
164private:
165 AtlasPacker packer_;
166 std::unordered_map<uint64_t, Entry> entries_;
167};
168
172public:
173 struct Entry {
175 uint64_t last_used = 0;
176 };
177
178 explicit PathAtlas(int size = 2048) : packer_(size, size) {}
179
180 bool allocate(uint64_t path_hash, int w, int h, AtlasPacker::Region& out);
181
182 void mark_used(uint64_t key, uint64_t frame);
183
184 size_t evict_stale(uint64_t current_frame, uint64_t max_age = 600);
185
186 size_t entry_count() const { return entries_.size(); }
187
189 int width() const { return packer_.width(); }
190 int height() const { return packer_.height(); }
191 float occupancy() const { return packer_.occupancy(); }
192
193private:
194 AtlasPacker packer_;
195 std::unordered_map<uint64_t, Entry> entries_;
196};
197
199template <typename T>
201public:
202 std::vector<T> acquire() {
203 if (!pool_.empty()) {
204 auto v = std::move(pool_.back());
205 pool_.pop_back();
206 v.clear();
207 return v;
208 }
209 return {};
210 }
211
212 void release(std::vector<T> v) {
213 if (pool_.size() < max_pool_size_) {
214 pool_.push_back(std::move(v));
215 }
216 }
217
218 size_t pool_size() const { return pool_.size(); }
219
220private:
221 std::vector<std::vector<T>> pool_;
222 size_t max_pool_size_ = 32;
223};
224
225} // namespace pulp::render
Simple rectangle packer for atlas allocation.
Definition texture_atlas.hpp:12
std::size_t used_area() const
Texels consumed by the shelf allocator so far.
Definition texture_atlas.hpp:42
std::size_t capacity() const
Area of the atlas surface (width × height), in texels.
Definition texture_atlas.hpp:30
AtlasPacker(int width, int height)
Definition texture_atlas.hpp:18
void reset()
Definition texture_atlas.hpp:24
int width() const
Definition texture_atlas.hpp:25
float occupancy() const
Fraction of the atlas the shelf allocator has consumed, in [0, 1].
Definition texture_atlas.hpp:58
int height() const
Definition texture_atlas.hpp:26
bool allocate(int w, int h, Region &out)
Try to allocate a region of the given size. Returns false if full.
Buffer pool — reuses std::vector allocations in hot rendering paths.
Definition texture_atlas.hpp:200
std::vector< T > acquire()
Definition texture_atlas.hpp:202
void release(std::vector< T > v)
Definition texture_atlas.hpp:212
size_t pool_size() const
Definition texture_atlas.hpp:218
int height() const
Definition texture_atlas.hpp:161
float occupancy() const
Definition texture_atlas.hpp:162
GlyphAtlas(int size=1024)
Definition texture_atlas.hpp:149
bool allocate(uint64_t glyph_key, int w, int h, AtlasPacker::Region &out)
int width() const
Read-only introspection for the inspector's atlas viewer.
Definition texture_atlas.hpp:160
size_t entry_count() const
Definition texture_atlas.hpp:157
size_t evict_stale(uint64_t current_frame, uint64_t max_age=300)
void mark_used(uint64_t key, uint64_t frame)
Gradient atlas — caches evaluated gradient ramps as GPU textures.
Definition texture_atlas.hpp:105
void mark_used(uint64_t key, uint64_t frame)
size_t entry_count() const
Definition texture_atlas.hpp:120
float occupancy() const
Definition texture_atlas.hpp:128
int row_capacity() const
Read-only introspection for the inspector's atlas viewer.
Definition texture_atlas.hpp:126
bool allocate(uint64_t key, int &row)
bool has(uint64_t key) const
Definition texture_atlas.hpp:112
size_t evict_stale(uint64_t current_frame, uint64_t max_age=120)
int rows_used() const
Definition texture_atlas.hpp:127
bool allocate(uint64_t key, int w, int h, AtlasPacker::Region &out)
Try to allocate space for an image. Returns the region, or empty on failure.
float occupancy() const
Definition texture_atlas.hpp:96
void mark_used(uint64_t key, uint64_t frame)
int width() const
Read-only introspection for the inspector's atlas viewer.
Definition texture_atlas.hpp:94
void release(uint64_t key)
size_t evict_stale(uint64_t current_frame, uint64_t max_age=120)
Evict entries not used since the given frame.
size_t entry_count() const
Definition texture_atlas.hpp:91
ImageAtlas(int size=2048)
Definition texture_atlas.hpp:73
int height() const
Definition texture_atlas.hpp:95
size_t entry_count() const
Definition texture_atlas.hpp:186
size_t evict_stale(uint64_t current_frame, uint64_t max_age=600)
int width() const
Read-only introspection for the inspector's atlas viewer.
Definition texture_atlas.hpp:189
bool allocate(uint64_t path_hash, int w, int h, AtlasPacker::Region &out)
PathAtlas(int size=2048)
Definition texture_atlas.hpp:178
void mark_used(uint64_t key, uint64_t frame)
float occupancy() const
Definition texture_atlas.hpp:191
int height() const
Definition texture_atlas.hpp:190
Definition gpu_fwd.hpp:31
Definition texture_atlas.hpp:14
int x
Definition texture_atlas.hpp:15
int y
Definition texture_atlas.hpp:15
int h
Definition texture_atlas.hpp:15
int w
Definition texture_atlas.hpp:15
Definition texture_atlas.hpp:144
AtlasPacker::Region region
Definition texture_atlas.hpp:145
uint64_t last_used
Definition texture_atlas.hpp:146
Definition texture_atlas.hpp:107
uint64_t last_used
Definition texture_atlas.hpp:109
int row
Row in the atlas texture.
Definition texture_atlas.hpp:108
Definition texture_atlas.hpp:75
uint64_t last_used_frame
Definition texture_atlas.hpp:78
uint64_t ref_count
Definition texture_atlas.hpp:77
AtlasPacker::Region region
Definition texture_atlas.hpp:76
Definition texture_atlas.hpp:173
uint64_t last_used
Definition texture_atlas.hpp:175
AtlasPacker::Region region
Definition texture_atlas.hpp:174