Blender V5.3
vk_data_conversion.hh
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
9#pragma once
10
12
14
15#include "vk_common.hh"
16
17namespace blender::gpu {
18struct VKWorkarounds;
19
27bool needs_conversion(eGPUDataFormat host_format,
28 TextureFormat host_texture_format,
29 TextureFormat device_format);
30
45void convert_host_to_device(void *dst_buffer,
46 const void *src_buffer,
47 size_t buffer_size,
48 eGPUDataFormat host_format,
49 TextureFormat host_texture_format,
50 TextureFormat device_format);
51
66void convert_device_to_host(void *dst_buffer,
67 const void *src_buffer,
68 size_t buffer_size,
69 eGPUDataFormat host_format,
70 TextureFormat host_texture_format,
71 TextureFormat device_format);
72
73/* -------------------------------------------------------------------- */
76
80template<bool HasSignBit, uint8_t MantissaBitLen, uint8_t ExponentBitLen>
82 public:
83 static constexpr bool HAS_SIGN = HasSignBit;
84 static constexpr uint8_t SIGN_SHIFT = MantissaBitLen + ExponentBitLen;
85 static constexpr uint32_t SIGN_MASK = HasSignBit ? 1 : 0;
86 static constexpr uint8_t MANTISSA_LEN = MantissaBitLen;
87 static constexpr uint8_t MANTISSA_SHIFT = 0;
88 static constexpr uint32_t MANTISSA_MASK = (1 << MantissaBitLen) - 1;
90 static constexpr uint8_t EXPONENT_SHIFT = MantissaBitLen;
91 static constexpr uint8_t EXPONENT_LEN = ExponentBitLen;
92 static constexpr uint32_t EXPONENT_MASK = (1 << ExponentBitLen) - 1;
93 static constexpr int32_t EXPONENT_BIAS = (1 << (ExponentBitLen - 1)) - 1;
95
96 static uint32_t get_mantissa(uint32_t floating_point_number)
97 {
98 return (floating_point_number >> MANTISSA_SHIFT) & MANTISSA_MASK;
99 }
100 static uint32_t clear_mantissa(uint32_t floating_point_number)
101 {
102 return floating_point_number & ~(MANTISSA_MASK << MANTISSA_SHIFT);
103 }
104 static uint32_t set_mantissa(uint32_t mantissa, uint32_t floating_point_number)
105 {
106 uint32_t result = clear_mantissa(floating_point_number);
107 result |= mantissa << MANTISSA_SHIFT;
108 return result;
109 }
110
111 static uint32_t get_exponent(uint32_t floating_point_number)
112 {
113 return ((floating_point_number >> EXPONENT_SHIFT) & EXPONENT_MASK);
114 }
115 static uint32_t clear_exponent(uint32_t floating_point_number)
116 {
117 return floating_point_number & ~(EXPONENT_MASK << EXPONENT_SHIFT);
118 }
119 static uint32_t set_exponent(uint32_t exponent, uint32_t floating_point_number)
120 {
121 uint32_t result = clear_exponent(floating_point_number);
122 result |= (exponent) << EXPONENT_SHIFT;
123 return result;
124 }
125
126 static bool is_signed(uint32_t floating_point_number)
127 {
128 if constexpr (HasSignBit) {
129 return (floating_point_number >> SIGN_SHIFT) & SIGN_MASK;
130 }
131 return false;
132 }
133 static uint32_t clear_sign(uint32_t floating_point_number)
134 {
135 return floating_point_number & ~(1 << SIGN_SHIFT);
136 }
137
138 static uint32_t set_sign(bool sign, uint32_t floating_point_number)
139 {
140 if constexpr (!HasSignBit) {
141 return floating_point_number;
142 }
143 uint32_t result = clear_sign(floating_point_number);
145 return result;
146 }
147};
148
152
161template<
165 typename DestinationFormat,
166
170 typename SourceFormat,
171
179 bool ClampNegativeToZero = true>
181{
182 bool is_signed = SourceFormat::is_signed(value);
183 uint32_t mantissa = SourceFormat::get_mantissa(value);
184 int32_t exponent = SourceFormat::get_exponent(value);
185
186 const bool is_nan = (exponent == SourceFormat::EXPONENT_SPECIAL_MASK) && mantissa;
187 const bool is_inf = (exponent == SourceFormat::EXPONENT_SPECIAL_MASK) && (mantissa == 0);
188 const bool is_zero = (exponent == 0 && mantissa == 0);
189
190 /* Sign conversion */
191 if constexpr (!DestinationFormat::HAS_SIGN && ClampNegativeToZero) {
192 if (is_signed && !is_nan) {
193 return 0;
194 }
195 }
196 if (is_zero) {
197 return 0;
198 }
199
200 if (is_inf) {
201 exponent = DestinationFormat::EXPONENT_SPECIAL_MASK;
202 }
203 else if (is_nan) {
204 exponent = DestinationFormat::EXPONENT_SPECIAL_MASK;
205 mantissa = DestinationFormat::MANTISSA_NAN_MASK;
206 }
207 else {
208 /* Exponent conversion */
209 exponent -= SourceFormat::EXPONENT_BIAS;
210 /* Clamping when destination has lower precision. */
211 if constexpr (SourceFormat::EXPONENT_LEN > DestinationFormat::EXPONENT_LEN) {
212 if (exponent > DestinationFormat::EXPONENT_BIAS) {
213 exponent = 0;
214 mantissa = SourceFormat::MANTISSA_MASK;
215 }
216 else if (exponent < -DestinationFormat::EXPONENT_BIAS) {
217 return 0;
218 }
219 }
220 exponent += DestinationFormat::EXPONENT_BIAS;
221
222 /* Mantissa conversion */
223 if constexpr (SourceFormat::MANTISSA_LEN > DestinationFormat::MANTISSA_LEN) {
224 mantissa = mantissa >> (SourceFormat::MANTISSA_LEN - DestinationFormat::MANTISSA_LEN);
225 }
226 else if constexpr (SourceFormat::MANTISSA_LEN < DestinationFormat::MANTISSA_LEN) {
227 mantissa = mantissa << (DestinationFormat::MANTISSA_LEN - SourceFormat::MANTISSA_LEN);
228 }
229 }
230
231 uint32_t result = 0;
232 result = DestinationFormat::set_sign(is_signed, result);
233 result = DestinationFormat::set_exponent(exponent, result);
234 result = DestinationFormat::set_mantissa(mantissa, result);
235 return result;
236}
237
239
240}; // namespace blender::gpu
static uint32_t get_mantissa(uint32_t floating_point_number)
static uint32_t clear_mantissa(uint32_t floating_point_number)
static uint32_t set_mantissa(uint32_t mantissa, uint32_t floating_point_number)
static uint32_t clear_exponent(uint32_t floating_point_number)
static uint32_t clear_sign(uint32_t floating_point_number)
static uint32_t set_exponent(uint32_t exponent, uint32_t floating_point_number)
static uint32_t get_exponent(uint32_t floating_point_number)
static bool is_signed(uint32_t floating_point_number)
static uint32_t set_sign(bool sign, uint32_t floating_point_number)
constexpr T sign(T) RET
bool is_zero(float3x3 a)
FloatingPointFormat< false, 6, 5 > FormatF11
FloatingPointFormat< false, 5, 5 > FormatF10
FloatingPointFormat< true, 23, 8 > FormatF32
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.
uint32_t convert_float_formats(uint32_t value)
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)
unsigned int uint32_t
Definition BKE_text.h:81
unsigned char uint8_t
Definition BKE_text.h:79
signed int int32_t
Definition BKE_text.h:78