More CModel re-reverse engineering

This commit is contained in:
Phillip Stephens 2022-02-01 23:18:03 -08:00
parent 05f841a5ce
commit aca91b9361
Signed by: Antidote
GPG Key ID: F8BEE4C83DACA60D
4 changed files with 108 additions and 37 deletions

View File

@ -1,9 +1,9 @@
#include "CCubeModel.hpp" #include "Runtime/Graphics/CCubeModel.hpp"
#include "Runtime/Graphics/CGraphics.hpp"
#include "CGraphics.hpp" #include "Runtime/CSimplePool.hpp"
namespace metaforce {
//TODO Remove WIP once we've transitioned to this
namespace metaforce::WIP {
#pragma region CModel #pragma region CModel
u32 CModel::sTotalMemory = 0; u32 CModel::sTotalMemory = 0;
u32 CModel::sFrameCounter = 0; u32 CModel::sFrameCounter = 0;
@ -12,8 +12,8 @@ CModel* CModel::sThisFrameList = nullptr;
CModel* CModel::sOneFrameList = nullptr; CModel* CModel::sOneFrameList = nullptr;
CModel* CModel::sTwoFrameList = nullptr; CModel* CModel::sTwoFrameList = nullptr;
static const u8* MemoryFromPartData(const u8*& dataCur, const u32*& secSizeCur) { static u8* MemoryFromPartData(u8*& dataCur, const u32*& secSizeCur) {
const u8* ret = nullptr; u8* ret = nullptr;
if (*secSizeCur != 0) { if (*secSizeCur != 0) {
ret = dataCur; ret = dataCur;
} }
@ -27,33 +27,70 @@ CModel::CModel(std::unique_ptr<u8[]> in, u32 dataLen, IObjectStore* store)
, x4_dataLen(dataLen) , x4_dataLen(dataLen)
, x34_next(sThisFrameList) , x34_next(sThisFrameList)
, x38_lastFrame(CGraphics::GetFrameCounter() - 2) { , x38_lastFrame(CGraphics::GetFrameCounter() - 2) {
const u8* data = reinterpret_cast<const u8*>(x0_data.get()); u8* data = x0_data.get();
u32 flags = *reinterpret_cast<const u32*>(data + 8); u32 flags = hecl::SBig(*reinterpret_cast<u32*>(data + 8));
u32 sectionSizeStart = 0x2c; u32 sectionSizeStart = 0x2c;
if (hecl::SBig(*reinterpret_cast<const u32*>(data + 4)) == 1) { if (hecl::SBig(*reinterpret_cast<u32*>(data + 4)) == 1) {
sectionSizeStart = 0x28; sectionSizeStart = 0x28;
} }
const u32* secSizeCur = reinterpret_cast<const u32*>(data + sectionSizeStart); const u32* secSizeCur = reinterpret_cast<u32*>(data + sectionSizeStart);
s32 numMatSets = 1; s32 numMatSets = 1;
if (hecl::SBig(*reinterpret_cast<const u32*>(data + 4)) > 1) { if (hecl::SBig(*reinterpret_cast<u32*>(data + 4)) > 1) {
numMatSets = hecl::SBig(*reinterpret_cast<const s32*>(data + 0x28)); numMatSets = hecl::SBig(*reinterpret_cast<s32*>(data + 0x28));
} }
const u8* dataCur = data + ROUND_UP_32(sectionSizeStart + hecl::SBig(*reinterpret_cast<const s32*>(data + 0x24)) * 4); u8* dataCur = data + ROUND_UP_32(sectionSizeStart + hecl::SBig(*reinterpret_cast<s32*>(data + 0x24)) * 4);
x18_matSets.reserve(numMatSets); x18_matSets.reserve(numMatSets);
for (s32 i = 0; i < numMatSets; ++i) { for (s32 i = 0; i < numMatSets; ++i) {
x18_matSets.emplace_back(static_cast<const u8*>(MemoryFromPartData(dataCur, secSizeCur))); x18_matSets.emplace_back(static_cast<u8*>(MemoryFromPartData(dataCur, secSizeCur)));
auto shader = x18_matSets.back(); auto shader = x18_matSets.back();
CCubeModel::MakeTexturesFromMats(shader.x10_data, shader.x0_textures, true); CCubeModel::MakeTexturesFromMats(shader.x10_data, shader.x0_textures, true);
x4_dataLen += shader.x0_textures.size() * sizeof(TCachedToken<CTexture>); x4_dataLen += shader.x0_textures.size() * sizeof(TCachedToken<CTexture>);
} }
/* Metaforce note: Due to padding in zeus types we need to convert these and store locally */
u32 numVertices = hecl::SBig(*secSizeCur) / (sizeof(float) * 3);
const u8* positions = MemoryFromPartData(dataCur, secSizeCur); const u8* positions = MemoryFromPartData(dataCur, secSizeCur);
for (u32 i = 0; i < numVertices; ++i) {
const auto* pos = reinterpret_cast<const float*>(positions + (i * (sizeof(float) * 3)));
m_positions.emplace_back(hecl::SBig(pos[0]), hecl::SBig(pos[1]), hecl::SBig(pos[2]));
}
u32 numNormals = hecl::SBig(*secSizeCur);
numNormals /= ((flags & 2) == 0 ? sizeof(float) : sizeof(s16)) * 3;
const u8* normals = MemoryFromPartData(dataCur, secSizeCur); const u8* normals = MemoryFromPartData(dataCur, secSizeCur);
for (u32 i = 0; i < numNormals; ++i) {
if ((flags & 2) == 0) {
const auto* norm = reinterpret_cast<const float*>(normals + (i * (sizeof(float) * 3)));
m_floatNormals.emplace_back(hecl::SBig(norm[0]), hecl::SBig(norm[1]), hecl::SBig(norm[2]));
} else {
const auto* norm = reinterpret_cast<const s16*>(normals + (i * (sizeof(s16) * 3)));
m_shortNormals.emplace_back(std::array{hecl::SBig(norm[0]), hecl::SBig(norm[1]), hecl::SBig(norm[2])});
}
}
u32 numColors = hecl::SBig(*secSizeCur) / (sizeof(int));
const u8* vtxColors = MemoryFromPartData(dataCur, secSizeCur); const u8* vtxColors = MemoryFromPartData(dataCur, secSizeCur);
for (u32 i = 0; i < numColors; ++i) {
const u32 col = hecl::SBig(*reinterpret_cast<const u32*>(vtxColors + (i * (sizeof(u32)))));
m_colors.emplace_back(zeus::CColor(zeus::Comp32(col)));
}
u32 numFloatUVs = hecl::SBig(*reinterpret_cast<const u32*>(secSizeCur)) / (sizeof(float) * 2);
const u8* floatUVs = MemoryFromPartData(dataCur, secSizeCur); const u8* floatUVs = MemoryFromPartData(dataCur, secSizeCur);
const u8* shortUVs = nullptr;
if (((flags >> 2) & 1) != 0) { for (u32 i = 0; i < numFloatUVs; ++i) {
shortUVs = MemoryFromPartData(dataCur, secSizeCur); const auto* norm = reinterpret_cast<const float*>(floatUVs + (i * (sizeof(float) * 2)));
m_floatUVs.emplace_back(hecl::SBig(norm[0]), hecl::SBig(norm[1]));
}
if ((flags & 4) != 0) {
u32 numShortUVs = hecl::SBig(*reinterpret_cast<const u32*>(secSizeCur)) / (sizeof(s16) * 2);
const u8* shortUVs = MemoryFromPartData(dataCur, secSizeCur);
for (u32 i = 0; i < numShortUVs; ++i) {
const auto* norm = reinterpret_cast<const s16*>(shortUVs + (i * (sizeof(s16) * 2)));
m_shortUVs.emplace_back(std::array{hecl::SBig(norm[0]), hecl::SBig(norm[1])});
}
} }
const u8* surfaceInfo = MemoryFromPartData(dataCur, secSizeCur); const u8* surfaceInfo = MemoryFromPartData(dataCur, secSizeCur);
@ -61,14 +98,21 @@ CModel::CModel(std::unique_ptr<u8[]> in, u32 dataLen, IObjectStore* store)
x8_surfaces.reserve(surfaceCount); x8_surfaces.reserve(surfaceCount);
for (u32 i = 0; i < surfaceCount; ++i) { for (u32 i = 0; i < surfaceCount; ++i) {
// Implement CCubeSurface loading taking into account endian if (x8_surfaces.capacity() <= x8_surfaces.size()) {
x8_surfaces.reserve(x8_surfaces.capacity() * 2);
} }
// TODO: need to endian swap the values x8_surfaces.emplace_back(MemoryFromPartData(dataCur, secSizeCur));
const auto* aabox = reinterpret_cast<const zeus::CAABox*>(data + 12); }
x28_modelInst =
std::make_unique<CCubeModel>(&x8_surfaces, &x18_matSets[0].x0_textures, x18_matSets[0].x10_data, positions, const float* bounds = reinterpret_cast<float*>(data + 12);
normals, vtxColors, floatUVs, shortUVs, aabox, flags, true, -1); m_aabox.min = {hecl::SBig(bounds[0]), hecl::SBig(bounds[1]), hecl::SBig(bounds[2])};
m_aabox.max = {hecl::SBig(bounds[3]), hecl::SBig(bounds[4]), hecl::SBig(bounds[5])};
/* This constructor has been changed from the original to take into account platform differences */
x28_modelInst = std::make_unique<CCubeModel>(&x8_surfaces, &x18_matSets[0].x0_textures, x18_matSets[0].x10_data,
&m_positions, &m_floatNormals, &m_shortNormals, &m_colors, &m_floatUVs,
&m_shortUVs, &m_aabox, flags, true, -1);
sThisFrameList = this; sThisFrameList = this;
if (x34_next != nullptr) { if (x34_next != nullptr) {
@ -86,6 +130,7 @@ void CModel::MoveToThisFrameList() {
return; return;
} }
RemoveFromList();
if (sThisFrameList != nullptr) { if (sThisFrameList != nullptr) {
x34_next = sThisFrameList; x34_next = sThisFrameList;
x34_next->x30_prev = this; x34_next->x30_prev = this;
@ -115,8 +160,8 @@ void CModel::RemoveFromList() {
void CModel::FrameDone() { void CModel::FrameDone() {
++sFrameCounter; ++sFrameCounter;
auto* iter = sTwoFrameList;
if (sIsTextureTimeoutEnabled) { if (sIsTextureTimeoutEnabled) {
auto* iter = sTwoFrameList;
while (iter != nullptr) { while (iter != nullptr) {
auto* next = iter->x34_next; auto* next = iter->x34_next;
iter->VerifyCurrentShader(0); iter->VerifyCurrentShader(0);
@ -148,4 +193,15 @@ void CCubeModel::MakeTexturesFromMats(const u8* ptr, std::vector<TCachedToken<CT
#pragma endregion #pragma endregion
#pragma region CCubeSurface
CCubeSurface::CCubeSurface(u8* ptr) {}
#pragma endregion
CFactoryFnReturn FModelFactory(const metaforce::SObjectTag& tag, std::unique_ptr<u8[]>&& in, u32 len,
const metaforce::CVParamTransfer& vparms, CObjectReference* selfRef) {
CSimplePool* sp = vparms.GetOwnedObj<CSimplePool*>();
CFactoryFnReturn ret = TToken<CModel>::GetIObjObjectFor(std::make_unique<CModel>(std::move(in), len, sp));
return ret;
}
} // namespace metaforce } // namespace metaforce

View File

@ -3,12 +3,12 @@
#include <memory> #include <memory>
#include <vector> #include <vector>
#include "GCNTypes.hpp" #include "Runtime/GCNTypes.hpp"
#include "IObjectStore.hpp" #include "Runtime/IObjectStore.hpp"
#include "CTexture.hpp" #include "Runtime/Graphics/CTexture.hpp"
#include "CToken.hpp" #include "Runtime/CToken.hpp"
namespace metaforce { namespace metaforce::WIP {
class CCubeSurface; class CCubeSurface;
class CCubeModel; class CCubeModel;
@ -34,7 +34,7 @@ private:
std::unique_ptr<u8[]> x0_data; std::unique_ptr<u8[]> x0_data;
u32 x4_dataLen; u32 x4_dataLen;
std::vector<CCubeSurface*> x8_surfaces; std::vector<CCubeSurface> x8_surfaces;
std::vector<SShader> x18_matSets; std::vector<SShader> x18_matSets;
std::unique_ptr<CCubeModel> x28_modelInst = nullptr; std::unique_ptr<CCubeModel> x28_modelInst = nullptr;
u16 x2c_ = 0; u16 x2c_ = 0;
@ -42,6 +42,14 @@ private:
CModel* x30_prev = nullptr; CModel* x30_prev = nullptr;
CModel* x34_next; CModel* x34_next;
u32 x38_lastFrame; u32 x38_lastFrame;
/* Resident copies of maintained data */
zeus::CAABox m_aabox;
std::vector<zeus::CVector3f> m_positions;
std::vector<zeus::CVector3f> m_floatNormals;
std::vector<std::array<s16, 3>> m_shortNormals;
std::vector<zeus::CColor> m_colors;
std::vector<zeus::CVector2f> m_floatUVs;
std::vector<std::array<s16, 2>> m_shortUVs;
public: public:
CModel(std::unique_ptr<u8[]> in, u32 dataLen, IObjectStore* store); CModel(std::unique_ptr<u8[]> in, u32 dataLen, IObjectStore* store);
@ -60,9 +68,11 @@ public:
#pragma region CCubeModel #pragma region CCubeModel
class CCubeModel { class CCubeModel {
public: public:
CCubeModel(const std::vector<CCubeSurface*>* surfaces, const std::vector<TCachedToken<CTexture>>* textures, CCubeModel(const std::vector<CCubeSurface>* surfaces, const std::vector<TCachedToken<CTexture>>* textures,
const u8* materialData, const u8* positions, const u8* normals, const u8* vtxColors, const u8* floatUvs, const u8* materialData, const std::vector<zeus::CVector3f>* positions,
const u8* shortUVs, const zeus::CAABox* aabox, u8 flags, bool b1, u32 w1) {} const std::vector<zeus::CVector3f>* floatNormals, const std::vector<std::array<s16, 3>>* shortNormals,
const std::vector<zeus::CColor>* vtxColors, const std::vector<zeus::CVector2f>* floatUvs,
const std::vector<std::array<s16, 2>>* shortUVs, const zeus::CAABox* aabox, u8 flags, bool b1, u32 w1) {}
void UnlockTextures(); void UnlockTextures();
@ -72,14 +82,17 @@ public:
#pragma region CCubeSurface #pragma region CCubeSurface
class CCubeSurface { class CCubeSurface {
static constexpr zeus::CVector3f skDefaultNormal{1.f, 0.f, 0.f};
public: public:
enum class ECookie { enum class ECookie {
}; };
public: public:
CCubeSurface(u8* ptr);
bool IsValid() const; bool IsValid() const;
static CCubeSurface* FromCookieValue(u32); static CCubeSurface* FromCookieValue(u32 value);
void GetCookie(ECookie cookie); void GetCookie(ECookie cookie);
void SetCookie(ECookie cookie, u32 value); void SetCookie(ECookie cookie, u32 value);
u32 GetCookieValue() const; u32 GetCookieValue() const;
@ -93,4 +106,6 @@ public:
}; };
#pragma endregion #pragma endregion
CFactoryFnReturn FModelFactory(const metaforce::SObjectTag& tag, std::unique_ptr<u8[]>&& in, u32 len,
const metaforce::CVParamTransfer& vparms, CObjectReference* selfRef);
} // namespace metaforce } // namespace metaforce

View File

@ -308,7 +308,7 @@ public:
static void WarmupShaders(const SObjectTag& cmdlTag); static void WarmupShaders(const SObjectTag& cmdlTag);
}; };
CFactoryFnReturn FModelFactory(const metaforce::SObjectTag& tag, std::unique_ptr<u8[]>&& in, u32 len, CFactoryFnReturn FPCModelFactory(const metaforce::SObjectTag& tag, std::unique_ptr<u8[]>&& in, u32 len,
const metaforce::CVParamTransfer& vparms, CObjectReference* selfRef); const metaforce::CVParamTransfer& vparms, CObjectReference* selfRef);
} // namespace metaforce } // namespace metaforce

View File

@ -1335,7 +1335,7 @@ void CModel::WarmupShaders(const SObjectTag& cmdlTag) {
modelObj->_WarmupShaders(); modelObj->_WarmupShaders();
} }
CFactoryFnReturn FModelFactory(const metaforce::SObjectTag& tag, std::unique_ptr<u8[]>&& in, u32 len, CFactoryFnReturn FPCModelFactory(const metaforce::SObjectTag& tag, std::unique_ptr<u8[]>&& in, u32 len,
const metaforce::CVParamTransfer& vparms, CObjectReference* selfRef) { const metaforce::CVParamTransfer& vparms, CObjectReference* selfRef) {
CSimplePool* sp = vparms.GetOwnedObj<CSimplePool*>(); CSimplePool* sp = vparms.GetOwnedObj<CSimplePool*>();
CFactoryFnReturn ret = TToken<CModel>::GetIObjObjectFor(std::make_unique<CModel>(std::move(in), len, sp, selfRef)); CFactoryFnReturn ret = TToken<CModel>::GetIObjObjectFor(std::make_unique<CModel>(std::move(in), len, sp, selfRef));