Skip to content

Commit 4316c44

Browse files
Merge pull request #71 from Devsh-Graphics-Programming/radix_sort
Asset Reload Fixes
2 parents 1f5f6c1 + 82a6515 commit 4316c44

File tree

5 files changed

+31
-18
lines changed

5 files changed

+31
-18
lines changed

examples_tests/18.MitsubaLoader/main.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -212,7 +212,7 @@ int main()
212212
asset::CQuantNormalCache* qnc = am->getMeshManipulator()->getQuantNormalCache();
213213

214214
auto serializedLoader = core::make_smart_refctd_ptr<nbl::ext::MitsubaLoader::CSerializedLoader>(am);
215-
auto mitsubaLoader = core::make_smart_refctd_ptr<nbl::ext::MitsubaLoader::CMitsubaLoader>(am, fs);
215+
auto mitsubaLoader = core::make_smart_refctd_ptr<nbl::ext::MitsubaLoader::CMitsubaLoader>(am,fs);
216216
serializedLoader->initialize();
217217
mitsubaLoader->initialize();
218218
am->addAssetLoader(std::move(serializedLoader));

include/nbl/asset/IAssetManager.h

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -280,7 +280,7 @@ class IAssetManager : public core::IReferenceCounted, public core::QuitSignallin
280280

281281
if (restoreLevels)
282282
{
283-
auto reloadParams = _params;
283+
auto reloadParams = IAssetLoader::SAssetLoadParams(_params, true);
284284
{
285285
using flags_t = std::underlying_type_t<IAssetLoader::E_CACHING_FLAGS>;
286286
constexpr uint32_t bitdepth = sizeof(flags_t)*8u;
@@ -290,25 +290,25 @@ class IAssetManager : public core::IReferenceCounted, public core::QuitSignallin
290290
// set flags for levels [_hierLevel,_hierLevel+_restoreLevels) to dont look into cache and dont put into cache
291291
reloadFlags = core::bitfieldInsert<flags_t>(reloadFlags, IAssetLoader::ECF_DUPLICATE_REFERENCES, _hierarchyLevel*2u, restoreLevels*2u);
292292
reloadParams.cacheFlags = static_cast<IAssetLoader::E_CACHING_FLAGS>(reloadFlags);
293-
294293
reloadParams.restoreLevels = 0u; // make sure it wont turn into infinite recursion
295-
reloadParams.reload = true; // TODO (consider): alternative to this flag: another method in override just to let user choose asset for restore
296294
}
297295

298296
auto reloadBundle = getAssetInHierarchy_impl<RestoreWholeBundle>(_file, _supposedFilename, reloadParams, _hierarchyLevel, _override);
299297

300-
if constexpr (RestoreWholeBundle)
298+
if constexpr (!RestoreWholeBundle)
301299
{
302300
IAssetLoader::SAssetLoadContext ctx(params, file);
303301
auto asset = _override->chooseDefaultAsset(bundle, ctx);
304302

305-
// user responsible for checking if assets he wanted to be restored are in fact restored
306-
_override->handleRestore(std::move(asset), bundle, reloadBundle, restoreLevels);
303+
auto newChosenAsset = _override->handleRestore(std::move(asset), bundle, reloadBundle, restoreLevels);
304+
if (!newChosenAsset || newChosenAsset->isAnyDependencyDummy(restoreLevels))
305+
return {};
307306
}
308307
else
309308
{
310-
// user responsible for checking if assets he wanted to be restored are in fact restored
311309
_override->handleRestore(bundle, reloadBundle, restoreLevels);
310+
if (!whole_bundle_not_dummy(bundle))
311+
return {};
312312
}
313313
}
314314

@@ -528,7 +528,7 @@ class IAssetManager : public core::IReferenceCounted, public core::QuitSignallin
528528

529529
//! Remove an asset from cache (calls the private methods of IAsset behind the scenes)
530530
//TODO change key
531-
bool removeAssetFromCache(SAssetBundle& _asset) //will actually look up by asset’s key instead
531+
bool removeAssetFromCache(SAssetBundle& _asset) //will actually look up by asset’s key instead
532532
{
533533
const uint32_t ix = IAsset::typeFlagToIndex(_asset.getAssetType());
534534
return m_assetCache[ix]->removeObject(_asset, _asset.getCacheKey());

include/nbl/asset/interchange/IAssetLoader.h

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -103,15 +103,26 @@ class IAssetLoader : public virtual core::IReferenceCounted
103103
{
104104
}
105105

106+
SAssetLoadParams(const SAssetLoadParams& rhs, bool _reload = false) :
107+
decryptionKeyLen(rhs.decryptionKeyLen),
108+
decryptionKey(rhs.decryptionKey),
109+
cacheFlags(rhs.cacheFlags),
110+
relativeDir(rhs.relativeDir),
111+
loaderFlags(rhs.loaderFlags),
112+
meshManipulatorOverride(rhs.meshManipulatorOverride),
113+
restoreLevels(rhs.restoreLevels),
114+
reload(_reload)
115+
{
116+
}
117+
106118
size_t decryptionKeyLen;
107119
const uint8_t* decryptionKey;
108120
E_CACHING_FLAGS cacheFlags;
109121
const char* relativeDir;
110122
E_LOADER_PARAMETER_FLAGS loaderFlags; //!< Flags having an impact on extraordinary tasks during loading process
111123
IMeshManipulator* meshManipulatorOverride = nullptr; //!< pointer used for specifying custom mesh manipulator to use, if nullptr - default mesh manipulator will be used
112124
uint32_t restoreLevels = 0u;
113-
// user should not ever write to this flag
114-
bool reload = false;
125+
const bool reload = false;
115126
};
116127

