MayaFlux 0.1.0
Digital-First Multimedia Processing Framework
Loading...
Searching...
No Matches
SamplerForge.hpp
Go to the documentation of this file.
1#pragma once
2
3#include <vulkan/vulkan.hpp>
4
5namespace MayaFlux::Core {
6class VulkanBackend;
7}
8
10
11enum class FilterMode : uint8_t;
12enum class AddressMode : uint8_t;
13struct SamplerConfig;
14
15/**
16 * @class SamplerForge
17 * @brief Creates and caches Vulkan samplers (Singleton)
18 *
19 * Samplers control how textures are filtered and addressed when sampled in shaders.
20 * This factory caches samplers based on configuration to avoid creating duplicates.
21 *
22 * Lifecycle:
23 * - Initialize with backend reference
24 * - Create samplers via get_or_create()
25 * - Samplers are cached and reused
26 * - Cleanup destroys all samplers
27 *
28 * Thread-safe after initialization.
29 */
30class MAYAFLUX_API SamplerForge {
31public:
33 {
34 static SamplerForge factory;
35 return factory;
36 }
37
38 SamplerForge(const SamplerForge&) = delete;
42
43 /**
44 * @brief Initialize with backend reference
45 * @param backend Vulkan backend instance
46 * @return True if initialization succeeded
47 */
48 bool initialize(const std::shared_ptr<Core::VulkanBackend>& backend);
49
50 /**
51 * @brief Shutdown and cleanup all samplers
52 */
53 void shutdown();
54
55 /**
56 * @brief Check if factory is initialized
57 */
58 [[nodiscard]] bool is_initialized() const { return m_backend != nullptr; }
59
60 /**
61 * @brief Get or create a sampler with the given configuration
62 * @param config Sampler configuration
63 * @return Vulkan sampler handle (cached)
64 *
65 * Samplers are cached - identical configs return the same sampler.
66 * All samplers are destroyed on shutdown.
67 */
68 vk::Sampler get_or_create(const SamplerConfig& config);
69
70 /**
71 * @brief Get a default linear sampler
72 * @return Sampler with linear filtering and repeat addressing
73 */
74 vk::Sampler get_default_linear();
75
76 /**
77 * @brief Get a default nearest sampler
78 * @return Sampler with nearest filtering and clamp-to-edge addressing
79 */
80 vk::Sampler get_default_nearest();
81
82 /**
83 * @brief Get an anisotropic sampler (high quality)
84 * @param max_anisotropy Maximum anisotropy level (1.0-16.0)
85 * @return Sampler with anisotropic filtering
86 */
87 vk::Sampler get_anisotropic(float max_anisotropy = 16.0F);
88
89 /**
90 * @brief Destroy a specific sampler
91 * @param sampler Sampler to destroy
92 *
93 * Removes from cache and destroys. Useful for hot-reloading.
94 */
95 void destroy_sampler(vk::Sampler sampler);
96
97 /**
98 * @brief Get number of cached samplers
99 */
100 [[nodiscard]] size_t get_sampler_count() const { return m_sampler_cache.size(); }
101
102private:
103 SamplerForge() = default;
105
106 std::shared_ptr<Core::VulkanBackend> m_backend;
107
108 // Sampler cache (config hash -> sampler)
109 std::unordered_map<size_t, vk::Sampler> m_sampler_cache;
110
111 // Helper: Create sampler from config
112 vk::Sampler create_sampler(const SamplerConfig& config);
113
114 // Helper: Hash sampler config for caching
115 static size_t hash_config(const SamplerConfig& config);
116
117 // Helper: Convert FilterMode to Vulkan filter
118 static vk::Filter to_vk_filter(FilterMode mode);
119
120 // Helper: Convert AddressMode to Vulkan address mode
121 static vk::SamplerAddressMode to_vk_address_mode(AddressMode mode);
122
123 static bool s_initialized;
124};
125
126/**
127 * @brief Convenience wrapper around SamplerForge::instance()
128 */
133
134} // namespace MayaFlux::Portal::Graphics
bool is_initialized() const
Check if factory is initialized.
std::unordered_map< size_t, vk::Sampler > m_sampler_cache
SamplerForge & operator=(const SamplerForge &)=delete
SamplerForge(SamplerForge &&)=delete
SamplerForge(const SamplerForge &)=delete
size_t get_sampler_count() const
Get number of cached samplers.
SamplerForge & operator=(SamplerForge &&)=delete
std::shared_ptr< Core::VulkanBackend > m_backend
Creates and caches Vulkan samplers (Singleton)
void initialize()
Definition main.cpp:11
void shutdown()
Shutdown Portal::Graphics subsystem.
Definition Graphics.cpp:69
AddressMode
Texture addressing mode (wrapping)
SamplerForge & get_sampler_factory()
Convenience wrapper around SamplerForge::instance()
FilterMode
Texture filtering mode.