Skip to content

Commit 8a4a1f6

Browse files
author
devsh
committed
save progress before splitting up
1 parent 5181687 commit 8a4a1f6

File tree

1 file changed

+156
-0
lines changed
  • include/nbl/asset/material_compiler3

1 file changed

+156
-0
lines changed
Lines changed: 156 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,156 @@
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_IR_H_INCLUDED__
5+
#define __NBL_ASSET_MATERIAL_COMPILER_V3_IR_H_INCLUDED__
6+
7+
8+
#include "nbl/core/declarations.h"
9+
#include "nbl/core/definitions.h"
10+
11+
#include "nbl/asset/ICPUImageView.h"
12+
13+
//#include "IRNode.h"
14+
#include <nbl/asset/ICPUImageView.h>
15+
16+
17+
// temporary
18+
#define NBL_API
19+
20+
namespace nbl::asset::material_compiler3
21+
{
22+
23+
// Class to manage all nodes' backing and hand them out as `uint32_t` handles
24+
class NBL_API CNodePool : public core::IReferenceCounted
25+
{
26+
// save myself some typing
27+
using refctd_pmr_t = core::smart_refctd_ptr<core::refctd_memory_resource>;
28+
29+
public:
30+
// constructor
31+
inline core::smart_refctd_ptr<CNodePool> create(const uint8_t chunkSizeLog2=19, const uint8_t maxNodeAlignLog2=4, refctd_pmr_t&& _pmr={})
32+
{
33+
if (chunkSizeLog2<14 || maxNodeAlignLog2<4)
34+
return nullptr;
35+
if (!_pmr)
36+
_pmr = core::getDefaultMemoryResource();
37+
return core::smart_refctd_ptr<CNodePool>(new CNodePool(chunkSizeLog2,maxNodeAlignLog2,std::move(_pmr)),core::dont_grab);
38+
}
39+
40+
// everything is handed out by index not pointer
41+
struct Handle
42+
{
43+
using value_t = uint32_t;
44+
constexpr static inline value_t Invalid = ~value_t(0);
45+
46+
inline operator bool() const {return value!=Invalid;}
47+
48+
// also serves as a byte offset into the pool
49+
value_t value = Invalid;
50+
};
51+
template<typename T>
52+
inline T* deref(const Handle& h) {chunks[getChunkIx(h)].data()}
53+
54+
inline Handle alloc(const uint32_t size, const uint16_t alignment)
55+
{
56+
Handle retval = {};
57+
auto allocFromChunk = [&](Chunk& chunk, const uint32_t chunkIx)
58+
{
59+
const auto localOffset = chunk.alloc(size,alignment);
60+
if (localOffset!=decltype(Chunk::m_alloc)::invalid_address)
61+
retval.value = localOffset|(chunkIx<<m_chunkSizeLog2);
62+
};
63+
// try current back chunk
64+
if (!m_chunks.empty())
65+
allocFromChunk(m_chunks.back(),m_chunks.size()-1);
66+
// if fail try new chunk
67+
if (!retval)
68+
{
69+
const auto chunkSize = 0x1u<<m_chunkSizeLog2;
70+
const auto chunkAlign = 0x1u<<m_maxNodeAlignLog2;
71+
Chunk newChunk = {
72+
.m_alloc = decltype(Chunk::m_alloc)(nullptr,0,0,chunkAlign,chunkSize),
73+
.m_data = reinterpret_cast<uint8_t*>(m_pmr->allocate(chunkSize,chunkAlign))
74+
};
75+
if (newChunk.m_data)
76+
{
77+
allocFromChunk(newChunk,m_chunks.size());
78+
if (retval)
79+
m_chunks.push_back(std::move(newChunk));
80+
else
81+
m_pmr->deallocate(newChunk.m_data,chunkSize,chunkAlign);
82+
}
83+
}
84+
return retval;
85+
}
86+
inline void free(const Handle& h)
87+
{
88+
assert(getChunkIx(h)<m_chunks.size());
89+
}
90+
91+
// new
92+
// delete
93+
94+
//
95+
96+
protected:
97+
// for now using KISS, we can use geeneralpupose allocator later
98+
struct Chunk
99+
{
100+
inline Handle::value_t alloc(const uint32_t size, const uint16_t alignment)
101+
{
102+
return m_alloc.alloc_addr(size,alignment);
103+
}
104+
inline void free(const Handle::value_t addr, const uint32_t size)
105+
{
106+
m_alloc.free_addr(addr,size);
107+
}
108+
109+
core::LinearAddressAllocatorST<Handle::value_t> m_alloc;
110+
uint8_t* m_data;
111+
};
112+
inline CNodePool(const uint8_t _chunkSizeLog2, const uint8_t _maxNodeAlignLog2, refctd_pmr_t&& _pmr) :
113+
m_chunkSizeLog2(_chunkSizeLog2), m_maxNodeAlignLog2(_maxNodeAlignLog2), m_pmr(std::move(_pmr)) {}
114+
inline uint32_t getChunkIx(const Handle& h) {h.value>>m_chunkSizeLog2;}
115+
116+
core::vector<Chunk> m_chunks;
117+
refctd_pmr_t m_pmr;
118+
const uint8_t m_chunkSizeLog2;
119+
const uint8_t m_maxNodeAlignLog2;
120+
};
121+
122+
class NBL_API CForest
123+
{
124+
public:
125+
class INode
126+
{
127+
public:
128+
enum class Type : uint8_t
129+
{
130+
Emission,
131+
BxDF,
132+
Multiply,
133+
Add,
134+
Sub,
135+
// 1 - C_0
136+
Complement,
137+
Function
138+
};
139+
virtual Type getType() const = 0;
140+
141+
// Only sane child count allowed
142+
virtual uint8_t getChildCount() = 0;
143+
144+
// based
145+
virtual CNodePool::Handle getChild(const uint8_t index) const;
146+
};
147+
148+
protected:
149+
};
150+
151+
//! DAG (baked)
152+
//! Nodes
153+
154+
} // namespace nbl::asset::material_compiler3
155+
156+
#endif

0 commit comments

Comments
 (0)