#include "Runtime/Graphics/CCubeModel.hpp" #include "Runtime/Graphics/CGraphics.hpp" #include "Runtime/CSimplePool.hpp" // TODO Remove WIP once we've transitioned to this namespace metaforce::WIP { #pragma region CModel u32 CModel::sTotalMemory = 0; u32 CModel::sFrameCounter = 0; bool CModel::sIsTextureTimeoutEnabled = true; CModel* CModel::sThisFrameList = nullptr; CModel* CModel::sOneFrameList = nullptr; CModel* CModel::sTwoFrameList = nullptr; static u8* MemoryFromPartData(u8*& dataCur, const u32*& secSizeCur) { u8* ret = nullptr; if (*secSizeCur != 0) { ret = dataCur; } dataCur += hecl::SBig(*secSizeCur); ++secSizeCur; return ret; } CModel::CModel(std::unique_ptr in, u32 dataLen, IObjectStore* store) : x0_data(std::move(in)) , x4_dataLen(dataLen) , x34_next(sThisFrameList) , x38_lastFrame(CGraphics::GetFrameCounter() - 2) { u8* data = x0_data.get(); u32 flags = hecl::SBig(*reinterpret_cast(data + 8)); u32 sectionSizeStart = 0x2c; if (hecl::SBig(*reinterpret_cast(data + 4)) == 1) { sectionSizeStart = 0x28; } const u32* secSizeCur = reinterpret_cast(data + sectionSizeStart); s32 numMatSets = 1; if (hecl::SBig(*reinterpret_cast(data + 4)) > 1) { numMatSets = hecl::SBig(*reinterpret_cast(data + 0x28)); } u8* dataCur = data + ROUND_UP_32(sectionSizeStart + hecl::SBig(*reinterpret_cast(data + 0x24)) * 4); x18_matSets.reserve(numMatSets); for (s32 i = 0; i < numMatSets; ++i) { x18_matSets.emplace_back(static_cast(MemoryFromPartData(dataCur, secSizeCur))); auto& shader = x18_matSets.back(); CCubeModel::MakeTexturesFromMats(shader.x10_data, shader.x0_textures, store, true); x4_dataLen += shader.x0_textures.size() * sizeof(TCachedToken); } /* 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); for (u32 i = 0; i < numVertices; ++i) { const auto* pos = reinterpret_cast(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); for (u32 i = 0; i < numNormals; ++i) { if ((flags & 2) == 0) { const auto* norm = reinterpret_cast(normals + (i * (sizeof(float) * 3))); m_normals.emplace_back(hecl::SBig(norm[0]), hecl::SBig(norm[1]), hecl::SBig(norm[2])); } else { const auto* norm = reinterpret_cast(normals + (i * (sizeof(s16) * 3))); m_normals.emplace_back(hecl::SBig(norm[0]) / 32767.f, hecl::SBig(norm[1]) / 32767.f, hecl::SBig(norm[2]) / 32767.f); } } u32 numColors = hecl::SBig(*secSizeCur) / (sizeof(int)); const u8* vtxColors = MemoryFromPartData(dataCur, secSizeCur); for (u32 i = 0; i < numColors; ++i) { const u32 col = hecl::SBig(*reinterpret_cast(vtxColors + (i * (sizeof(u32))))); m_colors.emplace_back(zeus::CColor(zeus::Comp32(col))); } u32 numFloatUVs = hecl::SBig(*reinterpret_cast(secSizeCur)) / (sizeof(float) * 2); const u8* floatUVs = MemoryFromPartData(dataCur, secSizeCur); for (u32 i = 0; i < numFloatUVs; ++i) { const auto* norm = reinterpret_cast(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(secSizeCur)) / (sizeof(s16) * 2); const u8* shortUVs = MemoryFromPartData(dataCur, secSizeCur); for (u32 i = 0; i < numShortUVs; ++i) { const auto* norm = reinterpret_cast(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); u32 surfaceCount = hecl::SBig(*reinterpret_cast(surfaceInfo)); x8_surfaces.reserve(surfaceCount); for (u32 i = 0; i < surfaceCount; ++i) { if (x8_surfaces.capacity() <= x8_surfaces.size()) { x8_surfaces.reserve(x8_surfaces.capacity() * 2); } x8_surfaces.emplace_back(std::make_unique(MemoryFromPartData(dataCur, secSizeCur))); } const float* bounds = reinterpret_cast(data + 12); zeus::CAABox aabox = zeus::skNullBox; aabox.min = {hecl::SBig(bounds[0]), hecl::SBig(bounds[1]), hecl::SBig(bounds[2])}; 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(&x8_surfaces, &x18_matSets[0].x0_textures, x18_matSets[0].x10_data, &m_positions, &m_colors, &m_normals, &m_floatUVs, &m_shortUVs, aabox, flags, true, -1); sThisFrameList = this; if (x34_next != nullptr) { x34_next->x30_prev = this; } x4_dataLen += x8_surfaces.size() * 4; sTotalMemory += x4_dataLen; // DCFlushRange(x0_data, dataLen); } void CModel::UpdateLastFrame() { x38_lastFrame = CGraphics::GetFrameCounter(); } void CModel::MoveToThisFrameList() { UpdateLastFrame(); if (sThisFrameList == this) { return; } RemoveFromList(); if (sThisFrameList != nullptr) { x34_next = sThisFrameList; x34_next->x30_prev = this; } sThisFrameList = this; } void CModel::RemoveFromList() { if (x30_prev == nullptr) { if (sThisFrameList == this) { sThisFrameList = x34_next; } else if (sOneFrameList == this) { sOneFrameList = x34_next; } else if (sTwoFrameList == this) { sTwoFrameList = x34_next; } } else { x30_prev->x34_next = x34_next; } if (x34_next != nullptr) { x34_next->x30_prev = x30_prev; } x30_prev = nullptr; x34_next = nullptr; } void CModel::FrameDone() { ++sFrameCounter; if (sIsTextureTimeoutEnabled) { auto* iter = sTwoFrameList; while (iter != nullptr) { auto* next = iter->x34_next; iter->VerifyCurrentShader(0); for (auto& shader : iter->x18_matSets) { shader.UnlockTextures(); } iter->x28_modelInst->UnlockTextures(); iter->x34_next = nullptr; iter->x30_prev = nullptr; iter = next; } sTwoFrameList = sOneFrameList; sOneFrameList = sThisFrameList; sThisFrameList = nullptr; } } void CModel::EnableTextureTimeout() { sIsTextureTimeoutEnabled = true; } void CModel::DisableTextureTimeout() { sIsTextureTimeoutEnabled = false; } #pragma endregion #pragma region CCubeModel CCubeModel::CCubeModel(std::vector>* surfaces, std::vector>* textures, const u8* materialData, const std::vector* positions, const std::vector* colors, const std::vector* normals, const std::vector* texCoords, const std::vector>* packedTexCoords, const zeus::CAABox& aabox, u8 flags, bool b1, u32 idx) : x0_modelInstance(surfaces, materialData, positions, colors, normals, texCoords, packedTexCoords) , x1c_textures(textures) , x20_worldAABB(aabox) , x40_24_(!b1) , x41_visorFlags(flags) , x44_idx(idx) { for (const auto& surf : x0_modelInstance.Surfaces()) { surf->SetParent(this); } for (u32 i = x0_modelInstance.Surfaces().size(); i > 0; --i) { const auto& surf = x0_modelInstance.Surfaces()[i - 1]; if (!GetMaterialByIndex(surf->GetMaterialIndex()).IsFlagSet(EStateFlags::DepthSorting)) { surf->SetNextSurface(x38_firstUnsortedSurf); x38_firstUnsortedSurf = surf.get(); } else { surf->SetNextSurface(x3c_firstSortedSurf); x3c_firstSortedSurf = surf.get(); } } } CCubeMaterial CCubeModel::GetMaterialByIndex(u32 idx) { u32 materialOffset = 0; const u8* matData = x0_modelInstance.GetMaterialPointer(); matData += (x1c_textures->size() + 1) * 4; if (idx != 0) { materialOffset = hecl::SBig(*reinterpret_cast(matData + (idx * 4))); } u32 materialCount = hecl::SBig(*reinterpret_cast(matData)); return CCubeMaterial(matData + materialOffset + (materialCount * 4) + 4); } void CCubeModel::UnlockTextures() { for (TCachedToken& tex : *x1c_textures) { tex.Unlock(); } } void CCubeModel::MakeTexturesFromMats(const u8* ptr, std::vector>& textures, IObjectStore* store, bool b1) { const u32* curId = reinterpret_cast(ptr + 4); u32 textureCount = hecl::SBig(*reinterpret_cast(ptr)); textures.reserve(textureCount); for (u32 i = 0; i < textureCount; ++i) { textures.emplace_back(store->GetObj({FOURCC('TXTR'), hecl::SBig(curId[i])})); if (!b1 && textures.back().IsNull()) { textures.back().GetObj(); } } } #pragma endregion #pragma region CCubeSurface CCubeSurface::CCubeSurface(u8* ptr) : x0_data(ptr) { CMemoryInStream mem(ptr, 10000); // Oversized so we can read everything in x0_center.readBig(mem); xc_materialIndex = mem.readUint32Big(); x10_displayListSize = mem.readUint32Big(); x14_parent = reinterpret_cast(mem.readUint32Big()); x18_nextSurface = reinterpret_cast(mem.readUint32Big()); x1c_extraSize = mem.readUint32Big(); x20_normal.readBig(mem); if (x1c_extraSize > 0) { x2c_bounds = zeus::CAABox::ReadBoundingBoxBig(mem); } } #pragma endregion CFactoryFnReturn FModelFactory(const metaforce::SObjectTag& tag, std::unique_ptr&& in, u32 len, const metaforce::CVParamTransfer& vparms, CObjectReference* selfRef) { CSimplePool* sp = vparms.GetOwnedObj(); CFactoryFnReturn ret = TToken::GetIObjObjectFor(std::make_unique(std::move(in), len, sp)); return ret; } } // namespace metaforce::WIP