Skip to content

Commit 2b34047

Browse files
I don't want to admit how long it took me to debug this one
1 parent 011e7f0 commit 2b34047

2 files changed

Lines changed: 67 additions & 98 deletions

File tree

src/nbl/ext/MitsubaLoader/CMitsubaLoader.cpp

Lines changed: 66 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -419,59 +419,69 @@ parameter_t SContext::getTexture(const CElementTexture* const rootTex, hlsl::flo
419419
SAssetBundle viewBundle = interm_getImageViewInHierarchy(tex->bitmap.filename,/*ICPUScene::MATERIAL_IMAGES_HIERARCHY_LEVELS_BELOW*/1);
420420
if (auto contents=viewBundle.getContents(); !contents.empty())
421421
{
422-
retval.view = IAsset::castDown<const ICPUImageView>(*contents.begin());
422+
auto view = IAsset::castDown<const ICPUImageView>(*contents.begin());
423+
const auto format = view->getCreationParameters().format;
423424
if (bitmap.channel!=CElementTexture::Bitmap::CHANNEL::INVALID)
424425
retval.viewChannel = bitmap.channel-CElementTexture::Bitmap::CHANNEL::R;
425-
// get sampler parameters
426-
using tex_clamp_e = asset::ISampler::E_TEXTURE_CLAMP;
427-
auto getWrapMode = [](CElementTexture::Bitmap::WRAP_MODE mode)
426+
if (retval.viewChannel<asset::getFormatChannelCount(format))
428427
{
429-
switch (mode)
428+
retval.view = std::move(view);
429+
// get sampler parameters
430+
using tex_clamp_e = asset::ISampler::E_TEXTURE_CLAMP;
431+
auto getWrapMode = [](CElementTexture::Bitmap::WRAP_MODE mode)
430432
{
431-
case CElementTexture::Bitmap::WRAP_MODE::CLAMP:
432-
return tex_clamp_e::ETC_CLAMP_TO_EDGE;
433-
break;
434-
case CElementTexture::Bitmap::WRAP_MODE::MIRROR:
435-
return tex_clamp_e::ETC_MIRROR;
436-
break;
437-
case CElementTexture::Bitmap::WRAP_MODE::ONE:
438-
assert(false); // TODO : replace whole texture?
439-
break;
440-
case CElementTexture::Bitmap::WRAP_MODE::ZERO:
441-
assert(false); // TODO : replace whole texture?
433+
switch (mode)
434+
{
435+
case CElementTexture::Bitmap::WRAP_MODE::CLAMP:
436+
return tex_clamp_e::ETC_CLAMP_TO_EDGE;
437+
break;
438+
case CElementTexture::Bitmap::WRAP_MODE::MIRROR:
439+
return tex_clamp_e::ETC_MIRROR;
440+
break;
441+
case CElementTexture::Bitmap::WRAP_MODE::ONE:
442+
assert(false); // TODO : replace whole texture?
443+
break;
444+
case CElementTexture::Bitmap::WRAP_MODE::ZERO:
445+
assert(false); // TODO : replace whole texture?
446+
break;
447+
default:
448+
break;
449+
}
450+
return tex_clamp_e::ETC_REPEAT;
451+
};
452+
auto& params = retval.sampler;
453+
params.TextureWrapU = getWrapMode(bitmap.wrapModeU);
454+
params.TextureWrapV = getWrapMode(bitmap.wrapModeV);
455+
switch (bitmap.filterType)
456+
{
457+
case CElementTexture::Bitmap::FILTER_TYPE::EWA:
458+
[[fallthrough]]; // we dont support this fancy stuff
459+
case CElementTexture::Bitmap::FILTER_TYPE::TRILINEAR:
460+
params.MinFilter = ISampler::ETF_LINEAR;
461+
params.MaxFilter = ISampler::ETF_LINEAR;
462+
params.MipmapMode = ISampler::ESMM_LINEAR;
442463
break;
443464
default:
465+
params.MinFilter = ISampler::ETF_NEAREST;
466+
params.MaxFilter = ISampler::ETF_NEAREST;
467+
params.MipmapMode = ISampler::ESMM_NEAREST;
444468
break;
445469
}
446-
return tex_clamp_e::ETC_REPEAT;
447-
};
448-
auto& params = retval.sampler;
449-
params.TextureWrapU = getWrapMode(bitmap.wrapModeU);
450-
params.TextureWrapV = getWrapMode(bitmap.wrapModeV);
451-
switch (bitmap.filterType)
452-
{
453-
case CElementTexture::Bitmap::FILTER_TYPE::EWA:
454-
[[fallthrough]]; // we dont support this fancy stuff
455-
case CElementTexture::Bitmap::FILTER_TYPE::TRILINEAR:
456-
params.MinFilter = ISampler::ETF_LINEAR;
457-
params.MaxFilter = ISampler::ETF_LINEAR;
458-
params.MipmapMode = ISampler::ESMM_LINEAR;
459-
break;
460-
default:
461-
params.MinFilter = ISampler::ETF_NEAREST;
462-
params.MaxFilter = ISampler::ETF_NEAREST;
463-
params.MipmapMode = ISampler::ESMM_NEAREST;
464-
break;
470+
params.AnisotropicFilter = core::max(hlsl::findMSB<uint32_t>(bitmap.maxAnisotropy),1u);
471+
// TODO: embed the gamma in the material compiler Frontend
472+
// or adjust gamma on pixels (painful and long process)
473+
//assert(std::isnan(bitmap.gamma));
474+
auto& transform = *outUvTransform;
475+
transform[0][0] = bitmap.uscale;
476+
transform[0][2] = bitmap.uoffset;
477+
transform[1][1] = bitmap.vscale;
478+
transform[1][2] = bitmap.voffset;
465479
}
466-
params.AnisotropicFilter = core::max(hlsl::findMSB<uint32_t>(bitmap.maxAnisotropy),1u);
467-
// TODO: embed the gamma in the material compiler Frontend
468-
// or adjust gamma on pixels (painful and long process)
469-
//assert(std::isnan(bitmap.gamma));
470-
auto& transform = *outUvTransform;
471-
transform[0][0] = bitmap.uscale;
472-
transform[0][2] = bitmap.uoffset;
473-
transform[1][1] = bitmap.vscale;
474-
transform[1][2] = bitmap.voffset;
480+
else
481+
inner.params.logger.log(
482+
"Failed to parse CElementTexture bitmap for %p with id %s from path \"%s\", channel swizzle %s requested out of range of format %s",
483+
LoggerError,tex,tex ? tex->id.c_str():"",tex->bitmap.filename,system::to_string(bitmap.channel),system::to_string(format)
484+
);
475485
}
476486
else
477487
inner.params.logger.log("Failed to load bitmap texture for %p with id %s from path \"%s\"",LoggerError,tex,tex ? tex->id.c_str():"",tex->bitmap.filename);
@@ -501,10 +511,19 @@ hlsl::float32_t2x3 SContext::getParameters(const std::span<parameter_t,3> out, c
501511
if (src.texture)
502512
{
503513
const auto param = getTexture(src.texture,&retval);
514+
const auto formatChannelCount = getFormatChannelCount(param.view ? param.view->getCreationParameters().format:E_FORMAT::EF_UNKNOWN);
515+
if (src.texture->bitmap.channel==CElementTexture::Bitmap::CHANNEL::INVALID && formatChannelCount>1 && out.size()>formatChannelCount)
516+
{
517+
for (auto c=0; c<out.size(); c++)
518+
out[c].scale = std::numeric_limits<decltype(out[c].scale)>::signaling_NaN();
519+
return hlsl::math::linalg::diagonal<hlsl::float32_t2x3>(0.f);;
520+
}
504521
for (auto c=0; c<out.size(); c++)
505522
{
506523
out[c].scale = param.scale;
507-
out[c].viewChannel = param.viewChannel+c;
524+
out[c].viewChannel = param.viewChannel;
525+
if (formatChannelCount>1 && src.texture->bitmap.channel==CElementTexture::Bitmap::CHANNEL::INVALID)
526+
out[c].viewChannel += c;
508527
out[c].view = param.view;
509528
out[c].sampler = param.sampler;
510529
}
@@ -535,7 +554,8 @@ hlsl::float32_t2x3 SContext::getParameters(const std::span<parameter_t> out, con
535554
for (auto c=0; c<out.size(); c++)
536555
{
537556
out[c].scale = param.scale;
538-
out[c].viewChannel = param.viewChannel+c;
557+
// assign same channel to everything because the `src` is monochrome
558+
out[c].viewChannel = param.viewChannel;
539559
out[c].view = param.view;
540560
out[c].sampler = param.sampler;
541561
}

src/nbl/ext/MitsubaLoader/CMitsubaMaterialCompilerFrontend.cpp

Lines changed: 1 addition & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -163,55 +163,4 @@ CMitsubaMaterialCompilerFrontend::tex_ass_type CMitsubaMaterialCompilerFrontend:
163163
std::tie(node->texture.image,node->texture.sampler,node->texture.scale) =
164164
getTexture(_bsdf->bumpmap.texture,_bsdf->bumpmap.wasNormal ? EIVS_NORMAL_MAP:EIVS_BUMP_MAP);
165165
}
166-
break;
167-
case CElementBSDF::COATING:
168-
case CElementBSDF::ROUGHCOATING:
169-
{
170-
ir_node = ir->allocNode<IR::CMicrofacetCoatingBSDFNode>();
171-
ir_node->children.count = 1u;
172-
173-
const float eta = _bsdf->dielectric.intIOR/_bsdf->dielectric.extIOR;
174-
175-
auto* node = static_cast<IR::CMicrofacetCoatingBSDFNode*>(ir_node);
176-
177-
const float thickness = _bsdf->coating.thickness;
178-
getSpectrumOrTexture(_bsdf->coating.sigmaA, node->thicknessSigmaA);
179-
if (node->thicknessSigmaA.isConstant())
180-
node->thicknessSigmaA.constant *= thickness;
181-
else
182-
node->thicknessSigmaA.texture.scale *= thickness;
183-
184-
node->eta = IR::INode::color_t(eta);
185-
node->shadowing = IR::CMicrofacetCoatingBSDFNode::EST_SMITH;
186-
if (type == CElementBSDF::ROUGHCOATING)
187-
{
188-
node->ndf = ndfMap[_bsdf->coating.distribution];
189-
getFloatOrTexture(_bsdf->coating.alphaU, node->alpha_u);
190-
if (node->ndf == IR::CMicrofacetSpecularBSDFNode::ENDF_ASHIKHMIN_SHIRLEY)
191-
getFloatOrTexture(_bsdf->coating.alphaV, node->alpha_v);
192-
else
193-
node->alpha_v = node->alpha_u;
194-
}
195-
else
196-
{
197-
node->setSmooth();
198-
}
199-
}
200-
break;
201-
202-
203-
case CElementBSDF::MIXTURE_BSDF:
204-
{
205-
ir_node = ir->allocNode<IR::CBSDFMixNode>();
206-
auto* node = static_cast<IR::CBSDFMixNode*>(ir_node);
207-
const size_t cnt = _bsdf->mixturebsdf.childCount;
208-
ir_node->children.count = cnt;
209-
const auto* weightIt = _bsdf->mixturebsdf.weights;
210-
for (size_t i=0u; i<cnt; i++)
211-
node->weights[i] = *(weightIt++);
212-
}
213-
break;
214-
}
215-
216-
return ir_node;
217-
}
166+
break;

0 commit comments

Comments
 (0)