relax_float_ops_test.cpp 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  1. // Copyright (c) 2019 Valve Corporation
  2. // Copyright (c) 2019 LunarG Inc.
  3. //
  4. // Licensed under the Apache License, Version 2.0 (the "License");
  5. // you may not use this file except in compliance with the License.
  6. // You may obtain a copy of the License at
  7. //
  8. // http://www.apache.org/licenses/LICENSE-2.0
  9. //
  10. // Unless required by applicable law or agreed to in writing, software
  11. // distributed under the License is distributed on an "AS IS" BASIS,
  12. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. // See the License for the specific language governing permissions and
  14. // limitations under the License.
  15. // Relax float ops tests
  16. #include <string>
  17. #include <vector>
  18. #include "test/opt/pass_fixture.h"
  19. #include "test/opt/pass_utils.h"
  20. namespace spvtools {
  21. namespace opt {
  22. namespace {
  23. using RelaxFloatOpsTest = PassTest<::testing::Test>;
  24. TEST_F(RelaxFloatOpsTest, RelaxFloatOpsBasic) {
  25. // All float result instructions in functions should be relaxed
  26. // clang-format off
  27. //
  28. // SamplerState g_sSamp : register(s0);
  29. // uniform Texture1D <float4> g_tTex1df4 : register(t0);
  30. //
  31. // struct PS_INPUT
  32. // {
  33. // float Tex0 : TEXCOORD0;
  34. // float Tex1 : TEXCOORD1;
  35. // };
  36. //
  37. // struct PS_OUTPUT
  38. // {
  39. // float4 Color : SV_Target0;
  40. // };
  41. //
  42. // PS_OUTPUT main(PS_INPUT i)
  43. // {
  44. // PS_OUTPUT psout;
  45. // float4 txval10 = g_tTex1df4.Sample(g_sSamp, i.Tex0);
  46. // float4 txval11 = g_tTex1df4.Sample(g_sSamp, i.Tex1);
  47. // float4 t = txval10 + txval11;
  48. // float4 t2 = t / 2.0;
  49. // psout.Color = t2;
  50. // return psout;
  51. // }
  52. // clang-format on
  53. const std::string defs0 =
  54. R"(OpCapability Shader
  55. OpCapability Sampled1D
  56. %1 = OpExtInstImport "GLSL.std.450"
  57. OpMemoryModel Logical GLSL450
  58. OpEntryPoint Fragment %main "main" %i_Tex0 %i_Tex1 %_entryPointOutput_Color
  59. OpExecutionMode %main OriginUpperLeft
  60. OpSource HLSL 500
  61. OpName %main "main"
  62. OpName %g_tTex1df4 "g_tTex1df4"
  63. OpName %g_sSamp "g_sSamp"
  64. OpName %i_Tex0 "i.Tex0"
  65. OpName %i_Tex1 "i.Tex1"
  66. OpName %_entryPointOutput_Color "@entryPointOutput.Color"
  67. OpDecorate %g_tTex1df4 DescriptorSet 0
  68. OpDecorate %g_tTex1df4 Binding 0
  69. OpDecorate %g_sSamp DescriptorSet 0
  70. OpDecorate %g_sSamp Binding 0
  71. OpDecorate %i_Tex0 Location 0
  72. OpDecorate %i_Tex1 Location 1
  73. OpDecorate %_entryPointOutput_Color Location 0
  74. )";
  75. const std::string defs1 =
  76. R"(%void = OpTypeVoid
  77. %3 = OpTypeFunction %void
  78. %float = OpTypeFloat 32
  79. %v4float = OpTypeVector %float 4
  80. %17 = OpTypeImage %float 1D 0 0 0 1 Unknown
  81. %_ptr_UniformConstant_17 = OpTypePointer UniformConstant %17
  82. %g_tTex1df4 = OpVariable %_ptr_UniformConstant_17 UniformConstant
  83. %21 = OpTypeSampler
  84. %_ptr_UniformConstant_21 = OpTypePointer UniformConstant %21
  85. %g_sSamp = OpVariable %_ptr_UniformConstant_21 UniformConstant
  86. %25 = OpTypeSampledImage %17
  87. %_ptr_Input_float = OpTypePointer Input %float
  88. %i_Tex0 = OpVariable %_ptr_Input_float Input
  89. %i_Tex1 = OpVariable %_ptr_Input_float Input
  90. %_ptr_Output_v4float = OpTypePointer Output %v4float
  91. %_entryPointOutput_Color = OpVariable %_ptr_Output_v4float Output
  92. %float_0_5 = OpConstant %float 0.5
  93. %116 = OpConstantComposite %v4float %float_0_5 %float_0_5 %float_0_5 %float_0_5
  94. )";
  95. const std::string relax_decos =
  96. R"(OpDecorate %60 RelaxedPrecision
  97. OpDecorate %63 RelaxedPrecision
  98. OpDecorate %82 RelaxedPrecision
  99. OpDecorate %88 RelaxedPrecision
  100. OpDecorate %91 RelaxedPrecision
  101. OpDecorate %94 RelaxedPrecision
  102. )";
  103. const std::string func_orig =
  104. R"(%main = OpFunction %void None %3
  105. %5 = OpLabel
  106. %60 = OpLoad %float %i_Tex0
  107. %63 = OpLoad %float %i_Tex1
  108. %77 = OpLoad %17 %g_tTex1df4
  109. %78 = OpLoad %21 %g_sSamp
  110. %79 = OpSampledImage %25 %77 %78
  111. %82 = OpImageSampleImplicitLod %v4float %79 %60
  112. %83 = OpLoad %17 %g_tTex1df4
  113. %84 = OpLoad %21 %g_sSamp
  114. %85 = OpSampledImage %25 %83 %84
  115. %88 = OpImageSampleImplicitLod %v4float %85 %63
  116. %91 = OpFAdd %v4float %82 %88
  117. %94 = OpFMul %v4float %91 %116
  118. OpStore %_entryPointOutput_Color %94
  119. OpReturn
  120. OpFunctionEnd
  121. )";
  122. SetAssembleOptions(SPV_TEXT_TO_BINARY_OPTION_PRESERVE_NUMERIC_IDS);
  123. SinglePassRunAndCheck<RelaxFloatOpsPass>(
  124. defs0 + defs1 + func_orig, defs0 + relax_decos + defs1 + func_orig, true,
  125. true);
  126. }
  127. TEST_F(RelaxFloatOpsTest, RelaxFloatOpsForLinkage) {
  128. const std::string defs0 =
  129. R"(OpCapability Shader
  130. OpCapability Linkage
  131. OpCapability Sampled1D
  132. %1 = OpExtInstImport "GLSL.std.450"
  133. OpMemoryModel Logical GLSL450
  134. OpSource HLSL 630
  135. OpName %main "main"
  136. OpName %g_tTex1df4 "g_tTex1df4"
  137. OpName %g_sSamp "g_sSamp"
  138. OpName %i_Tex0 "i.Tex0"
  139. OpName %i_Tex1 "i.Tex1"
  140. OpName %_entryPointOutput_Color "@entryPointOutput.Color"
  141. OpDecorate %main LinkageAttributes "main" Export
  142. OpDecorate %g_tTex1df4 DescriptorSet 0
  143. OpDecorate %g_tTex1df4 Binding 0
  144. OpDecorate %g_sSamp DescriptorSet 0
  145. OpDecorate %g_sSamp Binding 0
  146. OpDecorate %i_Tex0 Location 0
  147. OpDecorate %i_Tex1 Location 1
  148. OpDecorate %_entryPointOutput_Color Location 0
  149. )";
  150. const std::string defs1 =
  151. R"(%void = OpTypeVoid
  152. %3 = OpTypeFunction %void
  153. %float = OpTypeFloat 32
  154. %v4float = OpTypeVector %float 4
  155. %17 = OpTypeImage %float 1D 0 0 0 1 Unknown
  156. %_ptr_UniformConstant_17 = OpTypePointer UniformConstant %17
  157. %g_tTex1df4 = OpVariable %_ptr_UniformConstant_17 UniformConstant
  158. %21 = OpTypeSampler
  159. %_ptr_UniformConstant_21 = OpTypePointer UniformConstant %21
  160. %g_sSamp = OpVariable %_ptr_UniformConstant_21 UniformConstant
  161. %25 = OpTypeSampledImage %17
  162. %_ptr_Input_float = OpTypePointer Input %float
  163. %i_Tex0 = OpVariable %_ptr_Input_float Input
  164. %i_Tex1 = OpVariable %_ptr_Input_float Input
  165. %_ptr_Output_v4float = OpTypePointer Output %v4float
  166. %_entryPointOutput_Color = OpVariable %_ptr_Output_v4float Output
  167. %float_0_5 = OpConstant %float 0.5
  168. %116 = OpConstantComposite %v4float %float_0_5 %float_0_5 %float_0_5 %float_0_5
  169. )";
  170. const std::string relax_decos =
  171. R"(OpDecorate %60 RelaxedPrecision
  172. OpDecorate %63 RelaxedPrecision
  173. OpDecorate %82 RelaxedPrecision
  174. OpDecorate %88 RelaxedPrecision
  175. OpDecorate %91 RelaxedPrecision
  176. OpDecorate %94 RelaxedPrecision
  177. )";
  178. const std::string func_orig =
  179. R"(%main = OpFunction %void None %3
  180. %5 = OpLabel
  181. %60 = OpLoad %float %i_Tex0
  182. %63 = OpLoad %float %i_Tex1
  183. %77 = OpLoad %17 %g_tTex1df4
  184. %78 = OpLoad %21 %g_sSamp
  185. %79 = OpSampledImage %25 %77 %78
  186. %82 = OpImageSampleImplicitLod %v4float %79 %60
  187. %83 = OpLoad %17 %g_tTex1df4
  188. %84 = OpLoad %21 %g_sSamp
  189. %85 = OpSampledImage %25 %83 %84
  190. %88 = OpImageSampleImplicitLod %v4float %85 %63
  191. %91 = OpFAdd %v4float %82 %88
  192. %94 = OpFMul %v4float %91 %116
  193. OpStore %_entryPointOutput_Color %94
  194. OpReturn
  195. OpFunctionEnd
  196. )";
  197. SetAssembleOptions(SPV_TEXT_TO_BINARY_OPTION_PRESERVE_NUMERIC_IDS);
  198. SinglePassRunAndCheck<RelaxFloatOpsPass>(
  199. defs0 + defs1 + func_orig, defs0 + relax_decos + defs1 + func_orig, true,
  200. true);
  201. }
  202. } // namespace
  203. } // namespace opt
  204. } // namespace spvtools