Blender V4.5
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. */
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. */
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 /* Tests a different clear command on Metal. */
108 GPU_storagebuf_clear(ssbo, 0xFCFCFCFCu);
109
110 /* Read back data from SSBO. */
111 Vector<int32_t> read_data;
112 read_data.resize(SIZE, 0);
113 GPU_storagebuf_read(ssbo, read_data.data());
114
115 /* Check if datatest_ is the same. */
116 for (int i : IndexRange(SIZE)) {
117 EXPECT_EQ(0xFCFCFCFCu, read_data[i]);
118 }
119
121}
122
123GPU_TEST(storage_buffer_clear_byte_pattern);
124
126{
127 GPUStorageBuf *ssbo = GPU_storagebuf_create_ex(
128 SIZE_IN_BYTES, nullptr, GPU_USAGE_STATIC, __func__);
129 EXPECT_NE(ssbo, nullptr);
130
131 /* Create vertex buffer. */
132 GPUVertFormat format = {0};
135
137 GPU_vertbuf_data_alloc(*vbo, 4);
138
139 struct Vert {
140 float2 pos;
142 };
143 Vert data[4] = {
144 {float2(-1.0, -1.0), float4(0.0, 0.0, 0.0, 1.0)},
145 {float2(1.0, -1.0), float4(1.0, 0.0, 0.0, 1.0)},
146 {float2(1.0, 1.0), float4(1.0, 1.0, 0.0, 1.0)},
147 {float2(-1.0, 1.0), float4(0.0, 1.0, 0.0, 1.0)},
148 };
149 for (int i : IndexRange(4)) {
150 GPU_vertbuf_vert_set(vbo, i, &data[i]);
151 }
152 float *expected_data = static_cast<float *>(static_cast<void *>(&data));
153
154 Vector<float> read_data;
155 read_data.resize(SIZE, 0);
156
157 /* Copy vertex buffer to storage buffer. */
158 {
160 GPU_storagebuf_copy_sub_from_vertbuf(ssbo, vbo, 0, 0, sizeof(data));
161
162 /* Validate content of SSBO. */
163 GPU_storagebuf_read(ssbo, read_data.data());
164 EXPECT_EQ_ARRAY(expected_data, read_data.data(), 24);
165 for (int i : IndexRange(24, SIZE - 24)) {
166 EXPECT_EQ(0.0, read_data[i]);
167 }
168 }
169
170 /* Copy vertex buffer to storage buffer with 16 bytes of offset. */
171 {
173 GPU_storagebuf_copy_sub_from_vertbuf(ssbo, vbo, 16, 0, sizeof(data));
174
175 /* Validate content of SSBO. */
176 GPU_storagebuf_read(ssbo, read_data.data());
177 for (int i : IndexRange(4)) {
178 EXPECT_EQ(0.0, read_data[i]);
179 }
180 float *expected_data = static_cast<float *>(static_cast<void *>(&data));
181 EXPECT_EQ_ARRAY(expected_data, &(read_data.data()[4]), 24);
182 for (int i : IndexRange(28, SIZE - 28)) {
183 EXPECT_EQ(0.0, read_data[i]);
184 }
185 }
186
187 /* Partially Copy vertex buffer to storage buffer with 16 bytes of offset. */
188 {
190 GPU_storagebuf_copy_sub_from_vertbuf(ssbo, vbo, 16, sizeof(Vert), sizeof(data) / 2);
191
192 /* Validate content of SSBO. */
193 GPU_storagebuf_read(ssbo, read_data.data());
194 for (int i : IndexRange(4)) {
195 EXPECT_EQ(0.0, read_data[i]);
196 }
197 float *expected_data = static_cast<float *>(static_cast<void *>(&data));
198 EXPECT_EQ_ARRAY(&expected_data[6], &(read_data.data()[4]), 12);
199 for (int i : IndexRange(16, SIZE - 16)) {
200 EXPECT_EQ(0.0, read_data[i]);
201 }
202 }
203
206}
207
208GPU_TEST(storage_buffer_copy_from_vertex_buffer);
209
210} // 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 *, blender::StringRef name, GPUVertCompType, uint comp_len, GPUVertFetchMode)
@ GPU_COMP_F32
BMesh const char void * data
void resize(const int64_t new_size)
uint pos
#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
static void test_storage_buffer_clear_byte_pattern()
VecBase< float, 4 > float4
VecBase< float, 2 > float2
i
Definition text_draw.cc:230