Test iOS texture atlas packing on a cloud Mac
You maintain a small iOS game on Windows and run the script that combines its icons into a texture atlas on a cloud Mac. The images are generated, but colors from neighboring images bleed into each other on the device, and occasionally even the coordinates in the asset manifest change. Before adjusting compression settings, separate rectangle packing from drawing and independently verify padding, overlap, and determinism. That will help you identify which layer is failing.
Define the reserved area, not just the image bounds
This article uses a shelf-packing algorithm without rotation: sort images by height, place them from left to right, and start a new row when the next image will not fit. It provides a readable baseline for acceptance tests. It does not aim for the tightest possible layout or guarantee that it will find every feasible solution.
Each image has two rectangles: its actual pixel bounds and a reserved area with gap added on every side. Collision checks apply to the reserved areas. The image origin used for drawing and the coordinate manifest must be offset from the reserved area's origin by gap. This leaves at least twice gap between the pixel bounds of adjacent images.
Passing the packing checks proves only that space allocation meets the contract. It does not prove that texture sampling is free of color bleeding. Leaving empty space and duplicating edge pixels are two different things.
Start by agreeing on integer pixel coordinates, unique identifiers, and half-open intervals. The size limits below define an input budget for the test tool, not the texture capabilities of a device. You still need to validate against your target devices before release.
Build a runnable baseline on a cloud Mac
With your chosen Xcode toolchain active, create AtlasCheck.swift and add the following code. Dimensions must be positive, and duplicate identifiers are rejected immediately. Insufficient capacity throws an error rather than returning a partial layout that could accidentally be shipped.
struct Tile {
let id: String
let width: Int
let height: Int
}
struct Slot: Equatable {
let id: String
let x: Int
let y: Int
let width: Int
let height: Int
}
enum PackError: Error {
case invalidInput
case noSpace
}
func pack(_ tiles: [Tile], side: Int, gap: Int) throws -> [Slot] {
guard (1...16384).contains(side),
(0...64).contains(gap),
tiles.count <= 10000,
Set(tiles.map(\.id)).count == tiles.count,
tiles.allSatisfy({
!$0.id.isEmpty &&
(1...side).contains($0.width) &&
(1...side).contains($0.height)
}) else {
throw PackError.invalidInput
}
let ordered = tiles.sorted {
if $0.height != $1.height {
return $0.height > $1.height
}
if $0.width != $1.width {
return $0.width > $1.width
}
return $0.id < $1.id
}
var x = 0
var y = 0
var rowHeight = 0
var result: [Slot] = []
for tile in ordered {
let width = tile.width + 2 * gap
let height = tile.height + 2 * gap
guard width <= side, height <= side else {
throw PackError.noSpace
}
if x + width > side {
x = 0
y += rowHeight
rowHeight = 0
}
guard y + height <= side else {
throw PackError.noSpace
}
result.append(Slot(
id: tile.id, x: x, y: y,
width: width, height: height
))
x += width
rowHeight = max(rowHeight, height)
}
return result
}
func overlaps(_ a: Slot, _ b: Slot) -> Bool {
a.x < b.x + b.width &&
b.x < a.x + a.width &&
a.y < b.y + b.height &&
b.y < a.y + a.height
}
let input = [
Tile(id: "a", width: 6, height: 6),
Tile(id: "b", width: 6, height: 6),
Tile(id: "c", width: 6, height: 6),
Tile(id: "d", width: 6, height: 6)
]
let slots = try pack(input, side: 16, gap: 1)
precondition(slots == (try pack(
Array(input.reversed()), side: 16, gap: 1
)))
precondition(Set(slots.map(\.id)) == Set(input.map(\.id)))
for (index, slot) in slots.enumerated() {
precondition(slot.x >= 0 && slot.y >= 0)
precondition(slot.x + slot.width <= 16)
precondition(slot.y + slot.height <= 16)
for other in slots.dropFirst(index + 1) {
precondition(!overlaps(slot, other))
}
}
do {
_ = try pack(input + [
Tile(id: "e", width: 1, height: 1)
], side: 16, gap: 1)
preconditionFailure("Expected capacity rejection")
} catch PackError.noSpace {
}
Compile and run the checks. Do not add -Ounchecked, which removes the checks:
xcrun swiftc -Onone AtlasCheck.swift -o AtlasCheck
./AtlasCheck
The example produces no output on success. A failed assertion or an uncaught error causes the process to fail. Your pipeline should use the exit status directly rather than search for a success message.
Cover row wrapping and rejection behavior with boundary fixtures
Each of the four images above occupies an eight-by-eight-pixel reserved area, filling the canvas exactly. The fifth image cannot fit, testing an explicit capacity rejection. This makes an off-by-one error easier to locate than randomly feeding in dozens of images.
Add four more groups of test cases: empty input returns an empty array; duplicate identifiers and zero dimensions trigger an input error; a single image that exceeds the canvas after padding triggers a capacity error; and, with no padding, an image the same size as the canvas fits exactly.
Every failure test should check the error category. Treating any error as a pass can mistake a build-environment problem or another runtime failure for the expected rejection.
Touching edges do not mean overlap
The collision function uses strict less-than comparisons, so two reserved areas can share a boundary. Changing the comparisons to less-than-or-equal would incorrectly reject adjacent placements. Omitting a check on one axis could falsely report a collision between images in separate rows. Keep explicit fixtures for touching edges and actual overlap, and test them separately.
Fix the sort order and check the actual exported manifest
Sorting by height alone is not enough. The input order of equal-height images may come from directory enumeration, changing their coordinates when you switch machines. The example compares height, then width, then the unique identifier to establish a total order. Use controlled ASCII strings for asset identifiers rather than user-facing localized names as sort keys.
In addition to reversing the input, repeat the test with cyclic shifts of the input order and compare results by identifier. The same input, parameters, and algorithm version must produce the same layout. If an algorithm upgrade changes the layout, treat it as an asset change that needs review.
The production pipeline should read the final manifest back in, subtract gap from image coordinates, and add twice gap to image dimensions. Reconstruct the reserved areas, then run the bounds and collision checks again. Otherwise, the tests cover only the packing function: swapped width and height or a missing offset during export could still go undetected. Pairwise comparison works for small fixtures; large atlases need a separate item-count budget or a spatial index.
After geometry passes, validate sampling and asset handoff
Generate a test atlas with strongly contrasting neighboring images, then inspect it at native size, downscaled, and at non-integer scale factors. If linear filtering or mipmaps are enabled, explicitly define edge extrusion and how each texture level is generated. Transparent gaps are not a universal fix.
Check the coordinate-origin convention as well. The packing code accumulates vertical positions from top to bottom. If the renderer uses a different coordinate system, convert in one place and verify the orientation with an asymmetric image. Do not apply a flip in both drawing and manifest export.
Finally, save the algorithm version, canvas size, padding, input identifiers, and image dimensions alongside the manifest. On failure, preserve the smallest input set that reproduces the problem, and do not overwrite the last validated assets. This makes the cloud Mac responsible for an auditable asset pipeline, not merely for generating one large image that looks right.
Frequently asked questions
Does a failed shelf pack prove the images cannot fit?
No. It only means this heuristic, ordering, and no-rotation policy could not place them. Try a larger canvas, multiple atlases, or a different packing algorithm.
Does padding alone prevent texture bleeding?
No. Padding reserves geometric space. You still need an edge-extrusion policy, transparent-pixel handling, mipmap rules, and rendering tests with minification and linear filtering.
Get your own cloud Mac
A dedicated physical machine, not a virtual machine. Rent by the day, with same-day activation. Sign in to open the remote desktop in your browser. If you need help, submit a support ticket in the console.
Get a cloud Mac