Blender V5.3
framebuffer_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_batch.hh"
8#include "GPU_capabilities.hh"
9#include "GPU_context.hh"
10#include "GPU_framebuffer.hh"
11#include "GPU_shader.hh"
12#include "GPU_state.hh"
13#include "GPU_vertex_buffer.hh"
14#include "GPU_vertex_format.hh"
15
16#include "gpu_testing.hh"
17
18#include "BLI_math_vector.hh"
19
21
22namespace blender::gpu::tests {
23
25{
26 const int2 size(1, 1);
29 __func__, UNPACK2(size), 1, TextureFormat::SFLOAT_32_32_32_32, usage, nullptr);
30
31 gpu::FrameBuffer *framebuffer = GPU_framebuffer_create(__func__);
34 GPU_framebuffer_bind(framebuffer);
35
36 const float4 clear_color(0.1f, 0.2f, 0.5f, 1.0f);
37 GPU_framebuffer_clear_color(framebuffer, double4(clear_color));
38 GPU_finish();
39
40 float4 *read_data = static_cast<float4 *>(GPU_texture_read(texture, GPU_DATA_FLOAT, 0));
41 for (float4 pixel_color : Span<float4>(read_data, size.x * size.y)) {
42 EXPECT_EQ(clear_color, pixel_color);
43 }
44 MEM_delete(read_data);
45
46 GPU_framebuffer_free(framebuffer);
48}
49GPU_TEST(framebuffer_clear_color_single_attachment);
50
52{
53 const int2 size(1, 1);
56 __func__, UNPACK2(size), 1, TextureFormat::SFLOAT_32_32_32_32, usage, nullptr);
58 __func__, UNPACK2(size), 1, TextureFormat::UINT_32_32_32_32, usage, nullptr);
59
60 gpu::FrameBuffer *framebuffer = GPU_framebuffer_create(__func__);
62 &framebuffer,
64 GPU_framebuffer_bind(framebuffer);
65
66 const float4 clear_color(0.1f, 0.2f, 0.5f, 1.0f);
67 GPU_framebuffer_clear_color(framebuffer, double4(clear_color));
68 GPU_finish();
69
70 float4 *read_data1 = static_cast<float4 *>(GPU_texture_read(texture1, GPU_DATA_FLOAT, 0));
71 for (float4 pixel_color : Span<float4>(read_data1, size.x * size.y)) {
72 EXPECT_EQ(clear_color, pixel_color);
73 }
74 MEM_delete(read_data1);
75
76 uint4 *read_data2 = static_cast<uint4 *>(GPU_texture_read(texture2, GPU_DATA_UINT, 0));
77 uint4 clear_color_uint(0, 0, 0, 1);
78 for (uint4 pixel_color : Span<uint4>(read_data2, size.x * size.y)) {
79 EXPECT_EQ(clear_color_uint, pixel_color);
80 }
81 MEM_delete(read_data2);
82
83 GPU_framebuffer_free(framebuffer);
84 GPU_texture_free(texture1);
85 GPU_texture_free(texture2);
86}
87GPU_TEST(framebuffer_clear_color_multiple_attachments);
88
90{
91 const int2 size(1, 1);
94 __func__, UNPACK2(size), 1, TextureFormat::SFLOAT_32_32_32_32, usage, nullptr);
96 __func__, UNPACK2(size), 1, TextureFormat::SFLOAT_32_32_32_32, usage, nullptr);
97
98 gpu::FrameBuffer *framebuffer = GPU_framebuffer_create(__func__);
100 &framebuffer,
102 GPU_framebuffer_bind(framebuffer);
103
104 const std::array<double4, 2> clear_color = {double4(0.1f, 0.2f, 0.5f, 1.0f),
105 double4(0.5f, 0.2f, 0.1f, 1.0f)};
106 GPU_framebuffer_multi_clear(framebuffer, clear_color);
107 GPU_finish();
108
109 float4 *read_data1 = static_cast<float4 *>(GPU_texture_read(texture1, GPU_DATA_FLOAT, 0));
110 for (float4 pixel_color : Span<float4>(read_data1, size.x * size.y)) {
111 EXPECT_EQ(float4(clear_color[0]), pixel_color);
112 }
113 MEM_delete(read_data1);
114
115 float4 *read_data2 = static_cast<float4 *>(GPU_texture_read(texture2, GPU_DATA_FLOAT, 0));
116 for (float4 pixel_color : Span<float4>(read_data2, size.x * size.y)) {
117 EXPECT_EQ(float4(clear_color[1]), pixel_color);
118 }
119 MEM_delete(read_data2);
120
121 GPU_framebuffer_free(framebuffer);
122 GPU_texture_free(texture1);
123 GPU_texture_free(texture2);
124}
125GPU_TEST(framebuffer_clear_multiple_color_multiple_attachments);
126
128{
129 const int2 size(1, 1);
132 __func__, UNPACK2(size), 1, TextureFormat::SFLOAT_32_DEPTH, usage, nullptr);
133
134 gpu::FrameBuffer *framebuffer = GPU_framebuffer_create(__func__);
136 GPU_framebuffer_bind(framebuffer);
137
138 const float clear_depth = 0.5f;
139 GPU_framebuffer_clear_depth(framebuffer, clear_depth);
140 GPU_finish();
141
142 float *read_data = static_cast<float *>(GPU_texture_read(texture, GPU_DATA_FLOAT, 0));
143 for (float pixel_depth : Span<float>(read_data, size.x * size.y)) {
144 EXPECT_EQ(clear_depth, pixel_depth);
145 }
146 MEM_delete(read_data);
147
148 GPU_framebuffer_free(framebuffer);
150}
151GPU_TEST(framebuffer_clear_depth);
152
153#ifndef __APPLE__ /* Clearing with scissors is not supported on Metal. */
154
156{
157 const int2 size(2, 2);
160 __func__, UNPACK2(size), 1, TextureFormat::SFLOAT_32_32_32_32, usage, nullptr);
161
162 gpu::FrameBuffer *framebuffer = GPU_framebuffer_create(__func__);
165 GPU_framebuffer_bind(framebuffer);
166
167 const float4 color1(0.0f);
168 const float4 color2(0.5f);
169 const float4 color3(1.0f);
170 GPU_framebuffer_clear_color(framebuffer, double4(color1));
171
172 GPU_scissor_test(true);
173 GPU_scissor(0, 0, 1, 2);
174 GPU_framebuffer_clear_color(framebuffer, double4(color2));
175
176 GPU_scissor(0, 0, 2, 1);
177 GPU_framebuffer_clear_color(framebuffer, double4(color3));
178 GPU_scissor_test(false);
179 GPU_finish();
180
181 float4 *read_data = static_cast<float4 *>(GPU_texture_read(texture, GPU_DATA_FLOAT, 0));
182 EXPECT_EQ(color3, read_data[0]);
183 EXPECT_EQ(color3, read_data[1]);
184 EXPECT_EQ(color2, read_data[2]);
185 EXPECT_EQ(color1, read_data[3]);
186 MEM_delete(read_data);
187
188 GPU_framebuffer_free(framebuffer);
190}
191GPU_TEST(framebuffer_scissor_test);
192
193#endif
194
195/* Color each side of a cube-map with a different color. */
197{
198 const int SIZE = 32;
200
203 "tex", SIZE, 1, TextureFormat::SFLOAT_32_32_32_32, usage, nullptr);
204
205 const double4 clear_colors[6] = {
206 {0.5f, 0.0f, 0.0f, 1.0f},
207 {1.0f, 0.0f, 0.0f, 1.0f},
208 {0.0f, 0.5f, 0.0f, 1.0f},
209 {0.0f, 1.0f, 0.0f, 1.0f},
210 {0.0f, 0.0f, 0.5f, 1.0f},
211 {0.0f, 0.0f, 1.0f, 1.0f},
212 };
213 gpu::FrameBuffer *framebuffers[6] = {nullptr};
214
215 for (int i : IndexRange(6)) {
216 GPU_framebuffer_ensure_config(&framebuffers[i],
217 {
220 });
221 GPU_framebuffer_bind(framebuffers[i]);
222 GPU_framebuffer_clear_color(framebuffers[i], clear_colors[i]);
223 };
224
225 float4 *data = static_cast<float4 *>(GPU_texture_read(tex, GPU_DATA_FLOAT, 0));
226 for (int side : IndexRange(6)) {
227 for (int pixel_index : IndexRange(SIZE * SIZE)) {
228 int index = pixel_index + (SIZE * SIZE) * side;
229 EXPECT_EQ(float4(clear_colors[side]), data[index]);
230 }
231 }
232 MEM_delete(data);
233
234 GPU_texture_free(tex);
235
236 for (int i : IndexRange(6)) {
237 GPU_FRAMEBUFFER_FREE_SAFE(framebuffers[i]);
238 }
239
241}
242GPU_TEST(framebuffer_cube)
243
244/* Effectively tests the same way EEVEE-Next shadows are rendered. */
246{
247 using namespace gpu::shader;
251 {
252 GTEST_SKIP() << "NVIDIA fails to compile workaround due to reserved names. Gladly it doesn't "
253 "need the workaround.";
254 }
255
257 GTEST_SKIP() << "Device does not support multi viewport, but the test requires it.";
258 }
259
261
262 const int2 size(4, 4);
263 const int layers = 256;
266 __func__, UNPACK2(size), layers, 1, TextureFormat::SINT_32_32, usage, nullptr);
267
268 gpu::FrameBuffer *framebuffer = GPU_framebuffer_create(__func__);
271 GPU_framebuffer_bind(framebuffer);
272
273 int viewport_rects[16][4];
274 for (int i = 0; i < 16; i++) {
275 viewport_rects[i][0] = i % 4;
276 viewport_rects[i][1] = i / 4;
277 viewport_rects[i][2] = 1;
278 viewport_rects[i][3] = 1;
279 }
280 GPU_framebuffer_multi_viewports_set(framebuffer, viewport_rects);
281
282 GPU_framebuffer_clear_color(framebuffer, double4(0.0));
283
284 ShaderCreateInfo create_info("gpu_framebuffer_layer_viewport_test");
285 create_info.vertex_source("gpu_framebuffer_layer_viewport_test.glsl");
286 create_info.fragment_source("gpu_framebuffer_layer_viewport_test.glsl");
287 create_info.builtins(BuiltinBits::VIEWPORT_INDEX | BuiltinBits::LAYER | BuiltinBits::VERTEX_ID);
288 create_info.fragment_out(0, Type::int2_t, "out_value");
289
291 reinterpret_cast<GPUShaderCreateInfo *>(&create_info));
292
293 int tri_count = size.x * size.y * layers;
294
295 Batch *batch = GPU_batch_create_procedural(GPU_PRIM_TRIS, tri_count * 3);
296
298
299 GPU_batch_draw(batch);
300
301 GPU_batch_discard(batch);
302
303 GPU_finish();
304
305 int2 *read_data = static_cast<int2 *>(GPU_texture_read(texture, GPU_DATA_INT, 0));
306 for (auto layer : IndexRange(layers)) {
307 for (auto viewport : IndexRange(16)) {
308 int2 expected_color(layer, viewport);
309 int2 pixel_color = read_data[viewport + layer * 16];
310 EXPECT_EQ(pixel_color, expected_color);
311 }
312 }
313 MEM_delete(read_data);
314
316
317 GPU_framebuffer_free(framebuffer);
320
322}
323GPU_TEST(framebuffer_multi_viewport)
324
325
330{
331 using namespace gpu::shader;
332
334
335 const int2 size(1, 1);
338 __func__, UNPACK2(size), 1, TextureFormat::SINT_32, usage, nullptr);
340 __func__, UNPACK2(size), 1, TextureFormat::SINT_32, usage, nullptr);
341
342 gpu::FrameBuffer *framebuffer = GPU_framebuffer_create(__func__);
344 &framebuffer,
346 GPU_framebuffer_bind(framebuffer);
347
348 GPU_framebuffer_clear_color(framebuffer, {0.0, 0.0, 0.0, 0.0});
349
350 ShaderCreateInfo create_info_write("gpu_framebuffer_subpass_input_test");
351 create_info_write.define("WRITE");
352 create_info_write.builtins(BuiltinBits::VERTEX_ID);
353 create_info_write.vertex_source("gpu_framebuffer_subpass_input_test.glsl");
354 create_info_write.fragment_source("gpu_framebuffer_subpass_input_test.glsl");
355 create_info_write.fragment_out(0, Type::int_t, "out_value", DualBlend::NONE, 0);
356
358 reinterpret_cast<GPUShaderCreateInfo *>(&create_info_write));
359
360 ShaderCreateInfo create_info_read("gpu_framebuffer_subpass_input_test");
361 create_info_read.define("READ");
362 create_info_read.builtins(BuiltinBits::VERTEX_ID);
363 create_info_read.vertex_source("gpu_framebuffer_subpass_input_test.glsl");
364 create_info_read.fragment_source("gpu_framebuffer_subpass_input_test.glsl");
365 create_info_read.subpass_in(0, Type::int_t, ImageType::Int2D, "in_value", 0);
366 create_info_read.fragment_out(1, Type::int_t, "out_value");
367
369 reinterpret_cast<GPUShaderCreateInfo *>(&create_info_read));
370
372
373 /* Metal Raster Order Group does not need that. */
376
377 GPU_batch_set_shader(batch, shader_write);
378 GPU_batch_draw(batch);
379
380 /* Metal Raster Order Group does not need that. */
383
384 GPU_batch_set_shader(batch, shader_read);
385 GPU_batch_draw(batch);
386
387 GPU_batch_discard(batch);
388
389 GPU_finish();
390
391 int *read_data_a = static_cast<int *>(GPU_texture_read(texture_a, GPU_DATA_INT, 0));
392 EXPECT_EQ(*read_data_a, 0xDEADBEEF);
393 MEM_delete(read_data_a);
394
395 int *read_data_b = static_cast<int *>(GPU_texture_read(texture_b, GPU_DATA_INT, 0));
396 EXPECT_EQ(*read_data_b, 0xDEADC0DE);
397 MEM_delete(read_data_b);
398
400
401 GPU_framebuffer_free(framebuffer);
402 GPU_texture_free(texture_a);
403 GPU_texture_free(texture_b);
404 GPU_shader_free(shader_write);
405 GPU_shader_free(shader_read);
406
408}
409GPU_TEST(framebuffer_subpass_input)
410
412{
413 using namespace gpu::shader;
414
416
417 const int2 size(1, 1);
420 __func__, UNPACK2(size), 1, TextureFormat::SINT_32, usage, nullptr);
422 __func__, UNPACK2(size), 1, TextureFormat::SINT_32, usage, nullptr);
423
424 const int invalid_data = 0xDEADBEEF;
425 texture_a->clear(double4(invalid_data));
426
427 gpu::FrameBuffer *framebuffer = GPU_framebuffer_create(__func__);
429 &framebuffer,
432 framebuffer,
433 {
435 {GPU_LOADACTION_CLEAR, GPU_STOREACTION_STORE, {0.0f, 0.0f, 0.0f, 0.0f}},
436 {GPU_LOADACTION_CLEAR, GPU_STOREACTION_STORE, {0.0f, 0.0f, 0.0f, 0.0f}},
437 });
438
439 ShaderCreateInfo create_info_read("gpu_framebuffer_subpass_input_test");
440 create_info_read.define("READ");
441 create_info_read.builtins(BuiltinBits::VERTEX_ID);
442 create_info_read.vertex_source("gpu_framebuffer_subpass_input_test.glsl");
443 create_info_read.fragment_source("gpu_framebuffer_subpass_input_test.glsl");
444 create_info_read.subpass_in(0, Type::int_t, ImageType::Int2D, "in_value", 0);
445 create_info_read.fragment_out(1, Type::int_t, "out_value");
446
448 reinterpret_cast<GPUShaderCreateInfo *>(&create_info_read));
449
451
452 /* Metal Raster Order Group does not need that. */
455
456 /* Metal Raster Order Group does not need that. */
459
460 GPU_batch_set_shader(batch, shader_read);
461 GPU_batch_draw(batch);
462
463 GPU_batch_discard(batch);
464
465 GPU_finish();
466
467 int *read_data_a = static_cast<int *>(GPU_texture_read(texture_a, GPU_DATA_INT, 0));
468 EXPECT_EQ(*read_data_a, 0x0);
469 MEM_delete(read_data_a);
470
471 int *read_data_b = static_cast<int *>(GPU_texture_read(texture_b, GPU_DATA_INT, 0));
472 EXPECT_EQ(*read_data_b, 495);
473 MEM_delete(read_data_b);
474
476
477 GPU_framebuffer_free(framebuffer);
478 GPU_texture_free(texture_a);
479 GPU_texture_free(texture_b);
480 GPU_shader_free(shader_read);
481
483}
484GPU_TEST(framebuffer_subpass_input_clearops)
485
486} // namespace blender::gpu::tests
#define GPU_FRAMEBUFFER_FREE_SAFE(fb)
#define GPU_ATTACHMENT_TEXTURE(_texture)
#define GPU_ATTACHMENT_TEXTURE_CUBEFACE(_texture, _face)
#define GPU_ATTACHMENT_NONE
#define GPU_framebuffer_bind_ex(_fb,...)
#define GPU_framebuffer_ensure_config(_fb,...)
#define GPU_framebuffer_subpass_transition(_fb,...)
#define UNPACK2(a)
constexpr IndexRange()=default
virtual void clear(const double4 data)=0
#define layer
TEX_TEMPLATE DataVec texture(T, FltCoord, float=0.0f) RET
#define GPU_TEST(test_name)
static void test_framebuffer_clear_depth()
static void test_framebuffer_subpass_input_clearops()
static void test_framebuffer_clear_color_single_attachment()
static void test_framebuffer_clear_multiple_color_multiple_attachments()
static void test_framebuffer_cube()
static void test_framebuffer_subpass_input()
static void test_framebuffer_scissor_test()
static void test_framebuffer_multi_viewport()
static void test_framebuffer_clear_color_multiple_attachments()
GPU_TEST(compute_indirect)
void GPU_framebuffer_bind(gpu::FrameBuffer *fb)
void GPU_scissor_test(bool enable)
Definition gpu_state.cc:173
gpu::Batch * GPU_batch_create_procedural(GPUPrimType primitive_type, int32_t vertex_count)
Definition gpu_batch.cc:85
bool GPU_type_matches_ex(GPUDeviceType device, GPUOSType os, GPUDriverType driver, GPUBackendType backend)
void GPU_framebuffer_free(gpu::FrameBuffer *fb)
@ GPU_LOADACTION_DONT_CARE
VecBase< uint32_t, 4 > uint4
gpu::Shader * GPU_shader_create_from_info(const GPUShaderCreateInfo *_info)
void GPU_framebuffer_clear_color(gpu::FrameBuffer *fb, const double4 clear_col)
gpu::Texture * GPU_texture_create_2d_array(const char *name, int width, int height, int layer_len, int mip_len, gpu::TextureFormat format, eGPUTextureUsage usage, const float *data)
void GPU_batch_discard(gpu::Batch *batch)
void GPU_render_begin()
void GPU_batch_set_shader(gpu::Batch *batch, gpu::Shader *shader, const gpu::shader::SpecializationConstants *constants_state=nullptr)
@ GPU_DRIVER_OFFICIAL
@ GPU_STOREACTION_DONT_CARE
@ G_DEBUG_GPU_FORCE_WORKAROUNDS
Global G
Definition blender.cc:57
@ GPU_TEXTURE_USAGE_HOST_READ
@ GPU_TEXTURE_USAGE_ATTACHMENT
void GPU_render_end()
BMesh const char void * data
void GPU_shader_unbind()
void GPU_framebuffer_multi_viewports_set(gpu::FrameBuffer *gpu_fb, const int viewport_rects[GPU_MAX_VIEWPORTS][4])
void GPU_batch_draw(gpu::Batch *batch)
gpu::FrameBuffer * GPU_framebuffer_create(const char *name)
void GPU_framebuffer_clear_depth(gpu::FrameBuffer *fb, float clear_depth)
gpu::Texture * GPU_texture_create_cube(const char *name, int width, int mip_len, gpu::TextureFormat format, eGPUTextureUsage usage, const float *data)
void GPU_framebuffer_multi_clear(gpu::FrameBuffer *fb, Span< double4 > clear_colors)
EXPECT_EQ(BLI_expr_pylike_eval(expr, nullptr, 0, &result), EXPR_PYLIKE_INVALID)
void GPU_scissor(int x, int y, int width, int height)
Definition gpu_state.cc:178
void GPU_texture_free(gpu::Texture *texture)
bool GPU_multi_viewport_support()
void GPU_shader_free(gpu::Shader *shader)
void * GPU_texture_read(gpu::Texture *texture, eGPUDataFormat data_format, int mip_level)
void GPU_finish()
Definition gpu_state.cc:289
@ GPU_DEVICE_NVIDIA
VecBase< double, 4 > double4
gpu::Texture * GPU_texture_create_2d(const char *name, int width, int height, int mip_len, gpu::TextureFormat format, eGPUTextureUsage usage, const float *data)
Describe inputs & outputs, stage interfaces, resources and sources of a shader. If all data is correc...
Self & fragment_source(StringRefNull filename)
Self & fragment_out(int slot, Type type, StringRefNull name, DualBlend blend=DualBlend::NONE, int raster_order_group=-1, ConditionFn cond=nullptr)
Self & builtins(BuiltinBits builtin, ConditionFn cond=nullptr)
Self & subpass_in(int slot, Type type, ImageType img_type, StringRefNull name, int raster_order_group=-1)
Self & define(StringRefNull name, StringRefNull value="")