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