uniform_buffer_element_descriptor.cpp 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  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() != spv::Op::OpVariable) {
  46. continue;
  47. }
  48. if (spv::StorageClass(inst.GetSingleWordInOperand(0)) !=
  49. spv::StorageClass::Uniform) {
  50. continue;
  51. }
  52. // Determine whether the variable is decorated with a descriptor set
  53. // matching that in |uniform_buffer_element|.
  54. bool descriptor_set_matches = false;
  55. context->get_decoration_mgr()->ForEachDecoration(
  56. inst.result_id(), uint32_t(spv::Decoration::DescriptorSet),
  57. [&descriptor_set_matches, &uniform_buffer_element_descriptor](
  58. const opt::Instruction& decoration_inst) {
  59. const uint32_t kDescriptorSetOperandIndex = 2;
  60. if (decoration_inst.GetSingleWordInOperand(
  61. kDescriptorSetOperandIndex) ==
  62. uniform_buffer_element_descriptor.descriptor_set()) {
  63. descriptor_set_matches = true;
  64. }
  65. });
  66. if (!descriptor_set_matches) {
  67. // Descriptor set does not match.
  68. continue;
  69. }
  70. // Determine whether the variable is decorated with a binding matching that
  71. // in |uniform_buffer_element|.
  72. bool binding_matches = false;
  73. context->get_decoration_mgr()->ForEachDecoration(
  74. inst.result_id(), uint32_t(spv::Decoration::Binding),
  75. [&binding_matches, &uniform_buffer_element_descriptor](
  76. const opt::Instruction& decoration_inst) {
  77. const uint32_t kBindingOperandIndex = 2;
  78. if (decoration_inst.GetSingleWordInOperand(kBindingOperandIndex) ==
  79. uniform_buffer_element_descriptor.binding()) {
  80. binding_matches = true;
  81. }
  82. });
  83. if (!binding_matches) {
  84. // Binding does not match.
  85. continue;
  86. }
  87. // This instruction is a uniform variable with the right descriptor set and
  88. // binding.
  89. if (!check_unique) {
  90. // If we aren't checking uniqueness, return it.
  91. return &inst;
  92. }
  93. if (result) {
  94. // More than one uniform variable is decorated with the given descriptor
  95. // set and binding. This means the fact is ambiguous.
  96. return nullptr;
  97. }
  98. result = &inst;
  99. }
  100. // We get here either if no match was found, or if |check_unique| holds and
  101. // exactly one match was found.
  102. assert(result == nullptr || check_unique);
  103. return result;
  104. }
  105. } // namespace fuzz
  106. } // namespace spvtools