Skip to content

Commit 2d3719f

Browse files
fix the last thing in the material frontend
1 parent 2b34047 commit 2d3719f

4 files changed

Lines changed: 21 additions & 6 deletions

File tree

examples_tests

include/nbl/asset/material_compiler3/CFrontendIR.h

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -650,7 +650,7 @@ class CFrontendIR final : public CNodePool
650650
//
651651
// We actually allow you to use different reflectance nodes R_u and R_b, the NDFs of both layers remain the same but Reflectance Functions differ.
652652
// Note that e.g. using different Etas for the Fresnel used for the top and bottom reflectance will result in a compound Fresnel!=1.0
653-
// meaning that in such case you can no longer optimize the BTDF contributor into a DeltaTransmission but need a CookTorrance with
653+
// meaning that in such case you can no longer optimize the BTDF contributor into a DeltaTransmission but need a zero-roughness CookTorrance with
654654
// an Eta equal to the ratio of the first Eta over the second Eta (note that when they're equal the ratio is 1 which turns into Delta Trans).
655655
// This will require you to make an AST that "seems wrong" that is where neither of the Etas of the CFresnel nodes match the Cook Torrance one.
656656
//
@@ -755,7 +755,16 @@ class CFrontendIR final : public CNodePool
755755
// - Delta Reflection -> Any Cook Torrance BxDF with roughness=0 attached as BRDF
756756
// - Smooth Conductor -> above multiplied with Conductor-Fresnel
757757
// - Smooth Dielectric -> Any Cook Torrance BxDF with roughness=0 attached as BRDF on both sides of a Layer and BTDF multiplied with Dielectric-Fresnel (no imaginary component)
758-
// - Thindielectric Correlated -> Any Cook Torrance BxDF multiplied with Dielectric-Fresnel, then Delta Transmission as BTDF with fresnels
758+
// - Thindielectric Correlated -> Cook Torrance BxDF multiplied with Dielectric-Fresnel as top BRDF and its reciprocal as the bottom, then Delta Transmission as BTDF with fresnels of similar Eta
759+
// - Thindielectric Uncorrelated -> BRDF and BTDF same as above, no bottom BRDF, then another layer with delta transmission BTDF
760+
// For Smooth dielectrics it makes sense because fresnel of the interface is the same (microfacet equals macro surface normal, no confusion)
761+
// For Rough its a little more complicated, but using the same BTDF still makes sense.
762+
// Why? Because you enter all microfacets at once with a ray packet, and because their backfaces are correlated you don't refract.
763+
// If we then assume that they're quite big in relation to the thickness, most of the Total Internal Reflection stays within the same microfacet slab.
764+
// So for a single microfacet we have the thindielectric infinite TIR equation with `R_u = (1-Fresnel(VdotH))` and `R_b = (1-Fresnel(-LdotH))`,
765+
// which when convolved with the VNDF (integral of complete TIR equation over all H) can be approximated by substitution of `...dotH` with `...dotN`.
766+
// It also wouldn't matter if we dictate each slab have uniform perpendicular or geometric normal thickness, as the VNDF keeps projected surface area proportional to microfacet angle.
767+
// So the average VdotH or LdotH are equal to NdotV and NdotL respectively, which doesn't guarantee average `inversesqrt(1-VdotH*VdotH)` equals `inversesqrt(1-NdotV*NdotV)` but difference is small.
759768
// - Plastic -> Similar to layering the above over Diffuse BRDF, its of uttmost importance that the BTDF is Delta Transmission.
760769
class CDeltaTransmission final : public IBxDF
761770
{
@@ -912,9 +921,10 @@ class CFrontendIR final : public CNodePool
912921
// Some things we can't check such as the compatibility of the BTDF with the BRDF (matching indices of refraction, etc.)
913922
NBL_API2 bool valid(const typed_pointer_type<const CLayer> rootHandle, system::logger_opt_ptr logger) const;
914923

915-
// Each material comes down to this, YOU MUST NOT MODIFY THE NODES AFTER ADDING THEIR PARENT TO THE ROOT NODES!
916-
// TODO: shall we copy and hand out a new handle?
917924
inline std::span<const typed_pointer_type<const CLayer>> getMaterials() {return m_rootNodes;}
925+
926+
// Each material comes down to this, YOU MUST NOT MODIFY THE NODES AFTER ADDING THEIR PARENT TO THE ROOT NODES!
927+
// TODO: shall we copy and hand out a new handle? Allow RootNode from a foreign const pool
918928
inline bool addMaterial(const typed_pointer_type<const CLayer> rootNode, system::logger_opt_ptr logger)
919929
{
920930
if (valid(rootNode,logger))

src/nbl/asset/material_compiler3/CFrontendIR.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ bool CFrontendIR::CBeer::invalid(const SInvalidCheckArgs& args) const
3232
args.logger.log("Perpendicular Transparency node of correct type must be attached, but is %u of type %s",ELL_ERROR,perpTransmittance,args.pool->getTypeName(perpTransmittance).data());
3333
return true;
3434
}
35-
if (const auto* const thick=args.pool->getObjectPool().deref(thickness); !thick || thick->getKnotCount()!=0)
35+
if (const auto* const thick=args.pool->getObjectPool().deref(thickness); !thick || thick->getKnotCount()!=1)
3636
{
3737
args.logger.log("Monochromatic Thickness node must be attached, but is %u of type %s",ELL_ERROR,thickness,args.pool->getTypeName(thickness).data());
3838
return true;

src/nbl/ext/MitsubaLoader/CMitsubaLoader.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -897,9 +897,14 @@ auto SContext::genMaterial(const CElementBSDF* bsdf, system::ISystem* debugFileW
897897
mul->lhs = deltaTransmission._const_cast();
898898
mul->rhs = thinInfiniteScatterH;
899899
}
900+
#ifdef UNCORRELLATED
900901
// We need to attach the other glass interface as another layer because the Etas need to be reciprocated because the interactions are flipped
901902
leaf->coated = frontIR->reverse(retval);
903+
frontPool.deref(leaf->coated)->btdf = deltaTransmission._const_cast();
902904
// we're rethreading the fresnel here, but needed to be careful to not apply thindielectric scatter correction and volumetric absorption / Mitsuba extra factor twice
905+
#else
906+
leaf->brdfBottom = frontIR->reciprocate(brdfH)._const_cast();
907+
#endif
903908
btdfH = correctedTransmissionH;
904909
}
905910
else

0 commit comments

Comments
 (0)