relax_float_ops_test.cpp 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  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/assembly_builder.h"
  19. #include "test/opt/pass_fixture.h"
  20. #include "test/opt/pass_utils.h"
  21. namespace spvtools {
  22. namespace opt {
  23. namespace {
  24. using RelaxFloatOpsTest = PassTest<::testing::Test>;
  25. TEST_F(RelaxFloatOpsTest, RelaxFloatOpsBasic) {
  26. // All float result instructions in functions should be relaxed
  27. // clang-format off
  28. //
  29. // SamplerState g_sSamp : register(s0);
  30. // uniform Texture1D <float4> g_tTex1df4 : register(t0);
  31. //
  32. // struct PS_INPUT
  33. // {
  34. // float Tex0 : TEXCOORD0;
  35. // float Tex1 : TEXCOORD1;
  36. // };
  37. //
  38. // struct PS_OUTPUT
  39. // {
  40. // float4 Color : SV_Target0;
  41. // };
  42. //
  43. // PS_OUTPUT main(PS_INPUT i)
  44. // {
  45. // PS_OUTPUT psout;
  46. // float4 txval10 = g_tTex1df4.Sample(g_sSamp, i.Tex0);
  47. // float4 txval11 = g_tTex1df4.Sample(g_sSamp, i.Tex1);
  48. // float4 t = txval10 + txval11;
  49. // float4 t2 = t / 2.0;
  50. // psout.Color = t2;
  51. // return psout;
  52. // }
  53. // clang-format on
  54. const std::string defs0 =
  55. R"(OpCapability Shader
  56. OpCapability Sampled1D
  57. %1 = OpExtInstImport "GLSL.std.450"
  58. OpMemoryModel Logical GLSL450
  59. OpEntryPoint Fragment %main "main" %i_Tex0 %i_Tex1 %_entryPointOutput_Color
  60. OpExecutionMode %main OriginUpperLeft
  61. OpSource HLSL 500
  62. OpName %main "main"
  63. OpName %g_tTex1df4 "g_tTex1df4"
  64. OpName %g_sSamp "g_sSamp"
  65. OpName %i_Tex0 "i.Tex0"
  66. OpName %i_Tex1 "i.Tex1"
  67. OpName %_entryPointOutput_Color "@entryPointOutput.Color"
  68. OpDecorate %g_tTex1df4 DescriptorSet 0
  69. OpDecorate %g_tTex1df4 Binding 0
  70. OpDecorate %g_sSamp DescriptorSet 0
  71. OpDecorate %g_sSamp Binding 0
  72. OpDecorate %i_Tex0 Location 0
  73. OpDecorate %i_Tex1 Location 1
  74. OpDecorate %_entryPointOutput_Color Location 0
  75. )";
  76. const std::string defs1 =
  77. R"(%void = OpTypeVoid
  78. %3 = OpTypeFunction %void
  79. %float = OpTypeFloat 32
  80. %v4float = OpTypeVector %float 4
  81. %17 = OpTypeImage %float 1D 0 0 0 1 Unknown
  82. %_ptr_UniformConstant_17 = OpTypePointer UniformConstant %17
  83. %g_tTex1df4 = OpVariable %_ptr_UniformConstant_17 UniformConstant
  84. %21 = OpTypeSampler
  85. %_ptr_UniformConstant_21 = OpTypePointer UniformConstant %21
  86. %g_sSamp = OpVariable %_ptr_UniformConstant_21 UniformConstant
  87. %25 = OpTypeSampledImage %17
  88. %_ptr_Input_float = OpTypePointer Input %float
  89. %i_Tex0 = OpVariable %_ptr_Input_float Input
  90. %i_Tex1 = OpVariable %_ptr_Input_float Input
  91. %_ptr_Output_v4float = OpTypePointer Output %v4float
  92. %_entryPointOutput_Color = OpVariable %_ptr_Output_v4float Output
  93. %float_0_5 = OpConstant %float 0.5
  94. %116 = OpConstantComposite %v4float %float_0_5 %float_0_5 %float_0_5 %float_0_5
  95. )";
  96. const std::string relax_decos =
  97. R"(OpDecorate %60 RelaxedPrecision
  98. OpDecorate %63 RelaxedPrecision
  99. OpDecorate %82 RelaxedPrecision
  100. OpDecorate %88 RelaxedPrecision
  101. OpDecorate %91 RelaxedPrecision
  102. OpDecorate %94 RelaxedPrecision
  103. )";
  104. const std::string func_orig =
  105. R"(%main = OpFunction %void None %3
  106. %5 = OpLabel
  107. %60 = OpLoad %float %i_Tex0
  108. %63 = OpLoad %float %i_Tex1
  109. %77 = OpLoad %17 %g_tTex1df4
  110. %78 = OpLoad %21 %g_sSamp
  111. %79 = OpSampledImage %25 %77 %78
  112. %82 = OpImageSampleImplicitLod %v4float %79 %60
  113. %83 = OpLoad %17 %g_tTex1df4
  114. %84 = OpLoad %21 %g_sSamp
  115. %85 = OpSampledImage %25 %83 %84
  116. %88 = OpImageSampleImplicitLod %v4float %85 %63
  117. %91 = OpFAdd %v4float %82 %88
  118. %94 = OpFMul %v4float %91 %116
  119. OpStore %_entryPointOutput_Color %94
  120. OpReturn
  121. OpFunctionEnd
  122. )";
  123. SetAssembleOptions(SPV_TEXT_TO_BINARY_OPTION_PRESERVE_NUMERIC_IDS);
  124. SinglePassRunAndCheck<RelaxFloatOpsPass>(
  125. defs0 + defs1 + func_orig, defs0 + relax_decos + defs1 + func_orig, true,
  126. true);
  127. }
  128. } // namespace
  129. } // namespace opt
  130. } // namespace spvtools