// Copyright (c) 2012-2020 Wojciech Figat. All rights reserved. #pragma once #include "Engine/Core/Types/DateTime.h" #include "Engine/Core/Collections/SamplesBuffer.h" #include "Config.h" class StreamingGroup; class Task; /// /// Base class for all resource types that can be dynamically streamed. /// class FLAXENGINE_API StreamableResource { protected: StreamingGroup* _group; bool _isDynamic, _isStreaming; StreamingQuality _streamingQuality; StreamableResource(StreamingGroup* group); ~StreamableResource(); public: /// /// Gets resource group /// /// Streaming Group FORCE_INLINE StreamingGroup* GetGroup() const { return _group; } /// /// Gets value indicating whenever resource can be used in dynamic streaming (otherwise use always the best quality) /// /// Is dynamic streamable FORCE_INLINE bool IsDynamic() const { #if ENABLE_RESOURCES_DYNAMIC_STREAMING return _isDynamic; #else return false; #endif } /// /// Gets resource streaming quality level /// /// Streaming Quality level FORCE_INLINE StreamingQuality GetStreamingQuality() const { return _streamingQuality; } /// /// Gets resource maximum residency level. /// /// Residency virtual int32 GetMaxResidency() const = 0; /// /// Gets resource current residency level. /// /// Residency virtual int32 GetCurrentResidency() const = 0; /// /// Gets resource allocated residency level. /// /// Residency virtual int32 GetAllocatedResidency() const = 0; /// /// Gets resource target residency level. /// /// Residency int32 GetTargetResidency() const { return Streaming.TargetResidency; } /// /// Gets a value indicating whether this resource has been allocated. /// FORCE_INLINE bool IsAllocated() const { return GetAllocatedResidency() != 0; } public: /// /// Determines whether this instance can be updated. Which means: no async streaming, no pending action in background. /// /// true if this instance can be updated; otherwise, false. virtual bool CanBeUpdated() const = 0; /// /// Updates the resource allocation to the given residency level. May not be updated now but in an async operation. /// /// The target allocation residency. /// Async task that updates resource allocation or null if already done it. Warning: need to call task start to perform allocation. virtual Task* UpdateAllocation(int32 residency) = 0; /// /// Creates streaming task (or tasks sequence) to perform resource streaming for the desire residency level. /// /// The target residency. /// Async task or tasks that update resource residency level. Must be preceded with UpdateAllocation call. virtual Task* CreateStreamingTask(int32 residency) = 0; public: // Streaming Manager cached variables struct StreamingCache { /// /// The minimum usage distance since last update (eg. mesh draw distance from camera). /// Used to calculate resource quality. /// //float MinDstSinceLastUpdate; DateTime LastUpdate; int32 TargetResidency; DateTime TargetResidencyChange; SamplesBuffer QualitySamples; StreamingCache() //: MinDstSinceLastUpdate(MAX_float) : LastUpdate(0) , TargetResidency(0) , TargetResidencyChange(0) { } }; StreamingCache Streaming; /// /// Requests the streaming update for this resource during next streaming manager update. /// void RequestStreamingUpdate() { Streaming.LastUpdate.Ticks = 0; } protected: void startStreaming(bool isDynamic); void stopStreaming(); };