feature_manager.cpp 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  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 "source/opt/feature_manager.h"
  15. #include <queue>
  16. #include <stack>
  17. #include <string>
  18. #include "source/enum_string_mapping.h"
  19. namespace spvtools {
  20. namespace opt {
  21. void FeatureManager::Analyze(Module* module) {
  22. AddExtensions(module);
  23. AddCapabilities(module);
  24. AddExtInstImportIds(module);
  25. }
  26. void FeatureManager::AddExtensions(Module* module) {
  27. for (auto ext : module->extensions()) {
  28. const std::string name =
  29. reinterpret_cast<const char*>(ext.GetInOperand(0u).words.data());
  30. Extension extension;
  31. if (GetExtensionFromString(name.c_str(), &extension)) {
  32. extensions_.Add(extension);
  33. }
  34. }
  35. }
  36. void FeatureManager::AddCapability(SpvCapability cap) {
  37. if (capabilities_.Contains(cap)) return;
  38. capabilities_.Add(cap);
  39. spv_operand_desc desc = {};
  40. if (SPV_SUCCESS ==
  41. grammar_.lookupOperand(SPV_OPERAND_TYPE_CAPABILITY, cap, &desc)) {
  42. CapabilitySet(desc->numCapabilities, desc->capabilities)
  43. .ForEach([this](SpvCapability c) { AddCapability(c); });
  44. }
  45. }
  46. void FeatureManager::AddCapabilities(Module* module) {
  47. for (Instruction& inst : module->capabilities()) {
  48. AddCapability(static_cast<SpvCapability>(inst.GetSingleWordInOperand(0)));
  49. }
  50. }
  51. void FeatureManager::AddExtInstImportIds(Module* module) {
  52. extinst_importid_GLSLstd450_ = module->GetExtInstImportId("GLSL.std.450");
  53. }
  54. } // namespace opt
  55. } // namespace spvtools