Skip to content

Commit edb8750

Browse files
starting making the factor (SpectralVariable from CTrueIR by CFrontendIR) but realize its all just duplicate code
1 parent 0b28b76 commit edb8750

3 files changed

Lines changed: 76 additions & 4 deletions

File tree

include/nbl/asset/material_compiler3/CFrontendIR.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -361,6 +361,9 @@ class CFrontendIR final : public CNodePool
361361
private:
362362
SCreationParams<1>* pWonky() {return reinterpret_cast<SCreationParams<1>*>(this+1);}
363363
const SCreationParams<1>* pWonky() const {return reinterpret_cast<const SCreationParams<1>*>(this+1);}
364+
365+
friend class CFrontendIR;
366+
NBL_API2 CTrueIR::typed_pointer_type<CTrueIR::ISpectralVariable> createIRNode(const CFrontendIR* ast, CTrueIR* ir) const;
364367
};
365368
//
366369
class IUnaryOp : public obj_pool_type::INonTrivial, public IExprNode

include/nbl/asset/material_compiler3/CTrueIR.h

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -434,6 +434,11 @@ class CTrueIR : public CNodePool // TODO: turn into an asset!
434434
class ISpectralVariable : public IFactorLeaf
435435
{
436436
public:
437+
virtual uint8_t getSpectralBins() const = 0;
438+
439+
virtual SParameter* getParameter(const uint8_t i) = 0;
440+
inline const SParameter* getParameter(const uint8_t i) const {return const_cast<const SParameter*>(const_cast<ISpectralVariable*>(this)->getParameter(i));}
441+
437442
enum class ESemantics : uint8_t
438443
{
439444
NoneUndefined = 0,
@@ -447,6 +452,13 @@ class CTrueIR : public CNodePool // TODO: turn into an asset!
447452
// PairsLinear = 5, // linear interpolation
448453
// PairsLogLinear = 5, // linear interpolation in wavelenght log space
449454
};
455+
inline ESemantics getSemantics() const
456+
{
457+
if (getSpectralBins()>1)
458+
return static_cast<ESemantics>(getParameter(1)->padding[0]);
459+
else
460+
return ESemantics::NoneUndefined;
461+
}
450462
};
451463
template<uint8_t SpectralBins>
452464
class CSpectralVariable final : public obj_pool_type::INonTrivial, public ISpectralVariable
@@ -456,7 +468,7 @@ class CTrueIR : public CNodePool // TODO: turn into an asset!
456468
hasher << paramSet;
457469
if constexpr (SpectralBins>1)
458470
{
459-
const ESemantics semantics = getSemantics<SpectralBins>();
471+
const ESemantics semantics = getSemantics();
460472
if (semantics!=ESemantics::NoneUndefined)
461473
return false;
462474
hasher << semantics;
@@ -465,6 +477,7 @@ class CTrueIR : public CNodePool // TODO: turn into an asset!
465477
}
466478

