Skip to content

Commit e0a23fc

Browse files
change the signatures of IAssetLoader interm_get a little bit, and make our first indirect load example
1 parent f5bba61 commit e0a23fc

8 files changed

Lines changed: 185 additions & 342 deletions

File tree

include/nbl/asset/IAssetManager.h

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -385,8 +385,10 @@ class NBL_API2 IAssetManager : public core::IReferenceCounted
385385
uint32_t getAssetLoaderCount() { return static_cast<uint32_t>(m_loaders.vector.size()); }
386386

387387
//! @returns 0xdeadbeefu on failure or 0-based index on success.
388-
uint32_t addAssetLoader(core::smart_refctd_ptr<IAssetLoader>&& _loader)
388+
inline uint32_t addAssetLoader(core::smart_refctd_ptr<IAssetLoader>&& _loader)
389389
{
390+
if (!_loader)
391+
return 0xdeadbeefu;
390392
// there's no way it ever fails, so no 0xdeadbeef return
391393
const char** exts = _loader->getAssociatedFileExtensions();
392394
size_t extIx = 0u;
@@ -395,8 +397,10 @@ class NBL_API2 IAssetManager : public core::IReferenceCounted
395397
m_loaders.pushToVector(std::move(_loader));
396398
return static_cast<uint32_t>(m_loaders.vector.size())-1u;
397399
}
398-
void removeAssetLoader(IAssetLoader* _loader)
400+
inline void removeAssetLoader(IAssetLoader* _loader)
399401
{
402+
if (!_loader)
403+
return;
400404
m_loaders.eraseFromVector(
401405
std::find_if(std::begin(m_loaders.vector), std::end(m_loaders.vector), [_loader](const core::smart_refctd_ptr<IAssetLoader>& a)->bool { return a.get()==_loader; })
402406
);
@@ -409,8 +413,10 @@ class NBL_API2 IAssetManager : public core::IReferenceCounted
409413
// Asset Writers [FOLLOWING ARE NOT THREAD SAFE]
410414
uint32_t getAssetWriterCount() { return static_cast<uint32_t>(m_writers.perType.getSize()); } // todo.. well, it's not really writer count.. but rather type<->writer association count
411415

412-
void addAssetWriter(core::smart_refctd_ptr<IAssetWriter>&& _writer)
416+
inline void addAssetWriter(core::smart_refctd_ptr<IAssetWriter>&& _writer)
413417
{
418+
if (!_writer)
419+
return;
414420
const uint64_t suppTypes = _writer->getSupportedAssetTypesBitfield();
415421
const char** exts = _writer->getAssociatedFileExtensions();
416422
for (uint32_t i = 0u; i < IAsset::ET_STANDARD_TYPES_COUNT; ++i)
@@ -425,8 +431,10 @@ class NBL_API2 IAssetManager : public core::IReferenceCounted
425431
}
426432
}
427433
}
428-
void removeAssetWriter(IAssetWriter* _writer)
434+
inline void removeAssetWriter(IAssetWriter* _writer)
429435
{
436+
if (!_writer)
437+
return;
430438
const uint64_t suppTypes = _writer->getSupportedAssetTypesBitfield();
431439
const char** exts = _writer->getAssociatedFileExtensions();
432440
size_t extIx = 0u;

include/nbl/asset/ICPUScene.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -181,7 +181,7 @@ class ICPUScene final : public IAsset, public IScene
181181
Count
182182
};
183183
//
184-
inline bool addEnvLight(const EEnvLightType type, core::smart_refctd_ptr<ICPUImageView>&& tex)
184+
inline bool addEnvLight(const EEnvLightType type, core::smart_refctd_ptr<const ICPUImageView>&& tex)
185185
{
186186
if (!tex)
187187
return false;
@@ -232,7 +232,7 @@ class ICPUScene final : public IAsset, public IScene
232232
//
233233
SInstanceStorage m_instances;
234234
//
235-
core::vector<core::smart_refctd_ptr<ICPUImageView>> m_envLightTexs;
235+
core::vector<core::smart_refctd_ptr<const ICPUImageView>> m_envLightTexs;
236236
core::vector<EEnvLightType> m_envLightTypes;
237237
//
238238
const uint8_t m_maxMorphTargetGeometryCountLog2;

include/nbl/asset/interchange/IAssetLoader.h

Lines changed: 87 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -177,6 +177,9 @@ class NBL_API2 IAssetLoader : public virtual core::IReferenceCounted
177177
public:
178178
NBL_API2 IAssetLoaderOverride(IAssetManager* _manager);
179179

180+
//
181+
inline IAssetManager* getManager() const {return m_manager;}
182+
180183
//!
181184
template<class AssetT>
182185
inline std::pair<core::smart_refctd_ptr<AssetT>,const IAssetMetadata*> findDefaultAsset(const std::string& inSearchKey, const SAssetLoadContext& ctx, const uint32_t hierarchyLevel)
@@ -270,7 +273,7 @@ class NBL_API2 IAssetLoader : public virtual core::IReferenceCounted
270273

271274
//! After a successful load of an asset or sub-asset
272275
//TODO change name
273-
virtual void insertAssetIntoCache(SAssetBundle& asset, const std::string& supposedKey, const SAssetLoadContext& ctx, const uint32_t hierarchyLevel);
276+
virtual void insertAssetIntoCache(SAssetBundle& asset, const std::string& supposedKey, const SAssetLoadParams& _params, const uint32_t hierarchyLevel);
274277
};
275278

