interp_fixup_test.cpp 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. // Copyright (c) 2021 The Khronos Group Inc.
  2. // Copyright (c) 2021 Valve Corporation
  3. // Copyright (c) 2021 LunarG Inc.
  4. //
  5. // Licensed under the Apache License, Version 2.0 (the "License");
  6. // you may not use this file except in compliance with the License.
  7. // You may obtain a copy of the License at
  8. //
  9. // http://www.apache.org/licenses/LICENSE-2.0
  10. //
  11. // Unless required by applicable law or agreed to in writing, software
  12. // distributed under the License is distributed on an "AS IS" BASIS,
  13. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. // See the License for the specific language governing permissions and
  15. // limitations under the License.
  16. #include <vector>
  17. #include "gmock/gmock.h"
  18. #include "test/opt/pass_fixture.h"
  19. #include "test/opt/pass_utils.h"
  20. namespace spvtools {
  21. namespace opt {
  22. namespace {
  23. using InterpFixupTest = PassTest<::testing::Test>;
  24. using ::testing::HasSubstr;
  25. TEST_F(InterpFixupTest, FixInterpAtSample) {
  26. const std::string text = R"(
  27. OpCapability Shader
  28. OpCapability InterpolationFunction
  29. %1 = OpExtInstImport "GLSL.std.450"
  30. OpMemoryModel Logical GLSL450
  31. OpEntryPoint Fragment %MainPs "MainPs" %i_vPositionOs %_entryPointOutput
  32. OpExecutionMode %MainPs OriginUpperLeft
  33. OpSource HLSL 500
  34. OpName %MainPs "MainPs"
  35. OpName %i_vPositionOs "i.vPositionOs"
  36. OpName %_entryPointOutput "@entryPointOutput"
  37. OpDecorate %i_vPositionOs Location 0
  38. OpDecorate %_entryPointOutput Location 0
  39. %void = OpTypeVoid
  40. %6 = OpTypeFunction %void
  41. %float = OpTypeFloat 32
  42. %v4float = OpTypeVector %float 4
  43. %float_0 = OpConstant %float 0
  44. %10 = OpConstantComposite %v4float %float_0 %float_0 %float_0 %float_0
  45. %uint = OpTypeInt 32 0
  46. %uint_0 = OpConstant %uint 0
  47. %uint_4 = OpConstant %uint 4
  48. %bool = OpTypeBool
  49. %int = OpTypeInt 32 1
  50. %int_1 = OpConstant %int 1
  51. %_ptr_Input_v4float = OpTypePointer Input %v4float
  52. %i_vPositionOs = OpVariable %_ptr_Input_v4float Input
  53. %_ptr_Output_v4float = OpTypePointer Output %v4float
  54. %_entryPointOutput = OpVariable %_ptr_Output_v4float Output
  55. %MainPs = OpFunction %void None %6
  56. %19 = OpLabel
  57. %20 = OpLoad %v4float %i_vPositionOs
  58. OpBranch %21
  59. %21 = OpLabel
  60. %22 = OpPhi %v4float %10 %19 %23 %24
  61. %25 = OpPhi %uint %uint_0 %19 %26 %24
  62. %27 = OpULessThan %bool %25 %uint_4
  63. OpLoopMerge %28 %24 None
  64. OpBranchConditional %27 %24 %28
  65. %24 = OpLabel
  66. %29 = OpExtInst %v4float %1 InterpolateAtSample %20 %25
  67. ;CHECK: %29 = OpExtInst %v4float %1 InterpolateAtSample %i_vPositionOs %25
  68. %30 = OpCompositeExtract %float %29 0
  69. %31 = OpCompositeExtract %float %22 0
  70. %32 = OpFAdd %float %31 %30
  71. %23 = OpCompositeInsert %v4float %32 %22 0
  72. %26 = OpIAdd %uint %25 %int_1
  73. OpBranch %21
  74. %28 = OpLabel
  75. OpStore %_entryPointOutput %22
  76. OpReturn
  77. OpFunctionEnd
  78. )";
  79. SinglePassRunAndMatch<InterpFixupPass>(text, false);
  80. }
  81. TEST_F(InterpFixupTest, FixInterpAtCentroid) {
  82. const std::string text = R"(
  83. OpCapability Shader
  84. OpCapability InterpolationFunction
  85. %1 = OpExtInstImport "GLSL.std.450"
  86. OpMemoryModel Logical GLSL450
  87. OpEntryPoint Fragment %MainPs "MainPs" %i_vPositionOs %_entryPointOutput
  88. OpExecutionMode %MainPs OriginUpperLeft
  89. OpSource HLSL 500
  90. OpName %MainPs "MainPs"
  91. OpName %i_vPositionOs "i.vPositionOs"
  92. OpName %_entryPointOutput "@entryPointOutput"
  93. OpDecorate %i_vPositionOs Location 0
  94. OpDecorate %_entryPointOutput Location 0
  95. %void = OpTypeVoid
  96. %6 = OpTypeFunction %void
  97. %float = OpTypeFloat 32
  98. %v4float = OpTypeVector %float 4
  99. %float_0 = OpConstant %float 0
  100. %10 = OpConstantComposite %v4float %float_0 %float_0 %float_0 %float_0
  101. %_ptr_Input_v4float = OpTypePointer Input %v4float
  102. %i_vPositionOs = OpVariable %_ptr_Input_v4float Input
  103. %_ptr_Output_v4float = OpTypePointer Output %v4float
  104. %_entryPointOutput = OpVariable %_ptr_Output_v4float Output
  105. %MainPs = OpFunction %void None %6
  106. %13 = OpLabel
  107. %14 = OpLoad %v4float %i_vPositionOs
  108. %15 = OpExtInst %v4float %1 InterpolateAtCentroid %14
  109. ;CHECK: %15 = OpExtInst %v4float %1 InterpolateAtCentroid %i_vPositionOs
  110. %16 = OpCompositeExtract %float %15 0
  111. %17 = OpCompositeInsert %v4float %16 %10 0
  112. OpStore %_entryPointOutput %17
  113. OpReturn
  114. OpFunctionEnd
  115. )";
  116. SinglePassRunAndMatch<InterpFixupPass>(text, false);
  117. }
  118. TEST_F(InterpFixupTest, FixInterpAtOffset) {
  119. const std::string text = R"(
  120. OpCapability Shader
  121. OpCapability InterpolationFunction
  122. %1 = OpExtInstImport "GLSL.std.450"
  123. OpMemoryModel Logical GLSL450
  124. OpEntryPoint Fragment %MainPs "MainPs" %i_vPositionOs %_entryPointOutput
  125. OpExecutionMode %MainPs OriginUpperLeft
  126. OpSource HLSL 500
  127. OpName %MainPs "MainPs"
  128. OpName %i_vPositionOs "i.vPositionOs"
  129. OpName %_entryPointOutput "@entryPointOutput"
  130. OpDecorate %i_vPositionOs Location 0
  131. OpDecorate %_entryPointOutput Location 0
  132. %void = OpTypeVoid
  133. %6 = OpTypeFunction %void
  134. %float = OpTypeFloat 32
  135. %v4float = OpTypeVector %float 4
  136. %float_0 = OpConstant %float 0
  137. %10 = OpConstantComposite %v4float %float_0 %float_0 %float_0 %float_0
  138. %v2float = OpTypeVector %float 2
  139. %float_0_0625 = OpConstant %float 0.0625
  140. %13 = OpConstantComposite %v2float %float_0_0625 %float_0_0625
  141. %_ptr_Input_v4float = OpTypePointer Input %v4float
  142. %i_vPositionOs = OpVariable %_ptr_Input_v4float Input
  143. %_ptr_Output_v4float = OpTypePointer Output %v4float
  144. %_entryPointOutput = OpVariable %_ptr_Output_v4float Output
  145. %MainPs = OpFunction %void None %6
  146. %16 = OpLabel
  147. %17 = OpLoad %v4float %i_vPositionOs
  148. %18 = OpExtInst %v4float %1 InterpolateAtOffset %17 %13
  149. ;CHECK: %18 = OpExtInst %v4float %1 InterpolateAtOffset %i_vPositionOs %13
  150. %19 = OpCompositeExtract %float %18 0
  151. %20 = OpCompositeInsert %v4float %19 %10 0
  152. OpStore %_entryPointOutput %20
  153. OpReturn
  154. OpFunctionEnd
  155. )";
  156. SinglePassRunAndMatch<InterpFixupPass>(text, false);
  157. }
  158. } // namespace
  159. } // namespace opt
  160. } // namespace spvtools