3939namespace msl
4040{
4141
42- static void setBinding (std::vector<uint32_t >& spirv, std::uint32_t id, std::uint32_t index)
42+ static void setBinding (std::vector<uint32_t >& spirv, std::uint32_t id, std::uint32_t set,
43+ std::uint32_t binding)
4344{
4445 const unsigned int firstInstruction = 5 ;
4546 const std::uint32_t opCodeMask = 0xFFFF ;
4647 const std::uint32_t wordCountShift = 16 ;
4748 const std::uint32_t opFunction = 54 ;
4849 const std::uint32_t opDecorate = 71 ;
4950 const std::uint32_t decorationBinding = 33 ;
51+ const std::uint32_t decorationDescriptorSet = 34 ;
5052
53+ bool descriptorSet = false , bindingSet = false ;
5154 for (uint32_t i = firstInstruction; i < spirv.size ();)
5255 {
5356 uint32_t op = spirv[i] & opCodeMask;
@@ -57,22 +60,34 @@ static void setBinding(std::vector<uint32_t>& spirv, std::uint32_t id, std::uint
5760 if (op == opFunction)
5861 break ;
5962
60- if (op == opDecorate && spirv[i + 1 ] == id && spirv[i + 2 ] == decorationBinding )
63+ if (op == opDecorate && spirv[i + 1 ] == id)
6164 {
62- spirv[i + 3 ] = index;
63- break ;
65+ if (spirv[i + 2 ] == decorationDescriptorSet)
66+ {
67+ spirv[i + 3 ] = set;
68+ descriptorSet = true ;
69+ }
70+ else if (spirv[i + 2 ] == decorationBinding)
71+ {
72+ spirv[i + 3 ] = binding;
73+ bindingSet = true ;
74+ }
75+
76+ if (bindingSet && descriptorSet)
77+ break ;
6478 }
6579
6680 i += wordCount;
6781 }
6882}
6983
7084static std::vector<std::uint32_t > setBindingIndices (const std::vector<std::uint32_t >& spirv,
71- const std::vector<compile::Uniform>& uniforms, std::vector<std::uint32_t >& uniformIds)
85+ const std::vector<compile::Uniform>& uniforms, std::vector<std::uint32_t >& uniformIds,
86+ bool & hasPushConstant, std::uint32_t & bufferCount, std::uint32_t & textureCount)
7287{
7388 std::vector<std::uint32_t > adjustedSpirv = spirv;
7489
75- bool hasPushConstant = false ;
90+ hasPushConstant = false ;
7691 for (std::size_t i = 0 ; i < uniforms.size (); ++i)
7792 {
7893 if (uniformIds[i] != unknown && uniforms[i].uniformType == UniformType::PushConstant)
@@ -96,18 +111,20 @@ static std::vector<std::uint32_t> setBindingIndices(const std::vector<std::uint3
96111 break ;
97112 case UniformType::Block:
98113 case UniformType::BlockBuffer:
99- setBinding (adjustedSpirv, uniformIds[i], bufferIndex);
114+ setBinding (adjustedSpirv, uniformIds[i], 0 , bufferIndex);
100115 uniformIds[i] = bufferIndex++;
101116 break ;
102117 case UniformType::Image:
103118 case UniformType::SampledImage:
104119 case UniformType::SubpassInput:
105- setBinding (adjustedSpirv, uniformIds[i], textureIndex);
120+ setBinding (adjustedSpirv, uniformIds[i], 1 , textureIndex);
106121 uniformIds[i] = textureIndex++;
107122 break ;
108123 }
109124 }
110125
126+ bufferCount = bufferIndex;
127+ textureCount = textureIndex;
111128 return adjustedSpirv;
112129}
113130
@@ -182,12 +199,17 @@ bool TargetMetal::crossCompile(std::vector<std::uint8_t>& data, Output& output,
182199 const std::vector<std::uint32_t >& spirv, const std::string& entryPoint,
183200 const std::vector<compile::Uniform>& uniforms, std::vector<std::uint32_t >& uniformIds)
184201{
185- std::vector<std::uint32_t > adjustedSpirv = setBindingIndices (spirv, uniforms, uniformIds);
186202 bool outputToBuffer = stage == compile::Stage::Vertex &&
187203 (pipelineStages[static_cast <int >(compile::Stage::TessellationControl)] ||
188204 pipelineStages[static_cast <int >(compile::Stage::TessellationEvaluation)]);
189- std::string metal = MetalOutput::disassemble (output, adjustedSpirv, m_version, m_ios,
190- outputToBuffer, fileName, line, column);
205+
206+ bool hasPushConstant;
207+ std::uint32_t bufferCount, textureCount;
208+ std::vector<std::uint32_t > adjustedSpirv = setBindingIndices (spirv, uniforms, uniformIds,
209+ hasPushConstant, bufferCount, textureCount);
210+
211+ std::string metal = MetalOutput::disassemble (output, adjustedSpirv, stage, m_version, m_ios,
212+ outputToBuffer, hasPushConstant, bufferCount, textureCount, fileName, line, column);
191213 if (metal.empty ())
192214 return false ;
193215
0 commit comments