5#include "testing/testing.h"
35 os <<
"expect: " << testing::PrintToString(test_output.
expect) <<
"\n";
36 os <<
"result: " << testing::PrintToString(test_output.
result) <<
"\n";
37 os <<
"status: " << test_output.
status;
38 os <<
", line: " << test_output.
line;
39 os <<
", type: " << test_output.
type;
47using namespace blender::gpu::shader;
57 TextureFormat::SFLOAT_16_16_16_16,
59 ImageReadWriteType::image2D,
63 R
"(void main(){imageStore(test_img, ivec2(gl_GlobalInvocationID.xy), vec4(0));})";
66 reinterpret_cast<GPUShaderCreateInfo *
>(&create_info));
86 reinterpret_cast<GPUShaderCreateInfo *
>(&create_info));
107 TextureFormat::SFLOAT_32_32_32_32,
123 EXPECT_FLOAT_EQ(
data[
index * 4 + 0], 1.0f);
124 EXPECT_FLOAT_EQ(
data[
index * 4 + 1], 0.5f);
125 EXPECT_FLOAT_EQ(
data[
index * 4 + 2], 0.2f);
126 EXPECT_FLOAT_EQ(
data[
index * 4 + 3], 1.0f);
150 TextureFormat::SFLOAT_32_32_32_32,
168 float expected_value =
index;
169 EXPECT_FLOAT_EQ(
data[
index * 4 + 0], expected_value);
170 EXPECT_FLOAT_EQ(
data[
index * 4 + 1], expected_value);
171 EXPECT_FLOAT_EQ(
data[
index * 4 + 2], expected_value);
172 EXPECT_FLOAT_EQ(
data[
index * 4 + 3], expected_value);
210 float expected_value =
index;
211 EXPECT_FLOAT_EQ(
data[
index * 4 + 0], expected_value);
212 EXPECT_FLOAT_EQ(
data[
index * 4 + 1], expected_value);
213 EXPECT_FLOAT_EQ(
data[
index * 4 + 2], expected_value);
214 EXPECT_FLOAT_EQ(
data[
index * 4 + 3], expected_value);
308#ifdef WITH_METAL_BACKEND
309static void test_shader_sampler_argument_buffer_binding()
318 float4 tx_data(-1.0f, 1.0f, 2.0f, 3.0f);
368GPU_TEST(shader_sampler_argument_buffer_binding)
378 uint32_t tx_data[4] = {0u, 0u, 0u, 0u};
380 "tex_2d", 1, 1, 1, TextureFormat::UINT_32, usage,
nullptr);
382 "tex_2d_array", 1, 1, 2, 1, TextureFormat::UINT_32, usage,
nullptr);
384 "tex_3d", 1, 1, 2, 1, TextureFormat::UINT_32, usage,
nullptr);
430 std::stringstream ss;
434 ss << *reinterpret_cast<const uint *>(&raw);
437 ss << *reinterpret_cast<const int *>(&raw);
440 ss << *reinterpret_cast<const float *>(&raw);
443 ss << *reinterpret_cast<const int2 *>(&raw);
446 ss << *reinterpret_cast<const int3 *>(&raw);
449 ss << *reinterpret_cast<const int4 *>(&raw);
452 ss << *reinterpret_cast<const uint2 *>(&raw);
455 ss << *reinterpret_cast<const uint3 *>(&raw);
458 ss << *reinterpret_cast<const uint4 *>(&raw);
461 ss << *reinterpret_cast<const float2 *>(&raw);
464 ss << *reinterpret_cast<const float3 *>(&raw);
467 ss << *reinterpret_cast<const float4 *>(&raw);
470 ss << *reinterpret_cast<const float2x2 *>(&raw);
473 ss << *reinterpret_cast<const float2x3 *>(&raw);
476 ss << *reinterpret_cast<const float2x4 *>(&raw);
479 ss << *reinterpret_cast<const float3x2 *>(&raw);
482 ss << *reinterpret_cast<const float3x3 *>(&raw);
485 ss << *reinterpret_cast<const float3x4 *>(&raw);
488 ss << *reinterpret_cast<const float4x2 *>(&raw);
491 ss << *reinterpret_cast<const float4x3 *>(&raw);
494 ss << *reinterpret_cast<const float4x4 *>(&raw);
497 ss << *reinterpret_cast<const MatBase<uint, 4, 4> *>(&raw);
505 std::string needle =
"].line = " + std::to_string(test_line) +
";";
508 if (pos == std::string::npos) {
512 if (start == std::string::npos) {
516 if (end == std::string::npos) {
519 return test_src.
substr(start, end - start);
536 reinterpret_cast<GPUShaderCreateInfo *
>(&create_info));
542 while ((pos = test_src.
find(target, pos)) != std::string::npos) {
544 pos +=
sizeof(
"EXPECT_");
557 tests.resize(test_count);
565 ADD_FAILURE_AT(test_src_name.
c_str(), test.line)
571 ADD_FAILURE() <<
"Unexpected test status " << test.status <<
", test output:\n" << test;
593 GTEST_SKIP() <<
"Test is failing on NVIDIA/Vulkan for unknown reasons. It runs, but outputs "
594 "back the initial value 0xFFFFFFFFu.";
612 std::string input = R
"(
616 std::string expect = R"(
624 std::string input = R
"(
628 std::string expect = R"(
636 std::string input = R
"(
637#define MACRO(A, B, ...) \
640 return A(__VA_ARGS__); \
645 std::string expect = R"(
652a to_a(b m) { return a(1, 2); }
658 std::string input = R
"(
660# define drw_view_id 0
665 std::string expect = R"(
676 std::string input = R
"(
677#define SMAATexturePass2D(tex) tex
678#define mad(a, b, c) (a * b + c)
679mad(-(255.0f / 127.0f), SMAASearchLength(SMAATexturePass2D(searchTex), e, 0.0f), 3.25f);
681 std::string expect = R"(
684(-(255.0f / 127.0f) * SMAASearchLength(searchTex, e, 0.0f) + 3.25f);
690 std::string input = R
"(
693 std::string expect = R"(
700 std::string input = R
"(
701#define A(a, b) C(a[b])[(b)]
702#define B(a, b) (A(a, b) != 0u)
705 std::string expect = R"(
708(C(foo[bar])[(bar)] != 0u);
714 std::string input = R
"(
729 std::string expect = R"(
748 std::string input = R
"(
749#define ATOMIC_OP_EX(A, B, C) \
750 template<typename T> T atomic##B(A T &mem, T data) \
752 return atomic_##C##_explicit((A _atomic<T> *)&mem, data, memory_order_relaxed); \
755#define ATOMIC_OP(B, C) \
756 ATOMIC_OP_EX(threadgroup, B, C) \
757 ATOMIC_OP_EX(device, B, C)
759ATOMIC_OP(Max, fetch_max)
761 std::string expect = R"(
772template<typename T> T atomicMax(threadgroup T &mem, T data) { return atomic_fetch_max_explicit((threadgroup _atomic<T> *)&mem, data, memory_order_relaxed); } template<typename T> T atomicMax(device T &mem, T data) { return atomic_fetch_max_explicit((device _atomic<T> *)&mem, data, memory_order_relaxed); }
778 std::string input = R
"(
780#if defined(A) && !defined ( B ) && defined A && !defined B
784 std::string expect = R"(
795 std::string input = R
"(
800 std::string expect = R"(
810 std::string input = R
"(
814 std::string expect = R"(
823 std::string input = R
"(
826#define NAME shader_func
830 std::string expect = R
"(
841 std::string input = R
"(
842#define GLSL_FUNC(a, b) a = b;
843GLSL_FUNC(vec3(0.0, 1.0, 0.0), color)
845 std::string expect = R"(
847vec3(0.0, 1.0, 0.0) = color;
854 std::string input = R
"(
855#define CONCAT(a, b) a##b
859 std::string expect = R"(
869 std::string input = R
"(
871#if (VERSION == 1 + 1) && (UNDEFINED_VAR == 0)
881 std::string expect = R"(
897 std::string input = R
"(
898#define saturate(a) clamp(a, 0.0f, 1.0f)
899float s = saturate(pow5f(1.0f - saturate(HV)));
901 std::string expect = R"(
903float s = clamp(pow5f(1.0f - clamp(HV, 0.0f, 1.0f)), 0.0f, 1.0f);
909 std::string input = R
"(
916# if !defined(SELECT_ENABLE)
923 std::string expect = R"(
#define GPU_storagebuf_create(size)
Read Blender memory allocation and freeing.
constexpr const char * c_str() const
constexpr int64_t rfind(char c, int64_t pos=INT64_MAX) const
constexpr int64_t find(char c, int64_t pos=0) const
constexpr StringRef substr(int64_t start, int64_t size) const
static std::string run_preprocessor(StringRef source, bool no_dead_code_elimination)
TEX_TEMPLATE DataVec texture(T, FltCoord, float=0.0f) RET
#define EXPECT_NE(result, expect)
#define GPU_TEST(test_name)
StringRefNull gpu_shader_dependency_get_source(const StringRefNull shader_source_name)
static void test_shader_compute_2d()
static void test_shader_compute_ssbo()
static void test_shader_texture_atomic()
static std::string print_test_data(const TestOutputRawData &raw, TestType type)
static void test_shader_preprocessor()
static void gpu_shader_lib_test(StringRefNull create_info_name)
static void test_shader_python_graphic()
static void test_eevee_lib()
static void test_shader_python_compute()
static void test_shader_compute_vbo()
static void test_math_lib()
static void test_shader_ssbo_binding()
static void test_shader_compute_1d()
static StringRef print_test_line(StringRefNull test_src, int64_t test_line)
static void test_shader_compute_ibo()
void GPU_framebuffer_bind(gpu::FrameBuffer *fb)
int GPU_shader_get_sampler_binding(gpu::Shader *shader, const char *name)
gpu::Texture * GPU_texture_create_1d(const char *name, int width, int mip_len, gpu::TextureFormat format, eGPUTextureUsage usage, const float *data)
gpu::IndexBuf * GPU_indexbuf_build_on_device(uint index_len)
gpu::Batch * GPU_batch_create_procedural(GPUPrimType primitive_type, int32_t vertex_count)
bool GPU_type_matches_ex(GPUDeviceType device, GPUOSType os, GPUDriverType driver, GPUBackendType backend)
void GPU_framebuffer_free(gpu::FrameBuffer *fb)
gpu::Shader * GPU_shader_create_from_info_python(const GPUShaderCreateInfo *_info)
gpu::StorageBuf * GPU_storagebuf_create_ex(size_t size, const void *data, GPUUsageType usage, const char *name)
BLI_INLINE float fb(float length, float L)
gpu::Shader * GPU_shader_create_from_info_name(const char *info_name)
void GPU_vertbuf_read(const gpu::VertBuf *verts, void *data)
gpu::Shader * GPU_shader_create_from_info(const GPUShaderCreateInfo *_info)
gpu::Texture * GPU_texture_create_2d_array(const char *name, int width, int height, int layer_len, int mip_len, gpu::TextureFormat format, eGPUTextureUsage usage, const float *data)
void GPU_batch_discard(gpu::Batch *batch)
void GPU_batch_set_shader(gpu::Batch *batch, gpu::Shader *shader, const gpu::shader::SpecializationConstants *constants_state=nullptr)
void GPU_compute_dispatch(gpu::Shader *shader, uint groups_x_len, uint groups_y_len, uint groups_z_len, const gpu::shader::SpecializationConstants *constants_state=nullptr)
void GPU_shader_uniform_1b(gpu::Shader *sh, const char *name, bool value)
void GPU_vertbuf_bind_as_ssbo(gpu::VertBuf *verts, int binding)
int GPU_shader_get_ssbo_binding(gpu::Shader *shader, const char *name)
void GPU_indexbuf_bind_as_ssbo(gpu::IndexBuf *elem, int binding)
void GPU_storagebuf_clear(gpu::StorageBuf *ssbo, uint32_t clear_value)
VecBase< float, 4 > float4
bool GPU_type_matches(GPUDeviceType device, GPUOSType os, GPUDriverType driver)
@ GPU_TEXTURE_USAGE_SHADER_READ
@ GPU_TEXTURE_USAGE_SHADER_WRITE
@ GPU_TEXTURE_USAGE_GENERAL
@ GPU_TEXTURE_USAGE_ATOMIC
void GPU_memory_barrier(GPUBarrier barrier)
BMesh const char void * data
gpu::Texture * GPU_texture_create_3d(const char *name, int width, int height, int depth, int mip_len, gpu::TextureFormat format, eGPUTextureUsage usage, const void *data)
void GPU_batch_draw(gpu::Batch *batch)
void GPU_shader_bind(gpu::Shader *shader, const gpu::shader::SpecializationConstants *constants_state=nullptr)
@ GPU_BARRIER_BUFFER_UPDATE
@ GPU_BARRIER_TEXTURE_UPDATE
@ GPU_BARRIER_SHADER_IMAGE_ACCESS
void GPU_storagebuf_bind(gpu::StorageBuf *ssbo, int slot)
gpu::VertBuf * GPU_vertbuf_create_with_format_ex(const GPUVertFormat &format, GPUUsageType usage)
uint GPU_vertformat_attr_add(GPUVertFormat *format, StringRef name, gpu::VertAttrType type)
gpu::FrameBuffer * GPU_framebuffer_create(const char *name)
const GPUShaderCreateInfo * gpu_shader_create_info_get(const char *info_name)
EXPECT_EQ(BLI_expr_pylike_eval(expr, nullptr, 0, &result), EXPR_PYLIKE_INVALID)
void GPU_indexbuf_read(gpu::IndexBuf *elem, uint32_t *data)
void GPU_framebuffer_default_size(gpu::FrameBuffer *fb, int width, int height)
void GPU_texture_clear(gpu::Texture *texture, eGPUDataFormat data_format, const void *data)
void GPU_texture_bind(gpu::Texture *texture, int unit)
void GPU_texture_free(gpu::Texture *texture)
void GPU_vertbuf_data_alloc(gpu::VertBuf &verts, uint v_len)
void GPU_indexbuf_discard(gpu::IndexBuf *elem)
void GPU_texture_image_bind(gpu::Texture *texture, int unit)
void GPU_texture_unbind(gpu::Texture *texture)
void GPU_shader_free(gpu::Shader *shader)
void * GPU_texture_read(gpu::Texture *texture, eGPUDataFormat data_format, int mip_level)
void GPU_storagebuf_free(gpu::StorageBuf *ssbo)
void GPU_vertbuf_discard(gpu::VertBuf *)
gpu::Texture * GPU_texture_create_2d(const char *name, int width, int height, int mip_len, gpu::TextureFormat format, eGPUTextureUsage usage, const float *data)
void GPU_storagebuf_read(gpu::StorageBuf *ssbo, void *data)
static std::ostream & operator<<(std::ostream &os, const TestOutput &test_output)
struct TestOutputRawData result
struct TestOutputRawData expect
Describe inputs & outputs, stage interfaces, resources and sources of a shader. If all data is correc...
StringRefNull compute_source_
Self & image(int slot, TextureFormat format, Qualifier qualifiers, ImageReadWriteType type, StringRefNull name, Frequency freq=Frequency::PASS, ConditionFn cond=nullptr)
std::string fragment_source_generated
Self & vertex_in(int slot, Type type, StringRefNull name, ConditionFn cond=nullptr)
Self & fragment_out(int slot, Type type, StringRefNull name, DualBlend blend=DualBlend::NONE, int raster_order_group=-1, ConditionFn cond=nullptr)
std::string vertex_source_generated
std::string compute_source_generated
Self & local_group_size(int local_size_x, int local_size_y=1, int local_size_z=1)