Skip to content

Commit ed0766d

Browse files
fix most of the bugs and make all examples_tests run
1 parent b86adc5 commit ed0766d

8 files changed

Lines changed: 80 additions & 66 deletions

File tree

include/nbl/asset/material_compiler3/CFrontendIR.h

Lines changed: 18 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,7 @@ namespace nbl::asset::material_compiler3
7777
// polygonization. Using PN-Triangles/displacement would be the optimal solution here.
7878
class CFrontendIR final : public CNodePool
7979
{
80+
using block_allocator_type = CNodePool::obj_pool_type::block_allocator_type;
8081
template<typename T>
8182
using _typed_pointer_type = CNodePool::obj_pool_type::mem_pool_type::typed_pointer_type<T>;
8283

@@ -124,7 +125,7 @@ class CFrontendIR final : public CNodePool
124125
}
125126
inline bool operator==(const SParameter& other) const {return !operator!=(other);}
126127

127-
NBL_API void printDot(std::ostringstream& sstr, const core::string& selfID) const;
128+
NBL_API2 void printDot(std::ostringstream& sstr, const core::string& selfID) const;
128129

129130
// at this stage we store the multipliers in highest precision
130131
float scale = std::numeric_limits<float>::infinity();
@@ -435,8 +436,8 @@ class CFrontendIR final : public CNodePool
435436
return false;
436437
}
437438

438-
NBL_API core::string getLabelSuffix() const override;
439-
NBL_API void printDot(std::ostringstream& sstr, const core::string& selfID) const override;
439+
NBL_API2 core::string getLabelSuffix() const override;
440+
NBL_API2 void printDot(std::ostringstream& sstr, const core::string& selfID) const override;
440441

441442
private:
442443
SCreationParams<1>* pWonky() {return reinterpret_cast<SCreationParams<1>*>(this+1);}
@@ -517,8 +518,8 @@ class CFrontendIR final : public CNodePool
517518

518519
protected:
519520
inline typed_pointer_type<IExprNode> getChildHandle_impl(const uint8_t ix) const override {return {};}
520-
NBL_API bool invalid(const SInvalidCheckArgs& args) const override;
521-
NBL_API void printDot(std::ostringstream& sstr, const core::string& selfID) const override;
521+
NBL_API2 bool invalid(const SInvalidCheckArgs& args) const override;
522+
NBL_API2 void printDot(std::ostringstream& sstr, const core::string& selfID) const override;
522523
};
523524
//! Special nodes meant to be used as `CMul::rhs`, their behaviour depends on the IContributor in its MUL node relative subgraph.
524525
//! If you use a different contributor node type or normal for shading, these nodes get split and duplicated into two in our Final IR.
@@ -551,7 +552,7 @@ class CFrontendIR final : public CNodePool
551552
inline typed_pointer_type<IExprNode> getChildHandle_impl(const uint8_t ix) const override {return perpTransmittance;}
552553

553554
inline std::string_view getChildName_impl(const uint8_t ix) const override {return "Perpendicular\\nTransmittance";}
554-
NBL_API bool invalid(const SInvalidCheckArgs& args) const override;
555+
NBL_API2 bool invalid(const SInvalidCheckArgs& args) const override;
555556
};
556557
// The "oriented" in the Etas means from frontface to backface, so there's no need to reciprocate them when creating matching BTDF for BRDF
557558
// @kept_secret TODO: Thin Film Interference Fresnel
@@ -572,9 +573,9 @@ class CFrontendIR final : public CNodePool
572573

