Skip to content

Commit

Permalink
Fix old resource was unallocated while in use in current frame.
Browse files Browse the repository at this point in the history
  • Loading branch information
Nemirtingas committed Oct 13, 2024
1 parent 2e03d8b commit 3e14870
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 24 deletions.
11 changes: 9 additions & 2 deletions src/RendererHookInternal.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,11 +37,18 @@ void RendererHookInternal_t::_LoadResources()
if (_ResourcesToLoad.empty())
return;

for (auto& resource : _OldResourcesToUnload)
resource->UnloadOldResource();

_OldResourcesToUnload.clear();

auto batchSize = _ResourcesToLoad.size() > _BatchSize ? _BatchSize : _ResourcesToLoad.size();

for (int i = 0; i < batchSize; ++i)
{
if (_ResourcesToLoad[i]->IsLoaded())
_ResourcesToLoad[i]->Unload(false);
// Remove old resources next frame, it might be currently in use.
if (_ResourcesToLoad[i]->AttachementChanged())
_OldResourcesToUnload.emplace_back(_ResourcesToLoad[i]);

_ResourcesToLoad[i]->LoadAttachedResource();
}
Expand Down
1 change: 1 addition & 0 deletions src/RendererHookInternal.h
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ class RendererHookInternal_t : public RendererHook_t
ResourceAutoLoad_t _AutoLoad;
uint32_t _BatchSize;
std::vector<RendererResourceInternal_t*> _ResourcesToLoad;
std::vector<RendererResourceInternal_t*> _OldResourcesToUnload;

protected:
RendererHookInternal_t();
Expand Down
51 changes: 31 additions & 20 deletions src/RendererResourceInternal.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -62,19 +62,6 @@ bool RendererResourceInternal_t::_DoAutoLoad()
return _DoImmediateLoad();
}

bool RendererResourceInternal_t::_AttachementChanged()
{
return !_OldRendererResource.expired();
}

void RendererResourceInternal_t::_UnloadOldResource()
{
if (auto r = _OldRendererResource.lock())
_RendererHook->ReleaseImageResource(_OldRendererResource);

_OldRendererResource.reset();
}

void RendererResourceInternal_t::Delete()
{
delete this;
Expand Down Expand Up @@ -104,7 +91,7 @@ bool RendererResourceInternal_t::LoadAttachedResource()
{
if (IsLoaded())
{
if (!_AttachementChanged())
if (!AttachementChanged())
return true;

Unload(false);
Expand All @@ -113,7 +100,6 @@ bool RendererResourceInternal_t::LoadAttachedResource()
if (!CanBeLoaded())
return false;

_UnloadOldResource();
_RendererResource = _RendererHook->CreateImageResource(_Data, _Width, _Height);
return !_RendererResource.expired();
}
Expand All @@ -132,14 +118,26 @@ bool RendererResourceInternal_t::Load(const void* data, uint32_t width, uint32_t

uint64_t RendererResourceInternal_t::GetResourceId()
{
if (_AttachementChanged() || !IsLoaded())
if (AttachementChanged() || !IsLoaded())
{
if (!_DoAutoLoad() && !_AttachementChanged())
if (!_DoAutoLoad() && !AttachementChanged())
return 0;
}

auto r = !_AttachementChanged() ? _RendererResource.lock() : _OldRendererResource.lock();
return r != nullptr ? *r : 0;
if (IsLoaded())
{
auto r = _RendererResource.lock();
if (r != nullptr)
return *r;
}
if (AttachementChanged())
{
auto r = _OldRendererResource.lock();
if (r != nullptr)
return *r;
}

return 0;
}

uint32_t RendererResourceInternal_t::Width() const
Expand Down Expand Up @@ -176,7 +174,7 @@ void RendererResourceInternal_t::ClearAttachedResource()

void RendererResourceInternal_t::Unload(bool clearAttachedResource)
{
_UnloadOldResource();
UnloadOldResource();

if (auto r = _RendererResource.lock())
_RendererHook->ReleaseImageResource(_RendererResource);
Expand All @@ -187,4 +185,17 @@ void RendererResourceInternal_t::Unload(bool clearAttachedResource)
ClearAttachedResource();
}

bool RendererResourceInternal_t::AttachementChanged()
{
return !_OldRendererResource.expired();
}

void RendererResourceInternal_t::UnloadOldResource()
{
if (auto r = _OldRendererResource.lock())
_RendererHook->ReleaseImageResource(_OldRendererResource);

_OldRendererResource.reset();
}

}
6 changes: 4 additions & 2 deletions src/RendererResourceInternal.h
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,6 @@ class RendererResourceInternal_t : public RendererResource_t
bool _DoBatchLoad();
bool _DoImmediateLoad();
bool _DoAutoLoad();
bool _AttachementChanged();
void _UnloadOldResource();

public:
std::weak_ptr<uint64_t> _OldRendererResource;
Expand Down Expand Up @@ -81,6 +79,10 @@ class RendererResourceInternal_t : public RendererResource_t
virtual void ClearAttachedResource();

virtual void Unload(bool clearAttachedResource = true);

bool AttachementChanged();

void UnloadOldResource();
};

}

0 comments on commit 3e14870

Please sign in to comment.