Blender V5.0
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 blender::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
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
50 GPU_vertbuf_data_alloc(*vbo, data.size());
51
52 for (int i : IndexRange(data.size())) {
54 }
55
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 switch (attr_type) {
69 case VertAttrType::SNORM_8_8_8_8:
70 read_data[0] = read_data[0] * float(127);
71 break;
72 case VertAttrType::UNORM_8_8_8_8:
73 read_data[0] = read_data[0] * float(255);
74 break;
75 case VertAttrType::SNORM_16_16_16_16:
76 read_data[0] = read_data[0] * float(32767);
77 break;
78 case VertAttrType::UNORM_16_16_16_16:
79 read_data[0] = read_data[0] * float(65535);
80 break;
81 case VertAttrType::SNORM_10_10_10_2:
82 read_data[0] = read_data[0] * float4(511, 511, 511, 1);
83 break;
84 case VertAttrType::SFLOAT_32_32_32_32:
85 break;
86 default:
88 }
89
90 if (attr_type == VertAttrType::SFLOAT_32_32_32_32) {
91 EXPECT_EQ(read_data[0], float4(color));
92 }
93 else {
94 /* Do integer comparison to avoid floating point inaccuracies from each conversion steps. */
95 EXPECT_EQ(int4(read_data[0]), int4(float4(color)));
96 }
97
99 GPU_offscreen_free(offscreen);
100}
101
106GPU_TEST(vertex_buffer_fetch_mode__GPU_COMP_I8__GPU_FETCH_INT_TO_FLOAT_UNIT);
107
112GPU_TEST(vertex_buffer_fetch_mode__GPU_COMP_U8__GPU_FETCH_INT_TO_FLOAT_UNIT);
113
119GPU_TEST(vertex_buffer_fetch_mode__GPU_COMP_I16__GPU_FETCH_INT_TO_FLOAT_UNIT);
120
126GPU_TEST(vertex_buffer_fetch_mode__GPU_COMP_U16__GPU_FETCH_INT_TO_FLOAT_UNIT);
127
133GPU_TEST(vertex_buffer_fetch_mode__GPU_COMP_I10__GPU_FETCH_INT_TO_FLOAT_UNIT);
134
139GPU_TEST(vertex_buffer_fetch_mode__GPU_COMP_F32__GPU_FETCH_FLOAT);
140
141} // namespace blender::gpu::tests
#define BLI_assert_unreachable()
Definition BLI_assert.h:93
#define BLI_assert(a)
Definition BLI_assert.h:46
EXPECT_EQ(BLI_expr_pylike_eval(expr, nullptr, 0, &result), EXPR_PYLIKE_INVALID)
void GPU_batch_discard(blender::gpu::Batch *batch)
void GPU_batch_program_set_builtin(blender::gpu::Batch *batch, GPUBuiltinShader shader_id)
blender::gpu::Batch * GPU_batch_create_ex(GPUPrimType primitive_type, blender::gpu::VertBuf *vertex_buf, blender::gpu::IndexBuf *index_buf, GPUBatchFlag owns_flag)
Definition gpu_batch.cc:51
@ GPU_BATCH_OWNS_VBO
Definition GPU_batch.hh:42
void GPU_batch_draw(blender::gpu::Batch *batch)
blender::gpu::Texture * GPU_offscreen_color_texture(const GPUOffScreen *offscreen)
void GPU_offscreen_bind(GPUOffScreen *offscreen, bool save)
GPUOffScreen * GPU_offscreen_create(int width, int height, bool with_depth_buffer, blender::gpu::TextureFormat format, eGPUTextureUsage usage, bool clear, char err_out[256])
void GPU_offscreen_free(GPUOffScreen *offscreen)
void GPU_offscreen_read_color(GPUOffScreen *offscreen, eGPUDataFormat data_format, void *r_data)
void GPU_offscreen_unbind(GPUOffScreen *offscreen, bool restore)
@ GPU_PRIM_TRIS
@ GPU_SHADER_3D_FLAT_COLOR
void GPU_flush()
Definition gpu_state.cc:305
void GPU_texture_clear(blender::gpu::Texture *texture, eGPUDataFormat data_format, const void *data)
@ GPU_DATA_FLOAT
eGPUTextureUsage
@ GPU_TEXTURE_USAGE_HOST_READ
@ GPU_TEXTURE_USAGE_ATTACHMENT
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)
uint GPU_vertformat_attr_add(GPUVertFormat *format, blender::StringRef name, blender::gpu::VertAttrType type)
BMesh const char void * data
nullptr float
struct @021025263243242147216143265077100330027142264337::@225245033123204053237120173316075113304004012000 batch
uint pos
#define GPU_TEST(test_name)
format
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()
static void test_vertex_buffer_fetch_mode__GPU_COMP_I16__GPU_FETCH_INT_TO_FLOAT_UNIT()
VecBase< int32_t, 4 > int4
blender::VecBase< int16_t, 4 > short4
blender::VecBase< uint8_t, 4 > uchar4
VecBase< float, 4 > float4
blender::VecBase< int8_t, 4 > char4
VecBase< float, 2 > float2
blender::VecBase< uint16_t, 4 > ushort4
i
Definition text_draw.cc:230