spirv_cross_util.cpp 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. /*
  2. * Copyright 2015-2021 Arm Limited
  3. * SPDX-License-Identifier: Apache-2.0 OR MIT
  4. *
  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
  8. *
  9. * http://www.apache.org/licenses/LICENSE-2.0
  10. *
  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.
  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. */
  22. #include "spirv_cross_util.hpp"
  23. #include "spirv_common.hpp"
  24. using namespace spv;
  25. using namespace SPIRV_CROSS_NAMESPACE;
  26. namespace spirv_cross_util
  27. {
  28. void rename_interface_variable(Compiler &compiler, const SmallVector<Resource> &resources, uint32_t location,
  29. const std::string &name)
  30. {
  31. for (auto &v : resources)
  32. {
  33. if (!compiler.has_decoration(v.id, spv::DecorationLocation))
  34. continue;
  35. auto loc = compiler.get_decoration(v.id, spv::DecorationLocation);
  36. if (loc != location)
  37. continue;
  38. auto &type = compiler.get_type(v.base_type_id);
  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. compiler.set_name(v.id, name);
  48. }
  49. }
  50. void inherit_combined_sampler_bindings(Compiler &compiler)
  51. {
  52. auto &samplers = compiler.get_combined_image_samplers();
  53. for (auto &s : samplers)
  54. {
  55. if (compiler.has_decoration(s.image_id, spv::DecorationDescriptorSet))
  56. {
  57. uint32_t set = compiler.get_decoration(s.image_id, spv::DecorationDescriptorSet);
  58. compiler.set_decoration(s.combined_id, spv::DecorationDescriptorSet, set);
  59. }
  60. if (compiler.has_decoration(s.image_id, spv::DecorationBinding))
  61. {
  62. uint32_t binding = compiler.get_decoration(s.image_id, spv::DecorationBinding);
  63. compiler.set_decoration(s.combined_id, spv::DecorationBinding, binding);
  64. }
  65. }
  66. }
  67. } // namespace spirv_cross_util