#include "Graphics/CModel.hpp" #include "Graphics/CTexture.hpp" #include "Graphics/CGraphics.hpp" #include "Graphics/CLight.hpp" #include "hecl/HMDLMeta.hpp" #include "hecl/Runtime.hpp" #include "boo/graphicsdev/Metal.hpp" #include "Shaders/CModelShaders.hpp" #include "Graphics/CBooRenderer.hpp" #include "Character/CSkinRules.hpp" #include "GameGlobalObjects.hpp" #include "CSimplePool.hpp" #include namespace urde { static logvisor::Module Log("urde::CBooModel"); bool CBooModel::g_DrawingOccluders = false; static CBooModel* g_FirstModel = nullptr; void CBooModel::ClearModelUniformCounters() { for (CBooModel* model = g_FirstModel ; model ; model = model->m_next) model->ClearUniformCounter(); } zeus::CVector3f CBooModel::g_PlayerPosition = {}; float CBooModel::g_ModSeconds = 0.f; float CBooModel::g_TransformedTime = 0.f; float CBooModel::g_TransformedTime2 = 0.f; void CBooModel::SetNewPlayerPositionAndTime(const zeus::CVector3f& pos) { g_PlayerPosition = pos; KillCachedViewDepState(); u32 modMillis = std::chrono::duration_cast( std::chrono::steady_clock::now().time_since_epoch()).count() % u64(100000.f * 4.f * M_PIF / 3.f); g_ModSeconds = modMillis / 1000.f; g_TransformedTime = 1.f / -(0.05f * std::sin(g_ModSeconds * 1.5f) - 1.f); g_TransformedTime2 = 1.f / -(0.015f * std::sin(g_ModSeconds * 1.5f + 1.f) - 1.f); } CBooModel* CBooModel::g_LastModelCached = nullptr; void CBooModel::KillCachedViewDepState() { g_LastModelCached = nullptr; } bool CBooModel::g_DummyTextures = false; zeus::CVector3f CBooModel::g_ReflectViewPos = {}; static const zeus::CMatrix4f ReflectBaseMtx = { 0.f, 0.f, 0.f, 0.f, 0.f, 0.f, 0.f, 0.f, 0.f, 0.f, 0.f, 1.f, 0.f, 0.f, 0.f, 1.f }; void CBooModel::EnsureViewDepStateCached(const CBooModel& model, const CBooSurface* surf, zeus::CMatrix4f* mtxsOut, float& alphaOut) { zeus::CVector3f modelToPlayer = g_PlayerPosition - CGraphics::g_GXModelMatrix.origin; zeus::CVector3f modelToPlayerLocal = CGraphics::g_GXModelMatrix.transposeRotate(modelToPlayer); zeus::CVector3f surfPos; float surfSize = 0.f; if (surf) { zeus::CVector3f surfCenter(surf->m_data.centroid); zeus::CVector3f surfNormal(surf->m_data.reflectionNormal); float dotDelta = surfNormal.dot(modelToPlayerLocal) - surfCenter.dot(surfNormal); surfPos = modelToPlayerLocal - surfNormal * dotDelta; } else { surfPos = model.x20_aabb.center(); surfSize = (model.x20_aabb.max.x - model.x20_aabb.min.x) + (model.x20_aabb.max.y - model.x20_aabb.min.y) * 0.5f; } if (g_Renderer->x318_24_refectionDirty) { zeus::CVector3f playerToPos = g_ReflectViewPos - g_PlayerPosition; zeus::CVector3f vecToPos = surfPos - g_PlayerPosition; if (playerToPos.dot(playerToPos) < vecToPos.dot(vecToPos)) g_ReflectViewPos = surfPos; } else { g_ReflectViewPos = surfPos; g_Renderer->x318_24_refectionDirty = true; } zeus::CVector3f playerToSurf = surfPos - modelToPlayerLocal; float distance = std::max(-(0.5f * surfSize - playerToSurf.magnitude()), FLT_EPSILON); if (distance >= 5.f) { alphaOut = 0.f; } else { alphaOut = (5.f - distance) / 5.f; /* Indirect map matrix */ mtxsOut[0] = (CGraphics::g_ViewMatrix.inverse() * CGraphics::g_GXModelMatrix).toMatrix4f(); /* Reflection map matrix */ zeus::CVector3f v1 = playerToSurf * (1.f / surfSize); zeus::CVector3f v2 = v1.cross(zeus::CVector3f::skUp); if (v2.canBeNormalized()) v2.normalize(); else v2 = zeus::CVector3f::skRight; float timeScale = 0.32258067f * (0.02f * distance + 1.f); float f1 = timeScale * g_TransformedTime; float f2 = timeScale * g_TransformedTime2; mtxsOut[1] = ReflectBaseMtx; mtxsOut[1][0][0] = f1 * v2.x; mtxsOut[1][1][0] = f1 * v2.y; mtxsOut[1][3][0] = -surfPos.dot(v2) * f1 + 0.5f; mtxsOut[1][2][1] = f2; mtxsOut[1][3][1] = -modelToPlayerLocal.z * f2; } } boo::ObjToken CBooModel::g_shadowMap; zeus::CTransform CBooModel::g_shadowTexXf; void CBooModel::EnableShadowMaps(const boo::ObjToken& map, const zeus::CTransform& texXf) { g_shadowMap = map; g_shadowTexXf = texXf; } void CBooModel::DisableShadowMaps() { g_shadowMap = nullptr; } CBooModel::~CBooModel() { if (m_prev) m_prev->m_next = m_next; if (m_next) m_next->m_prev = m_prev; if (this == g_FirstModel) g_FirstModel = m_next; } CBooModel::CBooModel(TToken& token, CModel* parent, std::vector* surfaces, SShader& shader, const boo::ObjToken& vtxFmt, const boo::ObjToken& vbo, const boo::ObjToken& ibo, const zeus::CAABox& aabb, u8 renderMask, int numInsts, const boo::ObjToken txtrOverrides[8]) : m_modelTok(token), m_model(parent), x0_surfaces(surfaces), x4_matSet(&shader.m_matSet), m_geomLayout(&*shader.m_geomLayout), m_matSetIdx(shader.m_matSetIdx), m_pipelines(&shader.m_shaders), x1c_textures(shader.x0_textures), x20_aabb(aabb), x40_24_texturesLoaded(false), x40_25_modelVisible(0), x41_mask(renderMask), m_staticVtxFmt(vtxFmt), m_staticVbo(vbo), m_staticIbo(ibo) { if (txtrOverrides) for (int i=0 ; i<8 ; ++i) m_txtrOverrides[i] = txtrOverrides[i]; if (!g_FirstModel) g_FirstModel = this; else { g_FirstModel->m_prev = this; m_next = g_FirstModel; g_FirstModel = this; } for (CBooSurface& surf : *x0_surfaces) surf.m_parent = this; for (auto it=x0_surfaces->rbegin() ; it != x0_surfaces->rend() ; ++it) { u32 matId = it->m_data.matIdx; const MaterialSet::Material& matData = GetMaterialByIndex(matId); if (matData.flags.depthSorting() || matData.heclIr.m_doAlpha) { it->m_next = x3c_firstSortedSurface; x3c_firstSortedSurface = &*it; } else { it->m_next = x38_firstUnsortedSurface; x38_firstUnsortedSurface = &*it; } } m_instances.reserve(numInsts); for (int i=0 ; i CBooModel::ModelInstance::GetBooVBO(const CBooModel& model, boo::IGraphicsDataFactory::Context& ctx) { if (model.m_staticVbo) return model.m_staticVbo.get(); if (!m_dynamicVbo && model.m_model) { const CModel& parent = *model.m_model; m_dynamicVbo = ctx.newDynamicBuffer(boo::BufferUse::Vertex, parent.m_hmdlMeta.vertStride, parent.m_hmdlMeta.vertCount); m_dynamicVbo->load(parent.m_dynamicVertexData.get(), parent.m_hmdlMeta.vertStride * parent.m_hmdlMeta.vertCount); } return m_dynamicVbo.get(); } boo::ObjToken CBooModel::ModelInstance::GetBooVtxFmt(const CBooModel& model, boo::IGraphicsDataFactory::Context& ctx) { if (model.m_staticVtxFmt) return model.m_staticVtxFmt; if (!m_dynamicVtxFmt && model.m_model) { const CModel& parent = *model.m_model; m_dynamicVtxFmt = hecl::Runtime::HMDLData::NewVertexFormat(ctx, parent.m_hmdlMeta, GetBooVBO(model, ctx), parent.m_ibo.get()); } return m_dynamicVtxFmt; } GeometryUniformLayout::GeometryUniformLayout(const CModel* model, const MaterialSet* matSet) { if (model) { m_skinBankCount = model->m_hmdlMeta.bankCount; m_weightVecCount = model->m_hmdlMeta.weightCount; } m_skinOffs.reserve(std::max(size_t(1), m_skinBankCount)); m_skinSizes.reserve(std::max(size_t(1), m_skinBankCount)); m_uvOffs.reserve(matSet->materials.size()); m_uvSizes.reserve(matSet->materials.size()); if (m_skinBankCount) { /* Skinned */ for (size_t i=0 ; imaterials) { size_t thisSz = ROUND_UP_256(/*mat.uvAnims.size()*/ 8 * (sizeof(zeus::CMatrix4f) * 2)); m_uvOffs.push_back(m_geomBufferSize); m_uvSizes.push_back(thisSz); m_geomBufferSize += thisSz; } } CBooModel::ModelInstance* CBooModel::PushNewModelInstance() { if (!x40_24_texturesLoaded && !g_DummyTextures) return nullptr; if (m_instances.size() >= 256) Log.report(logvisor::Fatal, "Model buffer overflow"); m_instances.emplace_back(); ModelInstance& newInst = m_instances.back(); CGraphics::CommitResources([&](boo::IGraphicsDataFactory::Context& ctx) -> bool { /* Build geometry uniform buffer if shared not available */ boo::ObjToken geomUniformBuf; if (m_geomLayout->m_sharedBuffer) geomUniformBuf = m_geomLayout->m_sharedBuffer; else { geomUniformBuf = ctx.newDynamicBuffer(boo::BufferUse::Uniform, m_geomLayout->m_geomBufferSize, 1); newInst.m_geomUniformBuffer = geomUniformBuf; } /* Lighting and reflection uniforms */ size_t uniBufSize = 0; /* Lighting uniform */ size_t lightOff = 0; size_t lightSz = 0; { size_t thisSz = ROUND_UP_256(sizeof(CModelShaders::LightingUniform)); lightOff = uniBufSize; lightSz = thisSz; uniBufSize += thisSz; } /* Surface reflection texmatrix uniform with first identity slot */ size_t reflectOff = uniBufSize; uniBufSize += 256; for (const CBooSurface& surf : *x0_surfaces) { const MaterialSet::Material& mat = x4_matSet->materials.at(surf.m_data.matIdx); if (mat.flags.samusReflection() || mat.flags.samusReflectionSurfaceEye()) uniBufSize += 256; } /* Allocate resident buffer */ m_uniformDataSize = uniBufSize; newInst.m_uniformBuffer = ctx.newDynamicBuffer(boo::BufferUse::Uniform, uniBufSize, 1); boo::ObjToken bufs[] = {geomUniformBuf.get(), geomUniformBuf.get(), newInst.m_uniformBuffer.get(), newInst.m_uniformBuffer.get()}; /* Binding for each surface */ newInst.m_shaderDataBindings.reserve(x0_surfaces->size()); boo::ObjToken mbShadowTexs[8] = {g_Renderer->m_ballShadowId.get(), g_Renderer->x220_sphereRamp.get(), g_Renderer->m_ballFade.get(), g_Renderer->x220_sphereRamp.get(), g_Renderer->x220_sphereRamp.get(), g_Renderer->x220_sphereRamp.get(), g_Renderer->x220_sphereRamp.get(), g_Renderer->x220_sphereRamp.get()}; size_t thisOffs[4]; size_t thisSizes[4]; static const boo::PipelineStage stages[4] = {boo::PipelineStage::Vertex, boo::PipelineStage::Vertex, boo::PipelineStage::Fragment, boo::PipelineStage::Vertex}; /* Enumerate surfaces and build data bindings */ size_t curReflect = reflectOff + 256; for (const CBooSurface& surf : *x0_surfaces) { const MaterialSet::Material& mat = x4_matSet->materials.at(surf.m_data.matIdx); boo::ObjToken texs[8] = {g_Renderer->x220_sphereRamp.get(), g_Renderer->x220_sphereRamp.get(), g_Renderer->x220_sphereRamp.get(), g_Renderer->x220_sphereRamp.get(), g_Renderer->x220_sphereRamp.get(), g_Renderer->x220_sphereRamp.get(), g_Renderer->x220_sphereRamp.get(), g_Renderer->x220_sphereRamp.get()}; u32 texCount = 0; for (atUint32 idx : mat.textureIdxs) { if (boo::ObjToken overtex = m_txtrOverrides[texCount]) { texs[texCount++] = overtex; } else if (g_DummyTextures) { texs[texCount++] = g_Renderer->x220_sphereRamp.get(); } else { TCachedToken& tex = x1c_textures[idx]; if (boo::ObjToken btex = tex.GetObj()->GetBooTexture()) texs[texCount++] = btex; } } if (m_geomLayout->m_skinBankCount) { thisOffs[0] = m_geomLayout->m_skinOffs[surf.m_data.skinMtxBankIdx]; thisSizes[0] = m_geomLayout->m_skinSizes[surf.m_data.skinMtxBankIdx]; } else { thisOffs[0] = 0; thisSizes[0] = 256; } thisOffs[1] = m_geomLayout->m_uvOffs[surf.m_data.matIdx]; thisSizes[1] = m_geomLayout->m_uvSizes[surf.m_data.matIdx]; thisOffs[2] = lightOff; thisSizes[2] = lightSz; bool useReflection = mat.flags.samusReflection() || mat.flags.samusReflectionSurfaceEye(); if (useReflection) { if (g_Renderer->x14c_reflectionTex) texs[texCount] = g_Renderer->x14c_reflectionTex.get(); else texs[texCount] = g_Renderer->x220_sphereRamp.get(); thisOffs[3] = curReflect; curReflect += 256; } else { thisOffs[3] = reflectOff; } thisSizes[3] = 256; const std::shared_ptr& pipelines = m_pipelines->at(surf.m_data.matIdx); newInst.m_shaderDataBindings.emplace_back(); std::vector>& extendeds = newInst.m_shaderDataBindings.back(); extendeds.reserve(pipelines->m_pipelines.size()); int idx = 0; for (const boo::ObjToken& pipeline : pipelines->m_pipelines) { boo::ObjToken* ltexs; if (idx == EExtendedShader::Thermal) { texs[7] = g_Renderer->x220_sphereRamp.get(); ltexs = texs; } else if (idx == EExtendedShader::MorphBallShadow) { ltexs = mbShadowTexs; } else if (idx == EExtendedShader::WorldShadow) { if (g_shadowMap) texs[7] = g_shadowMap; else texs[7] = g_Renderer->x220_sphereRamp.get(); ltexs = texs; } else if (useReflection) { ltexs = texs; } else { ltexs = texs; } extendeds.push_back( ctx.newShaderDataBinding(pipeline, newInst.GetBooVtxFmt(*this, ctx), newInst.GetBooVBO(*this, ctx), nullptr, m_staticIbo.get(), 4, bufs, stages, thisOffs, thisSizes, 8, ltexs, nullptr, nullptr)); ++idx; } } return true; }); return &newInst; } void CBooModel::MakeTexturesFromMats(const MaterialSet& matSet, std::vector>& 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::MakeTexturesFromMats(std::vector>& toksOut, IObjectStore& store) { MakeTexturesFromMats(*x4_matSet, toksOut, store); } void CBooModel::ActivateLights(const std::vector& lights) { m_lightingData.ActivateLights(lights); } void CBooModel::DisableAllLights() { m_lightingData.ambient = zeus::CColor::skBlack; for (size_t curLight = 0 ; curLight>& pipelines) { x4_matSet = &shader.m_matSet; m_geomLayout = &*shader.m_geomLayout; m_matSetIdx = shader.m_matSetIdx; x1c_textures = shader.x0_textures; m_pipelines = &pipelines; x40_24_texturesLoaded = false; m_instances.clear(); } bool CBooModel::TryLockTextures() const { if (!x40_24_texturesLoaded) { bool allLoad = true; for (TCachedToken& tex : const_cast>&>(x1c_textures)) { tex.Lock(); if (!tex.IsLoaded()) allLoad = false; } const_cast(this)->x40_24_texturesLoaded = allLoad; } return x40_24_texturesLoaded; } void CBooModel::UnlockTextures() const { const_cast(this)->m_instances.clear(); for (TCachedToken& tex : const_cast>&>(x1c_textures)) tex.Unlock(); const_cast(this)->x40_24_texturesLoaded = false; } void CBooModel::SyncLoadTextures() const { if (!x40_24_texturesLoaded) { for (TCachedToken& tex : const_cast>&>(x1c_textures)) tex.GetObj(); const_cast(this)->x40_24_texturesLoaded = true; } } void CBooModel::DrawFlat(ESurfaceSelection sel, EExtendedShader extendedIdx) const { const CBooSurface* surf; CModelFlags flags = {}; flags.m_extendedShader = extendedIdx; if (sel != ESurfaceSelection::SortedOnly) { surf = x38_firstUnsortedSurface; while (surf) { DrawSurface(*surf, flags); surf = surf->m_next; } } if (sel != ESurfaceSelection::UnsortedOnly) { surf = x3c_firstSortedSurface; while (surf) { DrawSurface(*surf, flags); surf = surf->m_next; } } } void CBooModel::DrawAlphaSurfaces(const CModelFlags& flags) const { const CBooSurface* surf = x3c_firstSortedSurface; while (surf) { DrawSurface(*surf, flags); surf = surf->m_next; } } void CBooModel::DrawNormalSurfaces(const CModelFlags& flags) const { const CBooSurface* surf = x38_firstUnsortedSurface; while (surf) { DrawSurface(*surf, flags); surf = surf->m_next; } } void CBooModel::DrawSurfaces(const CModelFlags& flags) const { 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; } } void CBooModel::DrawSurface(const CBooSurface& surf, const CModelFlags& flags) const { //if (m_uniUpdateCount == 0) // Log.report(logvisor::Fatal, "UpdateUniformData() not called"); if (m_uniUpdateCount == 0 || m_uniUpdateCount > m_instances.size()) return; const ModelInstance& inst = m_instances[m_uniUpdateCount-1]; const MaterialSet::Material& data = GetMaterialByIndex(surf.m_data.matIdx); if (data.flags.shadowOccluderMesh() && !g_DrawingOccluders) return; const std::vector>& extendeds = inst.m_shaderDataBindings[surf.selfIdx]; EExtendedShader extended = EExtendedShader::Flat; if (flags.m_extendedShader == EExtendedShader::Lighting) { if (data.heclIr.m_blendSrc == boo::BlendFactor::One && data.heclIr.m_blendDst == boo::BlendFactor::Zero) { /* Override shader if originally opaque */ if (flags.x0_blendMode > 6) extended = flags.m_noCull ? EExtendedShader::ForcedAdditiveNoCull : EExtendedShader::ForcedAdditive; else if (flags.x0_blendMode > 4) extended = flags.m_noCull ? EExtendedShader::ForcedAlphaNoCull : EExtendedShader::ForcedAlpha; else extended = EExtendedShader::Lighting; } else if (flags.m_noCull) { /* Substitute no-cull pipeline if available */ if (data.heclIr.m_blendDst == boo::BlendFactor::InvSrcAlpha) extended = EExtendedShader::ForcedAlphaNoCull; else if (data.heclIr.m_blendDst == boo::BlendFactor::One) extended = EExtendedShader::ForcedAdditiveNoCull; else extended = EExtendedShader::Lighting; } else { extended = EExtendedShader::Lighting; } } else if (flags.m_extendedShader < extendeds.size()) { extended = flags.m_extendedShader; } boo::ObjToken binding = extendeds[extended]; CGraphics::SetShaderDataBinding(binding); CGraphics::DrawArrayIndexed(surf.m_data.idxStart, surf.m_data.idxCount); } void CBooModel::WarmupDrawSurfaces() const { const CBooSurface* surf = x38_firstUnsortedSurface; while (surf) { WarmupDrawSurface(*surf); surf = surf->m_next; } surf = x3c_firstSortedSurface; while (surf) { WarmupDrawSurface(*surf); surf = surf->m_next; } } void CBooModel::WarmupDrawSurface(const CBooSurface& surf) const { if (m_uniUpdateCount > m_instances.size()) return; const ModelInstance& inst = m_instances[m_uniUpdateCount-1]; // Only warmup normal lighting and thermal visor for (int i=1 ; i<=2 ; ++i) { auto& binding = inst.m_shaderDataBindings[surf.selfIdx][i]; CGraphics::SetShaderDataBinding(binding); CGraphics::DrawArrayIndexed(surf.m_data.idxStart, std::min(u32(3), surf.m_data.idxCount)); } } void CBooModel::UVAnimationBuffer::ProcessAnimation(u8*& bufOut, const UVAnimation& anim) { zeus::CMatrix4f& texMtxOut = reinterpret_cast(*bufOut); zeus::CMatrix4f& postMtxOut = reinterpret_cast(*(bufOut + sizeof(zeus::CMatrix4f))); texMtxOut = zeus::CMatrix4f(); postMtxOut = zeus::CMatrix4f(); switch (anim.mode) { case UVAnimation::Mode::MvInvNoTranslation: { texMtxOut = CGraphics::g_GXModelViewInvXpose.toMatrix4f(); texMtxOut.vec[3].zeroOut(); texMtxOut.vec[3].w = 1.f; postMtxOut.vec[0].x = 0.5f; postMtxOut.vec[1].y = 0.5f; postMtxOut.vec[3].x = 0.5f; postMtxOut.vec[3].y = 0.5f; break; } case UVAnimation::Mode::MvInv: { texMtxOut = CGraphics::g_GXModelViewInvXpose.toMatrix4f(); postMtxOut.vec[0].x = 0.5f; postMtxOut.vec[1].y = 0.5f; postMtxOut.vec[3].x = 0.5f; postMtxOut.vec[3].y = 0.5f; break; } case UVAnimation::Mode::Scroll: { texMtxOut.vec[3].x = CGraphics::GetSecondsMod900() * anim.vals[2] + anim.vals[0]; texMtxOut.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); texMtxOut.vec[0].x = acos; texMtxOut.vec[0].y = asin; texMtxOut.vec[1].x = -asin; texMtxOut.vec[1].y = acos; texMtxOut.vec[3].x = (1.0f - (acos - asin)) * 0.5f; texMtxOut.vec[3].y = (1.0f - (asin + acos)) * 0.5f; break; } case UVAnimation::Mode::HStrip: { float value = anim.vals[0] * anim.vals[2] * (anim.vals[3] + CGraphics::GetSecondsMod900()); texMtxOut.vec[3].x = (float)(short)(float)(anim.vals[1] * fmod(value, 1.0f)) * anim.vals[2]; break; } case UVAnimation::Mode::VStrip: { float value = anim.vals[0] * anim.vals[2] * (anim.vals[3] + CGraphics::GetSecondsMod900()); texMtxOut.vec[3].y = (float)(short)(float)(anim.vals[1] * fmod(value, 1.0f)) * anim.vals[2]; break; } case UVAnimation::Mode::Model: { texMtxOut = CGraphics::g_GXModelMatrix.toMatrix4f(); texMtxOut.vec[3].zeroOut(); postMtxOut.vec[0].x = 0.5f; postMtxOut.vec[2].y = 0.5f; postMtxOut.vec[3].x = CGraphics::g_GXModelMatrix.origin.x * 0.5f; postMtxOut.vec[3].y = CGraphics::g_GXModelMatrix.origin.y * 0.5f; break; } case UVAnimation::Mode::CylinderEnvironment: { //texMtxOut = (CGraphics::g_ViewMatrix.inverse() * CGraphics::g_GXModelMatrix).toMatrix4f(); texMtxOut = CGraphics::g_GXModelView.toMatrix4f(); texMtxOut.vec[3].zeroOut(); const zeus::CVector3f& viewOrigin = CGraphics::g_ViewMatrix.origin; float xy = (viewOrigin.x + viewOrigin.y) * 0.025f * anim.vals[1]; xy = (xy - (int)xy); float z = (viewOrigin.z) * 0.05f * anim.vals[1]; z = (z - (int)z); float halfA = anim.vals[0] * 0.5f; postMtxOut = zeus::CTransform(zeus::CMatrix3f(halfA, 0.0, 0.0, 0.0, 0.0, halfA, 0.0, 0.0, 0.0), zeus::CVector3f(xy, z, 1.0)).toMatrix4f(); break; } default: break; } bufOut += sizeof(zeus::CMatrix4f) * 2; } void CBooModel::UVAnimationBuffer::PadOutBuffer(u8*& bufStart, u8*& bufOut) { bufOut = bufStart + ROUND_UP_256(bufOut - bufStart); } static const zeus::CMatrix4f MBShadowPost0(1.f, 0.f, 0.f, 0.f, 0.f, -1.f, 0.f, 1.f, 0.f, 0.f, 0.f, 1.f, 0.f, 0.f, 0.f, 1.f); static const zeus::CMatrix4f MBShadowPost1(0.f, 0.f, 0.f, 1.f, 0.f, 0.f, 1.f, -0.0625f, 0.f, 0.f, 0.f, 1.f, 0.f, 0.f, 0.f, 1.f); void CBooModel::UVAnimationBuffer::Update(u8*& bufOut, const MaterialSet* matSet, const CModelFlags& flags) { u8* start = bufOut; /* Special matrices for MorphBall shadow rendering */ if (flags.m_extendedShader == EExtendedShader::MorphBallShadow) { zeus::CMatrix4f texMtx = (zeus::CTransform::Scale(1.f / (flags.mbShadowBox.max.x - flags.mbShadowBox.min.x), 1.f / (flags.mbShadowBox.max.y - flags.mbShadowBox.min.y), 1.f / (flags.mbShadowBox.max.z - flags.mbShadowBox.min.z)) * zeus::CTransform::Translate(-flags.mbShadowBox.min.x, -flags.mbShadowBox.min.y, -flags.mbShadowBox.min.z) * CGraphics::g_GXModelView).toMatrix4f(); for (const MaterialSet::Material& mat : matSet->materials) { std::array* mtxs = reinterpret_cast*>(bufOut); mtxs[0][0] = texMtx; mtxs[0][1] = MBShadowPost0; mtxs[1][0] = texMtx; mtxs[1][1] = MBShadowPost1; bufOut += sizeof(zeus::CMatrix4f) * 2 * 8; PadOutBuffer(start, bufOut); } return; } std::experimental::optional> specialMtxOut; if (flags.m_extendedShader == EExtendedShader::Thermal) { /* Special Mode0 matrix for exclusive Thermal Visor use */ specialMtxOut.emplace(); zeus::CMatrix4f& texMtxOut = (*specialMtxOut)[0]; texMtxOut = CGraphics::g_GXModelViewInvXpose.toMatrix4f(); texMtxOut.vec[3].zeroOut(); texMtxOut.vec[3].w = 1.f; zeus::CMatrix4f& postMtxOut = (*specialMtxOut)[1]; postMtxOut.vec[0].x = 0.5f; postMtxOut.vec[1].y = 0.5f; postMtxOut.vec[3].x = 0.5f; postMtxOut.vec[3].y = 0.5f; } else if (flags.m_extendedShader == EExtendedShader::WorldShadow) { /* Special matrix for mapping world shadow */ specialMtxOut.emplace(); zeus::CMatrix4f mat = g_shadowTexXf.toMatrix4f(); zeus::CMatrix4f& texMtxOut = (*specialMtxOut)[0]; texMtxOut[0][0] = mat[0][0]; texMtxOut[1][0] = mat[1][0]; texMtxOut[2][0] = mat[2][0]; texMtxOut[3][0] = mat[3][0]; texMtxOut[0][1] = mat[0][2]; texMtxOut[1][1] = mat[1][2]; texMtxOut[2][1] = mat[2][2]; texMtxOut[3][1] = mat[3][2]; } for (const MaterialSet::Material& mat : matSet->materials) { if (specialMtxOut) { std::array* mtxs = reinterpret_cast*>(bufOut); mtxs[7][0] = (*specialMtxOut)[0]; mtxs[7][1] = (*specialMtxOut)[1]; } u8* bufOrig = bufOut; for (const UVAnimation& anim : mat.uvAnims) ProcessAnimation(bufOut, anim); bufOut = bufOrig + sizeof(zeus::CMatrix4f) * 2 * 8; PadOutBuffer(start, bufOut); } } void GeometryUniformLayout::Update(const CModelFlags& flags, const CSkinRules* cskr, const CPoseAsTransforms* pose, const MaterialSet* matSet, const boo::ObjToken& buf) const { u8* dataOut = reinterpret_cast(buf->map(m_geomBufferSize)); u8* dataCur = dataOut; if (m_skinBankCount) { /* Skinned */ std::vector bankTransforms; size_t weightCount = m_weightVecCount * 4; bankTransforms.reserve(weightCount); for (size_t i=0 ; iGetBankTransforms(bankTransforms, *pose, i); for (size_t w=0 ; w(*dataCur); if (w >= bankTransforms.size()) mv = CGraphics::g_GXModelView.toMatrix4f(); else mv = (CGraphics::g_GXModelView * *bankTransforms[w]).toMatrix4f(); dataCur += sizeof(zeus::CMatrix4f); } for (size_t w=0 ; w(*dataCur); if (w >= bankTransforms.size()) mvinv = CGraphics::g_GXModelViewInvXpose.toMatrix4f(); else { zeus::CTransform xf = (CGraphics::g_GXModelView.basis * bankTransforms[w]->basis); xf.basis.invert(); xf.basis.transpose(); mvinv = xf.toMatrix4f(); } dataCur += sizeof(zeus::CMatrix4f); } bankTransforms.clear(); } else { for (size_t w=0 ; w(*dataCur); mv = CGraphics::g_GXModelView.toMatrix4f(); dataCur += sizeof(zeus::CMatrix4f); } for (size_t w=0 ; w(*dataCur); mvinv = CGraphics::g_GXModelViewInvXpose.toMatrix4f(); dataCur += sizeof(zeus::CMatrix4f); } } zeus::CMatrix4f& proj = reinterpret_cast(*dataCur); proj = CGraphics::GetPerspectiveProjectionMatrix(true); dataCur += sizeof(zeus::CMatrix4f); dataCur = dataOut + ROUND_UP_256(dataCur - dataOut); } } else { /* Non-Skinned */ zeus::CMatrix4f& mv = reinterpret_cast(*dataCur); mv = CGraphics::g_GXModelView.toMatrix4f(); dataCur += sizeof(zeus::CMatrix4f); zeus::CMatrix4f& mvinv = reinterpret_cast(*dataCur); mvinv = CGraphics::g_GXModelViewInvXpose.toMatrix4f(); dataCur += sizeof(zeus::CMatrix4f); zeus::CMatrix4f& proj = reinterpret_cast(*dataCur); proj = CGraphics::GetPerspectiveProjectionMatrix(true); dataCur += sizeof(zeus::CMatrix4f); dataCur = dataOut + ROUND_UP_256(dataCur - dataOut); } CBooModel::UVAnimationBuffer::Update(dataCur, matSet, flags); buf->unmap(); } boo::ObjToken CBooModel::UpdateUniformData(const CModelFlags& flags, const CSkinRules* cskr, const CPoseAsTransforms* pose) const { /* Invalidate instances if new shadow being drawn */ if (flags.m_extendedShader == EExtendedShader::WorldShadow && m_lastDrawnShadowMap != g_shadowMap) { const_cast(this)->m_lastDrawnShadowMap = g_shadowMap; const_cast(this)->m_instances.clear(); } const ModelInstance* inst; if (m_instances.size() <= m_uniUpdateCount) { inst = const_cast(this)->PushNewModelInstance(); if (!inst) return nullptr; } else inst = &m_instances[m_uniUpdateCount]; ++const_cast(this)->m_uniUpdateCount; if (inst->m_geomUniformBuffer) m_geomLayout->Update(flags, cskr, pose, x4_matSet, inst->m_geomUniformBuffer); u8* dataOut = reinterpret_cast(inst->m_uniformBuffer->map(m_uniformDataSize)); u8* dataCur = dataOut; if (flags.m_extendedShader == EExtendedShader::Thermal) /* Thermal Model (same as UV Mode 0) */ { CModelShaders::ThermalUniform& thermalOut = *reinterpret_cast(dataCur); thermalOut.mulColor = flags.x4_color; thermalOut.addColor = flags.addColor; } else if (flags.m_extendedShader >= EExtendedShader::SolidColor && flags.m_extendedShader <= EExtendedShader::SolidColorBackfaceCullGreaterAlphaOnly) /* Solid color render */ { CModelShaders::SolidUniform& solidOut = *reinterpret_cast(dataCur); solidOut.solidColor = flags.x4_color; } else if (flags.m_extendedShader == EExtendedShader::MorphBallShadow) /* MorphBall shadow render */ { CModelShaders::MBShadowUniform& shadowOut = *reinterpret_cast(dataCur); shadowOut.shadowUp = CGraphics::g_GXModelView * zeus::CVector3f::skUp; shadowOut.shadowUp.w = flags.x4_color.a; shadowOut.shadowId = flags.x4_color.r; } else { CModelShaders::LightingUniform& lightingOut = *reinterpret_cast(dataCur); lightingOut = m_lightingData; lightingOut.colorRegs[0] = CGraphics::g_ColorRegs[0]; lightingOut.colorRegs[1] = CGraphics::g_ColorRegs[1]; lightingOut.colorRegs[2] = CGraphics::g_ColorRegs[2]; lightingOut.mulColor = flags.x4_color; lightingOut.fog = CGraphics::g_Fog; } dataCur += sizeof(CModelShaders::LightingUniform); dataCur = dataOut + ROUND_UP_256(dataCur - dataOut); /* Reflection texmtx uniform */ zeus::CMatrix4f* identMtxs = reinterpret_cast(dataCur); identMtxs[0] = zeus::CMatrix4f(); identMtxs[1] = zeus::CMatrix4f(); u8* curReflect = dataCur + 256; for (const CBooSurface& surf : *x0_surfaces) { const MaterialSet::Material& mat = x4_matSet->materials.at(surf.m_data.matIdx); if (mat.flags.samusReflection() || mat.flags.samusReflectionSurfaceEye()) { zeus::CMatrix4f* mtxs = reinterpret_cast(curReflect); float& alpha = reinterpret_cast(mtxs[2]); curReflect += 256; EnsureViewDepStateCached(*this, mat.flags.samusReflectionSurfaceEye() ? &surf : nullptr, mtxs, alpha); } } inst->m_uniformBuffer->unmap(); return inst->m_dynamicVbo; } void CBooModel::DrawAlpha(const CModelFlags& flags, const CSkinRules* cskr, const CPoseAsTransforms* pose) const { if (TryLockTextures()) { UpdateUniformData(flags, cskr, pose); DrawAlphaSurfaces(flags); } } void CBooModel::DrawNormal(const CModelFlags& flags, const CSkinRules* cskr, const CPoseAsTransforms* pose) const { if (TryLockTextures()) { UpdateUniformData(flags, cskr, pose); DrawNormalSurfaces(flags); } } void CBooModel::Draw(const CModelFlags& flags, const CSkinRules* cskr, const CPoseAsTransforms* pose) const { if (TryLockTextures()) { UpdateUniformData(flags, cskr, pose); DrawSurfaces(flags); } } static const u8* MemoryFromPartData(const u8*& dataCur, const u32*& secSizeCur) { const u8* ret; if (*secSizeCur != 0) ret = dataCur; else ret = nullptr; dataCur += hecl::SBig(*secSizeCur); ++secSizeCur; return ret; } std::unique_ptr CModel::MakeNewInstance(int shaderIdx, int subInsts, const boo::ObjToken txtrOverrides[8], bool lockParent) { if (shaderIdx >= x18_matSets.size()) shaderIdx = 0; auto ret = std::make_unique(m_selfToken, this, &x8_surfaces, x18_matSets[shaderIdx], m_staticVtxFmt, m_staticVbo, m_ibo, m_aabb, (m_flags & 0x2) != 0, subInsts, txtrOverrides); if (lockParent) ret->LockParent(); return ret; } std::shared_ptr SShader::BuildShader(const hecl::HMDLMeta& meta, const MaterialSet::Material& mat) { hecl::Backend::ReflectionType reflectionType; if (mat.flags.samusReflectionIndirectTexture()) reflectionType = hecl::Backend::ReflectionType::Indirect; else if (mat.flags.samusReflection()) reflectionType = hecl::Backend::ReflectionType::Simple; else reflectionType = hecl::Backend::ReflectionType::None; hecl::Runtime::ShaderTag tag(mat.heclIr, meta.colorCount, meta.uvCount, meta.weightCount, meta.weightCount * 4, 8, boo::Primitive(meta.topology), reflectionType, true, true, true); return CModelShaders::g_ModelShaders->buildExtendedShader (tag, mat.heclIr, "CMDL", *CGraphics::g_BooFactory); } void SShader::BuildShaders(const hecl::HMDLMeta& meta, std::unordered_map>& shaders) { shaders.reserve(m_matSet.materials.size()); int idx = 0; for (const MaterialSet::Material& mat : m_matSet.materials) shaders[idx++] = BuildShader(meta, mat); } CModel::CModel(std::unique_ptr&& in, u32 /* dataLen */, IObjectStore* store, CObjectReference* selfRef) : m_selfToken(selfRef) { x38_lastFrame = CGraphics::GetFrameCounter() - 2; std::unique_ptr data = std::move(in); u32 version = hecl::SBig(*reinterpret_cast(data.get() + 0x4)); m_flags = hecl::SBig(*reinterpret_cast(data.get() + 0x8)); if (version != 0x10002) Log.report(logvisor::Fatal, "invalid CMDL for loading with boo"); u32 secCount = hecl::SBig(*reinterpret_cast(data.get() + 0x24)); u32 matSetCount = hecl::SBig(*reinterpret_cast(data.get() + 0x28)); x18_matSets.reserve(matSetCount); const u8* dataCur = data.get() + ROUND_UP_32(0x2c + secCount * 4); const u32* secSizeCur = reinterpret_cast(data.get() + 0x2c); for (u32 i=0 ; i bool { /* Index buffer is always static */ if (m_hmdlMeta.indexCount) m_ibo = ctx.newStaticBuffer(boo::BufferUse::Index, iboData, 4, m_hmdlMeta.indexCount); if (!m_hmdlMeta.bankCount) { /* Non-skinned models use static vertex buffers shared with CBooModel instances */ if (m_hmdlMeta.vertCount) m_staticVbo = ctx.newStaticBuffer(boo::BufferUse::Vertex, vboData, m_hmdlMeta.vertStride, m_hmdlMeta.vertCount); m_staticVtxFmt = hecl::Runtime::HMDLData::NewVertexFormat(ctx, m_hmdlMeta, m_staticVbo.get(), m_ibo.get()); } else { /* Skinned models use per-instance dynamic buffers for vertex manipulation effects */ size_t vboSz = m_hmdlMeta.vertStride * m_hmdlMeta.vertCount; if (vboSz) { m_dynamicVertexData.reset(new uint8_t[vboSz]); memmove(m_dynamicVertexData.get(), vboData, vboSz); } } return true; }); u32 surfCount = hecl::SBig(*reinterpret_cast(surfInfo)); x8_surfaces.reserve(surfCount); for (u32 i=0 ; i(data.get() + 0xc); m_aabb = zeus::CAABox(hecl::SBig(aabbPtr[0]), hecl::SBig(aabbPtr[1]), hecl::SBig(aabbPtr[2]), hecl::SBig(aabbPtr[3]), hecl::SBig(aabbPtr[4]), hecl::SBig(aabbPtr[5])); x28_modelInst = MakeNewInstance(0, 1, nullptr, false); } void SShader::UnlockTextures() { for (TCachedToken& tex : x0_textures) tex.Unlock(); } void CBooModel::VerifyCurrentShader(int shaderIdx) { if (shaderIdx != m_matSetIdx && m_model) RemapMaterialData(m_model->x18_matSets[shaderIdx]); } void CBooModel::Touch(int shaderIdx) const { const_cast(this)->VerifyCurrentShader(shaderIdx); TryLockTextures(); } void CModel::DrawSortedParts(const CModelFlags& flags) const { const_cast(*x28_modelInst).VerifyCurrentShader(flags.x1_matSetIdx); x28_modelInst->DrawAlpha(flags, nullptr, nullptr); } void CModel::DrawUnsortedParts(const CModelFlags& flags) const { const_cast(*x28_modelInst).VerifyCurrentShader(flags.x1_matSetIdx); x28_modelInst->DrawNormal(flags, nullptr, nullptr); } void CModel::Draw(const CModelFlags& flags) const { const_cast(*x28_modelInst).VerifyCurrentShader(flags.x1_matSetIdx); x28_modelInst->Draw(flags, nullptr, nullptr); } bool CModel::IsLoaded(int shaderIdx) const { const_cast(*x28_modelInst).VerifyCurrentShader(shaderIdx); std::vector>& texs = x28_modelInst->x1c_textures; bool loaded = true; for (TCachedToken& tex : texs) { if (!tex.IsLoaded()) { loaded = false; break; } } return loaded; } size_t CModel::GetPoolVertexOffset(size_t idx) const { return m_hmdlMeta.vertStride * idx; } zeus::CVector3f CModel::GetPoolVertex(size_t idx) const { auto* floats = reinterpret_cast(m_dynamicVertexData.get() + GetPoolVertexOffset(idx)); return {floats[0], floats[1], floats[2]}; } size_t CModel::GetPoolNormalOffset(size_t idx) const { return m_hmdlMeta.vertStride * idx + 12; } zeus::CVector3f CModel::GetPoolNormal(size_t idx) const { auto* floats = reinterpret_cast(m_dynamicVertexData.get() + GetPoolNormalOffset(idx)); return {floats[0], floats[1], floats[2]}; } void CModel::ApplyVerticesCPU(const boo::ObjToken& vertBuf, const std::vector>& vn) const { u8* data = reinterpret_cast(vertBuf->map(m_hmdlMeta.vertStride * m_hmdlMeta.vertCount)); for (u32 i=0 ; i& avn = vn[i]; float* floats = reinterpret_cast(data + GetPoolVertexOffset(i)); floats[0] = avn.first.x; floats[1] = avn.first.y; floats[2] = avn.first.z; floats[3] = avn.second.x; floats[4] = avn.second.y; floats[5] = avn.second.z; } vertBuf->unmap(); } void CModel::_WarmupShaders() { CBooModel::SetDummyTextures(true); CBooModel::EnableShadowMaps(g_Renderer->x220_sphereRamp.get(), zeus::CTransform::Identity()); CGraphics::CProjectionState backupProj = CGraphics::GetProjectionState(); zeus::CTransform backupViewPoint = CGraphics::g_ViewMatrix; zeus::CTransform backupModel = CGraphics::g_GXModelMatrix; CGraphics::SetModelMatrix(zeus::CTransform::Translate(-m_aabb.center())); CGraphics::SetViewPointMatrix(zeus::CTransform::Translate(0.f, -2048.f, 0.f)); CGraphics::SetOrtho(-2048.f, 2048.f, 2048.f, -2048.f, 0.f, 4096.f); CModelFlags defaultFlags; for (SShader& shader : x18_matSets) { GetInstance().RemapMaterialData(shader); GetInstance().UpdateUniformData(defaultFlags, nullptr, nullptr); GetInstance().WarmupDrawSurfaces(); } CGraphics::SetProjectionState(backupProj); CGraphics::SetViewPointMatrix(backupViewPoint); CGraphics::SetModelMatrix(backupModel); CBooModel::DisableShadowMaps(); CBooModel::SetDummyTextures(false); } void CModel::WarmupShaders(const SObjectTag& cmdlTag) { TToken model = g_SimplePool->GetObj(cmdlTag); CModel* modelObj = model.GetObj(); modelObj->_WarmupShaders(); } CFactoryFnReturn FModelFactory(const urde::SObjectTag& tag, std::unique_ptr&& in, u32 len, const urde::CVParamTransfer& vparms, CObjectReference* selfRef) { CSimplePool* sp = vparms.GetOwnedObj(); CFactoryFnReturn ret = TToken::GetIObjObjectFor(std::make_unique(std::move(in), len, sp, selfRef)); return ret; } }