Blender V4.3
storage_buffer_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
9#include "BLI_math_vector.hh"
10#include "BLI_vector.hh"
11
12#include "gpu_testing.hh"
13
14namespace blender::gpu::tests {
15
16constexpr size_t SIZE = 128;
17constexpr size_t SIZE_IN_BYTES = SIZE * sizeof(int);
18
20{
22 for (int i : IndexRange(SIZE)) {
23 data.append(i);
24 }
25 return data;
26}
27
29{
30 GPUStorageBuf *ssbo = GPU_storagebuf_create_ex(
31 SIZE_IN_BYTES, nullptr, GPU_USAGE_STATIC, __func__);
32 EXPECT_NE(ssbo, nullptr);
33
34 /* Upload some dummy data. */
35 const Vector<int32_t> data = test_data();
36 GPU_storagebuf_update(ssbo, data.data());
37
38 /* Read back data from SSBO. */
39 Vector<int32_t> read_data;
40 read_data.resize(SIZE, 0);
41 GPU_storagebuf_read(ssbo, read_data.data());
42
43 /* Check if data is the same. */
44 for (int i : IndexRange(SIZE)) {
45 EXPECT_EQ(data[i], read_data[i]);
46 }
47
49}
50
51GPU_TEST(storage_buffer_create_update_read);
52
54{
55 GPUStorageBuf *ssbo = GPU_storagebuf_create_ex(
56 SIZE_IN_BYTES, nullptr, GPU_USAGE_STATIC, __func__);
57 EXPECT_NE(ssbo, nullptr);
58
59 /* Upload some dummy data. */
60 const Vector<int32_t> data = test_data();
61 GPU_storagebuf_update(ssbo, data.data());
63
64 /* Read back data from SSBO. */
65 Vector<int32_t> read_data;
66 read_data.resize(SIZE, 0);
67 GPU_storagebuf_read(ssbo, read_data.data());
68
69 /* Check if data is the same. */
70 for (int i : IndexRange(SIZE)) {
71 EXPECT_EQ(0, read_data[i]);
72 }
73
75}
76GPU_TEST(storage_buffer_clear_zero);
77
79{
80 GPUStorageBuf *ssbo = GPU_storagebuf_create_ex(
81 SIZE_IN_BYTES, nullptr, GPU_USAGE_STATIC, __func__);
82 EXPECT_NE(ssbo, nullptr);
83
84 GPU_storagebuf_clear(ssbo, 157255);
85
86 /* Read back data from SSBO. */
87 Vector<int32_t> read_data;
88 read_data.resize(SIZE, 0);
89 GPU_storagebuf_read(ssbo, read_data.data());
90
91 /* Check if datatest_ is the same. */
92 for (int i : IndexRange(SIZE)) {
93 EXPECT_EQ(157255, read_data[i]);
94 }
95
97}
98
99GPU_TEST(storage_buffer_clear);
100
102{
103 GPUStorageBuf *ssbo = GPU_storagebuf_create_ex(
104 SIZE_IN_BYTES, nullptr, GPU_USAGE_STATIC, __func__);
105 EXPECT_NE(ssbo, nullptr);
106
107 /* Create vertex buffer. */
108 GPUVertFormat format = {0};
111
113 GPU_vertbuf_data_alloc(*vbo, 4);
114
115 struct Vert {
116 float2 pos;
118 };
119 Vert data[4] = {
120 {float2(-1.0, -1.0), float4(0.0, 0.0, 0.0, 1.0)},
121 {float2(1.0, -1.0), float4(1.0, 0.0, 0.0, 1.0)},
122 {float2(1.0, 1.0), float4(1.0, 1.0, 0.0, 1.0)},
123 {float2(-1.0, 1.0), float4(0.0, 1.0, 0.0, 1.0)},
124 };
125 for (int i : IndexRange(4)) {
126 GPU_vertbuf_vert_set(vbo, i, &data[i]);
127 }
128 float *expected_data = static_cast<float *>(static_cast<void *>(&data));
129
130 Vector<float> read_data;
131 read_data.resize(SIZE, 0);
132
133 /* Copy vertex buffer to storage buffer. */
134 {
136 GPU_storagebuf_copy_sub_from_vertbuf(ssbo, vbo, 0, 0, sizeof(data));
137
138 /* Validate content of SSBO. */
139 GPU_storagebuf_read(ssbo, read_data.data());
140 EXPECT_EQ_ARRAY(expected_data, read_data.data(), 24);
141 for (int i : IndexRange(24, SIZE - 24)) {
142 EXPECT_EQ(0.0, read_data[i]);
143 }
144 }
145
146 /* Copy vertex buffer to storage buffer with 16 bytes of offset. */
147 {
149 GPU_storagebuf_copy_sub_from_vertbuf(ssbo, vbo, 16, 0, sizeof(data));
150
151 /* Validate content of SSBO. */
152 GPU_storagebuf_read(ssbo, read_data.data());
153 for (int i : IndexRange(4)) {
154 EXPECT_EQ(0.0, read_data[i]);
155 }
156 float *expected_data = static_cast<float *>(static_cast<void *>(&data));
157 EXPECT_EQ_ARRAY(expected_data, &(read_data.data()[4]), 24);
158 for (int i : IndexRange(28, SIZE - 28)) {
159 EXPECT_EQ(0.0, read_data[i]);
160 }
161 }
162
163 /* Partially Copy vertex buffer to storage buffer with 16 bytes of offset. */
164 {
166 GPU_storagebuf_copy_sub_from_vertbuf(ssbo, vbo, 16, sizeof(Vert), sizeof(data) / 2);
167
168 /* Validate content of SSBO. */
169 GPU_storagebuf_read(ssbo, read_data.data());
170 for (int i : IndexRange(4)) {
171 EXPECT_EQ(0.0, read_data[i]);
172 }
173 float *expected_data = static_cast<float *>(static_cast<void *>(&data));
174 EXPECT_EQ_ARRAY(&expected_data[6], &(read_data.data()[4]), 12);
175 for (int i : IndexRange(16, SIZE - 16)) {
176 EXPECT_EQ(0.0, read_data[i]);
177 }
178 }
179
182}
183
184GPU_TEST(storage_buffer_copy_from_vertex_buffer);
185
186} // namespace blender::gpu::tests
EXPECT_EQ(BLI_expr_pylike_eval(expr, nullptr, 0, &result), EXPR_PYLIKE_INVALID)
GPUStorageBuf * GPU_storagebuf_create_ex(size_t size, const void *data, GPUUsageType usage, const char *name)
void GPU_storagebuf_copy_sub_from_vertbuf(GPUStorageBuf *ssbo, blender::gpu::VertBuf *src, uint dst_offset, uint src_offset, uint copy_size)
Copy a part of a vertex buffer to a storage buffer.
void GPU_storagebuf_clear_to_zero(GPUStorageBuf *ssbo)
void GPU_storagebuf_free(GPUStorageBuf *ssbo)
void GPU_storagebuf_update(GPUStorageBuf *ssbo, const void *data)
void GPU_storagebuf_read(GPUStorageBuf *ssbo, void *data)
void GPU_storagebuf_clear(GPUStorageBuf *ssbo, uint32_t clear_value)
#define GPU_vertbuf_create_with_format(format)
void GPU_vertbuf_vert_set(blender::gpu::VertBuf *verts, uint v_idx, const void *data)
void GPU_vertbuf_data_alloc(blender::gpu::VertBuf &verts, uint v_len)
void GPU_vertbuf_discard(blender::gpu::VertBuf *)
@ GPU_USAGE_STATIC
@ GPU_FETCH_FLOAT
uint GPU_vertformat_attr_add(GPUVertFormat *, const char *name, GPUVertCompType, uint comp_len, GPUVertFetchMode)
@ GPU_COMP_F32
Group Output data from inside of a node group A color picker Mix two input colors RGB to Convert a color s luminance to a grayscale value Generate a normal vector and a dot product Brightness Control the brightness and contrast of the input color Vector Map input vector components with curves Camera Retrieve information about the camera and how it relates to the current shading point s position Clamp a value between a minimum and a maximum Vector Perform vector math operation Invert Invert a color
void append(const T &value)
void resize(const int64_t new_size)
draw_view push_constant(Type::INT, "radiance_src") .push_constant(Type capture_info_buf storage_buf(1, Qualifier::READ, "ObjectBounds", "bounds_buf[]") .push_constant(Type draw_view int
#define GPU_TEST(test_name)
format
static void test_storage_buffer_clear_zero()
static void test_storage_buffer_clear()
static Vector< int32_t > test_data()
static void test_storage_buffer_create_update_read()
static void test_storage_buffer_copy_from_vertex_buffer()
constexpr size_t SIZE_IN_BYTES
VecBase< float, 4 > float4
VecBase< float, 2 > float2