Blender V5.3
shader_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 "BLI_math_base_c.hh"
9
10#include "GPU_batch.hh"
11#include "GPU_batch_presets.hh"
12#include "GPU_capabilities.hh"
13#include "GPU_compute.hh"
14#include "GPU_context.hh"
15#include "GPU_framebuffer.hh"
16#include "GPU_index_buffer.hh"
17#include "GPU_shader.hh"
18#include "GPU_shader_shared.hh"
19#include "GPU_state.hh"
20#include "GPU_texture.hh"
21#include "GPU_vertex_buffer.hh"
22#include "GPU_vertex_format.hh"
23
24#include "MEM_guardedalloc.h"
25
29#include "gpu_shader_private.hh"
30#include "gpu_testing.hh"
31
32/* GTest expects operator<< and Print to be defined in the same namespace as the type itself. */
33static std::ostream &operator<<(std::ostream &os, const TestOutput &test_output)
34{
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;
40 return os;
41}
42
43namespace blender {
44
45namespace gpu::tests {
46
47using namespace blender::gpu::shader;
48
49/* This test should contain pure GLSL source as this is what we are expecting from the Python API.
50 * Make sure to keep it in sync with the Python API. */
52{
53 using namespace shader;
54
55 ShaderCreateInfo create_info("pyGPU_Shader");
56 create_info.image(0,
57 TextureFormat::SFLOAT_16_16_16_16,
59 ImageReadWriteType::image2D,
60 "test_img");
61 create_info.local_group_size(16, 16, 1);
62 create_info.compute_source_generated =
63 R"(void main(){imageStore(test_img, ivec2(gl_GlobalInvocationID.xy), vec4(0));})";
64
66 reinterpret_cast<GPUShaderCreateInfo *>(&create_info));
67 EXPECT_NE(shader, nullptr);
68
70}
71GPU_TEST(shader_python_compute)
72
73/* This test should contain pure GLSL source as this is what we are expecting from the Python API.
74 * Make sure to keep it in sync with the Python API. */
76{
77 using namespace shader;
78
79 ShaderCreateInfo create_info("pyGPU_Shader");
80 create_info.vertex_in(0, Type::float4_t, "vert_in");
81 create_info.fragment_out(0, Type::float4_t, "frag_out");
82 create_info.vertex_source_generated = R"(void main(){gl_Position = vert_in;})";
83 create_info.fragment_source_generated = R"(void main() { frag_out = gl_FragCoord; })";
84
86 reinterpret_cast<GPUShaderCreateInfo *>(&create_info));
87 EXPECT_NE(shader, nullptr);
88
90}
91GPU_TEST(shader_python_graphic)
92
94{
95
96 static constexpr uint SIZE = 512;
97
98 /* Build compute shader. */
99 gpu::Shader *shader = GPU_shader_create_from_info_name("gpu_compute_2d_test");
100 EXPECT_NE(shader, nullptr);
101
102 /* Create texture to store result and attach to shader. */
103 gpu::Texture *texture = GPU_texture_create_2d("gpu_shader_compute_2d",
104 SIZE,
105 SIZE,
106 1,
107 TextureFormat::SFLOAT_32_32_32_32,
109 nullptr);
110 EXPECT_NE(texture, nullptr);
111
114
115 /* Dispatch compute task. */
117
118 /* Check if compute has been done. */
120 float *data = static_cast<float *>(GPU_texture_read(texture, GPU_DATA_FLOAT, 0));
121 EXPECT_NE(data, nullptr);
122 for (int index = 0; index < SIZE * SIZE; index++) {
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);
127 }
128 MEM_delete(data);
129
130 /* Cleanup. */
135}
136GPU_TEST(shader_compute_2d)
137
139{
140 static constexpr uint SIZE = 10;
141
142 /* Build compute shader. */
143 gpu::Shader *shader = GPU_shader_create_from_info_name("gpu_compute_1d_test");
144 EXPECT_NE(shader, nullptr);
145
146 /* Construct Texture. */
147 gpu::Texture *texture = GPU_texture_create_1d("gpu_shader_compute_1d",
148 SIZE,
149 1,
150 TextureFormat::SFLOAT_32_32_32_32,
152 nullptr);
153 EXPECT_NE(texture, nullptr);
154
157
158 /* Dispatch compute task. */
160
161 /* Check if compute has been done. */
163
164 /* Create texture to load back result. */
165 float *data = static_cast<float *>(GPU_texture_read(texture, GPU_DATA_FLOAT, 0));
166 EXPECT_NE(data, nullptr);
167 for (int index = 0; index < SIZE; index++) {
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);
173 }
174 MEM_delete(data);
175
176 /* Cleanup. */
181}
182GPU_TEST(shader_compute_1d)
183
185{
186 static constexpr uint SIZE = 128;
187
188 /* Build compute shader. */
189 gpu::Shader *shader = GPU_shader_create_from_info_name("gpu_compute_vbo_test");
190 EXPECT_NE(shader, nullptr);
192
193 /* Construct VBO. */
194 GPUVertFormat format = {0};
195 GPU_vertformat_attr_add(&format, "pos", gpu::VertAttrType::SFLOAT_32_32_32_32);
199
200 /* Dispatch compute task. */
202
203 /* Check if compute has been done. */
205
206 /* Download the vertex buffer. */
207 float data[SIZE * 4];
209 for (int index = 0; index < SIZE; index++) {
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);
215 }
216
217 /* Cleanup. */
221}
222GPU_TEST(shader_compute_vbo)
223
225{
226 static constexpr uint SIZE = 128;
227
228 /* Build compute shader. */
229 gpu::Shader *shader = GPU_shader_create_from_info_name("gpu_compute_ibo_test");
230 EXPECT_NE(shader, nullptr);
232
233 /* Construct IBO. */
236
237 /* Dispatch compute task. */
239
240 /* Check if compute has been done. */
242
243 /* Download the index buffer. */
246 for (int index = 0; index < SIZE; index++) {
247 uint32_t expected = index;
248 EXPECT_EQ(data[index], expected);
249 }
250
251 /* Cleanup. */
255}
256GPU_TEST(shader_compute_ibo)
257
259{
260 static constexpr uint SIZE = 128;
261
262 /* Build compute shader. */
263 gpu::Shader *shader = GPU_shader_create_from_info_name("gpu_compute_ssbo_test");
264 EXPECT_NE(shader, nullptr);
266
267 /* Construct SSBO. */
269 SIZE * sizeof(uint32_t), nullptr, GPU_USAGE_DEVICE_ONLY, __func__);
271
272 /* Dispatch compute task. */
274
275 /* Check if compute has been done. */
277
278 /* Download the storage buffer. */
281 for (int index = 0; index < SIZE; index++) {
282 uint32_t expected = index * 4;
283 EXPECT_EQ(data[index], expected);
284 }
285
286 /* Cleanup. */
290}
291GPU_TEST(shader_compute_ssbo)
292
294{
295 /* Build compute shader. */
296 gpu::Shader *shader = GPU_shader_create_from_info_name("gpu_compute_ssbo_binding_test");
297 EXPECT_NE(shader, nullptr);
298
299 /* Perform tests. */
302
303 /* Cleanup. */
305}
306GPU_TEST(shader_ssbo_binding)
307
308#ifdef WITH_METAL_BACKEND
309static void test_shader_sampler_argument_buffer_binding()
310{
311 gpu::Shader *shader = GPU_shader_create_from_info_name("gpu_sampler_arg_buf_test");
312 EXPECT_NE(shader, nullptr);
313
314 gpu::StorageBuf *ssbo = GPU_storagebuf_create(sizeof(float) * 4 * 18);
315
317
318 float4 tx_data(-1.0f, 1.0f, 2.0f, 3.0f);
320 "tx", 1, 1, 1, TextureFormat::SFLOAT_32_32_32_32, GPU_TEXTURE_USAGE_SHADER_READ, &tx_data.x);
321
340
344
346
348 GPU_batch_draw(batch);
349
350 GPU_batch_discard(batch);
351
352 GPU_finish();
353
354 float4 data[18];
356
357 for (int index = 0; index < 18; index++) {
358 EXPECT_EQ(data[index], tx_data);
359 }
360
361 /* Cleanup. */
365 GPU_texture_free(tex);
367}
368GPU_TEST(shader_sampler_argument_buffer_binding)
369#endif
370
372{
373 gpu::Shader *shader = GPU_shader_create_from_info_name("gpu_texture_atomic_test");
374 EXPECT_NE(shader, nullptr);
375
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);
385
387 GPU_texture_clear(tex_2d_array, eGPUDataFormat::GPU_DATA_UINT, &tx_data[0]);
389
391 GPU_texture_image_bind(tex_2d_array,
392 GPU_shader_get_sampler_binding(shader, "img_atomic_2D_array"));
394
397
399 GPU_shader_uniform_1b(shader, "write_phase", true);
402
403 /* We can't host read atomic texture. So we do a manual read phase to a SSBO. */
404 GPU_shader_uniform_1b(shader, "write_phase", false);
407 GPU_finish();
408
409 uint32_t data[5];
411
412 EXPECT_EQ(data[0], 0xFFFFFFFFu);
413 EXPECT_EQ(data[1], 0xFFFFFFFFu);
414 EXPECT_EQ(data[2], 0xFFFFFFFFu);
415 EXPECT_EQ(data[3], 0xFFFFFFFFu);
416 EXPECT_EQ(data[4], 0xFFFFFFFFu);
417
418 /* Cleanup. */
419 GPU_texture_free(tex_2d);
420 GPU_texture_free(tex_2d_array);
421 GPU_texture_free(tex_3d);
425}
426GPU_TEST(shader_texture_atomic)
427
428static std::string print_test_data(const TestOutputRawData &raw, TestType type)
429{
430 std::stringstream ss;
431 switch (type) {
432 case TEST_TYPE_BOOL:
433 case TEST_TYPE_UINT:
434 ss << *reinterpret_cast<const uint *>(&raw);
435 break;
436 case TEST_TYPE_INT:
437 ss << *reinterpret_cast<const int *>(&raw);
438 break;
439 case TEST_TYPE_FLOAT:
440 ss << *reinterpret_cast<const float *>(&raw);
441 break;
442 case TEST_TYPE_IVEC2:
443 ss << *reinterpret_cast<const int2 *>(&raw);
444 break;
445 case TEST_TYPE_IVEC3:
446 ss << *reinterpret_cast<const int3 *>(&raw);
447 break;
448 case TEST_TYPE_IVEC4:
449 ss << *reinterpret_cast<const int4 *>(&raw);
450 break;
451 case TEST_TYPE_UVEC2:
452 ss << *reinterpret_cast<const uint2 *>(&raw);
453 break;
454 case TEST_TYPE_UVEC3:
455 ss << *reinterpret_cast<const uint3 *>(&raw);
456 break;
457 case TEST_TYPE_UVEC4:
458 ss << *reinterpret_cast<const uint4 *>(&raw);
459 break;
460 case TEST_TYPE_VEC2:
461 ss << *reinterpret_cast<const float2 *>(&raw);
462 break;
463 case TEST_TYPE_VEC3:
464 ss << *reinterpret_cast<const float3 *>(&raw);
465 break;
466 case TEST_TYPE_VEC4:
467 ss << *reinterpret_cast<const float4 *>(&raw);
468 break;
469 case TEST_TYPE_MAT2X2:
470 ss << *reinterpret_cast<const float2x2 *>(&raw);
471 break;
472 case TEST_TYPE_MAT2X3:
473 ss << *reinterpret_cast<const float2x3 *>(&raw);
474 break;
475 case TEST_TYPE_MAT2X4:
476 ss << *reinterpret_cast<const float2x4 *>(&raw);
477 break;
478 case TEST_TYPE_MAT3X2:
479 ss << *reinterpret_cast<const float3x2 *>(&raw);
480 break;
481 case TEST_TYPE_MAT3X3:
482 ss << *reinterpret_cast<const float3x3 *>(&raw);
483 break;
484 case TEST_TYPE_MAT3X4:
485 ss << *reinterpret_cast<const float3x4 *>(&raw);
486 break;
487 case TEST_TYPE_MAT4X2:
488 ss << *reinterpret_cast<const float4x2 *>(&raw);
489 break;
490 case TEST_TYPE_MAT4X3:
491 ss << *reinterpret_cast<const float4x3 *>(&raw);
492 break;
493 case TEST_TYPE_MAT4X4:
494 ss << *reinterpret_cast<const float4x4 *>(&raw);
495 break;
496 default:
497 ss << *reinterpret_cast<const MatBase<uint, 4, 4> *>(&raw);
498 break;
499 }
500 return ss.str();
501}
502
504{
505 std::string needle = "].line = " + std::to_string(test_line) + ";";
506
507 int64_t pos = test_src.find(needle);
508 if (pos == std::string::npos) {
509 return "";
510 }
511 int64_t start = test_src.rfind('\n', pos);
512 if (start == std::string::npos) {
513 return "";
514 }
515 int64_t end = test_src.find('\n', pos);
516 if (end == std::string::npos) {
517 return "";
518 }
519 return test_src.substr(start, end - start);
520}
521
522static void gpu_shader_lib_test(StringRefNull create_info_name)
523{
524 using namespace shader;
525
527
528 const GPUShaderCreateInfo *info = gpu_shader_create_info_get(create_info_name.c_str());
530 *reinterpret_cast<const gpu::shader::ShaderCreateInfo *>(info);
531
532 StringRefNull test_src_name = create_info.compute_source_;
533 StringRefNull test_src = gpu_shader_dependency_get_source(test_src_name);
534
536 reinterpret_cast<GPUShaderCreateInfo *>(&create_info));
537
538 int test_count = 0;
539 /* Count tests. */
540 int64_t pos = 0;
541 StringRefNull target = "EXPECT_";
542 while ((pos = test_src.find(target, pos)) != std::string::npos) {
543 test_count++;
544 pos += sizeof("EXPECT_");
545 }
546
547 gpu::StorageBuf *output_buf = GPU_storagebuf_create(test_count * sizeof(TestOutput));
548
549 GPU_storagebuf_clear(output_buf, 0xFFFFFFFFu);
550 GPU_storagebuf_bind(output_buf, 0);
551
553
554 GPU_finish();
555
557 tests.resize(test_count);
558 GPU_storagebuf_read(output_buf, tests.data());
559
560 for (const TestOutput &test : tests) {
561 if (ELEM(test.status, TEST_STATUS_NONE, TEST_STATUS_PASSED)) {
562 continue;
563 }
564 if (test.status == TEST_STATUS_FAILED) {
565 ADD_FAILURE_AT(test_src_name.c_str(), test.line)
566 << "Value of: " << print_test_line(test_src, test.line) << "\n"
567 << " Actual: " << print_test_data(test.expect, TestType(test.type)) << "\n"
568 << "Expected: " << print_test_data(test.result, TestType(test.type)) << "\n";
569 }
570 else {
571 ADD_FAILURE() << "Unexpected test status " << test.status << ", test output:\n" << test;
572 }
573 }
574
575 /* Cleanup. */
578 GPU_storagebuf_free(output_buf);
579
581}
582
583static void test_math_lib()
584{
585 gpu_shader_lib_test("gpu_math_test");
586}
587GPU_TEST(math_lib)
588
589static void test_eevee_lib()
590{
592 {
593 GTEST_SKIP() << "Test is failing on NVIDIA/Vulkan for unknown reasons. It runs, but outputs "
594 "back the initial value 0xFFFFFFFFu.";
595 }
596
597 /* TODO(fclem): Not passing currently. Need to be updated. */
598 // gpu_shader_lib_test("eevee_shadow_test.bsl.hh");
599 gpu_shader_lib_test("eevee_test_occupancy");
600 gpu_shader_lib_test("eevee_test_fast_gi");
601 /* Disabled on mac because of a buffer reuse bug. Seems to be fixed on Tahoe. */
603 gpu_shader_lib_test("eevee_test_gbuffer_normal");
604 gpu_shader_lib_test("eevee_test_gbuffer_closure");
605 }
606}
607GPU_TEST(eevee_lib)
608
610{
611 {
612 std::string input = R"(
613# define MACRO() A
614MACRO()
615)";
616 std::string expect = R"(
617
618A
619)";
620 std::string result = blender::gpu::Shader::run_preprocessor(input, true);
621 EXPECT_EQ(expect, result);
622 }
623 {
624 std::string input = R"(
625# define MACRO(A, B)
626MACRO(a, 1)
627)";
628 std::string expect = R"(
629
630
631)";
632 std::string result = blender::gpu::Shader::run_preprocessor(input, true);
633 EXPECT_EQ(expect, result);
634 }
635 {
636 std::string input = R"(
637#define MACRO(A, B, ...) \
638 A to_##A(B m) \
639 { \
640 return A(__VA_ARGS__); \
641 }
642
643MACRO(a, b, 1, 2)
644)";
645 std::string expect = R"(
646
647
648
649
650
651
652a to_a(b m) { return a(1, 2); }
653)";
654 std::string result = blender::gpu::Shader::run_preprocessor(input, true);
655 EXPECT_EQ(expect, result);
656 }
657 {
658 std::string input = R"(
659# if 1
660# define drw_view_id 0
661# else
662uint drw_view_id = 0;
663# endif
664 )";
665 std::string expect = R"(
666
667
668
669
670
671 )";
672 std::string result = blender::gpu::Shader::run_preprocessor(input, true);
673 EXPECT_EQ(expect, result);
674 }
675 {
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);
680)";
681 std::string expect = R"(
682
683
684(-(255.0f / 127.0f) * SMAASearchLength(searchTex, e, 0.0f) + 3.25f);
685)";
686 std::string result = blender::gpu::Shader::run_preprocessor(input, true);
687 EXPECT_EQ(expect, result);
688 }
689 {
690 std::string input = R"(
691#define A() B
692A)";
693 std::string expect = R"(
694
695A)";
696 std::string result = blender::gpu::Shader::run_preprocessor(input, true);
697 EXPECT_EQ(expect, result);
698 }
699 {
700 std::string input = R"(
701#define A(a, b) C(a[b])[(b)]
702#define B(a, b) (A(a, b) != 0u)
703B(foo, bar);
704)";
705 std::string expect = R"(
706
707
708(C(foo[bar])[(bar)] != 0u);
709)";
710 std::string result = blender::gpu::Shader::run_preprocessor(input, true);
711 EXPECT_EQ(expect, result);
712 }
713 {
714 std::string input = R"(
715#define B2 5
716#define A(a,b) a##b
717#define B(a,b) a ## b
718#define C 3
719#define D(a,b) B(a,b)
720A( , )
721A(,2)
722A(1, )
723A(1,2)
724A(B,2)
725B(B,2)
726B(C,2)
727D(C,2)
728)";
729 std::string expect = R"(
730
731
732
733
734
735
7362
7371
73812
7395
7405
741C2
74232
743)";
744 std::string result = blender::gpu::Shader::run_preprocessor(input, true);
745 EXPECT_EQ(expect, result);
746 }
747 {
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) \
751 { \
752 return atomic_##C##_explicit((A _atomic<T> *)&mem, data, memory_order_relaxed); \
753 }
754
755#define ATOMIC_OP(B, C) \
756 ATOMIC_OP_EX(threadgroup, B, C) \
757 ATOMIC_OP_EX(device, B, C)
758
759ATOMIC_OP(Max, fetch_max)
760)";
761 std::string expect = R"(
762
763
764
765
766
767
768
769
770
771
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); }
773)";
774 std::string result = blender::gpu::Shader::run_preprocessor(input, true);
775 EXPECT_EQ(expect, result);
776 }
777 {
778 std::string input = R"(
779#define A
780#if defined(A) && !defined ( B ) && defined A && !defined B
781High there!
782#endif
783)";
784 std::string expect = R"(
785
786
787High there!
788
789)";
790 std::string result = blender::gpu::Shader::run_preprocessor(input, true);
791 EXPECT_EQ(expect, result);
792 }
793 {
794 /* Undefined identifier should evaluated to 0. */
795 std::string input = R"(
796#if !A
797A
798#endif
799)";
800 std::string expect = R"(
801
802A
803
804)";
805 std::string result = blender::gpu::Shader::run_preprocessor(input, true);
806 EXPECT_EQ(expect, result);
807 }
808 {
809 /* Infinite recursion. */
810 std::string input = R"(
811#define X (X + 1)
812X
813)";
814 std::string expect = R"(
815
816(X + 1)
817)";
818 std::string result = blender::gpu::Shader::run_preprocessor(input, true);
819 EXPECT_EQ(expect, result);
820 }
821 {
822 /* Arguments must be expanded before substitution. */
823 std::string input = R"(
824#define ESCAPE(x) x
825#define STR(x) x
826#define NAME shader_func
827STR(ESCAPE(NAME))
828)";
829 /* STR(ESCAPE(NAME)) -> STR(shader_func) -> "shader_func" */
830 std::string expect = R"(
831
832
833
834shader_func
835)";
836 std::string result = blender::gpu::Shader::run_preprocessor(input, true);
837 EXPECT_EQ(expect, result);
838 }
839 {
840 /* Commas inside parentheses should not split macro arguments. */
841 std::string input = R"(
842#define GLSL_FUNC(a, b) a = b;
843GLSL_FUNC(vec3(0.0, 1.0, 0.0), color)
844)";
845 std::string expect = R"(
846
847vec3(0.0, 1.0, 0.0) = color;
848)";
849 std::string result = blender::gpu::Shader::run_preprocessor(input, true);
850 EXPECT_EQ(expect, result);
851 }
852 {
853 /* PITFALL: Pasting an empty argument. */
854 std::string input = R"(
855#define CONCAT(a, b) a##b
856CONCAT(prefix_, )
857CONCAT(, _suffix)
858)";
859 std::string expect = R"(
860
861prefix_
862_suffix
863)";
864 std::string result = blender::gpu::Shader::run_preprocessor(input, true);
865 EXPECT_EQ(expect, result);
866 }
867 {
868 /* PITFALL: Evaluation of complex logical expressions and nested #if. */
869 std::string input = R"(
870#define VERSION 2
871#if (VERSION == 1 + 1) && (UNDEFINED_VAR == 0)
872 #if 0
873 Inside nested false
874 #else
875 Success
876 #endif
877#else
878Success
879#endif
880)";
881 std::string expect = R"(
882
883
884
885
886
887 Success
888
889
890
891
892)";
893 std::string result = blender::gpu::Shader::run_preprocessor(input, true);
894 EXPECT_EQ(expect, result);
895 }
896 {
897 std::string input = R"(
898#define saturate(a) clamp(a, 0.0f, 1.0f)
899float s = saturate(pow5f(1.0f - saturate(HV)));
900)";
901 std::string expect = R"(
902
903float s = clamp(pow5f(1.0f - clamp(HV, 0.0f, 1.0f)), 0.0f, 1.0f);
904)";
905 std::string result = blender::gpu::Shader::run_preprocessor(input, true);
906 EXPECT_EQ(expect, result);
907 }
908 {
909 std::string input = R"(
910#define POINTS
911H
912#if defined(POINTS)
913I
914#else
915J
916# if !defined(SELECT_ENABLE)
917K
918# endif
919P
920#endif
921Q
922)";
923 std::string expect = R"(
924
925H
926
927I
928
929
930
931
932
933
934
935Q
936)";
937 std::string result = blender::gpu::Shader::run_preprocessor(input, true);
938 EXPECT_EQ(expect, result);
939 }
940}
941GPU_TEST(shader_preprocessor)
942
943} // namespace gpu::tests
944} // namespace blender
@ TEST_TYPE_IVEC2
@ TEST_TYPE_UINT
@ TEST_TYPE_INT
@ TEST_TYPE_IVEC4
@ TEST_TYPE_MAT2X2
@ TEST_TYPE_VEC2
@ TEST_TYPE_FLOAT
@ TEST_TYPE_VEC3
@ TEST_TYPE_BOOL
@ TEST_TYPE_IVEC3
@ TEST_TYPE_UVEC3
@ TEST_TYPE_MAT3X3
@ TEST_TYPE_UVEC2
@ TEST_TYPE_MAT4X4
@ TEST_TYPE_MAT2X3
@ TEST_TYPE_MAT4X3
@ TEST_TYPE_MAT3X2
@ TEST_TYPE_MAT4X2
@ TEST_TYPE_UVEC4
@ TEST_TYPE_MAT2X4
@ TEST_TYPE_VEC4
@ TEST_TYPE_MAT3X4
@ TEST_STATUS_NONE
@ TEST_STATUS_PASSED
@ TEST_STATUS_FAILED
#define GPU_storagebuf_create(size)
Read Blender memory allocation and freeing.
#define ELEM(...)
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()
BMesh Iterators.
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)
__int64 int64_t
Definition BKE_text.h:90
gpu::IndexBuf * GPU_indexbuf_build_on_device(uint index_len)
gpu::Batch * GPU_batch_create_procedural(GPUPrimType primitive_type, int32_t vertex_count)
Definition gpu_batch.cc:85
bool GPU_type_matches_ex(GPUDeviceType device, GPUOSType os, GPUDriverType driver, GPUBackendType backend)
unsigned int uint
Definition BKE_text.h:55
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_render_begin()
void GPU_batch_set_shader(gpu::Batch *batch, gpu::Shader *shader, const gpu::shader::SpecializationConstants *constants_state=nullptr)
@ GPU_DRIVER_OFFICIAL
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
unsigned int uint32_t
Definition BKE_text.h:81
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_render_end()
void GPU_memory_barrier(GPUBarrier barrier)
Definition gpu_state.cc:305
BMesh const char void * data
void GPU_shader_unbind()
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
Definition GPU_state.hh:59
@ GPU_BARRIER_TEXTURE_UPDATE
Definition GPU_state.hh:42
@ GPU_BARRIER_SHADER_IMAGE_ACCESS
Definition GPU_state.hh:38
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 *)
void GPU_finish()
Definition gpu_state.cc:289
@ GPU_DEVICE_NVIDIA
@ GPU_DEVICE_APPLE
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...
Self & image(int slot, TextureFormat format, Qualifier qualifiers, ImageReadWriteType type, StringRefNull name, Frequency freq=Frequency::PASS, ConditionFn cond=nullptr)
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)
Self & local_group_size(int local_size_x, int local_size_y=1, int local_size_z=1)