Foray Library
rapid prototyping framework for crossplatform development of vulkan hardware ray tracing applications
Loading...
Searching...
No Matches
foray_imageformattraits.hpp
Go to the documentation of this file.
1#pragma once
2#include "../foray_basics.hpp"
3#include "../foray_vulkan.hpp"
4#include <limits>
5
6namespace foray::util {
7
11 template <typename COMPONENT_T, COMPONENT_T ALPHA_FALLBACK_, bool IS_FLOAT_, bool IS_SIGNED_>
13 {
14 public:
16 using COMPONENT = COMPONENT_T;
18 inline static constexpr uint32_t SIZE = sizeof(COMPONENT);
20 inline static constexpr COMPONENT ALPHA_FALLBACK = (COMPONENT)ALPHA_FALLBACK_;
22 inline static constexpr bool IS_FLOAT = IS_FLOAT_;
24 inline static constexpr bool IS_SIGNED = IS_SIGNED_;
25 };
26
28
30 using ComponentTraits_Fp16 = ComponentTraits<uint16_t, 0x3C00, true, true>; // Represented by an integer because x86 has no native support for half precision floats
32 using ComponentTraits_Fp32 = ComponentTraits<uint32_t, 0x3F800000, true, true>; // Also an integer because floats can not be passed as template parameters
34 using ComponentTraits_Fp64 = ComponentTraits<uint64_t, 0x3FF0000000000000, true, true>; // Also an integer because floats can not be passed as template parameters
39
42 ComponentTraits<uint32_t, 0b11, false, false>; // Component where the entire texel value is packed into a 32 bit value. Alpha has two bits
43
45 template <VkFormat FORMAT>
47 {
48 public:
52 inline static constexpr uint32_t COMPONENT_COUNT = 0;
54 inline static constexpr uint32_t COMPONENT_STRIDE = 4;
56 inline static constexpr uint32_t BYTESTRIDE = COMPONENT_TRAITS::SIZE * COMPONENT_COUNT;
57 };
58
60 template <typename COMPONENT_TRAITS_, uint32_t COMPONENT_COUNT_>
62
63 template <typename COMPONENT_TRAITS_>
64 class ImageFormatTraitsBase<COMPONENT_TRAITS_, 4>
65 {
66 public:
67 using COMPONENT_TRAITS = COMPONENT_TRAITS_;
68 using COMPONENT = typename COMPONENT_TRAITS_::COMPONENT;
69 inline static const uint32_t COMPONENT_COUNT = 4;
70 inline static constexpr uint32_t COMPONENT_STRIDE = 4;
71 inline static constexpr uint32_t BYTESTRIDE = COMPONENT_TRAITS::SIZE * COMPONENT_STRIDE;
72
74 inline static void WriteColor(void* out, COMPONENT r, COMPONENT g, COMPONENT b, COMPONENT a)
75 {
76 COMPONENT* data = reinterpret_cast<COMPONENT*>(out);
77 data[0] = r;
78 data[1] = g;
79 data[2] = b;
80 data[3] = a;
81 }
82
84 inline static void WriteGrayscale(void* out, COMPONENT y, COMPONENT a)
85 {
86 COMPONENT* data = reinterpret_cast<COMPONENT*>(out);
87 data[0] = y;
88 data[1] = y;
89 data[2] = y;
90 data[3] = a;
91 }
92 };
93
94 template <typename COMPONENT_TRAITS_>
95 class ImageFormatTraitsBase<COMPONENT_TRAITS_, 3>
96 {
97 public:
98 using COMPONENT_TRAITS = COMPONENT_TRAITS_;
99 using COMPONENT = typename COMPONENT_TRAITS_::COMPONENT;
100 inline static constexpr uint32_t COMPONENT_COUNT = 3;
101 inline static constexpr uint32_t COMPONENT_STRIDE = 3;
102 inline static constexpr uint32_t BYTESTRIDE = COMPONENT_TRAITS::SIZE * COMPONENT_STRIDE;
103
105 inline static void WriteColor(void* out, COMPONENT r, COMPONENT g, COMPONENT b, COMPONENT a)
106 {
107 COMPONENT* data = reinterpret_cast<COMPONENT*>(out);
108 data[0] = r;
109 data[1] = g;
110 data[2] = b;
111 }
112
114 inline static void WriteGrayscale(void* out, COMPONENT y, COMPONENT a)
115 {
116 COMPONENT* data = reinterpret_cast<COMPONENT*>(out);
117 data[0] = y;
118 data[1] = y;
119 data[2] = y;
120 }
121 };
122
123 template <typename COMPONENT_TRAITS_>
124 class ImageFormatTraitsBase<COMPONENT_TRAITS_, 2>
125 {
126 public:
127 using COMPONENT_TRAITS = COMPONENT_TRAITS_;
128 using COMPONENT = typename COMPONENT_TRAITS_::COMPONENT;
129 inline static constexpr uint32_t COMPONENT_COUNT = 2;
130 inline static constexpr uint32_t COMPONENT_STRIDE = 2;
131 inline static constexpr uint32_t BYTESTRIDE = COMPONENT_TRAITS::SIZE * COMPONENT_STRIDE;
132
134 inline static void WriteColor(void* out, COMPONENT r, COMPONENT g, COMPONENT b, COMPONENT a)
135 {
136 COMPONENT* data = reinterpret_cast<COMPONENT*>(out);
137 data[0] = r;
138 data[1] = g;
139 }
140
142 inline static void WriteGrayscale(void* out, COMPONENT y, COMPONENT a)
143 {
144 COMPONENT* data = reinterpret_cast<COMPONENT*>(out);
145 data[0] = y;
146 data[1] = y;
147 }
148 };
149
150 template <typename COMPONENT_TRAITS_>
151 class ImageFormatTraitsBase<COMPONENT_TRAITS_, 1>
152 {
153 public:
154 using COMPONENT_TRAITS = COMPONENT_TRAITS_;
155 using COMPONENT = typename COMPONENT_TRAITS_::COMPONENT;
156 inline static constexpr uint32_t COMPONENT_COUNT = 1;
157 inline static constexpr uint32_t COMPONENT_STRIDE = 1;
158 inline static constexpr uint32_t BYTESTRIDE = COMPONENT_TRAITS::SIZE * COMPONENT_STRIDE;
159
161 inline static void WriteColor(void* out, COMPONENT r, COMPONENT g, COMPONENT b, COMPONENT a)
162 {
163 COMPONENT* data = reinterpret_cast<COMPONENT*>(out);
164 data[0] = r;
165 }
166
168 inline static void WriteGrayscale(void* out, COMPONENT y, COMPONENT a)
169 {
170 COMPONENT* data = reinterpret_cast<COMPONENT*>(out);
171 data[0] = y;
172 }
173 };
174
175
176#pragma region float half
177 template <>
178 class ImageFormatTraits<VkFormat::VK_FORMAT_R16G16B16A16_SFLOAT> : public ImageFormatTraitsBase<ComponentTraits_Fp16, 4>
179 {
180 };
181 template <>
182 class ImageFormatTraits<VkFormat::VK_FORMAT_R16G16B16_SFLOAT> : public ImageFormatTraitsBase<ComponentTraits_Fp16, 3>
183 {
184 };
185 template <>
186 class ImageFormatTraits<VkFormat::VK_FORMAT_R16G16_SFLOAT> : public ImageFormatTraitsBase<ComponentTraits_Fp16, 2>
187 {
188 };
189 template <>
190 class ImageFormatTraits<VkFormat::VK_FORMAT_R16_SFLOAT> : public ImageFormatTraitsBase<ComponentTraits_Fp16, 1>
191 {
192 };
193#pragma endregion
194#pragma region float full
195 template <>
196 class ImageFormatTraits<VkFormat::VK_FORMAT_R32G32B32A32_SFLOAT> : public ImageFormatTraitsBase<ComponentTraits_Fp32, 4>
197 {
198 };
199 template <>
200 class ImageFormatTraits<VkFormat::VK_FORMAT_R32G32B32_SFLOAT> : public ImageFormatTraitsBase<ComponentTraits_Fp32, 3>
201 {
202 };
203 template <>
204 class ImageFormatTraits<VkFormat::VK_FORMAT_R32G32_SFLOAT> : public ImageFormatTraitsBase<ComponentTraits_Fp32, 2>
205 {
206 };
207 template <>
208 class ImageFormatTraits<VkFormat::VK_FORMAT_R32_SFLOAT> : public ImageFormatTraitsBase<ComponentTraits_Fp32, 1>
209 {
210 };
211#pragma endregion
212#pragma region float double
213 template <>
214 class ImageFormatTraits<VkFormat::VK_FORMAT_R64G64B64A64_SFLOAT> : public ImageFormatTraitsBase<ComponentTraits_Fp64, 4>
215 {
216 };
217 template <>
218 class ImageFormatTraits<VkFormat::VK_FORMAT_R64G64B64_SFLOAT> : public ImageFormatTraitsBase<ComponentTraits_Fp64, 3>
219 {
220 };
221 template <>
222 class ImageFormatTraits<VkFormat::VK_FORMAT_R64G64_SFLOAT> : public ImageFormatTraitsBase<ComponentTraits_Fp64, 2>
223 {
224 };
225 template <>
226 class ImageFormatTraits<VkFormat::VK_FORMAT_R64_SFLOAT> : public ImageFormatTraitsBase<ComponentTraits_Fp64, 1>
227 {
228 };
229#pragma endregion
230#pragma region integer 32 unsigned
231 template <>
232 class ImageFormatTraits<VkFormat::VK_FORMAT_R32G32B32A32_UINT> : public ImageFormatTraitsBase<ComponentTraits_UInt32, 4>
233 {
234 };
235 template <>
236 class ImageFormatTraits<VkFormat::VK_FORMAT_R32G32B32_UINT> : public ImageFormatTraitsBase<ComponentTraits_UInt32, 3>
237 {
238 };
239 template <>
240 class ImageFormatTraits<VkFormat::VK_FORMAT_R32G32_UINT> : public ImageFormatTraitsBase<ComponentTraits_UInt32, 2>
241 {
242 };
243 template <>
244 class ImageFormatTraits<VkFormat::VK_FORMAT_R32_UINT> : public ImageFormatTraitsBase<ComponentTraits_UInt32, 1>
245 {
246 };
247#pragma endregion
248#pragma region integer 8 unsigned
249 template <>
250 class ImageFormatTraits<VkFormat::VK_FORMAT_R8G8B8A8_UINT> : public ImageFormatTraitsBase<ComponentTraits_UInt8, 4>
251 {
252 };
253 template <>
254 class ImageFormatTraits<VkFormat::VK_FORMAT_R8G8B8_UINT> : public ImageFormatTraitsBase<ComponentTraits_UInt8, 3>
255 {
256 };
257 template <>
258 class ImageFormatTraits<VkFormat::VK_FORMAT_R8G8_UINT> : public ImageFormatTraitsBase<ComponentTraits_UInt8, 2>
259 {
260 };
261 template <>
262 class ImageFormatTraits<VkFormat::VK_FORMAT_R8_UINT> : public ImageFormatTraitsBase<ComponentTraits_UInt8, 1>
263 {
264 };
265 template <>
266 class ImageFormatTraits<VkFormat::VK_FORMAT_R8G8B8A8_UNORM> : public ImageFormatTraitsBase<ComponentTraits_UInt8, 4>
267 {
268 };
269 template <>
270 class ImageFormatTraits<VkFormat::VK_FORMAT_R8G8B8_UNORM> : public ImageFormatTraitsBase<ComponentTraits_UInt8, 3>
271 {
272 };
273 template <>
274 class ImageFormatTraits<VkFormat::VK_FORMAT_R8G8_UNORM> : public ImageFormatTraitsBase<ComponentTraits_UInt8, 2>
275 {
276 };
277 template <>
278 class ImageFormatTraits<VkFormat::VK_FORMAT_R8_UNORM> : public ImageFormatTraitsBase<ComponentTraits_UInt8, 1>
279 {
280 };
281#pragma endregion
282#pragma region integer packed 10 + 10 + 10 + 2 unsigned
283
284 template <>
285 class ImageFormatTraits<VkFormat::VK_FORMAT_A2R10G10B10_UINT_PACK32>
286 {
287 public:
289 using COMPONENT = COMPONENT_TRAITS::COMPONENT;
290 inline static constexpr uint32_t COMPONENT_COUNT = 4;
291 inline static constexpr uint32_t COMPONENT_STRIDE = 1;
292 inline static constexpr uint32_t BYTESTRIDE = COMPONENT_TRAITS::SIZE * COMPONENT_STRIDE;
293
295 inline static void WriteColor(void* out, COMPONENT r, COMPONENT g, COMPONENT b, COMPONENT a)
296 {
297 COMPONENT* data = reinterpret_cast<COMPONENT*>(out);
298 data[0] = ((a & 0b11) << 30) | ((r & 0b1111111111) << 20) | ((g & 0b1111111111) << 10) | (b & 0b1111111111);
299 }
300
302 inline static void WriteGrayscale(void* out, COMPONENT y, COMPONENT a)
303 {
304 COMPONENT* data = reinterpret_cast<COMPONENT*>(out);
305 y = y & 0b1111111111;
306 data[0] = ((a & 0b11) << 30) | (y << 20) | (y << 20) | y;
307 }
308 };
309
310} // namespace foray::util
Describes the traits of a component type.
Definition foray_imageformattraits.hpp:13
static constexpr bool IS_FLOAT
True if the internal representation is a floating point value.
Definition foray_imageformattraits.hpp:22
static constexpr COMPONENT ALPHA_FALLBACK
Full opacity alpha fallback value.
Definition foray_imageformattraits.hpp:20
static constexpr bool IS_SIGNED
True if the internal representation supports negative values.
Definition foray_imageformattraits.hpp:24
COMPONENT_T COMPONENT
Component type.
Definition foray_imageformattraits.hpp:16
static constexpr uint32_t SIZE
Size (bytes) of the component.
Definition foray_imageformattraits.hpp:18
Trait base class assembled from component trait type and component count.
Definition foray_imageformattraits.hpp:61
Describes the traits of a VkFormat value.
Definition foray_imageformattraits.hpp:47
ComponentTraits_None COMPONENT_TRAITS
Traits of the component.
Definition foray_imageformattraits.hpp:50
static constexpr uint32_t COMPONENT_STRIDE
The stride in component base type (may differ from component count for packed types)
Definition foray_imageformattraits.hpp:54
static constexpr uint32_t BYTESTRIDE
Stride (bytes) per texel.
Definition foray_imageformattraits.hpp:56
static constexpr uint32_t COMPONENT_COUNT
Count of components per texel (determines which channels it can represent)
Definition foray_imageformattraits.hpp:52
Definition foray_dualbuffer.hpp:5
ComponentTraits< std::nullptr_t, nullptr, false, false > ComponentTraits_None
Definition foray_imageformattraits.hpp:27
ComponentTraits< uint32_t, 0b11, false, false > ComponentTraits_PackedAlpha2Color30
32 bit unsigned integer packed component type (2 bits alpha, 30 bits color, 1 component per texel,...
Definition foray_imageformattraits.hpp:42