Merge pull request #2439 from KhronosGroup/fix-2261
[KhronosGroup/SPIRV-Cross.git] / spirv_cross_util.cpp
blob7cff010d1c1e9ae1ad02e14eab13b2db88eef069
1 /*
2 * Copyright 2015-2021 Arm Limited
3 * SPDX-License-Identifier: Apache-2.0 OR MIT
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
9 * http://www.apache.org/licenses/LICENSE-2.0
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
19 * At your option, you may choose to accept this material under either:
20 * 1. The Apache License, Version 2.0, found at <http://www.apache.org/licenses/LICENSE-2.0>, or
21 * 2. The MIT License, found at <http://opensource.org/licenses/MIT>.
24 #include "spirv_cross_util.hpp"
25 #include "spirv_common.hpp"
27 using namespace spv;
28 using namespace SPIRV_CROSS_NAMESPACE;
30 namespace spirv_cross_util
32 void rename_interface_variable(Compiler &compiler, const SmallVector<Resource> &resources, uint32_t location,
33 const std::string &name)
35 for (auto &v : resources)
37 if (!compiler.has_decoration(v.id, spv::DecorationLocation))
38 continue;
40 auto loc = compiler.get_decoration(v.id, spv::DecorationLocation);
41 if (loc != location)
42 continue;
44 auto &type = compiler.get_type(v.base_type_id);
46 // This is more of a friendly variant. If we need to rename interface variables, we might have to rename
47 // structs as well and make sure all the names match up.
48 if (type.basetype == SPIRType::Struct)
50 compiler.set_name(v.base_type_id, join("SPIRV_Cross_Interface_Location", location));
51 for (uint32_t i = 0; i < uint32_t(type.member_types.size()); i++)
52 compiler.set_member_name(v.base_type_id, i, join("InterfaceMember", i));
55 compiler.set_name(v.id, name);
59 void inherit_combined_sampler_bindings(Compiler &compiler)
61 auto &samplers = compiler.get_combined_image_samplers();
62 for (auto &s : samplers)
64 if (compiler.has_decoration(s.image_id, spv::DecorationDescriptorSet))
66 uint32_t set = compiler.get_decoration(s.image_id, spv::DecorationDescriptorSet);
67 compiler.set_decoration(s.combined_id, spv::DecorationDescriptorSet, set);
70 if (compiler.has_decoration(s.image_id, spv::DecorationBinding))
72 uint32_t binding = compiler.get_decoration(s.image_id, spv::DecorationBinding);
73 compiler.set_decoration(s.combined_id, spv::DecorationBinding, binding);
77 } // namespace spirv_cross_util