feature_manager_test.cpp 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. // Copyright (c) 2017 Google Inc.
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. #include <algorithm>
  15. #include <memory>
  16. #include <string>
  17. #include "gmock/gmock.h"
  18. #include "gtest/gtest.h"
  19. #include "source/opt/build_module.h"
  20. #include "source/opt/ir_context.h"
  21. namespace spvtools {
  22. namespace opt {
  23. namespace {
  24. using FeatureManagerTest = ::testing::Test;
  25. TEST_F(FeatureManagerTest, MissingExtension) {
  26. const std::string text = R"(
  27. OpCapability Shader
  28. OpMemoryModel Logical GLSL450
  29. )";
  30. std::unique_ptr<IRContext> context =
  31. BuildModule(SPV_ENV_UNIVERSAL_1_2, nullptr, text);
  32. ASSERT_NE(context, nullptr);
  33. EXPECT_FALSE(context->get_feature_mgr()->HasExtension(
  34. Extension::kSPV_KHR_variable_pointers));
  35. }
  36. TEST_F(FeatureManagerTest, OneExtension) {
  37. const std::string text = R"(
  38. OpCapability Shader
  39. OpMemoryModel Logical GLSL450
  40. OpExtension "SPV_KHR_variable_pointers"
  41. )";
  42. std::unique_ptr<IRContext> context =
  43. BuildModule(SPV_ENV_UNIVERSAL_1_2, nullptr, text);
  44. ASSERT_NE(context, nullptr);
  45. EXPECT_TRUE(context->get_feature_mgr()->HasExtension(
  46. Extension::kSPV_KHR_variable_pointers));
  47. }
  48. TEST_F(FeatureManagerTest, NotADifferentExtension) {
  49. const std::string text = R"(
  50. OpCapability Shader
  51. OpMemoryModel Logical GLSL450
  52. OpExtension "SPV_KHR_variable_pointers"
  53. )";
  54. std::unique_ptr<IRContext> context =
  55. BuildModule(SPV_ENV_UNIVERSAL_1_2, nullptr, text);
  56. ASSERT_NE(context, nullptr);
  57. EXPECT_FALSE(context->get_feature_mgr()->HasExtension(
  58. Extension::kSPV_KHR_storage_buffer_storage_class));
  59. }
  60. TEST_F(FeatureManagerTest, TwoExtensions) {
  61. const std::string text = R"(
  62. OpCapability Shader
  63. OpMemoryModel Logical GLSL450
  64. OpExtension "SPV_KHR_variable_pointers"
  65. OpExtension "SPV_KHR_storage_buffer_storage_class"
  66. )";
  67. std::unique_ptr<IRContext> context =
  68. BuildModule(SPV_ENV_UNIVERSAL_1_2, nullptr, text);
  69. ASSERT_NE(context, nullptr);
  70. EXPECT_TRUE(context->get_feature_mgr()->HasExtension(
  71. Extension::kSPV_KHR_variable_pointers));
  72. EXPECT_TRUE(context->get_feature_mgr()->HasExtension(
  73. Extension::kSPV_KHR_storage_buffer_storage_class));
  74. }
  75. // Test capability checks.
  76. TEST_F(FeatureManagerTest, ExplicitlyPresent1) {
  77. const std::string text = R"(
  78. OpCapability Shader
  79. OpMemoryModel Logical GLSL450
  80. )";
  81. std::unique_ptr<IRContext> context =
  82. BuildModule(SPV_ENV_UNIVERSAL_1_2, nullptr, text);
  83. ASSERT_NE(context, nullptr);
  84. EXPECT_TRUE(context->get_feature_mgr()->HasCapability(SpvCapabilityShader));
  85. EXPECT_FALSE(context->get_feature_mgr()->HasCapability(SpvCapabilityKernel));
  86. }
  87. TEST_F(FeatureManagerTest, ExplicitlyPresent2) {
  88. const std::string text = R"(
  89. OpCapability Kernel
  90. OpMemoryModel Logical GLSL450
  91. )";
  92. std::unique_ptr<IRContext> context =
  93. BuildModule(SPV_ENV_UNIVERSAL_1_2, nullptr, text);
  94. ASSERT_NE(context, nullptr);
  95. EXPECT_FALSE(context->get_feature_mgr()->HasCapability(SpvCapabilityShader));
  96. EXPECT_TRUE(context->get_feature_mgr()->HasCapability(SpvCapabilityKernel));
  97. }
  98. TEST_F(FeatureManagerTest, ImplicitlyPresent) {
  99. const std::string text = R"(
  100. OpCapability Tessellation
  101. OpMemoryModel Logical GLSL450
  102. )";
  103. std::unique_ptr<IRContext> context =
  104. BuildModule(SPV_ENV_UNIVERSAL_1_2, nullptr, text);
  105. ASSERT_NE(context, nullptr);
  106. // Check multiple levels of indirection. Tessellation implies Shader, which
  107. // implies Matrix.
  108. EXPECT_TRUE(
  109. context->get_feature_mgr()->HasCapability(SpvCapabilityTessellation));
  110. EXPECT_TRUE(context->get_feature_mgr()->HasCapability(SpvCapabilityShader));
  111. EXPECT_TRUE(context->get_feature_mgr()->HasCapability(SpvCapabilityMatrix));
  112. EXPECT_FALSE(context->get_feature_mgr()->HasCapability(SpvCapabilityKernel));
  113. }
  114. } // namespace
  115. } // namespace opt
  116. } // namespace spvtools