Blender V5.0
vk_data_conversion_test.cc
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2023 Blender Authors
2 *
3 * SPDX-License-Identifier: Apache-2.0 */
4
5#include "testing/testing.h"
6
8#include "vk_device.hh"
9
11
12namespace blender::gpu::tests {
13
14TEST(VulkanDataConversion, clamp_negative_to_zero)
15{
16 const uint32_t f32_2 = 0b11000000000000000000000000000000;
17 const uint32_t f32_inf_min = 0b11111111100000000000000000000000;
18 const uint32_t f32_inf_max = 0b01111111100000000000000000000000;
19 const uint32_t f32_nan = 0b11111111111111111111111111111111;
20
21 /* F32(-2) doesn't fit in F11 as F11 only supports unsigned values. Clamp to zero. */
22 const uint32_t f11_0_expected = 0b00000000000;
23 const uint32_t f11_2_expected = 0b10000000000;
24 const uint32_t f11_inf_expected = 0b11111000000;
25 const uint32_t f11_nan_expected = 0b11111111111;
26 {
27 const uint32_t f11_0 = convert_float_formats<FormatF11, FormatF32, true>(f32_2);
28 EXPECT_EQ(f11_0, f11_0_expected);
29 const uint32_t f11_0b = convert_float_formats<FormatF11, FormatF32, true>(f32_inf_min);
30 EXPECT_EQ(f11_0b, f11_0_expected);
31 const uint32_t f11_inf = convert_float_formats<FormatF11, FormatF32, true>(f32_inf_max);
32 EXPECT_EQ(f11_inf, f11_inf_expected);
33 const uint32_t f11_nan = convert_float_formats<FormatF11, FormatF32, true>(f32_nan);
34 EXPECT_EQ(f11_nan, f11_nan_expected);
35 }
36
37 /* F32(-2) doesn't fit in F11 as F11 only supports unsigned values. Make absolute. */
38 {
39 const uint32_t f11_2 = convert_float_formats<FormatF11, FormatF32, false>(f32_2);
40 EXPECT_EQ(f11_2, f11_2_expected);
41 const uint32_t f11_inf = convert_float_formats<FormatF11, FormatF32, false>(f32_inf_min);
42 EXPECT_EQ(f11_inf, f11_inf_expected);
43 const uint32_t f11_infb = convert_float_formats<FormatF11, FormatF32, false>(f32_inf_max);
44 EXPECT_EQ(f11_infb, f11_inf_expected);
45 const uint32_t f11_nan = convert_float_formats<FormatF11, FormatF32, false>(f32_nan);
46 EXPECT_EQ(f11_nan, f11_nan_expected);
47 }
48}
49
50TEST(VulkanDataConversion, infinity_upper)
51{
52 const uint32_t f32_inf = 0b01111111100000000000000000000000;
53
54 const uint32_t f11_inf_expected = 0b11111000000;
55 const uint32_t f11_inf = convert_float_formats<FormatF11, FormatF32, true>(f32_inf);
56 EXPECT_EQ(f11_inf, f11_inf_expected);
57
58 const uint32_t f10_inf_expected = 0b1111100000;
59 const uint32_t f10_inf = convert_float_formats<FormatF10, FormatF32, true>(f32_inf);
60 EXPECT_EQ(f10_inf, f10_inf_expected);
61}
62
63TEST(VulkanDataConversion, texture_rgb16f_as_floats_to_rgba16f)
64{
65 const size_t num_pixels = 4;
66 const float input[] = {
67 1.0,
68 0.5,
69 0.2,
70
71 0.2,
72 1.0,
73 0.3,
74
75 0.4,
76 0.2,
77 1.0,
78
79 1.0,
80 1.0,
81 1.0,
82 };
83
84 uint64_t device[num_pixels];
86 input,
87 num_pixels,
89 TextureFormat::SFLOAT_16_16_16,
90 TextureFormat::SFLOAT_16_16_16_16);
91
92 float read_back[num_pixels * 3];
93 convert_device_to_host(read_back,
94 device,
95 num_pixels,
97 TextureFormat::SFLOAT_16_16_16,
98 TextureFormat::SFLOAT_16_16_16_16);
99
100 for (int i : IndexRange(num_pixels * 3)) {
101 EXPECT_NEAR(input[i], read_back[i], 0.01);
102 }
103}
104
105TEST(VulkanDataConversion, texture_rgb32f_as_floats_to_rgba32f)
106{
107 const size_t num_pixels = 4;
108 const float input[] = {
109 1.0,
110 0.5,
111 0.2,
112
113 0.2,
114 1.0,
115 0.3,
116
117 0.4,
118 0.2,
119 1.0,
120
121 1.0,
122 1.0,
123 1.0,
124 };
125
126 float device[num_pixels * 4];
128 input,
129 num_pixels,
131 TextureFormat::SFLOAT_32_32_32,
132 TextureFormat::SFLOAT_32_32_32_32);
133
134 float read_back[num_pixels * 3];
135 convert_device_to_host(read_back,
136 device,
137 num_pixels,
139 TextureFormat::SFLOAT_32_32_32,
140 TextureFormat::SFLOAT_32_32_32_32);
141
142 for (int i : IndexRange(num_pixels * 3)) {
143 EXPECT_NEAR(input[i], read_back[i], 0.01);
144 }
145}
146
147} // namespace blender::gpu::tests
EXPECT_EQ(BLI_expr_pylike_eval(expr, nullptr, 0, &result), EXPR_PYLIKE_INVALID)
@ GPU_DATA_FLOAT
unsigned long long int uint64_t
#define input
TEST(VulkanDataConversion, clamp_negative_to_zero)
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)
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)
i
Definition text_draw.cc:230