Fix crash when closing handles to asset file while any asset streaming task is active for asset from that file
This commit is contained in:
@@ -45,7 +45,6 @@ StreamingTexture::StreamingTexture(ITextureOwner* parent, const String& name)
|
||||
: StreamableResource(StreamingGroups::Instance()->Textures())
|
||||
, _owner(parent)
|
||||
, _texture(nullptr)
|
||||
, _streamingTasksCount(0)
|
||||
, _isBlockCompressed(false)
|
||||
{
|
||||
ASSERT(_owner != nullptr);
|
||||
@@ -62,7 +61,7 @@ StreamingTexture::~StreamingTexture()
|
||||
{
|
||||
UnloadTexture();
|
||||
SAFE_DELETE(_texture);
|
||||
ASSERT(_streamingTasksCount == 0);
|
||||
ASSERT(_streamingTasks.Count() == 0);
|
||||
}
|
||||
|
||||
Float2 StreamingTexture::Size() const
|
||||
@@ -138,7 +137,7 @@ void StreamingTexture::UnloadTexture()
|
||||
_texture->ReleaseGPU();
|
||||
_header.MipLevels = 0;
|
||||
|
||||
ASSERT(_streamingTasksCount == 0);
|
||||
ASSERT(_streamingTasks.Count() == 0);
|
||||
}
|
||||
|
||||
uint64 StreamingTexture::GetTotalMemoryUsage() const
|
||||
@@ -173,7 +172,12 @@ bool StreamingTexture::CanBeUpdated() const
|
||||
// - is not initialized
|
||||
// - mip data uploading job running
|
||||
// - resize texture job running
|
||||
return IsInitialized() && Platform::AtomicRead(&_streamingTasksCount) == 0;
|
||||
if (IsInitialized())
|
||||
{
|
||||
ScopeLock lock(_owner->GetOwnerLocker());
|
||||
return _streamingTasks.Count() == 0;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
class StreamTextureResizeTask : public GPUTask
|
||||
@@ -189,7 +193,7 @@ public:
|
||||
, _streamingTexture(texture)
|
||||
, _newTexture(newTexture)
|
||||
{
|
||||
Platform::InterlockedIncrement(&_streamingTexture->_streamingTasksCount);
|
||||
_streamingTexture->_streamingTasks.Add(this);
|
||||
}
|
||||
|
||||
~StreamTextureResizeTask()
|
||||
@@ -222,7 +226,11 @@ protected:
|
||||
|
||||
void OnEnd() override
|
||||
{
|
||||
Platform::InterlockedDecrement(&_streamingTexture->_streamingTasksCount);
|
||||
if (_streamingTexture)
|
||||
{
|
||||
ScopeLock lock(_streamingTexture->GetOwner()->GetOwnerLocker());
|
||||
_streamingTexture->_streamingTasks.Remove(this);
|
||||
}
|
||||
|
||||
// Base
|
||||
GPUTask::OnEnd();
|
||||
@@ -318,7 +326,7 @@ public:
|
||||
, _streamingTexture(texture)
|
||||
, _dataLock(_streamingTexture->GetOwner()->LockData())
|
||||
{
|
||||
Platform::InterlockedIncrement(&_streamingTexture->_streamingTasksCount);
|
||||
_streamingTexture->_streamingTasks.Add(this);
|
||||
_texture.OnUnload.Bind<StreamTextureMipTask, &StreamTextureMipTask::onResourceUnload2>(this);
|
||||
}
|
||||
|
||||
@@ -328,7 +336,8 @@ private:
|
||||
// Unlink texture
|
||||
if (_streamingTexture)
|
||||
{
|
||||
Platform::InterlockedDecrement(&_streamingTexture->_streamingTasksCount);
|
||||
ScopeLock lock(_streamingTexture->GetOwner()->GetOwnerLocker());
|
||||
_streamingTexture->_streamingTasks.Remove(this);
|
||||
_streamingTexture = nullptr;
|
||||
}
|
||||
}
|
||||
@@ -379,7 +388,8 @@ protected:
|
||||
_dataLock.Release();
|
||||
if (_streamingTexture)
|
||||
{
|
||||
Platform::InterlockedDecrement(&_streamingTexture->_streamingTasksCount);
|
||||
ScopeLock lock(_streamingTexture->GetOwner()->GetOwnerLocker());
|
||||
_streamingTexture->_streamingTasks.Remove(this);
|
||||
_streamingTexture = nullptr;
|
||||
}
|
||||
|
||||
@@ -456,3 +466,11 @@ Task* StreamingTexture::CreateStreamingTask(int32 residency)
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
void StreamingTexture::CancelStreamingTasks()
|
||||
{
|
||||
ScopeLock lock(_owner->GetOwnerLocker());
|
||||
auto tasks = _streamingTasks;
|
||||
for (auto task : tasks)
|
||||
task->Cancel();
|
||||
}
|
||||
|
||||
@@ -19,9 +19,9 @@ protected:
|
||||
ITextureOwner* _owner;
|
||||
GPUTexture* _texture;
|
||||
TextureHeader _header;
|
||||
volatile mutable int64 _streamingTasksCount;
|
||||
int32 _minMipCountBlockCompressed;
|
||||
bool _isBlockCompressed;
|
||||
Array<Task*, FixedAllocation<16>> _streamingTasks;
|
||||
|
||||
public:
|
||||
StreamingTexture(ITextureOwner* owner, const String& name);
|
||||
@@ -173,4 +173,5 @@ public:
|
||||
bool CanBeUpdated() const override;
|
||||
Task* UpdateAllocation(int32 residency) override;
|
||||
Task* CreateStreamingTask(int32 residency) override;
|
||||
void CancelStreamingTasks() override;
|
||||
};
|
||||
|
||||
@@ -636,6 +636,11 @@ bool TextureBase::Init(void* ptr)
|
||||
return Init(initData);
|
||||
}
|
||||
|
||||
void TextureBase::CancelStreaming()
|
||||
{
|
||||
_texture.CancelStreamingTasks();
|
||||
}
|
||||
|
||||
int32 TextureBase::CalculateChunkIndex(int32 mipIndex) const
|
||||
{
|
||||
// Mips are in 0-13 chunks
|
||||
|
||||
@@ -218,6 +218,9 @@ private:
|
||||
API_FUNCTION(NoProxy) bool Init(void* ptr);
|
||||
|
||||
public:
|
||||
// [BinaryAsset]
|
||||
void CancelStreaming() override;
|
||||
|
||||
// [ITextureOwner]
|
||||
CriticalSection& GetOwnerLocker() const override;
|
||||
Task* RequestMipDataAsync(int32 mipIndex) override;
|
||||
|
||||
Reference in New Issue
Block a user