Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

New sprite compressor #5627

Draft
wants to merge 25 commits into
base: upcoming
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from 22 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
*.8bpp
*.gbapal
*.lz
*.smol
*.rl
*.latfont
*.hwjpnfont
Expand Down Expand Up @@ -41,3 +42,4 @@ prefabs.json
/pokeemerald-*.png
src/data/map_group_count.h
tools/trainerproc/trainerproc
tools/compresSmol/compresSmol
4 changes: 3 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,7 @@ endif
AUTO_GEN_TARGETS :=
include make_tools.mk
# Tool executables
SMOL := $(TOOLS_DIR)/compresSmol/compresSmol$(EXE)
GFX := $(TOOLS_DIR)/gbagfx/gbagfx$(EXE)
AIF := $(TOOLS_DIR)/aif2pcm/aif2pcm$(EXE)
MID := $(TOOLS_DIR)/mid2agb/mid2agb$(EXE)
Expand Down Expand Up @@ -294,7 +295,7 @@ clean-assets:
rm -f $(DATA_ASM_SUBDIR)/layouts/layouts.inc $(DATA_ASM_SUBDIR)/layouts/layouts_table.inc
rm -f $(DATA_ASM_SUBDIR)/maps/connections.inc $(DATA_ASM_SUBDIR)/maps/events.inc $(DATA_ASM_SUBDIR)/maps/groups.inc $(DATA_ASM_SUBDIR)/maps/headers.inc $(DATA_SRC_SUBDIR)/map_group_count.h
find sound -iname '*.bin' -exec rm {} +
find . \( -iname '*.1bpp' -o -iname '*.4bpp' -o -iname '*.8bpp' -o -iname '*.gbapal' -o -iname '*.lz' -o -iname '*.rl' -o -iname '*.latfont' -o -iname '*.hwjpnfont' -o -iname '*.fwjpnfont' \) -exec rm {} +
find . \( -iname '*.1bpp' -o -iname '*.4bpp' -o -iname '*.8bpp' -o -iname '*.gbapal' -o -iname '*.lz' -o -iname '*.smol' -o -iname '*.rl' -o -iname '*.latfont' -o -iname '*.hwjpnfont' -o -iname '*.fwjpnfont' \) -exec rm {} +
find $(DATA_ASM_SUBDIR)/maps \( -iname 'connections.inc' -o -iname 'events.inc' -o -iname 'header.inc' \) -exec rm {} +

tidy: tidymodern tidycheck tidydebug
Expand Down Expand Up @@ -332,6 +333,7 @@ generated: $(AUTO_GEN_TARGETS)
%.gbapal: %.pal ; $(GFX) $< $@
%.gbapal: %.png ; $(GFX) $< $@
%.lz: % ; $(GFX) $< $@
%.smol: % ; $(SMOL) -w $< $@
%.rl: % ; $(GFX) $< $@

clean-generated:
Expand Down
53 changes: 53 additions & 0 deletions include/decompress.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,61 @@

#include "sprite.h"

#define TANS_TABLE_SIZE 64
#define PACKED_FREQ_MASK 0x3F
#define PARTIAL_FREQ_MASK 0xC0000000

extern u8 ALIGNED(4) gDecompressionBuffer[0x4000];

struct CompressionHeader {
u32 mode:5;
u32 imageSize:11;
u32 symSize:15;
u32 padding:1;
u32 initialState:6;
u32 bitstreamSize:13;
u32 loSize:13;
};

struct DecodeYK {
u8 yVal;
u8 kVal;
};

enum CompressionMode {
BASE_ONLY = 0,
ENCODE_SYMS = 1,
ENCODE_DELTA_SYMS = 2,
ENCODE_LO = 3,
ENCODE_BOTH = 4,
ENCODE_BOTH_DELTA_SYMS = 5,
MODE_LZ77 = 16,
};

extern struct DecodeYK ykTemplate[2*TANS_TABLE_SIZE];

void UnpackFrequencies(const u32 *packedFreqs, u8 *freqs);

void DecompressDataVram(const u32 *src, void *dest);
void DecompressDataWram(const u32 *src, void *dest);

void SmolDecompressData(struct CompressionHeader *header, const u32 *data, void *dest);

