Vulkan Schnee 0.0.1 (22840dc8)
High-performance rendering engine
Loading...
Searching...
No Matches
TextureHandleRegistry.h
1#pragma once
2#include "TextureHandle.h"
3#include "TextureIdentity.h"
4
5#include <filesystem>
6#include <shared_mutex>
7#include <unordered_map>
8#include <unordered_set>
9#include <vector>
10
11namespace Engine::Assets
12{
13 class RuntimeTexture;
14} // namespace Engine::Assets
15
17{
18
54 {
55 public:
60
65
66 // Non-copyable, non-movable (contains mutex)
71
76
81
86
98 template <TextureType Type>
99 TypedTextureHandle<Type> getOrCreateHandle( const std::filesystem::path & path )
100 {
101 TextureHandle base = getOrCreateHandle( path, Type );
102 return TypedTextureHandle<Type>( base );
103 }
104
111 [[nodiscard]] TextureHandle getOrCreateHandle( const std::filesystem::path & path, TextureType type );
112
119 [[nodiscard]] uint32_t resolveDescriptorIndex( TextureHandle handle ) const;
120
127 [[nodiscard]] uint32_t resolveDescriptorSlot( TextureHandle handle ) const;
128
132 [[nodiscard]] bool isResident( TextureHandle handle ) const;
133
138 [[nodiscard]] RuntimeTexture * resolveTexture( TextureHandle handle ) const;
139
143 void setDescriptorSlot( TextureHandle handle, uint32_t descriptorIndex );
144
151
158 [[nodiscard]] TextureType getTextureType( TextureHandle handle ) const;
159
168 [[nodiscard]] TextureType getTextureTypeForPath( const std::filesystem::path & path ) const;
169
181 const std::filesystem::path & path,
182 TextureType type,
183 RuntimeTexture * texture,
184 uint32_t descriptorIndex
185 );
186
194 const TextureIdentityKey & identity,
195 RuntimeTexture * texture,
196 uint32_t descriptorIndex
197 );
198
206 const std::filesystem::path & path,
207 RuntimeTexture * texture,
208 uint32_t descriptorIndex
209 );
210
218 void onTextureUnloaded( const std::filesystem::path & path );
224 void onTextureUnloaded( const std::filesystem::path & path, TextureType type );
225
229 [[nodiscard]] size_t getHandleCount() const;
230
237 [[nodiscard]] std::filesystem::path getPathForHandle( TextureHandle handle ) const;
238
242 void clear();
243
244 private:
248 TextureHandle getOrCreateHandleImpl( const std::filesystem::path & path, TextureType type );
249
262
263 std::vector<Entry> entries_;
264 std::unordered_map<TextureIdentityKey, uint32_t, TextureIdentityKeyHash> identityToHandleId_;
265 std::unordered_map<std::string, std::vector<uint32_t>> pathToHandleIds_;
266 mutable std::shared_mutex mutex_;
267 };
268
269} // namespace Engine::Assets::Textures
Runtime-owned Vulkan texture resource.
Definition Texture.h:33
Type-erased texture handle for runtime storage.
static constexpr uint32_t INVALID_ID
Sentinel ID for an invalid handle.
void onTextureUnloaded(const std::filesystem::path &path, TextureType type)
Marks a registered path/type identity as not resident.
void onTextureLoaded(const std::filesystem::path &path, RuntimeTexture *texture, uint32_t descriptorIndex)
Marks a path as resident when it resolves to one registered texture type.
void setDescriptorSlot(TextureHandle handle, uint32_t descriptorIndex)
Assign a stable descriptor slot to a handle before the image is resident.
uint32_t resolveDescriptorSlot(TextureHandle handle) const
Resolve a handle to its stable descriptor slot.
TextureType getTextureTypeForPath(const std::filesystem::path &path) const
Get the texture type for a path (if registered)
TextureHandleRegistry()=default
Creates an empty texture handle registry.
void onTextureLoaded(const std::filesystem::path &path, TextureType type, RuntimeTexture *texture, uint32_t descriptorIndex)
Called when a texture finishes loading.
void onTextureUnloaded(const std::filesystem::path &path)
Called when a texture is unloaded.
void clear()
Clear all registry entries (typically called on shutdown)
TextureType getTextureType(TextureHandle handle) const
Get the texture type for a handle.
~TextureHandleRegistry()=default
Destroys the registry without owning registered RuntimeTexture objects.
void onTextureLoaded(const TextureIdentityKey &identity, RuntimeTexture *texture, uint32_t descriptorIndex)
Marks a registered texture identity as resident.
std::unordered_map< std::string, std::vector< uint32_t > > pathToHandleIds_
TextureHandleRegistry(const TextureHandleRegistry &)=delete
Copy construction is disabled because the registry contains synchronization state.
bool isResident(TextureHandle handle) const
True if the handle currently has a resident RuntimeTexture.
TextureHandleRegistry(TextureHandleRegistry &&)=delete
Move construction is disabled because handles refer to stable registry IDs.
TypedTextureHandle< Type > getOrCreateHandle(const std::filesystem::path &path)
Get or create a typed handle for a texture path.
TextureHandleRegistry & operator=(TextureHandleRegistry &&)=delete
Move assignment is disabled because handles refer to stable registry IDs.
TextureHandle getOrCreateHandle(const std::filesystem::path &path, TextureType type)
Gets or creates an untyped handle for a texture path and semantic type.
RuntimeTexture * resolveTexture(TextureHandle handle) const
Resolves a resident handle to its runtime Vulkan texture.
std::unordered_map< TextureIdentityKey, uint32_t, TextureIdentityKeyHash > identityToHandleId_
TextureIdentityKey getIdentityForHandle(TextureHandle handle) const
Gets the path/type identity associated with a handle.
size_t getHandleCount() const
Get the total number of registered texture handles.
TextureHandleRegistry & operator=(const TextureHandleRegistry &)=delete
Copy assignment is disabled because registry entries must remain stable.
TextureHandle getOrCreateHandleImpl(const std::filesystem::path &path, TextureType type)
Internal implementation of getOrCreateHandle.
uint32_t resolveDescriptorIndex(TextureHandle handle) const
Resolve a handle to its descriptor index (O(1) operation)
std::filesystem::path getPathForHandle(TextureHandle handle) const
Get the path associated with a handle (for debugging/validation)
Type-safe texture handle wrapper providing compile-time type safety.
TextureType
Represents the semantic type of a texture, determining its Vulkan format.
Definition TextureType.h:15
@ Unknown
Fallback type, uses SRGB as default.
Definition TextureType.h:21
Path and semantic type pair that uniquely identifies a texture handle.