metaforce/Runtime/Graphics/CModelBoo.cpp

712 lines
23 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"
2016-04-04 02:32:57 +00:00
#include "Graphics/CLight.hpp"
2016-03-30 19:16:01 +00:00
#include "hecl/HMDLMeta.hpp"
2016-03-31 02:44:43 +00:00
#include "hecl/Runtime.hpp"
2016-04-04 06:16:03 +00:00
#include "boo/graphicsdev/Metal.hpp"
2016-04-04 02:32:57 +00:00
#include "CModelShaders.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-04-29 10:08:46 +00:00
static rstl::optional_object<CModelShaders> g_ModelShaders;
2016-04-04 02:32:57 +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,
size_t weightVecCount, size_t skinBankCount, const zeus::CAABox& aabb)
: x0_surfaces(surfaces), x4_matSet(&shader.m_matSet), m_pipelines(&shader.m_shaders),
m_vtxFmt(vtxFmt), x8_vbo(vbo), xc_ibo(ibo), m_weightVecCount(weightVecCount),
m_skinBankCount(skinBankCount), x1c_textures(&shader.x0_textures), x20_aabb(aabb),
x40_24_texturesLoaded(false), x40_25_(0)
2016-03-31 02:44:43 +00:00
{
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-04-04 02:32:57 +00:00
/* Determine space required by uniform buffer */
std::vector<size_t> skinOffs;
std::vector<size_t> skinSizes;
skinOffs.reserve(std::max(size_t(1), m_skinBankCount));
skinSizes.reserve(std::max(size_t(1), m_skinBankCount));
std::vector<size_t> uvOffs;
std::vector<size_t> uvSizes;
uvOffs.reserve(x4_matSet->materials.size());
uvSizes.reserve(x4_matSet->materials.size());
/* Vert transform matrices */
size_t uniBufSize = 0;
if (m_skinBankCount)
{
/* Skinned */
for (size_t i=0 ; i<m_skinBankCount ; ++i)
{
size_t thisSz = ROUND_UP_256(sizeof(zeus::CMatrix4f) * (2 * m_weightVecCount * 4 + 1));
skinOffs.push_back(uniBufSize);
skinSizes.push_back(thisSz);
uniBufSize += thisSz;
}
}
else
{
/* Non-Skinned */
size_t thisSz = ROUND_UP_256(sizeof(zeus::CMatrix4f) * 3);
skinOffs.push_back(uniBufSize);
skinSizes.push_back(thisSz);
uniBufSize += thisSz;
}
/* Animated UV transform matrices */
for (const MaterialSet::Material& mat : x4_matSet->materials)
{
size_t thisSz = ROUND_UP_256(mat.uvAnims.size() * (sizeof(zeus::CMatrix4f) * 2));
2016-04-04 02:32:57 +00:00
uvOffs.push_back(uniBufSize);
uvSizes.push_back(thisSz);
uniBufSize += thisSz;
}
/* 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;
}
/* Allocate resident buffer */
m_uniformDataSize = uniBufSize;
m_uniformBuffer = ctx.newDynamicBuffer(boo::BufferUse::Uniform, uniBufSize, 1);
2016-04-04 05:02:09 +00:00
boo::IGraphicsBuffer* bufs[] = {m_uniformBuffer, m_uniformBuffer, m_uniformBuffer};
2016-04-04 02:32:57 +00:00
/* Binding for each surface */
2016-03-31 07:50:52 +00:00
m_shaderDataBindings.clear();
2016-04-04 02:32:57 +00:00
m_shaderDataBindings.reserve(x0_surfaces->size());
2016-03-31 02:44:43 +00:00
std::vector<boo::ITexture*> texs;
2016-04-04 05:02:09 +00:00
size_t thisOffs[3];
size_t thisSizes[3];
2016-04-04 02:32:57 +00:00
2016-04-04 06:16:03 +00:00
static const boo::PipelineStage stages[3] = {boo::PipelineStage::Vertex,
boo::PipelineStage::Vertex,
boo::PipelineStage::Fragment};
2016-04-04 02:32:57 +00:00
/* Enumerate surfaces and build data bindings */
for (const CBooSurface& surf : *x0_surfaces)
2016-03-31 02:44:43 +00:00
{
2016-04-04 02:32:57 +00:00
const MaterialSet::Material& mat = x4_matSet->materials.at(surf.m_data.matIdx);
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-04-04 02:32:57 +00:00
if (m_skinBankCount)
{
2016-04-04 05:02:09 +00:00
thisOffs[0] = skinOffs[surf.m_data.skinMtxBankIdx];
thisSizes[0] = skinSizes[surf.m_data.skinMtxBankIdx];
2016-04-04 02:32:57 +00:00
}
else
{
2016-04-04 05:02:09 +00:00
thisOffs[0] = 0;
thisSizes[0] = 256;
2016-04-04 02:32:57 +00:00
}
if (mat.uvAnims.size())
{
2016-04-04 05:02:09 +00:00
thisOffs[1] = uvOffs[surf.m_data.matIdx];
thisSizes[1] = uvSizes[surf.m_data.matIdx];
}
else
{
thisOffs[1] = 0;
thisSizes[1] = 0;
2016-04-04 02:32:57 +00:00
}
2016-04-04 05:02:09 +00:00
thisOffs[2] = lightOff;
thisSizes[2] = lightSz;
2016-04-04 02:32:57 +00:00
const std::vector<boo::IShaderPipeline*>& pipelines = m_pipelines->at(surf.m_data.matIdx);
m_shaderDataBindings.emplace_back();
std::vector<boo::IShaderDataBinding*>& extendeds = m_shaderDataBindings.back();
extendeds.reserve(pipelines.size());
for (boo::IShaderPipeline* pipeline : pipelines)
extendeds.push_back(
ctx.newShaderDataBinding(pipeline, m_vtxFmt,
x8_vbo, nullptr, xc_ibo, 3, bufs, stages,
thisOffs, thisSizes, mat.textureIdxs.size(), texs.data()));
2016-03-31 02:44:43 +00:00
}
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()}));
}
2016-04-04 02:32:57 +00:00
void CBooModel::ActivateLights(const std::vector<CLight>& lights)
{
2016-04-04 05:02:09 +00:00
m_lightingData.ambient = zeus::CColor::skBlack;
2016-04-04 02:32:57 +00:00
size_t curLight = 0;
for (const CLight& light : lights)
{
switch (light.x1c_type)
{
case ELightType::LocalAmbient:
2016-04-04 05:02:09 +00:00
m_lightingData.ambient += light.x18_color;
break;
2016-04-04 02:32:57 +00:00
case ELightType::Point:
case ELightType::Spot:
case ELightType::Custom:
case ELightType::Directional:
{
if (curLight >= URDE_MAX_LIGHTS)
continue;
CModelShaders::Light& lightOut = m_lightingData.lights[curLight++];
lightOut.pos = CGraphics::g_CameraMatrix * light.x0_pos;
2016-04-29 10:08:46 +00:00
lightOut.dir = CGraphics::g_CameraMatrix.basis * light.xc_dir;
2016-04-04 05:02:09 +00:00
lightOut.dir.normalize();
2016-04-04 02:32:57 +00:00
lightOut.color = light.x18_color;
lightOut.linAtt[0] = light.x24_distC;
lightOut.linAtt[1] = light.x28_distL;
lightOut.linAtt[2] = light.x2c_distQ;
lightOut.angAtt[0] = light.x30_angleC;
lightOut.angAtt[1] = light.x34_angleL;
lightOut.angAtt[2] = light.x38_angleQ;
2016-04-04 21:20:26 +00:00
if (light.x1c_type == ELightType::Directional)
lightOut.pos = (-lightOut.dir) * 1048576.f;
2016-04-04 02:32:57 +00:00
break;
}
default: break;
}
}
for (; curLight<URDE_MAX_LIGHTS ; ++curLight)
{
CModelShaders::Light& lightOut = m_lightingData.lights[curLight];
lightOut.color = zeus::CColor::skClear;
lightOut.linAtt[0] = 1.f;
lightOut.angAtt[0] = 1.f;
}
}
2016-03-31 02:44:43 +00:00
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-04-01 01:00:37 +00:00
const CBooSurface* surf = x3c_firstSortedSurface;
while (surf)
2016-03-31 02:44:43 +00:00
{
2016-04-01 01:00:37 +00:00
DrawSurface(*surf, flags);
surf = surf->m_next;
2016-03-31 02:44:43 +00:00
}
2016-03-30 19:16:01 +00:00
}
void CBooModel::DrawNormalSurfaces(const CModelFlags& flags) const
{
2016-04-01 01:00:37 +00:00
const CBooSurface* surf = x38_firstUnsortedSurface;
while (surf)
2016-03-31 02:44:43 +00:00
{
2016-04-01 01:00:37 +00:00
DrawSurface(*surf, flags);
surf = surf->m_next;
2016-03-31 02:44:43 +00:00
}
2016-03-30 19:16:01 +00:00
}
void CBooModel::DrawSurfaces(const CModelFlags& flags) const
{
2016-03-31 02:44:43 +00:00
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;
2016-04-04 02:32:57 +00:00
const std::vector<boo::IShaderDataBinding*>& extendeds = m_shaderDataBindings[surf.selfIdx];
boo::IShaderDataBinding* binding = extendeds[0];
if (flags.m_extendedShaderIdx < extendeds.size())
binding = extendeds[flags.m_extendedShaderIdx];
CGraphics::SetShaderDataBinding(binding);
2016-03-31 02:44:43 +00:00
CGraphics::DrawArrayIndexed(surf.m_data.idxStart, surf.m_data.idxCount);
2016-03-30 19:16:01 +00:00
}
2016-04-04 02:32:57 +00:00
void CBooModel::UVAnimationBuffer::ProcessAnimation(u8*& bufOut, const UVAnimation& anim)
2016-03-31 06:18:56 +00:00
{
2016-07-08 19:57:51 +00:00
zeus::CMatrix4f& texMtxOut = reinterpret_cast<zeus::CMatrix4f&>(*bufOut);
zeus::CMatrix4f& postMtxOut = reinterpret_cast<zeus::CMatrix4f&>(*(bufOut + sizeof(zeus::CMatrix4f)));
2016-07-08 19:57:51 +00:00
texMtxOut = zeus::CMatrix4f();
postMtxOut = zeus::CMatrix4f();
2016-03-31 06:18:56 +00:00
switch (anim.mode)
{
case UVAnimation::Mode::MvInvNoTranslation:
{
2016-07-09 22:02:31 +00:00
texMtxOut = (CGraphics::g_ViewMatrix * CGraphics::g_GXModelMatrix).inverse().toMatrix4f();
2016-07-08 19:57:51 +00:00
texMtxOut.vec[3].zeroOut();
2016-07-08 20:25:38 +00:00
postMtxOut.vec[0].x = 0.5f;
postMtxOut.vec[1].y = 0.0f;
postMtxOut.vec[2].y = 0.5f;
postMtxOut.vec[3].x = 0.5f;
postMtxOut.vec[3].y = 0.5f;
2016-03-31 06:18:56 +00:00
break;
}
case UVAnimation::Mode::MvInv:
{
2016-07-09 22:02:31 +00:00
texMtxOut = (CGraphics::g_ViewMatrix * CGraphics::g_GXModelMatrix).inverse().toMatrix4f();
2016-07-08 20:25:38 +00:00
postMtxOut.vec[0].x = 0.5f;
postMtxOut.vec[1].y = 0.0f;
postMtxOut.vec[2].y = 0.5f;
postMtxOut.vec[3].x = 0.5f;
postMtxOut.vec[3].y = 0.5f;
2016-03-31 06:18:56 +00:00
break;
}
case UVAnimation::Mode::Scroll:
{
2016-07-08 19:57:51 +00:00
texMtxOut.vec[3].x = CGraphics::GetSecondsMod900() * anim.vals[2] + anim.vals[0];
texMtxOut.vec[3].y = CGraphics::GetSecondsMod900() * anim.vals[3] + anim.vals[1];
2016-03-31 06:18:56 +00:00
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);
2016-07-08 19:57:51 +00:00
texMtxOut.vec[0].x = acos;
texMtxOut.vec[0].y = asin;
texMtxOut.vec[1].x = -asin;
texMtxOut.vec[1].y = acos;
2016-07-09 22:02:31 +00:00
texMtxOut.vec[3].x = (1.0f - (acos - asin)) * 0.5f;
texMtxOut.vec[3].y = (1.0f - (asin + acos)) * 0.5f;
2016-03-31 06:18:56 +00:00
break;
}
case UVAnimation::Mode::HStrip:
{
2016-07-08 19:57:51 +00:00
float value = anim.vals[0] * anim.vals[2] * (anim.vals[3] + CGraphics::GetSecondsMod900());
2016-07-09 22:02:31 +00:00
texMtxOut.vec[3].x = (float)(short)(float)(anim.vals[1] * fmod(value, 1.0f)) * anim.vals[2];
2016-03-31 06:18:56 +00:00
break;
}
case UVAnimation::Mode::VStrip:
{
2016-07-08 19:57:51 +00:00
float value = anim.vals[0] * anim.vals[2] * (anim.vals[3] + CGraphics::GetSecondsMod900());
2016-07-09 22:02:31 +00:00
texMtxOut.vec[3].y = (float)(short)(float)(anim.vals[1] * fmod(value, 1.0f)) * anim.vals[2];
2016-03-31 06:18:56 +00:00
break;
}
case UVAnimation::Mode::Model:
{
2016-07-08 20:25:38 +00:00
texMtxOut = CGraphics::g_GXModelMatrix.toMatrix4f();
texMtxOut.vec[3].zeroOut();
postMtxOut.vec[0].x = 0.5f;
postMtxOut.vec[1].y = 0.0f;
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;
2016-03-31 06:18:56 +00:00
break;
}
case UVAnimation::Mode::WhoMustNotBeNamed:
{
2016-07-08 20:25:38 +00:00
texMtxOut = (CGraphics::g_ViewMatrix.inverse() * CGraphics::g_GXModelMatrix).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();
2016-03-31 06:18:56 +00:00
break;
}
default: break;
}
bufOut += sizeof(zeus::CMatrix4f) * 2;
2016-03-31 06:18:56 +00:00
}
2016-04-04 02:32:57 +00:00
void CBooModel::UVAnimationBuffer::PadOutBuffer(u8*& bufStart, u8*& bufOut)
2016-03-31 06:18:56 +00:00
{
2016-04-04 02:32:57 +00:00
bufOut = bufStart + ROUND_UP_256(bufOut - bufStart);
2016-03-31 06:18:56 +00:00
}
2016-04-04 02:32:57 +00:00
void CBooModel::UVAnimationBuffer::Update(u8*& bufOut, const MaterialSet* matSet)
2016-03-31 06:18:56 +00:00
{
2016-04-04 02:32:57 +00:00
u8* start = bufOut;
2016-03-31 06:18:56 +00:00
for (const MaterialSet::Material& mat : matSet->materials)
{
for (const UVAnimation& anim : mat.uvAnims)
2016-04-04 02:32:57 +00:00
ProcessAnimation(bufOut, anim);
PadOutBuffer(start, bufOut);
2016-03-31 06:18:56 +00:00
}
}
void CBooModel::UpdateUniformData() const
{
2016-04-04 19:34:54 +00:00
u8* dataOut = reinterpret_cast<u8*>(m_uniformBuffer->map(m_uniformDataSize));
u8* dataCur = dataOut;
2016-03-31 06:18:56 +00:00
2016-04-04 02:32:57 +00:00
if (m_skinBankCount)
2016-03-31 21:06:41 +00:00
{
2016-04-04 02:32:57 +00:00
/* Skinned */
for (size_t i=0 ; i<m_skinBankCount ; ++i)
{
for (size_t w=0 ; w<m_weightVecCount*4 ; ++w)
{
2016-04-04 19:34:54 +00:00
zeus::CMatrix4f& mv = reinterpret_cast<zeus::CMatrix4f&>(*dataCur);
2016-04-04 02:32:57 +00:00
mv = CGraphics::g_GXModelView.toMatrix4f();
2016-04-04 19:34:54 +00:00
dataCur += sizeof(zeus::CMatrix4f);
2016-04-04 02:32:57 +00:00
}
for (size_t w=0 ; w<m_weightVecCount*4 ; ++w)
{
2016-04-04 19:34:54 +00:00
zeus::CMatrix4f& mvinv = reinterpret_cast<zeus::CMatrix4f&>(*dataCur);
2016-04-04 02:32:57 +00:00
mvinv = CGraphics::g_GXModelViewInvXpose.toMatrix4f();
2016-04-04 19:34:54 +00:00
dataCur += sizeof(zeus::CMatrix4f);
2016-04-04 02:32:57 +00:00
}
2016-04-04 19:34:54 +00:00
zeus::CMatrix4f& proj = reinterpret_cast<zeus::CMatrix4f&>(*dataCur);
2016-04-04 02:32:57 +00:00
proj = CGraphics::GetPerspectiveProjectionMatrix(true);
2016-04-04 19:34:54 +00:00
dataCur += sizeof(zeus::CMatrix4f);
2016-04-04 02:32:57 +00:00
2016-04-04 19:34:54 +00:00
dataCur = dataOut + ROUND_UP_256(dataCur - dataOut);
2016-04-04 02:32:57 +00:00
}
}
else
{
/* Non-Skinned */
2016-04-04 19:34:54 +00:00
zeus::CMatrix4f& mv = reinterpret_cast<zeus::CMatrix4f&>(*dataCur);
2016-04-04 02:32:57 +00:00
mv = CGraphics::g_GXModelView.toMatrix4f();
2016-04-04 19:34:54 +00:00
dataCur += sizeof(zeus::CMatrix4f);
2016-04-04 02:32:57 +00:00
2016-04-04 19:34:54 +00:00
zeus::CMatrix4f& mvinv = reinterpret_cast<zeus::CMatrix4f&>(*dataCur);
2016-04-04 02:32:57 +00:00
mvinv = CGraphics::g_GXModelViewInvXpose.toMatrix4f();
2016-04-04 19:34:54 +00:00
dataCur += sizeof(zeus::CMatrix4f);
2016-04-04 02:32:57 +00:00
2016-04-04 19:34:54 +00:00
zeus::CMatrix4f& proj = reinterpret_cast<zeus::CMatrix4f&>(*dataCur);
2016-04-04 02:32:57 +00:00
proj = CGraphics::GetPerspectiveProjectionMatrix(true);
2016-04-04 19:34:54 +00:00
dataCur += sizeof(zeus::CMatrix4f);
2016-04-04 02:32:57 +00:00
2016-04-04 19:34:54 +00:00
dataCur = dataOut + ROUND_UP_256(dataCur - dataOut);
2016-03-31 21:06:41 +00:00
}
2016-04-04 02:32:57 +00:00
2016-04-04 19:34:54 +00:00
UVAnimationBuffer::Update(dataCur, x4_matSet);
2016-04-04 02:32:57 +00:00
2016-04-04 19:34:54 +00:00
CModelShaders::LightingUniform& lightingOut = reinterpret_cast<CModelShaders::LightingUniform&>(*dataCur);
2016-04-04 02:32:57 +00:00
lightingOut = m_lightingData;
2016-04-04 19:34:54 +00:00
m_uniformBuffer->unmap();
2016-03-31 06:18:56 +00:00
}
2016-03-30 19:16:01 +00:00
void CBooModel::DrawAlpha(const CModelFlags& flags) const
{
2016-04-01 01:00:37 +00:00
if (TryLockTextures())
{
UpdateUniformData();
DrawAlphaSurfaces(flags);
}
2016-03-30 19:16:01 +00:00
}
void CBooModel::DrawNormal(const CModelFlags& flags) const
{
2016-04-01 01:00:37 +00:00
if (TryLockTextures())
{
UpdateUniformData();
DrawNormalSurfaces(flags);
}
2016-03-30 19:16:01 +00:00
}
void CBooModel::Draw(const CModelFlags& flags) const
{
2016-04-01 01:00:37 +00:00
if (TryLockTextures())
{
UpdateUniformData();
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;
}
std::unique_ptr<CBooModel> CModel::MakeNewInstance(int shaderIdx)
2016-03-29 23:14:14 +00:00
{
if (shaderIdx >= x18_matSets.size())
shaderIdx = 0;
return std::make_unique<CBooModel>(&x8_surfaces, x18_matSets[shaderIdx],
m_vtxFmt, m_vbo, m_ibo, 0, 0, m_aabb);
}
CModel::CModel(std::unique_ptr<u8[]>&& in, u32 /* dataLen */, IObjectStore* store)
{
std::unique_ptr<u8[]> data = std::move(in);
u32 version = hecl::SBig(*reinterpret_cast<u32*>(data.get() + 0x4));
u32 flags = hecl::SBig(*reinterpret_cast<u32*>(data.get() + 0x8));
2016-03-31 21:06:41 +00:00
if (version != 0x10002)
2016-03-29 23:14:14 +00:00
Log.report(logvisor::Fatal, "invalid CMDL for loading with boo");
u32 secCount = hecl::SBig(*reinterpret_cast<u32*>(data.get() + 0x24));
u32 matSetCount = hecl::SBig(*reinterpret_cast<u32*>(data.get() + 0x28));
2016-03-29 23:14:14 +00:00
x18_matSets.reserve(matSetCount);
const u8* dataCur = data.get() + ROUND_UP_32(0x2c + secCount * 4);
const s32* secSizeCur = reinterpret_cast<const s32*>(data.get() + 0x2c);
2016-03-29 23:14:14 +00:00
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);
m_vtxFmt = hecl::Runtime::HMDLData::NewVertexFormat(ctx, hmdlMeta, m_vbo, m_ibo);
2016-03-31 02:44:43 +00:00
for (CBooModel::SShader& matSet : x18_matSets)
{
2016-03-31 02:44:43 +00:00
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)
{
hecl::Runtime::ShaderTag tag(mat.heclIr,
hmdlMeta.colorCount, hmdlMeta.uvCount, hmdlMeta.weightCount,
0, mat.uvAnims.size(), boo::Primitive(hmdlMeta.topology),
true, true, true);
matSet.m_shaders.push_back(g_ModelShaders->buildExtendedShader(tag, mat.heclIr, "CMDL", ctx));
}
}
2016-03-31 02:44:43 +00:00
return true;
});
2016-03-30 19:16:01 +00:00
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();
2016-04-04 02:32:57 +00:00
surf.selfIdx = i;
2016-03-31 02:44:43 +00:00
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*>(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);
2016-03-29 23:14:14 +00:00
}
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-04-04 02:32:57 +00:00
hecl::Runtime::ShaderCacheExtensions
CModelShaders::GetShaderExtensions(boo::IGraphicsDataFactory::Platform plat)
{
switch (plat)
{
case boo::IGraphicsDataFactory::Platform::OGL:
case boo::IGraphicsDataFactory::Platform::Vulkan:
return GetShaderExtensionsGLSL(plat);
2016-04-04 02:32:57 +00:00
#if _WIN32
case boo::IGraphicsDataFactory::Platform::D3D11:
case boo::IGraphicsDataFactory::Platform::D3D12:
return GetShaderExtensionsHLSL(plat);
2016-04-04 02:32:57 +00:00
#endif
#if BOO_HAS_METAL
case boo::IGraphicsDataFactory::Platform::Metal:
return GetShaderExtensionsMetal(plat);
2016-04-04 02:32:57 +00:00
#endif
default:
return {boo::IGraphicsDataFactory::Platform::Null};
2016-04-04 02:32:57 +00:00
}
}
CModelShaders::CModelShaders(const hecl::Runtime::FileStoreManager& storeMgr,
boo::IGraphicsDataFactory* gfxFactory)
: m_shaderCache(storeMgr, gfxFactory, GetShaderExtensions(gfxFactory->platform())) {}
2016-04-04 02:32:57 +00:00
void CModelShaders::Initialize(const hecl::Runtime::FileStoreManager& storeMgr,
boo::IGraphicsDataFactory* gfxFactory)
{
g_ModelShaders.emplace(storeMgr, gfxFactory);
}
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();
2016-04-01 01:00:37 +00:00
CFactoryFnReturn ret = TToken<CModel>::GetIObjObjectFor(std::make_unique<CModel>(std::move(in), len, store));
return ret;
2016-03-29 23:14:14 +00:00
}
2016-02-13 00:57:09 +00:00
}