Skip to content

Commit ab9e7b8

Browse files
author
devsh
committed
start reworking the Mitsuba Loader into a Scene Loader
1 parent 8b6b210 commit ab9e7b8

File tree

3 files changed

+30
-211
lines changed

3 files changed

+30
-211
lines changed

include/nbl/ext/MitsubaLoader/CMitsubaLoader.h

Lines changed: 7 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,15 @@
11
// Copyright (C) 2018-2020 - DevSH Graphics Programming Sp. z O.O.
22
// This file is part of the "Nabla Engine".
33
// For conditions of distribution and use, see copyright notice in nabla.h
4+
#ifndef _NBL_EXT_MISTUBA_LOADER_C_MITSUBA_LOADER_H_INCLUDED_
5+
#define _NBL_EXT_MISTUBA_LOADER_C_MITSUBA_LOADER_H_INCLUDED_
46

5-
#ifndef __C_MITSUBA_LOADER_H_INCLUDED__
6-
#define __C_MITSUBA_LOADER_H_INCLUDED__
77

88
#include "nbl/asset/asset.h"
99

10-
#include "IFileSystem.h"
11-
#include "nbl/asset/utils/ICPUVirtualTexture.h"
12-
1310
#include "nbl/ext/MitsubaLoader/CSerializedLoader.h"
14-
#include "nbl/ext/MitsubaLoader/CMitsubaMetadata.h"
15-
#include "nbl/ext/MitsubaLoader/CElementShape.h"
11+
//#include "nbl/ext/MitsubaLoader/CMitsubaMetadata.h"
12+
//#include "nbl/ext/MitsubaLoader/CElementShape.h"
1613
#include "nbl/ext/MitsubaLoader/SContext.h"
1714

1815

@@ -23,8 +20,7 @@ namespace nbl::ext::MitsubaLoader
2320
class CElementBSDF;
2421
class CMitsubaMaterialCompilerFrontend;
2522

26-
27-
// TODO: we need a GLSL to C++ compatibility wrapper
23+
#if 0 // TODO
2824
//#include "nbl/builtin/glsl/ext/MitsubaLoader/instance_data_struct.glsl"
2925
#define uint uint32_t
3026
#define uvec2 uint64_t
@@ -52,7 +48,7 @@ struct nbl_glsl_ext_Mitsuba_Loader_instance_data_t
5248
using instance_data_t = nbl_glsl_ext_Mitsuba_Loader_instance_data_t;
5349

5450

55-
class CMitsubaLoader : public asset::IRenderpassIndependentPipelineLoader
51+
class CMitsubaLoader : public asset::ISceneLoader
5652
{
5753
friend class CMitsubaMaterialCompilerFrontend;
5854
public:
@@ -67,8 +63,6 @@ class CMitsubaLoader : public asset::IRenderpassIndependentPipelineLoader
6763
//! Destructor
6864
virtual ~CMitsubaLoader() = default;
6965

70-
static core::smart_refctd_ptr<asset::ICPUPipelineLayout> createPipelineLayout(asset::IAssetManager* _manager, const asset::ICPUVirtualTexture* _vt);
71-
7266
//
7367
core::vector<SContext::shape_ass_type> getMesh(SContext& ctx, uint32_t hierarchyLevel, CElementShape* shape);
7468
core::vector<SContext::shape_ass_type> loadShapeGroup(SContext& ctx, uint32_t hierarchyLevel, const CElementShape::ShapeGroup* shapegroup, const core::matrix3x4SIMD& relTform);
@@ -101,6 +95,7 @@ class CMitsubaLoader : public asset::IRenderpassIndependentPipelineLoader
10195
//! Loads an asset from an opened file, returns nullptr in case of failure.
10296
asset::SAssetBundle loadAsset(io::IReadFile* _file, const asset::IAssetLoader::SAssetLoadParams& _params, asset::IAssetLoader::IAssetLoaderOverride* _override = nullptr, uint32_t _hierarchyLevel = 0u) override;
10397
};
98+
#endif
10499

105100
}
106101
#endif
Lines changed: 6 additions & 104 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,10 @@
1-
// Copyright (C) 2018-2020 - DevSH Graphics Programming Sp. z O.O.
1+
// Copyright (C) 2018-2025 - DevSH Graphics Programming Sp. z O.O.
22
// This file is part of the "Nabla Engine".
33
// For conditions of distribution and use, see copyright notice in nabla.h
4+
#ifndef _NBL_EXT_MISTUBA_LOADER_C_MITSUBA_METADATA_H_INCLUDED_
5+
#define _NBL_EXT_MISTUBA_LOADER_C_MITSUBA_METADATA_H_INCLUDED_
46

