metaforce/Runtime/Graphics/CModelBoo.cpp

468 lines
15 KiB
C++
Raw Normal View History

2016-03-04 23:04:53 +00:00
#include "Graphics/CModel.hpp"
2016-03-30 20:44:19 +00:00
#include "Graphics/CTexture.hpp"
2016-03-30 19:16:01 +00:00
#include "Graphics/CGraphics.hpp"
#include "hecl/HMDLMeta.hpp"
2016-03-31 02:44:43 +00:00
#include "hecl/Runtime.hpp"
2016-02-13 00:57:09 +00:00
2016-03-04 23:04:53 +00:00
namespace urde
2016-02-13 00:57:09 +00:00
{
2016-03-29 23:14:14 +00:00
static logvisor::Module Log("urde::CModelBoo");
2016-03-30 19:16:01 +00:00
bool CBooModel::g_DrawingOccluders = false;
2016-03-29 23:14:14 +00:00
2016-03-31 02:44:43 +00:00
CBooModel::CBooModel(std::vector<CBooSurface>* surfaces, SShader& shader,
boo::IVertexFormat* vtxFmt, boo::IGraphicsBufferS* vbo, boo::IGraphicsBufferS* ibo,
const zeus::CAABox& aabb, u8 shortNormals, bool texturesLoaded)
: x0_surfaces(surfaces), x4_matSet(&shader.m_matSet), m_pipelines(&shader.m_shaders),
m_vtxFmt(vtxFmt), x8_vbo(vbo), xc_ibo(ibo), x1c_textures(&shader.x0_textures), x20_aabb(aabb),
x40_24_texturesLoaded(texturesLoaded), x40_25_(0), x41_shortNormals(shortNormals)
{
for (CBooSurface& surf : *x0_surfaces)
2016-03-29 23:14:14 +00:00
surf.m_parent = this;
for (auto it=x0_surfaces->rbegin() ; it != x0_surfaces->rend() ; ++it)
{
2016-03-31 02:44:43 +00:00
u32 matId = it->m_data.matIdx;
2016-03-31 06:18:56 +00:00
const MaterialSet::Material& matData = GetMaterialByIndex(matId);
2016-03-31 02:44:43 +00:00
if (matData.flags.depthSorting())
2016-03-29 23:14:14 +00:00
{
it->m_next = x3c_firstSortedSurface;
x3c_firstSortedSurface = &*it;
}
else
{
it->m_next = x38_firstUnsortedSurface;
x38_firstUnsortedSurface = &*it;
}
}
2016-03-31 02:44:43 +00:00
if (x40_24_texturesLoaded)
BuildGfxToken();
2016-03-29 23:14:14 +00:00
}
2016-03-31 02:44:43 +00:00
void CBooModel::BuildGfxToken()
2016-03-29 23:14:14 +00:00
{
2016-03-31 06:18:56 +00:00
m_gfxToken = CGraphics::CommitResources(
[&](boo::IGraphicsDataFactory::Context& ctx) -> bool
2016-03-29 23:14:14 +00:00
{
2016-03-31 06:18:56 +00:00
m_unskinnedXfBuffer = ctx.newDynamicBuffer(boo::BufferUse::Vertex,
sizeof(SUnskinnedXf), 1);
boo::IGraphicsBuffer* bufs[] = {m_unskinnedXfBuffer};
2016-03-31 02:44:43 +00:00
m_shaderDataBindings.reserve(x4_matSet->materials.size());
auto pipelineIt = m_pipelines->begin();
std::vector<boo::ITexture*> texs;
2016-03-31 06:18:56 +00:00
for (const MaterialSet::Material& mat : x4_matSet->materials)
2016-03-31 02:44:43 +00:00
{
texs.clear();
texs.reserve(mat.textureIdxs.size());
for (atUint32 idx : mat.textureIdxs)
{
TCachedToken<CTexture>& tex = (*x1c_textures)[idx];
texs.push_back(tex.GetObj()->GetBooTexture());
}
2016-03-31 06:18:56 +00:00
m_shaderDataBindings.push_back(
ctx.newShaderDataBinding(*pipelineIt, m_vtxFmt,
x8_vbo, nullptr, xc_ibo, 1, bufs,
mat.textureIdxs.size(), texs.data()));
2016-03-31 02:44:43 +00:00
++pipelineIt;
}
return true;
});
}
2016-03-31 06:18:56 +00:00
void CBooModel::MakeTexuresFromMats(const MaterialSet& matSet,
2016-03-31 02:44:43 +00:00
std::vector<TCachedToken<CTexture>>& toksOut,
IObjectStore& store)
{
toksOut.reserve(matSet.head.textureIDs.size());
for (const DataSpec::UniqueID32& id : matSet.head.textureIDs)
toksOut.emplace_back(store.GetObj({SBIG('TXTR'), id.toUint32()}));
}
void CBooModel::RemapMaterialData(SShader& shader)
{
x4_matSet = &shader.m_matSet;
x1c_textures = &shader.x0_textures;
m_pipelines = &shader.m_shaders;
x40_24_texturesLoaded = false;
m_gfxToken.doDestroy();
2016-03-29 23:14:14 +00:00
}
2016-03-31 02:44:43 +00:00
bool CBooModel::TryLockTextures() const
2016-03-30 19:16:01 +00:00
{
2016-03-31 02:44:43 +00:00
if (!x40_24_texturesLoaded)
{
bool allLoad = true;
for (TCachedToken<CTexture>& tex : *x1c_textures)
{
tex.Lock();
if (!tex.IsLoaded())
allLoad = false;
}
if (allLoad)
((CBooModel*)this)->BuildGfxToken();
((CBooModel*)this)->x40_24_texturesLoaded = allLoad;
}
return x40_24_texturesLoaded;
2016-03-30 19:16:01 +00:00
}
void CBooModel::UnlockTextures() const
{
2016-03-31 02:44:43 +00:00
for (TCachedToken<CTexture>& tex : *x1c_textures)
tex.Unlock();
((CBooModel*)this)->x40_24_texturesLoaded = false;
2016-03-30 19:16:01 +00:00
}
void CBooModel::DrawAlphaSurfaces(const CModelFlags& flags) const
{
2016-03-31 02:44:43 +00:00
if (TryLockTextures())
{
const CBooSurface* surf = x3c_firstSortedSurface;
while (surf)
{
DrawSurface(*surf, flags);
surf = surf->m_next;
}
}
2016-03-30 19:16:01 +00:00
}
void CBooModel::DrawNormalSurfaces(const CModelFlags& flags) const
{
2016-03-31 02:44:43 +00:00
if (TryLockTextures())
{
const CBooSurface* surf = x38_firstUnsortedSurface;
while (surf)
{
DrawSurface(*surf, flags);
surf = surf->m_next;
}
}
2016-03-30 19:16:01 +00:00
}
void CBooModel::DrawSurfaces(const CModelFlags& flags) const
{
2016-03-31 06:18:56 +00:00
if (!(flags.m_flags & 0x4))
2016-03-31 02:44:43 +00:00
if (!TryLockTextures())
return;
const CBooSurface* surf = x38_firstUnsortedSurface;
while (surf)
{
DrawSurface(*surf, flags);
surf = surf->m_next;
}
surf = x3c_firstSortedSurface;
while (surf)
{
DrawSurface(*surf, flags);
surf = surf->m_next;
}
2016-03-30 19:16:01 +00:00
}
void CBooModel::DrawSurface(const CBooSurface& surf, const CModelFlags& flags) const
{
2016-03-31 06:18:56 +00:00
const MaterialSet::Material& data = GetMaterialByIndex(surf.m_data.matIdx);
2016-03-31 02:44:43 +00:00
if (data.flags.shadowOccluderMesh() && !g_DrawingOccluders)
return;
CGraphics::SetShaderDataBinding(m_shaderDataBindings[surf.m_data.matIdx]);
CGraphics::DrawArrayIndexed(surf.m_data.idxStart, surf.m_data.idxCount);
2016-03-30 19:16:01 +00:00
}
2016-03-31 06:18:56 +00:00
void CBooModel::UVAnimationBuffer::ProcessAnimation(const UVAnimation& anim)
{
m_buffer.emplace_back();
zeus::CMatrix4f& matrixOut = m_buffer.back();
switch (anim.mode)
{
case UVAnimation::Mode::MvInvNoTranslation:
{
matrixOut = CGraphics::g_ViewMatrix.inverse().multiplyIgnoreTranslation(
CGraphics::g_GXModelMatrix).toMatrix4f();
matrixOut.vec[3].zeroOut();
break;
}
case UVAnimation::Mode::MvInv:
{
matrixOut = (CGraphics::g_ViewMatrix.inverse() * CGraphics::g_GXModelMatrix).toMatrix4f();
break;
}
case UVAnimation::Mode::Scroll:
{
matrixOut.vec[3].x = CGraphics::GetSecondsMod900() * anim.vals[2] + anim.vals[0];
matrixOut.vec[3].y = CGraphics::GetSecondsMod900() * anim.vals[3] + anim.vals[1];
break;
}
case UVAnimation::Mode::Rotation:
{
float angle = CGraphics::GetSecondsMod900() * anim.vals[1] + anim.vals[0];
float acos = std::cos(angle);
float asin = std::sin(angle);
matrixOut.vec[0].x = acos;
matrixOut.vec[0].y = asin;
matrixOut.vec[1].x = -asin;
matrixOut.vec[1].y = acos;
matrixOut.vec[3].x = (1.0 - (acos - asin)) * 0.5;
matrixOut.vec[3].y = (1.0 - (asin + acos)) * 0.5;
break;
}
case UVAnimation::Mode::HStrip:
{
float value = anim.vals[2] * anim.vals[0] * (anim.vals[3] + CGraphics::GetSecondsMod900());
matrixOut.vec[3].x = anim.vals[1] * fmod(value, 1.0f) * anim.vals[2];
break;
}
case UVAnimation::Mode::VStrip:
{
float value = anim.vals[2] * anim.vals[0] * (anim.vals[3] + CGraphics::GetSecondsMod900());
matrixOut.vec[3].y = anim.vals[1] * fmod(value, 1.0f) * anim.vals[2];
break;
}
case UVAnimation::Mode::Model:
{
matrixOut.vec[0].x = 0.5f;
matrixOut.vec[1].y = 0.0f;
matrixOut.vec[2].y = 0.5f;
matrixOut.vec[3].x = CGraphics::g_GXModelMatrix.m_origin.x * 0.5f;
matrixOut.vec[3].y = CGraphics::g_GXModelMatrix.m_origin.y * 0.5f;
break;
}
case UVAnimation::Mode::WhoMustNotBeNamed:
{
zeus::CTransform texmtx = CGraphics::g_ViewMatrix.inverse() * CGraphics::g_GXModelMatrix;
texmtx.m_origin.zeroOut();
/* TODO: Finish */
matrixOut = texmtx.toMatrix4f();
break;
}
default: break;
}
}
void CBooModel::UVAnimationBuffer::PadOutBuffer()
{
size_t curEnd = 0;
if (m_ranges.size())
curEnd = m_ranges.back().first + m_ranges.back().second;
size_t bufRem = m_buffer.size() % 4;
if (bufRem)
for (int i=0 ; i<(4-bufRem) ; ++i)
m_buffer.emplace_back();
size_t newEnd = m_buffer.size() * 64;
m_ranges.emplace_back(curEnd, newEnd - curEnd);
}
void CBooModel::UVAnimationBuffer::Update(const MaterialSet* matSet)
{
m_buffer.clear();
m_ranges.clear();
size_t bufCount = 0;
size_t count = 0;
for (const MaterialSet::Material& mat : matSet->materials)
{
count += mat.uvAnims.size();
bufCount += mat.uvAnims.size();
bufCount = ROUND_UP_4(bufCount);
}
m_buffer.reserve(bufCount);
m_ranges.reserve(count);
for (const MaterialSet::Material& mat : matSet->materials)
{
for (const UVAnimation& anim : mat.uvAnims)
ProcessAnimation(anim);
PadOutBuffer();
}
}
void CBooModel::UpdateUniformData() const
{
SUnskinnedXf unskinnedXf;
unskinnedXf.mv = CGraphics::g_GXModelView.toMatrix4f();
unskinnedXf.mvinv = CGraphics::g_GXModelViewInvXpose.toMatrix4f();
unskinnedXf.proj = CGraphics::GetPerspectiveProjectionMatrix();
m_unskinnedXfBuffer->load(&unskinnedXf, sizeof(unskinnedXf));
((CBooModel*)this)->m_uvAnimBuffer.Update(x4_matSet);
m_uvMtxBuffer->load(m_uvAnimBuffer.m_buffer.data(),
m_uvAnimBuffer.m_buffer.size() * 64);
}
2016-03-30 19:16:01 +00:00
void CBooModel::DrawAlpha(const CModelFlags& flags) const
{
2016-03-31 06:18:56 +00:00
UpdateUniformData();
2016-03-31 02:44:43 +00:00
DrawAlphaSurfaces(flags);
2016-03-30 19:16:01 +00:00
}
void CBooModel::DrawNormal(const CModelFlags& flags) const
{
2016-03-31 06:18:56 +00:00
UpdateUniformData();
2016-03-31 02:44:43 +00:00
DrawNormalSurfaces(flags);
2016-03-30 19:16:01 +00:00
}
void CBooModel::Draw(const CModelFlags& flags) const
{
2016-03-31 06:18:56 +00:00
UpdateUniformData();
2016-03-31 02:44:43 +00:00
DrawSurfaces(flags);
2016-03-29 23:14:14 +00:00
}
static const u8* MemoryFromPartData(const u8*& dataCur, const s32*& secSizeCur)
{
const u8* ret;
if (*secSizeCur)
ret = dataCur;
else
ret = nullptr;
dataCur += hecl::SBig(*secSizeCur);
++secSizeCur;
return ret;
}
CModel::CModel(std::unique_ptr<u8[]>&& in, u32 dataLen, IObjectStore* store)
: x0_data(std::move(in)), x4_dataLen(dataLen)
{
u32 version = hecl::SBig(*reinterpret_cast<u32*>(x0_data.get() + 0x4));
u32 flags = hecl::SBig(*reinterpret_cast<u32*>(x0_data.get() + 0x8));
if (version != 16)
Log.report(logvisor::Fatal, "invalid CMDL for loading with boo");
u32 secCount = hecl::SBig(*reinterpret_cast<u32*>(x0_data.get() + 0x24));
u32 matSetCount = hecl::SBig(*reinterpret_cast<u32*>(x0_data.get() + 0x28));
x18_matSets.reserve(matSetCount);
const u8* dataCur = x0_data.get() + ROUND_UP_32(0x2c + secCount * 4);
const s32* secSizeCur = reinterpret_cast<const s32*>(x0_data.get() + 0x2c);
for (u32 i=0 ; i<matSetCount ; ++i)
{
2016-03-31 02:44:43 +00:00
u32 matSetSz = hecl::SBig(*secSizeCur);
2016-03-29 23:14:14 +00:00
const u8* sec = MemoryFromPartData(dataCur, secSizeCur);
x18_matSets.emplace_back();
2016-03-31 02:44:43 +00:00
CBooModel::SShader& shader = x18_matSets.back();
athena::io::MemoryReader r(sec, matSetSz);
shader.m_matSet.read(r);
CBooModel::MakeTexuresFromMats(shader.m_matSet, shader.x0_textures, *store);
2016-03-29 23:14:14 +00:00
}
2016-03-30 19:16:01 +00:00
hecl::HMDLMeta hmdlMeta;
{
2016-03-31 02:44:43 +00:00
u32 hmdlSz = hecl::SBig(*secSizeCur);
2016-03-30 19:16:01 +00:00
const u8* hmdlMetadata = MemoryFromPartData(dataCur, secSizeCur);
2016-03-31 02:44:43 +00:00
athena::io::MemoryReader r(hmdlMetadata, hmdlSz);
2016-03-30 19:16:01 +00:00
hmdlMeta.read(r);
}
2016-03-29 23:14:14 +00:00
const u8* vboData = MemoryFromPartData(dataCur, secSizeCur);
const u8* iboData = MemoryFromPartData(dataCur, secSizeCur);
const u8* surfInfo = MemoryFromPartData(dataCur, secSizeCur);
2016-03-30 19:16:01 +00:00
m_gfxToken = CGraphics::CommitResources([&](boo::IGraphicsDataFactory::Context& ctx) -> bool
{
m_vbo = ctx.newStaticBuffer(boo::BufferUse::Vertex, vboData, hmdlMeta.vertStride, hmdlMeta.vertCount);
m_ibo = ctx.newStaticBuffer(boo::BufferUse::Index, iboData, 4, hmdlMeta.indexCount);
2016-03-31 02:44:43 +00:00
m_vtxFmt = hecl::Runtime::HMDLData::NewVertexFormat(ctx, hmdlMeta, m_vbo, m_ibo);
for (CBooModel::SShader& matSet : x18_matSets)
{
matSet.m_shaders.reserve(matSet.m_matSet.materials.size());
2016-03-31 06:18:56 +00:00
for (const MaterialSet::Material& mat : matSet.m_matSet.materials)
2016-03-31 02:44:43 +00:00
{
hecl::Runtime::ShaderTag tag(mat.heclIr,
hmdlMeta.colorCount, hmdlMeta.uvCount, hmdlMeta.weightCount,
0, mat.uvAnims.size(), true, true, true);
matSet.m_shaders.push_back(CGraphics::g_ShaderCacheMgr->buildShader(tag, mat.heclIr, "CMDL", ctx));
}
}
2016-03-30 19:16:01 +00:00
return true;
});
2016-03-29 23:14:14 +00:00
u32 surfCount = hecl::SBig(*reinterpret_cast<const u32*>(surfInfo));
x8_surfaces.reserve(surfCount);
for (u32 i=0 ; i<surfCount ; ++i)
{
2016-03-31 02:44:43 +00:00
u32 surfSz = hecl::SBig(*secSizeCur);
2016-03-29 23:14:14 +00:00
const u8* sec = MemoryFromPartData(dataCur, secSizeCur);
x8_surfaces.emplace_back();
2016-03-31 02:44:43 +00:00
CBooSurface& surf = x8_surfaces.back();
athena::io::MemoryReader r(sec, surfSz);
surf.m_data.read(r);
2016-03-29 23:14:14 +00:00
}
const float* aabbPtr = reinterpret_cast<const float*>(x0_data.get() + 0x18);
zeus::CAABox aabb(hecl::SBig(aabbPtr[0]), hecl::SBig(aabbPtr[1]), hecl::SBig(aabbPtr[2]),
hecl::SBig(aabbPtr[3]), hecl::SBig(aabbPtr[4]), hecl::SBig(aabbPtr[5]));
2016-03-31 02:44:43 +00:00
x28_modelInst = std::make_unique<CBooModel>(&x8_surfaces, x18_matSets[0],
m_vtxFmt, m_vbo, m_ibo,
2016-03-29 23:14:14 +00:00
aabb, flags & 0x2, true);
}
2016-02-13 00:57:09 +00:00
2016-03-31 02:44:43 +00:00
void CBooModel::SShader::UnlockTextures()
{
for (TCachedToken<CTexture>& tex : x0_textures)
tex.Unlock();
}
void CModel::VerifyCurrentShader(int shaderIdx) const
{
int idx = 0;
for (const CBooModel::SShader& shader : x18_matSets)
if (idx++ != shaderIdx)
((CBooModel::SShader&)shader).UnlockTextures();
}
void CModel::DrawSortedParts(const CModelFlags& flags) const
{
2016-03-31 06:18:56 +00:00
VerifyCurrentShader(flags.m_matSetIdx);
x28_modelInst->DrawAlpha(flags);
2016-03-31 02:44:43 +00:00
}
void CModel::DrawUnsortedParts(const CModelFlags& flags) const
{
2016-03-31 06:18:56 +00:00
VerifyCurrentShader(flags.m_matSetIdx);
x28_modelInst->DrawNormal(flags);
2016-03-31 02:44:43 +00:00
}
2016-02-13 00:57:09 +00:00
void CModel::Draw(const CModelFlags& flags) const
{
2016-03-31 06:18:56 +00:00
VerifyCurrentShader(flags.m_matSetIdx);
x28_modelInst->Draw(flags);
2016-02-13 00:57:09 +00:00
}
2016-03-31 02:44:43 +00:00
void CModel::Touch(int shaderIdx) const
2016-03-17 02:18:01 +00:00
{
2016-03-31 06:18:56 +00:00
VerifyCurrentShader(shaderIdx);
x28_modelInst->TryLockTextures();
2016-03-17 02:18:01 +00:00
}
2016-03-31 02:44:43 +00:00
bool CModel::IsLoaded(int shaderIdx) const
2016-03-17 02:18:01 +00:00
{
2016-03-31 02:44:43 +00:00
VerifyCurrentShader(shaderIdx);
2016-03-31 06:18:56 +00:00
std::vector<TCachedToken<CTexture>>* texs = x28_modelInst->x1c_textures;
bool loaded = true;
for (TCachedToken<CTexture>& tex : *texs)
{
if (!tex.IsLoaded())
{
loaded = false;
break;
}
}
return loaded;
2016-03-17 02:18:01 +00:00
}
2016-03-29 23:14:14 +00:00
CFactoryFnReturn FModelFactory(const urde::SObjectTag& tag,
std::unique_ptr<u8[]>&& in, u32 len,
const urde::CVParamTransfer& vparms)
{
IObjectStore* store = static_cast<TObjOwnerParam<IObjectStore*>*>(vparms.GetObj())->GetParam();
return TToken<CModel>::GetIObjObjectFor(std::make_unique<CModel>(std::move(in), len, store));
}
2016-02-13 00:57:09 +00:00
}