Blender V4.5
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 GPUShader *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 GPUTexture *texture = GPU_texture_create_2d(
28 "gpu_shader_compute_2d", SIZE, SIZE, 1, GPU_RGBA32F, GPU_TEXTURE_USAGE_GENERAL, nullptr);
29 EXPECT_NE(texture, nullptr);
30
33
34 /* Dispatch compute task. */
36
37 /* Check if compute has been done. */
40 const float4 expected_result(1.0f, 0.5f, 0.2f, 1.0f);
41 EXPECT_NE(data, nullptr);
42 for (int index = 0; index < SIZE * SIZE; index++) {
43 EXPECT_EQ(data[index], expected_result);
44 }
46
47 /* Cleanup. */
52}
53GPU_TEST(compute_direct)
54
56{
57 static constexpr uint SIZE = 32;
58
59 /* Build compute shader. */
60 GPUShader *shader = GPU_shader_create_from_info_name("gpu_compute_2d_test");
61 EXPECT_NE(shader, nullptr);
62
63 /* Create texture to store result and attach to shader. */
64 GPUTexture *texture = GPU_texture_create_2d(
65 "gpu_shader_compute_2d", SIZE, SIZE, 1, GPU_RGBA32F, GPU_TEXTURE_USAGE_GENERAL, nullptr);
66 EXPECT_NE(texture, nullptr);
68
71
72 /* Generate compute tasks. */
73 uint4 commands[1] = {
74 {SIZE, SIZE, 1, 0},
75 };
76 GPUStorageBuf *compute_commands = GPU_storagebuf_create_ex(
77 sizeof(commands), &commands, GPU_USAGE_STATIC, __func__);
78
79 /* Dispatch compute task. */
80 GPU_compute_dispatch_indirect(shader, compute_commands);
81
82 /* Check if compute has been done. */
85 const float4 expected_result(1.0f, 0.5f, 0.2f, 1.0f);
86 EXPECT_NE(data, nullptr);
87 for (int index = 0; index < SIZE * SIZE; index++) {
88 EXPECT_EQ(data[index], expected_result);
89 }
91
92 /* Cleanup. */
93 GPU_storagebuf_free(compute_commands);
98}
99GPU_TEST(compute_indirect);
100
101} // 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, const blender::gpu::shader::SpecializationConstants *constants_state=nullptr)
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, const blender::gpu::shader::SpecializationConstants *constants_state=nullptr)
void GPU_shader_free(GPUShader *shader)
void GPU_shader_unbind()
void GPU_memory_barrier(eGPUBarrier barrier)
Definition gpu_state.cc:385
@ 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_RGBA32F
@ GPU_USAGE_STATIC
Read Guarded memory(de)allocation.
BMesh const char void * data
void GPU_compute_dispatch_indirect(GPUShader *shader, GPUStorageBuf *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