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