Skip to content

Commit 0b28b76

Browse files
adjust the MitsubaLoader to changes in the Frontend, CI should pass now
1 parent 5be57c2 commit 0b28b76

4 files changed

Lines changed: 45 additions & 27 deletions

File tree

examples_tests

include/nbl/asset/asset.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,9 @@
4141
#include "nbl/asset/utils/CGLSLCompiler.h"
4242
#include "nbl/asset/utils/CSPIRVIntrospector.h"
4343

44+
// material compiler
45+
#include "nbl/asset/material_compiler3/CFrontendIR.h"
46+
4447
// pipelines
4548

4649
// skinning

include/nbl/ext/MitsubaLoader/SContext.h

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ struct SContext final
3838

3939
inline void writeFrontendForestDot3(system::ISystem* system, const system::path& filepath)
4040
{
41-
asset::material_compiler3::CFrontendIR::SDotPrinter printer = {frontIR.get(),frontIR->getMaterials()};
41+
asset::material_compiler3::CFrontendIR::SDotPrinter printer = {frontIR.get(),debugAllMaterials};
4242
writeDot3File(system,filepath,printer);
4343
}
4444

@@ -48,6 +48,10 @@ struct SContext final
4848
meta->setGeometryCollectionMeta(std::move(groupCache));
4949
}
5050

51+
using true_ir_t = asset::material_compiler3::CTrueIR;
52+
using frontend_ir_t = asset::material_compiler3::CFrontendIR;
53+
inline const frontend_ir_t* getMaterialFrontend() const {return frontIR.get();}
54+
5155
const asset::IAssetLoader::SAssetLoadContext inner;
5256
asset::IAssetLoader::IAssetLoaderOverride* override_;
5357
std::function<interm_getAssetInHierarchy_t> interm_getAssetInHierarchy;
@@ -56,7 +60,6 @@ struct SContext final
5660
core::smart_refctd_ptr<asset::ICPUScene> scene;
5761

5862
private:
59-
using frontend_ir_t = asset::material_compiler3::CFrontendIR;
6063
using frontend_material_t = frontend_ir_t::typed_pointer_type<const frontend_ir_t::CLayer>;
6164
// not `frontend_ir_t::CEmitter` because the color factor gets multiplied in
6265
using frontend_emitter_t = frontend_ir_t::typed_pointer_type<const frontend_ir_t::CMul>;
@@ -65,10 +68,10 @@ struct SContext final
6568
//
6669
void writeDot3File(system::ISystem* system, const system::path& filepath, frontend_ir_t::SDotPrinter& printer);
6770
//
68-
hlsl::float32_t2x3 getParameters(const std::span<frontend_ir_t::SParameter> out, const CElementTexture::FloatOrTexture& src);
69-
hlsl::float32_t2x3 getParameters(const std::span<frontend_ir_t::SParameter,3> out, const CElementTexture::SpectrumOrTexture& src);
70-
frontend_ir_t::SParameter getTexture(const CElementTexture* tex, hlsl::float32_t2x3* outUvTransform);
71-
frontend_ir_t::SParameter genProfile(const CElementEmissionProfile* profile);
71+
hlsl::float32_t2x3 getParameters(const std::span<true_ir_t::SParameter> out, const CElementTexture::FloatOrTexture& src);
72+
hlsl::float32_t2x3 getParameters(const std::span<true_ir_t::SParameter,3> out, const CElementTexture::SpectrumOrTexture& src);
73+
true_ir_t::SParameter getTexture(const CElementTexture* tex, hlsl::float32_t2x3* outUvTransform);
74+
true_ir_t::SParameter genProfile(const CElementEmissionProfile* profile);
7275

7376
//
7477
core::unordered_map<const CElementShape*,CMitsubaMetadata::SGeometryCollectionMetaPair> shapeCache;
@@ -77,7 +80,7 @@ struct SContext final
7780
//
7881
core::unordered_map<const CElementEmitter*,frontend_emitter_t> emitterCache;
7982
core::unordered_map<const CElementBSDF*,frontend_material_t> bsdfCache;
80-
core::unordered_map<const CElementEmissionProfile*,frontend_ir_t::SParameter> profileCache;
83+
core::unordered_map<const CElementEmissionProfile*,true_ir_t::SParameter> profileCache;
8184

