Blender V5.0
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{
31 EXPECT_NE(ssbo, nullptr);
32
33 /* Upload some dummy data. */
35 GPU_storagebuf_update(ssbo, data.data());
36
37 /* Read back data from SSBO. */
38 Vector<int32_t> read_data;
39 read_data.resize(SIZE, 0);
40 GPU_storagebuf_read(ssbo, read_data.data());
41
42 /* Check if data is the same. */
43 for (int i : IndexRange(SIZE)) {
44 EXPECT_EQ(data[i], read_data[i]);
45 }
46
48}
49
50GPU_TEST(storage_buffer_create_update_read);
51
53{
55 EXPECT_NE(ssbo, nullptr);
56
57 /* Upload some dummy data. */
59 GPU_storagebuf_update(ssbo, data.data());
61
62 /* Read back data from SSBO. */
63 Vector<int32_t> read_data;
64 read_data.resize(SIZE, 0);
65 GPU_storagebuf_read(ssbo, read_data.data());
66
67 /* Check if data is the same. */
68 for (int i : IndexRange(SIZE)) {
69 EXPECT_EQ(0, read_data[i]);
70 }
71
73}
74GPU_TEST(storage_buffer_clear_zero);
75
77{
79 EXPECT_NE(ssbo, nullptr);
80
81 GPU_storagebuf_clear(ssbo, 157255);
82
83 /* Read back data from SSBO. */
84 Vector<int32_t> read_data;
85 read_data.resize(SIZE, 0);
86 GPU_storagebuf_read(ssbo, read_data.data());
87
88 /* Check if datatest_ is the same. */
89 for (int i : IndexRange(SIZE)) {
90 EXPECT_EQ(157255, read_data[i]);
91 }
92
94}
95
96GPU_TEST(storage_buffer_clear);
97
99{
101 EXPECT_NE(ssbo, nullptr);
102
103 /* Tests a different clear command on Metal. */
104 GPU_storagebuf_clear(ssbo, 0xFCFCFCFCu);
105
106 /* Read back data from SSBO. */
107 Vector<int32_t> read_data;
108 read_data.resize(SIZE, 0);
109 GPU_storagebuf_read(ssbo, read_data.data());
110
111 /* Check if datatest_ is the same. */
112 for (int i : IndexRange(SIZE)) {
113 EXPECT_EQ(0xFCFCFCFCu, read_data[i]);
114 }
115
117}
118
119GPU_TEST(storage_buffer_clear_byte_pattern);
120
122{
124 EXPECT_NE(ssbo, nullptr);
125
126 /* Create vertex buffer. */
127 GPUVertFormat format = {0};
128 GPU_vertformat_attr_add(&format, "pos", gpu::VertAttrType::SFLOAT_32_32);
129 GPU_vertformat_attr_add(&format, "color", gpu::VertAttrType::SFLOAT_32_32_32_32);
130
132 GPU_vertbuf_data_alloc(*vbo, 4);
133
134 struct Vert {
135 float2 pos;
137 };
138 Vert data[4] = {
139 {float2(-1.0, -1.0), float4(0.0, 0.0, 0.0, 1.0)},
140 {float2(1.0, -1.0), float4(1.0, 0.0, 0.0, 1.0)},
141 {float2(1.0, 1.0), float4(1.0, 1.0, 0.0, 1.0)},
142 {float2(-1.0, 1.0), float4(0.0, 1.0, 0.0, 1.0)},
143 };
144 for (int i : IndexRange(4)) {
145 GPU_vertbuf_vert_set(vbo, i, &data[i]);
146 }
147 Span<float> expected_data(static_cast<float *>(static_cast<void *>(&data)), 24);
148
149 Vector<float> read_data;
150 read_data.resize(SIZE, 0);
151
152 /* Copy vertex buffer to storage buffer. */
153 {
155 GPU_storagebuf_copy_sub_from_vertbuf(ssbo, vbo, 0, 0, sizeof(data));
156
157 /* Validate content of SSBO. */
158 GPU_storagebuf_read(ssbo, read_data.data());
159 EXPECT_EQ_SPAN<float>(expected_data, read_data.as_span().slice(IndexRange(24)));
160 for (int i : IndexRange(24, SIZE - 24)) {
161 EXPECT_EQ(0.0, read_data[i]);
162 }
163 }
164
165 /* Copy vertex buffer to storage buffer with 16 bytes of offset. */
166 {
168 GPU_storagebuf_copy_sub_from_vertbuf(ssbo, vbo, 16, 0, sizeof(data));
169
170 /* Validate content of SSBO. */
171 GPU_storagebuf_read(ssbo, read_data.data());
172 for (int i : IndexRange(4)) {
173 EXPECT_EQ(0.0, read_data[i]);
174 }
175 EXPECT_EQ_SPAN(expected_data, read_data.as_span().slice(4, 24));
176 for (int i : IndexRange(28, SIZE - 28)) {
177 EXPECT_EQ(0.0, read_data[i]);
178 }
179 }
180
181 /* Partially Copy vertex buffer to storage buffer with 16 bytes of offset. */
182 {
184 GPU_storagebuf_copy_sub_from_vertbuf(ssbo, vbo, 16, sizeof(Vert), sizeof(data) / 2);
185
186 /* Validate content of SSBO. */
187 GPU_storagebuf_read(ssbo, read_data.data());
188 for (int i : IndexRange(4)) {
189 EXPECT_EQ(0.0, read_data[i]);
190 }
191 EXPECT_EQ_SPAN(expected_data.slice(6, 12), read_data.as_span().slice(4, 12));
192 for (int i : IndexRange(16, SIZE - 16)) {
193 EXPECT_EQ(0.0, read_data[i]);
194 }
195 }
196
199}
200
201GPU_TEST(storage_buffer_copy_from_vertex_buffer);
202
203} // namespace blender::gpu::tests
EXPECT_EQ(BLI_expr_pylike_eval(expr, nullptr, 0, &result), EXPR_PYLIKE_INVALID)
void GPU_storagebuf_free(blender::gpu::StorageBuf *ssbo)
blender::gpu::StorageBuf * GPU_storagebuf_create_ex(size_t size, const void *data, GPUUsageType usage, const char *name)
void GPU_storagebuf_clear_to_zero(blender::gpu::StorageBuf *ssbo)
void GPU_storagebuf_clear(blender::gpu::StorageBuf *ssbo, uint32_t clear_value)
void GPU_storagebuf_copy_sub_from_vertbuf(blender::gpu::StorageBuf *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_update(blender::gpu::StorageBuf *ssbo, const void *data)
void GPU_storagebuf_read(blender::gpu::StorageBuf *ssbo, void *data)
void GPU_vertbuf_vert_set(blender::gpu::VertBuf *verts, uint v_idx, const void *data)
static blender::gpu::VertBuf * GPU_vertbuf_create_with_format(const GPUVertFormat &format)
void GPU_vertbuf_data_alloc(blender::gpu::VertBuf &verts, uint v_len)
void GPU_vertbuf_discard(blender::gpu::VertBuf *)
@ GPU_USAGE_STATIC
uint GPU_vertformat_attr_add(GPUVertFormat *format, blender::StringRef name, blender::gpu::VertAttrType type)
BMesh const char void * data
constexpr Span slice(int64_t start, int64_t size) const
Definition BLI_span.hh:137
void resize(const int64_t new_size)
Span< T > as_span() const
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