Skip to content

armgit / godot

Public Imported from GitHub
0

Godot Engine – Multi-platform 2D and 3D game engine

24.7 KB
Raw Blame
/**************************************************************************/
/*  texture_streaming.h                                                   */
/**************************************************************************/
/*                         This file is part of:                          */
/*                             GODOT ENGINE                               */
/*                        https://godotengine.org                         */
/**************************************************************************/
/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur.                  */
/*                                                                        */
/* Permission is hereby granted, free of charge, to any person obtaining  */
/* a copy of this software and associated documentation files (the        */
/* "Software"), to deal in the Software without restriction, including    */
/* without limitation the rights to use, copy, modify, merge, publish,    */
/* distribute, sublicense, and/or sell copies of the Software, and to     */
/* permit persons to whom the Software is furnished to do so, subject to  */
/* the following conditions:                                              */
/*                                                                        */
/* The above copyright notice and this permission notice shall be         */
/* included in all copies or substantial portions of the Software.        */
/*                                                                        */
/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,        */
/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF     */
/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY   */
/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,   */
/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE      */
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.                 */
/**************************************************************************/

#pragma once

#include "modules/modules_enabled.gen.h"

#ifdef MODULE_TEXTURE_STREAMING_ENABLED

#include "core/io/image.h"
#include "core/os/condition_variable.h"
#include "core/os/thread.h"
#include "core/templates/rid_owner.h"
#include "core/templates/tuple.h"

// TextureStreaming singleton.
// Thread safety notes:
// - Public API methods are thread-safe and can be called from any thread.
// - Internal _impl methods usually run on the feedback processing thread, but may run on the caller thread before streaming starts.
// - Methods with ERR_NOT_ON_RENDER_THREAD must be called from the render thread.
// - I/O operations run on a dedicated texture reload thread.
class TextureStreaming : public Object {
	GDCLASS(TextureStreaming, Object);

	static TextureStreaming *singleton;

	// Sentinel value indicating no valid mip level (uninitialized or no feedback)
	static constexpr uint8_t INVALID_MIP = 255;

public:
	static constexpr uint8_t MIN_LOD_LEVEL = 0;
	static constexpr uint8_t MAX_LOD_LEVEL = 13;

	// Overrides are 1 higher, with zero meaning no override.
	static constexpr uint8_t LOD_NO_OVERRIDE = 0;
	static constexpr uint8_t MAX_LOD_OVERRIDE = MAX_LOD_LEVEL + 1;

protected:
	static void _bind_methods();

private:
	// Single-producer, single-consumer command queue used by the feedback and I/O threads.
	class SimpleCommandQueue {
		static const size_t MAX_COMMAND_SIZE = 1024;

		struct CommandBase {
			virtual void call() = 0;
			virtual ~CommandBase() = default;
		};

		template <typename T, typename M, typename... Args>
		struct Command : public CommandBase {
			T *instance;
			M method;
			Tuple<std::decay_t<Args>...> args;

			template <typename... FwdArgs>
			_FORCE_INLINE_ Command([[maybe_unused]] const char *p_method_name, T *p_instance, M p_method, FwdArgs &&...p_args) :
					instance(p_instance), method(p_method), args(std::forward<FwdArgs>(p_args)...) {
			}

			void call() override {
				call_impl(BuildIndexSequence<sizeof...(Args)>{});
			}

		private:
			template <size_t... I>
			_FORCE_INLINE_ void call_impl(IndexSequence<I...>) {
				(instance->*method)(std::move(get<I>())...);
			}

			template <size_t I>
			_FORCE_INLINE_ auto &get() { return ::tuple_get<I>(args); }
		};

		static const uint32_t DEFAULT_COMMAND_MEM_SIZE_KB = 64;

		mutable BinaryMutex mutex;
		mutable ConditionVariable condvar;
		LocalVector<uint8_t> command_mem;
		bool pending = false; // Always accessed under `mutex`; no atomic needed.
		bool should_exit = false; // Always accessed under `mutex`; no atomic needed.

		template <typename T, typename... Args>
		_FORCE_INLINE_ void create_command([[maybe_unused]] const char *p_method_name, Args &&...p_args) {
			constexpr uint64_t alloc_size = ((sizeof(T) + 8U - 1U) & ~(8U - 1U));
			static_assert(alloc_size < UINT32_MAX, "Type too large to fit in the command queue.");

			uint64_t size = command_mem.size();
			command_mem.resize(size + alloc_size + sizeof(uint64_t));
			*(uint64_t *)&command_mem[size] = alloc_size;
			void *cmd = &command_mem[size + sizeof(uint64_t)];
			new (cmd) T(p_method_name, std::forward<Args>(p_args)...);
		}

