4#ifndef GREM_RESOURCE_ARRAY_ATLAS_PACKER_HPP
5#define GREM_RESOURCE_ARRAY_ATLAS_PACKER_HPP
77 : resolution(options.initialResolution)
78 , depth(options.initialDepth)
79 , padding(options.padding)
80 , alignment(options.alignment) {
123 bool resized =
false;
124 while (paddedSize.
width > resolution || paddedSize.
height > resolution) {
126 throw std::length_error{
"Maximum atlas resolution exceeded."};
128 resolution *= GROWTH_FACTOR;
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});
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;
147 throw std::length_error{
"Maximum atlas depth exceeded."};
149 const uint32_t newLayerZEnd = newLayerZBegin + newLayerDepth;
150 if (newLayerZEnd > depth) {
151 depth = newLayerZEnd;
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};
160 const size_t layerIndex = foundRow->layerIndex;
161 const size_t rowIndex = foundRow->rowIndex;
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;
170 row.width += paddedSize.
width;
173 .offset{.x =
static_cast<int32_t
>(x), .y =
static_cast<int32_t
>(y), .z =
static_cast<int32_t
>(z)},
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;
211 struct FindSuitableRowResult {
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};
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) {
249 ArrayList<Layer> layers{};
#define GREM_ASSERT(expression)
Definition assertions.hpp:77
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
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