Skip to content

Commit a53f301

Browse files
showcase the solution to IAssetLoader::ELPF_RIGHT_HANDED_MESHES deprecation
1 parent c3bfff9 commit a53f301

1 file changed

Lines changed: 67 additions & 72 deletions

File tree

src/nbl/ext/MitsubaLoader/CMitsubaLoader.cpp

Lines changed: 67 additions & 72 deletions
Original file line numberDiff line numberDiff line change
@@ -694,52 +694,74 @@ auto SContext::loadBasicShape(const uint32_t hierarchyLevel, const CElementShape
694694
pGeometries->push_back(std::move(ref));
695695
};
696696

697-
#if 0
698-
constexpr uint32_t UV_ATTRIB_ID = 2u;
699-
700-
701697

702-
auto loadModel = [&](const ext::MitsubaLoader::SPropertyElementData& filename, int64_t index=-1) -> core::smart_refctd_ptr<asset::ICPUMesh>
698+
auto loadModel = [&](const char* filename, int64_t index=-1) -> void
703699
{
704-
assert(filename.type==ext::MitsubaLoader::SPropertyElementData::Type::STRING);
705-
auto loadParams = ctx.inner.params;
706-
loadParams.loaderFlags = static_cast<IAssetLoader::E_LOADER_PARAMETER_FLAGS>(loadParams.loaderFlags | IAssetLoader::ELPF_RIGHT_HANDED_MESHES);
707-
auto retval = interm_getAssetInHierarchy( filename.svalue, loadParams, hierarchyLevel/*+ICPUScene::MESH_HIERARCHY_LEVELS_BELOW*/, ctx.override_);
700+
#if 0
701+
auto retval = interm_getAssetInHierarchy(filename,inner.params,hierarchyLevel+/*ICPUScene::GEOMETRY_COLLECTION_HIERARCHY_LEVELS_BELOW*/1,override_);
708702
if (retval.getContents().empty())
709703
{
710704
os::Printer::log(std::string("[ERROR] Could Not Find Mesh: ") + filename.svalue, ELL_ERROR);
711-
return nullptr;
712-
}
713-
if (retval.getAssetType()!=asset::IAsset::ET_MESH)
714-
{
715-
os::Printer::log("[ERROR] Loaded an Asset but it wasn't a mesh, was E_ASSET_TYPE " + std::to_string(retval.getAssetType()), ELL_ERROR);
716-
return nullptr;
705+
return;
717706
}
718-
auto contentRange = retval.getContents();
719-
auto serializedMeta = retval.getMetadata()->selfCast<CMitsubaSerializedMetadata>();
720-
//
707+
721708
uint32_t actualIndex = 0;
722-
if (index>=0ll && serializedMeta)
723-
for (auto it=contentRange.begin(); it!=contentRange.end(); it++)
709+
switch (retval.getAssetType())
724710
{
725-
auto meshMeta = static_cast<const CMitsubaSerializedMetadata::CMesh*>(serializedMeta->getAssetSpecificMetadata(IAsset::castDown<ICPUMesh>(*it).get()));
726-
if (meshMeta->m_id!=static_cast<uint32_t>(index))
727-
continue;
728-
actualIndex = it-contentRange.begin();
729-
break;
711+
case IAsset::ET_GEOMETRY:
712+
{
713+
auto contentRange = retval.getContents();
714+
auto serializedMeta = retval.getMetadata()->selfCast<CMitsubaSerializedMetadata>();
715+
//
716+
if (index>=0ll && serializedMeta)
717+
for (auto it=contentRange.begin(); it!=contentRange.end(); it++)
718+
{
719+
auto meshMeta = static_cast<const CMitsubaSerializedMetadata::CMesh*>(serializedMeta->getAssetSpecificMetadata(IAsset::castDown<ICPUMesh>(*it).get()));
720+
if (meshMeta->m_id!=static_cast<uint32_t>(index))
721+
continue;
722+
actualIndex = it-contentRange.begin();
723+
break;
724+
}
725+
//
726+
if (contentRange.begin()+actualIndex < contentRange.end())
727+
{
728+
auto asset = contentRange.begin()[actualIndex];
729+
if (!asset)
730+
{
731+
return;
732+
}
733+
addGeometry(asset);
734+
}
735+
}
736+
case IAsset::ET_GEOMETRY_COLLECTION:
737+
{
738+
// TODO: replace the collection
739+
break;
740+
}
741+
case IAsset::ET_MORPH_TARGETS:
742+
{
743+
// TODO: take first target and replace the collection
744+
_NBL_DEBUG_BREAK_IF(true); // we have no such loaders right now
745+
break;
746+
}
747+
case IAsset::ET_SCENE:
748+
{
749+
// TODO: flatten the scene into a single instance, this is path for OBJ loading
750+
// NOTE: also need to preserve/forward the materials somehow (need to chape the `shape_ass_type` to have a default Material Binding Table)
751+
}
752+
default:
753+
os::Printer::log("[ERROR] Loaded an Asset but it wasn't a mesh, was E_ASSET_TYPE " + std::to_string(retval.getAssetType()), ELL_ERROR);
754+
break;
730755
}
731-
//
732-
if (contentRange.begin()+actualIndex < contentRange.end())
756+
#endif
757+
// we used to load with the IAssetLoader::ELPF_RIGHT_HANDED_MESHES flag, this means flipping the mesh x-axis
758+
for (auto& ref : *pGeometries)
733759
{
734-
auto asset = contentRange.begin()[actualIndex];
735-
if (!asset)
736-
return nullptr;
737-
return core::smart_refctd_ptr_static_cast<asset::ICPUMesh>(asset);
760+
ref.transform = math::linalg::diagonal<float32_t3x4>(1.f);
761+
ref.transform[0][0] = -1.f;
738762
}
739-
else
740-
return nullptr;
741763
};
742-
#endif
764+
743765
bool flipNormals = false;
744766
bool faceNormals = false;
745767
float maxSmoothAngle = bit_cast<float>(numeric_limits<float>::quiet_NaN);
@@ -792,8 +814,10 @@ auto SContext::loadBasicShape(const uint32_t hierarchyLevel, const CElementShape
792814
flipNormals = flipNormals!=shape->obj.flipNormals;
793815
faceNormals = shape->obj.faceNormals;
794816
maxSmoothAngle = shape->obj.maxSmoothAngle;
795-
if (mesh && shape->obj.flipTexCoords)
817+
if (!pGeometries->empty() && shape->obj.flipTexCoords)
796818
{
819+
_NBL_DEBUG_BREAK_IF(true);
820+
// TODO: find the UV attribute, it doesn't help we don't name them
797821
newMesh = core::smart_refctd_ptr_static_cast<asset::ICPUMesh> (mesh->clone(1u));
798822
for (auto& meshbuffer : mesh->getMeshBufferVector())
799823
{
@@ -811,54 +835,25 @@ auto SContext::loadBasicShape(const uint32_t hierarchyLevel, const CElementShape
811835
}
812836
}
813837
}
838+
#endif
814839
// collapse parameter gets ignored
815840
break;
816841
case CElementShape::Type::PLY:
817842
_NBL_DEBUG_BREAK_IF(true); // this code has never been tested
818-
mesh = loadModel(shape->ply.filename);
843+
loadModel(shape->ply.filename);
819844
flipNormals = flipNormals!=shape->ply.flipNormals;
820845
faceNormals = shape->ply.faceNormals;
821846
maxSmoothAngle = shape->ply.maxSmoothAngle;
822-
if (mesh && shape->ply.srgb)
847+
if (shape->ply.srgb)
848+
for (auto& ref : *pGeometries)
823849
{
824-
uint32_t totalVertexCount = 0u;
825-
for (auto meshbuffer : mesh->getMeshBuffers())
826-
totalVertexCount += IMeshManipulator::upperBoundVertexID(meshbuffer);
827-
if (totalVertexCount)
828-
{
829-
constexpr uint32_t hidefRGBSize = 4u;
830-
auto newRGBbuff = core::make_smart_refctd_ptr<asset::ICPUBuffer>(hidefRGBSize*totalVertexCount);
831-
newMesh = core::smart_refctd_ptr_static_cast<asset::ICPUMesh>(mesh->clone(1u));
832-
constexpr uint32_t COLOR_ATTR = 1u;
833-
constexpr uint32_t COLOR_BUF_BINDING = 15u;
834-
uint32_t* newRGB = reinterpret_cast<uint32_t*>(newRGBbuff->getPointer());
835-
uint32_t offset = 0u;
836-
for (auto& meshbuffer : mesh->getMeshBufferVector())
837-
{
838-
core::vectorSIMDf rgb;
839-
for (uint32_t i=0u; meshbuffer->getAttribute(rgb,COLOR_ATTR,i); i++,offset++)
840-
{
841-
for (auto i=0; i<3u; i++)
842-
rgb[i] = core::srgb2lin(rgb[i]);
843-
ICPUMeshBuffer::setAttribute(rgb,newRGB+offset,asset::EF_A2B10G10R10_UNORM_PACK32);
844-
}
845-
auto newPipeline = core::smart_refctd_ptr_static_cast<ICPURenderpassIndependentPipeline>(meshbuffer->getPipeline()->clone(0u));
846-
auto& vtxParams = newPipeline->getVertexInputParams();
847-
vtxParams.attributes[COLOR_ATTR].format = EF_A2B10G10R10_UNORM_PACK32;
848-
vtxParams.attributes[COLOR_ATTR].relativeOffset = 0u;
849-
vtxParams.attributes[COLOR_ATTR].binding = COLOR_BUF_BINDING;
850-
vtxParams.bindings[COLOR_BUF_BINDING].inputRate = EVIR_PER_VERTEX;
851-
vtxParams.bindings[COLOR_BUF_BINDING].stride = hidefRGBSize;
852-
vtxParams.enabledBindingFlags |= (1u<<COLOR_BUF_BINDING);
853-
meshbuffer->setPipeline(std::move(newPipeline));
854-
meshbuffer->setVertexBufferBinding({offset*hidefRGBSize,core::smart_refctd_ptr(newRGBbuff)},COLOR_BUF_BINDING);
855-
}
856-
}
850+
// TODO: find the color attribute (it doesn't help we don't name them, just slap them in vectors)
851+
// TODO: clone geometry
852+
// TODO: change the color aux attribute's format from UNORM8 to SRGB
857853
}
858-
#endif
859854
break;
860855
case CElementShape::Type::SERIALIZED:
861-
// mesh = loadModel(shape->serialized.filename,shape->serialized.shapeIndex);
856+
loadModel(shape->serialized.filename,shape->serialized.shapeIndex);
862857
flipNormals = flipNormals!=shape->serialized.flipNormals;
863858
faceNormals = shape->serialized.faceNormals;
864859
maxSmoothAngle = shape->serialized.maxSmoothAngle;

0 commit comments

Comments
 (0)