573574
protected:
574575
inline typed_pointer_type<IExprNode> getChildHandle_impl(const uint8_t ix) const override {return ix ? orientedImagEta:orientedRealEta;}
575-
NBL_API bool invalid(const SInvalidCheckArgs& args) const override;
576+
NBL_API2 bool invalid(const SInvalidCheckArgs& args) const override;
576577
inline std::string_view getChildName_impl(const uint8_t ix) const override {return ix ? "Real":"Imaginary";}
577-
NBL_API void printDot(std::ostringstream& sstr, const core::string& selfID) const override;
578+
NBL_API2 void printDot(std::ostringstream& sstr, const core::string& selfID) const override;
578579
};
579580
// Compute Inifinite Scatter and extinction between two parallel infinite planes.
580581
// It's a specialization of what would be a layer of two identical smooth BRDF and BTDF with arbitrary Fresnel function and beer's
@@ -595,7 +596,7 @@ class CFrontendIR final : public CNodePool
595596
{
596597
protected:
597598
inline typed_pointer_type<IExprNode> getChildHandle_impl(const uint8_t ix) const override final {return ix ? (ix>1 ? reflectanceBottom:extinction):reflectanceTop;}
598-
NBL_API bool invalid(const SInvalidCheckArgs& args) const override;
599+
NBL_API2 bool invalid(const SInvalidCheckArgs& args) const override;
599600

600601
inline std::string_view getChildName_impl(const uint8_t ix) const override {return ix ? (ix>1 ? "reflectanceBottom":"extinction"):"reflectanceTop";}
601602

@@ -649,7 +650,7 @@ class CFrontendIR final : public CNodePool
649650
// whether the derivative map and roughness is constant regardless of UV-space texture stretching
650651
inline bool stretchInvariant() const {return !(abs(hlsl::determinant(reference))>std::numeric_limits<float>::min());}
651652

652-
NBL_API void printDot(std::ostringstream& sstr, const core::string& selfID) const;
653+
NBL_API2 void printDot(std::ostringstream& sstr, const core::string& selfID) const;
653654

654655
// Ignored if not invertible, otherwise its the reference "stretch" (UV derivatives) at which identity roughness and normalmapping occurs
655656
hlsl::float32_t2x2 reference = hlsl::float32_t2x2(0,0,0,0);
@@ -687,8 +688,8 @@ class CFrontendIR final : public CNodePool
687688

688689
protected:
689690
inline _typed_pointer_type<IExprNode> getChildHandle_impl(const uint8_t ix) const override {return {};}
690-
NBL_API bool invalid(const SInvalidCheckArgs& args) const override;
691-
NBL_API void printDot(std::ostringstream& sstr, const core::string& selfID) const override;
691+
NBL_API2 bool invalid(const SInvalidCheckArgs& args) const override;
692+
NBL_API2 void printDot(std::ostringstream& sstr, const core::string& selfID) const override;
692693
};
693694
// Supports anisotropy for all models
694695
class CCookTorrance final : public IBxDF
@@ -717,11 +718,11 @@ class CFrontendIR final : public CNodePool
717718

718719
protected:
719720
inline typed_pointer_type<IExprNode> getChildHandle_impl(const uint8_t ix) const override {return orientedRealEta;}
720-
NBL_API bool invalid(const SInvalidCheckArgs& args) const override;
721+
NBL_API2 bool invalid(const SInvalidCheckArgs& args) const override;
721722

722723
inline core::string getLabelSuffix() const override {return ndf!=NDF::GGX ? "\\nNDF = Beckmann":"\\nNDF = GGX";}
723724
inline std::string_view getChildName_impl(const uint8_t ix) const override {return "Oriented η";}
724-
NBL_API void printDot(std::ostringstream& sstr, const core::string& selfID) const override;
725+
NBL_API2 void printDot(std::ostringstream& sstr, const core::string& selfID) const override;
725726
};
726727
#undef TYPE_NAME_STR
727728

@@ -740,16 +741,16 @@ class CFrontendIR final : public CNodePool
740741
}
741742

742743
// To quickly make a matching backface material from a frontface or vice versa
743-
NBL_API typed_pointer_type<IExprNode> reciprocate(const typed_pointer_type<const IExprNode> other);
744-
NBL_API typed_pointer_type<CFresnel> createNamedFresnel(const std::string_view name);
744+
NBL_API2 typed_pointer_type<IExprNode> reciprocate(const typed_pointer_type<const IExprNode> other);
745+
NBL_API2 typed_pointer_type<CFresnel> createNamedFresnel(const std::string_view name);
745746

746747
// IMPORTANT: Two BxDFs are not allowed to be multiplied together.
747748
// NOTE: Right now all Spectral Variables are required to be Monochrome or 3 bucket fixed semantics, all the same wavelength.
748749
// Some things we can't check such as the compatibility of the BTDF with the BRDF (matching indices of refraction, etc.)
749750
bool valid(const typed_pointer_type<const CLayer> rootHandle, system::logger_opt_ptr logger) const;
750751