void BuildDecompressionTable(u32 *freqs, struct DecodeYK *ykTable, u8 *symbolTable);

void DecodeLOtANS(const u32 *data, u32 *readIndex, u32 *bitIndex, struct DecodeYK *ykTable, u8 *symbolTable, u8 *resultVec, u32 *state, u32 count);

void DecodeSymtANS(const u32 *data, u32 *readIndex, u32 *bitIndex, struct DecodeYK *ykTable, u8 *symbolTable, u16 *resultVec, u32 *state, u32 count);

void DecodeSymDeltatANS(const u32 *data, u32 *readIndex, u32 *bitIndex, struct DecodeYK *ykTable, u8 *symbolTable, u16 *resultVec, u32 *state, u32 count);

void DecodeInstructions(struct CompressionHeader *header, u8 *loVec, u16 *symVec, void *dest);

bool32 isModeLoEncoded(enum CompressionMode mode);
bool32 isModeSymEncoded(enum CompressionMode mode);
bool32 isModeSymDelta(enum CompressionMode mode);

// Default Decompression functions are below here
void LZDecompressWram(const u32 *src, void *dest);
void LZDecompressVram(const u32 *src, void *dest);

Expand Down
1 change: 1 addition & 0 deletions include/global.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
#include "gba/gba.h"
#include "fpmath.h"
#include "metaprogram.h"
#include "decompress.h"
#include "constants/global.h"
#include "constants/flags.h"
#include "constants/vars.h"
Expand Down
2 changes: 1 addition & 1 deletion make_tools.mk
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ MAKEFLAGS += --no-print-directory

# Inclusive list. If you don't want a tool to be built, don't add it here.
TOOLS_DIR := tools
TOOL_NAMES := aif2pcm bin2c gbafix gbagfx jsonproc mapjson mid2agb preproc ramscrgen rsfont scaninc trainerproc
TOOL_NAMES := aif2pcm bin2c gbafix gbagfx jsonproc mapjson mid2agb preproc ramscrgen rsfont scaninc trainerproc compresSmol
CHECK_TOOL_NAMES = patchelf mgba-rom-test-hydra

TOOLDIRS := $(TOOL_NAMES:%=$(TOOLS_DIR)/%)
Expand Down
36 changes: 18 additions & 18 deletions src/battle_transition.c
Original file line number Diff line number Diff line change
Expand Up @@ -1370,7 +1370,7 @@ static bool8 Aqua_Init(struct Task *task)
InitPatternWeaveTransition(task);
GetBg0TilesDst(&tilemap, &tileset);
CpuFill16(0, tilemap, BG_SCREEN_SIZE);
LZ77UnCompVram(sTeamAqua_Tileset, tileset);
LZDecompressVram(sTeamAqua_Tileset, tileset);
LoadPalette(sEvilTeam_Palette, BG_PLTT_ID(15), sizeof(sEvilTeam_Palette));

task->tState++;
Expand All @@ -1385,7 +1385,7 @@ static bool8 Magma_Init(struct Task *task)
InitPatternWeaveTransition(task);
GetBg0TilesDst(&tilemap, &tileset);
CpuFill16(0, tilemap, BG_SCREEN_SIZE);
LZ77UnCompVram(sTeamMagma_Tileset, tileset);
LZDecompressVram(sTeamMagma_Tileset, tileset);
LoadPalette(sEvilTeam_Palette, BG_PLTT_ID(15), sizeof(sEvilTeam_Palette));

task->tState++;
Expand Down Expand Up @@ -1445,7 +1445,7 @@ static bool8 Aqua_SetGfx(struct Task *task)
u16 *tilemap, *tileset;

GetBg0TilesDst(&tilemap, &tileset);
LZ77UnCompVram(sTeamAqua_Tilemap, tilemap);
LZDecompressVram(sTeamAqua_Tilemap, tilemap);
SetSinWave((s16*)gScanlineEffectRegBuffers[0], 0, task->tSinIndex, 132, task->tAmplitude, DISPLAY_HEIGHT);

task->tState++;
Expand All @@ -1457,7 +1457,7 @@ static bool8 Magma_SetGfx(struct Task *task)
u16 *tilemap, *tileset;

