Blender V5.0
state_blend_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 "GPU_batch_utils.hh"
6#include "testing/testing.h"
7
8#include "gpu_testing.hh"
9
10#include "GPU_batch.hh"
11#include "GPU_batch_presets.hh"
12#include "GPU_framebuffer.hh"
13#include "GPU_state.hh"
14
15#include "BLI_math_vector.hh"
16
17#include "intern/draw_cache.hh"
18
19namespace blender::gpu::tests {
20
21template<GPUBlend blend_type>
22void blend_test(float4 source_a, float4 source_b, float4 expected_result)
23{
25 1,
26 false,
27 TextureFormat::SFLOAT_16_16_16_16,
30 false,
31 nullptr);
32 BLI_assert(offscreen != nullptr);
33 GPU_offscreen_bind(offscreen, false);
34 blender::gpu::Texture *color_texture = GPU_offscreen_color_texture(offscreen);
35 GPU_texture_clear(color_texture, GPU_DATA_FLOAT, source_a);
36
38
40 GPU_batch_uniform_4fv(batch, "color", source_b);
41 GPU_blend(blend_type);
42
44 GPU_offscreen_unbind(offscreen, false);
45 GPU_flush();
46
47 float4 read_back;
49 GPU_offscreen_read_color(offscreen, GPU_DATA_FLOAT, &read_back);
50 EXPECT_EQ(read_back, expected_result);
51
52 GPU_offscreen_free(offscreen);
53
54 /* Reset default. */
56}
57
58static void test_blend_none()
59{
60 blend_test<GPU_BLEND_NONE>(float4(1.0f, 0.0f, 1.0f, 1.0f),
61 float4(0.0f, 1.0f, 0.0f, 0.5f),
62 float4(0.0f, 1.0f, 0.0f, 0.5f));
63}
64GPU_TEST(blend_none)
65
66static void test_blend_alpha()
67{
68 blend_test<GPU_BLEND_ALPHA>(float4(1.0f, 0.0f, 1.0f, 1.0f),
69 float4(0.0f, 1.0f, 0.0f, 0.5f),
70 float4(0.5f, 0.5f, 0.5f, 1.0f));
71}
72GPU_TEST(blend_alpha)
73
75{
76 blend_test<GPU_BLEND_ALPHA_PREMULT>(float4(1.0f, 0.0f, 1.0f, 1.0f),
77 float4(0.0f, 1.0f, 0.0f, 0.5f),
78 float4(0.5f, 1.0f, 0.5f, 1.0f));
79}
80GPU_TEST(blend_alpha_premult)
81
83{
84 blend_test<GPU_BLEND_ADDITIVE>(float4(1.0f, 0.0f, 1.0f, 1.0f),
85 float4(0.0f, 1.0f, 0.0f, 0.5f),
86 float4(1.0f, 0.5f, 1.0f, 1.0f));
87}
88GPU_TEST(blend_additive)
89
91{
92 blend_test<GPU_BLEND_ADDITIVE_PREMULT>(float4(1.0f, 0.0f, 1.0f, 1.0f),
93 float4(0.0f, 1.0f, 0.0f, 0.5f),
94 float4(1.0f, 1.0f, 1.0f, 1.5f));
95}
96GPU_TEST(blend_additive_premult)
97
99{
100 blend_test<GPU_BLEND_MULTIPLY>(float4(1.0f, 0.0f, 1.0f, 1.0f),
101 float4(0.0f, 1.0f, 0.0f, 0.5f),
102 float4(0.0f, 0.0f, 0.0f, 0.5f));
103}
104GPU_TEST(blend_multiply)
105
107{
108 blend_test<GPU_BLEND_SUBTRACT>(float4(1.0f, 1.0f, 1.0f, 1.0f),
109 float4(0.0f, 1.0f, 0.0f, 0.5f),
110 float4(1.0f, 0.0f, 1.0f, 0.5f));
111}
112GPU_TEST(blend_subtract)
113
114static void test_blend_invert()
115{
116 blend_test<GPU_BLEND_INVERT>(float4(1.0f, 1.0f, 1.0f, 1.0f),
117 float4(0.0f, 1.0f, 0.0f, 0.5f),
118 float4(0.0f, 0.0f, 0.0f, 1.0f));
119}
120GPU_TEST(blend_invert)
121
122static void test_blend_oit()
123{
124 blend_test<GPU_BLEND_OIT>(float4(1.0f, 1.0f, 1.0f, 1.0f),
125 float4(0.0f, 1.0f, 0.0f, 0.5f),
126 float4(1.0f, 2.0f, 1.0f, 0.5f));
127}
128GPU_TEST(blend_oit)
129
131{
132 blend_test<GPU_BLEND_BACKGROUND>(float4(1.0f, 1.0f, 1.0f, 1.0f),
133 float4(0.0f, 1.0f, 0.0f, 0.5f),
134 float4(0.5f, 0.5f, 0.5f, 0.5f));
135}
136GPU_TEST(blend_background)
137
138} // namespace blender::gpu::tests
#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_program_set_builtin(blender::gpu::Batch *batch, GPUBuiltinShader shader_id)
void GPU_batch_draw(blender::gpu::Batch *batch)
#define GPU_batch_uniform_4fv(batch, name, val)
Definition GPU_batch.hh:279
blender::gpu::Batch * GPU_batch_preset_quad()
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_SHADER_3D_UNIFORM_COLOR
void GPU_flush()
Definition gpu_state.cc:305
@ GPU_BARRIER_TEXTURE_FETCH
Definition GPU_state.hh:37
@ GPU_BLEND_NONE
Definition GPU_state.hh:85
void GPU_blend(GPUBlend blend)
Definition gpu_state.cc:42
void GPU_memory_barrier(GPUBarrier barrier)
Definition gpu_state.cc:326
void GPU_texture_clear(blender::gpu::Texture *texture, eGPUDataFormat data_format, const void *data)
@ GPU_DATA_FLOAT
@ GPU_TEXTURE_USAGE_HOST_READ
@ GPU_TEXTURE_USAGE_ATTACHMENT
struct @021025263243242147216143265077100330027142264337::@225245033123204053237120173316075113304004012000 batch
#define GPU_TEST(test_name)
static void test_blend_none()
static void test_blend_alpha()
static void test_blend_background()
static void test_blend_alpha_premult()
void blend_test(float4 source_a, float4 source_b, float4 expected_result)
static void test_blend_multiply()
static void test_blend_invert()
static void test_blend_additive_premult()
static void test_blend_subtract()
static void test_blend_additive()
static void test_blend_oit()
VecBase< float, 4 > float4