Blender V4.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
7#include "GPU_context.hh"
8#include "GPU_framebuffer.hh"
9#include "GPU_immediate.hh"
10#include "GPU_shader.hh"
11#include "GPU_vertex_buffer.hh"
12#include "GPU_vertex_format.hh"
13
14#include "BLI_index_range.hh"
16
17#include "gpu_testing.hh"
18
19namespace blender::gpu::tests {
20
21static constexpr int Size = 256;
22
23template<GPUVertCompType comp_type, GPUVertFetchMode fetch_mode, typename ColorType>
24static void vertex_buffer_fetch_mode(ColorType color)
25{
26 GPUOffScreen *offscreen = GPU_offscreen_create(Size,
27 Size,
28 false,
32 nullptr);
33 BLI_assert(offscreen != nullptr);
34 GPU_offscreen_bind(offscreen, false);
35 GPUTexture *color_texture = GPU_offscreen_color_texture(offscreen);
36 GPU_texture_clear(color_texture, GPU_DATA_FLOAT, float4(0.0f));
37
40 GPU_vertformat_attr_add(&format, "color", comp_type, 4, fetch_mode);
41
44
45 struct Vert {
46 float2 pos;
47 ColorType color;
48 };
49 Vert data[4] = {
50 {float2(-1.0, -1.0), color},
51 {float2(1.0, -1.0), color},
52 {float2(1.0, 1.0), color},
53 {float2(-1.0, 1.0), color},
54 };
55 for (int i : IndexRange(4)) {
56 GPU_vertbuf_vert_set(vbo, i, &data[i]);
57 }
58
59 Batch *batch = GPU_batch_create(GPU_PRIM_TRI_FAN, vbo, nullptr);
62
63 GPU_offscreen_unbind(offscreen, false);
64 GPU_flush();
65
66 /* Read back data and perform some basic tests. */
67 float read_data[4 * Size * Size];
68 GPU_offscreen_read_color(offscreen, GPU_DATA_FLOAT, &read_data);
69 for (int pixel_index = 0; pixel_index < Size * Size; pixel_index++) {
70 float4 read_color = float4(&read_data[pixel_index * 4]);
71 EXPECT_EQ(read_color, float4(color));
72 }
73
76 GPU_offscreen_free(offscreen);
77}
78
83GPU_TEST(vertex_buffer_fetch_mode__GPU_COMP_I8__GPU_FETCH_INT_TO_FLOAT);
84
89GPU_TEST(vertex_buffer_fetch_mode__GPU_COMP_U8__GPU_FETCH_INT_TO_FLOAT);
90
95GPU_TEST(vertex_buffer_fetch_mode__GPU_COMP_I16__GPU_FETCH_INT_TO_FLOAT);
96
101GPU_TEST(vertex_buffer_fetch_mode__GPU_COMP_U16__GPU_FETCH_INT_TO_FLOAT);
102
107GPU_TEST(vertex_buffer_fetch_mode__GPU_COMP_I32__GPU_FETCH_INT_TO_FLOAT);
108
113GPU_TEST(vertex_buffer_fetch_mode__GPU_COMP_U32__GPU_FETCH_INT_TO_FLOAT);
114
119GPU_TEST(vertex_buffer_fetch_mode__GPU_COMP_F32__GPU_FETCH_FLOAT);
120
121} // namespace blender::gpu::tests
#define BLI_assert(a)
Definition BLI_assert.h:50
EXPECT_EQ(BLI_expr_pylike_eval(expr, nullptr, 0, &result), EXPR_PYLIKE_INVALID)
void GPU_batch_discard(blender::gpu::Batch *batch)
#define GPU_batch_create(primitive_type, vertex_buf, index_buf)
Definition GPU_batch.hh:149
void GPU_batch_program_set_builtin(blender::gpu::Batch *batch, eGPUBuiltinShader shader_id)
void GPU_batch_draw(blender::gpu::Batch *batch)
GPUOffScreen * GPU_offscreen_create(int width, int height, bool with_depth_buffer, eGPUTextureFormat format, eGPUTextureUsage usage, char err_out[256])
void GPU_offscreen_bind(GPUOffScreen *offscreen, bool save)
GPUTexture * GPU_offscreen_color_texture(const GPUOffScreen *offscreen)
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_TRI_FAN
@ GPU_SHADER_3D_FLAT_COLOR
void GPU_flush()
Definition gpu_state.cc:294
void GPU_texture_clear(GPUTexture *texture, eGPUDataFormat data_format, const void *data)
@ GPU_DATA_FLOAT
@ GPU_TEXTURE_USAGE_HOST_READ
@ GPU_TEXTURE_USAGE_ATTACHMENT
#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_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
struct @620::@622 batch
#define GPU_TEST(test_name)
format
static void test_vertex_buffer_fetch_mode__GPU_COMP_I32__GPU_FETCH_INT_TO_FLOAT()
static constexpr int Size
static void test_vertex_buffer_fetch_mode__GPU_COMP_U8__GPU_FETCH_INT_TO_FLOAT()
static void test_vertex_buffer_fetch_mode__GPU_COMP_I8__GPU_FETCH_INT_TO_FLOAT()
static void test_vertex_buffer_fetch_mode__GPU_COMP_U16__GPU_FETCH_INT_TO_FLOAT()
static void test_vertex_buffer_fetch_mode__GPU_COMP_F32__GPU_FETCH_FLOAT()
static void test_vertex_buffer_fetch_mode__GPU_COMP_U32__GPU_FETCH_INT_TO_FLOAT()
static void test_vertex_buffer_fetch_mode__GPU_COMP_I16__GPU_FETCH_INT_TO_FLOAT()
static void vertex_buffer_fetch_mode(ColorType color)
VecBase< int32_t, 4 > int4
VecBase< uint32_t, 4 > uint4
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