GetBg0TilesDst(&tilemap, &tileset);
LZ77UnCompVram(sTeamMagma_Tilemap, tilemap);
LZDecompressVram(sTeamMagma_Tilemap, tilemap);
SetSinWave((s16*)gScanlineEffectRegBuffers[0], 0, task->tSinIndex, 132, task->tAmplitude, DISPLAY_HEIGHT);

task->tState++;
Expand Down Expand Up @@ -1511,8 +1511,8 @@ static bool8 Kyogre_Init(struct Task *task)

GetBg0TilesDst(&tilemap, &tileset);
CpuFill16(0, tilemap, BG_SCREEN_SIZE);
LZ77UnCompVram(sKyogre_Tileset, tileset);
LZ77UnCompVram(sKyogre_Tilemap, tilemap);
LZDecompressVram(sKyogre_Tileset, tileset);
LZDecompressVram(sKyogre_Tilemap, tilemap);

task->tState++;
return FALSE;
Expand Down Expand Up @@ -3300,8 +3300,8 @@ static bool8 Groudon_Init(struct Task *task)

GetBg0TilesDst(&tilemap, &tileset);
CpuFill16(0, tilemap, BG_SCREEN_SIZE);
LZ77UnCompVram(sGroudon_Tileset, tileset);
LZ77UnCompVram(sGroudon_Tilemap, tilemap);
LZDecompressVram(sGroudon_Tileset, tileset);
LZDecompressVram(sGroudon_Tilemap, tilemap);

task->tState++;
task->tTimer = 0;
Expand Down Expand Up @@ -4165,7 +4165,7 @@ static bool8 FrontierLogoWiggle_Init(struct Task *task)
InitPatternWeaveTransition(task);
GetBg0TilesDst(&tilemap, &tileset);
CpuFill16(0, tilemap, BG_SCREEN_SIZE);
LZ77UnCompVram(sFrontierLogo_Tileset, tileset);
LZDecompressVram(sFrontierLogo_Tileset, tileset);
LoadPalette(sFrontierLogo_Palette, BG_PLTT_ID(15), sizeof(sFrontierLogo_Palette));

task->tState++;
Expand All @@ -4177,7 +4177,7 @@ static bool8 FrontierLogoWiggle_SetGfx(struct Task *task)
u16 *tilemap, *tileset;

GetBg0TilesDst(&tilemap, &tileset);
LZ77UnCompVram(sFrontierLogo_Tilemap, tilemap);
LZDecompressVram(sFrontierLogo_Tilemap, tilemap);
SetSinWave((s16*)gScanlineEffectRegBuffers[0], 0, task->tSinIndex, 132, task->tAmplitude, DISPLAY_HEIGHT);

task->tState++;
Expand Down Expand Up @@ -4227,7 +4227,7 @@ static bool8 FrontierLogoWave_Init(struct Task *task)
REG_BLDALPHA = sTransitionData->BLDALPHA;
GetBg0TilesDst(&tilemap, &tileset);
CpuFill16(0, tilemap, BG_SCREEN_SIZE);
LZ77UnCompVram(sFrontierLogo_Tileset, tileset);
LZDecompressVram(sFrontierLogo_Tileset, tileset);
LoadPalette(sFrontierLogo_Palette, BG_PLTT_ID(15), sizeof(sFrontierLogo_Palette));
sTransitionData->cameraY = 0;

Expand All @@ -4240,7 +4240,7 @@ static bool8 FrontierLogoWave_SetGfx(struct Task *task)
u16 *tilemap, *tileset;

GetBg0TilesDst(&tilemap, &tileset);
LZ77UnCompVram(sFrontierLogo_Tilemap, tilemap);
LZDecompressVram(sFrontierLogo_Tilemap, tilemap);

task->tState++;
return TRUE;
Expand Down Expand Up @@ -4372,7 +4372,7 @@ static bool8 FrontierSquares_Init(struct Task *task)
u16 *tilemap, *tileset;

GetBg0TilesDst(&tilemap, &tileset);
LZ77UnCompVram(sFrontierSquares_FilledBg_Tileset, tileset);
LZDecompressVram(sFrontierSquares_FilledBg_Tileset, tileset);