		void _flush_locked(MutexLock<BinaryMutex> &p_lock) {
			if (!pending) {
				return;
			}

			// Buffer to copy command into before unlocking.
			// This protects against reallocs invalidating the pointer during execution.
			char cmd_buffer[MAX_COMMAND_SIZE];

			uint64_t read_ptr = 0;
			while (read_ptr < command_mem.size()) {
				uint64_t size = *(uint64_t *)&command_mem[read_ptr];
				read_ptr += sizeof(uint64_t);

				CommandBase *cmd = reinterpret_cast<CommandBase *>(&command_mem[read_ptr]);

				// Copy command to local buffer so we can safely unlock during execution.
				// The copy is shallow - complex types like Callable share internal data with the original.
				memcpy(cmd_buffer, (char *)cmd, size);

				read_ptr += size;

				// Unlock while executing the command (allows new commands to be pushed)
				p_lock.temp_unlock();
				reinterpret_cast<CommandBase *>(cmd_buffer)->call();
				p_lock.temp_relock();

				// Destroy the ORIGINAL after execution completes.
				// We must not destroy before call() because the backup is a shallow copy
				// that shares internal data (e.g., Callable's bound arguments).
				// Handle potential realloc that may have happened during unlock.
				cmd = reinterpret_cast<CommandBase *>(&command_mem[read_ptr - size]);
				cmd->~CommandBase();
			}

			command_mem.clear();
			pending = false;
		}

	public:
		// Push a command to the queue and wake the waiting thread.
		template <typename T, typename M, typename... Args>
		void push_internal(const char *p_method_name, T *p_instance, M p_method, Args &&...p_args) {
			using CommandType = Command<T, M, Args...>;
			static_assert(sizeof(CommandType) <= MAX_COMMAND_SIZE);

			MutexLock lock(mutex);
			create_command<CommandType>(p_method_name, p_instance, p_method, std::forward<Args>(p_args)...);
			pending = true;
			condvar.notify_one();
		}

		// Signal the thread to exit and wake it up.
		void request_exit() {
			MutexLock lock(mutex);
			should_exit = true;
			condvar.notify_one();
		}

		// Flush all pending commands. Call from the processing thread.
		void flush() {
			MutexLock lock(mutex);
			_flush_locked(lock);
		}

		// Wait for commands or exit signal, then flush. Returns true if should continue, false if should exit.
		// This is the main loop function for the processing thread.
		bool wait_and_flush() {
			MutexLock lock(mutex);

			// Wait until we have work or should exit
			while (!pending && !should_exit) {
				condvar.wait(lock);
			}

			if (should_exit) {
				// Flush any remaining commands before exiting
				_flush_locked(lock);
				return false;
			}

			_flush_locked(lock);
			return true;
		}

		// Reset the queue so it can be reused after request_exit().
		// Must only be called after the consumer thread has finished (wait_to_finish).
		void reset() {
			MutexLock lock(mutex);
			should_exit = false;
			pending = false;
			command_mem.clear();
		}

		SimpleCommandQueue() {
			command_mem.reserve(DEFAULT_COMMAND_MEM_SIZE_KB * 1024);
		}

		~SimpleCommandQueue() {
			// Flush any remaining commands
			flush();
		}
	};

	// Helper class to manage RID_Owner with index-based access.
	// Thread-safe version: protects index_to_rid with a mutex.
	template <typename T>
	class RID_IndexedOwner {
		static constexpr uint32_t INVALID_INDEX = 0xFFFFFFFF;

		struct Wrapper {
			T data;
			uint32_t index;
		};

		mutable RID_Owner<Wrapper, true> owner;
		mutable BinaryMutex index_mutex;
		LocalVector<RID> index_to_rid;

	public:
		_FORCE_INLINE_ T *get_or_null(const RID &p_rid) {
			// No lock needed - RID_Owner<T, true> is already thread-safe for lookups
			Wrapper *wrapper = owner.get_or_null(p_rid);
			return wrapper ? &wrapper->data : nullptr;
		}

		_FORCE_INLINE_ T *allocate(RID &p_rid) {
			MutexLock<BinaryMutex> lock(index_mutex);
			uint32_t new_index = index_to_rid.size();
			Wrapper wrapper;
			wrapper.index = new_index;
			RID rid = owner.allocate_rid();
			owner.initialize_rid(rid, wrapper);
			index_to_rid.push_back(rid);
			Wrapper *wrapper_ptr = owner.get_or_null(rid);
			p_rid = rid;
			return wrapper_ptr ? &wrapper_ptr->data : nullptr;
		}