276279
public:
@@ -291,25 +294,105 @@ class NBL_API2 IAssetLoader : public virtual core::IReferenceCounted
291294
//! Loads an asset from an opened file, returns nullptr in case of failure.
292295
virtual SAssetBundle loadAsset(system::IFile* _file, const SAssetLoadParams& _params, IAssetLoaderOverride* _override, uint32_t _hierarchyLevel = 0u) = 0;
293296

297+
//
294298
virtual void initialize() {}
295299

300+
//
301+
static core::smart_refctd_ptr<ICPUImageView> createDefaultImageView(core::smart_refctd_ptr<asset::ICPUImage>&& image);
302+
296303
protected:
297304
// accessors for loaders
298-
SAssetBundle interm_getAssetInHierarchy(IAssetManager* _mgr, system::IFile* _file, const std::string& _supposedFilename, const IAssetLoader::SAssetLoadParams& _params, uint32_t _hierarchyLevel, IAssetLoader::IAssetLoaderOverride* _override);
299-
SAssetBundle interm_getAssetInHierarchy(IAssetManager* _mgr, const std::string& _filename, const IAssetLoader::SAssetLoadParams& _params, uint32_t _hierarchyLevel, IAssetLoader::IAssetLoaderOverride* _override);
305+
SAssetBundle interm_getAssetInHierarchy(system::IFile* _file, const std::string& _supposedFilename, const IAssetLoader::SAssetLoadParams& _params, uint32_t _hierarchyLevel, IAssetLoader::IAssetLoaderOverride* _override);
306+
SAssetBundle interm_getAssetInHierarchy(const std::string& _filename, const IAssetLoader::SAssetLoadParams& _params, uint32_t _hierarchyLevel, IAssetLoader::IAssetLoaderOverride* _override);
300307
// only the overload we use for now
301-
SAssetBundle interm_getAssetInHierarchyWithAllContent(IAssetManager* _mgr, const std::string& _filename, const IAssetLoader::SAssetLoadParams& _params, uint32_t _hierarchyLevel, IAssetLoader::IAssetLoaderOverride* _override);
308+
SAssetBundle interm_getAssetInHierarchyWithAllContent(const std::string& _filename, const IAssetLoader::SAssetLoadParams& _params, uint32_t _hierarchyLevel, IAssetLoader::IAssetLoaderOverride* _override);
302309

303310
void interm_setAssetMutability(const IAssetManager* _mgr, IAsset* _asset, const bool _val);
304311

305312
bool insertBuiltinAssetIntoCache(IAssetManager* _mgr, SAssetBundle& _asset, const std::string _path);
306313
bool insertBuiltinAssetIntoCache(IAssetManager* _mgr, core::smart_refctd_ptr<IAsset>& _asset, core::smart_refctd_ptr<IAssetMetadata>&& metadata, const std::string _path);
307314
bool insertBuiltinAssetIntoCache(IAssetManager* _mgr, core::smart_refctd_ptr<IAsset>&& _asset, core::smart_refctd_ptr<IAssetMetadata>&& metadata, const std::string _path);
308315

