1 | /* |
2 | * Copyright 2016-2021 The Brenwill Workshop Ltd. |
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 | /* |
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>. |
22 | */ |
23 | |
24 | #include "spirv_msl.hpp" |
25 | #include "GLSL.std.450.h" |
26 | |
27 | #include <algorithm> |
28 | #include <assert.h> |
29 | #include <numeric> |
30 | |
31 | using namespace spv; |
32 | using namespace SPIRV_CROSS_NAMESPACE; |
33 | using namespace std; |
34 | |
35 | static const uint32_t k_unknown_location = ~0u; |
36 | static const uint32_t k_unknown_component = ~0u; |
37 | static const char *force_inline = "static inline __attribute__((always_inline))" ; |
38 | |
39 | CompilerMSL::CompilerMSL(std::vector<uint32_t> spirv_) |
40 | : CompilerGLSL(std::move(spirv_)) |
41 | { |
42 | } |
43 | |
44 | CompilerMSL::CompilerMSL(const uint32_t *ir_, size_t word_count) |
45 | : CompilerGLSL(ir_, word_count) |
46 | { |
47 | } |
48 | |
49 | CompilerMSL::CompilerMSL(const ParsedIR &ir_) |
50 | : CompilerGLSL(ir_) |
51 | { |
52 | } |
53 | |
54 | CompilerMSL::CompilerMSL(ParsedIR &&ir_) |
55 | : CompilerGLSL(std::move(ir_)) |
56 | { |
57 | } |
58 | |
59 | void CompilerMSL::add_msl_shader_input(const MSLShaderInput &si) |
60 | { |
61 | inputs_by_location[{.location: si.location, .component: si.component}] = si; |
62 | if (si.builtin != BuiltInMax && !inputs_by_builtin.count(x: si.builtin)) |
63 | inputs_by_builtin[si.builtin] = si; |
64 | } |
65 | |
66 | void CompilerMSL::add_msl_resource_binding(const MSLResourceBinding &binding) |
67 | { |
68 | StageSetBinding tuple = { .model: binding.stage, .desc_set: binding.desc_set, .binding: binding.binding }; |
69 | resource_bindings[tuple] = { binding, false }; |
70 | |
71 | // If we might need to pad argument buffer members to positionally align |
72 | // arg buffer indexes, also maintain a lookup by argument buffer index. |
73 | if (msl_options.pad_argument_buffer_resources) |
74 | { |
75 | StageSetBinding arg_idx_tuple = { .model: binding.stage, .desc_set: binding.desc_set, .binding: k_unknown_component }; |
76 | |
77 | #define ADD_ARG_IDX_TO_BINDING_NUM_LOOKUP(rez) \ |
78 | arg_idx_tuple.binding = binding.msl_##rez; \ |
79 | resource_arg_buff_idx_to_binding_number[arg_idx_tuple] = binding.binding |
80 | |
81 | switch (binding.basetype) |
82 | { |
83 | case SPIRType::Void: |
84 | case SPIRType::Boolean: |
85 | case SPIRType::SByte: |
86 | case SPIRType::UByte: |
87 | case SPIRType::Short: |
88 | case SPIRType::UShort: |
89 | case SPIRType::Int: |
90 | case SPIRType::UInt: |
91 | case SPIRType::Int64: |
92 | case SPIRType::UInt64: |
93 | case SPIRType::AtomicCounter: |
94 | case SPIRType::Half: |
95 | case SPIRType::Float: |
96 | case SPIRType::Double: |
97 | ADD_ARG_IDX_TO_BINDING_NUM_LOOKUP(buffer); |
98 | break; |
99 | case SPIRType::Image: |
100 | ADD_ARG_IDX_TO_BINDING_NUM_LOOKUP(texture); |
101 | break; |
102 | case SPIRType::Sampler: |
103 | ADD_ARG_IDX_TO_BINDING_NUM_LOOKUP(sampler); |
104 | break; |
105 | case SPIRType::SampledImage: |
106 | ADD_ARG_IDX_TO_BINDING_NUM_LOOKUP(texture); |
107 | ADD_ARG_IDX_TO_BINDING_NUM_LOOKUP(sampler); |
108 | break; |
109 | default: |
110 | SPIRV_CROSS_THROW("Unexpected argument buffer resource base type. When padding argument buffer elements, " |
111 | "all descriptor set resources must be supplied with a base type by the app." ); |
112 | } |
113 | #undef ADD_ARG_IDX_TO_BINDING_NUM_LOOKUP |
114 | } |
115 | } |
116 | |
117 | void CompilerMSL::add_dynamic_buffer(uint32_t desc_set, uint32_t binding, uint32_t index) |
118 | { |
119 | SetBindingPair pair = { .desc_set: desc_set, .binding: binding }; |
120 | buffers_requiring_dynamic_offset[pair] = { index, 0 }; |
121 | } |
122 | |
123 | void CompilerMSL::add_inline_uniform_block(uint32_t desc_set, uint32_t binding) |
124 | { |
125 | SetBindingPair pair = { .desc_set: desc_set, .binding: binding }; |
126 | inline_uniform_blocks.insert(x: pair); |
127 | } |
128 | |
129 | void CompilerMSL::add_discrete_descriptor_set(uint32_t desc_set) |
130 | { |
131 | if (desc_set < kMaxArgumentBuffers) |
132 | argument_buffer_discrete_mask |= 1u << desc_set; |
133 | } |
134 | |
135 | void CompilerMSL::set_argument_buffer_device_address_space(uint32_t desc_set, bool device_storage) |
136 | { |
137 | if (desc_set < kMaxArgumentBuffers) |
138 | { |
139 | if (device_storage) |
140 | argument_buffer_device_storage_mask |= 1u << desc_set; |
141 | else |
142 | argument_buffer_device_storage_mask &= ~(1u << desc_set); |
143 | } |
144 | } |
145 | |
146 | bool CompilerMSL::is_msl_shader_input_used(uint32_t location) |
147 | { |
148 | // Don't report internal location allocations to app. |
149 | return location_inputs_in_use.count(x: location) != 0 && |
150 | location_inputs_in_use_fallback.count(x: location) == 0; |
151 | } |
152 | |
153 | uint32_t CompilerMSL::get_automatic_builtin_input_location(spv::BuiltIn builtin) const |
154 | { |
155 | auto itr = builtin_to_automatic_input_location.find(x: builtin); |
156 | if (itr == builtin_to_automatic_input_location.end()) |
157 | return k_unknown_location; |
158 | else |
159 | return itr->second; |
160 | } |
161 | |
162 | bool CompilerMSL::is_msl_resource_binding_used(ExecutionModel model, uint32_t desc_set, uint32_t binding) const |
163 | { |
164 | StageSetBinding tuple = { .model: model, .desc_set: desc_set, .binding: binding }; |
165 | auto itr = resource_bindings.find(x: tuple); |
166 | return itr != end(cont: resource_bindings) && itr->second.second; |
167 | } |
168 | |
169 | // Returns the size of the array of resources used by the variable with the specified id. |
170 | // The returned value is retrieved from the resource binding added using add_msl_resource_binding(). |
171 | uint32_t CompilerMSL::get_resource_array_size(uint32_t id) const |
172 | { |
173 | StageSetBinding tuple = { .model: get_entry_point().model, .desc_set: get_decoration(id, decoration: DecorationDescriptorSet), |
174 | .binding: get_decoration(id, decoration: DecorationBinding) }; |
175 | auto itr = resource_bindings.find(x: tuple); |
176 | return itr != end(cont: resource_bindings) ? itr->second.first.count : 0; |
177 | } |
178 | |
179 | uint32_t CompilerMSL::get_automatic_msl_resource_binding(uint32_t id) const |
180 | { |
181 | return get_extended_decoration(id, decoration: SPIRVCrossDecorationResourceIndexPrimary); |
182 | } |
183 | |
184 | uint32_t CompilerMSL::get_automatic_msl_resource_binding_secondary(uint32_t id) const |
185 | { |
186 | return get_extended_decoration(id, decoration: SPIRVCrossDecorationResourceIndexSecondary); |
187 | } |
188 | |
189 | uint32_t CompilerMSL::get_automatic_msl_resource_binding_tertiary(uint32_t id) const |
190 | { |
191 | return get_extended_decoration(id, decoration: SPIRVCrossDecorationResourceIndexTertiary); |
192 | } |
193 | |
194 | uint32_t CompilerMSL::get_automatic_msl_resource_binding_quaternary(uint32_t id) const |
195 | { |
196 | return get_extended_decoration(id, decoration: SPIRVCrossDecorationResourceIndexQuaternary); |
197 | } |
198 | |
199 | void CompilerMSL::set_fragment_output_components(uint32_t location, uint32_t components) |
200 | { |
201 | fragment_output_components[location] = components; |
202 | } |
203 | |
204 | bool CompilerMSL::builtin_translates_to_nonarray(spv::BuiltIn builtin) const |
205 | { |
206 | return (builtin == BuiltInSampleMask); |
207 | } |
208 | |
209 | void CompilerMSL::build_implicit_builtins() |
210 | { |
211 | bool need_sample_pos = active_input_builtins.get(bit: BuiltInSamplePosition); |
212 | bool need_vertex_params = capture_output_to_buffer && get_execution_model() == ExecutionModelVertex && |
213 | !msl_options.vertex_for_tessellation; |
214 | bool need_tesc_params = get_execution_model() == ExecutionModelTessellationControl; |
215 | bool need_subgroup_mask = |
216 | active_input_builtins.get(bit: BuiltInSubgroupEqMask) || active_input_builtins.get(bit: BuiltInSubgroupGeMask) || |
217 | active_input_builtins.get(bit: BuiltInSubgroupGtMask) || active_input_builtins.get(bit: BuiltInSubgroupLeMask) || |
218 | active_input_builtins.get(bit: BuiltInSubgroupLtMask); |
219 | bool need_subgroup_ge_mask = !msl_options.is_ios() && (active_input_builtins.get(bit: BuiltInSubgroupGeMask) || |
220 | active_input_builtins.get(bit: BuiltInSubgroupGtMask)); |
221 | bool need_multiview = get_execution_model() == ExecutionModelVertex && !msl_options.view_index_from_device_index && |
222 | msl_options.multiview_layered_rendering && |
223 | (msl_options.multiview || active_input_builtins.get(bit: BuiltInViewIndex)); |
224 | bool need_dispatch_base = |
225 | msl_options.dispatch_base && get_execution_model() == ExecutionModelGLCompute && |
226 | (active_input_builtins.get(bit: BuiltInWorkgroupId) || active_input_builtins.get(bit: BuiltInGlobalInvocationId)); |
227 | bool need_grid_params = get_execution_model() == ExecutionModelVertex && msl_options.vertex_for_tessellation; |
228 | bool need_vertex_base_params = |
229 | need_grid_params && |
230 | (active_input_builtins.get(bit: BuiltInVertexId) || active_input_builtins.get(bit: BuiltInVertexIndex) || |
231 | active_input_builtins.get(bit: BuiltInBaseVertex) || active_input_builtins.get(bit: BuiltInInstanceId) || |
232 | active_input_builtins.get(bit: BuiltInInstanceIndex) || active_input_builtins.get(bit: BuiltInBaseInstance)); |
233 | bool need_local_invocation_index = msl_options.emulate_subgroups && active_input_builtins.get(bit: BuiltInSubgroupId); |
234 | bool need_workgroup_size = msl_options.emulate_subgroups && active_input_builtins.get(bit: BuiltInNumSubgroups); |
235 | |
236 | if (need_subpass_input || need_sample_pos || need_subgroup_mask || need_vertex_params || need_tesc_params || |
237 | need_multiview || need_dispatch_base || need_vertex_base_params || need_grid_params || needs_sample_id || |
238 | needs_subgroup_invocation_id || needs_subgroup_size || has_additional_fixed_sample_mask() || need_local_invocation_index || |
239 | need_workgroup_size) |
240 | { |
241 | bool has_frag_coord = false; |
242 | bool has_sample_id = false; |
243 | bool has_vertex_idx = false; |
244 | bool has_base_vertex = false; |
245 | bool has_instance_idx = false; |
246 | bool has_base_instance = false; |
247 | bool has_invocation_id = false; |
248 | bool has_primitive_id = false; |
249 | bool has_subgroup_invocation_id = false; |
250 | bool has_subgroup_size = false; |
251 | bool has_view_idx = false; |
252 | bool has_layer = false; |
253 | bool has_local_invocation_index = false; |
254 | bool has_workgroup_size = false; |
255 | uint32_t workgroup_id_type = 0; |
256 | |
257 | ir.for_each_typed_id<SPIRVariable>(op: [&](uint32_t, SPIRVariable &var) { |
258 | if (var.storage != StorageClassInput && var.storage != StorageClassOutput) |
259 | return; |
260 | if (!interface_variable_exists_in_entry_point(id: var.self)) |
261 | return; |
262 | if (!has_decoration(id: var.self, decoration: DecorationBuiltIn)) |
263 | return; |
264 | |
265 | BuiltIn builtin = ir.meta[var.self].decoration.builtin_type; |
266 | |
267 | if (var.storage == StorageClassOutput) |
268 | { |
269 | if (has_additional_fixed_sample_mask() && builtin == BuiltInSampleMask) |
270 | { |
271 | builtin_sample_mask_id = var.self; |
272 | mark_implicit_builtin(storage: StorageClassOutput, builtin: BuiltInSampleMask, id: var.self); |
273 | does_shader_write_sample_mask = true; |
274 | } |
275 | } |
276 | |
277 | if (var.storage != StorageClassInput) |
278 | return; |
279 | |
280 | // Use Metal's native frame-buffer fetch API for subpass inputs. |
281 | if (need_subpass_input && (!msl_options.use_framebuffer_fetch_subpasses)) |
282 | { |
283 | switch (builtin) |
284 | { |
285 | case BuiltInFragCoord: |
286 | mark_implicit_builtin(storage: StorageClassInput, builtin: BuiltInFragCoord, id: var.self); |
287 | builtin_frag_coord_id = var.self; |
288 | has_frag_coord = true; |
289 | break; |
290 | case BuiltInLayer: |
291 | if (!msl_options.arrayed_subpass_input || msl_options.multiview) |
292 | break; |
293 | mark_implicit_builtin(storage: StorageClassInput, builtin: BuiltInLayer, id: var.self); |
294 | builtin_layer_id = var.self; |
295 | has_layer = true; |
296 | break; |
297 | case BuiltInViewIndex: |
298 | if (!msl_options.multiview) |
299 | break; |
300 | mark_implicit_builtin(storage: StorageClassInput, builtin: BuiltInViewIndex, id: var.self); |
301 | builtin_view_idx_id = var.self; |
302 | has_view_idx = true; |
303 | break; |
304 | default: |
305 | break; |
306 | } |
307 | } |
308 | |
309 | if ((need_sample_pos || needs_sample_id) && builtin == BuiltInSampleId) |
310 | { |
311 | builtin_sample_id_id = var.self; |
312 | mark_implicit_builtin(storage: StorageClassInput, builtin: BuiltInSampleId, id: var.self); |
313 | has_sample_id = true; |
314 | } |
315 | |
316 | if (need_vertex_params) |
317 | { |
318 | switch (builtin) |
319 | { |
320 | case BuiltInVertexIndex: |
321 | builtin_vertex_idx_id = var.self; |
322 | mark_implicit_builtin(storage: StorageClassInput, builtin: BuiltInVertexIndex, id: var.self); |
323 | has_vertex_idx = true; |
324 | break; |
325 | case BuiltInBaseVertex: |
326 | builtin_base_vertex_id = var.self; |
327 | mark_implicit_builtin(storage: StorageClassInput, builtin: BuiltInBaseVertex, id: var.self); |
328 | has_base_vertex = true; |
329 | break; |
330 | case BuiltInInstanceIndex: |
331 | builtin_instance_idx_id = var.self; |
332 | mark_implicit_builtin(storage: StorageClassInput, builtin: BuiltInInstanceIndex, id: var.self); |
333 | has_instance_idx = true; |
334 | break; |
335 | case BuiltInBaseInstance: |
336 | builtin_base_instance_id = var.self; |
337 | mark_implicit_builtin(storage: StorageClassInput, builtin: BuiltInBaseInstance, id: var.self); |
338 | has_base_instance = true; |
339 | break; |
340 | default: |
341 | break; |
342 | } |
343 | } |
344 | |
345 | if (need_tesc_params) |
346 | { |
347 | switch (builtin) |
348 | { |
349 | case BuiltInInvocationId: |
350 | builtin_invocation_id_id = var.self; |
351 | mark_implicit_builtin(storage: StorageClassInput, builtin: BuiltInInvocationId, id: var.self); |
352 | has_invocation_id = true; |
353 | break; |
354 | case BuiltInPrimitiveId: |
355 | builtin_primitive_id_id = var.self; |
356 | mark_implicit_builtin(storage: StorageClassInput, builtin: BuiltInPrimitiveId, id: var.self); |
357 | has_primitive_id = true; |
358 | break; |
359 | default: |
360 | break; |
361 | } |
362 | } |
363 | |
364 | if ((need_subgroup_mask || needs_subgroup_invocation_id) && builtin == BuiltInSubgroupLocalInvocationId) |
365 | { |
366 | builtin_subgroup_invocation_id_id = var.self; |
367 | mark_implicit_builtin(storage: StorageClassInput, builtin: BuiltInSubgroupLocalInvocationId, id: var.self); |
368 | has_subgroup_invocation_id = true; |
369 | } |
370 | |
371 | if ((need_subgroup_ge_mask || needs_subgroup_size) && builtin == BuiltInSubgroupSize) |
372 | { |
373 | builtin_subgroup_size_id = var.self; |
374 | mark_implicit_builtin(storage: StorageClassInput, builtin: BuiltInSubgroupSize, id: var.self); |
375 | has_subgroup_size = true; |
376 | } |
377 | |
378 | if (need_multiview) |
379 | { |
380 | switch (builtin) |
381 | { |
382 | case BuiltInInstanceIndex: |
383 | // The view index here is derived from the instance index. |
384 | builtin_instance_idx_id = var.self; |
385 | mark_implicit_builtin(storage: StorageClassInput, builtin: BuiltInInstanceIndex, id: var.self); |
386 | has_instance_idx = true; |
387 | break; |
388 | case BuiltInBaseInstance: |
389 | // If a non-zero base instance is used, we need to adjust for it when calculating the view index. |
390 | builtin_base_instance_id = var.self; |
391 | mark_implicit_builtin(storage: StorageClassInput, builtin: BuiltInBaseInstance, id: var.self); |
392 | has_base_instance = true; |
393 | break; |
394 | case BuiltInViewIndex: |
395 | builtin_view_idx_id = var.self; |
396 | mark_implicit_builtin(storage: StorageClassInput, builtin: BuiltInViewIndex, id: var.self); |
397 | has_view_idx = true; |
398 | break; |
399 | default: |
400 | break; |
401 | } |
402 | } |
403 | |
404 | if (need_local_invocation_index && builtin == BuiltInLocalInvocationIndex) |
405 | { |
406 | builtin_local_invocation_index_id = var.self; |
407 | mark_implicit_builtin(storage: StorageClassInput, builtin: BuiltInLocalInvocationIndex, id: var.self); |
408 | has_local_invocation_index = true; |
409 | } |
410 | |
411 | if (need_workgroup_size && builtin == BuiltInLocalInvocationId) |
412 | { |
413 | builtin_workgroup_size_id = var.self; |
414 | mark_implicit_builtin(storage: StorageClassInput, builtin: BuiltInWorkgroupSize, id: var.self); |
415 | has_workgroup_size = true; |
416 | } |
417 | |
418 | // The base workgroup needs to have the same type and vector size |
419 | // as the workgroup or invocation ID, so keep track of the type that |
420 | // was used. |
421 | if (need_dispatch_base && workgroup_id_type == 0 && |
422 | (builtin == BuiltInWorkgroupId || builtin == BuiltInGlobalInvocationId)) |
423 | workgroup_id_type = var.basetype; |
424 | }); |
425 | |
426 | // Use Metal's native frame-buffer fetch API for subpass inputs. |
427 | if ((!has_frag_coord || (msl_options.multiview && !has_view_idx) || |
428 | (msl_options.arrayed_subpass_input && !msl_options.multiview && !has_layer)) && |
429 | (!msl_options.use_framebuffer_fetch_subpasses) && need_subpass_input) |
430 | { |
431 | if (!has_frag_coord) |
432 | { |
433 | uint32_t offset = ir.increase_bound_by(count: 3); |
434 | uint32_t type_id = offset; |
435 | uint32_t type_ptr_id = offset + 1; |
436 | uint32_t var_id = offset + 2; |
437 | |
438 | // Create gl_FragCoord. |
439 | SPIRType vec4_type; |
440 | vec4_type.basetype = SPIRType::Float; |
441 | vec4_type.width = 32; |
442 | vec4_type.vecsize = 4; |
443 | set<SPIRType>(id: type_id, args&: vec4_type); |
444 | |
445 | SPIRType vec4_type_ptr; |
446 | vec4_type_ptr = vec4_type; |
447 | vec4_type_ptr.pointer = true; |
448 | vec4_type_ptr.pointer_depth++; |
449 | vec4_type_ptr.parent_type = type_id; |
450 | vec4_type_ptr.storage = StorageClassInput; |
451 | auto &ptr_type = set<SPIRType>(id: type_ptr_id, args&: vec4_type_ptr); |
452 | ptr_type.self = type_id; |
453 | |
454 | set<SPIRVariable>(id: var_id, args&: type_ptr_id, args: StorageClassInput); |
455 | set_decoration(id: var_id, decoration: DecorationBuiltIn, argument: BuiltInFragCoord); |
456 | builtin_frag_coord_id = var_id; |
457 | mark_implicit_builtin(storage: StorageClassInput, builtin: BuiltInFragCoord, id: var_id); |
458 | } |
459 | |
460 | if (!has_layer && msl_options.arrayed_subpass_input && !msl_options.multiview) |
461 | { |
462 | uint32_t offset = ir.increase_bound_by(count: 2); |
463 | uint32_t type_ptr_id = offset; |
464 | uint32_t var_id = offset + 1; |
465 | |
466 | // Create gl_Layer. |
467 | SPIRType uint_type_ptr; |
468 | uint_type_ptr = get_uint_type(); |
469 | uint_type_ptr.pointer = true; |
470 | uint_type_ptr.pointer_depth++; |
471 | uint_type_ptr.parent_type = get_uint_type_id(); |
472 | uint_type_ptr.storage = StorageClassInput; |
473 | auto &ptr_type = set<SPIRType>(id: type_ptr_id, args&: uint_type_ptr); |
474 | ptr_type.self = get_uint_type_id(); |
475 | |
476 | set<SPIRVariable>(id: var_id, args&: type_ptr_id, args: StorageClassInput); |
477 | set_decoration(id: var_id, decoration: DecorationBuiltIn, argument: BuiltInLayer); |
478 | builtin_layer_id = var_id; |
479 | mark_implicit_builtin(storage: StorageClassInput, builtin: BuiltInLayer, id: var_id); |
480 | } |
481 | |
482 | if (!has_view_idx && msl_options.multiview) |
483 | { |
484 | uint32_t offset = ir.increase_bound_by(count: 2); |
485 | uint32_t type_ptr_id = offset; |
486 | uint32_t var_id = offset + 1; |
487 | |
488 | // Create gl_ViewIndex. |
489 | SPIRType uint_type_ptr; |
490 | uint_type_ptr = get_uint_type(); |
491 | uint_type_ptr.pointer = true; |
492 | uint_type_ptr.pointer_depth++; |
493 | uint_type_ptr.parent_type = get_uint_type_id(); |
494 | uint_type_ptr.storage = StorageClassInput; |
495 | auto &ptr_type = set<SPIRType>(id: type_ptr_id, args&: uint_type_ptr); |
496 | ptr_type.self = get_uint_type_id(); |
497 | |
498 | set<SPIRVariable>(id: var_id, args&: type_ptr_id, args: StorageClassInput); |
499 | set_decoration(id: var_id, decoration: DecorationBuiltIn, argument: BuiltInViewIndex); |
500 | builtin_view_idx_id = var_id; |
501 | mark_implicit_builtin(storage: StorageClassInput, builtin: BuiltInViewIndex, id: var_id); |
502 | } |
503 | } |
504 | |
505 | if (!has_sample_id && (need_sample_pos || needs_sample_id)) |
506 | { |
507 | uint32_t offset = ir.increase_bound_by(count: 2); |
508 | uint32_t type_ptr_id = offset; |
509 | uint32_t var_id = offset + 1; |
510 | |
511 | // Create gl_SampleID. |
512 | SPIRType uint_type_ptr; |
513 | uint_type_ptr = get_uint_type(); |
514 | uint_type_ptr.pointer = true; |
515 | uint_type_ptr.pointer_depth++; |
516 | uint_type_ptr.parent_type = get_uint_type_id(); |
517 | uint_type_ptr.storage = StorageClassInput; |
518 | auto &ptr_type = set<SPIRType>(id: type_ptr_id, args&: uint_type_ptr); |
519 | ptr_type.self = get_uint_type_id(); |
520 | |
521 | set<SPIRVariable>(id: var_id, args&: type_ptr_id, args: StorageClassInput); |
522 | set_decoration(id: var_id, decoration: DecorationBuiltIn, argument: BuiltInSampleId); |
523 | builtin_sample_id_id = var_id; |
524 | mark_implicit_builtin(storage: StorageClassInput, builtin: BuiltInSampleId, id: var_id); |
525 | } |
526 | |
527 | if ((need_vertex_params && (!has_vertex_idx || !has_base_vertex || !has_instance_idx || !has_base_instance)) || |
528 | (need_multiview && (!has_instance_idx || !has_base_instance || !has_view_idx))) |
529 | { |
530 | uint32_t type_ptr_id = ir.increase_bound_by(count: 1); |
531 | |
532 | SPIRType uint_type_ptr; |
533 | uint_type_ptr = get_uint_type(); |
534 | uint_type_ptr.pointer = true; |
535 | uint_type_ptr.pointer_depth++; |
536 | uint_type_ptr.parent_type = get_uint_type_id(); |
537 | uint_type_ptr.storage = StorageClassInput; |
538 | auto &ptr_type = set<SPIRType>(id: type_ptr_id, args&: uint_type_ptr); |
539 | ptr_type.self = get_uint_type_id(); |
540 | |
541 | if (need_vertex_params && !has_vertex_idx) |
542 | { |
543 | uint32_t var_id = ir.increase_bound_by(count: 1); |
544 | |
545 | // Create gl_VertexIndex. |
546 | set<SPIRVariable>(id: var_id, args&: type_ptr_id, args: StorageClassInput); |
547 | set_decoration(id: var_id, decoration: DecorationBuiltIn, argument: BuiltInVertexIndex); |
548 | builtin_vertex_idx_id = var_id; |
549 | mark_implicit_builtin(storage: StorageClassInput, builtin: BuiltInVertexIndex, id: var_id); |
550 | } |
551 | |
552 | if (need_vertex_params && !has_base_vertex) |
553 | { |
554 | uint32_t var_id = ir.increase_bound_by(count: 1); |
555 | |
556 | // Create gl_BaseVertex. |
557 | set<SPIRVariable>(id: var_id, args&: type_ptr_id, args: StorageClassInput); |
558 | set_decoration(id: var_id, decoration: DecorationBuiltIn, argument: BuiltInBaseVertex); |
559 | builtin_base_vertex_id = var_id; |
560 | mark_implicit_builtin(storage: StorageClassInput, builtin: BuiltInBaseVertex, id: var_id); |
561 | } |
562 | |
563 | if (!has_instance_idx) // Needed by both multiview and tessellation |
564 | { |
565 | uint32_t var_id = ir.increase_bound_by(count: 1); |
566 | |
567 | // Create gl_InstanceIndex. |
568 | set<SPIRVariable>(id: var_id, args&: type_ptr_id, args: StorageClassInput); |
569 | set_decoration(id: var_id, decoration: DecorationBuiltIn, argument: BuiltInInstanceIndex); |
570 | builtin_instance_idx_id = var_id; |
571 | mark_implicit_builtin(storage: StorageClassInput, builtin: BuiltInInstanceIndex, id: var_id); |
572 | } |
573 | |
574 | if (!has_base_instance) // Needed by both multiview and tessellation |
575 | { |
576 | uint32_t var_id = ir.increase_bound_by(count: 1); |
577 | |
578 | // Create gl_BaseInstance. |
579 | set<SPIRVariable>(id: var_id, args&: type_ptr_id, args: StorageClassInput); |
580 | set_decoration(id: var_id, decoration: DecorationBuiltIn, argument: BuiltInBaseInstance); |
581 | builtin_base_instance_id = var_id; |
582 | mark_implicit_builtin(storage: StorageClassInput, builtin: BuiltInBaseInstance, id: var_id); |
583 | } |
584 | |
585 | if (need_multiview) |
586 | { |
587 | // Multiview shaders are not allowed to write to gl_Layer, ostensibly because |
588 | // it is implicitly written from gl_ViewIndex, but we have to do that explicitly. |
589 | // Note that we can't just abuse gl_ViewIndex for this purpose: it's an input, but |
590 | // gl_Layer is an output in vertex-pipeline shaders. |
591 | uint32_t type_ptr_out_id = ir.increase_bound_by(count: 2); |
592 | SPIRType uint_type_ptr_out; |
593 | uint_type_ptr_out = get_uint_type(); |
594 | uint_type_ptr_out.pointer = true; |
595 | uint_type_ptr_out.pointer_depth++; |
596 | uint_type_ptr_out.parent_type = get_uint_type_id(); |
597 | uint_type_ptr_out.storage = StorageClassOutput; |
598 | auto &ptr_out_type = set<SPIRType>(id: type_ptr_out_id, args&: uint_type_ptr_out); |
599 | ptr_out_type.self = get_uint_type_id(); |
600 | uint32_t var_id = type_ptr_out_id + 1; |
601 | set<SPIRVariable>(id: var_id, args&: type_ptr_out_id, args: StorageClassOutput); |
602 | set_decoration(id: var_id, decoration: DecorationBuiltIn, argument: BuiltInLayer); |
603 | builtin_layer_id = var_id; |
604 | mark_implicit_builtin(storage: StorageClassOutput, builtin: BuiltInLayer, id: var_id); |
605 | } |
606 | |
607 | if (need_multiview && !has_view_idx) |
608 | { |
609 | uint32_t var_id = ir.increase_bound_by(count: 1); |
610 | |
611 | // Create gl_ViewIndex. |
612 | set<SPIRVariable>(id: var_id, args&: type_ptr_id, args: StorageClassInput); |
613 | set_decoration(id: var_id, decoration: DecorationBuiltIn, argument: BuiltInViewIndex); |
614 | builtin_view_idx_id = var_id; |
615 | mark_implicit_builtin(storage: StorageClassInput, builtin: BuiltInViewIndex, id: var_id); |
616 | } |
617 | } |
618 | |
619 | if ((need_tesc_params && (msl_options.multi_patch_workgroup || !has_invocation_id || !has_primitive_id)) || |
620 | need_grid_params) |
621 | { |
622 | uint32_t type_ptr_id = ir.increase_bound_by(count: 1); |
623 | |
624 | SPIRType uint_type_ptr; |
625 | uint_type_ptr = get_uint_type(); |
626 | uint_type_ptr.pointer = true; |
627 | uint_type_ptr.pointer_depth++; |
628 | uint_type_ptr.parent_type = get_uint_type_id(); |
629 | uint_type_ptr.storage = StorageClassInput; |
630 | auto &ptr_type = set<SPIRType>(id: type_ptr_id, args&: uint_type_ptr); |
631 | ptr_type.self = get_uint_type_id(); |
632 | |
633 | if (msl_options.multi_patch_workgroup || need_grid_params) |
634 | { |
635 | uint32_t var_id = ir.increase_bound_by(count: 1); |
636 | |
637 | // Create gl_GlobalInvocationID. |
638 | set<SPIRVariable>(id: var_id, args&: type_ptr_id, args: StorageClassInput); |
639 | set_decoration(id: var_id, decoration: DecorationBuiltIn, argument: BuiltInGlobalInvocationId); |
640 | builtin_invocation_id_id = var_id; |
641 | mark_implicit_builtin(storage: StorageClassInput, builtin: BuiltInGlobalInvocationId, id: var_id); |
642 | } |
643 | else if (need_tesc_params && !has_invocation_id) |
644 | { |
645 | uint32_t var_id = ir.increase_bound_by(count: 1); |
646 | |
647 | // Create gl_InvocationID. |
648 | set<SPIRVariable>(id: var_id, args&: type_ptr_id, args: StorageClassInput); |
649 | set_decoration(id: var_id, decoration: DecorationBuiltIn, argument: BuiltInInvocationId); |
650 | builtin_invocation_id_id = var_id; |
651 | mark_implicit_builtin(storage: StorageClassInput, builtin: BuiltInInvocationId, id: var_id); |
652 | } |
653 | |
654 | if (need_tesc_params && !has_primitive_id) |
655 | { |
656 | uint32_t var_id = ir.increase_bound_by(count: 1); |
657 | |
658 | // Create gl_PrimitiveID. |
659 | set<SPIRVariable>(id: var_id, args&: type_ptr_id, args: StorageClassInput); |
660 | set_decoration(id: var_id, decoration: DecorationBuiltIn, argument: BuiltInPrimitiveId); |
661 | builtin_primitive_id_id = var_id; |
662 | mark_implicit_builtin(storage: StorageClassInput, builtin: BuiltInPrimitiveId, id: var_id); |
663 | } |
664 | |
665 | if (need_grid_params) |
666 | { |
667 | uint32_t var_id = ir.increase_bound_by(count: 1); |
668 | |
669 | set<SPIRVariable>(id: var_id, args: build_extended_vector_type(type_id: get_uint_type_id(), components: 3), args: StorageClassInput); |
670 | set_extended_decoration(id: var_id, decoration: SPIRVCrossDecorationBuiltInStageInputSize); |
671 | get_entry_point().interface_variables.push_back(t: var_id); |
672 | set_name(id: var_id, name: "spvStageInputSize" ); |
673 | builtin_stage_input_size_id = var_id; |
674 | } |
675 | } |
676 | |
677 | if (!has_subgroup_invocation_id && (need_subgroup_mask || needs_subgroup_invocation_id)) |
678 | { |
679 | uint32_t offset = ir.increase_bound_by(count: 2); |
680 | uint32_t type_ptr_id = offset; |
681 | uint32_t var_id = offset + 1; |
682 | |
683 | // Create gl_SubgroupInvocationID. |
684 | SPIRType uint_type_ptr; |
685 | uint_type_ptr = get_uint_type(); |
686 | uint_type_ptr.pointer = true; |
687 | uint_type_ptr.pointer_depth++; |
688 | uint_type_ptr.parent_type = get_uint_type_id(); |
689 | uint_type_ptr.storage = StorageClassInput; |
690 | auto &ptr_type = set<SPIRType>(id: type_ptr_id, args&: uint_type_ptr); |
691 | ptr_type.self = get_uint_type_id(); |
692 | |
693 | set<SPIRVariable>(id: var_id, args&: type_ptr_id, args: StorageClassInput); |
694 | set_decoration(id: var_id, decoration: DecorationBuiltIn, argument: BuiltInSubgroupLocalInvocationId); |
695 | builtin_subgroup_invocation_id_id = var_id; |
696 | mark_implicit_builtin(storage: StorageClassInput, builtin: BuiltInSubgroupLocalInvocationId, id: var_id); |
697 | } |
698 | |
699 | if (!has_subgroup_size && (need_subgroup_ge_mask || needs_subgroup_size)) |
700 | { |
701 | uint32_t offset = ir.increase_bound_by(count: 2); |
702 | uint32_t type_ptr_id = offset; |
703 | uint32_t var_id = offset + 1; |
704 | |
705 | // Create gl_SubgroupSize. |
706 | SPIRType uint_type_ptr; |
707 | uint_type_ptr = get_uint_type(); |
708 | uint_type_ptr.pointer = true; |
709 | uint_type_ptr.pointer_depth++; |
710 | uint_type_ptr.parent_type = get_uint_type_id(); |
711 | uint_type_ptr.storage = StorageClassInput; |
712 | auto &ptr_type = set<SPIRType>(id: type_ptr_id, args&: uint_type_ptr); |
713 | ptr_type.self = get_uint_type_id(); |
714 | |
715 | set<SPIRVariable>(id: var_id, args&: type_ptr_id, args: StorageClassInput); |
716 | set_decoration(id: var_id, decoration: DecorationBuiltIn, argument: BuiltInSubgroupSize); |
717 | builtin_subgroup_size_id = var_id; |
718 | mark_implicit_builtin(storage: StorageClassInput, builtin: BuiltInSubgroupSize, id: var_id); |
719 | } |
720 | |
721 | if (need_dispatch_base || need_vertex_base_params) |
722 | { |
723 | if (workgroup_id_type == 0) |
724 | workgroup_id_type = build_extended_vector_type(type_id: get_uint_type_id(), components: 3); |
725 | uint32_t var_id; |
726 | if (msl_options.supports_msl_version(major: 1, minor: 2)) |
727 | { |
728 | // If we have MSL 1.2, we can (ab)use the [[grid_origin]] builtin |
729 | // to convey this information and save a buffer slot. |
730 | uint32_t offset = ir.increase_bound_by(count: 1); |
731 | var_id = offset; |
732 | |
733 | set<SPIRVariable>(id: var_id, args&: workgroup_id_type, args: StorageClassInput); |
734 | set_extended_decoration(id: var_id, decoration: SPIRVCrossDecorationBuiltInDispatchBase); |
735 | get_entry_point().interface_variables.push_back(t: var_id); |
736 | } |
737 | else |
738 | { |
739 | // Otherwise, we need to fall back to a good ol' fashioned buffer. |
740 | uint32_t offset = ir.increase_bound_by(count: 2); |
741 | var_id = offset; |
742 | uint32_t type_id = offset + 1; |
743 | |
744 | SPIRType var_type = get<SPIRType>(id: workgroup_id_type); |
745 | var_type.storage = StorageClassUniform; |
746 | set<SPIRType>(id: type_id, args&: var_type); |
747 | |
748 | set<SPIRVariable>(id: var_id, args&: type_id, args: StorageClassUniform); |
749 | // This should never match anything. |
750 | set_decoration(id: var_id, decoration: DecorationDescriptorSet, argument: ~(5u)); |
751 | set_decoration(id: var_id, decoration: DecorationBinding, argument: msl_options.indirect_params_buffer_index); |
752 | set_extended_decoration(id: var_id, decoration: SPIRVCrossDecorationResourceIndexPrimary, |
753 | value: msl_options.indirect_params_buffer_index); |
754 | } |
755 | set_name(id: var_id, name: "spvDispatchBase" ); |
756 | builtin_dispatch_base_id = var_id; |
757 | } |
758 | |
759 | if (has_additional_fixed_sample_mask() && !does_shader_write_sample_mask) |
760 | { |
761 | uint32_t offset = ir.increase_bound_by(count: 2); |
762 | uint32_t var_id = offset + 1; |
763 | |
764 | // Create gl_SampleMask. |
765 | SPIRType uint_type_ptr_out; |
766 | uint_type_ptr_out = get_uint_type(); |
767 | uint_type_ptr_out.pointer = true; |
768 | uint_type_ptr_out.pointer_depth++; |
769 | uint_type_ptr_out.parent_type = get_uint_type_id(); |
770 | uint_type_ptr_out.storage = StorageClassOutput; |
771 | |
772 | auto &ptr_out_type = set<SPIRType>(id: offset, args&: uint_type_ptr_out); |
773 | ptr_out_type.self = get_uint_type_id(); |
774 | set<SPIRVariable>(id: var_id, args&: offset, args: StorageClassOutput); |
775 | set_decoration(id: var_id, decoration: DecorationBuiltIn, argument: BuiltInSampleMask); |
776 | builtin_sample_mask_id = var_id; |
777 | mark_implicit_builtin(storage: StorageClassOutput, builtin: BuiltInSampleMask, id: var_id); |
778 | } |
779 | |
780 | if (need_local_invocation_index && !has_local_invocation_index) |
781 | { |
782 | uint32_t offset = ir.increase_bound_by(count: 2); |
783 | uint32_t type_ptr_id = offset; |
784 | uint32_t var_id = offset + 1; |
785 | |
786 | // Create gl_LocalInvocationIndex. |
787 | SPIRType uint_type_ptr; |
788 | uint_type_ptr = get_uint_type(); |
789 | uint_type_ptr.pointer = true; |
790 | uint_type_ptr.pointer_depth++; |
791 | uint_type_ptr.parent_type = get_uint_type_id(); |
792 | uint_type_ptr.storage = StorageClassInput; |
793 | |
794 | auto &ptr_type = set<SPIRType>(id: type_ptr_id, args&: uint_type_ptr); |
795 | ptr_type.self = get_uint_type_id(); |
796 | set<SPIRVariable>(id: var_id, args&: type_ptr_id, args: StorageClassInput); |
797 | set_decoration(id: var_id, decoration: DecorationBuiltIn, argument: BuiltInLocalInvocationIndex); |
798 | builtin_local_invocation_index_id = var_id; |
799 | mark_implicit_builtin(storage: StorageClassInput, builtin: BuiltInLocalInvocationIndex, id: var_id); |
800 | } |
801 | |
802 | if (need_workgroup_size && !has_workgroup_size) |
803 | { |
804 | uint32_t offset = ir.increase_bound_by(count: 2); |
805 | uint32_t type_ptr_id = offset; |
806 | uint32_t var_id = offset + 1; |
807 | |
808 | // Create gl_WorkgroupSize. |
809 | uint32_t type_id = build_extended_vector_type(type_id: get_uint_type_id(), components: 3); |
810 | SPIRType uint_type_ptr = get<SPIRType>(id: type_id); |
811 | uint_type_ptr.pointer = true; |
812 | uint_type_ptr.pointer_depth++; |
813 | uint_type_ptr.parent_type = type_id; |
814 | uint_type_ptr.storage = StorageClassInput; |
815 | |
816 | auto &ptr_type = set<SPIRType>(id: type_ptr_id, args&: uint_type_ptr); |
817 | ptr_type.self = type_id; |
818 | set<SPIRVariable>(id: var_id, args&: type_ptr_id, args: StorageClassInput); |
819 | set_decoration(id: var_id, decoration: DecorationBuiltIn, argument: BuiltInWorkgroupSize); |
820 | builtin_workgroup_size_id = var_id; |
821 | mark_implicit_builtin(storage: StorageClassInput, builtin: BuiltInWorkgroupSize, id: var_id); |
822 | } |
823 | } |
824 | |
825 | if (needs_swizzle_buffer_def) |
826 | { |
827 | uint32_t var_id = build_constant_uint_array_pointer(); |
828 | set_name(id: var_id, name: "spvSwizzleConstants" ); |
829 | // This should never match anything. |
830 | set_decoration(id: var_id, decoration: DecorationDescriptorSet, argument: kSwizzleBufferBinding); |
831 | set_decoration(id: var_id, decoration: DecorationBinding, argument: msl_options.swizzle_buffer_index); |
832 | set_extended_decoration(id: var_id, decoration: SPIRVCrossDecorationResourceIndexPrimary, value: msl_options.swizzle_buffer_index); |
833 | swizzle_buffer_id = var_id; |
834 | } |
835 | |
836 | if (!buffers_requiring_array_length.empty()) |
837 | { |
838 | uint32_t var_id = build_constant_uint_array_pointer(); |
839 | set_name(id: var_id, name: "spvBufferSizeConstants" ); |
840 | // This should never match anything. |
841 | set_decoration(id: var_id, decoration: DecorationDescriptorSet, argument: kBufferSizeBufferBinding); |
842 | set_decoration(id: var_id, decoration: DecorationBinding, argument: msl_options.buffer_size_buffer_index); |
843 | set_extended_decoration(id: var_id, decoration: SPIRVCrossDecorationResourceIndexPrimary, value: msl_options.buffer_size_buffer_index); |
844 | buffer_size_buffer_id = var_id; |
845 | } |
846 | |
847 | if (needs_view_mask_buffer()) |
848 | { |
849 | uint32_t var_id = build_constant_uint_array_pointer(); |
850 | set_name(id: var_id, name: "spvViewMask" ); |
851 | // This should never match anything. |
852 | set_decoration(id: var_id, decoration: DecorationDescriptorSet, argument: ~(4u)); |
853 | set_decoration(id: var_id, decoration: DecorationBinding, argument: msl_options.view_mask_buffer_index); |
854 | set_extended_decoration(id: var_id, decoration: SPIRVCrossDecorationResourceIndexPrimary, value: msl_options.view_mask_buffer_index); |
855 | view_mask_buffer_id = var_id; |
856 | } |
857 | |
858 | if (!buffers_requiring_dynamic_offset.empty()) |
859 | { |
860 | uint32_t var_id = build_constant_uint_array_pointer(); |
861 | set_name(id: var_id, name: "spvDynamicOffsets" ); |
862 | // This should never match anything. |
863 | set_decoration(id: var_id, decoration: DecorationDescriptorSet, argument: ~(5u)); |
864 | set_decoration(id: var_id, decoration: DecorationBinding, argument: msl_options.dynamic_offsets_buffer_index); |
865 | set_extended_decoration(id: var_id, decoration: SPIRVCrossDecorationResourceIndexPrimary, |
866 | value: msl_options.dynamic_offsets_buffer_index); |
867 | dynamic_offsets_buffer_id = var_id; |
868 | } |
869 | |
870 | // If we're returning a struct from a vertex-like entry point, we must return a position attribute. |
871 | bool need_position = |
872 | (get_execution_model() == ExecutionModelVertex || |
873 | get_execution_model() == ExecutionModelTessellationEvaluation) && |
874 | !capture_output_to_buffer && !get_is_rasterization_disabled() && |
875 | !active_output_builtins.get(bit: BuiltInPosition); |
876 | |
877 | if (need_position) |
878 | { |
879 | // If we can get away with returning void from entry point, we don't need to care. |
880 | // If there is at least one other stage output, we need to return [[position]], |
881 | // so we need to create one if it doesn't appear in the SPIR-V. Before adding the |
882 | // implicit variable, check if it actually exists already, but just has not been used |
883 | // or initialized, and if so, mark it as active, and do not create the implicit variable. |
884 | bool has_output = false; |
885 | ir.for_each_typed_id<SPIRVariable>(op: [&](uint32_t, SPIRVariable &var) { |
886 | if (var.storage == StorageClassOutput && interface_variable_exists_in_entry_point(id: var.self)) |
887 | { |
888 | has_output = true; |
889 | |
890 | // Check if the var is the Position builtin |
891 | if (has_decoration(id: var.self, decoration: DecorationBuiltIn) && get_decoration(id: var.self, decoration: DecorationBuiltIn) == BuiltInPosition) |
892 | active_output_builtins.set(BuiltInPosition); |
893 | |
894 | // If the var is a struct, check if any members is the Position builtin |
895 | auto &var_type = get_variable_element_type(var); |
896 | if (var_type.basetype == SPIRType::Struct) |
897 | { |
898 | auto mbr_cnt = var_type.member_types.size(); |
899 | for (uint32_t mbr_idx = 0; mbr_idx < mbr_cnt; mbr_idx++) |
900 | { |
901 | auto builtin = BuiltInMax; |
902 | bool is_builtin = is_member_builtin(type: var_type, index: mbr_idx, builtin: &builtin); |
903 | if (is_builtin && builtin == BuiltInPosition) |
904 | active_output_builtins.set(BuiltInPosition); |
905 | } |
906 | } |
907 | } |
908 | }); |
909 | need_position = has_output && !active_output_builtins.get(bit: BuiltInPosition); |
910 | } |
911 | |
912 | if (need_position) |
913 | { |
914 | uint32_t offset = ir.increase_bound_by(count: 3); |
915 | uint32_t type_id = offset; |
916 | uint32_t type_ptr_id = offset + 1; |
917 | uint32_t var_id = offset + 2; |
918 | |
919 | // Create gl_Position. |
920 | SPIRType vec4_type; |
921 | vec4_type.basetype = SPIRType::Float; |
922 | vec4_type.width = 32; |
923 | vec4_type.vecsize = 4; |
924 | set<SPIRType>(id: type_id, args&: vec4_type); |
925 | |
926 | SPIRType vec4_type_ptr; |
927 | vec4_type_ptr = vec4_type; |
928 | vec4_type_ptr.pointer = true; |
929 | vec4_type_ptr.pointer_depth++; |
930 | vec4_type_ptr.parent_type = type_id; |
931 | vec4_type_ptr.storage = StorageClassOutput; |
932 | auto &ptr_type = set<SPIRType>(id: type_ptr_id, args&: vec4_type_ptr); |
933 | ptr_type.self = type_id; |
934 | |
935 | set<SPIRVariable>(id: var_id, args&: type_ptr_id, args: StorageClassOutput); |
936 | set_decoration(id: var_id, decoration: DecorationBuiltIn, argument: BuiltInPosition); |
937 | mark_implicit_builtin(storage: StorageClassOutput, builtin: BuiltInPosition, id: var_id); |
938 | } |
939 | } |
940 | |
941 | // Checks if the specified builtin variable (e.g. gl_InstanceIndex) is marked as active. |
942 | // If not, it marks it as active and forces a recompilation. |
943 | // This might be used when the optimization of inactive builtins was too optimistic (e.g. when "spvOut" is emitted). |
944 | void CompilerMSL::ensure_builtin(spv::StorageClass storage, spv::BuiltIn builtin) |
945 | { |
946 | Bitset *active_builtins = nullptr; |
947 | switch (storage) |
948 | { |
949 | case StorageClassInput: |
950 | active_builtins = &active_input_builtins; |
951 | break; |
952 | |
953 | case StorageClassOutput: |
954 | active_builtins = &active_output_builtins; |
955 | break; |
956 | |
957 | default: |
958 | break; |
959 | } |
960 | |
961 | // At this point, the specified builtin variable must have already been declared in the entry point. |
962 | // If not, mark as active and force recompile. |
963 | if (active_builtins != nullptr && !active_builtins->get(bit: builtin)) |
964 | { |
965 | active_builtins->set(builtin); |
966 | force_recompile(); |
967 | } |
968 | } |
969 | |
970 | void CompilerMSL::mark_implicit_builtin(StorageClass storage, BuiltIn builtin, uint32_t id) |
971 | { |
972 | Bitset *active_builtins = nullptr; |
973 | switch (storage) |
974 | { |
975 | case StorageClassInput: |
976 | active_builtins = &active_input_builtins; |
977 | break; |
978 | |
979 | case StorageClassOutput: |
980 | active_builtins = &active_output_builtins; |
981 | break; |
982 | |
983 | default: |
984 | break; |
985 | } |
986 | |
987 | assert(active_builtins != nullptr); |
988 | active_builtins->set(builtin); |
989 | |
990 | auto &var = get_entry_point().interface_variables; |
991 | if (find(first: begin(cont&: var), last: end(cont&: var), val: VariableID(id)) == end(cont&: var)) |
992 | var.push_back(t: id); |
993 | } |
994 | |
995 | uint32_t CompilerMSL::build_constant_uint_array_pointer() |
996 | { |
997 | uint32_t offset = ir.increase_bound_by(count: 3); |
998 | uint32_t type_ptr_id = offset; |
999 | uint32_t type_ptr_ptr_id = offset + 1; |
1000 | uint32_t var_id = offset + 2; |
1001 | |
1002 | // Create a buffer to hold extra data, including the swizzle constants. |
1003 | SPIRType uint_type_pointer = get_uint_type(); |
1004 | uint_type_pointer.pointer = true; |
1005 | uint_type_pointer.pointer_depth++; |
1006 | uint_type_pointer.parent_type = get_uint_type_id(); |
1007 | uint_type_pointer.storage = StorageClassUniform; |
1008 | set<SPIRType>(id: type_ptr_id, args&: uint_type_pointer); |
1009 | set_decoration(id: type_ptr_id, decoration: DecorationArrayStride, argument: 4); |
1010 | |
1011 | SPIRType uint_type_pointer2 = uint_type_pointer; |
1012 | uint_type_pointer2.pointer_depth++; |
1013 | uint_type_pointer2.parent_type = type_ptr_id; |
1014 | set<SPIRType>(id: type_ptr_ptr_id, args&: uint_type_pointer2); |
1015 | |
1016 | set<SPIRVariable>(id: var_id, args&: type_ptr_ptr_id, args: StorageClassUniformConstant); |
1017 | return var_id; |
1018 | } |
1019 | |
1020 | static string create_sampler_address(const char *prefix, MSLSamplerAddress addr) |
1021 | { |
1022 | switch (addr) |
1023 | { |
1024 | case MSL_SAMPLER_ADDRESS_CLAMP_TO_EDGE: |
1025 | return join(ts&: prefix, ts: "address::clamp_to_edge" ); |
1026 | case MSL_SAMPLER_ADDRESS_CLAMP_TO_ZERO: |
1027 | return join(ts&: prefix, ts: "address::clamp_to_zero" ); |
1028 | case MSL_SAMPLER_ADDRESS_CLAMP_TO_BORDER: |
1029 | return join(ts&: prefix, ts: "address::clamp_to_border" ); |
1030 | case MSL_SAMPLER_ADDRESS_REPEAT: |
1031 | return join(ts&: prefix, ts: "address::repeat" ); |
1032 | case MSL_SAMPLER_ADDRESS_MIRRORED_REPEAT: |
1033 | return join(ts&: prefix, ts: "address::mirrored_repeat" ); |
1034 | default: |
1035 | SPIRV_CROSS_THROW("Invalid sampler addressing mode." ); |
1036 | } |
1037 | } |
1038 | |
1039 | SPIRType &CompilerMSL::get_stage_in_struct_type() |
1040 | { |
1041 | auto &si_var = get<SPIRVariable>(id: stage_in_var_id); |
1042 | return get_variable_data_type(var: si_var); |
1043 | } |
1044 | |
1045 | SPIRType &CompilerMSL::get_stage_out_struct_type() |
1046 | { |
1047 | auto &so_var = get<SPIRVariable>(id: stage_out_var_id); |
1048 | return get_variable_data_type(var: so_var); |
1049 | } |
1050 | |
1051 | SPIRType &CompilerMSL::get_patch_stage_in_struct_type() |
1052 | { |
1053 | auto &si_var = get<SPIRVariable>(id: patch_stage_in_var_id); |
1054 | return get_variable_data_type(var: si_var); |
1055 | } |
1056 | |
1057 | SPIRType &CompilerMSL::get_patch_stage_out_struct_type() |
1058 | { |
1059 | auto &so_var = get<SPIRVariable>(id: patch_stage_out_var_id); |
1060 | return get_variable_data_type(var: so_var); |
1061 | } |
1062 | |
1063 | std::string CompilerMSL::get_tess_factor_struct_name() |
1064 | { |
1065 | if (get_entry_point().flags.get(bit: ExecutionModeTriangles)) |
1066 | return "MTLTriangleTessellationFactorsHalf" ; |
1067 | return "MTLQuadTessellationFactorsHalf" ; |
1068 | } |
1069 | |
1070 | SPIRType &CompilerMSL::get_uint_type() |
1071 | { |
1072 | return get<SPIRType>(id: get_uint_type_id()); |
1073 | } |
1074 | |
1075 | uint32_t CompilerMSL::get_uint_type_id() |
1076 | { |
1077 | if (uint_type_id != 0) |
1078 | return uint_type_id; |
1079 | |
1080 | uint_type_id = ir.increase_bound_by(count: 1); |
1081 | |
1082 | SPIRType type; |
1083 | type.basetype = SPIRType::UInt; |
1084 | type.width = 32; |
1085 | set<SPIRType>(id: uint_type_id, args&: type); |
1086 | return uint_type_id; |
1087 | } |
1088 | |
1089 | void CompilerMSL::emit_entry_point_declarations() |
1090 | { |
1091 | // FIXME: Get test coverage here ... |
1092 | // Constant arrays of non-primitive types (i.e. matrices) won't link properly into Metal libraries |
1093 | declare_complex_constant_arrays(); |
1094 | |
1095 | // Emit constexpr samplers here. |
1096 | for (auto &samp : constexpr_samplers_by_id) |
1097 | { |
1098 | auto &var = get<SPIRVariable>(id: samp.first); |
1099 | auto &type = get<SPIRType>(id: var.basetype); |
1100 | if (type.basetype == SPIRType::Sampler) |
1101 | add_resource_name(id: samp.first); |
1102 | |
1103 | SmallVector<string> args; |
1104 | auto &s = samp.second; |
1105 | |
1106 | if (s.coord != MSL_SAMPLER_COORD_NORMALIZED) |
1107 | args.push_back(t: "coord::pixel" ); |
1108 | |
1109 | if (s.min_filter == s.mag_filter) |
1110 | { |
1111 | if (s.min_filter != MSL_SAMPLER_FILTER_NEAREST) |
1112 | args.push_back(t: "filter::linear" ); |
1113 | } |
1114 | else |
1115 | { |
1116 | if (s.min_filter != MSL_SAMPLER_FILTER_NEAREST) |
1117 | args.push_back(t: "min_filter::linear" ); |
1118 | if (s.mag_filter != MSL_SAMPLER_FILTER_NEAREST) |
1119 | args.push_back(t: "mag_filter::linear" ); |
1120 | } |
1121 | |
1122 | switch (s.mip_filter) |
1123 | { |
1124 | case MSL_SAMPLER_MIP_FILTER_NONE: |
1125 | // Default |
1126 | break; |
1127 | case MSL_SAMPLER_MIP_FILTER_NEAREST: |
1128 | args.push_back(t: "mip_filter::nearest" ); |
1129 | break; |
1130 | case MSL_SAMPLER_MIP_FILTER_LINEAR: |
1131 | args.push_back(t: "mip_filter::linear" ); |
1132 | break; |
1133 | default: |
1134 | SPIRV_CROSS_THROW("Invalid mip filter." ); |
1135 | } |
1136 | |
1137 | if (s.s_address == s.t_address && s.s_address == s.r_address) |
1138 | { |
1139 | if (s.s_address != MSL_SAMPLER_ADDRESS_CLAMP_TO_EDGE) |
1140 | args.push_back(t: create_sampler_address(prefix: "" , addr: s.s_address)); |
1141 | } |
1142 | else |
1143 | { |
1144 | if (s.s_address != MSL_SAMPLER_ADDRESS_CLAMP_TO_EDGE) |
1145 | args.push_back(t: create_sampler_address(prefix: "s_" , addr: s.s_address)); |
1146 | if (s.t_address != MSL_SAMPLER_ADDRESS_CLAMP_TO_EDGE) |
1147 | args.push_back(t: create_sampler_address(prefix: "t_" , addr: s.t_address)); |
1148 | if (s.r_address != MSL_SAMPLER_ADDRESS_CLAMP_TO_EDGE) |
1149 | args.push_back(t: create_sampler_address(prefix: "r_" , addr: s.r_address)); |
1150 | } |
1151 | |
1152 | if (s.compare_enable) |
1153 | { |
1154 | switch (s.compare_func) |
1155 | { |
1156 | case MSL_SAMPLER_COMPARE_FUNC_ALWAYS: |
1157 | args.push_back(t: "compare_func::always" ); |
1158 | break; |
1159 | case MSL_SAMPLER_COMPARE_FUNC_NEVER: |
1160 | args.push_back(t: "compare_func::never" ); |
1161 | break; |
1162 | case MSL_SAMPLER_COMPARE_FUNC_EQUAL: |
1163 | args.push_back(t: "compare_func::equal" ); |
1164 | break; |
1165 | case MSL_SAMPLER_COMPARE_FUNC_NOT_EQUAL: |
1166 | args.push_back(t: "compare_func::not_equal" ); |
1167 | break; |
1168 | case MSL_SAMPLER_COMPARE_FUNC_LESS: |
1169 | args.push_back(t: "compare_func::less" ); |
1170 | break; |
1171 | case MSL_SAMPLER_COMPARE_FUNC_LESS_EQUAL: |
1172 | args.push_back(t: "compare_func::less_equal" ); |
1173 | break; |
1174 | case MSL_SAMPLER_COMPARE_FUNC_GREATER: |
1175 | args.push_back(t: "compare_func::greater" ); |
1176 | break; |
1177 | case MSL_SAMPLER_COMPARE_FUNC_GREATER_EQUAL: |
1178 | args.push_back(t: "compare_func::greater_equal" ); |
1179 | break; |
1180 | default: |
1181 | SPIRV_CROSS_THROW("Invalid sampler compare function." ); |
1182 | } |
1183 | } |
1184 | |
1185 | if (s.s_address == MSL_SAMPLER_ADDRESS_CLAMP_TO_BORDER || s.t_address == MSL_SAMPLER_ADDRESS_CLAMP_TO_BORDER || |
1186 | s.r_address == MSL_SAMPLER_ADDRESS_CLAMP_TO_BORDER) |
1187 | { |
1188 | switch (s.border_color) |
1189 | { |
1190 | case MSL_SAMPLER_BORDER_COLOR_OPAQUE_BLACK: |
1191 | args.push_back(t: "border_color::opaque_black" ); |
1192 | break; |
1193 | case MSL_SAMPLER_BORDER_COLOR_OPAQUE_WHITE: |
1194 | args.push_back(t: "border_color::opaque_white" ); |
1195 | break; |
1196 | case MSL_SAMPLER_BORDER_COLOR_TRANSPARENT_BLACK: |
1197 | args.push_back(t: "border_color::transparent_black" ); |
1198 | break; |
1199 | default: |
1200 | SPIRV_CROSS_THROW("Invalid sampler border color." ); |
1201 | } |
1202 | } |
1203 | |
1204 | if (s.anisotropy_enable) |
1205 | args.push_back(t: join(ts: "max_anisotropy(" , ts&: s.max_anisotropy, ts: ")" )); |
1206 | if (s.lod_clamp_enable) |
1207 | { |
1208 | args.push_back(t: join(ts: "lod_clamp(" , ts: convert_to_string(t: s.lod_clamp_min, locale_radix_point: current_locale_radix_character), ts: ", " , |
1209 | ts: convert_to_string(t: s.lod_clamp_max, locale_radix_point: current_locale_radix_character), ts: ")" )); |
1210 | } |
1211 | |
1212 | // If we would emit no arguments, then omit the parentheses entirely. Otherwise, |
1213 | // we'll wind up with a "most vexing parse" situation. |
1214 | if (args.empty()) |
1215 | statement(ts: "constexpr sampler " , |
1216 | ts: type.basetype == SPIRType::SampledImage ? to_sampler_expression(id: samp.first) : to_name(id: samp.first), |
1217 | ts: ";" ); |
1218 | else |
1219 | statement(ts: "constexpr sampler " , |
1220 | ts: type.basetype == SPIRType::SampledImage ? to_sampler_expression(id: samp.first) : to_name(id: samp.first), |
1221 | ts: "(" , ts: merge(list: args), ts: ");" ); |
1222 | } |
1223 | |
1224 | // Emit dynamic buffers here. |
1225 | for (auto &dynamic_buffer : buffers_requiring_dynamic_offset) |
1226 | { |
1227 | if (!dynamic_buffer.second.second) |
1228 | { |
1229 | // Could happen if no buffer was used at requested binding point. |
1230 | continue; |
1231 | } |
1232 | |
1233 | const auto &var = get<SPIRVariable>(id: dynamic_buffer.second.second); |
1234 | uint32_t var_id = var.self; |
1235 | const auto &type = get_variable_data_type(var); |
1236 | string name = to_name(id: var.self); |
1237 | uint32_t desc_set = get_decoration(id: var.self, decoration: DecorationDescriptorSet); |
1238 | uint32_t arg_id = argument_buffer_ids[desc_set]; |
1239 | uint32_t base_index = dynamic_buffer.second.first; |
1240 | |
1241 | if (!type.array.empty()) |
1242 | { |
1243 | // This is complicated, because we need to support arrays of arrays. |
1244 | // And it's even worse if the outermost dimension is a runtime array, because now |
1245 | // all this complicated goop has to go into the shader itself. (FIXME) |
1246 | if (!type.array[type.array.size() - 1]) |
1247 | SPIRV_CROSS_THROW("Runtime arrays with dynamic offsets are not supported yet." ); |
1248 | else |
1249 | { |
1250 | is_using_builtin_array = true; |
1251 | statement(ts: get_argument_address_space(argument: var), ts: " " , ts: type_to_glsl(type), ts: "* " , ts: to_restrict(id: var_id), ts&: name, |
1252 | ts: type_to_array_glsl(type), ts: " =" ); |
1253 | |
1254 | uint32_t dim = uint32_t(type.array.size()); |
1255 | uint32_t j = 0; |
1256 | for (SmallVector<uint32_t> indices(type.array.size()); |
1257 | indices[type.array.size() - 1] < to_array_size_literal(type); j++) |
1258 | { |
1259 | while (dim > 0) |
1260 | { |
1261 | begin_scope(); |
1262 | --dim; |
1263 | } |
1264 | |
1265 | string arrays; |
1266 | for (uint32_t i = uint32_t(type.array.size()); i; --i) |
1267 | arrays += join(ts: "[" , ts&: indices[i - 1], ts: "]" ); |
1268 | statement(ts: "(" , ts: get_argument_address_space(argument: var), ts: " " , ts: type_to_glsl(type), ts: "* " , |
1269 | ts: to_restrict(id: var_id, space: false), ts: ")((" , ts: get_argument_address_space(argument: var), ts: " char* " , |
1270 | ts: to_restrict(id: var_id, space: false), ts: ")" , ts: to_name(id: arg_id), ts: "." , ts: ensure_valid_name(name, pfx: "m" ), |
1271 | ts&: arrays, ts: " + " , ts: to_name(id: dynamic_offsets_buffer_id), ts: "[" , ts: base_index + j, ts: "])," ); |
1272 | |
1273 | while (++indices[dim] >= to_array_size_literal(type, index: dim) && dim < type.array.size() - 1) |
1274 | { |
1275 | end_scope(trailer: "," ); |
1276 | indices[dim++] = 0; |
1277 | } |
1278 | } |
1279 | end_scope_decl(); |
1280 | statement_no_indent(ts: "" ); |
1281 | is_using_builtin_array = false; |
1282 | } |
1283 | } |
1284 | else |
1285 | { |
1286 | statement(ts: get_argument_address_space(argument: var), ts: " auto& " , ts: to_restrict(id: var_id), ts&: name, ts: " = *(" , |
1287 | ts: get_argument_address_space(argument: var), ts: " " , ts: type_to_glsl(type), ts: "* " , ts: to_restrict(id: var_id, space: false), ts: ")((" , |
1288 | ts: get_argument_address_space(argument: var), ts: " char* " , ts: to_restrict(id: var_id, space: false), ts: ")" , ts: to_name(id: arg_id), ts: "." , |
1289 | ts: ensure_valid_name(name, pfx: "m" ), ts: " + " , ts: to_name(id: dynamic_offsets_buffer_id), ts: "[" , ts&: base_index, ts: "]);" ); |
1290 | } |
1291 | } |
1292 | |
1293 | // Emit buffer arrays here. |
1294 | for (uint32_t array_id : buffer_arrays) |
1295 | { |
1296 | const auto &var = get<SPIRVariable>(id: array_id); |
1297 | const auto &type = get_variable_data_type(var); |
1298 | const auto &buffer_type = get_variable_element_type(var); |
1299 | string name = to_name(id: array_id); |
1300 | statement(ts: get_argument_address_space(argument: var), ts: " " , ts: type_to_glsl(type: buffer_type), ts: "* " , ts: to_restrict(id: array_id), ts&: name, |
1301 | ts: "[] =" ); |
1302 | begin_scope(); |
1303 | for (uint32_t i = 0; i < to_array_size_literal(type); ++i) |
1304 | statement(ts&: name, ts: "_" , ts&: i, ts: "," ); |
1305 | end_scope_decl(); |
1306 | statement_no_indent(ts: "" ); |
1307 | } |
1308 | // For some reason, without this, we end up emitting the arrays twice. |
1309 | buffer_arrays.clear(); |
1310 | |
1311 | // Emit disabled fragment outputs. |
1312 | std::sort(first: disabled_frag_outputs.begin(), last: disabled_frag_outputs.end()); |
1313 | for (uint32_t var_id : disabled_frag_outputs) |
1314 | { |
1315 | auto &var = get<SPIRVariable>(id: var_id); |
1316 | add_local_variable_name(id: var_id); |
1317 | statement(ts: variable_decl(variable: var), ts: ";" ); |
1318 | var.deferred_declaration = false; |
1319 | } |
1320 | } |
1321 | |
1322 | string CompilerMSL::compile() |
1323 | { |
1324 | replace_illegal_entry_point_names(); |
1325 | ir.fixup_reserved_names(); |
1326 | |
1327 | // Do not deal with GLES-isms like precision, older extensions and such. |
1328 | options.vulkan_semantics = true; |
1329 | options.es = false; |
1330 | options.version = 450; |
1331 | backend.null_pointer_literal = "nullptr" ; |
1332 | backend.float_literal_suffix = false; |
1333 | backend.uint32_t_literal_suffix = true; |
1334 | backend.int16_t_literal_suffix = "" ; |
1335 | backend.uint16_t_literal_suffix = "" ; |
1336 | backend.basic_int_type = "int" ; |
1337 | backend.basic_uint_type = "uint" ; |
1338 | backend.basic_int8_type = "char" ; |
1339 | backend.basic_uint8_type = "uchar" ; |
1340 | backend.basic_int16_type = "short" ; |
1341 | backend.basic_uint16_type = "ushort" ; |
1342 | backend.discard_literal = "discard_fragment()" ; |
1343 | backend.demote_literal = "discard_fragment()" ; |
1344 | backend.boolean_mix_function = "select" ; |
1345 | backend.swizzle_is_function = false; |
1346 | backend.shared_is_implied = false; |
1347 | backend.use_initializer_list = true; |
1348 | backend.use_typed_initializer_list = true; |
1349 | backend.native_row_major_matrix = false; |
1350 | backend.unsized_array_supported = false; |
1351 | backend.can_declare_arrays_inline = false; |
1352 | backend.allow_truncated_access_chain = true; |
1353 | backend.comparison_image_samples_scalar = true; |
1354 | backend.native_pointers = true; |
1355 | backend.nonuniform_qualifier = "" ; |
1356 | backend.support_small_type_sampling_result = true; |
1357 | backend.supports_empty_struct = true; |
1358 | backend.support_64bit_switch = true; |
1359 | |
1360 | // Allow Metal to use the array<T> template unless we force it off. |
1361 | backend.can_return_array = !msl_options.force_native_arrays; |
1362 | backend.array_is_value_type = !msl_options.force_native_arrays; |
1363 | // Arrays which are part of buffer objects are never considered to be value types (just plain C-style). |
1364 | backend.array_is_value_type_in_buffer_blocks = false; |
1365 | backend.support_pointer_to_pointer = true; |
1366 | |
1367 | capture_output_to_buffer = msl_options.capture_output_to_buffer; |
1368 | is_rasterization_disabled = msl_options.disable_rasterization || capture_output_to_buffer; |
1369 | |
1370 | // Initialize array here rather than constructor, MSVC 2013 workaround. |
1371 | for (auto &id : next_metal_resource_ids) |
1372 | id = 0; |
1373 | |
1374 | fixup_anonymous_struct_names(); |
1375 | fixup_type_alias(); |
1376 | replace_illegal_names(); |
1377 | sync_entry_point_aliases_and_names(); |
1378 | |
1379 | build_function_control_flow_graphs_and_analyze(); |
1380 | update_active_builtins(); |
1381 | analyze_image_and_sampler_usage(); |
1382 | analyze_sampled_image_usage(); |
1383 | analyze_interlocked_resource_usage(); |
1384 | preprocess_op_codes(); |
1385 | build_implicit_builtins(); |
1386 | |
1387 | fixup_image_load_store_access(); |
1388 | |
1389 | set_enabled_interface_variables(get_active_interface_variables()); |
1390 | if (msl_options.force_active_argument_buffer_resources) |
1391 | activate_argument_buffer_resources(); |
1392 | |
1393 | if (swizzle_buffer_id) |
1394 | active_interface_variables.insert(x: swizzle_buffer_id); |
1395 | if (buffer_size_buffer_id) |
1396 | active_interface_variables.insert(x: buffer_size_buffer_id); |
1397 | if (view_mask_buffer_id) |
1398 | active_interface_variables.insert(x: view_mask_buffer_id); |
1399 | if (dynamic_offsets_buffer_id) |
1400 | active_interface_variables.insert(x: dynamic_offsets_buffer_id); |
1401 | if (builtin_layer_id) |
1402 | active_interface_variables.insert(x: builtin_layer_id); |
1403 | if (builtin_dispatch_base_id && !msl_options.supports_msl_version(major: 1, minor: 2)) |
1404 | active_interface_variables.insert(x: builtin_dispatch_base_id); |
1405 | if (builtin_sample_mask_id) |
1406 | active_interface_variables.insert(x: builtin_sample_mask_id); |
1407 | |
1408 | // Create structs to hold input, output and uniform variables. |
1409 | // Do output first to ensure out. is declared at top of entry function. |
1410 | qual_pos_var_name = "" ; |
1411 | stage_out_var_id = add_interface_block(storage: StorageClassOutput); |
1412 | patch_stage_out_var_id = add_interface_block(storage: StorageClassOutput, patch: true); |
1413 | stage_in_var_id = add_interface_block(storage: StorageClassInput); |
1414 | if (get_execution_model() == ExecutionModelTessellationEvaluation) |
1415 | patch_stage_in_var_id = add_interface_block(storage: StorageClassInput, patch: true); |
1416 | |
1417 | if (get_execution_model() == ExecutionModelTessellationControl) |
1418 | stage_out_ptr_var_id = add_interface_block_pointer(ib_var_id: stage_out_var_id, storage: StorageClassOutput); |
1419 | if (is_tessellation_shader()) |
1420 | stage_in_ptr_var_id = add_interface_block_pointer(ib_var_id: stage_in_var_id, storage: StorageClassInput); |
1421 | |
1422 | // Metal vertex functions that define no output must disable rasterization and return void. |
1423 | if (!stage_out_var_id) |
1424 | is_rasterization_disabled = true; |
1425 | |
1426 | // Convert the use of global variables to recursively-passed function parameters |
1427 | localize_global_variables(); |
1428 | extract_global_variables_from_functions(); |
1429 | |
1430 | // Mark any non-stage-in structs to be tightly packed. |
1431 | mark_packable_structs(); |
1432 | reorder_type_alias(); |
1433 | |
1434 | // Add fixup hooks required by shader inputs and outputs. This needs to happen before |
1435 | // the loop, so the hooks aren't added multiple times. |
1436 | fix_up_shader_inputs_outputs(); |
1437 | |
1438 | // If we are using argument buffers, we create argument buffer structures for them here. |
1439 | // These buffers will be used in the entry point, not the individual resources. |
1440 | if (msl_options.argument_buffers) |
1441 | { |
1442 | if (!msl_options.supports_msl_version(major: 2, minor: 0)) |
1443 | SPIRV_CROSS_THROW("Argument buffers can only be used with MSL 2.0 and up." ); |
1444 | analyze_argument_buffers(); |
1445 | } |
1446 | |
1447 | uint32_t pass_count = 0; |
1448 | do |
1449 | { |
1450 | reset(iteration_count: pass_count); |
1451 | |
1452 | // Start bindings at zero. |
1453 | next_metal_resource_index_buffer = 0; |
1454 | next_metal_resource_index_texture = 0; |
1455 | next_metal_resource_index_sampler = 0; |
1456 | for (auto &id : next_metal_resource_ids) |
1457 | id = 0; |
1458 | |
1459 | // Move constructor for this type is broken on GCC 4.9 ... |
1460 | buffer.reset(); |
1461 | |
1462 | emit_header(); |
1463 | emit_custom_templates(); |
1464 | emit_custom_functions(); |
1465 | emit_specialization_constants_and_structs(); |
1466 | emit_resources(); |
1467 | emit_function(func&: get<SPIRFunction>(id: ir.default_entry_point), return_flags: Bitset()); |
1468 | |
1469 | pass_count++; |
1470 | } while (is_forcing_recompilation()); |
1471 | |
1472 | return buffer.str(); |
1473 | } |
1474 | |
1475 | // Register the need to output any custom functions. |
1476 | void CompilerMSL::preprocess_op_codes() |
1477 | { |
1478 | OpCodePreprocessor preproc(*this); |
1479 | traverse_all_reachable_opcodes(block: get<SPIRFunction>(id: ir.default_entry_point), handler&: preproc); |
1480 | |
1481 | suppress_missing_prototypes = preproc.suppress_missing_prototypes; |
1482 | |
1483 | if (preproc.uses_atomics) |
1484 | { |
1485 | add_header_line(str: "#include <metal_atomic>" ); |
1486 | add_pragma_line(line: "#pragma clang diagnostic ignored \"-Wunused-variable\"" ); |
1487 | } |
1488 | |
1489 | // Before MSL 2.1 (2.2 for textures), Metal vertex functions that write to |
1490 | // resources must disable rasterization and return void. |
1491 | if (preproc.uses_resource_write) |
1492 | is_rasterization_disabled = true; |
1493 | |
1494 | // Tessellation control shaders are run as compute functions in Metal, and so |
1495 | // must capture their output to a buffer. |
1496 | if (get_execution_model() == ExecutionModelTessellationControl || |
1497 | (get_execution_model() == ExecutionModelVertex && msl_options.vertex_for_tessellation)) |
1498 | { |
1499 | is_rasterization_disabled = true; |
1500 | capture_output_to_buffer = true; |
1501 | } |
1502 | |
1503 | if (preproc.needs_subgroup_invocation_id) |
1504 | needs_subgroup_invocation_id = true; |
1505 | if (preproc.needs_subgroup_size) |
1506 | needs_subgroup_size = true; |
1507 | // build_implicit_builtins() hasn't run yet, and in fact, this needs to execute |
1508 | // before then so that gl_SampleID will get added; so we also need to check if |
1509 | // that function would add gl_FragCoord. |
1510 | if (preproc.needs_sample_id || msl_options.force_sample_rate_shading || |
1511 | (is_sample_rate() && (active_input_builtins.get(bit: BuiltInFragCoord) || |
1512 | (need_subpass_input && !msl_options.use_framebuffer_fetch_subpasses)))) |
1513 | needs_sample_id = true; |
1514 | |
1515 | if (is_intersection_query()) |
1516 | { |
1517 | add_header_line(str: "#if __METAL_VERSION__ >= 230" ); |
1518 | add_header_line(str: "#include <metal_raytracing>" ); |
1519 | add_header_line(str: "using namespace metal::raytracing;" ); |
1520 | add_header_line(str: "#endif" ); |
1521 | } |
1522 | } |
1523 | |
1524 | // Move the Private and Workgroup global variables to the entry function. |
1525 | // Non-constant variables cannot have global scope in Metal. |
1526 | void CompilerMSL::localize_global_variables() |
1527 | { |
1528 | auto &entry_func = get<SPIRFunction>(id: ir.default_entry_point); |
1529 | auto iter = global_variables.begin(); |
1530 | while (iter != global_variables.end()) |
1531 | { |
1532 | uint32_t v_id = *iter; |
1533 | auto &var = get<SPIRVariable>(id: v_id); |
1534 | if (var.storage == StorageClassPrivate || var.storage == StorageClassWorkgroup) |
1535 | { |
1536 | if (!variable_is_lut(var)) |
1537 | entry_func.add_local_variable(id: v_id); |
1538 | iter = global_variables.erase(itr: iter); |
1539 | } |
1540 | else |
1541 | iter++; |
1542 | } |
1543 | } |
1544 | |
1545 | // For any global variable accessed directly by a function, |
1546 | // extract that variable and add it as an argument to that function. |
1547 | void CompilerMSL::() |
1548 | { |
1549 | // Uniforms |
1550 | unordered_set<uint32_t> global_var_ids; |
1551 | ir.for_each_typed_id<SPIRVariable>(op: [&](uint32_t, SPIRVariable &var) { |
1552 | // Some builtins resolve directly to a function call which does not need any declared variables. |
1553 | // Skip these. |
1554 | if (var.storage == StorageClassInput && has_decoration(id: var.self, decoration: DecorationBuiltIn) && |
1555 | BuiltIn(get_decoration(id: var.self, decoration: DecorationBuiltIn)) == BuiltInHelperInvocation) |
1556 | { |
1557 | return; |
1558 | } |
1559 | |
1560 | if (var.storage == StorageClassInput || var.storage == StorageClassOutput || |
1561 | var.storage == StorageClassUniform || var.storage == StorageClassUniformConstant || |
1562 | var.storage == StorageClassPushConstant || var.storage == StorageClassStorageBuffer) |
1563 | { |
1564 | global_var_ids.insert(x: var.self); |
1565 | } |
1566 | }); |
1567 | |
1568 | // Local vars that are declared in the main function and accessed directly by a function |
1569 | auto &entry_func = get<SPIRFunction>(id: ir.default_entry_point); |
1570 | for (auto &var : entry_func.local_variables) |
1571 | if (get<SPIRVariable>(id: var).storage != StorageClassFunction) |
1572 | global_var_ids.insert(x: var); |
1573 | |
1574 | std::set<uint32_t> added_arg_ids; |
1575 | unordered_set<uint32_t> processed_func_ids; |
1576 | extract_global_variables_from_function(func_id: ir.default_entry_point, added_arg_ids, global_var_ids, processed_func_ids); |
1577 | } |
1578 | |
1579 | // MSL does not support the use of global variables for shader input content. |
1580 | // For any global variable accessed directly by the specified function, extract that variable, |
1581 | // add it as an argument to that function, and the arg to the added_arg_ids collection. |
1582 | void CompilerMSL::(uint32_t func_id, std::set<uint32_t> &added_arg_ids, |
1583 | unordered_set<uint32_t> &global_var_ids, |
1584 | unordered_set<uint32_t> &processed_func_ids) |
1585 | { |
1586 | // Avoid processing a function more than once |
1587 | if (processed_func_ids.find(x: func_id) != processed_func_ids.end()) |
1588 | { |
1589 | // Return function global variables |
1590 | added_arg_ids = function_global_vars[func_id]; |
1591 | return; |
1592 | } |
1593 | |
1594 | processed_func_ids.insert(x: func_id); |
1595 | |
1596 | auto &func = get<SPIRFunction>(id: func_id); |
1597 | |
1598 | // Recursively establish global args added to functions on which we depend. |
1599 | for (auto block : func.blocks) |
1600 | { |
1601 | auto &b = get<SPIRBlock>(id: block); |
1602 | for (auto &i : b.ops) |
1603 | { |
1604 | auto ops = stream(instr: i); |
1605 | auto op = static_cast<Op>(i.op); |
1606 | |
1607 | switch (op) |
1608 | { |
1609 | case OpLoad: |
1610 | case OpInBoundsAccessChain: |
1611 | case OpAccessChain: |
1612 | case OpPtrAccessChain: |
1613 | case OpArrayLength: |
1614 | { |
1615 | uint32_t base_id = ops[2]; |
1616 | if (global_var_ids.find(x: base_id) != global_var_ids.end()) |
1617 | added_arg_ids.insert(x: base_id); |
1618 | |
1619 | // Use Metal's native frame-buffer fetch API for subpass inputs. |
1620 | auto &type = get<SPIRType>(id: ops[0]); |
1621 | if (type.basetype == SPIRType::Image && type.image.dim == DimSubpassData && |
1622 | (!msl_options.use_framebuffer_fetch_subpasses)) |
1623 | { |
1624 | // Implicitly reads gl_FragCoord. |
1625 | assert(builtin_frag_coord_id != 0); |
1626 | added_arg_ids.insert(x: builtin_frag_coord_id); |
1627 | if (msl_options.multiview) |
1628 | { |
1629 | // Implicitly reads gl_ViewIndex. |
1630 | assert(builtin_view_idx_id != 0); |
1631 | added_arg_ids.insert(x: builtin_view_idx_id); |
1632 | } |
1633 | else if (msl_options.arrayed_subpass_input) |
1634 | { |
1635 | // Implicitly reads gl_Layer. |
1636 | assert(builtin_layer_id != 0); |
1637 | added_arg_ids.insert(x: builtin_layer_id); |
1638 | } |
1639 | } |
1640 | |
1641 | break; |
1642 | } |
1643 | |
1644 | case OpFunctionCall: |
1645 | { |
1646 | // First see if any of the function call args are globals |
1647 | for (uint32_t arg_idx = 3; arg_idx < i.length; arg_idx++) |
1648 | { |
1649 | uint32_t arg_id = ops[arg_idx]; |
1650 | if (global_var_ids.find(x: arg_id) != global_var_ids.end()) |
1651 | added_arg_ids.insert(x: arg_id); |
1652 | } |
1653 | |
1654 | // Then recurse into the function itself to extract globals used internally in the function |
1655 | uint32_t inner_func_id = ops[2]; |
1656 | std::set<uint32_t> inner_func_args; |
1657 | extract_global_variables_from_function(func_id: inner_func_id, added_arg_ids&: inner_func_args, global_var_ids, |
1658 | processed_func_ids); |
1659 | added_arg_ids.insert(first: inner_func_args.begin(), last: inner_func_args.end()); |
1660 | break; |
1661 | } |
1662 | |
1663 | case OpStore: |
1664 | { |
1665 | uint32_t base_id = ops[0]; |
1666 | if (global_var_ids.find(x: base_id) != global_var_ids.end()) |
1667 | added_arg_ids.insert(x: base_id); |
1668 | |
1669 | uint32_t rvalue_id = ops[1]; |
1670 | if (global_var_ids.find(x: rvalue_id) != global_var_ids.end()) |
1671 | added_arg_ids.insert(x: rvalue_id); |
1672 | |
1673 | break; |
1674 | } |
1675 | |
1676 | case OpSelect: |
1677 | { |
1678 | uint32_t base_id = ops[3]; |
1679 | if (global_var_ids.find(x: base_id) != global_var_ids.end()) |
1680 | added_arg_ids.insert(x: base_id); |
1681 | base_id = ops[4]; |
1682 | if (global_var_ids.find(x: base_id) != global_var_ids.end()) |
1683 | added_arg_ids.insert(x: base_id); |
1684 | break; |
1685 | } |
1686 | |
1687 | // Emulate texture2D atomic operations |
1688 | case OpImageTexelPointer: |
1689 | { |
1690 | // When using the pointer, we need to know which variable it is actually loaded from. |
1691 | uint32_t base_id = ops[2]; |
1692 | auto *var = maybe_get_backing_variable(chain: base_id); |
1693 | if (var && atomic_image_vars.count(x: var->self)) |
1694 | { |
1695 | if (global_var_ids.find(x: base_id) != global_var_ids.end()) |
1696 | added_arg_ids.insert(x: base_id); |
1697 | } |
1698 | break; |
1699 | } |
1700 | |
1701 | case OpExtInst: |
1702 | { |
1703 | uint32_t extension_set = ops[2]; |
1704 | if (get<SPIRExtension>(id: extension_set).ext == SPIRExtension::GLSL) |
1705 | { |
1706 | auto op_450 = static_cast<GLSLstd450>(ops[3]); |
1707 | switch (op_450) |
1708 | { |
1709 | case GLSLstd450InterpolateAtCentroid: |
1710 | case GLSLstd450InterpolateAtSample: |
1711 | case GLSLstd450InterpolateAtOffset: |
1712 | { |
1713 | // For these, we really need the stage-in block. It is theoretically possible to pass the |
1714 | // interpolant object, but a) doing so would require us to create an entirely new variable |
1715 | // with Interpolant type, and b) if we have a struct or array, handling all the members and |
1716 | // elements could get unwieldy fast. |
1717 | added_arg_ids.insert(x: stage_in_var_id); |
1718 | break; |
1719 | } |
1720 | |
1721 | case GLSLstd450Modf: |
1722 | case GLSLstd450Frexp: |
1723 | { |
1724 | uint32_t base_id = ops[5]; |
1725 | if (global_var_ids.find(x: base_id) != global_var_ids.end()) |
1726 | added_arg_ids.insert(x: base_id); |
1727 | break; |
1728 | } |
1729 | |
1730 | default: |
1731 | break; |
1732 | } |
1733 | } |
1734 | break; |
1735 | } |
1736 | |
1737 | case OpGroupNonUniformInverseBallot: |
1738 | { |
1739 | added_arg_ids.insert(x: builtin_subgroup_invocation_id_id); |
1740 | break; |
1741 | } |
1742 | |
1743 | case OpGroupNonUniformBallotFindLSB: |
1744 | case OpGroupNonUniformBallotFindMSB: |
1745 | { |
1746 | added_arg_ids.insert(x: builtin_subgroup_size_id); |
1747 | break; |
1748 | } |
1749 | |
1750 | case OpGroupNonUniformBallotBitCount: |
1751 | { |
1752 | auto operation = static_cast<GroupOperation>(ops[3]); |
1753 | switch (operation) |
1754 | { |
1755 | case GroupOperationReduce: |
1756 | added_arg_ids.insert(x: builtin_subgroup_size_id); |
1757 | break; |
1758 | case GroupOperationInclusiveScan: |
1759 | case GroupOperationExclusiveScan: |
1760 | added_arg_ids.insert(x: builtin_subgroup_invocation_id_id); |
1761 | break; |
1762 | default: |
1763 | break; |
1764 | } |
1765 | break; |
1766 | } |
1767 | |
1768 | case OpRayQueryInitializeKHR: |
1769 | case OpRayQueryProceedKHR: |
1770 | case OpRayQueryTerminateKHR: |
1771 | case OpRayQueryGenerateIntersectionKHR: |
1772 | case OpRayQueryConfirmIntersectionKHR: |
1773 | { |
1774 | // Ray query accesses memory directly, need check pass down object if using Private storage class. |
1775 | uint32_t base_id = ops[0]; |
1776 | if (global_var_ids.find(x: base_id) != global_var_ids.end()) |
1777 | added_arg_ids.insert(x: base_id); |
1778 | break; |
1779 | } |
1780 | |
1781 | case OpRayQueryGetRayTMinKHR: |
1782 | case OpRayQueryGetRayFlagsKHR: |
1783 | case OpRayQueryGetWorldRayOriginKHR: |
1784 | case OpRayQueryGetWorldRayDirectionKHR: |
1785 | case OpRayQueryGetIntersectionCandidateAABBOpaqueKHR: |
1786 | case OpRayQueryGetIntersectionTypeKHR: |
1787 | case OpRayQueryGetIntersectionTKHR: |
1788 | case OpRayQueryGetIntersectionInstanceCustomIndexKHR: |
1789 | case OpRayQueryGetIntersectionInstanceIdKHR: |
1790 | case OpRayQueryGetIntersectionInstanceShaderBindingTableRecordOffsetKHR: |
1791 | case OpRayQueryGetIntersectionGeometryIndexKHR: |
1792 | case OpRayQueryGetIntersectionPrimitiveIndexKHR: |
1793 | case OpRayQueryGetIntersectionBarycentricsKHR: |
1794 | case OpRayQueryGetIntersectionFrontFaceKHR: |
1795 | case OpRayQueryGetIntersectionObjectRayDirectionKHR: |
1796 | case OpRayQueryGetIntersectionObjectRayOriginKHR: |
1797 | case OpRayQueryGetIntersectionObjectToWorldKHR: |
1798 | case OpRayQueryGetIntersectionWorldToObjectKHR: |
1799 | { |
1800 | // Ray query accesses memory directly, need check pass down object if using Private storage class. |
1801 | uint32_t base_id = ops[2]; |
1802 | if (global_var_ids.find(x: base_id) != global_var_ids.end()) |
1803 | added_arg_ids.insert(x: base_id); |
1804 | break; |
1805 | } |
1806 | |
1807 | default: |
1808 | break; |
1809 | } |
1810 | |
1811 | // TODO: Add all other operations which can affect memory. |
1812 | // We should consider a more unified system here to reduce boiler-plate. |
1813 | // This kind of analysis is done in several places ... |
1814 | } |
1815 | } |
1816 | |
1817 | function_global_vars[func_id] = added_arg_ids; |
1818 | |
1819 | // Add the global variables as arguments to the function |
1820 | if (func_id != ir.default_entry_point) |
1821 | { |
1822 | bool control_point_added_in = false; |
1823 | bool control_point_added_out = false; |
1824 | bool patch_added_in = false; |
1825 | bool patch_added_out = false; |
1826 | |
1827 | for (uint32_t arg_id : added_arg_ids) |
1828 | { |
1829 | auto &var = get<SPIRVariable>(id: arg_id); |
1830 | uint32_t type_id = var.basetype; |
1831 | auto *p_type = &get<SPIRType>(id: type_id); |
1832 | BuiltIn bi_type = BuiltIn(get_decoration(id: arg_id, decoration: DecorationBuiltIn)); |
1833 | |
1834 | bool is_patch = has_decoration(id: arg_id, decoration: DecorationPatch) || is_patch_block(type: *p_type); |
1835 | bool is_block = has_decoration(id: p_type->self, decoration: DecorationBlock); |
1836 | bool is_control_point_storage = |
1837 | !is_patch && |
1838 | ((is_tessellation_shader() && var.storage == StorageClassInput) || |
1839 | (get_execution_model() == ExecutionModelTessellationControl && var.storage == StorageClassOutput)); |
1840 | bool is_patch_block_storage = is_patch && is_block && var.storage == StorageClassOutput; |
1841 | bool is_builtin = is_builtin_variable(var); |
1842 | bool variable_is_stage_io = |
1843 | !is_builtin || bi_type == BuiltInPosition || bi_type == BuiltInPointSize || |
1844 | bi_type == BuiltInClipDistance || bi_type == BuiltInCullDistance || |
1845 | p_type->basetype == SPIRType::Struct; |
1846 | bool is_redirected_to_global_stage_io = (is_control_point_storage || is_patch_block_storage) && |
1847 | variable_is_stage_io; |
1848 | |
1849 | // If output is masked it is not considered part of the global stage IO interface. |
1850 | if (is_redirected_to_global_stage_io && var.storage == StorageClassOutput) |
1851 | is_redirected_to_global_stage_io = !is_stage_output_variable_masked(var); |
1852 | |
1853 | if (is_redirected_to_global_stage_io) |
1854 | { |
1855 | // Tessellation control shaders see inputs and per-vertex outputs as arrays. |
1856 | // Similarly, tessellation evaluation shaders see per-vertex inputs as arrays. |
1857 | // We collected them into a structure; we must pass the array of this |
1858 | // structure to the function. |
1859 | std::string name; |
1860 | if (is_patch) |
1861 | name = var.storage == StorageClassInput ? patch_stage_in_var_name : patch_stage_out_var_name; |
1862 | else |
1863 | name = var.storage == StorageClassInput ? "gl_in" : "gl_out" ; |
1864 | |
1865 | if (var.storage == StorageClassOutput && has_decoration(id: p_type->self, decoration: DecorationBlock)) |
1866 | { |
1867 | // If we're redirecting a block, we might still need to access the original block |
1868 | // variable if we're masking some members. |
1869 | for (uint32_t mbr_idx = 0; mbr_idx < uint32_t(p_type->member_types.size()); mbr_idx++) |
1870 | { |
1871 | if (is_stage_output_block_member_masked(var, index: mbr_idx, strip_array: true)) |
1872 | { |
1873 | func.add_parameter(parameter_type: var.basetype, id: var.self, alias_global_variable: true); |
1874 | break; |
1875 | } |
1876 | } |
1877 | } |
1878 | |
1879 | // Tessellation control shaders see inputs and per-vertex outputs as arrays. |
1880 | // Similarly, tessellation evaluation shaders see per-vertex inputs as arrays. |
1881 | // We collected them into a structure; we must pass the array of this |
1882 | // structure to the function. |
1883 | if (var.storage == StorageClassInput) |
1884 | { |
1885 | auto &added_in = is_patch ? patch_added_in : control_point_added_in; |
1886 | if (added_in) |
1887 | continue; |
1888 | arg_id = is_patch ? patch_stage_in_var_id : stage_in_ptr_var_id; |
1889 | added_in = true; |
1890 | } |
1891 | else if (var.storage == StorageClassOutput) |
1892 | { |
1893 | auto &added_out = is_patch ? patch_added_out : control_point_added_out; |
1894 | if (added_out) |
1895 | continue; |
1896 | arg_id = is_patch ? patch_stage_out_var_id : stage_out_ptr_var_id; |
1897 | added_out = true; |
1898 | } |
1899 | |
1900 | type_id = get<SPIRVariable>(id: arg_id).basetype; |
1901 | uint32_t next_id = ir.increase_bound_by(count: 1); |
1902 | func.add_parameter(parameter_type: type_id, id: next_id, alias_global_variable: true); |
1903 | set<SPIRVariable>(id: next_id, args&: type_id, args: StorageClassFunction, args: 0, args&: arg_id); |
1904 | |
1905 | set_name(id: next_id, name); |
1906 | } |
1907 | else if (is_builtin && has_decoration(id: p_type->self, decoration: DecorationBlock)) |
1908 | { |
1909 | // Get the pointee type |
1910 | type_id = get_pointee_type_id(type_id); |
1911 | p_type = &get<SPIRType>(id: type_id); |
1912 | |
1913 | uint32_t mbr_idx = 0; |
1914 | for (auto &mbr_type_id : p_type->member_types) |
1915 | { |
1916 | BuiltIn builtin = BuiltInMax; |
1917 | is_builtin = is_member_builtin(type: *p_type, index: mbr_idx, builtin: &builtin); |
1918 | if (is_builtin && has_active_builtin(builtin, storage: var.storage)) |
1919 | { |
1920 | // Add a arg variable with the same type and decorations as the member |
1921 | uint32_t next_ids = ir.increase_bound_by(count: 2); |
1922 | uint32_t ptr_type_id = next_ids + 0; |
1923 | uint32_t var_id = next_ids + 1; |
1924 | |
1925 | // Make sure we have an actual pointer type, |
1926 | // so that we will get the appropriate address space when declaring these builtins. |
1927 | auto &ptr = set<SPIRType>(id: ptr_type_id, args&: get<SPIRType>(id: mbr_type_id)); |
1928 | ptr.self = mbr_type_id; |
1929 | ptr.storage = var.storage; |
1930 | ptr.pointer = true; |
1931 | ptr.pointer_depth++; |
1932 | ptr.parent_type = mbr_type_id; |
1933 | |
1934 | func.add_parameter(parameter_type: mbr_type_id, id: var_id, alias_global_variable: true); |
1935 | set<SPIRVariable>(id: var_id, args&: ptr_type_id, args: StorageClassFunction); |
1936 | ir.meta[var_id].decoration = ir.meta[type_id].members[mbr_idx]; |
1937 | } |
1938 | mbr_idx++; |
1939 | } |
1940 | } |
1941 | else |
1942 | { |
1943 | uint32_t next_id = ir.increase_bound_by(count: 1); |
1944 | func.add_parameter(parameter_type: type_id, id: next_id, alias_global_variable: true); |
1945 | set<SPIRVariable>(id: next_id, args&: type_id, args: StorageClassFunction, args: 0, args&: arg_id); |
1946 | |
1947 | // Ensure the existing variable has a valid name and the new variable has all the same meta info |
1948 | set_name(id: arg_id, name: ensure_valid_name(name: to_name(id: arg_id), pfx: "v" )); |
1949 | ir.meta[next_id] = ir.meta[arg_id]; |
1950 | } |
1951 | } |
1952 | } |
1953 | } |
1954 | |
1955 | // For all variables that are some form of non-input-output interface block, mark that all the structs |
1956 | // that are recursively contained within the type referenced by that variable should be packed tightly. |
1957 | void CompilerMSL::mark_packable_structs() |
1958 | { |
1959 | ir.for_each_typed_id<SPIRVariable>(op: [&](uint32_t, SPIRVariable &var) { |
1960 | if (var.storage != StorageClassFunction && !is_hidden_variable(var)) |
1961 | { |
1962 | auto &type = this->get<SPIRType>(id: var.basetype); |
1963 | if (type.pointer && |
1964 | (type.storage == StorageClassUniform || type.storage == StorageClassUniformConstant || |
1965 | type.storage == StorageClassPushConstant || type.storage == StorageClassStorageBuffer) && |
1966 | (has_decoration(id: type.self, decoration: DecorationBlock) || has_decoration(id: type.self, decoration: DecorationBufferBlock))) |
1967 | mark_as_packable(type); |
1968 | } |
1969 | }); |
1970 | } |
1971 | |
1972 | // If the specified type is a struct, it and any nested structs |
1973 | // are marked as packable with the SPIRVCrossDecorationBufferBlockRepacked decoration, |
1974 | void CompilerMSL::mark_as_packable(SPIRType &type) |
1975 | { |
1976 | // If this is not the base type (eg. it's a pointer or array), tunnel down |
1977 | if (type.parent_type) |
1978 | { |
1979 | mark_as_packable(type&: get<SPIRType>(id: type.parent_type)); |
1980 | return; |
1981 | } |
1982 | |
1983 | if (type.basetype == SPIRType::Struct) |
1984 | { |
1985 | set_extended_decoration(id: type.self, decoration: SPIRVCrossDecorationBufferBlockRepacked); |
1986 | |
1987 | // Recurse |
1988 | uint32_t mbr_cnt = uint32_t(type.member_types.size()); |
1989 | for (uint32_t mbr_idx = 0; mbr_idx < mbr_cnt; mbr_idx++) |
1990 | { |
1991 | uint32_t mbr_type_id = type.member_types[mbr_idx]; |
1992 | auto &mbr_type = get<SPIRType>(id: mbr_type_id); |
1993 | mark_as_packable(type&: mbr_type); |
1994 | if (mbr_type.type_alias) |
1995 | { |
1996 | auto &mbr_type_alias = get<SPIRType>(id: mbr_type.type_alias); |
1997 | mark_as_packable(type&: mbr_type_alias); |
1998 | } |
1999 | } |
2000 | } |
2001 | } |
2002 | |
2003 | // If a shader input exists at the location, it is marked as being used by this shader |
2004 | void CompilerMSL::mark_location_as_used_by_shader(uint32_t location, const SPIRType &type, |
2005 | StorageClass storage, bool fallback) |
2006 | { |
2007 | if (storage != StorageClassInput) |
2008 | return; |
2009 | |
2010 | uint32_t count = type_to_location_count(type); |
2011 | for (uint32_t i = 0; i < count; i++) |
2012 | { |
2013 | location_inputs_in_use.insert(x: location + i); |
2014 | if (fallback) |
2015 | location_inputs_in_use_fallback.insert(x: location + i); |
2016 | } |
2017 | } |
2018 | |
2019 | uint32_t CompilerMSL::get_target_components_for_fragment_location(uint32_t location) const |
2020 | { |
2021 | auto itr = fragment_output_components.find(x: location); |
2022 | if (itr == end(cont: fragment_output_components)) |
2023 | return 4; |
2024 | else |
2025 | return itr->second; |
2026 | } |
2027 | |
2028 | uint32_t CompilerMSL::build_extended_vector_type(uint32_t type_id, uint32_t components, SPIRType::BaseType basetype) |
2029 | { |
2030 | uint32_t new_type_id = ir.increase_bound_by(count: 1); |
2031 | auto &old_type = get<SPIRType>(id: type_id); |
2032 | auto *type = &set<SPIRType>(id: new_type_id, args&: old_type); |
2033 | type->vecsize = components; |
2034 | if (basetype != SPIRType::Unknown) |
2035 | type->basetype = basetype; |
2036 | type->self = new_type_id; |
2037 | type->parent_type = type_id; |
2038 | type->array.clear(); |
2039 | type->array_size_literal.clear(); |
2040 | type->pointer = false; |
2041 | |
2042 | if (is_array(type: old_type)) |
2043 | { |
2044 | uint32_t array_type_id = ir.increase_bound_by(count: 1); |
2045 | type = &set<SPIRType>(id: array_type_id, args&: *type); |
2046 | type->parent_type = new_type_id; |
2047 | type->array = old_type.array; |
2048 | type->array_size_literal = old_type.array_size_literal; |
2049 | new_type_id = array_type_id; |
2050 | } |
2051 | |
2052 | if (old_type.pointer) |
2053 | { |
2054 | uint32_t ptr_type_id = ir.increase_bound_by(count: 1); |
2055 | type = &set<SPIRType>(id: ptr_type_id, args&: *type); |
2056 | type->self = new_type_id; |
2057 | type->parent_type = new_type_id; |
2058 | type->storage = old_type.storage; |
2059 | type->pointer = true; |
2060 | type->pointer_depth++; |
2061 | new_type_id = ptr_type_id; |
2062 | } |
2063 | |
2064 | return new_type_id; |
2065 | } |
2066 | |
2067 | uint32_t CompilerMSL::build_msl_interpolant_type(uint32_t type_id, bool is_noperspective) |
2068 | { |
2069 | uint32_t new_type_id = ir.increase_bound_by(count: 1); |
2070 | SPIRType &type = set<SPIRType>(id: new_type_id, args&: get<SPIRType>(id: type_id)); |
2071 | type.basetype = SPIRType::Interpolant; |
2072 | type.parent_type = type_id; |
2073 | // In Metal, the pull-model interpolant type encodes perspective-vs-no-perspective in the type itself. |
2074 | // Add this decoration so we know which argument to pass to the template. |
2075 | if (is_noperspective) |
2076 | set_decoration(id: new_type_id, decoration: DecorationNoPerspective); |
2077 | return new_type_id; |
2078 | } |
2079 | |
2080 | bool CompilerMSL::add_component_variable_to_interface_block(spv::StorageClass storage, const std::string &ib_var_ref, |
2081 | SPIRVariable &var, |
2082 | const SPIRType &type, |
2083 | InterfaceBlockMeta &meta) |
2084 | { |
2085 | // Deal with Component decorations. |
2086 | const InterfaceBlockMeta::LocationMeta *location_meta = nullptr; |
2087 | uint32_t location = ~0u; |
2088 | if (has_decoration(id: var.self, decoration: DecorationLocation)) |
2089 | { |
2090 | location = get_decoration(id: var.self, decoration: DecorationLocation); |
2091 | auto location_meta_itr = meta.location_meta.find(x: location); |
2092 | if (location_meta_itr != end(cont&: meta.location_meta)) |
2093 | location_meta = &location_meta_itr->second; |
2094 | } |
2095 | |
2096 | // Check if we need to pad fragment output to match a certain number of components. |
2097 | if (location_meta) |
2098 | { |
2099 | bool pad_fragment_output = has_decoration(id: var.self, decoration: DecorationLocation) && |
2100 | msl_options.pad_fragment_output_components && |
2101 | get_entry_point().model == ExecutionModelFragment && storage == StorageClassOutput; |
2102 | |
2103 | auto &entry_func = get<SPIRFunction>(id: ir.default_entry_point); |
2104 | uint32_t start_component = get_decoration(id: var.self, decoration: DecorationComponent); |
2105 | uint32_t type_components = type.vecsize; |
2106 | uint32_t num_components = location_meta->num_components; |
2107 | |
2108 | if (pad_fragment_output) |
2109 | { |
2110 | uint32_t locn = get_decoration(id: var.self, decoration: DecorationLocation); |
2111 | num_components = std::max(a: num_components, b: get_target_components_for_fragment_location(location: locn)); |
2112 | } |
2113 | |
2114 | // We have already declared an IO block member as m_location_N. |
2115 | // Just emit an early-declared variable and fixup as needed. |
2116 | // Arrays need to be unrolled here since each location might need a different number of components. |
2117 | entry_func.add_local_variable(id: var.self); |
2118 | vars_needing_early_declaration.push_back(t: var.self); |
2119 | |
2120 | if (var.storage == StorageClassInput) |
2121 | { |
2122 | entry_func.fixup_hooks_in.push_back(t: [=, &type, &var]() { |
2123 | if (!type.array.empty()) |
2124 | { |
2125 | uint32_t array_size = to_array_size_literal(type); |
2126 | for (uint32_t loc_off = 0; loc_off < array_size; loc_off++) |
2127 | { |
2128 | statement(ts: to_name(id: var.self), ts: "[" , ts&: loc_off, ts: "]" , ts: " = " , ts: ib_var_ref, |
2129 | ts: ".m_location_" , ts: location + loc_off, |
2130 | ts: vector_swizzle(vecsize: type_components, index: start_component), ts: ";" ); |
2131 | } |
2132 | } |
2133 | else |
2134 | { |
2135 | statement(ts: to_name(id: var.self), ts: " = " , ts: ib_var_ref, ts: ".m_location_" , ts: location, |
2136 | ts: vector_swizzle(vecsize: type_components, index: start_component), ts: ";" ); |
2137 | } |
2138 | }); |
2139 | } |
2140 | else |
2141 | { |
2142 | entry_func.fixup_hooks_out.push_back(t: [=, &type, &var]() { |
2143 | if (!type.array.empty()) |
2144 | { |
2145 | uint32_t array_size = to_array_size_literal(type); |
2146 | for (uint32_t loc_off = 0; loc_off < array_size; loc_off++) |
2147 | { |
2148 | statement(ts: ib_var_ref, ts: ".m_location_" , ts: location + loc_off, |
2149 | ts: vector_swizzle(vecsize: type_components, index: start_component), ts: " = " , |
2150 | ts: to_name(id: var.self), ts: "[" , ts&: loc_off, ts: "];" ); |
2151 | } |
2152 | } |
2153 | else |
2154 | { |
2155 | statement(ts: ib_var_ref, ts: ".m_location_" , ts: location, |
2156 | ts: vector_swizzle(vecsize: type_components, index: start_component), ts: " = " , ts: to_name(id: var.self), ts: ";" ); |
2157 | } |
2158 | }); |
2159 | } |
2160 | return true; |
2161 | } |
2162 | else |
2163 | return false; |
2164 | } |
2165 | |
2166 | void CompilerMSL::add_plain_variable_to_interface_block(StorageClass storage, const string &ib_var_ref, |
2167 | SPIRType &ib_type, SPIRVariable &var, InterfaceBlockMeta &meta) |
2168 | { |
2169 | bool is_builtin = is_builtin_variable(var); |
2170 | BuiltIn builtin = BuiltIn(get_decoration(id: var.self, decoration: DecorationBuiltIn)); |
2171 | bool is_flat = has_decoration(id: var.self, decoration: DecorationFlat); |
2172 | bool is_noperspective = has_decoration(id: var.self, decoration: DecorationNoPerspective); |
2173 | bool is_centroid = has_decoration(id: var.self, decoration: DecorationCentroid); |
2174 | bool is_sample = has_decoration(id: var.self, decoration: DecorationSample); |
2175 | |
2176 | // Add a reference to the variable type to the interface struct. |
2177 | uint32_t ib_mbr_idx = uint32_t(ib_type.member_types.size()); |
2178 | uint32_t type_id = ensure_correct_builtin_type(type_id: var.basetype, builtin); |
2179 | var.basetype = type_id; |
2180 | |
2181 | type_id = get_pointee_type_id(type_id: var.basetype); |
2182 | if (meta.strip_array && is_array(type: get<SPIRType>(id: type_id))) |
2183 | type_id = get<SPIRType>(id: type_id).parent_type; |
2184 | auto &type = get<SPIRType>(id: type_id); |
2185 | uint32_t target_components = 0; |
2186 | uint32_t type_components = type.vecsize; |
2187 | |
2188 | bool padded_output = false; |
2189 | bool padded_input = false; |
2190 | uint32_t start_component = 0; |
2191 | |
2192 | auto &entry_func = get<SPIRFunction>(id: ir.default_entry_point); |
2193 | |
2194 | if (add_component_variable_to_interface_block(storage, ib_var_ref, var, type, meta)) |
2195 | return; |
2196 | |
2197 | bool pad_fragment_output = has_decoration(id: var.self, decoration: DecorationLocation) && |
2198 | msl_options.pad_fragment_output_components && |
2199 | get_entry_point().model == ExecutionModelFragment && storage == StorageClassOutput; |
2200 | |
2201 | if (pad_fragment_output) |
2202 | { |
2203 | uint32_t locn = get_decoration(id: var.self, decoration: DecorationLocation); |
2204 | target_components = get_target_components_for_fragment_location(location: locn); |
2205 | if (type_components < target_components) |
2206 | { |
2207 | // Make a new type here. |
2208 | type_id = build_extended_vector_type(type_id, components: target_components); |
2209 | padded_output = true; |
2210 | } |
2211 | } |
2212 | |
2213 | if (storage == StorageClassInput && pull_model_inputs.count(x: var.self)) |
2214 | ib_type.member_types.push_back(t: build_msl_interpolant_type(type_id, is_noperspective)); |
2215 | else |
2216 | ib_type.member_types.push_back(t: type_id); |
2217 | |
2218 | // Give the member a name |
2219 | string mbr_name = ensure_valid_name(name: to_expression(id: var.self), pfx: "m" ); |
2220 | set_member_name(id: ib_type.self, index: ib_mbr_idx, name: mbr_name); |
2221 | |
2222 | // Update the original variable reference to include the structure reference |
2223 | string qual_var_name = ib_var_ref + "." + mbr_name; |
2224 | // If using pull-model interpolation, need to add a call to the correct interpolation method. |
2225 | if (storage == StorageClassInput && pull_model_inputs.count(x: var.self)) |
2226 | { |
2227 | if (is_centroid) |
2228 | qual_var_name += ".interpolate_at_centroid()" ; |
2229 | else if (is_sample) |
2230 | qual_var_name += join(ts: ".interpolate_at_sample(" , ts: to_expression(id: builtin_sample_id_id), ts: ")" ); |
2231 | else |
2232 | qual_var_name += ".interpolate_at_center()" ; |
2233 | } |
2234 | |
2235 | if (padded_output || padded_input) |
2236 | { |
2237 | entry_func.add_local_variable(id: var.self); |
2238 | vars_needing_early_declaration.push_back(t: var.self); |
2239 | |
2240 | if (padded_output) |
2241 | { |
2242 | entry_func.fixup_hooks_out.push_back(t: [=, &var]() { |
2243 | statement(ts: qual_var_name, ts: vector_swizzle(vecsize: type_components, index: start_component), ts: " = " , ts: to_name(id: var.self), |
2244 | ts: ";" ); |
2245 | }); |
2246 | } |
2247 | else |
2248 | { |
2249 | entry_func.fixup_hooks_in.push_back(t: [=, &var]() { |
2250 | statement(ts: to_name(id: var.self), ts: " = " , ts: qual_var_name, ts: vector_swizzle(vecsize: type_components, index: start_component), |
2251 | ts: ";" ); |
2252 | }); |
2253 | } |
2254 | } |
2255 | else if (!meta.strip_array) |
2256 | ir.meta[var.self].decoration.qualified_alias = qual_var_name; |
2257 | |
2258 | if (var.storage == StorageClassOutput && var.initializer != ID(0)) |
2259 | { |
2260 | if (padded_output || padded_input) |
2261 | { |
2262 | entry_func.fixup_hooks_in.push_back( |
2263 | t: [=, &var]() { statement(ts: to_name(id: var.self), ts: " = " , ts: to_expression(id: var.initializer), ts: ";" ); }); |
2264 | } |
2265 | else |
2266 | { |
2267 | if (meta.strip_array) |
2268 | { |
2269 | entry_func.fixup_hooks_in.push_back(t: [=, &var]() { |
2270 | uint32_t index = get_extended_decoration(id: var.self, decoration: SPIRVCrossDecorationInterfaceMemberIndex); |
2271 | auto invocation = to_tesc_invocation_id(); |
2272 | statement(ts: to_expression(id: stage_out_ptr_var_id), ts: "[" , |
2273 | ts&: invocation, ts: "]." , |
2274 | ts: to_member_name(type: ib_type, index), ts: " = " , ts: to_expression(id: var.initializer), ts: "[" , |
2275 | ts&: invocation, ts: "];" ); |
2276 | }); |
2277 | } |
2278 | else |
2279 | { |
2280 | entry_func.fixup_hooks_in.push_back(t: [=, &var]() { |
2281 | statement(ts: qual_var_name, ts: " = " , ts: to_expression(id: var.initializer), ts: ";" ); |
2282 | }); |
2283 | } |
2284 | } |
2285 | } |
2286 | |
2287 | // Copy the variable location from the original variable to the member |
2288 | if (get_decoration_bitset(id: var.self).get(bit: DecorationLocation)) |
2289 | { |
2290 | uint32_t locn = get_decoration(id: var.self, decoration: DecorationLocation); |
2291 | uint32_t comp = get_decoration(id: var.self, decoration: DecorationComponent); |
2292 | if (storage == StorageClassInput) |
2293 | { |
2294 | type_id = ensure_correct_input_type(type_id: var.basetype, location: locn, component: comp, num_components: 0, strip_array: meta.strip_array); |
2295 | var.basetype = type_id; |
2296 | |
2297 | type_id = get_pointee_type_id(type_id); |
2298 | if (meta.strip_array && is_array(type: get<SPIRType>(id: type_id))) |
2299 | type_id = get<SPIRType>(id: type_id).parent_type; |
2300 | if (pull_model_inputs.count(x: var.self)) |
2301 | ib_type.member_types[ib_mbr_idx] = build_msl_interpolant_type(type_id, is_noperspective); |
2302 | else |
2303 | ib_type.member_types[ib_mbr_idx] = type_id; |
2304 | } |
2305 | set_member_decoration(id: ib_type.self, index: ib_mbr_idx, decoration: DecorationLocation, argument: locn); |
2306 | if (comp) |
2307 | set_member_decoration(id: ib_type.self, index: ib_mbr_idx, decoration: DecorationComponent, argument: comp); |
2308 | mark_location_as_used_by_shader(location: locn, type: get<SPIRType>(id: type_id), storage); |
2309 | } |
2310 | else if (is_builtin && is_tessellation_shader() && storage == StorageClassInput && inputs_by_builtin.count(x: builtin)) |
2311 | { |
2312 | uint32_t locn = inputs_by_builtin[builtin].location; |
2313 | set_member_decoration(id: ib_type.self, index: ib_mbr_idx, decoration: DecorationLocation, argument: locn); |
2314 | mark_location_as_used_by_shader(location: locn, type, storage); |
2315 | } |
2316 | |
2317 | if (get_decoration_bitset(id: var.self).get(bit: DecorationComponent)) |
2318 | { |
2319 | uint32_t component = get_decoration(id: var.self, decoration: DecorationComponent); |
2320 | set_member_decoration(id: ib_type.self, index: ib_mbr_idx, decoration: DecorationComponent, argument: component); |
2321 | } |
2322 | |
2323 | if (get_decoration_bitset(id: var.self).get(bit: DecorationIndex)) |
2324 | { |
2325 | uint32_t index = get_decoration(id: var.self, decoration: DecorationIndex); |
2326 | set_member_decoration(id: ib_type.self, index: ib_mbr_idx, decoration: DecorationIndex, argument: index); |
2327 | } |
2328 | |
2329 | // Mark the member as builtin if needed |
2330 | if (is_builtin) |
2331 | { |
2332 | set_member_decoration(id: ib_type.self, index: ib_mbr_idx, decoration: DecorationBuiltIn, argument: builtin); |
2333 | if (builtin == BuiltInPosition && storage == StorageClassOutput) |
2334 | qual_pos_var_name = qual_var_name; |
2335 | } |
2336 | |
2337 | // Copy interpolation decorations if needed |
2338 | if (storage != StorageClassInput || !pull_model_inputs.count(x: var.self)) |
2339 | { |
2340 | if (is_flat) |
2341 | set_member_decoration(id: ib_type.self, index: ib_mbr_idx, decoration: DecorationFlat); |
2342 | if (is_noperspective) |
2343 | set_member_decoration(id: ib_type.self, index: ib_mbr_idx, decoration: DecorationNoPerspective); |
2344 | if (is_centroid) |
2345 | set_member_decoration(id: ib_type.self, index: ib_mbr_idx, decoration: DecorationCentroid); |
2346 | if (is_sample) |
2347 | set_member_decoration(id: ib_type.self, index: ib_mbr_idx, decoration: DecorationSample); |
2348 | } |
2349 | |
2350 | set_extended_member_decoration(type: ib_type.self, index: ib_mbr_idx, decoration: SPIRVCrossDecorationInterfaceOrigID, value: var.self); |
2351 | } |
2352 | |
2353 | void CompilerMSL::add_composite_variable_to_interface_block(StorageClass storage, const string &ib_var_ref, |
2354 | SPIRType &ib_type, SPIRVariable &var, |
2355 | InterfaceBlockMeta &meta) |
2356 | { |
2357 | auto &entry_func = get<SPIRFunction>(id: ir.default_entry_point); |
2358 | auto &var_type = meta.strip_array ? get_variable_element_type(var) : get_variable_data_type(var); |
2359 | uint32_t elem_cnt = 0; |
2360 | |
2361 | if (add_component_variable_to_interface_block(storage, ib_var_ref, var, type: var_type, meta)) |
2362 | return; |
2363 | |
2364 | if (is_matrix(type: var_type)) |
2365 | { |
2366 | if (is_array(type: var_type)) |
2367 | SPIRV_CROSS_THROW("MSL cannot emit arrays-of-matrices in input and output variables." ); |
2368 | |
2369 | elem_cnt = var_type.columns; |
2370 | } |
2371 | else if (is_array(type: var_type)) |
2372 | { |
2373 | if (var_type.array.size() != 1) |
2374 | SPIRV_CROSS_THROW("MSL cannot emit arrays-of-arrays in input and output variables." ); |
2375 | |
2376 | elem_cnt = to_array_size_literal(type: var_type); |
2377 | } |
2378 | |
2379 | bool is_builtin = is_builtin_variable(var); |
2380 | BuiltIn builtin = BuiltIn(get_decoration(id: var.self, decoration: DecorationBuiltIn)); |
2381 | bool is_flat = has_decoration(id: var.self, decoration: DecorationFlat); |
2382 | bool is_noperspective = has_decoration(id: var.self, decoration: DecorationNoPerspective); |
2383 | bool is_centroid = has_decoration(id: var.self, decoration: DecorationCentroid); |
2384 | bool is_sample = has_decoration(id: var.self, decoration: DecorationSample); |
2385 | |
2386 | auto *usable_type = &var_type; |
2387 | if (usable_type->pointer) |
2388 | usable_type = &get<SPIRType>(id: usable_type->parent_type); |
2389 | while (is_array(type: *usable_type) || is_matrix(type: *usable_type)) |
2390 | usable_type = &get<SPIRType>(id: usable_type->parent_type); |
2391 | |
2392 | // If a builtin, force it to have the proper name. |
2393 | if (is_builtin) |
2394 | set_name(id: var.self, name: builtin_to_glsl(builtin, storage: StorageClassFunction)); |
2395 | |
2396 | bool flatten_from_ib_var = false; |
2397 | string flatten_from_ib_mbr_name; |
2398 | |
2399 | if (storage == StorageClassOutput && is_builtin && builtin == BuiltInClipDistance) |
2400 | { |
2401 | // Also declare [[clip_distance]] attribute here. |
2402 | uint32_t clip_array_mbr_idx = uint32_t(ib_type.member_types.size()); |
2403 | ib_type.member_types.push_back(t: get_variable_data_type_id(var)); |
2404 | set_member_decoration(id: ib_type.self, index: clip_array_mbr_idx, decoration: DecorationBuiltIn, argument: BuiltInClipDistance); |
2405 | |
2406 | flatten_from_ib_mbr_name = builtin_to_glsl(builtin: BuiltInClipDistance, storage: StorageClassOutput); |
2407 | set_member_name(id: ib_type.self, index: clip_array_mbr_idx, name: flatten_from_ib_mbr_name); |
2408 | |
2409 | // When we flatten, we flatten directly from the "out" struct, |
2410 | // not from a function variable. |
2411 | flatten_from_ib_var = true; |
2412 | |
2413 | if (!msl_options.enable_clip_distance_user_varying) |
2414 | return; |
2415 | } |
2416 | else if (!meta.strip_array) |
2417 | { |
2418 | // Only flatten/unflatten IO composites for non-tessellation cases where arrays are not stripped. |
2419 | entry_func.add_local_variable(id: var.self); |
2420 | // We need to declare the variable early and at entry-point scope. |
2421 | vars_needing_early_declaration.push_back(t: var.self); |
2422 | } |
2423 | |
2424 | for (uint32_t i = 0; i < elem_cnt; i++) |
2425 | { |
2426 | // Add a reference to the variable type to the interface struct. |
2427 | uint32_t ib_mbr_idx = uint32_t(ib_type.member_types.size()); |
2428 | |
2429 | uint32_t target_components = 0; |
2430 | bool padded_output = false; |
2431 | uint32_t type_id = usable_type->self; |
2432 | |
2433 | // Check if we need to pad fragment output to match a certain number of components. |
2434 | if (get_decoration_bitset(id: var.self).get(bit: DecorationLocation) && msl_options.pad_fragment_output_components && |
2435 | get_entry_point().model == ExecutionModelFragment && storage == StorageClassOutput) |
2436 | { |
2437 | uint32_t locn = get_decoration(id: var.self, decoration: DecorationLocation) + i; |
2438 | target_components = get_target_components_for_fragment_location(location: locn); |
2439 | if (usable_type->vecsize < target_components) |
2440 | { |
2441 | // Make a new type here. |
2442 | type_id = build_extended_vector_type(type_id: usable_type->self, components: target_components); |
2443 | padded_output = true; |
2444 | } |
2445 | } |
2446 | |
2447 | if (storage == StorageClassInput && pull_model_inputs.count(x: var.self)) |
2448 | ib_type.member_types.push_back(t: build_msl_interpolant_type(type_id: get_pointee_type_id(type_id), is_noperspective)); |
2449 | else |
2450 | ib_type.member_types.push_back(t: get_pointee_type_id(type_id)); |
2451 | |
2452 | // Give the member a name |
2453 | string mbr_name = ensure_valid_name(name: join(ts: to_expression(id: var.self), ts: "_" , ts&: i), pfx: "m" ); |
2454 | set_member_name(id: ib_type.self, index: ib_mbr_idx, name: mbr_name); |
2455 | |
2456 | // There is no qualified alias since we need to flatten the internal array on return. |
2457 | if (get_decoration_bitset(id: var.self).get(bit: DecorationLocation)) |
2458 | { |
2459 | uint32_t locn = get_decoration(id: var.self, decoration: DecorationLocation) + i; |
2460 | uint32_t comp = get_decoration(id: var.self, decoration: DecorationComponent); |
2461 | if (storage == StorageClassInput) |
2462 | { |
2463 | var.basetype = ensure_correct_input_type(type_id: var.basetype, location: locn, component: comp, num_components: 0, strip_array: meta.strip_array); |
2464 | uint32_t mbr_type_id = ensure_correct_input_type(type_id: usable_type->self, location: locn, component: comp, num_components: 0, strip_array: meta.strip_array); |
2465 | if (storage == StorageClassInput && pull_model_inputs.count(x: var.self)) |
2466 | ib_type.member_types[ib_mbr_idx] = build_msl_interpolant_type(type_id: mbr_type_id, is_noperspective); |
2467 | else |
2468 | ib_type.member_types[ib_mbr_idx] = mbr_type_id; |
2469 | } |
2470 | set_member_decoration(id: ib_type.self, index: ib_mbr_idx, decoration: DecorationLocation, argument: locn); |
2471 | if (comp) |
2472 | set_member_decoration(id: ib_type.self, index: ib_mbr_idx, decoration: DecorationComponent, argument: comp); |
2473 | mark_location_as_used_by_shader(location: locn, type: *usable_type, storage); |
2474 | } |
2475 | else if (is_builtin && is_tessellation_shader() && storage == StorageClassInput && inputs_by_builtin.count(x: builtin)) |
2476 | { |
2477 | uint32_t locn = inputs_by_builtin[builtin].location + i; |
2478 | set_member_decoration(id: ib_type.self, index: ib_mbr_idx, decoration: DecorationLocation, argument: locn); |
2479 | mark_location_as_used_by_shader(location: locn, type: *usable_type, storage); |
2480 | } |
2481 | else if (is_builtin && (builtin == BuiltInClipDistance || builtin == BuiltInCullDistance)) |
2482 | { |
2483 | // Declare the Clip/CullDistance as [[user(clip/cullN)]]. |
2484 | set_member_decoration(id: ib_type.self, index: ib_mbr_idx, decoration: DecorationBuiltIn, argument: builtin); |
2485 | set_member_decoration(id: ib_type.self, index: ib_mbr_idx, decoration: DecorationIndex, argument: i); |
2486 | } |
2487 | |
2488 | if (get_decoration_bitset(id: var.self).get(bit: DecorationIndex)) |
2489 | { |
2490 | uint32_t index = get_decoration(id: var.self, decoration: DecorationIndex); |
2491 | set_member_decoration(id: ib_type.self, index: ib_mbr_idx, decoration: DecorationIndex, argument: index); |
2492 | } |
2493 | |
2494 | if (storage != StorageClassInput || !pull_model_inputs.count(x: var.self)) |
2495 | { |
2496 | // Copy interpolation decorations if needed |
2497 | if (is_flat) |
2498 | set_member_decoration(id: ib_type.self, index: ib_mbr_idx, decoration: DecorationFlat); |
2499 | if (is_noperspective) |
2500 | set_member_decoration(id: ib_type.self, index: ib_mbr_idx, decoration: DecorationNoPerspective); |
2501 | if (is_centroid) |
2502 | set_member_decoration(id: ib_type.self, index: ib_mbr_idx, decoration: DecorationCentroid); |
2503 | if (is_sample) |
2504 | set_member_decoration(id: ib_type.self, index: ib_mbr_idx, decoration: DecorationSample); |
2505 | } |
2506 | |
2507 | set_extended_member_decoration(type: ib_type.self, index: ib_mbr_idx, decoration: SPIRVCrossDecorationInterfaceOrigID, value: var.self); |
2508 | |
2509 | // Only flatten/unflatten IO composites for non-tessellation cases where arrays are not stripped. |
2510 | if (!meta.strip_array) |
2511 | { |
2512 | switch (storage) |
2513 | { |
2514 | case StorageClassInput: |
2515 | entry_func.fixup_hooks_in.push_back(t: [=, &var]() { |
2516 | if (pull_model_inputs.count(x: var.self)) |
2517 | { |
2518 | string lerp_call; |
2519 | if (is_centroid) |
2520 | lerp_call = ".interpolate_at_centroid()" ; |
2521 | else if (is_sample) |
2522 | lerp_call = join(ts: ".interpolate_at_sample(" , ts: to_expression(id: builtin_sample_id_id), ts: ")" ); |
2523 | else |
2524 | lerp_call = ".interpolate_at_center()" ; |
2525 | statement(ts: to_name(id: var.self), ts: "[" , ts: i, ts: "] = " , ts: ib_var_ref, ts: "." , ts: mbr_name, ts&: lerp_call, ts: ";" ); |
2526 | } |
2527 | else |
2528 | { |
2529 | statement(ts: to_name(id: var.self), ts: "[" , ts: i, ts: "] = " , ts: ib_var_ref, ts: "." , ts: mbr_name, ts: ";" ); |
2530 | } |
2531 | }); |
2532 | break; |
2533 | |
2534 | case StorageClassOutput: |
2535 | entry_func.fixup_hooks_out.push_back(t: [=, &var]() { |
2536 | if (padded_output) |
2537 | { |
2538 | auto &padded_type = this->get<SPIRType>(id: type_id); |
2539 | statement( |
2540 | ts: ib_var_ref, ts: "." , ts: mbr_name, ts: " = " , |
2541 | ts: remap_swizzle(result_type: padded_type, input_components: usable_type->vecsize, expr: join(ts: to_name(id: var.self), ts: "[" , ts: i, ts: "]" )), |
2542 | ts: ";" ); |
2543 | } |
2544 | else if (flatten_from_ib_var) |
2545 | statement(ts: ib_var_ref, ts: "." , ts: mbr_name, ts: " = " , ts: ib_var_ref, ts: "." , ts: flatten_from_ib_mbr_name, ts: "[" , ts: i, |
2546 | ts: "];" ); |
2547 | else |
2548 | statement(ts: ib_var_ref, ts: "." , ts: mbr_name, ts: " = " , ts: to_name(id: var.self), ts: "[" , ts: i, ts: "];" ); |
2549 | }); |
2550 | break; |
2551 | |
2552 | default: |
2553 | break; |
2554 | } |
2555 | } |
2556 | } |
2557 | } |
2558 | |
2559 | void CompilerMSL::add_composite_member_variable_to_interface_block(StorageClass storage, |
2560 | const string &ib_var_ref, SPIRType &ib_type, |
2561 | SPIRVariable &var, SPIRType &var_type, |
2562 | uint32_t mbr_idx, InterfaceBlockMeta &meta, |
2563 | const string &mbr_name_qual, |
2564 | const string &var_chain_qual, |
2565 | uint32_t &location, uint32_t &var_mbr_idx) |
2566 | { |
2567 | auto &entry_func = get<SPIRFunction>(id: ir.default_entry_point); |
2568 | |
2569 | BuiltIn builtin = BuiltInMax; |
2570 | bool is_builtin = is_member_builtin(type: var_type, index: mbr_idx, builtin: &builtin); |
2571 | bool is_flat = |
2572 | has_member_decoration(id: var_type.self, index: mbr_idx, decoration: DecorationFlat) || has_decoration(id: var.self, decoration: DecorationFlat); |
2573 | bool is_noperspective = has_member_decoration(id: var_type.self, index: mbr_idx, decoration: DecorationNoPerspective) || |
2574 | has_decoration(id: var.self, decoration: DecorationNoPerspective); |
2575 | bool is_centroid = has_member_decoration(id: var_type.self, index: mbr_idx, decoration: DecorationCentroid) || |
2576 | has_decoration(id: var.self, decoration: DecorationCentroid); |
2577 | bool is_sample = |
2578 | has_member_decoration(id: var_type.self, index: mbr_idx, decoration: DecorationSample) || has_decoration(id: var.self, decoration: DecorationSample); |
2579 | |
2580 | uint32_t mbr_type_id = var_type.member_types[mbr_idx]; |
2581 | auto &mbr_type = get<SPIRType>(id: mbr_type_id); |
2582 | |
2583 | bool mbr_is_indexable = false; |
2584 | uint32_t elem_cnt = 1; |
2585 | if (is_matrix(type: mbr_type)) |
2586 | { |
2587 | if (is_array(type: mbr_type)) |
2588 | SPIRV_CROSS_THROW("MSL cannot emit arrays-of-matrices in input and output variables." ); |
2589 | |
2590 | mbr_is_indexable = true; |
2591 | elem_cnt = mbr_type.columns; |
2592 | } |
2593 | else if (is_array(type: mbr_type)) |
2594 | { |
2595 | if (mbr_type.array.size() != 1) |
2596 | SPIRV_CROSS_THROW("MSL cannot emit arrays-of-arrays in input and output variables." ); |
2597 | |
2598 | mbr_is_indexable = true; |
2599 | elem_cnt = to_array_size_literal(type: mbr_type); |
2600 | } |
2601 | |
2602 | auto *usable_type = &mbr_type; |
2603 | if (usable_type->pointer) |
2604 | usable_type = &get<SPIRType>(id: usable_type->parent_type); |
2605 | while (is_array(type: *usable_type) || is_matrix(type: *usable_type)) |
2606 | usable_type = &get<SPIRType>(id: usable_type->parent_type); |
2607 | |
2608 | bool flatten_from_ib_var = false; |
2609 | string flatten_from_ib_mbr_name; |
2610 | |
2611 | if (storage == StorageClassOutput && is_builtin && builtin == BuiltInClipDistance) |
2612 | { |
2613 | // Also declare [[clip_distance]] attribute here. |
2614 | uint32_t clip_array_mbr_idx = uint32_t(ib_type.member_types.size()); |
2615 | ib_type.member_types.push_back(t: mbr_type_id); |
2616 | set_member_decoration(id: ib_type.self, index: clip_array_mbr_idx, decoration: DecorationBuiltIn, argument: BuiltInClipDistance); |
2617 | |
2618 | flatten_from_ib_mbr_name = builtin_to_glsl(builtin: BuiltInClipDistance, storage: StorageClassOutput); |
2619 | set_member_name(id: ib_type.self, index: clip_array_mbr_idx, name: flatten_from_ib_mbr_name); |
2620 | |
2621 | // When we flatten, we flatten directly from the "out" struct, |
2622 | // not from a function variable. |
2623 | flatten_from_ib_var = true; |
2624 | |
2625 | if (!msl_options.enable_clip_distance_user_varying) |
2626 | return; |
2627 | } |
2628 | |
2629 | // Recursively handle nested structures. |
2630 | if (mbr_type.basetype == SPIRType::Struct) |
2631 | { |
2632 | for (uint32_t i = 0; i < elem_cnt; i++) |
2633 | { |
2634 | string mbr_name = append_member_name(qualifier: mbr_name_qual, type: var_type, index: mbr_idx) + (mbr_is_indexable ? join(ts: "_" , ts&: i) : "" ); |
2635 | string var_chain = join(ts: var_chain_qual, ts: "." , ts: to_member_name(type: var_type, index: mbr_idx), ts: (mbr_is_indexable ? join(ts: "[" , ts&: i, ts: "]" ) : "" )); |
2636 | uint32_t sub_mbr_cnt = uint32_t(mbr_type.member_types.size()); |
2637 | for (uint32_t sub_mbr_idx = 0; sub_mbr_idx < sub_mbr_cnt; sub_mbr_idx++) |
2638 | { |
2639 | add_composite_member_variable_to_interface_block(storage, ib_var_ref, ib_type, |
2640 | var, var_type&: mbr_type, mbr_idx: sub_mbr_idx, |
2641 | meta, mbr_name_qual: mbr_name, var_chain_qual: var_chain, |
2642 | location, var_mbr_idx); |
2643 | // FIXME: Recursive structs and tessellation breaks here. |
2644 | var_mbr_idx++; |
2645 | } |
2646 | } |
2647 | return; |
2648 | } |
2649 | |
2650 | for (uint32_t i = 0; i < elem_cnt; i++) |
2651 | { |
2652 | // Add a reference to the variable type to the interface struct. |
2653 | uint32_t ib_mbr_idx = uint32_t(ib_type.member_types.size()); |
2654 | if (storage == StorageClassInput && pull_model_inputs.count(x: var.self)) |
2655 | ib_type.member_types.push_back(t: build_msl_interpolant_type(type_id: usable_type->self, is_noperspective)); |
2656 | else |
2657 | ib_type.member_types.push_back(t: usable_type->self); |
2658 | |
2659 | // Give the member a name |
2660 | string mbr_name = ensure_valid_name(name: append_member_name(qualifier: mbr_name_qual, type: var_type, index: mbr_idx) + (mbr_is_indexable ? join(ts: "_" , ts&: i) : "" ), pfx: "m" ); |
2661 | set_member_name(id: ib_type.self, index: ib_mbr_idx, name: mbr_name); |
2662 | |
2663 | // Once we determine the location of the first member within nested structures, |
2664 | // from a var of the topmost structure, the remaining flattened members of |
2665 | // the nested structures will have consecutive location values. At this point, |
2666 | // we've recursively tunnelled into structs, arrays, and matrices, and are |
2667 | // down to a single location for each member now. |
2668 | if (!is_builtin && location != UINT32_MAX) |
2669 | { |
2670 | set_member_decoration(id: ib_type.self, index: ib_mbr_idx, decoration: DecorationLocation, argument: location); |
2671 | mark_location_as_used_by_shader(location, type: *usable_type, storage); |
2672 | location++; |
2673 | } |
2674 | else if (has_member_decoration(id: var_type.self, index: mbr_idx, decoration: DecorationLocation)) |
2675 | { |
2676 | location = get_member_decoration(id: var_type.self, index: mbr_idx, decoration: DecorationLocation) + i; |
2677 | set_member_decoration(id: ib_type.self, index: ib_mbr_idx, decoration: DecorationLocation, argument: location); |
2678 | mark_location_as_used_by_shader(location, type: *usable_type, storage); |
2679 | location++; |
2680 | } |
2681 | else if (has_decoration(id: var.self, decoration: DecorationLocation)) |
2682 | { |
2683 | location = get_accumulated_member_location(var, mbr_idx, strip_array: meta.strip_array) + i; |
2684 | set_member_decoration(id: ib_type.self, index: ib_mbr_idx, decoration: DecorationLocation, argument: location); |
2685 | mark_location_as_used_by_shader(location, type: *usable_type, storage); |
2686 | location++; |
2687 | } |
2688 | else if (is_builtin && is_tessellation_shader() && storage == StorageClassInput && inputs_by_builtin.count(x: builtin)) |
2689 | { |
2690 | location = inputs_by_builtin[builtin].location + i; |
2691 | set_member_decoration(id: ib_type.self, index: ib_mbr_idx, decoration: DecorationLocation, argument: location); |
2692 | mark_location_as_used_by_shader(location, type: *usable_type, storage); |
2693 | location++; |
2694 | } |
2695 | else if (is_builtin && (builtin == BuiltInClipDistance || builtin == BuiltInCullDistance)) |
2696 | { |
2697 | // Declare the Clip/CullDistance as [[user(clip/cullN)]]. |
2698 | set_member_decoration(id: ib_type.self, index: ib_mbr_idx, decoration: DecorationBuiltIn, argument: builtin); |
2699 | set_member_decoration(id: ib_type.self, index: ib_mbr_idx, decoration: DecorationIndex, argument: i); |
2700 | } |
2701 | |
2702 | if (has_member_decoration(id: var_type.self, index: mbr_idx, decoration: DecorationComponent)) |
2703 | SPIRV_CROSS_THROW("DecorationComponent on matrices and arrays is not supported." ); |
2704 | |
2705 | if (storage != StorageClassInput || !pull_model_inputs.count(x: var.self)) |
2706 | { |
2707 | // Copy interpolation decorations if needed |
2708 | if (is_flat) |
2709 | set_member_decoration(id: ib_type.self, index: ib_mbr_idx, decoration: DecorationFlat); |
2710 | if (is_noperspective) |
2711 | set_member_decoration(id: ib_type.self, index: ib_mbr_idx, decoration: DecorationNoPerspective); |
2712 | if (is_centroid) |
2713 | set_member_decoration(id: ib_type.self, index: ib_mbr_idx, decoration: DecorationCentroid); |
2714 | if (is_sample) |
2715 | set_member_decoration(id: ib_type.self, index: ib_mbr_idx, decoration: DecorationSample); |
2716 | } |
2717 | |
2718 | set_extended_member_decoration(type: ib_type.self, index: ib_mbr_idx, decoration: SPIRVCrossDecorationInterfaceOrigID, value: var.self); |
2719 | set_extended_member_decoration(type: ib_type.self, index: ib_mbr_idx, decoration: SPIRVCrossDecorationInterfaceMemberIndex, value: var_mbr_idx); |
2720 | |
2721 | // Unflatten or flatten from [[stage_in]] or [[stage_out]] as appropriate. |
2722 | if (!meta.strip_array && meta.allow_local_declaration) |
2723 | { |
2724 | string var_chain = join(ts: var_chain_qual, ts: "." , ts: to_member_name(type: var_type, index: mbr_idx), ts: (mbr_is_indexable ? join(ts: "[" , ts&: i, ts: "]" ) : "" )); |
2725 | switch (storage) |
2726 | { |
2727 | case StorageClassInput: |
2728 | entry_func.fixup_hooks_in.push_back(t: [=, &var]() { |
2729 | string lerp_call; |
2730 | if (pull_model_inputs.count(x: var.self)) |
2731 | { |
2732 | if (is_centroid) |
2733 | lerp_call = ".interpolate_at_centroid()" ; |
2734 | else if (is_sample) |
2735 | lerp_call = join(ts: ".interpolate_at_sample(" , ts: to_expression(id: builtin_sample_id_id), ts: ")" ); |
2736 | else |
2737 | lerp_call = ".interpolate_at_center()" ; |
2738 | } |
2739 | statement(ts: var_chain, ts: " = " , ts: ib_var_ref, ts: "." , ts: mbr_name, ts&: lerp_call, ts: ";" ); |
2740 | }); |
2741 | break; |
2742 | |
2743 | case StorageClassOutput: |
2744 | entry_func.fixup_hooks_out.push_back(t: [=]() { |
2745 | if (flatten_from_ib_var) |
2746 | statement(ts: ib_var_ref, ts: "." , ts: mbr_name, ts: " = " , ts: ib_var_ref, ts: "." , ts: flatten_from_ib_mbr_name, ts: "[" , ts: i, ts: "];" ); |
2747 | else |
2748 | statement(ts: ib_var_ref, ts: "." , ts: mbr_name, ts: " = " , ts: var_chain, ts: ";" ); |
2749 | }); |
2750 | break; |
2751 | |
2752 | default: |
2753 | break; |
2754 | } |
2755 | } |
2756 | } |
2757 | } |
2758 | |
2759 | void CompilerMSL::add_plain_member_variable_to_interface_block(StorageClass storage, |
2760 | const string &ib_var_ref, SPIRType &ib_type, |
2761 | SPIRVariable &var, SPIRType &var_type, |
2762 | uint32_t mbr_idx, InterfaceBlockMeta &meta, |
2763 | const string &mbr_name_qual, |
2764 | const string &var_chain_qual, |
2765 | uint32_t &location, uint32_t &var_mbr_idx) |
2766 | { |
2767 | auto &entry_func = get<SPIRFunction>(id: ir.default_entry_point); |
2768 | |
2769 | BuiltIn builtin = BuiltInMax; |
2770 | bool is_builtin = is_member_builtin(type: var_type, index: mbr_idx, builtin: &builtin); |
2771 | bool is_flat = |
2772 | has_member_decoration(id: var_type.self, index: mbr_idx, decoration: DecorationFlat) || has_decoration(id: var.self, decoration: DecorationFlat); |
2773 | bool is_noperspective = has_member_decoration(id: var_type.self, index: mbr_idx, decoration: DecorationNoPerspective) || |
2774 | has_decoration(id: var.self, decoration: DecorationNoPerspective); |
2775 | bool is_centroid = has_member_decoration(id: var_type.self, index: mbr_idx, decoration: DecorationCentroid) || |
2776 | has_decoration(id: var.self, decoration: DecorationCentroid); |
2777 | bool is_sample = |
2778 | has_member_decoration(id: var_type.self, index: mbr_idx, decoration: DecorationSample) || has_decoration(id: var.self, decoration: DecorationSample); |
2779 | |
2780 | // Add a reference to the member to the interface struct. |
2781 | uint32_t mbr_type_id = var_type.member_types[mbr_idx]; |
2782 | uint32_t ib_mbr_idx = uint32_t(ib_type.member_types.size()); |
2783 | mbr_type_id = ensure_correct_builtin_type(type_id: mbr_type_id, builtin); |
2784 | var_type.member_types[mbr_idx] = mbr_type_id; |
2785 | if (storage == StorageClassInput && pull_model_inputs.count(x: var.self)) |
2786 | ib_type.member_types.push_back(t: build_msl_interpolant_type(type_id: mbr_type_id, is_noperspective)); |
2787 | else |
2788 | ib_type.member_types.push_back(t: mbr_type_id); |
2789 | |
2790 | // Give the member a name |
2791 | string mbr_name = ensure_valid_name(name: append_member_name(qualifier: mbr_name_qual, type: var_type, index: mbr_idx), pfx: "m" ); |
2792 | set_member_name(id: ib_type.self, index: ib_mbr_idx, name: mbr_name); |
2793 | |
2794 | // Update the original variable reference to include the structure reference |
2795 | string qual_var_name = ib_var_ref + "." + mbr_name; |
2796 | // If using pull-model interpolation, need to add a call to the correct interpolation method. |
2797 | if (storage == StorageClassInput && pull_model_inputs.count(x: var.self)) |
2798 | { |
2799 | if (is_centroid) |
2800 | qual_var_name += ".interpolate_at_centroid()" ; |
2801 | else if (is_sample) |
2802 | qual_var_name += join(ts: ".interpolate_at_sample(" , ts: to_expression(id: builtin_sample_id_id), ts: ")" ); |
2803 | else |
2804 | qual_var_name += ".interpolate_at_center()" ; |
2805 | } |
2806 | |
2807 | bool flatten_stage_out = false; |
2808 | string var_chain = var_chain_qual + "." + to_member_name(type: var_type, index: mbr_idx); |
2809 | if (is_builtin && !meta.strip_array) |
2810 | { |
2811 | // For the builtin gl_PerVertex, we cannot treat it as a block anyways, |
2812 | // so redirect to qualified name. |
2813 | set_member_qualified_name(type_id: var_type.self, index: mbr_idx, name: qual_var_name); |
2814 | } |
2815 | else if (!meta.strip_array && meta.allow_local_declaration) |
2816 | { |
2817 | // Unflatten or flatten from [[stage_in]] or [[stage_out]] as appropriate. |
2818 | switch (storage) |
2819 | { |
2820 | case StorageClassInput: |
2821 | entry_func.fixup_hooks_in.push_back(t: [=]() { |
2822 | statement(ts: var_chain, ts: " = " , ts: qual_var_name, ts: ";" ); |
2823 | }); |
2824 | break; |
2825 | |
2826 | case StorageClassOutput: |
2827 | flatten_stage_out = true; |
2828 | entry_func.fixup_hooks_out.push_back(t: [=]() { |
2829 | statement(ts: qual_var_name, ts: " = " , ts: var_chain, ts: ";" ); |
2830 | }); |
2831 | break; |
2832 | |
2833 | default: |
2834 | break; |
2835 | } |
2836 | } |
2837 | |
2838 | // Once we determine the location of the first member within nested structures, |
2839 | // from a var of the topmost structure, the remaining flattened members of |
2840 | // the nested structures will have consecutive location values. At this point, |
2841 | // we've recursively tunnelled into structs, arrays, and matrices, and are |
2842 | // down to a single location for each member now. |
2843 | if (!is_builtin && location != UINT32_MAX) |
2844 | { |
2845 | set_member_decoration(id: ib_type.self, index: ib_mbr_idx, decoration: DecorationLocation, argument: location); |
2846 | mark_location_as_used_by_shader(location, type: get<SPIRType>(id: mbr_type_id), storage); |
2847 | location++; |
2848 | } |
2849 | else if (has_member_decoration(id: var_type.self, index: mbr_idx, decoration: DecorationLocation)) |
2850 | { |
2851 | location = get_member_decoration(id: var_type.self, index: mbr_idx, decoration: DecorationLocation); |
2852 | uint32_t comp = get_member_decoration(id: var_type.self, index: mbr_idx, decoration: DecorationComponent); |
2853 | if (storage == StorageClassInput) |
2854 | { |
2855 | mbr_type_id = ensure_correct_input_type(type_id: mbr_type_id, location, component: comp, num_components: 0, strip_array: meta.strip_array); |
2856 | var_type.member_types[mbr_idx] = mbr_type_id; |
2857 | if (storage == StorageClassInput && pull_model_inputs.count(x: var.self)) |
2858 | ib_type.member_types[ib_mbr_idx] = build_msl_interpolant_type(type_id: mbr_type_id, is_noperspective); |
2859 | else |
2860 | ib_type.member_types[ib_mbr_idx] = mbr_type_id; |
2861 | } |
2862 | set_member_decoration(id: ib_type.self, index: ib_mbr_idx, decoration: DecorationLocation, argument: location); |
2863 | mark_location_as_used_by_shader(location, type: get<SPIRType>(id: mbr_type_id), storage); |
2864 | location++; |
2865 | } |
2866 | else if (has_decoration(id: var.self, decoration: DecorationLocation)) |
2867 | { |
2868 | location = get_accumulated_member_location(var, mbr_idx, strip_array: meta.strip_array); |
2869 | if (storage == StorageClassInput) |
2870 | { |
2871 | mbr_type_id = ensure_correct_input_type(type_id: mbr_type_id, location, component: 0, num_components: 0, strip_array: meta.strip_array); |
2872 | var_type.member_types[mbr_idx] = mbr_type_id; |
2873 | if (storage == StorageClassInput && pull_model_inputs.count(x: var.self)) |
2874 | ib_type.member_types[ib_mbr_idx] = build_msl_interpolant_type(type_id: mbr_type_id, is_noperspective); |
2875 | else |
2876 | ib_type.member_types[ib_mbr_idx] = mbr_type_id; |
2877 | } |
2878 | set_member_decoration(id: ib_type.self, index: ib_mbr_idx, decoration: DecorationLocation, argument: location); |
2879 | mark_location_as_used_by_shader(location, type: get<SPIRType>(id: mbr_type_id), storage); |
2880 | location++; |
2881 | } |
2882 | else if (is_builtin && is_tessellation_shader() && storage == StorageClassInput && inputs_by_builtin.count(x: builtin)) |
2883 | { |
2884 | location = inputs_by_builtin[builtin].location; |
2885 | set_member_decoration(id: ib_type.self, index: ib_mbr_idx, decoration: DecorationLocation, argument: location); |
2886 | mark_location_as_used_by_shader(location, type: get<SPIRType>(id: mbr_type_id), storage); |
2887 | location++; |
2888 | } |
2889 | |
2890 | // Copy the component location, if present. |
2891 | if (has_member_decoration(id: var_type.self, index: mbr_idx, decoration: DecorationComponent)) |
2892 | { |
2893 | uint32_t comp = get_member_decoration(id: var_type.self, index: mbr_idx, decoration: DecorationComponent); |
2894 | set_member_decoration(id: ib_type.self, index: ib_mbr_idx, decoration: DecorationComponent, argument: comp); |
2895 | } |
2896 | |
2897 | // Mark the member as builtin if needed |
2898 | if (is_builtin) |
2899 | { |
2900 | set_member_decoration(id: ib_type.self, index: ib_mbr_idx, decoration: DecorationBuiltIn, argument: builtin); |
2901 | if (builtin == BuiltInPosition && storage == StorageClassOutput) |
2902 | qual_pos_var_name = qual_var_name; |
2903 | } |
2904 | |
2905 | const SPIRConstant *c = nullptr; |
2906 | if (!flatten_stage_out && var.storage == StorageClassOutput && |
2907 | var.initializer != ID(0) && (c = maybe_get<SPIRConstant>(id: var.initializer))) |
2908 | { |
2909 | if (meta.strip_array) |
2910 | { |
2911 | entry_func.fixup_hooks_in.push_back(t: [=, &var]() { |
2912 | auto &type = this->get<SPIRType>(id: var.basetype); |
2913 | uint32_t index = get_extended_member_decoration(type: var.self, index: mbr_idx, decoration: SPIRVCrossDecorationInterfaceMemberIndex); |
2914 | |
2915 | auto invocation = to_tesc_invocation_id(); |
2916 | auto constant_chain = join(ts: to_expression(id: var.initializer), ts: "[" , ts&: invocation, ts: "]" ); |
2917 | statement(ts: to_expression(id: stage_out_ptr_var_id), ts: "[" , |
2918 | ts&: invocation, ts: "]." , |
2919 | ts: to_member_name(type: ib_type, index), ts: " = " , |
2920 | ts&: constant_chain, ts: "." , ts: to_member_name(type, index: mbr_idx), ts: ";" ); |
2921 | }); |
2922 | } |
2923 | else |
2924 | { |
2925 | entry_func.fixup_hooks_in.push_back(t: [=]() { |
2926 | statement(ts: qual_var_name, ts: " = " , ts: constant_expression( |
2927 | c: this->get<SPIRConstant>(id: c->subconstants[mbr_idx])), ts: ";" ); |
2928 | }); |
2929 | } |
2930 | } |
2931 | |
2932 | if (storage != StorageClassInput || !pull_model_inputs.count(x: var.self)) |
2933 | { |
2934 | // Copy interpolation decorations if needed |
2935 | if (is_flat) |
2936 | set_member_decoration(id: ib_type.self, index: ib_mbr_idx, decoration: DecorationFlat); |
2937 | if (is_noperspective) |
2938 | set_member_decoration(id: ib_type.self, index: ib_mbr_idx, decoration: DecorationNoPerspective); |
2939 | if (is_centroid) |
2940 | set_member_decoration(id: ib_type.self, index: ib_mbr_idx, decoration: DecorationCentroid); |
2941 | if (is_sample) |
2942 | set_member_decoration(id: ib_type.self, index: ib_mbr_idx, decoration: DecorationSample); |
2943 | } |
2944 | |
2945 | set_extended_member_decoration(type: ib_type.self, index: ib_mbr_idx, decoration: SPIRVCrossDecorationInterfaceOrigID, value: var.self); |
2946 | set_extended_member_decoration(type: ib_type.self, index: ib_mbr_idx, decoration: SPIRVCrossDecorationInterfaceMemberIndex, value: var_mbr_idx); |
2947 | } |
2948 | |
2949 | // In Metal, the tessellation levels are stored as tightly packed half-precision floating point values. |
2950 | // But, stage-in attribute offsets and strides must be multiples of four, so we can't pass the levels |
2951 | // individually. Therefore, we must pass them as vectors. Triangles get a single float4, with the outer |
2952 | // levels in 'xyz' and the inner level in 'w'. Quads get a float4 containing the outer levels and a |
2953 | // float2 containing the inner levels. |
2954 | void CompilerMSL::add_tess_level_input_to_interface_block(const std::string &ib_var_ref, SPIRType &ib_type, |
2955 | SPIRVariable &var) |
2956 | { |
2957 | auto &entry_func = get<SPIRFunction>(id: ir.default_entry_point); |
2958 | auto &var_type = get_variable_element_type(var); |
2959 | |
2960 | BuiltIn builtin = BuiltIn(get_decoration(id: var.self, decoration: DecorationBuiltIn)); |
2961 | |
2962 | // Force the variable to have the proper name. |
2963 | string var_name = builtin_to_glsl(builtin, storage: StorageClassFunction); |
2964 | set_name(id: var.self, name: var_name); |
2965 | |
2966 | // We need to declare the variable early and at entry-point scope. |
2967 | entry_func.add_local_variable(id: var.self); |
2968 | vars_needing_early_declaration.push_back(t: var.self); |
2969 | bool triangles = get_execution_mode_bitset().get(bit: ExecutionModeTriangles); |
2970 | string mbr_name; |
2971 | |
2972 | // Add a reference to the variable type to the interface struct. |
2973 | uint32_t ib_mbr_idx = uint32_t(ib_type.member_types.size()); |
2974 | |
2975 | const auto mark_locations = [&](const SPIRType &new_var_type) { |
2976 | if (get_decoration_bitset(id: var.self).get(bit: DecorationLocation)) |
2977 | { |
2978 | uint32_t locn = get_decoration(id: var.self, decoration: DecorationLocation); |
2979 | set_member_decoration(id: ib_type.self, index: ib_mbr_idx, decoration: DecorationLocation, argument: locn); |
2980 | mark_location_as_used_by_shader(location: locn, type: new_var_type, storage: StorageClassInput); |
2981 | } |
2982 | else if (inputs_by_builtin.count(x: builtin)) |
2983 | { |
2984 | uint32_t locn = inputs_by_builtin[builtin].location; |
2985 | set_member_decoration(id: ib_type.self, index: ib_mbr_idx, decoration: DecorationLocation, argument: locn); |
2986 | mark_location_as_used_by_shader(location: locn, type: new_var_type, storage: StorageClassInput); |
2987 | } |
2988 | }; |
2989 | |
2990 | if (triangles) |
2991 | { |
2992 | // Triangles are tricky, because we want only one member in the struct. |
2993 | mbr_name = "gl_TessLevel" ; |
2994 | |
2995 | // If we already added the other one, we can skip this step. |
2996 | if (!added_builtin_tess_level) |
2997 | { |
2998 | uint32_t type_id = build_extended_vector_type(type_id: var_type.self, components: 4); |
2999 | |
3000 | ib_type.member_types.push_back(t: type_id); |
3001 | |
3002 | // Give the member a name |
3003 | set_member_name(id: ib_type.self, index: ib_mbr_idx, name: mbr_name); |
3004 | |
3005 | // We cannot decorate both, but the important part is that |
3006 | // it's marked as builtin so we can get automatic attribute assignment if needed. |
3007 | set_member_decoration(id: ib_type.self, index: ib_mbr_idx, decoration: DecorationBuiltIn, argument: builtin); |
3008 | |
3009 | mark_locations(var_type); |
3010 | added_builtin_tess_level = true; |
3011 | } |
3012 | } |
3013 | else |
3014 | { |
3015 | mbr_name = var_name; |
3016 | |
3017 | uint32_t type_id = build_extended_vector_type(type_id: var_type.self, components: builtin == BuiltInTessLevelOuter ? 4 : 2); |
3018 | |
3019 | uint32_t ptr_type_id = ir.increase_bound_by(count: 1); |
3020 | auto &new_var_type = set<SPIRType>(id: ptr_type_id, args&: get<SPIRType>(id: type_id)); |
3021 | new_var_type.pointer = true; |
3022 | new_var_type.pointer_depth++; |
3023 | new_var_type.storage = StorageClassInput; |
3024 | new_var_type.parent_type = type_id; |
3025 | |
3026 | ib_type.member_types.push_back(t: type_id); |
3027 | |
3028 | // Give the member a name |
3029 | set_member_name(id: ib_type.self, index: ib_mbr_idx, name: mbr_name); |
3030 | set_member_decoration(id: ib_type.self, index: ib_mbr_idx, decoration: DecorationBuiltIn, argument: builtin); |
3031 | |
3032 | mark_locations(new_var_type); |
3033 | } |
3034 | |
3035 | if (builtin == BuiltInTessLevelOuter) |
3036 | { |
3037 | entry_func.fixup_hooks_in.push_back(t: [=]() { |
3038 | statement(ts: var_name, ts: "[0] = " , ts: ib_var_ref, ts: "." , ts: mbr_name, ts: ".x;" ); |
3039 | statement(ts: var_name, ts: "[1] = " , ts: ib_var_ref, ts: "." , ts: mbr_name, ts: ".y;" ); |
3040 | statement(ts: var_name, ts: "[2] = " , ts: ib_var_ref, ts: "." , ts: mbr_name, ts: ".z;" ); |
3041 | if (!triangles) |
3042 | statement(ts: var_name, ts: "[3] = " , ts: ib_var_ref, ts: "." , ts: mbr_name, ts: ".w;" ); |
3043 | }); |
3044 | } |
3045 | else |
3046 | { |
3047 | entry_func.fixup_hooks_in.push_back(t: [=]() { |
3048 | if (triangles) |
3049 | { |
3050 | statement(ts: var_name, ts: "[0] = " , ts: ib_var_ref, ts: "." , ts: mbr_name, ts: ".w;" ); |
3051 | } |
3052 | else |
3053 | { |
3054 | statement(ts: var_name, ts: "[0] = " , ts: ib_var_ref, ts: "." , ts: mbr_name, ts: ".x;" ); |
3055 | statement(ts: var_name, ts: "[1] = " , ts: ib_var_ref, ts: "." , ts: mbr_name, ts: ".y;" ); |
3056 | } |
3057 | }); |
3058 | } |
3059 | } |
3060 | |
3061 | bool CompilerMSL::variable_storage_requires_stage_io(spv::StorageClass storage) const |
3062 | { |
3063 | if (storage == StorageClassOutput) |
3064 | return !capture_output_to_buffer; |
3065 | else if (storage == StorageClassInput) |
3066 | return !(get_execution_model() == ExecutionModelTessellationControl && msl_options.multi_patch_workgroup); |
3067 | else |
3068 | return false; |
3069 | } |
3070 | |
3071 | string CompilerMSL::to_tesc_invocation_id() |
3072 | { |
3073 | if (msl_options.multi_patch_workgroup) |
3074 | { |
3075 | // n.b. builtin_invocation_id_id here is the dispatch global invocation ID, |
3076 | // not the TC invocation ID. |
3077 | return join(ts: to_expression(id: builtin_invocation_id_id), ts: ".x % " , ts&: get_entry_point().output_vertices); |
3078 | } |
3079 | else |
3080 | return builtin_to_glsl(builtin: BuiltInInvocationId, storage: StorageClassInput); |
3081 | } |
3082 | |
3083 | void CompilerMSL::emit_local_masked_variable(const SPIRVariable &masked_var, bool strip_array) |
3084 | { |
3085 | auto &entry_func = get<SPIRFunction>(id: ir.default_entry_point); |
3086 | bool threadgroup_storage = variable_decl_is_remapped_storage(variable: masked_var, storage: StorageClassWorkgroup); |
3087 | |
3088 | if (threadgroup_storage && msl_options.multi_patch_workgroup) |
3089 | { |
3090 | // We need one threadgroup block per patch, so fake this. |
3091 | entry_func.fixup_hooks_in.push_back(t: [this, &masked_var]() { |
3092 | auto &type = get_variable_data_type(var: masked_var); |
3093 | add_local_variable_name(id: masked_var.self); |
3094 | |
3095 | bool old_is_builtin = is_using_builtin_array; |
3096 | is_using_builtin_array = true; |
3097 | |
3098 | const uint32_t max_control_points_per_patch = 32u; |
3099 | uint32_t max_num_instances = |
3100 | (max_control_points_per_patch + get_entry_point().output_vertices - 1u) / |
3101 | get_entry_point().output_vertices; |
3102 | statement(ts: "threadgroup " , ts: type_to_glsl(type), ts: " " , |
3103 | ts: "spvStorage" , ts: to_name(id: masked_var.self), ts: "[" , ts&: max_num_instances, ts: "]" , |
3104 | ts: type_to_array_glsl(type), ts: ";" ); |
3105 | |
3106 | // Assign a threadgroup slice to each PrimitiveID. |
3107 | // We assume here that workgroup size is rounded to 32, |
3108 | // since that's the maximum number of control points per patch. |
3109 | // We cannot size the array based on fixed dispatch parameters, |
3110 | // since Metal does not allow that. :( |
3111 | // FIXME: We will likely need an option to support passing down target workgroup size, |
3112 | // so we can emit appropriate size here. |
3113 | statement(ts: "threadgroup " , ts: type_to_glsl(type), ts: " " , |
3114 | ts: "(&" , ts: to_name(id: masked_var.self), ts: ")" , |
3115 | ts: type_to_array_glsl(type), ts: " = spvStorage" , ts: to_name(id: masked_var.self), ts: "[" , |
3116 | ts: "(" , ts: to_expression(id: builtin_invocation_id_id), ts: ".x / " , |
3117 | ts&: get_entry_point().output_vertices, ts: ") % " , |
3118 | ts&: max_num_instances, ts: "];" ); |
3119 | |
3120 | is_using_builtin_array = old_is_builtin; |
3121 | }); |
3122 | } |
3123 | else |
3124 | { |
3125 | entry_func.add_local_variable(id: masked_var.self); |
3126 | } |
3127 | |
3128 | if (!threadgroup_storage) |
3129 | { |
3130 | vars_needing_early_declaration.push_back(t: masked_var.self); |
3131 | } |
3132 | else if (masked_var.initializer) |
3133 | { |
3134 | // Cannot directly initialize threadgroup variables. Need fixup hooks. |
3135 | ID initializer = masked_var.initializer; |
3136 | if (strip_array) |
3137 | { |
3138 | entry_func.fixup_hooks_in.push_back(t: [this, &masked_var, initializer]() { |
3139 | auto invocation = to_tesc_invocation_id(); |
3140 | statement(ts: to_expression(id: masked_var.self), ts: "[" , |
3141 | ts&: invocation, ts: "] = " , |
3142 | ts: to_expression(id: initializer), ts: "[" , |
3143 | ts&: invocation, ts: "];" ); |
3144 | }); |
3145 | } |
3146 | else |
3147 | { |
3148 | entry_func.fixup_hooks_in.push_back(t: [this, &masked_var, initializer]() { |
3149 | statement(ts: to_expression(id: masked_var.self), ts: " = " , ts: to_expression(id: initializer), ts: ";" ); |
3150 | }); |
3151 | } |
3152 | } |
3153 | } |
3154 | |
3155 | void CompilerMSL::add_variable_to_interface_block(StorageClass storage, const string &ib_var_ref, SPIRType &ib_type, |
3156 | SPIRVariable &var, InterfaceBlockMeta &meta) |
3157 | { |
3158 | auto &entry_func = get<SPIRFunction>(id: ir.default_entry_point); |
3159 | // Tessellation control I/O variables and tessellation evaluation per-point inputs are |
3160 | // usually declared as arrays. In these cases, we want to add the element type to the |
3161 | // interface block, since in Metal it's the interface block itself which is arrayed. |
3162 | auto &var_type = meta.strip_array ? get_variable_element_type(var) : get_variable_data_type(var); |
3163 | bool is_builtin = is_builtin_variable(var); |
3164 | auto builtin = BuiltIn(get_decoration(id: var.self, decoration: DecorationBuiltIn)); |
3165 | bool is_block = has_decoration(id: var_type.self, decoration: DecorationBlock); |
3166 | |
3167 | // If stage variables are masked out, emit them as plain variables instead. |
3168 | // For builtins, we query them one by one later. |
3169 | // IO blocks are not masked here, we need to mask them per-member instead. |
3170 | if (storage == StorageClassOutput && is_stage_output_variable_masked(var)) |
3171 | { |
3172 | // If we ignore an output, we must still emit it, since it might be used by app. |
3173 | // Instead, just emit it as early declaration. |
3174 | emit_local_masked_variable(masked_var: var, strip_array: meta.strip_array); |
3175 | return; |
3176 | } |
3177 | |
3178 | if (storage == StorageClassInput && has_decoration(id: var.self, decoration: DecorationPerVertexKHR)) |
3179 | SPIRV_CROSS_THROW("PerVertexKHR decoration is not supported in MSL." ); |
3180 | |
3181 | // If variable names alias, they will end up with wrong names in the interface struct, because |
3182 | // there might be aliases in the member name cache and there would be a mismatch in fixup_in code. |
3183 | // Make sure to register the variables as unique resource names ahead of time. |
3184 | // This would normally conflict with the name cache when emitting local variables, |
3185 | // but this happens in the setup stage, before we hit compilation loops. |
3186 | // The name cache is cleared before we actually emit code, so this is safe. |
3187 | add_resource_name(id: var.self); |
3188 | |
3189 | if (var_type.basetype == SPIRType::Struct) |
3190 | { |
3191 | bool block_requires_flattening = variable_storage_requires_stage_io(storage) || is_block; |
3192 | bool needs_local_declaration = !is_builtin && block_requires_flattening && meta.allow_local_declaration; |
3193 | |
3194 | if (needs_local_declaration) |
3195 | { |
3196 | // For I/O blocks or structs, we will need to pass the block itself around |
3197 | // to functions if they are used globally in leaf functions. |
3198 | // Rather than passing down member by member, |
3199 | // we unflatten I/O blocks while running the shader, |
3200 | // and pass the actual struct type down to leaf functions. |
3201 | // We then unflatten inputs, and flatten outputs in the "fixup" stages. |
3202 | emit_local_masked_variable(masked_var: var, strip_array: meta.strip_array); |
3203 | } |
3204 | |
3205 | if (!block_requires_flattening) |
3206 | { |
3207 | // In Metal tessellation shaders, the interface block itself is arrayed. This makes things |
3208 | // very complicated, since stage-in structures in MSL don't support nested structures. |
3209 | // Luckily, for stage-out when capturing output, we can avoid this and just add |
3210 | // composite members directly, because the stage-out structure is stored to a buffer, |
3211 | // not returned. |
3212 | add_plain_variable_to_interface_block(storage, ib_var_ref, ib_type, var, meta); |
3213 | } |
3214 | else |
3215 | { |
3216 | bool masked_block = false; |
3217 | uint32_t location = UINT32_MAX; |
3218 | uint32_t var_mbr_idx = 0; |
3219 | uint32_t elem_cnt = 1; |
3220 | if (is_matrix(type: var_type)) |
3221 | { |
3222 | if (is_array(type: var_type)) |
3223 | SPIRV_CROSS_THROW("MSL cannot emit arrays-of-matrices in input and output variables." ); |
3224 | |
3225 | elem_cnt = var_type.columns; |
3226 | } |
3227 | else if (is_array(type: var_type)) |
3228 | { |
3229 | if (var_type.array.size() != 1) |
3230 | SPIRV_CROSS_THROW("MSL cannot emit arrays-of-arrays in input and output variables." ); |
3231 | |
3232 | elem_cnt = to_array_size_literal(type: var_type); |
3233 | } |
3234 | |
3235 | for (uint32_t elem_idx = 0; elem_idx < elem_cnt; elem_idx++) |
3236 | { |
3237 | // Flatten the struct members into the interface struct |
3238 | for (uint32_t mbr_idx = 0; mbr_idx < uint32_t(var_type.member_types.size()); mbr_idx++) |
3239 | { |
3240 | builtin = BuiltInMax; |
3241 | is_builtin = is_member_builtin(type: var_type, index: mbr_idx, builtin: &builtin); |
3242 | auto &mbr_type = get<SPIRType>(id: var_type.member_types[mbr_idx]); |
3243 | |
3244 | if (storage == StorageClassOutput && is_stage_output_block_member_masked(var, index: mbr_idx, strip_array: meta.strip_array)) |
3245 | { |
3246 | location = UINT32_MAX; // Skip this member and resolve location again on next var member |
3247 | |
3248 | if (is_block) |
3249 | masked_block = true; |
3250 | |
3251 | // Non-builtin block output variables are just ignored, since they will still access |
3252 | // the block variable as-is. They're just not flattened. |
3253 | if (is_builtin && !meta.strip_array) |
3254 | { |
3255 | // Emit a fake variable instead. |
3256 | uint32_t ids = ir.increase_bound_by(count: 2); |
3257 | uint32_t ptr_type_id = ids + 0; |
3258 | uint32_t var_id = ids + 1; |
3259 | |
3260 | auto ptr_type = mbr_type; |
3261 | ptr_type.pointer = true; |
3262 | ptr_type.pointer_depth++; |
3263 | ptr_type.parent_type = var_type.member_types[mbr_idx]; |
3264 | ptr_type.storage = StorageClassOutput; |
3265 | |
3266 | uint32_t initializer = 0; |
3267 | if (var.initializer) |
3268 | if (auto *c = maybe_get<SPIRConstant>(id: var.initializer)) |
3269 | initializer = c->subconstants[mbr_idx]; |
3270 | |
3271 | set<SPIRType>(id: ptr_type_id, args&: ptr_type); |
3272 | set<SPIRVariable>(id: var_id, args&: ptr_type_id, args: StorageClassOutput, args&: initializer); |
3273 | entry_func.add_local_variable(id: var_id); |
3274 | vars_needing_early_declaration.push_back(t: var_id); |
3275 | set_name(id: var_id, name: builtin_to_glsl(builtin, storage: StorageClassOutput)); |
3276 | set_decoration(id: var_id, decoration: DecorationBuiltIn, argument: builtin); |
3277 | } |
3278 | } |
3279 | else if (!is_builtin || has_active_builtin(builtin, storage)) |
3280 | { |
3281 | bool is_composite_type = is_matrix(type: mbr_type) || is_array(type: mbr_type) || mbr_type.basetype == SPIRType::Struct; |
3282 | bool attribute_load_store = |
3283 | storage == StorageClassInput && get_execution_model() != ExecutionModelFragment; |
3284 | bool storage_is_stage_io = variable_storage_requires_stage_io(storage); |
3285 | |
3286 | // Clip/CullDistance always need to be declared as user attributes. |
3287 | if (builtin == BuiltInClipDistance || builtin == BuiltInCullDistance) |
3288 | is_builtin = false; |
3289 | |
3290 | const string var_name = to_name(id: var.self); |
3291 | string mbr_name_qual = var_name; |
3292 | string var_chain_qual = var_name; |
3293 | if (elem_cnt > 1) |
3294 | { |
3295 | mbr_name_qual += join(ts: "_" , ts&: elem_idx); |
3296 | var_chain_qual += join(ts: "[" , ts&: elem_idx, ts: "]" ); |
3297 | } |
3298 | |
3299 | if ((!is_builtin || attribute_load_store) && storage_is_stage_io && is_composite_type) |
3300 | { |
3301 | add_composite_member_variable_to_interface_block(storage, ib_var_ref, ib_type, |
3302 | var, var_type, mbr_idx, meta, |
3303 | mbr_name_qual, var_chain_qual, |
3304 | location, var_mbr_idx); |
3305 | } |
3306 | else |
3307 | { |
3308 | add_plain_member_variable_to_interface_block(storage, ib_var_ref, ib_type, |
3309 | var, var_type, mbr_idx, meta, |
3310 | mbr_name_qual, var_chain_qual, |
3311 | location, var_mbr_idx); |
3312 | } |
3313 | } |
3314 | var_mbr_idx++; |
3315 | } |
3316 | } |
3317 | |
3318 | // If we're redirecting a block, we might still need to access the original block |
3319 | // variable if we're masking some members. |
3320 | if (masked_block && !needs_local_declaration && |
3321 | (!is_builtin_variable(var) || get_execution_model() == ExecutionModelTessellationControl)) |
3322 | { |
3323 | if (is_builtin_variable(var)) |
3324 | { |
3325 | // Ensure correct names for the block members if we're actually going to |
3326 | // declare gl_PerVertex. |
3327 | for (uint32_t mbr_idx = 0; mbr_idx < uint32_t(var_type.member_types.size()); mbr_idx++) |
3328 | { |
3329 | set_member_name(id: var_type.self, index: mbr_idx, name: builtin_to_glsl( |
3330 | builtin: BuiltIn(get_member_decoration(id: var_type.self, index: mbr_idx, decoration: DecorationBuiltIn)), |
3331 | storage: StorageClassOutput)); |
3332 | } |
3333 | |
3334 | set_name(id: var_type.self, name: "gl_PerVertex" ); |
3335 | set_name(id: var.self, name: "gl_out_masked" ); |
3336 | stage_out_masked_builtin_type_id = var_type.self; |
3337 | } |
3338 | emit_local_masked_variable(masked_var: var, strip_array: meta.strip_array); |
3339 | } |
3340 | } |
3341 | } |
3342 | else if (get_execution_model() == ExecutionModelTessellationEvaluation && storage == StorageClassInput && |
3343 | !meta.strip_array && is_builtin && (builtin == BuiltInTessLevelOuter || builtin == BuiltInTessLevelInner)) |
3344 | { |
3345 | add_tess_level_input_to_interface_block(ib_var_ref, ib_type, var); |
3346 | } |
3347 | else if (var_type.basetype == SPIRType::Boolean || var_type.basetype == SPIRType::Char || |
3348 | type_is_integral(type: var_type) || type_is_floating_point(type: var_type)) |
3349 | { |
3350 | if (!is_builtin || has_active_builtin(builtin, storage)) |
3351 | { |
3352 | bool is_composite_type = is_matrix(type: var_type) || is_array(type: var_type); |
3353 | bool storage_is_stage_io = variable_storage_requires_stage_io(storage); |
3354 | bool attribute_load_store = storage == StorageClassInput && get_execution_model() != ExecutionModelFragment; |
3355 | |
3356 | // Clip/CullDistance always needs to be declared as user attributes. |
3357 | if (builtin == BuiltInClipDistance || builtin == BuiltInCullDistance) |
3358 | is_builtin = false; |
3359 | |
3360 | // MSL does not allow matrices or arrays in input or output variables, so need to handle it specially. |
3361 | if ((!is_builtin || attribute_load_store) && storage_is_stage_io && is_composite_type) |
3362 | { |
3363 | add_composite_variable_to_interface_block(storage, ib_var_ref, ib_type, var, meta); |
3364 | } |
3365 | else |
3366 | { |
3367 | add_plain_variable_to_interface_block(storage, ib_var_ref, ib_type, var, meta); |
3368 | } |
3369 | } |
3370 | } |
3371 | } |
3372 | |
3373 | // Fix up the mapping of variables to interface member indices, which is used to compile access chains |
3374 | // for per-vertex variables in a tessellation control shader. |
3375 | void CompilerMSL::fix_up_interface_member_indices(StorageClass storage, uint32_t ib_type_id) |
3376 | { |
3377 | // Only needed for tessellation shaders and pull-model interpolants. |
3378 | // Need to redirect interface indices back to variables themselves. |
3379 | // For structs, each member of the struct need a separate instance. |
3380 | if (get_execution_model() != ExecutionModelTessellationControl && |
3381 | !(get_execution_model() == ExecutionModelTessellationEvaluation && storage == StorageClassInput) && |
3382 | !(get_execution_model() == ExecutionModelFragment && storage == StorageClassInput && |
3383 | !pull_model_inputs.empty())) |
3384 | return; |
3385 | |
3386 | auto mbr_cnt = uint32_t(ir.meta[ib_type_id].members.size()); |
3387 | for (uint32_t i = 0; i < mbr_cnt; i++) |
3388 | { |
3389 | uint32_t var_id = get_extended_member_decoration(type: ib_type_id, index: i, decoration: SPIRVCrossDecorationInterfaceOrigID); |
3390 | if (!var_id) |
3391 | continue; |
3392 | auto &var = get<SPIRVariable>(id: var_id); |
3393 | |
3394 | auto &type = get_variable_element_type(var); |
3395 | |
3396 | bool flatten_composites = variable_storage_requires_stage_io(storage: var.storage); |
3397 | bool is_block = has_decoration(id: type.self, decoration: DecorationBlock); |
3398 | |
3399 | uint32_t mbr_idx = uint32_t(-1); |
3400 | if (type.basetype == SPIRType::Struct && (flatten_composites || is_block)) |
3401 | mbr_idx = get_extended_member_decoration(type: ib_type_id, index: i, decoration: SPIRVCrossDecorationInterfaceMemberIndex); |
3402 | |
3403 | if (mbr_idx != uint32_t(-1)) |
3404 | { |
3405 | // Only set the lowest InterfaceMemberIndex for each variable member. |
3406 | // IB struct members will be emitted in-order w.r.t. interface member index. |
3407 | if (!has_extended_member_decoration(type: var_id, index: mbr_idx, decoration: SPIRVCrossDecorationInterfaceMemberIndex)) |
3408 | set_extended_member_decoration(type: var_id, index: mbr_idx, decoration: SPIRVCrossDecorationInterfaceMemberIndex, value: i); |
3409 | } |
3410 | else |
3411 | { |
3412 | // Only set the lowest InterfaceMemberIndex for each variable. |
3413 | // IB struct members will be emitted in-order w.r.t. interface member index. |
3414 | if (!has_extended_decoration(id: var_id, decoration: SPIRVCrossDecorationInterfaceMemberIndex)) |
3415 | set_extended_decoration(id: var_id, decoration: SPIRVCrossDecorationInterfaceMemberIndex, value: i); |
3416 | } |
3417 | } |
3418 | } |
3419 | |
3420 | // Add an interface structure for the type of storage, which is either StorageClassInput or StorageClassOutput. |
3421 | // Returns the ID of the newly added variable, or zero if no variable was added. |
3422 | uint32_t CompilerMSL::add_interface_block(StorageClass storage, bool patch) |
3423 | { |
3424 | // Accumulate the variables that should appear in the interface struct. |
3425 | SmallVector<SPIRVariable *> vars; |
3426 | bool incl_builtins = storage == StorageClassOutput || is_tessellation_shader(); |
3427 | bool has_seen_barycentric = false; |
3428 | |
3429 | InterfaceBlockMeta meta; |
3430 | |
3431 | // Varying interfaces between stages which use "user()" attribute can be dealt with |
3432 | // without explicit packing and unpacking of components. For any variables which link against the runtime |
3433 | // in some way (vertex attributes, fragment output, etc), we'll need to deal with it somehow. |
3434 | bool pack_components = |
3435 | (storage == StorageClassInput && get_execution_model() == ExecutionModelVertex) || |
3436 | (storage == StorageClassOutput && get_execution_model() == ExecutionModelFragment) || |
3437 | (storage == StorageClassOutput && get_execution_model() == ExecutionModelVertex && capture_output_to_buffer); |
3438 | |
3439 | ir.for_each_typed_id<SPIRVariable>(op: [&](uint32_t var_id, SPIRVariable &var) { |
3440 | if (var.storage != storage) |
3441 | return; |
3442 | |
3443 | auto &type = this->get<SPIRType>(id: var.basetype); |
3444 | |
3445 | bool is_builtin = is_builtin_variable(var); |
3446 | bool is_block = has_decoration(id: type.self, decoration: DecorationBlock); |
3447 | |
3448 | auto bi_type = BuiltInMax; |
3449 | bool builtin_is_gl_in_out = false; |
3450 | if (is_builtin && !is_block) |
3451 | { |
3452 | bi_type = BuiltIn(get_decoration(id: var_id, decoration: DecorationBuiltIn)); |
3453 | builtin_is_gl_in_out = bi_type == BuiltInPosition || bi_type == BuiltInPointSize || |
3454 | bi_type == BuiltInClipDistance || bi_type == BuiltInCullDistance; |
3455 | } |
3456 | |
3457 | if (is_builtin && is_block) |
3458 | builtin_is_gl_in_out = true; |
3459 | |
3460 | uint32_t location = get_decoration(id: var_id, decoration: DecorationLocation); |
3461 | |
3462 | bool builtin_is_stage_in_out = builtin_is_gl_in_out || |
3463 | bi_type == BuiltInLayer || bi_type == BuiltInViewportIndex || |
3464 | bi_type == BuiltInBaryCoordKHR || bi_type == BuiltInBaryCoordNoPerspKHR || |
3465 | bi_type == BuiltInFragDepth || |
3466 | bi_type == BuiltInFragStencilRefEXT || bi_type == BuiltInSampleMask; |
3467 | |
3468 | // These builtins are part of the stage in/out structs. |
3469 | bool is_interface_block_builtin = |
3470 | builtin_is_stage_in_out || |
3471 | (get_execution_model() == ExecutionModelTessellationEvaluation && |
3472 | (bi_type == BuiltInTessLevelOuter || bi_type == BuiltInTessLevelInner)); |
3473 | |
3474 | bool is_active = interface_variable_exists_in_entry_point(id: var.self); |
3475 | if (is_builtin && is_active) |
3476 | { |
3477 | // Only emit the builtin if it's active in this entry point. Interface variable list might lie. |
3478 | if (is_block) |
3479 | { |
3480 | // If any builtin is active, the block is active. |
3481 | uint32_t mbr_cnt = uint32_t(type.member_types.size()); |
3482 | for (uint32_t i = 0; !is_active && i < mbr_cnt; i++) |
3483 | is_active = has_active_builtin(builtin: BuiltIn(get_member_decoration(id: type.self, index: i, decoration: DecorationBuiltIn)), storage); |
3484 | } |
3485 | else |
3486 | { |
3487 | is_active = has_active_builtin(builtin: bi_type, storage); |
3488 | } |
3489 | } |
3490 | |
3491 | bool filter_patch_decoration = (has_decoration(id: var_id, decoration: DecorationPatch) || is_patch_block(type)) == patch; |
3492 | |
3493 | bool hidden = is_hidden_variable(var, include_builtins: incl_builtins); |
3494 | |
3495 | // ClipDistance is never hidden, we need to emulate it when used as an input. |
3496 | if (bi_type == BuiltInClipDistance || bi_type == BuiltInCullDistance) |
3497 | hidden = false; |
3498 | |
3499 | // It's not enough to simply avoid marking fragment outputs if the pipeline won't |
3500 | // accept them. We can't put them in the struct at all, or otherwise the compiler |
3501 | // complains that the outputs weren't explicitly marked. |
3502 | // Frag depth and stencil outputs are incompatible with explicit early fragment tests. |
3503 | // In GLSL, depth and stencil outputs are just ignored when explicit early fragment tests are required. |
3504 | // In Metal, it's a compilation error, so we need to exclude them from the output struct. |
3505 | if (get_execution_model() == ExecutionModelFragment && storage == StorageClassOutput && !patch && |
3506 | ((is_builtin && ((bi_type == BuiltInFragDepth && (!msl_options.enable_frag_depth_builtin || uses_explicit_early_fragment_test())) || |
3507 | (bi_type == BuiltInFragStencilRefEXT && (!msl_options.enable_frag_stencil_ref_builtin || uses_explicit_early_fragment_test())))) || |
3508 | (!is_builtin && !(msl_options.enable_frag_output_mask & (1 << location))))) |
3509 | { |
3510 | hidden = true; |
3511 | disabled_frag_outputs.push_back(t: var_id); |
3512 | // If a builtin, force it to have the proper name, and mark it as not part of the output struct. |
3513 | if (is_builtin) |
3514 | { |
3515 | set_name(id: var_id, name: builtin_to_glsl(builtin: bi_type, storage: StorageClassFunction)); |
3516 | mask_stage_output_by_builtin(builtin: bi_type); |
3517 | } |
3518 | } |
3519 | |
3520 | // Barycentric inputs must be emitted in stage-in, because they can have interpolation arguments. |
3521 | if (is_active && (bi_type == BuiltInBaryCoordKHR || bi_type == BuiltInBaryCoordNoPerspKHR)) |
3522 | { |
3523 | if (has_seen_barycentric) |
3524 | SPIRV_CROSS_THROW("Cannot declare both BaryCoordNV and BaryCoordNoPerspNV in same shader in MSL." ); |
3525 | has_seen_barycentric = true; |
3526 | hidden = false; |
3527 | } |
3528 | |
3529 | if (is_active && !hidden && type.pointer && filter_patch_decoration && |
3530 | (!is_builtin || is_interface_block_builtin)) |
3531 | { |
3532 | vars.push_back(t: &var); |
3533 | |
3534 | if (!is_builtin) |
3535 | { |
3536 | // Need to deal specially with DecorationComponent. |
3537 | // Multiple variables can alias the same Location, and try to make sure each location is declared only once. |
3538 | // We will swizzle data in and out to make this work. |
3539 | // This is only relevant for vertex inputs and fragment outputs. |
3540 | // Technically tessellation as well, but it is too complicated to support. |
3541 | uint32_t component = get_decoration(id: var_id, decoration: DecorationComponent); |
3542 | if (component != 0) |
3543 | { |
3544 | if (is_tessellation_shader()) |
3545 | SPIRV_CROSS_THROW("Component decoration is not supported in tessellation shaders." ); |
3546 | else if (pack_components) |
3547 | { |
3548 | uint32_t array_size = 1; |
3549 | if (!type.array.empty()) |
3550 | array_size = to_array_size_literal(type); |
3551 | |
3552 | for (uint32_t location_offset = 0; location_offset < array_size; location_offset++) |
3553 | { |
3554 | auto &location_meta = meta.location_meta[location + location_offset]; |
3555 | location_meta.num_components = std::max(a: location_meta.num_components, b: component + type.vecsize); |
3556 | |
3557 | // For variables sharing location, decorations and base type must match. |
3558 | location_meta.base_type_id = type.self; |
3559 | location_meta.flat = has_decoration(id: var.self, decoration: DecorationFlat); |
3560 | location_meta.noperspective = has_decoration(id: var.self, decoration: DecorationNoPerspective); |
3561 | location_meta.centroid = has_decoration(id: var.self, decoration: DecorationCentroid); |
3562 | location_meta.sample = has_decoration(id: var.self, decoration: DecorationSample); |
3563 | } |
3564 | } |
3565 | } |
3566 | } |
3567 | } |
3568 | }); |
3569 | |
3570 | // If no variables qualify, leave. |
3571 | // For patch input in a tessellation evaluation shader, the per-vertex stage inputs |
3572 | // are included in a special patch control point array. |
3573 | if (vars.empty() && !(storage == StorageClassInput && patch && stage_in_var_id)) |
3574 | return 0; |
3575 | |
3576 | // Add a new typed variable for this interface structure. |
3577 | // The initializer expression is allocated here, but populated when the function |
3578 | // declaraion is emitted, because it is cleared after each compilation pass. |
3579 | uint32_t next_id = ir.increase_bound_by(count: 3); |
3580 | uint32_t ib_type_id = next_id++; |
3581 | auto &ib_type = set<SPIRType>(ib_type_id); |
3582 | ib_type.basetype = SPIRType::Struct; |
3583 | ib_type.storage = storage; |
3584 | set_decoration(id: ib_type_id, decoration: DecorationBlock); |
3585 | |
3586 | uint32_t ib_var_id = next_id++; |
3587 | auto &var = set<SPIRVariable>(id: ib_var_id, args&: ib_type_id, args&: storage, args: 0); |
3588 | var.initializer = next_id++; |
3589 | |
3590 | string ib_var_ref; |
3591 | auto &entry_func = get<SPIRFunction>(id: ir.default_entry_point); |
3592 | switch (storage) |
3593 | { |
3594 | case StorageClassInput: |
3595 | ib_var_ref = patch ? patch_stage_in_var_name : stage_in_var_name; |
3596 | if (get_execution_model() == ExecutionModelTessellationControl) |
3597 | { |
3598 | // Add a hook to populate the shared workgroup memory containing the gl_in array. |
3599 | entry_func.fixup_hooks_in.push_back(t: [=]() { |
3600 | // Can't use PatchVertices, PrimitiveId, or InvocationId yet; the hooks for those may not have run yet. |
3601 | if (msl_options.multi_patch_workgroup) |
3602 | { |
3603 | // n.b. builtin_invocation_id_id here is the dispatch global invocation ID, |
3604 | // not the TC invocation ID. |
3605 | statement(ts: "device " , ts: to_name(id: ir.default_entry_point), ts: "_" , ts: ib_var_ref, ts: "* gl_in = &" , |
3606 | ts&: input_buffer_var_name, ts: "[min(" , ts: to_expression(id: builtin_invocation_id_id), ts: ".x / " , |
3607 | ts&: get_entry_point().output_vertices, |
3608 | ts: ", spvIndirectParams[1] - 1) * spvIndirectParams[0]];" ); |
3609 | } |
3610 | else |
3611 | { |
3612 | // It's safe to use InvocationId here because it's directly mapped to a |
3613 | // Metal builtin, and therefore doesn't need a hook. |
3614 | statement(ts: "if (" , ts: to_expression(id: builtin_invocation_id_id), ts: " < spvIndirectParams[0])" ); |
3615 | statement(ts: " " , ts&: input_wg_var_name, ts: "[" , ts: to_expression(id: builtin_invocation_id_id), |
3616 | ts: "] = " , ts: ib_var_ref, ts: ";" ); |
3617 | statement(ts: "threadgroup_barrier(mem_flags::mem_threadgroup);" ); |
3618 | statement(ts: "if (" , ts: to_expression(id: builtin_invocation_id_id), |
3619 | ts: " >= " , ts&: get_entry_point().output_vertices, ts: ")" ); |
3620 | statement(ts: " return;" ); |
3621 | } |
3622 | }); |
3623 | } |
3624 | break; |
3625 | |
3626 | case StorageClassOutput: |
3627 | { |
3628 | ib_var_ref = patch ? patch_stage_out_var_name : stage_out_var_name; |
3629 | |
3630 | // Add the output interface struct as a local variable to the entry function. |
3631 | // If the entry point should return the output struct, set the entry function |
3632 | // to return the output interface struct, otherwise to return nothing. |
3633 | // Watch out for the rare case where the terminator of the last entry point block is a |
3634 | // Kill, instead of a Return. Based on SPIR-V's block-domination rules, we assume that |
3635 | // any block that has a Kill will also have a terminating Return, except the last block. |
3636 | // Indicate the output var requires early initialization. |
3637 | bool ep_should_return_output = !get_is_rasterization_disabled(); |
3638 | uint32_t rtn_id = ep_should_return_output ? ib_var_id : 0; |
3639 | if (!capture_output_to_buffer) |
3640 | { |
3641 | entry_func.add_local_variable(id: ib_var_id); |
3642 | for (auto &blk_id : entry_func.blocks) |
3643 | { |
3644 | auto &blk = get<SPIRBlock>(id: blk_id); |
3645 | if (blk.terminator == SPIRBlock::Return || (blk.terminator == SPIRBlock::Kill && blk_id == entry_func.blocks.back())) |
3646 | blk.return_value = rtn_id; |
3647 | } |
3648 | vars_needing_early_declaration.push_back(t: ib_var_id); |
3649 | } |
3650 | else |
3651 | { |
3652 | switch (get_execution_model()) |
3653 | { |
3654 | case ExecutionModelVertex: |
3655 | case ExecutionModelTessellationEvaluation: |
3656 | // Instead of declaring a struct variable to hold the output and then |
3657 | // copying that to the output buffer, we'll declare the output variable |
3658 | // as a reference to the final output element in the buffer. Then we can |
3659 | // avoid the extra copy. |
3660 | entry_func.fixup_hooks_in.push_back(t: [=]() { |
3661 | if (stage_out_var_id) |
3662 | { |
3663 | // The first member of the indirect buffer is always the number of vertices |
3664 | // to draw. |
3665 | // We zero-base the InstanceID & VertexID variables for HLSL emulation elsewhere, so don't do it twice |
3666 | if (get_execution_model() == ExecutionModelVertex && msl_options.vertex_for_tessellation) |
3667 | { |
3668 | statement(ts: "device " , ts: to_name(id: ir.default_entry_point), ts: "_" , ts: ib_var_ref, ts: "& " , ts: ib_var_ref, |
3669 | ts: " = " , ts&: output_buffer_var_name, ts: "[" , ts: to_expression(id: builtin_invocation_id_id), |
3670 | ts: ".y * " , ts: to_expression(id: builtin_stage_input_size_id), ts: ".x + " , |
3671 | ts: to_expression(id: builtin_invocation_id_id), ts: ".x];" ); |
3672 | } |
3673 | else if (msl_options.enable_base_index_zero) |
3674 | { |
3675 | statement(ts: "device " , ts: to_name(id: ir.default_entry_point), ts: "_" , ts: ib_var_ref, ts: "& " , ts: ib_var_ref, |
3676 | ts: " = " , ts&: output_buffer_var_name, ts: "[" , ts: to_expression(id: builtin_instance_idx_id), |
3677 | ts: " * spvIndirectParams[0] + " , ts: to_expression(id: builtin_vertex_idx_id), ts: "];" ); |
3678 | } |
3679 | else |
3680 | { |
3681 | statement(ts: "device " , ts: to_name(id: ir.default_entry_point), ts: "_" , ts: ib_var_ref, ts: "& " , ts: ib_var_ref, |
3682 | ts: " = " , ts&: output_buffer_var_name, ts: "[(" , ts: to_expression(id: builtin_instance_idx_id), |
3683 | ts: " - " , ts: to_expression(id: builtin_base_instance_id), ts: ") * spvIndirectParams[0] + " , |
3684 | ts: to_expression(id: builtin_vertex_idx_id), ts: " - " , |
3685 | ts: to_expression(id: builtin_base_vertex_id), ts: "];" ); |
3686 | } |
3687 | } |
3688 | }); |
3689 | break; |
3690 | case ExecutionModelTessellationControl: |
3691 | if (msl_options.multi_patch_workgroup) |
3692 | { |
3693 | // We cannot use PrimitiveId here, because the hook may not have run yet. |
3694 | if (patch) |
3695 | { |
3696 | entry_func.fixup_hooks_in.push_back(t: [=]() { |
3697 | statement(ts: "device " , ts: to_name(id: ir.default_entry_point), ts: "_" , ts: ib_var_ref, ts: "& " , ts: ib_var_ref, |
3698 | ts: " = " , ts&: patch_output_buffer_var_name, ts: "[" , ts: to_expression(id: builtin_invocation_id_id), |
3699 | ts: ".x / " , ts&: get_entry_point().output_vertices, ts: "];" ); |
3700 | }); |
3701 | } |
3702 | else |
3703 | { |
3704 | entry_func.fixup_hooks_in.push_back(t: [=]() { |
3705 | statement(ts: "device " , ts: to_name(id: ir.default_entry_point), ts: "_" , ts: ib_var_ref, ts: "* gl_out = &" , |
3706 | ts&: output_buffer_var_name, ts: "[" , ts: to_expression(id: builtin_invocation_id_id), ts: ".x - " , |
3707 | ts: to_expression(id: builtin_invocation_id_id), ts: ".x % " , |
3708 | ts&: get_entry_point().output_vertices, ts: "];" ); |
3709 | }); |
3710 | } |
3711 | } |
3712 | else |
3713 | { |
3714 | if (patch) |
3715 | { |
3716 | entry_func.fixup_hooks_in.push_back(t: [=]() { |
3717 | statement(ts: "device " , ts: to_name(id: ir.default_entry_point), ts: "_" , ts: ib_var_ref, ts: "& " , ts: ib_var_ref, |
3718 | ts: " = " , ts&: patch_output_buffer_var_name, ts: "[" , ts: to_expression(id: builtin_primitive_id_id), |
3719 | ts: "];" ); |
3720 | }); |
3721 | } |
3722 | else |
3723 | { |
3724 | entry_func.fixup_hooks_in.push_back(t: [=]() { |
3725 | statement(ts: "device " , ts: to_name(id: ir.default_entry_point), ts: "_" , ts: ib_var_ref, ts: "* gl_out = &" , |
3726 | ts&: output_buffer_var_name, ts: "[" , ts: to_expression(id: builtin_primitive_id_id), ts: " * " , |
3727 | ts&: get_entry_point().output_vertices, ts: "];" ); |
3728 | }); |
3729 | } |
3730 | } |
3731 | break; |
3732 | default: |
3733 | break; |
3734 | } |
3735 | } |
3736 | break; |
3737 | } |
3738 | |
3739 | default: |
3740 | break; |
3741 | } |
3742 | |
3743 | set_name(id: ib_type_id, name: to_name(id: ir.default_entry_point) + "_" + ib_var_ref); |
3744 | set_name(id: ib_var_id, name: ib_var_ref); |
3745 | |
3746 | for (auto *p_var : vars) |
3747 | { |
3748 | bool strip_array = |
3749 | (get_execution_model() == ExecutionModelTessellationControl || |
3750 | (get_execution_model() == ExecutionModelTessellationEvaluation && storage == StorageClassInput)) && |
3751 | !patch; |
3752 | |
3753 | // Fixing up flattened stores in TESC is impossible since the memory is group shared either via |
3754 | // device (not masked) or threadgroup (masked) storage classes and it's race condition city. |
3755 | meta.strip_array = strip_array; |
3756 | meta.allow_local_declaration = !strip_array && !(get_execution_model() == ExecutionModelTessellationControl && |
3757 | storage == StorageClassOutput); |
3758 | add_variable_to_interface_block(storage, ib_var_ref, ib_type, var&: *p_var, meta); |
3759 | } |
3760 | |
3761 | if (get_execution_model() == ExecutionModelTessellationControl && msl_options.multi_patch_workgroup && |
3762 | storage == StorageClassInput) |
3763 | { |
3764 | // For tessellation control inputs, add all outputs from the vertex shader to ensure |
3765 | // the struct containing them is the correct size and layout. |
3766 | for (auto &input : inputs_by_location) |
3767 | { |
3768 | if (location_inputs_in_use.count(x: input.first.location) != 0) |
3769 | continue; |
3770 | |
3771 | // Create a fake variable to put at the location. |
3772 | uint32_t offset = ir.increase_bound_by(count: 4); |
3773 | uint32_t type_id = offset; |
3774 | uint32_t array_type_id = offset + 1; |
3775 | uint32_t ptr_type_id = offset + 2; |
3776 | uint32_t var_id = offset + 3; |
3777 | |
3778 | SPIRType type; |
3779 | switch (input.second.format) |
3780 | { |
3781 | case MSL_SHADER_INPUT_FORMAT_UINT16: |
3782 | case MSL_SHADER_INPUT_FORMAT_ANY16: |
3783 | type.basetype = SPIRType::UShort; |
3784 | type.width = 16; |
3785 | break; |
3786 | case MSL_SHADER_INPUT_FORMAT_ANY32: |
3787 | default: |
3788 | type.basetype = SPIRType::UInt; |
3789 | type.width = 32; |
3790 | break; |
3791 | } |
3792 | type.vecsize = input.second.vecsize; |
3793 | set<SPIRType>(id: type_id, args&: type); |
3794 | |
3795 | type.array.push_back(t: 0); |
3796 | type.array_size_literal.push_back(t: true); |
3797 | type.parent_type = type_id; |
3798 | set<SPIRType>(id: array_type_id, args&: type); |
3799 | |
3800 | type.pointer = true; |
3801 | type.pointer_depth++; |
3802 | type.parent_type = array_type_id; |
3803 | type.storage = storage; |
3804 | auto &ptr_type = set<SPIRType>(id: ptr_type_id, args&: type); |
3805 | ptr_type.self = array_type_id; |
3806 | |
3807 | auto &fake_var = set<SPIRVariable>(id: var_id, args&: ptr_type_id, args&: storage); |
3808 | set_decoration(id: var_id, decoration: DecorationLocation, argument: input.first.location); |
3809 | if (input.first.component) |
3810 | set_decoration(id: var_id, decoration: DecorationComponent, argument: input.first.component); |
3811 | |
3812 | meta.strip_array = true; |
3813 | meta.allow_local_declaration = false; |
3814 | add_variable_to_interface_block(storage, ib_var_ref, ib_type, var&: fake_var, meta); |
3815 | } |
3816 | } |
3817 | |
3818 | // When multiple variables need to access same location, |
3819 | // unroll locations one by one and we will flatten output or input as necessary. |
3820 | for (auto &loc : meta.location_meta) |
3821 | { |
3822 | uint32_t location = loc.first; |
3823 | auto &location_meta = loc.second; |
3824 | |
3825 | uint32_t ib_mbr_idx = uint32_t(ib_type.member_types.size()); |
3826 | uint32_t type_id = build_extended_vector_type(type_id: location_meta.base_type_id, components: location_meta.num_components); |
3827 | ib_type.member_types.push_back(t: type_id); |
3828 | |
3829 | set_member_name(id: ib_type.self, index: ib_mbr_idx, name: join(ts: "m_location_" , ts&: location)); |
3830 | set_member_decoration(id: ib_type.self, index: ib_mbr_idx, decoration: DecorationLocation, argument: location); |
3831 | mark_location_as_used_by_shader(location, type: get<SPIRType>(id: type_id), storage); |
3832 | |
3833 | if (location_meta.flat) |
3834 | set_member_decoration(id: ib_type.self, index: ib_mbr_idx, decoration: DecorationFlat); |
3835 | if (location_meta.noperspective) |
3836 | set_member_decoration(id: ib_type.self, index: ib_mbr_idx, decoration: DecorationNoPerspective); |
3837 | if (location_meta.centroid) |
3838 | set_member_decoration(id: ib_type.self, index: ib_mbr_idx, decoration: DecorationCentroid); |
3839 | if (location_meta.sample) |
3840 | set_member_decoration(id: ib_type.self, index: ib_mbr_idx, decoration: DecorationSample); |
3841 | } |
3842 | |
3843 | // Sort the members of the structure by their locations. |
3844 | MemberSorter member_sorter(ib_type, ir.meta[ib_type_id], MemberSorter::LocationThenBuiltInType); |
3845 | member_sorter.sort(); |
3846 | |
3847 | // The member indices were saved to the original variables, but after the members |
3848 | // were sorted, those indices are now likely incorrect. Fix those up now. |
3849 | fix_up_interface_member_indices(storage, ib_type_id); |
3850 | |
3851 | // For patch inputs, add one more member, holding the array of control point data. |
3852 | if (get_execution_model() == ExecutionModelTessellationEvaluation && storage == StorageClassInput && patch && |
3853 | stage_in_var_id) |
3854 | { |
3855 | uint32_t pcp_type_id = ir.increase_bound_by(count: 1); |
3856 | auto &pcp_type = set<SPIRType>(id: pcp_type_id, args&: ib_type); |
3857 | pcp_type.basetype = SPIRType::ControlPointArray; |
3858 | pcp_type.parent_type = pcp_type.type_alias = get_stage_in_struct_type().self; |
3859 | pcp_type.storage = storage; |
3860 | ir.meta[pcp_type_id] = ir.meta[ib_type.self]; |
3861 | uint32_t mbr_idx = uint32_t(ib_type.member_types.size()); |
3862 | ib_type.member_types.push_back(t: pcp_type_id); |
3863 | set_member_name(id: ib_type.self, index: mbr_idx, name: "gl_in" ); |
3864 | } |
3865 | |
3866 | return ib_var_id; |
3867 | } |
3868 | |
3869 | uint32_t CompilerMSL::add_interface_block_pointer(uint32_t ib_var_id, StorageClass storage) |
3870 | { |
3871 | if (!ib_var_id) |
3872 | return 0; |
3873 | |
3874 | uint32_t ib_ptr_var_id; |
3875 | uint32_t next_id = ir.increase_bound_by(count: 3); |
3876 | auto &ib_type = expression_type(id: ib_var_id); |
3877 | if (get_execution_model() == ExecutionModelTessellationControl) |
3878 | { |
3879 | // Tessellation control per-vertex I/O is presented as an array, so we must |
3880 | // do the same with our struct here. |
3881 | uint32_t ib_ptr_type_id = next_id++; |
3882 | auto &ib_ptr_type = set<SPIRType>(id: ib_ptr_type_id, args: ib_type); |
3883 | ib_ptr_type.parent_type = ib_ptr_type.type_alias = ib_type.self; |
3884 | ib_ptr_type.pointer = true; |
3885 | ib_ptr_type.pointer_depth++; |
3886 | ib_ptr_type.storage = |
3887 | storage == StorageClassInput ? |
3888 | (msl_options.multi_patch_workgroup ? StorageClassStorageBuffer : StorageClassWorkgroup) : |
3889 | StorageClassStorageBuffer; |
3890 | ir.meta[ib_ptr_type_id] = ir.meta[ib_type.self]; |
3891 | // To ensure that get_variable_data_type() doesn't strip off the pointer, |
3892 | // which we need, use another pointer. |
3893 | uint32_t ib_ptr_ptr_type_id = next_id++; |
3894 | auto &ib_ptr_ptr_type = set<SPIRType>(id: ib_ptr_ptr_type_id, args&: ib_ptr_type); |
3895 | ib_ptr_ptr_type.parent_type = ib_ptr_type_id; |
3896 | ib_ptr_ptr_type.type_alias = ib_type.self; |
3897 | ib_ptr_ptr_type.storage = StorageClassFunction; |
3898 | ir.meta[ib_ptr_ptr_type_id] = ir.meta[ib_type.self]; |
3899 | |
3900 | ib_ptr_var_id = next_id; |
3901 | set<SPIRVariable>(id: ib_ptr_var_id, args&: ib_ptr_ptr_type_id, args: StorageClassFunction, args: 0); |
3902 | set_name(id: ib_ptr_var_id, name: storage == StorageClassInput ? "gl_in" : "gl_out" ); |
3903 | } |
3904 | else |
3905 | { |
3906 | // Tessellation evaluation per-vertex inputs are also presented as arrays. |
3907 | // But, in Metal, this array uses a very special type, 'patch_control_point<T>', |
3908 | // which is a container that can be used to access the control point data. |
3909 | // To represent this, a special 'ControlPointArray' type has been added to the |
3910 | // SPIRV-Cross type system. It should only be generated by and seen in the MSL |
3911 | // backend (i.e. this one). |
3912 | uint32_t pcp_type_id = next_id++; |
3913 | auto &pcp_type = set<SPIRType>(id: pcp_type_id, args: ib_type); |
3914 | pcp_type.basetype = SPIRType::ControlPointArray; |
3915 | pcp_type.parent_type = pcp_type.type_alias = ib_type.self; |
3916 | pcp_type.storage = storage; |
3917 | ir.meta[pcp_type_id] = ir.meta[ib_type.self]; |
3918 | |
3919 | ib_ptr_var_id = next_id; |
3920 | set<SPIRVariable>(id: ib_ptr_var_id, args&: pcp_type_id, args&: storage, args: 0); |
3921 | set_name(id: ib_ptr_var_id, name: "gl_in" ); |
3922 | ir.meta[ib_ptr_var_id].decoration.qualified_alias = join(ts&: patch_stage_in_var_name, ts: ".gl_in" ); |
3923 | } |
3924 | return ib_ptr_var_id; |
3925 | } |
3926 | |
3927 | // Ensure that the type is compatible with the builtin. |
3928 | // If it is, simply return the given type ID. |
3929 | // Otherwise, create a new type, and return it's ID. |
3930 | uint32_t CompilerMSL::ensure_correct_builtin_type(uint32_t type_id, BuiltIn builtin) |
3931 | { |
3932 | auto &type = get<SPIRType>(id: type_id); |
3933 | |
3934 | if ((builtin == BuiltInSampleMask && is_array(type)) || |
3935 | ((builtin == BuiltInLayer || builtin == BuiltInViewportIndex || builtin == BuiltInFragStencilRefEXT) && |
3936 | type.basetype != SPIRType::UInt)) |
3937 | { |
3938 | uint32_t next_id = ir.increase_bound_by(count: type.pointer ? 2 : 1); |
3939 | uint32_t base_type_id = next_id++; |
3940 | auto &base_type = set<SPIRType>(base_type_id); |
3941 | base_type.basetype = SPIRType::UInt; |
3942 | base_type.width = 32; |
3943 | |
3944 | if (!type.pointer) |
3945 | return base_type_id; |
3946 | |
3947 | uint32_t ptr_type_id = next_id++; |
3948 | auto &ptr_type = set<SPIRType>(ptr_type_id); |
3949 | ptr_type = base_type; |
3950 | ptr_type.pointer = true; |
3951 | ptr_type.pointer_depth++; |
3952 | ptr_type.storage = type.storage; |
3953 | ptr_type.parent_type = base_type_id; |
3954 | return ptr_type_id; |
3955 | } |
3956 | |
3957 | return type_id; |
3958 | } |
3959 | |
3960 | // Ensure that the type is compatible with the shader input. |
3961 | // If it is, simply return the given type ID. |
3962 | // Otherwise, create a new type, and return its ID. |
3963 | uint32_t CompilerMSL::ensure_correct_input_type(uint32_t type_id, uint32_t location, uint32_t component, uint32_t num_components, bool strip_array) |
3964 | { |
3965 | auto &type = get<SPIRType>(id: type_id); |
3966 | |
3967 | uint32_t max_array_dimensions = strip_array ? 1 : 0; |
3968 | |
3969 | // Struct and array types must match exactly. |
3970 | if (type.basetype == SPIRType::Struct || type.array.size() > max_array_dimensions) |
3971 | return type_id; |
3972 | |
3973 | auto p_va = inputs_by_location.find(x: {.location: location, .component: component}); |
3974 | if (p_va == end(cont&: inputs_by_location)) |
3975 | { |
3976 | if (num_components > type.vecsize) |
3977 | return build_extended_vector_type(type_id, components: num_components); |
3978 | else |
3979 | return type_id; |
3980 | } |
3981 | |
3982 | if (num_components == 0) |
3983 | num_components = p_va->second.vecsize; |
3984 | |
3985 | switch (p_va->second.format) |
3986 | { |
3987 | case MSL_SHADER_INPUT_FORMAT_UINT8: |
3988 | { |
3989 | switch (type.basetype) |
3990 | { |
3991 | case SPIRType::UByte: |
3992 | case SPIRType::UShort: |
3993 | case SPIRType::UInt: |
3994 | if (num_components > type.vecsize) |
3995 | return build_extended_vector_type(type_id, components: num_components); |
3996 | else |
3997 | return type_id; |
3998 | |
3999 | case SPIRType::Short: |
4000 | return build_extended_vector_type(type_id, components: num_components > type.vecsize ? num_components : type.vecsize, |
4001 | basetype: SPIRType::UShort); |
4002 | case SPIRType::Int: |
4003 | return build_extended_vector_type(type_id, components: num_components > type.vecsize ? num_components : type.vecsize, |
4004 | basetype: SPIRType::UInt); |
4005 | |
4006 | default: |
4007 | SPIRV_CROSS_THROW("Vertex attribute type mismatch between host and shader" ); |
4008 | } |
4009 | } |
4010 | |
4011 | case MSL_SHADER_INPUT_FORMAT_UINT16: |
4012 | { |
4013 | switch (type.basetype) |
4014 | { |
4015 | case SPIRType::UShort: |
4016 | case SPIRType::UInt: |
4017 | if (num_components > type.vecsize) |
4018 | return build_extended_vector_type(type_id, components: num_components); |
4019 | else |
4020 | return type_id; |
4021 | |
4022 | case SPIRType::Int: |
4023 | return build_extended_vector_type(type_id, components: num_components > type.vecsize ? num_components : type.vecsize, |
4024 | basetype: SPIRType::UInt); |
4025 | |
4026 | default: |
4027 | SPIRV_CROSS_THROW("Vertex attribute type mismatch between host and shader" ); |
4028 | } |
4029 | } |
4030 | |
4031 | default: |
4032 | if (num_components > type.vecsize) |
4033 | type_id = build_extended_vector_type(type_id, components: num_components); |
4034 | break; |
4035 | } |
4036 | |
4037 | return type_id; |
4038 | } |
4039 | |
4040 | void CompilerMSL::mark_struct_members_packed(const SPIRType &type) |
4041 | { |
4042 | set_extended_decoration(id: type.self, decoration: SPIRVCrossDecorationPhysicalTypePacked); |
4043 | |
4044 | // Problem case! Struct needs to be placed at an awkward alignment. |
4045 | // Mark every member of the child struct as packed. |
4046 | uint32_t mbr_cnt = uint32_t(type.member_types.size()); |
4047 | for (uint32_t i = 0; i < mbr_cnt; i++) |
4048 | { |
4049 | auto &mbr_type = get<SPIRType>(id: type.member_types[i]); |
4050 | if (mbr_type.basetype == SPIRType::Struct) |
4051 | { |
4052 | // Recursively mark structs as packed. |
4053 | auto *struct_type = &mbr_type; |
4054 | while (!struct_type->array.empty()) |
4055 | struct_type = &get<SPIRType>(id: struct_type->parent_type); |
4056 | mark_struct_members_packed(type: *struct_type); |
4057 | } |
4058 | else if (!is_scalar(type: mbr_type)) |
4059 | set_extended_member_decoration(type: type.self, index: i, decoration: SPIRVCrossDecorationPhysicalTypePacked); |
4060 | } |
4061 | } |
4062 | |
4063 | void CompilerMSL::mark_scalar_layout_structs(const SPIRType &type) |
4064 | { |
4065 | uint32_t mbr_cnt = uint32_t(type.member_types.size()); |
4066 | for (uint32_t i = 0; i < mbr_cnt; i++) |
4067 | { |
4068 | auto &mbr_type = get<SPIRType>(id: type.member_types[i]); |
4069 | if (mbr_type.basetype == SPIRType::Struct) |
4070 | { |
4071 | auto *struct_type = &mbr_type; |
4072 | while (!struct_type->array.empty()) |
4073 | struct_type = &get<SPIRType>(id: struct_type->parent_type); |
4074 | |
4075 | if (has_extended_decoration(id: struct_type->self, decoration: SPIRVCrossDecorationPhysicalTypePacked)) |
4076 | continue; |
4077 | |
4078 | uint32_t msl_alignment = get_declared_struct_member_alignment_msl(struct_type: type, index: i); |
4079 | uint32_t msl_size = get_declared_struct_member_size_msl(struct_type: type, index: i); |
4080 | uint32_t spirv_offset = type_struct_member_offset(type, index: i); |
4081 | uint32_t spirv_offset_next; |
4082 | if (i + 1 < mbr_cnt) |
4083 | spirv_offset_next = type_struct_member_offset(type, index: i + 1); |
4084 | else |
4085 | spirv_offset_next = spirv_offset + msl_size; |
4086 | |
4087 | // Both are complicated cases. In scalar layout, a struct of float3 might just consume 12 bytes, |
4088 | // and the next member will be placed at offset 12. |
4089 | bool struct_is_misaligned = (spirv_offset % msl_alignment) != 0; |
4090 | bool struct_is_too_large = spirv_offset + msl_size > spirv_offset_next; |
4091 | uint32_t array_stride = 0; |
4092 | bool struct_needs_explicit_padding = false; |
4093 | |
4094 | // Verify that if a struct is used as an array that ArrayStride matches the effective size of the struct. |
4095 | if (!mbr_type.array.empty()) |
4096 | { |
4097 | array_stride = type_struct_member_array_stride(type, index: i); |
4098 | uint32_t dimensions = uint32_t(mbr_type.array.size() - 1); |
4099 | for (uint32_t dim = 0; dim < dimensions; dim++) |
4100 | { |
4101 | uint32_t array_size = to_array_size_literal(type: mbr_type, index: dim); |
4102 | array_stride /= max(a: array_size, b: 1u); |
4103 | } |
4104 | |
4105 | // Set expected struct size based on ArrayStride. |
4106 | struct_needs_explicit_padding = true; |
4107 | |
4108 | // If struct size is larger than array stride, we might be able to fit, if we tightly pack. |
4109 | if (get_declared_struct_size_msl(struct_type: *struct_type) > array_stride) |
4110 | struct_is_too_large = true; |
4111 | } |
4112 | |
4113 | if (struct_is_misaligned || struct_is_too_large) |
4114 | mark_struct_members_packed(type: *struct_type); |
4115 | mark_scalar_layout_structs(type: *struct_type); |
4116 | |
4117 | if (struct_needs_explicit_padding) |
4118 | { |
4119 | msl_size = get_declared_struct_size_msl(struct_type: *struct_type, ignore_alignment: true, ignore_padding: true); |
4120 | if (array_stride < msl_size) |
4121 | { |
4122 | SPIRV_CROSS_THROW("Cannot express an array stride smaller than size of struct type." ); |
4123 | } |
4124 | else |
4125 | { |
4126 | if (has_extended_decoration(id: struct_type->self, decoration: SPIRVCrossDecorationPaddingTarget)) |
4127 | { |
4128 | if (array_stride != |
4129 | get_extended_decoration(id: struct_type->self, decoration: SPIRVCrossDecorationPaddingTarget)) |
4130 | SPIRV_CROSS_THROW( |
4131 | "A struct is used with different array strides. Cannot express this in MSL." ); |
4132 | } |
4133 | else |
4134 | set_extended_decoration(id: struct_type->self, decoration: SPIRVCrossDecorationPaddingTarget, value: array_stride); |
4135 | } |
4136 | } |
4137 | } |
4138 | } |
4139 | } |
4140 | |
4141 | // Sort the members of the struct type by offset, and pack and then pad members where needed |
4142 | // to align MSL members with SPIR-V offsets. The struct members are iterated twice. Packing |
4143 | // occurs first, followed by padding, because packing a member reduces both its size and its |
4144 | // natural alignment, possibly requiring a padding member to be added ahead of it. |
4145 | void CompilerMSL::align_struct(SPIRType &ib_type, unordered_set<uint32_t> &aligned_structs) |
4146 | { |
4147 | // We align structs recursively, so stop any redundant work. |
4148 | ID &ib_type_id = ib_type.self; |
4149 | if (aligned_structs.count(x: ib_type_id)) |
4150 | return; |
4151 | aligned_structs.insert(x: ib_type_id); |
4152 | |
4153 | // Sort the members of the interface structure by their offset. |
4154 | // They should already be sorted per SPIR-V spec anyway. |
4155 | MemberSorter member_sorter(ib_type, ir.meta[ib_type_id], MemberSorter::Offset); |
4156 | member_sorter.sort(); |
4157 | |
4158 | auto mbr_cnt = uint32_t(ib_type.member_types.size()); |
4159 | |
4160 | for (uint32_t mbr_idx = 0; mbr_idx < mbr_cnt; mbr_idx++) |
4161 | { |
4162 | // Pack any dependent struct types before we pack a parent struct. |
4163 | auto &mbr_type = get<SPIRType>(id: ib_type.member_types[mbr_idx]); |
4164 | if (mbr_type.basetype == SPIRType::Struct) |
4165 | align_struct(ib_type&: mbr_type, aligned_structs); |
4166 | } |
4167 | |
4168 | // Test the alignment of each member, and if a member should be closer to the previous |
4169 | // member than the default spacing expects, it is likely that the previous member is in |
4170 | // a packed format. If so, and the previous member is packable, pack it. |
4171 | // For example ... this applies to any 3-element vector that is followed by a scalar. |
4172 | uint32_t msl_offset = 0; |
4173 | for (uint32_t mbr_idx = 0; mbr_idx < mbr_cnt; mbr_idx++) |
4174 | { |
4175 | // This checks the member in isolation, if the member needs some kind of type remapping to conform to SPIR-V |
4176 | // offsets, array strides and matrix strides. |
4177 | ensure_member_packing_rules_msl(ib_type, index: mbr_idx); |
4178 | |
4179 | // Align current offset to the current member's default alignment. If the member was packed, it will observe |
4180 | // the updated alignment here. |
4181 | uint32_t msl_align_mask = get_declared_struct_member_alignment_msl(struct_type: ib_type, index: mbr_idx) - 1; |
4182 | uint32_t aligned_msl_offset = (msl_offset + msl_align_mask) & ~msl_align_mask; |
4183 | |
4184 | // Fetch the member offset as declared in the SPIRV. |
4185 | uint32_t spirv_mbr_offset = get_member_decoration(id: ib_type_id, index: mbr_idx, decoration: DecorationOffset); |
4186 | if (spirv_mbr_offset > aligned_msl_offset) |
4187 | { |
4188 | // Since MSL and SPIR-V have slightly different struct member alignment and |
4189 | // size rules, we'll pad to standard C-packing rules with a char[] array. If the member is farther |
4190 | // away than C-packing, expects, add an inert padding member before the the member. |
4191 | uint32_t padding_bytes = spirv_mbr_offset - aligned_msl_offset; |
4192 | set_extended_member_decoration(type: ib_type_id, index: mbr_idx, decoration: SPIRVCrossDecorationPaddingTarget, value: padding_bytes); |
4193 | |
4194 | // Re-align as a sanity check that aligning post-padding matches up. |
4195 | msl_offset += padding_bytes; |
4196 | aligned_msl_offset = (msl_offset + msl_align_mask) & ~msl_align_mask; |
4197 | } |
4198 | else if (spirv_mbr_offset < aligned_msl_offset) |
4199 | { |
4200 | // This should not happen, but deal with unexpected scenarios. |
4201 | // It *might* happen if a sub-struct has a larger alignment requirement in MSL than SPIR-V. |
4202 | SPIRV_CROSS_THROW("Cannot represent buffer block correctly in MSL." ); |
4203 | } |
4204 | |
4205 | assert(aligned_msl_offset == spirv_mbr_offset); |
4206 | |
4207 | // Increment the current offset to be positioned immediately after the current member. |
4208 | // Don't do this for the last member since it can be unsized, and it is not relevant for padding purposes here. |
4209 | if (mbr_idx + 1 < mbr_cnt) |
4210 | msl_offset = aligned_msl_offset + get_declared_struct_member_size_msl(struct_type: ib_type, index: mbr_idx); |
4211 | } |
4212 | } |
4213 | |
4214 | bool CompilerMSL::validate_member_packing_rules_msl(const SPIRType &type, uint32_t index) const |
4215 | { |
4216 | auto &mbr_type = get<SPIRType>(id: type.member_types[index]); |
4217 | uint32_t spirv_offset = get_member_decoration(id: type.self, index, decoration: DecorationOffset); |
4218 | |
4219 | if (index + 1 < type.member_types.size()) |
4220 | { |
4221 | // First, we will check offsets. If SPIR-V offset + MSL size > SPIR-V offset of next member, |
4222 | // we *must* perform some kind of remapping, no way getting around it. |
4223 | // We can always pad after this member if necessary, so that case is fine. |
4224 | uint32_t spirv_offset_next = get_member_decoration(id: type.self, index: index + 1, decoration: DecorationOffset); |
4225 | assert(spirv_offset_next >= spirv_offset); |
4226 | uint32_t maximum_size = spirv_offset_next - spirv_offset; |
4227 | uint32_t msl_mbr_size = get_declared_struct_member_size_msl(struct_type: type, index); |
4228 | if (msl_mbr_size > maximum_size) |
4229 | return false; |
4230 | } |
4231 | |
4232 | if (!mbr_type.array.empty()) |
4233 | { |
4234 | // If we have an array type, array stride must match exactly with SPIR-V. |
4235 | |
4236 | // An exception to this requirement is if we have one array element. |
4237 | // This comes from DX scalar layout workaround. |
4238 | // If app tries to be cheeky and access the member out of bounds, this will not work, but this is the best we can do. |
4239 | // In OpAccessChain with logical memory models, access chains must be in-bounds in SPIR-V specification. |
4240 | bool relax_array_stride = mbr_type.array.back() == 1 && mbr_type.array_size_literal.back(); |
4241 | |
4242 | if (!relax_array_stride) |
4243 | { |
4244 | uint32_t spirv_array_stride = type_struct_member_array_stride(type, index); |
4245 | uint32_t msl_array_stride = get_declared_struct_member_array_stride_msl(struct_type: type, index); |
4246 | if (spirv_array_stride != msl_array_stride) |
4247 | return false; |
4248 | } |
4249 | } |
4250 | |
4251 | if (is_matrix(type: mbr_type)) |
4252 | { |
4253 | // Need to check MatrixStride as well. |
4254 | uint32_t spirv_matrix_stride = type_struct_member_matrix_stride(type, index); |
4255 | uint32_t msl_matrix_stride = get_declared_struct_member_matrix_stride_msl(struct_type: type, index); |
4256 | if (spirv_matrix_stride != msl_matrix_stride) |
4257 | return false; |
4258 | } |
4259 | |
4260 | // Now, we check alignment. |
4261 | uint32_t msl_alignment = get_declared_struct_member_alignment_msl(struct_type: type, index); |
4262 | if ((spirv_offset % msl_alignment) != 0) |
4263 | return false; |
4264 | |
4265 | // We're in the clear. |
4266 | return true; |
4267 | } |
4268 | |
4269 | // Here we need to verify that the member type we declare conforms to Offset, ArrayStride or MatrixStride restrictions. |
4270 | // If there is a mismatch, we need to emit remapped types, either normal types, or "packed_X" types. |
4271 | // In odd cases we need to emit packed and remapped types, for e.g. weird matrices or arrays with weird array strides. |
4272 | void CompilerMSL::ensure_member_packing_rules_msl(SPIRType &ib_type, uint32_t index) |
4273 | { |
4274 | if (validate_member_packing_rules_msl(type: ib_type, index)) |
4275 | return; |
4276 | |
4277 | // We failed validation. |
4278 | // This case will be nightmare-ish to deal with. This could possibly happen if struct alignment does not quite |
4279 | // match up with what we want. Scalar block layout comes to mind here where we might have to work around the rule |
4280 | // that struct alignment == max alignment of all members and struct size depends on this alignment. |
4281 | auto &mbr_type = get<SPIRType>(id: ib_type.member_types[index]); |
4282 | if (mbr_type.basetype == SPIRType::Struct) |
4283 | SPIRV_CROSS_THROW("Cannot perform any repacking for structs when it is used as a member of another struct." ); |
4284 | |
4285 | // Perform remapping here. |
4286 | // There is nothing to be gained by using packed scalars, so don't attempt it. |
4287 | if (!is_scalar(type: ib_type)) |
4288 | set_extended_member_decoration(type: ib_type.self, index, decoration: SPIRVCrossDecorationPhysicalTypePacked); |
4289 | |
4290 | // Try validating again, now with packed. |
4291 | if (validate_member_packing_rules_msl(type: ib_type, index)) |
4292 | return; |
4293 | |
4294 | // We're in deep trouble, and we need to create a new PhysicalType which matches up with what we expect. |
4295 | // A lot of work goes here ... |
4296 | // We will need remapping on Load and Store to translate the types between Logical and Physical. |
4297 | |
4298 | // First, we check if we have small vector std140 array. |
4299 | // We detect this if we have an array of vectors, and array stride is greater than number of elements. |
4300 | if (!mbr_type.array.empty() && !is_matrix(type: mbr_type)) |
4301 | { |
4302 | uint32_t array_stride = type_struct_member_array_stride(type: ib_type, index); |
4303 | |
4304 | // Hack off array-of-arrays until we find the array stride per element we must have to make it work. |
4305 | uint32_t dimensions = uint32_t(mbr_type.array.size() - 1); |
4306 | for (uint32_t dim = 0; dim < dimensions; dim++) |
4307 | array_stride /= max(a: to_array_size_literal(type: mbr_type, index: dim), b: 1u); |
4308 | |
4309 | uint32_t elems_per_stride = array_stride / (mbr_type.width / 8); |
4310 | |
4311 | if (elems_per_stride == 3) |
4312 | SPIRV_CROSS_THROW("Cannot use ArrayStride of 3 elements in remapping scenarios." ); |
4313 | else if (elems_per_stride > 4) |
4314 | SPIRV_CROSS_THROW("Cannot represent vectors with more than 4 elements in MSL." ); |
4315 | |
4316 | auto physical_type = mbr_type; |
4317 | physical_type.vecsize = elems_per_stride; |
4318 | physical_type.parent_type = 0; |
4319 | uint32_t type_id = ir.increase_bound_by(count: 1); |
4320 | set<SPIRType>(id: type_id, args&: physical_type); |
4321 | set_extended_member_decoration(type: ib_type.self, index, decoration: SPIRVCrossDecorationPhysicalTypeID, value: type_id); |
4322 | set_decoration(id: type_id, decoration: DecorationArrayStride, argument: array_stride); |
4323 | |
4324 | // Remove packed_ for vectors of size 1, 2 and 4. |
4325 | unset_extended_member_decoration(type: ib_type.self, index, decoration: SPIRVCrossDecorationPhysicalTypePacked); |
4326 | } |
4327 | else if (is_matrix(type: mbr_type)) |
4328 | { |
4329 | // MatrixStride might be std140-esque. |
4330 | uint32_t matrix_stride = type_struct_member_matrix_stride(type: ib_type, index); |
4331 | |
4332 | uint32_t elems_per_stride = matrix_stride / (mbr_type.width / 8); |
4333 | |
4334 | if (elems_per_stride == 3) |
4335 | SPIRV_CROSS_THROW("Cannot use ArrayStride of 3 elements in remapping scenarios." ); |
4336 | else if (elems_per_stride > 4) |
4337 | SPIRV_CROSS_THROW("Cannot represent vectors with more than 4 elements in MSL." ); |
4338 | |
4339 | bool row_major = has_member_decoration(id: ib_type.self, index, decoration: DecorationRowMajor); |
4340 | |
4341 | auto physical_type = mbr_type; |
4342 | physical_type.parent_type = 0; |
4343 | if (row_major) |
4344 | physical_type.columns = elems_per_stride; |
4345 | else |
4346 | physical_type.vecsize = elems_per_stride; |
4347 | uint32_t type_id = ir.increase_bound_by(count: 1); |
4348 | set<SPIRType>(id: type_id, args&: physical_type); |
4349 | set_extended_member_decoration(type: ib_type.self, index, decoration: SPIRVCrossDecorationPhysicalTypeID, value: type_id); |
4350 | |
4351 | // Remove packed_ for vectors of size 1, 2 and 4. |
4352 | unset_extended_member_decoration(type: ib_type.self, index, decoration: SPIRVCrossDecorationPhysicalTypePacked); |
4353 | } |
4354 | else |
4355 | SPIRV_CROSS_THROW("Found a buffer packing case which we cannot represent in MSL." ); |
4356 | |
4357 | // Try validating again, now with physical type remapping. |
4358 | if (validate_member_packing_rules_msl(type: ib_type, index)) |
4359 | return; |
4360 | |
4361 | // We might have a particular odd scalar layout case where the last element of an array |
4362 | // does not take up as much space as the ArrayStride or MatrixStride. This can happen with DX cbuffers. |
4363 | // The "proper" workaround for this is extremely painful and essentially impossible in the edge case of float3[], |
4364 | // so we hack around it by declaring the offending array or matrix with one less array size/col/row, |
4365 | // and rely on padding to get the correct value. We will technically access arrays out of bounds into the padding region, |
4366 | // but it should spill over gracefully without too much trouble. We rely on behavior like this for unsized arrays anyways. |
4367 | |
4368 | // E.g. we might observe a physical layout of: |
4369 | // { float2 a[2]; float b; } in cbuffer layout where ArrayStride of a is 16, but offset of b is 24, packed right after a[1] ... |
4370 | uint32_t type_id = get_extended_member_decoration(type: ib_type.self, index, decoration: SPIRVCrossDecorationPhysicalTypeID); |
4371 | auto &type = get<SPIRType>(id: type_id); |
4372 | |
4373 | // Modify the physical type in-place. This is safe since each physical type workaround is a copy. |
4374 | if (is_array(type)) |
4375 | { |
4376 | if (type.array.back() > 1) |
4377 | { |
4378 | if (!type.array_size_literal.back()) |
4379 | SPIRV_CROSS_THROW("Cannot apply scalar layout workaround with spec constant array size." ); |
4380 | type.array.back() -= 1; |
4381 | } |
4382 | else |
4383 | { |
4384 | // We have an array of size 1, so we cannot decrement that. Our only option now is to |
4385 | // force a packed layout instead, and drop the physical type remap since ArrayStride is meaningless now. |
4386 | unset_extended_member_decoration(type: ib_type.self, index, decoration: SPIRVCrossDecorationPhysicalTypeID); |
4387 | set_extended_member_decoration(type: ib_type.self, index, decoration: SPIRVCrossDecorationPhysicalTypePacked); |
4388 | } |
4389 | } |
4390 | else if (is_matrix(type)) |
4391 | { |
4392 | bool row_major = has_member_decoration(id: ib_type.self, index, decoration: DecorationRowMajor); |
4393 | if (!row_major) |
4394 | { |
4395 | // Slice off one column. If we only have 2 columns, this might turn the matrix into a vector with one array element instead. |
4396 | if (type.columns > 2) |
4397 | { |
4398 | type.columns--; |
4399 | } |
4400 | else if (type.columns == 2) |
4401 | { |
4402 | type.columns = 1; |
4403 | assert(type.array.empty()); |
4404 | type.array.push_back(t: 1); |
4405 | type.array_size_literal.push_back(t: true); |
4406 | } |
4407 | } |
4408 | else |
4409 | { |
4410 | // Slice off one row. If we only have 2 rows, this might turn the matrix into a vector with one array element instead. |
4411 | if (type.vecsize > 2) |
4412 | { |
4413 | type.vecsize--; |
4414 | } |
4415 | else if (type.vecsize == 2) |
4416 | { |
4417 | type.vecsize = type.columns; |
4418 | type.columns = 1; |
4419 | assert(type.array.empty()); |
4420 | type.array.push_back(t: 1); |
4421 | type.array_size_literal.push_back(t: true); |
4422 | } |
4423 | } |
4424 | } |
4425 | |
4426 | // This better validate now, or we must fail gracefully. |
4427 | if (!validate_member_packing_rules_msl(type: ib_type, index)) |
4428 | SPIRV_CROSS_THROW("Found a buffer packing case which we cannot represent in MSL." ); |
4429 | } |
4430 | |
4431 | void CompilerMSL::emit_store_statement(uint32_t lhs_expression, uint32_t rhs_expression) |
4432 | { |
4433 | auto &type = expression_type(id: rhs_expression); |
4434 | |
4435 | bool lhs_remapped_type = has_extended_decoration(id: lhs_expression, decoration: SPIRVCrossDecorationPhysicalTypeID); |
4436 | bool lhs_packed_type = has_extended_decoration(id: lhs_expression, decoration: SPIRVCrossDecorationPhysicalTypePacked); |
4437 | auto *lhs_e = maybe_get<SPIRExpression>(id: lhs_expression); |
4438 | auto *rhs_e = maybe_get<SPIRExpression>(id: rhs_expression); |
4439 | |
4440 | bool transpose = lhs_e && lhs_e->need_transpose; |
4441 | |
4442 | // No physical type remapping, and no packed type, so can just emit a store directly. |
4443 | if (!lhs_remapped_type && !lhs_packed_type) |
4444 | { |
4445 | // We might not be dealing with remapped physical types or packed types, |
4446 | // but we might be doing a clean store to a row-major matrix. |
4447 | // In this case, we just flip transpose states, and emit the store, a transpose must be in the RHS expression, if any. |
4448 | if (is_matrix(type) && lhs_e && lhs_e->need_transpose) |
4449 | { |
4450 | lhs_e->need_transpose = false; |
4451 | |
4452 | if (rhs_e && rhs_e->need_transpose) |
4453 | { |
4454 | // Direct copy, but might need to unpack RHS. |
4455 | // Skip the transpose, as we will transpose when writing to LHS and transpose(transpose(T)) == T. |
4456 | rhs_e->need_transpose = false; |
4457 | statement(ts: to_expression(id: lhs_expression), ts: " = " , ts: to_unpacked_row_major_matrix_expression(id: rhs_expression), |
4458 | ts: ";" ); |
4459 | rhs_e->need_transpose = true; |
4460 | } |
4461 | else |
4462 | statement(ts: to_expression(id: lhs_expression), ts: " = transpose(" , ts: to_unpacked_expression(id: rhs_expression), ts: ");" ); |
4463 | |
4464 | lhs_e->need_transpose = true; |
4465 | register_write(chain: lhs_expression); |
4466 | } |
4467 | else if (lhs_e && lhs_e->need_transpose) |
4468 | { |
4469 | lhs_e->need_transpose = false; |
4470 | |
4471 | // Storing a column to a row-major matrix. Unroll the write. |
4472 | for (uint32_t c = 0; c < type.vecsize; c++) |
4473 | { |
4474 | auto lhs_expr = to_dereferenced_expression(id: lhs_expression); |
4475 | auto column_index = lhs_expr.find_last_of(c: '['); |
4476 | if (column_index != string::npos) |
4477 | { |
4478 | statement(ts&: lhs_expr.insert(pos1: column_index, str: join(ts: '[', ts&: c, ts: ']')), ts: " = " , |
4479 | ts: to_extract_component_expression(id: rhs_expression, index: c), ts: ";" ); |
4480 | } |
4481 | } |
4482 | lhs_e->need_transpose = true; |
4483 | register_write(chain: lhs_expression); |
4484 | } |
4485 | else |
4486 | CompilerGLSL::emit_store_statement(lhs_expression, rhs_expression); |
4487 | } |
4488 | else if (!lhs_remapped_type && !is_matrix(type) && !transpose) |
4489 | { |
4490 | // Even if the target type is packed, we can directly store to it. We cannot store to packed matrices directly, |
4491 | // since they are declared as array of vectors instead, and we need the fallback path below. |
4492 | CompilerGLSL::emit_store_statement(lhs_expression, rhs_expression); |
4493 | } |
4494 | else |
4495 | { |
4496 | // Special handling when storing to a remapped physical type. |
4497 | // This is mostly to deal with std140 padded matrices or vectors. |
4498 | |
4499 | TypeID physical_type_id = lhs_remapped_type ? |
4500 | ID(get_extended_decoration(id: lhs_expression, decoration: SPIRVCrossDecorationPhysicalTypeID)) : |
4501 | type.self; |
4502 | |
4503 | auto &physical_type = get<SPIRType>(id: physical_type_id); |
4504 | |
4505 | if (is_matrix(type)) |
4506 | { |
4507 | const char *packed_pfx = lhs_packed_type ? "packed_" : "" ; |
4508 | |
4509 | // Packed matrices are stored as arrays of packed vectors, so we need |
4510 | // to assign the vectors one at a time. |
4511 | // For row-major matrices, we need to transpose the *right-hand* side, |
4512 | // not the left-hand side. |
4513 | |
4514 | // Lots of cases to cover here ... |
4515 | |
4516 | bool rhs_transpose = rhs_e && rhs_e->need_transpose; |
4517 | SPIRType write_type = type; |
4518 | string cast_expr; |
4519 | |
4520 | // We're dealing with transpose manually. |
4521 | if (rhs_transpose) |
4522 | rhs_e->need_transpose = false; |
4523 | |
4524 | if (transpose) |
4525 | { |
4526 | // We're dealing with transpose manually. |
4527 | lhs_e->need_transpose = false; |
4528 | write_type.vecsize = type.columns; |
4529 | write_type.columns = 1; |
4530 | |
4531 | if (physical_type.columns != type.columns) |
4532 | cast_expr = join(ts: "(device " , ts&: packed_pfx, ts: type_to_glsl(type: write_type), ts: "&)" ); |
4533 | |
4534 | if (rhs_transpose) |
4535 | { |
4536 | // If RHS is also transposed, we can just copy row by row. |
4537 | for (uint32_t i = 0; i < type.vecsize; i++) |
4538 | { |
4539 | statement(ts&: cast_expr, ts: to_enclosed_expression(id: lhs_expression), ts: "[" , ts&: i, ts: "]" , ts: " = " , |
4540 | ts: to_unpacked_row_major_matrix_expression(id: rhs_expression), ts: "[" , ts&: i, ts: "];" ); |
4541 | } |
4542 | } |
4543 | else |
4544 | { |
4545 | auto vector_type = expression_type(id: rhs_expression); |
4546 | vector_type.vecsize = vector_type.columns; |
4547 | vector_type.columns = 1; |
4548 | |
4549 | // Transpose on the fly. Emitting a lot of full transpose() ops and extracting lanes seems very bad, |
4550 | // so pick out individual components instead. |
4551 | for (uint32_t i = 0; i < type.vecsize; i++) |
4552 | { |
4553 | string rhs_row = type_to_glsl_constructor(type: vector_type) + "(" ; |
4554 | for (uint32_t j = 0; j < vector_type.vecsize; j++) |
4555 | { |
4556 | rhs_row += join(ts: to_enclosed_unpacked_expression(id: rhs_expression), ts: "[" , ts&: j, ts: "][" , ts&: i, ts: "]" ); |
4557 | if (j + 1 < vector_type.vecsize) |
4558 | rhs_row += ", " ; |
4559 | } |
4560 | rhs_row += ")" ; |
4561 | |
4562 | statement(ts&: cast_expr, ts: to_enclosed_expression(id: lhs_expression), ts: "[" , ts&: i, ts: "]" , ts: " = " , ts&: rhs_row, ts: ";" ); |
4563 | } |
4564 | } |
4565 | |
4566 | // We're dealing with transpose manually. |
4567 | lhs_e->need_transpose = true; |
4568 | } |
4569 | else |
4570 | { |
4571 | write_type.columns = 1; |
4572 | |
4573 | if (physical_type.vecsize != type.vecsize) |
4574 | cast_expr = join(ts: "(device " , ts&: packed_pfx, ts: type_to_glsl(type: write_type), ts: "&)" ); |
4575 | |
4576 | if (rhs_transpose) |
4577 | { |
4578 | auto vector_type = expression_type(id: rhs_expression); |
4579 | vector_type.columns = 1; |
4580 | |
4581 | // Transpose on the fly. Emitting a lot of full transpose() ops and extracting lanes seems very bad, |
4582 | // so pick out individual components instead. |
4583 | for (uint32_t i = 0; i < type.columns; i++) |
4584 | { |
4585 | string rhs_row = type_to_glsl_constructor(type: vector_type) + "(" ; |
4586 | for (uint32_t j = 0; j < vector_type.vecsize; j++) |
4587 | { |
4588 | // Need to explicitly unpack expression since we've mucked with transpose state. |
4589 | auto unpacked_expr = to_unpacked_row_major_matrix_expression(id: rhs_expression); |
4590 | rhs_row += join(ts&: unpacked_expr, ts: "[" , ts&: j, ts: "][" , ts&: i, ts: "]" ); |
4591 | if (j + 1 < vector_type.vecsize) |
4592 | rhs_row += ", " ; |
4593 | } |
4594 | rhs_row += ")" ; |
4595 | |
4596 | statement(ts&: cast_expr, ts: to_enclosed_expression(id: lhs_expression), ts: "[" , ts&: i, ts: "]" , ts: " = " , ts&: rhs_row, ts: ";" ); |
4597 | } |
4598 | } |
4599 | else |
4600 | { |
4601 | // Copy column-by-column. |
4602 | for (uint32_t i = 0; i < type.columns; i++) |
4603 | { |
4604 | statement(ts&: cast_expr, ts: to_enclosed_expression(id: lhs_expression), ts: "[" , ts&: i, ts: "]" , ts: " = " , |
4605 | ts: to_enclosed_unpacked_expression(id: rhs_expression), ts: "[" , ts&: i, ts: "];" ); |
4606 | } |
4607 | } |
4608 | } |
4609 | |
4610 | // We're dealing with transpose manually. |
4611 | if (rhs_transpose) |
4612 | rhs_e->need_transpose = true; |
4613 | } |
4614 | else if (transpose) |
4615 | { |
4616 | lhs_e->need_transpose = false; |
4617 | |
4618 | SPIRType write_type = type; |
4619 | write_type.vecsize = 1; |
4620 | write_type.columns = 1; |
4621 | |
4622 | // Storing a column to a row-major matrix. Unroll the write. |
4623 | for (uint32_t c = 0; c < type.vecsize; c++) |
4624 | { |
4625 | auto lhs_expr = to_enclosed_expression(id: lhs_expression); |
4626 | auto column_index = lhs_expr.find_last_of(c: '['); |
4627 | if (column_index != string::npos) |
4628 | { |
4629 | statement(ts: "((device " , ts: type_to_glsl(type: write_type), ts: "*)&" , |
4630 | ts&: lhs_expr.insert(pos1: column_index, str: join(ts: '[', ts&: c, ts: ']', ts: ")" )), ts: " = " , |
4631 | ts: to_extract_component_expression(id: rhs_expression, index: c), ts: ";" ); |
4632 | } |
4633 | } |
4634 | |
4635 | lhs_e->need_transpose = true; |
4636 | } |
4637 | else if ((is_matrix(type: physical_type) || is_array(type: physical_type)) && physical_type.vecsize > type.vecsize) |
4638 | { |
4639 | assert(type.vecsize >= 1 && type.vecsize <= 3); |
4640 | |
4641 | // If we have packed types, we cannot use swizzled stores. |
4642 | // We could technically unroll the store for each element if needed. |
4643 | // When remapping to a std140 physical type, we always get float4, |
4644 | // and the packed decoration should always be removed. |
4645 | assert(!lhs_packed_type); |
4646 | |
4647 | string lhs = to_dereferenced_expression(id: lhs_expression); |
4648 | string rhs = to_pointer_expression(id: rhs_expression); |
4649 | |
4650 | // Unpack the expression so we can store to it with a float or float2. |
4651 | // It's still an l-value, so it's fine. Most other unpacking of expressions turn them into r-values instead. |
4652 | lhs = join(ts: "(device " , ts: type_to_glsl(type), ts: "&)" , ts: enclose_expression(expr: lhs)); |
4653 | if (!optimize_read_modify_write(type: expression_type(id: rhs_expression), lhs, rhs)) |
4654 | statement(ts&: lhs, ts: " = " , ts&: rhs, ts: ";" ); |
4655 | } |
4656 | else if (!is_matrix(type)) |
4657 | { |
4658 | string lhs = to_dereferenced_expression(id: lhs_expression); |
4659 | string rhs = to_pointer_expression(id: rhs_expression); |
4660 | if (!optimize_read_modify_write(type: expression_type(id: rhs_expression), lhs, rhs)) |
4661 | statement(ts&: lhs, ts: " = " , ts&: rhs, ts: ";" ); |
4662 | } |
4663 | |
4664 | register_write(chain: lhs_expression); |
4665 | } |
4666 | } |
4667 | |
4668 | static bool expression_ends_with(const string &expr_str, const std::string &ending) |
4669 | { |
4670 | if (expr_str.length() >= ending.length()) |
4671 | return (expr_str.compare(pos: expr_str.length() - ending.length(), n: ending.length(), str: ending) == 0); |
4672 | else |
4673 | return false; |
4674 | } |
4675 | |
4676 | // Converts the format of the current expression from packed to unpacked, |
4677 | // by wrapping the expression in a constructor of the appropriate type. |
4678 | // Also, handle special physical ID remapping scenarios, similar to emit_store_statement(). |
4679 | string CompilerMSL::unpack_expression_type(string expr_str, const SPIRType &type, uint32_t physical_type_id, |
4680 | bool packed, bool row_major) |
4681 | { |
4682 | // Trivial case, nothing to do. |
4683 | if (physical_type_id == 0 && !packed) |
4684 | return expr_str; |
4685 | |
4686 | const SPIRType *physical_type = nullptr; |
4687 | if (physical_type_id) |
4688 | physical_type = &get<SPIRType>(id: physical_type_id); |
4689 | |
4690 | static const char *swizzle_lut[] = { |
4691 | ".x" , |
4692 | ".xy" , |
4693 | ".xyz" , |
4694 | }; |
4695 | |
4696 | if (physical_type && is_vector(type: *physical_type) && is_array(type: *physical_type) && |
4697 | physical_type->vecsize > type.vecsize && !expression_ends_with(expr_str, ending: swizzle_lut[type.vecsize - 1])) |
4698 | { |
4699 | // std140 array cases for vectors. |
4700 | assert(type.vecsize >= 1 && type.vecsize <= 3); |
4701 | return enclose_expression(expr: expr_str) + swizzle_lut[type.vecsize - 1]; |
4702 | } |
4703 | else if (physical_type && is_matrix(type: *physical_type) && is_vector(type) && physical_type->vecsize > type.vecsize) |
4704 | { |
4705 | // Extract column from padded matrix. |
4706 | assert(type.vecsize >= 1 && type.vecsize <= 3); |
4707 | return enclose_expression(expr: expr_str) + swizzle_lut[type.vecsize - 1]; |
4708 | } |
4709 | else if (is_matrix(type)) |
4710 | { |
4711 | // Packed matrices are stored as arrays of packed vectors. Unfortunately, |
4712 | // we can't just pass the array straight to the matrix constructor. We have to |
4713 | // pass each vector individually, so that they can be unpacked to normal vectors. |
4714 | if (!physical_type) |
4715 | physical_type = &type; |
4716 | |
4717 | uint32_t vecsize = type.vecsize; |
4718 | uint32_t columns = type.columns; |
4719 | if (row_major) |
4720 | swap(a&: vecsize, b&: columns); |
4721 | |
4722 | uint32_t physical_vecsize = row_major ? physical_type->columns : physical_type->vecsize; |
4723 | |
4724 | const char *base_type = type.width == 16 ? "half" : "float" ; |
4725 | string unpack_expr = join(ts&: base_type, ts&: columns, ts: "x" , ts&: vecsize, ts: "(" ); |
4726 | |
4727 | const char *load_swiz = "" ; |
4728 | |
4729 | if (physical_vecsize != vecsize) |
4730 | load_swiz = swizzle_lut[vecsize - 1]; |
4731 | |
4732 | for (uint32_t i = 0; i < columns; i++) |
4733 | { |
4734 | if (i > 0) |
4735 | unpack_expr += ", " ; |
4736 | |
4737 | if (packed) |
4738 | unpack_expr += join(ts&: base_type, ts&: physical_vecsize, ts: "(" , ts&: expr_str, ts: "[" , ts&: i, ts: "]" , ts: ")" , ts&: load_swiz); |
4739 | else |
4740 | unpack_expr += join(ts&: expr_str, ts: "[" , ts&: i, ts: "]" , ts&: load_swiz); |
4741 | } |
4742 | |
4743 | unpack_expr += ")" ; |
4744 | return unpack_expr; |
4745 | } |
4746 | else |
4747 | { |
4748 | return join(ts: type_to_glsl(type), ts: "(" , ts&: expr_str, ts: ")" ); |
4749 | } |
4750 | } |
4751 | |
4752 | // Emits the file header info |
4753 | void CompilerMSL::() |
4754 | { |
4755 | // This particular line can be overridden during compilation, so make it a flag and not a pragma line. |
4756 | if (suppress_missing_prototypes) |
4757 | statement(ts: "#pragma clang diagnostic ignored \"-Wmissing-prototypes\"" ); |
4758 | |
4759 | // Disable warning about missing braces for array<T> template to make arrays a value type |
4760 | if (spv_function_implementations.count(x: SPVFuncImplUnsafeArray) != 0) |
4761 | statement(ts: "#pragma clang diagnostic ignored \"-Wmissing-braces\"" ); |
4762 | |
4763 | for (auto &pragma : pragma_lines) |
4764 | statement(ts: pragma); |
4765 | |
4766 | if (!pragma_lines.empty() || suppress_missing_prototypes) |
4767 | statement(ts: "" ); |
4768 | |
4769 | statement(ts: "#include <metal_stdlib>" ); |
4770 | statement(ts: "#include <simd/simd.h>" ); |
4771 | |
4772 | for (auto & : header_lines) |
4773 | statement(ts&: header); |
4774 | |
4775 | statement(ts: "" ); |
4776 | statement(ts: "using namespace metal;" ); |
4777 | statement(ts: "" ); |
4778 | |
4779 | for (auto &td : typedef_lines) |
4780 | statement(ts: td); |
4781 | |
4782 | if (!typedef_lines.empty()) |
4783 | statement(ts: "" ); |
4784 | } |
4785 | |
4786 | void CompilerMSL::add_pragma_line(const string &line) |
4787 | { |
4788 | auto rslt = pragma_lines.insert(x: line); |
4789 | if (rslt.second) |
4790 | force_recompile(); |
4791 | } |
4792 | |
4793 | void CompilerMSL::add_typedef_line(const string &line) |
4794 | { |
4795 | auto rslt = typedef_lines.insert(x: line); |
4796 | if (rslt.second) |
4797 | force_recompile(); |
4798 | } |
4799 | |
4800 | // Template struct like spvUnsafeArray<> need to be declared *before* any resources are declared |
4801 | void CompilerMSL::emit_custom_templates() |
4802 | { |
4803 | for (const auto &spv_func : spv_function_implementations) |
4804 | { |
4805 | switch (spv_func) |
4806 | { |
4807 | case SPVFuncImplUnsafeArray: |
4808 | statement(ts: "template<typename T, size_t Num>" ); |
4809 | statement(ts: "struct spvUnsafeArray" ); |
4810 | begin_scope(); |
4811 | statement(ts: "T elements[Num ? Num : 1];" ); |
4812 | statement(ts: "" ); |
4813 | statement(ts: "thread T& operator [] (size_t pos) thread" ); |
4814 | begin_scope(); |
4815 | statement(ts: "return elements[pos];" ); |
4816 | end_scope(); |
4817 | statement(ts: "constexpr const thread T& operator [] (size_t pos) const thread" ); |
4818 | begin_scope(); |
4819 | statement(ts: "return elements[pos];" ); |
4820 | end_scope(); |
4821 | statement(ts: "" ); |
4822 | statement(ts: "device T& operator [] (size_t pos) device" ); |
4823 | begin_scope(); |
4824 | statement(ts: "return elements[pos];" ); |
4825 | end_scope(); |
4826 | statement(ts: "constexpr const device T& operator [] (size_t pos) const device" ); |
4827 | begin_scope(); |
4828 | statement(ts: "return elements[pos];" ); |
4829 | end_scope(); |
4830 | statement(ts: "" ); |
4831 | statement(ts: "constexpr const constant T& operator [] (size_t pos) const constant" ); |
4832 | begin_scope(); |
4833 | statement(ts: "return elements[pos];" ); |
4834 | end_scope(); |
4835 | statement(ts: "" ); |
4836 | statement(ts: "threadgroup T& operator [] (size_t pos) threadgroup" ); |
4837 | begin_scope(); |
4838 | statement(ts: "return elements[pos];" ); |
4839 | end_scope(); |
4840 | statement(ts: "constexpr const threadgroup T& operator [] (size_t pos) const threadgroup" ); |
4841 | begin_scope(); |
4842 | statement(ts: "return elements[pos];" ); |
4843 | end_scope(); |
4844 | end_scope_decl(); |
4845 | statement(ts: "" ); |
4846 | break; |
4847 | |
4848 | default: |
4849 | break; |
4850 | } |
4851 | } |
4852 | } |
4853 | |
4854 | // Emits any needed custom function bodies. |
4855 | // Metal helper functions must be static force-inline, i.e. static inline __attribute__((always_inline)) |
4856 | // otherwise they will cause problems when linked together in a single Metallib. |
4857 | void CompilerMSL::emit_custom_functions() |
4858 | { |
4859 | for (uint32_t i = kArrayCopyMultidimMax; i >= 2; i--) |
4860 | if (spv_function_implementations.count(x: static_cast<SPVFuncImpl>(SPVFuncImplArrayCopyMultidimBase + i))) |
4861 | spv_function_implementations.insert(x: static_cast<SPVFuncImpl>(SPVFuncImplArrayCopyMultidimBase + i - 1)); |
4862 | |
4863 | if (spv_function_implementations.count(x: SPVFuncImplDynamicImageSampler)) |
4864 | { |
4865 | // Unfortunately, this one needs a lot of the other functions to compile OK. |
4866 | if (!msl_options.supports_msl_version(major: 2)) |
4867 | SPIRV_CROSS_THROW( |
4868 | "spvDynamicImageSampler requires default-constructible texture objects, which require MSL 2.0." ); |
4869 | spv_function_implementations.insert(x: SPVFuncImplForwardArgs); |
4870 | spv_function_implementations.insert(x: SPVFuncImplTextureSwizzle); |
4871 | if (msl_options.swizzle_texture_samples) |
4872 | spv_function_implementations.insert(x: SPVFuncImplGatherSwizzle); |
4873 | for (uint32_t i = SPVFuncImplChromaReconstructNearest2Plane; |
4874 | i <= SPVFuncImplChromaReconstructLinear420XMidpointYMidpoint3Plane; i++) |
4875 | spv_function_implementations.insert(x: static_cast<SPVFuncImpl>(i)); |
4876 | spv_function_implementations.insert(x: SPVFuncImplExpandITUFullRange); |
4877 | spv_function_implementations.insert(x: SPVFuncImplExpandITUNarrowRange); |
4878 | spv_function_implementations.insert(x: SPVFuncImplConvertYCbCrBT709); |
4879 | spv_function_implementations.insert(x: SPVFuncImplConvertYCbCrBT601); |
4880 | spv_function_implementations.insert(x: SPVFuncImplConvertYCbCrBT2020); |
4881 | } |
4882 | |
4883 | for (uint32_t i = SPVFuncImplChromaReconstructNearest2Plane; |
4884 | i <= SPVFuncImplChromaReconstructLinear420XMidpointYMidpoint3Plane; i++) |
4885 | if (spv_function_implementations.count(x: static_cast<SPVFuncImpl>(i))) |
4886 | spv_function_implementations.insert(x: SPVFuncImplForwardArgs); |
4887 | |
4888 | if (spv_function_implementations.count(x: SPVFuncImplTextureSwizzle) || |
4889 | spv_function_implementations.count(x: SPVFuncImplGatherSwizzle) || |
4890 | spv_function_implementations.count(x: SPVFuncImplGatherCompareSwizzle)) |
4891 | { |
4892 | spv_function_implementations.insert(x: SPVFuncImplForwardArgs); |
4893 | spv_function_implementations.insert(x: SPVFuncImplGetSwizzle); |
4894 | } |
4895 | |
4896 | for (const auto &spv_func : spv_function_implementations) |
4897 | { |
4898 | switch (spv_func) |
4899 | { |
4900 | case SPVFuncImplMod: |
4901 | statement(ts: "// Implementation of the GLSL mod() function, which is slightly different than Metal fmod()" ); |
4902 | statement(ts: "template<typename Tx, typename Ty>" ); |
4903 | statement(ts: "inline Tx mod(Tx x, Ty y)" ); |
4904 | begin_scope(); |
4905 | statement(ts: "return x - y * floor(x / y);" ); |
4906 | end_scope(); |
4907 | statement(ts: "" ); |
4908 | break; |
4909 | |
4910 | case SPVFuncImplRadians: |
4911 | statement(ts: "// Implementation of the GLSL radians() function" ); |
4912 | statement(ts: "template<typename T>" ); |
4913 | statement(ts: "inline T radians(T d)" ); |
4914 | begin_scope(); |
4915 | statement(ts: "return d * T(0.01745329251);" ); |
4916 | end_scope(); |
4917 | statement(ts: "" ); |
4918 | break; |
4919 | |
4920 | case SPVFuncImplDegrees: |
4921 | statement(ts: "// Implementation of the GLSL degrees() function" ); |
4922 | statement(ts: "template<typename T>" ); |
4923 | statement(ts: "inline T degrees(T r)" ); |
4924 | begin_scope(); |
4925 | statement(ts: "return r * T(57.2957795131);" ); |
4926 | end_scope(); |
4927 | statement(ts: "" ); |
4928 | break; |
4929 | |
4930 | case SPVFuncImplFindILsb: |
4931 | statement(ts: "// Implementation of the GLSL findLSB() function" ); |
4932 | statement(ts: "template<typename T>" ); |
4933 | statement(ts: "inline T spvFindLSB(T x)" ); |
4934 | begin_scope(); |
4935 | statement(ts: "return select(ctz(x), T(-1), x == T(0));" ); |
4936 | end_scope(); |
4937 | statement(ts: "" ); |
4938 | break; |
4939 | |
4940 | case SPVFuncImplFindUMsb: |
4941 | statement(ts: "// Implementation of the unsigned GLSL findMSB() function" ); |
4942 | statement(ts: "template<typename T>" ); |
4943 | statement(ts: "inline T spvFindUMSB(T x)" ); |
4944 | begin_scope(); |
4945 | statement(ts: "return select(clz(T(0)) - (clz(x) + T(1)), T(-1), x == T(0));" ); |
4946 | end_scope(); |
4947 | statement(ts: "" ); |
4948 | break; |
4949 | |
4950 | case SPVFuncImplFindSMsb: |
4951 | statement(ts: "// Implementation of the signed GLSL findMSB() function" ); |
4952 | statement(ts: "template<typename T>" ); |
4953 | statement(ts: "inline T spvFindSMSB(T x)" ); |
4954 | begin_scope(); |
4955 | statement(ts: "T v = select(x, T(-1) - x, x < T(0));" ); |
4956 | statement(ts: "return select(clz(T(0)) - (clz(v) + T(1)), T(-1), v == T(0));" ); |
4957 | end_scope(); |
4958 | statement(ts: "" ); |
4959 | break; |
4960 | |
4961 | case SPVFuncImplSSign: |
4962 | statement(ts: "// Implementation of the GLSL sign() function for integer types" ); |
4963 | statement(ts: "template<typename T, typename E = typename enable_if<is_integral<T>::value>::type>" ); |
4964 | statement(ts: "inline T sign(T x)" ); |
4965 | begin_scope(); |
4966 | statement(ts: "return select(select(select(x, T(0), x == T(0)), T(1), x > T(0)), T(-1), x < T(0));" ); |
4967 | end_scope(); |
4968 | statement(ts: "" ); |
4969 | break; |
4970 | |
4971 | case SPVFuncImplArrayCopy: |
4972 | case SPVFuncImplArrayOfArrayCopy2Dim: |
4973 | case SPVFuncImplArrayOfArrayCopy3Dim: |
4974 | case SPVFuncImplArrayOfArrayCopy4Dim: |
4975 | case SPVFuncImplArrayOfArrayCopy5Dim: |
4976 | case SPVFuncImplArrayOfArrayCopy6Dim: |
4977 | { |
4978 | // Unfortunately we cannot template on the address space, so combinatorial explosion it is. |
4979 | static const char *function_name_tags[] = { |
4980 | "FromConstantToStack" , "FromConstantToThreadGroup" , "FromStackToStack" , |
4981 | "FromStackToThreadGroup" , "FromThreadGroupToStack" , "FromThreadGroupToThreadGroup" , |
4982 | "FromDeviceToDevice" , "FromConstantToDevice" , "FromStackToDevice" , |
4983 | "FromThreadGroupToDevice" , "FromDeviceToStack" , "FromDeviceToThreadGroup" , |
4984 | }; |
4985 | |
4986 | static const char *src_address_space[] = { |
4987 | "constant" , "constant" , "thread const" , "thread const" , |
4988 | "threadgroup const" , "threadgroup const" , "device const" , "constant" , |
4989 | "thread const" , "threadgroup const" , "device const" , "device const" , |
4990 | }; |
4991 | |
4992 | static const char *dst_address_space[] = { |
4993 | "thread" , "threadgroup" , "thread" , "threadgroup" , "thread" , "threadgroup" , |
4994 | "device" , "device" , "device" , "device" , "thread" , "threadgroup" , |
4995 | }; |
4996 | |
4997 | for (uint32_t variant = 0; variant < 12; variant++) |
4998 | { |
4999 | uint8_t dimensions = spv_func - SPVFuncImplArrayCopyMultidimBase; |
5000 | string tmp = "template<typename T" ; |
5001 | for (uint8_t i = 0; i < dimensions; i++) |
5002 | { |
5003 | tmp += ", uint " ; |
5004 | tmp += 'A' + i; |
5005 | } |
5006 | tmp += ">" ; |
5007 | statement(ts&: tmp); |
5008 | |
5009 | string array_arg; |
5010 | for (uint8_t i = 0; i < dimensions; i++) |
5011 | { |
5012 | array_arg += "[" ; |
5013 | array_arg += 'A' + i; |
5014 | array_arg += "]" ; |
5015 | } |
5016 | |
5017 | statement(ts: "inline void spvArrayCopy" , ts&: function_name_tags[variant], ts&: dimensions, ts: "(" , |
5018 | ts&: dst_address_space[variant], ts: " T (&dst)" , ts&: array_arg, ts: ", " , ts&: src_address_space[variant], |
5019 | ts: " T (&src)" , ts&: array_arg, ts: ")" ); |
5020 | |
5021 | begin_scope(); |
5022 | statement(ts: "for (uint i = 0; i < A; i++)" ); |
5023 | begin_scope(); |
5024 | |
5025 | if (dimensions == 1) |
5026 | statement(ts: "dst[i] = src[i];" ); |
5027 | else |
5028 | statement(ts: "spvArrayCopy" , ts&: function_name_tags[variant], ts: dimensions - 1, ts: "(dst[i], src[i]);" ); |
5029 | end_scope(); |
5030 | end_scope(); |
5031 | statement(ts: "" ); |
5032 | } |
5033 | break; |
5034 | } |
5035 | |
5036 | // Support for Metal 2.1's new texture_buffer type. |
5037 | case SPVFuncImplTexelBufferCoords: |
5038 | { |
5039 | if (msl_options.texel_buffer_texture_width > 0) |
5040 | { |
5041 | string tex_width_str = convert_to_string(t: msl_options.texel_buffer_texture_width); |
5042 | statement(ts: "// Returns 2D texture coords corresponding to 1D texel buffer coords" ); |
5043 | statement(ts&: force_inline); |
5044 | statement(ts: "uint2 spvTexelBufferCoord(uint tc)" ); |
5045 | begin_scope(); |
5046 | statement(ts: join(ts: "return uint2(tc % " , ts&: tex_width_str, ts: ", tc / " , ts&: tex_width_str, ts: ");" )); |
5047 | end_scope(); |
5048 | statement(ts: "" ); |
5049 | } |
5050 | else |
5051 | { |
5052 | statement(ts: "// Returns 2D texture coords corresponding to 1D texel buffer coords" ); |
5053 | statement( |
5054 | ts: "#define spvTexelBufferCoord(tc, tex) uint2((tc) % (tex).get_width(), (tc) / (tex).get_width())" ); |
5055 | statement(ts: "" ); |
5056 | } |
5057 | break; |
5058 | } |
5059 | |
5060 | // Emulate texture2D atomic operations |
5061 | case SPVFuncImplImage2DAtomicCoords: |
5062 | { |
5063 | if (msl_options.supports_msl_version(major: 1, minor: 2)) |
5064 | { |
5065 | statement(ts: "// The required alignment of a linear texture of R32Uint format." ); |
5066 | statement(ts: "constant uint spvLinearTextureAlignmentOverride [[function_constant(" , |
5067 | ts&: msl_options.r32ui_alignment_constant_id, ts: ")]];" ); |
5068 | statement(ts: "constant uint spvLinearTextureAlignment = " , |
5069 | ts: "is_function_constant_defined(spvLinearTextureAlignmentOverride) ? " , |
5070 | ts: "spvLinearTextureAlignmentOverride : " , ts&: msl_options.r32ui_linear_texture_alignment, ts: ";" ); |
5071 | } |
5072 | else |
5073 | { |
5074 | statement(ts: "// The required alignment of a linear texture of R32Uint format." ); |
5075 | statement(ts: "constant uint spvLinearTextureAlignment = " , ts&: msl_options.r32ui_linear_texture_alignment, |
5076 | ts: ";" ); |
5077 | } |
5078 | statement(ts: "// Returns buffer coords corresponding to 2D texture coords for emulating 2D texture atomics" ); |
5079 | statement(ts: "#define spvImage2DAtomicCoord(tc, tex) (((((tex).get_width() + " , |
5080 | ts: " spvLinearTextureAlignment / 4 - 1) & ~(" , |
5081 | ts: " spvLinearTextureAlignment / 4 - 1)) * (tc).y) + (tc).x)" ); |
5082 | statement(ts: "" ); |
5083 | break; |
5084 | } |
5085 | |
5086 | // "fadd" intrinsic support |
5087 | case SPVFuncImplFAdd: |
5088 | statement(ts: "template<typename T>" ); |
5089 | statement(ts: "[[clang::optnone]] T spvFAdd(T l, T r)" ); |
5090 | begin_scope(); |
5091 | statement(ts: "return fma(T(1), l, r);" ); |
5092 | end_scope(); |
5093 | statement(ts: "" ); |
5094 | break; |
5095 | |
5096 | // "fsub" intrinsic support |
5097 | case SPVFuncImplFSub: |
5098 | statement(ts: "template<typename T>" ); |
5099 | statement(ts: "[[clang::optnone]] T spvFSub(T l, T r)" ); |
5100 | begin_scope(); |
5101 | statement(ts: "return fma(T(-1), r, l);" ); |
5102 | end_scope(); |
5103 | statement(ts: "" ); |
5104 | break; |
5105 | |
5106 | // "fmul' intrinsic support |
5107 | case SPVFuncImplFMul: |
5108 | statement(ts: "template<typename T>" ); |
5109 | statement(ts: "[[clang::optnone]] T spvFMul(T l, T r)" ); |
5110 | begin_scope(); |
5111 | statement(ts: "return fma(l, r, T(0));" ); |
5112 | end_scope(); |
5113 | statement(ts: "" ); |
5114 | |
5115 | statement(ts: "template<typename T, int Cols, int Rows>" ); |
5116 | statement(ts: "[[clang::optnone]] vec<T, Cols> spvFMulVectorMatrix(vec<T, Rows> v, matrix<T, Cols, Rows> m)" ); |
5117 | begin_scope(); |
5118 | statement(ts: "vec<T, Cols> res = vec<T, Cols>(0);" ); |
5119 | statement(ts: "for (uint i = Rows; i > 0; --i)" ); |
5120 | begin_scope(); |
5121 | statement(ts: "vec<T, Cols> tmp(0);" ); |
5122 | statement(ts: "for (uint j = 0; j < Cols; ++j)" ); |
5123 | begin_scope(); |
5124 | statement(ts: "tmp[j] = m[j][i - 1];" ); |
5125 | end_scope(); |
5126 | statement(ts: "res = fma(tmp, vec<T, Cols>(v[i - 1]), res);" ); |
5127 | end_scope(); |
5128 | statement(ts: "return res;" ); |
5129 | end_scope(); |
5130 | statement(ts: "" ); |
5131 | |
5132 | statement(ts: "template<typename T, int Cols, int Rows>" ); |
5133 | statement(ts: "[[clang::optnone]] vec<T, Rows> spvFMulMatrixVector(matrix<T, Cols, Rows> m, vec<T, Cols> v)" ); |
5134 | begin_scope(); |
5135 | statement(ts: "vec<T, Rows> res = vec<T, Rows>(0);" ); |
5136 | statement(ts: "for (uint i = Cols; i > 0; --i)" ); |
5137 | begin_scope(); |
5138 | statement(ts: "res = fma(m[i - 1], vec<T, Rows>(v[i - 1]), res);" ); |
5139 | end_scope(); |
5140 | statement(ts: "return res;" ); |
5141 | end_scope(); |
5142 | statement(ts: "" ); |
5143 | |
5144 | statement(ts: "template<typename T, int LCols, int LRows, int RCols, int RRows>" ); |
5145 | statement(ts: "[[clang::optnone]] matrix<T, RCols, LRows> spvFMulMatrixMatrix(matrix<T, LCols, LRows> l, matrix<T, RCols, RRows> r)" ); |
5146 | begin_scope(); |
5147 | statement(ts: "matrix<T, RCols, LRows> res;" ); |
5148 | statement(ts: "for (uint i = 0; i < RCols; i++)" ); |
5149 | begin_scope(); |
5150 | statement(ts: "vec<T, RCols> tmp(0);" ); |
5151 | statement(ts: "for (uint j = 0; j < LCols; j++)" ); |
5152 | begin_scope(); |
5153 | statement(ts: "tmp = fma(vec<T, RCols>(r[i][j]), l[j], tmp);" ); |
5154 | end_scope(); |
5155 | statement(ts: "res[i] = tmp;" ); |
5156 | end_scope(); |
5157 | statement(ts: "return res;" ); |
5158 | end_scope(); |
5159 | statement(ts: "" ); |
5160 | break; |
5161 | |
5162 | case SPVFuncImplQuantizeToF16: |
5163 | // Ensure fast-math is disabled to match Vulkan results. |
5164 | // SpvHalfTypeSelector is used to match the half* template type to the float* template type. |
5165 | // Depending on GPU, MSL does not always flush converted subnormal halfs to zero, |
5166 | // as required by OpQuantizeToF16, so check for subnormals and flush them to zero. |
5167 | statement(ts: "template <typename F> struct SpvHalfTypeSelector;" ); |
5168 | statement(ts: "template <> struct SpvHalfTypeSelector<float> { public: using H = half; };" ); |
5169 | statement(ts: "template<uint N> struct SpvHalfTypeSelector<vec<float, N>> { using H = vec<half, N>; };" ); |
5170 | statement(ts: "template<typename F, typename H = typename SpvHalfTypeSelector<F>::H>" ); |
5171 | statement(ts: "[[clang::optnone]] F spvQuantizeToF16(F fval)" ); |
5172 | begin_scope(); |
5173 | statement(ts: "H hval = H(fval);" ); |
5174 | statement(ts: "hval = select(copysign(H(0), hval), hval, isnormal(hval) || isinf(hval) || isnan(hval));" ); |
5175 | statement(ts: "return F(hval);" ); |
5176 | end_scope(); |
5177 | statement(ts: "" ); |
5178 | break; |
5179 | |
5180 | // Emulate texturecube_array with texture2d_array for iOS where this type is not available |
5181 | case SPVFuncImplCubemapTo2DArrayFace: |
5182 | statement(ts&: force_inline); |
5183 | statement(ts: "float3 spvCubemapTo2DArrayFace(float3 P)" ); |
5184 | begin_scope(); |
5185 | statement(ts: "float3 Coords = abs(P.xyz);" ); |
5186 | statement(ts: "float CubeFace = 0;" ); |
5187 | statement(ts: "float ProjectionAxis = 0;" ); |
5188 | statement(ts: "float u = 0;" ); |
5189 | statement(ts: "float v = 0;" ); |
5190 | statement(ts: "if (Coords.x >= Coords.y && Coords.x >= Coords.z)" ); |
5191 | begin_scope(); |
5192 | statement(ts: "CubeFace = P.x >= 0 ? 0 : 1;" ); |
5193 | statement(ts: "ProjectionAxis = Coords.x;" ); |
5194 | statement(ts: "u = P.x >= 0 ? -P.z : P.z;" ); |
5195 | statement(ts: "v = -P.y;" ); |
5196 | end_scope(); |
5197 | statement(ts: "else if (Coords.y >= Coords.x && Coords.y >= Coords.z)" ); |
5198 | begin_scope(); |
5199 | statement(ts: "CubeFace = P.y >= 0 ? 2 : 3;" ); |
5200 | statement(ts: "ProjectionAxis = Coords.y;" ); |
5201 | statement(ts: "u = P.x;" ); |
5202 | statement(ts: "v = P.y >= 0 ? P.z : -P.z;" ); |
5203 | end_scope(); |
5204 | statement(ts: "else" ); |
5205 | begin_scope(); |
5206 | statement(ts: "CubeFace = P.z >= 0 ? 4 : 5;" ); |
5207 | statement(ts: "ProjectionAxis = Coords.z;" ); |
5208 | statement(ts: "u = P.z >= 0 ? P.x : -P.x;" ); |
5209 | statement(ts: "v = -P.y;" ); |
5210 | end_scope(); |
5211 | statement(ts: "u = 0.5 * (u/ProjectionAxis + 1);" ); |
5212 | statement(ts: "v = 0.5 * (v/ProjectionAxis + 1);" ); |
5213 | statement(ts: "return float3(u, v, CubeFace);" ); |
5214 | end_scope(); |
5215 | statement(ts: "" ); |
5216 | break; |
5217 | |
5218 | case SPVFuncImplInverse4x4: |
5219 | statement(ts: "// Returns the determinant of a 2x2 matrix." ); |
5220 | statement(ts&: force_inline); |
5221 | statement(ts: "float spvDet2x2(float a1, float a2, float b1, float b2)" ); |
5222 | begin_scope(); |
5223 | statement(ts: "return a1 * b2 - b1 * a2;" ); |
5224 | end_scope(); |
5225 | statement(ts: "" ); |
5226 | |
5227 | statement(ts: "// Returns the determinant of a 3x3 matrix." ); |
5228 | statement(ts&: force_inline); |
5229 | statement(ts: "float spvDet3x3(float a1, float a2, float a3, float b1, float b2, float b3, float c1, " |
5230 | "float c2, float c3)" ); |
5231 | begin_scope(); |
5232 | statement(ts: "return a1 * spvDet2x2(b2, b3, c2, c3) - b1 * spvDet2x2(a2, a3, c2, c3) + c1 * spvDet2x2(a2, a3, " |
5233 | "b2, b3);" ); |
5234 | end_scope(); |
5235 | statement(ts: "" ); |
5236 | statement(ts: "// Returns the inverse of a matrix, by using the algorithm of calculating the classical" ); |
5237 | statement(ts: "// adjoint and dividing by the determinant. The contents of the matrix are changed." ); |
5238 | statement(ts&: force_inline); |
5239 | statement(ts: "float4x4 spvInverse4x4(float4x4 m)" ); |
5240 | begin_scope(); |
5241 | statement(ts: "float4x4 adj; // The adjoint matrix (inverse after dividing by determinant)" ); |
5242 | statement_no_indent(ts: "" ); |
5243 | statement(ts: "// Create the transpose of the cofactors, as the classical adjoint of the matrix." ); |
5244 | statement(ts: "adj[0][0] = spvDet3x3(m[1][1], m[1][2], m[1][3], m[2][1], m[2][2], m[2][3], m[3][1], m[3][2], " |
5245 | "m[3][3]);" ); |
5246 | statement(ts: "adj[0][1] = -spvDet3x3(m[0][1], m[0][2], m[0][3], m[2][1], m[2][2], m[2][3], m[3][1], m[3][2], " |
5247 | "m[3][3]);" ); |
5248 | statement(ts: "adj[0][2] = spvDet3x3(m[0][1], m[0][2], m[0][3], m[1][1], m[1][2], m[1][3], m[3][1], m[3][2], " |
5249 | "m[3][3]);" ); |
5250 | statement(ts: "adj[0][3] = -spvDet3x3(m[0][1], m[0][2], m[0][3], m[1][1], m[1][2], m[1][3], m[2][1], m[2][2], " |
5251 | "m[2][3]);" ); |
5252 | statement_no_indent(ts: "" ); |
5253 | statement(ts: "adj[1][0] = -spvDet3x3(m[1][0], m[1][2], m[1][3], m[2][0], m[2][2], m[2][3], m[3][0], m[3][2], " |
5254 | "m[3][3]);" ); |
5255 | statement(ts: "adj[1][1] = spvDet3x3(m[0][0], m[0][2], m[0][3], m[2][0], m[2][2], m[2][3], m[3][0], m[3][2], " |
5256 | "m[3][3]);" ); |
5257 | statement(ts: "adj[1][2] = -spvDet3x3(m[0][0], m[0][2], m[0][3], m[1][0], m[1][2], m[1][3], m[3][0], m[3][2], " |
5258 | "m[3][3]);" ); |
5259 | statement(ts: "adj[1][3] = spvDet3x3(m[0][0], m[0][2], m[0][3], m[1][0], m[1][2], m[1][3], m[2][0], m[2][2], " |
5260 | "m[2][3]);" ); |
5261 | statement_no_indent(ts: "" ); |
5262 | statement(ts: "adj[2][0] = spvDet3x3(m[1][0], m[1][1], m[1][3], m[2][0], m[2][1], m[2][3], m[3][0], m[3][1], " |
5263 | "m[3][3]);" ); |
5264 | statement(ts: "adj[2][1] = -spvDet3x3(m[0][0], m[0][1], m[0][3], m[2][0], m[2][1], m[2][3], m[3][0], m[3][1], " |
5265 | "m[3][3]);" ); |
5266 | statement(ts: "adj[2][2] = spvDet3x3(m[0][0], m[0][1], m[0][3], m[1][0], m[1][1], m[1][3], m[3][0], m[3][1], " |
5267 | "m[3][3]);" ); |
5268 | statement(ts: "adj[2][3] = -spvDet3x3(m[0][0], m[0][1], m[0][3], m[1][0], m[1][1], m[1][3], m[2][0], m[2][1], " |
5269 | "m[2][3]);" ); |
5270 | statement_no_indent(ts: "" ); |
5271 | statement(ts: "adj[3][0] = -spvDet3x3(m[1][0], m[1][1], m[1][2], m[2][0], m[2][1], m[2][2], m[3][0], m[3][1], " |
5272 | "m[3][2]);" ); |
5273 | statement(ts: "adj[3][1] = spvDet3x3(m[0][0], m[0][1], m[0][2], m[2][0], m[2][1], m[2][2], m[3][0], m[3][1], " |
5274 | "m[3][2]);" ); |
5275 | statement(ts: "adj[3][2] = -spvDet3x3(m[0][0], m[0][1], m[0][2], m[1][0], m[1][1], m[1][2], m[3][0], m[3][1], " |
5276 | "m[3][2]);" ); |
5277 | statement(ts: "adj[3][3] = spvDet3x3(m[0][0], m[0][1], m[0][2], m[1][0], m[1][1], m[1][2], m[2][0], m[2][1], " |
5278 | "m[2][2]);" ); |
5279 | statement_no_indent(ts: "" ); |
5280 | statement(ts: "// Calculate the determinant as a combination of the cofactors of the first row." ); |
5281 | statement(ts: "float det = (adj[0][0] * m[0][0]) + (adj[0][1] * m[1][0]) + (adj[0][2] * m[2][0]) + (adj[0][3] " |
5282 | "* m[3][0]);" ); |
5283 | statement_no_indent(ts: "" ); |
5284 | statement(ts: "// Divide the classical adjoint matrix by the determinant." ); |
5285 | statement(ts: "// If determinant is zero, matrix is not invertable, so leave it unchanged." ); |
5286 | statement(ts: "return (det != 0.0f) ? (adj * (1.0f / det)) : m;" ); |
5287 | end_scope(); |
5288 | statement(ts: "" ); |
5289 | break; |
5290 | |
5291 | case SPVFuncImplInverse3x3: |
5292 | if (spv_function_implementations.count(x: SPVFuncImplInverse4x4) == 0) |
5293 | { |
5294 | statement(ts: "// Returns the determinant of a 2x2 matrix." ); |
5295 | statement(ts&: force_inline); |
5296 | statement(ts: "float spvDet2x2(float a1, float a2, float b1, float b2)" ); |
5297 | begin_scope(); |
5298 | statement(ts: "return a1 * b2 - b1 * a2;" ); |
5299 | end_scope(); |
5300 | statement(ts: "" ); |
5301 | } |
5302 | |
5303 | statement(ts: "// Returns the inverse of a matrix, by using the algorithm of calculating the classical" ); |
5304 | statement(ts: "// adjoint and dividing by the determinant. The contents of the matrix are changed." ); |
5305 | statement(ts&: force_inline); |
5306 | statement(ts: "float3x3 spvInverse3x3(float3x3 m)" ); |
5307 | begin_scope(); |
5308 | statement(ts: "float3x3 adj; // The adjoint matrix (inverse after dividing by determinant)" ); |
5309 | statement_no_indent(ts: "" ); |
5310 | statement(ts: "// Create the transpose of the cofactors, as the classical adjoint of the matrix." ); |
5311 | statement(ts: "adj[0][0] = spvDet2x2(m[1][1], m[1][2], m[2][1], m[2][2]);" ); |
5312 | statement(ts: "adj[0][1] = -spvDet2x2(m[0][1], m[0][2], m[2][1], m[2][2]);" ); |
5313 | statement(ts: "adj[0][2] = spvDet2x2(m[0][1], m[0][2], m[1][1], m[1][2]);" ); |
5314 | statement_no_indent(ts: "" ); |
5315 | statement(ts: "adj[1][0] = -spvDet2x2(m[1][0], m[1][2], m[2][0], m[2][2]);" ); |
5316 | statement(ts: "adj[1][1] = spvDet2x2(m[0][0], m[0][2], m[2][0], m[2][2]);" ); |
5317 | statement(ts: "adj[1][2] = -spvDet2x2(m[0][0], m[0][2], m[1][0], m[1][2]);" ); |
5318 | statement_no_indent(ts: "" ); |
5319 | statement(ts: "adj[2][0] = spvDet2x2(m[1][0], m[1][1], m[2][0], m[2][1]);" ); |
5320 | statement(ts: "adj[2][1] = -spvDet2x2(m[0][0], m[0][1], m[2][0], m[2][1]);" ); |
5321 | statement(ts: "adj[2][2] = spvDet2x2(m[0][0], m[0][1], m[1][0], m[1][1]);" ); |
5322 | statement_no_indent(ts: "" ); |
5323 | statement(ts: "// Calculate the determinant as a combination of the cofactors of the first row." ); |
5324 | statement(ts: "float det = (adj[0][0] * m[0][0]) + (adj[0][1] * m[1][0]) + (adj[0][2] * m[2][0]);" ); |
5325 | statement_no_indent(ts: "" ); |
5326 | statement(ts: "// Divide the classical adjoint matrix by the determinant." ); |
5327 | statement(ts: "// If determinant is zero, matrix is not invertable, so leave it unchanged." ); |
5328 | statement(ts: "return (det != 0.0f) ? (adj * (1.0f / det)) : m;" ); |
5329 | end_scope(); |
5330 | statement(ts: "" ); |
5331 | break; |
5332 | |
5333 | case SPVFuncImplInverse2x2: |
5334 | statement(ts: "// Returns the inverse of a matrix, by using the algorithm of calculating the classical" ); |
5335 | statement(ts: "// adjoint and dividing by the determinant. The contents of the matrix are changed." ); |
5336 | statement(ts&: force_inline); |
5337 | statement(ts: "float2x2 spvInverse2x2(float2x2 m)" ); |
5338 | begin_scope(); |
5339 | statement(ts: "float2x2 adj; // The adjoint matrix (inverse after dividing by determinant)" ); |
5340 | statement_no_indent(ts: "" ); |
5341 | statement(ts: "// Create the transpose of the cofactors, as the classical adjoint of the matrix." ); |
5342 | statement(ts: "adj[0][0] = m[1][1];" ); |
5343 | statement(ts: "adj[0][1] = -m[0][1];" ); |
5344 | statement_no_indent(ts: "" ); |
5345 | statement(ts: "adj[1][0] = -m[1][0];" ); |
5346 | statement(ts: "adj[1][1] = m[0][0];" ); |
5347 | statement_no_indent(ts: "" ); |
5348 | statement(ts: "// Calculate the determinant as a combination of the cofactors of the first row." ); |
5349 | statement(ts: "float det = (adj[0][0] * m[0][0]) + (adj[0][1] * m[1][0]);" ); |
5350 | statement_no_indent(ts: "" ); |
5351 | statement(ts: "// Divide the classical adjoint matrix by the determinant." ); |
5352 | statement(ts: "// If determinant is zero, matrix is not invertable, so leave it unchanged." ); |
5353 | statement(ts: "return (det != 0.0f) ? (adj * (1.0f / det)) : m;" ); |
5354 | end_scope(); |
5355 | statement(ts: "" ); |
5356 | break; |
5357 | |
5358 | case SPVFuncImplForwardArgs: |
5359 | statement(ts: "template<typename T> struct spvRemoveReference { typedef T type; };" ); |
5360 | statement(ts: "template<typename T> struct spvRemoveReference<thread T&> { typedef T type; };" ); |
5361 | statement(ts: "template<typename T> struct spvRemoveReference<thread T&&> { typedef T type; };" ); |
5362 | statement(ts: "template<typename T> inline constexpr thread T&& spvForward(thread typename " |
5363 | "spvRemoveReference<T>::type& x)" ); |
5364 | begin_scope(); |
5365 | statement(ts: "return static_cast<thread T&&>(x);" ); |
5366 | end_scope(); |
5367 | statement(ts: "template<typename T> inline constexpr thread T&& spvForward(thread typename " |
5368 | "spvRemoveReference<T>::type&& x)" ); |
5369 | begin_scope(); |
5370 | statement(ts: "return static_cast<thread T&&>(x);" ); |
5371 | end_scope(); |
5372 | statement(ts: "" ); |
5373 | break; |
5374 | |
5375 | case SPVFuncImplGetSwizzle: |
5376 | statement(ts: "enum class spvSwizzle : uint" ); |
5377 | begin_scope(); |
5378 | statement(ts: "none = 0," ); |
5379 | statement(ts: "zero," ); |
5380 | statement(ts: "one," ); |
5381 | statement(ts: "red," ); |
5382 | statement(ts: "green," ); |
5383 | statement(ts: "blue," ); |
5384 | statement(ts: "alpha" ); |
5385 | end_scope_decl(); |
5386 | statement(ts: "" ); |
5387 | statement(ts: "template<typename T>" ); |
5388 | statement(ts: "inline T spvGetSwizzle(vec<T, 4> x, T c, spvSwizzle s)" ); |
5389 | begin_scope(); |
5390 | statement(ts: "switch (s)" ); |
5391 | begin_scope(); |
5392 | statement(ts: "case spvSwizzle::none:" ); |
5393 | statement(ts: " return c;" ); |
5394 | statement(ts: "case spvSwizzle::zero:" ); |
5395 | statement(ts: " return 0;" ); |
5396 | statement(ts: "case spvSwizzle::one:" ); |
5397 | statement(ts: " return 1;" ); |
5398 | statement(ts: "case spvSwizzle::red:" ); |
5399 | statement(ts: " return x.r;" ); |
5400 | statement(ts: "case spvSwizzle::green:" ); |
5401 | statement(ts: " return x.g;" ); |
5402 | statement(ts: "case spvSwizzle::blue:" ); |
5403 | statement(ts: " return x.b;" ); |
5404 | statement(ts: "case spvSwizzle::alpha:" ); |
5405 | statement(ts: " return x.a;" ); |
5406 | end_scope(); |
5407 | end_scope(); |
5408 | statement(ts: "" ); |
5409 | break; |
5410 | |
5411 | case SPVFuncImplTextureSwizzle: |
5412 | statement(ts: "// Wrapper function that swizzles texture samples and fetches." ); |
5413 | statement(ts: "template<typename T>" ); |
5414 | statement(ts: "inline vec<T, 4> spvTextureSwizzle(vec<T, 4> x, uint s)" ); |
5415 | begin_scope(); |
5416 | statement(ts: "if (!s)" ); |
5417 | statement(ts: " return x;" ); |
5418 | statement(ts: "return vec<T, 4>(spvGetSwizzle(x, x.r, spvSwizzle((s >> 0) & 0xFF)), " |
5419 | "spvGetSwizzle(x, x.g, spvSwizzle((s >> 8) & 0xFF)), spvGetSwizzle(x, x.b, spvSwizzle((s >> 16) " |
5420 | "& 0xFF)), " |
5421 | "spvGetSwizzle(x, x.a, spvSwizzle((s >> 24) & 0xFF)));" ); |
5422 | end_scope(); |
5423 | statement(ts: "" ); |
5424 | statement(ts: "template<typename T>" ); |
5425 | statement(ts: "inline T spvTextureSwizzle(T x, uint s)" ); |
5426 | begin_scope(); |
5427 | statement(ts: "return spvTextureSwizzle(vec<T, 4>(x, 0, 0, 1), s).x;" ); |
5428 | end_scope(); |
5429 | statement(ts: "" ); |
5430 | break; |
5431 | |
5432 | case SPVFuncImplGatherSwizzle: |
5433 | statement(ts: "// Wrapper function that swizzles texture gathers." ); |
5434 | statement(ts: "template<typename T, template<typename, access = access::sample, typename = void> class Tex, " |
5435 | "typename... Ts>" ); |
5436 | statement(ts: "inline vec<T, 4> spvGatherSwizzle(const thread Tex<T>& t, sampler s, " |
5437 | "uint sw, component c, Ts... params) METAL_CONST_ARG(c)" ); |
5438 | begin_scope(); |
5439 | statement(ts: "if (sw)" ); |
5440 | begin_scope(); |
5441 | statement(ts: "switch (spvSwizzle((sw >> (uint(c) * 8)) & 0xFF))" ); |
5442 | begin_scope(); |
5443 | statement(ts: "case spvSwizzle::none:" ); |
5444 | statement(ts: " break;" ); |
5445 | statement(ts: "case spvSwizzle::zero:" ); |
5446 | statement(ts: " return vec<T, 4>(0, 0, 0, 0);" ); |
5447 | statement(ts: "case spvSwizzle::one:" ); |
5448 | statement(ts: " return vec<T, 4>(1, 1, 1, 1);" ); |
5449 | statement(ts: "case spvSwizzle::red:" ); |
5450 | statement(ts: " return t.gather(s, spvForward<Ts>(params)..., component::x);" ); |
5451 | statement(ts: "case spvSwizzle::green:" ); |
5452 | statement(ts: " return t.gather(s, spvForward<Ts>(params)..., component::y);" ); |
5453 | statement(ts: "case spvSwizzle::blue:" ); |
5454 | statement(ts: " return t.gather(s, spvForward<Ts>(params)..., component::z);" ); |
5455 | statement(ts: "case spvSwizzle::alpha:" ); |
5456 | statement(ts: " return t.gather(s, spvForward<Ts>(params)..., component::w);" ); |
5457 | end_scope(); |
5458 | end_scope(); |
5459 | // texture::gather insists on its component parameter being a constant |
5460 | // expression, so we need this silly workaround just to compile the shader. |
5461 | statement(ts: "switch (c)" ); |
5462 | begin_scope(); |
5463 | statement(ts: "case component::x:" ); |
5464 | statement(ts: " return t.gather(s, spvForward<Ts>(params)..., component::x);" ); |
5465 | statement(ts: "case component::y:" ); |
5466 | statement(ts: " return t.gather(s, spvForward<Ts>(params)..., component::y);" ); |
5467 | statement(ts: "case component::z:" ); |
5468 | statement(ts: " return t.gather(s, spvForward<Ts>(params)..., component::z);" ); |
5469 | statement(ts: "case component::w:" ); |
5470 | statement(ts: " return t.gather(s, spvForward<Ts>(params)..., component::w);" ); |
5471 | end_scope(); |
5472 | end_scope(); |
5473 | statement(ts: "" ); |
5474 | break; |
5475 | |
5476 | case SPVFuncImplGatherCompareSwizzle: |
5477 | statement(ts: "// Wrapper function that swizzles depth texture gathers." ); |
5478 | statement(ts: "template<typename T, template<typename, access = access::sample, typename = void> class Tex, " |
5479 | "typename... Ts>" ); |
5480 | statement(ts: "inline vec<T, 4> spvGatherCompareSwizzle(const thread Tex<T>& t, sampler " |
5481 | "s, uint sw, Ts... params) " ); |
5482 | begin_scope(); |
5483 | statement(ts: "if (sw)" ); |
5484 | begin_scope(); |
5485 | statement(ts: "switch (spvSwizzle(sw & 0xFF))" ); |
5486 | begin_scope(); |
5487 | statement(ts: "case spvSwizzle::none:" ); |
5488 | statement(ts: "case spvSwizzle::red:" ); |
5489 | statement(ts: " break;" ); |
5490 | statement(ts: "case spvSwizzle::zero:" ); |
5491 | statement(ts: "case spvSwizzle::green:" ); |
5492 | statement(ts: "case spvSwizzle::blue:" ); |
5493 | statement(ts: "case spvSwizzle::alpha:" ); |
5494 | statement(ts: " return vec<T, 4>(0, 0, 0, 0);" ); |
5495 | statement(ts: "case spvSwizzle::one:" ); |
5496 | statement(ts: " return vec<T, 4>(1, 1, 1, 1);" ); |
5497 | end_scope(); |
5498 | end_scope(); |
5499 | statement(ts: "return t.gather_compare(s, spvForward<Ts>(params)...);" ); |
5500 | end_scope(); |
5501 | statement(ts: "" ); |
5502 | break; |
5503 | |
5504 | case SPVFuncImplSubgroupBroadcast: |
5505 | // Metal doesn't allow broadcasting boolean values directly, but we can work around that by broadcasting |
5506 | // them as integers. |
5507 | statement(ts: "template<typename T>" ); |
5508 | statement(ts: "inline T spvSubgroupBroadcast(T value, ushort lane)" ); |
5509 | begin_scope(); |
5510 | if (msl_options.use_quadgroup_operation()) |
5511 | statement(ts: "return quad_broadcast(value, lane);" ); |
5512 | else |
5513 | statement(ts: "return simd_broadcast(value, lane);" ); |
5514 | end_scope(); |
5515 | statement(ts: "" ); |
5516 | statement(ts: "template<>" ); |
5517 | statement(ts: "inline bool spvSubgroupBroadcast(bool value, ushort lane)" ); |
5518 | begin_scope(); |
5519 | if (msl_options.use_quadgroup_operation()) |
5520 | statement(ts: "return !!quad_broadcast((ushort)value, lane);" ); |
5521 | else |
5522 | statement(ts: "return !!simd_broadcast((ushort)value, lane);" ); |
5523 | end_scope(); |
5524 | statement(ts: "" ); |
5525 | statement(ts: "template<uint N>" ); |
5526 | statement(ts: "inline vec<bool, N> spvSubgroupBroadcast(vec<bool, N> value, ushort lane)" ); |
5527 | begin_scope(); |
5528 | if (msl_options.use_quadgroup_operation()) |
5529 | statement(ts: "return (vec<bool, N>)quad_broadcast((vec<ushort, N>)value, lane);" ); |
5530 | else |
5531 | statement(ts: "return (vec<bool, N>)simd_broadcast((vec<ushort, N>)value, lane);" ); |
5532 | end_scope(); |
5533 | statement(ts: "" ); |
5534 | break; |
5535 | |
5536 | case SPVFuncImplSubgroupBroadcastFirst: |
5537 | statement(ts: "template<typename T>" ); |
5538 | statement(ts: "inline T spvSubgroupBroadcastFirst(T value)" ); |
5539 | begin_scope(); |
5540 | if (msl_options.use_quadgroup_operation()) |
5541 | statement(ts: "return quad_broadcast_first(value);" ); |
5542 | else |
5543 | statement(ts: "return simd_broadcast_first(value);" ); |
5544 | end_scope(); |
5545 | statement(ts: "" ); |
5546 | statement(ts: "template<>" ); |
5547 | statement(ts: "inline bool spvSubgroupBroadcastFirst(bool value)" ); |
5548 | begin_scope(); |
5549 | if (msl_options.use_quadgroup_operation()) |
5550 | statement(ts: "return !!quad_broadcast_first((ushort)value);" ); |
5551 | else |
5552 | statement(ts: "return !!simd_broadcast_first((ushort)value);" ); |
5553 | end_scope(); |
5554 | statement(ts: "" ); |
5555 | statement(ts: "template<uint N>" ); |
5556 | statement(ts: "inline vec<bool, N> spvSubgroupBroadcastFirst(vec<bool, N> value)" ); |
5557 | begin_scope(); |
5558 | if (msl_options.use_quadgroup_operation()) |
5559 | statement(ts: "return (vec<bool, N>)quad_broadcast_first((vec<ushort, N>)value);" ); |
5560 | else |
5561 | statement(ts: "return (vec<bool, N>)simd_broadcast_first((vec<ushort, N>)value);" ); |
5562 | end_scope(); |
5563 | statement(ts: "" ); |
5564 | break; |
5565 | |
5566 | case SPVFuncImplSubgroupBallot: |
5567 | statement(ts: "inline uint4 spvSubgroupBallot(bool value)" ); |
5568 | begin_scope(); |
5569 | if (msl_options.use_quadgroup_operation()) |
5570 | { |
5571 | statement(ts: "return uint4((quad_vote::vote_t)quad_ballot(value), 0, 0, 0);" ); |
5572 | } |
5573 | else if (msl_options.is_ios()) |
5574 | { |
5575 | // The current simd_vote on iOS uses a 32-bit integer-like object. |
5576 | statement(ts: "return uint4((simd_vote::vote_t)simd_ballot(value), 0, 0, 0);" ); |
5577 | } |
5578 | else |
5579 | { |
5580 | statement(ts: "simd_vote vote = simd_ballot(value);" ); |
5581 | statement(ts: "// simd_ballot() returns a 64-bit integer-like object, but" ); |
5582 | statement(ts: "// SPIR-V callers expect a uint4. We must convert." ); |
5583 | statement(ts: "// FIXME: This won't include higher bits if Apple ever supports" ); |
5584 | statement(ts: "// 128 lanes in an SIMD-group." ); |
5585 | statement(ts: "return uint4(as_type<uint2>((simd_vote::vote_t)vote), 0, 0);" ); |
5586 | } |
5587 | end_scope(); |
5588 | statement(ts: "" ); |
5589 | break; |
5590 | |
5591 | case SPVFuncImplSubgroupBallotBitExtract: |
5592 | statement(ts: "inline bool spvSubgroupBallotBitExtract(uint4 ballot, uint bit)" ); |
5593 | begin_scope(); |
5594 | statement(ts: "return !!extract_bits(ballot[bit / 32], bit % 32, 1);" ); |
5595 | end_scope(); |
5596 | statement(ts: "" ); |
5597 | break; |
5598 | |
5599 | case SPVFuncImplSubgroupBallotFindLSB: |
5600 | statement(ts: "inline uint spvSubgroupBallotFindLSB(uint4 ballot, uint gl_SubgroupSize)" ); |
5601 | begin_scope(); |
5602 | if (msl_options.is_ios()) |
5603 | { |
5604 | statement(ts: "uint4 mask = uint4(extract_bits(0xFFFFFFFF, 0, gl_SubgroupSize), uint3(0));" ); |
5605 | } |
5606 | else |
5607 | { |
5608 | statement(ts: "uint4 mask = uint4(extract_bits(0xFFFFFFFF, 0, min(gl_SubgroupSize, 32u)), " |
5609 | "extract_bits(0xFFFFFFFF, 0, (uint)max((int)gl_SubgroupSize - 32, 0)), uint2(0));" ); |
5610 | } |
5611 | statement(ts: "ballot &= mask;" ); |
5612 | statement(ts: "return select(ctz(ballot.x), select(32 + ctz(ballot.y), select(64 + ctz(ballot.z), select(96 + " |
5613 | "ctz(ballot.w), uint(-1), ballot.w == 0), ballot.z == 0), ballot.y == 0), ballot.x == 0);" ); |
5614 | end_scope(); |
5615 | statement(ts: "" ); |
5616 | break; |
5617 | |
5618 | case SPVFuncImplSubgroupBallotFindMSB: |
5619 | statement(ts: "inline uint spvSubgroupBallotFindMSB(uint4 ballot, uint gl_SubgroupSize)" ); |
5620 | begin_scope(); |
5621 | if (msl_options.is_ios()) |
5622 | { |
5623 | statement(ts: "uint4 mask = uint4(extract_bits(0xFFFFFFFF, 0, gl_SubgroupSize), uint3(0));" ); |
5624 | } |
5625 | else |
5626 | { |
5627 | statement(ts: "uint4 mask = uint4(extract_bits(0xFFFFFFFF, 0, min(gl_SubgroupSize, 32u)), " |
5628 | "extract_bits(0xFFFFFFFF, 0, (uint)max((int)gl_SubgroupSize - 32, 0)), uint2(0));" ); |
5629 | } |
5630 | statement(ts: "ballot &= mask;" ); |
5631 | statement(ts: "return select(128 - (clz(ballot.w) + 1), select(96 - (clz(ballot.z) + 1), select(64 - " |
5632 | "(clz(ballot.y) + 1), select(32 - (clz(ballot.x) + 1), uint(-1), ballot.x == 0), ballot.y == 0), " |
5633 | "ballot.z == 0), ballot.w == 0);" ); |
5634 | end_scope(); |
5635 | statement(ts: "" ); |
5636 | break; |
5637 | |
5638 | case SPVFuncImplSubgroupBallotBitCount: |
5639 | statement(ts: "inline uint spvPopCount4(uint4 ballot)" ); |
5640 | begin_scope(); |
5641 | statement(ts: "return popcount(ballot.x) + popcount(ballot.y) + popcount(ballot.z) + popcount(ballot.w);" ); |
5642 | end_scope(); |
5643 | statement(ts: "" ); |
5644 | statement(ts: "inline uint spvSubgroupBallotBitCount(uint4 ballot, uint gl_SubgroupSize)" ); |
5645 | begin_scope(); |
5646 | if (msl_options.is_ios()) |
5647 | { |
5648 | statement(ts: "uint4 mask = uint4(extract_bits(0xFFFFFFFF, 0, gl_SubgroupSize), uint3(0));" ); |
5649 | } |
5650 | else |
5651 | { |
5652 | statement(ts: "uint4 mask = uint4(extract_bits(0xFFFFFFFF, 0, min(gl_SubgroupSize, 32u)), " |
5653 | "extract_bits(0xFFFFFFFF, 0, (uint)max((int)gl_SubgroupSize - 32, 0)), uint2(0));" ); |
5654 | } |
5655 | statement(ts: "return spvPopCount4(ballot & mask);" ); |
5656 | end_scope(); |
5657 | statement(ts: "" ); |
5658 | statement(ts: "inline uint spvSubgroupBallotInclusiveBitCount(uint4 ballot, uint gl_SubgroupInvocationID)" ); |
5659 | begin_scope(); |
5660 | if (msl_options.is_ios()) |
5661 | { |
5662 | statement(ts: "uint4 mask = uint4(extract_bits(0xFFFFFFFF, 0, gl_SubgroupInvocationID + 1), uint3(0));" ); |
5663 | } |
5664 | else |
5665 | { |
5666 | statement(ts: "uint4 mask = uint4(extract_bits(0xFFFFFFFF, 0, min(gl_SubgroupInvocationID + 1, 32u)), " |
5667 | "extract_bits(0xFFFFFFFF, 0, (uint)max((int)gl_SubgroupInvocationID + 1 - 32, 0)), " |
5668 | "uint2(0));" ); |
5669 | } |
5670 | statement(ts: "return spvPopCount4(ballot & mask);" ); |
5671 | end_scope(); |
5672 | statement(ts: "" ); |
5673 | statement(ts: "inline uint spvSubgroupBallotExclusiveBitCount(uint4 ballot, uint gl_SubgroupInvocationID)" ); |
5674 | begin_scope(); |
5675 | if (msl_options.is_ios()) |
5676 | { |
5677 | statement(ts: "uint4 mask = uint4(extract_bits(0xFFFFFFFF, 0, gl_SubgroupInvocationID), uint2(0));" ); |
5678 | } |
5679 | else |
5680 | { |
5681 | statement(ts: "uint4 mask = uint4(extract_bits(0xFFFFFFFF, 0, min(gl_SubgroupInvocationID, 32u)), " |
5682 | "extract_bits(0xFFFFFFFF, 0, (uint)max((int)gl_SubgroupInvocationID - 32, 0)), uint2(0));" ); |
5683 | } |
5684 | statement(ts: "return spvPopCount4(ballot & mask);" ); |
5685 | end_scope(); |
5686 | statement(ts: "" ); |
5687 | break; |
5688 | |
5689 | case SPVFuncImplSubgroupAllEqual: |
5690 | // Metal doesn't provide a function to evaluate this directly. But, we can |
5691 | // implement this by comparing every thread's value to one thread's value |
5692 | // (in this case, the value of the first active thread). Then, by the transitive |
5693 | // property of equality, if all comparisons return true, then they are all equal. |
5694 | statement(ts: "template<typename T>" ); |
5695 | statement(ts: "inline bool spvSubgroupAllEqual(T value)" ); |
5696 | begin_scope(); |
5697 | if (msl_options.use_quadgroup_operation()) |
5698 | statement(ts: "return quad_all(all(value == quad_broadcast_first(value)));" ); |
5699 | else |
5700 | statement(ts: "return simd_all(all(value == simd_broadcast_first(value)));" ); |
5701 | end_scope(); |
5702 | statement(ts: "" ); |
5703 | statement(ts: "template<>" ); |
5704 | statement(ts: "inline bool spvSubgroupAllEqual(bool value)" ); |
5705 | begin_scope(); |
5706 | if (msl_options.use_quadgroup_operation()) |
5707 | statement(ts: "return quad_all(value) || !quad_any(value);" ); |
5708 | else |
5709 | statement(ts: "return simd_all(value) || !simd_any(value);" ); |
5710 | end_scope(); |
5711 | statement(ts: "" ); |
5712 | statement(ts: "template<uint N>" ); |
5713 | statement(ts: "inline bool spvSubgroupAllEqual(vec<bool, N> value)" ); |
5714 | begin_scope(); |
5715 | if (msl_options.use_quadgroup_operation()) |
5716 | statement(ts: "return quad_all(all(value == (vec<bool, N>)quad_broadcast_first((vec<ushort, N>)value)));" ); |
5717 | else |
5718 | statement(ts: "return simd_all(all(value == (vec<bool, N>)simd_broadcast_first((vec<ushort, N>)value)));" ); |
5719 | end_scope(); |
5720 | statement(ts: "" ); |
5721 | break; |
5722 | |
5723 | case SPVFuncImplSubgroupShuffle: |
5724 | statement(ts: "template<typename T>" ); |
5725 | statement(ts: "inline T spvSubgroupShuffle(T value, ushort lane)" ); |
5726 | begin_scope(); |
5727 | if (msl_options.use_quadgroup_operation()) |
5728 | statement(ts: "return quad_shuffle(value, lane);" ); |
5729 | else |
5730 | statement(ts: "return simd_shuffle(value, lane);" ); |
5731 | end_scope(); |
5732 | statement(ts: "" ); |
5733 | statement(ts: "template<>" ); |
5734 | statement(ts: "inline bool spvSubgroupShuffle(bool value, ushort lane)" ); |
5735 | begin_scope(); |
5736 | if (msl_options.use_quadgroup_operation()) |
5737 | statement(ts: "return !!quad_shuffle((ushort)value, lane);" ); |
5738 | else |
5739 | statement(ts: "return !!simd_shuffle((ushort)value, lane);" ); |
5740 | end_scope(); |
5741 | statement(ts: "" ); |
5742 | statement(ts: "template<uint N>" ); |
5743 | statement(ts: "inline vec<bool, N> spvSubgroupShuffle(vec<bool, N> value, ushort lane)" ); |
5744 | begin_scope(); |
5745 | if (msl_options.use_quadgroup_operation()) |
5746 | statement(ts: "return (vec<bool, N>)quad_shuffle((vec<ushort, N>)value, lane);" ); |
5747 | else |
5748 | statement(ts: "return (vec<bool, N>)simd_shuffle((vec<ushort, N>)value, lane);" ); |
5749 | end_scope(); |
5750 | statement(ts: "" ); |
5751 | break; |
5752 | |
5753 | case SPVFuncImplSubgroupShuffleXor: |
5754 | statement(ts: "template<typename T>" ); |
5755 | statement(ts: "inline T spvSubgroupShuffleXor(T value, ushort mask)" ); |
5756 | begin_scope(); |
5757 | if (msl_options.use_quadgroup_operation()) |
5758 | statement(ts: "return quad_shuffle_xor(value, mask);" ); |
5759 | else |
5760 | statement(ts: "return simd_shuffle_xor(value, mask);" ); |
5761 | end_scope(); |
5762 | statement(ts: "" ); |
5763 | statement(ts: "template<>" ); |
5764 | statement(ts: "inline bool spvSubgroupShuffleXor(bool value, ushort mask)" ); |
5765 | begin_scope(); |
5766 | if (msl_options.use_quadgroup_operation()) |
5767 | statement(ts: "return !!quad_shuffle_xor((ushort)value, mask);" ); |
5768 | else |
5769 | statement(ts: "return !!simd_shuffle_xor((ushort)value, mask);" ); |
5770 | end_scope(); |
5771 | statement(ts: "" ); |
5772 | statement(ts: "template<uint N>" ); |
5773 | statement(ts: "inline vec<bool, N> spvSubgroupShuffleXor(vec<bool, N> value, ushort mask)" ); |
5774 | begin_scope(); |
5775 | if (msl_options.use_quadgroup_operation()) |
5776 | statement(ts: "return (vec<bool, N>)quad_shuffle_xor((vec<ushort, N>)value, mask);" ); |
5777 | else |
5778 | statement(ts: "return (vec<bool, N>)simd_shuffle_xor((vec<ushort, N>)value, mask);" ); |
5779 | end_scope(); |
5780 | statement(ts: "" ); |
5781 | break; |
5782 | |
5783 | case SPVFuncImplSubgroupShuffleUp: |
5784 | statement(ts: "template<typename T>" ); |
5785 | statement(ts: "inline T spvSubgroupShuffleUp(T value, ushort delta)" ); |
5786 | begin_scope(); |
5787 | if (msl_options.use_quadgroup_operation()) |
5788 | statement(ts: "return quad_shuffle_up(value, delta);" ); |
5789 | else |
5790 | statement(ts: "return simd_shuffle_up(value, delta);" ); |
5791 | end_scope(); |
5792 | statement(ts: "" ); |
5793 | statement(ts: "template<>" ); |
5794 | statement(ts: "inline bool spvSubgroupShuffleUp(bool value, ushort delta)" ); |
5795 | begin_scope(); |
5796 | if (msl_options.use_quadgroup_operation()) |
5797 | statement(ts: "return !!quad_shuffle_up((ushort)value, delta);" ); |
5798 | else |
5799 | statement(ts: "return !!simd_shuffle_up((ushort)value, delta);" ); |
5800 | end_scope(); |
5801 | statement(ts: "" ); |
5802 | statement(ts: "template<uint N>" ); |
5803 | statement(ts: "inline vec<bool, N> spvSubgroupShuffleUp(vec<bool, N> value, ushort delta)" ); |
5804 | begin_scope(); |
5805 | if (msl_options.use_quadgroup_operation()) |
5806 | statement(ts: "return (vec<bool, N>)quad_shuffle_up((vec<ushort, N>)value, delta);" ); |
5807 | else |
5808 | statement(ts: "return (vec<bool, N>)simd_shuffle_up((vec<ushort, N>)value, delta);" ); |
5809 | end_scope(); |
5810 | statement(ts: "" ); |
5811 | break; |
5812 | |
5813 | case SPVFuncImplSubgroupShuffleDown: |
5814 | statement(ts: "template<typename T>" ); |
5815 | statement(ts: "inline T spvSubgroupShuffleDown(T value, ushort delta)" ); |
5816 | begin_scope(); |
5817 | if (msl_options.use_quadgroup_operation()) |
5818 | statement(ts: "return quad_shuffle_down(value, delta);" ); |
5819 | else |
5820 | statement(ts: "return simd_shuffle_down(value, delta);" ); |
5821 | end_scope(); |
5822 | statement(ts: "" ); |
5823 | statement(ts: "template<>" ); |
5824 | statement(ts: "inline bool spvSubgroupShuffleDown(bool value, ushort delta)" ); |
5825 | begin_scope(); |
5826 | if (msl_options.use_quadgroup_operation()) |
5827 | statement(ts: "return !!quad_shuffle_down((ushort)value, delta);" ); |
5828 | else |
5829 | statement(ts: "return !!simd_shuffle_down((ushort)value, delta);" ); |
5830 | end_scope(); |
5831 | statement(ts: "" ); |
5832 | statement(ts: "template<uint N>" ); |
5833 | statement(ts: "inline vec<bool, N> spvSubgroupShuffleDown(vec<bool, N> value, ushort delta)" ); |
5834 | begin_scope(); |
5835 | if (msl_options.use_quadgroup_operation()) |
5836 | statement(ts: "return (vec<bool, N>)quad_shuffle_down((vec<ushort, N>)value, delta);" ); |
5837 | else |
5838 | statement(ts: "return (vec<bool, N>)simd_shuffle_down((vec<ushort, N>)value, delta);" ); |
5839 | end_scope(); |
5840 | statement(ts: "" ); |
5841 | break; |
5842 | |
5843 | case SPVFuncImplQuadBroadcast: |
5844 | statement(ts: "template<typename T>" ); |
5845 | statement(ts: "inline T spvQuadBroadcast(T value, uint lane)" ); |
5846 | begin_scope(); |
5847 | statement(ts: "return quad_broadcast(value, lane);" ); |
5848 | end_scope(); |
5849 | statement(ts: "" ); |
5850 | statement(ts: "template<>" ); |
5851 | statement(ts: "inline bool spvQuadBroadcast(bool value, uint lane)" ); |
5852 | begin_scope(); |
5853 | statement(ts: "return !!quad_broadcast((ushort)value, lane);" ); |
5854 | end_scope(); |
5855 | statement(ts: "" ); |
5856 | statement(ts: "template<uint N>" ); |
5857 | statement(ts: "inline vec<bool, N> spvQuadBroadcast(vec<bool, N> value, uint lane)" ); |
5858 | begin_scope(); |
5859 | statement(ts: "return (vec<bool, N>)quad_broadcast((vec<ushort, N>)value, lane);" ); |
5860 | end_scope(); |
5861 | statement(ts: "" ); |
5862 | break; |
5863 | |
5864 | case SPVFuncImplQuadSwap: |
5865 | // We can implement this easily based on the following table giving |
5866 | // the target lane ID from the direction and current lane ID: |
5867 | // Direction |
5868 | // | 0 | 1 | 2 | |
5869 | // ---+---+---+---+ |
5870 | // L 0 | 1 2 3 |
5871 | // a 1 | 0 3 2 |
5872 | // n 2 | 3 0 1 |
5873 | // e 3 | 2 1 0 |
5874 | // Notice that target = source ^ (direction + 1). |
5875 | statement(ts: "template<typename T>" ); |
5876 | statement(ts: "inline T spvQuadSwap(T value, uint dir)" ); |
5877 | begin_scope(); |
5878 | statement(ts: "return quad_shuffle_xor(value, dir + 1);" ); |
5879 | end_scope(); |
5880 | statement(ts: "" ); |
5881 | statement(ts: "template<>" ); |
5882 | statement(ts: "inline bool spvQuadSwap(bool value, uint dir)" ); |
5883 | begin_scope(); |
5884 | statement(ts: "return !!quad_shuffle_xor((ushort)value, dir + 1);" ); |
5885 | end_scope(); |
5886 | statement(ts: "" ); |
5887 | statement(ts: "template<uint N>" ); |
5888 | statement(ts: "inline vec<bool, N> spvQuadSwap(vec<bool, N> value, uint dir)" ); |
5889 | begin_scope(); |
5890 | statement(ts: "return (vec<bool, N>)quad_shuffle_xor((vec<ushort, N>)value, dir + 1);" ); |
5891 | end_scope(); |
5892 | statement(ts: "" ); |
5893 | break; |
5894 | |
5895 | case SPVFuncImplReflectScalar: |
5896 | // Metal does not support scalar versions of these functions. |
5897 | // Ensure fast-math is disabled to match Vulkan results. |
5898 | statement(ts: "template<typename T>" ); |
5899 | statement(ts: "[[clang::optnone]] T spvReflect(T i, T n)" ); |
5900 | begin_scope(); |
5901 | statement(ts: "return i - T(2) * i * n * n;" ); |
5902 | end_scope(); |
5903 | statement(ts: "" ); |
5904 | break; |
5905 | |
5906 | case SPVFuncImplRefractScalar: |
5907 | // Metal does not support scalar versions of these functions. |
5908 | statement(ts: "template<typename T>" ); |
5909 | statement(ts: "inline T spvRefract(T i, T n, T eta)" ); |
5910 | begin_scope(); |
5911 | statement(ts: "T NoI = n * i;" ); |
5912 | statement(ts: "T NoI2 = NoI * NoI;" ); |
5913 | statement(ts: "T k = T(1) - eta * eta * (T(1) - NoI2);" ); |
5914 | statement(ts: "if (k < T(0))" ); |
5915 | begin_scope(); |
5916 | statement(ts: "return T(0);" ); |
5917 | end_scope(); |
5918 | statement(ts: "else" ); |
5919 | begin_scope(); |
5920 | statement(ts: "return eta * i - (eta * NoI + sqrt(k)) * n;" ); |
5921 | end_scope(); |
5922 | end_scope(); |
5923 | statement(ts: "" ); |
5924 | break; |
5925 | |
5926 | case SPVFuncImplFaceForwardScalar: |
5927 | // Metal does not support scalar versions of these functions. |
5928 | statement(ts: "template<typename T>" ); |
5929 | statement(ts: "inline T spvFaceForward(T n, T i, T nref)" ); |
5930 | begin_scope(); |
5931 | statement(ts: "return i * nref < T(0) ? n : -n;" ); |
5932 | end_scope(); |
5933 | statement(ts: "" ); |
5934 | break; |
5935 | |
5936 | case SPVFuncImplChromaReconstructNearest2Plane: |
5937 | statement(ts: "template<typename T, typename... LodOptions>" ); |
5938 | statement(ts: "inline vec<T, 4> spvChromaReconstructNearest(texture2d<T> plane0, texture2d<T> plane1, sampler " |
5939 | "samp, float2 coord, LodOptions... options)" ); |
5940 | begin_scope(); |
5941 | statement(ts: "vec<T, 4> ycbcr = vec<T, 4>(0, 0, 0, 1);" ); |
5942 | statement(ts: "ycbcr.g = plane0.sample(samp, coord, spvForward<LodOptions>(options)...).r;" ); |
5943 | statement(ts: "ycbcr.br = plane1.sample(samp, coord, spvForward<LodOptions>(options)...).rg;" ); |
5944 | statement(ts: "return ycbcr;" ); |
5945 | end_scope(); |
5946 | statement(ts: "" ); |
5947 | break; |
5948 | |
5949 | case SPVFuncImplChromaReconstructNearest3Plane: |
5950 | statement(ts: "template<typename T, typename... LodOptions>" ); |
5951 | statement(ts: "inline vec<T, 4> spvChromaReconstructNearest(texture2d<T> plane0, texture2d<T> plane1, " |
5952 | "texture2d<T> plane2, sampler samp, float2 coord, LodOptions... options)" ); |
5953 | begin_scope(); |
5954 | statement(ts: "vec<T, 4> ycbcr = vec<T, 4>(0, 0, 0, 1);" ); |
5955 | statement(ts: "ycbcr.g = plane0.sample(samp, coord, spvForward<LodOptions>(options)...).r;" ); |
5956 | statement(ts: "ycbcr.b = plane1.sample(samp, coord, spvForward<LodOptions>(options)...).r;" ); |
5957 | statement(ts: "ycbcr.r = plane2.sample(samp, coord, spvForward<LodOptions>(options)...).r;" ); |
5958 | statement(ts: "return ycbcr;" ); |
5959 | end_scope(); |
5960 | statement(ts: "" ); |
5961 | break; |
5962 | |
5963 | case SPVFuncImplChromaReconstructLinear422CositedEven2Plane: |
5964 | statement(ts: "template<typename T, typename... LodOptions>" ); |
5965 | statement(ts: "inline vec<T, 4> spvChromaReconstructLinear422CositedEven(texture2d<T> plane0, texture2d<T> " |
5966 | "plane1, sampler samp, float2 coord, LodOptions... options)" ); |
5967 | begin_scope(); |
5968 | statement(ts: "vec<T, 4> ycbcr = vec<T, 4>(0, 0, 0, 1);" ); |
5969 | statement(ts: "ycbcr.g = plane0.sample(samp, coord, spvForward<LodOptions>(options)...).r;" ); |
5970 | statement(ts: "if (fract(coord.x * plane1.get_width()) != 0.0)" ); |
5971 | begin_scope(); |
5972 | statement(ts: "ycbcr.br = vec<T, 2>(mix(plane1.sample(samp, coord, spvForward<LodOptions>(options)...), " |
5973 | "plane1.sample(samp, coord, spvForward<LodOptions>(options)..., int2(1, 0)), 0.5).rg);" ); |
5974 | end_scope(); |
5975 | statement(ts: "else" ); |
5976 | begin_scope(); |
5977 | statement(ts: "ycbcr.br = plane1.sample(samp, coord, spvForward<LodOptions>(options)...).rg;" ); |
5978 | end_scope(); |
5979 | statement(ts: "return ycbcr;" ); |
5980 | end_scope(); |
5981 | statement(ts: "" ); |
5982 | break; |
5983 | |
5984 | case SPVFuncImplChromaReconstructLinear422CositedEven3Plane: |
5985 | statement(ts: "template<typename T, typename... LodOptions>" ); |
5986 | statement(ts: "inline vec<T, 4> spvChromaReconstructLinear422CositedEven(texture2d<T> plane0, texture2d<T> " |
5987 | "plane1, texture2d<T> plane2, sampler samp, float2 coord, LodOptions... options)" ); |
5988 | begin_scope(); |
5989 | statement(ts: "vec<T, 4> ycbcr = vec<T, 4>(0, 0, 0, 1);" ); |
5990 | statement(ts: "ycbcr.g = plane0.sample(samp, coord, spvForward<LodOptions>(options)...).r;" ); |
5991 | statement(ts: "if (fract(coord.x * plane1.get_width()) != 0.0)" ); |
5992 | begin_scope(); |
5993 | statement(ts: "ycbcr.b = T(mix(plane1.sample(samp, coord, spvForward<LodOptions>(options)...), " |
5994 | "plane1.sample(samp, coord, spvForward<LodOptions>(options)..., int2(1, 0)), 0.5).r);" ); |
5995 | statement(ts: "ycbcr.r = T(mix(plane2.sample(samp, coord, spvForward<LodOptions>(options)...), " |
5996 | "plane2.sample(samp, coord, spvForward<LodOptions>(options)..., int2(1, 0)), 0.5).r);" ); |
5997 | end_scope(); |
5998 | statement(ts: "else" ); |
5999 | begin_scope(); |
6000 | statement(ts: "ycbcr.b = plane1.sample(samp, coord, spvForward<LodOptions>(options)...).r;" ); |
6001 | statement(ts: "ycbcr.r = plane2.sample(samp, coord, spvForward<LodOptions>(options)...).r;" ); |
6002 | end_scope(); |
6003 | statement(ts: "return ycbcr;" ); |
6004 | end_scope(); |
6005 | statement(ts: "" ); |
6006 | break; |
6007 | |
6008 | case SPVFuncImplChromaReconstructLinear422Midpoint2Plane: |
6009 | statement(ts: "template<typename T, typename... LodOptions>" ); |
6010 | statement(ts: "inline vec<T, 4> spvChromaReconstructLinear422Midpoint(texture2d<T> plane0, texture2d<T> " |
6011 | "plane1, sampler samp, float2 coord, LodOptions... options)" ); |
6012 | begin_scope(); |
6013 | statement(ts: "vec<T, 4> ycbcr = vec<T, 4>(0, 0, 0, 1);" ); |
6014 | statement(ts: "ycbcr.g = plane0.sample(samp, coord, spvForward<LodOptions>(options)...).r;" ); |
6015 | statement(ts: "int2 offs = int2(fract(coord.x * plane1.get_width()) != 0.0 ? 1 : -1, 0);" ); |
6016 | statement(ts: "ycbcr.br = vec<T, 2>(mix(plane1.sample(samp, coord, spvForward<LodOptions>(options)...), " |
6017 | "plane1.sample(samp, coord, spvForward<LodOptions>(options)..., offs), 0.25).rg);" ); |
6018 | statement(ts: "return ycbcr;" ); |
6019 | end_scope(); |
6020 | statement(ts: "" ); |
6021 | break; |
6022 | |
6023 | case SPVFuncImplChromaReconstructLinear422Midpoint3Plane: |
6024 | statement(ts: "template<typename T, typename... LodOptions>" ); |
6025 | statement(ts: "inline vec<T, 4> spvChromaReconstructLinear422Midpoint(texture2d<T> plane0, texture2d<T> " |
6026 | "plane1, texture2d<T> plane2, sampler samp, float2 coord, LodOptions... options)" ); |
6027 | begin_scope(); |
6028 | statement(ts: "vec<T, 4> ycbcr = vec<T, 4>(0, 0, 0, 1);" ); |
6029 | statement(ts: "ycbcr.g = plane0.sample(samp, coord, spvForward<LodOptions>(options)...).r;" ); |
6030 | statement(ts: "int2 offs = int2(fract(coord.x * plane1.get_width()) != 0.0 ? 1 : -1, 0);" ); |
6031 | statement(ts: "ycbcr.b = T(mix(plane1.sample(samp, coord, spvForward<LodOptions>(options)...), " |
6032 | "plane1.sample(samp, coord, spvForward<LodOptions>(options)..., offs), 0.25).r);" ); |
6033 | statement(ts: "ycbcr.r = T(mix(plane2.sample(samp, coord, spvForward<LodOptions>(options)...), " |
6034 | "plane2.sample(samp, coord, spvForward<LodOptions>(options)..., offs), 0.25).r);" ); |
6035 | statement(ts: "return ycbcr;" ); |
6036 | end_scope(); |
6037 | statement(ts: "" ); |
6038 | break; |
6039 | |
6040 | case SPVFuncImplChromaReconstructLinear420XCositedEvenYCositedEven2Plane: |
6041 | statement(ts: "template<typename T, typename... LodOptions>" ); |
6042 | statement(ts: "inline vec<T, 4> spvChromaReconstructLinear420XCositedEvenYCositedEven(texture2d<T> plane0, " |
6043 | "texture2d<T> plane1, sampler samp, float2 coord, LodOptions... options)" ); |
6044 | begin_scope(); |
6045 | statement(ts: "vec<T, 4> ycbcr = vec<T, 4>(0, 0, 0, 1);" ); |
6046 | statement(ts: "ycbcr.g = plane0.sample(samp, coord, spvForward<LodOptions>(options)...).r;" ); |
6047 | statement(ts: "float2 ab = fract(round(coord * float2(plane0.get_width(), plane0.get_height())) * 0.5);" ); |
6048 | statement(ts: "ycbcr.br = vec<T, 2>(mix(mix(plane1.sample(samp, coord, spvForward<LodOptions>(options)...), " |
6049 | "plane1.sample(samp, coord, spvForward<LodOptions>(options)..., int2(1, 0)), ab.x), " |
6050 | "mix(plane1.sample(samp, coord, spvForward<LodOptions>(options)..., int2(0, 1)), " |
6051 | "plane1.sample(samp, coord, spvForward<LodOptions>(options)..., int2(1, 1)), ab.x), ab.y).rg);" ); |
6052 | statement(ts: "return ycbcr;" ); |
6053 | end_scope(); |
6054 | statement(ts: "" ); |
6055 | break; |
6056 | |
6057 | case SPVFuncImplChromaReconstructLinear420XCositedEvenYCositedEven3Plane: |
6058 | statement(ts: "template<typename T, typename... LodOptions>" ); |
6059 | statement(ts: "inline vec<T, 4> spvChromaReconstructLinear420XCositedEvenYCositedEven(texture2d<T> plane0, " |
6060 | "texture2d<T> plane1, texture2d<T> plane2, sampler samp, float2 coord, LodOptions... options)" ); |
6061 | begin_scope(); |
6062 | statement(ts: "vec<T, 4> ycbcr = vec<T, 4>(0, 0, 0, 1);" ); |
6063 | statement(ts: "ycbcr.g = plane0.sample(samp, coord, spvForward<LodOptions>(options)...).r;" ); |
6064 | statement(ts: "float2 ab = fract(round(coord * float2(plane0.get_width(), plane0.get_height())) * 0.5);" ); |
6065 | statement(ts: "ycbcr.b = T(mix(mix(plane1.sample(samp, coord, spvForward<LodOptions>(options)...), " |
6066 | "plane1.sample(samp, coord, spvForward<LodOptions>(options)..., int2(1, 0)), ab.x), " |
6067 | "mix(plane1.sample(samp, coord, spvForward<LodOptions>(options)..., int2(0, 1)), " |
6068 | "plane1.sample(samp, coord, spvForward<LodOptions>(options)..., int2(1, 1)), ab.x), ab.y).r);" ); |
6069 | statement(ts: "ycbcr.r = T(mix(mix(plane2.sample(samp, coord, spvForward<LodOptions>(options)...), " |
6070 | "plane2.sample(samp, coord, spvForward<LodOptions>(options)..., int2(1, 0)), ab.x), " |
6071 | "mix(plane2.sample(samp, coord, spvForward<LodOptions>(options)..., int2(0, 1)), " |
6072 | "plane2.sample(samp, coord, spvForward<LodOptions>(options)..., int2(1, 1)), ab.x), ab.y).r);" ); |
6073 | statement(ts: "return ycbcr;" ); |
6074 | end_scope(); |
6075 | statement(ts: "" ); |
6076 | break; |
6077 | |
6078 | case SPVFuncImplChromaReconstructLinear420XMidpointYCositedEven2Plane: |
6079 | statement(ts: "template<typename T, typename... LodOptions>" ); |
6080 | statement(ts: "inline vec<T, 4> spvChromaReconstructLinear420XMidpointYCositedEven(texture2d<T> plane0, " |
6081 | "texture2d<T> plane1, sampler samp, float2 coord, LodOptions... options)" ); |
6082 | begin_scope(); |
6083 | statement(ts: "vec<T, 4> ycbcr = vec<T, 4>(0, 0, 0, 1);" ); |
6084 | statement(ts: "ycbcr.g = plane0.sample(samp, coord, spvForward<LodOptions>(options)...).r;" ); |
6085 | statement(ts: "float2 ab = fract((round(coord * float2(plane0.get_width(), plane0.get_height())) - float2(0.5, " |
6086 | "0)) * 0.5);" ); |
6087 | statement(ts: "ycbcr.br = vec<T, 2>(mix(mix(plane1.sample(samp, coord, spvForward<LodOptions>(options)...), " |
6088 | "plane1.sample(samp, coord, spvForward<LodOptions>(options)..., int2(1, 0)), ab.x), " |
6089 | "mix(plane1.sample(samp, coord, spvForward<LodOptions>(options)..., int2(0, 1)), " |
6090 | "plane1.sample(samp, coord, spvForward<LodOptions>(options)..., int2(1, 1)), ab.x), ab.y).rg);" ); |
6091 | statement(ts: "return ycbcr;" ); |
6092 | end_scope(); |
6093 | statement(ts: "" ); |
6094 | break; |
6095 | |
6096 | case SPVFuncImplChromaReconstructLinear420XMidpointYCositedEven3Plane: |
6097 | statement(ts: "template<typename T, typename... LodOptions>" ); |
6098 | statement(ts: "inline vec<T, 4> spvChromaReconstructLinear420XMidpointYCositedEven(texture2d<T> plane0, " |
6099 | "texture2d<T> plane1, texture2d<T> plane2, sampler samp, float2 coord, LodOptions... options)" ); |
6100 | begin_scope(); |
6101 | statement(ts: "vec<T, 4> ycbcr = vec<T, 4>(0, 0, 0, 1);" ); |
6102 | statement(ts: "ycbcr.g = plane0.sample(samp, coord, spvForward<LodOptions>(options)...).r;" ); |
6103 | statement(ts: "float2 ab = fract((round(coord * float2(plane0.get_width(), plane0.get_height())) - float2(0.5, " |
6104 | "0)) * 0.5);" ); |
6105 | statement(ts: "ycbcr.b = T(mix(mix(plane1.sample(samp, coord, spvForward<LodOptions>(options)...), " |
6106 | "plane1.sample(samp, coord, spvForward<LodOptions>(options)..., int2(1, 0)), ab.x), " |
6107 | "mix(plane1.sample(samp, coord, spvForward<LodOptions>(options)..., int2(0, 1)), " |
6108 | "plane1.sample(samp, coord, spvForward<LodOptions>(options)..., int2(1, 1)), ab.x), ab.y).r);" ); |
6109 | statement(ts: "ycbcr.r = T(mix(mix(plane2.sample(samp, coord, spvForward<LodOptions>(options)...), " |
6110 | "plane2.sample(samp, coord, spvForward<LodOptions>(options)..., int2(1, 0)), ab.x), " |
6111 | "mix(plane2.sample(samp, coord, spvForward<LodOptions>(options)..., int2(0, 1)), " |
6112 | "plane2.sample(samp, coord, spvForward<LodOptions>(options)..., int2(1, 1)), ab.x), ab.y).r);" ); |
6113 | statement(ts: "return ycbcr;" ); |
6114 | end_scope(); |
6115 | statement(ts: "" ); |
6116 | break; |
6117 | |
6118 | case SPVFuncImplChromaReconstructLinear420XCositedEvenYMidpoint2Plane: |
6119 | statement(ts: "template<typename T, typename... LodOptions>" ); |
6120 | statement(ts: "inline vec<T, 4> spvChromaReconstructLinear420XCositedEvenYMidpoint(texture2d<T> plane0, " |
6121 | "texture2d<T> plane1, sampler samp, float2 coord, LodOptions... options)" ); |
6122 | begin_scope(); |
6123 | statement(ts: "vec<T, 4> ycbcr = vec<T, 4>(0, 0, 0, 1);" ); |
6124 | statement(ts: "ycbcr.g = plane0.sample(samp, coord, spvForward<LodOptions>(options)...).r;" ); |
6125 | statement(ts: "float2 ab = fract((round(coord * float2(plane0.get_width(), plane0.get_height())) - float2(0, " |
6126 | "0.5)) * 0.5);" ); |
6127 | statement(ts: "ycbcr.br = vec<T, 2>(mix(mix(plane1.sample(samp, coord, spvForward<LodOptions>(options)...), " |
6128 | "plane1.sample(samp, coord, spvForward<LodOptions>(options)..., int2(1, 0)), ab.x), " |
6129 | "mix(plane1.sample(samp, coord, spvForward<LodOptions>(options)..., int2(0, 1)), " |
6130 | "plane1.sample(samp, coord, spvForward<LodOptions>(options)..., int2(1, 1)), ab.x), ab.y).rg);" ); |
6131 | statement(ts: "return ycbcr;" ); |
6132 | end_scope(); |
6133 | statement(ts: "" ); |
6134 | break; |
6135 | |
6136 | case SPVFuncImplChromaReconstructLinear420XCositedEvenYMidpoint3Plane: |
6137 | statement(ts: "template<typename T, typename... LodOptions>" ); |
6138 | statement(ts: "inline vec<T, 4> spvChromaReconstructLinear420XCositedEvenYMidpoint(texture2d<T> plane0, " |
6139 | "texture2d<T> plane1, texture2d<T> plane2, sampler samp, float2 coord, LodOptions... options)" ); |
6140 | begin_scope(); |
6141 | statement(ts: "vec<T, 4> ycbcr = vec<T, 4>(0, 0, 0, 1);" ); |
6142 | statement(ts: "ycbcr.g = plane0.sample(samp, coord, spvForward<LodOptions>(options)...).r;" ); |
6143 | statement(ts: "float2 ab = fract((round(coord * float2(plane0.get_width(), plane0.get_height())) - float2(0, " |
6144 | "0.5)) * 0.5);" ); |
6145 | statement(ts: "ycbcr.b = T(mix(mix(plane1.sample(samp, coord, spvForward<LodOptions>(options)...), " |
6146 | "plane1.sample(samp, coord, spvForward<LodOptions>(options)..., int2(1, 0)), ab.x), " |
6147 | "mix(plane1.sample(samp, coord, spvForward<LodOptions>(options)..., int2(0, 1)), " |
6148 | "plane1.sample(samp, coord, spvForward<LodOptions>(options)..., int2(1, 1)), ab.x), ab.y).r);" ); |
6149 | statement(ts: "ycbcr.r = T(mix(mix(plane2.sample(samp, coord, spvForward<LodOptions>(options)...), " |
6150 | "plane2.sample(samp, coord, spvForward<LodOptions>(options)..., int2(1, 0)), ab.x), " |
6151 | "mix(plane2.sample(samp, coord, spvForward<LodOptions>(options)..., int2(0, 1)), " |
6152 | "plane2.sample(samp, coord, spvForward<LodOptions>(options)..., int2(1, 1)), ab.x), ab.y).r);" ); |
6153 | statement(ts: "return ycbcr;" ); |
6154 | end_scope(); |
6155 | statement(ts: "" ); |
6156 | break; |
6157 | |
6158 | case SPVFuncImplChromaReconstructLinear420XMidpointYMidpoint2Plane: |
6159 | statement(ts: "template<typename T, typename... LodOptions>" ); |
6160 | statement(ts: "inline vec<T, 4> spvChromaReconstructLinear420XMidpointYMidpoint(texture2d<T> plane0, " |
6161 | "texture2d<T> plane1, sampler samp, float2 coord, LodOptions... options)" ); |
6162 | begin_scope(); |
6163 | statement(ts: "vec<T, 4> ycbcr = vec<T, 4>(0, 0, 0, 1);" ); |
6164 | statement(ts: "ycbcr.g = plane0.sample(samp, coord, spvForward<LodOptions>(options)...).r;" ); |
6165 | statement(ts: "float2 ab = fract((round(coord * float2(plane0.get_width(), plane0.get_height())) - float2(0.5, " |
6166 | "0.5)) * 0.5);" ); |
6167 | statement(ts: "ycbcr.br = vec<T, 2>(mix(mix(plane1.sample(samp, coord, spvForward<LodOptions>(options)...), " |
6168 | "plane1.sample(samp, coord, spvForward<LodOptions>(options)..., int2(1, 0)), ab.x), " |
6169 | "mix(plane1.sample(samp, coord, spvForward<LodOptions>(options)..., int2(0, 1)), " |
6170 | "plane1.sample(samp, coord, spvForward<LodOptions>(options)..., int2(1, 1)), ab.x), ab.y).rg);" ); |
6171 | statement(ts: "return ycbcr;" ); |
6172 | end_scope(); |
6173 | statement(ts: "" ); |
6174 | break; |
6175 | |
6176 | case SPVFuncImplChromaReconstructLinear420XMidpointYMidpoint3Plane: |
6177 | statement(ts: "template<typename T, typename... LodOptions>" ); |
6178 | statement(ts: "inline vec<T, 4> spvChromaReconstructLinear420XMidpointYMidpoint(texture2d<T> plane0, " |
6179 | "texture2d<T> plane1, texture2d<T> plane2, sampler samp, float2 coord, LodOptions... options)" ); |
6180 | begin_scope(); |
6181 | statement(ts: "vec<T, 4> ycbcr = vec<T, 4>(0, 0, 0, 1);" ); |
6182 | statement(ts: "ycbcr.g = plane0.sample(samp, coord, spvForward<LodOptions>(options)...).r;" ); |
6183 | statement(ts: "float2 ab = fract((round(coord * float2(plane0.get_width(), plane0.get_height())) - float2(0.5, " |
6184 | "0.5)) * 0.5);" ); |
6185 | statement(ts: "ycbcr.b = T(mix(mix(plane1.sample(samp, coord, spvForward<LodOptions>(options)...), " |
6186 | "plane1.sample(samp, coord, spvForward<LodOptions>(options)..., int2(1, 0)), ab.x), " |
6187 | "mix(plane1.sample(samp, coord, spvForward<LodOptions>(options)..., int2(0, 1)), " |
6188 | "plane1.sample(samp, coord, spvForward<LodOptions>(options)..., int2(1, 1)), ab.x), ab.y).r);" ); |
6189 | statement(ts: "ycbcr.r = T(mix(mix(plane2.sample(samp, coord, spvForward<LodOptions>(options)...), " |
6190 | "plane2.sample(samp, coord, spvForward<LodOptions>(options)..., int2(1, 0)), ab.x), " |
6191 | "mix(plane2.sample(samp, coord, spvForward<LodOptions>(options)..., int2(0, 1)), " |
6192 | "plane2.sample(samp, coord, spvForward<LodOptions>(options)..., int2(1, 1)), ab.x), ab.y).r);" ); |
6193 | statement(ts: "return ycbcr;" ); |
6194 | end_scope(); |
6195 | statement(ts: "" ); |
6196 | break; |
6197 | |
6198 | case SPVFuncImplExpandITUFullRange: |
6199 | statement(ts: "template<typename T>" ); |
6200 | statement(ts: "inline vec<T, 4> spvExpandITUFullRange(vec<T, 4> ycbcr, int n)" ); |
6201 | begin_scope(); |
6202 | statement(ts: "ycbcr.br -= exp2(T(n-1))/(exp2(T(n))-1);" ); |
6203 | statement(ts: "return ycbcr;" ); |
6204 | end_scope(); |
6205 | statement(ts: "" ); |
6206 | break; |
6207 | |
6208 | case SPVFuncImplExpandITUNarrowRange: |
6209 | statement(ts: "template<typename T>" ); |
6210 | statement(ts: "inline vec<T, 4> spvExpandITUNarrowRange(vec<T, 4> ycbcr, int n)" ); |
6211 | begin_scope(); |
6212 | statement(ts: "ycbcr.g = (ycbcr.g * (exp2(T(n)) - 1) - ldexp(T(16), n - 8))/ldexp(T(219), n - 8);" ); |
6213 | statement(ts: "ycbcr.br = (ycbcr.br * (exp2(T(n)) - 1) - ldexp(T(128), n - 8))/ldexp(T(224), n - 8);" ); |
6214 | statement(ts: "return ycbcr;" ); |
6215 | end_scope(); |
6216 | statement(ts: "" ); |
6217 | break; |
6218 | |
6219 | case SPVFuncImplConvertYCbCrBT709: |
6220 | statement(ts: "// cf. Khronos Data Format Specification, section 15.1.1" ); |
6221 | statement(ts: "constant float3x3 spvBT709Factors = {{1, 1, 1}, {0, -0.13397432/0.7152, 1.8556}, {1.5748, " |
6222 | "-0.33480248/0.7152, 0}};" ); |
6223 | statement(ts: "" ); |
6224 | statement(ts: "template<typename T>" ); |
6225 | statement(ts: "inline vec<T, 4> spvConvertYCbCrBT709(vec<T, 4> ycbcr)" ); |
6226 | begin_scope(); |
6227 | statement(ts: "vec<T, 4> rgba;" ); |
6228 | statement(ts: "rgba.rgb = vec<T, 3>(spvBT709Factors * ycbcr.gbr);" ); |
6229 | statement(ts: "rgba.a = ycbcr.a;" ); |
6230 | statement(ts: "return rgba;" ); |
6231 | end_scope(); |
6232 | statement(ts: "" ); |
6233 | break; |
6234 | |
6235 | case SPVFuncImplConvertYCbCrBT601: |
6236 | statement(ts: "// cf. Khronos Data Format Specification, section 15.1.2" ); |
6237 | statement(ts: "constant float3x3 spvBT601Factors = {{1, 1, 1}, {0, -0.202008/0.587, 1.772}, {1.402, " |
6238 | "-0.419198/0.587, 0}};" ); |
6239 | statement(ts: "" ); |
6240 | statement(ts: "template<typename T>" ); |
6241 | statement(ts: "inline vec<T, 4> spvConvertYCbCrBT601(vec<T, 4> ycbcr)" ); |
6242 | begin_scope(); |
6243 | statement(ts: "vec<T, 4> rgba;" ); |
6244 | statement(ts: "rgba.rgb = vec<T, 3>(spvBT601Factors * ycbcr.gbr);" ); |
6245 | statement(ts: "rgba.a = ycbcr.a;" ); |
6246 | statement(ts: "return rgba;" ); |
6247 | end_scope(); |
6248 | statement(ts: "" ); |
6249 | break; |
6250 | |
6251 | case SPVFuncImplConvertYCbCrBT2020: |
6252 | statement(ts: "// cf. Khronos Data Format Specification, section 15.1.3" ); |
6253 | statement(ts: "constant float3x3 spvBT2020Factors = {{1, 1, 1}, {0, -0.11156702/0.6780, 1.8814}, {1.4746, " |
6254 | "-0.38737742/0.6780, 0}};" ); |
6255 | statement(ts: "" ); |
6256 | statement(ts: "template<typename T>" ); |
6257 | statement(ts: "inline vec<T, 4> spvConvertYCbCrBT2020(vec<T, 4> ycbcr)" ); |
6258 | begin_scope(); |
6259 | statement(ts: "vec<T, 4> rgba;" ); |
6260 | statement(ts: "rgba.rgb = vec<T, 3>(spvBT2020Factors * ycbcr.gbr);" ); |
6261 | statement(ts: "rgba.a = ycbcr.a;" ); |
6262 | statement(ts: "return rgba;" ); |
6263 | end_scope(); |
6264 | statement(ts: "" ); |
6265 | break; |
6266 | |
6267 | case SPVFuncImplDynamicImageSampler: |
6268 | statement(ts: "enum class spvFormatResolution" ); |
6269 | begin_scope(); |
6270 | statement(ts: "_444 = 0," ); |
6271 | statement(ts: "_422," ); |
6272 | statement(ts: "_420" ); |
6273 | end_scope_decl(); |
6274 | statement(ts: "" ); |
6275 | statement(ts: "enum class spvChromaFilter" ); |
6276 | begin_scope(); |
6277 | statement(ts: "nearest = 0," ); |
6278 | statement(ts: "linear" ); |
6279 | end_scope_decl(); |
6280 | statement(ts: "" ); |
6281 | statement(ts: "enum class spvXChromaLocation" ); |
6282 | begin_scope(); |
6283 | statement(ts: "cosited_even = 0," ); |
6284 | statement(ts: "midpoint" ); |
6285 | end_scope_decl(); |
6286 | statement(ts: "" ); |
6287 | statement(ts: "enum class spvYChromaLocation" ); |
6288 | begin_scope(); |
6289 | statement(ts: "cosited_even = 0," ); |
6290 | statement(ts: "midpoint" ); |
6291 | end_scope_decl(); |
6292 | statement(ts: "" ); |
6293 | statement(ts: "enum class spvYCbCrModelConversion" ); |
6294 | begin_scope(); |
6295 | statement(ts: "rgb_identity = 0," ); |
6296 | statement(ts: "ycbcr_identity," ); |
6297 | statement(ts: "ycbcr_bt_709," ); |
6298 | statement(ts: "ycbcr_bt_601," ); |
6299 | statement(ts: "ycbcr_bt_2020" ); |
6300 | end_scope_decl(); |
6301 | statement(ts: "" ); |
6302 | statement(ts: "enum class spvYCbCrRange" ); |
6303 | begin_scope(); |
6304 | statement(ts: "itu_full = 0," ); |
6305 | statement(ts: "itu_narrow" ); |
6306 | end_scope_decl(); |
6307 | statement(ts: "" ); |
6308 | statement(ts: "struct spvComponentBits" ); |
6309 | begin_scope(); |
6310 | statement(ts: "constexpr explicit spvComponentBits(int v) thread : value(v) {}" ); |
6311 | statement(ts: "uchar value : 6;" ); |
6312 | end_scope_decl(); |
6313 | statement(ts: "// A class corresponding to metal::sampler which holds sampler" ); |
6314 | statement(ts: "// Y'CbCr conversion info." ); |
6315 | statement(ts: "struct spvYCbCrSampler" ); |
6316 | begin_scope(); |
6317 | statement(ts: "constexpr spvYCbCrSampler() thread : val(build()) {}" ); |
6318 | statement(ts: "template<typename... Ts>" ); |
6319 | statement(ts: "constexpr spvYCbCrSampler(Ts... t) thread : val(build(t...)) {}" ); |
6320 | statement(ts: "constexpr spvYCbCrSampler(const thread spvYCbCrSampler& s) thread = default;" ); |
6321 | statement(ts: "" ); |
6322 | statement(ts: "spvFormatResolution get_resolution() const thread" ); |
6323 | begin_scope(); |
6324 | statement(ts: "return spvFormatResolution((val & resolution_mask) >> resolution_base);" ); |
6325 | end_scope(); |
6326 | statement(ts: "spvChromaFilter get_chroma_filter() const thread" ); |
6327 | begin_scope(); |
6328 | statement(ts: "return spvChromaFilter((val & chroma_filter_mask) >> chroma_filter_base);" ); |
6329 | end_scope(); |
6330 | statement(ts: "spvXChromaLocation get_x_chroma_offset() const thread" ); |
6331 | begin_scope(); |
6332 | statement(ts: "return spvXChromaLocation((val & x_chroma_off_mask) >> x_chroma_off_base);" ); |
6333 | end_scope(); |
6334 | statement(ts: "spvYChromaLocation get_y_chroma_offset() const thread" ); |
6335 | begin_scope(); |
6336 | statement(ts: "return spvYChromaLocation((val & y_chroma_off_mask) >> y_chroma_off_base);" ); |
6337 | end_scope(); |
6338 | statement(ts: "spvYCbCrModelConversion get_ycbcr_model() const thread" ); |
6339 | begin_scope(); |
6340 | statement(ts: "return spvYCbCrModelConversion((val & ycbcr_model_mask) >> ycbcr_model_base);" ); |
6341 | end_scope(); |
6342 | statement(ts: "spvYCbCrRange get_ycbcr_range() const thread" ); |
6343 | begin_scope(); |
6344 | statement(ts: "return spvYCbCrRange((val & ycbcr_range_mask) >> ycbcr_range_base);" ); |
6345 | end_scope(); |
6346 | statement(ts: "int get_bpc() const thread { return (val & bpc_mask) >> bpc_base; }" ); |
6347 | statement(ts: "" ); |
6348 | statement(ts: "private:" ); |
6349 | statement(ts: "ushort val;" ); |
6350 | statement(ts: "" ); |
6351 | statement(ts: "constexpr static constant ushort resolution_bits = 2;" ); |
6352 | statement(ts: "constexpr static constant ushort chroma_filter_bits = 2;" ); |
6353 | statement(ts: "constexpr static constant ushort x_chroma_off_bit = 1;" ); |
6354 | statement(ts: "constexpr static constant ushort y_chroma_off_bit = 1;" ); |
6355 | statement(ts: "constexpr static constant ushort ycbcr_model_bits = 3;" ); |
6356 | statement(ts: "constexpr static constant ushort ycbcr_range_bit = 1;" ); |
6357 | statement(ts: "constexpr static constant ushort bpc_bits = 6;" ); |
6358 | statement(ts: "" ); |
6359 | statement(ts: "constexpr static constant ushort resolution_base = 0;" ); |
6360 | statement(ts: "constexpr static constant ushort chroma_filter_base = 2;" ); |
6361 | statement(ts: "constexpr static constant ushort x_chroma_off_base = 4;" ); |
6362 | statement(ts: "constexpr static constant ushort y_chroma_off_base = 5;" ); |
6363 | statement(ts: "constexpr static constant ushort ycbcr_model_base = 6;" ); |
6364 | statement(ts: "constexpr static constant ushort ycbcr_range_base = 9;" ); |
6365 | statement(ts: "constexpr static constant ushort bpc_base = 10;" ); |
6366 | statement(ts: "" ); |
6367 | statement( |
6368 | ts: "constexpr static constant ushort resolution_mask = ((1 << resolution_bits) - 1) << resolution_base;" ); |
6369 | statement(ts: "constexpr static constant ushort chroma_filter_mask = ((1 << chroma_filter_bits) - 1) << " |
6370 | "chroma_filter_base;" ); |
6371 | statement(ts: "constexpr static constant ushort x_chroma_off_mask = ((1 << x_chroma_off_bit) - 1) << " |
6372 | "x_chroma_off_base;" ); |
6373 | statement(ts: "constexpr static constant ushort y_chroma_off_mask = ((1 << y_chroma_off_bit) - 1) << " |
6374 | "y_chroma_off_base;" ); |
6375 | statement(ts: "constexpr static constant ushort ycbcr_model_mask = ((1 << ycbcr_model_bits) - 1) << " |
6376 | "ycbcr_model_base;" ); |
6377 | statement(ts: "constexpr static constant ushort ycbcr_range_mask = ((1 << ycbcr_range_bit) - 1) << " |
6378 | "ycbcr_range_base;" ); |
6379 | statement(ts: "constexpr static constant ushort bpc_mask = ((1 << bpc_bits) - 1) << bpc_base;" ); |
6380 | statement(ts: "" ); |
6381 | statement(ts: "static constexpr ushort build()" ); |
6382 | begin_scope(); |
6383 | statement(ts: "return 0;" ); |
6384 | end_scope(); |
6385 | statement(ts: "" ); |
6386 | statement(ts: "template<typename... Ts>" ); |
6387 | statement(ts: "static constexpr ushort build(spvFormatResolution res, Ts... t)" ); |
6388 | begin_scope(); |
6389 | statement(ts: "return (ushort(res) << resolution_base) | (build(t...) & ~resolution_mask);" ); |
6390 | end_scope(); |
6391 | statement(ts: "" ); |
6392 | statement(ts: "template<typename... Ts>" ); |
6393 | statement(ts: "static constexpr ushort build(spvChromaFilter filt, Ts... t)" ); |
6394 | begin_scope(); |
6395 | statement(ts: "return (ushort(filt) << chroma_filter_base) | (build(t...) & ~chroma_filter_mask);" ); |
6396 | end_scope(); |
6397 | statement(ts: "" ); |
6398 | statement(ts: "template<typename... Ts>" ); |
6399 | statement(ts: "static constexpr ushort build(spvXChromaLocation loc, Ts... t)" ); |
6400 | begin_scope(); |
6401 | statement(ts: "return (ushort(loc) << x_chroma_off_base) | (build(t...) & ~x_chroma_off_mask);" ); |
6402 | end_scope(); |
6403 | statement(ts: "" ); |
6404 | statement(ts: "template<typename... Ts>" ); |
6405 | statement(ts: "static constexpr ushort build(spvYChromaLocation loc, Ts... t)" ); |
6406 | begin_scope(); |
6407 | statement(ts: "return (ushort(loc) << y_chroma_off_base) | (build(t...) & ~y_chroma_off_mask);" ); |
6408 | end_scope(); |
6409 | statement(ts: "" ); |
6410 | statement(ts: "template<typename... Ts>" ); |
6411 | statement(ts: "static constexpr ushort build(spvYCbCrModelConversion model, Ts... t)" ); |
6412 | begin_scope(); |
6413 | statement(ts: "return (ushort(model) << ycbcr_model_base) | (build(t...) & ~ycbcr_model_mask);" ); |
6414 | end_scope(); |
6415 | statement(ts: "" ); |
6416 | statement(ts: "template<typename... Ts>" ); |
6417 | statement(ts: "static constexpr ushort build(spvYCbCrRange range, Ts... t)" ); |
6418 | begin_scope(); |
6419 | statement(ts: "return (ushort(range) << ycbcr_range_base) | (build(t...) & ~ycbcr_range_mask);" ); |
6420 | end_scope(); |
6421 | statement(ts: "" ); |
6422 | statement(ts: "template<typename... Ts>" ); |
6423 | statement(ts: "static constexpr ushort build(spvComponentBits bpc, Ts... t)" ); |
6424 | begin_scope(); |
6425 | statement(ts: "return (ushort(bpc.value) << bpc_base) | (build(t...) & ~bpc_mask);" ); |
6426 | end_scope(); |
6427 | end_scope_decl(); |
6428 | statement(ts: "" ); |
6429 | statement(ts: "// A class which can hold up to three textures and a sampler, including" ); |
6430 | statement(ts: "// Y'CbCr conversion info, used to pass combined image-samplers" ); |
6431 | statement(ts: "// dynamically to functions." ); |
6432 | statement(ts: "template<typename T>" ); |
6433 | statement(ts: "struct spvDynamicImageSampler" ); |
6434 | begin_scope(); |
6435 | statement(ts: "texture2d<T> plane0;" ); |
6436 | statement(ts: "texture2d<T> plane1;" ); |
6437 | statement(ts: "texture2d<T> plane2;" ); |
6438 | statement(ts: "sampler samp;" ); |
6439 | statement(ts: "spvYCbCrSampler ycbcr_samp;" ); |
6440 | statement(ts: "uint swizzle = 0;" ); |
6441 | statement(ts: "" ); |
6442 | if (msl_options.swizzle_texture_samples) |
6443 | { |
6444 | statement(ts: "constexpr spvDynamicImageSampler(texture2d<T> tex, sampler samp, uint sw) thread :" ); |
6445 | statement(ts: " plane0(tex), samp(samp), swizzle(sw) {}" ); |
6446 | } |
6447 | else |
6448 | { |
6449 | statement(ts: "constexpr spvDynamicImageSampler(texture2d<T> tex, sampler samp) thread :" ); |
6450 | statement(ts: " plane0(tex), samp(samp) {}" ); |
6451 | } |
6452 | statement(ts: "constexpr spvDynamicImageSampler(texture2d<T> tex, sampler samp, spvYCbCrSampler ycbcr_samp, " |
6453 | "uint sw) thread :" ); |
6454 | statement(ts: " plane0(tex), samp(samp), ycbcr_samp(ycbcr_samp), swizzle(sw) {}" ); |
6455 | statement(ts: "constexpr spvDynamicImageSampler(texture2d<T> plane0, texture2d<T> plane1," ); |
6456 | statement(ts: " sampler samp, spvYCbCrSampler ycbcr_samp, uint sw) thread :" ); |
6457 | statement(ts: " plane0(plane0), plane1(plane1), samp(samp), ycbcr_samp(ycbcr_samp), swizzle(sw) {}" ); |
6458 | statement( |
6459 | ts: "constexpr spvDynamicImageSampler(texture2d<T> plane0, texture2d<T> plane1, texture2d<T> plane2," ); |
6460 | statement(ts: " sampler samp, spvYCbCrSampler ycbcr_samp, uint sw) thread :" ); |
6461 | statement(ts: " plane0(plane0), plane1(plane1), plane2(plane2), samp(samp), ycbcr_samp(ycbcr_samp), " |
6462 | "swizzle(sw) {}" ); |
6463 | statement(ts: "" ); |
6464 | // XXX This is really hard to follow... I've left comments to make it a bit easier. |
6465 | statement(ts: "template<typename... LodOptions>" ); |
6466 | statement(ts: "vec<T, 4> do_sample(float2 coord, LodOptions... options) const thread" ); |
6467 | begin_scope(); |
6468 | statement(ts: "if (!is_null_texture(plane1))" ); |
6469 | begin_scope(); |
6470 | statement(ts: "if (ycbcr_samp.get_resolution() == spvFormatResolution::_444 ||" ); |
6471 | statement(ts: " ycbcr_samp.get_chroma_filter() == spvChromaFilter::nearest)" ); |
6472 | begin_scope(); |
6473 | statement(ts: "if (!is_null_texture(plane2))" ); |
6474 | statement(ts: " return spvChromaReconstructNearest(plane0, plane1, plane2, samp, coord," ); |
6475 | statement(ts: " spvForward<LodOptions>(options)...);" ); |
6476 | statement( |
6477 | ts: "return spvChromaReconstructNearest(plane0, plane1, samp, coord, spvForward<LodOptions>(options)...);" ); |
6478 | end_scope(); // if (resolution == 422 || chroma_filter == nearest) |
6479 | statement(ts: "switch (ycbcr_samp.get_resolution())" ); |
6480 | begin_scope(); |
6481 | statement(ts: "case spvFormatResolution::_444: break;" ); |
6482 | statement(ts: "case spvFormatResolution::_422:" ); |
6483 | begin_scope(); |
6484 | statement(ts: "switch (ycbcr_samp.get_x_chroma_offset())" ); |
6485 | begin_scope(); |
6486 | statement(ts: "case spvXChromaLocation::cosited_even:" ); |
6487 | statement(ts: " if (!is_null_texture(plane2))" ); |
6488 | statement(ts: " return spvChromaReconstructLinear422CositedEven(" ); |
6489 | statement(ts: " plane0, plane1, plane2, samp," ); |
6490 | statement(ts: " coord, spvForward<LodOptions>(options)...);" ); |
6491 | statement(ts: " return spvChromaReconstructLinear422CositedEven(" ); |
6492 | statement(ts: " plane0, plane1, samp, coord," ); |
6493 | statement(ts: " spvForward<LodOptions>(options)...);" ); |
6494 | statement(ts: "case spvXChromaLocation::midpoint:" ); |
6495 | statement(ts: " if (!is_null_texture(plane2))" ); |
6496 | statement(ts: " return spvChromaReconstructLinear422Midpoint(" ); |
6497 | statement(ts: " plane0, plane1, plane2, samp," ); |
6498 | statement(ts: " coord, spvForward<LodOptions>(options)...);" ); |
6499 | statement(ts: " return spvChromaReconstructLinear422Midpoint(" ); |
6500 | statement(ts: " plane0, plane1, samp, coord," ); |
6501 | statement(ts: " spvForward<LodOptions>(options)...);" ); |
6502 | end_scope(); // switch (x_chroma_offset) |
6503 | end_scope(); // case 422: |
6504 | statement(ts: "case spvFormatResolution::_420:" ); |
6505 | begin_scope(); |
6506 | statement(ts: "switch (ycbcr_samp.get_x_chroma_offset())" ); |
6507 | begin_scope(); |
6508 | statement(ts: "case spvXChromaLocation::cosited_even:" ); |
6509 | begin_scope(); |
6510 | statement(ts: "switch (ycbcr_samp.get_y_chroma_offset())" ); |
6511 | begin_scope(); |
6512 | statement(ts: "case spvYChromaLocation::cosited_even:" ); |
6513 | statement(ts: " if (!is_null_texture(plane2))" ); |
6514 | statement(ts: " return spvChromaReconstructLinear420XCositedEvenYCositedEven(" ); |
6515 | statement(ts: " plane0, plane1, plane2, samp," ); |
6516 | statement(ts: " coord, spvForward<LodOptions>(options)...);" ); |
6517 | statement(ts: " return spvChromaReconstructLinear420XCositedEvenYCositedEven(" ); |
6518 | statement(ts: " plane0, plane1, samp, coord," ); |
6519 | statement(ts: " spvForward<LodOptions>(options)...);" ); |
6520 | statement(ts: "case spvYChromaLocation::midpoint:" ); |
6521 | statement(ts: " if (!is_null_texture(plane2))" ); |
6522 | statement(ts: " return spvChromaReconstructLinear420XCositedEvenYMidpoint(" ); |
6523 | statement(ts: " plane0, plane1, plane2, samp," ); |
6524 | statement(ts: " coord, spvForward<LodOptions>(options)...);" ); |
6525 | statement(ts: " return spvChromaReconstructLinear420XCositedEvenYMidpoint(" ); |
6526 | statement(ts: " plane0, plane1, samp, coord," ); |
6527 | statement(ts: " spvForward<LodOptions>(options)...);" ); |
6528 | end_scope(); // switch (y_chroma_offset) |
6529 | end_scope(); // case x::cosited_even: |
6530 | statement(ts: "case spvXChromaLocation::midpoint:" ); |
6531 | begin_scope(); |
6532 | statement(ts: "switch (ycbcr_samp.get_y_chroma_offset())" ); |
6533 | begin_scope(); |
6534 | statement(ts: "case spvYChromaLocation::cosited_even:" ); |
6535 | statement(ts: " if (!is_null_texture(plane2))" ); |
6536 | statement(ts: " return spvChromaReconstructLinear420XMidpointYCositedEven(" ); |
6537 | statement(ts: " plane0, plane1, plane2, samp," ); |
6538 | statement(ts: " coord, spvForward<LodOptions>(options)...);" ); |
6539 | statement(ts: " return spvChromaReconstructLinear420XMidpointYCositedEven(" ); |
6540 | statement(ts: " plane0, plane1, samp, coord," ); |
6541 | statement(ts: " spvForward<LodOptions>(options)...);" ); |
6542 | statement(ts: "case spvYChromaLocation::midpoint:" ); |
6543 | statement(ts: " if (!is_null_texture(plane2))" ); |
6544 | statement(ts: " return spvChromaReconstructLinear420XMidpointYMidpoint(" ); |
6545 | statement(ts: " plane0, plane1, plane2, samp," ); |
6546 | statement(ts: " coord, spvForward<LodOptions>(options)...);" ); |
6547 | statement(ts: " return spvChromaReconstructLinear420XMidpointYMidpoint(" ); |
6548 | statement(ts: " plane0, plane1, samp, coord," ); |
6549 | statement(ts: " spvForward<LodOptions>(options)...);" ); |
6550 | end_scope(); // switch (y_chroma_offset) |
6551 | end_scope(); // case x::midpoint |
6552 | end_scope(); // switch (x_chroma_offset) |
6553 | end_scope(); // case 420: |
6554 | end_scope(); // switch (resolution) |
6555 | end_scope(); // if (multiplanar) |
6556 | statement(ts: "return plane0.sample(samp, coord, spvForward<LodOptions>(options)...);" ); |
6557 | end_scope(); // do_sample() |
6558 | statement(ts: "template <typename... LodOptions>" ); |
6559 | statement(ts: "vec<T, 4> sample(float2 coord, LodOptions... options) const thread" ); |
6560 | begin_scope(); |
6561 | statement( |
6562 | ts: "vec<T, 4> s = spvTextureSwizzle(do_sample(coord, spvForward<LodOptions>(options)...), swizzle);" ); |
6563 | statement(ts: "if (ycbcr_samp.get_ycbcr_model() == spvYCbCrModelConversion::rgb_identity)" ); |
6564 | statement(ts: " return s;" ); |
6565 | statement(ts: "" ); |
6566 | statement(ts: "switch (ycbcr_samp.get_ycbcr_range())" ); |
6567 | begin_scope(); |
6568 | statement(ts: "case spvYCbCrRange::itu_full:" ); |
6569 | statement(ts: " s = spvExpandITUFullRange(s, ycbcr_samp.get_bpc());" ); |
6570 | statement(ts: " break;" ); |
6571 | statement(ts: "case spvYCbCrRange::itu_narrow:" ); |
6572 | statement(ts: " s = spvExpandITUNarrowRange(s, ycbcr_samp.get_bpc());" ); |
6573 | statement(ts: " break;" ); |
6574 | end_scope(); |
6575 | statement(ts: "" ); |
6576 | statement(ts: "switch (ycbcr_samp.get_ycbcr_model())" ); |
6577 | begin_scope(); |
6578 | statement(ts: "case spvYCbCrModelConversion::rgb_identity:" ); // Silence Clang warning |
6579 | statement(ts: "case spvYCbCrModelConversion::ycbcr_identity:" ); |
6580 | statement(ts: " return s;" ); |
6581 | statement(ts: "case spvYCbCrModelConversion::ycbcr_bt_709:" ); |
6582 | statement(ts: " return spvConvertYCbCrBT709(s);" ); |
6583 | statement(ts: "case spvYCbCrModelConversion::ycbcr_bt_601:" ); |
6584 | statement(ts: " return spvConvertYCbCrBT601(s);" ); |
6585 | statement(ts: "case spvYCbCrModelConversion::ycbcr_bt_2020:" ); |
6586 | statement(ts: " return spvConvertYCbCrBT2020(s);" ); |
6587 | end_scope(); |
6588 | end_scope(); |
6589 | statement(ts: "" ); |
6590 | // Sampler Y'CbCr conversion forbids offsets. |
6591 | statement(ts: "vec<T, 4> sample(float2 coord, int2 offset) const thread" ); |
6592 | begin_scope(); |
6593 | if (msl_options.swizzle_texture_samples) |
6594 | statement(ts: "return spvTextureSwizzle(plane0.sample(samp, coord, offset), swizzle);" ); |
6595 | else |
6596 | statement(ts: "return plane0.sample(samp, coord, offset);" ); |
6597 | end_scope(); |
6598 | statement(ts: "template<typename lod_options>" ); |
6599 | statement(ts: "vec<T, 4> sample(float2 coord, lod_options options, int2 offset) const thread" ); |
6600 | begin_scope(); |
6601 | if (msl_options.swizzle_texture_samples) |
6602 | statement(ts: "return spvTextureSwizzle(plane0.sample(samp, coord, options, offset), swizzle);" ); |
6603 | else |
6604 | statement(ts: "return plane0.sample(samp, coord, options, offset);" ); |
6605 | end_scope(); |
6606 | statement(ts: "#if __HAVE_MIN_LOD_CLAMP__" ); |
6607 | statement(ts: "vec<T, 4> sample(float2 coord, bias b, min_lod_clamp min_lod, int2 offset) const thread" ); |
6608 | begin_scope(); |
6609 | statement(ts: "return plane0.sample(samp, coord, b, min_lod, offset);" ); |
6610 | end_scope(); |
6611 | statement( |
6612 | ts: "vec<T, 4> sample(float2 coord, gradient2d grad, min_lod_clamp min_lod, int2 offset) const thread" ); |
6613 | begin_scope(); |
6614 | statement(ts: "return plane0.sample(samp, coord, grad, min_lod, offset);" ); |
6615 | end_scope(); |
6616 | statement(ts: "#endif" ); |
6617 | statement(ts: "" ); |
6618 | // Y'CbCr conversion forbids all operations but sampling. |
6619 | statement(ts: "vec<T, 4> read(uint2 coord, uint lod = 0) const thread" ); |
6620 | begin_scope(); |
6621 | statement(ts: "return plane0.read(coord, lod);" ); |
6622 | end_scope(); |
6623 | statement(ts: "" ); |
6624 | statement(ts: "vec<T, 4> gather(float2 coord, int2 offset = int2(0), component c = component::x) const thread" ); |
6625 | begin_scope(); |
6626 | if (msl_options.swizzle_texture_samples) |
6627 | statement(ts: "return spvGatherSwizzle(plane0, samp, swizzle, c, coord, offset);" ); |
6628 | else |
6629 | statement(ts: "return plane0.gather(samp, coord, offset, c);" ); |
6630 | end_scope(); |
6631 | end_scope_decl(); |
6632 | statement(ts: "" ); |
6633 | |
6634 | default: |
6635 | break; |
6636 | } |
6637 | } |
6638 | } |
6639 | |
6640 | static string inject_top_level_storage_qualifier(const string &expr, const string &qualifier) |
6641 | { |
6642 | // Easier to do this through text munging since the qualifier does not exist in the type system at all, |
6643 | // and plumbing in all that information is not very helpful. |
6644 | size_t last_reference = expr.find_last_of(c: '&'); |
6645 | size_t last_pointer = expr.find_last_of(c: '*'); |
6646 | size_t last_significant = string::npos; |
6647 | |
6648 | if (last_reference == string::npos) |
6649 | last_significant = last_pointer; |
6650 | else if (last_pointer == string::npos) |
6651 | last_significant = last_reference; |
6652 | else |
6653 | last_significant = std::max(a: last_reference, b: last_pointer); |
6654 | |
6655 | if (last_significant == string::npos) |
6656 | return join(ts: qualifier, ts: " " , ts: expr); |
6657 | else |
6658 | { |
6659 | return join(ts: expr.substr(pos: 0, n: last_significant + 1), ts: " " , |
6660 | ts: qualifier, ts: expr.substr(pos: last_significant + 1, n: string::npos)); |
6661 | } |
6662 | } |
6663 | |
6664 | // Undefined global memory is not allowed in MSL. |
6665 | // Declare constant and init to zeros. Use {}, as global constructors can break Metal. |
6666 | void CompilerMSL::declare_undefined_values() |
6667 | { |
6668 | bool emitted = false; |
6669 | ir.for_each_typed_id<SPIRUndef>(op: [&](uint32_t, SPIRUndef &undef) { |
6670 | auto &type = this->get<SPIRType>(id: undef.basetype); |
6671 | // OpUndef can be void for some reason ... |
6672 | if (type.basetype == SPIRType::Void) |
6673 | return; |
6674 | |
6675 | statement(ts: inject_top_level_storage_qualifier( |
6676 | expr: variable_decl(type, name: to_name(id: undef.self), id: undef.self), |
6677 | qualifier: "constant" ), |
6678 | ts: " = {};" ); |
6679 | emitted = true; |
6680 | }); |
6681 | |
6682 | if (emitted) |
6683 | statement(ts: "" ); |
6684 | } |
6685 | |
6686 | void CompilerMSL::declare_constant_arrays() |
6687 | { |
6688 | bool fully_inlined = ir.ids_for_type[TypeFunction].size() == 1; |
6689 | |
6690 | // MSL cannot declare arrays inline (except when declaring a variable), so we must move them out to |
6691 | // global constants directly, so we are able to use constants as variable expressions. |
6692 | bool emitted = false; |
6693 | |
6694 | ir.for_each_typed_id<SPIRConstant>(op: [&](uint32_t, SPIRConstant &c) { |
6695 | if (c.specialization) |
6696 | return; |
6697 | |
6698 | auto &type = this->get<SPIRType>(id: c.constant_type); |
6699 | // Constant arrays of non-primitive types (i.e. matrices) won't link properly into Metal libraries. |
6700 | // FIXME: However, hoisting constants to main() means we need to pass down constant arrays to leaf functions if they are used there. |
6701 | // If there are multiple functions in the module, drop this case to avoid breaking use cases which do not need to |
6702 | // link into Metal libraries. This is hacky. |
6703 | if (!type.array.empty() && (!fully_inlined || is_scalar(type) || is_vector(type))) |
6704 | { |
6705 | add_resource_name(id: c.self); |
6706 | auto name = to_name(id: c.self); |
6707 | statement(ts: inject_top_level_storage_qualifier(expr: variable_decl(type, name), qualifier: "constant" ), |
6708 | ts: " = " , ts: constant_expression(c), ts: ";" ); |
6709 | emitted = true; |
6710 | } |
6711 | }); |
6712 | |
6713 | if (emitted) |
6714 | statement(ts: "" ); |
6715 | } |
6716 | |
6717 | // Constant arrays of non-primitive types (i.e. matrices) won't link properly into Metal libraries |
6718 | void CompilerMSL::declare_complex_constant_arrays() |
6719 | { |
6720 | // If we do not have a fully inlined module, we did not opt in to |
6721 | // declaring constant arrays of complex types. See CompilerMSL::declare_constant_arrays(). |
6722 | bool fully_inlined = ir.ids_for_type[TypeFunction].size() == 1; |
6723 | if (!fully_inlined) |
6724 | return; |
6725 | |
6726 | // MSL cannot declare arrays inline (except when declaring a variable), so we must move them out to |
6727 | // global constants directly, so we are able to use constants as variable expressions. |
6728 | bool emitted = false; |
6729 | |
6730 | ir.for_each_typed_id<SPIRConstant>(op: [&](uint32_t, SPIRConstant &c) { |
6731 | if (c.specialization) |
6732 | return; |
6733 | |
6734 | auto &type = this->get<SPIRType>(id: c.constant_type); |
6735 | if (!type.array.empty() && !(is_scalar(type) || is_vector(type))) |
6736 | { |
6737 | add_resource_name(id: c.self); |
6738 | auto name = to_name(id: c.self); |
6739 | statement(ts: "" , ts: variable_decl(type, name), ts: " = " , ts: constant_expression(c), ts: ";" ); |
6740 | emitted = true; |
6741 | } |
6742 | }); |
6743 | |
6744 | if (emitted) |
6745 | statement(ts: "" ); |
6746 | } |
6747 | |
6748 | void CompilerMSL::emit_resources() |
6749 | { |
6750 | declare_constant_arrays(); |
6751 | declare_undefined_values(); |
6752 | |
6753 | // Emit the special [[stage_in]] and [[stage_out]] interface blocks which we created. |
6754 | emit_interface_block(ib_var_id: stage_out_var_id); |
6755 | emit_interface_block(ib_var_id: patch_stage_out_var_id); |
6756 | emit_interface_block(ib_var_id: stage_in_var_id); |
6757 | emit_interface_block(ib_var_id: patch_stage_in_var_id); |
6758 | } |
6759 | |
6760 | // Emit declarations for the specialization Metal function constants |
6761 | void CompilerMSL::emit_specialization_constants_and_structs() |
6762 | { |
6763 | SpecializationConstant wg_x, wg_y, wg_z; |
6764 | ID workgroup_size_id = get_work_group_size_specialization_constants(x&: wg_x, y&: wg_y, z&: wg_z); |
6765 | bool emitted = false; |
6766 | |
6767 | unordered_set<uint32_t> declared_structs; |
6768 | unordered_set<uint32_t> aligned_structs; |
6769 | |
6770 | // First, we need to deal with scalar block layout. |
6771 | // It is possible that a struct may have to be placed at an alignment which does not match the innate alignment of the struct itself. |
6772 | // In that case, if such a case exists for a struct, we must force that all elements of the struct become packed_ types. |
6773 | // This makes the struct alignment as small as physically possible. |
6774 | // When we actually align the struct later, we can insert padding as necessary to make the packed members behave like normally aligned types. |
6775 | ir.for_each_typed_id<SPIRType>(op: [&](uint32_t type_id, const SPIRType &type) { |
6776 | if (type.basetype == SPIRType::Struct && |
6777 | has_extended_decoration(id: type_id, decoration: SPIRVCrossDecorationBufferBlockRepacked)) |
6778 | mark_scalar_layout_structs(type); |
6779 | }); |
6780 | |
6781 | bool builtin_block_type_is_required = false; |
6782 | // Very special case. If gl_PerVertex is initialized as an array (tessellation) |
6783 | // we have to potentially emit the gl_PerVertex struct type so that we can emit a constant LUT. |
6784 | ir.for_each_typed_id<SPIRConstant>(op: [&](uint32_t, SPIRConstant &c) { |
6785 | auto &type = this->get<SPIRType>(id: c.constant_type); |
6786 | if (is_array(type) && has_decoration(id: type.self, decoration: DecorationBlock) && is_builtin_type(type)) |
6787 | builtin_block_type_is_required = true; |
6788 | }); |
6789 | |
6790 | // Very particular use of the soft loop lock. |
6791 | // align_struct may need to create custom types on the fly, but we don't care about |
6792 | // these types for purpose of iterating over them in ir.ids_for_type and friends. |
6793 | auto loop_lock = ir.create_loop_soft_lock(); |
6794 | |
6795 | for (auto &id_ : ir.ids_for_constant_or_type) |
6796 | { |
6797 | auto &id = ir.ids[id_]; |
6798 | |
6799 | if (id.get_type() == TypeConstant) |
6800 | { |
6801 | auto &c = id.get<SPIRConstant>(); |
6802 | |
6803 | if (c.self == workgroup_size_id) |
6804 | { |
6805 | // TODO: This can be expressed as a [[threads_per_threadgroup]] input semantic, but we need to know |
6806 | // the work group size at compile time in SPIR-V, and [[threads_per_threadgroup]] would need to be passed around as a global. |
6807 | // The work group size may be a specialization constant. |
6808 | statement(ts: "constant uint3 " , ts: builtin_to_glsl(builtin: BuiltInWorkgroupSize, storage: StorageClassWorkgroup), |
6809 | ts: " [[maybe_unused]] = " , ts: constant_expression(c: get<SPIRConstant>(id: workgroup_size_id)), ts: ";" ); |
6810 | emitted = true; |
6811 | } |
6812 | else if (c.specialization) |
6813 | { |
6814 | auto &type = get<SPIRType>(id: c.constant_type); |
6815 | string sc_type_name = type_to_glsl(type); |
6816 | add_resource_name(id: c.self); |
6817 | string sc_name = to_name(id: c.self); |
6818 | string sc_tmp_name = sc_name + "_tmp" ; |
6819 | |
6820 | // Function constants are only supported in MSL 1.2 and later. |
6821 | // If we don't support it just declare the "default" directly. |
6822 | // This "default" value can be overridden to the true specialization constant by the API user. |
6823 | // Specialization constants which are used as array length expressions cannot be function constants in MSL, |
6824 | // so just fall back to macros. |
6825 | if (msl_options.supports_msl_version(major: 1, minor: 2) && has_decoration(id: c.self, decoration: DecorationSpecId) && |
6826 | !c.is_used_as_array_length) |
6827 | { |
6828 | uint32_t constant_id = get_decoration(id: c.self, decoration: DecorationSpecId); |
6829 | // Only scalar, non-composite values can be function constants. |
6830 | statement(ts: "constant " , ts&: sc_type_name, ts: " " , ts&: sc_tmp_name, ts: " [[function_constant(" , ts&: constant_id, |
6831 | ts: ")]];" ); |
6832 | statement(ts: "constant " , ts&: sc_type_name, ts: " " , ts&: sc_name, ts: " = is_function_constant_defined(" , ts&: sc_tmp_name, |
6833 | ts: ") ? " , ts&: sc_tmp_name, ts: " : " , ts: constant_expression(c), ts: ";" ); |
6834 | } |
6835 | else if (has_decoration(id: c.self, decoration: DecorationSpecId)) |
6836 | { |
6837 | // Fallback to macro overrides. |
6838 | c.specialization_constant_macro_name = |
6839 | constant_value_macro_name(id: get_decoration(id: c.self, decoration: DecorationSpecId)); |
6840 | |
6841 | statement(ts: "#ifndef " , ts&: c.specialization_constant_macro_name); |
6842 | statement(ts: "#define " , ts&: c.specialization_constant_macro_name, ts: " " , ts: constant_expression(c)); |
6843 | statement(ts: "#endif" ); |
6844 | statement(ts: "constant " , ts&: sc_type_name, ts: " " , ts&: sc_name, ts: " = " , ts&: c.specialization_constant_macro_name, |
6845 | ts: ";" ); |
6846 | } |
6847 | else |
6848 | { |
6849 | // Composite specialization constants must be built from other specialization constants. |
6850 | statement(ts: "constant " , ts&: sc_type_name, ts: " " , ts&: sc_name, ts: " = " , ts: constant_expression(c), ts: ";" ); |
6851 | } |
6852 | emitted = true; |
6853 | } |
6854 | } |
6855 | else if (id.get_type() == TypeConstantOp) |
6856 | { |
6857 | auto &c = id.get<SPIRConstantOp>(); |
6858 | auto &type = get<SPIRType>(id: c.basetype); |
6859 | add_resource_name(id: c.self); |
6860 | auto name = to_name(id: c.self); |
6861 | statement(ts: "constant " , ts: variable_decl(type, name), ts: " = " , ts: constant_op_expression(cop: c), ts: ";" ); |
6862 | emitted = true; |
6863 | } |
6864 | else if (id.get_type() == TypeType) |
6865 | { |
6866 | // Output non-builtin interface structs. These include local function structs |
6867 | // and structs nested within uniform and read-write buffers. |
6868 | auto &type = id.get<SPIRType>(); |
6869 | TypeID type_id = type.self; |
6870 | |
6871 | bool is_struct = (type.basetype == SPIRType::Struct) && type.array.empty() && !type.pointer; |
6872 | bool is_block = |
6873 | has_decoration(id: type.self, decoration: DecorationBlock) || has_decoration(id: type.self, decoration: DecorationBufferBlock); |
6874 | |
6875 | bool is_builtin_block = is_block && is_builtin_type(type); |
6876 | bool is_declarable_struct = is_struct && (!is_builtin_block || builtin_block_type_is_required); |
6877 | |
6878 | // We'll declare this later. |
6879 | if (stage_out_var_id && get_stage_out_struct_type().self == type_id) |
6880 | is_declarable_struct = false; |
6881 | if (patch_stage_out_var_id && get_patch_stage_out_struct_type().self == type_id) |
6882 | is_declarable_struct = false; |
6883 | if (stage_in_var_id && get_stage_in_struct_type().self == type_id) |
6884 | is_declarable_struct = false; |
6885 | if (patch_stage_in_var_id && get_patch_stage_in_struct_type().self == type_id) |
6886 | is_declarable_struct = false; |
6887 | |
6888 | // Special case. Declare builtin struct anyways if we need to emit a threadgroup version of it. |
6889 | if (stage_out_masked_builtin_type_id == type_id) |
6890 | is_declarable_struct = true; |
6891 | |
6892 | // Align and emit declarable structs...but avoid declaring each more than once. |
6893 | if (is_declarable_struct && declared_structs.count(x: type_id) == 0) |
6894 | { |
6895 | if (emitted) |
6896 | statement(ts: "" ); |
6897 | emitted = false; |
6898 | |
6899 | declared_structs.insert(x: type_id); |
6900 | |
6901 | if (has_extended_decoration(id: type_id, decoration: SPIRVCrossDecorationBufferBlockRepacked)) |
6902 | align_struct(ib_type&: type, aligned_structs); |
6903 | |
6904 | // Make sure we declare the underlying struct type, and not the "decorated" type with pointers, etc. |
6905 | emit_struct(type&: get<SPIRType>(id: type_id)); |
6906 | } |
6907 | } |
6908 | } |
6909 | |
6910 | if (emitted) |
6911 | statement(ts: "" ); |
6912 | } |
6913 | |
6914 | void CompilerMSL::emit_binary_unord_op(uint32_t result_type, uint32_t result_id, uint32_t op0, uint32_t op1, |
6915 | const char *op) |
6916 | { |
6917 | bool forward = should_forward(id: op0) && should_forward(id: op1); |
6918 | emit_op(result_type, result_id, |
6919 | rhs: join(ts: "(isunordered(" , ts: to_enclosed_unpacked_expression(id: op0), ts: ", " , ts: to_enclosed_unpacked_expression(id: op1), |
6920 | ts: ") || " , ts: to_enclosed_unpacked_expression(id: op0), ts: " " , ts&: op, ts: " " , ts: to_enclosed_unpacked_expression(id: op1), |
6921 | ts: ")" ), |
6922 | forward_rhs: forward); |
6923 | |
6924 | inherit_expression_dependencies(dst: result_id, source: op0); |
6925 | inherit_expression_dependencies(dst: result_id, source: op1); |
6926 | } |
6927 | |
6928 | bool CompilerMSL::emit_tessellation_io_load(uint32_t result_type_id, uint32_t id, uint32_t ptr) |
6929 | { |
6930 | auto &ptr_type = expression_type(id: ptr); |
6931 | auto &result_type = get<SPIRType>(id: result_type_id); |
6932 | if (ptr_type.storage != StorageClassInput && ptr_type.storage != StorageClassOutput) |
6933 | return false; |
6934 | if (ptr_type.storage == StorageClassOutput && get_execution_model() == ExecutionModelTessellationEvaluation) |
6935 | return false; |
6936 | |
6937 | if (has_decoration(id: ptr, decoration: DecorationPatch)) |
6938 | return false; |
6939 | bool ptr_is_io_variable = ir.ids[ptr].get_type() == TypeVariable; |
6940 | |
6941 | bool flattened_io = variable_storage_requires_stage_io(storage: ptr_type.storage); |
6942 | |
6943 | bool flat_data_type = flattened_io && |
6944 | (is_matrix(type: result_type) || is_array(type: result_type) || result_type.basetype == SPIRType::Struct); |
6945 | |
6946 | // Edge case, even with multi-patch workgroups, we still need to unroll load |
6947 | // if we're loading control points directly. |
6948 | if (ptr_is_io_variable && is_array(type: result_type)) |
6949 | flat_data_type = true; |
6950 | |
6951 | if (!flat_data_type) |
6952 | return false; |
6953 | |
6954 | // Now, we must unflatten a composite type and take care of interleaving array access with gl_in/gl_out. |
6955 | // Lots of painful code duplication since we *really* should not unroll these kinds of loads in entry point fixup |
6956 | // unless we're forced to do this when the code is emitting inoptimal OpLoads. |
6957 | string expr; |
6958 | |
6959 | uint32_t interface_index = get_extended_decoration(id: ptr, decoration: SPIRVCrossDecorationInterfaceMemberIndex); |
6960 | auto *var = maybe_get_backing_variable(chain: ptr); |
6961 | auto &expr_type = get_pointee_type(type_id: ptr_type.self); |
6962 | |
6963 | const auto &iface_type = expression_type(id: stage_in_ptr_var_id); |
6964 | |
6965 | if (!flattened_io) |
6966 | { |
6967 | // Simplest case for multi-patch workgroups, just unroll array as-is. |
6968 | if (interface_index == uint32_t(-1)) |
6969 | return false; |
6970 | |
6971 | expr += type_to_glsl(type: result_type) + "({ " ; |
6972 | uint32_t num_control_points = to_array_size_literal(type: result_type, index: uint32_t(result_type.array.size()) - 1); |
6973 | |
6974 | for (uint32_t i = 0; i < num_control_points; i++) |
6975 | { |
6976 | const uint32_t indices[2] = { i, interface_index }; |
6977 | AccessChainMeta meta; |
6978 | expr += access_chain_internal(base: stage_in_ptr_var_id, indices, count: 2, |
6979 | flags: ACCESS_CHAIN_INDEX_IS_LITERAL_BIT | ACCESS_CHAIN_PTR_CHAIN_BIT, meta: &meta); |
6980 | if (i + 1 < num_control_points) |
6981 | expr += ", " ; |
6982 | } |
6983 | expr += " })" ; |
6984 | } |
6985 | else if (result_type.array.size() > 2) |
6986 | { |
6987 | SPIRV_CROSS_THROW("Cannot load tessellation IO variables with more than 2 dimensions." ); |
6988 | } |
6989 | else if (result_type.array.size() == 2) |
6990 | { |
6991 | if (!ptr_is_io_variable) |
6992 | SPIRV_CROSS_THROW("Loading an array-of-array must be loaded directly from an IO variable." ); |
6993 | if (interface_index == uint32_t(-1)) |
6994 | SPIRV_CROSS_THROW("Interface index is unknown. Cannot continue." ); |
6995 | if (result_type.basetype == SPIRType::Struct || is_matrix(type: result_type)) |
6996 | SPIRV_CROSS_THROW("Cannot load array-of-array of composite type in tessellation IO." ); |
6997 | |
6998 | expr += type_to_glsl(type: result_type) + "({ " ; |
6999 | uint32_t num_control_points = to_array_size_literal(type: result_type, index: 1); |
7000 | uint32_t base_interface_index = interface_index; |
7001 | |
7002 | auto &sub_type = get<SPIRType>(id: result_type.parent_type); |
7003 | |
7004 | for (uint32_t i = 0; i < num_control_points; i++) |
7005 | { |
7006 | expr += type_to_glsl(type: sub_type) + "({ " ; |
7007 | interface_index = base_interface_index; |
7008 | uint32_t array_size = to_array_size_literal(type: result_type, index: 0); |
7009 | for (uint32_t j = 0; j < array_size; j++, interface_index++) |
7010 | { |
7011 | const uint32_t indices[2] = { i, interface_index }; |
7012 | |
7013 | AccessChainMeta meta; |
7014 | expr += access_chain_internal(base: stage_in_ptr_var_id, indices, count: 2, |
7015 | flags: ACCESS_CHAIN_INDEX_IS_LITERAL_BIT | ACCESS_CHAIN_PTR_CHAIN_BIT, meta: &meta); |
7016 | if (!is_matrix(type: sub_type) && sub_type.basetype != SPIRType::Struct && |
7017 | expr_type.vecsize > sub_type.vecsize) |
7018 | expr += vector_swizzle(vecsize: sub_type.vecsize, index: 0); |
7019 | |
7020 | if (j + 1 < array_size) |
7021 | expr += ", " ; |
7022 | } |
7023 | expr += " })" ; |
7024 | if (i + 1 < num_control_points) |
7025 | expr += ", " ; |
7026 | } |
7027 | expr += " })" ; |
7028 | } |
7029 | else if (result_type.basetype == SPIRType::Struct) |
7030 | { |
7031 | bool is_array_of_struct = is_array(type: result_type); |
7032 | if (is_array_of_struct && !ptr_is_io_variable) |
7033 | SPIRV_CROSS_THROW("Loading array of struct from IO variable must come directly from IO variable." ); |
7034 | |
7035 | uint32_t num_control_points = 1; |
7036 | if (is_array_of_struct) |
7037 | { |
7038 | num_control_points = to_array_size_literal(type: result_type, index: 0); |
7039 | expr += type_to_glsl(type: result_type) + "({ " ; |
7040 | } |
7041 | |
7042 | auto &struct_type = is_array_of_struct ? get<SPIRType>(id: result_type.parent_type) : result_type; |
7043 | assert(struct_type.array.empty()); |
7044 | |
7045 | for (uint32_t i = 0; i < num_control_points; i++) |
7046 | { |
7047 | expr += type_to_glsl(type: struct_type) + "{ " ; |
7048 | for (uint32_t j = 0; j < uint32_t(struct_type.member_types.size()); j++) |
7049 | { |
7050 | // The base interface index is stored per variable for structs. |
7051 | if (var) |
7052 | { |
7053 | interface_index = |
7054 | get_extended_member_decoration(type: var->self, index: j, decoration: SPIRVCrossDecorationInterfaceMemberIndex); |
7055 | } |
7056 | |
7057 | if (interface_index == uint32_t(-1)) |
7058 | SPIRV_CROSS_THROW("Interface index is unknown. Cannot continue." ); |
7059 | |
7060 | const auto &mbr_type = get<SPIRType>(id: struct_type.member_types[j]); |
7061 | const auto &expr_mbr_type = get<SPIRType>(id: expr_type.member_types[j]); |
7062 | if (is_matrix(type: mbr_type) && ptr_type.storage == StorageClassInput) |
7063 | { |
7064 | expr += type_to_glsl(type: mbr_type) + "(" ; |
7065 | for (uint32_t k = 0; k < mbr_type.columns; k++, interface_index++) |
7066 | { |
7067 | if (is_array_of_struct) |
7068 | { |
7069 | const uint32_t indices[2] = { i, interface_index }; |
7070 | AccessChainMeta meta; |
7071 | expr += access_chain_internal( |
7072 | base: stage_in_ptr_var_id, indices, count: 2, |
7073 | flags: ACCESS_CHAIN_INDEX_IS_LITERAL_BIT | ACCESS_CHAIN_PTR_CHAIN_BIT, meta: &meta); |
7074 | } |
7075 | else |
7076 | expr += to_expression(id: ptr) + "." + to_member_name(type: iface_type, index: interface_index); |
7077 | if (expr_mbr_type.vecsize > mbr_type.vecsize) |
7078 | expr += vector_swizzle(vecsize: mbr_type.vecsize, index: 0); |
7079 | |
7080 | if (k + 1 < mbr_type.columns) |
7081 | expr += ", " ; |
7082 | } |
7083 | expr += ")" ; |
7084 | } |
7085 | else if (is_array(type: mbr_type)) |
7086 | { |
7087 | expr += type_to_glsl(type: mbr_type) + "({ " ; |
7088 | uint32_t array_size = to_array_size_literal(type: mbr_type, index: 0); |
7089 | for (uint32_t k = 0; k < array_size; k++, interface_index++) |
7090 | { |
7091 | if (is_array_of_struct) |
7092 | { |
7093 | const uint32_t indices[2] = { i, interface_index }; |
7094 | AccessChainMeta meta; |
7095 | expr += access_chain_internal( |
7096 | base: stage_in_ptr_var_id, indices, count: 2, |
7097 | flags: ACCESS_CHAIN_INDEX_IS_LITERAL_BIT | ACCESS_CHAIN_PTR_CHAIN_BIT, meta: &meta); |
7098 | } |
7099 | else |
7100 | expr += to_expression(id: ptr) + "." + to_member_name(type: iface_type, index: interface_index); |
7101 | if (expr_mbr_type.vecsize > mbr_type.vecsize) |
7102 | expr += vector_swizzle(vecsize: mbr_type.vecsize, index: 0); |
7103 | |
7104 | if (k + 1 < array_size) |
7105 | expr += ", " ; |
7106 | } |
7107 | expr += " })" ; |
7108 | } |
7109 | else |
7110 | { |
7111 | if (is_array_of_struct) |
7112 | { |
7113 | const uint32_t indices[2] = { i, interface_index }; |
7114 | AccessChainMeta meta; |
7115 | expr += access_chain_internal(base: stage_in_ptr_var_id, indices, count: 2, |
7116 | flags: ACCESS_CHAIN_INDEX_IS_LITERAL_BIT | ACCESS_CHAIN_PTR_CHAIN_BIT, |
7117 | meta: &meta); |
7118 | } |
7119 | else |
7120 | expr += to_expression(id: ptr) + "." + to_member_name(type: iface_type, index: interface_index); |
7121 | if (expr_mbr_type.vecsize > mbr_type.vecsize) |
7122 | expr += vector_swizzle(vecsize: mbr_type.vecsize, index: 0); |
7123 | } |
7124 | |
7125 | if (j + 1 < struct_type.member_types.size()) |
7126 | expr += ", " ; |
7127 | } |
7128 | expr += " }" ; |
7129 | if (i + 1 < num_control_points) |
7130 | expr += ", " ; |
7131 | } |
7132 | if (is_array_of_struct) |
7133 | expr += " })" ; |
7134 | } |
7135 | else if (is_matrix(type: result_type)) |
7136 | { |
7137 | bool is_array_of_matrix = is_array(type: result_type); |
7138 | if (is_array_of_matrix && !ptr_is_io_variable) |
7139 | SPIRV_CROSS_THROW("Loading array of matrix from IO variable must come directly from IO variable." ); |
7140 | if (interface_index == uint32_t(-1)) |
7141 | SPIRV_CROSS_THROW("Interface index is unknown. Cannot continue." ); |
7142 | |
7143 | if (is_array_of_matrix) |
7144 | { |
7145 | // Loading a matrix from each control point. |
7146 | uint32_t base_interface_index = interface_index; |
7147 | uint32_t num_control_points = to_array_size_literal(type: result_type, index: 0); |
7148 | expr += type_to_glsl(type: result_type) + "({ " ; |
7149 | |
7150 | auto &matrix_type = get_variable_element_type(var: get<SPIRVariable>(id: ptr)); |
7151 | |
7152 | for (uint32_t i = 0; i < num_control_points; i++) |
7153 | { |
7154 | interface_index = base_interface_index; |
7155 | expr += type_to_glsl(type: matrix_type) + "(" ; |
7156 | for (uint32_t j = 0; j < result_type.columns; j++, interface_index++) |
7157 | { |
7158 | const uint32_t indices[2] = { i, interface_index }; |
7159 | |
7160 | AccessChainMeta meta; |
7161 | expr += access_chain_internal(base: stage_in_ptr_var_id, indices, count: 2, |
7162 | flags: ACCESS_CHAIN_INDEX_IS_LITERAL_BIT | ACCESS_CHAIN_PTR_CHAIN_BIT, meta: &meta); |
7163 | if (expr_type.vecsize > result_type.vecsize) |
7164 | expr += vector_swizzle(vecsize: result_type.vecsize, index: 0); |
7165 | if (j + 1 < result_type.columns) |
7166 | expr += ", " ; |
7167 | } |
7168 | expr += ")" ; |
7169 | if (i + 1 < num_control_points) |
7170 | expr += ", " ; |
7171 | } |
7172 | |
7173 | expr += " })" ; |
7174 | } |
7175 | else |
7176 | { |
7177 | expr += type_to_glsl(type: result_type) + "(" ; |
7178 | for (uint32_t i = 0; i < result_type.columns; i++, interface_index++) |
7179 | { |
7180 | expr += to_expression(id: ptr) + "." + to_member_name(type: iface_type, index: interface_index); |
7181 | if (expr_type.vecsize > result_type.vecsize) |
7182 | expr += vector_swizzle(vecsize: result_type.vecsize, index: 0); |
7183 | if (i + 1 < result_type.columns) |
7184 | expr += ", " ; |
7185 | } |
7186 | expr += ")" ; |
7187 | } |
7188 | } |
7189 | else if (ptr_is_io_variable) |
7190 | { |
7191 | assert(is_array(result_type)); |
7192 | assert(result_type.array.size() == 1); |
7193 | if (interface_index == uint32_t(-1)) |
7194 | SPIRV_CROSS_THROW("Interface index is unknown. Cannot continue." ); |
7195 | |
7196 | // We're loading an array directly from a global variable. |
7197 | // This means we're loading one member from each control point. |
7198 | expr += type_to_glsl(type: result_type) + "({ " ; |
7199 | uint32_t num_control_points = to_array_size_literal(type: result_type, index: 0); |
7200 | |
7201 | for (uint32_t i = 0; i < num_control_points; i++) |
7202 | { |
7203 | const uint32_t indices[2] = { i, interface_index }; |
7204 | |
7205 | AccessChainMeta meta; |
7206 | expr += access_chain_internal(base: stage_in_ptr_var_id, indices, count: 2, |
7207 | flags: ACCESS_CHAIN_INDEX_IS_LITERAL_BIT | ACCESS_CHAIN_PTR_CHAIN_BIT, meta: &meta); |
7208 | if (expr_type.vecsize > result_type.vecsize) |
7209 | expr += vector_swizzle(vecsize: result_type.vecsize, index: 0); |
7210 | |
7211 | if (i + 1 < num_control_points) |
7212 | expr += ", " ; |
7213 | } |
7214 | expr += " })" ; |
7215 | } |
7216 | else |
7217 | { |
7218 | // We're loading an array from a concrete control point. |
7219 | assert(is_array(result_type)); |
7220 | assert(result_type.array.size() == 1); |
7221 | if (interface_index == uint32_t(-1)) |
7222 | SPIRV_CROSS_THROW("Interface index is unknown. Cannot continue." ); |
7223 | |
7224 | expr += type_to_glsl(type: result_type) + "({ " ; |
7225 | uint32_t array_size = to_array_size_literal(type: result_type, index: 0); |
7226 | for (uint32_t i = 0; i < array_size; i++, interface_index++) |
7227 | { |
7228 | expr += to_expression(id: ptr) + "." + to_member_name(type: iface_type, index: interface_index); |
7229 | if (expr_type.vecsize > result_type.vecsize) |
7230 | expr += vector_swizzle(vecsize: result_type.vecsize, index: 0); |
7231 | if (i + 1 < array_size) |
7232 | expr += ", " ; |
7233 | } |
7234 | expr += " })" ; |
7235 | } |
7236 | |
7237 | emit_op(result_type: result_type_id, result_id: id, rhs: expr, forward_rhs: false); |
7238 | register_read(expr: id, chain: ptr, forwarded: false); |
7239 | return true; |
7240 | } |
7241 | |
7242 | bool CompilerMSL::emit_tessellation_access_chain(const uint32_t *ops, uint32_t length) |
7243 | { |
7244 | // If this is a per-vertex output, remap it to the I/O array buffer. |
7245 | |
7246 | // Any object which did not go through IO flattening shenanigans will go there instead. |
7247 | // We will unflatten on-demand instead as needed, but not all possible cases can be supported, especially with arrays. |
7248 | |
7249 | auto *var = maybe_get_backing_variable(chain: ops[2]); |
7250 | bool patch = false; |
7251 | bool flat_data = false; |
7252 | bool ptr_is_chain = false; |
7253 | bool flatten_composites = false; |
7254 | |
7255 | bool is_block = false; |
7256 | |
7257 | if (var) |
7258 | is_block = has_decoration(id: get_variable_data_type(var: *var).self, decoration: DecorationBlock); |
7259 | |
7260 | if (var) |
7261 | { |
7262 | flatten_composites = variable_storage_requires_stage_io(storage: var->storage); |
7263 | patch = has_decoration(id: ops[2], decoration: DecorationPatch) || is_patch_block(type: get_variable_data_type(var: *var)); |
7264 | |
7265 | // Should match strip_array in add_interface_block. |
7266 | flat_data = var->storage == StorageClassInput || |
7267 | (var->storage == StorageClassOutput && get_execution_model() == ExecutionModelTessellationControl); |
7268 | |
7269 | // Patch inputs are treated as normal block IO variables, so they don't deal with this path at all. |
7270 | if (patch && (!is_block || var->storage == StorageClassInput)) |
7271 | flat_data = false; |
7272 | |
7273 | // We might have a chained access chain, where |
7274 | // we first take the access chain to the control point, and then we chain into a member or something similar. |
7275 | // In this case, we need to skip gl_in/gl_out remapping. |
7276 | // Also, skip ptr chain for patches. |
7277 | ptr_is_chain = var->self != ID(ops[2]); |
7278 | } |
7279 | |
7280 | bool builtin_variable = false; |
7281 | bool variable_is_flat = false; |
7282 | |
7283 | if (var && flat_data) |
7284 | { |
7285 | builtin_variable = is_builtin_variable(var: *var); |
7286 | |
7287 | BuiltIn bi_type = BuiltInMax; |
7288 | if (builtin_variable && !is_block) |
7289 | bi_type = BuiltIn(get_decoration(id: var->self, decoration: DecorationBuiltIn)); |
7290 | |
7291 | variable_is_flat = !builtin_variable || is_block || |
7292 | bi_type == BuiltInPosition || bi_type == BuiltInPointSize || |
7293 | bi_type == BuiltInClipDistance || bi_type == BuiltInCullDistance; |
7294 | } |
7295 | |
7296 | if (variable_is_flat) |
7297 | { |
7298 | // If output is masked, it is emitted as a "normal" variable, just go through normal code paths. |
7299 | // Only check this for the first level of access chain. |
7300 | // Dealing with this for partial access chains should be possible, but awkward. |
7301 | if (var->storage == StorageClassOutput && !ptr_is_chain) |
7302 | { |
7303 | bool masked = false; |
7304 | if (is_block) |
7305 | { |
7306 | uint32_t relevant_member_index = patch ? 3 : 4; |
7307 | // FIXME: This won't work properly if the application first access chains into gl_out element, |
7308 | // then access chains into the member. Super weird, but theoretically possible ... |
7309 | if (length > relevant_member_index) |
7310 | { |
7311 | uint32_t mbr_idx = get<SPIRConstant>(id: ops[relevant_member_index]).scalar(); |
7312 | masked = is_stage_output_block_member_masked(var: *var, index: mbr_idx, strip_array: true); |
7313 | } |
7314 | } |
7315 | else if (var) |
7316 | masked = is_stage_output_variable_masked(var: *var); |
7317 | |
7318 | if (masked) |
7319 | return false; |
7320 | } |
7321 | |
7322 | AccessChainMeta meta; |
7323 | SmallVector<uint32_t> indices; |
7324 | uint32_t next_id = ir.increase_bound_by(count: 1); |
7325 | |
7326 | indices.reserve(count: length - 3 + 1); |
7327 | |
7328 | uint32_t first_non_array_index = (ptr_is_chain ? 3 : 4) - (patch ? 1 : 0); |
7329 | |
7330 | VariableID stage_var_id; |
7331 | if (patch) |
7332 | stage_var_id = var->storage == StorageClassInput ? patch_stage_in_var_id : patch_stage_out_var_id; |
7333 | else |
7334 | stage_var_id = var->storage == StorageClassInput ? stage_in_ptr_var_id : stage_out_ptr_var_id; |
7335 | |
7336 | VariableID ptr = ptr_is_chain ? VariableID(ops[2]) : stage_var_id; |
7337 | if (!ptr_is_chain && !patch) |
7338 | { |
7339 | // Index into gl_in/gl_out with first array index. |
7340 | indices.push_back(t: ops[first_non_array_index - 1]); |
7341 | } |
7342 | |
7343 | auto &result_ptr_type = get<SPIRType>(id: ops[0]); |
7344 | |
7345 | uint32_t const_mbr_id = next_id++; |
7346 | uint32_t index = get_extended_decoration(id: ops[2], decoration: SPIRVCrossDecorationInterfaceMemberIndex); |
7347 | |
7348 | // If we have a pointer chain expression, and we are no longer pointing to a composite |
7349 | // object, we are in the clear. There is no longer a need to flatten anything. |
7350 | bool further_access_chain_is_trivial = false; |
7351 | if (ptr_is_chain && flatten_composites) |
7352 | { |
7353 | auto &ptr_type = expression_type(id: ptr); |
7354 | if (!is_array(type: ptr_type) && !is_matrix(type: ptr_type) && ptr_type.basetype != SPIRType::Struct) |
7355 | further_access_chain_is_trivial = true; |
7356 | } |
7357 | |
7358 | if (!further_access_chain_is_trivial && (flatten_composites || is_block)) |
7359 | { |
7360 | uint32_t i = first_non_array_index; |
7361 | auto *type = &get_variable_element_type(var: *var); |
7362 | if (index == uint32_t(-1) && length >= (first_non_array_index + 1)) |
7363 | { |
7364 | // Maybe this is a struct type in the input class, in which case |
7365 | // we put it as a decoration on the corresponding member. |
7366 | uint32_t mbr_idx = get_constant(id: ops[first_non_array_index]).scalar(); |
7367 | index = get_extended_member_decoration(type: var->self, index: mbr_idx, |
7368 | decoration: SPIRVCrossDecorationInterfaceMemberIndex); |
7369 | assert(index != uint32_t(-1)); |
7370 | i++; |
7371 | type = &get<SPIRType>(id: type->member_types[mbr_idx]); |
7372 | } |
7373 | |
7374 | // In this case, we're poking into flattened structures and arrays, so now we have to |
7375 | // combine the following indices. If we encounter a non-constant index, |
7376 | // we're hosed. |
7377 | for (; flatten_composites && i < length; ++i) |
7378 | { |
7379 | if (!is_array(type: *type) && !is_matrix(type: *type) && type->basetype != SPIRType::Struct) |
7380 | break; |
7381 | |
7382 | auto *c = maybe_get<SPIRConstant>(id: ops[i]); |
7383 | if (!c || c->specialization) |
7384 | SPIRV_CROSS_THROW("Trying to dynamically index into an array interface variable in tessellation. " |
7385 | "This is currently unsupported." ); |
7386 | |
7387 | // We're in flattened space, so just increment the member index into IO block. |
7388 | // We can only do this once in the current implementation, so either: |
7389 | // Struct, Matrix or 1-dimensional array for a control point. |
7390 | if (type->basetype == SPIRType::Struct && var->storage == StorageClassOutput) |
7391 | { |
7392 | // Need to consider holes, since individual block members might be masked away. |
7393 | uint32_t mbr_idx = c->scalar(); |
7394 | for (uint32_t j = 0; j < mbr_idx; j++) |
7395 | if (!is_stage_output_block_member_masked(var: *var, index: j, strip_array: true)) |
7396 | index++; |
7397 | } |
7398 | else |
7399 | index += c->scalar(); |
7400 | |
7401 | if (type->parent_type) |
7402 | type = &get<SPIRType>(id: type->parent_type); |
7403 | else if (type->basetype == SPIRType::Struct) |
7404 | type = &get<SPIRType>(id: type->member_types[c->scalar()]); |
7405 | } |
7406 | |
7407 | // We're not going to emit the actual member name, we let any further OpLoad take care of that. |
7408 | // Tag the access chain with the member index we're referencing. |
7409 | bool defer_access_chain = flatten_composites && (is_matrix(type: result_ptr_type) || is_array(type: result_ptr_type) || |
7410 | result_ptr_type.basetype == SPIRType::Struct); |
7411 | |
7412 | if (!defer_access_chain) |
7413 | { |
7414 | // Access the appropriate member of gl_in/gl_out. |
7415 | set<SPIRConstant>(id: const_mbr_id, args: get_uint_type_id(), args&: index, args: false); |
7416 | indices.push_back(t: const_mbr_id); |
7417 | |
7418 | // Member index is now irrelevant. |
7419 | index = uint32_t(-1); |
7420 | |
7421 | // Append any straggling access chain indices. |
7422 | if (i < length) |
7423 | indices.insert(itr: indices.end(), insert_begin: ops + i, insert_end: ops + length); |
7424 | } |
7425 | else |
7426 | { |
7427 | // We must have consumed the entire access chain if we're deferring it. |
7428 | assert(i == length); |
7429 | } |
7430 | |
7431 | if (index != uint32_t(-1)) |
7432 | set_extended_decoration(id: ops[1], decoration: SPIRVCrossDecorationInterfaceMemberIndex, value: index); |
7433 | else |
7434 | unset_extended_decoration(id: ops[1], decoration: SPIRVCrossDecorationInterfaceMemberIndex); |
7435 | } |
7436 | else |
7437 | { |
7438 | if (index != uint32_t(-1)) |
7439 | { |
7440 | set<SPIRConstant>(id: const_mbr_id, args: get_uint_type_id(), args&: index, args: false); |
7441 | indices.push_back(t: const_mbr_id); |
7442 | } |
7443 | |
7444 | // Member index is now irrelevant. |
7445 | index = uint32_t(-1); |
7446 | unset_extended_decoration(id: ops[1], decoration: SPIRVCrossDecorationInterfaceMemberIndex); |
7447 | |
7448 | indices.insert(itr: indices.end(), insert_begin: ops + first_non_array_index, insert_end: ops + length); |
7449 | } |
7450 | |
7451 | // We use the pointer to the base of the input/output array here, |
7452 | // so this is always a pointer chain. |
7453 | string e; |
7454 | |
7455 | if (!ptr_is_chain) |
7456 | { |
7457 | // This is the start of an access chain, use ptr_chain to index into control point array. |
7458 | e = access_chain(base: ptr, indices: indices.data(), count: uint32_t(indices.size()), target_type: result_ptr_type, meta: &meta, ptr_chain: !patch); |
7459 | } |
7460 | else |
7461 | { |
7462 | // If we're accessing a struct, we need to use member indices which are based on the IO block, |
7463 | // not actual struct type, so we have to use a split access chain here where |
7464 | // first path resolves the control point index, i.e. gl_in[index], and second half deals with |
7465 | // looking up flattened member name. |
7466 | |
7467 | // However, it is possible that we partially accessed a struct, |
7468 | // by taking pointer to member inside the control-point array. |
7469 | // For this case, we fall back to a natural access chain since we have already dealt with remapping struct members. |
7470 | // One way to check this here is if we have 2 implied read expressions. |
7471 | // First one is the gl_in/gl_out struct itself, then an index into that array. |
7472 | // If we have traversed further, we use a normal access chain formulation. |
7473 | auto *ptr_expr = maybe_get<SPIRExpression>(id: ptr); |
7474 | bool split_access_chain_formulation = flatten_composites && ptr_expr && |
7475 | ptr_expr->implied_read_expressions.size() == 2 && |
7476 | !further_access_chain_is_trivial; |
7477 | |
7478 | if (split_access_chain_formulation) |
7479 | { |
7480 | e = join(ts: to_expression(id: ptr), |
7481 | ts: access_chain_internal(base: stage_var_id, indices: indices.data(), count: uint32_t(indices.size()), |
7482 | flags: ACCESS_CHAIN_CHAIN_ONLY_BIT, meta: &meta)); |
7483 | } |
7484 | else |
7485 | { |
7486 | e = access_chain_internal(base: ptr, indices: indices.data(), count: uint32_t(indices.size()), flags: 0, meta: &meta); |
7487 | } |
7488 | } |
7489 | |
7490 | // Get the actual type of the object that was accessed. If it's a vector type and we changed it, |
7491 | // then we'll need to add a swizzle. |
7492 | // For this, we can't necessarily rely on the type of the base expression, because it might be |
7493 | // another access chain, and it will therefore already have the "correct" type. |
7494 | auto *expr_type = &get_variable_data_type(var: *var); |
7495 | if (has_extended_decoration(id: ops[2], decoration: SPIRVCrossDecorationTessIOOriginalInputTypeID)) |
7496 | expr_type = &get<SPIRType>(id: get_extended_decoration(id: ops[2], decoration: SPIRVCrossDecorationTessIOOriginalInputTypeID)); |
7497 | for (uint32_t i = 3; i < length; i++) |
7498 | { |
7499 | if (!is_array(type: *expr_type) && expr_type->basetype == SPIRType::Struct) |
7500 | expr_type = &get<SPIRType>(id: expr_type->member_types[get<SPIRConstant>(id: ops[i]).scalar()]); |
7501 | else |
7502 | expr_type = &get<SPIRType>(id: expr_type->parent_type); |
7503 | } |
7504 | if (!is_array(type: *expr_type) && !is_matrix(type: *expr_type) && expr_type->basetype != SPIRType::Struct && |
7505 | expr_type->vecsize > result_ptr_type.vecsize) |
7506 | e += vector_swizzle(vecsize: result_ptr_type.vecsize, index: 0); |
7507 | |
7508 | auto &expr = set<SPIRExpression>(id: ops[1], args: std::move(e), args: ops[0], args: should_forward(id: ops[2])); |
7509 | expr.loaded_from = var->self; |
7510 | expr.need_transpose = meta.need_transpose; |
7511 | expr.access_chain = true; |
7512 | |
7513 | // Mark the result as being packed if necessary. |
7514 | if (meta.storage_is_packed) |
7515 | set_extended_decoration(id: ops[1], decoration: SPIRVCrossDecorationPhysicalTypePacked); |
7516 | if (meta.storage_physical_type != 0) |
7517 | set_extended_decoration(id: ops[1], decoration: SPIRVCrossDecorationPhysicalTypeID, value: meta.storage_physical_type); |
7518 | if (meta.storage_is_invariant) |
7519 | set_decoration(id: ops[1], decoration: DecorationInvariant); |
7520 | // Save the type we found in case the result is used in another access chain. |
7521 | set_extended_decoration(id: ops[1], decoration: SPIRVCrossDecorationTessIOOriginalInputTypeID, value: expr_type->self); |
7522 | |
7523 | // If we have some expression dependencies in our access chain, this access chain is technically a forwarded |
7524 | // temporary which could be subject to invalidation. |
7525 | // Need to assume we're forwarded while calling inherit_expression_depdendencies. |
7526 | forwarded_temporaries.insert(x: ops[1]); |
7527 | // The access chain itself is never forced to a temporary, but its dependencies might. |
7528 | suppressed_usage_tracking.insert(x: ops[1]); |
7529 | |
7530 | for (uint32_t i = 2; i < length; i++) |
7531 | { |
7532 | inherit_expression_dependencies(dst: ops[1], source: ops[i]); |
7533 | add_implied_read_expression(e&: expr, source: ops[i]); |
7534 | } |
7535 | |
7536 | // If we have no dependencies after all, i.e., all indices in the access chain are immutable temporaries, |
7537 | // we're not forwarded after all. |
7538 | if (expr.expression_dependencies.empty()) |
7539 | forwarded_temporaries.erase(x: ops[1]); |
7540 | |
7541 | return true; |
7542 | } |
7543 | |
7544 | // If this is the inner tessellation level, and we're tessellating triangles, |
7545 | // drop the last index. It isn't an array in this case, so we can't have an |
7546 | // array reference here. We need to make this ID a variable instead of an |
7547 | // expression so we don't try to dereference it as a variable pointer. |
7548 | // Don't do this if the index is a constant 1, though. We need to drop stores |
7549 | // to that one. |
7550 | auto *m = ir.find_meta(id: var ? var->self : ID(0)); |
7551 | if (get_execution_model() == ExecutionModelTessellationControl && var && m && |
7552 | m->decoration.builtin_type == BuiltInTessLevelInner && get_entry_point().flags.get(bit: ExecutionModeTriangles)) |
7553 | { |
7554 | auto *c = maybe_get<SPIRConstant>(id: ops[3]); |
7555 | if (c && c->scalar() == 1) |
7556 | return false; |
7557 | auto &dest_var = set<SPIRVariable>(id: ops[1], args&: *var); |
7558 | dest_var.basetype = ops[0]; |
7559 | ir.meta[ops[1]] = ir.meta[ops[2]]; |
7560 | inherit_expression_dependencies(dst: ops[1], source: ops[2]); |
7561 | return true; |
7562 | } |
7563 | |
7564 | return false; |
7565 | } |
7566 | |
7567 | bool CompilerMSL::is_out_of_bounds_tessellation_level(uint32_t id_lhs) |
7568 | { |
7569 | if (!get_entry_point().flags.get(bit: ExecutionModeTriangles)) |
7570 | return false; |
7571 | |
7572 | // In SPIR-V, TessLevelInner always has two elements and TessLevelOuter always has |
7573 | // four. This is true even if we are tessellating triangles. This allows clients |
7574 | // to use a single tessellation control shader with multiple tessellation evaluation |
7575 | // shaders. |
7576 | // In Metal, however, only the first element of TessLevelInner and the first three |
7577 | // of TessLevelOuter are accessible. This stems from how in Metal, the tessellation |
7578 | // levels must be stored to a dedicated buffer in a particular format that depends |
7579 | // on the patch type. Therefore, in Triangles mode, any access to the second |
7580 | // inner level or the fourth outer level must be dropped. |
7581 | const auto *e = maybe_get<SPIRExpression>(id: id_lhs); |
7582 | if (!e || !e->access_chain) |
7583 | return false; |
7584 | BuiltIn builtin = BuiltIn(get_decoration(id: e->loaded_from, decoration: DecorationBuiltIn)); |
7585 | if (builtin != BuiltInTessLevelInner && builtin != BuiltInTessLevelOuter) |
7586 | return false; |
7587 | auto *c = maybe_get<SPIRConstant>(id: e->implied_read_expressions[1]); |
7588 | if (!c) |
7589 | return false; |
7590 | return (builtin == BuiltInTessLevelInner && c->scalar() == 1) || |
7591 | (builtin == BuiltInTessLevelOuter && c->scalar() == 3); |
7592 | } |
7593 | |
7594 | void CompilerMSL::prepare_access_chain_for_scalar_access(std::string &expr, const SPIRType &type, |
7595 | spv::StorageClass storage, bool &is_packed) |
7596 | { |
7597 | // If there is any risk of writes happening with the access chain in question, |
7598 | // and there is a risk of concurrent write access to other components, |
7599 | // we must cast the access chain to a plain pointer to ensure we only access the exact scalars we expect. |
7600 | // The MSL compiler refuses to allow component-level access for any non-packed vector types. |
7601 | if (!is_packed && (storage == StorageClassStorageBuffer || storage == StorageClassWorkgroup)) |
7602 | { |
7603 | const char *addr_space = storage == StorageClassWorkgroup ? "threadgroup" : "device" ; |
7604 | expr = join(ts: "((" , ts&: addr_space, ts: " " , ts: type_to_glsl(type), ts: "*)&" , ts: enclose_expression(expr), ts: ")" ); |
7605 | |
7606 | // Further indexing should happen with packed rules (array index, not swizzle). |
7607 | is_packed = true; |
7608 | } |
7609 | } |
7610 | |
7611 | bool CompilerMSL::access_chain_needs_stage_io_builtin_translation(uint32_t base) |
7612 | { |
7613 | auto *var = maybe_get_backing_variable(chain: base); |
7614 | if (!var || !is_tessellation_shader()) |
7615 | return true; |
7616 | |
7617 | // We only need to rewrite builtin access chains when accessing flattened builtins like gl_ClipDistance_N. |
7618 | // Avoid overriding it back to just gl_ClipDistance. |
7619 | // This can only happen in scenarios where we cannot flatten/unflatten access chains, so, the only case |
7620 | // where this triggers is evaluation shader inputs. |
7621 | bool redirect_builtin = get_execution_model() == ExecutionModelTessellationEvaluation ? |
7622 | var->storage == StorageClassOutput : false; |
7623 | return redirect_builtin; |
7624 | } |
7625 | |
7626 | // Sets the interface member index for an access chain to a pull-model interpolant. |
7627 | void CompilerMSL::fix_up_interpolant_access_chain(const uint32_t *ops, uint32_t length) |
7628 | { |
7629 | auto *var = maybe_get_backing_variable(chain: ops[2]); |
7630 | if (!var || !pull_model_inputs.count(x: var->self)) |
7631 | return; |
7632 | // Get the base index. |
7633 | uint32_t interface_index; |
7634 | auto &var_type = get_variable_data_type(var: *var); |
7635 | auto &result_type = get<SPIRType>(id: ops[0]); |
7636 | auto *type = &var_type; |
7637 | if (has_extended_decoration(id: ops[2], decoration: SPIRVCrossDecorationInterfaceMemberIndex)) |
7638 | { |
7639 | interface_index = get_extended_decoration(id: ops[2], decoration: SPIRVCrossDecorationInterfaceMemberIndex); |
7640 | } |
7641 | else |
7642 | { |
7643 | // Assume an access chain into a struct variable. |
7644 | assert(var_type.basetype == SPIRType::Struct); |
7645 | auto &c = get<SPIRConstant>(id: ops[3 + var_type.array.size()]); |
7646 | interface_index = |
7647 | get_extended_member_decoration(type: var->self, index: c.scalar(), decoration: SPIRVCrossDecorationInterfaceMemberIndex); |
7648 | } |
7649 | // Accumulate indices. We'll have to skip over the one for the struct, if present, because we already accounted |
7650 | // for that getting the base index. |
7651 | for (uint32_t i = 3; i < length; ++i) |
7652 | { |
7653 | if (is_vector(type: *type) && !is_array(type: *type) && is_scalar(type: result_type)) |
7654 | { |
7655 | // We don't want to combine the next index. Actually, we need to save it |
7656 | // so we know to apply a swizzle to the result of the interpolation. |
7657 | set_extended_decoration(id: ops[1], decoration: SPIRVCrossDecorationInterpolantComponentExpr, value: ops[i]); |
7658 | break; |
7659 | } |
7660 | |
7661 | auto *c = maybe_get<SPIRConstant>(id: ops[i]); |
7662 | if (!c || c->specialization) |
7663 | SPIRV_CROSS_THROW("Trying to dynamically index into an array interface variable using pull-model " |
7664 | "interpolation. This is currently unsupported." ); |
7665 | |
7666 | if (type->parent_type) |
7667 | type = &get<SPIRType>(id: type->parent_type); |
7668 | else if (type->basetype == SPIRType::Struct) |
7669 | type = &get<SPIRType>(id: type->member_types[c->scalar()]); |
7670 | |
7671 | if (!has_extended_decoration(id: ops[2], decoration: SPIRVCrossDecorationInterfaceMemberIndex) && |
7672 | i - 3 == var_type.array.size()) |
7673 | continue; |
7674 | |
7675 | interface_index += c->scalar(); |
7676 | } |
7677 | // Save this to the access chain itself so we can recover it later when calling an interpolation function. |
7678 | set_extended_decoration(id: ops[1], decoration: SPIRVCrossDecorationInterfaceMemberIndex, value: interface_index); |
7679 | } |
7680 | |
7681 | // Override for MSL-specific syntax instructions |
7682 | void CompilerMSL::emit_instruction(const Instruction &instruction) |
7683 | { |
7684 | #define MSL_BOP(op) emit_binary_op(ops[0], ops[1], ops[2], ops[3], #op) |
7685 | #define MSL_BOP_CAST(op, type) \ |
7686 | emit_binary_op_cast(ops[0], ops[1], ops[2], ops[3], #op, type, opcode_is_sign_invariant(opcode)) |
7687 | #define MSL_UOP(op) emit_unary_op(ops[0], ops[1], ops[2], #op) |
7688 | #define MSL_QFOP(op) emit_quaternary_func_op(ops[0], ops[1], ops[2], ops[3], ops[4], ops[5], #op) |
7689 | #define MSL_TFOP(op) emit_trinary_func_op(ops[0], ops[1], ops[2], ops[3], ops[4], #op) |
7690 | #define MSL_BFOP(op) emit_binary_func_op(ops[0], ops[1], ops[2], ops[3], #op) |
7691 | #define MSL_BFOP_CAST(op, type) \ |
7692 | emit_binary_func_op_cast(ops[0], ops[1], ops[2], ops[3], #op, type, opcode_is_sign_invariant(opcode)) |
7693 | #define MSL_UFOP(op) emit_unary_func_op(ops[0], ops[1], ops[2], #op) |
7694 | #define MSL_UNORD_BOP(op) emit_binary_unord_op(ops[0], ops[1], ops[2], ops[3], #op) |
7695 | |
7696 | auto ops = stream(instr: instruction); |
7697 | auto opcode = static_cast<Op>(instruction.op); |
7698 | |
7699 | opcode = get_remapped_spirv_op(op: opcode); |
7700 | |
7701 | // If we need to do implicit bitcasts, make sure we do it with the correct type. |
7702 | uint32_t integer_width = get_integer_width_for_instruction(instr: instruction); |
7703 | auto int_type = to_signed_basetype(width: integer_width); |
7704 | auto uint_type = to_unsigned_basetype(width: integer_width); |
7705 | |
7706 | switch (opcode) |
7707 | { |
7708 | case OpLoad: |
7709 | { |
7710 | uint32_t id = ops[1]; |
7711 | uint32_t ptr = ops[2]; |
7712 | if (is_tessellation_shader()) |
7713 | { |
7714 | if (!emit_tessellation_io_load(result_type_id: ops[0], id, ptr)) |
7715 | CompilerGLSL::emit_instruction(instr: instruction); |
7716 | } |
7717 | else |
7718 | { |
7719 | // Sample mask input for Metal is not an array |
7720 | if (BuiltIn(get_decoration(id: ptr, decoration: DecorationBuiltIn)) == BuiltInSampleMask) |
7721 | set_decoration(id, decoration: DecorationBuiltIn, argument: BuiltInSampleMask); |
7722 | CompilerGLSL::emit_instruction(instr: instruction); |
7723 | } |
7724 | break; |
7725 | } |
7726 | |
7727 | // Comparisons |
7728 | case OpIEqual: |
7729 | MSL_BOP_CAST(==, int_type); |
7730 | break; |
7731 | |
7732 | case OpLogicalEqual: |
7733 | case OpFOrdEqual: |
7734 | MSL_BOP(==); |
7735 | break; |
7736 | |
7737 | case OpINotEqual: |
7738 | MSL_BOP_CAST(!=, int_type); |
7739 | break; |
7740 | |
7741 | case OpLogicalNotEqual: |
7742 | case OpFOrdNotEqual: |
7743 | // TODO: Should probably negate the == result here. |
7744 | // Typically OrdNotEqual comes from GLSL which itself does not really specify what |
7745 | // happens with NaN. |
7746 | // Consider fixing this if we run into real issues. |
7747 | MSL_BOP(!=); |
7748 | break; |
7749 | |
7750 | case OpUGreaterThan: |
7751 | MSL_BOP_CAST(>, uint_type); |
7752 | break; |
7753 | |
7754 | case OpSGreaterThan: |
7755 | MSL_BOP_CAST(>, int_type); |
7756 | break; |
7757 | |
7758 | case OpFOrdGreaterThan: |
7759 | MSL_BOP(>); |
7760 | break; |
7761 | |
7762 | case OpUGreaterThanEqual: |
7763 | MSL_BOP_CAST(>=, uint_type); |
7764 | break; |
7765 | |
7766 | case OpSGreaterThanEqual: |
7767 | MSL_BOP_CAST(>=, int_type); |
7768 | break; |
7769 | |
7770 | case OpFOrdGreaterThanEqual: |
7771 | MSL_BOP(>=); |
7772 | break; |
7773 | |
7774 | case OpULessThan: |
7775 | MSL_BOP_CAST(<, uint_type); |
7776 | break; |
7777 | |
7778 | case OpSLessThan: |
7779 | MSL_BOP_CAST(<, int_type); |
7780 | break; |
7781 | |
7782 | case OpFOrdLessThan: |
7783 | MSL_BOP(<); |
7784 | break; |
7785 | |
7786 | case OpULessThanEqual: |
7787 | MSL_BOP_CAST(<=, uint_type); |
7788 | break; |
7789 | |
7790 | case OpSLessThanEqual: |
7791 | MSL_BOP_CAST(<=, int_type); |
7792 | break; |
7793 | |
7794 | case OpFOrdLessThanEqual: |
7795 | MSL_BOP(<=); |
7796 | break; |
7797 | |
7798 | case OpFUnordEqual: |
7799 | MSL_UNORD_BOP(==); |
7800 | break; |
7801 | |
7802 | case OpFUnordNotEqual: |
7803 | // not equal in MSL generates une opcodes to begin with. |
7804 | // Since unordered not equal is how it works in C, just inherit that behavior. |
7805 | MSL_BOP(!=); |
7806 | break; |
7807 | |
7808 | case OpFUnordGreaterThan: |
7809 | MSL_UNORD_BOP(>); |
7810 | break; |
7811 | |
7812 | case OpFUnordGreaterThanEqual: |
7813 | MSL_UNORD_BOP(>=); |
7814 | break; |
7815 | |
7816 | case OpFUnordLessThan: |
7817 | MSL_UNORD_BOP(<); |
7818 | break; |
7819 | |
7820 | case OpFUnordLessThanEqual: |
7821 | MSL_UNORD_BOP(<=); |
7822 | break; |
7823 | |
7824 | // Derivatives |
7825 | case OpDPdx: |
7826 | case OpDPdxFine: |
7827 | case OpDPdxCoarse: |
7828 | MSL_UFOP(dfdx); |
7829 | register_control_dependent_expression(expr: ops[1]); |
7830 | break; |
7831 | |
7832 | case OpDPdy: |
7833 | case OpDPdyFine: |
7834 | case OpDPdyCoarse: |
7835 | MSL_UFOP(dfdy); |
7836 | register_control_dependent_expression(expr: ops[1]); |
7837 | break; |
7838 | |
7839 | case OpFwidth: |
7840 | case OpFwidthCoarse: |
7841 | case OpFwidthFine: |
7842 | MSL_UFOP(fwidth); |
7843 | register_control_dependent_expression(expr: ops[1]); |
7844 | break; |
7845 | |
7846 | // Bitfield |
7847 | case OpBitFieldInsert: |
7848 | { |
7849 | emit_bitfield_insert_op(result_type: ops[0], result_id: ops[1], op0: ops[2], op1: ops[3], op2: ops[4], op3: ops[5], op: "insert_bits" , offset_count_type: SPIRType::UInt); |
7850 | break; |
7851 | } |
7852 | |
7853 | case OpBitFieldSExtract: |
7854 | { |
7855 | emit_trinary_func_op_bitextract(result_type: ops[0], result_id: ops[1], op0: ops[2], op1: ops[3], op2: ops[4], op: "extract_bits" , expected_result_type: int_type, input_type0: int_type, |
7856 | input_type1: SPIRType::UInt, input_type2: SPIRType::UInt); |
7857 | break; |
7858 | } |
7859 | |
7860 | case OpBitFieldUExtract: |
7861 | { |
7862 | emit_trinary_func_op_bitextract(result_type: ops[0], result_id: ops[1], op0: ops[2], op1: ops[3], op2: ops[4], op: "extract_bits" , expected_result_type: uint_type, input_type0: uint_type, |
7863 | input_type1: SPIRType::UInt, input_type2: SPIRType::UInt); |
7864 | break; |
7865 | } |
7866 | |
7867 | case OpBitReverse: |
7868 | // BitReverse does not have issues with sign since result type must match input type. |
7869 | MSL_UFOP(reverse_bits); |
7870 | break; |
7871 | |
7872 | case OpBitCount: |
7873 | { |
7874 | auto basetype = expression_type(id: ops[2]).basetype; |
7875 | emit_unary_func_op_cast(result_type: ops[0], result_id: ops[1], op0: ops[2], op: "popcount" , input_type: basetype, expected_result_type: basetype); |
7876 | break; |
7877 | } |
7878 | |
7879 | case OpFRem: |
7880 | MSL_BFOP(fmod); |
7881 | break; |
7882 | |
7883 | case OpFMul: |
7884 | if (msl_options.invariant_float_math || has_decoration(id: ops[1], decoration: DecorationNoContraction)) |
7885 | MSL_BFOP(spvFMul); |
7886 | else |
7887 | MSL_BOP(*); |
7888 | break; |
7889 | |
7890 | case OpFAdd: |
7891 | if (msl_options.invariant_float_math || has_decoration(id: ops[1], decoration: DecorationNoContraction)) |
7892 | MSL_BFOP(spvFAdd); |
7893 | else |
7894 | MSL_BOP(+); |
7895 | break; |
7896 | |
7897 | case OpFSub: |
7898 | if (msl_options.invariant_float_math || has_decoration(id: ops[1], decoration: DecorationNoContraction)) |
7899 | MSL_BFOP(spvFSub); |
7900 | else |
7901 | MSL_BOP(-); |
7902 | break; |
7903 | |
7904 | // Atomics |
7905 | case OpAtomicExchange: |
7906 | { |
7907 | uint32_t result_type = ops[0]; |
7908 | uint32_t id = ops[1]; |
7909 | uint32_t ptr = ops[2]; |
7910 | uint32_t mem_sem = ops[4]; |
7911 | uint32_t val = ops[5]; |
7912 | emit_atomic_func_op(result_type, result_id: id, op: "atomic_exchange_explicit" , opcode, mem_order_1: mem_sem, mem_order_2: mem_sem, has_mem_order_2: false, op0: ptr, op1: val); |
7913 | break; |
7914 | } |
7915 | |
7916 | case OpAtomicCompareExchange: |
7917 | { |
7918 | uint32_t result_type = ops[0]; |
7919 | uint32_t id = ops[1]; |
7920 | uint32_t ptr = ops[2]; |
7921 | uint32_t mem_sem_pass = ops[4]; |
7922 | uint32_t mem_sem_fail = ops[5]; |
7923 | uint32_t val = ops[6]; |
7924 | uint32_t comp = ops[7]; |
7925 | emit_atomic_func_op(result_type, result_id: id, op: "atomic_compare_exchange_weak_explicit" , opcode, |
7926 | mem_order_1: mem_sem_pass, mem_order_2: mem_sem_fail, has_mem_order_2: true, |
7927 | op0: ptr, op1: comp, op1_is_pointer: true, op1_is_literal: false, op2: val); |
7928 | break; |
7929 | } |
7930 | |
7931 | case OpAtomicCompareExchangeWeak: |
7932 | SPIRV_CROSS_THROW("OpAtomicCompareExchangeWeak is only supported in kernel profile." ); |
7933 | |
7934 | case OpAtomicLoad: |
7935 | { |
7936 | uint32_t result_type = ops[0]; |
7937 | uint32_t id = ops[1]; |
7938 | uint32_t ptr = ops[2]; |
7939 | uint32_t mem_sem = ops[4]; |
7940 | emit_atomic_func_op(result_type, result_id: id, op: "atomic_load_explicit" , opcode, mem_order_1: mem_sem, mem_order_2: mem_sem, has_mem_order_2: false, op0: ptr, op1: 0); |
7941 | break; |
7942 | } |
7943 | |
7944 | case OpAtomicStore: |
7945 | { |
7946 | uint32_t result_type = expression_type(id: ops[0]).self; |
7947 | uint32_t id = ops[0]; |
7948 | uint32_t ptr = ops[0]; |
7949 | uint32_t mem_sem = ops[2]; |
7950 | uint32_t val = ops[3]; |
7951 | emit_atomic_func_op(result_type, result_id: id, op: "atomic_store_explicit" , opcode, mem_order_1: mem_sem, mem_order_2: mem_sem, has_mem_order_2: false, op0: ptr, op1: val); |
7952 | break; |
7953 | } |
7954 | |
7955 | #define MSL_AFMO_IMPL(op, valsrc, valconst) \ |
7956 | do \ |
7957 | { \ |
7958 | uint32_t result_type = ops[0]; \ |
7959 | uint32_t id = ops[1]; \ |
7960 | uint32_t ptr = ops[2]; \ |
7961 | uint32_t mem_sem = ops[4]; \ |
7962 | uint32_t val = valsrc; \ |
7963 | emit_atomic_func_op(result_type, id, "atomic_fetch_" #op "_explicit", opcode, \ |
7964 | mem_sem, mem_sem, false, ptr, val, \ |
7965 | false, valconst); \ |
7966 | } while (false) |
7967 | |
7968 | #define MSL_AFMO(op) MSL_AFMO_IMPL(op, ops[5], false) |
7969 | #define MSL_AFMIO(op) MSL_AFMO_IMPL(op, 1, true) |
7970 | |
7971 | case OpAtomicIIncrement: |
7972 | MSL_AFMIO(add); |
7973 | break; |
7974 | |
7975 | case OpAtomicIDecrement: |
7976 | MSL_AFMIO(sub); |
7977 | break; |
7978 | |
7979 | case OpAtomicIAdd: |
7980 | MSL_AFMO(add); |
7981 | break; |
7982 | |
7983 | case OpAtomicISub: |
7984 | MSL_AFMO(sub); |
7985 | break; |
7986 | |
7987 | case OpAtomicSMin: |
7988 | case OpAtomicUMin: |
7989 | MSL_AFMO(min); |
7990 | break; |
7991 | |
7992 | case OpAtomicSMax: |
7993 | case OpAtomicUMax: |
7994 | MSL_AFMO(max); |
7995 | break; |
7996 | |
7997 | case OpAtomicAnd: |
7998 | MSL_AFMO(and); |
7999 | break; |
8000 | |
8001 | case OpAtomicOr: |
8002 | MSL_AFMO(or); |
8003 | break; |
8004 | |
8005 | case OpAtomicXor: |
8006 | MSL_AFMO(xor); |
8007 | break; |
8008 | |
8009 | // Images |
8010 | |
8011 | // Reads == Fetches in Metal |
8012 | case OpImageRead: |
8013 | { |
8014 | // Mark that this shader reads from this image |
8015 | uint32_t img_id = ops[2]; |
8016 | auto &type = expression_type(id: img_id); |
8017 | if (type.image.dim != DimSubpassData) |
8018 | { |
8019 | auto *p_var = maybe_get_backing_variable(chain: img_id); |
8020 | if (p_var && has_decoration(id: p_var->self, decoration: DecorationNonReadable)) |
8021 | { |
8022 | unset_decoration(id: p_var->self, decoration: DecorationNonReadable); |
8023 | force_recompile(); |
8024 | } |
8025 | } |
8026 | |
8027 | emit_texture_op(i: instruction, sparse: false); |
8028 | break; |
8029 | } |
8030 | |
8031 | // Emulate texture2D atomic operations |
8032 | case OpImageTexelPointer: |
8033 | { |
8034 | // When using the pointer, we need to know which variable it is actually loaded from. |
8035 | auto *var = maybe_get_backing_variable(chain: ops[2]); |
8036 | if (var && atomic_image_vars.count(x: var->self)) |
8037 | { |
8038 | uint32_t result_type = ops[0]; |
8039 | uint32_t id = ops[1]; |
8040 | |
8041 | std::string coord = to_expression(id: ops[3]); |
8042 | auto &type = expression_type(id: ops[2]); |
8043 | if (type.image.dim == Dim2D) |
8044 | { |
8045 | coord = join(ts: "spvImage2DAtomicCoord(" , ts&: coord, ts: ", " , ts: to_expression(id: ops[2]), ts: ")" ); |
8046 | } |
8047 | |
8048 | auto &e = set<SPIRExpression>(id, args: join(ts: to_expression(id: ops[2]), ts: "_atomic[" , ts&: coord, ts: "]" ), args&: result_type, args: true); |
8049 | e.loaded_from = var ? var->self : ID(0); |
8050 | inherit_expression_dependencies(dst: id, source: ops[3]); |
8051 | } |
8052 | else |
8053 | { |
8054 | uint32_t result_type = ops[0]; |
8055 | uint32_t id = ops[1]; |
8056 | auto &e = |
8057 | set<SPIRExpression>(id, args: join(ts: to_expression(id: ops[2]), ts: ", " , ts: to_expression(id: ops[3])), args&: result_type, args: true); |
8058 | |
8059 | // When using the pointer, we need to know which variable it is actually loaded from. |
8060 | e.loaded_from = var ? var->self : ID(0); |
8061 | inherit_expression_dependencies(dst: id, source: ops[3]); |
8062 | } |
8063 | break; |
8064 | } |
8065 | |
8066 | case OpImageWrite: |
8067 | { |
8068 | uint32_t img_id = ops[0]; |
8069 | uint32_t coord_id = ops[1]; |
8070 | uint32_t texel_id = ops[2]; |
8071 | const uint32_t *opt = &ops[3]; |
8072 | uint32_t length = instruction.length - 3; |
8073 | |
8074 | // Bypass pointers because we need the real image struct |
8075 | auto &type = expression_type(id: img_id); |
8076 | auto &img_type = get<SPIRType>(id: type.self); |
8077 | |
8078 | // Ensure this image has been marked as being written to and force a |
8079 | // recommpile so that the image type output will include write access |
8080 | auto *p_var = maybe_get_backing_variable(chain: img_id); |
8081 | if (p_var && has_decoration(id: p_var->self, decoration: DecorationNonWritable)) |
8082 | { |
8083 | unset_decoration(id: p_var->self, decoration: DecorationNonWritable); |
8084 | force_recompile(); |
8085 | } |
8086 | |
8087 | bool forward = false; |
8088 | uint32_t bias = 0; |
8089 | uint32_t lod = 0; |
8090 | uint32_t flags = 0; |
8091 | |
8092 | if (length) |
8093 | { |
8094 | flags = *opt++; |
8095 | length--; |
8096 | } |
8097 | |
8098 | auto test = [&](uint32_t &v, uint32_t flag) { |
8099 | if (length && (flags & flag)) |
8100 | { |
8101 | v = *opt++; |
8102 | length--; |
8103 | } |
8104 | }; |
8105 | |
8106 | test(bias, ImageOperandsBiasMask); |
8107 | test(lod, ImageOperandsLodMask); |
8108 | |
8109 | auto &texel_type = expression_type(id: texel_id); |
8110 | auto store_type = texel_type; |
8111 | store_type.vecsize = 4; |
8112 | |
8113 | TextureFunctionArguments args = {}; |
8114 | args.base.img = img_id; |
8115 | args.base.imgtype = &img_type; |
8116 | args.base.is_fetch = true; |
8117 | args.coord = coord_id; |
8118 | args.lod = lod; |
8119 | statement(ts: join(ts: to_expression(id: img_id), ts: ".write(" , |
8120 | ts: remap_swizzle(result_type: store_type, input_components: texel_type.vecsize, expr: to_expression(id: texel_id)), ts: ", " , |
8121 | ts: CompilerMSL::to_function_args(args, p_forward: &forward), ts: ");" )); |
8122 | |
8123 | if (p_var && variable_storage_is_aliased(var: *p_var)) |
8124 | flush_all_aliased_variables(); |
8125 | |
8126 | break; |
8127 | } |
8128 | |
8129 | case OpImageQuerySize: |
8130 | case OpImageQuerySizeLod: |
8131 | { |
8132 | uint32_t rslt_type_id = ops[0]; |
8133 | auto &rslt_type = get<SPIRType>(id: rslt_type_id); |
8134 | |
8135 | uint32_t id = ops[1]; |
8136 | |
8137 | uint32_t img_id = ops[2]; |
8138 | string img_exp = to_expression(id: img_id); |
8139 | auto &img_type = expression_type(id: img_id); |
8140 | Dim img_dim = img_type.image.dim; |
8141 | bool img_is_array = img_type.image.arrayed; |
8142 | |
8143 | if (img_type.basetype != SPIRType::Image) |
8144 | SPIRV_CROSS_THROW("Invalid type for OpImageQuerySize." ); |
8145 | |
8146 | string lod; |
8147 | if (opcode == OpImageQuerySizeLod) |
8148 | { |
8149 | // LOD index defaults to zero, so don't bother outputing level zero index |
8150 | string decl_lod = to_expression(id: ops[3]); |
8151 | if (decl_lod != "0" ) |
8152 | lod = decl_lod; |
8153 | } |
8154 | |
8155 | string expr = type_to_glsl(type: rslt_type) + "(" ; |
8156 | expr += img_exp + ".get_width(" + lod + ")" ; |
8157 | |
8158 | if (img_dim == Dim2D || img_dim == DimCube || img_dim == Dim3D) |
8159 | expr += ", " + img_exp + ".get_height(" + lod + ")" ; |
8160 | |
8161 | if (img_dim == Dim3D) |
8162 | expr += ", " + img_exp + ".get_depth(" + lod + ")" ; |
8163 | |
8164 | if (img_is_array) |
8165 | { |
8166 | expr += ", " + img_exp + ".get_array_size()" ; |
8167 | if (img_dim == DimCube && msl_options.emulate_cube_array) |
8168 | expr += " / 6" ; |
8169 | } |
8170 | |
8171 | expr += ")" ; |
8172 | |
8173 | emit_op(result_type: rslt_type_id, result_id: id, rhs: expr, forward_rhs: should_forward(id: img_id)); |
8174 | |
8175 | break; |
8176 | } |
8177 | |
8178 | case OpImageQueryLod: |
8179 | { |
8180 | if (!msl_options.supports_msl_version(major: 2, minor: 2)) |
8181 | SPIRV_CROSS_THROW("ImageQueryLod is only supported on MSL 2.2 and up." ); |
8182 | uint32_t result_type = ops[0]; |
8183 | uint32_t id = ops[1]; |
8184 | uint32_t image_id = ops[2]; |
8185 | uint32_t coord_id = ops[3]; |
8186 | emit_uninitialized_temporary_expression(type: result_type, id); |
8187 | |
8188 | auto sampler_expr = to_sampler_expression(id: image_id); |
8189 | auto *combined = maybe_get<SPIRCombinedImageSampler>(id: image_id); |
8190 | auto image_expr = combined ? to_expression(id: combined->image) : to_expression(id: image_id); |
8191 | |
8192 | // TODO: It is unclear if calculcate_clamped_lod also conditionally rounds |
8193 | // the reported LOD based on the sampler. NEAREST miplevel should |
8194 | // round the LOD, but LINEAR miplevel should not round. |
8195 | // Let's hope this does not become an issue ... |
8196 | statement(ts: to_expression(id), ts: ".x = " , ts&: image_expr, ts: ".calculate_clamped_lod(" , ts&: sampler_expr, ts: ", " , |
8197 | ts: to_expression(id: coord_id), ts: ");" ); |
8198 | statement(ts: to_expression(id), ts: ".y = " , ts&: image_expr, ts: ".calculate_unclamped_lod(" , ts&: sampler_expr, ts: ", " , |
8199 | ts: to_expression(id: coord_id), ts: ");" ); |
8200 | register_control_dependent_expression(expr: id); |
8201 | break; |
8202 | } |
8203 | |
8204 | #define MSL_ImgQry(qrytype) \ |
8205 | do \ |
8206 | { \ |
8207 | uint32_t rslt_type_id = ops[0]; \ |
8208 | auto &rslt_type = get<SPIRType>(rslt_type_id); \ |
8209 | uint32_t id = ops[1]; \ |
8210 | uint32_t img_id = ops[2]; \ |
8211 | string img_exp = to_expression(img_id); \ |
8212 | string expr = type_to_glsl(rslt_type) + "(" + img_exp + ".get_num_" #qrytype "())"; \ |
8213 | emit_op(rslt_type_id, id, expr, should_forward(img_id)); \ |
8214 | } while (false) |
8215 | |
8216 | case OpImageQueryLevels: |
8217 | MSL_ImgQry(mip_levels); |
8218 | break; |
8219 | |
8220 | case OpImageQuerySamples: |
8221 | MSL_ImgQry(samples); |
8222 | break; |
8223 | |
8224 | case OpImage: |
8225 | { |
8226 | uint32_t result_type = ops[0]; |
8227 | uint32_t id = ops[1]; |
8228 | auto *combined = maybe_get<SPIRCombinedImageSampler>(id: ops[2]); |
8229 | |
8230 | if (combined) |
8231 | { |
8232 | auto &e = emit_op(result_type, result_id: id, rhs: to_expression(id: combined->image), forward_rhs: true, suppress_usage_tracking: true); |
8233 | auto *var = maybe_get_backing_variable(chain: combined->image); |
8234 | if (var) |
8235 | e.loaded_from = var->self; |
8236 | } |
8237 | else |
8238 | { |
8239 | auto *var = maybe_get_backing_variable(chain: ops[2]); |
8240 | SPIRExpression *e; |
8241 | if (var && has_extended_decoration(id: var->self, decoration: SPIRVCrossDecorationDynamicImageSampler)) |
8242 | e = &emit_op(result_type, result_id: id, rhs: join(ts: to_expression(id: ops[2]), ts: ".plane0" ), forward_rhs: true, suppress_usage_tracking: true); |
8243 | else |
8244 | e = &emit_op(result_type, result_id: id, rhs: to_expression(id: ops[2]), forward_rhs: true, suppress_usage_tracking: true); |
8245 | if (var) |
8246 | e->loaded_from = var->self; |
8247 | } |
8248 | break; |
8249 | } |
8250 | |
8251 | // Casting |
8252 | case OpQuantizeToF16: |
8253 | { |
8254 | uint32_t result_type = ops[0]; |
8255 | uint32_t id = ops[1]; |
8256 | uint32_t arg = ops[2]; |
8257 | string exp = join(ts: "spvQuantizeToF16(" , ts: to_expression(id: arg), ts: ")" ); |
8258 | emit_op(result_type, result_id: id, rhs: exp, forward_rhs: should_forward(id: arg)); |
8259 | break; |
8260 | } |
8261 | |
8262 | case OpInBoundsAccessChain: |
8263 | case OpAccessChain: |
8264 | case OpPtrAccessChain: |
8265 | if (is_tessellation_shader()) |
8266 | { |
8267 | if (!emit_tessellation_access_chain(ops, length: instruction.length)) |
8268 | CompilerGLSL::emit_instruction(instr: instruction); |
8269 | } |
8270 | else |
8271 | CompilerGLSL::emit_instruction(instr: instruction); |
8272 | fix_up_interpolant_access_chain(ops, length: instruction.length); |
8273 | break; |
8274 | |
8275 | case OpStore: |
8276 | if (is_out_of_bounds_tessellation_level(id_lhs: ops[0])) |
8277 | break; |
8278 | |
8279 | if (maybe_emit_array_assignment(id_lhs: ops[0], id_rhs: ops[1])) |
8280 | break; |
8281 | |
8282 | CompilerGLSL::emit_instruction(instr: instruction); |
8283 | break; |
8284 | |
8285 | // Compute barriers |
8286 | case OpMemoryBarrier: |
8287 | emit_barrier(id_exe_scope: 0, id_mem_scope: ops[0], id_mem_sem: ops[1]); |
8288 | break; |
8289 | |
8290 | case OpControlBarrier: |
8291 | // In GLSL a memory barrier is often followed by a control barrier. |
8292 | // But in MSL, memory barriers are also control barriers, so don't |
8293 | // emit a simple control barrier if a memory barrier has just been emitted. |
8294 | if (previous_instruction_opcode != OpMemoryBarrier) |
8295 | emit_barrier(id_exe_scope: ops[0], id_mem_scope: ops[1], id_mem_sem: ops[2]); |
8296 | break; |
8297 | |
8298 | case OpOuterProduct: |
8299 | { |
8300 | uint32_t result_type = ops[0]; |
8301 | uint32_t id = ops[1]; |
8302 | uint32_t a = ops[2]; |
8303 | uint32_t b = ops[3]; |
8304 | |
8305 | auto &type = get<SPIRType>(id: result_type); |
8306 | string expr = type_to_glsl_constructor(type); |
8307 | expr += "(" ; |
8308 | for (uint32_t col = 0; col < type.columns; col++) |
8309 | { |
8310 | expr += to_enclosed_unpacked_expression(id: a); |
8311 | expr += " * " ; |
8312 | expr += to_extract_component_expression(id: b, index: col); |
8313 | if (col + 1 < type.columns) |
8314 | expr += ", " ; |
8315 | } |
8316 | expr += ")" ; |
8317 | emit_op(result_type, result_id: id, rhs: expr, forward_rhs: should_forward(id: a) && should_forward(id: b)); |
8318 | inherit_expression_dependencies(dst: id, source: a); |
8319 | inherit_expression_dependencies(dst: id, source: b); |
8320 | break; |
8321 | } |
8322 | |
8323 | case OpVectorTimesMatrix: |
8324 | case OpMatrixTimesVector: |
8325 | { |
8326 | if (!msl_options.invariant_float_math && !has_decoration(id: ops[1], decoration: DecorationNoContraction)) |
8327 | { |
8328 | CompilerGLSL::emit_instruction(instr: instruction); |
8329 | break; |
8330 | } |
8331 | |
8332 | // If the matrix needs transpose, just flip the multiply order. |
8333 | auto *e = maybe_get<SPIRExpression>(id: ops[opcode == OpMatrixTimesVector ? 2 : 3]); |
8334 | if (e && e->need_transpose) |
8335 | { |
8336 | e->need_transpose = false; |
8337 | string expr; |
8338 | |
8339 | if (opcode == OpMatrixTimesVector) |
8340 | { |
8341 | expr = join(ts: "spvFMulVectorMatrix(" , ts: to_enclosed_unpacked_expression(id: ops[3]), ts: ", " , |
8342 | ts: to_unpacked_row_major_matrix_expression(id: ops[2]), ts: ")" ); |
8343 | } |
8344 | else |
8345 | { |
8346 | expr = join(ts: "spvFMulMatrixVector(" , ts: to_unpacked_row_major_matrix_expression(id: ops[3]), ts: ", " , |
8347 | ts: to_enclosed_unpacked_expression(id: ops[2]), ts: ")" ); |
8348 | } |
8349 | |
8350 | bool forward = should_forward(id: ops[2]) && should_forward(id: ops[3]); |
8351 | emit_op(result_type: ops[0], result_id: ops[1], rhs: expr, forward_rhs: forward); |
8352 | e->need_transpose = true; |
8353 | inherit_expression_dependencies(dst: ops[1], source: ops[2]); |
8354 | inherit_expression_dependencies(dst: ops[1], source: ops[3]); |
8355 | } |
8356 | else |
8357 | { |
8358 | if (opcode == OpMatrixTimesVector) |
8359 | MSL_BFOP(spvFMulMatrixVector); |
8360 | else |
8361 | MSL_BFOP(spvFMulVectorMatrix); |
8362 | } |
8363 | break; |
8364 | } |
8365 | |
8366 | case OpMatrixTimesMatrix: |
8367 | { |
8368 | if (!msl_options.invariant_float_math && !has_decoration(id: ops[1], decoration: DecorationNoContraction)) |
8369 | { |
8370 | CompilerGLSL::emit_instruction(instr: instruction); |
8371 | break; |
8372 | } |
8373 | |
8374 | auto *a = maybe_get<SPIRExpression>(id: ops[2]); |
8375 | auto *b = maybe_get<SPIRExpression>(id: ops[3]); |
8376 | |
8377 | // If both matrices need transpose, we can multiply in flipped order and tag the expression as transposed. |
8378 | // a^T * b^T = (b * a)^T. |
8379 | if (a && b && a->need_transpose && b->need_transpose) |
8380 | { |
8381 | a->need_transpose = false; |
8382 | b->need_transpose = false; |
8383 | |
8384 | auto expr = |
8385 | join(ts: "spvFMulMatrixMatrix(" , ts: enclose_expression(expr: to_unpacked_row_major_matrix_expression(id: ops[3])), ts: ", " , |
8386 | ts: enclose_expression(expr: to_unpacked_row_major_matrix_expression(id: ops[2])), ts: ")" ); |
8387 | |
8388 | bool forward = should_forward(id: ops[2]) && should_forward(id: ops[3]); |
8389 | auto &e = emit_op(result_type: ops[0], result_id: ops[1], rhs: expr, forward_rhs: forward); |
8390 | e.need_transpose = true; |
8391 | a->need_transpose = true; |
8392 | b->need_transpose = true; |
8393 | inherit_expression_dependencies(dst: ops[1], source: ops[2]); |
8394 | inherit_expression_dependencies(dst: ops[1], source: ops[3]); |
8395 | } |
8396 | else |
8397 | MSL_BFOP(spvFMulMatrixMatrix); |
8398 | |
8399 | break; |
8400 | } |
8401 | |
8402 | case OpIAddCarry: |
8403 | case OpISubBorrow: |
8404 | { |
8405 | uint32_t result_type = ops[0]; |
8406 | uint32_t result_id = ops[1]; |
8407 | uint32_t op0 = ops[2]; |
8408 | uint32_t op1 = ops[3]; |
8409 | auto &type = get<SPIRType>(id: result_type); |
8410 | emit_uninitialized_temporary_expression(type: result_type, id: result_id); |
8411 | |
8412 | auto &res_type = get<SPIRType>(id: type.member_types[1]); |
8413 | if (opcode == OpIAddCarry) |
8414 | { |
8415 | statement(ts: to_expression(id: result_id), ts: "." , ts: to_member_name(type, index: 0), ts: " = " , |
8416 | ts: to_enclosed_unpacked_expression(id: op0), ts: " + " , ts: to_enclosed_unpacked_expression(id: op1), ts: ";" ); |
8417 | statement(ts: to_expression(id: result_id), ts: "." , ts: to_member_name(type, index: 1), ts: " = select(" , ts: type_to_glsl(type: res_type), |
8418 | ts: "(1), " , ts: type_to_glsl(type: res_type), ts: "(0), " , ts: to_unpacked_expression(id: result_id), ts: "." , ts: to_member_name(type, index: 0), |
8419 | ts: " >= max(" , ts: to_unpacked_expression(id: op0), ts: ", " , ts: to_unpacked_expression(id: op1), ts: "));" ); |
8420 | } |
8421 | else |
8422 | { |
8423 | statement(ts: to_expression(id: result_id), ts: "." , ts: to_member_name(type, index: 0), ts: " = " , ts: to_enclosed_unpacked_expression(id: op0), ts: " - " , |
8424 | ts: to_enclosed_unpacked_expression(id: op1), ts: ";" ); |
8425 | statement(ts: to_expression(id: result_id), ts: "." , ts: to_member_name(type, index: 1), ts: " = select(" , ts: type_to_glsl(type: res_type), |
8426 | ts: "(1), " , ts: type_to_glsl(type: res_type), ts: "(0), " , ts: to_enclosed_unpacked_expression(id: op0), |
8427 | ts: " >= " , ts: to_enclosed_unpacked_expression(id: op1), ts: ");" ); |
8428 | } |
8429 | break; |
8430 | } |
8431 | |
8432 | case OpUMulExtended: |
8433 | case OpSMulExtended: |
8434 | { |
8435 | uint32_t result_type = ops[0]; |
8436 | uint32_t result_id = ops[1]; |
8437 | uint32_t op0 = ops[2]; |
8438 | uint32_t op1 = ops[3]; |
8439 | auto &type = get<SPIRType>(id: result_type); |
8440 | emit_uninitialized_temporary_expression(type: result_type, id: result_id); |
8441 | |
8442 | statement(ts: to_expression(id: result_id), ts: "." , ts: to_member_name(type, index: 0), ts: " = " , |
8443 | ts: to_enclosed_unpacked_expression(id: op0), ts: " * " , ts: to_enclosed_unpacked_expression(id: op1), ts: ";" ); |
8444 | statement(ts: to_expression(id: result_id), ts: "." , ts: to_member_name(type, index: 1), ts: " = mulhi(" , |
8445 | ts: to_unpacked_expression(id: op0), ts: ", " , ts: to_unpacked_expression(id: op1), ts: ");" ); |
8446 | break; |
8447 | } |
8448 | |
8449 | case OpArrayLength: |
8450 | { |
8451 | auto &type = expression_type(id: ops[2]); |
8452 | uint32_t offset = type_struct_member_offset(type, index: ops[3]); |
8453 | uint32_t stride = type_struct_member_array_stride(type, index: ops[3]); |
8454 | |
8455 | auto expr = join(ts: "(" , ts: to_buffer_size_expression(id: ops[2]), ts: " - " , ts&: offset, ts: ") / " , ts&: stride); |
8456 | emit_op(result_type: ops[0], result_id: ops[1], rhs: expr, forward_rhs: true); |
8457 | break; |
8458 | } |
8459 | |
8460 | // SPV_INTEL_shader_integer_functions2 |
8461 | case OpUCountLeadingZerosINTEL: |
8462 | MSL_UFOP(clz); |
8463 | break; |
8464 | |
8465 | case OpUCountTrailingZerosINTEL: |
8466 | MSL_UFOP(ctz); |
8467 | break; |
8468 | |
8469 | case OpAbsISubINTEL: |
8470 | case OpAbsUSubINTEL: |
8471 | MSL_BFOP(absdiff); |
8472 | break; |
8473 | |
8474 | case OpIAddSatINTEL: |
8475 | case OpUAddSatINTEL: |
8476 | MSL_BFOP(addsat); |
8477 | break; |
8478 | |
8479 | case OpIAverageINTEL: |
8480 | case OpUAverageINTEL: |
8481 | MSL_BFOP(hadd); |
8482 | break; |
8483 | |
8484 | case OpIAverageRoundedINTEL: |
8485 | case OpUAverageRoundedINTEL: |
8486 | MSL_BFOP(rhadd); |
8487 | break; |
8488 | |
8489 | case OpISubSatINTEL: |
8490 | case OpUSubSatINTEL: |
8491 | MSL_BFOP(subsat); |
8492 | break; |
8493 | |
8494 | case OpIMul32x16INTEL: |
8495 | { |
8496 | uint32_t result_type = ops[0]; |
8497 | uint32_t id = ops[1]; |
8498 | uint32_t a = ops[2], b = ops[3]; |
8499 | bool forward = should_forward(id: a) && should_forward(id: b); |
8500 | emit_op(result_type, result_id: id, rhs: join(ts: "int(short(" , ts: to_unpacked_expression(id: a), ts: ")) * int(short(" , ts: to_unpacked_expression(id: b), ts: "))" ), forward_rhs: forward); |
8501 | inherit_expression_dependencies(dst: id, source: a); |
8502 | inherit_expression_dependencies(dst: id, source: b); |
8503 | break; |
8504 | } |
8505 | |
8506 | case OpUMul32x16INTEL: |
8507 | { |
8508 | uint32_t result_type = ops[0]; |
8509 | uint32_t id = ops[1]; |
8510 | uint32_t a = ops[2], b = ops[3]; |
8511 | bool forward = should_forward(id: a) && should_forward(id: b); |
8512 | emit_op(result_type, result_id: id, rhs: join(ts: "uint(ushort(" , ts: to_unpacked_expression(id: a), ts: ")) * uint(ushort(" , ts: to_unpacked_expression(id: b), ts: "))" ), forward_rhs: forward); |
8513 | inherit_expression_dependencies(dst: id, source: a); |
8514 | inherit_expression_dependencies(dst: id, source: b); |
8515 | break; |
8516 | } |
8517 | |
8518 | // SPV_EXT_demote_to_helper_invocation |
8519 | case OpDemoteToHelperInvocationEXT: |
8520 | if (!msl_options.supports_msl_version(major: 2, minor: 3)) |
8521 | SPIRV_CROSS_THROW("discard_fragment() does not formally have demote semantics until MSL 2.3." ); |
8522 | CompilerGLSL::emit_instruction(instr: instruction); |
8523 | break; |
8524 | |
8525 | case OpIsHelperInvocationEXT: |
8526 | if (msl_options.is_ios() && !msl_options.supports_msl_version(major: 2, minor: 3)) |
8527 | SPIRV_CROSS_THROW("simd_is_helper_thread() requires MSL 2.3 on iOS." ); |
8528 | else if (msl_options.is_macos() && !msl_options.supports_msl_version(major: 2, minor: 1)) |
8529 | SPIRV_CROSS_THROW("simd_is_helper_thread() requires MSL 2.1 on macOS." ); |
8530 | emit_op(result_type: ops[0], result_id: ops[1], rhs: "simd_is_helper_thread()" , forward_rhs: false); |
8531 | break; |
8532 | |
8533 | case OpBeginInvocationInterlockEXT: |
8534 | case OpEndInvocationInterlockEXT: |
8535 | if (!msl_options.supports_msl_version(major: 2, minor: 0)) |
8536 | SPIRV_CROSS_THROW("Raster order groups require MSL 2.0." ); |
8537 | break; // Nothing to do in the body |
8538 | |
8539 | case OpConvertUToAccelerationStructureKHR: |
8540 | SPIRV_CROSS_THROW("ConvertUToAccelerationStructure is not supported in MSL." ); |
8541 | case OpRayQueryGetIntersectionInstanceShaderBindingTableRecordOffsetKHR: |
8542 | SPIRV_CROSS_THROW("BindingTableRecordOffset is not supported in MSL." ); |
8543 | |
8544 | case OpRayQueryInitializeKHR: |
8545 | { |
8546 | flush_variable_declaration(id: ops[0]); |
8547 | |
8548 | statement(ts: to_expression(id: ops[0]), ts: ".reset(" , ts: "ray(" , ts: to_expression(id: ops[4]), ts: ", " , ts: to_expression(id: ops[6]), ts: ", " , |
8549 | ts: to_expression(id: ops[5]), ts: ", " , ts: to_expression(id: ops[7]), ts: "), " , ts: to_expression(id: ops[1]), |
8550 | ts: ", intersection_params());" ); |
8551 | break; |
8552 | } |
8553 | case OpRayQueryProceedKHR: |
8554 | { |
8555 | flush_variable_declaration(id: ops[0]); |
8556 | emit_op(result_type: ops[0], result_id: ops[1], rhs: join(ts: to_expression(id: ops[2]), ts: ".next()" ), forward_rhs: false); |
8557 | break; |
8558 | } |
8559 | #define MSL_RAY_QUERY_IS_CANDIDATE get<SPIRConstant>(ops[3]).scalar_i32() == 0 |
8560 | |
8561 | #define MSL_RAY_QUERY_GET_OP(op, msl_op) \ |
8562 | case OpRayQueryGet##op##KHR: \ |
8563 | flush_variable_declaration(ops[2]); \ |
8564 | emit_op(ops[0], ops[1], join(to_expression(ops[2]), ".get_" #msl_op "()"), false); \ |
8565 | break |
8566 | |
8567 | #define MSL_RAY_QUERY_OP_INNER2(op, msl_prefix, msl_op) \ |
8568 | case OpRayQueryGet##op##KHR: \ |
8569 | flush_variable_declaration(ops[2]); \ |
8570 | if (MSL_RAY_QUERY_IS_CANDIDATE) \ |
8571 | emit_op(ops[0], ops[1], join(to_expression(ops[2]), #msl_prefix "_candidate_" #msl_op "()"), false); \ |
8572 | else \ |
8573 | emit_op(ops[0], ops[1], join(to_expression(ops[2]), #msl_prefix "_committed_" #msl_op "()"), false); \ |
8574 | break |
8575 | |
8576 | #define MSL_RAY_QUERY_GET_OP2(op, msl_op) MSL_RAY_QUERY_OP_INNER2(op, .get, msl_op) |
8577 | #define MSL_RAY_QUERY_IS_OP2(op, msl_op) MSL_RAY_QUERY_OP_INNER2(op, .is, msl_op) |
8578 | |
8579 | MSL_RAY_QUERY_GET_OP(RayTMin, ray_min_distance); |
8580 | MSL_RAY_QUERY_GET_OP(WorldRayOrigin, world_space_ray_origin); |
8581 | MSL_RAY_QUERY_GET_OP(WorldRayDirection, world_space_ray_direction); |
8582 | MSL_RAY_QUERY_GET_OP2(IntersectionInstanceId, instance_id); |
8583 | MSL_RAY_QUERY_GET_OP2(IntersectionInstanceCustomIndex, user_instance_id); |
8584 | MSL_RAY_QUERY_GET_OP2(IntersectionBarycentrics, triangle_barycentric_coord); |
8585 | MSL_RAY_QUERY_GET_OP2(IntersectionPrimitiveIndex, primitive_id); |
8586 | MSL_RAY_QUERY_GET_OP2(IntersectionGeometryIndex, geometry_id); |
8587 | MSL_RAY_QUERY_GET_OP2(IntersectionObjectRayOrigin, ray_origin); |
8588 | MSL_RAY_QUERY_GET_OP2(IntersectionObjectRayDirection, ray_direction); |
8589 | MSL_RAY_QUERY_GET_OP2(IntersectionObjectToWorld, object_to_world_transform); |
8590 | MSL_RAY_QUERY_GET_OP2(IntersectionWorldToObject, world_to_object_transform); |
8591 | MSL_RAY_QUERY_IS_OP2(IntersectionFrontFace, triangle_front_facing); |
8592 | |
8593 | case OpRayQueryGetIntersectionTypeKHR: |
8594 | flush_variable_declaration(id: ops[2]); |
8595 | if (MSL_RAY_QUERY_IS_CANDIDATE) |
8596 | emit_op(result_type: ops[0], result_id: ops[1], rhs: join(ts: "uint(" , ts: to_expression(id: ops[2]), ts: ".get_candidate_intersection_type()) - 1" ), |
8597 | forward_rhs: false); |
8598 | else |
8599 | emit_op(result_type: ops[0], result_id: ops[1], rhs: join(ts: "uint(" , ts: to_expression(id: ops[2]), ts: ".get_committed_intersection_type())" ), forward_rhs: false); |
8600 | break; |
8601 | case OpRayQueryGetIntersectionTKHR: |
8602 | flush_variable_declaration(id: ops[2]); |
8603 | if (MSL_RAY_QUERY_IS_CANDIDATE) |
8604 | emit_op(result_type: ops[0], result_id: ops[1], rhs: join(ts: to_expression(id: ops[2]), ts: ".get_candidate_triangle_distance()" ), forward_rhs: false); |
8605 | else |
8606 | emit_op(result_type: ops[0], result_id: ops[1], rhs: join(ts: to_expression(id: ops[2]), ts: ".get_committed_distance()" ), forward_rhs: false); |
8607 | break; |
8608 | case OpRayQueryGetIntersectionCandidateAABBOpaqueKHR: |
8609 | { |
8610 | flush_variable_declaration(id: ops[0]); |
8611 | emit_op(result_type: ops[0], result_id: ops[1], rhs: join(ts: to_expression(id: ops[2]), ts: ".is_candidate_non_opaque_bounding_box()" ), forward_rhs: false); |
8612 | break; |
8613 | } |
8614 | case OpRayQueryConfirmIntersectionKHR: |
8615 | flush_variable_declaration(id: ops[0]); |
8616 | statement(ts: to_expression(id: ops[0]), ts: ".commit_triangle_intersection();" ); |
8617 | break; |
8618 | case OpRayQueryGenerateIntersectionKHR: |
8619 | flush_variable_declaration(id: ops[0]); |
8620 | statement(ts: to_expression(id: ops[0]), ts: ".commit_bounding_box_intersection(" , ts: to_expression(id: ops[1]), ts: ");" ); |
8621 | break; |
8622 | case OpRayQueryTerminateKHR: |
8623 | flush_variable_declaration(id: ops[0]); |
8624 | statement(ts: to_expression(id: ops[0]), ts: ".abort();" ); |
8625 | break; |
8626 | #undef MSL_RAY_QUERY_GET_OP |
8627 | #undef MSL_RAY_QUERY_IS_CANDIDATE |
8628 | #undef MSL_RAY_QUERY_IS_OP2 |
8629 | #undef MSL_RAY_QUERY_GET_OP2 |
8630 | #undef MSL_RAY_QUERY_OP_INNER2 |
8631 | default: |
8632 | CompilerGLSL::emit_instruction(instr: instruction); |
8633 | break; |
8634 | } |
8635 | |
8636 | previous_instruction_opcode = opcode; |
8637 | } |
8638 | |
8639 | void CompilerMSL::emit_texture_op(const Instruction &i, bool sparse) |
8640 | { |
8641 | if (sparse) |
8642 | SPIRV_CROSS_THROW("Sparse feedback not yet supported in MSL." ); |
8643 | |
8644 | if (msl_options.use_framebuffer_fetch_subpasses) |
8645 | { |
8646 | auto *ops = stream(instr: i); |
8647 | |
8648 | uint32_t result_type_id = ops[0]; |
8649 | uint32_t id = ops[1]; |
8650 | uint32_t img = ops[2]; |
8651 | |
8652 | auto &type = expression_type(id: img); |
8653 | auto &imgtype = get<SPIRType>(id: type.self); |
8654 | |
8655 | // Use Metal's native frame-buffer fetch API for subpass inputs. |
8656 | if (imgtype.image.dim == DimSubpassData) |
8657 | { |
8658 | // Subpass inputs cannot be invalidated, |
8659 | // so just forward the expression directly. |
8660 | string expr = to_expression(id: img); |
8661 | emit_op(result_type: result_type_id, result_id: id, rhs: expr, forward_rhs: true); |
8662 | return; |
8663 | } |
8664 | } |
8665 | |
8666 | // Fallback to default implementation |
8667 | CompilerGLSL::emit_texture_op(i, sparse); |
8668 | } |
8669 | |
8670 | void CompilerMSL::emit_barrier(uint32_t id_exe_scope, uint32_t id_mem_scope, uint32_t id_mem_sem) |
8671 | { |
8672 | if (get_execution_model() != ExecutionModelGLCompute && get_execution_model() != ExecutionModelTessellationControl) |
8673 | return; |
8674 | |
8675 | uint32_t exe_scope = id_exe_scope ? evaluate_constant_u32(id: id_exe_scope) : uint32_t(ScopeInvocation); |
8676 | uint32_t mem_scope = id_mem_scope ? evaluate_constant_u32(id: id_mem_scope) : uint32_t(ScopeInvocation); |
8677 | // Use the wider of the two scopes (smaller value) |
8678 | exe_scope = min(a: exe_scope, b: mem_scope); |
8679 | |
8680 | if (msl_options.emulate_subgroups && exe_scope >= ScopeSubgroup && !id_mem_sem) |
8681 | // In this case, we assume a "subgroup" size of 1. The barrier, then, is a noop. |
8682 | return; |
8683 | |
8684 | string bar_stmt; |
8685 | if ((msl_options.is_ios() && msl_options.supports_msl_version(major: 1, minor: 2)) || msl_options.supports_msl_version(major: 2)) |
8686 | bar_stmt = exe_scope < ScopeSubgroup ? "threadgroup_barrier" : "simdgroup_barrier" ; |
8687 | else |
8688 | bar_stmt = "threadgroup_barrier" ; |
8689 | bar_stmt += "(" ; |
8690 | |
8691 | uint32_t mem_sem = id_mem_sem ? evaluate_constant_u32(id: id_mem_sem) : uint32_t(MemorySemanticsMaskNone); |
8692 | |
8693 | // Use the | operator to combine flags if we can. |
8694 | if (msl_options.supports_msl_version(major: 1, minor: 2)) |
8695 | { |
8696 | string mem_flags = "" ; |
8697 | // For tesc shaders, this also affects objects in the Output storage class. |
8698 | // Since in Metal, these are placed in a device buffer, we have to sync device memory here. |
8699 | if (get_execution_model() == ExecutionModelTessellationControl || |
8700 | (mem_sem & (MemorySemanticsUniformMemoryMask | MemorySemanticsCrossWorkgroupMemoryMask))) |
8701 | mem_flags += "mem_flags::mem_device" ; |
8702 | |
8703 | // Fix tessellation patch function processing |
8704 | if (get_execution_model() == ExecutionModelTessellationControl || |
8705 | (mem_sem & (MemorySemanticsSubgroupMemoryMask | MemorySemanticsWorkgroupMemoryMask))) |
8706 | { |
8707 | if (!mem_flags.empty()) |
8708 | mem_flags += " | " ; |
8709 | mem_flags += "mem_flags::mem_threadgroup" ; |
8710 | } |
8711 | if (mem_sem & MemorySemanticsImageMemoryMask) |
8712 | { |
8713 | if (!mem_flags.empty()) |
8714 | mem_flags += " | " ; |
8715 | mem_flags += "mem_flags::mem_texture" ; |
8716 | } |
8717 | |
8718 | if (mem_flags.empty()) |
8719 | mem_flags = "mem_flags::mem_none" ; |
8720 | |
8721 | bar_stmt += mem_flags; |
8722 | } |
8723 | else |
8724 | { |
8725 | if ((mem_sem & (MemorySemanticsUniformMemoryMask | MemorySemanticsCrossWorkgroupMemoryMask)) && |
8726 | (mem_sem & (MemorySemanticsSubgroupMemoryMask | MemorySemanticsWorkgroupMemoryMask))) |
8727 | bar_stmt += "mem_flags::mem_device_and_threadgroup" ; |
8728 | else if (mem_sem & (MemorySemanticsUniformMemoryMask | MemorySemanticsCrossWorkgroupMemoryMask)) |
8729 | bar_stmt += "mem_flags::mem_device" ; |
8730 | else if (mem_sem & (MemorySemanticsSubgroupMemoryMask | MemorySemanticsWorkgroupMemoryMask)) |
8731 | bar_stmt += "mem_flags::mem_threadgroup" ; |
8732 | else if (mem_sem & MemorySemanticsImageMemoryMask) |
8733 | bar_stmt += "mem_flags::mem_texture" ; |
8734 | else |
8735 | bar_stmt += "mem_flags::mem_none" ; |
8736 | } |
8737 | |
8738 | bar_stmt += ");" ; |
8739 | |
8740 | statement(ts&: bar_stmt); |
8741 | |
8742 | assert(current_emitting_block); |
8743 | flush_control_dependent_expressions(block: current_emitting_block->self); |
8744 | flush_all_active_variables(); |
8745 | } |
8746 | |
8747 | static bool storage_class_array_is_thread(StorageClass storage) |
8748 | { |
8749 | switch (storage) |
8750 | { |
8751 | case StorageClassInput: |
8752 | case StorageClassOutput: |
8753 | case StorageClassGeneric: |
8754 | case StorageClassFunction: |
8755 | case StorageClassPrivate: |
8756 | return true; |
8757 | |
8758 | default: |
8759 | return false; |
8760 | } |
8761 | } |
8762 | |
8763 | void CompilerMSL::emit_array_copy(const string &lhs, uint32_t lhs_id, uint32_t rhs_id, |
8764 | StorageClass lhs_storage, StorageClass rhs_storage) |
8765 | { |
8766 | // Allow Metal to use the array<T> template to make arrays a value type. |
8767 | // This, however, cannot be used for threadgroup address specifiers, so consider the custom array copy as fallback. |
8768 | bool lhs_is_thread_storage = storage_class_array_is_thread(storage: lhs_storage); |
8769 | bool rhs_is_thread_storage = storage_class_array_is_thread(storage: rhs_storage); |
8770 | |
8771 | bool lhs_is_array_template = lhs_is_thread_storage; |
8772 | bool rhs_is_array_template = rhs_is_thread_storage; |
8773 | |
8774 | // Special considerations for stage IO variables. |
8775 | // If the variable is actually backed by non-user visible device storage, we use array templates for those. |
8776 | // |
8777 | // Another special consideration is given to thread local variables which happen to have Offset decorations |
8778 | // applied to them. Block-like types do not use array templates, so we need to force POD path if we detect |
8779 | // these scenarios. This check isn't perfect since it would be technically possible to mix and match these things, |
8780 | // and for a fully correct solution we might have to track array template state through access chains as well, |
8781 | // but for all reasonable use cases, this should suffice. |
8782 | // This special case should also only apply to Function/Private storage classes. |
8783 | // We should not check backing variable for temporaries. |
8784 | auto *lhs_var = maybe_get_backing_variable(chain: lhs_id); |
8785 | if (lhs_var && lhs_storage == StorageClassStorageBuffer && storage_class_array_is_thread(storage: lhs_var->storage)) |
8786 | lhs_is_array_template = true; |
8787 | else if (lhs_var && (lhs_storage == StorageClassFunction || lhs_storage == StorageClassPrivate) && |
8788 | type_is_block_like(type: get<SPIRType>(id: lhs_var->basetype))) |
8789 | lhs_is_array_template = false; |
8790 | |
8791 | auto *rhs_var = maybe_get_backing_variable(chain: rhs_id); |
8792 | if (rhs_var && rhs_storage == StorageClassStorageBuffer && storage_class_array_is_thread(storage: rhs_var->storage)) |
8793 | rhs_is_array_template = true; |
8794 | else if (rhs_var && (rhs_storage == StorageClassFunction || rhs_storage == StorageClassPrivate) && |
8795 | type_is_block_like(type: get<SPIRType>(id: rhs_var->basetype))) |
8796 | rhs_is_array_template = false; |
8797 | |
8798 | // If threadgroup storage qualifiers are *not* used: |
8799 | // Avoid spvCopy* wrapper functions; Otherwise, spvUnsafeArray<> template cannot be used with that storage qualifier. |
8800 | if (lhs_is_array_template && rhs_is_array_template && !using_builtin_array()) |
8801 | { |
8802 | statement(ts: lhs, ts: " = " , ts: to_expression(id: rhs_id), ts: ";" ); |
8803 | } |
8804 | else |
8805 | { |
8806 | // Assignment from an array initializer is fine. |
8807 | auto &type = expression_type(id: rhs_id); |
8808 | auto *var = maybe_get_backing_variable(chain: rhs_id); |
8809 | |
8810 | // Unfortunately, we cannot template on address space in MSL, |
8811 | // so explicit address space redirection it is ... |
8812 | bool is_constant = false; |
8813 | if (ir.ids[rhs_id].get_type() == TypeConstant) |
8814 | { |
8815 | is_constant = true; |
8816 | } |
8817 | else if (var && var->remapped_variable && var->statically_assigned && |
8818 | ir.ids[var->static_expression].get_type() == TypeConstant) |
8819 | { |
8820 | is_constant = true; |
8821 | } |
8822 | else if (rhs_storage == StorageClassUniform || rhs_storage == StorageClassUniformConstant) |
8823 | { |
8824 | is_constant = true; |
8825 | } |
8826 | |
8827 | // For the case where we have OpLoad triggering an array copy, |
8828 | // we cannot easily detect this case ahead of time since it's |
8829 | // context dependent. We might have to force a recompile here |
8830 | // if this is the only use of array copies in our shader. |
8831 | if (type.array.size() > 1) |
8832 | { |
8833 | if (type.array.size() > kArrayCopyMultidimMax) |
8834 | SPIRV_CROSS_THROW("Cannot support this many dimensions for arrays of arrays." ); |
8835 | auto func = static_cast<SPVFuncImpl>(SPVFuncImplArrayCopyMultidimBase + type.array.size()); |
8836 | add_spv_func_and_recompile(spv_func: func); |
8837 | } |
8838 | else |
8839 | add_spv_func_and_recompile(spv_func: SPVFuncImplArrayCopy); |
8840 | |
8841 | const char *tag = nullptr; |
8842 | if (lhs_is_thread_storage && is_constant) |
8843 | tag = "FromConstantToStack" ; |
8844 | else if (lhs_storage == StorageClassWorkgroup && is_constant) |
8845 | tag = "FromConstantToThreadGroup" ; |
8846 | else if (lhs_is_thread_storage && rhs_is_thread_storage) |
8847 | tag = "FromStackToStack" ; |
8848 | else if (lhs_storage == StorageClassWorkgroup && rhs_is_thread_storage) |
8849 | tag = "FromStackToThreadGroup" ; |
8850 | else if (lhs_is_thread_storage && rhs_storage == StorageClassWorkgroup) |
8851 | tag = "FromThreadGroupToStack" ; |
8852 | else if (lhs_storage == StorageClassWorkgroup && rhs_storage == StorageClassWorkgroup) |
8853 | tag = "FromThreadGroupToThreadGroup" ; |
8854 | else if (lhs_storage == StorageClassStorageBuffer && rhs_storage == StorageClassStorageBuffer) |
8855 | tag = "FromDeviceToDevice" ; |
8856 | else if (lhs_storage == StorageClassStorageBuffer && is_constant) |
8857 | tag = "FromConstantToDevice" ; |
8858 | else if (lhs_storage == StorageClassStorageBuffer && rhs_storage == StorageClassWorkgroup) |
8859 | tag = "FromThreadGroupToDevice" ; |
8860 | else if (lhs_storage == StorageClassStorageBuffer && rhs_is_thread_storage) |
8861 | tag = "FromStackToDevice" ; |
8862 | else if (lhs_storage == StorageClassWorkgroup && rhs_storage == StorageClassStorageBuffer) |
8863 | tag = "FromDeviceToThreadGroup" ; |
8864 | else if (lhs_is_thread_storage && rhs_storage == StorageClassStorageBuffer) |
8865 | tag = "FromDeviceToStack" ; |
8866 | else |
8867 | SPIRV_CROSS_THROW("Unknown storage class used for copying arrays." ); |
8868 | |
8869 | // Pass internal array of spvUnsafeArray<> into wrapper functions |
8870 | if (lhs_is_array_template && rhs_is_array_template && !msl_options.force_native_arrays) |
8871 | statement(ts: "spvArrayCopy" , ts&: tag, ts: type.array.size(), ts: "(" , ts: lhs, ts: ".elements, " , ts: to_expression(id: rhs_id), ts: ".elements);" ); |
8872 | if (lhs_is_array_template && !msl_options.force_native_arrays) |
8873 | statement(ts: "spvArrayCopy" , ts&: tag, ts: type.array.size(), ts: "(" , ts: lhs, ts: ".elements, " , ts: to_expression(id: rhs_id), ts: ");" ); |
8874 | else if (rhs_is_array_template && !msl_options.force_native_arrays) |
8875 | statement(ts: "spvArrayCopy" , ts&: tag, ts: type.array.size(), ts: "(" , ts: lhs, ts: ", " , ts: to_expression(id: rhs_id), ts: ".elements);" ); |
8876 | else |
8877 | statement(ts: "spvArrayCopy" , ts&: tag, ts: type.array.size(), ts: "(" , ts: lhs, ts: ", " , ts: to_expression(id: rhs_id), ts: ");" ); |
8878 | } |
8879 | } |
8880 | |
8881 | uint32_t CompilerMSL::get_physical_tess_level_array_size(spv::BuiltIn builtin) const |
8882 | { |
8883 | if (get_execution_mode_bitset().get(bit: ExecutionModeTriangles)) |
8884 | return builtin == BuiltInTessLevelInner ? 1 : 3; |
8885 | else |
8886 | return builtin == BuiltInTessLevelInner ? 2 : 4; |
8887 | } |
8888 | |
8889 | // Since MSL does not allow arrays to be copied via simple variable assignment, |
8890 | // if the LHS and RHS represent an assignment of an entire array, it must be |
8891 | // implemented by calling an array copy function. |
8892 | // Returns whether the struct assignment was emitted. |
8893 | bool CompilerMSL::maybe_emit_array_assignment(uint32_t id_lhs, uint32_t id_rhs) |
8894 | { |
8895 | // We only care about assignments of an entire array |
8896 | auto &type = expression_type(id: id_rhs); |
8897 | if (type.array.size() == 0) |
8898 | return false; |
8899 | |
8900 | auto *var = maybe_get<SPIRVariable>(id: id_lhs); |
8901 | |
8902 | // Is this a remapped, static constant? Don't do anything. |
8903 | if (var && var->remapped_variable && var->statically_assigned) |
8904 | return true; |
8905 | |
8906 | if (ir.ids[id_rhs].get_type() == TypeConstant && var && var->deferred_declaration) |
8907 | { |
8908 | // Special case, if we end up declaring a variable when assigning the constant array, |
8909 | // we can avoid the copy by directly assigning the constant expression. |
8910 | // This is likely necessary to be able to use a variable as a true look-up table, as it is unlikely |
8911 | // the compiler will be able to optimize the spvArrayCopy() into a constant LUT. |
8912 | // After a variable has been declared, we can no longer assign constant arrays in MSL unfortunately. |
8913 | statement(ts: to_expression(id: id_lhs), ts: " = " , ts: constant_expression(c: get<SPIRConstant>(id: id_rhs)), ts: ";" ); |
8914 | return true; |
8915 | } |
8916 | |
8917 | if (get_execution_model() == ExecutionModelTessellationControl && |
8918 | has_decoration(id: id_lhs, decoration: DecorationBuiltIn)) |
8919 | { |
8920 | auto builtin = BuiltIn(get_decoration(id: id_lhs, decoration: DecorationBuiltIn)); |
8921 | // Need to manually unroll the array store. |
8922 | if (builtin == BuiltInTessLevelInner || builtin == BuiltInTessLevelOuter) |
8923 | { |
8924 | uint32_t array_size = get_physical_tess_level_array_size(builtin); |
8925 | if (array_size == 1) |
8926 | statement(ts: to_expression(id: id_lhs), ts: " = half(" , ts: to_expression(id: id_rhs), ts: "[0]);" ); |
8927 | else |
8928 | { |
8929 | for (uint32_t i = 0; i < array_size; i++) |
8930 | statement(ts: to_expression(id: id_lhs), ts: "[" , ts&: i, ts: "] = half(" , ts: to_expression(id: id_rhs), ts: "[" , ts&: i, ts: "]);" ); |
8931 | } |
8932 | return true; |
8933 | } |
8934 | } |
8935 | |
8936 | // Ensure the LHS variable has been declared |
8937 | auto *p_v_lhs = maybe_get_backing_variable(chain: id_lhs); |
8938 | if (p_v_lhs) |
8939 | flush_variable_declaration(id: p_v_lhs->self); |
8940 | |
8941 | auto lhs_storage = get_expression_effective_storage_class(ptr: id_lhs); |
8942 | auto rhs_storage = get_expression_effective_storage_class(ptr: id_rhs); |
8943 | emit_array_copy(lhs: to_expression(id: id_lhs), lhs_id: id_lhs, rhs_id: id_rhs, lhs_storage, rhs_storage); |
8944 | register_write(chain: id_lhs); |
8945 | |
8946 | return true; |
8947 | } |
8948 | |
8949 | // Emits one of the atomic functions. In MSL, the atomic functions operate on pointers |
8950 | void CompilerMSL::emit_atomic_func_op(uint32_t result_type, uint32_t result_id, const char *op, Op opcode, |
8951 | uint32_t mem_order_1, uint32_t mem_order_2, bool has_mem_order_2, uint32_t obj, uint32_t op1, |
8952 | bool op1_is_pointer, bool op1_is_literal, uint32_t op2) |
8953 | { |
8954 | string exp = string(op) + "(" ; |
8955 | |
8956 | auto &type = get_pointee_type(type: expression_type(id: obj)); |
8957 | auto expected_type = type.basetype; |
8958 | if (opcode == OpAtomicUMax || opcode == OpAtomicUMin) |
8959 | expected_type = to_unsigned_basetype(width: type.width); |
8960 | else if (opcode == OpAtomicSMax || opcode == OpAtomicSMin) |
8961 | expected_type = to_signed_basetype(width: type.width); |
8962 | |
8963 | auto remapped_type = type; |
8964 | remapped_type.basetype = expected_type; |
8965 | |
8966 | exp += "(" ; |
8967 | auto *var = maybe_get_backing_variable(chain: obj); |
8968 | if (!var) |
8969 | SPIRV_CROSS_THROW("No backing variable for atomic operation." ); |
8970 | |
8971 | // Emulate texture2D atomic operations |
8972 | const auto &res_type = get<SPIRType>(id: var->basetype); |
8973 | if (res_type.storage == StorageClassUniformConstant && res_type.basetype == SPIRType::Image) |
8974 | { |
8975 | exp += "device" ; |
8976 | } |
8977 | else |
8978 | { |
8979 | exp += get_argument_address_space(argument: *var); |
8980 | } |
8981 | |
8982 | exp += " atomic_" ; |
8983 | // For signed and unsigned min/max, we can signal this through the pointer type. |
8984 | // There is no other way, since C++ does not have explicit signage for atomics. |
8985 | exp += type_to_glsl(type: remapped_type); |
8986 | exp += "*)" ; |
8987 | |
8988 | exp += "&" ; |
8989 | exp += to_enclosed_expression(id: obj); |
8990 | |
8991 | bool is_atomic_compare_exchange_strong = op1_is_pointer && op1; |
8992 | |
8993 | if (is_atomic_compare_exchange_strong) |
8994 | { |
8995 | assert(strcmp(op, "atomic_compare_exchange_weak_explicit" ) == 0); |
8996 | assert(op2); |
8997 | assert(has_mem_order_2); |
8998 | exp += ", &" ; |
8999 | exp += to_name(id: result_id); |
9000 | exp += ", " ; |
9001 | exp += to_expression(id: op2); |
9002 | exp += ", " ; |
9003 | exp += get_memory_order(spv_mem_sem: mem_order_1); |
9004 | exp += ", " ; |
9005 | exp += get_memory_order(spv_mem_sem: mem_order_2); |
9006 | exp += ")" ; |
9007 | |
9008 | // MSL only supports the weak atomic compare exchange, so emit a CAS loop here. |
9009 | // The MSL function returns false if the atomic write fails OR the comparison test fails, |
9010 | // so we must validate that it wasn't the comparison test that failed before continuing |
9011 | // the CAS loop, otherwise it will loop infinitely, with the comparison test always failing. |
9012 | // The function updates the comparitor value from the memory value, so the additional |
9013 | // comparison test evaluates the memory value against the expected value. |
9014 | emit_uninitialized_temporary_expression(type: result_type, id: result_id); |
9015 | statement(ts: "do" ); |
9016 | begin_scope(); |
9017 | statement(ts: to_name(id: result_id), ts: " = " , ts: to_expression(id: op1), ts: ";" ); |
9018 | end_scope_decl(decl: join(ts: "while (!" , ts&: exp, ts: " && " , ts: to_name(id: result_id), ts: " == " , ts: to_enclosed_expression(id: op1), ts: ")" )); |
9019 | } |
9020 | else |
9021 | { |
9022 | assert(strcmp(op, "atomic_compare_exchange_weak_explicit" ) != 0); |
9023 | if (op1) |
9024 | { |
9025 | if (op1_is_literal) |
9026 | exp += join(ts: ", " , ts&: op1); |
9027 | else |
9028 | exp += ", " + bitcast_expression(target_type: expected_type, arg: op1); |
9029 | } |
9030 | if (op2) |
9031 | exp += ", " + to_expression(id: op2); |
9032 | |
9033 | exp += string(", " ) + get_memory_order(spv_mem_sem: mem_order_1); |
9034 | if (has_mem_order_2) |
9035 | exp += string(", " ) + get_memory_order(spv_mem_sem: mem_order_2); |
9036 | |
9037 | exp += ")" ; |
9038 | |
9039 | if (expected_type != type.basetype) |
9040 | exp = bitcast_expression(target_type: type, expr_type: expected_type, expr: exp); |
9041 | |
9042 | if (strcmp(s1: op, s2: "atomic_store_explicit" ) != 0) |
9043 | emit_op(result_type, result_id, rhs: exp, forward_rhs: false); |
9044 | else |
9045 | statement(ts&: exp, ts: ";" ); |
9046 | } |
9047 | |
9048 | flush_all_atomic_capable_variables(); |
9049 | } |
9050 | |
9051 | // Metal only supports relaxed memory order for now |
9052 | const char *CompilerMSL::get_memory_order(uint32_t) |
9053 | { |
9054 | return "memory_order_relaxed" ; |
9055 | } |
9056 | |
9057 | // Override for MSL-specific extension syntax instructions. |
9058 | // In some cases, deliberately select either the fast or precise versions of the MSL functions to match Vulkan math precision results. |
9059 | void CompilerMSL::emit_glsl_op(uint32_t result_type, uint32_t id, uint32_t eop, const uint32_t *args, uint32_t count) |
9060 | { |
9061 | auto op = static_cast<GLSLstd450>(eop); |
9062 | |
9063 | // If we need to do implicit bitcasts, make sure we do it with the correct type. |
9064 | uint32_t integer_width = get_integer_width_for_glsl_instruction(op, arguments: args, length: count); |
9065 | auto int_type = to_signed_basetype(width: integer_width); |
9066 | auto uint_type = to_unsigned_basetype(width: integer_width); |
9067 | |
9068 | op = get_remapped_glsl_op(std450_op: op); |
9069 | |
9070 | switch (op) |
9071 | { |
9072 | case GLSLstd450Sinh: |
9073 | emit_unary_func_op(result_type, result_id: id, op0: args[0], op: "fast::sinh" ); |
9074 | break; |
9075 | case GLSLstd450Cosh: |
9076 | emit_unary_func_op(result_type, result_id: id, op0: args[0], op: "fast::cosh" ); |
9077 | break; |
9078 | case GLSLstd450Tanh: |
9079 | emit_unary_func_op(result_type, result_id: id, op0: args[0], op: "precise::tanh" ); |
9080 | break; |
9081 | case GLSLstd450Atan2: |
9082 | emit_binary_func_op(result_type, result_id: id, op0: args[0], op1: args[1], op: "precise::atan2" ); |
9083 | break; |
9084 | case GLSLstd450InverseSqrt: |
9085 | emit_unary_func_op(result_type, result_id: id, op0: args[0], op: "rsqrt" ); |
9086 | break; |
9087 | case GLSLstd450RoundEven: |
9088 | emit_unary_func_op(result_type, result_id: id, op0: args[0], op: "rint" ); |
9089 | break; |
9090 | |
9091 | case GLSLstd450FindILsb: |
9092 | { |
9093 | // In this template version of findLSB, we return T. |
9094 | auto basetype = expression_type(id: args[0]).basetype; |
9095 | emit_unary_func_op_cast(result_type, result_id: id, op0: args[0], op: "spvFindLSB" , input_type: basetype, expected_result_type: basetype); |
9096 | break; |
9097 | } |
9098 | |
9099 | case GLSLstd450FindSMsb: |
9100 | emit_unary_func_op_cast(result_type, result_id: id, op0: args[0], op: "spvFindSMSB" , input_type: int_type, expected_result_type: int_type); |
9101 | break; |
9102 | |
9103 | case GLSLstd450FindUMsb: |
9104 | emit_unary_func_op_cast(result_type, result_id: id, op0: args[0], op: "spvFindUMSB" , input_type: uint_type, expected_result_type: uint_type); |
9105 | break; |
9106 | |
9107 | case GLSLstd450PackSnorm4x8: |
9108 | emit_unary_func_op(result_type, result_id: id, op0: args[0], op: "pack_float_to_snorm4x8" ); |
9109 | break; |
9110 | case GLSLstd450PackUnorm4x8: |
9111 | emit_unary_func_op(result_type, result_id: id, op0: args[0], op: "pack_float_to_unorm4x8" ); |
9112 | break; |
9113 | case GLSLstd450PackSnorm2x16: |
9114 | emit_unary_func_op(result_type, result_id: id, op0: args[0], op: "pack_float_to_snorm2x16" ); |
9115 | break; |
9116 | case GLSLstd450PackUnorm2x16: |
9117 | emit_unary_func_op(result_type, result_id: id, op0: args[0], op: "pack_float_to_unorm2x16" ); |
9118 | break; |
9119 | |
9120 | case GLSLstd450PackHalf2x16: |
9121 | { |
9122 | auto expr = join(ts: "as_type<uint>(half2(" , ts: to_expression(id: args[0]), ts: "))" ); |
9123 | emit_op(result_type, result_id: id, rhs: expr, forward_rhs: should_forward(id: args[0])); |
9124 | inherit_expression_dependencies(dst: id, source: args[0]); |
9125 | break; |
9126 | } |
9127 | |
9128 | case GLSLstd450UnpackSnorm4x8: |
9129 | emit_unary_func_op(result_type, result_id: id, op0: args[0], op: "unpack_snorm4x8_to_float" ); |
9130 | break; |
9131 | case GLSLstd450UnpackUnorm4x8: |
9132 | emit_unary_func_op(result_type, result_id: id, op0: args[0], op: "unpack_unorm4x8_to_float" ); |
9133 | break; |
9134 | case GLSLstd450UnpackSnorm2x16: |
9135 | emit_unary_func_op(result_type, result_id: id, op0: args[0], op: "unpack_snorm2x16_to_float" ); |
9136 | break; |
9137 | case GLSLstd450UnpackUnorm2x16: |
9138 | emit_unary_func_op(result_type, result_id: id, op0: args[0], op: "unpack_unorm2x16_to_float" ); |
9139 | break; |
9140 | |
9141 | case GLSLstd450UnpackHalf2x16: |
9142 | { |
9143 | auto expr = join(ts: "float2(as_type<half2>(" , ts: to_expression(id: args[0]), ts: "))" ); |
9144 | emit_op(result_type, result_id: id, rhs: expr, forward_rhs: should_forward(id: args[0])); |
9145 | inherit_expression_dependencies(dst: id, source: args[0]); |
9146 | break; |
9147 | } |
9148 | |
9149 | case GLSLstd450PackDouble2x32: |
9150 | emit_unary_func_op(result_type, result_id: id, op0: args[0], op: "unsupported_GLSLstd450PackDouble2x32" ); // Currently unsupported |
9151 | break; |
9152 | case GLSLstd450UnpackDouble2x32: |
9153 | emit_unary_func_op(result_type, result_id: id, op0: args[0], op: "unsupported_GLSLstd450UnpackDouble2x32" ); // Currently unsupported |
9154 | break; |
9155 | |
9156 | case GLSLstd450MatrixInverse: |
9157 | { |
9158 | auto &mat_type = get<SPIRType>(id: result_type); |
9159 | switch (mat_type.columns) |
9160 | { |
9161 | case 2: |
9162 | emit_unary_func_op(result_type, result_id: id, op0: args[0], op: "spvInverse2x2" ); |
9163 | break; |
9164 | case 3: |
9165 | emit_unary_func_op(result_type, result_id: id, op0: args[0], op: "spvInverse3x3" ); |
9166 | break; |
9167 | case 4: |
9168 | emit_unary_func_op(result_type, result_id: id, op0: args[0], op: "spvInverse4x4" ); |
9169 | break; |
9170 | default: |
9171 | break; |
9172 | } |
9173 | break; |
9174 | } |
9175 | |
9176 | case GLSLstd450FMin: |
9177 | // If the result type isn't float, don't bother calling the specific |
9178 | // precise::/fast:: version. Metal doesn't have those for half and |
9179 | // double types. |
9180 | if (get<SPIRType>(id: result_type).basetype != SPIRType::Float) |
9181 | emit_binary_func_op(result_type, result_id: id, op0: args[0], op1: args[1], op: "min" ); |
9182 | else |
9183 | emit_binary_func_op(result_type, result_id: id, op0: args[0], op1: args[1], op: "fast::min" ); |
9184 | break; |
9185 | |
9186 | case GLSLstd450FMax: |
9187 | if (get<SPIRType>(id: result_type).basetype != SPIRType::Float) |
9188 | emit_binary_func_op(result_type, result_id: id, op0: args[0], op1: args[1], op: "max" ); |
9189 | else |
9190 | emit_binary_func_op(result_type, result_id: id, op0: args[0], op1: args[1], op: "fast::max" ); |
9191 | break; |
9192 | |
9193 | case GLSLstd450FClamp: |
9194 | // TODO: If args[1] is 0 and args[2] is 1, emit a saturate() call. |
9195 | if (get<SPIRType>(id: result_type).basetype != SPIRType::Float) |
9196 | emit_trinary_func_op(result_type, result_id: id, op0: args[0], op1: args[1], op2: args[2], op: "clamp" ); |
9197 | else |
9198 | emit_trinary_func_op(result_type, result_id: id, op0: args[0], op1: args[1], op2: args[2], op: "fast::clamp" ); |
9199 | break; |
9200 | |
9201 | case GLSLstd450NMin: |
9202 | if (get<SPIRType>(id: result_type).basetype != SPIRType::Float) |
9203 | emit_binary_func_op(result_type, result_id: id, op0: args[0], op1: args[1], op: "min" ); |
9204 | else |
9205 | emit_binary_func_op(result_type, result_id: id, op0: args[0], op1: args[1], op: "precise::min" ); |
9206 | break; |
9207 | |
9208 | case GLSLstd450NMax: |
9209 | if (get<SPIRType>(id: result_type).basetype != SPIRType::Float) |
9210 | emit_binary_func_op(result_type, result_id: id, op0: args[0], op1: args[1], op: "max" ); |
9211 | else |
9212 | emit_binary_func_op(result_type, result_id: id, op0: args[0], op1: args[1], op: "precise::max" ); |
9213 | break; |
9214 | |
9215 | case GLSLstd450NClamp: |
9216 | // TODO: If args[1] is 0 and args[2] is 1, emit a saturate() call. |
9217 | if (get<SPIRType>(id: result_type).basetype != SPIRType::Float) |
9218 | emit_trinary_func_op(result_type, result_id: id, op0: args[0], op1: args[1], op2: args[2], op: "clamp" ); |
9219 | else |
9220 | emit_trinary_func_op(result_type, result_id: id, op0: args[0], op1: args[1], op2: args[2], op: "precise::clamp" ); |
9221 | break; |
9222 | |
9223 | case GLSLstd450InterpolateAtCentroid: |
9224 | { |
9225 | // We can't just emit the expression normally, because the qualified name contains a call to the default |
9226 | // interpolate method, or refers to a local variable. We saved the interface index we need; use it to construct |
9227 | // the base for the method call. |
9228 | uint32_t interface_index = get_extended_decoration(id: args[0], decoration: SPIRVCrossDecorationInterfaceMemberIndex); |
9229 | string component; |
9230 | if (has_extended_decoration(id: args[0], decoration: SPIRVCrossDecorationInterpolantComponentExpr)) |
9231 | { |
9232 | uint32_t index_expr = get_extended_decoration(id: args[0], decoration: SPIRVCrossDecorationInterpolantComponentExpr); |
9233 | auto *c = maybe_get<SPIRConstant>(id: index_expr); |
9234 | if (!c || c->specialization) |
9235 | component = join(ts: "[" , ts: to_expression(id: index_expr), ts: "]" ); |
9236 | else |
9237 | component = join(ts: "." , ts: index_to_swizzle(index: c->scalar())); |
9238 | } |
9239 | emit_op(result_type, result_id: id, |
9240 | rhs: join(ts: to_name(id: stage_in_var_id), ts: "." , ts: to_member_name(type: get_stage_in_struct_type(), index: interface_index), |
9241 | ts: ".interpolate_at_centroid()" , ts&: component), |
9242 | forward_rhs: should_forward(id: args[0])); |
9243 | break; |
9244 | } |
9245 | |
9246 | case GLSLstd450InterpolateAtSample: |
9247 | { |
9248 | uint32_t interface_index = get_extended_decoration(id: args[0], decoration: SPIRVCrossDecorationInterfaceMemberIndex); |
9249 | string component; |
9250 | if (has_extended_decoration(id: args[0], decoration: SPIRVCrossDecorationInterpolantComponentExpr)) |
9251 | { |
9252 | uint32_t index_expr = get_extended_decoration(id: args[0], decoration: SPIRVCrossDecorationInterpolantComponentExpr); |
9253 | auto *c = maybe_get<SPIRConstant>(id: index_expr); |
9254 | if (!c || c->specialization) |
9255 | component = join(ts: "[" , ts: to_expression(id: index_expr), ts: "]" ); |
9256 | else |
9257 | component = join(ts: "." , ts: index_to_swizzle(index: c->scalar())); |
9258 | } |
9259 | emit_op(result_type, result_id: id, |
9260 | rhs: join(ts: to_name(id: stage_in_var_id), ts: "." , ts: to_member_name(type: get_stage_in_struct_type(), index: interface_index), |
9261 | ts: ".interpolate_at_sample(" , ts: to_expression(id: args[1]), ts: ")" , ts&: component), |
9262 | forward_rhs: should_forward(id: args[0]) && should_forward(id: args[1])); |
9263 | break; |
9264 | } |
9265 | |
9266 | case GLSLstd450InterpolateAtOffset: |
9267 | { |
9268 | uint32_t interface_index = get_extended_decoration(id: args[0], decoration: SPIRVCrossDecorationInterfaceMemberIndex); |
9269 | string component; |
9270 | if (has_extended_decoration(id: args[0], decoration: SPIRVCrossDecorationInterpolantComponentExpr)) |
9271 | { |
9272 | uint32_t index_expr = get_extended_decoration(id: args[0], decoration: SPIRVCrossDecorationInterpolantComponentExpr); |
9273 | auto *c = maybe_get<SPIRConstant>(id: index_expr); |
9274 | if (!c || c->specialization) |
9275 | component = join(ts: "[" , ts: to_expression(id: index_expr), ts: "]" ); |
9276 | else |
9277 | component = join(ts: "." , ts: index_to_swizzle(index: c->scalar())); |
9278 | } |
9279 | // Like Direct3D, Metal puts the (0, 0) at the upper-left corner, not the center as SPIR-V and GLSL do. |
9280 | // Offset the offset by (1/2 - 1/16), or 0.4375, to compensate for this. |
9281 | // It has to be (1/2 - 1/16) and not 1/2, or several CTS tests subtly break on Intel. |
9282 | emit_op(result_type, result_id: id, |
9283 | rhs: join(ts: to_name(id: stage_in_var_id), ts: "." , ts: to_member_name(type: get_stage_in_struct_type(), index: interface_index), |
9284 | ts: ".interpolate_at_offset(" , ts: to_expression(id: args[1]), ts: " + 0.4375)" , ts&: component), |
9285 | forward_rhs: should_forward(id: args[0]) && should_forward(id: args[1])); |
9286 | break; |
9287 | } |
9288 | |
9289 | case GLSLstd450Distance: |
9290 | // MSL does not support scalar versions here. |
9291 | if (expression_type(id: args[0]).vecsize == 1) |
9292 | { |
9293 | // Equivalent to length(a - b) -> abs(a - b). |
9294 | emit_op(result_type, result_id: id, |
9295 | rhs: join(ts: "abs(" , ts: to_enclosed_unpacked_expression(id: args[0]), ts: " - " , |
9296 | ts: to_enclosed_unpacked_expression(id: args[1]), ts: ")" ), |
9297 | forward_rhs: should_forward(id: args[0]) && should_forward(id: args[1])); |
9298 | inherit_expression_dependencies(dst: id, source: args[0]); |
9299 | inherit_expression_dependencies(dst: id, source: args[1]); |
9300 | } |
9301 | else |
9302 | CompilerGLSL::emit_glsl_op(result_type, result_id: id, op: eop, args, count); |
9303 | break; |
9304 | |
9305 | case GLSLstd450Length: |
9306 | // MSL does not support scalar versions, so use abs(). |
9307 | if (expression_type(id: args[0]).vecsize == 1) |
9308 | emit_unary_func_op(result_type, result_id: id, op0: args[0], op: "abs" ); |
9309 | else |
9310 | CompilerGLSL::emit_glsl_op(result_type, result_id: id, op: eop, args, count); |
9311 | break; |
9312 | |
9313 | case GLSLstd450Normalize: |
9314 | { |
9315 | auto &exp_type = expression_type(id: args[0]); |
9316 | // MSL does not support scalar versions here. |
9317 | // MSL has no implementation for normalize in the fast:: namespace for half2 and half3 |
9318 | // Returns -1 or 1 for valid input, sign() does the job. |
9319 | if (exp_type.vecsize == 1) |
9320 | emit_unary_func_op(result_type, result_id: id, op0: args[0], op: "sign" ); |
9321 | else if (exp_type.vecsize <= 3 && exp_type.basetype == SPIRType::Half) |
9322 | emit_unary_func_op(result_type, result_id: id, op0: args[0], op: "normalize" ); |
9323 | else |
9324 | emit_unary_func_op(result_type, result_id: id, op0: args[0], op: "fast::normalize" ); |
9325 | break; |
9326 | } |
9327 | case GLSLstd450Reflect: |
9328 | if (get<SPIRType>(id: result_type).vecsize == 1) |
9329 | emit_binary_func_op(result_type, result_id: id, op0: args[0], op1: args[1], op: "spvReflect" ); |
9330 | else |
9331 | CompilerGLSL::emit_glsl_op(result_type, result_id: id, op: eop, args, count); |
9332 | break; |
9333 | |
9334 | case GLSLstd450Refract: |
9335 | if (get<SPIRType>(id: result_type).vecsize == 1) |
9336 | emit_trinary_func_op(result_type, result_id: id, op0: args[0], op1: args[1], op2: args[2], op: "spvRefract" ); |
9337 | else |
9338 | CompilerGLSL::emit_glsl_op(result_type, result_id: id, op: eop, args, count); |
9339 | break; |
9340 | |
9341 | case GLSLstd450FaceForward: |
9342 | if (get<SPIRType>(id: result_type).vecsize == 1) |
9343 | emit_trinary_func_op(result_type, result_id: id, op0: args[0], op1: args[1], op2: args[2], op: "spvFaceForward" ); |
9344 | else |
9345 | CompilerGLSL::emit_glsl_op(result_type, result_id: id, op: eop, args, count); |
9346 | break; |
9347 | |
9348 | case GLSLstd450Modf: |
9349 | case GLSLstd450Frexp: |
9350 | { |
9351 | // Special case. If the variable is a scalar access chain, we cannot use it directly. We have to emit a temporary. |
9352 | // Another special case is if the variable is in a storage class which is not thread. |
9353 | auto *ptr = maybe_get<SPIRExpression>(id: args[1]); |
9354 | auto &type = expression_type(id: args[1]); |
9355 | |
9356 | bool is_thread_storage = storage_class_array_is_thread(storage: type.storage); |
9357 | if (type.storage == StorageClassOutput && capture_output_to_buffer) |
9358 | is_thread_storage = false; |
9359 | |
9360 | if (!is_thread_storage || |
9361 | (ptr && ptr->access_chain && is_scalar(type: expression_type(id: args[1])))) |
9362 | { |
9363 | register_call_out_argument(id: args[1]); |
9364 | forced_temporaries.insert(x: id); |
9365 | |
9366 | // Need to create temporaries and copy over to access chain after. |
9367 | // We cannot directly take the reference of a vector swizzle in MSL, even if it's scalar ... |
9368 | uint32_t &tmp_id = extra_sub_expressions[id]; |
9369 | if (!tmp_id) |
9370 | tmp_id = ir.increase_bound_by(count: 1); |
9371 | |
9372 | uint32_t tmp_type_id = get_pointee_type_id(type_id: expression_type_id(id: args[1])); |
9373 | emit_uninitialized_temporary_expression(type: tmp_type_id, id: tmp_id); |
9374 | emit_binary_func_op(result_type, result_id: id, op0: args[0], op1: tmp_id, op: eop == GLSLstd450Modf ? "modf" : "frexp" ); |
9375 | statement(ts: to_expression(id: args[1]), ts: " = " , ts: to_expression(id: tmp_id), ts: ";" ); |
9376 | } |
9377 | else |
9378 | CompilerGLSL::emit_glsl_op(result_type, result_id: id, op: eop, args, count); |
9379 | break; |
9380 | } |
9381 | |
9382 | default: |
9383 | CompilerGLSL::emit_glsl_op(result_type, result_id: id, op: eop, args, count); |
9384 | break; |
9385 | } |
9386 | } |
9387 | |
9388 | void CompilerMSL::emit_spv_amd_shader_trinary_minmax_op(uint32_t result_type, uint32_t id, uint32_t eop, |
9389 | const uint32_t *args, uint32_t count) |
9390 | { |
9391 | enum AMDShaderTrinaryMinMax |
9392 | { |
9393 | FMin3AMD = 1, |
9394 | UMin3AMD = 2, |
9395 | SMin3AMD = 3, |
9396 | FMax3AMD = 4, |
9397 | UMax3AMD = 5, |
9398 | SMax3AMD = 6, |
9399 | FMid3AMD = 7, |
9400 | UMid3AMD = 8, |
9401 | SMid3AMD = 9 |
9402 | }; |
9403 | |
9404 | if (!msl_options.supports_msl_version(major: 2, minor: 1)) |
9405 | SPIRV_CROSS_THROW("Trinary min/max functions require MSL 2.1." ); |
9406 | |
9407 | auto op = static_cast<AMDShaderTrinaryMinMax>(eop); |
9408 | |
9409 | switch (op) |
9410 | { |
9411 | case FMid3AMD: |
9412 | case UMid3AMD: |
9413 | case SMid3AMD: |
9414 | emit_trinary_func_op(result_type, result_id: id, op0: args[0], op1: args[1], op2: args[2], op: "median3" ); |
9415 | break; |
9416 | default: |
9417 | CompilerGLSL::emit_spv_amd_shader_trinary_minmax_op(result_type, result_id: id, op: eop, args, count); |
9418 | break; |
9419 | } |
9420 | } |
9421 | |
9422 | // Emit a structure declaration for the specified interface variable. |
9423 | void CompilerMSL::emit_interface_block(uint32_t ib_var_id) |
9424 | { |
9425 | if (ib_var_id) |
9426 | { |
9427 | auto &ib_var = get<SPIRVariable>(id: ib_var_id); |
9428 | auto &ib_type = get_variable_data_type(var: ib_var); |
9429 | //assert(ib_type.basetype == SPIRType::Struct && !ib_type.member_types.empty()); |
9430 | assert(ib_type.basetype == SPIRType::Struct); |
9431 | emit_struct(type&: ib_type); |
9432 | } |
9433 | } |
9434 | |
9435 | // Emits the declaration signature of the specified function. |
9436 | // If this is the entry point function, Metal-specific return value and function arguments are added. |
9437 | void CompilerMSL::emit_function_prototype(SPIRFunction &func, const Bitset &) |
9438 | { |
9439 | if (func.self != ir.default_entry_point) |
9440 | add_function_overload(func); |
9441 | |
9442 | local_variable_names = resource_names; |
9443 | string decl; |
9444 | |
9445 | processing_entry_point = func.self == ir.default_entry_point; |
9446 | |
9447 | // Metal helper functions must be static force-inline otherwise they will cause problems when linked together in a single Metallib. |
9448 | if (!processing_entry_point) |
9449 | statement(ts&: force_inline); |
9450 | |
9451 | auto &type = get<SPIRType>(id: func.return_type); |
9452 | |
9453 | if (!type.array.empty() && msl_options.force_native_arrays) |
9454 | { |
9455 | // We cannot return native arrays in MSL, so "return" through an out variable. |
9456 | decl += "void" ; |
9457 | } |
9458 | else |
9459 | { |
9460 | decl += func_type_decl(type); |
9461 | } |
9462 | |
9463 | decl += " " ; |
9464 | decl += to_name(id: func.self); |
9465 | decl += "(" ; |
9466 | |
9467 | if (!type.array.empty() && msl_options.force_native_arrays) |
9468 | { |
9469 | // Fake arrays returns by writing to an out array instead. |
9470 | decl += "thread " ; |
9471 | decl += type_to_glsl(type); |
9472 | decl += " (&spvReturnValue)" ; |
9473 | decl += type_to_array_glsl(type); |
9474 | if (!func.arguments.empty()) |
9475 | decl += ", " ; |
9476 | } |
9477 | |
9478 | if (processing_entry_point) |
9479 | { |
9480 | if (msl_options.argument_buffers) |
9481 | decl += entry_point_args_argument_buffer(append_comma: !func.arguments.empty()); |
9482 | else |
9483 | decl += entry_point_args_classic(append_comma: !func.arguments.empty()); |
9484 | |
9485 | // append entry point args to avoid conflicts in local variable names. |
9486 | local_variable_names.insert(first: resource_names.begin(), last: resource_names.end()); |
9487 | |
9488 | // If entry point function has variables that require early declaration, |
9489 | // ensure they each have an empty initializer, creating one if needed. |
9490 | // This is done at this late stage because the initialization expression |
9491 | // is cleared after each compilation pass. |
9492 | for (auto var_id : vars_needing_early_declaration) |
9493 | { |
9494 | auto &ed_var = get<SPIRVariable>(id: var_id); |
9495 | ID &initializer = ed_var.initializer; |
9496 | if (!initializer) |
9497 | initializer = ir.increase_bound_by(count: 1); |
9498 | |
9499 | // Do not override proper initializers. |
9500 | if (ir.ids[initializer].get_type() == TypeNone || ir.ids[initializer].get_type() == TypeExpression) |
9501 | set<SPIRExpression>(id: ed_var.initializer, args: "{}" , args&: ed_var.basetype, args: true); |
9502 | } |
9503 | } |
9504 | |
9505 | for (auto &arg : func.arguments) |
9506 | { |
9507 | uint32_t name_id = arg.id; |
9508 | |
9509 | auto *var = maybe_get<SPIRVariable>(id: arg.id); |
9510 | if (var) |
9511 | { |
9512 | // If we need to modify the name of the variable, make sure we modify the original variable. |
9513 | // Our alias is just a shadow variable. |
9514 | if (arg.alias_global_variable && var->basevariable) |
9515 | name_id = var->basevariable; |
9516 | |
9517 | var->parameter = &arg; // Hold a pointer to the parameter so we can invalidate the readonly field if needed. |
9518 | } |
9519 | |
9520 | add_local_variable_name(id: name_id); |
9521 | |
9522 | decl += argument_decl(arg); |
9523 | |
9524 | bool is_dynamic_img_sampler = has_extended_decoration(id: arg.id, decoration: SPIRVCrossDecorationDynamicImageSampler); |
9525 | |
9526 | auto &arg_type = get<SPIRType>(id: arg.type); |
9527 | if (arg_type.basetype == SPIRType::SampledImage && !is_dynamic_img_sampler) |
9528 | { |
9529 | // Manufacture automatic plane args for multiplanar texture |
9530 | uint32_t planes = 1; |
9531 | if (auto *constexpr_sampler = find_constexpr_sampler(id: name_id)) |
9532 | if (constexpr_sampler->ycbcr_conversion_enable) |
9533 | planes = constexpr_sampler->planes; |
9534 | for (uint32_t i = 1; i < planes; i++) |
9535 | decl += join(ts: ", " , ts: argument_decl(arg), ts&: plane_name_suffix, ts&: i); |
9536 | |
9537 | // Manufacture automatic sampler arg for SampledImage texture |
9538 | if (arg_type.image.dim != DimBuffer) |
9539 | { |
9540 | if (arg_type.array.empty()) |
9541 | { |
9542 | decl += join(ts: ", " , ts: sampler_type(type: arg_type, id: arg.id), ts: " " , ts: to_sampler_expression(id: arg.id)); |
9543 | } |
9544 | else |
9545 | { |
9546 | const char *sampler_address_space = |
9547 | descriptor_address_space(id: name_id, |
9548 | storage: StorageClassUniformConstant, |
9549 | plain_address_space: "thread const" ); |
9550 | decl += join(ts: ", " , ts&: sampler_address_space, ts: " " , ts: sampler_type(type: arg_type, id: arg.id), ts: "& " , ts: to_sampler_expression(id: arg.id)); |
9551 | } |
9552 | } |
9553 | } |
9554 | |
9555 | // Manufacture automatic swizzle arg. |
9556 | if (msl_options.swizzle_texture_samples && has_sampled_images && is_sampled_image_type(type: arg_type) && |
9557 | !is_dynamic_img_sampler) |
9558 | { |
9559 | bool arg_is_array = !arg_type.array.empty(); |
9560 | decl += join(ts: ", constant uint" , ts: arg_is_array ? "* " : "& " , ts: to_swizzle_expression(id: arg.id)); |
9561 | } |
9562 | |
9563 | if (buffers_requiring_array_length.count(x: name_id)) |
9564 | { |
9565 | bool arg_is_array = !arg_type.array.empty(); |
9566 | decl += join(ts: ", constant uint" , ts: arg_is_array ? "* " : "& " , ts: to_buffer_size_expression(id: name_id)); |
9567 | } |
9568 | |
9569 | if (&arg != &func.arguments.back()) |
9570 | decl += ", " ; |
9571 | } |
9572 | |
9573 | decl += ")" ; |
9574 | statement(ts&: decl); |
9575 | } |
9576 | |
9577 | static bool needs_chroma_reconstruction(const MSLConstexprSampler *constexpr_sampler) |
9578 | { |
9579 | // For now, only multiplanar images need explicit reconstruction. GBGR and BGRG images |
9580 | // use implicit reconstruction. |
9581 | return constexpr_sampler && constexpr_sampler->ycbcr_conversion_enable && constexpr_sampler->planes > 1; |
9582 | } |
9583 | |
9584 | // Returns the texture sampling function string for the specified image and sampling characteristics. |
9585 | string CompilerMSL::to_function_name(const TextureFunctionNameArguments &args) |
9586 | { |
9587 | VariableID img = args.base.img; |
9588 | const MSLConstexprSampler *constexpr_sampler = nullptr; |
9589 | bool is_dynamic_img_sampler = false; |
9590 | if (auto *var = maybe_get_backing_variable(chain: img)) |
9591 | { |
9592 | constexpr_sampler = find_constexpr_sampler(id: var->basevariable ? var->basevariable : VariableID(var->self)); |
9593 | is_dynamic_img_sampler = has_extended_decoration(id: var->self, decoration: SPIRVCrossDecorationDynamicImageSampler); |
9594 | } |
9595 | |
9596 | // Special-case gather. We have to alter the component being looked up |
9597 | // in the swizzle case. |
9598 | if (msl_options.swizzle_texture_samples && args.base.is_gather && !is_dynamic_img_sampler && |
9599 | (!constexpr_sampler || !constexpr_sampler->ycbcr_conversion_enable)) |
9600 | { |
9601 | bool is_compare = comparison_ids.count(x: img); |
9602 | add_spv_func_and_recompile(spv_func: is_compare ? SPVFuncImplGatherCompareSwizzle : SPVFuncImplGatherSwizzle); |
9603 | return is_compare ? "spvGatherCompareSwizzle" : "spvGatherSwizzle" ; |
9604 | } |
9605 | |
9606 | auto *combined = maybe_get<SPIRCombinedImageSampler>(id: img); |
9607 | |
9608 | // Texture reference |
9609 | string fname; |
9610 | if (needs_chroma_reconstruction(constexpr_sampler) && !is_dynamic_img_sampler) |
9611 | { |
9612 | if (constexpr_sampler->planes != 2 && constexpr_sampler->planes != 3) |
9613 | SPIRV_CROSS_THROW("Unhandled number of color image planes!" ); |
9614 | // 444 images aren't downsampled, so we don't need to do linear filtering. |
9615 | if (constexpr_sampler->resolution == MSL_FORMAT_RESOLUTION_444 || |
9616 | constexpr_sampler->chroma_filter == MSL_SAMPLER_FILTER_NEAREST) |
9617 | { |
9618 | if (constexpr_sampler->planes == 2) |
9619 | add_spv_func_and_recompile(spv_func: SPVFuncImplChromaReconstructNearest2Plane); |
9620 | else |
9621 | add_spv_func_and_recompile(spv_func: SPVFuncImplChromaReconstructNearest3Plane); |
9622 | fname = "spvChromaReconstructNearest" ; |
9623 | } |
9624 | else // Linear with a downsampled format |
9625 | { |
9626 | fname = "spvChromaReconstructLinear" ; |
9627 | switch (constexpr_sampler->resolution) |
9628 | { |
9629 | case MSL_FORMAT_RESOLUTION_444: |
9630 | assert(false); |
9631 | break; // not reached |
9632 | case MSL_FORMAT_RESOLUTION_422: |
9633 | switch (constexpr_sampler->x_chroma_offset) |
9634 | { |
9635 | case MSL_CHROMA_LOCATION_COSITED_EVEN: |
9636 | if (constexpr_sampler->planes == 2) |
9637 | add_spv_func_and_recompile(spv_func: SPVFuncImplChromaReconstructLinear422CositedEven2Plane); |
9638 | else |
9639 | add_spv_func_and_recompile(spv_func: SPVFuncImplChromaReconstructLinear422CositedEven3Plane); |
9640 | fname += "422CositedEven" ; |
9641 | break; |
9642 | case MSL_CHROMA_LOCATION_MIDPOINT: |
9643 | if (constexpr_sampler->planes == 2) |
9644 | add_spv_func_and_recompile(spv_func: SPVFuncImplChromaReconstructLinear422Midpoint2Plane); |
9645 | else |
9646 | add_spv_func_and_recompile(spv_func: SPVFuncImplChromaReconstructLinear422Midpoint3Plane); |
9647 | fname += "422Midpoint" ; |
9648 | break; |
9649 | default: |
9650 | SPIRV_CROSS_THROW("Invalid chroma location." ); |
9651 | } |
9652 | break; |
9653 | case MSL_FORMAT_RESOLUTION_420: |
9654 | fname += "420" ; |
9655 | switch (constexpr_sampler->x_chroma_offset) |
9656 | { |
9657 | case MSL_CHROMA_LOCATION_COSITED_EVEN: |
9658 | switch (constexpr_sampler->y_chroma_offset) |
9659 | { |
9660 | case MSL_CHROMA_LOCATION_COSITED_EVEN: |
9661 | if (constexpr_sampler->planes == 2) |
9662 | add_spv_func_and_recompile( |
9663 | spv_func: SPVFuncImplChromaReconstructLinear420XCositedEvenYCositedEven2Plane); |
9664 | else |
9665 | add_spv_func_and_recompile( |
9666 | spv_func: SPVFuncImplChromaReconstructLinear420XCositedEvenYCositedEven3Plane); |
9667 | fname += "XCositedEvenYCositedEven" ; |
9668 | break; |
9669 | case MSL_CHROMA_LOCATION_MIDPOINT: |
9670 | if (constexpr_sampler->planes == 2) |
9671 | add_spv_func_and_recompile( |
9672 | spv_func: SPVFuncImplChromaReconstructLinear420XCositedEvenYMidpoint2Plane); |
9673 | else |
9674 | add_spv_func_and_recompile( |
9675 | spv_func: SPVFuncImplChromaReconstructLinear420XCositedEvenYMidpoint3Plane); |
9676 | fname += "XCositedEvenYMidpoint" ; |
9677 | break; |
9678 | default: |
9679 | SPIRV_CROSS_THROW("Invalid Y chroma location." ); |
9680 | } |
9681 | break; |
9682 | case MSL_CHROMA_LOCATION_MIDPOINT: |
9683 | switch (constexpr_sampler->y_chroma_offset) |
9684 | { |
9685 | case MSL_CHROMA_LOCATION_COSITED_EVEN: |
9686 | if (constexpr_sampler->planes == 2) |
9687 | add_spv_func_and_recompile( |
9688 | spv_func: SPVFuncImplChromaReconstructLinear420XMidpointYCositedEven2Plane); |
9689 | else |
9690 | add_spv_func_and_recompile( |
9691 | spv_func: SPVFuncImplChromaReconstructLinear420XMidpointYCositedEven3Plane); |
9692 | fname += "XMidpointYCositedEven" ; |
9693 | break; |
9694 | case MSL_CHROMA_LOCATION_MIDPOINT: |
9695 | if (constexpr_sampler->planes == 2) |
9696 | add_spv_func_and_recompile(spv_func: SPVFuncImplChromaReconstructLinear420XMidpointYMidpoint2Plane); |
9697 | else |
9698 | add_spv_func_and_recompile(spv_func: SPVFuncImplChromaReconstructLinear420XMidpointYMidpoint3Plane); |
9699 | fname += "XMidpointYMidpoint" ; |
9700 | break; |
9701 | default: |
9702 | SPIRV_CROSS_THROW("Invalid Y chroma location." ); |
9703 | } |
9704 | break; |
9705 | default: |
9706 | SPIRV_CROSS_THROW("Invalid X chroma location." ); |
9707 | } |
9708 | break; |
9709 | default: |
9710 | SPIRV_CROSS_THROW("Invalid format resolution." ); |
9711 | } |
9712 | } |
9713 | } |
9714 | else |
9715 | { |
9716 | fname = to_expression(id: combined ? combined->image : img) + "." ; |
9717 | |
9718 | // Texture function and sampler |
9719 | if (args.base.is_fetch) |
9720 | fname += "read" ; |
9721 | else if (args.base.is_gather) |
9722 | fname += "gather" ; |
9723 | else |
9724 | fname += "sample" ; |
9725 | |
9726 | if (args.has_dref) |
9727 | fname += "_compare" ; |
9728 | } |
9729 | |
9730 | return fname; |
9731 | } |
9732 | |
9733 | string CompilerMSL::convert_to_f32(const string &expr, uint32_t components) |
9734 | { |
9735 | SPIRType t; |
9736 | t.basetype = SPIRType::Float; |
9737 | t.vecsize = components; |
9738 | t.columns = 1; |
9739 | return join(ts: type_to_glsl_constructor(type: t), ts: "(" , ts: expr, ts: ")" ); |
9740 | } |
9741 | |
9742 | static inline bool sampling_type_needs_f32_conversion(const SPIRType &type) |
9743 | { |
9744 | // Double is not supported to begin with, but doesn't hurt to check for completion. |
9745 | return type.basetype == SPIRType::Half || type.basetype == SPIRType::Double; |
9746 | } |
9747 | |
9748 | // Returns the function args for a texture sampling function for the specified image and sampling characteristics. |
9749 | string CompilerMSL::to_function_args(const TextureFunctionArguments &args, bool *p_forward) |
9750 | { |
9751 | VariableID img = args.base.img; |
9752 | auto &imgtype = *args.base.imgtype; |
9753 | uint32_t lod = args.lod; |
9754 | uint32_t grad_x = args.grad_x; |
9755 | uint32_t grad_y = args.grad_y; |
9756 | uint32_t bias = args.bias; |
9757 | |
9758 | const MSLConstexprSampler *constexpr_sampler = nullptr; |
9759 | bool is_dynamic_img_sampler = false; |
9760 | if (auto *var = maybe_get_backing_variable(chain: img)) |
9761 | { |
9762 | constexpr_sampler = find_constexpr_sampler(id: var->basevariable ? var->basevariable : VariableID(var->self)); |
9763 | is_dynamic_img_sampler = has_extended_decoration(id: var->self, decoration: SPIRVCrossDecorationDynamicImageSampler); |
9764 | } |
9765 | |
9766 | string farg_str; |
9767 | bool forward = true; |
9768 | |
9769 | if (!is_dynamic_img_sampler) |
9770 | { |
9771 | // Texture reference (for some cases) |
9772 | if (needs_chroma_reconstruction(constexpr_sampler)) |
9773 | { |
9774 | // Multiplanar images need two or three textures. |
9775 | farg_str += to_expression(id: img); |
9776 | for (uint32_t i = 1; i < constexpr_sampler->planes; i++) |
9777 | farg_str += join(ts: ", " , ts: to_expression(id: img), ts&: plane_name_suffix, ts&: i); |
9778 | } |
9779 | else if ((!constexpr_sampler || !constexpr_sampler->ycbcr_conversion_enable) && |
9780 | msl_options.swizzle_texture_samples && args.base.is_gather) |
9781 | { |
9782 | auto *combined = maybe_get<SPIRCombinedImageSampler>(id: img); |
9783 | farg_str += to_expression(id: combined ? combined->image : img); |
9784 | } |
9785 | |
9786 | // Sampler reference |
9787 | if (!args.base.is_fetch) |
9788 | { |
9789 | if (!farg_str.empty()) |
9790 | farg_str += ", " ; |
9791 | farg_str += to_sampler_expression(id: img); |
9792 | } |
9793 | |
9794 | if ((!constexpr_sampler || !constexpr_sampler->ycbcr_conversion_enable) && |
9795 | msl_options.swizzle_texture_samples && args.base.is_gather) |
9796 | { |
9797 | // Add the swizzle constant from the swizzle buffer. |
9798 | farg_str += ", " + to_swizzle_expression(id: img); |
9799 | used_swizzle_buffer = true; |
9800 | } |
9801 | |
9802 | // Swizzled gather puts the component before the other args, to allow template |
9803 | // deduction to work. |
9804 | if (args.component && msl_options.swizzle_texture_samples) |
9805 | { |
9806 | forward = should_forward(id: args.component); |
9807 | farg_str += ", " + to_component_argument(id: args.component); |
9808 | } |
9809 | } |
9810 | |
9811 | // Texture coordinates |
9812 | forward = forward && should_forward(id: args.coord); |
9813 | auto coord_expr = to_enclosed_expression(id: args.coord); |
9814 | auto &coord_type = expression_type(id: args.coord); |
9815 | bool coord_is_fp = type_is_floating_point(type: coord_type); |
9816 | bool is_cube_fetch = false; |
9817 | |
9818 | string tex_coords = coord_expr; |
9819 | uint32_t alt_coord_component = 0; |
9820 | |
9821 | switch (imgtype.image.dim) |
9822 | { |
9823 | |
9824 | case Dim1D: |
9825 | if (coord_type.vecsize > 1) |
9826 | tex_coords = enclose_expression(expr: tex_coords) + ".x" ; |
9827 | |
9828 | if (args.base.is_fetch) |
9829 | tex_coords = "uint(" + round_fp_tex_coords(tex_coords, coord_is_fp) + ")" ; |
9830 | else if (sampling_type_needs_f32_conversion(type: coord_type)) |
9831 | tex_coords = convert_to_f32(expr: tex_coords, components: 1); |
9832 | |
9833 | if (msl_options.texture_1D_as_2D) |
9834 | { |
9835 | if (args.base.is_fetch) |
9836 | tex_coords = "uint2(" + tex_coords + ", 0)" ; |
9837 | else |
9838 | tex_coords = "float2(" + tex_coords + ", 0.5)" ; |
9839 | } |
9840 | |
9841 | alt_coord_component = 1; |
9842 | break; |
9843 | |
9844 | case DimBuffer: |
9845 | if (coord_type.vecsize > 1) |
9846 | tex_coords = enclose_expression(expr: tex_coords) + ".x" ; |
9847 | |
9848 | if (msl_options.texture_buffer_native) |
9849 | { |
9850 | tex_coords = "uint(" + round_fp_tex_coords(tex_coords, coord_is_fp) + ")" ; |
9851 | } |
9852 | else |
9853 | { |
9854 | // Metal texel buffer textures are 2D, so convert 1D coord to 2D. |
9855 | // Support for Metal 2.1's new texture_buffer type. |
9856 | if (args.base.is_fetch) |
9857 | { |
9858 | if (msl_options.texel_buffer_texture_width > 0) |
9859 | { |
9860 | tex_coords = "spvTexelBufferCoord(" + round_fp_tex_coords(tex_coords, coord_is_fp) + ")" ; |
9861 | } |
9862 | else |
9863 | { |
9864 | tex_coords = "spvTexelBufferCoord(" + round_fp_tex_coords(tex_coords, coord_is_fp) + ", " + |
9865 | to_expression(id: img) + ")" ; |
9866 | } |
9867 | } |
9868 | } |
9869 | |
9870 | alt_coord_component = 1; |
9871 | break; |
9872 | |
9873 | case DimSubpassData: |
9874 | // If we're using Metal's native frame-buffer fetch API for subpass inputs, |
9875 | // this path will not be hit. |
9876 | tex_coords = "uint2(gl_FragCoord.xy)" ; |
9877 | alt_coord_component = 2; |
9878 | break; |
9879 | |
9880 | case Dim2D: |
9881 | if (coord_type.vecsize > 2) |
9882 | tex_coords = enclose_expression(expr: tex_coords) + ".xy" ; |
9883 | |
9884 | if (args.base.is_fetch) |
9885 | tex_coords = "uint2(" + round_fp_tex_coords(tex_coords, coord_is_fp) + ")" ; |
9886 | else if (sampling_type_needs_f32_conversion(type: coord_type)) |
9887 | tex_coords = convert_to_f32(expr: tex_coords, components: 2); |
9888 | |
9889 | alt_coord_component = 2; |
9890 | break; |
9891 | |
9892 | case Dim3D: |
9893 | if (coord_type.vecsize > 3) |
9894 | tex_coords = enclose_expression(expr: tex_coords) + ".xyz" ; |
9895 | |
9896 | if (args.base.is_fetch) |
9897 | tex_coords = "uint3(" + round_fp_tex_coords(tex_coords, coord_is_fp) + ")" ; |
9898 | else if (sampling_type_needs_f32_conversion(type: coord_type)) |
9899 | tex_coords = convert_to_f32(expr: tex_coords, components: 3); |
9900 | |
9901 | alt_coord_component = 3; |
9902 | break; |
9903 | |
9904 | case DimCube: |
9905 | if (args.base.is_fetch) |
9906 | { |
9907 | is_cube_fetch = true; |
9908 | tex_coords += ".xy" ; |
9909 | tex_coords = "uint2(" + round_fp_tex_coords(tex_coords, coord_is_fp) + ")" ; |
9910 | } |
9911 | else |
9912 | { |
9913 | if (coord_type.vecsize > 3) |
9914 | tex_coords = enclose_expression(expr: tex_coords) + ".xyz" ; |
9915 | } |
9916 | |
9917 | if (sampling_type_needs_f32_conversion(type: coord_type)) |
9918 | tex_coords = convert_to_f32(expr: tex_coords, components: 3); |
9919 | |
9920 | alt_coord_component = 3; |
9921 | break; |
9922 | |
9923 | default: |
9924 | break; |
9925 | } |
9926 | |
9927 | if (args.base.is_fetch && (args.offset || args.coffset)) |
9928 | { |
9929 | uint32_t offset_expr = args.offset ? args.offset : args.coffset; |
9930 | // Fetch offsets must be applied directly to the coordinate. |
9931 | forward = forward && should_forward(id: offset_expr); |
9932 | auto &type = expression_type(id: offset_expr); |
9933 | if (imgtype.image.dim == Dim1D && msl_options.texture_1D_as_2D) |
9934 | { |
9935 | if (type.basetype != SPIRType::UInt) |
9936 | tex_coords += join(ts: " + uint2(" , ts: bitcast_expression(target_type: SPIRType::UInt, arg: offset_expr), ts: ", 0)" ); |
9937 | else |
9938 | tex_coords += join(ts: " + uint2(" , ts: to_enclosed_expression(id: offset_expr), ts: ", 0)" ); |
9939 | } |
9940 | else |
9941 | { |
9942 | if (type.basetype != SPIRType::UInt) |
9943 | tex_coords += " + " + bitcast_expression(target_type: SPIRType::UInt, arg: offset_expr); |
9944 | else |
9945 | tex_coords += " + " + to_enclosed_expression(id: offset_expr); |
9946 | } |
9947 | } |
9948 | |
9949 | // If projection, use alt coord as divisor |
9950 | if (args.base.is_proj) |
9951 | { |
9952 | if (sampling_type_needs_f32_conversion(type: coord_type)) |
9953 | tex_coords += " / " + convert_to_f32(expr: to_extract_component_expression(id: args.coord, index: alt_coord_component), components: 1); |
9954 | else |
9955 | tex_coords += " / " + to_extract_component_expression(id: args.coord, index: alt_coord_component); |
9956 | } |
9957 | |
9958 | if (!farg_str.empty()) |
9959 | farg_str += ", " ; |
9960 | |
9961 | if (imgtype.image.dim == DimCube && imgtype.image.arrayed && msl_options.emulate_cube_array) |
9962 | { |
9963 | farg_str += "spvCubemapTo2DArrayFace(" + tex_coords + ").xy" ; |
9964 | |
9965 | if (is_cube_fetch) |
9966 | farg_str += ", uint(" + to_extract_component_expression(id: args.coord, index: 2) + ")" ; |
9967 | else |
9968 | farg_str += |
9969 | ", uint(spvCubemapTo2DArrayFace(" + tex_coords + ").z) + (uint(" + |
9970 | round_fp_tex_coords(tex_coords: to_extract_component_expression(id: args.coord, index: alt_coord_component), coord_is_fp) + |
9971 | ") * 6u)" ; |
9972 | |
9973 | add_spv_func_and_recompile(spv_func: SPVFuncImplCubemapTo2DArrayFace); |
9974 | } |
9975 | else |
9976 | { |
9977 | farg_str += tex_coords; |
9978 | |
9979 | // If fetch from cube, add face explicitly |
9980 | if (is_cube_fetch) |
9981 | { |
9982 | // Special case for cube arrays, face and layer are packed in one dimension. |
9983 | if (imgtype.image.arrayed) |
9984 | farg_str += ", uint(" + to_extract_component_expression(id: args.coord, index: 2) + ") % 6u" ; |
9985 | else |
9986 | farg_str += |
9987 | ", uint(" + round_fp_tex_coords(tex_coords: to_extract_component_expression(id: args.coord, index: 2), coord_is_fp) + ")" ; |
9988 | } |
9989 | |
9990 | // If array, use alt coord |
9991 | if (imgtype.image.arrayed) |
9992 | { |
9993 | // Special case for cube arrays, face and layer are packed in one dimension. |
9994 | if (imgtype.image.dim == DimCube && args.base.is_fetch) |
9995 | { |
9996 | farg_str += ", uint(" + to_extract_component_expression(id: args.coord, index: 2) + ") / 6u" ; |
9997 | } |
9998 | else |
9999 | { |
10000 | farg_str += |
10001 | ", uint(" + |
10002 | round_fp_tex_coords(tex_coords: to_extract_component_expression(id: args.coord, index: alt_coord_component), coord_is_fp) + |
10003 | ")" ; |
10004 | if (imgtype.image.dim == DimSubpassData) |
10005 | { |
10006 | if (msl_options.multiview) |
10007 | farg_str += " + gl_ViewIndex" ; |
10008 | else if (msl_options.arrayed_subpass_input) |
10009 | farg_str += " + gl_Layer" ; |
10010 | } |
10011 | } |
10012 | } |
10013 | else if (imgtype.image.dim == DimSubpassData) |
10014 | { |
10015 | if (msl_options.multiview) |
10016 | farg_str += ", gl_ViewIndex" ; |
10017 | else if (msl_options.arrayed_subpass_input) |
10018 | farg_str += ", gl_Layer" ; |
10019 | } |
10020 | } |
10021 | |
10022 | // Depth compare reference value |
10023 | if (args.dref) |
10024 | { |
10025 | forward = forward && should_forward(id: args.dref); |
10026 | farg_str += ", " ; |
10027 | |
10028 | auto &dref_type = expression_type(id: args.dref); |
10029 | |
10030 | string dref_expr; |
10031 | if (args.base.is_proj) |
10032 | dref_expr = join(ts: to_enclosed_expression(id: args.dref), ts: " / " , |
10033 | ts: to_extract_component_expression(id: args.coord, index: alt_coord_component)); |
10034 | else |
10035 | dref_expr = to_expression(id: args.dref); |
10036 | |
10037 | if (sampling_type_needs_f32_conversion(type: dref_type)) |
10038 | dref_expr = convert_to_f32(expr: dref_expr, components: 1); |
10039 | |
10040 | farg_str += dref_expr; |
10041 | |
10042 | if (msl_options.is_macos() && (grad_x || grad_y)) |
10043 | { |
10044 | // For sample compare, MSL does not support gradient2d for all targets (only iOS apparently according to docs). |
10045 | // However, the most common case here is to have a constant gradient of 0, as that is the only way to express |
10046 | // LOD == 0 in GLSL with sampler2DArrayShadow (cascaded shadow mapping). |
10047 | // We will detect a compile-time constant 0 value for gradient and promote that to level(0) on MSL. |
10048 | bool constant_zero_x = !grad_x || expression_is_constant_null(id: grad_x); |
10049 | bool constant_zero_y = !grad_y || expression_is_constant_null(id: grad_y); |
10050 | if (constant_zero_x && constant_zero_y) |
10051 | { |
10052 | lod = 0; |
10053 | grad_x = 0; |
10054 | grad_y = 0; |
10055 | farg_str += ", level(0)" ; |
10056 | } |
10057 | else if (!msl_options.supports_msl_version(major: 2, minor: 3)) |
10058 | { |
10059 | SPIRV_CROSS_THROW("Using non-constant 0.0 gradient() qualifier for sample_compare. This is not " |
10060 | "supported on macOS prior to MSL 2.3." ); |
10061 | } |
10062 | } |
10063 | |
10064 | if (msl_options.is_macos() && bias) |
10065 | { |
10066 | // Bias is not supported either on macOS with sample_compare. |
10067 | // Verify it is compile-time zero, and drop the argument. |
10068 | if (expression_is_constant_null(id: bias)) |
10069 | { |
10070 | bias = 0; |
10071 | } |
10072 | else if (!msl_options.supports_msl_version(major: 2, minor: 3)) |
10073 | { |
10074 | SPIRV_CROSS_THROW("Using non-constant 0.0 bias() qualifier for sample_compare. This is not supported " |
10075 | "on macOS prior to MSL 2.3." ); |
10076 | } |
10077 | } |
10078 | } |
10079 | |
10080 | // LOD Options |
10081 | // Metal does not support LOD for 1D textures. |
10082 | if (bias && (imgtype.image.dim != Dim1D || msl_options.texture_1D_as_2D)) |
10083 | { |
10084 | forward = forward && should_forward(id: bias); |
10085 | farg_str += ", bias(" + to_expression(id: bias) + ")" ; |
10086 | } |
10087 | |
10088 | // Metal does not support LOD for 1D textures. |
10089 | if (lod && (imgtype.image.dim != Dim1D || msl_options.texture_1D_as_2D)) |
10090 | { |
10091 | forward = forward && should_forward(id: lod); |
10092 | if (args.base.is_fetch) |
10093 | { |
10094 | farg_str += ", " + to_expression(id: lod); |
10095 | } |
10096 | else |
10097 | { |
10098 | farg_str += ", level(" + to_expression(id: lod) + ")" ; |
10099 | } |
10100 | } |
10101 | else if (args.base.is_fetch && !lod && (imgtype.image.dim != Dim1D || msl_options.texture_1D_as_2D) && |
10102 | imgtype.image.dim != DimBuffer && !imgtype.image.ms && imgtype.image.sampled != 2) |
10103 | { |
10104 | // Lod argument is optional in OpImageFetch, but we require a LOD value, pick 0 as the default. |
10105 | // Check for sampled type as well, because is_fetch is also used for OpImageRead in MSL. |
10106 | farg_str += ", 0" ; |
10107 | } |
10108 | |
10109 | // Metal does not support LOD for 1D textures. |
10110 | if ((grad_x || grad_y) && (imgtype.image.dim != Dim1D || msl_options.texture_1D_as_2D)) |
10111 | { |
10112 | forward = forward && should_forward(id: grad_x); |
10113 | forward = forward && should_forward(id: grad_y); |
10114 | string grad_opt; |
10115 | switch (imgtype.image.dim) |
10116 | { |
10117 | case Dim1D: |
10118 | case Dim2D: |
10119 | grad_opt = "2d" ; |
10120 | break; |
10121 | case Dim3D: |
10122 | grad_opt = "3d" ; |
10123 | break; |
10124 | case DimCube: |
10125 | if (imgtype.image.arrayed && msl_options.emulate_cube_array) |
10126 | grad_opt = "2d" ; |
10127 | else |
10128 | grad_opt = "cube" ; |
10129 | break; |
10130 | default: |
10131 | grad_opt = "unsupported_gradient_dimension" ; |
10132 | break; |
10133 | } |
10134 | farg_str += ", gradient" + grad_opt + "(" + to_expression(id: grad_x) + ", " + to_expression(id: grad_y) + ")" ; |
10135 | } |
10136 | |
10137 | if (args.min_lod) |
10138 | { |
10139 | if (!msl_options.supports_msl_version(major: 2, minor: 2)) |
10140 | SPIRV_CROSS_THROW("min_lod_clamp() is only supported in MSL 2.2+ and up." ); |
10141 | |
10142 | forward = forward && should_forward(id: args.min_lod); |
10143 | farg_str += ", min_lod_clamp(" + to_expression(id: args.min_lod) + ")" ; |
10144 | } |
10145 | |
10146 | // Add offsets |
10147 | string offset_expr; |
10148 | const SPIRType *offset_type = nullptr; |
10149 | if (args.coffset && !args.base.is_fetch) |
10150 | { |
10151 | forward = forward && should_forward(id: args.coffset); |
10152 | offset_expr = to_expression(id: args.coffset); |
10153 | offset_type = &expression_type(id: args.coffset); |
10154 | } |
10155 | else if (args.offset && !args.base.is_fetch) |
10156 | { |
10157 | forward = forward && should_forward(id: args.offset); |
10158 | offset_expr = to_expression(id: args.offset); |
10159 | offset_type = &expression_type(id: args.offset); |
10160 | } |
10161 | |
10162 | if (!offset_expr.empty()) |
10163 | { |
10164 | switch (imgtype.image.dim) |
10165 | { |
10166 | case Dim1D: |
10167 | if (!msl_options.texture_1D_as_2D) |
10168 | break; |
10169 | if (offset_type->vecsize > 1) |
10170 | offset_expr = enclose_expression(expr: offset_expr) + ".x" ; |
10171 | |
10172 | farg_str += join(ts: ", int2(" , ts&: offset_expr, ts: ", 0)" ); |
10173 | break; |
10174 | |
10175 | case Dim2D: |
10176 | if (offset_type->vecsize > 2) |
10177 | offset_expr = enclose_expression(expr: offset_expr) + ".xy" ; |
10178 | |
10179 | farg_str += ", " + offset_expr; |
10180 | break; |
10181 | |
10182 | case Dim3D: |
10183 | if (offset_type->vecsize > 3) |
10184 | offset_expr = enclose_expression(expr: offset_expr) + ".xyz" ; |
10185 | |
10186 | farg_str += ", " + offset_expr; |
10187 | break; |
10188 | |
10189 | default: |
10190 | break; |
10191 | } |
10192 | } |
10193 | |
10194 | if (args.component) |
10195 | { |
10196 | // If 2D has gather component, ensure it also has an offset arg |
10197 | if (imgtype.image.dim == Dim2D && offset_expr.empty()) |
10198 | farg_str += ", int2(0)" ; |
10199 | |
10200 | if (!msl_options.swizzle_texture_samples || is_dynamic_img_sampler) |
10201 | { |
10202 | forward = forward && should_forward(id: args.component); |
10203 | |
10204 | uint32_t image_var = 0; |
10205 | if (const auto *combined = maybe_get<SPIRCombinedImageSampler>(id: img)) |
10206 | { |
10207 | if (const auto *img_var = maybe_get_backing_variable(chain: combined->image)) |
10208 | image_var = img_var->self; |
10209 | } |
10210 | else if (const auto *var = maybe_get_backing_variable(chain: img)) |
10211 | { |
10212 | image_var = var->self; |
10213 | } |
10214 | |
10215 | if (image_var == 0 || !is_depth_image(type: expression_type(id: image_var), id: image_var)) |
10216 | farg_str += ", " + to_component_argument(id: args.component); |
10217 | } |
10218 | } |
10219 | |
10220 | if (args.sample) |
10221 | { |
10222 | forward = forward && should_forward(id: args.sample); |
10223 | farg_str += ", " ; |
10224 | farg_str += to_expression(id: args.sample); |
10225 | } |
10226 | |
10227 | *p_forward = forward; |
10228 | |
10229 | return farg_str; |
10230 | } |
10231 | |
10232 | // If the texture coordinates are floating point, invokes MSL round() function to round them. |
10233 | string CompilerMSL::round_fp_tex_coords(string tex_coords, bool coord_is_fp) |
10234 | { |
10235 | return coord_is_fp ? ("round(" + tex_coords + ")" ) : tex_coords; |
10236 | } |
10237 | |
10238 | // Returns a string to use in an image sampling function argument. |
10239 | // The ID must be a scalar constant. |
10240 | string CompilerMSL::to_component_argument(uint32_t id) |
10241 | { |
10242 | uint32_t component_index = evaluate_constant_u32(id); |
10243 | switch (component_index) |
10244 | { |
10245 | case 0: |
10246 | return "component::x" ; |
10247 | case 1: |
10248 | return "component::y" ; |
10249 | case 2: |
10250 | return "component::z" ; |
10251 | case 3: |
10252 | return "component::w" ; |
10253 | |
10254 | default: |
10255 | SPIRV_CROSS_THROW("The value (" + to_string(component_index) + ") of OpConstant ID " + to_string(id) + |
10256 | " is not a valid Component index, which must be one of 0, 1, 2, or 3." ); |
10257 | } |
10258 | } |
10259 | |
10260 | // Establish sampled image as expression object and assign the sampler to it. |
10261 | void CompilerMSL::emit_sampled_image_op(uint32_t result_type, uint32_t result_id, uint32_t image_id, uint32_t samp_id) |
10262 | { |
10263 | set<SPIRCombinedImageSampler>(id: result_id, args&: result_type, args&: image_id, args&: samp_id); |
10264 | } |
10265 | |
10266 | string CompilerMSL::to_texture_op(const Instruction &i, bool sparse, bool *forward, |
10267 | SmallVector<uint32_t> &inherited_expressions) |
10268 | { |
10269 | auto *ops = stream(instr: i); |
10270 | uint32_t result_type_id = ops[0]; |
10271 | uint32_t img = ops[2]; |
10272 | auto &result_type = get<SPIRType>(id: result_type_id); |
10273 | auto op = static_cast<Op>(i.op); |
10274 | bool is_gather = (op == OpImageGather || op == OpImageDrefGather); |
10275 | |
10276 | // Bypass pointers because we need the real image struct |
10277 | auto &type = expression_type(id: img); |
10278 | auto &imgtype = get<SPIRType>(id: type.self); |
10279 | |
10280 | const MSLConstexprSampler *constexpr_sampler = nullptr; |
10281 | bool is_dynamic_img_sampler = false; |
10282 | if (auto *var = maybe_get_backing_variable(chain: img)) |
10283 | { |
10284 | constexpr_sampler = find_constexpr_sampler(id: var->basevariable ? var->basevariable : VariableID(var->self)); |
10285 | is_dynamic_img_sampler = has_extended_decoration(id: var->self, decoration: SPIRVCrossDecorationDynamicImageSampler); |
10286 | } |
10287 | |
10288 | string expr; |
10289 | if (constexpr_sampler && constexpr_sampler->ycbcr_conversion_enable && !is_dynamic_img_sampler) |
10290 | { |
10291 | // If this needs sampler Y'CbCr conversion, we need to do some additional |
10292 | // processing. |
10293 | switch (constexpr_sampler->ycbcr_model) |
10294 | { |
10295 | case MSL_SAMPLER_YCBCR_MODEL_CONVERSION_RGB_IDENTITY: |
10296 | case MSL_SAMPLER_YCBCR_MODEL_CONVERSION_YCBCR_IDENTITY: |
10297 | // Default |
10298 | break; |
10299 | case MSL_SAMPLER_YCBCR_MODEL_CONVERSION_YCBCR_BT_709: |
10300 | add_spv_func_and_recompile(spv_func: SPVFuncImplConvertYCbCrBT709); |
10301 | expr += "spvConvertYCbCrBT709(" ; |
10302 | break; |
10303 | case MSL_SAMPLER_YCBCR_MODEL_CONVERSION_YCBCR_BT_601: |
10304 | add_spv_func_and_recompile(spv_func: SPVFuncImplConvertYCbCrBT601); |
10305 | expr += "spvConvertYCbCrBT601(" ; |
10306 | break; |
10307 | case MSL_SAMPLER_YCBCR_MODEL_CONVERSION_YCBCR_BT_2020: |
10308 | add_spv_func_and_recompile(spv_func: SPVFuncImplConvertYCbCrBT2020); |
10309 | expr += "spvConvertYCbCrBT2020(" ; |
10310 | break; |
10311 | default: |
10312 | SPIRV_CROSS_THROW("Invalid Y'CbCr model conversion." ); |
10313 | } |
10314 | |
10315 | if (constexpr_sampler->ycbcr_model != MSL_SAMPLER_YCBCR_MODEL_CONVERSION_RGB_IDENTITY) |
10316 | { |
10317 | switch (constexpr_sampler->ycbcr_range) |
10318 | { |
10319 | case MSL_SAMPLER_YCBCR_RANGE_ITU_FULL: |
10320 | add_spv_func_and_recompile(spv_func: SPVFuncImplExpandITUFullRange); |
10321 | expr += "spvExpandITUFullRange(" ; |
10322 | break; |
10323 | case MSL_SAMPLER_YCBCR_RANGE_ITU_NARROW: |
10324 | add_spv_func_and_recompile(spv_func: SPVFuncImplExpandITUNarrowRange); |
10325 | expr += "spvExpandITUNarrowRange(" ; |
10326 | break; |
10327 | default: |
10328 | SPIRV_CROSS_THROW("Invalid Y'CbCr range." ); |
10329 | } |
10330 | } |
10331 | } |
10332 | else if (msl_options.swizzle_texture_samples && !is_gather && is_sampled_image_type(type: imgtype) && |
10333 | !is_dynamic_img_sampler) |
10334 | { |
10335 | add_spv_func_and_recompile(spv_func: SPVFuncImplTextureSwizzle); |
10336 | expr += "spvTextureSwizzle(" ; |
10337 | } |
10338 | |
10339 | string inner_expr = CompilerGLSL::to_texture_op(i, sparse, forward, inherited_expressions); |
10340 | |
10341 | if (constexpr_sampler && constexpr_sampler->ycbcr_conversion_enable && !is_dynamic_img_sampler) |
10342 | { |
10343 | if (!constexpr_sampler->swizzle_is_identity()) |
10344 | { |
10345 | static const char swizzle_names[] = "rgba" ; |
10346 | if (!constexpr_sampler->swizzle_has_one_or_zero()) |
10347 | { |
10348 | // If we can, do it inline. |
10349 | expr += inner_expr + "." ; |
10350 | for (uint32_t c = 0; c < 4; c++) |
10351 | { |
10352 | switch (constexpr_sampler->swizzle[c]) |
10353 | { |
10354 | case MSL_COMPONENT_SWIZZLE_IDENTITY: |
10355 | expr += swizzle_names[c]; |
10356 | break; |
10357 | case MSL_COMPONENT_SWIZZLE_R: |
10358 | case MSL_COMPONENT_SWIZZLE_G: |
10359 | case MSL_COMPONENT_SWIZZLE_B: |
10360 | case MSL_COMPONENT_SWIZZLE_A: |
10361 | expr += swizzle_names[constexpr_sampler->swizzle[c] - MSL_COMPONENT_SWIZZLE_R]; |
10362 | break; |
10363 | default: |
10364 | SPIRV_CROSS_THROW("Invalid component swizzle." ); |
10365 | } |
10366 | } |
10367 | } |
10368 | else |
10369 | { |
10370 | // Otherwise, we need to emit a temporary and swizzle that. |
10371 | uint32_t temp_id = ir.increase_bound_by(count: 1); |
10372 | emit_op(result_type: result_type_id, result_id: temp_id, rhs: inner_expr, forward_rhs: false); |
10373 | for (auto &inherit : inherited_expressions) |
10374 | inherit_expression_dependencies(dst: temp_id, source: inherit); |
10375 | inherited_expressions.clear(); |
10376 | inherited_expressions.push_back(t: temp_id); |
10377 | |
10378 | switch (op) |
10379 | { |
10380 | case OpImageSampleDrefImplicitLod: |
10381 | case OpImageSampleImplicitLod: |
10382 | case OpImageSampleProjImplicitLod: |
10383 | case OpImageSampleProjDrefImplicitLod: |
10384 | register_control_dependent_expression(expr: temp_id); |
10385 | break; |
10386 | |
10387 | default: |
10388 | break; |
10389 | } |
10390 | expr += type_to_glsl(type: result_type) + "(" ; |
10391 | for (uint32_t c = 0; c < 4; c++) |
10392 | { |
10393 | switch (constexpr_sampler->swizzle[c]) |
10394 | { |
10395 | case MSL_COMPONENT_SWIZZLE_IDENTITY: |
10396 | expr += to_expression(id: temp_id) + "." + swizzle_names[c]; |
10397 | break; |
10398 | case MSL_COMPONENT_SWIZZLE_ZERO: |
10399 | expr += "0" ; |
10400 | break; |
10401 | case MSL_COMPONENT_SWIZZLE_ONE: |
10402 | expr += "1" ; |
10403 | break; |
10404 | case MSL_COMPONENT_SWIZZLE_R: |
10405 | case MSL_COMPONENT_SWIZZLE_G: |
10406 | case MSL_COMPONENT_SWIZZLE_B: |
10407 | case MSL_COMPONENT_SWIZZLE_A: |
10408 | expr += to_expression(id: temp_id) + "." + |
10409 | swizzle_names[constexpr_sampler->swizzle[c] - MSL_COMPONENT_SWIZZLE_R]; |
10410 | break; |
10411 | default: |
10412 | SPIRV_CROSS_THROW("Invalid component swizzle." ); |
10413 | } |
10414 | if (c < 3) |
10415 | expr += ", " ; |
10416 | } |
10417 | expr += ")" ; |
10418 | } |
10419 | } |
10420 | else |
10421 | expr += inner_expr; |
10422 | if (constexpr_sampler->ycbcr_model != MSL_SAMPLER_YCBCR_MODEL_CONVERSION_RGB_IDENTITY) |
10423 | { |
10424 | expr += join(ts: ", " , ts: constexpr_sampler->bpc, ts: ")" ); |
10425 | if (constexpr_sampler->ycbcr_model != MSL_SAMPLER_YCBCR_MODEL_CONVERSION_YCBCR_IDENTITY) |
10426 | expr += ")" ; |
10427 | } |
10428 | } |
10429 | else |
10430 | { |
10431 | expr += inner_expr; |
10432 | if (msl_options.swizzle_texture_samples && !is_gather && is_sampled_image_type(type: imgtype) && |
10433 | !is_dynamic_img_sampler) |
10434 | { |
10435 | // Add the swizzle constant from the swizzle buffer. |
10436 | expr += ", " + to_swizzle_expression(id: img) + ")" ; |
10437 | used_swizzle_buffer = true; |
10438 | } |
10439 | } |
10440 | |
10441 | return expr; |
10442 | } |
10443 | |
10444 | static string create_swizzle(MSLComponentSwizzle swizzle) |
10445 | { |
10446 | switch (swizzle) |
10447 | { |
10448 | case MSL_COMPONENT_SWIZZLE_IDENTITY: |
10449 | return "spvSwizzle::none" ; |
10450 | case MSL_COMPONENT_SWIZZLE_ZERO: |
10451 | return "spvSwizzle::zero" ; |
10452 | case MSL_COMPONENT_SWIZZLE_ONE: |
10453 | return "spvSwizzle::one" ; |
10454 | case MSL_COMPONENT_SWIZZLE_R: |
10455 | return "spvSwizzle::red" ; |
10456 | case MSL_COMPONENT_SWIZZLE_G: |
10457 | return "spvSwizzle::green" ; |
10458 | case MSL_COMPONENT_SWIZZLE_B: |
10459 | return "spvSwizzle::blue" ; |
10460 | case MSL_COMPONENT_SWIZZLE_A: |
10461 | return "spvSwizzle::alpha" ; |
10462 | default: |
10463 | SPIRV_CROSS_THROW("Invalid component swizzle." ); |
10464 | } |
10465 | } |
10466 | |
10467 | // Returns a string representation of the ID, usable as a function arg. |
10468 | // Manufacture automatic sampler arg for SampledImage texture. |
10469 | string CompilerMSL::to_func_call_arg(const SPIRFunction::Parameter &arg, uint32_t id) |
10470 | { |
10471 | string arg_str; |
10472 | |
10473 | auto &type = expression_type(id); |
10474 | bool is_dynamic_img_sampler = has_extended_decoration(id: arg.id, decoration: SPIRVCrossDecorationDynamicImageSampler); |
10475 | // If the argument *itself* is a "dynamic" combined-image sampler, then we can just pass that around. |
10476 | bool arg_is_dynamic_img_sampler = has_extended_decoration(id, decoration: SPIRVCrossDecorationDynamicImageSampler); |
10477 | if (is_dynamic_img_sampler && !arg_is_dynamic_img_sampler) |
10478 | arg_str = join(ts: "spvDynamicImageSampler<" , ts: type_to_glsl(type: get<SPIRType>(id: type.image.type)), ts: ">(" ); |
10479 | |
10480 | auto *c = maybe_get<SPIRConstant>(id); |
10481 | if (msl_options.force_native_arrays && c && !get<SPIRType>(id: c->constant_type).array.empty()) |
10482 | { |
10483 | // If we are passing a constant array directly to a function for some reason, |
10484 | // the callee will expect an argument in thread const address space |
10485 | // (since we can only bind to arrays with references in MSL). |
10486 | // To resolve this, we must emit a copy in this address space. |
10487 | // This kind of code gen should be rare enough that performance is not a real concern. |
10488 | // Inline the SPIR-V to avoid this kind of suboptimal codegen. |
10489 | // |
10490 | // We risk calling this inside a continue block (invalid code), |
10491 | // so just create a thread local copy in the current function. |
10492 | arg_str = join(ts: "_" , ts&: id, ts: "_array_copy" ); |
10493 | auto &constants = current_function->constant_arrays_needed_on_stack; |
10494 | auto itr = find(first: begin(cont&: constants), last: end(cont&: constants), val: ID(id)); |
10495 | if (itr == end(cont&: constants)) |
10496 | { |
10497 | force_recompile(); |
10498 | constants.push_back(t: id); |
10499 | } |
10500 | } |
10501 | else |
10502 | arg_str += CompilerGLSL::to_func_call_arg(arg, id); |
10503 | |
10504 | // Need to check the base variable in case we need to apply a qualified alias. |
10505 | uint32_t var_id = 0; |
10506 | auto *var = maybe_get<SPIRVariable>(id); |
10507 | if (var) |
10508 | var_id = var->basevariable; |
10509 | |
10510 | if (!arg_is_dynamic_img_sampler) |
10511 | { |
10512 | auto *constexpr_sampler = find_constexpr_sampler(id: var_id ? var_id : id); |
10513 | if (type.basetype == SPIRType::SampledImage) |
10514 | { |
10515 | // Manufacture automatic plane args for multiplanar texture |
10516 | uint32_t planes = 1; |
10517 | if (constexpr_sampler && constexpr_sampler->ycbcr_conversion_enable) |
10518 | { |
10519 | planes = constexpr_sampler->planes; |
10520 | // If this parameter isn't aliasing a global, then we need to use |
10521 | // the special "dynamic image-sampler" class to pass it--and we need |
10522 | // to use it for *every* non-alias parameter, in case a combined |
10523 | // image-sampler with a Y'CbCr conversion is passed. Hopefully, this |
10524 | // pathological case is so rare that it should never be hit in practice. |
10525 | if (!arg.alias_global_variable) |
10526 | add_spv_func_and_recompile(spv_func: SPVFuncImplDynamicImageSampler); |
10527 | } |
10528 | for (uint32_t i = 1; i < planes; i++) |
10529 | arg_str += join(ts: ", " , ts: CompilerGLSL::to_func_call_arg(arg, id), ts&: plane_name_suffix, ts&: i); |
10530 | // Manufacture automatic sampler arg if the arg is a SampledImage texture. |
10531 | if (type.image.dim != DimBuffer) |
10532 | arg_str += ", " + to_sampler_expression(id: var_id ? var_id : id); |
10533 | |
10534 | // Add sampler Y'CbCr conversion info if we have it |
10535 | if (is_dynamic_img_sampler && constexpr_sampler && constexpr_sampler->ycbcr_conversion_enable) |
10536 | { |
10537 | SmallVector<string> samp_args; |
10538 | |
10539 | switch (constexpr_sampler->resolution) |
10540 | { |
10541 | case MSL_FORMAT_RESOLUTION_444: |
10542 | // Default |
10543 | break; |
10544 | case MSL_FORMAT_RESOLUTION_422: |
10545 | samp_args.push_back(t: "spvFormatResolution::_422" ); |
10546 | break; |
10547 | case MSL_FORMAT_RESOLUTION_420: |
10548 | samp_args.push_back(t: "spvFormatResolution::_420" ); |
10549 | break; |
10550 | default: |
10551 | SPIRV_CROSS_THROW("Invalid format resolution." ); |
10552 | } |
10553 | |
10554 | if (constexpr_sampler->chroma_filter != MSL_SAMPLER_FILTER_NEAREST) |
10555 | samp_args.push_back(t: "spvChromaFilter::linear" ); |
10556 | |
10557 | if (constexpr_sampler->x_chroma_offset != MSL_CHROMA_LOCATION_COSITED_EVEN) |
10558 | samp_args.push_back(t: "spvXChromaLocation::midpoint" ); |
10559 | if (constexpr_sampler->y_chroma_offset != MSL_CHROMA_LOCATION_COSITED_EVEN) |
10560 | samp_args.push_back(t: "spvYChromaLocation::midpoint" ); |
10561 | switch (constexpr_sampler->ycbcr_model) |
10562 | { |
10563 | case MSL_SAMPLER_YCBCR_MODEL_CONVERSION_RGB_IDENTITY: |
10564 | // Default |
10565 | break; |
10566 | case MSL_SAMPLER_YCBCR_MODEL_CONVERSION_YCBCR_IDENTITY: |
10567 | samp_args.push_back(t: "spvYCbCrModelConversion::ycbcr_identity" ); |
10568 | break; |
10569 | case MSL_SAMPLER_YCBCR_MODEL_CONVERSION_YCBCR_BT_709: |
10570 | samp_args.push_back(t: "spvYCbCrModelConversion::ycbcr_bt_709" ); |
10571 | break; |
10572 | case MSL_SAMPLER_YCBCR_MODEL_CONVERSION_YCBCR_BT_601: |
10573 | samp_args.push_back(t: "spvYCbCrModelConversion::ycbcr_bt_601" ); |
10574 | break; |
10575 | case MSL_SAMPLER_YCBCR_MODEL_CONVERSION_YCBCR_BT_2020: |
10576 | samp_args.push_back(t: "spvYCbCrModelConversion::ycbcr_bt_2020" ); |
10577 | break; |
10578 | default: |
10579 | SPIRV_CROSS_THROW("Invalid Y'CbCr model conversion." ); |
10580 | } |
10581 | if (constexpr_sampler->ycbcr_range != MSL_SAMPLER_YCBCR_RANGE_ITU_FULL) |
10582 | samp_args.push_back(t: "spvYCbCrRange::itu_narrow" ); |
10583 | samp_args.push_back(t: join(ts: "spvComponentBits(" , ts: constexpr_sampler->bpc, ts: ")" )); |
10584 | arg_str += join(ts: ", spvYCbCrSampler(" , ts: merge(list: samp_args), ts: ")" ); |
10585 | } |
10586 | } |
10587 | |
10588 | if (is_dynamic_img_sampler && constexpr_sampler && constexpr_sampler->ycbcr_conversion_enable) |
10589 | arg_str += join(ts: ", (uint(" , ts: create_swizzle(swizzle: constexpr_sampler->swizzle[3]), ts: ") << 24) | (uint(" , |
10590 | ts: create_swizzle(swizzle: constexpr_sampler->swizzle[2]), ts: ") << 16) | (uint(" , |
10591 | ts: create_swizzle(swizzle: constexpr_sampler->swizzle[1]), ts: ") << 8) | uint(" , |
10592 | ts: create_swizzle(swizzle: constexpr_sampler->swizzle[0]), ts: ")" ); |
10593 | else if (msl_options.swizzle_texture_samples && has_sampled_images && is_sampled_image_type(type)) |
10594 | arg_str += ", " + to_swizzle_expression(id: var_id ? var_id : id); |
10595 | |
10596 | if (buffers_requiring_array_length.count(x: var_id)) |
10597 | arg_str += ", " + to_buffer_size_expression(id: var_id ? var_id : id); |
10598 | |
10599 | if (is_dynamic_img_sampler) |
10600 | arg_str += ")" ; |
10601 | } |
10602 | |
10603 | // Emulate texture2D atomic operations |
10604 | auto *backing_var = maybe_get_backing_variable(chain: var_id); |
10605 | if (backing_var && atomic_image_vars.count(x: backing_var->self)) |
10606 | { |
10607 | arg_str += ", " + to_expression(id: var_id) + "_atomic" ; |
10608 | } |
10609 | |
10610 | return arg_str; |
10611 | } |
10612 | |
10613 | // If the ID represents a sampled image that has been assigned a sampler already, |
10614 | // generate an expression for the sampler, otherwise generate a fake sampler name |
10615 | // by appending a suffix to the expression constructed from the ID. |
10616 | string CompilerMSL::to_sampler_expression(uint32_t id) |
10617 | { |
10618 | auto *combined = maybe_get<SPIRCombinedImageSampler>(id); |
10619 | auto expr = to_expression(id: combined ? combined->image : VariableID(id)); |
10620 | auto index = expr.find_first_of(c: '['); |
10621 | |
10622 | uint32_t samp_id = 0; |
10623 | if (combined) |
10624 | samp_id = combined->sampler; |
10625 | |
10626 | if (index == string::npos) |
10627 | return samp_id ? to_expression(id: samp_id) : expr + sampler_name_suffix; |
10628 | else |
10629 | { |
10630 | auto image_expr = expr.substr(pos: 0, n: index); |
10631 | auto array_expr = expr.substr(pos: index); |
10632 | return samp_id ? to_expression(id: samp_id) : (image_expr + sampler_name_suffix + array_expr); |
10633 | } |
10634 | } |
10635 | |
10636 | string CompilerMSL::to_swizzle_expression(uint32_t id) |
10637 | { |
10638 | auto *combined = maybe_get<SPIRCombinedImageSampler>(id); |
10639 | |
10640 | auto expr = to_expression(id: combined ? combined->image : VariableID(id)); |
10641 | auto index = expr.find_first_of(c: '['); |
10642 | |
10643 | // If an image is part of an argument buffer translate this to a legal identifier. |
10644 | string::size_type period = 0; |
10645 | while ((period = expr.find_first_of(c: '.', pos: period)) != string::npos && period < index) |
10646 | expr[period] = '_'; |
10647 | |
10648 | if (index == string::npos) |
10649 | return expr + swizzle_name_suffix; |
10650 | else |
10651 | { |
10652 | auto image_expr = expr.substr(pos: 0, n: index); |
10653 | auto array_expr = expr.substr(pos: index); |
10654 | return image_expr + swizzle_name_suffix + array_expr; |
10655 | } |
10656 | } |
10657 | |
10658 | string CompilerMSL::to_buffer_size_expression(uint32_t id) |
10659 | { |
10660 | auto expr = to_expression(id); |
10661 | auto index = expr.find_first_of(c: '['); |
10662 | |
10663 | // This is quite crude, but we need to translate the reference name (*spvDescriptorSetN.name) to |
10664 | // the pointer expression spvDescriptorSetN.name to make a reasonable expression here. |
10665 | // This only happens if we have argument buffers and we are using OpArrayLength on a lone SSBO in that set. |
10666 | if (expr.size() >= 3 && expr[0] == '(' && expr[1] == '*') |
10667 | expr = address_of_expression(expr); |
10668 | |
10669 | // If a buffer is part of an argument buffer translate this to a legal identifier. |
10670 | for (auto &c : expr) |
10671 | if (c == '.') |
10672 | c = '_'; |
10673 | |
10674 | if (index == string::npos) |
10675 | return expr + buffer_size_name_suffix; |
10676 | else |
10677 | { |
10678 | auto buffer_expr = expr.substr(pos: 0, n: index); |
10679 | auto array_expr = expr.substr(pos: index); |
10680 | return buffer_expr + buffer_size_name_suffix + array_expr; |
10681 | } |
10682 | } |
10683 | |
10684 | // Checks whether the type is a Block all of whose members have DecorationPatch. |
10685 | bool CompilerMSL::is_patch_block(const SPIRType &type) |
10686 | { |
10687 | if (!has_decoration(id: type.self, decoration: DecorationBlock)) |
10688 | return false; |
10689 | |
10690 | for (uint32_t i = 0; i < type.member_types.size(); i++) |
10691 | { |
10692 | if (!has_member_decoration(id: type.self, index: i, decoration: DecorationPatch)) |
10693 | return false; |
10694 | } |
10695 | |
10696 | return true; |
10697 | } |
10698 | |
10699 | // Checks whether the ID is a row_major matrix that requires conversion before use |
10700 | bool CompilerMSL::is_non_native_row_major_matrix(uint32_t id) |
10701 | { |
10702 | auto *e = maybe_get<SPIRExpression>(id); |
10703 | if (e) |
10704 | return e->need_transpose; |
10705 | else |
10706 | return has_decoration(id, decoration: DecorationRowMajor); |
10707 | } |
10708 | |
10709 | // Checks whether the member is a row_major matrix that requires conversion before use |
10710 | bool CompilerMSL::member_is_non_native_row_major_matrix(const SPIRType &type, uint32_t index) |
10711 | { |
10712 | return has_member_decoration(id: type.self, index, decoration: DecorationRowMajor); |
10713 | } |
10714 | |
10715 | string CompilerMSL::convert_row_major_matrix(string exp_str, const SPIRType &exp_type, uint32_t physical_type_id, |
10716 | bool is_packed) |
10717 | { |
10718 | if (!is_matrix(type: exp_type)) |
10719 | { |
10720 | return CompilerGLSL::convert_row_major_matrix(exp_str: std::move(exp_str), exp_type, physical_type_id, is_packed); |
10721 | } |
10722 | else |
10723 | { |
10724 | strip_enclosed_expression(expr&: exp_str); |
10725 | if (physical_type_id != 0 || is_packed) |
10726 | exp_str = unpack_expression_type(expr_str: exp_str, type: exp_type, physical_type_id, packed: is_packed, row_major: true); |
10727 | return join(ts: "transpose(" , ts&: exp_str, ts: ")" ); |
10728 | } |
10729 | } |
10730 | |
10731 | // Called automatically at the end of the entry point function |
10732 | void CompilerMSL::emit_fixup() |
10733 | { |
10734 | if (is_vertex_like_shader() && stage_out_var_id && !qual_pos_var_name.empty() && !capture_output_to_buffer) |
10735 | { |
10736 | if (options.vertex.fixup_clipspace) |
10737 | statement(ts&: qual_pos_var_name, ts: ".z = (" , ts&: qual_pos_var_name, ts: ".z + " , ts&: qual_pos_var_name, |
10738 | ts: ".w) * 0.5; // Adjust clip-space for Metal" ); |
10739 | |
10740 | if (options.vertex.flip_vert_y) |
10741 | statement(ts&: qual_pos_var_name, ts: ".y = -(" , ts&: qual_pos_var_name, ts: ".y);" , ts: " // Invert Y-axis for Metal" ); |
10742 | } |
10743 | } |
10744 | |
10745 | // Return a string defining a structure member, with padding and packing. |
10746 | string CompilerMSL::to_struct_member(const SPIRType &type, uint32_t member_type_id, uint32_t index, |
10747 | const string &qualifier) |
10748 | { |
10749 | if (member_is_remapped_physical_type(type, index)) |
10750 | member_type_id = get_extended_member_decoration(type: type.self, index, decoration: SPIRVCrossDecorationPhysicalTypeID); |
10751 | auto &physical_type = get<SPIRType>(id: member_type_id); |
10752 | |
10753 | // If this member is packed, mark it as so. |
10754 | string pack_pfx; |
10755 | |
10756 | // Allow Metal to use the array<T> template to make arrays a value type |
10757 | uint32_t orig_id = 0; |
10758 | if (has_extended_member_decoration(type: type.self, index, decoration: SPIRVCrossDecorationInterfaceOrigID)) |
10759 | orig_id = get_extended_member_decoration(type: type.self, index, decoration: SPIRVCrossDecorationInterfaceOrigID); |
10760 | |
10761 | bool row_major = false; |
10762 | if (is_matrix(type: physical_type)) |
10763 | row_major = has_member_decoration(id: type.self, index, decoration: DecorationRowMajor); |
10764 | |
10765 | SPIRType row_major_physical_type; |
10766 | const SPIRType *declared_type = &physical_type; |
10767 | |
10768 | // If a struct is being declared with physical layout, |
10769 | // do not use array<T> wrappers. |
10770 | // This avoids a lot of complicated cases with packed vectors and matrices, |
10771 | // and generally we cannot copy full arrays in and out of buffers into Function |
10772 | // address space. |
10773 | // Array of resources should also be declared as builtin arrays. |
10774 | if (has_member_decoration(id: type.self, index, decoration: DecorationOffset)) |
10775 | is_using_builtin_array = true; |
10776 | else if (has_extended_member_decoration(type: type.self, index, decoration: SPIRVCrossDecorationResourceIndexPrimary)) |
10777 | is_using_builtin_array = true; |
10778 | |
10779 | if (member_is_packed_physical_type(type, index)) |
10780 | { |
10781 | // If we're packing a matrix, output an appropriate typedef |
10782 | if (physical_type.basetype == SPIRType::Struct) |
10783 | { |
10784 | SPIRV_CROSS_THROW("Cannot emit a packed struct currently." ); |
10785 | } |
10786 | else if (is_matrix(type: physical_type)) |
10787 | { |
10788 | uint32_t rows = physical_type.vecsize; |
10789 | uint32_t cols = physical_type.columns; |
10790 | pack_pfx = "packed_" ; |
10791 | if (row_major) |
10792 | { |
10793 | // These are stored transposed. |
10794 | rows = physical_type.columns; |
10795 | cols = physical_type.vecsize; |
10796 | pack_pfx = "packed_rm_" ; |
10797 | } |
10798 | string base_type = physical_type.width == 16 ? "half" : "float" ; |
10799 | string td_line = "typedef " ; |
10800 | td_line += "packed_" + base_type + to_string(val: rows); |
10801 | td_line += " " + pack_pfx; |
10802 | // Use the actual matrix size here. |
10803 | td_line += base_type + to_string(val: physical_type.columns) + "x" + to_string(val: physical_type.vecsize); |
10804 | td_line += "[" + to_string(val: cols) + "]" ; |
10805 | td_line += ";" ; |
10806 | add_typedef_line(line: td_line); |
10807 | } |
10808 | else if (!is_scalar(type: physical_type)) // scalar type is already packed. |
10809 | pack_pfx = "packed_" ; |
10810 | } |
10811 | else if (row_major) |
10812 | { |
10813 | // Need to declare type with flipped vecsize/columns. |
10814 | row_major_physical_type = physical_type; |
10815 | swap(a&: row_major_physical_type.vecsize, b&: row_major_physical_type.columns); |
10816 | declared_type = &row_major_physical_type; |
10817 | } |
10818 | |
10819 | // Very specifically, image load-store in argument buffers are disallowed on MSL on iOS. |
10820 | if (msl_options.is_ios() && physical_type.basetype == SPIRType::Image && physical_type.image.sampled == 2) |
10821 | { |
10822 | if (!has_decoration(id: orig_id, decoration: DecorationNonWritable)) |
10823 | SPIRV_CROSS_THROW("Writable images are not allowed in argument buffers on iOS." ); |
10824 | } |
10825 | |
10826 | // Array information is baked into these types. |
10827 | string array_type; |
10828 | if (physical_type.basetype != SPIRType::Image && physical_type.basetype != SPIRType::Sampler && |
10829 | physical_type.basetype != SPIRType::SampledImage) |
10830 | { |
10831 | BuiltIn builtin = BuiltInMax; |
10832 | |
10833 | // Special handling. In [[stage_out]] or [[stage_in]] blocks, |
10834 | // we need flat arrays, but if we're somehow declaring gl_PerVertex for constant array reasons, we want |
10835 | // template array types to be declared. |
10836 | bool is_ib_in_out = |
10837 | ((stage_out_var_id && get_stage_out_struct_type().self == type.self && |
10838 | variable_storage_requires_stage_io(storage: StorageClassOutput)) || |
10839 | (stage_in_var_id && get_stage_in_struct_type().self == type.self && |
10840 | variable_storage_requires_stage_io(storage: StorageClassInput))); |
10841 | if (is_ib_in_out && is_member_builtin(type, index, builtin: &builtin)) |
10842 | is_using_builtin_array = true; |
10843 | array_type = type_to_array_glsl(type: physical_type); |
10844 | } |
10845 | |
10846 | auto result = join(ts&: pack_pfx, ts: type_to_glsl(type: *declared_type, id: orig_id), ts: " " , ts: qualifier, ts: to_member_name(type, index), |
10847 | ts: member_attribute_qualifier(type, index), ts&: array_type, ts: ";" ); |
10848 | |
10849 | is_using_builtin_array = false; |
10850 | return result; |
10851 | } |
10852 | |
10853 | // Emit a structure member, padding and packing to maintain the correct memeber alignments. |
10854 | void CompilerMSL::emit_struct_member(const SPIRType &type, uint32_t member_type_id, uint32_t index, |
10855 | const string &qualifier, uint32_t) |
10856 | { |
10857 | // If this member requires padding to maintain its declared offset, emit a dummy padding member before it. |
10858 | if (has_extended_member_decoration(type: type.self, index, decoration: SPIRVCrossDecorationPaddingTarget)) |
10859 | { |
10860 | uint32_t pad_len = get_extended_member_decoration(type: type.self, index, decoration: SPIRVCrossDecorationPaddingTarget); |
10861 | statement(ts: "char _m" , ts&: index, ts: "_pad" , ts: "[" , ts&: pad_len, ts: "];" ); |
10862 | } |
10863 | |
10864 | // Handle HLSL-style 0-based vertex/instance index. |
10865 | builtin_declaration = true; |
10866 | statement(ts: to_struct_member(type, member_type_id, index, qualifier)); |
10867 | builtin_declaration = false; |
10868 | } |
10869 | |
10870 | void CompilerMSL::emit_struct_padding_target(const SPIRType &type) |
10871 | { |
10872 | uint32_t struct_size = get_declared_struct_size_msl(struct_type: type, ignore_alignment: true, ignore_padding: true); |
10873 | uint32_t target_size = get_extended_decoration(id: type.self, decoration: SPIRVCrossDecorationPaddingTarget); |
10874 | if (target_size < struct_size) |
10875 | SPIRV_CROSS_THROW("Cannot pad with negative bytes." ); |
10876 | else if (target_size > struct_size) |
10877 | statement(ts: "char _m0_final_padding[" , ts: target_size - struct_size, ts: "];" ); |
10878 | } |
10879 | |
10880 | // Return a MSL qualifier for the specified function attribute member |
10881 | string CompilerMSL::member_attribute_qualifier(const SPIRType &type, uint32_t index) |
10882 | { |
10883 | auto &execution = get_entry_point(); |
10884 | |
10885 | uint32_t mbr_type_id = type.member_types[index]; |
10886 | auto &mbr_type = get<SPIRType>(id: mbr_type_id); |
10887 | |
10888 | BuiltIn builtin = BuiltInMax; |
10889 | bool is_builtin = is_member_builtin(type, index, builtin: &builtin); |
10890 | |
10891 | if (has_extended_member_decoration(type: type.self, index, decoration: SPIRVCrossDecorationResourceIndexPrimary)) |
10892 | { |
10893 | string quals = join( |
10894 | ts: " [[id(" , ts: get_extended_member_decoration(type: type.self, index, decoration: SPIRVCrossDecorationResourceIndexPrimary), ts: ")" ); |
10895 | if (interlocked_resources.count( |
10896 | x: get_extended_member_decoration(type: type.self, index, decoration: SPIRVCrossDecorationInterfaceOrigID))) |
10897 | quals += ", raster_order_group(0)" ; |
10898 | quals += "]]" ; |
10899 | return quals; |
10900 | } |
10901 | |
10902 | // Vertex function inputs |
10903 | if (execution.model == ExecutionModelVertex && type.storage == StorageClassInput) |
10904 | { |
10905 | if (is_builtin) |
10906 | { |
10907 | switch (builtin) |
10908 | { |
10909 | case BuiltInVertexId: |
10910 | case BuiltInVertexIndex: |
10911 | case BuiltInBaseVertex: |
10912 | case BuiltInInstanceId: |
10913 | case BuiltInInstanceIndex: |
10914 | case BuiltInBaseInstance: |
10915 | if (msl_options.vertex_for_tessellation) |
10916 | return "" ; |
10917 | return string(" [[" ) + builtin_qualifier(builtin) + "]]" ; |
10918 | |
10919 | case BuiltInDrawIndex: |
10920 | SPIRV_CROSS_THROW("DrawIndex is not supported in MSL." ); |
10921 | |
10922 | default: |
10923 | return "" ; |
10924 | } |
10925 | } |
10926 | |
10927 | uint32_t locn; |
10928 | if (is_builtin) |
10929 | locn = get_or_allocate_builtin_input_member_location(builtin, type_id: type.self, index); |
10930 | else |
10931 | locn = get_member_location(type_id: type.self, index); |
10932 | |
10933 | if (locn != k_unknown_location) |
10934 | return string(" [[attribute(" ) + convert_to_string(t: locn) + ")]]" ; |
10935 | } |
10936 | |
10937 | // Vertex and tessellation evaluation function outputs |
10938 | if (((execution.model == ExecutionModelVertex && !msl_options.vertex_for_tessellation) || |
10939 | execution.model == ExecutionModelTessellationEvaluation) && |
10940 | type.storage == StorageClassOutput) |
10941 | { |
10942 | if (is_builtin) |
10943 | { |
10944 | switch (builtin) |
10945 | { |
10946 | case BuiltInPointSize: |
10947 | // Only mark the PointSize builtin if really rendering points. |
10948 | // Some shaders may include a PointSize builtin even when used to render |
10949 | // non-point topologies, and Metal will reject this builtin when compiling |
10950 | // the shader into a render pipeline that uses a non-point topology. |
10951 | return msl_options.enable_point_size_builtin ? (string(" [[" ) + builtin_qualifier(builtin) + "]]" ) : "" ; |
10952 | |
10953 | case BuiltInViewportIndex: |
10954 | if (!msl_options.supports_msl_version(major: 2, minor: 0)) |
10955 | SPIRV_CROSS_THROW("ViewportIndex requires Metal 2.0." ); |
10956 | /* fallthrough */ |
10957 | case BuiltInPosition: |
10958 | case BuiltInLayer: |
10959 | return string(" [[" ) + builtin_qualifier(builtin) + "]]" + (mbr_type.array.empty() ? "" : " " ); |
10960 | |
10961 | case BuiltInClipDistance: |
10962 | if (has_member_decoration(id: type.self, index, decoration: DecorationIndex)) |
10963 | return join(ts: " [[user(clip" , ts: get_member_decoration(id: type.self, index, decoration: DecorationIndex), ts: ")]]" ); |
10964 | else |
10965 | return string(" [[" ) + builtin_qualifier(builtin) + "]]" + (mbr_type.array.empty() ? "" : " " ); |
10966 | |
10967 | case BuiltInCullDistance: |
10968 | if (has_member_decoration(id: type.self, index, decoration: DecorationIndex)) |
10969 | return join(ts: " [[user(cull" , ts: get_member_decoration(id: type.self, index, decoration: DecorationIndex), ts: ")]]" ); |
10970 | else |
10971 | return string(" [[" ) + builtin_qualifier(builtin) + "]]" + (mbr_type.array.empty() ? "" : " " ); |
10972 | |
10973 | default: |
10974 | return "" ; |
10975 | } |
10976 | } |
10977 | string loc_qual = member_location_attribute_qualifier(type, index); |
10978 | if (!loc_qual.empty()) |
10979 | return join(ts: " [[" , ts&: loc_qual, ts: "]]" ); |
10980 | } |
10981 | |
10982 | // Tessellation control function inputs |
10983 | if (execution.model == ExecutionModelTessellationControl && type.storage == StorageClassInput) |
10984 | { |
10985 | if (is_builtin) |
10986 | { |
10987 | switch (builtin) |
10988 | { |
10989 | case BuiltInInvocationId: |
10990 | case BuiltInPrimitiveId: |
10991 | if (msl_options.multi_patch_workgroup) |
10992 | return "" ; |
10993 | return string(" [[" ) + builtin_qualifier(builtin) + "]]" + (mbr_type.array.empty() ? "" : " " ); |
10994 | case BuiltInSubgroupLocalInvocationId: // FIXME: Should work in any stage |
10995 | case BuiltInSubgroupSize: // FIXME: Should work in any stage |
10996 | if (msl_options.emulate_subgroups) |
10997 | return "" ; |
10998 | return string(" [[" ) + builtin_qualifier(builtin) + "]]" + (mbr_type.array.empty() ? "" : " " ); |
10999 | case BuiltInPatchVertices: |
11000 | return "" ; |
11001 | // Others come from stage input. |
11002 | default: |
11003 | break; |
11004 | } |
11005 | } |
11006 | if (msl_options.multi_patch_workgroup) |
11007 | return "" ; |
11008 | |
11009 | uint32_t locn; |
11010 | if (is_builtin) |
11011 | locn = get_or_allocate_builtin_input_member_location(builtin, type_id: type.self, index); |
11012 | else |
11013 | locn = get_member_location(type_id: type.self, index); |
11014 | |
11015 | if (locn != k_unknown_location) |
11016 | return string(" [[attribute(" ) + convert_to_string(t: locn) + ")]]" ; |
11017 | } |
11018 | |
11019 | // Tessellation control function outputs |
11020 | if (execution.model == ExecutionModelTessellationControl && type.storage == StorageClassOutput) |
11021 | { |
11022 | // For this type of shader, we always arrange for it to capture its |
11023 | // output to a buffer. For this reason, qualifiers are irrelevant here. |
11024 | return "" ; |
11025 | } |
11026 | |
11027 | // Tessellation evaluation function inputs |
11028 | if (execution.model == ExecutionModelTessellationEvaluation && type.storage == StorageClassInput) |
11029 | { |
11030 | if (is_builtin) |
11031 | { |
11032 | switch (builtin) |
11033 | { |
11034 | case BuiltInPrimitiveId: |
11035 | case BuiltInTessCoord: |
11036 | return string(" [[" ) + builtin_qualifier(builtin) + "]]" ; |
11037 | case BuiltInPatchVertices: |
11038 | return "" ; |
11039 | // Others come from stage input. |
11040 | default: |
11041 | break; |
11042 | } |
11043 | } |
11044 | // The special control point array must not be marked with an attribute. |
11045 | if (get_type(id: type.member_types[index]).basetype == SPIRType::ControlPointArray) |
11046 | return "" ; |
11047 | |
11048 | uint32_t locn; |
11049 | if (is_builtin) |
11050 | locn = get_or_allocate_builtin_input_member_location(builtin, type_id: type.self, index); |
11051 | else |
11052 | locn = get_member_location(type_id: type.self, index); |
11053 | |
11054 | if (locn != k_unknown_location) |
11055 | return string(" [[attribute(" ) + convert_to_string(t: locn) + ")]]" ; |
11056 | } |
11057 | |
11058 | // Tessellation evaluation function outputs were handled above. |
11059 | |
11060 | // Fragment function inputs |
11061 | if (execution.model == ExecutionModelFragment && type.storage == StorageClassInput) |
11062 | { |
11063 | string quals; |
11064 | if (is_builtin) |
11065 | { |
11066 | switch (builtin) |
11067 | { |
11068 | case BuiltInViewIndex: |
11069 | if (!msl_options.multiview || !msl_options.multiview_layered_rendering) |
11070 | break; |
11071 | /* fallthrough */ |
11072 | case BuiltInFrontFacing: |
11073 | case BuiltInPointCoord: |
11074 | case BuiltInFragCoord: |
11075 | case BuiltInSampleId: |
11076 | case BuiltInSampleMask: |
11077 | case BuiltInLayer: |
11078 | case BuiltInBaryCoordKHR: |
11079 | case BuiltInBaryCoordNoPerspKHR: |
11080 | quals = builtin_qualifier(builtin); |
11081 | break; |
11082 | |
11083 | case BuiltInClipDistance: |
11084 | return join(ts: " [[user(clip" , ts: get_member_decoration(id: type.self, index, decoration: DecorationIndex), ts: ")]]" ); |
11085 | case BuiltInCullDistance: |
11086 | return join(ts: " [[user(cull" , ts: get_member_decoration(id: type.self, index, decoration: DecorationIndex), ts: ")]]" ); |
11087 | |
11088 | default: |
11089 | break; |
11090 | } |
11091 | } |
11092 | else |
11093 | quals = member_location_attribute_qualifier(type, index); |
11094 | |
11095 | if (builtin == BuiltInBaryCoordKHR || builtin == BuiltInBaryCoordNoPerspKHR) |
11096 | { |
11097 | if (has_member_decoration(id: type.self, index, decoration: DecorationFlat) || |
11098 | has_member_decoration(id: type.self, index, decoration: DecorationCentroid) || |
11099 | has_member_decoration(id: type.self, index, decoration: DecorationSample) || |
11100 | has_member_decoration(id: type.self, index, decoration: DecorationNoPerspective)) |
11101 | { |
11102 | // NoPerspective is baked into the builtin type. |
11103 | SPIRV_CROSS_THROW( |
11104 | "Flat, Centroid, Sample, NoPerspective decorations are not supported for BaryCoord inputs." ); |
11105 | } |
11106 | } |
11107 | |
11108 | // Don't bother decorating integers with the 'flat' attribute; it's |
11109 | // the default (in fact, the only option). Also don't bother with the |
11110 | // FragCoord builtin; it's always noperspective on Metal. |
11111 | if (!type_is_integral(type: mbr_type) && (!is_builtin || builtin != BuiltInFragCoord)) |
11112 | { |
11113 | if (has_member_decoration(id: type.self, index, decoration: DecorationFlat)) |
11114 | { |
11115 | if (!quals.empty()) |
11116 | quals += ", " ; |
11117 | quals += "flat" ; |
11118 | } |
11119 | else if (has_member_decoration(id: type.self, index, decoration: DecorationCentroid)) |
11120 | { |
11121 | if (!quals.empty()) |
11122 | quals += ", " ; |
11123 | if (has_member_decoration(id: type.self, index, decoration: DecorationNoPerspective)) |
11124 | quals += "centroid_no_perspective" ; |
11125 | else |
11126 | quals += "centroid_perspective" ; |
11127 | } |
11128 | else if (has_member_decoration(id: type.self, index, decoration: DecorationSample)) |
11129 | { |
11130 | if (!quals.empty()) |
11131 | quals += ", " ; |
11132 | if (has_member_decoration(id: type.self, index, decoration: DecorationNoPerspective)) |
11133 | quals += "sample_no_perspective" ; |
11134 | else |
11135 | quals += "sample_perspective" ; |
11136 | } |
11137 | else if (has_member_decoration(id: type.self, index, decoration: DecorationNoPerspective)) |
11138 | { |
11139 | if (!quals.empty()) |
11140 | quals += ", " ; |
11141 | quals += "center_no_perspective" ; |
11142 | } |
11143 | } |
11144 | |
11145 | if (!quals.empty()) |
11146 | return " [[" + quals + "]]" ; |
11147 | } |
11148 | |
11149 | // Fragment function outputs |
11150 | if (execution.model == ExecutionModelFragment && type.storage == StorageClassOutput) |
11151 | { |
11152 | if (is_builtin) |
11153 | { |
11154 | switch (builtin) |
11155 | { |
11156 | case BuiltInFragStencilRefEXT: |
11157 | // Similar to PointSize, only mark FragStencilRef if there's a stencil buffer. |
11158 | // Some shaders may include a FragStencilRef builtin even when used to render |
11159 | // without a stencil attachment, and Metal will reject this builtin |
11160 | // when compiling the shader into a render pipeline that does not set |
11161 | // stencilAttachmentPixelFormat. |
11162 | if (!msl_options.enable_frag_stencil_ref_builtin) |
11163 | return "" ; |
11164 | if (!msl_options.supports_msl_version(major: 2, minor: 1)) |
11165 | SPIRV_CROSS_THROW("Stencil export only supported in MSL 2.1 and up." ); |
11166 | return string(" [[" ) + builtin_qualifier(builtin) + "]]" ; |
11167 | |
11168 | case BuiltInFragDepth: |
11169 | // Ditto FragDepth. |
11170 | if (!msl_options.enable_frag_depth_builtin) |
11171 | return "" ; |
11172 | /* fallthrough */ |
11173 | case BuiltInSampleMask: |
11174 | return string(" [[" ) + builtin_qualifier(builtin) + "]]" ; |
11175 | |
11176 | default: |
11177 | return "" ; |
11178 | } |
11179 | } |
11180 | uint32_t locn = get_member_location(type_id: type.self, index); |
11181 | // Metal will likely complain about missing color attachments, too. |
11182 | if (locn != k_unknown_location && !(msl_options.enable_frag_output_mask & (1 << locn))) |
11183 | return "" ; |
11184 | if (locn != k_unknown_location && has_member_decoration(id: type.self, index, decoration: DecorationIndex)) |
11185 | return join(ts: " [[color(" , ts&: locn, ts: "), index(" , ts: get_member_decoration(id: type.self, index, decoration: DecorationIndex), |
11186 | ts: ")]]" ); |
11187 | else if (locn != k_unknown_location) |
11188 | return join(ts: " [[color(" , ts&: locn, ts: ")]]" ); |
11189 | else if (has_member_decoration(id: type.self, index, decoration: DecorationIndex)) |
11190 | return join(ts: " [[index(" , ts: get_member_decoration(id: type.self, index, decoration: DecorationIndex), ts: ")]]" ); |
11191 | else |
11192 | return "" ; |
11193 | } |
11194 | |
11195 | // Compute function inputs |
11196 | if (execution.model == ExecutionModelGLCompute && type.storage == StorageClassInput) |
11197 | { |
11198 | if (is_builtin) |
11199 | { |
11200 | switch (builtin) |
11201 | { |
11202 | case BuiltInNumSubgroups: |
11203 | case BuiltInSubgroupId: |
11204 | case BuiltInSubgroupLocalInvocationId: // FIXME: Should work in any stage |
11205 | case BuiltInSubgroupSize: // FIXME: Should work in any stage |
11206 | if (msl_options.emulate_subgroups) |
11207 | break; |
11208 | /* fallthrough */ |
11209 | case BuiltInGlobalInvocationId: |
11210 | case BuiltInWorkgroupId: |
11211 | case BuiltInNumWorkgroups: |
11212 | case BuiltInLocalInvocationId: |
11213 | case BuiltInLocalInvocationIndex: |
11214 | return string(" [[" ) + builtin_qualifier(builtin) + "]]" ; |
11215 | |
11216 | default: |
11217 | return "" ; |
11218 | } |
11219 | } |
11220 | } |
11221 | |
11222 | return "" ; |
11223 | } |
11224 | |
11225 | // A user-defined output variable is considered to match an input variable in the subsequent |
11226 | // stage if the two variables are declared with the same Location and Component decoration and |
11227 | // match in type and decoration, except that interpolation decorations are not required to match. |
11228 | // For the purposes of interface matching, variables declared without a Component decoration are |
11229 | // considered to have a Component decoration of zero. |
11230 | string CompilerMSL::member_location_attribute_qualifier(const SPIRType &type, uint32_t index) |
11231 | { |
11232 | string quals; |
11233 | uint32_t comp; |
11234 | uint32_t locn = get_member_location(type_id: type.self, index, comp: &comp); |
11235 | if (locn != k_unknown_location) |
11236 | { |
11237 | quals += "user(locn" ; |
11238 | quals += convert_to_string(t: locn); |
11239 | if (comp != k_unknown_component && comp != 0) |
11240 | { |
11241 | quals += "_" ; |
11242 | quals += convert_to_string(t: comp); |
11243 | } |
11244 | quals += ")" ; |
11245 | } |
11246 | return quals; |
11247 | } |
11248 | |
11249 | // Returns the location decoration of the member with the specified index in the specified type. |
11250 | // If the location of the member has been explicitly set, that location is used. If not, this |
11251 | // function assumes the members are ordered in their location order, and simply returns the |
11252 | // index as the location. |
11253 | uint32_t CompilerMSL::get_member_location(uint32_t type_id, uint32_t index, uint32_t *comp) const |
11254 | { |
11255 | if (comp) |
11256 | { |
11257 | if (has_member_decoration(id: type_id, index, decoration: DecorationComponent)) |
11258 | *comp = get_member_decoration(id: type_id, index, decoration: DecorationComponent); |
11259 | else |
11260 | *comp = k_unknown_component; |
11261 | } |
11262 | |
11263 | if (has_member_decoration(id: type_id, index, decoration: DecorationLocation)) |
11264 | return get_member_decoration(id: type_id, index, decoration: DecorationLocation); |
11265 | else |
11266 | return k_unknown_location; |
11267 | } |
11268 | |
11269 | uint32_t CompilerMSL::get_or_allocate_builtin_input_member_location(spv::BuiltIn builtin, |
11270 | uint32_t type_id, uint32_t index, |
11271 | uint32_t *comp) |
11272 | { |
11273 | uint32_t loc = get_member_location(type_id, index, comp); |
11274 | if (loc != k_unknown_location) |
11275 | return loc; |
11276 | |
11277 | if (comp) |
11278 | *comp = k_unknown_component; |
11279 | |
11280 | // Late allocation. Find a location which is unused by the application. |
11281 | // This can happen for built-in inputs in tessellation which are mixed and matched with user inputs. |
11282 | auto &mbr_type = get<SPIRType>(id: get<SPIRType>(id: type_id).member_types[index]); |
11283 | uint32_t count = type_to_location_count(type: mbr_type); |
11284 | |
11285 | loc = 0; |
11286 | |
11287 | const auto location_range_in_use = [this](uint32_t location, uint32_t location_count) -> bool { |
11288 | for (uint32_t i = 0; i < location_count; i++) |
11289 | if (location_inputs_in_use.count(x: location + i) != 0) |
11290 | return true; |
11291 | return false; |
11292 | }; |
11293 | |
11294 | while (location_range_in_use(loc, count)) |
11295 | loc++; |
11296 | |
11297 | set_member_decoration(id: type_id, index, decoration: DecorationLocation, argument: loc); |
11298 | |
11299 | // Triangle tess level inputs are shared in one packed float4, |
11300 | // mark both builtins as sharing one location. |
11301 | if (get_execution_mode_bitset().get(bit: ExecutionModeTriangles) && |
11302 | (builtin == BuiltInTessLevelInner || builtin == BuiltInTessLevelOuter)) |
11303 | { |
11304 | builtin_to_automatic_input_location[BuiltInTessLevelInner] = loc; |
11305 | builtin_to_automatic_input_location[BuiltInTessLevelOuter] = loc; |
11306 | } |
11307 | else |
11308 | builtin_to_automatic_input_location[builtin] = loc; |
11309 | |
11310 | mark_location_as_used_by_shader(location: loc, type: mbr_type, storage: StorageClassInput, fallback: true); |
11311 | return loc; |
11312 | } |
11313 | |
11314 | // Returns the type declaration for a function, including the |
11315 | // entry type if the current function is the entry point function |
11316 | string CompilerMSL::func_type_decl(SPIRType &type) |
11317 | { |
11318 | // The regular function return type. If not processing the entry point function, that's all we need |
11319 | string return_type = type_to_glsl(type) + type_to_array_glsl(type); |
11320 | if (!processing_entry_point) |
11321 | return return_type; |
11322 | |
11323 | // If an outgoing interface block has been defined, and it should be returned, override the entry point return type |
11324 | bool ep_should_return_output = !get_is_rasterization_disabled(); |
11325 | if (stage_out_var_id && ep_should_return_output) |
11326 | return_type = type_to_glsl(type: get_stage_out_struct_type()) + type_to_array_glsl(type); |
11327 | |
11328 | // Prepend a entry type, based on the execution model |
11329 | string entry_type; |
11330 | auto &execution = get_entry_point(); |
11331 | switch (execution.model) |
11332 | { |
11333 | case ExecutionModelVertex: |
11334 | if (msl_options.vertex_for_tessellation && !msl_options.supports_msl_version(major: 1, minor: 2)) |
11335 | SPIRV_CROSS_THROW("Tessellation requires Metal 1.2." ); |
11336 | entry_type = msl_options.vertex_for_tessellation ? "kernel" : "vertex" ; |
11337 | break; |
11338 | case ExecutionModelTessellationEvaluation: |
11339 | if (!msl_options.supports_msl_version(major: 1, minor: 2)) |
11340 | SPIRV_CROSS_THROW("Tessellation requires Metal 1.2." ); |
11341 | if (execution.flags.get(bit: ExecutionModeIsolines)) |
11342 | SPIRV_CROSS_THROW("Metal does not support isoline tessellation." ); |
11343 | if (msl_options.is_ios()) |
11344 | entry_type = |
11345 | join(ts: "[[ patch(" , ts: execution.flags.get(bit: ExecutionModeTriangles) ? "triangle" : "quad" , ts: ") ]] vertex" ); |
11346 | else |
11347 | entry_type = join(ts: "[[ patch(" , ts: execution.flags.get(bit: ExecutionModeTriangles) ? "triangle" : "quad" , ts: ", " , |
11348 | ts&: execution.output_vertices, ts: ") ]] vertex" ); |
11349 | break; |
11350 | case ExecutionModelFragment: |
11351 | entry_type = uses_explicit_early_fragment_test() ? "[[ early_fragment_tests ]] fragment" : "fragment" ; |
11352 | break; |
11353 | case ExecutionModelTessellationControl: |
11354 | if (!msl_options.supports_msl_version(major: 1, minor: 2)) |
11355 | SPIRV_CROSS_THROW("Tessellation requires Metal 1.2." ); |
11356 | if (execution.flags.get(bit: ExecutionModeIsolines)) |
11357 | SPIRV_CROSS_THROW("Metal does not support isoline tessellation." ); |
11358 | /* fallthrough */ |
11359 | case ExecutionModelGLCompute: |
11360 | case ExecutionModelKernel: |
11361 | entry_type = "kernel" ; |
11362 | break; |
11363 | default: |
11364 | entry_type = "unknown" ; |
11365 | break; |
11366 | } |
11367 | |
11368 | return entry_type + " " + return_type; |
11369 | } |
11370 | |
11371 | bool CompilerMSL::uses_explicit_early_fragment_test() |
11372 | { |
11373 | auto &ep_flags = get_entry_point().flags; |
11374 | return ep_flags.get(bit: ExecutionModeEarlyFragmentTests) || ep_flags.get(bit: ExecutionModePostDepthCoverage); |
11375 | } |
11376 | |
11377 | // In MSL, address space qualifiers are required for all pointer or reference variables |
11378 | string CompilerMSL::get_argument_address_space(const SPIRVariable &argument) |
11379 | { |
11380 | const auto &type = get<SPIRType>(id: argument.basetype); |
11381 | return get_type_address_space(type, id: argument.self, argument: true); |
11382 | } |
11383 | |
11384 | string CompilerMSL::get_type_address_space(const SPIRType &type, uint32_t id, bool argument) |
11385 | { |
11386 | // This can be called for variable pointer contexts as well, so be very careful about which method we choose. |
11387 | Bitset flags; |
11388 | auto *var = maybe_get<SPIRVariable>(id); |
11389 | if (var && type.basetype == SPIRType::Struct && |
11390 | (has_decoration(id: type.self, decoration: DecorationBlock) || has_decoration(id: type.self, decoration: DecorationBufferBlock))) |
11391 | flags = get_buffer_block_flags(id); |
11392 | else |
11393 | flags = get_decoration_bitset(id); |
11394 | |
11395 | const char *addr_space = nullptr; |
11396 | switch (type.storage) |
11397 | { |
11398 | case StorageClassWorkgroup: |
11399 | addr_space = "threadgroup" ; |
11400 | break; |
11401 | |
11402 | case StorageClassStorageBuffer: |
11403 | { |
11404 | // For arguments from variable pointers, we use the write count deduction, so |
11405 | // we should not assume any constness here. Only for global SSBOs. |
11406 | bool readonly = false; |
11407 | if (!var || has_decoration(id: type.self, decoration: DecorationBlock)) |
11408 | readonly = flags.get(bit: DecorationNonWritable); |
11409 | |
11410 | addr_space = readonly ? "const device" : "device" ; |
11411 | break; |
11412 | } |
11413 | |
11414 | case StorageClassUniform: |
11415 | case StorageClassUniformConstant: |
11416 | case StorageClassPushConstant: |
11417 | if (type.basetype == SPIRType::Struct) |
11418 | { |
11419 | bool ssbo = has_decoration(id: type.self, decoration: DecorationBufferBlock); |
11420 | if (ssbo) |
11421 | addr_space = flags.get(bit: DecorationNonWritable) ? "const device" : "device" ; |
11422 | else |
11423 | addr_space = "constant" ; |
11424 | } |
11425 | else if (!argument) |
11426 | { |
11427 | addr_space = "constant" ; |
11428 | } |
11429 | else if (type_is_msl_framebuffer_fetch(type)) |
11430 | { |
11431 | // Subpass inputs are passed around by value. |
11432 | addr_space = "" ; |
11433 | } |
11434 | break; |
11435 | |
11436 | case StorageClassFunction: |
11437 | case StorageClassGeneric: |
11438 | break; |
11439 | |
11440 | case StorageClassInput: |
11441 | if (get_execution_model() == ExecutionModelTessellationControl && var && |
11442 | var->basevariable == stage_in_ptr_var_id) |
11443 | addr_space = msl_options.multi_patch_workgroup ? "constant" : "threadgroup" ; |
11444 | if (get_execution_model() == ExecutionModelFragment && var && var->basevariable == stage_in_var_id) |
11445 | addr_space = "thread" ; |
11446 | break; |
11447 | |
11448 | case StorageClassOutput: |
11449 | if (capture_output_to_buffer) |
11450 | { |
11451 | if (var && type.storage == StorageClassOutput) |
11452 | { |
11453 | bool is_masked = is_stage_output_variable_masked(var: *var); |
11454 | |
11455 | if (is_masked) |
11456 | { |
11457 | if (is_tessellation_shader()) |
11458 | addr_space = "threadgroup" ; |
11459 | else |
11460 | addr_space = "thread" ; |
11461 | } |
11462 | else if (variable_decl_is_remapped_storage(variable: *var, storage: StorageClassWorkgroup)) |
11463 | addr_space = "threadgroup" ; |
11464 | } |
11465 | |
11466 | if (!addr_space) |
11467 | addr_space = "device" ; |
11468 | } |
11469 | break; |
11470 | |
11471 | default: |
11472 | break; |
11473 | } |
11474 | |
11475 | if (!addr_space) |
11476 | { |
11477 | // No address space for plain values. |
11478 | addr_space = type.pointer || (argument && type.basetype == SPIRType::ControlPointArray) ? "thread" : "" ; |
11479 | } |
11480 | |
11481 | return join(ts: flags.get(bit: DecorationVolatile) || flags.get(bit: DecorationCoherent) ? "volatile " : "" , ts&: addr_space); |
11482 | } |
11483 | |
11484 | const char *CompilerMSL::to_restrict(uint32_t id, bool space) |
11485 | { |
11486 | // This can be called for variable pointer contexts as well, so be very careful about which method we choose. |
11487 | Bitset flags; |
11488 | if (ir.ids[id].get_type() == TypeVariable) |
11489 | { |
11490 | uint32_t type_id = expression_type_id(id); |
11491 | auto &type = expression_type(id); |
11492 | if (type.basetype == SPIRType::Struct && |
11493 | (has_decoration(id: type_id, decoration: DecorationBlock) || has_decoration(id: type_id, decoration: DecorationBufferBlock))) |
11494 | flags = get_buffer_block_flags(id); |
11495 | else |
11496 | flags = get_decoration_bitset(id); |
11497 | } |
11498 | else |
11499 | flags = get_decoration_bitset(id); |
11500 | |
11501 | return flags.get(bit: DecorationRestrict) ? (space ? "restrict " : "restrict" ) : "" ; |
11502 | } |
11503 | |
11504 | string CompilerMSL::entry_point_arg_stage_in() |
11505 | { |
11506 | string decl; |
11507 | |
11508 | if (get_execution_model() == ExecutionModelTessellationControl && msl_options.multi_patch_workgroup) |
11509 | return decl; |
11510 | |
11511 | // Stage-in structure |
11512 | uint32_t stage_in_id; |
11513 | if (get_execution_model() == ExecutionModelTessellationEvaluation) |
11514 | stage_in_id = patch_stage_in_var_id; |
11515 | else |
11516 | stage_in_id = stage_in_var_id; |
11517 | |
11518 | if (stage_in_id) |
11519 | { |
11520 | auto &var = get<SPIRVariable>(id: stage_in_id); |
11521 | auto &type = get_variable_data_type(var); |
11522 | |
11523 | add_resource_name(id: var.self); |
11524 | decl = join(ts: type_to_glsl(type), ts: " " , ts: to_name(id: var.self), ts: " [[stage_in]]" ); |
11525 | } |
11526 | |
11527 | return decl; |
11528 | } |
11529 | |
11530 | // Returns true if this input builtin should be a direct parameter on a shader function parameter list, |
11531 | // and false for builtins that should be passed or calculated some other way. |
11532 | bool CompilerMSL::is_direct_input_builtin(BuiltIn bi_type) |
11533 | { |
11534 | switch (bi_type) |
11535 | { |
11536 | // Vertex function in |
11537 | case BuiltInVertexId: |
11538 | case BuiltInVertexIndex: |
11539 | case BuiltInBaseVertex: |
11540 | case BuiltInInstanceId: |
11541 | case BuiltInInstanceIndex: |
11542 | case BuiltInBaseInstance: |
11543 | return get_execution_model() != ExecutionModelVertex || !msl_options.vertex_for_tessellation; |
11544 | // Tess. control function in |
11545 | case BuiltInPosition: |
11546 | case BuiltInPointSize: |
11547 | case BuiltInClipDistance: |
11548 | case BuiltInCullDistance: |
11549 | case BuiltInPatchVertices: |
11550 | return false; |
11551 | case BuiltInInvocationId: |
11552 | case BuiltInPrimitiveId: |
11553 | return get_execution_model() != ExecutionModelTessellationControl || !msl_options.multi_patch_workgroup; |
11554 | // Tess. evaluation function in |
11555 | case BuiltInTessLevelInner: |
11556 | case BuiltInTessLevelOuter: |
11557 | return false; |
11558 | // Fragment function in |
11559 | case BuiltInSamplePosition: |
11560 | case BuiltInHelperInvocation: |
11561 | case BuiltInBaryCoordKHR: |
11562 | case BuiltInBaryCoordNoPerspKHR: |
11563 | return false; |
11564 | case BuiltInViewIndex: |
11565 | return get_execution_model() == ExecutionModelFragment && msl_options.multiview && |
11566 | msl_options.multiview_layered_rendering; |
11567 | // Compute function in |
11568 | case BuiltInSubgroupId: |
11569 | case BuiltInNumSubgroups: |
11570 | return !msl_options.emulate_subgroups; |
11571 | // Any stage function in |
11572 | case BuiltInDeviceIndex: |
11573 | case BuiltInSubgroupEqMask: |
11574 | case BuiltInSubgroupGeMask: |
11575 | case BuiltInSubgroupGtMask: |
11576 | case BuiltInSubgroupLeMask: |
11577 | case BuiltInSubgroupLtMask: |
11578 | return false; |
11579 | case BuiltInSubgroupSize: |
11580 | if (msl_options.fixed_subgroup_size != 0) |
11581 | return false; |
11582 | /* fallthrough */ |
11583 | case BuiltInSubgroupLocalInvocationId: |
11584 | return !msl_options.emulate_subgroups; |
11585 | default: |
11586 | return true; |
11587 | } |
11588 | } |
11589 | |
11590 | // Returns true if this is a fragment shader that runs per sample, and false otherwise. |
11591 | bool CompilerMSL::is_sample_rate() const |
11592 | { |
11593 | auto &caps = get_declared_capabilities(); |
11594 | return get_execution_model() == ExecutionModelFragment && |
11595 | (msl_options.force_sample_rate_shading || |
11596 | std::find(first: caps.begin(), last: caps.end(), val: CapabilitySampleRateShading) != caps.end() || |
11597 | (msl_options.use_framebuffer_fetch_subpasses && need_subpass_input)); |
11598 | } |
11599 | |
11600 | bool CompilerMSL::is_intersection_query() const |
11601 | { |
11602 | auto &caps = get_declared_capabilities(); |
11603 | return std::find(first: caps.begin(), last: caps.end(), val: CapabilityRayQueryKHR) != caps.end(); |
11604 | } |
11605 | |
11606 | void CompilerMSL::entry_point_args_builtin(string &ep_args) |
11607 | { |
11608 | // Builtin variables |
11609 | SmallVector<pair<SPIRVariable *, BuiltIn>, 8> active_builtins; |
11610 | ir.for_each_typed_id<SPIRVariable>(op: [&](uint32_t var_id, SPIRVariable &var) { |
11611 | if (var.storage != StorageClassInput) |
11612 | return; |
11613 | |
11614 | auto bi_type = BuiltIn(get_decoration(id: var_id, decoration: DecorationBuiltIn)); |
11615 | |
11616 | // Don't emit SamplePosition as a separate parameter. In the entry |
11617 | // point, we get that by calling get_sample_position() on the sample ID. |
11618 | if (is_builtin_variable(var) && |
11619 | get_variable_data_type(var).basetype != SPIRType::Struct && |
11620 | get_variable_data_type(var).basetype != SPIRType::ControlPointArray) |
11621 | { |
11622 | // If the builtin is not part of the active input builtin set, don't emit it. |
11623 | // Relevant for multiple entry-point modules which might declare unused builtins. |
11624 | if (!active_input_builtins.get(bit: bi_type) || !interface_variable_exists_in_entry_point(id: var_id)) |
11625 | return; |
11626 | |
11627 | // Remember this variable. We may need to correct its type. |
11628 | active_builtins.push_back(t: make_pair(x: &var, y&: bi_type)); |
11629 | |
11630 | if (is_direct_input_builtin(bi_type)) |
11631 | { |
11632 | if (!ep_args.empty()) |
11633 | ep_args += ", " ; |
11634 | |
11635 | // Handle HLSL-style 0-based vertex/instance index. |
11636 | builtin_declaration = true; |
11637 | |
11638 | // Handle different MSL gl_TessCoord types. (float2, float3) |
11639 | if (bi_type == BuiltInTessCoord && get_entry_point().flags.get(bit: ExecutionModeQuads)) |
11640 | ep_args += "float2 " + to_expression(id: var_id) + "In" ; |
11641 | else |
11642 | ep_args += builtin_type_decl(builtin: bi_type, id: var_id) + " " + to_expression(id: var_id); |
11643 | |
11644 | ep_args += " [[" + builtin_qualifier(builtin: bi_type); |
11645 | if (bi_type == BuiltInSampleMask && get_entry_point().flags.get(bit: ExecutionModePostDepthCoverage)) |
11646 | { |
11647 | if (!msl_options.supports_msl_version(major: 2)) |
11648 | SPIRV_CROSS_THROW("Post-depth coverage requires MSL 2.0." ); |
11649 | if (msl_options.is_macos() && !msl_options.supports_msl_version(major: 2, minor: 3)) |
11650 | SPIRV_CROSS_THROW("Post-depth coverage on Mac requires MSL 2.3." ); |
11651 | ep_args += ", post_depth_coverage" ; |
11652 | } |
11653 | ep_args += "]]" ; |
11654 | builtin_declaration = false; |
11655 | } |
11656 | } |
11657 | |
11658 | if (has_extended_decoration(id: var_id, decoration: SPIRVCrossDecorationBuiltInDispatchBase)) |
11659 | { |
11660 | // This is a special implicit builtin, not corresponding to any SPIR-V builtin, |
11661 | // which holds the base that was passed to vkCmdDispatchBase() or vkCmdDrawIndexed(). If it's present, |
11662 | // assume we emitted it for a good reason. |
11663 | assert(msl_options.supports_msl_version(1, 2)); |
11664 | if (!ep_args.empty()) |
11665 | ep_args += ", " ; |
11666 | |
11667 | ep_args += type_to_glsl(type: get_variable_data_type(var)) + " " + to_expression(id: var_id) + " [[grid_origin]]" ; |
11668 | } |
11669 | |
11670 | if (has_extended_decoration(id: var_id, decoration: SPIRVCrossDecorationBuiltInStageInputSize)) |
11671 | { |
11672 | // This is another special implicit builtin, not corresponding to any SPIR-V builtin, |
11673 | // which holds the number of vertices and instances to draw. If it's present, |
11674 | // assume we emitted it for a good reason. |
11675 | assert(msl_options.supports_msl_version(1, 2)); |
11676 | if (!ep_args.empty()) |
11677 | ep_args += ", " ; |
11678 | |
11679 | ep_args += type_to_glsl(type: get_variable_data_type(var)) + " " + to_expression(id: var_id) + " [[grid_size]]" ; |
11680 | } |
11681 | }); |
11682 | |
11683 | // Correct the types of all encountered active builtins. We couldn't do this before |
11684 | // because ensure_correct_builtin_type() may increase the bound, which isn't allowed |
11685 | // while iterating over IDs. |
11686 | for (auto &var : active_builtins) |
11687 | var.first->basetype = ensure_correct_builtin_type(type_id: var.first->basetype, builtin: var.second); |
11688 | |
11689 | // Handle HLSL-style 0-based vertex/instance index. |
11690 | if (needs_base_vertex_arg == TriState::Yes) |
11691 | ep_args += built_in_func_arg(builtin: BuiltInBaseVertex, prefix_comma: !ep_args.empty()); |
11692 | |
11693 | if (needs_base_instance_arg == TriState::Yes) |
11694 | ep_args += built_in_func_arg(builtin: BuiltInBaseInstance, prefix_comma: !ep_args.empty()); |
11695 | |
11696 | if (capture_output_to_buffer) |
11697 | { |
11698 | // Add parameters to hold the indirect draw parameters and the shader output. This has to be handled |
11699 | // specially because it needs to be a pointer, not a reference. |
11700 | if (stage_out_var_id) |
11701 | { |
11702 | if (!ep_args.empty()) |
11703 | ep_args += ", " ; |
11704 | ep_args += join(ts: "device " , ts: type_to_glsl(type: get_stage_out_struct_type()), ts: "* " , ts&: output_buffer_var_name, |
11705 | ts: " [[buffer(" , ts&: msl_options.shader_output_buffer_index, ts: ")]]" ); |
11706 | } |
11707 | |
11708 | if (get_execution_model() == ExecutionModelTessellationControl) |
11709 | { |
11710 | if (!ep_args.empty()) |
11711 | ep_args += ", " ; |
11712 | ep_args += |
11713 | join(ts: "constant uint* spvIndirectParams [[buffer(" , ts&: msl_options.indirect_params_buffer_index, ts: ")]]" ); |
11714 | } |
11715 | else if (stage_out_var_id && |
11716 | !(get_execution_model() == ExecutionModelVertex && msl_options.vertex_for_tessellation)) |
11717 | { |
11718 | if (!ep_args.empty()) |
11719 | ep_args += ", " ; |
11720 | ep_args += |
11721 | join(ts: "device uint* spvIndirectParams [[buffer(" , ts&: msl_options.indirect_params_buffer_index, ts: ")]]" ); |
11722 | } |
11723 | |
11724 | if (get_execution_model() == ExecutionModelVertex && msl_options.vertex_for_tessellation && |
11725 | (active_input_builtins.get(bit: BuiltInVertexIndex) || active_input_builtins.get(bit: BuiltInVertexId)) && |
11726 | msl_options.vertex_index_type != Options::IndexType::None) |
11727 | { |
11728 | // Add the index buffer so we can set gl_VertexIndex correctly. |
11729 | if (!ep_args.empty()) |
11730 | ep_args += ", " ; |
11731 | switch (msl_options.vertex_index_type) |
11732 | { |
11733 | case Options::IndexType::None: |
11734 | break; |
11735 | case Options::IndexType::UInt16: |
11736 | ep_args += join(ts: "const device ushort* " , ts&: index_buffer_var_name, ts: " [[buffer(" , |
11737 | ts&: msl_options.shader_index_buffer_index, ts: ")]]" ); |
11738 | break; |
11739 | case Options::IndexType::UInt32: |
11740 | ep_args += join(ts: "const device uint* " , ts&: index_buffer_var_name, ts: " [[buffer(" , |
11741 | ts&: msl_options.shader_index_buffer_index, ts: ")]]" ); |
11742 | break; |
11743 | } |
11744 | } |
11745 | |
11746 | // Tessellation control shaders get three additional parameters: |
11747 | // a buffer to hold the per-patch data, a buffer to hold the per-patch |
11748 | // tessellation levels, and a block of workgroup memory to hold the |
11749 | // input control point data. |
11750 | if (get_execution_model() == ExecutionModelTessellationControl) |
11751 | { |
11752 | if (patch_stage_out_var_id) |
11753 | { |
11754 | if (!ep_args.empty()) |
11755 | ep_args += ", " ; |
11756 | ep_args += |
11757 | join(ts: "device " , ts: type_to_glsl(type: get_patch_stage_out_struct_type()), ts: "* " , ts&: patch_output_buffer_var_name, |
11758 | ts: " [[buffer(" , ts: convert_to_string(t: msl_options.shader_patch_output_buffer_index), ts: ")]]" ); |
11759 | } |
11760 | if (!ep_args.empty()) |
11761 | ep_args += ", " ; |
11762 | ep_args += join(ts: "device " , ts: get_tess_factor_struct_name(), ts: "* " , ts&: tess_factor_buffer_var_name, ts: " [[buffer(" , |
11763 | ts: convert_to_string(t: msl_options.shader_tess_factor_buffer_index), ts: ")]]" ); |
11764 | |
11765 | // Initializer for tess factors must be handled specially since it's never declared as a normal variable. |
11766 | uint32_t outer_factor_initializer_id = 0; |
11767 | uint32_t inner_factor_initializer_id = 0; |
11768 | ir.for_each_typed_id<SPIRVariable>(op: [&](uint32_t, SPIRVariable &var) { |
11769 | if (!has_decoration(id: var.self, decoration: DecorationBuiltIn) || var.storage != StorageClassOutput || !var.initializer) |
11770 | return; |
11771 | |
11772 | BuiltIn builtin = BuiltIn(get_decoration(id: var.self, decoration: DecorationBuiltIn)); |
11773 | if (builtin == BuiltInTessLevelInner) |
11774 | inner_factor_initializer_id = var.initializer; |
11775 | else if (builtin == BuiltInTessLevelOuter) |
11776 | outer_factor_initializer_id = var.initializer; |
11777 | }); |
11778 | |
11779 | const SPIRConstant *c = nullptr; |
11780 | |
11781 | if (outer_factor_initializer_id && (c = maybe_get<SPIRConstant>(id: outer_factor_initializer_id))) |
11782 | { |
11783 | auto &entry_func = get<SPIRFunction>(id: ir.default_entry_point); |
11784 | entry_func.fixup_hooks_in.push_back(t: [=]() { |
11785 | uint32_t components = get_execution_mode_bitset().get(bit: ExecutionModeTriangles) ? 3 : 4; |
11786 | for (uint32_t i = 0; i < components; i++) |
11787 | { |
11788 | statement(ts: builtin_to_glsl(builtin: BuiltInTessLevelOuter, storage: StorageClassOutput), ts: "[" , ts&: i, ts: "] = " , |
11789 | ts: "half(" , ts: to_expression(id: c->subconstants[i]), ts: ");" ); |
11790 | } |
11791 | }); |
11792 | } |
11793 | |
11794 | if (inner_factor_initializer_id && (c = maybe_get<SPIRConstant>(id: inner_factor_initializer_id))) |
11795 | { |
11796 | auto &entry_func = get<SPIRFunction>(id: ir.default_entry_point); |
11797 | if (get_execution_mode_bitset().get(bit: ExecutionModeTriangles)) |
11798 | { |
11799 | entry_func.fixup_hooks_in.push_back(t: [=]() { |
11800 | statement(ts: builtin_to_glsl(builtin: BuiltInTessLevelInner, storage: StorageClassOutput), ts: " = " , ts: "half(" , |
11801 | ts: to_expression(id: c->subconstants[0]), ts: ");" ); |
11802 | }); |
11803 | } |
11804 | else |
11805 | { |
11806 | entry_func.fixup_hooks_in.push_back(t: [=]() { |
11807 | for (uint32_t i = 0; i < 2; i++) |
11808 | { |
11809 | statement(ts: builtin_to_glsl(builtin: BuiltInTessLevelInner, storage: StorageClassOutput), ts: "[" , ts&: i, ts: "] = " , |
11810 | ts: "half(" , ts: to_expression(id: c->subconstants[i]), ts: ");" ); |
11811 | } |
11812 | }); |
11813 | } |
11814 | } |
11815 | |
11816 | if (stage_in_var_id) |
11817 | { |
11818 | if (!ep_args.empty()) |
11819 | ep_args += ", " ; |
11820 | if (msl_options.multi_patch_workgroup) |
11821 | { |
11822 | ep_args += join(ts: "device " , ts: type_to_glsl(type: get_stage_in_struct_type()), ts: "* " , ts&: input_buffer_var_name, |
11823 | ts: " [[buffer(" , ts: convert_to_string(t: msl_options.shader_input_buffer_index), ts: ")]]" ); |
11824 | } |
11825 | else |
11826 | { |
11827 | ep_args += join(ts: "threadgroup " , ts: type_to_glsl(type: get_stage_in_struct_type()), ts: "* " , ts&: input_wg_var_name, |
11828 | ts: " [[threadgroup(" , ts: convert_to_string(t: msl_options.shader_input_wg_index), ts: ")]]" ); |
11829 | } |
11830 | } |
11831 | } |
11832 | } |
11833 | } |
11834 | |
11835 | string CompilerMSL::entry_point_args_argument_buffer(bool append_comma) |
11836 | { |
11837 | string ep_args = entry_point_arg_stage_in(); |
11838 | Bitset claimed_bindings; |
11839 | |
11840 | for (uint32_t i = 0; i < kMaxArgumentBuffers; i++) |
11841 | { |
11842 | uint32_t id = argument_buffer_ids[i]; |
11843 | if (id == 0) |
11844 | continue; |
11845 | |
11846 | add_resource_name(id); |
11847 | auto &var = get<SPIRVariable>(id); |
11848 | auto &type = get_variable_data_type(var); |
11849 | |
11850 | if (!ep_args.empty()) |
11851 | ep_args += ", " ; |
11852 | |
11853 | // Check if the argument buffer binding itself has been remapped. |
11854 | uint32_t buffer_binding; |
11855 | auto itr = resource_bindings.find(x: { .model: get_entry_point().model, .desc_set: i, .binding: kArgumentBufferBinding }); |
11856 | if (itr != end(cont&: resource_bindings)) |
11857 | { |
11858 | buffer_binding = itr->second.first.msl_buffer; |
11859 | itr->second.second = true; |
11860 | } |
11861 | else |
11862 | { |
11863 | // As a fallback, directly map desc set <-> binding. |
11864 | // If that was taken, take the next buffer binding. |
11865 | if (claimed_bindings.get(bit: i)) |
11866 | buffer_binding = next_metal_resource_index_buffer; |
11867 | else |
11868 | buffer_binding = i; |
11869 | } |
11870 | |
11871 | claimed_bindings.set(buffer_binding); |
11872 | |
11873 | ep_args += get_argument_address_space(argument: var) + " " + type_to_glsl(type) + "& " + to_restrict(id) + to_name(id); |
11874 | ep_args += " [[buffer(" + convert_to_string(t: buffer_binding) + ")]]" ; |
11875 | |
11876 | next_metal_resource_index_buffer = max(a: next_metal_resource_index_buffer, b: buffer_binding + 1); |
11877 | } |
11878 | |
11879 | entry_point_args_discrete_descriptors(args&: ep_args); |
11880 | entry_point_args_builtin(ep_args); |
11881 | |
11882 | if (!ep_args.empty() && append_comma) |
11883 | ep_args += ", " ; |
11884 | |
11885 | return ep_args; |
11886 | } |
11887 | |
11888 | const MSLConstexprSampler *CompilerMSL::find_constexpr_sampler(uint32_t id) const |
11889 | { |
11890 | // Try by ID. |
11891 | { |
11892 | auto itr = constexpr_samplers_by_id.find(x: id); |
11893 | if (itr != end(cont: constexpr_samplers_by_id)) |
11894 | return &itr->second; |
11895 | } |
11896 | |
11897 | // Try by binding. |
11898 | { |
11899 | uint32_t desc_set = get_decoration(id, decoration: DecorationDescriptorSet); |
11900 | uint32_t binding = get_decoration(id, decoration: DecorationBinding); |
11901 | |
11902 | auto itr = constexpr_samplers_by_binding.find(x: { .desc_set: desc_set, .binding: binding }); |
11903 | if (itr != end(cont: constexpr_samplers_by_binding)) |
11904 | return &itr->second; |
11905 | } |
11906 | |
11907 | return nullptr; |
11908 | } |
11909 | |
11910 | void CompilerMSL::entry_point_args_discrete_descriptors(string &ep_args) |
11911 | { |
11912 | // Output resources, sorted by resource index & type |
11913 | // We need to sort to work around a bug on macOS 10.13 with NVidia drivers where switching between shaders |
11914 | // with different order of buffers can result in issues with buffer assignments inside the driver. |
11915 | struct Resource |
11916 | { |
11917 | SPIRVariable *var; |
11918 | string name; |
11919 | SPIRType::BaseType basetype; |
11920 | uint32_t index; |
11921 | uint32_t plane; |
11922 | uint32_t secondary_index; |
11923 | }; |
11924 | |
11925 | SmallVector<Resource> resources; |
11926 | |
11927 | ir.for_each_typed_id<SPIRVariable>(op: [&](uint32_t var_id, SPIRVariable &var) { |
11928 | if ((var.storage == StorageClassUniform || var.storage == StorageClassUniformConstant || |
11929 | var.storage == StorageClassPushConstant || var.storage == StorageClassStorageBuffer) && |
11930 | !is_hidden_variable(var)) |
11931 | { |
11932 | auto &type = get_variable_data_type(var); |
11933 | |
11934 | if (is_supported_argument_buffer_type(type) && var.storage != StorageClassPushConstant) |
11935 | { |
11936 | uint32_t desc_set = get_decoration(id: var_id, decoration: DecorationDescriptorSet); |
11937 | if (descriptor_set_is_argument_buffer(desc_set)) |
11938 | return; |
11939 | } |
11940 | |
11941 | const MSLConstexprSampler *constexpr_sampler = nullptr; |
11942 | if (type.basetype == SPIRType::SampledImage || type.basetype == SPIRType::Sampler) |
11943 | { |
11944 | constexpr_sampler = find_constexpr_sampler(id: var_id); |
11945 | if (constexpr_sampler) |
11946 | { |
11947 | // Mark this ID as a constexpr sampler for later in case it came from set/bindings. |
11948 | constexpr_samplers_by_id[var_id] = *constexpr_sampler; |
11949 | } |
11950 | } |
11951 | |
11952 | // Emulate texture2D atomic operations |
11953 | uint32_t secondary_index = 0; |
11954 | if (atomic_image_vars.count(x: var.self)) |
11955 | { |
11956 | secondary_index = get_metal_resource_index(var, basetype: SPIRType::AtomicCounter, plane: 0); |
11957 | } |
11958 | |
11959 | if (type.basetype == SPIRType::SampledImage) |
11960 | { |
11961 | add_resource_name(id: var_id); |
11962 | |
11963 | uint32_t plane_count = 1; |
11964 | if (constexpr_sampler && constexpr_sampler->ycbcr_conversion_enable) |
11965 | plane_count = constexpr_sampler->planes; |
11966 | |
11967 | for (uint32_t i = 0; i < plane_count; i++) |
11968 | resources.push_back(t: { .var: &var, .name: to_name(id: var_id), .basetype: SPIRType::Image, |
11969 | .index: get_metal_resource_index(var, basetype: SPIRType::Image, plane: i), .plane: i, .secondary_index: secondary_index }); |
11970 | |
11971 | if (type.image.dim != DimBuffer && !constexpr_sampler) |
11972 | { |
11973 | resources.push_back(t: { .var: &var, .name: to_sampler_expression(id: var_id), .basetype: SPIRType::Sampler, |
11974 | .index: get_metal_resource_index(var, basetype: SPIRType::Sampler), .plane: 0, .secondary_index: 0 }); |
11975 | } |
11976 | } |
11977 | else if (!constexpr_sampler) |
11978 | { |
11979 | // constexpr samplers are not declared as resources. |
11980 | add_resource_name(id: var_id); |
11981 | resources.push_back(t: { .var: &var, .name: to_name(id: var_id), .basetype: type.basetype, |
11982 | .index: get_metal_resource_index(var, basetype: type.basetype), .plane: 0, .secondary_index: secondary_index }); |
11983 | } |
11984 | } |
11985 | }); |
11986 | |
11987 | sort(first: resources.begin(), last: resources.end(), comp: [](const Resource &lhs, const Resource &rhs) { |
11988 | return tie(args: lhs.basetype, args: lhs.index) < tie(args: rhs.basetype, args: rhs.index); |
11989 | }); |
11990 | |
11991 | for (auto &r : resources) |
11992 | { |
11993 | auto &var = *r.var; |
11994 | auto &type = get_variable_data_type(var); |
11995 | |
11996 | uint32_t var_id = var.self; |
11997 | |
11998 | switch (r.basetype) |
11999 | { |
12000 | case SPIRType::Struct: |
12001 | { |
12002 | auto &m = ir.meta[type.self]; |
12003 | if (m.members.size() == 0) |
12004 | break; |
12005 | if (!type.array.empty()) |
12006 | { |
12007 | if (type.array.size() > 1) |
12008 | SPIRV_CROSS_THROW("Arrays of arrays of buffers are not supported." ); |
12009 | |
12010 | // Metal doesn't directly support this, so we must expand the |
12011 | // array. We'll declare a local array to hold these elements |
12012 | // later. |
12013 | uint32_t array_size = to_array_size_literal(type); |
12014 | |
12015 | if (array_size == 0) |
12016 | SPIRV_CROSS_THROW("Unsized arrays of buffers are not supported in MSL." ); |
12017 | |
12018 | // Allow Metal to use the array<T> template to make arrays a value type |
12019 | is_using_builtin_array = true; |
12020 | buffer_arrays.push_back(t: var_id); |
12021 | for (uint32_t i = 0; i < array_size; ++i) |
12022 | { |
12023 | if (!ep_args.empty()) |
12024 | ep_args += ", " ; |
12025 | ep_args += get_argument_address_space(argument: var) + " " + type_to_glsl(type) + "* " + to_restrict(id: var_id) + |
12026 | r.name + "_" + convert_to_string(t: i); |
12027 | ep_args += " [[buffer(" + convert_to_string(t: r.index + i) + ")" ; |
12028 | if (interlocked_resources.count(x: var_id)) |
12029 | ep_args += ", raster_order_group(0)" ; |
12030 | ep_args += "]]" ; |
12031 | } |
12032 | is_using_builtin_array = false; |
12033 | } |
12034 | else |
12035 | { |
12036 | if (!ep_args.empty()) |
12037 | ep_args += ", " ; |
12038 | ep_args += |
12039 | get_argument_address_space(argument: var) + " " + type_to_glsl(type) + "& " + to_restrict(id: var_id) + r.name; |
12040 | ep_args += " [[buffer(" + convert_to_string(t: r.index) + ")" ; |
12041 | if (interlocked_resources.count(x: var_id)) |
12042 | ep_args += ", raster_order_group(0)" ; |
12043 | ep_args += "]]" ; |
12044 | } |
12045 | break; |
12046 | } |
12047 | case SPIRType::Sampler: |
12048 | if (!ep_args.empty()) |
12049 | ep_args += ", " ; |
12050 | ep_args += sampler_type(type, id: var_id) + " " + r.name; |
12051 | ep_args += " [[sampler(" + convert_to_string(t: r.index) + ")]]" ; |
12052 | break; |
12053 | case SPIRType::Image: |
12054 | { |
12055 | if (!ep_args.empty()) |
12056 | ep_args += ", " ; |
12057 | |
12058 | // Use Metal's native frame-buffer fetch API for subpass inputs. |
12059 | const auto &basetype = get<SPIRType>(id: var.basetype); |
12060 | if (!type_is_msl_framebuffer_fetch(type: basetype)) |
12061 | { |
12062 | ep_args += image_type_glsl(type, id: var_id) + " " + r.name; |
12063 | if (r.plane > 0) |
12064 | ep_args += join(ts&: plane_name_suffix, ts&: r.plane); |
12065 | ep_args += " [[texture(" + convert_to_string(t: r.index) + ")" ; |
12066 | if (interlocked_resources.count(x: var_id)) |
12067 | ep_args += ", raster_order_group(0)" ; |
12068 | ep_args += "]]" ; |
12069 | } |
12070 | else |
12071 | { |
12072 | if (msl_options.is_macos() && !msl_options.supports_msl_version(major: 2, minor: 3)) |
12073 | SPIRV_CROSS_THROW("Framebuffer fetch on Mac is not supported before MSL 2.3." ); |
12074 | ep_args += image_type_glsl(type, id: var_id) + " " + r.name; |
12075 | ep_args += " [[color(" + convert_to_string(t: r.index) + ")]]" ; |
12076 | } |
12077 | |
12078 | // Emulate texture2D atomic operations |
12079 | if (atomic_image_vars.count(x: var.self)) |
12080 | { |
12081 | ep_args += ", device atomic_" + type_to_glsl(type: get<SPIRType>(id: basetype.image.type), id: 0); |
12082 | ep_args += "* " + r.name + "_atomic" ; |
12083 | ep_args += " [[buffer(" + convert_to_string(t: r.secondary_index) + ")" ; |
12084 | if (interlocked_resources.count(x: var_id)) |
12085 | ep_args += ", raster_order_group(0)" ; |
12086 | ep_args += "]]" ; |
12087 | } |
12088 | break; |
12089 | } |
12090 | case SPIRType::AccelerationStructure: |
12091 | ep_args += ", " + type_to_glsl(type, id: var_id) + " " + r.name; |
12092 | ep_args += " [[buffer(" + convert_to_string(t: r.index) + ")]]" ; |
12093 | break; |
12094 | default: |
12095 | if (!ep_args.empty()) |
12096 | ep_args += ", " ; |
12097 | if (!type.pointer) |
12098 | ep_args += get_type_address_space(type: get<SPIRType>(id: var.basetype), id: var_id) + " " + |
12099 | type_to_glsl(type, id: var_id) + "& " + r.name; |
12100 | else |
12101 | ep_args += type_to_glsl(type, id: var_id) + " " + r.name; |
12102 | ep_args += " [[buffer(" + convert_to_string(t: r.index) + ")" ; |
12103 | if (interlocked_resources.count(x: var_id)) |
12104 | ep_args += ", raster_order_group(0)" ; |
12105 | ep_args += "]]" ; |
12106 | break; |
12107 | } |
12108 | } |
12109 | } |
12110 | |
12111 | // Returns a string containing a comma-delimited list of args for the entry point function |
12112 | // This is the "classic" method of MSL 1 when we don't have argument buffer support. |
12113 | string CompilerMSL::entry_point_args_classic(bool append_comma) |
12114 | { |
12115 | string ep_args = entry_point_arg_stage_in(); |
12116 | entry_point_args_discrete_descriptors(ep_args); |
12117 | entry_point_args_builtin(ep_args); |
12118 | |
12119 | if (!ep_args.empty() && append_comma) |
12120 | ep_args += ", " ; |
12121 | |
12122 | return ep_args; |
12123 | } |
12124 | |
12125 | void CompilerMSL::fix_up_shader_inputs_outputs() |
12126 | { |
12127 | auto &entry_func = this->get<SPIRFunction>(id: ir.default_entry_point); |
12128 | |
12129 | // Emit a guard to ensure we don't execute beyond the last vertex. |
12130 | // Vertex shaders shouldn't have the problems with barriers in non-uniform control flow that |
12131 | // tessellation control shaders do, so early returns should be OK. We may need to revisit this |
12132 | // if it ever becomes possible to use barriers from a vertex shader. |
12133 | if (get_execution_model() == ExecutionModelVertex && msl_options.vertex_for_tessellation) |
12134 | { |
12135 | entry_func.fixup_hooks_in.push_back(t: [this]() { |
12136 | statement(ts: "if (any(" , ts: to_expression(id: builtin_invocation_id_id), |
12137 | ts: " >= " , ts: to_expression(id: builtin_stage_input_size_id), ts: "))" ); |
12138 | statement(ts: " return;" ); |
12139 | }); |
12140 | } |
12141 | |
12142 | // Look for sampled images and buffer. Add hooks to set up the swizzle constants or array lengths. |
12143 | ir.for_each_typed_id<SPIRVariable>(op: [&](uint32_t, SPIRVariable &var) { |
12144 | auto &type = get_variable_data_type(var); |
12145 | uint32_t var_id = var.self; |
12146 | bool ssbo = has_decoration(id: type.self, decoration: DecorationBufferBlock); |
12147 | |
12148 | if (var.storage == StorageClassUniformConstant && !is_hidden_variable(var)) |
12149 | { |
12150 | if (msl_options.swizzle_texture_samples && has_sampled_images && is_sampled_image_type(type)) |
12151 | { |
12152 | entry_func.fixup_hooks_in.push_back(t: [this, &type, &var, var_id]() { |
12153 | bool is_array_type = !type.array.empty(); |
12154 | |
12155 | uint32_t desc_set = get_decoration(id: var_id, decoration: DecorationDescriptorSet); |
12156 | if (descriptor_set_is_argument_buffer(desc_set)) |
12157 | { |
12158 | statement(ts: "constant uint" , ts: is_array_type ? "* " : "& " , ts: to_swizzle_expression(id: var_id), |
12159 | ts: is_array_type ? " = &" : " = " , ts: to_name(id: argument_buffer_ids[desc_set]), |
12160 | ts: ".spvSwizzleConstants" , ts: "[" , |
12161 | ts: convert_to_string(t: get_metal_resource_index(var, basetype: SPIRType::Image)), ts: "];" ); |
12162 | } |
12163 | else |
12164 | { |
12165 | // If we have an array of images, we need to be able to index into it, so take a pointer instead. |
12166 | statement(ts: "constant uint" , ts: is_array_type ? "* " : "& " , ts: to_swizzle_expression(id: var_id), |
12167 | ts: is_array_type ? " = &" : " = " , ts: to_name(id: swizzle_buffer_id), ts: "[" , |
12168 | ts: convert_to_string(t: get_metal_resource_index(var, basetype: SPIRType::Image)), ts: "];" ); |
12169 | } |
12170 | }); |
12171 | } |
12172 | } |
12173 | else if ((var.storage == StorageClassStorageBuffer || (var.storage == StorageClassUniform && ssbo)) && |
12174 | !is_hidden_variable(var)) |
12175 | { |
12176 | if (buffers_requiring_array_length.count(x: var.self)) |
12177 | { |
12178 | entry_func.fixup_hooks_in.push_back(t: [this, &type, &var, var_id]() { |
12179 | bool is_array_type = !type.array.empty(); |
12180 | |
12181 | uint32_t desc_set = get_decoration(id: var_id, decoration: DecorationDescriptorSet); |
12182 | if (descriptor_set_is_argument_buffer(desc_set)) |
12183 | { |
12184 | statement(ts: "constant uint" , ts: is_array_type ? "* " : "& " , ts: to_buffer_size_expression(id: var_id), |
12185 | ts: is_array_type ? " = &" : " = " , ts: to_name(id: argument_buffer_ids[desc_set]), |
12186 | ts: ".spvBufferSizeConstants" , ts: "[" , |
12187 | ts: convert_to_string(t: get_metal_resource_index(var, basetype: SPIRType::Image)), ts: "];" ); |
12188 | } |
12189 | else |
12190 | { |
12191 | // If we have an array of images, we need to be able to index into it, so take a pointer instead. |
12192 | statement(ts: "constant uint" , ts: is_array_type ? "* " : "& " , ts: to_buffer_size_expression(id: var_id), |
12193 | ts: is_array_type ? " = &" : " = " , ts: to_name(id: buffer_size_buffer_id), ts: "[" , |
12194 | ts: convert_to_string(t: get_metal_resource_index(var, basetype: type.basetype)), ts: "];" ); |
12195 | } |
12196 | }); |
12197 | } |
12198 | } |
12199 | }); |
12200 | |
12201 | // Builtin variables |
12202 | ir.for_each_typed_id<SPIRVariable>(op: [this, &entry_func](uint32_t, SPIRVariable &var) { |
12203 | uint32_t var_id = var.self; |
12204 | BuiltIn bi_type = ir.meta[var_id].decoration.builtin_type; |
12205 | |
12206 | if (var.storage != StorageClassInput && var.storage != StorageClassOutput) |
12207 | return; |
12208 | if (!interface_variable_exists_in_entry_point(id: var.self)) |
12209 | return; |
12210 | |
12211 | if (var.storage == StorageClassInput && is_builtin_variable(var) && active_input_builtins.get(bit: bi_type)) |
12212 | { |
12213 | switch (bi_type) |
12214 | { |
12215 | case BuiltInSamplePosition: |
12216 | entry_func.fixup_hooks_in.push_back(t: [=]() { |
12217 | statement(ts: builtin_type_decl(builtin: bi_type), ts: " " , ts: to_expression(id: var_id), ts: " = get_sample_position(" , |
12218 | ts: to_expression(id: builtin_sample_id_id), ts: ");" ); |
12219 | }); |
12220 | break; |
12221 | case BuiltInFragCoord: |
12222 | if (is_sample_rate()) |
12223 | { |
12224 | entry_func.fixup_hooks_in.push_back(t: [=]() { |
12225 | statement(ts: to_expression(id: var_id), ts: ".xy += get_sample_position(" , |
12226 | ts: to_expression(id: builtin_sample_id_id), ts: ") - 0.5;" ); |
12227 | }); |
12228 | } |
12229 | break; |
12230 | case BuiltInInvocationId: |
12231 | // This is direct-mapped without multi-patch workgroups. |
12232 | if (get_execution_model() != ExecutionModelTessellationControl || !msl_options.multi_patch_workgroup) |
12233 | break; |
12234 | |
12235 | entry_func.fixup_hooks_in.push_back(t: [=]() { |
12236 | statement(ts: builtin_type_decl(builtin: bi_type), ts: " " , ts: to_expression(id: var_id), ts: " = " , |
12237 | ts: to_expression(id: builtin_invocation_id_id), ts: ".x % " , ts&: this->get_entry_point().output_vertices, |
12238 | ts: ";" ); |
12239 | }); |
12240 | break; |
12241 | case BuiltInPrimitiveId: |
12242 | // This is natively supported by fragment and tessellation evaluation shaders. |
12243 | // In tessellation control shaders, this is direct-mapped without multi-patch workgroups. |
12244 | if (get_execution_model() != ExecutionModelTessellationControl || !msl_options.multi_patch_workgroup) |
12245 | break; |
12246 | |
12247 | entry_func.fixup_hooks_in.push_back(t: [=]() { |
12248 | statement(ts: builtin_type_decl(builtin: bi_type), ts: " " , ts: to_expression(id: var_id), ts: " = min(" , |
12249 | ts: to_expression(id: builtin_invocation_id_id), ts: ".x / " , ts&: this->get_entry_point().output_vertices, |
12250 | ts: ", spvIndirectParams[1] - 1);" ); |
12251 | }); |
12252 | break; |
12253 | case BuiltInPatchVertices: |
12254 | if (get_execution_model() == ExecutionModelTessellationEvaluation) |
12255 | entry_func.fixup_hooks_in.push_back(t: [=]() { |
12256 | statement(ts: builtin_type_decl(builtin: bi_type), ts: " " , ts: to_expression(id: var_id), ts: " = " , |
12257 | ts: to_expression(id: patch_stage_in_var_id), ts: ".gl_in.size();" ); |
12258 | }); |
12259 | else |
12260 | entry_func.fixup_hooks_in.push_back(t: [=]() { |
12261 | statement(ts: builtin_type_decl(builtin: bi_type), ts: " " , ts: to_expression(id: var_id), ts: " = spvIndirectParams[0];" ); |
12262 | }); |
12263 | break; |
12264 | case BuiltInTessCoord: |
12265 | if (get_entry_point().flags.get(bit: ExecutionModeQuads)) |
12266 | { |
12267 | // The entry point will only have a float2 TessCoord variable. |
12268 | // Pad to float3. |
12269 | entry_func.fixup_hooks_in.push_back(t: [=]() { |
12270 | auto name = builtin_to_glsl(builtin: BuiltInTessCoord, storage: StorageClassInput); |
12271 | statement(ts: "float3 " + name + " = float3(" + name + "In.x, " + name + "In.y, 0.0);" ); |
12272 | }); |
12273 | } |
12274 | |
12275 | // Emit a fixup to account for the shifted domain. Don't do this for triangles; |
12276 | // MoltenVK will just reverse the winding order instead. |
12277 | if (msl_options.tess_domain_origin_lower_left && !get_entry_point().flags.get(bit: ExecutionModeTriangles)) |
12278 | { |
12279 | string tc = to_expression(id: var_id); |
12280 | entry_func.fixup_hooks_in.push_back(t: [=]() { statement(ts: tc, ts: ".y = 1.0 - " , ts: tc, ts: ".y;" ); }); |
12281 | } |
12282 | break; |
12283 | case BuiltInSubgroupId: |
12284 | if (!msl_options.emulate_subgroups) |
12285 | break; |
12286 | // For subgroup emulation, this is the same as the local invocation index. |
12287 | entry_func.fixup_hooks_in.push_back(t: [=]() { |
12288 | statement(ts: builtin_type_decl(builtin: bi_type), ts: " " , ts: to_expression(id: var_id), ts: " = " , |
12289 | ts: to_expression(id: builtin_local_invocation_index_id), ts: ";" ); |
12290 | }); |
12291 | break; |
12292 | case BuiltInNumSubgroups: |
12293 | if (!msl_options.emulate_subgroups) |
12294 | break; |
12295 | // For subgroup emulation, this is the same as the workgroup size. |
12296 | entry_func.fixup_hooks_in.push_back(t: [=]() { |
12297 | auto &type = expression_type(id: builtin_workgroup_size_id); |
12298 | string size_expr = to_expression(id: builtin_workgroup_size_id); |
12299 | if (type.vecsize >= 3) |
12300 | size_expr = join(ts&: size_expr, ts: ".x * " , ts&: size_expr, ts: ".y * " , ts&: size_expr, ts: ".z" ); |
12301 | else if (type.vecsize == 2) |
12302 | size_expr = join(ts&: size_expr, ts: ".x * " , ts&: size_expr, ts: ".y" ); |
12303 | statement(ts: builtin_type_decl(builtin: bi_type), ts: " " , ts: to_expression(id: var_id), ts: " = " , ts&: size_expr, ts: ";" ); |
12304 | }); |
12305 | break; |
12306 | case BuiltInSubgroupLocalInvocationId: |
12307 | if (!msl_options.emulate_subgroups) |
12308 | break; |
12309 | // For subgroup emulation, assume subgroups of size 1. |
12310 | entry_func.fixup_hooks_in.push_back( |
12311 | t: [=]() { statement(ts: builtin_type_decl(builtin: bi_type), ts: " " , ts: to_expression(id: var_id), ts: " = 0;" ); }); |
12312 | break; |
12313 | case BuiltInSubgroupSize: |
12314 | if (msl_options.emulate_subgroups) |
12315 | { |
12316 | // For subgroup emulation, assume subgroups of size 1. |
12317 | entry_func.fixup_hooks_in.push_back( |
12318 | t: [=]() { statement(ts: builtin_type_decl(builtin: bi_type), ts: " " , ts: to_expression(id: var_id), ts: " = 1;" ); }); |
12319 | } |
12320 | else if (msl_options.fixed_subgroup_size != 0) |
12321 | { |
12322 | entry_func.fixup_hooks_in.push_back(t: [=]() { |
12323 | statement(ts: builtin_type_decl(builtin: bi_type), ts: " " , ts: to_expression(id: var_id), ts: " = " , |
12324 | ts&: msl_options.fixed_subgroup_size, ts: ";" ); |
12325 | }); |
12326 | } |
12327 | break; |
12328 | case BuiltInSubgroupEqMask: |
12329 | if (msl_options.is_ios() && !msl_options.supports_msl_version(major: 2, minor: 2)) |
12330 | SPIRV_CROSS_THROW("Subgroup ballot functionality requires Metal 2.2 on iOS." ); |
12331 | if (!msl_options.supports_msl_version(major: 2, minor: 1)) |
12332 | SPIRV_CROSS_THROW("Subgroup ballot functionality requires Metal 2.1." ); |
12333 | entry_func.fixup_hooks_in.push_back(t: [=]() { |
12334 | if (msl_options.is_ios()) |
12335 | { |
12336 | statement(ts: builtin_type_decl(builtin: bi_type), ts: " " , ts: to_expression(id: var_id), ts: " = " , ts: "uint4(1 << " , |
12337 | ts: to_expression(id: builtin_subgroup_invocation_id_id), ts: ", uint3(0));" ); |
12338 | } |
12339 | else |
12340 | { |
12341 | statement(ts: builtin_type_decl(builtin: bi_type), ts: " " , ts: to_expression(id: var_id), ts: " = " , |
12342 | ts: to_expression(id: builtin_subgroup_invocation_id_id), ts: " >= 32 ? uint4(0, (1 << (" , |
12343 | ts: to_expression(id: builtin_subgroup_invocation_id_id), ts: " - 32)), uint2(0)) : uint4(1 << " , |
12344 | ts: to_expression(id: builtin_subgroup_invocation_id_id), ts: ", uint3(0));" ); |
12345 | } |
12346 | }); |
12347 | break; |
12348 | case BuiltInSubgroupGeMask: |
12349 | if (msl_options.is_ios() && !msl_options.supports_msl_version(major: 2, minor: 2)) |
12350 | SPIRV_CROSS_THROW("Subgroup ballot functionality requires Metal 2.2 on iOS." ); |
12351 | if (!msl_options.supports_msl_version(major: 2, minor: 1)) |
12352 | SPIRV_CROSS_THROW("Subgroup ballot functionality requires Metal 2.1." ); |
12353 | if (msl_options.fixed_subgroup_size != 0) |
12354 | add_spv_func_and_recompile(spv_func: SPVFuncImplSubgroupBallot); |
12355 | entry_func.fixup_hooks_in.push_back(t: [=]() { |
12356 | // Case where index < 32, size < 32: |
12357 | // mask0 = bfi(0, 0xFFFFFFFF, index, size - index); |
12358 | // mask1 = bfi(0, 0xFFFFFFFF, 0, 0); // Gives 0 |
12359 | // Case where index < 32 but size >= 32: |
12360 | // mask0 = bfi(0, 0xFFFFFFFF, index, 32 - index); |
12361 | // mask1 = bfi(0, 0xFFFFFFFF, 0, size - 32); |
12362 | // Case where index >= 32: |
12363 | // mask0 = bfi(0, 0xFFFFFFFF, 32, 0); // Gives 0 |
12364 | // mask1 = bfi(0, 0xFFFFFFFF, index - 32, size - index); |
12365 | // This is expressed without branches to avoid divergent |
12366 | // control flow--hence the complicated min/max expressions. |
12367 | // This is further complicated by the fact that if you attempt |
12368 | // to bfi/bfe out-of-bounds on Metal, undefined behavior is the |
12369 | // result. |
12370 | if (msl_options.fixed_subgroup_size > 32) |
12371 | { |
12372 | // Don't use the subgroup size variable with fixed subgroup sizes, |
12373 | // since the variables could be defined in the wrong order. |
12374 | statement(ts: builtin_type_decl(builtin: bi_type), ts: " " , ts: to_expression(id: var_id), |
12375 | ts: " = uint4(insert_bits(0u, 0xFFFFFFFF, min(" , |
12376 | ts: to_expression(id: builtin_subgroup_invocation_id_id), ts: ", 32u), (uint)max(32 - (int)" , |
12377 | ts: to_expression(id: builtin_subgroup_invocation_id_id), |
12378 | ts: ", 0)), insert_bits(0u, 0xFFFFFFFF," |
12379 | " (uint)max((int)" , |
12380 | ts: to_expression(id: builtin_subgroup_invocation_id_id), ts: " - 32, 0), " , |
12381 | ts&: msl_options.fixed_subgroup_size, ts: " - max(" , |
12382 | ts: to_expression(id: builtin_subgroup_invocation_id_id), |
12383 | ts: ", 32u)), uint2(0));" ); |
12384 | } |
12385 | else if (msl_options.fixed_subgroup_size != 0) |
12386 | { |
12387 | statement(ts: builtin_type_decl(builtin: bi_type), ts: " " , ts: to_expression(id: var_id), |
12388 | ts: " = uint4(insert_bits(0u, 0xFFFFFFFF, " , |
12389 | ts: to_expression(id: builtin_subgroup_invocation_id_id), ts: ", " , |
12390 | ts&: msl_options.fixed_subgroup_size, ts: " - " , |
12391 | ts: to_expression(id: builtin_subgroup_invocation_id_id), |
12392 | ts: "), uint3(0));" ); |
12393 | } |
12394 | else if (msl_options.is_ios()) |
12395 | { |
12396 | // On iOS, the SIMD-group size will currently never exceed 32. |
12397 | statement(ts: builtin_type_decl(builtin: bi_type), ts: " " , ts: to_expression(id: var_id), |
12398 | ts: " = uint4(insert_bits(0u, 0xFFFFFFFF, " , |
12399 | ts: to_expression(id: builtin_subgroup_invocation_id_id), ts: ", " , |
12400 | ts: to_expression(id: builtin_subgroup_size_id), ts: " - " , |
12401 | ts: to_expression(id: builtin_subgroup_invocation_id_id), ts: "), uint3(0));" ); |
12402 | } |
12403 | else |
12404 | { |
12405 | statement(ts: builtin_type_decl(builtin: bi_type), ts: " " , ts: to_expression(id: var_id), |
12406 | ts: " = uint4(insert_bits(0u, 0xFFFFFFFF, min(" , |
12407 | ts: to_expression(id: builtin_subgroup_invocation_id_id), ts: ", 32u), (uint)max(min((int)" , |
12408 | ts: to_expression(id: builtin_subgroup_size_id), ts: ", 32) - (int)" , |
12409 | ts: to_expression(id: builtin_subgroup_invocation_id_id), |
12410 | ts: ", 0)), insert_bits(0u, 0xFFFFFFFF, (uint)max((int)" , |
12411 | ts: to_expression(id: builtin_subgroup_invocation_id_id), ts: " - 32, 0), (uint)max((int)" , |
12412 | ts: to_expression(id: builtin_subgroup_size_id), ts: " - (int)max(" , |
12413 | ts: to_expression(id: builtin_subgroup_invocation_id_id), ts: ", 32u), 0)), uint2(0));" ); |
12414 | } |
12415 | }); |
12416 | break; |
12417 | case BuiltInSubgroupGtMask: |
12418 | if (msl_options.is_ios() && !msl_options.supports_msl_version(major: 2, minor: 2)) |
12419 | SPIRV_CROSS_THROW("Subgroup ballot functionality requires Metal 2.2 on iOS." ); |
12420 | if (!msl_options.supports_msl_version(major: 2, minor: 1)) |
12421 | SPIRV_CROSS_THROW("Subgroup ballot functionality requires Metal 2.1." ); |
12422 | add_spv_func_and_recompile(spv_func: SPVFuncImplSubgroupBallot); |
12423 | entry_func.fixup_hooks_in.push_back(t: [=]() { |
12424 | // The same logic applies here, except now the index is one |
12425 | // more than the subgroup invocation ID. |
12426 | if (msl_options.fixed_subgroup_size > 32) |
12427 | { |
12428 | statement(ts: builtin_type_decl(builtin: bi_type), ts: " " , ts: to_expression(id: var_id), |
12429 | ts: " = uint4(insert_bits(0u, 0xFFFFFFFF, min(" , |
12430 | ts: to_expression(id: builtin_subgroup_invocation_id_id), ts: " + 1, 32u), (uint)max(32 - (int)" , |
12431 | ts: to_expression(id: builtin_subgroup_invocation_id_id), |
12432 | ts: " - 1, 0)), insert_bits(0u, 0xFFFFFFFF, (uint)max((int)" , |
12433 | ts: to_expression(id: builtin_subgroup_invocation_id_id), ts: " + 1 - 32, 0), " , |
12434 | ts&: msl_options.fixed_subgroup_size, ts: " - max(" , |
12435 | ts: to_expression(id: builtin_subgroup_invocation_id_id), |
12436 | ts: " + 1, 32u)), uint2(0));" ); |
12437 | } |
12438 | else if (msl_options.fixed_subgroup_size != 0) |
12439 | { |
12440 | statement(ts: builtin_type_decl(builtin: bi_type), ts: " " , ts: to_expression(id: var_id), |
12441 | ts: " = uint4(insert_bits(0u, 0xFFFFFFFF, " , |
12442 | ts: to_expression(id: builtin_subgroup_invocation_id_id), ts: " + 1, " , |
12443 | ts&: msl_options.fixed_subgroup_size, ts: " - " , |
12444 | ts: to_expression(id: builtin_subgroup_invocation_id_id), |
12445 | ts: " - 1), uint3(0));" ); |
12446 | } |
12447 | else if (msl_options.is_ios()) |
12448 | { |
12449 | statement(ts: builtin_type_decl(builtin: bi_type), ts: " " , ts: to_expression(id: var_id), |
12450 | ts: " = uint4(insert_bits(0u, 0xFFFFFFFF, " , |
12451 | ts: to_expression(id: builtin_subgroup_invocation_id_id), ts: " + 1, " , |
12452 | ts: to_expression(id: builtin_subgroup_size_id), ts: " - " , |
12453 | ts: to_expression(id: builtin_subgroup_invocation_id_id), ts: " - 1), uint3(0));" ); |
12454 | } |
12455 | else |
12456 | { |
12457 | statement(ts: builtin_type_decl(builtin: bi_type), ts: " " , ts: to_expression(id: var_id), |
12458 | ts: " = uint4(insert_bits(0u, 0xFFFFFFFF, min(" , |
12459 | ts: to_expression(id: builtin_subgroup_invocation_id_id), ts: " + 1, 32u), (uint)max(min((int)" , |
12460 | ts: to_expression(id: builtin_subgroup_size_id), ts: ", 32) - (int)" , |
12461 | ts: to_expression(id: builtin_subgroup_invocation_id_id), |
12462 | ts: " - 1, 0)), insert_bits(0u, 0xFFFFFFFF, (uint)max((int)" , |
12463 | ts: to_expression(id: builtin_subgroup_invocation_id_id), ts: " + 1 - 32, 0), (uint)max((int)" , |
12464 | ts: to_expression(id: builtin_subgroup_size_id), ts: " - (int)max(" , |
12465 | ts: to_expression(id: builtin_subgroup_invocation_id_id), ts: " + 1, 32u), 0)), uint2(0));" ); |
12466 | } |
12467 | }); |
12468 | break; |
12469 | case BuiltInSubgroupLeMask: |
12470 | if (msl_options.is_ios() && !msl_options.supports_msl_version(major: 2, minor: 2)) |
12471 | SPIRV_CROSS_THROW("Subgroup ballot functionality requires Metal 2.2 on iOS." ); |
12472 | if (!msl_options.supports_msl_version(major: 2, minor: 1)) |
12473 | SPIRV_CROSS_THROW("Subgroup ballot functionality requires Metal 2.1." ); |
12474 | add_spv_func_and_recompile(spv_func: SPVFuncImplSubgroupBallot); |
12475 | entry_func.fixup_hooks_in.push_back(t: [=]() { |
12476 | if (msl_options.is_ios()) |
12477 | { |
12478 | statement(ts: builtin_type_decl(builtin: bi_type), ts: " " , ts: to_expression(id: var_id), |
12479 | ts: " = uint4(extract_bits(0xFFFFFFFF, 0, " , |
12480 | ts: to_expression(id: builtin_subgroup_invocation_id_id), ts: " + 1), uint3(0));" ); |
12481 | } |
12482 | else |
12483 | { |
12484 | statement(ts: builtin_type_decl(builtin: bi_type), ts: " " , ts: to_expression(id: var_id), |
12485 | ts: " = uint4(extract_bits(0xFFFFFFFF, 0, min(" , |
12486 | ts: to_expression(id: builtin_subgroup_invocation_id_id), |
12487 | ts: " + 1, 32u)), extract_bits(0xFFFFFFFF, 0, (uint)max((int)" , |
12488 | ts: to_expression(id: builtin_subgroup_invocation_id_id), ts: " + 1 - 32, 0)), uint2(0));" ); |
12489 | } |
12490 | }); |
12491 | break; |
12492 | case BuiltInSubgroupLtMask: |
12493 | if (msl_options.is_ios() && !msl_options.supports_msl_version(major: 2, minor: 2)) |
12494 | SPIRV_CROSS_THROW("Subgroup ballot functionality requires Metal 2.2 on iOS." ); |
12495 | if (!msl_options.supports_msl_version(major: 2, minor: 1)) |
12496 | SPIRV_CROSS_THROW("Subgroup ballot functionality requires Metal 2.1." ); |
12497 | add_spv_func_and_recompile(spv_func: SPVFuncImplSubgroupBallot); |
12498 | entry_func.fixup_hooks_in.push_back(t: [=]() { |
12499 | if (msl_options.is_ios()) |
12500 | { |
12501 | statement(ts: builtin_type_decl(builtin: bi_type), ts: " " , ts: to_expression(id: var_id), |
12502 | ts: " = uint4(extract_bits(0xFFFFFFFF, 0, " , |
12503 | ts: to_expression(id: builtin_subgroup_invocation_id_id), ts: "), uint3(0));" ); |
12504 | } |
12505 | else |
12506 | { |
12507 | statement(ts: builtin_type_decl(builtin: bi_type), ts: " " , ts: to_expression(id: var_id), |
12508 | ts: " = uint4(extract_bits(0xFFFFFFFF, 0, min(" , |
12509 | ts: to_expression(id: builtin_subgroup_invocation_id_id), |
12510 | ts: ", 32u)), extract_bits(0xFFFFFFFF, 0, (uint)max((int)" , |
12511 | ts: to_expression(id: builtin_subgroup_invocation_id_id), ts: " - 32, 0)), uint2(0));" ); |
12512 | } |
12513 | }); |
12514 | break; |
12515 | case BuiltInViewIndex: |
12516 | if (!msl_options.multiview) |
12517 | { |
12518 | // According to the Vulkan spec, when not running under a multiview |
12519 | // render pass, ViewIndex is 0. |
12520 | entry_func.fixup_hooks_in.push_back(t: [=]() { |
12521 | statement(ts: "const " , ts: builtin_type_decl(builtin: bi_type), ts: " " , ts: to_expression(id: var_id), ts: " = 0;" ); |
12522 | }); |
12523 | } |
12524 | else if (msl_options.view_index_from_device_index) |
12525 | { |
12526 | // In this case, we take the view index from that of the device we're running on. |
12527 | entry_func.fixup_hooks_in.push_back(t: [=]() { |
12528 | statement(ts: "const " , ts: builtin_type_decl(builtin: bi_type), ts: " " , ts: to_expression(id: var_id), ts: " = " , |
12529 | ts&: msl_options.device_index, ts: ";" ); |
12530 | }); |
12531 | // We actually don't want to set the render_target_array_index here. |
12532 | // Since every physical device is rendering a different view, |
12533 | // there's no need for layered rendering here. |
12534 | } |
12535 | else if (!msl_options.multiview_layered_rendering) |
12536 | { |
12537 | // In this case, the views are rendered one at a time. The view index, then, |
12538 | // is just the first part of the "view mask". |
12539 | entry_func.fixup_hooks_in.push_back(t: [=]() { |
12540 | statement(ts: "const " , ts: builtin_type_decl(builtin: bi_type), ts: " " , ts: to_expression(id: var_id), ts: " = " , |
12541 | ts: to_expression(id: view_mask_buffer_id), ts: "[0];" ); |
12542 | }); |
12543 | } |
12544 | else if (get_execution_model() == ExecutionModelFragment) |
12545 | { |
12546 | // Because we adjusted the view index in the vertex shader, we have to |
12547 | // adjust it back here. |
12548 | entry_func.fixup_hooks_in.push_back(t: [=]() { |
12549 | statement(ts: to_expression(id: var_id), ts: " += " , ts: to_expression(id: view_mask_buffer_id), ts: "[0];" ); |
12550 | }); |
12551 | } |
12552 | else if (get_execution_model() == ExecutionModelVertex) |
12553 | { |
12554 | // Metal provides no special support for multiview, so we smuggle |
12555 | // the view index in the instance index. |
12556 | entry_func.fixup_hooks_in.push_back(t: [=]() { |
12557 | statement(ts: builtin_type_decl(builtin: bi_type), ts: " " , ts: to_expression(id: var_id), ts: " = " , |
12558 | ts: to_expression(id: view_mask_buffer_id), ts: "[0] + (" , ts: to_expression(id: builtin_instance_idx_id), |
12559 | ts: " - " , ts: to_expression(id: builtin_base_instance_id), ts: ") % " , |
12560 | ts: to_expression(id: view_mask_buffer_id), ts: "[1];" ); |
12561 | statement(ts: to_expression(id: builtin_instance_idx_id), ts: " = (" , |
12562 | ts: to_expression(id: builtin_instance_idx_id), ts: " - " , |
12563 | ts: to_expression(id: builtin_base_instance_id), ts: ") / " , ts: to_expression(id: view_mask_buffer_id), |
12564 | ts: "[1] + " , ts: to_expression(id: builtin_base_instance_id), ts: ";" ); |
12565 | }); |
12566 | // In addition to setting the variable itself, we also need to |
12567 | // set the render_target_array_index with it on output. We have to |
12568 | // offset this by the base view index, because Metal isn't in on |
12569 | // our little game here. |
12570 | entry_func.fixup_hooks_out.push_back(t: [=]() { |
12571 | statement(ts: to_expression(id: builtin_layer_id), ts: " = " , ts: to_expression(id: var_id), ts: " - " , |
12572 | ts: to_expression(id: view_mask_buffer_id), ts: "[0];" ); |
12573 | }); |
12574 | } |
12575 | break; |
12576 | case BuiltInDeviceIndex: |
12577 | // Metal pipelines belong to the devices which create them, so we'll |
12578 | // need to create a MTLPipelineState for every MTLDevice in a grouped |
12579 | // VkDevice. We can assume, then, that the device index is constant. |
12580 | entry_func.fixup_hooks_in.push_back(t: [=]() { |
12581 | statement(ts: "const " , ts: builtin_type_decl(builtin: bi_type), ts: " " , ts: to_expression(id: var_id), ts: " = " , |
12582 | ts&: msl_options.device_index, ts: ";" ); |
12583 | }); |
12584 | break; |
12585 | case BuiltInWorkgroupId: |
12586 | if (!msl_options.dispatch_base || !active_input_builtins.get(bit: BuiltInWorkgroupId)) |
12587 | break; |
12588 | |
12589 | // The vkCmdDispatchBase() command lets the client set the base value |
12590 | // of WorkgroupId. Metal has no direct equivalent; we must make this |
12591 | // adjustment ourselves. |
12592 | entry_func.fixup_hooks_in.push_back(t: [=]() { |
12593 | statement(ts: to_expression(id: var_id), ts: " += " , ts: to_dereferenced_expression(id: builtin_dispatch_base_id), ts: ";" ); |
12594 | }); |
12595 | break; |
12596 | case BuiltInGlobalInvocationId: |
12597 | if (!msl_options.dispatch_base || !active_input_builtins.get(bit: BuiltInGlobalInvocationId)) |
12598 | break; |
12599 | |
12600 | // GlobalInvocationId is defined as LocalInvocationId + WorkgroupId * WorkgroupSize. |
12601 | // This needs to be adjusted too. |
12602 | entry_func.fixup_hooks_in.push_back(t: [=]() { |
12603 | auto &execution = this->get_entry_point(); |
12604 | uint32_t workgroup_size_id = execution.workgroup_size.constant; |
12605 | if (workgroup_size_id) |
12606 | statement(ts: to_expression(id: var_id), ts: " += " , ts: to_dereferenced_expression(id: builtin_dispatch_base_id), |
12607 | ts: " * " , ts: to_expression(id: workgroup_size_id), ts: ";" ); |
12608 | else |
12609 | statement(ts: to_expression(id: var_id), ts: " += " , ts: to_dereferenced_expression(id: builtin_dispatch_base_id), |
12610 | ts: " * uint3(" , ts&: execution.workgroup_size.x, ts: ", " , ts&: execution.workgroup_size.y, ts: ", " , |
12611 | ts&: execution.workgroup_size.z, ts: ");" ); |
12612 | }); |
12613 | break; |
12614 | case BuiltInVertexId: |
12615 | case BuiltInVertexIndex: |
12616 | // This is direct-mapped normally. |
12617 | if (!msl_options.vertex_for_tessellation) |
12618 | break; |
12619 | |
12620 | entry_func.fixup_hooks_in.push_back(t: [=]() { |
12621 | builtin_declaration = true; |
12622 | switch (msl_options.vertex_index_type) |
12623 | { |
12624 | case Options::IndexType::None: |
12625 | statement(ts: builtin_type_decl(builtin: bi_type), ts: " " , ts: to_expression(id: var_id), ts: " = " , |
12626 | ts: to_expression(id: builtin_invocation_id_id), ts: ".x + " , |
12627 | ts: to_expression(id: builtin_dispatch_base_id), ts: ".x;" ); |
12628 | break; |
12629 | case Options::IndexType::UInt16: |
12630 | case Options::IndexType::UInt32: |
12631 | statement(ts: builtin_type_decl(builtin: bi_type), ts: " " , ts: to_expression(id: var_id), ts: " = " , ts&: index_buffer_var_name, |
12632 | ts: "[" , ts: to_expression(id: builtin_invocation_id_id), ts: ".x] + " , |
12633 | ts: to_expression(id: builtin_dispatch_base_id), ts: ".x;" ); |
12634 | break; |
12635 | } |
12636 | builtin_declaration = false; |
12637 | }); |
12638 | break; |
12639 | case BuiltInBaseVertex: |
12640 | // This is direct-mapped normally. |
12641 | if (!msl_options.vertex_for_tessellation) |
12642 | break; |
12643 | |
12644 | entry_func.fixup_hooks_in.push_back(t: [=]() { |
12645 | statement(ts: builtin_type_decl(builtin: bi_type), ts: " " , ts: to_expression(id: var_id), ts: " = " , |
12646 | ts: to_expression(id: builtin_dispatch_base_id), ts: ".x;" ); |
12647 | }); |
12648 | break; |
12649 | case BuiltInInstanceId: |
12650 | case BuiltInInstanceIndex: |
12651 | // This is direct-mapped normally. |
12652 | if (!msl_options.vertex_for_tessellation) |
12653 | break; |
12654 | |
12655 | entry_func.fixup_hooks_in.push_back(t: [=]() { |
12656 | builtin_declaration = true; |
12657 | statement(ts: builtin_type_decl(builtin: bi_type), ts: " " , ts: to_expression(id: var_id), ts: " = " , |
12658 | ts: to_expression(id: builtin_invocation_id_id), ts: ".y + " , ts: to_expression(id: builtin_dispatch_base_id), |
12659 | ts: ".y;" ); |
12660 | builtin_declaration = false; |
12661 | }); |
12662 | break; |
12663 | case BuiltInBaseInstance: |
12664 | // This is direct-mapped normally. |
12665 | if (!msl_options.vertex_for_tessellation) |
12666 | break; |
12667 | |
12668 | entry_func.fixup_hooks_in.push_back(t: [=]() { |
12669 | statement(ts: builtin_type_decl(builtin: bi_type), ts: " " , ts: to_expression(id: var_id), ts: " = " , |
12670 | ts: to_expression(id: builtin_dispatch_base_id), ts: ".y;" ); |
12671 | }); |
12672 | break; |
12673 | default: |
12674 | break; |
12675 | } |
12676 | } |
12677 | else if (var.storage == StorageClassOutput && get_execution_model() == ExecutionModelFragment && |
12678 | is_builtin_variable(var) && active_output_builtins.get(bit: bi_type) && |
12679 | bi_type == BuiltInSampleMask && has_additional_fixed_sample_mask()) |
12680 | { |
12681 | // If the additional fixed sample mask was set, we need to adjust the sample_mask |
12682 | // output to reflect that. If the shader outputs the sample_mask itself too, we need |
12683 | // to AND the two masks to get the final one. |
12684 | string op_str = does_shader_write_sample_mask ? " &= " : " = " ; |
12685 | entry_func.fixup_hooks_out.push_back(t: [=]() { |
12686 | statement(ts: to_expression(id: builtin_sample_mask_id), ts: op_str, ts: additional_fixed_sample_mask_str(), ts: ";" ); |
12687 | }); |
12688 | } |
12689 | }); |
12690 | } |
12691 | |
12692 | // Returns the Metal index of the resource of the specified type as used by the specified variable. |
12693 | uint32_t CompilerMSL::get_metal_resource_index(SPIRVariable &var, SPIRType::BaseType basetype, uint32_t plane) |
12694 | { |
12695 | auto &execution = get_entry_point(); |
12696 | auto &var_dec = ir.meta[var.self].decoration; |
12697 | auto &var_type = get<SPIRType>(id: var.basetype); |
12698 | uint32_t var_desc_set = (var.storage == StorageClassPushConstant) ? kPushConstDescSet : var_dec.set; |
12699 | uint32_t var_binding = (var.storage == StorageClassPushConstant) ? kPushConstBinding : var_dec.binding; |
12700 | |
12701 | // If a matching binding has been specified, find and use it. |
12702 | auto itr = resource_bindings.find(x: { .model: execution.model, .desc_set: var_desc_set, .binding: var_binding }); |
12703 | |
12704 | // Atomic helper buffers for image atomics need to use secondary bindings as well. |
12705 | bool use_secondary_binding = (var_type.basetype == SPIRType::SampledImage && basetype == SPIRType::Sampler) || |
12706 | basetype == SPIRType::AtomicCounter; |
12707 | |
12708 | auto resource_decoration = |
12709 | use_secondary_binding ? SPIRVCrossDecorationResourceIndexSecondary : SPIRVCrossDecorationResourceIndexPrimary; |
12710 | |
12711 | if (plane == 1) |
12712 | resource_decoration = SPIRVCrossDecorationResourceIndexTertiary; |
12713 | if (plane == 2) |
12714 | resource_decoration = SPIRVCrossDecorationResourceIndexQuaternary; |
12715 | |
12716 | if (itr != end(cont&: resource_bindings)) |
12717 | { |
12718 | auto &remap = itr->second; |
12719 | remap.second = true; |
12720 | switch (basetype) |
12721 | { |
12722 | case SPIRType::Image: |
12723 | set_extended_decoration(id: var.self, decoration: resource_decoration, value: remap.first.msl_texture + plane); |
12724 | return remap.first.msl_texture + plane; |
12725 | case SPIRType::Sampler: |
12726 | set_extended_decoration(id: var.self, decoration: resource_decoration, value: remap.first.msl_sampler); |
12727 | return remap.first.msl_sampler; |
12728 | default: |
12729 | set_extended_decoration(id: var.self, decoration: resource_decoration, value: remap.first.msl_buffer); |
12730 | return remap.first.msl_buffer; |
12731 | } |
12732 | } |
12733 | |
12734 | // If we have already allocated an index, keep using it. |
12735 | if (has_extended_decoration(id: var.self, decoration: resource_decoration)) |
12736 | return get_extended_decoration(id: var.self, decoration: resource_decoration); |
12737 | |
12738 | auto &type = get<SPIRType>(id: var.basetype); |
12739 | |
12740 | if (type_is_msl_framebuffer_fetch(type)) |
12741 | { |
12742 | // Frame-buffer fetch gets its fallback resource index from the input attachment index, |
12743 | // which is then treated as color index. |
12744 | return get_decoration(id: var.self, decoration: DecorationInputAttachmentIndex); |
12745 | } |
12746 | else if (msl_options.enable_decoration_binding) |
12747 | { |
12748 | // Allow user to enable decoration binding. |
12749 | // If there is no explicit mapping of bindings to MSL, use the declared binding as a fallback. |
12750 | if (has_decoration(id: var.self, decoration: DecorationBinding)) |
12751 | { |
12752 | var_binding = get_decoration(id: var.self, decoration: DecorationBinding); |
12753 | // Avoid emitting sentinel bindings. |
12754 | if (var_binding < 0x80000000u) |
12755 | return var_binding; |
12756 | } |
12757 | } |
12758 | |
12759 | // If we did not explicitly remap, allocate bindings on demand. |
12760 | // We cannot reliably use Binding decorations since SPIR-V and MSL's binding models are very different. |
12761 | |
12762 | bool allocate_argument_buffer_ids = false; |
12763 | |
12764 | if (var.storage != StorageClassPushConstant) |
12765 | allocate_argument_buffer_ids = descriptor_set_is_argument_buffer(desc_set: var_desc_set); |
12766 | |
12767 | uint32_t binding_stride = 1; |
12768 | for (uint32_t i = 0; i < uint32_t(type.array.size()); i++) |
12769 | binding_stride *= to_array_size_literal(type, index: i); |
12770 | |
12771 | assert(binding_stride != 0); |
12772 | |
12773 | // If a binding has not been specified, revert to incrementing resource indices. |
12774 | uint32_t resource_index; |
12775 | |
12776 | if (allocate_argument_buffer_ids) |
12777 | { |
12778 | // Allocate from a flat ID binding space. |
12779 | resource_index = next_metal_resource_ids[var_desc_set]; |
12780 | next_metal_resource_ids[var_desc_set] += binding_stride; |
12781 | } |
12782 | else |
12783 | { |
12784 | // Allocate from plain bindings which are allocated per resource type. |
12785 | switch (basetype) |
12786 | { |
12787 | case SPIRType::Image: |
12788 | resource_index = next_metal_resource_index_texture; |
12789 | next_metal_resource_index_texture += binding_stride; |
12790 | break; |
12791 | case SPIRType::Sampler: |
12792 | resource_index = next_metal_resource_index_sampler; |
12793 | next_metal_resource_index_sampler += binding_stride; |
12794 | break; |
12795 | default: |
12796 | resource_index = next_metal_resource_index_buffer; |
12797 | next_metal_resource_index_buffer += binding_stride; |
12798 | break; |
12799 | } |
12800 | } |
12801 | |
12802 | set_extended_decoration(id: var.self, decoration: resource_decoration, value: resource_index); |
12803 | return resource_index; |
12804 | } |
12805 | |
12806 | bool CompilerMSL::type_is_msl_framebuffer_fetch(const SPIRType &type) const |
12807 | { |
12808 | return type.basetype == SPIRType::Image && type.image.dim == DimSubpassData && |
12809 | msl_options.use_framebuffer_fetch_subpasses; |
12810 | } |
12811 | |
12812 | bool CompilerMSL::type_is_pointer(const SPIRType &type) const |
12813 | { |
12814 | if (!type.pointer) |
12815 | return false; |
12816 | auto &parent_type = get<SPIRType>(id: type.parent_type); |
12817 | // Safeguards when we forget to set pointer_depth (there is an assert for it in type_to_glsl), |
12818 | // but the extra check shouldn't hurt. |
12819 | return (type.pointer_depth > parent_type.pointer_depth) || !parent_type.pointer; |
12820 | } |
12821 | |
12822 | bool CompilerMSL::type_is_pointer_to_pointer(const SPIRType &type) const |
12823 | { |
12824 | if (!type.pointer) |
12825 | return false; |
12826 | auto &parent_type = get<SPIRType>(id: type.parent_type); |
12827 | return type.pointer_depth > parent_type.pointer_depth && type_is_pointer(type: parent_type); |
12828 | } |
12829 | |
12830 | const char *CompilerMSL::descriptor_address_space(uint32_t id, StorageClass storage, const char *plain_address_space) const |
12831 | { |
12832 | if (msl_options.argument_buffers) |
12833 | { |
12834 | bool storage_class_is_descriptor = storage == StorageClassUniform || |
12835 | storage == StorageClassStorageBuffer || |
12836 | storage == StorageClassUniformConstant; |
12837 | |
12838 | uint32_t desc_set = get_decoration(id, decoration: DecorationDescriptorSet); |
12839 | if (storage_class_is_descriptor && descriptor_set_is_argument_buffer(desc_set)) |
12840 | { |
12841 | // An awkward case where we need to emit *more* address space declarations (yay!). |
12842 | // An example is where we pass down an array of buffer pointers to leaf functions. |
12843 | // It's a constant array containing pointers to constants. |
12844 | // The pointer array is always constant however. E.g. |
12845 | // device SSBO * constant (&array)[N]. |
12846 | // const device SSBO * constant (&array)[N]. |
12847 | // constant SSBO * constant (&array)[N]. |
12848 | // However, this only matters for argument buffers, since for MSL 1.0 style codegen, |
12849 | // we emit the buffer array on stack instead, and that seems to work just fine apparently. |
12850 | |
12851 | // If the argument was marked as being in device address space, any pointer to member would |
12852 | // be const device, not constant. |
12853 | if (argument_buffer_device_storage_mask & (1u << desc_set)) |
12854 | return "const device" ; |
12855 | else |
12856 | return "constant" ; |
12857 | } |
12858 | } |
12859 | |
12860 | return plain_address_space; |
12861 | } |
12862 | |
12863 | string CompilerMSL::argument_decl(const SPIRFunction::Parameter &arg) |
12864 | { |
12865 | auto &var = get<SPIRVariable>(id: arg.id); |
12866 | auto &type = get_variable_data_type(var); |
12867 | auto &var_type = get<SPIRType>(id: arg.type); |
12868 | StorageClass type_storage = var_type.storage; |
12869 | bool is_pointer = var_type.pointer; |
12870 | |
12871 | // If we need to modify the name of the variable, make sure we use the original variable. |
12872 | // Our alias is just a shadow variable. |
12873 | uint32_t name_id = var.self; |
12874 | if (arg.alias_global_variable && var.basevariable) |
12875 | name_id = var.basevariable; |
12876 | |
12877 | bool constref = !arg.alias_global_variable && is_pointer && arg.write_count == 0; |
12878 | // Framebuffer fetch is plain value, const looks out of place, but it is not wrong. |
12879 | if (type_is_msl_framebuffer_fetch(type)) |
12880 | constref = false; |
12881 | else if (type_storage == StorageClassUniformConstant) |
12882 | constref = true; |
12883 | |
12884 | bool type_is_image = type.basetype == SPIRType::Image || type.basetype == SPIRType::SampledImage || |
12885 | type.basetype == SPIRType::Sampler; |
12886 | |
12887 | // For opaque types we handle const later due to descriptor address spaces. |
12888 | const char *cv_qualifier = (constref && !type_is_image) ? "const " : "" ; |
12889 | string decl; |
12890 | |
12891 | // If this is a combined image-sampler for a 2D image with floating-point type, |
12892 | // we emitted the 'spvDynamicImageSampler' type, and this is *not* an alias parameter |
12893 | // for a global, then we need to emit a "dynamic" combined image-sampler. |
12894 | // Unfortunately, this is necessary to properly support passing around |
12895 | // combined image-samplers with Y'CbCr conversions on them. |
12896 | bool is_dynamic_img_sampler = !arg.alias_global_variable && type.basetype == SPIRType::SampledImage && |
12897 | type.image.dim == Dim2D && type_is_floating_point(type: get<SPIRType>(id: type.image.type)) && |
12898 | spv_function_implementations.count(x: SPVFuncImplDynamicImageSampler); |
12899 | |
12900 | // Allow Metal to use the array<T> template to make arrays a value type |
12901 | string address_space = get_argument_address_space(argument: var); |
12902 | bool builtin = has_decoration(id: var.self, decoration: DecorationBuiltIn); |
12903 | auto builtin_type = BuiltIn(get_decoration(id: arg.id, decoration: DecorationBuiltIn)); |
12904 | |
12905 | if (address_space == "threadgroup" ) |
12906 | is_using_builtin_array = true; |
12907 | |
12908 | if (var.basevariable && (var.basevariable == stage_in_ptr_var_id || var.basevariable == stage_out_ptr_var_id)) |
12909 | decl = join(ts&: cv_qualifier, ts: type_to_glsl(type, id: arg.id)); |
12910 | else if (builtin) |
12911 | { |
12912 | // Only use templated array for Clip/Cull distance when feasible. |
12913 | // In other scenarios, we need need to override array length for tess levels (if used as outputs), |
12914 | // or we need to emit the expected type for builtins (uint vs int). |
12915 | auto storage = get<SPIRType>(id: var.basetype).storage; |
12916 | |
12917 | if (storage == StorageClassInput && |
12918 | (builtin_type == BuiltInTessLevelInner || builtin_type == BuiltInTessLevelOuter)) |
12919 | { |
12920 | is_using_builtin_array = false; |
12921 | } |
12922 | else if (builtin_type != BuiltInClipDistance && builtin_type != BuiltInCullDistance) |
12923 | { |
12924 | is_using_builtin_array = true; |
12925 | } |
12926 | |
12927 | if (storage == StorageClassOutput && variable_storage_requires_stage_io(storage) && |
12928 | !is_stage_output_builtin_masked(builtin: builtin_type)) |
12929 | is_using_builtin_array = true; |
12930 | |
12931 | if (is_using_builtin_array) |
12932 | decl = join(ts&: cv_qualifier, ts: builtin_type_decl(builtin: builtin_type, id: arg.id)); |
12933 | else |
12934 | decl = join(ts&: cv_qualifier, ts: type_to_glsl(type, id: arg.id)); |
12935 | } |
12936 | else if ((type_storage == StorageClassUniform || type_storage == StorageClassStorageBuffer) && is_array(type)) |
12937 | { |
12938 | is_using_builtin_array = true; |
12939 | decl += join(ts&: cv_qualifier, ts: type_to_glsl(type, id: arg.id), ts: "*" ); |
12940 | } |
12941 | else if (is_dynamic_img_sampler) |
12942 | { |
12943 | decl = join(ts&: cv_qualifier, ts: "spvDynamicImageSampler<" , ts: type_to_glsl(type: get<SPIRType>(id: type.image.type)), ts: ">" ); |
12944 | // Mark the variable so that we can handle passing it to another function. |
12945 | set_extended_decoration(id: arg.id, decoration: SPIRVCrossDecorationDynamicImageSampler); |
12946 | } |
12947 | else |
12948 | { |
12949 | // The type is a pointer type we need to emit cv_qualifier late. |
12950 | if (type_is_pointer(type)) |
12951 | { |
12952 | decl = type_to_glsl(type, id: arg.id); |
12953 | if (*cv_qualifier != '\0') |
12954 | decl += join(ts: " " , ts&: cv_qualifier); |
12955 | } |
12956 | else |
12957 | decl = join(ts&: cv_qualifier, ts: type_to_glsl(type, id: arg.id)); |
12958 | } |
12959 | |
12960 | if (!builtin && !is_pointer && |
12961 | (type_storage == StorageClassFunction || type_storage == StorageClassGeneric)) |
12962 | { |
12963 | // If the argument is a pure value and not an opaque type, we will pass by value. |
12964 | if (msl_options.force_native_arrays && is_array(type)) |
12965 | { |
12966 | // We are receiving an array by value. This is problematic. |
12967 | // We cannot be sure of the target address space since we are supposed to receive a copy, |
12968 | // but this is not possible with MSL without some extra work. |
12969 | // We will have to assume we're getting a reference in thread address space. |
12970 | // If we happen to get a reference in constant address space, the caller must emit a copy and pass that. |
12971 | // Thread const therefore becomes the only logical choice, since we cannot "create" a constant array from |
12972 | // non-constant arrays, but we can create thread const from constant. |
12973 | decl = string("thread const " ) + decl; |
12974 | decl += " (&" ; |
12975 | const char *restrict_kw = to_restrict(id: name_id); |
12976 | if (*restrict_kw) |
12977 | { |
12978 | decl += " " ; |
12979 | decl += restrict_kw; |
12980 | } |
12981 | decl += to_expression(id: name_id); |
12982 | decl += ")" ; |
12983 | decl += type_to_array_glsl(type); |
12984 | } |
12985 | else |
12986 | { |
12987 | if (!address_space.empty()) |
12988 | decl = join(ts&: address_space, ts: " " , ts&: decl); |
12989 | decl += " " ; |
12990 | decl += to_expression(id: name_id); |
12991 | } |
12992 | } |
12993 | else if (is_array(type) && !type_is_image) |
12994 | { |
12995 | // Arrays of opaque types are special cased. |
12996 | if (!address_space.empty()) |
12997 | decl = join(ts&: address_space, ts: " " , ts&: decl); |
12998 | |
12999 | const char *argument_buffer_space = descriptor_address_space(id: name_id, storage: type_storage, plain_address_space: nullptr); |
13000 | if (argument_buffer_space) |
13001 | { |
13002 | decl += " " ; |
13003 | decl += argument_buffer_space; |
13004 | } |
13005 | |
13006 | // Special case, need to override the array size here if we're using tess level as an argument. |
13007 | if (get_execution_model() == ExecutionModelTessellationControl && builtin && |
13008 | (builtin_type == BuiltInTessLevelInner || builtin_type == BuiltInTessLevelOuter)) |
13009 | { |
13010 | uint32_t array_size = get_physical_tess_level_array_size(builtin: builtin_type); |
13011 | if (array_size == 1) |
13012 | { |
13013 | decl += " &" ; |
13014 | decl += to_expression(id: name_id); |
13015 | } |
13016 | else |
13017 | { |
13018 | decl += " (&" ; |
13019 | decl += to_expression(id: name_id); |
13020 | decl += ")" ; |
13021 | decl += join(ts: "[" , ts&: array_size, ts: "]" ); |
13022 | } |
13023 | } |
13024 | else |
13025 | { |
13026 | auto array_size_decl = type_to_array_glsl(type); |
13027 | if (array_size_decl.empty()) |
13028 | decl += "& " ; |
13029 | else |
13030 | decl += " (&" ; |
13031 | |
13032 | const char *restrict_kw = to_restrict(id: name_id); |
13033 | if (*restrict_kw) |
13034 | { |
13035 | decl += " " ; |
13036 | decl += restrict_kw; |
13037 | } |
13038 | decl += to_expression(id: name_id); |
13039 | |
13040 | if (!array_size_decl.empty()) |
13041 | { |
13042 | decl += ")" ; |
13043 | decl += array_size_decl; |
13044 | } |
13045 | } |
13046 | } |
13047 | else if (!type_is_image && (!pull_model_inputs.count(x: var.basevariable) || type.basetype == SPIRType::Struct)) |
13048 | { |
13049 | // If this is going to be a reference to a variable pointer, the address space |
13050 | // for the reference has to go before the '&', but after the '*'. |
13051 | if (!address_space.empty()) |
13052 | { |
13053 | if (type_is_pointer(type)) |
13054 | { |
13055 | if (*cv_qualifier == '\0') |
13056 | decl += ' '; |
13057 | decl += join(ts&: address_space, ts: " " ); |
13058 | } |
13059 | else |
13060 | decl = join(ts&: address_space, ts: " " , ts&: decl); |
13061 | } |
13062 | decl += "&" ; |
13063 | decl += " " ; |
13064 | decl += to_restrict(id: name_id); |
13065 | decl += to_expression(id: name_id); |
13066 | } |
13067 | else if (type_is_image) |
13068 | { |
13069 | if (type.array.empty()) |
13070 | { |
13071 | // For non-arrayed types we can just pass opaque descriptors by value. |
13072 | // This fixes problems if descriptors are passed by value from argument buffers and plain descriptors |
13073 | // in same shader. |
13074 | // There is no address space we can actually use, but value will work. |
13075 | // This will break if applications attempt to pass down descriptor arrays as arguments, but |
13076 | // fortunately that is extremely unlikely ... |
13077 | decl += " " ; |
13078 | decl += to_expression(id: name_id); |
13079 | } |
13080 | else |
13081 | { |
13082 | const char *img_address_space = descriptor_address_space(id: name_id, storage: type_storage, plain_address_space: "thread const" ); |
13083 | decl = join(ts&: img_address_space, ts: " " , ts&: decl); |
13084 | decl += "& " ; |
13085 | decl += to_expression(id: name_id); |
13086 | } |
13087 | } |
13088 | else |
13089 | { |
13090 | if (!address_space.empty()) |
13091 | decl = join(ts&: address_space, ts: " " , ts&: decl); |
13092 | decl += " " ; |
13093 | decl += to_expression(id: name_id); |
13094 | } |
13095 | |
13096 | // Emulate texture2D atomic operations |
13097 | auto *backing_var = maybe_get_backing_variable(chain: name_id); |
13098 | if (backing_var && atomic_image_vars.count(x: backing_var->self)) |
13099 | { |
13100 | decl += ", device atomic_" + type_to_glsl(type: get<SPIRType>(id: var_type.image.type), id: 0); |
13101 | decl += "* " + to_expression(id: name_id) + "_atomic" ; |
13102 | } |
13103 | |
13104 | is_using_builtin_array = false; |
13105 | |
13106 | return decl; |
13107 | } |
13108 | |
13109 | // If we're currently in the entry point function, and the object |
13110 | // has a qualified name, use it, otherwise use the standard name. |
13111 | string CompilerMSL::to_name(uint32_t id, bool allow_alias) const |
13112 | { |
13113 | if (current_function && (current_function->self == ir.default_entry_point)) |
13114 | { |
13115 | auto *m = ir.find_meta(id); |
13116 | if (m && !m->decoration.qualified_alias.empty()) |
13117 | return m->decoration.qualified_alias; |
13118 | } |
13119 | return Compiler::to_name(id, allow_alias); |
13120 | } |
13121 | |
13122 | // Appends the name of the member to the variable qualifier string, except for Builtins. |
13123 | string CompilerMSL::append_member_name(const string &qualifier, const SPIRType &type, uint32_t index) |
13124 | { |
13125 | // Don't qualify Builtin names because they are unique and are treated as such when building expressions |
13126 | BuiltIn builtin = BuiltInMax; |
13127 | if (is_member_builtin(type, index, builtin: &builtin)) |
13128 | return builtin_to_glsl(builtin, storage: type.storage); |
13129 | |
13130 | // Strip any underscore prefix from member name |
13131 | string mbr_name = to_member_name(type, index); |
13132 | size_t startPos = mbr_name.find_first_not_of(s: "_" ); |
13133 | mbr_name = (startPos != string::npos) ? mbr_name.substr(pos: startPos) : "" ; |
13134 | return join(ts: qualifier, ts: "_" , ts&: mbr_name); |
13135 | } |
13136 | |
13137 | // Ensures that the specified name is permanently usable by prepending a prefix |
13138 | // if the first chars are _ and a digit, which indicate a transient name. |
13139 | string CompilerMSL::ensure_valid_name(string name, string pfx) |
13140 | { |
13141 | return (name.size() >= 2 && name[0] == '_' && isdigit(name[1])) ? (pfx + name) : name; |
13142 | } |
13143 | |
13144 | const std::unordered_set<std::string> &CompilerMSL::get_reserved_keyword_set() |
13145 | { |
13146 | static const unordered_set<string> keywords = { |
13147 | "kernel" , |
13148 | "vertex" , |
13149 | "fragment" , |
13150 | "compute" , |
13151 | "bias" , |
13152 | "level" , |
13153 | "gradient2d" , |
13154 | "gradientcube" , |
13155 | "gradient3d" , |
13156 | "min_lod_clamp" , |
13157 | "assert" , |
13158 | "VARIABLE_TRACEPOINT" , |
13159 | "STATIC_DATA_TRACEPOINT" , |
13160 | "STATIC_DATA_TRACEPOINT_V" , |
13161 | "METAL_ALIGN" , |
13162 | "METAL_ASM" , |
13163 | "METAL_CONST" , |
13164 | "METAL_DEPRECATED" , |
13165 | "METAL_ENABLE_IF" , |
13166 | "METAL_FUNC" , |
13167 | "METAL_INTERNAL" , |
13168 | "METAL_NON_NULL_RETURN" , |
13169 | "METAL_NORETURN" , |
13170 | "METAL_NOTHROW" , |
13171 | "METAL_PURE" , |
13172 | "METAL_UNAVAILABLE" , |
13173 | "METAL_IMPLICIT" , |
13174 | "METAL_EXPLICIT" , |
13175 | "METAL_CONST_ARG" , |
13176 | "METAL_ARG_UNIFORM" , |
13177 | "METAL_ZERO_ARG" , |
13178 | "METAL_VALID_LOD_ARG" , |
13179 | "METAL_VALID_LEVEL_ARG" , |
13180 | "METAL_VALID_STORE_ORDER" , |
13181 | "METAL_VALID_LOAD_ORDER" , |
13182 | "METAL_VALID_COMPARE_EXCHANGE_FAILURE_ORDER" , |
13183 | "METAL_COMPATIBLE_COMPARE_EXCHANGE_ORDERS" , |
13184 | "METAL_VALID_RENDER_TARGET" , |
13185 | "is_function_constant_defined" , |
13186 | "CHAR_BIT" , |
13187 | "SCHAR_MAX" , |
13188 | "SCHAR_MIN" , |
13189 | "UCHAR_MAX" , |
13190 | "CHAR_MAX" , |
13191 | "CHAR_MIN" , |
13192 | "USHRT_MAX" , |
13193 | "SHRT_MAX" , |
13194 | "SHRT_MIN" , |
13195 | "UINT_MAX" , |
13196 | "INT_MAX" , |
13197 | "INT_MIN" , |
13198 | "FLT_DIG" , |
13199 | "FLT_MANT_DIG" , |
13200 | "FLT_MAX_10_EXP" , |
13201 | "FLT_MAX_EXP" , |
13202 | "FLT_MIN_10_EXP" , |
13203 | "FLT_MIN_EXP" , |
13204 | "FLT_RADIX" , |
13205 | "FLT_MAX" , |
13206 | "FLT_MIN" , |
13207 | "FLT_EPSILON" , |
13208 | "FP_ILOGB0" , |
13209 | "FP_ILOGBNAN" , |
13210 | "MAXFLOAT" , |
13211 | "HUGE_VALF" , |
13212 | "INFINITY" , |
13213 | "NAN" , |
13214 | "M_E_F" , |
13215 | "M_LOG2E_F" , |
13216 | "M_LOG10E_F" , |
13217 | "M_LN2_F" , |
13218 | "M_LN10_F" , |
13219 | "M_PI_F" , |
13220 | "M_PI_2_F" , |
13221 | "M_PI_4_F" , |
13222 | "M_1_PI_F" , |
13223 | "M_2_PI_F" , |
13224 | "M_2_SQRTPI_F" , |
13225 | "M_SQRT2_F" , |
13226 | "M_SQRT1_2_F" , |
13227 | "HALF_DIG" , |
13228 | "HALF_MANT_DIG" , |
13229 | "HALF_MAX_10_EXP" , |
13230 | "HALF_MAX_EXP" , |
13231 | "HALF_MIN_10_EXP" , |
13232 | "HALF_MIN_EXP" , |
13233 | "HALF_RADIX" , |
13234 | "HALF_MAX" , |
13235 | "HALF_MIN" , |
13236 | "HALF_EPSILON" , |
13237 | "MAXHALF" , |
13238 | "HUGE_VALH" , |
13239 | "M_E_H" , |
13240 | "M_LOG2E_H" , |
13241 | "M_LOG10E_H" , |
13242 | "M_LN2_H" , |
13243 | "M_LN10_H" , |
13244 | "M_PI_H" , |
13245 | "M_PI_2_H" , |
13246 | "M_PI_4_H" , |
13247 | "M_1_PI_H" , |
13248 | "M_2_PI_H" , |
13249 | "M_2_SQRTPI_H" , |
13250 | "M_SQRT2_H" , |
13251 | "M_SQRT1_2_H" , |
13252 | "DBL_DIG" , |
13253 | "DBL_MANT_DIG" , |
13254 | "DBL_MAX_10_EXP" , |
13255 | "DBL_MAX_EXP" , |
13256 | "DBL_MIN_10_EXP" , |
13257 | "DBL_MIN_EXP" , |
13258 | "DBL_RADIX" , |
13259 | "DBL_MAX" , |
13260 | "DBL_MIN" , |
13261 | "DBL_EPSILON" , |
13262 | "HUGE_VAL" , |
13263 | "M_E" , |
13264 | "M_LOG2E" , |
13265 | "M_LOG10E" , |
13266 | "M_LN2" , |
13267 | "M_LN10" , |
13268 | "M_PI" , |
13269 | "M_PI_2" , |
13270 | "M_PI_4" , |
13271 | "M_1_PI" , |
13272 | "M_2_PI" , |
13273 | "M_2_SQRTPI" , |
13274 | "M_SQRT2" , |
13275 | "M_SQRT1_2" , |
13276 | "quad_broadcast" , |
13277 | }; |
13278 | |
13279 | return keywords; |
13280 | } |
13281 | |
13282 | const std::unordered_set<std::string> &CompilerMSL::get_illegal_func_names() |
13283 | { |
13284 | static const unordered_set<string> illegal_func_names = { |
13285 | "main" , |
13286 | "saturate" , |
13287 | "assert" , |
13288 | "fmin3" , |
13289 | "fmax3" , |
13290 | "VARIABLE_TRACEPOINT" , |
13291 | "STATIC_DATA_TRACEPOINT" , |
13292 | "STATIC_DATA_TRACEPOINT_V" , |
13293 | "METAL_ALIGN" , |
13294 | "METAL_ASM" , |
13295 | "METAL_CONST" , |
13296 | "METAL_DEPRECATED" , |
13297 | "METAL_ENABLE_IF" , |
13298 | "METAL_FUNC" , |
13299 | "METAL_INTERNAL" , |
13300 | "METAL_NON_NULL_RETURN" , |
13301 | "METAL_NORETURN" , |
13302 | "METAL_NOTHROW" , |
13303 | "METAL_PURE" , |
13304 | "METAL_UNAVAILABLE" , |
13305 | "METAL_IMPLICIT" , |
13306 | "METAL_EXPLICIT" , |
13307 | "METAL_CONST_ARG" , |
13308 | "METAL_ARG_UNIFORM" , |
13309 | "METAL_ZERO_ARG" , |
13310 | "METAL_VALID_LOD_ARG" , |
13311 | "METAL_VALID_LEVEL_ARG" , |
13312 | "METAL_VALID_STORE_ORDER" , |
13313 | "METAL_VALID_LOAD_ORDER" , |
13314 | "METAL_VALID_COMPARE_EXCHANGE_FAILURE_ORDER" , |
13315 | "METAL_COMPATIBLE_COMPARE_EXCHANGE_ORDERS" , |
13316 | "METAL_VALID_RENDER_TARGET" , |
13317 | "is_function_constant_defined" , |
13318 | "CHAR_BIT" , |
13319 | "SCHAR_MAX" , |
13320 | "SCHAR_MIN" , |
13321 | "UCHAR_MAX" , |
13322 | "CHAR_MAX" , |
13323 | "CHAR_MIN" , |
13324 | "USHRT_MAX" , |
13325 | "SHRT_MAX" , |
13326 | "SHRT_MIN" , |
13327 | "UINT_MAX" , |
13328 | "INT_MAX" , |
13329 | "INT_MIN" , |
13330 | "FLT_DIG" , |
13331 | "FLT_MANT_DIG" , |
13332 | "FLT_MAX_10_EXP" , |
13333 | "FLT_MAX_EXP" , |
13334 | "FLT_MIN_10_EXP" , |
13335 | "FLT_MIN_EXP" , |
13336 | "FLT_RADIX" , |
13337 | "FLT_MAX" , |
13338 | "FLT_MIN" , |
13339 | "FLT_EPSILON" , |
13340 | "FP_ILOGB0" , |
13341 | "FP_ILOGBNAN" , |
13342 | "MAXFLOAT" , |
13343 | "HUGE_VALF" , |
13344 | "INFINITY" , |
13345 | "NAN" , |
13346 | "M_E_F" , |
13347 | "M_LOG2E_F" , |
13348 | "M_LOG10E_F" , |
13349 | "M_LN2_F" , |
13350 | "M_LN10_F" , |
13351 | "M_PI_F" , |
13352 | "M_PI_2_F" , |
13353 | "M_PI_4_F" , |
13354 | "M_1_PI_F" , |
13355 | "M_2_PI_F" , |
13356 | "M_2_SQRTPI_F" , |
13357 | "M_SQRT2_F" , |
13358 | "M_SQRT1_2_F" , |
13359 | "HALF_DIG" , |
13360 | "HALF_MANT_DIG" , |
13361 | "HALF_MAX_10_EXP" , |
13362 | "HALF_MAX_EXP" , |
13363 | "HALF_MIN_10_EXP" , |
13364 | "HALF_MIN_EXP" , |
13365 | "HALF_RADIX" , |
13366 | "HALF_MAX" , |
13367 | "HALF_MIN" , |
13368 | "HALF_EPSILON" , |
13369 | "MAXHALF" , |
13370 | "HUGE_VALH" , |
13371 | "M_E_H" , |
13372 | "M_LOG2E_H" , |
13373 | "M_LOG10E_H" , |
13374 | "M_LN2_H" , |
13375 | "M_LN10_H" , |
13376 | "M_PI_H" , |
13377 | "M_PI_2_H" , |
13378 | "M_PI_4_H" , |
13379 | "M_1_PI_H" , |
13380 | "M_2_PI_H" , |
13381 | "M_2_SQRTPI_H" , |
13382 | "M_SQRT2_H" , |
13383 | "M_SQRT1_2_H" , |
13384 | "DBL_DIG" , |
13385 | "DBL_MANT_DIG" , |
13386 | "DBL_MAX_10_EXP" , |
13387 | "DBL_MAX_EXP" , |
13388 | "DBL_MIN_10_EXP" , |
13389 | "DBL_MIN_EXP" , |
13390 | "DBL_RADIX" , |
13391 | "DBL_MAX" , |
13392 | "DBL_MIN" , |
13393 | "DBL_EPSILON" , |
13394 | "HUGE_VAL" , |
13395 | "M_E" , |
13396 | "M_LOG2E" , |
13397 | "M_LOG10E" , |
13398 | "M_LN2" , |
13399 | "M_LN10" , |
13400 | "M_PI" , |
13401 | "M_PI_2" , |
13402 | "M_PI_4" , |
13403 | "M_1_PI" , |
13404 | "M_2_PI" , |
13405 | "M_2_SQRTPI" , |
13406 | "M_SQRT2" , |
13407 | "M_SQRT1_2" , |
13408 | }; |
13409 | |
13410 | return illegal_func_names; |
13411 | } |
13412 | |
13413 | // Replace all names that match MSL keywords or Metal Standard Library functions. |
13414 | void CompilerMSL::replace_illegal_names() |
13415 | { |
13416 | // FIXME: MSL and GLSL are doing two different things here. |
13417 | // Agree on convention and remove this override. |
13418 | auto &keywords = get_reserved_keyword_set(); |
13419 | auto &illegal_func_names = get_illegal_func_names(); |
13420 | |
13421 | ir.for_each_typed_id<SPIRVariable>(op: [&](uint32_t self, SPIRVariable &) { |
13422 | auto *meta = ir.find_meta(id: self); |
13423 | if (!meta) |
13424 | return; |
13425 | |
13426 | auto &dec = meta->decoration; |
13427 | if (keywords.find(x: dec.alias) != end(cont: keywords)) |
13428 | dec.alias += "0" ; |
13429 | }); |
13430 | |
13431 | ir.for_each_typed_id<SPIRFunction>(op: [&](uint32_t self, SPIRFunction &) { |
13432 | auto *meta = ir.find_meta(id: self); |
13433 | if (!meta) |
13434 | return; |
13435 | |
13436 | auto &dec = meta->decoration; |
13437 | if (illegal_func_names.find(x: dec.alias) != end(cont: illegal_func_names)) |
13438 | dec.alias += "0" ; |
13439 | }); |
13440 | |
13441 | ir.for_each_typed_id<SPIRType>(op: [&](uint32_t self, SPIRType &) { |
13442 | auto *meta = ir.find_meta(id: self); |
13443 | if (!meta) |
13444 | return; |
13445 | |
13446 | for (auto &mbr_dec : meta->members) |
13447 | if (keywords.find(x: mbr_dec.alias) != end(cont: keywords)) |
13448 | mbr_dec.alias += "0" ; |
13449 | }); |
13450 | |
13451 | CompilerGLSL::replace_illegal_names(); |
13452 | } |
13453 | |
13454 | void CompilerMSL::replace_illegal_entry_point_names() |
13455 | { |
13456 | auto &illegal_func_names = get_illegal_func_names(); |
13457 | |
13458 | // It is important to this before we fixup identifiers, |
13459 | // since if ep_name is reserved, we will need to fix that up, |
13460 | // and then copy alias back into entry.name after the fixup. |
13461 | for (auto &entry : ir.entry_points) |
13462 | { |
13463 | // Change both the entry point name and the alias, to keep them synced. |
13464 | string &ep_name = entry.second.name; |
13465 | if (illegal_func_names.find(x: ep_name) != end(cont: illegal_func_names)) |
13466 | ep_name += "0" ; |
13467 | |
13468 | ir.meta[entry.first].decoration.alias = ep_name; |
13469 | } |
13470 | } |
13471 | |
13472 | void CompilerMSL::sync_entry_point_aliases_and_names() |
13473 | { |
13474 | for (auto &entry : ir.entry_points) |
13475 | entry.second.name = ir.meta[entry.first].decoration.alias; |
13476 | } |
13477 | |
13478 | string CompilerMSL::to_member_reference(uint32_t base, const SPIRType &type, uint32_t index, bool ptr_chain) |
13479 | { |
13480 | auto *var = maybe_get<SPIRVariable>(id: base); |
13481 | // If this is a buffer array, we have to dereference the buffer pointers. |
13482 | // Otherwise, if this is a pointer expression, dereference it. |
13483 | |
13484 | bool declared_as_pointer = false; |
13485 | |
13486 | if (var) |
13487 | { |
13488 | // Only allow -> dereference for block types. This is so we get expressions like |
13489 | // buffer[i]->first_member.second_member, rather than buffer[i]->first->second. |
13490 | bool is_block = has_decoration(id: type.self, decoration: DecorationBlock) || has_decoration(id: type.self, decoration: DecorationBufferBlock); |
13491 | |
13492 | bool is_buffer_variable = |
13493 | is_block && (var->storage == StorageClassUniform || var->storage == StorageClassStorageBuffer); |
13494 | declared_as_pointer = is_buffer_variable && is_array(type: get<SPIRType>(id: var->basetype)); |
13495 | } |
13496 | |
13497 | if (declared_as_pointer || (!ptr_chain && should_dereference(id: base))) |
13498 | return join(ts: "->" , ts: to_member_name(type, index)); |
13499 | else |
13500 | return join(ts: "." , ts: to_member_name(type, index)); |
13501 | } |
13502 | |
13503 | string CompilerMSL::to_qualifiers_glsl(uint32_t id) |
13504 | { |
13505 | string quals; |
13506 | |
13507 | auto *var = maybe_get<SPIRVariable>(id); |
13508 | auto &type = expression_type(id); |
13509 | |
13510 | if (type.storage == StorageClassWorkgroup || (var && variable_decl_is_remapped_storage(variable: *var, storage: StorageClassWorkgroup))) |
13511 | quals += "threadgroup " ; |
13512 | |
13513 | return quals; |
13514 | } |
13515 | |
13516 | // The optional id parameter indicates the object whose type we are trying |
13517 | // to find the description for. It is optional. Most type descriptions do not |
13518 | // depend on a specific object's use of that type. |
13519 | string CompilerMSL::type_to_glsl(const SPIRType &type, uint32_t id) |
13520 | { |
13521 | string type_name; |
13522 | |
13523 | // Pointer? |
13524 | if (type.pointer) |
13525 | { |
13526 | assert(type.pointer_depth > 0); |
13527 | |
13528 | const char *restrict_kw; |
13529 | |
13530 | auto type_address_space = get_type_address_space(type, id); |
13531 | auto type_decl = type_to_glsl(type: get<SPIRType>(id: type.parent_type), id); |
13532 | |
13533 | // Work around C pointer qualifier rules. If glsl_type is a pointer type as well |
13534 | // we'll need to emit the address space to the right. |
13535 | // We could always go this route, but it makes the code unnatural. |
13536 | // Prefer emitting thread T *foo over T thread* foo since it's more readable, |
13537 | // but we'll have to emit thread T * thread * T constant bar; for example. |
13538 | if (type_is_pointer_to_pointer(type)) |
13539 | type_name = join(ts&: type_decl, ts: " " , ts&: type_address_space, ts: " " ); |
13540 | else |
13541 | type_name = join(ts&: type_address_space, ts: " " , ts&: type_decl); |
13542 | |
13543 | switch (type.basetype) |
13544 | { |
13545 | case SPIRType::Image: |
13546 | case SPIRType::SampledImage: |
13547 | case SPIRType::Sampler: |
13548 | // These are handles. |
13549 | break; |
13550 | default: |
13551 | // Anything else can be a raw pointer. |
13552 | type_name += "*" ; |
13553 | restrict_kw = to_restrict(id); |
13554 | if (*restrict_kw) |
13555 | { |
13556 | type_name += " " ; |
13557 | type_name += restrict_kw; |
13558 | } |
13559 | break; |
13560 | } |
13561 | return type_name; |
13562 | } |
13563 | |
13564 | switch (type.basetype) |
13565 | { |
13566 | case SPIRType::Struct: |
13567 | // Need OpName lookup here to get a "sensible" name for a struct. |
13568 | // Allow Metal to use the array<T> template to make arrays a value type |
13569 | type_name = to_name(id: type.self); |
13570 | break; |
13571 | |
13572 | case SPIRType::Image: |
13573 | case SPIRType::SampledImage: |
13574 | return image_type_glsl(type, id); |
13575 | |
13576 | case SPIRType::Sampler: |
13577 | return sampler_type(type, id); |
13578 | |
13579 | case SPIRType::Void: |
13580 | return "void" ; |
13581 | |
13582 | case SPIRType::AtomicCounter: |
13583 | return "atomic_uint" ; |
13584 | |
13585 | case SPIRType::ControlPointArray: |
13586 | return join(ts: "patch_control_point<" , ts: type_to_glsl(type: get<SPIRType>(id: type.parent_type), id), ts: ">" ); |
13587 | |
13588 | case SPIRType::Interpolant: |
13589 | return join(ts: "interpolant<" , ts: type_to_glsl(type: get<SPIRType>(id: type.parent_type), id), ts: ", interpolation::" , |
13590 | ts: has_decoration(id: type.self, decoration: DecorationNoPerspective) ? "no_perspective" : "perspective" , ts: ">" ); |
13591 | |
13592 | // Scalars |
13593 | case SPIRType::Boolean: |
13594 | { |
13595 | auto *var = maybe_get_backing_variable(chain: id); |
13596 | if (var && var->basevariable) |
13597 | var = &get<SPIRVariable>(id: var->basevariable); |
13598 | |
13599 | // Need to special-case threadgroup booleans. They are supposed to be logical |
13600 | // storage, but MSL compilers will sometimes crash if you use threadgroup bool. |
13601 | // Workaround this by using 16-bit types instead and fixup on load-store to this data. |
13602 | // FIXME: We have no sane way of working around this problem if a struct member is boolean |
13603 | // and that struct is used as a threadgroup variable, but ... sigh. |
13604 | if ((var && var->storage == StorageClassWorkgroup) || type.storage == StorageClassWorkgroup) |
13605 | type_name = "short" ; |
13606 | else |
13607 | type_name = "bool" ; |
13608 | break; |
13609 | } |
13610 | |
13611 | case SPIRType::Char: |
13612 | case SPIRType::SByte: |
13613 | type_name = "char" ; |
13614 | break; |
13615 | case SPIRType::UByte: |
13616 | type_name = "uchar" ; |
13617 | break; |
13618 | case SPIRType::Short: |
13619 | type_name = "short" ; |
13620 | break; |
13621 | case SPIRType::UShort: |
13622 | type_name = "ushort" ; |
13623 | break; |
13624 | case SPIRType::Int: |
13625 | type_name = "int" ; |
13626 | break; |
13627 | case SPIRType::UInt: |
13628 | type_name = "uint" ; |
13629 | break; |
13630 | case SPIRType::Int64: |
13631 | if (!msl_options.supports_msl_version(major: 2, minor: 2)) |
13632 | SPIRV_CROSS_THROW("64-bit integers are only supported in MSL 2.2 and above." ); |
13633 | type_name = "long" ; |
13634 | break; |
13635 | case SPIRType::UInt64: |
13636 | if (!msl_options.supports_msl_version(major: 2, minor: 2)) |
13637 | SPIRV_CROSS_THROW("64-bit integers are only supported in MSL 2.2 and above." ); |
13638 | type_name = "ulong" ; |
13639 | break; |
13640 | case SPIRType::Half: |
13641 | type_name = "half" ; |
13642 | break; |
13643 | case SPIRType::Float: |
13644 | type_name = "float" ; |
13645 | break; |
13646 | case SPIRType::Double: |
13647 | type_name = "double" ; // Currently unsupported |
13648 | break; |
13649 | case SPIRType::AccelerationStructure: |
13650 | if (msl_options.supports_msl_version(major: 2, minor: 4)) |
13651 | type_name = "raytracing::acceleration_structure<raytracing::instancing>" ; |
13652 | else if (msl_options.supports_msl_version(major: 2, minor: 3)) |
13653 | type_name = "raytracing::instance_acceleration_structure" ; |
13654 | else |
13655 | SPIRV_CROSS_THROW("Acceleration Structure Type is supported in MSL 2.3 and above." ); |
13656 | break; |
13657 | case SPIRType::RayQuery: |
13658 | return "raytracing::intersection_query<raytracing::instancing, raytracing::triangle_data>" ; |
13659 | |
13660 | default: |
13661 | return "unknown_type" ; |
13662 | } |
13663 | |
13664 | // Matrix? |
13665 | if (type.columns > 1) |
13666 | type_name += to_string(val: type.columns) + "x" ; |
13667 | |
13668 | // Vector or Matrix? |
13669 | if (type.vecsize > 1) |
13670 | type_name += to_string(val: type.vecsize); |
13671 | |
13672 | if (type.array.empty() || using_builtin_array()) |
13673 | { |
13674 | return type_name; |
13675 | } |
13676 | else |
13677 | { |
13678 | // Allow Metal to use the array<T> template to make arrays a value type |
13679 | add_spv_func_and_recompile(spv_func: SPVFuncImplUnsafeArray); |
13680 | string res; |
13681 | string sizes; |
13682 | |
13683 | for (uint32_t i = 0; i < uint32_t(type.array.size()); i++) |
13684 | { |
13685 | res += "spvUnsafeArray<" ; |
13686 | sizes += ", " ; |
13687 | sizes += to_array_size(type, index: i); |
13688 | sizes += ">" ; |
13689 | } |
13690 | |
13691 | res += type_name + sizes; |
13692 | return res; |
13693 | } |
13694 | } |
13695 | |
13696 | string CompilerMSL::type_to_array_glsl(const SPIRType &type) |
13697 | { |
13698 | // Allow Metal to use the array<T> template to make arrays a value type |
13699 | switch (type.basetype) |
13700 | { |
13701 | case SPIRType::AtomicCounter: |
13702 | case SPIRType::ControlPointArray: |
13703 | case SPIRType::RayQuery: |
13704 | { |
13705 | return CompilerGLSL::type_to_array_glsl(type); |
13706 | } |
13707 | default: |
13708 | { |
13709 | if (using_builtin_array()) |
13710 | return CompilerGLSL::type_to_array_glsl(type); |
13711 | else |
13712 | return "" ; |
13713 | } |
13714 | } |
13715 | } |
13716 | |
13717 | string CompilerMSL::constant_op_expression(const SPIRConstantOp &cop) |
13718 | { |
13719 | switch (cop.opcode) |
13720 | { |
13721 | case OpQuantizeToF16: |
13722 | add_spv_func_and_recompile(spv_func: SPVFuncImplQuantizeToF16); |
13723 | return join(ts: "spvQuantizeToF16(" , ts: to_expression(id: cop.arguments[0]), ts: ")" ); |
13724 | default: |
13725 | return CompilerGLSL::constant_op_expression(cop); |
13726 | } |
13727 | } |
13728 | |
13729 | bool CompilerMSL::variable_decl_is_remapped_storage(const SPIRVariable &variable, spv::StorageClass storage) const |
13730 | { |
13731 | if (variable.storage == storage) |
13732 | return true; |
13733 | |
13734 | if (storage == StorageClassWorkgroup) |
13735 | { |
13736 | auto model = get_execution_model(); |
13737 | |
13738 | // Specially masked IO block variable. |
13739 | // Normally, we will never access IO blocks directly here. |
13740 | // The only scenario which that should occur is with a masked IO block. |
13741 | if (model == ExecutionModelTessellationControl && variable.storage == StorageClassOutput && |
13742 | has_decoration(id: get<SPIRType>(id: variable.basetype).self, decoration: DecorationBlock)) |
13743 | { |
13744 | return true; |
13745 | } |
13746 | |
13747 | return variable.storage == StorageClassOutput && |
13748 | model == ExecutionModelTessellationControl && |
13749 | is_stage_output_variable_masked(var: variable); |
13750 | } |
13751 | else if (storage == StorageClassStorageBuffer) |
13752 | { |
13753 | // We won't be able to catch writes to control point outputs here since variable |
13754 | // refers to a function local pointer. |
13755 | // This is fine, as there cannot be concurrent writers to that memory anyways, |
13756 | // so we just ignore that case. |
13757 | |
13758 | return (variable.storage == StorageClassOutput || variable.storage == StorageClassInput) && |
13759 | !variable_storage_requires_stage_io(storage: variable.storage) && |
13760 | (variable.storage != StorageClassOutput || !is_stage_output_variable_masked(var: variable)); |
13761 | } |
13762 | else |
13763 | { |
13764 | return false; |
13765 | } |
13766 | } |
13767 | |
13768 | std::string CompilerMSL::variable_decl(const SPIRVariable &variable) |
13769 | { |
13770 | bool old_is_using_builtin_array = is_using_builtin_array; |
13771 | |
13772 | // Threadgroup arrays can't have a wrapper type. |
13773 | if (variable_decl_is_remapped_storage(variable, storage: StorageClassWorkgroup)) |
13774 | is_using_builtin_array = true; |
13775 | |
13776 | auto expr = CompilerGLSL::variable_decl(variable); |
13777 | is_using_builtin_array = old_is_using_builtin_array; |
13778 | return expr; |
13779 | } |
13780 | |
13781 | // GCC workaround of lambdas calling protected funcs |
13782 | std::string CompilerMSL::variable_decl(const SPIRType &type, const std::string &name, uint32_t id) |
13783 | { |
13784 | return CompilerGLSL::variable_decl(type, name, id); |
13785 | } |
13786 | |
13787 | std::string CompilerMSL::sampler_type(const SPIRType &type, uint32_t id) |
13788 | { |
13789 | auto *var = maybe_get<SPIRVariable>(id); |
13790 | if (var && var->basevariable) |
13791 | { |
13792 | // Check against the base variable, and not a fake ID which might have been generated for this variable. |
13793 | id = var->basevariable; |
13794 | } |
13795 | |
13796 | if (!type.array.empty()) |
13797 | { |
13798 | if (!msl_options.supports_msl_version(major: 2)) |
13799 | SPIRV_CROSS_THROW("MSL 2.0 or greater is required for arrays of samplers." ); |
13800 | |
13801 | if (type.array.size() > 1) |
13802 | SPIRV_CROSS_THROW("Arrays of arrays of samplers are not supported in MSL." ); |
13803 | |
13804 | // Arrays of samplers in MSL must be declared with a special array<T, N> syntax ala C++11 std::array. |
13805 | // If we have a runtime array, it could be a variable-count descriptor set binding. |
13806 | uint32_t array_size = to_array_size_literal(type); |
13807 | if (array_size == 0) |
13808 | array_size = get_resource_array_size(id); |
13809 | |
13810 | if (array_size == 0) |
13811 | SPIRV_CROSS_THROW("Unsized array of samplers is not supported in MSL." ); |
13812 | |
13813 | auto &parent = get<SPIRType>(id: get_pointee_type(type).parent_type); |
13814 | return join(ts: "array<" , ts: sampler_type(type: parent, id), ts: ", " , ts&: array_size, ts: ">" ); |
13815 | } |
13816 | else |
13817 | return "sampler" ; |
13818 | } |
13819 | |
13820 | // Returns an MSL string describing the SPIR-V image type |
13821 | string CompilerMSL::image_type_glsl(const SPIRType &type, uint32_t id) |
13822 | { |
13823 | auto *var = maybe_get<SPIRVariable>(id); |
13824 | if (var && var->basevariable) |
13825 | { |
13826 | // For comparison images, check against the base variable, |
13827 | // and not the fake ID which might have been generated for this variable. |
13828 | id = var->basevariable; |
13829 | } |
13830 | |
13831 | if (!type.array.empty()) |
13832 | { |
13833 | uint32_t major = 2, minor = 0; |
13834 | if (msl_options.is_ios()) |
13835 | { |
13836 | major = 1; |
13837 | minor = 2; |
13838 | } |
13839 | if (!msl_options.supports_msl_version(major, minor)) |
13840 | { |
13841 | if (msl_options.is_ios()) |
13842 | SPIRV_CROSS_THROW("MSL 1.2 or greater is required for arrays of textures." ); |
13843 | else |
13844 | SPIRV_CROSS_THROW("MSL 2.0 or greater is required for arrays of textures." ); |
13845 | } |
13846 | |
13847 | if (type.array.size() > 1) |
13848 | SPIRV_CROSS_THROW("Arrays of arrays of textures are not supported in MSL." ); |
13849 | |
13850 | // Arrays of images in MSL must be declared with a special array<T, N> syntax ala C++11 std::array. |
13851 | // If we have a runtime array, it could be a variable-count descriptor set binding. |
13852 | uint32_t array_size = to_array_size_literal(type); |
13853 | if (array_size == 0) |
13854 | array_size = get_resource_array_size(id); |
13855 | |
13856 | if (array_size == 0) |
13857 | SPIRV_CROSS_THROW("Unsized array of images is not supported in MSL." ); |
13858 | |
13859 | auto &parent = get<SPIRType>(id: get_pointee_type(type).parent_type); |
13860 | return join(ts: "array<" , ts: image_type_glsl(type: parent, id), ts: ", " , ts&: array_size, ts: ">" ); |
13861 | } |
13862 | |
13863 | string img_type_name; |
13864 | |
13865 | // Bypass pointers because we need the real image struct |
13866 | auto &img_type = get<SPIRType>(id: type.self).image; |
13867 | if (is_depth_image(type, id)) |
13868 | { |
13869 | switch (img_type.dim) |
13870 | { |
13871 | case Dim1D: |
13872 | case Dim2D: |
13873 | if (img_type.dim == Dim1D && !msl_options.texture_1D_as_2D) |
13874 | { |
13875 | // Use a native Metal 1D texture |
13876 | img_type_name += "depth1d_unsupported_by_metal" ; |
13877 | break; |
13878 | } |
13879 | |
13880 | if (img_type.ms && img_type.arrayed) |
13881 | { |
13882 | if (!msl_options.supports_msl_version(major: 2, minor: 1)) |
13883 | SPIRV_CROSS_THROW("Multisampled array textures are supported from 2.1." ); |
13884 | img_type_name += "depth2d_ms_array" ; |
13885 | } |
13886 | else if (img_type.ms) |
13887 | img_type_name += "depth2d_ms" ; |
13888 | else if (img_type.arrayed) |
13889 | img_type_name += "depth2d_array" ; |
13890 | else |
13891 | img_type_name += "depth2d" ; |
13892 | break; |
13893 | case Dim3D: |
13894 | img_type_name += "depth3d_unsupported_by_metal" ; |
13895 | break; |
13896 | case DimCube: |
13897 | if (!msl_options.emulate_cube_array) |
13898 | img_type_name += (img_type.arrayed ? "depthcube_array" : "depthcube" ); |
13899 | else |
13900 | img_type_name += (img_type.arrayed ? "depth2d_array" : "depthcube" ); |
13901 | break; |
13902 | default: |
13903 | img_type_name += "unknown_depth_texture_type" ; |
13904 | break; |
13905 | } |
13906 | } |
13907 | else |
13908 | { |
13909 | switch (img_type.dim) |
13910 | { |
13911 | case DimBuffer: |
13912 | if (img_type.ms || img_type.arrayed) |
13913 | SPIRV_CROSS_THROW("Cannot use texel buffers with multisampling or array layers." ); |
13914 | |
13915 | if (msl_options.texture_buffer_native) |
13916 | { |
13917 | if (!msl_options.supports_msl_version(major: 2, minor: 1)) |
13918 | SPIRV_CROSS_THROW("Native texture_buffer type is only supported in MSL 2.1." ); |
13919 | img_type_name = "texture_buffer" ; |
13920 | } |
13921 | else |
13922 | img_type_name += "texture2d" ; |
13923 | break; |
13924 | case Dim1D: |
13925 | case Dim2D: |
13926 | case DimSubpassData: |
13927 | { |
13928 | bool subpass_array = |
13929 | img_type.dim == DimSubpassData && (msl_options.multiview || msl_options.arrayed_subpass_input); |
13930 | if (img_type.dim == Dim1D && !msl_options.texture_1D_as_2D) |
13931 | { |
13932 | // Use a native Metal 1D texture |
13933 | img_type_name += (img_type.arrayed ? "texture1d_array" : "texture1d" ); |
13934 | break; |
13935 | } |
13936 | |
13937 | // Use Metal's native frame-buffer fetch API for subpass inputs. |
13938 | if (type_is_msl_framebuffer_fetch(type)) |
13939 | { |
13940 | auto img_type_4 = get<SPIRType>(id: img_type.type); |
13941 | img_type_4.vecsize = 4; |
13942 | return type_to_glsl(type: img_type_4); |
13943 | } |
13944 | if (img_type.ms && (img_type.arrayed || subpass_array)) |
13945 | { |
13946 | if (!msl_options.supports_msl_version(major: 2, minor: 1)) |
13947 | SPIRV_CROSS_THROW("Multisampled array textures are supported from 2.1." ); |
13948 | img_type_name += "texture2d_ms_array" ; |
13949 | } |
13950 | else if (img_type.ms) |
13951 | img_type_name += "texture2d_ms" ; |
13952 | else if (img_type.arrayed || subpass_array) |
13953 | img_type_name += "texture2d_array" ; |
13954 | else |
13955 | img_type_name += "texture2d" ; |
13956 | break; |
13957 | } |
13958 | case Dim3D: |
13959 | img_type_name += "texture3d" ; |
13960 | break; |
13961 | case DimCube: |
13962 | if (!msl_options.emulate_cube_array) |
13963 | img_type_name += (img_type.arrayed ? "texturecube_array" : "texturecube" ); |
13964 | else |
13965 | img_type_name += (img_type.arrayed ? "texture2d_array" : "texturecube" ); |
13966 | break; |
13967 | default: |
13968 | img_type_name += "unknown_texture_type" ; |
13969 | break; |
13970 | } |
13971 | } |
13972 | |
13973 | // Append the pixel type |
13974 | img_type_name += "<" ; |
13975 | img_type_name += type_to_glsl(type: get<SPIRType>(id: img_type.type)); |
13976 | |
13977 | // For unsampled images, append the sample/read/write access qualifier. |
13978 | // For kernel images, the access qualifier my be supplied directly by SPIR-V. |
13979 | // Otherwise it may be set based on whether the image is read from or written to within the shader. |
13980 | if (type.basetype == SPIRType::Image && type.image.sampled == 2 && type.image.dim != DimSubpassData) |
13981 | { |
13982 | switch (img_type.access) |
13983 | { |
13984 | case AccessQualifierReadOnly: |
13985 | img_type_name += ", access::read" ; |
13986 | break; |
13987 | |
13988 | case AccessQualifierWriteOnly: |
13989 | img_type_name += ", access::write" ; |
13990 | break; |
13991 | |
13992 | case AccessQualifierReadWrite: |
13993 | img_type_name += ", access::read_write" ; |
13994 | break; |
13995 | |
13996 | default: |
13997 | { |
13998 | auto *p_var = maybe_get_backing_variable(chain: id); |
13999 | if (p_var && p_var->basevariable) |
14000 | p_var = maybe_get<SPIRVariable>(id: p_var->basevariable); |
14001 | if (p_var && !has_decoration(id: p_var->self, decoration: DecorationNonWritable)) |
14002 | { |
14003 | img_type_name += ", access::" ; |
14004 | |
14005 | if (!has_decoration(id: p_var->self, decoration: DecorationNonReadable)) |
14006 | img_type_name += "read_" ; |
14007 | |
14008 | img_type_name += "write" ; |
14009 | } |
14010 | break; |
14011 | } |
14012 | } |
14013 | } |
14014 | |
14015 | img_type_name += ">" ; |
14016 | |
14017 | return img_type_name; |
14018 | } |
14019 | |
14020 | void CompilerMSL::emit_subgroup_op(const Instruction &i) |
14021 | { |
14022 | const uint32_t *ops = stream(instr: i); |
14023 | auto op = static_cast<Op>(i.op); |
14024 | |
14025 | if (msl_options.emulate_subgroups) |
14026 | { |
14027 | // In this mode, only the GroupNonUniform cap is supported. The only op |
14028 | // we need to handle, then, is OpGroupNonUniformElect. |
14029 | if (op != OpGroupNonUniformElect) |
14030 | SPIRV_CROSS_THROW("Subgroup emulation does not support operations other than Elect." ); |
14031 | // In this mode, the subgroup size is assumed to be one, so every invocation |
14032 | // is elected. |
14033 | emit_op(result_type: ops[0], result_id: ops[1], rhs: "true" , forward_rhs: true); |
14034 | return; |
14035 | } |
14036 | |
14037 | // Metal 2.0 is required. iOS only supports quad ops on 11.0 (2.0), with |
14038 | // full support in 13.0 (2.2). macOS only supports broadcast and shuffle on |
14039 | // 10.13 (2.0), with full support in 10.14 (2.1). |
14040 | // Note that Apple GPUs before A13 make no distinction between a quad-group |
14041 | // and a SIMD-group; all SIMD-groups are quad-groups on those. |
14042 | if (!msl_options.supports_msl_version(major: 2)) |
14043 | SPIRV_CROSS_THROW("Subgroups are only supported in Metal 2.0 and up." ); |
14044 | |
14045 | // If we need to do implicit bitcasts, make sure we do it with the correct type. |
14046 | uint32_t integer_width = get_integer_width_for_instruction(instr: i); |
14047 | auto int_type = to_signed_basetype(width: integer_width); |
14048 | auto uint_type = to_unsigned_basetype(width: integer_width); |
14049 | |
14050 | if (msl_options.is_ios() && (!msl_options.supports_msl_version(major: 2, minor: 3) || !msl_options.ios_use_simdgroup_functions)) |
14051 | { |
14052 | switch (op) |
14053 | { |
14054 | default: |
14055 | SPIRV_CROSS_THROW("Subgroup ops beyond broadcast, ballot, and shuffle on iOS require Metal 2.3 and up." ); |
14056 | case OpGroupNonUniformBroadcastFirst: |
14057 | if (!msl_options.supports_msl_version(major: 2, minor: 2)) |
14058 | SPIRV_CROSS_THROW("BroadcastFirst on iOS requires Metal 2.2 and up." ); |
14059 | break; |
14060 | case OpGroupNonUniformElect: |
14061 | if (!msl_options.supports_msl_version(major: 2, minor: 2)) |
14062 | SPIRV_CROSS_THROW("Elect on iOS requires Metal 2.2 and up." ); |
14063 | break; |
14064 | case OpGroupNonUniformAny: |
14065 | case OpGroupNonUniformAll: |
14066 | case OpGroupNonUniformAllEqual: |
14067 | case OpGroupNonUniformBallot: |
14068 | case OpGroupNonUniformInverseBallot: |
14069 | case OpGroupNonUniformBallotBitExtract: |
14070 | case OpGroupNonUniformBallotFindLSB: |
14071 | case OpGroupNonUniformBallotFindMSB: |
14072 | case OpGroupNonUniformBallotBitCount: |
14073 | if (!msl_options.supports_msl_version(major: 2, minor: 2)) |
14074 | SPIRV_CROSS_THROW("Ballot ops on iOS requires Metal 2.2 and up." ); |
14075 | break; |
14076 | case OpGroupNonUniformBroadcast: |
14077 | case OpGroupNonUniformShuffle: |
14078 | case OpGroupNonUniformShuffleXor: |
14079 | case OpGroupNonUniformShuffleUp: |
14080 | case OpGroupNonUniformShuffleDown: |
14081 | case OpGroupNonUniformQuadSwap: |
14082 | case OpGroupNonUniformQuadBroadcast: |
14083 | break; |
14084 | } |
14085 | } |
14086 | |
14087 | if (msl_options.is_macos() && !msl_options.supports_msl_version(major: 2, minor: 1)) |
14088 | { |
14089 | switch (op) |
14090 | { |
14091 | default: |
14092 | SPIRV_CROSS_THROW("Subgroup ops beyond broadcast and shuffle on macOS require Metal 2.1 and up." ); |
14093 | case OpGroupNonUniformBroadcast: |
14094 | case OpGroupNonUniformShuffle: |
14095 | case OpGroupNonUniformShuffleXor: |
14096 | case OpGroupNonUniformShuffleUp: |
14097 | case OpGroupNonUniformShuffleDown: |
14098 | break; |
14099 | } |
14100 | } |
14101 | |
14102 | uint32_t result_type = ops[0]; |
14103 | uint32_t id = ops[1]; |
14104 | |
14105 | auto scope = static_cast<Scope>(evaluate_constant_u32(id: ops[2])); |
14106 | if (scope != ScopeSubgroup) |
14107 | SPIRV_CROSS_THROW("Only subgroup scope is supported." ); |
14108 | |
14109 | switch (op) |
14110 | { |
14111 | case OpGroupNonUniformElect: |
14112 | if (msl_options.use_quadgroup_operation()) |
14113 | emit_op(result_type, result_id: id, rhs: "quad_is_first()" , forward_rhs: false); |
14114 | else |
14115 | emit_op(result_type, result_id: id, rhs: "simd_is_first()" , forward_rhs: false); |
14116 | break; |
14117 | |
14118 | case OpGroupNonUniformBroadcast: |
14119 | emit_binary_func_op(result_type, result_id: id, op0: ops[3], op1: ops[4], op: "spvSubgroupBroadcast" ); |
14120 | break; |
14121 | |
14122 | case OpGroupNonUniformBroadcastFirst: |
14123 | emit_unary_func_op(result_type, result_id: id, op0: ops[3], op: "spvSubgroupBroadcastFirst" ); |
14124 | break; |
14125 | |
14126 | case OpGroupNonUniformBallot: |
14127 | emit_unary_func_op(result_type, result_id: id, op0: ops[3], op: "spvSubgroupBallot" ); |
14128 | break; |
14129 | |
14130 | case OpGroupNonUniformInverseBallot: |
14131 | emit_binary_func_op(result_type, result_id: id, op0: ops[3], op1: builtin_subgroup_invocation_id_id, op: "spvSubgroupBallotBitExtract" ); |
14132 | break; |
14133 | |
14134 | case OpGroupNonUniformBallotBitExtract: |
14135 | emit_binary_func_op(result_type, result_id: id, op0: ops[3], op1: ops[4], op: "spvSubgroupBallotBitExtract" ); |
14136 | break; |
14137 | |
14138 | case OpGroupNonUniformBallotFindLSB: |
14139 | emit_binary_func_op(result_type, result_id: id, op0: ops[3], op1: builtin_subgroup_size_id, op: "spvSubgroupBallotFindLSB" ); |
14140 | break; |
14141 | |
14142 | case OpGroupNonUniformBallotFindMSB: |
14143 | emit_binary_func_op(result_type, result_id: id, op0: ops[3], op1: builtin_subgroup_size_id, op: "spvSubgroupBallotFindMSB" ); |
14144 | break; |
14145 | |
14146 | case OpGroupNonUniformBallotBitCount: |
14147 | { |
14148 | auto operation = static_cast<GroupOperation>(ops[3]); |
14149 | switch (operation) |
14150 | { |
14151 | case GroupOperationReduce: |
14152 | emit_binary_func_op(result_type, result_id: id, op0: ops[4], op1: builtin_subgroup_size_id, op: "spvSubgroupBallotBitCount" ); |
14153 | break; |
14154 | case GroupOperationInclusiveScan: |
14155 | emit_binary_func_op(result_type, result_id: id, op0: ops[4], op1: builtin_subgroup_invocation_id_id, |
14156 | op: "spvSubgroupBallotInclusiveBitCount" ); |
14157 | break; |
14158 | case GroupOperationExclusiveScan: |
14159 | emit_binary_func_op(result_type, result_id: id, op0: ops[4], op1: builtin_subgroup_invocation_id_id, |
14160 | op: "spvSubgroupBallotExclusiveBitCount" ); |
14161 | break; |
14162 | default: |
14163 | SPIRV_CROSS_THROW("Invalid BitCount operation." ); |
14164 | } |
14165 | break; |
14166 | } |
14167 | |
14168 | case OpGroupNonUniformShuffle: |
14169 | emit_binary_func_op(result_type, result_id: id, op0: ops[3], op1: ops[4], op: "spvSubgroupShuffle" ); |
14170 | break; |
14171 | |
14172 | case OpGroupNonUniformShuffleXor: |
14173 | emit_binary_func_op(result_type, result_id: id, op0: ops[3], op1: ops[4], op: "spvSubgroupShuffleXor" ); |
14174 | break; |
14175 | |
14176 | case OpGroupNonUniformShuffleUp: |
14177 | emit_binary_func_op(result_type, result_id: id, op0: ops[3], op1: ops[4], op: "spvSubgroupShuffleUp" ); |
14178 | break; |
14179 | |
14180 | case OpGroupNonUniformShuffleDown: |
14181 | emit_binary_func_op(result_type, result_id: id, op0: ops[3], op1: ops[4], op: "spvSubgroupShuffleDown" ); |
14182 | break; |
14183 | |
14184 | case OpGroupNonUniformAll: |
14185 | if (msl_options.use_quadgroup_operation()) |
14186 | emit_unary_func_op(result_type, result_id: id, op0: ops[3], op: "quad_all" ); |
14187 | else |
14188 | emit_unary_func_op(result_type, result_id: id, op0: ops[3], op: "simd_all" ); |
14189 | break; |
14190 | |
14191 | case OpGroupNonUniformAny: |
14192 | if (msl_options.use_quadgroup_operation()) |
14193 | emit_unary_func_op(result_type, result_id: id, op0: ops[3], op: "quad_any" ); |
14194 | else |
14195 | emit_unary_func_op(result_type, result_id: id, op0: ops[3], op: "simd_any" ); |
14196 | break; |
14197 | |
14198 | case OpGroupNonUniformAllEqual: |
14199 | emit_unary_func_op(result_type, result_id: id, op0: ops[3], op: "spvSubgroupAllEqual" ); |
14200 | break; |
14201 | |
14202 | // clang-format off |
14203 | #define MSL_GROUP_OP(op, msl_op) \ |
14204 | case OpGroupNonUniform##op: \ |
14205 | { \ |
14206 | auto operation = static_cast<GroupOperation>(ops[3]); \ |
14207 | if (operation == GroupOperationReduce) \ |
14208 | emit_unary_func_op(result_type, id, ops[4], "simd_" #msl_op); \ |
14209 | else if (operation == GroupOperationInclusiveScan) \ |
14210 | emit_unary_func_op(result_type, id, ops[4], "simd_prefix_inclusive_" #msl_op); \ |
14211 | else if (operation == GroupOperationExclusiveScan) \ |
14212 | emit_unary_func_op(result_type, id, ops[4], "simd_prefix_exclusive_" #msl_op); \ |
14213 | else if (operation == GroupOperationClusteredReduce) \ |
14214 | { \ |
14215 | /* Only cluster sizes of 4 are supported. */ \ |
14216 | uint32_t cluster_size = evaluate_constant_u32(ops[5]); \ |
14217 | if (cluster_size != 4) \ |
14218 | SPIRV_CROSS_THROW("Metal only supports quad ClusteredReduce."); \ |
14219 | emit_unary_func_op(result_type, id, ops[4], "quad_" #msl_op); \ |
14220 | } \ |
14221 | else \ |
14222 | SPIRV_CROSS_THROW("Invalid group operation."); \ |
14223 | break; \ |
14224 | } |
14225 | MSL_GROUP_OP(FAdd, sum) |
14226 | MSL_GROUP_OP(FMul, product) |
14227 | MSL_GROUP_OP(IAdd, sum) |
14228 | MSL_GROUP_OP(IMul, product) |
14229 | #undef MSL_GROUP_OP |
14230 | // The others, unfortunately, don't support InclusiveScan or ExclusiveScan. |
14231 | |
14232 | #define MSL_GROUP_OP(op, msl_op) \ |
14233 | case OpGroupNonUniform##op: \ |
14234 | { \ |
14235 | auto operation = static_cast<GroupOperation>(ops[3]); \ |
14236 | if (operation == GroupOperationReduce) \ |
14237 | emit_unary_func_op(result_type, id, ops[4], "simd_" #msl_op); \ |
14238 | else if (operation == GroupOperationInclusiveScan) \ |
14239 | SPIRV_CROSS_THROW("Metal doesn't support InclusiveScan for OpGroupNonUniform" #op "."); \ |
14240 | else if (operation == GroupOperationExclusiveScan) \ |
14241 | SPIRV_CROSS_THROW("Metal doesn't support ExclusiveScan for OpGroupNonUniform" #op "."); \ |
14242 | else if (operation == GroupOperationClusteredReduce) \ |
14243 | { \ |
14244 | /* Only cluster sizes of 4 are supported. */ \ |
14245 | uint32_t cluster_size = evaluate_constant_u32(ops[5]); \ |
14246 | if (cluster_size != 4) \ |
14247 | SPIRV_CROSS_THROW("Metal only supports quad ClusteredReduce."); \ |
14248 | emit_unary_func_op(result_type, id, ops[4], "quad_" #msl_op); \ |
14249 | } \ |
14250 | else \ |
14251 | SPIRV_CROSS_THROW("Invalid group operation."); \ |
14252 | break; \ |
14253 | } |
14254 | |
14255 | #define MSL_GROUP_OP_CAST(op, msl_op, type) \ |
14256 | case OpGroupNonUniform##op: \ |
14257 | { \ |
14258 | auto operation = static_cast<GroupOperation>(ops[3]); \ |
14259 | if (operation == GroupOperationReduce) \ |
14260 | emit_unary_func_op_cast(result_type, id, ops[4], "simd_" #msl_op, type, type); \ |
14261 | else if (operation == GroupOperationInclusiveScan) \ |
14262 | SPIRV_CROSS_THROW("Metal doesn't support InclusiveScan for OpGroupNonUniform" #op "."); \ |
14263 | else if (operation == GroupOperationExclusiveScan) \ |
14264 | SPIRV_CROSS_THROW("Metal doesn't support ExclusiveScan for OpGroupNonUniform" #op "."); \ |
14265 | else if (operation == GroupOperationClusteredReduce) \ |
14266 | { \ |
14267 | /* Only cluster sizes of 4 are supported. */ \ |
14268 | uint32_t cluster_size = evaluate_constant_u32(ops[5]); \ |
14269 | if (cluster_size != 4) \ |
14270 | SPIRV_CROSS_THROW("Metal only supports quad ClusteredReduce."); \ |
14271 | emit_unary_func_op_cast(result_type, id, ops[4], "quad_" #msl_op, type, type); \ |
14272 | } \ |
14273 | else \ |
14274 | SPIRV_CROSS_THROW("Invalid group operation."); \ |
14275 | break; \ |
14276 | } |
14277 | |
14278 | MSL_GROUP_OP(FMin, min) |
14279 | MSL_GROUP_OP(FMax, max) |
14280 | MSL_GROUP_OP_CAST(SMin, min, int_type) |
14281 | MSL_GROUP_OP_CAST(SMax, max, int_type) |
14282 | MSL_GROUP_OP_CAST(UMin, min, uint_type) |
14283 | MSL_GROUP_OP_CAST(UMax, max, uint_type) |
14284 | MSL_GROUP_OP(BitwiseAnd, and) |
14285 | MSL_GROUP_OP(BitwiseOr, or) |
14286 | MSL_GROUP_OP(BitwiseXor, xor) |
14287 | MSL_GROUP_OP(LogicalAnd, and) |
14288 | MSL_GROUP_OP(LogicalOr, or) |
14289 | MSL_GROUP_OP(LogicalXor, xor) |
14290 | // clang-format on |
14291 | #undef MSL_GROUP_OP |
14292 | #undef MSL_GROUP_OP_CAST |
14293 | |
14294 | case OpGroupNonUniformQuadSwap: |
14295 | emit_binary_func_op(result_type, result_id: id, op0: ops[3], op1: ops[4], op: "spvQuadSwap" ); |
14296 | break; |
14297 | |
14298 | case OpGroupNonUniformQuadBroadcast: |
14299 | emit_binary_func_op(result_type, result_id: id, op0: ops[3], op1: ops[4], op: "spvQuadBroadcast" ); |
14300 | break; |
14301 | |
14302 | default: |
14303 | SPIRV_CROSS_THROW("Invalid opcode for subgroup." ); |
14304 | } |
14305 | |
14306 | register_control_dependent_expression(expr: id); |
14307 | } |
14308 | |
14309 | string CompilerMSL::bitcast_glsl_op(const SPIRType &out_type, const SPIRType &in_type) |
14310 | { |
14311 | if (out_type.basetype == in_type.basetype) |
14312 | return "" ; |
14313 | |
14314 | assert(out_type.basetype != SPIRType::Boolean); |
14315 | assert(in_type.basetype != SPIRType::Boolean); |
14316 | |
14317 | bool integral_cast = type_is_integral(type: out_type) && type_is_integral(type: in_type) && (out_type.vecsize == in_type.vecsize); |
14318 | bool same_size_cast = (out_type.width * out_type.vecsize) == (in_type.width * in_type.vecsize); |
14319 | |
14320 | // Bitcasting can only be used between types of the same overall size. |
14321 | // And always formally cast between integers, because it's trivial, and also |
14322 | // because Metal can internally cast the results of some integer ops to a larger |
14323 | // size (eg. short shift right becomes int), which means chaining integer ops |
14324 | // together may introduce size variations that SPIR-V doesn't know about. |
14325 | if (same_size_cast && !integral_cast) |
14326 | { |
14327 | return "as_type<" + type_to_glsl(type: out_type) + ">" ; |
14328 | } |
14329 | else |
14330 | { |
14331 | return type_to_glsl(type: out_type); |
14332 | } |
14333 | } |
14334 | |
14335 | bool CompilerMSL::emit_complex_bitcast(uint32_t, uint32_t, uint32_t) |
14336 | { |
14337 | return false; |
14338 | } |
14339 | |
14340 | // Returns an MSL string identifying the name of a SPIR-V builtin. |
14341 | // Output builtins are qualified with the name of the stage out structure. |
14342 | string CompilerMSL::builtin_to_glsl(BuiltIn builtin, StorageClass storage) |
14343 | { |
14344 | switch (builtin) |
14345 | { |
14346 | // Handle HLSL-style 0-based vertex/instance index. |
14347 | // Override GLSL compiler strictness |
14348 | case BuiltInVertexId: |
14349 | ensure_builtin(storage: StorageClassInput, builtin: BuiltInVertexId); |
14350 | if (msl_options.enable_base_index_zero && msl_options.supports_msl_version(major: 1, minor: 1) && |
14351 | (msl_options.ios_support_base_vertex_instance || msl_options.is_macos())) |
14352 | { |
14353 | if (builtin_declaration) |
14354 | { |
14355 | if (needs_base_vertex_arg != TriState::No) |
14356 | needs_base_vertex_arg = TriState::Yes; |
14357 | return "gl_VertexID" ; |
14358 | } |
14359 | else |
14360 | { |
14361 | ensure_builtin(storage: StorageClassInput, builtin: BuiltInBaseVertex); |
14362 | return "(gl_VertexID - gl_BaseVertex)" ; |
14363 | } |
14364 | } |
14365 | else |
14366 | { |
14367 | return "gl_VertexID" ; |
14368 | } |
14369 | case BuiltInInstanceId: |
14370 | ensure_builtin(storage: StorageClassInput, builtin: BuiltInInstanceId); |
14371 | if (msl_options.enable_base_index_zero && msl_options.supports_msl_version(major: 1, minor: 1) && |
14372 | (msl_options.ios_support_base_vertex_instance || msl_options.is_macos())) |
14373 | { |
14374 | if (builtin_declaration) |
14375 | { |
14376 | if (needs_base_instance_arg != TriState::No) |
14377 | needs_base_instance_arg = TriState::Yes; |
14378 | return "gl_InstanceID" ; |
14379 | } |
14380 | else |
14381 | { |
14382 | ensure_builtin(storage: StorageClassInput, builtin: BuiltInBaseInstance); |
14383 | return "(gl_InstanceID - gl_BaseInstance)" ; |
14384 | } |
14385 | } |
14386 | else |
14387 | { |
14388 | return "gl_InstanceID" ; |
14389 | } |
14390 | case BuiltInVertexIndex: |
14391 | ensure_builtin(storage: StorageClassInput, builtin: BuiltInVertexIndex); |
14392 | if (msl_options.enable_base_index_zero && msl_options.supports_msl_version(major: 1, minor: 1) && |
14393 | (msl_options.ios_support_base_vertex_instance || msl_options.is_macos())) |
14394 | { |
14395 | if (builtin_declaration) |
14396 | { |
14397 | if (needs_base_vertex_arg != TriState::No) |
14398 | needs_base_vertex_arg = TriState::Yes; |
14399 | return "gl_VertexIndex" ; |
14400 | } |
14401 | else |
14402 | { |
14403 | ensure_builtin(storage: StorageClassInput, builtin: BuiltInBaseVertex); |
14404 | return "(gl_VertexIndex - gl_BaseVertex)" ; |
14405 | } |
14406 | } |
14407 | else |
14408 | { |
14409 | return "gl_VertexIndex" ; |
14410 | } |
14411 | case BuiltInInstanceIndex: |
14412 | ensure_builtin(storage: StorageClassInput, builtin: BuiltInInstanceIndex); |
14413 | if (msl_options.enable_base_index_zero && msl_options.supports_msl_version(major: 1, minor: 1) && |
14414 | (msl_options.ios_support_base_vertex_instance || msl_options.is_macos())) |
14415 | { |
14416 | if (builtin_declaration) |
14417 | { |
14418 | if (needs_base_instance_arg != TriState::No) |
14419 | needs_base_instance_arg = TriState::Yes; |
14420 | return "gl_InstanceIndex" ; |
14421 | } |
14422 | else |
14423 | { |
14424 | ensure_builtin(storage: StorageClassInput, builtin: BuiltInBaseInstance); |
14425 | return "(gl_InstanceIndex - gl_BaseInstance)" ; |
14426 | } |
14427 | } |
14428 | else |
14429 | { |
14430 | return "gl_InstanceIndex" ; |
14431 | } |
14432 | case BuiltInBaseVertex: |
14433 | if (msl_options.supports_msl_version(major: 1, minor: 1) && |
14434 | (msl_options.ios_support_base_vertex_instance || msl_options.is_macos())) |
14435 | { |
14436 | needs_base_vertex_arg = TriState::No; |
14437 | return "gl_BaseVertex" ; |
14438 | } |
14439 | else |
14440 | { |
14441 | SPIRV_CROSS_THROW("BaseVertex requires Metal 1.1 and Mac or Apple A9+ hardware." ); |
14442 | } |
14443 | case BuiltInBaseInstance: |
14444 | if (msl_options.supports_msl_version(major: 1, minor: 1) && |
14445 | (msl_options.ios_support_base_vertex_instance || msl_options.is_macos())) |
14446 | { |
14447 | needs_base_instance_arg = TriState::No; |
14448 | return "gl_BaseInstance" ; |
14449 | } |
14450 | else |
14451 | { |
14452 | SPIRV_CROSS_THROW("BaseInstance requires Metal 1.1 and Mac or Apple A9+ hardware." ); |
14453 | } |
14454 | case BuiltInDrawIndex: |
14455 | SPIRV_CROSS_THROW("DrawIndex is not supported in MSL." ); |
14456 | |
14457 | // When used in the entry function, output builtins are qualified with output struct name. |
14458 | // Test storage class as NOT Input, as output builtins might be part of generic type. |
14459 | // Also don't do this for tessellation control shaders. |
14460 | case BuiltInViewportIndex: |
14461 | if (!msl_options.supports_msl_version(major: 2, minor: 0)) |
14462 | SPIRV_CROSS_THROW("ViewportIndex requires Metal 2.0." ); |
14463 | /* fallthrough */ |
14464 | case BuiltInFragDepth: |
14465 | case BuiltInFragStencilRefEXT: |
14466 | if ((builtin == BuiltInFragDepth && !msl_options.enable_frag_depth_builtin) || |
14467 | (builtin == BuiltInFragStencilRefEXT && !msl_options.enable_frag_stencil_ref_builtin)) |
14468 | break; |
14469 | /* fallthrough */ |
14470 | case BuiltInPosition: |
14471 | case BuiltInPointSize: |
14472 | case BuiltInClipDistance: |
14473 | case BuiltInCullDistance: |
14474 | case BuiltInLayer: |
14475 | if (get_execution_model() == ExecutionModelTessellationControl) |
14476 | break; |
14477 | if (storage != StorageClassInput && current_function && (current_function->self == ir.default_entry_point) && |
14478 | !is_stage_output_builtin_masked(builtin)) |
14479 | return stage_out_var_name + "." + CompilerGLSL::builtin_to_glsl(builtin, storage); |
14480 | break; |
14481 | |
14482 | case BuiltInSampleMask: |
14483 | if (storage == StorageClassInput && current_function && (current_function->self == ir.default_entry_point) && |
14484 | (has_additional_fixed_sample_mask() || needs_sample_id)) |
14485 | { |
14486 | string samp_mask_in; |
14487 | samp_mask_in += "(" + CompilerGLSL::builtin_to_glsl(builtin, storage); |
14488 | if (has_additional_fixed_sample_mask()) |
14489 | samp_mask_in += " & " + additional_fixed_sample_mask_str(); |
14490 | if (needs_sample_id) |
14491 | samp_mask_in += " & (1 << gl_SampleID)" ; |
14492 | samp_mask_in += ")" ; |
14493 | return samp_mask_in; |
14494 | } |
14495 | if (storage != StorageClassInput && current_function && (current_function->self == ir.default_entry_point) && |
14496 | !is_stage_output_builtin_masked(builtin)) |
14497 | return stage_out_var_name + "." + CompilerGLSL::builtin_to_glsl(builtin, storage); |
14498 | break; |
14499 | |
14500 | case BuiltInBaryCoordKHR: |
14501 | case BuiltInBaryCoordNoPerspKHR: |
14502 | if (storage == StorageClassInput && current_function && (current_function->self == ir.default_entry_point)) |
14503 | return stage_in_var_name + "." + CompilerGLSL::builtin_to_glsl(builtin, storage); |
14504 | break; |
14505 | |
14506 | case BuiltInTessLevelOuter: |
14507 | if (get_execution_model() == ExecutionModelTessellationControl && |
14508 | storage != StorageClassInput && current_function && (current_function->self == ir.default_entry_point)) |
14509 | { |
14510 | return join(ts&: tess_factor_buffer_var_name, ts: "[" , ts: to_expression(id: builtin_primitive_id_id), |
14511 | ts: "].edgeTessellationFactor" ); |
14512 | } |
14513 | break; |
14514 | |
14515 | case BuiltInTessLevelInner: |
14516 | if (get_execution_model() == ExecutionModelTessellationControl && |
14517 | storage != StorageClassInput && current_function && (current_function->self == ir.default_entry_point)) |
14518 | { |
14519 | return join(ts&: tess_factor_buffer_var_name, ts: "[" , ts: to_expression(id: builtin_primitive_id_id), |
14520 | ts: "].insideTessellationFactor" ); |
14521 | } |
14522 | break; |
14523 | |
14524 | case BuiltInHelperInvocation: |
14525 | if (msl_options.is_ios() && !msl_options.supports_msl_version(major: 2, minor: 3)) |
14526 | SPIRV_CROSS_THROW("simd_is_helper_thread() requires version 2.3 on iOS." ); |
14527 | else if (msl_options.is_macos() && !msl_options.supports_msl_version(major: 2, minor: 1)) |
14528 | SPIRV_CROSS_THROW("simd_is_helper_thread() requires version 2.1 on macOS." ); |
14529 | // In SPIR-V 1.6 with Volatile HelperInvocation, we cannot emit a fixup early. |
14530 | return "simd_is_helper_thread()" ; |
14531 | |
14532 | default: |
14533 | break; |
14534 | } |
14535 | |
14536 | return CompilerGLSL::builtin_to_glsl(builtin, storage); |
14537 | } |
14538 | |
14539 | // Returns an MSL string attribute qualifer for a SPIR-V builtin |
14540 | string CompilerMSL::builtin_qualifier(BuiltIn builtin) |
14541 | { |
14542 | auto &execution = get_entry_point(); |
14543 | |
14544 | switch (builtin) |
14545 | { |
14546 | // Vertex function in |
14547 | case BuiltInVertexId: |
14548 | return "vertex_id" ; |
14549 | case BuiltInVertexIndex: |
14550 | return "vertex_id" ; |
14551 | case BuiltInBaseVertex: |
14552 | return "base_vertex" ; |
14553 | case BuiltInInstanceId: |
14554 | return "instance_id" ; |
14555 | case BuiltInInstanceIndex: |
14556 | return "instance_id" ; |
14557 | case BuiltInBaseInstance: |
14558 | return "base_instance" ; |
14559 | case BuiltInDrawIndex: |
14560 | SPIRV_CROSS_THROW("DrawIndex is not supported in MSL." ); |
14561 | |
14562 | // Vertex function out |
14563 | case BuiltInClipDistance: |
14564 | return "clip_distance" ; |
14565 | case BuiltInPointSize: |
14566 | return "point_size" ; |
14567 | case BuiltInPosition: |
14568 | if (position_invariant) |
14569 | { |
14570 | if (!msl_options.supports_msl_version(major: 2, minor: 1)) |
14571 | SPIRV_CROSS_THROW("Invariant position is only supported on MSL 2.1 and up." ); |
14572 | return "position, invariant" ; |
14573 | } |
14574 | else |
14575 | return "position" ; |
14576 | case BuiltInLayer: |
14577 | return "render_target_array_index" ; |
14578 | case BuiltInViewportIndex: |
14579 | if (!msl_options.supports_msl_version(major: 2, minor: 0)) |
14580 | SPIRV_CROSS_THROW("ViewportIndex requires Metal 2.0." ); |
14581 | return "viewport_array_index" ; |
14582 | |
14583 | // Tess. control function in |
14584 | case BuiltInInvocationId: |
14585 | if (msl_options.multi_patch_workgroup) |
14586 | { |
14587 | // Shouldn't be reached. |
14588 | SPIRV_CROSS_THROW("InvocationId is computed manually with multi-patch workgroups in MSL." ); |
14589 | } |
14590 | return "thread_index_in_threadgroup" ; |
14591 | case BuiltInPatchVertices: |
14592 | // Shouldn't be reached. |
14593 | SPIRV_CROSS_THROW("PatchVertices is derived from the auxiliary buffer in MSL." ); |
14594 | case BuiltInPrimitiveId: |
14595 | switch (execution.model) |
14596 | { |
14597 | case ExecutionModelTessellationControl: |
14598 | if (msl_options.multi_patch_workgroup) |
14599 | { |
14600 | // Shouldn't be reached. |
14601 | SPIRV_CROSS_THROW("PrimitiveId is computed manually with multi-patch workgroups in MSL." ); |
14602 | } |
14603 | return "threadgroup_position_in_grid" ; |
14604 | case ExecutionModelTessellationEvaluation: |
14605 | return "patch_id" ; |
14606 | case ExecutionModelFragment: |
14607 | if (msl_options.is_ios() && !msl_options.supports_msl_version(major: 2, minor: 3)) |
14608 | SPIRV_CROSS_THROW("PrimitiveId on iOS requires MSL 2.3." ); |
14609 | else if (msl_options.is_macos() && !msl_options.supports_msl_version(major: 2, minor: 2)) |
14610 | SPIRV_CROSS_THROW("PrimitiveId on macOS requires MSL 2.2." ); |
14611 | return "primitive_id" ; |
14612 | default: |
14613 | SPIRV_CROSS_THROW("PrimitiveId is not supported in this execution model." ); |
14614 | } |
14615 | |
14616 | // Tess. control function out |
14617 | case BuiltInTessLevelOuter: |
14618 | case BuiltInTessLevelInner: |
14619 | // Shouldn't be reached. |
14620 | SPIRV_CROSS_THROW("Tessellation levels are handled specially in MSL." ); |
14621 | |
14622 | // Tess. evaluation function in |
14623 | case BuiltInTessCoord: |
14624 | return "position_in_patch" ; |
14625 | |
14626 | // Fragment function in |
14627 | case BuiltInFrontFacing: |
14628 | return "front_facing" ; |
14629 | case BuiltInPointCoord: |
14630 | return "point_coord" ; |
14631 | case BuiltInFragCoord: |
14632 | return "position" ; |
14633 | case BuiltInSampleId: |
14634 | return "sample_id" ; |
14635 | case BuiltInSampleMask: |
14636 | return "sample_mask" ; |
14637 | case BuiltInSamplePosition: |
14638 | // Shouldn't be reached. |
14639 | SPIRV_CROSS_THROW("Sample position is retrieved by a function in MSL." ); |
14640 | case BuiltInViewIndex: |
14641 | if (execution.model != ExecutionModelFragment) |
14642 | SPIRV_CROSS_THROW("ViewIndex is handled specially outside fragment shaders." ); |
14643 | // The ViewIndex was implicitly used in the prior stages to set the render_target_array_index, |
14644 | // so we can get it from there. |
14645 | return "render_target_array_index" ; |
14646 | |
14647 | // Fragment function out |
14648 | case BuiltInFragDepth: |
14649 | if (execution.flags.get(bit: ExecutionModeDepthGreater)) |
14650 | return "depth(greater)" ; |
14651 | else if (execution.flags.get(bit: ExecutionModeDepthLess)) |
14652 | return "depth(less)" ; |
14653 | else |
14654 | return "depth(any)" ; |
14655 | |
14656 | case BuiltInFragStencilRefEXT: |
14657 | return "stencil" ; |
14658 | |
14659 | // Compute function in |
14660 | case BuiltInGlobalInvocationId: |
14661 | return "thread_position_in_grid" ; |
14662 | |
14663 | case BuiltInWorkgroupId: |
14664 | return "threadgroup_position_in_grid" ; |
14665 | |
14666 | case BuiltInNumWorkgroups: |
14667 | return "threadgroups_per_grid" ; |
14668 | |
14669 | case BuiltInLocalInvocationId: |
14670 | return "thread_position_in_threadgroup" ; |
14671 | |
14672 | case BuiltInLocalInvocationIndex: |
14673 | return "thread_index_in_threadgroup" ; |
14674 | |
14675 | case BuiltInSubgroupSize: |
14676 | if (msl_options.emulate_subgroups || msl_options.fixed_subgroup_size != 0) |
14677 | // Shouldn't be reached. |
14678 | SPIRV_CROSS_THROW("Emitting threads_per_simdgroup attribute with fixed subgroup size??" ); |
14679 | if (execution.model == ExecutionModelFragment) |
14680 | { |
14681 | if (!msl_options.supports_msl_version(major: 2, minor: 2)) |
14682 | SPIRV_CROSS_THROW("threads_per_simdgroup requires Metal 2.2 in fragment shaders." ); |
14683 | return "threads_per_simdgroup" ; |
14684 | } |
14685 | else |
14686 | { |
14687 | // thread_execution_width is an alias for threads_per_simdgroup, and it's only available since 1.0, |
14688 | // but not in fragment. |
14689 | return "thread_execution_width" ; |
14690 | } |
14691 | |
14692 | case BuiltInNumSubgroups: |
14693 | if (msl_options.emulate_subgroups) |
14694 | // Shouldn't be reached. |
14695 | SPIRV_CROSS_THROW("NumSubgroups is handled specially with emulation." ); |
14696 | if (!msl_options.supports_msl_version(major: 2)) |
14697 | SPIRV_CROSS_THROW("Subgroup builtins require Metal 2.0." ); |
14698 | return msl_options.use_quadgroup_operation() ? "quadgroups_per_threadgroup" : "simdgroups_per_threadgroup" ; |
14699 | |
14700 | case BuiltInSubgroupId: |
14701 | if (msl_options.emulate_subgroups) |
14702 | // Shouldn't be reached. |
14703 | SPIRV_CROSS_THROW("SubgroupId is handled specially with emulation." ); |
14704 | if (!msl_options.supports_msl_version(major: 2)) |
14705 | SPIRV_CROSS_THROW("Subgroup builtins require Metal 2.0." ); |
14706 | return msl_options.use_quadgroup_operation() ? "quadgroup_index_in_threadgroup" : "simdgroup_index_in_threadgroup" ; |
14707 | |
14708 | case BuiltInSubgroupLocalInvocationId: |
14709 | if (msl_options.emulate_subgroups) |
14710 | // Shouldn't be reached. |
14711 | SPIRV_CROSS_THROW("SubgroupLocalInvocationId is handled specially with emulation." ); |
14712 | if (execution.model == ExecutionModelFragment) |
14713 | { |
14714 | if (!msl_options.supports_msl_version(major: 2, minor: 2)) |
14715 | SPIRV_CROSS_THROW("thread_index_in_simdgroup requires Metal 2.2 in fragment shaders." ); |
14716 | return "thread_index_in_simdgroup" ; |
14717 | } |
14718 | else if (execution.model == ExecutionModelKernel || execution.model == ExecutionModelGLCompute || |
14719 | execution.model == ExecutionModelTessellationControl || |
14720 | (execution.model == ExecutionModelVertex && msl_options.vertex_for_tessellation)) |
14721 | { |
14722 | // We are generating a Metal kernel function. |
14723 | if (!msl_options.supports_msl_version(major: 2)) |
14724 | SPIRV_CROSS_THROW("Subgroup builtins in kernel functions require Metal 2.0." ); |
14725 | return msl_options.use_quadgroup_operation() ? "thread_index_in_quadgroup" : "thread_index_in_simdgroup" ; |
14726 | } |
14727 | else |
14728 | SPIRV_CROSS_THROW("Subgroup builtins are not available in this type of function." ); |
14729 | |
14730 | case BuiltInSubgroupEqMask: |
14731 | case BuiltInSubgroupGeMask: |
14732 | case BuiltInSubgroupGtMask: |
14733 | case BuiltInSubgroupLeMask: |
14734 | case BuiltInSubgroupLtMask: |
14735 | // Shouldn't be reached. |
14736 | SPIRV_CROSS_THROW("Subgroup ballot masks are handled specially in MSL." ); |
14737 | |
14738 | case BuiltInBaryCoordKHR: |
14739 | if (msl_options.is_ios() && !msl_options.supports_msl_version(major: 2, minor: 3)) |
14740 | SPIRV_CROSS_THROW("Barycentrics are only supported in MSL 2.3 and above on iOS." ); |
14741 | else if (!msl_options.supports_msl_version(major: 2, minor: 2)) |
14742 | SPIRV_CROSS_THROW("Barycentrics are only supported in MSL 2.2 and above on macOS." ); |
14743 | return "barycentric_coord, center_perspective" ; |
14744 | |
14745 | case BuiltInBaryCoordNoPerspKHR: |
14746 | if (msl_options.is_ios() && !msl_options.supports_msl_version(major: 2, minor: 3)) |
14747 | SPIRV_CROSS_THROW("Barycentrics are only supported in MSL 2.3 and above on iOS." ); |
14748 | else if (!msl_options.supports_msl_version(major: 2, minor: 2)) |
14749 | SPIRV_CROSS_THROW("Barycentrics are only supported in MSL 2.2 and above on macOS." ); |
14750 | return "barycentric_coord, center_no_perspective" ; |
14751 | |
14752 | default: |
14753 | return "unsupported-built-in" ; |
14754 | } |
14755 | } |
14756 | |
14757 | // Returns an MSL string type declaration for a SPIR-V builtin |
14758 | string CompilerMSL::builtin_type_decl(BuiltIn builtin, uint32_t id) |
14759 | { |
14760 | const SPIREntryPoint &execution = get_entry_point(); |
14761 | switch (builtin) |
14762 | { |
14763 | // Vertex function in |
14764 | case BuiltInVertexId: |
14765 | return "uint" ; |
14766 | case BuiltInVertexIndex: |
14767 | return "uint" ; |
14768 | case BuiltInBaseVertex: |
14769 | return "uint" ; |
14770 | case BuiltInInstanceId: |
14771 | return "uint" ; |
14772 | case BuiltInInstanceIndex: |
14773 | return "uint" ; |
14774 | case BuiltInBaseInstance: |
14775 | return "uint" ; |
14776 | case BuiltInDrawIndex: |
14777 | SPIRV_CROSS_THROW("DrawIndex is not supported in MSL." ); |
14778 | |
14779 | // Vertex function out |
14780 | case BuiltInClipDistance: |
14781 | case BuiltInCullDistance: |
14782 | return "float" ; |
14783 | case BuiltInPointSize: |
14784 | return "float" ; |
14785 | case BuiltInPosition: |
14786 | return "float4" ; |
14787 | case BuiltInLayer: |
14788 | return "uint" ; |
14789 | case BuiltInViewportIndex: |
14790 | if (!msl_options.supports_msl_version(major: 2, minor: 0)) |
14791 | SPIRV_CROSS_THROW("ViewportIndex requires Metal 2.0." ); |
14792 | return "uint" ; |
14793 | |
14794 | // Tess. control function in |
14795 | case BuiltInInvocationId: |
14796 | return "uint" ; |
14797 | case BuiltInPatchVertices: |
14798 | return "uint" ; |
14799 | case BuiltInPrimitiveId: |
14800 | return "uint" ; |
14801 | |
14802 | // Tess. control function out |
14803 | case BuiltInTessLevelInner: |
14804 | if (execution.model == ExecutionModelTessellationEvaluation) |
14805 | return !execution.flags.get(bit: ExecutionModeTriangles) ? "float2" : "float" ; |
14806 | return "half" ; |
14807 | case BuiltInTessLevelOuter: |
14808 | if (execution.model == ExecutionModelTessellationEvaluation) |
14809 | return !execution.flags.get(bit: ExecutionModeTriangles) ? "float4" : "float" ; |
14810 | return "half" ; |
14811 | |
14812 | // Tess. evaluation function in |
14813 | case BuiltInTessCoord: |
14814 | return "float3" ; |
14815 | |
14816 | // Fragment function in |
14817 | case BuiltInFrontFacing: |
14818 | return "bool" ; |
14819 | case BuiltInPointCoord: |
14820 | return "float2" ; |
14821 | case BuiltInFragCoord: |
14822 | return "float4" ; |
14823 | case BuiltInSampleId: |
14824 | return "uint" ; |
14825 | case BuiltInSampleMask: |
14826 | return "uint" ; |
14827 | case BuiltInSamplePosition: |
14828 | return "float2" ; |
14829 | case BuiltInViewIndex: |
14830 | return "uint" ; |
14831 | |
14832 | case BuiltInHelperInvocation: |
14833 | return "bool" ; |
14834 | |
14835 | case BuiltInBaryCoordKHR: |
14836 | case BuiltInBaryCoordNoPerspKHR: |
14837 | // Use the type as declared, can be 1, 2 or 3 components. |
14838 | return type_to_glsl(type: get_variable_data_type(var: get<SPIRVariable>(id))); |
14839 | |
14840 | // Fragment function out |
14841 | case BuiltInFragDepth: |
14842 | return "float" ; |
14843 | |
14844 | case BuiltInFragStencilRefEXT: |
14845 | return "uint" ; |
14846 | |
14847 | // Compute function in |
14848 | case BuiltInGlobalInvocationId: |
14849 | case BuiltInLocalInvocationId: |
14850 | case BuiltInNumWorkgroups: |
14851 | case BuiltInWorkgroupId: |
14852 | return "uint3" ; |
14853 | case BuiltInLocalInvocationIndex: |
14854 | case BuiltInNumSubgroups: |
14855 | case BuiltInSubgroupId: |
14856 | case BuiltInSubgroupSize: |
14857 | case BuiltInSubgroupLocalInvocationId: |
14858 | return "uint" ; |
14859 | case BuiltInSubgroupEqMask: |
14860 | case BuiltInSubgroupGeMask: |
14861 | case BuiltInSubgroupGtMask: |
14862 | case BuiltInSubgroupLeMask: |
14863 | case BuiltInSubgroupLtMask: |
14864 | return "uint4" ; |
14865 | |
14866 | case BuiltInDeviceIndex: |
14867 | return "int" ; |
14868 | |
14869 | default: |
14870 | return "unsupported-built-in-type" ; |
14871 | } |
14872 | } |
14873 | |
14874 | // Returns the declaration of a built-in argument to a function |
14875 | string CompilerMSL::built_in_func_arg(BuiltIn builtin, bool prefix_comma) |
14876 | { |
14877 | string bi_arg; |
14878 | if (prefix_comma) |
14879 | bi_arg += ", " ; |
14880 | |
14881 | // Handle HLSL-style 0-based vertex/instance index. |
14882 | builtin_declaration = true; |
14883 | bi_arg += builtin_type_decl(builtin); |
14884 | bi_arg += " " + builtin_to_glsl(builtin, storage: StorageClassInput); |
14885 | bi_arg += " [[" + builtin_qualifier(builtin) + "]]" ; |
14886 | builtin_declaration = false; |
14887 | |
14888 | return bi_arg; |
14889 | } |
14890 | |
14891 | const SPIRType &CompilerMSL::get_physical_member_type(const SPIRType &type, uint32_t index) const |
14892 | { |
14893 | if (member_is_remapped_physical_type(type, index)) |
14894 | return get<SPIRType>(id: get_extended_member_decoration(type: type.self, index, decoration: SPIRVCrossDecorationPhysicalTypeID)); |
14895 | else |
14896 | return get<SPIRType>(id: type.member_types[index]); |
14897 | } |
14898 | |
14899 | SPIRType CompilerMSL::get_presumed_input_type(const SPIRType &ib_type, uint32_t index) const |
14900 | { |
14901 | SPIRType type = get_physical_member_type(type: ib_type, index); |
14902 | uint32_t loc = get_member_decoration(id: ib_type.self, index, decoration: DecorationLocation); |
14903 | uint32_t cmp = get_member_decoration(id: ib_type.self, index, decoration: DecorationComponent); |
14904 | auto p_va = inputs_by_location.find(x: {.location: loc, .component: cmp}); |
14905 | if (p_va != end(cont: inputs_by_location) && p_va->second.vecsize > type.vecsize) |
14906 | type.vecsize = p_va->second.vecsize; |
14907 | |
14908 | return type; |
14909 | } |
14910 | |
14911 | uint32_t CompilerMSL::get_declared_type_array_stride_msl(const SPIRType &type, bool is_packed, bool row_major) const |
14912 | { |
14913 | // Array stride in MSL is always size * array_size. sizeof(float3) == 16, |
14914 | // unlike GLSL and HLSL where array stride would be 16 and size 12. |
14915 | |
14916 | // We could use parent type here and recurse, but that makes creating physical type remappings |
14917 | // far more complicated. We'd rather just create the final type, and ignore having to create the entire type |
14918 | // hierarchy in order to compute this value, so make a temporary type on the stack. |
14919 | |
14920 | auto basic_type = type; |
14921 | basic_type.array.clear(); |
14922 | basic_type.array_size_literal.clear(); |
14923 | uint32_t value_size = get_declared_type_size_msl(type: basic_type, packed: is_packed, row_major); |
14924 | |
14925 | uint32_t dimensions = uint32_t(type.array.size()); |
14926 | assert(dimensions > 0); |
14927 | dimensions--; |
14928 | |
14929 | // Multiply together every dimension, except the last one. |
14930 | for (uint32_t dim = 0; dim < dimensions; dim++) |
14931 | { |
14932 | uint32_t array_size = to_array_size_literal(type, index: dim); |
14933 | value_size *= max(a: array_size, b: 1u); |
14934 | } |
14935 | |
14936 | return value_size; |
14937 | } |
14938 | |
14939 | uint32_t CompilerMSL::get_declared_struct_member_array_stride_msl(const SPIRType &type, uint32_t index) const |
14940 | { |
14941 | return get_declared_type_array_stride_msl(type: get_physical_member_type(type, index), |
14942 | is_packed: member_is_packed_physical_type(type, index), |
14943 | row_major: has_member_decoration(id: type.self, index, decoration: DecorationRowMajor)); |
14944 | } |
14945 | |
14946 | uint32_t CompilerMSL::get_declared_input_array_stride_msl(const SPIRType &type, uint32_t index) const |
14947 | { |
14948 | return get_declared_type_array_stride_msl(type: get_presumed_input_type(ib_type: type, index), is_packed: false, |
14949 | row_major: has_member_decoration(id: type.self, index, decoration: DecorationRowMajor)); |
14950 | } |
14951 | |
14952 | uint32_t CompilerMSL::get_declared_type_matrix_stride_msl(const SPIRType &type, bool packed, bool row_major) const |
14953 | { |
14954 | // For packed matrices, we just use the size of the vector type. |
14955 | // Otherwise, MatrixStride == alignment, which is the size of the underlying vector type. |
14956 | if (packed) |
14957 | return (type.width / 8) * ((row_major && type.columns > 1) ? type.columns : type.vecsize); |
14958 | else |
14959 | return get_declared_type_alignment_msl(type, packed: false, row_major); |
14960 | } |
14961 | |
14962 | uint32_t CompilerMSL::get_declared_struct_member_matrix_stride_msl(const SPIRType &type, uint32_t index) const |
14963 | { |
14964 | return get_declared_type_matrix_stride_msl(type: get_physical_member_type(type, index), |
14965 | packed: member_is_packed_physical_type(type, index), |
14966 | row_major: has_member_decoration(id: type.self, index, decoration: DecorationRowMajor)); |
14967 | } |
14968 | |
14969 | uint32_t CompilerMSL::get_declared_input_matrix_stride_msl(const SPIRType &type, uint32_t index) const |
14970 | { |
14971 | return get_declared_type_matrix_stride_msl(type: get_presumed_input_type(ib_type: type, index), packed: false, |
14972 | row_major: has_member_decoration(id: type.self, index, decoration: DecorationRowMajor)); |
14973 | } |
14974 | |
14975 | uint32_t CompilerMSL::get_declared_struct_size_msl(const SPIRType &struct_type, bool ignore_alignment, |
14976 | bool ignore_padding) const |
14977 | { |
14978 | // If we have a target size, that is the declared size as well. |
14979 | if (!ignore_padding && has_extended_decoration(id: struct_type.self, decoration: SPIRVCrossDecorationPaddingTarget)) |
14980 | return get_extended_decoration(id: struct_type.self, decoration: SPIRVCrossDecorationPaddingTarget); |
14981 | |
14982 | if (struct_type.member_types.empty()) |
14983 | return 0; |
14984 | |
14985 | uint32_t mbr_cnt = uint32_t(struct_type.member_types.size()); |
14986 | |
14987 | // In MSL, a struct's alignment is equal to the maximum alignment of any of its members. |
14988 | uint32_t alignment = 1; |
14989 | |
14990 | if (!ignore_alignment) |
14991 | { |
14992 | for (uint32_t i = 0; i < mbr_cnt; i++) |
14993 | { |
14994 | uint32_t mbr_alignment = get_declared_struct_member_alignment_msl(struct_type, index: i); |
14995 | alignment = max(a: alignment, b: mbr_alignment); |
14996 | } |
14997 | } |
14998 | |
14999 | // Last member will always be matched to the final Offset decoration, but size of struct in MSL now depends |
15000 | // on physical size in MSL, and the size of the struct itself is then aligned to struct alignment. |
15001 | uint32_t spirv_offset = type_struct_member_offset(type: struct_type, index: mbr_cnt - 1); |
15002 | uint32_t msl_size = spirv_offset + get_declared_struct_member_size_msl(struct_type, index: mbr_cnt - 1); |
15003 | msl_size = (msl_size + alignment - 1) & ~(alignment - 1); |
15004 | return msl_size; |
15005 | } |
15006 | |
15007 | // Returns the byte size of a struct member. |
15008 | uint32_t CompilerMSL::get_declared_type_size_msl(const SPIRType &type, bool is_packed, bool row_major) const |
15009 | { |
15010 | switch (type.basetype) |
15011 | { |
15012 | case SPIRType::Unknown: |
15013 | case SPIRType::Void: |
15014 | case SPIRType::AtomicCounter: |
15015 | case SPIRType::Image: |
15016 | case SPIRType::SampledImage: |
15017 | case SPIRType::Sampler: |
15018 | SPIRV_CROSS_THROW("Querying size of opaque object." ); |
15019 | |
15020 | default: |
15021 | { |
15022 | if (!type.array.empty()) |
15023 | { |
15024 | uint32_t array_size = to_array_size_literal(type); |
15025 | return get_declared_type_array_stride_msl(type, is_packed, row_major) * max(a: array_size, b: 1u); |
15026 | } |
15027 | |
15028 | if (type.basetype == SPIRType::Struct) |
15029 | return get_declared_struct_size_msl(struct_type: type); |
15030 | |
15031 | if (is_packed) |
15032 | { |
15033 | return type.vecsize * type.columns * (type.width / 8); |
15034 | } |
15035 | else |
15036 | { |
15037 | // An unpacked 3-element vector or matrix column is the same memory size as a 4-element. |
15038 | uint32_t vecsize = type.vecsize; |
15039 | uint32_t columns = type.columns; |
15040 | |
15041 | if (row_major && columns > 1) |
15042 | swap(a&: vecsize, b&: columns); |
15043 | |
15044 | if (vecsize == 3) |
15045 | vecsize = 4; |
15046 | |
15047 | return vecsize * columns * (type.width / 8); |
15048 | } |
15049 | } |
15050 | } |
15051 | } |
15052 | |
15053 | uint32_t CompilerMSL::get_declared_struct_member_size_msl(const SPIRType &type, uint32_t index) const |
15054 | { |
15055 | return get_declared_type_size_msl(type: get_physical_member_type(type, index), |
15056 | is_packed: member_is_packed_physical_type(type, index), |
15057 | row_major: has_member_decoration(id: type.self, index, decoration: DecorationRowMajor)); |
15058 | } |
15059 | |
15060 | uint32_t CompilerMSL::get_declared_input_size_msl(const SPIRType &type, uint32_t index) const |
15061 | { |
15062 | return get_declared_type_size_msl(type: get_presumed_input_type(ib_type: type, index), is_packed: false, |
15063 | row_major: has_member_decoration(id: type.self, index, decoration: DecorationRowMajor)); |
15064 | } |
15065 | |
15066 | // Returns the byte alignment of a type. |
15067 | uint32_t CompilerMSL::get_declared_type_alignment_msl(const SPIRType &type, bool is_packed, bool row_major) const |
15068 | { |
15069 | switch (type.basetype) |
15070 | { |
15071 | case SPIRType::Unknown: |
15072 | case SPIRType::Void: |
15073 | case SPIRType::AtomicCounter: |
15074 | case SPIRType::Image: |
15075 | case SPIRType::SampledImage: |
15076 | case SPIRType::Sampler: |
15077 | SPIRV_CROSS_THROW("Querying alignment of opaque object." ); |
15078 | |
15079 | case SPIRType::Double: |
15080 | SPIRV_CROSS_THROW("double types are not supported in buffers in MSL." ); |
15081 | |
15082 | case SPIRType::Struct: |
15083 | { |
15084 | // In MSL, a struct's alignment is equal to the maximum alignment of any of its members. |
15085 | uint32_t alignment = 1; |
15086 | for (uint32_t i = 0; i < type.member_types.size(); i++) |
15087 | alignment = max(a: alignment, b: uint32_t(get_declared_struct_member_alignment_msl(struct_type: type, index: i))); |
15088 | return alignment; |
15089 | } |
15090 | |
15091 | default: |
15092 | { |
15093 | if (type.basetype == SPIRType::Int64 && !msl_options.supports_msl_version(major: 2, minor: 3)) |
15094 | SPIRV_CROSS_THROW("long types in buffers are only supported in MSL 2.3 and above." ); |
15095 | if (type.basetype == SPIRType::UInt64 && !msl_options.supports_msl_version(major: 2, minor: 3)) |
15096 | SPIRV_CROSS_THROW("ulong types in buffers are only supported in MSL 2.3 and above." ); |
15097 | // Alignment of packed type is the same as the underlying component or column size. |
15098 | // Alignment of unpacked type is the same as the vector size. |
15099 | // Alignment of 3-elements vector is the same as 4-elements (including packed using column). |
15100 | if (is_packed) |
15101 | { |
15102 | // If we have packed_T and friends, the alignment is always scalar. |
15103 | return type.width / 8; |
15104 | } |
15105 | else |
15106 | { |
15107 | // This is the general rule for MSL. Size == alignment. |
15108 | uint32_t vecsize = (row_major && type.columns > 1) ? type.columns : type.vecsize; |
15109 | return (type.width / 8) * (vecsize == 3 ? 4 : vecsize); |
15110 | } |
15111 | } |
15112 | } |
15113 | } |
15114 | |
15115 | uint32_t CompilerMSL::get_declared_struct_member_alignment_msl(const SPIRType &type, uint32_t index) const |
15116 | { |
15117 | return get_declared_type_alignment_msl(type: get_physical_member_type(type, index), |
15118 | is_packed: member_is_packed_physical_type(type, index), |
15119 | row_major: has_member_decoration(id: type.self, index, decoration: DecorationRowMajor)); |
15120 | } |
15121 | |
15122 | uint32_t CompilerMSL::get_declared_input_alignment_msl(const SPIRType &type, uint32_t index) const |
15123 | { |
15124 | return get_declared_type_alignment_msl(type: get_presumed_input_type(ib_type: type, index), is_packed: false, |
15125 | row_major: has_member_decoration(id: type.self, index, decoration: DecorationRowMajor)); |
15126 | } |
15127 | |
15128 | bool CompilerMSL::skip_argument(uint32_t) const |
15129 | { |
15130 | return false; |
15131 | } |
15132 | |
15133 | void CompilerMSL::analyze_sampled_image_usage() |
15134 | { |
15135 | if (msl_options.swizzle_texture_samples) |
15136 | { |
15137 | SampledImageScanner scanner(*this); |
15138 | traverse_all_reachable_opcodes(block: get<SPIRFunction>(id: ir.default_entry_point), handler&: scanner); |
15139 | } |
15140 | } |
15141 | |
15142 | bool CompilerMSL::SampledImageScanner::handle(spv::Op opcode, const uint32_t *args, uint32_t length) |
15143 | { |
15144 | switch (opcode) |
15145 | { |
15146 | case OpLoad: |
15147 | case OpImage: |
15148 | case OpSampledImage: |
15149 | { |
15150 | if (length < 3) |
15151 | return false; |
15152 | |
15153 | uint32_t result_type = args[0]; |
15154 | auto &type = compiler.get<SPIRType>(id: result_type); |
15155 | if ((type.basetype != SPIRType::Image && type.basetype != SPIRType::SampledImage) || type.image.sampled != 1) |
15156 | return true; |
15157 | |
15158 | uint32_t id = args[1]; |
15159 | compiler.set<SPIRExpression>(id, args: "" , args&: result_type, args: true); |
15160 | break; |
15161 | } |
15162 | case OpImageSampleExplicitLod: |
15163 | case OpImageSampleProjExplicitLod: |
15164 | case OpImageSampleDrefExplicitLod: |
15165 | case OpImageSampleProjDrefExplicitLod: |
15166 | case OpImageSampleImplicitLod: |
15167 | case OpImageSampleProjImplicitLod: |
15168 | case OpImageSampleDrefImplicitLod: |
15169 | case OpImageSampleProjDrefImplicitLod: |
15170 | case OpImageFetch: |
15171 | case OpImageGather: |
15172 | case OpImageDrefGather: |
15173 | compiler.has_sampled_images = |
15174 | compiler.has_sampled_images || compiler.is_sampled_image_type(type: compiler.expression_type(id: args[2])); |
15175 | compiler.needs_swizzle_buffer_def = compiler.needs_swizzle_buffer_def || compiler.has_sampled_images; |
15176 | break; |
15177 | default: |
15178 | break; |
15179 | } |
15180 | return true; |
15181 | } |
15182 | |
15183 | // If a needed custom function wasn't added before, add it and force a recompile. |
15184 | void CompilerMSL::add_spv_func_and_recompile(SPVFuncImpl spv_func) |
15185 | { |
15186 | if (spv_function_implementations.count(x: spv_func) == 0) |
15187 | { |
15188 | spv_function_implementations.insert(x: spv_func); |
15189 | suppress_missing_prototypes = true; |
15190 | force_recompile(); |
15191 | } |
15192 | } |
15193 | |
15194 | bool CompilerMSL::OpCodePreprocessor::handle(Op opcode, const uint32_t *args, uint32_t length) |
15195 | { |
15196 | // Since MSL exists in a single execution scope, function prototype declarations are not |
15197 | // needed, and clutter the output. If secondary functions are output (either as a SPIR-V |
15198 | // function implementation or as indicated by the presence of OpFunctionCall), then set |
15199 | // suppress_missing_prototypes to suppress compiler warnings of missing function prototypes. |
15200 | |
15201 | // Mark if the input requires the implementation of an SPIR-V function that does not exist in Metal. |
15202 | SPVFuncImpl spv_func = get_spv_func_impl(opcode, args); |
15203 | if (spv_func != SPVFuncImplNone) |
15204 | { |
15205 | compiler.spv_function_implementations.insert(x: spv_func); |
15206 | suppress_missing_prototypes = true; |
15207 | } |
15208 | |
15209 | switch (opcode) |
15210 | { |
15211 | |
15212 | case OpFunctionCall: |
15213 | suppress_missing_prototypes = true; |
15214 | break; |
15215 | |
15216 | // Emulate texture2D atomic operations |
15217 | case OpImageTexelPointer: |
15218 | { |
15219 | auto *var = compiler.maybe_get_backing_variable(chain: args[2]); |
15220 | image_pointers[args[1]] = var ? var->self : ID(0); |
15221 | break; |
15222 | } |
15223 | |
15224 | case OpImageWrite: |
15225 | if (!compiler.msl_options.supports_msl_version(major: 2, minor: 2)) |
15226 | uses_resource_write = true; |
15227 | break; |
15228 | |
15229 | case OpStore: |
15230 | check_resource_write(var_id: args[0]); |
15231 | break; |
15232 | |
15233 | // Emulate texture2D atomic operations |
15234 | case OpAtomicExchange: |
15235 | case OpAtomicCompareExchange: |
15236 | case OpAtomicCompareExchangeWeak: |
15237 | case OpAtomicIIncrement: |
15238 | case OpAtomicIDecrement: |
15239 | case OpAtomicIAdd: |
15240 | case OpAtomicISub: |
15241 | case OpAtomicSMin: |
15242 | case OpAtomicUMin: |
15243 | case OpAtomicSMax: |
15244 | case OpAtomicUMax: |
15245 | case OpAtomicAnd: |
15246 | case OpAtomicOr: |
15247 | case OpAtomicXor: |
15248 | { |
15249 | uses_atomics = true; |
15250 | auto it = image_pointers.find(x: args[2]); |
15251 | if (it != image_pointers.end()) |
15252 | { |
15253 | compiler.atomic_image_vars.insert(x: it->second); |
15254 | } |
15255 | check_resource_write(var_id: args[2]); |
15256 | break; |
15257 | } |
15258 | |
15259 | case OpAtomicStore: |
15260 | { |
15261 | uses_atomics = true; |
15262 | auto it = image_pointers.find(x: args[0]); |
15263 | if (it != image_pointers.end()) |
15264 | { |
15265 | compiler.atomic_image_vars.insert(x: it->second); |
15266 | } |
15267 | check_resource_write(var_id: args[0]); |
15268 | break; |
15269 | } |
15270 | |
15271 | case OpAtomicLoad: |
15272 | { |
15273 | uses_atomics = true; |
15274 | auto it = image_pointers.find(x: args[2]); |
15275 | if (it != image_pointers.end()) |
15276 | { |
15277 | compiler.atomic_image_vars.insert(x: it->second); |
15278 | } |
15279 | break; |
15280 | } |
15281 | |
15282 | case OpGroupNonUniformInverseBallot: |
15283 | needs_subgroup_invocation_id = true; |
15284 | break; |
15285 | |
15286 | case OpGroupNonUniformBallotFindLSB: |
15287 | case OpGroupNonUniformBallotFindMSB: |
15288 | needs_subgroup_size = true; |
15289 | break; |
15290 | |
15291 | case OpGroupNonUniformBallotBitCount: |
15292 | if (args[3] == GroupOperationReduce) |
15293 | needs_subgroup_size = true; |
15294 | else |
15295 | needs_subgroup_invocation_id = true; |
15296 | break; |
15297 | |
15298 | case OpArrayLength: |
15299 | { |
15300 | auto *var = compiler.maybe_get_backing_variable(chain: args[2]); |
15301 | if (var) |
15302 | compiler.buffers_requiring_array_length.insert(x: var->self); |
15303 | break; |
15304 | } |
15305 | |
15306 | case OpInBoundsAccessChain: |
15307 | case OpAccessChain: |
15308 | case OpPtrAccessChain: |
15309 | { |
15310 | // OpArrayLength might want to know if taking ArrayLength of an array of SSBOs. |
15311 | uint32_t result_type = args[0]; |
15312 | uint32_t id = args[1]; |
15313 | uint32_t ptr = args[2]; |
15314 | |
15315 | compiler.set<SPIRExpression>(id, args: "" , args&: result_type, args: true); |
15316 | compiler.register_read(expr: id, chain: ptr, forwarded: true); |
15317 | compiler.ir.ids[id].set_allow_type_rewrite(); |
15318 | break; |
15319 | } |
15320 | |
15321 | case OpExtInst: |
15322 | { |
15323 | uint32_t extension_set = args[2]; |
15324 | if (compiler.get<SPIRExtension>(id: extension_set).ext == SPIRExtension::GLSL) |
15325 | { |
15326 | auto op_450 = static_cast<GLSLstd450>(args[3]); |
15327 | switch (op_450) |
15328 | { |
15329 | case GLSLstd450InterpolateAtCentroid: |
15330 | case GLSLstd450InterpolateAtSample: |
15331 | case GLSLstd450InterpolateAtOffset: |
15332 | { |
15333 | if (!compiler.msl_options.supports_msl_version(major: 2, minor: 3)) |
15334 | SPIRV_CROSS_THROW("Pull-model interpolation requires MSL 2.3." ); |
15335 | // Fragment varyings used with pull-model interpolation need special handling, |
15336 | // due to the way pull-model interpolation works in Metal. |
15337 | auto *var = compiler.maybe_get_backing_variable(chain: args[4]); |
15338 | if (var) |
15339 | { |
15340 | compiler.pull_model_inputs.insert(x: var->self); |
15341 | auto &var_type = compiler.get_variable_element_type(var: *var); |
15342 | // In addition, if this variable has a 'Sample' decoration, we need the sample ID |
15343 | // in order to do default interpolation. |
15344 | if (compiler.has_decoration(id: var->self, decoration: DecorationSample)) |
15345 | { |
15346 | needs_sample_id = true; |
15347 | } |
15348 | else if (var_type.basetype == SPIRType::Struct) |
15349 | { |
15350 | // Now we need to check each member and see if it has this decoration. |
15351 | for (uint32_t i = 0; i < var_type.member_types.size(); ++i) |
15352 | { |
15353 | if (compiler.has_member_decoration(id: var_type.self, index: i, decoration: DecorationSample)) |
15354 | { |
15355 | needs_sample_id = true; |
15356 | break; |
15357 | } |
15358 | } |
15359 | } |
15360 | } |
15361 | break; |
15362 | } |
15363 | default: |
15364 | break; |
15365 | } |
15366 | } |
15367 | break; |
15368 | } |
15369 | |
15370 | default: |
15371 | break; |
15372 | } |
15373 | |
15374 | // If it has one, keep track of the instruction's result type, mapped by ID |
15375 | uint32_t result_type, result_id; |
15376 | if (compiler.instruction_to_result_type(result_type, result_id, op: opcode, args, length)) |
15377 | result_types[result_id] = result_type; |
15378 | |
15379 | return true; |
15380 | } |
15381 | |
15382 | // If the variable is a Uniform or StorageBuffer, mark that a resource has been written to. |
15383 | void CompilerMSL::OpCodePreprocessor::check_resource_write(uint32_t var_id) |
15384 | { |
15385 | auto *p_var = compiler.maybe_get_backing_variable(chain: var_id); |
15386 | StorageClass sc = p_var ? p_var->storage : StorageClassMax; |
15387 | if (!compiler.msl_options.supports_msl_version(major: 2, minor: 1) && |
15388 | (sc == StorageClassUniform || sc == StorageClassStorageBuffer)) |
15389 | uses_resource_write = true; |
15390 | } |
15391 | |
15392 | // Returns an enumeration of a SPIR-V function that needs to be output for certain Op codes. |
15393 | CompilerMSL::SPVFuncImpl CompilerMSL::OpCodePreprocessor::get_spv_func_impl(Op opcode, const uint32_t *args) |
15394 | { |
15395 | switch (opcode) |
15396 | { |
15397 | case OpFMod: |
15398 | return SPVFuncImplMod; |
15399 | |
15400 | case OpFAdd: |
15401 | case OpFSub: |
15402 | if (compiler.msl_options.invariant_float_math || |
15403 | compiler.has_decoration(id: args[1], decoration: DecorationNoContraction)) |
15404 | { |
15405 | return opcode == OpFAdd ? SPVFuncImplFAdd : SPVFuncImplFSub; |
15406 | } |
15407 | break; |
15408 | |
15409 | case OpFMul: |
15410 | case OpOuterProduct: |
15411 | case OpMatrixTimesVector: |
15412 | case OpVectorTimesMatrix: |
15413 | case OpMatrixTimesMatrix: |
15414 | if (compiler.msl_options.invariant_float_math || |
15415 | compiler.has_decoration(id: args[1], decoration: DecorationNoContraction)) |
15416 | { |
15417 | return SPVFuncImplFMul; |
15418 | } |
15419 | break; |
15420 | |
15421 | case OpQuantizeToF16: |
15422 | return SPVFuncImplQuantizeToF16; |
15423 | |
15424 | case OpTypeArray: |
15425 | { |
15426 | // Allow Metal to use the array<T> template to make arrays a value type |
15427 | return SPVFuncImplUnsafeArray; |
15428 | } |
15429 | |
15430 | // Emulate texture2D atomic operations |
15431 | case OpAtomicExchange: |
15432 | case OpAtomicCompareExchange: |
15433 | case OpAtomicCompareExchangeWeak: |
15434 | case OpAtomicIIncrement: |
15435 | case OpAtomicIDecrement: |
15436 | case OpAtomicIAdd: |
15437 | case OpAtomicISub: |
15438 | case OpAtomicSMin: |
15439 | case OpAtomicUMin: |
15440 | case OpAtomicSMax: |
15441 | case OpAtomicUMax: |
15442 | case OpAtomicAnd: |
15443 | case OpAtomicOr: |
15444 | case OpAtomicXor: |
15445 | case OpAtomicLoad: |
15446 | case OpAtomicStore: |
15447 | { |
15448 | auto it = image_pointers.find(x: args[opcode == OpAtomicStore ? 0 : 2]); |
15449 | if (it != image_pointers.end()) |
15450 | { |
15451 | uint32_t tid = compiler.get<SPIRVariable>(id: it->second).basetype; |
15452 | if (tid && compiler.get<SPIRType>(id: tid).image.dim == Dim2D) |
15453 | return SPVFuncImplImage2DAtomicCoords; |
15454 | } |
15455 | break; |
15456 | } |
15457 | |
15458 | case OpImageFetch: |
15459 | case OpImageRead: |
15460 | case OpImageWrite: |
15461 | { |
15462 | // Retrieve the image type, and if it's a Buffer, emit a texel coordinate function |
15463 | uint32_t tid = result_types[args[opcode == OpImageWrite ? 0 : 2]]; |
15464 | if (tid && compiler.get<SPIRType>(id: tid).image.dim == DimBuffer && !compiler.msl_options.texture_buffer_native) |
15465 | return SPVFuncImplTexelBufferCoords; |
15466 | break; |
15467 | } |
15468 | |
15469 | case OpExtInst: |
15470 | { |
15471 | uint32_t extension_set = args[2]; |
15472 | if (compiler.get<SPIRExtension>(id: extension_set).ext == SPIRExtension::GLSL) |
15473 | { |
15474 | auto op_450 = static_cast<GLSLstd450>(args[3]); |
15475 | switch (op_450) |
15476 | { |
15477 | case GLSLstd450Radians: |
15478 | return SPVFuncImplRadians; |
15479 | case GLSLstd450Degrees: |
15480 | return SPVFuncImplDegrees; |
15481 | case GLSLstd450FindILsb: |
15482 | return SPVFuncImplFindILsb; |
15483 | case GLSLstd450FindSMsb: |
15484 | return SPVFuncImplFindSMsb; |
15485 | case GLSLstd450FindUMsb: |
15486 | return SPVFuncImplFindUMsb; |
15487 | case GLSLstd450SSign: |
15488 | return SPVFuncImplSSign; |
15489 | case GLSLstd450Reflect: |
15490 | { |
15491 | auto &type = compiler.get<SPIRType>(id: args[0]); |
15492 | if (type.vecsize == 1) |
15493 | return SPVFuncImplReflectScalar; |
15494 | break; |
15495 | } |
15496 | case GLSLstd450Refract: |
15497 | { |
15498 | auto &type = compiler.get<SPIRType>(id: args[0]); |
15499 | if (type.vecsize == 1) |
15500 | return SPVFuncImplRefractScalar; |
15501 | break; |
15502 | } |
15503 | case GLSLstd450FaceForward: |
15504 | { |
15505 | auto &type = compiler.get<SPIRType>(id: args[0]); |
15506 | if (type.vecsize == 1) |
15507 | return SPVFuncImplFaceForwardScalar; |
15508 | break; |
15509 | } |
15510 | case GLSLstd450MatrixInverse: |
15511 | { |
15512 | auto &mat_type = compiler.get<SPIRType>(id: args[0]); |
15513 | switch (mat_type.columns) |
15514 | { |
15515 | case 2: |
15516 | return SPVFuncImplInverse2x2; |
15517 | case 3: |
15518 | return SPVFuncImplInverse3x3; |
15519 | case 4: |
15520 | return SPVFuncImplInverse4x4; |
15521 | default: |
15522 | break; |
15523 | } |
15524 | break; |
15525 | } |
15526 | default: |
15527 | break; |
15528 | } |
15529 | } |
15530 | break; |
15531 | } |
15532 | |
15533 | case OpGroupNonUniformBroadcast: |
15534 | return SPVFuncImplSubgroupBroadcast; |
15535 | |
15536 | case OpGroupNonUniformBroadcastFirst: |
15537 | return SPVFuncImplSubgroupBroadcastFirst; |
15538 | |
15539 | case OpGroupNonUniformBallot: |
15540 | return SPVFuncImplSubgroupBallot; |
15541 | |
15542 | case OpGroupNonUniformInverseBallot: |
15543 | case OpGroupNonUniformBallotBitExtract: |
15544 | return SPVFuncImplSubgroupBallotBitExtract; |
15545 | |
15546 | case OpGroupNonUniformBallotFindLSB: |
15547 | return SPVFuncImplSubgroupBallotFindLSB; |
15548 | |
15549 | case OpGroupNonUniformBallotFindMSB: |
15550 | return SPVFuncImplSubgroupBallotFindMSB; |
15551 | |
15552 | case OpGroupNonUniformBallotBitCount: |
15553 | return SPVFuncImplSubgroupBallotBitCount; |
15554 | |
15555 | case OpGroupNonUniformAllEqual: |
15556 | return SPVFuncImplSubgroupAllEqual; |
15557 | |
15558 | case OpGroupNonUniformShuffle: |
15559 | return SPVFuncImplSubgroupShuffle; |
15560 | |
15561 | case OpGroupNonUniformShuffleXor: |
15562 | return SPVFuncImplSubgroupShuffleXor; |
15563 | |
15564 | case OpGroupNonUniformShuffleUp: |
15565 | return SPVFuncImplSubgroupShuffleUp; |
15566 | |
15567 | case OpGroupNonUniformShuffleDown: |
15568 | return SPVFuncImplSubgroupShuffleDown; |
15569 | |
15570 | case OpGroupNonUniformQuadBroadcast: |
15571 | return SPVFuncImplQuadBroadcast; |
15572 | |
15573 | case OpGroupNonUniformQuadSwap: |
15574 | return SPVFuncImplQuadSwap; |
15575 | |
15576 | default: |
15577 | break; |
15578 | } |
15579 | return SPVFuncImplNone; |
15580 | } |
15581 | |
15582 | // Sort both type and meta member content based on builtin status (put builtins at end), |
15583 | // then by the required sorting aspect. |
15584 | void CompilerMSL::MemberSorter::sort() |
15585 | { |
15586 | // Create a temporary array of consecutive member indices and sort it based on how |
15587 | // the members should be reordered, based on builtin and sorting aspect meta info. |
15588 | size_t mbr_cnt = type.member_types.size(); |
15589 | SmallVector<uint32_t> mbr_idxs(mbr_cnt); |
15590 | std::iota(first: mbr_idxs.begin(), last: mbr_idxs.end(), value: 0); // Fill with consecutive indices |
15591 | std::stable_sort(first: mbr_idxs.begin(), last: mbr_idxs.end(), comp: *this); // Sort member indices based on sorting aspect |
15592 | |
15593 | bool sort_is_identity = true; |
15594 | for (uint32_t mbr_idx = 0; mbr_idx < mbr_cnt; mbr_idx++) |
15595 | { |
15596 | if (mbr_idx != mbr_idxs[mbr_idx]) |
15597 | { |
15598 | sort_is_identity = false; |
15599 | break; |
15600 | } |
15601 | } |
15602 | |
15603 | if (sort_is_identity) |
15604 | return; |
15605 | |
15606 | if (meta.members.size() < type.member_types.size()) |
15607 | { |
15608 | // This should never trigger in normal circumstances, but to be safe. |
15609 | meta.members.resize(new_size: type.member_types.size()); |
15610 | } |
15611 | |
15612 | // Move type and meta member info to the order defined by the sorted member indices. |
15613 | // This is done by creating temporary copies of both member types and meta, and then |
15614 | // copying back to the original content at the sorted indices. |
15615 | auto mbr_types_cpy = type.member_types; |
15616 | auto mbr_meta_cpy = meta.members; |
15617 | for (uint32_t mbr_idx = 0; mbr_idx < mbr_cnt; mbr_idx++) |
15618 | { |
15619 | type.member_types[mbr_idx] = mbr_types_cpy[mbr_idxs[mbr_idx]]; |
15620 | meta.members[mbr_idx] = mbr_meta_cpy[mbr_idxs[mbr_idx]]; |
15621 | } |
15622 | |
15623 | // If we're sorting by Offset, this might affect user code which accesses a buffer block. |
15624 | // We will need to redirect member indices from defined index to sorted index using reverse lookup. |
15625 | if (sort_aspect == SortAspect::Offset) |
15626 | { |
15627 | type.member_type_index_redirection.resize(new_size: mbr_cnt); |
15628 | for (uint32_t map_idx = 0; map_idx < mbr_cnt; map_idx++) |
15629 | type.member_type_index_redirection[mbr_idxs[map_idx]] = map_idx; |
15630 | } |
15631 | } |
15632 | |
15633 | bool CompilerMSL::MemberSorter::operator()(uint32_t mbr_idx1, uint32_t mbr_idx2) |
15634 | { |
15635 | auto &mbr_meta1 = meta.members[mbr_idx1]; |
15636 | auto &mbr_meta2 = meta.members[mbr_idx2]; |
15637 | |
15638 | if (sort_aspect == LocationThenBuiltInType) |
15639 | { |
15640 | // Sort first by builtin status (put builtins at end), then by the sorting aspect. |
15641 | if (mbr_meta1.builtin != mbr_meta2.builtin) |
15642 | return mbr_meta2.builtin; |
15643 | else if (mbr_meta1.builtin) |
15644 | return mbr_meta1.builtin_type < mbr_meta2.builtin_type; |
15645 | else if (mbr_meta1.location == mbr_meta2.location) |
15646 | return mbr_meta1.component < mbr_meta2.component; |
15647 | else |
15648 | return mbr_meta1.location < mbr_meta2.location; |
15649 | } |
15650 | else |
15651 | return mbr_meta1.offset < mbr_meta2.offset; |
15652 | } |
15653 | |
15654 | CompilerMSL::MemberSorter::MemberSorter(SPIRType &t, Meta &m, SortAspect sa) |
15655 | : type(t) |
15656 | , meta(m) |
15657 | , sort_aspect(sa) |
15658 | { |
15659 | // Ensure enough meta info is available |
15660 | meta.members.resize(new_size: max(a: type.member_types.size(), b: meta.members.size())); |
15661 | } |
15662 | |
15663 | void CompilerMSL::remap_constexpr_sampler(VariableID id, const MSLConstexprSampler &sampler) |
15664 | { |
15665 | auto &type = get<SPIRType>(id: get<SPIRVariable>(id).basetype); |
15666 | if (type.basetype != SPIRType::SampledImage && type.basetype != SPIRType::Sampler) |
15667 | SPIRV_CROSS_THROW("Can only remap SampledImage and Sampler type." ); |
15668 | if (!type.array.empty()) |
15669 | SPIRV_CROSS_THROW("Can not remap array of samplers." ); |
15670 | constexpr_samplers_by_id[id] = sampler; |
15671 | } |
15672 | |
15673 | void CompilerMSL::remap_constexpr_sampler_by_binding(uint32_t desc_set, uint32_t binding, |
15674 | const MSLConstexprSampler &sampler) |
15675 | { |
15676 | constexpr_samplers_by_binding[{ .desc_set: desc_set, .binding: binding }] = sampler; |
15677 | } |
15678 | |
15679 | void CompilerMSL::cast_from_variable_load(uint32_t source_id, std::string &expr, const SPIRType &expr_type) |
15680 | { |
15681 | auto *var = maybe_get_backing_variable(chain: source_id); |
15682 | if (var) |
15683 | source_id = var->self; |
15684 | |
15685 | // Type fixups for workgroup variables if they are booleans. |
15686 | if (var && var->storage == StorageClassWorkgroup && expr_type.basetype == SPIRType::Boolean) |
15687 | expr = join(ts: type_to_glsl(type: expr_type), ts: "(" , ts&: expr, ts: ")" ); |
15688 | |
15689 | // Only interested in standalone builtin variables in the switch below. |
15690 | if (!has_decoration(id: source_id, decoration: DecorationBuiltIn)) |
15691 | { |
15692 | // If the backing variable does not match our expected sign, we can fix it up here. |
15693 | // See ensure_correct_input_type(). |
15694 | if (var && var->storage == StorageClassInput) |
15695 | { |
15696 | auto &base_type = get<SPIRType>(id: var->basetype); |
15697 | if (base_type.basetype != SPIRType::Struct && expr_type.basetype != base_type.basetype) |
15698 | expr = join(ts: type_to_glsl(type: expr_type), ts: "(" , ts&: expr, ts: ")" ); |
15699 | } |
15700 | return; |
15701 | } |
15702 | |
15703 | auto builtin = static_cast<BuiltIn>(get_decoration(id: source_id, decoration: DecorationBuiltIn)); |
15704 | auto expected_type = expr_type.basetype; |
15705 | auto expected_width = expr_type.width; |
15706 | switch (builtin) |
15707 | { |
15708 | case BuiltInGlobalInvocationId: |
15709 | case BuiltInLocalInvocationId: |
15710 | case BuiltInWorkgroupId: |
15711 | case BuiltInLocalInvocationIndex: |
15712 | case BuiltInWorkgroupSize: |
15713 | case BuiltInNumWorkgroups: |
15714 | case BuiltInLayer: |
15715 | case BuiltInViewportIndex: |
15716 | case BuiltInFragStencilRefEXT: |
15717 | case BuiltInPrimitiveId: |
15718 | case BuiltInSubgroupSize: |
15719 | case BuiltInSubgroupLocalInvocationId: |
15720 | case BuiltInViewIndex: |
15721 | case BuiltInVertexIndex: |
15722 | case BuiltInInstanceIndex: |
15723 | case BuiltInBaseInstance: |
15724 | case BuiltInBaseVertex: |
15725 | expected_type = SPIRType::UInt; |
15726 | expected_width = 32; |
15727 | break; |
15728 | |
15729 | case BuiltInTessLevelInner: |
15730 | case BuiltInTessLevelOuter: |
15731 | if (get_execution_model() == ExecutionModelTessellationControl) |
15732 | { |
15733 | expected_type = SPIRType::Half; |
15734 | expected_width = 16; |
15735 | } |
15736 | break; |
15737 | |
15738 | default: |
15739 | break; |
15740 | } |
15741 | |
15742 | if (expected_type != expr_type.basetype) |
15743 | { |
15744 | if (!expr_type.array.empty() && (builtin == BuiltInTessLevelInner || builtin == BuiltInTessLevelOuter)) |
15745 | { |
15746 | // Triggers when loading TessLevel directly as an array. |
15747 | // Need explicit padding + cast. |
15748 | auto wrap_expr = join(ts: type_to_glsl(type: expr_type), ts: "({ " ); |
15749 | |
15750 | uint32_t array_size = get_physical_tess_level_array_size(builtin); |
15751 | for (uint32_t i = 0; i < array_size; i++) |
15752 | { |
15753 | if (array_size > 1) |
15754 | wrap_expr += join(ts: "float(" , ts&: expr, ts: "[" , ts&: i, ts: "])" ); |
15755 | else |
15756 | wrap_expr += join(ts: "float(" , ts&: expr, ts: ")" ); |
15757 | if (i + 1 < array_size) |
15758 | wrap_expr += ", " ; |
15759 | } |
15760 | |
15761 | if (get_execution_mode_bitset().get(bit: ExecutionModeTriangles)) |
15762 | wrap_expr += ", 0.0" ; |
15763 | |
15764 | wrap_expr += " })" ; |
15765 | expr = std::move(wrap_expr); |
15766 | } |
15767 | else |
15768 | { |
15769 | // These are of different widths, so we cannot do a straight bitcast. |
15770 | if (expected_width != expr_type.width) |
15771 | expr = join(ts: type_to_glsl(type: expr_type), ts: "(" , ts&: expr, ts: ")" ); |
15772 | else |
15773 | expr = bitcast_expression(target_type: expr_type, expr_type: expected_type, expr); |
15774 | } |
15775 | } |
15776 | } |
15777 | |
15778 | void CompilerMSL::cast_to_variable_store(uint32_t target_id, std::string &expr, const SPIRType &expr_type) |
15779 | { |
15780 | auto *var = maybe_get_backing_variable(chain: target_id); |
15781 | if (var) |
15782 | target_id = var->self; |
15783 | |
15784 | // Type fixups for workgroup variables if they are booleans. |
15785 | if (var && var->storage == StorageClassWorkgroup && expr_type.basetype == SPIRType::Boolean) |
15786 | { |
15787 | auto short_type = expr_type; |
15788 | short_type.basetype = SPIRType::Short; |
15789 | expr = join(ts: type_to_glsl(type: short_type), ts: "(" , ts&: expr, ts: ")" ); |
15790 | } |
15791 | |
15792 | // Only interested in standalone builtin variables. |
15793 | if (!has_decoration(id: target_id, decoration: DecorationBuiltIn)) |
15794 | return; |
15795 | |
15796 | auto builtin = static_cast<BuiltIn>(get_decoration(id: target_id, decoration: DecorationBuiltIn)); |
15797 | auto expected_type = expr_type.basetype; |
15798 | auto expected_width = expr_type.width; |
15799 | switch (builtin) |
15800 | { |
15801 | case BuiltInLayer: |
15802 | case BuiltInViewportIndex: |
15803 | case BuiltInFragStencilRefEXT: |
15804 | case BuiltInPrimitiveId: |
15805 | case BuiltInViewIndex: |
15806 | expected_type = SPIRType::UInt; |
15807 | expected_width = 32; |
15808 | break; |
15809 | |
15810 | case BuiltInTessLevelInner: |
15811 | case BuiltInTessLevelOuter: |
15812 | expected_type = SPIRType::Half; |
15813 | expected_width = 16; |
15814 | break; |
15815 | |
15816 | default: |
15817 | break; |
15818 | } |
15819 | |
15820 | if (expected_type != expr_type.basetype) |
15821 | { |
15822 | if (expected_width != expr_type.width) |
15823 | { |
15824 | // These are of different widths, so we cannot do a straight bitcast. |
15825 | auto type = expr_type; |
15826 | type.basetype = expected_type; |
15827 | type.width = expected_width; |
15828 | expr = join(ts: type_to_glsl(type), ts: "(" , ts&: expr, ts: ")" ); |
15829 | } |
15830 | else |
15831 | { |
15832 | auto type = expr_type; |
15833 | type.basetype = expected_type; |
15834 | expr = bitcast_expression(target_type: type, expr_type: expr_type.basetype, expr); |
15835 | } |
15836 | } |
15837 | } |
15838 | |
15839 | string CompilerMSL::to_initializer_expression(const SPIRVariable &var) |
15840 | { |
15841 | // We risk getting an array initializer here with MSL. If we have an array. |
15842 | // FIXME: We cannot handle non-constant arrays being initialized. |
15843 | // We will need to inject spvArrayCopy here somehow ... |
15844 | auto &type = get<SPIRType>(id: var.basetype); |
15845 | string expr; |
15846 | if (ir.ids[var.initializer].get_type() == TypeConstant && |
15847 | (!type.array.empty() || type.basetype == SPIRType::Struct)) |
15848 | expr = constant_expression(c: get<SPIRConstant>(id: var.initializer)); |
15849 | else |
15850 | expr = CompilerGLSL::to_initializer_expression(var); |
15851 | // If the initializer has more vector components than the variable, add a swizzle. |
15852 | // FIXME: This can't handle arrays or structs. |
15853 | auto &init_type = expression_type(id: var.initializer); |
15854 | if (type.array.empty() && type.basetype != SPIRType::Struct && init_type.vecsize > type.vecsize) |
15855 | expr = enclose_expression(expr: expr + vector_swizzle(vecsize: type.vecsize, index: 0)); |
15856 | return expr; |
15857 | } |
15858 | |
15859 | string CompilerMSL::to_zero_initialized_expression(uint32_t) |
15860 | { |
15861 | return "{}" ; |
15862 | } |
15863 | |
15864 | bool CompilerMSL::descriptor_set_is_argument_buffer(uint32_t desc_set) const |
15865 | { |
15866 | if (!msl_options.argument_buffers) |
15867 | return false; |
15868 | if (desc_set >= kMaxArgumentBuffers) |
15869 | return false; |
15870 | |
15871 | return (argument_buffer_discrete_mask & (1u << desc_set)) == 0; |
15872 | } |
15873 | |
15874 | bool CompilerMSL::is_supported_argument_buffer_type(const SPIRType &type) const |
15875 | { |
15876 | // Very specifically, image load-store in argument buffers are disallowed on MSL on iOS. |
15877 | // But we won't know when the argument buffer is encoded whether this image will have |
15878 | // a NonWritable decoration. So just use discrete arguments for all storage images |
15879 | // on iOS. |
15880 | bool is_storage_image = type.basetype == SPIRType::Image && type.image.sampled == 2; |
15881 | bool is_supported_type = !msl_options.is_ios() || !is_storage_image; |
15882 | return !type_is_msl_framebuffer_fetch(type) && is_supported_type; |
15883 | } |
15884 | |
15885 | void CompilerMSL::analyze_argument_buffers() |
15886 | { |
15887 | // Gather all used resources and sort them out into argument buffers. |
15888 | // Each argument buffer corresponds to a descriptor set in SPIR-V. |
15889 | // The [[id(N)]] values used correspond to the resource mapping we have for MSL. |
15890 | // Otherwise, the binding number is used, but this is generally not safe some types like |
15891 | // combined image samplers and arrays of resources. Metal needs different indices here, |
15892 | // while SPIR-V can have one descriptor set binding. To use argument buffers in practice, |
15893 | // you will need to use the remapping from the API. |
15894 | for (auto &id : argument_buffer_ids) |
15895 | id = 0; |
15896 | |
15897 | // Output resources, sorted by resource index & type. |
15898 | struct Resource |
15899 | { |
15900 | SPIRVariable *var; |
15901 | string name; |
15902 | SPIRType::BaseType basetype; |
15903 | uint32_t index; |
15904 | uint32_t plane; |
15905 | }; |
15906 | SmallVector<Resource> resources_in_set[kMaxArgumentBuffers]; |
15907 | SmallVector<uint32_t> inline_block_vars; |
15908 | |
15909 | bool set_needs_swizzle_buffer[kMaxArgumentBuffers] = {}; |
15910 | bool set_needs_buffer_sizes[kMaxArgumentBuffers] = {}; |
15911 | bool needs_buffer_sizes = false; |
15912 | |
15913 | ir.for_each_typed_id<SPIRVariable>(op: [&](uint32_t self, SPIRVariable &var) { |
15914 | if ((var.storage == StorageClassUniform || var.storage == StorageClassUniformConstant || |
15915 | var.storage == StorageClassStorageBuffer) && |
15916 | !is_hidden_variable(var)) |
15917 | { |
15918 | uint32_t desc_set = get_decoration(id: self, decoration: DecorationDescriptorSet); |
15919 | // Ignore if it's part of a push descriptor set. |
15920 | if (!descriptor_set_is_argument_buffer(desc_set)) |
15921 | return; |
15922 | |
15923 | uint32_t var_id = var.self; |
15924 | auto &type = get_variable_data_type(var); |
15925 | |
15926 | if (desc_set >= kMaxArgumentBuffers) |
15927 | SPIRV_CROSS_THROW("Descriptor set index is out of range." ); |
15928 | |
15929 | const MSLConstexprSampler *constexpr_sampler = nullptr; |
15930 | if (type.basetype == SPIRType::SampledImage || type.basetype == SPIRType::Sampler) |
15931 | { |
15932 | constexpr_sampler = find_constexpr_sampler(id: var_id); |
15933 | if (constexpr_sampler) |
15934 | { |
15935 | // Mark this ID as a constexpr sampler for later in case it came from set/bindings. |
15936 | constexpr_samplers_by_id[var_id] = *constexpr_sampler; |
15937 | } |
15938 | } |
15939 | |
15940 | uint32_t binding = get_decoration(id: var_id, decoration: DecorationBinding); |
15941 | if (type.basetype == SPIRType::SampledImage) |
15942 | { |
15943 | add_resource_name(id: var_id); |
15944 | |
15945 | uint32_t plane_count = 1; |
15946 | if (constexpr_sampler && constexpr_sampler->ycbcr_conversion_enable) |
15947 | plane_count = constexpr_sampler->planes; |
15948 | |
15949 | for (uint32_t i = 0; i < plane_count; i++) |
15950 | { |
15951 | uint32_t image_resource_index = get_metal_resource_index(var, basetype: SPIRType::Image, plane: i); |
15952 | resources_in_set[desc_set].push_back( |
15953 | t: { .var: &var, .name: to_name(id: var_id), .basetype: SPIRType::Image, .index: image_resource_index, .plane: i }); |
15954 | } |
15955 | |
15956 | if (type.image.dim != DimBuffer && !constexpr_sampler) |
15957 | { |
15958 | uint32_t sampler_resource_index = get_metal_resource_index(var, basetype: SPIRType::Sampler); |
15959 | resources_in_set[desc_set].push_back( |
15960 | t: { .var: &var, .name: to_sampler_expression(id: var_id), .basetype: SPIRType::Sampler, .index: sampler_resource_index, .plane: 0 }); |
15961 | } |
15962 | } |
15963 | else if (inline_uniform_blocks.count(x: SetBindingPair{ .desc_set: desc_set, .binding: binding })) |
15964 | { |
15965 | inline_block_vars.push_back(t: var_id); |
15966 | } |
15967 | else if (!constexpr_sampler && is_supported_argument_buffer_type(type)) |
15968 | { |
15969 | // constexpr samplers are not declared as resources. |
15970 | // Inline uniform blocks are always emitted at the end. |
15971 | add_resource_name(id: var_id); |
15972 | resources_in_set[desc_set].push_back( |
15973 | t: { .var: &var, .name: to_name(id: var_id), .basetype: type.basetype, .index: get_metal_resource_index(var, basetype: type.basetype), .plane: 0 }); |
15974 | |
15975 | // Emulate texture2D atomic operations |
15976 | if (atomic_image_vars.count(x: var.self)) |
15977 | { |
15978 | uint32_t buffer_resource_index = get_metal_resource_index(var, basetype: SPIRType::AtomicCounter, plane: 0); |
15979 | resources_in_set[desc_set].push_back( |
15980 | t: { .var: &var, .name: to_name(id: var_id) + "_atomic" , .basetype: SPIRType::Struct, .index: buffer_resource_index, .plane: 0 }); |
15981 | } |
15982 | } |
15983 | |
15984 | // Check if this descriptor set needs a swizzle buffer. |
15985 | if (needs_swizzle_buffer_def && is_sampled_image_type(type)) |
15986 | set_needs_swizzle_buffer[desc_set] = true; |
15987 | else if (buffers_requiring_array_length.count(x: var_id) != 0) |
15988 | { |
15989 | set_needs_buffer_sizes[desc_set] = true; |
15990 | needs_buffer_sizes = true; |
15991 | } |
15992 | } |
15993 | }); |
15994 | |
15995 | if (needs_swizzle_buffer_def || needs_buffer_sizes) |
15996 | { |
15997 | uint32_t uint_ptr_type_id = 0; |
15998 | |
15999 | // We might have to add a swizzle buffer resource to the set. |
16000 | for (uint32_t desc_set = 0; desc_set < kMaxArgumentBuffers; desc_set++) |
16001 | { |
16002 | if (!set_needs_swizzle_buffer[desc_set] && !set_needs_buffer_sizes[desc_set]) |
16003 | continue; |
16004 | |
16005 | if (uint_ptr_type_id == 0) |
16006 | { |
16007 | uint_ptr_type_id = ir.increase_bound_by(count: 1); |
16008 | |
16009 | // Create a buffer to hold extra data, including the swizzle constants. |
16010 | SPIRType uint_type_pointer = get_uint_type(); |
16011 | uint_type_pointer.pointer = true; |
16012 | uint_type_pointer.pointer_depth++; |
16013 | uint_type_pointer.parent_type = get_uint_type_id(); |
16014 | uint_type_pointer.storage = StorageClassUniform; |
16015 | set<SPIRType>(id: uint_ptr_type_id, args&: uint_type_pointer); |
16016 | set_decoration(id: uint_ptr_type_id, decoration: DecorationArrayStride, argument: 4); |
16017 | } |
16018 | |
16019 | if (set_needs_swizzle_buffer[desc_set]) |
16020 | { |
16021 | uint32_t var_id = ir.increase_bound_by(count: 1); |
16022 | auto &var = set<SPIRVariable>(id: var_id, args&: uint_ptr_type_id, args: StorageClassUniformConstant); |
16023 | set_name(id: var_id, name: "spvSwizzleConstants" ); |
16024 | set_decoration(id: var_id, decoration: DecorationDescriptorSet, argument: desc_set); |
16025 | set_decoration(id: var_id, decoration: DecorationBinding, argument: kSwizzleBufferBinding); |
16026 | resources_in_set[desc_set].push_back( |
16027 | t: { .var: &var, .name: to_name(id: var_id), .basetype: SPIRType::UInt, .index: get_metal_resource_index(var, basetype: SPIRType::UInt), .plane: 0 }); |
16028 | } |
16029 | |
16030 | if (set_needs_buffer_sizes[desc_set]) |
16031 | { |
16032 | uint32_t var_id = ir.increase_bound_by(count: 1); |
16033 | auto &var = set<SPIRVariable>(id: var_id, args&: uint_ptr_type_id, args: StorageClassUniformConstant); |
16034 | set_name(id: var_id, name: "spvBufferSizeConstants" ); |
16035 | set_decoration(id: var_id, decoration: DecorationDescriptorSet, argument: desc_set); |
16036 | set_decoration(id: var_id, decoration: DecorationBinding, argument: kBufferSizeBufferBinding); |
16037 | resources_in_set[desc_set].push_back( |
16038 | t: { .var: &var, .name: to_name(id: var_id), .basetype: SPIRType::UInt, .index: get_metal_resource_index(var, basetype: SPIRType::UInt), .plane: 0 }); |
16039 | } |
16040 | } |
16041 | } |
16042 | |
16043 | // Now add inline uniform blocks. |
16044 | for (uint32_t var_id : inline_block_vars) |
16045 | { |
16046 | auto &var = get<SPIRVariable>(id: var_id); |
16047 | uint32_t desc_set = get_decoration(id: var_id, decoration: DecorationDescriptorSet); |
16048 | add_resource_name(id: var_id); |
16049 | resources_in_set[desc_set].push_back( |
16050 | t: { .var: &var, .name: to_name(id: var_id), .basetype: SPIRType::Struct, .index: get_metal_resource_index(var, basetype: SPIRType::Struct), .plane: 0 }); |
16051 | } |
16052 | |
16053 | for (uint32_t desc_set = 0; desc_set < kMaxArgumentBuffers; desc_set++) |
16054 | { |
16055 | auto &resources = resources_in_set[desc_set]; |
16056 | if (resources.empty()) |
16057 | continue; |
16058 | |
16059 | assert(descriptor_set_is_argument_buffer(desc_set)); |
16060 | |
16061 | uint32_t next_id = ir.increase_bound_by(count: 3); |
16062 | uint32_t type_id = next_id + 1; |
16063 | uint32_t ptr_type_id = next_id + 2; |
16064 | argument_buffer_ids[desc_set] = next_id; |
16065 | |
16066 | auto &buffer_type = set<SPIRType>(type_id); |
16067 | |
16068 | buffer_type.basetype = SPIRType::Struct; |
16069 | |
16070 | if ((argument_buffer_device_storage_mask & (1u << desc_set)) != 0) |
16071 | { |
16072 | buffer_type.storage = StorageClassStorageBuffer; |
16073 | // Make sure the argument buffer gets marked as const device. |
16074 | set_decoration(id: next_id, decoration: DecorationNonWritable); |
16075 | // Need to mark the type as a Block to enable this. |
16076 | set_decoration(id: type_id, decoration: DecorationBlock); |
16077 | } |
16078 | else |
16079 | buffer_type.storage = StorageClassUniform; |
16080 | |
16081 | set_name(id: type_id, name: join(ts: "spvDescriptorSetBuffer" , ts&: desc_set)); |
16082 | |
16083 | auto &ptr_type = set<SPIRType>(ptr_type_id); |
16084 | ptr_type = buffer_type; |
16085 | ptr_type.pointer = true; |
16086 | ptr_type.pointer_depth++; |
16087 | ptr_type.parent_type = type_id; |
16088 | |
16089 | uint32_t buffer_variable_id = next_id; |
16090 | set<SPIRVariable>(id: buffer_variable_id, args&: ptr_type_id, args: StorageClassUniform); |
16091 | set_name(id: buffer_variable_id, name: join(ts: "spvDescriptorSet" , ts&: desc_set)); |
16092 | |
16093 | // Ids must be emitted in ID order. |
16094 | sort(first: begin(cont&: resources), last: end(cont&: resources), comp: [&](const Resource &lhs, const Resource &rhs) -> bool { |
16095 | return tie(args: lhs.index, args: lhs.basetype) < tie(args: rhs.index, args: rhs.basetype); |
16096 | }); |
16097 | |
16098 | uint32_t member_index = 0; |
16099 | uint32_t next_arg_buff_index = 0; |
16100 | for (auto &resource : resources) |
16101 | { |
16102 | auto &var = *resource.var; |
16103 | auto &type = get_variable_data_type(var); |
16104 | |
16105 | // If needed, synthesize and add padding members. |
16106 | // member_index and next_arg_buff_index are incremented when padding members are added. |
16107 | if (msl_options.pad_argument_buffer_resources) |
16108 | { |
16109 | while (resource.index > next_arg_buff_index) |
16110 | { |
16111 | auto &rez_bind = get_argument_buffer_resource(desc_set, arg_idx: next_arg_buff_index); |
16112 | switch (rez_bind.basetype) |
16113 | { |
16114 | case SPIRType::Void: |
16115 | case SPIRType::Boolean: |
16116 | case SPIRType::SByte: |
16117 | case SPIRType::UByte: |
16118 | case SPIRType::Short: |
16119 | case SPIRType::UShort: |
16120 | case SPIRType::Int: |
16121 | case SPIRType::UInt: |
16122 | case SPIRType::Int64: |
16123 | case SPIRType::UInt64: |
16124 | case SPIRType::AtomicCounter: |
16125 | case SPIRType::Half: |
16126 | case SPIRType::Float: |
16127 | case SPIRType::Double: |
16128 | add_argument_buffer_padding_buffer_type(struct_type&: buffer_type, mbr_idx&: member_index, arg_buff_index&: next_arg_buff_index, rez_bind); |
16129 | break; |
16130 | case SPIRType::Image: |
16131 | add_argument_buffer_padding_image_type(struct_type&: buffer_type, mbr_idx&: member_index, arg_buff_index&: next_arg_buff_index, rez_bind); |
16132 | break; |
16133 | case SPIRType::Sampler: |
16134 | add_argument_buffer_padding_sampler_type(struct_type&: buffer_type, mbr_idx&: member_index, arg_buff_index&: next_arg_buff_index, rez_bind); |
16135 | break; |
16136 | case SPIRType::SampledImage: |
16137 | if (next_arg_buff_index == rez_bind.msl_sampler) |
16138 | add_argument_buffer_padding_sampler_type(struct_type&: buffer_type, mbr_idx&: member_index, arg_buff_index&: next_arg_buff_index, rez_bind); |
16139 | else |
16140 | add_argument_buffer_padding_image_type(struct_type&: buffer_type, mbr_idx&: member_index, arg_buff_index&: next_arg_buff_index, rez_bind); |
16141 | break; |
16142 | default: |
16143 | break; |
16144 | } |
16145 | } |
16146 | |
16147 | // Adjust the number of slots consumed by current member itself. |
16148 | // If actual member is an array, allow runtime array resolution as well. |
16149 | uint32_t elem_cnt = type.array.empty() ? 1 : to_array_size_literal(type); |
16150 | if (elem_cnt == 0) |
16151 | elem_cnt = get_resource_array_size(id: var.self); |
16152 | |
16153 | next_arg_buff_index += elem_cnt; |
16154 | } |
16155 | |
16156 | string mbr_name = ensure_valid_name(name: resource.name, pfx: "m" ); |
16157 | if (resource.plane > 0) |
16158 | mbr_name += join(ts&: plane_name_suffix, ts&: resource.plane); |
16159 | set_member_name(id: buffer_type.self, index: member_index, name: mbr_name); |
16160 | |
16161 | if (resource.basetype == SPIRType::Sampler && type.basetype != SPIRType::Sampler) |
16162 | { |
16163 | // Have to synthesize a sampler type here. |
16164 | |
16165 | bool type_is_array = !type.array.empty(); |
16166 | uint32_t sampler_type_id = ir.increase_bound_by(count: type_is_array ? 2 : 1); |
16167 | auto &new_sampler_type = set<SPIRType>(sampler_type_id); |
16168 | new_sampler_type.basetype = SPIRType::Sampler; |
16169 | new_sampler_type.storage = StorageClassUniformConstant; |
16170 | |
16171 | if (type_is_array) |
16172 | { |
16173 | uint32_t sampler_type_array_id = sampler_type_id + 1; |
16174 | auto &sampler_type_array = set<SPIRType>(sampler_type_array_id); |
16175 | sampler_type_array = new_sampler_type; |
16176 | sampler_type_array.array = type.array; |
16177 | sampler_type_array.array_size_literal = type.array_size_literal; |
16178 | sampler_type_array.parent_type = sampler_type_id; |
16179 | buffer_type.member_types.push_back(t: sampler_type_array_id); |
16180 | } |
16181 | else |
16182 | buffer_type.member_types.push_back(t: sampler_type_id); |
16183 | } |
16184 | else |
16185 | { |
16186 | uint32_t binding = get_decoration(id: var.self, decoration: DecorationBinding); |
16187 | SetBindingPair pair = { .desc_set: desc_set, .binding: binding }; |
16188 | |
16189 | if (resource.basetype == SPIRType::Image || resource.basetype == SPIRType::Sampler || |
16190 | resource.basetype == SPIRType::SampledImage) |
16191 | { |
16192 | // Drop pointer information when we emit the resources into a struct. |
16193 | buffer_type.member_types.push_back(t: get_variable_data_type_id(var)); |
16194 | if (resource.plane == 0) |
16195 | set_qualified_name(id: var.self, name: join(ts: to_name(id: buffer_variable_id), ts: "." , ts&: mbr_name)); |
16196 | } |
16197 | else if (buffers_requiring_dynamic_offset.count(x: pair)) |
16198 | { |
16199 | // Don't set the qualified name here; we'll define a variable holding the corrected buffer address later. |
16200 | buffer_type.member_types.push_back(t: var.basetype); |
16201 | buffers_requiring_dynamic_offset[pair].second = var.self; |
16202 | } |
16203 | else if (inline_uniform_blocks.count(x: pair)) |
16204 | { |
16205 | // Put the buffer block itself into the argument buffer. |
16206 | buffer_type.member_types.push_back(t: get_variable_data_type_id(var)); |
16207 | set_qualified_name(id: var.self, name: join(ts: to_name(id: buffer_variable_id), ts: "." , ts&: mbr_name)); |
16208 | } |
16209 | else if (atomic_image_vars.count(x: var.self)) |
16210 | { |
16211 | // Emulate texture2D atomic operations. |
16212 | // Don't set the qualified name: it's already set for this variable, |
16213 | // and the code that references the buffer manually appends "_atomic" |
16214 | // to the name. |
16215 | uint32_t offset = ir.increase_bound_by(count: 2); |
16216 | uint32_t atomic_type_id = offset; |
16217 | uint32_t type_ptr_id = offset + 1; |
16218 | |
16219 | SPIRType atomic_type; |
16220 | atomic_type.basetype = SPIRType::AtomicCounter; |
16221 | atomic_type.width = 32; |
16222 | atomic_type.vecsize = 1; |
16223 | set<SPIRType>(id: atomic_type_id, args&: atomic_type); |
16224 | |
16225 | atomic_type.pointer = true; |
16226 | atomic_type.pointer_depth++; |
16227 | atomic_type.parent_type = atomic_type_id; |
16228 | atomic_type.storage = StorageClassStorageBuffer; |
16229 | auto &atomic_ptr_type = set<SPIRType>(id: type_ptr_id, args&: atomic_type); |
16230 | atomic_ptr_type.self = atomic_type_id; |
16231 | |
16232 | buffer_type.member_types.push_back(t: type_ptr_id); |
16233 | } |
16234 | else |
16235 | { |
16236 | // Resources will be declared as pointers not references, so automatically dereference as appropriate. |
16237 | buffer_type.member_types.push_back(t: var.basetype); |
16238 | if (type.array.empty()) |
16239 | set_qualified_name(id: var.self, name: join(ts: "(*" , ts: to_name(id: buffer_variable_id), ts: "." , ts&: mbr_name, ts: ")" )); |
16240 | else |
16241 | set_qualified_name(id: var.self, name: join(ts: to_name(id: buffer_variable_id), ts: "." , ts&: mbr_name)); |
16242 | } |
16243 | } |
16244 | |
16245 | set_extended_member_decoration(type: buffer_type.self, index: member_index, decoration: SPIRVCrossDecorationResourceIndexPrimary, |
16246 | value: resource.index); |
16247 | set_extended_member_decoration(type: buffer_type.self, index: member_index, decoration: SPIRVCrossDecorationInterfaceOrigID, |
16248 | value: var.self); |
16249 | member_index++; |
16250 | } |
16251 | } |
16252 | } |
16253 | |
16254 | // Return the resource type of the app-provided resources for the descriptor set, |
16255 | // that matches the resource index of the argument buffer index. |
16256 | // This is a two-step lookup, first lookup the resource binding number from the argument buffer index, |
16257 | // then lookup the resource binding using the binding number. |
16258 | MSLResourceBinding &CompilerMSL::get_argument_buffer_resource(uint32_t desc_set, uint32_t arg_idx) |
16259 | { |
16260 | auto stage = get_entry_point().model; |
16261 | StageSetBinding arg_idx_tuple = { .model: stage, .desc_set: desc_set, .binding: arg_idx }; |
16262 | auto arg_itr = resource_arg_buff_idx_to_binding_number.find(x: arg_idx_tuple); |
16263 | if (arg_itr != end(cont&: resource_arg_buff_idx_to_binding_number)) |
16264 | { |
16265 | StageSetBinding bind_tuple = { .model: stage, .desc_set: desc_set, .binding: arg_itr->second }; |
16266 | auto bind_itr = resource_bindings.find(x: bind_tuple); |
16267 | if (bind_itr != end(cont&: resource_bindings)) |
16268 | return bind_itr->second.first; |
16269 | } |
16270 | SPIRV_CROSS_THROW("Argument buffer resource base type could not be determined. When padding argument buffer " |
16271 | "elements, all descriptor set resources must be supplied with a base type by the app." ); |
16272 | } |
16273 | |
16274 | // Adds an argument buffer padding argument buffer type as one or more members of the struct type at the member index. |
16275 | // Metal does not support arrays of buffers, so these are emitted as multiple struct members. |
16276 | void CompilerMSL::add_argument_buffer_padding_buffer_type(SPIRType &struct_type, uint32_t &mbr_idx, |
16277 | uint32_t &arg_buff_index, MSLResourceBinding &rez_bind) |
16278 | { |
16279 | if (!argument_buffer_padding_buffer_type_id) |
16280 | { |
16281 | uint32_t buff_type_id = ir.increase_bound_by(count: 2); |
16282 | auto &buff_type = set<SPIRType>(buff_type_id); |
16283 | buff_type.basetype = rez_bind.basetype; |
16284 | buff_type.storage = StorageClassUniformConstant; |
16285 | |
16286 | uint32_t ptr_type_id = buff_type_id + 1; |
16287 | auto &ptr_type = set<SPIRType>(ptr_type_id); |
16288 | ptr_type = buff_type; |
16289 | ptr_type.pointer = true; |
16290 | ptr_type.pointer_depth++; |
16291 | ptr_type.parent_type = buff_type_id; |
16292 | |
16293 | argument_buffer_padding_buffer_type_id = ptr_type_id; |
16294 | } |
16295 | |
16296 | for (uint32_t rez_idx = 0; rez_idx < rez_bind.count; rez_idx++) |
16297 | add_argument_buffer_padding_type(mbr_type_id: argument_buffer_padding_buffer_type_id, struct_type, mbr_idx, arg_buff_index, count: 1); |
16298 | } |
16299 | |
16300 | // Adds an argument buffer padding argument image type as a member of the struct type at the member index. |
16301 | void CompilerMSL::add_argument_buffer_padding_image_type(SPIRType &struct_type, uint32_t &mbr_idx, |
16302 | uint32_t &arg_buff_index, MSLResourceBinding &rez_bind) |
16303 | { |
16304 | if (!argument_buffer_padding_image_type_id) |
16305 | { |
16306 | uint32_t base_type_id = ir.increase_bound_by(count: 2); |
16307 | auto &base_type = set<SPIRType>(base_type_id); |
16308 | base_type.basetype = SPIRType::Float; |
16309 | base_type.width = 32; |
16310 | |
16311 | uint32_t img_type_id = base_type_id + 1; |
16312 | auto &img_type = set<SPIRType>(img_type_id); |
16313 | img_type.basetype = SPIRType::Image; |
16314 | img_type.storage = StorageClassUniformConstant; |
16315 | |
16316 | img_type.image.type = base_type_id; |
16317 | img_type.image.dim = Dim2D; |
16318 | img_type.image.depth = false; |
16319 | img_type.image.arrayed = false; |
16320 | img_type.image.ms = false; |
16321 | img_type.image.sampled = 1; |
16322 | img_type.image.format = ImageFormatUnknown; |
16323 | img_type.image.access = AccessQualifierMax; |
16324 | |
16325 | argument_buffer_padding_image_type_id = img_type_id; |
16326 | } |
16327 | |
16328 | add_argument_buffer_padding_type(mbr_type_id: argument_buffer_padding_image_type_id, struct_type, mbr_idx, arg_buff_index, count: rez_bind.count); |
16329 | } |
16330 | |
16331 | // Adds an argument buffer padding argument sampler type as a member of the struct type at the member index. |
16332 | void CompilerMSL::add_argument_buffer_padding_sampler_type(SPIRType &struct_type, uint32_t &mbr_idx, |
16333 | uint32_t &arg_buff_index, MSLResourceBinding &rez_bind) |
16334 | { |
16335 | if (!argument_buffer_padding_sampler_type_id) |
16336 | { |
16337 | uint32_t samp_type_id = ir.increase_bound_by(count: 1); |
16338 | auto &samp_type = set<SPIRType>(samp_type_id); |
16339 | samp_type.basetype = SPIRType::Sampler; |
16340 | samp_type.storage = StorageClassUniformConstant; |
16341 | |
16342 | argument_buffer_padding_sampler_type_id = samp_type_id; |
16343 | } |
16344 | |
16345 | add_argument_buffer_padding_type(mbr_type_id: argument_buffer_padding_sampler_type_id, struct_type, mbr_idx, arg_buff_index, count: rez_bind.count); |
16346 | } |
16347 | |
16348 | // Adds the argument buffer padding argument type as a member of the struct type at the member index. |
16349 | // Advances both arg_buff_index and mbr_idx to next argument slots. |
16350 | void CompilerMSL::add_argument_buffer_padding_type(uint32_t mbr_type_id, SPIRType &struct_type, uint32_t &mbr_idx, |
16351 | uint32_t &arg_buff_index, uint32_t count) |
16352 | { |
16353 | uint32_t type_id = mbr_type_id; |
16354 | if (count > 1) |
16355 | { |
16356 | uint32_t ary_type_id = ir.increase_bound_by(count: 1); |
16357 | auto &ary_type = set<SPIRType>(ary_type_id); |
16358 | ary_type = get<SPIRType>(id: type_id); |
16359 | ary_type.array.push_back(t: count); |
16360 | ary_type.array_size_literal.push_back(t: true); |
16361 | ary_type.parent_type = type_id; |
16362 | type_id = ary_type_id; |
16363 | } |
16364 | |
16365 | set_member_name(id: struct_type.self, index: mbr_idx, name: join(ts: "_m" , ts&: arg_buff_index, ts: "_pad" )); |
16366 | set_extended_member_decoration(type: struct_type.self, index: mbr_idx, decoration: SPIRVCrossDecorationResourceIndexPrimary, value: arg_buff_index); |
16367 | struct_type.member_types.push_back(t: type_id); |
16368 | |
16369 | arg_buff_index += count; |
16370 | mbr_idx++; |
16371 | } |
16372 | |
16373 | void CompilerMSL::activate_argument_buffer_resources() |
16374 | { |
16375 | // For ABI compatibility, force-enable all resources which are part of argument buffers. |
16376 | ir.for_each_typed_id<SPIRVariable>(op: [&](uint32_t self, const SPIRVariable &) { |
16377 | if (!has_decoration(id: self, decoration: DecorationDescriptorSet)) |
16378 | return; |
16379 | |
16380 | uint32_t desc_set = get_decoration(id: self, decoration: DecorationDescriptorSet); |
16381 | if (descriptor_set_is_argument_buffer(desc_set)) |
16382 | active_interface_variables.insert(x: self); |
16383 | }); |
16384 | } |
16385 | |
16386 | bool CompilerMSL::using_builtin_array() const |
16387 | { |
16388 | return msl_options.force_native_arrays || is_using_builtin_array; |
16389 | } |
16390 | |
16391 | void CompilerMSL::set_combined_sampler_suffix(const char *suffix) |
16392 | { |
16393 | sampler_name_suffix = suffix; |
16394 | } |
16395 | |
16396 | const char *CompilerMSL::get_combined_sampler_suffix() const |
16397 | { |
16398 | return sampler_name_suffix.c_str(); |
16399 | } |
16400 | |
16401 | void CompilerMSL::emit_block_hints(const SPIRBlock &) |
16402 | { |
16403 | } |
16404 | |
16405 | string CompilerMSL::additional_fixed_sample_mask_str() const |
16406 | { |
16407 | char print_buffer[32]; |
16408 | sprintf(s: print_buffer, format: "0x%x" , msl_options.additional_fixed_sample_mask); |
16409 | return print_buffer; |
16410 | } |
16411 | |