Blender V4.3
compute_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_testing.hh"
6
7#include "MEM_guardedalloc.h"
8
10
11#include "GPU_capabilities.hh"
12#include "GPU_compute.hh"
13#include "GPU_storage_buffer.hh"
14#include "GPU_texture.hh"
15
16namespace blender::gpu::tests {
18{
19 static constexpr uint SIZE = 32;
20
21 /* Build compute shader. */
22 GPUShader *shader = GPU_shader_create_from_info_name("gpu_compute_2d_test");
23 EXPECT_NE(shader, nullptr);
24
25 /* Create texture to store result and attach to shader. */
26 GPUTexture *texture = GPU_texture_create_2d(
27 "gpu_shader_compute_2d", SIZE, SIZE, 1, GPU_RGBA32F, GPU_TEXTURE_USAGE_GENERAL, nullptr);
28 EXPECT_NE(texture, nullptr);
29
30 GPU_shader_bind(shader);
31 GPU_texture_image_bind(texture, GPU_shader_get_sampler_binding(shader, "img_output"));
32
33 /* Dispatch compute task. */
34 GPU_compute_dispatch(shader, SIZE, SIZE, 1);
35
36 /* Check if compute has been done. */
38 float4 *data = static_cast<float4 *>(GPU_texture_read(texture, GPU_DATA_FLOAT, 0));
39 const float4 expected_result(1.0f, 0.5f, 0.2f, 1.0f);
40 EXPECT_NE(data, nullptr);
41 for (int index = 0; index < SIZE * SIZE; index++) {
42 EXPECT_EQ(data[index], expected_result);
43 }
44 MEM_freeN(data);
45
46 /* Cleanup. */
48 GPU_texture_unbind(texture);
49 GPU_texture_free(texture);
50 GPU_shader_free(shader);
51}
52GPU_TEST(compute_direct)
53
55{
56 static constexpr uint SIZE = 32;
57
58 /* Build compute shader. */
59 GPUShader *shader = GPU_shader_create_from_info_name("gpu_compute_2d_test");
60 EXPECT_NE(shader, nullptr);
61
62 /* Create texture to store result and attach to shader. */
63 GPUTexture *texture = GPU_texture_create_2d(
64 "gpu_shader_compute_2d", SIZE, SIZE, 1, GPU_RGBA32F, GPU_TEXTURE_USAGE_GENERAL, nullptr);
65 EXPECT_NE(texture, nullptr);
67
68 GPU_shader_bind(shader);
69 GPU_texture_image_bind(texture, GPU_shader_get_sampler_binding(shader, "img_output"));
70
71 /* Generate compute tasks. */
72 uint4 commands[1] = {
73 {SIZE, SIZE, 1, 0},
74 };
75 GPUStorageBuf *compute_commands = GPU_storagebuf_create_ex(
76 sizeof(commands), &commands, GPU_USAGE_STATIC, __func__);
77
78 /* Dispatch compute task. */
79 GPU_compute_dispatch_indirect(shader, compute_commands);
80
81 /* Check if compute has been done. */
83 float4 *data = static_cast<float4 *>(GPU_texture_read(texture, GPU_DATA_FLOAT, 0));
84 const float4 expected_result(1.0f, 0.5f, 0.2f, 1.0f);
85 EXPECT_NE(data, nullptr);
86 for (int index = 0; index < SIZE * SIZE; index++) {
87 EXPECT_EQ(data[index], expected_result);
88 }
89 MEM_freeN(data);
90
91 /* Cleanup. */
92 GPU_storagebuf_free(compute_commands);
94 GPU_texture_unbind(texture);
95 GPU_texture_free(texture);
96 GPU_shader_free(shader);
97}
98GPU_TEST(compute_indirect);
99
100} // namespace blender::gpu::tests
EXPECT_EQ(BLI_expr_pylike_eval(expr, nullptr, 0, &result), EXPR_PYLIKE_INVALID)
unsigned int uint
void GPU_compute_dispatch(GPUShader *shader, uint groups_x_len, uint groups_y_len, uint groups_z_len)
int GPU_shader_get_sampler_binding(GPUShader *shader, const char *name)
GPUShader * GPU_shader_create_from_info_name(const char *info_name)
void GPU_shader_bind(GPUShader *shader)
void GPU_shader_free(GPUShader *shader)
void GPU_shader_unbind()
void GPU_memory_barrier(eGPUBarrier barrier)
Definition gpu_state.cc:374
@ GPU_BARRIER_TEXTURE_UPDATE
Definition GPU_state.hh:39
GPUStorageBuf * GPU_storagebuf_create_ex(size_t size, const void *data, GPUUsageType usage, const char *name)
void GPU_storagebuf_free(GPUStorageBuf *ssbo)
GPUTexture * GPU_texture_create_2d(const char *name, int width, int height, int mip_len, eGPUTextureFormat format, eGPUTextureUsage usage, const float *data)
void GPU_texture_free(GPUTexture *texture)
void GPU_texture_clear(GPUTexture *texture, eGPUDataFormat data_format, const void *data)
void * GPU_texture_read(GPUTexture *texture, eGPUDataFormat data_format, int mip_level)
void GPU_texture_unbind(GPUTexture *texture)
@ GPU_DATA_FLOAT
@ GPU_TEXTURE_USAGE_GENERAL
void GPU_texture_image_bind(GPUTexture *texture, int unit)
@ GPU_USAGE_STATIC
Read Guarded memory(de)allocation.
struct GPUShader GPUShader
additional_info("compositor_sum_float_shared") .push_constant(Type additional_info("compositor_sum_float_shared") .push_constant(Type GPU_RGBA32F
void GPU_compute_dispatch_indirect(GPUShader *shader, GPUStorageBuf *indirect_buf_)
#define GPU_TEST(test_name)
void MEM_freeN(void *vmemh)
Definition mallocn.cc:105
static void test_compute_indirect()
static void test_compute_direct()
VecBase< float, 4 > float4