467479
public:
480+
// TODO: maybe undo this?
468481
inline EFinalType getFinalType() const override
469482
{
470483
static_assert(1<=SpectralBins && SpectralBins<=4);
@@ -475,11 +488,12 @@ class CTrueIR : public CNodePool // TODO: turn into an asset!
475488
inline const std::string_view getTypeName() const override {return TYPE_NAME_STR(CSpectralVariable<SpectralBins>);}
476489

477490
//
478-
inline uint8_t getSpectralBins() const {return SpectralBins;}
491+
inline uint8_t getSpectralBins() const override {return SpectralBins;}
492+
493+
//
494+
inline SParameter* getParameter(const uint8_t i) {return paramSet.params+i;}
479495

480496
//
481-
template<uint8_t N> requires (N>1 && N==SpectralBins)
482-
inline ESemantics getSemantics() const {return static_cast<ESemantics>(paramSet.params[1].padding[0]);}
483497
template<uint8_t N> requires (N>1 && N==SpectralBins)
484498
inline void setSemantics(const ESemantics value) {paramSet.params[1].padding[0] = static_cast<uint8_t>(value);}
485499

src/nbl/asset/material_compiler3/CFrontendIR.cpp

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -753,6 +753,7 @@ auto CFrontendIR::SAdd2IRSession::makeContributors(const CFrontendIR::typed_poin
753753
assert(contributorStack.empty());
754754
exprStack.push_back({.nodeH=bxdfRootH});
755755
CTrueIR::typed_pointer_type<CTrueIR::CContributorSum> tailH = {};
756+
// the mul node gets visited after children are made
756757
while (!exprStack.empty())
757758
{
758759
auto& entry = exprStack.back();
@@ -814,6 +815,11 @@ auto CFrontendIR::SAdd2IRSession::makeContributors(const CFrontendIR::typed_poin
814815
// do stuff now
815816
switch (astExprType)
816817
{
818+
case ast_expr_type_e::Mul:
819+
{
820+
// silently skip
821+
break;
822+
}
817823
case ast_expr_type_e::Add:
818824
{
819825
// we visited the leftmost subtrees first so this is the right order
@@ -837,6 +843,26 @@ auto CFrontendIR::SAdd2IRSession::makeContributors(const CFrontendIR::typed_poin
837843
mulChain.resize(entry.mulChainLen);
838844
break;
839845
}
846+
case ast_expr_type_e::SpectralVariable:
847+
{
848+
const auto varH = static_cast<const CSpectralVariable*>(node)->createIRNode(srcAST,tmpIR.get());
849+
auto* const var = irPool.deref(varH);
850+
if (!var)
851+
{
852+
args.logger.log("Failed to create the Spectral Variable.",ELL_ERROR);
853+
return (headH=errorRetval);
854+
}
855+
auto& factor = mulChain.emplace_back();
856+
factor.handle = varH;
857+
const auto channels = var->getSpectralBins();
858+
factor.monochrome = channels>1;
859+
for (uint8_t c=0; c<channels; c++)
860+
{
861+
if (!(var->getParameter(c)->scale>std::numeric_limits<float>::min()))
862+
factor.liveSpectralChannels &= ~(0b1<<c);
863+
}
864+
break;
865+
}
840866
default:
841867
{
842868
args.logger.log("Unsupported AST Expression type \"%s\"",ELL_ERROR,system::to_string(astExprType).c_str());
@@ -867,6 +893,35 @@ auto CFrontendIR::SAdd2IRSession::makeContributors(const CFrontendIR::typed_poin
867893
return headH;
868894
}
869895

896+
auto CFrontendIR::CSpectralVariable::createIRNode(const CFrontendIR* ast, CTrueIR* ir) const -> CTrueIR::typed_pointer_type<CTrueIR::ISpectralVariable>
897+
{
898+
return {};
899+
auto& irPool = ir->getObjectPool();
900+
uint8_t realCount = 1;
901+
for (uint8_t c=0; c<getKnotCount(); c++)
902+
if (*getParam(c)!=*getParam(0))
903+
realCount = 3;
904+
CTrueIR::typed_pointer_type<CTrueIR::ISpectralVariable> retval = {};
905+
// TODO: maybe make it wholly polymorphic like the AST ? Or even have both classes use the same struct ?
906+
switch (realCount)
907+
{
908+
case 1:
909+
{
910+
retval = irPool.emplace<CTrueIR::CSpectralVariable<1>>();
911+
break;
912+
}
913+
case 3:
914+
{
915+
retval = irPool.emplace<CTrueIR::CSpectralVariable<3>>();
916+
break;
917+
}
918+
default:
919+
break;
920+
}
921+
// ==
922+
return retval;
923+
}
924+
870925
//
871926
auto CFrontendIR::SAdd2IRSession::popContributor() -> CTrueIR::typed_pointer_type<const CTrueIR::CWeightedContributor>
872927
{

0 commit comments

Comments
 (0)