Blender V4.5
specialization_constants_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_compute.hh"
10#include "GPU_context.hh"
11#include "GPU_framebuffer.hh"
12#include "GPU_shader.hh"
13#include "GPU_state.hh"
14#include "GPU_storage_buffer.hh"
15#include "GPU_vertex_format.hh"
16
17#include "BLI_vector.hh"
18
21#include "gpu_testing.hh"
22
23namespace blender::gpu::tests {
24
26 GPUShader *shader = nullptr;
27 GPUStorageBuf *ssbo = nullptr;
29
30 float float_in;
32 int int_in;
33 bool bool_in;
34
35 bool is_graphic = false;
36
37 ShaderSpecializationConst(const char *info_name)
38 {
40
41 this->init_shader(info_name);
42
44
45 /* Test values. */
46 float_in = 52;
47 uint_in = 324;
48 int_in = 455;
49 bool_in = false;
50
51 int float_in_loc = GPU_shader_get_constant(shader, "float_in");
52 int uint_in_loc = GPU_shader_get_constant(shader, "uint_in");
53 int int_in_loc = GPU_shader_get_constant(shader, "int_in");
54 int bool_in_loc = GPU_shader_get_constant(shader, "bool_in");
55
57 constants.set_value(float_in_loc, float_in);
58 constants.set_value(uint_in_loc, uint_in);
59 constants.set_value(int_in_loc, int_in);
60 constants.set_value(bool_in_loc, bool_in);
61
62 this->validate(constants);
63
65 }
66
68 {
69 if (shader != nullptr) {
72 }
73 if (ssbo != nullptr) {
75 }
76 }
77
78 void init_shader(const char *info_name)
79 {
80 using namespace blender::gpu::shader;
81
82 uint data_len = 4;
83 ssbo = GPU_storagebuf_create_ex(data_len * sizeof(int), nullptr, GPU_USAGE_STREAM, __func__);
84 data.resize(data_len);
85
86 const GPUShaderCreateInfo *_info = gpu_shader_create_info_get(info_name);
87 const ShaderCreateInfo &info = *reinterpret_cast<const ShaderCreateInfo *>(_info);
90 EXPECT_NE(shader, nullptr);
91 }
92
94 {
95 if (is_graphic) {
96 GPUFrameBuffer *fb = GPU_framebuffer_create("test_fb");
99
100 /* TODO(fclem): remove this boilerplate. */
104
107 GPU_batch_set_shader(batch, shader, &constants);
108 GPU_batch_draw_advanced(batch, 0, 1, 0, 1);
110
112 }
113 else {
114 GPU_compute_dispatch(shader, 1, 1, 1, &constants);
115 }
116
117 GPU_finish();
120
121 EXPECT_EQ(data[0], int(float_in));
122 EXPECT_EQ(data[1], int(uint_in));
123 EXPECT_EQ(data[2], int(int_in));
124 EXPECT_EQ(data[3], int(bool_in));
125 }
126};
127
129{
130 ShaderSpecializationConst("gpu_compute_specialization_test");
131}
132GPU_TEST(specialization_constants_compute)
133
135{
136 ShaderSpecializationConst("gpu_graphic_specialization_test");
137}
138GPU_TEST(specialization_constants_graphic)
139
140} // namespace blender::gpu::tests
EXPECT_EQ(BLI_expr_pylike_eval(expr, nullptr, 0, &result), EXPR_PYLIKE_INVALID)
unsigned int uint
blender::gpu::Batch * GPU_batch_create_ex(GPUPrimType primitive_type, blender::gpu::VertBuf *vertex_buf, blender::gpu::IndexBuf *index_buf, eGPUBatchFlag owns_flag)
Definition gpu_batch.cc:51
void GPU_batch_discard(blender::gpu::Batch *batch)
void GPU_batch_draw_advanced(blender::gpu::Batch *batch, int vertex_first, int vertex_count, int instance_first, int instance_count)
void GPU_batch_set_shader(blender::gpu::Batch *batch, GPUShader *shader, const blender::gpu::shader::SpecializationConstants *constants_state=nullptr)
@ GPU_BATCH_OWNS_VBO
Definition GPU_batch.hh:41
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)
void GPU_render_end()
void GPU_render_begin()
GPUFrameBuffer * GPU_framebuffer_create(const char *name)
void GPU_framebuffer_free(GPUFrameBuffer *fb)
void GPU_framebuffer_bind(GPUFrameBuffer *fb)
void GPU_framebuffer_default_size(GPUFrameBuffer *fb, int width, int height)
@ GPU_PRIM_POINTS
GPUShader * GPU_shader_create_from_info_name(const char *info_name)
int GPU_shader_get_ssbo_binding(GPUShader *shader, const char *name)
int GPU_shader_get_constant(GPUShader *shader, const char *name)
void GPU_shader_free(GPUShader *shader)
void GPU_shader_unbind()
const blender::gpu::shader::SpecializationConstants & GPU_shader_get_default_constant_state(GPUShader *sh)
void GPU_memory_barrier(eGPUBarrier barrier)
Definition gpu_state.cc:385
void GPU_finish()
Definition gpu_state.cc:310
@ GPU_BARRIER_BUFFER_UPDATE
Definition GPU_state.hh:56
void GPU_storagebuf_bind(GPUStorageBuf *ssbo, int slot)
GPUStorageBuf * GPU_storagebuf_create_ex(size_t size, const void *data, GPUUsageType usage, const char *name)
void GPU_storagebuf_free(GPUStorageBuf *ssbo)
void GPU_storagebuf_read(GPUStorageBuf *ssbo, void *data)
#define GPU_vertbuf_create_with_format(format)
void GPU_vertbuf_data_alloc(blender::gpu::VertBuf &verts, uint v_len)
@ GPU_USAGE_STREAM
@ GPU_FETCH_INT
uint GPU_vertformat_attr_add(GPUVertFormat *, blender::StringRef name, GPUVertCompType, uint comp_len, GPUVertFetchMode)
@ GPU_COMP_U32
constexpr bool is_empty() const
static float verts[][3]
struct @242053044010324116347033273112253060004051364061::@051143074301336237271216303350234260141112266062 batch
const GPUShaderCreateInfo * gpu_shader_create_info_get(const char *info_name)
#define GPU_TEST(test_name)
BLI_INLINE float fb(float length, float L)
format
Describe inputs & outputs, stage interfaces, resources and sources of a shader. If all data is correc...
void validate(shader::SpecializationConstants &constants)