1 /*
2  * Copyright 2015-2021 Arm Limited
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 /*
18  * At your option, you may choose to accept this material under either:
19  *  1. The Apache License, Version 2.0, found at <http://www.apache.org/licenses/LICENSE-2.0>, or
20  *  2. The MIT License, found at <http://opensource.org/licenses/MIT>.
21  * SPDX-License-Identifier: Apache-2.0 OR MIT.
22  */
23 
24 #include "spirv_cross_util.hpp"
25 #include "spirv_common.hpp"
26 
27 using namespace spv;
28 using namespace SPIRV_CROSS_NAMESPACE;
29 
30 namespace spirv_cross_util
31 {
rename_interface_variable(Compiler & compiler,const SmallVector<Resource> & resources,uint32_t location,const std::string & name)32 void rename_interface_variable(Compiler &compiler, const SmallVector<Resource> &resources, uint32_t location,
33                                const std::string &name)
34 {
35 	for (auto &v : resources)
36 	{
37 		if (!compiler.has_decoration(v.id, spv::DecorationLocation))
38 			continue;
39 
40 		auto loc = compiler.get_decoration(v.id, spv::DecorationLocation);
41 		if (loc != location)
42 			continue;
43 
44 		auto &type = compiler.get_type(v.base_type_id);
45 
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)
49 		{
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));
53 		}
54 
55 		compiler.set_name(v.id, name);
56 	}
57 }
58 
inherit_combined_sampler_bindings(Compiler & compiler)59 void inherit_combined_sampler_bindings(Compiler &compiler)
60 {
61 	auto &samplers = compiler.get_combined_image_samplers();
62 	for (auto &s : samplers)
63 	{
64 		if (compiler.has_decoration(s.image_id, spv::DecorationDescriptorSet))
65 		{
66 			uint32_t set = compiler.get_decoration(s.image_id, spv::DecorationDescriptorSet);
67 			compiler.set_decoration(s.combined_id, spv::DecorationDescriptorSet, set);
68 		}
69 
70 		if (compiler.has_decoration(s.image_id, spv::DecorationBinding))
71 		{
72 			uint32_t binding = compiler.get_decoration(s.image_id, spv::DecorationBinding);
73 			compiler.set_decoration(s.combined_id, spv::DecorationBinding, binding);
74 		}
75 	}
76 }
77 } // namespace spirv_cross_util
78