8285
#if 0 // stuff that belongs in the Material Compiler backend
8386
//image, sampler
@@ -86,6 +89,8 @@ struct SContext final
8689
core::map<core::smart_refctd_ptr<asset::ICPUImage>,float> derivMapCache;
8790
#endif
8891
core::smart_refctd_ptr<frontend_ir_t> frontIR;
92+
// for a debug print
93+
core::vector<frontend_ir_t::typed_pointer_type<const frontend_ir_t::CLayer>> debugAllMaterials;
8994
// common frontend nodes
9095
frontend_ir_t::typed_pointer_type<const frontend_ir_t::CSpectralVariable> unityFactor;
9196
frontend_ir_t::typed_pointer_type<const frontend_ir_t::IExprNode> errorBRDF;

src/nbl/ext/MitsubaLoader/CMitsubaLoader.cpp

Lines changed: 29 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -271,8 +271,17 @@ SAssetBundle CMitsubaLoader::loadAsset(system::IFile* _file, const IAssetLoader:
271271
instances.getInitialTransforms()[index] = shape->getTransform();
272272
//
273273
const core::string debugName = shape->id.empty() ? std::format("0x{:x}",ptrdiff_t(shape)):shape->id;
274-
ctx.getMaterial(shape->bsdf,shape->obtainEmitter(),debugName,PrintMaterialDot3 ? m_system.get():nullptr);
275-
// TODO: compile the material into the true IR and push it into the instances
274+
const auto astRootH = ctx.getMaterial(shape->bsdf,shape->obtainEmitter(),debugName,PrintMaterialDot3 ? m_system.get():nullptr);
275+
// TODO: get the output slot for the material in the ICPUScene from `instances.getMaterialTables()`
276+
material_compiler3::CTrueIR::SMaterialHandle rMaterialHandle = {}; // instances.getMaterialTables()
277+
if (astRootH)
278+
{
279+
// we can't batch because of how the parser works
280+
const auto addedCount = ctx.getMaterialFrontend()->addMaterials({.rootNodes={&astRootH,1},.ir=ctx.scene->getMaterialPool(),.result=&rMaterialHandle,.logger=_params.logger});
281+
// we could check `addedCount` but the function above does its own logging
282+
}
283+
else // TODO: make a special Error material
284+
rMaterialHandle = material_compiler3::CTrueIR::BlackholeMaterialHandle;
276285
};
277286

278287
// first go over all actually used shapes which are not shapegroups (regular shapes and instances)
@@ -385,7 +394,8 @@ auto SContext::getMaterial(
385394
}
386395
else
387396
root->brdfTop = foundEmitter->second._const_cast();
388-
const bool success = frontIR->addMaterial(rootH,inner.params.logger);
397+
//
398+
const bool success = frontIR->valid(rootH,inner.params.logger);
389399

390400
auto logger = inner.params.logger;
391401
if (!success)
@@ -398,13 +408,14 @@ auto SContext::getMaterial(
398408
const frontend_ir_t::typed_pointer_type<const frontend_ir_t::CLayer> constRootH = rootH;
399409
frontend_ir_t::SDotPrinter printer = {frontIR.get(),{&constRootH,1}};
400410
writeDot3File(debugFileWriter,DebugDir/"material_frontend"/(debugName+".dot"),printer);
411+
debugAllMaterials.push_back(constRootH);
401412
}
402413

403414
return rootH;
404415
}
405416

406417

407-
using parameter_t = asset::material_compiler3::CFrontendIR::SParameter;
418+
using parameter_t = asset::material_compiler3::CTrueIR::SParameter;
408419
parameter_t SContext::getTexture(const CElementTexture* const rootTex, hlsl::float32_t2x3* outUvTransform)
409420
{
410421
parameter_t retval = {};
@@ -594,22 +605,22 @@ auto SContext::genEmitter(const CElementEmitter* _emitter, system::ISystem* debu
594605
spectral_var_t::SCreationParams<3> params = {};
595606
// if you wanted a textured emitter, this would be the place to do it
596607
MitsubaLoader::getParameters<3>(params.knots.params,_emitter->area.radiance);
597-
params.getSemantics() = spectral_var_t::Semantics::Fixed3_SRGB;
608+
params.getSemantics() = true_ir_t::ISpectralVariable::ESemantics::Fixed3_SRGB;
598609
mul->rhs = frontPool.emplace<spectral_var_t>(std::move(params));
599610
}
600611

601612
if (debugFileWriter)
602613
{
603614
frontend_ir_t::SDotPrinter printer = {frontIR.get()};
604-
printer.exprStack.push(handle);
615+
printer.exprStack.push_back(handle);
605616
writeDot3File(debugFileWriter,DebugDir/"material_frontend/emitters"/(debugName+".dot"),printer);
606617
}
607618
return handle;
608619
}
609620

610-
auto SContext::genProfile(const CElementEmissionProfile* profile) -> frontend_ir_t::SParameter
621+
auto SContext::genProfile(const CElementEmissionProfile* profile) -> true_ir_t::SParameter
611622
{
612-
frontend_ir_t::SParameter retval = {};
623+
true_ir_t::SParameter retval = {};
613624
// load it!
614625
using namespace nbl::asset;
615626
const CIESProfileMetadata* iesMeta = nullptr;
@@ -659,7 +670,7 @@ auto SContext::genMaterial(const CElementBSDF* bsdf, system::ISystem* debugFileW
659670
{
660671
spectral_var_t::SCreationParams<3> params = {};
661672
params.knots.uvTransform = getParameters(params.knots.params,factor);
662-
params.getSemantics() = spectral_var_t::Semantics::Fixed3_SRGB;
673+
params.getSemantics() = true_ir_t::ISpectralVariable::ESemantics::Fixed3_SRGB;
663674
const auto factorH = frontPool.emplace<spectral_var_t>(std::move(params));
664675
frontPool.deref(factorH)->debugInfo = commonDebugNames[uint16_t(debug)]._const_cast();
665676
return factorH;
@@ -678,7 +689,7 @@ auto SContext::genMaterial(const CElementBSDF* bsdf, system::ISystem* debugFileW
678689
auto* mul = frontPool.deref(mulH);
679690
const auto ctH = frontPool.emplace<frontend_ir_t::CCookTorrance>();
680691
{
681-
using ndf_e = frontend_ir_t::CCookTorrance::NDF;
692+
using ndf_e = true_ir_t::SBasicNDFParams::EDistribution;
682693
constexpr ndf_e ndfMap[4] = {
683694
ndf_e::Beckmann,
684695
ndf_e::GGX,
@@ -690,7 +701,7 @@ auto SContext::genMaterial(const CElementBSDF* bsdf, system::ISystem* debugFileW
690701
if (base)
691702
{
692703
// ct->orientedRealEta gets set in the fresnel part
693-
ct->ndf = ndfMap[base->distribution];
704+
ct->ndParams.getDistribution() = ndfMap[base->distribution];
694705
// this function sets both roughnesses to same alpha
695706
ct->ndParams.uvTransform = getParameters(roughness,base->alpha);
696707
if (base->alphaV.texture || !hlsl::isnan(base->alphaV.value))
@@ -819,7 +830,7 @@ auto SContext::genMaterial(const CElementBSDF* bsdf, system::ISystem* debugFileW
819830
{
820831
spectral_var_t::SCreationParams<3> params = {};
821832
params.knots.uvTransform = getParameters(params.knots.params,_bsdf->diffuse.reflectance);
822-
params.getSemantics() = spectral_var_t::Semantics::Fixed3_SRGB;
833+
params.getSemantics() = true_ir_t::ISpectralVariable::ESemantics::Fixed3_SRGB;
823834
const auto albedoH = frontPool.emplace<spectral_var_t>(std::move(params));
824835
frontPool.deref(albedoH)->debugInfo = commonDebugNames[uint16_t(ECommonDebug::Albedo)]._const_cast();
825836
mul->rhs = albedoH;
@@ -858,14 +869,14 @@ auto SContext::genMaterial(const CElementBSDF* bsdf, system::ISystem* debugFileW
858869
spectral_var_t::SCreationParams<3> params = {};
859870
const hlsl::float32_t3 eta = _bsdf->conductor.eta.vvalue.xyz;
860871
MitsubaLoader::getParameters<3>(params.knots.params,eta/extEta);
861-
params.getSemantics() = spectral_var_t::Semantics::Fixed3_SRGB;
872+
params.getSemantics() = true_ir_t::ISpectralVariable::ESemantics::Fixed3_SRGB;
862873
fresnel->orientedRealEta = frontPool.emplace<spectral_var_t>(std::move(params));
863874
}
864875
{
865876
spectral_var_t::SCreationParams<3> params = {};
866877
const hlsl::float32_t3 k = _bsdf->conductor.k.vvalue.xyz;
867878
MitsubaLoader::getParameters<3>(params.knots.params,k/extEta);
868-
params.getSemantics() = spectral_var_t::Semantics::Fixed3_SRGB;
879+
params.getSemantics() = true_ir_t::ISpectralVariable::ESemantics::Fixed3_SRGB;
869880
fresnel->orientedImagEta = frontPool.emplace<spectral_var_t>(std::move(params));
870881
}
871882
}
@@ -1049,7 +1060,7 @@ auto SContext::genMaterial(const CElementBSDF* bsdf, system::ISystem* debugFileW
10491060
{
10501061
spectral_var_t::SCreationParams<3> params = {};
10511062
params.knots.uvTransform = getParameters(params.knots.params,sigmaA);
1052-
params.getSemantics() = spectral_var_t::Semantics::Fixed3_SRGB;
1063+
params.getSemantics() = true_ir_t::ISpectralVariable::ESemantics::Fixed3_SRGB;
10531064
beer->perpTransmittance = frontPool.emplace<spectral_var_t>(std::move(params));
10541065
}
10551066
fillCoatingLayer(coating,_bsdf->coating,_bsdf->type==CElementBSDF::ROUGHCOATING,beerH);
@@ -1172,7 +1183,7 @@ auto SContext::genMaterial(const CElementBSDF* bsdf, system::ISystem* debugFileW
11721183
auto* const root = frontPool.deref(rootH);
11731184
root->debugInfo = frontPool.emplace<frontend_ir_t::CDebugInfo>(debugName);
11741185

1175-
const bool success = frontIR->addMaterial(rootH,inner.params.logger);
1186+
const bool success = frontIR->valid(rootH,inner.params.logger);
11761187
if (!success)
11771188
{
11781189
logger.log("Failed to add Material for %s",LoggerError,debugName.c_str());
@@ -1281,8 +1292,7 @@ SContext::SContext(
12811292
) : inner(_ctx), override_(_override), meta(_metadata)
12821293
//,ir(core::make_smart_refctd_ptr<asset::material_compiler::IR>()), frontend(this)
12831294
{
1284-
auto materialPool = material_compiler3::CTrueIR::create({.composed={.blockSizeKBLog2=4}});
1285-
scene = ICPUScene::create(core::smart_refctd_ptr(materialPool)); // TODO: feed it max shapes per group
1295+
scene = ICPUScene::create(material_compiler3::CTrueIR::create({.composed={.blockSizeKBLog2=4}})); // TODO: feed it max shapes per group
12861296
//
12871297
{
12881298
frontIR = frontend_ir_t::create({.composed={.blockSizeKBLog2=4}});
@@ -1313,7 +1323,7 @@ SContext::SContext(
13131323
mul->lhs = frontPool.emplace<frontend_ir_t::COrenNayar>();
13141324
spectral_var_t::SCreationParams<3> params = {};
13151325
MitsubaLoader::getParameters<3>(params.knots.params,{1.f,0.f,1.f});
1316-
params.getSemantics() = spectral_var_t::Semantics::Fixed3_SRGB;
1326+
params.getSemantics() = true_ir_t::ISpectralVariable::ESemantics::Fixed3_SRGB;
13171327
mul->rhs = frontPool.emplace<spectral_var_t>(std::move(params));
13181328
}
13191329
errorBRDF = mulH;

0 commit comments

Comments
 (0)