diff --git a/asm/Kyoto/Graphics/CGX.s b/asm/Kyoto/Graphics/CGX.s index 61125ac9..30221a9c 100644 --- a/asm/Kyoto/Graphics/CGX.s +++ b/asm/Kyoto/Graphics/CGX.s @@ -704,7 +704,7 @@ SetFog__3CGXF10_GXFogTypeffffRC8_GXColor: /* 80308530 00305490 D0 85 02 58 */ stfs f4, 0x258(r5) /* 80308534 00305494 90 85 02 5C */ stw r4, 0x25c(r5) /* 80308538 00305498 40 82 00 10 */ bne lbl_80308548 -/* 8030853C 0030549C 80 02 8C A8 */ lwz r0, lbl_805AA9C8@sda21(r2) +/* 8030853C 0030549C 80 02 8C A8 */ lwz r0, black$localstatic3$apply_fog__3CGXFv@sda21(r2) /* 80308540 003054A0 90 01 00 0C */ stw r0, 0xc(r1) /* 80308544 003054A4 48 00 00 08 */ b lbl_8030854C lbl_80308548: diff --git a/asm/MetroidPrime/ScriptObjects/CScriptPlatform.s b/asm/MetroidPrime/ScriptObjects/CScriptPlatform.s index 757e28dc..d12958a0 100644 --- a/asm/MetroidPrime/ScriptObjects/CScriptPlatform.s +++ b/asm/MetroidPrime/ScriptObjects/CScriptPlatform.s @@ -123,8 +123,8 @@ lbl_805A720C: .section .sdata2, "a" .balign 8 -.global lbl_805AA9C8 -lbl_805AA9C8: +.global black$localstatic3$apply_fog__3CGXFv +black$localstatic3$apply_fog__3CGXFv: # ROM: 0x3F7268 .4byte 0 diff --git a/include/Kyoto/Graphics/CGX.hpp b/include/Kyoto/Graphics/CGX.hpp index d2220cec..d005ee31 100644 --- a/include/Kyoto/Graphics/CGX.hpp +++ b/include/Kyoto/Graphics/CGX.hpp @@ -3,12 +3,9 @@ #include "types.h" -#include "dolphin/gx.h" #include -#ifdef __MWERKS__ -#pragma cpp_extensions on -#endif +#include "dolphin/gx.h" class CGX { public: @@ -54,12 +51,12 @@ public: GXColor x40_chanMatColors[2]; u32 x48_descList; union { - u8 x4c_flags; + u8 x4c_chanFlags; struct { - u8 x4c_unk : 5; - u8 x4c_dirtyChanCtrl : 2; - u8 x4c_numChansDirty : 1; - }; + u8 unused : 5; + u8 chansDirty : 2; + u8 numDirty : 1; + } x4c_flags; }; u8 x4d_prevNumChans; u8 x4e_numChans; @@ -142,6 +139,12 @@ public: private: static void FlushState(); static void update_fog(u32 flags); + static void apply_fog() { + static const GXColor black = {0, 0, 0, 0}; + GXSetFog(static_cast< GXFogType >(sGXState.x53_fogType), sGXState.x24c_fogParams.x0_fogStartZ, sGXState.x24c_fogParams.x4_fogEndZ, + sGXState.x24c_fogParams.x8_fogNearZ, sGXState.x24c_fogParams.xc_fogFarZ, + (sGXState.x56_blendMode & 0xE0) == 0x20 ? black : sGXState.x24c_fogParams.x10_fogColor); + } static SGXState sGXState; }; diff --git a/obj_files.mk b/obj_files.mk index d771261c..7cbefe5e 100644 --- a/obj_files.mk +++ b/obj_files.mk @@ -514,7 +514,7 @@ KYOTO_1 :=\ $(BUILD_DIR)/asm/Kyoto/Text/CFont.o\ $(BUILD_DIR)/asm/Kyoto/Graphics/CLight.o\ $(BUILD_DIR)/asm/Kyoto/Graphics/CCubeModel.o\ - $(BUILD_DIR)/asm/Kyoto/Graphics/CGX.o\ + $(BUILD_DIR)/src/Kyoto/Graphics/CGX.o\ $(BUILD_DIR)/asm/Kyoto/Graphics/CTevCombiners.o\ $(BUILD_DIR)/asm/Kyoto/Graphics/DolphinCGraphics.o\ $(BUILD_DIR)/asm/Kyoto/Graphics/DolphinCPalette.o\ diff --git a/src/Kyoto/Graphics/CGX.cpp b/src/Kyoto/Graphics/CGX.cpp index 4a98513e..d3f1f194 100644 --- a/src/Kyoto/Graphics/CGX.cpp +++ b/src/Kyoto/Graphics/CGX.cpp @@ -6,11 +6,11 @@ CGX::SGXState CGX::sGXState; -static GXVtxDescList sVtxDescList[GX_MAX_VTXDESCLIST_SZ]; +static GXVtxDescList sVtxDescList[30]; void CGX::SetNumChans(u8 num) { sGXState.x4e_numChans = num; - sGXState.x4c_numChansDirty = sGXState.x4e_numChans != sGXState.x4d_prevNumChans; + sGXState.x4c_flags.numDirty = sGXState.x4e_numChans != sGXState.x4d_prevNumChans; } void CGX::SetNumTexGens(u8 num) { @@ -36,18 +36,17 @@ void CGX::SetChanMatColor(EChannelId channel, const GXColor& color) { } } -// TODO https://decomp.me/scratch/09nam void CGX::SetChanCtrl(EChannelId channel, GXBool enable, GXColorSrc ambSrc, GXColorSrc matSrc, GXLightID lights, GXDiffuseFn diffFn, GXAttnFn attnFn) { + u16& state = sGXState.x34_chanCtrls[channel]; u16 prevFlags = sGXState.x30_prevChanCtrls[channel]; - u32 flags = enable; if (lights == GX_LIGHT_NULL) { - flags = 0; + enable = GX_FALSE; } - flags = MaskAndShiftLeft(flags, 1, 0) | MaskAndShiftLeft(ambSrc, 1, 1) | MaskAndShiftLeft(matSrc, 1, 2) | - MaskAndShiftLeft(lights, 0xFF, 3) | MaskAndShiftLeft(diffFn, 3, 11) | MaskAndShiftLeft(attnFn, 3, 13); - sGXState.x34_chanCtrls[channel] = flags; - sGXState.x4c_flags = ((flags != prevFlags) << (channel + 1)) | (sGXState.x4c_flags & ~(1 << (channel + 1))); + u32 flags = MaskAndShiftLeft(enable, 1, 0) | MaskAndShiftLeft(ambSrc, 1, 1) | MaskAndShiftLeft(matSrc, 1, 2) | + MaskAndShiftLeft(lights, 0xFF, 3) | MaskAndShiftLeft(diffFn, 3, 11) | MaskAndShiftLeft(attnFn, 3, 13); + state = flags; + sGXState.x4c_chanFlags = ((flags != prevFlags) << (channel + 1)) | (sGXState.x4c_chanFlags & ~(1 << (channel + 1))); } void CGX::SetNumTevStages(u8 num) { @@ -57,7 +56,6 @@ void CGX::SetNumTevStages(u8 num) { } } -// TODO https://decomp.me/scratch/V2iVD void CGX::SetTevKColor(GXTevKColorID id, const GXColor& color) { if (!CompareGXColors(sGXState.x58_kColors[id], color)) { CopyGXColor(sGXState.x58_kColors[id], color); @@ -148,9 +146,8 @@ void CGX::SetTevOrder(GXTevStageID stageId, GXTexCoordID texCoord, GXTexMapID te } } -// TODO https://decomp.me/scratch/YWJTk void CGX::SetBlendMode(GXBlendMode mode, GXBlendFactor srcFac, GXBlendFactor dstFac, GXLogicOp op) { - u16 flags = MaskAndShiftLeft(mode, 3, 0) | MaskAndShiftLeft(srcFac, 7, 2) | MaskAndShiftLeft(dstFac, 7, 5) | MaskAndShiftLeft(op, 0xF, 8); + u32 flags = MaskAndShiftLeft(mode, 3, 0) | MaskAndShiftLeft(srcFac, 7, 2) | MaskAndShiftLeft(dstFac, 7, 5) | MaskAndShiftLeft(op, 0xF, 8); if (flags != sGXState.x56_blendMode) { update_fog(flags); sGXState.x56_blendMode = flags; @@ -230,14 +227,14 @@ void CGX::SetArray(GXAttr attr, const void* data, u8 stride) { } void CGX::CallDisplayList(const void* ptr, size_t size) { - if (sGXState.x4c_flags != 0) { + if (sGXState.x4c_chanFlags != 0) { FlushState(); } GXCallDisplayList(ptr, size); } void CGX::Begin(GXPrimitive prim, GXVtxFmt fmt, u16 numVtx) { - if (sGXState.x4c_flags != 0) { + if (sGXState.x4c_chanFlags != 0) { FlushState(); } GXBegin(prim, fmt, numVtx); @@ -252,9 +249,7 @@ void CGX::SetFog(GXFogType type, f32 startZ, f32 endZ, f32 nearZ, f32 farZ, cons sGXState.x24c_fogParams.x8_fogNearZ = nearZ; sGXState.x24c_fogParams.xc_fogFarZ = farZ; CopyGXColor(sGXState.x24c_fogParams.x10_fogColor, color); - GXSetFog(static_cast< GXFogType >(sGXState.x53_fogType), sGXState.x24c_fogParams.x0_fogStartZ, sGXState.x24c_fogParams.x4_fogEndZ, - sGXState.x24c_fogParams.x8_fogNearZ, sGXState.x24c_fogParams.xc_fogFarZ, - (sGXState.x56_blendMode & 0xE0) == 0x20 ? GXColor() : sGXState.x24c_fogParams.x10_fogColor); + apply_fog(); } void CGX::SetLineWidth(u8 width, GXTexOffset offset) { @@ -340,11 +335,11 @@ void CGX::ResetGXStatesFull() { } void CGX::FlushState() { - if (sGXState.x4c_numChansDirty) { + if (sGXState.x4c_chanFlags & 1) { GXSetNumChans(sGXState.x4e_numChans); sGXState.x4d_prevNumChans = sGXState.x4e_numChans; } - if (sGXState.x4c_flags & 2) { + if (sGXState.x4c_chanFlags & 2) { u16 flags = sGXState.x34_chanCtrls[0]; GXBool enable = ShiftRightAndMask(flags, 1, 0); GXColorSrc ambSrc = static_cast< GXColorSrc >(ShiftRightAndMask(flags, 1, 1)); @@ -355,7 +350,7 @@ void CGX::FlushState() { GXSetChanCtrl(GX_COLOR0, enable, ambSrc, matSrc, lightMask, diffFn, attnFn); sGXState.x30_prevChanCtrls[0] = sGXState.x34_chanCtrls[0]; } - if (sGXState.x4c_flags & 4) { + if (sGXState.x4c_chanFlags & 4) { u16 flags = sGXState.x34_chanCtrls[1]; GXBool enable = ShiftRightAndMask(flags, 1, 0); GXColorSrc ambSrc = static_cast< GXColorSrc >(ShiftRightAndMask(flags, 1, 1)); @@ -366,7 +361,7 @@ void CGX::FlushState() { GXSetChanCtrl(GX_COLOR1, enable, ambSrc, matSrc, lightMask, diffFn, attnFn); sGXState.x30_prevChanCtrls[1] = sGXState.x34_chanCtrls[1]; } - sGXState.x4c_flags = 0; + sGXState.x4c_chanFlags = 0; } void CGX::SetIndTexMtxSTPointFive(GXIndTexMtxID id, s8 scaleExp) {