316+
// TODO: make static?
309317
inline void setAssetInBundle(SAssetBundle& bundle, const uint32_t offset, core::smart_refctd_ptr<IAsset>&& _asset)
310318
{
311319
bundle.setAsset(offset,std::move(_asset));
312320
}
321+
322+
//
323+
template<typename PathOrFile> requires is_any_of_v<PathOrFile,system::IFile*,std::string>
324+
SAssetBundle interm_getImageViewInHierarchy(const PathOrFile& pathOrFile, const IAssetLoader::SAssetLoadParams& _params, const uint32_t _hierarchyLevel, IAssetLoader::IAssetLoaderOverride* _override)
325+
{
326+
// TODO: first we needed to try-load to figure out the cache key to be used, but we could do it differently I guess
327+
// TODO: this load shouldn't add to cache until we exit successfully
328+
auto bundle = interm_getAssetInHierarchy(pathOrFile,_params,_hierarchyLevel,_override);
329+
auto contentRange = bundle.getContents();
330+
if (contentRange.empty())
331+
return {};
332+
if (const auto origType=bundle.getAssetType(); origType==IAsset::E_TYPE::ET_IMAGE_VIEW)
333+
return bundle;
334+
else if (origType!=IAsset::E_TYPE::ET_IMAGE)
335+
{
336+
_params.logger.log(
337+
"IAssetLoader::interm_getImageViewInHierarchy loaded assed with key \"%s\" with was of type %s not IMAGE",
338+
system::ILogger::ELL_ERROR,bundle.getCacheKey().c_str(),system::to_string(origType).c_str()
339+
);
340+
return {};
341+
}
342+
343+
const auto cacheKey = bundle.getCacheKey()+"?view?IAssetLoader?default";
344+
SAssetLoadContext ctx(_params,nullptr);
345+
// search the cache for the imageview
346+
{
347+
const asset::IAsset::E_TYPE types[]{asset::IAsset::ET_IMAGE_VIEW,asset::IAsset::ET_TERMINATING_ZERO};
348+
auto cachedBundle = _override->findCachedAsset(cacheKey,types,ctx,_hierarchyLevel);
349+
// check if found
350+
if (!cachedBundle.getContents().empty())
351+
return cachedBundle;
352+
}
353+
354+
// ok now create default views for all the images
355+
auto container = core::make_refctd_dynamic_array<SAssetBundle::contents_container_t>(contentRange.size());
356+
auto outIt = container->begin();
357+
for (auto& asset : contentRange)
358+
*(outIt++) = createDefaultImageView(core::smart_refctd_ptr_static_cast<ICPUImage>(asset));
359+
bundle = SAssetBundle(nullptr,std::move(container));
360+
_override->insertAssetIntoCache(bundle,cacheKey,_params,_hierarchyLevel);
361+
return bundle;
362+
}
363+
364+
#if 0
365+
// should we have derivative, bump and normalmap getters or shall we support all 3 in Frontend?
366+
static std::string imageViewCacheKey(const CElementTexture::Bitmap& bitmap, const CMitsubaMaterialCompilerFrontend::E_IMAGE_VIEW_SEMANTIC semantic)
367+
{
368+
std::string key = bitmap.filename.svalue;
369+
switch (semantic)
370+
{
371+
case CMitsubaMaterialCompilerFrontend::EIVS_NORMAL_MAP:
372+
key += "?deriv?n";
373+
break;
374+
case CMitsubaMaterialCompilerFrontend::EIVS_BUMP_MAP:
375+
key += "?deriv?h";
376+
{
377+
static const char* wrap[5]
378+
{
379+
"?repeat",
380+
"?mirror",
381+
"?clamp",
382+
"?zero",
383+
"?one"
384+
};
385+
key += wrap[bitmap.wrapModeU];
386+
key += wrap[bitmap.wrapModeV];
387+
}
388+
break;
389+
default:
390+
break;
391+
}
392+
key += "?view";
393+
return key;
394+
}
395+
#endif
313396
};
314397