751752
// For Debug Visualization (TODO: refactor to allow printing invalid nodes not in the `m_rootNodes` -> `printDotTree(std::ostringstream&,typed_pointer_type<const INode>)`)
752-
NBL_API void printDotGraph(std::ostringstream& str) const;
753+
NBL_API2 void printDotGraph(std::ostringstream& str) const;
753754
inline core::string printDotGraph() const
754755
{
755756
std::ostringstream tmp;

include/nbl/core/alloc/AddressAllocatorConcurrencyAdaptors.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,11 +23,11 @@ class AddressAllocatorBasicConcurrencyAdaptor : private AddressAllocator
2323
AddressAllocator& getBaseRef() {return reinterpret_cast<AddressAllocator&>(*this);}
2424
public:
2525
_NBL_DECLARE_ADDRESS_ALLOCATOR_TYPEDEFS(typename AddressAllocator::size_type);
26+
using extra_ctor_param_types = typename AddressAllocator::extra_ctor_param_types;
2627

2728
typedef address_allocator_traits<AddressAllocator> traits;
2829
static_assert(address_allocator_traits<AddressAllocator>::supportsArbitraryOrderFrees,"AddressAllocator does not support arbitrary order frees!");
2930

30-
3131
using AddressAllocator::AddressAllocator;
3232
virtual ~AddressAllocatorBasicConcurrencyAdaptor() {}
3333

include/nbl/core/alloc/SimpleBlockBasedAllocator.h

Lines changed: 19 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -58,9 +58,10 @@ struct TypedHandle final : std::conditional_t<std::is_const_v<T>,typename _Handl
5858
TypedHandle<U,_Handle> retval;
5959
retval.value = base_t::value;
6060
if constexpr (!std::is_void_v<U>)
61+
if (*this)
6162
{
6263
const T* const fake_this = reinterpret_cast<const T*>(sizeof(T));
63-
retval.value -= ptrdiff_t(static_cast<const U*>(fake_this)) - sizeof(T);
64+
retval.value += ptrdiff_t(static_cast<const U*>(fake_this)) - sizeof(T);
6465
}
6566
return retval;
6667
}
@@ -219,8 +220,8 @@ class SimpleBlockBasedAllocator<AddressAllocator,void*> final : protected BlockB
219220
//
220221
template<typename T> requires (!std::is_const_v<T>)
221222
inline T* deref(typed_pointer_type<T> p) {return p;}
222-
template<typename T> requires std::is_const_v<T>
223-
inline T* deref(typed_pointer_type<T> p) const {return p;}
223+
template<typename T>
224+
inline const T* deref(typed_pointer_type<T> p) const {return p;}
224225

225226
// deallocates everything
226227
inline void reset()
@@ -323,17 +324,22 @@ class SimpleBlockBasedAllocator<AddressAllocator,HandleValue> final : protected
323324
template<typename T, typename U>
324325
static inline typed_pointer_type<T> _static_cast(typed_pointer_type<U> p)
325326
{
326-
const U* const fake_p = reinterpret_cast<const U*>(sizeof(U));
327-
HandleValue shift = ptrdiff_t(static_cast<const T*>(fake_p)) - sizeof(U);
328327
typed_pointer_type<T> retval;
329-
retval.value = p.value - shift;
328+
retval.value = p.value;
329+
if (p)
330+
{
331+
const auto begin = std::max(sizeof(T),sizeof(U));
332+
const U* const fake_p = reinterpret_cast<const U*>(begin);
333+
retval.value += ptrdiff_t(static_cast<const T*>(fake_p)) - begin;
334+
}
330335
return retval;
331336
}
332337

333338
struct SCreationParams final
334339
{
335340
base_t::SCreationParams composed;
336-
block_id_t maxBlocks = 0x1<<15;
341+
// TODO: `reserved_size` probably needs to return `size_t` cause otherwise this value can't be bigger than default
342+
block_id_t maxBlocks = 0x1<<13;
337343
};
338344
inline SimpleBlockBasedAllocator(SCreationParams&& params) : base_t(std::move(params.composed)),
339345
m_blockIndexAlloc(base_t::m_mem_resource->allocate(block_id_alloc_t::reserved_size(1,params.maxBlocks,1),_NBL_SIMD_ALIGNMENT),0,0,1,params.maxBlocks,1),
@@ -362,11 +368,11 @@ class SimpleBlockBasedAllocator<AddressAllocator,HandleValue> final : protected
362368
{
363369
return reinterpret_cast<T*>(std::launder(getBlock(p)->data(base_t::m_blockCreationParams)+getOffsetInBlock(p)));
364370
}
365-
template<typename T> requires std::is_const_v<T>
366-
inline T* deref(typed_pointer_type<T> p) const
371+
template<typename T>
372+
inline const T* deref(typed_pointer_type<T> p) const
367373
{
368-
auto* mut = const_cast<this_t*>(this)->deref<std::remove_const_t<T>>(p);
369-
return const_cast<T*>(mut);
374+
std::remove_const_t<T>* mut = const_cast<this_t*>(this)->deref<std::remove_const_t<T>>(p);
375+
return mut;
370376
}
371377

