uniform_buffer_element_descriptor.cpp 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. // Copyright (c) 2019 Google LLC
  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/fuzz/uniform_buffer_element_descriptor.h"
  15. #include <algorithm>
  16. namespace spvtools {
  17. namespace fuzz {
  18. protobufs::UniformBufferElementDescriptor MakeUniformBufferElementDescriptor(
  19. uint32_t descriptor_set, uint32_t binding,
  20. std::vector<uint32_t>&& indices) {
  21. protobufs::UniformBufferElementDescriptor result;
  22. result.set_descriptor_set(descriptor_set);
  23. result.set_binding(binding);
  24. for (auto index : indices) {
  25. result.add_index(index);
  26. }
  27. return result;
  28. }
  29. bool UniformBufferElementDescriptorEquals::operator()(
  30. const protobufs::UniformBufferElementDescriptor* first,
  31. const protobufs::UniformBufferElementDescriptor* second) const {
  32. return first->descriptor_set() == second->descriptor_set() &&
  33. first->binding() == second->binding() &&
  34. first->index().size() == second->index().size() &&
  35. std::equal(first->index().begin(), first->index().end(),
  36. second->index().begin());
  37. }
  38. opt::Instruction* FindUniformVariable(
  39. const protobufs::UniformBufferElementDescriptor&
  40. uniform_buffer_element_descriptor,
  41. opt::IRContext* context, bool check_unique) {
  42. opt::Instruction* result = nullptr;
  43. for (auto& inst : context->types_values()) {
  44. // Consider all global variables with uniform storage class.
  45. if (inst.opcode() != SpvOpVariable) {
  46. continue;
  47. }
  48. if (inst.GetSingleWordInOperand(0) != SpvStorageClassUniform) {
  49. continue;
  50. }
  51. // Determine whether the variable is decorated with a descriptor set
  52. // matching that in |uniform_buffer_element|.
  53. bool descriptor_set_matches = false;
  54. context->get_decoration_mgr()->ForEachDecoration(
  55. inst.result_id(), SpvDecorationDescriptorSet,
  56. [&descriptor_set_matches, &uniform_buffer_element_descriptor](
  57. const opt::Instruction& decoration_inst) {
  58. const uint32_t kDescriptorSetOperandIndex = 2;
  59. if (decoration_inst.GetSingleWordInOperand(
  60. kDescriptorSetOperandIndex) ==
  61. uniform_buffer_element_descriptor.descriptor_set()) {
  62. descriptor_set_matches = true;
  63. }
  64. });
  65. if (!descriptor_set_matches) {
  66. // Descriptor set does not match.
  67. continue;
  68. }
  69. // Determine whether the variable is decorated with a binding matching that
  70. // in |uniform_buffer_element|.
  71. bool binding_matches = false;
  72. context->get_decoration_mgr()->ForEachDecoration(
  73. inst.result_id(), SpvDecorationBinding,
  74. [&binding_matches, &uniform_buffer_element_descriptor](
  75. const opt::Instruction& decoration_inst) {
  76. const uint32_t kBindingOperandIndex = 2;
  77. if (decoration_inst.GetSingleWordInOperand(kBindingOperandIndex) ==
  78. uniform_buffer_element_descriptor.binding()) {
  79. binding_matches = true;
  80. }
  81. });
  82. if (!binding_matches) {
  83. // Binding does not match.
  84. continue;
  85. }
  86. // This instruction is a uniform variable with the right descriptor set and
  87. // binding.
  88. if (!check_unique) {
  89. // If we aren't checking uniqueness, return it.
  90. return &inst;
  91. }
  92. if (result) {
  93. // More than one uniform variable is decorated with the given descriptor
  94. // set and binding. This means the fact is ambiguous.
  95. return nullptr;
  96. }
  97. result = &inst;
  98. }
  99. // We get here either if no match was found, or if |check_unique| holds and
  100. // exactly one match was found.
  101. assert(result == nullptr || check_unique);
  102. return result;
  103. }
  104. } // namespace fuzz
  105. } // namespace spvtools