GREM 3.12.0
Game/Rendering Engine Modules for C++
Loading...
Searching...
No Matches
AtlasPacker.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_ATLAS_PACKER_HPP
5#define GREM_RESOURCE_ATLAS_PACKER_HPP
6
8
12#include <GREM/core/extents.hpp>
14
15namespace grem::resource {
16
26 uint32_t initialResolution = 128;
27
31 uint32_t padding = 0;
32
38 uint32_t alignment = 1;
39
47 [[nodiscard]] constexpr bool operator==(const AtlasPackerOptions& other) const = default;
48};
49
54public:
60
66 explicit AtlasPacker(const AtlasPackerOptions& options)
67 : resolution(options.initialResolution)
68 , padding(options.padding)
69 , alignment(options.alignment) {
70 GREM_ASSERT(options.initialResolution > 0);
72 }
73
91
105 const Extent2D paddedSize{
106 .width = roundUpToMultiple(size.width + padding * 2, alignment),
107 .height = roundUpToMultiple(size.height + padding * 2, alignment),
108 };
109
110 Optional<size_t> foundRow = findSuitableRow(paddedSize);
111
112 bool resized = false;
113 if (!foundRow) {
114 const uint32_t newRowBottom = (rows.empty()) ? uint32_t{0} : rows.back().top;
115 const uint32_t newRowHeight = paddedSize.height + paddedSize.height / uint32_t{8};
116 while (paddedSize.width > resolution || newRowHeight > resolution - newRowBottom) {
117 if (resolution > Limits<uint32_t>::MAX / GROWTH_FACTOR) {
118 throw std::length_error{"Maximum atlas resolution exceeded."};
119 }
120 resolution *= GROWTH_FACTOR;
121 resized = true;
122 }
123 foundRow = rows.size();
124 rows.push_back(Row{.top = newRowBottom + newRowHeight, .width = 0});
125 }
126 const size_t rowIndex = *foundRow;
127
128 Row& row = rows[rowIndex];
129 const uint32_t rowBottom = (rowIndex == 0) ? uint32_t{0} : rows[rowIndex - 1].top;
130 const uint32_t x = row.width + padding;
131 const uint32_t y = rowBottom + padding;
132
133 row.width += paddedSize.width;
134
136 .offset{.x = static_cast<int32_t>(x), .y = static_cast<int32_t>(y)},
137 .resized = resized,
138 };
139 }
140
146 [[nodiscard]] uint32_t getResolution() const noexcept {
147 return resolution;
148 }
149
150private:
151 static constexpr uint32_t GROWTH_FACTOR = 2;
152 static constexpr float MINIMUM_ROW_HEIGHT_RATIO = 0.7f;
153
154 struct Row {
155 uint32_t top;
156 uint32_t width;
157 };
158
159 [[nodiscard]] Optional<size_t> findSuitableRow(Extent2D paddedSize) const {
160 for (size_t rowIndex = 0; rowIndex < rows.size(); ++rowIndex) {
161 const Row& row = rows[rowIndex];
162 const uint32_t rowHeight = (rowIndex == 0) ? row.top : row.top - rows[rowIndex - 1].top;
163 if (const float heightRatio = static_cast<float>(paddedSize.height) / static_cast<float>(rowHeight);
164 heightRatio >= MINIMUM_ROW_HEIGHT_RATIO && paddedSize.height <= rowHeight && paddedSize.width <= resolution - row.width) {
165 return rowIndex;
166 }
167 }
168 return {};
169 }
170
171 Buffer<Row> rows{};
172 uint32_t resolution;
173 uint32_t padding;
174 uint32_t alignment;
175};
176
177} // namespace grem::resource
178
179#endif
#define GREM_ASSERT(expression)
Definition assertions.hpp:77
size_type size() const noexcept
Definition Buffer.hpp:299
Optional value type that holds either a value or no value.
Definition Optional.hpp:58
uint32_t getResolution() const noexcept
Get the current required resolution of the atlas.
Definition AtlasPacker.hpp:146
AtlasPacker() noexcept
Construct an atlas packer with the default options.
Definition AtlasPacker.hpp:58
AtlasPacker(const AtlasPackerOptions &options)
Construct an atlas packer.
Definition AtlasPacker.hpp:66
InsertRectangleResult insertRectangle(Extent2D size)
Find and reserve a suitable space for a new axis-aligned rectangle to be inserted into the atlas.
Definition AtlasPacker.hpp:104
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
Extent< 2 > Extent2D
Size of a 2D data region.
Definition extents.hpp:31
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
Offset< 2 > Offset2D
Offset in a 2D data region.
Definition extents.hpp:21
uint32_t width
Horizontal extent of the region.
Definition extents.hpp:537
uint32_t height
Vertical extent of the region.
Definition extents.hpp:538
static constexpr T MAX
Definition fundamentals.hpp:29
Configuration options for an AtlasPacker.
Definition AtlasPacker.hpp:20
constexpr bool operator==(const AtlasPackerOptions &other) const =default
Compare these options to another set of options for equality.
uint32_t alignment
Align padded rectangles to a multiple of this number of pixels.
Definition AtlasPacker.hpp:38
uint32_t padding
Empty space to reserve between inserted rectangles, in pixels.
Definition AtlasPacker.hpp:31
uint32_t initialResolution
Initial width of the square atlas region, in pixels.
Definition AtlasPacker.hpp:26
Result of the insertRectangle() function.
Definition AtlasPacker.hpp:77
Offset2D offset
The offset, in pixels, from the bottom left of the atlas where the new rectangle was inserted.
Definition AtlasPacker.hpp:82
bool resized
Whether the atlas needed to grow in order to accommodate the new rectangle or not.
Definition AtlasPacker.hpp:89