		_FORCE_INLINE_ uint32_t get_index(const RID &p_rid) const {
			// No lock needed - we only read from owner which is thread-safe
			Wrapper *wrapper = owner.get_or_null(p_rid);
			return wrapper ? wrapper->index : INVALID_INDEX;
		}

		_FORCE_INLINE_ void free(const RID &p_rid) {
			MutexLock<BinaryMutex> lock(index_mutex);
			Wrapper *wrapper = owner.get_or_null(p_rid);
			if (wrapper) {
				uint32_t index = wrapper->index;
				uint32_t last_index = index_to_rid.size() - 1;

				if (index != last_index) {
					// Move last element to the freed slot
					RID last_rid = index_to_rid[last_index];
					index_to_rid[index] = last_rid;

					// Update the moved element's index
					Wrapper *last_wrapper = owner.get_or_null(last_rid);
					if (last_wrapper) {
						last_wrapper->index = index;
					}
				}

				index_to_rid.resize(last_index);
				owner.free(p_rid);
			}
		}

		_FORCE_INLINE_ uint32_t get_count() const {
			MutexLock<BinaryMutex> lock(index_mutex);
			return index_to_rid.size();
		}
	};

	// Per texture streaming state
	// All mip levels use the convention: 0 = full resolution, higher = lower quality
	//
	// Thread access model:
	//   feedback_mip, request_mip, smoothed_mip, requested_tick_msec,
	//   width, height, format, min_lod_override, max_lod_override — feedback thread only.
	//   fit_mip        — written by feedback thread, read by I/O thread.
	//   current_mip    — written by I/O thread (and bootstrap on feedback), read by feedback thread.
	//   pending_reload_mip — exchanged/CAS'd by both threads to coordinate reload ownership.
	//   removing        — write-once (feedback → I/O), acts as a cancellation flag.
	//
	// All inter-thread visibility for the above is guaranteed by the command-queue
	// mutexes (push → wait_and_flush). The atomics therefore only need atomicity
	// (tear-free reads/writes), not ordering, so memory_order_relaxed is used.
	struct StreamingState {
		// Configuration
		RID texture;
		uint16_t width = 0;
		uint16_t height = 0;
		Image::Format format = Image::FORMAT_MAX;

		// Settings & Constraints (mip levels)
		Callable reload_callable;
		uint8_t min_lod_override = LOD_NO_OVERRIDE; // Best quality mip level allowed
		uint8_t max_lod_override = LOD_NO_OVERRIDE; // Worst quality mip level allowed

		// Runtime state (mip levels)
		uint8_t feedback_mip = INVALID_MIP; // The mip level requested by shader feedback (INVALID_MIP = not requested)
		uint8_t request_mip = INVALID_MIP; // The mip level after clamping to best/worst
		std::atomic<uint8_t> fit_mip = INVALID_MIP; // The mip level that fits in the budget
		std::atomic<uint8_t> current_mip = INVALID_MIP; // The current mip level that has been set
		std::atomic<uint8_t> pending_reload_mip = INVALID_MIP; // Mip level queued for reload (INVALID_MIP = none pending)
		std::atomic<bool> removing{ false }; // True once removal starts; blocks new reload work and final free waits on the I/O queue.

		// NOTE: All atomic operations on these fields use memory_order_relaxed.
		// Ordering is provided by the command-queue mutexes, not by the atomics themselves.
		uint64_t requested_tick_msec = 0;

		// Helper to get the number of mip levels for this texture
		_FORCE_INLINE_ uint8_t get_mip_count() const {
			const uint32_t max_dim = MAX(width, height);
			return max_dim > 0 ? uint8_t(Math::floor(Math::log2(float(max_dim)))) + 1 : 1;
		}

		// Helper to get memory size at a specific mip level
		_FORCE_INLINE_ uint64_t get_bytes_at_mip(uint8_t p_mip) const {
			uint32_t w = MAX(1u, uint32_t(width) >> p_mip);
			uint32_t h = MAX(1u, uint32_t(height) >> p_mip);
			return Image::get_image_data_size(w, h, format, true);
		}

		StreamingState() {}