5-
#ifndef __NBL_C_MITSUBA_METADATA_H_INCLUDED__
6-
#define __NBL_C_MITSUBA_METADATA_H_INCLUDED__
77

8-
#include "nbl/core/compile_config.h"
98
#include "nbl/asset/metadata/IAssetMetadata.h"
109
#include "nbl/asset/ICPUImage.h"
1110

@@ -15,15 +14,11 @@
1514
#include "nbl/ext/MitsubaLoader/CElementSensor.h"
1615
#include "nbl/ext/MitsubaLoader/CElementShape.h"
1716

18-
namespace nbl
19-
{
20-
namespace ext
21-
{
22-
namespace MitsubaLoader
17+
18+
namespace nbl::ext::MitsubaLoader
2319
{
2420

2521
//! A class to derive mitsuba mesh loader metadata objects from
26-
2722
class CMitsubaMetadata : public asset::IAssetMetadata
2823
{
2924
public:
@@ -32,55 +27,12 @@ class CMitsubaMetadata : public asset::IAssetMetadata
3227
public:
3328
std::string m_id;
3429
};
35-
class CDerivativeMap : public asset::IImageMetadata
36-
{
37-
public:
38-
CDerivativeMap() : m_scale(1.f) {}
39-
explicit CDerivativeMap(float scale) : m_scale(scale) {}
40-
41-
float m_scale;
42-
};
43-
class CRenderpassIndependentPipeline : public asset::IRenderpassIndependentPipelineMetadata
44-
{
45-
public:
46-
CRenderpassIndependentPipeline() : IRenderpassIndependentPipelineMetadata(), m_ds0() {}
47-
template<typename... Args>
48-
CRenderpassIndependentPipeline(core::smart_refctd_ptr<asset::ICPUDescriptorSet>&& _ds0, Args&&... args) : IRenderpassIndependentPipelineMetadata(std::forward<Args>(args)...), m_ds0(std::move(_ds0))
49-
{
50-
}
51-
52-
inline CRenderpassIndependentPipeline& operator=(CRenderpassIndependentPipeline&& other)
53-
{
54-
IRenderpassIndependentPipelineMetadata::operator=(std::move(other));
55-
std::swap(m_ds0, other.m_ds0);
56-
return *this;
57-
}
58-
59-
core::smart_refctd_ptr<asset::ICPUDescriptorSet> m_ds0;
60-
};
6130
class CMesh : public asset::IMeshMetadata, public CID
6231
{
6332
public:
64-
CMesh() : IMeshMetadata(), CID(), m_instanceAuxData(nullptr,nullptr), type(CElementShape::Type::INVALID) {}
33+
CMesh() : IMeshMetadata(), CID(), type(CElementShape::Type::INVALID) {}
6534
~CMesh() {}
6635

67-
struct SInstanceAuxilaryData
68-
{
69-
SInstanceAuxilaryData& operator=(SInstanceAuxilaryData&& other)
70-
{
71-
frontEmitter = std::move(other.frontEmitter);
72-
backEmitter = std::move(other.backEmitter);
73-
bsdf = std::move(other.bsdf);
74-
return *this;
75-
}
76-
77-
CElementEmitter frontEmitter; // type is invalid if not used
78-
CElementEmitter backEmitter; // type is invalid if not used
79-
CMitsubaMaterialCompilerFrontend::front_and_back_t bsdf;
80-
};
81-
82-
core::SRange<const SInstanceAuxilaryData> m_instanceAuxData;
83-
8436
CElementShape::Type type;
8537
};
8638
struct SGlobal
@@ -92,14 +44,6 @@ class CMitsubaMetadata : public asset::IAssetMetadata
9244

9345
CElementIntegrator m_integrator;
9446
core::vector<CElementSensor> m_sensors;
95-
core::vector<CElementEmitter> m_emitters;
96-
core::smart_refctd_ptr<asset::ICPUVirtualTexture> m_VT;
97-
core::smart_refctd_ptr<asset::ICPUDescriptorSet> m_ds0;
98-
core::vector<core::smart_refctd_ptr<asset::ICPUImage>> m_envMapImages;
99-
//has to go after #version and before required user-provided descriptors and functions
100-
std::string m_materialCompilerGLSL_declarations;
101-
//has to go after required user-provided descriptors and functions and before the rest of shader (especially entry point function)
102-
std::string m_materialCompilerGLSL_source;
10347
} m_global;
10448

10549
CMitsubaMetadata() :
@@ -113,11 +57,6 @@ class CMitsubaMetadata : public asset::IAssetMetadata
11357
const char* getLoaderName() const override { return LoaderName; }
11458

11559
//!
116-
inline const CRenderpassIndependentPipeline* getAssetSpecificMetadata(const asset::ICPURenderpassIndependentPipeline* asset) const
117-
{
118-
const auto found = IAssetMetadata::getAssetSpecificMetadata(asset);
119-
return static_cast<const CRenderpassIndependentPipeline*>(found);
120-
}
12160
inline const CMesh* getAssetSpecificMetadata(const asset::ICPUMesh* asset) const
12261
{
12362
const auto found = IAssetMetadata::getAssetSpecificMetadata(asset);
@@ -127,26 +66,9 @@ class CMitsubaMetadata : public asset::IAssetMetadata
12766
private:
12867
friend class CMitsubaLoader;
12968

130-
meta_container_t<CRenderpassIndependentPipeline> m_metaPplnStorage;
131-
core::smart_refctd_dynamic_array<asset::IRenderpassIndependentPipelineMetadata::ShaderInputSemantic> m_semanticStorage;
132-
CRenderpassIndependentPipeline* m_metaPplnStorageIt;
133-
13469
meta_container_t<CMesh> m_metaMeshStorage;
135-
core::smart_refctd_dynamic_array<CMesh::SInstance> m_metaMeshInstanceStorage;
136-
core::smart_refctd_dynamic_array<CMesh::SInstanceAuxilaryData> m_metaMeshInstanceAuxStorage;
13770
CMesh* m_meshStorageIt;
138-
CMesh::SInstance* m_instanceStorageIt;
139-
CMesh::SInstanceAuxilaryData* m_instanceAuxStorageIt;
140-
141-
meta_container_t<CDerivativeMap> m_metaDerivMapStorage;
142-
CDerivativeMap* m_metaDerivMapStorageIt;
14371

144-
inline void reservePplnStorage(uint32_t pplnCount, core::smart_refctd_dynamic_array<asset::IRenderpassIndependentPipelineMetadata::ShaderInputSemantic>&& _semanticStorage)
145-
{
146-
m_metaPplnStorage = IAssetMetadata::createContainer<CRenderpassIndependentPipeline>(pplnCount);
147-
m_semanticStorage = std::move(_semanticStorage);
148-
m_metaPplnStorageIt = m_metaPplnStorage->begin();
149-
}
15072
inline void reserveMeshStorage(uint32_t meshCount, uint32_t instanceCount)
15173
{
15274
m_metaMeshStorage = IAssetMetadata::createContainer<CMesh>(meshCount);
@@ -156,17 +78,6 @@ class CMitsubaMetadata : public asset::IAssetMetadata
15678
m_instanceStorageIt = m_metaMeshInstanceStorage->begin();
15779
m_instanceAuxStorageIt = m_metaMeshInstanceAuxStorage->begin();
15880
}
159-
inline void reserveDerivMapStorage(uint32_t count)
160-
{
161-
m_metaDerivMapStorage = IAssetMetadata::createContainer<CDerivativeMap>(count);
162-
m_metaDerivMapStorageIt = m_metaDerivMapStorage->begin();
163-
}
164-
inline void addPplnMeta(const asset::ICPURenderpassIndependentPipeline* ppln, core::smart_refctd_ptr<asset::ICPUDescriptorSet>&& _ds0)
165-
{
166-
*m_metaPplnStorageIt = CMitsubaMetadata::CRenderpassIndependentPipeline(std::move(_ds0),core::SRange<const asset::IRenderpassIndependentPipelineMetadata::ShaderInputSemantic>(m_semanticStorage->begin(),m_semanticStorage->end()));
167-
IAssetMetadata::insertAssetSpecificMetadata(ppln,m_metaPplnStorageIt);
168-
m_metaPplnStorageIt++;
169-
}
17081
template<typename InstanceIterator>
17182
inline uint32_t addMeshMeta(const asset::ICPUMesh* mesh, std::string&& id, const CElementShape::Type type, InstanceIterator instancesBegin, InstanceIterator instancesEnd)
17283
{
@@ -195,16 +106,7 @@ class CMitsubaMetadata : public asset::IAssetMetadata
195106

196107
return meta->m_instances.size();
197108
}
198-
inline void addDerivMapMeta(const asset::ICPUImage* derivmap, float scale)
199-
{
200-
auto* meta = m_metaDerivMapStorageIt++;
201-
meta->m_scale = scale;
202-
IAssetMetadata::insertAssetSpecificMetadata(derivmap, meta);
203-
}
204109
};
205110

206111
}
207-
}
208-
}
209-
210112
#endif

