MayaFlux 0.3.0
Digital-First Multimedia Processing Framework
Loading...
Searching...
No Matches
VKEnumUtils.hpp
Go to the documentation of this file.
1#pragma once
2#include <vulkan/vulkan.hpp>
3
5
6namespace MayaFlux::Core {
7
8/**
9 * @struct SurfaceFormatTraits
10 * @brief DataVariant dispatch descriptor for a swapchain surface format.
11 *
12 * Byte count is intentionally absent — callers derive it from
13 * vk_format_bytes_per_pixel(to_vk_format(fmt)) to avoid duplication.
14 * Only the fields that cannot be trivially derived from the vk::Format
15 * alone are stored here: the element-type discriminants needed to choose
16 * the correct DataVariant alternative.
17 */
19 uint32_t channel_count; ///< Number of colour channels.
20 uint32_t bits_per_channel; ///< Bits per individual channel.
21 bool is_float; ///< True for SFLOAT formats.
22 bool is_packed; ///< True for packed formats (e.g. A2B10G10R10).
23};
24
26{
28 switch (fmt) {
29 case SF::B8G8R8A8_SRGB:
30 return vk::Format::eB8G8R8A8Srgb;
31 case SF::R8G8B8A8_SRGB:
32 return vk::Format::eR8G8B8A8Srgb;
33 case SF::B8G8R8A8_UNORM:
34 return vk::Format::eB8G8R8A8Unorm;
35 case SF::R8G8B8A8_UNORM:
36 return vk::Format::eR8G8B8A8Unorm;
37 case SF::R16G16B16A16_SFLOAT:
38 return vk::Format::eR16G16B16A16Sfloat;
39 case SF::A2B10G10R10_UNORM:
40 return vk::Format::eA2B10G10R10UnormPack32;
41 case SF::R32G32B32A32_SFLOAT:
42 return vk::Format::eR32G32B32A32Sfloat;
43 default:
44 return vk::Format::eB8G8R8A8Srgb;
45 }
46}
47
49{
51 switch (space) {
52 case CS::SRGB_NONLINEAR:
53 return vk::ColorSpaceKHR::eSrgbNonlinear;
54 case CS::EXTENDED_SRGB:
55 return vk::ColorSpaceKHR::eExtendedSrgbLinearEXT;
56 case CS::HDR10_ST2084:
57 return vk::ColorSpaceKHR::eHdr10St2084EXT;
58 case CS::DISPLAY_P3:
59 return vk::ColorSpaceKHR::eDisplayP3NonlinearEXT;
60 default:
61 return vk::ColorSpaceKHR::eSrgbNonlinear;
62 }
63}
64
66{
68 switch (mode) {
69 case PM::IMMEDIATE:
70 return vk::PresentModeKHR::eImmediate;
71 case PM::MAILBOX:
72 return vk::PresentModeKHR::eMailbox;
73 case PM::FIFO:
74 return vk::PresentModeKHR::eFifo;
75 case PM::FIFO_RELAXED:
76 return vk::PresentModeKHR::eFifoRelaxed;
77 default:
78 return vk::PresentModeKHR::eFifo;
79 }
80}
81
82/**
83 * @brief Byte width of a single pixel for a given Vulkan format.
84 *
85 * Covers all formats currently tracked in GraphicsSurfaceInfo plus the full
86 * set used by VKImage (depth, stencil, compute). Returns 4 with a warning for
87 * unknown formats, matching the previous VKImage fallback behaviour.
88 *
89 * @param fmt Vulkan format.
90 * @return Bytes per pixel, or 4 on unknown formats.
91 */
92inline uint32_t vk_format_bytes_per_pixel(vk::Format fmt)
93{
94 switch (fmt) {
95 case vk::Format::eR8Unorm:
96 case vk::Format::eR8Snorm:
97 case vk::Format::eR8Uint:
98 case vk::Format::eR8Sint:
99 return 1U;
100
101 case vk::Format::eR8G8Unorm:
102 case vk::Format::eR8G8Snorm:
103 case vk::Format::eR8G8Uint:
104 case vk::Format::eR8G8Sint:
105 case vk::Format::eR16Unorm:
106 case vk::Format::eR16Snorm:
107 case vk::Format::eR16Uint:
108 case vk::Format::eR16Sint:
109 case vk::Format::eR16Sfloat:
110 return 2U;
111
112 case vk::Format::eR8G8B8Unorm:
113 case vk::Format::eR8G8B8Snorm:
114 case vk::Format::eR8G8B8Uint:
115 case vk::Format::eR8G8B8Sint:
116 case vk::Format::eB8G8R8Unorm:
117 return 3U;
118
119 case vk::Format::eR8G8B8A8Unorm:
120 case vk::Format::eR8G8B8A8Snorm:
121 case vk::Format::eR8G8B8A8Uint:
122 case vk::Format::eR8G8B8A8Sint:
123 case vk::Format::eR8G8B8A8Srgb:
124 case vk::Format::eB8G8R8A8Unorm:
125 case vk::Format::eB8G8R8A8Srgb:
126 case vk::Format::eR16G16Unorm:
127 case vk::Format::eR16G16Snorm:
128 case vk::Format::eR16G16Uint:
129 case vk::Format::eR16G16Sint:
130 case vk::Format::eR16G16Sfloat:
131 case vk::Format::eR32Uint:
132 case vk::Format::eR32Sint:
133 case vk::Format::eR32Sfloat:
134 case vk::Format::eD24UnormS8Uint:
135 case vk::Format::eD32Sfloat:
136 case vk::Format::eA2B10G10R10UnormPack32:
137 return 4U;
138
139 case vk::Format::eR16G16B16A16Unorm:
140 case vk::Format::eR16G16B16A16Snorm:
141 case vk::Format::eR16G16B16A16Uint:
142 case vk::Format::eR16G16B16A16Sint:
143 case vk::Format::eR16G16B16A16Sfloat:
144 case vk::Format::eR32G32Uint:
145 case vk::Format::eR32G32Sint:
146 case vk::Format::eR32G32Sfloat:
147 return 8U;
148
149 case vk::Format::eR32G32B32Uint:
150 case vk::Format::eR32G32B32Sint:
151 case vk::Format::eR32G32B32Sfloat:
152 return 12U;
153
154 case vk::Format::eR32G32B32A32Uint:
155 case vk::Format::eR32G32B32A32Sint:
156 case vk::Format::eR32G32B32A32Sfloat:
157 return 16U;
158
159 default:
160 return 4U;
161 }
162}
163
164/**
165 * @brief Reverse-map a vk::Format to the nearest GraphicsSurfaceInfo::SurfaceFormat.
166 * Returns B8G8R8A8_SRGB as a safe fallback for unmapped formats.
167 * @param fmt Vulkan format obtained from the live swapchain.
168 * @return Closest MayaFlux surface format enum value.
169 */
171{
173 switch (fmt) {
174 case vk::Format::eB8G8R8A8Srgb:
175 return SF::B8G8R8A8_SRGB;
176 case vk::Format::eR8G8B8A8Srgb:
177 return SF::R8G8B8A8_SRGB;
178 case vk::Format::eB8G8R8A8Unorm:
179 return SF::B8G8R8A8_UNORM;
180 case vk::Format::eR8G8B8A8Unorm:
181 return SF::R8G8B8A8_UNORM;
182 case vk::Format::eR16G16B16A16Sfloat:
183 return SF::R16G16B16A16_SFLOAT;
184 case vk::Format::eA2B10G10R10UnormPack32:
185 return SF::A2B10G10R10_UNORM;
186 case vk::Format::eR32G32B32A32Sfloat:
187 return SF::R32G32B32A32_SFLOAT;
188 default:
189 return SF::B8G8R8A8_SRGB;
190 }
191}
192
193/**
194 * @brief Query DataVariant-dispatch traits for a surface format.
195 * For the byte count, call vk_format_bytes_per_pixel(to_vk_format(fmt)).
196 * @param fmt The MayaFlux surface format enum value.
197 * @return SurfaceFormatTraits for that format.
198 */
201{
203 switch (fmt) {
204 case SF::B8G8R8A8_SRGB:
205 case SF::R8G8B8A8_SRGB:
206 case SF::B8G8R8A8_UNORM:
207 case SF::R8G8B8A8_UNORM:
208 return {
209 .channel_count = 4U,
210 .bits_per_channel = 8U,
211 .is_float = false,
212 .is_packed = false
213 };
214
215 case SF::R16G16B16A16_SFLOAT:
216 return {
217 .channel_count = 4U,
218 .bits_per_channel = 16U,
219 .is_float = true,
220 .is_packed = false
221 };
222
223 case SF::A2B10G10R10_UNORM:
224 return {
225 .channel_count = 4U,
226 .bits_per_channel = 10U,
227 .is_float = false,
228 .is_packed = true
229 };
230
231 case SF::R32G32B32A32_SFLOAT:
232 return {
233 .channel_count = 4U,
234 .bits_per_channel = 32U,
235 .is_float = true,
236 .is_packed = false
237 };
238
239 default:
240 return {
241 .channel_count = 4U,
242 .bits_per_channel = 8U,
243 .is_float = false,
244 .is_packed = false
245 };
246 }
247}
248
249} // namespace MF::VulkanEnumTranslator
GraphicsSurfaceInfo::SurfaceFormat from_vk_format(vk::Format fmt)
Reverse-map a vk::Format to the nearest GraphicsSurfaceInfo::SurfaceFormat.
vk::Format to_vk_format(GraphicsSurfaceInfo::SurfaceFormat fmt)
SurfaceFormatTraits get_surface_format_traits(GraphicsSurfaceInfo::SurfaceFormat fmt)
Query DataVariant-dispatch traits for a surface format.
uint32_t vk_format_bytes_per_pixel(vk::Format fmt)
Byte width of a single pixel for a given Vulkan format.
vk::PresentModeKHR to_vk_present_mode(GraphicsSurfaceInfo::PresentMode mode)
vk::ColorSpaceKHR to_vk_color_space(GraphicsSurfaceInfo::ColorSpace space)
ColorSpace
Default color space for window surfaces.
SurfaceFormat
Default pixel format for window surfaces (Vulkan-compatible)
PresentMode
Frame presentation strategy.
uint32_t bits_per_channel
Bits per individual channel.
bool is_float
True for SFLOAT formats.
uint32_t channel_count
Number of colour channels.
bool is_packed
True for packed formats (e.g. A2B10G10R10).
DataVariant dispatch descriptor for a swapchain surface format.