Blender V4.3
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_storage_buffer.hh"
14
15#include "BLI_math_vector.hh"
16#include "BLI_utility_mixins.hh"
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 /* Expect defaults. */
46 float_in = 2;
47 uint_in = 3;
48 int_in = 4;
49 bool_in = true;
50
51 this->validate();
52
53 /* Test values. */
54 float_in = 52;
55 uint_in = 324;
56 int_in = 455;
57 bool_in = false;
58
59 GPU_shader_constant_float(shader, "float_in", float_in);
60 GPU_shader_constant_uint(shader, "uint_in", uint_in);
61 GPU_shader_constant_int(shader, "int_in", int_in);
62 GPU_shader_constant_bool(shader, "bool_in", bool_in);
63
64 this->validate();
65
67 }
68
70 {
71 if (shader != nullptr) {
73 GPU_shader_free(shader);
74 }
75 if (ssbo != nullptr) {
77 }
78 }
79
80 void init_shader(const char *info_name)
81 {
82 using namespace blender::gpu::shader;
83
84 uint data_len = 4;
85 ssbo = GPU_storagebuf_create_ex(data_len * sizeof(int), nullptr, GPU_USAGE_STREAM, __func__);
86 data.resize(data_len);
87
88 const GPUShaderCreateInfo *_info = gpu_shader_create_info_get(info_name);
89 const ShaderCreateInfo &info = *reinterpret_cast<const ShaderCreateInfo *>(_info);
91 shader = GPU_shader_create_from_info_name(info_name);
92 EXPECT_NE(shader, nullptr);
93 }
94
95 void validate()
96 {
97 if (is_graphic) {
98 GPUFrameBuffer *fb = GPU_framebuffer_create("test_fb");
101
102 /* TODO(fclem): remove this boilerplate. */
106
110 GPU_batch_draw_advanced(batch, 0, 1, 0, 1);
112
114 }
115 else {
116 GPU_compute_dispatch(shader, 1, 1, 1);
117 }
118
119 GPU_finish();
121 GPU_storagebuf_read(ssbo, data.data());
122
123 EXPECT_EQ(data[0], int(float_in));
124 EXPECT_EQ(data[1], int(uint_in));
125 EXPECT_EQ(data[2], int(int_in));
126 EXPECT_EQ(data[3], int(bool_in));
127 }
128};
129
131{
132 ShaderSpecializationConst("gpu_compute_specialization_test");
133}
134GPU_TEST(specialization_constants_compute)
135
137{
138 ShaderSpecializationConst("gpu_graphic_specialization_test");
139}
140GPU_TEST(specialization_constants_graphic)
141
142} // 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:56
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)
@ GPU_BATCH_OWNS_VBO
Definition GPU_batch.hh:42
void GPU_compute_dispatch(GPUShader *shader, uint groups_x_len, uint groups_y_len, uint groups_z_len)
void GPU_render_end()
void GPU_render_begin()
GPUFrameBuffer * GPU_framebuffer_create(const char *name)
void GPU_framebuffer_default_size(GPUFrameBuffer *framebuffer, int width, int height)
void GPU_framebuffer_bind(GPUFrameBuffer *framebuffer)
void GPU_framebuffer_free(GPUFrameBuffer *framebuffer)
@ GPU_PRIM_POINTS
void GPU_shader_constant_int(GPUShader *sh, const char *name, int value)
GPUShader * GPU_shader_create_from_info_name(const char *info_name)
void GPU_shader_constant_uint(GPUShader *sh, const char *name, unsigned int value)
int GPU_shader_get_ssbo_binding(GPUShader *shader, const char *name)
void GPU_shader_constant_bool(GPUShader *sh, const char *name, bool value)
void GPU_shader_free(GPUShader *shader)
void GPU_shader_unbind()
void GPU_shader_constant_float(GPUShader *sh, const char *name, float value)
void GPU_memory_barrier(eGPUBarrier barrier)
Definition gpu_state.cc:374
void GPU_finish()
Definition gpu_state.cc:299
@ 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 *, const char *name, GPUVertCompType, uint comp_len, GPUVertFetchMode)
@ GPU_COMP_U32
struct GPUShader GPUShader
constexpr bool is_empty() const
static float verts[][3]
struct @620::@622 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...