| 1 |
/* |
| 2 |
* Copyright 2016-2020 The Brenwill Workshop Ltd. |
| 3 |
* |
| 4 |
* Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 |
* you may not use this file except in compliance with the License. |
| 6 |
* You may obtain a copy of the License at |
| 7 |
* |
| 8 |
* http://www.apache.org/licenses/LICENSE-2.0 |
| 9 |
* |
| 10 |
* Unless required by applicable law or agreed to in writing, software |
| 11 |
* distributed under the License is distributed on an "AS IS" BASIS, |
| 12 |
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 |
* See the License for the specific language governing permissions and |
| 14 |
* limitations under the License. |
| 15 |
*/ |
| 16 |
|
| 17 |
#include "spirv_msl.hpp" |
| 18 |
#include "GLSL.std.450.h" |
| 19 |
|
| 20 |
#include <algorithm> |
| 21 |
#include <assert.h> |
| 22 |
#include <numeric> |
| 23 |
|
| 24 |
using namespace spv; |
| 25 |
using namespace SPIRV_CROSS_NAMESPACE; |
| 26 |
using namespace std; |
| 27 |
|
| 28 |
static const uint32_t k_unknown_location = ~0u; |
| 29 |
static const uint32_t k_unknown_component = ~0u; |
| 30 |
static const char *force_inline = "static inline __attribute__((always_inline))"; |
| 31 |
|
| 32 |
CompilerMSL::CompilerMSL(std::vector<uint32_t> spirv_) |
| 33 |
: CompilerGLSL(move(spirv_)) |
| 34 |
{ |
| 35 |
} |
| 36 |
|
| 37 |
CompilerMSL::CompilerMSL(const uint32_t *ir_, size_t word_count) |
| 38 |
: CompilerGLSL(ir_, word_count) |
| 39 |
{ |
| 40 |
} |
| 41 |
|
| 42 |
CompilerMSL::CompilerMSL(const ParsedIR &ir_) |
| 43 |
: CompilerGLSL(ir_) |
| 44 |
{ |
| 45 |
} |
| 46 |
|
| 47 |
CompilerMSL::CompilerMSL(ParsedIR &&ir_) |
| 48 |
: CompilerGLSL(std::move(ir_)) |
| 49 |
{ |
| 50 |
} |
| 51 |
|
| 52 |
void CompilerMSL::add_msl_shader_input(const MSLShaderInput &si) |
| 53 |
{ |
| 54 |
inputs_by_location[si.location] = si; |
| 55 |
if (si.builtin != BuiltInMax && !inputs_by_builtin.count(si.builtin)) |
| 56 |
inputs_by_builtin[si.builtin] = si; |
| 57 |
} |
| 58 |
|
| 59 |
void CompilerMSL::add_msl_resource_binding(const MSLResourceBinding &binding) |
| 60 |
{ |
| 61 |
StageSetBinding tuple = { binding.stage, binding.desc_set, binding.binding }; |
| 62 |
resource_bindings[tuple] = { binding, false }; |
| 63 |
} |
| 64 |
|
| 65 |
void CompilerMSL::add_dynamic_buffer(uint32_t desc_set, uint32_t binding, uint32_t index) |
| 66 |
{ |
| 67 |
SetBindingPair pair = { desc_set, binding }; |
| 68 |
buffers_requiring_dynamic_offset[pair] = { index, 0 }; |
| 69 |
} |
| 70 |
|
| 71 |
void CompilerMSL::add_inline_uniform_block(uint32_t desc_set, uint32_t binding) |
| 72 |
{ |
| 73 |
SetBindingPair pair = { desc_set, binding }; |
| 74 |
inline_uniform_blocks.insert(pair); |
| 75 |
} |
| 76 |
|
| 77 |
void CompilerMSL::add_discrete_descriptor_set(uint32_t desc_set) |
| 78 |
{ |
| 79 |
if (desc_set < kMaxArgumentBuffers) |
| 80 |
argument_buffer_discrete_mask |= 1u << desc_set; |
| 81 |
} |
| 82 |
|
| 83 |
void CompilerMSL::set_argument_buffer_device_address_space(uint32_t desc_set, bool device_storage) |
| 84 |
{ |
| 85 |
if (desc_set < kMaxArgumentBuffers) |
| 86 |
{ |
| 87 |
if (device_storage) |
| 88 |
argument_buffer_device_storage_mask |= 1u << desc_set; |
| 89 |
else |
| 90 |
argument_buffer_device_storage_mask &= ~(1u << desc_set); |
| 91 |
} |
| 92 |
} |
| 93 |
|
| 94 |
bool CompilerMSL::is_msl_shader_input_used(uint32_t location) |
| 95 |
{ |
| 96 |
return inputs_in_use.count(location) != 0; |
| 97 |
} |
| 98 |
|
| 99 |
bool CompilerMSL::is_msl_resource_binding_used(ExecutionModel model, uint32_t desc_set, uint32_t binding) const |
| 100 |
{ |
| 101 |
StageSetBinding tuple = { model, desc_set, binding }; |
| 102 |
auto itr = resource_bindings.find(tuple); |
| 103 |
return itr != end(resource_bindings) && itr->second.second; |
| 104 |
} |
| 105 |
|
| 106 |
// Returns the size of the array of resources used by the variable with the specified id. |
| 107 |
// The returned value is retrieved from the resource binding added using add_msl_resource_binding(). |
| 108 |
uint32_t CompilerMSL::get_resource_array_size(uint32_t id) const |
| 109 |
{ |
| 110 |
StageSetBinding tuple = { get_entry_point().model, get_decoration(id, DecorationDescriptorSet), |
| 111 |
get_decoration(id, DecorationBinding) }; |
| 112 |
auto itr = resource_bindings.find(tuple); |
| 113 |
return itr != end(resource_bindings) ? itr->second.first.count : 0; |
| 114 |
} |
| 115 |
|
| 116 |
uint32_t CompilerMSL::get_automatic_msl_resource_binding(uint32_t id) const |
| 117 |
{ |
| 118 |
return get_extended_decoration(id, SPIRVCrossDecorationResourceIndexPrimary); |
| 119 |
} |
| 120 |
|
| 121 |
uint32_t CompilerMSL::get_automatic_msl_resource_binding_secondary(uint32_t id) const |
| 122 |
{ |
| 123 |
return get_extended_decoration(id, SPIRVCrossDecorationResourceIndexSecondary); |
| 124 |
} |
| 125 |
|
| 126 |
uint32_t CompilerMSL::get_automatic_msl_resource_binding_tertiary(uint32_t id) const |
| 127 |
{ |
| 128 |
return get_extended_decoration(id, SPIRVCrossDecorationResourceIndexTertiary); |
| 129 |
} |
| 130 |
|
| 131 |
uint32_t CompilerMSL::get_automatic_msl_resource_binding_quaternary(uint32_t id) const |
| 132 |
{ |
| 133 |
return get_extended_decoration(id, SPIRVCrossDecorationResourceIndexQuaternary); |
| 134 |
} |
| 135 |
|
| 136 |
void CompilerMSL::set_fragment_output_components(uint32_t location, uint32_t components) |
| 137 |
{ |
| 138 |
fragment_output_components[location] = components; |
| 139 |
} |
| 140 |
|
| 141 |
bool CompilerMSL::builtin_translates_to_nonarray(spv::BuiltIn builtin) const |
| 142 |
{ |
| 143 |
return (builtin == BuiltInSampleMask); |
| 144 |
} |
| 145 |
|
| 146 |
void CompilerMSL::build_implicit_builtins() |
| 147 |
{ |
| 148 |
bool need_sample_pos = active_input_builtins.get(BuiltInSamplePosition); |
| 149 |
bool need_vertex_params = capture_output_to_buffer && get_execution_model() == ExecutionModelVertex && |
| 150 |
!msl_options.vertex_for_tessellation; |
| 151 |
bool need_tesc_params = get_execution_model() == ExecutionModelTessellationControl; |
| 152 |
bool need_subgroup_mask = |
| 153 |
active_input_builtins.get(BuiltInSubgroupEqMask) || active_input_builtins.get(BuiltInSubgroupGeMask) || |
| 154 |
active_input_builtins.get(BuiltInSubgroupGtMask) || active_input_builtins.get(BuiltInSubgroupLeMask) || |
| 155 |
active_input_builtins.get(BuiltInSubgroupLtMask); |
| 156 |
bool need_subgroup_ge_mask = !msl_options.is_ios() && (active_input_builtins.get(BuiltInSubgroupGeMask) || |
| 157 |
active_input_builtins.get(BuiltInSubgroupGtMask)); |
| 158 |
bool need_multiview = get_execution_model() == ExecutionModelVertex && !msl_options.view_index_from_device_index && |
| 159 |
msl_options.multiview_layered_rendering && |
| 160 |
(msl_options.multiview || active_input_builtins.get(BuiltInViewIndex)); |
| 161 |
bool need_dispatch_base = |
| 162 |
msl_options.dispatch_base && get_execution_model() == ExecutionModelGLCompute && |
| 163 |
(active_input_builtins.get(BuiltInWorkgroupId) || active_input_builtins.get(BuiltInGlobalInvocationId)); |
| 164 |
bool need_grid_params = get_execution_model() == ExecutionModelVertex && msl_options.vertex_for_tessellation; |
| 165 |
bool need_vertex_base_params = |
| 166 |
need_grid_params && |
| 167 |
(active_input_builtins.get(BuiltInVertexId) || active_input_builtins.get(BuiltInVertexIndex) || |
| 168 |
active_input_builtins.get(BuiltInBaseVertex) || active_input_builtins.get(BuiltInInstanceId) || |
| 169 |
active_input_builtins.get(BuiltInInstanceIndex) || active_input_builtins.get(BuiltInBaseInstance)); |
| 170 |
bool need_sample_mask = msl_options.additional_fixed_sample_mask != 0xffffffff; |
| 171 |
if (need_subpass_input || need_sample_pos || need_subgroup_mask || need_vertex_params || need_tesc_params || |
| 172 |
need_multiview || need_dispatch_base || need_vertex_base_params || need_grid_params || needs_sample_id || |
| 173 |
needs_subgroup_invocation_id || needs_subgroup_size || need_sample_mask) |
| 174 |
{ |
| 175 |
bool has_frag_coord = false; |
| 176 |
bool has_sample_id = false; |
| 177 |
bool has_vertex_idx = false; |
| 178 |
bool has_base_vertex = false; |
| 179 |
bool has_instance_idx = false; |
| 180 |
bool has_base_instance = false; |
| 181 |
bool has_invocation_id = false; |
| 182 |
bool has_primitive_id = false; |
| 183 |
bool has_subgroup_invocation_id = false; |
| 184 |
bool has_subgroup_size = false; |
| 185 |
bool has_view_idx = false; |
| 186 |
bool has_layer = false; |
| 187 |
uint32_t workgroup_id_type = 0; |
| 188 |
|
| 189 |
// FIXME: Investigate the fact that there are no checks for the entry point interface variables. |
| 190 |
ir.for_each_typed_id<SPIRVariable>([&](uint32_t, SPIRVariable &var) { |
| 191 |
if (!ir.meta[var.self].decoration.builtin) |
| 192 |
return; |
| 193 |
|
| 194 |
// Use Metal's native frame-buffer fetch API for subpass inputs. |
| 195 |
BuiltIn builtin = ir.meta[var.self].decoration.builtin_type; |
| 196 |
|
| 197 |
if (var.storage == StorageClassOutput) |
| 198 |
{ |
| 199 |
if (need_sample_mask && builtin == BuiltInSampleMask) |
| 200 |
{ |
| 201 |
builtin_sample_mask_id = var.self; |
| 202 |
mark_implicit_builtin(StorageClassOutput, BuiltInSampleMask, var.self); |
| 203 |
does_shader_write_sample_mask = true; |
| 204 |
} |
| 205 |
} |
| 206 |
|
| 207 |
if (var.storage != StorageClassInput) |
| 208 |
return; |
| 209 |
|
| 210 |
if (need_subpass_input && (!msl_options.use_framebuffer_fetch_subpasses)) |
| 211 |
{ |
| 212 |
switch (builtin) |
| 213 |
{ |
| 214 |
case BuiltInFragCoord: |
| 215 |
mark_implicit_builtin(StorageClassInput, BuiltInFragCoord, var.self); |
| 216 |
builtin_frag_coord_id = var.self; |
| 217 |
has_frag_coord = true; |
| 218 |
break; |
| 219 |
case BuiltInLayer: |
| 220 |
if (!msl_options.arrayed_subpass_input || msl_options.multiview) |
| 221 |
break; |
| 222 |
mark_implicit_builtin(StorageClassInput, BuiltInLayer, var.self); |
| 223 |
builtin_layer_id = var.self; |
| 224 |
has_layer = true; |
| 225 |
break; |
| 226 |
case BuiltInViewIndex: |
| 227 |
if (!msl_options.multiview) |
| 228 |
break; |
| 229 |
mark_implicit_builtin(StorageClassInput, BuiltInViewIndex, var.self); |
| 230 |
builtin_view_idx_id = var.self; |
| 231 |
has_view_idx = true; |
| 232 |
break; |
| 233 |
default: |
| 234 |
break; |
| 235 |
} |
| 236 |
} |
| 237 |
|
| 238 |
if ((need_sample_pos || needs_sample_id) && builtin == BuiltInSampleId) |
| 239 |
{ |
| 240 |
builtin_sample_id_id = var.self; |
| 241 |
mark_implicit_builtin(StorageClassInput, BuiltInSampleId, var.self); |
| 242 |
has_sample_id = true; |
| 243 |
} |
| 244 |
|
| 245 |
if (need_vertex_params) |
| 246 |
{ |
| 247 |
switch (builtin) |
| 248 |
{ |
| 249 |
case BuiltInVertexIndex: |
| 250 |
builtin_vertex_idx_id = var.self; |
| 251 |
mark_implicit_builtin(StorageClassInput, BuiltInVertexIndex, var.self); |
| 252 |
has_vertex_idx = true; |
| 253 |
break; |
| 254 |
case BuiltInBaseVertex: |
| 255 |
builtin_base_vertex_id = var.self; |
| 256 |
mark_implicit_builtin(StorageClassInput, BuiltInBaseVertex, var.self); |
| 257 |
has_base_vertex = true; |
| 258 |
break; |
| 259 |
case BuiltInInstanceIndex: |
| 260 |
builtin_instance_idx_id = var.self; |
| 261 |
mark_implicit_builtin(StorageClassInput, BuiltInInstanceIndex, var.self); |
| 262 |
has_instance_idx = true; |
| 263 |
break; |
| 264 |
case BuiltInBaseInstance: |
| 265 |
builtin_base_instance_id = var.self; |
| 266 |
mark_implicit_builtin(StorageClassInput, BuiltInBaseInstance, var.self); |
| 267 |
has_base_instance = true; |
| 268 |
break; |
| 269 |
default: |
| 270 |
break; |
| 271 |
} |
| 272 |
} |
| 273 |
|
| 274 |
if (need_tesc_params) |
| 275 |
{ |
| 276 |
switch (builtin) |
| 277 |
{ |
| 278 |
case BuiltInInvocationId: |
| 279 |
builtin_invocation_id_id = var.self; |
| 280 |
mark_implicit_builtin(StorageClassInput, BuiltInInvocationId, var.self); |
| 281 |
has_invocation_id = true; |
| 282 |
break; |
| 283 |
case BuiltInPrimitiveId: |
| 284 |
builtin_primitive_id_id = var.self; |
| 285 |
mark_implicit_builtin(StorageClassInput, BuiltInPrimitiveId, var.self); |
| 286 |
has_primitive_id = true; |
| 287 |
break; |
| 288 |
default: |
| 289 |
break; |
| 290 |
} |
| 291 |
} |
| 292 |
|
| 293 |
if ((need_subgroup_mask || needs_subgroup_invocation_id) && builtin == BuiltInSubgroupLocalInvocationId) |
| 294 |
{ |
| 295 |
builtin_subgroup_invocation_id_id = var.self; |
| 296 |
mark_implicit_builtin(StorageClassInput, BuiltInSubgroupLocalInvocationId, var.self); |
| 297 |
has_subgroup_invocation_id = true; |
| 298 |
} |
| 299 |
|
| 300 |
if ((need_subgroup_ge_mask || needs_subgroup_size) && builtin == BuiltInSubgroupSize) |
| 301 |
{ |
| 302 |
builtin_subgroup_size_id = var.self; |
| 303 |
mark_implicit_builtin(StorageClassInput, BuiltInSubgroupSize, var.self); |
| 304 |
has_subgroup_size = true; |
| 305 |
} |
| 306 |
|
| 307 |
if (need_multiview) |
| 308 |
{ |
| 309 |
switch (builtin) |
| 310 |
{ |
| 311 |
case BuiltInInstanceIndex: |
| 312 |
// The view index here is derived from the instance index. |
| 313 |
builtin_instance_idx_id = var.self; |
| 314 |
mark_implicit_builtin(StorageClassInput, BuiltInInstanceIndex, var.self); |
| 315 |
has_instance_idx = true; |
| 316 |
break; |
| 317 |
case BuiltInBaseInstance: |
| 318 |
// If a non-zero base instance is used, we need to adjust for it when calculating the view index. |
| 319 |
builtin_base_instance_id = var.self; |
| 320 |
mark_implicit_builtin(StorageClassInput, BuiltInBaseInstance, var.self); |
| 321 |
has_base_instance = true; |
| 322 |
break; |
| 323 |
case BuiltInViewIndex: |
| 324 |
builtin_view_idx_id = var.self; |
| 325 |
mark_implicit_builtin(StorageClassInput, BuiltInViewIndex, var.self); |
| 326 |
has_view_idx = true; |
| 327 |
break; |
| 328 |
default: |
| 329 |
break; |
| 330 |
} |
| 331 |
} |
| 332 |
|
| 333 |
// The base workgroup needs to have the same type and vector size |
| 334 |
// as the workgroup or invocation ID, so keep track of the type that |
| 335 |
// was used. |
| 336 |
if (need_dispatch_base && workgroup_id_type == 0 && |
| 337 |
(builtin == BuiltInWorkgroupId || builtin == BuiltInGlobalInvocationId)) |
| 338 |
workgroup_id_type = var.basetype; |
| 339 |
}); |
| 340 |
|
| 341 |
// Use Metal's native frame-buffer fetch API for subpass inputs. |
| 342 |
if ((!has_frag_coord || (msl_options.multiview && !has_view_idx) || |
| 343 |
(msl_options.arrayed_subpass_input && !msl_options.multiview && !has_layer)) && |
| 344 |
(!msl_options.use_framebuffer_fetch_subpasses) && need_subpass_input) |
| 345 |
{ |
| 346 |
if (!has_frag_coord) |
| 347 |
{ |
| 348 |
uint32_t offset = ir.increase_bound_by(3); |
| 349 |
uint32_t type_id = offset; |
| 350 |
uint32_t type_ptr_id = offset + 1; |
| 351 |
uint32_t var_id = offset + 2; |
| 352 |
|
| 353 |
// Create gl_FragCoord. |
| 354 |
SPIRType vec4_type; |
| 355 |
vec4_type.basetype = SPIRType::Float; |
| 356 |
vec4_type.width = 32; |
| 357 |
vec4_type.vecsize = 4; |
| 358 |
set<SPIRType>(type_id, vec4_type); |
| 359 |
|
| 360 |
SPIRType vec4_type_ptr; |
| 361 |
vec4_type_ptr = vec4_type; |
| 362 |
vec4_type_ptr.pointer = true; |
| 363 |
vec4_type_ptr.parent_type = type_id; |
| 364 |
vec4_type_ptr.storage = StorageClassInput; |
| 365 |
auto &ptr_type = set<SPIRType>(type_ptr_id, vec4_type_ptr); |
| 366 |
ptr_type.self = type_id; |
| 367 |
|
| 368 |
set<SPIRVariable>(var_id, type_ptr_id, StorageClassInput); |
| 369 |
set_decoration(var_id, DecorationBuiltIn, BuiltInFragCoord); |
| 370 |
builtin_frag_coord_id = var_id; |
| 371 |
mark_implicit_builtin(StorageClassInput, BuiltInFragCoord, var_id); |
| 372 |
} |
| 373 |
|
| 374 |
if (!has_layer && msl_options.arrayed_subpass_input && !msl_options.multiview) |
| 375 |
{ |
| 376 |
uint32_t offset = ir.increase_bound_by(2); |
| 377 |
uint32_t type_ptr_id = offset; |
| 378 |
uint32_t var_id = offset + 1; |
| 379 |
|
| 380 |
// Create gl_Layer. |
| 381 |
SPIRType uint_type_ptr; |
| 382 |
uint_type_ptr = get_uint_type(); |
| 383 |
uint_type_ptr.pointer = true; |
| 384 |
uint_type_ptr.parent_type = get_uint_type_id(); |
| 385 |
uint_type_ptr.storage = StorageClassInput; |
| 386 |
auto &ptr_type = set<SPIRType>(type_ptr_id, uint_type_ptr); |
| 387 |
ptr_type.self = get_uint_type_id(); |
| 388 |
|
| 389 |
set<SPIRVariable>(var_id, type_ptr_id, StorageClassInput); |
| 390 |
set_decoration(var_id, DecorationBuiltIn, BuiltInLayer); |
| 391 |
builtin_layer_id = var_id; |
| 392 |
mark_implicit_builtin(StorageClassInput, BuiltInLayer, var_id); |
| 393 |
} |
| 394 |
|
| 395 |
if (!has_view_idx && msl_options.multiview) |
| 396 |
{ |
| 397 |
uint32_t offset = ir.increase_bound_by(2); |
| 398 |
uint32_t type_ptr_id = offset; |
| 399 |
uint32_t var_id = offset + 1; |
| 400 |
|
| 401 |
// Create gl_ViewIndex. |
| 402 |
SPIRType uint_type_ptr; |
| 403 |
uint_type_ptr = get_uint_type(); |
| 404 |
uint_type_ptr.pointer = true; |
| 405 |
uint_type_ptr.parent_type = get_uint_type_id(); |
| 406 |
uint_type_ptr.storage = StorageClassInput; |
| 407 |
auto &ptr_type = set<SPIRType>(type_ptr_id, uint_type_ptr); |
| 408 |
ptr_type.self = get_uint_type_id(); |
| 409 |
|
| 410 |
set<SPIRVariable>(var_id, type_ptr_id, StorageClassInput); |
| 411 |
set_decoration(var_id, DecorationBuiltIn, BuiltInViewIndex); |
| 412 |
builtin_view_idx_id = var_id; |
| 413 |
mark_implicit_builtin(StorageClassInput, BuiltInViewIndex, var_id); |
| 414 |
} |
| 415 |
} |
| 416 |
|
| 417 |
if (!has_sample_id && (need_sample_pos || needs_sample_id)) |
| 418 |
{ |
| 419 |
uint32_t offset = ir.increase_bound_by(2); |
| 420 |
uint32_t type_ptr_id = offset; |
| 421 |
uint32_t var_id = offset + 1; |
| 422 |
|
| 423 |
// Create gl_SampleID. |
| 424 |
SPIRType uint_type_ptr; |
| 425 |
uint_type_ptr = get_uint_type(); |
| 426 |
uint_type_ptr.pointer = true; |
| 427 |
uint_type_ptr.parent_type = get_uint_type_id(); |
| 428 |
uint_type_ptr.storage = StorageClassInput; |
| 429 |
auto &ptr_type = set<SPIRType>(type_ptr_id, uint_type_ptr); |
| 430 |
ptr_type.self = get_uint_type_id(); |
| 431 |
|
| 432 |
set<SPIRVariable>(var_id, type_ptr_id, StorageClassInput); |
| 433 |
set_decoration(var_id, DecorationBuiltIn, BuiltInSampleId); |
| 434 |
builtin_sample_id_id = var_id; |
| 435 |
mark_implicit_builtin(StorageClassInput, BuiltInSampleId, var_id); |
| 436 |
} |
| 437 |
|
| 438 |
if ((need_vertex_params && (!has_vertex_idx || !has_base_vertex || !has_instance_idx || !has_base_instance)) || |
| 439 |
(need_multiview && (!has_instance_idx || !has_base_instance || !has_view_idx))) |
| 440 |
{ |
| 441 |
uint32_t type_ptr_id = ir.increase_bound_by(1); |
| 442 |
|
| 443 |
SPIRType uint_type_ptr; |
| 444 |
uint_type_ptr = get_uint_type(); |
| 445 |
uint_type_ptr.pointer = true; |
| 446 |
uint_type_ptr.parent_type = get_uint_type_id(); |
| 447 |
uint_type_ptr.storage = StorageClassInput; |
| 448 |
auto &ptr_type = set<SPIRType>(type_ptr_id, uint_type_ptr); |
| 449 |
ptr_type.self = get_uint_type_id(); |
| 450 |
|
| 451 |
if (need_vertex_params && !has_vertex_idx) |
| 452 |
{ |
| 453 |
uint32_t var_id = ir.increase_bound_by(1); |
| 454 |
|
| 455 |
// Create gl_VertexIndex. |
| 456 |
set<SPIRVariable>(var_id, type_ptr_id, StorageClassInput); |
| 457 |
set_decoration(var_id, DecorationBuiltIn, BuiltInVertexIndex); |
| 458 |
builtin_vertex_idx_id = var_id; |
| 459 |
mark_implicit_builtin(StorageClassInput, BuiltInVertexIndex, var_id); |
| 460 |
} |
| 461 |
|
| 462 |
if (need_vertex_params && !has_base_vertex) |
| 463 |
{ |
| 464 |
uint32_t var_id = ir.increase_bound_by(1); |
| 465 |
|
| 466 |
// Create gl_BaseVertex. |
| 467 |
set<SPIRVariable>(var_id, type_ptr_id, StorageClassInput); |
| 468 |
set_decoration(var_id, DecorationBuiltIn, BuiltInBaseVertex); |
| 469 |
builtin_base_vertex_id = var_id; |
| 470 |
mark_implicit_builtin(StorageClassInput, BuiltInBaseVertex, var_id); |
| 471 |
} |
| 472 |
|
| 473 |
if (!has_instance_idx) // Needed by both multiview and tessellation |
| 474 |
{ |
| 475 |
uint32_t var_id = ir.increase_bound_by(1); |
| 476 |
|
| 477 |
// Create gl_InstanceIndex. |
| 478 |
set<SPIRVariable>(var_id, type_ptr_id, StorageClassInput); |
| 479 |
set_decoration(var_id, DecorationBuiltIn, BuiltInInstanceIndex); |
| 480 |
builtin_instance_idx_id = var_id; |
| 481 |
mark_implicit_builtin(StorageClassInput, BuiltInInstanceIndex, var_id); |
| 482 |
} |
| 483 |
|
| 484 |
if (!has_base_instance) // Needed by both multiview and tessellation |
| 485 |
{ |
| 486 |
uint32_t var_id = ir.increase_bound_by(1); |
| 487 |
|
| 488 |
// Create gl_BaseInstance. |
| 489 |
set<SPIRVariable>(var_id, type_ptr_id, StorageClassInput); |
| 490 |
set_decoration(var_id, DecorationBuiltIn, BuiltInBaseInstance); |
| 491 |
builtin_base_instance_id = var_id; |
| 492 |
mark_implicit_builtin(StorageClassInput, BuiltInBaseInstance, var_id); |
| 493 |
} |
| 494 |
|
| 495 |
if (need_multiview) |
| 496 |
{ |
| 497 |
// Multiview shaders are not allowed to write to gl_Layer, ostensibly because |
| 498 |
// it is implicitly written from gl_ViewIndex, but we have to do that explicitly. |
| 499 |
// Note that we can't just abuse gl_ViewIndex for this purpose: it's an input, but |
| 500 |
// gl_Layer is an output in vertex-pipeline shaders. |
| 501 |
uint32_t type_ptr_out_id = ir.increase_bound_by(2); |
| 502 |
SPIRType uint_type_ptr_out; |
| 503 |
uint_type_ptr_out = get_uint_type(); |
| 504 |
uint_type_ptr_out.pointer = true; |
| 505 |
uint_type_ptr_out.parent_type = get_uint_type_id(); |
| 506 |
uint_type_ptr_out.storage = StorageClassOutput; |
| 507 |
auto &ptr_out_type = set<SPIRType>(type_ptr_out_id, uint_type_ptr_out); |
| 508 |
ptr_out_type.self = get_uint_type_id(); |
| 509 |
uint32_t var_id = type_ptr_out_id + 1; |
| 510 |
set<SPIRVariable>(var_id, type_ptr_out_id, StorageClassOutput); |
| 511 |
set_decoration(var_id, DecorationBuiltIn, BuiltInLayer); |
| 512 |
builtin_layer_id = var_id; |
| 513 |
mark_implicit_builtin(StorageClassOutput, BuiltInLayer, var_id); |
| 514 |
} |
| 515 |
|
| 516 |
if (need_multiview && !has_view_idx) |
| 517 |
{ |
| 518 |
uint32_t var_id = ir.increase_bound_by(1); |
| 519 |
|
| 520 |
// Create gl_ViewIndex. |
| 521 |
set<SPIRVariable>(var_id, type_ptr_id, StorageClassInput); |
| 522 |
set_decoration(var_id, DecorationBuiltIn, BuiltInViewIndex); |
| 523 |
builtin_view_idx_id = var_id; |
| 524 |
mark_implicit_builtin(StorageClassInput, BuiltInViewIndex, var_id); |
| 525 |
} |
| 526 |
} |
| 527 |
|
| 528 |
if ((need_tesc_params && (msl_options.multi_patch_workgroup || !has_invocation_id || !has_primitive_id)) || |
| 529 |
need_grid_params) |
| 530 |
{ |
| 531 |
uint32_t type_ptr_id = ir.increase_bound_by(1); |
| 532 |
|
| 533 |
SPIRType uint_type_ptr; |
| 534 |
uint_type_ptr = get_uint_type(); |
| 535 |
uint_type_ptr.pointer = true; |
| 536 |
uint_type_ptr.parent_type = get_uint_type_id(); |
| 537 |
uint_type_ptr.storage = StorageClassInput; |
| 538 |
auto &ptr_type = set<SPIRType>(type_ptr_id, uint_type_ptr); |
| 539 |
ptr_type.self = get_uint_type_id(); |
| 540 |
|
| 541 |
if (msl_options.multi_patch_workgroup || need_grid_params) |
| 542 |
{ |
| 543 |
uint32_t var_id = ir.increase_bound_by(1); |
| 544 |
|
| 545 |
// Create gl_GlobalInvocationID. |
| 546 |
set<SPIRVariable>(var_id, type_ptr_id, StorageClassInput); |
| 547 |
set_decoration(var_id, DecorationBuiltIn, BuiltInGlobalInvocationId); |
| 548 |
builtin_invocation_id_id = var_id; |
| 549 |
mark_implicit_builtin(StorageClassInput, BuiltInGlobalInvocationId, var_id); |
| 550 |
} |
| 551 |
else if (need_tesc_params && !has_invocation_id) |
| 552 |
{ |
| 553 |
uint32_t var_id = ir.increase_bound_by(1); |
| 554 |
|
| 555 |
// Create gl_InvocationID. |
| 556 |
set<SPIRVariable>(var_id, type_ptr_id, StorageClassInput); |
| 557 |
set_decoration(var_id, DecorationBuiltIn, BuiltInInvocationId); |
| 558 |
builtin_invocation_id_id = var_id; |
| 559 |
mark_implicit_builtin(StorageClassInput, BuiltInInvocationId, var_id); |
| 560 |
} |
| 561 |
|
| 562 |
if (need_tesc_params && !has_primitive_id) |
| 563 |
{ |
| 564 |
uint32_t var_id = ir.increase_bound_by(1); |
| 565 |
|
| 566 |
// Create gl_PrimitiveID. |
| 567 |
set<SPIRVariable>(var_id, type_ptr_id, StorageClassInput); |
| 568 |
set_decoration(var_id, DecorationBuiltIn, BuiltInPrimitiveId); |
| 569 |
builtin_primitive_id_id = var_id; |
| 570 |
mark_implicit_builtin(StorageClassInput, BuiltInPrimitiveId, var_id); |
| 571 |
} |
| 572 |
|
| 573 |
if (need_grid_params) |
| 574 |
{ |
| 575 |
uint32_t var_id = ir.increase_bound_by(1); |
| 576 |
|
| 577 |
set<SPIRVariable>(var_id, build_extended_vector_type(get_uint_type_id(), 3), StorageClassInput); |
| 578 |
set_extended_decoration(var_id, SPIRVCrossDecorationBuiltInStageInputSize); |
| 579 |
get_entry_point().interface_variables.push_back(var_id); |
| 580 |
set_name(var_id, "spvStageInputSize"); |
| 581 |
builtin_stage_input_size_id = var_id; |
| 582 |
} |
| 583 |
} |
| 584 |
|
| 585 |
if (!has_subgroup_invocation_id && (need_subgroup_mask || needs_subgroup_invocation_id)) |
| 586 |
{ |
| 587 |
uint32_t offset = ir.increase_bound_by(2); |
| 588 |
uint32_t type_ptr_id = offset; |
| 589 |
uint32_t var_id = offset + 1; |
| 590 |
|
| 591 |
// Create gl_SubgroupInvocationID. |
| 592 |
SPIRType uint_type_ptr; |
| 593 |
uint_type_ptr = get_uint_type(); |
| 594 |
uint_type_ptr.pointer = true; |
| 595 |
uint_type_ptr.parent_type = get_uint_type_id(); |
| 596 |
uint_type_ptr.storage = StorageClassInput; |
| 597 |
auto &ptr_type = set<SPIRType>(type_ptr_id, uint_type_ptr); |
| 598 |
ptr_type.self = get_uint_type_id(); |
| 599 |
|
| 600 |
set<SPIRVariable>(var_id, type_ptr_id, StorageClassInput); |
| 601 |
set_decoration(var_id, DecorationBuiltIn, BuiltInSubgroupLocalInvocationId); |
| 602 |
builtin_subgroup_invocation_id_id = var_id; |
| 603 |
mark_implicit_builtin(StorageClassInput, BuiltInSubgroupLocalInvocationId, var_id); |
| 604 |
} |
| 605 |
|
| 606 |
if (!has_subgroup_size && (need_subgroup_ge_mask || needs_subgroup_size)) |
| 607 |
{ |
| 608 |
uint32_t offset = ir.increase_bound_by(2); |
| 609 |
uint32_t type_ptr_id = offset; |
| 610 |
uint32_t var_id = offset + 1; |
| 611 |
|
| 612 |
// Create gl_SubgroupSize. |
| 613 |
SPIRType uint_type_ptr; |
| 614 |
uint_type_ptr = get_uint_type(); |
| 615 |
uint_type_ptr.pointer = true; |
| 616 |
uint_type_ptr.parent_type = get_uint_type_id(); |
| 617 |
uint_type_ptr.storage = StorageClassInput; |
| 618 |
auto &ptr_type = set<SPIRType>(type_ptr_id, uint_type_ptr); |
| 619 |
ptr_type.self = get_uint_type_id(); |
| 620 |
|
| 621 |
set<SPIRVariable>(var_id, type_ptr_id, StorageClassInput); |
| 622 |
set_decoration(var_id, DecorationBuiltIn, BuiltInSubgroupSize); |
| 623 |
builtin_subgroup_size_id = var_id; |
| 624 |
mark_implicit_builtin(StorageClassInput, BuiltInSubgroupSize, var_id); |
| 625 |
} |
| 626 |
|
| 627 |
if (need_dispatch_base || need_vertex_base_params) |
| 628 |
{ |
| 629 |
if (workgroup_id_type == 0) |
| 630 |
workgroup_id_type = build_extended_vector_type(get_uint_type_id(), 3); |
| 631 |
uint32_t var_id; |
| 632 |
if (msl_options.supports_msl_version(1, 2)) |
| 633 |
{ |
| 634 |
// If we have MSL 1.2, we can (ab)use the [[grid_origin]] builtin |
| 635 |
// to convey this information and save a buffer slot. |
| 636 |
uint32_t offset = ir.increase_bound_by(1); |
| 637 |
var_id = offset; |
| 638 |
|
| 639 |
set<SPIRVariable>(var_id, workgroup_id_type, StorageClassInput); |
| 640 |
set_extended_decoration(var_id, SPIRVCrossDecorationBuiltInDispatchBase); |
| 641 |
get_entry_point().interface_variables.push_back(var_id); |
| 642 |
} |
| 643 |
else |
| 644 |
{ |
| 645 |
// Otherwise, we need to fall back to a good ol' fashioned buffer. |
| 646 |
uint32_t offset = ir.increase_bound_by(2); |
| 647 |
var_id = offset; |
| 648 |
uint32_t type_id = offset + 1; |
| 649 |
|
| 650 |
SPIRType var_type = get<SPIRType>(workgroup_id_type); |
| 651 |
var_type.storage = StorageClassUniform; |
| 652 |
set<SPIRType>(type_id, var_type); |
| 653 |
|
| 654 |
set<SPIRVariable>(var_id, type_id, StorageClassUniform); |
| 655 |
// This should never match anything. |
| 656 |
set_decoration(var_id, DecorationDescriptorSet, ~(5u)); |
| 657 |
set_decoration(var_id, DecorationBinding, msl_options.indirect_params_buffer_index); |
| 658 |
set_extended_decoration(var_id, SPIRVCrossDecorationResourceIndexPrimary, |
| 659 |
msl_options.indirect_params_buffer_index); |
| 660 |
} |
| 661 |
set_name(var_id, "spvDispatchBase"); |
| 662 |
builtin_dispatch_base_id = var_id; |
| 663 |
} |
| 664 |
|
| 665 |
if (need_sample_mask && !does_shader_write_sample_mask) |
| 666 |
{ |
| 667 |
uint32_t offset = ir.increase_bound_by(2); |
| 668 |
uint32_t var_id = offset + 1; |
| 669 |
|
| 670 |
// Create gl_SampleMask. |
| 671 |
SPIRType uint_type_ptr_out; |
| 672 |
uint_type_ptr_out = get_uint_type(); |
| 673 |
uint_type_ptr_out.pointer = true; |
| 674 |
uint_type_ptr_out.parent_type = get_uint_type_id(); |
| 675 |
uint_type_ptr_out.storage = StorageClassOutput; |
| 676 |
|
| 677 |
auto &ptr_out_type = set<SPIRType>(offset, uint_type_ptr_out); |
| 678 |
ptr_out_type.self = get_uint_type_id(); |
| 679 |
set<SPIRVariable>(var_id, offset, StorageClassOutput); |
| 680 |
set_decoration(var_id, DecorationBuiltIn, BuiltInSampleMask); |
| 681 |
builtin_sample_mask_id = var_id; |
| 682 |
mark_implicit_builtin(StorageClassOutput, BuiltInSampleMask, var_id); |
| 683 |
} |
| 684 |
} |
| 685 |
|
| 686 |
if (needs_swizzle_buffer_def) |
| 687 |
{ |
| 688 |
uint32_t var_id = build_constant_uint_array_pointer(); |
| 689 |
set_name(var_id, "spvSwizzleConstants"); |
| 690 |
// This should never match anything. |
| 691 |
set_decoration(var_id, DecorationDescriptorSet, kSwizzleBufferBinding); |
| 692 |
set_decoration(var_id, DecorationBinding, msl_options.swizzle_buffer_index); |
| 693 |
set_extended_decoration(var_id, SPIRVCrossDecorationResourceIndexPrimary, msl_options.swizzle_buffer_index); |
| 694 |
swizzle_buffer_id = var_id; |
| 695 |
} |
| 696 |
|
| 697 |
if (!buffers_requiring_array_length.empty()) |
| 698 |
{ |
| 699 |
uint32_t var_id = build_constant_uint_array_pointer(); |
| 700 |
set_name(var_id, "spvBufferSizeConstants"); |
| 701 |
// This should never match anything. |
| 702 |
set_decoration(var_id, DecorationDescriptorSet, kBufferSizeBufferBinding); |
| 703 |
set_decoration(var_id, DecorationBinding, msl_options.buffer_size_buffer_index); |
| 704 |
set_extended_decoration(var_id, SPIRVCrossDecorationResourceIndexPrimary, msl_options.buffer_size_buffer_index); |
| 705 |
buffer_size_buffer_id = var_id; |
| 706 |
} |
| 707 |
|
| 708 |
if (needs_view_mask_buffer()) |
| 709 |
{ |
| 710 |
uint32_t var_id = build_constant_uint_array_pointer(); |
| 711 |
set_name(var_id, "spvViewMask"); |
| 712 |
// This should never match anything. |
| 713 |
set_decoration(var_id, DecorationDescriptorSet, ~(4u)); |
| 714 |
set_decoration(var_id, DecorationBinding, msl_options.view_mask_buffer_index); |
| 715 |
set_extended_decoration(var_id, SPIRVCrossDecorationResourceIndexPrimary, msl_options.view_mask_buffer_index); |
| 716 |
view_mask_buffer_id = var_id; |
| 717 |
} |
| 718 |
|
| 719 |
if (!buffers_requiring_dynamic_offset.empty()) |
| 720 |
{ |
| 721 |
uint32_t var_id = build_constant_uint_array_pointer(); |
| 722 |
set_name(var_id, "spvDynamicOffsets"); |
| 723 |
// This should never match anything. |
| 724 |
set_decoration(var_id, DecorationDescriptorSet, ~(5u)); |
| 725 |
set_decoration(var_id, DecorationBinding, msl_options.dynamic_offsets_buffer_index); |
| 726 |
set_extended_decoration(var_id, SPIRVCrossDecorationResourceIndexPrimary, |
| 727 |
msl_options.dynamic_offsets_buffer_index); |
| 728 |
dynamic_offsets_buffer_id = var_id; |
| 729 |
} |
| 730 |
} |
| 731 |
|
| 732 |
// Checks if the specified builtin variable (e.g. gl_InstanceIndex) is marked as active. |
| 733 |
// If not, it marks it as active and forces a recompilation. |
| 734 |
// This might be used when the optimization of inactive builtins was too optimistic (e.g. when "spvOut" is emitted). |
| 735 |
void CompilerMSL::ensure_builtin(spv::StorageClass storage, spv::BuiltIn builtin) |
| 736 |
{ |
| 737 |
Bitset *active_builtins = nullptr; |
| 738 |
switch (storage) |
| 739 |
{ |
| 740 |
case StorageClassInput: |
| 741 |
active_builtins = &active_input_builtins; |
| 742 |
break; |
| 743 |
|
| 744 |
case StorageClassOutput: |
| 745 |
active_builtins = &active_output_builtins; |
| 746 |
break; |
| 747 |
|
| 748 |
default: |
| 749 |
break; |
| 750 |
} |
| 751 |
|
| 752 |
// At this point, the specified builtin variable must have already been declared in the entry point. |
| 753 |
// If not, mark as active and force recompile. |
| 754 |
if (active_builtins != nullptr && !active_builtins->get(builtin)) |
| 755 |
{ |
| 756 |
active_builtins->set(builtin); |
| 757 |
force_recompile(); |
| 758 |
} |
| 759 |
} |
| 760 |
|
| 761 |
void CompilerMSL::mark_implicit_builtin(StorageClass storage, BuiltIn builtin, uint32_t id) |
| 762 |
{ |
| 763 |
Bitset *active_builtins = nullptr; |
| 764 |
switch (storage) |
| 765 |
{ |
| 766 |
case StorageClassInput: |
| 767 |
active_builtins = &active_input_builtins; |
| 768 |
break; |
| 769 |
|
| 770 |
case StorageClassOutput: |
| 771 |
active_builtins = &active_output_builtins; |
| 772 |
break; |
| 773 |
|
| 774 |
default: |
| 775 |
break; |
| 776 |
} |
| 777 |
|
| 778 |
assert(active_builtins != nullptr); |
| 779 |
active_builtins->set(builtin); |
| 780 |
|
| 781 |
auto &var = get_entry_point().interface_variables; |
| 782 |
if (find(begin(var), end(var), VariableID(id)) == end(var)) |
| 783 |
var.push_back(id); |
| 784 |
} |
| 785 |
|
| 786 |
uint32_t CompilerMSL::build_constant_uint_array_pointer() |
| 787 |
{ |
| 788 |
uint32_t offset = ir.increase_bound_by(3); |
| 789 |
uint32_t type_ptr_id = offset; |
| 790 |
uint32_t type_ptr_ptr_id = offset + 1; |
| 791 |
uint32_t var_id = offset + 2; |
| 792 |
|
| 793 |
// Create a buffer to hold extra data, including the swizzle constants. |
| 794 |
SPIRType uint_type_pointer = get_uint_type(); |
| 795 |
uint_type_pointer.pointer = true; |
| 796 |
uint_type_pointer.pointer_depth = 1; |
| 797 |
uint_type_pointer.parent_type = get_uint_type_id(); |
| 798 |
uint_type_pointer.storage = StorageClassUniform; |
| 799 |
set<SPIRType>(type_ptr_id, uint_type_pointer); |
| 800 |
set_decoration(type_ptr_id, DecorationArrayStride, 4); |
| 801 |
|
| 802 |
SPIRType uint_type_pointer2 = uint_type_pointer; |
| 803 |
uint_type_pointer2.pointer_depth++; |
| 804 |
uint_type_pointer2.parent_type = type_ptr_id; |
| 805 |
set<SPIRType>(type_ptr_ptr_id, uint_type_pointer2); |
| 806 |
|
| 807 |
set<SPIRVariable>(var_id, type_ptr_ptr_id, StorageClassUniformConstant); |
| 808 |
return var_id; |
| 809 |
} |
| 810 |
|
| 811 |
static string create_sampler_address(const char *prefix, MSLSamplerAddress addr) |
| 812 |
{ |
| 813 |
switch (addr) |
| 814 |
{ |
| 815 |
case MSL_SAMPLER_ADDRESS_CLAMP_TO_EDGE: |
| 816 |
return join(prefix, "address::clamp_to_edge"); |
| 817 |
case MSL_SAMPLER_ADDRESS_CLAMP_TO_ZERO: |
| 818 |
return join(prefix, "address::clamp_to_zero"); |
| 819 |
case MSL_SAMPLER_ADDRESS_CLAMP_TO_BORDER: |
| 820 |
return join(prefix, "address::clamp_to_border"); |
| 821 |
case MSL_SAMPLER_ADDRESS_REPEAT: |
| 822 |
return join(prefix, "address::repeat"); |
| 823 |
case MSL_SAMPLER_ADDRESS_MIRRORED_REPEAT: |
| 824 |
return join(prefix, "address::mirrored_repeat"); |
| 825 |
default: |
| 826 |
SPIRV_CROSS_THROW("Invalid sampler addressing mode."); |
| 827 |
} |
| 828 |
} |
| 829 |
|
| 830 |
SPIRType &CompilerMSL::get_stage_in_struct_type() |
| 831 |
{ |
| 832 |
auto &si_var = get<SPIRVariable>(stage_in_var_id); |
| 833 |
return get_variable_data_type(si_var); |
| 834 |
} |
| 835 |
|
| 836 |
SPIRType &CompilerMSL::get_stage_out_struct_type() |
| 837 |
{ |
| 838 |
auto &so_var = get<SPIRVariable>(stage_out_var_id); |
| 839 |
return get_variable_data_type(so_var); |
| 840 |
} |
| 841 |
|
| 842 |
SPIRType &CompilerMSL::get_patch_stage_in_struct_type() |
| 843 |
{ |
| 844 |
auto &si_var = get<SPIRVariable>(patch_stage_in_var_id); |
| 845 |
return get_variable_data_type(si_var); |
| 846 |
} |
| 847 |
|
| 848 |
SPIRType &CompilerMSL::get_patch_stage_out_struct_type() |
| 849 |
{ |
| 850 |
auto &so_var = get<SPIRVariable>(patch_stage_out_var_id); |
| 851 |
return get_variable_data_type(so_var); |
| 852 |
} |
| 853 |
|
| 854 |
std::string CompilerMSL::get_tess_factor_struct_name() |
| 855 |
{ |
| 856 |
if (get_entry_point().flags.get(ExecutionModeTriangles)) |
| 857 |
return "MTLTriangleTessellationFactorsHalf"; |
| 858 |
return "MTLQuadTessellationFactorsHalf"; |
| 859 |
} |
| 860 |
|
| 861 |
SPIRType &CompilerMSL::get_uint_type() |
| 862 |
{ |
| 863 |
return get<SPIRType>(get_uint_type_id()); |
| 864 |
} |
| 865 |
|
| 866 |
uint32_t CompilerMSL::get_uint_type_id() |
| 867 |
{ |
| 868 |
if (uint_type_id != 0) |
| 869 |
return uint_type_id; |
| 870 |
|
| 871 |
uint_type_id = ir.increase_bound_by(1); |
| 872 |
|
| 873 |
SPIRType type; |
| 874 |
type.basetype = SPIRType::UInt; |
| 875 |
type.width = 32; |
| 876 |
set<SPIRType>(uint_type_id, type); |
| 877 |
return uint_type_id; |
| 878 |
} |
| 879 |
|
| 880 |
void CompilerMSL::emit_entry_point_declarations() |
| 881 |
{ |
| 882 |
// FIXME: Get test coverage here ... |
| 883 |
// Constant arrays of non-primitive types (i.e. matrices) won't link properly into Metal libraries |
| 884 |
declare_complex_constant_arrays(); |
| 885 |
|
| 886 |
// Emit constexpr samplers here. |
| 887 |
for (auto &samp : constexpr_samplers_by_id) |
| 888 |
{ |
| 889 |
auto &var = get<SPIRVariable>(samp.first); |
| 890 |
auto &type = get<SPIRType>(var.basetype); |
| 891 |
if (type.basetype == SPIRType::Sampler) |
| 892 |
add_resource_name(samp.first); |
| 893 |
|
| 894 |
SmallVector<string> args; |
| 895 |
auto &s = samp.second; |
| 896 |
|
| 897 |
if (s.coord != MSL_SAMPLER_COORD_NORMALIZED) |
| 898 |
args.push_back("coord::pixel"); |
| 899 |
|
| 900 |
if (s.min_filter == s.mag_filter) |
| 901 |
{ |
| 902 |
if (s.min_filter != MSL_SAMPLER_FILTER_NEAREST) |
| 903 |
args.push_back("filter::linear"); |
| 904 |
} |
| 905 |
else |
| 906 |
{ |
| 907 |
if (s.min_filter != MSL_SAMPLER_FILTER_NEAREST) |
| 908 |
args.push_back("min_filter::linear"); |
| 909 |
if (s.mag_filter != MSL_SAMPLER_FILTER_NEAREST) |
| 910 |
args.push_back("mag_filter::linear"); |
| 911 |
} |
| 912 |
|
| 913 |
switch (s.mip_filter) |
| 914 |
{ |
| 915 |
case MSL_SAMPLER_MIP_FILTER_NONE: |
| 916 |
// Default |
| 917 |
break; |
| 918 |
case MSL_SAMPLER_MIP_FILTER_NEAREST: |
| 919 |
args.push_back("mip_filter::nearest"); |
| 920 |
break; |
| 921 |
case MSL_SAMPLER_MIP_FILTER_LINEAR: |
| 922 |
args.push_back("mip_filter::linear"); |
| 923 |
break; |
| 924 |
default: |
| 925 |
SPIRV_CROSS_THROW("Invalid mip filter."); |
| 926 |
} |
| 927 |
|
| 928 |
if (s.s_address == s.t_address && s.s_address == s.r_address) |
| 929 |
{ |
| 930 |
if (s.s_address != MSL_SAMPLER_ADDRESS_CLAMP_TO_EDGE) |
| 931 |
args.push_back(create_sampler_address("", s.s_address)); |
| 932 |
} |
| 933 |
else |
| 934 |
{ |
| 935 |
if (s.s_address != MSL_SAMPLER_ADDRESS_CLAMP_TO_EDGE) |
| 936 |
args.push_back(create_sampler_address("s_", s.s_address)); |
| 937 |
if (s.t_address != MSL_SAMPLER_ADDRESS_CLAMP_TO_EDGE) |
| 938 |
args.push_back(create_sampler_address("t_", s.t_address)); |
| 939 |
if (s.r_address != MSL_SAMPLER_ADDRESS_CLAMP_TO_EDGE) |
| 940 |
args.push_back(create_sampler_address("r_", s.r_address)); |
| 941 |
} |
| 942 |
|
| 943 |
if (s.compare_enable) |
| 944 |
{ |
| 945 |
switch (s.compare_func) |
| 946 |
{ |
| 947 |
case MSL_SAMPLER_COMPARE_FUNC_ALWAYS: |
| 948 |
args.push_back("compare_func::always"); |
| 949 |
break; |
| 950 |
case MSL_SAMPLER_COMPARE_FUNC_NEVER: |
| 951 |
args.push_back("compare_func::never"); |
| 952 |
break; |
| 953 |
case MSL_SAMPLER_COMPARE_FUNC_EQUAL: |
| 954 |
args.push_back("compare_func::equal"); |
| 955 |
break; |
| 956 |
case MSL_SAMPLER_COMPARE_FUNC_NOT_EQUAL: |
| 957 |
args.push_back("compare_func::not_equal"); |
| 958 |
break; |
| 959 |
case MSL_SAMPLER_COMPARE_FUNC_LESS: |
| 960 |
args.push_back("compare_func::less"); |
| 961 |
break; |
| 962 |
case MSL_SAMPLER_COMPARE_FUNC_LESS_EQUAL: |
| 963 |
args.push_back("compare_func::less_equal"); |
| 964 |
break; |
| 965 |
case MSL_SAMPLER_COMPARE_FUNC_GREATER: |
| 966 |
args.push_back("compare_func::greater"); |
| 967 |
break; |
| 968 |
case MSL_SAMPLER_COMPARE_FUNC_GREATER_EQUAL: |
| 969 |
args.push_back("compare_func::greater_equal"); |
| 970 |
break; |
| 971 |
default: |
| 972 |
SPIRV_CROSS_THROW("Invalid sampler compare function."); |
| 973 |
} |
| 974 |
} |
| 975 |
|
| 976 |
if (s.s_address == MSL_SAMPLER_ADDRESS_CLAMP_TO_BORDER || s.t_address == MSL_SAMPLER_ADDRESS_CLAMP_TO_BORDER || |
| 977 |
s.r_address == MSL_SAMPLER_ADDRESS_CLAMP_TO_BORDER) |
| 978 |
{ |
| 979 |
switch (s.border_color) |
| 980 |
{ |
| 981 |
case MSL_SAMPLER_BORDER_COLOR_OPAQUE_BLACK: |
| 982 |
args.push_back("border_color::opaque_black"); |
| 983 |
break; |
| 984 |
case MSL_SAMPLER_BORDER_COLOR_OPAQUE_WHITE: |
| 985 |
args.push_back("border_color::opaque_white"); |
| 986 |
break; |
| 987 |
case MSL_SAMPLER_BORDER_COLOR_TRANSPARENT_BLACK: |
| 988 |
args.push_back("border_color::transparent_black"); |
| 989 |
break; |
| 990 |
default: |
| 991 |
SPIRV_CROSS_THROW("Invalid sampler border color."); |
| 992 |
} |
| 993 |
} |
| 994 |
|
| 995 |
if (s.anisotropy_enable) |
| 996 |
args.push_back(join("max_anisotropy(", s.max_anisotropy, ")")); |
| 997 |
if (s.lod_clamp_enable) |
| 998 |
{ |
| 999 |
args.push_back(join("lod_clamp(", convert_to_string(s.lod_clamp_min, current_locale_radix_character), ", ", |
| 1000 |
convert_to_string(s.lod_clamp_max, current_locale_radix_character), ")")); |
| 1001 |
} |
| 1002 |
|
| 1003 |
// If we would emit no arguments, then omit the parentheses entirely. Otherwise, |
| 1004 |
// we'll wind up with a "most vexing parse" situation. |
| 1005 |
if (args.empty()) |
| 1006 |
statement("constexpr sampler ", |
| 1007 |
type.basetype == SPIRType::SampledImage ? to_sampler_expression(samp.first) : to_name(samp.first), |
| 1008 |
";"); |
| 1009 |
else |
| 1010 |
statement("constexpr sampler ", |
| 1011 |
type.basetype == SPIRType::SampledImage ? to_sampler_expression(samp.first) : to_name(samp.first), |
| 1012 |
"(", merge(args), ");"); |
| 1013 |
} |
| 1014 |
|
| 1015 |
// Emit dynamic buffers here. |
| 1016 |
for (auto &dynamic_buffer : buffers_requiring_dynamic_offset) |
| 1017 |
{ |
| 1018 |
if (!dynamic_buffer.second.second) |
| 1019 |
{ |
| 1020 |
// Could happen if no buffer was used at requested binding point. |
| 1021 |
continue; |
| 1022 |
} |
| 1023 |
|
| 1024 |
const auto &var = get<SPIRVariable>(dynamic_buffer.second.second); |
| 1025 |
uint32_t var_id = var.self; |
| 1026 |
const auto &type = get_variable_data_type(var); |
| 1027 |
string name = to_name(var.self); |
| 1028 |
uint32_t desc_set = get_decoration(var.self, DecorationDescriptorSet); |
| 1029 |
uint32_t arg_id = argument_buffer_ids[desc_set]; |
| 1030 |
uint32_t base_index = dynamic_buffer.second.first; |
| 1031 |
|
| 1032 |
if (!type.array.empty()) |
| 1033 |
{ |
| 1034 |
// This is complicated, because we need to support arrays of arrays. |
| 1035 |
// And it's even worse if the outermost dimension is a runtime array, because now |
| 1036 |
// all this complicated goop has to go into the shader itself. (FIXME) |
| 1037 |
if (!type.array[type.array.size() - 1]) |
| 1038 |
SPIRV_CROSS_THROW("Runtime arrays with dynamic offsets are not supported yet."); |
| 1039 |
else |
| 1040 |
{ |
| 1041 |
is_using_builtin_array = true; |
| 1042 |
statement(get_argument_address_space(var), " ", type_to_glsl(type), "* ", to_restrict(var_id), name, |
| 1043 |
type_to_array_glsl(type), " ="); |
| 1044 |
|
| 1045 |
uint32_t dim = uint32_t(type.array.size()); |
| 1046 |
uint32_t j = 0; |
| 1047 |
for (SmallVector<uint32_t> indices(type.array.size()); |
| 1048 |
indices[type.array.size() - 1] < to_array_size_literal(type); j++) |
| 1049 |
{ |
| 1050 |
while (dim > 0) |
| 1051 |
{ |
| 1052 |
begin_scope(); |
| 1053 |
--dim; |
| 1054 |
} |
| 1055 |
|
| 1056 |
string arrays; |
| 1057 |
for (uint32_t i = uint32_t(type.array.size()); i; --i) |
| 1058 |
arrays += join("[", indices[i - 1], "]"); |
| 1059 |
statement("(", get_argument_address_space(var), " ", type_to_glsl(type), "* ", |
| 1060 |
to_restrict(var_id, false), ")((", get_argument_address_space(var), " char* ", |
| 1061 |
to_restrict(var_id, false), ")", to_name(arg_id), ".", ensure_valid_name(name, "m"), |
| 1062 |
arrays, " + ", to_name(dynamic_offsets_buffer_id), "[", base_index + j, "]),"); |
| 1063 |
|
| 1064 |
while (++indices[dim] >= to_array_size_literal(type, dim) && dim < type.array.size() - 1) |
| 1065 |
{ |
| 1066 |
end_scope(","); |
| 1067 |
indices[dim++] = 0; |
| 1068 |
} |
| 1069 |
} |
| 1070 |
end_scope_decl(); |
| 1071 |
statement_no_indent(""); |
| 1072 |
is_using_builtin_array = false; |
| 1073 |
} |
| 1074 |
} |
| 1075 |
else |
| 1076 |
{ |
| 1077 |
statement(get_argument_address_space(var), " auto& ", to_restrict(var_id), name, " = *(", |
| 1078 |
get_argument_address_space(var), " ", type_to_glsl(type), "* ", to_restrict(var_id, false), ")((", |
| 1079 |
get_argument_address_space(var), " char* ", to_restrict(var_id, false), ")", to_name(arg_id), ".", |
| 1080 |
ensure_valid_name(name, "m"), " + ", to_name(dynamic_offsets_buffer_id), "[", base_index, "]);"); |
| 1081 |
} |
| 1082 |
} |
| 1083 |
|
| 1084 |
// Emit buffer arrays here. |
| 1085 |
for (uint32_t array_id : buffer_arrays) |
| 1086 |
{ |
| 1087 |
const auto &var = get<SPIRVariable>(array_id); |
| 1088 |
const auto &type = get_variable_data_type(var); |
| 1089 |
const auto &buffer_type = get_variable_element_type(var); |
| 1090 |
string name = to_name(array_id); |
| 1091 |
statement(get_argument_address_space(var), " ", type_to_glsl(buffer_type), "* ", to_restrict(array_id), name, |
| 1092 |
"[] ="); |
| 1093 |
begin_scope(); |
| 1094 |
for (uint32_t i = 0; i < to_array_size_literal(type); ++i) |
| 1095 |
statement(name, "_", i, ","); |
| 1096 |
end_scope_decl(); |
| 1097 |
statement_no_indent(""); |
| 1098 |
} |
| 1099 |
// For some reason, without this, we end up emitting the arrays twice. |
| 1100 |
buffer_arrays.clear(); |
| 1101 |
|
| 1102 |
// Emit disabled fragment outputs. |
| 1103 |
std::sort(disabled_frag_outputs.begin(), disabled_frag_outputs.end()); |
| 1104 |
for (uint32_t var_id : disabled_frag_outputs) |
| 1105 |
{ |
| 1106 |
auto &var = get<SPIRVariable>(var_id); |
| 1107 |
add_local_variable_name(var_id); |
| 1108 |
statement(variable_decl(var), ";"); |
| 1109 |
var.deferred_declaration = false; |
| 1110 |
} |
| 1111 |
} |
| 1112 |
|
| 1113 |
string CompilerMSL::compile() |
| 1114 |
{ |
| 1115 |
ir.fixup_reserved_names(); |
| 1116 |
|
| 1117 |
// Do not deal with GLES-isms like precision, older extensions and such. |
| 1118 |
options.vulkan_semantics = true; |
| 1119 |
options.es = false; |
| 1120 |
options.version = 450; |
| 1121 |
backend.null_pointer_literal = "nullptr"; |
| 1122 |
backend.float_literal_suffix = false; |
| 1123 |
backend.uint32_t_literal_suffix = true; |
| 1124 |
backend.int16_t_literal_suffix = ""; |
| 1125 |
backend.uint16_t_literal_suffix = ""; |
| 1126 |
backend.basic_int_type = "int"; |
| 1127 |
backend.basic_uint_type = "uint"; |
| 1128 |
backend.basic_int8_type = "char"; |
| 1129 |
backend.basic_uint8_type = "uchar"; |
| 1130 |
backend.basic_int16_type = "short"; |
| 1131 |
backend.basic_uint16_type = "ushort"; |
| 1132 |
backend.discard_literal = "discard_fragment()"; |
| 1133 |
backend.demote_literal = "discard_fragment()"; |
| 1134 |
backend.boolean_mix_function = "select"; |
| 1135 |
backend.swizzle_is_function = false; |
| 1136 |
backend.shared_is_implied = false; |
| 1137 |
backend.use_initializer_list = true; |
| 1138 |
backend.use_typed_initializer_list = true; |
| 1139 |
backend.native_row_major_matrix = false; |
| 1140 |
backend.unsized_array_supported = false; |
| 1141 |
backend.can_declare_arrays_inline = false; |
| 1142 |
backend.allow_truncated_access_chain = true; |
| 1143 |
backend.comparison_image_samples_scalar = true; |
| 1144 |
backend.native_pointers = true; |
| 1145 |
backend.nonuniform_qualifier = ""; |
| 1146 |
backend.support_small_type_sampling_result = true; |
| 1147 |
backend.supports_empty_struct = true; |
| 1148 |
|
| 1149 |
// Allow Metal to use the array<T> template unless we force it off. |
| 1150 |
backend.can_return_array = !msl_options.force_native_arrays; |
| 1151 |
backend.array_is_value_type = !msl_options.force_native_arrays; |
| 1152 |
// Arrays which are part of buffer objects are never considered to be native arrays. |
| 1153 |
backend.buffer_offset_array_is_value_type = false; |
| 1154 |
|
| 1155 |
capture_output_to_buffer = msl_options.capture_output_to_buffer; |
| 1156 |
is_rasterization_disabled = msl_options.disable_rasterization || capture_output_to_buffer; |
| 1157 |
|
| 1158 |
// Initialize array here rather than constructor, MSVC 2013 workaround. |
| 1159 |
for (auto &id : next_metal_resource_ids) |
| 1160 |
id = 0; |
| 1161 |
|
| 1162 |
fixup_type_alias(); |
| 1163 |
replace_illegal_names(); |
| 1164 |
|
| 1165 |
build_function_control_flow_graphs_and_analyze(); |
| 1166 |
update_active_builtins(); |
| 1167 |
analyze_image_and_sampler_usage(); |
| 1168 |
analyze_sampled_image_usage(); |
| 1169 |
analyze_interlocked_resource_usage(); |
| 1170 |
preprocess_op_codes(); |
| 1171 |
build_implicit_builtins(); |
| 1172 |
|
| 1173 |
fixup_image_load_store_access(); |
| 1174 |
|
| 1175 |
set_enabled_interface_variables(get_active_interface_variables()); |
| 1176 |
if (msl_options.force_active_argument_buffer_resources) |
| 1177 |
activate_argument_buffer_resources(); |
| 1178 |
|
| 1179 |
if (swizzle_buffer_id) |
| 1180 |
active_interface_variables.insert(swizzle_buffer_id); |
| 1181 |
if (buffer_size_buffer_id) |
| 1182 |
active_interface_variables.insert(buffer_size_buffer_id); |
| 1183 |
if (view_mask_buffer_id) |
| 1184 |
active_interface_variables.insert(view_mask_buffer_id); |
| 1185 |
if (dynamic_offsets_buffer_id) |
| 1186 |
active_interface_variables.insert(dynamic_offsets_buffer_id); |
| 1187 |
if (builtin_layer_id) |
| 1188 |
active_interface_variables.insert(builtin_layer_id); |
| 1189 |
if (builtin_dispatch_base_id && !msl_options.supports_msl_version(1, 2)) |
| 1190 |
active_interface_variables.insert(builtin_dispatch_base_id); |
| 1191 |
if (builtin_sample_mask_id) |
| 1192 |
active_interface_variables.insert(builtin_sample_mask_id); |
| 1193 |
|
| 1194 |
// Create structs to hold input, output and uniform variables. |
| 1195 |
// Do output first to ensure out. is declared at top of entry function. |
| 1196 |
qual_pos_var_name = ""; |
| 1197 |
stage_out_var_id = add_interface_block(StorageClassOutput); |
| 1198 |
patch_stage_out_var_id = add_interface_block(StorageClassOutput, true); |
| 1199 |
stage_in_var_id = add_interface_block(StorageClassInput); |
| 1200 |
if (get_execution_model() == ExecutionModelTessellationEvaluation) |
| 1201 |
patch_stage_in_var_id = add_interface_block(StorageClassInput, true); |
| 1202 |
|
| 1203 |
if (get_execution_model() == ExecutionModelTessellationControl) |
| 1204 |
stage_out_ptr_var_id = add_interface_block_pointer(stage_out_var_id, StorageClassOutput); |
| 1205 |
if (is_tessellation_shader()) |
| 1206 |
stage_in_ptr_var_id = add_interface_block_pointer(stage_in_var_id, StorageClassInput); |
| 1207 |
|
| 1208 |
// Metal vertex functions that define no output must disable rasterization and return void. |
| 1209 |
if (!stage_out_var_id) |
| 1210 |
is_rasterization_disabled = true; |
| 1211 |
|
| 1212 |
// Convert the use of global variables to recursively-passed function parameters |
| 1213 |
localize_global_variables(); |
| 1214 |
extract_global_variables_from_functions(); |
| 1215 |
|
| 1216 |
// Mark any non-stage-in structs to be tightly packed. |
| 1217 |
mark_packable_structs(); |
| 1218 |
reorder_type_alias(); |
| 1219 |
|
| 1220 |
// Add fixup hooks required by shader inputs and outputs. This needs to happen before |
| 1221 |
// the loop, so the hooks aren't added multiple times. |
| 1222 |
fix_up_shader_inputs_outputs(); |
| 1223 |
|
| 1224 |
// If we are using argument buffers, we create argument buffer structures for them here. |
| 1225 |
// These buffers will be used in the entry point, not the individual resources. |
| 1226 |
if (msl_options.argument_buffers) |
| 1227 |
{ |
| 1228 |
if (!msl_options.supports_msl_version(2, 0)) |
| 1229 |
SPIRV_CROSS_THROW("Argument buffers can only be used with MSL 2.0 and up."); |
| 1230 |
analyze_argument_buffers(); |
| 1231 |
} |
| 1232 |
|
| 1233 |
uint32_t pass_count = 0; |
| 1234 |
do |
| 1235 |
{ |
| 1236 |
if (pass_count >= 3) |
| 1237 |
SPIRV_CROSS_THROW("Over 3 compilation loops detected. Must be a bug!"); |
| 1238 |
|
| 1239 |
reset(); |
| 1240 |
|
| 1241 |
// Start bindings at zero. |
| 1242 |
next_metal_resource_index_buffer = 0; |
| 1243 |
next_metal_resource_index_texture = 0; |
| 1244 |
next_metal_resource_index_sampler = 0; |
| 1245 |
for (auto &id : next_metal_resource_ids) |
| 1246 |
id = 0; |
| 1247 |
|
| 1248 |
// Move constructor for this type is broken on GCC 4.9 ... |
| 1249 |
buffer.reset(); |
| 1250 |
|
| 1251 |
emit_header(); |
| 1252 |
emit_custom_templates(); |
| 1253 |
emit_specialization_constants_and_structs(); |
| 1254 |
emit_resources(); |
| 1255 |
emit_custom_functions(); |
| 1256 |
emit_function(get<SPIRFunction>(ir.default_entry_point), Bitset()); |
| 1257 |
|
| 1258 |
pass_count++; |
| 1259 |
} while (is_forcing_recompilation()); |
| 1260 |
|
| 1261 |
return buffer.str(); |
| 1262 |
} |
| 1263 |
|
| 1264 |
// Register the need to output any custom functions. |
| 1265 |
void CompilerMSL::preprocess_op_codes() |
| 1266 |
{ |
| 1267 |
OpCodePreprocessor preproc(*this); |
| 1268 |
traverse_all_reachable_opcodes(get<SPIRFunction>(ir.default_entry_point), preproc); |
| 1269 |
|
| 1270 |
suppress_missing_prototypes = preproc.suppress_missing_prototypes; |
| 1271 |
|
| 1272 |
if (preproc.uses_atomics) |
| 1273 |
{ |
| 1274 |
add_header_line("#include <metal_atomic>"); |
| 1275 |
add_pragma_line("#pragma clang diagnostic ignored \"-Wunused-variable\""); |
| 1276 |
} |
| 1277 |
|
| 1278 |
// Before MSL 2.1 (2.2 for textures), Metal vertex functions that write to |
| 1279 |
// resources must disable rasterization and return void. |
| 1280 |
if (preproc.uses_resource_write) |
| 1281 |
is_rasterization_disabled = true; |
| 1282 |
|
| 1283 |
// Tessellation control shaders are run as compute functions in Metal, and so |
| 1284 |
// must capture their output to a buffer. |
| 1285 |
if (get_execution_model() == ExecutionModelTessellationControl || |
| 1286 |
(get_execution_model() == ExecutionModelVertex && msl_options.vertex_for_tessellation)) |
| 1287 |
{ |
| 1288 |
is_rasterization_disabled = true; |
| 1289 |
capture_output_to_buffer = true; |
| 1290 |
} |
| 1291 |
|
| 1292 |
if (preproc.needs_subgroup_invocation_id) |
| 1293 |
needs_subgroup_invocation_id = true; |
| 1294 |
if (preproc.needs_subgroup_size) |
| 1295 |
needs_subgroup_size = true; |
| 1296 |
if (preproc.needs_sample_id) |
| 1297 |
needs_sample_id = true; |
| 1298 |
} |
| 1299 |
|
| 1300 |
// Move the Private and Workgroup global variables to the entry function. |
| 1301 |
// Non-constant variables cannot have global scope in Metal. |
| 1302 |
void CompilerMSL::localize_global_variables() |
| 1303 |
{ |
| 1304 |
auto &entry_func = get<SPIRFunction>(ir.default_entry_point); |
| 1305 |
auto iter = global_variables.begin(); |
| 1306 |
while (iter != global_variables.end()) |
| 1307 |
{ |
| 1308 |
uint32_t v_id = *iter; |
| 1309 |
auto &var = get<SPIRVariable>(v_id); |
| 1310 |
if (var.storage == StorageClassPrivate || var.storage == StorageClassWorkgroup) |
| 1311 |
{ |
| 1312 |
if (!variable_is_lut(var)) |
| 1313 |
entry_func.add_local_variable(v_id); |
| 1314 |
iter = global_variables.erase(iter); |
| 1315 |
} |
| 1316 |
else |
| 1317 |
iter++; |
| 1318 |
} |
| 1319 |
} |
| 1320 |
|
| 1321 |
// For any global variable accessed directly by a function, |
| 1322 |
// extract that variable and add it as an argument to that function. |
| 1323 |
void CompilerMSL::extract_global_variables_from_functions() |
| 1324 |
{ |
| 1325 |
// Uniforms |
| 1326 |
unordered_set<uint32_t> global_var_ids; |
| 1327 |
ir.for_each_typed_id<SPIRVariable>([&](uint32_t, SPIRVariable &var) { |
| 1328 |
if (var.storage == StorageClassInput || var.storage == StorageClassOutput || |
| 1329 |
var.storage == StorageClassUniform || var.storage == StorageClassUniformConstant || |
| 1330 |
var.storage == StorageClassPushConstant || var.storage == StorageClassStorageBuffer) |
| 1331 |
{ |
| 1332 |
global_var_ids.insert(var.self); |
| 1333 |
} |
| 1334 |
}); |
| 1335 |
|
| 1336 |
// Local vars that are declared in the main function and accessed directly by a function |
| 1337 |
auto &entry_func = get<SPIRFunction>(ir.default_entry_point); |
| 1338 |
for (auto &var : entry_func.local_variables) |
| 1339 |
if (get<SPIRVariable>(var).storage != StorageClassFunction) |
| 1340 |
global_var_ids.insert(var); |
| 1341 |
|
| 1342 |
std::set<uint32_t> added_arg_ids; |
| 1343 |
unordered_set<uint32_t> processed_func_ids; |
| 1344 |
extract_global_variables_from_function(ir.default_entry_point, added_arg_ids, global_var_ids, processed_func_ids); |
| 1345 |
} |
| 1346 |
|
| 1347 |
// MSL does not support the use of global variables for shader input content. |
| 1348 |
// For any global variable accessed directly by the specified function, extract that variable, |
| 1349 |
// add it as an argument to that function, and the arg to the added_arg_ids collection. |
| 1350 |
void CompilerMSL::extract_global_variables_from_function(uint32_t func_id, std::set<uint32_t> &added_arg_ids, |
| 1351 |
unordered_set<uint32_t> &global_var_ids, |
| 1352 |
unordered_set<uint32_t> &processed_func_ids) |
| 1353 |
{ |
| 1354 |
// Avoid processing a function more than once |
| 1355 |
if (processed_func_ids.find(func_id) != processed_func_ids.end()) |
| 1356 |
{ |
| 1357 |
// Return function global variables |
| 1358 |
added_arg_ids = function_global_vars[func_id]; |
| 1359 |
return; |
| 1360 |
} |
| 1361 |
|
| 1362 |
processed_func_ids.insert(func_id); |
| 1363 |
|
| 1364 |
auto &func = get<SPIRFunction>(func_id); |
| 1365 |
|
| 1366 |
// Recursively establish global args added to functions on which we depend. |
| 1367 |
for (auto block : func.blocks) |
| 1368 |
{ |
| 1369 |
auto &b = get<SPIRBlock>(block); |
| 1370 |
for (auto &i : b.ops) |
| 1371 |
{ |
| 1372 |
auto ops = stream(i); |
| 1373 |
auto op = static_cast<Op>(i.op); |
| 1374 |
|
| 1375 |
switch (op) |
| 1376 |
{ |
| 1377 |
case OpLoad: |
| 1378 |
case OpInBoundsAccessChain: |
| 1379 |
case OpAccessChain: |
| 1380 |
case OpPtrAccessChain: |
| 1381 |
case OpArrayLength: |
| 1382 |
{ |
| 1383 |
uint32_t base_id = ops[2]; |
| 1384 |
if (global_var_ids.find(base_id) != global_var_ids.end()) |
| 1385 |
added_arg_ids.insert(base_id); |
| 1386 |
|
| 1387 |
// Use Metal's native frame-buffer fetch API for subpass inputs. |
| 1388 |
auto &type = get<SPIRType>(ops[0]); |
| 1389 |
if (type.basetype == SPIRType::Image && type.image.dim == DimSubpassData && |
| 1390 |
(!msl_options.use_framebuffer_fetch_subpasses)) |
| 1391 |
{ |
| 1392 |
// Implicitly reads gl_FragCoord. |
| 1393 |
assert(builtin_frag_coord_id != 0); |
| 1394 |
added_arg_ids.insert(builtin_frag_coord_id); |
| 1395 |
if (msl_options.multiview) |
| 1396 |
{ |
| 1397 |
// Implicitly reads gl_ViewIndex. |
| 1398 |
assert(builtin_view_idx_id != 0); |
| 1399 |
added_arg_ids.insert(builtin_view_idx_id); |
| 1400 |
} |
| 1401 |
else if (msl_options.arrayed_subpass_input) |
| 1402 |
{ |
| 1403 |
// Implicitly reads gl_Layer. |
| 1404 |
assert(builtin_layer_id != 0); |
| 1405 |
added_arg_ids.insert(builtin_layer_id); |
| 1406 |
} |
| 1407 |
} |
| 1408 |
|
| 1409 |
break; |
| 1410 |
} |
| 1411 |
|
| 1412 |
case OpFunctionCall: |
| 1413 |
{ |
| 1414 |
// First see if any of the function call args are globals |
| 1415 |
for (uint32_t arg_idx = 3; arg_idx < i.length; arg_idx++) |
| 1416 |
{ |
| 1417 |
uint32_t arg_id = ops[arg_idx]; |
| 1418 |
if (global_var_ids.find(arg_id) != global_var_ids.end()) |
| 1419 |
added_arg_ids.insert(arg_id); |
| 1420 |
} |
| 1421 |
|
| 1422 |
// Then recurse into the function itself to extract globals used internally in the function |
| 1423 |
uint32_t inner_func_id = ops[2]; |
| 1424 |
std::set<uint32_t> inner_func_args; |
| 1425 |
extract_global_variables_from_function(inner_func_id, inner_func_args, global_var_ids, |
| 1426 |
processed_func_ids); |
| 1427 |
added_arg_ids.insert(inner_func_args.begin(), inner_func_args.end()); |
| 1428 |
break; |
| 1429 |
} |
| 1430 |
|
| 1431 |
case OpStore: |
| 1432 |
{ |
| 1433 |
uint32_t base_id = ops[0]; |
| 1434 |
if (global_var_ids.find(base_id) != global_var_ids.end()) |
| 1435 |
added_arg_ids.insert(base_id); |
| 1436 |
|
| 1437 |
uint32_t rvalue_id = ops[1]; |
| 1438 |
if (global_var_ids.find(rvalue_id) != global_var_ids.end()) |
| 1439 |
added_arg_ids.insert(rvalue_id); |
| 1440 |
|
| 1441 |
break; |
| 1442 |
} |
| 1443 |
|
| 1444 |
case OpSelect: |
| 1445 |
{ |
| 1446 |
uint32_t base_id = ops[3]; |
| 1447 |
if (global_var_ids.find(base_id) != global_var_ids.end()) |
| 1448 |
added_arg_ids.insert(base_id); |
| 1449 |
base_id = ops[4]; |
| 1450 |
if (global_var_ids.find(base_id) != global_var_ids.end()) |
| 1451 |
added_arg_ids.insert(base_id); |
| 1452 |
break; |
| 1453 |
} |
| 1454 |
|
| 1455 |
// Emulate texture2D atomic operations |
| 1456 |
case OpImageTexelPointer: |
| 1457 |
{ |
| 1458 |
// When using the pointer, we need to know which variable it is actually loaded from. |
| 1459 |
uint32_t base_id = ops[2]; |
| 1460 |
auto *var = maybe_get_backing_variable(base_id); |
| 1461 |
if (var && atomic_image_vars.count(var->self)) |
| 1462 |
{ |
| 1463 |
if (global_var_ids.find(base_id) != global_var_ids.end()) |
| 1464 |
added_arg_ids.insert(base_id); |
| 1465 |
} |
| 1466 |
break; |
| 1467 |
} |
| 1468 |
|
| 1469 |
case OpExtInst: |
| 1470 |
{ |
| 1471 |
uint32_t extension_set = ops[2]; |
| 1472 |
if (get<SPIRExtension>(extension_set).ext == SPIRExtension::GLSL) |
| 1473 |
{ |
| 1474 |
auto op_450 = static_cast<GLSLstd450>(ops[3]); |
| 1475 |
switch (op_450) |
| 1476 |
{ |
| 1477 |
case GLSLstd450InterpolateAtCentroid: |
| 1478 |
case GLSLstd450InterpolateAtSample: |
| 1479 |
case GLSLstd450InterpolateAtOffset: |
| 1480 |
{ |
| 1481 |
// For these, we really need the stage-in block. It is theoretically possible to pass the |
| 1482 |
// interpolant object, but a) doing so would require us to create an entirely new variable |
| 1483 |
// with Interpolant type, and b) if we have a struct or array, handling all the members and |
| 1484 |
// elements could get unwieldy fast. |
| 1485 |
added_arg_ids.insert(stage_in_var_id); |
| 1486 |
break; |
| 1487 |
} |
| 1488 |
default: |
| 1489 |
break; |
| 1490 |
} |
| 1491 |
} |
| 1492 |
} |
| 1493 |
|
| 1494 |
default: |
| 1495 |
break; |
| 1496 |
} |
| 1497 |
|
| 1498 |
// TODO: Add all other operations which can affect memory. |
| 1499 |
// We should consider a more unified system here to reduce boiler-plate. |
| 1500 |
// This kind of analysis is done in several places ... |
| 1501 |
} |
| 1502 |
} |
| 1503 |
|
| 1504 |
function_global_vars[func_id] = added_arg_ids; |
| 1505 |
|
| 1506 |
// Add the global variables as arguments to the function |
| 1507 |
if (func_id != ir.default_entry_point) |
| 1508 |
{ |
| 1509 |
bool added_in = false; |
| 1510 |
bool added_out = false; |
| 1511 |
for (uint32_t arg_id : added_arg_ids) |
| 1512 |
{ |
| 1513 |
auto &var = get<SPIRVariable>(arg_id); |
| 1514 |
uint32_t type_id = var.basetype; |
| 1515 |
auto *p_type = &get<SPIRType>(type_id); |
| 1516 |
BuiltIn bi_type = BuiltIn(get_decoration(arg_id, DecorationBuiltIn)); |
| 1517 |
|
| 1518 |
if (((is_tessellation_shader() && var.storage == StorageClassInput) || |
| 1519 |
(get_execution_model() == ExecutionModelTessellationControl && var.storage == StorageClassOutput)) && |
| 1520 |
!(has_decoration(arg_id, DecorationPatch) || is_patch_block(*p_type)) && |
| 1521 |
(!is_builtin_variable(var) || bi_type == BuiltInPosition || bi_type == BuiltInPointSize || |
| 1522 |
bi_type == BuiltInClipDistance || bi_type == BuiltInCullDistance || |
| 1523 |
p_type->basetype == SPIRType::Struct)) |
| 1524 |
{ |
| 1525 |
// Tessellation control shaders see inputs and per-vertex outputs as arrays. |
| 1526 |
// Similarly, tessellation evaluation shaders see per-vertex inputs as arrays. |
| 1527 |
// We collected them into a structure; we must pass the array of this |
| 1528 |
// structure to the function. |
| 1529 |
std::string name; |
| 1530 |
if (var.storage == StorageClassInput) |
| 1531 |
{ |
| 1532 |
if (added_in) |
| 1533 |
continue; |
| 1534 |
name = "gl_in"; |
| 1535 |
arg_id = stage_in_ptr_var_id; |
| 1536 |
added_in = true; |
| 1537 |
} |
| 1538 |
else if (var.storage == StorageClassOutput) |
| 1539 |
{ |
| 1540 |
if (added_out) |
| 1541 |
continue; |
| 1542 |
name = "gl_out"; |
| 1543 |
arg_id = stage_out_ptr_var_id; |
| 1544 |
added_out = true; |
| 1545 |
} |
| 1546 |
type_id = get<SPIRVariable>(arg_id).basetype; |
| 1547 |
uint32_t next_id = ir.increase_bound_by(1); |
| 1548 |
func.add_parameter(type_id, next_id, true); |
| 1549 |
set<SPIRVariable>(next_id, type_id, StorageClassFunction, 0, arg_id); |
| 1550 |
|
| 1551 |
set_name(next_id, name); |
| 1552 |
} |
| 1553 |
else if (is_builtin_variable(var) && p_type->basetype == SPIRType::Struct) |
| 1554 |
{ |
| 1555 |
// Get the pointee type |
| 1556 |
type_id = get_pointee_type_id(type_id); |
| 1557 |
p_type = &get<SPIRType>(type_id); |
| 1558 |
|
| 1559 |
uint32_t mbr_idx = 0; |
| 1560 |
for (auto &mbr_type_id : p_type->member_types) |
| 1561 |
{ |
| 1562 |
BuiltIn builtin = BuiltInMax; |
| 1563 |
bool is_builtin = is_member_builtin(*p_type, mbr_idx, &builtin); |
| 1564 |
if (is_builtin && has_active_builtin(builtin, var.storage)) |
| 1565 |
{ |
| 1566 |
// Add a arg variable with the same type and decorations as the member |
| 1567 |
uint32_t next_ids = ir.increase_bound_by(2); |
| 1568 |
uint32_t ptr_type_id = next_ids + 0; |
| 1569 |
uint32_t var_id = next_ids + 1; |
| 1570 |
|
| 1571 |
// Make sure we have an actual pointer type, |
| 1572 |
// so that we will get the appropriate address space when declaring these builtins. |
| 1573 |
auto &ptr = set<SPIRType>(ptr_type_id, get<SPIRType>(mbr_type_id)); |
| 1574 |
ptr.self = mbr_type_id; |
| 1575 |
ptr.storage = var.storage; |
| 1576 |
ptr.pointer = true; |
| 1577 |
ptr.parent_type = mbr_type_id; |
| 1578 |
|
| 1579 |
func.add_parameter(mbr_type_id, var_id, true); |
| 1580 |
set<SPIRVariable>(var_id, ptr_type_id, StorageClassFunction); |
| 1581 |
ir.meta[var_id].decoration = ir.meta[type_id].members[mbr_idx]; |
| 1582 |
} |
| 1583 |
mbr_idx++; |
| 1584 |
} |
| 1585 |
} |
| 1586 |
else |
| 1587 |
{ |
| 1588 |
uint32_t next_id = ir.increase_bound_by(1); |
| 1589 |
func.add_parameter(type_id, next_id, true); |
| 1590 |
set<SPIRVariable>(next_id, type_id, StorageClassFunction, 0, arg_id); |
| 1591 |
|
| 1592 |
// Ensure the existing variable has a valid name and the new variable has all the same meta info |
| 1593 |
set_name(arg_id, ensure_valid_name(to_name(arg_id), "v")); |
| 1594 |
ir.meta[next_id] = ir.meta[arg_id]; |
| 1595 |
} |
| 1596 |
} |
| 1597 |
} |
| 1598 |
} |
| 1599 |
|
| 1600 |
// For all variables that are some form of non-input-output interface block, mark that all the structs |
| 1601 |
// that are recursively contained within the type referenced by that variable should be packed tightly. |
| 1602 |
void CompilerMSL::mark_packable_structs() |
| 1603 |
{ |
| 1604 |
ir.for_each_typed_id<SPIRVariable>([&](uint32_t, SPIRVariable &var) { |
| 1605 |
if (var.storage != StorageClassFunction && !is_hidden_variable(var)) |
| 1606 |
{ |
| 1607 |
auto &type = this->get<SPIRType>(var.basetype); |
| 1608 |
if (type.pointer && |
| 1609 |
(type.storage == StorageClassUniform || type.storage == StorageClassUniformConstant || |
| 1610 |
type.storage == StorageClassPushConstant || type.storage == StorageClassStorageBuffer) && |
| 1611 |
(has_decoration(type.self, DecorationBlock) || has_decoration(type.self, DecorationBufferBlock))) |
| 1612 |
mark_as_packable(type); |
| 1613 |
} |
| 1614 |
}); |
| 1615 |
} |
| 1616 |
|
| 1617 |
// If the specified type is a struct, it and any nested structs |
| 1618 |
// are marked as packable with the SPIRVCrossDecorationBufferBlockRepacked decoration, |
| 1619 |
void CompilerMSL::mark_as_packable(SPIRType &type) |
| 1620 |
{ |
| 1621 |
// If this is not the base type (eg. it's a pointer or array), tunnel down |
| 1622 |
if (type.parent_type) |
| 1623 |
{ |
| 1624 |
mark_as_packable(get<SPIRType>(type.parent_type)); |
| 1625 |
return; |
| 1626 |
} |
| 1627 |
|
| 1628 |
if (type.basetype == SPIRType::Struct) |
| 1629 |
{ |
| 1630 |
set_extended_decoration(type.self, SPIRVCrossDecorationBufferBlockRepacked); |
| 1631 |
|
| 1632 |
// Recurse |
| 1633 |
uint32_t mbr_cnt = uint32_t(type.member_types.size()); |
| 1634 |
for (uint32_t mbr_idx = 0; mbr_idx < mbr_cnt; mbr_idx++) |
| 1635 |
{ |
| 1636 |
uint32_t mbr_type_id = type.member_types[mbr_idx]; |
| 1637 |
auto &mbr_type = get<SPIRType>(mbr_type_id); |
| 1638 |
mark_as_packable(mbr_type); |
| 1639 |
if (mbr_type.type_alias) |
| 1640 |
{ |
| 1641 |
auto &mbr_type_alias = get<SPIRType>(mbr_type.type_alias); |
| 1642 |
mark_as_packable(mbr_type_alias); |
| 1643 |
} |
| 1644 |
} |
| 1645 |
} |
| 1646 |
} |
| 1647 |
|
| 1648 |
// If a shader input exists at the location, it is marked as being used by this shader |
| 1649 |
void CompilerMSL::mark_location_as_used_by_shader(uint32_t location, const SPIRType &type, StorageClass storage) |
| 1650 |
{ |
| 1651 |
if (storage != StorageClassInput) |
| 1652 |
return; |
| 1653 |
if (is_array(type)) |
| 1654 |
{ |
| 1655 |
uint32_t dim = 1; |
| 1656 |
for (uint32_t i = 0; i < type.array.size(); i++) |
| 1657 |
dim *= to_array_size_literal(type, i); |
| 1658 |
for (uint32_t i = 0; i < dim; i++) |
| 1659 |
{ |
| 1660 |
if (is_matrix(type)) |
| 1661 |
{ |
| 1662 |
for (uint32_t j = 0; j < type.columns; j++) |
| 1663 |
inputs_in_use.insert(location++); |
| 1664 |
} |
| 1665 |
else |
| 1666 |
inputs_in_use.insert(location++); |
| 1667 |
} |
| 1668 |
} |
| 1669 |
else if (is_matrix(type)) |
| 1670 |
{ |
| 1671 |
for (uint32_t i = 0; i < type.columns; i++) |
| 1672 |
inputs_in_use.insert(location + i); |
| 1673 |
} |
| 1674 |
else |
| 1675 |
inputs_in_use.insert(location); |
| 1676 |
} |
| 1677 |
|
| 1678 |
uint32_t CompilerMSL::get_target_components_for_fragment_location(uint32_t location) const |
| 1679 |
{ |
| 1680 |
auto itr = fragment_output_components.find(location); |
| 1681 |
if (itr == end(fragment_output_components)) |
| 1682 |
return 4; |
| 1683 |
else |
| 1684 |
return itr->second; |
| 1685 |
} |
| 1686 |
|
| 1687 |
uint32_t CompilerMSL::build_extended_vector_type(uint32_t type_id, uint32_t components, SPIRType::BaseType basetype) |
| 1688 |
{ |
| 1689 |
uint32_t new_type_id = ir.increase_bound_by(1); |
| 1690 |
auto &old_type = get<SPIRType>(type_id); |
| 1691 |
auto *type = &set<SPIRType>(new_type_id, old_type); |
| 1692 |
type->vecsize = components; |
| 1693 |
if (basetype != SPIRType::Unknown) |
| 1694 |
type->basetype = basetype; |
| 1695 |
type->self = new_type_id; |
| 1696 |
type->parent_type = type_id; |
| 1697 |
type->array.clear(); |
| 1698 |
type->array_size_literal.clear(); |
| 1699 |
type->pointer = false; |
| 1700 |
|
| 1701 |
if (is_array(old_type)) |
| 1702 |
{ |
| 1703 |
uint32_t array_type_id = ir.increase_bound_by(1); |
| 1704 |
type = &set<SPIRType>(array_type_id, *type); |
| 1705 |
type->parent_type = new_type_id; |
| 1706 |
type->array = old_type.array; |
| 1707 |
type->array_size_literal = old_type.array_size_literal; |
| 1708 |
new_type_id = array_type_id; |
| 1709 |
} |
| 1710 |
|
| 1711 |
if (old_type.pointer) |
| 1712 |
{ |
| 1713 |
uint32_t ptr_type_id = ir.increase_bound_by(1); |
| 1714 |
type = &set<SPIRType>(ptr_type_id, *type); |
| 1715 |
type->self = new_type_id; |
| 1716 |
type->parent_type = new_type_id; |
| 1717 |
type->storage = old_type.storage; |
| 1718 |
type->pointer = true; |
| 1719 |
new_type_id = ptr_type_id; |
| 1720 |
} |
| 1721 |
|
| 1722 |
return new_type_id; |
| 1723 |
} |
| 1724 |
|
| 1725 |
uint32_t CompilerMSL::build_msl_interpolant_type(uint32_t type_id, bool is_noperspective) |
| 1726 |
{ |
| 1727 |
uint32_t new_type_id = ir.increase_bound_by(1); |
| 1728 |
SPIRType &type = set<SPIRType>(new_type_id, get<SPIRType>(type_id)); |
| 1729 |
type.basetype = SPIRType::Interpolant; |
| 1730 |
type.parent_type = type_id; |
| 1731 |
// In Metal, the pull-model interpolant type encodes perspective-vs-no-perspective in the type itself. |
| 1732 |
// Add this decoration so we know which argument to pass to the template. |
| 1733 |
if (is_noperspective) |
| 1734 |
set_decoration(new_type_id, DecorationNoPerspective); |
| 1735 |
return new_type_id; |
| 1736 |
} |
| 1737 |
|
| 1738 |
void CompilerMSL::add_plain_variable_to_interface_block(StorageClass storage, const string &ib_var_ref, |
| 1739 |
SPIRType &ib_type, SPIRVariable &var, InterfaceBlockMeta &meta) |
| 1740 |
{ |
| 1741 |
bool is_builtin = is_builtin_variable(var); |
| 1742 |
BuiltIn builtin = BuiltIn(get_decoration(var.self, DecorationBuiltIn)); |
| 1743 |
bool is_flat = has_decoration(var.self, DecorationFlat); |
| 1744 |
bool is_noperspective = has_decoration(var.self, DecorationNoPerspective); |
| 1745 |
bool is_centroid = has_decoration(var.self, DecorationCentroid); |
| 1746 |
bool is_sample = has_decoration(var.self, DecorationSample); |
| 1747 |
|
| 1748 |
// Add a reference to the variable type to the interface struct. |
| 1749 |
uint32_t ib_mbr_idx = uint32_t(ib_type.member_types.size()); |
| 1750 |
uint32_t type_id = ensure_correct_builtin_type(var.basetype, builtin); |
| 1751 |
var.basetype = type_id; |
| 1752 |
|
| 1753 |
type_id = get_pointee_type_id(var.basetype); |
| 1754 |
if (meta.strip_array && is_array(get<SPIRType>(type_id))) |
| 1755 |
type_id = get<SPIRType>(type_id).parent_type; |
| 1756 |
auto &type = get<SPIRType>(type_id); |
| 1757 |
uint32_t target_components = 0; |
| 1758 |
uint32_t type_components = type.vecsize; |
| 1759 |
|
| 1760 |
bool padded_output = false; |
| 1761 |
bool padded_input = false; |
| 1762 |
uint32_t start_component = 0; |
| 1763 |
|
| 1764 |
auto &entry_func = get<SPIRFunction>(ir.default_entry_point); |
| 1765 |
|
| 1766 |
// Deal with Component decorations. |
| 1767 |
InterfaceBlockMeta::LocationMeta *location_meta = nullptr; |
| 1768 |
if (has_decoration(var.self, DecorationLocation)) |
| 1769 |
{ |
| 1770 |
auto location_meta_itr = meta.location_meta.find(get_decoration(var.self, DecorationLocation)); |
| 1771 |
if (location_meta_itr != end(meta.location_meta)) |
| 1772 |
location_meta = &location_meta_itr->second; |
| 1773 |
} |
| 1774 |
|
| 1775 |
bool pad_fragment_output = has_decoration(var.self, DecorationLocation) && |
| 1776 |
msl_options.pad_fragment_output_components && |
| 1777 |
get_entry_point().model == ExecutionModelFragment && storage == StorageClassOutput; |
| 1778 |
|
| 1779 |
// Check if we need to pad fragment output to match a certain number of components. |
| 1780 |
if (location_meta) |
| 1781 |
{ |
| 1782 |
start_component = get_decoration(var.self, DecorationComponent); |
| 1783 |
uint32_t num_components = location_meta->num_components; |
| 1784 |
if (pad_fragment_output) |
| 1785 |
{ |
| 1786 |
uint32_t locn = get_decoration(var.self, DecorationLocation); |
| 1787 |
num_components = std::max(num_components, get_target_components_for_fragment_location(locn)); |
| 1788 |
} |
| 1789 |
|
| 1790 |
if (location_meta->ib_index != ~0u) |
| 1791 |
{ |
| 1792 |
// We have already declared the variable. Just emit an early-declared variable and fixup as needed. |
| 1793 |
entry_func.add_local_variable(var.self); |
| 1794 |
vars_needing_early_declaration.push_back(var.self); |
| 1795 |
|
| 1796 |
if (var.storage == StorageClassInput) |
| 1797 |
{ |
| 1798 |
uint32_t ib_index = location_meta->ib_index; |
| 1799 |
entry_func.fixup_hooks_in.push_back([=, &var]() { |
| 1800 |
statement(to_name(var.self), " = ", ib_var_ref, ".", to_member_name(ib_type, ib_index), |
| 1801 |
vector_swizzle(type_components, start_component), ";"); |
| 1802 |
}); |
| 1803 |
} |
| 1804 |
else |
| 1805 |
{ |
| 1806 |
uint32_t ib_index = location_meta->ib_index; |
| 1807 |
entry_func.fixup_hooks_out.push_back([=, &var]() { |
| 1808 |
statement(ib_var_ref, ".", to_member_name(ib_type, ib_index), |
| 1809 |
vector_swizzle(type_components, start_component), " = ", to_name(var.self), ";"); |
| 1810 |
}); |
| 1811 |
} |
| 1812 |
return; |
| 1813 |
} |
| 1814 |
else |
| 1815 |
{ |
| 1816 |
location_meta->ib_index = uint32_t(ib_type.member_types.size()); |
| 1817 |
type_id = build_extended_vector_type(type_id, num_components); |
| 1818 |
if (var.storage == StorageClassInput) |
| 1819 |
padded_input = true; |
| 1820 |
else |
| 1821 |
padded_output = true; |
| 1822 |
} |
| 1823 |
} |
| 1824 |
else if (pad_fragment_output) |
| 1825 |
{ |
| 1826 |
uint32_t locn = get_decoration(var.self, DecorationLocation); |
| 1827 |
target_components = get_target_components_for_fragment_location(locn); |
| 1828 |
if (type_components < target_components) |
| 1829 |
{ |
| 1830 |
// Make a new type here. |
| 1831 |
type_id = build_extended_vector_type(type_id, target_components); |
| 1832 |
padded_output = true; |
| 1833 |
} |
| 1834 |
} |
| 1835 |
|
| 1836 |
if (storage == StorageClassInput && pull_model_inputs.count(var.self)) |
| 1837 |
ib_type.member_types.push_back(build_msl_interpolant_type(type_id, is_noperspective)); |
| 1838 |
else |
| 1839 |
ib_type.member_types.push_back(type_id); |
| 1840 |
|
| 1841 |
// Give the member a name |
| 1842 |
string mbr_name = ensure_valid_name(to_expression(var.self), "m"); |
| 1843 |
set_member_name(ib_type.self, ib_mbr_idx, mbr_name); |
| 1844 |
|
| 1845 |
// Update the original variable reference to include the structure reference |
| 1846 |
string qual_var_name = ib_var_ref + "." + mbr_name; |
| 1847 |
// If using pull-model interpolation, need to add a call to the correct interpolation method. |
| 1848 |
if (storage == StorageClassInput && pull_model_inputs.count(var.self)) |
| 1849 |
{ |
| 1850 |
if (is_centroid) |
| 1851 |
qual_var_name += ".interpolate_at_centroid()"; |
| 1852 |
else if (is_sample) |
| 1853 |
qual_var_name += join(".interpolate_at_sample(", to_expression(builtin_sample_id_id), ")"); |
| 1854 |
else |
| 1855 |
qual_var_name += ".interpolate_at_center()"; |
| 1856 |
} |
| 1857 |
|
| 1858 |
if (padded_output || padded_input) |
| 1859 |
{ |
| 1860 |
entry_func.add_local_variable(var.self); |
| 1861 |
vars_needing_early_declaration.push_back(var.self); |
| 1862 |
|
| 1863 |
if (padded_output) |
| 1864 |
{ |
| 1865 |
entry_func.fixup_hooks_out.push_back([=, &var]() { |
| 1866 |
statement(qual_var_name, vector_swizzle(type_components, start_component), " = ", to_name(var.self), |
| 1867 |
";"); |
| 1868 |
}); |
| 1869 |
} |
| 1870 |
else |
| 1871 |
{ |
| 1872 |
entry_func.fixup_hooks_in.push_back([=, &var]() { |
| 1873 |
statement(to_name(var.self), " = ", qual_var_name, vector_swizzle(type_components, start_component), |
| 1874 |
";"); |
| 1875 |
}); |
| 1876 |
} |
| 1877 |
} |
| 1878 |
else if (!meta.strip_array) |
| 1879 |
ir.meta[var.self].decoration.qualified_alias = qual_var_name; |
| 1880 |
|
| 1881 |
if (var.storage == StorageClassOutput && var.initializer != ID(0)) |
| 1882 |
{ |
| 1883 |
if (padded_output || padded_input) |
| 1884 |
{ |
| 1885 |
entry_func.fixup_hooks_in.push_back( |
| 1886 |
[=, &var]() { statement(to_name(var.self), " = ", to_expression(var.initializer), ";"); }); |
| 1887 |
} |
| 1888 |
else |
| 1889 |
{ |
| 1890 |
entry_func.fixup_hooks_in.push_back( |
| 1891 |
[=, &var]() { statement(qual_var_name, " = ", to_expression(var.initializer), ";"); }); |
| 1892 |
} |
| 1893 |
} |
| 1894 |
|
| 1895 |
// Copy the variable location from the original variable to the member |
| 1896 |
if (get_decoration_bitset(var.self).get(DecorationLocation)) |
| 1897 |
{ |
| 1898 |
uint32_t locn = get_decoration(var.self, DecorationLocation); |
| 1899 |
if (storage == StorageClassInput) |
| 1900 |
{ |
| 1901 |
type_id = ensure_correct_input_type(var.basetype, locn, location_meta ? location_meta->num_components : 0); |
| 1902 |
if (!location_meta) |
| 1903 |
var.basetype = type_id; |
| 1904 |
|
| 1905 |
type_id = get_pointee_type_id(type_id); |
| 1906 |
if (meta.strip_array && is_array(get<SPIRType>(type_id))) |
| 1907 |
type_id = get<SPIRType>(type_id).parent_type; |
| 1908 |
if (pull_model_inputs.count(var.self)) |
| 1909 |
ib_type.member_types[ib_mbr_idx] = build_msl_interpolant_type(type_id, is_noperspective); |
| 1910 |
else |
| 1911 |
ib_type.member_types[ib_mbr_idx] = type_id; |
| 1912 |
} |
| 1913 |
set_member_decoration(ib_type.self, ib_mbr_idx, DecorationLocation, locn); |
| 1914 |
mark_location_as_used_by_shader(locn, get<SPIRType>(type_id), storage); |
| 1915 |
} |
| 1916 |
else if (is_builtin && is_tessellation_shader() && inputs_by_builtin.count(builtin)) |
| 1917 |
{ |
| 1918 |
uint32_t locn = inputs_by_builtin[builtin].location; |
| 1919 |
set_member_decoration(ib_type.self, ib_mbr_idx, DecorationLocation, locn); |
| 1920 |
mark_location_as_used_by_shader(locn, type, storage); |
| 1921 |
} |
| 1922 |
|
| 1923 |
if (!location_meta) |
| 1924 |
{ |
| 1925 |
if (get_decoration_bitset(var.self).get(DecorationComponent)) |
| 1926 |
{ |
| 1927 |
uint32_t component = get_decoration(var.self, DecorationComponent); |
| 1928 |
set_member_decoration(ib_type.self, ib_mbr_idx, DecorationComponent, component); |
| 1929 |
} |
| 1930 |
} |
| 1931 |
|
| 1932 |
if (get_decoration_bitset(var.self).get(DecorationIndex)) |
| 1933 |
{ |
| 1934 |
uint32_t index = get_decoration(var.self, DecorationIndex); |
| 1935 |
set_member_decoration(ib_type.self, ib_mbr_idx, DecorationIndex, index); |
| 1936 |
} |
| 1937 |
|
| 1938 |
// Mark the member as builtin if needed |
| 1939 |
if (is_builtin) |
| 1940 |
{ |
| 1941 |
set_member_decoration(ib_type.self, ib_mbr_idx, DecorationBuiltIn, builtin); |
| 1942 |
if (builtin == BuiltInPosition && storage == StorageClassOutput) |
| 1943 |
qual_pos_var_name = qual_var_name; |
| 1944 |
} |
| 1945 |
|
| 1946 |
// Copy interpolation decorations if needed |
| 1947 |
if (storage != StorageClassInput || !pull_model_inputs.count(var.self)) |
| 1948 |
{ |
| 1949 |
if (is_flat) |
| 1950 |
set_member_decoration(ib_type.self, ib_mbr_idx, DecorationFlat); |
| 1951 |
if (is_noperspective) |
| 1952 |
set_member_decoration(ib_type.self, ib_mbr_idx, DecorationNoPerspective); |
| 1953 |
if (is_centroid) |
| 1954 |
set_member_decoration(ib_type.self, ib_mbr_idx, DecorationCentroid); |
| 1955 |
if (is_sample) |
| 1956 |
set_member_decoration(ib_type.self, ib_mbr_idx, DecorationSample); |
| 1957 |
} |
| 1958 |
|
| 1959 |
// If we have location meta, there is no unique OrigID. We won't need it, since we flatten/unflatten |
| 1960 |
// the variable to stack anyways here. |
| 1961 |
if (!location_meta) |
| 1962 |
set_extended_member_decoration(ib_type.self, ib_mbr_idx, SPIRVCrossDecorationInterfaceOrigID, var.self); |
| 1963 |
} |
| 1964 |
|
| 1965 |
void CompilerMSL::add_composite_variable_to_interface_block(StorageClass storage, const string &ib_var_ref, |
| 1966 |
SPIRType &ib_type, SPIRVariable &var, |
| 1967 |
InterfaceBlockMeta &meta) |
| 1968 |
{ |
| 1969 |
auto &entry_func = get<SPIRFunction>(ir.default_entry_point); |
| 1970 |
auto &var_type = meta.strip_array ? get_variable_element_type(var) : get_variable_data_type(var); |
| 1971 |
uint32_t elem_cnt = 0; |
| 1972 |
|
| 1973 |
if (is_matrix(var_type)) |
| 1974 |
{ |
| 1975 |
if (is_array(var_type)) |
| 1976 |
SPIRV_CROSS_THROW("MSL cannot emit arrays-of-matrices in input and output variables."); |
| 1977 |
|
| 1978 |
elem_cnt = var_type.columns; |
| 1979 |
} |
| 1980 |
else if (is_array(var_type)) |
| 1981 |
{ |
| 1982 |
if (var_type.array.size() != 1) |
| 1983 |
SPIRV_CROSS_THROW("MSL cannot emit arrays-of-arrays in input and output variables."); |
| 1984 |
|
| 1985 |
elem_cnt = to_array_size_literal(var_type); |
| 1986 |
} |
| 1987 |
|
| 1988 |
bool is_builtin = is_builtin_variable(var); |
| 1989 |
BuiltIn builtin = BuiltIn(get_decoration(var.self, DecorationBuiltIn)); |
| 1990 |
bool is_flat = has_decoration(var.self, DecorationFlat); |
| 1991 |
bool is_noperspective = has_decoration(var.self, DecorationNoPerspective); |
| 1992 |
bool is_centroid = has_decoration(var.self, DecorationCentroid); |
| 1993 |
bool is_sample = has_decoration(var.self, DecorationSample); |
| 1994 |
|
| 1995 |
auto *usable_type = &var_type; |
| 1996 |
if (usable_type->pointer) |
| 1997 |
usable_type = &get<SPIRType>(usable_type->parent_type); |
| 1998 |
while (is_array(*usable_type) || is_matrix(*usable_type)) |
| 1999 |
usable_type = &get<SPIRType>(usable_type->parent_type); |
| 2000 |
|
| 2001 |
// If a builtin, force it to have the proper name. |
| 2002 |
if (is_builtin) |
| 2003 |
set_name(var.self, builtin_to_glsl(builtin, StorageClassFunction)); |
| 2004 |
|
| 2005 |
bool flatten_from_ib_var = false; |
| 2006 |
string flatten_from_ib_mbr_name; |
| 2007 |
|
| 2008 |
if (storage == StorageClassOutput && is_builtin && builtin == BuiltInClipDistance) |
| 2009 |
{ |
| 2010 |
// Also declare [[clip_distance]] attribute here. |
| 2011 |
uint32_t clip_array_mbr_idx = uint32_t(ib_type.member_types.size()); |
| 2012 |
ib_type.member_types.push_back(get_variable_data_type_id(var)); |
| 2013 |
set_member_decoration(ib_type.self, clip_array_mbr_idx, DecorationBuiltIn, BuiltInClipDistance); |
| 2014 |
|
| 2015 |
flatten_from_ib_mbr_name = builtin_to_glsl(BuiltInClipDistance, StorageClassOutput); |
| 2016 |
set_member_name(ib_type.self, clip_array_mbr_idx, flatten_from_ib_mbr_name); |
| 2017 |
|
| 2018 |
// When we flatten, we flatten directly from the "out" struct, |
| 2019 |
// not from a function variable. |
| 2020 |
flatten_from_ib_var = true; |
| 2021 |
|
| 2022 |
if (!msl_options.enable_clip_distance_user_varying) |
| 2023 |
return; |
| 2024 |
} |
| 2025 |
else if (!meta.strip_array) |
| 2026 |
{ |
| 2027 |
// Only flatten/unflatten IO composites for non-tessellation cases where arrays are not stripped. |
| 2028 |
entry_func.add_local_variable(var.self); |
| 2029 |
// We need to declare the variable early and at entry-point scope. |
| 2030 |
vars_needing_early_declaration.push_back(var.self); |
| 2031 |
} |
| 2032 |
|
| 2033 |
for (uint32_t i = 0; i < elem_cnt; i++) |
| 2034 |
{ |
| 2035 |
// Add a reference to the variable type to the interface struct. |
| 2036 |
uint32_t ib_mbr_idx = uint32_t(ib_type.member_types.size()); |
| 2037 |
|
| 2038 |
uint32_t target_components = 0; |
| 2039 |
bool padded_output = false; |
| 2040 |
uint32_t type_id = usable_type->self; |
| 2041 |
|
| 2042 |
// Check if we need to pad fragment output to match a certain number of components. |
| 2043 |
if (get_decoration_bitset(var.self).get(DecorationLocation) && msl_options.pad_fragment_output_components && |
| 2044 |
get_entry_point().model == ExecutionModelFragment && storage == StorageClassOutput) |
| 2045 |
{ |
| 2046 |
uint32_t locn = get_decoration(var.self, DecorationLocation) + i; |
| 2047 |
target_components = get_target_components_for_fragment_location(locn); |
| 2048 |
if (usable_type->vecsize < target_components) |
| 2049 |
{ |
| 2050 |
// Make a new type here. |
| 2051 |
type_id = build_extended_vector_type(usable_type->self, target_components); |
| 2052 |
padded_output = true; |
| 2053 |
} |
| 2054 |
} |
| 2055 |
|
| 2056 |
if (storage == StorageClassInput && pull_model_inputs.count(var.self)) |
| 2057 |
ib_type.member_types.push_back(build_msl_interpolant_type(get_pointee_type_id(type_id), is_noperspective)); |
| 2058 |
else |
| 2059 |
ib_type.member_types.push_back(get_pointee_type_id(type_id)); |
| 2060 |
|
| 2061 |
// Give the member a name |
| 2062 |
string mbr_name = ensure_valid_name(join(to_expression(var.self), "_", i), "m"); |
| 2063 |
set_member_name(ib_type.self, ib_mbr_idx, mbr_name); |
| 2064 |
|
| 2065 |
// There is no qualified alias since we need to flatten the internal array on return. |
| 2066 |
if (get_decoration_bitset(var.self).get(DecorationLocation)) |
| 2067 |
{ |
| 2068 |
uint32_t locn = get_decoration(var.self, DecorationLocation) + i; |
| 2069 |
if (storage == StorageClassInput) |
| 2070 |
{ |
| 2071 |
var.basetype = ensure_correct_input_type(var.basetype, locn); |
| 2072 |
uint32_t mbr_type_id = ensure_correct_input_type(usable_type->self, locn); |
| 2073 |
if (storage == StorageClassInput && pull_model_inputs.count(var.self)) |
| 2074 |
ib_type.member_types[ib_mbr_idx] = build_msl_interpolant_type(mbr_type_id, is_noperspective); |
| 2075 |
else |
| 2076 |
ib_type.member_types[ib_mbr_idx] = mbr_type_id; |
| 2077 |
} |
| 2078 |
set_member_decoration(ib_type.self, ib_mbr_idx, DecorationLocation, locn); |
| 2079 |
mark_location_as_used_by_shader(locn, *usable_type, storage); |
| 2080 |
} |
| 2081 |
else if (is_builtin && is_tessellation_shader() && inputs_by_builtin.count(builtin)) |
| 2082 |
{ |
| 2083 |
uint32_t locn = inputs_by_builtin[builtin].location + i; |
| 2084 |
set_member_decoration(ib_type.self, ib_mbr_idx, DecorationLocation, locn); |
| 2085 |
mark_location_as_used_by_shader(locn, *usable_type, storage); |
| 2086 |
} |
| 2087 |
else if (is_builtin && builtin == BuiltInClipDistance) |
| 2088 |
{ |
| 2089 |
// Declare the ClipDistance as [[user(clipN)]]. |
| 2090 |
set_member_decoration(ib_type.self, ib_mbr_idx, DecorationBuiltIn, BuiltInClipDistance); |
| 2091 |
set_member_decoration(ib_type.self, ib_mbr_idx, DecorationLocation, i); |
| 2092 |
} |
| 2093 |
|
| 2094 |
if (get_decoration_bitset(var.self).get(DecorationIndex)) |
| 2095 |
{ |
| 2096 |
uint32_t index = get_decoration(var.self, DecorationIndex); |
| 2097 |
set_member_decoration(ib_type.self, ib_mbr_idx, DecorationIndex, index); |
| 2098 |
} |
| 2099 |
|
| 2100 |
if (storage != StorageClassInput || !pull_model_inputs.count(var.self)) |
| 2101 |
{ |
| 2102 |
// Copy interpolation decorations if needed |
| 2103 |
if (is_flat) |
| 2104 |
set_member_decoration(ib_type.self, ib_mbr_idx, DecorationFlat); |
| 2105 |
if (is_noperspective) |
| 2106 |
set_member_decoration(ib_type.self, ib_mbr_idx, DecorationNoPerspective); |
| 2107 |
if (is_centroid) |
| 2108 |
set_member_decoration(ib_type.self, ib_mbr_idx, DecorationCentroid); |
| 2109 |
if (is_sample) |
| 2110 |
set_member_decoration(ib_type.self, ib_mbr_idx, DecorationSample); |
| 2111 |
} |
| 2112 |
|
| 2113 |
set_extended_member_decoration(ib_type.self, ib_mbr_idx, SPIRVCrossDecorationInterfaceOrigID, var.self); |
| 2114 |
|
| 2115 |
// Only flatten/unflatten IO composites for non-tessellation cases where arrays are not stripped. |
| 2116 |
if (!meta.strip_array) |
| 2117 |
{ |
| 2118 |
switch (storage) |
| 2119 |
{ |
| 2120 |
case StorageClassInput: |
| 2121 |
entry_func.fixup_hooks_in.push_back([=, &var]() { |
| 2122 |
if (pull_model_inputs.count(var.self)) |
| 2123 |
{ |
| 2124 |
string lerp_call; |
| 2125 |
if (is_centroid) |
| 2126 |
lerp_call = ".interpolate_at_centroid()"; |
| 2127 |
else if (is_sample) |
| 2128 |
lerp_call = join(".interpolate_at_sample(", to_expression(builtin_sample_id_id), ")"); |
| 2129 |
else |
| 2130 |
lerp_call = ".interpolate_at_center()"; |
| 2131 |
statement(to_name(var.self), "[", i, "] = ", ib_var_ref, ".", mbr_name, lerp_call, ";"); |
| 2132 |
} |
| 2133 |
else |
| 2134 |
{ |
| 2135 |
statement(to_name(var.self), "[", i, "] = ", ib_var_ref, ".", mbr_name, ";"); |
| 2136 |
} |
| 2137 |
}); |
| 2138 |
break; |
| 2139 |
|
| 2140 |
case StorageClassOutput: |
| 2141 |
entry_func.fixup_hooks_out.push_back([=, &var]() { |
| 2142 |
if (padded_output) |
| 2143 |
{ |
| 2144 |
auto &padded_type = this->get<SPIRType>(type_id); |
| 2145 |
statement( |
| 2146 |
ib_var_ref, ".", mbr_name, " = ", |
| 2147 |
remap_swizzle(padded_type, usable_type->vecsize, join(to_name(var.self), "[", i, "]")), |
| 2148 |
";"); |
| 2149 |
} |
| 2150 |
else if (flatten_from_ib_var) |
| 2151 |
statement(ib_var_ref, ".", mbr_name, " = ", ib_var_ref, ".", flatten_from_ib_mbr_name, "[", i, |
| 2152 |
"];"); |
| 2153 |
else |
| 2154 |
statement(ib_var_ref, ".", mbr_name, " = ", to_name(var.self), "[", i, "];"); |
| 2155 |
}); |
| 2156 |
break; |
| 2157 |
|
| 2158 |
default: |
| 2159 |
break; |
| 2160 |
} |
| 2161 |
} |
| 2162 |
} |
| 2163 |
} |
| 2164 |
|
| 2165 |
uint32_t CompilerMSL::get_accumulated_member_location(const SPIRVariable &var, uint32_t mbr_idx, bool strip_array) |
| 2166 |
{ |
| 2167 |
auto &type = strip_array ? get_variable_element_type(var) : get_variable_data_type(var); |
| 2168 |
uint32_t location = get_decoration(var.self, DecorationLocation); |
| 2169 |
|
| 2170 |
for (uint32_t i = 0; i < mbr_idx; i++) |
| 2171 |
{ |
| 2172 |
auto &mbr_type = get<SPIRType>(type.member_types[i]); |
| 2173 |
|
| 2174 |
// Start counting from any place we have a new location decoration. |
| 2175 |
if (has_member_decoration(type.self, mbr_idx, DecorationLocation)) |
| 2176 |
location = get_member_decoration(type.self, mbr_idx, DecorationLocation); |
| 2177 |
|
| 2178 |
uint32_t location_count = 1; |
| 2179 |
|
| 2180 |
if (mbr_type.columns > 1) |
| 2181 |
location_count = mbr_type.columns; |
| 2182 |
|
| 2183 |
if (!mbr_type.array.empty()) |
| 2184 |
for (uint32_t j = 0; j < uint32_t(mbr_type.array.size()); j++) |
| 2185 |
location_count *= to_array_size_literal(mbr_type, j); |
| 2186 |
|
| 2187 |
location += location_count; |
| 2188 |
} |
| 2189 |
|
| 2190 |
return location; |
| 2191 |
} |
| 2192 |
|
| 2193 |
void CompilerMSL::add_composite_member_variable_to_interface_block(StorageClass storage, const string &ib_var_ref, |
| 2194 |
SPIRType &ib_type, SPIRVariable &var, |
| 2195 |
uint32_t mbr_idx, InterfaceBlockMeta &meta) |
| 2196 |
{ |
| 2197 |
auto &entry_func = get<SPIRFunction>(ir.default_entry_point); |
| 2198 |
auto &var_type = meta.strip_array ? get_variable_element_type(var) : get_variable_data_type(var); |
| 2199 |
|
| 2200 |
BuiltIn builtin = BuiltInMax; |
| 2201 |
bool is_builtin = is_member_builtin(var_type, mbr_idx, &builtin); |
| 2202 |
bool is_flat = |
| 2203 |
has_member_decoration(var_type.self, mbr_idx, DecorationFlat) || has_decoration(var.self, DecorationFlat); |
| 2204 |
bool is_noperspective = has_member_decoration(var_type.self, mbr_idx, DecorationNoPerspective) || |
| 2205 |
has_decoration(var.self, DecorationNoPerspective); |
| 2206 |
bool is_centroid = has_member_decoration(var_type.self, mbr_idx, DecorationCentroid) || |
| 2207 |
has_decoration(var.self, DecorationCentroid); |
| 2208 |
bool is_sample = |
| 2209 |
has_member_decoration(var_type.self, mbr_idx, DecorationSample) || has_decoration(var.self, DecorationSample); |
| 2210 |
|
| 2211 |
uint32_t mbr_type_id = var_type.member_types[mbr_idx]; |
| 2212 |
auto &mbr_type = get<SPIRType>(mbr_type_id); |
| 2213 |
uint32_t elem_cnt = 0; |
| 2214 |
|
| 2215 |
if (is_matrix(mbr_type)) |
| 2216 |
{ |
| 2217 |
if (is_array(mbr_type)) |
| 2218 |
SPIRV_CROSS_THROW("MSL cannot emit arrays-of-matrices in input and output variables."); |
| 2219 |
|
| 2220 |
elem_cnt = mbr_type.columns; |
| 2221 |
} |
| 2222 |
else if (is_array(mbr_type)) |
| 2223 |
{ |
| 2224 |
if (mbr_type.array.size() != 1) |
| 2225 |
SPIRV_CROSS_THROW("MSL cannot emit arrays-of-arrays in input and output variables."); |
| 2226 |
|
| 2227 |
elem_cnt = to_array_size_literal(mbr_type); |
| 2228 |
} |
| 2229 |
|
| 2230 |
auto *usable_type = &mbr_type; |
| 2231 |
if (usable_type->pointer) |
| 2232 |
usable_type = &get<SPIRType>(usable_type->parent_type); |
| 2233 |
while (is_array(*usable_type) || is_matrix(*usable_type)) |
| 2234 |
usable_type = &get<SPIRType>(usable_type->parent_type); |
| 2235 |
|
| 2236 |
bool flatten_from_ib_var = false; |
| 2237 |
string flatten_from_ib_mbr_name; |
| 2238 |
|
| 2239 |
if (storage == StorageClassOutput && is_builtin && builtin == BuiltInClipDistance) |
| 2240 |
{ |
| 2241 |
// Also declare [[clip_distance]] attribute here. |
| 2242 |
uint32_t clip_array_mbr_idx = uint32_t(ib_type.member_types.size()); |
| 2243 |
ib_type.member_types.push_back(mbr_type_id); |
| 2244 |
set_member_decoration(ib_type.self, clip_array_mbr_idx, DecorationBuiltIn, BuiltInClipDistance); |
| 2245 |
|
| 2246 |
flatten_from_ib_mbr_name = builtin_to_glsl(BuiltInClipDistance, StorageClassOutput); |
| 2247 |
set_member_name(ib_type.self, clip_array_mbr_idx, flatten_from_ib_mbr_name); |
| 2248 |
|
| 2249 |
// When we flatten, we flatten directly from the "out" struct, |
| 2250 |
// not from a function variable. |
| 2251 |
flatten_from_ib_var = true; |
| 2252 |
|
| 2253 |
if (!msl_options.enable_clip_distance_user_varying) |
| 2254 |
return; |
| 2255 |
} |
| 2256 |
|
| 2257 |
for (uint32_t i = 0; i < elem_cnt; i++) |
| 2258 |
{ |
| 2259 |
// Add a reference to the variable type to the interface struct. |
| 2260 |
uint32_t ib_mbr_idx = uint32_t(ib_type.member_types.size()); |
| 2261 |
if (storage == StorageClassInput && pull_model_inputs.count(var.self)) |
| 2262 |
ib_type.member_types.push_back(build_msl_interpolant_type(usable_type->self, is_noperspective)); |
| 2263 |
else |
| 2264 |
ib_type.member_types.push_back(usable_type->self); |
| 2265 |
|
| 2266 |
// Give the member a name |
| 2267 |
string mbr_name = ensure_valid_name(join(to_qualified_member_name(var_type, mbr_idx), "_", i), "m"); |
| 2268 |
set_member_name(ib_type.self, ib_mbr_idx, mbr_name); |
| 2269 |
|
| 2270 |
if (has_member_decoration(var_type.self, mbr_idx, DecorationLocation)) |
| 2271 |
{ |
| 2272 |
uint32_t locn = get_member_decoration(var_type.self, mbr_idx, DecorationLocation) + i; |
| 2273 |
set_member_decoration(ib_type.self, ib_mbr_idx, DecorationLocation, locn); |
| 2274 |
mark_location_as_used_by_shader(locn, *usable_type, storage); |
| 2275 |
} |
| 2276 |
else if (has_decoration(var.self, DecorationLocation)) |
| 2277 |
{ |
| 2278 |
uint32_t locn = get_accumulated_member_location(var, mbr_idx, meta.strip_array) + i; |
| 2279 |
set_member_decoration(ib_type.self, ib_mbr_idx, DecorationLocation, locn); |
| 2280 |
mark_location_as_used_by_shader(locn, *usable_type, storage); |
| 2281 |
} |
| 2282 |
else if (is_builtin && is_tessellation_shader() && inputs_by_builtin.count(builtin)) |
| 2283 |
{ |
| 2284 |
uint32_t locn = inputs_by_builtin[builtin].location + i; |
| 2285 |
set_member_decoration(ib_type.self, ib_mbr_idx, DecorationLocation, locn); |
| 2286 |
mark_location_as_used_by_shader(locn, *usable_type, storage); |
| 2287 |
} |
| 2288 |
else if (is_builtin && builtin == BuiltInClipDistance) |
| 2289 |
{ |
| 2290 |
// Declare the ClipDistance as [[user(clipN)]]. |
| 2291 |
set_member_decoration(ib_type.self, ib_mbr_idx, DecorationBuiltIn, BuiltInClipDistance); |
| 2292 |
set_member_decoration(ib_type.self, ib_mbr_idx, DecorationLocation, i); |
| 2293 |
} |
| 2294 |
|
| 2295 |
if (has_member_decoration(var_type.self, mbr_idx, DecorationComponent)) |
| 2296 |
SPIRV_CROSS_THROW("DecorationComponent on matrices and arrays make little sense."); |
| 2297 |
|
| 2298 |
if (storage != StorageClassInput || !pull_model_inputs.count(var.self)) |
| 2299 |
{ |
| 2300 |
// Copy interpolation decorations if needed |
| 2301 |
if (is_flat) |
| 2302 |
set_member_decoration(ib_type.self, ib_mbr_idx, DecorationFlat); |
| 2303 |
if (is_noperspective) |
| 2304 |
set_member_decoration(ib_type.self, ib_mbr_idx, DecorationNoPerspective); |
| 2305 |
if (is_centroid) |
| 2306 |
set_member_decoration(ib_type.self, ib_mbr_idx, DecorationCentroid); |
| 2307 |
if (is_sample) |
| 2308 |
set_member_decoration(ib_type.self, ib_mbr_idx, DecorationSample); |
| 2309 |
} |
| 2310 |
|
| 2311 |
set_extended_member_decoration(ib_type.self, ib_mbr_idx, SPIRVCrossDecorationInterfaceOrigID, var.self); |
| 2312 |
set_extended_member_decoration(ib_type.self, ib_mbr_idx, SPIRVCrossDecorationInterfaceMemberIndex, mbr_idx); |
| 2313 |
|
| 2314 |
// Unflatten or flatten from [[stage_in]] or [[stage_out]] as appropriate. |
| 2315 |
if (!meta.strip_array) |
| 2316 |
{ |
| 2317 |
switch (storage) |
| 2318 |
{ |
| 2319 |
case StorageClassInput: |
| 2320 |
entry_func.fixup_hooks_in.push_back([=, &var, &var_type]() { |
| 2321 |
if (pull_model_inputs.count(var.self)) |
| 2322 |
{ |
| 2323 |
string lerp_call; |
| 2324 |
if (is_centroid) |
| 2325 |
lerp_call = ".interpolate_at_centroid()"; |
| 2326 |
else if (is_sample) |
| 2327 |
lerp_call = join(".interpolate_at_sample(", to_expression(builtin_sample_id_id), ")"); |
| 2328 |
else |
| 2329 |
lerp_call = ".interpolate_at_center()"; |
| 2330 |
statement(to_name(var.self), ".", to_member_name(var_type, mbr_idx), "[", i, "] = ", ib_var_ref, |
| 2331 |
".", mbr_name, lerp_call, ";"); |
| 2332 |
} |
| 2333 |
else |
| 2334 |
{ |
| 2335 |
statement(to_name(var.self), ".", to_member_name(var_type, mbr_idx), "[", i, "] = ", ib_var_ref, |
| 2336 |
".", mbr_name, ";"); |
| 2337 |
} |
| 2338 |
}); |
| 2339 |
break; |
| 2340 |
|
| 2341 |
case StorageClassOutput: |
| 2342 |
entry_func.fixup_hooks_out.push_back([=, &var, &var_type]() { |
| 2343 |
if (flatten_from_ib_var) |
| 2344 |
{ |
| 2345 |
statement(ib_var_ref, ".", mbr_name, " = ", ib_var_ref, ".", flatten_from_ib_mbr_name, "[", i, |
| 2346 |
"];"); |
| 2347 |
} |
| 2348 |
else |
| 2349 |
{ |
| 2350 |
statement(ib_var_ref, ".", mbr_name, " = ", to_name(var.self), ".", |
| 2351 |
to_member_name(var_type, mbr_idx), "[", i, "];"); |
| 2352 |
} |
| 2353 |
}); |
| 2354 |
break; |
| 2355 |
|
| 2356 |
default: |
| 2357 |
break; |
| 2358 |
} |
| 2359 |
} |
| 2360 |
} |
| 2361 |
} |
| 2362 |
|
| 2363 |
void CompilerMSL::add_plain_member_variable_to_interface_block(StorageClass storage, const string &ib_var_ref, |
| 2364 |
SPIRType &ib_type, SPIRVariable &var, uint32_t mbr_idx, |
| 2365 |
InterfaceBlockMeta &meta) |
| 2366 |
{ |
| 2367 |
auto &var_type = meta.strip_array ? get_variable_element_type(var) : get_variable_data_type(var); |
| 2368 |
auto &entry_func = get<SPIRFunction>(ir.default_entry_point); |
| 2369 |
|
| 2370 |
BuiltIn builtin = BuiltInMax; |
| 2371 |
bool is_builtin = is_member_builtin(var_type, mbr_idx, &builtin); |
| 2372 |
bool is_flat = |
| 2373 |
has_member_decoration(var_type.self, mbr_idx, DecorationFlat) || has_decoration(var.self, DecorationFlat); |
| 2374 |
bool is_noperspective = has_member_decoration(var_type.self, mbr_idx, DecorationNoPerspective) || |
| 2375 |
has_decoration(var.self, DecorationNoPerspective); |
| 2376 |
bool is_centroid = has_member_decoration(var_type.self, mbr_idx, DecorationCentroid) || |
| 2377 |
has_decoration(var.self, DecorationCentroid); |
| 2378 |
bool is_sample = |
| 2379 |
has_member_decoration(var_type.self, mbr_idx, DecorationSample) || has_decoration(var.self, DecorationSample); |
| 2380 |
|
| 2381 |
// Add a reference to the member to the interface struct. |
| 2382 |
uint32_t mbr_type_id = var_type.member_types[mbr_idx]; |
| 2383 |
uint32_t ib_mbr_idx = uint32_t(ib_type.member_types.size()); |
| 2384 |
mbr_type_id = ensure_correct_builtin_type(mbr_type_id, builtin); |
| 2385 |
var_type.member_types[mbr_idx] = mbr_type_id; |
| 2386 |
if (storage == StorageClassInput && pull_model_inputs.count(var.self)) |
| 2387 |
ib_type.member_types.push_back(build_msl_interpolant_type(mbr_type_id, is_noperspective)); |
| 2388 |
else |
| 2389 |
ib_type.member_types.push_back(mbr_type_id); |
| 2390 |
|
| 2391 |
// Give the member a name |
| 2392 |
string mbr_name = ensure_valid_name(to_qualified_member_name(var_type, mbr_idx), "m"); |
| 2393 |
set_member_name(ib_type.self, ib_mbr_idx, mbr_name); |
| 2394 |
|
| 2395 |
// Update the original variable reference to include the structure reference |
| 2396 |
string qual_var_name = ib_var_ref + "." + mbr_name; |
| 2397 |
// If using pull-model interpolation, need to add a call to the correct interpolation method. |
| 2398 |
if (storage == StorageClassInput && pull_model_inputs.count(var.self)) |
| 2399 |
{ |
| 2400 |
if (is_centroid) |
| 2401 |
qual_var_name += ".interpolate_at_centroid()"; |
| 2402 |
else if (is_sample) |
| 2403 |
qual_var_name += join(".interpolate_at_sample(", to_expression(builtin_sample_id_id), ")"); |
| 2404 |
else |
| 2405 |
qual_var_name += ".interpolate_at_center()"; |
| 2406 |
} |
| 2407 |
|
| 2408 |
if (is_builtin && !meta.strip_array) |
| 2409 |
{ |
| 2410 |
// For the builtin gl_PerVertex, we cannot treat it as a block anyways, |
| 2411 |
// so redirect to qualified name. |
| 2412 |
set_member_qualified_name(var_type.self, mbr_idx, qual_var_name); |
| 2413 |
} |
| 2414 |
else if (!meta.strip_array) |
| 2415 |
{ |
| 2416 |
// Unflatten or flatten from [[stage_in]] or [[stage_out]] as appropriate. |
| 2417 |
switch (storage) |
| 2418 |
{ |
| 2419 |
case StorageClassInput: |
| 2420 |
entry_func.fixup_hooks_in.push_back([=, &var, &var_type]() { |
| 2421 |
statement(to_name(var.self), ".", to_member_name(var_type, mbr_idx), " = ", qual_var_name, ";"); |
| 2422 |
}); |
| 2423 |
break; |
| 2424 |
|
| 2425 |
case StorageClassOutput: |
| 2426 |
entry_func.fixup_hooks_out.push_back([=, &var, &var_type]() { |
| 2427 |
statement(qual_var_name, " = ", to_name(var.self), ".", to_member_name(var_type, mbr_idx), ";"); |
| 2428 |
}); |
| 2429 |
break; |
| 2430 |
|
| 2431 |
default: |
| 2432 |
break; |
| 2433 |
} |
| 2434 |
} |
| 2435 |
|
| 2436 |
// Copy the variable location from the original variable to the member |
| 2437 |
if (has_member_decoration(var_type.self, mbr_idx, DecorationLocation)) |
| 2438 |
{ |
| 2439 |
uint32_t locn = get_member_decoration(var_type.self, mbr_idx, DecorationLocation); |
| 2440 |
if (storage == StorageClassInput) |
| 2441 |
{ |
| 2442 |
mbr_type_id = ensure_correct_input_type(mbr_type_id, locn); |
| 2443 |
var_type.member_types[mbr_idx] = mbr_type_id; |
| 2444 |
if (storage == StorageClassInput && pull_model_inputs.count(var.self)) |
| 2445 |
ib_type.member_types[ib_mbr_idx] = build_msl_interpolant_type(mbr_type_id, is_noperspective); |
| 2446 |
else |
| 2447 |
ib_type.member_types[ib_mbr_idx] = mbr_type_id; |
| 2448 |
} |
| 2449 |
set_member_decoration(ib_type.self, ib_mbr_idx, DecorationLocation, locn); |
| 2450 |
mark_location_as_used_by_shader(locn, get<SPIRType>(mbr_type_id), storage); |
| 2451 |
} |
| 2452 |
else if (has_decoration(var.self, DecorationLocation)) |
| 2453 |
{ |
| 2454 |
// The block itself might have a location and in this case, all members of the block |
| 2455 |
// receive incrementing locations. |
| 2456 |
uint32_t locn = get_accumulated_member_location(var, mbr_idx, meta.strip_array); |
| 2457 |
if (storage == StorageClassInput) |
| 2458 |
{ |
| 2459 |
mbr_type_id = ensure_correct_input_type(mbr_type_id, locn); |
| 2460 |
var_type.member_types[mbr_idx] = mbr_type_id; |
| 2461 |
if (storage == StorageClassInput && pull_model_inputs.count(var.self)) |
| 2462 |
ib_type.member_types[ib_mbr_idx] = build_msl_interpolant_type(mbr_type_id, is_noperspective); |
| 2463 |
else |
| 2464 |
ib_type.member_types[ib_mbr_idx] = mbr_type_id; |
| 2465 |
} |
| 2466 |
set_member_decoration(ib_type.self, ib_mbr_idx, DecorationLocation, locn); |
| 2467 |
mark_location_as_used_by_shader(locn, get<SPIRType>(mbr_type_id), storage); |
| 2468 |
} |
| 2469 |
else if (is_builtin && is_tessellation_shader() && inputs_by_builtin.count(builtin)) |
| 2470 |
{ |
| 2471 |
uint32_t locn = 0; |
| 2472 |
auto builtin_itr = inputs_by_builtin.find(builtin); |
| 2473 |
if (builtin_itr != end(inputs_by_builtin)) |
| 2474 |
locn = builtin_itr->second.location; |
| 2475 |
set_member_decoration(ib_type.self, ib_mbr_idx, DecorationLocation, locn); |
| 2476 |
mark_location_as_used_by_shader(locn, get<SPIRType>(mbr_type_id), storage); |
| 2477 |
} |
| 2478 |
|
| 2479 |
// Copy the component location, if present. |
| 2480 |
if (has_member_decoration(var_type.self, mbr_idx, DecorationComponent)) |
| 2481 |
{ |
| 2482 |
uint32_t comp = get_member_decoration(var_type.self, mbr_idx, DecorationComponent); |
| 2483 |
set_member_decoration(ib_type.self, ib_mbr_idx, DecorationComponent, comp); |
| 2484 |
} |
| 2485 |
|
| 2486 |
// Mark the member as builtin if needed |
| 2487 |
if (is_builtin) |
| 2488 |
{ |
| 2489 |
set_member_decoration(ib_type.self, ib_mbr_idx, DecorationBuiltIn, builtin); |
| 2490 |
if (builtin == BuiltInPosition && storage == StorageClassOutput) |
| 2491 |
qual_pos_var_name = qual_var_name; |
| 2492 |
} |
| 2493 |
|
| 2494 |
if (storage != StorageClassInput || !pull_model_inputs.count(var.self)) |
| 2495 |
{ |
| 2496 |
// Copy interpolation decorations if needed |
| 2497 |
if (is_flat) |
| 2498 |
set_member_decoration(ib_type.self, ib_mbr_idx, DecorationFlat); |
| 2499 |
if (is_noperspective) |
| 2500 |
set_member_decoration(ib_type.self, ib_mbr_idx, DecorationNoPerspective); |
| 2501 |
if (is_centroid) |
| 2502 |
set_member_decoration(ib_type.self, ib_mbr_idx, DecorationCentroid); |
| 2503 |
if (is_sample) |
| 2504 |
set_member_decoration(ib_type.self, ib_mbr_idx, DecorationSample); |
| 2505 |
} |
| 2506 |
|
| 2507 |
set_extended_member_decoration(ib_type.self, ib_mbr_idx, SPIRVCrossDecorationInterfaceOrigID, var.self); |
| 2508 |
set_extended_member_decoration(ib_type.self, ib_mbr_idx, SPIRVCrossDecorationInterfaceMemberIndex, mbr_idx); |
| 2509 |
} |
| 2510 |
|
| 2511 |
// In Metal, the tessellation levels are stored as tightly packed half-precision floating point values. |
| 2512 |
// But, stage-in attribute offsets and strides must be multiples of four, so we can't pass the levels |
| 2513 |
// individually. Therefore, we must pass them as vectors. Triangles get a single float4, with the outer |
| 2514 |
// levels in 'xyz' and the inner level in 'w'. Quads get a float4 containing the outer levels and a |
| 2515 |
// float2 containing the inner levels. |
| 2516 |
void CompilerMSL::add_tess_level_input_to_interface_block(const std::string &ib_var_ref, SPIRType &ib_type, |
| 2517 |
SPIRVariable &var) |
| 2518 |
{ |
| 2519 |
auto &entry_func = get<SPIRFunction>(ir.default_entry_point); |
| 2520 |
auto &var_type = get_variable_element_type(var); |
| 2521 |
|
| 2522 |
BuiltIn builtin = BuiltIn(get_decoration(var.self, DecorationBuiltIn)); |
| 2523 |
|
| 2524 |
// Force the variable to have the proper name. |
| 2525 |
set_name(var.self, builtin_to_glsl(builtin, StorageClassFunction)); |
| 2526 |
|
| 2527 |
if (get_entry_point().flags.get(ExecutionModeTriangles)) |
| 2528 |
{ |
| 2529 |
// Triangles are tricky, because we want only one member in the struct. |
| 2530 |
|
| 2531 |
// We need to declare the variable early and at entry-point scope. |
| 2532 |
entry_func.add_local_variable(var.self); |
| 2533 |
vars_needing_early_declaration.push_back(var.self); |
| 2534 |
|
| 2535 |
string mbr_name = "gl_TessLevel"; |
| 2536 |
|
| 2537 |
// If we already added the other one, we can skip this step. |
| 2538 |
if (!added_builtin_tess_level) |
| 2539 |
{ |
| 2540 |
// Add a reference to the variable type to the interface struct. |
| 2541 |
uint32_t ib_mbr_idx = uint32_t(ib_type.member_types.size()); |
| 2542 |
|
| 2543 |
uint32_t type_id = build_extended_vector_type(var_type.self, 4); |
| 2544 |
|
| 2545 |
ib_type.member_types.push_back(type_id); |
| 2546 |
|
| 2547 |
// Give the member a name |
| 2548 |
set_member_name(ib_type.self, ib_mbr_idx, mbr_name); |
| 2549 |
|
| 2550 |
// There is no qualified alias since we need to flatten the internal array on return. |
| 2551 |
if (get_decoration_bitset(var.self).get(DecorationLocation)) |
| 2552 |
{ |
| 2553 |
uint32_t locn = get_decoration(var.self, DecorationLocation); |
| 2554 |
set_member_decoration(ib_type.self, ib_mbr_idx, DecorationLocation, locn); |
| 2555 |
mark_location_as_used_by_shader(locn, var_type, StorageClassInput); |
| 2556 |
} |
| 2557 |
else if (inputs_by_builtin.count(builtin)) |
| 2558 |
{ |
| 2559 |
uint32_t locn = inputs_by_builtin[builtin].location; |
| 2560 |
set_member_decoration(ib_type.self, ib_mbr_idx, DecorationLocation, locn); |
| 2561 |
mark_location_as_used_by_shader(locn, var_type, StorageClassInput); |
| 2562 |
} |
| 2563 |
|
| 2564 |
added_builtin_tess_level = true; |
| 2565 |
} |
| 2566 |
|
| 2567 |
switch (builtin) |
| 2568 |
{ |
| 2569 |
case BuiltInTessLevelOuter: |
| 2570 |
entry_func.fixup_hooks_in.push_back([=, &var]() { |
| 2571 |
statement(to_name(var.self), "[0] = ", ib_var_ref, ".", mbr_name, ".x;"); |
| 2572 |
statement(to_name(var.self), "[1] = ", ib_var_ref, ".", mbr_name, ".y;"); |
| 2573 |
statement(to_name(var.self), "[2] = ", ib_var_ref, ".", mbr_name, ".z;"); |
| 2574 |
}); |
| 2575 |
break; |
| 2576 |
|
| 2577 |
case BuiltInTessLevelInner: |
| 2578 |
entry_func.fixup_hooks_in.push_back( |
| 2579 |
[=, &var]() { statement(to_name(var.self), "[0] = ", ib_var_ref, ".", mbr_name, ".w;"); }); |
| 2580 |
break; |
| 2581 |
|
| 2582 |
default: |
| 2583 |
assert(false); |
| 2584 |
break; |
| 2585 |
} |
| 2586 |
} |
| 2587 |
else |
| 2588 |
{ |
| 2589 |
// Add a reference to the variable type to the interface struct. |
| 2590 |
uint32_t ib_mbr_idx = uint32_t(ib_type.member_types.size()); |
| 2591 |
|
| 2592 |
uint32_t type_id = build_extended_vector_type(var_type.self, builtin == BuiltInTessLevelOuter ? 4 : 2); |
| 2593 |
// Change the type of the variable, too. |
| 2594 |
uint32_t ptr_type_id = ir.increase_bound_by(1); |
| 2595 |
auto &new_var_type = set<SPIRType>(ptr_type_id, get<SPIRType>(type_id)); |
| 2596 |
new_var_type.pointer = true; |
| 2597 |
new_var_type.storage = StorageClassInput; |
| 2598 |
new_var_type.parent_type = type_id; |
| 2599 |
var.basetype = ptr_type_id; |
| 2600 |
|
| 2601 |
ib_type.member_types.push_back(type_id); |
| 2602 |
|
| 2603 |
// Give the member a name |
| 2604 |
string mbr_name = to_expression(var.self); |
| 2605 |
set_member_name(ib_type.self, ib_mbr_idx, mbr_name); |
| 2606 |
|
| 2607 |
// Since vectors can be indexed like arrays, there is no need to unpack this. We can |
| 2608 |
// just refer to the vector directly. So give it a qualified alias. |
| 2609 |
string qual_var_name = ib_var_ref + "." + mbr_name; |
| 2610 |
ir.meta[var.self].decoration.qualified_alias = qual_var_name; |
| 2611 |
|
| 2612 |
if (get_decoration_bitset(var.self).get(DecorationLocation)) |
| 2613 |
{ |
| 2614 |
uint32_t locn = get_decoration(var.self, DecorationLocation); |
| 2615 |
set_member_decoration(ib_type.self, ib_mbr_idx, DecorationLocation, locn); |
| 2616 |
mark_location_as_used_by_shader(locn, new_var_type, StorageClassInput); |
| 2617 |
} |
| 2618 |
else if (inputs_by_builtin.count(builtin)) |
| 2619 |
{ |
| 2620 |
uint32_t locn = inputs_by_builtin[builtin].location; |
| 2621 |
set_member_decoration(ib_type.self, ib_mbr_idx, DecorationLocation, locn); |
| 2622 |
mark_location_as_used_by_shader(locn, new_var_type, StorageClassInput); |
| 2623 |
} |
| 2624 |
} |
| 2625 |
} |
| 2626 |
|
| 2627 |
void CompilerMSL::add_variable_to_interface_block(StorageClass storage, const string &ib_var_ref, SPIRType &ib_type, |
| 2628 |
SPIRVariable &var, InterfaceBlockMeta &meta) |
| 2629 |
{ |
| 2630 |
auto &entry_func = get<SPIRFunction>(ir.default_entry_point); |
| 2631 |
// Tessellation control I/O variables and tessellation evaluation per-point inputs are |
| 2632 |
// usually declared as arrays. In these cases, we want to add the element type to the |
| 2633 |
// interface block, since in Metal it's the interface block itself which is arrayed. |
| 2634 |
auto &var_type = meta.strip_array ? get_variable_element_type(var) : get_variable_data_type(var); |
| 2635 |
bool is_builtin = is_builtin_variable(var); |
| 2636 |
auto builtin = BuiltIn(get_decoration(var.self, DecorationBuiltIn)); |
| 2637 |
|
| 2638 |
if (var_type.basetype == SPIRType::Struct) |
| 2639 |
{ |
| 2640 |
if (!is_builtin_type(var_type) && (!capture_output_to_buffer || storage == StorageClassInput) && |
| 2641 |
!meta.strip_array) |
| 2642 |
{ |
| 2643 |
// For I/O blocks or structs, we will need to pass the block itself around |
| 2644 |
// to functions if they are used globally in leaf functions. |
| 2645 |
// Rather than passing down member by member, |
| 2646 |
// we unflatten I/O blocks while running the shader, |
| 2647 |
// and pass the actual struct type down to leaf functions. |
| 2648 |
// We then unflatten inputs, and flatten outputs in the "fixup" stages. |
| 2649 |
entry_func.add_local_variable(var.self); |
| 2650 |
vars_needing_early_declaration.push_back(var.self); |
| 2651 |
} |
| 2652 |
|
| 2653 |
if (capture_output_to_buffer && storage != StorageClassInput && !has_decoration(var_type.self, DecorationBlock)) |
| 2654 |
{ |
| 2655 |
// In Metal tessellation shaders, the interface block itself is arrayed. This makes things |
| 2656 |
// very complicated, since stage-in structures in MSL don't support nested structures. |
| 2657 |
// Luckily, for stage-out when capturing output, we can avoid this and just add |
| 2658 |
// composite members directly, because the stage-out structure is stored to a buffer, |
| 2659 |
// not returned. |
| 2660 |
add_plain_variable_to_interface_block(storage, ib_var_ref, ib_type, var, meta); |
| 2661 |
} |
| 2662 |
else |
| 2663 |
{ |
| 2664 |
// Flatten the struct members into the interface struct |
| 2665 |
for (uint32_t mbr_idx = 0; mbr_idx < uint32_t(var_type.member_types.size()); mbr_idx++) |
| 2666 |
{ |
| 2667 |
builtin = BuiltInMax; |
| 2668 |
is_builtin = is_member_builtin(var_type, mbr_idx, &builtin); |
| 2669 |
auto &mbr_type = get<SPIRType>(var_type.member_types[mbr_idx]); |
| 2670 |
|
| 2671 |
if (!is_builtin || has_active_builtin(builtin, storage)) |
| 2672 |
{ |
| 2673 |
bool is_composite_type = is_matrix(mbr_type) || is_array(mbr_type); |
| 2674 |
bool attribute_load_store = |
| 2675 |
storage == StorageClassInput && get_execution_model() != ExecutionModelFragment; |
| 2676 |
bool storage_is_stage_io = |
| 2677 |
(storage == StorageClassInput && !(get_execution_model() == ExecutionModelTessellationControl && |
| 2678 |
msl_options.multi_patch_workgroup)) || |
| 2679 |
storage == StorageClassOutput; |
| 2680 |
|
| 2681 |
// ClipDistance always needs to be declared as user attributes. |
| 2682 |
if (builtin == BuiltInClipDistance) |
| 2683 |
is_builtin = false; |
| 2684 |
|
| 2685 |
if ((!is_builtin || attribute_load_store) && storage_is_stage_io && is_composite_type) |
| 2686 |
{ |
| 2687 |
add_composite_member_variable_to_interface_block(storage, ib_var_ref, ib_type, var, mbr_idx, |
| 2688 |
meta); |
| 2689 |
} |
| 2690 |
else |
| 2691 |
{ |
| 2692 |
add_plain_member_variable_to_interface_block(storage, ib_var_ref, ib_type, var, mbr_idx, meta); |
| 2693 |
} |
| 2694 |
} |
| 2695 |
} |
| 2696 |
} |
| 2697 |
} |
| 2698 |
else if (get_execution_model() == ExecutionModelTessellationEvaluation && storage == StorageClassInput && |
| 2699 |
!meta.strip_array && is_builtin && (builtin == BuiltInTessLevelOuter || builtin == BuiltInTessLevelInner)) |
| 2700 |
{ |
| 2701 |
add_tess_level_input_to_interface_block(ib_var_ref, ib_type, var); |
| 2702 |
} |
| 2703 |
else if (var_type.basetype == SPIRType::Boolean || var_type.basetype == SPIRType::Char || |
| 2704 |
type_is_integral(var_type) || type_is_floating_point(var_type) || var_type.basetype == SPIRType::Boolean) |
| 2705 |
{ |
| 2706 |
if (!is_builtin || has_active_builtin(builtin, storage)) |
| 2707 |
{ |
| 2708 |
bool is_composite_type = is_matrix(var_type) || is_array(var_type); |
| 2709 |
bool storage_is_stage_io = |
| 2710 |
(storage == StorageClassInput && |
| 2711 |
!(get_execution_model() == ExecutionModelTessellationControl && msl_options.multi_patch_workgroup)) || |
| 2712 |
(storage == StorageClassOutput && !capture_output_to_buffer); |
| 2713 |
bool attribute_load_store = storage == StorageClassInput && get_execution_model() != ExecutionModelFragment; |
| 2714 |
|
| 2715 |
// ClipDistance always needs to be declared as user attributes. |
| 2716 |
if (builtin == BuiltInClipDistance) |
| 2717 |
is_builtin = false; |
| 2718 |
|
| 2719 |
// MSL does not allow matrices or arrays in input or output variables, so need to handle it specially. |
| 2720 |
if ((!is_builtin || attribute_load_store) && storage_is_stage_io && is_composite_type) |
| 2721 |
{ |
| 2722 |
add_composite_variable_to_interface_block(storage, ib_var_ref, ib_type, var, meta); |
| 2723 |
} |
| 2724 |
else |
| 2725 |
{ |
| 2726 |
add_plain_variable_to_interface_block(storage, ib_var_ref, ib_type, var, meta); |
| 2727 |
} |
| 2728 |
} |
| 2729 |
} |
| 2730 |
} |
| 2731 |
|
| 2732 |
// Fix up the mapping of variables to interface member indices, which is used to compile access chains |
| 2733 |
// for per-vertex variables in a tessellation control shader. |
| 2734 |
void CompilerMSL::fix_up_interface_member_indices(StorageClass storage, uint32_t ib_type_id) |
| 2735 |
{ |
| 2736 |
// Only needed for tessellation shaders and pull-model interpolants. |
| 2737 |
// Need to redirect interface indices back to variables themselves. |
| 2738 |
// For structs, each member of the struct need a separate instance. |
| 2739 |
if (get_execution_model() != ExecutionModelTessellationControl && |
| 2740 |
!(get_execution_model() == ExecutionModelTessellationEvaluation && storage == StorageClassInput) && |
| 2741 |
!(get_execution_model() == ExecutionModelFragment && storage == StorageClassInput && |
| 2742 |
!pull_model_inputs.empty())) |
| 2743 |
return; |
| 2744 |
|
| 2745 |
auto mbr_cnt = uint32_t(ir.meta[ib_type_id].members.size()); |
| 2746 |
for (uint32_t i = 0; i < mbr_cnt; i++) |
| 2747 |
{ |
| 2748 |
uint32_t var_id = get_extended_member_decoration(ib_type_id, i, SPIRVCrossDecorationInterfaceOrigID); |
| 2749 |
if (!var_id) |
| 2750 |
continue; |
| 2751 |
auto &var = get<SPIRVariable>(var_id); |
| 2752 |
|
| 2753 |
auto &type = get_variable_element_type(var); |
| 2754 |
if (storage == StorageClassInput && type.basetype == SPIRType::Struct) |
| 2755 |
{ |
| 2756 |
uint32_t mbr_idx = get_extended_member_decoration(ib_type_id, i, SPIRVCrossDecorationInterfaceMemberIndex); |
| 2757 |
|
| 2758 |
// Only set the lowest InterfaceMemberIndex for each variable member. |
| 2759 |
// IB struct members will be emitted in-order w.r.t. interface member index. |
| 2760 |
if (!has_extended_member_decoration(var_id, mbr_idx, SPIRVCrossDecorationInterfaceMemberIndex)) |
| 2761 |
set_extended_member_decoration(var_id, mbr_idx, SPIRVCrossDecorationInterfaceMemberIndex, i); |
| 2762 |
} |
| 2763 |
else |
| 2764 |
{ |
| 2765 |
// Only set the lowest InterfaceMemberIndex for each variable. |
| 2766 |
// IB struct members will be emitted in-order w.r.t. interface member index. |
| 2767 |
if (!has_extended_decoration(var_id, SPIRVCrossDecorationInterfaceMemberIndex)) |
| 2768 |
set_extended_decoration(var_id, SPIRVCrossDecorationInterfaceMemberIndex, i); |
| 2769 |
} |
| 2770 |
} |
| 2771 |
} |
| 2772 |
|
| 2773 |
// Add an interface structure for the type of storage, which is either StorageClassInput or StorageClassOutput. |
| 2774 |
// Returns the ID of the newly added variable, or zero if no variable was added. |
| 2775 |
uint32_t CompilerMSL::add_interface_block(StorageClass storage, bool patch) |
| 2776 |
{ |
| 2777 |
// Accumulate the variables that should appear in the interface struct. |
| 2778 |
SmallVector<SPIRVariable *> vars; |
| 2779 |
bool incl_builtins = storage == StorageClassOutput || is_tessellation_shader(); |
| 2780 |
bool has_seen_barycentric = false; |
| 2781 |
|
| 2782 |
InterfaceBlockMeta meta; |
| 2783 |
|
| 2784 |
// Varying interfaces between stages which use "user()" attribute can be dealt with |
| 2785 |
// without explicit packing and unpacking of components. For any variables which link against the runtime |
| 2786 |
// in some way (vertex attributes, fragment output, etc), we'll need to deal with it somehow. |
| 2787 |
bool pack_components = |
| 2788 |
(storage == StorageClassInput && get_execution_model() == ExecutionModelVertex) || |
| 2789 |
(storage == StorageClassOutput && get_execution_model() == ExecutionModelFragment) || |
| 2790 |
(storage == StorageClassOutput && get_execution_model() == ExecutionModelVertex && capture_output_to_buffer); |
| 2791 |
|
| 2792 |
ir.for_each_typed_id<SPIRVariable>([&](uint32_t var_id, SPIRVariable &var) { |
| 2793 |
if (var.storage != storage) |
| 2794 |
return; |
| 2795 |
|
| 2796 |
auto &type = this->get<SPIRType>(var.basetype); |
| 2797 |
|
| 2798 |
bool is_builtin = is_builtin_variable(var); |
| 2799 |
auto bi_type = BuiltIn(get_decoration(var_id, DecorationBuiltIn)); |
| 2800 |
uint32_t location = get_decoration(var_id, DecorationLocation); |
| 2801 |
|
| 2802 |
// These builtins are part of the stage in/out structs. |
| 2803 |
bool is_interface_block_builtin = |
| 2804 |
(bi_type == BuiltInPosition || bi_type == BuiltInPointSize || bi_type == BuiltInClipDistance || |
| 2805 |
bi_type == BuiltInCullDistance || bi_type == BuiltInLayer || bi_type == BuiltInViewportIndex || |
| 2806 |
bi_type == BuiltInBaryCoordNV || bi_type == BuiltInBaryCoordNoPerspNV || bi_type == BuiltInFragDepth || |
| 2807 |
bi_type == BuiltInFragStencilRefEXT || bi_type == BuiltInSampleMask) || |
| 2808 |
(get_execution_model() == ExecutionModelTessellationEvaluation && |
| 2809 |
(bi_type == BuiltInTessLevelOuter || bi_type == BuiltInTessLevelInner)); |
| 2810 |
|
| 2811 |
bool is_active = interface_variable_exists_in_entry_point(var.self); |
| 2812 |
if (is_builtin && is_active) |
| 2813 |
{ |
| 2814 |
// Only emit the builtin if it's active in this entry point. Interface variable list might lie. |
| 2815 |
is_active = has_active_builtin(bi_type, storage); |
| 2816 |
} |
| 2817 |
|
| 2818 |
bool filter_patch_decoration = (has_decoration(var_id, DecorationPatch) || is_patch_block(type)) == patch; |
| 2819 |
|
| 2820 |
bool hidden = is_hidden_variable(var, incl_builtins); |
| 2821 |
|
| 2822 |
// ClipDistance is never hidden, we need to emulate it when used as an input. |
| 2823 |
if (bi_type == BuiltInClipDistance) |
| 2824 |
hidden = false; |
| 2825 |
|
| 2826 |
// It's not enough to simply avoid marking fragment outputs if the pipeline won't |
| 2827 |
// accept them. We can't put them in the struct at all, or otherwise the compiler |
| 2828 |
// complains that the outputs weren't explicitly marked. |
| 2829 |
if (get_execution_model() == ExecutionModelFragment && storage == StorageClassOutput && !patch && |
| 2830 |
((is_builtin && ((bi_type == BuiltInFragDepth && !msl_options.enable_frag_depth_builtin) || |
| 2831 |
(bi_type == BuiltInFragStencilRefEXT && !msl_options.enable_frag_stencil_ref_builtin))) || |
| 2832 |
(!is_builtin && !(msl_options.enable_frag_output_mask & (1 << location))))) |
| 2833 |
{ |
| 2834 |
hidden = true; |
| 2835 |
disabled_frag_outputs.push_back(var_id); |
| 2836 |
// If a builtin, force it to have the proper name. |
| 2837 |
if (is_builtin) |
| 2838 |
set_name(var_id, builtin_to_glsl(bi_type, StorageClassFunction)); |
| 2839 |
} |
| 2840 |
|
| 2841 |
// Barycentric inputs must be emitted in stage-in, because they can have interpolation arguments. |
| 2842 |
if (is_active && (bi_type == BuiltInBaryCoordNV || bi_type == BuiltInBaryCoordNoPerspNV)) |
| 2843 |
{ |
| 2844 |
if (has_seen_barycentric) |
| 2845 |
SPIRV_CROSS_THROW("Cannot declare both BaryCoordNV and BaryCoordNoPerspNV in same shader in MSL."); |
| 2846 |
has_seen_barycentric = true; |
| 2847 |
hidden = false; |
| 2848 |
} |
| 2849 |
|
| 2850 |
if (is_active && !hidden && type.pointer && filter_patch_decoration && |
| 2851 |
(!is_builtin || is_interface_block_builtin)) |
| 2852 |
{ |
| 2853 |
vars.push_back(&var); |
| 2854 |
|
| 2855 |
if (!is_builtin) |
| 2856 |
{ |
| 2857 |
// Need to deal specially with DecorationComponent. |
| 2858 |
// Multiple variables can alias the same Location, and try to make sure each location is declared only once. |
| 2859 |
// We will swizzle data in and out to make this work. |
| 2860 |
// We only need to consider plain variables here, not composites. |
| 2861 |
// This is only relevant for vertex inputs and fragment outputs. |
| 2862 |
// Technically tessellation as well, but it is too complicated to support. |
| 2863 |
uint32_t component = get_decoration(var_id, DecorationComponent); |
| 2864 |
if (component != 0) |
| 2865 |
{ |
| 2866 |
if (is_tessellation_shader()) |
| 2867 |
SPIRV_CROSS_THROW("Component decoration is not supported in tessellation shaders."); |
| 2868 |
else if (pack_components) |
| 2869 |
{ |
| 2870 |
auto &location_meta = meta.location_meta[location]; |
| 2871 |
location_meta.num_components = std::max(location_meta.num_components, component + type.vecsize); |
| 2872 |
} |
| 2873 |
} |
| 2874 |
} |
| 2875 |
} |
| 2876 |
}); |
| 2877 |
|
| 2878 |
// If no variables qualify, leave. |
| 2879 |
// For patch input in a tessellation evaluation shader, the per-vertex stage inputs |
| 2880 |
// are included in a special patch control point array. |
| 2881 |
if (vars.empty() && !(storage == StorageClassInput && patch && stage_in_var_id)) |
| 2882 |
return 0; |
| 2883 |
|
| 2884 |
// Add a new typed variable for this interface structure. |
| 2885 |
// The initializer expression is allocated here, but populated when the function |
| 2886 |
// declaraion is emitted, because it is cleared after each compilation pass. |
| 2887 |
uint32_t next_id = ir.increase_bound_by(3); |
| 2888 |
uint32_t ib_type_id = next_id++; |
| 2889 |
auto &ib_type = set<SPIRType>(ib_type_id); |
| 2890 |
ib_type.basetype = SPIRType::Struct; |
| 2891 |
ib_type.storage = storage; |
| 2892 |
set_decoration(ib_type_id, DecorationBlock); |
| 2893 |
|
| 2894 |
uint32_t ib_var_id = next_id++; |
| 2895 |
auto &var = set<SPIRVariable>(ib_var_id, ib_type_id, storage, 0); |
| 2896 |
var.initializer = next_id++; |
| 2897 |
|
| 2898 |
string ib_var_ref; |
| 2899 |
auto &entry_func = get<SPIRFunction>(ir.default_entry_point); |
| 2900 |
switch (storage) |
| 2901 |
{ |
| 2902 |
case StorageClassInput: |
| 2903 |
ib_var_ref = patch ? patch_stage_in_var_name : stage_in_var_name; |
| 2904 |
if (get_execution_model() == ExecutionModelTessellationControl) |
| 2905 |
{ |
| 2906 |
// Add a hook to populate the shared workgroup memory containing the gl_in array. |
| 2907 |
entry_func.fixup_hooks_in.push_back([=]() { |
| 2908 |
// Can't use PatchVertices, PrimitiveId, or InvocationId yet; the hooks for those may not have run yet. |
| 2909 |
if (msl_options.multi_patch_workgroup) |
| 2910 |
{ |
| 2911 |
// n.b. builtin_invocation_id_id here is the dispatch global invocation ID, |
| 2912 |
// not the TC invocation ID. |
| 2913 |
statement("device ", to_name(ir.default_entry_point), "_", ib_var_ref, "* gl_in = &", |
| 2914 |
input_buffer_var_name, "[min(", to_expression(builtin_invocation_id_id), ".x / ", |
| 2915 |
get_entry_point().output_vertices, |
| 2916 |
", spvIndirectParams[1] - 1) * spvIndirectParams[0]];"); |
| 2917 |
} |
| 2918 |
else |
| 2919 |
{ |
| 2920 |
// It's safe to use InvocationId here because it's directly mapped to a |
| 2921 |
// Metal builtin, and therefore doesn't need a hook. |
| 2922 |
statement("if (", to_expression(builtin_invocation_id_id), " < spvIndirectParams[0])"); |
| 2923 |
statement(" ", input_wg_var_name, "[", to_expression(builtin_invocation_id_id), |
| 2924 |
"] = ", ib_var_ref, ";"); |
| 2925 |
statement("threadgroup_barrier(mem_flags::mem_threadgroup);"); |
| 2926 |
statement("if (", to_expression(builtin_invocation_id_id), |
| 2927 |
" >= ", get_entry_point().output_vertices, ")"); |
| 2928 |
statement(" return;"); |
| 2929 |
} |
| 2930 |
}); |
| 2931 |
} |
| 2932 |
break; |
| 2933 |
|
| 2934 |
case StorageClassOutput: |
| 2935 |
{ |
| 2936 |
ib_var_ref = patch ? patch_stage_out_var_name : stage_out_var_name; |
| 2937 |
|
| 2938 |
// Add the output interface struct as a local variable to the entry function. |
| 2939 |
// If the entry point should return the output struct, set the entry function |
| 2940 |
// to return the output interface struct, otherwise to return nothing. |
| 2941 |
// Indicate the output var requires early initialization. |
| 2942 |
bool ep_should_return_output = !get_is_rasterization_disabled(); |
| 2943 |
uint32_t rtn_id = ep_should_return_output ? ib_var_id : 0; |
| 2944 |
if (!capture_output_to_buffer) |
| 2945 |
{ |
| 2946 |
entry_func.add_local_variable(ib_var_id); |
| 2947 |
for (auto &blk_id : entry_func.blocks) |
| 2948 |
{ |
| 2949 |
auto &blk = get<SPIRBlock>(blk_id); |
| 2950 |
if (blk.terminator == SPIRBlock::Return) |
| 2951 |
blk.return_value = rtn_id; |
| 2952 |
} |
| 2953 |
vars_needing_early_declaration.push_back(ib_var_id); |
| 2954 |
} |
| 2955 |
else |
| 2956 |
{ |
| 2957 |
switch (get_execution_model()) |
| 2958 |
{ |
| 2959 |
case ExecutionModelVertex: |
| 2960 |
case ExecutionModelTessellationEvaluation: |
| 2961 |
// Instead of declaring a struct variable to hold the output and then |
| 2962 |
// copying that to the output buffer, we'll declare the output variable |
| 2963 |
// as a reference to the final output element in the buffer. Then we can |
| 2964 |
// avoid the extra copy. |
| 2965 |
entry_func.fixup_hooks_in.push_back([=]() { |
| 2966 |
if (stage_out_var_id) |
| 2967 |
{ |
| 2968 |
// The first member of the indirect buffer is always the number of vertices |
| 2969 |
// to draw. |
| 2970 |
// We zero-base the InstanceID & VertexID variables for HLSL emulation elsewhere, so don't do it twice |
| 2971 |
if (get_execution_model() == ExecutionModelVertex && msl_options.vertex_for_tessellation) |
| 2972 |
{ |
| 2973 |
statement("device ", to_name(ir.default_entry_point), "_", ib_var_ref, "& ", ib_var_ref, |
| 2974 |
" = ", output_buffer_var_name, "[", to_expression(builtin_invocation_id_id), |
| 2975 |
".y * ", to_expression(builtin_stage_input_size_id), ".x + ", |
| 2976 |
to_expression(builtin_invocation_id_id), ".x];"); |
| 2977 |
} |
| 2978 |
else if (msl_options.enable_base_index_zero) |
| 2979 |
{ |
| 2980 |
statement("device ", to_name(ir.default_entry_point), "_", ib_var_ref, "& ", ib_var_ref, |
| 2981 |
" = ", output_buffer_var_name, "[", to_expression(builtin_instance_idx_id), |
| 2982 |
" * spvIndirectParams[0] + ", to_expression(builtin_vertex_idx_id), "];"); |
| 2983 |
} |
| 2984 |
else |
| 2985 |
{ |
| 2986 |
statement("device ", to_name(ir.default_entry_point), "_", ib_var_ref, "& ", ib_var_ref, |
| 2987 |
" = ", output_buffer_var_name, "[(", to_expression(builtin_instance_idx_id), |
| 2988 |
" - ", to_expression(builtin_base_instance_id), ") * spvIndirectParams[0] + ", |
| 2989 |
to_expression(builtin_vertex_idx_id), " - ", |
| 2990 |
to_expression(builtin_base_vertex_id), "];"); |
| 2991 |
} |
| 2992 |
} |
| 2993 |
}); |
| 2994 |
break; |
| 2995 |
case ExecutionModelTessellationControl: |
| 2996 |
if (msl_options.multi_patch_workgroup) |
| 2997 |
{ |
| 2998 |
// We cannot use PrimitiveId here, because the hook may not have run yet. |
| 2999 |
if (patch) |
| 3000 |
{ |
| 3001 |
entry_func.fixup_hooks_in.push_back([=]() { |
| 3002 |
statement("device ", to_name(ir.default_entry_point), "_", ib_var_ref, "& ", ib_var_ref, |
| 3003 |
" = ", patch_output_buffer_var_name, "[", to_expression(builtin_invocation_id_id), |
| 3004 |
".x / ", get_entry_point().output_vertices, "];"); |
| 3005 |
}); |
| 3006 |
} |
| 3007 |
else |
| 3008 |
{ |
| 3009 |
entry_func.fixup_hooks_in.push_back([=]() { |
| 3010 |
statement("device ", to_name(ir.default_entry_point), "_", ib_var_ref, "* gl_out = &", |
| 3011 |
output_buffer_var_name, "[", to_expression(builtin_invocation_id_id), ".x - ", |
| 3012 |
to_expression(builtin_invocation_id_id), ".x % ", |
| 3013 |
get_entry_point().output_vertices, "];"); |
| 3014 |
}); |
| 3015 |
} |
| 3016 |
} |
| 3017 |
else |
| 3018 |
{ |
| 3019 |
if (patch) |
| 3020 |
{ |
| 3021 |
entry_func.fixup_hooks_in.push_back([=]() { |
| 3022 |
statement("device ", to_name(ir.default_entry_point), "_", ib_var_ref, "& ", ib_var_ref, |
| 3023 |
" = ", patch_output_buffer_var_name, "[", to_expression(builtin_primitive_id_id), |
| 3024 |
"];"); |
| 3025 |
}); |
| 3026 |
} |
| 3027 |
else |
| 3028 |
{ |
| 3029 |
entry_func.fixup_hooks_in.push_back([=]() { |
| 3030 |
statement("device ", to_name(ir.default_entry_point), "_", ib_var_ref, "* gl_out = &", |
| 3031 |
output_buffer_var_name, "[", to_expression(builtin_primitive_id_id), " * ", |
| 3032 |
get_entry_point().output_vertices, "];"); |
| 3033 |
}); |
| 3034 |
} |
| 3035 |
} |
| 3036 |
break; |
| 3037 |
default: |
| 3038 |
break; |
| 3039 |
} |
| 3040 |
} |
| 3041 |
break; |
| 3042 |
} |
| 3043 |
|
| 3044 |
default: |
| 3045 |
break; |
| 3046 |
} |
| 3047 |
|
| 3048 |
set_name(ib_type_id, to_name(ir.default_entry_point) + "_" + ib_var_ref); |
| 3049 |
set_name(ib_var_id, ib_var_ref); |
| 3050 |
|
| 3051 |
for (auto *p_var : vars) |
| 3052 |
{ |
| 3053 |
bool strip_array = |
| 3054 |
(get_execution_model() == ExecutionModelTessellationControl || |
| 3055 |
(get_execution_model() == ExecutionModelTessellationEvaluation && storage == StorageClassInput)) && |
| 3056 |
!patch; |
| 3057 |
|
| 3058 |
meta.strip_array = strip_array; |
| 3059 |
add_variable_to_interface_block(storage, ib_var_ref, ib_type, *p_var, meta); |
| 3060 |
} |
| 3061 |
|
| 3062 |
if (get_execution_model() == ExecutionModelTessellationControl && msl_options.multi_patch_workgroup && |
| 3063 |
storage == StorageClassInput) |
| 3064 |
{ |
| 3065 |
// For tessellation control inputs, add all outputs from the vertex shader to ensure |
| 3066 |
// the struct containing them is the correct size and layout. |
| 3067 |
for (auto &input : inputs_by_location) |
| 3068 |
{ |
| 3069 |
if (is_msl_shader_input_used(input.first)) |
| 3070 |
continue; |
| 3071 |
|
| 3072 |
// Create a fake variable to put at the location. |
| 3073 |
uint32_t offset = ir.increase_bound_by(4); |
| 3074 |
uint32_t type_id = offset; |
| 3075 |
uint32_t array_type_id = offset + 1; |
| 3076 |
uint32_t ptr_type_id = offset + 2; |
| 3077 |
uint32_t var_id = offset + 3; |
| 3078 |
|
| 3079 |
SPIRType type; |
| 3080 |
switch (input.second.format) |
| 3081 |
{ |
| 3082 |
case MSL_SHADER_INPUT_FORMAT_UINT16: |
| 3083 |
case MSL_SHADER_INPUT_FORMAT_ANY16: |
| 3084 |
type.basetype = SPIRType::UShort; |
| 3085 |
type.width = 16; |
| 3086 |
break; |
| 3087 |
case MSL_SHADER_INPUT_FORMAT_ANY32: |
| 3088 |
default: |
| 3089 |
type.basetype = SPIRType::UInt; |
| 3090 |
type.width = 32; |
| 3091 |
break; |
| 3092 |
} |
| 3093 |
type.vecsize = input.second.vecsize; |
| 3094 |
set<SPIRType>(type_id, type); |
| 3095 |
|
| 3096 |
type.array.push_back(0); |
| 3097 |
type.array_size_literal.push_back(true); |
| 3098 |
type.parent_type = type_id; |
| 3099 |
set<SPIRType>(array_type_id, type); |
| 3100 |
|
| 3101 |
type.pointer = true; |
| 3102 |
type.parent_type = array_type_id; |
| 3103 |
type.storage = storage; |
| 3104 |
auto &ptr_type = set<SPIRType>(ptr_type_id, type); |
| 3105 |
ptr_type.self = array_type_id; |
| 3106 |
|
| 3107 |
auto &fake_var = set<SPIRVariable>(var_id, ptr_type_id, storage); |
| 3108 |
set_decoration(var_id, DecorationLocation, input.first); |
| 3109 |
meta.strip_array = true; |
| 3110 |
add_variable_to_interface_block(storage, ib_var_ref, ib_type, fake_var, meta); |
| 3111 |
} |
| 3112 |
} |
| 3113 |
|
| 3114 |
// Sort the members of the structure by their locations. |
| 3115 |
MemberSorter member_sorter(ib_type, ir.meta[ib_type_id], MemberSorter::Location); |
| 3116 |
member_sorter.sort(); |
| 3117 |
|
| 3118 |
// The member indices were saved to the original variables, but after the members |
| 3119 |
// were sorted, those indices are now likely incorrect. Fix those up now. |
| 3120 |
if (!patch) |
| 3121 |
fix_up_interface_member_indices(storage, ib_type_id); |
| 3122 |
|
| 3123 |
// For patch inputs, add one more member, holding the array of control point data. |
| 3124 |
if (get_execution_model() == ExecutionModelTessellationEvaluation && storage == StorageClassInput && patch && |
| 3125 |
stage_in_var_id) |
| 3126 |
{ |
| 3127 |
uint32_t pcp_type_id = ir.increase_bound_by(1); |
| 3128 |
auto &pcp_type = set<SPIRType>(pcp_type_id, ib_type); |
| 3129 |
pcp_type.basetype = SPIRType::ControlPointArray; |
| 3130 |
pcp_type.parent_type = pcp_type.type_alias = get_stage_in_struct_type().self; |
| 3131 |
pcp_type.storage = storage; |
| 3132 |
ir.meta[pcp_type_id] = ir.meta[ib_type.self]; |
| 3133 |
uint32_t mbr_idx = uint32_t(ib_type.member_types.size()); |
| 3134 |
ib_type.member_types.push_back(pcp_type_id); |
| 3135 |
set_member_name(ib_type.self, mbr_idx, "gl_in"); |
| 3136 |
} |
| 3137 |
|
| 3138 |
return ib_var_id; |
| 3139 |
} |
| 3140 |
|
| 3141 |
uint32_t CompilerMSL::add_interface_block_pointer(uint32_t ib_var_id, StorageClass storage) |
| 3142 |
{ |
| 3143 |
if (!ib_var_id) |
| 3144 |
return 0; |
| 3145 |
|
| 3146 |
uint32_t ib_ptr_var_id; |
| 3147 |
uint32_t next_id = ir.increase_bound_by(3); |
| 3148 |
auto &ib_type = expression_type(ib_var_id); |
| 3149 |
if (get_execution_model() == ExecutionModelTessellationControl) |
| 3150 |
{ |
| 3151 |
// Tessellation control per-vertex I/O is presented as an array, so we must |
| 3152 |
// do the same with our struct here. |
| 3153 |
uint32_t ib_ptr_type_id = next_id++; |
| 3154 |
auto &ib_ptr_type = set<SPIRType>(ib_ptr_type_id, ib_type); |
| 3155 |
ib_ptr_type.parent_type = ib_ptr_type.type_alias = ib_type.self; |
| 3156 |
ib_ptr_type.pointer = true; |
| 3157 |
ib_ptr_type.storage = |
| 3158 |
storage == StorageClassInput ? |
| 3159 |
(msl_options.multi_patch_workgroup ? StorageClassStorageBuffer : StorageClassWorkgroup) : |
| 3160 |
StorageClassStorageBuffer; |
| 3161 |
ir.meta[ib_ptr_type_id] = ir.meta[ib_type.self]; |
| 3162 |
// To ensure that get_variable_data_type() doesn't strip off the pointer, |
| 3163 |
// which we need, use another pointer. |
| 3164 |
uint32_t ib_ptr_ptr_type_id = next_id++; |
| 3165 |
auto &ib_ptr_ptr_type = set<SPIRType>(ib_ptr_ptr_type_id, ib_ptr_type); |
| 3166 |
ib_ptr_ptr_type.parent_type = ib_ptr_type_id; |
| 3167 |
ib_ptr_ptr_type.type_alias = ib_type.self; |
| 3168 |
ib_ptr_ptr_type.storage = StorageClassFunction; |
| 3169 |
ir.meta[ib_ptr_ptr_type_id] = ir.meta[ib_type.self]; |
| 3170 |
|
| 3171 |
ib_ptr_var_id = next_id; |
| 3172 |
set<SPIRVariable>(ib_ptr_var_id, ib_ptr_ptr_type_id, StorageClassFunction, 0); |
| 3173 |
set_name(ib_ptr_var_id, storage == StorageClassInput ? "gl_in" : "gl_out"); |
| 3174 |
} |
| 3175 |
else |
| 3176 |
{ |
| 3177 |
// Tessellation evaluation per-vertex inputs are also presented as arrays. |
| 3178 |
// But, in Metal, this array uses a very special type, 'patch_control_point<T>', |
| 3179 |
// which is a container that can be used to access the control point data. |
| 3180 |
// To represent this, a special 'ControlPointArray' type has been added to the |
| 3181 |
// SPIRV-Cross type system. It should only be generated by and seen in the MSL |
| 3182 |
// backend (i.e. this one). |
| 3183 |
uint32_t pcp_type_id = next_id++; |
| 3184 |
auto &pcp_type = set<SPIRType>(pcp_type_id, ib_type); |
| 3185 |
pcp_type.basetype = SPIRType::ControlPointArray; |
| 3186 |
pcp_type.parent_type = pcp_type.type_alias = ib_type.self; |
| 3187 |
pcp_type.storage = storage; |
| 3188 |
ir.meta[pcp_type_id] = ir.meta[ib_type.self]; |
| 3189 |
|
| 3190 |
ib_ptr_var_id = next_id; |
| 3191 |
set<SPIRVariable>(ib_ptr_var_id, pcp_type_id, storage, 0); |
| 3192 |
set_name(ib_ptr_var_id, "gl_in"); |
| 3193 |
ir.meta[ib_ptr_var_id].decoration.qualified_alias = join(patch_stage_in_var_name, ".gl_in"); |
| 3194 |
} |
| 3195 |
return ib_ptr_var_id; |
| 3196 |
} |
| 3197 |
|
| 3198 |
// Ensure that the type is compatible with the builtin. |
| 3199 |
// If it is, simply return the given type ID. |
| 3200 |
// Otherwise, create a new type, and return it's ID. |
| 3201 |
uint32_t CompilerMSL::ensure_correct_builtin_type(uint32_t type_id, BuiltIn builtin) |
| 3202 |
{ |
| 3203 |
auto &type = get<SPIRType>(type_id); |
| 3204 |
|
| 3205 |
if ((builtin == BuiltInSampleMask && is_array(type)) || |
| 3206 |
((builtin == BuiltInLayer || builtin == BuiltInViewportIndex || builtin == BuiltInFragStencilRefEXT) && |
| 3207 |
type.basetype != SPIRType::UInt)) |
| 3208 |
{ |
| 3209 |
uint32_t next_id = ir.increase_bound_by(type.pointer ? 2 : 1); |
| 3210 |
uint32_t base_type_id = next_id++; |
| 3211 |
auto &base_type = set<SPIRType>(base_type_id); |
| 3212 |
base_type.basetype = SPIRType::UInt; |
| 3213 |
base_type.width = 32; |
| 3214 |
|
| 3215 |
if (!type.pointer) |
| 3216 |
return base_type_id; |
| 3217 |
|
| 3218 |
uint32_t ptr_type_id = next_id++; |
| 3219 |
auto &ptr_type = set<SPIRType>(ptr_type_id); |
| 3220 |
ptr_type = base_type; |
| 3221 |
ptr_type.pointer = true; |
| 3222 |
ptr_type.storage = type.storage; |
| 3223 |
ptr_type.parent_type = base_type_id; |
| 3224 |
return ptr_type_id; |
| 3225 |
} |
| 3226 |
|
| 3227 |
return type_id; |
| 3228 |
} |
| 3229 |
|
| 3230 |
// Ensure that the type is compatible with the shader input. |
| 3231 |
// If it is, simply return the given type ID. |
| 3232 |
// Otherwise, create a new type, and return its ID. |
| 3233 |
uint32_t CompilerMSL::ensure_correct_input_type(uint32_t type_id, uint32_t location, uint32_t num_components) |
| 3234 |
{ |
| 3235 |
auto &type = get<SPIRType>(type_id); |
| 3236 |
|
| 3237 |
auto p_va = inputs_by_location.find(location); |
| 3238 |
if (p_va == end(inputs_by_location)) |
| 3239 |
{ |
| 3240 |
if (num_components > type.vecsize) |
| 3241 |
return build_extended_vector_type(type_id, num_components); |
| 3242 |
else |
| 3243 |
return type_id; |
| 3244 |
} |
| 3245 |
|
| 3246 |
if (num_components == 0) |
| 3247 |
num_components = p_va->second.vecsize; |
| 3248 |
|
| 3249 |
switch (p_va->second.format) |
| 3250 |
{ |
| 3251 |
case MSL_SHADER_INPUT_FORMAT_UINT8: |
| 3252 |
{ |
| 3253 |
switch (type.basetype) |
| 3254 |
{ |
| 3255 |
case SPIRType::UByte: |
| 3256 |
case SPIRType::UShort: |
| 3257 |
case SPIRType::UInt: |
| 3258 |
if (num_components > type.vecsize) |
| 3259 |
return build_extended_vector_type(type_id, num_components); |
| 3260 |
else |
| 3261 |
return type_id; |
| 3262 |
|
| 3263 |
case SPIRType::Short: |
| 3264 |
return build_extended_vector_type(type_id, num_components > type.vecsize ? num_components : type.vecsize, |
| 3265 |
SPIRType::UShort); |
| 3266 |
case SPIRType::Int: |
| 3267 |
return build_extended_vector_type(type_id, num_components > type.vecsize ? num_components : type.vecsize, |
| 3268 |
SPIRType::UInt); |
| 3269 |
|
| 3270 |
default: |
| 3271 |
SPIRV_CROSS_THROW("Vertex attribute type mismatch between host and shader"); |
| 3272 |
} |
| 3273 |
} |
| 3274 |
|
| 3275 |
case MSL_SHADER_INPUT_FORMAT_UINT16: |
| 3276 |
{ |
| 3277 |
switch (type.basetype) |
| 3278 |
{ |
| 3279 |
case SPIRType::UShort: |
| 3280 |
case SPIRType::UInt: |
| 3281 |
if (num_components > type.vecsize) |
| 3282 |
return build_extended_vector_type(type_id, num_components); |
| 3283 |
else |
| 3284 |
return type_id; |
| 3285 |
|
| 3286 |
case SPIRType::Int: |
| 3287 |
return build_extended_vector_type(type_id, num_components > type.vecsize ? num_components : type.vecsize, |
| 3288 |
SPIRType::UInt); |
| 3289 |
|
| 3290 |
default: |
| 3291 |
SPIRV_CROSS_THROW("Vertex attribute type mismatch between host and shader"); |
| 3292 |
} |
| 3293 |
} |
| 3294 |
|
| 3295 |
default: |
| 3296 |
if (num_components > type.vecsize) |
| 3297 |
type_id = build_extended_vector_type(type_id, num_components); |
| 3298 |
break; |
| 3299 |
} |
| 3300 |
|
| 3301 |
return type_id; |
| 3302 |
} |
| 3303 |
|
| 3304 |
void CompilerMSL::mark_struct_members_packed(const SPIRType &type) |
| 3305 |
{ |
| 3306 |
set_extended_decoration(type.self, SPIRVCrossDecorationPhysicalTypePacked); |
| 3307 |
|
| 3308 |
// Problem case! Struct needs to be placed at an awkward alignment. |
| 3309 |
// Mark every member of the child struct as packed. |
| 3310 |
uint32_t mbr_cnt = uint32_t(type.member_types.size()); |
| 3311 |
for (uint32_t i = 0; i < mbr_cnt; i++) |
| 3312 |
{ |
| 3313 |
auto &mbr_type = get<SPIRType>(type.member_types[i]); |
| 3314 |
if (mbr_type.basetype == SPIRType::Struct) |
| 3315 |
{ |
| 3316 |
// Recursively mark structs as packed. |
| 3317 |
auto *struct_type = &mbr_type; |
| 3318 |
while (!struct_type->array.empty()) |
| 3319 |
struct_type = &get<SPIRType>(struct_type->parent_type); |
| 3320 |
mark_struct_members_packed(*struct_type); |
| 3321 |
} |
| 3322 |
else if (!is_scalar(mbr_type)) |
| 3323 |
set_extended_member_decoration(type.self, i, SPIRVCrossDecorationPhysicalTypePacked); |
| 3324 |
} |
| 3325 |
} |
| 3326 |
|
| 3327 |
void CompilerMSL::mark_scalar_layout_structs(const SPIRType &type) |
| 3328 |
{ |
| 3329 |
uint32_t mbr_cnt = uint32_t(type.member_types.size()); |
| 3330 |
for (uint32_t i = 0; i < mbr_cnt; i++) |
| 3331 |
{ |
| 3332 |
auto &mbr_type = get<SPIRType>(type.member_types[i]); |
| 3333 |
if (mbr_type.basetype == SPIRType::Struct) |
| 3334 |
{ |
| 3335 |
auto *struct_type = &mbr_type; |
| 3336 |
while (!struct_type->array.empty()) |
| 3337 |
struct_type = &get<SPIRType>(struct_type->parent_type); |
| 3338 |
|
| 3339 |
if (has_extended_decoration(struct_type->self, SPIRVCrossDecorationPhysicalTypePacked)) |
| 3340 |
continue; |
| 3341 |
|
| 3342 |
uint32_t msl_alignment = get_declared_struct_member_alignment_msl(type, i); |
| 3343 |
uint32_t msl_size = get_declared_struct_member_size_msl(type, i); |
| 3344 |
uint32_t spirv_offset = type_struct_member_offset(type, i); |
| 3345 |
uint32_t spirv_offset_next; |
| 3346 |
if (i + 1 < mbr_cnt) |
| 3347 |
spirv_offset_next = type_struct_member_offset(type, i + 1); |
| 3348 |
else |
| 3349 |
spirv_offset_next = spirv_offset + msl_size; |
| 3350 |
|
| 3351 |
// Both are complicated cases. In scalar layout, a struct of float3 might just consume 12 bytes, |
| 3352 |
// and the next member will be placed at offset 12. |
| 3353 |
bool struct_is_misaligned = (spirv_offset % msl_alignment) != 0; |
| 3354 |
bool struct_is_too_large = spirv_offset + msl_size > spirv_offset_next; |
| 3355 |
uint32_t array_stride = 0; |
| 3356 |
bool struct_needs_explicit_padding = false; |
| 3357 |
|
| 3358 |
// Verify that if a struct is used as an array that ArrayStride matches the effective size of the struct. |
| 3359 |
if (!mbr_type.array.empty()) |
| 3360 |
{ |
| 3361 |
array_stride = type_struct_member_array_stride(type, i); |
| 3362 |
uint32_t dimensions = uint32_t(mbr_type.array.size() - 1); |
| 3363 |
for (uint32_t dim = 0; dim < dimensions; dim++) |
| 3364 |
{ |
| 3365 |
uint32_t array_size = to_array_size_literal(mbr_type, dim); |
| 3366 |
array_stride /= max(array_size, 1u); |
| 3367 |
} |
| 3368 |
|
| 3369 |
// Set expected struct size based on ArrayStride. |
| 3370 |
struct_needs_explicit_padding = true; |
| 3371 |
|
| 3372 |
// If struct size is larger than array stride, we might be able to fit, if we tightly pack. |
| 3373 |
if (get_declared_struct_size_msl(*struct_type) > array_stride) |
| 3374 |
struct_is_too_large = true; |
| 3375 |
} |
| 3376 |
|
| 3377 |
if (struct_is_misaligned || struct_is_too_large) |
| 3378 |
mark_struct_members_packed(*struct_type); |
| 3379 |
mark_scalar_layout_structs(*struct_type); |
| 3380 |
|
| 3381 |
if (struct_needs_explicit_padding) |
| 3382 |
{ |
| 3383 |
msl_size = get_declared_struct_size_msl(*struct_type, true, true); |
| 3384 |
if (array_stride < msl_size) |
| 3385 |
{ |
| 3386 |
SPIRV_CROSS_THROW("Cannot express an array stride smaller than size of struct type."); |
| 3387 |
} |
| 3388 |
else |
| 3389 |
{ |
| 3390 |
if (has_extended_decoration(struct_type->self, SPIRVCrossDecorationPaddingTarget)) |
| 3391 |
{ |
| 3392 |
if (array_stride != |
| 3393 |
get_extended_decoration(struct_type->self, SPIRVCrossDecorationPaddingTarget)) |
| 3394 |
SPIRV_CROSS_THROW( |
| 3395 |
"A struct is used with different array strides. Cannot express this in MSL."); |
| 3396 |
} |
| 3397 |
else |
| 3398 |
set_extended_decoration(struct_type->self, SPIRVCrossDecorationPaddingTarget, array_stride); |
| 3399 |
} |
| 3400 |
} |
| 3401 |
} |
| 3402 |
} |
| 3403 |
} |
| 3404 |
|
| 3405 |
// Sort the members of the struct type by offset, and pack and then pad members where needed |
| 3406 |
// to align MSL members with SPIR-V offsets. The struct members are iterated twice. Packing |
| 3407 |
// occurs first, followed by padding, because packing a member reduces both its size and its |
| 3408 |
// natural alignment, possibly requiring a padding member to be added ahead of it. |
| 3409 |
void CompilerMSL::align_struct(SPIRType &ib_type, unordered_set<uint32_t> &aligned_structs) |
| 3410 |
{ |
| 3411 |
// We align structs recursively, so stop any redundant work. |
| 3412 |
ID &ib_type_id = ib_type.self; |
| 3413 |
if (aligned_structs.count(ib_type_id)) |
| 3414 |
return; |
| 3415 |
aligned_structs.insert(ib_type_id); |
| 3416 |
|
| 3417 |
// Sort the members of the interface structure by their offset. |
| 3418 |
// They should already be sorted per SPIR-V spec anyway. |
| 3419 |
MemberSorter member_sorter(ib_type, ir.meta[ib_type_id], MemberSorter::Offset); |
| 3420 |
member_sorter.sort(); |
| 3421 |
|
| 3422 |
auto mbr_cnt = uint32_t(ib_type.member_types.size()); |
| 3423 |
|
| 3424 |
for (uint32_t mbr_idx = 0; mbr_idx < mbr_cnt; mbr_idx++) |
| 3425 |
{ |
| 3426 |
// Pack any dependent struct types before we pack a parent struct. |
| 3427 |
auto &mbr_type = get<SPIRType>(ib_type.member_types[mbr_idx]); |
| 3428 |
if (mbr_type.basetype == SPIRType::Struct) |
| 3429 |
align_struct(mbr_type, aligned_structs); |
| 3430 |
} |
| 3431 |
|
| 3432 |
// Test the alignment of each member, and if a member should be closer to the previous |
| 3433 |
// member than the default spacing expects, it is likely that the previous member is in |
| 3434 |
// a packed format. If so, and the previous member is packable, pack it. |
| 3435 |
// For example ... this applies to any 3-element vector that is followed by a scalar. |
| 3436 |
uint32_t msl_offset = 0; |
| 3437 |
for (uint32_t mbr_idx = 0; mbr_idx < mbr_cnt; mbr_idx++) |
| 3438 |
{ |
| 3439 |
// This checks the member in isolation, if the member needs some kind of type remapping to conform to SPIR-V |
| 3440 |
// offsets, array strides and matrix strides. |
| 3441 |
ensure_member_packing_rules_msl(ib_type, mbr_idx); |
| 3442 |
|
| 3443 |
// Align current offset to the current member's default alignment. If the member was packed, it will observe |
| 3444 |
// the updated alignment here. |
| 3445 |
uint32_t msl_align_mask = get_declared_struct_member_alignment_msl(ib_type, mbr_idx) - 1; |
| 3446 |
uint32_t aligned_msl_offset = (msl_offset + msl_align_mask) & ~msl_align_mask; |
| 3447 |
|
| 3448 |
// Fetch the member offset as declared in the SPIRV. |
| 3449 |
uint32_t spirv_mbr_offset = get_member_decoration(ib_type_id, mbr_idx, DecorationOffset); |
| 3450 |
if (spirv_mbr_offset > aligned_msl_offset) |
| 3451 |
{ |
| 3452 |
// Since MSL and SPIR-V have slightly different struct member alignment and |
| 3453 |
// size rules, we'll pad to standard C-packing rules with a char[] array. If the member is farther |
| 3454 |
// away than C-packing, expects, add an inert padding member before the the member. |
| 3455 |
uint32_t padding_bytes = spirv_mbr_offset - aligned_msl_offset; |
| 3456 |
set_extended_member_decoration(ib_type_id, mbr_idx, SPIRVCrossDecorationPaddingTarget, padding_bytes); |
| 3457 |
|
| 3458 |
// Re-align as a sanity check that aligning post-padding matches up. |
| 3459 |
msl_offset += padding_bytes; |
| 3460 |
aligned_msl_offset = (msl_offset + msl_align_mask) & ~msl_align_mask; |
| 3461 |
} |
| 3462 |
else if (spirv_mbr_offset < aligned_msl_offset) |
| 3463 |
{ |
| 3464 |
// This should not happen, but deal with unexpected scenarios. |
| 3465 |
// It *might* happen if a sub-struct has a larger alignment requirement in MSL than SPIR-V. |
| 3466 |
SPIRV_CROSS_THROW("Cannot represent buffer block correctly in MSL."); |
| 3467 |
} |
| 3468 |
|
| 3469 |
assert(aligned_msl_offset == spirv_mbr_offset); |
| 3470 |
|
| 3471 |
// Increment the current offset to be positioned immediately after the current member. |
| 3472 |
// Don't do this for the last member since it can be unsized, and it is not relevant for padding purposes here. |
| 3473 |
if (mbr_idx + 1 < mbr_cnt) |
| 3474 |
msl_offset = aligned_msl_offset + get_declared_struct_member_size_msl(ib_type, mbr_idx); |
| 3475 |
} |
| 3476 |
} |
| 3477 |
|
| 3478 |
bool CompilerMSL::validate_member_packing_rules_msl(const SPIRType &type, uint32_t index) const |
| 3479 |
{ |
| 3480 |
auto &mbr_type = get<SPIRType>(type.member_types[index]); |
| 3481 |
uint32_t spirv_offset = get_member_decoration(type.self, index, DecorationOffset); |
| 3482 |
|
| 3483 |
if (index + 1 < type.member_types.size()) |
| 3484 |
{ |
| 3485 |
// First, we will check offsets. If SPIR-V offset + MSL size > SPIR-V offset of next member, |
| 3486 |
// we *must* perform some kind of remapping, no way getting around it. |
| 3487 |
// We can always pad after this member if necessary, so that case is fine. |
| 3488 |
uint32_t spirv_offset_next = get_member_decoration(type.self, index + 1, DecorationOffset); |
| 3489 |
assert(spirv_offset_next >= spirv_offset); |
| 3490 |
uint32_t maximum_size = spirv_offset_next - spirv_offset; |
| 3491 |
uint32_t msl_mbr_size = get_declared_struct_member_size_msl(type, index); |
| 3492 |
if (msl_mbr_size > maximum_size) |
| 3493 |
return false; |
| 3494 |
} |
| 3495 |
|
| 3496 |
if (!mbr_type.array.empty()) |
| 3497 |
{ |
| 3498 |
// If we have an array type, array stride must match exactly with SPIR-V. |
| 3499 |
|
| 3500 |
// An exception to this requirement is if we have one array element. |
| 3501 |
// This comes from DX scalar layout workaround. |
| 3502 |
// 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. |
| 3503 |
// In OpAccessChain with logical memory models, access chains must be in-bounds in SPIR-V specification. |
| 3504 |
bool relax_array_stride = mbr_type.array.back() == 1 && mbr_type.array_size_literal.back(); |
| 3505 |
|
| 3506 |
if (!relax_array_stride) |
| 3507 |
{ |
| 3508 |
uint32_t spirv_array_stride = type_struct_member_array_stride(type, index); |
| 3509 |
uint32_t msl_array_stride = get_declared_struct_member_array_stride_msl(type, index); |
| 3510 |
if (spirv_array_stride != msl_array_stride) |
| 3511 |
return false; |
| 3512 |
} |
| 3513 |
} |
| 3514 |
|
| 3515 |
if (is_matrix(mbr_type)) |
| 3516 |
{ |
| 3517 |
// Need to check MatrixStride as well. |
| 3518 |
uint32_t spirv_matrix_stride = type_struct_member_matrix_stride(type, index); |
| 3519 |
uint32_t msl_matrix_stride = get_declared_struct_member_matrix_stride_msl(type, index); |
| 3520 |
if (spirv_matrix_stride != msl_matrix_stride) |
| 3521 |
return false; |
| 3522 |
} |
| 3523 |
|
| 3524 |
// Now, we check alignment. |
| 3525 |
uint32_t msl_alignment = get_declared_struct_member_alignment_msl(type, index); |
| 3526 |
if ((spirv_offset % msl_alignment) != 0) |
| 3527 |
return false; |
| 3528 |
|
| 3529 |
// We're in the clear. |
| 3530 |
return true; |
| 3531 |
} |
| 3532 |
|
| 3533 |
// Here we need to verify that the member type we declare conforms to Offset, ArrayStride or MatrixStride restrictions. |
| 3534 |
// If there is a mismatch, we need to emit remapped types, either normal types, or "packed_X" types. |
| 3535 |
// In odd cases we need to emit packed and remapped types, for e.g. weird matrices or arrays with weird array strides. |
| 3536 |
void CompilerMSL::ensure_member_packing_rules_msl(SPIRType &ib_type, uint32_t index) |
| 3537 |
{ |
| 3538 |
if (validate_member_packing_rules_msl(ib_type, index)) |
| 3539 |
return; |
| 3540 |
|
| 3541 |
// We failed validation. |
| 3542 |
// This case will be nightmare-ish to deal with. This could possibly happen if struct alignment does not quite |
| 3543 |
// match up with what we want. Scalar block layout comes to mind here where we might have to work around the rule |
| 3544 |
// that struct alignment == max alignment of all members and struct size depends on this alignment. |
| 3545 |
auto &mbr_type = get<SPIRType>(ib_type.member_types[index]); |
| 3546 |
if (mbr_type.basetype == SPIRType::Struct) |
| 3547 |
SPIRV_CROSS_THROW("Cannot perform any repacking for structs when it is used as a member of another struct."); |
| 3548 |
|
| 3549 |
// Perform remapping here. |
| 3550 |
// There is nothing to be gained by using packed scalars, so don't attempt it. |
| 3551 |
if (!is_scalar(ib_type)) |
| 3552 |
set_extended_member_decoration(ib_type.self, index, SPIRVCrossDecorationPhysicalTypePacked); |
| 3553 |
|
| 3554 |
// Try validating again, now with packed. |
| 3555 |
if (validate_member_packing_rules_msl(ib_type, index)) |
| 3556 |
return; |
| 3557 |
|
| 3558 |
// We're in deep trouble, and we need to create a new PhysicalType which matches up with what we expect. |
| 3559 |
// A lot of work goes here ... |
| 3560 |
// We will need remapping on Load and Store to translate the types between Logical and Physical. |
| 3561 |
|
| 3562 |
// First, we check if we have small vector std140 array. |
| 3563 |
// We detect this if we have an array of vectors, and array stride is greater than number of elements. |
| 3564 |
if (!mbr_type.array.empty() && !is_matrix(mbr_type)) |
| 3565 |
{ |
| 3566 |
uint32_t array_stride = type_struct_member_array_stride(ib_type, index); |
| 3567 |
|
| 3568 |
// Hack off array-of-arrays until we find the array stride per element we must have to make it work. |
| 3569 |
uint32_t dimensions = uint32_t(mbr_type.array.size() - 1); |
| 3570 |
for (uint32_t dim = 0; dim < dimensions; dim++) |
| 3571 |
array_stride /= max(to_array_size_literal(mbr_type, dim), 1u); |
| 3572 |
|
| 3573 |
uint32_t elems_per_stride = array_stride / (mbr_type.width / 8); |
| 3574 |
|
| 3575 |
if (elems_per_stride == 3) |
| 3576 |
SPIRV_CROSS_THROW("Cannot use ArrayStride of 3 elements in remapping scenarios."); |
| 3577 |
else if (elems_per_stride > 4) |
| 3578 |
SPIRV_CROSS_THROW("Cannot represent vectors with more than 4 elements in MSL."); |
| 3579 |
|
| 3580 |
auto physical_type = mbr_type; |
| 3581 |
physical_type.vecsize = elems_per_stride; |
| 3582 |
physical_type.parent_type = 0; |
| 3583 |
uint32_t type_id = ir.increase_bound_by(1); |
| 3584 |
set<SPIRType>(type_id, physical_type); |
| 3585 |
set_extended_member_decoration(ib_type.self, index, SPIRVCrossDecorationPhysicalTypeID, type_id); |
| 3586 |
set_decoration(type_id, DecorationArrayStride, array_stride); |
| 3587 |
|
| 3588 |
// Remove packed_ for vectors of size 1, 2 and 4. |
| 3589 |
if (has_extended_decoration(ib_type.self, SPIRVCrossDecorationPhysicalTypePacked)) |
| 3590 |
SPIRV_CROSS_THROW("Unable to remove packed decoration as entire struct must be fully packed. Do not mix " |
| 3591 |
"scalar and std140 layout rules."); |
| 3592 |
else |
| 3593 |
unset_extended_member_decoration(ib_type.self, index, SPIRVCrossDecorationPhysicalTypePacked); |
| 3594 |
} |
| 3595 |
else if (is_matrix(mbr_type)) |
| 3596 |
{ |
| 3597 |
// MatrixStride might be std140-esque. |
| 3598 |
uint32_t matrix_stride = type_struct_member_matrix_stride(ib_type, index); |
| 3599 |
|
| 3600 |
uint32_t elems_per_stride = matrix_stride / (mbr_type.width / 8); |
| 3601 |
|
| 3602 |
if (elems_per_stride == 3) |
| 3603 |
SPIRV_CROSS_THROW("Cannot use ArrayStride of 3 elements in remapping scenarios."); |
| 3604 |
else if (elems_per_stride > 4) |
| 3605 |
SPIRV_CROSS_THROW("Cannot represent vectors with more than 4 elements in MSL."); |
| 3606 |
|
| 3607 |
bool row_major = has_member_decoration(ib_type.self, index, DecorationRowMajor); |
| 3608 |
|
| 3609 |
auto physical_type = mbr_type; |
| 3610 |
physical_type.parent_type = 0; |
| 3611 |
if (row_major) |
| 3612 |
physical_type.columns = elems_per_stride; |
| 3613 |
else |
| 3614 |
physical_type.vecsize = elems_per_stride; |
| 3615 |
uint32_t type_id = ir.increase_bound_by(1); |
| 3616 |
set<SPIRType>(type_id, physical_type); |
| 3617 |
set_extended_member_decoration(ib_type.self, index, SPIRVCrossDecorationPhysicalTypeID, type_id); |
| 3618 |
|
| 3619 |
// Remove packed_ for vectors of size 1, 2 and 4. |
| 3620 |
if (has_extended_decoration(ib_type.self, SPIRVCrossDecorationPhysicalTypePacked)) |
| 3621 |
SPIRV_CROSS_THROW("Unable to remove packed decoration as entire struct must be fully packed. Do not mix " |
| 3622 |
"scalar and std140 layout rules."); |
| 3623 |
else |
| 3624 |
unset_extended_member_decoration(ib_type.self, index, SPIRVCrossDecorationPhysicalTypePacked); |
| 3625 |
} |
| 3626 |
else |
| 3627 |
SPIRV_CROSS_THROW("Found a buffer packing case which we cannot represent in MSL."); |
| 3628 |
|
| 3629 |
// Try validating again, now with physical type remapping. |
| 3630 |
if (validate_member_packing_rules_msl(ib_type, index)) |
| 3631 |
return; |
| 3632 |
|
| 3633 |
// We might have a particular odd scalar layout case where the last element of an array |
| 3634 |
// does not take up as much space as the ArrayStride or MatrixStride. This can happen with DX cbuffers. |
| 3635 |
// The "proper" workaround for this is extremely painful and essentially impossible in the edge case of float3[], |
| 3636 |
// so we hack around it by declaring the offending array or matrix with one less array size/col/row, |
| 3637 |
// and rely on padding to get the correct value. We will technically access arrays out of bounds into the padding region, |
| 3638 |
// but it should spill over gracefully without too much trouble. We rely on behavior like this for unsized arrays anyways. |
| 3639 |
|
| 3640 |
// E.g. we might observe a physical layout of: |
| 3641 |
// { 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] ... |
| 3642 |
uint32_t type_id = get_extended_member_decoration(ib_type.self, index, SPIRVCrossDecorationPhysicalTypeID); |
| 3643 |
auto &type = get<SPIRType>(type_id); |
| 3644 |
|
| 3645 |
// Modify the physical type in-place. This is safe since each physical type workaround is a copy. |
| 3646 |
if (is_array(type)) |
| 3647 |
{ |
| 3648 |
if (type.array.back() > 1) |
| 3649 |
{ |
| 3650 |
if (!type.array_size_literal.back()) |
| 3651 |
SPIRV_CROSS_THROW("Cannot apply scalar layout workaround with spec constant array size."); |
| 3652 |
type.array.back() -= 1; |
| 3653 |
} |
| 3654 |
else |
| 3655 |
{ |
| 3656 |
// We have an array of size 1, so we cannot decrement that. Our only option now is to |
| 3657 |
// force a packed layout instead, and drop the physical type remap since ArrayStride is meaningless now. |
| 3658 |
unset_extended_member_decoration(ib_type.self, index, SPIRVCrossDecorationPhysicalTypeID); |
| 3659 |
set_extended_member_decoration(ib_type.self, index, SPIRVCrossDecorationPhysicalTypePacked); |
| 3660 |
} |
| 3661 |
} |
| 3662 |
else if (is_matrix(type)) |
| 3663 |
{ |
| 3664 |
bool row_major = has_member_decoration(ib_type.self, index, DecorationRowMajor); |
| 3665 |
if (!row_major) |
| 3666 |
{ |
| 3667 |
// Slice off one column. If we only have 2 columns, this might turn the matrix into a vector with one array element instead. |
| 3668 |
if (type.columns > 2) |
| 3669 |
{ |
| 3670 |
type.columns--; |
| 3671 |
} |
| 3672 |
else if (type.columns == 2) |
| 3673 |
{ |
| 3674 |
type.columns = 1; |
| 3675 |
assert(type.array.empty()); |
| 3676 |
type.array.push_back(1); |
| 3677 |
type.array_size_literal.push_back(true); |
| 3678 |
} |
| 3679 |
} |
| 3680 |
else |
| 3681 |
{ |
| 3682 |
// Slice off one row. If we only have 2 rows, this might turn the matrix into a vector with one array element instead. |
| 3683 |
if (type.vecsize > 2) |
| 3684 |
{ |
| 3685 |
type.vecsize--; |
| 3686 |
} |
| 3687 |
else if (type.vecsize == 2) |
| 3688 |
{ |
| 3689 |
type.vecsize = type.columns; |
| 3690 |
type.columns = 1; |
| 3691 |
assert(type.array.empty()); |
| 3692 |
type.array.push_back(1); |
| 3693 |
type.array_size_literal.push_back(true); |
| 3694 |
} |
| 3695 |
} |
| 3696 |
} |
| 3697 |
|
| 3698 |
// This better validate now, or we must fail gracefully. |
| 3699 |
if (!validate_member_packing_rules_msl(ib_type, index)) |
| 3700 |
SPIRV_CROSS_THROW("Found a buffer packing case which we cannot represent in MSL."); |
| 3701 |
} |
| 3702 |
|
| 3703 |
void CompilerMSL::emit_store_statement(uint32_t lhs_expression, uint32_t rhs_expression) |
| 3704 |
{ |
| 3705 |
auto &type = expression_type(rhs_expression); |
| 3706 |
|
| 3707 |
bool lhs_remapped_type = has_extended_decoration(lhs_expression, SPIRVCrossDecorationPhysicalTypeID); |
| 3708 |
bool lhs_packed_type = has_extended_decoration(lhs_expression, SPIRVCrossDecorationPhysicalTypePacked); |
| 3709 |
auto *lhs_e = maybe_get<SPIRExpression>(lhs_expression); |
| 3710 |
auto *rhs_e = maybe_get<SPIRExpression>(rhs_expression); |
| 3711 |
|
| 3712 |
bool transpose = lhs_e && lhs_e->need_transpose; |
| 3713 |
|
| 3714 |
// No physical type remapping, and no packed type, so can just emit a store directly. |
| 3715 |
if (!lhs_remapped_type && !lhs_packed_type) |
| 3716 |
{ |
| 3717 |
// We might not be dealing with remapped physical types or packed types, |
| 3718 |
// but we might be doing a clean store to a row-major matrix. |
| 3719 |
// In this case, we just flip transpose states, and emit the store, a transpose must be in the RHS expression, if any. |
| 3720 |
if (is_matrix(type) && lhs_e && lhs_e->need_transpose) |
| 3721 |
{ |
| 3722 |
if (!rhs_e) |
| 3723 |
SPIRV_CROSS_THROW("Need to transpose right-side expression of a store to row-major matrix, but it is " |
| 3724 |
"not a SPIRExpression."); |
| 3725 |
lhs_e->need_transpose = false; |
| 3726 |
|
| 3727 |
if (rhs_e && rhs_e->need_transpose) |
| 3728 |
{ |
| 3729 |
// Direct copy, but might need to unpack RHS. |
| 3730 |
// Skip the transpose, as we will transpose when writing to LHS and transpose(transpose(T)) == T. |
| 3731 |
rhs_e->need_transpose = false; |
| 3732 |
statement(to_expression(lhs_expression), " = ", to_unpacked_row_major_matrix_expression(rhs_expression), |
| 3733 |
";"); |
| 3734 |
rhs_e->need_transpose = true; |
| 3735 |
} |
| 3736 |
else |
| 3737 |
statement(to_expression(lhs_expression), " = transpose(", to_unpacked_expression(rhs_expression), ");"); |
| 3738 |
|
| 3739 |
lhs_e->need_transpose = true; |
| 3740 |
register_write(lhs_expression); |
| 3741 |
} |
| 3742 |
else if (lhs_e && lhs_e->need_transpose) |
| 3743 |
{ |
| 3744 |
lhs_e->need_transpose = false; |
| 3745 |
|
| 3746 |
// Storing a column to a row-major matrix. Unroll the write. |
| 3747 |
for (uint32_t c = 0; c < type.vecsize; c++) |
| 3748 |
{ |
| 3749 |
auto lhs_expr = to_dereferenced_expression(lhs_expression); |
| 3750 |
auto column_index = lhs_expr.find_last_of('['); |
| 3751 |
if (column_index != string::npos) |
| 3752 |
{ |
| 3753 |
statement(lhs_expr.insert(column_index, join('[', c, ']')), " = ", |
| 3754 |
to_extract_component_expression(rhs_expression, c), ";"); |
| 3755 |
} |
| 3756 |
} |
| 3757 |
lhs_e->need_transpose = true; |
| 3758 |
register_write(lhs_expression); |
| 3759 |
} |
| 3760 |
else |
| 3761 |
CompilerGLSL::emit_store_statement(lhs_expression, rhs_expression); |
| 3762 |
} |
| 3763 |
else if (!lhs_remapped_type && !is_matrix(type) && !transpose) |
| 3764 |
{ |
| 3765 |
// Even if the target type is packed, we can directly store to it. We cannot store to packed matrices directly, |
| 3766 |
// since they are declared as array of vectors instead, and we need the fallback path below. |
| 3767 |
CompilerGLSL::emit_store_statement(lhs_expression, rhs_expression); |
| 3768 |
} |
| 3769 |
else |
| 3770 |
{ |
| 3771 |
// Special handling when storing to a remapped physical type. |
| 3772 |
// This is mostly to deal with std140 padded matrices or vectors. |
| 3773 |
|
| 3774 |
TypeID physical_type_id = lhs_remapped_type ? |
| 3775 |
ID(get_extended_decoration(lhs_expression, SPIRVCrossDecorationPhysicalTypeID)) : |
| 3776 |
type.self; |
| 3777 |
|
| 3778 |
auto &physical_type = get<SPIRType>(physical_type_id); |
| 3779 |
|
| 3780 |
if (is_matrix(type)) |
| 3781 |
{ |
| 3782 |
const char *packed_pfx = lhs_packed_type ? "packed_" : ""; |
| 3783 |
|
| 3784 |
// Packed matrices are stored as arrays of packed vectors, so we need |
| 3785 |
// to assign the vectors one at a time. |
| 3786 |
// For row-major matrices, we need to transpose the *right-hand* side, |
| 3787 |
// not the left-hand side. |
| 3788 |
|
| 3789 |
// Lots of cases to cover here ... |
| 3790 |
|
| 3791 |
bool rhs_transpose = rhs_e && rhs_e->need_transpose; |
| 3792 |
SPIRType write_type = type; |
| 3793 |
string cast_expr; |
| 3794 |
|
| 3795 |
// We're dealing with transpose manually. |
| 3796 |
if (rhs_transpose) |
| 3797 |
rhs_e->need_transpose = false; |
| 3798 |
|
| 3799 |
if (transpose) |
| 3800 |
{ |
| 3801 |
// We're dealing with transpose manually. |
| 3802 |
lhs_e->need_transpose = false; |
| 3803 |
write_type.vecsize = type.columns; |
| 3804 |
write_type.columns = 1; |
| 3805 |
|
| 3806 |
if (physical_type.columns != type.columns) |
| 3807 |
cast_expr = join("(device ", packed_pfx, type_to_glsl(write_type), "&)"); |
| 3808 |
|
| 3809 |
if (rhs_transpose) |
| 3810 |
{ |
| 3811 |
// If RHS is also transposed, we can just copy row by row. |
| 3812 |
for (uint32_t i = 0; i < type.vecsize; i++) |
| 3813 |
{ |
| 3814 |
statement(cast_expr, to_enclosed_expression(lhs_expression), "[", i, "]", " = ", |
| 3815 |
to_unpacked_row_major_matrix_expression(rhs_expression), "[", i, "];"); |
| 3816 |
} |
| 3817 |
} |
| 3818 |
else |
| 3819 |
{ |
| 3820 |
auto vector_type = expression_type(rhs_expression); |
| 3821 |
vector_type.vecsize = vector_type.columns; |
| 3822 |
vector_type.columns = 1; |
| 3823 |
|
| 3824 |
// Transpose on the fly. Emitting a lot of full transpose() ops and extracting lanes seems very bad, |
| 3825 |
// so pick out individual components instead. |
| 3826 |
for (uint32_t i = 0; i < type.vecsize; i++) |
| 3827 |
{ |
| 3828 |
string rhs_row = type_to_glsl_constructor(vector_type) + "("; |
| 3829 |
for (uint32_t j = 0; j < vector_type.vecsize; j++) |
| 3830 |
{ |
| 3831 |
rhs_row += join(to_enclosed_unpacked_expression(rhs_expression), "[", j, "][", i, "]"); |
| 3832 |
if (j + 1 < vector_type.vecsize) |
| 3833 |
rhs_row += ", "; |
| 3834 |
} |
| 3835 |
rhs_row += ")"; |
| 3836 |
|
| 3837 |
statement(cast_expr, to_enclosed_expression(lhs_expression), "[", i, "]", " = ", rhs_row, ";"); |
| 3838 |
} |
| 3839 |
} |
| 3840 |
|
| 3841 |
// We're dealing with transpose manually. |
| 3842 |
lhs_e->need_transpose = true; |
| 3843 |
} |
| 3844 |
else |
| 3845 |
{ |
| 3846 |
write_type.columns = 1; |
| 3847 |
|
| 3848 |
if (physical_type.vecsize != type.vecsize) |
| 3849 |
cast_expr = join("(device ", packed_pfx, type_to_glsl(write_type), "&)"); |
| 3850 |
|
| 3851 |
if (rhs_transpose) |
| 3852 |
{ |
| 3853 |
auto vector_type = expression_type(rhs_expression); |
| 3854 |
vector_type.columns = 1; |
| 3855 |
|
| 3856 |
// Transpose on the fly. Emitting a lot of full transpose() ops and extracting lanes seems very bad, |
| 3857 |
// so pick out individual components instead. |
| 3858 |
for (uint32_t i = 0; i < type.columns; i++) |
| 3859 |
{ |
| 3860 |
string rhs_row = type_to_glsl_constructor(vector_type) + "("; |
| 3861 |
for (uint32_t j = 0; j < vector_type.vecsize; j++) |
| 3862 |
{ |
| 3863 |
// Need to explicitly unpack expression since we've mucked with transpose state. |
| 3864 |
auto unpacked_expr = to_unpacked_row_major_matrix_expression(rhs_expression); |
| 3865 |
rhs_row += join(unpacked_expr, "[", j, "][", i, "]"); |
| 3866 |
if (j + 1 < vector_type.vecsize) |
| 3867 |
rhs_row += ", "; |
| 3868 |
} |
| 3869 |
rhs_row += ")"; |
| 3870 |
|
| 3871 |
statement(cast_expr, to_enclosed_expression(lhs_expression), "[", i, "]", " = ", rhs_row, ";"); |
| 3872 |
} |
| 3873 |
} |
| 3874 |
else |
| 3875 |
{ |
| 3876 |
// Copy column-by-column. |
| 3877 |
for (uint32_t i = 0; i < type.columns; i++) |
| 3878 |
{ |
| 3879 |
statement(cast_expr, to_enclosed_expression(lhs_expression), "[", i, "]", " = ", |
| 3880 |
to_enclosed_unpacked_expression(rhs_expression), "[", i, "];"); |
| 3881 |
} |
| 3882 |
} |
| 3883 |
} |
| 3884 |
|
| 3885 |
// We're dealing with transpose manually. |
| 3886 |
if (rhs_transpose) |
| 3887 |
rhs_e->need_transpose = true; |
| 3888 |
} |
| 3889 |
else if (transpose) |
| 3890 |
{ |
| 3891 |
lhs_e->need_transpose = false; |
| 3892 |
|
| 3893 |
SPIRType write_type = type; |
| 3894 |
write_type.vecsize = 1; |
| 3895 |
write_type.columns = 1; |
| 3896 |
|
| 3897 |
// Storing a column to a row-major matrix. Unroll the write. |
| 3898 |
for (uint32_t c = 0; c < type.vecsize; c++) |
| 3899 |
{ |
| 3900 |
auto lhs_expr = to_enclosed_expression(lhs_expression); |
| 3901 |
auto column_index = lhs_expr.find_last_of('['); |
| 3902 |
if (column_index != string::npos) |
| 3903 |
{ |
| 3904 |
statement("((device ", type_to_glsl(write_type), "*)&", |
| 3905 |
lhs_expr.insert(column_index, join('[', c, ']', ")")), " = ", |
| 3906 |
to_extract_component_expression(rhs_expression, c), ";"); |
| 3907 |
} |
| 3908 |
} |
| 3909 |
|
| 3910 |
lhs_e->need_transpose = true; |
| 3911 |
} |
| 3912 |
else if ((is_matrix(physical_type) || is_array(physical_type)) && physical_type.vecsize > type.vecsize) |
| 3913 |
{ |
| 3914 |
assert(type.vecsize >= 1 && type.vecsize <= 3); |
| 3915 |
|
| 3916 |
// If we have packed types, we cannot use swizzled stores. |
| 3917 |
// We could technically unroll the store for each element if needed. |
| 3918 |
// When remapping to a std140 physical type, we always get float4, |
| 3919 |
// and the packed decoration should always be removed. |
| 3920 |
assert(!lhs_packed_type); |
| 3921 |
|
| 3922 |
string lhs = to_dereferenced_expression(lhs_expression); |
| 3923 |
string rhs = to_pointer_expression(rhs_expression); |
| 3924 |
|
| 3925 |
// Unpack the expression so we can store to it with a float or float2. |
| 3926 |
// It's still an l-value, so it's fine. Most other unpacking of expressions turn them into r-values instead. |
| 3927 |
lhs = join("(device ", type_to_glsl(type), "&)", enclose_expression(lhs)); |
| 3928 |
if (!optimize_read_modify_write(expression_type(rhs_expression), lhs, rhs)) |
| 3929 |
statement(lhs, " = ", rhs, ";"); |
| 3930 |
} |
| 3931 |
else if (!is_matrix(type)) |
| 3932 |
{ |
| 3933 |
string lhs = to_dereferenced_expression(lhs_expression); |
| 3934 |
string rhs = to_pointer_expression(rhs_expression); |
| 3935 |
if (!optimize_read_modify_write(expression_type(rhs_expression), lhs, rhs)) |
| 3936 |
statement(lhs, " = ", rhs, ";"); |
| 3937 |
} |
| 3938 |
|
| 3939 |
register_write(lhs_expression); |
| 3940 |
} |
| 3941 |
} |
| 3942 |
|
| 3943 |
static bool expression_ends_with(const string &expr_str, const std::string &ending) |
| 3944 |
{ |
| 3945 |
if (expr_str.length() >= ending.length()) |
| 3946 |
return (expr_str.compare(expr_str.length() - ending.length(), ending.length(), ending) == 0); |
| 3947 |
else |
| 3948 |
return false; |
| 3949 |
} |
| 3950 |
|
| 3951 |
// Converts the format of the current expression from packed to unpacked, |
| 3952 |
// by wrapping the expression in a constructor of the appropriate type. |
| 3953 |
// Also, handle special physical ID remapping scenarios, similar to emit_store_statement(). |
| 3954 |
string CompilerMSL::unpack_expression_type(string expr_str, const SPIRType &type, uint32_t physical_type_id, |
| 3955 |
bool packed, bool row_major) |
| 3956 |
{ |
| 3957 |
// Trivial case, nothing to do. |
| 3958 |
if (physical_type_id == 0 && !packed) |
| 3959 |
return expr_str; |
| 3960 |
|
| 3961 |
const SPIRType *physical_type = nullptr; |
| 3962 |
if (physical_type_id) |
| 3963 |
physical_type = &get<SPIRType>(physical_type_id); |
| 3964 |
|
| 3965 |
static const char *swizzle_lut[] = { |
| 3966 |
".x", |
| 3967 |
".xy", |
| 3968 |
".xyz", |
| 3969 |
}; |
| 3970 |
|
| 3971 |
if (physical_type && is_vector(*physical_type) && is_array(*physical_type) && |
| 3972 |
physical_type->vecsize > type.vecsize && !expression_ends_with(expr_str, swizzle_lut[type.vecsize - 1])) |
| 3973 |
{ |
| 3974 |
// std140 array cases for vectors. |
| 3975 |
assert(type.vecsize >= 1 && type.vecsize <= 3); |
| 3976 |
return enclose_expression(expr_str) + swizzle_lut[type.vecsize - 1]; |
| 3977 |
} |
| 3978 |
else if (physical_type && is_matrix(*physical_type) && is_vector(type) && physical_type->vecsize > type.vecsize) |
| 3979 |
{ |
| 3980 |
// Extract column from padded matrix. |
| 3981 |
assert(type.vecsize >= 1 && type.vecsize <= 3); |
| 3982 |
return enclose_expression(expr_str) + swizzle_lut[type.vecsize - 1]; |
| 3983 |
} |
| 3984 |
else if (is_matrix(type)) |
| 3985 |
{ |
| 3986 |
// Packed matrices are stored as arrays of packed vectors. Unfortunately, |
| 3987 |
// we can't just pass the array straight to the matrix constructor. We have to |
| 3988 |
// pass each vector individually, so that they can be unpacked to normal vectors. |
| 3989 |
if (!physical_type) |
| 3990 |
physical_type = &type; |
| 3991 |
|
| 3992 |
uint32_t vecsize = type.vecsize; |
| 3993 |
uint32_t columns = type.columns; |
| 3994 |
if (row_major) |
| 3995 |
swap(vecsize, columns); |
| 3996 |
|
| 3997 |
uint32_t physical_vecsize = row_major ? physical_type->columns : physical_type->vecsize; |
| 3998 |
|
| 3999 |
const char *base_type = type.width == 16 ? "half" : "float"; |
| 4000 |
string unpack_expr = join(base_type, columns, "x", vecsize, "("); |
| 4001 |
|
| 4002 |
const char *load_swiz = ""; |
| 4003 |
|
| 4004 |
if (physical_vecsize != vecsize) |
| 4005 |
load_swiz = swizzle_lut[vecsize - 1]; |
| 4006 |
|
| 4007 |
for (uint32_t i = 0; i < columns; i++) |
| 4008 |
{ |
| 4009 |
if (i > 0) |
| 4010 |
unpack_expr += ", "; |
| 4011 |
|
| 4012 |
if (packed) |
| 4013 |
unpack_expr += join(base_type, physical_vecsize, "(", expr_str, "[", i, "]", ")", load_swiz); |
| 4014 |
else |
| 4015 |
unpack_expr += join(expr_str, "[", i, "]", load_swiz); |
| 4016 |
} |
| 4017 |
|
| 4018 |
unpack_expr += ")"; |
| 4019 |
return unpack_expr; |
| 4020 |
} |
| 4021 |
else |
| 4022 |
{ |
| 4023 |
return join(type_to_glsl(type), "(", expr_str, ")"); |
| 4024 |
} |
| 4025 |
} |
| 4026 |
|
| 4027 |
// Emits the file header info |
| 4028 |
void CompilerMSL::emit_header() |
| 4029 |
{ |
| 4030 |
// This particular line can be overridden during compilation, so make it a flag and not a pragma line. |
| 4031 |
if (suppress_missing_prototypes) |
| 4032 |
statement("#pragma clang diagnostic ignored \"-Wmissing-prototypes\""); |
| 4033 |
|
| 4034 |
// Disable warning about missing braces for array<T> template to make arrays a value type |
| 4035 |
if (spv_function_implementations.count(SPVFuncImplUnsafeArray) != 0) |
| 4036 |
statement("#pragma clang diagnostic ignored \"-Wmissing-braces\""); |
| 4037 |
|
| 4038 |
for (auto &pragma : pragma_lines) |
| 4039 |
statement(pragma); |
| 4040 |
|
| 4041 |
if (!pragma_lines.empty() || suppress_missing_prototypes) |
| 4042 |
statement(""); |
| 4043 |
|
| 4044 |
statement("#include <metal_stdlib>"); |
| 4045 |
statement("#include <simd/simd.h>"); |
| 4046 |
|
| 4047 |
for (auto &header : header_lines) |
| 4048 |
statement(header); |
| 4049 |
|
| 4050 |
statement(""); |
| 4051 |
statement("using namespace metal;"); |
| 4052 |
statement(""); |
| 4053 |
|
| 4054 |
for (auto &td : typedef_lines) |
| 4055 |
statement(td); |
| 4056 |
|
| 4057 |
if (!typedef_lines.empty()) |
| 4058 |
statement(""); |
| 4059 |
} |
| 4060 |
|
| 4061 |
void CompilerMSL::add_pragma_line(const string &line) |
| 4062 |
{ |
| 4063 |
auto rslt = pragma_lines.insert(line); |
| 4064 |
if (rslt.second) |
| 4065 |
force_recompile(); |
| 4066 |
} |
| 4067 |
|
| 4068 |
void CompilerMSL::add_typedef_line(const string &line) |
| 4069 |
{ |
| 4070 |
auto rslt = typedef_lines.insert(line); |
| 4071 |
if (rslt.second) |
| 4072 |
force_recompile(); |
| 4073 |
} |
| 4074 |
|
| 4075 |
// Template struct like spvUnsafeArray<> need to be declared *before* any resources are declared |
| 4076 |
void CompilerMSL::emit_custom_templates() |
| 4077 |
{ |
| 4078 |
for (const auto &spv_func : spv_function_implementations) |
| 4079 |
{ |
| 4080 |
switch (spv_func) |
| 4081 |
{ |
| 4082 |
case SPVFuncImplUnsafeArray: |
| 4083 |
statement("template<typename T, size_t Num>"); |
| 4084 |
statement("struct spvUnsafeArray"); |
| 4085 |
begin_scope(); |
| 4086 |
statement("T elements[Num ? Num : 1];"); |
| 4087 |
statement(""); |
| 4088 |
statement("thread T& operator [] (size_t pos) thread"); |
| 4089 |
begin_scope(); |
| 4090 |
statement("return elements[pos];"); |
| 4091 |
end_scope(); |
| 4092 |
statement("constexpr const thread T& operator [] (size_t pos) const thread"); |
| 4093 |
begin_scope(); |
| 4094 |
statement("return elements[pos];"); |
| 4095 |
end_scope(); |
| 4096 |
statement(""); |
| 4097 |
statement("device T& operator [] (size_t pos) device"); |
| 4098 |
begin_scope(); |
| 4099 |
statement("return elements[pos];"); |
| 4100 |
end_scope(); |
| 4101 |
statement("constexpr const device T& operator [] (size_t pos) const device"); |
| 4102 |
begin_scope(); |
| 4103 |
statement("return elements[pos];"); |
| 4104 |
end_scope(); |
| 4105 |
statement(""); |
| 4106 |
statement("constexpr const constant T& operator [] (size_t pos) const constant"); |
| 4107 |
begin_scope(); |
| 4108 |
statement("return elements[pos];"); |
| 4109 |
end_scope(); |
| 4110 |
statement(""); |
| 4111 |
statement("threadgroup T& operator [] (size_t pos) threadgroup"); |
| 4112 |
begin_scope(); |
| 4113 |
statement("return elements[pos];"); |
| 4114 |
end_scope(); |
| 4115 |
statement("constexpr const threadgroup T& operator [] (size_t pos) const threadgroup"); |
| 4116 |
begin_scope(); |
| 4117 |
statement("return elements[pos];"); |
| 4118 |
end_scope(); |
| 4119 |
end_scope_decl(); |
| 4120 |
statement(""); |
| 4121 |
break; |
| 4122 |
|
| 4123 |
default: |
| 4124 |
break; |
| 4125 |
} |
| 4126 |
} |
| 4127 |
} |
| 4128 |
|
| 4129 |
// Emits any needed custom function bodies. |
| 4130 |
// Metal helper functions must be static force-inline, i.e. static inline __attribute__((always_inline)) |
| 4131 |
// otherwise they will cause problems when linked together in a single Metallib. |
| 4132 |
void CompilerMSL::emit_custom_functions() |
| 4133 |
{ |
| 4134 |
for (uint32_t i = kArrayCopyMultidimMax; i >= 2; i--) |
| 4135 |
if (spv_function_implementations.count(static_cast<SPVFuncImpl>(SPVFuncImplArrayCopyMultidimBase + i))) |
| 4136 |
spv_function_implementations.insert(static_cast<SPVFuncImpl>(SPVFuncImplArrayCopyMultidimBase + i - 1)); |
| 4137 |
|
| 4138 |
if (spv_function_implementations.count(SPVFuncImplDynamicImageSampler)) |
| 4139 |
{ |
| 4140 |
// Unfortunately, this one needs a lot of the other functions to compile OK. |
| 4141 |
if (!msl_options.supports_msl_version(2)) |
| 4142 |
SPIRV_CROSS_THROW( |
| 4143 |
"spvDynamicImageSampler requires default-constructible texture objects, which require MSL 2.0."); |
| 4144 |
spv_function_implementations.insert(SPVFuncImplForwardArgs); |
| 4145 |
spv_function_implementations.insert(SPVFuncImplTextureSwizzle); |
| 4146 |
if (msl_options.swizzle_texture_samples) |
| 4147 |
spv_function_implementations.insert(SPVFuncImplGatherSwizzle); |
| 4148 |
for (uint32_t i = SPVFuncImplChromaReconstructNearest2Plane; |
| 4149 |
i <= SPVFuncImplChromaReconstructLinear420XMidpointYMidpoint3Plane; i++) |
| 4150 |
spv_function_implementations.insert(static_cast<SPVFuncImpl>(i)); |
| 4151 |
spv_function_implementations.insert(SPVFuncImplExpandITUFullRange); |
| 4152 |
spv_function_implementations.insert(SPVFuncImplExpandITUNarrowRange); |
| 4153 |
spv_function_implementations.insert(SPVFuncImplConvertYCbCrBT709); |
| 4154 |
spv_function_implementations.insert(SPVFuncImplConvertYCbCrBT601); |
| 4155 |
spv_function_implementations.insert(SPVFuncImplConvertYCbCrBT2020); |
| 4156 |
} |
| 4157 |
|
| 4158 |
for (uint32_t i = SPVFuncImplChromaReconstructNearest2Plane; |
| 4159 |
i <= SPVFuncImplChromaReconstructLinear420XMidpointYMidpoint3Plane; i++) |
| 4160 |
if (spv_function_implementations.count(static_cast<SPVFuncImpl>(i))) |
| 4161 |
spv_function_implementations.insert(SPVFuncImplForwardArgs); |
| 4162 |
|
| 4163 |
if (spv_function_implementations.count(SPVFuncImplTextureSwizzle) || |
| 4164 |
spv_function_implementations.count(SPVFuncImplGatherSwizzle) || |
| 4165 |
spv_function_implementations.count(SPVFuncImplGatherCompareSwizzle)) |
| 4166 |
{ |
| 4167 |
spv_function_implementations.insert(SPVFuncImplForwardArgs); |
| 4168 |
spv_function_implementations.insert(SPVFuncImplGetSwizzle); |
| 4169 |
} |
| 4170 |
|
| 4171 |
for (const auto &spv_func : spv_function_implementations) |
| 4172 |
{ |
| 4173 |
switch (spv_func) |
| 4174 |
{ |
| 4175 |
case SPVFuncImplMod: |
| 4176 |
statement("// Implementation of the GLSL mod() function, which is slightly different than Metal fmod()"); |
| 4177 |
statement("template<typename Tx, typename Ty>"); |
| 4178 |
statement("inline Tx mod(Tx x, Ty y)"); |
| 4179 |
begin_scope(); |
| 4180 |
statement("return x - y * floor(x / y);"); |
| 4181 |
end_scope(); |
| 4182 |
statement(""); |
| 4183 |
break; |
| 4184 |
|
| 4185 |
case SPVFuncImplRadians: |
| 4186 |
statement("// Implementation of the GLSL radians() function"); |
| 4187 |
statement("template<typename T>"); |
| 4188 |
statement("inline T radians(T d)"); |
| 4189 |
begin_scope(); |
| 4190 |
statement("return d * T(0.01745329251);"); |
| 4191 |
end_scope(); |
| 4192 |
statement(""); |
| 4193 |
break; |
| 4194 |
|
| 4195 |
case SPVFuncImplDegrees: |
| 4196 |
statement("// Implementation of the GLSL degrees() function"); |
| 4197 |
statement("template<typename T>"); |
| 4198 |
statement("inline T degrees(T r)"); |
| 4199 |
begin_scope(); |
| 4200 |
statement("return r * T(57.2957795131);"); |
| 4201 |
end_scope(); |
| 4202 |
statement(""); |
| 4203 |
break; |
| 4204 |
|
| 4205 |
case SPVFuncImplFindILsb: |
| 4206 |
statement("// Implementation of the GLSL findLSB() function"); |
| 4207 |
statement("template<typename T>"); |
| 4208 |
statement("inline T spvFindLSB(T x)"); |
| 4209 |
begin_scope(); |
| 4210 |
statement("return select(ctz(x), T(-1), x == T(0));"); |
| 4211 |
end_scope(); |
| 4212 |
statement(""); |
| 4213 |
break; |
| 4214 |
|
| 4215 |
case SPVFuncImplFindUMsb: |
| 4216 |
statement("// Implementation of the unsigned GLSL findMSB() function"); |
| 4217 |
statement("template<typename T>"); |
| 4218 |
statement("inline T spvFindUMSB(T x)"); |
| 4219 |
begin_scope(); |
| 4220 |
statement("return select(clz(T(0)) - (clz(x) + T(1)), T(-1), x == T(0));"); |
| 4221 |
end_scope(); |
| 4222 |
statement(""); |
| 4223 |
break; |
| 4224 |
|
| 4225 |
case SPVFuncImplFindSMsb: |
| 4226 |
statement("// Implementation of the signed GLSL findMSB() function"); |
| 4227 |
statement("template<typename T>"); |
| 4228 |
statement("inline T spvFindSMSB(T x)"); |
| 4229 |
begin_scope(); |
| 4230 |
statement("T v = select(x, T(-1) - x, x < T(0));"); |
| 4231 |
statement("return select(clz(T(0)) - (clz(v) + T(1)), T(-1), v == T(0));"); |
| 4232 |
end_scope(); |
| 4233 |
statement(""); |
| 4234 |
break; |
| 4235 |
|
| 4236 |
case SPVFuncImplSSign: |
| 4237 |
statement("// Implementation of the GLSL sign() function for integer types"); |
| 4238 |
statement("template<typename T, typename E = typename enable_if<is_integral<T>::value>::type>"); |
| 4239 |
statement("inline T sign(T x)"); |
| 4240 |
begin_scope(); |
| 4241 |
statement("return select(select(select(x, T(0), x == T(0)), T(1), x > T(0)), T(-1), x < T(0));"); |
| 4242 |
end_scope(); |
| 4243 |
statement(""); |
| 4244 |
break; |
| 4245 |
|
| 4246 |
case SPVFuncImplArrayCopy: |
| 4247 |
case SPVFuncImplArrayOfArrayCopy2Dim: |
| 4248 |
case SPVFuncImplArrayOfArrayCopy3Dim: |
| 4249 |
case SPVFuncImplArrayOfArrayCopy4Dim: |
| 4250 |
case SPVFuncImplArrayOfArrayCopy5Dim: |
| 4251 |
case SPVFuncImplArrayOfArrayCopy6Dim: |
| 4252 |
{ |
| 4253 |
// Unfortunately we cannot template on the address space, so combinatorial explosion it is. |
| 4254 |
static const char *function_name_tags[] = { |
| 4255 |
"FromConstantToStack", "FromConstantToThreadGroup", "FromStackToStack", |
| 4256 |
"FromStackToThreadGroup", "FromThreadGroupToStack", "FromThreadGroupToThreadGroup", |
| 4257 |
"FromDeviceToDevice", "FromConstantToDevice", "FromStackToDevice", |
| 4258 |
"FromThreadGroupToDevice", "FromDeviceToStack", "FromDeviceToThreadGroup", |
| 4259 |
}; |
| 4260 |
|
| 4261 |
static const char *src_address_space[] = { |
| 4262 |
"constant", "constant", "thread const", "thread const", |
| 4263 |
"threadgroup const", "threadgroup const", "device const", "constant", |
| 4264 |
"thread const", "threadgroup const", "device const", "device const", |
| 4265 |
}; |
| 4266 |
|
| 4267 |
static const char *dst_address_space[] = { |
| 4268 |
"thread", "threadgroup", "thread", "threadgroup", "thread", "threadgroup", |
| 4269 |
"device", "device", "device", "device", "thread", "threadgroup", |
| 4270 |
}; |
| 4271 |
|
| 4272 |
for (uint32_t variant = 0; variant < 12; variant++) |
| 4273 |
{ |
| 4274 |
uint32_t dimensions = spv_func - SPVFuncImplArrayCopyMultidimBase; |
| 4275 |
string tmp = "template<typename T"; |
| 4276 |
for (uint8_t i = 0; i < dimensions; i++) |
| 4277 |
{ |
| 4278 |
tmp += ", uint "; |
| 4279 |
tmp += 'A' + i; |
| 4280 |
} |
| 4281 |
tmp += ">"; |
| 4282 |
statement(tmp); |
| 4283 |
|
| 4284 |
string array_arg; |
| 4285 |
for (uint8_t i = 0; i < dimensions; i++) |
| 4286 |
{ |
| 4287 |
array_arg += "["; |
| 4288 |
array_arg += 'A' + i; |
| 4289 |
array_arg += "]"; |
| 4290 |
} |
| 4291 |
|
| 4292 |
statement("inline void spvArrayCopy", function_name_tags[variant], dimensions, "(", |
| 4293 |
dst_address_space[variant], " T (&dst)", array_arg, ", ", src_address_space[variant], |
| 4294 |
" T (&src)", array_arg, ")"); |
| 4295 |
|
| 4296 |
begin_scope(); |
| 4297 |
statement("for (uint i = 0; i < A; i++)"); |
| 4298 |
begin_scope(); |
| 4299 |
|
| 4300 |
if (dimensions == 1) |
| 4301 |
statement("dst[i] = src[i];"); |
| 4302 |
else |
| 4303 |
statement("spvArrayCopy", function_name_tags[variant], dimensions - 1, "(dst[i], src[i]);"); |
| 4304 |
end_scope(); |
| 4305 |
end_scope(); |
| 4306 |
statement(""); |
| 4307 |
} |
| 4308 |
break; |
| 4309 |
} |
| 4310 |
|
| 4311 |
// Support for Metal 2.1's new texture_buffer type. |
| 4312 |
case SPVFuncImplTexelBufferCoords: |
| 4313 |
{ |
| 4314 |
if (msl_options.texel_buffer_texture_width > 0) |
| 4315 |
{ |
| 4316 |
string tex_width_str = convert_to_string(msl_options.texel_buffer_texture_width); |
| 4317 |
statement("// Returns 2D texture coords corresponding to 1D texel buffer coords"); |
| 4318 |
statement(force_inline); |
| 4319 |
statement("uint2 spvTexelBufferCoord(uint tc)"); |
| 4320 |
begin_scope(); |
| 4321 |
statement(join("return uint2(tc % ", tex_width_str, ", tc / ", tex_width_str, ");")); |
| 4322 |
end_scope(); |
| 4323 |
statement(""); |
| 4324 |
} |
| 4325 |
else |
| 4326 |
{ |
| 4327 |
statement("// Returns 2D texture coords corresponding to 1D texel buffer coords"); |
| 4328 |
statement( |
| 4329 |
"#define spvTexelBufferCoord(tc, tex) uint2((tc) % (tex).get_width(), (tc) / (tex).get_width())"); |
| 4330 |
statement(""); |
| 4331 |
} |
| 4332 |
break; |
| 4333 |
} |
| 4334 |
|
| 4335 |
// Emulate texture2D atomic operations |
| 4336 |
case SPVFuncImplImage2DAtomicCoords: |
| 4337 |
{ |
| 4338 |
if (msl_options.supports_msl_version(1, 2)) |
| 4339 |
{ |
| 4340 |
statement("// The required alignment of a linear texture of R32Uint format."); |
| 4341 |
statement("constant uint spvLinearTextureAlignmentOverride [[function_constant(", |
| 4342 |
msl_options.r32ui_alignment_constant_id, ")]];"); |
| 4343 |
statement("constant uint spvLinearTextureAlignment = ", |
| 4344 |
"is_function_constant_defined(spvLinearTextureAlignmentOverride) ? ", |
| 4345 |
"spvLinearTextureAlignmentOverride : ", msl_options.r32ui_linear_texture_alignment, ";"); |
| 4346 |
} |
| 4347 |
else |
| 4348 |
{ |
| 4349 |
statement("// The required alignment of a linear texture of R32Uint format."); |
| 4350 |
statement("constant uint spvLinearTextureAlignment = ", msl_options.r32ui_linear_texture_alignment, |
| 4351 |
";"); |
| 4352 |
} |
| 4353 |
statement("// Returns buffer coords corresponding to 2D texture coords for emulating 2D texture atomics"); |
| 4354 |
statement("#define spvImage2DAtomicCoord(tc, tex) (((((tex).get_width() + ", |
| 4355 |
" spvLinearTextureAlignment / 4 - 1) & ~(", |
| 4356 |
" spvLinearTextureAlignment / 4 - 1)) * (tc).y) + (tc).x)"); |
| 4357 |
statement(""); |
| 4358 |
break; |
| 4359 |
} |
| 4360 |
|
| 4361 |
// "fadd" intrinsic support |
| 4362 |
case SPVFuncImplFAdd: |
| 4363 |
statement("template<typename T>"); |
| 4364 |
statement("T spvFAdd(T l, T r)"); |
| 4365 |
begin_scope(); |
| 4366 |
statement("return fma(T(1), l, r);"); |
| 4367 |
end_scope(); |
| 4368 |
statement(""); |
| 4369 |
break; |
| 4370 |
|
| 4371 |
// "fmul' intrinsic support |
| 4372 |
case SPVFuncImplFMul: |
| 4373 |
statement("template<typename T>"); |
| 4374 |
statement("T spvFMul(T l, T r)"); |
| 4375 |
begin_scope(); |
| 4376 |
statement("return fma(l, r, T(0));"); |
| 4377 |
end_scope(); |
| 4378 |
statement(""); |
| 4379 |
|
| 4380 |
statement("template<typename T, int Cols, int Rows>"); |
| 4381 |
statement("vec<T, Cols> spvFMulVectorMatrix(vec<T, Rows> v, matrix<T, Cols, Rows> m)"); |
| 4382 |
begin_scope(); |
| 4383 |
statement("vec<T, Cols> res = vec<T, Cols>(0);"); |
| 4384 |
statement("for (uint i = Rows; i > 0; --i)"); |
| 4385 |
begin_scope(); |
| 4386 |
statement("vec<T, Cols> tmp(0);"); |
| 4387 |
statement("for (uint j = 0; j < Cols; ++j)"); |
| 4388 |
begin_scope(); |
| 4389 |
statement("tmp[j] = m[j][i - 1];"); |
| 4390 |
end_scope(); |
| 4391 |
statement("res = fma(tmp, vec<T, Cols>(v[i - 1]), res);"); |
| 4392 |
end_scope(); |
| 4393 |
statement("return res;"); |
| 4394 |
end_scope(); |
| 4395 |
statement(""); |
| 4396 |
|
| 4397 |
statement("template<typename T, int Cols, int Rows>"); |
| 4398 |
statement("vec<T, Rows> spvFMulMatrixVector(matrix<T, Cols, Rows> m, vec<T, Cols> v)"); |
| 4399 |
begin_scope(); |
| 4400 |
statement("vec<T, Rows> res = vec<T, Rows>(0);"); |
| 4401 |
statement("for (uint i = Cols; i > 0; --i)"); |
| 4402 |
begin_scope(); |
| 4403 |
statement("res = fma(m[i - 1], vec<T, Rows>(v[i - 1]), res);"); |
| 4404 |
end_scope(); |
| 4405 |
statement("return res;"); |
| 4406 |
end_scope(); |
| 4407 |
statement(""); |
| 4408 |
|
| 4409 |
statement("template<typename T, int LCols, int LRows, int RCols, int RRows>"); |
| 4410 |
statement( |
| 4411 |
"matrix<T, RCols, LRows> spvFMulMatrixMatrix(matrix<T, LCols, LRows> l, matrix<T, RCols, RRows> r)"); |
| 4412 |
begin_scope(); |
| 4413 |
statement("matrix<T, RCols, LRows> res;"); |
| 4414 |
statement("for (uint i = 0; i < RCols; i++)"); |
| 4415 |
begin_scope(); |
| 4416 |
statement("vec<T, RCols> tmp(0);"); |
| 4417 |
statement("for (uint j = 0; j < LCols; j++)"); |
| 4418 |
begin_scope(); |
| 4419 |
statement("tmp = fma(vec<T, RCols>(r[i][j]), l[j], tmp);"); |
| 4420 |
end_scope(); |
| 4421 |
statement("res[i] = tmp;"); |
| 4422 |
end_scope(); |
| 4423 |
statement("return res;"); |
| 4424 |
end_scope(); |
| 4425 |
statement(""); |
| 4426 |
break; |
| 4427 |
|
| 4428 |
// Emulate texturecube_array with texture2d_array for iOS where this type is not available |
| 4429 |
case SPVFuncImplCubemapTo2DArrayFace: |
| 4430 |
statement(force_inline); |
| 4431 |
statement("float3 spvCubemapTo2DArrayFace(float3 P)"); |
| 4432 |
begin_scope(); |
| 4433 |
statement("float3 Coords = abs(P.xyz);"); |
| 4434 |
statement("float CubeFace = 0;"); |
| 4435 |
statement("float ProjectionAxis = 0;"); |
| 4436 |
statement("float u = 0;"); |
| 4437 |
statement("float v = 0;"); |
| 4438 |
statement("if (Coords.x >= Coords.y && Coords.x >= Coords.z)"); |
| 4439 |
begin_scope(); |
| 4440 |
statement("CubeFace = P.x >= 0 ? 0 : 1;"); |
| 4441 |
statement("ProjectionAxis = Coords.x;"); |
| 4442 |
statement("u = P.x >= 0 ? -P.z : P.z;"); |
| 4443 |
statement("v = -P.y;"); |
| 4444 |
end_scope(); |
| 4445 |
statement("else if (Coords.y >= Coords.x && Coords.y >= Coords.z)"); |
| 4446 |
begin_scope(); |
| 4447 |
statement("CubeFace = P.y >= 0 ? 2 : 3;"); |
| 4448 |
statement("ProjectionAxis = Coords.y;"); |
| 4449 |
statement("u = P.x;"); |
| 4450 |
statement("v = P.y >= 0 ? P.z : -P.z;"); |
| 4451 |
end_scope(); |
| 4452 |
statement("else"); |
| 4453 |
begin_scope(); |
| 4454 |
statement("CubeFace = P.z >= 0 ? 4 : 5;"); |
| 4455 |
statement("ProjectionAxis = Coords.z;"); |
| 4456 |
statement("u = P.z >= 0 ? P.x : -P.x;"); |
| 4457 |
statement("v = -P.y;"); |
| 4458 |
end_scope(); |
| 4459 |
statement("u = 0.5 * (u/ProjectionAxis + 1);"); |
| 4460 |
statement("v = 0.5 * (v/ProjectionAxis + 1);"); |
| 4461 |
statement("return float3(u, v, CubeFace);"); |
| 4462 |
end_scope(); |
| 4463 |
statement(""); |
| 4464 |
break; |
| 4465 |
|
| 4466 |
case SPVFuncImplInverse4x4: |
| 4467 |
statement("// Returns the determinant of a 2x2 matrix."); |
| 4468 |
statement(force_inline); |
| 4469 |
statement("float spvDet2x2(float a1, float a2, float b1, float b2)"); |
| 4470 |
begin_scope(); |
| 4471 |
statement("return a1 * b2 - b1 * a2;"); |
| 4472 |
end_scope(); |
| 4473 |
statement(""); |
| 4474 |
|
| 4475 |
statement("// Returns the determinant of a 3x3 matrix."); |
| 4476 |
statement(force_inline); |
| 4477 |
statement("float spvDet3x3(float a1, float a2, float a3, float b1, float b2, float b3, float c1, " |
| 4478 |
"float c2, float c3)"); |
| 4479 |
begin_scope(); |
| 4480 |
statement("return a1 * spvDet2x2(b2, b3, c2, c3) - b1 * spvDet2x2(a2, a3, c2, c3) + c1 * spvDet2x2(a2, a3, " |
| 4481 |
"b2, b3);"); |
| 4482 |
end_scope(); |
| 4483 |
statement(""); |
| 4484 |
statement("// Returns the inverse of a matrix, by using the algorithm of calculating the classical"); |
| 4485 |
statement("// adjoint and dividing by the determinant. The contents of the matrix are changed."); |
| 4486 |
statement(force_inline); |
| 4487 |
statement("float4x4 spvInverse4x4(float4x4 m)"); |
| 4488 |
begin_scope(); |
| 4489 |
statement("float4x4 adj; // The adjoint matrix (inverse after dividing by determinant)"); |
| 4490 |
statement_no_indent(""); |
| 4491 |
statement("// Create the transpose of the cofactors, as the classical adjoint of the matrix."); |
| 4492 |
statement("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], " |
| 4493 |
"m[3][3]);"); |
| 4494 |
statement("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], " |
| 4495 |
"m[3][3]);"); |
| 4496 |
statement("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], " |
| 4497 |
"m[3][3]);"); |
| 4498 |
statement("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], " |
| 4499 |
"m[2][3]);"); |
| 4500 |
statement_no_indent(""); |
| 4501 |
statement("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], " |
| 4502 |
"m[3][3]);"); |
| 4503 |
statement("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], " |
| 4504 |
"m[3][3]);"); |
| 4505 |
statement("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], " |
| 4506 |
"m[3][3]);"); |
| 4507 |
statement("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], " |
| 4508 |
"m[2][3]);"); |
| 4509 |
statement_no_indent(""); |
| 4510 |
statement("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], " |
| 4511 |
"m[3][3]);"); |
| 4512 |
statement("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], " |
| 4513 |
"m[3][3]);"); |
| 4514 |
statement("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], " |
| 4515 |
"m[3][3]);"); |
| 4516 |
statement("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], " |
| 4517 |
"m[2][3]);"); |
| 4518 |
statement_no_indent(""); |
| 4519 |
statement("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], " |
| 4520 |
"m[3][2]);"); |
| 4521 |
statement("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], " |
| 4522 |
"m[3][2]);"); |
| 4523 |
statement("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], " |
| 4524 |
"m[3][2]);"); |
| 4525 |
statement("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], " |
| 4526 |
"m[2][2]);"); |
| 4527 |
statement_no_indent(""); |
| 4528 |
statement("// Calculate the determinant as a combination of the cofactors of the first row."); |
| 4529 |
statement("float det = (adj[0][0] * m[0][0]) + (adj[0][1] * m[1][0]) + (adj[0][2] * m[2][0]) + (adj[0][3] " |
| 4530 |
"* m[3][0]);"); |
| 4531 |
statement_no_indent(""); |
| 4532 |
statement("// Divide the classical adjoint matrix by the determinant."); |
| 4533 |
statement("// If determinant is zero, matrix is not invertable, so leave it unchanged."); |
| 4534 |
statement("return (det != 0.0f) ? (adj * (1.0f / det)) : m;"); |
| 4535 |
end_scope(); |
| 4536 |
statement(""); |
| 4537 |
break; |
| 4538 |
|
| 4539 |
case SPVFuncImplInverse3x3: |
| 4540 |
if (spv_function_implementations.count(SPVFuncImplInverse4x4) == 0) |
| 4541 |
{ |
| 4542 |
statement("// Returns the determinant of a 2x2 matrix."); |
| 4543 |
statement(force_inline); |
| 4544 |
statement("float spvDet2x2(float a1, float a2, float b1, float b2)"); |
| 4545 |
begin_scope(); |
| 4546 |
statement("return a1 * b2 - b1 * a2;"); |
| 4547 |
end_scope(); |
| 4548 |
statement(""); |
| 4549 |
} |
| 4550 |
|
| 4551 |
statement("// Returns the inverse of a matrix, by using the algorithm of calculating the classical"); |
| 4552 |
statement("// adjoint and dividing by the determinant. The contents of the matrix are changed."); |
| 4553 |
statement(force_inline); |
| 4554 |
statement("float3x3 spvInverse3x3(float3x3 m)"); |
| 4555 |
begin_scope(); |
| 4556 |
statement("float3x3 adj; // The adjoint matrix (inverse after dividing by determinant)"); |
| 4557 |
statement_no_indent(""); |
| 4558 |
statement("// Create the transpose of the cofactors, as the classical adjoint of the matrix."); |
| 4559 |
statement("adj[0][0] = spvDet2x2(m[1][1], m[1][2], m[2][1], m[2][2]);"); |
| 4560 |
statement("adj[0][1] = -spvDet2x2(m[0][1], m[0][2], m[2][1], m[2][2]);"); |
| 4561 |
statement("adj[0][2] = spvDet2x2(m[0][1], m[0][2], m[1][1], m[1][2]);"); |
| 4562 |
statement_no_indent(""); |
| 4563 |
statement("adj[1][0] = -spvDet2x2(m[1][0], m[1][2], m[2][0], m[2][2]);"); |
| 4564 |
statement("adj[1][1] = spvDet2x2(m[0][0], m[0][2], m[2][0], m[2][2]);"); |
| 4565 |
statement("adj[1][2] = -spvDet2x2(m[0][0], m[0][2], m[1][0], m[1][2]);"); |
| 4566 |
statement_no_indent(""); |
| 4567 |
statement("adj[2][0] = spvDet2x2(m[1][0], m[1][1], m[2][0], m[2][1]);"); |
| 4568 |
statement("adj[2][1] = -spvDet2x2(m[0][0], m[0][1], m[2][0], m[2][1]);"); |
| 4569 |
statement("adj[2][2] = spvDet2x2(m[0][0], m[0][1], m[1][0], m[1][1]);"); |
| 4570 |
statement_no_indent(""); |
| 4571 |
statement("// Calculate the determinant as a combination of the cofactors of the first row."); |
| 4572 |
statement("float det = (adj[0][0] * m[0][0]) + (adj[0][1] * m[1][0]) + (adj[0][2] * m[2][0]);"); |
| 4573 |
statement_no_indent(""); |
| 4574 |
statement("// Divide the classical adjoint matrix by the determinant."); |
| 4575 |
statement("// If determinant is zero, matrix is not invertable, so leave it unchanged."); |
| 4576 |
statement("return (det != 0.0f) ? (adj * (1.0f / det)) : m;"); |
| 4577 |
end_scope(); |
| 4578 |
statement(""); |
| 4579 |
break; |
| 4580 |
|
| 4581 |
case SPVFuncImplInverse2x2: |
| 4582 |
statement("// Returns the inverse of a matrix, by using the algorithm of calculating the classical"); |
| 4583 |
statement("// adjoint and dividing by the determinant. The contents of the matrix are changed."); |
| 4584 |
statement(force_inline); |
| 4585 |
statement("float2x2 spvInverse2x2(float2x2 m)"); |
| 4586 |
begin_scope(); |
| 4587 |
statement("float2x2 adj; // The adjoint matrix (inverse after dividing by determinant)"); |
| 4588 |
statement_no_indent(""); |
| 4589 |
statement("// Create the transpose of the cofactors, as the classical adjoint of the matrix."); |
| 4590 |
statement("adj[0][0] = m[1][1];"); |
| 4591 |
statement("adj[0][1] = -m[0][1];"); |
| 4592 |
statement_no_indent(""); |
| 4593 |
statement("adj[1][0] = -m[1][0];"); |
| 4594 |
statement("adj[1][1] = m[0][0];"); |
| 4595 |
statement_no_indent(""); |
| 4596 |
statement("// Calculate the determinant as a combination of the cofactors of the first row."); |
| 4597 |
statement("float det = (adj[0][0] * m[0][0]) + (adj[0][1] * m[1][0]);"); |
| 4598 |
statement_no_indent(""); |
| 4599 |
statement("// Divide the classical adjoint matrix by the determinant."); |
| 4600 |
statement("// If determinant is zero, matrix is not invertable, so leave it unchanged."); |
| 4601 |
statement("return (det != 0.0f) ? (adj * (1.0f / det)) : m;"); |
| 4602 |
end_scope(); |
| 4603 |
statement(""); |
| 4604 |
break; |
| 4605 |
|
| 4606 |
case SPVFuncImplForwardArgs: |
| 4607 |
statement("template<typename T> struct spvRemoveReference { typedef T type; };"); |
| 4608 |
statement("template<typename T> struct spvRemoveReference<thread T&> { typedef T type; };"); |
| 4609 |
statement("template<typename T> struct spvRemoveReference<thread T&&> { typedef T type; };"); |
| 4610 |
statement("template<typename T> inline constexpr thread T&& spvForward(thread typename " |
| 4611 |
"spvRemoveReference<T>::type& x)"); |
| 4612 |
begin_scope(); |
| 4613 |
statement("return static_cast<thread T&&>(x);"); |
| 4614 |
end_scope(); |
| 4615 |
statement("template<typename T> inline constexpr thread T&& spvForward(thread typename " |
| 4616 |
"spvRemoveReference<T>::type&& x)"); |
| 4617 |
begin_scope(); |
| 4618 |
statement("return static_cast<thread T&&>(x);"); |
| 4619 |
end_scope(); |
| 4620 |
statement(""); |
| 4621 |
break; |
| 4622 |
|
| 4623 |
case SPVFuncImplGetSwizzle: |
| 4624 |
statement("enum class spvSwizzle : uint"); |
| 4625 |
begin_scope(); |
| 4626 |
statement("none = 0,"); |
| 4627 |
statement("zero,"); |
| 4628 |
statement("one,"); |
| 4629 |
statement("red,"); |
| 4630 |
statement("green,"); |
| 4631 |
statement("blue,"); |
| 4632 |
statement("alpha"); |
| 4633 |
end_scope_decl(); |
| 4634 |
statement(""); |
| 4635 |
statement("template<typename T>"); |
| 4636 |
statement("inline T spvGetSwizzle(vec<T, 4> x, T c, spvSwizzle s)"); |
| 4637 |
begin_scope(); |
| 4638 |
statement("switch (s)"); |
| 4639 |
begin_scope(); |
| 4640 |
statement("case spvSwizzle::none:"); |
| 4641 |
statement(" return c;"); |
| 4642 |
statement("case spvSwizzle::zero:"); |
| 4643 |
statement(" return 0;"); |
| 4644 |
statement("case spvSwizzle::one:"); |
| 4645 |
statement(" return 1;"); |
| 4646 |
statement("case spvSwizzle::red:"); |
| 4647 |
statement(" return x.r;"); |
| 4648 |
statement("case spvSwizzle::green:"); |
| 4649 |
statement(" return x.g;"); |
| 4650 |
statement("case spvSwizzle::blue:"); |
| 4651 |
statement(" return x.b;"); |
| 4652 |
statement("case spvSwizzle::alpha:"); |
| 4653 |
statement(" return x.a;"); |
| 4654 |
end_scope(); |
| 4655 |
end_scope(); |
| 4656 |
statement(""); |
| 4657 |
break; |
| 4658 |
|
| 4659 |
case SPVFuncImplTextureSwizzle: |
| 4660 |
statement("// Wrapper function that swizzles texture samples and fetches."); |
| 4661 |
statement("template<typename T>"); |
| 4662 |
statement("inline vec<T, 4> spvTextureSwizzle(vec<T, 4> x, uint s)"); |
| 4663 |
begin_scope(); |
| 4664 |
statement("if (!s)"); |
| 4665 |
statement(" return x;"); |
| 4666 |
statement("return vec<T, 4>(spvGetSwizzle(x, x.r, spvSwizzle((s >> 0) & 0xFF)), " |
| 4667 |
"spvGetSwizzle(x, x.g, spvSwizzle((s >> 8) & 0xFF)), spvGetSwizzle(x, x.b, spvSwizzle((s >> 16) " |
| 4668 |
"& 0xFF)), " |
| 4669 |
"spvGetSwizzle(x, x.a, spvSwizzle((s >> 24) & 0xFF)));"); |
| 4670 |
end_scope(); |
| 4671 |
statement(""); |
| 4672 |
statement("template<typename T>"); |
| 4673 |
statement("inline T spvTextureSwizzle(T x, uint s)"); |
| 4674 |
begin_scope(); |
| 4675 |
statement("return spvTextureSwizzle(vec<T, 4>(x, 0, 0, 1), s).x;"); |
| 4676 |
end_scope(); |
| 4677 |
statement(""); |
| 4678 |
break; |
| 4679 |
|
| 4680 |
case SPVFuncImplGatherSwizzle: |
| 4681 |
statement("// Wrapper function that swizzles texture gathers."); |
| 4682 |
statement("template<typename T, template<typename, access = access::sample, typename = void> class Tex, " |
| 4683 |
"typename... Ts>"); |
| 4684 |
statement("inline vec<T, 4> spvGatherSwizzle(const thread Tex<T>& t, sampler s, " |
| 4685 |
"uint sw, component c, Ts... params) METAL_CONST_ARG(c)"); |
| 4686 |
begin_scope(); |
| 4687 |
statement("if (sw)"); |
| 4688 |
begin_scope(); |
| 4689 |
statement("switch (spvSwizzle((sw >> (uint(c) * 8)) & 0xFF))"); |
| 4690 |
begin_scope(); |
| 4691 |
statement("case spvSwizzle::none:"); |
| 4692 |
statement(" break;"); |
| 4693 |
statement("case spvSwizzle::zero:"); |
| 4694 |
statement(" return vec<T, 4>(0, 0, 0, 0);"); |
| 4695 |
statement("case spvSwizzle::one:"); |
| 4696 |
statement(" return vec<T, 4>(1, 1, 1, 1);"); |
| 4697 |
statement("case spvSwizzle::red:"); |
| 4698 |
statement(" return t.gather(s, spvForward<Ts>(params)..., component::x);"); |
| 4699 |
statement("case spvSwizzle::green:"); |
| 4700 |
statement(" return t.gather(s, spvForward<Ts>(params)..., component::y);"); |
| 4701 |
statement("case spvSwizzle::blue:"); |
| 4702 |
statement(" return t.gather(s, spvForward<Ts>(params)..., component::z);"); |
| 4703 |
statement("case spvSwizzle::alpha:"); |
| 4704 |
statement(" return t.gather(s, spvForward<Ts>(params)..., component::w);"); |
| 4705 |
end_scope(); |
| 4706 |
end_scope(); |
| 4707 |
// texture::gather insists on its component parameter being a constant |
| 4708 |
// expression, so we need this silly workaround just to compile the shader. |
| 4709 |
statement("switch (c)"); |
| 4710 |
begin_scope(); |
| 4711 |
statement("case component::x:"); |
| 4712 |
statement(" return t.gather(s, spvForward<Ts>(params)..., component::x);"); |
| 4713 |
statement("case component::y:"); |
| 4714 |
statement(" return t.gather(s, spvForward<Ts>(params)..., component::y);"); |
| 4715 |
statement("case component::z:"); |
| 4716 |
statement(" return t.gather(s, spvForward<Ts>(params)..., component::z);"); |
| 4717 |
statement("case component::w:"); |
| 4718 |
statement(" return t.gather(s, spvForward<Ts>(params)..., component::w);"); |
| 4719 |
end_scope(); |
| 4720 |
end_scope(); |
| 4721 |
statement(""); |
| 4722 |
break; |
| 4723 |
|
| 4724 |
case SPVFuncImplGatherCompareSwizzle: |
| 4725 |
statement("// Wrapper function that swizzles depth texture gathers."); |
| 4726 |
statement("template<typename T, template<typename, access = access::sample, typename = void> class Tex, " |
| 4727 |
"typename... Ts>"); |
| 4728 |
statement("inline vec<T, 4> spvGatherCompareSwizzle(const thread Tex<T>& t, sampler " |
| 4729 |
"s, uint sw, Ts... params) "); |
| 4730 |
begin_scope(); |
| 4731 |
statement("if (sw)"); |
| 4732 |
begin_scope(); |
| 4733 |
statement("switch (spvSwizzle(sw & 0xFF))"); |
| 4734 |
begin_scope(); |
| 4735 |
statement("case spvSwizzle::none:"); |
| 4736 |
statement("case spvSwizzle::red:"); |
| 4737 |
statement(" break;"); |
| 4738 |
statement("case spvSwizzle::zero:"); |
| 4739 |
statement("case spvSwizzle::green:"); |
| 4740 |
statement("case spvSwizzle::blue:"); |
| 4741 |
statement("case spvSwizzle::alpha:"); |
| 4742 |
statement(" return vec<T, 4>(0, 0, 0, 0);"); |
| 4743 |
statement("case spvSwizzle::one:"); |
| 4744 |
statement(" return vec<T, 4>(1, 1, 1, 1);"); |
| 4745 |
end_scope(); |
| 4746 |
end_scope(); |
| 4747 |
statement("return t.gather_compare(s, spvForward<Ts>(params)...);"); |
| 4748 |
end_scope(); |
| 4749 |
statement(""); |
| 4750 |
break; |
| 4751 |
|
| 4752 |
case SPVFuncImplSubgroupBroadcast: |
| 4753 |
// Metal doesn't allow broadcasting boolean values directly, but we can work around that by broadcasting |
| 4754 |
// them as integers. |
| 4755 |
statement("template<typename T>"); |
| 4756 |
statement("inline T spvSubgroupBroadcast(T value, ushort lane)"); |
| 4757 |
begin_scope(); |
| 4758 |
if (msl_options.is_ios()) |
| 4759 |
statement("return quad_broadcast(value, lane);"); |
| 4760 |
else |
| 4761 |
statement("return simd_broadcast(value, lane);"); |
| 4762 |
end_scope(); |
| 4763 |
statement(""); |
| 4764 |
statement("template<>"); |
| 4765 |
statement("inline bool spvSubgroupBroadcast(bool value, ushort lane)"); |
| 4766 |
begin_scope(); |
| 4767 |
if (msl_options.is_ios()) |
| 4768 |
statement("return !!quad_broadcast((ushort)value, lane);"); |
| 4769 |
else |
| 4770 |
statement("return !!simd_broadcast((ushort)value, lane);"); |
| 4771 |
end_scope(); |
| 4772 |
statement(""); |
| 4773 |
statement("template<uint N>"); |
| 4774 |
statement("inline vec<bool, N> spvSubgroupBroadcast(vec<bool, N> value, ushort lane)"); |
| 4775 |
begin_scope(); |
| 4776 |
if (msl_options.is_ios()) |
| 4777 |
statement("return (vec<bool, N>)quad_broadcast((vec<ushort, N>)value, lane);"); |
| 4778 |
else |
| 4779 |
statement("return (vec<bool, N>)simd_broadcast((vec<ushort, N>)value, lane);"); |
| 4780 |
end_scope(); |
| 4781 |
statement(""); |
| 4782 |
break; |
| 4783 |
|
| 4784 |
case SPVFuncImplSubgroupBroadcastFirst: |
| 4785 |
statement("template<typename T>"); |
| 4786 |
statement("inline T spvSubgroupBroadcastFirst(T value)"); |
| 4787 |
begin_scope(); |
| 4788 |
statement("return simd_broadcast_first(value);"); |
| 4789 |
end_scope(); |
| 4790 |
statement(""); |
| 4791 |
statement("template<>"); |
| 4792 |
statement("inline bool spvSubgroupBroadcastFirst(bool value)"); |
| 4793 |
begin_scope(); |
| 4794 |
statement("return !!simd_broadcast_first((ushort)value);"); |
| 4795 |
end_scope(); |
| 4796 |
statement(""); |
| 4797 |
statement("template<uint N>"); |
| 4798 |
statement("inline vec<bool, N> spvSubgroupBroadcastFirst(vec<bool, N> value)"); |
| 4799 |
begin_scope(); |
| 4800 |
statement("return (vec<bool, N>)simd_broadcast_first((vec<ushort, N>)value);"); |
| 4801 |
end_scope(); |
| 4802 |
statement(""); |
| 4803 |
break; |
| 4804 |
|
| 4805 |
case SPVFuncImplSubgroupBallot: |
| 4806 |
statement("inline uint4 spvSubgroupBallot(bool value)"); |
| 4807 |
begin_scope(); |
| 4808 |
statement("simd_vote vote = simd_ballot(value);"); |
| 4809 |
statement("// simd_ballot() returns a 64-bit integer-like object, but"); |
| 4810 |
statement("// SPIR-V callers expect a uint4. We must convert."); |
| 4811 |
statement("// FIXME: This won't include higher bits if Apple ever supports"); |
| 4812 |
statement("// 128 lanes in an SIMD-group."); |
| 4813 |
statement("return uint4((uint)((simd_vote::vote_t)vote & 0xFFFFFFFF), (uint)(((simd_vote::vote_t)vote >> " |
| 4814 |
"32) & 0xFFFFFFFF), 0, 0);"); |
| 4815 |
end_scope(); |
| 4816 |
statement(""); |
| 4817 |
break; |
| 4818 |
|
| 4819 |
case SPVFuncImplSubgroupBallotBitExtract: |
| 4820 |
statement("inline bool spvSubgroupBallotBitExtract(uint4 ballot, uint bit)"); |
| 4821 |
begin_scope(); |
| 4822 |
statement("return !!extract_bits(ballot[bit / 32], bit % 32, 1);"); |
| 4823 |
end_scope(); |
| 4824 |
statement(""); |
| 4825 |
break; |
| 4826 |
|
| 4827 |
case SPVFuncImplSubgroupBallotFindLSB: |
| 4828 |
statement("inline uint spvSubgroupBallotFindLSB(uint4 ballot, uint gl_SubgroupSize)"); |
| 4829 |
begin_scope(); |
| 4830 |
statement("uint4 mask = uint4(extract_bits(0xFFFFFFFF, 0, min(gl_SubgroupSize, 32u)), " |
| 4831 |
"extract_bits(0xFFFFFFFF, 0, (uint)max((int)gl_SubgroupSize - 32, 0)), uint2(0));"); |
| 4832 |
statement("ballot &= mask;"); |
| 4833 |
statement("return select(ctz(ballot.x), select(32 + ctz(ballot.y), select(64 + ctz(ballot.z), select(96 + " |
| 4834 |
"ctz(ballot.w), uint(-1), ballot.w == 0), ballot.z == 0), ballot.y == 0), ballot.x == 0);"); |
| 4835 |
end_scope(); |
| 4836 |
statement(""); |
| 4837 |
break; |
| 4838 |
|
| 4839 |
case SPVFuncImplSubgroupBallotFindMSB: |
| 4840 |
statement("inline uint spvSubgroupBallotFindMSB(uint4 ballot, uint gl_SubgroupSize)"); |
| 4841 |
begin_scope(); |
| 4842 |
statement("uint4 mask = uint4(extract_bits(0xFFFFFFFF, 0, min(gl_SubgroupSize, 32u)), " |
| 4843 |
"extract_bits(0xFFFFFFFF, 0, (uint)max((int)gl_SubgroupSize - 32, 0)), uint2(0));"); |
| 4844 |
statement("ballot &= mask;"); |
| 4845 |
statement("return select(128 - (clz(ballot.w) + 1), select(96 - (clz(ballot.z) + 1), select(64 - " |
| 4846 |
"(clz(ballot.y) + 1), select(32 - (clz(ballot.x) + 1), uint(-1), ballot.x == 0), ballot.y == 0), " |
| 4847 |
"ballot.z == 0), ballot.w == 0);"); |
| 4848 |
end_scope(); |
| 4849 |
statement(""); |
| 4850 |
break; |
| 4851 |
|
| 4852 |
case SPVFuncImplSubgroupBallotBitCount: |
| 4853 |
statement("inline uint spvPopCount4(uint4 ballot)"); |
| 4854 |
begin_scope(); |
| 4855 |
statement("return popcount(ballot.x) + popcount(ballot.y) + popcount(ballot.z) + popcount(ballot.w);"); |
| 4856 |
end_scope(); |
| 4857 |
statement(""); |
| 4858 |
statement("inline uint spvSubgroupBallotBitCount(uint4 ballot, uint gl_SubgroupSize)"); |
| 4859 |
begin_scope(); |
| 4860 |
statement("uint4 mask = uint4(extract_bits(0xFFFFFFFF, 0, min(gl_SubgroupSize, 32u)), " |
| 4861 |
"extract_bits(0xFFFFFFFF, 0, (uint)max((int)gl_SubgroupSize - 32, 0)), uint2(0));"); |
| 4862 |
statement("return spvPopCount4(ballot & mask);"); |
| 4863 |
end_scope(); |
| 4864 |
statement(""); |
| 4865 |
statement("inline uint spvSubgroupBallotInclusiveBitCount(uint4 ballot, uint gl_SubgroupInvocationID)"); |
| 4866 |
begin_scope(); |
| 4867 |
statement("uint4 mask = uint4(extract_bits(0xFFFFFFFF, 0, min(gl_SubgroupInvocationID + 1, 32u)), " |
| 4868 |
"extract_bits(0xFFFFFFFF, 0, (uint)max((int)gl_SubgroupInvocationID + 1 - 32, 0)), " |
| 4869 |
"uint2(0));"); |
| 4870 |
statement("return spvPopCount4(ballot & mask);"); |
| 4871 |
end_scope(); |
| 4872 |
statement(""); |
| 4873 |
statement("inline uint spvSubgroupBallotExclusiveBitCount(uint4 ballot, uint gl_SubgroupInvocationID)"); |
| 4874 |
begin_scope(); |
| 4875 |
statement("uint4 mask = uint4(extract_bits(0xFFFFFFFF, 0, min(gl_SubgroupInvocationID, 32u)), " |
| 4876 |
"extract_bits(0xFFFFFFFF, 0, (uint)max((int)gl_SubgroupInvocationID - 32, 0)), uint2(0));"); |
| 4877 |
statement("return spvPopCount4(ballot & mask);"); |
| 4878 |
end_scope(); |
| 4879 |
statement(""); |
| 4880 |
break; |
| 4881 |
|
| 4882 |
case SPVFuncImplSubgroupAllEqual: |
| 4883 |
// Metal doesn't provide a function to evaluate this directly. But, we can |
| 4884 |
// implement this by comparing every thread's value to one thread's value |
| 4885 |
// (in this case, the value of the first active thread). Then, by the transitive |
| 4886 |
// property of equality, if all comparisons return true, then they are all equal. |
| 4887 |
statement("template<typename T>"); |
| 4888 |
statement("inline bool spvSubgroupAllEqual(T value)"); |
| 4889 |
begin_scope(); |
| 4890 |
statement("return simd_all(all(value == simd_broadcast_first(value)));"); |
| 4891 |
end_scope(); |
| 4892 |
statement(""); |
| 4893 |
statement("template<>"); |
| 4894 |
statement("inline bool spvSubgroupAllEqual(bool value)"); |
| 4895 |
begin_scope(); |
| 4896 |
statement("return simd_all(value) || !simd_any(value);"); |
| 4897 |
end_scope(); |
| 4898 |
statement(""); |
| 4899 |
statement("template<uint N>"); |
| 4900 |
statement("inline bool spvSubgroupAllEqual(vec<bool, N> value)"); |
| 4901 |
begin_scope(); |
| 4902 |
statement("return simd_all(all(value == (vec<bool, N>)simd_broadcast_first((vec<ushort, N>)value)));"); |
| 4903 |
end_scope(); |
| 4904 |
statement(""); |
| 4905 |
break; |
| 4906 |
|
| 4907 |
case SPVFuncImplSubgroupShuffle: |
| 4908 |
statement("template<typename T>"); |
| 4909 |
statement("inline T spvSubgroupShuffle(T value, ushort lane)"); |
| 4910 |
begin_scope(); |
| 4911 |
if (msl_options.is_ios()) |
| 4912 |
statement("return quad_shuffle(value, lane);"); |
| 4913 |
else |
| 4914 |
statement("return simd_shuffle(value, lane);"); |
| 4915 |
end_scope(); |
| 4916 |
statement(""); |
| 4917 |
statement("template<>"); |
| 4918 |
statement("inline bool spvSubgroupShuffle(bool value, ushort lane)"); |
| 4919 |
begin_scope(); |
| 4920 |
if (msl_options.is_ios()) |
| 4921 |
statement("return !!quad_shuffle((ushort)value, lane);"); |
| 4922 |
else |
| 4923 |
statement("return !!simd_shuffle((ushort)value, lane);"); |
| 4924 |
end_scope(); |
| 4925 |
statement(""); |
| 4926 |
statement("template<uint N>"); |
| 4927 |
statement("inline vec<bool, N> spvSubgroupShuffle(vec<bool, N> value, ushort lane)"); |
| 4928 |
begin_scope(); |
| 4929 |
if (msl_options.is_ios()) |
| 4930 |
statement("return (vec<bool, N>)quad_shuffle((vec<ushort, N>)value, lane);"); |
| 4931 |
else |
| 4932 |
statement("return (vec<bool, N>)simd_shuffle((vec<ushort, N>)value, lane);"); |
| 4933 |
end_scope(); |
| 4934 |
statement(""); |
| 4935 |
break; |
| 4936 |
|
| 4937 |
case SPVFuncImplSubgroupShuffleXor: |
| 4938 |
statement("template<typename T>"); |
| 4939 |
statement("inline T spvSubgroupShuffleXor(T value, ushort mask)"); |
| 4940 |
begin_scope(); |
| 4941 |
if (msl_options.is_ios()) |
| 4942 |
statement("return quad_shuffle_xor(value, mask);"); |
| 4943 |
else |
| 4944 |
statement("return simd_shuffle_xor(value, mask);"); |
| 4945 |
end_scope(); |
| 4946 |
statement(""); |
| 4947 |
statement("template<>"); |
| 4948 |
statement("inline bool spvSubgroupShuffleXor(bool value, ushort mask)"); |
| 4949 |
begin_scope(); |
| 4950 |
if (msl_options.is_ios()) |
| 4951 |
statement("return !!quad_shuffle_xor((ushort)value, mask);"); |
| 4952 |
else |
| 4953 |
statement("return !!simd_shuffle_xor((ushort)value, mask);"); |
| 4954 |
end_scope(); |
| 4955 |
statement(""); |
| 4956 |
statement("template<uint N>"); |
| 4957 |
statement("inline vec<bool, N> spvSubgroupShuffleXor(vec<bool, N> value, ushort mask)"); |
| 4958 |
begin_scope(); |
| 4959 |
if (msl_options.is_ios()) |
| 4960 |
statement("return (vec<bool, N>)quad_shuffle_xor((vec<ushort, N>)value, mask);"); |
| 4961 |
else |
| 4962 |
statement("return (vec<bool, N>)simd_shuffle_xor((vec<ushort, N>)value, mask);"); |
| 4963 |
end_scope(); |
| 4964 |
statement(""); |
| 4965 |
break; |
| 4966 |
|
| 4967 |
case SPVFuncImplSubgroupShuffleUp: |
| 4968 |
statement("template<typename T>"); |
| 4969 |
statement("inline T spvSubgroupShuffleUp(T value, ushort delta)"); |
| 4970 |
begin_scope(); |
| 4971 |
if (msl_options.is_ios()) |
| 4972 |
statement("return quad_shuffle_up(value, delta);"); |
| 4973 |
else |
| 4974 |
statement("return simd_shuffle_up(value, delta);"); |
| 4975 |
end_scope(); |
| 4976 |
statement(""); |
| 4977 |
statement("template<>"); |
| 4978 |
statement("inline bool spvSubgroupShuffleUp(bool value, ushort delta)"); |
| 4979 |
begin_scope(); |
| 4980 |
if (msl_options.is_ios()) |
| 4981 |
statement("return !!quad_shuffle_up((ushort)value, delta);"); |
| 4982 |
else |
| 4983 |
statement("return !!simd_shuffle_up((ushort)value, delta);"); |
| 4984 |
end_scope(); |
| 4985 |
statement(""); |
| 4986 |
statement("template<uint N>"); |
| 4987 |
statement("inline vec<bool, N> spvSubgroupShuffleUp(vec<bool, N> value, ushort delta)"); |
| 4988 |
begin_scope(); |
| 4989 |
if (msl_options.is_ios()) |
| 4990 |
statement("return (vec<bool, N>)quad_shuffle_up((vec<ushort, N>)value, delta);"); |
| 4991 |
else |
| 4992 |
statement("return (vec<bool, N>)simd_shuffle_up((vec<ushort, N>)value, delta);"); |
| 4993 |
end_scope(); |
| 4994 |
statement(""); |
| 4995 |
break; |
| 4996 |
|
| 4997 |
case SPVFuncImplSubgroupShuffleDown: |
| 4998 |
statement("template<typename T>"); |
| 4999 |
statement("inline T spvSubgroupShuffleDown(T value, ushort delta)"); |
| 5000 |
begin_scope(); |
| 5001 |
if (msl_options.is_ios()) |
| 5002 |
statement("return quad_shuffle_down(value, delta);"); |
| 5003 |
else |
| 5004 |
statement("return simd_shuffle_down(value, delta);"); |
| 5005 |
end_scope(); |
| 5006 |
statement(""); |
| 5007 |
statement("template<>"); |
| 5008 |
statement("inline bool spvSubgroupShuffleDown(bool value, ushort delta)"); |
| 5009 |
begin_scope(); |
| 5010 |
if (msl_options.is_ios()) |
| 5011 |
statement("return !!quad_shuffle_down((ushort)value, delta);"); |
| 5012 |
else |
| 5013 |
statement("return !!simd_shuffle_down((ushort)value, delta);"); |
| 5014 |
end_scope(); |
| 5015 |
statement(""); |
| 5016 |
statement("template<uint N>"); |
| 5017 |
statement("inline vec<bool, N> spvSubgroupShuffleDown(vec<bool, N> value, ushort delta)"); |
| 5018 |
begin_scope(); |
| 5019 |
if (msl_options.is_ios()) |
| 5020 |
statement("return (vec<bool, N>)quad_shuffle_down((vec<ushort, N>)value, delta);"); |
| 5021 |
else |
| 5022 |
statement("return (vec<bool, N>)simd_shuffle_down((vec<ushort, N>)value, delta);"); |
| 5023 |
end_scope(); |
| 5024 |
statement(""); |
| 5025 |
break; |
| 5026 |
|
| 5027 |
case SPVFuncImplQuadBroadcast: |
| 5028 |
statement("template<typename T>"); |
| 5029 |
statement("inline T spvQuadBroadcast(T value, uint lane)"); |
| 5030 |
begin_scope(); |
| 5031 |
statement("return quad_broadcast(value, lane);"); |
| 5032 |
end_scope(); |
| 5033 |
statement(""); |
| 5034 |
statement("template<>"); |
| 5035 |
statement("inline bool spvQuadBroadcast(bool value, uint lane)"); |
| 5036 |
begin_scope(); |
| 5037 |
statement("return !!quad_broadcast((ushort)value, lane);"); |
| 5038 |
end_scope(); |
| 5039 |
statement(""); |
| 5040 |
statement("template<uint N>"); |
| 5041 |
statement("inline vec<bool, N> spvQuadBroadcast(vec<bool, N> value, uint lane)"); |
| 5042 |
begin_scope(); |
| 5043 |
statement("return (vec<bool, N>)quad_broadcast((vec<ushort, N>)value, lane);"); |
| 5044 |
end_scope(); |
| 5045 |
statement(""); |
| 5046 |
break; |
| 5047 |
|
| 5048 |
case SPVFuncImplQuadSwap: |
| 5049 |
// We can implement this easily based on the following table giving |
| 5050 |
// the target lane ID from the direction and current lane ID: |
| 5051 |
// Direction |
| 5052 |
// | 0 | 1 | 2 | |
| 5053 |
// ---+---+---+---+ |
| 5054 |
// L 0 | 1 2 3 |
| 5055 |
// a 1 | 0 3 2 |
| 5056 |
// n 2 | 3 0 1 |
| 5057 |
// e 3 | 2 1 0 |
| 5058 |
// Notice that target = source ^ (direction + 1). |
| 5059 |
statement("template<typename T>"); |
| 5060 |
statement("inline T spvQuadSwap(T value, uint dir)"); |
| 5061 |
begin_scope(); |
| 5062 |
statement("return quad_shuffle_xor(value, dir + 1);"); |
| 5063 |
end_scope(); |
| 5064 |
statement(""); |
| 5065 |
statement("template<>"); |
| 5066 |
statement("inline bool spvQuadSwap(bool value, uint dir)"); |
| 5067 |
begin_scope(); |
| 5068 |
statement("return !!quad_shuffle_xor((ushort)value, dir + 1);"); |
| 5069 |
end_scope(); |
| 5070 |
statement(""); |
| 5071 |
statement("template<uint N>"); |
| 5072 |
statement("inline vec<bool, N> spvQuadSwap(vec<bool, N> value, uint dir)"); |
| 5073 |
begin_scope(); |
| 5074 |
statement("return (vec<bool, N>)quad_shuffle_xor((vec<ushort, N>)value, dir + 1);"); |
| 5075 |
end_scope(); |
| 5076 |
statement(""); |
| 5077 |
break; |
| 5078 |
|
| 5079 |
case SPVFuncImplReflectScalar: |
| 5080 |
// Metal does not support scalar versions of these functions. |
| 5081 |
statement("template<typename T>"); |
| 5082 |
statement("inline T spvReflect(T i, T n)"); |
| 5083 |
begin_scope(); |
| 5084 |
statement("return i - T(2) * i * n * n;"); |
| 5085 |
end_scope(); |
| 5086 |
statement(""); |
| 5087 |
break; |
| 5088 |
|
| 5089 |
case SPVFuncImplRefractScalar: |
| 5090 |
// Metal does not support scalar versions of these functions. |
| 5091 |
statement("template<typename T>"); |
| 5092 |
statement("inline T spvRefract(T i, T n, T eta)"); |
| 5093 |
begin_scope(); |
| 5094 |
statement("T NoI = n * i;"); |
| 5095 |
statement("T NoI2 = NoI * NoI;"); |
| 5096 |
statement("T k = T(1) - eta * eta * (T(1) - NoI2);"); |
| 5097 |
statement("if (k < T(0))"); |
| 5098 |
begin_scope(); |
| 5099 |
statement("return T(0);"); |
| 5100 |
end_scope(); |
| 5101 |
statement("else"); |
| 5102 |
begin_scope(); |
| 5103 |
statement("return eta * i - (eta * NoI + sqrt(k)) * n;"); |
| 5104 |
end_scope(); |
| 5105 |
end_scope(); |
| 5106 |
statement(""); |
| 5107 |
break; |
| 5108 |
|
| 5109 |
case SPVFuncImplFaceForwardScalar: |
| 5110 |
// Metal does not support scalar versions of these functions. |
| 5111 |
statement("template<typename T>"); |
| 5112 |
statement("inline T spvFaceForward(T n, T i, T nref)"); |
| 5113 |
begin_scope(); |
| 5114 |
statement("return i * nref < T(0) ? n : -n;"); |
| 5115 |
end_scope(); |
| 5116 |
statement(""); |
| 5117 |
break; |
| 5118 |
|
| 5119 |
case SPVFuncImplChromaReconstructNearest2Plane: |
| 5120 |
statement("template<typename T, typename... LodOptions>"); |
| 5121 |
statement("inline vec<T, 4> spvChromaReconstructNearest(texture2d<T> plane0, texture2d<T> plane1, sampler " |
| 5122 |
"samp, float2 coord, LodOptions... options)"); |
| 5123 |
begin_scope(); |
| 5124 |
statement("vec<T, 4> ycbcr = vec<T, 4>(0, 0, 0, 1);"); |
| 5125 |
statement("ycbcr.g = plane0.sample(samp, coord, spvForward<LodOptions>(options)...).r;"); |
| 5126 |
statement("ycbcr.br = plane1.sample(samp, coord, spvForward<LodOptions>(options)...).rg;"); |
| 5127 |
statement("return ycbcr;"); |
| 5128 |
end_scope(); |
| 5129 |
statement(""); |
| 5130 |
break; |
| 5131 |
|
| 5132 |
case SPVFuncImplChromaReconstructNearest3Plane: |
| 5133 |
statement("template<typename T, typename... LodOptions>"); |
| 5134 |
statement("inline vec<T, 4> spvChromaReconstructNearest(texture2d<T> plane0, texture2d<T> plane1, " |
| 5135 |
"texture2d<T> plane2, sampler samp, float2 coord, LodOptions... options)"); |
| 5136 |
begin_scope(); |
| 5137 |
statement("vec<T, 4> ycbcr = vec<T, 4>(0, 0, 0, 1);"); |
| 5138 |
statement("ycbcr.g = plane0.sample(samp, coord, spvForward<LodOptions>(options)...).r;"); |
| 5139 |
statement("ycbcr.b = plane1.sample(samp, coord, spvForward<LodOptions>(options)...).r;"); |
| 5140 |
statement("ycbcr.r = plane2.sample(samp, coord, spvForward<LodOptions>(options)...).r;"); |
| 5141 |
statement("return ycbcr;"); |
| 5142 |
end_scope(); |
| 5143 |
statement(""); |
| 5144 |
break; |
| 5145 |
|
| 5146 |
case SPVFuncImplChromaReconstructLinear422CositedEven2Plane: |
| 5147 |
statement("template<typename T, typename... LodOptions>"); |
| 5148 |
statement("inline vec<T, 4> spvChromaReconstructLinear422CositedEven(texture2d<T> plane0, texture2d<T> " |
| 5149 |
"plane1, sampler samp, float2 coord, LodOptions... options)"); |
| 5150 |
begin_scope(); |
| 5151 |
statement("vec<T, 4> ycbcr = vec<T, 4>(0, 0, 0, 1);"); |
| 5152 |
statement("ycbcr.g = plane0.sample(samp, coord, spvForward<LodOptions>(options)...).r;"); |
| 5153 |
statement("if (fract(coord.x * plane1.get_width()) != 0.0)"); |
| 5154 |
begin_scope(); |
| 5155 |
statement("ycbcr.br = vec<T, 2>(mix(plane1.sample(samp, coord, spvForward<LodOptions>(options)...), " |
| 5156 |
"plane1.sample(samp, coord, spvForward<LodOptions>(options)..., int2(1, 0)), 0.5).rg);"); |
| 5157 |
end_scope(); |
| 5158 |
statement("else"); |
| 5159 |
begin_scope(); |
| 5160 |
statement("ycbcr.br = plane1.sample(samp, coord, spvForward<LodOptions>(options)...).rg;"); |
| 5161 |
end_scope(); |
| 5162 |
statement("return ycbcr;"); |
| 5163 |
end_scope(); |
| 5164 |
statement(""); |
| 5165 |
break; |
| 5166 |
|
| 5167 |
case SPVFuncImplChromaReconstructLinear422CositedEven3Plane: |
| 5168 |
statement("template<typename T, typename... LodOptions>"); |
| 5169 |
statement("inline vec<T, 4> spvChromaReconstructLinear422CositedEven(texture2d<T> plane0, texture2d<T> " |
| 5170 |
"plane1, texture2d<T> plane2, sampler samp, float2 coord, LodOptions... options)"); |
| 5171 |
begin_scope(); |
| 5172 |
statement("vec<T, 4> ycbcr = vec<T, 4>(0, 0, 0, 1);"); |
| 5173 |
statement("ycbcr.g = plane0.sample(samp, coord, spvForward<LodOptions>(options)...).r;"); |
| 5174 |
statement("if (fract(coord.x * plane1.get_width()) != 0.0)"); |
| 5175 |
begin_scope(); |
| 5176 |
statement("ycbcr.b = T(mix(plane1.sample(samp, coord, spvForward<LodOptions>(options)...), " |
| 5177 |
"plane1.sample(samp, coord, spvForward<LodOptions>(options)..., int2(1, 0)), 0.5).r);"); |
| 5178 |
statement("ycbcr.r = T(mix(plane2.sample(samp, coord, spvForward<LodOptions>(options)...), " |
| 5179 |
"plane2.sample(samp, coord, spvForward<LodOptions>(options)..., int2(1, 0)), 0.5).r);"); |
| 5180 |
end_scope(); |
| 5181 |
statement("else"); |
| 5182 |
begin_scope(); |
| 5183 |
statement("ycbcr.b = plane1.sample(samp, coord, spvForward<LodOptions>(options)...).r;"); |
| 5184 |
statement("ycbcr.r = plane2.sample(samp, coord, spvForward<LodOptions>(options)...).r;"); |
| 5185 |
end_scope(); |
| 5186 |
statement("return ycbcr;"); |
| 5187 |
end_scope(); |
| 5188 |
statement(""); |
| 5189 |
break; |
| 5190 |
|
| 5191 |
case SPVFuncImplChromaReconstructLinear422Midpoint2Plane: |
| 5192 |
statement("template<typename T, typename... LodOptions>"); |
| 5193 |
statement("inline vec<T, 4> spvChromaReconstructLinear422Midpoint(texture2d<T> plane0, texture2d<T> " |
| 5194 |
"plane1, sampler samp, float2 coord, LodOptions... options)"); |
| 5195 |
begin_scope(); |
| 5196 |
statement("vec<T, 4> ycbcr = vec<T, 4>(0, 0, 0, 1);"); |
| 5197 |
statement("ycbcr.g = plane0.sample(samp, coord, spvForward<LodOptions>(options)...).r;"); |
| 5198 |
statement("int2 offs = int2(fract(coord.x * plane1.get_width()) != 0.0 ? 1 : -1, 0);"); |
| 5199 |
statement("ycbcr.br = vec<T, 2>(mix(plane1.sample(samp, coord, spvForward<LodOptions>(options)...), " |
| 5200 |
"plane1.sample(samp, coord, spvForward<LodOptions>(options)..., offs), 0.25).rg);"); |
| 5201 |
statement("return ycbcr;"); |
| 5202 |
end_scope(); |
| 5203 |
statement(""); |
| 5204 |
break; |
| 5205 |
|
| 5206 |
case SPVFuncImplChromaReconstructLinear422Midpoint3Plane: |
| 5207 |
statement("template<typename T, typename... LodOptions>"); |
| 5208 |
statement("inline vec<T, 4> spvChromaReconstructLinear422Midpoint(texture2d<T> plane0, texture2d<T> " |
| 5209 |
"plane1, texture2d<T> plane2, sampler samp, float2 coord, LodOptions... options)"); |
| 5210 |
begin_scope(); |
| 5211 |
statement("vec<T, 4> ycbcr = vec<T, 4>(0, 0, 0, 1);"); |
| 5212 |
statement("ycbcr.g = plane0.sample(samp, coord, spvForward<LodOptions>(options)...).r;"); |
| 5213 |
statement("int2 offs = int2(fract(coord.x * plane1.get_width()) != 0.0 ? 1 : -1, 0);"); |
| 5214 |
statement("ycbcr.b = T(mix(plane1.sample(samp, coord, spvForward<LodOptions>(options)...), " |
| 5215 |
"plane1.sample(samp, coord, spvForward<LodOptions>(options)..., offs), 0.25).r);"); |
| 5216 |
statement("ycbcr.r = T(mix(plane2.sample(samp, coord, spvForward<LodOptions>(options)...), " |
| 5217 |
"plane2.sample(samp, coord, spvForward<LodOptions>(options)..., offs), 0.25).r);"); |
| 5218 |
statement("return ycbcr;"); |
| 5219 |
end_scope(); |
| 5220 |
statement(""); |
| 5221 |
break; |
| 5222 |
|
| 5223 |
case SPVFuncImplChromaReconstructLinear420XCositedEvenYCositedEven2Plane: |
| 5224 |
statement("template<typename T, typename... LodOptions>"); |
| 5225 |
statement("inline vec<T, 4> spvChromaReconstructLinear420XCositedEvenYCositedEven(texture2d<T> plane0, " |
| 5226 |
"texture2d<T> plane1, sampler samp, float2 coord, LodOptions... options)"); |
| 5227 |
begin_scope(); |
| 5228 |
statement("vec<T, 4> ycbcr = vec<T, 4>(0, 0, 0, 1);"); |
| 5229 |
statement("ycbcr.g = plane0.sample(samp, coord, spvForward<LodOptions>(options)...).r;"); |
| 5230 |
statement("float2 ab = fract(round(coord * float2(plane0.get_width(), plane0.get_height())) * 0.5);"); |
| 5231 |
statement("ycbcr.br = vec<T, 2>(mix(mix(plane1.sample(samp, coord, spvForward<LodOptions>(options)...), " |
| 5232 |
"plane1.sample(samp, coord, spvForward<LodOptions>(options)..., int2(1, 0)), ab.x), " |
| 5233 |
"mix(plane1.sample(samp, coord, spvForward<LodOptions>(options)..., int2(0, 1)), " |
| 5234 |
"plane1.sample(samp, coord, spvForward<LodOptions>(options)..., int2(1, 1)), ab.x), ab.y).rg);"); |
| 5235 |
statement("return ycbcr;"); |
| 5236 |
end_scope(); |
| 5237 |
statement(""); |
| 5238 |
break; |
| 5239 |
|
| 5240 |
case SPVFuncImplChromaReconstructLinear420XCositedEvenYCositedEven3Plane: |
| 5241 |
statement("template<typename T, typename... LodOptions>"); |
| 5242 |
statement("inline vec<T, 4> spvChromaReconstructLinear420XCositedEvenYCositedEven(texture2d<T> plane0, " |
| 5243 |
"texture2d<T> plane1, texture2d<T> plane2, sampler samp, float2 coord, LodOptions... options)"); |
| 5244 |
begin_scope(); |
| 5245 |
statement("vec<T, 4> ycbcr = vec<T, 4>(0, 0, 0, 1);"); |
| 5246 |
statement("ycbcr.g = plane0.sample(samp, coord, spvForward<LodOptions>(options)...).r;"); |
| 5247 |
statement("float2 ab = fract(round(coord * float2(plane0.get_width(), plane0.get_height())) * 0.5);"); |
| 5248 |
statement("ycbcr.b = T(mix(mix(plane1.sample(samp, coord, spvForward<LodOptions>(options)...), " |
| 5249 |
"plane1.sample(samp, coord, spvForward<LodOptions>(options)..., int2(1, 0)), ab.x), " |
| 5250 |
"mix(plane1.sample(samp, coord, spvForward<LodOptions>(options)..., int2(0, 1)), " |
| 5251 |
"plane1.sample(samp, coord, spvForward<LodOptions>(options)..., int2(1, 1)), ab.x), ab.y).r);"); |
| 5252 |
statement("ycbcr.r = T(mix(mix(plane2.sample(samp, coord, spvForward<LodOptions>(options)...), " |
| 5253 |
"plane2.sample(samp, coord, spvForward<LodOptions>(options)..., int2(1, 0)), ab.x), " |
| 5254 |
"mix(plane2.sample(samp, coord, spvForward<LodOptions>(options)..., int2(0, 1)), " |
| 5255 |
"plane2.sample(samp, coord, spvForward<LodOptions>(options)..., int2(1, 1)), ab.x), ab.y).r);"); |
| 5256 |
statement("return ycbcr;"); |
| 5257 |
end_scope(); |
| 5258 |
statement(""); |
| 5259 |
break; |
| 5260 |
|
| 5261 |
case SPVFuncImplChromaReconstructLinear420XMidpointYCositedEven2Plane: |
| 5262 |
statement("template<typename T, typename... LodOptions>"); |
| 5263 |
statement("inline vec<T, 4> spvChromaReconstructLinear420XMidpointYCositedEven(texture2d<T> plane0, " |
| 5264 |
"texture2d<T> plane1, sampler samp, float2 coord, LodOptions... options)"); |
| 5265 |
begin_scope(); |
| 5266 |
statement("vec<T, 4> ycbcr = vec<T, 4>(0, 0, 0, 1);"); |
| 5267 |
statement("ycbcr.g = plane0.sample(samp, coord, spvForward<LodOptions>(options)...).r;"); |
| 5268 |
statement("float2 ab = fract((round(coord * float2(plane0.get_width(), plane0.get_height())) - float2(0.5, " |
| 5269 |
"0)) * 0.5);"); |
| 5270 |
statement("ycbcr.br = vec<T, 2>(mix(mix(plane1.sample(samp, coord, spvForward<LodOptions>(options)...), " |
| 5271 |
"plane1.sample(samp, coord, spvForward<LodOptions>(options)..., int2(1, 0)), ab.x), " |
| 5272 |
"mix(plane1.sample(samp, coord, spvForward<LodOptions>(options)..., int2(0, 1)), " |
| 5273 |
"plane1.sample(samp, coord, spvForward<LodOptions>(options)..., int2(1, 1)), ab.x), ab.y).rg);"); |
| 5274 |
statement("return ycbcr;"); |
| 5275 |
end_scope(); |
| 5276 |
statement(""); |
| 5277 |
break; |
| 5278 |
|
| 5279 |
case SPVFuncImplChromaReconstructLinear420XMidpointYCositedEven3Plane: |
| 5280 |
statement("template<typename T, typename... LodOptions>"); |
| 5281 |
statement("inline vec<T, 4> spvChromaReconstructLinear420XMidpointYCositedEven(texture2d<T> plane0, " |
| 5282 |
"texture2d<T> plane1, texture2d<T> plane2, sampler samp, float2 coord, LodOptions... options)"); |
| 5283 |
begin_scope(); |
| 5284 |
statement("vec<T, 4> ycbcr = vec<T, 4>(0, 0, 0, 1);"); |
| 5285 |
statement("ycbcr.g = plane0.sample(samp, coord, spvForward<LodOptions>(options)...).r;"); |
| 5286 |
statement("float2 ab = fract((round(coord * float2(plane0.get_width(), plane0.get_height())) - float2(0.5, " |
| 5287 |
"0)) * 0.5);"); |
| 5288 |
statement("ycbcr.b = T(mix(mix(plane1.sample(samp, coord, spvForward<LodOptions>(options)...), " |
| 5289 |
"plane1.sample(samp, coord, spvForward<LodOptions>(options)..., int2(1, 0)), ab.x), " |
| 5290 |
"mix(plane1.sample(samp, coord, spvForward<LodOptions>(options)..., int2(0, 1)), " |
| 5291 |
"plane1.sample(samp, coord, spvForward<LodOptions>(options)..., int2(1, 1)), ab.x), ab.y).r);"); |
| 5292 |
statement("ycbcr.r = T(mix(mix(plane2.sample(samp, coord, spvForward<LodOptions>(options)...), " |
| 5293 |
"plane2.sample(samp, coord, spvForward<LodOptions>(options)..., int2(1, 0)), ab.x), " |
| 5294 |
"mix(plane2.sample(samp, coord, spvForward<LodOptions>(options)..., int2(0, 1)), " |
| 5295 |
"plane2.sample(samp, coord, spvForward<LodOptions>(options)..., int2(1, 1)), ab.x), ab.y).r);"); |
| 5296 |
statement("return ycbcr;"); |
| 5297 |
end_scope(); |
| 5298 |
statement(""); |
| 5299 |
break; |
| 5300 |
|
| 5301 |
case SPVFuncImplChromaReconstructLinear420XCositedEvenYMidpoint2Plane: |
| 5302 |
statement("template<typename T, typename... LodOptions>"); |
| 5303 |
statement("inline vec<T, 4> spvChromaReconstructLinear420XCositedEvenYMidpoint(texture2d<T> plane0, " |
| 5304 |
"texture2d<T> plane1, sampler samp, float2 coord, LodOptions... options)"); |
| 5305 |
begin_scope(); |
| 5306 |
statement("vec<T, 4> ycbcr = vec<T, 4>(0, 0, 0, 1);"); |
| 5307 |
statement("ycbcr.g = plane0.sample(samp, coord, spvForward<LodOptions>(options)...).r;"); |
| 5308 |
statement("float2 ab = fract((round(coord * float2(plane0.get_width(), plane0.get_height())) - float2(0, " |
| 5309 |
"0.5)) * 0.5);"); |
| 5310 |
statement("ycbcr.br = vec<T, 2>(mix(mix(plane1.sample(samp, coord, spvForward<LodOptions>(options)...), " |
| 5311 |
"plane1.sample(samp, coord, spvForward<LodOptions>(options)..., int2(1, 0)), ab.x), " |
| 5312 |
"mix(plane1.sample(samp, coord, spvForward<LodOptions>(options)..., int2(0, 1)), " |
| 5313 |
"plane1.sample(samp, coord, spvForward<LodOptions>(options)..., int2(1, 1)), ab.x), ab.y).rg);"); |
| 5314 |
statement("return ycbcr;"); |
| 5315 |
end_scope(); |
| 5316 |
statement(""); |
| 5317 |
break; |
| 5318 |
|
| 5319 |
case SPVFuncImplChromaReconstructLinear420XCositedEvenYMidpoint3Plane: |
| 5320 |
statement("template<typename T, typename... LodOptions>"); |
| 5321 |
statement("inline vec<T, 4> spvChromaReconstructLinear420XCositedEvenYMidpoint(texture2d<T> plane0, " |
| 5322 |
"texture2d<T> plane1, texture2d<T> plane2, sampler samp, float2 coord, LodOptions... options)"); |
| 5323 |
begin_scope(); |
| 5324 |
statement("vec<T, 4> ycbcr = vec<T, 4>(0, 0, 0, 1);"); |
| 5325 |
statement("ycbcr.g = plane0.sample(samp, coord, spvForward<LodOptions>(options)...).r;"); |
| 5326 |
statement("float2 ab = fract((round(coord * float2(plane0.get_width(), plane0.get_height())) - float2(0, " |
| 5327 |
"0.5)) * 0.5);"); |
| 5328 |
statement("ycbcr.b = T(mix(mix(plane1.sample(samp, coord, spvForward<LodOptions>(options)...), " |
| 5329 |
"plane1.sample(samp, coord, spvForward<LodOptions>(options)..., int2(1, 0)), ab.x), " |
| 5330 |
"mix(plane1.sample(samp, coord, spvForward<LodOptions>(options)..., int2(0, 1)), " |
| 5331 |
"plane1.sample(samp, coord, spvForward<LodOptions>(options)..., int2(1, 1)), ab.x), ab.y).r);"); |
| 5332 |
statement("ycbcr.r = T(mix(mix(plane2.sample(samp, coord, spvForward<LodOptions>(options)...), " |
| 5333 |
"plane2.sample(samp, coord, spvForward<LodOptions>(options)..., int2(1, 0)), ab.x), " |
| 5334 |
"mix(plane2.sample(samp, coord, spvForward<LodOptions>(options)..., int2(0, 1)), " |
| 5335 |
"plane2.sample(samp, coord, spvForward<LodOptions>(options)..., int2(1, 1)), ab.x), ab.y).r);"); |
| 5336 |
statement("return ycbcr;"); |
| 5337 |
end_scope(); |
| 5338 |
statement(""); |
| 5339 |
break; |
| 5340 |
|
| 5341 |
case SPVFuncImplChromaReconstructLinear420XMidpointYMidpoint2Plane: |
| 5342 |
statement("template<typename T, typename... LodOptions>"); |
| 5343 |
statement("inline vec<T, 4> spvChromaReconstructLinear420XMidpointYMidpoint(texture2d<T> plane0, " |
| 5344 |
"texture2d<T> plane1, sampler samp, float2 coord, LodOptions... options)"); |
| 5345 |
begin_scope(); |
| 5346 |
statement("vec<T, 4> ycbcr = vec<T, 4>(0, 0, 0, 1);"); |
| 5347 |
statement("ycbcr.g = plane0.sample(samp, coord, spvForward<LodOptions>(options)...).r;"); |
| 5348 |
statement("float2 ab = fract((round(coord * float2(plane0.get_width(), plane0.get_height())) - float2(0.5, " |
| 5349 |
"0.5)) * 0.5);"); |
| 5350 |
statement("ycbcr.br = vec<T, 2>(mix(mix(plane1.sample(samp, coord, spvForward<LodOptions>(options)...), " |
| 5351 |
"plane1.sample(samp, coord, spvForward<LodOptions>(options)..., int2(1, 0)), ab.x), " |
| 5352 |
"mix(plane1.sample(samp, coord, spvForward<LodOptions>(options)..., int2(0, 1)), " |
| 5353 |
"plane1.sample(samp, coord, spvForward<LodOptions>(options)..., int2(1, 1)), ab.x), ab.y).rg);"); |
| 5354 |
statement("return ycbcr;"); |
| 5355 |
end_scope(); |
| 5356 |
statement(""); |
| 5357 |
break; |
| 5358 |
|
| 5359 |
case SPVFuncImplChromaReconstructLinear420XMidpointYMidpoint3Plane: |
| 5360 |
statement("template<typename T, typename... LodOptions>"); |
| 5361 |
statement("inline vec<T, 4> spvChromaReconstructLinear420XMidpointYMidpoint(texture2d<T> plane0, " |
| 5362 |
"texture2d<T> plane1, texture2d<T> plane2, sampler samp, float2 coord, LodOptions... options)"); |
| 5363 |
begin_scope(); |
| 5364 |
statement("vec<T, 4> ycbcr = vec<T, 4>(0, 0, 0, 1);"); |
| 5365 |
statement("ycbcr.g = plane0.sample(samp, coord, spvForward<LodOptions>(options)...).r;"); |
| 5366 |
statement("float2 ab = fract((round(coord * float2(plane0.get_width(), plane0.get_height())) - float2(0.5, " |
| 5367 |
"0.5)) * 0.5);"); |
| 5368 |
statement("ycbcr.b = T(mix(mix(plane1.sample(samp, coord, spvForward<LodOptions>(options)...), " |
| 5369 |
"plane1.sample(samp, coord, spvForward<LodOptions>(options)..., int2(1, 0)), ab.x), " |
| 5370 |
"mix(plane1.sample(samp, coord, spvForward<LodOptions>(options)..., int2(0, 1)), " |
| 5371 |
"plane1.sample(samp, coord, spvForward<LodOptions>(options)..., int2(1, 1)), ab.x), ab.y).r);"); |
| 5372 |
statement("ycbcr.r = T(mix(mix(plane2.sample(samp, coord, spvForward<LodOptions>(options)...), " |
| 5373 |
"plane2.sample(samp, coord, spvForward<LodOptions>(options)..., int2(1, 0)), ab.x), " |
| 5374 |
"mix(plane2.sample(samp, coord, spvForward<LodOptions>(options)..., int2(0, 1)), " |
| 5375 |
"plane2.sample(samp, coord, spvForward<LodOptions>(options)..., int2(1, 1)), ab.x), ab.y).r);"); |
| 5376 |
statement("return ycbcr;"); |
| 5377 |
end_scope(); |
| 5378 |
statement(""); |
| 5379 |
break; |
| 5380 |
|
| 5381 |
case SPVFuncImplExpandITUFullRange: |
| 5382 |
statement("template<typename T>"); |
| 5383 |
statement("inline vec<T, 4> spvExpandITUFullRange(vec<T, 4> ycbcr, int n)"); |
| 5384 |
begin_scope(); |
| 5385 |
statement("ycbcr.br -= exp2(T(n-1))/(exp2(T(n))-1);"); |
| 5386 |
statement("return ycbcr;"); |
| 5387 |
end_scope(); |
| 5388 |
statement(""); |
| 5389 |
break; |
| 5390 |
|
| 5391 |
case SPVFuncImplExpandITUNarrowRange: |
| 5392 |
statement("template<typename T>"); |
| 5393 |
statement("inline vec<T, 4> spvExpandITUNarrowRange(vec<T, 4> ycbcr, int n)"); |
| 5394 |
begin_scope(); |
| 5395 |
statement("ycbcr.g = (ycbcr.g * (exp2(T(n)) - 1) - ldexp(T(16), n - 8))/ldexp(T(219), n - 8);"); |
| 5396 |
statement("ycbcr.br = (ycbcr.br * (exp2(T(n)) - 1) - ldexp(T(128), n - 8))/ldexp(T(224), n - 8);"); |
| 5397 |
statement("return ycbcr;"); |
| 5398 |
end_scope(); |
| 5399 |
statement(""); |
| 5400 |
break; |
| 5401 |
|
| 5402 |
case SPVFuncImplConvertYCbCrBT709: |
| 5403 |
statement("// cf. Khronos Data Format Specification, section 15.1.1"); |
| 5404 |
statement("constant float3x3 spvBT709Factors = {{1, 1, 1}, {0, -0.13397432/0.7152, 1.8556}, {1.5748, " |
| 5405 |
"-0.33480248/0.7152, 0}};"); |
| 5406 |
statement(""); |
| 5407 |
statement("template<typename T>"); |
| 5408 |
statement("inline vec<T, 4> spvConvertYCbCrBT709(vec<T, 4> ycbcr)"); |
| 5409 |
begin_scope(); |
| 5410 |
statement("vec<T, 4> rgba;"); |
| 5411 |
statement("rgba.rgb = vec<T, 3>(spvBT709Factors * ycbcr.gbr);"); |
| 5412 |
statement("rgba.a = ycbcr.a;"); |
| 5413 |
statement("return rgba;"); |
| 5414 |
end_scope(); |
| 5415 |
statement(""); |
| 5416 |
break; |
| 5417 |
|
| 5418 |
case SPVFuncImplConvertYCbCrBT601: |
| 5419 |
statement("// cf. Khronos Data Format Specification, section 15.1.2"); |
| 5420 |
statement("constant float3x3 spvBT601Factors = {{1, 1, 1}, {0, -0.202008/0.587, 1.772}, {1.402, " |
| 5421 |
"-0.419198/0.587, 0}};"); |
| 5422 |
statement(""); |
| 5423 |
statement("template<typename T>"); |
| 5424 |
statement("inline vec<T, 4> spvConvertYCbCrBT601(vec<T, 4> ycbcr)"); |
| 5425 |
begin_scope(); |
| 5426 |
statement("vec<T, 4> rgba;"); |
| 5427 |
statement("rgba.rgb = vec<T, 3>(spvBT601Factors * ycbcr.gbr);"); |
| 5428 |
statement("rgba.a = ycbcr.a;"); |
| 5429 |
statement("return rgba;"); |
| 5430 |
end_scope(); |
| 5431 |
statement(""); |
| 5432 |
break; |
| 5433 |
|
| 5434 |
case SPVFuncImplConvertYCbCrBT2020: |
| 5435 |
statement("// cf. Khronos Data Format Specification, section 15.1.3"); |
| 5436 |
statement("constant float3x3 spvBT2020Factors = {{1, 1, 1}, {0, -0.11156702/0.6780, 1.8814}, {1.4746, " |
| 5437 |
"-0.38737742/0.6780, 0}};"); |
| 5438 |
statement(""); |
| 5439 |
statement("template<typename T>"); |
| 5440 |
statement("inline vec<T, 4> spvConvertYCbCrBT2020(vec<T, 4> ycbcr)"); |
| 5441 |
begin_scope(); |
| 5442 |
statement("vec<T, 4> rgba;"); |
| 5443 |
statement("rgba.rgb = vec<T, 3>(spvBT2020Factors * ycbcr.gbr);"); |
| 5444 |
statement("rgba.a = ycbcr.a;"); |
| 5445 |
statement("return rgba;"); |
| 5446 |
end_scope(); |
| 5447 |
statement(""); |
| 5448 |
break; |
| 5449 |
|
| 5450 |
case SPVFuncImplDynamicImageSampler: |
| 5451 |
statement("enum class spvFormatResolution"); |
| 5452 |
begin_scope(); |
| 5453 |
statement("_444 = 0,"); |
| 5454 |
statement("_422,"); |
| 5455 |
statement("_420"); |
| 5456 |
end_scope_decl(); |
| 5457 |
statement(""); |
| 5458 |
statement("enum class spvChromaFilter"); |
| 5459 |
begin_scope(); |
| 5460 |
statement("nearest = 0,"); |
| 5461 |
statement("linear"); |
| 5462 |
end_scope_decl(); |
| 5463 |
statement(""); |
| 5464 |
statement("enum class spvXChromaLocation"); |
| 5465 |
begin_scope(); |
| 5466 |
statement("cosited_even = 0,"); |
| 5467 |
statement("midpoint"); |
| 5468 |
end_scope_decl(); |
| 5469 |
statement(""); |
| 5470 |
statement("enum class spvYChromaLocation"); |
| 5471 |
begin_scope(); |
| 5472 |
statement("cosited_even = 0,"); |
| 5473 |
statement("midpoint"); |
| 5474 |
end_scope_decl(); |
| 5475 |
statement(""); |
| 5476 |
statement("enum class spvYCbCrModelConversion"); |
| 5477 |
begin_scope(); |
| 5478 |
statement("rgb_identity = 0,"); |
| 5479 |
statement("ycbcr_identity,"); |
| 5480 |
statement("ycbcr_bt_709,"); |
| 5481 |
statement("ycbcr_bt_601,"); |
| 5482 |
statement("ycbcr_bt_2020"); |
| 5483 |
end_scope_decl(); |
| 5484 |
statement(""); |
| 5485 |
statement("enum class spvYCbCrRange"); |
| 5486 |
begin_scope(); |
| 5487 |
statement("itu_full = 0,"); |
| 5488 |
statement("itu_narrow"); |
| 5489 |
end_scope_decl(); |
| 5490 |
statement(""); |
| 5491 |
statement("struct spvComponentBits"); |
| 5492 |
begin_scope(); |
| 5493 |
statement("constexpr explicit spvComponentBits(int v) thread : value(v) {}"); |
| 5494 |
statement("uchar value : 6;"); |
| 5495 |
end_scope_decl(); |
| 5496 |
statement("// A class corresponding to metal::sampler which holds sampler"); |
| 5497 |
statement("// Y'CbCr conversion info."); |
| 5498 |
statement("struct spvYCbCrSampler"); |
| 5499 |
begin_scope(); |
| 5500 |
statement("constexpr spvYCbCrSampler() thread : val(build()) {}"); |
| 5501 |
statement("template<typename... Ts>"); |
| 5502 |
statement("constexpr spvYCbCrSampler(Ts... t) thread : val(build(t...)) {}"); |
| 5503 |
statement("constexpr spvYCbCrSampler(const thread spvYCbCrSampler& s) thread = default;"); |
| 5504 |
statement(""); |
| 5505 |
statement("spvFormatResolution get_resolution() const thread"); |
| 5506 |
begin_scope(); |
| 5507 |
statement("return spvFormatResolution((val & resolution_mask) >> resolution_base);"); |
| 5508 |
end_scope(); |
| 5509 |
statement("spvChromaFilter get_chroma_filter() const thread"); |
| 5510 |
begin_scope(); |
| 5511 |
statement("return spvChromaFilter((val & chroma_filter_mask) >> chroma_filter_base);"); |
| 5512 |
end_scope(); |
| 5513 |
statement("spvXChromaLocation get_x_chroma_offset() const thread"); |
| 5514 |
begin_scope(); |
| 5515 |
statement("return spvXChromaLocation((val & x_chroma_off_mask) >> x_chroma_off_base);"); |
| 5516 |
end_scope(); |
| 5517 |
statement("spvYChromaLocation get_y_chroma_offset() const thread"); |
| 5518 |
begin_scope(); |
| 5519 |
statement("return spvYChromaLocation((val & y_chroma_off_mask) >> y_chroma_off_base);"); |
| 5520 |
end_scope(); |
| 5521 |
statement("spvYCbCrModelConversion get_ycbcr_model() const thread"); |
| 5522 |
begin_scope(); |
| 5523 |
statement("return spvYCbCrModelConversion((val & ycbcr_model_mask) >> ycbcr_model_base);"); |
| 5524 |
end_scope(); |
| 5525 |
statement("spvYCbCrRange get_ycbcr_range() const thread"); |
| 5526 |
begin_scope(); |
| 5527 |
statement("return spvYCbCrRange((val & ycbcr_range_mask) >> ycbcr_range_base);"); |
| 5528 |
end_scope(); |
| 5529 |
statement("int get_bpc() const thread { return (val & bpc_mask) >> bpc_base; }"); |
| 5530 |
statement(""); |
| 5531 |
statement("private:"); |
| 5532 |
statement("ushort val;"); |
| 5533 |
statement(""); |
| 5534 |
statement("constexpr static constant ushort resolution_bits = 2;"); |
| 5535 |
statement("constexpr static constant ushort chroma_filter_bits = 2;"); |
| 5536 |
statement("constexpr static constant ushort x_chroma_off_bit = 1;"); |
| 5537 |
statement("constexpr static constant ushort y_chroma_off_bit = 1;"); |
| 5538 |
statement("constexpr static constant ushort ycbcr_model_bits = 3;"); |
| 5539 |
statement("constexpr static constant ushort ycbcr_range_bit = 1;"); |
| 5540 |
statement("constexpr static constant ushort bpc_bits = 6;"); |
| 5541 |
statement(""); |
| 5542 |
statement("constexpr static constant ushort resolution_base = 0;"); |
| 5543 |
statement("constexpr static constant ushort chroma_filter_base = 2;"); |
| 5544 |
statement("constexpr static constant ushort x_chroma_off_base = 4;"); |
| 5545 |
statement("constexpr static constant ushort y_chroma_off_base = 5;"); |
| 5546 |
statement("constexpr static constant ushort ycbcr_model_base = 6;"); |
| 5547 |
statement("constexpr static constant ushort ycbcr_range_base = 9;"); |
| 5548 |
statement("constexpr static constant ushort bpc_base = 10;"); |
| 5549 |
statement(""); |
| 5550 |
statement( |
| 5551 |
"constexpr static constant ushort resolution_mask = ((1 << resolution_bits) - 1) << resolution_base;"); |
| 5552 |
statement("constexpr static constant ushort chroma_filter_mask = ((1 << chroma_filter_bits) - 1) << " |
| 5553 |
"chroma_filter_base;"); |
| 5554 |
statement("constexpr static constant ushort x_chroma_off_mask = ((1 << x_chroma_off_bit) - 1) << " |
| 5555 |
"x_chroma_off_base;"); |
| 5556 |
statement("constexpr static constant ushort y_chroma_off_mask = ((1 << y_chroma_off_bit) - 1) << " |
| 5557 |
"y_chroma_off_base;"); |
| 5558 |
statement("constexpr static constant ushort ycbcr_model_mask = ((1 << ycbcr_model_bits) - 1) << " |
| 5559 |
"ycbcr_model_base;"); |
| 5560 |
statement("constexpr static constant ushort ycbcr_range_mask = ((1 << ycbcr_range_bit) - 1) << " |
| 5561 |
"ycbcr_range_base;"); |
| 5562 |
statement("constexpr static constant ushort bpc_mask = ((1 << bpc_bits) - 1) << bpc_base;"); |
| 5563 |
statement(""); |
| 5564 |
statement("static constexpr ushort build()"); |
| 5565 |
begin_scope(); |
| 5566 |
statement("return 0;"); |
| 5567 |
end_scope(); |
| 5568 |
statement(""); |
| 5569 |
statement("template<typename... Ts>"); |
| 5570 |
statement("static constexpr ushort build(spvFormatResolution res, Ts... t)"); |
| 5571 |
begin_scope(); |
| 5572 |
statement("return (ushort(res) << resolution_base) | (build(t...) & ~resolution_mask);"); |
| 5573 |
end_scope(); |
| 5574 |
statement(""); |
| 5575 |
statement("template<typename... Ts>"); |
| 5576 |
statement("static constexpr ushort build(spvChromaFilter filt, Ts... t)"); |
| 5577 |
begin_scope(); |
| 5578 |
statement("return (ushort(filt) << chroma_filter_base) | (build(t...) & ~chroma_filter_mask);"); |
| 5579 |
end_scope(); |
| 5580 |
statement(""); |
| 5581 |
statement("template<typename... Ts>"); |
| 5582 |
statement("static constexpr ushort build(spvXChromaLocation loc, Ts... t)"); |
| 5583 |
begin_scope(); |
| 5584 |
statement("return (ushort(loc) << x_chroma_off_base) | (build(t...) & ~x_chroma_off_mask);"); |
| 5585 |
end_scope(); |
| 5586 |
statement(""); |
| 5587 |
statement("template<typename... Ts>"); |
| 5588 |
statement("static constexpr ushort build(spvYChromaLocation loc, Ts... t)"); |
| 5589 |
begin_scope(); |
| 5590 |
statement("return (ushort(loc) << y_chroma_off_base) | (build(t...) & ~y_chroma_off_mask);"); |
| 5591 |
end_scope(); |
| 5592 |
statement(""); |
| 5593 |
statement("template<typename... Ts>"); |
| 5594 |
statement("static constexpr ushort build(spvYCbCrModelConversion model, Ts... t)"); |
| 5595 |
begin_scope(); |
| 5596 |
statement("return (ushort(model) << ycbcr_model_base) | (build(t...) & ~ycbcr_model_mask);"); |
| 5597 |
end_scope(); |
| 5598 |
statement(""); |
| 5599 |
statement("template<typename... Ts>"); |
| 5600 |
statement("static constexpr ushort build(spvYCbCrRange range, Ts... t)"); |
| 5601 |
begin_scope(); |
| 5602 |
statement("return (ushort(range) << ycbcr_range_base) | (build(t...) & ~ycbcr_range_mask);"); |
| 5603 |
end_scope(); |
| 5604 |
statement(""); |
| 5605 |
statement("template<typename... Ts>"); |
| 5606 |
statement("static constexpr ushort build(spvComponentBits bpc, Ts... t)"); |
| 5607 |
begin_scope(); |
| 5608 |
statement("return (ushort(bpc.value) << bpc_base) | (build(t...) & ~bpc_mask);"); |
| 5609 |
end_scope(); |
| 5610 |
end_scope_decl(); |
| 5611 |
statement(""); |
| 5612 |
statement("// A class which can hold up to three textures and a sampler, including"); |
| 5613 |
statement("// Y'CbCr conversion info, used to pass combined image-samplers"); |
| 5614 |
statement("// dynamically to functions."); |
| 5615 |
statement("template<typename T>"); |
| 5616 |
statement("struct spvDynamicImageSampler"); |
| 5617 |
begin_scope(); |
| 5618 |
statement("texture2d<T> plane0;"); |
| 5619 |
statement("texture2d<T> plane1;"); |
| 5620 |
statement("texture2d<T> plane2;"); |
| 5621 |
statement("sampler samp;"); |
| 5622 |
statement("spvYCbCrSampler ycbcr_samp;"); |
| 5623 |
statement("uint swizzle = 0;"); |
| 5624 |
statement(""); |
| 5625 |
if (msl_options.swizzle_texture_samples) |
| 5626 |
{ |
| 5627 |
statement("constexpr spvDynamicImageSampler(texture2d<T> tex, sampler samp, uint sw) thread :"); |
| 5628 |
statement(" plane0(tex), samp(samp), swizzle(sw) {}"); |
| 5629 |
} |
| 5630 |
else |
| 5631 |
{ |
| 5632 |
statement("constexpr spvDynamicImageSampler(texture2d<T> tex, sampler samp) thread :"); |
| 5633 |
statement(" plane0(tex), samp(samp) {}"); |
| 5634 |
} |
| 5635 |
statement("constexpr spvDynamicImageSampler(texture2d<T> tex, sampler samp, spvYCbCrSampler ycbcr_samp, " |
| 5636 |
"uint sw) thread :"); |
| 5637 |
statement(" plane0(tex), samp(samp), ycbcr_samp(ycbcr_samp), swizzle(sw) {}"); |
| 5638 |
statement("constexpr spvDynamicImageSampler(texture2d<T> plane0, texture2d<T> plane1,"); |
| 5639 |
statement(" sampler samp, spvYCbCrSampler ycbcr_samp, uint sw) thread :"); |
| 5640 |
statement(" plane0(plane0), plane1(plane1), samp(samp), ycbcr_samp(ycbcr_samp), swizzle(sw) {}"); |
| 5641 |
statement( |
| 5642 |
"constexpr spvDynamicImageSampler(texture2d<T> plane0, texture2d<T> plane1, texture2d<T> plane2,"); |
| 5643 |
statement(" sampler samp, spvYCbCrSampler ycbcr_samp, uint sw) thread :"); |
| 5644 |
statement(" plane0(plane0), plane1(plane1), plane2(plane2), samp(samp), ycbcr_samp(ycbcr_samp), " |
| 5645 |
"swizzle(sw) {}"); |
| 5646 |
statement(""); |
| 5647 |
// XXX This is really hard to follow... I've left comments to make it a bit easier. |
| 5648 |
statement("template<typename... LodOptions>"); |
| 5649 |
statement("vec<T, 4> do_sample(float2 coord, LodOptions... options) const thread"); |
| 5650 |
begin_scope(); |
| 5651 |
statement("if (!is_null_texture(plane1))"); |
| 5652 |
begin_scope(); |
| 5653 |
statement("if (ycbcr_samp.get_resolution() == spvFormatResolution::_444 ||"); |
| 5654 |
statement(" ycbcr_samp.get_chroma_filter() == spvChromaFilter::nearest)"); |
| 5655 |
begin_scope(); |
| 5656 |
statement("if (!is_null_texture(plane2))"); |
| 5657 |
statement(" return spvChromaReconstructNearest(plane0, plane1, plane2, samp, coord,"); |
| 5658 |
statement(" spvForward<LodOptions>(options)...);"); |
| 5659 |
statement( |
| 5660 |
"return spvChromaReconstructNearest(plane0, plane1, samp, coord, spvForward<LodOptions>(options)...);"); |
| 5661 |
end_scope(); // if (resolution == 422 || chroma_filter == nearest) |
| 5662 |
statement("switch (ycbcr_samp.get_resolution())"); |
| 5663 |
begin_scope(); |
| 5664 |
statement("case spvFormatResolution::_444: break;"); |
| 5665 |
statement("case spvFormatResolution::_422:"); |
| 5666 |
begin_scope(); |
| 5667 |
statement("switch (ycbcr_samp.get_x_chroma_offset())"); |
| 5668 |
begin_scope(); |
| 5669 |
statement("case spvXChromaLocation::cosited_even:"); |
| 5670 |
statement(" if (!is_null_texture(plane2))"); |
| 5671 |
statement(" return spvChromaReconstructLinear422CositedEven("); |
| 5672 |
statement(" plane0, plane1, plane2, samp,"); |
| 5673 |
statement(" coord, spvForward<LodOptions>(options)...);"); |
| 5674 |
statement(" return spvChromaReconstructLinear422CositedEven("); |
| 5675 |
statement(" plane0, plane1, samp, coord,"); |
| 5676 |
statement(" spvForward<LodOptions>(options)...);"); |
| 5677 |
statement("case spvXChromaLocation::midpoint:"); |
| 5678 |
statement(" if (!is_null_texture(plane2))"); |
| 5679 |
statement(" return spvChromaReconstructLinear422Midpoint("); |
| 5680 |
statement(" plane0, plane1, plane2, samp,"); |
| 5681 |
statement(" coord, spvForward<LodOptions>(options)...);"); |
| 5682 |
statement(" return spvChromaReconstructLinear422Midpoint("); |
| 5683 |
statement(" plane0, plane1, samp, coord,"); |
| 5684 |
statement(" spvForward<LodOptions>(options)...);"); |
| 5685 |
end_scope(); // switch (x_chroma_offset) |
| 5686 |
end_scope(); // case 422: |
| 5687 |
statement("case spvFormatResolution::_420:"); |
| 5688 |
begin_scope(); |
| 5689 |
statement("switch (ycbcr_samp.get_x_chroma_offset())"); |
| 5690 |
begin_scope(); |
| 5691 |
statement("case spvXChromaLocation::cosited_even:"); |
| 5692 |
begin_scope(); |
| 5693 |
statement("switch (ycbcr_samp.get_y_chroma_offset())"); |
| 5694 |
begin_scope(); |
| 5695 |
statement("case spvYChromaLocation::cosited_even:"); |
| 5696 |
statement(" if (!is_null_texture(plane2))"); |
| 5697 |
statement(" return spvChromaReconstructLinear420XCositedEvenYCositedEven("); |
| 5698 |
statement(" plane0, plane1, plane2, samp,"); |
| 5699 |
statement(" coord, spvForward<LodOptions>(options)...);"); |
| 5700 |
statement(" return spvChromaReconstructLinear420XCositedEvenYCositedEven("); |
| 5701 |
statement(" plane0, plane1, samp, coord,"); |
| 5702 |
statement(" spvForward<LodOptions>(options)...);"); |
| 5703 |
statement("case spvYChromaLocation::midpoint:"); |
| 5704 |
statement(" if (!is_null_texture(plane2))"); |
| 5705 |
statement(" return spvChromaReconstructLinear420XCositedEvenYMidpoint("); |
| 5706 |
statement(" plane0, plane1, plane2, samp,"); |
| 5707 |
statement(" coord, spvForward<LodOptions>(options)...);"); |
| 5708 |
statement(" return spvChromaReconstructLinear420XCositedEvenYMidpoint("); |
| 5709 |
statement(" plane0, plane1, samp, coord,"); |
| 5710 |
statement(" spvForward<LodOptions>(options)...);"); |
| 5711 |
end_scope(); // switch (y_chroma_offset) |
| 5712 |
end_scope(); // case x::cosited_even: |
| 5713 |
statement("case spvXChromaLocation::midpoint:"); |
| 5714 |
begin_scope(); |
| 5715 |
statement("switch (ycbcr_samp.get_y_chroma_offset())"); |
| 5716 |
begin_scope(); |
| 5717 |
statement("case spvYChromaLocation::cosited_even:"); |
| 5718 |
statement(" if (!is_null_texture(plane2))"); |
| 5719 |
statement(" return spvChromaReconstructLinear420XMidpointYCositedEven("); |
| 5720 |
statement(" plane0, plane1, plane2, samp,"); |
| 5721 |
statement(" coord, spvForward<LodOptions>(options)...);"); |
| 5722 |
statement(" return spvChromaReconstructLinear420XMidpointYCositedEven("); |
| 5723 |
statement(" plane0, plane1, samp, coord,"); |
| 5724 |
statement(" spvForward<LodOptions>(options)...);"); |
| 5725 |
statement("case spvYChromaLocation::midpoint:"); |
| 5726 |
statement(" if (!is_null_texture(plane2))"); |
| 5727 |
statement(" return spvChromaReconstructLinear420XMidpointYMidpoint("); |
| 5728 |
statement(" plane0, plane1, plane2, samp,"); |
| 5729 |
statement(" coord, spvForward<LodOptions>(options)...);"); |
| 5730 |
statement(" return spvChromaReconstructLinear420XMidpointYMidpoint("); |
| 5731 |
statement(" plane0, plane1, samp, coord,"); |
| 5732 |
statement(" spvForward<LodOptions>(options)...);"); |
| 5733 |
end_scope(); // switch (y_chroma_offset) |
| 5734 |
end_scope(); // case x::midpoint |
| 5735 |
end_scope(); // switch (x_chroma_offset) |
| 5736 |
end_scope(); // case 420: |
| 5737 |
end_scope(); // switch (resolution) |
| 5738 |
end_scope(); // if (multiplanar) |
| 5739 |
statement("return plane0.sample(samp, coord, spvForward<LodOptions>(options)...);"); |
| 5740 |
end_scope(); // do_sample() |
| 5741 |
statement("template <typename... LodOptions>"); |
| 5742 |
statement("vec<T, 4> sample(float2 coord, LodOptions... options) const thread"); |
| 5743 |
begin_scope(); |
| 5744 |
statement( |
| 5745 |
"vec<T, 4> s = spvTextureSwizzle(do_sample(coord, spvForward<LodOptions>(options)...), swizzle);"); |
| 5746 |
statement("if (ycbcr_samp.get_ycbcr_model() == spvYCbCrModelConversion::rgb_identity)"); |
| 5747 |
statement(" return s;"); |
| 5748 |
statement(""); |
| 5749 |
statement("switch (ycbcr_samp.get_ycbcr_range())"); |
| 5750 |
begin_scope(); |
| 5751 |
statement("case spvYCbCrRange::itu_full:"); |
| 5752 |
statement(" s = spvExpandITUFullRange(s, ycbcr_samp.get_bpc());"); |
| 5753 |
statement(" break;"); |
| 5754 |
statement("case spvYCbCrRange::itu_narrow:"); |
| 5755 |
statement(" s = spvExpandITUNarrowRange(s, ycbcr_samp.get_bpc());"); |
| 5756 |
statement(" break;"); |
| 5757 |
end_scope(); |
| 5758 |
statement(""); |
| 5759 |
statement("switch (ycbcr_samp.get_ycbcr_model())"); |
| 5760 |
begin_scope(); |
| 5761 |
statement("case spvYCbCrModelConversion::rgb_identity:"); // Silence Clang warning |
| 5762 |
statement("case spvYCbCrModelConversion::ycbcr_identity:"); |
| 5763 |
statement(" return s;"); |
| 5764 |
statement("case spvYCbCrModelConversion::ycbcr_bt_709:"); |
| 5765 |
statement(" return spvConvertYCbCrBT709(s);"); |
| 5766 |
statement("case spvYCbCrModelConversion::ycbcr_bt_601:"); |
| 5767 |
statement(" return spvConvertYCbCrBT601(s);"); |
| 5768 |
statement("case spvYCbCrModelConversion::ycbcr_bt_2020:"); |
| 5769 |
statement(" return spvConvertYCbCrBT2020(s);"); |
| 5770 |
end_scope(); |
| 5771 |
end_scope(); |
| 5772 |
statement(""); |
| 5773 |
// Sampler Y'CbCr conversion forbids offsets. |
| 5774 |
statement("vec<T, 4> sample(float2 coord, int2 offset) const thread"); |
| 5775 |
begin_scope(); |
| 5776 |
if (msl_options.swizzle_texture_samples) |
| 5777 |
statement("return spvTextureSwizzle(plane0.sample(samp, coord, offset), swizzle);"); |
| 5778 |
else |
| 5779 |
statement("return plane0.sample(samp, coord, offset);"); |
| 5780 |
end_scope(); |
| 5781 |
statement("template<typename lod_options>"); |
| 5782 |
statement("vec<T, 4> sample(float2 coord, lod_options options, int2 offset) const thread"); |
| 5783 |
begin_scope(); |
| 5784 |
if (msl_options.swizzle_texture_samples) |
| 5785 |
statement("return spvTextureSwizzle(plane0.sample(samp, coord, options, offset), swizzle);"); |
| 5786 |
else |
| 5787 |
statement("return plane0.sample(samp, coord, options, offset);"); |
| 5788 |
end_scope(); |
| 5789 |
statement("#if __HAVE_MIN_LOD_CLAMP__"); |
| 5790 |
statement("vec<T, 4> sample(float2 coord, bias b, min_lod_clamp min_lod, int2 offset) const thread"); |
| 5791 |
begin_scope(); |
| 5792 |
statement("return plane0.sample(samp, coord, b, min_lod, offset);"); |
| 5793 |
end_scope(); |
| 5794 |
statement( |
| 5795 |
"vec<T, 4> sample(float2 coord, gradient2d grad, min_lod_clamp min_lod, int2 offset) const thread"); |
| 5796 |
begin_scope(); |
| 5797 |
statement("return plane0.sample(samp, coord, grad, min_lod, offset);"); |
| 5798 |
end_scope(); |
| 5799 |
statement("#endif"); |
| 5800 |
statement(""); |
| 5801 |
// Y'CbCr conversion forbids all operations but sampling. |
| 5802 |
statement("vec<T, 4> read(uint2 coord, uint lod = 0) const thread"); |
| 5803 |
begin_scope(); |
| 5804 |
statement("return plane0.read(coord, lod);"); |
| 5805 |
end_scope(); |
| 5806 |
statement(""); |
| 5807 |
statement("vec<T, 4> gather(float2 coord, int2 offset = int2(0), component c = component::x) const thread"); |
| 5808 |
begin_scope(); |
| 5809 |
if (msl_options.swizzle_texture_samples) |
| 5810 |
statement("return spvGatherSwizzle(plane0, samp, swizzle, c, coord, offset);"); |
| 5811 |
else |
| 5812 |
statement("return plane0.gather(samp, coord, offset, c);"); |
| 5813 |
end_scope(); |
| 5814 |
end_scope_decl(); |
| 5815 |
statement(""); |
| 5816 |
|
| 5817 |
default: |
| 5818 |
break; |
| 5819 |
} |
| 5820 |
} |
| 5821 |
} |
| 5822 |
|
| 5823 |
// Undefined global memory is not allowed in MSL. |
| 5824 |
// Declare constant and init to zeros. Use {}, as global constructors can break Metal. |
| 5825 |
void CompilerMSL::declare_undefined_values() |
| 5826 |
{ |
| 5827 |
bool emitted = false; |
| 5828 |
ir.for_each_typed_id<SPIRUndef>([&](uint32_t, SPIRUndef &undef) { |
| 5829 |
auto &type = this->get<SPIRType>(undef.basetype); |
| 5830 |
// OpUndef can be void for some reason ... |
| 5831 |
if (type.basetype == SPIRType::Void) |
| 5832 |
return; |
| 5833 |
|
| 5834 |
statement("constant ", variable_decl(type, to_name(undef.self), undef.self), " = {};"); |
| 5835 |
emitted = true; |
| 5836 |
}); |
| 5837 |
|
| 5838 |
if (emitted) |
| 5839 |
statement(""); |
| 5840 |
} |
| 5841 |
|
| 5842 |
void CompilerMSL::declare_constant_arrays() |
| 5843 |
{ |
| 5844 |
bool fully_inlined = ir.ids_for_type[TypeFunction].size() == 1; |
| 5845 |
|
| 5846 |
// MSL cannot declare arrays inline (except when declaring a variable), so we must move them out to |
| 5847 |
// global constants directly, so we are able to use constants as variable expressions. |
| 5848 |
bool emitted = false; |
| 5849 |
|
| 5850 |
ir.for_each_typed_id<SPIRConstant>([&](uint32_t, SPIRConstant &c) { |
| 5851 |
if (c.specialization) |
| 5852 |
return; |
| 5853 |
|
| 5854 |
auto &type = this->get<SPIRType>(c.constant_type); |
| 5855 |
// Constant arrays of non-primitive types (i.e. matrices) won't link properly into Metal libraries. |
| 5856 |
// FIXME: However, hoisting constants to main() means we need to pass down constant arrays to leaf functions if they are used there. |
| 5857 |
// If there are multiple functions in the module, drop this case to avoid breaking use cases which do not need to |
| 5858 |
// link into Metal libraries. This is hacky. |
| 5859 |
if (!type.array.empty() && (!fully_inlined || is_scalar(type) || is_vector(type))) |
| 5860 |
{ |
| 5861 |
auto name = to_name(c.self); |
| 5862 |
statement("constant ", variable_decl(type, name), " = ", constant_expression(c), ";"); |
| 5863 |
emitted = true; |
| 5864 |
} |
| 5865 |
}); |
| 5866 |
|
| 5867 |
if (emitted) |
| 5868 |
statement(""); |
| 5869 |
} |
| 5870 |
|
| 5871 |
// Constant arrays of non-primitive types (i.e. matrices) won't link properly into Metal libraries |
| 5872 |
void CompilerMSL::declare_complex_constant_arrays() |
| 5873 |
{ |
| 5874 |
// If we do not have a fully inlined module, we did not opt in to |
| 5875 |
// declaring constant arrays of complex types. See CompilerMSL::declare_constant_arrays(). |
| 5876 |
bool fully_inlined = ir.ids_for_type[TypeFunction].size() == 1; |
| 5877 |
if (!fully_inlined) |
| 5878 |
return; |
| 5879 |
|
| 5880 |
// MSL cannot declare arrays inline (except when declaring a variable), so we must move them out to |
| 5881 |
// global constants directly, so we are able to use constants as variable expressions. |
| 5882 |
bool emitted = false; |
| 5883 |
|
| 5884 |
ir.for_each_typed_id<SPIRConstant>([&](uint32_t, SPIRConstant &c) { |
| 5885 |
if (c.specialization) |
| 5886 |
return; |
| 5887 |
|
| 5888 |
auto &type = this->get<SPIRType>(c.constant_type); |
| 5889 |
if (!type.array.empty() && !(is_scalar(type) || is_vector(type))) |
| 5890 |
{ |
| 5891 |
auto name = to_name(c.self); |
| 5892 |
statement("", variable_decl(type, name), " = ", constant_expression(c), ";"); |
| 5893 |
emitted = true; |
| 5894 |
} |
| 5895 |
}); |
| 5896 |
|
| 5897 |
if (emitted) |
| 5898 |
statement(""); |
| 5899 |
} |
| 5900 |
|
| 5901 |
void CompilerMSL::emit_resources() |
| 5902 |
{ |
| 5903 |
declare_constant_arrays(); |
| 5904 |
declare_undefined_values(); |
| 5905 |
|
| 5906 |
// Emit the special [[stage_in]] and [[stage_out]] interface blocks which we created. |
| 5907 |
emit_interface_block(stage_out_var_id); |
| 5908 |
emit_interface_block(patch_stage_out_var_id); |
| 5909 |
emit_interface_block(stage_in_var_id); |
| 5910 |
emit_interface_block(patch_stage_in_var_id); |
| 5911 |
} |
| 5912 |
|
| 5913 |
// Emit declarations for the specialization Metal function constants |
| 5914 |
void CompilerMSL::emit_specialization_constants_and_structs() |
| 5915 |
{ |
| 5916 |
SpecializationConstant wg_x, wg_y, wg_z; |
| 5917 |
ID workgroup_size_id = get_work_group_size_specialization_constants(wg_x, wg_y, wg_z); |
| 5918 |
bool emitted = false; |
| 5919 |
|
| 5920 |
unordered_set<uint32_t> declared_structs; |
| 5921 |
unordered_set<uint32_t> aligned_structs; |
| 5922 |
|
| 5923 |
// First, we need to deal with scalar block layout. |
| 5924 |
// 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. |
| 5925 |
// In that case, if such a case exists for a struct, we must force that all elements of the struct become packed_ types. |
| 5926 |
// This makes the struct alignment as small as physically possible. |
| 5927 |
// When we actually align the struct later, we can insert padding as necessary to make the packed members behave like normally aligned types. |
| 5928 |
ir.for_each_typed_id<SPIRType>([&](uint32_t type_id, const SPIRType &type) { |
| 5929 |
if (type.basetype == SPIRType::Struct && |
| 5930 |
has_extended_decoration(type_id, SPIRVCrossDecorationBufferBlockRepacked)) |
| 5931 |
mark_scalar_layout_structs(type); |
| 5932 |
}); |
| 5933 |
|
| 5934 |
// Very particular use of the soft loop lock. |
| 5935 |
// align_struct may need to create custom types on the fly, but we don't care about |
| 5936 |
// these types for purpose of iterating over them in ir.ids_for_type and friends. |
| 5937 |
auto loop_lock = ir.create_loop_soft_lock(); |
| 5938 |
|
| 5939 |
for (auto &id_ : ir.ids_for_constant_or_type) |
| 5940 |
{ |
| 5941 |
auto &id = ir.ids[id_]; |
| 5942 |
|
| 5943 |
if (id.get_type() == TypeConstant) |
| 5944 |
{ |
| 5945 |
auto &c = id.get<SPIRConstant>(); |
| 5946 |
|
| 5947 |
if (c.self == workgroup_size_id) |
| 5948 |
{ |
| 5949 |
// TODO: This can be expressed as a [[threads_per_threadgroup]] input semantic, but we need to know |
| 5950 |
// the work group size at compile time in SPIR-V, and [[threads_per_threadgroup]] would need to be passed around as a global. |
| 5951 |
// The work group size may be a specialization constant. |
| 5952 |
statement("constant uint3 ", builtin_to_glsl(BuiltInWorkgroupSize, StorageClassWorkgroup), |
| 5953 |
" [[maybe_unused]] = ", constant_expression(get<SPIRConstant>(workgroup_size_id)), ";"); |
| 5954 |
emitted = true; |
| 5955 |
} |
| 5956 |
else if (c.specialization) |
| 5957 |
{ |
| 5958 |
auto &type = get<SPIRType>(c.constant_type); |
| 5959 |
string sc_type_name = type_to_glsl(type); |
| 5960 |
string sc_name = to_name(c.self); |
| 5961 |
string sc_tmp_name = sc_name + "_tmp"; |
| 5962 |
|
| 5963 |
// Function constants are only supported in MSL 1.2 and later. |
| 5964 |
// If we don't support it just declare the "default" directly. |
| 5965 |
// This "default" value can be overridden to the true specialization constant by the API user. |
| 5966 |
// Specialization constants which are used as array length expressions cannot be function constants in MSL, |
| 5967 |
// so just fall back to macros. |
| 5968 |
if (msl_options.supports_msl_version(1, 2) && has_decoration(c.self, DecorationSpecId) && |
| 5969 |
!c.is_used_as_array_length) |
| 5970 |
{ |
| 5971 |
uint32_t constant_id = get_decoration(c.self, DecorationSpecId); |
| 5972 |
// Only scalar, non-composite values can be function constants. |
| 5973 |
statement("constant ", sc_type_name, " ", sc_tmp_name, " [[function_constant(", constant_id, |
| 5974 |
")]];"); |
| 5975 |
statement("constant ", sc_type_name, " ", sc_name, " = is_function_constant_defined(", sc_tmp_name, |
| 5976 |
") ? ", sc_tmp_name, " : ", constant_expression(c), ";"); |
| 5977 |
} |
| 5978 |
else if (has_decoration(c.self, DecorationSpecId)) |
| 5979 |
{ |
| 5980 |
// Fallback to macro overrides. |
| 5981 |
c.specialization_constant_macro_name = |
| 5982 |
constant_value_macro_name(get_decoration(c.self, DecorationSpecId)); |
| 5983 |
|
| 5984 |
statement("#ifndef ", c.specialization_constant_macro_name); |
| 5985 |
statement("#define ", c.specialization_constant_macro_name, " ", constant_expression(c)); |
| 5986 |
statement("#endif"); |
| 5987 |
statement("constant ", sc_type_name, " ", sc_name, " = ", c.specialization_constant_macro_name, |
| 5988 |
";"); |
| 5989 |
} |
| 5990 |
else |
| 5991 |
{ |
| 5992 |
// Composite specialization constants must be built from other specialization constants. |
| 5993 |
statement("constant ", sc_type_name, " ", sc_name, " = ", constant_expression(c), ";"); |
| 5994 |
} |
| 5995 |
emitted = true; |
| 5996 |
} |
| 5997 |
} |
| 5998 |
else if (id.get_type() == TypeConstantOp) |
| 5999 |
{ |
| 6000 |
auto &c = id.get<SPIRConstantOp>(); |
| 6001 |
auto &type = get<SPIRType>(c.basetype); |
| 6002 |
auto name = to_name(c.self); |
| 6003 |
statement("constant ", variable_decl(type, name), " = ", constant_op_expression(c), ";"); |
| 6004 |
emitted = true; |
| 6005 |
} |
| 6006 |
else if (id.get_type() == TypeType) |
| 6007 |
{ |
| 6008 |
// Output non-builtin interface structs. These include local function structs |
| 6009 |
// and structs nested within uniform and read-write buffers. |
| 6010 |
auto &type = id.get<SPIRType>(); |
| 6011 |
TypeID type_id = type.self; |
| 6012 |
|
| 6013 |
bool is_struct = (type.basetype == SPIRType::Struct) && type.array.empty() && !type.pointer; |
| 6014 |
bool is_block = |
| 6015 |
has_decoration(type.self, DecorationBlock) || has_decoration(type.self, DecorationBufferBlock); |
| 6016 |
|
| 6017 |
bool is_builtin_block = is_block && is_builtin_type(type); |
| 6018 |
bool is_declarable_struct = is_struct && !is_builtin_block; |
| 6019 |
|
| 6020 |
// We'll declare this later. |
| 6021 |
if (stage_out_var_id && get_stage_out_struct_type().self == type_id) |
| 6022 |
is_declarable_struct = false; |
| 6023 |
if (patch_stage_out_var_id && get_patch_stage_out_struct_type().self == type_id) |
| 6024 |
is_declarable_struct = false; |
| 6025 |
if (stage_in_var_id && get_stage_in_struct_type().self == type_id) |
| 6026 |
is_declarable_struct = false; |
| 6027 |
if (patch_stage_in_var_id && get_patch_stage_in_struct_type().self == type_id) |
| 6028 |
is_declarable_struct = false; |
| 6029 |
|
| 6030 |
// Align and emit declarable structs...but avoid declaring each more than once. |
| 6031 |
if (is_declarable_struct && declared_structs.count(type_id) == 0) |
| 6032 |
{ |
| 6033 |
if (emitted) |
| 6034 |
statement(""); |
| 6035 |
emitted = false; |
| 6036 |
|
| 6037 |
declared_structs.insert(type_id); |
| 6038 |
|
| 6039 |
if (has_extended_decoration(type_id, SPIRVCrossDecorationBufferBlockRepacked)) |
| 6040 |
align_struct(type, aligned_structs); |
| 6041 |
|
| 6042 |
// Make sure we declare the underlying struct type, and not the "decorated" type with pointers, etc. |
| 6043 |
emit_struct(get<SPIRType>(type_id)); |
| 6044 |
} |
| 6045 |
} |
| 6046 |
} |
| 6047 |
|
| 6048 |
if (emitted) |
| 6049 |
statement(""); |
| 6050 |
} |
| 6051 |
|
| 6052 |
void CompilerMSL::emit_binary_unord_op(uint32_t result_type, uint32_t result_id, uint32_t op0, uint32_t op1, |
| 6053 |
const char *op) |
| 6054 |
{ |
| 6055 |
bool forward = should_forward(op0) && should_forward(op1); |
| 6056 |
emit_op(result_type, result_id, |
| 6057 |
join("(isunordered(", to_enclosed_unpacked_expression(op0), ", ", to_enclosed_unpacked_expression(op1), |
| 6058 |
") || ", to_enclosed_unpacked_expression(op0), " ", op, " ", to_enclosed_unpacked_expression(op1), |
| 6059 |
")"), |
| 6060 |
forward); |
| 6061 |
|
| 6062 |
inherit_expression_dependencies(result_id, op0); |
| 6063 |
inherit_expression_dependencies(result_id, op1); |
| 6064 |
} |
| 6065 |
|
| 6066 |
bool CompilerMSL::emit_tessellation_io_load(uint32_t result_type_id, uint32_t id, uint32_t ptr) |
| 6067 |
{ |
| 6068 |
auto &ptr_type = expression_type(ptr); |
| 6069 |
auto &result_type = get<SPIRType>(result_type_id); |
| 6070 |
if (ptr_type.storage != StorageClassInput && ptr_type.storage != StorageClassOutput) |
| 6071 |
return false; |
| 6072 |
if (ptr_type.storage == StorageClassOutput && get_execution_model() == ExecutionModelTessellationEvaluation) |
| 6073 |
return false; |
| 6074 |
|
| 6075 |
bool multi_patch_tess_ctl = get_execution_model() == ExecutionModelTessellationControl && |
| 6076 |
msl_options.multi_patch_workgroup && ptr_type.storage == StorageClassInput; |
| 6077 |
bool flat_matrix = is_matrix(result_type) && ptr_type.storage == StorageClassInput && !multi_patch_tess_ctl; |
| 6078 |
bool flat_struct = result_type.basetype == SPIRType::Struct && ptr_type.storage == StorageClassInput; |
| 6079 |
bool flat_data_type = flat_matrix || is_array(result_type) || flat_struct; |
| 6080 |
if (!flat_data_type) |
| 6081 |
return false; |
| 6082 |
|
| 6083 |
if (has_decoration(ptr, DecorationPatch)) |
| 6084 |
return false; |
| 6085 |
|
| 6086 |
// Now, we must unflatten a composite type and take care of interleaving array access with gl_in/gl_out. |
| 6087 |
// Lots of painful code duplication since we *really* should not unroll these kinds of loads in entry point fixup |
| 6088 |
// unless we're forced to do this when the code is emitting inoptimal OpLoads. |
| 6089 |
string expr; |
| 6090 |
|
| 6091 |
uint32_t interface_index = get_extended_decoration(ptr, SPIRVCrossDecorationInterfaceMemberIndex); |
| 6092 |
auto *var = maybe_get_backing_variable(ptr); |
| 6093 |
bool ptr_is_io_variable = ir.ids[ptr].get_type() == TypeVariable; |
| 6094 |
auto &expr_type = get_pointee_type(ptr_type.self); |
| 6095 |
|
| 6096 |
const auto &iface_type = expression_type(stage_in_ptr_var_id); |
| 6097 |
|
| 6098 |
if (result_type.array.size() > 2) |
| 6099 |
{ |
| 6100 |
SPIRV_CROSS_THROW("Cannot load tessellation IO variables with more than 2 dimensions."); |
| 6101 |
} |
| 6102 |
else if (result_type.array.size() == 2) |
| 6103 |
{ |
| 6104 |
if (!ptr_is_io_variable) |
| 6105 |
SPIRV_CROSS_THROW("Loading an array-of-array must be loaded directly from an IO variable."); |
| 6106 |
if (interface_index == uint32_t(-1)) |
| 6107 |
SPIRV_CROSS_THROW("Interface index is unknown. Cannot continue."); |
| 6108 |
if (result_type.basetype == SPIRType::Struct || flat_matrix) |
| 6109 |
SPIRV_CROSS_THROW("Cannot load array-of-array of composite type in tessellation IO."); |
| 6110 |
|
| 6111 |
expr += type_to_glsl(result_type) + "({ "; |
| 6112 |
uint32_t num_control_points = to_array_size_literal(result_type, 1); |
| 6113 |
uint32_t base_interface_index = interface_index; |
| 6114 |
|
| 6115 |
auto &sub_type = get<SPIRType>(result_type.parent_type); |
| 6116 |
|
| 6117 |
for (uint32_t i = 0; i < num_control_points; i++) |
| 6118 |
{ |
| 6119 |
expr += type_to_glsl(sub_type) + "({ "; |
| 6120 |
interface_index = base_interface_index; |
| 6121 |
uint32_t array_size = to_array_size_literal(result_type, 0); |
| 6122 |
if (multi_patch_tess_ctl) |
| 6123 |
{ |
| 6124 |
for (uint32_t j = 0; j < array_size; j++) |
| 6125 |
{ |
| 6126 |
const uint32_t indices[3] = { i, interface_index, j }; |
| 6127 |
|
| 6128 |
AccessChainMeta meta; |
| 6129 |
expr += |
| 6130 |
access_chain_internal(stage_in_ptr_var_id, indices, 3, |
| 6131 |
ACCESS_CHAIN_INDEX_IS_LITERAL_BIT | ACCESS_CHAIN_PTR_CHAIN_BIT, &meta); |
| 6132 |
// If the expression has more vector components than the result type, insert |
| 6133 |
// a swizzle. This shouldn't happen normally on valid SPIR-V, but it might |
| 6134 |
// happen if we replace the type of an input variable. |
| 6135 |
if (!is_matrix(sub_type) && sub_type.basetype != SPIRType::Struct && |
| 6136 |
expr_type.vecsize > sub_type.vecsize) |
| 6137 |
expr += vector_swizzle(sub_type.vecsize, 0); |
| 6138 |
|
| 6139 |
if (j + 1 < array_size) |
| 6140 |
expr += ", "; |
| 6141 |
} |
| 6142 |
} |
| 6143 |
else |
| 6144 |
{ |
| 6145 |
for (uint32_t j = 0; j < array_size; j++, interface_index++) |
| 6146 |
{ |
| 6147 |
const uint32_t indices[2] = { i, interface_index }; |
| 6148 |
|
| 6149 |
AccessChainMeta meta; |
| 6150 |
expr += |
| 6151 |
access_chain_internal(stage_in_ptr_var_id, indices, 2, |
| 6152 |
ACCESS_CHAIN_INDEX_IS_LITERAL_BIT | ACCESS_CHAIN_PTR_CHAIN_BIT, &meta); |
| 6153 |
if (!is_matrix(sub_type) && sub_type.basetype != SPIRType::Struct && |
| 6154 |
expr_type.vecsize > sub_type.vecsize) |
| 6155 |
expr += vector_swizzle(sub_type.vecsize, 0); |
| 6156 |
|
| 6157 |
if (j + 1 < array_size) |
| 6158 |
expr += ", "; |
| 6159 |
} |
| 6160 |
} |
| 6161 |
expr += " })"; |
| 6162 |
if (i + 1 < num_control_points) |
| 6163 |
expr += ", "; |
| 6164 |
} |
| 6165 |
expr += " })"; |
| 6166 |
} |
| 6167 |
else if (flat_struct) |
| 6168 |
{ |
| 6169 |
bool is_array_of_struct = is_array(result_type); |
| 6170 |
if (is_array_of_struct && !ptr_is_io_variable) |
| 6171 |
SPIRV_CROSS_THROW("Loading array of struct from IO variable must come directly from IO variable."); |
| 6172 |
|
| 6173 |
uint32_t num_control_points = 1; |
| 6174 |
if (is_array_of_struct) |
| 6175 |
{ |
| 6176 |
num_control_points = to_array_size_literal(result_type, 0); |
| 6177 |
expr += type_to_glsl(result_type) + "({ "; |
| 6178 |
} |
| 6179 |
|
| 6180 |
auto &struct_type = is_array_of_struct ? get<SPIRType>(result_type.parent_type) : result_type; |
| 6181 |
assert(struct_type.array.empty()); |
| 6182 |
|
| 6183 |
for (uint32_t i = 0; i < num_control_points; i++) |
| 6184 |
{ |
| 6185 |
expr += type_to_glsl(struct_type) + "{ "; |
| 6186 |
for (uint32_t j = 0; j < uint32_t(struct_type.member_types.size()); j++) |
| 6187 |
{ |
| 6188 |
// The base interface index is stored per variable for structs. |
| 6189 |
if (var) |
| 6190 |
{ |
| 6191 |
interface_index = |
| 6192 |
get_extended_member_decoration(var->self, j, SPIRVCrossDecorationInterfaceMemberIndex); |
| 6193 |
} |
| 6194 |
|
| 6195 |
if (interface_index == uint32_t(-1)) |
| 6196 |
SPIRV_CROSS_THROW("Interface index is unknown. Cannot continue."); |
| 6197 |
|
| 6198 |
const auto &mbr_type = get<SPIRType>(struct_type.member_types[j]); |
| 6199 |
const auto &expr_mbr_type = get<SPIRType>(expr_type.member_types[j]); |
| 6200 |
if (is_matrix(mbr_type) && ptr_type.storage == StorageClassInput && !multi_patch_tess_ctl) |
| 6201 |
{ |
| 6202 |
expr += type_to_glsl(mbr_type) + "("; |
| 6203 |
for (uint32_t k = 0; k < mbr_type.columns; k++, interface_index++) |
| 6204 |
{ |
| 6205 |
if (is_array_of_struct) |
| 6206 |
{ |
| 6207 |
const uint32_t indices[2] = { i, interface_index }; |
| 6208 |
AccessChainMeta meta; |
| 6209 |
expr += access_chain_internal( |
| 6210 |
stage_in_ptr_var_id, indices, 2, |
| 6211 |
ACCESS_CHAIN_INDEX_IS_LITERAL_BIT | ACCESS_CHAIN_PTR_CHAIN_BIT, &meta); |
| 6212 |
} |
| 6213 |
else |
| 6214 |
expr += to_expression(ptr) + "." + to_member_name(iface_type, interface_index); |
| 6215 |
if (expr_mbr_type.vecsize > mbr_type.vecsize) |
| 6216 |
expr += vector_swizzle(mbr_type.vecsize, 0); |
| 6217 |
|
| 6218 |
if (k + 1 < mbr_type.columns) |
| 6219 |
expr += ", "; |
| 6220 |
} |
| 6221 |
expr += ")"; |
| 6222 |
} |
| 6223 |
else if (is_array(mbr_type)) |
| 6224 |
{ |
| 6225 |
expr += type_to_glsl(mbr_type) + "({ "; |
| 6226 |
uint32_t array_size = to_array_size_literal(mbr_type, 0); |
| 6227 |
if (multi_patch_tess_ctl) |
| 6228 |
{ |
| 6229 |
for (uint32_t k = 0; k < array_size; k++) |
| 6230 |
{ |
| 6231 |
if (is_array_of_struct) |
| 6232 |
{ |
| 6233 |
const uint32_t indices[3] = { i, interface_index, k }; |
| 6234 |
AccessChainMeta meta; |
| 6235 |
expr += access_chain_internal( |
| 6236 |
stage_in_ptr_var_id, indices, 3, |
| 6237 |
ACCESS_CHAIN_INDEX_IS_LITERAL_BIT | ACCESS_CHAIN_PTR_CHAIN_BIT, &meta); |
| 6238 |
} |
| 6239 |
else |
| 6240 |
expr += join(to_expression(ptr), ".", to_member_name(iface_type, interface_index), "[", |
| 6241 |
k, "]"); |
| 6242 |
if (expr_mbr_type.vecsize > mbr_type.vecsize) |
| 6243 |
expr += vector_swizzle(mbr_type.vecsize, 0); |
| 6244 |
|
| 6245 |
if (k + 1 < array_size) |
| 6246 |
expr += ", "; |
| 6247 |
} |
| 6248 |
} |
| 6249 |
else |
| 6250 |
{ |
| 6251 |
for (uint32_t k = 0; k < array_size; k++, interface_index++) |
| 6252 |
{ |
| 6253 |
if (is_array_of_struct) |
| 6254 |
{ |
| 6255 |
const uint32_t indices[2] = { i, interface_index }; |
| 6256 |
AccessChainMeta meta; |
| 6257 |
expr += access_chain_internal( |
| 6258 |
stage_in_ptr_var_id, indices, 2, |
| 6259 |
ACCESS_CHAIN_INDEX_IS_LITERAL_BIT | ACCESS_CHAIN_PTR_CHAIN_BIT, &meta); |
| 6260 |
} |
| 6261 |
else |
| 6262 |
expr += to_expression(ptr) + "." + to_member_name(iface_type, interface_index); |
| 6263 |
if (expr_mbr_type.vecsize > mbr_type.vecsize) |
| 6264 |
expr += vector_swizzle(mbr_type.vecsize, 0); |
| 6265 |
|
| 6266 |
if (k + 1 < array_size) |
| 6267 |
expr += ", "; |
| 6268 |
} |
| 6269 |
} |
| 6270 |
expr += " })"; |
| 6271 |
} |
| 6272 |
else |
| 6273 |
{ |
| 6274 |
if (is_array_of_struct) |
| 6275 |
{ |
| 6276 |
const uint32_t indices[2] = { i, interface_index }; |
| 6277 |
AccessChainMeta meta; |
| 6278 |
expr += access_chain_internal(stage_in_ptr_var_id, indices, 2, |
| 6279 |
ACCESS_CHAIN_INDEX_IS_LITERAL_BIT | ACCESS_CHAIN_PTR_CHAIN_BIT, |
| 6280 |
&meta); |
| 6281 |
} |
| 6282 |
else |
| 6283 |
expr += to_expression(ptr) + "." + to_member_name(iface_type, interface_index); |
| 6284 |
if (expr_mbr_type.vecsize > mbr_type.vecsize) |
| 6285 |
expr += vector_swizzle(mbr_type.vecsize, 0); |
| 6286 |
} |
| 6287 |
|
| 6288 |
if (j + 1 < struct_type.member_types.size()) |
| 6289 |
expr += ", "; |
| 6290 |
} |
| 6291 |
expr += " }"; |
| 6292 |
if (i + 1 < num_control_points) |
| 6293 |
expr += ", "; |
| 6294 |
} |
| 6295 |
if (is_array_of_struct) |
| 6296 |
expr += " })"; |
| 6297 |
} |
| 6298 |
else if (flat_matrix) |
| 6299 |
{ |
| 6300 |
bool is_array_of_matrix = is_array(result_type); |
| 6301 |
if (is_array_of_matrix && !ptr_is_io_variable) |
| 6302 |
SPIRV_CROSS_THROW("Loading array of matrix from IO variable must come directly from IO variable."); |
| 6303 |
if (interface_index == uint32_t(-1)) |
| 6304 |
SPIRV_CROSS_THROW("Interface index is unknown. Cannot continue."); |
| 6305 |
|
| 6306 |
if (is_array_of_matrix) |
| 6307 |
{ |
| 6308 |
// Loading a matrix from each control point. |
| 6309 |
uint32_t base_interface_index = interface_index; |
| 6310 |
uint32_t num_control_points = to_array_size_literal(result_type, 0); |
| 6311 |
expr += type_to_glsl(result_type) + "({ "; |
| 6312 |
|
| 6313 |
auto &matrix_type = get_variable_element_type(get<SPIRVariable>(ptr)); |
| 6314 |
|
| 6315 |
for (uint32_t i = 0; i < num_control_points; i++) |
| 6316 |
{ |
| 6317 |
interface_index = base_interface_index; |
| 6318 |
expr += type_to_glsl(matrix_type) + "("; |
| 6319 |
for (uint32_t j = 0; j < result_type.columns; j++, interface_index++) |
| 6320 |
{ |
| 6321 |
const uint32_t indices[2] = { i, interface_index }; |
| 6322 |
|
| 6323 |
AccessChainMeta meta; |
| 6324 |
expr += |
| 6325 |
access_chain_internal(stage_in_ptr_var_id, indices, 2, |
| 6326 |
ACCESS_CHAIN_INDEX_IS_LITERAL_BIT | ACCESS_CHAIN_PTR_CHAIN_BIT, &meta); |
| 6327 |
if (expr_type.vecsize > result_type.vecsize) |
| 6328 |
expr += vector_swizzle(result_type.vecsize, 0); |
| 6329 |
if (j + 1 < result_type.columns) |
| 6330 |
expr += ", "; |
| 6331 |
} |
| 6332 |
expr += ")"; |
| 6333 |
if (i + 1 < num_control_points) |
| 6334 |
expr += ", "; |
| 6335 |
} |
| 6336 |
|
| 6337 |
expr += " })"; |
| 6338 |
} |
| 6339 |
else |
| 6340 |
{ |
| 6341 |
expr += type_to_glsl(result_type) + "("; |
| 6342 |
for (uint32_t i = 0; i < result_type.columns; i++, interface_index++) |
| 6343 |
{ |
| 6344 |
expr += to_expression(ptr) + "." + to_member_name(iface_type, interface_index); |
| 6345 |
if (expr_type.vecsize > result_type.vecsize) |
| 6346 |
expr += vector_swizzle(result_type.vecsize, 0); |
| 6347 |
if (i + 1 < result_type.columns) |
| 6348 |
expr += ", "; |
| 6349 |
} |
| 6350 |
expr += ")"; |
| 6351 |
} |
| 6352 |
} |
| 6353 |
else if (ptr_is_io_variable) |
| 6354 |
{ |
| 6355 |
assert(is_array(result_type)); |
| 6356 |
assert(result_type.array.size() == 1); |
| 6357 |
if (interface_index == uint32_t(-1)) |
| 6358 |
SPIRV_CROSS_THROW("Interface index is unknown. Cannot continue."); |
| 6359 |
|
| 6360 |
// We're loading an array directly from a global variable. |
| 6361 |
// This means we're loading one member from each control point. |
| 6362 |
expr += type_to_glsl(result_type) + "({ "; |
| 6363 |
uint32_t num_control_points = to_array_size_literal(result_type, 0); |
| 6364 |
|
| 6365 |
for (uint32_t i = 0; i < num_control_points; i++) |
| 6366 |
{ |
| 6367 |
const uint32_t indices[2] = { i, interface_index }; |
| 6368 |
|
| 6369 |
AccessChainMeta meta; |
| 6370 |
expr += access_chain_internal(stage_in_ptr_var_id, indices, 2, |
| 6371 |
ACCESS_CHAIN_INDEX_IS_LITERAL_BIT | ACCESS_CHAIN_PTR_CHAIN_BIT, &meta); |
| 6372 |
if (expr_type.vecsize > result_type.vecsize) |
| 6373 |
expr += vector_swizzle(result_type.vecsize, 0); |
| 6374 |
|
| 6375 |
if (i + 1 < num_control_points) |
| 6376 |
expr += ", "; |
| 6377 |
} |
| 6378 |
expr += " })"; |
| 6379 |
} |
| 6380 |
else |
| 6381 |
{ |
| 6382 |
// We're loading an array from a concrete control point. |
| 6383 |
assert(is_array(result_type)); |
| 6384 |
assert(result_type.array.size() == 1); |
| 6385 |
if (interface_index == uint32_t(-1)) |
| 6386 |
SPIRV_CROSS_THROW("Interface index is unknown. Cannot continue."); |
| 6387 |
|
| 6388 |
expr += type_to_glsl(result_type) + "({ "; |
| 6389 |
uint32_t array_size = to_array_size_literal(result_type, 0); |
| 6390 |
for (uint32_t i = 0; i < array_size; i++, interface_index++) |
| 6391 |
{ |
| 6392 |
expr += to_expression(ptr) + "." + to_member_name(iface_type, interface_index); |
| 6393 |
if (expr_type.vecsize > result_type.vecsize) |
| 6394 |
expr += vector_swizzle(result_type.vecsize, 0); |
| 6395 |
if (i + 1 < array_size) |
| 6396 |
expr += ", "; |
| 6397 |
} |
| 6398 |
expr += " })"; |
| 6399 |
} |
| 6400 |
|
| 6401 |
emit_op(result_type_id, id, expr, false); |
| 6402 |
register_read(id, ptr, false); |
| 6403 |
return true; |
| 6404 |
} |
| 6405 |
|
| 6406 |
bool CompilerMSL::emit_tessellation_access_chain(const uint32_t *ops, uint32_t length) |
| 6407 |
{ |
| 6408 |
// If this is a per-vertex output, remap it to the I/O array buffer. |
| 6409 |
|
| 6410 |
// Any object which did not go through IO flattening shenanigans will go there instead. |
| 6411 |
// We will unflatten on-demand instead as needed, but not all possible cases can be supported, especially with arrays. |
| 6412 |
|
| 6413 |
auto *var = maybe_get_backing_variable(ops[2]); |
| 6414 |
bool patch = false; |
| 6415 |
bool flat_data = false; |
| 6416 |
bool ptr_is_chain = false; |
| 6417 |
bool multi_patch = get_execution_model() == ExecutionModelTessellationControl && msl_options.multi_patch_workgroup; |
| 6418 |
|
| 6419 |
if (var) |
| 6420 |
{ |
| 6421 |
patch = has_decoration(ops[2], DecorationPatch) || is_patch_block(get_variable_data_type(*var)); |
| 6422 |
|
| 6423 |
// Should match strip_array in add_interface_block. |
| 6424 |
flat_data = var->storage == StorageClassInput || |
| 6425 |
(var->storage == StorageClassOutput && get_execution_model() == ExecutionModelTessellationControl); |
| 6426 |
|
| 6427 |
// We might have a chained access chain, where |
| 6428 |
// we first take the access chain to the control point, and then we chain into a member or something similar. |
| 6429 |
// In this case, we need to skip gl_in/gl_out remapping. |
| 6430 |
ptr_is_chain = var->self != ID(ops[2]); |
| 6431 |
} |
| 6432 |
|
| 6433 |
BuiltIn bi_type = BuiltIn(get_decoration(ops[2], DecorationBuiltIn)); |
| 6434 |
if (var && flat_data && !patch && |
| 6435 |
(!is_builtin_variable(*var) || bi_type == BuiltInPosition || bi_type == BuiltInPointSize || |
| 6436 |
bi_type == BuiltInClipDistance || bi_type == BuiltInCullDistance || |
| 6437 |
get_variable_data_type(*var).basetype == SPIRType::Struct)) |
| 6438 |
{ |
| 6439 |
AccessChainMeta meta; |
| 6440 |
SmallVector<uint32_t> indices; |
| 6441 |
uint32_t next_id = ir.increase_bound_by(1); |
| 6442 |
|
| 6443 |
indices.reserve(length - 3 + 1); |
| 6444 |
|
| 6445 |
uint32_t first_non_array_index = ptr_is_chain ? 3 : 4; |
| 6446 |
VariableID stage_var_id = var->storage == StorageClassInput ? stage_in_ptr_var_id : stage_out_ptr_var_id; |
| 6447 |
VariableID ptr = ptr_is_chain ? VariableID(ops[2]) : stage_var_id; |
| 6448 |
if (!ptr_is_chain) |
| 6449 |
{ |
| 6450 |
// Index into gl_in/gl_out with first array index. |
| 6451 |
indices.push_back(ops[3]); |
| 6452 |
} |
| 6453 |
|
| 6454 |
auto &result_ptr_type = get<SPIRType>(ops[0]); |
| 6455 |
|
| 6456 |
uint32_t const_mbr_id = next_id++; |
| 6457 |
uint32_t index = get_extended_decoration(var->self, SPIRVCrossDecorationInterfaceMemberIndex); |
| 6458 |
if (var->storage == StorageClassInput || has_decoration(get_variable_element_type(*var).self, DecorationBlock)) |
| 6459 |
{ |
| 6460 |
uint32_t i = first_non_array_index; |
| 6461 |
auto *type = &get_variable_element_type(*var); |
| 6462 |
if (index == uint32_t(-1) && length >= (first_non_array_index + 1)) |
| 6463 |
{ |
| 6464 |
// Maybe this is a struct type in the input class, in which case |
| 6465 |
// we put it as a decoration on the corresponding member. |
| 6466 |
index = get_extended_member_decoration(var->self, get_constant(ops[first_non_array_index]).scalar(), |
| 6467 |
SPIRVCrossDecorationInterfaceMemberIndex); |
| 6468 |
assert(index != uint32_t(-1)); |
| 6469 |
i++; |
| 6470 |
type = &get<SPIRType>(type->member_types[get_constant(ops[first_non_array_index]).scalar()]); |
| 6471 |
} |
| 6472 |
|
| 6473 |
// In this case, we're poking into flattened structures and arrays, so now we have to |
| 6474 |
// combine the following indices. If we encounter a non-constant index, |
| 6475 |
// we're hosed. |
| 6476 |
for (; i < length; ++i) |
| 6477 |
{ |
| 6478 |
if ((multi_patch || (!is_array(*type) && !is_matrix(*type))) && type->basetype != SPIRType::Struct) |
| 6479 |
break; |
| 6480 |
|
| 6481 |
auto *c = maybe_get<SPIRConstant>(ops[i]); |
| 6482 |
if (!c || c->specialization) |
| 6483 |
SPIRV_CROSS_THROW("Trying to dynamically index into an array interface variable in tessellation. " |
| 6484 |
"This is currently unsupported."); |
| 6485 |
|
| 6486 |
// We're in flattened space, so just increment the member index into IO block. |
| 6487 |
// We can only do this once in the current implementation, so either: |
| 6488 |
// Struct, Matrix or 1-dimensional array for a control point. |
| 6489 |
index += c->scalar(); |
| 6490 |
|
| 6491 |
if (type->parent_type) |
| 6492 |
type = &get<SPIRType>(type->parent_type); |
| 6493 |
else if (type->basetype == SPIRType::Struct) |
| 6494 |
type = &get<SPIRType>(type->member_types[c->scalar()]); |
| 6495 |
} |
| 6496 |
|
| 6497 |
if ((!multi_patch && (is_matrix(result_ptr_type) || is_array(result_ptr_type))) || |
| 6498 |
result_ptr_type.basetype == SPIRType::Struct) |
| 6499 |
{ |
| 6500 |
// We're not going to emit the actual member name, we let any further OpLoad take care of that. |
| 6501 |
// Tag the access chain with the member index we're referencing. |
| 6502 |
set_extended_decoration(ops[1], SPIRVCrossDecorationInterfaceMemberIndex, index); |
| 6503 |
} |
| 6504 |
else |
| 6505 |
{ |
| 6506 |
// Access the appropriate member of gl_in/gl_out. |
| 6507 |
set<SPIRConstant>(const_mbr_id, get_uint_type_id(), index, false); |
| 6508 |
indices.push_back(const_mbr_id); |
| 6509 |
|
| 6510 |
// Append any straggling access chain indices. |
| 6511 |
if (i < length) |
| 6512 |
indices.insert(indices.end(), ops + i, ops + length); |
| 6513 |
} |
| 6514 |
} |
| 6515 |
else |
| 6516 |
{ |
| 6517 |
assert(index != uint32_t(-1)); |
| 6518 |
set<SPIRConstant>(const_mbr_id, get_uint_type_id(), index, false); |
| 6519 |
indices.push_back(const_mbr_id); |
| 6520 |
|
| 6521 |
indices.insert(indices.end(), ops + 4, ops + length); |
| 6522 |
} |
| 6523 |
|
| 6524 |
// We use the pointer to the base of the input/output array here, |
| 6525 |
// so this is always a pointer chain. |
| 6526 |
string e; |
| 6527 |
|
| 6528 |
if (!ptr_is_chain) |
| 6529 |
{ |
| 6530 |
// This is the start of an access chain, use ptr_chain to index into control point array. |
| 6531 |
e = access_chain(ptr, indices.data(), uint32_t(indices.size()), result_ptr_type, &meta, true); |
| 6532 |
} |
| 6533 |
else |
| 6534 |
{ |
| 6535 |
// If we're accessing a struct, we need to use member indices which are based on the IO block, |
| 6536 |
// not actual struct type, so we have to use a split access chain here where |
| 6537 |
// first path resolves the control point index, i.e. gl_in[index], and second half deals with |
| 6538 |
// looking up flattened member name. |
| 6539 |
|
| 6540 |
// However, it is possible that we partially accessed a struct, |
| 6541 |
// by taking pointer to member inside the control-point array. |
| 6542 |
// For this case, we fall back to a natural access chain since we have already dealt with remapping struct members. |
| 6543 |
// One way to check this here is if we have 2 implied read expressions. |
| 6544 |
// First one is the gl_in/gl_out struct itself, then an index into that array. |
| 6545 |
// If we have traversed further, we use a normal access chain formulation. |
| 6546 |
auto *ptr_expr = maybe_get<SPIRExpression>(ptr); |
| 6547 |
if (ptr_expr && ptr_expr->implied_read_expressions.size() == 2) |
| 6548 |
{ |
| 6549 |
e = join(to_expression(ptr), |
| 6550 |
access_chain_internal(stage_var_id, indices.data(), uint32_t(indices.size()), |
| 6551 |
ACCESS_CHAIN_CHAIN_ONLY_BIT, &meta)); |
| 6552 |
} |
| 6553 |
else |
| 6554 |
{ |
| 6555 |
e = access_chain_internal(ptr, indices.data(), uint32_t(indices.size()), 0, &meta); |
| 6556 |
} |
| 6557 |
} |
| 6558 |
|
| 6559 |
// Get the actual type of the object that was accessed. If it's a vector type and we changed it, |
| 6560 |
// then we'll need to add a swizzle. |
| 6561 |
// For this, we can't necessarily rely on the type of the base expression, because it might be |
| 6562 |
// another access chain, and it will therefore already have the "correct" type. |
| 6563 |
auto *expr_type = &get_variable_data_type(*var); |
| 6564 |
if (has_extended_decoration(ops[2], SPIRVCrossDecorationTessIOOriginalInputTypeID)) |
| 6565 |
expr_type = &get<SPIRType>(get_extended_decoration(ops[2], SPIRVCrossDecorationTessIOOriginalInputTypeID)); |
| 6566 |
for (uint32_t i = 3; i < length; i++) |
| 6567 |
{ |
| 6568 |
if (!is_array(*expr_type) && expr_type->basetype == SPIRType::Struct) |
| 6569 |
expr_type = &get<SPIRType>(expr_type->member_types[get<SPIRConstant>(ops[i]).scalar()]); |
| 6570 |
else |
| 6571 |
expr_type = &get<SPIRType>(expr_type->parent_type); |
| 6572 |
} |
| 6573 |
if (!is_array(*expr_type) && !is_matrix(*expr_type) && expr_type->basetype != SPIRType::Struct && |
| 6574 |
expr_type->vecsize > result_ptr_type.vecsize) |
| 6575 |
e += vector_swizzle(result_ptr_type.vecsize, 0); |
| 6576 |
|
| 6577 |
auto &expr = set<SPIRExpression>(ops[1], move(e), ops[0], should_forward(ops[2])); |
| 6578 |
expr.loaded_from = var->self; |
| 6579 |
expr.need_transpose = meta.need_transpose; |
| 6580 |
expr.access_chain = true; |
| 6581 |
|
| 6582 |
// Mark the result as being packed if necessary. |
| 6583 |
if (meta.storage_is_packed) |
| 6584 |
set_extended_decoration(ops[1], SPIRVCrossDecorationPhysicalTypePacked); |
| 6585 |
if (meta.storage_physical_type != 0) |
| 6586 |
set_extended_decoration(ops[1], SPIRVCrossDecorationPhysicalTypeID, meta.storage_physical_type); |
| 6587 |
if (meta.storage_is_invariant) |
| 6588 |
set_decoration(ops[1], DecorationInvariant); |
| 6589 |
// Save the type we found in case the result is used in another access chain. |
| 6590 |
set_extended_decoration(ops[1], SPIRVCrossDecorationTessIOOriginalInputTypeID, expr_type->self); |
| 6591 |
|
| 6592 |
// If we have some expression dependencies in our access chain, this access chain is technically a forwarded |
| 6593 |
// temporary which could be subject to invalidation. |
| 6594 |
// Need to assume we're forwarded while calling inherit_expression_depdendencies. |
| 6595 |
forwarded_temporaries.insert(ops[1]); |
| 6596 |
// The access chain itself is never forced to a temporary, but its dependencies might. |
| 6597 |
suppressed_usage_tracking.insert(ops[1]); |
| 6598 |
|
| 6599 |
for (uint32_t i = 2; i < length; i++) |
| 6600 |
{ |
| 6601 |
inherit_expression_dependencies(ops[1], ops[i]); |
| 6602 |
add_implied_read_expression(expr, ops[i]); |
| 6603 |
} |
| 6604 |
|
| 6605 |
// If we have no dependencies after all, i.e., all indices in the access chain are immutable temporaries, |
| 6606 |
// we're not forwarded after all. |
| 6607 |
if (expr.expression_dependencies.empty()) |
| 6608 |
forwarded_temporaries.erase(ops[1]); |
| 6609 |
|
| 6610 |
return true; |
| 6611 |
} |
| 6612 |
|
| 6613 |
// If this is the inner tessellation level, and we're tessellating triangles, |
| 6614 |
// drop the last index. It isn't an array in this case, so we can't have an |
| 6615 |
// array reference here. We need to make this ID a variable instead of an |
| 6616 |
// expression so we don't try to dereference it as a variable pointer. |
| 6617 |
// Don't do this if the index is a constant 1, though. We need to drop stores |
| 6618 |
// to that one. |
| 6619 |
auto *m = ir.find_meta(var ? var->self : ID(0)); |
| 6620 |
if (get_execution_model() == ExecutionModelTessellationControl && var && m && |
| 6621 |
m->decoration.builtin_type == BuiltInTessLevelInner && get_entry_point().flags.get(ExecutionModeTriangles)) |
| 6622 |
{ |
| 6623 |
auto *c = maybe_get<SPIRConstant>(ops[3]); |
| 6624 |
if (c && c->scalar() == 1) |
| 6625 |
return false; |
| 6626 |
auto &dest_var = set<SPIRVariable>(ops[1], *var); |
| 6627 |
dest_var.basetype = ops[0]; |
| 6628 |
ir.meta[ops[1]] = ir.meta[ops[2]]; |
| 6629 |
inherit_expression_dependencies(ops[1], ops[2]); |
| 6630 |
return true; |
| 6631 |
} |
| 6632 |
|
| 6633 |
return false; |
| 6634 |
} |
| 6635 |
|
| 6636 |
bool CompilerMSL::is_out_of_bounds_tessellation_level(uint32_t id_lhs) |
| 6637 |
{ |
| 6638 |
if (!get_entry_point().flags.get(ExecutionModeTriangles)) |
| 6639 |
return false; |
| 6640 |
|
| 6641 |
// In SPIR-V, TessLevelInner always has two elements and TessLevelOuter always has |
| 6642 |
// four. This is true even if we are tessellating triangles. This allows clients |
| 6643 |
// to use a single tessellation control shader with multiple tessellation evaluation |
| 6644 |
// shaders. |
| 6645 |
// In Metal, however, only the first element of TessLevelInner and the first three |
| 6646 |
// of TessLevelOuter are accessible. This stems from how in Metal, the tessellation |
| 6647 |
// levels must be stored to a dedicated buffer in a particular format that depends |
| 6648 |
// on the patch type. Therefore, in Triangles mode, any access to the second |
| 6649 |
// inner level or the fourth outer level must be dropped. |
| 6650 |
const auto *e = maybe_get<SPIRExpression>(id_lhs); |
| 6651 |
if (!e || !e->access_chain) |
| 6652 |
return false; |
| 6653 |
BuiltIn builtin = BuiltIn(get_decoration(e->loaded_from, DecorationBuiltIn)); |
| 6654 |
if (builtin != BuiltInTessLevelInner && builtin != BuiltInTessLevelOuter) |
| 6655 |
return false; |
| 6656 |
auto *c = maybe_get<SPIRConstant>(e->implied_read_expressions[1]); |
| 6657 |
if (!c) |
| 6658 |
return false; |
| 6659 |
return (builtin == BuiltInTessLevelInner && c->scalar() == 1) || |
| 6660 |
(builtin == BuiltInTessLevelOuter && c->scalar() == 3); |
| 6661 |
} |
| 6662 |
|
| 6663 |
void CompilerMSL::prepare_access_chain_for_scalar_access(std::string &expr, const SPIRType &type, |
| 6664 |
spv::StorageClass storage, bool &is_packed) |
| 6665 |
{ |
| 6666 |
// If there is any risk of writes happening with the access chain in question, |
| 6667 |
// and there is a risk of concurrent write access to other components, |
| 6668 |
// we must cast the access chain to a plain pointer to ensure we only access the exact scalars we expect. |
| 6669 |
// The MSL compiler refuses to allow component-level access for any non-packed vector types. |
| 6670 |
if (!is_packed && (storage == StorageClassStorageBuffer || storage == StorageClassWorkgroup)) |
| 6671 |
{ |
| 6672 |
const char *addr_space = storage == StorageClassWorkgroup ? "threadgroup" : "device"; |
| 6673 |
expr = join("((", addr_space, " ", type_to_glsl(type), "*)&", enclose_expression(expr), ")"); |
| 6674 |
|
| 6675 |
// Further indexing should happen with packed rules (array index, not swizzle). |
| 6676 |
is_packed = true; |
| 6677 |
} |
| 6678 |
} |
| 6679 |
|
| 6680 |
// Sets the interface member index for an access chain to a pull-model interpolant. |
| 6681 |
void CompilerMSL::fix_up_interpolant_access_chain(const uint32_t *ops, uint32_t length) |
| 6682 |
{ |
| 6683 |
auto *var = maybe_get_backing_variable(ops[2]); |
| 6684 |
if (!var || !pull_model_inputs.count(var->self)) |
| 6685 |
return; |
| 6686 |
// Get the base index. |
| 6687 |
uint32_t interface_index; |
| 6688 |
auto &var_type = get_variable_data_type(*var); |
| 6689 |
auto &result_type = get<SPIRType>(ops[0]); |
| 6690 |
auto *type = &var_type; |
| 6691 |
if (has_extended_decoration(ops[2], SPIRVCrossDecorationInterfaceMemberIndex)) |
| 6692 |
{ |
| 6693 |
interface_index = get_extended_decoration(ops[2], SPIRVCrossDecorationInterfaceMemberIndex); |
| 6694 |
} |
| 6695 |
else |
| 6696 |
{ |
| 6697 |
// Assume an access chain into a struct variable. |
| 6698 |
assert(var_type.basetype == SPIRType::Struct); |
| 6699 |
auto &c = get<SPIRConstant>(ops[3 + var_type.array.size()]); |
| 6700 |
interface_index = |
| 6701 |
get_extended_member_decoration(var->self, c.scalar(), SPIRVCrossDecorationInterfaceMemberIndex); |
| 6702 |
} |
| 6703 |
// Accumulate indices. We'll have to skip over the one for the struct, if present, because we already accounted |
| 6704 |
// for that getting the base index. |
| 6705 |
for (uint32_t i = 3; i < length; ++i) |
| 6706 |
{ |
| 6707 |
if (is_vector(*type) && is_scalar(result_type)) |
| 6708 |
{ |
| 6709 |
// We don't want to combine the next index. Actually, we need to save it |
| 6710 |
// so we know to apply a swizzle to the result of the interpolation. |
| 6711 |
set_extended_decoration(ops[1], SPIRVCrossDecorationInterpolantComponentExpr, ops[i]); |
| 6712 |
break; |
| 6713 |
} |
| 6714 |
|
| 6715 |
auto *c = maybe_get<SPIRConstant>(ops[i]); |
| 6716 |
if (!c || c->specialization) |
| 6717 |
SPIRV_CROSS_THROW("Trying to dynamically index into an array interface variable using pull-model " |
| 6718 |
"interpolation. This is currently unsupported."); |
| 6719 |
|
| 6720 |
if (type->parent_type) |
| 6721 |
type = &get<SPIRType>(type->parent_type); |
| 6722 |
else if (type->basetype == SPIRType::Struct) |
| 6723 |
type = &get<SPIRType>(type->member_types[c->scalar()]); |
| 6724 |
|
| 6725 |
if (!has_extended_decoration(ops[2], SPIRVCrossDecorationInterfaceMemberIndex) && |
| 6726 |
i - 3 == var_type.array.size()) |
| 6727 |
continue; |
| 6728 |
|
| 6729 |
interface_index += c->scalar(); |
| 6730 |
} |
| 6731 |
// Save this to the access chain itself so we can recover it later when calling an interpolation function. |
| 6732 |
set_extended_decoration(ops[1], SPIRVCrossDecorationInterfaceMemberIndex, interface_index); |
| 6733 |
} |
| 6734 |
|
| 6735 |
// Override for MSL-specific syntax instructions |
| 6736 |
void CompilerMSL::emit_instruction(const Instruction &instruction) |
| 6737 |
{ |
| 6738 |
#define MSL_BOP(op) emit_binary_op(ops[0], ops[1], ops[2], ops[3], #op) |
| 6739 |
#define MSL_BOP_CAST(op, type) \ |
| 6740 |
emit_binary_op_cast(ops[0], ops[1], ops[2], ops[3], #op, type, opcode_is_sign_invariant(opcode)) |
| 6741 |
#define MSL_UOP(op) emit_unary_op(ops[0], ops[1], ops[2], #op) |
| 6742 |
#define MSL_QFOP(op) emit_quaternary_func_op(ops[0], ops[1], ops[2], ops[3], ops[4], ops[5], #op) |
| 6743 |
#define MSL_TFOP(op) emit_trinary_func_op(ops[0], ops[1], ops[2], ops[3], ops[4], #op) |
| 6744 |
#define MSL_BFOP(op) emit_binary_func_op(ops[0], ops[1], ops[2], ops[3], #op) |
| 6745 |
#define MSL_BFOP_CAST(op, type) \ |
| 6746 |
emit_binary_func_op_cast(ops[0], ops[1], ops[2], ops[3], #op, type, opcode_is_sign_invariant(opcode)) |
| 6747 |
#define MSL_UFOP(op) emit_unary_func_op(ops[0], ops[1], ops[2], #op) |
| 6748 |
#define MSL_UNORD_BOP(op) emit_binary_unord_op(ops[0], ops[1], ops[2], ops[3], #op) |
| 6749 |
|
| 6750 |
auto ops = stream(instruction); |
| 6751 |
auto opcode = static_cast<Op>(instruction.op); |
| 6752 |
|
| 6753 |
// If we need to do implicit bitcasts, make sure we do it with the correct type. |
| 6754 |
uint32_t integer_width = get_integer_width_for_instruction(instruction); |
| 6755 |
auto int_type = to_signed_basetype(integer_width); |
| 6756 |
auto uint_type = to_unsigned_basetype(integer_width); |
| 6757 |
|
| 6758 |
switch (opcode) |
| 6759 |
{ |
| 6760 |
case OpLoad: |
| 6761 |
{ |
| 6762 |
uint32_t id = ops[1]; |
| 6763 |
uint32_t ptr = ops[2]; |
| 6764 |
if (is_tessellation_shader()) |
| 6765 |
{ |
| 6766 |
if (!emit_tessellation_io_load(ops[0], id, ptr)) |
| 6767 |
CompilerGLSL::emit_instruction(instruction); |
| 6768 |
} |
| 6769 |
else |
| 6770 |
{ |
| 6771 |
// Sample mask input for Metal is not an array |
| 6772 |
if (BuiltIn(get_decoration(ptr, DecorationBuiltIn)) == BuiltInSampleMask) |
| 6773 |
set_decoration(id, DecorationBuiltIn, BuiltInSampleMask); |
| 6774 |
CompilerGLSL::emit_instruction(instruction); |
| 6775 |
} |
| 6776 |
break; |
| 6777 |
} |
| 6778 |
|
| 6779 |
// Comparisons |
| 6780 |
case OpIEqual: |
| 6781 |
MSL_BOP_CAST(==, int_type); |
| 6782 |
break; |
| 6783 |
|
| 6784 |
case OpLogicalEqual: |
| 6785 |
case OpFOrdEqual: |
| 6786 |
MSL_BOP(==); |
| 6787 |
break; |
| 6788 |
|
| 6789 |
case OpINotEqual: |
| 6790 |
MSL_BOP_CAST(!=, int_type); |
| 6791 |
break; |
| 6792 |
|
| 6793 |
case OpLogicalNotEqual: |
| 6794 |
case OpFOrdNotEqual: |
| 6795 |
MSL_BOP(!=); |
| 6796 |
break; |
| 6797 |
|
| 6798 |
case OpUGreaterThan: |
| 6799 |
MSL_BOP_CAST(>, uint_type); |
| 6800 |
break; |
| 6801 |
|
| 6802 |
case OpSGreaterThan: |
| 6803 |
MSL_BOP_CAST(>, int_type); |
| 6804 |
break; |
| 6805 |
|
| 6806 |
case OpFOrdGreaterThan: |
| 6807 |
MSL_BOP(>); |
| 6808 |
break; |
| 6809 |
|
| 6810 |
case OpUGreaterThanEqual: |
| 6811 |
MSL_BOP_CAST(>=, uint_type); |
| 6812 |
break; |
| 6813 |
|
| 6814 |
case OpSGreaterThanEqual: |
| 6815 |
MSL_BOP_CAST(>=, int_type); |
| 6816 |
break; |
| 6817 |
|
| 6818 |
case OpFOrdGreaterThanEqual: |
| 6819 |
MSL_BOP(>=); |
| 6820 |
break; |
| 6821 |
|
| 6822 |
case OpULessThan: |
| 6823 |
MSL_BOP_CAST(<, uint_type); |
| 6824 |
break; |
| 6825 |
|
| 6826 |
case OpSLessThan: |
| 6827 |
MSL_BOP_CAST(<, int_type); |
| 6828 |
break; |
| 6829 |
|
| 6830 |
case OpFOrdLessThan: |
| 6831 |
MSL_BOP(<); |
| 6832 |
break; |
| 6833 |
|
| 6834 |
case OpULessThanEqual: |
| 6835 |
MSL_BOP_CAST(<=, uint_type); |
| 6836 |
break; |
| 6837 |
|
| 6838 |
case OpSLessThanEqual: |
| 6839 |
MSL_BOP_CAST(<=, int_type); |
| 6840 |
break; |
| 6841 |
|
| 6842 |
case OpFOrdLessThanEqual: |
| 6843 |
MSL_BOP(<=); |
| 6844 |
break; |
| 6845 |
|
| 6846 |
case OpFUnordEqual: |
| 6847 |
MSL_UNORD_BOP(==); |
| 6848 |
break; |
| 6849 |
|
| 6850 |
case OpFUnordNotEqual: |
| 6851 |
MSL_UNORD_BOP(!=); |
| 6852 |
break; |
| 6853 |
|
| 6854 |
case OpFUnordGreaterThan: |
| 6855 |
MSL_UNORD_BOP(>); |
| 6856 |
break; |
| 6857 |
|
| 6858 |
case OpFUnordGreaterThanEqual: |
| 6859 |
MSL_UNORD_BOP(>=); |
| 6860 |
break; |
| 6861 |
|
| 6862 |
case OpFUnordLessThan: |
| 6863 |
MSL_UNORD_BOP(<); |
| 6864 |
break; |
| 6865 |
|
| 6866 |
case OpFUnordLessThanEqual: |
| 6867 |
MSL_UNORD_BOP(<=); |
| 6868 |
break; |
| 6869 |
|
| 6870 |
// Derivatives |
| 6871 |
case OpDPdx: |
| 6872 |
case OpDPdxFine: |
| 6873 |
case OpDPdxCoarse: |
| 6874 |
MSL_UFOP(dfdx); |
| 6875 |
register_control_dependent_expression(ops[1]); |
| 6876 |
break; |
| 6877 |
|
| 6878 |
case OpDPdy: |
| 6879 |
case OpDPdyFine: |
| 6880 |
case OpDPdyCoarse: |
| 6881 |
MSL_UFOP(dfdy); |
| 6882 |
register_control_dependent_expression(ops[1]); |
| 6883 |
break; |
| 6884 |
|
| 6885 |
case OpFwidth: |
| 6886 |
case OpFwidthCoarse: |
| 6887 |
case OpFwidthFine: |
| 6888 |
MSL_UFOP(fwidth); |
| 6889 |
register_control_dependent_expression(ops[1]); |
| 6890 |
break; |
| 6891 |
|
| 6892 |
// Bitfield |
| 6893 |
case OpBitFieldInsert: |
| 6894 |
{ |
| 6895 |
emit_bitfield_insert_op(ops[0], ops[1], ops[2], ops[3], ops[4], ops[5], "insert_bits", SPIRType::UInt); |
| 6896 |
break; |
| 6897 |
} |
| 6898 |
|
| 6899 |
case OpBitFieldSExtract: |
| 6900 |
{ |
| 6901 |
emit_trinary_func_op_bitextract(ops[0], ops[1], ops[2], ops[3], ops[4], "extract_bits", int_type, int_type, |
| 6902 |
SPIRType::UInt, SPIRType::UInt); |
| 6903 |
break; |
| 6904 |
} |
| 6905 |
|
| 6906 |
case OpBitFieldUExtract: |
| 6907 |
{ |
| 6908 |
emit_trinary_func_op_bitextract(ops[0], ops[1], ops[2], ops[3], ops[4], "extract_bits", uint_type, uint_type, |
| 6909 |
SPIRType::UInt, SPIRType::UInt); |
| 6910 |
break; |
| 6911 |
} |
| 6912 |
|
| 6913 |
case OpBitReverse: |
| 6914 |
// BitReverse does not have issues with sign since result type must match input type. |
| 6915 |
MSL_UFOP(reverse_bits); |
| 6916 |
break; |
| 6917 |
|
| 6918 |
case OpBitCount: |
| 6919 |
{ |
| 6920 |
auto basetype = expression_type(ops[2]).basetype; |
| 6921 |
emit_unary_func_op_cast(ops[0], ops[1], ops[2], "popcount", basetype, basetype); |
| 6922 |
break; |
| 6923 |
} |
| 6924 |
|
| 6925 |
case OpFRem: |
| 6926 |
MSL_BFOP(fmod); |
| 6927 |
break; |
| 6928 |
|
| 6929 |
case OpFMul: |
| 6930 |
if (msl_options.invariant_float_math) |
| 6931 |
MSL_BFOP(spvFMul); |
| 6932 |
else |
| 6933 |
MSL_BOP(*); |
| 6934 |
break; |
| 6935 |
|
| 6936 |
case OpFAdd: |
| 6937 |
if (msl_options.invariant_float_math) |
| 6938 |
MSL_BFOP(spvFAdd); |
| 6939 |
else |
| 6940 |
MSL_BOP(+); |
| 6941 |
break; |
| 6942 |
|
| 6943 |
// Atomics |
| 6944 |
case OpAtomicExchange: |
| 6945 |
{ |
| 6946 |
uint32_t result_type = ops[0]; |
| 6947 |
uint32_t id = ops[1]; |
| 6948 |
uint32_t ptr = ops[2]; |
| 6949 |
uint32_t mem_sem = ops[4]; |
| 6950 |
uint32_t val = ops[5]; |
| 6951 |
emit_atomic_func_op(result_type, id, "atomic_exchange_explicit", mem_sem, mem_sem, false, ptr, val); |
| 6952 |
break; |
| 6953 |
} |
| 6954 |
|
| 6955 |
case OpAtomicCompareExchange: |
| 6956 |
{ |
| 6957 |
uint32_t result_type = ops[0]; |
| 6958 |
uint32_t id = ops[1]; |
| 6959 |
uint32_t ptr = ops[2]; |
| 6960 |
uint32_t mem_sem_pass = ops[4]; |
| 6961 |
uint32_t mem_sem_fail = ops[5]; |
| 6962 |
uint32_t val = ops[6]; |
| 6963 |
uint32_t comp = ops[7]; |
| 6964 |
emit_atomic_func_op(result_type, id, "atomic_compare_exchange_weak_explicit", mem_sem_pass, mem_sem_fail, true, |
| 6965 |
ptr, comp, true, false, val); |
| 6966 |
break; |
| 6967 |
} |
| 6968 |
|
| 6969 |
case OpAtomicCompareExchangeWeak: |
| 6970 |
SPIRV_CROSS_THROW("OpAtomicCompareExchangeWeak is only supported in kernel profile."); |
| 6971 |
|
| 6972 |
case OpAtomicLoad: |
| 6973 |
{ |
| 6974 |
uint32_t result_type = ops[0]; |
| 6975 |
uint32_t id = ops[1]; |
| 6976 |
uint32_t ptr = ops[2]; |
| 6977 |
uint32_t mem_sem = ops[4]; |
| 6978 |
emit_atomic_func_op(result_type, id, "atomic_load_explicit", mem_sem, mem_sem, false, ptr, 0); |
| 6979 |
break; |
| 6980 |
} |
| 6981 |
|
| 6982 |
case OpAtomicStore: |
| 6983 |
{ |
| 6984 |
uint32_t result_type = expression_type(ops[0]).self; |
| 6985 |
uint32_t id = ops[0]; |
| 6986 |
uint32_t ptr = ops[0]; |
| 6987 |
uint32_t mem_sem = ops[2]; |
| 6988 |
uint32_t val = ops[3]; |
| 6989 |
emit_atomic_func_op(result_type, id, "atomic_store_explicit", mem_sem, mem_sem, false, ptr, val); |
| 6990 |
break; |
| 6991 |
} |
| 6992 |
|
| 6993 |
#define MSL_AFMO_IMPL(op, valsrc, valconst) \ |
| 6994 |
do \ |
| 6995 |
{ \ |
| 6996 |
uint32_t result_type = ops[0]; \ |
| 6997 |
uint32_t id = ops[1]; \ |
| 6998 |
uint32_t ptr = ops[2]; \ |
| 6999 |
uint32_t mem_sem = ops[4]; \ |
| 7000 |
uint32_t val = valsrc; \ |
| 7001 |
emit_atomic_func_op(result_type, id, "atomic_fetch_" #op "_explicit", mem_sem, mem_sem, false, ptr, val, \ |
| 7002 |
false, valconst); \ |
| 7003 |
} while (false) |
| 7004 |
|
| 7005 |
#define MSL_AFMO(op) MSL_AFMO_IMPL(op, ops[5], false) |
| 7006 |
#define MSL_AFMIO(op) MSL_AFMO_IMPL(op, 1, true) |
| 7007 |
|
| 7008 |
case OpAtomicIIncrement: |
| 7009 |
MSL_AFMIO(add); |
| 7010 |
break; |
| 7011 |
|
| 7012 |
case OpAtomicIDecrement: |
| 7013 |
MSL_AFMIO(sub); |
| 7014 |
break; |
| 7015 |
|
| 7016 |
case OpAtomicIAdd: |
| 7017 |
MSL_AFMO(add); |
| 7018 |
break; |
| 7019 |
|
| 7020 |
case OpAtomicISub: |
| 7021 |
MSL_AFMO(sub); |
| 7022 |
break; |
| 7023 |
|
| 7024 |
case OpAtomicSMin: |
| 7025 |
case OpAtomicUMin: |
| 7026 |
MSL_AFMO(min); |
| 7027 |
break; |
| 7028 |
|
| 7029 |
case OpAtomicSMax: |
| 7030 |
case OpAtomicUMax: |
| 7031 |
MSL_AFMO(max); |
| 7032 |
break; |
| 7033 |
|
| 7034 |
case OpAtomicAnd: |
| 7035 |
MSL_AFMO(and); |
| 7036 |
break; |
| 7037 |
|
| 7038 |
case OpAtomicOr: |
| 7039 |
MSL_AFMO(or); |
| 7040 |
break; |
| 7041 |
|
| 7042 |
case OpAtomicXor: |
| 7043 |
MSL_AFMO(xor); |
| 7044 |
break; |
| 7045 |
|
| 7046 |
// Images |
| 7047 |
|
| 7048 |
// Reads == Fetches in Metal |
| 7049 |
case OpImageRead: |
| 7050 |
{ |
| 7051 |
// Mark that this shader reads from this image |
| 7052 |
uint32_t img_id = ops[2]; |
| 7053 |
auto &type = expression_type(img_id); |
| 7054 |
if (type.image.dim != DimSubpassData) |
| 7055 |
{ |
| 7056 |
auto *p_var = maybe_get_backing_variable(img_id); |
| 7057 |
if (p_var && has_decoration(p_var->self, DecorationNonReadable)) |
| 7058 |
{ |
| 7059 |
unset_decoration(p_var->self, DecorationNonReadable); |
| 7060 |
force_recompile(); |
| 7061 |
} |
| 7062 |
} |
| 7063 |
|
| 7064 |
emit_texture_op(instruction, false); |
| 7065 |
break; |
| 7066 |
} |
| 7067 |
|
| 7068 |
// Emulate texture2D atomic operations |
| 7069 |
case OpImageTexelPointer: |
| 7070 |
{ |
| 7071 |
// When using the pointer, we need to know which variable it is actually loaded from. |
| 7072 |
auto *var = maybe_get_backing_variable(ops[2]); |
| 7073 |
if (var && atomic_image_vars.count(var->self)) |
| 7074 |
{ |
| 7075 |
uint32_t result_type = ops[0]; |
| 7076 |
uint32_t id = ops[1]; |
| 7077 |
|
| 7078 |
std::string coord = to_expression(ops[3]); |
| 7079 |
auto &type = expression_type(ops[2]); |
| 7080 |
if (type.image.dim == Dim2D) |
| 7081 |
{ |
| 7082 |
coord = join("spvImage2DAtomicCoord(", coord, ", ", to_expression(ops[2]), ")"); |
| 7083 |
} |
| 7084 |
|
| 7085 |
auto &e = set<SPIRExpression>(id, join(to_expression(ops[2]), "_atomic[", coord, "]"), result_type, true); |
| 7086 |
e.loaded_from = var ? var->self : ID(0); |
| 7087 |
inherit_expression_dependencies(id, ops[3]); |
| 7088 |
} |
| 7089 |
else |
| 7090 |
{ |
| 7091 |
uint32_t result_type = ops[0]; |
| 7092 |
uint32_t id = ops[1]; |
| 7093 |
auto &e = |
| 7094 |
set<SPIRExpression>(id, join(to_expression(ops[2]), ", ", to_expression(ops[3])), result_type, true); |
| 7095 |
|
| 7096 |
// When using the pointer, we need to know which variable it is actually loaded from. |
| 7097 |
e.loaded_from = var ? var->self : ID(0); |
| 7098 |
inherit_expression_dependencies(id, ops[3]); |
| 7099 |
} |
| 7100 |
break; |
| 7101 |
} |
| 7102 |
|
| 7103 |
case OpImageWrite: |
| 7104 |
{ |
| 7105 |
uint32_t img_id = ops[0]; |
| 7106 |
uint32_t coord_id = ops[1]; |
| 7107 |
uint32_t texel_id = ops[2]; |
| 7108 |
const uint32_t *opt = &ops[3]; |
| 7109 |
uint32_t length = instruction.length - 3; |
| 7110 |
|
| 7111 |
// Bypass pointers because we need the real image struct |
| 7112 |
auto &type = expression_type(img_id); |
| 7113 |
auto &img_type = get<SPIRType>(type.self); |
| 7114 |
|
| 7115 |
// Ensure this image has been marked as being written to and force a |
| 7116 |
// recommpile so that the image type output will include write access |
| 7117 |
auto *p_var = maybe_get_backing_variable(img_id); |
| 7118 |
if (p_var && has_decoration(p_var->self, DecorationNonWritable)) |
| 7119 |
{ |
| 7120 |
unset_decoration(p_var->self, DecorationNonWritable); |
| 7121 |
force_recompile(); |
| 7122 |
} |
| 7123 |
|
| 7124 |
bool forward = false; |
| 7125 |
uint32_t bias = 0; |
| 7126 |
uint32_t lod = 0; |
| 7127 |
uint32_t flags = 0; |
| 7128 |
|
| 7129 |
if (length) |
| 7130 |
{ |
| 7131 |
flags = *opt++; |
| 7132 |
length--; |
| 7133 |
} |
| 7134 |
|
| 7135 |
auto test = [&](uint32_t &v, uint32_t flag) { |
| 7136 |
if (length && (flags & flag)) |
| 7137 |
{ |
| 7138 |
v = *opt++; |
| 7139 |
length--; |
| 7140 |
} |
| 7141 |
}; |
| 7142 |
|
| 7143 |
test(bias, ImageOperandsBiasMask); |
| 7144 |
test(lod, ImageOperandsLodMask); |
| 7145 |
|
| 7146 |
auto &texel_type = expression_type(texel_id); |
| 7147 |
auto store_type = texel_type; |
| 7148 |
store_type.vecsize = 4; |
| 7149 |
|
| 7150 |
TextureFunctionArguments args = {}; |
| 7151 |
args.base.img = img_id; |
| 7152 |
args.base.imgtype = &img_type; |
| 7153 |
args.base.is_fetch = true; |
| 7154 |
args.coord = coord_id; |
| 7155 |
args.lod = lod; |
| 7156 |
statement(join(to_expression(img_id), ".write(", |
| 7157 |
remap_swizzle(store_type, texel_type.vecsize, to_expression(texel_id)), ", ", |
| 7158 |
CompilerMSL::to_function_args(args, &forward), ");")); |
| 7159 |
|
| 7160 |
if (p_var && variable_storage_is_aliased(*p_var)) |
| 7161 |
flush_all_aliased_variables(); |
| 7162 |
|
| 7163 |
break; |
| 7164 |
} |
| 7165 |
|
| 7166 |
case OpImageQuerySize: |
| 7167 |
case OpImageQuerySizeLod: |
| 7168 |
{ |
| 7169 |
uint32_t rslt_type_id = ops[0]; |
| 7170 |
auto &rslt_type = get<SPIRType>(rslt_type_id); |
| 7171 |
|
| 7172 |
uint32_t id = ops[1]; |
| 7173 |
|
| 7174 |
uint32_t img_id = ops[2]; |
| 7175 |
string img_exp = to_expression(img_id); |
| 7176 |
auto &img_type = expression_type(img_id); |
| 7177 |
Dim img_dim = img_type.image.dim; |
| 7178 |
bool img_is_array = img_type.image.arrayed; |
| 7179 |
|
| 7180 |
if (img_type.basetype != SPIRType::Image) |
| 7181 |
SPIRV_CROSS_THROW("Invalid type for OpImageQuerySize."); |
| 7182 |
|
| 7183 |
string lod; |
| 7184 |
if (opcode == OpImageQuerySizeLod) |
| 7185 |
{ |
| 7186 |
// LOD index defaults to zero, so don't bother outputing level zero index |
| 7187 |
string decl_lod = to_expression(ops[3]); |
| 7188 |
if (decl_lod != "0") |
| 7189 |
lod = decl_lod; |
| 7190 |
} |
| 7191 |
|
| 7192 |
string expr = type_to_glsl(rslt_type) + "("; |
| 7193 |
expr += img_exp + ".get_width(" + lod + ")"; |
| 7194 |
|
| 7195 |
if (img_dim == Dim2D || img_dim == DimCube || img_dim == Dim3D) |
| 7196 |
expr += ", " + img_exp + ".get_height(" + lod + ")"; |
| 7197 |
|
| 7198 |
if (img_dim == Dim3D) |
| 7199 |
expr += ", " + img_exp + ".get_depth(" + lod + ")"; |
| 7200 |
|
| 7201 |
if (img_is_array) |
| 7202 |
{ |
| 7203 |
expr += ", " + img_exp + ".get_array_size()"; |
| 7204 |
if (img_dim == DimCube && msl_options.emulate_cube_array) |
| 7205 |
expr += " / 6"; |
| 7206 |
} |
| 7207 |
|
| 7208 |
expr += ")"; |
| 7209 |
|
| 7210 |
emit_op(rslt_type_id, id, expr, should_forward(img_id)); |
| 7211 |
|
| 7212 |
break; |
| 7213 |
} |
| 7214 |
|
| 7215 |
case OpImageQueryLod: |
| 7216 |
{ |
| 7217 |
if (!msl_options.supports_msl_version(2, 2)) |
| 7218 |
SPIRV_CROSS_THROW("ImageQueryLod is only supported on MSL 2.2 and up."); |
| 7219 |
uint32_t result_type = ops[0]; |
| 7220 |
uint32_t id = ops[1]; |
| 7221 |
uint32_t image_id = ops[2]; |
| 7222 |
uint32_t coord_id = ops[3]; |
| 7223 |
emit_uninitialized_temporary_expression(result_type, id); |
| 7224 |
|
| 7225 |
auto sampler_expr = to_sampler_expression(image_id); |
| 7226 |
auto *combined = maybe_get<SPIRCombinedImageSampler>(image_id); |
| 7227 |
auto image_expr = combined ? to_expression(combined->image) : to_expression(image_id); |
| 7228 |
|
| 7229 |
// TODO: It is unclear if calculcate_clamped_lod also conditionally rounds |
| 7230 |
// the reported LOD based on the sampler. NEAREST miplevel should |
| 7231 |
// round the LOD, but LINEAR miplevel should not round. |
| 7232 |
// Let's hope this does not become an issue ... |
| 7233 |
statement(to_expression(id), ".x = ", image_expr, ".calculate_clamped_lod(", sampler_expr, ", ", |
| 7234 |
to_expression(coord_id), ");"); |
| 7235 |
statement(to_expression(id), ".y = ", image_expr, ".calculate_unclamped_lod(", sampler_expr, ", ", |
| 7236 |
to_expression(coord_id), ");"); |
| 7237 |
register_control_dependent_expression(id); |
| 7238 |
break; |
| 7239 |
} |
| 7240 |
|
| 7241 |
#define MSL_ImgQry(qrytype) \ |
| 7242 |
do \ |
| 7243 |
{ \ |
| 7244 |
uint32_t rslt_type_id = ops[0]; \ |
| 7245 |
auto &rslt_type = get<SPIRType>(rslt_type_id); \ |
| 7246 |
uint32_t id = ops[1]; \ |
| 7247 |
uint32_t img_id = ops[2]; \ |
| 7248 |
string img_exp = to_expression(img_id); \ |
| 7249 |
string expr = type_to_glsl(rslt_type) + "(" + img_exp + ".get_num_" #qrytype "())"; \ |
| 7250 |
emit_op(rslt_type_id, id, expr, should_forward(img_id)); \ |
| 7251 |
} while (false) |
| 7252 |
|
| 7253 |
case OpImageQueryLevels: |
| 7254 |
MSL_ImgQry(mip_levels); |
| 7255 |
break; |
| 7256 |
|
| 7257 |
case OpImageQuerySamples: |
| 7258 |
MSL_ImgQry(samples); |
| 7259 |
break; |
| 7260 |
|
| 7261 |
case OpImage: |
| 7262 |
{ |
| 7263 |
uint32_t result_type = ops[0]; |
| 7264 |
uint32_t id = ops[1]; |
| 7265 |
auto *combined = maybe_get<SPIRCombinedImageSampler>(ops[2]); |
| 7266 |
|
| 7267 |
if (combined) |
| 7268 |
{ |
| 7269 |
auto &e = emit_op(result_type, id, to_expression(combined->image), true, true); |
| 7270 |
auto *var = maybe_get_backing_variable(combined->image); |
| 7271 |
if (var) |
| 7272 |
e.loaded_from = var->self; |
| 7273 |
} |
| 7274 |
else |
| 7275 |
{ |
| 7276 |
auto *var = maybe_get_backing_variable(ops[2]); |
| 7277 |
SPIRExpression *e; |
| 7278 |
if (var && has_extended_decoration(var->self, SPIRVCrossDecorationDynamicImageSampler)) |
| 7279 |
e = &emit_op(result_type, id, join(to_expression(ops[2]), ".plane0"), true, true); |
| 7280 |
else |
| 7281 |
e = &emit_op(result_type, id, to_expression(ops[2]), true, true); |
| 7282 |
if (var) |
| 7283 |
e->loaded_from = var->self; |
| 7284 |
} |
| 7285 |
break; |
| 7286 |
} |
| 7287 |
|
| 7288 |
// Casting |
| 7289 |
case OpQuantizeToF16: |
| 7290 |
{ |
| 7291 |
uint32_t result_type = ops[0]; |
| 7292 |
uint32_t id = ops[1]; |
| 7293 |
uint32_t arg = ops[2]; |
| 7294 |
|
| 7295 |
string exp; |
| 7296 |
auto &type = get<SPIRType>(result_type); |
| 7297 |
|
| 7298 |
switch (type.vecsize) |
| 7299 |
{ |
| 7300 |
case 1: |
| 7301 |
exp = join("float(half(", to_expression(arg), "))"); |
| 7302 |
break; |
| 7303 |
case 2: |
| 7304 |
exp = join("float2(half2(", to_expression(arg), "))"); |
| 7305 |
break; |
| 7306 |
case 3: |
| 7307 |
exp = join("float3(half3(", to_expression(arg), "))"); |
| 7308 |
break; |
| 7309 |
case 4: |
| 7310 |
exp = join("float4(half4(", to_expression(arg), "))"); |
| 7311 |
break; |
| 7312 |
default: |
| 7313 |
SPIRV_CROSS_THROW("Illegal argument to OpQuantizeToF16."); |
| 7314 |
} |
| 7315 |
|
| 7316 |
emit_op(result_type, id, exp, should_forward(arg)); |
| 7317 |
break; |
| 7318 |
} |
| 7319 |
|
| 7320 |
case OpInBoundsAccessChain: |
| 7321 |
case OpAccessChain: |
| 7322 |
case OpPtrAccessChain: |
| 7323 |
if (is_tessellation_shader()) |
| 7324 |
{ |
| 7325 |
if (!emit_tessellation_access_chain(ops, instruction.length)) |
| 7326 |
CompilerGLSL::emit_instruction(instruction); |
| 7327 |
} |
| 7328 |
else |
| 7329 |
CompilerGLSL::emit_instruction(instruction); |
| 7330 |
fix_up_interpolant_access_chain(ops, instruction.length); |
| 7331 |
break; |
| 7332 |
|
| 7333 |
case OpStore: |
| 7334 |
if (is_out_of_bounds_tessellation_level(ops[0])) |
| 7335 |
break; |
| 7336 |
|
| 7337 |
if (maybe_emit_array_assignment(ops[0], ops[1])) |
| 7338 |
break; |
| 7339 |
|
| 7340 |
CompilerGLSL::emit_instruction(instruction); |
| 7341 |
break; |
| 7342 |
|
| 7343 |
// Compute barriers |
| 7344 |
case OpMemoryBarrier: |
| 7345 |
emit_barrier(0, ops[0], ops[1]); |
| 7346 |
break; |
| 7347 |
|
| 7348 |
case OpControlBarrier: |
| 7349 |
// In GLSL a memory barrier is often followed by a control barrier. |
| 7350 |
// But in MSL, memory barriers are also control barriers, so don't |
| 7351 |
// emit a simple control barrier if a memory barrier has just been emitted. |
| 7352 |
if (previous_instruction_opcode != OpMemoryBarrier) |
| 7353 |
emit_barrier(ops[0], ops[1], ops[2]); |
| 7354 |
break; |
| 7355 |
|
| 7356 |
case OpOuterProduct: |
| 7357 |
{ |
| 7358 |
uint32_t result_type = ops[0]; |
| 7359 |
uint32_t id = ops[1]; |
| 7360 |
uint32_t a = ops[2]; |
| 7361 |
uint32_t b = ops[3]; |
| 7362 |
|
| 7363 |
auto &type = get<SPIRType>(result_type); |
| 7364 |
string expr = type_to_glsl_constructor(type); |
| 7365 |
expr += "("; |
| 7366 |
for (uint32_t col = 0; col < type.columns; col++) |
| 7367 |
{ |
| 7368 |
expr += to_enclosed_expression(a); |
| 7369 |
expr += " * "; |
| 7370 |
expr += to_extract_component_expression(b, col); |
| 7371 |
if (col + 1 < type.columns) |
| 7372 |
expr += ", "; |
| 7373 |
} |
| 7374 |
expr += ")"; |
| 7375 |
emit_op(result_type, id, expr, should_forward(a) && should_forward(b)); |
| 7376 |
inherit_expression_dependencies(id, a); |
| 7377 |
inherit_expression_dependencies(id, b); |
| 7378 |
break; |
| 7379 |
} |
| 7380 |
|
| 7381 |
case OpVectorTimesMatrix: |
| 7382 |
case OpMatrixTimesVector: |
| 7383 |
{ |
| 7384 |
if (!msl_options.invariant_float_math) |
| 7385 |
{ |
| 7386 |
CompilerGLSL::emit_instruction(instruction); |
| 7387 |
break; |
| 7388 |
} |
| 7389 |
|
| 7390 |
// If the matrix needs transpose, just flip the multiply order. |
| 7391 |
auto *e = maybe_get<SPIRExpression>(ops[opcode == OpMatrixTimesVector ? 2 : 3]); |
| 7392 |
if (e && e->need_transpose) |
| 7393 |
{ |
| 7394 |
e->need_transpose = false; |
| 7395 |
string expr; |
| 7396 |
|
| 7397 |
if (opcode == OpMatrixTimesVector) |
| 7398 |
{ |
| 7399 |
expr = join("spvFMulVectorMatrix(", to_enclosed_unpacked_expression(ops[3]), ", ", |
| 7400 |
to_unpacked_row_major_matrix_expression(ops[2]), ")"); |
| 7401 |
} |
| 7402 |
else |
| 7403 |
{ |
| 7404 |
expr = join("spvFMulMatrixVector(", to_unpacked_row_major_matrix_expression(ops[3]), ", ", |
| 7405 |
to_enclosed_unpacked_expression(ops[2]), ")"); |
| 7406 |
} |
| 7407 |
|
| 7408 |
bool forward = should_forward(ops[2]) && should_forward(ops[3]); |
| 7409 |
emit_op(ops[0], ops[1], expr, forward); |
| 7410 |
e->need_transpose = true; |
| 7411 |
inherit_expression_dependencies(ops[1], ops[2]); |
| 7412 |
inherit_expression_dependencies(ops[1], ops[3]); |
| 7413 |
} |
| 7414 |
else |
| 7415 |
{ |
| 7416 |
if (opcode == OpMatrixTimesVector) |
| 7417 |
MSL_BFOP(spvFMulMatrixVector); |
| 7418 |
else |
| 7419 |
MSL_BFOP(spvFMulVectorMatrix); |
| 7420 |
} |
| 7421 |
break; |
| 7422 |
} |
| 7423 |
|
| 7424 |
case OpMatrixTimesMatrix: |
| 7425 |
{ |
| 7426 |
if (!msl_options.invariant_float_math) |
| 7427 |
{ |
| 7428 |
CompilerGLSL::emit_instruction(instruction); |
| 7429 |
break; |
| 7430 |
} |
| 7431 |
|
| 7432 |
auto *a = maybe_get<SPIRExpression>(ops[2]); |
| 7433 |
auto *b = maybe_get<SPIRExpression>(ops[3]); |
| 7434 |
|
| 7435 |
// If both matrices need transpose, we can multiply in flipped order and tag the expression as transposed. |
| 7436 |
// a^T * b^T = (b * a)^T. |
| 7437 |
if (a && b && a->need_transpose && b->need_transpose) |
| 7438 |
{ |
| 7439 |
a->need_transpose = false; |
| 7440 |
b->need_transpose = false; |
| 7441 |
|
| 7442 |
auto expr = |
| 7443 |
join("spvFMulMatrixMatrix(", enclose_expression(to_unpacked_row_major_matrix_expression(ops[3])), ", ", |
| 7444 |
enclose_expression(to_unpacked_row_major_matrix_expression(ops[2])), ")"); |
| 7445 |
|
| 7446 |
bool forward = should_forward(ops[2]) && should_forward(ops[3]); |
| 7447 |
auto &e = emit_op(ops[0], ops[1], expr, forward); |
| 7448 |
e.need_transpose = true; |
| 7449 |
a->need_transpose = true; |
| 7450 |
b->need_transpose = true; |
| 7451 |
inherit_expression_dependencies(ops[1], ops[2]); |
| 7452 |
inherit_expression_dependencies(ops[1], ops[3]); |
| 7453 |
} |
| 7454 |
else |
| 7455 |
MSL_BFOP(spvFMulMatrixMatrix); |
| 7456 |
|
| 7457 |
break; |
| 7458 |
} |
| 7459 |
|
| 7460 |
case OpIAddCarry: |
| 7461 |
case OpISubBorrow: |
| 7462 |
{ |
| 7463 |
uint32_t result_type = ops[0]; |
| 7464 |
uint32_t result_id = ops[1]; |
| 7465 |
uint32_t op0 = ops[2]; |
| 7466 |
uint32_t op1 = ops[3]; |
| 7467 |
auto &type = get<SPIRType>(result_type); |
| 7468 |
emit_uninitialized_temporary_expression(result_type, result_id); |
| 7469 |
|
| 7470 |
auto &res_type = get<SPIRType>(type.member_types[1]); |
| 7471 |
if (opcode == OpIAddCarry) |
| 7472 |
{ |
| 7473 |
statement(to_expression(result_id), ".", to_member_name(type, 0), " = ", to_enclosed_expression(op0), " + ", |
| 7474 |
to_enclosed_expression(op1), ";"); |
| 7475 |
statement(to_expression(result_id), ".", to_member_name(type, 1), " = select(", type_to_glsl(res_type), |
| 7476 |
"(1), ", type_to_glsl(res_type), "(0), ", to_expression(result_id), ".", to_member_name(type, 0), |
| 7477 |
" >= max(", to_expression(op0), ", ", to_expression(op1), "));"); |
| 7478 |
} |
| 7479 |
else |
| 7480 |
{ |
| 7481 |
statement(to_expression(result_id), ".", to_member_name(type, 0), " = ", to_enclosed_expression(op0), " - ", |
| 7482 |
to_enclosed_expression(op1), ";"); |
| 7483 |
statement(to_expression(result_id), ".", to_member_name(type, 1), " = select(", type_to_glsl(res_type), |
| 7484 |
"(1), ", type_to_glsl(res_type), "(0), ", to_enclosed_expression(op0), |
| 7485 |
" >= ", to_enclosed_expression(op1), ");"); |
| 7486 |
} |
| 7487 |
break; |
| 7488 |
} |
| 7489 |
|
| 7490 |
case OpUMulExtended: |
| 7491 |
case OpSMulExtended: |
| 7492 |
{ |
| 7493 |
uint32_t result_type = ops[0]; |
| 7494 |
uint32_t result_id = ops[1]; |
| 7495 |
uint32_t op0 = ops[2]; |
| 7496 |
uint32_t op1 = ops[3]; |
| 7497 |
auto &type = get<SPIRType>(result_type); |
| 7498 |
emit_uninitialized_temporary_expression(result_type, result_id); |
| 7499 |
|
| 7500 |
statement(to_expression(result_id), ".", to_member_name(type, 0), " = ", to_enclosed_expression(op0), " * ", |
| 7501 |
to_enclosed_expression(op1), ";"); |
| 7502 |
statement(to_expression(result_id), ".", to_member_name(type, 1), " = mulhi(", to_expression(op0), ", ", |
| 7503 |
to_expression(op1), ");"); |
| 7504 |
break; |
| 7505 |
} |
| 7506 |
|
| 7507 |
case OpArrayLength: |
| 7508 |
{ |
| 7509 |
auto &type = expression_type(ops[2]); |
| 7510 |
uint32_t offset = type_struct_member_offset(type, ops[3]); |
| 7511 |
uint32_t stride = type_struct_member_array_stride(type, ops[3]); |
| 7512 |
|
| 7513 |
auto expr = join("(", to_buffer_size_expression(ops[2]), " - ", offset, ") / ", stride); |
| 7514 |
emit_op(ops[0], ops[1], expr, true); |
| 7515 |
break; |
| 7516 |
} |
| 7517 |
|
| 7518 |
// SPV_INTEL_shader_integer_functions2 |
| 7519 |
case OpUCountLeadingZerosINTEL: |
| 7520 |
MSL_UFOP(clz); |
| 7521 |
break; |
| 7522 |
|
| 7523 |
case OpUCountTrailingZerosINTEL: |
| 7524 |
MSL_UFOP(ctz); |
| 7525 |
break; |
| 7526 |
|
| 7527 |
case OpAbsISubINTEL: |
| 7528 |
case OpAbsUSubINTEL: |
| 7529 |
MSL_BFOP(absdiff); |
| 7530 |
break; |
| 7531 |
|
| 7532 |
case OpIAddSatINTEL: |
| 7533 |
case OpUAddSatINTEL: |
| 7534 |
MSL_BFOP(addsat); |
| 7535 |
break; |
| 7536 |
|
| 7537 |
case OpIAverageINTEL: |
| 7538 |
case OpUAverageINTEL: |
| 7539 |
MSL_BFOP(hadd); |
| 7540 |
break; |
| 7541 |
|
| 7542 |
case OpIAverageRoundedINTEL: |
| 7543 |
case OpUAverageRoundedINTEL: |
| 7544 |
MSL_BFOP(rhadd); |
| 7545 |
break; |
| 7546 |
|
| 7547 |
case OpISubSatINTEL: |
| 7548 |
case OpUSubSatINTEL: |
| 7549 |
MSL_BFOP(subsat); |
| 7550 |
break; |
| 7551 |
|
| 7552 |
case OpIMul32x16INTEL: |
| 7553 |
{ |
| 7554 |
uint32_t result_type = ops[0]; |
| 7555 |
uint32_t id = ops[1]; |
| 7556 |
uint32_t a = ops[2], b = ops[3]; |
| 7557 |
bool forward = should_forward(a) && should_forward(b); |
| 7558 |
emit_op(result_type, id, join("int(short(", to_expression(a), ")) * int(short(", to_expression(b), "))"), |
| 7559 |
forward); |
| 7560 |
inherit_expression_dependencies(id, a); |
| 7561 |
inherit_expression_dependencies(id, b); |
| 7562 |
break; |
| 7563 |
} |
| 7564 |
|
| 7565 |
case OpUMul32x16INTEL: |
| 7566 |
{ |
| 7567 |
uint32_t result_type = ops[0]; |
| 7568 |
uint32_t id = ops[1]; |
| 7569 |
uint32_t a = ops[2], b = ops[3]; |
| 7570 |
bool forward = should_forward(a) && should_forward(b); |
| 7571 |
emit_op(result_type, id, join("uint(ushort(", to_expression(a), ")) * uint(ushort(", to_expression(b), "))"), |
| 7572 |
forward); |
| 7573 |
inherit_expression_dependencies(id, a); |
| 7574 |
inherit_expression_dependencies(id, b); |
| 7575 |
break; |
| 7576 |
} |
| 7577 |
|
| 7578 |
// SPV_EXT_demote_to_helper_invocation |
| 7579 |
case OpDemoteToHelperInvocationEXT: |
| 7580 |
if (!msl_options.supports_msl_version(2, 3)) |
| 7581 |
SPIRV_CROSS_THROW("discard_fragment() does not formally have demote semantics until MSL 2.3."); |
| 7582 |
CompilerGLSL::emit_instruction(instruction); |
| 7583 |
break; |
| 7584 |
|
| 7585 |
case OpIsHelperInvocationEXT: |
| 7586 |
if (msl_options.is_ios() && !msl_options.supports_msl_version(2, 3)) |
| 7587 |
SPIRV_CROSS_THROW("simd_is_helper_thread() requires MSL 2.3 on iOS."); |
| 7588 |
else if (msl_options.is_macos() && !msl_options.supports_msl_version(2, 1)) |
| 7589 |
SPIRV_CROSS_THROW("simd_is_helper_thread() requires MSL 2.1 on macOS."); |
| 7590 |
emit_op(ops[0], ops[1], "simd_is_helper_thread()", false); |
| 7591 |
break; |
| 7592 |
|
| 7593 |
case OpBeginInvocationInterlockEXT: |
| 7594 |
case OpEndInvocationInterlockEXT: |
| 7595 |
if (!msl_options.supports_msl_version(2, 0)) |
| 7596 |
SPIRV_CROSS_THROW("Raster order groups require MSL 2.0."); |
| 7597 |
break; // Nothing to do in the body |
| 7598 |
|
| 7599 |
default: |
| 7600 |
CompilerGLSL::emit_instruction(instruction); |
| 7601 |
break; |
| 7602 |
} |
| 7603 |
|
| 7604 |
previous_instruction_opcode = opcode; |
| 7605 |
} |
| 7606 |
|
| 7607 |
void CompilerMSL::emit_texture_op(const Instruction &i, bool sparse) |
| 7608 |
{ |
| 7609 |
if (sparse) |
| 7610 |
SPIRV_CROSS_THROW("Sparse feedback not yet supported in MSL."); |
| 7611 |
|
| 7612 |
if (msl_options.use_framebuffer_fetch_subpasses) |
| 7613 |
{ |
| 7614 |
auto *ops = stream(i); |
| 7615 |
|
| 7616 |
uint32_t result_type_id = ops[0]; |
| 7617 |
uint32_t id = ops[1]; |
| 7618 |
uint32_t img = ops[2]; |
| 7619 |
|
| 7620 |
auto &type = expression_type(img); |
| 7621 |
auto &imgtype = get<SPIRType>(type.self); |
| 7622 |
|
| 7623 |
// Use Metal's native frame-buffer fetch API for subpass inputs. |
| 7624 |
if (imgtype.image.dim == DimSubpassData) |
| 7625 |
{ |
| 7626 |
// Subpass inputs cannot be invalidated, |
| 7627 |
// so just forward the expression directly. |
| 7628 |
string expr = to_expression(img); |
| 7629 |
emit_op(result_type_id, id, expr, true); |
| 7630 |
return; |
| 7631 |
} |
| 7632 |
} |
| 7633 |
|
| 7634 |
// Fallback to default implementation |
| 7635 |
CompilerGLSL::emit_texture_op(i, sparse); |
| 7636 |
} |
| 7637 |
|
| 7638 |
void CompilerMSL::emit_barrier(uint32_t id_exe_scope, uint32_t id_mem_scope, uint32_t id_mem_sem) |
| 7639 |
{ |
| 7640 |
if (get_execution_model() != ExecutionModelGLCompute && get_execution_model() != ExecutionModelTessellationControl) |
| 7641 |
return; |
| 7642 |
|
| 7643 |
uint32_t exe_scope = id_exe_scope ? evaluate_constant_u32(id_exe_scope) : uint32_t(ScopeInvocation); |
| 7644 |
uint32_t mem_scope = id_mem_scope ? evaluate_constant_u32(id_mem_scope) : uint32_t(ScopeInvocation); |
| 7645 |
// Use the wider of the two scopes (smaller value) |
| 7646 |
exe_scope = min(exe_scope, mem_scope); |
| 7647 |
|
| 7648 |
string bar_stmt; |
| 7649 |
if ((msl_options.is_ios() && msl_options.supports_msl_version(1, 2)) || msl_options.supports_msl_version(2)) |
| 7650 |
bar_stmt = exe_scope < ScopeSubgroup ? "threadgroup_barrier" : "simdgroup_barrier"; |
| 7651 |
else |
| 7652 |
bar_stmt = "threadgroup_barrier"; |
| 7653 |
bar_stmt += "("; |
| 7654 |
|
| 7655 |
uint32_t mem_sem = id_mem_sem ? evaluate_constant_u32(id_mem_sem) : uint32_t(MemorySemanticsMaskNone); |
| 7656 |
|
| 7657 |
// Use the | operator to combine flags if we can. |
| 7658 |
if (msl_options.supports_msl_version(1, 2)) |
| 7659 |
{ |
| 7660 |
string mem_flags = ""; |
| 7661 |
// For tesc shaders, this also affects objects in the Output storage class. |
| 7662 |
// Since in Metal, these are placed in a device buffer, we have to sync device memory here. |
| 7663 |
if (get_execution_model() == ExecutionModelTessellationControl || |
| 7664 |
(mem_sem & (MemorySemanticsUniformMemoryMask | MemorySemanticsCrossWorkgroupMemoryMask))) |
| 7665 |
mem_flags += "mem_flags::mem_device"; |
| 7666 |
|
| 7667 |
// Fix tessellation patch function processing |
| 7668 |
if (get_execution_model() == ExecutionModelTessellationControl || |
| 7669 |
(mem_sem & (MemorySemanticsSubgroupMemoryMask | MemorySemanticsWorkgroupMemoryMask))) |
| 7670 |
{ |
| 7671 |
if (!mem_flags.empty()) |
| 7672 |
mem_flags += " | "; |
| 7673 |
mem_flags += "mem_flags::mem_threadgroup"; |
| 7674 |
} |
| 7675 |
if (mem_sem & MemorySemanticsImageMemoryMask) |
| 7676 |
{ |
| 7677 |
if (!mem_flags.empty()) |
| 7678 |
mem_flags += " | "; |
| 7679 |
mem_flags += "mem_flags::mem_texture"; |
| 7680 |
} |
| 7681 |
|
| 7682 |
if (mem_flags.empty()) |
| 7683 |
mem_flags = "mem_flags::mem_none"; |
| 7684 |
|
| 7685 |
bar_stmt += mem_flags; |
| 7686 |
} |
| 7687 |
else |
| 7688 |
{ |
| 7689 |
if ((mem_sem & (MemorySemanticsUniformMemoryMask | MemorySemanticsCrossWorkgroupMemoryMask)) && |
| 7690 |
(mem_sem & (MemorySemanticsSubgroupMemoryMask | MemorySemanticsWorkgroupMemoryMask))) |
| 7691 |
bar_stmt += "mem_flags::mem_device_and_threadgroup"; |
| 7692 |
else if (mem_sem & (MemorySemanticsUniformMemoryMask | MemorySemanticsCrossWorkgroupMemoryMask)) |
| 7693 |
bar_stmt += "mem_flags::mem_device"; |
| 7694 |
else if (mem_sem & (MemorySemanticsSubgroupMemoryMask | MemorySemanticsWorkgroupMemoryMask)) |
| 7695 |
bar_stmt += "mem_flags::mem_threadgroup"; |
| 7696 |
else if (mem_sem & MemorySemanticsImageMemoryMask) |
| 7697 |
bar_stmt += "mem_flags::mem_texture"; |
| 7698 |
else |
| 7699 |
bar_stmt += "mem_flags::mem_none"; |
| 7700 |
} |
| 7701 |
|
| 7702 |
bar_stmt += ");"; |
| 7703 |
|
| 7704 |
statement(bar_stmt); |
| 7705 |
|
| 7706 |
assert(current_emitting_block); |
| 7707 |
flush_control_dependent_expressions(current_emitting_block->self); |
| 7708 |
flush_all_active_variables(); |
| 7709 |
} |
| 7710 |
|
| 7711 |
void CompilerMSL::emit_array_copy(const string &lhs, uint32_t rhs_id, StorageClass lhs_storage, |
| 7712 |
StorageClass rhs_storage) |
| 7713 |
{ |
| 7714 |
// Allow Metal to use the array<T> template to make arrays a value type. |
| 7715 |
// This, however, cannot be used for threadgroup address specifiers, so consider the custom array copy as fallback. |
| 7716 |
bool lhs_thread = (lhs_storage == StorageClassOutput || lhs_storage == StorageClassFunction || |
| 7717 |
lhs_storage == StorageClassGeneric || lhs_storage == StorageClassPrivate); |
| 7718 |
bool rhs_thread = (rhs_storage == StorageClassInput || rhs_storage == StorageClassFunction || |
| 7719 |
rhs_storage == StorageClassGeneric || rhs_storage == StorageClassPrivate); |
| 7720 |
|
| 7721 |
// If threadgroup storage qualifiers are *not* used: |
| 7722 |
// Avoid spvCopy* wrapper functions; Otherwise, spvUnsafeArray<> template cannot be used with that storage qualifier. |
| 7723 |
if (lhs_thread && rhs_thread && !using_builtin_array()) |
| 7724 |
{ |
| 7725 |
statement(lhs, " = ", to_expression(rhs_id), ";"); |
| 7726 |
} |
| 7727 |
else |
| 7728 |
{ |
| 7729 |
// Assignment from an array initializer is fine. |
| 7730 |
auto &type = expression_type(rhs_id); |
| 7731 |
auto *var = maybe_get_backing_variable(rhs_id); |
| 7732 |
|
| 7733 |
// Unfortunately, we cannot template on address space in MSL, |
| 7734 |
// so explicit address space redirection it is ... |
| 7735 |
bool is_constant = false; |
| 7736 |
if (ir.ids[rhs_id].get_type() == TypeConstant) |
| 7737 |
{ |
| 7738 |
is_constant = true; |
| 7739 |
} |
| 7740 |
else if (var && var->remapped_variable && var->statically_assigned && |
| 7741 |
ir.ids[var->static_expression].get_type() == TypeConstant) |
| 7742 |
{ |
| 7743 |
is_constant = true; |
| 7744 |
} |
| 7745 |
else if (rhs_storage == StorageClassUniform) |
| 7746 |
{ |
| 7747 |
is_constant = true; |
| 7748 |
} |
| 7749 |
|
| 7750 |
// For the case where we have OpLoad triggering an array copy, |
| 7751 |
// we cannot easily detect this case ahead of time since it's |
| 7752 |
// context dependent. We might have to force a recompile here |
| 7753 |
// if this is the only use of array copies in our shader. |
| 7754 |
if (type.array.size() > 1) |
| 7755 |
{ |
| 7756 |
if (type.array.size() > kArrayCopyMultidimMax) |
| 7757 |
SPIRV_CROSS_THROW("Cannot support this many dimensions for arrays of arrays."); |
| 7758 |
auto func = static_cast<SPVFuncImpl>(SPVFuncImplArrayCopyMultidimBase + type.array.size()); |
| 7759 |
add_spv_func_and_recompile(func); |
| 7760 |
} |
| 7761 |
else |
| 7762 |
add_spv_func_and_recompile(SPVFuncImplArrayCopy); |
| 7763 |
|
| 7764 |
const char *tag = nullptr; |
| 7765 |
if (lhs_thread && is_constant) |
| 7766 |
tag = "FromConstantToStack"; |
| 7767 |
else if (lhs_storage == StorageClassWorkgroup && is_constant) |
| 7768 |
tag = "FromConstantToThreadGroup"; |
| 7769 |
else if (lhs_thread && rhs_thread) |
| 7770 |
tag = "FromStackToStack"; |
| 7771 |
else if (lhs_storage == StorageClassWorkgroup && rhs_thread) |
| 7772 |
tag = "FromStackToThreadGroup"; |
| 7773 |
else if (lhs_thread && rhs_storage == StorageClassWorkgroup) |
| 7774 |
tag = "FromThreadGroupToStack"; |
| 7775 |
else if (lhs_storage == StorageClassWorkgroup && rhs_storage == StorageClassWorkgroup) |
| 7776 |
tag = "FromThreadGroupToThreadGroup"; |
| 7777 |
else if (lhs_storage == StorageClassStorageBuffer && rhs_storage == StorageClassStorageBuffer) |
| 7778 |
tag = "FromDeviceToDevice"; |
| 7779 |
else if (lhs_storage == StorageClassStorageBuffer && is_constant) |
| 7780 |
tag = "FromConstantToDevice"; |
| 7781 |
else if (lhs_storage == StorageClassStorageBuffer && rhs_storage == StorageClassWorkgroup) |
| 7782 |
tag = "FromThreadGroupToDevice"; |
| 7783 |
else if (lhs_storage == StorageClassStorageBuffer && rhs_thread) |
| 7784 |
tag = "FromStackToDevice"; |
| 7785 |
else if (lhs_storage == StorageClassWorkgroup && rhs_storage == StorageClassStorageBuffer) |
| 7786 |
tag = "FromDeviceToThreadGroup"; |
| 7787 |
else if (lhs_thread && rhs_storage == StorageClassStorageBuffer) |
| 7788 |
tag = "FromDeviceToStack"; |
| 7789 |
else |
| 7790 |
SPIRV_CROSS_THROW("Unknown storage class used for copying arrays."); |
| 7791 |
|
| 7792 |
// Pass internal array of spvUnsafeArray<> into wrapper functions |
| 7793 |
if (lhs_thread && !msl_options.force_native_arrays) |
| 7794 |
statement("spvArrayCopy", tag, type.array.size(), "(", lhs, ".elements, ", to_expression(rhs_id), ");"); |
| 7795 |
else if (rhs_thread && !msl_options.force_native_arrays) |
| 7796 |
statement("spvArrayCopy", tag, type.array.size(), "(", lhs, ", ", to_expression(rhs_id), ".elements);"); |
| 7797 |
else |
| 7798 |
statement("spvArrayCopy", tag, type.array.size(), "(", lhs, ", ", to_expression(rhs_id), ");"); |
| 7799 |
} |
| 7800 |
} |
| 7801 |
|
| 7802 |
// Since MSL does not allow arrays to be copied via simple variable assignment, |
| 7803 |
// if the LHS and RHS represent an assignment of an entire array, it must be |
| 7804 |
// implemented by calling an array copy function. |
| 7805 |
// Returns whether the struct assignment was emitted. |
| 7806 |
bool CompilerMSL::maybe_emit_array_assignment(uint32_t id_lhs, uint32_t id_rhs) |
| 7807 |
{ |
| 7808 |
// We only care about assignments of an entire array |
| 7809 |
auto &type = expression_type(id_rhs); |
| 7810 |
if (type.array.size() == 0) |
| 7811 |
return false; |
| 7812 |
|
| 7813 |
auto *var = maybe_get<SPIRVariable>(id_lhs); |
| 7814 |
|
| 7815 |
// Is this a remapped, static constant? Don't do anything. |
| 7816 |
if (var && var->remapped_variable && var->statically_assigned) |
| 7817 |
return true; |
| 7818 |
|
| 7819 |
if (ir.ids[id_rhs].get_type() == TypeConstant && var && var->deferred_declaration) |
| 7820 |
{ |
| 7821 |
// Special case, if we end up declaring a variable when assigning the constant array, |
| 7822 |
// we can avoid the copy by directly assigning the constant expression. |
| 7823 |
// This is likely necessary to be able to use a variable as a true look-up table, as it is unlikely |
| 7824 |
// the compiler will be able to optimize the spvArrayCopy() into a constant LUT. |
| 7825 |
// After a variable has been declared, we can no longer assign constant arrays in MSL unfortunately. |
| 7826 |
statement(to_expression(id_lhs), " = ", constant_expression(get<SPIRConstant>(id_rhs)), ";"); |
| 7827 |
return true; |
| 7828 |
} |
| 7829 |
|
| 7830 |
// Ensure the LHS variable has been declared |
| 7831 |
auto *p_v_lhs = maybe_get_backing_variable(id_lhs); |
| 7832 |
if (p_v_lhs) |
| 7833 |
flush_variable_declaration(p_v_lhs->self); |
| 7834 |
|
| 7835 |
emit_array_copy(to_expression(id_lhs), id_rhs, get_expression_effective_storage_class(id_lhs), |
| 7836 |
get_expression_effective_storage_class(id_rhs)); |
| 7837 |
register_write(id_lhs); |
| 7838 |
|
| 7839 |
return true; |
| 7840 |
} |
| 7841 |
|
| 7842 |
// Emits one of the atomic functions. In MSL, the atomic functions operate on pointers |
| 7843 |
void CompilerMSL::emit_atomic_func_op(uint32_t result_type, uint32_t result_id, const char *op, uint32_t mem_order_1, |
| 7844 |
uint32_t mem_order_2, bool has_mem_order_2, uint32_t obj, uint32_t op1, |
| 7845 |
bool op1_is_pointer, bool op1_is_literal, uint32_t op2) |
| 7846 |
{ |
| 7847 |
string exp = string(op) + "("; |
| 7848 |
|
| 7849 |
auto &type = get_pointee_type(expression_type(obj)); |
| 7850 |
exp += "("; |
| 7851 |
auto *var = maybe_get_backing_variable(obj); |
| 7852 |
if (!var) |
| 7853 |
SPIRV_CROSS_THROW("No backing variable for atomic operation."); |
| 7854 |
|
| 7855 |
// Emulate texture2D atomic operations |
| 7856 |
const auto &res_type = get<SPIRType>(var->basetype); |
| 7857 |
if (res_type.storage == StorageClassUniformConstant && res_type.basetype == SPIRType::Image) |
| 7858 |
{ |
| 7859 |
exp += "device"; |
| 7860 |
} |
| 7861 |
else |
| 7862 |
{ |
| 7863 |
exp += get_argument_address_space(*var); |
| 7864 |
} |
| 7865 |
|
| 7866 |
exp += " atomic_"; |
| 7867 |
exp += type_to_glsl(type); |
| 7868 |
exp += "*)"; |
| 7869 |
|
| 7870 |
exp += "&"; |
| 7871 |
exp += to_enclosed_expression(obj); |
| 7872 |
|
| 7873 |
bool is_atomic_compare_exchange_strong = op1_is_pointer && op1; |
| 7874 |
|
| 7875 |
if (is_atomic_compare_exchange_strong) |
| 7876 |
{ |
| 7877 |
assert(strcmp(op, "atomic_compare_exchange_weak_explicit") == 0); |
| 7878 |
assert(op2); |
| 7879 |
assert(has_mem_order_2); |
| 7880 |
exp += ", &"; |
| 7881 |
exp += to_name(result_id); |
| 7882 |
exp += ", "; |
| 7883 |
exp += to_expression(op2); |
| 7884 |
exp += ", "; |
| 7885 |
exp += get_memory_order(mem_order_1); |
| 7886 |
exp += ", "; |
| 7887 |
exp += get_memory_order(mem_order_2); |
| 7888 |
exp += ")"; |
| 7889 |
|
| 7890 |
// MSL only supports the weak atomic compare exchange, so emit a CAS loop here. |
| 7891 |
// The MSL function returns false if the atomic write fails OR the comparison test fails, |
| 7892 |
// so we must validate that it wasn't the comparison test that failed before continuing |
| 7893 |
// the CAS loop, otherwise it will loop infinitely, with the comparison test always failing. |
| 7894 |
// The function updates the comparitor value from the memory value, so the additional |
| 7895 |
// comparison test evaluates the memory value against the expected value. |
| 7896 |
emit_uninitialized_temporary_expression(result_type, result_id); |
| 7897 |
statement("do"); |
| 7898 |
begin_scope(); |
| 7899 |
statement(to_name(result_id), " = ", to_expression(op1), ";"); |
| 7900 |
end_scope_decl(join("while (!", exp, " && ", to_name(result_id), " == ", to_enclosed_expression(op1), ")")); |
| 7901 |
} |
| 7902 |
else |
| 7903 |
{ |
| 7904 |
assert(strcmp(op, "atomic_compare_exchange_weak_explicit") != 0); |
| 7905 |
if (op1) |
| 7906 |
{ |
| 7907 |
if (op1_is_literal) |
| 7908 |
exp += join(", ", op1); |
| 7909 |
else |
| 7910 |
exp += ", " + to_expression(op1); |
| 7911 |
} |
| 7912 |
if (op2) |
| 7913 |
exp += ", " + to_expression(op2); |
| 7914 |
|
| 7915 |
exp += string(", ") + get_memory_order(mem_order_1); |
| 7916 |
if (has_mem_order_2) |
| 7917 |
exp += string(", ") + get_memory_order(mem_order_2); |
| 7918 |
|
| 7919 |
exp += ")"; |
| 7920 |
|
| 7921 |
if (strcmp(op, "atomic_store_explicit") != 0) |
| 7922 |
emit_op(result_type, result_id, exp, false); |
| 7923 |
else |
| 7924 |
statement(exp, ";"); |
| 7925 |
} |
| 7926 |
|
| 7927 |
flush_all_atomic_capable_variables(); |
| 7928 |
} |
| 7929 |
|
| 7930 |
// Metal only supports relaxed memory order for now |
| 7931 |
const char *CompilerMSL::get_memory_order(uint32_t) |
| 7932 |
{ |
| 7933 |
return "memory_order_relaxed"; |
| 7934 |
} |
| 7935 |
|
| 7936 |
// Override for MSL-specific extension syntax instructions |
| 7937 |
void CompilerMSL::emit_glsl_op(uint32_t result_type, uint32_t id, uint32_t eop, const uint32_t *args, uint32_t count) |
| 7938 |
{ |
| 7939 |
auto op = static_cast<GLSLstd450>(eop); |
| 7940 |
|
| 7941 |
// If we need to do implicit bitcasts, make sure we do it with the correct type. |
| 7942 |
uint32_t integer_width = get_integer_width_for_glsl_instruction(op, args, count); |
| 7943 |
auto int_type = to_signed_basetype(integer_width); |
| 7944 |
auto uint_type = to_unsigned_basetype(integer_width); |
| 7945 |
|
| 7946 |
switch (op) |
| 7947 |
{ |
| 7948 |
case GLSLstd450Atan2: |
| 7949 |
emit_binary_func_op(result_type, id, args[0], args[1], "atan2"); |
| 7950 |
break; |
| 7951 |
case GLSLstd450InverseSqrt: |
| 7952 |
emit_unary_func_op(result_type, id, args[0], "rsqrt"); |
| 7953 |
break; |
| 7954 |
case GLSLstd450RoundEven: |
| 7955 |
emit_unary_func_op(result_type, id, args[0], "rint"); |
| 7956 |
break; |
| 7957 |
|
| 7958 |
case GLSLstd450FindILsb: |
| 7959 |
{ |
| 7960 |
// In this template version of findLSB, we return T. |
| 7961 |
auto basetype = expression_type(args[0]).basetype; |
| 7962 |
emit_unary_func_op_cast(result_type, id, args[0], "spvFindLSB", basetype, basetype); |
| 7963 |
break; |
| 7964 |
} |
| 7965 |
|
| 7966 |
case GLSLstd450FindSMsb: |
| 7967 |
emit_unary_func_op_cast(result_type, id, args[0], "spvFindSMSB", int_type, int_type); |
| 7968 |
break; |
| 7969 |
|
| 7970 |
case GLSLstd450FindUMsb: |
| 7971 |
emit_unary_func_op_cast(result_type, id, args[0], "spvFindUMSB", uint_type, uint_type); |
| 7972 |
break; |
| 7973 |
|
| 7974 |
case GLSLstd450PackSnorm4x8: |
| 7975 |
emit_unary_func_op(result_type, id, args[0], "pack_float_to_snorm4x8"); |
| 7976 |
break; |
| 7977 |
case GLSLstd450PackUnorm4x8: |
| 7978 |
emit_unary_func_op(result_type, id, args[0], "pack_float_to_unorm4x8"); |
| 7979 |
break; |
| 7980 |
case GLSLstd450PackSnorm2x16: |
| 7981 |
emit_unary_func_op(result_type, id, args[0], "pack_float_to_snorm2x16"); |
| 7982 |
break; |
| 7983 |
case GLSLstd450PackUnorm2x16: |
| 7984 |
emit_unary_func_op(result_type, id, args[0], "pack_float_to_unorm2x16"); |
| 7985 |
break; |
| 7986 |
|
| 7987 |
case GLSLstd450PackHalf2x16: |
| 7988 |
{ |
| 7989 |
auto expr = join("as_type<uint>(half2(", to_expression(args[0]), "))"); |
| 7990 |
emit_op(result_type, id, expr, should_forward(args[0])); |
| 7991 |
inherit_expression_dependencies(id, args[0]); |
| 7992 |
break; |
| 7993 |
} |
| 7994 |
|
| 7995 |
case GLSLstd450UnpackSnorm4x8: |
| 7996 |
emit_unary_func_op(result_type, id, args[0], "unpack_snorm4x8_to_float"); |
| 7997 |
break; |
| 7998 |
case GLSLstd450UnpackUnorm4x8: |
| 7999 |
emit_unary_func_op(result_type, id, args[0], "unpack_unorm4x8_to_float"); |
| 8000 |
break; |
| 8001 |
case GLSLstd450UnpackSnorm2x16: |
| 8002 |
emit_unary_func_op(result_type, id, args[0], "unpack_snorm2x16_to_float"); |
| 8003 |
break; |
| 8004 |
case GLSLstd450UnpackUnorm2x16: |
| 8005 |
emit_unary_func_op(result_type, id, args[0], "unpack_unorm2x16_to_float"); |
| 8006 |
break; |
| 8007 |
|
| 8008 |
case GLSLstd450UnpackHalf2x16: |
| 8009 |
{ |
| 8010 |
auto expr = join("float2(as_type<half2>(", to_expression(args[0]), "))"); |
| 8011 |
emit_op(result_type, id, expr, should_forward(args[0])); |
| 8012 |
inherit_expression_dependencies(id, args[0]); |
| 8013 |
break; |
| 8014 |
} |
| 8015 |
|
| 8016 |
case GLSLstd450PackDouble2x32: |
| 8017 |
emit_unary_func_op(result_type, id, args[0], "unsupported_GLSLstd450PackDouble2x32"); // Currently unsupported |
| 8018 |
break; |
| 8019 |
case GLSLstd450UnpackDouble2x32: |
| 8020 |
emit_unary_func_op(result_type, id, args[0], "unsupported_GLSLstd450UnpackDouble2x32"); // Currently unsupported |
| 8021 |
break; |
| 8022 |
|
| 8023 |
case GLSLstd450MatrixInverse: |
| 8024 |
{ |
| 8025 |
auto &mat_type = get<SPIRType>(result_type); |
| 8026 |
switch (mat_type.columns) |
| 8027 |
{ |
| 8028 |
case 2: |
| 8029 |
emit_unary_func_op(result_type, id, args[0], "spvInverse2x2"); |
| 8030 |
break; |
| 8031 |
case 3: |
| 8032 |
emit_unary_func_op(result_type, id, args[0], "spvInverse3x3"); |
| 8033 |
break; |
| 8034 |
case 4: |
| 8035 |
emit_unary_func_op(result_type, id, args[0], "spvInverse4x4"); |
| 8036 |
break; |
| 8037 |
default: |
| 8038 |
break; |
| 8039 |
} |
| 8040 |
break; |
| 8041 |
} |
| 8042 |
|
| 8043 |
case GLSLstd450FMin: |
| 8044 |
// If the result type isn't float, don't bother calling the specific |
| 8045 |
// precise::/fast:: version. Metal doesn't have those for half and |
| 8046 |
// double types. |
| 8047 |
if (get<SPIRType>(result_type).basetype != SPIRType::Float) |
| 8048 |
emit_binary_func_op(result_type, id, args[0], args[1], "min"); |
| 8049 |
else |
| 8050 |
emit_binary_func_op(result_type, id, args[0], args[1], "fast::min"); |
| 8051 |
break; |
| 8052 |
|
| 8053 |
case GLSLstd450FMax: |
| 8054 |
if (get<SPIRType>(result_type).basetype != SPIRType::Float) |
| 8055 |
emit_binary_func_op(result_type, id, args[0], args[1], "max"); |
| 8056 |
else |
| 8057 |
emit_binary_func_op(result_type, id, args[0], args[1], "fast::max"); |
| 8058 |
break; |
| 8059 |
|
| 8060 |
case GLSLstd450FClamp: |
| 8061 |
// TODO: If args[1] is 0 and args[2] is 1, emit a saturate() call. |
| 8062 |
if (get<SPIRType>(result_type).basetype != SPIRType::Float) |
| 8063 |
emit_trinary_func_op(result_type, id, args[0], args[1], args[2], "clamp"); |
| 8064 |
else |
| 8065 |
emit_trinary_func_op(result_type, id, args[0], args[1], args[2], "fast::clamp"); |
| 8066 |
break; |
| 8067 |
|
| 8068 |
case GLSLstd450NMin: |
| 8069 |
if (get<SPIRType>(result_type).basetype != SPIRType::Float) |
| 8070 |
emit_binary_func_op(result_type, id, args[0], args[1], "min"); |
| 8071 |
else |
| 8072 |
emit_binary_func_op(result_type, id, args[0], args[1], "precise::min"); |
| 8073 |
break; |
| 8074 |
|
| 8075 |
case GLSLstd450NMax: |
| 8076 |
if (get<SPIRType>(result_type).basetype != SPIRType::Float) |
| 8077 |
emit_binary_func_op(result_type, id, args[0], args[1], "max"); |
| 8078 |
else |
| 8079 |
emit_binary_func_op(result_type, id, args[0], args[1], "precise::max"); |
| 8080 |
break; |
| 8081 |
|
| 8082 |
case GLSLstd450NClamp: |
| 8083 |
// TODO: If args[1] is 0 and args[2] is 1, emit a saturate() call. |
| 8084 |
if (get<SPIRType>(result_type).basetype != SPIRType::Float) |
| 8085 |
emit_trinary_func_op(result_type, id, args[0], args[1], args[2], "clamp"); |
| 8086 |
else |
| 8087 |
emit_trinary_func_op(result_type, id, args[0], args[1], args[2], "precise::clamp"); |
| 8088 |
break; |
| 8089 |
|
| 8090 |
case GLSLstd450InterpolateAtCentroid: |
| 8091 |
{ |
| 8092 |
// We can't just emit the expression normally, because the qualified name contains a call to the default |
| 8093 |
// interpolate method, or refers to a local variable. We saved the interface index we need; use it to construct |
| 8094 |
// the base for the method call. |
| 8095 |
uint32_t interface_index = get_extended_decoration(args[0], SPIRVCrossDecorationInterfaceMemberIndex); |
| 8096 |
string component; |
| 8097 |
if (has_extended_decoration(args[0], SPIRVCrossDecorationInterpolantComponentExpr)) |
| 8098 |
{ |
| 8099 |
uint32_t index_expr = get_extended_decoration(args[0], SPIRVCrossDecorationInterpolantComponentExpr); |
| 8100 |
auto *c = maybe_get<SPIRConstant>(index_expr); |
| 8101 |
if (!c || c->specialization) |
| 8102 |
component = join("[", to_expression(index_expr), "]"); |
| 8103 |
else |
| 8104 |
component = join(".", index_to_swizzle(c->scalar())); |
| 8105 |
} |
| 8106 |
emit_op(result_type, id, |
| 8107 |
join(to_name(stage_in_var_id), ".", to_member_name(get_stage_in_struct_type(), interface_index), |
| 8108 |
".interpolate_at_centroid()", component), |
| 8109 |
should_forward(args[0])); |
| 8110 |
break; |
| 8111 |
} |
| 8112 |
|
| 8113 |
case GLSLstd450InterpolateAtSample: |
| 8114 |
{ |
| 8115 |
uint32_t interface_index = get_extended_decoration(args[0], SPIRVCrossDecorationInterfaceMemberIndex); |
| 8116 |
string component; |
| 8117 |
if (has_extended_decoration(args[0], SPIRVCrossDecorationInterpolantComponentExpr)) |
| 8118 |
{ |
| 8119 |
uint32_t index_expr = get_extended_decoration(args[0], SPIRVCrossDecorationInterpolantComponentExpr); |
| 8120 |
auto *c = maybe_get<SPIRConstant>(index_expr); |
| 8121 |
if (!c || c->specialization) |
| 8122 |
component = join("[", to_expression(index_expr), "]"); |
| 8123 |
else |
| 8124 |
component = join(".", index_to_swizzle(c->scalar())); |
| 8125 |
} |
| 8126 |
emit_op(result_type, id, |
| 8127 |
join(to_name(stage_in_var_id), ".", to_member_name(get_stage_in_struct_type(), interface_index), |
| 8128 |
".interpolate_at_sample(", to_expression(args[1]), ")", component), |
| 8129 |
should_forward(args[0]) && should_forward(args[1])); |
| 8130 |
break; |
| 8131 |
} |
| 8132 |
|
| 8133 |
case GLSLstd450InterpolateAtOffset: |
| 8134 |
{ |
| 8135 |
uint32_t interface_index = get_extended_decoration(args[0], SPIRVCrossDecorationInterfaceMemberIndex); |
| 8136 |
string component; |
| 8137 |
if (has_extended_decoration(args[0], SPIRVCrossDecorationInterpolantComponentExpr)) |
| 8138 |
{ |
| 8139 |
uint32_t index_expr = get_extended_decoration(args[0], SPIRVCrossDecorationInterpolantComponentExpr); |
| 8140 |
auto *c = maybe_get<SPIRConstant>(index_expr); |
| 8141 |
if (!c || c->specialization) |
| 8142 |
component = join("[", to_expression(index_expr), "]"); |
| 8143 |
else |
| 8144 |
component = join(".", index_to_swizzle(c->scalar())); |
| 8145 |
} |
| 8146 |
// Like Direct3D, Metal puts the (0, 0) at the upper-left corner, not the center as SPIR-V and GLSL do. |
| 8147 |
// Offset the offset by (1/2 - 1/16), or 0.4375, to compensate for this. |
| 8148 |
// It has to be (1/2 - 1/16) and not 1/2, or several CTS tests subtly break on Intel. |
| 8149 |
emit_op(result_type, id, |
| 8150 |
join(to_name(stage_in_var_id), ".", to_member_name(get_stage_in_struct_type(), interface_index), |
| 8151 |
".interpolate_at_offset(", to_expression(args[1]), " + 0.4375)", component), |
| 8152 |
should_forward(args[0]) && should_forward(args[1])); |
| 8153 |
break; |
| 8154 |
} |
| 8155 |
|
| 8156 |
case GLSLstd450Distance: |
| 8157 |
// MSL does not support scalar versions here. |
| 8158 |
if (expression_type(args[0]).vecsize == 1) |
| 8159 |
{ |
| 8160 |
// Equivalent to length(a - b) -> abs(a - b). |
| 8161 |
emit_op(result_type, id, |
| 8162 |
join("abs(", to_enclosed_unpacked_expression(args[0]), " - ", |
| 8163 |
to_enclosed_unpacked_expression(args[1]), ")"), |
| 8164 |
should_forward(args[0]) && should_forward(args[1])); |
| 8165 |
inherit_expression_dependencies(id, args[0]); |
| 8166 |
inherit_expression_dependencies(id, args[1]); |
| 8167 |
} |
| 8168 |
else |
| 8169 |
CompilerGLSL::emit_glsl_op(result_type, id, eop, args, count); |
| 8170 |
break; |
| 8171 |
|
| 8172 |
case GLSLstd450Length: |
| 8173 |
// MSL does not support scalar versions here. |
| 8174 |
if (expression_type(args[0]).vecsize == 1) |
| 8175 |
{ |
| 8176 |
// Equivalent to abs(). |
| 8177 |
emit_unary_func_op(result_type, id, args[0], "abs"); |
| 8178 |
} |
| 8179 |
else |
| 8180 |
CompilerGLSL::emit_glsl_op(result_type, id, eop, args, count); |
| 8181 |
break; |
| 8182 |
|
| 8183 |
case GLSLstd450Normalize: |
| 8184 |
// MSL does not support scalar versions here. |
| 8185 |
if (expression_type(args[0]).vecsize == 1) |
| 8186 |
{ |
| 8187 |
// Returns -1 or 1 for valid input, sign() does the job. |
| 8188 |
emit_unary_func_op(result_type, id, args[0], "sign"); |
| 8189 |
} |
| 8190 |
else |
| 8191 |
CompilerGLSL::emit_glsl_op(result_type, id, eop, args, count); |
| 8192 |
break; |
| 8193 |
|
| 8194 |
case GLSLstd450Reflect: |
| 8195 |
if (get<SPIRType>(result_type).vecsize == 1) |
| 8196 |
emit_binary_func_op(result_type, id, args[0], args[1], "spvReflect"); |
| 8197 |
else |
| 8198 |
CompilerGLSL::emit_glsl_op(result_type, id, eop, args, count); |
| 8199 |
break; |
| 8200 |
|
| 8201 |
case GLSLstd450Refract: |
| 8202 |
if (get<SPIRType>(result_type).vecsize == 1) |
| 8203 |
emit_trinary_func_op(result_type, id, args[0], args[1], args[2], "spvRefract"); |
| 8204 |
else |
| 8205 |
CompilerGLSL::emit_glsl_op(result_type, id, eop, args, count); |
| 8206 |
break; |
| 8207 |
|
| 8208 |
case GLSLstd450FaceForward: |
| 8209 |
if (get<SPIRType>(result_type).vecsize == 1) |
| 8210 |
emit_trinary_func_op(result_type, id, args[0], args[1], args[2], "spvFaceForward"); |
| 8211 |
else |
| 8212 |
CompilerGLSL::emit_glsl_op(result_type, id, eop, args, count); |
| 8213 |
break; |
| 8214 |
|
| 8215 |
case GLSLstd450Modf: |
| 8216 |
case GLSLstd450Frexp: |
| 8217 |
{ |
| 8218 |
// Special case. If the variable is a scalar access chain, we cannot use it directly. We have to emit a temporary. |
| 8219 |
auto *ptr = maybe_get<SPIRExpression>(args[1]); |
| 8220 |
if (ptr && ptr->access_chain && is_scalar(expression_type(args[1]))) |
| 8221 |
{ |
| 8222 |
register_call_out_argument(args[1]); |
| 8223 |
forced_temporaries.insert(id); |
| 8224 |
|
| 8225 |
// Need to create temporaries and copy over to access chain after. |
| 8226 |
// We cannot directly take the reference of a vector swizzle in MSL, even if it's scalar ... |
| 8227 |
uint32_t &tmp_id = extra_sub_expressions[id]; |
| 8228 |
if (!tmp_id) |
| 8229 |
tmp_id = ir.increase_bound_by(1); |
| 8230 |
|
| 8231 |
uint32_t tmp_type_id = get_pointee_type_id(ptr->expression_type); |
| 8232 |
emit_uninitialized_temporary_expression(tmp_type_id, tmp_id); |
| 8233 |
emit_binary_func_op(result_type, id, args[0], tmp_id, eop == GLSLstd450Modf ? "modf" : "frexp"); |
| 8234 |
statement(to_expression(args[1]), " = ", to_expression(tmp_id), ";"); |
| 8235 |
} |
| 8236 |
else |
| 8237 |
CompilerGLSL::emit_glsl_op(result_type, id, eop, args, count); |
| 8238 |
break; |
| 8239 |
} |
| 8240 |
|
| 8241 |
default: |
| 8242 |
CompilerGLSL::emit_glsl_op(result_type, id, eop, args, count); |
| 8243 |
break; |
| 8244 |
} |
| 8245 |
} |
| 8246 |
|
| 8247 |
void CompilerMSL::emit_spv_amd_shader_trinary_minmax_op(uint32_t result_type, uint32_t id, uint32_t eop, |
| 8248 |
const uint32_t *args, uint32_t count) |
| 8249 |
{ |
| 8250 |
enum AMDShaderTrinaryMinMax |
| 8251 |
{ |
| 8252 |
FMin3AMD = 1, |
| 8253 |
UMin3AMD = 2, |
| 8254 |
SMin3AMD = 3, |
| 8255 |
FMax3AMD = 4, |
| 8256 |
UMax3AMD = 5, |
| 8257 |
SMax3AMD = 6, |
| 8258 |
FMid3AMD = 7, |
| 8259 |
UMid3AMD = 8, |
| 8260 |
SMid3AMD = 9 |
| 8261 |
}; |
| 8262 |
|
| 8263 |
if (!msl_options.supports_msl_version(2, 1)) |
| 8264 |
SPIRV_CROSS_THROW("Trinary min/max functions require MSL 2.1."); |
| 8265 |
|
| 8266 |
auto op = static_cast<AMDShaderTrinaryMinMax>(eop); |
| 8267 |
|
| 8268 |
switch (op) |
| 8269 |
{ |
| 8270 |
case FMid3AMD: |
| 8271 |
case UMid3AMD: |
| 8272 |
case SMid3AMD: |
| 8273 |
emit_trinary_func_op(result_type, id, args[0], args[1], args[2], "median3"); |
| 8274 |
break; |
| 8275 |
default: |
| 8276 |
CompilerGLSL::emit_spv_amd_shader_trinary_minmax_op(result_type, id, eop, args, count); |
| 8277 |
break; |
| 8278 |
} |
| 8279 |
} |
| 8280 |
|
| 8281 |
// Emit a structure declaration for the specified interface variable. |
| 8282 |
void CompilerMSL::emit_interface_block(uint32_t ib_var_id) |
| 8283 |
{ |
| 8284 |
if (ib_var_id) |
| 8285 |
{ |
| 8286 |
auto &ib_var = get<SPIRVariable>(ib_var_id); |
| 8287 |
auto &ib_type = get_variable_data_type(ib_var); |
| 8288 |
assert(ib_type.basetype == SPIRType::Struct && !ib_type.member_types.empty()); |
| 8289 |
emit_struct(ib_type); |
| 8290 |
} |
| 8291 |
} |
| 8292 |
|
| 8293 |
// Emits the declaration signature of the specified function. |
| 8294 |
// If this is the entry point function, Metal-specific return value and function arguments are added. |
| 8295 |
void CompilerMSL::emit_function_prototype(SPIRFunction &func, const Bitset &) |
| 8296 |
{ |
| 8297 |
if (func.self != ir.default_entry_point) |
| 8298 |
add_function_overload(func); |
| 8299 |
|
| 8300 |
local_variable_names = resource_names; |
| 8301 |
string decl; |
| 8302 |
|
| 8303 |
processing_entry_point = func.self == ir.default_entry_point; |
| 8304 |
|
| 8305 |
// Metal helper functions must be static force-inline otherwise they will cause problems when linked together in a single Metallib. |
| 8306 |
if (!processing_entry_point) |
| 8307 |
statement(force_inline); |
| 8308 |
|
| 8309 |
auto &type = get<SPIRType>(func.return_type); |
| 8310 |
|
| 8311 |
if (!type.array.empty() && msl_options.force_native_arrays) |
| 8312 |
{ |
| 8313 |
// We cannot return native arrays in MSL, so "return" through an out variable. |
| 8314 |
decl += "void"; |
| 8315 |
} |
| 8316 |
else |
| 8317 |
{ |
| 8318 |
decl += func_type_decl(type); |
| 8319 |
} |
| 8320 |
|
| 8321 |
decl += " "; |
| 8322 |
decl += to_name(func.self); |
| 8323 |
decl += "("; |
| 8324 |
|
| 8325 |
if (!type.array.empty() && msl_options.force_native_arrays) |
| 8326 |
{ |
| 8327 |
// Fake arrays returns by writing to an out array instead. |
| 8328 |
decl += "thread "; |
| 8329 |
decl += type_to_glsl(type); |
| 8330 |
decl += " (&SPIRV_Cross_return_value)"; |
| 8331 |
decl += type_to_array_glsl(type); |
| 8332 |
if (!func.arguments.empty()) |
| 8333 |
decl += ", "; |
| 8334 |
} |
| 8335 |
|
| 8336 |
if (processing_entry_point) |
| 8337 |
{ |
| 8338 |
if (msl_options.argument_buffers) |
| 8339 |
decl += entry_point_args_argument_buffer(!func.arguments.empty()); |
| 8340 |
else |
| 8341 |
decl += entry_point_args_classic(!func.arguments.empty()); |
| 8342 |
|
| 8343 |
// If entry point function has variables that require early declaration, |
| 8344 |
// ensure they each have an empty initializer, creating one if needed. |
| 8345 |
// This is done at this late stage because the initialization expression |
| 8346 |
// is cleared after each compilation pass. |
| 8347 |
for (auto var_id : vars_needing_early_declaration) |
| 8348 |
{ |
| 8349 |
auto &ed_var = get<SPIRVariable>(var_id); |
| 8350 |
ID &initializer = ed_var.initializer; |
| 8351 |
if (!initializer) |
| 8352 |
initializer = ir.increase_bound_by(1); |
| 8353 |
|
| 8354 |
// Do not override proper initializers. |
| 8355 |
if (ir.ids[initializer].get_type() == TypeNone || ir.ids[initializer].get_type() == TypeExpression) |
| 8356 |
set<SPIRExpression>(ed_var.initializer, "{}", ed_var.basetype, true); |
| 8357 |
} |
| 8358 |
} |
| 8359 |
|
| 8360 |
for (auto &arg : func.arguments) |
| 8361 |
{ |
| 8362 |
uint32_t name_id = arg.id; |
| 8363 |
|
| 8364 |
auto *var = maybe_get<SPIRVariable>(arg.id); |
| 8365 |
if (var) |
| 8366 |
{ |
| 8367 |
// If we need to modify the name of the variable, make sure we modify the original variable. |
| 8368 |
// Our alias is just a shadow variable. |
| 8369 |
if (arg.alias_global_variable && var->basevariable) |
| 8370 |
name_id = var->basevariable; |
| 8371 |
|
| 8372 |
var->parameter = &arg; // Hold a pointer to the parameter so we can invalidate the readonly field if needed. |
| 8373 |
} |
| 8374 |
|
| 8375 |
add_local_variable_name(name_id); |
| 8376 |
|
| 8377 |
decl += argument_decl(arg); |
| 8378 |
|
| 8379 |
bool is_dynamic_img_sampler = has_extended_decoration(arg.id, SPIRVCrossDecorationDynamicImageSampler); |
| 8380 |
|
| 8381 |
auto &arg_type = get<SPIRType>(arg.type); |
| 8382 |
if (arg_type.basetype == SPIRType::SampledImage && !is_dynamic_img_sampler) |
| 8383 |
{ |
| 8384 |
// Manufacture automatic plane args for multiplanar texture |
| 8385 |
uint32_t planes = 1; |
| 8386 |
if (auto *constexpr_sampler = find_constexpr_sampler(name_id)) |
| 8387 |
if (constexpr_sampler->ycbcr_conversion_enable) |
| 8388 |
planes = constexpr_sampler->planes; |
| 8389 |
for (uint32_t i = 1; i < planes; i++) |
| 8390 |
decl += join(", ", argument_decl(arg), plane_name_suffix, i); |
| 8391 |
|
| 8392 |
// Manufacture automatic sampler arg for SampledImage texture |
| 8393 |
if (arg_type.image.dim != DimBuffer) |
| 8394 |
decl += join(", thread const ", sampler_type(arg_type, arg.id), " ", to_sampler_expression(arg.id)); |
| 8395 |
} |
| 8396 |
|
| 8397 |
// Manufacture automatic swizzle arg. |
| 8398 |
if (msl_options.swizzle_texture_samples && has_sampled_images && is_sampled_image_type(arg_type) && |
| 8399 |
!is_dynamic_img_sampler) |
| 8400 |
{ |
| 8401 |
bool arg_is_array = !arg_type.array.empty(); |
| 8402 |
decl += join(", constant uint", arg_is_array ? "* " : "& ", to_swizzle_expression(arg.id)); |
| 8403 |
} |
| 8404 |
|
| 8405 |
if (buffers_requiring_array_length.count(name_id)) |
| 8406 |
{ |
| 8407 |
bool arg_is_array = !arg_type.array.empty(); |
| 8408 |
decl += join(", constant uint", arg_is_array ? "* " : "& ", to_buffer_size_expression(name_id)); |
| 8409 |
} |
| 8410 |
|
| 8411 |
if (&arg != &func.arguments.back()) |
| 8412 |
decl += ", "; |
| 8413 |
} |
| 8414 |
|
| 8415 |
decl += ")"; |
| 8416 |
statement(decl); |
| 8417 |
} |
| 8418 |
|
| 8419 |
static bool needs_chroma_reconstruction(const MSLConstexprSampler *constexpr_sampler) |
| 8420 |
{ |
| 8421 |
// For now, only multiplanar images need explicit reconstruction. GBGR and BGRG images |
| 8422 |
// use implicit reconstruction. |
| 8423 |
return constexpr_sampler && constexpr_sampler->ycbcr_conversion_enable && constexpr_sampler->planes > 1; |
| 8424 |
} |
| 8425 |
|
| 8426 |
// Returns the texture sampling function string for the specified image and sampling characteristics. |
| 8427 |
string CompilerMSL::to_function_name(const TextureFunctionNameArguments &args) |
| 8428 |
{ |
| 8429 |
VariableID img = args.base.img; |
| 8430 |
auto &imgtype = *args.base.imgtype; |
| 8431 |
|
| 8432 |
const MSLConstexprSampler *constexpr_sampler = nullptr; |
| 8433 |
bool is_dynamic_img_sampler = false; |
| 8434 |
if (auto *var = maybe_get_backing_variable(img)) |
| 8435 |
{ |
| 8436 |
constexpr_sampler = find_constexpr_sampler(var->basevariable ? var->basevariable : VariableID(var->self)); |
| 8437 |
is_dynamic_img_sampler = has_extended_decoration(var->self, SPIRVCrossDecorationDynamicImageSampler); |
| 8438 |
} |
| 8439 |
|
| 8440 |
// Special-case gather. We have to alter the component being looked up |
| 8441 |
// in the swizzle case. |
| 8442 |
if (msl_options.swizzle_texture_samples && args.base.is_gather && !is_dynamic_img_sampler && |
| 8443 |
(!constexpr_sampler || !constexpr_sampler->ycbcr_conversion_enable)) |
| 8444 |
{ |
| 8445 |
add_spv_func_and_recompile(imgtype.image.depth ? SPVFuncImplGatherCompareSwizzle : SPVFuncImplGatherSwizzle); |
| 8446 |
return imgtype.image.depth ? "spvGatherCompareSwizzle" : "spvGatherSwizzle"; |
| 8447 |
} |
| 8448 |
|
| 8449 |
auto *combined = maybe_get<SPIRCombinedImageSampler>(img); |
| 8450 |
|
| 8451 |
// Texture reference |
| 8452 |
string fname; |
| 8453 |
if (needs_chroma_reconstruction(constexpr_sampler) && !is_dynamic_img_sampler) |
| 8454 |
{ |
| 8455 |
if (constexpr_sampler->planes != 2 && constexpr_sampler->planes != 3) |
| 8456 |
SPIRV_CROSS_THROW("Unhandled number of color image planes!"); |
| 8457 |
// 444 images aren't downsampled, so we don't need to do linear filtering. |
| 8458 |
if (constexpr_sampler->resolution == MSL_FORMAT_RESOLUTION_444 || |
| 8459 |
constexpr_sampler->chroma_filter == MSL_SAMPLER_FILTER_NEAREST) |
| 8460 |
{ |
| 8461 |
if (constexpr_sampler->planes == 2) |
| 8462 |
add_spv_func_and_recompile(SPVFuncImplChromaReconstructNearest2Plane); |
| 8463 |
else |
| 8464 |
add_spv_func_and_recompile(SPVFuncImplChromaReconstructNearest3Plane); |
| 8465 |
fname = "spvChromaReconstructNearest"; |
| 8466 |
} |
| 8467 |
else // Linear with a downsampled format |
| 8468 |
{ |
| 8469 |
fname = "spvChromaReconstructLinear"; |
| 8470 |
switch (constexpr_sampler->resolution) |
| 8471 |
{ |
| 8472 |
case MSL_FORMAT_RESOLUTION_444: |
| 8473 |
assert(false); |
| 8474 |
break; // not reached |
| 8475 |
case MSL_FORMAT_RESOLUTION_422: |
| 8476 |
switch (constexpr_sampler->x_chroma_offset) |
| 8477 |
{ |
| 8478 |
case MSL_CHROMA_LOCATION_COSITED_EVEN: |
| 8479 |
if (constexpr_sampler->planes == 2) |
| 8480 |
add_spv_func_and_recompile(SPVFuncImplChromaReconstructLinear422CositedEven2Plane); |
| 8481 |
else |
| 8482 |
add_spv_func_and_recompile(SPVFuncImplChromaReconstructLinear422CositedEven3Plane); |
| 8483 |
fname += "422CositedEven"; |
| 8484 |
break; |
| 8485 |
case MSL_CHROMA_LOCATION_MIDPOINT: |
| 8486 |
if (constexpr_sampler->planes == 2) |
| 8487 |
add_spv_func_and_recompile(SPVFuncImplChromaReconstructLinear422Midpoint2Plane); |
| 8488 |
else |
| 8489 |
add_spv_func_and_recompile(SPVFuncImplChromaReconstructLinear422Midpoint3Plane); |
| 8490 |
fname += "422Midpoint"; |
| 8491 |
break; |
| 8492 |
default: |
| 8493 |
SPIRV_CROSS_THROW("Invalid chroma location."); |
| 8494 |
} |
| 8495 |
break; |
| 8496 |
case MSL_FORMAT_RESOLUTION_420: |
| 8497 |
fname += "420"; |
| 8498 |
switch (constexpr_sampler->x_chroma_offset) |
| 8499 |
{ |
| 8500 |
case MSL_CHROMA_LOCATION_COSITED_EVEN: |
| 8501 |
switch (constexpr_sampler->y_chroma_offset) |
| 8502 |
{ |
| 8503 |
case MSL_CHROMA_LOCATION_COSITED_EVEN: |
| 8504 |
if (constexpr_sampler->planes == 2) |
| 8505 |
add_spv_func_and_recompile( |
| 8506 |
SPVFuncImplChromaReconstructLinear420XCositedEvenYCositedEven2Plane); |
| 8507 |
else |
| 8508 |
add_spv_func_and_recompile( |
| 8509 |
SPVFuncImplChromaReconstructLinear420XCositedEvenYCositedEven3Plane); |
| 8510 |
fname += "XCositedEvenYCositedEven"; |
| 8511 |
break; |
| 8512 |
case MSL_CHROMA_LOCATION_MIDPOINT: |
| 8513 |
if (constexpr_sampler->planes == 2) |
| 8514 |
add_spv_func_and_recompile( |
| 8515 |
SPVFuncImplChromaReconstructLinear420XCositedEvenYMidpoint2Plane); |
| 8516 |
else |
| 8517 |
add_spv_func_and_recompile( |
| 8518 |
SPVFuncImplChromaReconstructLinear420XCositedEvenYMidpoint3Plane); |
| 8519 |
fname += "XCositedEvenYMidpoint"; |
| 8520 |
break; |
| 8521 |
default: |
| 8522 |
SPIRV_CROSS_THROW("Invalid Y chroma location."); |
| 8523 |
} |
| 8524 |
break; |
| 8525 |
case MSL_CHROMA_LOCATION_MIDPOINT: |
| 8526 |
switch (constexpr_sampler->y_chroma_offset) |
| 8527 |
{ |
| 8528 |
case MSL_CHROMA_LOCATION_COSITED_EVEN: |
| 8529 |
if (constexpr_sampler->planes == 2) |
| 8530 |
add_spv_func_and_recompile( |
| 8531 |
SPVFuncImplChromaReconstructLinear420XMidpointYCositedEven2Plane); |
| 8532 |
else |
| 8533 |
add_spv_func_and_recompile( |
| 8534 |
SPVFuncImplChromaReconstructLinear420XMidpointYCositedEven3Plane); |
| 8535 |
fname += "XMidpointYCositedEven"; |
| 8536 |
break; |
| 8537 |
case MSL_CHROMA_LOCATION_MIDPOINT: |
| 8538 |
if (constexpr_sampler->planes == 2) |
| 8539 |
add_spv_func_and_recompile(SPVFuncImplChromaReconstructLinear420XMidpointYMidpoint2Plane); |
| 8540 |
else |
| 8541 |
add_spv_func_and_recompile(SPVFuncImplChromaReconstructLinear420XMidpointYMidpoint3Plane); |
| 8542 |
fname += "XMidpointYMidpoint"; |
| 8543 |
break; |
| 8544 |
default: |
| 8545 |
SPIRV_CROSS_THROW("Invalid Y chroma location."); |
| 8546 |
} |
| 8547 |
break; |
| 8548 |
default: |
| 8549 |
SPIRV_CROSS_THROW("Invalid X chroma location."); |
| 8550 |
} |
| 8551 |
break; |
| 8552 |
default: |
| 8553 |
SPIRV_CROSS_THROW("Invalid format resolution."); |
| 8554 |
} |
| 8555 |
} |
| 8556 |
} |
| 8557 |
else |
| 8558 |
{ |
| 8559 |
fname = to_expression(combined ? combined->image : img) + "."; |
| 8560 |
|
| 8561 |
// Texture function and sampler |
| 8562 |
if (args.base.is_fetch) |
| 8563 |
fname += "read"; |
| 8564 |
else if (args.base.is_gather) |
| 8565 |
fname += "gather"; |
| 8566 |
else |
| 8567 |
fname += "sample"; |
| 8568 |
|
| 8569 |
if (args.has_dref) |
| 8570 |
fname += "_compare"; |
| 8571 |
} |
| 8572 |
|
| 8573 |
return fname; |
| 8574 |
} |
| 8575 |
|
| 8576 |
string CompilerMSL::convert_to_f32(const string &expr, uint32_t components) |
| 8577 |
{ |
| 8578 |
SPIRType t; |
| 8579 |
t.basetype = SPIRType::Float; |
| 8580 |
t.vecsize = components; |
| 8581 |
t.columns = 1; |
| 8582 |
return join(type_to_glsl_constructor(t), "(", expr, ")"); |
| 8583 |
} |
| 8584 |
|
| 8585 |
static inline bool sampling_type_needs_f32_conversion(const SPIRType &type) |
| 8586 |
{ |
| 8587 |
// Double is not supported to begin with, but doesn't hurt to check for completion. |
| 8588 |
return type.basetype == SPIRType::Half || type.basetype == SPIRType::Double; |
| 8589 |
} |
| 8590 |
|
| 8591 |
// Returns the function args for a texture sampling function for the specified image and sampling characteristics. |
| 8592 |
string CompilerMSL::to_function_args(const TextureFunctionArguments &args, bool *p_forward) |
| 8593 |
{ |
| 8594 |
VariableID img = args.base.img; |
| 8595 |
auto &imgtype = *args.base.imgtype; |
| 8596 |
uint32_t lod = args.lod; |
| 8597 |
uint32_t grad_x = args.grad_x; |
| 8598 |
uint32_t grad_y = args.grad_y; |
| 8599 |
uint32_t bias = args.bias; |
| 8600 |
|
| 8601 |
const MSLConstexprSampler *constexpr_sampler = nullptr; |
| 8602 |
bool is_dynamic_img_sampler = false; |
| 8603 |
if (auto *var = maybe_get_backing_variable(img)) |
| 8604 |
{ |
| 8605 |
constexpr_sampler = find_constexpr_sampler(var->basevariable ? var->basevariable : VariableID(var->self)); |
| 8606 |
is_dynamic_img_sampler = has_extended_decoration(var->self, SPIRVCrossDecorationDynamicImageSampler); |
| 8607 |
} |
| 8608 |
|
| 8609 |
string farg_str; |
| 8610 |
bool forward = true; |
| 8611 |
|
| 8612 |
if (!is_dynamic_img_sampler) |
| 8613 |
{ |
| 8614 |
// Texture reference (for some cases) |
| 8615 |
if (needs_chroma_reconstruction(constexpr_sampler)) |
| 8616 |
{ |
| 8617 |
// Multiplanar images need two or three textures. |
| 8618 |
farg_str += to_expression(img); |
| 8619 |
for (uint32_t i = 1; i < constexpr_sampler->planes; i++) |
| 8620 |
farg_str += join(", ", to_expression(img), plane_name_suffix, i); |
| 8621 |
} |
| 8622 |
else if ((!constexpr_sampler || !constexpr_sampler->ycbcr_conversion_enable) && |
| 8623 |
msl_options.swizzle_texture_samples && args.base.is_gather) |
| 8624 |
{ |
| 8625 |
auto *combined = maybe_get<SPIRCombinedImageSampler>(img); |
| 8626 |
farg_str += to_expression(combined ? combined->image : img); |
| 8627 |
} |
| 8628 |
|
| 8629 |
// Sampler reference |
| 8630 |
if (!args.base.is_fetch) |
| 8631 |
{ |
| 8632 |
if (!farg_str.empty()) |
| 8633 |
farg_str += ", "; |
| 8634 |
farg_str += to_sampler_expression(img); |
| 8635 |
} |
| 8636 |
|
| 8637 |
if ((!constexpr_sampler || !constexpr_sampler->ycbcr_conversion_enable) && |
| 8638 |
msl_options.swizzle_texture_samples && args.base.is_gather) |
| 8639 |
{ |
| 8640 |
// Add the swizzle constant from the swizzle buffer. |
| 8641 |
farg_str += ", " + to_swizzle_expression(img); |
| 8642 |
used_swizzle_buffer = true; |
| 8643 |
} |
| 8644 |
|
| 8645 |
// Swizzled gather puts the component before the other args, to allow template |
| 8646 |
// deduction to work. |
| 8647 |
if (args.component && msl_options.swizzle_texture_samples) |
| 8648 |
{ |
| 8649 |
forward = should_forward(args.component); |
| 8650 |
farg_str += ", " + to_component_argument(args.component); |
| 8651 |
} |
| 8652 |
} |
| 8653 |
|
| 8654 |
// Texture coordinates |
| 8655 |
forward = forward && should_forward(args.coord); |
| 8656 |
auto coord_expr = to_enclosed_expression(args.coord); |
| 8657 |
auto &coord_type = expression_type(args.coord); |
| 8658 |
bool coord_is_fp = type_is_floating_point(coord_type); |
| 8659 |
bool is_cube_fetch = false; |
| 8660 |
|
| 8661 |
string tex_coords = coord_expr; |
| 8662 |
uint32_t alt_coord_component = 0; |
| 8663 |
|
| 8664 |
switch (imgtype.image.dim) |
| 8665 |
{ |
| 8666 |
|
| 8667 |
case Dim1D: |
| 8668 |
if (coord_type.vecsize > 1) |
| 8669 |
tex_coords = enclose_expression(tex_coords) + ".x"; |
| 8670 |
|
| 8671 |
if (args.base.is_fetch) |
| 8672 |
tex_coords = "uint(" + round_fp_tex_coords(tex_coords, coord_is_fp) + ")"; |
| 8673 |
else if (sampling_type_needs_f32_conversion(coord_type)) |
| 8674 |
tex_coords = convert_to_f32(tex_coords, 1); |
| 8675 |
|
| 8676 |
if (msl_options.texture_1D_as_2D) |
| 8677 |
{ |
| 8678 |
if (args.base.is_fetch) |
| 8679 |
tex_coords = "uint2(" + tex_coords + ", 0)"; |
| 8680 |
else |
| 8681 |
tex_coords = "float2(" + tex_coords + ", 0.5)"; |
| 8682 |
} |
| 8683 |
|
| 8684 |
alt_coord_component = 1; |
| 8685 |
break; |
| 8686 |
|
| 8687 |
case DimBuffer: |
| 8688 |
if (coord_type.vecsize > 1) |
| 8689 |
tex_coords = enclose_expression(tex_coords) + ".x"; |
| 8690 |
|
| 8691 |
if (msl_options.texture_buffer_native) |
| 8692 |
{ |
| 8693 |
tex_coords = "uint(" + round_fp_tex_coords(tex_coords, coord_is_fp) + ")"; |
| 8694 |
} |
| 8695 |
else |
| 8696 |
{ |
| 8697 |
// Metal texel buffer textures are 2D, so convert 1D coord to 2D. |
| 8698 |
// Support for Metal 2.1's new texture_buffer type. |
| 8699 |
if (args.base.is_fetch) |
| 8700 |
{ |
| 8701 |
if (msl_options.texel_buffer_texture_width > 0) |
| 8702 |
{ |
| 8703 |
tex_coords = "spvTexelBufferCoord(" + round_fp_tex_coords(tex_coords, coord_is_fp) + ")"; |
| 8704 |
} |
| 8705 |
else |
| 8706 |
{ |
| 8707 |
tex_coords = "spvTexelBufferCoord(" + round_fp_tex_coords(tex_coords, coord_is_fp) + ", " + |
| 8708 |
to_expression(img) + ")"; |
| 8709 |
} |
| 8710 |
} |
| 8711 |
} |
| 8712 |
|
| 8713 |
alt_coord_component = 1; |
| 8714 |
break; |
| 8715 |
|
| 8716 |
case DimSubpassData: |
| 8717 |
// If we're using Metal's native frame-buffer fetch API for subpass inputs, |
| 8718 |
// this path will not be hit. |
| 8719 |
tex_coords = "uint2(gl_FragCoord.xy)"; |
| 8720 |
alt_coord_component = 2; |
| 8721 |
break; |
| 8722 |
|
| 8723 |
case Dim2D: |
| 8724 |
if (coord_type.vecsize > 2) |
| 8725 |
tex_coords = enclose_expression(tex_coords) + ".xy"; |
| 8726 |
|
| 8727 |
if (args.base.is_fetch) |
| 8728 |
tex_coords = "uint2(" + round_fp_tex_coords(tex_coords, coord_is_fp) + ")"; |
| 8729 |
else if (sampling_type_needs_f32_conversion(coord_type)) |
| 8730 |
tex_coords = convert_to_f32(tex_coords, 2); |
| 8731 |
|
| 8732 |
alt_coord_component = 2; |
| 8733 |
break; |
| 8734 |
|
| 8735 |
case Dim3D: |
| 8736 |
if (coord_type.vecsize > 3) |
| 8737 |
tex_coords = enclose_expression(tex_coords) + ".xyz"; |
| 8738 |
|
| 8739 |
if (args.base.is_fetch) |
| 8740 |
tex_coords = "uint3(" + round_fp_tex_coords(tex_coords, coord_is_fp) + ")"; |
| 8741 |
else if (sampling_type_needs_f32_conversion(coord_type)) |
| 8742 |
tex_coords = convert_to_f32(tex_coords, 3); |
| 8743 |
|
| 8744 |
alt_coord_component = 3; |
| 8745 |
break; |
| 8746 |
|
| 8747 |
case DimCube: |
| 8748 |
if (args.base.is_fetch) |
| 8749 |
{ |
| 8750 |
is_cube_fetch = true; |
| 8751 |
tex_coords += ".xy"; |
| 8752 |
tex_coords = "uint2(" + round_fp_tex_coords(tex_coords, coord_is_fp) + ")"; |
| 8753 |
} |
| 8754 |
else |
| 8755 |
{ |
| 8756 |
if (coord_type.vecsize > 3) |
| 8757 |
tex_coords = enclose_expression(tex_coords) + ".xyz"; |
| 8758 |
} |
| 8759 |
|
| 8760 |
if (sampling_type_needs_f32_conversion(coord_type)) |
| 8761 |
tex_coords = convert_to_f32(tex_coords, 3); |
| 8762 |
|
| 8763 |
alt_coord_component = 3; |
| 8764 |
break; |
| 8765 |
|
| 8766 |
default: |
| 8767 |
break; |
| 8768 |
} |
| 8769 |
|
| 8770 |
if (args.base.is_fetch && (args.offset || args.coffset)) |
| 8771 |
{ |
| 8772 |
uint32_t offset_expr = args.offset ? args.offset : args.coffset; |
| 8773 |
// Fetch offsets must be applied directly to the coordinate. |
| 8774 |
forward = forward && should_forward(offset_expr); |
| 8775 |
auto &type = expression_type(offset_expr); |
| 8776 |
if (imgtype.image.dim == Dim1D && msl_options.texture_1D_as_2D) |
| 8777 |
{ |
| 8778 |
if (type.basetype != SPIRType::UInt) |
| 8779 |
tex_coords += join(" + uint2(", bitcast_expression(SPIRType::UInt, offset_expr), ", 0)"); |
| 8780 |
else |
| 8781 |
tex_coords += join(" + uint2(", to_enclosed_expression(offset_expr), ", 0)"); |
| 8782 |
} |
| 8783 |
else |
| 8784 |
{ |
| 8785 |
if (type.basetype != SPIRType::UInt) |
| 8786 |
tex_coords += " + " + bitcast_expression(SPIRType::UInt, offset_expr); |
| 8787 |
else |
| 8788 |
tex_coords += " + " + to_enclosed_expression(offset_expr); |
| 8789 |
} |
| 8790 |
} |
| 8791 |
|
| 8792 |
// If projection, use alt coord as divisor |
| 8793 |
if (args.base.is_proj) |
| 8794 |
{ |
| 8795 |
if (sampling_type_needs_f32_conversion(coord_type)) |
| 8796 |
tex_coords += " / " + convert_to_f32(to_extract_component_expression(args.coord, alt_coord_component), 1); |
| 8797 |
else |
| 8798 |
tex_coords += " / " + to_extract_component_expression(args.coord, alt_coord_component); |
| 8799 |
} |
| 8800 |
|
| 8801 |
if (!farg_str.empty()) |
| 8802 |
farg_str += ", "; |
| 8803 |
|
| 8804 |
if (imgtype.image.dim == DimCube && imgtype.image.arrayed && msl_options.emulate_cube_array) |
| 8805 |
{ |
| 8806 |
farg_str += "spvCubemapTo2DArrayFace(" + tex_coords + ").xy"; |
| 8807 |
|
| 8808 |
if (is_cube_fetch) |
| 8809 |
farg_str += ", uint(" + to_extract_component_expression(args.coord, 2) + ")"; |
| 8810 |
else |
| 8811 |
farg_str += |
| 8812 |
", uint(spvCubemapTo2DArrayFace(" + tex_coords + ").z) + (uint(" + |
| 8813 |
round_fp_tex_coords(to_extract_component_expression(args.coord, alt_coord_component), coord_is_fp) + |
| 8814 |
") * 6u)"; |
| 8815 |
|
| 8816 |
add_spv_func_and_recompile(SPVFuncImplCubemapTo2DArrayFace); |
| 8817 |
} |
| 8818 |
else |
| 8819 |
{ |
| 8820 |
farg_str += tex_coords; |
| 8821 |
|
| 8822 |
// If fetch from cube, add face explicitly |
| 8823 |
if (is_cube_fetch) |
| 8824 |
{ |
| 8825 |
// Special case for cube arrays, face and layer are packed in one dimension. |
| 8826 |
if (imgtype.image.arrayed) |
| 8827 |
farg_str += ", uint(" + to_extract_component_expression(args.coord, 2) + ") % 6u"; |
| 8828 |
else |
| 8829 |
farg_str += |
| 8830 |
", uint(" + round_fp_tex_coords(to_extract_component_expression(args.coord, 2), coord_is_fp) + ")"; |
| 8831 |
} |
| 8832 |
|
| 8833 |
// If array, use alt coord |
| 8834 |
if (imgtype.image.arrayed) |
| 8835 |
{ |
| 8836 |
// Special case for cube arrays, face and layer are packed in one dimension. |
| 8837 |
if (imgtype.image.dim == DimCube && args.base.is_fetch) |
| 8838 |
{ |
| 8839 |
farg_str += ", uint(" + to_extract_component_expression(args.coord, 2) + ") / 6u"; |
| 8840 |
} |
| 8841 |
else |
| 8842 |
{ |
| 8843 |
farg_str += |
| 8844 |
", uint(" + |
| 8845 |
round_fp_tex_coords(to_extract_component_expression(args.coord, alt_coord_component), coord_is_fp) + |
| 8846 |
")"; |
| 8847 |
if (imgtype.image.dim == DimSubpassData) |
| 8848 |
{ |
| 8849 |
if (msl_options.multiview) |
| 8850 |
farg_str += " + gl_ViewIndex"; |
| 8851 |
else if (msl_options.arrayed_subpass_input) |
| 8852 |
farg_str += " + gl_Layer"; |
| 8853 |
} |
| 8854 |
} |
| 8855 |
} |
| 8856 |
else if (imgtype.image.dim == DimSubpassData) |
| 8857 |
{ |
| 8858 |
if (msl_options.multiview) |
| 8859 |
farg_str += ", gl_ViewIndex"; |
| 8860 |
else if (msl_options.arrayed_subpass_input) |
| 8861 |
farg_str += ", gl_Layer"; |
| 8862 |
} |
| 8863 |
} |
| 8864 |
|
| 8865 |
// Depth compare reference value |
| 8866 |
if (args.dref) |
| 8867 |
{ |
| 8868 |
forward = forward && should_forward(args.dref); |
| 8869 |
farg_str += ", "; |
| 8870 |
|
| 8871 |
auto &dref_type = expression_type(args.dref); |
| 8872 |
|
| 8873 |
string dref_expr; |
| 8874 |
if (args.base.is_proj) |
| 8875 |
dref_expr = join(to_enclosed_expression(args.dref), " / ", |
| 8876 |
to_extract_component_expression(args.coord, alt_coord_component)); |
| 8877 |
else |
| 8878 |
dref_expr = to_expression(args.dref); |
| 8879 |
|
| 8880 |
if (sampling_type_needs_f32_conversion(dref_type)) |
| 8881 |
dref_expr = convert_to_f32(dref_expr, 1); |
| 8882 |
|
| 8883 |
farg_str += dref_expr; |
| 8884 |
|
| 8885 |
if (msl_options.is_macos() && (grad_x || grad_y)) |
| 8886 |
{ |
| 8887 |
// For sample compare, MSL does not support gradient2d for all targets (only iOS apparently according to docs). |
| 8888 |
// However, the most common case here is to have a constant gradient of 0, as that is the only way to express |
| 8889 |
// LOD == 0 in GLSL with sampler2DArrayShadow (cascaded shadow mapping). |
| 8890 |
// We will detect a compile-time constant 0 value for gradient and promote that to level(0) on MSL. |
| 8891 |
bool constant_zero_x = !grad_x || expression_is_constant_null(grad_x); |
| 8892 |
bool constant_zero_y = !grad_y || expression_is_constant_null(grad_y); |
| 8893 |
if (constant_zero_x && constant_zero_y) |
| 8894 |
{ |
| 8895 |
lod = 0; |
| 8896 |
grad_x = 0; |
| 8897 |
grad_y = 0; |
| 8898 |
farg_str += ", level(0)"; |
| 8899 |
} |
| 8900 |
else if (!msl_options.supports_msl_version(2, 3)) |
| 8901 |
{ |
| 8902 |
SPIRV_CROSS_THROW("Using non-constant 0.0 gradient() qualifier for sample_compare. This is not " |
| 8903 |
"supported on macOS prior to MSL 2.3."); |
| 8904 |
} |
| 8905 |
} |
| 8906 |
|
| 8907 |
if (msl_options.is_macos() && bias) |
| 8908 |
{ |
| 8909 |
// Bias is not supported either on macOS with sample_compare. |
| 8910 |
// Verify it is compile-time zero, and drop the argument. |
| 8911 |
if (expression_is_constant_null(bias)) |
| 8912 |
{ |
| 8913 |
bias = 0; |
| 8914 |
} |
| 8915 |
else if (!msl_options.supports_msl_version(2, 3)) |
| 8916 |
{ |
| 8917 |
SPIRV_CROSS_THROW("Using non-constant 0.0 bias() qualifier for sample_compare. This is not supported " |
| 8918 |
"on macOS prior to MSL 2.3."); |
| 8919 |
} |
| 8920 |
} |
| 8921 |
} |
| 8922 |
|
| 8923 |
// LOD Options |
| 8924 |
// Metal does not support LOD for 1D textures. |
| 8925 |
if (bias && (imgtype.image.dim != Dim1D || msl_options.texture_1D_as_2D)) |
| 8926 |
{ |
| 8927 |
forward = forward && should_forward(bias); |
| 8928 |
farg_str += ", bias(" + to_expression(bias) + ")"; |
| 8929 |
} |
| 8930 |
|
| 8931 |
// Metal does not support LOD for 1D textures. |
| 8932 |
if (lod && (imgtype.image.dim != Dim1D || msl_options.texture_1D_as_2D)) |
| 8933 |
{ |
| 8934 |
forward = forward && should_forward(lod); |
| 8935 |
if (args.base.is_fetch) |
| 8936 |
{ |
| 8937 |
farg_str += ", " + to_expression(lod); |
| 8938 |
} |
| 8939 |
else |
| 8940 |
{ |
| 8941 |
farg_str += ", level(" + to_expression(lod) + ")"; |
| 8942 |
} |
| 8943 |
} |
| 8944 |
else if (args.base.is_fetch && !lod && (imgtype.image.dim != Dim1D || msl_options.texture_1D_as_2D) && |
| 8945 |
imgtype.image.dim != DimBuffer && !imgtype.image.ms && imgtype.image.sampled != 2) |
| 8946 |
{ |
| 8947 |
// Lod argument is optional in OpImageFetch, but we require a LOD value, pick 0 as the default. |
| 8948 |
// Check for sampled type as well, because is_fetch is also used for OpImageRead in MSL. |
| 8949 |
farg_str += ", 0"; |
| 8950 |
} |
| 8951 |
|
| 8952 |
// Metal does not support LOD for 1D textures. |
| 8953 |
if ((grad_x || grad_y) && (imgtype.image.dim != Dim1D || msl_options.texture_1D_as_2D)) |
| 8954 |
{ |
| 8955 |
forward = forward && should_forward(grad_x); |
| 8956 |
forward = forward && should_forward(grad_y); |
| 8957 |
string grad_opt; |
| 8958 |
switch (imgtype.image.dim) |
| 8959 |
{ |
| 8960 |
case Dim1D: |
| 8961 |
case Dim2D: |
| 8962 |
grad_opt = "2d"; |
| 8963 |
break; |
| 8964 |
case Dim3D: |
| 8965 |
grad_opt = "3d"; |
| 8966 |
break; |
| 8967 |
case DimCube: |
| 8968 |
if (imgtype.image.arrayed && msl_options.emulate_cube_array) |
| 8969 |
grad_opt = "2d"; |
| 8970 |
else |
| 8971 |
grad_opt = "cube"; |
| 8972 |
break; |
| 8973 |
default: |
| 8974 |
grad_opt = "unsupported_gradient_dimension"; |
| 8975 |
break; |
| 8976 |
} |
| 8977 |
farg_str += ", gradient" + grad_opt + "(" + to_expression(grad_x) + ", " + to_expression(grad_y) + ")"; |
| 8978 |
} |
| 8979 |
|
| 8980 |
if (args.min_lod) |
| 8981 |
{ |
| 8982 |
if (msl_options.is_macos()) |
| 8983 |
{ |
| 8984 |
if (!msl_options.supports_msl_version(2, 2)) |
| 8985 |
SPIRV_CROSS_THROW("min_lod_clamp() is only supported in MSL 2.2+ and up on macOS."); |
| 8986 |
} |
| 8987 |
else if (msl_options.is_ios()) |
| 8988 |
SPIRV_CROSS_THROW("min_lod_clamp() is not supported on iOS."); |
| 8989 |
|
| 8990 |
forward = forward && should_forward(args.min_lod); |
| 8991 |
farg_str += ", min_lod_clamp(" + to_expression(args.min_lod) + ")"; |
| 8992 |
} |
| 8993 |
|
| 8994 |
// Add offsets |
| 8995 |
string offset_expr; |
| 8996 |
const SPIRType *offset_type = nullptr; |
| 8997 |
if (args.coffset && !args.base.is_fetch) |
| 8998 |
{ |
| 8999 |
forward = forward && should_forward(args.coffset); |
| 9000 |
offset_expr = to_expression(args.coffset); |
| 9001 |
offset_type = &expression_type(args.coffset); |
| 9002 |
} |
| 9003 |
else if (args.offset && !args.base.is_fetch) |
| 9004 |
{ |
| 9005 |
forward = forward && should_forward(args.offset); |
| 9006 |
offset_expr = to_expression(args.offset); |
| 9007 |
offset_type = &expression_type(args.offset); |
| 9008 |
} |
| 9009 |
|
| 9010 |
if (!offset_expr.empty()) |
| 9011 |
{ |
| 9012 |
switch (imgtype.image.dim) |
| 9013 |
{ |
| 9014 |
case Dim1D: |
| 9015 |
if (!msl_options.texture_1D_as_2D) |
| 9016 |
break; |
| 9017 |
if (offset_type->vecsize > 1) |
| 9018 |
offset_expr = enclose_expression(offset_expr) + ".x"; |
| 9019 |
|
| 9020 |
farg_str += join(", int2(", offset_expr, ", 0)"); |
| 9021 |
break; |
| 9022 |
|
| 9023 |
case Dim2D: |
| 9024 |
if (offset_type->vecsize > 2) |
| 9025 |
offset_expr = enclose_expression(offset_expr) + ".xy"; |
| 9026 |
|
| 9027 |
farg_str += ", " + offset_expr; |
| 9028 |
break; |
| 9029 |
|
| 9030 |
case Dim3D: |
| 9031 |
if (offset_type->vecsize > 3) |
| 9032 |
offset_expr = enclose_expression(offset_expr) + ".xyz"; |
| 9033 |
|
| 9034 |
farg_str += ", " + offset_expr; |
| 9035 |
break; |
| 9036 |
|
| 9037 |
default: |
| 9038 |
break; |
| 9039 |
} |
| 9040 |
} |
| 9041 |
|
| 9042 |
if (args.component) |
| 9043 |
{ |
| 9044 |
// If 2D has gather component, ensure it also has an offset arg |
| 9045 |
if (imgtype.image.dim == Dim2D && offset_expr.empty()) |
| 9046 |
farg_str += ", int2(0)"; |
| 9047 |
|
| 9048 |
if (!msl_options.swizzle_texture_samples || is_dynamic_img_sampler) |
| 9049 |
{ |
| 9050 |
forward = forward && should_forward(args.component); |
| 9051 |
|
| 9052 |
uint32_t image_var = 0; |
| 9053 |
if (const auto *combined = maybe_get<SPIRCombinedImageSampler>(img)) |
| 9054 |
{ |
| 9055 |
if (const auto *img_var = maybe_get_backing_variable(combined->image)) |
| 9056 |
image_var = img_var->self; |
| 9057 |
} |
| 9058 |
else if (const auto *var = maybe_get_backing_variable(img)) |
| 9059 |
{ |
| 9060 |
image_var = var->self; |
| 9061 |
} |
| 9062 |
|
| 9063 |
if (image_var == 0 || !image_is_comparison(expression_type(image_var), image_var)) |
| 9064 |
farg_str += ", " + to_component_argument(args.component); |
| 9065 |
} |
| 9066 |
} |
| 9067 |
|
| 9068 |
if (args.sample) |
| 9069 |
{ |
| 9070 |
forward = forward && should_forward(args.sample); |
| 9071 |
farg_str += ", "; |
| 9072 |
farg_str += to_expression(args.sample); |
| 9073 |
} |
| 9074 |
|
| 9075 |
*p_forward = forward; |
| 9076 |
|
| 9077 |
return farg_str; |
| 9078 |
} |
| 9079 |
|
| 9080 |
// If the texture coordinates are floating point, invokes MSL round() function to round them. |
| 9081 |
string CompilerMSL::round_fp_tex_coords(string tex_coords, bool coord_is_fp) |
| 9082 |
{ |
| 9083 |
return coord_is_fp ? ("round(" + tex_coords + ")") : tex_coords; |
| 9084 |
} |
| 9085 |
|
| 9086 |
// Returns a string to use in an image sampling function argument. |
| 9087 |
// The ID must be a scalar constant. |
| 9088 |
string CompilerMSL::to_component_argument(uint32_t id) |
| 9089 |
{ |
| 9090 |
uint32_t component_index = evaluate_constant_u32(id); |
| 9091 |
switch (component_index) |
| 9092 |
{ |
| 9093 |
case 0: |
| 9094 |
return "component::x"; |
| 9095 |
case 1: |
| 9096 |
return "component::y"; |
| 9097 |
case 2: |
| 9098 |
return "component::z"; |
| 9099 |
case 3: |
| 9100 |
return "component::w"; |
| 9101 |
|
| 9102 |
default: |
| 9103 |
SPIRV_CROSS_THROW("The value (" + to_string(component_index) + ") of OpConstant ID " + to_string(id) + |
| 9104 |
" is not a valid Component index, which must be one of 0, 1, 2, or 3."); |
| 9105 |
return "component::x"; |
| 9106 |
} |
| 9107 |
} |
| 9108 |
|
| 9109 |
// Establish sampled image as expression object and assign the sampler to it. |
| 9110 |
void CompilerMSL::emit_sampled_image_op(uint32_t result_type, uint32_t result_id, uint32_t image_id, uint32_t samp_id) |
| 9111 |
{ |
| 9112 |
set<SPIRCombinedImageSampler>(result_id, result_type, image_id, samp_id); |
| 9113 |
} |
| 9114 |
|
| 9115 |
string CompilerMSL::to_texture_op(const Instruction &i, bool sparse, bool *forward, |
| 9116 |
SmallVector<uint32_t> &inherited_expressions) |
| 9117 |
{ |
| 9118 |
auto *ops = stream(i); |
| 9119 |
uint32_t result_type_id = ops[0]; |
| 9120 |
uint32_t img = ops[2]; |
| 9121 |
auto &result_type = get<SPIRType>(result_type_id); |
| 9122 |
auto op = static_cast<Op>(i.op); |
| 9123 |
bool is_gather = (op == OpImageGather || op == OpImageDrefGather); |
| 9124 |
|
| 9125 |
// Bypass pointers because we need the real image struct |
| 9126 |
auto &type = expression_type(img); |
| 9127 |
auto &imgtype = get<SPIRType>(type.self); |
| 9128 |
|
| 9129 |
const MSLConstexprSampler *constexpr_sampler = nullptr; |
| 9130 |
bool is_dynamic_img_sampler = false; |
| 9131 |
if (auto *var = maybe_get_backing_variable(img)) |
| 9132 |
{ |
| 9133 |
constexpr_sampler = find_constexpr_sampler(var->basevariable ? var->basevariable : VariableID(var->self)); |
| 9134 |
is_dynamic_img_sampler = has_extended_decoration(var->self, SPIRVCrossDecorationDynamicImageSampler); |
| 9135 |
} |
| 9136 |
|
| 9137 |
string expr; |
| 9138 |
if (constexpr_sampler && constexpr_sampler->ycbcr_conversion_enable && !is_dynamic_img_sampler) |
| 9139 |
{ |
| 9140 |
// If this needs sampler Y'CbCr conversion, we need to do some additional |
| 9141 |
// processing. |
| 9142 |
switch (constexpr_sampler->ycbcr_model) |
| 9143 |
{ |
| 9144 |
case MSL_SAMPLER_YCBCR_MODEL_CONVERSION_RGB_IDENTITY: |
| 9145 |
case MSL_SAMPLER_YCBCR_MODEL_CONVERSION_YCBCR_IDENTITY: |
| 9146 |
// Default |
| 9147 |
break; |
| 9148 |
case MSL_SAMPLER_YCBCR_MODEL_CONVERSION_YCBCR_BT_709: |
| 9149 |
add_spv_func_and_recompile(SPVFuncImplConvertYCbCrBT709); |
| 9150 |
expr += "spvConvertYCbCrBT709("; |
| 9151 |
break; |
| 9152 |
case MSL_SAMPLER_YCBCR_MODEL_CONVERSION_YCBCR_BT_601: |
| 9153 |
add_spv_func_and_recompile(SPVFuncImplConvertYCbCrBT601); |
| 9154 |
expr += "spvConvertYCbCrBT601("; |
| 9155 |
break; |
| 9156 |
case MSL_SAMPLER_YCBCR_MODEL_CONVERSION_YCBCR_BT_2020: |
| 9157 |
add_spv_func_and_recompile(SPVFuncImplConvertYCbCrBT2020); |
| 9158 |
expr += "spvConvertYCbCrBT2020("; |
| 9159 |
break; |
| 9160 |
default: |
| 9161 |
SPIRV_CROSS_THROW("Invalid Y'CbCr model conversion."); |
| 9162 |
} |
| 9163 |
|
| 9164 |
if (constexpr_sampler->ycbcr_model != MSL_SAMPLER_YCBCR_MODEL_CONVERSION_RGB_IDENTITY) |
| 9165 |
{ |
| 9166 |
switch (constexpr_sampler->ycbcr_range) |
| 9167 |
{ |
| 9168 |
case MSL_SAMPLER_YCBCR_RANGE_ITU_FULL: |
| 9169 |
add_spv_func_and_recompile(SPVFuncImplExpandITUFullRange); |
| 9170 |
expr += "spvExpandITUFullRange("; |
| 9171 |
break; |
| 9172 |
case MSL_SAMPLER_YCBCR_RANGE_ITU_NARROW: |
| 9173 |
add_spv_func_and_recompile(SPVFuncImplExpandITUNarrowRange); |
| 9174 |
expr += "spvExpandITUNarrowRange("; |
| 9175 |
break; |
| 9176 |
default: |
| 9177 |
SPIRV_CROSS_THROW("Invalid Y'CbCr range."); |
| 9178 |
} |
| 9179 |
} |
| 9180 |
} |
| 9181 |
else if (msl_options.swizzle_texture_samples && !is_gather && is_sampled_image_type(imgtype) && |
| 9182 |
!is_dynamic_img_sampler) |
| 9183 |
{ |
| 9184 |
add_spv_func_and_recompile(SPVFuncImplTextureSwizzle); |
| 9185 |
expr += "spvTextureSwizzle("; |
| 9186 |
} |
| 9187 |
|
| 9188 |
string inner_expr = CompilerGLSL::to_texture_op(i, sparse, forward, inherited_expressions); |
| 9189 |
|
| 9190 |
if (constexpr_sampler && constexpr_sampler->ycbcr_conversion_enable && !is_dynamic_img_sampler) |
| 9191 |
{ |
| 9192 |
if (!constexpr_sampler->swizzle_is_identity()) |
| 9193 |
{ |
| 9194 |
static const char swizzle_names[] = "rgba"; |
| 9195 |
if (!constexpr_sampler->swizzle_has_one_or_zero()) |
| 9196 |
{ |
| 9197 |
// If we can, do it inline. |
| 9198 |
expr += inner_expr + "."; |
| 9199 |
for (uint32_t c = 0; c < 4; c++) |
| 9200 |
{ |
| 9201 |
switch (constexpr_sampler->swizzle[c]) |
| 9202 |
{ |
| 9203 |
case MSL_COMPONENT_SWIZZLE_IDENTITY: |
| 9204 |
expr += swizzle_names[c]; |
| 9205 |
break; |
| 9206 |
case MSL_COMPONENT_SWIZZLE_R: |
| 9207 |
case MSL_COMPONENT_SWIZZLE_G: |
| 9208 |
case MSL_COMPONENT_SWIZZLE_B: |
| 9209 |
case MSL_COMPONENT_SWIZZLE_A: |
| 9210 |
expr += swizzle_names[constexpr_sampler->swizzle[c] - MSL_COMPONENT_SWIZZLE_R]; |
| 9211 |
break; |
| 9212 |
default: |
| 9213 |
SPIRV_CROSS_THROW("Invalid component swizzle."); |
| 9214 |
} |
| 9215 |
} |
| 9216 |
} |
| 9217 |
else |
| 9218 |
{ |
| 9219 |
// Otherwise, we need to emit a temporary and swizzle that. |
| 9220 |
uint32_t temp_id = ir.increase_bound_by(1); |
| 9221 |
emit_op(result_type_id, temp_id, inner_expr, false); |
| 9222 |
for (auto &inherit : inherited_expressions) |
| 9223 |
inherit_expression_dependencies(temp_id, inherit); |
| 9224 |
inherited_expressions.clear(); |
| 9225 |
inherited_expressions.push_back(temp_id); |
| 9226 |
|
| 9227 |
switch (op) |
| 9228 |
{ |
| 9229 |
case OpImageSampleDrefImplicitLod: |
| 9230 |
case OpImageSampleImplicitLod: |
| 9231 |
case OpImageSampleProjImplicitLod: |
| 9232 |
case OpImageSampleProjDrefImplicitLod: |
| 9233 |
register_control_dependent_expression(temp_id); |
| 9234 |
break; |
| 9235 |
|
| 9236 |
default: |
| 9237 |
break; |
| 9238 |
} |
| 9239 |
expr += type_to_glsl(result_type) + "("; |
| 9240 |
for (uint32_t c = 0; c < 4; c++) |
| 9241 |
{ |
| 9242 |
switch (constexpr_sampler->swizzle[c]) |
| 9243 |
{ |
| 9244 |
case MSL_COMPONENT_SWIZZLE_IDENTITY: |
| 9245 |
expr += to_expression(temp_id) + "." + swizzle_names[c]; |
| 9246 |
break; |
| 9247 |
case MSL_COMPONENT_SWIZZLE_ZERO: |
| 9248 |
expr += "0"; |
| 9249 |
break; |
| 9250 |
case MSL_COMPONENT_SWIZZLE_ONE: |
| 9251 |
expr += "1"; |
| 9252 |
break; |
| 9253 |
case MSL_COMPONENT_SWIZZLE_R: |
| 9254 |
case MSL_COMPONENT_SWIZZLE_G: |
| 9255 |
case MSL_COMPONENT_SWIZZLE_B: |
| 9256 |
case MSL_COMPONENT_SWIZZLE_A: |
| 9257 |
expr += to_expression(temp_id) + "." + |
| 9258 |
swizzle_names[constexpr_sampler->swizzle[c] - MSL_COMPONENT_SWIZZLE_R]; |
| 9259 |
break; |
| 9260 |
default: |
| 9261 |
SPIRV_CROSS_THROW("Invalid component swizzle."); |
| 9262 |
} |
| 9263 |
if (c < 3) |
| 9264 |
expr += ", "; |
| 9265 |
} |
| 9266 |
expr += ")"; |
| 9267 |
} |
| 9268 |
} |
| 9269 |
else |
| 9270 |
expr += inner_expr; |
| 9271 |
if (constexpr_sampler->ycbcr_model != MSL_SAMPLER_YCBCR_MODEL_CONVERSION_RGB_IDENTITY) |
| 9272 |
{ |
| 9273 |
expr += join(", ", constexpr_sampler->bpc, ")"); |
| 9274 |
if (constexpr_sampler->ycbcr_model != MSL_SAMPLER_YCBCR_MODEL_CONVERSION_YCBCR_IDENTITY) |
| 9275 |
expr += ")"; |
| 9276 |
} |
| 9277 |
} |
| 9278 |
else |
| 9279 |
{ |
| 9280 |
expr += inner_expr; |
| 9281 |
if (msl_options.swizzle_texture_samples && !is_gather && is_sampled_image_type(imgtype) && |
| 9282 |
!is_dynamic_img_sampler) |
| 9283 |
{ |
| 9284 |
// Add the swizzle constant from the swizzle buffer. |
| 9285 |
expr += ", " + to_swizzle_expression(img) + ")"; |
| 9286 |
used_swizzle_buffer = true; |
| 9287 |
} |
| 9288 |
} |
| 9289 |
|
| 9290 |
return expr; |
| 9291 |
} |
| 9292 |
|
| 9293 |
static string create_swizzle(MSLComponentSwizzle swizzle) |
| 9294 |
{ |
| 9295 |
switch (swizzle) |
| 9296 |
{ |
| 9297 |
case MSL_COMPONENT_SWIZZLE_IDENTITY: |
| 9298 |
return "spvSwizzle::none"; |
| 9299 |
case MSL_COMPONENT_SWIZZLE_ZERO: |
| 9300 |
return "spvSwizzle::zero"; |
| 9301 |
case MSL_COMPONENT_SWIZZLE_ONE: |
| 9302 |
return "spvSwizzle::one"; |
| 9303 |
case MSL_COMPONENT_SWIZZLE_R: |
| 9304 |
return "spvSwizzle::red"; |
| 9305 |
case MSL_COMPONENT_SWIZZLE_G: |
| 9306 |
return "spvSwizzle::green"; |
| 9307 |
case MSL_COMPONENT_SWIZZLE_B: |
| 9308 |
return "spvSwizzle::blue"; |
| 9309 |
case MSL_COMPONENT_SWIZZLE_A: |
| 9310 |
return "spvSwizzle::alpha"; |
| 9311 |
default: |
| 9312 |
SPIRV_CROSS_THROW("Invalid component swizzle."); |
| 9313 |
return ""; |
| 9314 |
} |
| 9315 |
} |
| 9316 |
|
| 9317 |
// Returns a string representation of the ID, usable as a function arg. |
| 9318 |
// Manufacture automatic sampler arg for SampledImage texture. |
| 9319 |
string CompilerMSL::to_func_call_arg(const SPIRFunction::Parameter &arg, uint32_t id) |
| 9320 |
{ |
| 9321 |
string arg_str; |
| 9322 |
|
| 9323 |
auto &type = expression_type(id); |
| 9324 |
bool is_dynamic_img_sampler = has_extended_decoration(arg.id, SPIRVCrossDecorationDynamicImageSampler); |
| 9325 |
// If the argument *itself* is a "dynamic" combined-image sampler, then we can just pass that around. |
| 9326 |
bool arg_is_dynamic_img_sampler = has_extended_decoration(id, SPIRVCrossDecorationDynamicImageSampler); |
| 9327 |
if (is_dynamic_img_sampler && !arg_is_dynamic_img_sampler) |
| 9328 |
arg_str = join("spvDynamicImageSampler<", type_to_glsl(get<SPIRType>(type.image.type)), ">("); |
| 9329 |
|
| 9330 |
auto *c = maybe_get<SPIRConstant>(id); |
| 9331 |
if (msl_options.force_native_arrays && c && !get<SPIRType>(c->constant_type).array.empty()) |
| 9332 |
{ |
| 9333 |
// If we are passing a constant array directly to a function for some reason, |
| 9334 |
// the callee will expect an argument in thread const address space |
| 9335 |
// (since we can only bind to arrays with references in MSL). |
| 9336 |
// To resolve this, we must emit a copy in this address space. |
| 9337 |
// This kind of code gen should be rare enough that performance is not a real concern. |
| 9338 |
// Inline the SPIR-V to avoid this kind of suboptimal codegen. |
| 9339 |
// |
| 9340 |
// We risk calling this inside a continue block (invalid code), |
| 9341 |
// so just create a thread local copy in the current function. |
| 9342 |
arg_str = join("_", id, "_array_copy"); |
| 9343 |
auto &constants = current_function->constant_arrays_needed_on_stack; |
| 9344 |
auto itr = find(begin(constants), end(constants), ID(id)); |
| 9345 |
if (itr == end(constants)) |
| 9346 |
{ |
| 9347 |
force_recompile(); |
| 9348 |
constants.push_back(id); |
| 9349 |
} |
| 9350 |
} |
| 9351 |
else |
| 9352 |
arg_str += CompilerGLSL::to_func_call_arg(arg, id); |
| 9353 |
|
| 9354 |
// Need to check the base variable in case we need to apply a qualified alias. |
| 9355 |
uint32_t var_id = 0; |
| 9356 |
auto *var = maybe_get<SPIRVariable>(id); |
| 9357 |
if (var) |
| 9358 |
var_id = var->basevariable; |
| 9359 |
|
| 9360 |
if (!arg_is_dynamic_img_sampler) |
| 9361 |
{ |
| 9362 |
auto *constexpr_sampler = find_constexpr_sampler(var_id ? var_id : id); |
| 9363 |
if (type.basetype == SPIRType::SampledImage) |
| 9364 |
{ |
| 9365 |
// Manufacture automatic plane args for multiplanar texture |
| 9366 |
uint32_t planes = 1; |
| 9367 |
if (constexpr_sampler && constexpr_sampler->ycbcr_conversion_enable) |
| 9368 |
{ |
| 9369 |
planes = constexpr_sampler->planes; |
| 9370 |
// If this parameter isn't aliasing a global, then we need to use |
| 9371 |
// the special "dynamic image-sampler" class to pass it--and we need |
| 9372 |
// to use it for *every* non-alias parameter, in case a combined |
| 9373 |
// image-sampler with a Y'CbCr conversion is passed. Hopefully, this |
| 9374 |
// pathological case is so rare that it should never be hit in practice. |
| 9375 |
if (!arg.alias_global_variable) |
| 9376 |
add_spv_func_and_recompile(SPVFuncImplDynamicImageSampler); |
| 9377 |
} |
| 9378 |
for (uint32_t i = 1; i < planes; i++) |
| 9379 |
arg_str += join(", ", CompilerGLSL::to_func_call_arg(arg, id), plane_name_suffix, i); |
| 9380 |
// Manufacture automatic sampler arg if the arg is a SampledImage texture. |
| 9381 |
if (type.image.dim != DimBuffer) |
| 9382 |
arg_str += ", " + to_sampler_expression(var_id ? var_id : id); |
| 9383 |
|
| 9384 |
// Add sampler Y'CbCr conversion info if we have it |
| 9385 |
if (is_dynamic_img_sampler && constexpr_sampler && constexpr_sampler->ycbcr_conversion_enable) |
| 9386 |
{ |
| 9387 |
SmallVector<string> samp_args; |
| 9388 |
|
| 9389 |
switch (constexpr_sampler->resolution) |
| 9390 |
{ |
| 9391 |
case MSL_FORMAT_RESOLUTION_444: |
| 9392 |
// Default |
| 9393 |
break; |
| 9394 |
case MSL_FORMAT_RESOLUTION_422: |
| 9395 |
samp_args.push_back("spvFormatResolution::_422"); |
| 9396 |
break; |
| 9397 |
case MSL_FORMAT_RESOLUTION_420: |
| 9398 |
samp_args.push_back("spvFormatResolution::_420"); |
| 9399 |
break; |
| 9400 |
default: |
| 9401 |
SPIRV_CROSS_THROW("Invalid format resolution."); |
| 9402 |
} |
| 9403 |
|
| 9404 |
if (constexpr_sampler->chroma_filter != MSL_SAMPLER_FILTER_NEAREST) |
| 9405 |
samp_args.push_back("spvChromaFilter::linear"); |
| 9406 |
|
| 9407 |
if (constexpr_sampler->x_chroma_offset != MSL_CHROMA_LOCATION_COSITED_EVEN) |
| 9408 |
samp_args.push_back("spvXChromaLocation::midpoint"); |
| 9409 |
if (constexpr_sampler->y_chroma_offset != MSL_CHROMA_LOCATION_COSITED_EVEN) |
| 9410 |
samp_args.push_back("spvYChromaLocation::midpoint"); |
| 9411 |
switch (constexpr_sampler->ycbcr_model) |
| 9412 |
{ |
| 9413 |
case MSL_SAMPLER_YCBCR_MODEL_CONVERSION_RGB_IDENTITY: |
| 9414 |
// Default |
| 9415 |
break; |
| 9416 |
case MSL_SAMPLER_YCBCR_MODEL_CONVERSION_YCBCR_IDENTITY: |
| 9417 |
samp_args.push_back("spvYCbCrModelConversion::ycbcr_identity"); |
| 9418 |
break; |
| 9419 |
case MSL_SAMPLER_YCBCR_MODEL_CONVERSION_YCBCR_BT_709: |
| 9420 |
samp_args.push_back("spvYCbCrModelConversion::ycbcr_bt_709"); |
| 9421 |
break; |
| 9422 |
case MSL_SAMPLER_YCBCR_MODEL_CONVERSION_YCBCR_BT_601: |
| 9423 |
samp_args.push_back("spvYCbCrModelConversion::ycbcr_bt_601"); |
| 9424 |
break; |
| 9425 |
case MSL_SAMPLER_YCBCR_MODEL_CONVERSION_YCBCR_BT_2020: |
| 9426 |
samp_args.push_back("spvYCbCrModelConversion::ycbcr_bt_2020"); |
| 9427 |
break; |
| 9428 |
default: |
| 9429 |
SPIRV_CROSS_THROW("Invalid Y'CbCr model conversion."); |
| 9430 |
} |
| 9431 |
if (constexpr_sampler->ycbcr_range != MSL_SAMPLER_YCBCR_RANGE_ITU_FULL) |
| 9432 |
samp_args.push_back("spvYCbCrRange::itu_narrow"); |
| 9433 |
samp_args.push_back(join("spvComponentBits(", constexpr_sampler->bpc, ")")); |
| 9434 |
arg_str += join(", spvYCbCrSampler(", merge(samp_args), ")"); |
| 9435 |
} |
| 9436 |
} |
| 9437 |
|
| 9438 |
if (is_dynamic_img_sampler && constexpr_sampler && constexpr_sampler->ycbcr_conversion_enable) |
| 9439 |
arg_str += join(", (uint(", create_swizzle(constexpr_sampler->swizzle[3]), ") << 24) | (uint(", |
| 9440 |
create_swizzle(constexpr_sampler->swizzle[2]), ") << 16) | (uint(", |
| 9441 |
create_swizzle(constexpr_sampler->swizzle[1]), ") << 8) | uint(", |
| 9442 |
create_swizzle(constexpr_sampler->swizzle[0]), ")"); |
| 9443 |
else if (msl_options.swizzle_texture_samples && has_sampled_images && is_sampled_image_type(type)) |
| 9444 |
arg_str += ", " + to_swizzle_expression(var_id ? var_id : id); |
| 9445 |
|
| 9446 |
if (buffers_requiring_array_length.count(var_id)) |
| 9447 |
arg_str += ", " + to_buffer_size_expression(var_id ? var_id : id); |
| 9448 |
|
| 9449 |
if (is_dynamic_img_sampler) |
| 9450 |
arg_str += ")"; |
| 9451 |
} |
| 9452 |
|
| 9453 |
// Emulate texture2D atomic operations |
| 9454 |
auto *backing_var = maybe_get_backing_variable(var_id); |
| 9455 |
if (backing_var && atomic_image_vars.count(backing_var->self)) |
| 9456 |
{ |
| 9457 |
arg_str += ", " + to_expression(var_id) + "_atomic"; |
| 9458 |
} |
| 9459 |
|
| 9460 |
return arg_str; |
| 9461 |
} |
| 9462 |
|
| 9463 |
// If the ID represents a sampled image that has been assigned a sampler already, |
| 9464 |
// generate an expression for the sampler, otherwise generate a fake sampler name |
| 9465 |
// by appending a suffix to the expression constructed from the ID. |
| 9466 |
string CompilerMSL::to_sampler_expression(uint32_t id) |
| 9467 |
{ |
| 9468 |
auto *combined = maybe_get<SPIRCombinedImageSampler>(id); |
| 9469 |
auto expr = to_expression(combined ? combined->image : VariableID(id)); |
| 9470 |
auto index = expr.find_first_of('['); |
| 9471 |
|
| 9472 |
uint32_t samp_id = 0; |
| 9473 |
if (combined) |
| 9474 |
samp_id = combined->sampler; |
| 9475 |
|
| 9476 |
if (index == string::npos) |
| 9477 |
return samp_id ? to_expression(samp_id) : expr + sampler_name_suffix; |
| 9478 |
else |
| 9479 |
{ |
| 9480 |
auto image_expr = expr.substr(0, index); |
| 9481 |
auto array_expr = expr.substr(index); |
| 9482 |
return samp_id ? to_expression(samp_id) : (image_expr + sampler_name_suffix + array_expr); |
| 9483 |
} |
| 9484 |
} |
| 9485 |
|
| 9486 |
string CompilerMSL::to_swizzle_expression(uint32_t id) |
| 9487 |
{ |
| 9488 |
auto *combined = maybe_get<SPIRCombinedImageSampler>(id); |
| 9489 |
|
| 9490 |
auto expr = to_expression(combined ? combined->image : VariableID(id)); |
| 9491 |
auto index = expr.find_first_of('['); |
| 9492 |
|
| 9493 |
// If an image is part of an argument buffer translate this to a legal identifier. |
| 9494 |
string::size_type period = 0; |
| 9495 |
while ((period = expr.find_first_of('.', period)) != string::npos && period < index) |
| 9496 |
expr[period] = '_'; |
| 9497 |
|
| 9498 |
if (index == string::npos) |
| 9499 |
return expr + swizzle_name_suffix; |
| 9500 |
else |
| 9501 |
{ |
| 9502 |
auto image_expr = expr.substr(0, index); |
| 9503 |
auto array_expr = expr.substr(index); |
| 9504 |
return image_expr + swizzle_name_suffix + array_expr; |
| 9505 |
} |
| 9506 |
} |
| 9507 |
|
| 9508 |
string CompilerMSL::to_buffer_size_expression(uint32_t id) |
| 9509 |
{ |
| 9510 |
auto expr = to_expression(id); |
| 9511 |
auto index = expr.find_first_of('['); |
| 9512 |
|
| 9513 |
// This is quite crude, but we need to translate the reference name (*spvDescriptorSetN.name) to |
| 9514 |
// the pointer expression spvDescriptorSetN.name to make a reasonable expression here. |
| 9515 |
// This only happens if we have argument buffers and we are using OpArrayLength on a lone SSBO in that set. |
| 9516 |
if (expr.size() >= 3 && expr[0] == '(' && expr[1] == '*') |
| 9517 |
expr = address_of_expression(expr); |
| 9518 |
|
| 9519 |
// If a buffer is part of an argument buffer translate this to a legal identifier. |
| 9520 |
for (auto &c : expr) |
| 9521 |
if (c == '.') |
| 9522 |
c = '_'; |
| 9523 |
|
| 9524 |
if (index == string::npos) |
| 9525 |
return expr + buffer_size_name_suffix; |
| 9526 |
else |
| 9527 |
{ |
| 9528 |
auto buffer_expr = expr.substr(0, index); |
| 9529 |
auto array_expr = expr.substr(index); |
| 9530 |
return buffer_expr + buffer_size_name_suffix + array_expr; |
| 9531 |
} |
| 9532 |
} |
| 9533 |
|
| 9534 |
// Checks whether the type is a Block all of whose members have DecorationPatch. |
| 9535 |
bool CompilerMSL::is_patch_block(const SPIRType &type) |
| 9536 |
{ |
| 9537 |
if (!has_decoration(type.self, DecorationBlock)) |
| 9538 |
return false; |
| 9539 |
|
| 9540 |
for (uint32_t i = 0; i < type.member_types.size(); i++) |
| 9541 |
{ |
| 9542 |
if (!has_member_decoration(type.self, i, DecorationPatch)) |
| 9543 |
return false; |
| 9544 |
} |
| 9545 |
|
| 9546 |
return true; |
| 9547 |
} |
| 9548 |
|
| 9549 |
// Checks whether the ID is a row_major matrix that requires conversion before use |
| 9550 |
bool CompilerMSL::is_non_native_row_major_matrix(uint32_t id) |
| 9551 |
{ |
| 9552 |
auto *e = maybe_get<SPIRExpression>(id); |
| 9553 |
if (e) |
| 9554 |
return e->need_transpose; |
| 9555 |
else |
| 9556 |
return has_decoration(id, DecorationRowMajor); |
| 9557 |
} |
| 9558 |
|
| 9559 |
// Checks whether the member is a row_major matrix that requires conversion before use |
| 9560 |
bool CompilerMSL::member_is_non_native_row_major_matrix(const SPIRType &type, uint32_t index) |
| 9561 |
{ |
| 9562 |
return has_member_decoration(type.self, index, DecorationRowMajor); |
| 9563 |
} |
| 9564 |
|
| 9565 |
string CompilerMSL::convert_row_major_matrix(string exp_str, const SPIRType &exp_type, uint32_t physical_type_id, |
| 9566 |
bool is_packed) |
| 9567 |
{ |
| 9568 |
if (!is_matrix(exp_type)) |
| 9569 |
{ |
| 9570 |
return CompilerGLSL::convert_row_major_matrix(move(exp_str), exp_type, physical_type_id, is_packed); |
| 9571 |
} |
| 9572 |
else |
| 9573 |
{ |
| 9574 |
strip_enclosed_expression(exp_str); |
| 9575 |
if (physical_type_id != 0 || is_packed) |
| 9576 |
exp_str = unpack_expression_type(exp_str, exp_type, physical_type_id, is_packed, true); |
| 9577 |
return join("transpose(", exp_str, ")"); |
| 9578 |
} |
| 9579 |
} |
| 9580 |
|
| 9581 |
// Called automatically at the end of the entry point function |
| 9582 |
void CompilerMSL::emit_fixup() |
| 9583 |
{ |
| 9584 |
if (is_vertex_like_shader() && stage_out_var_id && !qual_pos_var_name.empty() && !capture_output_to_buffer) |
| 9585 |
{ |
| 9586 |
if (options.vertex.fixup_clipspace) |
| 9587 |
statement(qual_pos_var_name, ".z = (", qual_pos_var_name, ".z + ", qual_pos_var_name, |
| 9588 |
".w) * 0.5; // Adjust clip-space for Metal"); |
| 9589 |
|
| 9590 |
if (options.vertex.flip_vert_y) |
| 9591 |
statement(qual_pos_var_name, ".y = -(", qual_pos_var_name, ".y);", " // Invert Y-axis for Metal"); |
| 9592 |
} |
| 9593 |
} |
| 9594 |
|
| 9595 |
// Return a string defining a structure member, with padding and packing. |
| 9596 |
string CompilerMSL::to_struct_member(const SPIRType &type, uint32_t member_type_id, uint32_t index, |
| 9597 |
const string &qualifier) |
| 9598 |
{ |
| 9599 |
if (member_is_remapped_physical_type(type, index)) |
| 9600 |
member_type_id = get_extended_member_decoration(type.self, index, SPIRVCrossDecorationPhysicalTypeID); |
| 9601 |
auto &physical_type = get<SPIRType>(member_type_id); |
| 9602 |
|
| 9603 |
// If this member is packed, mark it as so. |
| 9604 |
string pack_pfx; |
| 9605 |
|
| 9606 |
// Allow Metal to use the array<T> template to make arrays a value type |
| 9607 |
uint32_t orig_id = 0; |
| 9608 |
if (has_extended_member_decoration(type.self, index, SPIRVCrossDecorationInterfaceOrigID)) |
| 9609 |
orig_id = get_extended_member_decoration(type.self, index, SPIRVCrossDecorationInterfaceOrigID); |
| 9610 |
|
| 9611 |
bool row_major = false; |
| 9612 |
if (is_matrix(physical_type)) |
| 9613 |
row_major = has_member_decoration(type.self, index, DecorationRowMajor); |
| 9614 |
|
| 9615 |
SPIRType row_major_physical_type; |
| 9616 |
const SPIRType *declared_type = &physical_type; |
| 9617 |
|
| 9618 |
// If a struct is being declared with physical layout, |
| 9619 |
// do not use array<T> wrappers. |
| 9620 |
// This avoids a lot of complicated cases with packed vectors and matrices, |
| 9621 |
// and generally we cannot copy full arrays in and out of buffers into Function |
| 9622 |
// address space. |
| 9623 |
// Array of resources should also be declared as builtin arrays. |
| 9624 |
if (has_member_decoration(type.self, index, DecorationOffset)) |
| 9625 |
is_using_builtin_array = true; |
| 9626 |
else if (has_extended_member_decoration(type.self, index, SPIRVCrossDecorationResourceIndexPrimary)) |
| 9627 |
is_using_builtin_array = true; |
| 9628 |
|
| 9629 |
if (member_is_packed_physical_type(type, index)) |
| 9630 |
{ |
| 9631 |
// If we're packing a matrix, output an appropriate typedef |
| 9632 |
if (physical_type.basetype == SPIRType::Struct) |
| 9633 |
{ |
| 9634 |
SPIRV_CROSS_THROW("Cannot emit a packed struct currently."); |
| 9635 |
} |
| 9636 |
else if (is_matrix(physical_type)) |
| 9637 |
{ |
| 9638 |
uint32_t rows = physical_type.vecsize; |
| 9639 |
uint32_t cols = physical_type.columns; |
| 9640 |
pack_pfx = "packed_"; |
| 9641 |
if (row_major) |
| 9642 |
{ |
| 9643 |
// These are stored transposed. |
| 9644 |
rows = physical_type.columns; |
| 9645 |
cols = physical_type.vecsize; |
| 9646 |
pack_pfx = "packed_rm_"; |
| 9647 |
} |
| 9648 |
string base_type = physical_type.width == 16 ? "half" : "float"; |
| 9649 |
string td_line = "typedef "; |
| 9650 |
td_line += "packed_" + base_type + to_string(rows); |
| 9651 |
td_line += " " + pack_pfx; |
| 9652 |
// Use the actual matrix size here. |
| 9653 |
td_line += base_type + to_string(physical_type.columns) + "x" + to_string(physical_type.vecsize); |
| 9654 |
td_line += "[" + to_string(cols) + "]"; |
| 9655 |
td_line += ";"; |
| 9656 |
add_typedef_line(td_line); |
| 9657 |
} |
| 9658 |
else if (!is_scalar(physical_type)) // scalar type is already packed. |
| 9659 |
pack_pfx = "packed_"; |
| 9660 |
} |
| 9661 |
else if (row_major) |
| 9662 |
{ |
| 9663 |
// Need to declare type with flipped vecsize/columns. |
| 9664 |
row_major_physical_type = physical_type; |
| 9665 |
swap(row_major_physical_type.vecsize, row_major_physical_type.columns); |
| 9666 |
declared_type = &row_major_physical_type; |
| 9667 |
} |
| 9668 |
|
| 9669 |
// Very specifically, image load-store in argument buffers are disallowed on MSL on iOS. |
| 9670 |
if (msl_options.is_ios() && physical_type.basetype == SPIRType::Image && physical_type.image.sampled == 2) |
| 9671 |
{ |
| 9672 |
if (!has_decoration(orig_id, DecorationNonWritable)) |
| 9673 |
SPIRV_CROSS_THROW("Writable images are not allowed in argument buffers on iOS."); |
| 9674 |
} |
| 9675 |
|
| 9676 |
// Array information is baked into these types. |
| 9677 |
string array_type; |
| 9678 |
if (physical_type.basetype != SPIRType::Image && physical_type.basetype != SPIRType::Sampler && |
| 9679 |
physical_type.basetype != SPIRType::SampledImage) |
| 9680 |
{ |
| 9681 |
BuiltIn builtin = BuiltInMax; |
| 9682 |
if (is_member_builtin(type, index, &builtin)) |
| 9683 |
is_using_builtin_array = true; |
| 9684 |
array_type = type_to_array_glsl(physical_type); |
| 9685 |
} |
| 9686 |
|
| 9687 |
auto result = join(pack_pfx, type_to_glsl(*declared_type, orig_id), " ", qualifier, to_member_name(type, index), |
| 9688 |
member_attribute_qualifier(type, index), array_type, ";"); |
| 9689 |
|
| 9690 |
is_using_builtin_array = false; |
| 9691 |
return result; |
| 9692 |
} |
| 9693 |
|
| 9694 |
// Emit a structure member, padding and packing to maintain the correct memeber alignments. |
| 9695 |
void CompilerMSL::emit_struct_member(const SPIRType &type, uint32_t member_type_id, uint32_t index, |
| 9696 |
const string &qualifier, uint32_t) |
| 9697 |
{ |
| 9698 |
// If this member requires padding to maintain its declared offset, emit a dummy padding member before it. |
| 9699 |
if (has_extended_member_decoration(type.self, index, SPIRVCrossDecorationPaddingTarget)) |
| 9700 |
{ |
| 9701 |
uint32_t pad_len = get_extended_member_decoration(type.self, index, SPIRVCrossDecorationPaddingTarget); |
| 9702 |
statement("char _m", index, "_pad", "[", pad_len, "];"); |
| 9703 |
} |
| 9704 |
|
| 9705 |
// Handle HLSL-style 0-based vertex/instance index. |
| 9706 |
builtin_declaration = true; |
| 9707 |
statement(to_struct_member(type, member_type_id, index, qualifier)); |
| 9708 |
builtin_declaration = false; |
| 9709 |
} |
| 9710 |
|
| 9711 |
void CompilerMSL::emit_struct_padding_target(const SPIRType &type) |
| 9712 |
{ |
| 9713 |
uint32_t struct_size = get_declared_struct_size_msl(type, true, true); |
| 9714 |
uint32_t target_size = get_extended_decoration(type.self, SPIRVCrossDecorationPaddingTarget); |
| 9715 |
if (target_size < struct_size) |
| 9716 |
SPIRV_CROSS_THROW("Cannot pad with negative bytes."); |
| 9717 |
else if (target_size > struct_size) |
| 9718 |
statement("char _m0_final_padding[", target_size - struct_size, "];"); |
| 9719 |
} |
| 9720 |
|
| 9721 |
// Return a MSL qualifier for the specified function attribute member |
| 9722 |
string CompilerMSL::member_attribute_qualifier(const SPIRType &type, uint32_t index) |
| 9723 |
{ |
| 9724 |
auto &execution = get_entry_point(); |
| 9725 |
|
| 9726 |
uint32_t mbr_type_id = type.member_types[index]; |
| 9727 |
auto &mbr_type = get<SPIRType>(mbr_type_id); |
| 9728 |
|
| 9729 |
BuiltIn builtin = BuiltInMax; |
| 9730 |
bool is_builtin = is_member_builtin(type, index, &builtin); |
| 9731 |
|
| 9732 |
if (has_extended_member_decoration(type.self, index, SPIRVCrossDecorationResourceIndexPrimary)) |
| 9733 |
{ |
| 9734 |
string quals = join( |
| 9735 |
" [[id(", get_extended_member_decoration(type.self, index, SPIRVCrossDecorationResourceIndexPrimary), ")"); |
| 9736 |
if (interlocked_resources.count( |
| 9737 |
get_extended_member_decoration(type.self, index, SPIRVCrossDecorationInterfaceOrigID))) |
| 9738 |
quals += ", raster_order_group(0)"; |
| 9739 |
quals += "]]"; |
| 9740 |
return quals; |
| 9741 |
} |
| 9742 |
|
| 9743 |
// Vertex function inputs |
| 9744 |
if (execution.model == ExecutionModelVertex && type.storage == StorageClassInput) |
| 9745 |
{ |
| 9746 |
if (is_builtin) |
| 9747 |
{ |
| 9748 |
switch (builtin) |
| 9749 |
{ |
| 9750 |
case BuiltInVertexId: |
| 9751 |
case BuiltInVertexIndex: |
| 9752 |
case BuiltInBaseVertex: |
| 9753 |
case BuiltInInstanceId: |
| 9754 |
case BuiltInInstanceIndex: |
| 9755 |
case BuiltInBaseInstance: |
| 9756 |
if (msl_options.vertex_for_tessellation) |
| 9757 |
return ""; |
| 9758 |
return string(" [[") + builtin_qualifier(builtin) + "]]"; |
| 9759 |
|
| 9760 |
case BuiltInDrawIndex: |
| 9761 |
SPIRV_CROSS_THROW("DrawIndex is not supported in MSL."); |
| 9762 |
|
| 9763 |
default: |
| 9764 |
return ""; |
| 9765 |
} |
| 9766 |
} |
| 9767 |
uint32_t locn = get_ordered_member_location(type.self, index); |
| 9768 |
if (locn != k_unknown_location) |
| 9769 |
return string(" [[attribute(") + convert_to_string(locn) + ")]]"; |
| 9770 |
} |
| 9771 |
|
| 9772 |
// Vertex and tessellation evaluation function outputs |
| 9773 |
if (((execution.model == ExecutionModelVertex && !msl_options.vertex_for_tessellation) || |
| 9774 |
execution.model == ExecutionModelTessellationEvaluation) && |
| 9775 |
type.storage == StorageClassOutput) |
| 9776 |
{ |
| 9777 |
if (is_builtin) |
| 9778 |
{ |
| 9779 |
switch (builtin) |
| 9780 |
{ |
| 9781 |
case BuiltInPointSize: |
| 9782 |
// Only mark the PointSize builtin if really rendering points. |
| 9783 |
// Some shaders may include a PointSize builtin even when used to render |
| 9784 |
// non-point topologies, and Metal will reject this builtin when compiling |
| 9785 |
// the shader into a render pipeline that uses a non-point topology. |
| 9786 |
return msl_options.enable_point_size_builtin ? (string(" [[") + builtin_qualifier(builtin) + "]]") : ""; |
| 9787 |
|
| 9788 |
case BuiltInViewportIndex: |
| 9789 |
if (!msl_options.supports_msl_version(2, 0)) |
| 9790 |
SPIRV_CROSS_THROW("ViewportIndex requires Metal 2.0."); |
| 9791 |
/* fallthrough */ |
| 9792 |
case BuiltInPosition: |
| 9793 |
case BuiltInLayer: |
| 9794 |
return string(" [[") + builtin_qualifier(builtin) + "]]" + (mbr_type.array.empty() ? "" : " "); |
| 9795 |
|
| 9796 |
case BuiltInClipDistance: |
| 9797 |
if (has_member_decoration(type.self, index, DecorationLocation)) |
| 9798 |
return join(" [[user(clip", get_member_decoration(type.self, index, DecorationLocation), ")]]"); |
| 9799 |
else |
| 9800 |
return string(" [[") + builtin_qualifier(builtin) + "]]" + (mbr_type.array.empty() ? "" : " "); |
| 9801 |
|
| 9802 |
default: |
| 9803 |
return ""; |
| 9804 |
} |
| 9805 |
} |
| 9806 |
uint32_t comp; |
| 9807 |
uint32_t locn = get_ordered_member_location(type.self, index, &comp); |
| 9808 |
if (locn != k_unknown_location) |
| 9809 |
{ |
| 9810 |
if (comp != k_unknown_component) |
| 9811 |
return string(" [[user(locn") + convert_to_string(locn) + "_" + convert_to_string(comp) + ")]]"; |
| 9812 |
else |
| 9813 |
return string(" [[user(locn") + convert_to_string(locn) + ")]]"; |
| 9814 |
} |
| 9815 |
} |
| 9816 |
|
| 9817 |
// Tessellation control function inputs |
| 9818 |
if (execution.model == ExecutionModelTessellationControl && type.storage == StorageClassInput) |
| 9819 |
{ |
| 9820 |
if (is_builtin) |
| 9821 |
{ |
| 9822 |
switch (builtin) |
| 9823 |
{ |
| 9824 |
case BuiltInInvocationId: |
| 9825 |
case BuiltInPrimitiveId: |
| 9826 |
if (msl_options.multi_patch_workgroup) |
| 9827 |
return ""; |
| 9828 |
/* fallthrough */ |
| 9829 |
case BuiltInSubgroupLocalInvocationId: // FIXME: Should work in any stage |
| 9830 |
case BuiltInSubgroupSize: // FIXME: Should work in any stage |
| 9831 |
return string(" [[") + builtin_qualifier(builtin) + "]]" + (mbr_type.array.empty() ? "" : " "); |
| 9832 |
case BuiltInPatchVertices: |
| 9833 |
return ""; |
| 9834 |
// Others come from stage input. |
| 9835 |
default: |
| 9836 |
break; |
| 9837 |
} |
| 9838 |
} |
| 9839 |
if (msl_options.multi_patch_workgroup) |
| 9840 |
return ""; |
| 9841 |
uint32_t locn = get_ordered_member_location(type.self, index); |
| 9842 |
if (locn != k_unknown_location) |
| 9843 |
return string(" [[attribute(") + convert_to_string(locn) + ")]]"; |
| 9844 |
} |
| 9845 |
|
| 9846 |
// Tessellation control function outputs |
| 9847 |
if (execution.model == ExecutionModelTessellationControl && type.storage == StorageClassOutput) |
| 9848 |
{ |
| 9849 |
// For this type of shader, we always arrange for it to capture its |
| 9850 |
// output to a buffer. For this reason, qualifiers are irrelevant here. |
| 9851 |
return ""; |
| 9852 |
} |
| 9853 |
|
| 9854 |
// Tessellation evaluation function inputs |
| 9855 |
if (execution.model == ExecutionModelTessellationEvaluation && type.storage == StorageClassInput) |
| 9856 |
{ |
| 9857 |
if (is_builtin) |
| 9858 |
{ |
| 9859 |
switch (builtin) |
| 9860 |
{ |
| 9861 |
case BuiltInPrimitiveId: |
| 9862 |
case BuiltInTessCoord: |
| 9863 |
return string(" [[") + builtin_qualifier(builtin) + "]]"; |
| 9864 |
case BuiltInPatchVertices: |
| 9865 |
return ""; |
| 9866 |
// Others come from stage input. |
| 9867 |
default: |
| 9868 |
break; |
| 9869 |
} |
| 9870 |
} |
| 9871 |
// The special control point array must not be marked with an attribute. |
| 9872 |
if (get_type(type.member_types[index]).basetype == SPIRType::ControlPointArray) |
| 9873 |
return ""; |
| 9874 |
uint32_t locn = get_ordered_member_location(type.self, index); |
| 9875 |
if (locn != k_unknown_location) |
| 9876 |
return string(" [[attribute(") + convert_to_string(locn) + ")]]"; |
| 9877 |
} |
| 9878 |
|
| 9879 |
// Tessellation evaluation function outputs were handled above. |
| 9880 |
|
| 9881 |
// Fragment function inputs |
| 9882 |
if (execution.model == ExecutionModelFragment && type.storage == StorageClassInput) |
| 9883 |
{ |
| 9884 |
string quals; |
| 9885 |
if (is_builtin) |
| 9886 |
{ |
| 9887 |
switch (builtin) |
| 9888 |
{ |
| 9889 |
case BuiltInViewIndex: |
| 9890 |
if (!msl_options.multiview || !msl_options.multiview_layered_rendering) |
| 9891 |
break; |
| 9892 |
/* fallthrough */ |
| 9893 |
case BuiltInFrontFacing: |
| 9894 |
case BuiltInPointCoord: |
| 9895 |
case BuiltInFragCoord: |
| 9896 |
case BuiltInSampleId: |
| 9897 |
case BuiltInSampleMask: |
| 9898 |
case BuiltInLayer: |
| 9899 |
case BuiltInBaryCoordNV: |
| 9900 |
case BuiltInBaryCoordNoPerspNV: |
| 9901 |
quals = builtin_qualifier(builtin); |
| 9902 |
break; |
| 9903 |
|
| 9904 |
case BuiltInClipDistance: |
| 9905 |
return join(" [[user(clip", get_member_decoration(type.self, index, DecorationLocation), ")]]"); |
| 9906 |
|
| 9907 |
default: |
| 9908 |
break; |
| 9909 |
} |
| 9910 |
} |
| 9911 |
else |
| 9912 |
{ |
| 9913 |
uint32_t comp; |
| 9914 |
uint32_t locn = get_ordered_member_location(type.self, index, &comp); |
| 9915 |
if (locn != k_unknown_location) |
| 9916 |
{ |
| 9917 |
// For user-defined attributes, this is fine. From Vulkan spec: |
| 9918 |
// A user-defined output variable is considered to match an input variable in the subsequent stage if |
| 9919 |
// the two variables are declared with the same Location and Component decoration and match in type |
| 9920 |
// and decoration, except that interpolation decorations are not required to match. For the purposes |
| 9921 |
// of interface matching, variables declared without a Component decoration are considered to have a |
| 9922 |
// Component decoration of zero. |
| 9923 |
|
| 9924 |
if (comp != k_unknown_component && comp != 0) |
| 9925 |
quals = string("user(locn") + convert_to_string(locn) + "_" + convert_to_string(comp) + ")"; |
| 9926 |
else |
| 9927 |
quals = string("user(locn") + convert_to_string(locn) + ")"; |
| 9928 |
} |
| 9929 |
} |
| 9930 |
|
| 9931 |
if (builtin == BuiltInBaryCoordNV || builtin == BuiltInBaryCoordNoPerspNV) |
| 9932 |
{ |
| 9933 |
if (has_member_decoration(type.self, index, DecorationFlat) || |
| 9934 |
has_member_decoration(type.self, index, DecorationCentroid) || |
| 9935 |
has_member_decoration(type.self, index, DecorationSample) || |
| 9936 |
has_member_decoration(type.self, index, DecorationNoPerspective)) |
| 9937 |
{ |
| 9938 |
// NoPerspective is baked into the builtin type. |
| 9939 |
SPIRV_CROSS_THROW( |
| 9940 |
"Flat, Centroid, Sample, NoPerspective decorations are not supported for BaryCoord inputs."); |
| 9941 |
} |
| 9942 |
} |
| 9943 |
|
| 9944 |
// Don't bother decorating integers with the 'flat' attribute; it's |
| 9945 |
// the default (in fact, the only option). Also don't bother with the |
| 9946 |
// FragCoord builtin; it's always noperspective on Metal. |
| 9947 |
if (!type_is_integral(mbr_type) && (!is_builtin || builtin != BuiltInFragCoord)) |
| 9948 |
{ |
| 9949 |
if (has_member_decoration(type.self, index, DecorationFlat)) |
| 9950 |
{ |
| 9951 |
if (!quals.empty()) |
| 9952 |
quals += ", "; |
| 9953 |
quals += "flat"; |
| 9954 |
} |
| 9955 |
else if (has_member_decoration(type.self, index, DecorationCentroid)) |
| 9956 |
{ |
| 9957 |
if (!quals.empty()) |
| 9958 |
quals += ", "; |
| 9959 |
if (has_member_decoration(type.self, index, DecorationNoPerspective)) |
| 9960 |
quals += "centroid_no_perspective"; |
| 9961 |
else |
| 9962 |
quals += "centroid_perspective"; |
| 9963 |
} |
| 9964 |
else if (has_member_decoration(type.self, index, DecorationSample)) |
| 9965 |
{ |
| 9966 |
if (!quals.empty()) |
| 9967 |
quals += ", "; |
| 9968 |
if (has_member_decoration(type.self, index, DecorationNoPerspective)) |
| 9969 |
quals += "sample_no_perspective"; |
| 9970 |
else |
| 9971 |
quals += "sample_perspective"; |
| 9972 |
} |
| 9973 |
else if (has_member_decoration(type.self, index, DecorationNoPerspective)) |
| 9974 |
{ |
| 9975 |
if (!quals.empty()) |
| 9976 |
quals += ", "; |
| 9977 |
quals += "center_no_perspective"; |
| 9978 |
} |
| 9979 |
} |
| 9980 |
|
| 9981 |
if (!quals.empty()) |
| 9982 |
return " [[" + quals + "]]"; |
| 9983 |
} |
| 9984 |
|
| 9985 |
// Fragment function outputs |
| 9986 |
if (execution.model == ExecutionModelFragment && type.storage == StorageClassOutput) |
| 9987 |
{ |
| 9988 |
if (is_builtin) |
| 9989 |
{ |
| 9990 |
switch (builtin) |
| 9991 |
{ |
| 9992 |
case BuiltInFragStencilRefEXT: |
| 9993 |
// Similar to PointSize, only mark FragStencilRef if there's a stencil buffer. |
| 9994 |
// Some shaders may include a FragStencilRef builtin even when used to render |
| 9995 |
// without a stencil attachment, and Metal will reject this builtin |
| 9996 |
// when compiling the shader into a render pipeline that does not set |
| 9997 |
// stencilAttachmentPixelFormat. |
| 9998 |
if (!msl_options.enable_frag_stencil_ref_builtin) |
| 9999 |
return ""; |
| 10000 |
if (!msl_options.supports_msl_version(2, 1)) |
| 10001 |
SPIRV_CROSS_THROW("Stencil export only supported in MSL 2.1 and up."); |
| 10002 |
return string(" [[") + builtin_qualifier(builtin) + "]]"; |
| 10003 |
|
| 10004 |
case BuiltInFragDepth: |
| 10005 |
// Ditto FragDepth. |
| 10006 |
if (!msl_options.enable_frag_depth_builtin) |
| 10007 |
return ""; |
| 10008 |
/* fallthrough */ |
| 10009 |
case BuiltInSampleMask: |
| 10010 |
return string(" [[") + builtin_qualifier(builtin) + "]]"; |
| 10011 |
|
| 10012 |
default: |
| 10013 |
return ""; |
| 10014 |
} |
| 10015 |
} |
| 10016 |
uint32_t locn = get_ordered_member_location(type.self, index); |
| 10017 |
// Metal will likely complain about missing color attachments, too. |
| 10018 |
if (locn != k_unknown_location && !(msl_options.enable_frag_output_mask & (1 << locn))) |
| 10019 |
return ""; |
| 10020 |
if (locn != k_unknown_location && has_member_decoration(type.self, index, DecorationIndex)) |
| 10021 |
return join(" [[color(", locn, "), index(", get_member_decoration(type.self, index, DecorationIndex), |
| 10022 |
")]]"); |
| 10023 |
else if (locn != k_unknown_location) |
| 10024 |
return join(" [[color(", locn, ")]]"); |
| 10025 |
else if (has_member_decoration(type.self, index, DecorationIndex)) |
| 10026 |
return join(" [[index(", get_member_decoration(type.self, index, DecorationIndex), ")]]"); |
| 10027 |
else |
| 10028 |
return ""; |
| 10029 |
} |
| 10030 |
|
| 10031 |
// Compute function inputs |
| 10032 |
if (execution.model == ExecutionModelGLCompute && type.storage == StorageClassInput) |
| 10033 |
{ |
| 10034 |
if (is_builtin) |
| 10035 |
{ |
| 10036 |
switch (builtin) |
| 10037 |
{ |
| 10038 |
case BuiltInGlobalInvocationId: |
| 10039 |
case BuiltInWorkgroupId: |
| 10040 |
case BuiltInNumWorkgroups: |
| 10041 |
case BuiltInLocalInvocationId: |
| 10042 |
case BuiltInLocalInvocationIndex: |
| 10043 |
case BuiltInNumSubgroups: |
| 10044 |
case BuiltInSubgroupId: |
| 10045 |
case BuiltInSubgroupLocalInvocationId: // FIXME: Should work in any stage |
| 10046 |
case BuiltInSubgroupSize: // FIXME: Should work in any stage |
| 10047 |
return string(" [[") + builtin_qualifier(builtin) + "]]"; |
| 10048 |
|
| 10049 |
default: |
| 10050 |
return ""; |
| 10051 |
} |
| 10052 |
} |
| 10053 |
} |
| 10054 |
|
| 10055 |
return ""; |
| 10056 |
} |
| 10057 |
|
| 10058 |
// Returns the location decoration of the member with the specified index in the specified type. |
| 10059 |
// If the location of the member has been explicitly set, that location is used. If not, this |
| 10060 |
// function assumes the members are ordered in their location order, and simply returns the |
| 10061 |
// index as the location. |
| 10062 |
uint32_t CompilerMSL::get_ordered_member_location(uint32_t type_id, uint32_t index, uint32_t *comp) |
| 10063 |
{ |
| 10064 |
auto &m = ir.meta[type_id]; |
| 10065 |
if (index < m.members.size()) |
| 10066 |
{ |
| 10067 |
auto &dec = m.members[index]; |
| 10068 |
if (comp) |
| 10069 |
{ |
| 10070 |
if (dec.decoration_flags.get(DecorationComponent)) |
| 10071 |
*comp = dec.component; |
| 10072 |
else |
| 10073 |
*comp = k_unknown_component; |
| 10074 |
} |
| 10075 |
if (dec.decoration_flags.get(DecorationLocation)) |
| 10076 |
return dec.location; |
| 10077 |
} |
| 10078 |
|
| 10079 |
return index; |
| 10080 |
} |
| 10081 |
|
| 10082 |
// Returns the type declaration for a function, including the |
| 10083 |
// entry type if the current function is the entry point function |
| 10084 |
string CompilerMSL::func_type_decl(SPIRType &type) |
| 10085 |
{ |
| 10086 |
// The regular function return type. If not processing the entry point function, that's all we need |
| 10087 |
string return_type = type_to_glsl(type) + type_to_array_glsl(type); |
| 10088 |
if (!processing_entry_point) |
| 10089 |
return return_type; |
| 10090 |
|
| 10091 |
// If an outgoing interface block has been defined, and it should be returned, override the entry point return type |
| 10092 |
bool ep_should_return_output = !get_is_rasterization_disabled(); |
| 10093 |
if (stage_out_var_id && ep_should_return_output) |
| 10094 |
return_type = type_to_glsl(get_stage_out_struct_type()) + type_to_array_glsl(type); |
| 10095 |
|
| 10096 |
// Prepend a entry type, based on the execution model |
| 10097 |
string entry_type; |
| 10098 |
auto &execution = get_entry_point(); |
| 10099 |
switch (execution.model) |
| 10100 |
{ |
| 10101 |
case ExecutionModelVertex: |
| 10102 |
if (msl_options.vertex_for_tessellation && !msl_options.supports_msl_version(1, 2)) |
| 10103 |
SPIRV_CROSS_THROW("Tessellation requires Metal 1.2."); |
| 10104 |
entry_type = msl_options.vertex_for_tessellation ? "kernel" : "vertex"; |
| 10105 |
break; |
| 10106 |
case ExecutionModelTessellationEvaluation: |
| 10107 |
if (!msl_options.supports_msl_version(1, 2)) |
| 10108 |
SPIRV_CROSS_THROW("Tessellation requires Metal 1.2."); |
| 10109 |
if (execution.flags.get(ExecutionModeIsolines)) |
| 10110 |
SPIRV_CROSS_THROW("Metal does not support isoline tessellation."); |
| 10111 |
if (msl_options.is_ios()) |
| 10112 |
entry_type = |
| 10113 |
join("[[ patch(", execution.flags.get(ExecutionModeTriangles) ? "triangle" : "quad", ") ]] vertex"); |
| 10114 |
else |
| 10115 |
entry_type = join("[[ patch(", execution.flags.get(ExecutionModeTriangles) ? "triangle" : "quad", ", ", |
| 10116 |
execution.output_vertices, ") ]] vertex"); |
| 10117 |
break; |
| 10118 |
case ExecutionModelFragment: |
| 10119 |
entry_type = execution.flags.get(ExecutionModeEarlyFragmentTests) || |
| 10120 |
execution.flags.get(ExecutionModePostDepthCoverage) ? |
| 10121 |
"[[ early_fragment_tests ]] fragment" : |
| 10122 |
"fragment"; |
| 10123 |
break; |
| 10124 |
case ExecutionModelTessellationControl: |
| 10125 |
if (!msl_options.supports_msl_version(1, 2)) |
| 10126 |
SPIRV_CROSS_THROW("Tessellation requires Metal 1.2."); |
| 10127 |
if (execution.flags.get(ExecutionModeIsolines)) |
| 10128 |
SPIRV_CROSS_THROW("Metal does not support isoline tessellation."); |
| 10129 |
/* fallthrough */ |
| 10130 |
case ExecutionModelGLCompute: |
| 10131 |
case ExecutionModelKernel: |
| 10132 |
entry_type = "kernel"; |
| 10133 |
break; |
| 10134 |
default: |
| 10135 |
entry_type = "unknown"; |
| 10136 |
break; |
| 10137 |
} |
| 10138 |
|
| 10139 |
return entry_type + " " + return_type; |
| 10140 |
} |
| 10141 |
|
| 10142 |
// In MSL, address space qualifiers are required for all pointer or reference variables |
| 10143 |
string CompilerMSL::get_argument_address_space(const SPIRVariable &argument) |
| 10144 |
{ |
| 10145 |
const auto &type = get<SPIRType>(argument.basetype); |
| 10146 |
return get_type_address_space(type, argument.self, true); |
| 10147 |
} |
| 10148 |
|
| 10149 |
string CompilerMSL::get_type_address_space(const SPIRType &type, uint32_t id, bool argument) |
| 10150 |
{ |
| 10151 |
// This can be called for variable pointer contexts as well, so be very careful about which method we choose. |
| 10152 |
Bitset flags; |
| 10153 |
auto *var = maybe_get<SPIRVariable>(id); |
| 10154 |
if (var && type.basetype == SPIRType::Struct && |
| 10155 |
(has_decoration(type.self, DecorationBlock) || has_decoration(type.self, DecorationBufferBlock))) |
| 10156 |
flags = get_buffer_block_flags(id); |
| 10157 |
else |
| 10158 |
flags = get_decoration_bitset(id); |
| 10159 |
|
| 10160 |
const char *addr_space = nullptr; |
| 10161 |
switch (type.storage) |
| 10162 |
{ |
| 10163 |
case StorageClassWorkgroup: |
| 10164 |
addr_space = "threadgroup"; |
| 10165 |
break; |
| 10166 |
|
| 10167 |
case StorageClassStorageBuffer: |
| 10168 |
{ |
| 10169 |
// For arguments from variable pointers, we use the write count deduction, so |
| 10170 |
// we should not assume any constness here. Only for global SSBOs. |
| 10171 |
bool readonly = false; |
| 10172 |
if (!var || has_decoration(type.self, DecorationBlock)) |
| 10173 |
readonly = flags.get(DecorationNonWritable); |
| 10174 |
|
| 10175 |
addr_space = readonly ? "const device" : "device"; |
| 10176 |
break; |
| 10177 |
} |
| 10178 |
|
| 10179 |
case StorageClassUniform: |
| 10180 |
case StorageClassUniformConstant: |
| 10181 |
case StorageClassPushConstant: |
| 10182 |
if (type.basetype == SPIRType::Struct) |
| 10183 |
{ |
| 10184 |
bool ssbo = has_decoration(type.self, DecorationBufferBlock); |
| 10185 |
if (ssbo) |
| 10186 |
addr_space = flags.get(DecorationNonWritable) ? "const device" : "device"; |
| 10187 |
else |
| 10188 |
addr_space = "constant"; |
| 10189 |
} |
| 10190 |
else if (!argument) |
| 10191 |
{ |
| 10192 |
addr_space = "constant"; |
| 10193 |
} |
| 10194 |
else if (type_is_msl_framebuffer_fetch(type)) |
| 10195 |
{ |
| 10196 |
// Subpass inputs are passed around by value. |
| 10197 |
addr_space = ""; |
| 10198 |
} |
| 10199 |
break; |
| 10200 |
|
| 10201 |
case StorageClassFunction: |
| 10202 |
case StorageClassGeneric: |
| 10203 |
break; |
| 10204 |
|
| 10205 |
case StorageClassInput: |
| 10206 |
if (get_execution_model() == ExecutionModelTessellationControl && var && |
| 10207 |
var->basevariable == stage_in_ptr_var_id) |
| 10208 |
addr_space = msl_options.multi_patch_workgroup ? "constant" : "threadgroup"; |
| 10209 |
if (get_execution_model() == ExecutionModelFragment && var && var->basevariable == stage_in_var_id) |
| 10210 |
addr_space = "thread"; |
| 10211 |
break; |
| 10212 |
|
| 10213 |
case StorageClassOutput: |
| 10214 |
if (capture_output_to_buffer) |
| 10215 |
addr_space = "device"; |
| 10216 |
break; |
| 10217 |
|
| 10218 |
default: |
| 10219 |
break; |
| 10220 |
} |
| 10221 |
|
| 10222 |
if (!addr_space) |
| 10223 |
// No address space for plain values. |
| 10224 |
addr_space = type.pointer || (argument && type.basetype == SPIRType::ControlPointArray) ? "thread" : ""; |
| 10225 |
|
| 10226 |
return join(flags.get(DecorationVolatile) || flags.get(DecorationCoherent) ? "volatile " : "", addr_space); |
| 10227 |
} |
| 10228 |
|
| 10229 |
const char *CompilerMSL::to_restrict(uint32_t id, bool space) |
| 10230 |
{ |
| 10231 |
// This can be called for variable pointer contexts as well, so be very careful about which method we choose. |
| 10232 |
Bitset flags; |
| 10233 |
if (ir.ids[id].get_type() == TypeVariable) |
| 10234 |
{ |
| 10235 |
uint32_t type_id = expression_type_id(id); |
| 10236 |
auto &type = expression_type(id); |
| 10237 |
if (type.basetype == SPIRType::Struct && |
| 10238 |
(has_decoration(type_id, DecorationBlock) || has_decoration(type_id, DecorationBufferBlock))) |
| 10239 |
flags = get_buffer_block_flags(id); |
| 10240 |
else |
| 10241 |
flags = get_decoration_bitset(id); |
| 10242 |
} |
| 10243 |
else |
| 10244 |
flags = get_decoration_bitset(id); |
| 10245 |
|
| 10246 |
return flags.get(DecorationRestrict) ? (space ? "restrict " : "restrict") : ""; |
| 10247 |
} |
| 10248 |
|
| 10249 |
string CompilerMSL::entry_point_arg_stage_in() |
| 10250 |
{ |
| 10251 |
string decl; |
| 10252 |
|
| 10253 |
if (get_execution_model() == ExecutionModelTessellationControl && msl_options.multi_patch_workgroup) |
| 10254 |
return decl; |
| 10255 |
|
| 10256 |
// Stage-in structure |
| 10257 |
uint32_t stage_in_id; |
| 10258 |
if (get_execution_model() == ExecutionModelTessellationEvaluation) |
| 10259 |
stage_in_id = patch_stage_in_var_id; |
| 10260 |
else |
| 10261 |
stage_in_id = stage_in_var_id; |
| 10262 |
|
| 10263 |
if (stage_in_id) |
| 10264 |
{ |
| 10265 |
auto &var = get<SPIRVariable>(stage_in_id); |
| 10266 |
auto &type = get_variable_data_type(var); |
| 10267 |
|
| 10268 |
add_resource_name(var.self); |
| 10269 |
decl = join(type_to_glsl(type), " ", to_name(var.self), " [[stage_in]]"); |
| 10270 |
} |
| 10271 |
|
| 10272 |
return decl; |
| 10273 |
} |
| 10274 |
|
| 10275 |
// Returns true if this input builtin should be a direct parameter on a shader function parameter list, |
| 10276 |
// and false for builtins that should be passed or calculated some other way. |
| 10277 |
bool CompilerMSL::is_direct_input_builtin(BuiltIn bi_type) |
| 10278 |
{ |
| 10279 |
switch (bi_type) |
| 10280 |
{ |
| 10281 |
// Vertex function in |
| 10282 |
case BuiltInVertexId: |
| 10283 |
case BuiltInVertexIndex: |
| 10284 |
case BuiltInBaseVertex: |
| 10285 |
case BuiltInInstanceId: |
| 10286 |
case BuiltInInstanceIndex: |
| 10287 |
case BuiltInBaseInstance: |
| 10288 |
return get_execution_model() != ExecutionModelVertex || !msl_options.vertex_for_tessellation; |
| 10289 |
// Tess. control function in |
| 10290 |
case BuiltInPosition: |
| 10291 |
case BuiltInPointSize: |
| 10292 |
case BuiltInClipDistance: |
| 10293 |
case BuiltInCullDistance: |
| 10294 |
case BuiltInPatchVertices: |
| 10295 |
return false; |
| 10296 |
case BuiltInInvocationId: |
| 10297 |
case BuiltInPrimitiveId: |
| 10298 |
return get_execution_model() != ExecutionModelTessellationControl || !msl_options.multi_patch_workgroup; |
| 10299 |
// Tess. evaluation function in |
| 10300 |
case BuiltInTessLevelInner: |
| 10301 |
case BuiltInTessLevelOuter: |
| 10302 |
return false; |
| 10303 |
// Fragment function in |
| 10304 |
case BuiltInSamplePosition: |
| 10305 |
case BuiltInHelperInvocation: |
| 10306 |
case BuiltInBaryCoordNV: |
| 10307 |
case BuiltInBaryCoordNoPerspNV: |
| 10308 |
return false; |
| 10309 |
case BuiltInViewIndex: |
| 10310 |
return get_execution_model() == ExecutionModelFragment && msl_options.multiview && |
| 10311 |
msl_options.multiview_layered_rendering; |
| 10312 |
// Any stage function in |
| 10313 |
case BuiltInDeviceIndex: |
| 10314 |
case BuiltInSubgroupEqMask: |
| 10315 |
case BuiltInSubgroupGeMask: |
| 10316 |
case BuiltInSubgroupGtMask: |
| 10317 |
case BuiltInSubgroupLeMask: |
| 10318 |
case BuiltInSubgroupLtMask: |
| 10319 |
return false; |
| 10320 |
case BuiltInSubgroupLocalInvocationId: |
| 10321 |
case BuiltInSubgroupSize: |
| 10322 |
return get_execution_model() == ExecutionModelGLCompute || |
| 10323 |
(get_execution_model() == ExecutionModelFragment && msl_options.supports_msl_version(2, 2)); |
| 10324 |
default: |
| 10325 |
return true; |
| 10326 |
} |
| 10327 |
} |
| 10328 |
|
| 10329 |
void CompilerMSL::entry_point_args_builtin(string &ep_args) |
| 10330 |
{ |
| 10331 |
// Builtin variables |
| 10332 |
SmallVector<pair<SPIRVariable *, BuiltIn>, 8> active_builtins; |
| 10333 |
ir.for_each_typed_id<SPIRVariable>([&](uint32_t var_id, SPIRVariable &var) { |
| 10334 |
auto bi_type = BuiltIn(get_decoration(var_id, DecorationBuiltIn)); |
| 10335 |
|
| 10336 |
// Don't emit SamplePosition as a separate parameter. In the entry |
| 10337 |
// point, we get that by calling get_sample_position() on the sample ID. |
| 10338 |
if (var.storage == StorageClassInput && is_builtin_variable(var) && |
| 10339 |
get_variable_data_type(var).basetype != SPIRType::Struct && |
| 10340 |
get_variable_data_type(var).basetype != SPIRType::ControlPointArray) |
| 10341 |
{ |
| 10342 |
// If the builtin is not part of the active input builtin set, don't emit it. |
| 10343 |
// Relevant for multiple entry-point modules which might declare unused builtins. |
| 10344 |
if (!active_input_builtins.get(bi_type) || !interface_variable_exists_in_entry_point(var_id)) |
| 10345 |
return; |
| 10346 |
|
| 10347 |
// Remember this variable. We may need to correct its type. |
| 10348 |
active_builtins.push_back(make_pair(&var, bi_type)); |
| 10349 |
|
| 10350 |
if (is_direct_input_builtin(bi_type)) |
| 10351 |
{ |
| 10352 |
if (!ep_args.empty()) |
| 10353 |
ep_args += ", "; |
| 10354 |
|
| 10355 |
// Handle HLSL-style 0-based vertex/instance index. |
| 10356 |
builtin_declaration = true; |
| 10357 |
ep_args += builtin_type_decl(bi_type, var_id) + " " + to_expression(var_id); |
| 10358 |
ep_args += " [[" + builtin_qualifier(bi_type); |
| 10359 |
if (bi_type == BuiltInSampleMask && get_entry_point().flags.get(ExecutionModePostDepthCoverage)) |
| 10360 |
{ |
| 10361 |
if (!msl_options.supports_msl_version(2)) |
| 10362 |
SPIRV_CROSS_THROW("Post-depth coverage requires MSL 2.0."); |
| 10363 |
if (msl_options.is_macos() && !msl_options.supports_msl_version(2, 3)) |
| 10364 |
SPIRV_CROSS_THROW("Post-depth coverage on Mac requires MSL 2.3."); |
| 10365 |
ep_args += ", post_depth_coverage"; |
| 10366 |
} |
| 10367 |
ep_args += "]]"; |
| 10368 |
builtin_declaration = false; |
| 10369 |
} |
| 10370 |
} |
| 10371 |
|
| 10372 |
if (var.storage == StorageClassInput && |
| 10373 |
has_extended_decoration(var_id, SPIRVCrossDecorationBuiltInDispatchBase)) |
| 10374 |
{ |
| 10375 |
// This is a special implicit builtin, not corresponding to any SPIR-V builtin, |
| 10376 |
// which holds the base that was passed to vkCmdDispatchBase() or vkCmdDrawIndexed(). If it's present, |
| 10377 |
// assume we emitted it for a good reason. |
| 10378 |
assert(msl_options.supports_msl_version(1, 2)); |
| 10379 |
if (!ep_args.empty()) |
| 10380 |
ep_args += ", "; |
| 10381 |
|
| 10382 |
ep_args += type_to_glsl(get_variable_data_type(var)) + " " + to_expression(var_id) + " [[grid_origin]]"; |
| 10383 |
} |
| 10384 |
|
| 10385 |
if (var.storage == StorageClassInput && |
| 10386 |
has_extended_decoration(var_id, SPIRVCrossDecorationBuiltInStageInputSize)) |
| 10387 |
{ |
| 10388 |
// This is another special implicit builtin, not corresponding to any SPIR-V builtin, |
| 10389 |
// which holds the number of vertices and instances to draw. If it's present, |
| 10390 |
// assume we emitted it for a good reason. |
| 10391 |
assert(msl_options.supports_msl_version(1, 2)); |
| 10392 |
if (!ep_args.empty()) |
| 10393 |
ep_args += ", "; |
| 10394 |
|
| 10395 |
ep_args += type_to_glsl(get_variable_data_type(var)) + " " + to_expression(var_id) + " [[grid_size]]"; |
| 10396 |
} |
| 10397 |
}); |
| 10398 |
|
| 10399 |
// Correct the types of all encountered active builtins. We couldn't do this before |
| 10400 |
// because ensure_correct_builtin_type() may increase the bound, which isn't allowed |
| 10401 |
// while iterating over IDs. |
| 10402 |
for (auto &var : active_builtins) |
| 10403 |
var.first->basetype = ensure_correct_builtin_type(var.first->basetype, var.second); |
| 10404 |
|
| 10405 |
// Handle HLSL-style 0-based vertex/instance index. |
| 10406 |
if (needs_base_vertex_arg == TriState::Yes) |
| 10407 |
ep_args += built_in_func_arg(BuiltInBaseVertex, !ep_args.empty()); |
| 10408 |
|
| 10409 |
if (needs_base_instance_arg == TriState::Yes) |
| 10410 |
ep_args += built_in_func_arg(BuiltInBaseInstance, !ep_args.empty()); |
| 10411 |
|
| 10412 |
if (capture_output_to_buffer) |
| 10413 |
{ |
| 10414 |
// Add parameters to hold the indirect draw parameters and the shader output. This has to be handled |
| 10415 |
// specially because it needs to be a pointer, not a reference. |
| 10416 |
if (stage_out_var_id) |
| 10417 |
{ |
| 10418 |
if (!ep_args.empty()) |
| 10419 |
ep_args += ", "; |
| 10420 |
ep_args += join("device ", type_to_glsl(get_stage_out_struct_type()), "* ", output_buffer_var_name, |
| 10421 |
" [[buffer(", msl_options.shader_output_buffer_index, ")]]"); |
| 10422 |
} |
| 10423 |
|
| 10424 |
if (get_execution_model() == ExecutionModelTessellationControl) |
| 10425 |
{ |
| 10426 |
if (!ep_args.empty()) |
| 10427 |
ep_args += ", "; |
| 10428 |
ep_args += |
| 10429 |
join("constant uint* spvIndirectParams [[buffer(", msl_options.indirect_params_buffer_index, ")]]"); |
| 10430 |
} |
| 10431 |
else if (stage_out_var_id && |
| 10432 |
!(get_execution_model() == ExecutionModelVertex && msl_options.vertex_for_tessellation)) |
| 10433 |
{ |
| 10434 |
if (!ep_args.empty()) |
| 10435 |
ep_args += ", "; |
| 10436 |
ep_args += |
| 10437 |
join("device uint* spvIndirectParams [[buffer(", msl_options.indirect_params_buffer_index, ")]]"); |
| 10438 |
} |
| 10439 |
|
| 10440 |
if (get_execution_model() == ExecutionModelVertex && msl_options.vertex_for_tessellation && |
| 10441 |
(active_input_builtins.get(BuiltInVertexIndex) || active_input_builtins.get(BuiltInVertexId)) && |
| 10442 |
msl_options.vertex_index_type != Options::IndexType::None) |
| 10443 |
{ |
| 10444 |
// Add the index buffer so we can set gl_VertexIndex correctly. |
| 10445 |
if (!ep_args.empty()) |
| 10446 |
ep_args += ", "; |
| 10447 |
switch (msl_options.vertex_index_type) |
| 10448 |
{ |
| 10449 |
case Options::IndexType::None: |
| 10450 |
break; |
| 10451 |
case Options::IndexType::UInt16: |
| 10452 |
ep_args += join("const device ushort* ", index_buffer_var_name, " [[buffer(", |
| 10453 |
msl_options.shader_index_buffer_index, ")]]"); |
| 10454 |
break; |
| 10455 |
case Options::IndexType::UInt32: |
| 10456 |
ep_args += join("const device uint* ", index_buffer_var_name, " [[buffer(", |
| 10457 |
msl_options.shader_index_buffer_index, ")]]"); |
| 10458 |
break; |
| 10459 |
} |
| 10460 |
} |
| 10461 |
|
| 10462 |
// Tessellation control shaders get three additional parameters: |
| 10463 |
// a buffer to hold the per-patch data, a buffer to hold the per-patch |
| 10464 |
// tessellation levels, and a block of workgroup memory to hold the |
| 10465 |
// input control point data. |
| 10466 |
if (get_execution_model() == ExecutionModelTessellationControl) |
| 10467 |
{ |
| 10468 |
if (patch_stage_out_var_id) |
| 10469 |
{ |
| 10470 |
if (!ep_args.empty()) |
| 10471 |
ep_args += ", "; |
| 10472 |
ep_args += |
| 10473 |
join("device ", type_to_glsl(get_patch_stage_out_struct_type()), "* ", patch_output_buffer_var_name, |
| 10474 |
" [[buffer(", convert_to_string(msl_options.shader_patch_output_buffer_index), ")]]"); |
| 10475 |
} |
| 10476 |
if (!ep_args.empty()) |
| 10477 |
ep_args += ", "; |
| 10478 |
ep_args += join("device ", get_tess_factor_struct_name(), "* ", tess_factor_buffer_var_name, " [[buffer(", |
| 10479 |
convert_to_string(msl_options.shader_tess_factor_buffer_index), ")]]"); |
| 10480 |
if (stage_in_var_id) |
| 10481 |
{ |
| 10482 |
if (!ep_args.empty()) |
| 10483 |
ep_args += ", "; |
| 10484 |
if (msl_options.multi_patch_workgroup) |
| 10485 |
{ |
| 10486 |
ep_args += join("device ", type_to_glsl(get_stage_in_struct_type()), "* ", input_buffer_var_name, |
| 10487 |
" [[buffer(", convert_to_string(msl_options.shader_input_buffer_index), ")]]"); |
| 10488 |
} |
| 10489 |
else |
| 10490 |
{ |
| 10491 |
ep_args += join("threadgroup ", type_to_glsl(get_stage_in_struct_type()), "* ", input_wg_var_name, |
| 10492 |
" [[threadgroup(", convert_to_string(msl_options.shader_input_wg_index), ")]]"); |
| 10493 |
} |
| 10494 |
} |
| 10495 |
} |
| 10496 |
} |
| 10497 |
} |
| 10498 |
|
| 10499 |
string CompilerMSL::entry_point_args_argument_buffer(bool append_comma) |
| 10500 |
{ |
| 10501 |
string ep_args = entry_point_arg_stage_in(); |
| 10502 |
Bitset claimed_bindings; |
| 10503 |
|
| 10504 |
for (uint32_t i = 0; i < kMaxArgumentBuffers; i++) |
| 10505 |
{ |
| 10506 |
uint32_t id = argument_buffer_ids[i]; |
| 10507 |
if (id == 0) |
| 10508 |
continue; |
| 10509 |
|
| 10510 |
add_resource_name(id); |
| 10511 |
auto &var = get<SPIRVariable>(id); |
| 10512 |
auto &type = get_variable_data_type(var); |
| 10513 |
|
| 10514 |
if (!ep_args.empty()) |
| 10515 |
ep_args += ", "; |
| 10516 |
|
| 10517 |
// Check if the argument buffer binding itself has been remapped. |
| 10518 |
uint32_t buffer_binding; |
| 10519 |
auto itr = resource_bindings.find({ get_entry_point().model, i, kArgumentBufferBinding }); |
| 10520 |
if (itr != end(resource_bindings)) |
| 10521 |
{ |
| 10522 |
buffer_binding = itr->second.first.msl_buffer; |
| 10523 |
itr->second.second = true; |
| 10524 |
} |
| 10525 |
else |
| 10526 |
{ |
| 10527 |
// As a fallback, directly map desc set <-> binding. |
| 10528 |
// If that was taken, take the next buffer binding. |
| 10529 |
if (claimed_bindings.get(i)) |
| 10530 |
buffer_binding = next_metal_resource_index_buffer; |
| 10531 |
else |
| 10532 |
buffer_binding = i; |
| 10533 |
} |
| 10534 |
|
| 10535 |
claimed_bindings.set(buffer_binding); |
| 10536 |
|
| 10537 |
ep_args += get_argument_address_space(var) + " " + type_to_glsl(type) + "& " + to_restrict(id) + to_name(id); |
| 10538 |
ep_args += " [[buffer(" + convert_to_string(buffer_binding) + ")]]"; |
| 10539 |
|
| 10540 |
next_metal_resource_index_buffer = max(next_metal_resource_index_buffer, buffer_binding + 1); |
| 10541 |
} |
| 10542 |
|
| 10543 |
entry_point_args_discrete_descriptors(ep_args); |
| 10544 |
entry_point_args_builtin(ep_args); |
| 10545 |
|
| 10546 |
if (!ep_args.empty() && append_comma) |
| 10547 |
ep_args += ", "; |
| 10548 |
|
| 10549 |
return ep_args; |
| 10550 |
} |
| 10551 |
|
| 10552 |
const MSLConstexprSampler *CompilerMSL::find_constexpr_sampler(uint32_t id) const |
| 10553 |
{ |
| 10554 |
// Try by ID. |
| 10555 |
{ |
| 10556 |
auto itr = constexpr_samplers_by_id.find(id); |
| 10557 |
if (itr != end(constexpr_samplers_by_id)) |
| 10558 |
return &itr->second; |
| 10559 |
} |
| 10560 |
|
| 10561 |
// Try by binding. |
| 10562 |
{ |
| 10563 |
uint32_t desc_set = get_decoration(id, DecorationDescriptorSet); |
| 10564 |
uint32_t binding = get_decoration(id, DecorationBinding); |
| 10565 |
|
| 10566 |
auto itr = constexpr_samplers_by_binding.find({ desc_set, binding }); |
| 10567 |
if (itr != end(constexpr_samplers_by_binding)) |
| 10568 |
return &itr->second; |
| 10569 |
} |
| 10570 |
|
| 10571 |
return nullptr; |
| 10572 |
} |
| 10573 |
|
| 10574 |
void CompilerMSL::entry_point_args_discrete_descriptors(string &ep_args) |
| 10575 |
{ |
| 10576 |
// Output resources, sorted by resource index & type |
| 10577 |
// We need to sort to work around a bug on macOS 10.13 with NVidia drivers where switching between shaders |
| 10578 |
// with different order of buffers can result in issues with buffer assignments inside the driver. |
| 10579 |
struct Resource |
| 10580 |
{ |
| 10581 |
SPIRVariable *var; |
| 10582 |
string name; |
| 10583 |
SPIRType::BaseType basetype; |
| 10584 |
uint32_t index; |
| 10585 |
uint32_t plane; |
| 10586 |
uint32_t secondary_index; |
| 10587 |
}; |
| 10588 |
|
| 10589 |
SmallVector<Resource> resources; |
| 10590 |
|
| 10591 |
ir.for_each_typed_id<SPIRVariable>([&](uint32_t var_id, SPIRVariable &var) { |
| 10592 |
if ((var.storage == StorageClassUniform || var.storage == StorageClassUniformConstant || |
| 10593 |
var.storage == StorageClassPushConstant || var.storage == StorageClassStorageBuffer) && |
| 10594 |
!is_hidden_variable(var)) |
| 10595 |
{ |
| 10596 |
auto &type = get_variable_data_type(var); |
| 10597 |
|
| 10598 |
// Very specifically, image load-store in argument buffers are disallowed on MSL on iOS. |
| 10599 |
// But we won't know when the argument buffer is encoded whether this image will have |
| 10600 |
// a NonWritable decoration. So just use discrete arguments for all storage images |
| 10601 |
// on iOS. |
| 10602 |
if (!(msl_options.is_ios() && type.basetype == SPIRType::Image && type.image.sampled == 2) && |
| 10603 |
var.storage != StorageClassPushConstant) |
| 10604 |
{ |
| 10605 |
uint32_t desc_set = get_decoration(var_id, DecorationDescriptorSet); |
| 10606 |
if (descriptor_set_is_argument_buffer(desc_set)) |
| 10607 |
return; |
| 10608 |
} |
| 10609 |
|
| 10610 |
const MSLConstexprSampler *constexpr_sampler = nullptr; |
| 10611 |
if (type.basetype == SPIRType::SampledImage || type.basetype == SPIRType::Sampler) |
| 10612 |
{ |
| 10613 |
constexpr_sampler = find_constexpr_sampler(var_id); |
| 10614 |
if (constexpr_sampler) |
| 10615 |
{ |
| 10616 |
// Mark this ID as a constexpr sampler for later in case it came from set/bindings. |
| 10617 |
constexpr_samplers_by_id[var_id] = *constexpr_sampler; |
| 10618 |
} |
| 10619 |
} |
| 10620 |
|
| 10621 |
// Emulate texture2D atomic operations |
| 10622 |
uint32_t secondary_index = 0; |
| 10623 |
if (atomic_image_vars.count(var.self)) |
| 10624 |
{ |
| 10625 |
secondary_index = get_metal_resource_index(var, SPIRType::AtomicCounter, 0); |
| 10626 |
} |
| 10627 |
|
| 10628 |
if (type.basetype == SPIRType::SampledImage) |
| 10629 |
{ |
| 10630 |
add_resource_name(var_id); |
| 10631 |
|
| 10632 |
uint32_t plane_count = 1; |
| 10633 |
if (constexpr_sampler && constexpr_sampler->ycbcr_conversion_enable) |
| 10634 |
plane_count = constexpr_sampler->planes; |
| 10635 |
|
| 10636 |
for (uint32_t i = 0; i < plane_count; i++) |
| 10637 |
resources.push_back({ &var, to_name(var_id), SPIRType::Image, |
| 10638 |
get_metal_resource_index(var, SPIRType::Image, i), i, secondary_index }); |
| 10639 |
|
| 10640 |
if (type.image.dim != DimBuffer && !constexpr_sampler) |
| 10641 |
{ |
| 10642 |
resources.push_back({ &var, to_sampler_expression(var_id), SPIRType::Sampler, |
| 10643 |
get_metal_resource_index(var, SPIRType::Sampler), 0, 0 }); |
| 10644 |
} |
| 10645 |
} |
| 10646 |
else if (!constexpr_sampler) |
| 10647 |
{ |
| 10648 |
// constexpr samplers are not declared as resources. |
| 10649 |
add_resource_name(var_id); |
| 10650 |
resources.push_back({ &var, to_name(var_id), type.basetype, |
| 10651 |
get_metal_resource_index(var, type.basetype), 0, secondary_index }); |
| 10652 |
} |
| 10653 |
} |
| 10654 |
}); |
| 10655 |
|
| 10656 |
sort(resources.begin(), resources.end(), [](const Resource &lhs, const Resource &rhs) { |
| 10657 |
return tie(lhs.basetype, lhs.index) < tie(rhs.basetype, rhs.index); |
| 10658 |
}); |
| 10659 |
|
| 10660 |
for (auto &r : resources) |
| 10661 |
{ |
| 10662 |
auto &var = *r.var; |
| 10663 |
auto &type = get_variable_data_type(var); |
| 10664 |
|
| 10665 |
uint32_t var_id = var.self; |
| 10666 |
|
| 10667 |
switch (r.basetype) |
| 10668 |
{ |
| 10669 |
case SPIRType::Struct: |
| 10670 |
{ |
| 10671 |
auto &m = ir.meta[type.self]; |
| 10672 |
if (m.members.size() == 0) |
| 10673 |
break; |
| 10674 |
if (!type.array.empty()) |
| 10675 |
{ |
| 10676 |
if (type.array.size() > 1) |
| 10677 |
SPIRV_CROSS_THROW("Arrays of arrays of buffers are not supported."); |
| 10678 |
|
| 10679 |
// Metal doesn't directly support this, so we must expand the |
| 10680 |
// array. We'll declare a local array to hold these elements |
| 10681 |
// later. |
| 10682 |
uint32_t array_size = to_array_size_literal(type); |
| 10683 |
|
| 10684 |
if (array_size == 0) |
| 10685 |
SPIRV_CROSS_THROW("Unsized arrays of buffers are not supported in MSL."); |
| 10686 |
|
| 10687 |
// Allow Metal to use the array<T> template to make arrays a value type |
| 10688 |
is_using_builtin_array = true; |
| 10689 |
buffer_arrays.push_back(var_id); |
| 10690 |
for (uint32_t i = 0; i < array_size; ++i) |
| 10691 |
{ |
| 10692 |
if (!ep_args.empty()) |
| 10693 |
ep_args += ", "; |
| 10694 |
ep_args += get_argument_address_space(var) + " " + type_to_glsl(type) + "* " + to_restrict(var_id) + |
| 10695 |
r.name + "_" + convert_to_string(i); |
| 10696 |
ep_args += " [[buffer(" + convert_to_string(r.index + i) + ")"; |
| 10697 |
if (interlocked_resources.count(var_id)) |
| 10698 |
ep_args += ", raster_order_group(0)"; |
| 10699 |
ep_args += "]]"; |
| 10700 |
} |
| 10701 |
is_using_builtin_array = false; |
| 10702 |
} |
| 10703 |
else |
| 10704 |
{ |
| 10705 |
if (!ep_args.empty()) |
| 10706 |
ep_args += ", "; |
| 10707 |
ep_args += |
| 10708 |
get_argument_address_space(var) + " " + type_to_glsl(type) + "& " + to_restrict(var_id) + r.name; |
| 10709 |
ep_args += " [[buffer(" + convert_to_string(r.index) + ")"; |
| 10710 |
if (interlocked_resources.count(var_id)) |
| 10711 |
ep_args += ", raster_order_group(0)"; |
| 10712 |
ep_args += "]]"; |
| 10713 |
} |
| 10714 |
break; |
| 10715 |
} |
| 10716 |
case SPIRType::Sampler: |
| 10717 |
if (!ep_args.empty()) |
| 10718 |
ep_args += ", "; |
| 10719 |
ep_args += sampler_type(type, var_id) + " " + r.name; |
| 10720 |
ep_args += " [[sampler(" + convert_to_string(r.index) + ")]]"; |
| 10721 |
break; |
| 10722 |
case SPIRType::Image: |
| 10723 |
{ |
| 10724 |
if (!ep_args.empty()) |
| 10725 |
ep_args += ", "; |
| 10726 |
|
| 10727 |
// Use Metal's native frame-buffer fetch API for subpass inputs. |
| 10728 |
const auto &basetype = get<SPIRType>(var.basetype); |
| 10729 |
if (!type_is_msl_framebuffer_fetch(basetype)) |
| 10730 |
{ |
| 10731 |
ep_args += image_type_glsl(type, var_id) + " " + r.name; |
| 10732 |
if (r.plane > 0) |
| 10733 |
ep_args += join(plane_name_suffix, r.plane); |
| 10734 |
ep_args += " [[texture(" + convert_to_string(r.index) + ")"; |
| 10735 |
if (interlocked_resources.count(var_id)) |
| 10736 |
ep_args += ", raster_order_group(0)"; |
| 10737 |
ep_args += "]]"; |
| 10738 |
} |
| 10739 |
else |
| 10740 |
{ |
| 10741 |
if (msl_options.is_macos() && !msl_options.supports_msl_version(2, 3)) |
| 10742 |
SPIRV_CROSS_THROW("Framebuffer fetch on Mac is not supported before MSL 2.3."); |
| 10743 |
ep_args += image_type_glsl(type, var_id) + " " + r.name; |
| 10744 |
ep_args += " [[color(" + convert_to_string(r.index) + ")]]"; |
| 10745 |
} |
| 10746 |
|
| 10747 |
// Emulate texture2D atomic operations |
| 10748 |
if (atomic_image_vars.count(var.self)) |
| 10749 |
{ |
| 10750 |
ep_args += ", device atomic_" + type_to_glsl(get<SPIRType>(basetype.image.type), 0); |
| 10751 |
ep_args += "* " + r.name + "_atomic"; |
| 10752 |
ep_args += " [[buffer(" + convert_to_string(r.secondary_index) + ")"; |
| 10753 |
if (interlocked_resources.count(var_id)) |
| 10754 |
ep_args += ", raster_order_group(0)"; |
| 10755 |
ep_args += "]]"; |
| 10756 |
} |
| 10757 |
break; |
| 10758 |
} |
| 10759 |
default: |
| 10760 |
if (!ep_args.empty()) |
| 10761 |
ep_args += ", "; |
| 10762 |
if (!type.pointer) |
| 10763 |
ep_args += get_type_address_space(get<SPIRType>(var.basetype), var_id) + " " + |
| 10764 |
type_to_glsl(type, var_id) + "& " + r.name; |
| 10765 |
else |
| 10766 |
ep_args += type_to_glsl(type, var_id) + " " + r.name; |
| 10767 |
ep_args += " [[buffer(" + convert_to_string(r.index) + ")"; |
| 10768 |
if (interlocked_resources.count(var_id)) |
| 10769 |
ep_args += ", raster_order_group(0)"; |
| 10770 |
ep_args += "]]"; |
| 10771 |
break; |
| 10772 |
} |
| 10773 |
} |
| 10774 |
} |
| 10775 |
|
| 10776 |
// Returns a string containing a comma-delimited list of args for the entry point function |
| 10777 |
// This is the "classic" method of MSL 1 when we don't have argument buffer support. |
| 10778 |
string CompilerMSL::entry_point_args_classic(bool append_comma) |
| 10779 |
{ |
| 10780 |
string ep_args = entry_point_arg_stage_in(); |
| 10781 |
entry_point_args_discrete_descriptors(ep_args); |
| 10782 |
entry_point_args_builtin(ep_args); |
| 10783 |
|
| 10784 |
if (!ep_args.empty() && append_comma) |
| 10785 |
ep_args += ", "; |
| 10786 |
|
| 10787 |
return ep_args; |
| 10788 |
} |
| 10789 |
|
| 10790 |
void CompilerMSL::fix_up_shader_inputs_outputs() |
| 10791 |
{ |
| 10792 |
auto &entry_func = this->get<SPIRFunction>(ir.default_entry_point); |
| 10793 |
|
| 10794 |
// Emit a guard to ensure we don't execute beyond the last vertex. |
| 10795 |
// Vertex shaders shouldn't have the problems with barriers in non-uniform control flow that |
| 10796 |
// tessellation control shaders do, so early returns should be OK. We may need to revisit this |
| 10797 |
// if it ever becomes possible to use barriers from a vertex shader. |
| 10798 |
if (get_execution_model() == ExecutionModelVertex && msl_options.vertex_for_tessellation) |
| 10799 |
{ |
| 10800 |
entry_func.fixup_hooks_in.push_back([this]() { |
| 10801 |
statement("if (any(", to_expression(builtin_invocation_id_id), |
| 10802 |
" >= ", to_expression(builtin_stage_input_size_id), "))"); |
| 10803 |
statement(" return;"); |
| 10804 |
}); |
| 10805 |
} |
| 10806 |
|
| 10807 |
// Look for sampled images and buffer. Add hooks to set up the swizzle constants or array lengths. |
| 10808 |
ir.for_each_typed_id<SPIRVariable>([&](uint32_t, SPIRVariable &var) { |
| 10809 |
auto &type = get_variable_data_type(var); |
| 10810 |
uint32_t var_id = var.self; |
| 10811 |
bool ssbo = has_decoration(type.self, DecorationBufferBlock); |
| 10812 |
|
| 10813 |
if (var.storage == StorageClassUniformConstant && !is_hidden_variable(var)) |
| 10814 |
{ |
| 10815 |
if (msl_options.swizzle_texture_samples && has_sampled_images && is_sampled_image_type(type)) |
| 10816 |
{ |
| 10817 |
entry_func.fixup_hooks_in.push_back([this, &type, &var, var_id]() { |
| 10818 |
bool is_array_type = !type.array.empty(); |
| 10819 |
|
| 10820 |
uint32_t desc_set = get_decoration(var_id, DecorationDescriptorSet); |
| 10821 |
if (descriptor_set_is_argument_buffer(desc_set)) |
| 10822 |
{ |
| 10823 |
statement("constant uint", is_array_type ? "* " : "& ", to_swizzle_expression(var_id), |
| 10824 |
is_array_type ? " = &" : " = ", to_name(argument_buffer_ids[desc_set]), |
| 10825 |
".spvSwizzleConstants", "[", |
| 10826 |
convert_to_string(get_metal_resource_index(var, SPIRType::Image)), "];"); |
| 10827 |
} |
| 10828 |
else |
| 10829 |
{ |
| 10830 |
// If we have an array of images, we need to be able to index into it, so take a pointer instead. |
| 10831 |
statement("constant uint", is_array_type ? "* " : "& ", to_swizzle_expression(var_id), |
| 10832 |
is_array_type ? " = &" : " = ", to_name(swizzle_buffer_id), "[", |
| 10833 |
convert_to_string(get_metal_resource_index(var, SPIRType::Image)), "];"); |
| 10834 |
} |
| 10835 |
}); |
| 10836 |
} |
| 10837 |
} |
| 10838 |
else if ((var.storage == StorageClassStorageBuffer || (var.storage == StorageClassUniform && ssbo)) && |
| 10839 |
!is_hidden_variable(var)) |
| 10840 |
{ |
| 10841 |
if (buffers_requiring_array_length.count(var.self)) |
| 10842 |
{ |
| 10843 |
entry_func.fixup_hooks_in.push_back([this, &type, &var, var_id]() { |
| 10844 |
bool is_array_type = !type.array.empty(); |
| 10845 |
|
| 10846 |
uint32_t desc_set = get_decoration(var_id, DecorationDescriptorSet); |
| 10847 |
if (descriptor_set_is_argument_buffer(desc_set)) |
| 10848 |
{ |
| 10849 |
statement("constant uint", is_array_type ? "* " : "& ", to_buffer_size_expression(var_id), |
| 10850 |
is_array_type ? " = &" : " = ", to_name(argument_buffer_ids[desc_set]), |
| 10851 |
".spvBufferSizeConstants", "[", |
| 10852 |
convert_to_string(get_metal_resource_index(var, SPIRType::Image)), "];"); |
| 10853 |
} |
| 10854 |
else |
| 10855 |
{ |
| 10856 |
// If we have an array of images, we need to be able to index into it, so take a pointer instead. |
| 10857 |
statement("constant uint", is_array_type ? "* " : "& ", to_buffer_size_expression(var_id), |
| 10858 |
is_array_type ? " = &" : " = ", to_name(buffer_size_buffer_id), "[", |
| 10859 |
convert_to_string(get_metal_resource_index(var, type.basetype)), "];"); |
| 10860 |
} |
| 10861 |
}); |
| 10862 |
} |
| 10863 |
} |
| 10864 |
}); |
| 10865 |
|
| 10866 |
// Builtin variables |
| 10867 |
ir.for_each_typed_id<SPIRVariable>([this, &entry_func](uint32_t, SPIRVariable &var) { |
| 10868 |
uint32_t var_id = var.self; |
| 10869 |
BuiltIn bi_type = ir.meta[var_id].decoration.builtin_type; |
| 10870 |
|
| 10871 |
if (var.storage == StorageClassInput && is_builtin_variable(var)) |
| 10872 |
{ |
| 10873 |
switch (bi_type) |
| 10874 |
{ |
| 10875 |
case BuiltInSamplePosition: |
| 10876 |
entry_func.fixup_hooks_in.push_back([=]() { |
| 10877 |
statement(builtin_type_decl(bi_type), " ", to_expression(var_id), " = get_sample_position(", |
| 10878 |
to_expression(builtin_sample_id_id), ");"); |
| 10879 |
}); |
| 10880 |
break; |
| 10881 |
case BuiltInHelperInvocation: |
| 10882 |
if (msl_options.is_ios()) |
| 10883 |
SPIRV_CROSS_THROW("simd_is_helper_thread() is only supported on macOS."); |
| 10884 |
else if (msl_options.is_macos() && !msl_options.supports_msl_version(2, 1)) |
| 10885 |
SPIRV_CROSS_THROW("simd_is_helper_thread() requires version 2.1 on macOS."); |
| 10886 |
|
| 10887 |
entry_func.fixup_hooks_in.push_back([=]() { |
| 10888 |
statement(builtin_type_decl(bi_type), " ", to_expression(var_id), " = simd_is_helper_thread();"); |
| 10889 |
}); |
| 10890 |
break; |
| 10891 |
case BuiltInInvocationId: |
| 10892 |
// This is direct-mapped without multi-patch workgroups. |
| 10893 |
if (get_execution_model() != ExecutionModelTessellationControl || !msl_options.multi_patch_workgroup) |
| 10894 |
break; |
| 10895 |
|
| 10896 |
entry_func.fixup_hooks_in.push_back([=]() { |
| 10897 |
statement(builtin_type_decl(bi_type), " ", to_expression(var_id), " = ", |
| 10898 |
to_expression(builtin_invocation_id_id), ".x % ", this->get_entry_point().output_vertices, |
| 10899 |
";"); |
| 10900 |
}); |
| 10901 |
break; |
| 10902 |
case BuiltInPrimitiveId: |
| 10903 |
// This is natively supported by fragment and tessellation evaluation shaders. |
| 10904 |
// In tessellation control shaders, this is direct-mapped without multi-patch workgroups. |
| 10905 |
if (get_execution_model() != ExecutionModelTessellationControl || !msl_options.multi_patch_workgroup) |
| 10906 |
break; |
| 10907 |
|
| 10908 |
entry_func.fixup_hooks_in.push_back([=]() { |
| 10909 |
statement(builtin_type_decl(bi_type), " ", to_expression(var_id), " = min(", |
| 10910 |
to_expression(builtin_invocation_id_id), ".x / ", this->get_entry_point().output_vertices, |
| 10911 |
", spvIndirectParams[1]);"); |
| 10912 |
}); |
| 10913 |
break; |
| 10914 |
case BuiltInPatchVertices: |
| 10915 |
if (get_execution_model() == ExecutionModelTessellationEvaluation) |
| 10916 |
entry_func.fixup_hooks_in.push_back([=]() { |
| 10917 |
statement(builtin_type_decl(bi_type), " ", to_expression(var_id), " = ", |
| 10918 |
to_expression(patch_stage_in_var_id), ".gl_in.size();"); |
| 10919 |
}); |
| 10920 |
else |
| 10921 |
entry_func.fixup_hooks_in.push_back([=]() { |
| 10922 |
statement(builtin_type_decl(bi_type), " ", to_expression(var_id), " = spvIndirectParams[0];"); |
| 10923 |
}); |
| 10924 |
break; |
| 10925 |
case BuiltInTessCoord: |
| 10926 |
// Emit a fixup to account for the shifted domain. Don't do this for triangles; |
| 10927 |
// MoltenVK will just reverse the winding order instead. |
| 10928 |
if (msl_options.tess_domain_origin_lower_left && !get_entry_point().flags.get(ExecutionModeTriangles)) |
| 10929 |
{ |
| 10930 |
string tc = to_expression(var_id); |
| 10931 |
entry_func.fixup_hooks_in.push_back([=]() { statement(tc, ".y = 1.0 - ", tc, ".y;"); }); |
| 10932 |
} |
| 10933 |
break; |
| 10934 |
case BuiltInSubgroupLocalInvocationId: |
| 10935 |
// This is natively supported in compute shaders. |
| 10936 |
if (get_execution_model() == ExecutionModelGLCompute) |
| 10937 |
break; |
| 10938 |
|
| 10939 |
// This is natively supported in fragment shaders in MSL 2.2. |
| 10940 |
if (get_execution_model() == ExecutionModelFragment && msl_options.supports_msl_version(2, 2)) |
| 10941 |
break; |
| 10942 |
|
| 10943 |
if (msl_options.is_ios()) |
| 10944 |
SPIRV_CROSS_THROW( |
| 10945 |
"SubgroupLocalInvocationId cannot be used outside of compute shaders before MSL 2.2 on iOS."); |
| 10946 |
|
| 10947 |
if (!msl_options.supports_msl_version(2, 1)) |
| 10948 |
SPIRV_CROSS_THROW( |
| 10949 |
"SubgroupLocalInvocationId cannot be used outside of compute shaders before MSL 2.1."); |
| 10950 |
|
| 10951 |
// Shaders other than compute shaders don't support the SIMD-group |
| 10952 |
// builtins directly, but we can emulate them using the SIMD-group |
| 10953 |
// functions. This might break if some of the subgroup terminated |
| 10954 |
// before reaching the entry point. |
| 10955 |
entry_func.fixup_hooks_in.push_back([=]() { |
| 10956 |
statement(builtin_type_decl(bi_type), " ", to_expression(var_id), |
| 10957 |
" = simd_prefix_exclusive_sum(1);"); |
| 10958 |
}); |
| 10959 |
break; |
| 10960 |
case BuiltInSubgroupSize: |
| 10961 |
// This is natively supported in compute shaders. |
| 10962 |
if (get_execution_model() == ExecutionModelGLCompute) |
| 10963 |
break; |
| 10964 |
|
| 10965 |
// This is natively supported in fragment shaders in MSL 2.2. |
| 10966 |
if (get_execution_model() == ExecutionModelFragment && msl_options.supports_msl_version(2, 2)) |
| 10967 |
break; |
| 10968 |
|
| 10969 |
if (msl_options.is_ios()) |
| 10970 |
SPIRV_CROSS_THROW("SubgroupSize cannot be used outside of compute shaders on iOS."); |
| 10971 |
|
| 10972 |
if (!msl_options.supports_msl_version(2, 1)) |
| 10973 |
SPIRV_CROSS_THROW("SubgroupSize cannot be used outside of compute shaders before Metal 2.1."); |
| 10974 |
|
| 10975 |
entry_func.fixup_hooks_in.push_back( |
| 10976 |
[=]() { statement(builtin_type_decl(bi_type), " ", to_expression(var_id), " = simd_sum(1);"); }); |
| 10977 |
break; |
| 10978 |
case BuiltInSubgroupEqMask: |
| 10979 |
if (msl_options.is_ios()) |
| 10980 |
SPIRV_CROSS_THROW("Subgroup ballot functionality is unavailable on iOS."); |
| 10981 |
if (!msl_options.supports_msl_version(2, 1)) |
| 10982 |
SPIRV_CROSS_THROW("Subgroup ballot functionality requires Metal 2.1."); |
| 10983 |
entry_func.fixup_hooks_in.push_back([=]() { |
| 10984 |
statement(builtin_type_decl(bi_type), " ", to_expression(var_id), " = ", |
| 10985 |
to_expression(builtin_subgroup_invocation_id_id), " >= 32 ? uint4(0, (1 << (", |
| 10986 |
to_expression(builtin_subgroup_invocation_id_id), " - 32)), uint2(0)) : uint4(1 << ", |
| 10987 |
to_expression(builtin_subgroup_invocation_id_id), ", uint3(0));"); |
| 10988 |
}); |
| 10989 |
break; |
| 10990 |
case BuiltInSubgroupGeMask: |
| 10991 |
if (msl_options.is_ios()) |
| 10992 |
SPIRV_CROSS_THROW("Subgroup ballot functionality is unavailable on iOS."); |
| 10993 |
if (!msl_options.supports_msl_version(2, 1)) |
| 10994 |
SPIRV_CROSS_THROW("Subgroup ballot functionality requires Metal 2.1."); |
| 10995 |
entry_func.fixup_hooks_in.push_back([=]() { |
| 10996 |
// Case where index < 32, size < 32: |
| 10997 |
// mask0 = bfi(0, 0xFFFFFFFF, index, size - index); |
| 10998 |
// mask1 = bfi(0, 0xFFFFFFFF, 0, 0); // Gives 0 |
| 10999 |
// Case where index < 32 but size >= 32: |
| 11000 |
// mask0 = bfi(0, 0xFFFFFFFF, index, 32 - index); |
| 11001 |
// mask1 = bfi(0, 0xFFFFFFFF, 0, size - 32); |
| 11002 |
// Case where index >= 32: |
| 11003 |
// mask0 = bfi(0, 0xFFFFFFFF, 32, 0); // Gives 0 |
| 11004 |
// mask1 = bfi(0, 0xFFFFFFFF, index - 32, size - index); |
| 11005 |
// This is expressed without branches to avoid divergent |
| 11006 |
// control flow--hence the complicated min/max expressions. |
| 11007 |
// This is further complicated by the fact that if you attempt |
| 11008 |
// to bfi/bfe out-of-bounds on Metal, undefined behavior is the |
| 11009 |
// result. |
| 11010 |
statement(builtin_type_decl(bi_type), " ", to_expression(var_id), |
| 11011 |
" = uint4(insert_bits(0u, 0xFFFFFFFF, min(", |
| 11012 |
to_expression(builtin_subgroup_invocation_id_id), ", 32u), (uint)max(min((int)", |
| 11013 |
to_expression(builtin_subgroup_size_id), ", 32) - (int)", |
| 11014 |
to_expression(builtin_subgroup_invocation_id_id), |
| 11015 |
", 0)), insert_bits(0u, 0xFFFFFFFF, (uint)max((int)", |
| 11016 |
to_expression(builtin_subgroup_invocation_id_id), " - 32, 0), (uint)max((int)", |
| 11017 |
to_expression(builtin_subgroup_size_id), " - (int)max(", |
| 11018 |
to_expression(builtin_subgroup_invocation_id_id), ", 32u), 0)), uint2(0));"); |
| 11019 |
}); |
| 11020 |
break; |
| 11021 |
case BuiltInSubgroupGtMask: |
| 11022 |
if (msl_options.is_ios()) |
| 11023 |
SPIRV_CROSS_THROW("Subgroup ballot functionality is unavailable on iOS."); |
| 11024 |
if (!msl_options.supports_msl_version(2, 1)) |
| 11025 |
SPIRV_CROSS_THROW("Subgroup ballot functionality requires Metal 2.1."); |
| 11026 |
entry_func.fixup_hooks_in.push_back([=]() { |
| 11027 |
// The same logic applies here, except now the index is one |
| 11028 |
// more than the subgroup invocation ID. |
| 11029 |
statement(builtin_type_decl(bi_type), " ", to_expression(var_id), |
| 11030 |
" = uint4(insert_bits(0u, 0xFFFFFFFF, min(", |
| 11031 |
to_expression(builtin_subgroup_invocation_id_id), " + 1, 32u), (uint)max(min((int)", |
| 11032 |
to_expression(builtin_subgroup_size_id), ", 32) - (int)", |
| 11033 |
to_expression(builtin_subgroup_invocation_id_id), |
| 11034 |
" - 1, 0)), insert_bits(0u, 0xFFFFFFFF, (uint)max((int)", |
| 11035 |
to_expression(builtin_subgroup_invocation_id_id), " + 1 - 32, 0), (uint)max((int)", |
| 11036 |
to_expression(builtin_subgroup_size_id), " - (int)max(", |
| 11037 |
to_expression(builtin_subgroup_invocation_id_id), " + 1, 32u), 0)), uint2(0));"); |
| 11038 |
}); |
| 11039 |
break; |
| 11040 |
case BuiltInSubgroupLeMask: |
| 11041 |
if (msl_options.is_ios()) |
| 11042 |
SPIRV_CROSS_THROW("Subgroup ballot functionality is unavailable on iOS."); |
| 11043 |
if (!msl_options.supports_msl_version(2, 1)) |
| 11044 |
SPIRV_CROSS_THROW("Subgroup ballot functionality requires Metal 2.1."); |
| 11045 |
entry_func.fixup_hooks_in.push_back([=]() { |
| 11046 |
statement(builtin_type_decl(bi_type), " ", to_expression(var_id), |
| 11047 |
" = uint4(extract_bits(0xFFFFFFFF, 0, min(", |
| 11048 |
to_expression(builtin_subgroup_invocation_id_id), |
| 11049 |
" + 1, 32u)), extract_bits(0xFFFFFFFF, 0, (uint)max((int)", |
| 11050 |
to_expression(builtin_subgroup_invocation_id_id), " + 1 - 32, 0)), uint2(0));"); |
| 11051 |
}); |
| 11052 |
break; |
| 11053 |
case BuiltInSubgroupLtMask: |
| 11054 |
if (msl_options.is_ios()) |
| 11055 |
SPIRV_CROSS_THROW("Subgroup ballot functionality is unavailable on iOS."); |
| 11056 |
if (!msl_options.supports_msl_version(2, 1)) |
| 11057 |
SPIRV_CROSS_THROW("Subgroup ballot functionality requires Metal 2.1."); |
| 11058 |
entry_func.fixup_hooks_in.push_back([=]() { |
| 11059 |
statement(builtin_type_decl(bi_type), " ", to_expression(var_id), |
| 11060 |
" = uint4(extract_bits(0xFFFFFFFF, 0, min(", |
| 11061 |
to_expression(builtin_subgroup_invocation_id_id), |
| 11062 |
", 32u)), extract_bits(0xFFFFFFFF, 0, (uint)max((int)", |
| 11063 |
to_expression(builtin_subgroup_invocation_id_id), " - 32, 0)), uint2(0));"); |
| 11064 |
}); |
| 11065 |
break; |
| 11066 |
case BuiltInViewIndex: |
| 11067 |
if (!msl_options.multiview) |
| 11068 |
{ |
| 11069 |
// According to the Vulkan spec, when not running under a multiview |
| 11070 |
// render pass, ViewIndex is 0. |
| 11071 |
entry_func.fixup_hooks_in.push_back([=]() { |
| 11072 |
statement("const ", builtin_type_decl(bi_type), " ", to_expression(var_id), " = 0;"); |
| 11073 |
}); |
| 11074 |
} |
| 11075 |
else if (msl_options.view_index_from_device_index) |
| 11076 |
{ |
| 11077 |
// In this case, we take the view index from that of the device we're running on. |
| 11078 |
entry_func.fixup_hooks_in.push_back([=]() { |
| 11079 |
statement("const ", builtin_type_decl(bi_type), " ", to_expression(var_id), " = ", |
| 11080 |
msl_options.device_index, ";"); |
| 11081 |
}); |
| 11082 |
// We actually don't want to set the render_target_array_index here. |
| 11083 |
// Since every physical device is rendering a different view, |
| 11084 |
// there's no need for layered rendering here. |
| 11085 |
} |
| 11086 |
else if (!msl_options.multiview_layered_rendering) |
| 11087 |
{ |
| 11088 |
// In this case, the views are rendered one at a time. The view index, then, |
| 11089 |
// is just the first part of the "view mask". |
| 11090 |
entry_func.fixup_hooks_in.push_back([=]() { |
| 11091 |
statement("const ", builtin_type_decl(bi_type), " ", to_expression(var_id), " = ", |
| 11092 |
to_expression(view_mask_buffer_id), "[0];"); |
| 11093 |
}); |
| 11094 |
} |
| 11095 |
else if (get_execution_model() == ExecutionModelFragment) |
| 11096 |
{ |
| 11097 |
// Because we adjusted the view index in the vertex shader, we have to |
| 11098 |
// adjust it back here. |
| 11099 |
entry_func.fixup_hooks_in.push_back([=]() { |
| 11100 |
statement(to_expression(var_id), " += ", to_expression(view_mask_buffer_id), "[0];"); |
| 11101 |
}); |
| 11102 |
} |
| 11103 |
else if (get_execution_model() == ExecutionModelVertex) |
| 11104 |
{ |
| 11105 |
// Metal provides no special support for multiview, so we smuggle |
| 11106 |
// the view index in the instance index. |
| 11107 |
entry_func.fixup_hooks_in.push_back([=]() { |
| 11108 |
statement(builtin_type_decl(bi_type), " ", to_expression(var_id), " = ", |
| 11109 |
to_expression(view_mask_buffer_id), "[0] + (", to_expression(builtin_instance_idx_id), |
| 11110 |
" - ", to_expression(builtin_base_instance_id), ") % ", |
| 11111 |
to_expression(view_mask_buffer_id), "[1];"); |
| 11112 |
statement(to_expression(builtin_instance_idx_id), " = (", |
| 11113 |
to_expression(builtin_instance_idx_id), " - ", |
| 11114 |
to_expression(builtin_base_instance_id), ") / ", to_expression(view_mask_buffer_id), |
| 11115 |
"[1] + ", to_expression(builtin_base_instance_id), ";"); |
| 11116 |
}); |
| 11117 |
// In addition to setting the variable itself, we also need to |
| 11118 |
// set the render_target_array_index with it on output. We have to |
| 11119 |
// offset this by the base view index, because Metal isn't in on |
| 11120 |
// our little game here. |
| 11121 |
entry_func.fixup_hooks_out.push_back([=]() { |
| 11122 |
statement(to_expression(builtin_layer_id), " = ", to_expression(var_id), " - ", |
| 11123 |
to_expression(view_mask_buffer_id), "[0];"); |
| 11124 |
}); |
| 11125 |
} |
| 11126 |
break; |
| 11127 |
case BuiltInDeviceIndex: |
| 11128 |
// Metal pipelines belong to the devices which create them, so we'll |
| 11129 |
// need to create a MTLPipelineState for every MTLDevice in a grouped |
| 11130 |
// VkDevice. We can assume, then, that the device index is constant. |
| 11131 |
entry_func.fixup_hooks_in.push_back([=]() { |
| 11132 |
statement("const ", builtin_type_decl(bi_type), " ", to_expression(var_id), " = ", |
| 11133 |
msl_options.device_index, ";"); |
| 11134 |
}); |
| 11135 |
break; |
| 11136 |
case BuiltInWorkgroupId: |
| 11137 |
if (!msl_options.dispatch_base || !active_input_builtins.get(BuiltInWorkgroupId)) |
| 11138 |
break; |
| 11139 |
|
| 11140 |
// The vkCmdDispatchBase() command lets the client set the base value |
| 11141 |
// of WorkgroupId. Metal has no direct equivalent; we must make this |
| 11142 |
// adjustment ourselves. |
| 11143 |
entry_func.fixup_hooks_in.push_back([=]() { |
| 11144 |
statement(to_expression(var_id), " += ", to_dereferenced_expression(builtin_dispatch_base_id), ";"); |
| 11145 |
}); |
| 11146 |
break; |
| 11147 |
case BuiltInGlobalInvocationId: |
| 11148 |
if (!msl_options.dispatch_base || !active_input_builtins.get(BuiltInGlobalInvocationId)) |
| 11149 |
break; |
| 11150 |
|
| 11151 |
// GlobalInvocationId is defined as LocalInvocationId + WorkgroupId * WorkgroupSize. |
| 11152 |
// This needs to be adjusted too. |
| 11153 |
entry_func.fixup_hooks_in.push_back([=]() { |
| 11154 |
auto &execution = this->get_entry_point(); |
| 11155 |
uint32_t workgroup_size_id = execution.workgroup_size.constant; |
| 11156 |
if (workgroup_size_id) |
| 11157 |
statement(to_expression(var_id), " += ", to_dereferenced_expression(builtin_dispatch_base_id), |
| 11158 |
" * ", to_expression(workgroup_size_id), ";"); |
| 11159 |
else |
| 11160 |
statement(to_expression(var_id), " += ", to_dereferenced_expression(builtin_dispatch_base_id), |
| 11161 |
" * uint3(", execution.workgroup_size.x, ", ", execution.workgroup_size.y, ", ", |
| 11162 |
execution.workgroup_size.z, ");"); |
| 11163 |
}); |
| 11164 |
break; |
| 11165 |
case BuiltInVertexId: |
| 11166 |
case BuiltInVertexIndex: |
| 11167 |
// This is direct-mapped normally. |
| 11168 |
if (!msl_options.vertex_for_tessellation) |
| 11169 |
break; |
| 11170 |
|
| 11171 |
entry_func.fixup_hooks_in.push_back([=]() { |
| 11172 |
builtin_declaration = true; |
| 11173 |
switch (msl_options.vertex_index_type) |
| 11174 |
{ |
| 11175 |
case Options::IndexType::None: |
| 11176 |
statement(builtin_type_decl(bi_type), " ", to_expression(var_id), " = ", |
| 11177 |
to_expression(builtin_invocation_id_id), ".x + ", |
| 11178 |
to_expression(builtin_dispatch_base_id), ".x;"); |
| 11179 |
break; |
| 11180 |
case Options::IndexType::UInt16: |
| 11181 |
case Options::IndexType::UInt32: |
| 11182 |
statement(builtin_type_decl(bi_type), " ", to_expression(var_id), " = ", index_buffer_var_name, |
| 11183 |
"[", to_expression(builtin_invocation_id_id), ".x] + ", |
| 11184 |
to_expression(builtin_dispatch_base_id), ".x;"); |
| 11185 |
break; |
| 11186 |
} |
| 11187 |
builtin_declaration = false; |
| 11188 |
}); |
| 11189 |
break; |
| 11190 |
case BuiltInBaseVertex: |
| 11191 |
// This is direct-mapped normally. |
| 11192 |
if (!msl_options.vertex_for_tessellation) |
| 11193 |
break; |
| 11194 |
|
| 11195 |
entry_func.fixup_hooks_in.push_back([=]() { |
| 11196 |
statement(builtin_type_decl(bi_type), " ", to_expression(var_id), " = ", |
| 11197 |
to_expression(builtin_dispatch_base_id), ".x;"); |
| 11198 |
}); |
| 11199 |
break; |
| 11200 |
case BuiltInInstanceId: |
| 11201 |
case BuiltInInstanceIndex: |
| 11202 |
// This is direct-mapped normally. |
| 11203 |
if (!msl_options.vertex_for_tessellation) |
| 11204 |
break; |
| 11205 |
|
| 11206 |
entry_func.fixup_hooks_in.push_back([=]() { |
| 11207 |
builtin_declaration = true; |
| 11208 |
statement(builtin_type_decl(bi_type), " ", to_expression(var_id), " = ", |
| 11209 |
to_expression(builtin_invocation_id_id), ".y + ", to_expression(builtin_dispatch_base_id), |
| 11210 |
".y;"); |
| 11211 |
builtin_declaration = false; |
| 11212 |
}); |
| 11213 |
break; |
| 11214 |
case BuiltInBaseInstance: |
| 11215 |
// This is direct-mapped normally. |
| 11216 |
if (!msl_options.vertex_for_tessellation) |
| 11217 |
break; |
| 11218 |
|
| 11219 |
entry_func.fixup_hooks_in.push_back([=]() { |
| 11220 |
statement(builtin_type_decl(bi_type), " ", to_expression(var_id), " = ", |
| 11221 |
to_expression(builtin_dispatch_base_id), ".y;"); |
| 11222 |
}); |
| 11223 |
break; |
| 11224 |
default: |
| 11225 |
break; |
| 11226 |
} |
| 11227 |
} |
| 11228 |
else if (var.storage == StorageClassOutput && is_builtin_variable(var)) |
| 11229 |
{ |
| 11230 |
if (bi_type == BuiltInSampleMask && get_execution_model() == ExecutionModelFragment && |
| 11231 |
msl_options.additional_fixed_sample_mask != 0xffffffff) |
| 11232 |
{ |
| 11233 |
// If the additional fixed sample mask was set, we need to adjust the sample_mask |
| 11234 |
// output to reflect that. If the shader outputs the sample_mask itself too, we need |
| 11235 |
// to AND the two masks to get the final one. |
| 11236 |
if (does_shader_write_sample_mask) |
| 11237 |
{ |
| 11238 |
entry_func.fixup_hooks_out.push_back([=]() { |
| 11239 |
statement(to_expression(builtin_sample_mask_id), |
| 11240 |
" &= ", msl_options.additional_fixed_sample_mask, ";"); |
| 11241 |
}); |
| 11242 |
} |
| 11243 |
else |
| 11244 |
{ |
| 11245 |
entry_func.fixup_hooks_out.push_back([=]() { |
| 11246 |
statement(to_expression(builtin_sample_mask_id), " = ", |
| 11247 |
msl_options.additional_fixed_sample_mask, ";"); |
| 11248 |
}); |
| 11249 |
} |
| 11250 |
} |
| 11251 |
} |
| 11252 |
}); |
| 11253 |
} |
| 11254 |
|
| 11255 |
// Returns the Metal index of the resource of the specified type as used by the specified variable. |
| 11256 |
uint32_t CompilerMSL::get_metal_resource_index(SPIRVariable &var, SPIRType::BaseType basetype, uint32_t plane) |
| 11257 |
{ |
| 11258 |
auto &execution = get_entry_point(); |
| 11259 |
auto &var_dec = ir.meta[var.self].decoration; |
| 11260 |
auto &var_type = get<SPIRType>(var.basetype); |
| 11261 |
uint32_t var_desc_set = (var.storage == StorageClassPushConstant) ? kPushConstDescSet : var_dec.set; |
| 11262 |
uint32_t var_binding = (var.storage == StorageClassPushConstant) ? kPushConstBinding : var_dec.binding; |
| 11263 |
|
| 11264 |
// If a matching binding has been specified, find and use it. |
| 11265 |
auto itr = resource_bindings.find({ execution.model, var_desc_set, var_binding }); |
| 11266 |
|
| 11267 |
// Atomic helper buffers for image atomics need to use secondary bindings as well. |
| 11268 |
bool use_secondary_binding = (var_type.basetype == SPIRType::SampledImage && basetype == SPIRType::Sampler) || |
| 11269 |
basetype == SPIRType::AtomicCounter; |
| 11270 |
|
| 11271 |
auto resource_decoration = |
| 11272 |
use_secondary_binding ? SPIRVCrossDecorationResourceIndexSecondary : SPIRVCrossDecorationResourceIndexPrimary; |
| 11273 |
|
| 11274 |
if (plane == 1) |
| 11275 |
resource_decoration = SPIRVCrossDecorationResourceIndexTertiary; |
| 11276 |
if (plane == 2) |
| 11277 |
resource_decoration = SPIRVCrossDecorationResourceIndexQuaternary; |
| 11278 |
|
| 11279 |
if (itr != end(resource_bindings)) |
| 11280 |
{ |
| 11281 |
auto &remap = itr->second; |
| 11282 |
remap.second = true; |
| 11283 |
switch (basetype) |
| 11284 |
{ |
| 11285 |
case SPIRType::Image: |
| 11286 |
set_extended_decoration(var.self, resource_decoration, remap.first.msl_texture + plane); |
| 11287 |
return remap.first.msl_texture + plane; |
| 11288 |
case SPIRType::Sampler: |
| 11289 |
set_extended_decoration(var.self, resource_decoration, remap.first.msl_sampler); |
| 11290 |
return remap.first.msl_sampler; |
| 11291 |
default: |
| 11292 |
set_extended_decoration(var.self, resource_decoration, remap.first.msl_buffer); |
| 11293 |
return remap.first.msl_buffer; |
| 11294 |
} |
| 11295 |
} |
| 11296 |
|
| 11297 |
// If we have already allocated an index, keep using it. |
| 11298 |
if (has_extended_decoration(var.self, resource_decoration)) |
| 11299 |
return get_extended_decoration(var.self, resource_decoration); |
| 11300 |
|
| 11301 |
// Allow user to enable decoration binding |
| 11302 |
if (msl_options.enable_decoration_binding) |
| 11303 |
{ |
| 11304 |
// If there is no explicit mapping of bindings to MSL, use the declared binding. |
| 11305 |
if (has_decoration(var.self, DecorationBinding)) |
| 11306 |
{ |
| 11307 |
var_binding = get_decoration(var.self, DecorationBinding); |
| 11308 |
// Avoid emitting sentinel bindings. |
| 11309 |
if (var_binding < 0x80000000u) |
| 11310 |
return var_binding; |
| 11311 |
} |
| 11312 |
} |
| 11313 |
|
| 11314 |
// If we did not explicitly remap, allocate bindings on demand. |
| 11315 |
// We cannot reliably use Binding decorations since SPIR-V and MSL's binding models are very different. |
| 11316 |
|
| 11317 |
bool allocate_argument_buffer_ids = false; |
| 11318 |
|
| 11319 |
if (var.storage != StorageClassPushConstant) |
| 11320 |
allocate_argument_buffer_ids = descriptor_set_is_argument_buffer(var_desc_set); |
| 11321 |
|
| 11322 |
uint32_t binding_stride = 1; |
| 11323 |
auto &type = get<SPIRType>(var.basetype); |
| 11324 |
for (uint32_t i = 0; i < uint32_t(type.array.size()); i++) |
| 11325 |
binding_stride *= to_array_size_literal(type, i); |
| 11326 |
|
| 11327 |
assert(binding_stride != 0); |
| 11328 |
|
| 11329 |
// If a binding has not been specified, revert to incrementing resource indices. |
| 11330 |
uint32_t resource_index; |
| 11331 |
|
| 11332 |
if (type_is_msl_framebuffer_fetch(type)) |
| 11333 |
{ |
| 11334 |
// Frame-buffer fetch gets its fallback resource index from the input attachment index, |
| 11335 |
// which is then treated as color index. |
| 11336 |
resource_index = get_decoration(var.self, DecorationInputAttachmentIndex); |
| 11337 |
} |
| 11338 |
else if (allocate_argument_buffer_ids) |
| 11339 |
{ |
| 11340 |
// Allocate from a flat ID binding space. |
| 11341 |
resource_index = next_metal_resource_ids[var_desc_set]; |
| 11342 |
next_metal_resource_ids[var_desc_set] += binding_stride; |
| 11343 |
} |
| 11344 |
else |
| 11345 |
{ |
| 11346 |
// Allocate from plain bindings which are allocated per resource type. |
| 11347 |
switch (basetype) |
| 11348 |
{ |
| 11349 |
case SPIRType::Image: |
| 11350 |
resource_index = next_metal_resource_index_texture; |
| 11351 |
next_metal_resource_index_texture += binding_stride; |
| 11352 |
break; |
| 11353 |
case SPIRType::Sampler: |
| 11354 |
resource_index = next_metal_resource_index_sampler; |
| 11355 |
next_metal_resource_index_sampler += binding_stride; |
| 11356 |
break; |
| 11357 |
default: |
| 11358 |
resource_index = next_metal_resource_index_buffer; |
| 11359 |
next_metal_resource_index_buffer += binding_stride; |
| 11360 |
break; |
| 11361 |
} |
| 11362 |
} |
| 11363 |
|
| 11364 |
set_extended_decoration(var.self, resource_decoration, resource_index); |
| 11365 |
return resource_index; |
| 11366 |
} |
| 11367 |
|
| 11368 |
bool CompilerMSL::type_is_msl_framebuffer_fetch(const SPIRType &type) const |
| 11369 |
{ |
| 11370 |
return type.basetype == SPIRType::Image && type.image.dim == DimSubpassData && |
| 11371 |
msl_options.use_framebuffer_fetch_subpasses; |
| 11372 |
} |
| 11373 |
|
| 11374 |
string CompilerMSL::argument_decl(const SPIRFunction::Parameter &arg) |
| 11375 |
{ |
| 11376 |
auto &var = get<SPIRVariable>(arg.id); |
| 11377 |
auto &type = get_variable_data_type(var); |
| 11378 |
auto &var_type = get<SPIRType>(arg.type); |
| 11379 |
StorageClass storage = var_type.storage; |
| 11380 |
bool is_pointer = var_type.pointer; |
| 11381 |
|
| 11382 |
// If we need to modify the name of the variable, make sure we use the original variable. |
| 11383 |
// Our alias is just a shadow variable. |
| 11384 |
uint32_t name_id = var.self; |
| 11385 |
if (arg.alias_global_variable && var.basevariable) |
| 11386 |
name_id = var.basevariable; |
| 11387 |
|
| 11388 |
bool constref = !arg.alias_global_variable && is_pointer && arg.write_count == 0; |
| 11389 |
// Framebuffer fetch is plain value, const looks out of place, but it is not wrong. |
| 11390 |
if (type_is_msl_framebuffer_fetch(type)) |
| 11391 |
constref = false; |
| 11392 |
|
| 11393 |
bool type_is_image = type.basetype == SPIRType::Image || type.basetype == SPIRType::SampledImage || |
| 11394 |
type.basetype == SPIRType::Sampler; |
| 11395 |
|
| 11396 |
// Arrays of images/samplers in MSL are always const. |
| 11397 |
if (!type.array.empty() && type_is_image) |
| 11398 |
constref = true; |
| 11399 |
|
| 11400 |
string decl; |
| 11401 |
if (constref) |
| 11402 |
decl += "const "; |
| 11403 |
|
| 11404 |
// If this is a combined image-sampler for a 2D image with floating-point type, |
| 11405 |
// we emitted the 'spvDynamicImageSampler' type, and this is *not* an alias parameter |
| 11406 |
// for a global, then we need to emit a "dynamic" combined image-sampler. |
| 11407 |
// Unfortunately, this is necessary to properly support passing around |
| 11408 |
// combined image-samplers with Y'CbCr conversions on them. |
| 11409 |
bool is_dynamic_img_sampler = !arg.alias_global_variable && type.basetype == SPIRType::SampledImage && |
| 11410 |
type.image.dim == Dim2D && type_is_floating_point(get<SPIRType>(type.image.type)) && |
| 11411 |
spv_function_implementations.count(SPVFuncImplDynamicImageSampler); |
| 11412 |
|
| 11413 |
// Allow Metal to use the array<T> template to make arrays a value type |
| 11414 |
string address_space = get_argument_address_space(var); |
| 11415 |
bool builtin = is_builtin_variable(var); |
| 11416 |
is_using_builtin_array = builtin; |
| 11417 |
if (address_space == "threadgroup") |
| 11418 |
is_using_builtin_array = true; |
| 11419 |
|
| 11420 |
if (var.basevariable && (var.basevariable == stage_in_ptr_var_id || var.basevariable == stage_out_ptr_var_id)) |
| 11421 |
decl += type_to_glsl(type, arg.id); |
| 11422 |
else if (builtin) |
| 11423 |
decl += builtin_type_decl(static_cast<BuiltIn>(get_decoration(arg.id, DecorationBuiltIn)), arg.id); |
| 11424 |
else if ((storage == StorageClassUniform || storage == StorageClassStorageBuffer) && is_array(type)) |
| 11425 |
{ |
| 11426 |
is_using_builtin_array = true; |
| 11427 |
decl += join(type_to_glsl(type, arg.id), "*"); |
| 11428 |
} |
| 11429 |
else if (is_dynamic_img_sampler) |
| 11430 |
{ |
| 11431 |
decl += join("spvDynamicImageSampler<", type_to_glsl(get<SPIRType>(type.image.type)), ">"); |
| 11432 |
// Mark the variable so that we can handle passing it to another function. |
| 11433 |
set_extended_decoration(arg.id, SPIRVCrossDecorationDynamicImageSampler); |
| 11434 |
} |
| 11435 |
else |
| 11436 |
decl += type_to_glsl(type, arg.id); |
| 11437 |
|
| 11438 |
bool opaque_handle = storage == StorageClassUniformConstant; |
| 11439 |
|
| 11440 |
if (!builtin && !opaque_handle && !is_pointer && |
| 11441 |
(storage == StorageClassFunction || storage == StorageClassGeneric)) |
| 11442 |
{ |
| 11443 |
// If the argument is a pure value and not an opaque type, we will pass by value. |
| 11444 |
if (msl_options.force_native_arrays && is_array(type)) |
| 11445 |
{ |
| 11446 |
// We are receiving an array by value. This is problematic. |
| 11447 |
// We cannot be sure of the target address space since we are supposed to receive a copy, |
| 11448 |
// but this is not possible with MSL without some extra work. |
| 11449 |
// We will have to assume we're getting a reference in thread address space. |
| 11450 |
// If we happen to get a reference in constant address space, the caller must emit a copy and pass that. |
| 11451 |
// Thread const therefore becomes the only logical choice, since we cannot "create" a constant array from |
| 11452 |
// non-constant arrays, but we can create thread const from constant. |
| 11453 |
decl = string("thread const ") + decl; |
| 11454 |
decl += " (&"; |
| 11455 |
const char *restrict_kw = to_restrict(name_id); |
| 11456 |
if (*restrict_kw) |
| 11457 |
{ |
| 11458 |
decl += " "; |
| 11459 |
decl += restrict_kw; |
| 11460 |
} |
| 11461 |
decl += to_expression(name_id); |
| 11462 |
decl += ")"; |
| 11463 |
decl += type_to_array_glsl(type); |
| 11464 |
} |
| 11465 |
else |
| 11466 |
{ |
| 11467 |
if (!address_space.empty()) |
| 11468 |
decl = join(address_space, " ", decl); |
| 11469 |
decl += " "; |
| 11470 |
decl += to_expression(name_id); |
| 11471 |
} |
| 11472 |
} |
| 11473 |
else if (is_array(type) && !type_is_image) |
| 11474 |
{ |
| 11475 |
// Arrays of images and samplers are special cased. |
| 11476 |
if (!address_space.empty()) |
| 11477 |
decl = join(address_space, " ", decl); |
| 11478 |
|
| 11479 |
if (msl_options.argument_buffers) |
| 11480 |
{ |
| 11481 |
uint32_t desc_set = get_decoration(name_id, DecorationDescriptorSet); |
| 11482 |
if ((storage == StorageClassUniform || storage == StorageClassStorageBuffer) && |
| 11483 |
descriptor_set_is_argument_buffer(desc_set)) |
| 11484 |
{ |
| 11485 |
// An awkward case where we need to emit *more* address space declarations (yay!). |
| 11486 |
// An example is where we pass down an array of buffer pointers to leaf functions. |
| 11487 |
// It's a constant array containing pointers to constants. |
| 11488 |
// The pointer array is always constant however. E.g. |
| 11489 |
// device SSBO * constant (&array)[N]. |
| 11490 |
// const device SSBO * constant (&array)[N]. |
| 11491 |
// constant SSBO * constant (&array)[N]. |
| 11492 |
// However, this only matters for argument buffers, since for MSL 1.0 style codegen, |
| 11493 |
// we emit the buffer array on stack instead, and that seems to work just fine apparently. |
| 11494 |
|
| 11495 |
// If the argument was marked as being in device address space, any pointer to member would |
| 11496 |
// be const device, not constant. |
| 11497 |
if (argument_buffer_device_storage_mask & (1u << desc_set)) |
| 11498 |
decl += " const device"; |
| 11499 |
else |
| 11500 |
decl += " constant"; |
| 11501 |
} |
| 11502 |
} |
| 11503 |
|
| 11504 |
decl += " (&"; |
| 11505 |
const char *restrict_kw = to_restrict(name_id); |
| 11506 |
if (*restrict_kw) |
| 11507 |
{ |
| 11508 |
decl += " "; |
| 11509 |
decl += restrict_kw; |
| 11510 |
} |
| 11511 |
decl += to_expression(name_id); |
| 11512 |
decl += ")"; |
| 11513 |
decl += type_to_array_glsl(type); |
| 11514 |
} |
| 11515 |
else if (!opaque_handle && (!pull_model_inputs.count(var.basevariable) || type.basetype == SPIRType::Struct)) |
| 11516 |
{ |
| 11517 |
// If this is going to be a reference to a variable pointer, the address space |
| 11518 |
// for the reference has to go before the '&', but after the '*'. |
| 11519 |
if (!address_space.empty()) |
| 11520 |
{ |
| 11521 |
if (decl.back() == '*') |
| 11522 |
decl += join(" ", address_space, " "); |
| 11523 |
else |
| 11524 |
decl = join(address_space, " ", decl); |
| 11525 |
} |
| 11526 |
decl += "&"; |
| 11527 |
decl += " "; |
| 11528 |
decl += to_restrict(name_id); |
| 11529 |
decl += to_expression(name_id); |
| 11530 |
} |
| 11531 |
else |
| 11532 |
{ |
| 11533 |
if (!address_space.empty()) |
| 11534 |
decl = join(address_space, " ", decl); |
| 11535 |
decl += " "; |
| 11536 |
decl += to_expression(name_id); |
| 11537 |
} |
| 11538 |
|
| 11539 |
// Emulate texture2D atomic operations |
| 11540 |
auto *backing_var = maybe_get_backing_variable(name_id); |
| 11541 |
if (backing_var && atomic_image_vars.count(backing_var->self)) |
| 11542 |
{ |
| 11543 |
decl += ", device atomic_" + type_to_glsl(get<SPIRType>(var_type.image.type), 0); |
| 11544 |
decl += "* " + to_expression(name_id) + "_atomic"; |
| 11545 |
} |
| 11546 |
|
| 11547 |
is_using_builtin_array = false; |
| 11548 |
|
| 11549 |
return decl; |
| 11550 |
} |
| 11551 |
|
| 11552 |
// If we're currently in the entry point function, and the object |
| 11553 |
// has a qualified name, use it, otherwise use the standard name. |
| 11554 |
string CompilerMSL::to_name(uint32_t id, bool allow_alias) const |
| 11555 |
{ |
| 11556 |
if (current_function && (current_function->self == ir.default_entry_point)) |
| 11557 |
{ |
| 11558 |
auto *m = ir.find_meta(id); |
| 11559 |
if (m && !m->decoration.qualified_alias.empty()) |
| 11560 |
return m->decoration.qualified_alias; |
| 11561 |
} |
| 11562 |
return Compiler::to_name(id, allow_alias); |
| 11563 |
} |
| 11564 |
|
| 11565 |
// Returns a name that combines the name of the struct with the name of the member, except for Builtins |
| 11566 |
string CompilerMSL::to_qualified_member_name(const SPIRType &type, uint32_t index) |
| 11567 |
{ |
| 11568 |
// Don't qualify Builtin names because they are unique and are treated as such when building expressions |
| 11569 |
BuiltIn builtin = BuiltInMax; |
| 11570 |
if (is_member_builtin(type, index, &builtin)) |
| 11571 |
return builtin_to_glsl(builtin, type.storage); |
| 11572 |
|
| 11573 |
// Strip any underscore prefix from member name |
| 11574 |
string mbr_name = to_member_name(type, index); |
| 11575 |
size_t startPos = mbr_name.find_first_not_of("_"); |
| 11576 |
mbr_name = (startPos != string::npos) ? mbr_name.substr(startPos) : ""; |
| 11577 |
return join(to_name(type.self), "_", mbr_name); |
| 11578 |
} |
| 11579 |
|
| 11580 |
// Ensures that the specified name is permanently usable by prepending a prefix |
| 11581 |
// if the first chars are _ and a digit, which indicate a transient name. |
| 11582 |
string CompilerMSL::ensure_valid_name(string name, string pfx) |
| 11583 |
{ |
| 11584 |
return (name.size() >= 2 && name[0] == '_' && isdigit(name[1])) ? (pfx + name) : name; |
| 11585 |
} |
| 11586 |
|
| 11587 |
// Replace all names that match MSL keywords or Metal Standard Library functions. |
| 11588 |
void CompilerMSL::replace_illegal_names() |
| 11589 |
{ |
| 11590 |
// FIXME: MSL and GLSL are doing two different things here. |
| 11591 |
// Agree on convention and remove this override. |
| 11592 |
static const unordered_set<string> keywords = { |
| 11593 |
"kernel", |
| 11594 |
"vertex", |
| 11595 |
"fragment", |
| 11596 |
"compute", |
| 11597 |
"bias", |
| 11598 |
"level", |
| 11599 |
"gradient2d", |
| 11600 |
"gradientcube", |
| 11601 |
"gradient3d", |
| 11602 |
"min_lod_clamp", |
| 11603 |
"assert", |
| 11604 |
"VARIABLE_TRACEPOINT", |
| 11605 |
"STATIC_DATA_TRACEPOINT", |
| 11606 |
"STATIC_DATA_TRACEPOINT_V", |
| 11607 |
"METAL_ALIGN", |
| 11608 |
"METAL_ASM", |
| 11609 |
"METAL_CONST", |
| 11610 |
"METAL_DEPRECATED", |
| 11611 |
"METAL_ENABLE_IF", |
| 11612 |
"METAL_FUNC", |
| 11613 |
"METAL_INTERNAL", |
| 11614 |
"METAL_NON_NULL_RETURN", |
| 11615 |
"METAL_NORETURN", |
| 11616 |
"METAL_NOTHROW", |
| 11617 |
"METAL_PURE", |
| 11618 |
"METAL_UNAVAILABLE", |
| 11619 |
"METAL_IMPLICIT", |
| 11620 |
"METAL_EXPLICIT", |
| 11621 |
"METAL_CONST_ARG", |
| 11622 |
"METAL_ARG_UNIFORM", |
| 11623 |
"METAL_ZERO_ARG", |
| 11624 |
"METAL_VALID_LOD_ARG", |
| 11625 |
"METAL_VALID_LEVEL_ARG", |
| 11626 |
"METAL_VALID_STORE_ORDER", |
| 11627 |
"METAL_VALID_LOAD_ORDER", |
| 11628 |
"METAL_VALID_COMPARE_EXCHANGE_FAILURE_ORDER", |
| 11629 |
"METAL_COMPATIBLE_COMPARE_EXCHANGE_ORDERS", |
| 11630 |
"METAL_VALID_RENDER_TARGET", |
| 11631 |
"is_function_constant_defined", |
| 11632 |
"CHAR_BIT", |
| 11633 |
"SCHAR_MAX", |
| 11634 |
"SCHAR_MIN", |
| 11635 |
"UCHAR_MAX", |
| 11636 |
"CHAR_MAX", |
| 11637 |
"CHAR_MIN", |
| 11638 |
"USHRT_MAX", |
| 11639 |
"SHRT_MAX", |
| 11640 |
"SHRT_MIN", |
| 11641 |
"UINT_MAX", |
| 11642 |
"INT_MAX", |
| 11643 |
"INT_MIN", |
| 11644 |
"FLT_DIG", |
| 11645 |
"FLT_MANT_DIG", |
| 11646 |
"FLT_MAX_10_EXP", |
| 11647 |
"FLT_MAX_EXP", |
| 11648 |
"FLT_MIN_10_EXP", |
| 11649 |
"FLT_MIN_EXP", |
| 11650 |
"FLT_RADIX", |
| 11651 |
"FLT_MAX", |
| 11652 |
"FLT_MIN", |
| 11653 |
"FLT_EPSILON", |
| 11654 |
"FP_ILOGB0", |
| 11655 |
"FP_ILOGBNAN", |
| 11656 |
"MAXFLOAT", |
| 11657 |
"HUGE_VALF", |
| 11658 |
"INFINITY", |
| 11659 |
"NAN", |
| 11660 |
"M_E_F", |
| 11661 |
"M_LOG2E_F", |
| 11662 |
"M_LOG10E_F", |
| 11663 |
"M_LN2_F", |
| 11664 |
"M_LN10_F", |
| 11665 |
"M_PI_F", |
| 11666 |
"M_PI_2_F", |
| 11667 |
"M_PI_4_F", |
| 11668 |
"M_1_PI_F", |
| 11669 |
"M_2_PI_F", |
| 11670 |
"M_2_SQRTPI_F", |
| 11671 |
"M_SQRT2_F", |
| 11672 |
"M_SQRT1_2_F", |
| 11673 |
"HALF_DIG", |
| 11674 |
"HALF_MANT_DIG", |
| 11675 |
"HALF_MAX_10_EXP", |
| 11676 |
"HALF_MAX_EXP", |
| 11677 |
"HALF_MIN_10_EXP", |
| 11678 |
"HALF_MIN_EXP", |
| 11679 |
"HALF_RADIX", |
| 11680 |
"HALF_MAX", |
| 11681 |
"HALF_MIN", |
| 11682 |
"HALF_EPSILON", |
| 11683 |
"MAXHALF", |
| 11684 |
"HUGE_VALH", |
| 11685 |
"M_E_H", |
| 11686 |
"M_LOG2E_H", |
| 11687 |
"M_LOG10E_H", |
| 11688 |
"M_LN2_H", |
| 11689 |
"M_LN10_H", |
| 11690 |
"M_PI_H", |
| 11691 |
"M_PI_2_H", |
| 11692 |
"M_PI_4_H", |
| 11693 |
"M_1_PI_H", |
| 11694 |
"M_2_PI_H", |
| 11695 |
"M_2_SQRTPI_H", |
| 11696 |
"M_SQRT2_H", |
| 11697 |
"M_SQRT1_2_H", |
| 11698 |
"DBL_DIG", |
| 11699 |
"DBL_MANT_DIG", |
| 11700 |
"DBL_MAX_10_EXP", |
| 11701 |
"DBL_MAX_EXP", |
| 11702 |
"DBL_MIN_10_EXP", |
| 11703 |
"DBL_MIN_EXP", |
| 11704 |
"DBL_RADIX", |
| 11705 |
"DBL_MAX", |
| 11706 |
"DBL_MIN", |
| 11707 |
"DBL_EPSILON", |
| 11708 |
"HUGE_VAL", |
| 11709 |
"M_E", |
| 11710 |
"M_LOG2E", |
| 11711 |
"M_LOG10E", |
| 11712 |
"M_LN2", |
| 11713 |
"M_LN10", |
| 11714 |
"M_PI", |
| 11715 |
"M_PI_2", |
| 11716 |
"M_PI_4", |
| 11717 |
"M_1_PI", |
| 11718 |
"M_2_PI", |
| 11719 |
"M_2_SQRTPI", |
| 11720 |
"M_SQRT2", |
| 11721 |
"M_SQRT1_2", |
| 11722 |
"quad_broadcast", |
| 11723 |
}; |
| 11724 |
|
| 11725 |
static const unordered_set<string> illegal_func_names = { |
| 11726 |
"main", |
| 11727 |
"saturate", |
| 11728 |
"assert", |
| 11729 |
"VARIABLE_TRACEPOINT", |
| 11730 |
"STATIC_DATA_TRACEPOINT", |
| 11731 |
"STATIC_DATA_TRACEPOINT_V", |
| 11732 |
"METAL_ALIGN", |
| 11733 |
"METAL_ASM", |
| 11734 |
"METAL_CONST", |
| 11735 |
"METAL_DEPRECATED", |
| 11736 |
"METAL_ENABLE_IF", |
| 11737 |
"METAL_FUNC", |
| 11738 |
"METAL_INTERNAL", |
| 11739 |
"METAL_NON_NULL_RETURN", |
| 11740 |
"METAL_NORETURN", |
| 11741 |
"METAL_NOTHROW", |
| 11742 |
"METAL_PURE", |
| 11743 |
"METAL_UNAVAILABLE", |
| 11744 |
"METAL_IMPLICIT", |
| 11745 |
"METAL_EXPLICIT", |
| 11746 |
"METAL_CONST_ARG", |
| 11747 |
"METAL_ARG_UNIFORM", |
| 11748 |
"METAL_ZERO_ARG", |
| 11749 |
"METAL_VALID_LOD_ARG", |
| 11750 |
"METAL_VALID_LEVEL_ARG", |
| 11751 |
"METAL_VALID_STORE_ORDER", |
| 11752 |
"METAL_VALID_LOAD_ORDER", |
| 11753 |
"METAL_VALID_COMPARE_EXCHANGE_FAILURE_ORDER", |
| 11754 |
"METAL_COMPATIBLE_COMPARE_EXCHANGE_ORDERS", |
| 11755 |
"METAL_VALID_RENDER_TARGET", |
| 11756 |
"is_function_constant_defined", |
| 11757 |
"CHAR_BIT", |
| 11758 |
"SCHAR_MAX", |
| 11759 |
"SCHAR_MIN", |
| 11760 |
"UCHAR_MAX", |
| 11761 |
"CHAR_MAX", |
| 11762 |
"CHAR_MIN", |
| 11763 |
"USHRT_MAX", |
| 11764 |
"SHRT_MAX", |
| 11765 |
"SHRT_MIN", |
| 11766 |
"UINT_MAX", |
| 11767 |
"INT_MAX", |
| 11768 |
"INT_MIN", |
| 11769 |
"FLT_DIG", |
| 11770 |
"FLT_MANT_DIG", |
| 11771 |
"FLT_MAX_10_EXP", |
| 11772 |
"FLT_MAX_EXP", |
| 11773 |
"FLT_MIN_10_EXP", |
| 11774 |
"FLT_MIN_EXP", |
| 11775 |
"FLT_RADIX", |
| 11776 |
"FLT_MAX", |
| 11777 |
"FLT_MIN", |
| 11778 |
"FLT_EPSILON", |
| 11779 |
"FP_ILOGB0", |
| 11780 |
"FP_ILOGBNAN", |
| 11781 |
"MAXFLOAT", |
| 11782 |
"HUGE_VALF", |
| 11783 |
"INFINITY", |
| 11784 |
"NAN", |
| 11785 |
"M_E_F", |
| 11786 |
"M_LOG2E_F", |
| 11787 |
"M_LOG10E_F", |
| 11788 |
"M_LN2_F", |
| 11789 |
"M_LN10_F", |
| 11790 |
"M_PI_F", |
| 11791 |
"M_PI_2_F", |
| 11792 |
"M_PI_4_F", |
| 11793 |
"M_1_PI_F", |
| 11794 |
"M_2_PI_F", |
| 11795 |
"M_2_SQRTPI_F", |
| 11796 |
"M_SQRT2_F", |
| 11797 |
"M_SQRT1_2_F", |
| 11798 |
"HALF_DIG", |
| 11799 |
"HALF_MANT_DIG", |
| 11800 |
"HALF_MAX_10_EXP", |
| 11801 |
"HALF_MAX_EXP", |
| 11802 |
"HALF_MIN_10_EXP", |
| 11803 |
"HALF_MIN_EXP", |
| 11804 |
"HALF_RADIX", |
| 11805 |
"HALF_MAX", |
| 11806 |
"HALF_MIN", |
| 11807 |
"HALF_EPSILON", |
| 11808 |
"MAXHALF", |
| 11809 |
"HUGE_VALH", |
| 11810 |
"M_E_H", |
| 11811 |
"M_LOG2E_H", |
| 11812 |
"M_LOG10E_H", |
| 11813 |
"M_LN2_H", |
| 11814 |
"M_LN10_H", |
| 11815 |
"M_PI_H", |
| 11816 |
"M_PI_2_H", |
| 11817 |
"M_PI_4_H", |
| 11818 |
"M_1_PI_H", |
| 11819 |
"M_2_PI_H", |
| 11820 |
"M_2_SQRTPI_H", |
| 11821 |
"M_SQRT2_H", |
| 11822 |
"M_SQRT1_2_H", |
| 11823 |
"DBL_DIG", |
| 11824 |
"DBL_MANT_DIG", |
| 11825 |
"DBL_MAX_10_EXP", |
| 11826 |
"DBL_MAX_EXP", |
| 11827 |
"DBL_MIN_10_EXP", |
| 11828 |
"DBL_MIN_EXP", |
| 11829 |
"DBL_RADIX", |
| 11830 |
"DBL_MAX", |
| 11831 |
"DBL_MIN", |
| 11832 |
"DBL_EPSILON", |
| 11833 |
"HUGE_VAL", |
| 11834 |
"M_E", |
| 11835 |
"M_LOG2E", |
| 11836 |
"M_LOG10E", |
| 11837 |
"M_LN2", |
| 11838 |
"M_LN10", |
| 11839 |
"M_PI", |
| 11840 |
"M_PI_2", |
| 11841 |
"M_PI_4", |
| 11842 |
"M_1_PI", |
| 11843 |
"M_2_PI", |
| 11844 |
"M_2_SQRTPI", |
| 11845 |
"M_SQRT2", |
| 11846 |
"M_SQRT1_2", |
| 11847 |
}; |
| 11848 |
|
| 11849 |
ir.for_each_typed_id<SPIRVariable>([&](uint32_t self, SPIRVariable &) { |
| 11850 |
auto *meta = ir.find_meta(self); |
| 11851 |
if (!meta) |
| 11852 |
return; |
| 11853 |
|
| 11854 |
auto &dec = meta->decoration; |
| 11855 |
if (keywords.find(dec.alias) != end(keywords)) |
| 11856 |
dec.alias += "0"; |
| 11857 |
}); |
| 11858 |
|
| 11859 |
ir.for_each_typed_id<SPIRFunction>([&](uint32_t self, SPIRFunction &) { |
| 11860 |
auto *meta = ir.find_meta(self); |
| 11861 |
if (!meta) |
| 11862 |
return; |
| 11863 |
|
| 11864 |
auto &dec = meta->decoration; |
| 11865 |
if (illegal_func_names.find(dec.alias) != end(illegal_func_names)) |
| 11866 |
dec.alias += "0"; |
| 11867 |
}); |
| 11868 |
|
| 11869 |
ir.for_each_typed_id<SPIRType>([&](uint32_t self, SPIRType &) { |
| 11870 |
auto *meta = ir.find_meta(self); |
| 11871 |
if (!meta) |
| 11872 |
return; |
| 11873 |
|
| 11874 |
for (auto &mbr_dec : meta->members) |
| 11875 |
if (keywords.find(mbr_dec.alias) != end(keywords)) |
| 11876 |
mbr_dec.alias += "0"; |
| 11877 |
}); |
| 11878 |
|
| 11879 |
for (auto &entry : ir.entry_points) |
| 11880 |
{ |
| 11881 |
// Change both the entry point name and the alias, to keep them synced. |
| 11882 |
string &ep_name = entry.second.name; |
| 11883 |
if (illegal_func_names.find(ep_name) != end(illegal_func_names)) |
| 11884 |
ep_name += "0"; |
| 11885 |
|
| 11886 |
// Always write this because entry point might have been renamed earlier. |
| 11887 |
ir.meta[entry.first].decoration.alias = ep_name; |
| 11888 |
} |
| 11889 |
|
| 11890 |
CompilerGLSL::replace_illegal_names(); |
| 11891 |
} |
| 11892 |
|
| 11893 |
string CompilerMSL::to_member_reference(uint32_t base, const SPIRType &type, uint32_t index, bool ptr_chain) |
| 11894 |
{ |
| 11895 |
if (index < uint32_t(type.member_type_index_redirection.size())) |
| 11896 |
index = type.member_type_index_redirection[index]; |
| 11897 |
|
| 11898 |
auto *var = maybe_get<SPIRVariable>(base); |
| 11899 |
// If this is a buffer array, we have to dereference the buffer pointers. |
| 11900 |
// Otherwise, if this is a pointer expression, dereference it. |
| 11901 |
|
| 11902 |
bool declared_as_pointer = false; |
| 11903 |
|
| 11904 |
if (var) |
| 11905 |
{ |
| 11906 |
// Only allow -> dereference for block types. This is so we get expressions like |
| 11907 |
// buffer[i]->first_member.second_member, rather than buffer[i]->first->second. |
| 11908 |
bool is_block = has_decoration(type.self, DecorationBlock) || has_decoration(type.self, DecorationBufferBlock); |
| 11909 |
|
| 11910 |
bool is_buffer_variable = |
| 11911 |
is_block && (var->storage == StorageClassUniform || var->storage == StorageClassStorageBuffer); |
| 11912 |
declared_as_pointer = is_buffer_variable && is_array(get<SPIRType>(var->basetype)); |
| 11913 |
} |
| 11914 |
|
| 11915 |
if (declared_as_pointer || (!ptr_chain && should_dereference(base))) |
| 11916 |
return join("->", to_member_name(type, index)); |
| 11917 |
else |
| 11918 |
return join(".", to_member_name(type, index)); |
| 11919 |
} |
| 11920 |
|
| 11921 |
string CompilerMSL::to_qualifiers_glsl(uint32_t id) |
| 11922 |
{ |
| 11923 |
string quals; |
| 11924 |
|
| 11925 |
auto &type = expression_type(id); |
| 11926 |
if (type.storage == StorageClassWorkgroup) |
| 11927 |
quals += "threadgroup "; |
| 11928 |
|
| 11929 |
return quals; |
| 11930 |
} |
| 11931 |
|
| 11932 |
// The optional id parameter indicates the object whose type we are trying |
| 11933 |
// to find the description for. It is optional. Most type descriptions do not |
| 11934 |
// depend on a specific object's use of that type. |
| 11935 |
string CompilerMSL::type_to_glsl(const SPIRType &type, uint32_t id) |
| 11936 |
{ |
| 11937 |
string type_name; |
| 11938 |
|
| 11939 |
// Pointer? |
| 11940 |
if (type.pointer) |
| 11941 |
{ |
| 11942 |
const char *restrict_kw; |
| 11943 |
type_name = join(get_type_address_space(type, id), " ", type_to_glsl(get<SPIRType>(type.parent_type), id)); |
| 11944 |
|
| 11945 |
switch (type.basetype) |
| 11946 |
{ |
| 11947 |
case SPIRType::Image: |
| 11948 |
case SPIRType::SampledImage: |
| 11949 |
case SPIRType::Sampler: |
| 11950 |
// These are handles. |
| 11951 |
break; |
| 11952 |
default: |
| 11953 |
// Anything else can be a raw pointer. |
| 11954 |
type_name += "*"; |
| 11955 |
restrict_kw = to_restrict(id); |
| 11956 |
if (*restrict_kw) |
| 11957 |
{ |
| 11958 |
type_name += " "; |
| 11959 |
type_name += restrict_kw; |
| 11960 |
} |
| 11961 |
break; |
| 11962 |
} |
| 11963 |
return type_name; |
| 11964 |
} |
| 11965 |
|
| 11966 |
switch (type.basetype) |
| 11967 |
{ |
| 11968 |
case SPIRType::Struct: |
| 11969 |
// Need OpName lookup here to get a "sensible" name for a struct. |
| 11970 |
// Allow Metal to use the array<T> template to make arrays a value type |
| 11971 |
type_name = to_name(type.self); |
| 11972 |
break; |
| 11973 |
|
| 11974 |
case SPIRType::Image: |
| 11975 |
case SPIRType::SampledImage: |
| 11976 |
return image_type_glsl(type, id); |
| 11977 |
|
| 11978 |
case SPIRType::Sampler: |
| 11979 |
return sampler_type(type, id); |
| 11980 |
|
| 11981 |
case SPIRType::Void: |
| 11982 |
return "void"; |
| 11983 |
|
| 11984 |
case SPIRType::AtomicCounter: |
| 11985 |
return "atomic_uint"; |
| 11986 |
|
| 11987 |
case SPIRType::ControlPointArray: |
| 11988 |
return join("patch_control_point<", type_to_glsl(get<SPIRType>(type.parent_type), id), ">"); |
| 11989 |
|
| 11990 |
case SPIRType::Interpolant: |
| 11991 |
return join("interpolant<", type_to_glsl(get<SPIRType>(type.parent_type), id), ", interpolation::", |
| 11992 |
has_decoration(type.self, DecorationNoPerspective) ? "no_perspective" : "perspective", ">"); |
| 11993 |
|
| 11994 |
// Scalars |
| 11995 |
case SPIRType::Boolean: |
| 11996 |
type_name = "bool"; |
| 11997 |
break; |
| 11998 |
case SPIRType::Char: |
| 11999 |
case SPIRType::SByte: |
| 12000 |
type_name = "char"; |
| 12001 |
break; |
| 12002 |
case SPIRType::UByte: |
| 12003 |
type_name = "uchar"; |
| 12004 |
break; |
| 12005 |
case SPIRType::Short: |
| 12006 |
type_name = "short"; |
| 12007 |
break; |
| 12008 |
case SPIRType::UShort: |
| 12009 |
type_name = "ushort"; |
| 12010 |
break; |
| 12011 |
case SPIRType::Int: |
| 12012 |
type_name = "int"; |
| 12013 |
break; |
| 12014 |
case SPIRType::UInt: |
| 12015 |
type_name = "uint"; |
| 12016 |
break; |
| 12017 |
case SPIRType::Int64: |
| 12018 |
if (!msl_options.supports_msl_version(2, 2)) |
| 12019 |
SPIRV_CROSS_THROW("64-bit integers are only supported in MSL 2.2 and above."); |
| 12020 |
type_name = "long"; |
| 12021 |
break; |
| 12022 |
case SPIRType::UInt64: |
| 12023 |
if (!msl_options.supports_msl_version(2, 2)) |
| 12024 |
SPIRV_CROSS_THROW("64-bit integers are only supported in MSL 2.2 and above."); |
| 12025 |
type_name = "ulong"; |
| 12026 |
break; |
| 12027 |
case SPIRType::Half: |
| 12028 |
type_name = "half"; |
| 12029 |
break; |
| 12030 |
case SPIRType::Float: |
| 12031 |
type_name = "float"; |
| 12032 |
break; |
| 12033 |
case SPIRType::Double: |
| 12034 |
type_name = "double"; // Currently unsupported |
| 12035 |
break; |
| 12036 |
|
| 12037 |
default: |
| 12038 |
return "unknown_type"; |
| 12039 |
} |
| 12040 |
|
| 12041 |
// Matrix? |
| 12042 |
if (type.columns > 1) |
| 12043 |
type_name += to_string(type.columns) + "x"; |
| 12044 |
|
| 12045 |
// Vector or Matrix? |
| 12046 |
if (type.vecsize > 1) |
| 12047 |
type_name += to_string(type.vecsize); |
| 12048 |
|
| 12049 |
if (type.array.empty() || using_builtin_array()) |
| 12050 |
{ |
| 12051 |
return type_name; |
| 12052 |
} |
| 12053 |
else |
| 12054 |
{ |
| 12055 |
// Allow Metal to use the array<T> template to make arrays a value type |
| 12056 |
add_spv_func_and_recompile(SPVFuncImplUnsafeArray); |
| 12057 |
string res; |
| 12058 |
string sizes; |
| 12059 |
|
| 12060 |
for (uint32_t i = 0; i < uint32_t(type.array.size()); i++) |
| 12061 |
{ |
| 12062 |
res += "spvUnsafeArray<"; |
| 12063 |
sizes += ", "; |
| 12064 |
sizes += to_array_size(type, i); |
| 12065 |
sizes += ">"; |
| 12066 |
} |
| 12067 |
|
| 12068 |
res += type_name + sizes; |
| 12069 |
return res; |
| 12070 |
} |
| 12071 |
} |
| 12072 |
|
| 12073 |
string CompilerMSL::type_to_array_glsl(const SPIRType &type) |
| 12074 |
{ |
| 12075 |
// Allow Metal to use the array<T> template to make arrays a value type |
| 12076 |
switch (type.basetype) |
| 12077 |
{ |
| 12078 |
case SPIRType::AtomicCounter: |
| 12079 |
case SPIRType::ControlPointArray: |
| 12080 |
{ |
| 12081 |
return CompilerGLSL::type_to_array_glsl(type); |
| 12082 |
} |
| 12083 |
default: |
| 12084 |
{ |
| 12085 |
if (using_builtin_array()) |
| 12086 |
return CompilerGLSL::type_to_array_glsl(type); |
| 12087 |
else |
| 12088 |
return ""; |
| 12089 |
} |
| 12090 |
} |
| 12091 |
} |
| 12092 |
|
| 12093 |
// Threadgroup arrays can't have a wrapper type |
| 12094 |
std::string CompilerMSL::variable_decl(const SPIRVariable &variable) |
| 12095 |
{ |
| 12096 |
if (variable.storage == StorageClassWorkgroup) |
| 12097 |
{ |
| 12098 |
is_using_builtin_array = true; |
| 12099 |
} |
| 12100 |
std::string expr = CompilerGLSL::variable_decl(variable); |
| 12101 |
if (variable.storage == StorageClassWorkgroup) |
| 12102 |
{ |
| 12103 |
is_using_builtin_array = false; |
| 12104 |
} |
| 12105 |
return expr; |
| 12106 |
} |
| 12107 |
|
| 12108 |
// GCC workaround of lambdas calling protected funcs |
| 12109 |
std::string CompilerMSL::variable_decl(const SPIRType &type, const std::string &name, uint32_t id) |
| 12110 |
{ |
| 12111 |
return CompilerGLSL::variable_decl(type, name, id); |
| 12112 |
} |
| 12113 |
|
| 12114 |
std::string CompilerMSL::sampler_type(const SPIRType &type, uint32_t id) |
| 12115 |
{ |
| 12116 |
auto *var = maybe_get<SPIRVariable>(id); |
| 12117 |
if (var && var->basevariable) |
| 12118 |
{ |
| 12119 |
// Check against the base variable, and not a fake ID which might have been generated for this variable. |
| 12120 |
id = var->basevariable; |
| 12121 |
} |
| 12122 |
|
| 12123 |
if (!type.array.empty()) |
| 12124 |
{ |
| 12125 |
if (!msl_options.supports_msl_version(2)) |
| 12126 |
SPIRV_CROSS_THROW("MSL 2.0 or greater is required for arrays of samplers."); |
| 12127 |
|
| 12128 |
if (type.array.size() > 1) |
| 12129 |
SPIRV_CROSS_THROW("Arrays of arrays of samplers are not supported in MSL."); |
| 12130 |
|
| 12131 |
// Arrays of samplers in MSL must be declared with a special array<T, N> syntax ala C++11 std::array. |
| 12132 |
// If we have a runtime array, it could be a variable-count descriptor set binding. |
| 12133 |
uint32_t array_size = to_array_size_literal(type); |
| 12134 |
if (array_size == 0) |
| 12135 |
array_size = get_resource_array_size(id); |
| 12136 |
|
| 12137 |
if (array_size == 0) |
| 12138 |
SPIRV_CROSS_THROW("Unsized array of samplers is not supported in MSL."); |
| 12139 |
|
| 12140 |
auto &parent = get<SPIRType>(get_pointee_type(type).parent_type); |
| 12141 |
return join("array<", sampler_type(parent, id), ", ", array_size, ">"); |
| 12142 |
} |
| 12143 |
else |
| 12144 |
return "sampler"; |
| 12145 |
} |
| 12146 |
|
| 12147 |
// Returns an MSL string describing the SPIR-V image type |
| 12148 |
string CompilerMSL::image_type_glsl(const SPIRType &type, uint32_t id) |
| 12149 |
{ |
| 12150 |
auto *var = maybe_get<SPIRVariable>(id); |
| 12151 |
if (var && var->basevariable) |
| 12152 |
{ |
| 12153 |
// For comparison images, check against the base variable, |
| 12154 |
// and not the fake ID which might have been generated for this variable. |
| 12155 |
id = var->basevariable; |
| 12156 |
} |
| 12157 |
|
| 12158 |
if (!type.array.empty()) |
| 12159 |
{ |
| 12160 |
uint32_t major = 2, minor = 0; |
| 12161 |
if (msl_options.is_ios()) |
| 12162 |
{ |
| 12163 |
major = 1; |
| 12164 |
minor = 2; |
| 12165 |
} |
| 12166 |
if (!msl_options.supports_msl_version(major, minor)) |
| 12167 |
{ |
| 12168 |
if (msl_options.is_ios()) |
| 12169 |
SPIRV_CROSS_THROW("MSL 1.2 or greater is required for arrays of textures."); |
| 12170 |
else |
| 12171 |
SPIRV_CROSS_THROW("MSL 2.0 or greater is required for arrays of textures."); |
| 12172 |
} |
| 12173 |
|
| 12174 |
if (type.array.size() > 1) |
| 12175 |
SPIRV_CROSS_THROW("Arrays of arrays of textures are not supported in MSL."); |
| 12176 |
|
| 12177 |
// Arrays of images in MSL must be declared with a special array<T, N> syntax ala C++11 std::array. |
| 12178 |
// If we have a runtime array, it could be a variable-count descriptor set binding. |
| 12179 |
uint32_t array_size = to_array_size_literal(type); |
| 12180 |
if (array_size == 0) |
| 12181 |
array_size = get_resource_array_size(id); |
| 12182 |
|
| 12183 |
if (array_size == 0) |
| 12184 |
SPIRV_CROSS_THROW("Unsized array of images is not supported in MSL."); |
| 12185 |
|
| 12186 |
auto &parent = get<SPIRType>(get_pointee_type(type).parent_type); |
| 12187 |
return join("array<", image_type_glsl(parent, id), ", ", array_size, ">"); |
| 12188 |
} |
| 12189 |
|
| 12190 |
string img_type_name; |
| 12191 |
|
| 12192 |
// Bypass pointers because we need the real image struct |
| 12193 |
auto &img_type = get<SPIRType>(type.self).image; |
| 12194 |
if (image_is_comparison(type, id)) |
| 12195 |
{ |
| 12196 |
switch (img_type.dim) |
| 12197 |
{ |
| 12198 |
case Dim1D: |
| 12199 |
case Dim2D: |
| 12200 |
if (img_type.dim == Dim1D && !msl_options.texture_1D_as_2D) |
| 12201 |
{ |
| 12202 |
// Use a native Metal 1D texture |
| 12203 |
img_type_name += "depth1d_unsupported_by_metal"; |
| 12204 |
break; |
| 12205 |
} |
| 12206 |
|
| 12207 |
if (img_type.ms && img_type.arrayed) |
| 12208 |
{ |
| 12209 |
if (!msl_options.supports_msl_version(2, 1)) |
| 12210 |
SPIRV_CROSS_THROW("Multisampled array textures are supported from 2.1."); |
| 12211 |
img_type_name += "depth2d_ms_array"; |
| 12212 |
} |
| 12213 |
else if (img_type.ms) |
| 12214 |
img_type_name += "depth2d_ms"; |
| 12215 |
else if (img_type.arrayed) |
| 12216 |
img_type_name += "depth2d_array"; |
| 12217 |
else |
| 12218 |
img_type_name += "depth2d"; |
| 12219 |
break; |
| 12220 |
case Dim3D: |
| 12221 |
img_type_name += "depth3d_unsupported_by_metal"; |
| 12222 |
break; |
| 12223 |
case DimCube: |
| 12224 |
if (!msl_options.emulate_cube_array) |
| 12225 |
img_type_name += (img_type.arrayed ? "depthcube_array" : "depthcube"); |
| 12226 |
else |
| 12227 |
img_type_name += (img_type.arrayed ? "depth2d_array" : "depthcube"); |
| 12228 |
break; |
| 12229 |
default: |
| 12230 |
img_type_name += "unknown_depth_texture_type"; |
| 12231 |
break; |
| 12232 |
} |
| 12233 |
} |
| 12234 |
else |
| 12235 |
{ |
| 12236 |
switch (img_type.dim) |
| 12237 |
{ |
| 12238 |
case DimBuffer: |
| 12239 |
if (img_type.ms || img_type.arrayed) |
| 12240 |
SPIRV_CROSS_THROW("Cannot use texel buffers with multisampling or array layers."); |
| 12241 |
|
| 12242 |
if (msl_options.texture_buffer_native) |
| 12243 |
{ |
| 12244 |
if (!msl_options.supports_msl_version(2, 1)) |
| 12245 |
SPIRV_CROSS_THROW("Native texture_buffer type is only supported in MSL 2.1."); |
| 12246 |
img_type_name = "texture_buffer"; |
| 12247 |
} |
| 12248 |
else |
| 12249 |
img_type_name += "texture2d"; |
| 12250 |
break; |
| 12251 |
case Dim1D: |
| 12252 |
case Dim2D: |
| 12253 |
case DimSubpassData: |
| 12254 |
{ |
| 12255 |
bool subpass_array = |
| 12256 |
img_type.dim == DimSubpassData && (msl_options.multiview || msl_options.arrayed_subpass_input); |
| 12257 |
if (img_type.dim == Dim1D && !msl_options.texture_1D_as_2D) |
| 12258 |
{ |
| 12259 |
// Use a native Metal 1D texture |
| 12260 |
img_type_name += (img_type.arrayed ? "texture1d_array" : "texture1d"); |
| 12261 |
break; |
| 12262 |
} |
| 12263 |
|
| 12264 |
// Use Metal's native frame-buffer fetch API for subpass inputs. |
| 12265 |
if (type_is_msl_framebuffer_fetch(type)) |
| 12266 |
{ |
| 12267 |
auto img_type_4 = get<SPIRType>(img_type.type); |
| 12268 |
img_type_4.vecsize = 4; |
| 12269 |
return type_to_glsl(img_type_4); |
| 12270 |
} |
| 12271 |
if (img_type.ms && (img_type.arrayed || subpass_array)) |
| 12272 |
{ |
| 12273 |
if (!msl_options.supports_msl_version(2, 1)) |
| 12274 |
SPIRV_CROSS_THROW("Multisampled array textures are supported from 2.1."); |
| 12275 |
img_type_name += "texture2d_ms_array"; |
| 12276 |
} |
| 12277 |
else if (img_type.ms) |
| 12278 |
img_type_name += "texture2d_ms"; |
| 12279 |
else if (img_type.arrayed || subpass_array) |
| 12280 |
img_type_name += "texture2d_array"; |
| 12281 |
else |
| 12282 |
img_type_name += "texture2d"; |
| 12283 |
break; |
| 12284 |
} |
| 12285 |
case Dim3D: |
| 12286 |
img_type_name += "texture3d"; |
| 12287 |
break; |
| 12288 |
case DimCube: |
| 12289 |
if (!msl_options.emulate_cube_array) |
| 12290 |
img_type_name += (img_type.arrayed ? "texturecube_array" : "texturecube"); |
| 12291 |
else |
| 12292 |
img_type_name += (img_type.arrayed ? "texture2d_array" : "texturecube"); |
| 12293 |
break; |
| 12294 |
default: |
| 12295 |
img_type_name += "unknown_texture_type"; |
| 12296 |
break; |
| 12297 |
} |
| 12298 |
} |
| 12299 |
|
| 12300 |
// Append the pixel type |
| 12301 |
img_type_name += "<"; |
| 12302 |
img_type_name += type_to_glsl(get<SPIRType>(img_type.type)); |
| 12303 |
|
| 12304 |
// For unsampled images, append the sample/read/write access qualifier. |
| 12305 |
// For kernel images, the access qualifier my be supplied directly by SPIR-V. |
| 12306 |
// Otherwise it may be set based on whether the image is read from or written to within the shader. |
| 12307 |
if (type.basetype == SPIRType::Image && type.image.sampled == 2 && type.image.dim != DimSubpassData) |
| 12308 |
{ |
| 12309 |
switch (img_type.access) |
| 12310 |
{ |
| 12311 |
case AccessQualifierReadOnly: |
| 12312 |
img_type_name += ", access::read"; |
| 12313 |
break; |
| 12314 |
|
| 12315 |
case AccessQualifierWriteOnly: |
| 12316 |
img_type_name += ", access::write"; |
| 12317 |
break; |
| 12318 |
|
| 12319 |
case AccessQualifierReadWrite: |
| 12320 |
img_type_name += ", access::read_write"; |
| 12321 |
break; |
| 12322 |
|
| 12323 |
default: |
| 12324 |
{ |
| 12325 |
auto *p_var = maybe_get_backing_variable(id); |
| 12326 |
if (p_var && p_var->basevariable) |
| 12327 |
p_var = maybe_get<SPIRVariable>(p_var->basevariable); |
| 12328 |
if (p_var && !has_decoration(p_var->self, DecorationNonWritable)) |
| 12329 |
{ |
| 12330 |
img_type_name += ", access::"; |
| 12331 |
|
| 12332 |
if (!has_decoration(p_var->self, DecorationNonReadable)) |
| 12333 |
img_type_name += "read_"; |
| 12334 |
|
| 12335 |
img_type_name += "write"; |
| 12336 |
} |
| 12337 |
break; |
| 12338 |
} |
| 12339 |
} |
| 12340 |
} |
| 12341 |
|
| 12342 |
img_type_name += ">"; |
| 12343 |
|
| 12344 |
return img_type_name; |
| 12345 |
} |
| 12346 |
|
| 12347 |
void CompilerMSL::emit_subgroup_op(const Instruction &i) |
| 12348 |
{ |
| 12349 |
const uint32_t *ops = stream(i); |
| 12350 |
auto op = static_cast<Op>(i.op); |
| 12351 |
|
| 12352 |
// Metal 2.0 is required. iOS only supports quad ops. macOS only supports |
| 12353 |
// broadcast and shuffle on 10.13 (2.0), with full support in 10.14 (2.1). |
| 12354 |
// Note that iOS makes no distinction between a quad-group and a subgroup; |
| 12355 |
// all subgroups are quad-groups there. |
| 12356 |
if (!msl_options.supports_msl_version(2)) |
| 12357 |
SPIRV_CROSS_THROW("Subgroups are only supported in Metal 2.0 and up."); |
| 12358 |
|
| 12359 |
// If we need to do implicit bitcasts, make sure we do it with the correct type. |
| 12360 |
uint32_t integer_width = get_integer_width_for_instruction(i); |
| 12361 |
auto int_type = to_signed_basetype(integer_width); |
| 12362 |
auto uint_type = to_unsigned_basetype(integer_width); |
| 12363 |
|
| 12364 |
if (msl_options.is_ios()) |
| 12365 |
{ |
| 12366 |
switch (op) |
| 12367 |
{ |
| 12368 |
default: |
| 12369 |
SPIRV_CROSS_THROW("iOS only supports quad-group operations."); |
| 12370 |
case OpGroupNonUniformBroadcast: |
| 12371 |
case OpGroupNonUniformShuffle: |
| 12372 |
case OpGroupNonUniformShuffleXor: |
| 12373 |
case OpGroupNonUniformShuffleUp: |
| 12374 |
case OpGroupNonUniformShuffleDown: |
| 12375 |
case OpGroupNonUniformQuadSwap: |
| 12376 |
case OpGroupNonUniformQuadBroadcast: |
| 12377 |
break; |
| 12378 |
} |
| 12379 |
} |
| 12380 |
|
| 12381 |
if (msl_options.is_macos() && !msl_options.supports_msl_version(2, 1)) |
| 12382 |
{ |
| 12383 |
switch (op) |
| 12384 |
{ |
| 12385 |
default: |
| 12386 |
SPIRV_CROSS_THROW("Subgroup ops beyond broadcast and shuffle on macOS require Metal 2.1 and up."); |
| 12387 |
case OpGroupNonUniformBroadcast: |
| 12388 |
case OpGroupNonUniformShuffle: |
| 12389 |
case OpGroupNonUniformShuffleXor: |
| 12390 |
case OpGroupNonUniformShuffleUp: |
| 12391 |
case OpGroupNonUniformShuffleDown: |
| 12392 |
break; |
| 12393 |
} |
| 12394 |
} |
| 12395 |
|
| 12396 |
uint32_t result_type = ops[0]; |
| 12397 |
uint32_t id = ops[1]; |
| 12398 |
|
| 12399 |
auto scope = static_cast<Scope>(evaluate_constant_u32(ops[2])); |
| 12400 |
if (scope != ScopeSubgroup) |
| 12401 |
SPIRV_CROSS_THROW("Only subgroup scope is supported."); |
| 12402 |
|
| 12403 |
switch (op) |
| 12404 |
{ |
| 12405 |
case OpGroupNonUniformElect: |
| 12406 |
emit_op(result_type, id, "simd_is_first()", true); |
| 12407 |
break; |
| 12408 |
|
| 12409 |
case OpGroupNonUniformBroadcast: |
| 12410 |
emit_binary_func_op(result_type, id, ops[3], ops[4], "spvSubgroupBroadcast"); |
| 12411 |
break; |
| 12412 |
|
| 12413 |
case OpGroupNonUniformBroadcastFirst: |
| 12414 |
emit_unary_func_op(result_type, id, ops[3], "spvSubgroupBroadcastFirst"); |
| 12415 |
break; |
| 12416 |
|
| 12417 |
case OpGroupNonUniformBallot: |
| 12418 |
emit_unary_func_op(result_type, id, ops[3], "spvSubgroupBallot"); |
| 12419 |
break; |
| 12420 |
|
| 12421 |
case OpGroupNonUniformInverseBallot: |
| 12422 |
emit_binary_func_op(result_type, id, ops[3], builtin_subgroup_invocation_id_id, "spvSubgroupBallotBitExtract"); |
| 12423 |
break; |
| 12424 |
|
| 12425 |
case OpGroupNonUniformBallotBitExtract: |
| 12426 |
emit_binary_func_op(result_type, id, ops[3], ops[4], "spvSubgroupBallotBitExtract"); |
| 12427 |
break; |
| 12428 |
|
| 12429 |
case OpGroupNonUniformBallotFindLSB: |
| 12430 |
emit_binary_func_op(result_type, id, ops[3], builtin_subgroup_size_id, "spvSubgroupBallotFindLSB"); |
| 12431 |
break; |
| 12432 |
|
| 12433 |
case OpGroupNonUniformBallotFindMSB: |
| 12434 |
emit_binary_func_op(result_type, id, ops[3], builtin_subgroup_size_id, "spvSubgroupBallotFindMSB"); |
| 12435 |
break; |
| 12436 |
|
| 12437 |
case OpGroupNonUniformBallotBitCount: |
| 12438 |
{ |
| 12439 |
auto operation = static_cast<GroupOperation>(ops[3]); |
| 12440 |
switch (operation) |
| 12441 |
{ |
| 12442 |
case GroupOperationReduce: |
| 12443 |
emit_binary_func_op(result_type, id, ops[4], builtin_subgroup_size_id, "spvSubgroupBallotBitCount"); |
| 12444 |
break; |
| 12445 |
case GroupOperationInclusiveScan: |
| 12446 |
emit_binary_func_op(result_type, id, ops[4], builtin_subgroup_invocation_id_id, |
| 12447 |
"spvSubgroupBallotInclusiveBitCount"); |
| 12448 |
break; |
| 12449 |
case GroupOperationExclusiveScan: |
| 12450 |
emit_binary_func_op(result_type, id, ops[4], builtin_subgroup_invocation_id_id, |
| 12451 |
"spvSubgroupBallotExclusiveBitCount"); |
| 12452 |
break; |
| 12453 |
default: |
| 12454 |
SPIRV_CROSS_THROW("Invalid BitCount operation."); |
| 12455 |
break; |
| 12456 |
} |
| 12457 |
break; |
| 12458 |
} |
| 12459 |
|
| 12460 |
case OpGroupNonUniformShuffle: |
| 12461 |
emit_binary_func_op(result_type, id, ops[3], ops[4], "spvSubgroupShuffle"); |
| 12462 |
break; |
| 12463 |
|
| 12464 |
case OpGroupNonUniformShuffleXor: |
| 12465 |
emit_binary_func_op(result_type, id, ops[3], ops[4], "spvSubgroupShuffleXor"); |
| 12466 |
break; |
| 12467 |
|
| 12468 |
case OpGroupNonUniformShuffleUp: |
| 12469 |
emit_binary_func_op(result_type, id, ops[3], ops[4], "spvSubgroupShuffleUp"); |
| 12470 |
break; |
| 12471 |
|
| 12472 |
case OpGroupNonUniformShuffleDown: |
| 12473 |
emit_binary_func_op(result_type, id, ops[3], ops[4], "spvSubgroupShuffleDown"); |
| 12474 |
break; |
| 12475 |
|
| 12476 |
case OpGroupNonUniformAll: |
| 12477 |
emit_unary_func_op(result_type, id, ops[3], "simd_all"); |
| 12478 |
break; |
| 12479 |
|
| 12480 |
case OpGroupNonUniformAny: |
| 12481 |
emit_unary_func_op(result_type, id, ops[3], "simd_any"); |
| 12482 |
break; |
| 12483 |
|
| 12484 |
case OpGroupNonUniformAllEqual: |
| 12485 |
emit_unary_func_op(result_type, id, ops[3], "spvSubgroupAllEqual"); |
| 12486 |
break; |
| 12487 |
|
| 12488 |
// clang-format off |
| 12489 |
#define MSL_GROUP_OP(op, msl_op) \ |
| 12490 |
case OpGroupNonUniform##op: \ |
| 12491 |
{ \ |
| 12492 |
auto operation = static_cast<GroupOperation>(ops[3]); \ |
| 12493 |
if (operation == GroupOperationReduce) \ |
| 12494 |
emit_unary_func_op(result_type, id, ops[4], "simd_" #msl_op); \ |
| 12495 |
else if (operation == GroupOperationInclusiveScan) \ |
| 12496 |
emit_unary_func_op(result_type, id, ops[4], "simd_prefix_inclusive_" #msl_op); \ |
| 12497 |
else if (operation == GroupOperationExclusiveScan) \ |
| 12498 |
emit_unary_func_op(result_type, id, ops[4], "simd_prefix_exclusive_" #msl_op); \ |
| 12499 |
else if (operation == GroupOperationClusteredReduce) \ |
| 12500 |
{ \ |
| 12501 |
/* Only cluster sizes of 4 are supported. */ \ |
| 12502 |
uint32_t cluster_size = evaluate_constant_u32(ops[5]); \ |
| 12503 |
if (cluster_size != 4) \ |
| 12504 |
SPIRV_CROSS_THROW("Metal only supports quad ClusteredReduce."); \ |
| 12505 |
emit_unary_func_op(result_type, id, ops[4], "quad_" #msl_op); \ |
| 12506 |
} \ |
| 12507 |
else \ |
| 12508 |
SPIRV_CROSS_THROW("Invalid group operation."); \ |
| 12509 |
break; \ |
| 12510 |
} |
| 12511 |
MSL_GROUP_OP(FAdd, sum) |
| 12512 |
MSL_GROUP_OP(FMul, product) |
| 12513 |
MSL_GROUP_OP(IAdd, sum) |
| 12514 |
MSL_GROUP_OP(IMul, product) |
| 12515 |
#undef MSL_GROUP_OP |
| 12516 |
// The others, unfortunately, don't support InclusiveScan or ExclusiveScan. |
| 12517 |
|
| 12518 |
#define MSL_GROUP_OP(op, msl_op) \ |
| 12519 |
case OpGroupNonUniform##op: \ |
| 12520 |
{ \ |
| 12521 |
auto operation = static_cast<GroupOperation>(ops[3]); \ |
| 12522 |
if (operation == GroupOperationReduce) \ |
| 12523 |
emit_unary_func_op(result_type, id, ops[4], "simd_" #msl_op); \ |
| 12524 |
else if (operation == GroupOperationInclusiveScan) \ |
| 12525 |
SPIRV_CROSS_THROW("Metal doesn't support InclusiveScan for OpGroupNonUniform" #op "."); \ |
| 12526 |
else if (operation == GroupOperationExclusiveScan) \ |
| 12527 |
SPIRV_CROSS_THROW("Metal doesn't support ExclusiveScan for OpGroupNonUniform" #op "."); \ |
| 12528 |
else if (operation == GroupOperationClusteredReduce) \ |
| 12529 |
{ \ |
| 12530 |
/* Only cluster sizes of 4 are supported. */ \ |
| 12531 |
uint32_t cluster_size = evaluate_constant_u32(ops[5]); \ |
| 12532 |
if (cluster_size != 4) \ |
| 12533 |
SPIRV_CROSS_THROW("Metal only supports quad ClusteredReduce."); \ |
| 12534 |
emit_unary_func_op(result_type, id, ops[4], "quad_" #msl_op); \ |
| 12535 |
} \ |
| 12536 |
else \ |
| 12537 |
SPIRV_CROSS_THROW("Invalid group operation."); \ |
| 12538 |
break; \ |
| 12539 |
} |
| 12540 |
|
| 12541 |
#define MSL_GROUP_OP_CAST(op, msl_op, type) \ |
| 12542 |
case OpGroupNonUniform##op: \ |
| 12543 |
{ \ |
| 12544 |
auto operation = static_cast<GroupOperation>(ops[3]); \ |
| 12545 |
if (operation == GroupOperationReduce) \ |
| 12546 |
emit_unary_func_op_cast(result_type, id, ops[4], "simd_" #msl_op, type, type); \ |
| 12547 |
else if (operation == GroupOperationInclusiveScan) \ |
| 12548 |
SPIRV_CROSS_THROW("Metal doesn't support InclusiveScan for OpGroupNonUniform" #op "."); \ |
| 12549 |
else if (operation == GroupOperationExclusiveScan) \ |
| 12550 |
SPIRV_CROSS_THROW("Metal doesn't support ExclusiveScan for OpGroupNonUniform" #op "."); \ |
| 12551 |
else if (operation == GroupOperationClusteredReduce) \ |
| 12552 |
{ \ |
| 12553 |
/* Only cluster sizes of 4 are supported. */ \ |
| 12554 |
uint32_t cluster_size = evaluate_constant_u32(ops[5]); \ |
| 12555 |
if (cluster_size != 4) \ |
| 12556 |
SPIRV_CROSS_THROW("Metal only supports quad ClusteredReduce."); \ |
| 12557 |
emit_unary_func_op_cast(result_type, id, ops[4], "quad_" #msl_op, type, type); \ |
| 12558 |
} \ |
| 12559 |
else \ |
| 12560 |
SPIRV_CROSS_THROW("Invalid group operation."); \ |
| 12561 |
break; \ |
| 12562 |
} |
| 12563 |
|
| 12564 |
MSL_GROUP_OP(FMin, min) |
| 12565 |
MSL_GROUP_OP(FMax, max) |
| 12566 |
MSL_GROUP_OP_CAST(SMin, min, int_type) |
| 12567 |
MSL_GROUP_OP_CAST(SMax, max, int_type) |
| 12568 |
MSL_GROUP_OP_CAST(UMin, min, uint_type) |
| 12569 |
MSL_GROUP_OP_CAST(UMax, max, uint_type) |
| 12570 |
MSL_GROUP_OP(BitwiseAnd, and) |
| 12571 |
MSL_GROUP_OP(BitwiseOr, or) |
| 12572 |
MSL_GROUP_OP(BitwiseXor, xor) |
| 12573 |
MSL_GROUP_OP(LogicalAnd, and) |
| 12574 |
MSL_GROUP_OP(LogicalOr, or) |
| 12575 |
MSL_GROUP_OP(LogicalXor, xor) |
| 12576 |
// clang-format on |
| 12577 |
#undef MSL_GROUP_OP |
| 12578 |
#undef MSL_GROUP_OP_CAST |
| 12579 |
|
| 12580 |
case OpGroupNonUniformQuadSwap: |
| 12581 |
emit_binary_func_op(result_type, id, ops[3], ops[4], "spvQuadSwap"); |
| 12582 |
break; |
| 12583 |
|
| 12584 |
case OpGroupNonUniformQuadBroadcast: |
| 12585 |
emit_binary_func_op(result_type, id, ops[3], ops[4], "spvQuadBroadcast"); |
| 12586 |
break; |
| 12587 |
|
| 12588 |
default: |
| 12589 |
SPIRV_CROSS_THROW("Invalid opcode for subgroup."); |
| 12590 |
} |
| 12591 |
|
| 12592 |
register_control_dependent_expression(id); |
| 12593 |
} |
| 12594 |
|
| 12595 |
string CompilerMSL::bitcast_glsl_op(const SPIRType &out_type, const SPIRType &in_type) |
| 12596 |
{ |
| 12597 |
if (out_type.basetype == in_type.basetype) |
| 12598 |
return ""; |
| 12599 |
|
| 12600 |
assert(out_type.basetype != SPIRType::Boolean); |
| 12601 |
assert(in_type.basetype != SPIRType::Boolean); |
| 12602 |
|
| 12603 |
bool integral_cast = type_is_integral(out_type) && type_is_integral(in_type); |
| 12604 |
bool same_size_cast = out_type.width == in_type.width; |
| 12605 |
|
| 12606 |
if (integral_cast && same_size_cast) |
| 12607 |
{ |
| 12608 |
// Trivial bitcast case, casts between integers. |
| 12609 |
return type_to_glsl(out_type); |
| 12610 |
} |
| 12611 |
else |
| 12612 |
{ |
| 12613 |
// Fall back to the catch-all bitcast in MSL. |
| 12614 |
return "as_type<" + type_to_glsl(out_type) + ">"; |
| 12615 |
} |
| 12616 |
} |
| 12617 |
|
| 12618 |
bool CompilerMSL::emit_complex_bitcast(uint32_t, uint32_t, uint32_t) |
| 12619 |
{ |
| 12620 |
return false; |
| 12621 |
} |
| 12622 |
|
| 12623 |
// Returns an MSL string identifying the name of a SPIR-V builtin. |
| 12624 |
// Output builtins are qualified with the name of the stage out structure. |
| 12625 |
string CompilerMSL::builtin_to_glsl(BuiltIn builtin, StorageClass storage) |
| 12626 |
{ |
| 12627 |
switch (builtin) |
| 12628 |
{ |
| 12629 |
|
| 12630 |
// Handle HLSL-style 0-based vertex/instance index. |
| 12631 |
// Override GLSL compiler strictness |
| 12632 |
case BuiltInVertexId: |
| 12633 |
ensure_builtin(StorageClassInput, BuiltInVertexId); |
| 12634 |
if (msl_options.enable_base_index_zero && msl_options.supports_msl_version(1, 1) && |
| 12635 |
(msl_options.ios_support_base_vertex_instance || msl_options.is_macos())) |
| 12636 |
{ |
| 12637 |
if (builtin_declaration) |
| 12638 |
{ |
| 12639 |
if (needs_base_vertex_arg != TriState::No) |
| 12640 |
needs_base_vertex_arg = TriState::Yes; |
| 12641 |
return "gl_VertexID"; |
| 12642 |
} |
| 12643 |
else |
| 12644 |
{ |
| 12645 |
ensure_builtin(StorageClassInput, BuiltInBaseVertex); |
| 12646 |
return "(gl_VertexID - gl_BaseVertex)"; |
| 12647 |
} |
| 12648 |
} |
| 12649 |
else |
| 12650 |
{ |
| 12651 |
return "gl_VertexID"; |
| 12652 |
} |
| 12653 |
case BuiltInInstanceId: |
| 12654 |
ensure_builtin(StorageClassInput, BuiltInInstanceId); |
| 12655 |
if (msl_options.enable_base_index_zero && msl_options.supports_msl_version(1, 1) && |
| 12656 |
(msl_options.ios_support_base_vertex_instance || msl_options.is_macos())) |
| 12657 |
{ |
| 12658 |
if (builtin_declaration) |
| 12659 |
{ |
| 12660 |
if (needs_base_instance_arg != TriState::No) |
| 12661 |
needs_base_instance_arg = TriState::Yes; |
| 12662 |
return "gl_InstanceID"; |
| 12663 |
} |
| 12664 |
else |
| 12665 |
{ |
| 12666 |
ensure_builtin(StorageClassInput, BuiltInBaseInstance); |
| 12667 |
return "(gl_InstanceID - gl_BaseInstance)"; |
| 12668 |
} |
| 12669 |
} |
| 12670 |
else |
| 12671 |
{ |
| 12672 |
return "gl_InstanceID"; |
| 12673 |
} |
| 12674 |
case BuiltInVertexIndex: |
| 12675 |
ensure_builtin(StorageClassInput, BuiltInVertexIndex); |
| 12676 |
if (msl_options.enable_base_index_zero && msl_options.supports_msl_version(1, 1) && |
| 12677 |
(msl_options.ios_support_base_vertex_instance || msl_options.is_macos())) |
| 12678 |
{ |
| 12679 |
if (builtin_declaration) |
| 12680 |
{ |
| 12681 |
if (needs_base_vertex_arg != TriState::No) |
| 12682 |
needs_base_vertex_arg = TriState::Yes; |
| 12683 |
return "gl_VertexIndex"; |
| 12684 |
} |
| 12685 |
else |
| 12686 |
{ |
| 12687 |
ensure_builtin(StorageClassInput, BuiltInBaseVertex); |
| 12688 |
return "(gl_VertexIndex - gl_BaseVertex)"; |
| 12689 |
} |
| 12690 |
} |
| 12691 |
else |
| 12692 |
{ |
| 12693 |
return "gl_VertexIndex"; |
| 12694 |
} |
| 12695 |
case BuiltInInstanceIndex: |
| 12696 |
ensure_builtin(StorageClassInput, BuiltInInstanceIndex); |
| 12697 |
if (msl_options.enable_base_index_zero && msl_options.supports_msl_version(1, 1) && |
| 12698 |
(msl_options.ios_support_base_vertex_instance || msl_options.is_macos())) |
| 12699 |
{ |
| 12700 |
if (builtin_declaration) |
| 12701 |
{ |
| 12702 |
if (needs_base_instance_arg != TriState::No) |
| 12703 |
needs_base_instance_arg = TriState::Yes; |
| 12704 |
return "gl_InstanceIndex"; |
| 12705 |
} |
| 12706 |
else |
| 12707 |
{ |
| 12708 |
ensure_builtin(StorageClassInput, BuiltInBaseInstance); |
| 12709 |
return "(gl_InstanceIndex - gl_BaseInstance)"; |
| 12710 |
} |
| 12711 |
} |
| 12712 |
else |
| 12713 |
{ |
| 12714 |
return "gl_InstanceIndex"; |
| 12715 |
} |
| 12716 |
case BuiltInBaseVertex: |
| 12717 |
if (msl_options.supports_msl_version(1, 1) && |
| 12718 |
(msl_options.ios_support_base_vertex_instance || msl_options.is_macos())) |
| 12719 |
{ |
| 12720 |
needs_base_vertex_arg = TriState::No; |
| 12721 |
return "gl_BaseVertex"; |
| 12722 |
} |
| 12723 |
else |
| 12724 |
{ |
| 12725 |
SPIRV_CROSS_THROW("BaseVertex requires Metal 1.1 and Mac or Apple A9+ hardware."); |
| 12726 |
} |
| 12727 |
case BuiltInBaseInstance: |
| 12728 |
if (msl_options.supports_msl_version(1, 1) && |
| 12729 |
(msl_options.ios_support_base_vertex_instance || msl_options.is_macos())) |
| 12730 |
{ |
| 12731 |
needs_base_instance_arg = TriState::No; |
| 12732 |
return "gl_BaseInstance"; |
| 12733 |
} |
| 12734 |
else |
| 12735 |
{ |
| 12736 |
SPIRV_CROSS_THROW("BaseInstance requires Metal 1.1 and Mac or Apple A9+ hardware."); |
| 12737 |
} |
| 12738 |
case BuiltInDrawIndex: |
| 12739 |
SPIRV_CROSS_THROW("DrawIndex is not supported in MSL."); |
| 12740 |
|
| 12741 |
// When used in the entry function, output builtins are qualified with output struct name. |
| 12742 |
// Test storage class as NOT Input, as output builtins might be part of generic type. |
| 12743 |
// Also don't do this for tessellation control shaders. |
| 12744 |
case BuiltInViewportIndex: |
| 12745 |
if (!msl_options.supports_msl_version(2, 0)) |
| 12746 |
SPIRV_CROSS_THROW("ViewportIndex requires Metal 2.0."); |
| 12747 |
/* fallthrough */ |
| 12748 |
case BuiltInFragDepth: |
| 12749 |
case BuiltInFragStencilRefEXT: |
| 12750 |
if ((builtin == BuiltInFragDepth && !msl_options.enable_frag_depth_builtin) || |
| 12751 |
(builtin == BuiltInFragStencilRefEXT && !msl_options.enable_frag_stencil_ref_builtin)) |
| 12752 |
break; |
| 12753 |
/* fallthrough */ |
| 12754 |
case BuiltInPosition: |
| 12755 |
case BuiltInPointSize: |
| 12756 |
case BuiltInClipDistance: |
| 12757 |
case BuiltInCullDistance: |
| 12758 |
case BuiltInLayer: |
| 12759 |
case BuiltInSampleMask: |
| 12760 |
if (get_execution_model() == ExecutionModelTessellationControl) |
| 12761 |
break; |
| 12762 |
if (storage != StorageClassInput && current_function && (current_function->self == ir.default_entry_point)) |
| 12763 |
return stage_out_var_name + "." + CompilerGLSL::builtin_to_glsl(builtin, storage); |
| 12764 |
|
| 12765 |
break; |
| 12766 |
|
| 12767 |
case BuiltInBaryCoordNV: |
| 12768 |
case BuiltInBaryCoordNoPerspNV: |
| 12769 |
if (storage == StorageClassInput && current_function && (current_function->self == ir.default_entry_point)) |
| 12770 |
return stage_in_var_name + "." + CompilerGLSL::builtin_to_glsl(builtin, storage); |
| 12771 |
break; |
| 12772 |
|
| 12773 |
case BuiltInTessLevelOuter: |
| 12774 |
if (get_execution_model() == ExecutionModelTessellationEvaluation) |
| 12775 |
{ |
| 12776 |
if (storage != StorageClassOutput && !get_entry_point().flags.get(ExecutionModeTriangles) && |
| 12777 |
current_function && (current_function->self == ir.default_entry_point)) |
| 12778 |
return join(patch_stage_in_var_name, ".", CompilerGLSL::builtin_to_glsl(builtin, storage)); |
| 12779 |
else |
| 12780 |
break; |
| 12781 |
} |
| 12782 |
if (storage != StorageClassInput && current_function && (current_function->self == ir.default_entry_point)) |
| 12783 |
return join(tess_factor_buffer_var_name, "[", to_expression(builtin_primitive_id_id), |
| 12784 |
"].edgeTessellationFactor"); |
| 12785 |
break; |
| 12786 |
|
| 12787 |
case BuiltInTessLevelInner: |
| 12788 |
if (get_execution_model() == ExecutionModelTessellationEvaluation) |
| 12789 |
{ |
| 12790 |
if (storage != StorageClassOutput && !get_entry_point().flags.get(ExecutionModeTriangles) && |
| 12791 |
current_function && (current_function->self == ir.default_entry_point)) |
| 12792 |
return join(patch_stage_in_var_name, ".", CompilerGLSL::builtin_to_glsl(builtin, storage)); |
| 12793 |
else |
| 12794 |
break; |
| 12795 |
} |
| 12796 |
if (storage != StorageClassInput && current_function && (current_function->self == ir.default_entry_point)) |
| 12797 |
return join(tess_factor_buffer_var_name, "[", to_expression(builtin_primitive_id_id), |
| 12798 |
"].insideTessellationFactor"); |
| 12799 |
break; |
| 12800 |
|
| 12801 |
default: |
| 12802 |
break; |
| 12803 |
} |
| 12804 |
|
| 12805 |
return CompilerGLSL::builtin_to_glsl(builtin, storage); |
| 12806 |
} |
| 12807 |
|
| 12808 |
// Returns an MSL string attribute qualifer for a SPIR-V builtin |
| 12809 |
string CompilerMSL::builtin_qualifier(BuiltIn builtin) |
| 12810 |
{ |
| 12811 |
auto &execution = get_entry_point(); |
| 12812 |
|
| 12813 |
switch (builtin) |
| 12814 |
{ |
| 12815 |
// Vertex function in |
| 12816 |
case BuiltInVertexId: |
| 12817 |
return "vertex_id"; |
| 12818 |
case BuiltInVertexIndex: |
| 12819 |
return "vertex_id"; |
| 12820 |
case BuiltInBaseVertex: |
| 12821 |
return "base_vertex"; |
| 12822 |
case BuiltInInstanceId: |
| 12823 |
return "instance_id"; |
| 12824 |
case BuiltInInstanceIndex: |
| 12825 |
return "instance_id"; |
| 12826 |
case BuiltInBaseInstance: |
| 12827 |
return "base_instance"; |
| 12828 |
case BuiltInDrawIndex: |
| 12829 |
SPIRV_CROSS_THROW("DrawIndex is not supported in MSL."); |
| 12830 |
|
| 12831 |
// Vertex function out |
| 12832 |
case BuiltInClipDistance: |
| 12833 |
return "clip_distance"; |
| 12834 |
case BuiltInPointSize: |
| 12835 |
return "point_size"; |
| 12836 |
case BuiltInPosition: |
| 12837 |
if (position_invariant) |
| 12838 |
{ |
| 12839 |
if (!msl_options.supports_msl_version(2, 1)) |
| 12840 |
SPIRV_CROSS_THROW("Invariant position is only supported on MSL 2.1 and up."); |
| 12841 |
return "position, invariant"; |
| 12842 |
} |
| 12843 |
else |
| 12844 |
return "position"; |
| 12845 |
case BuiltInLayer: |
| 12846 |
return "render_target_array_index"; |
| 12847 |
case BuiltInViewportIndex: |
| 12848 |
if (!msl_options.supports_msl_version(2, 0)) |
| 12849 |
SPIRV_CROSS_THROW("ViewportIndex requires Metal 2.0."); |
| 12850 |
return "viewport_array_index"; |
| 12851 |
|
| 12852 |
// Tess. control function in |
| 12853 |
case BuiltInInvocationId: |
| 12854 |
if (msl_options.multi_patch_workgroup) |
| 12855 |
{ |
| 12856 |
// Shouldn't be reached. |
| 12857 |
SPIRV_CROSS_THROW("InvocationId is computed manually with multi-patch workgroups in MSL."); |
| 12858 |
} |
| 12859 |
return "thread_index_in_threadgroup"; |
| 12860 |
case BuiltInPatchVertices: |
| 12861 |
// Shouldn't be reached. |
| 12862 |
SPIRV_CROSS_THROW("PatchVertices is derived from the auxiliary buffer in MSL."); |
| 12863 |
case BuiltInPrimitiveId: |
| 12864 |
switch (execution.model) |
| 12865 |
{ |
| 12866 |
case ExecutionModelTessellationControl: |
| 12867 |
if (msl_options.multi_patch_workgroup) |
| 12868 |
{ |
| 12869 |
// Shouldn't be reached. |
| 12870 |
SPIRV_CROSS_THROW("PrimitiveId is computed manually with multi-patch workgroups in MSL."); |
| 12871 |
} |
| 12872 |
return "threadgroup_position_in_grid"; |
| 12873 |
case ExecutionModelTessellationEvaluation: |
| 12874 |
return "patch_id"; |
| 12875 |
case ExecutionModelFragment: |
| 12876 |
if (msl_options.is_ios()) |
| 12877 |
SPIRV_CROSS_THROW("PrimitiveId is not supported in fragment on iOS."); |
| 12878 |
else if (msl_options.is_macos() && !msl_options.supports_msl_version(2, 2)) |
| 12879 |
SPIRV_CROSS_THROW("PrimitiveId on macOS requires MSL 2.2."); |
| 12880 |
return "primitive_id"; |
| 12881 |
default: |
| 12882 |
SPIRV_CROSS_THROW("PrimitiveId is not supported in this execution model."); |
| 12883 |
} |
| 12884 |
|
| 12885 |
// Tess. control function out |
| 12886 |
case BuiltInTessLevelOuter: |
| 12887 |
case BuiltInTessLevelInner: |
| 12888 |
// Shouldn't be reached. |
| 12889 |
SPIRV_CROSS_THROW("Tessellation levels are handled specially in MSL."); |
| 12890 |
|
| 12891 |
// Tess. evaluation function in |
| 12892 |
case BuiltInTessCoord: |
| 12893 |
return "position_in_patch"; |
| 12894 |
|
| 12895 |
// Fragment function in |
| 12896 |
case BuiltInFrontFacing: |
| 12897 |
return "front_facing"; |
| 12898 |
case BuiltInPointCoord: |
| 12899 |
return "point_coord"; |
| 12900 |
case BuiltInFragCoord: |
| 12901 |
return "position"; |
| 12902 |
case BuiltInSampleId: |
| 12903 |
return "sample_id"; |
| 12904 |
case BuiltInSampleMask: |
| 12905 |
return "sample_mask"; |
| 12906 |
case BuiltInSamplePosition: |
| 12907 |
// Shouldn't be reached. |
| 12908 |
SPIRV_CROSS_THROW("Sample position is retrieved by a function in MSL."); |
| 12909 |
case BuiltInViewIndex: |
| 12910 |
if (execution.model != ExecutionModelFragment) |
| 12911 |
SPIRV_CROSS_THROW("ViewIndex is handled specially outside fragment shaders."); |
| 12912 |
// The ViewIndex was implicitly used in the prior stages to set the render_target_array_index, |
| 12913 |
// so we can get it from there. |
| 12914 |
return "render_target_array_index"; |
| 12915 |
|
| 12916 |
// Fragment function out |
| 12917 |
case BuiltInFragDepth: |
| 12918 |
if (execution.flags.get(ExecutionModeDepthGreater)) |
| 12919 |
return "depth(greater)"; |
| 12920 |
else if (execution.flags.get(ExecutionModeDepthLess)) |
| 12921 |
return "depth(less)"; |
| 12922 |
else |
| 12923 |
return "depth(any)"; |
| 12924 |
|
| 12925 |
case BuiltInFragStencilRefEXT: |
| 12926 |
return "stencil"; |
| 12927 |
|
| 12928 |
// Compute function in |
| 12929 |
case BuiltInGlobalInvocationId: |
| 12930 |
return "thread_position_in_grid"; |
| 12931 |
|
| 12932 |
case BuiltInWorkgroupId: |
| 12933 |
return "threadgroup_position_in_grid"; |
| 12934 |
|
| 12935 |
case BuiltInNumWorkgroups: |
| 12936 |
return "threadgroups_per_grid"; |
| 12937 |
|
| 12938 |
case BuiltInLocalInvocationId: |
| 12939 |
return "thread_position_in_threadgroup"; |
| 12940 |
|
| 12941 |
case BuiltInLocalInvocationIndex: |
| 12942 |
return "thread_index_in_threadgroup"; |
| 12943 |
|
| 12944 |
case BuiltInSubgroupSize: |
| 12945 |
if (execution.model == ExecutionModelFragment) |
| 12946 |
{ |
| 12947 |
if (!msl_options.supports_msl_version(2, 2)) |
| 12948 |
SPIRV_CROSS_THROW("threads_per_simdgroup requires Metal 2.2 in fragment shaders."); |
| 12949 |
return "threads_per_simdgroup"; |
| 12950 |
} |
| 12951 |
else |
| 12952 |
{ |
| 12953 |
// thread_execution_width is an alias for threads_per_simdgroup, and it's only available since 1.0, |
| 12954 |
// but not in fragment. |
| 12955 |
return "thread_execution_width"; |
| 12956 |
} |
| 12957 |
|
| 12958 |
case BuiltInNumSubgroups: |
| 12959 |
if (!msl_options.supports_msl_version(2)) |
| 12960 |
SPIRV_CROSS_THROW("Subgroup builtins require Metal 2.0."); |
| 12961 |
return msl_options.is_ios() ? "quadgroups_per_threadgroup" : "simdgroups_per_threadgroup"; |
| 12962 |
|
| 12963 |
case BuiltInSubgroupId: |
| 12964 |
if (!msl_options.supports_msl_version(2)) |
| 12965 |
SPIRV_CROSS_THROW("Subgroup builtins require Metal 2.0."); |
| 12966 |
return msl_options.is_ios() ? "quadgroup_index_in_threadgroup" : "simdgroup_index_in_threadgroup"; |
| 12967 |
|
| 12968 |
case BuiltInSubgroupLocalInvocationId: |
| 12969 |
if (execution.model == ExecutionModelFragment) |
| 12970 |
{ |
| 12971 |
if (!msl_options.supports_msl_version(2, 2)) |
| 12972 |
SPIRV_CROSS_THROW("thread_index_in_simdgroup requires Metal 2.2 in fragment shaders."); |
| 12973 |
return "thread_index_in_simdgroup"; |
| 12974 |
} |
| 12975 |
else |
| 12976 |
{ |
| 12977 |
if (!msl_options.supports_msl_version(2)) |
| 12978 |
SPIRV_CROSS_THROW("Subgroup builtins require Metal 2.0."); |
| 12979 |
return msl_options.is_ios() ? "thread_index_in_quadgroup" : "thread_index_in_simdgroup"; |
| 12980 |
} |
| 12981 |
|
| 12982 |
case BuiltInSubgroupEqMask: |
| 12983 |
case BuiltInSubgroupGeMask: |
| 12984 |
case BuiltInSubgroupGtMask: |
| 12985 |
case BuiltInSubgroupLeMask: |
| 12986 |
case BuiltInSubgroupLtMask: |
| 12987 |
// Shouldn't be reached. |
| 12988 |
SPIRV_CROSS_THROW("Subgroup ballot masks are handled specially in MSL."); |
| 12989 |
|
| 12990 |
case BuiltInBaryCoordNV: |
| 12991 |
// TODO: AMD barycentrics as well? Seem to have different swizzle and 2 components rather than 3. |
| 12992 |
if (msl_options.is_ios()) |
| 12993 |
SPIRV_CROSS_THROW("Barycentrics not supported on iOS."); |
| 12994 |
else if (!msl_options.supports_msl_version(2, 2)) |
| 12995 |
SPIRV_CROSS_THROW("Barycentrics are only supported in MSL 2.2 and above on macOS."); |
| 12996 |
return "barycentric_coord, center_perspective"; |
| 12997 |
|
| 12998 |
case BuiltInBaryCoordNoPerspNV: |
| 12999 |
// TODO: AMD barycentrics as well? Seem to have different swizzle and 2 components rather than 3. |
| 13000 |
if (msl_options.is_ios()) |
| 13001 |
SPIRV_CROSS_THROW("Barycentrics not supported on iOS."); |
| 13002 |
else if (!msl_options.supports_msl_version(2, 2)) |
| 13003 |
SPIRV_CROSS_THROW("Barycentrics are only supported in MSL 2.2 and above on macOS."); |
| 13004 |
return "barycentric_coord, center_no_perspective"; |
| 13005 |
|
| 13006 |
default: |
| 13007 |
return "unsupported-built-in"; |
| 13008 |
} |
| 13009 |
} |
| 13010 |
|
| 13011 |
// Returns an MSL string type declaration for a SPIR-V builtin |
| 13012 |
string CompilerMSL::builtin_type_decl(BuiltIn builtin, uint32_t id) |
| 13013 |
{ |
| 13014 |
const SPIREntryPoint &execution = get_entry_point(); |
| 13015 |
switch (builtin) |
| 13016 |
{ |
| 13017 |
// Vertex function in |
| 13018 |
case BuiltInVertexId: |
| 13019 |
return "uint"; |
| 13020 |
case BuiltInVertexIndex: |
| 13021 |
return "uint"; |
| 13022 |
case BuiltInBaseVertex: |
| 13023 |
return "uint"; |
| 13024 |
case BuiltInInstanceId: |
| 13025 |
return "uint"; |
| 13026 |
case BuiltInInstanceIndex: |
| 13027 |
return "uint"; |
| 13028 |
case BuiltInBaseInstance: |
| 13029 |
return "uint"; |
| 13030 |
case BuiltInDrawIndex: |
| 13031 |
SPIRV_CROSS_THROW("DrawIndex is not supported in MSL."); |
| 13032 |
|
| 13033 |
// Vertex function out |
| 13034 |
case BuiltInClipDistance: |
| 13035 |
return "float"; |
| 13036 |
case BuiltInPointSize: |
| 13037 |
return "float"; |
| 13038 |
case BuiltInPosition: |
| 13039 |
return "float4"; |
| 13040 |
case BuiltInLayer: |
| 13041 |
return "uint"; |
| 13042 |
case BuiltInViewportIndex: |
| 13043 |
if (!msl_options.supports_msl_version(2, 0)) |
| 13044 |
SPIRV_CROSS_THROW("ViewportIndex requires Metal 2.0."); |
| 13045 |
return "uint"; |
| 13046 |
|
| 13047 |
// Tess. control function in |
| 13048 |
case BuiltInInvocationId: |
| 13049 |
return "uint"; |
| 13050 |
case BuiltInPatchVertices: |
| 13051 |
return "uint"; |
| 13052 |
case BuiltInPrimitiveId: |
| 13053 |
return "uint"; |
| 13054 |
|
| 13055 |
// Tess. control function out |
| 13056 |
case BuiltInTessLevelInner: |
| 13057 |
if (execution.model == ExecutionModelTessellationEvaluation) |
| 13058 |
return !execution.flags.get(ExecutionModeTriangles) ? "float2" : "float"; |
| 13059 |
return "half"; |
| 13060 |
case BuiltInTessLevelOuter: |
| 13061 |
if (execution.model == ExecutionModelTessellationEvaluation) |
| 13062 |
return !execution.flags.get(ExecutionModeTriangles) ? "float4" : "float"; |
| 13063 |
return "half"; |
| 13064 |
|
| 13065 |
// Tess. evaluation function in |
| 13066 |
case BuiltInTessCoord: |
| 13067 |
return execution.flags.get(ExecutionModeTriangles) ? "float3" : "float2"; |
| 13068 |
|
| 13069 |
// Fragment function in |
| 13070 |
case BuiltInFrontFacing: |
| 13071 |
return "bool"; |
| 13072 |
case BuiltInPointCoord: |
| 13073 |
return "float2"; |
| 13074 |
case BuiltInFragCoord: |
| 13075 |
return "float4"; |
| 13076 |
case BuiltInSampleId: |
| 13077 |
return "uint"; |
| 13078 |
case BuiltInSampleMask: |
| 13079 |
return "uint"; |
| 13080 |
case BuiltInSamplePosition: |
| 13081 |
return "float2"; |
| 13082 |
case BuiltInViewIndex: |
| 13083 |
return "uint"; |
| 13084 |
|
| 13085 |
case BuiltInHelperInvocation: |
| 13086 |
return "bool"; |
| 13087 |
|
| 13088 |
case BuiltInBaryCoordNV: |
| 13089 |
case BuiltInBaryCoordNoPerspNV: |
| 13090 |
// Use the type as declared, can be 1, 2 or 3 components. |
| 13091 |
return type_to_glsl(get_variable_data_type(get<SPIRVariable>(id))); |
| 13092 |
|
| 13093 |
// Fragment function out |
| 13094 |
case BuiltInFragDepth: |
| 13095 |
return "float"; |
| 13096 |
|
| 13097 |
case BuiltInFragStencilRefEXT: |
| 13098 |
return "uint"; |
| 13099 |
|
| 13100 |
// Compute function in |
| 13101 |
case BuiltInGlobalInvocationId: |
| 13102 |
case BuiltInLocalInvocationId: |
| 13103 |
case BuiltInNumWorkgroups: |
| 13104 |
case BuiltInWorkgroupId: |
| 13105 |
return "uint3"; |
| 13106 |
case BuiltInLocalInvocationIndex: |
| 13107 |
case BuiltInNumSubgroups: |
| 13108 |
case BuiltInSubgroupId: |
| 13109 |
case BuiltInSubgroupSize: |
| 13110 |
case BuiltInSubgroupLocalInvocationId: |
| 13111 |
return "uint"; |
| 13112 |
case BuiltInSubgroupEqMask: |
| 13113 |
case BuiltInSubgroupGeMask: |
| 13114 |
case BuiltInSubgroupGtMask: |
| 13115 |
case BuiltInSubgroupLeMask: |
| 13116 |
case BuiltInSubgroupLtMask: |
| 13117 |
return "uint4"; |
| 13118 |
|
| 13119 |
case BuiltInDeviceIndex: |
| 13120 |
return "int"; |
| 13121 |
|
| 13122 |
default: |
| 13123 |
return "unsupported-built-in-type"; |
| 13124 |
} |
| 13125 |
} |
| 13126 |
|
| 13127 |
// Returns the declaration of a built-in argument to a function |
| 13128 |
string CompilerMSL::built_in_func_arg(BuiltIn builtin, bool prefix_comma) |
| 13129 |
{ |
| 13130 |
string bi_arg; |
| 13131 |
if (prefix_comma) |
| 13132 |
bi_arg += ", "; |
| 13133 |
|
| 13134 |
// Handle HLSL-style 0-based vertex/instance index. |
| 13135 |
builtin_declaration = true; |
| 13136 |
bi_arg += builtin_type_decl(builtin); |
| 13137 |
bi_arg += " " + builtin_to_glsl(builtin, StorageClassInput); |
| 13138 |
bi_arg += " [[" + builtin_qualifier(builtin) + "]]"; |
| 13139 |
builtin_declaration = false; |
| 13140 |
|
| 13141 |
return bi_arg; |
| 13142 |
} |
| 13143 |
|
| 13144 |
const SPIRType &CompilerMSL::get_physical_member_type(const SPIRType &type, uint32_t index) const |
| 13145 |
{ |
| 13146 |
if (member_is_remapped_physical_type(type, index)) |
| 13147 |
return get<SPIRType>(get_extended_member_decoration(type.self, index, SPIRVCrossDecorationPhysicalTypeID)); |
| 13148 |
else |
| 13149 |
return get<SPIRType>(type.member_types[index]); |
| 13150 |
} |
| 13151 |
|
| 13152 |
SPIRType CompilerMSL::get_presumed_input_type(const SPIRType &ib_type, uint32_t index) const |
| 13153 |
{ |
| 13154 |
SPIRType type = get_physical_member_type(ib_type, index); |
| 13155 |
uint32_t loc = get_member_decoration(ib_type.self, index, DecorationLocation); |
| 13156 |
if (inputs_by_location.count(loc)) |
| 13157 |
{ |
| 13158 |
if (inputs_by_location.at(loc).vecsize > type.vecsize) |
| 13159 |
type.vecsize = inputs_by_location.at(loc).vecsize; |
| 13160 |
} |
| 13161 |
return type; |
| 13162 |
} |
| 13163 |
|
| 13164 |
uint32_t CompilerMSL::get_declared_type_array_stride_msl(const SPIRType &type, bool is_packed, bool row_major) const |
| 13165 |
{ |
| 13166 |
// Array stride in MSL is always size * array_size. sizeof(float3) == 16, |
| 13167 |
// unlike GLSL and HLSL where array stride would be 16 and size 12. |
| 13168 |
|
| 13169 |
// We could use parent type here and recurse, but that makes creating physical type remappings |
| 13170 |
// far more complicated. We'd rather just create the final type, and ignore having to create the entire type |
| 13171 |
// hierarchy in order to compute this value, so make a temporary type on the stack. |
| 13172 |
|
| 13173 |
auto basic_type = type; |
| 13174 |
basic_type.array.clear(); |
| 13175 |
basic_type.array_size_literal.clear(); |
| 13176 |
uint32_t value_size = get_declared_type_size_msl(basic_type, is_packed, row_major); |
| 13177 |
|
| 13178 |
uint32_t dimensions = uint32_t(type.array.size()); |
| 13179 |
assert(dimensions > 0); |
| 13180 |
dimensions--; |
| 13181 |
|
| 13182 |
// Multiply together every dimension, except the last one. |
| 13183 |
for (uint32_t dim = 0; dim < dimensions; dim++) |
| 13184 |
{ |
| 13185 |
uint32_t array_size = to_array_size_literal(type, dim); |
| 13186 |
value_size *= max(array_size, 1u); |
| 13187 |
} |
| 13188 |
|
| 13189 |
return value_size; |
| 13190 |
} |
| 13191 |
|
| 13192 |
uint32_t CompilerMSL::get_declared_struct_member_array_stride_msl(const SPIRType &type, uint32_t index) const |
| 13193 |
{ |
| 13194 |
return get_declared_type_array_stride_msl(get_physical_member_type(type, index), |
| 13195 |
member_is_packed_physical_type(type, index), |
| 13196 |
has_member_decoration(type.self, index, DecorationRowMajor)); |
| 13197 |
} |
| 13198 |
|
| 13199 |
uint32_t CompilerMSL::get_declared_input_array_stride_msl(const SPIRType &type, uint32_t index) const |
| 13200 |
{ |
| 13201 |
return get_declared_type_array_stride_msl(get_presumed_input_type(type, index), false, |
| 13202 |
has_member_decoration(type.self, index, DecorationRowMajor)); |
| 13203 |
} |
| 13204 |
|
| 13205 |
uint32_t CompilerMSL::get_declared_type_matrix_stride_msl(const SPIRType &type, bool packed, bool row_major) const |
| 13206 |
{ |
| 13207 |
// For packed matrices, we just use the size of the vector type. |
| 13208 |
// Otherwise, MatrixStride == alignment, which is the size of the underlying vector type. |
| 13209 |
if (packed) |
| 13210 |
return (type.width / 8) * ((row_major && type.columns > 1) ? type.columns : type.vecsize); |
| 13211 |
else |
| 13212 |
return get_declared_type_alignment_msl(type, false, row_major); |
| 13213 |
} |
| 13214 |
|
| 13215 |
uint32_t CompilerMSL::get_declared_struct_member_matrix_stride_msl(const SPIRType &type, uint32_t index) const |
| 13216 |
{ |
| 13217 |
return get_declared_type_matrix_stride_msl(get_physical_member_type(type, index), |
| 13218 |
member_is_packed_physical_type(type, index), |
| 13219 |
has_member_decoration(type.self, index, DecorationRowMajor)); |
| 13220 |
} |
| 13221 |
|
| 13222 |
uint32_t CompilerMSL::get_declared_input_matrix_stride_msl(const SPIRType &type, uint32_t index) const |
| 13223 |
{ |
| 13224 |
return get_declared_type_matrix_stride_msl(get_presumed_input_type(type, index), false, |
| 13225 |
has_member_decoration(type.self, index, DecorationRowMajor)); |
| 13226 |
} |
| 13227 |
|
| 13228 |
uint32_t CompilerMSL::get_declared_struct_size_msl(const SPIRType &struct_type, bool ignore_alignment, |
| 13229 |
bool ignore_padding) const |
| 13230 |
{ |
| 13231 |
// If we have a target size, that is the declared size as well. |
| 13232 |
if (!ignore_padding && has_extended_decoration(struct_type.self, SPIRVCrossDecorationPaddingTarget)) |
| 13233 |
return get_extended_decoration(struct_type.self, SPIRVCrossDecorationPaddingTarget); |
| 13234 |
|
| 13235 |
if (struct_type.member_types.empty()) |
| 13236 |
return 0; |
| 13237 |
|
| 13238 |
uint32_t mbr_cnt = uint32_t(struct_type.member_types.size()); |
| 13239 |
|
| 13240 |
// In MSL, a struct's alignment is equal to the maximum alignment of any of its members. |
| 13241 |
uint32_t alignment = 1; |
| 13242 |
|
| 13243 |
if (!ignore_alignment) |
| 13244 |
{ |
| 13245 |
for (uint32_t i = 0; i < mbr_cnt; i++) |
| 13246 |
{ |
| 13247 |
uint32_t mbr_alignment = get_declared_struct_member_alignment_msl(struct_type, i); |
| 13248 |
alignment = max(alignment, mbr_alignment); |
| 13249 |
} |
| 13250 |
} |
| 13251 |
|
| 13252 |
// Last member will always be matched to the final Offset decoration, but size of struct in MSL now depends |
| 13253 |
// on physical size in MSL, and the size of the struct itself is then aligned to struct alignment. |
| 13254 |
uint32_t spirv_offset = type_struct_member_offset(struct_type, mbr_cnt - 1); |
| 13255 |
uint32_t msl_size = spirv_offset + get_declared_struct_member_size_msl(struct_type, mbr_cnt - 1); |
| 13256 |
msl_size = (msl_size + alignment - 1) & ~(alignment - 1); |
| 13257 |
return msl_size; |
| 13258 |
} |
| 13259 |
|
| 13260 |
// Returns the byte size of a struct member. |
| 13261 |
uint32_t CompilerMSL::get_declared_type_size_msl(const SPIRType &type, bool is_packed, bool row_major) const |
| 13262 |
{ |
| 13263 |
switch (type.basetype) |
| 13264 |
{ |
| 13265 |
case SPIRType::Unknown: |
| 13266 |
case SPIRType::Void: |
| 13267 |
case SPIRType::AtomicCounter: |
| 13268 |
case SPIRType::Image: |
| 13269 |
case SPIRType::SampledImage: |
| 13270 |
case SPIRType::Sampler: |
| 13271 |
SPIRV_CROSS_THROW("Querying size of opaque object."); |
| 13272 |
|
| 13273 |
default: |
| 13274 |
{ |
| 13275 |
if (!type.array.empty()) |
| 13276 |
{ |
| 13277 |
uint32_t array_size = to_array_size_literal(type); |
| 13278 |
return get_declared_type_array_stride_msl(type, is_packed, row_major) * max(array_size, 1u); |
| 13279 |
} |
| 13280 |
|
| 13281 |
if (type.basetype == SPIRType::Struct) |
| 13282 |
return get_declared_struct_size_msl(type); |
| 13283 |
|
| 13284 |
if (is_packed) |
| 13285 |
{ |
| 13286 |
return type.vecsize * type.columns * (type.width / 8); |
| 13287 |
} |
| 13288 |
else |
| 13289 |
{ |
| 13290 |
// An unpacked 3-element vector or matrix column is the same memory size as a 4-element. |
| 13291 |
uint32_t vecsize = type.vecsize; |
| 13292 |
uint32_t columns = type.columns; |
| 13293 |
|
| 13294 |
if (row_major && columns > 1) |
| 13295 |
swap(vecsize, columns); |
| 13296 |
|
| 13297 |
if (vecsize == 3) |
| 13298 |
vecsize = 4; |
| 13299 |
|
| 13300 |
return vecsize * columns * (type.width / 8); |
| 13301 |
} |
| 13302 |
} |
| 13303 |
} |
| 13304 |
} |
| 13305 |
|
| 13306 |
uint32_t CompilerMSL::get_declared_struct_member_size_msl(const SPIRType &type, uint32_t index) const |
| 13307 |
{ |
| 13308 |
return get_declared_type_size_msl(get_physical_member_type(type, index), |
| 13309 |
member_is_packed_physical_type(type, index), |
| 13310 |
has_member_decoration(type.self, index, DecorationRowMajor)); |
| 13311 |
} |
| 13312 |
|
| 13313 |
uint32_t CompilerMSL::get_declared_input_size_msl(const SPIRType &type, uint32_t index) const |
| 13314 |
{ |
| 13315 |
return get_declared_type_size_msl(get_presumed_input_type(type, index), false, |
| 13316 |
has_member_decoration(type.self, index, DecorationRowMajor)); |
| 13317 |
} |
| 13318 |
|
| 13319 |
// Returns the byte alignment of a type. |
| 13320 |
uint32_t CompilerMSL::get_declared_type_alignment_msl(const SPIRType &type, bool is_packed, bool row_major) const |
| 13321 |
{ |
| 13322 |
switch (type.basetype) |
| 13323 |
{ |
| 13324 |
case SPIRType::Unknown: |
| 13325 |
case SPIRType::Void: |
| 13326 |
case SPIRType::AtomicCounter: |
| 13327 |
case SPIRType::Image: |
| 13328 |
case SPIRType::SampledImage: |
| 13329 |
case SPIRType::Sampler: |
| 13330 |
SPIRV_CROSS_THROW("Querying alignment of opaque object."); |
| 13331 |
|
| 13332 |
case SPIRType::Int64: |
| 13333 |
SPIRV_CROSS_THROW("long types are not supported in buffers in MSL."); |
| 13334 |
case SPIRType::UInt64: |
| 13335 |
SPIRV_CROSS_THROW("ulong types are not supported in buffers in MSL."); |
| 13336 |
case SPIRType::Double: |
| 13337 |
SPIRV_CROSS_THROW("double types are not supported in buffers in MSL."); |
| 13338 |
|
| 13339 |
case SPIRType::Struct: |
| 13340 |
{ |
| 13341 |
// In MSL, a struct's alignment is equal to the maximum alignment of any of its members. |
| 13342 |
uint32_t alignment = 1; |
| 13343 |
for (uint32_t i = 0; i < type.member_types.size(); i++) |
| 13344 |
alignment = max(alignment, uint32_t(get_declared_struct_member_alignment_msl(type, i))); |
| 13345 |
return alignment; |
| 13346 |
} |
| 13347 |
|
| 13348 |
default: |
| 13349 |
{ |
| 13350 |
// Alignment of packed type is the same as the underlying component or column size. |
| 13351 |
// Alignment of unpacked type is the same as the vector size. |
| 13352 |
// Alignment of 3-elements vector is the same as 4-elements (including packed using column). |
| 13353 |
if (is_packed) |
| 13354 |
{ |
| 13355 |
// If we have packed_T and friends, the alignment is always scalar. |
| 13356 |
return type.width / 8; |
| 13357 |
} |
| 13358 |
else |
| 13359 |
{ |
| 13360 |
// This is the general rule for MSL. Size == alignment. |
| 13361 |
uint32_t vecsize = (row_major && type.columns > 1) ? type.columns : type.vecsize; |
| 13362 |
return (type.width / 8) * (vecsize == 3 ? 4 : vecsize); |
| 13363 |
} |
| 13364 |
} |
| 13365 |
} |
| 13366 |
} |
| 13367 |
|
| 13368 |
uint32_t CompilerMSL::get_declared_struct_member_alignment_msl(const SPIRType &type, uint32_t index) const |
| 13369 |
{ |
| 13370 |
return get_declared_type_alignment_msl(get_physical_member_type(type, index), |
| 13371 |
member_is_packed_physical_type(type, index), |
| 13372 |
has_member_decoration(type.self, index, DecorationRowMajor)); |
| 13373 |
} |
| 13374 |
|
| 13375 |
uint32_t CompilerMSL::get_declared_input_alignment_msl(const SPIRType &type, uint32_t index) const |
| 13376 |
{ |
| 13377 |
return get_declared_type_alignment_msl(get_presumed_input_type(type, index), false, |
| 13378 |
has_member_decoration(type.self, index, DecorationRowMajor)); |
| 13379 |
} |
| 13380 |
|
| 13381 |
bool CompilerMSL::skip_argument(uint32_t) const |
| 13382 |
{ |
| 13383 |
return false; |
| 13384 |
} |
| 13385 |
|
| 13386 |
void CompilerMSL::analyze_sampled_image_usage() |
| 13387 |
{ |
| 13388 |
if (msl_options.swizzle_texture_samples) |
| 13389 |
{ |
| 13390 |
SampledImageScanner scanner(*this); |
| 13391 |
traverse_all_reachable_opcodes(get<SPIRFunction>(ir.default_entry_point), scanner); |
| 13392 |
} |
| 13393 |
} |
| 13394 |
|
| 13395 |
bool CompilerMSL::SampledImageScanner::handle(spv::Op opcode, const uint32_t *args, uint32_t length) |
| 13396 |
{ |
| 13397 |
switch (opcode) |
| 13398 |
{ |
| 13399 |
case OpLoad: |
| 13400 |
case OpImage: |
| 13401 |
case OpSampledImage: |
| 13402 |
{ |
| 13403 |
if (length < 3) |
| 13404 |
return false; |
| 13405 |
|
| 13406 |
uint32_t result_type = args[0]; |
| 13407 |
auto &type = compiler.get<SPIRType>(result_type); |
| 13408 |
if ((type.basetype != SPIRType::Image && type.basetype != SPIRType::SampledImage) || type.image.sampled != 1) |
| 13409 |
return true; |
| 13410 |
|
| 13411 |
uint32_t id = args[1]; |
| 13412 |
compiler.set<SPIRExpression>(id, "", result_type, true); |
| 13413 |
break; |
| 13414 |
} |
| 13415 |
case OpImageSampleExplicitLod: |
| 13416 |
case OpImageSampleProjExplicitLod: |
| 13417 |
case OpImageSampleDrefExplicitLod: |
| 13418 |
case OpImageSampleProjDrefExplicitLod: |
| 13419 |
case OpImageSampleImplicitLod: |
| 13420 |
case OpImageSampleProjImplicitLod: |
| 13421 |
case OpImageSampleDrefImplicitLod: |
| 13422 |
case OpImageSampleProjDrefImplicitLod: |
| 13423 |
case OpImageFetch: |
| 13424 |
case OpImageGather: |
| 13425 |
case OpImageDrefGather: |
| 13426 |
compiler.has_sampled_images = |
| 13427 |
compiler.has_sampled_images || compiler.is_sampled_image_type(compiler.expression_type(args[2])); |
| 13428 |
compiler.needs_swizzle_buffer_def = compiler.needs_swizzle_buffer_def || compiler.has_sampled_images; |
| 13429 |
break; |
| 13430 |
default: |
| 13431 |
break; |
| 13432 |
} |
| 13433 |
return true; |
| 13434 |
} |
| 13435 |
|
| 13436 |
// If a needed custom function wasn't added before, add it and force a recompile. |
| 13437 |
void CompilerMSL::add_spv_func_and_recompile(SPVFuncImpl spv_func) |
| 13438 |
{ |
| 13439 |
if (spv_function_implementations.count(spv_func) == 0) |
| 13440 |
{ |
| 13441 |
spv_function_implementations.insert(spv_func); |
| 13442 |
suppress_missing_prototypes = true; |
| 13443 |
force_recompile(); |
| 13444 |
} |
| 13445 |
} |
| 13446 |
|
| 13447 |
bool CompilerMSL::OpCodePreprocessor::handle(Op opcode, const uint32_t *args, uint32_t length) |
| 13448 |
{ |
| 13449 |
// Since MSL exists in a single execution scope, function prototype declarations are not |
| 13450 |
// needed, and clutter the output. If secondary functions are output (either as a SPIR-V |
| 13451 |
// function implementation or as indicated by the presence of OpFunctionCall), then set |
| 13452 |
// suppress_missing_prototypes to suppress compiler warnings of missing function prototypes. |
| 13453 |
|
| 13454 |
// Mark if the input requires the implementation of an SPIR-V function that does not exist in Metal. |
| 13455 |
SPVFuncImpl spv_func = get_spv_func_impl(opcode, args); |
| 13456 |
if (spv_func != SPVFuncImplNone) |
| 13457 |
{ |
| 13458 |
compiler.spv_function_implementations.insert(spv_func); |
| 13459 |
suppress_missing_prototypes = true; |
| 13460 |
} |
| 13461 |
|
| 13462 |
switch (opcode) |
| 13463 |
{ |
| 13464 |
|
| 13465 |
case OpFunctionCall: |
| 13466 |
suppress_missing_prototypes = true; |
| 13467 |
break; |
| 13468 |
|
| 13469 |
// Emulate texture2D atomic operations |
| 13470 |
case OpImageTexelPointer: |
| 13471 |
{ |
| 13472 |
auto *var = compiler.maybe_get_backing_variable(args[2]); |
| 13473 |
image_pointers[args[1]] = var ? var->self : ID(0); |
| 13474 |
break; |
| 13475 |
} |
| 13476 |
|
| 13477 |
case OpImageWrite: |
| 13478 |
if (!compiler.msl_options.supports_msl_version(2, 2)) |
| 13479 |
uses_resource_write = true; |
| 13480 |
break; |
| 13481 |
|
| 13482 |
case OpStore: |
| 13483 |
check_resource_write(args[0]); |
| 13484 |
break; |
| 13485 |
|
| 13486 |
// Emulate texture2D atomic operations |
| 13487 |
case OpAtomicExchange: |
| 13488 |
case OpAtomicCompareExchange: |
| 13489 |
case OpAtomicCompareExchangeWeak: |
| 13490 |
case OpAtomicIIncrement: |
| 13491 |
case OpAtomicIDecrement: |
| 13492 |
case OpAtomicIAdd: |
| 13493 |
case OpAtomicISub: |
| 13494 |
case OpAtomicSMin: |
| 13495 |
case OpAtomicUMin: |
| 13496 |
case OpAtomicSMax: |
| 13497 |
case OpAtomicUMax: |
| 13498 |
case OpAtomicAnd: |
| 13499 |
case OpAtomicOr: |
| 13500 |
case OpAtomicXor: |
| 13501 |
{ |
| 13502 |
uses_atomics = true; |
| 13503 |
auto it = image_pointers.find(args[2]); |
| 13504 |
if (it != image_pointers.end()) |
| 13505 |
{ |
| 13506 |
compiler.atomic_image_vars.insert(it->second); |
| 13507 |
} |
| 13508 |
check_resource_write(args[2]); |
| 13509 |
break; |
| 13510 |
} |
| 13511 |
|
| 13512 |
case OpAtomicStore: |
| 13513 |
{ |
| 13514 |
uses_atomics = true; |
| 13515 |
auto it = image_pointers.find(args[0]); |
| 13516 |
if (it != image_pointers.end()) |
| 13517 |
{ |
| 13518 |
compiler.atomic_image_vars.insert(it->second); |
| 13519 |
} |
| 13520 |
check_resource_write(args[0]); |
| 13521 |
break; |
| 13522 |
} |
| 13523 |
|
| 13524 |
case OpAtomicLoad: |
| 13525 |
{ |
| 13526 |
uses_atomics = true; |
| 13527 |
auto it = image_pointers.find(args[2]); |
| 13528 |
if (it != image_pointers.end()) |
| 13529 |
{ |
| 13530 |
compiler.atomic_image_vars.insert(it->second); |
| 13531 |
} |
| 13532 |
break; |
| 13533 |
} |
| 13534 |
|
| 13535 |
case OpGroupNonUniformInverseBallot: |
| 13536 |
needs_subgroup_invocation_id = true; |
| 13537 |
break; |
| 13538 |
|
| 13539 |
case OpGroupNonUniformBallotFindLSB: |
| 13540 |
case OpGroupNonUniformBallotFindMSB: |
| 13541 |
needs_subgroup_size = true; |
| 13542 |
break; |
| 13543 |
|
| 13544 |
case OpGroupNonUniformBallotBitCount: |
| 13545 |
if (args[3] == GroupOperationReduce) |
| 13546 |
needs_subgroup_size = true; |
| 13547 |
else |
| 13548 |
needs_subgroup_invocation_id = true; |
| 13549 |
break; |
| 13550 |
|
| 13551 |
case OpArrayLength: |
| 13552 |
{ |
| 13553 |
auto *var = compiler.maybe_get_backing_variable(args[2]); |
| 13554 |
if (var) |
| 13555 |
compiler.buffers_requiring_array_length.insert(var->self); |
| 13556 |
break; |
| 13557 |
} |
| 13558 |
|
| 13559 |
case OpInBoundsAccessChain: |
| 13560 |
case OpAccessChain: |
| 13561 |
case OpPtrAccessChain: |
| 13562 |
{ |
| 13563 |
// OpArrayLength might want to know if taking ArrayLength of an array of SSBOs. |
| 13564 |
uint32_t result_type = args[0]; |
| 13565 |
uint32_t id = args[1]; |
| 13566 |
uint32_t ptr = args[2]; |
| 13567 |
|
| 13568 |
compiler.set<SPIRExpression>(id, "", result_type, true); |
| 13569 |
compiler.register_read(id, ptr, true); |
| 13570 |
compiler.ir.ids[id].set_allow_type_rewrite(); |
| 13571 |
break; |
| 13572 |
} |
| 13573 |
|
| 13574 |
case OpExtInst: |
| 13575 |
{ |
| 13576 |
uint32_t extension_set = args[2]; |
| 13577 |
if (compiler.get<SPIRExtension>(extension_set).ext == SPIRExtension::GLSL) |
| 13578 |
{ |
| 13579 |
auto op_450 = static_cast<GLSLstd450>(args[3]); |
| 13580 |
switch (op_450) |
| 13581 |
{ |
| 13582 |
case GLSLstd450InterpolateAtCentroid: |
| 13583 |
case GLSLstd450InterpolateAtSample: |
| 13584 |
case GLSLstd450InterpolateAtOffset: |
| 13585 |
{ |
| 13586 |
if (!compiler.msl_options.supports_msl_version(2, 3)) |
| 13587 |
SPIRV_CROSS_THROW("Pull-model interpolation requires MSL 2.3."); |
| 13588 |
// Fragment varyings used with pull-model interpolation need special handling, |
| 13589 |
// due to the way pull-model interpolation works in Metal. |
| 13590 |
auto *var = compiler.maybe_get_backing_variable(args[4]); |
| 13591 |
if (var) |
| 13592 |
{ |
| 13593 |
compiler.pull_model_inputs.insert(var->self); |
| 13594 |
auto &var_type = compiler.get_variable_element_type(*var); |
| 13595 |
// In addition, if this variable has a 'Sample' decoration, we need the sample ID |
| 13596 |
// in order to do default interpolation. |
| 13597 |
if (compiler.has_decoration(var->self, DecorationSample)) |
| 13598 |
{ |
| 13599 |
needs_sample_id = true; |
| 13600 |
} |
| 13601 |
else if (var_type.basetype == SPIRType::Struct) |
| 13602 |
{ |
| 13603 |
// Now we need to check each member and see if it has this decoration. |
| 13604 |
for (uint32_t i = 0; i < var_type.member_types.size(); ++i) |
| 13605 |
{ |
| 13606 |
if (compiler.has_member_decoration(var_type.self, i, DecorationSample)) |
| 13607 |
{ |
| 13608 |
needs_sample_id = true; |
| 13609 |
break; |
| 13610 |
} |
| 13611 |
} |
| 13612 |
} |
| 13613 |
} |
| 13614 |
break; |
| 13615 |
} |
| 13616 |
default: |
| 13617 |
break; |
| 13618 |
} |
| 13619 |
} |
| 13620 |
break; |
| 13621 |
} |
| 13622 |
|
| 13623 |
default: |
| 13624 |
break; |
| 13625 |
} |
| 13626 |
|
| 13627 |
// If it has one, keep track of the instruction's result type, mapped by ID |
| 13628 |
uint32_t result_type, result_id; |
| 13629 |
if (compiler.instruction_to_result_type(result_type, result_id, opcode, args, length)) |
| 13630 |
result_types[result_id] = result_type; |
| 13631 |
|
| 13632 |
return true; |
| 13633 |
} |
| 13634 |
|
| 13635 |
// If the variable is a Uniform or StorageBuffer, mark that a resource has been written to. |
| 13636 |
void CompilerMSL::OpCodePreprocessor::check_resource_write(uint32_t var_id) |
| 13637 |
{ |
| 13638 |
auto *p_var = compiler.maybe_get_backing_variable(var_id); |
| 13639 |
StorageClass sc = p_var ? p_var->storage : StorageClassMax; |
| 13640 |
if (!compiler.msl_options.supports_msl_version(2, 1) && |
| 13641 |
(sc == StorageClassUniform || sc == StorageClassStorageBuffer)) |
| 13642 |
uses_resource_write = true; |
| 13643 |
} |
| 13644 |
|
| 13645 |
// Returns an enumeration of a SPIR-V function that needs to be output for certain Op codes. |
| 13646 |
CompilerMSL::SPVFuncImpl CompilerMSL::OpCodePreprocessor::get_spv_func_impl(Op opcode, const uint32_t *args) |
| 13647 |
{ |
| 13648 |
switch (opcode) |
| 13649 |
{ |
| 13650 |
case OpFMod: |
| 13651 |
return SPVFuncImplMod; |
| 13652 |
|
| 13653 |
case OpFAdd: |
| 13654 |
if (compiler.msl_options.invariant_float_math) |
| 13655 |
{ |
| 13656 |
return SPVFuncImplFAdd; |
| 13657 |
} |
| 13658 |
break; |
| 13659 |
|
| 13660 |
case OpFMul: |
| 13661 |
case OpOuterProduct: |
| 13662 |
case OpMatrixTimesVector: |
| 13663 |
case OpVectorTimesMatrix: |
| 13664 |
case OpMatrixTimesMatrix: |
| 13665 |
if (compiler.msl_options.invariant_float_math) |
| 13666 |
{ |
| 13667 |
return SPVFuncImplFMul; |
| 13668 |
} |
| 13669 |
break; |
| 13670 |
|
| 13671 |
case OpTypeArray: |
| 13672 |
{ |
| 13673 |
// Allow Metal to use the array<T> template to make arrays a value type |
| 13674 |
return SPVFuncImplUnsafeArray; |
| 13675 |
} |
| 13676 |
|
| 13677 |
// Emulate texture2D atomic operations |
| 13678 |
case OpAtomicExchange: |
| 13679 |
case OpAtomicCompareExchange: |
| 13680 |
case OpAtomicCompareExchangeWeak: |
| 13681 |
case OpAtomicIIncrement: |
| 13682 |
case OpAtomicIDecrement: |
| 13683 |
case OpAtomicIAdd: |
| 13684 |
case OpAtomicISub: |
| 13685 |
case OpAtomicSMin: |
| 13686 |
case OpAtomicUMin: |
| 13687 |
case OpAtomicSMax: |
| 13688 |
case OpAtomicUMax: |
| 13689 |
case OpAtomicAnd: |
| 13690 |
case OpAtomicOr: |
| 13691 |
case OpAtomicXor: |
| 13692 |
case OpAtomicLoad: |
| 13693 |
case OpAtomicStore: |
| 13694 |
{ |
| 13695 |
auto it = image_pointers.find(args[opcode == OpAtomicStore ? 0 : 2]); |
| 13696 |
if (it != image_pointers.end()) |
| 13697 |
{ |
| 13698 |
uint32_t tid = compiler.get<SPIRVariable>(it->second).basetype; |
| 13699 |
if (tid && compiler.get<SPIRType>(tid).image.dim == Dim2D) |
| 13700 |
return SPVFuncImplImage2DAtomicCoords; |
| 13701 |
} |
| 13702 |
break; |
| 13703 |
} |
| 13704 |
|
| 13705 |
case OpImageFetch: |
| 13706 |
case OpImageRead: |
| 13707 |
case OpImageWrite: |
| 13708 |
{ |
| 13709 |
// Retrieve the image type, and if it's a Buffer, emit a texel coordinate function |
| 13710 |
uint32_t tid = result_types[args[opcode == OpImageWrite ? 0 : 2]]; |
| 13711 |
if (tid && compiler.get<SPIRType>(tid).image.dim == DimBuffer && !compiler.msl_options.texture_buffer_native) |
| 13712 |
return SPVFuncImplTexelBufferCoords; |
| 13713 |
break; |
| 13714 |
} |
| 13715 |
|
| 13716 |
case OpExtInst: |
| 13717 |
{ |
| 13718 |
uint32_t extension_set = args[2]; |
| 13719 |
if (compiler.get<SPIRExtension>(extension_set).ext == SPIRExtension::GLSL) |
| 13720 |
{ |
| 13721 |
auto op_450 = static_cast<GLSLstd450>(args[3]); |
| 13722 |
switch (op_450) |
| 13723 |
{ |
| 13724 |
case GLSLstd450Radians: |
| 13725 |
return SPVFuncImplRadians; |
| 13726 |
case GLSLstd450Degrees: |
| 13727 |
return SPVFuncImplDegrees; |
| 13728 |
case GLSLstd450FindILsb: |
| 13729 |
return SPVFuncImplFindILsb; |
| 13730 |
case GLSLstd450FindSMsb: |
| 13731 |
return SPVFuncImplFindSMsb; |
| 13732 |
case GLSLstd450FindUMsb: |
| 13733 |
return SPVFuncImplFindUMsb; |
| 13734 |
case GLSLstd450SSign: |
| 13735 |
return SPVFuncImplSSign; |
| 13736 |
case GLSLstd450Reflect: |
| 13737 |
{ |
| 13738 |
auto &type = compiler.get<SPIRType>(args[0]); |
| 13739 |
if (type.vecsize == 1) |
| 13740 |
return SPVFuncImplReflectScalar; |
| 13741 |
break; |
| 13742 |
} |
| 13743 |
case GLSLstd450Refract: |
| 13744 |
{ |
| 13745 |
auto &type = compiler.get<SPIRType>(args[0]); |
| 13746 |
if (type.vecsize == 1) |
| 13747 |
return SPVFuncImplRefractScalar; |
| 13748 |
break; |
| 13749 |
} |
| 13750 |
case GLSLstd450FaceForward: |
| 13751 |
{ |
| 13752 |
auto &type = compiler.get<SPIRType>(args[0]); |
| 13753 |
if (type.vecsize == 1) |
| 13754 |
return SPVFuncImplFaceForwardScalar; |
| 13755 |
break; |
| 13756 |
} |
| 13757 |
case GLSLstd450MatrixInverse: |
| 13758 |
{ |
| 13759 |
auto &mat_type = compiler.get<SPIRType>(args[0]); |
| 13760 |
switch (mat_type.columns) |
| 13761 |
{ |
| 13762 |
case 2: |
| 13763 |
return SPVFuncImplInverse2x2; |
| 13764 |
case 3: |
| 13765 |
return SPVFuncImplInverse3x3; |
| 13766 |
case 4: |
| 13767 |
return SPVFuncImplInverse4x4; |
| 13768 |
default: |
| 13769 |
break; |
| 13770 |
} |
| 13771 |
break; |
| 13772 |
} |
| 13773 |
default: |
| 13774 |
break; |
| 13775 |
} |
| 13776 |
} |
| 13777 |
break; |
| 13778 |
} |
| 13779 |
|
| 13780 |
case OpGroupNonUniformBroadcast: |
| 13781 |
return SPVFuncImplSubgroupBroadcast; |
| 13782 |
|
| 13783 |
case OpGroupNonUniformBroadcastFirst: |
| 13784 |
return SPVFuncImplSubgroupBroadcastFirst; |
| 13785 |
|
| 13786 |
case OpGroupNonUniformBallot: |
| 13787 |
return SPVFuncImplSubgroupBallot; |
| 13788 |
|
| 13789 |
case OpGroupNonUniformInverseBallot: |
| 13790 |
case OpGroupNonUniformBallotBitExtract: |
| 13791 |
return SPVFuncImplSubgroupBallotBitExtract; |
| 13792 |
|
| 13793 |
case OpGroupNonUniformBallotFindLSB: |
| 13794 |
return SPVFuncImplSubgroupBallotFindLSB; |
| 13795 |
|
| 13796 |
case OpGroupNonUniformBallotFindMSB: |
| 13797 |
return SPVFuncImplSubgroupBallotFindMSB; |
| 13798 |
|
| 13799 |
case OpGroupNonUniformBallotBitCount: |
| 13800 |
return SPVFuncImplSubgroupBallotBitCount; |
| 13801 |
|
| 13802 |
case OpGroupNonUniformAllEqual: |
| 13803 |
return SPVFuncImplSubgroupAllEqual; |
| 13804 |
|
| 13805 |
case OpGroupNonUniformShuffle: |
| 13806 |
return SPVFuncImplSubgroupShuffle; |
| 13807 |
|
| 13808 |
case OpGroupNonUniformShuffleXor: |
| 13809 |
return SPVFuncImplSubgroupShuffleXor; |
| 13810 |
|
| 13811 |
case OpGroupNonUniformShuffleUp: |
| 13812 |
return SPVFuncImplSubgroupShuffleUp; |
| 13813 |
|
| 13814 |
case OpGroupNonUniformShuffleDown: |
| 13815 |
return SPVFuncImplSubgroupShuffleDown; |
| 13816 |
|
| 13817 |
case OpGroupNonUniformQuadBroadcast: |
| 13818 |
return SPVFuncImplQuadBroadcast; |
| 13819 |
|
| 13820 |
case OpGroupNonUniformQuadSwap: |
| 13821 |
return SPVFuncImplQuadSwap; |
| 13822 |
|
| 13823 |
default: |
| 13824 |
break; |
| 13825 |
} |
| 13826 |
return SPVFuncImplNone; |
| 13827 |
} |
| 13828 |
|
| 13829 |
// Sort both type and meta member content based on builtin status (put builtins at end), |
| 13830 |
// then by the required sorting aspect. |
| 13831 |
void CompilerMSL::MemberSorter::sort() |
| 13832 |
{ |
| 13833 |
// Create a temporary array of consecutive member indices and sort it based on how |
| 13834 |
// the members should be reordered, based on builtin and sorting aspect meta info. |
| 13835 |
size_t mbr_cnt = type.member_types.size(); |
| 13836 |
SmallVector<uint32_t> mbr_idxs(mbr_cnt); |
| 13837 |
std::iota(mbr_idxs.begin(), mbr_idxs.end(), 0); // Fill with consecutive indices |
| 13838 |
std::stable_sort(mbr_idxs.begin(), mbr_idxs.end(), *this); // Sort member indices based on sorting aspect |
| 13839 |
|
| 13840 |
bool sort_is_identity = true; |
| 13841 |
for (uint32_t mbr_idx = 0; mbr_idx < mbr_cnt; mbr_idx++) |
| 13842 |
{ |
| 13843 |
if (mbr_idx != mbr_idxs[mbr_idx]) |
| 13844 |
{ |
| 13845 |
sort_is_identity = false; |
| 13846 |
break; |
| 13847 |
} |
| 13848 |
} |
| 13849 |
|
| 13850 |
if (sort_is_identity) |
| 13851 |
return; |
| 13852 |
|
| 13853 |
if (meta.members.size() < type.member_types.size()) |
| 13854 |
{ |
| 13855 |
// This should never trigger in normal circumstances, but to be safe. |
| 13856 |
meta.members.resize(type.member_types.size()); |
| 13857 |
} |
| 13858 |
|
| 13859 |
// Move type and meta member info to the order defined by the sorted member indices. |
| 13860 |
// This is done by creating temporary copies of both member types and meta, and then |
| 13861 |
// copying back to the original content at the sorted indices. |
| 13862 |
auto mbr_types_cpy = type.member_types; |
| 13863 |
auto mbr_meta_cpy = meta.members; |
| 13864 |
for (uint32_t mbr_idx = 0; mbr_idx < mbr_cnt; mbr_idx++) |
| 13865 |
{ |
| 13866 |
type.member_types[mbr_idx] = mbr_types_cpy[mbr_idxs[mbr_idx]]; |
| 13867 |
meta.members[mbr_idx] = mbr_meta_cpy[mbr_idxs[mbr_idx]]; |
| 13868 |
} |
| 13869 |
|
| 13870 |
if (sort_aspect == SortAspect::Offset) |
| 13871 |
{ |
| 13872 |
// If we're sorting by Offset, this might affect user code which accesses a buffer block. |
| 13873 |
// We will need to redirect member indices from one index to sorted index. |
| 13874 |
type.member_type_index_redirection = std::move(mbr_idxs); |
| 13875 |
} |
| 13876 |
} |
| 13877 |
|
| 13878 |
// Sort first by builtin status (put builtins at end), then by the sorting aspect. |
| 13879 |
bool CompilerMSL::MemberSorter::operator()(uint32_t mbr_idx1, uint32_t mbr_idx2) |
| 13880 |
{ |
| 13881 |
auto &mbr_meta1 = meta.members[mbr_idx1]; |
| 13882 |
auto &mbr_meta2 = meta.members[mbr_idx2]; |
| 13883 |
if (mbr_meta1.builtin != mbr_meta2.builtin) |
| 13884 |
return mbr_meta2.builtin; |
| 13885 |
else |
| 13886 |
switch (sort_aspect) |
| 13887 |
{ |
| 13888 |
case Location: |
| 13889 |
return mbr_meta1.location < mbr_meta2.location; |
| 13890 |
case LocationReverse: |
| 13891 |
return mbr_meta1.location > mbr_meta2.location; |
| 13892 |
case Offset: |
| 13893 |
return mbr_meta1.offset < mbr_meta2.offset; |
| 13894 |
case OffsetThenLocationReverse: |
| 13895 |
return (mbr_meta1.offset < mbr_meta2.offset) || |
| 13896 |
((mbr_meta1.offset == mbr_meta2.offset) && (mbr_meta1.location > mbr_meta2.location)); |
| 13897 |
case Alphabetical: |
| 13898 |
return mbr_meta1.alias < mbr_meta2.alias; |
| 13899 |
default: |
| 13900 |
return false; |
| 13901 |
} |
| 13902 |
} |
| 13903 |
|
| 13904 |
CompilerMSL::MemberSorter::MemberSorter(SPIRType &t, Meta &m, SortAspect sa) |
| 13905 |
: type(t) |
| 13906 |
, meta(m) |
| 13907 |
, sort_aspect(sa) |
| 13908 |
{ |
| 13909 |
// Ensure enough meta info is available |
| 13910 |
meta.members.resize(max(type.member_types.size(), meta.members.size())); |
| 13911 |
} |
| 13912 |
|
| 13913 |
void CompilerMSL::remap_constexpr_sampler(VariableID id, const MSLConstexprSampler &sampler) |
| 13914 |
{ |
| 13915 |
auto &type = get<SPIRType>(get<SPIRVariable>(id).basetype); |
| 13916 |
if (type.basetype != SPIRType::SampledImage && type.basetype != SPIRType::Sampler) |
| 13917 |
SPIRV_CROSS_THROW("Can only remap SampledImage and Sampler type."); |
| 13918 |
if (!type.array.empty()) |
| 13919 |
SPIRV_CROSS_THROW("Can not remap array of samplers."); |
| 13920 |
constexpr_samplers_by_id[id] = sampler; |
| 13921 |
} |
| 13922 |
|
| 13923 |
void CompilerMSL::remap_constexpr_sampler_by_binding(uint32_t desc_set, uint32_t binding, |
| 13924 |
const MSLConstexprSampler &sampler) |
| 13925 |
{ |
| 13926 |
constexpr_samplers_by_binding[{ desc_set, binding }] = sampler; |
| 13927 |
} |
| 13928 |
|
| 13929 |
void CompilerMSL::cast_from_builtin_load(uint32_t source_id, std::string &expr, const SPIRType &expr_type) |
| 13930 |
{ |
| 13931 |
auto *var = maybe_get_backing_variable(source_id); |
| 13932 |
if (var) |
| 13933 |
source_id = var->self; |
| 13934 |
|
| 13935 |
// Only interested in standalone builtin variables. |
| 13936 |
if (!has_decoration(source_id, DecorationBuiltIn)) |
| 13937 |
return; |
| 13938 |
|
| 13939 |
auto builtin = static_cast<BuiltIn>(get_decoration(source_id, DecorationBuiltIn)); |
| 13940 |
auto expected_type = expr_type.basetype; |
| 13941 |
auto expected_width = expr_type.width; |
| 13942 |
switch (builtin) |
| 13943 |
{ |
| 13944 |
case BuiltInGlobalInvocationId: |
| 13945 |
case BuiltInLocalInvocationId: |
| 13946 |
case BuiltInWorkgroupId: |
| 13947 |
case BuiltInLocalInvocationIndex: |
| 13948 |
case BuiltInWorkgroupSize: |
| 13949 |
case BuiltInNumWorkgroups: |
| 13950 |
case BuiltInLayer: |
| 13951 |
case BuiltInViewportIndex: |
| 13952 |
case BuiltInFragStencilRefEXT: |
| 13953 |
case BuiltInPrimitiveId: |
| 13954 |
case BuiltInSubgroupSize: |
| 13955 |
case BuiltInSubgroupLocalInvocationId: |
| 13956 |
case BuiltInViewIndex: |
| 13957 |
case BuiltInVertexIndex: |
| 13958 |
case BuiltInInstanceIndex: |
| 13959 |
case BuiltInBaseInstance: |
| 13960 |
case BuiltInBaseVertex: |
| 13961 |
expected_type = SPIRType::UInt; |
| 13962 |
expected_width = 32; |
| 13963 |
break; |
| 13964 |
|
| 13965 |
case BuiltInTessLevelInner: |
| 13966 |
case BuiltInTessLevelOuter: |
| 13967 |
if (get_execution_model() == ExecutionModelTessellationControl) |
| 13968 |
{ |
| 13969 |
expected_type = SPIRType::Half; |
| 13970 |
expected_width = 16; |
| 13971 |
} |
| 13972 |
break; |
| 13973 |
|
| 13974 |
default: |
| 13975 |
break; |
| 13976 |
} |
| 13977 |
|
| 13978 |
if (expected_type != expr_type.basetype) |
| 13979 |
{ |
| 13980 |
if (expected_width != expr_type.width) |
| 13981 |
{ |
| 13982 |
// These are of different widths, so we cannot do a straight bitcast. |
| 13983 |
expr = join(type_to_glsl(expr_type), "(", expr, ")"); |
| 13984 |
} |
| 13985 |
else |
| 13986 |
{ |
| 13987 |
expr = bitcast_expression(expr_type, expected_type, expr); |
| 13988 |
} |
| 13989 |
} |
| 13990 |
|
| 13991 |
if (builtin == BuiltInTessCoord && get_entry_point().flags.get(ExecutionModeQuads) && expr_type.vecsize == 3) |
| 13992 |
{ |
| 13993 |
// In SPIR-V, this is always a vec3, even for quads. In Metal, though, it's a float2 for quads. |
| 13994 |
// The code is expecting a float3, so we need to widen this. |
| 13995 |
expr = join("float3(", expr, ", 0)"); |
| 13996 |
} |
| 13997 |
} |
| 13998 |
|
| 13999 |
void CompilerMSL::cast_to_builtin_store(uint32_t target_id, std::string &expr, const SPIRType &expr_type) |
| 14000 |
{ |
| 14001 |
auto *var = maybe_get_backing_variable(target_id); |
| 14002 |
if (var) |
| 14003 |
target_id = var->self; |
| 14004 |
|
| 14005 |
// Only interested in standalone builtin variables. |
| 14006 |
if (!has_decoration(target_id, DecorationBuiltIn)) |
| 14007 |
return; |
| 14008 |
|
| 14009 |
auto builtin = static_cast<BuiltIn>(get_decoration(target_id, DecorationBuiltIn)); |
| 14010 |
auto expected_type = expr_type.basetype; |
| 14011 |
auto expected_width = expr_type.width; |
| 14012 |
switch (builtin) |
| 14013 |
{ |
| 14014 |
case BuiltInLayer: |
| 14015 |
case BuiltInViewportIndex: |
| 14016 |
case BuiltInFragStencilRefEXT: |
| 14017 |
case BuiltInPrimitiveId: |
| 14018 |
case BuiltInViewIndex: |
| 14019 |
expected_type = SPIRType::UInt; |
| 14020 |
expected_width = 32; |
| 14021 |
break; |
| 14022 |
|
| 14023 |
case BuiltInTessLevelInner: |
| 14024 |
case BuiltInTessLevelOuter: |
| 14025 |
expected_type = SPIRType::Half; |
| 14026 |
expected_width = 16; |
| 14027 |
break; |
| 14028 |
|
| 14029 |
default: |
| 14030 |
break; |
| 14031 |
} |
| 14032 |
|
| 14033 |
if (expected_type != expr_type.basetype) |
| 14034 |
{ |
| 14035 |
if (expected_width != expr_type.width) |
| 14036 |
{ |
| 14037 |
// These are of different widths, so we cannot do a straight bitcast. |
| 14038 |
auto type = expr_type; |
| 14039 |
type.basetype = expected_type; |
| 14040 |
type.width = expected_width; |
| 14041 |
expr = join(type_to_glsl(type), "(", expr, ")"); |
| 14042 |
} |
| 14043 |
else |
| 14044 |
{ |
| 14045 |
auto type = expr_type; |
| 14046 |
type.basetype = expected_type; |
| 14047 |
expr = bitcast_expression(type, expr_type.basetype, expr); |
| 14048 |
} |
| 14049 |
} |
| 14050 |
} |
| 14051 |
|
| 14052 |
string CompilerMSL::to_initializer_expression(const SPIRVariable &var) |
| 14053 |
{ |
| 14054 |
// We risk getting an array initializer here with MSL. If we have an array. |
| 14055 |
// FIXME: We cannot handle non-constant arrays being initialized. |
| 14056 |
// We will need to inject spvArrayCopy here somehow ... |
| 14057 |
auto &type = get<SPIRType>(var.basetype); |
| 14058 |
string expr; |
| 14059 |
if (ir.ids[var.initializer].get_type() == TypeConstant && |
| 14060 |
(!type.array.empty() || type.basetype == SPIRType::Struct)) |
| 14061 |
expr = constant_expression(get<SPIRConstant>(var.initializer)); |
| 14062 |
else |
| 14063 |
expr = CompilerGLSL::to_initializer_expression(var); |
| 14064 |
// If the initializer has more vector components than the variable, add a swizzle. |
| 14065 |
// FIXME: This can't handle arrays or structs. |
| 14066 |
auto &init_type = expression_type(var.initializer); |
| 14067 |
if (type.array.empty() && type.basetype != SPIRType::Struct && init_type.vecsize > type.vecsize) |
| 14068 |
expr = enclose_expression(expr + vector_swizzle(type.vecsize, 0)); |
| 14069 |
return expr; |
| 14070 |
} |
| 14071 |
|
| 14072 |
string CompilerMSL::to_zero_initialized_expression(uint32_t) |
| 14073 |
{ |
| 14074 |
return "{}"; |
| 14075 |
} |
| 14076 |
|
| 14077 |
bool CompilerMSL::descriptor_set_is_argument_buffer(uint32_t desc_set) const |
| 14078 |
{ |
| 14079 |
if (!msl_options.argument_buffers) |
| 14080 |
return false; |
| 14081 |
if (desc_set >= kMaxArgumentBuffers) |
| 14082 |
return false; |
| 14083 |
|
| 14084 |
return (argument_buffer_discrete_mask & (1u << desc_set)) == 0; |
| 14085 |
} |
| 14086 |
|
| 14087 |
void CompilerMSL::analyze_argument_buffers() |
| 14088 |
{ |
| 14089 |
// Gather all used resources and sort them out into argument buffers. |
| 14090 |
// Each argument buffer corresponds to a descriptor set in SPIR-V. |
| 14091 |
// The [[id(N)]] values used correspond to the resource mapping we have for MSL. |
| 14092 |
// Otherwise, the binding number is used, but this is generally not safe some types like |
| 14093 |
// combined image samplers and arrays of resources. Metal needs different indices here, |
| 14094 |
// while SPIR-V can have one descriptor set binding. To use argument buffers in practice, |
| 14095 |
// you will need to use the remapping from the API. |
| 14096 |
for (auto &id : argument_buffer_ids) |
| 14097 |
id = 0; |
| 14098 |
|
| 14099 |
// Output resources, sorted by resource index & type. |
| 14100 |
struct Resource |
| 14101 |
{ |
| 14102 |
SPIRVariable *var; |
| 14103 |
string name; |
| 14104 |
SPIRType::BaseType basetype; |
| 14105 |
uint32_t index; |
| 14106 |
uint32_t plane; |
| 14107 |
}; |
| 14108 |
SmallVector<Resource> resources_in_set[kMaxArgumentBuffers]; |
| 14109 |
SmallVector<uint32_t> inline_block_vars; |
| 14110 |
|
| 14111 |
bool set_needs_swizzle_buffer[kMaxArgumentBuffers] = {}; |
| 14112 |
bool set_needs_buffer_sizes[kMaxArgumentBuffers] = {}; |
| 14113 |
bool needs_buffer_sizes = false; |
| 14114 |
|
| 14115 |
ir.for_each_typed_id<SPIRVariable>([&](uint32_t self, SPIRVariable &var) { |
| 14116 |
if ((var.storage == StorageClassUniform || var.storage == StorageClassUniformConstant || |
| 14117 |
var.storage == StorageClassStorageBuffer) && |
| 14118 |
!is_hidden_variable(var)) |
| 14119 |
{ |
| 14120 |
uint32_t desc_set = get_decoration(self, DecorationDescriptorSet); |
| 14121 |
// Ignore if it's part of a push descriptor set. |
| 14122 |
if (!descriptor_set_is_argument_buffer(desc_set)) |
| 14123 |
return; |
| 14124 |
|
| 14125 |
uint32_t var_id = var.self; |
| 14126 |
auto &type = get_variable_data_type(var); |
| 14127 |
|
| 14128 |
if (desc_set >= kMaxArgumentBuffers) |
| 14129 |
SPIRV_CROSS_THROW("Descriptor set index is out of range."); |
| 14130 |
|
| 14131 |
const MSLConstexprSampler *constexpr_sampler = nullptr; |
| 14132 |
if (type.basetype == SPIRType::SampledImage || type.basetype == SPIRType::Sampler) |
| 14133 |
{ |
| 14134 |
constexpr_sampler = find_constexpr_sampler(var_id); |
| 14135 |
if (constexpr_sampler) |
| 14136 |
{ |
| 14137 |
// Mark this ID as a constexpr sampler for later in case it came from set/bindings. |
| 14138 |
constexpr_samplers_by_id[var_id] = *constexpr_sampler; |
| 14139 |
} |
| 14140 |
} |
| 14141 |
|
| 14142 |
uint32_t binding = get_decoration(var_id, DecorationBinding); |
| 14143 |
if (type.basetype == SPIRType::SampledImage) |
| 14144 |
{ |
| 14145 |
add_resource_name(var_id); |
| 14146 |
|
| 14147 |
uint32_t plane_count = 1; |
| 14148 |
if (constexpr_sampler && constexpr_sampler->ycbcr_conversion_enable) |
| 14149 |
plane_count = constexpr_sampler->planes; |
| 14150 |
|
| 14151 |
for (uint32_t i = 0; i < plane_count; i++) |
| 14152 |
{ |
| 14153 |
uint32_t image_resource_index = get_metal_resource_index(var, SPIRType::Image, i); |
| 14154 |
resources_in_set[desc_set].push_back( |
| 14155 |
{ &var, to_name(var_id), SPIRType::Image, image_resource_index, i }); |
| 14156 |
} |
| 14157 |
|
| 14158 |
if (type.image.dim != DimBuffer && !constexpr_sampler) |
| 14159 |
{ |
| 14160 |
uint32_t sampler_resource_index = get_metal_resource_index(var, SPIRType::Sampler); |
| 14161 |
resources_in_set[desc_set].push_back( |
| 14162 |
{ &var, to_sampler_expression(var_id), SPIRType::Sampler, sampler_resource_index, 0 }); |
| 14163 |
} |
| 14164 |
} |
| 14165 |
else if (inline_uniform_blocks.count(SetBindingPair{ desc_set, binding })) |
| 14166 |
{ |
| 14167 |
inline_block_vars.push_back(var_id); |
| 14168 |
} |
| 14169 |
else if (!constexpr_sampler) |
| 14170 |
{ |
| 14171 |
// constexpr samplers are not declared as resources. |
| 14172 |
// Inline uniform blocks are always emitted at the end. |
| 14173 |
if (!msl_options.is_ios() || type.basetype != SPIRType::Image || type.image.sampled != 2) |
| 14174 |
{ |
| 14175 |
add_resource_name(var_id); |
| 14176 |
resources_in_set[desc_set].push_back( |
| 14177 |
{ &var, to_name(var_id), type.basetype, get_metal_resource_index(var, type.basetype), 0 }); |
| 14178 |
|
| 14179 |
// Emulate texture2D atomic operations |
| 14180 |
if (atomic_image_vars.count(var.self)) |
| 14181 |
{ |
| 14182 |
uint32_t buffer_resource_index = get_metal_resource_index(var, SPIRType::AtomicCounter, 0); |
| 14183 |
resources_in_set[desc_set].push_back( |
| 14184 |
{ &var, to_name(var_id) + "_atomic", SPIRType::Struct, buffer_resource_index, 0 }); |
| 14185 |
} |
| 14186 |
} |
| 14187 |
} |
| 14188 |
|
| 14189 |
// Check if this descriptor set needs a swizzle buffer. |
| 14190 |
if (needs_swizzle_buffer_def && is_sampled_image_type(type)) |
| 14191 |
set_needs_swizzle_buffer[desc_set] = true; |
| 14192 |
else if (buffers_requiring_array_length.count(var_id) != 0) |
| 14193 |
{ |
| 14194 |
set_needs_buffer_sizes[desc_set] = true; |
| 14195 |
needs_buffer_sizes = true; |
| 14196 |
} |
| 14197 |
} |
| 14198 |
}); |
| 14199 |
|
| 14200 |
if (needs_swizzle_buffer_def || needs_buffer_sizes) |
| 14201 |
{ |
| 14202 |
uint32_t uint_ptr_type_id = 0; |
| 14203 |
|
| 14204 |
// We might have to add a swizzle buffer resource to the set. |
| 14205 |
for (uint32_t desc_set = 0; desc_set < kMaxArgumentBuffers; desc_set++) |
| 14206 |
{ |
| 14207 |
if (!set_needs_swizzle_buffer[desc_set] && !set_needs_buffer_sizes[desc_set]) |
| 14208 |
continue; |
| 14209 |
|
| 14210 |
if (uint_ptr_type_id == 0) |
| 14211 |
{ |
| 14212 |
uint_ptr_type_id = ir.increase_bound_by(1); |
| 14213 |
|
| 14214 |
// Create a buffer to hold extra data, including the swizzle constants. |
| 14215 |
SPIRType uint_type_pointer = get_uint_type(); |
| 14216 |
uint_type_pointer.pointer = true; |
| 14217 |
uint_type_pointer.pointer_depth = 1; |
| 14218 |
uint_type_pointer.parent_type = get_uint_type_id(); |
| 14219 |
uint_type_pointer.storage = StorageClassUniform; |
| 14220 |
set<SPIRType>(uint_ptr_type_id, uint_type_pointer); |
| 14221 |
set_decoration(uint_ptr_type_id, DecorationArrayStride, 4); |
| 14222 |
} |
| 14223 |
|
| 14224 |
if (set_needs_swizzle_buffer[desc_set]) |
| 14225 |
{ |
| 14226 |
uint32_t var_id = ir.increase_bound_by(1); |
| 14227 |
auto &var = set<SPIRVariable>(var_id, uint_ptr_type_id, StorageClassUniformConstant); |
| 14228 |
set_name(var_id, "spvSwizzleConstants"); |
| 14229 |
set_decoration(var_id, DecorationDescriptorSet, desc_set); |
| 14230 |
set_decoration(var_id, DecorationBinding, kSwizzleBufferBinding); |
| 14231 |
resources_in_set[desc_set].push_back( |
| 14232 |
{ &var, to_name(var_id), SPIRType::UInt, get_metal_resource_index(var, SPIRType::UInt), 0 }); |
| 14233 |
} |
| 14234 |
|
| 14235 |
if (set_needs_buffer_sizes[desc_set]) |
| 14236 |
{ |
| 14237 |
uint32_t var_id = ir.increase_bound_by(1); |
| 14238 |
auto &var = set<SPIRVariable>(var_id, uint_ptr_type_id, StorageClassUniformConstant); |
| 14239 |
set_name(var_id, "spvBufferSizeConstants"); |
| 14240 |
set_decoration(var_id, DecorationDescriptorSet, desc_set); |
| 14241 |
set_decoration(var_id, DecorationBinding, kBufferSizeBufferBinding); |
| 14242 |
resources_in_set[desc_set].push_back( |
| 14243 |
{ &var, to_name(var_id), SPIRType::UInt, get_metal_resource_index(var, SPIRType::UInt), 0 }); |
| 14244 |
} |
| 14245 |
} |
| 14246 |
} |
| 14247 |
|
| 14248 |
// Now add inline uniform blocks. |
| 14249 |
for (uint32_t var_id : inline_block_vars) |
| 14250 |
{ |
| 14251 |
auto &var = get<SPIRVariable>(var_id); |
| 14252 |
uint32_t desc_set = get_decoration(var_id, DecorationDescriptorSet); |
| 14253 |
add_resource_name(var_id); |
| 14254 |
resources_in_set[desc_set].push_back( |
| 14255 |
{ &var, to_name(var_id), SPIRType::Struct, get_metal_resource_index(var, SPIRType::Struct), 0 }); |
| 14256 |
} |
| 14257 |
|
| 14258 |
for (uint32_t desc_set = 0; desc_set < kMaxArgumentBuffers; desc_set++) |
| 14259 |
{ |
| 14260 |
auto &resources = resources_in_set[desc_set]; |
| 14261 |
if (resources.empty()) |
| 14262 |
continue; |
| 14263 |
|
| 14264 |
assert(descriptor_set_is_argument_buffer(desc_set)); |
| 14265 |
|
| 14266 |
uint32_t next_id = ir.increase_bound_by(3); |
| 14267 |
uint32_t type_id = next_id + 1; |
| 14268 |
uint32_t ptr_type_id = next_id + 2; |
| 14269 |
argument_buffer_ids[desc_set] = next_id; |
| 14270 |
|
| 14271 |
auto &buffer_type = set<SPIRType>(type_id); |
| 14272 |
|
| 14273 |
buffer_type.basetype = SPIRType::Struct; |
| 14274 |
|
| 14275 |
if ((argument_buffer_device_storage_mask & (1u << desc_set)) != 0) |
| 14276 |
{ |
| 14277 |
buffer_type.storage = StorageClassStorageBuffer; |
| 14278 |
// Make sure the argument buffer gets marked as const device. |
| 14279 |
set_decoration(next_id, DecorationNonWritable); |
| 14280 |
// Need to mark the type as a Block to enable this. |
| 14281 |
set_decoration(type_id, DecorationBlock); |
| 14282 |
} |
| 14283 |
else |
| 14284 |
buffer_type.storage = StorageClassUniform; |
| 14285 |
|
| 14286 |
set_name(type_id, join("spvDescriptorSetBuffer", desc_set)); |
| 14287 |
|
| 14288 |
auto &ptr_type = set<SPIRType>(ptr_type_id); |
| 14289 |
ptr_type = buffer_type; |
| 14290 |
ptr_type.pointer = true; |
| 14291 |
ptr_type.pointer_depth = 1; |
| 14292 |
ptr_type.parent_type = type_id; |
| 14293 |
|
| 14294 |
uint32_t buffer_variable_id = next_id; |
| 14295 |
set<SPIRVariable>(buffer_variable_id, ptr_type_id, StorageClassUniform); |
| 14296 |
set_name(buffer_variable_id, join("spvDescriptorSet", desc_set)); |
| 14297 |
|
| 14298 |
// Ids must be emitted in ID order. |
| 14299 |
sort(begin(resources), end(resources), [&](const Resource &lhs, const Resource &rhs) -> bool { |
| 14300 |
return tie(lhs.index, lhs.basetype) < tie(rhs.index, rhs.basetype); |
| 14301 |
}); |
| 14302 |
|
| 14303 |
uint32_t member_index = 0; |
| 14304 |
for (auto &resource : resources) |
| 14305 |
{ |
| 14306 |
auto &var = *resource.var; |
| 14307 |
auto &type = get_variable_data_type(var); |
| 14308 |
string mbr_name = ensure_valid_name(resource.name, "m"); |
| 14309 |
if (resource.plane > 0) |
| 14310 |
mbr_name += join(plane_name_suffix, resource.plane); |
| 14311 |
set_member_name(buffer_type.self, member_index, mbr_name); |
| 14312 |
|
| 14313 |
if (resource.basetype == SPIRType::Sampler && type.basetype != SPIRType::Sampler) |
| 14314 |
{ |
| 14315 |
// Have to synthesize a sampler type here. |
| 14316 |
|
| 14317 |
bool type_is_array = !type.array.empty(); |
| 14318 |
uint32_t sampler_type_id = ir.increase_bound_by(type_is_array ? 2 : 1); |
| 14319 |
auto &new_sampler_type = set<SPIRType>(sampler_type_id); |
| 14320 |
new_sampler_type.basetype = SPIRType::Sampler; |
| 14321 |
new_sampler_type.storage = StorageClassUniformConstant; |
| 14322 |
|
| 14323 |
if (type_is_array) |
| 14324 |
{ |
| 14325 |
uint32_t sampler_type_array_id = sampler_type_id + 1; |
| 14326 |
auto &sampler_type_array = set<SPIRType>(sampler_type_array_id); |
| 14327 |
sampler_type_array = new_sampler_type; |
| 14328 |
sampler_type_array.array = type.array; |
| 14329 |
sampler_type_array.array_size_literal = type.array_size_literal; |
| 14330 |
sampler_type_array.parent_type = sampler_type_id; |
| 14331 |
buffer_type.member_types.push_back(sampler_type_array_id); |
| 14332 |
} |
| 14333 |
else |
| 14334 |
buffer_type.member_types.push_back(sampler_type_id); |
| 14335 |
} |
| 14336 |
else |
| 14337 |
{ |
| 14338 |
uint32_t binding = get_decoration(var.self, DecorationBinding); |
| 14339 |
SetBindingPair pair = { desc_set, binding }; |
| 14340 |
|
| 14341 |
if (resource.basetype == SPIRType::Image || resource.basetype == SPIRType::Sampler || |
| 14342 |
resource.basetype == SPIRType::SampledImage) |
| 14343 |
{ |
| 14344 |
// Drop pointer information when we emit the resources into a struct. |
| 14345 |
buffer_type.member_types.push_back(get_variable_data_type_id(var)); |
| 14346 |
if (resource.plane == 0) |
| 14347 |
set_qualified_name(var.self, join(to_name(buffer_variable_id), ".", mbr_name)); |
| 14348 |
} |
| 14349 |
else if (buffers_requiring_dynamic_offset.count(pair)) |
| 14350 |
{ |
| 14351 |
// Don't set the qualified name here; we'll define a variable holding the corrected buffer address later. |
| 14352 |
buffer_type.member_types.push_back(var.basetype); |
| 14353 |
buffers_requiring_dynamic_offset[pair].second = var.self; |
| 14354 |
} |
| 14355 |
else if (inline_uniform_blocks.count(pair)) |
| 14356 |
{ |
| 14357 |
// Put the buffer block itself into the argument buffer. |
| 14358 |
buffer_type.member_types.push_back(get_variable_data_type_id(var)); |
| 14359 |
set_qualified_name(var.self, join(to_name(buffer_variable_id), ".", mbr_name)); |
| 14360 |
} |
| 14361 |
else if (atomic_image_vars.count(var.self)) |
| 14362 |
{ |
| 14363 |
// Emulate texture2D atomic operations. |
| 14364 |
// Don't set the qualified name: it's already set for this variable, |
| 14365 |
// and the code that references the buffer manually appends "_atomic" |
| 14366 |
// to the name. |
| 14367 |
uint32_t offset = ir.increase_bound_by(2); |
| 14368 |
uint32_t atomic_type_id = offset; |
| 14369 |
uint32_t type_ptr_id = offset + 1; |
| 14370 |
|
| 14371 |
SPIRType atomic_type; |
| 14372 |
atomic_type.basetype = SPIRType::AtomicCounter; |
| 14373 |
atomic_type.width = 32; |
| 14374 |
atomic_type.vecsize = 1; |
| 14375 |
set<SPIRType>(atomic_type_id, atomic_type); |
| 14376 |
|
| 14377 |
atomic_type.pointer = true; |
| 14378 |
atomic_type.parent_type = atomic_type_id; |
| 14379 |
atomic_type.storage = StorageClassStorageBuffer; |
| 14380 |
auto &atomic_ptr_type = set<SPIRType>(type_ptr_id, atomic_type); |
| 14381 |
atomic_ptr_type.self = atomic_type_id; |
| 14382 |
|
| 14383 |
buffer_type.member_types.push_back(type_ptr_id); |
| 14384 |
} |
| 14385 |
else |
| 14386 |
{ |
| 14387 |
// Resources will be declared as pointers not references, so automatically dereference as appropriate. |
| 14388 |
buffer_type.member_types.push_back(var.basetype); |
| 14389 |
if (type.array.empty()) |
| 14390 |
set_qualified_name(var.self, join("(*", to_name(buffer_variable_id), ".", mbr_name, ")")); |
| 14391 |
else |
| 14392 |
set_qualified_name(var.self, join(to_name(buffer_variable_id), ".", mbr_name)); |
| 14393 |
} |
| 14394 |
} |
| 14395 |
|
| 14396 |
set_extended_member_decoration(buffer_type.self, member_index, SPIRVCrossDecorationResourceIndexPrimary, |
| 14397 |
resource.index); |
| 14398 |
set_extended_member_decoration(buffer_type.self, member_index, SPIRVCrossDecorationInterfaceOrigID, |
| 14399 |
var.self); |
| 14400 |
member_index++; |
| 14401 |
} |
| 14402 |
} |
| 14403 |
} |
| 14404 |
|
| 14405 |
void CompilerMSL::activate_argument_buffer_resources() |
| 14406 |
{ |
| 14407 |
// For ABI compatibility, force-enable all resources which are part of argument buffers. |
| 14408 |
ir.for_each_typed_id<SPIRVariable>([&](uint32_t self, const SPIRVariable &) { |
| 14409 |
if (!has_decoration(self, DecorationDescriptorSet)) |
| 14410 |
return; |
| 14411 |
|
| 14412 |
uint32_t desc_set = get_decoration(self, DecorationDescriptorSet); |
| 14413 |
if (descriptor_set_is_argument_buffer(desc_set)) |
| 14414 |
active_interface_variables.insert(self); |
| 14415 |
}); |
| 14416 |
} |
| 14417 |
|
| 14418 |
bool CompilerMSL::using_builtin_array() const |
| 14419 |
{ |
| 14420 |
return msl_options.force_native_arrays || is_using_builtin_array; |
| 14421 |
} |
| 14422 |
|
| 14423 |
void CompilerMSL::set_combined_sampler_suffix(const char *suffix) |
| 14424 |
{ |
| 14425 |
sampler_name_suffix = suffix; |
| 14426 |
} |
| 14427 |
|
| 14428 |
const char *CompilerMSL::get_combined_sampler_suffix() const |
| 14429 |
{ |
| 14430 |
return sampler_name_suffix.c_str(); |
| 14431 |
} |