FillBgTilemapBufferRect_Palette0(0, 0, 0, 0, 32, 32);
FillBgTilemapBufferRect(0, 1, 0, 0, MARGIN_SIZE, 32, 15);
Expand Down Expand Up @@ -4430,13 +4430,13 @@ static bool8 FrontierSquares_Shrink(struct Task *task)
break;
case 1:
BlendPalettes(PALETTES_ALL & ~(1 << 15), 16, RGB_BLACK);
LZ77UnCompVram(sFrontierSquares_EmptyBg_Tileset, tileset);
LZDecompressVram(sFrontierSquares_EmptyBg_Tileset, tileset);
break;
case 2:
LZ77UnCompVram(sFrontierSquares_Shrink1_Tileset, tileset);
LZDecompressVram(sFrontierSquares_Shrink1_Tileset, tileset);
break;
case 3:
LZ77UnCompVram(sFrontierSquares_Shrink2_Tileset, tileset);
LZDecompressVram(sFrontierSquares_Shrink2_Tileset, tileset);
break;
default:
FillBgTilemapBufferRect_Palette0(0, 1, 0, 0, 32, 32);
Expand Down Expand Up @@ -4467,7 +4467,7 @@ static bool8 FrontierSquaresSpiral_Init(struct Task *task)
u16 *tilemap, *tileset;

GetBg0TilesDst(&tilemap, &tileset);
LZ77UnCompVram(sFrontierSquares_FilledBg_Tileset, tileset);
LZDecompressVram(sFrontierSquares_FilledBg_Tileset, tileset);

FillBgTilemapBufferRect_Palette0(0, 0, 0, 0, 32, 32);
FillBgTilemapBufferRect(0, 1, 0, 0, MARGIN_SIZE, 32, 15);
Expand Down Expand Up @@ -4585,7 +4585,7 @@ static bool8 FrontierSquaresScroll_Init(struct Task *task)
u16 *tilemap, *tileset;

GetBg0TilesDst(&tilemap, &tileset);
LZ77UnCompVram(sFrontierSquares_FilledBg_Tileset, tileset);
LZDecompressVram(sFrontierSquares_FilledBg_Tileset, tileset);
FillBgTilemapBufferRect_Palette0(0, 0, 0, 0, 32, 32);
CopyBgTilemapBufferToVram(0);
LoadPalette(sFrontierSquares_Palette, BG_PLTT_ID(15), sizeof(sFrontierSquares_Palette));
Expand Down
4 changes: 2 additions & 2 deletions src/battle_transition_frontier.c
Original file line number Diff line number Diff line change
Expand Up @@ -221,8 +221,8 @@ static void LoadLogoGfx(void)
u16 *tilemap, *tileset;

GetBg0TilesDst(&tilemap, &tileset);
LZ77UnCompVram(sLogoCenter_Gfx, tileset);
LZ77UnCompVram(sLogoCenter_Tilemap, tilemap);
LZDecompressVram(sLogoCenter_Gfx, tileset);
LZDecompressVram(sLogoCenter_Tilemap, tilemap);
LoadPalette(sLogo_Pal, BG_PLTT_ID(15), sizeof(sLogo_Pal));
LoadCompressedSpriteSheet(&sSpriteSheet_LogoCircles);
LoadSpritePalette(&sSpritePalette_LogoCircles);
Expand Down
2 changes: 1 addition & 1 deletion src/berry_crush.c
Original file line number Diff line number Diff line change
Expand Up @@ -1944,7 +1944,7 @@ static void CopyPlayerNameWindowGfxToBg(struct BerryCrushGame *game)
u8 i = 0;
u8 *windowGfx;

LZ77UnCompWram(gBerryCrush_TextWindows_Tilemap, gDecompressionBuffer);
LZDecompressWram(gBerryCrush_TextWindows_Tilemap, gDecompressionBuffer);

