Blender V5.3
vertex_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#include "GPU_batch.hh"
9#include "GPU_framebuffer.hh"
10#include "GPU_shader.hh"
11#include "GPU_state.hh"
12#include "GPU_vertex_buffer.hh"
13#include "GPU_vertex_format.hh"
14
15#include "BLI_index_range.hh"
17
18#include "gpu_testing.hh"
19
20namespace blender::gpu::tests {
21
22static constexpr int Size = 2;
23
24template<VertAttrType attr_type, typename ColorType>
25static void vertex_buffer_fetch_mode(ColorType color)
26{
29 Size, Size, false, TextureFormat::SFLOAT_32_32_32_32, usage, false, nullptr);
30 BLI_assert(offscreen != nullptr);
31 GPU_offscreen_bind(offscreen, false);
32 gpu::Texture *color_texture = GPU_offscreen_color_texture(offscreen);
33 GPU_texture_clear(color_texture, GPU_DATA_FLOAT, float4(1.0f, 2.0f, 3.0f, 0.0f));
34
35 GPUVertFormat format = {0};
36 GPU_vertformat_attr_add(&format, "pos", gpu::VertAttrType::SFLOAT_32_32);
37 GPU_vertformat_attr_add(&format, "color", attr_type);
38
39 struct Vert {
40 float2 pos;
41 ColorType color;
42 };
43 std::array<Vert, 3> data = {
44 Vert{float2(-1.0, -1.0), color},
45 Vert{float2(3.0, -1.0), color},
46 Vert{float2(-1.0, 3.0), color},
47 };
48
49 VertBuf *vbo = GPU_vertbuf_create_with_format(format);
50 GPU_vertbuf_data_alloc(*vbo, data.size());
51
52 for (int i : IndexRange(data.size())) {
53 GPU_vertbuf_vert_set(vbo, i, &data[i]);
54 }
55
56 Batch *batch = GPU_batch_create_ex(GPU_PRIM_TRIS, vbo, nullptr, GPU_BATCH_OWNS_VBO);
58 GPU_batch_draw(batch);
59
60 GPU_offscreen_unbind(offscreen, false);
61 GPU_flush();
62
63 /* Read back data and perform some basic tests. */
64 Vector<float4> read_data(Size * Size);
65
66 GPU_offscreen_read_color(offscreen, GPU_DATA_FLOAT, read_data.data());
67
68 /* Do integer comparison to avoid floating point inaccuracies from each conversion steps. */
69 if constexpr (std::is_same_v<ColorType, char4_norm> || std::is_same_v<ColorType, short2_norm> ||
70 std::is_same_v<ColorType, short4_norm> ||
71 std::is_same_v<ColorType, int1010102_norm>)
72 {
73
74 EXPECT_EQ(int4(ColorType(read_data[0])), int4(color));
75 }
76 else if constexpr (std::is_same_v<ColorType, uchar4_norm> ||
77 std::is_same_v<ColorType, ushort2_norm> ||
78 std::is_same_v<ColorType, ushort4_norm> ||
79 std::is_same_v<ColorType, uint1010102_norm>)
80 {
81 EXPECT_EQ(uint4(ColorType(read_data[0])), uint4(color));
82 }
83 else if constexpr (std::is_same_v<ColorType, float4>) {
84 EXPECT_EQ(read_data[0], float4(color));
85 }
86 else {
87 static_assert(false, "Missing implementation");
88 }
89
90 GPU_batch_discard(batch);
91 GPU_offscreen_free(offscreen);
92}
93
98GPU_TEST(vertex_buffer_fetch_mode__GPU_COMP_I8__GPU_FETCH_INT_TO_FLOAT_UNIT);
99
104GPU_TEST(vertex_buffer_fetch_mode__GPU_COMP_U8__GPU_FETCH_INT_TO_FLOAT_UNIT);
105
111GPU_TEST(vertex_buffer_fetch_mode__GPU_COMP_I16__GPU_FETCH_INT_TO_FLOAT_UNIT);
112
118GPU_TEST(vertex_buffer_fetch_mode__GPU_COMP_U16__GPU_FETCH_INT_TO_FLOAT_UNIT);
119
125GPU_TEST(vertex_buffer_fetch_mode__GPU_COMP_I10__GPU_FETCH_INT_TO_FLOAT_UNIT);
126
131GPU_TEST(vertex_buffer_fetch_mode__GPU_COMP_F32__GPU_FETCH_FLOAT);
132
134{
135 GPUVertFormat format = {0};
136 GPU_vertformat_attr_add(&format, "pos", gpu::VertAttrType::SFLOAT_32_32);
137 GPU_vertformat_attr_add(&format, "color", gpu::VertAttrType::SFLOAT_32_32_32_32);
138
139 struct Vert {
140 float2 pos;
142 };
143
144 /* Create source vertex buffer with known data. */
145 VertBuf *src_vbo = GPU_vertbuf_create_with_format(format);
146 GPU_vertbuf_data_alloc(*src_vbo, 4);
147
148 Vert src_data[4] = {
149 {float2(-1.0, -1.0), float4(0.0, 0.0, 0.0, 1.0)},
150 {float2(1.0, -1.0), float4(1.0, 0.0, 0.0, 1.0)},
151 {float2(1.0, 1.0), float4(1.0, 1.0, 0.0, 1.0)},
152 {float2(-1.0, 1.0), float4(0.0, 1.0, 0.0, 1.0)},
153 };
154 for (int i : IndexRange(4)) {
155 GPU_vertbuf_vert_set(src_vbo, i, &src_data[i]);
156 }
157 GPU_vertbuf_use(src_vbo);
158
159 /* Create destination vertex buffer, pre-filled with sentinel data. */
160 VertBuf *dst_vbo = GPU_vertbuf_create_with_format(format);
161 GPU_vertbuf_data_alloc(*dst_vbo, 4);
162
163 Vert dest_data[4] = {
164 {float2(-2.0, -2.0), float4(0.0, 0.0, 0.0, 0.0)},
165 {float2(-1.0, -2.0), float4(0.0, 0.0, 0.0, 0.0)},
166 {float2(0.0, -2.0), float4(0.0, 0.0, 0.0, 0.0)},
167 {float2(1.0, -2.0), float4(0.0, 0.0, 0.0, 0.0)},
168 };
169 for (int i : IndexRange(4)) {
170 GPU_vertbuf_vert_set(dst_vbo, i, &dest_data[i]);
171 }
172 GPU_vertbuf_use(dst_vbo);
173
174 /* Copy vertices [1, 3) from the source buffer to [0, 2) of the destination buffer. */
175 dst_vbo->copy_sub(*src_vbo, 1, 0, 2);
176
177 /* Read back and validate content. */
178 Vert read_data[4];
179 GPU_vertbuf_read(dst_vbo, read_data);
180 for (int i : IndexRange(2)) {
181 EXPECT_EQ(read_data[i].pos, src_data[i + 1].pos);
182 EXPECT_EQ(read_data[i].color, src_data[i + 1].color);
183 }
184 for (int i : IndexRange(2, 2)) {
185 EXPECT_EQ(read_data[i].pos, dest_data[i].pos);
186 EXPECT_EQ(read_data[i].color, dest_data[i].color);
187 }
188
189 GPU_vertbuf_discard(dst_vbo);
190 GPU_vertbuf_discard(src_vbo);
191}
192
193GPU_TEST(vertex_buffer_copy_sub);
194
195} // namespace blender::gpu::tests
constexpr IndexRange()=default
virtual void copy_sub(VertBuf &source_buf, uint source_first_vertex, uint dest_first_vertex, uint vertex_len)=0
Copy from part of source vertex buffer into the vertex buffer.
static void test_vertex_buffer_copy_sub()
static void test_vertex_buffer_fetch_mode__GPU_COMP_I8__GPU_FETCH_INT_TO_FLOAT_UNIT()
static constexpr int Size
static void vertex_buffer_fetch_mode(ColorType color)
static void test_vertex_buffer_fetch_mode__GPU_COMP_F32__GPU_FETCH_FLOAT()
static void test_vertex_buffer_fetch_mode__GPU_COMP_U16__GPU_FETCH_INT_TO_FLOAT_UNIT()
static void test_vertex_buffer_fetch_mode__GPU_COMP_I10__GPU_FETCH_INT_TO_FLOAT_UNIT()
static void test_vertex_buffer_fetch_mode__GPU_COMP_U8__GPU_FETCH_INT_TO_FLOAT_UNIT()
GPU_TEST(compute_indirect)
static void test_vertex_buffer_fetch_mode__GPU_COMP_I16__GPU_FETCH_INT_TO_FLOAT_UNIT()
BLI_assert(slot->slot_subtype.map==BMO_OP_SLOT_SUBTYPE_MAP_FLT)
void GPU_offscreen_free(GPUOffScreen *offscreen)
gpu::Batch * GPU_batch_create_ex(GPUPrimType primitive_type, gpu::VertBuf *vertex_buf, gpu::IndexBuf *index_buf, GPUBatchFlag owns_flag)
Definition gpu_batch.cc:53
VecBase< uint32_t, 4 > uint4
void GPU_vertbuf_read(const gpu::VertBuf *verts, void *data)
void GPU_batch_discard(gpu::Batch *batch)
void GPU_vertbuf_use(gpu::VertBuf *)
void GPU_offscreen_unbind(GPUOffScreen *offscreen, bool restore)
@ GPU_TEXTURE_USAGE_HOST_READ
@ GPU_TEXTURE_USAGE_ATTACHMENT
BMesh const char void * data
void GPU_batch_draw(gpu::Batch *batch)
@ GPU_BATCH_OWNS_VBO
Definition GPU_batch.hh:44
void GPU_offscreen_bind(GPUOffScreen *offscreen, bool save)
uint GPU_vertformat_attr_add(GPUVertFormat *format, StringRef name, gpu::VertAttrType type)
void GPU_vertbuf_vert_set(gpu::VertBuf *verts, uint v_idx, const void *data)
void GPU_batch_program_set_builtin(gpu::Batch *batch, GPUBuiltinShader shader_id)
EXPECT_EQ(BLI_expr_pylike_eval(expr, nullptr, 0, &result), EXPR_PYLIKE_INVALID)
void GPU_flush()
Definition gpu_state.cc:284
gpu::Texture * GPU_offscreen_color_texture(const GPUOffScreen *offscreen)
void GPU_texture_clear(gpu::Texture *texture, eGPUDataFormat data_format, const void *data)
void GPU_vertbuf_data_alloc(gpu::VertBuf &verts, uint v_len)
GPUOffScreen * GPU_offscreen_create(int width, int height, bool with_depth_buffer, gpu::TextureFormat format, eGPUTextureUsage usage, bool clear, char err_out[256])
void GPU_vertbuf_discard(gpu::VertBuf *)
void GPU_offscreen_read_color(GPUOffScreen *offscreen, eGPUDataFormat data_format, void *r_data)