315398
}

include/nbl/ext/MitsubaLoader/SContext.h

Lines changed: 3 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66

77

88
#include "nbl/asset/ICPUPolygonGeometry.h"
9-
//#include "nbl/asset/utils/IGeometryCreator.h"
109
#include "nbl/asset/interchange/CIESProfileLoader.h"
1110

1211
#include "nbl/ext/MitsubaLoader/CMitsubaMetadata.h"
@@ -49,63 +48,13 @@ struct SContext final
4948
core::unordered_map<const CElementShape::ShapeGroup*,group_ass_type> groupCache;
5049
//
5150
core::unordered_map<const CElementShape*,CMitsubaMetadata::SGeometryMetaPair> shapeCache;
52-
#if 0
51+
52+
#if 0 // stuff that belongs in the Material Compiler backend
5353
//image, sampler
5454
using tex_ass_type = std::tuple<core::smart_refctd_ptr<asset::ICPUImageView>,core::smart_refctd_ptr<asset::ICPUSampler>>;
55-
//image, scale
55+
//image, scale
5656
core::map<core::smart_refctd_ptr<asset::ICPUImage>,float> derivMapCache;
5757

58-
//
59-
static std::string imageViewCacheKey(const CElementTexture::Bitmap& bitmap, const CMitsubaMaterialCompilerFrontend::E_IMAGE_VIEW_SEMANTIC semantic)
60-
{
61-
std::string key = bitmap.filename.svalue;
62-
switch (bitmap.channel)
63-
{
64-
case CElementTexture::Bitmap::CHANNEL::R:
65-
key += "?rrrr";
66-
break;
67-
case CElementTexture::Bitmap::CHANNEL::G:
68-
key += "?gggg";
69-
break;
70-
case CElementTexture::Bitmap::CHANNEL::B:
71-
key += "?bbbb";
72-
break;
73-
case CElementTexture::Bitmap::CHANNEL::A:
74-
key += "?aaaa";
75-
break;
76-
default:
77-
break;
78-
}
79-
switch (semantic)
80-
{
81-
case CMitsubaMaterialCompilerFrontend::EIVS_BLEND_WEIGHT:
82-
key += "?blend";
83-
break;
84-
case CMitsubaMaterialCompilerFrontend::EIVS_NORMAL_MAP:
85-
key += "?deriv?n";
86-
break;
87-
case CMitsubaMaterialCompilerFrontend::EIVS_BUMP_MAP:
88-
key += "?deriv?h";
89-
{
90-
static const char* wrap[5]
91-
{
92-
"?repeat",
93-
"?mirror",
94-
"?clamp",
95-
"?zero",
96-
"?one"
97-
};
98-
key += wrap[bitmap.wrapModeU];
99-
key += wrap[bitmap.wrapModeV];
100-
}
101-
break;
102-
default:
103-
break;
104-
}
105-
key += "?view";
106-
return key;
107-
}
108-
10958
static asset::ISampler::SParams emissionProfileSamplerParams(const CElementEmissionProfile* profile, const asset::CIESProfileMetadata& meta)
11059
{
11160
return {

src/nbl/asset/IAssetManager.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -236,14 +236,14 @@ SAssetBundle IAssetManager::getAssetInHierarchy_impl(system::IFile* _file, const
236236
((levelFlags & IAssetLoader::ECF_DONT_CACHE_TOP_LEVEL) != IAssetLoader::ECF_DONT_CACHE_TOP_LEVEL) &&
237237
((levelFlags & IAssetLoader::ECF_DUPLICATE_TOP_LEVEL) != IAssetLoader::ECF_DUPLICATE_TOP_LEVEL))
238238
{
239-
_override->insertAssetIntoCache(bundle, filename.string(), ctx, _hierarchyLevel);
239+
_override->insertAssetIntoCache(bundle, filename.string(), ctx.params, _hierarchyLevel);
240240
}
241241
else if (bundle.getContents().empty())
242242
{
243243
bool addToCache;
244244
bundle = _override->handleLoadFail(addToCache, file.get(), filename.string(), filename.string(), ctx, _hierarchyLevel);
245245
if (!bundle.getContents().empty() && addToCache)
246-
_override->insertAssetIntoCache(bundle, filename.string(), ctx, _hierarchyLevel);
246+
_override->insertAssetIntoCache(bundle, filename.string(), ctx.params, _hierarchyLevel);
247247
}
248248
return bundle;
249249
}

src/nbl/asset/interchange/IAssetLoader.cpp

Lines changed: 47 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@
66

77
#include "nbl/asset/IAssetManager.h"
88

9-
using namespace nbl;
10-
using namespace asset;
9+
using namespace nbl::core;
10+
using namespace nbl::asset;
1111

1212
// todo NEED DOCS
1313
IAssetLoader::IAssetLoaderOverride::IAssetLoaderOverride(IAssetManager* _manager) : m_manager(_manager), m_system(m_manager->getSystem())
@@ -26,28 +26,28 @@ SAssetBundle IAssetLoader::IAssetLoaderOverride::findCachedAsset(const std::stri
2626
return chooseRelevantFromFound(found->begin(), found->end(), ctx, hierarchyLevel);
2727
}
2828

29-
void IAssetLoader::IAssetLoaderOverride::insertAssetIntoCache(SAssetBundle& asset, const std::string& supposedKey, const SAssetLoadContext& ctx, const uint32_t hierarchyLevel)
29+
void IAssetLoader::IAssetLoaderOverride::insertAssetIntoCache(SAssetBundle& asset, const std::string& supposedKey, const SAssetLoadParams& _params, const uint32_t hierarchyLevel)
3030
{
3131
m_manager->changeAssetKey(asset, supposedKey);
3232

33-
auto levelFlag = ctx.params.cacheFlags >> (uint64_t(hierarchyLevel) * 2ull);
33+
auto levelFlag = _params.cacheFlags >> (uint64_t(hierarchyLevel) * 2ull);
3434
if (!(levelFlag&ECF_DONT_CACHE_TOP_LEVEL))
3535
m_manager->insertAssetIntoCache(asset,ASSET_MUTABILITY_ON_CACHE_INSERT);
3636
}
3737

38-
SAssetBundle IAssetLoader::interm_getAssetInHierarchy(IAssetManager* _mgr, system::IFile* _file, const std::string& _supposedFilename, const IAssetLoader::SAssetLoadParams& _params, uint32_t _hierarchyLevel, IAssetLoader::IAssetLoaderOverride* _override)
38+
SAssetBundle IAssetLoader::interm_getAssetInHierarchy(system::IFile* _file, const std::string& _supposedFilename, const IAssetLoader::SAssetLoadParams& _params, uint32_t _hierarchyLevel, IAssetLoader::IAssetLoaderOverride* _override)
3939
{
40-
return _mgr->getAssetInHierarchy(_file, _supposedFilename, _params, _hierarchyLevel, _override);
40+
return _override->getManager()->getAssetInHierarchy(_file, _supposedFilename, _params, _hierarchyLevel, _override);
4141
}
4242

43-
SAssetBundle IAssetLoader::interm_getAssetInHierarchy(IAssetManager* _mgr, const std::string& _filename, const IAssetLoader::SAssetLoadParams& _params, uint32_t _hierarchyLevel, IAssetLoader::IAssetLoaderOverride* _override)
43+
SAssetBundle IAssetLoader::interm_getAssetInHierarchy(const std::string& _filename, const IAssetLoader::SAssetLoadParams& _params, uint32_t _hierarchyLevel, IAssetLoader::IAssetLoaderOverride* _override)
4444
{
45-
return _mgr->getAssetInHierarchy(_filename, _params, _hierarchyLevel, _override);
45+
return _override->getManager()->getAssetInHierarchy(_filename, _params, _hierarchyLevel, _override);
4646
}
4747

48-
SAssetBundle IAssetLoader::interm_getAssetInHierarchyWithAllContent(IAssetManager* _mgr, const std::string& _filename, const IAssetLoader::SAssetLoadParams& _params, uint32_t _hierarchyLevel, IAssetLoader::IAssetLoaderOverride* _override)
48+
SAssetBundle IAssetLoader::interm_getAssetInHierarchyWithAllContent(const std::string& _filename, const IAssetLoader::SAssetLoadParams& _params, uint32_t _hierarchyLevel, IAssetLoader::IAssetLoaderOverride* _override)
4949
{
50-
auto firstLoad = interm_getAssetInHierarchy(_mgr,_filename,_params,_hierarchyLevel,_override);
50+
auto firstLoad = interm_getAssetInHierarchy(_filename,_params,_hierarchyLevel,_override);
5151
auto bundleHasAllContent = [](const SAssetBundle& retval)->bool
5252
{
5353
for (const auto& asset : retval.getContents())
@@ -60,10 +60,10 @@ SAssetBundle IAssetLoader::interm_getAssetInHierarchyWithAllContent(IAssetManage
6060

6161
IAssetLoader::SAssetLoadParams paramCopy = _params;
6262
paramCopy.cacheFlags = ECF_DUPLICATE_REFERENCES;
63-
auto secondLoad = interm_getAssetInHierarchy(_mgr,_filename,paramCopy,_hierarchyLevel,_override);
63+
auto secondLoad = interm_getAssetInHierarchy(_filename,paramCopy,_hierarchyLevel,_override);
6464
if (bundleHasAllContent(secondLoad))
6565
{
66-
_mgr->removeAssetFromCache(firstLoad);
66+
_override->getManager()->removeAssetFromCache(firstLoad);
6767
return secondLoad;
6868
}
6969
else
@@ -83,15 +83,47 @@ bool IAssetLoader::insertBuiltinAssetIntoCache(IAssetManager* _mgr, SAssetBundle
8383

8484

8585

86-
87-
bool IAssetLoader::insertBuiltinAssetIntoCache(IAssetManager* _mgr, core::smart_refctd_ptr<IAsset>& _asset, core::smart_refctd_ptr<IAssetMetadata>&& metadata, const std::string _path)
86+
// if I can figure out the template for this, move to header
87+
bool IAssetLoader::insertBuiltinAssetIntoCache(IAssetManager* _mgr, smart_refctd_ptr<IAsset>& _asset, smart_refctd_ptr<IAssetMetadata>&& metadata, const std::string _path)
8888
{
8989
asset::SAssetBundle bundle(std::move(metadata), { _asset });
9090
return insertBuiltinAssetIntoCache(_mgr, bundle, _path);
9191
}
9292

93-
bool IAssetLoader::insertBuiltinAssetIntoCache(IAssetManager* _mgr, core::smart_refctd_ptr<IAsset>&& _asset, core::smart_refctd_ptr<IAssetMetadata>&& metadata, const std::string _path)
93+
bool IAssetLoader::insertBuiltinAssetIntoCache(IAssetManager* _mgr, smart_refctd_ptr<IAsset>&& _asset, smart_refctd_ptr<IAssetMetadata>&& metadata, const std::string _path)
9494
{
9595
asset::SAssetBundle bundle(std::move(metadata), { std::move(_asset) });
9696
return insertBuiltinAssetIntoCache(_mgr, bundle, _path);
9797
}
98+
99+
100+
smart_refctd_ptr<ICPUImageView> IAssetLoader::createDefaultImageView(core::smart_refctd_ptr<asset::ICPUImage>&& image)
101+
{
102+
if (!image)
103+
return nullptr;
104+
const auto& imageParams = image->getCreationParameters();
105+
106+
using view_type_e = IImageViewBase::E_TYPE;
107+
IImageViewBase::E_TYPE viewType;
108+
switch (imageParams.type)
109+
{
110+
case ICPUImage::ET_1D:
111+
viewType = view_type_e::ET_1D_ARRAY;
112+
break;
113+
case ICPUImage::ET_2D:
114+
viewType = view_type_e::ET_2D_ARRAY;
115+
break;
116+
case ICPUImage::ET_3D:
117+
viewType = view_type_e::ET_3D;
118+
break;
119+
default:
120+
return nullptr;
121+
}
122+
123+
return ICPUImageView::create({
124+
//.subUsages = // shall we somehow narrow in-case the image itself has extended usage?
125+
.image = std::move(image),
126+
.viewType = viewType,
127+
.format = imageParams.format
128+
});
129+
}

0 commit comments

Comments
 (0)