1 /*
2  * Copyright 2015-2020 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 #include "spirv_cross_util.hpp"
18 #include "spirv_common.hpp"
19 
20 using namespace spv;
21 using namespace SPIRV_CROSS_NAMESPACE;
22 
23 namespace spirv_cross_util
24 {
rename_interface_variable(Compiler & compiler,const SmallVector<Resource> & resources,uint32_t location,const std::string & name)25 void rename_interface_variable(Compiler &compiler, const SmallVector<Resource> &resources, uint32_t location,
26                                const std::string &name)
27 {
28 	for (auto &v : resources)
29 	{
30 		if (!compiler.has_decoration(v.id, spv::DecorationLocation))
31 			continue;
32 
33 		auto loc = compiler.get_decoration(v.id, spv::DecorationLocation);
34 		if (loc != location)
35 			continue;
36 
37 		auto &type = compiler.get_type(v.base_type_id);
38 
39 		// This is more of a friendly variant. If we need to rename interface variables, we might have to rename
40 		// structs as well and make sure all the names match up.
41 		if (type.basetype == SPIRType::Struct)
42 		{
43 			compiler.set_name(v.base_type_id, join("SPIRV_Cross_Interface_Location", location));
44 			for (uint32_t i = 0; i < uint32_t(type.member_types.size()); i++)
45 				compiler.set_member_name(v.base_type_id, i, join("InterfaceMember", i));
46 		}
47 
48 		compiler.set_name(v.id, name);
49 	}
50 }
51 
inherit_combined_sampler_bindings(Compiler & compiler)52 void inherit_combined_sampler_bindings(Compiler &compiler)
53 {
54 	auto &samplers = compiler.get_combined_image_samplers();
55 	for (auto &s : samplers)
56 	{
57 		if (compiler.has_decoration(s.image_id, spv::DecorationDescriptorSet))
58 		{
59 			uint32_t set = compiler.get_decoration(s.image_id, spv::DecorationDescriptorSet);
60 			compiler.set_decoration(s.combined_id, spv::DecorationDescriptorSet, set);
61 		}
62 
63 		if (compiler.has_decoration(s.image_id, spv::DecorationBinding))
64 		{
65 			uint32_t binding = compiler.get_decoration(s.image_id, spv::DecorationBinding);
66 			compiler.set_decoration(s.combined_id, spv::DecorationBinding, binding);
67 		}
68 	}
69 }
70 } // namespace spirv_cross_util
71