MayaFlux 1.0.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
5#include "TextureLoom.hpp"
6
7namespace MayaFlux::Core {
8class VulkanBackend;
9}
10
12
13enum class FilterMode : uint8_t;
14enum class AddressMode : uint8_t;
15enum class BorderColor : uint8_t;
16struct SamplerConfig;
17
18/**
19 * @brief One fit expressed for both ways of applying it.
20 *
21 * @c blits copies the source into the destination when the fit is written into an
22 * image, with layers left at zero for the caller to set. @c sampler is the
23 * addressing that gives the same result when a shader samples the source directly;
24 * the scale and offset of that sampling follow from the rectangles in @c blits.
25 */
26struct FitPlan {
27 std::vector<LayerBlit> blits;
29};
30
31/**
32 * @class SamplerForge
33 * @brief Creates and caches Vulkan samplers (Singleton)
34 *
35 * Samplers control how textures are filtered and addressed when sampled in shaders.
36 * This factory caches samplers based on configuration to avoid creating duplicates.
37 *
38 * Lifecycle:
39 * - Initialize with backend reference
40 * - Create samplers via get_or_create()
41 * - Samplers are cached and reused
42 * - Cleanup destroys all samplers
43 *
44 * Thread-safe after initialization.
45 */
46class MAYAFLUX_API SamplerForge {
47public:
49 {
50 static SamplerForge factory;
51 return factory;
52 }
53
54 SamplerForge(const SamplerForge&) = delete;
58
59 /**
60 * @brief Initialize with backend reference
61 * @param backend Vulkan backend instance
62 * @return True if initialization succeeded
63 */
64 bool initialize(const std::shared_ptr<Core::VulkanBackend>& backend);
65
66 /**
67 * @brief Shutdown and cleanup all samplers
68 */
69 void shutdown();
70
71 /**
72 * @brief Check if factory is initialized
73 */
74 [[nodiscard]] bool is_initialized() const { return m_backend != nullptr; }
75
76 /**
77 * @brief Get or create a sampler with the given configuration
78 * @param config Sampler configuration
79 * @return Vulkan sampler handle (cached)
80 *
81 * Samplers are cached - identical configs return the same sampler.
82 * All samplers are destroyed on shutdown.
83 */
84 vk::Sampler get_or_create(const SamplerConfig& config);
85
86 /**
87 * @brief Get a default linear sampler
88 * @return Sampler with linear filtering and repeat addressing
89 */
90 vk::Sampler get_default_linear();
91
92 /**
93 * @brief Get a default nearest sampler
94 * @return Sampler with nearest filtering and clamp-to-edge addressing
95 */
96 vk::Sampler get_default_nearest();
97
98 /**
99 * @brief Get an anisotropic sampler (high quality)
100 * @param max_anisotropy Maximum anisotropy level (1.0-16.0)
101 * @return Sampler with anisotropic filtering
102 */
103 vk::Sampler get_anisotropic(float max_anisotropy = 16.0F);
104
105 /**
106 * @brief Destroy a specific sampler
107 * @param sampler Sampler to destroy
108 *
109 * Removes from cache and destroys. Useful for hot-reloading.
110 */
111 void destroy_sampler(vk::Sampler sampler);
112
113 /**
114 * @brief Get number of cached samplers
115 */
116 [[nodiscard]] size_t get_sampler_count() const { return m_sampler_cache.size(); }
117
118 /**
119 * @brief Work out how a source extent maps onto a destination extent.
120 *
121 * Pure arithmetic, no device needed. Zero in any extent yields an empty plan.
122 * Tiling produces one blit per tile, so a source much smaller than the
123 * destination produces many.
124 *
125 * @param src_width Source width in pixels.
126 * @param src_height Source height in pixels.
127 * @param dst_width Destination width in pixels.
128 * @param dst_height Destination height in pixels.
129 * @param mode How the source is fitted.
130 * @param filter Filter for the blits and the sampler.
131 * @return Rectangles for an image write and the matching sampler.
132 */
133 [[nodiscard]] static FitPlan plan_fit(
134 uint32_t src_width,
135 uint32_t src_height,
136 uint32_t dst_width,
137 uint32_t dst_height,
138 FitMode mode = FitMode::STRETCH,
139 FilterMode filter = FilterMode::LINEAR);
140
141private:
142 SamplerForge() = default;
143 ~SamplerForge() { shutdown(); }
144
145 std::shared_ptr<Core::VulkanBackend> m_backend;
146
147 // Sampler cache (config hash -> sampler)
148 std::unordered_map<size_t, vk::Sampler> m_sampler_cache;
149
150 // Helper: Create sampler from config
151 vk::Sampler create_sampler(const SamplerConfig& config);
152
153 // Helper: Hash sampler config for caching
154 static size_t hash_config(const SamplerConfig& config);
155
156 // Helper: Convert FilterMode to Vulkan filter
157 static vk::Filter to_vk_filter(FilterMode mode);
158
159 // Helper: Convert AddressMode to Vulkan address mode
160 static vk::SamplerAddressMode to_vk_address_mode(AddressMode mode);
161
162 // Helper: Convert BorderColor to Vulkan border color
163 static vk::BorderColor to_vk_border_color(BorderColor color);
164
165 static bool s_initialized;
166};
167
168/**
169 * @brief Convenience wrapper around SamplerForge::instance()
170 */
175
176} // namespace MayaFlux::Portal::Graphics
glm::vec3 color
Definition Decoder.cpp:97
uint32_t mode
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:10
AddressMode
Texture addressing mode (wrapping)
SamplerForge & get_sampler_factory()
Convenience wrapper around SamplerForge::instance()
FilterMode
Texture filtering mode.
BorderColor
Color read outside the texture when an address mode is CLAMP_TO_BORDER.
FitMode
How a source extent is mapped onto a destination extent.
std::shared_ptr< Kriya::SamplingPipeline > create_sampler(const std::string &filepath, uint32_t num_samples, bool truncate, uint32_t channel, uint64_t max_dur_ms)
Construct a built SamplingPipeline from an audio file.
Definition Rigs.cpp:20
std::vector< LayerBlit > blits
One fit expressed for both ways of applying it.