Skip to content

Commit 54aad34

Browse files
author
devsh
committed
start the True IR
1 parent af3cc2a commit 54aad34

File tree

3 files changed

+103
-1
lines changed

3 files changed

+103
-1
lines changed

include/nbl/asset/material_compiler3/CFrontendIR.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,8 @@ class CFrontendIR : public CNodePool
7474
return false;
7575
}
7676

77+
CNodePool::TypedHandle<CNodePool::CDebugInfo> debugInfo;
78+
7779
protected:
7880
//
7981
virtual inline bool isBxDFAllowedInSubtree_impl(const uint8_t ix) const {return false;}
@@ -115,7 +117,7 @@ class CFrontendIR : public CNodePool
115117
template<uint8_t Count>
116118
static inline uint32_t calc_size(const SCreationParams<Count>&)
117119
{
118-
return sizeof(this)+sizeof(SCreationParams<Count>);
120+
return sizeof(CSpectralVariable)+sizeof(SCreationParams<Count>);
119121
}
120122

121123
inline uint8_t getKnotCount() const

include/nbl/asset/material_compiler3/CNodePool.h

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,38 @@ class CNodePool : public core::IReferenceCounted
6161
return const_cast<Handle*>(const_cast<INode*>(this)->getChildHandleStorage(ix));
6262
}
6363
};
64+
// Debug Info node
65+
class CDebugInfo : public INode
66+
{
67+
public:
68+
inline const std::string_view getTypeName() const override {return "nbl::CNodePool::CDebugInfo";}
69+
inline uint8_t getChildCount() const override {return 0;}
70+
inline uint32_t getSize() const {return m_size;}
71+
72+
static inline uint32_t calc_size(const void* data, const uint32_t size)
73+
{
74+
return sizeof(CDebugInfo)+size;
75+
}
76+
static inline uint32_t calc_size(const std::string_view& view)
77+
{
78+
return view.length();
79+
}
80+
inline CDebugInfo(const void* data, const uint32_t size) : m_size(size)
81+
{
82+
if (data)
83+
memcpy(this+1,data,m_size);
84+
}
85+
inline CDebugInfo(const std::string_view& view) : CDebugInfo(nullptr,view.length()+1)
86+
{
87+
auto* out = reinterpret_cast<char*>(this+1);
88+
if (m_size>1)
89+
memcpy(out,view.data(),m_size);
90+
out[m_size-1] = 0;
91+
}
92+
93+
protected:
94+
const uint32_t m_size;
95+
};
6496

6597
//
6698
template<typename T> requires std::is_base_of_v<INode,T>
@@ -173,7 +205,10 @@ class CNodePool : public core::IReferenceCounted
173205
const auto chunkSize = 0x1u<<m_chunkSizeLog2;
174206
const auto chunkAlign = 0x1u<<m_maxNodeAlignLog2;
175207
for (auto& chunk : m_chunks)
208+
{
209+
// TODO: destroy nodes allocated from chunk
176210
m_pmr->deallocate(chunk.m_data,chunkSize,chunkAlign);
211+
}
177212
}
178213

179214
private:
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
// Copyright (C) 2018-2025 - DevSH Graphics Programming Sp. z O.O.
2+
// This file is part of the "Nabla Engine".
3+
// For conditions of distribution and use, see copyright notice in nabla.h
4+
#ifndef _NBL_ASSET_MATERIAL_COMPILER_V3_C_TRUE_IR_H_INCLUDED_
5+
#define _NBL_ASSET_MATERIAL_COMPILER_V3_C_TRUE_IR_H_INCLUDED_
6+
7+
8+
#include "nbl/asset/material_compiler3/CFrontendIR.h"
9+
10+
11+
namespace nbl::asset::material_compiler3
12+
{
13+
14+
// You make the Materials with a classical expression IR, one Root Node per material
15+
class CTrueIR : public CNodePool
16+
{
17+
public:
18+
// constructor
19+
inline core::smart_refctd_ptr<CTrueIR> create(const uint8_t chunkSizeLog2=19, const uint8_t maxNodeAlignLog2=4, refctd_pmr_t&& _pmr={})
20+
{
21+
if (chunkSizeLog2<14 || maxNodeAlignLog2<4)
22+
return nullptr;
23+
if (!_pmr)
24+
_pmr = core::getDefaultMemoryResource();
25+
return core::smart_refctd_ptr<CTrueIR>(new CFrontendIR(chunkSizeLog2,maxNodeAlignLog2,std::move(_pmr)),core::dont_grab);
26+
}
27+
28+
29+
// basic "built-in" nodes
30+
class INode : public CNodePool::INode
31+
{
32+
public:
33+
34+
protected:
35+
// Each node is final and immutable, has a precomputed hash for the whole subtree beneath it.
36+
// Debug info does not form part of the hash, so can get wildly replaced.
37+
core::blake3_hash_t hash;
38+
};
39+
template<typename T> requires std::is_base_of_v<INode,T>
40+
using TypedHandle = CNodePool::TypedHandle<T>;
41+
42+
43+
// Each material comes down to this
44+
struct Material
45+
{
46+
// TypedHandle<CRootNode> root;
47+
CNodePool::TypedHandle<CNodePool::CDebugInfo> debugInfo;
48+
};
49+
inline std::span<const Material> getMaterials() const {return m_materials;}
50+
51+
// We take the trees from the forest, and canonicalize them into our weird Domain Specific IR with Upside down expression trees.
52+
bool addMaterials(const CFrontendIR* forest);
53+
54+
protected:
55+
using CNodePool::CNodePool;
56+
57+
core::vector<Material> m_materials;
58+
core::unordered_map<core::blake3_hash_t,Handle> m_uniqueNodes;
59+
};
60+
61+
//! DAG (baked)
62+
63+
} // namespace nbl::asset::material_compiler3
64+
65+
#endif

0 commit comments

Comments
 (0)