		StreamingState(const StreamingState &p_other) {
			texture = p_other.texture;
			width = p_other.width;
			height = p_other.height;
			format = p_other.format;
			reload_callable = p_other.reload_callable;
			min_lod_override = p_other.min_lod_override;
			max_lod_override = p_other.max_lod_override;
			feedback_mip = p_other.feedback_mip;
			request_mip = p_other.request_mip;
			fit_mip = p_other.fit_mip.load(std::memory_order_relaxed);
			current_mip = p_other.current_mip.load(std::memory_order_relaxed);
			pending_reload_mip = p_other.pending_reload_mip.load(std::memory_order_relaxed);
			removing = p_other.removing.load(std::memory_order_relaxed);
			requested_tick_msec = p_other.requested_tick_msec;
		}

		float smoothed_mip = 15.0f; // Start at max mip (lowest quality)
		uint64_t last_update_tick_msec = 0;

		// Tracks the lowest requested mip and lets it drift upward over time to avoid rapid changes from view jitter.
		_FORCE_INLINE_ float update(uint32_t p_mip_level, uint64_t p_current_tick_msec, float p_decay_per_msec = 0.0001f) {
			float value = float(p_mip_level);
			// Linear decay upward based on time (toward higher mip = lower quality)
			if (last_update_tick_msec > 0) {
				uint64_t delta_msec = p_current_tick_msec - last_update_tick_msec;
				smoothed_mip = MIN(15.0f, smoothed_mip + (float(delta_msec) * p_decay_per_msec));
			}
			smoothed_mip = MIN(value, smoothed_mip);
			last_update_tick_msec = p_current_tick_msec;

			return smoothed_mip;
		}
	};

	RID_Owner<StreamingState, true> streaming_info_owner;

	// Rendering feedback buffer and the RID map used to interpret its entries.
	struct MaterialFeedbackBuffer {
		RID buffer; // RID for the material feedback buffer.
		uint32_t buffer_size = 0; // Size of the buffer in bytes.
		LocalVector<RID> rid_map; // Maps indices in the buffer to texture RIDs.
		RID self;

		PackedByteArray data;

		void clear();
		void resize();

		MaterialFeedbackBuffer();
		~MaterialFeedbackBuffer();
	};
	RID_Owner<MaterialFeedbackBuffer, true> feedback_buffer_owner;

	BinaryMutex material_mutex;

	// Buffer pool management
	BinaryMutex buffer_pool_mutex;
	Vector<RID> buffer_pool;

	struct MaterialInfo {
		Vector<RID> textures;
	};
	RID_IndexedOwner<MaterialInfo> material_info_owner;

	// Settings
	bool setting_streaming_is_enabled = true;
	uint32_t setting_budget_mb = 512;
	bool setting_budget_enabled = false;
	uint8_t setting_min_lod = MIN_LOD_LEVEL; // Best quality mip level - 0 = full resolution
	uint8_t setting_max_lod = 3; // Worst quality mip level - 3 ≈ 128px for 8K texture
	uint32_t setting_max_ops_per_second = 500; // 60 fps would be ~8 ops/frame
	uint64_t setting_inactivity_decay_rate_ms = 5000; // Time per mip level of decay (ms)
	uint32_t setting_budget_mb_override = UINT32_MAX;
	uint8_t setting_min_lod_override = UINT8_MAX;
	uint8_t setting_max_lod_override = UINT8_MAX;

	void clear_overrides() {
		setting_budget_mb_override = UINT32_MAX;
		setting_min_lod_override = UINT8_MAX;
		setting_max_lod_override = UINT8_MAX;
	}

	_FORCE_INLINE_ uint32_t _get_memory_budget_mb() const { return setting_budget_mb_override != UINT32_MAX ? setting_budget_mb_override : setting_budget_mb; }
	_FORCE_INLINE_ uint8_t _get_system_min_lod() const { return setting_min_lod_override != UINT8_MAX ? setting_min_lod_override : setting_min_lod; }
	_FORCE_INLINE_ uint8_t _get_system_max_lod() const { return setting_max_lod_override != UINT8_MAX ? setting_max_lod_override : setting_max_lod; }

	// Feedback buffer processing
	Thread feedback_buffer_thread;
	uint64_t feedback_buffer_last_submit_ticks = 0;

