Blender V5.3
vk_data_conversion.cc
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2023 Blender Authors
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
8
10
11#include "BLI_color_types.hh"
12#include "BLI_math_half.hh"
13
14namespace blender::gpu {
15
17constexpr uint16_t HALF_VALUE_1 = 0x3c00;
18
19/* -------------------------------------------------------------------- */
22
81
83 const TextureFormat device_format)
84{
85 if (host_format != device_format) {
86 if (host_format == TextureFormat::SFLOAT_16_16_16 &&
87 device_format == TextureFormat::SFLOAT_16_16_16_16)
88 {
90 }
91 if (host_format == TextureFormat::SFLOAT_32_32_32 &&
92 device_format == TextureFormat::SFLOAT_32_32_32_32)
93 {
95 }
96
98 }
99
100 switch (device_format) {
101 case TextureFormat::SFLOAT_32_32_32_32:
102 case TextureFormat::SFLOAT_32_32:
103 case TextureFormat::SFLOAT_32:
104 case TextureFormat::SFLOAT_32_DEPTH:
106
107 case TextureFormat::SFLOAT_32_DEPTH_UINT_8:
109
110 case TextureFormat::SFLOAT_16_16_16_16:
111 case TextureFormat::SFLOAT_16_16:
112 case TextureFormat::SFLOAT_16:
113 case TextureFormat::SFLOAT_16_16_16:
115
116 case TextureFormat::SRGBA_8_8_8_8:
117 case TextureFormat::UNORM_8_8_8_8:
118 case TextureFormat::UNORM_8_8:
119 case TextureFormat::UNORM_8:
121
122 case TextureFormat::SNORM_8_8_8_8:
123 case TextureFormat::SNORM_8_8_8:
124 case TextureFormat::SNORM_8_8:
125 case TextureFormat::SNORM_8:
127
128 case TextureFormat::UNORM_16_16_16_16:
129 case TextureFormat::UNORM_16_16:
130 case TextureFormat::UNORM_16:
132
133 case TextureFormat::SNORM_16_16_16_16:
134 case TextureFormat::SNORM_16_16_16:
135 case TextureFormat::SNORM_16_16:
136 case TextureFormat::SNORM_16:
138
139 case TextureFormat::UFLOAT_11_11_10:
141
142 case TextureFormat::SRGB_DXT1:
143 case TextureFormat::SRGB_DXT3:
144 case TextureFormat::SRGB_DXT5:
145 case TextureFormat::SNORM_DXT1:
146 case TextureFormat::SNORM_DXT3:
147 case TextureFormat::SNORM_DXT5:
148 /* Not an actual "conversion", but compressed texture upload code
149 * pretends that host data is a float. It is actually raw BCn bits. */
151
152 /* #TextureFormat::SFLOAT_32_32_32 Not supported by vendors. */
153 case TextureFormat::SFLOAT_32_32_32:
154
155 case TextureFormat::UINT_8_8_8_8:
156 case TextureFormat::SINT_8_8_8_8:
157 case TextureFormat::UINT_16_16_16_16:
158 case TextureFormat::SINT_16_16_16_16:
159 case TextureFormat::UINT_32_32_32_32:
160 case TextureFormat::SINT_32_32_32_32:
161 case TextureFormat::UINT_8_8:
162 case TextureFormat::SINT_8_8:
163 case TextureFormat::UINT_16_16:
164 case TextureFormat::SINT_16_16:
165 case TextureFormat::UINT_32_32:
166 case TextureFormat::SINT_32_32:
167 case TextureFormat::UINT_8:
168 case TextureFormat::SINT_8:
169 case TextureFormat::UINT_16:
170 case TextureFormat::SINT_16:
171 case TextureFormat::UINT_32:
172 case TextureFormat::SINT_32:
173 case TextureFormat::UNORM_10_10_10_2:
174 case TextureFormat::UINT_10_10_10_2:
175 case TextureFormat::UINT_8_8_8:
176 case TextureFormat::SINT_8_8_8:
177 case TextureFormat::UNORM_8_8_8:
178 case TextureFormat::UINT_16_16_16:
179 case TextureFormat::SINT_16_16_16:
180 case TextureFormat::UNORM_16_16_16:
181 case TextureFormat::UINT_32_32_32:
182 case TextureFormat::SINT_32_32_32:
183 case TextureFormat::SRGBA_8_8_8:
184 case TextureFormat::UFLOAT_9_9_9_EXP_5:
185 case TextureFormat::UNORM_16_DEPTH:
187
190 break;
191 }
193}
194
196{
197 switch (device_format) {
198 case TextureFormat::SINT_32_32_32_32:
199 case TextureFormat::SINT_32_32:
200 case TextureFormat::SINT_32:
202
203 case TextureFormat::SINT_16_16_16_16:
204 case TextureFormat::SINT_16_16:
205 case TextureFormat::SINT_16:
207
208 case TextureFormat::SINT_8_8_8_8:
209 case TextureFormat::SINT_8_8:
210 case TextureFormat::SINT_8:
212
213 case TextureFormat::UINT_8_8_8_8:
214 case TextureFormat::UNORM_8_8_8_8:
215 case TextureFormat::UINT_16_16_16_16:
216 case TextureFormat::SFLOAT_16_16_16_16:
217 case TextureFormat::UNORM_16_16_16_16:
218 case TextureFormat::UINT_32_32_32_32:
219 case TextureFormat::SFLOAT_32_32_32_32:
220 case TextureFormat::UINT_8_8:
221 case TextureFormat::UNORM_8_8:
222 case TextureFormat::UINT_16_16:
223 case TextureFormat::SFLOAT_16_16:
224 case TextureFormat::UINT_32_32:
225 case TextureFormat::SFLOAT_32_32:
226 case TextureFormat::UNORM_16_16:
227 case TextureFormat::UINT_8:
228 case TextureFormat::UNORM_8:
229 case TextureFormat::UINT_16:
230 case TextureFormat::SFLOAT_16:
231 case TextureFormat::UNORM_16:
232 case TextureFormat::UINT_32:
233 case TextureFormat::SFLOAT_32:
234 case TextureFormat::UNORM_10_10_10_2:
235 case TextureFormat::UINT_10_10_10_2:
236 case TextureFormat::UFLOAT_11_11_10:
237 case TextureFormat::SFLOAT_32_DEPTH_UINT_8:
238 case TextureFormat::SRGBA_8_8_8_8:
239 case TextureFormat::SNORM_8_8_8_8:
240 case TextureFormat::SNORM_16_16_16_16:
241 case TextureFormat::UINT_8_8_8:
242 case TextureFormat::SINT_8_8_8:
243 case TextureFormat::UNORM_8_8_8:
244 case TextureFormat::SNORM_8_8_8:
245 case TextureFormat::UINT_16_16_16:
246 case TextureFormat::SINT_16_16_16:
247 case TextureFormat::SFLOAT_16_16_16:
248 case TextureFormat::UNORM_16_16_16:
249 case TextureFormat::SNORM_16_16_16:
250 case TextureFormat::UINT_32_32_32:
251 case TextureFormat::SINT_32_32_32:
252 case TextureFormat::SFLOAT_32_32_32:
253 case TextureFormat::SNORM_8_8:
254 case TextureFormat::SNORM_16_16:
255 case TextureFormat::SNORM_8:
256 case TextureFormat::SNORM_16:
257 case TextureFormat::SRGB_DXT1:
258 case TextureFormat::SRGB_DXT3:
259 case TextureFormat::SRGB_DXT5:
260 case TextureFormat::SNORM_DXT1:
261 case TextureFormat::SNORM_DXT3:
262 case TextureFormat::SNORM_DXT5:
263 case TextureFormat::SRGBA_8_8_8:
264 case TextureFormat::UFLOAT_9_9_9_EXP_5:
265 case TextureFormat::SFLOAT_32_DEPTH:
266 case TextureFormat::UNORM_16_DEPTH:
268
271 break;
272 }
274}
275
277{
278 switch (device_format) {
279 case TextureFormat::UINT_32_32_32_32:
280 case TextureFormat::UINT_32_32:
281 case TextureFormat::UINT_32:
283
284 case TextureFormat::UINT_16_16_16_16:
285 case TextureFormat::UINT_16_16:
286 case TextureFormat::UINT_16:
287 case TextureFormat::UINT_16_16_16:
289
290 case TextureFormat::UINT_8_8_8_8:
291 case TextureFormat::UINT_8_8:
292 case TextureFormat::UINT_8:
294
295 case TextureFormat::SFLOAT_32_DEPTH:
296 case TextureFormat::SFLOAT_32_DEPTH_UINT_8:
298
299 case TextureFormat::SINT_8_8_8_8:
300 case TextureFormat::UNORM_8_8_8_8:
301 case TextureFormat::SINT_16_16_16_16:
302 case TextureFormat::SFLOAT_16_16_16_16:
303 case TextureFormat::UNORM_16_16_16_16:
304 case TextureFormat::SINT_32_32_32_32:
305 case TextureFormat::SFLOAT_32_32_32_32:
306 case TextureFormat::SINT_8_8:
307 case TextureFormat::UNORM_8_8:
308 case TextureFormat::SINT_16_16:
309 case TextureFormat::SFLOAT_16_16:
310 case TextureFormat::UNORM_16_16:
311 case TextureFormat::SINT_32_32:
312 case TextureFormat::SFLOAT_32_32:
313 case TextureFormat::SINT_8:
314 case TextureFormat::UNORM_8:
315 case TextureFormat::SINT_16:
316 case TextureFormat::SFLOAT_16:
317 case TextureFormat::UNORM_16:
318 case TextureFormat::SINT_32:
319 case TextureFormat::SFLOAT_32:
320 case TextureFormat::UNORM_10_10_10_2:
321 case TextureFormat::UINT_10_10_10_2:
322 case TextureFormat::UFLOAT_11_11_10:
323 case TextureFormat::SRGBA_8_8_8_8:
324 case TextureFormat::SNORM_8_8_8_8:
325 case TextureFormat::SNORM_16_16_16_16:
326 case TextureFormat::UINT_8_8_8:
327 case TextureFormat::SINT_8_8_8:
328 case TextureFormat::UNORM_8_8_8:
329 case TextureFormat::SNORM_8_8_8:
330 case TextureFormat::SINT_16_16_16:
331 case TextureFormat::SFLOAT_16_16_16:
332 case TextureFormat::UNORM_16_16_16:
333 case TextureFormat::SNORM_16_16_16:
334 case TextureFormat::UINT_32_32_32:
335 case TextureFormat::SINT_32_32_32:
336 case TextureFormat::SFLOAT_32_32_32:
337 case TextureFormat::SNORM_8_8:
338 case TextureFormat::SNORM_16_16:
339 case TextureFormat::SNORM_8:
340 case TextureFormat::SNORM_16:
341 case TextureFormat::SRGB_DXT1:
342 case TextureFormat::SRGB_DXT3:
343 case TextureFormat::SRGB_DXT5:
344 case TextureFormat::SNORM_DXT1:
345 case TextureFormat::SNORM_DXT3:
346 case TextureFormat::SNORM_DXT5:
347 case TextureFormat::SRGBA_8_8_8:
348 case TextureFormat::UFLOAT_9_9_9_EXP_5:
349 case TextureFormat::UNORM_16_DEPTH:
351
354 break;
355 }
357}
358
360{
361 switch (device_format) {
362 case TextureFormat::SFLOAT_16_16_16_16:
363 case TextureFormat::SFLOAT_16_16:
364 case TextureFormat::SFLOAT_16:
366
367 case TextureFormat::UINT_8_8_8_8:
368 case TextureFormat::SINT_8_8_8_8:
369 case TextureFormat::UNORM_8_8_8_8:
370 case TextureFormat::UINT_16_16_16_16:
371 case TextureFormat::SINT_16_16_16_16:
372 case TextureFormat::UNORM_16_16_16_16:
373 case TextureFormat::UINT_32_32_32_32:
374 case TextureFormat::SINT_32_32_32_32:
375 case TextureFormat::SFLOAT_32_32_32_32:
376 case TextureFormat::UINT_8_8:
377 case TextureFormat::SINT_8_8:
378 case TextureFormat::UNORM_8_8:
379 case TextureFormat::UINT_16_16:
380 case TextureFormat::SINT_16_16:
381 case TextureFormat::UNORM_16_16:
382 case TextureFormat::UINT_32_32:
383 case TextureFormat::SINT_32_32:
384 case TextureFormat::SFLOAT_32_32:
385 case TextureFormat::UINT_8:
386 case TextureFormat::SINT_8:
387 case TextureFormat::UNORM_8:
388 case TextureFormat::UINT_16:
389 case TextureFormat::SINT_16:
390 case TextureFormat::UNORM_16:
391 case TextureFormat::UINT_32:
392 case TextureFormat::SINT_32:
393 case TextureFormat::SFLOAT_32:
394 case TextureFormat::UNORM_10_10_10_2:
395 case TextureFormat::UINT_10_10_10_2:
396 case TextureFormat::UFLOAT_11_11_10:
397 case TextureFormat::SFLOAT_32_DEPTH_UINT_8:
398 case TextureFormat::SRGBA_8_8_8_8:
399 case TextureFormat::SNORM_8_8_8_8:
400 case TextureFormat::SNORM_16_16_16_16:
401 case TextureFormat::UINT_8_8_8:
402 case TextureFormat::SINT_8_8_8:
403 case TextureFormat::UNORM_8_8_8:
404 case TextureFormat::SNORM_8_8_8:
405 case TextureFormat::UINT_16_16_16:
406 case TextureFormat::SINT_16_16_16:
407 case TextureFormat::SFLOAT_16_16_16:
408 case TextureFormat::UNORM_16_16_16:
409 case TextureFormat::SNORM_16_16_16:
410 case TextureFormat::UINT_32_32_32:
411 case TextureFormat::SINT_32_32_32:
412 case TextureFormat::SFLOAT_32_32_32:
413 case TextureFormat::SNORM_8_8:
414 case TextureFormat::SNORM_16_16:
415 case TextureFormat::SNORM_8:
416 case TextureFormat::SNORM_16:
417 case TextureFormat::SRGB_DXT1:
418 case TextureFormat::SRGB_DXT3:
419 case TextureFormat::SRGB_DXT5:
420 case TextureFormat::SNORM_DXT1:
421 case TextureFormat::SNORM_DXT3:
422 case TextureFormat::SNORM_DXT5:
423 case TextureFormat::SRGBA_8_8_8:
424 case TextureFormat::UFLOAT_9_9_9_EXP_5:
425 case TextureFormat::SFLOAT_32_DEPTH:
426 case TextureFormat::UNORM_16_DEPTH:
428
431 break;
432 }
434}
435
437{
438 switch (device_format) {
439 case TextureFormat::UINT_8_8_8_8:
440 case TextureFormat::UNORM_8_8_8_8:
441 case TextureFormat::UINT_8_8:
442 case TextureFormat::UNORM_8_8:
443 case TextureFormat::UINT_8:
444 case TextureFormat::UNORM_8:
445 case TextureFormat::SRGBA_8_8_8_8:
447
448 case TextureFormat::SFLOAT_16_16_16_16:
449 case TextureFormat::SFLOAT_16_16:
450 case TextureFormat::SFLOAT_16:
452
453 case TextureFormat::SINT_8_8_8_8:
454 case TextureFormat::UINT_16_16_16_16:
455 case TextureFormat::SINT_16_16_16_16:
456 case TextureFormat::UNORM_16_16_16_16:
457 case TextureFormat::UINT_32_32_32_32:
458 case TextureFormat::SINT_32_32_32_32:
459 case TextureFormat::SFLOAT_32_32_32_32:
460 case TextureFormat::SINT_8_8:
461 case TextureFormat::UINT_16_16:
462 case TextureFormat::SINT_16_16:
463 case TextureFormat::UNORM_16_16:
464 case TextureFormat::UINT_32_32:
465 case TextureFormat::SINT_32_32:
466 case TextureFormat::SFLOAT_32_32:
467 case TextureFormat::SINT_8:
468 case TextureFormat::UINT_16:
469 case TextureFormat::SINT_16:
470 case TextureFormat::UNORM_16:
471 case TextureFormat::UINT_32:
472 case TextureFormat::SINT_32:
473 case TextureFormat::SFLOAT_32:
474 case TextureFormat::UNORM_10_10_10_2:
475 case TextureFormat::UINT_10_10_10_2:
476 case TextureFormat::UFLOAT_11_11_10:
477 case TextureFormat::SFLOAT_32_DEPTH_UINT_8:
478 case TextureFormat::SNORM_8_8_8_8:
479 case TextureFormat::SNORM_16_16_16_16:
480 case TextureFormat::UINT_8_8_8:
481 case TextureFormat::SINT_8_8_8:
482 case TextureFormat::UNORM_8_8_8:
483 case TextureFormat::SNORM_8_8_8:
484 case TextureFormat::UINT_16_16_16:
485 case TextureFormat::SINT_16_16_16:
486 case TextureFormat::SFLOAT_16_16_16:
487 case TextureFormat::UNORM_16_16_16:
488 case TextureFormat::SNORM_16_16_16:
489 case TextureFormat::UINT_32_32_32:
490 case TextureFormat::SINT_32_32_32:
491 case TextureFormat::SFLOAT_32_32_32:
492 case TextureFormat::SNORM_8_8:
493 case TextureFormat::SNORM_16_16:
494 case TextureFormat::SNORM_8:
495 case TextureFormat::SNORM_16:
496 case TextureFormat::SRGB_DXT1:
497 case TextureFormat::SRGB_DXT3:
498 case TextureFormat::SRGB_DXT5:
499 case TextureFormat::SNORM_DXT1:
500 case TextureFormat::SNORM_DXT3:
501 case TextureFormat::SNORM_DXT5:
502 case TextureFormat::SRGBA_8_8_8:
503 case TextureFormat::UFLOAT_9_9_9_EXP_5:
504 case TextureFormat::SFLOAT_32_DEPTH:
505 case TextureFormat::UNORM_16_DEPTH:
507
510 break;
511 }
513}
514
516{
517 switch (device_format) {
518 case TextureFormat::SFLOAT_32_DEPTH_UINT_8:
520
521 case TextureFormat::SFLOAT_32_32_32_32:
522 case TextureFormat::SFLOAT_32_32:
523 case TextureFormat::SFLOAT_32:
524 case TextureFormat::SFLOAT_16_16_16_16:
525 case TextureFormat::SFLOAT_16_16:
526 case TextureFormat::SFLOAT_16:
527 case TextureFormat::SFLOAT_16_16_16:
528 case TextureFormat::UNORM_8_8_8_8:
529 case TextureFormat::UNORM_8_8:
530 case TextureFormat::UNORM_8:
531 case TextureFormat::SNORM_8_8_8_8:
532 case TextureFormat::SNORM_8_8_8:
533 case TextureFormat::SNORM_8_8:
534 case TextureFormat::SNORM_8:
535 case TextureFormat::UNORM_16_16_16_16:
536 case TextureFormat::UNORM_16_16:
537 case TextureFormat::UNORM_16:
538 case TextureFormat::SNORM_16_16_16_16:
539 case TextureFormat::SNORM_16_16_16:
540 case TextureFormat::SNORM_16_16:
541 case TextureFormat::SNORM_16:
542 case TextureFormat::SRGBA_8_8_8_8:
543 case TextureFormat::SFLOAT_32_DEPTH:
544 case TextureFormat::UFLOAT_11_11_10:
545 case TextureFormat::SRGB_DXT1:
546 case TextureFormat::SRGB_DXT3:
547 case TextureFormat::SRGB_DXT5:
548 case TextureFormat::SNORM_DXT1:
549 case TextureFormat::SNORM_DXT3:
550 case TextureFormat::SNORM_DXT5:
551
552 /* #TextureFormat::SFLOAT_32_32_32 Not supported by vendors. */
553 case TextureFormat::SFLOAT_32_32_32:
554
555 case TextureFormat::UINT_8_8_8_8:
556 case TextureFormat::SINT_8_8_8_8:
557 case TextureFormat::UINT_16_16_16_16:
558 case TextureFormat::SINT_16_16_16_16:
559 case TextureFormat::UINT_32_32_32_32:
560 case TextureFormat::SINT_32_32_32_32:
561 case TextureFormat::UINT_8_8:
562 case TextureFormat::SINT_8_8:
563 case TextureFormat::UINT_16_16:
564 case TextureFormat::SINT_16_16:
565 case TextureFormat::UINT_32_32:
566 case TextureFormat::SINT_32_32:
567 case TextureFormat::UINT_8:
568 case TextureFormat::SINT_8:
569 case TextureFormat::UINT_16:
570 case TextureFormat::SINT_16:
571 case TextureFormat::UINT_32:
572 case TextureFormat::SINT_32:
573 case TextureFormat::UNORM_10_10_10_2:
574 case TextureFormat::UINT_10_10_10_2:
575 case TextureFormat::UINT_8_8_8:
576 case TextureFormat::SINT_8_8_8:
577 case TextureFormat::UNORM_8_8_8:
578 case TextureFormat::UINT_16_16_16:
579 case TextureFormat::SINT_16_16_16:
580 case TextureFormat::UNORM_16_16_16:
581 case TextureFormat::UINT_32_32_32:
582 case TextureFormat::SINT_32_32_32:
583 case TextureFormat::SRGBA_8_8_8:
584 case TextureFormat::UFLOAT_9_9_9_EXP_5:
585 case TextureFormat::UNORM_16_DEPTH:
587
590 break;
591 }
593}
594
596{
597 if (device_format == TextureFormat::UFLOAT_11_11_10) {
599 }
601}
602
604{
605 if (ELEM(device_format, TextureFormat::UNORM_10_10_10_2, TextureFormat::UINT_10_10_10_2)) {
607 }
609}
610
612 const TextureFormat host_texture_format,
613 const TextureFormat device_format)
614{
615 switch (host_format) {
616 case GPU_DATA_FLOAT:
617 return type_of_conversion_float(host_texture_format, device_format);
618 case GPU_DATA_UINT:
619 return type_of_conversion_uint(device_format);
620 case GPU_DATA_INT:
621 return type_of_conversion_int(device_format);
623 return type_of_conversion_half(device_format);
624 case GPU_DATA_UBYTE:
625 return type_of_conversion_ubyte(device_format);
627 return type_of_conversion_r11g11b10(device_format);
629 return type_of_conversion_r10g10b10a2(device_format);
631 return type_of_conversion_uint248(device_format);
632 }
633
635}
636
638{
639#define CASE_SINGLE(a, b) \
640 case ConversionType::a##_TO_##b: \
641 return ConversionType::b##_TO_##a;
642
643#define CASE_PAIR(a, b) \
644 CASE_SINGLE(a, b) \
645 CASE_SINGLE(b, a)
646
647 switch (type) {
652
653 CASE_PAIR(FLOAT, UNORM8)
654 CASE_PAIR(FLOAT, SNORM8)
655 CASE_PAIR(FLOAT, UNORM16)
656 CASE_PAIR(FLOAT, SNORM16)
657 CASE_PAIR(FLOAT, UNORM32)
663 CASE_PAIR(FLOAT, B10F_G11F_R11F)
666 CASE_PAIR(UINT, DEPTH32F_STENCIL8)
668
671 }
672
673#undef CASE_PAIR
674#undef CASE_SINGLE
675
677}
678
680
681/* -------------------------------------------------------------------- */
684
685static uint32_t float_to_uint32_t(float value)
686{
687 union {
688 float fl;
689 uint32_t u;
690 } float_to_bits;
691 float_to_bits.fl = value;
692 return float_to_bits.u;
693}
694
695static float uint32_t_to_float(uint32_t value)
696{
697 union {
698 float fl;
699 uint32_t u;
700 } float_to_bits;
701 float_to_bits.u = value;
702 return float_to_bits.fl;
703}
704
705template<typename InnerType> struct ComponentValue {
706 InnerType value;
707};
708template<typename InnerType> struct PixelValue {
709 InnerType value;
710};
711
722/* NOTE: Vulkan stores R11_G11_B10 in reverse component order. */
723class B10F_G11G_R11F : public PixelValue<uint32_t> {};
724
725class HALF4 : public PixelValue<uint64_t> {
726 public:
728 {
729 return value & 0xffff;
730 }
731
732 void set_r(uint64_t new_value)
733 {
734 value = (value & 0xffffffffffff0000) | (new_value & 0xffff);
735 }
737 {
738 return (value >> 16) & 0xffff;
739 }
740
741 void set_g(uint64_t new_value)
742 {
743 value = (value & 0xffffffff0000ffff) | ((new_value & 0xffff) << 16);
744 }
746 {
747 return (value >> 32) & 0xffff;
748 }
749
750 void set_b(uint64_t new_value)
751 {
752 value = (value & 0xffff0000ffffffff) | ((new_value & 0xffff) << 32);
753 }
754
755 void set_a(uint64_t new_value)
756 {
757 value = (value & 0xffffffffffff) | ((new_value & 0xffff) << 48);
758 }
759};
760
761/* Use a float as we only have the depth aspect in the staging buffers. */
763
764template<typename InnerType> struct SignedNormalized {
765 static_assert(std::is_same<InnerType, uint8_t>() || std::is_same<InnerType, uint16_t>());
766 InnerType value;
767
768 static constexpr int32_t scalar()
769 {
770 return (1 << (sizeof(InnerType) * 8 - 1));
771 }
772
773 static constexpr int32_t delta()
774 {
775 return (1 << (sizeof(InnerType) * 8 - 1)) - 1;
776 }
777
778 static constexpr int32_t max()
779 {
780 return ((1 << (sizeof(InnerType) * 8)) - 1);
781 }
782};
783
784template<typename InnerType> struct UnsignedNormalized {
785 static_assert(std::is_same<InnerType, uint8_t>() || std::is_same<InnerType, uint16_t>() ||
786 std::is_same<InnerType, uint32_t>());
787 InnerType value;
788
789 static constexpr size_t used_byte_size()
790 {
791 return sizeof(InnerType);
792 }
793
794 static constexpr uint32_t scalar()
795 {
796 return std::numeric_limits<InnerType>::max();
797 }
798
799 static constexpr uint32_t max()
800 {
801 return std::numeric_limits<InnerType>::max();
802 }
803};
804
805template<typename StorageType> void convert(SignedNormalized<StorageType> &dst, const F32 &src)
806{
807 static constexpr int32_t scalar = SignedNormalized<StorageType>::scalar();
808 static constexpr int32_t delta = SignedNormalized<StorageType>::delta();
810 dst.value = clamp_i((src.value * scalar + delta), 0, max);
811}
812
813template<typename StorageType> void convert(F32 &dst, const SignedNormalized<StorageType> &src)
814{
815 static constexpr int32_t scalar = SignedNormalized<StorageType>::scalar();
816 static constexpr int32_t delta = SignedNormalized<StorageType>::delta();
817 dst.value = float(int32_t(src.value) - delta) / scalar;
818}
819
820template<typename StorageType> void convert(UnsignedNormalized<StorageType> &dst, const F32 &src)
821{
822 static constexpr uint32_t scalar = UnsignedNormalized<StorageType>::scalar();
823 static constexpr uint32_t max = scalar;
824 /* When converting a DEPTH32F to DEPTH24 the scalar gets to large where 1.0 will wrap around and
825 * become 0. Make sure that depth 1.0 will not wrap around. Without this gpu_select_pick will
826 * fail as all depth 1.0 will occlude previous depths. */
827 dst.value = src.value >= 1.0f ? max : max_ff(src.value * float(scalar), 0.0);
828}
829
830template<typename StorageType> void convert(F32 &dst, const UnsignedNormalized<StorageType> &src)
831{
832 static constexpr uint32_t scalar = UnsignedNormalized<StorageType>::scalar();
833 dst.value = float(uint32_t(src.value & scalar)) / float(scalar);
834}
835
836template<typename StorageType>
837void convert(UnsignedNormalized<StorageType> & /*dst*/, const UI32 & /*src*/)
838{
840}
841
842template<typename StorageType> void convert(UI32 &dst, const UnsignedNormalized<StorageType> &src)
843{
844 static constexpr uint32_t scalar = UnsignedNormalized<StorageType>::scalar();
845 dst.value = uint32_t(src.value) & scalar;
846}
847
848/* Copy the contents of src to dst with out performing any actual conversion. */
849template<typename DestinationType, typename SourceType>
850void convert(DestinationType &dst, const SourceType &src)
851{
852 static_assert(std::is_same<DestinationType, UI8>() || std::is_same<DestinationType, UI16>() ||
853 std::is_same<DestinationType, UI32>() || std::is_same<DestinationType, I8>() ||
854 std::is_same<DestinationType, I16>() || std::is_same<DestinationType, I32>());
855 static_assert(std::is_same<SourceType, UI8>() || std::is_same<SourceType, UI16>() ||
856 std::is_same<SourceType, UI32>() || std::is_same<SourceType, I8>() ||
857 std::is_same<SourceType, I16>() || std::is_same<SourceType, I32>());
858 static_assert(!std::is_same<DestinationType, SourceType>());
859 dst.value = src.value;
860}
861
862static void convert(FLOAT3 &dst, const HALF4 &src)
863{
864 dst.value.x = math::half_to_float(src.get_r());
865 dst.value.y = math::half_to_float(src.get_g());
866 dst.value.z = math::half_to_float(src.get_b());
867}
868
869static void convert(FLOAT3 &dst, const FLOAT4 &src)
870{
871 dst.value.x = src.value.r;
872 dst.value.y = src.value.g;
873 dst.value.z = src.value.b;
874}
875
876static void convert(FLOAT4 &dst, const FLOAT3 &src)
877{
878 dst.value.r = src.value.x;
879 dst.value.g = src.value.y;
880 dst.value.b = src.value.z;
881 dst.value.a = 1.0f;
882}
883
884static void convert(F16 &dst, const UI8 &src)
885{
887 un8.value = src.value;
888 F32 f32;
889 convert(f32, un8);
891}
892
893static void convert(UI8 &dst, const F16 &src)
894{
895 F32 f32;
898 convert(un8, f32);
899 dst.value = un8.value;
900}
901
902constexpr uint32_t MASK_10_BITS = 0b1111111111;
903constexpr uint32_t MASK_11_BITS = 0b11111111111;
904constexpr uint8_t SHIFT_B = 22;
905constexpr uint8_t SHIFT_G = 11;
906constexpr uint8_t SHIFT_R = 0;
907
917
925
927
928static void convert(UI32 &dst, const Depth32fStencil8 &src)
929{
930 uint32_t depth = uint32_t(src.value * 0xFFFFFF);
931 dst.value = (depth << 8);
932}
933
934static void convert(Depth32fStencil8 &dst, const UI32 &src)
935{
936 uint32_t depth = (src.value >> 8) & 0xFFFFFF;
937 dst.value = float(depth) * 0xFFFFFF;
938}
939
940template<typename DestinationType, typename SourceType>
942{
943 BLI_assert(src.size() == dst.size());
944 for (int64_t index : IndexRange(src.size())) {
945 convert(dst[index], src[index]);
946 }
947}
948
949template<typename DestinationType, typename SourceType>
950void convert_per_component(void *dst_memory,
951 const void *src_memory,
952 size_t buffer_size,
953 TextureFormat device_format)
954{
955 size_t total_components = to_component_len(device_format) * buffer_size;
956 Span<SourceType> src = Span<SourceType>(static_cast<const SourceType *>(src_memory),
957 total_components);
959 static_cast<DestinationType *>(dst_memory), total_components);
961}
962
963template<typename DestinationType, typename SourceType>
964void convert_per_pixel(void *dst_memory, const void *src_memory, size_t buffer_size)
965{
966 Span<SourceType> src = Span<SourceType>(static_cast<const SourceType *>(src_memory),
967 buffer_size);
969 static_cast<DestinationType *>(dst_memory), buffer_size);
971}
972
977template<typename ComponentType>
979 int64_t size,
980 ComponentType component_4_value)
981{
982 for (int index : IndexRange(size)) {
983 const int src_index = index * 3 + size;
984 const int dst_index = index * 4;
985 components[dst_index] = components[src_index];
986 components[dst_index + 1] = components[src_index + 1];
987 components[dst_index + 2] = components[src_index + 2];
988 components[dst_index + 3] = component_4_value;
989 }
990}
991
992static void convert_buffer(void *dst_memory,
993 const void *src_memory,
994 size_t buffer_size,
995 TextureFormat device_format,
996 ConversionType type)
997{
998 switch (type) {
1000 return;
1001
1003 memcpy(dst_memory, src_memory, buffer_size * to_bytesize(device_format));
1004 return;
1005
1007 memcpy(dst_memory, src_memory, buffer_size * to_bytesize(TextureFormat::SFLOAT_32_DEPTH));
1008 return;
1009
1011 convert_per_component<UI16, UI32>(dst_memory, src_memory, buffer_size, device_format);
1012 break;
1013
1015 convert_per_component<UI32, UI16>(dst_memory, src_memory, buffer_size, device_format);
1016 break;
1017
1019 convert_per_component<UI8, UI32>(dst_memory, src_memory, buffer_size, device_format);
1020 break;
1021
1023 convert_per_component<UI32, UI8>(dst_memory, src_memory, buffer_size, device_format);
1024 break;
1025
1027 convert_per_component<I16, I32>(dst_memory, src_memory, buffer_size, device_format);
1028 break;
1029
1031 convert_per_component<I32, I16>(dst_memory, src_memory, buffer_size, device_format);
1032 break;
1033
1035 convert_per_component<I8, I32>(dst_memory, src_memory, buffer_size, device_format);
1036 break;
1037
1039 convert_per_component<I32, I8>(dst_memory, src_memory, buffer_size, device_format);
1040 break;
1041
1044 dst_memory, src_memory, buffer_size, device_format);
1045 break;
1048 dst_memory, src_memory, buffer_size, device_format);
1049 break;
1050
1053 dst_memory, src_memory, buffer_size, device_format);
1054 break;
1057 dst_memory, src_memory, buffer_size, device_format);
1058 break;
1059
1062 dst_memory, src_memory, buffer_size, device_format);
1063 break;
1066 dst_memory, src_memory, buffer_size, device_format);
1067 break;
1068
1071 dst_memory, src_memory, buffer_size, device_format);
1072 break;
1075 dst_memory, src_memory, buffer_size, device_format);
1076 break;
1077
1080 dst_memory, src_memory, buffer_size, device_format);
1081 break;
1084 dst_memory, src_memory, buffer_size, device_format);
1085 break;
1086
1088 convert_per_component<F16, UI8>(dst_memory, src_memory, buffer_size, device_format);
1089 break;
1091 convert_per_component<UI8, F16>(dst_memory, src_memory, buffer_size, device_format);
1092 break;
1093
1095 size_t element_len = to_component_len(device_format) * buffer_size;
1096 Span<float> src(static_cast<const float *>(src_memory), element_len);
1097 MutableSpan<uint16_t> dst(static_cast<uint16_t *>(dst_memory), element_len);
1098
1099 constexpr int64_t chunk_size = 4 * 1024 * 1024;
1100
1101 threading::parallel_for(IndexRange(element_len), chunk_size, [&](const IndexRange range) {
1102 /* Doing float to half conversion manually to avoid implementation specific behavior
1103 * regarding Inf and NaNs. Use make finite version to avoid unexpected black pixels on
1104 * certain implementation. For platform parity we clamp these infinite values to finite
1105 * values. */
1107 src.slice(range).data(), dst.slice(range).data(), range.size());
1108 });
1109 break;
1110 }
1112 math::half_to_float_array(static_cast<const uint16_t *>(src_memory),
1113 static_cast<float *>(dst_memory),
1114 to_component_len(device_format) * buffer_size);
1115 break;
1116
1118 convert_per_pixel<B10F_G11G_R11F, FLOAT3>(dst_memory, src_memory, buffer_size);
1119 break;
1120
1122 convert_per_pixel<UI32, Depth32fStencil8>(dst_memory, src_memory, buffer_size);
1123 break;
1125 convert_per_pixel<Depth32fStencil8, UI32>(dst_memory, src_memory, buffer_size);
1126 break;
1127
1129 convert_per_pixel<FLOAT3, B10F_G11G_R11F>(dst_memory, src_memory, buffer_size);
1130 break;
1131
1133 size_t element_len = buffer_size;
1134 constexpr int64_t src_component_len = 3;
1135 constexpr int64_t dst_component_len = 4;
1136 Span<float> src(static_cast<const float *>(src_memory), element_len * src_component_len);
1137 MutableSpan<uint16_t> dst(static_cast<uint16_t *>(dst_memory),
1138 element_len * dst_component_len);
1139
1140 /* Number of pixels to process in a single chunk. */
1141 constexpr int64_t chunk_size = 16 * 1024;
1142
1143 threading::parallel_for(IndexRange(element_len), chunk_size, [&](const IndexRange range) {
1144 IndexRange src_range = IndexRange(range.start() * src_component_len,
1145 range.size() * src_component_len);
1146 IndexRange dst_range = IndexRange(range.start() * dst_component_len,
1147 range.size() * dst_component_len);
1148 IndexRange dst_range_offset = IndexRange(range.start() * dst_component_len + range.size(),
1149 range.size() * src_component_len);
1150 /* Doing float to half conversion manually to avoid implementation specific behavior
1151 * regarding Inf and NaNs. Use make finite version to avoid unexpected black pixels on
1152 * certain implementation. For platform parity we clamp these infinite values to finite
1153 * values. */
1155 src.slice(src_range).data(), dst.slice(dst_range_offset).data(), src_range.size());
1156 /* Conversion are stored at the end of the dst_memory, so remapping works with sequential
1157 * access. */
1159 });
1160
1161 break;
1162 }
1164 convert_per_pixel<FLOAT3, HALF4>(dst_memory, src_memory, buffer_size);
1165 break;
1166
1168 convert_per_pixel<FLOAT4, FLOAT3>(dst_memory, src_memory, buffer_size);
1169 break;
1171 convert_per_pixel<FLOAT3, FLOAT4>(dst_memory, src_memory, buffer_size);
1172 break;
1173 }
1174}
1175
1176/* -------------------------------------------------------------------- */
1179
1181 TextureFormat host_texture_format,
1182 TextureFormat device_format)
1183{
1184 ConversionType conversion_type = host_to_device(host_format, host_texture_format, device_format);
1185 return conversion_type != ConversionType::PASS_THROUGH;
1186}
1187
1188void convert_host_to_device(void *dst_buffer,
1189 const void *src_buffer,
1190 size_t buffer_size,
1191 eGPUDataFormat host_format,
1192 TextureFormat host_texture_format,
1193 TextureFormat device_format)
1194{
1195 ConversionType conversion_type = host_to_device(host_format, host_texture_format, device_format);
1196 BLI_assert(conversion_type != ConversionType::UNSUPPORTED);
1197 convert_buffer(dst_buffer, src_buffer, buffer_size, device_format, conversion_type);
1198}
1199
1200void convert_device_to_host(void *dst_buffer,
1201 const void *src_buffer,
1202 size_t buffer_size,
1203 eGPUDataFormat host_format,
1204 TextureFormat host_texture_format,
1205 TextureFormat device_format)
1206{
1207 ConversionType conversion_type = reversed(
1208 host_to_device(host_format, host_texture_format, device_format));
1210 "Data conversion between host_format and device_format isn't supported (yet).");
1211 convert_buffer(dst_buffer, src_buffer, buffer_size, device_format, conversion_type);
1212}
1213
1215
1216} // namespace blender::gpu
#define BLI_assert_unreachable()
#define ELEM(...)
#define BLI_assert_msg(a, msg)
#define CASE_PAIR(value_src, value_dst)
btScalar max
ChannelStorageType r
ChannelStorageType g
ChannelStorageType b
ChannelStorageType a
constexpr int64_t size() const
constexpr IndexRange()=default
constexpr int64_t start() const
constexpr MutableSpan()=default
constexpr int64_t size() const
Definition BLI_span.hh:499
constexpr MutableSpan slice(const int64_t start, const int64_t size) const
Definition BLI_span.hh:579
constexpr T * data() const
Definition BLI_span.hh:545
constexpr Span slice(int64_t start, int64_t size) const
Definition BLI_span.hh:141
constexpr const T * data() const
Definition BLI_span.hh:219
constexpr Span()=default
constexpr int64_t size() const
Definition BLI_span.hh:256
void set_g(uint64_t new_value)
void set_r(uint64_t new_value)
void set_a(uint64_t new_value)
void set_b(uint64_t new_value)
ComponentValue< int16_t > I16
ComponentValue< uint16_t > F16
ComponentValue< uint8_t > UI8
constexpr uint32_t MASK_10_BITS
void convert_per_component(void *dst_memory, const void *src_memory, size_t buffer_size, TextureFormat device_format)
void remap_components_3to4_front_to_back(MutableSpan< ComponentType > components, int64_t size, ComponentType component_4_value)
Inline remapping from 3 components to 4 components where the data is stored at the end of the buffer.
ComponentValue< uint32_t > UI32
constexpr uint8_t SHIFT_B
static ConversionType reversed(ConversionType type)
static ConversionType host_to_device(const eGPUDataFormat host_format, const TextureFormat host_texture_format, const TextureFormat device_format)
static ConversionType type_of_conversion_uint(TextureFormat device_format)
constexpr uint16_t HALF_VALUE_1
static uint32_t float_to_uint32_t(float value)
void convert(SignedNormalized< StorageType > &dst, const F32 &src)
ComponentValue< uint16_t > UI16
constexpr uint8_t SHIFT_R
static ConversionType type_of_conversion_float(const TextureFormat host_format, const TextureFormat device_format)
constexpr uint8_t SHIFT_G
PixelValue< float3 > FLOAT3
static ConversionType type_of_conversion_uint248(const TextureFormat device_format)
int to_bytesize(const DataFormat format)
static ConversionType type_of_conversion_ubyte(TextureFormat device_format)
ComponentValue< float > F32
void convert_host_to_device(void *dst_buffer, const void *src_buffer, size_t buffer_size, eGPUDataFormat host_format, TextureFormat host_texture_format, TextureFormat device_format)
bool needs_conversion(eGPUDataFormat host_format, TextureFormat host_texture_format, TextureFormat device_format)
Check if data conversion is needed when uploading host_format to a device_format.
ComponentValue< int32_t > I32
PixelValue< ColorSceneLinear4f< eAlpha::Premultiplied > > FLOAT4
ComponentValue< int8_t > I8
static ConversionType type_of_conversion_r10g10b10a2(TextureFormat device_format)
static void convert_buffer(void *dst_memory, const void *src_memory, size_t buffer_size, TextureFormat device_format, ConversionType type)
int to_component_len(TextureFormat format)
static ConversionType type_of_conversion_r11g11b10(TextureFormat device_format)
uint32_t convert_float_formats(uint32_t value)
static float uint32_t_to_float(uint32_t value)
void convert_per_pixel(void *dst_memory, const void *src_memory, size_t buffer_size)
static ConversionType type_of_conversion_half(TextureFormat device_format)
void convert_device_to_host(void *dst_buffer, const void *src_buffer, size_t buffer_size, eGPUDataFormat host_format, TextureFormat host_texture_format, TextureFormat device_format)
constexpr uint32_t MASK_11_BITS
static ConversionType type_of_conversion_int(TextureFormat device_format)
uint16_t float_to_half(float v)
Definition math_half.cc:32
void half_to_float_array(const uint16_t *src, float *dst, size_t length)
Definition math_half.cc:449
void float_to_half_make_finite_array(const float *src, uint16_t *dst, size_t length)
Definition math_half.cc:355
float half_to_float(uint16_t v)
Definition math_half.cc:113
void parallel_for(const IndexRange range, const int64_t grain_size, const Function &function, const TaskSizeHints &size_hints=detail::TaskSizeHints_Static(1))
Definition BLI_task.hh:69
BLI_assert(slot->slot_subtype.map==BMO_OP_SLOT_SUBTYPE_MAP_FLT)
unsigned __int64 uint64_t
Definition BKE_text.h:91
__int64 int64_t
Definition BKE_text.h:90
@ GPU_DATA_2_10_10_10_REV
@ GPU_DATA_HALF_FLOAT
@ GPU_DATA_10_11_11_REV
@ GPU_DATA_UINT_24_8_DEPRECATED
unsigned short uint16_t
Definition BKE_text.h:80
unsigned int uint32_t
Definition BKE_text.h:81
unsigned char uint8_t
Definition BKE_text.h:79
MINLINE float max_ff(float a, float b)
signed int int32_t
Definition BKE_text.h:78
MINLINE int clamp_i(int value, int min, int max)
KERNEL_FEATURE_LIGHT_PASSES KERNEL_FEATURE_PATH_GUIDING float
static constexpr int32_t scalar()
static constexpr int32_t delta()
static constexpr int32_t max()
static constexpr uint32_t max()
static constexpr size_t used_byte_size()
static constexpr uint32_t scalar()