feature_manager_test.cpp 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  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 "gtest/gtest.h"
  17. #include "source/opt/build_module.h"
  18. #include "source/opt/ir_context.h"
  19. namespace spvtools {
  20. namespace opt {
  21. namespace {
  22. using FeatureManagerTest = ::testing::Test;
  23. TEST_F(FeatureManagerTest, MissingExtension) {
  24. const std::string text = R"(
  25. OpCapability Shader
  26. OpMemoryModel Logical GLSL450
  27. )";
  28. std::unique_ptr<IRContext> context =
  29. BuildModule(SPV_ENV_UNIVERSAL_1_2, nullptr, text);
  30. ASSERT_NE(context, nullptr);
  31. EXPECT_FALSE(context->get_feature_mgr()->HasExtension(
  32. Extension::kSPV_KHR_variable_pointers));
  33. }
  34. TEST_F(FeatureManagerTest, OneExtension) {
  35. const std::string text = R"(
  36. OpCapability Shader
  37. OpMemoryModel Logical GLSL450
  38. OpExtension "SPV_KHR_variable_pointers"
  39. )";
  40. std::unique_ptr<IRContext> context =
  41. BuildModule(SPV_ENV_UNIVERSAL_1_2, nullptr, text);
  42. ASSERT_NE(context, nullptr);
  43. EXPECT_TRUE(context->get_feature_mgr()->HasExtension(
  44. Extension::kSPV_KHR_variable_pointers));
  45. }
  46. TEST_F(FeatureManagerTest, NotADifferentExtension) {
  47. const std::string text = R"(
  48. OpCapability Shader
  49. OpMemoryModel Logical GLSL450
  50. OpExtension "SPV_KHR_variable_pointers"
  51. )";
  52. std::unique_ptr<IRContext> context =
  53. BuildModule(SPV_ENV_UNIVERSAL_1_2, nullptr, text);
  54. ASSERT_NE(context, nullptr);
  55. EXPECT_FALSE(context->get_feature_mgr()->HasExtension(
  56. Extension::kSPV_KHR_storage_buffer_storage_class));
  57. }
  58. TEST_F(FeatureManagerTest, TwoExtensions) {
  59. const std::string text = R"(
  60. OpCapability Shader
  61. OpMemoryModel Logical GLSL450
  62. OpExtension "SPV_KHR_variable_pointers"
  63. OpExtension "SPV_KHR_storage_buffer_storage_class"
  64. )";
  65. std::unique_ptr<IRContext> context =
  66. BuildModule(SPV_ENV_UNIVERSAL_1_2, nullptr, text);
  67. ASSERT_NE(context, nullptr);
  68. EXPECT_TRUE(context->get_feature_mgr()->HasExtension(
  69. Extension::kSPV_KHR_variable_pointers));
  70. EXPECT_TRUE(context->get_feature_mgr()->HasExtension(
  71. Extension::kSPV_KHR_storage_buffer_storage_class));
  72. }
  73. TEST_F(FeatureManagerTest, GetExtensionsReturnsExtensions) {
  74. const std::string text = R"(
  75. OpCapability Shader
  76. OpMemoryModel Logical GLSL450
  77. OpExtension "SPV_KHR_variable_pointers"
  78. OpExtension "SPV_KHR_storage_buffer_storage_class"
  79. )";
  80. std::unique_ptr<IRContext> context =
  81. BuildModule(SPV_ENV_UNIVERSAL_1_2, nullptr, text);
  82. ASSERT_NE(context, nullptr);
  83. const auto& extensions = context->get_feature_mgr()->GetExtensions();
  84. EXPECT_EQ(extensions.size(), 2);
  85. EXPECT_TRUE(extensions.contains(Extension::kSPV_KHR_variable_pointers));
  86. EXPECT_TRUE(
  87. extensions.contains(Extension::kSPV_KHR_storage_buffer_storage_class));
  88. }
  89. // Test capability checks.
  90. TEST_F(FeatureManagerTest, ExplicitlyPresent1) {
  91. const std::string text = R"(
  92. OpCapability Shader
  93. OpMemoryModel Logical GLSL450
  94. )";
  95. std::unique_ptr<IRContext> context =
  96. BuildModule(SPV_ENV_UNIVERSAL_1_2, nullptr, text);
  97. ASSERT_NE(context, nullptr);
  98. EXPECT_TRUE(
  99. context->get_feature_mgr()->HasCapability(spv::Capability::Shader));
  100. EXPECT_FALSE(
  101. context->get_feature_mgr()->HasCapability(spv::Capability::Kernel));
  102. }
  103. TEST_F(FeatureManagerTest, ExplicitlyPresent2) {
  104. const std::string text = R"(
  105. OpCapability Kernel
  106. OpMemoryModel Logical GLSL450
  107. )";
  108. std::unique_ptr<IRContext> context =
  109. BuildModule(SPV_ENV_UNIVERSAL_1_2, nullptr, text);
  110. ASSERT_NE(context, nullptr);
  111. EXPECT_FALSE(
  112. context->get_feature_mgr()->HasCapability(spv::Capability::Shader));
  113. EXPECT_TRUE(
  114. context->get_feature_mgr()->HasCapability(spv::Capability::Kernel));
  115. }
  116. TEST_F(FeatureManagerTest, ImplicitlyPresent) {
  117. const std::string text = R"(
  118. OpCapability Tessellation
  119. OpMemoryModel Logical GLSL450
  120. )";
  121. std::unique_ptr<IRContext> context =
  122. BuildModule(SPV_ENV_UNIVERSAL_1_2, nullptr, text);
  123. ASSERT_NE(context, nullptr);
  124. // Check multiple levels of indirection. Tessellation implies Shader, which
  125. // implies Matrix.
  126. EXPECT_TRUE(
  127. context->get_feature_mgr()->HasCapability(spv::Capability::Tessellation));
  128. EXPECT_TRUE(
  129. context->get_feature_mgr()->HasCapability(spv::Capability::Shader));
  130. EXPECT_TRUE(
  131. context->get_feature_mgr()->HasCapability(spv::Capability::Matrix));
  132. EXPECT_FALSE(
  133. context->get_feature_mgr()->HasCapability(spv::Capability::Kernel));
  134. }
  135. TEST_F(FeatureManagerTest, GetCapabilitiesReturnsImplicitCapabilities) {
  136. const std::string text = R"(
  137. OpCapability Tessellation
  138. OpMemoryModel Logical GLSL450
  139. )";
  140. std::unique_ptr<IRContext> context =
  141. BuildModule(SPV_ENV_UNIVERSAL_1_2, nullptr, text);
  142. ASSERT_NE(context, nullptr);
  143. const auto& capabilities = context->get_feature_mgr()->GetCapabilities();
  144. // Tesselation implies Shader, which implies Matrix.
  145. EXPECT_EQ(capabilities.size(), 3);
  146. EXPECT_TRUE(capabilities.contains(spv::Capability::Tessellation));
  147. EXPECT_TRUE(capabilities.contains(spv::Capability::Shader));
  148. EXPECT_TRUE(capabilities.contains(spv::Capability::Matrix));
  149. }
  150. } // namespace
  151. } // namespace opt
  152. } // namespace spvtools