	// Budget fitting candidate structure.
	// Tracks each texture's constraints, target mip level, and priority metrics.
	// Mip levels: 0 = full resolution (best quality), higher = lower quality
	struct FitCandidate {
		RID state_rid;
		StreamingState *state = nullptr; // Pointer to the texture's streaming state (nullptr = exhausted/skip)
		uint8_t min_lod = MIN_LOD_LEVEL; // Best quality mip level allowed
		uint8_t max_lod = MAX_LOD_LEVEL; // Worst quality mip level allowed
		uint8_t target_mip = 0; // Current target mip level (adjusted during budget fitting)
		uint64_t inactivity_msec = 0; // Time since last shader request for this texture
		uint64_t bytes = 0; // Memory footprint at target_mip (updated as target changes)
	};
	LocalVector<FitCandidate> fit_candidates; // Reused across _feedback_buffer_process calls to reduce allocations

	struct FitCandidateComparator {
		_FORCE_INLINE_ bool operator()(const FitCandidate *p_a, const FitCandidate *p_b) const {
			// Skip exhausted candidates (treat as lowest priority)
			// Exhausted = already at max_lod (lowest quality)
			if (!p_a->state || p_a->target_mip >= p_a->max_lod) {
				return false; // 'a' is exhausted, so 'b' has priority
			}

			if (!p_b->state || p_b->target_mip >= p_b->max_lod) {
				return true; // 'b' is exhausted, so 'a' has priority
			}

			// Determine if candidates are below their requested mip (higher quality than requested)
			// Note: lower mip = higher quality, so target_mip < request_mip means over-quality
			uint8_t a_requested = CLAMP(p_a->state->request_mip, p_a->min_lod, p_a->max_lod);
			uint8_t b_requested = CLAMP(p_b->state->request_mip, p_b->min_lod, p_b->max_lod);
			bool a_over_quality = p_a->target_mip < a_requested;
			bool b_over_quality = p_b->target_mip < b_requested;

			// Priority 1: Prefer reducing textures that are above their requested quality
			if (a_over_quality != b_over_quality) {
				return b_over_quality; // Inverted: if b is over-quality, a should come after b in heap
			}

			// Priority 2: Among same category, prefer longer inactivity
			if (p_a->inactivity_msec != p_b->inactivity_msec) {
				return p_a->inactivity_msec < p_b->inactivity_msec; // Inverted
			}

			// Priority 3: Among equally inactive, prefer larger memory footprint
			if (p_a->bytes != p_b->bytes) {
				return p_a->bytes < p_b->bytes; // Inverted
			}

			// Priority 4: Break ties by targeting higher quality first (lower mip = reduce first)
			if (p_a->target_mip != p_b->target_mip) {
				return p_a->target_mip > p_b->target_mip; // Inverted (lower mip should be reduced first)
			}

			// Priority 5: Final tie-breaker using stable pointer address for deterministic ordering.
			// This prevents oscillation when all other priorities are equal.
			return p_a->state < p_b->state;
		}
	};

	LocalVector<FitCandidate *> reduction_heap; // Heap of candidate pointers for efficient budget fitting

	static void _feedback_buffer_thread_func(void *p_udata);
	void _feedback_buffer_thread_main();
	void _feedback_buffer_process(uint64_t p_ticks_msec);

	// I/O thread for texture reloading - uses its own command queue
	SimpleCommandQueue io_command_queue;
	Thread texture_reload_thread;

	static void _texture_reload_thread_func(void *p_udata);
	void _texture_reload_thread_main();
	void _do_texture_reload(RID p_state_rid);
	void _texture_remove_finalize(RID p_state_rid);

	uint64_t next_io_op_ticks = 0;
	std::atomic<uint64_t> texture_streaming_total_memory = 0;

	void _feedback_handle_data(const PackedByteArray &p_array, RID p_buffer);

	RID current_feedback_buffer;
	void _feedback_frame_done_callback();
	void _feedback_frame_done_callback_render_thread();
	RID _feedback_buffer_get_next();

	std::atomic<bool> initialized{ false };
	std::atomic<uint32_t> flushing_count{ 0 };

	// Completion helper for synchronous command queue calls.
	// Used to wait for a result from a command executed on the feedback thread.
	template <typename T>
	struct Completion {
		BinaryMutex mutex;
		ConditionVariable condvar;
		bool done = false;
		T result;

		void wait() {
			MutexLock<BinaryMutex> lock(mutex);
			while (!done) {
				condvar.wait(lock);
			}
		}

		void complete(const T &p_result) {
			MutexLock<BinaryMutex> lock(mutex);
			result = p_result;
			done = true;
			condvar.notify_all();
		}
	};

	// Separate struct for void completion (commands that don't return a value).
	struct CompletionVoid {
		BinaryMutex mutex;
		ConditionVariable condvar;
		bool done = false;

