Blender V5.0
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_state.hh"
14#include "GPU_storage_buffer.hh"
15#include "GPU_texture.hh"
16
17namespace blender::gpu::tests {
19{
20 static constexpr uint SIZE = 32;
21
22 /* Build compute shader. */
23 gpu::Shader *shader = GPU_shader_create_from_info_name("gpu_compute_2d_test");
24 EXPECT_NE(shader, nullptr);
25
26 /* Create texture to store result and attach to shader. */
27 blender::gpu::Texture *texture = GPU_texture_create_2d("gpu_shader_compute_2d",
28 SIZE,
29 SIZE,
30 1,
31 TextureFormat::SFLOAT_32_32_32_32,
33 nullptr);
34 EXPECT_NE(texture, nullptr);
35
38
39 /* Dispatch compute task. */
41
42 /* Check if compute has been done. */
45 const float4 expected_result(1.0f, 0.5f, 0.2f, 1.0f);
46 EXPECT_NE(data, nullptr);
47 for (int index = 0; index < SIZE * SIZE; index++) {
48 EXPECT_EQ(data[index], expected_result);
49 }
51
52 /* Cleanup. */
57}
58GPU_TEST(compute_direct)
59
61{
62 static constexpr uint SIZE = 32;
63
64 /* Build compute shader. */
65 gpu::Shader *shader = GPU_shader_create_from_info_name("gpu_compute_2d_test");
66 EXPECT_NE(shader, nullptr);
67
68 /* Create texture to store result and attach to shader. */
69 blender::gpu::Texture *texture = GPU_texture_create_2d("gpu_shader_compute_2d",
70 SIZE,
71 SIZE,
72 1,
73 TextureFormat::SFLOAT_32_32_32_32,
75 nullptr);
76 EXPECT_NE(texture, nullptr);
78
81
82 /* Generate compute tasks. */
83 uint4 commands[1] = {
84 {SIZE, SIZE, 1, 0},
85 };
86 StorageBuf *compute_commands = GPU_storagebuf_create_ex(
87 sizeof(commands), &commands, GPU_USAGE_STATIC, __func__);
88
89 /* Dispatch compute task. */
90 GPU_compute_dispatch_indirect(shader, compute_commands);
91
92 /* Check if compute has been done. */
95 const float4 expected_result(1.0f, 0.5f, 0.2f, 1.0f);
96 EXPECT_NE(data, nullptr);
97 for (int index = 0; index < SIZE * SIZE; index++) {
98 EXPECT_EQ(data[index], expected_result);
99 }
101
102 /* Cleanup. */
103 GPU_storagebuf_free(compute_commands);
108}
109GPU_TEST(compute_indirect);
110
111} // namespace blender::gpu::tests
EXPECT_EQ(BLI_expr_pylike_eval(expr, nullptr, 0, &result), EXPR_PYLIKE_INVALID)
unsigned int uint
void GPU_compute_dispatch(blender::gpu::Shader *shader, uint groups_x_len, uint groups_y_len, uint groups_z_len, const blender::gpu::shader::SpecializationConstants *constants_state=nullptr)
void GPU_shader_free(blender::gpu::Shader *shader)
int GPU_shader_get_sampler_binding(blender::gpu::Shader *shader, const char *name)
void GPU_shader_bind(blender::gpu::Shader *shader, const blender::gpu::shader::SpecializationConstants *constants_state=nullptr)
blender::gpu::Shader * GPU_shader_create_from_info_name(const char *info_name)
void GPU_shader_unbind()
@ GPU_BARRIER_TEXTURE_UPDATE
Definition GPU_state.hh:39
void GPU_memory_barrier(GPUBarrier barrier)
Definition gpu_state.cc:326
void GPU_storagebuf_free(blender::gpu::StorageBuf *ssbo)
blender::gpu::StorageBuf * GPU_storagebuf_create_ex(size_t size, const void *data, GPUUsageType usage, const char *name)
void GPU_texture_clear(blender::gpu::Texture *texture, eGPUDataFormat data_format, const void *data)
void GPU_texture_unbind(blender::gpu::Texture *texture)
@ GPU_DATA_FLOAT
@ GPU_TEXTURE_USAGE_GENERAL
void * GPU_texture_read(blender::gpu::Texture *texture, eGPUDataFormat data_format, int mip_level)
void GPU_texture_image_bind(blender::gpu::Texture *texture, int unit)
blender::gpu::Texture * GPU_texture_create_2d(const char *name, int width, int height, int mip_len, blender::gpu::TextureFormat format, eGPUTextureUsage usage, const float *data)
void GPU_texture_free(blender::gpu::Texture *texture)
@ GPU_USAGE_STATIC
Read Guarded memory(de)allocation.
BMesh const char void * data
void GPU_compute_dispatch_indirect(blender::gpu::Shader *shader, blender::gpu::StorageBuf *indirect_buf_, const blender::gpu::shader::SpecializationConstants *constants_state)
TEX_TEMPLATE DataVec texture(T, FltCoord, float=0.0f) RET
#define GPU_TEST(test_name)
void MEM_freeN(void *vmemh)
Definition mallocn.cc:113
static void test_compute_indirect()
static void test_compute_direct()
VecBase< uint32_t, 4 > uint4
VecBase< float, 4 > float4