Blender V5.0
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 gpu::Shader *shader = nullptr;
27 StorageBuf *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) {
99
101
102 GPU_batch_set_shader(batch, shader, &constants);
103 GPU_batch_draw_advanced(batch, 0, 1, 0, 1);
105
107 }
108 else {
109 GPU_compute_dispatch(shader, 1, 1, 1, &constants);
110 }
111
112 GPU_finish();
115
116 EXPECT_EQ(data[0], int(float_in));
117 EXPECT_EQ(data[1], int(uint_in));
118 EXPECT_EQ(data[2], int(int_in));
119 EXPECT_EQ(data[3], int(bool_in));
120 }
121};
122
124{
125 ShaderSpecializationConst("gpu_compute_specialization_test");
126}
127GPU_TEST(specialization_constants_compute)
128
130{
131 ShaderSpecializationConst("gpu_graphic_specialization_test");
132}
133GPU_TEST(specialization_constants_graphic)
134
135} // namespace blender::gpu::tests
EXPECT_EQ(BLI_expr_pylike_eval(expr, nullptr, 0, &result), EXPR_PYLIKE_INVALID)
unsigned int uint
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)
blender::gpu::Batch * GPU_batch_create_procedural(GPUPrimType primitive_type, int32_t vertex_count)
Definition gpu_batch.cc:83
void GPU_batch_set_shader(blender::gpu::Batch *batch, blender::gpu::Shader *shader, const blender::gpu::shader::SpecializationConstants *constants_state=nullptr)
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_render_end()
void GPU_render_begin()
blender::gpu::FrameBuffer * GPU_framebuffer_create(const char *name)
void GPU_framebuffer_default_size(blender::gpu::FrameBuffer *fb, int width, int height)
void GPU_framebuffer_free(blender::gpu::FrameBuffer *fb)
void GPU_framebuffer_bind(blender::gpu::FrameBuffer *fb)
@ GPU_PRIM_POINTS
int GPU_shader_get_constant(blender::gpu::Shader *shader, const char *name)
const blender::gpu::shader::SpecializationConstants & GPU_shader_get_default_constant_state(blender::gpu::Shader *sh)
void GPU_shader_free(blender::gpu::Shader *shader)
int GPU_shader_get_ssbo_binding(blender::gpu::Shader *shader, const char *name)
blender::gpu::Shader * GPU_shader_create_from_info_name(const char *info_name)
void GPU_shader_unbind()
@ GPU_BARRIER_BUFFER_UPDATE
Definition GPU_state.hh:56
void GPU_finish()
Definition gpu_state.cc:310
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_storagebuf_bind(blender::gpu::StorageBuf *ssbo, int slot)
void GPU_storagebuf_read(blender::gpu::StorageBuf *ssbo, void *data)
@ GPU_USAGE_STREAM
constexpr bool is_empty() const
struct @021025263243242147216143265077100330027142264337::@225245033123204053237120173316075113304004012000 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)
Describe inputs & outputs, stage interfaces, resources and sources of a shader. If all data is correc...
void validate(shader::SpecializationConstants &constants)