include/nbl/ext/MitsubaLoader/SContext.h

Lines changed: 17 additions & 95 deletions
Original file line numberDiff line numberDiff line change
@@ -1,48 +1,38 @@
1-
// Copyright (C) 2018-2020 - DevSH Graphics Programming Sp. z O.O.
1+
// Copyright (C) 2018-2025 - DevSH Graphics Programming Sp. z O.O.
22
// This file is part of the "Nabla Engine".
33
// For conditions of distribution and use, see copyright notice in nabla.h
4+
#ifndef _NBL_EXT_MISTUBA_LOADER_C_MITSUBA_LOADER_CONTEXT_H_INCLUDED_
5+
#define _NBL_EXT_MISTUBA_LOADER_C_MITSUBA_LOADER_CONTEXT_H_INCLUDED_
46

5-
#ifndef __C_MITSUBA_LOADER_CONTEXT_H_INCLUDED__
6-
#define __C_MITSUBA_LOADER_CONTEXT_H_INCLUDED__
77

8-
9-
#include "nbl/asset/ICPUMesh.h"
10-
#include "nbl/asset/utils/IGeometryCreator.h"
11-
#include "nbl/asset/material_compiler/CMaterialCompilerGLSLRasterBackend.h"
8+
#include "nbl/asset/ICPUPolygonGeometry.h"
9+
//#include "nbl/asset/utils/IGeometryCreator.h"
1210
#include "nbl/asset/interchange/CIESProfileLoader.h"
1311

