GREM 3.12.0
Game/Rendering Engine Modules for C++
Loading...
Searching...
No Matches
ArrayAtlasPacker.hpp
Go to the documentation of this file.
1// SPDX-FileCopyrightText: 2026 Ivar Härnqvist
2// SPDX-License-Identifier: MIT
3
4#ifndef GREM_RESOURCE_ARRAY_ATLAS_PACKER_HPP
5#define GREM_RESOURCE_ARRAY_ATLAS_PACKER_HPP
6
8
14#include <GREM/core/extents.hpp>
16
17namespace grem::resource {
18
28 uint32_t initialResolution = 128;
29
35 uint32_t initialDepth = 1;
36
40 uint32_t padding = 0;
41
47 uint32_t alignment = 1;
48
56 [[nodiscard]] constexpr bool operator==(const ArrayAtlasPackerOptions& other) const = default;
57};
58
64public:
70
77 : resolution(options.initialResolution)
78 , depth(options.initialDepth)
79 , padding(options.padding)
80 , alignment(options.alignment) {
81 GREM_ASSERT(options.initialResolution > 0);
82 GREM_ASSERT(options.initialDepth > 0);
84 }
85
95
102 };
103
116 [[nodiscard]] InsertBoxResult insertBox(Extent3D size) {
117 const Extent3D paddedSize{
118 .width = roundUpToMultiple(size.width + padding * 2, alignment),
119 .height = roundUpToMultiple(size.height + padding * 2, alignment),
120 .depth = size.depth,
121 };
122
123 bool resized = false;
124 while (paddedSize.width > resolution || paddedSize.height > resolution) {
125 if (resolution > Limits<uint32_t>::MAX / GROWTH_FACTOR) {
126 throw std::length_error{"Maximum atlas resolution exceeded."};
127 }
128 resolution *= GROWTH_FACTOR;
129 resized = true;
130 }
131
132 Optional<FindSuitableRowResult> foundRow = findSuitableRow(paddedSize);
133
134 if (!foundRow) {
135 if (const Optional<size_t> foundLayer = findSuitableLayer(paddedSize)) {
136 Layer& layer = layers[*foundLayer];
137 const uint32_t newRowBottom = (layer.rows.empty()) ? uint32_t{0} : layer.rows.back().top;
138 const uint32_t newRowHeight = min(paddedSize.height + paddedSize.height / uint32_t{8}, resolution - newRowBottom);
139 const uint32_t newRowTop = newRowBottom + newRowHeight;
140 foundRow = FindSuitableRowResult{.layerIndex = *foundLayer, .rowIndex = static_cast<uint32_t>(layer.rows.size())};
141 layer.rows.push_back(Row{.top = newRowTop, .width = 0});
142 } else {
143 const uint32_t newRowHeight = min(paddedSize.height + paddedSize.height / uint32_t{8}, resolution);
144 const uint32_t newLayerZBegin = (layers.empty()) ? uint32_t{0} : layers.back().zEnd;
145 const uint32_t newLayerDepth = roundUpToPowerOf2(size.depth);
146 if (newLayerDepth == 0 || newLayerDepth > Limits<uint32_t>::MAX - depth) {
147 throw std::length_error{"Maximum atlas depth exceeded."};
148 }
149 const uint32_t newLayerZEnd = newLayerZBegin + newLayerDepth;
150 if (newLayerZEnd > depth) {
151 depth = newLayerZEnd;
152 resized = true;
153 }
154 const size_t layerIndex = layers.size();
155 layers.push_back(Layer{.zEnd = newLayerZEnd, .rows{Row{.top = newRowHeight, .width = 0}}});
156 foundRow = FindSuitableRowResult{.layerIndex = layerIndex, .rowIndex = 0};
157 }
158 }
159
160 const size_t layerIndex = foundRow->layerIndex;
161 const size_t rowIndex = foundRow->rowIndex;
162
163 Layer& layer = layers[layerIndex];
164 Row& row = layer.rows[rowIndex];
165 const uint32_t rowBottom = (rowIndex == 0) ? uint32_t{0} : layer.rows[rowIndex - 1].top;
166 const uint32_t x = row.width + padding;
167 const uint32_t y = rowBottom + padding;
168 const uint32_t z = (layerIndex == 0) ? uint32_t{0} : layers[layerIndex - 1].zEnd;
169
170 row.width += paddedSize.width;
171
172 return InsertBoxResult{
173 .offset{.x = static_cast<int32_t>(x), .y = static_cast<int32_t>(y), .z = static_cast<int32_t>(z)},
174 .resized = resized,
175 };
176 }
177
183 [[nodiscard]] uint32_t getResolution() const noexcept {
184 return resolution;
185 }
186
192 [[nodiscard]] uint32_t getDepth() const noexcept {
193 return depth;
194 }
195
196private:
197 static constexpr uint32_t GROWTH_FACTOR = 2;
198 static constexpr float MINIMUM_ROW_HEIGHT_RATIO = 0.7f;
199 static constexpr float MINIMUM_LAYER_DEPTH_RATIO = 0.7f;
200
201 struct Row {
202 uint32_t top;
203 uint32_t width;
204 };
205
206 struct Layer {
207 uint32_t zEnd;
208 Buffer<Row> rows;
209 };
210
211 struct FindSuitableRowResult {
212 size_t layerIndex;
213 size_t rowIndex;
214 };
215
216 [[nodiscard]] Optional<FindSuitableRowResult> findSuitableRow(Extent3D paddedSize) const {
217 for (size_t layerIndex = 0; layerIndex < layers.size(); ++layerIndex) {
218 const Layer& layer = layers[layerIndex];
219 const uint32_t layerDepth = (layerIndex == 0) ? layer.zEnd : layer.zEnd - layers[layerIndex - 1].zEnd;
220 if (const float depthRatio = static_cast<float>(paddedSize.depth) / static_cast<float>(layerDepth);
221 depthRatio >= MINIMUM_LAYER_DEPTH_RATIO && paddedSize.depth <= layerDepth) {
222 for (size_t rowIndex = 0; rowIndex < layer.rows.size(); ++rowIndex) {
223 const Row& row = layer.rows[rowIndex];
224 const uint32_t rowHeight = (rowIndex == 0) ? row.top : row.top - layer.rows[rowIndex - 1].top;
225 if (const float heightRatio = static_cast<float>(paddedSize.height) / static_cast<float>(rowHeight);
226 heightRatio >= MINIMUM_ROW_HEIGHT_RATIO && paddedSize.height <= rowHeight && paddedSize.width <= resolution - row.width) {
227 return FindSuitableRowResult{.layerIndex = layerIndex, .rowIndex = rowIndex};
228 }
229 }
230 }
231 }
232 return {};
233 }
234
235 [[nodiscard]] Optional<size_t> findSuitableLayer(Extent3D paddedSize) const {
236 for (size_t layerIndex = 0; layerIndex < layers.size(); ++layerIndex) {
237 const Layer& layer = layers[layerIndex];
238 const uint32_t layerDepth = (layerIndex == 0) ? layer.zEnd : layer.zEnd - layers[layerIndex - 1].zEnd;
239 if (const float depthRatio = static_cast<float>(paddedSize.depth) / static_cast<float>(layerDepth);
240 depthRatio >= MINIMUM_LAYER_DEPTH_RATIO && paddedSize.depth <= layerDepth) {
241 if (layer.rows.empty() || paddedSize.height <= resolution - layer.rows.back().top) {
242 return layerIndex;
243 }
244 }
245 }
246 return {};
247 }
248
249 ArrayList<Layer> layers{};
250 uint32_t resolution;
251 uint32_t depth;
252 uint32_t padding;
253 uint32_t alignment;
254};
255
256} // namespace grem::resource
257
258#endif
#define GREM_ASSERT(expression)
Definition assertions.hpp:77
Definition Buffer.hpp:28
reference back()
Definition Buffer.hpp:227
bool empty() const noexcept
Definition Buffer.hpp:295
void push_back(const T &value)
Definition Buffer.hpp:472
size_type size() const noexcept
Definition Buffer.hpp:299
Optional value type that holds either a value or no value.
Definition Optional.hpp:58
ArrayAtlasPacker() noexcept
Construct an atlas packer with the default options.
Definition ArrayAtlasPacker.hpp:68
uint32_t getResolution() const noexcept
Get the current required resolution of the atlas.
Definition ArrayAtlasPacker.hpp:183
uint32_t getDepth() const noexcept
Get the current required depth of the atlas.
Definition ArrayAtlasPacker.hpp:192
ArrayAtlasPacker(const ArrayAtlasPackerOptions &options)
Construct an atlas packer.
Definition ArrayAtlasPacker.hpp:76
InsertBoxResult insertBox(Extent3D size)
Find and reserve a suitable space for a new axis-aligned box to be inserted into the atlas.
Definition ArrayAtlasPacker.hpp:116
Definition Window.hpp:15
GREM_ALWAYS_INLINE constexpr bool isPowerOf2(UnsignedInteger value) noexcept
Check if a given integer is a power of 2, i.e. if the number appears in the infinite sequence 2^0,...
Definition fundamentals.hpp:781
GREM_ALWAYS_INLINE constexpr UnsignedInteger roundUpToMultiple(UnsignedInteger value, UnsignedInteger n)
Get the smallest multiple of n that is not greater than a given value.
Definition fundamentals.hpp:822
GREM_ALWAYS_INLINE constexpr UnsignedInteger roundUpToPowerOf2(UnsignedInteger value) noexcept
Get the smallest integral power of 2 that is not less than a given value.
Definition fundamentals.hpp:794
Offset< 3 > Offset3D
Offset in a 3D data region.
Definition extents.hpp:22
Extent< 3 > Extent3D
Size of a 3D data region.
Definition extents.hpp:32
GREM_ALWAYS_INLINE constexpr decltype(auto) min(A &&a, B &&b)
Get the smaller of two values, or the first value if they are equivalent.
Definition fundamentals.hpp:585
uint32_t height
Vertical extent.
Definition extents.hpp:239
uint32_t width
Horizontal extent.
Definition extents.hpp:238
uint32_t depth
Number of layers.
Definition extents.hpp:240
static constexpr T MAX
Definition fundamentals.hpp:29
Configuration options for an ArrayAtlasPacker.
Definition ArrayAtlasPacker.hpp:22
uint32_t initialResolution
Initial width of the square atlas region, in pixels.
Definition ArrayAtlasPacker.hpp:28
uint32_t padding
Empty space to reserve between inserted rectangles, in pixels.
Definition ArrayAtlasPacker.hpp:40
constexpr bool operator==(const ArrayAtlasPackerOptions &other) const =default
Compare these options to another set of options for equality.
uint32_t initialDepth
Initial number of array layers in the atlas.
Definition ArrayAtlasPacker.hpp:35
uint32_t alignment
Align padded rectangles to a multiple of this number of pixels.
Definition ArrayAtlasPacker.hpp:47
Result of the insertBox() function.
Definition ArrayAtlasPacker.hpp:89
Offset3D offset
The offset, in pixels and layers, from the bottom left of the first layer of the atlas where the new ...
Definition ArrayAtlasPacker.hpp:94
bool resized
Whether the atlas needed to grow in order to accommodate the new box or not.
Definition ArrayAtlasPacker.hpp:101