for (windowGfx = gDecompressionBuffer; i < game->playerCount; i++)
{
Expand Down
4 changes: 2 additions & 2 deletions src/berry_fix_graphics.c
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,8 @@ static void UNUSED LoadBerryFixGraphics(u32 idx)
REG_BG0HOFS = 0;
REG_BG0VOFS = 0;
REG_BLDCNT = 0;
LZ77UnCompVram(sBerryFixGraphics[idx].gfx, (void *)BG_CHAR_ADDR(0));
LZ77UnCompVram(sBerryFixGraphics[idx].tilemap, (void *)BG_SCREEN_ADDR(31));
LZDecompressVram(sBerryFixGraphics[idx].gfx, (void *)BG_CHAR_ADDR(0));
LZDecompressVram(sBerryFixGraphics[idx].tilemap, (void *)BG_SCREEN_ADDR(31));
CpuCopy16(sBerryFixGraphics[idx].pltt, (void *)BG_PLTT, BG_PLTT_SIZE);
REG_BG0CNT = BGCNT_SCREENBASE(31);
REG_DISPCNT = DISPCNT_BG0_ON;
Expand Down
4 changes: 2 additions & 2 deletions src/berry_fix_program.c
Original file line number Diff line number Diff line change
Expand Up @@ -366,8 +366,8 @@ static void BerryFix_SetScene(int scene)
break;
}
CopyBgTilemapBufferToVram(0);
LZ77UnCompVram(sBerryFixGraphics[scene].gfx, (void *)BG_CHAR_ADDR(1));
LZ77UnCompVram(sBerryFixGraphics[scene].tilemap, (void *)BG_SCREEN_ADDR(31));
LZDecompressVram(sBerryFixGraphics[scene].gfx, (void *)BG_CHAR_ADDR(1));
LZDecompressVram(sBerryFixGraphics[scene].tilemap, (void *)BG_SCREEN_ADDR(31));
// These palettes range in size from 32-48 colors, so the below is interpreting whatever
// follows the palette (by default, the corresponding tiles) as the remaining 80-96.
CpuCopy32(sBerryFixGraphics[scene].palette, (void *)BG_PLTT, PLTT_SIZEOF(128));
Expand Down
2 changes: 1 addition & 1 deletion src/bg.c
Original file line number Diff line number Diff line change
Expand Up @@ -872,7 +872,7 @@ void CopyToBgTilemapBuffer(u32 bg, const void *src, u16 mode, u16 destOffset)
if (mode != 0)
CpuCopy16(src, (void *)(sGpuBgConfigs2[bg].tilemap + (destOffset * 2)), mode);
else
LZ77UnCompWram(src, (void *)(sGpuBgConfigs2[bg].tilemap + (destOffset * 2)));
LZDecompressWram(src, (void *)(sGpuBgConfigs2[bg].tilemap + (destOffset * 2)));
}
}

Expand Down
6 changes: 3 additions & 3 deletions src/credits.c
Original file line number Diff line number Diff line change
Expand Up @@ -548,8 +548,8 @@ static void Task_LoadShowMons(u8 taskId)
ResetAllPicSprites();
FreeAllSpritePalettes();
gReservedSpritePaletteCount = 8;
LZ77UnCompVram(gBirchBagGrass_Gfx, (void *)VRAM);
LZ77UnCompVram(gBirchGrassTilemap, (void *)(BG_SCREEN_ADDR(7)));
LZDecompressVram(gBirchBagGrass_Gfx, (void *)VRAM);
LZDecompressVram(gBirchGrassTilemap, (void *)(BG_SCREEN_ADDR(7)));
LoadPalette(gBirchBagGrass_Pal + 1, BG_PLTT_ID(0) + 1, PLTT_SIZEOF(2 * 16 - 1));

for (i = 0; i < MON_PIC_SIZE; i++)
Expand Down Expand Up @@ -1291,7 +1291,7 @@ static void LoadTheEndScreen(u16 tileOffsetLoad, u16 tileOffsetWrite, u16 palOff
u16 baseTile;
u16 i;

LZ77UnCompVram(sCreditsCopyrightEnd_Gfx, (void *)(VRAM + tileOffsetLoad));
LZDecompressVram(sCreditsCopyrightEnd_Gfx, (void *)(VRAM + tileOffsetLoad));
LoadPalette(gIntroCopyright_Pal, palOffset, sizeof(gIntroCopyright_Pal));

baseTile = (palOffset / 16) << 12;
Expand Down
Loading
Loading