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{
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
struct @157336070235062372277311340362362342103123126032::@262166344314164341202215145112231240022370055142 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