From 4eba49d9f8aa8059f7a1de387e2c7df2b48d82a9 Mon Sep 17 00:00:00 2001 From: robojumper Date: Sun, 16 Mar 2025 15:37:19 +0100 Subject: [PATCH] Rough outline for LightTextureManager --- config/SOUE01/splits.txt | 1 + config/SOUE01/symbols.txt | 10 +- include/egg/gfx/eggLightTexture.h | 22 ++-- include/egg/gfx/eggLightTextureMgr.h | 34 +++++- include/egg/gfx/eggStateGX.h | 1 + include/egg/prim/eggBinary.h | 14 +-- src/egg/gfx/eggLightTexture.cpp | 2 +- src/egg/gfx/eggLightTextureMgr.cpp | 156 ++++++++++++++++++++++++++- 8 files changed, 215 insertions(+), 25 deletions(-) diff --git a/config/SOUE01/splits.txt b/config/SOUE01/splits.txt index 9ed8d492..4cc3bc24 100644 --- a/config/SOUE01/splits.txt +++ b/config/SOUE01/splits.txt @@ -2256,6 +2256,7 @@ egg/gfx/eggLightTexture.cpp: egg/gfx/eggLightTextureMgr.cpp: .text start:0x804AD450 end:0x804AE644 + .data start:0x8056F0F0 end:0x8056F110 egg/gfx/eggModelEx.cpp: .text start:0x804AE650 end:0x804AEC20 diff --git a/config/SOUE01/symbols.txt b/config/SOUE01/symbols.txt index 2b45fd62..b9d8a2d0 100644 --- a/config/SOUE01/symbols.txt +++ b/config/SOUE01/symbols.txt @@ -26665,8 +26665,8 @@ __sinit_\eggLightTexture_cpp = .text:0x804AD3C0; // type:function size:0x44 scop __ct__Q23EGG19LightTextureManagerFPCQ23EGG12LightManager = .text:0x804AD450; // type:function size:0x1AC __dt__Q23EGG19LightTextureManagerFv = .text:0x804AD600; // type:function size:0x90 createTexture__Q23EGG19LightTextureManagerFPCc = .text:0x804AD690; // type:function size:0x110 -EGG__LightTextureManager__createTextureFromBin = .text:0x804AD7A0; // type:function size:0x5C -EGG__LightTextureManager__createTexturesFromBin = .text:0x804AD800; // type:function size:0x6C +createTextureFromBin__Q23EGG19LightTextureManagerFPCv = .text:0x804AD7A0; // type:function size:0x5C +createTexturesFromBin__Q23EGG19LightTextureManagerFPCv = .text:0x804AD800; // type:function size:0x6C deleteTexture__Q23EGG19LightTextureManagerFi = .text:0x804AD870; // type:function size:0x84 replaceModelTextures__Q23EGG19LightTextureManagerCFQ34nw4r3g3d6ResMdl = .text:0x804AD900; // type:function size:0x84 replaceModelTexture__Q23EGG19LightTextureManagerCFiQ34nw4r3g3d6ResMdl = .text:0x804AD990; // type:function size:0x104 @@ -26677,7 +26677,7 @@ frameReset__Q23EGG19LightTextureManagerFv = .text:0x804ADC40; // type:function s drawAndCaptureTexture__Q23EGG19LightTextureManagerFffff = .text:0x804ADC80; // type:function size:0x4C8 SetBinaryInner__Q23EGG19LightTextureManagerFRCQ33EGG36IBinary3Bin = .text:0x804AE150; // type:function size:0x80 GetBinaryInner__Q23EGG19LightTextureManagerCFPQ33EGG36IBinary3Bin = .text:0x804AE1D0; // type:function size:0x90 -getLTEXfromLMAP = .text:0x804AE260; // type:function size:0xDC +getLtexFromLmap__Q23EGG19LightTextureManagerFPCvUs = .text:0x804AE260; // type:function size:0xDC FUN_804ae340 = .text:0x804AE340; // type:function size:0xBC GetBinarySize__Q23EGG19LightTextureManagerCFv = .text:0x804AE400; // type:function size:0x8 SetBinaryInner__Q23EGG36IBinaryFRCQ33EGG36IBinary3BinRCQ33EGG36IBinary3Binf = .text:0x804AE410; // type:function size:0x4 @@ -37372,7 +37372,7 @@ __vt__Q23EGG8IScnProc = .data:0x8056F058; // type:object size:0x10 EGG__LightManager__vtable = .data:0x8056F068; // type:object size:0x30 EGG__LightObject__vtable = .data:0x8056F098; // type:object size:0x20 __vt__Q23EGG12LightTexture = .data:0x8056F0B8; // type:object size:0x38 -EGG__LightTextureManager__vtable = .data:0x8056F0F0; // type:object size:0x20 +__vt__Q23EGG19LightTextureManager = .data:0x8056F0F0; // type:object size:0x1C lbl_8056F110 = .data:0x8056F110; // type:object size:0x10 jumptable_8056F120 = .data:0x8056F120; // type:object size:0x30 scope:local __vt__Q23EGG14PostEffectBase = .data:0x8056F150; // type:object size:0x1C @@ -41185,7 +41185,7 @@ lbl_805768A2 = .sbss:0x805768A2; // type:object size:0x2 data:2byte lbl_805768A4 = .sbss:0x805768A4; // type:object size:0x4 data:4byte lbl_805768A8 = .sbss:0x805768A8; // type:object size:0x4 data:4byte lbl_805768AC = .sbss:0x805768AC; // type:object size:0x1 data:byte -lbl_805768B0 = .sbss:0x805768B0; // type:object size:0x2 data:2byte +s_flag__Q23EGG7StateGX = .sbss:0x805768B0; // type:object size:0x2 data:2byte s_commandFlag__Q23EGG7StateGX = .sbss:0x805768B2; // type:object size:0x6 data:2byte lbl_805768B8 = .sbss:0x805768B8; // type:object size:0x4 data:4byte lbl_805768BC = .sbss:0x805768BC; // type:object size:0x4 data:4byte diff --git a/include/egg/gfx/eggLightTexture.h b/include/egg/gfx/eggLightTexture.h index e14fa4fb..c278e227 100644 --- a/include/egg/gfx/eggLightTexture.h +++ b/include/egg/gfx/eggLightTexture.h @@ -12,13 +12,14 @@ class LightTextureManager; class LightTexture : public CapTexture, public IBinary { public: +#pragma pack(push, 1) struct SubData { /* 0x00 */ f32 mIntensity; /* 0x04 */ u8 mGradientUsed; /* 0x05 */ u8 field_0x05; /* 0x06 */ u8 _0x06[2]; }; - + // Implicit +0x10 from BinHeader struct BinData { /* 0x00 */ u16 mNumEntries; @@ -26,29 +27,34 @@ public: /* 0x03 */ u8 field_0x03; /* 0x04 */ char mName[32]; /* 0x24 */ u8 mType; - /* 0x25 */ u8 _0x25[7]; + /* 0x25 */ u16 field_0x25; + /* 0x27 */ u8 field_0x27; + /* 0x28 */ u8 _0x28[4]; /* 0x2C */ f32 field_0x2C; /* 0x30 */ u8 _0x30[4]; /* 0x34 */ char mName2[32]; /* 0x54 */ f32 field_0x54; - - u8 _0x00[0x1D]; + u8 _0x00[0x20]; SubData mSubData[1]; }; - +#pragma pack(pop) + LightTexture(const char *name, const LightTextureManager *mgr); virtual ~LightTexture(); - + virtual void configure() override; // at 0xC virtual void SetBinaryInner(const Bin &) override; virtual void GetBinaryInner(Bin *) const override; virtual size_t GetBinarySize() const override; - + static void initialize(u16 textureSize, Heap *pHeap); -private: + const char *getName() const { + return mName1; + } +private: f32 getFloat(u16 idx) const { return mpFloatData[idx]; } diff --git a/include/egg/gfx/eggLightTextureMgr.h b/include/egg/gfx/eggLightTextureMgr.h index eafa4524..7696dd6c 100644 --- a/include/egg/gfx/eggLightTextureMgr.h +++ b/include/egg/gfx/eggLightTextureMgr.h @@ -1,29 +1,57 @@ #ifndef EGG_LIGHT_TEXTURE_MGR_H #define EGG_LIGHT_TEXTURE_MGR_H - +#include "egg/gfx/eggLightTexture.h" #include "egg/prim/eggBinary.h" #include "nw4r/g3d/res/g3d_resmdl.h" namespace EGG { +class LightManager; +class LightObject; + class LightTextureManager : public IBinary { public: - struct BinData {}; + struct BinData { + u16 mDataCount; + u8 _0x02[14]; + BinHeader mSubData[1]; + }; + LightTextureManager(const LightManager *lightMgr); virtual ~LightTextureManager(); virtual void SetBinaryInner(const Bin &) override; virtual void GetBinaryInner(Bin *) const override; virtual size_t GetBinarySize() const override; - void replaceModelTextures(nw4r::g3d::ResMdl) const; + u16 createTexture(const char *name); + bool setBinaryToTexture(const void *data); + bool deleteTexture(int idx); + void replaceModelTextures(nw4r::g3d::ResMdl) const; void drawAndCaptureTexture(f32, f32, f32, f32); + // Inofficial + static const void *getLtexFromLmap(const void *lmap, u16 index); + int createTexturesFromBin(const void *bin); + u16 createTextureFromBin(const void *bin); + u16 getMaxNumLightTextures() const { return mMaxNumTextures; } + + u16 getNumLightTextures() const { + return mTextureCount; + } + private: + /* 0x04 */ u8 field_0x04; + /* 0x06 */ u16 mTextureCount; + /* 0x08 */ LightTexture **mpTextures; + /* 0x0C */ const LightManager *mpLightMgr; + /* 0x10 */ u32 field_0x10; /* 0x14 */ u16 mMaxNumTextures; + /* 0x16 */ u8 field_0x16; + /* 0x18 */ LightObject **mpObjects; }; } // namespace EGG diff --git a/include/egg/gfx/eggStateGX.h b/include/egg/gfx/eggStateGX.h index 321586ba..fe712a3e 100644 --- a/include/egg/gfx/eggStateGX.h +++ b/include/egg/gfx/eggStateGX.h @@ -92,6 +92,7 @@ public: static u16 s_commandFlag; + static u16 s_flag; }; } // namespace EGG diff --git a/include/egg/prim/eggBinary.h b/include/egg/prim/eggBinary.h index b1c17cf0..9b517545 100644 --- a/include/egg/prim/eggBinary.h +++ b/include/egg/prim/eggBinary.h @@ -53,11 +53,17 @@ public: // These functions below are automatically provided, you should not need to // touch them to implement de-/serialization for your type. They will parse // the binary header and then invoke the above virtual functions. - void GetBinary(void *pData) const; void SetBinary(const void *); + void GetBinary(void *pData) const; void SetBinaryBlend(const void *a, const void *b, f32 blend); }; +template +void IBinary::SetBinary(const void *a) { + const Bin *pBinA = reinterpret_cast(a); + SetBinaryInner(*pBinA); +} + template void IBinary::GetBinary(void *pData) const { Bin *pBin = reinterpret_cast(pData); @@ -74,12 +80,6 @@ void IBinary::GetBinary(void *pData) const { GetBinaryInner(pBin); } -template -void IBinary::SetBinary(const void *a) { - const Bin *pBinA = reinterpret_cast(a); - SetBinaryInner(*pBinA); -} - template void IBinary::SetBinaryBlend(const void *a, const void *b, f32 blend) { const Bin *pBinA = reinterpret_cast(a); diff --git a/src/egg/gfx/eggLightTexture.cpp b/src/egg/gfx/eggLightTexture.cpp index 13f2049f..768b447f 100644 --- a/src/egg/gfx/eggLightTexture.cpp +++ b/src/egg/gfx/eggLightTexture.cpp @@ -19,7 +19,7 @@ static const int sCpuTexGradientOp[NUM_CPU_TEX] = {0, 0, 0, 1, 2, 3, 4, 0, 5, 5, static nw4r::math::MTX34 sMtx(1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f, 0.0f); -u16 LightTexture::sTextureSize; +u16 LightTexture::sTextureSize = 0x20; CpuTexture *LightTexture::spNormalEnvironment; static s8 lbl_80574F3A = 0xFE; diff --git a/src/egg/gfx/eggLightTextureMgr.cpp b/src/egg/gfx/eggLightTextureMgr.cpp index 6d8d0928..5aee8709 100644 --- a/src/egg/gfx/eggLightTextureMgr.cpp +++ b/src/egg/gfx/eggLightTextureMgr.cpp @@ -1,3 +1,157 @@ #include "egg/gfx/eggLightTextureMgr.h" -namespace EGG {} // namespace EGG +#include "common.h" +#include "egg/gfx/eggLightTexture.h" +#include "egg/gfx/eggStateGX.h" + +#include + +namespace EGG { + +LightTextureManager::LightTextureManager(const LightManager *lightMgr) { + field_0x04 = 0x78; + mTextureCount = 0; + mpTextures = nullptr; + mpLightMgr = lightMgr; + field_0x10 = 0; + mMaxNumTextures = 0x80; + field_0x16 = 1; + mpObjects = nullptr; + mpTextures = new LightTexture *[0x80]; + for (int i = 0; i < 0x80; i++) { + mpTextures[i] = nullptr; + } + + mpObjects = new LightObject *[mMaxNumTextures]; + for (int i = 0; i < mMaxNumTextures; i++) { + mpObjects[i] = nullptr; + } + + if ((StateGX::s_flag & 0x40) != 0) { + field_0x04 |= 0x80; + field_0x04 &= ~0x40; + field_0x16 = 3; + } +} + +LightTextureManager::~LightTextureManager() { + for (int i = 0; i < 0x80; i++) { + deleteTexture(i); + } + delete[] mpTextures; + delete[] mpObjects; +} + +u16 LightTextureManager::createTexture(const char *name) { + // Check if one already exists with the same name + for (int i = 0; i < getNumLightTextures(); i++) { + if (mpTextures[i] != nullptr && !strcmp(name, mpTextures[i]->getName())) { + return i; + } + } + + // Find a hole to fill + int targetPosition = getNumLightTextures(); + for (int i = 0; i < getNumLightTextures(); i++) { + if (mpTextures[i] == nullptr) { + targetPosition = i; + } + } + if (targetPosition == getNumLightTextures()) { + mTextureCount++; + } + LightTexture *tex = new LightTexture(name, this); + mpTextures[targetPosition] = tex; + tex->configure(); + return targetPosition; +} + +u16 LightTextureManager::createTextureFromBin(const void *bin) { + const LightTexture::Bin *data = reinterpret_cast(bin); + u16 idx = createTexture(data->mData.mName); + mpTextures[idx]->SetBinary(data); + return idx; +} + +int LightTextureManager::createTexturesFromBin(const void *bin) { + const LightTextureManager::Bin *data = reinterpret_cast(bin); + // Moderate inefficiency here - this algorithm has quadratic runtime + // since we always search from the beginning + for (int i = 0; i < data->mData.mDataCount; i++) { + const void *subData = getLtexFromLmap(data, (u16)i); + createTextureFromBin(subData); + } + return data->mData.mDataCount; +} + +bool LightTextureManager::deleteTexture(int idx) { + if (0 <= idx && idx < 0x80) { + if (mpTextures[idx] != nullptr) { + delete mpTextures[idx]; + mpTextures[idx] = nullptr; + return true; + } + } + return false; +} + +bool LightTextureManager::setBinaryToTexture(const void *data) { + const LightTexture::Bin *bin = reinterpret_cast(data); + for (int i = 0; i < getNumLightTextures(); i++) { + if (!std::strcmp(bin->mData.mName, mpTextures[i]->getName())) { + mpTextures[i]->SetBinary(bin); + return true; + } + } + return false; +} + +void LightTextureManager::SetBinaryInner(const Bin &bin) { + if (bin.mHeader.mVersion != 0) { + return; + } + const char *buf = reinterpret_cast(bin.mData.mSubData); + for (int i = 0; i < bin.mData.mDataCount; i++) { + const BinHeader *hdr = reinterpret_cast(buf); + setBinaryToTexture(buf); + buf += hdr->mSize; + } +} + +void LightTextureManager::GetBinaryInner(Bin *pOutBin) const { + int count = 0; + char *buf = reinterpret_cast(pOutBin->mData.mSubData); + for (int i = 0; i < getNumLightTextures(); i++) { + mpTextures[i]->GetBinary(buf); + BinHeader *hdr = reinterpret_cast(buf); + // This is where the actually written size is added to the whole object + pOutBin->mHeader.mSize += hdr->mSize; + buf += hdr->mSize; + count++; + } + pOutBin->mData.mDataCount = count; +} + +// Performs a linear search through the data to find the offset for the given index +const void *LightTextureManager::getLtexFromLmap(const void *lmap, u16 index) { + // A bit awkward but it matches + const BinHeader *buf = nullptr; + const Bin *bin = reinterpret_cast(lmap); + if (bin->mHeader.mVersion == 0 && index < bin->mData.mDataCount) { + buf = bin->mData.mSubData; + for (int i = 0; i < index; i++) { + buf = reinterpret_cast(reinterpret_cast(buf) + buf->mSize); + } + } + + return buf; +} + +size_t LightTextureManager::GetBinarySize() const { + // Contrary to the other dynamically-sized Bins, this only + // returns the fixed size. GetBinaryInner will add the dynamic + // parts when writing. + return sizeof(Bin) - sizeof(BinHeader); +} + +} // namespace EGG