14-
#include "nbl/ext/MitsubaLoader/CMitsubaMaterialCompilerFrontend.h"
15-
#include "nbl/ext/MitsubaLoader/CElementShape.h"
12+
//#include "nbl/ext/MitsubaLoader/CMitsubaMaterialCompilerFrontend.h"
13+
//#include "nbl/ext/MitsubaLoader/CElementShape.h"
1614

17-
namespace nbl
18-
{
19-
namespace ext
20-
{
21-
namespace MitsubaLoader
15+
namespace nbl::ext::MitsubaLoader
2216
{
2317

2418
struct SContext
2519
{
2620
public:
2721
SContext(
28-
const asset::IGeometryCreator* _geomCreator,
29-
const asset::IMeshManipulator* _manipulator,
22+
// const asset::IGeometryCreator* _geomCreator,
23+
// const asset::IMeshManipulator* _manipulator,
3024
const asset::IAssetLoader::SAssetLoadContext& _params,
3125
asset::IAssetLoader::IAssetLoaderOverride* _override,
32-
CMitsubaMetadata* _metadata
26+
// CMitsubaMetadata* _metadata
3327
);
3428

35-
const asset::IGeometryCreator* creator;
36-
const asset::IMeshManipulator* manipulator;
29+
// const asset::IGeometryCreator* creator;
30+
// const asset::IMeshManipulator* manipulator;
3731
const asset::IAssetLoader::SAssetLoadContext inner;
3832
asset::IAssetLoader::IAssetLoaderOverride* override_;
39-
CMitsubaMetadata* meta;
40-
41-
_NBL_STATIC_INLINE_CONSTEXPR uint32_t VT_PAGE_SZ_LOG2 = 7u;//128
42-
_NBL_STATIC_INLINE_CONSTEXPR uint32_t VT_PHYSICAL_PAGE_TEX_TILES_PER_DIM_LOG2 = 4u;//16
43-
_NBL_STATIC_INLINE_CONSTEXPR uint32_t VT_PAGE_PADDING = 8u;
44-
_NBL_STATIC_INLINE_CONSTEXPR uint32_t VT_MAX_ALLOCATABLE_TEX_SZ_LOG2 = 12u;//4096
33+
// CMitsubaMetadata* meta;
4534

35+
#if 0
4636
//
4737
using group_ass_type = core::vector<core::smart_refctd_ptr<asset::ICPUMesh>>;
4838
//core::map<const CElementShape::ShapeGroup*, group_ass_type> groupCache;
@@ -171,21 +161,6 @@ struct SContext
171161
return params;
172162
}
173163

174-
inline core::smart_refctd_ptr<asset::ICPUSampler> getSampler(const asset::ICPUSampler::SParams& params) const
175-
{
176-
const std::string samplerKey = samplerCacheKey(params);
177-
const asset::IAsset::E_TYPE types[2] = {asset::IAsset::ET_SAMPLER,asset::IAsset::ET_TERMINATING_ZERO};
178-
auto samplerBundle = override_->findCachedAsset(samplerKey,types,inner,0u);
179-
if (samplerBundle.getContents().empty())
180-
{
181-
auto sampler = core::make_smart_refctd_ptr<asset::ICPUSampler>(params);
182-
override_->insertAssetIntoCache(asset::SAssetBundle(nullptr,{sampler}),samplerKey,inner,0);
183-
return sampler;
184-
}
185-
else
186-
return core::smart_refctd_ptr_static_cast<asset::ICPUSampler>(samplerBundle.getContents().begin()[0]);
187-
}
188-
189164
//index of root node in IR
190165
using bsdf_type = const CMitsubaMaterialCompilerFrontend::front_and_back_t;
191166
//caches instr buffer instr-wise offset (.first) and instruction count (.second) for each bsdf node
@@ -214,67 +189,14 @@ struct SContext
214189
};
215190
core::unordered_multimap<const shape_ass_type::pointee*, SInstanceData> mapMesh2instanceData;
216191

217-
struct SPipelineCacheKey
218-
{
219-
asset::SVertexInputParams vtxParams;
220-
asset::SPrimitiveAssemblyParams primParams;
221-
222-
inline bool operator==(const SPipelineCacheKey& rhs) const
223-
{
224-
return memcmp(&vtxParams, &rhs.vtxParams, sizeof(vtxParams)) == 0 && memcmp(&primParams, &rhs.primParams, sizeof(primParams)) == 0;
225-
}
226-
227-
struct hash
228-
{
229-
inline size_t operator()(const SPipelineCacheKey& k) const
230-
{
231-
constexpr size_t BYTESZ = sizeof(k.vtxParams) + sizeof(k.primParams);
232-
uint8_t mem[BYTESZ]{};
233-
uint8_t* ptr = mem;
234-
memcpy(ptr, &k.vtxParams, sizeof(k.vtxParams));
235-
ptr += sizeof(k.vtxParams);
236-
memcpy(ptr, &k.primParams, sizeof(k.primParams));
237-
ptr += sizeof(k.primParams);
238-
239-
return std::hash<std::string_view>{}(std::string_view(reinterpret_cast<const char*>(mem), BYTESZ));
240-
}
241-
};
242-
};
243192
core::unordered_map<SPipelineCacheKey, core::smart_refctd_ptr<asset::ICPURenderpassIndependentPipeline>, SPipelineCacheKey::hash> pipelineCache;
244-
193+
#endif
245194
//material compiler
246195
core::smart_refctd_ptr<asset::material_compiler::IR> ir;
247196
CMitsubaMaterialCompilerFrontend frontend;
248-
asset::material_compiler::CMaterialCompilerGLSLRasterBackend::SContext backend_ctx;
249-
asset::material_compiler::CMaterialCompilerGLSLRasterBackend backend;
250197

251198
private:
252-
// TODO: commonalize this to all loaders
253-
static std::string samplerCacheKey(const asset::ICPUSampler::SParams& samplerParams)
254-
{
255-
std::string samplerCacheKey = "__Sampler";
256-
257-
if (samplerParams.MinFilter==asset::ISampler::ETF_LINEAR)
258-
samplerCacheKey += "?trilinear";
259-
else
260-
samplerCacheKey += "?nearest";
261-
262-
static const char* wrapModeName[] =
263-
{
264-
"?repeat",
265-
"?clamp_to_edge",
266-
"?clamp_to_border",
267-
"?mirror",
268-
"?mirror_clamp_to_edge",
269-
"?mirror_clamp_to_border"
270-
};
271-
samplerCacheKey += wrapModeName[samplerParams.TextureWrapU];
272-
samplerCacheKey += wrapModeName[samplerParams.TextureWrapV];
273-
274-
return samplerCacheKey;
275-
}
276199
};
277200

278-
}}}
279-
201+
}
280202
#endif

0 commit comments

Comments
 (0)