1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909
|
// Copyright (c) 2022 The Khronos Group Inc.
// Copyright (c) 2022 LunarG Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#include <unordered_set>
#include "test/opt/pass_fixture.h"
#include "test/opt/pass_utils.h"
namespace spvtools {
namespace opt {
namespace {
using AnalyzeLiveInputTest = PassTest<::testing::Test>;
TEST_F(AnalyzeLiveInputTest, FragMultipleLocations) {
// Should report locations {2, 5}
//
// #version 450
//
// layout(location = 2) in Vertex
// {
// vec4 color0;
// vec4 color1;
// vec4 color2[3];
// } iVert;
//
// layout(location = 0) out vec4 oFragColor;
//
// void main()
// {
// oFragColor = iVert.color0 + iVert.color2[1];
// }
const std::string text = R"(
OpCapability Shader
%1 = OpExtInstImport "GLSL.std.450"
OpMemoryModel Logical GLSL450
OpEntryPoint Fragment %main "main" %oFragColor %iVert
OpExecutionMode %main OriginUpperLeft
OpSource GLSL 450
OpName %main "main"
OpName %oFragColor "oFragColor"
OpName %Vertex "Vertex"
OpMemberName %Vertex 0 "color0"
OpMemberName %Vertex 1 "color1"
OpMemberName %Vertex 2 "color2"
OpName %iVert "iVert"
OpDecorate %oFragColor Location 0
OpDecorate %Vertex Block
OpDecorate %iVert Location 2
%void = OpTypeVoid
%3 = OpTypeFunction %void
%float = OpTypeFloat 32
%v4float = OpTypeVector %float 4
%_ptr_Output_v4float = OpTypePointer Output %v4float
%oFragColor = OpVariable %_ptr_Output_v4float Output
%uint = OpTypeInt 32 0
%uint_3 = OpConstant %uint 3
%_arr_v4float_uint_3 = OpTypeArray %v4float %uint_3
%Vertex = OpTypeStruct %v4float %v4float %_arr_v4float_uint_3
%_ptr_Input_Vertex = OpTypePointer Input %Vertex
%iVert = OpVariable %_ptr_Input_Vertex Input
%int = OpTypeInt 32 1
%int_0 = OpConstant %int 0
%_ptr_Input_v4float = OpTypePointer Input %v4float
%int_2 = OpConstant %int 2
%int_1 = OpConstant %int 1
%main = OpFunction %void None %3
%5 = OpLabel
%19 = OpAccessChain %_ptr_Input_v4float %iVert %int_0
%20 = OpLoad %v4float %19
%23 = OpAccessChain %_ptr_Input_v4float %iVert %int_2 %int_1
%24 = OpLoad %v4float %23
%25 = OpFAdd %v4float %20 %24
OpStore %oFragColor %25
OpReturn
OpFunctionEnd
)";
SetTargetEnv(SPV_ENV_VULKAN_1_3);
SetAssembleOptions(SPV_TEXT_TO_BINARY_OPTION_PRESERVE_NUMERIC_IDS);
std::unordered_set<uint32_t> live_inputs;
std::unordered_set<uint32_t> live_builtins;
auto result = SinglePassRunToBinary<AnalyzeLiveInputPass>(
text, true, &live_inputs, &live_builtins);
auto itr0 = live_inputs.find(0);
auto itr1 = live_inputs.find(1);
auto itr2 = live_inputs.find(2);
auto itr3 = live_inputs.find(3);
auto itr4 = live_inputs.find(4);
auto itr5 = live_inputs.find(5);
auto itr6 = live_inputs.find(6);
// Expect live_inputs == {2, 5}
EXPECT_TRUE(itr0 == live_inputs.end());
EXPECT_TRUE(itr1 == live_inputs.end());
EXPECT_TRUE(itr2 != live_inputs.end());
EXPECT_TRUE(itr3 == live_inputs.end());
EXPECT_TRUE(itr4 == live_inputs.end());
EXPECT_TRUE(itr5 != live_inputs.end());
EXPECT_TRUE(itr6 == live_inputs.end());
}
TEST_F(AnalyzeLiveInputTest, FragMatrix) {
// Should report locations {2, 8, 9, 10, 11}
//
// #version 450
//
// uniform ui_name {
// int i;
// } ui_inst;
//
// layout(location = 2) in Vertex
// {
// vec4 color0;
// vec4 color1;
// mat4 color2;
// mat4 color3;
// mat4 color4;
// } iVert;
//
// // Output variable for the color
// layout(location = 0) out vec4 oFragColor;
//
// void main()
// {
// oFragColor = iVert.color0 + iVert.color3[ui_inst.i];
// }
const std::string text = R"(
OpCapability Shader
%1 = OpExtInstImport "GLSL.std.450"
OpMemoryModel Logical GLSL450
OpEntryPoint Fragment %main "main" %oFragColor %iVert %ui_inst
OpExecutionMode %main OriginUpperLeft
OpSource GLSL 450
OpName %main "main"
OpName %oFragColor "oFragColor"
OpName %Vertex "Vertex"
OpMemberName %Vertex 0 "color0"
OpMemberName %Vertex 1 "color1"
OpMemberName %Vertex 2 "color2"
OpMemberName %Vertex 3 "color3"
OpMemberName %Vertex 4 "color4"
OpName %iVert "iVert"
OpName %ui_name "ui_name"
OpMemberName %ui_name 0 "i"
OpName %ui_inst "ui_inst"
OpDecorate %oFragColor Location 0
OpDecorate %Vertex Block
OpDecorate %iVert Location 2
OpMemberDecorate %ui_name 0 Offset 0
OpDecorate %ui_name Block
OpDecorate %ui_inst DescriptorSet 0
OpDecorate %ui_inst Binding 0
%void = OpTypeVoid
%3 = OpTypeFunction %void
%float = OpTypeFloat 32
%v4float = OpTypeVector %float 4
%_ptr_Output_v4float = OpTypePointer Output %v4float
%oFragColor = OpVariable %_ptr_Output_v4float Output
%mat4v4float = OpTypeMatrix %v4float 4
%Vertex = OpTypeStruct %v4float %v4float %mat4v4float %mat4v4float %mat4v4float
%_ptr_Input_Vertex = OpTypePointer Input %Vertex
%iVert = OpVariable %_ptr_Input_Vertex Input
%int = OpTypeInt 32 1
%int_0 = OpConstant %int 0
%_ptr_Input_v4float = OpTypePointer Input %v4float
%int_3 = OpConstant %int 3
%ui_name = OpTypeStruct %int
%_ptr_Uniform_ui_name = OpTypePointer Uniform %ui_name
%ui_inst = OpVariable %_ptr_Uniform_ui_name Uniform
%_ptr_Uniform_int = OpTypePointer Uniform %int
%main = OpFunction %void None %3
%5 = OpLabel
%17 = OpAccessChain %_ptr_Input_v4float %iVert %int_0
%18 = OpLoad %v4float %17
%24 = OpAccessChain %_ptr_Uniform_int %ui_inst %int_0
%25 = OpLoad %int %24
%26 = OpAccessChain %_ptr_Input_v4float %iVert %int_3 %25
%27 = OpLoad %v4float %26
%28 = OpFAdd %v4float %18 %27
OpStore %oFragColor %28
OpReturn
OpFunctionEnd
)";
SetTargetEnv(SPV_ENV_VULKAN_1_3);
SetAssembleOptions(SPV_TEXT_TO_BINARY_OPTION_PRESERVE_NUMERIC_IDS);
std::unordered_set<uint32_t> live_inputs;
std::unordered_set<uint32_t> live_builtins;
auto result = SinglePassRunToBinary<AnalyzeLiveInputPass>(
text, true, &live_inputs, &live_builtins);
auto itr0 = live_inputs.find(0);
auto itr1 = live_inputs.find(1);
auto itr2 = live_inputs.find(2);
auto itr3 = live_inputs.find(3);
auto itr4 = live_inputs.find(4);
auto itr5 = live_inputs.find(5);
auto itr6 = live_inputs.find(6);
auto itr7 = live_inputs.find(7);
auto itr8 = live_inputs.find(8);
auto itr9 = live_inputs.find(9);
auto itr10 = live_inputs.find(10);
auto itr11 = live_inputs.find(11);
auto itr12 = live_inputs.find(12);
auto itr13 = live_inputs.find(13);
auto itr14 = live_inputs.find(14);
auto itr15 = live_inputs.find(15);
// Expect live_inputs == {2, 8, 9, 10, 11}
EXPECT_TRUE(itr0 == live_inputs.end());
EXPECT_TRUE(itr1 == live_inputs.end());
EXPECT_TRUE(itr2 != live_inputs.end());
EXPECT_TRUE(itr3 == live_inputs.end());
EXPECT_TRUE(itr4 == live_inputs.end());
EXPECT_TRUE(itr5 == live_inputs.end());
EXPECT_TRUE(itr6 == live_inputs.end());
EXPECT_TRUE(itr7 == live_inputs.end());
EXPECT_TRUE(itr8 != live_inputs.end());
EXPECT_TRUE(itr9 != live_inputs.end());
EXPECT_TRUE(itr10 != live_inputs.end());
EXPECT_TRUE(itr11 != live_inputs.end());
EXPECT_TRUE(itr12 == live_inputs.end());
EXPECT_TRUE(itr13 == live_inputs.end());
EXPECT_TRUE(itr14 == live_inputs.end());
EXPECT_TRUE(itr15 == live_inputs.end());
}
TEST_F(AnalyzeLiveInputTest, FragMemberLocs) {
// Should report location {1}
//
// #version 450
//
// in Vertex
// {
// layout (location = 1) vec4 Cd;
// layout (location = 0) vec2 uv;
// } iVert;
//
// layout (location = 0) out vec4 fragColor;
//
// void main()
// {
// vec4 color = vec4(iVert.Cd);
// fragColor = color;
// }
const std::string text = R"(
OpCapability Shader
%1 = OpExtInstImport "GLSL.std.450"
OpMemoryModel Logical GLSL450
OpEntryPoint Fragment %main "main" %iVert %fragColor
OpExecutionMode %main OriginUpperLeft
OpSource GLSL 450
OpName %main "main"
OpName %color "color"
OpName %Vertex "Vertex"
OpMemberName %Vertex 0 "Cd"
OpMemberName %Vertex 1 "uv"
OpName %iVert "iVert"
OpName %fragColor "fragColor"
OpMemberDecorate %Vertex 0 Location 1
OpMemberDecorate %Vertex 1 Location 0
OpDecorate %Vertex Block
OpDecorate %fragColor Location 0
OpDecorate %_struct_27 Block
OpMemberDecorate %_struct_27 0 Location 1
%void = OpTypeVoid
%3 = OpTypeFunction %void
%float = OpTypeFloat 32
%v4float = OpTypeVector %float 4
%_ptr_Function_v4float = OpTypePointer Function %v4float
%v2float = OpTypeVector %float 2
%Vertex = OpTypeStruct %v4float %v2float
%_ptr_Input_Vertex = OpTypePointer Input %Vertex
%int = OpTypeInt 32 1
%int_0 = OpConstant %int 0
%_ptr_Input_v4float = OpTypePointer Input %v4float
%_ptr_Output_v4float = OpTypePointer Output %v4float
%fragColor = OpVariable %_ptr_Output_v4float Output
%_struct_27 = OpTypeStruct %v4float
%_ptr_Input__struct_27 = OpTypePointer Input %_struct_27
%iVert = OpVariable %_ptr_Input__struct_27 Input
%main = OpFunction %void None %3
%5 = OpLabel
%color = OpVariable %_ptr_Function_v4float Function
%17 = OpAccessChain %_ptr_Input_v4float %iVert %int_0
%18 = OpLoad %v4float %17
%19 = OpCompositeExtract %float %18 0
%20 = OpCompositeExtract %float %18 1
%21 = OpCompositeExtract %float %18 2
%22 = OpCompositeExtract %float %18 3
%23 = OpCompositeConstruct %v4float %19 %20 %21 %22
OpStore %color %23
%26 = OpLoad %v4float %color
OpStore %fragColor %26
OpReturn
OpFunctionEnd
)";
SetTargetEnv(SPV_ENV_VULKAN_1_3);
SetAssembleOptions(SPV_TEXT_TO_BINARY_OPTION_PRESERVE_NUMERIC_IDS);
std::unordered_set<uint32_t> live_inputs;
std::unordered_set<uint32_t> live_builtins;
auto result = SinglePassRunToBinary<AnalyzeLiveInputPass>(
text, true, &live_inputs, &live_builtins);
auto itr0 = live_inputs.find(0);
auto itr1 = live_inputs.find(1);
// Expect live_inputs == {2, 5}
EXPECT_TRUE(itr0 == live_inputs.end());
EXPECT_TRUE(itr1 != live_inputs.end());
}
TEST_F(AnalyzeLiveInputTest, ArrayedInput) {
// Tests handling of arrayed input seen in Tesc, Tese and Geom shaders.
//
// Should report location {1, 10}.
//
// #version 450
//
// layout (vertices = 4) out;
//
// layout (location = 1) in Vertex
// {
// vec4 p;
// vec3 n;
// vec4 f[100];
// } iVert[];
//
// layout (location = 0) out vec4 position[4];
//
// void main()
// {
// vec4 pos = iVert[gl_InvocationID].p *
// iVert[gl_InvocationID].f[7];
// position[gl_InvocationID] = pos;
// }
const std::string text = R"(
OpCapability Tessellation
%1 = OpExtInstImport "GLSL.std.450"
OpMemoryModel Logical GLSL450
OpEntryPoint TessellationControl %main "main" %iVert %gl_InvocationID %position
OpExecutionMode %main OutputVertices 4
OpSource GLSL 450
OpName %main "main"
OpName %Vertex "Vertex"
OpMemberName %Vertex 0 "p"
OpMemberName %Vertex 1 "n"
OpMemberName %Vertex 2 "f"
OpName %iVert "iVert"
OpName %gl_InvocationID "gl_InvocationID"
OpName %position "position"
OpDecorate %Vertex Block
OpDecorate %iVert Location 1
OpDecorate %gl_InvocationID BuiltIn InvocationId
OpDecorate %position Location 0
%void = OpTypeVoid
%3 = OpTypeFunction %void
%float = OpTypeFloat 32
%v4float = OpTypeVector %float 4
%v3float = OpTypeVector %float 3
%uint = OpTypeInt 32 0
%uint_100 = OpConstant %uint 100
%_arr_v4float_uint_100 = OpTypeArray %v4float %uint_100
%Vertex = OpTypeStruct %v4float %v3float %_arr_v4float_uint_100
%uint_32 = OpConstant %uint 32
%_arr_Vertex_uint_32 = OpTypeArray %Vertex %uint_32
%_ptr_Input__arr_Vertex_uint_32 = OpTypePointer Input %_arr_Vertex_uint_32
%iVert = OpVariable %_ptr_Input__arr_Vertex_uint_32 Input
%int = OpTypeInt 32 1
%_ptr_Input_int = OpTypePointer Input %int
%gl_InvocationID = OpVariable %_ptr_Input_int Input
%int_0 = OpConstant %int 0
%_ptr_Input_v4float = OpTypePointer Input %v4float
%int_2 = OpConstant %int 2
%int_7 = OpConstant %int 7
%uint_4 = OpConstant %uint 4
%_arr_v4float_uint_4 = OpTypeArray %v4float %uint_4
%_ptr_Output__arr_v4float_uint_4 = OpTypePointer Output %_arr_v4float_uint_4
%position = OpVariable %_ptr_Output__arr_v4float_uint_4 Output
%_ptr_Output_v4float = OpTypePointer Output %v4float
%main = OpFunction %void None %3
%5 = OpLabel
%22 = OpLoad %int %gl_InvocationID
%25 = OpAccessChain %_ptr_Input_v4float %iVert %22 %int_0
%26 = OpLoad %v4float %25
%30 = OpAccessChain %_ptr_Input_v4float %iVert %22 %int_2 %int_7
%31 = OpLoad %v4float %30
%32 = OpFMul %v4float %26 %31
%40 = OpAccessChain %_ptr_Output_v4float %position %22
OpStore %40 %32
OpReturn
OpFunctionEnd
)";
SetTargetEnv(SPV_ENV_VULKAN_1_3);
SetAssembleOptions(SPV_TEXT_TO_BINARY_OPTION_PRESERVE_NUMERIC_IDS);
std::unordered_set<uint32_t> live_inputs;
std::unordered_set<uint32_t> live_builtins;
auto result = SinglePassRunToBinary<AnalyzeLiveInputPass>(
text, true, &live_inputs, &live_builtins);
auto itr0 = live_inputs.find(0);
auto itr1 = live_inputs.find(1);
auto itr2 = live_inputs.find(2);
auto itr3 = live_inputs.find(3);
auto itr4 = live_inputs.find(4);
auto itr5 = live_inputs.find(5);
auto itr6 = live_inputs.find(6);
auto itr7 = live_inputs.find(7);
auto itr8 = live_inputs.find(8);
auto itr9 = live_inputs.find(9);
auto itr10 = live_inputs.find(10);
auto itr11 = live_inputs.find(11);
// Expect live_inputs == {1, 10}
EXPECT_TRUE(itr0 == live_inputs.end());
EXPECT_TRUE(itr1 != live_inputs.end());
EXPECT_TRUE(itr2 == live_inputs.end());
EXPECT_TRUE(itr3 == live_inputs.end());
EXPECT_TRUE(itr4 == live_inputs.end());
EXPECT_TRUE(itr5 == live_inputs.end());
EXPECT_TRUE(itr6 == live_inputs.end());
EXPECT_TRUE(itr7 == live_inputs.end());
EXPECT_TRUE(itr8 == live_inputs.end());
EXPECT_TRUE(itr9 == live_inputs.end());
EXPECT_TRUE(itr10 != live_inputs.end());
EXPECT_TRUE(itr11 == live_inputs.end());
}
TEST_F(AnalyzeLiveInputTest, ArrayedInputMemberLocs) {
// Tests handling of member locs with arrayed input seen in Tesc, Tese
// and Geom shaders.
//
// Should report location {1, 12}.
//
// #version 450
//
// layout (vertices = 4) out;
//
// in Vertex
// {
// layout (location = 1) vec4 p;
// layout (location = 3) vec3 n;
// layout (location = 5) vec4 f[100];
// } iVert[];
//
// layout (location = 0) out vec4 position[4];
//
// void main()
// {
// vec4 pos = iVert[gl_InvocationID].p *
// iVert[gl_InvocationID].f[7];
// position[gl_InvocationID] = pos;
// }
const std::string text = R"(
OpCapability Tessellation
%1 = OpExtInstImport "GLSL.std.450"
OpMemoryModel Logical GLSL450
OpEntryPoint TessellationControl %main "main" %iVert %gl_InvocationID %position
OpExecutionMode %main OutputVertices 4
OpSource GLSL 450
OpName %main "main"
OpName %Vertex "Vertex"
OpMemberName %Vertex 0 "p"
OpMemberName %Vertex 1 "n"
OpMemberName %Vertex 2 "f"
OpName %iVert "iVert"
OpName %gl_InvocationID "gl_InvocationID"
OpName %position "position"
OpMemberDecorate %Vertex 0 Location 1
OpMemberDecorate %Vertex 1 Location 3
OpMemberDecorate %Vertex 2 Location 5
OpDecorate %Vertex Block
OpDecorate %gl_InvocationID BuiltIn InvocationId
OpDecorate %position Location 0
%void = OpTypeVoid
%3 = OpTypeFunction %void
%float = OpTypeFloat 32
%v4float = OpTypeVector %float 4
%v3float = OpTypeVector %float 3
%uint = OpTypeInt 32 0
%uint_100 = OpConstant %uint 100
%_arr_v4float_uint_100 = OpTypeArray %v4float %uint_100
%Vertex = OpTypeStruct %v4float %v3float %_arr_v4float_uint_100
%uint_32 = OpConstant %uint 32
%_arr_Vertex_uint_32 = OpTypeArray %Vertex %uint_32
%_ptr_Input__arr_Vertex_uint_32 = OpTypePointer Input %_arr_Vertex_uint_32
%iVert = OpVariable %_ptr_Input__arr_Vertex_uint_32 Input
%int = OpTypeInt 32 1
%_ptr_Input_int = OpTypePointer Input %int
%gl_InvocationID = OpVariable %_ptr_Input_int Input
%int_0 = OpConstant %int 0
%_ptr_Input_v4float = OpTypePointer Input %v4float
%int_2 = OpConstant %int 2
%int_7 = OpConstant %int 7
%uint_4 = OpConstant %uint 4
%_arr_v4float_uint_4 = OpTypeArray %v4float %uint_4
%_ptr_Output__arr_v4float_uint_4 = OpTypePointer Output %_arr_v4float_uint_4
%position = OpVariable %_ptr_Output__arr_v4float_uint_4 Output
%_ptr_Output_v4float = OpTypePointer Output %v4float
%main = OpFunction %void None %3
%5 = OpLabel
%22 = OpLoad %int %gl_InvocationID
%25 = OpAccessChain %_ptr_Input_v4float %iVert %22 %int_0
%26 = OpLoad %v4float %25
%30 = OpAccessChain %_ptr_Input_v4float %iVert %22 %int_2 %int_7
%31 = OpLoad %v4float %30
%32 = OpFMul %v4float %26 %31
%40 = OpAccessChain %_ptr_Output_v4float %position %22
OpStore %40 %32
OpReturn
OpFunctionEnd
)";
SetTargetEnv(SPV_ENV_VULKAN_1_3);
SetAssembleOptions(SPV_TEXT_TO_BINARY_OPTION_PRESERVE_NUMERIC_IDS);
std::unordered_set<uint32_t> live_inputs;
std::unordered_set<uint32_t> live_builtins;
auto result = SinglePassRunToBinary<AnalyzeLiveInputPass>(
text, true, &live_inputs, &live_builtins);
auto itr0 = live_inputs.find(0);
auto itr1 = live_inputs.find(1);
auto itr2 = live_inputs.find(2);
auto itr3 = live_inputs.find(3);
auto itr4 = live_inputs.find(4);
auto itr5 = live_inputs.find(5);
auto itr6 = live_inputs.find(6);
auto itr7 = live_inputs.find(7);
auto itr8 = live_inputs.find(8);
auto itr9 = live_inputs.find(9);
auto itr10 = live_inputs.find(10);
auto itr11 = live_inputs.find(11);
auto itr12 = live_inputs.find(12);
auto itr13 = live_inputs.find(13);
// Expect live_inputs == {1, 12}
EXPECT_TRUE(itr0 == live_inputs.end());
EXPECT_TRUE(itr1 != live_inputs.end());
EXPECT_TRUE(itr2 == live_inputs.end());
EXPECT_TRUE(itr3 == live_inputs.end());
EXPECT_TRUE(itr4 == live_inputs.end());
EXPECT_TRUE(itr5 == live_inputs.end());
EXPECT_TRUE(itr6 == live_inputs.end());
EXPECT_TRUE(itr7 == live_inputs.end());
EXPECT_TRUE(itr8 == live_inputs.end());
EXPECT_TRUE(itr9 == live_inputs.end());
EXPECT_TRUE(itr10 == live_inputs.end());
EXPECT_TRUE(itr11 == live_inputs.end());
EXPECT_TRUE(itr12 != live_inputs.end());
EXPECT_TRUE(itr13 == live_inputs.end());
}
TEST_F(AnalyzeLiveInputTest, Builtins) {
// Tests handling of builtin input seen in Tesc, Tese and Geom shaders.
//
// Should report builtin gl_PointSize only.
//
// #version 460
//
// layout(triangle_strip, max_vertices = 3) out;
// layout(triangles) in;
//
// void main()
// {
// for (int i = 0; i < 3; i++)
// {
// gl_Position = gl_in[i].gl_Position;
// gl_PointSize = gl_in[i].gl_PointSize;
//
// EmitVertex();
// }
//
// EndPrimitive();
// }
const std::string text = R"(
OpCapability Geometry
OpCapability GeometryPointSize
%1 = OpExtInstImport "GLSL.std.450"
OpMemoryModel Logical GLSL450
OpEntryPoint Geometry %main "main" %_ %gl_in
OpExecutionMode %main Triangles
OpExecutionMode %main Invocations 1
OpExecutionMode %main OutputTriangleStrip
OpExecutionMode %main OutputVertices 3
OpSource GLSL 460
OpName %main "main"
OpName %i "i"
OpName %gl_PerVertex "gl_PerVertex"
OpMemberName %gl_PerVertex 0 "gl_Position"
OpMemberName %gl_PerVertex 1 "gl_PointSize"
OpMemberName %gl_PerVertex 2 "gl_ClipDistance"
OpMemberName %gl_PerVertex 3 "gl_CullDistance"
OpName %_ ""
OpName %gl_PerVertex_0 "gl_PerVertex"
OpMemberName %gl_PerVertex_0 0 "gl_Position"
OpMemberName %gl_PerVertex_0 1 "gl_PointSize"
OpMemberName %gl_PerVertex_0 2 "gl_ClipDistance"
OpMemberName %gl_PerVertex_0 3 "gl_CullDistance"
OpName %gl_in "gl_in"
OpMemberDecorate %gl_PerVertex 0 BuiltIn Position
OpMemberDecorate %gl_PerVertex 1 BuiltIn PointSize
OpMemberDecorate %gl_PerVertex 2 BuiltIn ClipDistance
OpMemberDecorate %gl_PerVertex 3 BuiltIn CullDistance
OpDecorate %gl_PerVertex Block
OpMemberDecorate %gl_PerVertex_0 0 BuiltIn Position
OpMemberDecorate %gl_PerVertex_0 1 BuiltIn PointSize
OpDecorate %gl_PerVertex_0 Block
%void = OpTypeVoid
%3 = OpTypeFunction %void
%int = OpTypeInt 32 1
%_ptr_Function_int = OpTypePointer Function %int
%int_0 = OpConstant %int 0
%int_3 = OpConstant %int 3
%bool = OpTypeBool
%float = OpTypeFloat 32
%v4float = OpTypeVector %float 4
%uint = OpTypeInt 32 0
%uint_1 = OpConstant %uint 1
%_arr_float_uint_1 = OpTypeArray %float %uint_1
%gl_PerVertex = OpTypeStruct %v4float %float %_arr_float_uint_1 %_arr_float_uint_1
%_ptr_Output_gl_PerVertex = OpTypePointer Output %gl_PerVertex
%_ = OpVariable %_ptr_Output_gl_PerVertex Output
%gl_PerVertex_0 = OpTypeStruct %v4float %float
%uint_3 = OpConstant %uint 3
%_arr_gl_PerVertex_0_uint_3 = OpTypeArray %gl_PerVertex_0 %uint_3
%_ptr_Input__arr_gl_PerVertex_0_uint_3 = OpTypePointer Input %_arr_gl_PerVertex_0_uint_3
%gl_in = OpVariable %_ptr_Input__arr_gl_PerVertex_0_uint_3 Input
%_ptr_Input_v4float = OpTypePointer Input %v4float
%_ptr_Output_v4float = OpTypePointer Output %v4float
%int_1 = OpConstant %int 1
%_ptr_Input_float = OpTypePointer Input %float
%_ptr_Output_float = OpTypePointer Output %float
%main = OpFunction %void None %3
%5 = OpLabel
%i = OpVariable %_ptr_Function_int Function
OpStore %i %int_0
OpBranch %10
%10 = OpLabel
OpLoopMerge %12 %13 None
OpBranch %14
%14 = OpLabel
%15 = OpLoad %int %i
%18 = OpSLessThan %bool %15 %int_3
OpBranchConditional %18 %11 %12
%11 = OpLabel
%32 = OpLoad %int %i
%34 = OpAccessChain %_ptr_Input_v4float %gl_in %32 %int_0
%35 = OpLoad %v4float %34
%37 = OpAccessChain %_ptr_Output_v4float %_ %int_0
OpStore %37 %35
%39 = OpLoad %int %i
%41 = OpAccessChain %_ptr_Input_float %gl_in %39 %int_1
%42 = OpLoad %float %41
%44 = OpAccessChain %_ptr_Output_float %_ %int_1
OpStore %44 %42
OpEmitVertex
OpBranch %13
%13 = OpLabel
%45 = OpLoad %int %i
%46 = OpIAdd %int %45 %int_1
OpStore %i %46
OpBranch %10
%12 = OpLabel
OpEndPrimitive
OpReturn
OpFunctionEnd
)";
SetTargetEnv(SPV_ENV_VULKAN_1_3);
SetAssembleOptions(SPV_TEXT_TO_BINARY_OPTION_PRESERVE_NUMERIC_IDS);
std::unordered_set<uint32_t> live_inputs;
std::unordered_set<uint32_t> live_builtins;
auto result = SinglePassRunToBinary<AnalyzeLiveInputPass>(
text, true, &live_inputs, &live_builtins);
auto itr0 = live_builtins.find((uint32_t)spv::BuiltIn::PointSize);
auto itr1 = live_builtins.find((uint32_t)spv::BuiltIn::ClipDistance);
auto itr2 = live_builtins.find((uint32_t)spv::BuiltIn::CullDistance);
// Expect live_builtins == { spv::BuiltIn::PointSize }
EXPECT_TRUE(itr0 != live_builtins.end());
EXPECT_TRUE(itr1 == live_builtins.end());
EXPECT_TRUE(itr2 == live_builtins.end());
}
TEST_F(AnalyzeLiveInputTest, ArrayedInputPatchLocs) {
// Tests handling of locs with arrayed input patch seen in Tese
//
// Should report location {3}.
//
// #version 450 core
//
// layout(triangles, ccw) in;
//
// layout(fractional_odd_spacing) in;
//
// layout(point_mode) in;
//
// layout(location=2) patch in float patchIn1[2];
//
// void main()
// {
// vec4 p = gl_in[1].gl_Position;
// gl_Position = p * patchIn1[1];
// }
const std::string text = R"(
OpCapability Tessellation
%1 = OpExtInstImport "GLSL.std.450"
OpMemoryModel Logical GLSL450
OpEntryPoint TessellationEvaluation %main "main" %gl_in %_ %patchIn1
OpExecutionMode %main Triangles
OpExecutionMode %main SpacingFractionalOdd
OpExecutionMode %main VertexOrderCcw
OpExecutionMode %main PointMode
OpSource GLSL 450
OpName %main "main"
OpName %p "p"
OpName %gl_PerVertex "gl_PerVertex"
OpMemberName %gl_PerVertex 0 "gl_Position"
OpName %gl_in "gl_in"
OpName %gl_PerVertex_0 "gl_PerVertex"
OpMemberName %gl_PerVertex_0 0 "gl_Position"
OpName %_ ""
OpName %patchIn1 "patchIn1"
OpMemberDecorate %gl_PerVertex 0 BuiltIn Position
OpDecorate %gl_PerVertex Block
OpMemberDecorate %gl_PerVertex_0 0 BuiltIn Position
OpDecorate %gl_PerVertex_0 Block
OpDecorate %patchIn1 Patch
OpDecorate %patchIn1 Location 2
%void = OpTypeVoid
%3 = OpTypeFunction %void
%float = OpTypeFloat 32
%v4float = OpTypeVector %float 4
%_ptr_Function_v4float = OpTypePointer Function %v4float
%uint = OpTypeInt 32 0
%uint_1 = OpConstant %uint 1
%_arr_float_uint_1 = OpTypeArray %float %uint_1
%gl_PerVertex = OpTypeStruct %v4float
%uint_32 = OpConstant %uint 32
%_arr_gl_PerVertex_uint_32 = OpTypeArray %gl_PerVertex %uint_32
%_ptr_Input__arr_gl_PerVertex_uint_32 = OpTypePointer Input %_arr_gl_PerVertex_uint_32
%gl_in = OpVariable %_ptr_Input__arr_gl_PerVertex_uint_32 Input
%int = OpTypeInt 32 1
%int_1 = OpConstant %int 1
%int_0 = OpConstant %int 0
%_ptr_Input_v4float = OpTypePointer Input %v4float
%gl_PerVertex_0 = OpTypeStruct %v4float
%_ptr_Output_gl_PerVertex_0 = OpTypePointer Output %gl_PerVertex_0
%_ = OpVariable %_ptr_Output_gl_PerVertex_0 Output
%uint_2 = OpConstant %uint 2
%_arr_float_uint_2 = OpTypeArray %float %uint_2
%_ptr_Input__arr_float_uint_2 = OpTypePointer Input %_arr_float_uint_2
%patchIn1 = OpVariable %_ptr_Input__arr_float_uint_2 Input
%_ptr_Input_float = OpTypePointer Input %float
%_ptr_Output_v4float = OpTypePointer Output %v4float
%main = OpFunction %void None %3
%5 = OpLabel
%p = OpVariable %_ptr_Function_v4float Function
%22 = OpAccessChain %_ptr_Input_v4float %gl_in %int_1 %int_0
%23 = OpLoad %v4float %22
OpStore %p %23
%27 = OpLoad %v4float %p
%33 = OpAccessChain %_ptr_Input_float %patchIn1 %int_1
%34 = OpLoad %float %33
%35 = OpVectorTimesScalar %v4float %27 %34
%37 = OpAccessChain %_ptr_Output_v4float %_ %int_0
OpStore %37 %35
OpReturn
OpFunctionEnd
)";
SetTargetEnv(SPV_ENV_VULKAN_1_3);
SetAssembleOptions(SPV_TEXT_TO_BINARY_OPTION_PRESERVE_NUMERIC_IDS);
std::unordered_set<uint32_t> live_inputs;
std::unordered_set<uint32_t> live_builtins;
auto result = SinglePassRunToBinary<AnalyzeLiveInputPass>(
text, true, &live_inputs, &live_builtins);
auto itr0 = live_inputs.find(0);
auto itr1 = live_inputs.find(1);
auto itr2 = live_inputs.find(2);
auto itr3 = live_inputs.find(3);
// Expect live_inputs == {3}
EXPECT_TRUE(itr0 == live_inputs.end());
EXPECT_TRUE(itr1 == live_inputs.end());
EXPECT_TRUE(itr2 == live_inputs.end());
EXPECT_TRUE(itr3 != live_inputs.end());
}
TEST_F(AnalyzeLiveInputTest, FragMultipleLocationsF16) {
// Should report locations {2, 5}
//
// #version 450
//
// layout(location = 2) in Vertex
// {
// f16vec4 color0;
// f16vec4 color1;
// f16vec4 color2[3];
// } iVert;
//
// layout(location = 0) out f16vec4 oFragColor;
//
// void main()
// {
// oFragColor = iVert.color0 + iVert.color2[1];
// }
const std::string text = R"(
OpCapability Shader
OpCapability Float16
OpCapability StorageInputOutput16
%1 = OpExtInstImport "GLSL.std.450"
OpMemoryModel Logical GLSL450
OpEntryPoint Fragment %main "main" %oFragColor %iVert
OpExecutionMode %main OriginUpperLeft
OpSource GLSL 450
OpName %main "main"
OpName %oFragColor "oFragColor"
OpName %Vertex "Vertex"
OpMemberName %Vertex 0 "color0"
OpMemberName %Vertex 1 "color1"
OpMemberName %Vertex 2 "color2"
OpName %iVert "iVert"
OpDecorate %oFragColor Location 0
OpDecorate %Vertex Block
OpDecorate %iVert Location 2
%void = OpTypeVoid
%3 = OpTypeFunction %void
%half = OpTypeFloat 16
%v4half = OpTypeVector %half 4
%_ptr_Output_v4half = OpTypePointer Output %v4half
%oFragColor = OpVariable %_ptr_Output_v4half Output
%uint = OpTypeInt 32 0
%uint_3 = OpConstant %uint 3
%_arr_v4half_uint_3 = OpTypeArray %v4half %uint_3
%Vertex = OpTypeStruct %v4half %v4half %_arr_v4half_uint_3
%_ptr_Input_Vertex = OpTypePointer Input %Vertex
%iVert = OpVariable %_ptr_Input_Vertex Input
%int = OpTypeInt 32 1
%int_0 = OpConstant %int 0
%_ptr_Input_v4half = OpTypePointer Input %v4half
%int_2 = OpConstant %int 2
%int_1 = OpConstant %int 1
%main = OpFunction %void None %3
%5 = OpLabel
%19 = OpAccessChain %_ptr_Input_v4half %iVert %int_0
%20 = OpLoad %v4half %19
%23 = OpAccessChain %_ptr_Input_v4half %iVert %int_2 %int_1
%24 = OpLoad %v4half %23
%25 = OpFAdd %v4half %20 %24
OpStore %oFragColor %25
OpReturn
OpFunctionEnd
)";
SetTargetEnv(SPV_ENV_VULKAN_1_3);
SetAssembleOptions(SPV_TEXT_TO_BINARY_OPTION_PRESERVE_NUMERIC_IDS);
std::unordered_set<uint32_t> live_inputs;
std::unordered_set<uint32_t> live_builtins;
auto result = SinglePassRunToBinary<AnalyzeLiveInputPass>(
text, true, &live_inputs, &live_builtins);
auto itr0 = live_inputs.find(0);
auto itr1 = live_inputs.find(1);
auto itr2 = live_inputs.find(2);
auto itr3 = live_inputs.find(3);
auto itr4 = live_inputs.find(4);
auto itr5 = live_inputs.find(5);
auto itr6 = live_inputs.find(6);
// Expect live_inputs == {2, 5}
EXPECT_TRUE(itr0 == live_inputs.end());
EXPECT_TRUE(itr1 == live_inputs.end());
EXPECT_TRUE(itr2 != live_inputs.end());
EXPECT_TRUE(itr3 == live_inputs.end());
EXPECT_TRUE(itr4 == live_inputs.end());
EXPECT_TRUE(itr5 != live_inputs.end());
EXPECT_TRUE(itr6 == live_inputs.end());
}
} // namespace
} // namespace opt
} // namespace spvtools
|