372378
// deallocates everything
@@ -401,7 +407,7 @@ class SimpleBlockBasedAllocator<AddressAllocator,HandleValue> final : protected
401407
{
402408
block_t* block = entry.second;
403409
if (const auto addr=block->alloc(bytes,alignment); addr!=invalid_address)
404-
return {.value=(entry.first<<m_blockSizeLog2)|addr};
410+
return {{{.value=(entry.first<<m_blockSizeLog2)|addr}}};
405411
}
406412
const auto newID = m_blockIndexAlloc.alloc_addr(1,1);
407413
if (newID!=block_id_alloc_t::invalid_address)
@@ -410,7 +416,7 @@ class SimpleBlockBasedAllocator<AddressAllocator,HandleValue> final : protected
410416
if (const auto addr=block->alloc(bytes,alignment); addr!=invalid_address)
411417
{
412418
m_blocks[newID] = block;
413-
return {.value=(newID<<m_blockSizeLog2)|addr};
419+
return {{{.value=(newID<<m_blockSizeLog2)|addr}}};
414420
}
415421
else
416422
base_t::deleteBlock(block);

include/nbl/core/containers/CMemoryPool.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -45,10 +45,10 @@ class CMemoryPool final : public Uncopyable
4545
{
4646
return m_block_alctr.deref<T>(p);
4747
}
48-
template<typename T> requires std::is_const_v<T>
49-
inline T* deref(typed_pointer_type<T> p) const
48+
template<typename T>
49+
inline const T* deref(typed_pointer_type<T> p) const
5050
{
51-
return m_block_alctr.deref<T>(p);
51+
return m_block_alctr.deref<const T>(p);
5252
}
5353

5454
//

include/nbl/core/containers/CObjectPool.h

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -107,11 +107,15 @@ class CObjectPool final : public IObjectPoolBase
107107
}
108108
return retval;
109109
}
110-
template<typename T> requires std::is_const_v<T>
111-
inline T* deref(const typed_pointer_type<T> h, check_t check={.value=false}) const
110+
template<typename T>
111+
inline const T* deref(const typed_pointer_type<T> h, check_t check={.value=false}) const
112112
{
113113
using non_const_t = std::remove_const_t<T>;
114-
const auto mutableH = block_allocator_type::template _const_cast<non_const_t>(h);
114+
typed_pointer_type<non_const_t> mutableH;
115+
if constexpr (std::is_const_v<T>)
116+
mutableH = block_allocator_type::template _const_cast<non_const_t>(h);
117+
else
118+
mutableH = h;
115119
return const_cast<this_t*>(this)->deref<non_const_t>(mutableH,check);
116120
}
117121

@@ -139,7 +143,7 @@ class CObjectPool final : public IObjectPoolBase
139143
return sizeof(T);
140144
}();
141145
//
142-
const typed_pointer_type<T> retval = m_pool.allocate(size*n,a);
146+
const auto retval = block_allocator_type::template _reinterpret_cast<T>(m_pool.allocate(size*n,a));
143147
// record existence if needed
144148
if constexpr (std::is_base_of_v<INonTrivial,T>)
145149
{
@@ -161,7 +165,7 @@ class CObjectPool final : public IObjectPoolBase
161165
if constexpr (sizeof...(FuncArgs)!=0u)
162166
std::construct_at(ptr,std::forward<FuncArgs>(args)...);
163167
else
164-
std::uninitialized_default_construct(ptr);
168+
std::uninitialized_default_construct(ptr,ptr+1);
165169
// go to next object
166170
ptr = reinterpret_cast<T*>(reinterpret_cast<char*>(ptr)+size);
167171
}

src/nbl/CMakeLists.txt

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -167,6 +167,9 @@ set(NBL_ASSET_SOURCES
167167
asset/interchange/IAssetWriter.cpp
168168
asset/interchange/IAssetLoader.cpp
169169

170+
# Materials
171+
asset/material_compiler3/CFrontendIR.cpp
172+
170173
# Shaders
171174
asset/utils/ISPIRVOptimizer.cpp
172175
asset/utils/ISPIRVEntryPointTrimmer.cpp
@@ -214,15 +217,15 @@ set(NBL_ASSET_SOURCES
214217
asset/interchange/CImageLoaderJPG.cpp
215218
asset/interchange/CImageLoaderPNG.cpp
216219
asset/interchange/CImageLoaderTGA.cpp
217-
asset/interchange/CImageLoaderOpenEXR.cpp # TODO: Nahim
220+
asset/interchange/CImageLoaderOpenEXR.cpp
218221
asset/interchange/CGLILoader.cpp
219222

220223
# Image writers
221224
asset/interchange/IImageWriter.cpp
222225
asset/interchange/CImageWriterJPG.cpp
223226
asset/interchange/CImageWriterPNG.cpp
224227
asset/interchange/CImageWriterTGA.cpp
225-
asset/interchange/CImageWriterOpenEXR.cpp # TODO: Nahim
228+
asset/interchange/CImageWriterOpenEXR.cpp
226229
asset/interchange/CGLIWriter.cpp
227230

228231
# IES profile loaders

0 commit comments

Comments
 (0)