		void wait() {
			MutexLock<BinaryMutex> lock(mutex);
			while (!done) {
				condvar.wait(lock);
			}
		}

		void complete() {
			MutexLock<BinaryMutex> lock(mutex);
			done = true;
			condvar.notify_all();
		}
	};

	mutable SimpleCommandQueue command_queue;

	// Push a command and wait for a return value. Runs inline if the processing thread is not active.
	template <typename R, typename T, typename M, typename... Args>
	_FORCE_INLINE_ R push_and_wait(const char *p_method_name, T *p_instance, M p_method, Args &&...p_args) {
		Completion<R> completion;
		if (!feedback_buffer_thread.is_started()) {
			(p_instance->*p_method)(&completion, std::forward<Args>(p_args)...);
		} else {
			command_queue.push_internal(p_method_name, p_instance, p_method, &completion, std::forward<Args>(p_args)...);
			completion.wait();
		}
		return completion.result;
	}

	// Push a command and wait for completion. Runs inline if the processing thread is not active.
	template <typename T, typename M, typename... Args>
	_FORCE_INLINE_ void push_and_sync(const char *p_method_name, T *p_instance, M p_method, Args &&...p_args) {
		CompletionVoid completion;
		if (!feedback_buffer_thread.is_started()) {
			(p_instance->*p_method)(&completion, std::forward<Args>(p_args)...);
		} else {
			command_queue.push_internal(p_method_name, p_instance, p_method, &completion, std::forward<Args>(p_args)...);
			completion.wait();
		}
	}

	// Internal implementations usually called on the feedback thread.
	void _texture_configure_streaming_impl(Completion<RID> *p_completion, RID p_texture, Image::Format p_format, int p_width, int p_height, int p_min_lod, int p_max_lod, const Callable &p_reload_callable);
	void _texture_update_impl(CompletionVoid *p_completion, RID p_rid, int p_width, int p_height, int p_min_lod, int p_max_lod);
	void _texture_remove_impl(CompletionVoid *p_completion, RID p_rid);

	void _process_material_feedback_buffer(MaterialFeedbackBuffer *p_mb, uint64_t p_ticks_msec);

	void _render_thread_specific_initialization();

	void _texture_replace_optimized(StreamingState *p_state, uint16_t p_resolution);

	void _on_settings_changed();

	void _start_streaming();
	void _stop_streaming();

	void _flush_texture_streaming_impl();
	void _flush_fence();
	void _emit_flush_completed();

public:
	static TextureStreaming *get_singleton();

	static void register_project_settings();

	// Feedback Buffer API
	uint32_t feedback_buffer_material_index(RID p_material);
	RID feedback_buffer_get_uniform_rid();

	// Texture API - safe from any thread
	RID texture_configure_streaming(RID p_texture, Image::Format p_format, int p_width, int p_height, int p_min_lod = 0, int p_max_lod = 0, Callable p_reload_callable = Callable());
	void texture_update(RID p_rid, int p_width, int p_height, int p_min_lod, int p_max_lod);
	void texture_remove(RID p_rid);

	// Material API
	RID material_set_textures(RID p_feedback_rid, const Vector<RID> &p_textures);

	// Status API
	uint64_t get_memory_budget_bytes_used();

	// Properties
	void set_min_lod_override(int32_t p_mip) {
		setting_min_lod_override = (p_mip >= int32_t(MIN_LOD_LEVEL) && p_mip <= int32_t(MAX_LOD_LEVEL)) ? uint8_t(p_mip) : UINT8_MAX;
	}

	int32_t get_min_lod_override() const {
		return setting_min_lod_override != UINT8_MAX ? int32_t(setting_min_lod_override) : -1;
	}

	void set_max_lod_override(int32_t p_mip) {
		setting_max_lod_override = (p_mip >= int32_t(MIN_LOD_LEVEL) && p_mip <= int32_t(MAX_LOD_LEVEL)) ? uint8_t(p_mip) : UINT8_MAX;
	}

	int32_t get_max_lod_override() const {
		return setting_max_lod_override != UINT8_MAX ? int32_t(setting_max_lod_override) : -1;
	}

	void set_memory_budget_mb_override(uint32_t p_mb) {
		setting_budget_mb_override = p_mb;
	}

	uint32_t get_memory_budget_mb_override() const {
		return setting_budget_mb_override;
	}

	void flush_texture_streaming();

	TextureStreaming();
	virtual ~TextureStreaming();
};

#endif // MODULE_TEXTURE_STREAMING_ENABLED