diff --git a/include/functions.h b/include/functions.h index 4c6c74cef4..31ebb9c207 100644 --- a/include/functions.h +++ b/include/functions.h @@ -1160,34 +1160,6 @@ void Play_AssignPlayerCsIdsFromScene(GameState* thisx, s32 spawnCsId); void Play_FillScreen(GameState* thisx, s16 fillScreenOn, u8 red, u8 green, u8 blue, u8 alpha); void Play_Init(GameState* thisx); -void PreRender_SetValuesSave(PreRender* this, u32 width, u32 height, void* fbuf, void* zbuf, void* cvg); -void PreRender_Init(PreRender* this); -void PreRender_SetValues(PreRender* this, u32 width, u32 height, void* fbuf, void* zbuf); -void PreRender_Destroy(PreRender* this); -void func_8016FDB8(PreRender* this, Gfx** gfxp, void* buf, void* bufSave, u32 arg4); -void func_8016FF70(PreRender* this, Gfx** gfxp, void* buf, void* bufSave); -void func_8016FF90(PreRender* this, Gfx** gfxp, void* buf, void* bufSave, s32 envR, s32 envG, s32 envB, s32 envA); -void func_80170200(PreRender* this, Gfx** gfxp, void* buf, void* bufSave); -void func_8017023C(PreRender* this, Gfx** gfxp, void* buf, void* bufSave); -void func_8017057C(PreRender* this, Gfx** gfxp); -void func_801705B4(PreRender* this, Gfx** gfxp); -void func_801705EC(PreRender* this, Gfx** gfxp); -void func_80170730(PreRender* this, Gfx** gfxp); -void func_80170774(PreRender* this, Gfx** gfxp); -void func_80170798(PreRender* this, Gfx** gfxp); -void func_80170AE0(PreRender* this, Gfx** gfxp, s32 alpha); -void func_80170B28(PreRender* this, Gfx** gfxp); -void PreRender_AntiAliasAlgorithm(PreRender* this, s32 x, s32 y); -void PreRender_ApplyAntiAliasingFilter(PreRender* this); -u32 func_801716C4(u8* arg0, u8* arg1, u8* arg2); -void func_801717F8(PreRender* this); -void PreRender_ApplyFilters(PreRender* this); -void PreRender_ApplyFiltersSlowlyInit(PreRender* this); -void PreRender_ApplyFiltersSlowlyDestroy(PreRender* this); -void func_801720C4(PreRender* this); -void func_801720FC(PreRenderParams* params, Gfx** gfxp); -void Prerender_DrawBackground2D(Gfx** gfxp, void* timg, void* tlut, u16 width, u16 height, u8 fmt, u8 siz, u16 tt, u16 arg8, f32 x, f32 y, f32 xScale, f32 yScale, u32 flags); - void GameAlloc_Log(GameAlloc* this); void* GameAlloc_Malloc(GameAlloc* this, size_t size); void GameAlloc_Free(GameAlloc* this, void* data); @@ -1208,10 +1180,6 @@ void Graph_ThreadEntry(void* arg); Gfx* Graph_GfxPlusOne(Gfx* gfx); Gfx* Graph_BranchDlist(Gfx* gfx, Gfx* dst); void* Graph_DlistAlloc(Gfx** gfx, size_t size); -ListAlloc* ListAlloc_Init(ListAlloc* this); -// void ListAlloc_Alloc(void); -// void ListAlloc_Free(void); -void ListAlloc_FreeAll(ListAlloc* this); void Sched_SwapFramebuffer(CfbInfo* cfbInfo); void Sched_RetraceUpdateFramebuffer(SchedContext* sched, CfbInfo* cfbInfo); diff --git a/include/listalloc.h b/include/listalloc.h new file mode 100644 index 0000000000..e4ca07f072 --- /dev/null +++ b/include/listalloc.h @@ -0,0 +1,16 @@ +#ifndef LISTALLOC_H +#define LISTALLOC_H + +#include "ultra64.h" + +typedef struct ListAlloc { + /* 0x0 */ struct ListAlloc* prev; + /* 0x4 */ struct ListAlloc* next; +} ListAlloc; // size = 0x8 + +ListAlloc* ListAlloc_Init(ListAlloc* this); +void* ListAlloc_Alloc(ListAlloc* this, size_t size); +void ListAlloc_Free(ListAlloc* this, void* data); +void ListAlloc_FreeAll(ListAlloc* this); + +#endif diff --git a/include/z64.h b/include/z64.h index ca9748509b..63aec32efb 100644 --- a/include/z64.h +++ b/include/z64.h @@ -52,6 +52,7 @@ #include "z64object.h" #include "z64ocarina.h" #include "z64player.h" +#include "z64prerender.h" #include "z64save.h" #include "z64scene.h" #include "z64schedule.h" @@ -222,53 +223,6 @@ typedef struct { /* 0xFA */ u8 unk_FA[4]; } EnvironmentContext; // size = 0x100 -typedef struct ListAlloc { - /* 0x0 */ struct ListAlloc* prev; - /* 0x4 */ struct ListAlloc* next; -} ListAlloc; // size = 0x8 - -typedef struct { - /* 0x00 */ u16 width; - /* 0x02 */ u16 height; - /* 0x04 */ u16 widthSave; - /* 0x06 */ u16 heightSave; - /* 0x08 */ char unk_8[8]; - /* 0x10 */ u16* fbuf; - /* 0x14 */ u16* fbufSave; - /* 0x18 */ u8* cvgSave; - /* 0x1C */ u16* zbuf; - /* 0x20 */ u16* zbufSave; - /* 0x24 */ u16 ulxSave; - /* 0x26 */ u16 ulySave; - /* 0x28 */ u16 lrxSave; - /* 0x2A */ u16 lrySave; - /* 0x2C */ u16 ulx; - /* 0x2E */ u16 uly; - /* 0x30 */ u16 lrx; - /* 0x32 */ u16 lry; - /* 0x34 */ char unk_34[16]; - /* 0x44 */ ListAlloc alloc; - /* 0x4C */ u8 unk_4C; - /* 0x4D */ u8 unk_4D; - /* 0x4E */ char unk_4E[2]; -} PreRender; // size = 0x50 - -typedef struct { - /* 0x00 */ void* timg; - /* 0x04 */ void* tlut; - /* 0x08 */ u16 width; - /* 0x0A */ u16 height; - /* 0x0C */ u8 fmt; - /* 0x0D */ u8 siz; - /* 0x0E */ u16 tt; - /* 0x10 */ u16 unk_10; - /* 0x14 */ f32 x; - /* 0x18 */ f32 y; - /* 0x1C */ f32 xScale; - /* 0x20 */ f32 yScale; - /* 0x24 */ u32 flags; -} PreRenderParams; // size = 0x28 - struct PlayState; typedef struct { diff --git a/include/z64prerender.h b/include/z64prerender.h new file mode 100644 index 0000000000..813f6827ac --- /dev/null +++ b/include/z64prerender.h @@ -0,0 +1,73 @@ +#ifndef Z64_PRERENDER_H +#define Z64_PRERENDER_H + +#include "ultra64.h" +#include "listalloc.h" +#include "unk.h" + +#define BG2D_FLAGS_1 (1 << 0) +#define BG2D_FLAGS_2 (1 << 1) +#define BG2D_FLAGS_AC_THRESHOLD (1 << 2) +#define BG2D_FLAGS_LOAD_S2DEX2 (1 << 3) +#define BG2D_FLAGS_COPY (1 << 4) + +typedef enum PrerenderFilterState { + /* 0 */ PRERENDER_FILTER_STATE_NONE, + /* 1 */ PRERENDER_FILTER_STATE_PROCESS, + /* 2 */ PRERENDER_FILTER_STATE_DONE +} PrerenderFilterState; + +typedef struct PreRender { + /* 0x00 */ u16 width; + /* 0x02 */ u16 height; + /* 0x04 */ u16 widthSave; + /* 0x06 */ u16 heightSave; + /* 0x08 */ UNK_TYPE1 unk_08[0x8]; + /* 0x10 */ u16* fbuf; + /* 0x14 */ u16* fbufSave; + /* 0x18 */ u8* cvgSave; + /* 0x1C */ u16* zbuf; + /* 0x20 */ u16* zbufSave; + /* 0x24 */ u16 ulxSave; + /* 0x26 */ u16 ulySave; + /* 0x28 */ u16 lrxSave; + /* 0x2A */ u16 lrySave; + /* 0x2C */ u16 ulx; + /* 0x2E */ u16 uly; + /* 0x30 */ u16 lrx; + /* 0x32 */ u16 lry; + /* 0x34 */ UNK_TYPE1 unk_34[0x10]; + /* 0x44 */ ListAlloc alloc; + /* 0x4C */ u8 unk_4C; + /* 0x4D */ u8 filterState; // See `PrerenderFilterState` +} PreRender; // size = 0x50 + + +void PreRender_SetValuesSave(PreRender* this, u32 width, u32 height, void* fbuf, void* zbuf, void* cvg); +void PreRender_Init(PreRender* this); +void PreRender_SetValues(PreRender* this, u32 width, u32 height, void* fbuf, void* zbuf); +void PreRender_Destroy(PreRender* this); +void PreRender_CopyImage(PreRender* this, Gfx** gfxp, void* img, void* imgDst, u32 useThresholdAlphaCompare); +void PreRender_RestoreBuffer(PreRender* this, Gfx** gfxp, void* buf, void* bufSave); +void func_8016FF90(PreRender* this, Gfx** gfxp, void* buf, void* bufSave, s32 envR, s32 envG, s32 envB, s32 envA); +void func_80170200(PreRender* this, Gfx** gfxp, void* buf, void* bufSave); +void PreRender_CoverageRgba16ToI8(PreRender* this, Gfx** gfxp, void* img, void* cvgDst); +void PreRender_SaveZBuffer(PreRender* this, Gfx** gfxp); +void PreRender_SaveFramebuffer(PreRender* this, Gfx** gfxp); +void PreRender_FetchFbufCoverage(PreRender* this, Gfx** gfxp); +void PreRender_DrawCoverage(PreRender* this, Gfx** gfxp); +void PreRender_RestoreZBuffer(PreRender* this, Gfx** gfxp); +void func_80170798(PreRender* this, Gfx** gfxp); +void func_80170AE0(PreRender* this, Gfx** gfxp, s32 alpha); +void PreRender_RestoreFramebuffer(PreRender* this, Gfx** gfxp); +void PreRender_AntiAliasFilterPixel(PreRender* this, s32 x, s32 y); +void PreRender_AntiAliasFilter(PreRender* this); +u32 PreRender_Get5bMedian9(u8* px1, u8* px2, u8* px3); +void PreRender_DivotFilter(PreRender* this); +void PreRender_ApplyFilters(PreRender* this); +void PreRender_ApplyFiltersSlowlyInit(PreRender* this); +void PreRender_ApplyFiltersSlowlyDestroy(PreRender* this); +void func_801720C4(PreRender* this); +void Prerender_DrawBackground2D(Gfx** gfxp, void* timg, void* tlut, u16 width, u16 height, u8 fmt, u8 siz, u16 tt, u16 tlutCount, f32 x, f32 y, f32 xScale, f32 yScale, u32 flags); + +#endif diff --git a/spec b/spec index 90459b0366..295af646b3 100644 --- a/spec +++ b/spec @@ -547,7 +547,6 @@ beginseg include "build/src/code/z_play.o" include "build/src/code/z_play_hireso.o" include "build/src/code/PreRender.o" - include "build/data/code/PreRender.bss.o" include "build/src/code/TwoHeadGfxArena.o" include "build/src/code/TwoHeadArena.o" include "build/src/code/audio_stop_all_sfx.o" diff --git a/src/code/PreRender.c b/src/code/PreRender.c index c9f279e759..d058fded39 100644 --- a/src/code/PreRender.c +++ b/src/code/PreRender.c @@ -1,4 +1,18 @@ +/** + * @file PreRender.c + * + * This file implements various routines important to framebuffer effects, such as RDP accelerated color and depth + * buffer copies and coverage drawing. Also contains software implementations of the Video Interface anti-aliasing and + * divot filters. + */ + +#include "z64prerender.h" #include "global.h" +#include "PR/gs2dex.h" +#include "libc/alloca.h" +#include "libc/stdbool.h" +#include "color.h" +#include "macros.h" #include "slowly.h" #include "stack.h" #include "stackcheck.h" @@ -41,20 +55,20 @@ void PreRender_Destroy(PreRender* this) { ListAlloc_FreeAll(&this->alloc); } -void func_8016FDB8(PreRender* this, Gfx** gfxp, void* buf, void* bufSave, u32 arg4) { +void PreRender_CopyImage(PreRender* this, Gfx** gfxp, void* img, void* imgDst, u32 useThresholdAlphaCompare) { Gfx* gfx = *gfxp; u32 flags; gDPPipeSync(gfx++); - gDPSetColorImage(gfx++, G_IM_FMT_RGBA, G_IM_SIZ_16b, this->width, bufSave); + gDPSetColorImage(gfx++, G_IM_FMT_RGBA, G_IM_SIZ_16b, this->width, imgDst); gDPSetScissor(gfx++, G_SC_NON_INTERLACE, 0, 0, this->width, this->height); - flags = 0x18; - if (arg4 == true) { - flags = 0x1C; + flags = BG2D_FLAGS_LOAD_S2DEX2 | BG2D_FLAGS_COPY; + if (useThresholdAlphaCompare == true) { + flags |= BG2D_FLAGS_AC_THRESHOLD; } - Prerender_DrawBackground2D(&gfx, buf, NULL, this->width, this->height, G_IM_FMT_RGBA, G_IM_SIZ_16b, G_TT_NONE, 0, + Prerender_DrawBackground2D(&gfx, img, NULL, this->width, this->height, G_IM_FMT_RGBA, G_IM_SIZ_16b, G_TT_NONE, 0, 0.0f, 0.0f, 1.0f, 1.0f, flags); gDPPipeSync(gfx++); gDPSetColorImage(gfx++, G_IM_FMT_RGBA, G_IM_SIZ_16b, this->width, this->fbuf); @@ -62,8 +76,8 @@ void func_8016FDB8(PreRender* this, Gfx** gfxp, void* buf, void* bufSave, u32 ar *gfxp = gfx; } -void func_8016FF70(PreRender* this, Gfx** gfxp, void* buf, void* bufSave) { - func_8016FDB8(this, gfxp, buf, bufSave, false); +void PreRender_RestoreBuffer(PreRender* this, Gfx** gfxp, void* buf, void* bufSave) { + PreRender_CopyImage(this, gfxp, buf, bufSave, false); } void func_8016FF90(PreRender* this, Gfx** gfxp, void* buf, void* bufSave, s32 envR, s32 envG, s32 envB, s32 envA) { @@ -91,7 +105,7 @@ void func_8016FF90(PreRender* this, Gfx** gfxp, void* buf, void* bufSave, s32 en gDPSetScissor(gfx++, G_SC_NON_INTERLACE, 0, 0, this->width, this->height); Prerender_DrawBackground2D(&gfx, buf, 0, this->width, this->height, G_IM_FMT_RGBA, G_IM_SIZ_16b, G_TT_NONE, 0, 0.0f, - 0.0f, 1.0f, 1.0f, 0xB); + 0.0f, 1.0f, 1.0f, BG2D_FLAGS_1 | BG2D_FLAGS_2 | BG2D_FLAGS_LOAD_S2DEX2); gDPPipeSync(gfx++); gDPSetColorImage(gfx++, G_IM_FMT_RGBA, G_IM_SIZ_16b, this->width, this->fbuf); @@ -102,103 +116,174 @@ void func_80170200(PreRender* this, Gfx** gfxp, void* buf, void* bufSave) { func_8016FF90(this, gfxp, buf, bufSave, 255, 255, 255, 255); } -#ifdef NON_MATCHING -// just regalloc -void func_8017023C(PreRender* this, Gfx** gfxp, void* buf, void* bufSave) { +/** + * Reads the coverage values stored in the RGBA16 format `img` with dimensions `this->width`, `this->height` and + * converts it to an 8-bpp intensity image. + * + * @param gfxp Display list pointer + * @param img Image to read coverage from + * @param cvgDst Buffer to store coverage into + */ +void PreRender_CoverageRgba16ToI8(PreRender* this, Gfx** gfxp, void* img, void* cvgDst) { Gfx* gfx = *gfxp; - s32 x; - s32 x2; - s32 dx; + s32 rowsRemaining; + s32 curRow; + s32 nRows; gDPPipeSync(gfx++); gDPSetOtherMode(gfx++, G_AD_DISABLE | G_CD_DISABLE | G_CK_NONE | G_TC_FILT | G_TF_POINT | G_TT_NONE | G_TL_TILE | G_TD_CLAMP | G_TP_NONE | G_CYC_1CYCLE | G_PM_NPRIMITIVE, G_AC_NONE | G_ZS_PRIM | G_RM_PASS | G_RM_OPA_CI2); + + // Set the combiner to draw the texture as-is, discarding alpha channel gDPSetCombineLERP(gfx++, 0, 0, 0, TEXEL0, 0, 0, 0, 0, 0, 0, 0, TEXEL0, 0, 0, 0, 0); - gDPSetColorImage(gfx++, G_IM_FMT_I, G_IM_SIZ_8b, this->width, bufSave); + // Set the destination color image to the provided address + gDPSetColorImage(gfx++, G_IM_FMT_I, G_IM_SIZ_8b, this->width, cvgDst); + // Set up a scissor based on the source image gDPSetScissor(gfx++, G_SC_NON_INTERLACE, 0, 0, this->width, this->height); - dx = 0x1000 / (this->width * 2); - x = this->height; - x2 = 0; + // Calculate the max number of rows that can fit into TMEM at once + nRows = TMEM_SIZE / (this->width * G_IM_SIZ_16b_BYTES); - while (x > 0) { + // Set up the number of remaining rows + rowsRemaining = this->height; + curRow = 0; + while (rowsRemaining > 0) { s32 uls = 0; s32 lrs = this->width - 1; s32 ult; s32 lrt; - dx = CLAMP_MAX(dx, x); - ult = x2; - lrt = x2 + dx - 1; + // Make sure that we don't load past the end of the source image + if (nRows > rowsRemaining) { + nRows = rowsRemaining; + } - gDPLoadTextureTile(gfx++, buf, G_IM_FMT_IA, G_IM_SIZ_16b, this->width, this->height, uls, ult, lrs, lrt, 0, + // Determine the upper and lower bounds of the rect to draw + ult = curRow; + lrt = curRow + nRows - 1; + + // Load a horizontal strip of the source image in IA16 format. Since the source image is stored in memory as + // RGBA16, the bits are reinterpreted into IA16: + // + // r g b a + // 11111 111 11 11111 1 + // i a + // 11111 111 11 11111 1 + // + // I = (r << 3) | (g >> 2) + // A = (g << 6) | (b << 1) | a + // + // Since it is expected that r = g = b = cvg in the source image, this results in + // I = (cvg << 3) | (cvg >> 2) + // This expands the 5-bit coverage into an 8-bit value + gDPLoadTextureTile(gfx++, img, G_IM_FMT_IA, G_IM_SIZ_16b, this->width, this->height, uls, ult, lrs, lrt, 0, G_TX_NOMIRROR | G_TX_WRAP, G_TX_NOMIRROR | G_TX_WRAP, G_TX_NOMASK, G_TX_NOMASK, G_TX_NOLOD, G_TX_NOLOD); + // Draw that horizontal strip to the destination image. With the combiner and blender configuration set above, + // the intensity (I) channel of the loaded IA16 texture will be written as-is to the I8 color image, each pixel + // in the final image is + // I = (cvg << 3) | (cvg >> 2) gSPTextureRectangle(gfx++, uls << 2, ult << 2, (lrs + 1) << 2, (lrt + 1) << 2, G_TX_RENDERTILE, uls << 5, ult << 5, 1 << 10, 1 << 10); - x2 += dx; - x -= dx; + // Update the number of rows remaining and index of the row being drawn + curRow += nRows; + rowsRemaining -= nRows; } + // Reset the color image to the current framebuffer gDPPipeSync(gfx++); gDPSetColorImage(gfx++, G_IM_FMT_RGBA, G_IM_SIZ_16b, this->width, this->fbuf); *gfxp = gfx; } -#else -#pragma GLOBAL_ASM("asm/non_matchings/code/PreRender/func_8017023C.s") -#endif -void func_8017057C(PreRender* this, Gfx** gfxp) { +/** + * Saves zbuf to zbufSave + */ +void PreRender_SaveZBuffer(PreRender* this, Gfx** gfxp) { if ((this->zbufSave != NULL) && (this->zbuf != NULL)) { - func_8016FF70(this, gfxp, this->zbuf, this->zbufSave); + PreRender_RestoreBuffer(this, gfxp, this->zbuf, this->zbufSave); } } -void func_801705B4(PreRender* this, Gfx** gfxp) { +/** + * Saves fbuf to fbufSave + */ +void PreRender_SaveFramebuffer(PreRender* this, Gfx** gfxp) { if ((this->fbufSave != NULL) && (this->fbuf != NULL)) { func_80170200(this, gfxp, this->fbuf, this->fbufSave); } } -void func_801705EC(PreRender* this, Gfx** gfxp) { +/** + * Fetches the coverage of the current framebuffer into an image of the same format as the current color image, storing + * it over the framebuffer in memory. + */ +void PreRender_FetchFbufCoverage(PreRender* this, Gfx** gfxp) { Gfx* gfx = *gfxp; gDPPipeSync(gfx++); + // Set the blend color to full white and set maximum depth gDPSetBlendColor(gfx++, 255, 255, 255, 8); - gDPSetPrimDepth(gfx++, -1, -1); + gDPSetPrimDepth(gfx++, 0xFFFF, 0xFFFF); + + // Uses G_RM_VISCVG to blit the coverage values to the framebuffer + // + // G_RM_VISCVG is the following special render mode: + // IM_RD : Allow read-modify-write operations on the framebuffer + // FORCE_BL : Apply the blender to all pixels rather than just edges, skip the division step of the blend formula + // (G_BL_CLR_IN * G_BL_0 + G_BL_CLR_BL * G_BL_A_MEM) = G_BL_CLR_BL * G_BL_A_MEM + // + // G_BL_A_MEM ("memory alpha") is coverage, therefore this blender configuration emits only the coverage (up to a + // constant factor determined by blend color) and discards any pixel colors. For an RGBA16 framebuffer, each of the + // three color channels r,g,b will receive the coverage value individually. + // + // Also disables other modes such as alpha compare and texture perspective correction gDPSetOtherMode(gfx++, G_AD_DISABLE | G_CD_DISABLE | G_CK_NONE | G_TC_FILT | G_TF_POINT | G_TT_NONE | G_TL_TILE | G_TD_CLAMP | G_TP_NONE | G_CYC_1CYCLE | G_PM_NPRIMITIVE, G_AC_NONE | G_ZS_PRIM | G_RM_VISCVG | G_RM_VISCVG2); + // Set up a scissor with the same dimensions as the framebuffer gDPSetScissor(gfx++, G_SC_NON_INTERLACE, 0, 0, this->width, this->height); + // Fill rectangle to obtain the coverage values as an RGBA16 image gDPFillRectangle(gfx++, 0, 0, this->width, this->height); gDPPipeSync(gfx++); *gfxp = gfx; } -void func_80170730(PreRender* this, Gfx** gfxp) { - func_801705EC(this, gfxp); +/** + * Draws the coverage of the current framebuffer `this->fbuf` to an I8 image at `this->cvgSave`. Overwrites + * `this->fbuf` in the process. + */ +void PreRender_DrawCoverage(PreRender* this, Gfx** gfxp) { + PreRender_FetchFbufCoverage(this, gfxp); if (this->cvgSave != NULL) { - func_8017023C(this, gfxp, this->fbuf, this->cvgSave); + PreRender_CoverageRgba16ToI8(this, gfxp, this->fbuf, this->cvgSave); } } -void func_80170774(PreRender* this, Gfx** gfxp) { - func_8016FF70(this, gfxp, this->zbufSave, this->zbuf); +/** + * Restores zbufSave to zbuf + */ +void PreRender_RestoreZBuffer(PreRender* this, Gfx** gfxp) { + PreRender_RestoreBuffer(this, gfxp, this->zbufSave, this->zbuf); } +/** + * Draws a full-screen image to the current framebuffer, that sources the rgb channel from `this->fbufSave` and + * the alpha channel from `this->cvgSave` modulated by environment color. + */ void func_80170798(PreRender* this, Gfx** gfxp) { Gfx* gfx; - s32 y; - s32 y2; - s32 dy; + s32 rowsRemaining; + s32 curRow; + s32 nRows; s32 rtile = 1; if (this->cvgSave != NULL) { @@ -206,43 +291,55 @@ void func_80170798(PreRender* this, Gfx** gfxp) { gDPPipeSync(gfx++); gDPSetEnvColor(gfx++, 255, 255, 255, 32); + // Effectively disable blending in both cycles. It's 2-cycle so that TEXEL1 can be used to point to a different + // texture tile. gDPSetOtherMode(gfx++, G_AD_DISABLE | G_CD_DISABLE | G_CK_NONE | G_TC_FILT | G_TF_POINT | G_TT_NONE | G_TL_TILE | G_TD_CLAMP | G_TP_NONE | G_CYC_2CYCLE | G_PM_NPRIMITIVE, G_AC_NONE | G_ZS_PRIM | AA_EN | CVG_DST_CLAMP | ZMODE_OPA | CVG_X_ALPHA | GBL_c1(G_BL_CLR_IN, G_BL_0, G_BL_CLR_IN, G_BL_1) | GBL_c2(G_BL_CLR_IN, G_BL_0, G_BL_CLR_IN, G_BL_1)); + + // Set up the color combiner: first cycle: TEXEL0, TEXEL1 + ENVIRONMENT; second cycle: G_CC_PASS2 gDPSetCombineLERP(gfx++, 0, 0, 0, TEXEL0, 1, 0, TEXEL1, ENVIRONMENT, 0, 0, 0, COMBINED, 0, 0, 0, COMBINED); - dy = (this->width > 320) ? 2 : 4; - y = this->height; - y2 = 0; + nRows = (this->width > SCREEN_WIDTH) ? 2 : 4; - while (y > 0) { + rowsRemaining = this->height; + curRow = 0; + + while (rowsRemaining > 0) { s32 uls = 0; s32 lrs = this->width - 1; s32 ult; s32 lrt; - dy = CLAMP_MAX(dy, y); + // Make sure that we don't load past the end of the source image + if (nRows > rowsRemaining) { + nRows = rowsRemaining; + } - ult = y2; - lrt = (y2 + dy - 1); + // Determine the upper and lower bounds of the rect to draw + ult = curRow; + lrt = curRow + nRows - 1; + // Load the frame buffer line gDPLoadMultiTile(gfx++, this->fbufSave, 0x0000, G_TX_RENDERTILE, G_IM_FMT_RGBA, G_IM_SIZ_16b, this->width, this->height, uls, ult, lrs, lrt, 0, G_TX_NOMIRROR | G_TX_WRAP, G_TX_NOMIRROR | G_TX_WRAP, G_TX_NOMASK, G_TX_NOMASK, G_TX_NOLOD, G_TX_NOLOD); + + // Load the coverage line gDPLoadMultiTile(gfx++, this->cvgSave, 0x0160, rtile, G_IM_FMT_I, G_IM_SIZ_8b, this->width, this->height, uls, ult, lrs, lrt, 0, G_TX_NOMIRROR | G_TX_WRAP, G_TX_NOMIRROR | G_TX_WRAP, G_TX_NOMASK, G_TX_NOMASK, G_TX_NOLOD, G_TX_NOLOD); - //! FAKE - if (1) {} - if (1) {} + + // Draw a texture for which the rgb channels come from the framebuffer and the alpha channel comes from + // coverage, modulated by env color gSPTextureRectangle(gfx++, uls << 2, ult << 2, (lrs + 1) << 2, (lrt + 1) << 2, G_TX_RENDERTILE, uls << 5, ult << 5, 1 << 10, 1 << 10); - y2 += dy; - y -= dy; + curRow += nRows; + rowsRemaining -= nRows; } gDPPipeSync(gfx++); @@ -254,134 +351,186 @@ void func_80170AE0(PreRender* this, Gfx** gfxp, s32 alpha) { func_8016FF90(this, gfxp, this->fbufSave, this->fbuf, 255, 255, 255, alpha); } -void func_80170B28(PreRender* this, Gfx** gfxp) { - func_8016FF70(this, gfxp, this->fbufSave, this->fbuf); +/** + * Copies fbufSave to fbuf + */ +void PreRender_RestoreFramebuffer(PreRender* this, Gfx** gfxp) { + PreRender_RestoreBuffer(this, gfxp, this->fbufSave, this->fbuf); } /** - * Applies an anti-alias filter where the middle pixel (index 7) correspond to (x, y) in this 3x5 rectangle - * _ _ _ _ _ - * | 0 1 2 3 4 | - * | 5 6 7 8 9 | - * | A B C D E | - * ‾ ‾ ‾ ‾ ‾ + * Applies the Video Interface anti-aliasing of silhouette edges to an image. + * + * This filter performs a linear interpolation on partially covered pixels between the current pixel color (called + * foreground color) and a "background" pixel color obtained by sampling fully covered pixels at the six highlighted + * points in the following 5x3 neighborhood: + * _ _ _ _ _ + * | o o | + * | o X o | + * | o o | + * ‾ ‾ ‾ ‾ ‾ + * Whether a pixel is partially covered is determined by reading the coverage values associated with the image. + * Coverage is a measure of how many subpixels the last drawn primitive covered. A fully covered pixel is one with a + * full coverage value, the entire pixel was covered by the primitive. + * The background color is calculated as the average of the "penultimate" minimum and maximum colors in the 5x3 + * neighborhood. + * + * The final color is calculated by interpolating the foreground and background color weighted by the coverage: + * OutputColor = cvg * ForeGround + (1.0 - cvg) * BackGround + * + * This is a software implementation of the same algorithm used in the Video Interface hardware when Anti-Aliasing is + * enabled in the VI Control Register. + * + * Patent describing the algorithm: + * + * Gossett, C. P., & van Hook, T. J. (Filed 1995, Published 1998) + * Antialiasing of silhouette edges (USOO5742277A) + * U.S. Patent and Trademark Office + * Expired 2015-10-06 + * https://patents.google.com/patent/US5742277A/en + * + * @param this PreRender instance + * @param x Center pixel x + * @param y Center pixel y */ -void PreRender_AntiAliasAlgorithm(PreRender* this, s32 x, s32 y) { +void PreRender_AntiAliasFilterPixel(PreRender* this, s32 x, s32 y) { s32 i; s32 j; - s32 buffA[3 * 5]; + s32 buffCvg[3 * 5]; s32 buffR[3 * 5]; s32 buffG[3 * 5]; s32 buffB[3 * 5]; - s32 x1; - s32 y1; - s32 pad; - s32 pxR; - s32 pxG; - s32 pxB; - s32 pxR2; - s32 pxG2; - s32 pxB2; + s32 xi; + s32 yi; + s32 temp; + s32 pmaxR; + s32 pmaxG; + s32 pmaxB; + s32 pminR; + s32 pminG; + s32 pminB; Color_RGBA16 pxIn; Color_RGBA16 pxOut; - u32 pxR3; - u32 pxG3; - u32 pxB3; + u32 outR; + u32 outG; + u32 outB; - for (i = 0; i < 3 * 5; i++) { - x1 = (i % 5) + x - 2; - y1 = (i / 5) + y - 1; + // Extract pixels in the 5x3 neighborhood + for (i = 0; i < 5 * 3; i++) { + xi = x + (i % 5) - 2; + yi = y + (i / 5) - 1; - if (x1 < 0) { - x1 = 0; - } else if (x1 > (this->width - 1)) { - x1 = this->width - 1; + // Clamp coordinates to the edges of the image + if (xi < 0) { + xi = 0; + } else if (xi > (this->width - 1)) { + xi = this->width - 1; } - if (y1 < 0) { - y1 = 0; - } else if (y1 > (this->height - 1)) { - y1 = this->height - 1; + if (yi < 0) { + yi = 0; + } else if (yi > (this->height - 1)) { + yi = this->height - 1; } - pxIn.rgba = this->fbufSave[x1 + y1 * this->width]; + // Extract color channels for each pixel, convert 5-bit color channels to 8-bit + pxIn.rgba = this->fbufSave[xi + yi * this->width]; buffR[i] = (pxIn.r << 3) | (pxIn.r >> 2); buffG[i] = (pxIn.g << 3) | (pxIn.g >> 2); buffB[i] = (pxIn.b << 3) | (pxIn.b >> 2); - buffA[i] = this->cvgSave[x1 + y1 * this->width] >> 5; + buffCvg[i] = this->cvgSave[xi + yi * this->width] >> 5; } - pxR = pxR2 = buffR[7]; - pxG = pxG2 = buffG[7]; - pxB = pxB2 = buffB[7]; + pmaxR = pminR = buffR[7]; + pmaxG = pminG = buffG[7]; + pmaxB = pminB = buffB[7]; - for (i = 1; i < 3 * 5; i += 2) { - if (buffA[i] == 7) { - if (pxR < buffR[i]) { - for (j = 1; j < 15; j += 2) { - if ((i != j) && (buffR[j] >= buffR[i]) && (buffA[j] == 7)) { - pxR = buffR[i]; + // For each neighbor + for (i = 1; i < 5 * 3; i += 2) { + // Only sample fully covered pixels + if (buffCvg[i] == 7) { + // Determine "Penultimate Maximum" Value + + // If current maximum is less than this neighbor + if (pmaxR < buffR[i]) { + // For each neighbor (again) + for (j = 1; j < 5 * 3; j += 2) { + // If not the neighbor we were at before, and this neighbor has a larger value and this pixel is + // fully covered, that means the neighbor at `i` is the "penultimate maximum" + if ((i != j) && (buffR[j] >= buffR[i]) && (buffCvg[j] == 7)) { + pmaxR = buffR[i]; } } } - if (pxG < buffG[i]) { - for (j = 1; j < 15; j += 2) { - if ((i != j) && (buffG[j] >= buffG[i]) && (buffA[j] == 7)) { - pxG = buffG[i]; + if (pmaxG < buffG[i]) { + for (j = 1; j < 5 * 3; j += 2) { + if ((i != j) && (buffG[j] >= buffG[i]) && (buffCvg[j] == 7)) { + pmaxG = buffG[i]; } } } - if (pxB < buffB[i]) { - for (j = 1; j < 15; j += 2) { - if ((i != j) && (buffB[j] >= buffB[i]) && (buffA[j] == 7)) { - pxB = buffB[i]; + if (pmaxB < buffB[i]) { + for (j = 1; j < 5 * 3; j += 2) { + if ((i != j) && (buffB[j] >= buffB[i]) && (buffCvg[j] == 7)) { + pmaxB = buffB[i]; } } } + if (1) {} - if (pxR2 > buffR[i]) { - for (j = 1; j < 15; j += 2) { - if ((i != j) && (buffR[j] <= buffR[i]) && (buffA[j] == 7)) { - pxR2 = buffR[i]; + + // Determine "Penultimate Minimum" Value + + // Same as above with inverted conditions + if (pminR > buffR[i]) { + for (j = 1; j < 5 * 3; j += 2) { + if ((i != j) && (buffR[j] <= buffR[i]) && (buffCvg[j] == 7)) { + pminR = buffR[i]; } } } - if (pxG2 > buffG[i]) { - for (j = 1; j < 15; j += 2) { - if ((i != j) && (buffG[j] <= buffG[i]) && (buffA[j] == 7)) { - pxG2 = buffG[i]; + if (pminG > buffG[i]) { + for (j = 1; j < 5 * 3; j += 2) { + if ((i != j) && (buffG[j] <= buffG[i]) && (buffCvg[j] == 7)) { + pminG = buffG[i]; } } } - if (pxB2 > buffB[i]) { - for (j = 1; j < 15; j += 2) { - if ((i != j) && (buffB[j] <= buffB[i]) && (buffA[j] == 7)) { - pxB2 = buffB[i]; + if (pminB > buffB[i]) { + for (j = 1; j < 5 * 3; j += 2) { + if ((i != j) && (buffB[j] <= buffB[i]) && (buffCvg[j] == 7)) { + pminB = buffB[i]; } } } } } - pad = 7 - buffA[7]; - pxR3 = buffR[7] + (((s32)((pad * ((pxR + pxR2) - (buffR[7] << 1))) + 4)) >> 3); - pxG3 = buffG[7] + (((s32)((pad * ((pxG + pxG2) - (buffG[7] << 1))) + 4)) >> 3); - pxB3 = buffB[7] + (((s32)((pad * ((pxB + pxB2) - (buffB[7] << 1))) + 4)) >> 3); + // The background color is determined by averaging the penultimate minimum and maximum pixels, and subtracting the + // Foreground color: + // BackGround = (pMax + pMin) - (ForeGround) * 2 - pxOut.r = pxR3 >> 3; - pxOut.g = pxG3 >> 3; - pxOut.b = pxB3 >> 3; + // OutputColor = cvg * ForeGround + (1.0 - cvg) * BackGround + temp = 7 - buffCvg[7]; + outR = buffR[7] + ((s32)(temp * (pmaxR + pminR - (buffR[7] * 2)) + 4) >> 3); + outG = buffG[7] + ((s32)(temp * (pmaxG + pminG - (buffG[7] * 2)) + 4) >> 3); + outB = buffB[7] + ((s32)(temp * (pmaxB + pminB - (buffB[7] * 2)) + 4) >> 3); + + pxOut.r = outR >> 3; + pxOut.g = outG >> 3; + pxOut.b = outB >> 3; pxOut.a = 1; this->fbufSave[x + y * this->width] = pxOut.rgba; } /** - * Applies an anti-alias filter to the current prerender + * Applies the Video Interface anti-aliasing filter to `this->fbufSave` using `this->cvgSave` */ -void PreRender_ApplyAntiAliasingFilter(PreRender* this) { +void PreRender_AntiAliasFilter(PreRender* this) { s32 x; s32 y; s32 cvg; + // Apply AA filter for (y = 0; y < this->height; y++) { for (x = 0; x < this->width; x++) { cvg = this->cvgSave[x + y * this->width]; @@ -389,49 +538,187 @@ void PreRender_ApplyAntiAliasingFilter(PreRender* this) { cvg++; if (cvg != 8) { - PreRender_AntiAliasAlgorithm(this, x, y); + // If this pixel has only partial coverage, perform the Video Filter interpolation on it + PreRender_AntiAliasFilterPixel(this, x, y); } } } } -#pragma GLOBAL_ASM("asm/non_matchings/code/PreRender/func_801716C4.s") +/** + * Selects the median value from 9 different 5-bit pixels: + * px1[0], px1[1], px1[2], px2[0], px2[1], px2[2], px3[0], px3[1], px3[2] + * all args are expected to be an array of 3 different 5-bit values + */ +u32 PreRender_Get5bMedian9(u8* px1, u8* px2, u8* px3) { + u8 pxValCount[32]; // Stores the count for each of the possible 32 5-bit pixel values + u32 pxCount; // Pixel count + s32 pxMed; // Pixel median value -#pragma GLOBAL_ASM("asm/non_matchings/code/PreRender/func_801717F8.s") + // Initialize count to 0 in groups of 4 bits + *(s32*)(&pxValCount[0]) = 0; + *(s32*)(&pxValCount[4]) = 0; + *(s32*)(&pxValCount[8]) = 0; + *(s32*)(&pxValCount[12]) = 0; + *(s32*)(&pxValCount[16]) = 0; + *(s32*)(&pxValCount[20]) = 0; + *(s32*)(&pxValCount[24]) = 0; + *(s32*)(&pxValCount[28]) = 0; + + // Increment the count that contains the pixel values + pxValCount[px1[0]]++; + pxValCount[px1[1]]++; + pxValCount[px1[2]]++; + + pxValCount[px2[0]]++; + pxValCount[px2[1]]++; + pxValCount[px2[2]]++; + + pxValCount[px3[0]]++; + pxValCount[px3[1]]++; + pxValCount[px3[2]]++; + + // Loop through the 32 bits until 5 values are found, then return that bit value. + // Note that the median of 9 is the 5th sequential value. + pxCount = 0; + pxMed = 0; + while (true) { + pxCount += pxValCount[pxMed]; + if (pxCount >= 5) { + // the median is found + break; + } + pxMed++; + } + + return pxMed; +} + +// Despite the name, this function doesn't seem like an hardware-accurate divot filter +void PreRender_DivotFilter(PreRender* this) { + u32 width = this->width; + u32 height = this->height; + u8* buffer = alloca(width * 10); + u8* redRow[3]; + u8* greenRow[3]; + u8* blueRow[3]; + u8* cvgFull; + Color_RGBA16 inPx; + Color_RGBA16 outPx; + u32 x; + u32 y; + s32 pad; + + redRow[0] = &buffer[width * 0]; + redRow[1] = &buffer[width * 1]; + redRow[2] = &buffer[width * 2]; + + greenRow[0] = &buffer[width * 3]; + greenRow[1] = &buffer[width * 4]; + greenRow[2] = &buffer[width * 5]; + + blueRow[0] = &buffer[width * 6]; + blueRow[1] = &buffer[width * 7]; + blueRow[2] = &buffer[width * 8]; + + cvgFull = &buffer[width * 9]; + + // Fill line buffers for first 2 rows + for (y = 0; y < 2; y++) { + for (x = 0; x < width; x++) { + inPx.rgba = this->fbufSave[x + y * this->width]; + + redRow[y][x] = inPx.r; + greenRow[y][x] = inPx.g; + blueRow[y][x] = inPx.b; + } + } + + // For each row in the image, except first and last + for (y = 1; y < height - 1; y++) { + // Find start of pixels and coverage for current line (bug? this should probably be fetching the NEXT line, but + // really the divot filter only cares about individual lines so it's already wrong) + u8* redRow2 = redRow[2]; + u8* greenRow2 = greenRow[2]; + u8* blueRow2 = blueRow[2]; + u8* lineCvg = &this->cvgSave[width * y]; + u16* linePx = &this->fbufSave[width * y]; + + // Obtain next row from current line, current line becomes the bottom row?? (weird, you would expect this to + // sample the NEXT line?) + for (x = 0; x < width; x++) { + inPx.rgba = linePx[x]; + + redRow2[x] = inPx.r; + greenRow2[x] = inPx.g; + blueRow2[x] = inPx.b; + + // checking for full coverage + cvgFull[x] = (lineCvg[x] >> 5) == 7; + } + + for (x = 1; x < width - 1; x++) { + // if the coverage of the three adjacent pixels on the current line are not all fully covered + if (cvgFull[x - 1] && cvgFull[x] && cvgFull[x + 1]) { + continue; + } + + // find median value in 3x3 square for each (r,g,b), replaces the pixel marked by X: + // * * * + // * * * + // * X * + outPx.r = PreRender_Get5bMedian9(&redRow[0][x - 1], &redRow[1][x - 1], &redRow[2][x - 1]); + outPx.g = PreRender_Get5bMedian9(&greenRow[0][x - 1], &greenRow[1][x - 1], &greenRow[2][x - 1]); + outPx.b = PreRender_Get5bMedian9(&blueRow[0][x - 1], &blueRow[1][x - 1], &blueRow[2][x - 1]); + outPx.a = 1; + + this->fbufSave[x + y * this->width] = outPx.rgba; + } + + // Shuffle row 1 -> 0 + redRow[0] = redRow[1]; + greenRow[0] = greenRow[1]; + blueRow[0] = blueRow[1]; + // Shuffle row 2 -> 1 + redRow[1] = redRow[2]; + greenRow[1] = greenRow[2]; + blueRow[1] = blueRow[2]; + } +} /** * Applies filters to the framebuffer prerender to make it look smoother */ void PreRender_ApplyFilters(PreRender* this) { if ((this->cvgSave == NULL) || (this->fbufSave == NULL)) { - this->unk_4D = 0; + this->filterState = PRERENDER_FILTER_STATE_NONE; } else { - this->unk_4D = 1; - PreRender_ApplyAntiAliasingFilter(this); - func_801717F8(this); - this->unk_4D = 2; + this->filterState = PRERENDER_FILTER_STATE_PROCESS; + PreRender_AntiAliasFilter(this); + PreRender_DivotFilter(this); + this->filterState = PRERENDER_FILTER_STATE_DONE; } } -extern SlowlyMgr sSlowlyMgr; -extern s32 D_801F6FC0; -extern StackEntry sSlowlyStackInfo; -extern STACK(sSlowlyStack, 0x1000); +SlowlyMgr sSlowlyMgr; +s32 sSlowlyRunning; +StackEntry sSlowlyStackInfo; +STACK(sSlowlyStack, 0x1000); /** * Initializes `PreRender_ApplyFilters` onto a new "slowly" thread */ void PreRender_ApplyFiltersSlowlyInit(PreRender* this) { if ((this->cvgSave != NULL) && (this->fbufSave != NULL)) { - if (D_801F6FC0) { + if (sSlowlyRunning) { StackCheck_Cleanup(&sSlowlyStackInfo); Slowly_Destroy(&sSlowlyMgr); } - this->unk_4D = 1; + this->filterState = PRERENDER_FILTER_STATE_PROCESS; StackCheck_Init(&sSlowlyStackInfo, sSlowlyStack, STACK_TOP(sSlowlyStack), 0, 0x100, "slowly"); Slowly_Init(&sSlowlyMgr, STACK_TOP(sSlowlyStack), (void*)PreRender_ApplyFilters, this, NULL); - D_801F6FC0 = true; + sSlowlyRunning = true; } } @@ -439,10 +726,10 @@ void PreRender_ApplyFiltersSlowlyInit(PreRender* this) { * Destroys the "slowly" thread */ void PreRender_ApplyFiltersSlowlyDestroy(PreRender* this) { - if (D_801F6FC0) { + if (sSlowlyRunning) { StackCheck_Cleanup(&sSlowlyStackInfo); Slowly_Destroy(&sSlowlyMgr); - D_801F6FC0 = false; + sSlowlyRunning = false; } } @@ -453,26 +740,121 @@ void func_801720C4(PreRender* this) { } } -#pragma GLOBAL_ASM("asm/non_matchings/code/PreRender/func_801720FC.s") +typedef struct { + /* 0x00 */ void* timg; + /* 0x04 */ void* tlut; + /* 0x08 */ u16 width; + /* 0x0A */ u16 height; + /* 0x0C */ u8 fmt; + /* 0x0D */ u8 siz; + /* 0x0E */ u16 tt; + /* 0x10 */ u16 tlutCount; + /* 0x14 */ f32 x; + /* 0x18 */ f32 y; + /* 0x1C */ f32 xScale; + /* 0x20 */ f32 yScale; + /* 0x24 */ u32 flags; +} PreRenderBackground2DParams; // size = 0x28 + +void Prerender_DrawBackground2DImpl(PreRenderBackground2DParams* bg2D, Gfx** gfxp) { + Gfx* gfx; + uObjBg* bg; + u32 alphaCompare; + Gfx* gfxTemp; + u32 loadS2DEX2; + + loadS2DEX2 = (bg2D->flags & BG2D_FLAGS_LOAD_S2DEX2) != 0; + alphaCompare = (bg2D->flags & BG2D_FLAGS_AC_THRESHOLD) ? G_AC_THRESHOLD : G_AC_NONE; + + gfxTemp = *gfxp; + bg = Graph_DlistAlloc(&gfxTemp, sizeof(uObjBg)); + gfx = gfxTemp; + + bg->b.imageX = 0; + bg->b.imageW = (bg2D->width * (1 << 2)) + 1; + bg->b.frameX = bg2D->x * (1 << 2); + + bg->b.imageY = 0; + bg->b.imageH = (bg2D->height * (1 << 2)) + 1; + bg->b.frameY = bg2D->y * (1 << 2); + + bg->b.imagePtr = bg2D->timg; + bg->b.imageLoad = G_BGLT_LOADTILE; + bg->b.imageFmt = bg2D->fmt; + bg->b.imageSiz = bg2D->siz; + bg->b.imagePal = 0; + bg->b.imageFlip = 0; + + if (loadS2DEX2) { + gSPLoadUcodeL(gfx++, gspS2DEX2_fifo); + } + + if ((bg2D->fmt == G_IM_FMT_CI) && (bg2D->tlut != NULL)) { + gDPLoadTLUT(gfx++, bg2D->tlutCount, 256, bg2D->tlut); + } else { + gDPPipeSync(gfx++); + } + + if (bg2D->flags & BG2D_FLAGS_COPY) { + bg->b.frameW = bg2D->width * (1 << 2); + bg->b.frameH = bg2D->height * (1 << 2); + + guS2DInitBg(bg); + + if (!(bg2D->flags & BG2D_FLAGS_1)) { + gDPSetOtherMode(gfx++, bg2D->tt | G_CYC_COPY, alphaCompare); + } + + gSPBgRectCopy(gfx++, bg); + } else { + bg->b.frameW = (u32)(bg2D->width * (1 << 2)) * bg2D->xScale; + bg->b.frameH = (u32)(bg2D->height * (1 << 2)) * bg2D->yScale; + bg->b.tmemW = (1 << 10) / bg2D->xScale; + bg->b.tmemH = (1 << 10) / bg2D->yScale; + bg->s.imageYorig = bg->b.imageY; + + if (!(bg2D->flags & BG2D_FLAGS_1)) { + gDPSetOtherMode(gfx++, bg2D->tt | G_AD_DISABLE | G_CD_DISABLE | G_TC_FILT, + AA_EN | CVG_X_ALPHA | ALPHA_CVG_SEL | + GBL_c1(G_BL_CLR_IN, G_BL_A_IN, G_BL_CLR_BL, G_BL_1MA) | + GBL_c2(G_BL_CLR_IN, G_BL_A_IN, G_BL_CLR_BL, G_BL_1MA) | alphaCompare); + } + + if (!(bg2D->flags & BG2D_FLAGS_2)) { + gDPSetCombineLERP(gfx++, 0, 0, 0, TEXEL0, 0, 0, 0, 1, 0, 0, 0, TEXEL0, 0, 0, 0, 1); + } + + gSPObjRenderMode(gfx++, G_OBJRM_ANTIALIAS | G_OBJRM_BILERP); + gSPBgRect1Cyc(gfx++, bg); + } + + gDPPipeSync(gfx++); + + if (loadS2DEX2) { + gSPLoadUcode(gfx++, SysUcode_GetUCode(), SysUcode_GetUCodeData()); + } + + *gfxp = gfx; +} void Prerender_DrawBackground2D(Gfx** gfxp, void* timg, void* tlut, u16 width, u16 height, u8 fmt, u8 siz, u16 tt, - u16 arg8, f32 x, f32 y, f32 xScale, f32 yScale, u32 flags) { - PreRenderParams params; - PreRenderParams* paramsp = ¶ms; + u16 tlutCount, f32 x, f32 y, f32 xScale, f32 yScale, u32 flags) { + PreRenderBackground2DParams bg2D; + PreRenderBackground2DParams* bg2DPtr = &bg2D; - params.timg = timg; - params.tlut = tlut; - params.width = width; - params.height = height; - params.fmt = fmt; - params.siz = siz; - params.tt = tt; - params.unk_10 = arg8; - params.x = x; - params.y = y; - params.xScale = xScale; - params.yScale = yScale; - params.flags = flags; + bg2D.timg = timg; + bg2D.tlut = tlut; + bg2D.width = width; + bg2D.height = height; + bg2D.fmt = fmt; + bg2D.siz = siz; + bg2D.tt = tt; + bg2D.tlutCount = tlutCount; + bg2D.x = x; + bg2D.y = y; + bg2D.xScale = xScale; + bg2D.yScale = yScale; + bg2D.flags = flags; - func_801720FC(paramsp, gfxp); + Prerender_DrawBackground2DImpl(bg2DPtr, gfxp); } diff --git a/src/code/listalloc.c b/src/code/listalloc.c index 33f9989ba1..ba346d954d 100644 --- a/src/code/listalloc.c +++ b/src/code/listalloc.c @@ -1,4 +1,4 @@ -#include "global.h" +#include "listalloc.h" #include "system_malloc.h" ListAlloc* ListAlloc_Init(ListAlloc* this) { diff --git a/src/code/z_actor.c b/src/code/z_actor.c index cef3c89da1..a69dada412 100644 --- a/src/code/z_actor.c +++ b/src/code/z_actor.c @@ -2892,7 +2892,7 @@ void Actor_DrawLensActors(PlayState* play, s32 numLensActors, Actor** lensActors play->pauseBgPreRender.fbuf); gfxTemp = gfx; - func_8016FDB8(&play->pauseBgPreRender, &gfxTemp, spA4, zbuffer, 1); + PreRender_CopyImage(&play->pauseBgPreRender, &gfxTemp, spA4, zbuffer, true); gfx = gfxTemp; } diff --git a/src/code/z_lifemeter.c b/src/code/z_lifemeter.c index d9abe151d6..5d1299bbf9 100644 --- a/src/code/z_lifemeter.c +++ b/src/code/z_lifemeter.c @@ -1,4 +1,3 @@ -#include "prevent_bss_reordering.h" #include "global.h" #include "overlays/kaleido_scope/ovl_kaleido_scope/z_kaleido_scope.h" #include "interface/parameter_static/parameter_static.h" diff --git a/src/code/z_play.c b/src/code/z_play.c index 48fbb1f2d4..9bb8d5817b 100644 --- a/src/code/z_play.c +++ b/src/code/z_play.c @@ -83,7 +83,7 @@ void Play_DrawMotionBlur(PlayState* this) { sMotionBlurStatus = MOTION_BLUR_PROCESS; } - func_801705B4(&this->pauseBgPreRender, &gfx); + PreRender_SaveFramebuffer(&this->pauseBgPreRender, &gfx); gSPEndDisplayList(gfx++); @@ -1267,8 +1267,8 @@ void Play_DrawMain(PlayState* this) { if (R_PAUSE_BG_PRERENDER_STATE == PAUSE_BG_PRERENDER_READY) { Gfx* sp8C = POLY_OPA_DISP; - if (this->pauseBgPreRender.unk_4D == 2) { - func_80170B28(&this->pauseBgPreRender, &sp8C); + if (this->pauseBgPreRender.filterState == PRERENDER_FILTER_STATE_DONE) { + PreRender_RestoreFramebuffer(&this->pauseBgPreRender, &sp8C); } else { func_80170798(&this->pauseBgPreRender, &sp8C); } @@ -1405,10 +1405,10 @@ void Play_DrawMain(PlayState* this) { this->pauseBgPreRender.cvgSave = NULL; } - func_801705B4(&this->pauseBgPreRender, &sp74); + PreRender_SaveFramebuffer(&this->pauseBgPreRender, &sp74); if (this->pauseBgPreRender.cvgSave != NULL) { - func_80170730(&this->pauseBgPreRender, &sp74); + PreRender_DrawCoverage(&this->pauseBgPreRender, &sp74); } gSPEndDisplayList(sp74++); diff --git a/src/overlays/actors/ovl_Dm_Char01/z_dm_char01.c b/src/overlays/actors/ovl_Dm_Char01/z_dm_char01.c index f31b6d3081..764b002247 100644 --- a/src/overlays/actors/ovl_Dm_Char01/z_dm_char01.c +++ b/src/overlays/actors/ovl_Dm_Char01/z_dm_char01.c @@ -4,6 +4,7 @@ * Description: Woodfall scene objects (temple, water, walls, etc) */ +#include "prevent_bss_reordering.h" #include "z_dm_char01.h" #include "objects/object_mtoride/object_mtoride.h" #include "overlays/actors/ovl_Obj_Etcetera/z_obj_etcetera.h" diff --git a/tools/disasm/functions.txt b/tools/disasm/functions.txt index e3c54b8947..3f8a338d00 100644 --- a/tools/disasm/functions.txt +++ b/tools/disasm/functions.txt @@ -3057,28 +3057,28 @@ 0x8016FD2C:("PreRender_Init",), 0x8016FD60:("PreRender_SetValues",), 0x8016FD94:("PreRender_Destroy",), - 0x8016FDB8:("func_8016FDB8",), - 0x8016FF70:("func_8016FF70",), + 0x8016FDB8:("PreRender_CopyImage",), + 0x8016FF70:("PreRender_RestoreBuffer",), 0x8016FF90:("func_8016FF90",), 0x80170200:("func_80170200",), - 0x8017023C:("func_8017023C",), - 0x8017057C:("func_8017057C",), - 0x801705B4:("func_801705B4",), - 0x801705EC:("func_801705EC",), - 0x80170730:("func_80170730",), - 0x80170774:("func_80170774",), + 0x8017023C:("PreRender_CoverageRgba16ToI8",), + 0x8017057C:("PreRender_SaveZBuffer",), + 0x801705B4:("PreRender_SaveFramebuffer",), + 0x801705EC:("PreRender_FetchFbufCoverage",), + 0x80170730:("PreRender_DrawCoverage",), + 0x80170774:("PreRender_RestoreZBuffer",), 0x80170798:("func_80170798",), 0x80170AE0:("func_80170AE0",), - 0x80170B28:("func_80170B28",), - 0x80170B4C:("PreRender_AntiAliasAlgorithm",), - 0x8017160C:("PreRender_ApplyAntiAliasingFilter",), - 0x801716C4:("func_801716C4",), - 0x801717F8:("func_801717F8",), + 0x80170B28:("PreRender_RestoreFramebuffer",), + 0x80170B4C:("PreRender_AntiAliasFilterPixel",), + 0x8017160C:("PreRender_AntiAliasFilter",), + 0x801716C4:("PreRender_Get5bMedian9",), + 0x801717F8:("PreRender_DivotFilter",), 0x80171F4C:("PreRender_ApplyFilters",), 0x80171FA8:("PreRender_ApplyFiltersSlowlyInit",), 0x80172078:("PreRender_ApplyFiltersSlowlyDestroy",), 0x801720C4:("func_801720C4",), - 0x801720FC:("func_801720FC",), + 0x801720FC:("Prerender_DrawBackground2DImpl",), 0x80172758:("Prerender_DrawBackground2D",), 0x801727F0:("THGA_Init",), 0x80172810:("THGA_Destroy",), diff --git a/tools/disasm/variables.txt b/tools/disasm/variables.txt index 2da44bc71a..ba2889d54d 100644 --- a/tools/disasm/variables.txt +++ b/tools/disasm/variables.txt @@ -4021,7 +4021,7 @@ 0x801F6DFC:("sBombersNotebookOpen","u8","",0x1), 0x801F6DFD:("sMotionBlurStatus","UNK_TYPE1","",0x1), 0x801F6E00:("sSlowlyMgr","SlowlyMgr","",0x1c0), - 0x801F6FC0:("D_801F6FC0","UNK_TYPE1","",0x1), + 0x801F6FC0:("sSlowlyRunning","UNK_TYPE1","",0x1), 0x801F6FC8:("sSlowlyStackInfo","StackEntry","",0x1c), 0x801F6FE8:("sSlowlyStack","u8","[4096]",0x1000), 0x801F7FF0:("sGameSpeedMeter","SpeedMeter","", 0x20), diff --git a/tools/sizes/code_functions.csv b/tools/sizes/code_functions.csv index ba509ef78e..929ae61a7b 100644 --- a/tools/sizes/code_functions.csv +++ b/tools/sizes/code_functions.csv @@ -2571,28 +2571,28 @@ asm/non_matchings/code/PreRender/PreRender_SetValuesSave.s,PreRender_SetValuesSa asm/non_matchings/code/PreRender/PreRender_Init.s,PreRender_Init,0x8016FD2C,0xD asm/non_matchings/code/PreRender/PreRender_SetValues.s,PreRender_SetValues,0x8016FD60,0xD asm/non_matchings/code/PreRender/PreRender_Destroy.s,PreRender_Destroy,0x8016FD94,0x9 -asm/non_matchings/code/PreRender/func_8016FDB8.s,func_8016FDB8,0x8016FDB8,0x6E -asm/non_matchings/code/PreRender/func_8016FF70.s,func_8016FF70,0x8016FF70,0x8 +asm/non_matchings/code/PreRender/PreRender_CopyImage.s,PreRender_CopyImage,0x8016FDB8,0x6E +asm/non_matchings/code/PreRender/PreRender_RestoreBuffer.s,PreRender_RestoreBuffer,0x8016FF70,0x8 asm/non_matchings/code/PreRender/func_8016FF90.s,func_8016FF90,0x8016FF90,0x9C asm/non_matchings/code/PreRender/func_80170200.s,func_80170200,0x80170200,0xF -asm/non_matchings/code/PreRender/func_8017023C.s,func_8017023C,0x8017023C,0xD0 -asm/non_matchings/code/PreRender/func_8017057C.s,func_8017057C,0x8017057C,0xE -asm/non_matchings/code/PreRender/func_801705B4.s,func_801705B4,0x801705B4,0xE -asm/non_matchings/code/PreRender/func_801705EC.s,func_801705EC,0x801705EC,0x51 -asm/non_matchings/code/PreRender/func_80170730.s,func_80170730,0x80170730,0x11 -asm/non_matchings/code/PreRender/func_80170774.s,func_80170774,0x80170774,0x9 +asm/non_matchings/code/PreRender/PreRender_CoverageRgba16ToI8.s,PreRender_CoverageRgba16ToI8,0x8017023C,0xD0 +asm/non_matchings/code/PreRender/PreRender_SaveZBuffer.s,PreRender_SaveZBuffer,0x8017057C,0xE +asm/non_matchings/code/PreRender/PreRender_SaveFramebuffer.s,PreRender_SaveFramebuffer,0x801705B4,0xE +asm/non_matchings/code/PreRender/PreRender_FetchFbufCoverage.s,PreRender_FetchFbufCoverage,0x801705EC,0x51 +asm/non_matchings/code/PreRender/PreRender_DrawCoverage.s,PreRender_DrawCoverage,0x80170730,0x11 +asm/non_matchings/code/PreRender/PreRender_RestoreZBuffer.s,PreRender_RestoreZBuffer,0x80170774,0x9 asm/non_matchings/code/PreRender/func_80170798.s,func_80170798,0x80170798,0xD2 asm/non_matchings/code/PreRender/func_80170AE0.s,func_80170AE0,0x80170AE0,0x12 -asm/non_matchings/code/PreRender/func_80170B28.s,func_80170B28,0x80170B28,0x9 -asm/non_matchings/code/PreRender/PreRender_AntiAliasAlgorithm.s,PreRender_AntiAliasAlgorithm,0x80170B4C,0x2B0 -asm/non_matchings/code/PreRender/PreRender_ApplyAntiAliasingFilter.s,PreRender_ApplyAntiAliasingFilter,0x8017160C,0x2E -asm/non_matchings/code/PreRender/func_801716C4.s,func_801716C4,0x801716C4,0x4D -asm/non_matchings/code/PreRender/func_801717F8.s,func_801717F8,0x801717F8,0x1D5 +asm/non_matchings/code/PreRender/PreRender_RestoreFramebuffer.s,PreRender_RestoreFramebuffer,0x80170B28,0x9 +asm/non_matchings/code/PreRender/PreRender_AntiAliasFilterPixel.s,PreRender_AntiAliasFilterPixel,0x80170B4C,0x2B0 +asm/non_matchings/code/PreRender/PreRender_AntiAliasFilter.s,PreRender_AntiAliasFilter,0x8017160C,0x2E +asm/non_matchings/code/PreRender/PreRender_Get5bMedian9.s,PreRender_Get5bMedian9,0x801716C4,0x4D +asm/non_matchings/code/PreRender/PreRender_DivotFilter.s,PreRender_DivotFilter,0x801717F8,0x1D5 asm/non_matchings/code/PreRender/PreRender_ApplyFilters.s,PreRender_ApplyFilters,0x80171F4C,0x17 asm/non_matchings/code/PreRender/PreRender_ApplyFiltersSlowlyInit.s,PreRender_ApplyFiltersSlowlyInit,0x80171FA8,0x34 asm/non_matchings/code/PreRender/PreRender_ApplyFiltersSlowlyDestroy.s,PreRender_ApplyFiltersSlowlyDestroy,0x80172078,0x13 asm/non_matchings/code/PreRender/func_801720C4.s,func_801720C4,0x801720C4,0xE -asm/non_matchings/code/PreRender/func_801720FC.s,func_801720FC,0x801720FC,0x197 +asm/non_matchings/code/PreRender/Prerender_DrawBackground2DImpl.s,Prerender_DrawBackground2DImpl,0x801720FC,0x197 asm/non_matchings/code/PreRender/Prerender_DrawBackground2D.s,Prerender_DrawBackground2D,0x80172758,0x26 asm/non_matchings/code/TwoHeadGfxArena/THGA_Init.s,THGA_Init,0x801727F0,0x8 asm/non_matchings/code/TwoHeadGfxArena/THGA_Destroy.s,THGA_Destroy,0x80172810,0x8