Blender V4.3
immediate_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_framebuffer.hh"
8#include "GPU_immediate.hh"
10#include "gpu_testing.hh"
11
12#include "BLI_math_vector.hh"
13
14namespace blender::gpu::tests {
15
16static constexpr int Size = 256;
17
19{
20 GPUOffScreen *offscreen = GPU_offscreen_create(Size,
21 Size,
22 false,
26 nullptr);
27 BLI_assert(offscreen != nullptr);
28 GPU_offscreen_bind(offscreen, false);
29
32
34
35 float4 color(1.0, 0.5, 0.25, 1.0);
36 immUniformColor4fv(color);
38 immVertex3f(pos, -1.0f, 1.0f, 0.0f);
39 immVertex3f(pos, 1.0f, 1.0f, 0.0f);
40 immVertex3f(pos, -1.0f, -1.0f, 0.0f);
41 immVertex3f(pos, 1.0f, -1.0f, 0.0f);
42 immEnd();
43
44 GPU_offscreen_unbind(offscreen, false);
45 GPU_flush();
46
47 /* Read back data and perform some basic tests. */
48 float read_data[4 * Size * Size];
49 GPU_offscreen_read_color(offscreen, GPU_DATA_FLOAT, &read_data);
50 for (int pixel_index = 0; pixel_index < Size * Size; pixel_index++) {
51 float4 read_color = float4(&read_data[pixel_index * 4]);
52 EXPECT_EQ(read_color, color);
53 }
54
55 GPU_offscreen_free(offscreen);
56}
57GPU_TEST(immediate_one_plane)
58
59
66{
67 GPUOffScreen *offscreen = GPU_offscreen_create(Size,
68 Size,
69 false,
73 nullptr);
74 BLI_assert(offscreen != nullptr);
75 GPU_offscreen_bind(offscreen, false);
76
79
81
82 float4 color(1.0, 0.5, 0.25, 1.0);
83 immUniformColor4fv(color);
85 immVertex3f(pos, -1.0f, 1.0f, 0.0f);
86 immVertex3f(pos, 0.0f, 1.0f, 0.0f);
87 immVertex3f(pos, -1.0f, -1.0f, 0.0f);
88 immVertex3f(pos, 0.0f, -1.0f, 0.0f);
89 immEnd();
90
91 float4 color2(0.25, 0.5, 1.0, 1.0);
92 immUniformColor4fv(color2);
94 immVertex3f(pos, 0.0f, 1.0f, 0.0f);
95 immVertex3f(pos, 1.0f, 1.0f, 0.0f);
96 immVertex3f(pos, 0.0f, -1.0f, 0.0f);
97 immVertex3f(pos, 1.0f, -1.0f, 0.0f);
98 immEnd();
99
100 GPU_offscreen_unbind(offscreen, false);
101 GPU_flush();
102
103 /* Read back data and perform some basic tests.
104 * Not performing detailed tests as there might be driver specific limitations. */
105 float read_data[4 * Size * Size];
106 GPU_offscreen_read_color(offscreen, GPU_DATA_FLOAT, &read_data);
107 int64_t color_num = 0;
108 int64_t color2_num = 0;
109 for (int pixel_index = 0; pixel_index < Size * Size; pixel_index++) {
110 float4 read_color = float4(&read_data[pixel_index * 4]);
111 if (read_color == color) {
112 color_num++;
113 }
114 else if (read_color == color2) {
115 color2_num++;
116 }
117 else {
118 EXPECT_TRUE(read_color == color || read_color == color2);
119 }
120 }
121 EXPECT_TRUE(color_num > 0);
122 EXPECT_TRUE(color2_num > 0);
123
124 GPU_offscreen_free(offscreen);
125}
126GPU_TEST(immediate_two_planes)
127
128} // 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)
unsigned int uint
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)
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)
void immEnd()
void immBindBuiltinProgram(eGPUBuiltinShader shader_id)
void immVertex3f(uint attr_id, float x, float y, float z)
GPUVertFormat * immVertexFormat()
void immUniformColor4fv(const float rgba[4])
void immBegin(GPUPrimType, uint vertex_len)
@ GPU_PRIM_TRI_STRIP
@ GPU_SHADER_3D_UNIFORM_COLOR
void GPU_flush()
Definition gpu_state.cc:294
@ GPU_DATA_FLOAT
@ GPU_TEXTURE_USAGE_HOST_READ
@ GPU_TEXTURE_USAGE_ATTACHMENT
@ GPU_FETCH_FLOAT
uint GPU_vertformat_attr_add(GPUVertFormat *, const char *name, GPUVertCompType, uint comp_len, GPUVertFetchMode)
@ GPU_COMP_F32
#define GPU_TEST(test_name)
format
static void test_immediate_one_plane()
static constexpr int Size
static void test_immediate_two_planes()
VecBase< float, 4 > float4
__int64 int64_t
Definition stdint.h:89