117128
//! Struct for keeping the state of the current loadoperation for safe threading
@@ -267,7 +278,7 @@ class IAssetLoader : public virtual core::IReferenceCounted
267278

268279
//! Restores only the chosen asset
269280
//! The asset is chosen via chooseDefaultAsset()
270-
virtual void handleRestore(core::smart_refctd_ptr<IAsset>&& _chosenAsset, SAssetBundle& _bundle, SAssetBundle& _reloadedBundle, uint32_t _restoreLevels);
281+
virtual core::smart_refctd_ptr<IAsset> handleRestore(core::smart_refctd_ptr<IAsset>&& _chosenAsset, SAssetBundle& _bundle, SAssetBundle& _reloadedBundle, uint32_t _restoreLevels);
271282

272283
//! Restores all of assets in _bundle
273284
virtual void handleRestore(SAssetBundle& _bundle, SAssetBundle& _reloadedBundle, uint32_t _restoreLevels);

src/nbl/asset/interchange/IAssetLoader.cpp

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -35,22 +35,24 @@ void IAssetLoader::IAssetLoaderOverride::insertAssetIntoCache(SAssetBundle& asse
3535
m_manager->insertAssetIntoCache(asset, ASSET_MUTABILITY_ON_CACHE_INSERT);
3636
}
3737

38-
void IAssetLoader::IAssetLoaderOverride::handleRestore(core::smart_refctd_ptr<IAsset>&& _chosenAsset, SAssetBundle& _bundle, SAssetBundle& _reloadedBundle, uint32_t _restoreLevels)
38+
core::smart_refctd_ptr<IAsset> IAssetLoader::IAssetLoaderOverride::handleRestore(core::smart_refctd_ptr<IAsset>&& _chosenAsset, SAssetBundle& _bundle, SAssetBundle& _reloadedBundle, uint32_t _restoreLevels)
3939
{
4040
if (_bundle.getContents().size() != _reloadedBundle.getContents().size())
41-
return;
41+
return nullptr;
4242

4343
auto dummies = _bundle.getContents();
4444
auto found_it = std::find(dummies.begin(), dummies.end(), _chosenAsset);
4545
if (found_it == dummies.end())
46-
return;
46+
return nullptr;
4747
const uint32_t ix = found_it - dummies.begin();
4848

4949
auto reloaded = _reloadedBundle.getContents();
50-
if (dummies.begin()[ix]->isADummyObjectForCache() && !dummies.begin()[ix]->canBeRestoredFrom(reloaded.begin()[ix].get()))
51-
return;
50+
if (_chosenAsset->isADummyObjectForCache() && !_chosenAsset->canBeRestoredFrom(reloaded.begin()[ix].get()))
51+
return nullptr;
5252

5353
_chosenAsset->restoreFromDummy(reloaded.begin()[ix].get(), _restoreLevels);
54+
55+
return _chosenAsset;
5456
}
5557

5658
void IAssetLoader::IAssetLoaderOverride::handleRestore(SAssetBundle& _bundle, SAssetBundle& _reloadedBundle, uint32_t _restoreLevels)

src/nbl/ext/MitsubaLoader/CMitsubaLoader.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -951,7 +951,7 @@ SContext::tex_ass_type CMitsubaLoader::cacheTexture(SContext& ctx, uint32_t hier
951951
if (_restore && image->isADummyObjectForCache())
952952
{
953953
auto loadParams = ctx.inner.params;
954-
loadParams.restoreLevels = hierarchyLevel + 2u;
954+
loadParams.restoreLevels = std::max(loadParams.restoreLevels, hierarchyLevel + 2u);
955955
// this will restore the image being kept by found `view`
956956
auto bundle = interm_getAssetInHierarchy(m_assetMgr, tex->bitmap.filename.svalue, loadParams, hierarchyLevel, ctx.override_);
957957
if (bundle.getContents().empty() || image->isADummyObjectForCache())

0 commit comments

Comments
 (0)