Skip to content

Improve sheet packing in Dune 2000.#21369

Merged
PunkPun merged 1 commit into
OpenRA:bleedfrom
RoosterDragon:d2k-sheet-packing
Mar 12, 2024
Merged

Improve sheet packing in Dune 2000.#21369
PunkPun merged 1 commit into
OpenRA:bleedfrom
RoosterDragon:d2k-sheet-packing

Conversation

@RoosterDragon

@RoosterDragon RoosterDragon commented Mar 12, 2024

Copy link
Copy Markdown
Member

In #21363, SpriteCache is updated to sort sprites by height before adding them onto the sheet. This improves packing by reducing wasted space as the sprites are packed onto the sheet. D2kSpriteSequence does not fully benefit from this change, as it creates additional sprites afterwards in the ResolveSprites method. These are not sorted, so they often waste space due to height changes between adjacent sprites and cause an inefficient packing. Sorting them in place is insufficient, as each sequence performs the operation independently. So sets of sprites across different sequences end up with poor packing overall. We need all the sprites to be collected together and sorted in one place for best effect.

We restructure SpriteCache to allow a frame mutation function to be provided when reserving sprites. This removes the need for the ReserveFrames and ResolveFrames methods in SpriteCache. D2kSpriteSequence can use this new function to pass in the required modification, and no longer has to add frames to the sheet builder itself. Now the SpriteCache can apply the desired frame mutations, it can batch together these mutated frames with the other frames and sort them all as a single batch. With all frames sorted together the maximum benefit of this packing approach is realised.

This reduces the number of BGRA sheets required for the d2k mod from 3 to 2.

Test by running the following utility commands and inspecting the sheet images:

./utility.cmd d2k --extract PALETTE.BIN    
./utility.cmd d2k --dump-sequence-sheets PALETTE.BIN ARRAKIS

In a3d0a50, SpriteCache is updated to sort sprites by height before adding them onto the sheet. This improves packing by reducing wasted space as the sprites are packed onto the sheet. D2kSpriteSequence does not fully benefit from this change, as it creates additional sprites afterwards in the ResolveSprites method. These are not sorted, so they often waste space due to height changes between adjacent sprites and cause an inefficient packing. Sorting them in place is insufficient, as each sequence performs the operation independently. So sets of sprites across different sequences end up with poor packing overall. We need all the sprites to be collected together and sorted in one place for best effect.

We restructure SpriteCache to allow a frame mutation function to be provided when reserving sprites. This removes the need for the ReserveFrames and ResolveFrames methods in SpriteCache. D2kSpriteSequence can use this new function to pass in the required modification, and no longer has to add frames to the sheet builder itself. Now the SpriteCache can apply the desired frame mutations, it can batch together these mutated frames with the other frames and sort them all as a single batch. With all frames sorted together the maximum benefit of this packing approach is realised.

This reduces the number of BGRA sheets required for the d2k mod from 3 to 2.

@PunkPun PunkPun left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

lgtm

// to cover the case where the same image is requested in both versions.
spritesForToken[frameIndex] = spriteCache.GetOrAdd(
(filename, frameIndex, premultiplied),
(filename, frameIndex, premultiplied, adjustFrame),

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

adjustFrame is a reference type, so the filtering won't work perfectly when non-null. Though I suppose this implementation is still much cleaner than on bleed

@PunkPun
PunkPun merged commit 4fca85f into OpenRA:bleed Mar 12, 2024
@PunkPun

PunkPun commented Mar 12, 2024

Copy link
Copy Markdown
Member

changelog

@RoosterDragon
RoosterDragon deleted the d2k-sheet-packing branch March 13, 2024 17:33
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants