Skip to content

Commit 01de56c

Browse files
committed
Fixed a few things in mitsuba loader/material compiler
1 parent 8ae65e5 commit 01de56c

File tree

9 files changed

+74
-21
lines changed

9 files changed

+74
-21
lines changed

include/nbl/asset/material_compiler/CMaterialCompilerGLSLBackendCommon.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -641,8 +641,8 @@ class CMaterialCompilerGLSLBackendCommon
641641
vt->shrink();
642642

643643
bool success = true;
644-
for (commit_t& cm : pendingCommits)
645-
success &= commit(cm);
644+
//for (commit_t& cm : pendingCommits)
645+
// success &= commit(cm);
646646
pendingCommits.clear();
647647
return success;
648648
}

include/nbl/ext/MitsubaLoader/CElementBSDF.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -220,11 +220,11 @@ class CElementBSDF : public IElement
220220
size_t childCount = 0u;
221221
CElementBSDF* bsdf[MaxChildCount] = { nullptr };
222222
};
223-
struct AllCoating : RoughSpecularBase, TransmissiveBase, MetaBSDF
223+
struct AllCoating : MetaBSDF, RoughSpecularBase, TransmissiveBase
224224
{
225225
_NBL_STATIC_INLINE_CONSTEXPR size_t MaxChildCount = 1u;
226226

227-
AllCoating() : RoughSpecularBase(0.1f), TransmissiveBase("bk7","air"), MetaBSDF(), thickness(1.f), sigmaA(0.f) {}
227+
AllCoating() : MetaBSDF(), RoughSpecularBase(0.1f), TransmissiveBase("bk7","air"), thickness(1.f), sigmaA(0.f) {}
228228

229229
inline AllCoating& operator=(const AllCoating& other)
230230
{

include/nbl/ext/MitsubaLoader/CMitsubaMaterialCompilerFrontend.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,8 @@ class CMitsubaMaterialCompilerFrontend
3131
tex_ass_type getTexture(const CElementTexture* _element) const;
3232
tex_ass_type getTexture(const std::string& _key, const CElementTexture* _element, float _scale) const;
3333

34+
tex_ass_type getErrorTexture() const;
35+
3436
IRNode* createIRNode(asset::material_compiler::IR* ir, const CElementBSDF* _bsdf);
3537

3638
public:
@@ -40,7 +42,7 @@ class CMitsubaMaterialCompilerFrontend
4042
IRNode* back;
4143
};
4244

43-
CMitsubaMaterialCompilerFrontend(const SContext* _ctx) : m_loaderContext(_ctx) {}
45+
explicit CMitsubaMaterialCompilerFrontend(const SContext* _ctx) : m_loaderContext(_ctx) {}
4446

4547
front_and_back_t compileToIRTree(asset::material_compiler::IR* ir, const CElementBSDF* _bsdf);
4648
};

include/nbl/ext/MitsubaLoader/CMitsubaMetadata.h

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
#ifndef __NBL_C_MITSUBA_METADATA_H_INCLUDED__
66
#define __NBL_C_MITSUBA_METADATA_H_INCLUDED__
77

8+
#include "nbl/core/compile_config.h"
89
#include "nbl/asset/metadata/IAssetMetadata.h"
910

1011
#include "nbl/ext/MitsubaLoader/SContext.h"
@@ -67,6 +68,9 @@ class CMitsubaMetadata : public asset::IAssetMetadata
6768
CElementEmitter frontEmitter; // type is invalid if not used
6869
CElementEmitter backEmitter; // type is invalid if not used
6970
CMitsubaMaterialCompilerFrontend::front_and_back_t bsdf;
71+
#if defined(_NBL_DEBUG) || defined(_NBL_RELWITHDEBINFO)
72+
std::string bsdf_id;
73+
#endif
7074
};
7175

7276
core::SRange<const SInstanceAuxilaryData> m_instanceAuxData;
@@ -162,7 +166,14 @@ class CMitsubaMetadata : public asset::IAssetMetadata
162166
{
163167
auto& inst = it->second;
164168
(m_instanceStorageIt++)->worldTform = inst.tform;
165-
*(m_instanceAuxStorageIt++) = {inst.emitter.front,inst.emitter.back,inst.bsdf};
169+
*(m_instanceAuxStorageIt++) = {
170+
inst.emitter.front,
171+
inst.emitter.back,
172+
inst.bsdf
173+
#if defined(_NBL_DEBUG) || defined(_NBL_RELWITHDEBINFO)
174+
,inst.bsdf_id
175+
#endif
176+
};
166177
}
167178
meta->m_instances = { instanceStorageBegin,m_instanceStorageIt };
168179
meta->m_instanceAuxData = { instanceAuxStorageBegin,m_instanceAuxStorageIt };

include/nbl/macros.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,8 @@
6262
#if defined(_NBL_DEBUG) || defined(_NBL_RELWITHDEBINFO)
6363
// TODO even though it is defined in RWDI build, _DEBUG #define is not defined in msvc (in rwdi) so debug break is not triggered anyway
6464
// idk what about other compilers
65-
#define _NBL_DEBUG_BREAK_IF( _CONDITION_ ) _NBL_BREAK_IF(_CONDITION_)
65+
//#define _NBL_DEBUG_BREAK_IF( _CONDITION_ ) _NBL_BREAK_IF(_CONDITION_)
66+
#define _NBL_DEBUG_BREAK_IF( _CONDITION_ )
6667
#else
6768
#define _NBL_DEBUG_BREAK_IF( _CONDITION_ )
6869
#endif

src/nbl/asset/interchange/CImageLoaderTGA.cpp

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -105,10 +105,12 @@ bool CImageLoaderTGA::isALoadableFileFormat(io::IReadFile* _file) const
105105
// 16 bytes for "TRUEVISION-XFILE", 17th byte is '.', and the 18th byte contains '\0'.
106106
if (strncmp(footer.Signature, "TRUEVISION-XFILE.", 18u) != 0)
107107
{
108-
os::Printer::log("Invalid (non-TGA) file!", ELL_ERROR);
108+
//os::Printer::log("Invalid (non-TGA) file!", _file->getFileName().c_str(), ELL_ERROR);
109109
return false;
110110
}
111111

112+
// why did it even check gamma in isALoadableFileFormat ???
113+
/*
112114
float gamma;
113115
114116
if (footer.ExtensionOffset == 0)
@@ -133,6 +135,7 @@ bool CImageLoaderTGA::isALoadableFileFormat(io::IReadFile* _file) const
133135
// TODO - pass gamma to LoadAsset()?
134136
// Actually I think metadata will be in used here in near future
135137
}
138+
*/
136139

137140
_file->seek(prevPos);
138141

src/nbl/asset/material_compiler/CMaterialCompilerGLSLBackendCommon.cpp

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -691,7 +691,6 @@ std::pair<instr_t, const IR::INode*> CInterpreter::processSubtree(IR* ir, const
691691

692692
assert(node->children.count == 1u);
693693
auto* coated = const_cast<IR::INode*>(node->children[0]);
694-
out_next = IR::INode::createChildrenArray(coat, coated);
695694

696695
instr = instr_stream::OP_COATING;
697696

@@ -700,9 +699,19 @@ std::pair<instr_t, const IR::INode*> CInterpreter::processSubtree(IR* ir, const
700699
instr = instr_stream::OP_INVALID;
701700
const IR::CBSDFNode::E_TYPE coated_bxdf = static_cast<const IR::CBSDFNode*>(coated)->type;
702701
const bool is_coated_diffuse = (coated_bxdf == IR::CBSDFNode::ET_MICROFACET_DIFFUSE || coated_bxdf == IR::CBSDFNode::ET_MICROFACET_DIFFTRANS);
703-
assert(is_coated_diffuse);
702+
//assert(is_coated_diffuse);
703+
// we dont support coating over non-diffuse materials
704+
// so we ignore coating layer and process only the coated material
704705
if (!is_coated_diffuse)
705-
instr = instr_stream::OP_INVALID;
706+
{
707+
auto retval = processSubtree(ir, coated, out_next, cache);
708+
instr = retval.first;
709+
tree = retval.second;
710+
}
711+
else
712+
{
713+
out_next = IR::INode::createChildrenArray(coat, coated);
714+
}
706715
}
707716
break;
708717
case IR::CBSDFNode::ET_MICROFACET_DIELECTRIC:

src/nbl/ext/MitsubaLoader/CMitsubaLoader.cpp

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1221,6 +1221,9 @@ auto CMitsubaLoader::genBSDFtreeTraversal(SContext& ctx, const CElementBSDF* _bs
12211221
switch (bsdf->type)
12221222
{
12231223
case CElementBSDF::COATING:
1224+
for (uint32_t i = 0u; i < bsdf->coating.childCount; ++i)
1225+
stack.push(bsdf->coating.bsdf[i]);
1226+
break;
12241227
case CElementBSDF::ROUGHCOATING:
12251228
case CElementBSDF::BUMPMAP:
12261229
case CElementBSDF::BLEND_BSDF:
@@ -1427,9 +1430,9 @@ inline core::smart_refctd_ptr<asset::ICPUDescriptorSet> CMitsubaLoader::createDS
14271430

14281431
#ifdef DEBUG_MITSUBA_LOADER
14291432
//@Crisspl TODO No way how to fix it for reporting the `inst.bsdf_id`
1430-
//os::Printer::log("Debug print front BSDF with id = ", inst.bsdf_id, ELL_INFORMATION);
1431-
//ofile << "Debug print front BSDF with id = " << inst.bsdf_id << std::endl;
1432-
//_ctx.backend.debugPrint(ofile, streams, _compResult, &_ctx.backend_ctx);
1433+
os::Printer::log("Debug print front BSDF with id = ", inst.bsdf_id, ELL_INFORMATION);
1434+
ofile << "Debug print front BSDF with id = " << inst.bsdf_id << std::endl;
1435+
_ctx.backend.debugPrint(ofile, streams, _compResult, &_ctx.backend_ctx);
14331436
#endif
14341437
const auto emissive = inst.frontEmitter.type==CElementEmitter::AREA ? inst.frontEmitter.area.radiance:core::vectorSIMDf(0.f);
14351438
instData.material.front = impl_backendToGLSLStream(emissive,streams);
@@ -1441,9 +1444,9 @@ inline core::smart_refctd_ptr<asset::ICPUDescriptorSet> CMitsubaLoader::createDS
14411444

14421445
#ifdef DEBUG_MITSUBA_LOADER
14431446
//@Crisspl TODO No way how to fix it for reporting the `inst.bsdf_id`
1444-
//os::Printer::log("Debug print back BSDF with id = ", inst.bsdf_id, ELL_INFORMATION);
1445-
//ofile << "Debug print back BSDF with id = " << inst.bsdf_id << std::endl;
1446-
//_ctx.backend.debugPrint(ofile, streams, _compResult, &_ctx.backend_ctx);
1447+
os::Printer::log("Debug print back BSDF with id = ", inst.bsdf_id, ELL_INFORMATION);
1448+
ofile << "Debug print back BSDF with id = " << inst.bsdf_id << std::endl;
1449+
_ctx.backend.debugPrint(ofile, streams, _compResult, &_ctx.backend_ctx);
14471450
#endif
14481451
const auto emissive = inst.backEmitter.type==CElementEmitter::AREA ? inst.backEmitter.area.radiance:core::vectorSIMDf(0.f);
14491452
instData.material.back = impl_backendToGLSLStream(emissive,streams);

src/nbl/ext/MitsubaLoader/CMitsubaMaterialCompilerFrontend.cpp

Lines changed: 28 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,26 @@ namespace MitsubaLoader
7777
return { nullptr, nullptr, _scale };
7878
}
7979

80+
auto CMitsubaMaterialCompilerFrontend::getErrorTexture() const -> tex_ass_type
81+
{
82+
constexpr const char* ERR_TEX_CACHE_NAME = "nbl/builtin/image_view/dummy2d";
83+
constexpr const char* ERR_SMPLR_CACHE_NAME = "nbl/builtin/sampler/default";
84+
85+
asset::IAsset::E_TYPE types[2]{ asset::IAsset::ET_IMAGE_VIEW, asset::IAsset::ET_TERMINATING_ZERO };
86+
auto bundle = m_loaderContext->override_->findCachedAsset(ERR_TEX_CACHE_NAME, types, m_loaderContext->inner, 0u);
87+
assert(!bundle.getContents().empty()); // this shouldnt ever happen since ERR_TEX_CACHE_NAME is builtin asset
88+
89+
auto view = core::smart_refctd_ptr_static_cast<asset::ICPUImageView>(bundle.getContents().begin()[0]);
90+
91+
types[0] = asset::IAsset::ET_SAMPLER;
92+
auto sbundle = m_loaderContext->override_->findCachedAsset(ERR_SMPLR_CACHE_NAME, types, m_loaderContext->inner, 0u);
93+
assert(!sbundle.getContents().empty()); // this shouldnt ever happen since ERR_SMPLR_CACHE_NAME is builtin asset
94+
95+
auto smplr = core::smart_refctd_ptr_static_cast<asset::ICPUSampler>(sbundle.getContents().begin()[0]);
96+
97+
return { view, smplr, 1.f };
98+
}
99+
80100
auto CMitsubaMaterialCompilerFrontend::createIRNode(asset::material_compiler::IR* ir, const CElementBSDF* _bsdf) -> IRNode*
81101
{
82102
using namespace asset;
@@ -352,7 +372,11 @@ auto CMitsubaMaterialCompilerFrontend::compileToIRTree(asset::material_compiler:
352372
if (tex.image)
353373
dst = std::move(tex);
354374
else
355-
dst = IR::INode::color_t(1.f, 0.f, 0.f); // red in case of no texture
375+
{
376+
// error texture in case of not-found texture
377+
std::tie(tex.image, tex.sampler, tex.scale) = getErrorTexture();
378+
dst = std::move(tex);
379+
}
356380
}
357381
else dst = src.value.vvalue;
358382
};
@@ -387,11 +411,11 @@ auto CMitsubaMaterialCompilerFrontend::compileToIRTree(asset::material_compiler:
387411

388412
if (parent.bsdf->isMeta())
389413
{
390-
const auto& meta = parent.bsdf->meta_common;
391-
for (uint32_t i = 0u; i < meta.childCount; ++i)
414+
const uint32_t child_count = (parent.bsdf->type == CElementBSDF::COATING) ? parent.bsdf->coating.childCount : parent.bsdf->meta_common.childCount;
415+
for (uint32_t i = 0u; i < child_count; ++i)
392416
{
393417
SNode child_node;
394-
child_node.bsdf = meta.bsdf[i];
418+
child_node.bsdf = (parent.bsdf->type == CElementBSDF::COATING) ? parent.bsdf->coating.bsdf[i] : parent.bsdf->meta_common.bsdf[i];
395419
child_node.parent_ix = parent.type() == CElementBSDF::TWO_SIDED ? parent.parent_ix : bfs.size();
396420
child_node.twosided = (child_node.type() == CElementBSDF::TWO_SIDED) || parent.twosided;
397421
child_node.child_num = (parent.type() == CElementBSDF::TWO_SIDED) ? parent.child_num : i;

0 commit comments

Comments
 (0)