metaforce/Runtime/Graphics/CCubeModel.cpp

275 lines
10 KiB
C++
Raw Normal View History

2022-02-02 07:18:03 +00:00
#include "Runtime/Graphics/CCubeModel.hpp"
#include "Runtime/Graphics/CGraphics.hpp"
#include "Runtime/CSimplePool.hpp"
2022-02-01 09:18:42 +00:00
// TODO Remove WIP once we've transitioned to this
2022-02-02 07:18:03 +00:00
namespace metaforce::WIP {
2022-02-01 09:18:42 +00:00
#pragma region CModel
u32 CModel::sTotalMemory = 0;
2022-02-01 08:14:49 +00:00
u32 CModel::sFrameCounter = 0;
bool CModel::sIsTextureTimeoutEnabled = true;
CModel* CModel::sThisFrameList = nullptr;
CModel* CModel::sOneFrameList = nullptr;
CModel* CModel::sTwoFrameList = nullptr;
2022-02-02 07:18:03 +00:00
static u8* MemoryFromPartData(u8*& dataCur, const u32*& secSizeCur) {
u8* ret = nullptr;
2022-02-01 00:06:54 +00:00
if (*secSizeCur != 0) {
ret = dataCur;
}
dataCur += hecl::SBig(*secSizeCur);
++secSizeCur;
return ret;
}
CModel::CModel(std::unique_ptr<u8[]> in, u32 dataLen, IObjectStore* store)
2022-02-01 09:18:42 +00:00
: x0_data(std::move(in))
, x4_dataLen(dataLen)
, x34_next(sThisFrameList)
, x38_lastFrame(CGraphics::GetFrameCounter() - 2) {
2022-02-02 07:18:03 +00:00
u8* data = x0_data.get();
u32 flags = hecl::SBig(*reinterpret_cast<u32*>(data + 8));
2022-02-01 00:06:54 +00:00
u32 sectionSizeStart = 0x2c;
2022-02-02 07:18:03 +00:00
if (hecl::SBig(*reinterpret_cast<u32*>(data + 4)) == 1) {
2022-02-01 00:06:54 +00:00
sectionSizeStart = 0x28;
}
2022-02-02 07:18:03 +00:00
const u32* secSizeCur = reinterpret_cast<u32*>(data + sectionSizeStart);
2022-02-01 00:06:54 +00:00
s32 numMatSets = 1;
2022-02-02 07:18:03 +00:00
if (hecl::SBig(*reinterpret_cast<u32*>(data + 4)) > 1) {
numMatSets = hecl::SBig(*reinterpret_cast<s32*>(data + 0x28));
2022-02-01 00:06:54 +00:00
}
2022-02-02 07:18:03 +00:00
u8* dataCur = data + ROUND_UP_32(sectionSizeStart + hecl::SBig(*reinterpret_cast<s32*>(data + 0x24)) * 4);
2022-02-01 00:06:54 +00:00
x18_matSets.reserve(numMatSets);
2022-02-01 08:14:49 +00:00
for (s32 i = 0; i < numMatSets; ++i) {
2022-02-02 07:18:03 +00:00
x18_matSets.emplace_back(static_cast<u8*>(MemoryFromPartData(dataCur, secSizeCur)));
2022-02-04 08:10:09 +00:00
auto& shader = x18_matSets.back();
CCubeModel::MakeTexturesFromMats(shader.x10_data, shader.x0_textures, store, true);
2022-02-01 09:18:42 +00:00
x4_dataLen += shader.x0_textures.size() * sizeof(TCachedToken<CTexture>);
}
2022-02-02 07:18:03 +00:00
/* Metaforce note: Due to padding in zeus types we need to convert these and store locally */
u32 numVertices = hecl::SBig(*secSizeCur) / (sizeof(float) * 3);
2022-02-01 09:18:42 +00:00
const u8* positions = MemoryFromPartData(dataCur, secSizeCur);
2022-02-02 07:18:03 +00:00
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;
2022-02-01 09:18:42 +00:00
const u8* normals = MemoryFromPartData(dataCur, secSizeCur);
2022-02-02 07:18:03 +00:00
for (u32 i = 0; i < numNormals; ++i) {
if ((flags & 2) == 0) {
const auto* norm = reinterpret_cast<const float*>(normals + (i * (sizeof(float) * 3)));
m_normals.emplace_back(hecl::SBig(norm[0]), hecl::SBig(norm[1]), hecl::SBig(norm[2]));
2022-02-02 07:18:03 +00:00
} else {
const auto* norm = reinterpret_cast<const s16*>(normals + (i * (sizeof(s16) * 3)));
2022-02-03 15:26:33 +00:00
m_normals.emplace_back(hecl::SBig(norm[0]) / 32767.f, hecl::SBig(norm[1]) / 32767.f,
hecl::SBig(norm[2]) / 32767.f);
2022-02-02 07:18:03 +00:00
}
}
u32 numColors = hecl::SBig(*secSizeCur) / (sizeof(int));
2022-02-01 09:18:42 +00:00
const u8* vtxColors = MemoryFromPartData(dataCur, secSizeCur);
2022-02-02 07:18:03 +00:00
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);
2022-02-01 09:18:42 +00:00
const u8* floatUVs = MemoryFromPartData(dataCur, secSizeCur);
2022-02-02 07:18:03 +00:00
for (u32 i = 0; i < numFloatUVs; ++i) {
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])});
}
2022-02-01 09:18:42 +00:00
}
const u8* surfaceInfo = MemoryFromPartData(dataCur, secSizeCur);
u32 surfaceCount = hecl::SBig(*reinterpret_cast<const u32*>(surfaceInfo));
x8_surfaces.reserve(surfaceCount);
for (u32 i = 0; i < surfaceCount; ++i) {
2022-02-02 07:18:03 +00:00
if (x8_surfaces.capacity() <= x8_surfaces.size()) {
x8_surfaces.reserve(x8_surfaces.capacity() * 2);
}
x8_surfaces.emplace_back(std::make_unique<CCubeSurface>(MemoryFromPartData(dataCur, secSizeCur)));
2022-02-01 09:18:42 +00:00
}
2022-02-02 07:18:03 +00:00
const float* bounds = reinterpret_cast<float*>(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])};
2022-02-02 07:18:03 +00:00
/* 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_colors, &m_normals, &m_floatUVs, &m_shortUVs, aabox, flags, true, -1);
2022-02-01 09:18:42 +00:00
sThisFrameList = this;
if (x34_next != nullptr) {
x34_next->x30_prev = this;
2022-02-01 08:14:49 +00:00
}
2022-02-01 09:18:42 +00:00
x4_dataLen += x8_surfaces.size() * 4;
sTotalMemory += x4_dataLen;
// DCFlushRange(x0_data, dataLen);
2022-02-01 08:14:49 +00:00
}
void CModel::UpdateLastFrame() { x38_lastFrame = CGraphics::GetFrameCounter(); }
void CModel::MoveToThisFrameList() {
UpdateLastFrame();
2022-02-01 09:18:42 +00:00
if (sThisFrameList == this) {
return;
2022-02-01 00:06:54 +00:00
}
2022-02-01 08:14:49 +00:00
2022-02-02 07:18:03 +00:00
RemoveFromList();
2022-02-01 09:18:42 +00:00
if (sThisFrameList != nullptr) {
x34_next = sThisFrameList;
x34_next->x30_prev = this;
}
sThisFrameList = this;
2022-02-01 00:06:54 +00:00
}
2022-02-01 08:14:49 +00:00
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) {
2022-02-02 07:18:03 +00:00
auto* iter = sTwoFrameList;
2022-02-01 08:14:49 +00:00
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; }
2022-02-01 09:18:42 +00:00
#pragma endregion
#pragma region CCubeModel
CCubeModel::CCubeModel(const std::vector<std::unique_ptr<CCubeSurface>>* surfaces,
const std::vector<TCachedToken<CTexture>>* textures, const u8* materialData,
const std::vector<zeus::CVector3f>* positions, const std::vector<zeus::CColor>* colors,
const std::vector<zeus::CVector3f>* normals, const std::vector<zeus::CVector2f>* texCoords,
const std::vector<std::array<s16, 2>>* 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);
}
2022-02-04 08:10:09 +00:00
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<const u32*>(matData + (idx * 4)));
}
u32 materialCount = hecl::SBig(*reinterpret_cast<const u32*>(matData));
return CCubeMaterial(matData + materialOffset + (materialCount * 4) + 4);
}
2022-02-01 09:18:42 +00:00
void CCubeModel::UnlockTextures() {}
2022-02-04 08:10:09 +00:00
void CCubeModel::MakeTexturesFromMats(const u8* ptr, std::vector<TCachedToken<CTexture>>& textures, IObjectStore* store,
bool b1) {
const u32* curId = reinterpret_cast<const u32*>(ptr + 4);
u32 textureCount = hecl::SBig(*reinterpret_cast<const u32*>(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();
}
}
}
2022-02-01 09:18:42 +00:00
#pragma endregion
2022-02-02 07:18:03 +00:00
#pragma region CCubeSurface
2022-02-04 08:10:09 +00:00
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<CCubeModel*>(mem.readUint32Big());
x18_nextSurface = reinterpret_cast<CCubeSurface*>(mem.readUint32Big());
x1c_extraSize = mem.readUint32Big();
x20_normal.readBig(mem);
if (x1c_extraSize > 0) {
x2c_bounds = zeus::CAABox::ReadBoundingBoxBig(mem);
}
}
2022-02-04 08:10:09 +00:00
2022-02-02 07:18:03 +00:00
#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::WIP