diff --git a/Source/Engine/Streaming/StreamableResource.cpp b/Source/Engine/Streaming/StreamableResource.cpp deleted file mode 100644 index 685faff03..000000000 --- a/Source/Engine/Streaming/StreamableResource.cpp +++ /dev/null @@ -1,39 +0,0 @@ -// Copyright (c) 2012-2021 Wojciech Figat. All rights reserved. - -#include "StreamableResource.h" -#include "Streaming.h" - -StreamableResource::StreamableResource(StreamingGroup* group) - : _group(group) - , _isDynamic(true) - , _isStreaming(false) - , _streamingQuality(1.0f) -{ - ASSERT(_group != nullptr); -} - -StreamableResource::~StreamableResource() -{ - StopStreaming(); -} - -void StreamableResource::StartStreaming(bool isDynamic) -{ - _isDynamic = isDynamic; - - if (_isStreaming == false) - { - Streaming::Resources.Add(this); - _isStreaming = true; - } -} - -void StreamableResource::StopStreaming() -{ - if (_isStreaming == true) - { - Streaming::Resources.Remove(this); - Streaming = StreamingCache(); - _isStreaming = false; - } -} diff --git a/Source/Engine/Streaming/StreamableResourcesCollection.h b/Source/Engine/Streaming/StreamableResourcesCollection.h deleted file mode 100644 index a8231726d..000000000 --- a/Source/Engine/Streaming/StreamableResourcesCollection.h +++ /dev/null @@ -1,103 +0,0 @@ -// Copyright (c) 2012-2021 Wojciech Figat. All rights reserved. - -#pragma once - -#include "Engine/Core/Delegate.h" -#include "Engine/Core/Collections/Array.h" -#include "Engine/Platform/CriticalSection.h" - -class StreamableResource; - -/// -/// Container for collection of Streamable Resources. -/// -class StreamableResourcesCollection -{ -private: - - CriticalSection _locker; - Array _resources; - -public: - - /// - /// Initializes a new instance of the class. - /// - StreamableResourcesCollection() - : _resources(4096) - { - } - -public: - - /// - /// Event called on new resource added to the collection. - /// - Delegate Added; - - /// - /// Event called on new resource added from the collection. - /// - Delegate Removed; - -public: - - /// - /// Gets the amount of registered resources. - /// - /// The resources count. - int32 ResourcesCount() const - { - _locker.Lock(); - const int32 result = _resources.Count(); - _locker.Unlock(); - return result; - } - - /// - /// Gets the resource at the given index. - /// - /// The index. - /// The resource at the given index. - StreamableResource* operator[](int32 index) const - { - _locker.Lock(); - StreamableResource* result = _resources.At(index); - _locker.Unlock(); - return result; - } - -public: - - /// - /// Adds the resource to the collection. - /// - /// The resource to add. - void Add(StreamableResource* resource) - { - ASSERT(resource); - - _locker.Lock(); - ASSERT(_resources.Contains(resource) == false); - _resources.Add(resource); - _locker.Unlock(); - - Added(resource); - } - - /// - /// Removes resource from the collection. - /// - /// The resource to remove. - void Remove(StreamableResource* resource) - { - ASSERT(resource); - - _locker.Lock(); - ASSERT(_resources.Contains(resource) == true); - _resources.Remove(resource); - _locker.Unlock(); - - Removed(resource); - } -}; diff --git a/Source/Engine/Streaming/Streaming.cpp b/Source/Engine/Streaming/Streaming.cpp index a2e010105..8b879b3dd 100644 --- a/Source/Engine/Streaming/Streaming.cpp +++ b/Source/Engine/Streaming/Streaming.cpp @@ -14,8 +14,9 @@ namespace StreamingManagerImpl { DateTime LastUpdateTime(0); - CriticalSection UpdateLocker; int32 LastUpdateResourcesIndex = 0; + CriticalSection ResourcesLock; + Array Resources; } using namespace StreamingManagerImpl; @@ -33,7 +34,6 @@ public: StreamingManagerService StreamingManagerServiceInstance; -StreamableResourcesCollection Streaming::Resources; Array> Streaming::TextureGroups; void StreamingSettings::Apply() @@ -46,6 +46,44 @@ void StreamingSettings::Deserialize(DeserializeStream& stream, ISerializeModifie DESERIALIZE(TextureGroups); } +StreamableResource::StreamableResource(StreamingGroup* group) + : _group(group) + , _isDynamic(true) + , _isStreaming(false) + , _streamingQuality(1.0f) +{ + ASSERT(_group != nullptr); +} + +StreamableResource::~StreamableResource() +{ + StopStreaming(); +} + +void StreamableResource::StartStreaming(bool isDynamic) +{ + _isDynamic = isDynamic; + if (!_isStreaming) + { + _isStreaming = true; + ResourcesLock.Lock(); + Resources.Add(this); + ResourcesLock.Unlock(); + } +} + +void StreamableResource::StopStreaming() +{ + if (_isStreaming) + { + ResourcesLock.Lock(); + Resources.Remove(this); + ResourcesLock.Unlock(); + Streaming = StreamingCache(); + _isStreaming = false; + } +} + void UpdateResource(StreamableResource* resource, DateTime now) { ASSERT(resource && resource->CanBeUpdated()); @@ -143,7 +181,8 @@ void StreamingManagerService::Update() // Check if skip update auto now = DateTime::NowUTC(); auto delta = now - LastUpdateTime; - int32 resourcesCount = Streaming::Resources.ResourcesCount(); + ScopeLock lock(ResourcesLock); + const int32 resourcesCount = Resources.Count(); if (resourcesCount == 0 || delta < ManagerUpdatesInterval || GPUDevice::Instance->GetState() != GPUDevice::DeviceState::Ready) return; LastUpdateTime = now; @@ -151,7 +190,6 @@ void StreamingManagerService::Update() PROFILE_CPU(); // Start update - ScopeLock lock(UpdateLocker); int32 resourcesUpdates = Math::Min(MaxResourcesPerUpdate, resourcesCount); // Update high priority queue and then rest of the resources @@ -165,7 +203,7 @@ void StreamingManagerService::Update() LastUpdateResourcesIndex = 0; // Peek resource - const auto resource = Streaming::Resources[LastUpdateResourcesIndex]; + const auto resource = Resources[LastUpdateResourcesIndex]; // Try to update it if (now - resource->Streaming.LastUpdate >= ResourceUpdatesInterval && resource->CanBeUpdated()) @@ -177,3 +215,12 @@ void StreamingManagerService::Update() // TODO: add StreamingManager stats, update time per frame, updates per frame, etc. } + +void Streaming::RequestStreamingUpdate() +{ + PROFILE_CPU(); + ResourcesLock.Lock(); + for (auto e : Resources) + e->RequestStreamingUpdate(); + ResourcesLock.Unlock(); +} diff --git a/Source/Engine/Streaming/Streaming.h b/Source/Engine/Streaming/Streaming.h index 35766639a..2afb404f6 100644 --- a/Source/Engine/Streaming/Streaming.h +++ b/Source/Engine/Streaming/Streaming.h @@ -2,9 +2,9 @@ #pragma once +#include "Engine/Core/Collections/Array.h" #include "Engine/Scripting/ScriptingType.h" #include "TextureGroup.h" -#include "StreamableResourcesCollection.h" /// /// The content streaming service. @@ -13,13 +13,13 @@ API_CLASS(Static) class FLAXENGINE_API Streaming { DECLARE_SCRIPTING_TYPE_NO_SPAWN(Streaming); - /// - /// List with all resources - /// - static StreamableResourcesCollection Resources; - /// /// Textures streaming configuration (per-group). /// API_FIELD() static Array> TextureGroups; + + /// + /// Requests the streaming update for all the loaded resources. Use it to refresh content streaming after changing configuration. + /// + API_FUNCTION() static void RequestStreamingUpdate(); };