| 1 |
/* |
| 2 |
* Copyright 2015-2020 Arm Limited |
| 3 |
* |
| 4 |
* Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 |
* you may not use this file except in compliance with the License. |
| 6 |
* You may obtain a copy of the License at |
| 7 |
* |
| 8 |
* http://www.apache.org/licenses/LICENSE-2.0 |
| 9 |
* |
| 10 |
* Unless required by applicable law or agreed to in writing, software |
| 11 |
* distributed under the License is distributed on an "AS IS" BASIS, |
| 12 |
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 |
* See the License for the specific language governing permissions and |
| 14 |
* limitations under the License. |
| 15 |
*/ |
| 16 |
|
| 17 |
#include "spirv_glsl.hpp" |
| 18 |
#include "GLSL.std.450.h" |
| 19 |
#include "spirv_common.hpp" |
| 20 |
#include <algorithm> |
| 21 |
#include <assert.h> |
| 22 |
#include <cmath> |
| 23 |
#include <limits> |
| 24 |
#include <locale.h> |
| 25 |
#include <utility> |
| 26 |
|
| 27 |
#ifndef _WIN32 |
| 28 |
#include <langinfo.h> |
| 29 |
#endif |
| 30 |
#include <locale.h> |
| 31 |
|
| 32 |
using namespace spv; |
| 33 |
using namespace SPIRV_CROSS_NAMESPACE; |
| 34 |
using namespace std; |
| 35 |
|
| 36 |
static bool is_unsigned_opcode(Op op) |
| 37 |
{ |
| 38 |
// Don't have to be exhaustive, only relevant for legacy target checking ... |
| 39 |
switch (op) |
| 40 |
{ |
| 41 |
case OpShiftRightLogical: |
| 42 |
case OpUGreaterThan: |
| 43 |
case OpUGreaterThanEqual: |
| 44 |
case OpULessThan: |
| 45 |
case OpULessThanEqual: |
| 46 |
case OpUConvert: |
| 47 |
case OpUDiv: |
| 48 |
case OpUMod: |
| 49 |
case OpUMulExtended: |
| 50 |
case OpConvertUToF: |
| 51 |
case OpConvertFToU: |
| 52 |
return true; |
| 53 |
|
| 54 |
default: |
| 55 |
return false; |
| 56 |
} |
| 57 |
} |
| 58 |
|
| 59 |
static bool is_unsigned_glsl_opcode(GLSLstd450 op) |
| 60 |
{ |
| 61 |
// Don't have to be exhaustive, only relevant for legacy target checking ... |
| 62 |
switch (op) |
| 63 |
{ |
| 64 |
case GLSLstd450UClamp: |
| 65 |
case GLSLstd450UMin: |
| 66 |
case GLSLstd450UMax: |
| 67 |
case GLSLstd450FindUMsb: |
| 68 |
return true; |
| 69 |
|
| 70 |
default: |
| 71 |
return false; |
| 72 |
} |
| 73 |
} |
| 74 |
|
| 75 |
static bool packing_is_vec4_padded(BufferPackingStandard packing) |
| 76 |
{ |
| 77 |
switch (packing) |
| 78 |
{ |
| 79 |
case BufferPackingHLSLCbuffer: |
| 80 |
case BufferPackingHLSLCbufferPackOffset: |
| 81 |
case BufferPackingStd140: |
| 82 |
case BufferPackingStd140EnhancedLayout: |
| 83 |
return true; |
| 84 |
|
| 85 |
default: |
| 86 |
return false; |
| 87 |
} |
| 88 |
} |
| 89 |
|
| 90 |
static bool packing_is_hlsl(BufferPackingStandard packing) |
| 91 |
{ |
| 92 |
switch (packing) |
| 93 |
{ |
| 94 |
case BufferPackingHLSLCbuffer: |
| 95 |
case BufferPackingHLSLCbufferPackOffset: |
| 96 |
return true; |
| 97 |
|
| 98 |
default: |
| 99 |
return false; |
| 100 |
} |
| 101 |
} |
| 102 |
|
| 103 |
static bool packing_has_flexible_offset(BufferPackingStandard packing) |
| 104 |
{ |
| 105 |
switch (packing) |
| 106 |
{ |
| 107 |
case BufferPackingStd140: |
| 108 |
case BufferPackingStd430: |
| 109 |
case BufferPackingScalar: |
| 110 |
case BufferPackingHLSLCbuffer: |
| 111 |
return false; |
| 112 |
|
| 113 |
default: |
| 114 |
return true; |
| 115 |
} |
| 116 |
} |
| 117 |
|
| 118 |
static bool packing_is_scalar(BufferPackingStandard packing) |
| 119 |
{ |
| 120 |
switch (packing) |
| 121 |
{ |
| 122 |
case BufferPackingScalar: |
| 123 |
case BufferPackingScalarEnhancedLayout: |
| 124 |
return true; |
| 125 |
|
| 126 |
default: |
| 127 |
return false; |
| 128 |
} |
| 129 |
} |
| 130 |
|
| 131 |
static BufferPackingStandard packing_to_substruct_packing(BufferPackingStandard packing) |
| 132 |
{ |
| 133 |
switch (packing) |
| 134 |
{ |
| 135 |
case BufferPackingStd140EnhancedLayout: |
| 136 |
return BufferPackingStd140; |
| 137 |
case BufferPackingStd430EnhancedLayout: |
| 138 |
return BufferPackingStd430; |
| 139 |
case BufferPackingHLSLCbufferPackOffset: |
| 140 |
return BufferPackingHLSLCbuffer; |
| 141 |
case BufferPackingScalarEnhancedLayout: |
| 142 |
return BufferPackingScalar; |
| 143 |
default: |
| 144 |
return packing; |
| 145 |
} |
| 146 |
} |
| 147 |
|
| 148 |
void CompilerGLSL::init() |
| 149 |
{ |
| 150 |
if (ir.source.known) |
| 151 |
{ |
| 152 |
options.es = ir.source.es; |
| 153 |
options.version = ir.source.version; |
| 154 |
} |
| 155 |
|
| 156 |
// Query the locale to see what the decimal point is. |
| 157 |
// We'll rely on fixing it up ourselves in the rare case we have a comma-as-decimal locale |
| 158 |
// rather than setting locales ourselves. Settings locales in a safe and isolated way is rather |
| 159 |
// tricky. |
| 160 |
#ifdef _WIN32 |
| 161 |
// On Windows, localeconv uses thread-local storage, so it should be fine. |
| 162 |
const struct lconv *conv = localeconv(); |
| 163 |
if (conv && conv->decimal_point) |
| 164 |
current_locale_radix_character = *conv->decimal_point; |
| 165 |
#elif defined(__ANDROID__) && __ANDROID_API__ < 26 |
| 166 |
// nl_langinfo is not supported on this platform, fall back to the worse alternative. |
| 167 |
const struct lconv *conv = localeconv(); |
| 168 |
if (conv && conv->decimal_point) |
| 169 |
current_locale_radix_character = *conv->decimal_point; |
| 170 |
#else |
| 171 |
// localeconv, the portable function is not MT safe ... |
| 172 |
const char *decimal_point = nl_langinfo(RADIXCHAR); |
| 173 |
if (decimal_point && *decimal_point != '\0') |
| 174 |
current_locale_radix_character = *decimal_point; |
| 175 |
#endif |
| 176 |
} |
| 177 |
|
| 178 |
static const char *to_pls_layout(PlsFormat format) |
| 179 |
{ |
| 180 |
switch (format) |
| 181 |
{ |
| 182 |
case PlsR11FG11FB10F: |
| 183 |
return "layout(r11f_g11f_b10f) "; |
| 184 |
case PlsR32F: |
| 185 |
return "layout(r32f) "; |
| 186 |
case PlsRG16F: |
| 187 |
return "layout(rg16f) "; |
| 188 |
case PlsRGB10A2: |
| 189 |
return "layout(rgb10_a2) "; |
| 190 |
case PlsRGBA8: |
| 191 |
return "layout(rgba8) "; |
| 192 |
case PlsRG16: |
| 193 |
return "layout(rg16) "; |
| 194 |
case PlsRGBA8I: |
| 195 |
return "layout(rgba8i)"; |
| 196 |
case PlsRG16I: |
| 197 |
return "layout(rg16i) "; |
| 198 |
case PlsRGB10A2UI: |
| 199 |
return "layout(rgb10_a2ui) "; |
| 200 |
case PlsRGBA8UI: |
| 201 |
return "layout(rgba8ui) "; |
| 202 |
case PlsRG16UI: |
| 203 |
return "layout(rg16ui) "; |
| 204 |
case PlsR32UI: |
| 205 |
return "layout(r32ui) "; |
| 206 |
default: |
| 207 |
return ""; |
| 208 |
} |
| 209 |
} |
| 210 |
|
| 211 |
static SPIRType::BaseType pls_format_to_basetype(PlsFormat format) |
| 212 |
{ |
| 213 |
switch (format) |
| 214 |
{ |
| 215 |
default: |
| 216 |
case PlsR11FG11FB10F: |
| 217 |
case PlsR32F: |
| 218 |
case PlsRG16F: |
| 219 |
case PlsRGB10A2: |
| 220 |
case PlsRGBA8: |
| 221 |
case PlsRG16: |
| 222 |
return SPIRType::Float; |
| 223 |
|
| 224 |
case PlsRGBA8I: |
| 225 |
case PlsRG16I: |
| 226 |
return SPIRType::Int; |
| 227 |
|
| 228 |
case PlsRGB10A2UI: |
| 229 |
case PlsRGBA8UI: |
| 230 |
case PlsRG16UI: |
| 231 |
case PlsR32UI: |
| 232 |
return SPIRType::UInt; |
| 233 |
} |
| 234 |
} |
| 235 |
|
| 236 |
static uint32_t pls_format_to_components(PlsFormat format) |
| 237 |
{ |
| 238 |
switch (format) |
| 239 |
{ |
| 240 |
default: |
| 241 |
case PlsR32F: |
| 242 |
case PlsR32UI: |
| 243 |
return 1; |
| 244 |
|
| 245 |
case PlsRG16F: |
| 246 |
case PlsRG16: |
| 247 |
case PlsRG16UI: |
| 248 |
case PlsRG16I: |
| 249 |
return 2; |
| 250 |
|
| 251 |
case PlsR11FG11FB10F: |
| 252 |
return 3; |
| 253 |
|
| 254 |
case PlsRGB10A2: |
| 255 |
case PlsRGBA8: |
| 256 |
case PlsRGBA8I: |
| 257 |
case PlsRGB10A2UI: |
| 258 |
case PlsRGBA8UI: |
| 259 |
return 4; |
| 260 |
} |
| 261 |
} |
| 262 |
|
| 263 |
const char *CompilerGLSL::vector_swizzle(int vecsize, int index) |
| 264 |
{ |
| 265 |
static const char *const swizzle[4][4] = { |
| 266 |
{ ".x", ".y", ".z", ".w" }, |
| 267 |
{ ".xy", ".yz", ".zw", nullptr }, |
| 268 |
{ ".xyz", ".yzw", nullptr, nullptr }, |
| 269 |
#if defined(__GNUC__) && (__GNUC__ == 9) |
| 270 |
// This works around a GCC 9 bug, see details in https://gcc.gnu.org/bugzilla/show_bug.cgi?id=90947. |
| 271 |
// This array ends up being compiled as all nullptrs, tripping the assertions below. |
| 272 |
{ "", nullptr, nullptr, "$" }, |
| 273 |
#else |
| 274 |
{ "", nullptr, nullptr, nullptr }, |
| 275 |
#endif |
| 276 |
}; |
| 277 |
|
| 278 |
assert(vecsize >= 1 && vecsize <= 4); |
| 279 |
assert(index >= 0 && index < 4); |
| 280 |
assert(swizzle[vecsize - 1][index]); |
| 281 |
|
| 282 |
return swizzle[vecsize - 1][index]; |
| 283 |
} |
| 284 |
|
| 285 |
void CompilerGLSL::reset() |
| 286 |
{ |
| 287 |
// We do some speculative optimizations which should pretty much always work out, |
| 288 |
// but just in case the SPIR-V is rather weird, recompile until it's happy. |
| 289 |
// This typically only means one extra pass. |
| 290 |
clear_force_recompile(); |
| 291 |
|
| 292 |
// Clear invalid expression tracking. |
| 293 |
invalid_expressions.clear(); |
| 294 |
current_function = nullptr; |
| 295 |
|
| 296 |
// Clear temporary usage tracking. |
| 297 |
expression_usage_counts.clear(); |
| 298 |
forwarded_temporaries.clear(); |
| 299 |
suppressed_usage_tracking.clear(); |
| 300 |
|
| 301 |
// Ensure that we declare phi-variable copies even if the original declaration isn't deferred |
| 302 |
flushed_phi_variables.clear(); |
| 303 |
|
| 304 |
reset_name_caches(); |
| 305 |
|
| 306 |
ir.for_each_typed_id<SPIRFunction>([&](uint32_t, SPIRFunction &func) { |
| 307 |
func.active = false; |
| 308 |
func.flush_undeclared = true; |
| 309 |
}); |
| 310 |
|
| 311 |
ir.for_each_typed_id<SPIRVariable>([&](uint32_t, SPIRVariable &var) { var.dependees.clear(); }); |
| 312 |
|
| 313 |
ir.reset_all_of_type<SPIRExpression>(); |
| 314 |
ir.reset_all_of_type<SPIRAccessChain>(); |
| 315 |
|
| 316 |
statement_count = 0; |
| 317 |
indent = 0; |
| 318 |
current_loop_level = 0; |
| 319 |
} |
| 320 |
|
| 321 |
void CompilerGLSL::remap_pls_variables() |
| 322 |
{ |
| 323 |
for (auto &input : pls_inputs) |
| 324 |
{ |
| 325 |
auto &var = get<SPIRVariable>(input.id); |
| 326 |
|
| 327 |
bool input_is_target = false; |
| 328 |
if (var.storage == StorageClassUniformConstant) |
| 329 |
{ |
| 330 |
auto &type = get<SPIRType>(var.basetype); |
| 331 |
input_is_target = type.image.dim == DimSubpassData; |
| 332 |
} |
| 333 |
|
| 334 |
if (var.storage != StorageClassInput && !input_is_target) |
| 335 |
SPIRV_CROSS_THROW("Can only use in and target variables for PLS inputs."); |
| 336 |
var.remapped_variable = true; |
| 337 |
} |
| 338 |
|
| 339 |
for (auto &output : pls_outputs) |
| 340 |
{ |
| 341 |
auto &var = get<SPIRVariable>(output.id); |
| 342 |
if (var.storage != StorageClassOutput) |
| 343 |
SPIRV_CROSS_THROW("Can only use out variables for PLS outputs."); |
| 344 |
var.remapped_variable = true; |
| 345 |
} |
| 346 |
} |
| 347 |
|
| 348 |
void CompilerGLSL::remap_ext_framebuffer_fetch(uint32_t input_attachment_index, uint32_t color_location) |
| 349 |
{ |
| 350 |
subpass_to_framebuffer_fetch_attachment.push_back({ input_attachment_index, color_location }); |
| 351 |
inout_color_attachments.insert(color_location); |
| 352 |
} |
| 353 |
|
| 354 |
void CompilerGLSL::find_static_extensions() |
| 355 |
{ |
| 356 |
ir.for_each_typed_id<SPIRType>([&](uint32_t, const SPIRType &type) { |
| 357 |
if (type.basetype == SPIRType::Double) |
| 358 |
{ |
| 359 |
if (options.es) |
| 360 |
SPIRV_CROSS_THROW("FP64 not supported in ES profile."); |
| 361 |
if (!options.es && options.version < 400) |
| 362 |
require_extension_internal("GL_ARB_gpu_shader_fp64"); |
| 363 |
} |
| 364 |
else if (type.basetype == SPIRType::Int64 || type.basetype == SPIRType::UInt64) |
| 365 |
{ |
| 366 |
if (options.es) |
| 367 |
SPIRV_CROSS_THROW("64-bit integers not supported in ES profile."); |
| 368 |
if (!options.es) |
| 369 |
require_extension_internal("GL_ARB_gpu_shader_int64"); |
| 370 |
} |
| 371 |
else if (type.basetype == SPIRType::Half) |
| 372 |
{ |
| 373 |
require_extension_internal("GL_EXT_shader_explicit_arithmetic_types_float16"); |
| 374 |
if (options.vulkan_semantics) |
| 375 |
require_extension_internal("GL_EXT_shader_16bit_storage"); |
| 376 |
} |
| 377 |
else if (type.basetype == SPIRType::SByte || type.basetype == SPIRType::UByte) |
| 378 |
{ |
| 379 |
require_extension_internal("GL_EXT_shader_explicit_arithmetic_types_int8"); |
| 380 |
if (options.vulkan_semantics) |
| 381 |
require_extension_internal("GL_EXT_shader_8bit_storage"); |
| 382 |
} |
| 383 |
else if (type.basetype == SPIRType::Short || type.basetype == SPIRType::UShort) |
| 384 |
{ |
| 385 |
require_extension_internal("GL_EXT_shader_explicit_arithmetic_types_int16"); |
| 386 |
if (options.vulkan_semantics) |
| 387 |
require_extension_internal("GL_EXT_shader_16bit_storage"); |
| 388 |
} |
| 389 |
}); |
| 390 |
|
| 391 |
auto &execution = get_entry_point(); |
| 392 |
switch (execution.model) |
| 393 |
{ |
| 394 |
case ExecutionModelGLCompute: |
| 395 |
if (!options.es && options.version < 430) |
| 396 |
require_extension_internal("GL_ARB_compute_shader"); |
| 397 |
if (options.es && options.version < 310) |
| 398 |
SPIRV_CROSS_THROW("At least ESSL 3.10 required for compute shaders."); |
| 399 |
break; |
| 400 |
|
| 401 |
case ExecutionModelGeometry: |
| 402 |
if (options.es && options.version < 320) |
| 403 |
require_extension_internal("GL_EXT_geometry_shader"); |
| 404 |
if (!options.es && options.version < 150) |
| 405 |
require_extension_internal("GL_ARB_geometry_shader4"); |
| 406 |
|
| 407 |
if (execution.flags.get(ExecutionModeInvocations) && execution.invocations != 1) |
| 408 |
{ |
| 409 |
// Instanced GS is part of 400 core or this extension. |
| 410 |
if (!options.es && options.version < 400) |
| 411 |
require_extension_internal("GL_ARB_gpu_shader5"); |
| 412 |
} |
| 413 |
break; |
| 414 |
|
| 415 |
case ExecutionModelTessellationEvaluation: |
| 416 |
case ExecutionModelTessellationControl: |
| 417 |
if (options.es && options.version < 320) |
| 418 |
require_extension_internal("GL_EXT_tessellation_shader"); |
| 419 |
if (!options.es && options.version < 400) |
| 420 |
require_extension_internal("GL_ARB_tessellation_shader"); |
| 421 |
break; |
| 422 |
|
| 423 |
case ExecutionModelRayGenerationNV: |
| 424 |
case ExecutionModelIntersectionNV: |
| 425 |
case ExecutionModelAnyHitNV: |
| 426 |
case ExecutionModelClosestHitNV: |
| 427 |
case ExecutionModelMissNV: |
| 428 |
case ExecutionModelCallableNV: |
| 429 |
if (options.es || options.version < 460) |
| 430 |
SPIRV_CROSS_THROW("Ray tracing shaders require non-es profile with version 460 or above."); |
| 431 |
require_extension_internal("GL_NV_ray_tracing"); |
| 432 |
break; |
| 433 |
|
| 434 |
default: |
| 435 |
break; |
| 436 |
} |
| 437 |
|
| 438 |
if (!pls_inputs.empty() || !pls_outputs.empty()) |
| 439 |
{ |
| 440 |
if (execution.model != ExecutionModelFragment) |
| 441 |
SPIRV_CROSS_THROW("Can only use GL_EXT_shader_pixel_local_storage in fragment shaders."); |
| 442 |
require_extension_internal("GL_EXT_shader_pixel_local_storage"); |
| 443 |
} |
| 444 |
|
| 445 |
if (!inout_color_attachments.empty()) |
| 446 |
{ |
| 447 |
if (execution.model != ExecutionModelFragment) |
| 448 |
SPIRV_CROSS_THROW("Can only use GL_EXT_shader_framebuffer_fetch in fragment shaders."); |
| 449 |
if (options.vulkan_semantics) |
| 450 |
SPIRV_CROSS_THROW("Cannot use EXT_shader_framebuffer_fetch in Vulkan GLSL."); |
| 451 |
require_extension_internal("GL_EXT_shader_framebuffer_fetch"); |
| 452 |
} |
| 453 |
|
| 454 |
if (options.separate_shader_objects && !options.es && options.version < 410) |
| 455 |
require_extension_internal("GL_ARB_separate_shader_objects"); |
| 456 |
|
| 457 |
if (ir.addressing_model == AddressingModelPhysicalStorageBuffer64EXT) |
| 458 |
{ |
| 459 |
if (!options.vulkan_semantics) |
| 460 |
SPIRV_CROSS_THROW("GL_EXT_buffer_reference is only supported in Vulkan GLSL."); |
| 461 |
if (options.es && options.version < 320) |
| 462 |
SPIRV_CROSS_THROW("GL_EXT_buffer_reference requires ESSL 320."); |
| 463 |
else if (!options.es && options.version < 450) |
| 464 |
SPIRV_CROSS_THROW("GL_EXT_buffer_reference requires GLSL 450."); |
| 465 |
require_extension_internal("GL_EXT_buffer_reference"); |
| 466 |
} |
| 467 |
else if (ir.addressing_model != AddressingModelLogical) |
| 468 |
{ |
| 469 |
SPIRV_CROSS_THROW("Only Logical and PhysicalStorageBuffer64EXT addressing models are supported."); |
| 470 |
} |
| 471 |
|
| 472 |
// Check for nonuniform qualifier and passthrough. |
| 473 |
// Instead of looping over all decorations to find this, just look at capabilities. |
| 474 |
for (auto &cap : ir.declared_capabilities) |
| 475 |
{ |
| 476 |
switch (cap) |
| 477 |
{ |
| 478 |
case CapabilityShaderNonUniformEXT: |
| 479 |
if (!options.vulkan_semantics) |
| 480 |
require_extension_internal("GL_NV_gpu_shader5"); |
| 481 |
else |
| 482 |
require_extension_internal("GL_EXT_nonuniform_qualifier"); |
| 483 |
break; |
| 484 |
case CapabilityRuntimeDescriptorArrayEXT: |
| 485 |
if (!options.vulkan_semantics) |
| 486 |
SPIRV_CROSS_THROW("GL_EXT_nonuniform_qualifier is only supported in Vulkan GLSL."); |
| 487 |
require_extension_internal("GL_EXT_nonuniform_qualifier"); |
| 488 |
break; |
| 489 |
|
| 490 |
case CapabilityGeometryShaderPassthroughNV: |
| 491 |
if (execution.model == ExecutionModelGeometry) |
| 492 |
{ |
| 493 |
require_extension_internal("GL_NV_geometry_shader_passthrough"); |
| 494 |
execution.geometry_passthrough = true; |
| 495 |
} |
| 496 |
break; |
| 497 |
|
| 498 |
default: |
| 499 |
break; |
| 500 |
} |
| 501 |
} |
| 502 |
} |
| 503 |
|
| 504 |
string CompilerGLSL::compile() |
| 505 |
{ |
| 506 |
ir.fixup_reserved_names(); |
| 507 |
|
| 508 |
if (options.vulkan_semantics) |
| 509 |
backend.allow_precision_qualifiers = true; |
| 510 |
else |
| 511 |
{ |
| 512 |
// only NV_gpu_shader5 supports divergent indexing on OpenGL, and it does so without extra qualifiers |
| 513 |
backend.nonuniform_qualifier = ""; |
| 514 |
backend.needs_row_major_load_workaround = true; |
| 515 |
} |
| 516 |
backend.force_gl_in_out_block = true; |
| 517 |
backend.supports_extensions = true; |
| 518 |
backend.use_array_constructor = true; |
| 519 |
|
| 520 |
if (is_legacy_es()) |
| 521 |
backend.support_case_fallthrough = false; |
| 522 |
|
| 523 |
// Scan the SPIR-V to find trivial uses of extensions. |
| 524 |
fixup_type_alias(); |
| 525 |
reorder_type_alias(); |
| 526 |
build_function_control_flow_graphs_and_analyze(); |
| 527 |
find_static_extensions(); |
| 528 |
fixup_image_load_store_access(); |
| 529 |
update_active_builtins(); |
| 530 |
analyze_image_and_sampler_usage(); |
| 531 |
analyze_interlocked_resource_usage(); |
| 532 |
if (!inout_color_attachments.empty()) |
| 533 |
emit_inout_fragment_outputs_copy_to_subpass_inputs(); |
| 534 |
|
| 535 |
// Shaders might cast unrelated data to pointers of non-block types. |
| 536 |
// Find all such instances and make sure we can cast the pointers to a synthesized block type. |
| 537 |
if (ir.addressing_model == AddressingModelPhysicalStorageBuffer64EXT) |
| 538 |
analyze_non_block_pointer_types(); |
| 539 |
|
| 540 |
uint32_t pass_count = 0; |
| 541 |
do |
| 542 |
{ |
| 543 |
if (pass_count >= 3) |
| 544 |
SPIRV_CROSS_THROW("Over 3 compilation loops detected. Must be a bug!"); |
| 545 |
|
| 546 |
reset(); |
| 547 |
|
| 548 |
buffer.reset(); |
| 549 |
|
| 550 |
emit_header(); |
| 551 |
emit_resources(); |
| 552 |
emit_extension_workarounds(get_execution_model()); |
| 553 |
|
| 554 |
emit_function(get<SPIRFunction>(ir.default_entry_point), Bitset()); |
| 555 |
|
| 556 |
pass_count++; |
| 557 |
} while (is_forcing_recompilation()); |
| 558 |
|
| 559 |
// Implement the interlocked wrapper function at the end. |
| 560 |
// The body was implemented in lieu of main(). |
| 561 |
if (interlocked_is_complex) |
| 562 |
{ |
| 563 |
statement("void main()"); |
| 564 |
begin_scope(); |
| 565 |
statement("// Interlocks were used in a way not compatible with GLSL, this is very slow."); |
| 566 |
if (options.es) |
| 567 |
statement("beginInvocationInterlockNV();"); |
| 568 |
else |
| 569 |
statement("beginInvocationInterlockARB();"); |
| 570 |
statement("spvMainInterlockedBody();"); |
| 571 |
if (options.es) |
| 572 |
statement("endInvocationInterlockNV();"); |
| 573 |
else |
| 574 |
statement("endInvocationInterlockARB();"); |
| 575 |
end_scope(); |
| 576 |
} |
| 577 |
|
| 578 |
// Entry point in GLSL is always main(). |
| 579 |
get_entry_point().name = "main"; |
| 580 |
|
| 581 |
return buffer.str(); |
| 582 |
} |
| 583 |
|
| 584 |
std::string CompilerGLSL::get_partial_source() |
| 585 |
{ |
| 586 |
return buffer.str(); |
| 587 |
} |
| 588 |
|
| 589 |
void CompilerGLSL::build_workgroup_size(SmallVector<string> &arguments, const SpecializationConstant &wg_x, |
| 590 |
const SpecializationConstant &wg_y, const SpecializationConstant &wg_z) |
| 591 |
{ |
| 592 |
auto &execution = get_entry_point(); |
| 593 |
|
| 594 |
if (wg_x.id) |
| 595 |
{ |
| 596 |
if (options.vulkan_semantics) |
| 597 |
arguments.push_back(join("local_size_x_id = ", wg_x.constant_id)); |
| 598 |
else |
| 599 |
arguments.push_back(join("local_size_x = ", get<SPIRConstant>(wg_x.id).specialization_constant_macro_name)); |
| 600 |
} |
| 601 |
else |
| 602 |
arguments.push_back(join("local_size_x = ", execution.workgroup_size.x)); |
| 603 |
|
| 604 |
if (wg_y.id) |
| 605 |
{ |
| 606 |
if (options.vulkan_semantics) |
| 607 |
arguments.push_back(join("local_size_y_id = ", wg_y.constant_id)); |
| 608 |
else |
| 609 |
arguments.push_back(join("local_size_y = ", get<SPIRConstant>(wg_y.id).specialization_constant_macro_name)); |
| 610 |
} |
| 611 |
else |
| 612 |
arguments.push_back(join("local_size_y = ", execution.workgroup_size.y)); |
| 613 |
|
| 614 |
if (wg_z.id) |
| 615 |
{ |
| 616 |
if (options.vulkan_semantics) |
| 617 |
arguments.push_back(join("local_size_z_id = ", wg_z.constant_id)); |
| 618 |
else |
| 619 |
arguments.push_back(join("local_size_z = ", get<SPIRConstant>(wg_z.id).specialization_constant_macro_name)); |
| 620 |
} |
| 621 |
else |
| 622 |
arguments.push_back(join("local_size_z = ", execution.workgroup_size.z)); |
| 623 |
} |
| 624 |
|
| 625 |
void CompilerGLSL::request_subgroup_feature(ShaderSubgroupSupportHelper::Feature feature) |
| 626 |
{ |
| 627 |
if (options.vulkan_semantics) |
| 628 |
{ |
| 629 |
auto khr_extension = ShaderSubgroupSupportHelper::get_KHR_extension_for_feature(feature); |
| 630 |
require_extension_internal(ShaderSubgroupSupportHelper::get_extension_name(khr_extension)); |
| 631 |
} |
| 632 |
else |
| 633 |
{ |
| 634 |
if (!shader_subgroup_supporter.is_feature_requested(feature)) |
| 635 |
force_recompile(); |
| 636 |
shader_subgroup_supporter.request_feature(feature); |
| 637 |
} |
| 638 |
} |
| 639 |
|
| 640 |
void CompilerGLSL::emit_header() |
| 641 |
{ |
| 642 |
auto &execution = get_entry_point(); |
| 643 |
statement("#version ", options.version, options.es && options.version > 100 ? " es" : ""); |
| 644 |
|
| 645 |
if (!options.es && options.version < 420) |
| 646 |
{ |
| 647 |
// Needed for binding = # on UBOs, etc. |
| 648 |
if (options.enable_420pack_extension) |
| 649 |
{ |
| 650 |
statement("#ifdef GL_ARB_shading_language_420pack"); |
| 651 |
statement("#extension GL_ARB_shading_language_420pack : require"); |
| 652 |
statement("#endif"); |
| 653 |
} |
| 654 |
// Needed for: layout(early_fragment_tests) in; |
| 655 |
if (execution.flags.get(ExecutionModeEarlyFragmentTests)) |
| 656 |
require_extension_internal("GL_ARB_shader_image_load_store"); |
| 657 |
} |
| 658 |
|
| 659 |
// Needed for: layout(post_depth_coverage) in; |
| 660 |
if (execution.flags.get(ExecutionModePostDepthCoverage)) |
| 661 |
require_extension_internal("GL_ARB_post_depth_coverage"); |
| 662 |
|
| 663 |
// Needed for: layout({pixel,sample}_interlock_[un]ordered) in; |
| 664 |
if (execution.flags.get(ExecutionModePixelInterlockOrderedEXT) || |
| 665 |
execution.flags.get(ExecutionModePixelInterlockUnorderedEXT) || |
| 666 |
execution.flags.get(ExecutionModeSampleInterlockOrderedEXT) || |
| 667 |
execution.flags.get(ExecutionModeSampleInterlockUnorderedEXT)) |
| 668 |
{ |
| 669 |
if (options.es) |
| 670 |
{ |
| 671 |
if (options.version < 310) |
| 672 |
SPIRV_CROSS_THROW("At least ESSL 3.10 required for fragment shader interlock."); |
| 673 |
require_extension_internal("GL_NV_fragment_shader_interlock"); |
| 674 |
} |
| 675 |
else |
| 676 |
{ |
| 677 |
if (options.version < 420) |
| 678 |
require_extension_internal("GL_ARB_shader_image_load_store"); |
| 679 |
require_extension_internal("GL_ARB_fragment_shader_interlock"); |
| 680 |
} |
| 681 |
} |
| 682 |
|
| 683 |
for (auto &ext : forced_extensions) |
| 684 |
{ |
| 685 |
if (ext == "GL_EXT_shader_explicit_arithmetic_types_float16") |
| 686 |
{ |
| 687 |
// Special case, this extension has a potential fallback to another vendor extension in normal GLSL. |
| 688 |
// GL_AMD_gpu_shader_half_float is a superset, so try that first. |
| 689 |
statement("#if defined(GL_AMD_gpu_shader_half_float)"); |
| 690 |
statement("#extension GL_AMD_gpu_shader_half_float : require"); |
| 691 |
if (!options.vulkan_semantics) |
| 692 |
{ |
| 693 |
statement("#elif defined(GL_NV_gpu_shader5)"); |
| 694 |
statement("#extension GL_NV_gpu_shader5 : require"); |
| 695 |
} |
| 696 |
else |
| 697 |
{ |
| 698 |
statement("#elif defined(GL_EXT_shader_explicit_arithmetic_types_float16)"); |
| 699 |
statement("#extension GL_EXT_shader_explicit_arithmetic_types_float16 : require"); |
| 700 |
} |
| 701 |
statement("#else"); |
| 702 |
statement("#error No extension available for FP16."); |
| 703 |
statement("#endif"); |
| 704 |
} |
| 705 |
else if (ext == "GL_EXT_shader_explicit_arithmetic_types_int16") |
| 706 |
{ |
| 707 |
if (options.vulkan_semantics) |
| 708 |
statement("#extension GL_EXT_shader_explicit_arithmetic_types_int16 : require"); |
| 709 |
else |
| 710 |
{ |
| 711 |
statement("#if defined(GL_AMD_gpu_shader_int16)"); |
| 712 |
statement("#extension GL_AMD_gpu_shader_int16 : require"); |
| 713 |
statement("#else"); |
| 714 |
statement("#error No extension available for Int16."); |
| 715 |
statement("#endif"); |
| 716 |
} |
| 717 |
} |
| 718 |
else if (ext == "GL_ARB_post_depth_coverage") |
| 719 |
{ |
| 720 |
if (options.es) |
| 721 |
statement("#extension GL_EXT_post_depth_coverage : require"); |
| 722 |
else |
| 723 |
{ |
| 724 |
statement("#if defined(GL_ARB_post_depth_coverge)"); |
| 725 |
statement("#extension GL_ARB_post_depth_coverage : require"); |
| 726 |
statement("#else"); |
| 727 |
statement("#extension GL_EXT_post_depth_coverage : require"); |
| 728 |
statement("#endif"); |
| 729 |
} |
| 730 |
} |
| 731 |
else if (!options.vulkan_semantics && ext == "GL_ARB_shader_draw_parameters") |
| 732 |
{ |
| 733 |
// Soft-enable this extension on plain GLSL. |
| 734 |
statement("#ifdef ", ext); |
| 735 |
statement("#extension ", ext, " : enable"); |
| 736 |
statement("#endif"); |
| 737 |
} |
| 738 |
else |
| 739 |
statement("#extension ", ext, " : require"); |
| 740 |
} |
| 741 |
|
| 742 |
if (!options.vulkan_semantics) |
| 743 |
{ |
| 744 |
using Supp = ShaderSubgroupSupportHelper; |
| 745 |
auto result = shader_subgroup_supporter.resolve(); |
| 746 |
|
| 747 |
for (uint32_t feature_index = 0; feature_index < Supp::FeatureCount; feature_index++) |
| 748 |
{ |
| 749 |
auto feature = static_cast<Supp::Feature>(feature_index); |
| 750 |
if (!shader_subgroup_supporter.is_feature_requested(feature)) |
| 751 |
continue; |
| 752 |
|
| 753 |
auto exts = Supp::get_candidates_for_feature(feature, result); |
| 754 |
if (exts.empty()) |
| 755 |
continue; |
| 756 |
|
| 757 |
statement(""); |
| 758 |
|
| 759 |
for (auto &ext : exts) |
| 760 |
{ |
| 761 |
const char *name = Supp::get_extension_name(ext); |
| 762 |
const char *extra_predicate = Supp::get_extra_required_extension_predicate(ext); |
| 763 |
auto extra_names = Supp::get_extra_required_extension_names(ext); |
| 764 |
statement(&ext != &exts.front() ? "#elif" : "#if", " defined(", name, ")", |
| 765 |
(*extra_predicate != '\0' ? " && " : ""), extra_predicate); |
| 766 |
for (const auto &e : extra_names) |
| 767 |
statement("#extension ", e, " : enable"); |
| 768 |
statement("#extension ", name, " : require"); |
| 769 |
} |
| 770 |
|
| 771 |
if (!Supp::can_feature_be_implemented_without_extensions(feature)) |
| 772 |
{ |
| 773 |
statement("#else"); |
| 774 |
statement("#error No extensions available to emulate requested subgroup feature."); |
| 775 |
} |
| 776 |
|
| 777 |
statement("#endif"); |
| 778 |
} |
| 779 |
} |
| 780 |
|
| 781 |
for (auto &header : header_lines) |
| 782 |
statement(header); |
| 783 |
|
| 784 |
SmallVector<string> inputs; |
| 785 |
SmallVector<string> outputs; |
| 786 |
|
| 787 |
switch (execution.model) |
| 788 |
{ |
| 789 |
case ExecutionModelGeometry: |
| 790 |
if ((execution.flags.get(ExecutionModeInvocations)) && execution.invocations != 1) |
| 791 |
inputs.push_back(join("invocations = ", execution.invocations)); |
| 792 |
if (execution.flags.get(ExecutionModeInputPoints)) |
| 793 |
inputs.push_back("points"); |
| 794 |
if (execution.flags.get(ExecutionModeInputLines)) |
| 795 |
inputs.push_back("lines"); |
| 796 |
if (execution.flags.get(ExecutionModeInputLinesAdjacency)) |
| 797 |
inputs.push_back("lines_adjacency"); |
| 798 |
if (execution.flags.get(ExecutionModeTriangles)) |
| 799 |
inputs.push_back("triangles"); |
| 800 |
if (execution.flags.get(ExecutionModeInputTrianglesAdjacency)) |
| 801 |
inputs.push_back("triangles_adjacency"); |
| 802 |
|
| 803 |
if (!execution.geometry_passthrough) |
| 804 |
{ |
| 805 |
// For passthrough, these are implies and cannot be declared in shader. |
| 806 |
outputs.push_back(join("max_vertices = ", execution.output_vertices)); |
| 807 |
if (execution.flags.get(ExecutionModeOutputTriangleStrip)) |
| 808 |
outputs.push_back("triangle_strip"); |
| 809 |
if (execution.flags.get(ExecutionModeOutputPoints)) |
| 810 |
outputs.push_back("points"); |
| 811 |
if (execution.flags.get(ExecutionModeOutputLineStrip)) |
| 812 |
outputs.push_back("line_strip"); |
| 813 |
} |
| 814 |
break; |
| 815 |
|
| 816 |
case ExecutionModelTessellationControl: |
| 817 |
if (execution.flags.get(ExecutionModeOutputVertices)) |
| 818 |
outputs.push_back(join("vertices = ", execution.output_vertices)); |
| 819 |
break; |
| 820 |
|
| 821 |
case ExecutionModelTessellationEvaluation: |
| 822 |
if (execution.flags.get(ExecutionModeQuads)) |
| 823 |
inputs.push_back("quads"); |
| 824 |
if (execution.flags.get(ExecutionModeTriangles)) |
| 825 |
inputs.push_back("triangles"); |
| 826 |
if (execution.flags.get(ExecutionModeIsolines)) |
| 827 |
inputs.push_back("isolines"); |
| 828 |
if (execution.flags.get(ExecutionModePointMode)) |
| 829 |
inputs.push_back("point_mode"); |
| 830 |
|
| 831 |
if (!execution.flags.get(ExecutionModeIsolines)) |
| 832 |
{ |
| 833 |
if (execution.flags.get(ExecutionModeVertexOrderCw)) |
| 834 |
inputs.push_back("cw"); |
| 835 |
if (execution.flags.get(ExecutionModeVertexOrderCcw)) |
| 836 |
inputs.push_back("ccw"); |
| 837 |
} |
| 838 |
|
| 839 |
if (execution.flags.get(ExecutionModeSpacingFractionalEven)) |
| 840 |
inputs.push_back("fractional_even_spacing"); |
| 841 |
if (execution.flags.get(ExecutionModeSpacingFractionalOdd)) |
| 842 |
inputs.push_back("fractional_odd_spacing"); |
| 843 |
if (execution.flags.get(ExecutionModeSpacingEqual)) |
| 844 |
inputs.push_back("equal_spacing"); |
| 845 |
break; |
| 846 |
|
| 847 |
case ExecutionModelGLCompute: |
| 848 |
{ |
| 849 |
if (execution.workgroup_size.constant != 0) |
| 850 |
{ |
| 851 |
SpecializationConstant wg_x, wg_y, wg_z; |
| 852 |
get_work_group_size_specialization_constants(wg_x, wg_y, wg_z); |
| 853 |
|
| 854 |
// If there are any spec constants on legacy GLSL, defer declaration, we need to set up macro |
| 855 |
// declarations before we can emit the work group size. |
| 856 |
if (options.vulkan_semantics || |
| 857 |
((wg_x.id == ConstantID(0)) && (wg_y.id == ConstantID(0)) && (wg_z.id == ConstantID(0)))) |
| 858 |
build_workgroup_size(inputs, wg_x, wg_y, wg_z); |
| 859 |
} |
| 860 |
else |
| 861 |
{ |
| 862 |
inputs.push_back(join("local_size_x = ", execution.workgroup_size.x)); |
| 863 |
inputs.push_back(join("local_size_y = ", execution.workgroup_size.y)); |
| 864 |
inputs.push_back(join("local_size_z = ", execution.workgroup_size.z)); |
| 865 |
} |
| 866 |
break; |
| 867 |
} |
| 868 |
|
| 869 |
case ExecutionModelFragment: |
| 870 |
if (options.es) |
| 871 |
{ |
| 872 |
switch (options.fragment.default_float_precision) |
| 873 |
{ |
| 874 |
case Options::Lowp: |
| 875 |
statement("precision lowp float;"); |
| 876 |
break; |
| 877 |
|
| 878 |
case Options::Mediump: |
| 879 |
statement("precision mediump float;"); |
| 880 |
break; |
| 881 |
|
| 882 |
case Options::Highp: |
| 883 |
statement("precision highp float;"); |
| 884 |
break; |
| 885 |
|
| 886 |
default: |
| 887 |
break; |
| 888 |
} |
| 889 |
|
| 890 |
switch (options.fragment.default_int_precision) |
| 891 |
{ |
| 892 |
case Options::Lowp: |
| 893 |
statement("precision lowp int;"); |
| 894 |
break; |
| 895 |
|
| 896 |
case Options::Mediump: |
| 897 |
statement("precision mediump int;"); |
| 898 |
break; |
| 899 |
|
| 900 |
case Options::Highp: |
| 901 |
statement("precision highp int;"); |
| 902 |
break; |
| 903 |
|
| 904 |
default: |
| 905 |
break; |
| 906 |
} |
| 907 |
} |
| 908 |
|
| 909 |
if (execution.flags.get(ExecutionModeEarlyFragmentTests)) |
| 910 |
inputs.push_back("early_fragment_tests"); |
| 911 |
if (execution.flags.get(ExecutionModePostDepthCoverage)) |
| 912 |
inputs.push_back("post_depth_coverage"); |
| 913 |
|
| 914 |
if (execution.flags.get(ExecutionModePixelInterlockOrderedEXT)) |
| 915 |
inputs.push_back("pixel_interlock_ordered"); |
| 916 |
else if (execution.flags.get(ExecutionModePixelInterlockUnorderedEXT)) |
| 917 |
inputs.push_back("pixel_interlock_unordered"); |
| 918 |
else if (execution.flags.get(ExecutionModeSampleInterlockOrderedEXT)) |
| 919 |
inputs.push_back("sample_interlock_ordered"); |
| 920 |
else if (execution.flags.get(ExecutionModeSampleInterlockUnorderedEXT)) |
| 921 |
inputs.push_back("sample_interlock_unordered"); |
| 922 |
|
| 923 |
if (!options.es && execution.flags.get(ExecutionModeDepthGreater)) |
| 924 |
statement("layout(depth_greater) out float gl_FragDepth;"); |
| 925 |
else if (!options.es && execution.flags.get(ExecutionModeDepthLess)) |
| 926 |
statement("layout(depth_less) out float gl_FragDepth;"); |
| 927 |
|
| 928 |
break; |
| 929 |
|
| 930 |
default: |
| 931 |
break; |
| 932 |
} |
| 933 |
|
| 934 |
if (!inputs.empty()) |
| 935 |
statement("layout(", merge(inputs), ") in;"); |
| 936 |
if (!outputs.empty()) |
| 937 |
statement("layout(", merge(outputs), ") out;"); |
| 938 |
|
| 939 |
statement(""); |
| 940 |
} |
| 941 |
|
| 942 |
bool CompilerGLSL::type_is_empty(const SPIRType &type) |
| 943 |
{ |
| 944 |
return type.basetype == SPIRType::Struct && type.member_types.empty(); |
| 945 |
} |
| 946 |
|
| 947 |
void CompilerGLSL::emit_struct(SPIRType &type) |
| 948 |
{ |
| 949 |
// Struct types can be stamped out multiple times |
| 950 |
// with just different offsets, matrix layouts, etc ... |
| 951 |
// Type-punning with these types is legal, which complicates things |
| 952 |
// when we are storing struct and array types in an SSBO for example. |
| 953 |
// If the type master is packed however, we can no longer assume that the struct declaration will be redundant. |
| 954 |
if (type.type_alias != TypeID(0) && |
| 955 |
!has_extended_decoration(type.type_alias, SPIRVCrossDecorationBufferBlockRepacked)) |
| 956 |
return; |
| 957 |
|
| 958 |
add_resource_name(type.self); |
| 959 |
auto name = type_to_glsl(type); |
| 960 |
|
| 961 |
statement(!backend.explicit_struct_type ? "struct " : "", name); |
| 962 |
begin_scope(); |
| 963 |
|
| 964 |
type.member_name_cache.clear(); |
| 965 |
|
| 966 |
uint32_t i = 0; |
| 967 |
bool emitted = false; |
| 968 |
for (auto &member : type.member_types) |
| 969 |
{ |
| 970 |
add_member_name(type, i); |
| 971 |
emit_struct_member(type, member, i); |
| 972 |
i++; |
| 973 |
emitted = true; |
| 974 |
} |
| 975 |
|
| 976 |
// Don't declare empty structs in GLSL, this is not allowed. |
| 977 |
if (type_is_empty(type) && !backend.supports_empty_struct) |
| 978 |
{ |
| 979 |
statement("int empty_struct_member;"); |
| 980 |
emitted = true; |
| 981 |
} |
| 982 |
|
| 983 |
if (has_extended_decoration(type.self, SPIRVCrossDecorationPaddingTarget)) |
| 984 |
emit_struct_padding_target(type); |
| 985 |
|
| 986 |
end_scope_decl(); |
| 987 |
|
| 988 |
if (emitted) |
| 989 |
statement(""); |
| 990 |
} |
| 991 |
|
| 992 |
string CompilerGLSL::to_interpolation_qualifiers(const Bitset &flags) |
| 993 |
{ |
| 994 |
string res; |
| 995 |
//if (flags & (1ull << DecorationSmooth)) |
| 996 |
// res += "smooth "; |
| 997 |
if (flags.get(DecorationFlat)) |
| 998 |
res += "flat "; |
| 999 |
if (flags.get(DecorationNoPerspective)) |
| 1000 |
res += "noperspective "; |
| 1001 |
if (flags.get(DecorationCentroid)) |
| 1002 |
res += "centroid "; |
| 1003 |
if (flags.get(DecorationPatch)) |
| 1004 |
res += "patch "; |
| 1005 |
if (flags.get(DecorationSample)) |
| 1006 |
res += "sample "; |
| 1007 |
if (flags.get(DecorationInvariant)) |
| 1008 |
res += "invariant "; |
| 1009 |
if (flags.get(DecorationExplicitInterpAMD)) |
| 1010 |
res += "__explicitInterpAMD "; |
| 1011 |
|
| 1012 |
return res; |
| 1013 |
} |
| 1014 |
|
| 1015 |
string CompilerGLSL::layout_for_member(const SPIRType &type, uint32_t index) |
| 1016 |
{ |
| 1017 |
if (is_legacy()) |
| 1018 |
return ""; |
| 1019 |
|
| 1020 |
bool is_block = ir.meta[type.self].decoration.decoration_flags.get(DecorationBlock) || |
| 1021 |
ir.meta[type.self].decoration.decoration_flags.get(DecorationBufferBlock); |
| 1022 |
if (!is_block) |
| 1023 |
return ""; |
| 1024 |
|
| 1025 |
auto &memb = ir.meta[type.self].members; |
| 1026 |
if (index >= memb.size()) |
| 1027 |
return ""; |
| 1028 |
auto &dec = memb[index]; |
| 1029 |
|
| 1030 |
SmallVector<string> attr; |
| 1031 |
|
| 1032 |
if (has_member_decoration(type.self, index, DecorationPassthroughNV)) |
| 1033 |
attr.push_back("passthrough"); |
| 1034 |
|
| 1035 |
// We can only apply layouts on members in block interfaces. |
| 1036 |
// This is a bit problematic because in SPIR-V decorations are applied on the struct types directly. |
| 1037 |
// This is not supported on GLSL, so we have to make the assumption that if a struct within our buffer block struct |
| 1038 |
// has a decoration, it was originally caused by a top-level layout() qualifier in GLSL. |
| 1039 |
// |
| 1040 |
// We would like to go from (SPIR-V style): |
| 1041 |
// |
| 1042 |
// struct Foo { layout(row_major) mat4 matrix; }; |
| 1043 |
// buffer UBO { Foo foo; }; |
| 1044 |
// |
| 1045 |
// to |
| 1046 |
// |
| 1047 |
// struct Foo { mat4 matrix; }; // GLSL doesn't support any layout shenanigans in raw struct declarations. |
| 1048 |
// buffer UBO { layout(row_major) Foo foo; }; // Apply the layout on top-level. |
| 1049 |
auto flags = combined_decoration_for_member(type, index); |
| 1050 |
|
| 1051 |
if (flags.get(DecorationRowMajor)) |
| 1052 |
attr.push_back("row_major"); |
| 1053 |
// We don't emit any global layouts, so column_major is default. |
| 1054 |
//if (flags & (1ull << DecorationColMajor)) |
| 1055 |
// attr.push_back("column_major"); |
| 1056 |
|
| 1057 |
if (dec.decoration_flags.get(DecorationLocation) && can_use_io_location(type.storage, true)) |
| 1058 |
attr.push_back(join("location = ", dec.location)); |
| 1059 |
|
| 1060 |
// Can only declare component if we can declare location. |
| 1061 |
if (dec.decoration_flags.get(DecorationComponent) && can_use_io_location(type.storage, true)) |
| 1062 |
{ |
| 1063 |
if (!options.es) |
| 1064 |
{ |
| 1065 |
if (options.version < 440 && options.version >= 140) |
| 1066 |
require_extension_internal("GL_ARB_enhanced_layouts"); |
| 1067 |
else if (options.version < 140) |
| 1068 |
SPIRV_CROSS_THROW("Component decoration is not supported in targets below GLSL 1.40."); |
| 1069 |
attr.push_back(join("component = ", dec.component)); |
| 1070 |
} |
| 1071 |
else |
| 1072 |
SPIRV_CROSS_THROW("Component decoration is not supported in ES targets."); |
| 1073 |
} |
| 1074 |
|
| 1075 |
// SPIRVCrossDecorationPacked is set by layout_for_variable earlier to mark that we need to emit offset qualifiers. |
| 1076 |
// This is only done selectively in GLSL as needed. |
| 1077 |
if (has_extended_decoration(type.self, SPIRVCrossDecorationExplicitOffset) && |
| 1078 |
dec.decoration_flags.get(DecorationOffset)) |
| 1079 |
attr.push_back(join("offset = ", dec.offset)); |
| 1080 |
else if (type.storage == StorageClassOutput && dec.decoration_flags.get(DecorationOffset)) |
| 1081 |
attr.push_back(join("xfb_offset = ", dec.offset)); |
| 1082 |
|
| 1083 |
if (attr.empty()) |
| 1084 |
return ""; |
| 1085 |
|
| 1086 |
string res = "layout("; |
| 1087 |
res += merge(attr); |
| 1088 |
res += ") "; |
| 1089 |
return res; |
| 1090 |
} |
| 1091 |
|
| 1092 |
const char *CompilerGLSL::format_to_glsl(spv::ImageFormat format) |
| 1093 |
{ |
| 1094 |
if (options.es && is_desktop_only_format(format)) |
| 1095 |
SPIRV_CROSS_THROW("Attempting to use image format not supported in ES profile."); |
| 1096 |
|
| 1097 |
switch (format) |
| 1098 |
{ |
| 1099 |
case ImageFormatRgba32f: |
| 1100 |
return "rgba32f"; |
| 1101 |
case ImageFormatRgba16f: |
| 1102 |
return "rgba16f"; |
| 1103 |
case ImageFormatR32f: |
| 1104 |
return "r32f"; |
| 1105 |
case ImageFormatRgba8: |
| 1106 |
return "rgba8"; |
| 1107 |
case ImageFormatRgba8Snorm: |
| 1108 |
return "rgba8_snorm"; |
| 1109 |
case ImageFormatRg32f: |
| 1110 |
return "rg32f"; |
| 1111 |
case ImageFormatRg16f: |
| 1112 |
return "rg16f"; |
| 1113 |
case ImageFormatRgba32i: |
| 1114 |
return "rgba32i"; |
| 1115 |
case ImageFormatRgba16i: |
| 1116 |
return "rgba16i"; |
| 1117 |
case ImageFormatR32i: |
| 1118 |
return "r32i"; |
| 1119 |
case ImageFormatRgba8i: |
| 1120 |
return "rgba8i"; |
| 1121 |
case ImageFormatRg32i: |
| 1122 |
return "rg32i"; |
| 1123 |
case ImageFormatRg16i: |
| 1124 |
return "rg16i"; |
| 1125 |
case ImageFormatRgba32ui: |
| 1126 |
return "rgba32ui"; |
| 1127 |
case ImageFormatRgba16ui: |
| 1128 |
return "rgba16ui"; |
| 1129 |
case ImageFormatR32ui: |
| 1130 |
return "r32ui"; |
| 1131 |
case ImageFormatRgba8ui: |
| 1132 |
return "rgba8ui"; |
| 1133 |
case ImageFormatRg32ui: |
| 1134 |
return "rg32ui"; |
| 1135 |
case ImageFormatRg16ui: |
| 1136 |
return "rg16ui"; |
| 1137 |
case ImageFormatR11fG11fB10f: |
| 1138 |
return "r11f_g11f_b10f"; |
| 1139 |
case ImageFormatR16f: |
| 1140 |
return "r16f"; |
| 1141 |
case ImageFormatRgb10A2: |
| 1142 |
return "rgb10_a2"; |
| 1143 |
case ImageFormatR8: |
| 1144 |
return "r8"; |
| 1145 |
case ImageFormatRg8: |
| 1146 |
return "rg8"; |
| 1147 |
case ImageFormatR16: |
| 1148 |
return "r16"; |
| 1149 |
case ImageFormatRg16: |
| 1150 |
return "rg16"; |
| 1151 |
case ImageFormatRgba16: |
| 1152 |
return "rgba16"; |
| 1153 |
case ImageFormatR16Snorm: |
| 1154 |
return "r16_snorm"; |
| 1155 |
case ImageFormatRg16Snorm: |
| 1156 |
return "rg16_snorm"; |
| 1157 |
case ImageFormatRgba16Snorm: |
| 1158 |
return "rgba16_snorm"; |
| 1159 |
case ImageFormatR8Snorm: |
| 1160 |
return "r8_snorm"; |
| 1161 |
case ImageFormatRg8Snorm: |
| 1162 |
return "rg8_snorm"; |
| 1163 |
case ImageFormatR8ui: |
| 1164 |
return "r8ui"; |
| 1165 |
case ImageFormatRg8ui: |
| 1166 |
return "rg8ui"; |
| 1167 |
case ImageFormatR16ui: |
| 1168 |
return "r16ui"; |
| 1169 |
case ImageFormatRgb10a2ui: |
| 1170 |
return "rgb10_a2ui"; |
| 1171 |
case ImageFormatR8i: |
| 1172 |
return "r8i"; |
| 1173 |
case ImageFormatRg8i: |
| 1174 |
return "rg8i"; |
| 1175 |
case ImageFormatR16i: |
| 1176 |
return "r16i"; |
| 1177 |
default: |
| 1178 |
case ImageFormatUnknown: |
| 1179 |
return nullptr; |
| 1180 |
} |
| 1181 |
} |
| 1182 |
|
| 1183 |
uint32_t CompilerGLSL::type_to_packed_base_size(const SPIRType &type, BufferPackingStandard) |
| 1184 |
{ |
| 1185 |
switch (type.basetype) |
| 1186 |
{ |
| 1187 |
case SPIRType::Double: |
| 1188 |
case SPIRType::Int64: |
| 1189 |
case SPIRType::UInt64: |
| 1190 |
return 8; |
| 1191 |
case SPIRType::Float: |
| 1192 |
case SPIRType::Int: |
| 1193 |
case SPIRType::UInt: |
| 1194 |
return 4; |
| 1195 |
case SPIRType::Half: |
| 1196 |
case SPIRType::Short: |
| 1197 |
case SPIRType::UShort: |
| 1198 |
return 2; |
| 1199 |
case SPIRType::SByte: |
| 1200 |
case SPIRType::UByte: |
| 1201 |
return 1; |
| 1202 |
|
| 1203 |
default: |
| 1204 |
SPIRV_CROSS_THROW("Unrecognized type in type_to_packed_base_size."); |
| 1205 |
} |
| 1206 |
} |
| 1207 |
|
| 1208 |
uint32_t CompilerGLSL::type_to_packed_alignment(const SPIRType &type, const Bitset &flags, |
| 1209 |
BufferPackingStandard packing) |
| 1210 |
{ |
| 1211 |
// If using PhysicalStorageBufferEXT storage class, this is a pointer, |
| 1212 |
// and is 64-bit. |
| 1213 |
if (type.storage == StorageClassPhysicalStorageBufferEXT) |
| 1214 |
{ |
| 1215 |
if (!type.pointer) |
| 1216 |
SPIRV_CROSS_THROW("Types in PhysicalStorageBufferEXT must be pointers."); |
| 1217 |
|
| 1218 |
if (ir.addressing_model == AddressingModelPhysicalStorageBuffer64EXT) |
| 1219 |
{ |
| 1220 |
if (packing_is_vec4_padded(packing) && type_is_array_of_pointers(type)) |
| 1221 |
return 16; |
| 1222 |
else |
| 1223 |
return 8; |
| 1224 |
} |
| 1225 |
else |
| 1226 |
SPIRV_CROSS_THROW("AddressingModelPhysicalStorageBuffer64EXT must be used for PhysicalStorageBufferEXT."); |
| 1227 |
} |
| 1228 |
|
| 1229 |
if (!type.array.empty()) |
| 1230 |
{ |
| 1231 |
uint32_t minimum_alignment = 1; |
| 1232 |
if (packing_is_vec4_padded(packing)) |
| 1233 |
minimum_alignment = 16; |
| 1234 |
|
| 1235 |
auto *tmp = &get<SPIRType>(type.parent_type); |
| 1236 |
while (!tmp->array.empty()) |
| 1237 |
tmp = &get<SPIRType>(tmp->parent_type); |
| 1238 |
|
| 1239 |
// Get the alignment of the base type, then maybe round up. |
| 1240 |
return max(minimum_alignment, type_to_packed_alignment(*tmp, flags, packing)); |
| 1241 |
} |
| 1242 |
|
| 1243 |
if (type.basetype == SPIRType::Struct) |
| 1244 |
{ |
| 1245 |
// Rule 9. Structs alignments are maximum alignment of its members. |
| 1246 |
uint32_t alignment = 1; |
| 1247 |
for (uint32_t i = 0; i < type.member_types.size(); i++) |
| 1248 |
{ |
| 1249 |
auto member_flags = ir.meta[type.self].members[i].decoration_flags; |
| 1250 |
alignment = |
| 1251 |
max(alignment, type_to_packed_alignment(get<SPIRType>(type.member_types[i]), member_flags, packing)); |
| 1252 |
} |
| 1253 |
|
| 1254 |
// In std140, struct alignment is rounded up to 16. |
| 1255 |
if (packing_is_vec4_padded(packing)) |
| 1256 |
alignment = max(alignment, 16u); |
| 1257 |
|
| 1258 |
return alignment; |
| 1259 |
} |
| 1260 |
else |
| 1261 |
{ |
| 1262 |
const uint32_t base_alignment = type_to_packed_base_size(type, packing); |
| 1263 |
|
| 1264 |
// Alignment requirement for scalar block layout is always the alignment for the most basic component. |
| 1265 |
if (packing_is_scalar(packing)) |
| 1266 |
return base_alignment; |
| 1267 |
|
| 1268 |
// Vectors are *not* aligned in HLSL, but there's an extra rule where vectors cannot straddle |
| 1269 |
// a vec4, this is handled outside since that part knows our current offset. |
| 1270 |
if (type.columns == 1 && packing_is_hlsl(packing)) |
| 1271 |
return base_alignment; |
| 1272 |
|
| 1273 |
// From 7.6.2.2 in GL 4.5 core spec. |
| 1274 |
// Rule 1 |
| 1275 |
if (type.vecsize == 1 && type.columns == 1) |
| 1276 |
return base_alignment; |
| 1277 |
|
| 1278 |
// Rule 2 |
| 1279 |
if ((type.vecsize == 2 || type.vecsize == 4) && type.columns == 1) |
| 1280 |
return type.vecsize * base_alignment; |
| 1281 |
|
| 1282 |
// Rule 3 |
| 1283 |
if (type.vecsize == 3 && type.columns == 1) |
| 1284 |
return 4 * base_alignment; |
| 1285 |
|
| 1286 |
// Rule 4 implied. Alignment does not change in std430. |
| 1287 |
|
| 1288 |
// Rule 5. Column-major matrices are stored as arrays of |
| 1289 |
// vectors. |
| 1290 |
if (flags.get(DecorationColMajor) && type.columns > 1) |
| 1291 |
{ |
| 1292 |
if (packing_is_vec4_padded(packing)) |
| 1293 |
return 4 * base_alignment; |
| 1294 |
else if (type.vecsize == 3) |
| 1295 |
return 4 * base_alignment; |
| 1296 |
else |
| 1297 |
return type.vecsize * base_alignment; |
| 1298 |
} |
| 1299 |
|
| 1300 |
// Rule 6 implied. |
| 1301 |
|
| 1302 |
// Rule 7. |
| 1303 |
if (flags.get(DecorationRowMajor) && type.vecsize > 1) |
| 1304 |
{ |
| 1305 |
if (packing_is_vec4_padded(packing)) |
| 1306 |
return 4 * base_alignment; |
| 1307 |
else if (type.columns == 3) |
| 1308 |
return 4 * base_alignment; |
| 1309 |
else |
| 1310 |
return type.columns * base_alignment; |
| 1311 |
} |
| 1312 |
|
| 1313 |
// Rule 8 implied. |
| 1314 |
} |
| 1315 |
|
| 1316 |
SPIRV_CROSS_THROW("Did not find suitable rule for type. Bogus decorations?"); |
| 1317 |
} |
| 1318 |
|
| 1319 |
uint32_t CompilerGLSL::type_to_packed_array_stride(const SPIRType &type, const Bitset &flags, |
| 1320 |
BufferPackingStandard packing) |
| 1321 |
{ |
| 1322 |
// Array stride is equal to aligned size of the underlying type. |
| 1323 |
uint32_t parent = type.parent_type; |
| 1324 |
assert(parent); |
| 1325 |
|
| 1326 |
auto &tmp = get<SPIRType>(parent); |
| 1327 |
|
| 1328 |
uint32_t size = type_to_packed_size(tmp, flags, packing); |
| 1329 |
uint32_t alignment = type_to_packed_alignment(type, flags, packing); |
| 1330 |
return (size + alignment - 1) & ~(alignment - 1); |
| 1331 |
} |
| 1332 |
|
| 1333 |
uint32_t CompilerGLSL::type_to_packed_size(const SPIRType &type, const Bitset &flags, BufferPackingStandard packing) |
| 1334 |
{ |
| 1335 |
if (!type.array.empty()) |
| 1336 |
{ |
| 1337 |
uint32_t packed_size = to_array_size_literal(type) * type_to_packed_array_stride(type, flags, packing); |
| 1338 |
|
| 1339 |
// For arrays of vectors and matrices in HLSL, the last element has a size which depends on its vector size, |
| 1340 |
// so that it is possible to pack other vectors into the last element. |
| 1341 |
if (packing_is_hlsl(packing) && type.basetype != SPIRType::Struct) |
| 1342 |
packed_size -= (4 - type.vecsize) * (type.width / 8); |
| 1343 |
|
| 1344 |
return packed_size; |
| 1345 |
} |
| 1346 |
|
| 1347 |
// If using PhysicalStorageBufferEXT storage class, this is a pointer, |
| 1348 |
// and is 64-bit. |
| 1349 |
if (type.storage == StorageClassPhysicalStorageBufferEXT) |
| 1350 |
{ |
| 1351 |
if (!type.pointer) |
| 1352 |
SPIRV_CROSS_THROW("Types in PhysicalStorageBufferEXT must be pointers."); |
| 1353 |
|
| 1354 |
if (ir.addressing_model == AddressingModelPhysicalStorageBuffer64EXT) |
| 1355 |
return 8; |
| 1356 |
else |
| 1357 |
SPIRV_CROSS_THROW("AddressingModelPhysicalStorageBuffer64EXT must be used for PhysicalStorageBufferEXT."); |
| 1358 |
} |
| 1359 |
|
| 1360 |
uint32_t size = 0; |
| 1361 |
|
| 1362 |
if (type.basetype == SPIRType::Struct) |
| 1363 |
{ |
| 1364 |
uint32_t pad_alignment = 1; |
| 1365 |
|
| 1366 |
for (uint32_t i = 0; i < type.member_types.size(); i++) |
| 1367 |
{ |
| 1368 |
auto member_flags = ir.meta[type.self].members[i].decoration_flags; |
| 1369 |
auto &member_type = get<SPIRType>(type.member_types[i]); |
| 1370 |
|
| 1371 |
uint32_t packed_alignment = type_to_packed_alignment(member_type, member_flags, packing); |
| 1372 |
uint32_t alignment = max(packed_alignment, pad_alignment); |
| 1373 |
|
| 1374 |
// The next member following a struct member is aligned to the base alignment of the struct that came before. |
| 1375 |
// GL 4.5 spec, 7.6.2.2. |
| 1376 |
if (member_type.basetype == SPIRType::Struct) |
| 1377 |
pad_alignment = packed_alignment; |
| 1378 |
else |
| 1379 |
pad_alignment = 1; |
| 1380 |
|
| 1381 |
size = (size + alignment - 1) & ~(alignment - 1); |
| 1382 |
size += type_to_packed_size(member_type, member_flags, packing); |
| 1383 |
} |
| 1384 |
} |
| 1385 |
else |
| 1386 |
{ |
| 1387 |
const uint32_t base_alignment = type_to_packed_base_size(type, packing); |
| 1388 |
|
| 1389 |
if (packing_is_scalar(packing)) |
| 1390 |
{ |
| 1391 |
size = type.vecsize * type.columns * base_alignment; |
| 1392 |
} |
| 1393 |
else |
| 1394 |
{ |
| 1395 |
if (type.columns == 1) |
| 1396 |
size = type.vecsize * base_alignment; |
| 1397 |
|
| 1398 |
if (flags.get(DecorationColMajor) && type.columns > 1) |
| 1399 |
{ |
| 1400 |
if (packing_is_vec4_padded(packing)) |
| 1401 |
size = type.columns * 4 * base_alignment; |
| 1402 |
else if (type.vecsize == 3) |
| 1403 |
size = type.columns * 4 * base_alignment; |
| 1404 |
else |
| 1405 |
size = type.columns * type.vecsize * base_alignment; |
| 1406 |
} |
| 1407 |
|
| 1408 |
if (flags.get(DecorationRowMajor) && type.vecsize > 1) |
| 1409 |
{ |
| 1410 |
if (packing_is_vec4_padded(packing)) |
| 1411 |
size = type.vecsize * 4 * base_alignment; |
| 1412 |
else if (type.columns == 3) |
| 1413 |
size = type.vecsize * 4 * base_alignment; |
| 1414 |
else |
| 1415 |
size = type.vecsize * type.columns * base_alignment; |
| 1416 |
} |
| 1417 |
|
| 1418 |
// For matrices in HLSL, the last element has a size which depends on its vector size, |
| 1419 |
// so that it is possible to pack other vectors into the last element. |
| 1420 |
if (packing_is_hlsl(packing) && type.columns > 1) |
| 1421 |
size -= (4 - type.vecsize) * (type.width / 8); |
| 1422 |
} |
| 1423 |
} |
| 1424 |
|
| 1425 |
return size; |
| 1426 |
} |
| 1427 |
|
| 1428 |
bool CompilerGLSL::buffer_is_packing_standard(const SPIRType &type, BufferPackingStandard packing, |
| 1429 |
uint32_t *failed_validation_index, uint32_t start_offset, |
| 1430 |
uint32_t end_offset) |
| 1431 |
{ |
| 1432 |
// This is very tricky and error prone, but try to be exhaustive and correct here. |
| 1433 |
// SPIR-V doesn't directly say if we're using std430 or std140. |
| 1434 |
// SPIR-V communicates this using Offset and ArrayStride decorations (which is what really matters), |
| 1435 |
// so we have to try to infer whether or not the original GLSL source was std140 or std430 based on this information. |
| 1436 |
// We do not have to consider shared or packed since these layouts are not allowed in Vulkan SPIR-V (they are useless anyways, and custom offsets would do the same thing). |
| 1437 |
// |
| 1438 |
// It is almost certain that we're using std430, but it gets tricky with arrays in particular. |
| 1439 |
// We will assume std430, but infer std140 if we can prove the struct is not compliant with std430. |
| 1440 |
// |
| 1441 |
// The only two differences between std140 and std430 are related to padding alignment/array stride |
| 1442 |
// in arrays and structs. In std140 they take minimum vec4 alignment. |
| 1443 |
// std430 only removes the vec4 requirement. |
| 1444 |
|
| 1445 |
uint32_t offset = 0; |
| 1446 |
uint32_t pad_alignment = 1; |
| 1447 |
|
| 1448 |
bool is_top_level_block = |
| 1449 |
has_decoration(type.self, DecorationBlock) || has_decoration(type.self, DecorationBufferBlock); |
| 1450 |
|
| 1451 |
for (uint32_t i = 0; i < type.member_types.size(); i++) |
| 1452 |
{ |
| 1453 |
auto &memb_type = get<SPIRType>(type.member_types[i]); |
| 1454 |
auto member_flags = ir.meta[type.self].members[i].decoration_flags; |
| 1455 |
|
| 1456 |
// Verify alignment rules. |
| 1457 |
uint32_t packed_alignment = type_to_packed_alignment(memb_type, member_flags, packing); |
| 1458 |
|
| 1459 |
// This is a rather dirty workaround to deal with some cases of OpSpecConstantOp used as array size, e.g: |
| 1460 |
// layout(constant_id = 0) const int s = 10; |
| 1461 |
// const int S = s + 5; // SpecConstantOp |
| 1462 |
// buffer Foo { int data[S]; }; // <-- Very hard for us to deduce a fixed value here, |
| 1463 |
// we would need full implementation of compile-time constant folding. :( |
| 1464 |
// If we are the last member of a struct, there might be cases where the actual size of that member is irrelevant |
| 1465 |
// for our analysis (e.g. unsized arrays). |
| 1466 |
// This lets us simply ignore that there are spec constant op sized arrays in our buffers. |
| 1467 |
// Querying size of this member will fail, so just don't call it unless we have to. |
| 1468 |
// |
| 1469 |
// This is likely "best effort" we can support without going into unacceptably complicated workarounds. |
| 1470 |
bool member_can_be_unsized = |
| 1471 |
is_top_level_block && size_t(i + 1) == type.member_types.size() && !memb_type.array.empty(); |
| 1472 |
|
| 1473 |
uint32_t packed_size = 0; |
| 1474 |
if (!member_can_be_unsized || packing_is_hlsl(packing)) |
| 1475 |
packed_size = type_to_packed_size(memb_type, member_flags, packing); |
| 1476 |
|
| 1477 |
// We only need to care about this if we have non-array types which can straddle the vec4 boundary. |
| 1478 |
if (packing_is_hlsl(packing)) |
| 1479 |
{ |
| 1480 |
// If a member straddles across a vec4 boundary, alignment is actually vec4. |
| 1481 |
uint32_t begin_word = offset / 16; |
| 1482 |
uint32_t end_word = (offset + packed_size - 1) / 16; |
| 1483 |
if (begin_word != end_word) |
| 1484 |
packed_alignment = max(packed_alignment, 16u); |
| 1485 |
} |
| 1486 |
|
| 1487 |
uint32_t actual_offset = type_struct_member_offset(type, i); |
| 1488 |
// Field is not in the specified range anymore and we can ignore any further fields. |
| 1489 |
if (actual_offset >= end_offset) |
| 1490 |
break; |
| 1491 |
|
| 1492 |
uint32_t alignment = max(packed_alignment, pad_alignment); |
| 1493 |
offset = (offset + alignment - 1) & ~(alignment - 1); |
| 1494 |
|
| 1495 |
// The next member following a struct member is aligned to the base alignment of the struct that came before. |
| 1496 |
// GL 4.5 spec, 7.6.2.2. |
| 1497 |
if (memb_type.basetype == SPIRType::Struct && !memb_type.pointer) |
| 1498 |
pad_alignment = packed_alignment; |
| 1499 |
else |
| 1500 |
pad_alignment = 1; |
| 1501 |
|
| 1502 |
// Only care about packing if we are in the given range |
| 1503 |
if (actual_offset >= start_offset) |
| 1504 |
{ |
| 1505 |
// We only care about offsets in std140, std430, etc ... |
| 1506 |
// For EnhancedLayout variants, we have the flexibility to choose our own offsets. |
| 1507 |
if (!packing_has_flexible_offset(packing)) |
| 1508 |
{ |
| 1509 |
if (actual_offset != offset) // This cannot be the packing we're looking for. |
| 1510 |
{ |
| 1511 |
if (failed_validation_index) |
| 1512 |
*failed_validation_index = i; |
| 1513 |
return false; |
| 1514 |
} |
| 1515 |
} |
| 1516 |
else if ((actual_offset & (alignment - 1)) != 0) |
| 1517 |
{ |
| 1518 |
// We still need to verify that alignment rules are observed, even if we have explicit offset. |
| 1519 |
if (failed_validation_index) |
| 1520 |
*failed_validation_index = i; |
| 1521 |
return false; |
| 1522 |
} |
| 1523 |
|
| 1524 |
// Verify array stride rules. |
| 1525 |
if (!memb_type.array.empty() && type_to_packed_array_stride(memb_type, member_flags, packing) != |
| 1526 |
type_struct_member_array_stride(type, i)) |
| 1527 |
{ |
| 1528 |
if (failed_validation_index) |
| 1529 |
*failed_validation_index = i; |
| 1530 |
return false; |
| 1531 |
} |
| 1532 |
|
| 1533 |
// Verify that sub-structs also follow packing rules. |
| 1534 |
// We cannot use enhanced layouts on substructs, so they better be up to spec. |
| 1535 |
auto substruct_packing = packing_to_substruct_packing(packing); |
| 1536 |
|
| 1537 |
if (!memb_type.pointer && !memb_type.member_types.empty() && |
| 1538 |
!buffer_is_packing_standard(memb_type, substruct_packing)) |
| 1539 |
{ |
| 1540 |
if (failed_validation_index) |
| 1541 |
*failed_validation_index = i; |
| 1542 |
return false; |
| 1543 |
} |
| 1544 |
} |
| 1545 |
|
| 1546 |
// Bump size. |
| 1547 |
offset = actual_offset + packed_size; |
| 1548 |
} |
| 1549 |
|
| 1550 |
return true; |
| 1551 |
} |
| 1552 |
|
| 1553 |
bool CompilerGLSL::can_use_io_location(StorageClass storage, bool block) |
| 1554 |
{ |
| 1555 |
// Location specifiers are must have in SPIR-V, but they aren't really supported in earlier versions of GLSL. |
| 1556 |
// Be very explicit here about how to solve the issue. |
| 1557 |
if ((get_execution_model() != ExecutionModelVertex && storage == StorageClassInput) || |
| 1558 |
(get_execution_model() != ExecutionModelFragment && storage == StorageClassOutput)) |
| 1559 |
{ |
| 1560 |
uint32_t minimum_desktop_version = block ? 440 : 410; |
| 1561 |
// ARB_enhanced_layouts vs ARB_separate_shader_objects ... |
| 1562 |
|
| 1563 |
if (!options.es && options.version < minimum_desktop_version && !options.separate_shader_objects) |
| 1564 |
return false; |
| 1565 |
else if (options.es && options.version < 310) |
| 1566 |
return false; |
| 1567 |
} |
| 1568 |
|
| 1569 |
if ((get_execution_model() == ExecutionModelVertex && storage == StorageClassInput) || |
| 1570 |
(get_execution_model() == ExecutionModelFragment && storage == StorageClassOutput)) |
| 1571 |
{ |
| 1572 |
if (options.es && options.version < 300) |
| 1573 |
return false; |
| 1574 |
else if (!options.es && options.version < 330) |
| 1575 |
return false; |
| 1576 |
} |
| 1577 |
|
| 1578 |
if (storage == StorageClassUniform || storage == StorageClassUniformConstant || storage == StorageClassPushConstant) |
| 1579 |
{ |
| 1580 |
if (options.es && options.version < 310) |
| 1581 |
return false; |
| 1582 |
else if (!options.es && options.version < 430) |
| 1583 |
return false; |
| 1584 |
} |
| 1585 |
|
| 1586 |
return true; |
| 1587 |
} |
| 1588 |
|
| 1589 |
string CompilerGLSL::layout_for_variable(const SPIRVariable &var) |
| 1590 |
{ |
| 1591 |
// FIXME: Come up with a better solution for when to disable layouts. |
| 1592 |
// Having layouts depend on extensions as well as which types |
| 1593 |
// of layouts are used. For now, the simple solution is to just disable |
| 1594 |
// layouts for legacy versions. |
| 1595 |
if (is_legacy()) |
| 1596 |
return ""; |
| 1597 |
|
| 1598 |
if (subpass_input_is_framebuffer_fetch(var.self)) |
| 1599 |
return ""; |
| 1600 |
|
| 1601 |
SmallVector<string> attr; |
| 1602 |
|
| 1603 |
auto &type = get<SPIRType>(var.basetype); |
| 1604 |
auto &flags = get_decoration_bitset(var.self); |
| 1605 |
auto &typeflags = get_decoration_bitset(type.self); |
| 1606 |
|
| 1607 |
if (flags.get(DecorationPassthroughNV)) |
| 1608 |
attr.push_back("passthrough"); |
| 1609 |
|
| 1610 |
if (options.vulkan_semantics && var.storage == StorageClassPushConstant) |
| 1611 |
attr.push_back("push_constant"); |
| 1612 |
else if (var.storage == StorageClassShaderRecordBufferNV) |
| 1613 |
attr.push_back("shaderRecordNV"); |
| 1614 |
|
| 1615 |
if (flags.get(DecorationRowMajor)) |
| 1616 |
attr.push_back("row_major"); |
| 1617 |
if (flags.get(DecorationColMajor)) |
| 1618 |
attr.push_back("column_major"); |
| 1619 |
|
| 1620 |
if (options.vulkan_semantics) |
| 1621 |
{ |
| 1622 |
if (flags.get(DecorationInputAttachmentIndex)) |
| 1623 |
attr.push_back(join("input_attachment_index = ", get_decoration(var.self, DecorationInputAttachmentIndex))); |
| 1624 |
} |
| 1625 |
|
| 1626 |
bool is_block = has_decoration(type.self, DecorationBlock); |
| 1627 |
if (flags.get(DecorationLocation) && can_use_io_location(var.storage, is_block)) |
| 1628 |
{ |
| 1629 |
Bitset combined_decoration; |
| 1630 |
for (uint32_t i = 0; i < ir.meta[type.self].members.size(); i++) |
| 1631 |
combined_decoration.merge_or(combined_decoration_for_member(type, i)); |
| 1632 |
|
| 1633 |
// If our members have location decorations, we don't need to |
| 1634 |
// emit location decorations at the top as well (looks weird). |
| 1635 |
if (!combined_decoration.get(DecorationLocation)) |
| 1636 |
attr.push_back(join("location = ", get_decoration(var.self, DecorationLocation))); |
| 1637 |
} |
| 1638 |
|
| 1639 |
// Transform feedback |
| 1640 |
bool uses_enhanced_layouts = false; |
| 1641 |
if (is_block && var.storage == StorageClassOutput) |
| 1642 |
{ |
| 1643 |
// For blocks, there is a restriction where xfb_stride/xfb_buffer must only be declared on the block itself, |
| 1644 |
// since all members must match the same xfb_buffer. The only thing we will declare for members of the block |
| 1645 |
// is the xfb_offset. |
| 1646 |
uint32_t member_count = uint32_t(type.member_types.size()); |
| 1647 |
bool have_xfb_buffer_stride = false; |
| 1648 |
bool have_any_xfb_offset = false; |
| 1649 |
bool have_geom_stream = false; |
| 1650 |
uint32_t xfb_stride = 0, xfb_buffer = 0, geom_stream = 0; |
| 1651 |
|
| 1652 |
if (flags.get(DecorationXfbBuffer) && flags.get(DecorationXfbStride)) |
| 1653 |
{ |
| 1654 |
have_xfb_buffer_stride = true; |
| 1655 |
xfb_buffer = get_decoration(var.self, DecorationXfbBuffer); |
| 1656 |
xfb_stride = get_decoration(var.self, DecorationXfbStride); |
| 1657 |
} |
| 1658 |
|
| 1659 |
if (flags.get(DecorationStream)) |
| 1660 |
{ |
| 1661 |
have_geom_stream = true; |
| 1662 |
geom_stream = get_decoration(var.self, DecorationStream); |
| 1663 |
} |
| 1664 |
|
| 1665 |
// Verify that none of the members violate our assumption. |
| 1666 |
for (uint32_t i = 0; i < member_count; i++) |
| 1667 |
{ |
| 1668 |
if (has_member_decoration(type.self, i, DecorationStream)) |
| 1669 |
{ |
| 1670 |
uint32_t member_geom_stream = get_member_decoration(type.self, i, DecorationStream); |
| 1671 |
if (have_geom_stream && member_geom_stream != geom_stream) |
| 1672 |
SPIRV_CROSS_THROW("IO block member Stream mismatch."); |
| 1673 |
have_geom_stream = true; |
| 1674 |
geom_stream = member_geom_stream; |
| 1675 |
} |
| 1676 |
|
| 1677 |
// Only members with an Offset decoration participate in XFB. |
| 1678 |
if (!has_member_decoration(type.self, i, DecorationOffset)) |
| 1679 |
continue; |
| 1680 |
have_any_xfb_offset = true; |
| 1681 |
|
| 1682 |
if (has_member_decoration(type.self, i, DecorationXfbBuffer)) |
| 1683 |
{ |
| 1684 |
uint32_t buffer_index = get_member_decoration(type.self, i, DecorationXfbBuffer); |
| 1685 |
if (have_xfb_buffer_stride && buffer_index != xfb_buffer) |
| 1686 |
SPIRV_CROSS_THROW("IO block member XfbBuffer mismatch."); |
| 1687 |
have_xfb_buffer_stride = true; |
| 1688 |
xfb_buffer = buffer_index; |
| 1689 |
} |
| 1690 |
|
| 1691 |
if (has_member_decoration(type.self, i, DecorationXfbStride)) |
| 1692 |
{ |
| 1693 |
uint32_t stride = get_member_decoration(type.self, i, DecorationXfbStride); |
| 1694 |
if (have_xfb_buffer_stride && stride != xfb_stride) |
| 1695 |
SPIRV_CROSS_THROW("IO block member XfbStride mismatch."); |
| 1696 |
have_xfb_buffer_stride = true; |
| 1697 |
xfb_stride = stride; |
| 1698 |
} |
| 1699 |
} |
| 1700 |
|
| 1701 |
if (have_xfb_buffer_stride && have_any_xfb_offset) |
| 1702 |
{ |
| 1703 |
attr.push_back(join("xfb_buffer = ", xfb_buffer)); |
| 1704 |
attr.push_back(join("xfb_stride = ", xfb_stride)); |
| 1705 |
uses_enhanced_layouts = true; |
| 1706 |
} |
| 1707 |
|
| 1708 |
if (have_geom_stream) |
| 1709 |
{ |
| 1710 |
if (get_execution_model() != ExecutionModelGeometry) |
| 1711 |
SPIRV_CROSS_THROW("Geometry streams can only be used in geometry shaders."); |
| 1712 |
if (options.es) |
| 1713 |
SPIRV_CROSS_THROW("Multiple geometry streams not supported in ESSL."); |
| 1714 |
if (options.version < 400) |
| 1715 |
require_extension_internal("GL_ARB_transform_feedback3"); |
| 1716 |
attr.push_back(join("stream = ", get_decoration(var.self, DecorationStream))); |
| 1717 |
} |
| 1718 |
} |
| 1719 |
else if (var.storage == StorageClassOutput) |
| 1720 |
{ |
| 1721 |
if (flags.get(DecorationXfbBuffer) && flags.get(DecorationXfbStride) && flags.get(DecorationOffset)) |
| 1722 |
{ |
| 1723 |
// XFB for standalone variables, we can emit all decorations. |
| 1724 |
attr.push_back(join("xfb_buffer = ", get_decoration(var.self, DecorationXfbBuffer))); |
| 1725 |
attr.push_back(join("xfb_stride = ", get_decoration(var.self, DecorationXfbStride))); |
| 1726 |
attr.push_back(join("xfb_offset = ", get_decoration(var.self, DecorationOffset))); |
| 1727 |
uses_enhanced_layouts = true; |
| 1728 |
} |
| 1729 |
|
| 1730 |
if (flags.get(DecorationStream)) |
| 1731 |
{ |
| 1732 |
if (get_execution_model() != ExecutionModelGeometry) |
| 1733 |
SPIRV_CROSS_THROW("Geometry streams can only be used in geometry shaders."); |
| 1734 |
if (options.es) |
| 1735 |
SPIRV_CROSS_THROW("Multiple geometry streams not supported in ESSL."); |
| 1736 |
if (options.version < 400) |
| 1737 |
require_extension_internal("GL_ARB_transform_feedback3"); |
| 1738 |
attr.push_back(join("stream = ", get_decoration(var.self, DecorationStream))); |
| 1739 |
} |
| 1740 |
} |
| 1741 |
|
| 1742 |
// Can only declare Component if we can declare location. |
| 1743 |
if (flags.get(DecorationComponent) && can_use_io_location(var.storage, is_block)) |
| 1744 |
{ |
| 1745 |
uses_enhanced_layouts = true; |
| 1746 |
attr.push_back(join("component = ", get_decoration(var.self, DecorationComponent))); |
| 1747 |
} |
| 1748 |
|
| 1749 |
if (uses_enhanced_layouts) |
| 1750 |
{ |
| 1751 |
if (!options.es) |
| 1752 |
{ |
| 1753 |
if (options.version < 440 && options.version >= 140) |
| 1754 |
require_extension_internal("GL_ARB_enhanced_layouts"); |
| 1755 |
else if (options.version < 140) |
| 1756 |
SPIRV_CROSS_THROW("GL_ARB_enhanced_layouts is not supported in targets below GLSL 1.40."); |
| 1757 |
if (!options.es && options.version < 440) |
| 1758 |
require_extension_internal("GL_ARB_enhanced_layouts"); |
| 1759 |
} |
| 1760 |
else if (options.es) |
| 1761 |
SPIRV_CROSS_THROW("GL_ARB_enhanced_layouts is not supported in ESSL."); |
| 1762 |
} |
| 1763 |
|
| 1764 |
if (flags.get(DecorationIndex)) |
| 1765 |
attr.push_back(join("index = ", get_decoration(var.self, DecorationIndex))); |
| 1766 |
|
| 1767 |
// Do not emit set = decoration in regular GLSL output, but |
| 1768 |
// we need to preserve it in Vulkan GLSL mode. |
| 1769 |
if (var.storage != StorageClassPushConstant && var.storage != StorageClassShaderRecordBufferNV) |
| 1770 |
{ |
| 1771 |
if (flags.get(DecorationDescriptorSet) && options.vulkan_semantics) |
| 1772 |
attr.push_back(join("set = ", get_decoration(var.self, DecorationDescriptorSet))); |
| 1773 |
} |
| 1774 |
|
| 1775 |
bool push_constant_block = options.vulkan_semantics && var.storage == StorageClassPushConstant; |
| 1776 |
bool ssbo_block = var.storage == StorageClassStorageBuffer || var.storage == StorageClassShaderRecordBufferNV || |
| 1777 |
(var.storage == StorageClassUniform && typeflags.get(DecorationBufferBlock)); |
| 1778 |
bool emulated_ubo = var.storage == StorageClassPushConstant && options.emit_push_constant_as_uniform_buffer; |
| 1779 |
bool ubo_block = var.storage == StorageClassUniform && typeflags.get(DecorationBlock); |
| 1780 |
|
| 1781 |
// GL 3.0/GLSL 1.30 is not considered legacy, but it doesn't have UBOs ... |
| 1782 |
bool can_use_buffer_blocks = (options.es && options.version >= 300) || (!options.es && options.version >= 140); |
| 1783 |
|
| 1784 |
// pretend no UBOs when options say so |
| 1785 |
if (ubo_block && options.emit_uniform_buffer_as_plain_uniforms) |
| 1786 |
can_use_buffer_blocks = false; |
| 1787 |
|
| 1788 |
bool can_use_binding; |
| 1789 |
if (options.es) |
| 1790 |
can_use_binding = options.version >= 310; |
| 1791 |
else |
| 1792 |
can_use_binding = options.enable_420pack_extension || (options.version >= 420); |
| 1793 |
|
| 1794 |
// Make sure we don't emit binding layout for a classic uniform on GLSL 1.30. |
| 1795 |
if (!can_use_buffer_blocks && var.storage == StorageClassUniform) |
| 1796 |
can_use_binding = false; |
| 1797 |
|
| 1798 |
if (var.storage == StorageClassShaderRecordBufferNV) |
| 1799 |
can_use_binding = false; |
| 1800 |
|
| 1801 |
if (can_use_binding && flags.get(DecorationBinding)) |
| 1802 |
attr.push_back(join("binding = ", get_decoration(var.self, DecorationBinding))); |
| 1803 |
|
| 1804 |
if (var.storage != StorageClassOutput && flags.get(DecorationOffset)) |
| 1805 |
attr.push_back(join("offset = ", get_decoration(var.self, DecorationOffset))); |
| 1806 |
|
| 1807 |
// Instead of adding explicit offsets for every element here, just assume we're using std140 or std430. |
| 1808 |
// If SPIR-V does not comply with either layout, we cannot really work around it. |
| 1809 |
if (can_use_buffer_blocks && (ubo_block || emulated_ubo)) |
| 1810 |
{ |
| 1811 |
attr.push_back(buffer_to_packing_standard(type, false)); |
| 1812 |
} |
| 1813 |
else if (can_use_buffer_blocks && (push_constant_block || ssbo_block)) |
| 1814 |
{ |
| 1815 |
attr.push_back(buffer_to_packing_standard(type, true)); |
| 1816 |
} |
| 1817 |
|
| 1818 |
// For images, the type itself adds a layout qualifer. |
| 1819 |
// Only emit the format for storage images. |
| 1820 |
if (type.basetype == SPIRType::Image && type.image.sampled == 2) |
| 1821 |
{ |
| 1822 |
const char *fmt = format_to_glsl(type.image.format); |
| 1823 |
if (fmt) |
| 1824 |
attr.push_back(fmt); |
| 1825 |
} |
| 1826 |
|
| 1827 |
if (attr.empty()) |
| 1828 |
return ""; |
| 1829 |
|
| 1830 |
string res = "layout("; |
| 1831 |
res += merge(attr); |
| 1832 |
res += ") "; |
| 1833 |
return res; |
| 1834 |
} |
| 1835 |
|
| 1836 |
string CompilerGLSL::buffer_to_packing_standard(const SPIRType &type, bool support_std430_without_scalar_layout) |
| 1837 |
{ |
| 1838 |
if (support_std430_without_scalar_layout && buffer_is_packing_standard(type, BufferPackingStd430)) |
| 1839 |
return "std430"; |
| 1840 |
else if (buffer_is_packing_standard(type, BufferPackingStd140)) |
| 1841 |
return "std140"; |
| 1842 |
else if (options.vulkan_semantics && buffer_is_packing_standard(type, BufferPackingScalar)) |
| 1843 |
{ |
| 1844 |
require_extension_internal("GL_EXT_scalar_block_layout"); |
| 1845 |
return "scalar"; |
| 1846 |
} |
| 1847 |
else if (support_std430_without_scalar_layout && |
| 1848 |
buffer_is_packing_standard(type, BufferPackingStd430EnhancedLayout)) |
| 1849 |
{ |
| 1850 |
if (options.es && !options.vulkan_semantics) |
| 1851 |
SPIRV_CROSS_THROW("Push constant block cannot be expressed as neither std430 nor std140. ES-targets do " |
| 1852 |
"not support GL_ARB_enhanced_layouts."); |
| 1853 |
if (!options.es && !options.vulkan_semantics && options.version < 440) |
| 1854 |
require_extension_internal("GL_ARB_enhanced_layouts"); |
| 1855 |
|
| 1856 |
set_extended_decoration(type.self, SPIRVCrossDecorationExplicitOffset); |
| 1857 |
return "std430"; |
| 1858 |
} |
| 1859 |
else if (buffer_is_packing_standard(type, BufferPackingStd140EnhancedLayout)) |
| 1860 |
{ |
| 1861 |
// Fallback time. We might be able to use the ARB_enhanced_layouts to deal with this difference, |
| 1862 |
// however, we can only use layout(offset) on the block itself, not any substructs, so the substructs better be the appropriate layout. |
| 1863 |
// Enhanced layouts seem to always work in Vulkan GLSL, so no need for extensions there. |
| 1864 |
if (options.es && !options.vulkan_semantics) |
| 1865 |
SPIRV_CROSS_THROW("Push constant block cannot be expressed as neither std430 nor std140. ES-targets do " |
| 1866 |
"not support GL_ARB_enhanced_layouts."); |
| 1867 |
if (!options.es && !options.vulkan_semantics && options.version < 440) |
| 1868 |
require_extension_internal("GL_ARB_enhanced_layouts"); |
| 1869 |
|
| 1870 |
set_extended_decoration(type.self, SPIRVCrossDecorationExplicitOffset); |
| 1871 |
return "std140"; |
| 1872 |
} |
| 1873 |
else if (options.vulkan_semantics && buffer_is_packing_standard(type, BufferPackingScalarEnhancedLayout)) |
| 1874 |
{ |
| 1875 |
set_extended_decoration(type.self, SPIRVCrossDecorationExplicitOffset); |
| 1876 |
require_extension_internal("GL_EXT_scalar_block_layout"); |
| 1877 |
return "scalar"; |
| 1878 |
} |
| 1879 |
else if (!support_std430_without_scalar_layout && options.vulkan_semantics && |
| 1880 |
buffer_is_packing_standard(type, BufferPackingStd430)) |
| 1881 |
{ |
| 1882 |
// UBOs can support std430 with GL_EXT_scalar_block_layout. |
| 1883 |
require_extension_internal("GL_EXT_scalar_block_layout"); |
| 1884 |
return "std430"; |
| 1885 |
} |
| 1886 |
else if (!support_std430_without_scalar_layout && options.vulkan_semantics && |
| 1887 |
buffer_is_packing_standard(type, BufferPackingStd430EnhancedLayout)) |
| 1888 |
{ |
| 1889 |
// UBOs can support std430 with GL_EXT_scalar_block_layout. |
| 1890 |
set_extended_decoration(type.self, SPIRVCrossDecorationExplicitOffset); |
| 1891 |
require_extension_internal("GL_EXT_scalar_block_layout"); |
| 1892 |
return "std430"; |
| 1893 |
} |
| 1894 |
else |
| 1895 |
{ |
| 1896 |
SPIRV_CROSS_THROW("Buffer block cannot be expressed as any of std430, std140, scalar, even with enhanced " |
| 1897 |
"layouts. You can try flattening this block to support a more flexible layout."); |
| 1898 |
} |
| 1899 |
} |
| 1900 |
|
| 1901 |
void CompilerGLSL::emit_push_constant_block(const SPIRVariable &var) |
| 1902 |
{ |
| 1903 |
if (flattened_buffer_blocks.count(var.self)) |
| 1904 |
emit_buffer_block_flattened(var); |
| 1905 |
else if (options.vulkan_semantics) |
| 1906 |
emit_push_constant_block_vulkan(var); |
| 1907 |
else if (options.emit_push_constant_as_uniform_buffer) |
| 1908 |
emit_buffer_block_native(var); |
| 1909 |
else |
| 1910 |
emit_push_constant_block_glsl(var); |
| 1911 |
} |
| 1912 |
|
| 1913 |
void CompilerGLSL::emit_push_constant_block_vulkan(const SPIRVariable &var) |
| 1914 |
{ |
| 1915 |
emit_buffer_block(var); |
| 1916 |
} |
| 1917 |
|
| 1918 |
void CompilerGLSL::emit_push_constant_block_glsl(const SPIRVariable &var) |
| 1919 |
{ |
| 1920 |
// OpenGL has no concept of push constant blocks, implement it as a uniform struct. |
| 1921 |
auto &type = get<SPIRType>(var.basetype); |
| 1922 |
|
| 1923 |
auto &flags = ir.meta[var.self].decoration.decoration_flags; |
| 1924 |
flags.clear(DecorationBinding); |
| 1925 |
flags.clear(DecorationDescriptorSet); |
| 1926 |
|
| 1927 |
#if 0 |
| 1928 |
if (flags & ((1ull << DecorationBinding) | (1ull << DecorationDescriptorSet))) |
| 1929 |
SPIRV_CROSS_THROW("Push constant blocks cannot be compiled to GLSL with Binding or Set syntax. " |
| 1930 |
"Remap to location with reflection API first or disable these decorations."); |
| 1931 |
#endif |
| 1932 |
|
| 1933 |
// We're emitting the push constant block as a regular struct, so disable the block qualifier temporarily. |
| 1934 |
// Otherwise, we will end up emitting layout() qualifiers on naked structs which is not allowed. |
| 1935 |
auto &block_flags = ir.meta[type.self].decoration.decoration_flags; |
| 1936 |
bool block_flag = block_flags.get(DecorationBlock); |
| 1937 |
block_flags.clear(DecorationBlock); |
| 1938 |
|
| 1939 |
emit_struct(type); |
| 1940 |
|
| 1941 |
if (block_flag) |
| 1942 |
block_flags.set(DecorationBlock); |
| 1943 |
|
| 1944 |
emit_uniform(var); |
| 1945 |
statement(""); |
| 1946 |
} |
| 1947 |
|
| 1948 |
void CompilerGLSL::emit_buffer_block(const SPIRVariable &var) |
| 1949 |
{ |
| 1950 |
auto &type = get<SPIRType>(var.basetype); |
| 1951 |
bool ubo_block = var.storage == StorageClassUniform && has_decoration(type.self, DecorationBlock); |
| 1952 |
|
| 1953 |
if (flattened_buffer_blocks.count(var.self)) |
| 1954 |
emit_buffer_block_flattened(var); |
| 1955 |
else if (is_legacy() || (!options.es && options.version == 130) || |
| 1956 |
(ubo_block && options.emit_uniform_buffer_as_plain_uniforms)) |
| 1957 |
emit_buffer_block_legacy(var); |
| 1958 |
else |
| 1959 |
emit_buffer_block_native(var); |
| 1960 |
} |
| 1961 |
|
| 1962 |
void CompilerGLSL::emit_buffer_block_legacy(const SPIRVariable &var) |
| 1963 |
{ |
| 1964 |
auto &type = get<SPIRType>(var.basetype); |
| 1965 |
bool ssbo = var.storage == StorageClassStorageBuffer || |
| 1966 |
ir.meta[type.self].decoration.decoration_flags.get(DecorationBufferBlock); |
| 1967 |
if (ssbo) |
| 1968 |
SPIRV_CROSS_THROW("SSBOs not supported in legacy targets."); |
| 1969 |
|
| 1970 |
// We're emitting the push constant block as a regular struct, so disable the block qualifier temporarily. |
| 1971 |
// Otherwise, we will end up emitting layout() qualifiers on naked structs which is not allowed. |
| 1972 |
auto &block_flags = ir.meta[type.self].decoration.decoration_flags; |
| 1973 |
bool block_flag = block_flags.get(DecorationBlock); |
| 1974 |
block_flags.clear(DecorationBlock); |
| 1975 |
emit_struct(type); |
| 1976 |
if (block_flag) |
| 1977 |
block_flags.set(DecorationBlock); |
| 1978 |
emit_uniform(var); |
| 1979 |
statement(""); |
| 1980 |
} |
| 1981 |
|
| 1982 |
void CompilerGLSL::emit_buffer_reference_block(SPIRType &type, bool forward_declaration) |
| 1983 |
{ |
| 1984 |
string buffer_name; |
| 1985 |
|
| 1986 |
if (forward_declaration) |
| 1987 |
{ |
| 1988 |
// Block names should never alias, but from HLSL input they kind of can because block types are reused for UAVs ... |
| 1989 |
// Allow aliased name since we might be declaring the block twice. Once with buffer reference (forward declared) and one proper declaration. |
| 1990 |
// The names must match up. |
| 1991 |
buffer_name = to_name(type.self, false); |
| 1992 |
|
| 1993 |
// Shaders never use the block by interface name, so we don't |
| 1994 |
// have to track this other than updating name caches. |
| 1995 |
// If we have a collision for any reason, just fallback immediately. |
| 1996 |
if (ir.meta[type.self].decoration.alias.empty() || |
| 1997 |
block_ssbo_names.find(buffer_name) != end(block_ssbo_names) || |
| 1998 |
resource_names.find(buffer_name) != end(resource_names)) |
| 1999 |
{ |
| 2000 |
buffer_name = join("_", type.self); |
| 2001 |
} |
| 2002 |
|
| 2003 |
// Make sure we get something unique for both global name scope and block name scope. |
| 2004 |
// See GLSL 4.5 spec: section 4.3.9 for details. |
| 2005 |
add_variable(block_ssbo_names, resource_names, buffer_name); |
| 2006 |
|
| 2007 |
// If for some reason buffer_name is an illegal name, make a final fallback to a workaround name. |
| 2008 |
// This cannot conflict with anything else, so we're safe now. |
| 2009 |
// We cannot reuse this fallback name in neither global scope (blocked by block_names) nor block name scope. |
| 2010 |
if (buffer_name.empty()) |
| 2011 |
buffer_name = join("_", type.self); |
| 2012 |
|
| 2013 |
block_names.insert(buffer_name); |
| 2014 |
block_ssbo_names.insert(buffer_name); |
| 2015 |
} |
| 2016 |
else if (type.basetype != SPIRType::Struct) |
| 2017 |
buffer_name = type_to_glsl(type); |
| 2018 |
else |
| 2019 |
buffer_name = to_name(type.self, false); |
| 2020 |
|
| 2021 |
if (!forward_declaration) |
| 2022 |
{ |
| 2023 |
if (type.basetype == SPIRType::Struct) |
| 2024 |
statement("layout(buffer_reference, ", buffer_to_packing_standard(type, true), ") buffer ", buffer_name); |
| 2025 |
else |
| 2026 |
statement("layout(buffer_reference) buffer ", buffer_name); |
| 2027 |
|
| 2028 |
begin_scope(); |
| 2029 |
|
| 2030 |
if (type.basetype == SPIRType::Struct) |
| 2031 |
{ |
| 2032 |
type.member_name_cache.clear(); |
| 2033 |
|
| 2034 |
uint32_t i = 0; |
| 2035 |
for (auto &member : type.member_types) |
| 2036 |
{ |
| 2037 |
add_member_name(type, i); |
| 2038 |
emit_struct_member(type, member, i); |
| 2039 |
i++; |
| 2040 |
} |
| 2041 |
} |
| 2042 |
else |
| 2043 |
{ |
| 2044 |
auto &pointee_type = get_pointee_type(type); |
| 2045 |
statement(type_to_glsl(pointee_type), " value", type_to_array_glsl(pointee_type), ";"); |
| 2046 |
} |
| 2047 |
|
| 2048 |
end_scope_decl(); |
| 2049 |
statement(""); |
| 2050 |
} |
| 2051 |
else |
| 2052 |
{ |
| 2053 |
statement("layout(buffer_reference) buffer ", buffer_name, ";"); |
| 2054 |
} |
| 2055 |
} |
| 2056 |
|
| 2057 |
void CompilerGLSL::emit_buffer_block_native(const SPIRVariable &var) |
| 2058 |
{ |
| 2059 |
auto &type = get<SPIRType>(var.basetype); |
| 2060 |
|
| 2061 |
Bitset flags = ir.get_buffer_block_flags(var); |
| 2062 |
bool ssbo = var.storage == StorageClassStorageBuffer || var.storage == StorageClassShaderRecordBufferNV || |
| 2063 |
ir.meta[type.self].decoration.decoration_flags.get(DecorationBufferBlock); |
| 2064 |
bool is_restrict = ssbo && flags.get(DecorationRestrict); |
| 2065 |
bool is_writeonly = ssbo && flags.get(DecorationNonReadable); |
| 2066 |
bool is_readonly = ssbo && flags.get(DecorationNonWritable); |
| 2067 |
bool is_coherent = ssbo && flags.get(DecorationCoherent); |
| 2068 |
|
| 2069 |
// Block names should never alias, but from HLSL input they kind of can because block types are reused for UAVs ... |
| 2070 |
auto buffer_name = to_name(type.self, false); |
| 2071 |
|
| 2072 |
auto &block_namespace = ssbo ? block_ssbo_names : block_ubo_names; |
| 2073 |
|
| 2074 |
// Shaders never use the block by interface name, so we don't |
| 2075 |
// have to track this other than updating name caches. |
| 2076 |
// If we have a collision for any reason, just fallback immediately. |
| 2077 |
if (ir.meta[type.self].decoration.alias.empty() || block_namespace.find(buffer_name) != end(block_namespace) || |
| 2078 |
resource_names.find(buffer_name) != end(resource_names)) |
| 2079 |
{ |
| 2080 |
buffer_name = get_block_fallback_name(var.self); |
| 2081 |
} |
| 2082 |
|
| 2083 |
// Make sure we get something unique for both global name scope and block name scope. |
| 2084 |
// See GLSL 4.5 spec: section 4.3.9 for details. |
| 2085 |
add_variable(block_namespace, resource_names, buffer_name); |
| 2086 |
|
| 2087 |
// If for some reason buffer_name is an illegal name, make a final fallback to a workaround name. |
| 2088 |
// This cannot conflict with anything else, so we're safe now. |
| 2089 |
// We cannot reuse this fallback name in neither global scope (blocked by block_names) nor block name scope. |
| 2090 |
if (buffer_name.empty()) |
| 2091 |
buffer_name = join("_", get<SPIRType>(var.basetype).self, "_", var.self); |
| 2092 |
|
| 2093 |
block_names.insert(buffer_name); |
| 2094 |
block_namespace.insert(buffer_name); |
| 2095 |
|
| 2096 |
// Save for post-reflection later. |
| 2097 |
declared_block_names[var.self] = buffer_name; |
| 2098 |
|
| 2099 |
statement(layout_for_variable(var), is_coherent ? "coherent " : "", is_restrict ? "restrict " : "", |
| 2100 |
is_writeonly ? "writeonly " : "", is_readonly ? "readonly " : "", ssbo ? "buffer " : "uniform ", |
| 2101 |
buffer_name); |
| 2102 |
|
| 2103 |
begin_scope(); |
| 2104 |
|
| 2105 |
type.member_name_cache.clear(); |
| 2106 |
|
| 2107 |
uint32_t i = 0; |
| 2108 |
for (auto &member : type.member_types) |
| 2109 |
{ |
| 2110 |
add_member_name(type, i); |
| 2111 |
emit_struct_member(type, member, i); |
| 2112 |
i++; |
| 2113 |
} |
| 2114 |
|
| 2115 |
// var.self can be used as a backup name for the block name, |
| 2116 |
// so we need to make sure we don't disturb the name here on a recompile. |
| 2117 |
// It will need to be reset if we have to recompile. |
| 2118 |
preserve_alias_on_reset(var.self); |
| 2119 |
add_resource_name(var.self); |
| 2120 |
end_scope_decl(to_name(var.self) + type_to_array_glsl(type)); |
| 2121 |
statement(""); |
| 2122 |
} |
| 2123 |
|
| 2124 |
void CompilerGLSL::emit_buffer_block_flattened(const SPIRVariable &var) |
| 2125 |
{ |
| 2126 |
auto &type = get<SPIRType>(var.basetype); |
| 2127 |
|
| 2128 |
// Block names should never alias. |
| 2129 |
auto buffer_name = to_name(type.self, false); |
| 2130 |
size_t buffer_size = (get_declared_struct_size(type) + 15) / 16; |
| 2131 |
|
| 2132 |
SPIRType::BaseType basic_type; |
| 2133 |
if (get_common_basic_type(type, basic_type)) |
| 2134 |
{ |
| 2135 |
SPIRType tmp; |
| 2136 |
tmp.basetype = basic_type; |
| 2137 |
tmp.vecsize = 4; |
| 2138 |
if (basic_type != SPIRType::Float && basic_type != SPIRType::Int && basic_type != SPIRType::UInt) |
| 2139 |
SPIRV_CROSS_THROW("Basic types in a flattened UBO must be float, int or uint."); |
| 2140 |
|
| 2141 |
auto flags = ir.get_buffer_block_flags(var); |
| 2142 |
statement("uniform ", flags_to_qualifiers_glsl(tmp, flags), type_to_glsl(tmp), " ", buffer_name, "[", |
| 2143 |
buffer_size, "];"); |
| 2144 |
} |
| 2145 |
else |
| 2146 |
SPIRV_CROSS_THROW("All basic types in a flattened block must be the same."); |
| 2147 |
} |
| 2148 |
|
| 2149 |
const char *CompilerGLSL::to_storage_qualifiers_glsl(const SPIRVariable &var) |
| 2150 |
{ |
| 2151 |
auto &execution = get_entry_point(); |
| 2152 |
|
| 2153 |
if (subpass_input_is_framebuffer_fetch(var.self)) |
| 2154 |
return ""; |
| 2155 |
|
| 2156 |
if (var.storage == StorageClassInput || var.storage == StorageClassOutput) |
| 2157 |
{ |
| 2158 |
if (is_legacy() && execution.model == ExecutionModelVertex) |
| 2159 |
return var.storage == StorageClassInput ? "attribute " : "varying "; |
| 2160 |
else if (is_legacy() && execution.model == ExecutionModelFragment) |
| 2161 |
return "varying "; // Fragment outputs are renamed so they never hit this case. |
| 2162 |
else if (execution.model == ExecutionModelFragment && var.storage == StorageClassOutput) |
| 2163 |
{ |
| 2164 |
if (inout_color_attachments.count(get_decoration(var.self, DecorationLocation)) != 0) |
| 2165 |
return "inout "; |
| 2166 |
else |
| 2167 |
return "out "; |
| 2168 |
} |
| 2169 |
else |
| 2170 |
return var.storage == StorageClassInput ? "in " : "out "; |
| 2171 |
} |
| 2172 |
else if (var.storage == StorageClassUniformConstant || var.storage == StorageClassUniform || |
| 2173 |
var.storage == StorageClassPushConstant) |
| 2174 |
{ |
| 2175 |
return "uniform "; |
| 2176 |
} |
| 2177 |
else if (var.storage == StorageClassRayPayloadNV) |
| 2178 |
{ |
| 2179 |
return "rayPayloadNV "; |
| 2180 |
} |
| 2181 |
else if (var.storage == StorageClassIncomingRayPayloadNV) |
| 2182 |
{ |
| 2183 |
return "rayPayloadInNV "; |
| 2184 |
} |
| 2185 |
else if (var.storage == StorageClassHitAttributeNV) |
| 2186 |
{ |
| 2187 |
return "hitAttributeNV "; |
| 2188 |
} |
| 2189 |
else if (var.storage == StorageClassCallableDataNV) |
| 2190 |
{ |
| 2191 |
return "callableDataNV "; |
| 2192 |
} |
| 2193 |
else if (var.storage == StorageClassIncomingCallableDataNV) |
| 2194 |
{ |
| 2195 |
return "callableDataInNV "; |
| 2196 |
} |
| 2197 |
|
| 2198 |
return ""; |
| 2199 |
} |
| 2200 |
|
| 2201 |
void CompilerGLSL::emit_flattened_io_block_member(const std::string &basename, const SPIRType &type, const char *qual, |
| 2202 |
const SmallVector<uint32_t> &indices) |
| 2203 |
{ |
| 2204 |
uint32_t member_type_id = type.self; |
| 2205 |
const SPIRType *member_type = &type; |
| 2206 |
const SPIRType *parent_type = nullptr; |
| 2207 |
auto flattened_name = basename; |
| 2208 |
for (auto &index : indices) |
| 2209 |
{ |
| 2210 |
flattened_name += "_"; |
| 2211 |
flattened_name += to_member_name(*member_type, index); |
| 2212 |
parent_type = member_type; |
| 2213 |
member_type_id = member_type->member_types[index]; |
| 2214 |
member_type = &get<SPIRType>(member_type_id); |
| 2215 |
} |
| 2216 |
|
| 2217 |
assert(member_type->basetype != SPIRType::Struct); |
| 2218 |
|
| 2219 |
// We're overriding struct member names, so ensure we do so on the primary type. |
| 2220 |
if (parent_type->type_alias) |
| 2221 |
parent_type = &get<SPIRType>(parent_type->type_alias); |
| 2222 |
|
| 2223 |
// Sanitize underscores because joining the two identifiers might create more than 1 underscore in a row, |
| 2224 |
// which is not allowed. |
| 2225 |
ParsedIR::sanitize_underscores(flattened_name); |
| 2226 |
|
| 2227 |
uint32_t last_index = indices.back(); |
| 2228 |
|
| 2229 |
// Pass in the varying qualifier here so it will appear in the correct declaration order. |
| 2230 |
// Replace member name while emitting it so it encodes both struct name and member name. |
| 2231 |
auto backup_name = get_member_name(parent_type->self, last_index); |
| 2232 |
auto member_name = to_member_name(*parent_type, last_index); |
| 2233 |
set_member_name(parent_type->self, last_index, flattened_name); |
| 2234 |
emit_struct_member(*parent_type, member_type_id, last_index, qual); |
| 2235 |
// Restore member name. |
| 2236 |
set_member_name(parent_type->self, last_index, member_name); |
| 2237 |
} |
| 2238 |
|
| 2239 |
void CompilerGLSL::emit_flattened_io_block_struct(const std::string &basename, const SPIRType &type, const char *qual, |
| 2240 |
const SmallVector<uint32_t> &indices) |
| 2241 |
{ |
| 2242 |
auto sub_indices = indices; |
| 2243 |
sub_indices.push_back(0); |
| 2244 |
|
| 2245 |
const SPIRType *member_type = &type; |
| 2246 |
for (auto &index : indices) |
| 2247 |
member_type = &get<SPIRType>(member_type->member_types[index]); |
| 2248 |
|
| 2249 |
assert(member_type->basetype == SPIRType::Struct); |
| 2250 |
|
| 2251 |
if (!member_type->array.empty()) |
| 2252 |
SPIRV_CROSS_THROW("Cannot flatten array of structs in I/O blocks."); |
| 2253 |
|
| 2254 |
for (uint32_t i = 0; i < uint32_t(member_type->member_types.size()); i++) |
| 2255 |
{ |
| 2256 |
sub_indices.back() = i; |
| 2257 |
if (get<SPIRType>(member_type->member_types[i]).basetype == SPIRType::Struct) |
| 2258 |
emit_flattened_io_block_struct(basename, type, qual, sub_indices); |
| 2259 |
else |
| 2260 |
emit_flattened_io_block_member(basename, type, qual, sub_indices); |
| 2261 |
} |
| 2262 |
} |
| 2263 |
|
| 2264 |
void CompilerGLSL::emit_flattened_io_block(const SPIRVariable &var, const char *qual) |
| 2265 |
{ |
| 2266 |
auto &var_type = get<SPIRType>(var.basetype); |
| 2267 |
if (!var_type.array.empty()) |
| 2268 |
SPIRV_CROSS_THROW("Array of varying structs cannot be flattened to legacy-compatible varyings."); |
| 2269 |
|
| 2270 |
// Emit flattened types based on the type alias. Normally, we are never supposed to emit |
| 2271 |
// struct declarations for aliased types. |
| 2272 |
auto &type = var_type.type_alias ? get<SPIRType>(var_type.type_alias) : var_type; |
| 2273 |
|
| 2274 |
auto old_flags = ir.meta[type.self].decoration.decoration_flags; |
| 2275 |
// Emit the members as if they are part of a block to get all qualifiers. |
| 2276 |
ir.meta[type.self].decoration.decoration_flags.set(DecorationBlock); |
| 2277 |
|
| 2278 |
type.member_name_cache.clear(); |
| 2279 |
|
| 2280 |
SmallVector<uint32_t> member_indices; |
| 2281 |
member_indices.push_back(0); |
| 2282 |
auto basename = to_name(var.self); |
| 2283 |
|
| 2284 |
uint32_t i = 0; |
| 2285 |
for (auto &member : type.member_types) |
| 2286 |
{ |
| 2287 |
add_member_name(type, i); |
| 2288 |
auto &membertype = get<SPIRType>(member); |
| 2289 |
|
| 2290 |
member_indices.back() = i; |
| 2291 |
if (membertype.basetype == SPIRType::Struct) |
| 2292 |
emit_flattened_io_block_struct(basename, type, qual, member_indices); |
| 2293 |
else |
| 2294 |
emit_flattened_io_block_member(basename, type, qual, member_indices); |
| 2295 |
i++; |
| 2296 |
} |
| 2297 |
|
| 2298 |
ir.meta[type.self].decoration.decoration_flags = old_flags; |
| 2299 |
|
| 2300 |
// Treat this variable as fully flattened from now on. |
| 2301 |
flattened_structs[var.self] = true; |
| 2302 |
} |
| 2303 |
|
| 2304 |
void CompilerGLSL::emit_interface_block(const SPIRVariable &var) |
| 2305 |
{ |
| 2306 |
auto &type = get<SPIRType>(var.basetype); |
| 2307 |
|
| 2308 |
// Either make it plain in/out or in/out blocks depending on what shader is doing ... |
| 2309 |
bool block = ir.meta[type.self].decoration.decoration_flags.get(DecorationBlock); |
| 2310 |
const char *qual = to_storage_qualifiers_glsl(var); |
| 2311 |
|
| 2312 |
if (block) |
| 2313 |
{ |
| 2314 |
// ESSL earlier than 310 and GLSL earlier than 150 did not support |
| 2315 |
// I/O variables which are struct types. |
| 2316 |
// To support this, flatten the struct into separate varyings instead. |
| 2317 |
if (options.force_flattened_io_blocks || (options.es && options.version < 310) || |
| 2318 |
(!options.es && options.version < 150)) |
| 2319 |
{ |
| 2320 |
// I/O blocks on ES require version 310 with Android Extension Pack extensions, or core version 320. |
| 2321 |
// On desktop, I/O blocks were introduced with geometry shaders in GL 3.2 (GLSL 150). |
| 2322 |
emit_flattened_io_block(var, qual); |
| 2323 |
} |
| 2324 |
else |
| 2325 |
{ |
| 2326 |
if (options.es && options.version < 320) |
| 2327 |
{ |
| 2328 |
// Geometry and tessellation extensions imply this extension. |
| 2329 |
if (!has_extension("GL_EXT_geometry_shader") && !has_extension("GL_EXT_tessellation_shader")) |
| 2330 |
require_extension_internal("GL_EXT_shader_io_blocks"); |
| 2331 |
} |
| 2332 |
|
| 2333 |
// Block names should never alias. |
| 2334 |
auto block_name = to_name(type.self, false); |
| 2335 |
|
| 2336 |
// The namespace for I/O blocks is separate from other variables in GLSL. |
| 2337 |
auto &block_namespace = type.storage == StorageClassInput ? block_input_names : block_output_names; |
| 2338 |
|
| 2339 |
// Shaders never use the block by interface name, so we don't |
| 2340 |
// have to track this other than updating name caches. |
| 2341 |
if (block_name.empty() || block_namespace.find(block_name) != end(block_namespace)) |
| 2342 |
block_name = get_fallback_name(type.self); |
| 2343 |
else |
| 2344 |
block_namespace.insert(block_name); |
| 2345 |
|
| 2346 |
// If for some reason buffer_name is an illegal name, make a final fallback to a workaround name. |
| 2347 |
// This cannot conflict with anything else, so we're safe now. |
| 2348 |
if (block_name.empty()) |
| 2349 |
block_name = join("_", get<SPIRType>(var.basetype).self, "_", var.self); |
| 2350 |
|
| 2351 |
// Instance names cannot alias block names. |
| 2352 |
resource_names.insert(block_name); |
| 2353 |
|
| 2354 |
statement(layout_for_variable(var), qual, block_name); |
| 2355 |
begin_scope(); |
| 2356 |
|
| 2357 |
type.member_name_cache.clear(); |
| 2358 |
|
| 2359 |
uint32_t i = 0; |
| 2360 |
for (auto &member : type.member_types) |
| 2361 |
{ |
| 2362 |
add_member_name(type, i); |
| 2363 |
emit_struct_member(type, member, i); |
| 2364 |
i++; |
| 2365 |
} |
| 2366 |
|
| 2367 |
add_resource_name(var.self); |
| 2368 |
end_scope_decl(join(to_name(var.self), type_to_array_glsl(type))); |
| 2369 |
statement(""); |
| 2370 |
} |
| 2371 |
} |
| 2372 |
else |
| 2373 |
{ |
| 2374 |
// ESSL earlier than 310 and GLSL earlier than 150 did not support |
| 2375 |
// I/O variables which are struct types. |
| 2376 |
// To support this, flatten the struct into separate varyings instead. |
| 2377 |
if (type.basetype == SPIRType::Struct && |
| 2378 |
(options.force_flattened_io_blocks || (options.es && options.version < 310) || |
| 2379 |
(!options.es && options.version < 150))) |
| 2380 |
{ |
| 2381 |
emit_flattened_io_block(var, qual); |
| 2382 |
} |
| 2383 |
else |
| 2384 |
{ |
| 2385 |
add_resource_name(var.self); |
| 2386 |
|
| 2387 |
// Tessellation control and evaluation shaders must have either gl_MaxPatchVertices or unsized arrays for input arrays. |
| 2388 |
// Opt for unsized as it's the more "correct" variant to use. |
| 2389 |
bool control_point_input_array = type.storage == StorageClassInput && !type.array.empty() && |
| 2390 |
!has_decoration(var.self, DecorationPatch) && |
| 2391 |
(get_entry_point().model == ExecutionModelTessellationControl || |
| 2392 |
get_entry_point().model == ExecutionModelTessellationEvaluation); |
| 2393 |
|
| 2394 |
uint32_t old_array_size = 0; |
| 2395 |
bool old_array_size_literal = true; |
| 2396 |
|
| 2397 |
if (control_point_input_array) |
| 2398 |
{ |
| 2399 |
swap(type.array.back(), old_array_size); |
| 2400 |
swap(type.array_size_literal.back(), old_array_size_literal); |
| 2401 |
} |
| 2402 |
|
| 2403 |
statement(layout_for_variable(var), to_qualifiers_glsl(var.self), |
| 2404 |
variable_decl(type, to_name(var.self), var.self), ";"); |
| 2405 |
|
| 2406 |
if (control_point_input_array) |
| 2407 |
{ |
| 2408 |
swap(type.array.back(), old_array_size); |
| 2409 |
swap(type.array_size_literal.back(), old_array_size_literal); |
| 2410 |
} |
| 2411 |
|
| 2412 |
// If a StorageClassOutput variable has an initializer, we need to initialize it in main(). |
| 2413 |
if (var.storage == StorageClassOutput && var.initializer) |
| 2414 |
{ |
| 2415 |
auto &entry_func = this->get<SPIRFunction>(ir.default_entry_point); |
| 2416 |
entry_func.fixup_hooks_in.push_back( |
| 2417 |
[&]() { statement(to_name(var.self), " = ", to_expression(var.initializer), ";"); }); |
| 2418 |
} |
| 2419 |
} |
| 2420 |
} |
| 2421 |
} |
| 2422 |
|
| 2423 |
void CompilerGLSL::emit_uniform(const SPIRVariable &var) |
| 2424 |
{ |
| 2425 |
auto &type = get<SPIRType>(var.basetype); |
| 2426 |
if (type.basetype == SPIRType::Image && type.image.sampled == 2 && type.image.dim != DimSubpassData) |
| 2427 |
{ |
| 2428 |
if (!options.es && options.version < 420) |
| 2429 |
require_extension_internal("GL_ARB_shader_image_load_store"); |
| 2430 |
else if (options.es && options.version < 310) |
| 2431 |
SPIRV_CROSS_THROW("At least ESSL 3.10 required for shader image load store."); |
| 2432 |
} |
| 2433 |
|
| 2434 |
add_resource_name(var.self); |
| 2435 |
statement(layout_for_variable(var), variable_decl(var), ";"); |
| 2436 |
} |
| 2437 |
|
| 2438 |
string CompilerGLSL::constant_value_macro_name(uint32_t id) |
| 2439 |
{ |
| 2440 |
return join("SPIRV_CROSS_CONSTANT_ID_", id); |
| 2441 |
} |
| 2442 |
|
| 2443 |
void CompilerGLSL::emit_specialization_constant_op(const SPIRConstantOp &constant) |
| 2444 |
{ |
| 2445 |
auto &type = get<SPIRType>(constant.basetype); |
| 2446 |
auto name = to_name(constant.self); |
| 2447 |
statement("const ", variable_decl(type, name), " = ", constant_op_expression(constant), ";"); |
| 2448 |
} |
| 2449 |
|
| 2450 |
void CompilerGLSL::emit_constant(const SPIRConstant &constant) |
| 2451 |
{ |
| 2452 |
auto &type = get<SPIRType>(constant.constant_type); |
| 2453 |
auto name = to_name(constant.self); |
| 2454 |
|
| 2455 |
SpecializationConstant wg_x, wg_y, wg_z; |
| 2456 |
ID workgroup_size_id = get_work_group_size_specialization_constants(wg_x, wg_y, wg_z); |
| 2457 |
|
| 2458 |
// This specialization constant is implicitly declared by emitting layout() in; |
| 2459 |
if (constant.self == workgroup_size_id) |
| 2460 |
return; |
| 2461 |
|
| 2462 |
// These specialization constants are implicitly declared by emitting layout() in; |
| 2463 |
// In legacy GLSL, we will still need to emit macros for these, so a layout() in; declaration |
| 2464 |
// later can use macro overrides for work group size. |
| 2465 |
bool is_workgroup_size_constant = ConstantID(constant.self) == wg_x.id || ConstantID(constant.self) == wg_y.id || |
| 2466 |
ConstantID(constant.self) == wg_z.id; |
| 2467 |
|
| 2468 |
if (options.vulkan_semantics && is_workgroup_size_constant) |
| 2469 |
{ |
| 2470 |
// Vulkan GLSL does not need to declare workgroup spec constants explicitly, it is handled in layout(). |
| 2471 |
return; |
| 2472 |
} |
| 2473 |
else if (!options.vulkan_semantics && is_workgroup_size_constant && |
| 2474 |
!has_decoration(constant.self, DecorationSpecId)) |
| 2475 |
{ |
| 2476 |
// Only bother declaring a workgroup size if it is actually a specialization constant, because we need macros. |
| 2477 |
return; |
| 2478 |
} |
| 2479 |
|
| 2480 |
// Only scalars have constant IDs. |
| 2481 |
if (has_decoration(constant.self, DecorationSpecId)) |
| 2482 |
{ |
| 2483 |
if (options.vulkan_semantics) |
| 2484 |
{ |
| 2485 |
statement("layout(constant_id = ", get_decoration(constant.self, DecorationSpecId), ") const ", |
| 2486 |
variable_decl(type, name), " = ", constant_expression(constant), ";"); |
| 2487 |
} |
| 2488 |
else |
| 2489 |
{ |
| 2490 |
const string ¯o_name = constant.specialization_constant_macro_name; |
| 2491 |
statement("#ifndef ", macro_name); |
| 2492 |
statement("#define ", macro_name, " ", constant_expression(constant)); |
| 2493 |
statement("#endif"); |
| 2494 |
|
| 2495 |
// For workgroup size constants, only emit the macros. |
| 2496 |
if (!is_workgroup_size_constant) |
| 2497 |
statement("const ", variable_decl(type, name), " = ", macro_name, ";"); |
| 2498 |
} |
| 2499 |
} |
| 2500 |
else |
| 2501 |
{ |
| 2502 |
statement("const ", variable_decl(type, name), " = ", constant_expression(constant), ";"); |
| 2503 |
} |
| 2504 |
} |
| 2505 |
|
| 2506 |
void CompilerGLSL::emit_entry_point_declarations() |
| 2507 |
{ |
| 2508 |
} |
| 2509 |
|
| 2510 |
void CompilerGLSL::replace_illegal_names(const unordered_set<string> &keywords) |
| 2511 |
{ |
| 2512 |
ir.for_each_typed_id<SPIRVariable>([&](uint32_t, const SPIRVariable &var) { |
| 2513 |
if (is_hidden_variable(var)) |
| 2514 |
return; |
| 2515 |
|
| 2516 |
auto *meta = ir.find_meta(var.self); |
| 2517 |
if (!meta) |
| 2518 |
return; |
| 2519 |
|
| 2520 |
auto &m = meta->decoration; |
| 2521 |
if (m.alias.compare(0, 3, "gl_") == 0 || keywords.find(m.alias) != end(keywords)) |
| 2522 |
m.alias = join("_", m.alias); |
| 2523 |
}); |
| 2524 |
|
| 2525 |
ir.for_each_typed_id<SPIRType>([&](uint32_t, const SPIRType &type) { |
| 2526 |
auto *meta = ir.find_meta(type.self); |
| 2527 |
if (!meta) |
| 2528 |
return; |
| 2529 |
|
| 2530 |
auto &m = meta->decoration; |
| 2531 |
if (m.alias.compare(0, 3, "gl_") == 0 || keywords.find(m.alias) != end(keywords)) |
| 2532 |
m.alias = join("_", m.alias); |
| 2533 |
|
| 2534 |
for (auto &memb : meta->members) |
| 2535 |
if (memb.alias.compare(0, 3, "gl_") == 0 || keywords.find(memb.alias) != end(keywords)) |
| 2536 |
memb.alias = join("_", memb.alias); |
| 2537 |
}); |
| 2538 |
} |
| 2539 |
|
| 2540 |
void CompilerGLSL::replace_illegal_names() |
| 2541 |
{ |
| 2542 |
// clang-format off |
| 2543 |
static const unordered_set<string> keywords = { |
| 2544 |
"abs", "acos", "acosh", "all", "any", "asin", "asinh", "atan", "atanh", |
| 2545 |
"atomicAdd", "atomicCompSwap", "atomicCounter", "atomicCounterDecrement", "atomicCounterIncrement", |
| 2546 |
"atomicExchange", "atomicMax", "atomicMin", "atomicOr", "atomicXor", |
| 2547 |
"bitCount", "bitfieldExtract", "bitfieldInsert", "bitfieldReverse", |
| 2548 |
"ceil", "cos", "cosh", "cross", "degrees", |
| 2549 |
"dFdx", "dFdxCoarse", "dFdxFine", |
| 2550 |
"dFdy", "dFdyCoarse", "dFdyFine", |
| 2551 |
"distance", "dot", "EmitStreamVertex", "EmitVertex", "EndPrimitive", "EndStreamPrimitive", "equal", "exp", "exp2", |
| 2552 |
"faceforward", "findLSB", "findMSB", "float16BitsToInt16", "float16BitsToUint16", "floatBitsToInt", "floatBitsToUint", "floor", "fma", "fract", |
| 2553 |
"frexp", "fwidth", "fwidthCoarse", "fwidthFine", |
| 2554 |
"greaterThan", "greaterThanEqual", "groupMemoryBarrier", |
| 2555 |
"imageAtomicAdd", "imageAtomicAnd", "imageAtomicCompSwap", "imageAtomicExchange", "imageAtomicMax", "imageAtomicMin", "imageAtomicOr", "imageAtomicXor", |
| 2556 |
"imageLoad", "imageSamples", "imageSize", "imageStore", "imulExtended", "int16BitsToFloat16", "intBitsToFloat", "interpolateAtOffset", "interpolateAtCentroid", "interpolateAtSample", |
| 2557 |
"inverse", "inversesqrt", "isinf", "isnan", "ldexp", "length", "lessThan", "lessThanEqual", "log", "log2", |
| 2558 |
"matrixCompMult", "max", "memoryBarrier", "memoryBarrierAtomicCounter", "memoryBarrierBuffer", "memoryBarrierImage", "memoryBarrierShared", |
| 2559 |
"min", "mix", "mod", "modf", "noise", "noise1", "noise2", "noise3", "noise4", "normalize", "not", "notEqual", |
| 2560 |
"outerProduct", "packDouble2x32", "packHalf2x16", "packInt2x16", "packInt4x16", "packSnorm2x16", "packSnorm4x8", |
| 2561 |
"packUint2x16", "packUint4x16", "packUnorm2x16", "packUnorm4x8", "pow", |
| 2562 |
"radians", "reflect", "refract", "round", "roundEven", "sign", "sin", "sinh", "smoothstep", "sqrt", "step", |
| 2563 |
"tan", "tanh", "texelFetch", "texelFetchOffset", "texture", "textureGather", "textureGatherOffset", "textureGatherOffsets", |
| 2564 |
"textureGrad", "textureGradOffset", "textureLod", "textureLodOffset", "textureOffset", "textureProj", "textureProjGrad", |
| 2565 |
"textureProjGradOffset", "textureProjLod", "textureProjLodOffset", "textureProjOffset", "textureQueryLevels", "textureQueryLod", "textureSamples", "textureSize", |
| 2566 |
"transpose", "trunc", "uaddCarry", "uint16BitsToFloat16", "uintBitsToFloat", "umulExtended", "unpackDouble2x32", "unpackHalf2x16", "unpackInt2x16", "unpackInt4x16", |
| 2567 |
"unpackSnorm2x16", "unpackSnorm4x8", "unpackUint2x16", "unpackUint4x16", "unpackUnorm2x16", "unpackUnorm4x8", "usubBorrow", |
| 2568 |
|
| 2569 |
"active", "asm", "atomic_uint", "attribute", "bool", "break", "buffer", |
| 2570 |
"bvec2", "bvec3", "bvec4", "case", "cast", "centroid", "class", "coherent", "common", "const", "continue", "default", "discard", |
| 2571 |
"dmat2", "dmat2x2", "dmat2x3", "dmat2x4", "dmat3", "dmat3x2", "dmat3x3", "dmat3x4", "dmat4", "dmat4x2", "dmat4x3", "dmat4x4", |
| 2572 |
"do", "double", "dvec2", "dvec3", "dvec4", "else", "enum", "extern", "external", "false", "filter", "fixed", "flat", "float", |
| 2573 |
"for", "fvec2", "fvec3", "fvec4", "goto", "half", "highp", "hvec2", "hvec3", "hvec4", "if", "iimage1D", "iimage1DArray", |
| 2574 |
"iimage2D", "iimage2DArray", "iimage2DMS", "iimage2DMSArray", "iimage2DRect", "iimage3D", "iimageBuffer", "iimageCube", |
| 2575 |
"iimageCubeArray", "image1D", "image1DArray", "image2D", "image2DArray", "image2DMS", "image2DMSArray", "image2DRect", |
| 2576 |
"image3D", "imageBuffer", "imageCube", "imageCubeArray", "in", "inline", "inout", "input", "int", "interface", "invariant", |
| 2577 |
"isampler1D", "isampler1DArray", "isampler2D", "isampler2DArray", "isampler2DMS", "isampler2DMSArray", "isampler2DRect", |
| 2578 |
"isampler3D", "isamplerBuffer", "isamplerCube", "isamplerCubeArray", "ivec2", "ivec3", "ivec4", "layout", "long", "lowp", |
| 2579 |
"mat2", "mat2x2", "mat2x3", "mat2x4", "mat3", "mat3x2", "mat3x3", "mat3x4", "mat4", "mat4x2", "mat4x3", "mat4x4", "mediump", |
| 2580 |
"namespace", "noinline", "noperspective", "out", "output", "packed", "partition", "patch", "precise", "precision", "public", "readonly", |
| 2581 |
"resource", "restrict", "return", "sample", "sampler1D", "sampler1DArray", "sampler1DArrayShadow", |
| 2582 |
"sampler1DShadow", "sampler2D", "sampler2DArray", "sampler2DArrayShadow", "sampler2DMS", "sampler2DMSArray", |
| 2583 |
"sampler2DRect", "sampler2DRectShadow", "sampler2DShadow", "sampler3D", "sampler3DRect", "samplerBuffer", |
| 2584 |
"samplerCube", "samplerCubeArray", "samplerCubeArrayShadow", "samplerCubeShadow", "shared", "short", "sizeof", "smooth", "static", |
| 2585 |
"struct", "subroutine", "superp", "switch", "template", "this", "true", "typedef", "uimage1D", "uimage1DArray", "uimage2D", |
| 2586 |
"uimage2DArray", "uimage2DMS", "uimage2DMSArray", "uimage2DRect", "uimage3D", "uimageBuffer", "uimageCube", |
| 2587 |
"uimageCubeArray", "uint", "uniform", "union", "unsigned", "usampler1D", "usampler1DArray", "usampler2D", "usampler2DArray", |
| 2588 |
"usampler2DMS", "usampler2DMSArray", "usampler2DRect", "usampler3D", "usamplerBuffer", "usamplerCube", |
| 2589 |
"usamplerCubeArray", "using", "uvec2", "uvec3", "uvec4", "varying", "vec2", "vec3", "vec4", "void", "volatile", |
| 2590 |
"while", "writeonly", |
| 2591 |
}; |
| 2592 |
// clang-format on |
| 2593 |
|
| 2594 |
replace_illegal_names(keywords); |
| 2595 |
} |
| 2596 |
|
| 2597 |
void CompilerGLSL::replace_fragment_output(SPIRVariable &var) |
| 2598 |
{ |
| 2599 |
auto &m = ir.meta[var.self].decoration; |
| 2600 |
uint32_t location = 0; |
| 2601 |
if (m.decoration_flags.get(DecorationLocation)) |
| 2602 |
location = m.location; |
| 2603 |
|
| 2604 |
// If our variable is arrayed, we must not emit the array part of this as the SPIR-V will |
| 2605 |
// do the access chain part of this for us. |
| 2606 |
auto &type = get<SPIRType>(var.basetype); |
| 2607 |
|
| 2608 |
if (type.array.empty()) |
| 2609 |
{ |
| 2610 |
// Redirect the write to a specific render target in legacy GLSL. |
| 2611 |
m.alias = join("gl_FragData[", location, "]"); |
| 2612 |
|
| 2613 |
if (is_legacy_es() && location != 0) |
| 2614 |
require_extension_internal("GL_EXT_draw_buffers"); |
| 2615 |
} |
| 2616 |
else if (type.array.size() == 1) |
| 2617 |
{ |
| 2618 |
// If location is non-zero, we probably have to add an offset. |
| 2619 |
// This gets really tricky since we'd have to inject an offset in the access chain. |
| 2620 |
// FIXME: This seems like an extremely odd-ball case, so it's probably fine to leave it like this for now. |
| 2621 |
m.alias = "gl_FragData"; |
| 2622 |
if (location != 0) |
| 2623 |
SPIRV_CROSS_THROW("Arrayed output variable used, but location is not 0. " |
| 2624 |
"This is unimplemented in SPIRV-Cross."); |
| 2625 |
|
| 2626 |
if (is_legacy_es()) |
| 2627 |
require_extension_internal("GL_EXT_draw_buffers"); |
| 2628 |
} |
| 2629 |
else |
| 2630 |
SPIRV_CROSS_THROW("Array-of-array output variable used. This cannot be implemented in legacy GLSL."); |
| 2631 |
|
| 2632 |
var.compat_builtin = true; // We don't want to declare this variable, but use the name as-is. |
| 2633 |
} |
| 2634 |
|
| 2635 |
void CompilerGLSL::replace_fragment_outputs() |
| 2636 |
{ |
| 2637 |
ir.for_each_typed_id<SPIRVariable>([&](uint32_t, SPIRVariable &var) { |
| 2638 |
auto &type = this->get<SPIRType>(var.basetype); |
| 2639 |
|
| 2640 |
if (!is_builtin_variable(var) && !var.remapped_variable && type.pointer && var.storage == StorageClassOutput) |
| 2641 |
replace_fragment_output(var); |
| 2642 |
}); |
| 2643 |
} |
| 2644 |
|
| 2645 |
string CompilerGLSL::remap_swizzle(const SPIRType &out_type, uint32_t input_components, const string &expr) |
| 2646 |
{ |
| 2647 |
if (out_type.vecsize == input_components) |
| 2648 |
return expr; |
| 2649 |
else if (input_components == 1 && !backend.can_swizzle_scalar) |
| 2650 |
return join(type_to_glsl(out_type), "(", expr, ")"); |
| 2651 |
else |
| 2652 |
{ |
| 2653 |
// FIXME: This will not work with packed expressions. |
| 2654 |
auto e = enclose_expression(expr) + "."; |
| 2655 |
// Just clamp the swizzle index if we have more outputs than inputs. |
| 2656 |
for (uint32_t c = 0; c < out_type.vecsize; c++) |
| 2657 |
e += index_to_swizzle(min(c, input_components - 1)); |
| 2658 |
if (backend.swizzle_is_function && out_type.vecsize > 1) |
| 2659 |
e += "()"; |
| 2660 |
|
| 2661 |
remove_duplicate_swizzle(e); |
| 2662 |
return e; |
| 2663 |
} |
| 2664 |
} |
| 2665 |
|
| 2666 |
void CompilerGLSL::emit_pls() |
| 2667 |
{ |
| 2668 |
auto &execution = get_entry_point(); |
| 2669 |
if (execution.model != ExecutionModelFragment) |
| 2670 |
SPIRV_CROSS_THROW("Pixel local storage only supported in fragment shaders."); |
| 2671 |
|
| 2672 |
if (!options.es) |
| 2673 |
SPIRV_CROSS_THROW("Pixel local storage only supported in OpenGL ES."); |
| 2674 |
|
| 2675 |
if (options.version < 300) |
| 2676 |
SPIRV_CROSS_THROW("Pixel local storage only supported in ESSL 3.0 and above."); |
| 2677 |
|
| 2678 |
if (!pls_inputs.empty()) |
| 2679 |
{ |
| 2680 |
statement("__pixel_local_inEXT _PLSIn"); |
| 2681 |
begin_scope(); |
| 2682 |
for (auto &input : pls_inputs) |
| 2683 |
statement(pls_decl(input), ";"); |
| 2684 |
end_scope_decl(); |
| 2685 |
statement(""); |
| 2686 |
} |
| 2687 |
|
| 2688 |
if (!pls_outputs.empty()) |
| 2689 |
{ |
| 2690 |
statement("__pixel_local_outEXT _PLSOut"); |
| 2691 |
begin_scope(); |
| 2692 |
for (auto &output : pls_outputs) |
| 2693 |
statement(pls_decl(output), ";"); |
| 2694 |
end_scope_decl(); |
| 2695 |
statement(""); |
| 2696 |
} |
| 2697 |
} |
| 2698 |
|
| 2699 |
void CompilerGLSL::fixup_image_load_store_access() |
| 2700 |
{ |
| 2701 |
if (!options.enable_storage_image_qualifier_deduction) |
| 2702 |
return; |
| 2703 |
|
| 2704 |
ir.for_each_typed_id<SPIRVariable>([&](uint32_t var, const SPIRVariable &) { |
| 2705 |
auto &vartype = expression_type(var); |
| 2706 |
if (vartype.basetype == SPIRType::Image && vartype.image.sampled == 2) |
| 2707 |
{ |
| 2708 |
// Very old glslangValidator and HLSL compilers do not emit required qualifiers here. |
| 2709 |
// Solve this by making the image access as restricted as possible and loosen up if we need to. |
| 2710 |
// If any no-read/no-write flags are actually set, assume that the compiler knows what it's doing. |
| 2711 |
|
| 2712 |
auto &flags = ir.meta[var].decoration.decoration_flags; |
| 2713 |
if (!flags.get(DecorationNonWritable) && !flags.get(DecorationNonReadable)) |
| 2714 |
{ |
| 2715 |
flags.set(DecorationNonWritable); |
| 2716 |
flags.set(DecorationNonReadable); |
| 2717 |
} |
| 2718 |
} |
| 2719 |
}); |
| 2720 |
} |
| 2721 |
|
| 2722 |
static bool is_block_builtin(BuiltIn builtin) |
| 2723 |
{ |
| 2724 |
return builtin == BuiltInPosition || builtin == BuiltInPointSize || builtin == BuiltInClipDistance || |
| 2725 |
builtin == BuiltInCullDistance; |
| 2726 |
} |
| 2727 |
|
| 2728 |
bool CompilerGLSL::should_force_emit_builtin_block(StorageClass storage) |
| 2729 |
{ |
| 2730 |
// If the builtin block uses XFB, we need to force explicit redeclaration of the builtin block. |
| 2731 |
|
| 2732 |
if (storage != StorageClassOutput) |
| 2733 |
return false; |
| 2734 |
bool should_force = false; |
| 2735 |
|
| 2736 |
ir.for_each_typed_id<SPIRVariable>([&](uint32_t, SPIRVariable &var) { |
| 2737 |
if (should_force) |
| 2738 |
return; |
| 2739 |
|
| 2740 |
auto &type = this->get<SPIRType>(var.basetype); |
| 2741 |
bool block = has_decoration(type.self, DecorationBlock); |
| 2742 |
if (var.storage == storage && block && is_builtin_variable(var)) |
| 2743 |
{ |
| 2744 |
uint32_t member_count = uint32_t(type.member_types.size()); |
| 2745 |
for (uint32_t i = 0; i < member_count; i++) |
| 2746 |
{ |
| 2747 |
if (has_member_decoration(type.self, i, DecorationBuiltIn) && |
| 2748 |
is_block_builtin(BuiltIn(get_member_decoration(type.self, i, DecorationBuiltIn))) && |
| 2749 |
has_member_decoration(type.self, i, DecorationOffset)) |
| 2750 |
{ |
| 2751 |
should_force = true; |
| 2752 |
} |
| 2753 |
} |
| 2754 |
} |
| 2755 |
else if (var.storage == storage && !block && is_builtin_variable(var)) |
| 2756 |
{ |
| 2757 |
if (is_block_builtin(BuiltIn(get_decoration(type.self, DecorationBuiltIn))) && |
| 2758 |
has_decoration(var.self, DecorationOffset)) |
| 2759 |
{ |
| 2760 |
should_force = true; |
| 2761 |
} |
| 2762 |
} |
| 2763 |
}); |
| 2764 |
|
| 2765 |
return should_force; |
| 2766 |
} |
| 2767 |
|
| 2768 |
void CompilerGLSL::fixup_implicit_builtin_block_names() |
| 2769 |
{ |
| 2770 |
ir.for_each_typed_id<SPIRVariable>([&](uint32_t, SPIRVariable &var) { |
| 2771 |
auto &type = this->get<SPIRType>(var.basetype); |
| 2772 |
bool block = has_decoration(type.self, DecorationBlock); |
| 2773 |
if ((var.storage == StorageClassOutput || var.storage == StorageClassInput) && block && |
| 2774 |
is_builtin_variable(var)) |
| 2775 |
{ |
| 2776 |
// Make sure the array has a supported name in the code. |
| 2777 |
if (var.storage == StorageClassOutput) |
| 2778 |
set_name(var.self, "gl_out"); |
| 2779 |
else if (var.storage == StorageClassInput) |
| 2780 |
set_name(var.self, "gl_in"); |
| 2781 |
} |
| 2782 |
}); |
| 2783 |
} |
| 2784 |
|
| 2785 |
void CompilerGLSL::emit_declared_builtin_block(StorageClass storage, ExecutionModel model) |
| 2786 |
{ |
| 2787 |
Bitset emitted_builtins; |
| 2788 |
Bitset global_builtins; |
| 2789 |
const SPIRVariable *block_var = nullptr; |
| 2790 |
bool emitted_block = false; |
| 2791 |
bool builtin_array = false; |
| 2792 |
|
| 2793 |
// Need to use declared size in the type. |
| 2794 |
// These variables might have been declared, but not statically used, so we haven't deduced their size yet. |
| 2795 |
uint32_t cull_distance_size = 0; |
| 2796 |
uint32_t clip_distance_size = 0; |
| 2797 |
|
| 2798 |
bool have_xfb_buffer_stride = false; |
| 2799 |
bool have_geom_stream = false; |
| 2800 |
bool have_any_xfb_offset = false; |
| 2801 |
uint32_t xfb_stride = 0, xfb_buffer = 0, geom_stream = 0; |
| 2802 |
std::unordered_map<uint32_t, uint32_t> builtin_xfb_offsets; |
| 2803 |
|
| 2804 |
ir.for_each_typed_id<SPIRVariable>([&](uint32_t, SPIRVariable &var) { |
| 2805 |
auto &type = this->get<SPIRType>(var.basetype); |
| 2806 |
bool block = has_decoration(type.self, DecorationBlock); |
| 2807 |
Bitset builtins; |
| 2808 |
|
| 2809 |
if (var.storage == storage && block && is_builtin_variable(var)) |
| 2810 |
{ |
| 2811 |
uint32_t index = 0; |
| 2812 |
for (auto &m : ir.meta[type.self].members) |
| 2813 |
{ |
| 2814 |
if (m.builtin) |
| 2815 |
{ |
| 2816 |
builtins.set(m.builtin_type); |
| 2817 |
if (m.builtin_type == BuiltInCullDistance) |
| 2818 |
cull_distance_size = to_array_size_literal(this->get<SPIRType>(type.member_types[index])); |
| 2819 |
else if (m.builtin_type == BuiltInClipDistance) |
| 2820 |
clip_distance_size = to_array_size_literal(this->get<SPIRType>(type.member_types[index])); |
| 2821 |
|
| 2822 |
if (is_block_builtin(m.builtin_type) && m.decoration_flags.get(DecorationOffset)) |
| 2823 |
{ |
| 2824 |
have_any_xfb_offset = true; |
| 2825 |
builtin_xfb_offsets[m.builtin_type] = m.offset; |
| 2826 |
} |
| 2827 |
|
| 2828 |
if (is_block_builtin(m.builtin_type) && m.decoration_flags.get(DecorationStream)) |
| 2829 |
{ |
| 2830 |
uint32_t stream = m.stream; |
| 2831 |
if (have_geom_stream && geom_stream != stream) |
| 2832 |
SPIRV_CROSS_THROW("IO block member Stream mismatch."); |
| 2833 |
have_geom_stream = true; |
| 2834 |
geom_stream = stream; |
| 2835 |
} |
| 2836 |
} |
| 2837 |
index++; |
| 2838 |
} |
| 2839 |
|
| 2840 |
if (storage == StorageClassOutput && has_decoration(var.self, DecorationXfbBuffer) && |
| 2841 |
has_decoration(var.self, DecorationXfbStride)) |
| 2842 |
{ |
| 2843 |
uint32_t buffer_index = get_decoration(var.self, DecorationXfbBuffer); |
| 2844 |
uint32_t stride = get_decoration(var.self, DecorationXfbStride); |
| 2845 |
if (have_xfb_buffer_stride && buffer_index != xfb_buffer) |
| 2846 |
SPIRV_CROSS_THROW("IO block member XfbBuffer mismatch."); |
| 2847 |
if (have_xfb_buffer_stride && stride != xfb_stride) |
| 2848 |
SPIRV_CROSS_THROW("IO block member XfbBuffer mismatch."); |
| 2849 |
have_xfb_buffer_stride = true; |
| 2850 |
xfb_buffer = buffer_index; |
| 2851 |
xfb_stride = stride; |
| 2852 |
} |
| 2853 |
|
| 2854 |
if (storage == StorageClassOutput && has_decoration(var.self, DecorationStream)) |
| 2855 |
{ |
| 2856 |
uint32_t stream = get_decoration(var.self, DecorationStream); |
| 2857 |
if (have_geom_stream && geom_stream != stream) |
| 2858 |
SPIRV_CROSS_THROW("IO block member Stream mismatch."); |
| 2859 |
have_geom_stream = true; |
| 2860 |
geom_stream = stream; |
| 2861 |
} |
| 2862 |
} |
| 2863 |
else if (var.storage == storage && !block && is_builtin_variable(var)) |
| 2864 |
{ |
| 2865 |
// While we're at it, collect all declared global builtins (HLSL mostly ...). |
| 2866 |
auto &m = ir.meta[var.self].decoration; |
| 2867 |
if (m.builtin) |
| 2868 |
{ |
| 2869 |
global_builtins.set(m.builtin_type); |
| 2870 |
if (m.builtin_type == BuiltInCullDistance) |
| 2871 |
cull_distance_size = to_array_size_literal(type); |
| 2872 |
else if (m.builtin_type == BuiltInClipDistance) |
| 2873 |
clip_distance_size = to_array_size_literal(type); |
| 2874 |
|
| 2875 |
if (is_block_builtin(m.builtin_type) && m.decoration_flags.get(DecorationXfbStride) && |
| 2876 |
m.decoration_flags.get(DecorationXfbBuffer) && m.decoration_flags.get(DecorationOffset)) |
| 2877 |
{ |
| 2878 |
have_any_xfb_offset = true; |
| 2879 |
builtin_xfb_offsets[m.builtin_type] = m.offset; |
| 2880 |
uint32_t buffer_index = m.xfb_buffer; |
| 2881 |
uint32_t stride = m.xfb_stride; |
| 2882 |
if (have_xfb_buffer_stride && buffer_index != xfb_buffer) |
| 2883 |
SPIRV_CROSS_THROW("IO block member XfbBuffer mismatch."); |
| 2884 |
if (have_xfb_buffer_stride && stride != xfb_stride) |
| 2885 |
SPIRV_CROSS_THROW("IO block member XfbBuffer mismatch."); |
| 2886 |
have_xfb_buffer_stride = true; |
| 2887 |
xfb_buffer = buffer_index; |
| 2888 |
xfb_stride = stride; |
| 2889 |
} |
| 2890 |
|
| 2891 |
if (is_block_builtin(m.builtin_type) && m.decoration_flags.get(DecorationStream)) |
| 2892 |
{ |
| 2893 |
uint32_t stream = get_decoration(var.self, DecorationStream); |
| 2894 |
if (have_geom_stream && geom_stream != stream) |
| 2895 |
SPIRV_CROSS_THROW("IO block member Stream mismatch."); |
| 2896 |
have_geom_stream = true; |
| 2897 |
geom_stream = stream; |
| 2898 |
} |
| 2899 |
} |
| 2900 |
} |
| 2901 |
|
| 2902 |
if (builtins.empty()) |
| 2903 |
return; |
| 2904 |
|
| 2905 |
if (emitted_block) |
| 2906 |
SPIRV_CROSS_THROW("Cannot use more than one builtin I/O block."); |
| 2907 |
|
| 2908 |
emitted_builtins = builtins; |
| 2909 |
emitted_block = true; |
| 2910 |
builtin_array = !type.array.empty(); |
| 2911 |
block_var = &var; |
| 2912 |
}); |
| 2913 |
|
| 2914 |
global_builtins = |
| 2915 |
Bitset(global_builtins.get_lower() & ((1ull << BuiltInPosition) | (1ull << BuiltInPointSize) | |
| 2916 |
(1ull << BuiltInClipDistance) | (1ull << BuiltInCullDistance))); |
| 2917 |
|
| 2918 |
// Try to collect all other declared builtins. |
| 2919 |
if (!emitted_block) |
| 2920 |
emitted_builtins = global_builtins; |
| 2921 |
|
| 2922 |
// Can't declare an empty interface block. |
| 2923 |
if (emitted_builtins.empty()) |
| 2924 |
return; |
| 2925 |
|
| 2926 |
if (storage == StorageClassOutput) |
| 2927 |
{ |
| 2928 |
SmallVector<string> attr; |
| 2929 |
if (have_xfb_buffer_stride && have_any_xfb_offset) |
| 2930 |
{ |
| 2931 |
if (!options.es) |
| 2932 |
{ |
| 2933 |
if (options.version < 440 && options.version >= 140) |
| 2934 |
require_extension_internal("GL_ARB_enhanced_layouts"); |
| 2935 |
else if (options.version < 140) |
| 2936 |
SPIRV_CROSS_THROW("Component decoration is not supported in targets below GLSL 1.40."); |
| 2937 |
if (!options.es && options.version < 440) |
| 2938 |
require_extension_internal("GL_ARB_enhanced_layouts"); |
| 2939 |
} |
| 2940 |
else if (options.es) |
| 2941 |
SPIRV_CROSS_THROW("Need GL_ARB_enhanced_layouts for xfb_stride or xfb_buffer."); |
| 2942 |
attr.push_back(join("xfb_buffer = ", xfb_buffer, ", xfb_stride = ", xfb_stride)); |
| 2943 |
} |
| 2944 |
|
| 2945 |
if (have_geom_stream) |
| 2946 |
{ |
| 2947 |
if (get_execution_model() != ExecutionModelGeometry) |
| 2948 |
SPIRV_CROSS_THROW("Geometry streams can only be used in geometry shaders."); |
| 2949 |
if (options.es) |
| 2950 |
SPIRV_CROSS_THROW("Multiple geometry streams not supported in ESSL."); |
| 2951 |
if (options.version < 400) |
| 2952 |
require_extension_internal("GL_ARB_transform_feedback3"); |
| 2953 |
attr.push_back(join("stream = ", geom_stream)); |
| 2954 |
} |
| 2955 |
|
| 2956 |
if (!attr.empty()) |
| 2957 |
statement("layout(", merge(attr), ") out gl_PerVertex"); |
| 2958 |
else |
| 2959 |
statement("out gl_PerVertex"); |
| 2960 |
} |
| 2961 |
else |
| 2962 |
{ |
| 2963 |
// If we have passthrough, there is no way PerVertex cannot be passthrough. |
| 2964 |
if (get_entry_point().geometry_passthrough) |
| 2965 |
statement("layout(passthrough) in gl_PerVertex"); |
| 2966 |
else |
| 2967 |
statement("in gl_PerVertex"); |
| 2968 |
} |
| 2969 |
|
| 2970 |
begin_scope(); |
| 2971 |
if (emitted_builtins.get(BuiltInPosition)) |
| 2972 |
{ |
| 2973 |
auto itr = builtin_xfb_offsets.find(BuiltInPosition); |
| 2974 |
if (itr != end(builtin_xfb_offsets)) |
| 2975 |
statement("layout(xfb_offset = ", itr->second, ") vec4 gl_Position;"); |
| 2976 |
else |
| 2977 |
statement("vec4 gl_Position;"); |
| 2978 |
} |
| 2979 |
|
| 2980 |
if (emitted_builtins.get(BuiltInPointSize)) |
| 2981 |
{ |
| 2982 |
auto itr = builtin_xfb_offsets.find(BuiltInPointSize); |
| 2983 |
if (itr != end(builtin_xfb_offsets)) |
| 2984 |
statement("layout(xfb_offset = ", itr->second, ") float gl_PointSize;"); |
| 2985 |
else |
| 2986 |
statement("float gl_PointSize;"); |
| 2987 |
} |
| 2988 |
|
| 2989 |
if (emitted_builtins.get(BuiltInClipDistance)) |
| 2990 |
{ |
| 2991 |
auto itr = builtin_xfb_offsets.find(BuiltInClipDistance); |
| 2992 |
if (itr != end(builtin_xfb_offsets)) |
| 2993 |
statement("layout(xfb_offset = ", itr->second, ") float gl_ClipDistance[", clip_distance_size, "];"); |
| 2994 |
else |
| 2995 |
statement("float gl_ClipDistance[", clip_distance_size, "];"); |
| 2996 |
} |
| 2997 |
|
| 2998 |
if (emitted_builtins.get(BuiltInCullDistance)) |
| 2999 |
{ |
| 3000 |
auto itr = builtin_xfb_offsets.find(BuiltInCullDistance); |
| 3001 |
if (itr != end(builtin_xfb_offsets)) |
| 3002 |
statement("layout(xfb_offset = ", itr->second, ") float gl_CullDistance[", cull_distance_size, "];"); |
| 3003 |
else |
| 3004 |
statement("float gl_CullDistance[", cull_distance_size, "];"); |
| 3005 |
} |
| 3006 |
|
| 3007 |
if (builtin_array) |
| 3008 |
{ |
| 3009 |
if (model == ExecutionModelTessellationControl && storage == StorageClassOutput) |
| 3010 |
end_scope_decl(join(to_name(block_var->self), "[", get_entry_point().output_vertices, "]")); |
| 3011 |
else |
| 3012 |
end_scope_decl(join(to_name(block_var->self), "[]")); |
| 3013 |
} |
| 3014 |
else |
| 3015 |
end_scope_decl(); |
| 3016 |
statement(""); |
| 3017 |
} |
| 3018 |
|
| 3019 |
void CompilerGLSL::declare_undefined_values() |
| 3020 |
{ |
| 3021 |
bool emitted = false; |
| 3022 |
ir.for_each_typed_id<SPIRUndef>([&](uint32_t, const SPIRUndef &undef) { |
| 3023 |
auto &type = this->get<SPIRType>(undef.basetype); |
| 3024 |
// OpUndef can be void for some reason ... |
| 3025 |
if (type.basetype == SPIRType::Void) |
| 3026 |
return; |
| 3027 |
|
| 3028 |
string initializer; |
| 3029 |
if (options.force_zero_initialized_variables && type_can_zero_initialize(type)) |
| 3030 |
initializer = join(" = ", to_zero_initialized_expression(undef.basetype)); |
| 3031 |
|
| 3032 |
statement(variable_decl(type, to_name(undef.self), undef.self), initializer, ";"); |
| 3033 |
emitted = true; |
| 3034 |
}); |
| 3035 |
|
| 3036 |
if (emitted) |
| 3037 |
statement(""); |
| 3038 |
} |
| 3039 |
|
| 3040 |
bool CompilerGLSL::variable_is_lut(const SPIRVariable &var) const |
| 3041 |
{ |
| 3042 |
bool statically_assigned = var.statically_assigned && var.static_expression != ID(0) && var.remapped_variable; |
| 3043 |
|
| 3044 |
if (statically_assigned) |
| 3045 |
{ |
| 3046 |
auto *constant = maybe_get<SPIRConstant>(var.static_expression); |
| 3047 |
if (constant && constant->is_used_as_lut) |
| 3048 |
return true; |
| 3049 |
} |
| 3050 |
|
| 3051 |
return false; |
| 3052 |
} |
| 3053 |
|
| 3054 |
void CompilerGLSL::emit_resources() |
| 3055 |
{ |
| 3056 |
auto &execution = get_entry_point(); |
| 3057 |
|
| 3058 |
replace_illegal_names(); |
| 3059 |
|
| 3060 |
// Legacy GL uses gl_FragData[], redeclare all fragment outputs |
| 3061 |
// with builtins. |
| 3062 |
if (execution.model == ExecutionModelFragment && is_legacy()) |
| 3063 |
replace_fragment_outputs(); |
| 3064 |
|
| 3065 |
// Emit PLS blocks if we have such variables. |
| 3066 |
if (!pls_inputs.empty() || !pls_outputs.empty()) |
| 3067 |
emit_pls(); |
| 3068 |
|
| 3069 |
switch (execution.model) |
| 3070 |
{ |
| 3071 |
case ExecutionModelGeometry: |
| 3072 |
case ExecutionModelTessellationControl: |
| 3073 |
case ExecutionModelTessellationEvaluation: |
| 3074 |
fixup_implicit_builtin_block_names(); |
| 3075 |
break; |
| 3076 |
|
| 3077 |
default: |
| 3078 |
break; |
| 3079 |
} |
| 3080 |
|
| 3081 |
// Emit custom gl_PerVertex for SSO compatibility. |
| 3082 |
if (options.separate_shader_objects && !options.es && execution.model != ExecutionModelFragment) |
| 3083 |
{ |
| 3084 |
switch (execution.model) |
| 3085 |
{ |
| 3086 |
case ExecutionModelGeometry: |
| 3087 |
case ExecutionModelTessellationControl: |
| 3088 |
case ExecutionModelTessellationEvaluation: |
| 3089 |
emit_declared_builtin_block(StorageClassInput, execution.model); |
| 3090 |
emit_declared_builtin_block(StorageClassOutput, execution.model); |
| 3091 |
break; |
| 3092 |
|
| 3093 |
case ExecutionModelVertex: |
| 3094 |
emit_declared_builtin_block(StorageClassOutput, execution.model); |
| 3095 |
break; |
| 3096 |
|
| 3097 |
default: |
| 3098 |
break; |
| 3099 |
} |
| 3100 |
} |
| 3101 |
else if (should_force_emit_builtin_block(StorageClassOutput)) |
| 3102 |
{ |
| 3103 |
emit_declared_builtin_block(StorageClassOutput, execution.model); |
| 3104 |
} |
| 3105 |
else if (execution.geometry_passthrough) |
| 3106 |
{ |
| 3107 |
// Need to declare gl_in with Passthrough. |
| 3108 |
// If we're doing passthrough, we cannot emit an output block, so the output block test above will never pass. |
| 3109 |
emit_declared_builtin_block(StorageClassInput, execution.model); |
| 3110 |
} |
| 3111 |
else |
| 3112 |
{ |
| 3113 |
// Need to redeclare clip/cull distance with explicit size to use them. |
| 3114 |
// SPIR-V mandates these builtins have a size declared. |
| 3115 |
const char *storage = execution.model == ExecutionModelFragment ? "in" : "out"; |
| 3116 |
if (clip_distance_count != 0) |
| 3117 |
statement(storage, " float gl_ClipDistance[", clip_distance_count, "];"); |
| 3118 |
if (cull_distance_count != 0) |
| 3119 |
statement(storage, " float gl_CullDistance[", cull_distance_count, "];"); |
| 3120 |
if (clip_distance_count != 0 || cull_distance_count != 0) |
| 3121 |
statement(""); |
| 3122 |
} |
| 3123 |
|
| 3124 |
if (position_invariant) |
| 3125 |
{ |
| 3126 |
statement("invariant gl_Position;"); |
| 3127 |
statement(""); |
| 3128 |
} |
| 3129 |
|
| 3130 |
bool emitted = false; |
| 3131 |
|
| 3132 |
// If emitted Vulkan GLSL, |
| 3133 |
// emit specialization constants as actual floats, |
| 3134 |
// spec op expressions will redirect to the constant name. |
| 3135 |
// |
| 3136 |
{ |
| 3137 |
auto loop_lock = ir.create_loop_hard_lock(); |
| 3138 |
for (auto &id_ : ir.ids_for_constant_or_type) |
| 3139 |
{ |
| 3140 |
auto &id = ir.ids[id_]; |
| 3141 |
|
| 3142 |
if (id.get_type() == TypeConstant) |
| 3143 |
{ |
| 3144 |
auto &c = id.get<SPIRConstant>(); |
| 3145 |
|
| 3146 |
bool needs_declaration = c.specialization || c.is_used_as_lut; |
| 3147 |
|
| 3148 |
if (needs_declaration) |
| 3149 |
{ |
| 3150 |
if (!options.vulkan_semantics && c.specialization) |
| 3151 |
{ |
| 3152 |
c.specialization_constant_macro_name = |
| 3153 |
constant_value_macro_name(get_decoration(c.self, DecorationSpecId)); |
| 3154 |
} |
| 3155 |
emit_constant(c); |
| 3156 |
emitted = true; |
| 3157 |
} |
| 3158 |
} |
| 3159 |
else if (id.get_type() == TypeConstantOp) |
| 3160 |
{ |
| 3161 |
emit_specialization_constant_op(id.get<SPIRConstantOp>()); |
| 3162 |
emitted = true; |
| 3163 |
} |
| 3164 |
else if (id.get_type() == TypeType) |
| 3165 |
{ |
| 3166 |
auto *type = &id.get<SPIRType>(); |
| 3167 |
|
| 3168 |
bool is_natural_struct = type->basetype == SPIRType::Struct && type->array.empty() && !type->pointer && |
| 3169 |
(!has_decoration(type->self, DecorationBlock) && |
| 3170 |
!has_decoration(type->self, DecorationBufferBlock)); |
| 3171 |
|
| 3172 |
// Special case, ray payload and hit attribute blocks are not really blocks, just regular structs. |
| 3173 |
if (type->basetype == SPIRType::Struct && type->pointer && |
| 3174 |
has_decoration(type->self, DecorationBlock) && |
| 3175 |
(type->storage == StorageClassRayPayloadNV || type->storage == StorageClassIncomingRayPayloadNV || |
| 3176 |
type->storage == StorageClassHitAttributeNV)) |
| 3177 |
{ |
| 3178 |
type = &get<SPIRType>(type->parent_type); |
| 3179 |
is_natural_struct = true; |
| 3180 |
} |
| 3181 |
|
| 3182 |
if (is_natural_struct) |
| 3183 |
{ |
| 3184 |
if (emitted) |
| 3185 |
statement(""); |
| 3186 |
emitted = false; |
| 3187 |
|
| 3188 |
emit_struct(*type); |
| 3189 |
} |
| 3190 |
} |
| 3191 |
} |
| 3192 |
} |
| 3193 |
|
| 3194 |
if (emitted) |
| 3195 |
statement(""); |
| 3196 |
|
| 3197 |
// If we needed to declare work group size late, check here. |
| 3198 |
// If the work group size depends on a specialization constant, we need to declare the layout() block |
| 3199 |
// after constants (and their macros) have been declared. |
| 3200 |
if (execution.model == ExecutionModelGLCompute && !options.vulkan_semantics && |
| 3201 |
execution.workgroup_size.constant != 0) |
| 3202 |
{ |
| 3203 |
SpecializationConstant wg_x, wg_y, wg_z; |
| 3204 |
get_work_group_size_specialization_constants(wg_x, wg_y, wg_z); |
| 3205 |
|
| 3206 |
if ((wg_x.id != ConstantID(0)) || (wg_y.id != ConstantID(0)) || (wg_z.id != ConstantID(0))) |
| 3207 |
{ |
| 3208 |
SmallVector<string> inputs; |
| 3209 |
build_workgroup_size(inputs, wg_x, wg_y, wg_z); |
| 3210 |
statement("layout(", merge(inputs), ") in;"); |
| 3211 |
statement(""); |
| 3212 |
} |
| 3213 |
} |
| 3214 |
|
| 3215 |
emitted = false; |
| 3216 |
|
| 3217 |
if (ir.addressing_model == AddressingModelPhysicalStorageBuffer64EXT) |
| 3218 |
{ |
| 3219 |
for (auto type : physical_storage_non_block_pointer_types) |
| 3220 |
{ |
| 3221 |
emit_buffer_reference_block(get<SPIRType>(type), false); |
| 3222 |
} |
| 3223 |
|
| 3224 |
// Output buffer reference blocks. |
| 3225 |
// Do this in two stages, one with forward declaration, |
| 3226 |
// and one without. Buffer reference blocks can reference themselves |
| 3227 |
// to support things like linked lists. |
| 3228 |
ir.for_each_typed_id<SPIRType>([&](uint32_t, SPIRType &type) { |
| 3229 |
bool has_block_flags = has_decoration(type.self, DecorationBlock); |
| 3230 |
if (has_block_flags && type.pointer && type.pointer_depth == 1 && !type_is_array_of_pointers(type) && |
| 3231 |
type.storage == StorageClassPhysicalStorageBufferEXT) |
| 3232 |
{ |
| 3233 |
emit_buffer_reference_block(type, true); |
| 3234 |
} |
| 3235 |
}); |
| 3236 |
|
| 3237 |
ir.for_each_typed_id<SPIRType>([&](uint32_t, SPIRType &type) { |
| 3238 |
bool has_block_flags = has_decoration(type.self, DecorationBlock); |
| 3239 |
if (has_block_flags && type.pointer && type.pointer_depth == 1 && !type_is_array_of_pointers(type) && |
| 3240 |
type.storage == StorageClassPhysicalStorageBufferEXT) |
| 3241 |
{ |
| 3242 |
emit_buffer_reference_block(type, false); |
| 3243 |
} |
| 3244 |
}); |
| 3245 |
} |
| 3246 |
|
| 3247 |
// Output UBOs and SSBOs |
| 3248 |
ir.for_each_typed_id<SPIRVariable>([&](uint32_t, SPIRVariable &var) { |
| 3249 |
auto &type = this->get<SPIRType>(var.basetype); |
| 3250 |
|
| 3251 |
bool is_block_storage = type.storage == StorageClassStorageBuffer || type.storage == StorageClassUniform || |
| 3252 |
type.storage == StorageClassShaderRecordBufferNV; |
| 3253 |
bool has_block_flags = ir.meta[type.self].decoration.decoration_flags.get(DecorationBlock) || |
| 3254 |
ir.meta[type.self].decoration.decoration_flags.get(DecorationBufferBlock); |
| 3255 |
|
| 3256 |
if (var.storage != StorageClassFunction && type.pointer && is_block_storage && !is_hidden_variable(var) && |
| 3257 |
has_block_flags) |
| 3258 |
{ |
| 3259 |
emit_buffer_block(var); |
| 3260 |
} |
| 3261 |
}); |
| 3262 |
|
| 3263 |
// Output push constant blocks |
| 3264 |
ir.for_each_typed_id<SPIRVariable>([&](uint32_t, SPIRVariable &var) { |
| 3265 |
auto &type = this->get<SPIRType>(var.basetype); |
| 3266 |
if (var.storage != StorageClassFunction && type.pointer && type.storage == StorageClassPushConstant && |
| 3267 |
!is_hidden_variable(var)) |
| 3268 |
{ |
| 3269 |
emit_push_constant_block(var); |
| 3270 |
} |
| 3271 |
}); |
| 3272 |
|
| 3273 |
bool skip_separate_image_sampler = !combined_image_samplers.empty() || !options.vulkan_semantics; |
| 3274 |
|
| 3275 |
// Output Uniform Constants (values, samplers, images, etc). |
| 3276 |
ir.for_each_typed_id<SPIRVariable>([&](uint32_t, SPIRVariable &var) { |
| 3277 |
auto &type = this->get<SPIRType>(var.basetype); |
| 3278 |
|
| 3279 |
// If we're remapping separate samplers and images, only emit the combined samplers. |
| 3280 |
if (skip_separate_image_sampler) |
| 3281 |
{ |
| 3282 |
// Sampler buffers are always used without a sampler, and they will also work in regular GL. |
| 3283 |
bool sampler_buffer = type.basetype == SPIRType::Image && type.image.dim == DimBuffer; |
| 3284 |
bool separate_image = type.basetype == SPIRType::Image && type.image.sampled == 1; |
| 3285 |
bool separate_sampler = type.basetype == SPIRType::Sampler; |
| 3286 |
if (!sampler_buffer && (separate_image || separate_sampler)) |
| 3287 |
return; |
| 3288 |
} |
| 3289 |
|
| 3290 |
if (var.storage != StorageClassFunction && type.pointer && |
| 3291 |
(type.storage == StorageClassUniformConstant || type.storage == StorageClassAtomicCounter || |
| 3292 |
type.storage == StorageClassRayPayloadNV || type.storage == StorageClassIncomingRayPayloadNV || |
| 3293 |
type.storage == StorageClassCallableDataNV || type.storage == StorageClassIncomingCallableDataNV || |
| 3294 |
type.storage == StorageClassHitAttributeNV) && |
| 3295 |
!is_hidden_variable(var)) |
| 3296 |
{ |
| 3297 |
emit_uniform(var); |
| 3298 |
emitted = true; |
| 3299 |
} |
| 3300 |
}); |
| 3301 |
|
| 3302 |
if (emitted) |
| 3303 |
statement(""); |
| 3304 |
emitted = false; |
| 3305 |
|
| 3306 |
bool emitted_base_instance = false; |
| 3307 |
|
| 3308 |
// Output in/out interfaces. |
| 3309 |
ir.for_each_typed_id<SPIRVariable>([&](uint32_t, SPIRVariable &var) { |
| 3310 |
auto &type = this->get<SPIRType>(var.basetype); |
| 3311 |
|
| 3312 |
bool is_hidden = is_hidden_variable(var); |
| 3313 |
|
| 3314 |
// Unused output I/O variables might still be required to implement framebuffer fetch. |
| 3315 |
if (var.storage == StorageClassOutput && !is_legacy() && |
| 3316 |
inout_color_attachments.count(get_decoration(var.self, DecorationLocation)) != 0) |
| 3317 |
{ |
| 3318 |
is_hidden = false; |
| 3319 |
} |
| 3320 |
|
| 3321 |
if (var.storage != StorageClassFunction && type.pointer && |
| 3322 |
(var.storage == StorageClassInput || var.storage == StorageClassOutput) && |
| 3323 |
interface_variable_exists_in_entry_point(var.self) && !is_hidden) |
| 3324 |
{ |
| 3325 |
emit_interface_block(var); |
| 3326 |
emitted = true; |
| 3327 |
} |
| 3328 |
else if (is_builtin_variable(var)) |
| 3329 |
{ |
| 3330 |
auto builtin = BuiltIn(get_decoration(var.self, DecorationBuiltIn)); |
| 3331 |
// For gl_InstanceIndex emulation on GLES, the API user needs to |
| 3332 |
// supply this uniform. |
| 3333 |
|
| 3334 |
// The draw parameter extension is soft-enabled on GL with some fallbacks. |
| 3335 |
if (!options.vulkan_semantics) |
| 3336 |
{ |
| 3337 |
if (!emitted_base_instance && |
| 3338 |
((options.vertex.support_nonzero_base_instance && builtin == BuiltInInstanceIndex) || |
| 3339 |
(builtin == BuiltInBaseInstance))) |
| 3340 |
{ |
| 3341 |
statement("#ifdef GL_ARB_shader_draw_parameters"); |
| 3342 |
statement("#define SPIRV_Cross_BaseInstance gl_BaseInstanceARB"); |
| 3343 |
statement("#else"); |
| 3344 |
// A crude, but simple workaround which should be good enough for non-indirect draws. |
| 3345 |
statement("uniform int SPIRV_Cross_BaseInstance;"); |
| 3346 |
statement("#endif"); |
| 3347 |
emitted = true; |
| 3348 |
emitted_base_instance = true; |
| 3349 |
} |
| 3350 |
else if (builtin == BuiltInBaseVertex) |
| 3351 |
{ |
| 3352 |
statement("#ifdef GL_ARB_shader_draw_parameters"); |
| 3353 |
statement("#define SPIRV_Cross_BaseVertex gl_BaseVertexARB"); |
| 3354 |
statement("#else"); |
| 3355 |
// A crude, but simple workaround which should be good enough for non-indirect draws. |
| 3356 |
statement("uniform int SPIRV_Cross_BaseVertex;"); |
| 3357 |
statement("#endif"); |
| 3358 |
} |
| 3359 |
else if (builtin == BuiltInDrawIndex) |
| 3360 |
{ |
| 3361 |
statement("#ifndef GL_ARB_shader_draw_parameters"); |
| 3362 |
// Cannot really be worked around. |
| 3363 |
statement("#error GL_ARB_shader_draw_parameters is not supported."); |
| 3364 |
statement("#endif"); |
| 3365 |
} |
| 3366 |
} |
| 3367 |
} |
| 3368 |
}); |
| 3369 |
|
| 3370 |
// Global variables. |
| 3371 |
for (auto global : global_variables) |
| 3372 |
{ |
| 3373 |
auto &var = get<SPIRVariable>(global); |
| 3374 |
if (var.storage != StorageClassOutput) |
| 3375 |
{ |
| 3376 |
if (!variable_is_lut(var)) |
| 3377 |
{ |
| 3378 |
add_resource_name(var.self); |
| 3379 |
|
| 3380 |
string initializer; |
| 3381 |
if (options.force_zero_initialized_variables && var.storage == StorageClassPrivate && |
| 3382 |
!var.initializer && !var.static_expression && type_can_zero_initialize(get_variable_data_type(var))) |
| 3383 |
{ |
| 3384 |
initializer = join(" = ", to_zero_initialized_expression(get_variable_data_type_id(var))); |
| 3385 |
} |
| 3386 |
|
| 3387 |
statement(variable_decl(var), initializer, ";"); |
| 3388 |
emitted = true; |
| 3389 |
} |
| 3390 |
} |
| 3391 |
} |
| 3392 |
|
| 3393 |
if (emitted) |
| 3394 |
statement(""); |
| 3395 |
|
| 3396 |
declare_undefined_values(); |
| 3397 |
} |
| 3398 |
|
| 3399 |
void CompilerGLSL::emit_extension_workarounds(spv::ExecutionModel model) |
| 3400 |
{ |
| 3401 |
static const char *workaround_types[] = { "int", "ivec2", "ivec3", "ivec4", "uint", "uvec2", "uvec3", "uvec4", |
| 3402 |
"float", "vec2", "vec3", "vec4", "double", "dvec2", "dvec3", "dvec4" }; |
| 3403 |
|
| 3404 |
if (!options.vulkan_semantics) |
| 3405 |
{ |
| 3406 |
using Supp = ShaderSubgroupSupportHelper; |
| 3407 |
auto result = shader_subgroup_supporter.resolve(); |
| 3408 |
|
| 3409 |
if (shader_subgroup_supporter.is_feature_requested(Supp::SubgroupMask)) |
| 3410 |
{ |
| 3411 |
auto exts = Supp::get_candidates_for_feature(Supp::SubgroupMask, result); |
| 3412 |
|
| 3413 |
for (auto &e : exts) |
| 3414 |
{ |
| 3415 |
const char *name = Supp::get_extension_name(e); |
| 3416 |
statement(&e == &exts.front() ? "#if" : "#elif", " defined(", name, ")"); |
| 3417 |
|
| 3418 |
switch (e) |
| 3419 |
{ |
| 3420 |
case Supp::NV_shader_thread_group: |
| 3421 |
statement("#define gl_SubgroupEqMask uvec4(gl_ThreadEqMaskNV, 0u, 0u, 0u)"); |
| 3422 |
statement("#define gl_SubgroupGeMask uvec4(gl_ThreadGeMaskNV, 0u, 0u, 0u)"); |
| 3423 |
statement("#define gl_SubgroupGtMask uvec4(gl_ThreadGtMaskNV, 0u, 0u, 0u)"); |
| 3424 |
statement("#define gl_SubgroupLeMask uvec4(gl_ThreadLeMaskNV, 0u, 0u, 0u)"); |
| 3425 |
statement("#define gl_SubgroupLtMask uvec4(gl_ThreadLtMaskNV, 0u, 0u, 0u)"); |
| 3426 |
break; |
| 3427 |
case Supp::ARB_shader_ballot: |
| 3428 |
statement("#define gl_SubgroupEqMask uvec4(unpackUint2x32(gl_SubGroupEqMaskARB), 0u, 0u)"); |
| 3429 |
statement("#define gl_SubgroupGeMask uvec4(unpackUint2x32(gl_SubGroupGeMaskARB), 0u, 0u)"); |
| 3430 |
statement("#define gl_SubgroupGtMask uvec4(unpackUint2x32(gl_SubGroupGtMaskARB), 0u, 0u)"); |
| 3431 |
statement("#define gl_SubgroupLeMask uvec4(unpackUint2x32(gl_SubGroupLeMaskARB), 0u, 0u)"); |
| 3432 |
statement("#define gl_SubgroupLtMask uvec4(unpackUint2x32(gl_SubGroupLtMaskARB), 0u, 0u)"); |
| 3433 |
break; |
| 3434 |
default: |
| 3435 |
break; |
| 3436 |
} |
| 3437 |
} |
| 3438 |
statement("#endif"); |
| 3439 |
statement(""); |
| 3440 |
} |
| 3441 |
|
| 3442 |
if (shader_subgroup_supporter.is_feature_requested(Supp::SubgroupSize)) |
| 3443 |
{ |
| 3444 |
auto exts = Supp::get_candidates_for_feature(Supp::SubgroupSize, result); |
| 3445 |
|
| 3446 |
for (auto &e : exts) |
| 3447 |
{ |
| 3448 |
const char *name = Supp::get_extension_name(e); |
| 3449 |
statement(&e == &exts.front() ? "#if" : "#elif", " defined(", name, ")"); |
| 3450 |
|
| 3451 |
switch (e) |
| 3452 |
{ |
| 3453 |
case Supp::NV_shader_thread_group: |
| 3454 |
statement("#define gl_SubgroupSize gl_WarpSizeNV"); |
| 3455 |
break; |
| 3456 |
case Supp::ARB_shader_ballot: |
| 3457 |
statement("#define gl_SubgroupSize gl_SubGroupSizeARB"); |
| 3458 |
break; |
| 3459 |
case Supp::AMD_gcn_shader: |
| 3460 |
statement("#define gl_SubgroupSize uint(gl_SIMDGroupSizeAMD)"); |
| 3461 |
break; |
| 3462 |
default: |
| 3463 |
break; |
| 3464 |
} |
| 3465 |
} |
| 3466 |
statement("#endif"); |
| 3467 |
statement(""); |
| 3468 |
} |
| 3469 |
|
| 3470 |
if (shader_subgroup_supporter.is_feature_requested(Supp::SubgroupInvocationID)) |
| 3471 |
{ |
| 3472 |
auto exts = Supp::get_candidates_for_feature(Supp::SubgroupInvocationID, result); |
| 3473 |
|
| 3474 |
for (auto &e : exts) |
| 3475 |
{ |
| 3476 |
const char *name = Supp::get_extension_name(e); |
| 3477 |
statement(&e == &exts.front() ? "#if" : "#elif", " defined(", name, ")"); |
| 3478 |
|
| 3479 |
switch (e) |
| 3480 |
{ |
| 3481 |
case Supp::NV_shader_thread_group: |
| 3482 |
statement("#define gl_SubgroupInvocationID gl_ThreadInWarpNV"); |
| 3483 |
break; |
| 3484 |
case Supp::ARB_shader_ballot: |
| 3485 |
statement("#define gl_SubgroupInvocationID gl_SubGroupInvocationARB"); |
| 3486 |
break; |
| 3487 |
default: |
| 3488 |
break; |
| 3489 |
} |
| 3490 |
} |
| 3491 |
statement("#endif"); |
| 3492 |
statement(""); |
| 3493 |
} |
| 3494 |
|
| 3495 |
if (shader_subgroup_supporter.is_feature_requested(Supp::SubgroupID)) |
| 3496 |
{ |
| 3497 |
auto exts = Supp::get_candidates_for_feature(Supp::SubgroupID, result); |
| 3498 |
|
| 3499 |
for (auto &e : exts) |
| 3500 |
{ |
| 3501 |
const char *name = Supp::get_extension_name(e); |
| 3502 |
statement(&e == &exts.front() ? "#if" : "#elif", " defined(", name, ")"); |
| 3503 |
|
| 3504 |
switch (e) |
| 3505 |
{ |
| 3506 |
case Supp::NV_shader_thread_group: |
| 3507 |
statement("#define gl_SubgroupID gl_WarpIDNV"); |
| 3508 |
break; |
| 3509 |
default: |
| 3510 |
break; |
| 3511 |
} |
| 3512 |
} |
| 3513 |
statement("#endif"); |
| 3514 |
statement(""); |
| 3515 |
} |
| 3516 |
|
| 3517 |
if (shader_subgroup_supporter.is_feature_requested(Supp::NumSubgroups)) |
| 3518 |
{ |
| 3519 |
auto exts = Supp::get_candidates_for_feature(Supp::NumSubgroups, result); |
| 3520 |
|
| 3521 |
for (auto &e : exts) |
| 3522 |
{ |
| 3523 |
const char *name = Supp::get_extension_name(e); |
| 3524 |
statement(&e == &exts.front() ? "#if" : "#elif", " defined(", name, ")"); |
| 3525 |
|
| 3526 |
switch (e) |
| 3527 |
{ |
| 3528 |
case Supp::NV_shader_thread_group: |
| 3529 |
statement("#define gl_NumSubgroups gl_WarpsPerSMNV"); |
| 3530 |
break; |
| 3531 |
default: |
| 3532 |
break; |
| 3533 |
} |
| 3534 |
} |
| 3535 |
statement("#endif"); |
| 3536 |
statement(""); |
| 3537 |
} |
| 3538 |
|
| 3539 |
if (shader_subgroup_supporter.is_feature_requested(Supp::SubgroupBrodcast_First)) |
| 3540 |
{ |
| 3541 |
auto exts = Supp::get_candidates_for_feature(Supp::SubgroupBrodcast_First, result); |
| 3542 |
|
| 3543 |
for (auto &e : exts) |
| 3544 |
{ |
| 3545 |
const char *name = Supp::get_extension_name(e); |
| 3546 |
statement(&e == &exts.front() ? "#if" : "#elif", " defined(", name, ")"); |
| 3547 |
|
| 3548 |
switch (e) |
| 3549 |
{ |
| 3550 |
case Supp::NV_shader_thread_shuffle: |
| 3551 |
for (const char *t : workaround_types) |
| 3552 |
{ |
| 3553 |
statement(t, " subgroupBroadcastFirst(", t, |
| 3554 |
" value) { return shuffleNV(value, findLSB(ballotThreadNV(true)), gl_WarpSizeNV); }"); |
| 3555 |
} |
| 3556 |
for (const char *t : workaround_types) |
| 3557 |
{ |
| 3558 |
statement(t, " subgroupBroadcast(", t, |
| 3559 |
" value, uint id) { return shuffleNV(value, id, gl_WarpSizeNV); }"); |
| 3560 |
} |
| 3561 |
break; |
| 3562 |
case Supp::ARB_shader_ballot: |
| 3563 |
for (const char *t : workaround_types) |
| 3564 |
{ |
| 3565 |
statement(t, " subgroupBroadcastFirst(", t, |
| 3566 |
" value) { return readFirstInvocationARB(value); }"); |
| 3567 |
} |
| 3568 |
for (const char *t : workaround_types) |
| 3569 |
{ |
| 3570 |
statement(t, " subgroupBroadcast(", t, |
| 3571 |
" value, uint id) { return readInvocationARB(value, id); }"); |
| 3572 |
} |
| 3573 |
break; |
| 3574 |
default: |
| 3575 |
break; |
| 3576 |
} |
| 3577 |
} |
| 3578 |
statement("#endif"); |
| 3579 |
statement(""); |
| 3580 |
} |
| 3581 |
|
| 3582 |
if (shader_subgroup_supporter.is_feature_requested(Supp::SubgroupBallotFindLSB_MSB)) |
| 3583 |
{ |
| 3584 |
auto exts = Supp::get_candidates_for_feature(Supp::SubgroupBallotFindLSB_MSB, result); |
| 3585 |
|
| 3586 |
for (auto &e : exts) |
| 3587 |
{ |
| 3588 |
const char *name = Supp::get_extension_name(e); |
| 3589 |
statement(&e == &exts.front() ? "#if" : "#elif", " defined(", name, ")"); |
| 3590 |
|
| 3591 |
switch (e) |
| 3592 |
{ |
| 3593 |
case Supp::NV_shader_thread_group: |
| 3594 |
statement("uint subgroupBallotFindLSB(uvec4 value) { return findLSB(value.x); }"); |
| 3595 |
statement("uint subgroupBallotFindMSB(uvec4 value) { return findMSB(value.x); }"); |
| 3596 |
break; |
| 3597 |
default: |
| 3598 |
break; |
| 3599 |
} |
| 3600 |
} |
| 3601 |
statement("#else"); |
| 3602 |
statement("uint subgroupBallotFindLSB(uvec4 value)"); |
| 3603 |
begin_scope(); |
| 3604 |
statement("int firstLive = findLSB(value.x);"); |
| 3605 |
statement("return uint(firstLive != -1 ? firstLive : (findLSB(value.y) + 32));"); |
| 3606 |
end_scope(); |
| 3607 |
statement("uint subgroupBallotFindMSB(uvec4 value)"); |
| 3608 |
begin_scope(); |
| 3609 |
statement("int firstLive = findMSB(value.y);"); |
| 3610 |
statement("return uint(firstLive != -1 ? (firstLive + 32) : findMSB(value.x));"); |
| 3611 |
end_scope(); |
| 3612 |
statement("#endif"); |
| 3613 |
statement(""); |
| 3614 |
} |
| 3615 |
|
| 3616 |
if (shader_subgroup_supporter.is_feature_requested(Supp::SubgroupAll_Any_AllEqualBool)) |
| 3617 |
{ |
| 3618 |
auto exts = Supp::get_candidates_for_feature(Supp::SubgroupAll_Any_AllEqualBool, result); |
| 3619 |
|
| 3620 |
for (auto &e : exts) |
| 3621 |
{ |
| 3622 |
const char *name = Supp::get_extension_name(e); |
| 3623 |
statement(&e == &exts.front() ? "#if" : "#elif", " defined(", name, ")"); |
| 3624 |
|
| 3625 |
switch (e) |
| 3626 |
{ |
| 3627 |
case Supp::NV_gpu_shader_5: |
| 3628 |
statement("bool subgroupAll(bool value) { return allThreadsNV(value); }"); |
| 3629 |
statement("bool subgroupAny(bool value) { return anyThreadNV(value); }"); |
| 3630 |
statement("bool subgroupAllEqual(bool value) { return allThreadsEqualNV(value); }"); |
| 3631 |
break; |
| 3632 |
case Supp::ARB_shader_group_vote: |
| 3633 |
statement("bool subgroupAll(bool v) { return allInvocationsARB(v); }"); |
| 3634 |
statement("bool subgroupAny(bool v) { return anyInvocationARB(v); }"); |
| 3635 |
statement("bool subgroupAllEqual(bool v) { return allInvocationsEqualARB(v); }"); |
| 3636 |
break; |
| 3637 |
case Supp::AMD_gcn_shader: |
| 3638 |
statement("bool subgroupAll(bool value) { return ballotAMD(value) == ballotAMD(true); }"); |
| 3639 |
statement("bool subgroupAny(bool value) { return ballotAMD(value) != 0ull; }"); |
| 3640 |
statement("bool subgroupAllEqual(bool value) { uint64_t b = ballotAMD(value); return b == 0ull || " |
| 3641 |
"b == ballotAMD(true); }"); |
| 3642 |
break; |
| 3643 |
default: |
| 3644 |
break; |
| 3645 |
} |
| 3646 |
} |
| 3647 |
statement("#endif"); |
| 3648 |
statement(""); |
| 3649 |
} |
| 3650 |
|
| 3651 |
if (shader_subgroup_supporter.is_feature_requested(Supp::SubgroupAllEqualT)) |
| 3652 |
{ |
| 3653 |
statement("#ifndef GL_KHR_shader_subgroup_vote"); |
| 3654 |
statement( |
| 3655 |
"#define _SPIRV_CROSS_SUBGROUP_ALL_EQUAL_WORKAROUND(type) bool subgroupAllEqual(type value) { return " |
| 3656 |
"subgroupAllEqual(subgroupBroadcastFirst(value) == value); }"); |
| 3657 |
for (const char *t : workaround_types) |
| 3658 |
statement("_SPIRV_CROSS_SUBGROUP_ALL_EQUAL_WORKAROUND(", t, ")"); |
| 3659 |
statement("#undef _SPIRV_CROSS_SUBGROUP_ALL_EQUAL_WORKAROUND"); |
| 3660 |
statement("#endif"); |
| 3661 |
statement(""); |
| 3662 |
} |
| 3663 |
|
| 3664 |
if (shader_subgroup_supporter.is_feature_requested(Supp::SubgroupBallot)) |
| 3665 |
{ |
| 3666 |
auto exts = Supp::get_candidates_for_feature(Supp::SubgroupBallot, result); |
| 3667 |
|
| 3668 |
for (auto &e : exts) |
| 3669 |
{ |
| 3670 |
const char *name = Supp::get_extension_name(e); |
| 3671 |
statement(&e == &exts.front() ? "#if" : "#elif", " defined(", name, ")"); |
| 3672 |
|
| 3673 |
switch (e) |
| 3674 |
{ |
| 3675 |
case Supp::NV_shader_thread_group: |
| 3676 |
statement("uvec4 subgroupBallot(bool v) { return uvec4(ballotThreadNV(v), 0u, 0u, 0u); }"); |
| 3677 |
break; |
| 3678 |
case Supp::ARB_shader_ballot: |
| 3679 |
statement("uvec4 subgroupBallot(bool v) { return uvec4(unpackUint2x32(ballotARB(v)), 0u, 0u); }"); |
| 3680 |
break; |
| 3681 |
default: |
| 3682 |
break; |
| 3683 |
} |
| 3684 |
} |
| 3685 |
statement("#endif"); |
| 3686 |
statement(""); |
| 3687 |
} |
| 3688 |
|
| 3689 |
if (shader_subgroup_supporter.is_feature_requested(Supp::SubgroupElect)) |
| 3690 |
{ |
| 3691 |
statement("#ifndef GL_KHR_shader_subgroup_basic"); |
| 3692 |
statement("bool subgroupElect()"); |
| 3693 |
begin_scope(); |
| 3694 |
statement("uvec4 activeMask = subgroupBallot(true);"); |
| 3695 |
statement("uint firstLive = subgroupBallotFindLSB(activeMask);"); |
| 3696 |
statement("return gl_SubgroupInvocationID == firstLive;"); |
| 3697 |
end_scope(); |
| 3698 |
statement("#endif"); |
| 3699 |
statement(""); |
| 3700 |
} |
| 3701 |
|
| 3702 |
if (shader_subgroup_supporter.is_feature_requested(Supp::SubgroupBarrier)) |
| 3703 |
{ |
| 3704 |
// Extensions we're using in place of GL_KHR_shader_subgroup_basic state |
| 3705 |
// that subgroup execute in lockstep so this barrier is implicit. |
| 3706 |
// However the GL 4.6 spec also states that `barrier` implies a shared memory barrier, |
| 3707 |
// and a specific test of optimizing scans by leveraging lock-step invocation execution, |
| 3708 |
// has shown that a `memoryBarrierShared` is needed in place of a `subgroupBarrier`. |
| 3709 |
// https://github.com/buildaworldnet/IrrlichtBAW/commit/d8536857991b89a30a6b65d29441e51b64c2c7ad#diff-9f898d27be1ea6fc79b03d9b361e299334c1a347b6e4dc344ee66110c6aa596aR19 |
| 3710 |
statement("#ifndef GL_KHR_shader_subgroup_basic"); |
| 3711 |
statement("void subgroupBarrier() { memoryBarrierShared(); }"); |
| 3712 |
statement("#endif"); |
| 3713 |
statement(""); |
| 3714 |
} |
| 3715 |
|
| 3716 |
if (shader_subgroup_supporter.is_feature_requested(Supp::SubgroupMemBarrier)) |
| 3717 |
{ |
| 3718 |
if (model == spv::ExecutionModelGLCompute) |
| 3719 |
{ |
| 3720 |
statement("#ifndef GL_KHR_shader_subgroup_basic"); |
| 3721 |
statement("void subgroupMemoryBarrier() { groupMemoryBarrier(); }"); |
| 3722 |
statement("void subgroupMemoryBarrierBuffer() { groupMemoryBarrier(); }"); |
| 3723 |
statement("void subgroupMemoryBarrierShared() { memoryBarrierShared(); }"); |
| 3724 |
statement("void subgroupMemoryBarrierImage() { groupMemoryBarrier(); }"); |
| 3725 |
statement("#endif"); |
| 3726 |
} |
| 3727 |
else |
| 3728 |
{ |
| 3729 |
statement("#ifndef GL_KHR_shader_subgroup_basic"); |
| 3730 |
statement("void subgroupMemoryBarrier() { memoryBarrier(); }"); |
| 3731 |
statement("void subgroupMemoryBarrierBuffer() { memoryBarrierBuffer(); }"); |
| 3732 |
statement("void subgroupMemoryBarrierImage() { memoryBarrierImage(); }"); |
| 3733 |
statement("#endif"); |
| 3734 |
} |
| 3735 |
statement(""); |
| 3736 |
} |
| 3737 |
|
| 3738 |
if (shader_subgroup_supporter.is_feature_requested(Supp::SubgroupInverseBallot_InclBitCount_ExclBitCout)) |
| 3739 |
{ |
| 3740 |
statement("#ifndef GL_KHR_shader_subgroup_ballot"); |
| 3741 |
statement("bool subgroupInverseBallot(uvec4 value)"); |
| 3742 |
begin_scope(); |
| 3743 |
statement("return any(notEqual(value.xy & gl_SubgroupEqMask.xy, uvec2(0u)));"); |
| 3744 |
end_scope(); |
| 3745 |
|
| 3746 |
statement("uint subgroupBallotInclusiveBitCount(uvec4 value)"); |
| 3747 |
begin_scope(); |
| 3748 |
statement("uvec2 v = value.xy & gl_SubgroupLeMask.xy;"); |
| 3749 |
statement("ivec2 c = bitCount(v);"); |
| 3750 |
statement_no_indent("#ifdef GL_NV_shader_thread_group"); |
| 3751 |
statement("return uint(c.x);"); |
| 3752 |
statement_no_indent("#else"); |
| 3753 |
statement("return uint(c.x + c.y);"); |
| 3754 |
statement_no_indent("#endif"); |
| 3755 |
end_scope(); |
| 3756 |
|
| 3757 |
statement("uint subgroupBallotExclusiveBitCount(uvec4 value)"); |
| 3758 |
begin_scope(); |
| 3759 |
statement("uvec2 v = value.xy & gl_SubgroupLtMask.xy;"); |
| 3760 |
statement("ivec2 c = bitCount(v);"); |
| 3761 |
statement_no_indent("#ifdef GL_NV_shader_thread_group"); |
| 3762 |
statement("return uint(c.x);"); |
| 3763 |
statement_no_indent("#else"); |
| 3764 |
statement("return uint(c.x + c.y);"); |
| 3765 |
statement_no_indent("#endif"); |
| 3766 |
end_scope(); |
| 3767 |
statement("#endif"); |
| 3768 |
statement(""); |
| 3769 |
} |
| 3770 |
|
| 3771 |
if (shader_subgroup_supporter.is_feature_requested(Supp::SubgroupBallotBitCount)) |
| 3772 |
{ |
| 3773 |
statement("#ifndef GL_KHR_shader_subgroup_ballot"); |
| 3774 |
statement("uint subgroupBallotBitCount(uvec4 value)"); |
| 3775 |
begin_scope(); |
| 3776 |
statement("ivec2 c = bitCount(value.xy);"); |
| 3777 |
statement_no_indent("#ifdef GL_NV_shader_thread_group"); |
| 3778 |
statement("return uint(c.x);"); |
| 3779 |
statement_no_indent("#else"); |
| 3780 |
statement("return uint(c.x + c.y);"); |
| 3781 |
statement_no_indent("#endif"); |
| 3782 |
end_scope(); |
| 3783 |
statement("#endif"); |
| 3784 |
statement(""); |
| 3785 |
} |
| 3786 |
|
| 3787 |
if (shader_subgroup_supporter.is_feature_requested(Supp::SubgroupBallotBitExtract)) |
| 3788 |
{ |
| 3789 |
statement("#ifndef GL_KHR_shader_subgroup_ballot"); |
| 3790 |
statement("bool subgroupBallotBitExtract(uvec4 value, uint index)"); |
| 3791 |
begin_scope(); |
| 3792 |
statement_no_indent("#ifdef GL_NV_shader_thread_group"); |
| 3793 |
statement("uint shifted = value.x >> index;"); |
| 3794 |
statement_no_indent("#else"); |
| 3795 |
statement("uint shifted = value[index >> 5u] >> (index & 0x1fu);"); |
| 3796 |
statement_no_indent("#endif"); |
| 3797 |
statement("return (shifted & 1u) != 0u;"); |
| 3798 |
end_scope(); |
| 3799 |
statement("#endif"); |
| 3800 |
statement(""); |
| 3801 |
} |
| 3802 |
} |
| 3803 |
|
| 3804 |
if (!workaround_ubo_load_overload_types.empty()) |
| 3805 |
{ |
| 3806 |
for (auto &type_id : workaround_ubo_load_overload_types) |
| 3807 |
{ |
| 3808 |
auto &type = get<SPIRType>(type_id); |
| 3809 |
statement(type_to_glsl(type), " SPIRV_Cross_workaround_load_row_major(", type_to_glsl(type), |
| 3810 |
" wrap) { return wrap; }"); |
| 3811 |
} |
| 3812 |
statement(""); |
| 3813 |
} |
| 3814 |
|
| 3815 |
if (requires_transpose_2x2) |
| 3816 |
{ |
| 3817 |
statement("mat2 SPIRV_Cross_Transpose(mat2 m)"); |
| 3818 |
begin_scope(); |
| 3819 |
statement("return mat2(m[0][0], m[1][0], m[0][1], m[1][1]);"); |
| 3820 |
end_scope(); |
| 3821 |
statement(""); |
| 3822 |
} |
| 3823 |
|
| 3824 |
if (requires_transpose_3x3) |
| 3825 |
{ |
| 3826 |
statement("mat3 SPIRV_Cross_Transpose(mat3 m)"); |
| 3827 |
begin_scope(); |
| 3828 |
statement("return mat3(m[0][0], m[1][0], m[2][0], m[0][1], m[1][1], m[2][1], m[0][2], m[1][2], m[2][2]);"); |
| 3829 |
end_scope(); |
| 3830 |
statement(""); |
| 3831 |
} |
| 3832 |
|
| 3833 |
if (requires_transpose_4x4) |
| 3834 |
{ |
| 3835 |
statement("mat4 SPIRV_Cross_Transpose(mat4 m)"); |
| 3836 |
begin_scope(); |
| 3837 |
statement("return mat4(m[0][0], m[1][0], m[2][0], m[3][0], m[0][1], m[1][1], m[2][1], m[3][1], m[0][2], " |
| 3838 |
"m[1][2], m[2][2], m[3][2], m[0][3], m[1][3], m[2][3], m[3][3]);"); |
| 3839 |
end_scope(); |
| 3840 |
statement(""); |
| 3841 |
} |
| 3842 |
} |
| 3843 |
|
| 3844 |
// Returns a string representation of the ID, usable as a function arg. |
| 3845 |
// Default is to simply return the expression representation fo the arg ID. |
| 3846 |
// Subclasses may override to modify the return value. |
| 3847 |
string CompilerGLSL::to_func_call_arg(const SPIRFunction::Parameter &, uint32_t id) |
| 3848 |
{ |
| 3849 |
// Make sure that we use the name of the original variable, and not the parameter alias. |
| 3850 |
uint32_t name_id = id; |
| 3851 |
auto *var = maybe_get<SPIRVariable>(id); |
| 3852 |
if (var && var->basevariable) |
| 3853 |
name_id = var->basevariable; |
| 3854 |
return to_expression(name_id); |
| 3855 |
} |
| 3856 |
|
| 3857 |
void CompilerGLSL::handle_invalid_expression(uint32_t id) |
| 3858 |
{ |
| 3859 |
// We tried to read an invalidated expression. |
| 3860 |
// This means we need another pass at compilation, but next time, force temporary variables so that they cannot be invalidated. |
| 3861 |
forced_temporaries.insert(id); |
| 3862 |
force_recompile(); |
| 3863 |
} |
| 3864 |
|
| 3865 |
// Converts the format of the current expression from packed to unpacked, |
| 3866 |
// by wrapping the expression in a constructor of the appropriate type. |
| 3867 |
// GLSL does not support packed formats, so simply return the expression. |
| 3868 |
// Subclasses that do will override. |
| 3869 |
string CompilerGLSL::unpack_expression_type(string expr_str, const SPIRType &, uint32_t, bool, bool) |
| 3870 |
{ |
| 3871 |
return expr_str; |
| 3872 |
} |
| 3873 |
|
| 3874 |
// Sometimes we proactively enclosed an expression where it turns out we might have not needed it after all. |
| 3875 |
void CompilerGLSL::strip_enclosed_expression(string &expr) |
| 3876 |
{ |
| 3877 |
if (expr.size() < 2 || expr.front() != '(' || expr.back() != ')') |
| 3878 |
return; |
| 3879 |
|
| 3880 |
// Have to make sure that our first and last parens actually enclose everything inside it. |
| 3881 |
uint32_t paren_count = 0; |
| 3882 |
for (auto &c : expr) |
| 3883 |
{ |
| 3884 |
if (c == '(') |
| 3885 |
paren_count++; |
| 3886 |
else if (c == ')') |
| 3887 |
{ |
| 3888 |
paren_count--; |
| 3889 |
|
| 3890 |
// If we hit 0 and this is not the final char, our first and final parens actually don't |
| 3891 |
// enclose the expression, and we cannot strip, e.g.: (a + b) * (c + d). |
| 3892 |
if (paren_count == 0 && &c != &expr.back()) |
| 3893 |
return; |
| 3894 |
} |
| 3895 |
} |
| 3896 |
expr.erase(expr.size() - 1, 1); |
| 3897 |
expr.erase(begin(expr)); |
| 3898 |
} |
| 3899 |
|
| 3900 |
string CompilerGLSL::enclose_expression(const string &expr) |
| 3901 |
{ |
| 3902 |
bool need_parens = false; |
| 3903 |
|
| 3904 |
// If the expression starts with a unary we need to enclose to deal with cases where we have back-to-back |
| 3905 |
// unary expressions. |
| 3906 |
if (!expr.empty()) |
| 3907 |
{ |
| 3908 |
auto c = expr.front(); |
| 3909 |
if (c == '-' || c == '+' || c == '!' || c == '~' || c == '&' || c == '*') |
| 3910 |
need_parens = true; |
| 3911 |
} |
| 3912 |
|
| 3913 |
if (!need_parens) |
| 3914 |
{ |
| 3915 |
uint32_t paren_count = 0; |
| 3916 |
for (auto c : expr) |
| 3917 |
{ |
| 3918 |
if (c == '(' || c == '[') |
| 3919 |
paren_count++; |
| 3920 |
else if (c == ')' || c == ']') |
| 3921 |
{ |
| 3922 |
assert(paren_count); |
| 3923 |
paren_count--; |
| 3924 |
} |
| 3925 |
else if (c == ' ' && paren_count == 0) |
| 3926 |
{ |
| 3927 |
need_parens = true; |
| 3928 |
break; |
| 3929 |
} |
| 3930 |
} |
| 3931 |
assert(paren_count == 0); |
| 3932 |
} |
| 3933 |
|
| 3934 |
// If this expression contains any spaces which are not enclosed by parentheses, |
| 3935 |
// we need to enclose it so we can treat the whole string as an expression. |
| 3936 |
// This happens when two expressions have been part of a binary op earlier. |
| 3937 |
if (need_parens) |
| 3938 |
return join('(', expr, ')'); |
| 3939 |
else |
| 3940 |
return expr; |
| 3941 |
} |
| 3942 |
|
| 3943 |
string CompilerGLSL::dereference_expression(const SPIRType &expr_type, const std::string &expr) |
| 3944 |
{ |
| 3945 |
// If this expression starts with an address-of operator ('&'), then |
| 3946 |
// just return the part after the operator. |
| 3947 |
// TODO: Strip parens if unnecessary? |
| 3948 |
if (expr.front() == '&') |
| 3949 |
return expr.substr(1); |
| 3950 |
else if (backend.native_pointers) |
| 3951 |
return join('*', expr); |
| 3952 |
else if (expr_type.storage == StorageClassPhysicalStorageBufferEXT && expr_type.basetype != SPIRType::Struct && |
| 3953 |
expr_type.pointer_depth == 1) |
| 3954 |
{ |
| 3955 |
return join(enclose_expression(expr), ".value"); |
| 3956 |
} |
| 3957 |
else |
| 3958 |
return expr; |
| 3959 |
} |
| 3960 |
|
| 3961 |
string CompilerGLSL::address_of_expression(const std::string &expr) |
| 3962 |
{ |
| 3963 |
if (expr.size() > 3 && expr[0] == '(' && expr[1] == '*' && expr.back() == ')') |
| 3964 |
{ |
| 3965 |
// If we have an expression which looks like (*foo), taking the address of it is the same as stripping |
| 3966 |
// the first two and last characters. We might have to enclose the expression. |
| 3967 |
// This doesn't work for cases like (*foo + 10), |
| 3968 |
// but this is an r-value expression which we cannot take the address of anyways. |
| 3969 |
return enclose_expression(expr.substr(2, expr.size() - 3)); |
| 3970 |
} |
| 3971 |
else if (expr.front() == '*') |
| 3972 |
{ |
| 3973 |
// If this expression starts with a dereference operator ('*'), then |
| 3974 |
// just return the part after the operator. |
| 3975 |
return expr.substr(1); |
| 3976 |
} |
| 3977 |
else |
| 3978 |
return join('&', enclose_expression(expr)); |
| 3979 |
} |
| 3980 |
|
| 3981 |
// Just like to_expression except that we enclose the expression inside parentheses if needed. |
| 3982 |
string CompilerGLSL::to_enclosed_expression(uint32_t id, bool register_expression_read) |
| 3983 |
{ |
| 3984 |
return enclose_expression(to_expression(id, register_expression_read)); |
| 3985 |
} |
| 3986 |
|
| 3987 |
// Used explicitly when we want to read a row-major expression, but without any transpose shenanigans. |
| 3988 |
// need_transpose must be forced to false. |
| 3989 |
string CompilerGLSL::to_unpacked_row_major_matrix_expression(uint32_t id) |
| 3990 |
{ |
| 3991 |
return unpack_expression_type(to_expression(id), expression_type(id), |
| 3992 |
get_extended_decoration(id, SPIRVCrossDecorationPhysicalTypeID), |
| 3993 |
has_extended_decoration(id, SPIRVCrossDecorationPhysicalTypePacked), true); |
| 3994 |
} |
| 3995 |
|
| 3996 |
string CompilerGLSL::to_unpacked_expression(uint32_t id, bool register_expression_read) |
| 3997 |
{ |
| 3998 |
// If we need to transpose, it will also take care of unpacking rules. |
| 3999 |
auto *e = maybe_get<SPIRExpression>(id); |
| 4000 |
bool need_transpose = e && e->need_transpose; |
| 4001 |
bool is_remapped = has_extended_decoration(id, SPIRVCrossDecorationPhysicalTypeID); |
| 4002 |
bool is_packed = has_extended_decoration(id, SPIRVCrossDecorationPhysicalTypePacked); |
| 4003 |
|
| 4004 |
if (!need_transpose && (is_remapped || is_packed)) |
| 4005 |
{ |
| 4006 |
return unpack_expression_type(to_expression(id, register_expression_read), |
| 4007 |
get_pointee_type(expression_type_id(id)), |
| 4008 |
get_extended_decoration(id, SPIRVCrossDecorationPhysicalTypeID), |
| 4009 |
has_extended_decoration(id, SPIRVCrossDecorationPhysicalTypePacked), false); |
| 4010 |
} |
| 4011 |
else |
| 4012 |
return to_expression(id, register_expression_read); |
| 4013 |
} |
| 4014 |
|
| 4015 |
string CompilerGLSL::to_enclosed_unpacked_expression(uint32_t id, bool register_expression_read) |
| 4016 |
{ |
| 4017 |
// If we need to transpose, it will also take care of unpacking rules. |
| 4018 |
auto *e = maybe_get<SPIRExpression>(id); |
| 4019 |
bool need_transpose = e && e->need_transpose; |
| 4020 |
bool is_remapped = has_extended_decoration(id, SPIRVCrossDecorationPhysicalTypeID); |
| 4021 |
bool is_packed = has_extended_decoration(id, SPIRVCrossDecorationPhysicalTypePacked); |
| 4022 |
if (!need_transpose && (is_remapped || is_packed)) |
| 4023 |
{ |
| 4024 |
return unpack_expression_type(to_expression(id, register_expression_read), expression_type(id), |
| 4025 |
get_extended_decoration(id, SPIRVCrossDecorationPhysicalTypeID), |
| 4026 |
has_extended_decoration(id, SPIRVCrossDecorationPhysicalTypePacked), false); |
| 4027 |
} |
| 4028 |
else |
| 4029 |
return to_enclosed_expression(id, register_expression_read); |
| 4030 |
} |
| 4031 |
|
| 4032 |
string CompilerGLSL::to_dereferenced_expression(uint32_t id, bool register_expression_read) |
| 4033 |
{ |
| 4034 |
auto &type = expression_type(id); |
| 4035 |
if (type.pointer && should_dereference(id)) |
| 4036 |
return dereference_expression(type, to_enclosed_expression(id, register_expression_read)); |
| 4037 |
else |
| 4038 |
return to_expression(id, register_expression_read); |
| 4039 |
} |
| 4040 |
|
| 4041 |
string CompilerGLSL::to_pointer_expression(uint32_t id, bool register_expression_read) |
| 4042 |
{ |
| 4043 |
auto &type = expression_type(id); |
| 4044 |
if (type.pointer && expression_is_lvalue(id) && !should_dereference(id)) |
| 4045 |
return address_of_expression(to_enclosed_expression(id, register_expression_read)); |
| 4046 |
else |
| 4047 |
return to_unpacked_expression(id, register_expression_read); |
| 4048 |
} |
| 4049 |
|
| 4050 |
string CompilerGLSL::to_enclosed_pointer_expression(uint32_t id, bool register_expression_read) |
| 4051 |
{ |
| 4052 |
auto &type = expression_type(id); |
| 4053 |
if (type.pointer && expression_is_lvalue(id) && !should_dereference(id)) |
| 4054 |
return address_of_expression(to_enclosed_expression(id, register_expression_read)); |
| 4055 |
else |
| 4056 |
return to_enclosed_unpacked_expression(id, register_expression_read); |
| 4057 |
} |
| 4058 |
|
| 4059 |
string CompilerGLSL::to_extract_component_expression(uint32_t id, uint32_t index) |
| 4060 |
{ |
| 4061 |
auto expr = to_enclosed_expression(id); |
| 4062 |
if (has_extended_decoration(id, SPIRVCrossDecorationPhysicalTypePacked)) |
| 4063 |
return join(expr, "[", index, "]"); |
| 4064 |
else |
| 4065 |
return join(expr, ".", index_to_swizzle(index)); |
| 4066 |
} |
| 4067 |
|
| 4068 |
string CompilerGLSL::to_rerolled_array_expression(const string &base_expr, const SPIRType &type) |
| 4069 |
{ |
| 4070 |
uint32_t size = to_array_size_literal(type); |
| 4071 |
auto &parent = get<SPIRType>(type.parent_type); |
| 4072 |
string expr = "{ "; |
| 4073 |
|
| 4074 |
for (uint32_t i = 0; i < size; i++) |
| 4075 |
{ |
| 4076 |
auto subexpr = join(base_expr, "[", convert_to_string(i), "]"); |
| 4077 |
if (parent.array.empty()) |
| 4078 |
expr += subexpr; |
| 4079 |
else |
| 4080 |
expr += to_rerolled_array_expression(subexpr, parent); |
| 4081 |
|
| 4082 |
if (i + 1 < size) |
| 4083 |
expr += ", "; |
| 4084 |
} |
| 4085 |
|
| 4086 |
expr += " }"; |
| 4087 |
return expr; |
| 4088 |
} |
| 4089 |
|
| 4090 |
string CompilerGLSL::to_composite_constructor_expression(uint32_t id, bool uses_buffer_offset) |
| 4091 |
{ |
| 4092 |
auto &type = expression_type(id); |
| 4093 |
|
| 4094 |
bool reroll_array = !type.array.empty() && (!backend.array_is_value_type || |
| 4095 |
(uses_buffer_offset && !backend.buffer_offset_array_is_value_type)); |
| 4096 |
|
| 4097 |
if (reroll_array) |
| 4098 |
{ |
| 4099 |
// For this case, we need to "re-roll" an array initializer from a temporary. |
| 4100 |
// We cannot simply pass the array directly, since it decays to a pointer and it cannot |
| 4101 |
// participate in a struct initializer. E.g. |
| 4102 |
// float arr[2] = { 1.0, 2.0 }; |
| 4103 |
// Foo foo = { arr }; must be transformed to |
| 4104 |
// Foo foo = { { arr[0], arr[1] } }; |
| 4105 |
// The array sizes cannot be deduced from specialization constants since we cannot use any loops. |
| 4106 |
|
| 4107 |
// We're only triggering one read of the array expression, but this is fine since arrays have to be declared |
| 4108 |
// as temporaries anyways. |
| 4109 |
return to_rerolled_array_expression(to_enclosed_expression(id), type); |
| 4110 |
} |
| 4111 |
else |
| 4112 |
return to_unpacked_expression(id); |
| 4113 |
} |
| 4114 |
|
| 4115 |
string CompilerGLSL::to_expression(uint32_t id, bool register_expression_read) |
| 4116 |
{ |
| 4117 |
auto itr = invalid_expressions.find(id); |
| 4118 |
if (itr != end(invalid_expressions)) |
| 4119 |
handle_invalid_expression(id); |
| 4120 |
|
| 4121 |
if (ir.ids[id].get_type() == TypeExpression) |
| 4122 |
{ |
| 4123 |
// We might have a more complex chain of dependencies. |
| 4124 |
// A possible scenario is that we |
| 4125 |
// |
| 4126 |
// %1 = OpLoad |
| 4127 |
// %2 = OpDoSomething %1 %1. here %2 will have a dependency on %1. |
| 4128 |
// %3 = OpDoSomethingAgain %2 %2. Here %3 will lose the link to %1 since we don't propagate the dependencies like that. |
| 4129 |
// OpStore %1 %foo // Here we can invalidate %1, and hence all expressions which depend on %1. Only %2 will know since it's part of invalid_expressions. |
| 4130 |
// %4 = OpDoSomethingAnotherTime %3 %3 // If we forward all expressions we will see %1 expression after store, not before. |
| 4131 |
// |
| 4132 |
// However, we can propagate up a list of depended expressions when we used %2, so we can check if %2 is invalid when reading %3 after the store, |
| 4133 |
// and see that we should not forward reads of the original variable. |
| 4134 |
auto &expr = get<SPIRExpression>(id); |
| 4135 |
for (uint32_t dep : expr.expression_dependencies) |
| 4136 |
if (invalid_expressions.find(dep) != end(invalid_expressions)) |
| 4137 |
handle_invalid_expression(dep); |
| 4138 |
} |
| 4139 |
|
| 4140 |
if (register_expression_read) |
| 4141 |
track_expression_read(id); |
| 4142 |
|
| 4143 |
switch (ir.ids[id].get_type()) |
| 4144 |
{ |
| 4145 |
case TypeExpression: |
| 4146 |
{ |
| 4147 |
auto &e = get<SPIRExpression>(id); |
| 4148 |
if (e.base_expression) |
| 4149 |
return to_enclosed_expression(e.base_expression) + e.expression; |
| 4150 |
else if (e.need_transpose) |
| 4151 |
{ |
| 4152 |
// This should not be reached for access chains, since we always deal explicitly with transpose state |
| 4153 |
// when consuming an access chain expression. |
| 4154 |
uint32_t physical_type_id = get_extended_decoration(id, SPIRVCrossDecorationPhysicalTypeID); |
| 4155 |
bool is_packed = has_extended_decoration(id, SPIRVCrossDecorationPhysicalTypePacked); |
| 4156 |
return convert_row_major_matrix(e.expression, get<SPIRType>(e.expression_type), physical_type_id, |
| 4157 |
is_packed); |
| 4158 |
} |
| 4159 |
else if (flattened_structs.count(id)) |
| 4160 |
{ |
| 4161 |
return load_flattened_struct(e.expression, get<SPIRType>(e.expression_type)); |
| 4162 |
} |
| 4163 |
else |
| 4164 |
{ |
| 4165 |
if (is_forcing_recompilation()) |
| 4166 |
{ |
| 4167 |
// During first compilation phase, certain expression patterns can trigger exponential growth of memory. |
| 4168 |
// Avoid this by returning dummy expressions during this phase. |
| 4169 |
// Do not use empty expressions here, because those are sentinels for other cases. |
| 4170 |
return "_"; |
| 4171 |
} |
| 4172 |
else |
| 4173 |
return e.expression; |
| 4174 |
} |
| 4175 |
} |
| 4176 |
|
| 4177 |
case TypeConstant: |
| 4178 |
{ |
| 4179 |
auto &c = get<SPIRConstant>(id); |
| 4180 |
auto &type = get<SPIRType>(c.constant_type); |
| 4181 |
|
| 4182 |
// WorkGroupSize may be a constant. |
| 4183 |
auto &dec = ir.meta[c.self].decoration; |
| 4184 |
if (dec.builtin) |
| 4185 |
return builtin_to_glsl(dec.builtin_type, StorageClassGeneric); |
| 4186 |
else if (c.specialization) |
| 4187 |
return to_name(id); |
| 4188 |
else if (c.is_used_as_lut) |
| 4189 |
return to_name(id); |
| 4190 |
else if (type.basetype == SPIRType::Struct && !backend.can_declare_struct_inline) |
| 4191 |
return to_name(id); |
| 4192 |
else if (!type.array.empty() && !backend.can_declare_arrays_inline) |
| 4193 |
return to_name(id); |
| 4194 |
else |
| 4195 |
return constant_expression(c); |
| 4196 |
} |
| 4197 |
|
| 4198 |
case TypeConstantOp: |
| 4199 |
return to_name(id); |
| 4200 |
|
| 4201 |
case TypeVariable: |
| 4202 |
{ |
| 4203 |
auto &var = get<SPIRVariable>(id); |
| 4204 |
// If we try to use a loop variable before the loop header, we have to redirect it to the static expression, |
| 4205 |
// the variable has not been declared yet. |
| 4206 |
if (var.statically_assigned || (var.loop_variable && !var.loop_variable_enable)) |
| 4207 |
return to_expression(var.static_expression); |
| 4208 |
else if (var.deferred_declaration) |
| 4209 |
{ |
| 4210 |
var.deferred_declaration = false; |
| 4211 |
return variable_decl(var); |
| 4212 |
} |
| 4213 |
else if (flattened_structs.count(id)) |
| 4214 |
{ |
| 4215 |
return load_flattened_struct(to_name(id), get<SPIRType>(var.basetype)); |
| 4216 |
} |
| 4217 |
else |
| 4218 |
{ |
| 4219 |
auto &dec = ir.meta[var.self].decoration; |
| 4220 |
if (dec.builtin) |
| 4221 |
return builtin_to_glsl(dec.builtin_type, var.storage); |
| 4222 |
else |
| 4223 |
return to_name(id); |
| 4224 |
} |
| 4225 |
} |
| 4226 |
|
| 4227 |
case TypeCombinedImageSampler: |
| 4228 |
// This type should never be taken the expression of directly. |
| 4229 |
// The intention is that texture sampling functions will extract the image and samplers |
| 4230 |
// separately and take their expressions as needed. |
| 4231 |
// GLSL does not use this type because OpSampledImage immediately creates a combined image sampler |
| 4232 |
// expression ala sampler2D(texture, sampler). |
| 4233 |
SPIRV_CROSS_THROW("Combined image samplers have no default expression representation."); |
| 4234 |
|
| 4235 |
case TypeAccessChain: |
| 4236 |
// We cannot express this type. They only have meaning in other OpAccessChains, OpStore or OpLoad. |
| 4237 |
SPIRV_CROSS_THROW("Access chains have no default expression representation."); |
| 4238 |
|
| 4239 |
default: |
| 4240 |
return to_name(id); |
| 4241 |
} |
| 4242 |
} |
| 4243 |
|
| 4244 |
string CompilerGLSL::constant_op_expression(const SPIRConstantOp &cop) |
| 4245 |
{ |
| 4246 |
auto &type = get<SPIRType>(cop.basetype); |
| 4247 |
bool binary = false; |
| 4248 |
bool unary = false; |
| 4249 |
string op; |
| 4250 |
|
| 4251 |
if (is_legacy() && is_unsigned_opcode(cop.opcode)) |
| 4252 |
SPIRV_CROSS_THROW("Unsigned integers are not supported on legacy targets."); |
| 4253 |
|
| 4254 |
// TODO: Find a clean way to reuse emit_instruction. |
| 4255 |
switch (cop.opcode) |
| 4256 |
{ |
| 4257 |
case OpSConvert: |
| 4258 |
case OpUConvert: |
| 4259 |
case OpFConvert: |
| 4260 |
op = type_to_glsl_constructor(type); |
| 4261 |
break; |
| 4262 |
|
| 4263 |
#define GLSL_BOP(opname, x) \ |
| 4264 |
case Op##opname: \ |
| 4265 |
binary = true; \ |
| 4266 |
op = x; \ |
| 4267 |
break |
| 4268 |
|
| 4269 |
#define GLSL_UOP(opname, x) \ |
| 4270 |
case Op##opname: \ |
| 4271 |
unary = true; \ |
| 4272 |
op = x; \ |
| 4273 |
break |
| 4274 |
|
| 4275 |
GLSL_UOP(SNegate, "-"); |
| 4276 |
GLSL_UOP(Not, "~"); |
| 4277 |
GLSL_BOP(IAdd, "+"); |
| 4278 |
GLSL_BOP(ISub, "-"); |
| 4279 |
GLSL_BOP(IMul, "*"); |
| 4280 |
GLSL_BOP(SDiv, "/"); |
| 4281 |
GLSL_BOP(UDiv, "/"); |
| 4282 |
GLSL_BOP(UMod, "%"); |
| 4283 |
GLSL_BOP(SMod, "%"); |
| 4284 |
GLSL_BOP(ShiftRightLogical, ">>"); |
| 4285 |
GLSL_BOP(ShiftRightArithmetic, ">>"); |
| 4286 |
GLSL_BOP(ShiftLeftLogical, "<<"); |
| 4287 |
GLSL_BOP(BitwiseOr, "|"); |
| 4288 |
GLSL_BOP(BitwiseXor, "^"); |
| 4289 |
GLSL_BOP(BitwiseAnd, "&"); |
| 4290 |
GLSL_BOP(LogicalOr, "||"); |
| 4291 |
GLSL_BOP(LogicalAnd, "&&"); |
| 4292 |
GLSL_UOP(LogicalNot, "!"); |
| 4293 |
GLSL_BOP(LogicalEqual, "=="); |
| 4294 |
GLSL_BOP(LogicalNotEqual, "!="); |
| 4295 |
GLSL_BOP(IEqual, "=="); |
| 4296 |
GLSL_BOP(INotEqual, "!="); |
| 4297 |
GLSL_BOP(ULessThan, "<"); |
| 4298 |
GLSL_BOP(SLessThan, "<"); |
| 4299 |
GLSL_BOP(ULessThanEqual, "<="); |
| 4300 |
GLSL_BOP(SLessThanEqual, "<="); |
| 4301 |
GLSL_BOP(UGreaterThan, ">"); |
| 4302 |
GLSL_BOP(SGreaterThan, ">"); |
| 4303 |
GLSL_BOP(UGreaterThanEqual, ">="); |
| 4304 |
GLSL_BOP(SGreaterThanEqual, ">="); |
| 4305 |
|
| 4306 |
case OpSelect: |
| 4307 |
{ |
| 4308 |
if (cop.arguments.size() < 3) |
| 4309 |
SPIRV_CROSS_THROW("Not enough arguments to OpSpecConstantOp."); |
| 4310 |
|
| 4311 |
// This one is pretty annoying. It's triggered from |
| 4312 |
// uint(bool), int(bool) from spec constants. |
| 4313 |
// In order to preserve its compile-time constness in Vulkan GLSL, |
| 4314 |
// we need to reduce the OpSelect expression back to this simplified model. |
| 4315 |
// If we cannot, fail. |
| 4316 |
if (to_trivial_mix_op(type, op, cop.arguments[2], cop.arguments[1], cop.arguments[0])) |
| 4317 |
{ |
| 4318 |
// Implement as a simple cast down below. |
| 4319 |
} |
| 4320 |
else |
| 4321 |
{ |
| 4322 |
// Implement a ternary and pray the compiler understands it :) |
| 4323 |
return to_ternary_expression(type, cop.arguments[0], cop.arguments[1], cop.arguments[2]); |
| 4324 |
} |
| 4325 |
break; |
| 4326 |
} |
| 4327 |
|
| 4328 |
case OpVectorShuffle: |
| 4329 |
{ |
| 4330 |
string expr = type_to_glsl_constructor(type); |
| 4331 |
expr += "("; |
| 4332 |
|
| 4333 |
uint32_t left_components = expression_type(cop.arguments[0]).vecsize; |
| 4334 |
string left_arg = to_enclosed_expression(cop.arguments[0]); |
| 4335 |
string right_arg = to_enclosed_expression(cop.arguments[1]); |
| 4336 |
|
| 4337 |
for (uint32_t i = 2; i < uint32_t(cop.arguments.size()); i++) |
| 4338 |
{ |
| 4339 |
uint32_t index = cop.arguments[i]; |
| 4340 |
if (index >= left_components) |
| 4341 |
expr += right_arg + "." + "xyzw"[index - left_components]; |
| 4342 |
else |
| 4343 |
expr += left_arg + "." + "xyzw"[index]; |
| 4344 |
|
| 4345 |
if (i + 1 < uint32_t(cop.arguments.size())) |
| 4346 |
expr += ", "; |
| 4347 |
} |
| 4348 |
|
| 4349 |
expr += ")"; |
| 4350 |
return expr; |
| 4351 |
} |
| 4352 |
|
| 4353 |
case OpCompositeExtract: |
| 4354 |
{ |
| 4355 |
auto expr = access_chain_internal(cop.arguments[0], &cop.arguments[1], uint32_t(cop.arguments.size() - 1), |
| 4356 |
ACCESS_CHAIN_INDEX_IS_LITERAL_BIT, nullptr); |
| 4357 |
return expr; |
| 4358 |
} |
| 4359 |
|
| 4360 |
case OpCompositeInsert: |
| 4361 |
SPIRV_CROSS_THROW("OpCompositeInsert spec constant op is not supported."); |
| 4362 |
|
| 4363 |
default: |
| 4364 |
// Some opcodes are unimplemented here, these are currently not possible to test from glslang. |
| 4365 |
SPIRV_CROSS_THROW("Unimplemented spec constant op."); |
| 4366 |
} |
| 4367 |
|
| 4368 |
uint32_t bit_width = 0; |
| 4369 |
if (unary || binary || cop.opcode == OpSConvert || cop.opcode == OpUConvert) |
| 4370 |
bit_width = expression_type(cop.arguments[0]).width; |
| 4371 |
|
| 4372 |
SPIRType::BaseType input_type; |
| 4373 |
bool skip_cast_if_equal_type = opcode_is_sign_invariant(cop.opcode); |
| 4374 |
|
| 4375 |
switch (cop.opcode) |
| 4376 |
{ |
| 4377 |
case OpIEqual: |
| 4378 |
case OpINotEqual: |
| 4379 |
input_type = to_signed_basetype(bit_width); |
| 4380 |
break; |
| 4381 |
|
| 4382 |
case OpSLessThan: |
| 4383 |
case OpSLessThanEqual: |
| 4384 |
case OpSGreaterThan: |
| 4385 |
case OpSGreaterThanEqual: |
| 4386 |
case OpSMod: |
| 4387 |
case OpSDiv: |
| 4388 |
case OpShiftRightArithmetic: |
| 4389 |
case OpSConvert: |
| 4390 |
case OpSNegate: |
| 4391 |
input_type = to_signed_basetype(bit_width); |
| 4392 |
break; |
| 4393 |
|
| 4394 |
case OpULessThan: |
| 4395 |
case OpULessThanEqual: |
| 4396 |
case OpUGreaterThan: |
| 4397 |
case OpUGreaterThanEqual: |
| 4398 |
case OpUMod: |
| 4399 |
case OpUDiv: |
| 4400 |
case OpShiftRightLogical: |
| 4401 |
case OpUConvert: |
| 4402 |
input_type = to_unsigned_basetype(bit_width); |
| 4403 |
break; |
| 4404 |
|
| 4405 |
default: |
| 4406 |
input_type = type.basetype; |
| 4407 |
break; |
| 4408 |
} |
| 4409 |
|
| 4410 |
#undef GLSL_BOP |
| 4411 |
#undef GLSL_UOP |
| 4412 |
if (binary) |
| 4413 |
{ |
| 4414 |
if (cop.arguments.size() < 2) |
| 4415 |
SPIRV_CROSS_THROW("Not enough arguments to OpSpecConstantOp."); |
| 4416 |
|
| 4417 |
string cast_op0; |
| 4418 |
string cast_op1; |
| 4419 |
auto expected_type = binary_op_bitcast_helper(cast_op0, cast_op1, input_type, cop.arguments[0], |
| 4420 |
cop.arguments[1], skip_cast_if_equal_type); |
| 4421 |
|
| 4422 |
if (type.basetype != input_type && type.basetype != SPIRType::Boolean) |
| 4423 |
{ |
| 4424 |
expected_type.basetype = input_type; |
| 4425 |
auto expr = bitcast_glsl_op(type, expected_type); |
| 4426 |
expr += '('; |
| 4427 |
expr += join(cast_op0, " ", op, " ", cast_op1); |
| 4428 |
expr += ')'; |
| 4429 |
return expr; |
| 4430 |
} |
| 4431 |
else |
| 4432 |
return join("(", cast_op0, " ", op, " ", cast_op1, ")"); |
| 4433 |
} |
| 4434 |
else if (unary) |
| 4435 |
{ |
| 4436 |
if (cop.arguments.size() < 1) |
| 4437 |
SPIRV_CROSS_THROW("Not enough arguments to OpSpecConstantOp."); |
| 4438 |
|
| 4439 |
// Auto-bitcast to result type as needed. |
| 4440 |
// Works around various casting scenarios in glslang as there is no OpBitcast for specialization constants. |
| 4441 |
return join("(", op, bitcast_glsl(type, cop.arguments[0]), ")"); |
| 4442 |
} |
| 4443 |
else if (cop.opcode == OpSConvert || cop.opcode == OpUConvert) |
| 4444 |
{ |
| 4445 |
if (cop.arguments.size() < 1) |
| 4446 |
SPIRV_CROSS_THROW("Not enough arguments to OpSpecConstantOp."); |
| 4447 |
|
| 4448 |
auto &arg_type = expression_type(cop.arguments[0]); |
| 4449 |
if (arg_type.width < type.width && input_type != arg_type.basetype) |
| 4450 |
{ |
| 4451 |
auto expected = arg_type; |
| 4452 |
expected.basetype = input_type; |
| 4453 |
return join(op, "(", bitcast_glsl(expected, cop.arguments[0]), ")"); |
| 4454 |
} |
| 4455 |
else |
| 4456 |
return join(op, "(", to_expression(cop.arguments[0]), ")"); |
| 4457 |
} |
| 4458 |
else |
| 4459 |
{ |
| 4460 |
if (cop.arguments.size() < 1) |
| 4461 |
SPIRV_CROSS_THROW("Not enough arguments to OpSpecConstantOp."); |
| 4462 |
return join(op, "(", to_expression(cop.arguments[0]), ")"); |
| 4463 |
} |
| 4464 |
} |
| 4465 |
|
| 4466 |
string CompilerGLSL::constant_expression(const SPIRConstant &c) |
| 4467 |
{ |
| 4468 |
auto &type = get<SPIRType>(c.constant_type); |
| 4469 |
|
| 4470 |
if (type.pointer) |
| 4471 |
{ |
| 4472 |
return backend.null_pointer_literal; |
| 4473 |
} |
| 4474 |
else if (!c.subconstants.empty()) |
| 4475 |
{ |
| 4476 |
// Handles Arrays and structures. |
| 4477 |
string res; |
| 4478 |
|
| 4479 |
// Allow Metal to use the array<T> template to make arrays a value type |
| 4480 |
bool needs_trailing_tracket = false; |
| 4481 |
if (backend.use_initializer_list && backend.use_typed_initializer_list && type.basetype == SPIRType::Struct && |
| 4482 |
type.array.empty()) |
| 4483 |
{ |
| 4484 |
res = type_to_glsl_constructor(type) + "{ "; |
| 4485 |
} |
| 4486 |
else if (backend.use_initializer_list && backend.use_typed_initializer_list && backend.array_is_value_type && |
| 4487 |
!type.array.empty()) |
| 4488 |
{ |
| 4489 |
res = type_to_glsl_constructor(type) + "({ "; |
| 4490 |
needs_trailing_tracket = true; |
| 4491 |
} |
| 4492 |
else if (backend.use_initializer_list) |
| 4493 |
{ |
| 4494 |
res = "{ "; |
| 4495 |
} |
| 4496 |
else |
| 4497 |
{ |
| 4498 |
res = type_to_glsl_constructor(type) + "("; |
| 4499 |
} |
| 4500 |
|
| 4501 |
for (auto &elem : c.subconstants) |
| 4502 |
{ |
| 4503 |
auto &subc = get<SPIRConstant>(elem); |
| 4504 |
if (subc.specialization) |
| 4505 |
res += to_name(elem); |
| 4506 |
else |
| 4507 |
res += constant_expression(subc); |
| 4508 |
|
| 4509 |
if (&elem != &c.subconstants.back()) |
| 4510 |
res += ", "; |
| 4511 |
} |
| 4512 |
|
| 4513 |
res += backend.use_initializer_list ? " }" : ")"; |
| 4514 |
if (needs_trailing_tracket) |
| 4515 |
res += ")"; |
| 4516 |
|
| 4517 |
return res; |
| 4518 |
} |
| 4519 |
else if (type.basetype == SPIRType::Struct && type.member_types.size() == 0) |
| 4520 |
{ |
| 4521 |
// Metal tessellation likes empty structs which are then constant expressions. |
| 4522 |
if (backend.supports_empty_struct) |
| 4523 |
return "{ }"; |
| 4524 |
else if (backend.use_typed_initializer_list) |
| 4525 |
return join(type_to_glsl(get<SPIRType>(c.constant_type)), "{ 0 }"); |
| 4526 |
else if (backend.use_initializer_list) |
| 4527 |
return "{ 0 }"; |
| 4528 |
else |
| 4529 |
return join(type_to_glsl(get<SPIRType>(c.constant_type)), "(0)"); |
| 4530 |
} |
| 4531 |
else if (c.columns() == 1) |
| 4532 |
{ |
| 4533 |
return constant_expression_vector(c, 0); |
| 4534 |
} |
| 4535 |
else |
| 4536 |
{ |
| 4537 |
string res = type_to_glsl(get<SPIRType>(c.constant_type)) + "("; |
| 4538 |
for (uint32_t col = 0; col < c.columns(); col++) |
| 4539 |
{ |
| 4540 |
if (c.specialization_constant_id(col) != 0) |
| 4541 |
res += to_name(c.specialization_constant_id(col)); |
| 4542 |
else |
| 4543 |
res += constant_expression_vector(c, col); |
| 4544 |
|
| 4545 |
if (col + 1 < c.columns()) |
| 4546 |
res += ", "; |
| 4547 |
} |
| 4548 |
res += ")"; |
| 4549 |
return res; |
| 4550 |
} |
| 4551 |
} |
| 4552 |
|
| 4553 |
#ifdef _MSC_VER |
| 4554 |
// sprintf warning. |
| 4555 |
// We cannot rely on snprintf existing because, ..., MSVC. |
| 4556 |
#pragma warning(push) |
| 4557 |
#pragma warning(disable : 4996) |
| 4558 |
#endif |
| 4559 |
|
| 4560 |
string CompilerGLSL::convert_half_to_string(const SPIRConstant &c, uint32_t col, uint32_t row) |
| 4561 |
{ |
| 4562 |
string res; |
| 4563 |
float float_value = c.scalar_f16(col, row); |
| 4564 |
|
| 4565 |
// There is no literal "hf" in GL_NV_gpu_shader5, so to avoid lots |
| 4566 |
// of complicated workarounds, just value-cast to the half type always. |
| 4567 |
if (std::isnan(float_value) || std::isinf(float_value)) |
| 4568 |
{ |
| 4569 |
SPIRType type; |
| 4570 |
type.basetype = SPIRType::Half; |
| 4571 |
type.vecsize = 1; |
| 4572 |
type.columns = 1; |
| 4573 |
|
| 4574 |
if (float_value == numeric_limits<float>::infinity()) |
| 4575 |
res = join(type_to_glsl(type), "(1.0 / 0.0)"); |
| 4576 |
else if (float_value == -numeric_limits<float>::infinity()) |
| 4577 |
res = join(type_to_glsl(type), "(-1.0 / 0.0)"); |
| 4578 |
else if (std::isnan(float_value)) |
| 4579 |
res = join(type_to_glsl(type), "(0.0 / 0.0)"); |
| 4580 |
else |
| 4581 |
SPIRV_CROSS_THROW("Cannot represent non-finite floating point constant."); |
| 4582 |
} |
| 4583 |
else |
| 4584 |
{ |
| 4585 |
SPIRType type; |
| 4586 |
type.basetype = SPIRType::Half; |
| 4587 |
type.vecsize = 1; |
| 4588 |
type.columns = 1; |
| 4589 |
res = join(type_to_glsl(type), "(", convert_to_string(float_value, current_locale_radix_character), ")"); |
| 4590 |
} |
| 4591 |
|
| 4592 |
return res; |
| 4593 |
} |
| 4594 |
|
| 4595 |
string CompilerGLSL::convert_float_to_string(const SPIRConstant &c, uint32_t col, uint32_t row) |
| 4596 |
{ |
| 4597 |
string res; |
| 4598 |
float float_value = c.scalar_f32(col, row); |
| 4599 |
|
| 4600 |
if (std::isnan(float_value) || std::isinf(float_value)) |
| 4601 |
{ |
| 4602 |
// Use special representation. |
| 4603 |
if (!is_legacy()) |
| 4604 |
{ |
| 4605 |
SPIRType out_type; |
| 4606 |
SPIRType in_type; |
| 4607 |
out_type.basetype = SPIRType::Float; |
| 4608 |
in_type.basetype = SPIRType::UInt; |
| 4609 |
out_type.vecsize = 1; |
| 4610 |
in_type.vecsize = 1; |
| 4611 |
out_type.width = 32; |
| 4612 |
in_type.width = 32; |
| 4613 |
|
| 4614 |
char print_buffer[32]; |
| 4615 |
sprintf(print_buffer, "0x%xu", c.scalar(col, row)); |
| 4616 |
res = join(bitcast_glsl_op(out_type, in_type), "(", print_buffer, ")"); |
| 4617 |
} |
| 4618 |
else |
| 4619 |
{ |
| 4620 |
if (float_value == numeric_limits<float>::infinity()) |
| 4621 |
{ |
| 4622 |
if (backend.float_literal_suffix) |
| 4623 |
res = "(1.0f / 0.0f)"; |
| 4624 |
else |
| 4625 |
res = "(1.0 / 0.0)"; |
| 4626 |
} |
| 4627 |
else if (float_value == -numeric_limits<float>::infinity()) |
| 4628 |
{ |
| 4629 |
if (backend.float_literal_suffix) |
| 4630 |
res = "(-1.0f / 0.0f)"; |
| 4631 |
else |
| 4632 |
res = "(-1.0 / 0.0)"; |
| 4633 |
} |
| 4634 |
else if (std::isnan(float_value)) |
| 4635 |
{ |
| 4636 |
if (backend.float_literal_suffix) |
| 4637 |
res = "(0.0f / 0.0f)"; |
| 4638 |
else |
| 4639 |
res = "(0.0 / 0.0)"; |
| 4640 |
} |
| 4641 |
else |
| 4642 |
SPIRV_CROSS_THROW("Cannot represent non-finite floating point constant."); |
| 4643 |
} |
| 4644 |
} |
| 4645 |
else |
| 4646 |
{ |
| 4647 |
res = convert_to_string(float_value, current_locale_radix_character); |
| 4648 |
if (backend.float_literal_suffix) |
| 4649 |
res += "f"; |
| 4650 |
} |
| 4651 |
|
| 4652 |
return res; |
| 4653 |
} |
| 4654 |
|
| 4655 |
std::string CompilerGLSL::convert_double_to_string(const SPIRConstant &c, uint32_t col, uint32_t row) |
| 4656 |
{ |
| 4657 |
string res; |
| 4658 |
double double_value = c.scalar_f64(col, row); |
| 4659 |
|
| 4660 |
if (std::isnan(double_value) || std::isinf(double_value)) |
| 4661 |
{ |
| 4662 |
// Use special representation. |
| 4663 |
if (!is_legacy()) |
| 4664 |
{ |
| 4665 |
SPIRType out_type; |
| 4666 |
SPIRType in_type; |
| 4667 |
out_type.basetype = SPIRType::Double; |
| 4668 |
in_type.basetype = SPIRType::UInt64; |
| 4669 |
out_type.vecsize = 1; |
| 4670 |
in_type.vecsize = 1; |
| 4671 |
out_type.width = 64; |
| 4672 |
in_type.width = 64; |
| 4673 |
|
| 4674 |
uint64_t u64_value = c.scalar_u64(col, row); |
| 4675 |
|
| 4676 |
if (options.es) |
| 4677 |
SPIRV_CROSS_THROW("64-bit integers/float not supported in ES profile."); |
| 4678 |
require_extension_internal("GL_ARB_gpu_shader_int64"); |
| 4679 |
|
| 4680 |
char print_buffer[64]; |
| 4681 |
sprintf(print_buffer, "0x%llx%s", static_cast<unsigned long long>(u64_value), |
| 4682 |
backend.long_long_literal_suffix ? "ull" : "ul"); |
| 4683 |
res = join(bitcast_glsl_op(out_type, in_type), "(", print_buffer, ")"); |
| 4684 |
} |
| 4685 |
else |
| 4686 |
{ |
| 4687 |
if (options.es) |
| 4688 |
SPIRV_CROSS_THROW("FP64 not supported in ES profile."); |
| 4689 |
if (options.version < 400) |
| 4690 |
require_extension_internal("GL_ARB_gpu_shader_fp64"); |
| 4691 |
|
| 4692 |
if (double_value == numeric_limits<double>::infinity()) |
| 4693 |
{ |
| 4694 |
if (backend.double_literal_suffix) |
| 4695 |
res = "(1.0lf / 0.0lf)"; |
| 4696 |
else |
| 4697 |
res = "(1.0 / 0.0)"; |
| 4698 |
} |
| 4699 |
else if (double_value == -numeric_limits<double>::infinity()) |
| 4700 |
{ |
| 4701 |
if (backend.double_literal_suffix) |
| 4702 |
res = "(-1.0lf / 0.0lf)"; |
| 4703 |
else |
| 4704 |
res = "(-1.0 / 0.0)"; |
| 4705 |
} |
| 4706 |
else if (std::isnan(double_value)) |
| 4707 |
{ |
| 4708 |
if (backend.double_literal_suffix) |
| 4709 |
res = "(0.0lf / 0.0lf)"; |
| 4710 |
else |
| 4711 |
res = "(0.0 / 0.0)"; |
| 4712 |
} |
| 4713 |
else |
| 4714 |
SPIRV_CROSS_THROW("Cannot represent non-finite floating point constant."); |
| 4715 |
} |
| 4716 |
} |
| 4717 |
else |
| 4718 |
{ |
| 4719 |
res = convert_to_string(double_value, current_locale_radix_character); |
| 4720 |
if (backend.double_literal_suffix) |
| 4721 |
res += "lf"; |
| 4722 |
} |
| 4723 |
|
| 4724 |
return res; |
| 4725 |
} |
| 4726 |
|
| 4727 |
#ifdef _MSC_VER |
| 4728 |
#pragma warning(pop) |
| 4729 |
#endif |
| 4730 |
|
| 4731 |
string CompilerGLSL::constant_expression_vector(const SPIRConstant &c, uint32_t vector) |
| 4732 |
{ |
| 4733 |
auto type = get<SPIRType>(c.constant_type); |
| 4734 |
type.columns = 1; |
| 4735 |
|
| 4736 |
auto scalar_type = type; |
| 4737 |
scalar_type.vecsize = 1; |
| 4738 |
|
| 4739 |
string res; |
| 4740 |
bool splat = backend.use_constructor_splatting && c.vector_size() > 1; |
| 4741 |
bool swizzle_splat = backend.can_swizzle_scalar && c.vector_size() > 1; |
| 4742 |
|
| 4743 |
if (!type_is_floating_point(type)) |
| 4744 |
{ |
| 4745 |
// Cannot swizzle literal integers as a special case. |
| 4746 |
swizzle_splat = false; |
| 4747 |
} |
| 4748 |
|
| 4749 |
if (splat || swizzle_splat) |
| 4750 |
{ |
| 4751 |
// Cannot use constant splatting if we have specialization constants somewhere in the vector. |
| 4752 |
for (uint32_t i = 0; i < c.vector_size(); i++) |
| 4753 |
{ |
| 4754 |
if (c.specialization_constant_id(vector, i) != 0) |
| 4755 |
{ |
| 4756 |
splat = false; |
| 4757 |
swizzle_splat = false; |
| 4758 |
break; |
| 4759 |
} |
| 4760 |
} |
| 4761 |
} |
| 4762 |
|
| 4763 |
if (splat || swizzle_splat) |
| 4764 |
{ |
| 4765 |
if (type.width == 64) |
| 4766 |
{ |
| 4767 |
uint64_t ident = c.scalar_u64(vector, 0); |
| 4768 |
for (uint32_t i = 1; i < c.vector_size(); i++) |
| 4769 |
{ |
| 4770 |
if (ident != c.scalar_u64(vector, i)) |
| 4771 |
{ |
| 4772 |
splat = false; |
| 4773 |
swizzle_splat = false; |
| 4774 |
break; |
| 4775 |
} |
| 4776 |
} |
| 4777 |
} |
| 4778 |
else |
| 4779 |
{ |
| 4780 |
uint32_t ident = c.scalar(vector, 0); |
| 4781 |
for (uint32_t i = 1; i < c.vector_size(); i++) |
| 4782 |
{ |
| 4783 |
if (ident != c.scalar(vector, i)) |
| 4784 |
{ |
| 4785 |
splat = false; |
| 4786 |
swizzle_splat = false; |
| 4787 |
} |
| 4788 |
} |
| 4789 |
} |
| 4790 |
} |
| 4791 |
|
| 4792 |
if (c.vector_size() > 1 && !swizzle_splat) |
| 4793 |
res += type_to_glsl(type) + "("; |
| 4794 |
|
| 4795 |
switch (type.basetype) |
| 4796 |
{ |
| 4797 |
case SPIRType::Half: |
| 4798 |
if (splat || swizzle_splat) |
| 4799 |
{ |
| 4800 |
res += convert_half_to_string(c, vector, 0); |
| 4801 |
if (swizzle_splat) |
| 4802 |
res = remap_swizzle(get<SPIRType>(c.constant_type), 1, res); |
| 4803 |
} |
| 4804 |
else |
| 4805 |
{ |
| 4806 |
for (uint32_t i = 0; i < c.vector_size(); i++) |
| 4807 |
{ |
| 4808 |
if (c.vector_size() > 1 && c.specialization_constant_id(vector, i) != 0) |
| 4809 |
res += to_name(c.specialization_constant_id(vector, i)); |
| 4810 |
else |
| 4811 |
res += convert_half_to_string(c, vector, i); |
| 4812 |
|
| 4813 |
if (i + 1 < c.vector_size()) |
| 4814 |
res += ", "; |
| 4815 |
} |
| 4816 |
} |
| 4817 |
break; |
| 4818 |
|
| 4819 |
case SPIRType::Float: |
| 4820 |
if (splat || swizzle_splat) |
| 4821 |
{ |
| 4822 |
res += convert_float_to_string(c, vector, 0); |
| 4823 |
if (swizzle_splat) |
| 4824 |
res = remap_swizzle(get<SPIRType>(c.constant_type), 1, res); |
| 4825 |
} |
| 4826 |
else |
| 4827 |
{ |
| 4828 |
for (uint32_t i = 0; i < c.vector_size(); i++) |
| 4829 |
{ |
| 4830 |
if (c.vector_size() > 1 && c.specialization_constant_id(vector, i) != 0) |
| 4831 |
res += to_name(c.specialization_constant_id(vector, i)); |
| 4832 |
else |
| 4833 |
res += convert_float_to_string(c, vector, i); |
| 4834 |
|
| 4835 |
if (i + 1 < c.vector_size()) |
| 4836 |
res += ", "; |
| 4837 |
} |
| 4838 |
} |
| 4839 |
break; |
| 4840 |
|
| 4841 |
case SPIRType::Double: |
| 4842 |
if (splat || swizzle_splat) |
| 4843 |
{ |
| 4844 |
res += convert_double_to_string(c, vector, 0); |
| 4845 |
if (swizzle_splat) |
| 4846 |
res = remap_swizzle(get<SPIRType>(c.constant_type), 1, res); |
| 4847 |
} |
| 4848 |
else |
| 4849 |
{ |
| 4850 |
for (uint32_t i = 0; i < c.vector_size(); i++) |
| 4851 |
{ |
| 4852 |
if (c.vector_size() > 1 && c.specialization_constant_id(vector, i) != 0) |
| 4853 |
res += to_name(c.specialization_constant_id(vector, i)); |
| 4854 |
else |
| 4855 |
res += convert_double_to_string(c, vector, i); |
| 4856 |
|
| 4857 |
if (i + 1 < c.vector_size()) |
| 4858 |
res += ", "; |
| 4859 |
} |
| 4860 |
} |
| 4861 |
break; |
| 4862 |
|
| 4863 |
case SPIRType::Int64: |
| 4864 |
if (splat) |
| 4865 |
{ |
| 4866 |
res += convert_to_string(c.scalar_i64(vector, 0)); |
| 4867 |
if (backend.long_long_literal_suffix) |
| 4868 |
res += "ll"; |
| 4869 |
else |
| 4870 |
res += "l"; |
| 4871 |
} |
| 4872 |
else |
| 4873 |
{ |
| 4874 |
for (uint32_t i = 0; i < c.vector_size(); i++) |
| 4875 |
{ |
| 4876 |
if (c.vector_size() > 1 && c.specialization_constant_id(vector, i) != 0) |
| 4877 |
res += to_name(c.specialization_constant_id(vector, i)); |
| 4878 |
else |
| 4879 |
{ |
| 4880 |
res += convert_to_string(c.scalar_i64(vector, i)); |
| 4881 |
if (backend.long_long_literal_suffix) |
| 4882 |
res += "ll"; |
| 4883 |
else |
| 4884 |
res += "l"; |
| 4885 |
} |
| 4886 |
|
| 4887 |
if (i + 1 < c.vector_size()) |
| 4888 |
res += ", "; |
| 4889 |
} |
| 4890 |
} |
| 4891 |
break; |
| 4892 |
|
| 4893 |
case SPIRType::UInt64: |
| 4894 |
if (splat) |
| 4895 |
{ |
| 4896 |
res += convert_to_string(c.scalar_u64(vector, 0)); |
| 4897 |
if (backend.long_long_literal_suffix) |
| 4898 |
res += "ull"; |
| 4899 |
else |
| 4900 |
res += "ul"; |
| 4901 |
} |
| 4902 |
else |
| 4903 |
{ |
| 4904 |
for (uint32_t i = 0; i < c.vector_size(); i++) |
| 4905 |
{ |
| 4906 |
if (c.vector_size() > 1 && c.specialization_constant_id(vector, i) != 0) |
| 4907 |
res += to_name(c.specialization_constant_id(vector, i)); |
| 4908 |
else |
| 4909 |
{ |
| 4910 |
res += convert_to_string(c.scalar_u64(vector, i)); |
| 4911 |
if (backend.long_long_literal_suffix) |
| 4912 |
res += "ull"; |
| 4913 |
else |
| 4914 |
res += "ul"; |
| 4915 |
} |
| 4916 |
|
| 4917 |
if (i + 1 < c.vector_size()) |
| 4918 |
res += ", "; |
| 4919 |
} |
| 4920 |
} |
| 4921 |
break; |
| 4922 |
|
| 4923 |
case SPIRType::UInt: |
| 4924 |
if (splat) |
| 4925 |
{ |
| 4926 |
res += convert_to_string(c.scalar(vector, 0)); |
| 4927 |
if (is_legacy()) |
| 4928 |
{ |
| 4929 |
// Fake unsigned constant literals with signed ones if possible. |
| 4930 |
// Things like array sizes, etc, tend to be unsigned even though they could just as easily be signed. |
| 4931 |
if (c.scalar_i32(vector, 0) < 0) |
| 4932 |
SPIRV_CROSS_THROW("Tried to convert uint literal into int, but this made the literal negative."); |
| 4933 |
} |
| 4934 |
else if (backend.uint32_t_literal_suffix) |
| 4935 |
res += "u"; |
| 4936 |
} |
| 4937 |
else |
| 4938 |
{ |
| 4939 |
for (uint32_t i = 0; i < c.vector_size(); i++) |
| 4940 |
{ |
| 4941 |
if (c.vector_size() > 1 && c.specialization_constant_id(vector, i) != 0) |
| 4942 |
res += to_name(c.specialization_constant_id(vector, i)); |
| 4943 |
else |
| 4944 |
{ |
| 4945 |
res += convert_to_string(c.scalar(vector, i)); |
| 4946 |
if (is_legacy()) |
| 4947 |
{ |
| 4948 |
// Fake unsigned constant literals with signed ones if possible. |
| 4949 |
// Things like array sizes, etc, tend to be unsigned even though they could just as easily be signed. |
| 4950 |
if (c.scalar_i32(vector, i) < 0) |
| 4951 |
SPIRV_CROSS_THROW("Tried to convert uint literal into int, but this made " |
| 4952 |
"the literal negative."); |
| 4953 |
} |
| 4954 |
else if (backend.uint32_t_literal_suffix) |
| 4955 |
res += "u"; |
| 4956 |
} |
| 4957 |
|
| 4958 |
if (i + 1 < c.vector_size()) |
| 4959 |
res += ", "; |
| 4960 |
} |
| 4961 |
} |
| 4962 |
break; |
| 4963 |
|
| 4964 |
case SPIRType::Int: |
| 4965 |
if (splat) |
| 4966 |
res += convert_to_string(c.scalar_i32(vector, 0)); |
| 4967 |
else |
| 4968 |
{ |
| 4969 |
for (uint32_t i = 0; i < c.vector_size(); i++) |
| 4970 |
{ |
| 4971 |
if (c.vector_size() > 1 && c.specialization_constant_id(vector, i) != 0) |
| 4972 |
res += to_name(c.specialization_constant_id(vector, i)); |
| 4973 |
else |
| 4974 |
res += convert_to_string(c.scalar_i32(vector, i)); |
| 4975 |
if (i + 1 < c.vector_size()) |
| 4976 |
res += ", "; |
| 4977 |
} |
| 4978 |
} |
| 4979 |
break; |
| 4980 |
|
| 4981 |
case SPIRType::UShort: |
| 4982 |
if (splat) |
| 4983 |
{ |
| 4984 |
res += convert_to_string(c.scalar(vector, 0)); |
| 4985 |
} |
| 4986 |
else |
| 4987 |
{ |
| 4988 |
for (uint32_t i = 0; i < c.vector_size(); i++) |
| 4989 |
{ |
| 4990 |
if (c.vector_size() > 1 && c.specialization_constant_id(vector, i) != 0) |
| 4991 |
res += to_name(c.specialization_constant_id(vector, i)); |
| 4992 |
else |
| 4993 |
{ |
| 4994 |
if (*backend.uint16_t_literal_suffix) |
| 4995 |
{ |
| 4996 |
res += convert_to_string(c.scalar_u16(vector, i)); |
| 4997 |
res += backend.uint16_t_literal_suffix; |
| 4998 |
} |
| 4999 |
else |
| 5000 |
{ |
| 5001 |
// If backend doesn't have a literal suffix, we need to value cast. |
| 5002 |
res += type_to_glsl(scalar_type); |
| 5003 |
res += "("; |
| 5004 |
res += convert_to_string(c.scalar_u16(vector, i)); |
| 5005 |
res += ")"; |
| 5006 |
} |
| 5007 |
} |
| 5008 |
|
| 5009 |
if (i + 1 < c.vector_size()) |
| 5010 |
res += ", "; |
| 5011 |
} |
| 5012 |
} |
| 5013 |
break; |
| 5014 |
|
| 5015 |
case SPIRType::Short: |
| 5016 |
if (splat) |
| 5017 |
{ |
| 5018 |
res += convert_to_string(c.scalar_i16(vector, 0)); |
| 5019 |
} |
| 5020 |
else |
| 5021 |
{ |
| 5022 |
for (uint32_t i = 0; i < c.vector_size(); i++) |
| 5023 |
{ |
| 5024 |
if (c.vector_size() > 1 && c.specialization_constant_id(vector, i) != 0) |
| 5025 |
res += to_name(c.specialization_constant_id(vector, i)); |
| 5026 |
else |
| 5027 |
{ |
| 5028 |
if (*backend.int16_t_literal_suffix) |
| 5029 |
{ |
| 5030 |
res += convert_to_string(c.scalar_i16(vector, i)); |
| 5031 |
res += backend.int16_t_literal_suffix; |
| 5032 |
} |
| 5033 |
else |
| 5034 |
{ |
| 5035 |
// If backend doesn't have a literal suffix, we need to value cast. |
| 5036 |
res += type_to_glsl(scalar_type); |
| 5037 |
res += "("; |
| 5038 |
res += convert_to_string(c.scalar_i16(vector, i)); |
| 5039 |
res += ")"; |
| 5040 |
} |
| 5041 |
} |
| 5042 |
|
| 5043 |
if (i + 1 < c.vector_size()) |
| 5044 |
res += ", "; |
| 5045 |
} |
| 5046 |
} |
| 5047 |
break; |
| 5048 |
|
| 5049 |
case SPIRType::UByte: |
| 5050 |
if (splat) |
| 5051 |
{ |
| 5052 |
res += convert_to_string(c.scalar_u8(vector, 0)); |
| 5053 |
} |
| 5054 |
else |
| 5055 |
{ |
| 5056 |
for (uint32_t i = 0; i < c.vector_size(); i++) |
| 5057 |
{ |
| 5058 |
if (c.vector_size() > 1 && c.specialization_constant_id(vector, i) != 0) |
| 5059 |
res += to_name(c.specialization_constant_id(vector, i)); |
| 5060 |
else |
| 5061 |
{ |
| 5062 |
res += type_to_glsl(scalar_type); |
| 5063 |
res += "("; |
| 5064 |
res += convert_to_string(c.scalar_u8(vector, i)); |
| 5065 |
res += ")"; |
| 5066 |
} |
| 5067 |
|
| 5068 |
if (i + 1 < c.vector_size()) |
| 5069 |
res += ", "; |
| 5070 |
} |
| 5071 |
} |
| 5072 |
break; |
| 5073 |
|
| 5074 |
case SPIRType::SByte: |
| 5075 |
if (splat) |
| 5076 |
{ |
| 5077 |
res += convert_to_string(c.scalar_i8(vector, 0)); |
| 5078 |
} |
| 5079 |
else |
| 5080 |
{ |
| 5081 |
for (uint32_t i = 0; i < c.vector_size(); i++) |
| 5082 |
{ |
| 5083 |
if (c.vector_size() > 1 && c.specialization_constant_id(vector, i) != 0) |
| 5084 |
res += to_name(c.specialization_constant_id(vector, i)); |
| 5085 |
else |
| 5086 |
{ |
| 5087 |
res += type_to_glsl(scalar_type); |
| 5088 |
res += "("; |
| 5089 |
res += convert_to_string(c.scalar_i8(vector, i)); |
| 5090 |
res += ")"; |
| 5091 |
} |
| 5092 |
|
| 5093 |
if (i + 1 < c.vector_size()) |
| 5094 |
res += ", "; |
| 5095 |
} |
| 5096 |
} |
| 5097 |
break; |
| 5098 |
|
| 5099 |
case SPIRType::Boolean: |
| 5100 |
if (splat) |
| 5101 |
res += c.scalar(vector, 0) ? "true" : "false"; |
| 5102 |
else |
| 5103 |
{ |
| 5104 |
for (uint32_t i = 0; i < c.vector_size(); i++) |
| 5105 |
{ |
| 5106 |
if (c.vector_size() > 1 && c.specialization_constant_id(vector, i) != 0) |
| 5107 |
res += to_name(c.specialization_constant_id(vector, i)); |
| 5108 |
else |
| 5109 |
res += c.scalar(vector, i) ? "true" : "false"; |
| 5110 |
|
| 5111 |
if (i + 1 < c.vector_size()) |
| 5112 |
res += ", "; |
| 5113 |
} |
| 5114 |
} |
| 5115 |
break; |
| 5116 |
|
| 5117 |
default: |
| 5118 |
SPIRV_CROSS_THROW("Invalid constant expression basetype."); |
| 5119 |
} |
| 5120 |
|
| 5121 |
if (c.vector_size() > 1 && !swizzle_splat) |
| 5122 |
res += ")"; |
| 5123 |
|
| 5124 |
return res; |
| 5125 |
} |
| 5126 |
|
| 5127 |
SPIRExpression &CompilerGLSL::emit_uninitialized_temporary_expression(uint32_t type, uint32_t id) |
| 5128 |
{ |
| 5129 |
forced_temporaries.insert(id); |
| 5130 |
emit_uninitialized_temporary(type, id); |
| 5131 |
return set<SPIRExpression>(id, to_name(id), type, true); |
| 5132 |
} |
| 5133 |
|
| 5134 |
void CompilerGLSL::emit_uninitialized_temporary(uint32_t result_type, uint32_t result_id) |
| 5135 |
{ |
| 5136 |
// If we're declaring temporaries inside continue blocks, |
| 5137 |
// we must declare the temporary in the loop header so that the continue block can avoid declaring new variables. |
| 5138 |
if (current_continue_block && !hoisted_temporaries.count(result_id)) |
| 5139 |
{ |
| 5140 |
auto &header = get<SPIRBlock>(current_continue_block->loop_dominator); |
| 5141 |
if (find_if(begin(header.declare_temporary), end(header.declare_temporary), |
| 5142 |
[result_type, result_id](const pair<uint32_t, uint32_t> &tmp) { |
| 5143 |
return tmp.first == result_type && tmp.second == result_id; |
| 5144 |
}) == end(header.declare_temporary)) |
| 5145 |
{ |
| 5146 |
header.declare_temporary.emplace_back(result_type, result_id); |
| 5147 |
hoisted_temporaries.insert(result_id); |
| 5148 |
force_recompile(); |
| 5149 |
} |
| 5150 |
} |
| 5151 |
else if (hoisted_temporaries.count(result_id) == 0) |
| 5152 |
{ |
| 5153 |
auto &type = get<SPIRType>(result_type); |
| 5154 |
auto &flags = ir.meta[result_id].decoration.decoration_flags; |
| 5155 |
|
| 5156 |
// The result_id has not been made into an expression yet, so use flags interface. |
| 5157 |
add_local_variable_name(result_id); |
| 5158 |
|
| 5159 |
string initializer; |
| 5160 |
if (options.force_zero_initialized_variables && type_can_zero_initialize(type)) |
| 5161 |
initializer = join(" = ", to_zero_initialized_expression(result_type)); |
| 5162 |
|
| 5163 |
statement(flags_to_qualifiers_glsl(type, flags), variable_decl(type, to_name(result_id)), initializer, ";"); |
| 5164 |
} |
| 5165 |
} |
| 5166 |
|
| 5167 |
string CompilerGLSL::declare_temporary(uint32_t result_type, uint32_t result_id) |
| 5168 |
{ |
| 5169 |
auto &type = get<SPIRType>(result_type); |
| 5170 |
auto &flags = ir.meta[result_id].decoration.decoration_flags; |
| 5171 |
|
| 5172 |
// If we're declaring temporaries inside continue blocks, |
| 5173 |
// we must declare the temporary in the loop header so that the continue block can avoid declaring new variables. |
| 5174 |
if (current_continue_block && !hoisted_temporaries.count(result_id)) |
| 5175 |
{ |
| 5176 |
auto &header = get<SPIRBlock>(current_continue_block->loop_dominator); |
| 5177 |
if (find_if(begin(header.declare_temporary), end(header.declare_temporary), |
| 5178 |
[result_type, result_id](const pair<uint32_t, uint32_t> &tmp) { |
| 5179 |
return tmp.first == result_type && tmp.second == result_id; |
| 5180 |
}) == end(header.declare_temporary)) |
| 5181 |
{ |
| 5182 |
header.declare_temporary.emplace_back(result_type, result_id); |
| 5183 |
hoisted_temporaries.insert(result_id); |
| 5184 |
force_recompile(); |
| 5185 |
} |
| 5186 |
|
| 5187 |
return join(to_name(result_id), " = "); |
| 5188 |
} |
| 5189 |
else if (hoisted_temporaries.count(result_id)) |
| 5190 |
{ |
| 5191 |
// The temporary has already been declared earlier, so just "declare" the temporary by writing to it. |
| 5192 |
return join(to_name(result_id), " = "); |
| 5193 |
} |
| 5194 |
else |
| 5195 |
{ |
| 5196 |
// The result_id has not been made into an expression yet, so use flags interface. |
| 5197 |
add_local_variable_name(result_id); |
| 5198 |
return join(flags_to_qualifiers_glsl(type, flags), variable_decl(type, to_name(result_id)), " = "); |
| 5199 |
} |
| 5200 |
} |
| 5201 |
|
| 5202 |
bool CompilerGLSL::expression_is_forwarded(uint32_t id) const |
| 5203 |
{ |
| 5204 |
return forwarded_temporaries.count(id) != 0; |
| 5205 |
} |
| 5206 |
|
| 5207 |
bool CompilerGLSL::expression_suppresses_usage_tracking(uint32_t id) const |
| 5208 |
{ |
| 5209 |
return suppressed_usage_tracking.count(id) != 0; |
| 5210 |
} |
| 5211 |
|
| 5212 |
bool CompilerGLSL::expression_read_implies_multiple_reads(uint32_t id) const |
| 5213 |
{ |
| 5214 |
auto *expr = maybe_get<SPIRExpression>(id); |
| 5215 |
if (!expr) |
| 5216 |
return false; |
| 5217 |
|
| 5218 |
// If we're emitting code at a deeper loop level than when we emitted the expression, |
| 5219 |
// we're probably reading the same expression over and over. |
| 5220 |
return current_loop_level > expr->emitted_loop_level; |
| 5221 |
} |
| 5222 |
|
| 5223 |
SPIRExpression &CompilerGLSL::emit_op(uint32_t result_type, uint32_t result_id, const string &rhs, bool forwarding, |
| 5224 |
bool suppress_usage_tracking) |
| 5225 |
{ |
| 5226 |
if (forwarding && (forced_temporaries.find(result_id) == end(forced_temporaries))) |
| 5227 |
{ |
| 5228 |
// Just forward it without temporary. |
| 5229 |
// If the forward is trivial, we do not force flushing to temporary for this expression. |
| 5230 |
forwarded_temporaries.insert(result_id); |
| 5231 |
if (suppress_usage_tracking) |
| 5232 |
suppressed_usage_tracking.insert(result_id); |
| 5233 |
|
| 5234 |
return set<SPIRExpression>(result_id, rhs, result_type, true); |
| 5235 |
} |
| 5236 |
else |
| 5237 |
{ |
| 5238 |
// If expression isn't immutable, bind it to a temporary and make the new temporary immutable (they always are). |
| 5239 |
statement(declare_temporary(result_type, result_id), rhs, ";"); |
| 5240 |
return set<SPIRExpression>(result_id, to_name(result_id), result_type, true); |
| 5241 |
} |
| 5242 |
} |
| 5243 |
|
| 5244 |
void CompilerGLSL::emit_unary_op(uint32_t result_type, uint32_t result_id, uint32_t op0, const char *op) |
| 5245 |
{ |
| 5246 |
bool forward = should_forward(op0); |
| 5247 |
emit_op(result_type, result_id, join(op, to_enclosed_unpacked_expression(op0)), forward); |
| 5248 |
inherit_expression_dependencies(result_id, op0); |
| 5249 |
} |
| 5250 |
|
| 5251 |
void CompilerGLSL::emit_binary_op(uint32_t result_type, uint32_t result_id, uint32_t op0, uint32_t op1, const char *op) |
| 5252 |
{ |
| 5253 |
bool forward = should_forward(op0) && should_forward(op1); |
| 5254 |
emit_op(result_type, result_id, |
| 5255 |
join(to_enclosed_unpacked_expression(op0), " ", op, " ", to_enclosed_unpacked_expression(op1)), forward); |
| 5256 |
|
| 5257 |
inherit_expression_dependencies(result_id, op0); |
| 5258 |
inherit_expression_dependencies(result_id, op1); |
| 5259 |
} |
| 5260 |
|
| 5261 |
void CompilerGLSL::emit_unrolled_unary_op(uint32_t result_type, uint32_t result_id, uint32_t operand, const char *op) |
| 5262 |
{ |
| 5263 |
auto &type = get<SPIRType>(result_type); |
| 5264 |
auto expr = type_to_glsl_constructor(type); |
| 5265 |
expr += '('; |
| 5266 |
for (uint32_t i = 0; i < type.vecsize; i++) |
| 5267 |
{ |
| 5268 |
// Make sure to call to_expression multiple times to ensure |
| 5269 |
// that these expressions are properly flushed to temporaries if needed. |
| 5270 |
expr += op; |
| 5271 |
expr += to_extract_component_expression(operand, i); |
| 5272 |
|
| 5273 |
if (i + 1 < type.vecsize) |
| 5274 |
expr += ", "; |
| 5275 |
} |
| 5276 |
expr += ')'; |
| 5277 |
emit_op(result_type, result_id, expr, should_forward(operand)); |
| 5278 |
|
| 5279 |
inherit_expression_dependencies(result_id, operand); |
| 5280 |
} |
| 5281 |
|
| 5282 |
void CompilerGLSL::emit_unrolled_binary_op(uint32_t result_type, uint32_t result_id, uint32_t op0, uint32_t op1, |
| 5283 |
const char *op, bool negate, SPIRType::BaseType expected_type) |
| 5284 |
{ |
| 5285 |
auto &type0 = expression_type(op0); |
| 5286 |
auto &type1 = expression_type(op1); |
| 5287 |
|
| 5288 |
SPIRType target_type0 = type0; |
| 5289 |
SPIRType target_type1 = type1; |
| 5290 |
target_type0.basetype = expected_type; |
| 5291 |
target_type1.basetype = expected_type; |
| 5292 |
target_type0.vecsize = 1; |
| 5293 |
target_type1.vecsize = 1; |
| 5294 |
|
| 5295 |
auto &type = get<SPIRType>(result_type); |
| 5296 |
auto expr = type_to_glsl_constructor(type); |
| 5297 |
expr += '('; |
| 5298 |
for (uint32_t i = 0; i < type.vecsize; i++) |
| 5299 |
{ |
| 5300 |
// Make sure to call to_expression multiple times to ensure |
| 5301 |
// that these expressions are properly flushed to temporaries if needed. |
| 5302 |
if (negate) |
| 5303 |
expr += "!("; |
| 5304 |
|
| 5305 |
if (expected_type != SPIRType::Unknown && type0.basetype != expected_type) |
| 5306 |
expr += bitcast_expression(target_type0, type0.basetype, to_extract_component_expression(op0, i)); |
| 5307 |
else |
| 5308 |
expr += to_extract_component_expression(op0, i); |
| 5309 |
|
| 5310 |
expr += ' '; |
| 5311 |
expr += op; |
| 5312 |
expr += ' '; |
| 5313 |
|
| 5314 |
if (expected_type != SPIRType::Unknown && type1.basetype != expected_type) |
| 5315 |
expr += bitcast_expression(target_type1, type1.basetype, to_extract_component_expression(op1, i)); |
| 5316 |
else |
| 5317 |
expr += to_extract_component_expression(op1, i); |
| 5318 |
|
| 5319 |
if (negate) |
| 5320 |
expr += ")"; |
| 5321 |
|
| 5322 |
if (i + 1 < type.vecsize) |
| 5323 |
expr += ", "; |
| 5324 |
} |
| 5325 |
expr += ')'; |
| 5326 |
emit_op(result_type, result_id, expr, should_forward(op0) && should_forward(op1)); |
| 5327 |
|
| 5328 |
inherit_expression_dependencies(result_id, op0); |
| 5329 |
inherit_expression_dependencies(result_id, op1); |
| 5330 |
} |
| 5331 |
|
| 5332 |
SPIRType CompilerGLSL::binary_op_bitcast_helper(string &cast_op0, string &cast_op1, SPIRType::BaseType &input_type, |
| 5333 |
uint32_t op0, uint32_t op1, bool skip_cast_if_equal_type) |
| 5334 |
{ |
| 5335 |
auto &type0 = expression_type(op0); |
| 5336 |
auto &type1 = expression_type(op1); |
| 5337 |
|
| 5338 |
// We have to bitcast if our inputs are of different type, or if our types are not equal to expected inputs. |
| 5339 |
// For some functions like OpIEqual and INotEqual, we don't care if inputs are of different types than expected |
| 5340 |
// since equality test is exactly the same. |
| 5341 |
bool cast = (type0.basetype != type1.basetype) || (!skip_cast_if_equal_type && type0.basetype != input_type); |
| 5342 |
|
| 5343 |
// Create a fake type so we can bitcast to it. |
| 5344 |
// We only deal with regular arithmetic types here like int, uints and so on. |
| 5345 |
SPIRType expected_type; |
| 5346 |
expected_type.basetype = input_type; |
| 5347 |
expected_type.vecsize = type0.vecsize; |
| 5348 |
expected_type.columns = type0.columns; |
| 5349 |
expected_type.width = type0.width; |
| 5350 |
|
| 5351 |
if (cast) |
| 5352 |
{ |
| 5353 |
cast_op0 = bitcast_glsl(expected_type, op0); |
| 5354 |
cast_op1 = bitcast_glsl(expected_type, op1); |
| 5355 |
} |
| 5356 |
else |
| 5357 |
{ |
| 5358 |
// If we don't cast, our actual input type is that of the first (or second) argument. |
| 5359 |
cast_op0 = to_enclosed_unpacked_expression(op0); |
| 5360 |
cast_op1 = to_enclosed_unpacked_expression(op1); |
| 5361 |
input_type = type0.basetype; |
| 5362 |
} |
| 5363 |
|
| 5364 |
return expected_type; |
| 5365 |
} |
| 5366 |
|
| 5367 |
bool CompilerGLSL::emit_complex_bitcast(uint32_t result_type, uint32_t id, uint32_t op0) |
| 5368 |
{ |
| 5369 |
// Some bitcasts may require complex casting sequences, and are implemented here. |
| 5370 |
// Otherwise a simply unary function will do with bitcast_glsl_op. |
| 5371 |
|
| 5372 |
auto &output_type = get<SPIRType>(result_type); |
| 5373 |
auto &input_type = expression_type(op0); |
| 5374 |
string expr; |
| 5375 |
|
| 5376 |
if (output_type.basetype == SPIRType::Half && input_type.basetype == SPIRType::Float && input_type.vecsize == 1) |
| 5377 |
expr = join("unpackFloat2x16(floatBitsToUint(", to_unpacked_expression(op0), "))"); |
| 5378 |
else if (output_type.basetype == SPIRType::Float && input_type.basetype == SPIRType::Half && |
| 5379 |
input_type.vecsize == 2) |
| 5380 |
expr = join("uintBitsToFloat(packFloat2x16(", to_unpacked_expression(op0), "))"); |
| 5381 |
else |
| 5382 |
return false; |
| 5383 |
|
| 5384 |
emit_op(result_type, id, expr, should_forward(op0)); |
| 5385 |
return true; |
| 5386 |
} |
| 5387 |
|
| 5388 |
void CompilerGLSL::emit_binary_op_cast(uint32_t result_type, uint32_t result_id, uint32_t op0, uint32_t op1, |
| 5389 |
const char *op, SPIRType::BaseType input_type, bool skip_cast_if_equal_type) |
| 5390 |
{ |
| 5391 |
string cast_op0, cast_op1; |
| 5392 |
auto expected_type = binary_op_bitcast_helper(cast_op0, cast_op1, input_type, op0, op1, skip_cast_if_equal_type); |
| 5393 |
auto &out_type = get<SPIRType>(result_type); |
| 5394 |
|
| 5395 |
// We might have casted away from the result type, so bitcast again. |
| 5396 |
// For example, arithmetic right shift with uint inputs. |
| 5397 |
// Special case boolean outputs since relational opcodes output booleans instead of int/uint. |
| 5398 |
string expr; |
| 5399 |
if (out_type.basetype != input_type && out_type.basetype != SPIRType::Boolean) |
| 5400 |
{ |
| 5401 |
expected_type.basetype = input_type; |
| 5402 |
expr = bitcast_glsl_op(out_type, expected_type); |
| 5403 |
expr += '('; |
| 5404 |
expr += join(cast_op0, " ", op, " ", cast_op1); |
| 5405 |
expr += ')'; |
| 5406 |
} |
| 5407 |
else |
| 5408 |
expr += join(cast_op0, " ", op, " ", cast_op1); |
| 5409 |
|
| 5410 |
emit_op(result_type, result_id, expr, should_forward(op0) && should_forward(op1)); |
| 5411 |
inherit_expression_dependencies(result_id, op0); |
| 5412 |
inherit_expression_dependencies(result_id, op1); |
| 5413 |
} |
| 5414 |
|
| 5415 |
void CompilerGLSL::emit_unary_func_op(uint32_t result_type, uint32_t result_id, uint32_t op0, const char *op) |
| 5416 |
{ |
| 5417 |
bool forward = should_forward(op0); |
| 5418 |
emit_op(result_type, result_id, join(op, "(", to_unpacked_expression(op0), ")"), forward); |
| 5419 |
inherit_expression_dependencies(result_id, op0); |
| 5420 |
} |
| 5421 |
|
| 5422 |
void CompilerGLSL::emit_binary_func_op(uint32_t result_type, uint32_t result_id, uint32_t op0, uint32_t op1, |
| 5423 |
const char *op) |
| 5424 |
{ |
| 5425 |
bool forward = should_forward(op0) && should_forward(op1); |
| 5426 |
emit_op(result_type, result_id, join(op, "(", to_unpacked_expression(op0), ", ", to_unpacked_expression(op1), ")"), |
| 5427 |
forward); |
| 5428 |
inherit_expression_dependencies(result_id, op0); |
| 5429 |
inherit_expression_dependencies(result_id, op1); |
| 5430 |
} |
| 5431 |
|
| 5432 |
void CompilerGLSL::emit_unary_func_op_cast(uint32_t result_type, uint32_t result_id, uint32_t op0, const char *op, |
| 5433 |
SPIRType::BaseType input_type, SPIRType::BaseType expected_result_type) |
| 5434 |
{ |
| 5435 |
auto &out_type = get<SPIRType>(result_type); |
| 5436 |
auto &expr_type = expression_type(op0); |
| 5437 |
auto expected_type = out_type; |
| 5438 |
|
| 5439 |
// Bit-widths might be different in unary cases because we use it for SConvert/UConvert and friends. |
| 5440 |
expected_type.basetype = input_type; |
| 5441 |
expected_type.width = expr_type.width; |
| 5442 |
string cast_op = expr_type.basetype != input_type ? bitcast_glsl(expected_type, op0) : to_unpacked_expression(op0); |
| 5443 |
|
| 5444 |
string expr; |
| 5445 |
if (out_type.basetype != expected_result_type) |
| 5446 |
{ |
| 5447 |
expected_type.basetype = expected_result_type; |
| 5448 |
expected_type.width = out_type.width; |
| 5449 |
expr = bitcast_glsl_op(out_type, expected_type); |
| 5450 |
expr += '('; |
| 5451 |
expr += join(op, "(", cast_op, ")"); |
| 5452 |
expr += ')'; |
| 5453 |
} |
| 5454 |
else |
| 5455 |
{ |
| 5456 |
expr += join(op, "(", cast_op, ")"); |
| 5457 |
} |
| 5458 |
|
| 5459 |
emit_op(result_type, result_id, expr, should_forward(op0)); |
| 5460 |
inherit_expression_dependencies(result_id, op0); |
| 5461 |
} |
| 5462 |
|
| 5463 |
// Very special case. Handling bitfieldExtract requires us to deal with different bitcasts of different signs |
| 5464 |
// and different vector sizes all at once. Need a special purpose method here. |
| 5465 |
void CompilerGLSL::emit_trinary_func_op_bitextract(uint32_t result_type, uint32_t result_id, uint32_t op0, uint32_t op1, |
| 5466 |
uint32_t op2, const char *op, |
| 5467 |
SPIRType::BaseType expected_result_type, |
| 5468 |
SPIRType::BaseType input_type0, SPIRType::BaseType input_type1, |
| 5469 |
SPIRType::BaseType input_type2) |
| 5470 |
{ |
| 5471 |
auto &out_type = get<SPIRType>(result_type); |
| 5472 |
auto expected_type = out_type; |
| 5473 |
expected_type.basetype = input_type0; |
| 5474 |
|
| 5475 |
string cast_op0 = |
| 5476 |
expression_type(op0).basetype != input_type0 ? bitcast_glsl(expected_type, op0) : to_unpacked_expression(op0); |
| 5477 |
|
| 5478 |
auto op1_expr = to_unpacked_expression(op1); |
| 5479 |
auto op2_expr = to_unpacked_expression(op2); |
| 5480 |
|
| 5481 |
// Use value casts here instead. Input must be exactly int or uint, but SPIR-V might be 16-bit. |
| 5482 |
expected_type.basetype = input_type1; |
| 5483 |
expected_type.vecsize = 1; |
| 5484 |
string cast_op1 = expression_type(op1).basetype != input_type1 ? |
| 5485 |
join(type_to_glsl_constructor(expected_type), "(", op1_expr, ")") : |
| 5486 |
op1_expr; |
| 5487 |
|
| 5488 |
expected_type.basetype = input_type2; |
| 5489 |
expected_type.vecsize = 1; |
| 5490 |
string cast_op2 = expression_type(op2).basetype != input_type2 ? |
| 5491 |
join(type_to_glsl_constructor(expected_type), "(", op2_expr, ")") : |
| 5492 |
op2_expr; |
| 5493 |
|
| 5494 |
string expr; |
| 5495 |
if (out_type.basetype != expected_result_type) |
| 5496 |
{ |
| 5497 |
expected_type.vecsize = out_type.vecsize; |
| 5498 |
expected_type.basetype = expected_result_type; |
| 5499 |
expr = bitcast_glsl_op(out_type, expected_type); |
| 5500 |
expr += '('; |
| 5501 |
expr += join(op, "(", cast_op0, ", ", cast_op1, ", ", cast_op2, ")"); |
| 5502 |
expr += ')'; |
| 5503 |
} |
| 5504 |
else |
| 5505 |
{ |
| 5506 |
expr += join(op, "(", cast_op0, ", ", cast_op1, ", ", cast_op2, ")"); |
| 5507 |
} |
| 5508 |
|
| 5509 |
emit_op(result_type, result_id, expr, should_forward(op0) && should_forward(op1) && should_forward(op2)); |
| 5510 |
inherit_expression_dependencies(result_id, op0); |
| 5511 |
inherit_expression_dependencies(result_id, op1); |
| 5512 |
inherit_expression_dependencies(result_id, op2); |
| 5513 |
} |
| 5514 |
|
| 5515 |
void CompilerGLSL::emit_trinary_func_op_cast(uint32_t result_type, uint32_t result_id, uint32_t op0, uint32_t op1, |
| 5516 |
uint32_t op2, const char *op, SPIRType::BaseType input_type) |
| 5517 |
{ |
| 5518 |
auto &out_type = get<SPIRType>(result_type); |
| 5519 |
auto expected_type = out_type; |
| 5520 |
expected_type.basetype = input_type; |
| 5521 |
string cast_op0 = |
| 5522 |
expression_type(op0).basetype != input_type ? bitcast_glsl(expected_type, op0) : to_unpacked_expression(op0); |
| 5523 |
string cast_op1 = |
| 5524 |
expression_type(op1).basetype != input_type ? bitcast_glsl(expected_type, op1) : to_unpacked_expression(op1); |
| 5525 |
string cast_op2 = |
| 5526 |
expression_type(op2).basetype != input_type ? bitcast_glsl(expected_type, op2) : to_unpacked_expression(op2); |
| 5527 |
|
| 5528 |
string expr; |
| 5529 |
if (out_type.basetype != input_type) |
| 5530 |
{ |
| 5531 |
expr = bitcast_glsl_op(out_type, expected_type); |
| 5532 |
expr += '('; |
| 5533 |
expr += join(op, "(", cast_op0, ", ", cast_op1, ", ", cast_op2, ")"); |
| 5534 |
expr += ')'; |
| 5535 |
} |
| 5536 |
else |
| 5537 |
{ |
| 5538 |
expr += join(op, "(", cast_op0, ", ", cast_op1, ", ", cast_op2, ")"); |
| 5539 |
} |
| 5540 |
|
| 5541 |
emit_op(result_type, result_id, expr, should_forward(op0) && should_forward(op1) && should_forward(op2)); |
| 5542 |
inherit_expression_dependencies(result_id, op0); |
| 5543 |
inherit_expression_dependencies(result_id, op1); |
| 5544 |
inherit_expression_dependencies(result_id, op2); |
| 5545 |
} |
| 5546 |
|
| 5547 |
void CompilerGLSL::emit_binary_func_op_cast_clustered(uint32_t result_type, uint32_t result_id, uint32_t op0, |
| 5548 |
uint32_t op1, const char *op, SPIRType::BaseType input_type) |
| 5549 |
{ |
| 5550 |
// Special purpose method for implementing clustered subgroup opcodes. |
| 5551 |
// Main difference is that op1 does not participate in any casting, it needs to be a literal. |
| 5552 |
auto &out_type = get<SPIRType>(result_type); |
| 5553 |
auto expected_type = out_type; |
| 5554 |
expected_type.basetype = input_type; |
| 5555 |
string cast_op0 = |
| 5556 |
expression_type(op0).basetype != input_type ? bitcast_glsl(expected_type, op0) : to_unpacked_expression(op0); |
| 5557 |
|
| 5558 |
string expr; |
| 5559 |
if (out_type.basetype != input_type) |
| 5560 |
{ |
| 5561 |
expr = bitcast_glsl_op(out_type, expected_type); |
| 5562 |
expr += '('; |
| 5563 |
expr += join(op, "(", cast_op0, ", ", to_expression(op1), ")"); |
| 5564 |
expr += ')'; |
| 5565 |
} |
| 5566 |
else |
| 5567 |
{ |
| 5568 |
expr += join(op, "(", cast_op0, ", ", to_expression(op1), ")"); |
| 5569 |
} |
| 5570 |
|
| 5571 |
emit_op(result_type, result_id, expr, should_forward(op0)); |
| 5572 |
inherit_expression_dependencies(result_id, op0); |
| 5573 |
} |
| 5574 |
|
| 5575 |
void CompilerGLSL::emit_binary_func_op_cast(uint32_t result_type, uint32_t result_id, uint32_t op0, uint32_t op1, |
| 5576 |
const char *op, SPIRType::BaseType input_type, bool skip_cast_if_equal_type) |
| 5577 |
{ |
| 5578 |
string cast_op0, cast_op1; |
| 5579 |
auto expected_type = binary_op_bitcast_helper(cast_op0, cast_op1, input_type, op0, op1, skip_cast_if_equal_type); |
| 5580 |
auto &out_type = get<SPIRType>(result_type); |
| 5581 |
|
| 5582 |
// Special case boolean outputs since relational opcodes output booleans instead of int/uint. |
| 5583 |
string expr; |
| 5584 |
if (out_type.basetype != input_type && out_type.basetype != SPIRType::Boolean) |
| 5585 |
{ |
| 5586 |
expected_type.basetype = input_type; |
| 5587 |
expr = bitcast_glsl_op(out_type, expected_type); |
| 5588 |
expr += '('; |
| 5589 |
expr += join(op, "(", cast_op0, ", ", cast_op1, ")"); |
| 5590 |
expr += ')'; |
| 5591 |
} |
| 5592 |
else |
| 5593 |
{ |
| 5594 |
expr += join(op, "(", cast_op0, ", ", cast_op1, ")"); |
| 5595 |
} |
| 5596 |
|
| 5597 |
emit_op(result_type, result_id, expr, should_forward(op0) && should_forward(op1)); |
| 5598 |
inherit_expression_dependencies(result_id, op0); |
| 5599 |
inherit_expression_dependencies(result_id, op1); |
| 5600 |
} |
| 5601 |
|
| 5602 |
void CompilerGLSL::emit_trinary_func_op(uint32_t result_type, uint32_t result_id, uint32_t op0, uint32_t op1, |
| 5603 |
uint32_t op2, const char *op) |
| 5604 |
{ |
| 5605 |
bool forward = should_forward(op0) && should_forward(op1) && should_forward(op2); |
| 5606 |
emit_op(result_type, result_id, |
| 5607 |
join(op, "(", to_unpacked_expression(op0), ", ", to_unpacked_expression(op1), ", ", |
| 5608 |
to_unpacked_expression(op2), ")"), |
| 5609 |
forward); |
| 5610 |
|
| 5611 |
inherit_expression_dependencies(result_id, op0); |
| 5612 |
inherit_expression_dependencies(result_id, op1); |
| 5613 |
inherit_expression_dependencies(result_id, op2); |
| 5614 |
} |
| 5615 |
|
| 5616 |
void CompilerGLSL::emit_quaternary_func_op(uint32_t result_type, uint32_t result_id, uint32_t op0, uint32_t op1, |
| 5617 |
uint32_t op2, uint32_t op3, const char *op) |
| 5618 |
{ |
| 5619 |
bool forward = should_forward(op0) && should_forward(op1) && should_forward(op2) && should_forward(op3); |
| 5620 |
emit_op(result_type, result_id, |
| 5621 |
join(op, "(", to_unpacked_expression(op0), ", ", to_unpacked_expression(op1), ", ", |
| 5622 |
to_unpacked_expression(op2), ", ", to_unpacked_expression(op3), ")"), |
| 5623 |
forward); |
| 5624 |
|
| 5625 |
inherit_expression_dependencies(result_id, op0); |
| 5626 |
inherit_expression_dependencies(result_id, op1); |
| 5627 |
inherit_expression_dependencies(result_id, op2); |
| 5628 |
inherit_expression_dependencies(result_id, op3); |
| 5629 |
} |
| 5630 |
|
| 5631 |
void CompilerGLSL::emit_bitfield_insert_op(uint32_t result_type, uint32_t result_id, uint32_t op0, uint32_t op1, |
| 5632 |
uint32_t op2, uint32_t op3, const char *op, |
| 5633 |
SPIRType::BaseType offset_count_type) |
| 5634 |
{ |
| 5635 |
// Only need to cast offset/count arguments. Types of base/insert must be same as result type, |
| 5636 |
// and bitfieldInsert is sign invariant. |
| 5637 |
bool forward = should_forward(op0) && should_forward(op1) && should_forward(op2) && should_forward(op3); |
| 5638 |
|
| 5639 |
auto op0_expr = to_unpacked_expression(op0); |
| 5640 |
auto op1_expr = to_unpacked_expression(op1); |
| 5641 |
auto op2_expr = to_unpacked_expression(op2); |
| 5642 |
auto op3_expr = to_unpacked_expression(op3); |
| 5643 |
|
| 5644 |
SPIRType target_type; |
| 5645 |
target_type.vecsize = 1; |
| 5646 |
target_type.basetype = offset_count_type; |
| 5647 |
|
| 5648 |
if (expression_type(op2).basetype != offset_count_type) |
| 5649 |
{ |
| 5650 |
// Value-cast here. Input might be 16-bit. GLSL requires int. |
| 5651 |
op2_expr = join(type_to_glsl_constructor(target_type), "(", op2_expr, ")"); |
| 5652 |
} |
| 5653 |
|
| 5654 |
if (expression_type(op3).basetype != offset_count_type) |
| 5655 |
{ |
| 5656 |
// Value-cast here. Input might be 16-bit. GLSL requires int. |
| 5657 |
op3_expr = join(type_to_glsl_constructor(target_type), "(", op3_expr, ")"); |
| 5658 |
} |
| 5659 |
|
| 5660 |
emit_op(result_type, result_id, join(op, "(", op0_expr, ", ", op1_expr, ", ", op2_expr, ", ", op3_expr, ")"), |
| 5661 |
forward); |
| 5662 |
|
| 5663 |
inherit_expression_dependencies(result_id, op0); |
| 5664 |
inherit_expression_dependencies(result_id, op1); |
| 5665 |
inherit_expression_dependencies(result_id, op2); |
| 5666 |
inherit_expression_dependencies(result_id, op3); |
| 5667 |
} |
| 5668 |
|
| 5669 |
string CompilerGLSL::legacy_tex_op(const std::string &op, const SPIRType &imgtype, uint32_t tex) |
| 5670 |
{ |
| 5671 |
const char *type; |
| 5672 |
switch (imgtype.image.dim) |
| 5673 |
{ |
| 5674 |
case spv::Dim1D: |
| 5675 |
type = (imgtype.image.arrayed && !options.es) ? "1DArray" : "1D"; |
| 5676 |
break; |
| 5677 |
case spv::Dim2D: |
| 5678 |
type = (imgtype.image.arrayed && !options.es) ? "2DArray" : "2D"; |
| 5679 |
break; |
| 5680 |
case spv::Dim3D: |
| 5681 |
type = "3D"; |
| 5682 |
break; |
| 5683 |
case spv::DimCube: |
| 5684 |
type = "Cube"; |
| 5685 |
break; |
| 5686 |
case spv::DimRect: |
| 5687 |
type = "2DRect"; |
| 5688 |
break; |
| 5689 |
case spv::DimBuffer: |
| 5690 |
type = "Buffer"; |
| 5691 |
break; |
| 5692 |
case spv::DimSubpassData: |
| 5693 |
type = "2D"; |
| 5694 |
break; |
| 5695 |
default: |
| 5696 |
type = ""; |
| 5697 |
break; |
| 5698 |
} |
| 5699 |
|
| 5700 |
// In legacy GLSL, an extension is required for textureLod in the fragment |
| 5701 |
// shader or textureGrad anywhere. |
| 5702 |
bool legacy_lod_ext = false; |
| 5703 |
auto &execution = get_entry_point(); |
| 5704 |
if (op == "textureGrad" || op == "textureProjGrad" || |
| 5705 |
((op == "textureLod" || op == "textureProjLod") && execution.model != ExecutionModelVertex)) |
| 5706 |
{ |
| 5707 |
if (is_legacy_es()) |
| 5708 |
{ |
| 5709 |
legacy_lod_ext = true; |
| 5710 |
require_extension_internal("GL_EXT_shader_texture_lod"); |
| 5711 |
} |
| 5712 |
else if (is_legacy_desktop()) |
| 5713 |
require_extension_internal("GL_ARB_shader_texture_lod"); |
| 5714 |
} |
| 5715 |
|
| 5716 |
if (op == "textureLodOffset" || op == "textureProjLodOffset") |
| 5717 |
{ |
| 5718 |
if (is_legacy_es()) |
| 5719 |
SPIRV_CROSS_THROW(join(op, " not allowed in legacy ES")); |
| 5720 |
|
| 5721 |
require_extension_internal("GL_EXT_gpu_shader4"); |
| 5722 |
} |
| 5723 |
|
| 5724 |
// GLES has very limited support for shadow samplers. |
| 5725 |
// Basically shadow2D and shadow2DProj work through EXT_shadow_samplers, |
| 5726 |
// everything else can just throw |
| 5727 |
if (image_is_comparison(imgtype, tex) && is_legacy_es()) |
| 5728 |
{ |
| 5729 |
if (op == "texture" || op == "textureProj") |
| 5730 |
require_extension_internal("GL_EXT_shadow_samplers"); |
| 5731 |
else |
| 5732 |
SPIRV_CROSS_THROW(join(op, " not allowed on depth samplers in legacy ES")); |
| 5733 |
} |
| 5734 |
|
| 5735 |
bool is_es_and_depth = is_legacy_es() && image_is_comparison(imgtype, tex); |
| 5736 |
std::string type_prefix = image_is_comparison(imgtype, tex) ? "shadow" : "texture"; |
| 5737 |
|
| 5738 |
if (op == "texture") |
| 5739 |
return is_es_and_depth ? join(type_prefix, type, "EXT") : join(type_prefix, type); |
| 5740 |
else if (op == "textureLod") |
| 5741 |
return join(type_prefix, type, legacy_lod_ext ? "LodEXT" : "Lod"); |
| 5742 |
else if (op == "textureProj") |
| 5743 |
return join(type_prefix, type, is_es_and_depth ? "ProjEXT" : "Proj"); |
| 5744 |
else if (op == "textureGrad") |
| 5745 |
return join(type_prefix, type, is_legacy_es() ? "GradEXT" : is_legacy_desktop() ? "GradARB" : "Grad"); |
| 5746 |
else if (op == "textureProjLod") |
| 5747 |
return join(type_prefix, type, legacy_lod_ext ? "ProjLodEXT" : "ProjLod"); |
| 5748 |
else if (op == "textureLodOffset") |
| 5749 |
return join(type_prefix, type, "LodOffset"); |
| 5750 |
else if (op == "textureProjGrad") |
| 5751 |
return join(type_prefix, type, |
| 5752 |
is_legacy_es() ? "ProjGradEXT" : is_legacy_desktop() ? "ProjGradARB" : "ProjGrad"); |
| 5753 |
else if (op == "textureProjLodOffset") |
| 5754 |
return join(type_prefix, type, "ProjLodOffset"); |
| 5755 |
else |
| 5756 |
{ |
| 5757 |
SPIRV_CROSS_THROW(join("Unsupported legacy texture op: ", op)); |
| 5758 |
} |
| 5759 |
} |
| 5760 |
|
| 5761 |
bool CompilerGLSL::to_trivial_mix_op(const SPIRType &type, string &op, uint32_t left, uint32_t right, uint32_t lerp) |
| 5762 |
{ |
| 5763 |
auto *cleft = maybe_get<SPIRConstant>(left); |
| 5764 |
auto *cright = maybe_get<SPIRConstant>(right); |
| 5765 |
auto &lerptype = expression_type(lerp); |
| 5766 |
|
| 5767 |
// If our targets aren't constants, we cannot use construction. |
| 5768 |
if (!cleft || !cright) |
| 5769 |
return false; |
| 5770 |
|
| 5771 |
// If our targets are spec constants, we cannot use construction. |
| 5772 |
if (cleft->specialization || cright->specialization) |
| 5773 |
return false; |
| 5774 |
|
| 5775 |
// We can only use trivial construction if we have a scalar |
| 5776 |
// (should be possible to do it for vectors as well, but that is overkill for now). |
| 5777 |
if (lerptype.basetype != SPIRType::Boolean || lerptype.vecsize > 1) |
| 5778 |
return false; |
| 5779 |
|
| 5780 |
// If our bool selects between 0 and 1, we can cast from bool instead, making our trivial constructor. |
| 5781 |
bool ret = false; |
| 5782 |
switch (type.basetype) |
| 5783 |
{ |
| 5784 |
case SPIRType::Short: |
| 5785 |
case SPIRType::UShort: |
| 5786 |
ret = cleft->scalar_u16() == 0 && cright->scalar_u16() == 1; |
| 5787 |
break; |
| 5788 |
|
| 5789 |
case SPIRType::Int: |
| 5790 |
case SPIRType::UInt: |
| 5791 |
ret = cleft->scalar() == 0 && cright->scalar() == 1; |
| 5792 |
break; |
| 5793 |
|
| 5794 |
case SPIRType::Half: |
| 5795 |
ret = cleft->scalar_f16() == 0.0f && cright->scalar_f16() == 1.0f; |
| 5796 |
break; |
| 5797 |
|
| 5798 |
case SPIRType::Float: |
| 5799 |
ret = cleft->scalar_f32() == 0.0f && cright->scalar_f32() == 1.0f; |
| 5800 |
break; |
| 5801 |
|
| 5802 |
case SPIRType::Double: |
| 5803 |
ret = cleft->scalar_f64() == 0.0 && cright->scalar_f64() == 1.0; |
| 5804 |
break; |
| 5805 |
|
| 5806 |
case SPIRType::Int64: |
| 5807 |
case SPIRType::UInt64: |
| 5808 |
ret = cleft->scalar_u64() == 0 && cright->scalar_u64() == 1; |
| 5809 |
break; |
| 5810 |
|
| 5811 |
default: |
| 5812 |
break; |
| 5813 |
} |
| 5814 |
|
| 5815 |
if (ret) |
| 5816 |
op = type_to_glsl_constructor(type); |
| 5817 |
return ret; |
| 5818 |
} |
| 5819 |
|
| 5820 |
string CompilerGLSL::to_ternary_expression(const SPIRType &restype, uint32_t select, uint32_t true_value, |
| 5821 |
uint32_t false_value) |
| 5822 |
{ |
| 5823 |
string expr; |
| 5824 |
auto &lerptype = expression_type(select); |
| 5825 |
|
| 5826 |
if (lerptype.vecsize == 1) |
| 5827 |
expr = join(to_enclosed_expression(select), " ? ", to_enclosed_pointer_expression(true_value), " : ", |
| 5828 |
to_enclosed_pointer_expression(false_value)); |
| 5829 |
else |
| 5830 |
{ |
| 5831 |
auto swiz = [this](uint32_t expression, uint32_t i) { return to_extract_component_expression(expression, i); }; |
| 5832 |
|
| 5833 |
expr = type_to_glsl_constructor(restype); |
| 5834 |
expr += "("; |
| 5835 |
for (uint32_t i = 0; i < restype.vecsize; i++) |
| 5836 |
{ |
| 5837 |
expr += swiz(select, i); |
| 5838 |
expr += " ? "; |
| 5839 |
expr += swiz(true_value, i); |
| 5840 |
expr += " : "; |
| 5841 |
expr += swiz(false_value, i); |
| 5842 |
if (i + 1 < restype.vecsize) |
| 5843 |
expr += ", "; |
| 5844 |
} |
| 5845 |
expr += ")"; |
| 5846 |
} |
| 5847 |
|
| 5848 |
return expr; |
| 5849 |
} |
| 5850 |
|
| 5851 |
void CompilerGLSL::emit_mix_op(uint32_t result_type, uint32_t id, uint32_t left, uint32_t right, uint32_t lerp) |
| 5852 |
{ |
| 5853 |
auto &lerptype = expression_type(lerp); |
| 5854 |
auto &restype = get<SPIRType>(result_type); |
| 5855 |
|
| 5856 |
// If this results in a variable pointer, assume it may be written through. |
| 5857 |
if (restype.pointer) |
| 5858 |
{ |
| 5859 |
register_write(left); |
| 5860 |
register_write(right); |
| 5861 |
} |
| 5862 |
|
| 5863 |
string mix_op; |
| 5864 |
bool has_boolean_mix = *backend.boolean_mix_function && |
| 5865 |
((options.es && options.version >= 310) || (!options.es && options.version >= 450)); |
| 5866 |
bool trivial_mix = to_trivial_mix_op(restype, mix_op, left, right, lerp); |
| 5867 |
|
| 5868 |
// Cannot use boolean mix when the lerp argument is just one boolean, |
| 5869 |
// fall back to regular trinary statements. |
| 5870 |
if (lerptype.vecsize == 1) |
| 5871 |
has_boolean_mix = false; |
| 5872 |
|
| 5873 |
// If we can reduce the mix to a simple cast, do so. |
| 5874 |
// This helps for cases like int(bool), uint(bool) which is implemented with |
| 5875 |
// OpSelect bool 1 0. |
| 5876 |
if (trivial_mix) |
| 5877 |
{ |
| 5878 |
emit_unary_func_op(result_type, id, lerp, mix_op.c_str()); |
| 5879 |
} |
| 5880 |
else if (!has_boolean_mix && lerptype.basetype == SPIRType::Boolean) |
| 5881 |
{ |
| 5882 |
// Boolean mix not supported on desktop without extension. |
| 5883 |
// Was added in OpenGL 4.5 with ES 3.1 compat. |
| 5884 |
// |
| 5885 |
// Could use GL_EXT_shader_integer_mix on desktop at least, |
| 5886 |
// but Apple doesn't support it. :( |
| 5887 |
// Just implement it as ternary expressions. |
| 5888 |
auto expr = to_ternary_expression(get<SPIRType>(result_type), lerp, right, left); |
| 5889 |
emit_op(result_type, id, expr, should_forward(left) && should_forward(right) && should_forward(lerp)); |
| 5890 |
inherit_expression_dependencies(id, left); |
| 5891 |
inherit_expression_dependencies(id, right); |
| 5892 |
inherit_expression_dependencies(id, lerp); |
| 5893 |
} |
| 5894 |
else if (lerptype.basetype == SPIRType::Boolean) |
| 5895 |
emit_trinary_func_op(result_type, id, left, right, lerp, backend.boolean_mix_function); |
| 5896 |
else |
| 5897 |
emit_trinary_func_op(result_type, id, left, right, lerp, "mix"); |
| 5898 |
} |
| 5899 |
|
| 5900 |
string CompilerGLSL::to_combined_image_sampler(VariableID image_id, VariableID samp_id) |
| 5901 |
{ |
| 5902 |
// Keep track of the array indices we have used to load the image. |
| 5903 |
// We'll need to use the same array index into the combined image sampler array. |
| 5904 |
auto image_expr = to_expression(image_id); |
| 5905 |
string array_expr; |
| 5906 |
auto array_index = image_expr.find_first_of('['); |
| 5907 |
if (array_index != string::npos) |
| 5908 |
array_expr = image_expr.substr(array_index, string::npos); |
| 5909 |
|
| 5910 |
auto &args = current_function->arguments; |
| 5911 |
|
| 5912 |
// For GLSL and ESSL targets, we must enumerate all possible combinations for sampler2D(texture2D, sampler) and redirect |
| 5913 |
// all possible combinations into new sampler2D uniforms. |
| 5914 |
auto *image = maybe_get_backing_variable(image_id); |
| 5915 |
auto *samp = maybe_get_backing_variable(samp_id); |
| 5916 |
if (image) |
| 5917 |
image_id = image->self; |
| 5918 |
if (samp) |
| 5919 |
samp_id = samp->self; |
| 5920 |
|
| 5921 |
auto image_itr = find_if(begin(args), end(args), |
| 5922 |
[image_id](const SPIRFunction::Parameter ¶m) { return image_id == param.id; }); |
| 5923 |
|
| 5924 |
auto sampler_itr = find_if(begin(args), end(args), |
| 5925 |
[samp_id](const SPIRFunction::Parameter ¶m) { return samp_id == param.id; }); |
| 5926 |
|
| 5927 |
if (image_itr != end(args) || sampler_itr != end(args)) |
| 5928 |
{ |
| 5929 |
// If any parameter originates from a parameter, we will find it in our argument list. |
| 5930 |
bool global_image = image_itr == end(args); |
| 5931 |
bool global_sampler = sampler_itr == end(args); |
| 5932 |
VariableID iid = global_image ? image_id : VariableID(uint32_t(image_itr - begin(args))); |
| 5933 |
VariableID sid = global_sampler ? samp_id : VariableID(uint32_t(sampler_itr - begin(args))); |
| 5934 |
|
| 5935 |
auto &combined = current_function->combined_parameters; |
| 5936 |
auto itr = find_if(begin(combined), end(combined), [=](const SPIRFunction::CombinedImageSamplerParameter &p) { |
| 5937 |
return p.global_image == global_image && p.global_sampler == global_sampler && p.image_id == iid && |
| 5938 |
p.sampler_id == sid; |
| 5939 |
}); |
| 5940 |
|
| 5941 |
if (itr != end(combined)) |
| 5942 |
return to_expression(itr->id) + array_expr; |
| 5943 |
else |
| 5944 |
{ |
| 5945 |
SPIRV_CROSS_THROW("Cannot find mapping for combined sampler parameter, was " |
| 5946 |
"build_combined_image_samplers() used " |
| 5947 |
"before compile() was called?"); |
| 5948 |
} |
| 5949 |
} |
| 5950 |
else |
| 5951 |
{ |
| 5952 |
// For global sampler2D, look directly at the global remapping table. |
| 5953 |
auto &mapping = combined_image_samplers; |
| 5954 |
auto itr = find_if(begin(mapping), end(mapping), [image_id, samp_id](const CombinedImageSampler &combined) { |
| 5955 |
return combined.image_id == image_id && combined.sampler_id == samp_id; |
| 5956 |
}); |
| 5957 |
|
| 5958 |
if (itr != end(combined_image_samplers)) |
| 5959 |
return to_expression(itr->combined_id) + array_expr; |
| 5960 |
else |
| 5961 |
{ |
| 5962 |
SPIRV_CROSS_THROW("Cannot find mapping for combined sampler, was build_combined_image_samplers() used " |
| 5963 |
"before compile() was called?"); |
| 5964 |
} |
| 5965 |
} |
| 5966 |
} |
| 5967 |
|
| 5968 |
bool CompilerGLSL::is_supported_subgroup_op_in_opengl(spv::Op op) |
| 5969 |
{ |
| 5970 |
switch (op) |
| 5971 |
{ |
| 5972 |
case OpGroupNonUniformElect: |
| 5973 |
case OpGroupNonUniformBallot: |
| 5974 |
case OpGroupNonUniformBallotFindLSB: |
| 5975 |
case OpGroupNonUniformBallotFindMSB: |
| 5976 |
case OpGroupNonUniformBroadcast: |
| 5977 |
case OpGroupNonUniformBroadcastFirst: |
| 5978 |
case OpGroupNonUniformAll: |
| 5979 |
case OpGroupNonUniformAny: |
| 5980 |
case OpGroupNonUniformAllEqual: |
| 5981 |
case OpControlBarrier: |
| 5982 |
case OpMemoryBarrier: |
| 5983 |
case OpGroupNonUniformBallotBitCount: |
| 5984 |
case OpGroupNonUniformBallotBitExtract: |
| 5985 |
case OpGroupNonUniformInverseBallot: |
| 5986 |
return true; |
| 5987 |
default: |
| 5988 |
return false; |
| 5989 |
} |
| 5990 |
} |
| 5991 |
|
| 5992 |
void CompilerGLSL::emit_sampled_image_op(uint32_t result_type, uint32_t result_id, uint32_t image_id, uint32_t samp_id) |
| 5993 |
{ |
| 5994 |
if (options.vulkan_semantics && combined_image_samplers.empty()) |
| 5995 |
{ |
| 5996 |
emit_binary_func_op(result_type, result_id, image_id, samp_id, |
| 5997 |
type_to_glsl(get<SPIRType>(result_type), result_id).c_str()); |
| 5998 |
} |
| 5999 |
else |
| 6000 |
{ |
| 6001 |
// Make sure to suppress usage tracking. It is illegal to create temporaries of opaque types. |
| 6002 |
emit_op(result_type, result_id, to_combined_image_sampler(image_id, samp_id), true, true); |
| 6003 |
} |
| 6004 |
|
| 6005 |
// Make sure to suppress usage tracking and any expression invalidation. |
| 6006 |
// It is illegal to create temporaries of opaque types. |
| 6007 |
forwarded_temporaries.erase(result_id); |
| 6008 |
} |
| 6009 |
|
| 6010 |
static inline bool image_opcode_is_sample_no_dref(Op op) |
| 6011 |
{ |
| 6012 |
switch (op) |
| 6013 |
{ |
| 6014 |
case OpImageSampleExplicitLod: |
| 6015 |
case OpImageSampleImplicitLod: |
| 6016 |
case OpImageSampleProjExplicitLod: |
| 6017 |
case OpImageSampleProjImplicitLod: |
| 6018 |
case OpImageFetch: |
| 6019 |
case OpImageRead: |
| 6020 |
case OpImageSparseSampleExplicitLod: |
| 6021 |
case OpImageSparseSampleImplicitLod: |
| 6022 |
case OpImageSparseSampleProjExplicitLod: |
| 6023 |
case OpImageSparseSampleProjImplicitLod: |
| 6024 |
case OpImageSparseFetch: |
| 6025 |
case OpImageSparseRead: |
| 6026 |
return true; |
| 6027 |
|
| 6028 |
default: |
| 6029 |
return false; |
| 6030 |
} |
| 6031 |
} |
| 6032 |
|
| 6033 |
void CompilerGLSL::emit_sparse_feedback_temporaries(uint32_t result_type_id, uint32_t id, uint32_t &feedback_id, |
| 6034 |
uint32_t &texel_id) |
| 6035 |
{ |
| 6036 |
// Need to allocate two temporaries. |
| 6037 |
if (options.es) |
| 6038 |
SPIRV_CROSS_THROW("Sparse texture feedback is not supported on ESSL."); |
| 6039 |
require_extension_internal("GL_ARB_sparse_texture2"); |
| 6040 |
|
| 6041 |
auto &temps = extra_sub_expressions[id]; |
| 6042 |
if (temps == 0) |
| 6043 |
temps = ir.increase_bound_by(2); |
| 6044 |
|
| 6045 |
feedback_id = temps + 0; |
| 6046 |
texel_id = temps + 1; |
| 6047 |
|
| 6048 |
auto &return_type = get<SPIRType>(result_type_id); |
| 6049 |
if (return_type.basetype != SPIRType::Struct || return_type.member_types.size() != 2) |
| 6050 |
SPIRV_CROSS_THROW("Invalid return type for sparse feedback."); |
| 6051 |
emit_uninitialized_temporary(return_type.member_types[0], feedback_id); |
| 6052 |
emit_uninitialized_temporary(return_type.member_types[1], texel_id); |
| 6053 |
} |
| 6054 |
|
| 6055 |
uint32_t CompilerGLSL::get_sparse_feedback_texel_id(uint32_t id) const |
| 6056 |
{ |
| 6057 |
auto itr = extra_sub_expressions.find(id); |
| 6058 |
if (itr == extra_sub_expressions.end()) |
| 6059 |
return 0; |
| 6060 |
else |
| 6061 |
return itr->second + 1; |
| 6062 |
} |
| 6063 |
|
| 6064 |
void CompilerGLSL::emit_texture_op(const Instruction &i, bool sparse) |
| 6065 |
{ |
| 6066 |
auto *ops = stream(i); |
| 6067 |
auto op = static_cast<Op>(i.op); |
| 6068 |
|
| 6069 |
SmallVector<uint32_t> inherited_expressions; |
| 6070 |
|
| 6071 |
uint32_t result_type_id = ops[0]; |
| 6072 |
uint32_t id = ops[1]; |
| 6073 |
auto &return_type = get<SPIRType>(result_type_id); |
| 6074 |
|
| 6075 |
uint32_t sparse_code_id = 0; |
| 6076 |
uint32_t sparse_texel_id = 0; |
| 6077 |
if (sparse) |
| 6078 |
emit_sparse_feedback_temporaries(result_type_id, id, sparse_code_id, sparse_texel_id); |
| 6079 |
|
| 6080 |
bool forward = false; |
| 6081 |
string expr = to_texture_op(i, sparse, &forward, inherited_expressions); |
| 6082 |
|
| 6083 |
if (sparse) |
| 6084 |
{ |
| 6085 |
statement(to_expression(sparse_code_id), " = ", expr, ";"); |
| 6086 |
expr = join(type_to_glsl(return_type), "(", to_expression(sparse_code_id), ", ", to_expression(sparse_texel_id), |
| 6087 |
")"); |
| 6088 |
forward = true; |
| 6089 |
inherited_expressions.clear(); |
| 6090 |
} |
| 6091 |
|
| 6092 |
emit_op(result_type_id, id, expr, forward); |
| 6093 |
for (auto &inherit : inherited_expressions) |
| 6094 |
inherit_expression_dependencies(id, inherit); |
| 6095 |
|
| 6096 |
// Do not register sparse ops as control dependent as they are always lowered to a temporary. |
| 6097 |
switch (op) |
| 6098 |
{ |
| 6099 |
case OpImageSampleDrefImplicitLod: |
| 6100 |
case OpImageSampleImplicitLod: |
| 6101 |
case OpImageSampleProjImplicitLod: |
| 6102 |
case OpImageSampleProjDrefImplicitLod: |
| 6103 |
register_control_dependent_expression(id); |
| 6104 |
break; |
| 6105 |
|
| 6106 |
default: |
| 6107 |
break; |
| 6108 |
} |
| 6109 |
} |
| 6110 |
|
| 6111 |
std::string CompilerGLSL::to_texture_op(const Instruction &i, bool sparse, bool *forward, |
| 6112 |
SmallVector<uint32_t> &inherited_expressions) |
| 6113 |
{ |
| 6114 |
auto *ops = stream(i); |
| 6115 |
auto op = static_cast<Op>(i.op); |
| 6116 |
uint32_t length = i.length; |
| 6117 |
|
| 6118 |
uint32_t result_type_id = ops[0]; |
| 6119 |
VariableID img = ops[2]; |
| 6120 |
uint32_t coord = ops[3]; |
| 6121 |
uint32_t dref = 0; |
| 6122 |
uint32_t comp = 0; |
| 6123 |
bool gather = false; |
| 6124 |
bool proj = false; |
| 6125 |
bool fetch = false; |
| 6126 |
const uint32_t *opt = nullptr; |
| 6127 |
|
| 6128 |
auto &result_type = get<SPIRType>(result_type_id); |
| 6129 |
|
| 6130 |
inherited_expressions.push_back(coord); |
| 6131 |
|
| 6132 |
// Make sure non-uniform decoration is back-propagated to where it needs to be. |
| 6133 |
if (has_decoration(img, DecorationNonUniformEXT)) |
| 6134 |
propagate_nonuniform_qualifier(img); |
| 6135 |
|
| 6136 |
switch (op) |
| 6137 |
{ |
| 6138 |
case OpImageSampleDrefImplicitLod: |
| 6139 |
case OpImageSampleDrefExplicitLod: |
| 6140 |
case OpImageSparseSampleDrefImplicitLod: |
| 6141 |
case OpImageSparseSampleDrefExplicitLod: |
| 6142 |
dref = ops[4]; |
| 6143 |
opt = &ops[5]; |
| 6144 |
length -= 5; |
| 6145 |
break; |
| 6146 |
|
| 6147 |
case OpImageSampleProjDrefImplicitLod: |
| 6148 |
case OpImageSampleProjDrefExplicitLod: |
| 6149 |
case OpImageSparseSampleProjDrefImplicitLod: |
| 6150 |
case OpImageSparseSampleProjDrefExplicitLod: |
| 6151 |
dref = ops[4]; |
| 6152 |
opt = &ops[5]; |
| 6153 |
length -= 5; |
| 6154 |
proj = true; |
| 6155 |
break; |
| 6156 |
|
| 6157 |
case OpImageDrefGather: |
| 6158 |
case OpImageSparseDrefGather: |
| 6159 |
dref = ops[4]; |
| 6160 |
opt = &ops[5]; |
| 6161 |
length -= 5; |
| 6162 |
gather = true; |
| 6163 |
if (options.es && options.version < 310) |
| 6164 |
SPIRV_CROSS_THROW("textureGather requires ESSL 310."); |
| 6165 |
else if (!options.es && options.version < 400) |
| 6166 |
SPIRV_CROSS_THROW("textureGather with depth compare requires GLSL 400."); |
| 6167 |
break; |
| 6168 |
|
| 6169 |
case OpImageGather: |
| 6170 |
case OpImageSparseGather: |
| 6171 |
comp = ops[4]; |
| 6172 |
opt = &ops[5]; |
| 6173 |
length -= 5; |
| 6174 |
gather = true; |
| 6175 |
if (options.es && options.version < 310) |
| 6176 |
SPIRV_CROSS_THROW("textureGather requires ESSL 310."); |
| 6177 |
else if (!options.es && options.version < 400) |
| 6178 |
{ |
| 6179 |
if (!expression_is_constant_null(comp)) |
| 6180 |
SPIRV_CROSS_THROW("textureGather with component requires GLSL 400."); |
| 6181 |
require_extension_internal("GL_ARB_texture_gather"); |
| 6182 |
} |
| 6183 |
break; |
| 6184 |
|
| 6185 |
case OpImageFetch: |
| 6186 |
case OpImageSparseFetch: |
| 6187 |
case OpImageRead: // Reads == fetches in Metal (other langs will not get here) |
| 6188 |
opt = &ops[4]; |
| 6189 |
length -= 4; |
| 6190 |
fetch = true; |
| 6191 |
break; |
| 6192 |
|
| 6193 |
case OpImageSampleProjImplicitLod: |
| 6194 |
case OpImageSampleProjExplicitLod: |
| 6195 |
case OpImageSparseSampleProjImplicitLod: |
| 6196 |
case OpImageSparseSampleProjExplicitLod: |
| 6197 |
opt = &ops[4]; |
| 6198 |
length -= 4; |
| 6199 |
proj = true; |
| 6200 |
break; |
| 6201 |
|
| 6202 |
default: |
| 6203 |
opt = &ops[4]; |
| 6204 |
length -= 4; |
| 6205 |
break; |
| 6206 |
} |
| 6207 |
|
| 6208 |
// Bypass pointers because we need the real image struct |
| 6209 |
auto &type = expression_type(img); |
| 6210 |
auto &imgtype = get<SPIRType>(type.self); |
| 6211 |
|
| 6212 |
uint32_t coord_components = 0; |
| 6213 |
switch (imgtype.image.dim) |
| 6214 |
{ |
| 6215 |
case spv::Dim1D: |
| 6216 |
coord_components = 1; |
| 6217 |
break; |
| 6218 |
case spv::Dim2D: |
| 6219 |
coord_components = 2; |
| 6220 |
break; |
| 6221 |
case spv::Dim3D: |
| 6222 |
coord_components = 3; |
| 6223 |
break; |
| 6224 |
case spv::DimCube: |
| 6225 |
coord_components = 3; |
| 6226 |
break; |
| 6227 |
case spv::DimBuffer: |
| 6228 |
coord_components = 1; |
| 6229 |
break; |
| 6230 |
default: |
| 6231 |
coord_components = 2; |
| 6232 |
break; |
| 6233 |
} |
| 6234 |
|
| 6235 |
if (dref) |
| 6236 |
inherited_expressions.push_back(dref); |
| 6237 |
|
| 6238 |
if (proj) |
| 6239 |
coord_components++; |
| 6240 |
if (imgtype.image.arrayed) |
| 6241 |
coord_components++; |
| 6242 |
|
| 6243 |
uint32_t bias = 0; |
| 6244 |
uint32_t lod = 0; |
| 6245 |
uint32_t grad_x = 0; |
| 6246 |
uint32_t grad_y = 0; |
| 6247 |
uint32_t coffset = 0; |
| 6248 |
uint32_t offset = 0; |
| 6249 |
uint32_t coffsets = 0; |
| 6250 |
uint32_t sample = 0; |
| 6251 |
uint32_t minlod = 0; |
| 6252 |
uint32_t flags = 0; |
| 6253 |
|
| 6254 |
if (length) |
| 6255 |
{ |
| 6256 |
flags = *opt++; |
| 6257 |
length--; |
| 6258 |
} |
| 6259 |
|
| 6260 |
auto test = [&](uint32_t &v, uint32_t flag) { |
| 6261 |
if (length && (flags & flag)) |
| 6262 |
{ |
| 6263 |
v = *opt++; |
| 6264 |
inherited_expressions.push_back(v); |
| 6265 |
length--; |
| 6266 |
} |
| 6267 |
}; |
| 6268 |
|
| 6269 |
test(bias, ImageOperandsBiasMask); |
| 6270 |
test(lod, ImageOperandsLodMask); |
| 6271 |
test(grad_x, ImageOperandsGradMask); |
| 6272 |
test(grad_y, ImageOperandsGradMask); |
| 6273 |
test(coffset, ImageOperandsConstOffsetMask); |
| 6274 |
test(offset, ImageOperandsOffsetMask); |
| 6275 |
test(coffsets, ImageOperandsConstOffsetsMask); |
| 6276 |
test(sample, ImageOperandsSampleMask); |
| 6277 |
test(minlod, ImageOperandsMinLodMask); |
| 6278 |
|
| 6279 |
TextureFunctionBaseArguments base_args = {}; |
| 6280 |
base_args.img = img; |
| 6281 |
base_args.imgtype = &imgtype; |
| 6282 |
base_args.is_fetch = fetch != 0; |
| 6283 |
base_args.is_gather = gather != 0; |
| 6284 |
base_args.is_proj = proj != 0; |
| 6285 |
|
| 6286 |
string expr; |
| 6287 |
TextureFunctionNameArguments name_args = {}; |
| 6288 |
|
| 6289 |
name_args.base = base_args; |
| 6290 |
name_args.has_array_offsets = coffsets != 0; |
| 6291 |
name_args.has_offset = coffset != 0 || offset != 0; |
| 6292 |
name_args.has_grad = grad_x != 0 || grad_y != 0; |
| 6293 |
name_args.has_dref = dref != 0; |
| 6294 |
name_args.is_sparse_feedback = sparse; |
| 6295 |
name_args.has_min_lod = minlod != 0; |
| 6296 |
name_args.lod = lod; |
| 6297 |
expr += to_function_name(name_args); |
| 6298 |
expr += "("; |
| 6299 |
|
| 6300 |
uint32_t sparse_texel_id = 0; |
| 6301 |
if (sparse) |
| 6302 |
sparse_texel_id = get_sparse_feedback_texel_id(ops[1]); |
| 6303 |
|
| 6304 |
TextureFunctionArguments args = {}; |
| 6305 |
args.base = base_args; |
| 6306 |
args.coord = coord; |
| 6307 |
args.coord_components = coord_components; |
| 6308 |
args.dref = dref; |
| 6309 |
args.grad_x = grad_x; |
| 6310 |
args.grad_y = grad_y; |
| 6311 |
args.lod = lod; |
| 6312 |
args.coffset = coffset; |
| 6313 |
args.offset = offset; |
| 6314 |
args.bias = bias; |
| 6315 |
args.component = comp; |
| 6316 |
args.sample = sample; |
| 6317 |
args.sparse_texel = sparse_texel_id; |
| 6318 |
args.min_lod = minlod; |
| 6319 |
expr += to_function_args(args, forward); |
| 6320 |
expr += ")"; |
| 6321 |
|
| 6322 |
// texture(samplerXShadow) returns float. shadowX() returns vec4. Swizzle here. |
| 6323 |
if (is_legacy() && image_is_comparison(imgtype, img)) |
| 6324 |
expr += ".r"; |
| 6325 |
|
| 6326 |
// Sampling from a texture which was deduced to be a depth image, might actually return 1 component here. |
| 6327 |
// Remap back to 4 components as sampling opcodes expect. |
| 6328 |
if (backend.comparison_image_samples_scalar && image_opcode_is_sample_no_dref(op)) |
| 6329 |
{ |
| 6330 |
bool image_is_depth = false; |
| 6331 |
const auto *combined = maybe_get<SPIRCombinedImageSampler>(img); |
| 6332 |
VariableID image_id = combined ? combined->image : img; |
| 6333 |
|
| 6334 |
if (combined && image_is_comparison(imgtype, combined->image)) |
| 6335 |
image_is_depth = true; |
| 6336 |
else if (image_is_comparison(imgtype, img)) |
| 6337 |
image_is_depth = true; |
| 6338 |
|
| 6339 |
// We must also check the backing variable for the image. |
| 6340 |
// We might have loaded an OpImage, and used that handle for two different purposes. |
| 6341 |
// Once with comparison, once without. |
| 6342 |
auto *image_variable = maybe_get_backing_variable(image_id); |
| 6343 |
if (image_variable && image_is_comparison(get<SPIRType>(image_variable->basetype), image_variable->self)) |
| 6344 |
image_is_depth = true; |
| 6345 |
|
| 6346 |
if (image_is_depth) |
| 6347 |
expr = remap_swizzle(result_type, 1, expr); |
| 6348 |
} |
| 6349 |
|
| 6350 |
if (!sparse && !backend.support_small_type_sampling_result && result_type.width < 32) |
| 6351 |
{ |
| 6352 |
// Just value cast (narrowing) to expected type since we cannot rely on narrowing to work automatically. |
| 6353 |
// Hopefully compiler picks this up and converts the texturing instruction to the appropriate precision. |
| 6354 |
expr = join(type_to_glsl_constructor(result_type), "(", expr, ")"); |
| 6355 |
} |
| 6356 |
|
| 6357 |
// Deals with reads from MSL. We might need to downconvert to fewer components. |
| 6358 |
if (op == OpImageRead) |
| 6359 |
expr = remap_swizzle(result_type, 4, expr); |
| 6360 |
|
| 6361 |
return expr; |
| 6362 |
} |
| 6363 |
|
| 6364 |
bool CompilerGLSL::expression_is_constant_null(uint32_t id) const |
| 6365 |
{ |
| 6366 |
auto *c = maybe_get<SPIRConstant>(id); |
| 6367 |
if (!c) |
| 6368 |
return false; |
| 6369 |
return c->constant_is_null(); |
| 6370 |
} |
| 6371 |
|
| 6372 |
bool CompilerGLSL::expression_is_non_value_type_array(uint32_t ptr) |
| 6373 |
{ |
| 6374 |
auto &type = expression_type(ptr); |
| 6375 |
if (type.array.empty()) |
| 6376 |
return false; |
| 6377 |
|
| 6378 |
if (!backend.array_is_value_type) |
| 6379 |
return true; |
| 6380 |
|
| 6381 |
auto *var = maybe_get_backing_variable(ptr); |
| 6382 |
if (!var) |
| 6383 |
return false; |
| 6384 |
|
| 6385 |
auto &backed_type = get<SPIRType>(var->basetype); |
| 6386 |
return !backend.buffer_offset_array_is_value_type && backed_type.basetype == SPIRType::Struct && |
| 6387 |
has_member_decoration(backed_type.self, 0, DecorationOffset); |
| 6388 |
} |
| 6389 |
|
| 6390 |
// Returns the function name for a texture sampling function for the specified image and sampling characteristics. |
| 6391 |
// For some subclasses, the function is a method on the specified image. |
| 6392 |
string CompilerGLSL::to_function_name(const TextureFunctionNameArguments &args) |
| 6393 |
{ |
| 6394 |
if (args.has_min_lod) |
| 6395 |
{ |
| 6396 |
if (options.es) |
| 6397 |
SPIRV_CROSS_THROW("Sparse residency is not supported in ESSL."); |
| 6398 |
require_extension_internal("GL_ARB_sparse_texture_clamp"); |
| 6399 |
} |
| 6400 |
|
| 6401 |
string fname; |
| 6402 |
auto &imgtype = *args.base.imgtype; |
| 6403 |
VariableID tex = args.base.img; |
| 6404 |
|
| 6405 |
// textureLod on sampler2DArrayShadow and samplerCubeShadow does not exist in GLSL for some reason. |
| 6406 |
// To emulate this, we will have to use textureGrad with a constant gradient of 0. |
| 6407 |
// The workaround will assert that the LOD is in fact constant 0, or we cannot emit correct code. |
| 6408 |
// This happens for HLSL SampleCmpLevelZero on Texture2DArray and TextureCube. |
| 6409 |
bool workaround_lod_array_shadow_as_grad = false; |
| 6410 |
if (((imgtype.image.arrayed && imgtype.image.dim == Dim2D) || imgtype.image.dim == DimCube) && |
| 6411 |
image_is_comparison(imgtype, tex) && args.lod) |
| 6412 |
{ |
| 6413 |
if (!expression_is_constant_null(args.lod)) |
| 6414 |
{ |
| 6415 |
SPIRV_CROSS_THROW("textureLod on sampler2DArrayShadow is not constant 0.0. This cannot be " |
| 6416 |
"expressed in GLSL."); |
| 6417 |
} |
| 6418 |
workaround_lod_array_shadow_as_grad = true; |
| 6419 |
} |
| 6420 |
|
| 6421 |
if (args.is_sparse_feedback) |
| 6422 |
fname += "sparse"; |
| 6423 |
|
| 6424 |
if (args.base.is_fetch) |
| 6425 |
fname += args.is_sparse_feedback ? "TexelFetch" : "texelFetch"; |
| 6426 |
else |
| 6427 |
{ |
| 6428 |
fname += args.is_sparse_feedback ? "Texture" : "texture"; |
| 6429 |
|
| 6430 |
if (args.base.is_gather) |
| 6431 |
fname += "Gather"; |
| 6432 |
if (args.has_array_offsets) |
| 6433 |
fname += "Offsets"; |
| 6434 |
if (args.base.is_proj) |
| 6435 |
fname += "Proj"; |
| 6436 |
if (args.has_grad || workaround_lod_array_shadow_as_grad) |
| 6437 |
fname += "Grad"; |
| 6438 |
if (args.lod != 0 && !workaround_lod_array_shadow_as_grad) |
| 6439 |
fname += "Lod"; |
| 6440 |
} |
| 6441 |
|
| 6442 |
if (args.has_offset) |
| 6443 |
fname += "Offset"; |
| 6444 |
|
| 6445 |
if (args.has_min_lod) |
| 6446 |
fname += "Clamp"; |
| 6447 |
|
| 6448 |
if (args.is_sparse_feedback || args.has_min_lod) |
| 6449 |
fname += "ARB"; |
| 6450 |
|
| 6451 |
return (is_legacy() && !args.base.is_gather) ? legacy_tex_op(fname, imgtype, tex) : fname; |
| 6452 |
} |
| 6453 |
|
| 6454 |
std::string CompilerGLSL::convert_separate_image_to_expression(uint32_t id) |
| 6455 |
{ |
| 6456 |
auto *var = maybe_get_backing_variable(id); |
| 6457 |
|
| 6458 |
// If we are fetching from a plain OpTypeImage, we must combine with a dummy sampler in GLSL. |
| 6459 |
// In Vulkan GLSL, we can make use of the newer GL_EXT_samplerless_texture_functions. |
| 6460 |
if (var) |
| 6461 |
{ |
| 6462 |
auto &type = get<SPIRType>(var->basetype); |
| 6463 |
if (type.basetype == SPIRType::Image && type.image.sampled == 1 && type.image.dim != DimBuffer) |
| 6464 |
{ |
| 6465 |
if (options.vulkan_semantics) |
| 6466 |
{ |
| 6467 |
if (dummy_sampler_id) |
| 6468 |
{ |
| 6469 |
// Don't need to consider Shadow state since the dummy sampler is always non-shadow. |
| 6470 |
auto sampled_type = type; |
| 6471 |
sampled_type.basetype = SPIRType::SampledImage; |
| 6472 |
return join(type_to_glsl(sampled_type), "(", to_expression(id), ", ", |
| 6473 |
to_expression(dummy_sampler_id), ")"); |
| 6474 |
} |
| 6475 |
else |
| 6476 |
{ |
| 6477 |
// Newer glslang supports this extension to deal with texture2D as argument to texture functions. |
| 6478 |
require_extension_internal("GL_EXT_samplerless_texture_functions"); |
| 6479 |
} |
| 6480 |
} |
| 6481 |
else |
| 6482 |
{ |
| 6483 |
if (!dummy_sampler_id) |
| 6484 |
SPIRV_CROSS_THROW("Cannot find dummy sampler ID. Was " |
| 6485 |
"build_dummy_sampler_for_combined_images() called?"); |
| 6486 |
|
| 6487 |
return to_combined_image_sampler(id, dummy_sampler_id); |
| 6488 |
} |
| 6489 |
} |
| 6490 |
} |
| 6491 |
|
| 6492 |
return to_expression(id); |
| 6493 |
} |
| 6494 |
|
| 6495 |
// Returns the function args for a texture sampling function for the specified image and sampling characteristics. |
| 6496 |
string CompilerGLSL::to_function_args(const TextureFunctionArguments &args, bool *p_forward) |
| 6497 |
{ |
| 6498 |
VariableID img = args.base.img; |
| 6499 |
auto &imgtype = *args.base.imgtype; |
| 6500 |
|
| 6501 |
string farg_str; |
| 6502 |
if (args.base.is_fetch) |
| 6503 |
farg_str = convert_separate_image_to_expression(img); |
| 6504 |
else |
| 6505 |
farg_str = to_expression(img); |
| 6506 |
|
| 6507 |
bool swizz_func = backend.swizzle_is_function; |
| 6508 |
auto swizzle = [swizz_func](uint32_t comps, uint32_t in_comps) -> const char * { |
| 6509 |
if (comps == in_comps) |
| 6510 |
return ""; |
| 6511 |
|
| 6512 |
switch (comps) |
| 6513 |
{ |
| 6514 |
case 1: |
| 6515 |
return ".x"; |
| 6516 |
case 2: |
| 6517 |
return swizz_func ? ".xy()" : ".xy"; |
| 6518 |
case 3: |
| 6519 |
return swizz_func ? ".xyz()" : ".xyz"; |
| 6520 |
default: |
| 6521 |
return ""; |
| 6522 |
} |
| 6523 |
}; |
| 6524 |
|
| 6525 |
bool forward = should_forward(args.coord); |
| 6526 |
|
| 6527 |
// The IR can give us more components than we need, so chop them off as needed. |
| 6528 |
auto swizzle_expr = swizzle(args.coord_components, expression_type(args.coord).vecsize); |
| 6529 |
// Only enclose the UV expression if needed. |
| 6530 |
auto coord_expr = |
| 6531 |
(*swizzle_expr == '\0') ? to_expression(args.coord) : (to_enclosed_expression(args.coord) + swizzle_expr); |
| 6532 |
|
| 6533 |
// texelFetch only takes int, not uint. |
| 6534 |
auto &coord_type = expression_type(args.coord); |
| 6535 |
if (coord_type.basetype == SPIRType::UInt) |
| 6536 |
{ |
| 6537 |
auto expected_type = coord_type; |
| 6538 |
expected_type.vecsize = args.coord_components; |
| 6539 |
expected_type.basetype = SPIRType::Int; |
| 6540 |
coord_expr = bitcast_expression(expected_type, coord_type.basetype, coord_expr); |
| 6541 |
} |
| 6542 |
|
| 6543 |
// textureLod on sampler2DArrayShadow and samplerCubeShadow does not exist in GLSL for some reason. |
| 6544 |
// To emulate this, we will have to use textureGrad with a constant gradient of 0. |
| 6545 |
// The workaround will assert that the LOD is in fact constant 0, or we cannot emit correct code. |
| 6546 |
// This happens for HLSL SampleCmpLevelZero on Texture2DArray and TextureCube. |
| 6547 |
bool workaround_lod_array_shadow_as_grad = |
| 6548 |
((imgtype.image.arrayed && imgtype.image.dim == Dim2D) || imgtype.image.dim == DimCube) && |
| 6549 |
image_is_comparison(imgtype, img) && args.lod != 0; |
| 6550 |
|
| 6551 |
if (args.dref) |
| 6552 |
{ |
| 6553 |
forward = forward && should_forward(args.dref); |
| 6554 |
|
| 6555 |
// SPIR-V splits dref and coordinate. |
| 6556 |
if (args.base.is_gather || |
| 6557 |
args.coord_components == 4) // GLSL also splits the arguments in two. Same for textureGather. |
| 6558 |
{ |
| 6559 |
farg_str += ", "; |
| 6560 |
farg_str += to_expression(args.coord); |
| 6561 |
farg_str += ", "; |
| 6562 |
farg_str += to_expression(args.dref); |
| 6563 |
} |
| 6564 |
else if (args.base.is_proj) |
| 6565 |
{ |
| 6566 |
// Have to reshuffle so we get vec4(coord, dref, proj), special case. |
| 6567 |
// Other shading languages splits up the arguments for coord and compare value like SPIR-V. |
| 6568 |
// The coordinate type for textureProj shadow is always vec4 even for sampler1DShadow. |
| 6569 |
farg_str += ", vec4("; |
| 6570 |
|
| 6571 |
if (imgtype.image.dim == Dim1D) |
| 6572 |
{ |
| 6573 |
// Could reuse coord_expr, but we will mess up the temporary usage checking. |
| 6574 |
farg_str += to_enclosed_expression(args.coord) + ".x"; |
| 6575 |
farg_str += ", "; |
| 6576 |
farg_str += "0.0, "; |
| 6577 |
farg_str += to_expression(args.dref); |
| 6578 |
farg_str += ", "; |
| 6579 |
farg_str += to_enclosed_expression(args.coord) + ".y)"; |
| 6580 |
} |
| 6581 |
else if (imgtype.image.dim == Dim2D) |
| 6582 |
{ |
| 6583 |
// Could reuse coord_expr, but we will mess up the temporary usage checking. |
| 6584 |
farg_str += to_enclosed_expression(args.coord) + (swizz_func ? ".xy()" : ".xy"); |
| 6585 |
farg_str += ", "; |
| 6586 |
farg_str += to_expression(args.dref); |
| 6587 |
farg_str += ", "; |
| 6588 |
farg_str += to_enclosed_expression(args.coord) + ".z)"; |
| 6589 |
} |
| 6590 |
else |
| 6591 |
SPIRV_CROSS_THROW("Invalid type for textureProj with shadow."); |
| 6592 |
} |
| 6593 |
else |
| 6594 |
{ |
| 6595 |
// Create a composite which merges coord/dref into a single vector. |
| 6596 |
auto type = expression_type(args.coord); |
| 6597 |
type.vecsize = args.coord_components + 1; |
| 6598 |
farg_str += ", "; |
| 6599 |
farg_str += type_to_glsl_constructor(type); |
| 6600 |
farg_str += "("; |
| 6601 |
farg_str += coord_expr; |
| 6602 |
farg_str += ", "; |
| 6603 |
farg_str += to_expression(args.dref); |
| 6604 |
farg_str += ")"; |
| 6605 |
} |
| 6606 |
} |
| 6607 |
else |
| 6608 |
{ |
| 6609 |
farg_str += ", "; |
| 6610 |
farg_str += coord_expr; |
| 6611 |
} |
| 6612 |
|
| 6613 |
if (args.grad_x || args.grad_y) |
| 6614 |
{ |
| 6615 |
forward = forward && should_forward(args.grad_x); |
| 6616 |
forward = forward && should_forward(args.grad_y); |
| 6617 |
farg_str += ", "; |
| 6618 |
farg_str += to_expression(args.grad_x); |
| 6619 |
farg_str += ", "; |
| 6620 |
farg_str += to_expression(args.grad_y); |
| 6621 |
} |
| 6622 |
|
| 6623 |
if (args.lod) |
| 6624 |
{ |
| 6625 |
if (workaround_lod_array_shadow_as_grad) |
| 6626 |
{ |
| 6627 |
// Implement textureGrad() instead. LOD == 0.0 is implemented as gradient of 0.0. |
| 6628 |
// Implementing this as plain texture() is not safe on some implementations. |
| 6629 |
if (imgtype.image.dim == Dim2D) |
| 6630 |
farg_str += ", vec2(0.0), vec2(0.0)"; |
| 6631 |
else if (imgtype.image.dim == DimCube) |
| 6632 |
farg_str += ", vec3(0.0), vec3(0.0)"; |
| 6633 |
} |
| 6634 |
else |
| 6635 |
{ |
| 6636 |
forward = forward && should_forward(args.lod); |
| 6637 |
farg_str += ", "; |
| 6638 |
|
| 6639 |
auto &lod_expr_type = expression_type(args.lod); |
| 6640 |
|
| 6641 |
// Lod expression for TexelFetch in GLSL must be int, and only int. |
| 6642 |
if (args.base.is_fetch && imgtype.image.dim != DimBuffer && !imgtype.image.ms && |
| 6643 |
lod_expr_type.basetype != SPIRType::Int) |
| 6644 |
{ |
| 6645 |
farg_str += join("int(", to_expression(args.lod), ")"); |
| 6646 |
} |
| 6647 |
else |
| 6648 |
{ |
| 6649 |
farg_str += to_expression(args.lod); |
| 6650 |
} |
| 6651 |
} |
| 6652 |
} |
| 6653 |
else if (args.base.is_fetch && imgtype.image.dim != DimBuffer && !imgtype.image.ms) |
| 6654 |
{ |
| 6655 |
// Lod argument is optional in OpImageFetch, but we require a LOD value, pick 0 as the default. |
| 6656 |
farg_str += ", 0"; |
| 6657 |
} |
| 6658 |
|
| 6659 |
if (args.coffset) |
| 6660 |
{ |
| 6661 |
forward = forward && should_forward(args.coffset); |
| 6662 |
farg_str += ", "; |
| 6663 |
farg_str += to_expression(args.coffset); |
| 6664 |
} |
| 6665 |
else if (args.offset) |
| 6666 |
{ |
| 6667 |
forward = forward && should_forward(args.offset); |
| 6668 |
farg_str += ", "; |
| 6669 |
farg_str += to_expression(args.offset); |
| 6670 |
} |
| 6671 |
|
| 6672 |
if (args.sample) |
| 6673 |
{ |
| 6674 |
farg_str += ", "; |
| 6675 |
farg_str += to_expression(args.sample); |
| 6676 |
} |
| 6677 |
|
| 6678 |
if (args.min_lod) |
| 6679 |
{ |
| 6680 |
farg_str += ", "; |
| 6681 |
farg_str += to_expression(args.min_lod); |
| 6682 |
} |
| 6683 |
|
| 6684 |
if (args.sparse_texel) |
| 6685 |
{ |
| 6686 |
// Sparse texel output parameter comes after everything else, except it's before the optional, component/bias arguments. |
| 6687 |
farg_str += ", "; |
| 6688 |
farg_str += to_expression(args.sparse_texel); |
| 6689 |
} |
| 6690 |
|
| 6691 |
if (args.bias) |
| 6692 |
{ |
| 6693 |
forward = forward && should_forward(args.bias); |
| 6694 |
farg_str += ", "; |
| 6695 |
farg_str += to_expression(args.bias); |
| 6696 |
} |
| 6697 |
|
| 6698 |
if (args.component && !expression_is_constant_null(args.component)) |
| 6699 |
{ |
| 6700 |
forward = forward && should_forward(args.component); |
| 6701 |
farg_str += ", "; |
| 6702 |
farg_str += to_expression(args.component); |
| 6703 |
} |
| 6704 |
|
| 6705 |
*p_forward = forward; |
| 6706 |
|
| 6707 |
return farg_str; |
| 6708 |
} |
| 6709 |
|
| 6710 |
void CompilerGLSL::emit_glsl_op(uint32_t result_type, uint32_t id, uint32_t eop, const uint32_t *args, uint32_t length) |
| 6711 |
{ |
| 6712 |
auto op = static_cast<GLSLstd450>(eop); |
| 6713 |
|
| 6714 |
if (is_legacy() && is_unsigned_glsl_opcode(op)) |
| 6715 |
SPIRV_CROSS_THROW("Unsigned integers are not supported on legacy GLSL targets."); |
| 6716 |
|
| 6717 |
// If we need to do implicit bitcasts, make sure we do it with the correct type. |
| 6718 |
uint32_t integer_width = get_integer_width_for_glsl_instruction(op, args, length); |
| 6719 |
auto int_type = to_signed_basetype(integer_width); |
| 6720 |
auto uint_type = to_unsigned_basetype(integer_width); |
| 6721 |
|
| 6722 |
switch (op) |
| 6723 |
{ |
| 6724 |
// FP fiddling |
| 6725 |
case GLSLstd450Round: |
| 6726 |
if (!is_legacy()) |
| 6727 |
emit_unary_func_op(result_type, id, args[0], "round"); |
| 6728 |
else |
| 6729 |
{ |
| 6730 |
auto op0 = to_enclosed_expression(args[0]); |
| 6731 |
auto &op0_type = expression_type(args[0]); |
| 6732 |
auto expr = join("floor(", op0, " + ", type_to_glsl_constructor(op0_type), "(0.5))"); |
| 6733 |
bool forward = should_forward(args[0]); |
| 6734 |
emit_op(result_type, id, expr, forward); |
| 6735 |
inherit_expression_dependencies(id, args[0]); |
| 6736 |
} |
| 6737 |
break; |
| 6738 |
|
| 6739 |
case GLSLstd450RoundEven: |
| 6740 |
if (!is_legacy()) |
| 6741 |
emit_unary_func_op(result_type, id, args[0], "roundEven"); |
| 6742 |
else if (!options.es) |
| 6743 |
{ |
| 6744 |
// This extension provides round() with round-to-even semantics. |
| 6745 |
require_extension_internal("GL_EXT_gpu_shader4"); |
| 6746 |
emit_unary_func_op(result_type, id, args[0], "round"); |
| 6747 |
} |
| 6748 |
else |
| 6749 |
SPIRV_CROSS_THROW("roundEven supported only in ESSL 300."); |
| 6750 |
break; |
| 6751 |
|
| 6752 |
case GLSLstd450Trunc: |
| 6753 |
emit_unary_func_op(result_type, id, args[0], "trunc"); |
| 6754 |
break; |
| 6755 |
case GLSLstd450SAbs: |
| 6756 |
emit_unary_func_op_cast(result_type, id, args[0], "abs", int_type, int_type); |
| 6757 |
break; |
| 6758 |
case GLSLstd450FAbs: |
| 6759 |
emit_unary_func_op(result_type, id, args[0], "abs"); |
| 6760 |
break; |
| 6761 |
case GLSLstd450SSign: |
| 6762 |
emit_unary_func_op_cast(result_type, id, args[0], "sign", int_type, int_type); |
| 6763 |
break; |
| 6764 |
case GLSLstd450FSign: |
| 6765 |
emit_unary_func_op(result_type, id, args[0], "sign"); |
| 6766 |
break; |
| 6767 |
case GLSLstd450Floor: |
| 6768 |
emit_unary_func_op(result_type, id, args[0], "floor"); |
| 6769 |
break; |
| 6770 |
case GLSLstd450Ceil: |
| 6771 |
emit_unary_func_op(result_type, id, args[0], "ceil"); |
| 6772 |
break; |
| 6773 |
case GLSLstd450Fract: |
| 6774 |
emit_unary_func_op(result_type, id, args[0], "fract"); |
| 6775 |
break; |
| 6776 |
case GLSLstd450Radians: |
| 6777 |
emit_unary_func_op(result_type, id, args[0], "radians"); |
| 6778 |
break; |
| 6779 |
case GLSLstd450Degrees: |
| 6780 |
emit_unary_func_op(result_type, id, args[0], "degrees"); |
| 6781 |
break; |
| 6782 |
case GLSLstd450Fma: |
| 6783 |
if ((!options.es && options.version < 400) || (options.es && options.version < 320)) |
| 6784 |
{ |
| 6785 |
auto expr = join(to_enclosed_expression(args[0]), " * ", to_enclosed_expression(args[1]), " + ", |
| 6786 |
to_enclosed_expression(args[2])); |
| 6787 |
|
| 6788 |
emit_op(result_type, id, expr, |
| 6789 |
should_forward(args[0]) && should_forward(args[1]) && should_forward(args[2])); |
| 6790 |
for (uint32_t i = 0; i < 3; i++) |
| 6791 |
inherit_expression_dependencies(id, args[i]); |
| 6792 |
} |
| 6793 |
else |
| 6794 |
emit_trinary_func_op(result_type, id, args[0], args[1], args[2], "fma"); |
| 6795 |
break; |
| 6796 |
case GLSLstd450Modf: |
| 6797 |
register_call_out_argument(args[1]); |
| 6798 |
forced_temporaries.insert(id); |
| 6799 |
emit_binary_func_op(result_type, id, args[0], args[1], "modf"); |
| 6800 |
break; |
| 6801 |
|
| 6802 |
case GLSLstd450ModfStruct: |
| 6803 |
{ |
| 6804 |
auto &type = get<SPIRType>(result_type); |
| 6805 |
emit_uninitialized_temporary_expression(result_type, id); |
| 6806 |
statement(to_expression(id), ".", to_member_name(type, 0), " = ", "modf(", to_expression(args[0]), ", ", |
| 6807 |
to_expression(id), ".", to_member_name(type, 1), ");"); |
| 6808 |
break; |
| 6809 |
} |
| 6810 |
|
| 6811 |
// Minmax |
| 6812 |
case GLSLstd450UMin: |
| 6813 |
emit_binary_func_op_cast(result_type, id, args[0], args[1], "min", uint_type, false); |
| 6814 |
break; |
| 6815 |
|
| 6816 |
case GLSLstd450SMin: |
| 6817 |
emit_binary_func_op_cast(result_type, id, args[0], args[1], "min", int_type, false); |
| 6818 |
break; |
| 6819 |
|
| 6820 |
case GLSLstd450FMin: |
| 6821 |
emit_binary_func_op(result_type, id, args[0], args[1], "min"); |
| 6822 |
break; |
| 6823 |
|
| 6824 |
case GLSLstd450FMax: |
| 6825 |
emit_binary_func_op(result_type, id, args[0], args[1], "max"); |
| 6826 |
break; |
| 6827 |
|
| 6828 |
case GLSLstd450UMax: |
| 6829 |
emit_binary_func_op_cast(result_type, id, args[0], args[1], "max", uint_type, false); |
| 6830 |
break; |
| 6831 |
|
| 6832 |
case GLSLstd450SMax: |
| 6833 |
emit_binary_func_op_cast(result_type, id, args[0], args[1], "max", int_type, false); |
| 6834 |
break; |
| 6835 |
|
| 6836 |
case GLSLstd450FClamp: |
| 6837 |
emit_trinary_func_op(result_type, id, args[0], args[1], args[2], "clamp"); |
| 6838 |
break; |
| 6839 |
|
| 6840 |
case GLSLstd450UClamp: |
| 6841 |
emit_trinary_func_op_cast(result_type, id, args[0], args[1], args[2], "clamp", uint_type); |
| 6842 |
break; |
| 6843 |
|
| 6844 |
case GLSLstd450SClamp: |
| 6845 |
emit_trinary_func_op_cast(result_type, id, args[0], args[1], args[2], "clamp", int_type); |
| 6846 |
break; |
| 6847 |
|
| 6848 |
// Trig |
| 6849 |
case GLSLstd450Sin: |
| 6850 |
emit_unary_func_op(result_type, id, args[0], "sin"); |
| 6851 |
break; |
| 6852 |
case GLSLstd450Cos: |
| 6853 |
emit_unary_func_op(result_type, id, args[0], "cos"); |
| 6854 |
break; |
| 6855 |
case GLSLstd450Tan: |
| 6856 |
emit_unary_func_op(result_type, id, args[0], "tan"); |
| 6857 |
break; |
| 6858 |
case GLSLstd450Asin: |
| 6859 |
emit_unary_func_op(result_type, id, args[0], "asin"); |
| 6860 |
break; |
| 6861 |
case GLSLstd450Acos: |
| 6862 |
emit_unary_func_op(result_type, id, args[0], "acos"); |
| 6863 |
break; |
| 6864 |
case GLSLstd450Atan: |
| 6865 |
emit_unary_func_op(result_type, id, args[0], "atan"); |
| 6866 |
break; |
| 6867 |
case GLSLstd450Sinh: |
| 6868 |
emit_unary_func_op(result_type, id, args[0], "sinh"); |
| 6869 |
break; |
| 6870 |
case GLSLstd450Cosh: |
| 6871 |
emit_unary_func_op(result_type, id, args[0], "cosh"); |
| 6872 |
break; |
| 6873 |
case GLSLstd450Tanh: |
| 6874 |
emit_unary_func_op(result_type, id, args[0], "tanh"); |
| 6875 |
break; |
| 6876 |
case GLSLstd450Asinh: |
| 6877 |
emit_unary_func_op(result_type, id, args[0], "asinh"); |
| 6878 |
break; |
| 6879 |
case GLSLstd450Acosh: |
| 6880 |
emit_unary_func_op(result_type, id, args[0], "acosh"); |
| 6881 |
break; |
| 6882 |
case GLSLstd450Atanh: |
| 6883 |
emit_unary_func_op(result_type, id, args[0], "atanh"); |
| 6884 |
break; |
| 6885 |
case GLSLstd450Atan2: |
| 6886 |
emit_binary_func_op(result_type, id, args[0], args[1], "atan"); |
| 6887 |
break; |
| 6888 |
|
| 6889 |
// Exponentials |
| 6890 |
case GLSLstd450Pow: |
| 6891 |
emit_binary_func_op(result_type, id, args[0], args[1], "pow"); |
| 6892 |
break; |
| 6893 |
case GLSLstd450Exp: |
| 6894 |
emit_unary_func_op(result_type, id, args[0], "exp"); |
| 6895 |
break; |
| 6896 |
case GLSLstd450Log: |
| 6897 |
emit_unary_func_op(result_type, id, args[0], "log"); |
| 6898 |
break; |
| 6899 |
case GLSLstd450Exp2: |
| 6900 |
emit_unary_func_op(result_type, id, args[0], "exp2"); |
| 6901 |
break; |
| 6902 |
case GLSLstd450Log2: |
| 6903 |
emit_unary_func_op(result_type, id, args[0], "log2"); |
| 6904 |
break; |
| 6905 |
case GLSLstd450Sqrt: |
| 6906 |
emit_unary_func_op(result_type, id, args[0], "sqrt"); |
| 6907 |
break; |
| 6908 |
case GLSLstd450InverseSqrt: |
| 6909 |
emit_unary_func_op(result_type, id, args[0], "inversesqrt"); |
| 6910 |
break; |
| 6911 |
|
| 6912 |
// Matrix math |
| 6913 |
case GLSLstd450Determinant: |
| 6914 |
emit_unary_func_op(result_type, id, args[0], "determinant"); |
| 6915 |
break; |
| 6916 |
case GLSLstd450MatrixInverse: |
| 6917 |
emit_unary_func_op(result_type, id, args[0], "inverse"); |
| 6918 |
break; |
| 6919 |
|
| 6920 |
// Lerping |
| 6921 |
case GLSLstd450FMix: |
| 6922 |
case GLSLstd450IMix: |
| 6923 |
{ |
| 6924 |
emit_mix_op(result_type, id, args[0], args[1], args[2]); |
| 6925 |
break; |
| 6926 |
} |
| 6927 |
case GLSLstd450Step: |
| 6928 |
emit_binary_func_op(result_type, id, args[0], args[1], "step"); |
| 6929 |
break; |
| 6930 |
case GLSLstd450SmoothStep: |
| 6931 |
emit_trinary_func_op(result_type, id, args[0], args[1], args[2], "smoothstep"); |
| 6932 |
break; |
| 6933 |
|
| 6934 |
// Packing |
| 6935 |
case GLSLstd450Frexp: |
| 6936 |
register_call_out_argument(args[1]); |
| 6937 |
forced_temporaries.insert(id); |
| 6938 |
emit_binary_func_op(result_type, id, args[0], args[1], "frexp"); |
| 6939 |
break; |
| 6940 |
|
| 6941 |
case GLSLstd450FrexpStruct: |
| 6942 |
{ |
| 6943 |
auto &type = get<SPIRType>(result_type); |
| 6944 |
emit_uninitialized_temporary_expression(result_type, id); |
| 6945 |
statement(to_expression(id), ".", to_member_name(type, 0), " = ", "frexp(", to_expression(args[0]), ", ", |
| 6946 |
to_expression(id), ".", to_member_name(type, 1), ");"); |
| 6947 |
break; |
| 6948 |
} |
| 6949 |
|
| 6950 |
case GLSLstd450Ldexp: |
| 6951 |
{ |
| 6952 |
bool forward = should_forward(args[0]) && should_forward(args[1]); |
| 6953 |
|
| 6954 |
auto op0 = to_unpacked_expression(args[0]); |
| 6955 |
auto op1 = to_unpacked_expression(args[1]); |
| 6956 |
auto &op1_type = expression_type(args[1]); |
| 6957 |
if (op1_type.basetype != SPIRType::Int) |
| 6958 |
{ |
| 6959 |
// Need a value cast here. |
| 6960 |
auto target_type = op1_type; |
| 6961 |
target_type.basetype = SPIRType::Int; |
| 6962 |
op1 = join(type_to_glsl_constructor(target_type), "(", op1, ")"); |
| 6963 |
} |
| 6964 |
|
| 6965 |
auto expr = join("ldexp(", op0, ", ", op1, ")"); |
| 6966 |
|
| 6967 |
emit_op(result_type, id, expr, forward); |
| 6968 |
inherit_expression_dependencies(id, args[0]); |
| 6969 |
inherit_expression_dependencies(id, args[1]); |
| 6970 |
break; |
| 6971 |
} |
| 6972 |
|
| 6973 |
case GLSLstd450PackSnorm4x8: |
| 6974 |
emit_unary_func_op(result_type, id, args[0], "packSnorm4x8"); |
| 6975 |
break; |
| 6976 |
case GLSLstd450PackUnorm4x8: |
| 6977 |
emit_unary_func_op(result_type, id, args[0], "packUnorm4x8"); |
| 6978 |
break; |
| 6979 |
case GLSLstd450PackSnorm2x16: |
| 6980 |
emit_unary_func_op(result_type, id, args[0], "packSnorm2x16"); |
| 6981 |
break; |
| 6982 |
case GLSLstd450PackUnorm2x16: |
| 6983 |
emit_unary_func_op(result_type, id, args[0], "packUnorm2x16"); |
| 6984 |
break; |
| 6985 |
case GLSLstd450PackHalf2x16: |
| 6986 |
emit_unary_func_op(result_type, id, args[0], "packHalf2x16"); |
| 6987 |
break; |
| 6988 |
case GLSLstd450UnpackSnorm4x8: |
| 6989 |
emit_unary_func_op(result_type, id, args[0], "unpackSnorm4x8"); |
| 6990 |
break; |
| 6991 |
case GLSLstd450UnpackUnorm4x8: |
| 6992 |
emit_unary_func_op(result_type, id, args[0], "unpackUnorm4x8"); |
| 6993 |
break; |
| 6994 |
case GLSLstd450UnpackSnorm2x16: |
| 6995 |
emit_unary_func_op(result_type, id, args[0], "unpackSnorm2x16"); |
| 6996 |
break; |
| 6997 |
case GLSLstd450UnpackUnorm2x16: |
| 6998 |
emit_unary_func_op(result_type, id, args[0], "unpackUnorm2x16"); |
| 6999 |
break; |
| 7000 |
case GLSLstd450UnpackHalf2x16: |
| 7001 |
emit_unary_func_op(result_type, id, args[0], "unpackHalf2x16"); |
| 7002 |
break; |
| 7003 |
|
| 7004 |
case GLSLstd450PackDouble2x32: |
| 7005 |
emit_unary_func_op(result_type, id, args[0], "packDouble2x32"); |
| 7006 |
break; |
| 7007 |
case GLSLstd450UnpackDouble2x32: |
| 7008 |
emit_unary_func_op(result_type, id, args[0], "unpackDouble2x32"); |
| 7009 |
break; |
| 7010 |
|
| 7011 |
// Vector math |
| 7012 |
case GLSLstd450Length: |
| 7013 |
emit_unary_func_op(result_type, id, args[0], "length"); |
| 7014 |
break; |
| 7015 |
case GLSLstd450Distance: |
| 7016 |
emit_binary_func_op(result_type, id, args[0], args[1], "distance"); |
| 7017 |
break; |
| 7018 |
case GLSLstd450Cross: |
| 7019 |
emit_binary_func_op(result_type, id, args[0], args[1], "cross"); |
| 7020 |
break; |
| 7021 |
case GLSLstd450Normalize: |
| 7022 |
emit_unary_func_op(result_type, id, args[0], "normalize"); |
| 7023 |
break; |
| 7024 |
case GLSLstd450FaceForward: |
| 7025 |
emit_trinary_func_op(result_type, id, args[0], args[1], args[2], "faceforward"); |
| 7026 |
break; |
| 7027 |
case GLSLstd450Reflect: |
| 7028 |
emit_binary_func_op(result_type, id, args[0], args[1], "reflect"); |
| 7029 |
break; |
| 7030 |
case GLSLstd450Refract: |
| 7031 |
emit_trinary_func_op(result_type, id, args[0], args[1], args[2], "refract"); |
| 7032 |
break; |
| 7033 |
|
| 7034 |
// Bit-fiddling |
| 7035 |
case GLSLstd450FindILsb: |
| 7036 |
// findLSB always returns int. |
| 7037 |
emit_unary_func_op_cast(result_type, id, args[0], "findLSB", expression_type(args[0]).basetype, int_type); |
| 7038 |
break; |
| 7039 |
|
| 7040 |
case GLSLstd450FindSMsb: |
| 7041 |
emit_unary_func_op_cast(result_type, id, args[0], "findMSB", int_type, int_type); |
| 7042 |
break; |
| 7043 |
|
| 7044 |
case GLSLstd450FindUMsb: |
| 7045 |
emit_unary_func_op_cast(result_type, id, args[0], "findMSB", uint_type, |
| 7046 |
int_type); // findMSB always returns int. |
| 7047 |
break; |
| 7048 |
|
| 7049 |
// Multisampled varying |
| 7050 |
case GLSLstd450InterpolateAtCentroid: |
| 7051 |
emit_unary_func_op(result_type, id, args[0], "interpolateAtCentroid"); |
| 7052 |
break; |
| 7053 |
case GLSLstd450InterpolateAtSample: |
| 7054 |
emit_binary_func_op(result_type, id, args[0], args[1], "interpolateAtSample"); |
| 7055 |
break; |
| 7056 |
case GLSLstd450InterpolateAtOffset: |
| 7057 |
emit_binary_func_op(result_type, id, args[0], args[1], "interpolateAtOffset"); |
| 7058 |
break; |
| 7059 |
|
| 7060 |
case GLSLstd450NMin: |
| 7061 |
case GLSLstd450NMax: |
| 7062 |
{ |
| 7063 |
emit_nminmax_op(result_type, id, args[0], args[1], op); |
| 7064 |
break; |
| 7065 |
} |
| 7066 |
|
| 7067 |
case GLSLstd450NClamp: |
| 7068 |
{ |
| 7069 |
// Make sure we have a unique ID here to avoid aliasing the extra sub-expressions between clamp and NMin sub-op. |
| 7070 |
// IDs cannot exceed 24 bits, so we can make use of the higher bits for some unique flags. |
| 7071 |
uint32_t &max_id = extra_sub_expressions[id | 0x80000000u]; |
| 7072 |
if (!max_id) |
| 7073 |
max_id = ir.increase_bound_by(1); |
| 7074 |
|
| 7075 |
// Inherit precision qualifiers. |
| 7076 |
ir.meta[max_id] = ir.meta[id]; |
| 7077 |
|
| 7078 |
emit_nminmax_op(result_type, max_id, args[0], args[1], GLSLstd450NMax); |
| 7079 |
emit_nminmax_op(result_type, id, max_id, args[2], GLSLstd450NMin); |
| 7080 |
break; |
| 7081 |
} |
| 7082 |
|
| 7083 |
default: |
| 7084 |
statement("// unimplemented GLSL op ", eop); |
| 7085 |
break; |
| 7086 |
} |
| 7087 |
} |
| 7088 |
|
| 7089 |
void CompilerGLSL::emit_nminmax_op(uint32_t result_type, uint32_t id, uint32_t op0, uint32_t op1, GLSLstd450 op) |
| 7090 |
{ |
| 7091 |
// Need to emulate this call. |
| 7092 |
uint32_t &ids = extra_sub_expressions[id]; |
| 7093 |
if (!ids) |
| 7094 |
{ |
| 7095 |
ids = ir.increase_bound_by(5); |
| 7096 |
auto btype = get<SPIRType>(result_type); |
| 7097 |
btype.basetype = SPIRType::Boolean; |
| 7098 |
set<SPIRType>(ids, btype); |
| 7099 |
} |
| 7100 |
|
| 7101 |
uint32_t btype_id = ids + 0; |
| 7102 |
uint32_t left_nan_id = ids + 1; |
| 7103 |
uint32_t right_nan_id = ids + 2; |
| 7104 |
uint32_t tmp_id = ids + 3; |
| 7105 |
uint32_t mixed_first_id = ids + 4; |
| 7106 |
|
| 7107 |
// Inherit precision qualifiers. |
| 7108 |
ir.meta[tmp_id] = ir.meta[id]; |
| 7109 |
ir.meta[mixed_first_id] = ir.meta[id]; |
| 7110 |
|
| 7111 |
emit_unary_func_op(btype_id, left_nan_id, op0, "isnan"); |
| 7112 |
emit_unary_func_op(btype_id, right_nan_id, op1, "isnan"); |
| 7113 |
emit_binary_func_op(result_type, tmp_id, op0, op1, op == GLSLstd450NMin ? "min" : "max"); |
| 7114 |
emit_mix_op(result_type, mixed_first_id, tmp_id, op1, left_nan_id); |
| 7115 |
emit_mix_op(result_type, id, mixed_first_id, op0, right_nan_id); |
| 7116 |
} |
| 7117 |
|
| 7118 |
void CompilerGLSL::emit_spv_amd_shader_ballot_op(uint32_t result_type, uint32_t id, uint32_t eop, const uint32_t *args, |
| 7119 |
uint32_t) |
| 7120 |
{ |
| 7121 |
require_extension_internal("GL_AMD_shader_ballot"); |
| 7122 |
|
| 7123 |
enum AMDShaderBallot |
| 7124 |
{ |
| 7125 |
SwizzleInvocationsAMD = 1, |
| 7126 |
SwizzleInvocationsMaskedAMD = 2, |
| 7127 |
WriteInvocationAMD = 3, |
| 7128 |
MbcntAMD = 4 |
| 7129 |
}; |
| 7130 |
|
| 7131 |
auto op = static_cast<AMDShaderBallot>(eop); |
| 7132 |
|
| 7133 |
switch (op) |
| 7134 |
{ |
| 7135 |
case SwizzleInvocationsAMD: |
| 7136 |
emit_binary_func_op(result_type, id, args[0], args[1], "swizzleInvocationsAMD"); |
| 7137 |
register_control_dependent_expression(id); |
| 7138 |
break; |
| 7139 |
|
| 7140 |
case SwizzleInvocationsMaskedAMD: |
| 7141 |
emit_binary_func_op(result_type, id, args[0], args[1], "swizzleInvocationsMaskedAMD"); |
| 7142 |
register_control_dependent_expression(id); |
| 7143 |
break; |
| 7144 |
|
| 7145 |
case WriteInvocationAMD: |
| 7146 |
emit_trinary_func_op(result_type, id, args[0], args[1], args[2], "writeInvocationAMD"); |
| 7147 |
register_control_dependent_expression(id); |
| 7148 |
break; |
| 7149 |
|
| 7150 |
case MbcntAMD: |
| 7151 |
emit_unary_func_op(result_type, id, args[0], "mbcntAMD"); |
| 7152 |
register_control_dependent_expression(id); |
| 7153 |
break; |
| 7154 |
|
| 7155 |
default: |
| 7156 |
statement("// unimplemented SPV AMD shader ballot op ", eop); |
| 7157 |
break; |
| 7158 |
} |
| 7159 |
} |
| 7160 |
|
| 7161 |
void CompilerGLSL::emit_spv_amd_shader_explicit_vertex_parameter_op(uint32_t result_type, uint32_t id, uint32_t eop, |
| 7162 |
const uint32_t *args, uint32_t) |
| 7163 |
{ |
| 7164 |
require_extension_internal("GL_AMD_shader_explicit_vertex_parameter"); |
| 7165 |
|
| 7166 |
enum AMDShaderExplicitVertexParameter |
| 7167 |
{ |
| 7168 |
InterpolateAtVertexAMD = 1 |
| 7169 |
}; |
| 7170 |
|
| 7171 |
auto op = static_cast<AMDShaderExplicitVertexParameter>(eop); |
| 7172 |
|
| 7173 |
switch (op) |
| 7174 |
{ |
| 7175 |
case InterpolateAtVertexAMD: |
| 7176 |
emit_binary_func_op(result_type, id, args[0], args[1], "interpolateAtVertexAMD"); |
| 7177 |
break; |
| 7178 |
|
| 7179 |
default: |
| 7180 |
statement("// unimplemented SPV AMD shader explicit vertex parameter op ", eop); |
| 7181 |
break; |
| 7182 |
} |
| 7183 |
} |
| 7184 |
|
| 7185 |
void CompilerGLSL::emit_spv_amd_shader_trinary_minmax_op(uint32_t result_type, uint32_t id, uint32_t eop, |
| 7186 |
const uint32_t *args, uint32_t) |
| 7187 |
{ |
| 7188 |
require_extension_internal("GL_AMD_shader_trinary_minmax"); |
| 7189 |
|
| 7190 |
enum AMDShaderTrinaryMinMax |
| 7191 |
{ |
| 7192 |
FMin3AMD = 1, |
| 7193 |
UMin3AMD = 2, |
| 7194 |
SMin3AMD = 3, |
| 7195 |
FMax3AMD = 4, |
| 7196 |
UMax3AMD = 5, |
| 7197 |
SMax3AMD = 6, |
| 7198 |
FMid3AMD = 7, |
| 7199 |
UMid3AMD = 8, |
| 7200 |
SMid3AMD = 9 |
| 7201 |
}; |
| 7202 |
|
| 7203 |
auto op = static_cast<AMDShaderTrinaryMinMax>(eop); |
| 7204 |
|
| 7205 |
switch (op) |
| 7206 |
{ |
| 7207 |
case FMin3AMD: |
| 7208 |
case UMin3AMD: |
| 7209 |
case SMin3AMD: |
| 7210 |
emit_trinary_func_op(result_type, id, args[0], args[1], args[2], "min3"); |
| 7211 |
break; |
| 7212 |
|
| 7213 |
case FMax3AMD: |
| 7214 |
case UMax3AMD: |
| 7215 |
case SMax3AMD: |
| 7216 |
emit_trinary_func_op(result_type, id, args[0], args[1], args[2], "max3"); |
| 7217 |
break; |
| 7218 |
|
| 7219 |
case FMid3AMD: |
| 7220 |
case UMid3AMD: |
| 7221 |
case SMid3AMD: |
| 7222 |
emit_trinary_func_op(result_type, id, args[0], args[1], args[2], "mid3"); |
| 7223 |
break; |
| 7224 |
|
| 7225 |
default: |
| 7226 |
statement("// unimplemented SPV AMD shader trinary minmax op ", eop); |
| 7227 |
break; |
| 7228 |
} |
| 7229 |
} |
| 7230 |
|
| 7231 |
void CompilerGLSL::emit_spv_amd_gcn_shader_op(uint32_t result_type, uint32_t id, uint32_t eop, const uint32_t *args, |
| 7232 |
uint32_t) |
| 7233 |
{ |
| 7234 |
require_extension_internal("GL_AMD_gcn_shader"); |
| 7235 |
|
| 7236 |
enum AMDGCNShader |
| 7237 |
{ |
| 7238 |
CubeFaceIndexAMD = 1, |
| 7239 |
CubeFaceCoordAMD = 2, |
| 7240 |
TimeAMD = 3 |
| 7241 |
}; |
| 7242 |
|
| 7243 |
auto op = static_cast<AMDGCNShader>(eop); |
| 7244 |
|
| 7245 |
switch (op) |
| 7246 |
{ |
| 7247 |
case CubeFaceIndexAMD: |
| 7248 |
emit_unary_func_op(result_type, id, args[0], "cubeFaceIndexAMD"); |
| 7249 |
break; |
| 7250 |
case CubeFaceCoordAMD: |
| 7251 |
emit_unary_func_op(result_type, id, args[0], "cubeFaceCoordAMD"); |
| 7252 |
break; |
| 7253 |
case TimeAMD: |
| 7254 |
{ |
| 7255 |
string expr = "timeAMD()"; |
| 7256 |
emit_op(result_type, id, expr, true); |
| 7257 |
register_control_dependent_expression(id); |
| 7258 |
break; |
| 7259 |
} |
| 7260 |
|
| 7261 |
default: |
| 7262 |
statement("// unimplemented SPV AMD gcn shader op ", eop); |
| 7263 |
break; |
| 7264 |
} |
| 7265 |
} |
| 7266 |
|
| 7267 |
void CompilerGLSL::emit_subgroup_op(const Instruction &i) |
| 7268 |
{ |
| 7269 |
const uint32_t *ops = stream(i); |
| 7270 |
auto op = static_cast<Op>(i.op); |
| 7271 |
|
| 7272 |
if (!options.vulkan_semantics && !is_supported_subgroup_op_in_opengl(op)) |
| 7273 |
SPIRV_CROSS_THROW("This subgroup operation is only supported in Vulkan semantics."); |
| 7274 |
|
| 7275 |
// If we need to do implicit bitcasts, make sure we do it with the correct type. |
| 7276 |
uint32_t integer_width = get_integer_width_for_instruction(i); |
| 7277 |
auto int_type = to_signed_basetype(integer_width); |
| 7278 |
auto uint_type = to_unsigned_basetype(integer_width); |
| 7279 |
|
| 7280 |
switch (op) |
| 7281 |
{ |
| 7282 |
case OpGroupNonUniformElect: |
| 7283 |
request_subgroup_feature(ShaderSubgroupSupportHelper::SubgroupElect); |
| 7284 |
break; |
| 7285 |
|
| 7286 |
case OpGroupNonUniformBallotBitCount: |
| 7287 |
{ |
| 7288 |
const GroupOperation operation = static_cast<GroupOperation>(ops[3]); |
| 7289 |
if (operation == GroupOperationReduce) |
| 7290 |
request_subgroup_feature(ShaderSubgroupSupportHelper::SubgroupBallotBitCount); |
| 7291 |
else if (operation == GroupOperationInclusiveScan || operation == GroupOperationExclusiveScan) |
| 7292 |
request_subgroup_feature(ShaderSubgroupSupportHelper::SubgroupInverseBallot_InclBitCount_ExclBitCout); |
| 7293 |
} |
| 7294 |
break; |
| 7295 |
|
| 7296 |
case OpGroupNonUniformBallotBitExtract: |
| 7297 |
request_subgroup_feature(ShaderSubgroupSupportHelper::SubgroupBallotBitExtract); |
| 7298 |
break; |
| 7299 |
|
| 7300 |
case OpGroupNonUniformInverseBallot: |
| 7301 |
request_subgroup_feature(ShaderSubgroupSupportHelper::SubgroupInverseBallot_InclBitCount_ExclBitCout); |
| 7302 |
break; |
| 7303 |
|
| 7304 |
case OpGroupNonUniformBallot: |
| 7305 |
request_subgroup_feature(ShaderSubgroupSupportHelper::SubgroupBallot); |
| 7306 |
break; |
| 7307 |
|
| 7308 |
case OpGroupNonUniformBallotFindLSB: |
| 7309 |
case OpGroupNonUniformBallotFindMSB: |
| 7310 |
request_subgroup_feature(ShaderSubgroupSupportHelper::SubgroupBallotFindLSB_MSB); |
| 7311 |
break; |
| 7312 |
|
| 7313 |
case OpGroupNonUniformBroadcast: |
| 7314 |
case OpGroupNonUniformBroadcastFirst: |
| 7315 |
request_subgroup_feature(ShaderSubgroupSupportHelper::SubgroupBrodcast_First); |
| 7316 |
break; |
| 7317 |
|
| 7318 |
case OpGroupNonUniformShuffle: |
| 7319 |
case OpGroupNonUniformShuffleXor: |
| 7320 |
require_extension_internal("GL_KHR_shader_subgroup_shuffle"); |
| 7321 |
break; |
| 7322 |
|
| 7323 |
case OpGroupNonUniformShuffleUp: |
| 7324 |
case OpGroupNonUniformShuffleDown: |
| 7325 |
require_extension_internal("GL_KHR_shader_subgroup_shuffle_relative"); |
| 7326 |
break; |
| 7327 |
|
| 7328 |
case OpGroupNonUniformAll: |
| 7329 |
case OpGroupNonUniformAny: |
| 7330 |
case OpGroupNonUniformAllEqual: |
| 7331 |
{ |
| 7332 |
const SPIRType &type = expression_type(ops[3]); |
| 7333 |
if (type.basetype == SPIRType::BaseType::Boolean && type.vecsize == 1u) |
| 7334 |
request_subgroup_feature(ShaderSubgroupSupportHelper::SubgroupAll_Any_AllEqualBool); |
| 7335 |
else |
| 7336 |
request_subgroup_feature(ShaderSubgroupSupportHelper::SubgroupAllEqualT); |
| 7337 |
} |
| 7338 |
break; |
| 7339 |
|
| 7340 |
case OpGroupNonUniformFAdd: |
| 7341 |
case OpGroupNonUniformFMul: |
| 7342 |
case OpGroupNonUniformFMin: |
| 7343 |
case OpGroupNonUniformFMax: |
| 7344 |
case OpGroupNonUniformIAdd: |
| 7345 |
case OpGroupNonUniformIMul: |
| 7346 |
case OpGroupNonUniformSMin: |
| 7347 |
case OpGroupNonUniformSMax: |
| 7348 |
case OpGroupNonUniformUMin: |
| 7349 |
case OpGroupNonUniformUMax: |
| 7350 |
case OpGroupNonUniformBitwiseAnd: |
| 7351 |
case OpGroupNonUniformBitwiseOr: |
| 7352 |
case OpGroupNonUniformBitwiseXor: |
| 7353 |
{ |
| 7354 |
auto operation = static_cast<GroupOperation>(ops[3]); |
| 7355 |
if (operation == GroupOperationClusteredReduce) |
| 7356 |
{ |
| 7357 |
require_extension_internal("GL_KHR_shader_subgroup_clustered"); |
| 7358 |
} |
| 7359 |
else if (operation == GroupOperationExclusiveScan || operation == GroupOperationInclusiveScan || |
| 7360 |
operation == GroupOperationReduce) |
| 7361 |
{ |
| 7362 |
require_extension_internal("GL_KHR_shader_subgroup_arithmetic"); |
| 7363 |
} |
| 7364 |
else |
| 7365 |
SPIRV_CROSS_THROW("Invalid group operation."); |
| 7366 |
break; |
| 7367 |
} |
| 7368 |
|
| 7369 |
case OpGroupNonUniformQuadSwap: |
| 7370 |
case OpGroupNonUniformQuadBroadcast: |
| 7371 |
require_extension_internal("GL_KHR_shader_subgroup_quad"); |
| 7372 |
break; |
| 7373 |
|
| 7374 |
default: |
| 7375 |
SPIRV_CROSS_THROW("Invalid opcode for subgroup."); |
| 7376 |
} |
| 7377 |
|
| 7378 |
uint32_t result_type = ops[0]; |
| 7379 |
uint32_t id = ops[1]; |
| 7380 |
|
| 7381 |
auto scope = static_cast<Scope>(evaluate_constant_u32(ops[2])); |
| 7382 |
if (scope != ScopeSubgroup) |
| 7383 |
SPIRV_CROSS_THROW("Only subgroup scope is supported."); |
| 7384 |
|
| 7385 |
switch (op) |
| 7386 |
{ |
| 7387 |
case OpGroupNonUniformElect: |
| 7388 |
emit_op(result_type, id, "subgroupElect()", true); |
| 7389 |
break; |
| 7390 |
|
| 7391 |
case OpGroupNonUniformBroadcast: |
| 7392 |
emit_binary_func_op(result_type, id, ops[3], ops[4], "subgroupBroadcast"); |
| 7393 |
break; |
| 7394 |
|
| 7395 |
case OpGroupNonUniformBroadcastFirst: |
| 7396 |
emit_unary_func_op(result_type, id, ops[3], "subgroupBroadcastFirst"); |
| 7397 |
break; |
| 7398 |
|
| 7399 |
case OpGroupNonUniformBallot: |
| 7400 |
emit_unary_func_op(result_type, id, ops[3], "subgroupBallot"); |
| 7401 |
break; |
| 7402 |
|
| 7403 |
case OpGroupNonUniformInverseBallot: |
| 7404 |
emit_unary_func_op(result_type, id, ops[3], "subgroupInverseBallot"); |
| 7405 |
break; |
| 7406 |
|
| 7407 |
case OpGroupNonUniformBallotBitExtract: |
| 7408 |
emit_binary_func_op(result_type, id, ops[3], ops[4], "subgroupBallotBitExtract"); |
| 7409 |
break; |
| 7410 |
|
| 7411 |
case OpGroupNonUniformBallotFindLSB: |
| 7412 |
emit_unary_func_op(result_type, id, ops[3], "subgroupBallotFindLSB"); |
| 7413 |
break; |
| 7414 |
|
| 7415 |
case OpGroupNonUniformBallotFindMSB: |
| 7416 |
emit_unary_func_op(result_type, id, ops[3], "subgroupBallotFindMSB"); |
| 7417 |
break; |
| 7418 |
|
| 7419 |
case OpGroupNonUniformBallotBitCount: |
| 7420 |
{ |
| 7421 |
auto operation = static_cast<GroupOperation>(ops[3]); |
| 7422 |
if (operation == GroupOperationReduce) |
| 7423 |
emit_unary_func_op(result_type, id, ops[4], "subgroupBallotBitCount"); |
| 7424 |
else if (operation == GroupOperationInclusiveScan) |
| 7425 |
emit_unary_func_op(result_type, id, ops[4], "subgroupBallotInclusiveBitCount"); |
| 7426 |
else if (operation == GroupOperationExclusiveScan) |
| 7427 |
emit_unary_func_op(result_type, id, ops[4], "subgroupBallotExclusiveBitCount"); |
| 7428 |
else |
| 7429 |
SPIRV_CROSS_THROW("Invalid BitCount operation."); |
| 7430 |
break; |
| 7431 |
} |
| 7432 |
|
| 7433 |
case OpGroupNonUniformShuffle: |
| 7434 |
emit_binary_func_op(result_type, id, ops[3], ops[4], "subgroupShuffle"); |
| 7435 |
break; |
| 7436 |
|
| 7437 |
case OpGroupNonUniformShuffleXor: |
| 7438 |
emit_binary_func_op(result_type, id, ops[3], ops[4], "subgroupShuffleXor"); |
| 7439 |
break; |
| 7440 |
|
| 7441 |
case OpGroupNonUniformShuffleUp: |
| 7442 |
emit_binary_func_op(result_type, id, ops[3], ops[4], "subgroupShuffleUp"); |
| 7443 |
break; |
| 7444 |
|
| 7445 |
case OpGroupNonUniformShuffleDown: |
| 7446 |
emit_binary_func_op(result_type, id, ops[3], ops[4], "subgroupShuffleDown"); |
| 7447 |
break; |
| 7448 |
|
| 7449 |
case OpGroupNonUniformAll: |
| 7450 |
emit_unary_func_op(result_type, id, ops[3], "subgroupAll"); |
| 7451 |
break; |
| 7452 |
|
| 7453 |
case OpGroupNonUniformAny: |
| 7454 |
emit_unary_func_op(result_type, id, ops[3], "subgroupAny"); |
| 7455 |
break; |
| 7456 |
|
| 7457 |
case OpGroupNonUniformAllEqual: |
| 7458 |
emit_unary_func_op(result_type, id, ops[3], "subgroupAllEqual"); |
| 7459 |
break; |
| 7460 |
|
| 7461 |
// clang-format off |
| 7462 |
#define GLSL_GROUP_OP(op, glsl_op) \ |
| 7463 |
case OpGroupNonUniform##op: \ |
| 7464 |
{ \ |
| 7465 |
auto operation = static_cast<GroupOperation>(ops[3]); \ |
| 7466 |
if (operation == GroupOperationReduce) \ |
| 7467 |
emit_unary_func_op(result_type, id, ops[4], "subgroup" #glsl_op); \ |
| 7468 |
else if (operation == GroupOperationInclusiveScan) \ |
| 7469 |
emit_unary_func_op(result_type, id, ops[4], "subgroupInclusive" #glsl_op); \ |
| 7470 |
else if (operation == GroupOperationExclusiveScan) \ |
| 7471 |
emit_unary_func_op(result_type, id, ops[4], "subgroupExclusive" #glsl_op); \ |
| 7472 |
else if (operation == GroupOperationClusteredReduce) \ |
| 7473 |
emit_binary_func_op(result_type, id, ops[4], ops[5], "subgroupClustered" #glsl_op); \ |
| 7474 |
else \ |
| 7475 |
SPIRV_CROSS_THROW("Invalid group operation."); \ |
| 7476 |
break; \ |
| 7477 |
} |
| 7478 |
|
| 7479 |
#define GLSL_GROUP_OP_CAST(op, glsl_op, type) \ |
| 7480 |
case OpGroupNonUniform##op: \ |
| 7481 |
{ \ |
| 7482 |
auto operation = static_cast<GroupOperation>(ops[3]); \ |
| 7483 |
if (operation == GroupOperationReduce) \ |
| 7484 |
emit_unary_func_op_cast(result_type, id, ops[4], "subgroup" #glsl_op, type, type); \ |
| 7485 |
else if (operation == GroupOperationInclusiveScan) \ |
| 7486 |
emit_unary_func_op_cast(result_type, id, ops[4], "subgroupInclusive" #glsl_op, type, type); \ |
| 7487 |
else if (operation == GroupOperationExclusiveScan) \ |
| 7488 |
emit_unary_func_op_cast(result_type, id, ops[4], "subgroupExclusive" #glsl_op, type, type); \ |
| 7489 |
else if (operation == GroupOperationClusteredReduce) \ |
| 7490 |
emit_binary_func_op_cast_clustered(result_type, id, ops[4], ops[5], "subgroupClustered" #glsl_op, type); \ |
| 7491 |
else \ |
| 7492 |
SPIRV_CROSS_THROW("Invalid group operation."); \ |
| 7493 |
break; \ |
| 7494 |
} |
| 7495 |
|
| 7496 |
GLSL_GROUP_OP(FAdd, Add) |
| 7497 |
GLSL_GROUP_OP(FMul, Mul) |
| 7498 |
GLSL_GROUP_OP(FMin, Min) |
| 7499 |
GLSL_GROUP_OP(FMax, Max) |
| 7500 |
GLSL_GROUP_OP(IAdd, Add) |
| 7501 |
GLSL_GROUP_OP(IMul, Mul) |
| 7502 |
GLSL_GROUP_OP_CAST(SMin, Min, int_type) |
| 7503 |
GLSL_GROUP_OP_CAST(SMax, Max, int_type) |
| 7504 |
GLSL_GROUP_OP_CAST(UMin, Min, uint_type) |
| 7505 |
GLSL_GROUP_OP_CAST(UMax, Max, uint_type) |
| 7506 |
GLSL_GROUP_OP(BitwiseAnd, And) |
| 7507 |
GLSL_GROUP_OP(BitwiseOr, Or) |
| 7508 |
GLSL_GROUP_OP(BitwiseXor, Xor) |
| 7509 |
#undef GLSL_GROUP_OP |
| 7510 |
#undef GLSL_GROUP_OP_CAST |
| 7511 |
// clang-format on |
| 7512 |
|
| 7513 |
case OpGroupNonUniformQuadSwap: |
| 7514 |
{ |
| 7515 |
uint32_t direction = evaluate_constant_u32(ops[4]); |
| 7516 |
if (direction == 0) |
| 7517 |
emit_unary_func_op(result_type, id, ops[3], "subgroupQuadSwapHorizontal"); |
| 7518 |
else if (direction == 1) |
| 7519 |
emit_unary_func_op(result_type, id, ops[3], "subgroupQuadSwapVertical"); |
| 7520 |
else if (direction == 2) |
| 7521 |
emit_unary_func_op(result_type, id, ops[3], "subgroupQuadSwapDiagonal"); |
| 7522 |
else |
| 7523 |
SPIRV_CROSS_THROW("Invalid quad swap direction."); |
| 7524 |
break; |
| 7525 |
} |
| 7526 |
|
| 7527 |
case OpGroupNonUniformQuadBroadcast: |
| 7528 |
{ |
| 7529 |
emit_binary_func_op(result_type, id, ops[3], ops[4], "subgroupQuadBroadcast"); |
| 7530 |
break; |
| 7531 |
} |
| 7532 |
|
| 7533 |
default: |
| 7534 |
SPIRV_CROSS_THROW("Invalid opcode for subgroup."); |
| 7535 |
} |
| 7536 |
|
| 7537 |
register_control_dependent_expression(id); |
| 7538 |
} |
| 7539 |
|
| 7540 |
string CompilerGLSL::bitcast_glsl_op(const SPIRType &out_type, const SPIRType &in_type) |
| 7541 |
{ |
| 7542 |
// OpBitcast can deal with pointers. |
| 7543 |
if (out_type.pointer || in_type.pointer) |
| 7544 |
return type_to_glsl(out_type); |
| 7545 |
|
| 7546 |
if (out_type.basetype == in_type.basetype) |
| 7547 |
return ""; |
| 7548 |
|
| 7549 |
assert(out_type.basetype != SPIRType::Boolean); |
| 7550 |
assert(in_type.basetype != SPIRType::Boolean); |
| 7551 |
|
| 7552 |
bool integral_cast = type_is_integral(out_type) && type_is_integral(in_type); |
| 7553 |
bool same_size_cast = out_type.width == in_type.width; |
| 7554 |
|
| 7555 |
// Trivial bitcast case, casts between integers. |
| 7556 |
if (integral_cast && same_size_cast) |
| 7557 |
return type_to_glsl(out_type); |
| 7558 |
|
| 7559 |
// Catch-all 8-bit arithmetic casts (GL_EXT_shader_explicit_arithmetic_types). |
| 7560 |
if (out_type.width == 8 && in_type.width >= 16 && integral_cast && in_type.vecsize == 1) |
| 7561 |
return "unpack8"; |
| 7562 |
else if (in_type.width == 8 && out_type.width == 16 && integral_cast && out_type.vecsize == 1) |
| 7563 |
return "pack16"; |
| 7564 |
else if (in_type.width == 8 && out_type.width == 32 && integral_cast && out_type.vecsize == 1) |
| 7565 |
return "pack32"; |
| 7566 |
|
| 7567 |
// Floating <-> Integer special casts. Just have to enumerate all cases. :( |
| 7568 |
// 16-bit, 32-bit and 64-bit floats. |
| 7569 |
if (out_type.basetype == SPIRType::UInt && in_type.basetype == SPIRType::Float) |
| 7570 |
{ |
| 7571 |
if (is_legacy_es()) |
| 7572 |
SPIRV_CROSS_THROW("Float -> Uint bitcast not supported on legacy ESSL."); |
| 7573 |
else if (!options.es && options.version < 330) |
| 7574 |
require_extension_internal("GL_ARB_shader_bit_encoding"); |
| 7575 |
return "floatBitsToUint"; |
| 7576 |
} |
| 7577 |
else if (out_type.basetype == SPIRType::Int && in_type.basetype == SPIRType::Float) |
| 7578 |
{ |
| 7579 |
if (is_legacy_es()) |
| 7580 |
SPIRV_CROSS_THROW("Float -> Int bitcast not supported on legacy ESSL."); |
| 7581 |
else if (!options.es && options.version < 330) |
| 7582 |
require_extension_internal("GL_ARB_shader_bit_encoding"); |
| 7583 |
return "floatBitsToInt"; |
| 7584 |
} |
| 7585 |
else if (out_type.basetype == SPIRType::Float && in_type.basetype == SPIRType::UInt) |
| 7586 |
{ |
| 7587 |
if (is_legacy_es()) |
| 7588 |
SPIRV_CROSS_THROW("Uint -> Float bitcast not supported on legacy ESSL."); |
| 7589 |
else if (!options.es && options.version < 330) |
| 7590 |
require_extension_internal("GL_ARB_shader_bit_encoding"); |
| 7591 |
return "uintBitsToFloat"; |
| 7592 |
} |
| 7593 |
else if (out_type.basetype == SPIRType::Float && in_type.basetype == SPIRType::Int) |
| 7594 |
{ |
| 7595 |
if (is_legacy_es()) |
| 7596 |
SPIRV_CROSS_THROW("Int -> Float bitcast not supported on legacy ESSL."); |
| 7597 |
else if (!options.es && options.version < 330) |
| 7598 |
require_extension_internal("GL_ARB_shader_bit_encoding"); |
| 7599 |
return "intBitsToFloat"; |
| 7600 |
} |
| 7601 |
|
| 7602 |
else if (out_type.basetype == SPIRType::Int64 && in_type.basetype == SPIRType::Double) |
| 7603 |
return "doubleBitsToInt64"; |
| 7604 |
else if (out_type.basetype == SPIRType::UInt64 && in_type.basetype == SPIRType::Double) |
| 7605 |
return "doubleBitsToUint64"; |
| 7606 |
else if (out_type.basetype == SPIRType::Double && in_type.basetype == SPIRType::Int64) |
| 7607 |
return "int64BitsToDouble"; |
| 7608 |
else if (out_type.basetype == SPIRType::Double && in_type.basetype == SPIRType::UInt64) |
| 7609 |
return "uint64BitsToDouble"; |
| 7610 |
else if (out_type.basetype == SPIRType::Short && in_type.basetype == SPIRType::Half) |
| 7611 |
return "float16BitsToInt16"; |
| 7612 |
else if (out_type.basetype == SPIRType::UShort && in_type.basetype == SPIRType::Half) |
| 7613 |
return "float16BitsToUint16"; |
| 7614 |
else if (out_type.basetype == SPIRType::Half && in_type.basetype == SPIRType::Short) |
| 7615 |
return "int16BitsToFloat16"; |
| 7616 |
else if (out_type.basetype == SPIRType::Half && in_type.basetype == SPIRType::UShort) |
| 7617 |
return "uint16BitsToFloat16"; |
| 7618 |
|
| 7619 |
// And finally, some even more special purpose casts. |
| 7620 |
if (out_type.basetype == SPIRType::UInt64 && in_type.basetype == SPIRType::UInt && in_type.vecsize == 2) |
| 7621 |
return "packUint2x32"; |
| 7622 |
else if (out_type.basetype == SPIRType::UInt && in_type.basetype == SPIRType::UInt64 && out_type.vecsize == 2) |
| 7623 |
return "unpackUint2x32"; |
| 7624 |
else if (out_type.basetype == SPIRType::Half && in_type.basetype == SPIRType::UInt && in_type.vecsize == 1) |
| 7625 |
return "unpackFloat2x16"; |
| 7626 |
else if (out_type.basetype == SPIRType::UInt && in_type.basetype == SPIRType::Half && in_type.vecsize == 2) |
| 7627 |
return "packFloat2x16"; |
| 7628 |
else if (out_type.basetype == SPIRType::Int && in_type.basetype == SPIRType::Short && in_type.vecsize == 2) |
| 7629 |
return "packInt2x16"; |
| 7630 |
else if (out_type.basetype == SPIRType::Short && in_type.basetype == SPIRType::Int && in_type.vecsize == 1) |
| 7631 |
return "unpackInt2x16"; |
| 7632 |
else if (out_type.basetype == SPIRType::UInt && in_type.basetype == SPIRType::UShort && in_type.vecsize == 2) |
| 7633 |
return "packUint2x16"; |
| 7634 |
else if (out_type.basetype == SPIRType::UShort && in_type.basetype == SPIRType::UInt && in_type.vecsize == 1) |
| 7635 |
return "unpackUint2x16"; |
| 7636 |
else if (out_type.basetype == SPIRType::Int64 && in_type.basetype == SPIRType::Short && in_type.vecsize == 4) |
| 7637 |
return "packInt4x16"; |
| 7638 |
else if (out_type.basetype == SPIRType::Short && in_type.basetype == SPIRType::Int64 && in_type.vecsize == 1) |
| 7639 |
return "unpackInt4x16"; |
| 7640 |
else if (out_type.basetype == SPIRType::UInt64 && in_type.basetype == SPIRType::UShort && in_type.vecsize == 4) |
| 7641 |
return "packUint4x16"; |
| 7642 |
else if (out_type.basetype == SPIRType::UShort && in_type.basetype == SPIRType::UInt64 && in_type.vecsize == 1) |
| 7643 |
return "unpackUint4x16"; |
| 7644 |
|
| 7645 |
return ""; |
| 7646 |
} |
| 7647 |
|
| 7648 |
string CompilerGLSL::bitcast_glsl(const SPIRType &result_type, uint32_t argument) |
| 7649 |
{ |
| 7650 |
auto op = bitcast_glsl_op(result_type, expression_type(argument)); |
| 7651 |
if (op.empty()) |
| 7652 |
return to_enclosed_unpacked_expression(argument); |
| 7653 |
else |
| 7654 |
return join(op, "(", to_unpacked_expression(argument), ")"); |
| 7655 |
} |
| 7656 |
|
| 7657 |
std::string CompilerGLSL::bitcast_expression(SPIRType::BaseType target_type, uint32_t arg) |
| 7658 |
{ |
| 7659 |
auto expr = to_expression(arg); |
| 7660 |
auto &src_type = expression_type(arg); |
| 7661 |
if (src_type.basetype != target_type) |
| 7662 |
{ |
| 7663 |
auto target = src_type; |
| 7664 |
target.basetype = target_type; |
| 7665 |
expr = join(bitcast_glsl_op(target, src_type), "(", expr, ")"); |
| 7666 |
} |
| 7667 |
|
| 7668 |
return expr; |
| 7669 |
} |
| 7670 |
|
| 7671 |
std::string CompilerGLSL::bitcast_expression(const SPIRType &target_type, SPIRType::BaseType expr_type, |
| 7672 |
const std::string &expr) |
| 7673 |
{ |
| 7674 |
if (target_type.basetype == expr_type) |
| 7675 |
return expr; |
| 7676 |
|
| 7677 |
auto src_type = target_type; |
| 7678 |
src_type.basetype = expr_type; |
| 7679 |
return join(bitcast_glsl_op(target_type, src_type), "(", expr, ")"); |
| 7680 |
} |
| 7681 |
|
| 7682 |
string CompilerGLSL::builtin_to_glsl(BuiltIn builtin, StorageClass storage) |
| 7683 |
{ |
| 7684 |
switch (builtin) |
| 7685 |
{ |
| 7686 |
case BuiltInPosition: |
| 7687 |
return "gl_Position"; |
| 7688 |
case BuiltInPointSize: |
| 7689 |
return "gl_PointSize"; |
| 7690 |
case BuiltInClipDistance: |
| 7691 |
return "gl_ClipDistance"; |
| 7692 |
case BuiltInCullDistance: |
| 7693 |
return "gl_CullDistance"; |
| 7694 |
case BuiltInVertexId: |
| 7695 |
if (options.vulkan_semantics) |
| 7696 |
SPIRV_CROSS_THROW("Cannot implement gl_VertexID in Vulkan GLSL. This shader was created " |
| 7697 |
"with GL semantics."); |
| 7698 |
return "gl_VertexID"; |
| 7699 |
case BuiltInInstanceId: |
| 7700 |
if (options.vulkan_semantics) |
| 7701 |
{ |
| 7702 |
auto model = get_entry_point().model; |
| 7703 |
switch (model) |
| 7704 |
{ |
| 7705 |
case spv::ExecutionModelIntersectionKHR: |
| 7706 |
case spv::ExecutionModelAnyHitKHR: |
| 7707 |
case spv::ExecutionModelClosestHitKHR: |
| 7708 |
// gl_InstanceID is allowed in these shaders. |
| 7709 |
break; |
| 7710 |
|
| 7711 |
default: |
| 7712 |
SPIRV_CROSS_THROW("Cannot implement gl_InstanceID in Vulkan GLSL. This shader was " |
| 7713 |
"created with GL semantics."); |
| 7714 |
} |
| 7715 |
} |
| 7716 |
if (!options.es && options.version < 140) |
| 7717 |
{ |
| 7718 |
require_extension_internal("GL_ARB_draw_instanced"); |
| 7719 |
} |
| 7720 |
return "gl_InstanceID"; |
| 7721 |
case BuiltInVertexIndex: |
| 7722 |
if (options.vulkan_semantics) |
| 7723 |
return "gl_VertexIndex"; |
| 7724 |
else |
| 7725 |
return "gl_VertexID"; // gl_VertexID already has the base offset applied. |
| 7726 |
case BuiltInInstanceIndex: |
| 7727 |
if (options.vulkan_semantics) |
| 7728 |
return "gl_InstanceIndex"; |
| 7729 |
|
| 7730 |
if (!options.es && options.version < 140) |
| 7731 |
{ |
| 7732 |
require_extension_internal("GL_ARB_draw_instanced"); |
| 7733 |
} |
| 7734 |
|
| 7735 |
if (options.vertex.support_nonzero_base_instance) |
| 7736 |
{ |
| 7737 |
if (!options.vulkan_semantics) |
| 7738 |
{ |
| 7739 |
// This is a soft-enable. We will opt-in to using gl_BaseInstanceARB if supported. |
| 7740 |
require_extension_internal("GL_ARB_shader_draw_parameters"); |
| 7741 |
} |
| 7742 |
return "(gl_InstanceID + SPIRV_Cross_BaseInstance)"; // ... but not gl_InstanceID. |
| 7743 |
} |
| 7744 |
else |
| 7745 |
return "gl_InstanceID"; |
| 7746 |
case BuiltInPrimitiveId: |
| 7747 |
if (storage == StorageClassInput && get_entry_point().model == ExecutionModelGeometry) |
| 7748 |
return "gl_PrimitiveIDIn"; |
| 7749 |
else |
| 7750 |
return "gl_PrimitiveID"; |
| 7751 |
case BuiltInInvocationId: |
| 7752 |
return "gl_InvocationID"; |
| 7753 |
case BuiltInLayer: |
| 7754 |
return "gl_Layer"; |
| 7755 |
case BuiltInViewportIndex: |
| 7756 |
return "gl_ViewportIndex"; |
| 7757 |
case BuiltInTessLevelOuter: |
| 7758 |
return "gl_TessLevelOuter"; |
| 7759 |
case BuiltInTessLevelInner: |
| 7760 |
return "gl_TessLevelInner"; |
| 7761 |
case BuiltInTessCoord: |
| 7762 |
return "gl_TessCoord"; |
| 7763 |
case BuiltInFragCoord: |
| 7764 |
return "gl_FragCoord"; |
| 7765 |
case BuiltInPointCoord: |
| 7766 |
return "gl_PointCoord"; |
| 7767 |
case BuiltInFrontFacing: |
| 7768 |
return "gl_FrontFacing"; |
| 7769 |
case BuiltInFragDepth: |
| 7770 |
return "gl_FragDepth"; |
| 7771 |
case BuiltInNumWorkgroups: |
| 7772 |
return "gl_NumWorkGroups"; |
| 7773 |
case BuiltInWorkgroupSize: |
| 7774 |
return "gl_WorkGroupSize"; |
| 7775 |
case BuiltInWorkgroupId: |
| 7776 |
return "gl_WorkGroupID"; |
| 7777 |
case BuiltInLocalInvocationId: |
| 7778 |
return "gl_LocalInvocationID"; |
| 7779 |
case BuiltInGlobalInvocationId: |
| 7780 |
return "gl_GlobalInvocationID"; |
| 7781 |
case BuiltInLocalInvocationIndex: |
| 7782 |
return "gl_LocalInvocationIndex"; |
| 7783 |
case BuiltInHelperInvocation: |
| 7784 |
return "gl_HelperInvocation"; |
| 7785 |
|
| 7786 |
case BuiltInBaseVertex: |
| 7787 |
if (options.es) |
| 7788 |
SPIRV_CROSS_THROW("BaseVertex not supported in ES profile."); |
| 7789 |
|
| 7790 |
if (options.vulkan_semantics) |
| 7791 |
{ |
| 7792 |
if (options.version < 460) |
| 7793 |
{ |
| 7794 |
require_extension_internal("GL_ARB_shader_draw_parameters"); |
| 7795 |
return "gl_BaseVertexARB"; |
| 7796 |
} |
| 7797 |
return "gl_BaseVertex"; |
| 7798 |
} |
| 7799 |
else |
| 7800 |
{ |
| 7801 |
// On regular GL, this is soft-enabled and we emit ifdefs in code. |
| 7802 |
require_extension_internal("GL_ARB_shader_draw_parameters"); |
| 7803 |
return "SPIRV_Cross_BaseVertex"; |
| 7804 |
} |
| 7805 |
break; |
| 7806 |
|
| 7807 |
case BuiltInBaseInstance: |
| 7808 |
if (options.es) |
| 7809 |
SPIRV_CROSS_THROW("BaseInstance not supported in ES profile."); |
| 7810 |
|
| 7811 |
if (options.vulkan_semantics) |
| 7812 |
{ |
| 7813 |
if (options.version < 460) |
| 7814 |
{ |
| 7815 |
require_extension_internal("GL_ARB_shader_draw_parameters"); |
| 7816 |
return "gl_BaseInstanceARB"; |
| 7817 |
} |
| 7818 |
return "gl_BaseInstance"; |
| 7819 |
} |
| 7820 |
else |
| 7821 |
{ |
| 7822 |
// On regular GL, this is soft-enabled and we emit ifdefs in code. |
| 7823 |
require_extension_internal("GL_ARB_shader_draw_parameters"); |
| 7824 |
return "SPIRV_Cross_BaseInstance"; |
| 7825 |
} |
| 7826 |
break; |
| 7827 |
|
| 7828 |
case BuiltInDrawIndex: |
| 7829 |
if (options.es) |
| 7830 |
SPIRV_CROSS_THROW("DrawIndex not supported in ES profile."); |
| 7831 |
|
| 7832 |
if (options.vulkan_semantics) |
| 7833 |
{ |
| 7834 |
if (options.version < 460) |
| 7835 |
{ |
| 7836 |
require_extension_internal("GL_ARB_shader_draw_parameters"); |
| 7837 |
return "gl_DrawIDARB"; |
| 7838 |
} |
| 7839 |
return "gl_DrawID"; |
| 7840 |
} |
| 7841 |
else |
| 7842 |
{ |
| 7843 |
// On regular GL, this is soft-enabled and we emit ifdefs in code. |
| 7844 |
require_extension_internal("GL_ARB_shader_draw_parameters"); |
| 7845 |
return "gl_DrawIDARB"; |
| 7846 |
} |
| 7847 |
break; |
| 7848 |
|
| 7849 |
case BuiltInSampleId: |
| 7850 |
if (options.es && options.version < 320) |
| 7851 |
require_extension_internal("GL_OES_sample_variables"); |
| 7852 |
if (!options.es && options.version < 400) |
| 7853 |
SPIRV_CROSS_THROW("gl_SampleID not supported before GLSL 400."); |
| 7854 |
return "gl_SampleID"; |
| 7855 |
|
| 7856 |
case BuiltInSampleMask: |
| 7857 |
if (options.es && options.version < 320) |
| 7858 |
require_extension_internal("GL_OES_sample_variables"); |
| 7859 |
if (!options.es && options.version < 400) |
| 7860 |
SPIRV_CROSS_THROW("gl_SampleMask/gl_SampleMaskIn not supported before GLSL 400."); |
| 7861 |
|
| 7862 |
if (storage == StorageClassInput) |
| 7863 |
return "gl_SampleMaskIn"; |
| 7864 |
else |
| 7865 |
return "gl_SampleMask"; |
| 7866 |
|
| 7867 |
case BuiltInSamplePosition: |
| 7868 |
if (options.es && options.version < 320) |
| 7869 |
require_extension_internal("GL_OES_sample_variables"); |
| 7870 |
if (!options.es && options.version < 400) |
| 7871 |
SPIRV_CROSS_THROW("gl_SamplePosition not supported before GLSL 400."); |
| 7872 |
return "gl_SamplePosition"; |
| 7873 |
|
| 7874 |
case BuiltInViewIndex: |
| 7875 |
if (options.vulkan_semantics) |
| 7876 |
{ |
| 7877 |
require_extension_internal("GL_EXT_multiview"); |
| 7878 |
return "gl_ViewIndex"; |
| 7879 |
} |
| 7880 |
else |
| 7881 |
{ |
| 7882 |
require_extension_internal("GL_OVR_multiview2"); |
| 7883 |
return "gl_ViewID_OVR"; |
| 7884 |
} |
| 7885 |
|
| 7886 |
case BuiltInNumSubgroups: |
| 7887 |
request_subgroup_feature(ShaderSubgroupSupportHelper::NumSubgroups); |
| 7888 |
return "gl_NumSubgroups"; |
| 7889 |
|
| 7890 |
case BuiltInSubgroupId: |
| 7891 |
request_subgroup_feature(ShaderSubgroupSupportHelper::SubgroupID); |
| 7892 |
return "gl_SubgroupID"; |
| 7893 |
|
| 7894 |
case BuiltInSubgroupSize: |
| 7895 |
request_subgroup_feature(ShaderSubgroupSupportHelper::SubgroupSize); |
| 7896 |
return "gl_SubgroupSize"; |
| 7897 |
|
| 7898 |
case BuiltInSubgroupLocalInvocationId: |
| 7899 |
request_subgroup_feature(ShaderSubgroupSupportHelper::SubgroupInvocationID); |
| 7900 |
return "gl_SubgroupInvocationID"; |
| 7901 |
|
| 7902 |
case BuiltInSubgroupEqMask: |
| 7903 |
request_subgroup_feature(ShaderSubgroupSupportHelper::SubgroupMask); |
| 7904 |
return "gl_SubgroupEqMask"; |
| 7905 |
|
| 7906 |
case BuiltInSubgroupGeMask: |
| 7907 |
request_subgroup_feature(ShaderSubgroupSupportHelper::SubgroupMask); |
| 7908 |
return "gl_SubgroupGeMask"; |
| 7909 |
|
| 7910 |
case BuiltInSubgroupGtMask: |
| 7911 |
request_subgroup_feature(ShaderSubgroupSupportHelper::SubgroupMask); |
| 7912 |
return "gl_SubgroupGtMask"; |
| 7913 |
|
| 7914 |
case BuiltInSubgroupLeMask: |
| 7915 |
request_subgroup_feature(ShaderSubgroupSupportHelper::SubgroupMask); |
| 7916 |
return "gl_SubgroupLeMask"; |
| 7917 |
|
| 7918 |
case BuiltInSubgroupLtMask: |
| 7919 |
request_subgroup_feature(ShaderSubgroupSupportHelper::SubgroupMask); |
| 7920 |
return "gl_SubgroupLtMask"; |
| 7921 |
|
| 7922 |
case BuiltInLaunchIdNV: |
| 7923 |
return "gl_LaunchIDNV"; |
| 7924 |
case BuiltInLaunchSizeNV: |
| 7925 |
return "gl_LaunchSizeNV"; |
| 7926 |
case BuiltInWorldRayOriginNV: |
| 7927 |
return "gl_WorldRayOriginNV"; |
| 7928 |
case BuiltInWorldRayDirectionNV: |
| 7929 |
return "gl_WorldRayDirectionNV"; |
| 7930 |
case BuiltInObjectRayOriginNV: |
| 7931 |
return "gl_ObjectRayOriginNV"; |
| 7932 |
case BuiltInObjectRayDirectionNV: |
| 7933 |
return "gl_ObjectRayDirectionNV"; |
| 7934 |
case BuiltInRayTminNV: |
| 7935 |
return "gl_RayTminNV"; |
| 7936 |
case BuiltInRayTmaxNV: |
| 7937 |
return "gl_RayTmaxNV"; |
| 7938 |
case BuiltInInstanceCustomIndexNV: |
| 7939 |
return "gl_InstanceCustomIndexNV"; |
| 7940 |
case BuiltInObjectToWorldNV: |
| 7941 |
return "gl_ObjectToWorldNV"; |
| 7942 |
case BuiltInWorldToObjectNV: |
| 7943 |
return "gl_WorldToObjectNV"; |
| 7944 |
case BuiltInHitTNV: |
| 7945 |
return "gl_HitTNV"; |
| 7946 |
case BuiltInHitKindNV: |
| 7947 |
return "gl_HitKindNV"; |
| 7948 |
case BuiltInIncomingRayFlagsNV: |
| 7949 |
return "gl_IncomingRayFlagsNV"; |
| 7950 |
|
| 7951 |
case BuiltInBaryCoordNV: |
| 7952 |
{ |
| 7953 |
if (options.es && options.version < 320) |
| 7954 |
SPIRV_CROSS_THROW("gl_BaryCoordNV requires ESSL 320."); |
| 7955 |
else if (!options.es && options.version < 450) |
| 7956 |
SPIRV_CROSS_THROW("gl_BaryCoordNV requires GLSL 450."); |
| 7957 |
require_extension_internal("GL_NV_fragment_shader_barycentric"); |
| 7958 |
return "gl_BaryCoordNV"; |
| 7959 |
} |
| 7960 |
|
| 7961 |
case BuiltInBaryCoordNoPerspNV: |
| 7962 |
{ |
| 7963 |
if (options.es && options.version < 320) |
| 7964 |
SPIRV_CROSS_THROW("gl_BaryCoordNoPerspNV requires ESSL 320."); |
| 7965 |
else if (!options.es && options.version < 450) |
| 7966 |
SPIRV_CROSS_THROW("gl_BaryCoordNoPerspNV requires GLSL 450."); |
| 7967 |
require_extension_internal("GL_NV_fragment_shader_barycentric"); |
| 7968 |
return "gl_BaryCoordNoPerspNV"; |
| 7969 |
} |
| 7970 |
|
| 7971 |
case BuiltInFragStencilRefEXT: |
| 7972 |
{ |
| 7973 |
if (!options.es) |
| 7974 |
{ |
| 7975 |
require_extension_internal("GL_ARB_shader_stencil_export"); |
| 7976 |
return "gl_FragStencilRefARB"; |
| 7977 |
} |
| 7978 |
else |
| 7979 |
SPIRV_CROSS_THROW("Stencil export not supported in GLES."); |
| 7980 |
} |
| 7981 |
|
| 7982 |
case BuiltInDeviceIndex: |
| 7983 |
if (!options.vulkan_semantics) |
| 7984 |
SPIRV_CROSS_THROW("Need Vulkan semantics for device group support."); |
| 7985 |
require_extension_internal("GL_EXT_device_group"); |
| 7986 |
return "gl_DeviceIndex"; |
| 7987 |
|
| 7988 |
default: |
| 7989 |
return join("gl_BuiltIn_", convert_to_string(builtin)); |
| 7990 |
} |
| 7991 |
} |
| 7992 |
|
| 7993 |
const char *CompilerGLSL::index_to_swizzle(uint32_t index) |
| 7994 |
{ |
| 7995 |
switch (index) |
| 7996 |
{ |
| 7997 |
case 0: |
| 7998 |
return "x"; |
| 7999 |
case 1: |
| 8000 |
return "y"; |
| 8001 |
case 2: |
| 8002 |
return "z"; |
| 8003 |
case 3: |
| 8004 |
return "w"; |
| 8005 |
default: |
| 8006 |
SPIRV_CROSS_THROW("Swizzle index out of range"); |
| 8007 |
} |
| 8008 |
} |
| 8009 |
|
| 8010 |
void CompilerGLSL::access_chain_internal_append_index(std::string &expr, uint32_t /*base*/, const SPIRType *type, |
| 8011 |
AccessChainFlags flags, bool & /*access_chain_is_arrayed*/, |
| 8012 |
uint32_t index) |
| 8013 |
{ |
| 8014 |
bool index_is_literal = (flags & ACCESS_CHAIN_INDEX_IS_LITERAL_BIT) != 0; |
| 8015 |
bool register_expression_read = (flags & ACCESS_CHAIN_SKIP_REGISTER_EXPRESSION_READ_BIT) == 0; |
| 8016 |
|
| 8017 |
expr += "["; |
| 8018 |
|
| 8019 |
// If we are indexing into an array of SSBOs or UBOs, we need to index it with a non-uniform qualifier. |
| 8020 |
bool nonuniform_index = |
| 8021 |
has_decoration(index, DecorationNonUniformEXT) && |
| 8022 |
(has_decoration(type->self, DecorationBlock) || has_decoration(type->self, DecorationBufferBlock)); |
| 8023 |
if (nonuniform_index) |
| 8024 |
{ |
| 8025 |
expr += backend.nonuniform_qualifier; |
| 8026 |
expr += "("; |
| 8027 |
} |
| 8028 |
|
| 8029 |
if (index_is_literal) |
| 8030 |
expr += convert_to_string(index); |
| 8031 |
else |
| 8032 |
expr += to_expression(index, register_expression_read); |
| 8033 |
|
| 8034 |
if (nonuniform_index) |
| 8035 |
expr += ")"; |
| 8036 |
|
| 8037 |
expr += "]"; |
| 8038 |
} |
| 8039 |
|
| 8040 |
string CompilerGLSL::access_chain_internal(uint32_t base, const uint32_t *indices, uint32_t count, |
| 8041 |
AccessChainFlags flags, AccessChainMeta *meta) |
| 8042 |
{ |
| 8043 |
string expr; |
| 8044 |
|
| 8045 |
bool index_is_literal = (flags & ACCESS_CHAIN_INDEX_IS_LITERAL_BIT) != 0; |
| 8046 |
bool msb_is_id = (flags & ACCESS_CHAIN_LITERAL_MSB_FORCE_ID) != 0; |
| 8047 |
bool chain_only = (flags & ACCESS_CHAIN_CHAIN_ONLY_BIT) != 0; |
| 8048 |
bool ptr_chain = (flags & ACCESS_CHAIN_PTR_CHAIN_BIT) != 0; |
| 8049 |
bool register_expression_read = (flags & ACCESS_CHAIN_SKIP_REGISTER_EXPRESSION_READ_BIT) == 0; |
| 8050 |
bool flatten_member_reference = (flags & ACCESS_CHAIN_FLATTEN_ALL_MEMBERS_BIT) != 0; |
| 8051 |
|
| 8052 |
if (!chain_only) |
| 8053 |
{ |
| 8054 |
// We handle transpose explicitly, so don't resolve that here. |
| 8055 |
auto *e = maybe_get<SPIRExpression>(base); |
| 8056 |
bool old_transpose = e && e->need_transpose; |
| 8057 |
if (e) |
| 8058 |
e->need_transpose = false; |
| 8059 |
expr = to_enclosed_expression(base, register_expression_read); |
| 8060 |
if (e) |
| 8061 |
e->need_transpose = old_transpose; |
| 8062 |
} |
| 8063 |
|
| 8064 |
// Start traversing type hierarchy at the proper non-pointer types, |
| 8065 |
// but keep type_id referencing the original pointer for use below. |
| 8066 |
uint32_t type_id = expression_type_id(base); |
| 8067 |
|
| 8068 |
if (!backend.native_pointers) |
| 8069 |
{ |
| 8070 |
if (ptr_chain) |
| 8071 |
SPIRV_CROSS_THROW("Backend does not support native pointers and does not support OpPtrAccessChain."); |
| 8072 |
|
| 8073 |
// Wrapped buffer reference pointer types will need to poke into the internal "value" member before |
| 8074 |
// continuing the access chain. |
| 8075 |
if (should_dereference(base)) |
| 8076 |
{ |
| 8077 |
auto &type = get<SPIRType>(type_id); |
| 8078 |
expr = dereference_expression(type, expr); |
| 8079 |
} |
| 8080 |
} |
| 8081 |
|
| 8082 |
const auto *type = &get_pointee_type(type_id); |
| 8083 |
|
| 8084 |
bool access_chain_is_arrayed = expr.find_first_of('[') != string::npos; |
| 8085 |
bool row_major_matrix_needs_conversion = is_non_native_row_major_matrix(base); |
| 8086 |
bool is_packed = has_extended_decoration(base, SPIRVCrossDecorationPhysicalTypePacked); |
| 8087 |
uint32_t physical_type = get_extended_decoration(base, SPIRVCrossDecorationPhysicalTypeID); |
| 8088 |
bool is_invariant = has_decoration(base, DecorationInvariant); |
| 8089 |
bool pending_array_enclose = false; |
| 8090 |
bool dimension_flatten = false; |
| 8091 |
|
| 8092 |
const auto append_index = [&](uint32_t index, bool is_literal) { |
| 8093 |
AccessChainFlags mod_flags = flags; |
| 8094 |
if (!is_literal) |
| 8095 |
mod_flags &= ~ACCESS_CHAIN_INDEX_IS_LITERAL_BIT; |
| 8096 |
access_chain_internal_append_index(expr, base, type, mod_flags, access_chain_is_arrayed, index); |
| 8097 |
}; |
| 8098 |
|
| 8099 |
for (uint32_t i = 0; i < count; i++) |
| 8100 |
{ |
| 8101 |
uint32_t index = indices[i]; |
| 8102 |
|
| 8103 |
bool is_literal = index_is_literal; |
| 8104 |
if (is_literal && msb_is_id && (index >> 31u) != 0u) |
| 8105 |
{ |
| 8106 |
is_literal = false; |
| 8107 |
index &= 0x7fffffffu; |
| 8108 |
} |
| 8109 |
|
| 8110 |
// Pointer chains |
| 8111 |
if (ptr_chain && i == 0) |
| 8112 |
{ |
| 8113 |
// If we are flattening multidimensional arrays, only create opening bracket on first |
| 8114 |
// array index. |
| 8115 |
if (options.flatten_multidimensional_arrays) |
| 8116 |
{ |
| 8117 |
dimension_flatten = type->array.size() >= 1; |
| 8118 |
pending_array_enclose = dimension_flatten; |
| 8119 |
if (pending_array_enclose) |
| 8120 |
expr += "["; |
| 8121 |
} |
| 8122 |
|
| 8123 |
if (options.flatten_multidimensional_arrays && dimension_flatten) |
| 8124 |
{ |
| 8125 |
// If we are flattening multidimensional arrays, do manual stride computation. |
| 8126 |
if (is_literal) |
| 8127 |
expr += convert_to_string(index); |
| 8128 |
else |
| 8129 |
expr += to_enclosed_expression(index, register_expression_read); |
| 8130 |
|
| 8131 |
for (auto j = uint32_t(type->array.size()); j; j--) |
| 8132 |
{ |
| 8133 |
expr += " * "; |
| 8134 |
expr += enclose_expression(to_array_size(*type, j - 1)); |
| 8135 |
} |
| 8136 |
|
| 8137 |
if (type->array.empty()) |
| 8138 |
pending_array_enclose = false; |
| 8139 |
else |
| 8140 |
expr += " + "; |
| 8141 |
|
| 8142 |
if (!pending_array_enclose) |
| 8143 |
expr += "]"; |
| 8144 |
} |
| 8145 |
else |
| 8146 |
{ |
| 8147 |
append_index(index, is_literal); |
| 8148 |
} |
| 8149 |
|
| 8150 |
if (type->basetype == SPIRType::ControlPointArray) |
| 8151 |
{ |
| 8152 |
type_id = type->parent_type; |
| 8153 |
type = &get<SPIRType>(type_id); |
| 8154 |
} |
| 8155 |
|
| 8156 |
access_chain_is_arrayed = true; |
| 8157 |
} |
| 8158 |
// Arrays |
| 8159 |
else if (!type->array.empty()) |
| 8160 |
{ |
| 8161 |
// If we are flattening multidimensional arrays, only create opening bracket on first |
| 8162 |
// array index. |
| 8163 |
if (options.flatten_multidimensional_arrays && !pending_array_enclose) |
| 8164 |
{ |
| 8165 |
dimension_flatten = type->array.size() > 1; |
| 8166 |
pending_array_enclose = dimension_flatten; |
| 8167 |
if (pending_array_enclose) |
| 8168 |
expr += "["; |
| 8169 |
} |
| 8170 |
|
| 8171 |
assert(type->parent_type); |
| 8172 |
|
| 8173 |
auto *var = maybe_get<SPIRVariable>(base); |
| 8174 |
if (backend.force_gl_in_out_block && i == 0 && var && is_builtin_variable(*var) && |
| 8175 |
!has_decoration(type->self, DecorationBlock)) |
| 8176 |
{ |
| 8177 |
// This deals with scenarios for tesc/geom where arrays of gl_Position[] are declared. |
| 8178 |
// Normally, these variables live in blocks when compiled from GLSL, |
| 8179 |
// but HLSL seems to just emit straight arrays here. |
| 8180 |
// We must pretend this access goes through gl_in/gl_out arrays |
| 8181 |
// to be able to access certain builtins as arrays. |
| 8182 |
auto builtin = ir.meta[base].decoration.builtin_type; |
| 8183 |
switch (builtin) |
| 8184 |
{ |
| 8185 |
// case BuiltInCullDistance: // These are already arrays, need to figure out rules for these in tess/geom. |
| 8186 |
// case BuiltInClipDistance: |
| 8187 |
case BuiltInPosition: |
| 8188 |
case BuiltInPointSize: |
| 8189 |
if (var->storage == StorageClassInput) |
| 8190 |
expr = join("gl_in[", to_expression(index, register_expression_read), "].", expr); |
| 8191 |
else if (var->storage == StorageClassOutput) |
| 8192 |
expr = join("gl_out[", to_expression(index, register_expression_read), "].", expr); |
| 8193 |
else |
| 8194 |
append_index(index, is_literal); |
| 8195 |
break; |
| 8196 |
|
| 8197 |
default: |
| 8198 |
append_index(index, is_literal); |
| 8199 |
break; |
| 8200 |
} |
| 8201 |
} |
| 8202 |
else if (options.flatten_multidimensional_arrays && dimension_flatten) |
| 8203 |
{ |
| 8204 |
// If we are flattening multidimensional arrays, do manual stride computation. |
| 8205 |
auto &parent_type = get<SPIRType>(type->parent_type); |
| 8206 |
|
| 8207 |
if (is_literal) |
| 8208 |
expr += convert_to_string(index); |
| 8209 |
else |
| 8210 |
expr += to_enclosed_expression(index, register_expression_read); |
| 8211 |
|
| 8212 |
for (auto j = uint32_t(parent_type.array.size()); j; j--) |
| 8213 |
{ |
| 8214 |
expr += " * "; |
| 8215 |
expr += enclose_expression(to_array_size(parent_type, j - 1)); |
| 8216 |
} |
| 8217 |
|
| 8218 |
if (parent_type.array.empty()) |
| 8219 |
pending_array_enclose = false; |
| 8220 |
else |
| 8221 |
expr += " + "; |
| 8222 |
|
| 8223 |
if (!pending_array_enclose) |
| 8224 |
expr += "]"; |
| 8225 |
} |
| 8226 |
// Some builtins are arrays in SPIR-V but not in other languages, e.g. gl_SampleMask[] is an array in SPIR-V but not in Metal. |
| 8227 |
// By throwing away the index, we imply the index was 0, which it must be for gl_SampleMask. |
| 8228 |
else if (!builtin_translates_to_nonarray(BuiltIn(get_decoration(base, DecorationBuiltIn)))) |
| 8229 |
{ |
| 8230 |
append_index(index, is_literal); |
| 8231 |
} |
| 8232 |
|
| 8233 |
type_id = type->parent_type; |
| 8234 |
type = &get<SPIRType>(type_id); |
| 8235 |
|
| 8236 |
access_chain_is_arrayed = true; |
| 8237 |
} |
| 8238 |
// For structs, the index refers to a constant, which indexes into the members. |
| 8239 |
// We also check if this member is a builtin, since we then replace the entire expression with the builtin one. |
| 8240 |
else if (type->basetype == SPIRType::Struct) |
| 8241 |
{ |
| 8242 |
if (!is_literal) |
| 8243 |
index = evaluate_constant_u32(index); |
| 8244 |
|
| 8245 |
if (index >= type->member_types.size()) |
| 8246 |
SPIRV_CROSS_THROW("Member index is out of bounds!"); |
| 8247 |
|
| 8248 |
BuiltIn builtin; |
| 8249 |
if (is_member_builtin(*type, index, &builtin)) |
| 8250 |
{ |
| 8251 |
if (access_chain_is_arrayed) |
| 8252 |
{ |
| 8253 |
expr += "."; |
| 8254 |
expr += builtin_to_glsl(builtin, type->storage); |
| 8255 |
} |
| 8256 |
else |
| 8257 |
expr = builtin_to_glsl(builtin, type->storage); |
| 8258 |
} |
| 8259 |
else |
| 8260 |
{ |
| 8261 |
// If the member has a qualified name, use it as the entire chain |
| 8262 |
string qual_mbr_name = get_member_qualified_name(type_id, index); |
| 8263 |
if (!qual_mbr_name.empty()) |
| 8264 |
expr = qual_mbr_name; |
| 8265 |
else if (flatten_member_reference) |
| 8266 |
expr += join("_", to_member_name(*type, index)); |
| 8267 |
else |
| 8268 |
expr += to_member_reference(base, *type, index, ptr_chain); |
| 8269 |
} |
| 8270 |
|
| 8271 |
if (has_member_decoration(type->self, index, DecorationInvariant)) |
| 8272 |
is_invariant = true; |
| 8273 |
|
| 8274 |
is_packed = member_is_packed_physical_type(*type, index); |
| 8275 |
if (member_is_remapped_physical_type(*type, index)) |
| 8276 |
physical_type = get_extended_member_decoration(type->self, index, SPIRVCrossDecorationPhysicalTypeID); |
| 8277 |
else |
| 8278 |
physical_type = 0; |
| 8279 |
|
| 8280 |
row_major_matrix_needs_conversion = member_is_non_native_row_major_matrix(*type, index); |
| 8281 |
type = &get<SPIRType>(type->member_types[index]); |
| 8282 |
} |
| 8283 |
// Matrix -> Vector |
| 8284 |
else if (type->columns > 1) |
| 8285 |
{ |
| 8286 |
// If we have a row-major matrix here, we need to defer any transpose in case this access chain |
| 8287 |
// is used to store a column. We can resolve it right here and now if we access a scalar directly, |
| 8288 |
// by flipping indexing order of the matrix. |
| 8289 |
|
| 8290 |
expr += "["; |
| 8291 |
if (is_literal) |
| 8292 |
expr += convert_to_string(index); |
| 8293 |
else |
| 8294 |
expr += to_expression(index, register_expression_read); |
| 8295 |
expr += "]"; |
| 8296 |
|
| 8297 |
type_id = type->parent_type; |
| 8298 |
type = &get<SPIRType>(type_id); |
| 8299 |
} |
| 8300 |
// Vector -> Scalar |
| 8301 |
else if (type->vecsize > 1) |
| 8302 |
{ |
| 8303 |
string deferred_index; |
| 8304 |
if (row_major_matrix_needs_conversion) |
| 8305 |
{ |
| 8306 |
// Flip indexing order. |
| 8307 |
auto column_index = expr.find_last_of('['); |
| 8308 |
if (column_index != string::npos) |
| 8309 |
{ |
| 8310 |
deferred_index = expr.substr(column_index); |
| 8311 |
expr.resize(column_index); |
| 8312 |
} |
| 8313 |
} |
| 8314 |
|
| 8315 |
// Internally, access chain implementation can also be used on composites, |
| 8316 |
// ignore scalar access workarounds in this case. |
| 8317 |
StorageClass effective_storage; |
| 8318 |
if (expression_type(base).pointer) |
| 8319 |
effective_storage = get_expression_effective_storage_class(base); |
| 8320 |
else |
| 8321 |
effective_storage = StorageClassGeneric; |
| 8322 |
|
| 8323 |
if (!row_major_matrix_needs_conversion) |
| 8324 |
{ |
| 8325 |
// On some backends, we might not be able to safely access individual scalars in a vector. |
| 8326 |
// To work around this, we might have to cast the access chain reference to something which can, |
| 8327 |
// like a pointer to scalar, which we can then index into. |
| 8328 |
prepare_access_chain_for_scalar_access(expr, get<SPIRType>(type->parent_type), effective_storage, |
| 8329 |
is_packed); |
| 8330 |
} |
| 8331 |
|
| 8332 |
if (is_literal && !is_packed && !row_major_matrix_needs_conversion) |
| 8333 |
{ |
| 8334 |
expr += "."; |
| 8335 |
expr += index_to_swizzle(index); |
| 8336 |
} |
| 8337 |
else if (ir.ids[index].get_type() == TypeConstant && !is_packed && !row_major_matrix_needs_conversion) |
| 8338 |
{ |
| 8339 |
auto &c = get<SPIRConstant>(index); |
| 8340 |
if (c.specialization) |
| 8341 |
{ |
| 8342 |
// If the index is a spec constant, we cannot turn extract into a swizzle. |
| 8343 |
expr += join("[", to_expression(index), "]"); |
| 8344 |
} |
| 8345 |
else |
| 8346 |
{ |
| 8347 |
expr += "."; |
| 8348 |
expr += index_to_swizzle(c.scalar()); |
| 8349 |
} |
| 8350 |
} |
| 8351 |
else if (is_literal) |
| 8352 |
{ |
| 8353 |
// For packed vectors, we can only access them as an array, not by swizzle. |
| 8354 |
expr += join("[", index, "]"); |
| 8355 |
} |
| 8356 |
else |
| 8357 |
{ |
| 8358 |
expr += "["; |
| 8359 |
expr += to_expression(index, register_expression_read); |
| 8360 |
expr += "]"; |
| 8361 |
} |
| 8362 |
|
| 8363 |
if (row_major_matrix_needs_conversion) |
| 8364 |
{ |
| 8365 |
prepare_access_chain_for_scalar_access(expr, get<SPIRType>(type->parent_type), effective_storage, |
| 8366 |
is_packed); |
| 8367 |
} |
| 8368 |
|
| 8369 |
expr += deferred_index; |
| 8370 |
row_major_matrix_needs_conversion = false; |
| 8371 |
|
| 8372 |
is_packed = false; |
| 8373 |
physical_type = 0; |
| 8374 |
type_id = type->parent_type; |
| 8375 |
type = &get<SPIRType>(type_id); |
| 8376 |
} |
| 8377 |
else if (!backend.allow_truncated_access_chain) |
| 8378 |
SPIRV_CROSS_THROW("Cannot subdivide a scalar value!"); |
| 8379 |
} |
| 8380 |
|
| 8381 |
if (pending_array_enclose) |
| 8382 |
{ |
| 8383 |
SPIRV_CROSS_THROW("Flattening of multidimensional arrays were enabled, " |
| 8384 |
"but the access chain was terminated in the middle of a multidimensional array. " |
| 8385 |
"This is not supported."); |
| 8386 |
} |
| 8387 |
|
| 8388 |
if (meta) |
| 8389 |
{ |
| 8390 |
meta->need_transpose = row_major_matrix_needs_conversion; |
| 8391 |
meta->storage_is_packed = is_packed; |
| 8392 |
meta->storage_is_invariant = is_invariant; |
| 8393 |
meta->storage_physical_type = physical_type; |
| 8394 |
} |
| 8395 |
|
| 8396 |
return expr; |
| 8397 |
} |
| 8398 |
|
| 8399 |
void CompilerGLSL::prepare_access_chain_for_scalar_access(std::string &, const SPIRType &, spv::StorageClass, bool &) |
| 8400 |
{ |
| 8401 |
} |
| 8402 |
|
| 8403 |
string CompilerGLSL::to_flattened_struct_member(const string &basename, const SPIRType &type, uint32_t index) |
| 8404 |
{ |
| 8405 |
auto ret = join(basename, "_", to_member_name(type, index)); |
| 8406 |
ParsedIR::sanitize_underscores(ret); |
| 8407 |
return ret; |
| 8408 |
} |
| 8409 |
|
| 8410 |
string CompilerGLSL::access_chain(uint32_t base, const uint32_t *indices, uint32_t count, const SPIRType &target_type, |
| 8411 |
AccessChainMeta *meta, bool ptr_chain) |
| 8412 |
{ |
| 8413 |
if (flattened_buffer_blocks.count(base)) |
| 8414 |
{ |
| 8415 |
uint32_t matrix_stride = 0; |
| 8416 |
uint32_t array_stride = 0; |
| 8417 |
bool need_transpose = false; |
| 8418 |
flattened_access_chain_offset(expression_type(base), indices, count, 0, 16, &need_transpose, &matrix_stride, |
| 8419 |
&array_stride, ptr_chain); |
| 8420 |
|
| 8421 |
if (meta) |
| 8422 |
{ |
| 8423 |
meta->need_transpose = target_type.columns > 1 && need_transpose; |
| 8424 |
meta->storage_is_packed = false; |
| 8425 |
} |
| 8426 |
|
| 8427 |
return flattened_access_chain(base, indices, count, target_type, 0, matrix_stride, array_stride, |
| 8428 |
need_transpose); |
| 8429 |
} |
| 8430 |
else if (flattened_structs.count(base) && count > 0) |
| 8431 |
{ |
| 8432 |
AccessChainFlags flags = ACCESS_CHAIN_CHAIN_ONLY_BIT | ACCESS_CHAIN_SKIP_REGISTER_EXPRESSION_READ_BIT; |
| 8433 |
if (ptr_chain) |
| 8434 |
flags |= ACCESS_CHAIN_PTR_CHAIN_BIT; |
| 8435 |
|
| 8436 |
if (flattened_structs[base]) |
| 8437 |
{ |
| 8438 |
flags |= ACCESS_CHAIN_FLATTEN_ALL_MEMBERS_BIT; |
| 8439 |
if (meta) |
| 8440 |
meta->flattened_struct = target_type.basetype == SPIRType::Struct; |
| 8441 |
} |
| 8442 |
|
| 8443 |
auto chain = access_chain_internal(base, indices, count, flags, nullptr).substr(1); |
| 8444 |
if (meta) |
| 8445 |
{ |
| 8446 |
meta->need_transpose = false; |
| 8447 |
meta->storage_is_packed = false; |
| 8448 |
} |
| 8449 |
|
| 8450 |
auto basename = to_flattened_access_chain_expression(base); |
| 8451 |
auto ret = join(basename, "_", chain); |
| 8452 |
ParsedIR::sanitize_underscores(ret); |
| 8453 |
return ret; |
| 8454 |
} |
| 8455 |
else |
| 8456 |
{ |
| 8457 |
AccessChainFlags flags = ACCESS_CHAIN_SKIP_REGISTER_EXPRESSION_READ_BIT; |
| 8458 |
if (ptr_chain) |
| 8459 |
flags |= ACCESS_CHAIN_PTR_CHAIN_BIT; |
| 8460 |
return access_chain_internal(base, indices, count, flags, meta); |
| 8461 |
} |
| 8462 |
} |
| 8463 |
|
| 8464 |
string CompilerGLSL::load_flattened_struct(const string &basename, const SPIRType &type) |
| 8465 |
{ |
| 8466 |
auto expr = type_to_glsl_constructor(type); |
| 8467 |
expr += '('; |
| 8468 |
|
| 8469 |
for (uint32_t i = 0; i < uint32_t(type.member_types.size()); i++) |
| 8470 |
{ |
| 8471 |
if (i) |
| 8472 |
expr += ", "; |
| 8473 |
|
| 8474 |
auto &member_type = get<SPIRType>(type.member_types[i]); |
| 8475 |
if (member_type.basetype == SPIRType::Struct) |
| 8476 |
expr += load_flattened_struct(to_flattened_struct_member(basename, type, i), member_type); |
| 8477 |
else |
| 8478 |
expr += to_flattened_struct_member(basename, type, i); |
| 8479 |
} |
| 8480 |
expr += ')'; |
| 8481 |
return expr; |
| 8482 |
} |
| 8483 |
|
| 8484 |
std::string CompilerGLSL::to_flattened_access_chain_expression(uint32_t id) |
| 8485 |
{ |
| 8486 |
// Do not use to_expression as that will unflatten access chains. |
| 8487 |
string basename; |
| 8488 |
if (const auto *var = maybe_get<SPIRVariable>(id)) |
| 8489 |
basename = to_name(var->self); |
| 8490 |
else if (const auto *expr = maybe_get<SPIRExpression>(id)) |
| 8491 |
basename = expr->expression; |
| 8492 |
else |
| 8493 |
basename = to_expression(id); |
| 8494 |
|
| 8495 |
return basename; |
| 8496 |
} |
| 8497 |
|
| 8498 |
void CompilerGLSL::store_flattened_struct(const string &basename, uint32_t rhs_id, const SPIRType &type, |
| 8499 |
const SmallVector<uint32_t> &indices) |
| 8500 |
{ |
| 8501 |
SmallVector<uint32_t> sub_indices = indices; |
| 8502 |
sub_indices.push_back(0); |
| 8503 |
|
| 8504 |
auto *member_type = &type; |
| 8505 |
for (auto &index : indices) |
| 8506 |
member_type = &get<SPIRType>(member_type->member_types[index]); |
| 8507 |
|
| 8508 |
for (uint32_t i = 0; i < uint32_t(member_type->member_types.size()); i++) |
| 8509 |
{ |
| 8510 |
sub_indices.back() = i; |
| 8511 |
auto lhs = join(basename, "_", to_member_name(*member_type, i)); |
| 8512 |
ParsedIR::sanitize_underscores(lhs); |
| 8513 |
|
| 8514 |
if (get<SPIRType>(member_type->member_types[i]).basetype == SPIRType::Struct) |
| 8515 |
{ |
| 8516 |
store_flattened_struct(lhs, rhs_id, type, sub_indices); |
| 8517 |
} |
| 8518 |
else |
| 8519 |
{ |
| 8520 |
auto rhs = to_expression(rhs_id) + to_multi_member_reference(type, sub_indices); |
| 8521 |
statement(lhs, " = ", rhs, ";"); |
| 8522 |
} |
| 8523 |
} |
| 8524 |
} |
| 8525 |
|
| 8526 |
void CompilerGLSL::store_flattened_struct(uint32_t lhs_id, uint32_t value) |
| 8527 |
{ |
| 8528 |
auto &type = expression_type(lhs_id); |
| 8529 |
auto basename = to_flattened_access_chain_expression(lhs_id); |
| 8530 |
store_flattened_struct(basename, value, type, {}); |
| 8531 |
} |
| 8532 |
|
| 8533 |
std::string CompilerGLSL::flattened_access_chain(uint32_t base, const uint32_t *indices, uint32_t count, |
| 8534 |
const SPIRType &target_type, uint32_t offset, uint32_t matrix_stride, |
| 8535 |
uint32_t /* array_stride */, bool need_transpose) |
| 8536 |
{ |
| 8537 |
if (!target_type.array.empty()) |
| 8538 |
SPIRV_CROSS_THROW("Access chains that result in an array can not be flattened"); |
| 8539 |
else if (target_type.basetype == SPIRType::Struct) |
| 8540 |
return flattened_access_chain_struct(base, indices, count, target_type, offset); |
| 8541 |
else if (target_type.columns > 1) |
| 8542 |
return flattened_access_chain_matrix(base, indices, count, target_type, offset, matrix_stride, need_transpose); |
| 8543 |
else |
| 8544 |
return flattened_access_chain_vector(base, indices, count, target_type, offset, matrix_stride, need_transpose); |
| 8545 |
} |
| 8546 |
|
| 8547 |
std::string CompilerGLSL::flattened_access_chain_struct(uint32_t base, const uint32_t *indices, uint32_t count, |
| 8548 |
const SPIRType &target_type, uint32_t offset) |
| 8549 |
{ |
| 8550 |
std::string expr; |
| 8551 |
|
| 8552 |
expr += type_to_glsl_constructor(target_type); |
| 8553 |
expr += "("; |
| 8554 |
|
| 8555 |
for (uint32_t i = 0; i < uint32_t(target_type.member_types.size()); ++i) |
| 8556 |
{ |
| 8557 |
if (i != 0) |
| 8558 |
expr += ", "; |
| 8559 |
|
| 8560 |
const SPIRType &member_type = get<SPIRType>(target_type.member_types[i]); |
| 8561 |
uint32_t member_offset = type_struct_member_offset(target_type, i); |
| 8562 |
|
| 8563 |
// The access chain terminates at the struct, so we need to find matrix strides and row-major information |
| 8564 |
// ahead of time. |
| 8565 |
bool need_transpose = false; |
| 8566 |
uint32_t matrix_stride = 0; |
| 8567 |
if (member_type.columns > 1) |
| 8568 |
{ |
| 8569 |
need_transpose = combined_decoration_for_member(target_type, i).get(DecorationRowMajor); |
| 8570 |
matrix_stride = type_struct_member_matrix_stride(target_type, i); |
| 8571 |
} |
| 8572 |
|
| 8573 |
auto tmp = flattened_access_chain(base, indices, count, member_type, offset + member_offset, matrix_stride, |
| 8574 |
0 /* array_stride */, need_transpose); |
| 8575 |
|
| 8576 |
// Cannot forward transpositions, so resolve them here. |
| 8577 |
if (need_transpose) |
| 8578 |
expr += convert_row_major_matrix(tmp, member_type, 0, false); |
| 8579 |
else |
| 8580 |
expr += tmp; |
| 8581 |
} |
| 8582 |
|
| 8583 |
expr += ")"; |
| 8584 |
|
| 8585 |
return expr; |
| 8586 |
} |
| 8587 |
|
| 8588 |
std::string CompilerGLSL::flattened_access_chain_matrix(uint32_t base, const uint32_t *indices, uint32_t count, |
| 8589 |
const SPIRType &target_type, uint32_t offset, |
| 8590 |
uint32_t matrix_stride, bool need_transpose) |
| 8591 |
{ |
| 8592 |
assert(matrix_stride); |
| 8593 |
SPIRType tmp_type = target_type; |
| 8594 |
if (need_transpose) |
| 8595 |
swap(tmp_type.vecsize, tmp_type.columns); |
| 8596 |
|
| 8597 |
std::string expr; |
| 8598 |
|
| 8599 |
expr += type_to_glsl_constructor(tmp_type); |
| 8600 |
expr += "("; |
| 8601 |
|
| 8602 |
for (uint32_t i = 0; i < tmp_type.columns; i++) |
| 8603 |
{ |
| 8604 |
if (i != 0) |
| 8605 |
expr += ", "; |
| 8606 |
|
| 8607 |
expr += flattened_access_chain_vector(base, indices, count, tmp_type, offset + i * matrix_stride, matrix_stride, |
| 8608 |
/* need_transpose= */ false); |
| 8609 |
} |
| 8610 |
|
| 8611 |
expr += ")"; |
| 8612 |
|
| 8613 |
return expr; |
| 8614 |
} |
| 8615 |
|
| 8616 |
std::string CompilerGLSL::flattened_access_chain_vector(uint32_t base, const uint32_t *indices, uint32_t count, |
| 8617 |
const SPIRType &target_type, uint32_t offset, |
| 8618 |
uint32_t matrix_stride, bool need_transpose) |
| 8619 |
{ |
| 8620 |
auto result = flattened_access_chain_offset(expression_type(base), indices, count, offset, 16); |
| 8621 |
|
| 8622 |
auto buffer_name = to_name(expression_type(base).self); |
| 8623 |
|
| 8624 |
if (need_transpose) |
| 8625 |
{ |
| 8626 |
std::string expr; |
| 8627 |
|
| 8628 |
if (target_type.vecsize > 1) |
| 8629 |
{ |
| 8630 |
expr += type_to_glsl_constructor(target_type); |
| 8631 |
expr += "("; |
| 8632 |
} |
| 8633 |
|
| 8634 |
for (uint32_t i = 0; i < target_type.vecsize; ++i) |
| 8635 |
{ |
| 8636 |
if (i != 0) |
| 8637 |
expr += ", "; |
| 8638 |
|
| 8639 |
uint32_t component_offset = result.second + i * matrix_stride; |
| 8640 |
|
| 8641 |
assert(component_offset % (target_type.width / 8) == 0); |
| 8642 |
uint32_t index = component_offset / (target_type.width / 8); |
| 8643 |
|
| 8644 |
expr += buffer_name; |
| 8645 |
expr += "["; |
| 8646 |
expr += result.first; // this is a series of N1 * k1 + N2 * k2 + ... that is either empty or ends with a + |
| 8647 |
expr += convert_to_string(index / 4); |
| 8648 |
expr += "]"; |
| 8649 |
|
| 8650 |
expr += vector_swizzle(1, index % 4); |
| 8651 |
} |
| 8652 |
|
| 8653 |
if (target_type.vecsize > 1) |
| 8654 |
{ |
| 8655 |
expr += ")"; |
| 8656 |
} |
| 8657 |
|
| 8658 |
return expr; |
| 8659 |
} |
| 8660 |
else |
| 8661 |
{ |
| 8662 |
assert(result.second % (target_type.width / 8) == 0); |
| 8663 |
uint32_t index = result.second / (target_type.width / 8); |
| 8664 |
|
| 8665 |
std::string expr; |
| 8666 |
|
| 8667 |
expr += buffer_name; |
| 8668 |
expr += "["; |
| 8669 |
expr += result.first; // this is a series of N1 * k1 + N2 * k2 + ... that is either empty or ends with a + |
| 8670 |
expr += convert_to_string(index / 4); |
| 8671 |
expr += "]"; |
| 8672 |
|
| 8673 |
expr += vector_swizzle(target_type.vecsize, index % 4); |
| 8674 |
|
| 8675 |
return expr; |
| 8676 |
} |
| 8677 |
} |
| 8678 |
|
| 8679 |
std::pair<std::string, uint32_t> CompilerGLSL::flattened_access_chain_offset( |
| 8680 |
const SPIRType &basetype, const uint32_t *indices, uint32_t count, uint32_t offset, uint32_t word_stride, |
| 8681 |
bool *need_transpose, uint32_t *out_matrix_stride, uint32_t *out_array_stride, bool ptr_chain) |
| 8682 |
{ |
| 8683 |
// Start traversing type hierarchy at the proper non-pointer types. |
| 8684 |
const auto *type = &get_pointee_type(basetype); |
| 8685 |
|
| 8686 |
std::string expr; |
| 8687 |
|
| 8688 |
// Inherit matrix information in case we are access chaining a vector which might have come from a row major layout. |
| 8689 |
bool row_major_matrix_needs_conversion = need_transpose ? *need_transpose : false; |
| 8690 |
uint32_t matrix_stride = out_matrix_stride ? *out_matrix_stride : 0; |
| 8691 |
uint32_t array_stride = out_array_stride ? *out_array_stride : 0; |
| 8692 |
|
| 8693 |
for (uint32_t i = 0; i < count; i++) |
| 8694 |
{ |
| 8695 |
uint32_t index = indices[i]; |
| 8696 |
|
| 8697 |
// Pointers |
| 8698 |
if (ptr_chain && i == 0) |
| 8699 |
{ |
| 8700 |
// Here, the pointer type will be decorated with an array stride. |
| 8701 |
array_stride = get_decoration(basetype.self, DecorationArrayStride); |
| 8702 |
if (!array_stride) |
| 8703 |
SPIRV_CROSS_THROW("SPIR-V does not define ArrayStride for buffer block."); |
| 8704 |
|
| 8705 |
auto *constant = maybe_get<SPIRConstant>(index); |
| 8706 |
if (constant) |
| 8707 |
{ |
| 8708 |
// Constant array access. |
| 8709 |
offset += constant->scalar() * array_stride; |
| 8710 |
} |
| 8711 |
else |
| 8712 |
{ |
| 8713 |
// Dynamic array access. |
| 8714 |
if (array_stride % word_stride) |
| 8715 |
{ |
| 8716 |
SPIRV_CROSS_THROW("Array stride for dynamic indexing must be divisible by the size " |
| 8717 |
"of a 4-component vector. " |
| 8718 |
"Likely culprit here is a float or vec2 array inside a push " |
| 8719 |
"constant block which is std430. " |
| 8720 |
"This cannot be flattened. Try using std140 layout instead."); |
| 8721 |
} |
| 8722 |
|
| 8723 |
expr += to_enclosed_expression(index); |
| 8724 |
expr += " * "; |
| 8725 |
expr += convert_to_string(array_stride / word_stride); |
| 8726 |
expr += " + "; |
| 8727 |
} |
| 8728 |
} |
| 8729 |
// Arrays |
| 8730 |
else if (!type->array.empty()) |
| 8731 |
{ |
| 8732 |
auto *constant = maybe_get<SPIRConstant>(index); |
| 8733 |
if (constant) |
| 8734 |
{ |
| 8735 |
// Constant array access. |
| 8736 |
offset += constant->scalar() * array_stride; |
| 8737 |
} |
| 8738 |
else |
| 8739 |
{ |
| 8740 |
// Dynamic array access. |
| 8741 |
if (array_stride % word_stride) |
| 8742 |
{ |
| 8743 |
SPIRV_CROSS_THROW("Array stride for dynamic indexing must be divisible by the size " |
| 8744 |
"of a 4-component vector. " |
| 8745 |
"Likely culprit here is a float or vec2 array inside a push " |
| 8746 |
"constant block which is std430. " |
| 8747 |
"This cannot be flattened. Try using std140 layout instead."); |
| 8748 |
} |
| 8749 |
|
| 8750 |
expr += to_enclosed_expression(index, false); |
| 8751 |
expr += " * "; |
| 8752 |
expr += convert_to_string(array_stride / word_stride); |
| 8753 |
expr += " + "; |
| 8754 |
} |
| 8755 |
|
| 8756 |
uint32_t parent_type = type->parent_type; |
| 8757 |
type = &get<SPIRType>(parent_type); |
| 8758 |
|
| 8759 |
if (!type->array.empty()) |
| 8760 |
array_stride = get_decoration(parent_type, DecorationArrayStride); |
| 8761 |
} |
| 8762 |
// For structs, the index refers to a constant, which indexes into the members. |
| 8763 |
// We also check if this member is a builtin, since we then replace the entire expression with the builtin one. |
| 8764 |
else if (type->basetype == SPIRType::Struct) |
| 8765 |
{ |
| 8766 |
index = evaluate_constant_u32(index); |
| 8767 |
|
| 8768 |
if (index >= type->member_types.size()) |
| 8769 |
SPIRV_CROSS_THROW("Member index is out of bounds!"); |
| 8770 |
|
| 8771 |
offset += type_struct_member_offset(*type, index); |
| 8772 |
|
| 8773 |
auto &struct_type = *type; |
| 8774 |
type = &get<SPIRType>(type->member_types[index]); |
| 8775 |
|
| 8776 |
if (type->columns > 1) |
| 8777 |
{ |
| 8778 |
matrix_stride = type_struct_member_matrix_stride(struct_type, index); |
| 8779 |
row_major_matrix_needs_conversion = |
| 8780 |
combined_decoration_for_member(struct_type, index).get(DecorationRowMajor); |
| 8781 |
} |
| 8782 |
else |
| 8783 |
row_major_matrix_needs_conversion = false; |
| 8784 |
|
| 8785 |
if (!type->array.empty()) |
| 8786 |
array_stride = type_struct_member_array_stride(struct_type, index); |
| 8787 |
} |
| 8788 |
// Matrix -> Vector |
| 8789 |
else if (type->columns > 1) |
| 8790 |
{ |
| 8791 |
auto *constant = maybe_get<SPIRConstant>(index); |
| 8792 |
if (constant) |
| 8793 |
{ |
| 8794 |
index = evaluate_constant_u32(index); |
| 8795 |
offset += index * (row_major_matrix_needs_conversion ? (type->width / 8) : matrix_stride); |
| 8796 |
} |
| 8797 |
else |
| 8798 |
{ |
| 8799 |
uint32_t indexing_stride = row_major_matrix_needs_conversion ? (type->width / 8) : matrix_stride; |
| 8800 |
// Dynamic array access. |
| 8801 |
if (indexing_stride % word_stride) |
| 8802 |
{ |
| 8803 |
SPIRV_CROSS_THROW("Matrix stride for dynamic indexing must be divisible by the size of a " |
| 8804 |
"4-component vector. " |
| 8805 |
"Likely culprit here is a row-major matrix being accessed dynamically. " |
| 8806 |
"This cannot be flattened. Try using std140 layout instead."); |
| 8807 |
} |
| 8808 |
|
| 8809 |
expr += to_enclosed_expression(index, false); |
| 8810 |
expr += " * "; |
| 8811 |
expr += convert_to_string(indexing_stride / word_stride); |
| 8812 |
expr += " + "; |
| 8813 |
} |
| 8814 |
|
| 8815 |
type = &get<SPIRType>(type->parent_type); |
| 8816 |
} |
| 8817 |
// Vector -> Scalar |
| 8818 |
else if (type->vecsize > 1) |
| 8819 |
{ |
| 8820 |
auto *constant = maybe_get<SPIRConstant>(index); |
| 8821 |
if (constant) |
| 8822 |
{ |
| 8823 |
index = evaluate_constant_u32(index); |
| 8824 |
offset += index * (row_major_matrix_needs_conversion ? matrix_stride : (type->width / 8)); |
| 8825 |
} |
| 8826 |
else |
| 8827 |
{ |
| 8828 |
uint32_t indexing_stride = row_major_matrix_needs_conversion ? matrix_stride : (type->width / 8); |
| 8829 |
|
| 8830 |
// Dynamic array access. |
| 8831 |
if (indexing_stride % word_stride) |
| 8832 |
{ |
| 8833 |
SPIRV_CROSS_THROW("Stride for dynamic vector indexing must be divisible by the " |
| 8834 |
"size of a 4-component vector. " |
| 8835 |
"This cannot be flattened in legacy targets."); |
| 8836 |
} |
| 8837 |
|
| 8838 |
expr += to_enclosed_expression(index, false); |
| 8839 |
expr += " * "; |
| 8840 |
expr += convert_to_string(indexing_stride / word_stride); |
| 8841 |
expr += " + "; |
| 8842 |
} |
| 8843 |
|
| 8844 |
type = &get<SPIRType>(type->parent_type); |
| 8845 |
} |
| 8846 |
else |
| 8847 |
SPIRV_CROSS_THROW("Cannot subdivide a scalar value!"); |
| 8848 |
} |
| 8849 |
|
| 8850 |
if (need_transpose) |
| 8851 |
*need_transpose = row_major_matrix_needs_conversion; |
| 8852 |
if (out_matrix_stride) |
| 8853 |
*out_matrix_stride = matrix_stride; |
| 8854 |
if (out_array_stride) |
| 8855 |
*out_array_stride = array_stride; |
| 8856 |
|
| 8857 |
return std::make_pair(expr, offset); |
| 8858 |
} |
| 8859 |
|
| 8860 |
bool CompilerGLSL::should_dereference(uint32_t id) |
| 8861 |
{ |
| 8862 |
const auto &type = expression_type(id); |
| 8863 |
// Non-pointer expressions don't need to be dereferenced. |
| 8864 |
if (!type.pointer) |
| 8865 |
return false; |
| 8866 |
|
| 8867 |
// Handles shouldn't be dereferenced either. |
| 8868 |
if (!expression_is_lvalue(id)) |
| 8869 |
return false; |
| 8870 |
|
| 8871 |
// If id is a variable but not a phi variable, we should not dereference it. |
| 8872 |
if (auto *var = maybe_get<SPIRVariable>(id)) |
| 8873 |
return var->phi_variable; |
| 8874 |
|
| 8875 |
// If id is an access chain, we should not dereference it. |
| 8876 |
if (auto *expr = maybe_get<SPIRExpression>(id)) |
| 8877 |
return !expr->access_chain; |
| 8878 |
|
| 8879 |
// Otherwise, we should dereference this pointer expression. |
| 8880 |
return true; |
| 8881 |
} |
| 8882 |
|
| 8883 |
bool CompilerGLSL::should_forward(uint32_t id) const |
| 8884 |
{ |
| 8885 |
// If id is a variable we will try to forward it regardless of force_temporary check below |
| 8886 |
// This is important because otherwise we'll get local sampler copies (highp sampler2D foo = bar) that are invalid in OpenGL GLSL |
| 8887 |
auto *var = maybe_get<SPIRVariable>(id); |
| 8888 |
if (var && var->forwardable) |
| 8889 |
return true; |
| 8890 |
|
| 8891 |
// For debugging emit temporary variables for all expressions |
| 8892 |
if (options.force_temporary) |
| 8893 |
return false; |
| 8894 |
|
| 8895 |
// Immutable expression can always be forwarded. |
| 8896 |
if (is_immutable(id)) |
| 8897 |
return true; |
| 8898 |
|
| 8899 |
return false; |
| 8900 |
} |
| 8901 |
|
| 8902 |
bool CompilerGLSL::should_suppress_usage_tracking(uint32_t id) const |
| 8903 |
{ |
| 8904 |
// Used only by opcodes which don't do any real "work", they just swizzle data in some fashion. |
| 8905 |
return !expression_is_forwarded(id) || expression_suppresses_usage_tracking(id); |
| 8906 |
} |
| 8907 |
|
| 8908 |
void CompilerGLSL::track_expression_read(uint32_t id) |
| 8909 |
{ |
| 8910 |
switch (ir.ids[id].get_type()) |
| 8911 |
{ |
| 8912 |
case TypeExpression: |
| 8913 |
{ |
| 8914 |
auto &e = get<SPIRExpression>(id); |
| 8915 |
for (auto implied_read : e.implied_read_expressions) |
| 8916 |
track_expression_read(implied_read); |
| 8917 |
break; |
| 8918 |
} |
| 8919 |
|
| 8920 |
case TypeAccessChain: |
| 8921 |
{ |
| 8922 |
auto &e = get<SPIRAccessChain>(id); |
| 8923 |
for (auto implied_read : e.implied_read_expressions) |
| 8924 |
track_expression_read(implied_read); |
| 8925 |
break; |
| 8926 |
} |
| 8927 |
|
| 8928 |
default: |
| 8929 |
break; |
| 8930 |
} |
| 8931 |
|
| 8932 |
// If we try to read a forwarded temporary more than once we will stamp out possibly complex code twice. |
| 8933 |
// In this case, it's better to just bind the complex expression to the temporary and read that temporary twice. |
| 8934 |
if (expression_is_forwarded(id) && !expression_suppresses_usage_tracking(id)) |
| 8935 |
{ |
| 8936 |
auto &v = expression_usage_counts[id]; |
| 8937 |
v++; |
| 8938 |
|
| 8939 |
// If we create an expression outside a loop, |
| 8940 |
// but access it inside a loop, we're implicitly reading it multiple times. |
| 8941 |
// If the expression in question is expensive, we should hoist it out to avoid relying on loop-invariant code motion |
| 8942 |
// working inside the backend compiler. |
| 8943 |
if (expression_read_implies_multiple_reads(id)) |
| 8944 |
v++; |
| 8945 |
|
| 8946 |
if (v >= 2) |
| 8947 |
{ |
| 8948 |
//if (v == 2) |
| 8949 |
// fprintf(stderr, "ID %u was forced to temporary due to more than 1 expression use!\n", id); |
| 8950 |
|
| 8951 |
forced_temporaries.insert(id); |
| 8952 |
// Force a recompile after this pass to avoid forwarding this variable. |
| 8953 |
force_recompile(); |
| 8954 |
} |
| 8955 |
} |
| 8956 |
} |
| 8957 |
|
| 8958 |
bool CompilerGLSL::args_will_forward(uint32_t id, const uint32_t *args, uint32_t num_args, bool pure) |
| 8959 |
{ |
| 8960 |
if (forced_temporaries.find(id) != end(forced_temporaries)) |
| 8961 |
return false; |
| 8962 |
|
| 8963 |
for (uint32_t i = 0; i < num_args; i++) |
| 8964 |
if (!should_forward(args[i])) |
| 8965 |
return false; |
| 8966 |
|
| 8967 |
// We need to forward globals as well. |
| 8968 |
if (!pure) |
| 8969 |
{ |
| 8970 |
for (auto global : global_variables) |
| 8971 |
if (!should_forward(global)) |
| 8972 |
return false; |
| 8973 |
for (auto aliased : aliased_variables) |
| 8974 |
if (!should_forward(aliased)) |
| 8975 |
return false; |
| 8976 |
} |
| 8977 |
|
| 8978 |
return true; |
| 8979 |
} |
| 8980 |
|
| 8981 |
void CompilerGLSL::register_impure_function_call() |
| 8982 |
{ |
| 8983 |
// Impure functions can modify globals and aliased variables, so invalidate them as well. |
| 8984 |
for (auto global : global_variables) |
| 8985 |
flush_dependees(get<SPIRVariable>(global)); |
| 8986 |
for (auto aliased : aliased_variables) |
| 8987 |
flush_dependees(get<SPIRVariable>(aliased)); |
| 8988 |
} |
| 8989 |
|
| 8990 |
void CompilerGLSL::register_call_out_argument(uint32_t id) |
| 8991 |
{ |
| 8992 |
register_write(id); |
| 8993 |
|
| 8994 |
auto *var = maybe_get<SPIRVariable>(id); |
| 8995 |
if (var) |
| 8996 |
flush_variable_declaration(var->self); |
| 8997 |
} |
| 8998 |
|
| 8999 |
string CompilerGLSL::variable_decl_function_local(SPIRVariable &var) |
| 9000 |
{ |
| 9001 |
// These variables are always function local, |
| 9002 |
// so make sure we emit the variable without storage qualifiers. |
| 9003 |
// Some backends will inject custom variables locally in a function |
| 9004 |
// with a storage qualifier which is not function-local. |
| 9005 |
auto old_storage = var.storage; |
| 9006 |
var.storage = StorageClassFunction; |
| 9007 |
auto expr = variable_decl(var); |
| 9008 |
var.storage = old_storage; |
| 9009 |
return expr; |
| 9010 |
} |
| 9011 |
|
| 9012 |
void CompilerGLSL::emit_variable_temporary_copies(const SPIRVariable &var) |
| 9013 |
{ |
| 9014 |
// Ensure that we declare phi-variable copies even if the original declaration isn't deferred |
| 9015 |
if (var.allocate_temporary_copy && !flushed_phi_variables.count(var.self)) |
| 9016 |
{ |
| 9017 |
auto &type = get<SPIRType>(var.basetype); |
| 9018 |
auto &flags = get_decoration_bitset(var.self); |
| 9019 |
statement(flags_to_qualifiers_glsl(type, flags), variable_decl(type, join("_", var.self, "_copy")), ";"); |
| 9020 |
flushed_phi_variables.insert(var.self); |
| 9021 |
} |
| 9022 |
} |
| 9023 |
|
| 9024 |
void CompilerGLSL::flush_variable_declaration(uint32_t id) |
| 9025 |
{ |
| 9026 |
// Ensure that we declare phi-variable copies even if the original declaration isn't deferred |
| 9027 |
auto *var = maybe_get<SPIRVariable>(id); |
| 9028 |
if (var && var->deferred_declaration) |
| 9029 |
{ |
| 9030 |
string initializer; |
| 9031 |
if (options.force_zero_initialized_variables && |
| 9032 |
(var->storage == StorageClassFunction || var->storage == StorageClassGeneric || |
| 9033 |
var->storage == StorageClassPrivate) && |
| 9034 |
!var->initializer && type_can_zero_initialize(get_variable_data_type(*var))) |
| 9035 |
{ |
| 9036 |
initializer = join(" = ", to_zero_initialized_expression(get_variable_data_type_id(*var))); |
| 9037 |
} |
| 9038 |
|
| 9039 |
statement(variable_decl_function_local(*var), initializer, ";"); |
| 9040 |
var->deferred_declaration = false; |
| 9041 |
} |
| 9042 |
if (var) |
| 9043 |
{ |
| 9044 |
emit_variable_temporary_copies(*var); |
| 9045 |
} |
| 9046 |
} |
| 9047 |
|
| 9048 |
bool CompilerGLSL::remove_duplicate_swizzle(string &op) |
| 9049 |
{ |
| 9050 |
auto pos = op.find_last_of('.'); |
| 9051 |
if (pos == string::npos || pos == 0) |
| 9052 |
return false; |
| 9053 |
|
| 9054 |
string final_swiz = op.substr(pos + 1, string::npos); |
| 9055 |
|
| 9056 |
if (backend.swizzle_is_function) |
| 9057 |
{ |
| 9058 |
if (final_swiz.size() < 2) |
| 9059 |
return false; |
| 9060 |
|
| 9061 |
if (final_swiz.substr(final_swiz.size() - 2, string::npos) == "()") |
| 9062 |
final_swiz.erase(final_swiz.size() - 2, string::npos); |
| 9063 |
else |
| 9064 |
return false; |
| 9065 |
} |
| 9066 |
|
| 9067 |
// Check if final swizzle is of form .x, .xy, .xyz, .xyzw or similar. |
| 9068 |
// If so, and previous swizzle is of same length, |
| 9069 |
// we can drop the final swizzle altogether. |
| 9070 |
for (uint32_t i = 0; i < final_swiz.size(); i++) |
| 9071 |
{ |
| 9072 |
static const char expected[] = { 'x', 'y', 'z', 'w' }; |
| 9073 |
if (i >= 4 || final_swiz[i] != expected[i]) |
| 9074 |
return false; |
| 9075 |
} |
| 9076 |
|
| 9077 |
auto prevpos = op.find_last_of('.', pos - 1); |
| 9078 |
if (prevpos == string::npos) |
| 9079 |
return false; |
| 9080 |
|
| 9081 |
prevpos++; |
| 9082 |
|
| 9083 |
// Make sure there are only swizzles here ... |
| 9084 |
for (auto i = prevpos; i < pos; i++) |
| 9085 |
{ |
| 9086 |
if (op[i] < 'w' || op[i] > 'z') |
| 9087 |
{ |
| 9088 |
// If swizzles are foo.xyz() like in C++ backend for example, check for that. |
| 9089 |
if (backend.swizzle_is_function && i + 2 == pos && op[i] == '(' && op[i + 1] == ')') |
| 9090 |
break; |
| 9091 |
return false; |
| 9092 |
} |
| 9093 |
} |
| 9094 |
|
| 9095 |
// If original swizzle is large enough, just carve out the components we need. |
| 9096 |
// E.g. foobar.wyx.xy will turn into foobar.wy. |
| 9097 |
if (pos - prevpos >= final_swiz.size()) |
| 9098 |
{ |
| 9099 |
op.erase(prevpos + final_swiz.size(), string::npos); |
| 9100 |
|
| 9101 |
// Add back the function call ... |
| 9102 |
if (backend.swizzle_is_function) |
| 9103 |
op += "()"; |
| 9104 |
} |
| 9105 |
return true; |
| 9106 |
} |
| 9107 |
|
| 9108 |
// Optimizes away vector swizzles where we have something like |
| 9109 |
// vec3 foo; |
| 9110 |
// foo.xyz <-- swizzle expression does nothing. |
| 9111 |
// This is a very common pattern after OpCompositeCombine. |
| 9112 |
bool CompilerGLSL::remove_unity_swizzle(uint32_t base, string &op) |
| 9113 |
{ |
| 9114 |
auto pos = op.find_last_of('.'); |
| 9115 |
if (pos == string::npos || pos == 0) |
| 9116 |
return false; |
| 9117 |
|
| 9118 |
string final_swiz = op.substr(pos + 1, string::npos); |
| 9119 |
|
| 9120 |
if (backend.swizzle_is_function) |
| 9121 |
{ |
| 9122 |
if (final_swiz.size() < 2) |
| 9123 |
return false; |
| 9124 |
|
| 9125 |
if (final_swiz.substr(final_swiz.size() - 2, string::npos) == "()") |
| 9126 |
final_swiz.erase(final_swiz.size() - 2, string::npos); |
| 9127 |
else |
| 9128 |
return false; |
| 9129 |
} |
| 9130 |
|
| 9131 |
// Check if final swizzle is of form .x, .xy, .xyz, .xyzw or similar. |
| 9132 |
// If so, and previous swizzle is of same length, |
| 9133 |
// we can drop the final swizzle altogether. |
| 9134 |
for (uint32_t i = 0; i < final_swiz.size(); i++) |
| 9135 |
{ |
| 9136 |
static const char expected[] = { 'x', 'y', 'z', 'w' }; |
| 9137 |
if (i >= 4 || final_swiz[i] != expected[i]) |
| 9138 |
return false; |
| 9139 |
} |
| 9140 |
|
| 9141 |
auto &type = expression_type(base); |
| 9142 |
|
| 9143 |
// Sanity checking ... |
| 9144 |
assert(type.columns == 1 && type.array.empty()); |
| 9145 |
|
| 9146 |
if (type.vecsize == final_swiz.size()) |
| 9147 |
op.erase(pos, string::npos); |
| 9148 |
return true; |
| 9149 |
} |
| 9150 |
|
| 9151 |
string CompilerGLSL::build_composite_combiner(uint32_t return_type, const uint32_t *elems, uint32_t length) |
| 9152 |
{ |
| 9153 |
ID base = 0; |
| 9154 |
string op; |
| 9155 |
string subop; |
| 9156 |
|
| 9157 |
// Can only merge swizzles for vectors. |
| 9158 |
auto &type = get<SPIRType>(return_type); |
| 9159 |
bool can_apply_swizzle_opt = type.basetype != SPIRType::Struct && type.array.empty() && type.columns == 1; |
| 9160 |
bool swizzle_optimization = false; |
| 9161 |
|
| 9162 |
for (uint32_t i = 0; i < length; i++) |
| 9163 |
{ |
| 9164 |
auto *e = maybe_get<SPIRExpression>(elems[i]); |
| 9165 |
|
| 9166 |
// If we're merging another scalar which belongs to the same base |
| 9167 |
// object, just merge the swizzles to avoid triggering more than 1 expression read as much as possible! |
| 9168 |
if (can_apply_swizzle_opt && e && e->base_expression && e->base_expression == base) |
| 9169 |
{ |
| 9170 |
// Only supposed to be used for vector swizzle -> scalar. |
| 9171 |
assert(!e->expression.empty() && e->expression.front() == '.'); |
| 9172 |
subop += e->expression.substr(1, string::npos); |
| 9173 |
swizzle_optimization = true; |
| 9174 |
} |
| 9175 |
else |
| 9176 |
{ |
| 9177 |
// We'll likely end up with duplicated swizzles, e.g. |
| 9178 |
// foobar.xyz.xyz from patterns like |
| 9179 |
// OpVectorShuffle |
| 9180 |
// OpCompositeExtract x 3 |
| 9181 |
// OpCompositeConstruct 3x + other scalar. |
| 9182 |
// Just modify op in-place. |
| 9183 |
if (swizzle_optimization) |
| 9184 |
{ |
| 9185 |
if (backend.swizzle_is_function) |
| 9186 |
subop += "()"; |
| 9187 |
|
| 9188 |
// Don't attempt to remove unity swizzling if we managed to remove duplicate swizzles. |
| 9189 |
// The base "foo" might be vec4, while foo.xyz is vec3 (OpVectorShuffle) and looks like a vec3 due to the .xyz tacked on. |
| 9190 |
// We only want to remove the swizzles if we're certain that the resulting base will be the same vecsize. |
| 9191 |
// Essentially, we can only remove one set of swizzles, since that's what we have control over ... |
| 9192 |
// Case 1: |
| 9193 |
// foo.yxz.xyz: Duplicate swizzle kicks in, giving foo.yxz, we are done. |
| 9194 |
// foo.yxz was the result of OpVectorShuffle and we don't know the type of foo. |
| 9195 |
// Case 2: |
| 9196 |
// foo.xyz: Duplicate swizzle won't kick in. |
| 9197 |
// If foo is vec3, we can remove xyz, giving just foo. |
| 9198 |
if (!remove_duplicate_swizzle(subop)) |
| 9199 |
remove_unity_swizzle(base, subop); |
| 9200 |
|
| 9201 |
// Strips away redundant parens if we created them during component extraction. |
| 9202 |
strip_enclosed_expression(subop); |
| 9203 |
swizzle_optimization = false; |
| 9204 |
op += subop; |
| 9205 |
} |
| 9206 |
else |
| 9207 |
op += subop; |
| 9208 |
|
| 9209 |
if (i) |
| 9210 |
op += ", "; |
| 9211 |
|
| 9212 |
bool uses_buffer_offset = |
| 9213 |
type.basetype == SPIRType::Struct && has_member_decoration(type.self, i, DecorationOffset); |
| 9214 |
subop = to_composite_constructor_expression(elems[i], uses_buffer_offset); |
| 9215 |
} |
| 9216 |
|
| 9217 |
base = e ? e->base_expression : ID(0); |
| 9218 |
} |
| 9219 |
|
| 9220 |
if (swizzle_optimization) |
| 9221 |
{ |
| 9222 |
if (backend.swizzle_is_function) |
| 9223 |
subop += "()"; |
| 9224 |
|
| 9225 |
if (!remove_duplicate_swizzle(subop)) |
| 9226 |
remove_unity_swizzle(base, subop); |
| 9227 |
// Strips away redundant parens if we created them during component extraction. |
| 9228 |
strip_enclosed_expression(subop); |
| 9229 |
} |
| 9230 |
|
| 9231 |
op += subop; |
| 9232 |
return op; |
| 9233 |
} |
| 9234 |
|
| 9235 |
bool CompilerGLSL::skip_argument(uint32_t id) const |
| 9236 |
{ |
| 9237 |
if (!combined_image_samplers.empty() || !options.vulkan_semantics) |
| 9238 |
{ |
| 9239 |
auto &type = expression_type(id); |
| 9240 |
if (type.basetype == SPIRType::Sampler || (type.basetype == SPIRType::Image && type.image.sampled == 1)) |
| 9241 |
return true; |
| 9242 |
} |
| 9243 |
return false; |
| 9244 |
} |
| 9245 |
|
| 9246 |
bool CompilerGLSL::optimize_read_modify_write(const SPIRType &type, const string &lhs, const string &rhs) |
| 9247 |
{ |
| 9248 |
// Do this with strings because we have a very clear pattern we can check for and it avoids |
| 9249 |
// adding lots of special cases to the code emission. |
| 9250 |
if (rhs.size() < lhs.size() + 3) |
| 9251 |
return false; |
| 9252 |
|
| 9253 |
// Do not optimize matrices. They are a bit awkward to reason about in general |
| 9254 |
// (in which order does operation happen?), and it does not work on MSL anyways. |
| 9255 |
if (type.vecsize > 1 && type.columns > 1) |
| 9256 |
return false; |
| 9257 |
|
| 9258 |
auto index = rhs.find(lhs); |
| 9259 |
if (index != 0) |
| 9260 |
return false; |
| 9261 |
|
| 9262 |
// TODO: Shift operators, but it's not important for now. |
| 9263 |
auto op = rhs.find_first_of("+-/*%|&^", lhs.size() + 1); |
| 9264 |
if (op != lhs.size() + 1) |
| 9265 |
return false; |
| 9266 |
|
| 9267 |
// Check that the op is followed by space. This excludes && and ||. |
| 9268 |
if (rhs[op + 1] != ' ') |
| 9269 |
return false; |
| 9270 |
|
| 9271 |
char bop = rhs[op]; |
| 9272 |
auto expr = rhs.substr(lhs.size() + 3); |
| 9273 |
// Try to find increments and decrements. Makes it look neater as += 1, -= 1 is fairly rare to see in real code. |
| 9274 |
// Find some common patterns which are equivalent. |
| 9275 |
if ((bop == '+' || bop == '-') && (expr == "1" || expr == "uint(1)" || expr == "1u" || expr == "int(1u)")) |
| 9276 |
statement(lhs, bop, bop, ";"); |
| 9277 |
else |
| 9278 |
statement(lhs, " ", bop, "= ", expr, ";"); |
| 9279 |
return true; |
| 9280 |
} |
| 9281 |
|
| 9282 |
void CompilerGLSL::register_control_dependent_expression(uint32_t expr) |
| 9283 |
{ |
| 9284 |
if (forwarded_temporaries.find(expr) == end(forwarded_temporaries)) |
| 9285 |
return; |
| 9286 |
|
| 9287 |
assert(current_emitting_block); |
| 9288 |
current_emitting_block->invalidate_expressions.push_back(expr); |
| 9289 |
} |
| 9290 |
|
| 9291 |
void CompilerGLSL::emit_block_instructions(SPIRBlock &block) |
| 9292 |
{ |
| 9293 |
current_emitting_block = █ |
| 9294 |
for (auto &op : block.ops) |
| 9295 |
emit_instruction(op); |
| 9296 |
current_emitting_block = nullptr; |
| 9297 |
} |
| 9298 |
|
| 9299 |
void CompilerGLSL::disallow_forwarding_in_expression_chain(const SPIRExpression &expr) |
| 9300 |
{ |
| 9301 |
// Allow trivially forwarded expressions like OpLoad or trivial shuffles, |
| 9302 |
// these will be marked as having suppressed usage tracking. |
| 9303 |
// Our only concern is to make sure arithmetic operations are done in similar ways. |
| 9304 |
if (expression_is_forwarded(expr.self) && !expression_suppresses_usage_tracking(expr.self) && |
| 9305 |
forced_invariant_temporaries.count(expr.self) == 0) |
| 9306 |
{ |
| 9307 |
forced_temporaries.insert(expr.self); |
| 9308 |
forced_invariant_temporaries.insert(expr.self); |
| 9309 |
force_recompile(); |
| 9310 |
|
| 9311 |
for (auto &dependent : expr.expression_dependencies) |
| 9312 |
disallow_forwarding_in_expression_chain(get<SPIRExpression>(dependent)); |
| 9313 |
} |
| 9314 |
} |
| 9315 |
|
| 9316 |
void CompilerGLSL::handle_store_to_invariant_variable(uint32_t store_id, uint32_t value_id) |
| 9317 |
{ |
| 9318 |
// Variables or access chains marked invariant are complicated. We will need to make sure the code-gen leading up to |
| 9319 |
// this variable is consistent. The failure case for SPIRV-Cross is when an expression is forced to a temporary |
| 9320 |
// in one translation unit, but not another, e.g. due to multiple use of an expression. |
| 9321 |
// This causes variance despite the output variable being marked invariant, so the solution here is to force all dependent |
| 9322 |
// expressions to be temporaries. |
| 9323 |
// It is uncertain if this is enough to support invariant in all possible cases, but it should be good enough |
| 9324 |
// for all reasonable uses of invariant. |
| 9325 |
if (!has_decoration(store_id, DecorationInvariant)) |
| 9326 |
return; |
| 9327 |
|
| 9328 |
auto *expr = maybe_get<SPIRExpression>(value_id); |
| 9329 |
if (!expr) |
| 9330 |
return; |
| 9331 |
|
| 9332 |
disallow_forwarding_in_expression_chain(*expr); |
| 9333 |
} |
| 9334 |
|
| 9335 |
void CompilerGLSL::emit_store_statement(uint32_t lhs_expression, uint32_t rhs_expression) |
| 9336 |
{ |
| 9337 |
auto rhs = to_pointer_expression(rhs_expression); |
| 9338 |
|
| 9339 |
// Statements to OpStore may be empty if it is a struct with zero members. Just forward the store to /dev/null. |
| 9340 |
if (!rhs.empty()) |
| 9341 |
{ |
| 9342 |
handle_store_to_invariant_variable(lhs_expression, rhs_expression); |
| 9343 |
|
| 9344 |
auto lhs = to_dereferenced_expression(lhs_expression); |
| 9345 |
|
| 9346 |
// We might need to cast in order to store to a builtin. |
| 9347 |
cast_to_builtin_store(lhs_expression, rhs, expression_type(rhs_expression)); |
| 9348 |
|
| 9349 |
// Tries to optimize assignments like "<lhs> = <lhs> op expr". |
| 9350 |
// While this is purely cosmetic, this is important for legacy ESSL where loop |
| 9351 |
// variable increments must be in either i++ or i += const-expr. |
| 9352 |
// Without this, we end up with i = i + 1, which is correct GLSL, but not correct GLES 2.0. |
| 9353 |
if (!optimize_read_modify_write(expression_type(rhs_expression), lhs, rhs)) |
| 9354 |
statement(lhs, " = ", rhs, ";"); |
| 9355 |
register_write(lhs_expression); |
| 9356 |
} |
| 9357 |
} |
| 9358 |
|
| 9359 |
uint32_t CompilerGLSL::get_integer_width_for_instruction(const Instruction &instr) const |
| 9360 |
{ |
| 9361 |
if (instr.length < 3) |
| 9362 |
return 32; |
| 9363 |
|
| 9364 |
auto *ops = stream(instr); |
| 9365 |
|
| 9366 |
switch (instr.op) |
| 9367 |
{ |
| 9368 |
case OpSConvert: |
| 9369 |
case OpConvertSToF: |
| 9370 |
case OpUConvert: |
| 9371 |
case OpConvertUToF: |
| 9372 |
case OpIEqual: |
| 9373 |
case OpINotEqual: |
| 9374 |
case OpSLessThan: |
| 9375 |
case OpSLessThanEqual: |
| 9376 |
case OpSGreaterThan: |
| 9377 |
case OpSGreaterThanEqual: |
| 9378 |
case OpULessThan: |
| 9379 |
case OpULessThanEqual: |
| 9380 |
case OpUGreaterThan: |
| 9381 |
case OpUGreaterThanEqual: |
| 9382 |
return expression_type(ops[2]).width; |
| 9383 |
|
| 9384 |
default: |
| 9385 |
{ |
| 9386 |
// We can look at result type which is more robust. |
| 9387 |
auto *type = maybe_get<SPIRType>(ops[0]); |
| 9388 |
if (type && type_is_integral(*type)) |
| 9389 |
return type->width; |
| 9390 |
else |
| 9391 |
return 32; |
| 9392 |
} |
| 9393 |
} |
| 9394 |
} |
| 9395 |
|
| 9396 |
uint32_t CompilerGLSL::get_integer_width_for_glsl_instruction(GLSLstd450 op, const uint32_t *ops, uint32_t length) const |
| 9397 |
{ |
| 9398 |
if (length < 1) |
| 9399 |
return 32; |
| 9400 |
|
| 9401 |
switch (op) |
| 9402 |
{ |
| 9403 |
case GLSLstd450SAbs: |
| 9404 |
case GLSLstd450SSign: |
| 9405 |
case GLSLstd450UMin: |
| 9406 |
case GLSLstd450SMin: |
| 9407 |
case GLSLstd450UMax: |
| 9408 |
case GLSLstd450SMax: |
| 9409 |
case GLSLstd450UClamp: |
| 9410 |
case GLSLstd450SClamp: |
| 9411 |
case GLSLstd450FindSMsb: |
| 9412 |
case GLSLstd450FindUMsb: |
| 9413 |
return expression_type(ops[0]).width; |
| 9414 |
|
| 9415 |
default: |
| 9416 |
{ |
| 9417 |
// We don't need to care about other opcodes, just return 32. |
| 9418 |
return 32; |
| 9419 |
} |
| 9420 |
} |
| 9421 |
} |
| 9422 |
|
| 9423 |
void CompilerGLSL::emit_instruction(const Instruction &instruction) |
| 9424 |
{ |
| 9425 |
auto ops = stream(instruction); |
| 9426 |
auto opcode = static_cast<Op>(instruction.op); |
| 9427 |
uint32_t length = instruction.length; |
| 9428 |
|
| 9429 |
#define GLSL_BOP(op) emit_binary_op(ops[0], ops[1], ops[2], ops[3], #op) |
| 9430 |
#define GLSL_BOP_CAST(op, type) \ |
| 9431 |
emit_binary_op_cast(ops[0], ops[1], ops[2], ops[3], #op, type, opcode_is_sign_invariant(opcode)) |
| 9432 |
#define GLSL_UOP(op) emit_unary_op(ops[0], ops[1], ops[2], #op) |
| 9433 |
#define GLSL_QFOP(op) emit_quaternary_func_op(ops[0], ops[1], ops[2], ops[3], ops[4], ops[5], #op) |
| 9434 |
#define GLSL_TFOP(op) emit_trinary_func_op(ops[0], ops[1], ops[2], ops[3], ops[4], #op) |
| 9435 |
#define GLSL_BFOP(op) emit_binary_func_op(ops[0], ops[1], ops[2], ops[3], #op) |
| 9436 |
#define GLSL_BFOP_CAST(op, type) \ |
| 9437 |
emit_binary_func_op_cast(ops[0], ops[1], ops[2], ops[3], #op, type, opcode_is_sign_invariant(opcode)) |
| 9438 |
#define GLSL_BFOP(op) emit_binary_func_op(ops[0], ops[1], ops[2], ops[3], #op) |
| 9439 |
#define GLSL_UFOP(op) emit_unary_func_op(ops[0], ops[1], ops[2], #op) |
| 9440 |
|
| 9441 |
// If we need to do implicit bitcasts, make sure we do it with the correct type. |
| 9442 |
uint32_t integer_width = get_integer_width_for_instruction(instruction); |
| 9443 |
auto int_type = to_signed_basetype(integer_width); |
| 9444 |
auto uint_type = to_unsigned_basetype(integer_width); |
| 9445 |
|
| 9446 |
switch (opcode) |
| 9447 |
{ |
| 9448 |
// Dealing with memory |
| 9449 |
case OpLoad: |
| 9450 |
{ |
| 9451 |
uint32_t result_type = ops[0]; |
| 9452 |
uint32_t id = ops[1]; |
| 9453 |
uint32_t ptr = ops[2]; |
| 9454 |
|
| 9455 |
flush_variable_declaration(ptr); |
| 9456 |
|
| 9457 |
// If we're loading from memory that cannot be changed by the shader, |
| 9458 |
// just forward the expression directly to avoid needless temporaries. |
| 9459 |
// If an expression is mutable and forwardable, we speculate that it is immutable. |
| 9460 |
bool forward = should_forward(ptr) && forced_temporaries.find(id) == end(forced_temporaries); |
| 9461 |
|
| 9462 |
// If loading a non-native row-major matrix, mark the expression as need_transpose. |
| 9463 |
bool need_transpose = false; |
| 9464 |
bool old_need_transpose = false; |
| 9465 |
|
| 9466 |
auto *ptr_expression = maybe_get<SPIRExpression>(ptr); |
| 9467 |
|
| 9468 |
if (forward) |
| 9469 |
{ |
| 9470 |
// If we're forwarding the load, we're also going to forward transpose state, so don't transpose while |
| 9471 |
// taking the expression. |
| 9472 |
if (ptr_expression && ptr_expression->need_transpose) |
| 9473 |
{ |
| 9474 |
old_need_transpose = true; |
| 9475 |
ptr_expression->need_transpose = false; |
| 9476 |
need_transpose = true; |
| 9477 |
} |
| 9478 |
else if (is_non_native_row_major_matrix(ptr)) |
| 9479 |
need_transpose = true; |
| 9480 |
} |
| 9481 |
|
| 9482 |
// If we are forwarding this load, |
| 9483 |
// don't register the read to access chain here, defer that to when we actually use the expression, |
| 9484 |
// using the add_implied_read_expression mechanism. |
| 9485 |
string expr; |
| 9486 |
|
| 9487 |
bool is_packed = has_extended_decoration(ptr, SPIRVCrossDecorationPhysicalTypePacked); |
| 9488 |
bool is_remapped = has_extended_decoration(ptr, SPIRVCrossDecorationPhysicalTypeID); |
| 9489 |
if (forward || (!is_packed && !is_remapped)) |
| 9490 |
{ |
| 9491 |
// For the simple case, we do not need to deal with repacking. |
| 9492 |
expr = to_dereferenced_expression(ptr, false); |
| 9493 |
} |
| 9494 |
else |
| 9495 |
{ |
| 9496 |
// If we are not forwarding the expression, we need to unpack and resolve any physical type remapping here before |
| 9497 |
// storing the expression to a temporary. |
| 9498 |
expr = to_unpacked_expression(ptr); |
| 9499 |
} |
| 9500 |
|
| 9501 |
auto &type = get<SPIRType>(result_type); |
| 9502 |
auto &expr_type = expression_type(ptr); |
| 9503 |
|
| 9504 |
// If the expression has more vector components than the result type, insert |
| 9505 |
// a swizzle. This shouldn't happen normally on valid SPIR-V, but it might |
| 9506 |
// happen with e.g. the MSL backend replacing the type of an input variable. |
| 9507 |
if (expr_type.vecsize > type.vecsize) |
| 9508 |
expr = enclose_expression(expr + vector_swizzle(type.vecsize, 0)); |
| 9509 |
|
| 9510 |
// We might need to cast in order to load from a builtin. |
| 9511 |
cast_from_builtin_load(ptr, expr, type); |
| 9512 |
|
| 9513 |
// We might be trying to load a gl_Position[N], where we should be |
| 9514 |
// doing float4[](gl_in[i].gl_Position, ...) instead. |
| 9515 |
// Similar workarounds are required for input arrays in tessellation. |
| 9516 |
unroll_array_from_complex_load(id, ptr, expr); |
| 9517 |
|
| 9518 |
// Shouldn't need to check for ID, but current glslang codegen requires it in some cases |
| 9519 |
// when loading Image/Sampler descriptors. It does not hurt to check ID as well. |
| 9520 |
if (has_decoration(id, DecorationNonUniformEXT) || has_decoration(ptr, DecorationNonUniformEXT)) |
| 9521 |
{ |
| 9522 |
propagate_nonuniform_qualifier(ptr); |
| 9523 |
convert_non_uniform_expression(type, expr); |
| 9524 |
} |
| 9525 |
|
| 9526 |
if (forward && ptr_expression) |
| 9527 |
ptr_expression->need_transpose = old_need_transpose; |
| 9528 |
|
| 9529 |
bool flattened = ptr_expression && flattened_buffer_blocks.count(ptr_expression->loaded_from) != 0; |
| 9530 |
|
| 9531 |
if (backend.needs_row_major_load_workaround && !is_non_native_row_major_matrix(ptr) && !flattened) |
| 9532 |
rewrite_load_for_wrapped_row_major(expr, result_type, ptr); |
| 9533 |
|
| 9534 |
// By default, suppress usage tracking since using same expression multiple times does not imply any extra work. |
| 9535 |
// However, if we try to load a complex, composite object from a flattened buffer, |
| 9536 |
// we should avoid emitting the same code over and over and lower the result to a temporary. |
| 9537 |
bool usage_tracking = flattened && (type.basetype == SPIRType::Struct || (type.columns > 1)); |
| 9538 |
|
| 9539 |
SPIRExpression *e = nullptr; |
| 9540 |
if (!forward && expression_is_non_value_type_array(ptr)) |
| 9541 |
{ |
| 9542 |
// Complicated load case where we need to make a copy of ptr, but we cannot, because |
| 9543 |
// it is an array, and our backend does not support arrays as value types. |
| 9544 |
// Emit the temporary, and copy it explicitly. |
| 9545 |
e = &emit_uninitialized_temporary_expression(result_type, id); |
| 9546 |
emit_array_copy(to_expression(id), ptr, StorageClassFunction, get_expression_effective_storage_class(ptr)); |
| 9547 |
} |
| 9548 |
else |
| 9549 |
e = &emit_op(result_type, id, expr, forward, !usage_tracking); |
| 9550 |
|
| 9551 |
e->need_transpose = need_transpose; |
| 9552 |
register_read(id, ptr, forward); |
| 9553 |
|
| 9554 |
if (forward) |
| 9555 |
{ |
| 9556 |
// Pass through whether the result is of a packed type and the physical type ID. |
| 9557 |
if (has_extended_decoration(ptr, SPIRVCrossDecorationPhysicalTypePacked)) |
| 9558 |
set_extended_decoration(id, SPIRVCrossDecorationPhysicalTypePacked); |
| 9559 |
if (has_extended_decoration(ptr, SPIRVCrossDecorationPhysicalTypeID)) |
| 9560 |
{ |
| 9561 |
set_extended_decoration(id, SPIRVCrossDecorationPhysicalTypeID, |
| 9562 |
get_extended_decoration(ptr, SPIRVCrossDecorationPhysicalTypeID)); |
| 9563 |
} |
| 9564 |
} |
| 9565 |
else |
| 9566 |
{ |
| 9567 |
// This might have been set on an earlier compilation iteration, force it to be unset. |
| 9568 |
unset_extended_decoration(id, SPIRVCrossDecorationPhysicalTypePacked); |
| 9569 |
unset_extended_decoration(id, SPIRVCrossDecorationPhysicalTypeID); |
| 9570 |
} |
| 9571 |
|
| 9572 |
inherit_expression_dependencies(id, ptr); |
| 9573 |
if (forward) |
| 9574 |
add_implied_read_expression(*e, ptr); |
| 9575 |
break; |
| 9576 |
} |
| 9577 |
|
| 9578 |
case OpInBoundsAccessChain: |
| 9579 |
case OpAccessChain: |
| 9580 |
case OpPtrAccessChain: |
| 9581 |
{ |
| 9582 |
auto *var = maybe_get<SPIRVariable>(ops[2]); |
| 9583 |
if (var) |
| 9584 |
flush_variable_declaration(var->self); |
| 9585 |
|
| 9586 |
// If the base is immutable, the access chain pointer must also be. |
| 9587 |
// If an expression is mutable and forwardable, we speculate that it is immutable. |
| 9588 |
AccessChainMeta meta; |
| 9589 |
bool ptr_chain = opcode == OpPtrAccessChain; |
| 9590 |
auto e = access_chain(ops[2], &ops[3], length - 3, get<SPIRType>(ops[0]), &meta, ptr_chain); |
| 9591 |
|
| 9592 |
auto &expr = set<SPIRExpression>(ops[1], move(e), ops[0], should_forward(ops[2])); |
| 9593 |
|
| 9594 |
auto *backing_variable = maybe_get_backing_variable(ops[2]); |
| 9595 |
expr.loaded_from = backing_variable ? backing_variable->self : ID(ops[2]); |
| 9596 |
expr.need_transpose = meta.need_transpose; |
| 9597 |
expr.access_chain = true; |
| 9598 |
|
| 9599 |
// Mark the result as being packed. Some platforms handled packed vectors differently than non-packed. |
| 9600 |
if (meta.storage_is_packed) |
| 9601 |
set_extended_decoration(ops[1], SPIRVCrossDecorationPhysicalTypePacked); |
| 9602 |
if (meta.storage_physical_type != 0) |
| 9603 |
set_extended_decoration(ops[1], SPIRVCrossDecorationPhysicalTypeID, meta.storage_physical_type); |
| 9604 |
if (meta.storage_is_invariant) |
| 9605 |
set_decoration(ops[1], DecorationInvariant); |
| 9606 |
if (meta.flattened_struct) |
| 9607 |
flattened_structs[ops[1]] = true; |
| 9608 |
|
| 9609 |
// If we have some expression dependencies in our access chain, this access chain is technically a forwarded |
| 9610 |
// temporary which could be subject to invalidation. |
| 9611 |
// Need to assume we're forwarded while calling inherit_expression_depdendencies. |
| 9612 |
forwarded_temporaries.insert(ops[1]); |
| 9613 |
// The access chain itself is never forced to a temporary, but its dependencies might. |
| 9614 |
suppressed_usage_tracking.insert(ops[1]); |
| 9615 |
|
| 9616 |
for (uint32_t i = 2; i < length; i++) |
| 9617 |
{ |
| 9618 |
inherit_expression_dependencies(ops[1], ops[i]); |
| 9619 |
add_implied_read_expression(expr, ops[i]); |
| 9620 |
} |
| 9621 |
|
| 9622 |
// If we have no dependencies after all, i.e., all indices in the access chain are immutable temporaries, |
| 9623 |
// we're not forwarded after all. |
| 9624 |
if (expr.expression_dependencies.empty()) |
| 9625 |
forwarded_temporaries.erase(ops[1]); |
| 9626 |
|
| 9627 |
if (has_decoration(ops[1], DecorationNonUniformEXT)) |
| 9628 |
propagate_nonuniform_qualifier(ops[1]); |
| 9629 |
|
| 9630 |
break; |
| 9631 |
} |
| 9632 |
|
| 9633 |
case OpStore: |
| 9634 |
{ |
| 9635 |
auto *var = maybe_get<SPIRVariable>(ops[0]); |
| 9636 |
|
| 9637 |
if (var && var->statically_assigned) |
| 9638 |
var->static_expression = ops[1]; |
| 9639 |
else if (var && var->loop_variable && !var->loop_variable_enable) |
| 9640 |
var->static_expression = ops[1]; |
| 9641 |
else if (var && var->remapped_variable && var->static_expression) |
| 9642 |
{ |
| 9643 |
// Skip the write. |
| 9644 |
} |
| 9645 |
else if (flattened_structs.count(ops[0])) |
| 9646 |
{ |
| 9647 |
store_flattened_struct(ops[0], ops[1]); |
| 9648 |
register_write(ops[0]); |
| 9649 |
} |
| 9650 |
else |
| 9651 |
{ |
| 9652 |
emit_store_statement(ops[0], ops[1]); |
| 9653 |
} |
| 9654 |
|
| 9655 |
// Storing a pointer results in a variable pointer, so we must conservatively assume |
| 9656 |
// we can write through it. |
| 9657 |
if (expression_type(ops[1]).pointer) |
| 9658 |
register_write(ops[1]); |
| 9659 |
break; |
| 9660 |
} |
| 9661 |
|
| 9662 |
case OpArrayLength: |
| 9663 |
{ |
| 9664 |
uint32_t result_type = ops[0]; |
| 9665 |
uint32_t id = ops[1]; |
| 9666 |
auto e = access_chain_internal(ops[2], &ops[3], length - 3, ACCESS_CHAIN_INDEX_IS_LITERAL_BIT, nullptr); |
| 9667 |
set<SPIRExpression>(id, join(type_to_glsl(get<SPIRType>(result_type)), "(", e, ".length())"), result_type, |
| 9668 |
true); |
| 9669 |
break; |
| 9670 |
} |
| 9671 |
|
| 9672 |
// Function calls |
| 9673 |
case OpFunctionCall: |
| 9674 |
{ |
| 9675 |
uint32_t result_type = ops[0]; |
| 9676 |
uint32_t id = ops[1]; |
| 9677 |
uint32_t func = ops[2]; |
| 9678 |
const auto *arg = &ops[3]; |
| 9679 |
length -= 3; |
| 9680 |
|
| 9681 |
auto &callee = get<SPIRFunction>(func); |
| 9682 |
auto &return_type = get<SPIRType>(callee.return_type); |
| 9683 |
bool pure = function_is_pure(callee); |
| 9684 |
|
| 9685 |
bool callee_has_out_variables = false; |
| 9686 |
bool emit_return_value_as_argument = false; |
| 9687 |
|
| 9688 |
// Invalidate out variables passed to functions since they can be OpStore'd to. |
| 9689 |
for (uint32_t i = 0; i < length; i++) |
| 9690 |
{ |
| 9691 |
if (callee.arguments[i].write_count) |
| 9692 |
{ |
| 9693 |
register_call_out_argument(arg[i]); |
| 9694 |
callee_has_out_variables = true; |
| 9695 |
} |
| 9696 |
|
| 9697 |
flush_variable_declaration(arg[i]); |
| 9698 |
} |
| 9699 |
|
| 9700 |
if (!return_type.array.empty() && !backend.can_return_array) |
| 9701 |
{ |
| 9702 |
callee_has_out_variables = true; |
| 9703 |
emit_return_value_as_argument = true; |
| 9704 |
} |
| 9705 |
|
| 9706 |
if (!pure) |
| 9707 |
register_impure_function_call(); |
| 9708 |
|
| 9709 |
string funexpr; |
| 9710 |
SmallVector<string> arglist; |
| 9711 |
funexpr += to_name(func) + "("; |
| 9712 |
|
| 9713 |
if (emit_return_value_as_argument) |
| 9714 |
{ |
| 9715 |
statement(type_to_glsl(return_type), " ", to_name(id), type_to_array_glsl(return_type), ";"); |
| 9716 |
arglist.push_back(to_name(id)); |
| 9717 |
} |
| 9718 |
|
| 9719 |
for (uint32_t i = 0; i < length; i++) |
| 9720 |
{ |
| 9721 |
// Do not pass in separate images or samplers if we're remapping |
| 9722 |
// to combined image samplers. |
| 9723 |
if (skip_argument(arg[i])) |
| 9724 |
continue; |
| 9725 |
|
| 9726 |
arglist.push_back(to_func_call_arg(callee.arguments[i], arg[i])); |
| 9727 |
} |
| 9728 |
|
| 9729 |
for (auto &combined : callee.combined_parameters) |
| 9730 |
{ |
| 9731 |
auto image_id = combined.global_image ? combined.image_id : VariableID(arg[combined.image_id]); |
| 9732 |
auto sampler_id = combined.global_sampler ? combined.sampler_id : VariableID(arg[combined.sampler_id]); |
| 9733 |
arglist.push_back(to_combined_image_sampler(image_id, sampler_id)); |
| 9734 |
} |
| 9735 |
|
| 9736 |
append_global_func_args(callee, length, arglist); |
| 9737 |
|
| 9738 |
funexpr += merge(arglist); |
| 9739 |
funexpr += ")"; |
| 9740 |
|
| 9741 |
// Check for function call constraints. |
| 9742 |
check_function_call_constraints(arg, length); |
| 9743 |
|
| 9744 |
if (return_type.basetype != SPIRType::Void) |
| 9745 |
{ |
| 9746 |
// If the function actually writes to an out variable, |
| 9747 |
// take the conservative route and do not forward. |
| 9748 |
// The problem is that we might not read the function |
| 9749 |
// result (and emit the function) before an out variable |
| 9750 |
// is read (common case when return value is ignored! |
| 9751 |
// In order to avoid start tracking invalid variables, |
| 9752 |
// just avoid the forwarding problem altogether. |
| 9753 |
bool forward = args_will_forward(id, arg, length, pure) && !callee_has_out_variables && pure && |
| 9754 |
(forced_temporaries.find(id) == end(forced_temporaries)); |
| 9755 |
|
| 9756 |
if (emit_return_value_as_argument) |
| 9757 |
{ |
| 9758 |
statement(funexpr, ";"); |
| 9759 |
set<SPIRExpression>(id, to_name(id), result_type, true); |
| 9760 |
} |
| 9761 |
else |
| 9762 |
emit_op(result_type, id, funexpr, forward); |
| 9763 |
|
| 9764 |
// Function calls are implicit loads from all variables in question. |
| 9765 |
// Set dependencies for them. |
| 9766 |
for (uint32_t i = 0; i < length; i++) |
| 9767 |
register_read(id, arg[i], forward); |
| 9768 |
|
| 9769 |
// If we're going to forward the temporary result, |
| 9770 |
// put dependencies on every variable that must not change. |
| 9771 |
if (forward) |
| 9772 |
register_global_read_dependencies(callee, id); |
| 9773 |
} |
| 9774 |
else |
| 9775 |
statement(funexpr, ";"); |
| 9776 |
|
| 9777 |
break; |
| 9778 |
} |
| 9779 |
|
| 9780 |
// Composite munging |
| 9781 |
case OpCompositeConstruct: |
| 9782 |
{ |
| 9783 |
uint32_t result_type = ops[0]; |
| 9784 |
uint32_t id = ops[1]; |
| 9785 |
const auto *const elems = &ops[2]; |
| 9786 |
length -= 2; |
| 9787 |
|
| 9788 |
bool forward = true; |
| 9789 |
for (uint32_t i = 0; i < length; i++) |
| 9790 |
forward = forward && should_forward(elems[i]); |
| 9791 |
|
| 9792 |
auto &out_type = get<SPIRType>(result_type); |
| 9793 |
auto *in_type = length > 0 ? &expression_type(elems[0]) : nullptr; |
| 9794 |
|
| 9795 |
// Only splat if we have vector constructors. |
| 9796 |
// Arrays and structs must be initialized properly in full. |
| 9797 |
bool composite = !out_type.array.empty() || out_type.basetype == SPIRType::Struct; |
| 9798 |
|
| 9799 |
bool splat = false; |
| 9800 |
bool swizzle_splat = false; |
| 9801 |
|
| 9802 |
if (in_type) |
| 9803 |
{ |
| 9804 |
splat = in_type->vecsize == 1 && in_type->columns == 1 && !composite && backend.use_constructor_splatting; |
| 9805 |
swizzle_splat = in_type->vecsize == 1 && in_type->columns == 1 && backend.can_swizzle_scalar; |
| 9806 |
|
| 9807 |
if (ir.ids[elems[0]].get_type() == TypeConstant && !type_is_floating_point(*in_type)) |
| 9808 |
{ |
| 9809 |
// Cannot swizzle literal integers as a special case. |
| 9810 |
swizzle_splat = false; |
| 9811 |
} |
| 9812 |
} |
| 9813 |
|
| 9814 |
if (splat || swizzle_splat) |
| 9815 |
{ |
| 9816 |
uint32_t input = elems[0]; |
| 9817 |
for (uint32_t i = 0; i < length; i++) |
| 9818 |
{ |
| 9819 |
if (input != elems[i]) |
| 9820 |
{ |
| 9821 |
splat = false; |
| 9822 |
swizzle_splat = false; |
| 9823 |
} |
| 9824 |
} |
| 9825 |
} |
| 9826 |
|
| 9827 |
if (out_type.basetype == SPIRType::Struct && !backend.can_declare_struct_inline) |
| 9828 |
forward = false; |
| 9829 |
if (!out_type.array.empty() && !backend.can_declare_arrays_inline) |
| 9830 |
forward = false; |
| 9831 |
if (type_is_empty(out_type) && !backend.supports_empty_struct) |
| 9832 |
forward = false; |
| 9833 |
|
| 9834 |
string constructor_op; |
| 9835 |
if (backend.use_initializer_list && composite) |
| 9836 |
{ |
| 9837 |
bool needs_trailing_tracket = false; |
| 9838 |
// Only use this path if we are building composites. |
| 9839 |
// This path cannot be used for arithmetic. |
| 9840 |
if (backend.use_typed_initializer_list && out_type.basetype == SPIRType::Struct && out_type.array.empty()) |
| 9841 |
constructor_op += type_to_glsl_constructor(get<SPIRType>(result_type)); |
| 9842 |
else if (backend.use_typed_initializer_list && backend.array_is_value_type && !out_type.array.empty()) |
| 9843 |
{ |
| 9844 |
// MSL path. Array constructor is baked into type here, do not use _constructor variant. |
| 9845 |
constructor_op += type_to_glsl_constructor(get<SPIRType>(result_type)) + "("; |
| 9846 |
needs_trailing_tracket = true; |
| 9847 |
} |
| 9848 |
constructor_op += "{ "; |
| 9849 |
|
| 9850 |
if (type_is_empty(out_type) && !backend.supports_empty_struct) |
| 9851 |
constructor_op += "0"; |
| 9852 |
else if (splat) |
| 9853 |
constructor_op += to_unpacked_expression(elems[0]); |
| 9854 |
else |
| 9855 |
constructor_op += build_composite_combiner(result_type, elems, length); |
| 9856 |
constructor_op += " }"; |
| 9857 |
if (needs_trailing_tracket) |
| 9858 |
constructor_op += ")"; |
| 9859 |
} |
| 9860 |
else if (swizzle_splat && !composite) |
| 9861 |
{ |
| 9862 |
constructor_op = remap_swizzle(get<SPIRType>(result_type), 1, to_unpacked_expression(elems[0])); |
| 9863 |
} |
| 9864 |
else |
| 9865 |
{ |
| 9866 |
constructor_op = type_to_glsl_constructor(get<SPIRType>(result_type)) + "("; |
| 9867 |
if (type_is_empty(out_type) && !backend.supports_empty_struct) |
| 9868 |
constructor_op += "0"; |
| 9869 |
else if (splat) |
| 9870 |
constructor_op += to_unpacked_expression(elems[0]); |
| 9871 |
else |
| 9872 |
constructor_op += build_composite_combiner(result_type, elems, length); |
| 9873 |
constructor_op += ")"; |
| 9874 |
} |
| 9875 |
|
| 9876 |
if (!constructor_op.empty()) |
| 9877 |
{ |
| 9878 |
emit_op(result_type, id, constructor_op, forward); |
| 9879 |
for (uint32_t i = 0; i < length; i++) |
| 9880 |
inherit_expression_dependencies(id, elems[i]); |
| 9881 |
} |
| 9882 |
break; |
| 9883 |
} |
| 9884 |
|
| 9885 |
case OpVectorInsertDynamic: |
| 9886 |
{ |
| 9887 |
uint32_t result_type = ops[0]; |
| 9888 |
uint32_t id = ops[1]; |
| 9889 |
uint32_t vec = ops[2]; |
| 9890 |
uint32_t comp = ops[3]; |
| 9891 |
uint32_t index = ops[4]; |
| 9892 |
|
| 9893 |
flush_variable_declaration(vec); |
| 9894 |
|
| 9895 |
// Make a copy, then use access chain to store the variable. |
| 9896 |
statement(declare_temporary(result_type, id), to_expression(vec), ";"); |
| 9897 |
set<SPIRExpression>(id, to_name(id), result_type, true); |
| 9898 |
auto chain = access_chain_internal(id, &index, 1, 0, nullptr); |
| 9899 |
statement(chain, " = ", to_unpacked_expression(comp), ";"); |
| 9900 |
break; |
| 9901 |
} |
| 9902 |
|
| 9903 |
case OpVectorExtractDynamic: |
| 9904 |
{ |
| 9905 |
uint32_t result_type = ops[0]; |
| 9906 |
uint32_t id = ops[1]; |
| 9907 |
|
| 9908 |
auto expr = access_chain_internal(ops[2], &ops[3], 1, 0, nullptr); |
| 9909 |
emit_op(result_type, id, expr, should_forward(ops[2])); |
| 9910 |
inherit_expression_dependencies(id, ops[2]); |
| 9911 |
inherit_expression_dependencies(id, ops[3]); |
| 9912 |
break; |
| 9913 |
} |
| 9914 |
|
| 9915 |
case OpCompositeExtract: |
| 9916 |
{ |
| 9917 |
uint32_t result_type = ops[0]; |
| 9918 |
uint32_t id = ops[1]; |
| 9919 |
length -= 3; |
| 9920 |
|
| 9921 |
auto &type = get<SPIRType>(result_type); |
| 9922 |
|
| 9923 |
// We can only split the expression here if our expression is forwarded as a temporary. |
| 9924 |
bool allow_base_expression = forced_temporaries.find(id) == end(forced_temporaries); |
| 9925 |
|
| 9926 |
// Do not allow base expression for struct members. We risk doing "swizzle" optimizations in this case. |
| 9927 |
auto &composite_type = expression_type(ops[2]); |
| 9928 |
if (composite_type.basetype == SPIRType::Struct || !composite_type.array.empty()) |
| 9929 |
allow_base_expression = false; |
| 9930 |
|
| 9931 |
// Packed expressions or physical ID mapped expressions cannot be split up. |
| 9932 |
if (has_extended_decoration(ops[2], SPIRVCrossDecorationPhysicalTypePacked) || |
| 9933 |
has_extended_decoration(ops[2], SPIRVCrossDecorationPhysicalTypeID)) |
| 9934 |
allow_base_expression = false; |
| 9935 |
|
| 9936 |
// Cannot use base expression for row-major matrix row-extraction since we need to interleave access pattern |
| 9937 |
// into the base expression. |
| 9938 |
if (is_non_native_row_major_matrix(ops[2])) |
| 9939 |
allow_base_expression = false; |
| 9940 |
|
| 9941 |
AccessChainMeta meta; |
| 9942 |
SPIRExpression *e = nullptr; |
| 9943 |
|
| 9944 |
// Only apply this optimization if result is scalar. |
| 9945 |
if (allow_base_expression && should_forward(ops[2]) && type.vecsize == 1 && type.columns == 1 && length == 1) |
| 9946 |
{ |
| 9947 |
// We want to split the access chain from the base. |
| 9948 |
// This is so we can later combine different CompositeExtract results |
| 9949 |
// with CompositeConstruct without emitting code like |
| 9950 |
// |
| 9951 |
// vec3 temp = texture(...).xyz |
| 9952 |
// vec4(temp.x, temp.y, temp.z, 1.0). |
| 9953 |
// |
| 9954 |
// when we actually wanted to emit this |
| 9955 |
// vec4(texture(...).xyz, 1.0). |
| 9956 |
// |
| 9957 |
// Including the base will prevent this and would trigger multiple reads |
| 9958 |
// from expression causing it to be forced to an actual temporary in GLSL. |
| 9959 |
auto expr = access_chain_internal(ops[2], &ops[3], length, |
| 9960 |
ACCESS_CHAIN_INDEX_IS_LITERAL_BIT | ACCESS_CHAIN_CHAIN_ONLY_BIT, &meta); |
| 9961 |
e = &emit_op(result_type, id, expr, true, should_suppress_usage_tracking(ops[2])); |
| 9962 |
inherit_expression_dependencies(id, ops[2]); |
| 9963 |
e->base_expression = ops[2]; |
| 9964 |
} |
| 9965 |
else |
| 9966 |
{ |
| 9967 |
auto expr = access_chain_internal(ops[2], &ops[3], length, ACCESS_CHAIN_INDEX_IS_LITERAL_BIT, &meta); |
| 9968 |
e = &emit_op(result_type, id, expr, should_forward(ops[2]), should_suppress_usage_tracking(ops[2])); |
| 9969 |
inherit_expression_dependencies(id, ops[2]); |
| 9970 |
} |
| 9971 |
|
| 9972 |
// Pass through some meta information to the loaded expression. |
| 9973 |
// We can still end up loading a buffer type to a variable, then CompositeExtract from it |
| 9974 |
// instead of loading everything through an access chain. |
| 9975 |
e->need_transpose = meta.need_transpose; |
| 9976 |
if (meta.storage_is_packed) |
| 9977 |
set_extended_decoration(id, SPIRVCrossDecorationPhysicalTypePacked); |
| 9978 |
if (meta.storage_physical_type != 0) |
| 9979 |
set_extended_decoration(id, SPIRVCrossDecorationPhysicalTypeID, meta.storage_physical_type); |
| 9980 |
if (meta.storage_is_invariant) |
| 9981 |
set_decoration(id, DecorationInvariant); |
| 9982 |
|
| 9983 |
break; |
| 9984 |
} |
| 9985 |
|
| 9986 |
case OpCompositeInsert: |
| 9987 |
{ |
| 9988 |
uint32_t result_type = ops[0]; |
| 9989 |
uint32_t id = ops[1]; |
| 9990 |
uint32_t obj = ops[2]; |
| 9991 |
uint32_t composite = ops[3]; |
| 9992 |
const auto *elems = &ops[4]; |
| 9993 |
length -= 4; |
| 9994 |
|
| 9995 |
flush_variable_declaration(composite); |
| 9996 |
|
| 9997 |
// Make a copy, then use access chain to store the variable. |
| 9998 |
statement(declare_temporary(result_type, id), to_expression(composite), ";"); |
| 9999 |
set<SPIRExpression>(id, to_name(id), result_type, true); |
| 10000 |
auto chain = access_chain_internal(id, elems, length, ACCESS_CHAIN_INDEX_IS_LITERAL_BIT, nullptr); |
| 10001 |
statement(chain, " = ", to_unpacked_expression(obj), ";"); |
| 10002 |
|
| 10003 |
break; |
| 10004 |
} |
| 10005 |
|
| 10006 |
case OpCopyMemory: |
| 10007 |
{ |
| 10008 |
uint32_t lhs = ops[0]; |
| 10009 |
uint32_t rhs = ops[1]; |
| 10010 |
if (lhs != rhs) |
| 10011 |
{ |
| 10012 |
flush_variable_declaration(lhs); |
| 10013 |
flush_variable_declaration(rhs); |
| 10014 |
statement(to_expression(lhs), " = ", to_unpacked_expression(rhs), ";"); |
| 10015 |
register_write(lhs); |
| 10016 |
} |
| 10017 |
break; |
| 10018 |
} |
| 10019 |
|
| 10020 |
case OpCopyLogical: |
| 10021 |
{ |
| 10022 |
// This is used for copying object of different types, arrays and structs. |
| 10023 |
// We need to unroll the copy, element-by-element. |
| 10024 |
uint32_t result_type = ops[0]; |
| 10025 |
uint32_t id = ops[1]; |
| 10026 |
uint32_t rhs = ops[2]; |
| 10027 |
|
| 10028 |
emit_uninitialized_temporary_expression(result_type, id); |
| 10029 |
emit_copy_logical_type(id, result_type, rhs, expression_type_id(rhs), {}); |
| 10030 |
break; |
| 10031 |
} |
| 10032 |
|
| 10033 |
case OpCopyObject: |
| 10034 |
{ |
| 10035 |
uint32_t result_type = ops[0]; |
| 10036 |
uint32_t id = ops[1]; |
| 10037 |
uint32_t rhs = ops[2]; |
| 10038 |
bool pointer = get<SPIRType>(result_type).pointer; |
| 10039 |
|
| 10040 |
auto *chain = maybe_get<SPIRAccessChain>(rhs); |
| 10041 |
auto *imgsamp = maybe_get<SPIRCombinedImageSampler>(rhs); |
| 10042 |
if (chain) |
| 10043 |
{ |
| 10044 |
// Cannot lower to a SPIRExpression, just copy the object. |
| 10045 |
auto &e = set<SPIRAccessChain>(id, *chain); |
| 10046 |
e.self = id; |
| 10047 |
} |
| 10048 |
else if (imgsamp) |
| 10049 |
{ |
| 10050 |
// Cannot lower to a SPIRExpression, just copy the object. |
| 10051 |
// GLSL does not currently use this type and will never get here, but MSL does. |
| 10052 |
// Handled here instead of CompilerMSL for better integration and general handling, |
| 10053 |
// and in case GLSL or other subclasses require it in the future. |
| 10054 |
auto &e = set<SPIRCombinedImageSampler>(id, *imgsamp); |
| 10055 |
e.self = id; |
| 10056 |
} |
| 10057 |
else if (expression_is_lvalue(rhs) && !pointer) |
| 10058 |
{ |
| 10059 |
// Need a copy. |
| 10060 |
// For pointer types, we copy the pointer itself. |
| 10061 |
statement(declare_temporary(result_type, id), to_unpacked_expression(rhs), ";"); |
| 10062 |
set<SPIRExpression>(id, to_name(id), result_type, true); |
| 10063 |
} |
| 10064 |
else |
| 10065 |
{ |
| 10066 |
// RHS expression is immutable, so just forward it. |
| 10067 |
// Copying these things really make no sense, but |
| 10068 |
// seems to be allowed anyways. |
| 10069 |
auto &e = set<SPIRExpression>(id, to_expression(rhs), result_type, true); |
| 10070 |
if (pointer) |
| 10071 |
{ |
| 10072 |
auto *var = maybe_get_backing_variable(rhs); |
| 10073 |
e.loaded_from = var ? var->self : ID(0); |
| 10074 |
} |
| 10075 |
|
| 10076 |
// If we're copying an access chain, need to inherit the read expressions. |
| 10077 |
auto *rhs_expr = maybe_get<SPIRExpression>(rhs); |
| 10078 |
if (rhs_expr) |
| 10079 |
{ |
| 10080 |
e.implied_read_expressions = rhs_expr->implied_read_expressions; |
| 10081 |
e.expression_dependencies = rhs_expr->expression_dependencies; |
| 10082 |
} |
| 10083 |
} |
| 10084 |
break; |
| 10085 |
} |
| 10086 |
|
| 10087 |
case OpVectorShuffle: |
| 10088 |
{ |
| 10089 |
uint32_t result_type = ops[0]; |
| 10090 |
uint32_t id = ops[1]; |
| 10091 |
uint32_t vec0 = ops[2]; |
| 10092 |
uint32_t vec1 = ops[3]; |
| 10093 |
const auto *elems = &ops[4]; |
| 10094 |
length -= 4; |
| 10095 |
|
| 10096 |
auto &type0 = expression_type(vec0); |
| 10097 |
|
| 10098 |
// If we have the undefined swizzle index -1, we need to swizzle in undefined data, |
| 10099 |
// or in our case, T(0). |
| 10100 |
bool shuffle = false; |
| 10101 |
for (uint32_t i = 0; i < length; i++) |
| 10102 |
if (elems[i] >= type0.vecsize || elems[i] == 0xffffffffu) |
| 10103 |
shuffle = true; |
| 10104 |
|
| 10105 |
// Cannot use swizzles with packed expressions, force shuffle path. |
| 10106 |
if (!shuffle && has_extended_decoration(vec0, SPIRVCrossDecorationPhysicalTypePacked)) |
| 10107 |
shuffle = true; |
| 10108 |
|
| 10109 |
string expr; |
| 10110 |
bool should_fwd, trivial_forward; |
| 10111 |
|
| 10112 |
if (shuffle) |
| 10113 |
{ |
| 10114 |
should_fwd = should_forward(vec0) && should_forward(vec1); |
| 10115 |
trivial_forward = should_suppress_usage_tracking(vec0) && should_suppress_usage_tracking(vec1); |
| 10116 |
|
| 10117 |
// Constructor style and shuffling from two different vectors. |
| 10118 |
SmallVector<string> args; |
| 10119 |
for (uint32_t i = 0; i < length; i++) |
| 10120 |
{ |
| 10121 |
if (elems[i] == 0xffffffffu) |
| 10122 |
{ |
| 10123 |
// Use a constant 0 here. |
| 10124 |
// We could use the first component or similar, but then we risk propagating |
| 10125 |
// a value we might not need, and bog down codegen. |
| 10126 |
SPIRConstant c; |
| 10127 |
c.constant_type = type0.parent_type; |
| 10128 |
assert(type0.parent_type != ID(0)); |
| 10129 |
args.push_back(constant_expression(c)); |
| 10130 |
} |
| 10131 |
else if (elems[i] >= type0.vecsize) |
| 10132 |
args.push_back(to_extract_component_expression(vec1, elems[i] - type0.vecsize)); |
| 10133 |
else |
| 10134 |
args.push_back(to_extract_component_expression(vec0, elems[i])); |
| 10135 |
} |
| 10136 |
expr += join(type_to_glsl_constructor(get<SPIRType>(result_type)), "(", merge(args), ")"); |
| 10137 |
} |
| 10138 |
else |
| 10139 |
{ |
| 10140 |
should_fwd = should_forward(vec0); |
| 10141 |
trivial_forward = should_suppress_usage_tracking(vec0); |
| 10142 |
|
| 10143 |
// We only source from first vector, so can use swizzle. |
| 10144 |
// If the vector is packed, unpack it before applying a swizzle (needed for MSL) |
| 10145 |
expr += to_enclosed_unpacked_expression(vec0); |
| 10146 |
expr += "."; |
| 10147 |
for (uint32_t i = 0; i < length; i++) |
| 10148 |
{ |
| 10149 |
assert(elems[i] != 0xffffffffu); |
| 10150 |
expr += index_to_swizzle(elems[i]); |
| 10151 |
} |
| 10152 |
|
| 10153 |
if (backend.swizzle_is_function && length > 1) |
| 10154 |
expr += "()"; |
| 10155 |
} |
| 10156 |
|
| 10157 |
// A shuffle is trivial in that it doesn't actually *do* anything. |
| 10158 |
// We inherit the forwardedness from our arguments to avoid flushing out to temporaries when it's not really needed. |
| 10159 |
|
| 10160 |
emit_op(result_type, id, expr, should_fwd, trivial_forward); |
| 10161 |
|
| 10162 |
inherit_expression_dependencies(id, vec0); |
| 10163 |
if (vec0 != vec1) |
| 10164 |
inherit_expression_dependencies(id, vec1); |
| 10165 |
break; |
| 10166 |
} |
| 10167 |
|
| 10168 |
// ALU |
| 10169 |
case OpIsNan: |
| 10170 |
GLSL_UFOP(isnan); |
| 10171 |
break; |
| 10172 |
|
| 10173 |
case OpIsInf: |
| 10174 |
GLSL_UFOP(isinf); |
| 10175 |
break; |
| 10176 |
|
| 10177 |
case OpSNegate: |
| 10178 |
case OpFNegate: |
| 10179 |
GLSL_UOP(-); |
| 10180 |
break; |
| 10181 |
|
| 10182 |
case OpIAdd: |
| 10183 |
{ |
| 10184 |
// For simple arith ops, prefer the output type if there's a mismatch to avoid extra bitcasts. |
| 10185 |
auto type = get<SPIRType>(ops[0]).basetype; |
| 10186 |
GLSL_BOP_CAST(+, type); |
| 10187 |
break; |
| 10188 |
} |
| 10189 |
|
| 10190 |
case OpFAdd: |
| 10191 |
GLSL_BOP(+); |
| 10192 |
break; |
| 10193 |
|
| 10194 |
case OpISub: |
| 10195 |
{ |
| 10196 |
auto type = get<SPIRType>(ops[0]).basetype; |
| 10197 |
GLSL_BOP_CAST(-, type); |
| 10198 |
break; |
| 10199 |
} |
| 10200 |
|
| 10201 |
case OpFSub: |
| 10202 |
GLSL_BOP(-); |
| 10203 |
break; |
| 10204 |
|
| 10205 |
case OpIMul: |
| 10206 |
{ |
| 10207 |
auto type = get<SPIRType>(ops[0]).basetype; |
| 10208 |
GLSL_BOP_CAST(*, type); |
| 10209 |
break; |
| 10210 |
} |
| 10211 |
|
| 10212 |
case OpVectorTimesMatrix: |
| 10213 |
case OpMatrixTimesVector: |
| 10214 |
{ |
| 10215 |
// If the matrix needs transpose, just flip the multiply order. |
| 10216 |
auto *e = maybe_get<SPIRExpression>(ops[opcode == OpMatrixTimesVector ? 2 : 3]); |
| 10217 |
if (e && e->need_transpose) |
| 10218 |
{ |
| 10219 |
e->need_transpose = false; |
| 10220 |
string expr; |
| 10221 |
|
| 10222 |
if (opcode == OpMatrixTimesVector) |
| 10223 |
expr = join(to_enclosed_unpacked_expression(ops[3]), " * ", |
| 10224 |
enclose_expression(to_unpacked_row_major_matrix_expression(ops[2]))); |
| 10225 |
else |
| 10226 |
expr = join(enclose_expression(to_unpacked_row_major_matrix_expression(ops[3])), " * ", |
| 10227 |
to_enclosed_unpacked_expression(ops[2])); |
| 10228 |
|
| 10229 |
bool forward = should_forward(ops[2]) && should_forward(ops[3]); |
| 10230 |
emit_op(ops[0], ops[1], expr, forward); |
| 10231 |
e->need_transpose = true; |
| 10232 |
inherit_expression_dependencies(ops[1], ops[2]); |
| 10233 |
inherit_expression_dependencies(ops[1], ops[3]); |
| 10234 |
} |
| 10235 |
else |
| 10236 |
GLSL_BOP(*); |
| 10237 |
break; |
| 10238 |
} |
| 10239 |
|
| 10240 |
case OpMatrixTimesMatrix: |
| 10241 |
{ |
| 10242 |
auto *a = maybe_get<SPIRExpression>(ops[2]); |
| 10243 |
auto *b = maybe_get<SPIRExpression>(ops[3]); |
| 10244 |
|
| 10245 |
// If both matrices need transpose, we can multiply in flipped order and tag the expression as transposed. |
| 10246 |
// a^T * b^T = (b * a)^T. |
| 10247 |
if (a && b && a->need_transpose && b->need_transpose) |
| 10248 |
{ |
| 10249 |
a->need_transpose = false; |
| 10250 |
b->need_transpose = false; |
| 10251 |
auto expr = join(enclose_expression(to_unpacked_row_major_matrix_expression(ops[3])), " * ", |
| 10252 |
enclose_expression(to_unpacked_row_major_matrix_expression(ops[2]))); |
| 10253 |
bool forward = should_forward(ops[2]) && should_forward(ops[3]); |
| 10254 |
auto &e = emit_op(ops[0], ops[1], expr, forward); |
| 10255 |
e.need_transpose = true; |
| 10256 |
a->need_transpose = true; |
| 10257 |
b->need_transpose = true; |
| 10258 |
inherit_expression_dependencies(ops[1], ops[2]); |
| 10259 |
inherit_expression_dependencies(ops[1], ops[3]); |
| 10260 |
} |
| 10261 |
else |
| 10262 |
GLSL_BOP(*); |
| 10263 |
|
| 10264 |
break; |
| 10265 |
} |
| 10266 |
|
| 10267 |
case OpFMul: |
| 10268 |
case OpMatrixTimesScalar: |
| 10269 |
case OpVectorTimesScalar: |
| 10270 |
GLSL_BOP(*); |
| 10271 |
break; |
| 10272 |
|
| 10273 |
case OpOuterProduct: |
| 10274 |
GLSL_BFOP(outerProduct); |
| 10275 |
break; |
| 10276 |
|
| 10277 |
case OpDot: |
| 10278 |
GLSL_BFOP(dot); |
| 10279 |
break; |
| 10280 |
|
| 10281 |
case OpTranspose: |
| 10282 |
if (options.version < 120) // Matches GLSL 1.10 / ESSL 1.00 |
| 10283 |
{ |
| 10284 |
// transpose() is not available, so instead, flip need_transpose, |
| 10285 |
// which can later be turned into an emulated transpose op by |
| 10286 |
// convert_row_major_matrix(), if necessary. |
| 10287 |
uint32_t result_type = ops[0]; |
| 10288 |
uint32_t result_id = ops[1]; |
| 10289 |
uint32_t input = ops[2]; |
| 10290 |
|
| 10291 |
// Force need_transpose to false temporarily to prevent |
| 10292 |
// to_expression() from doing the transpose. |
| 10293 |
bool need_transpose = false; |
| 10294 |
auto *input_e = maybe_get<SPIRExpression>(input); |
| 10295 |
if (input_e) |
| 10296 |
swap(need_transpose, input_e->need_transpose); |
| 10297 |
|
| 10298 |
bool forward = should_forward(input); |
| 10299 |
auto &e = emit_op(result_type, result_id, to_expression(input), forward); |
| 10300 |
e.need_transpose = !need_transpose; |
| 10301 |
|
| 10302 |
// Restore the old need_transpose flag. |
| 10303 |
if (input_e) |
| 10304 |
input_e->need_transpose = need_transpose; |
| 10305 |
} |
| 10306 |
else |
| 10307 |
GLSL_UFOP(transpose); |
| 10308 |
break; |
| 10309 |
|
| 10310 |
case OpSRem: |
| 10311 |
{ |
| 10312 |
uint32_t result_type = ops[0]; |
| 10313 |
uint32_t result_id = ops[1]; |
| 10314 |
uint32_t op0 = ops[2]; |
| 10315 |
uint32_t op1 = ops[3]; |
| 10316 |
|
| 10317 |
// Needs special handling. |
| 10318 |
bool forward = should_forward(op0) && should_forward(op1); |
| 10319 |
auto expr = join(to_enclosed_expression(op0), " - ", to_enclosed_expression(op1), " * ", "(", |
| 10320 |
to_enclosed_expression(op0), " / ", to_enclosed_expression(op1), ")"); |
| 10321 |
|
| 10322 |
emit_op(result_type, result_id, expr, forward); |
| 10323 |
inherit_expression_dependencies(result_id, op0); |
| 10324 |
inherit_expression_dependencies(result_id, op1); |
| 10325 |
break; |
| 10326 |
} |
| 10327 |
|
| 10328 |
case OpSDiv: |
| 10329 |
GLSL_BOP_CAST(/, int_type); |
| 10330 |
break; |
| 10331 |
|
| 10332 |
case OpUDiv: |
| 10333 |
GLSL_BOP_CAST(/, uint_type); |
| 10334 |
break; |
| 10335 |
|
| 10336 |
case OpIAddCarry: |
| 10337 |
case OpISubBorrow: |
| 10338 |
{ |
| 10339 |
if (options.es && options.version < 310) |
| 10340 |
SPIRV_CROSS_THROW("Extended arithmetic is only available from ESSL 310."); |
| 10341 |
else if (!options.es && options.version < 400) |
| 10342 |
SPIRV_CROSS_THROW("Extended arithmetic is only available from GLSL 400."); |
| 10343 |
|
| 10344 |
uint32_t result_type = ops[0]; |
| 10345 |
uint32_t result_id = ops[1]; |
| 10346 |
uint32_t op0 = ops[2]; |
| 10347 |
uint32_t op1 = ops[3]; |
| 10348 |
auto &type = get<SPIRType>(result_type); |
| 10349 |
emit_uninitialized_temporary_expression(result_type, result_id); |
| 10350 |
const char *op = opcode == OpIAddCarry ? "uaddCarry" : "usubBorrow"; |
| 10351 |
|
| 10352 |
statement(to_expression(result_id), ".", to_member_name(type, 0), " = ", op, "(", to_expression(op0), ", ", |
| 10353 |
to_expression(op1), ", ", to_expression(result_id), ".", to_member_name(type, 1), ");"); |
| 10354 |
break; |
| 10355 |
} |
| 10356 |
|
| 10357 |
case OpUMulExtended: |
| 10358 |
case OpSMulExtended: |
| 10359 |
{ |
| 10360 |
if (options.es && options.version < 310) |
| 10361 |
SPIRV_CROSS_THROW("Extended arithmetic is only available from ESSL 310."); |
| 10362 |
else if (!options.es && options.version < 400) |
| 10363 |
SPIRV_CROSS_THROW("Extended arithmetic is only available from GLSL 4000."); |
| 10364 |
|
| 10365 |
uint32_t result_type = ops[0]; |
| 10366 |
uint32_t result_id = ops[1]; |
| 10367 |
uint32_t op0 = ops[2]; |
| 10368 |
uint32_t op1 = ops[3]; |
| 10369 |
auto &type = get<SPIRType>(result_type); |
| 10370 |
emit_uninitialized_temporary_expression(result_type, result_id); |
| 10371 |
const char *op = opcode == OpUMulExtended ? "umulExtended" : "imulExtended"; |
| 10372 |
|
| 10373 |
statement(op, "(", to_expression(op0), ", ", to_expression(op1), ", ", to_expression(result_id), ".", |
| 10374 |
to_member_name(type, 1), ", ", to_expression(result_id), ".", to_member_name(type, 0), ");"); |
| 10375 |
break; |
| 10376 |
} |
| 10377 |
|
| 10378 |
case OpFDiv: |
| 10379 |
GLSL_BOP(/); |
| 10380 |
break; |
| 10381 |
|
| 10382 |
case OpShiftRightLogical: |
| 10383 |
GLSL_BOP_CAST(>>, uint_type); |
| 10384 |
break; |
| 10385 |
|
| 10386 |
case OpShiftRightArithmetic: |
| 10387 |
GLSL_BOP_CAST(>>, int_type); |
| 10388 |
break; |
| 10389 |
|
| 10390 |
case OpShiftLeftLogical: |
| 10391 |
{ |
| 10392 |
auto type = get<SPIRType>(ops[0]).basetype; |
| 10393 |
GLSL_BOP_CAST(<<, type); |
| 10394 |
break; |
| 10395 |
} |
| 10396 |
|
| 10397 |
case OpBitwiseOr: |
| 10398 |
{ |
| 10399 |
auto type = get<SPIRType>(ops[0]).basetype; |
| 10400 |
GLSL_BOP_CAST(|, type); |
| 10401 |
break; |
| 10402 |
} |
| 10403 |
|
| 10404 |
case OpBitwiseXor: |
| 10405 |
{ |
| 10406 |
auto type = get<SPIRType>(ops[0]).basetype; |
| 10407 |
GLSL_BOP_CAST(^, type); |
| 10408 |
break; |
| 10409 |
} |
| 10410 |
|
| 10411 |
case OpBitwiseAnd: |
| 10412 |
{ |
| 10413 |
auto type = get<SPIRType>(ops[0]).basetype; |
| 10414 |
GLSL_BOP_CAST(&, type); |
| 10415 |
break; |
| 10416 |
} |
| 10417 |
|
| 10418 |
case OpNot: |
| 10419 |
GLSL_UOP(~); |
| 10420 |
break; |
| 10421 |
|
| 10422 |
case OpUMod: |
| 10423 |
GLSL_BOP_CAST(%, uint_type); |
| 10424 |
break; |
| 10425 |
|
| 10426 |
case OpSMod: |
| 10427 |
GLSL_BOP_CAST(%, int_type); |
| 10428 |
break; |
| 10429 |
|
| 10430 |
case OpFMod: |
| 10431 |
GLSL_BFOP(mod); |
| 10432 |
break; |
| 10433 |
|
| 10434 |
case OpFRem: |
| 10435 |
{ |
| 10436 |
if (is_legacy()) |
| 10437 |
SPIRV_CROSS_THROW("OpFRem requires trunc() and is only supported on non-legacy targets. A workaround is " |
| 10438 |
"needed for legacy."); |
| 10439 |
|
| 10440 |
uint32_t result_type = ops[0]; |
| 10441 |
uint32_t result_id = ops[1]; |
| 10442 |
uint32_t op0 = ops[2]; |
| 10443 |
uint32_t op1 = ops[3]; |
| 10444 |
|
| 10445 |
// Needs special handling. |
| 10446 |
bool forward = should_forward(op0) && should_forward(op1); |
| 10447 |
auto expr = join(to_enclosed_expression(op0), " - ", to_enclosed_expression(op1), " * ", "trunc(", |
| 10448 |
to_enclosed_expression(op0), " / ", to_enclosed_expression(op1), ")"); |
| 10449 |
|
| 10450 |
emit_op(result_type, result_id, expr, forward); |
| 10451 |
inherit_expression_dependencies(result_id, op0); |
| 10452 |
inherit_expression_dependencies(result_id, op1); |
| 10453 |
break; |
| 10454 |
} |
| 10455 |
|
| 10456 |
// Relational |
| 10457 |
case OpAny: |
| 10458 |
GLSL_UFOP(any); |
| 10459 |
break; |
| 10460 |
|
| 10461 |
case OpAll: |
| 10462 |
GLSL_UFOP(all); |
| 10463 |
break; |
| 10464 |
|
| 10465 |
case OpSelect: |
| 10466 |
emit_mix_op(ops[0], ops[1], ops[4], ops[3], ops[2]); |
| 10467 |
break; |
| 10468 |
|
| 10469 |
case OpLogicalOr: |
| 10470 |
{ |
| 10471 |
// No vector variant in GLSL for logical OR. |
| 10472 |
auto result_type = ops[0]; |
| 10473 |
auto id = ops[1]; |
| 10474 |
auto &type = get<SPIRType>(result_type); |
| 10475 |
|
| 10476 |
if (type.vecsize > 1) |
| 10477 |
emit_unrolled_binary_op(result_type, id, ops[2], ops[3], "||", false, SPIRType::Unknown); |
| 10478 |
else |
| 10479 |
GLSL_BOP(||); |
| 10480 |
break; |
| 10481 |
} |
| 10482 |
|
| 10483 |
case OpLogicalAnd: |
| 10484 |
{ |
| 10485 |
// No vector variant in GLSL for logical AND. |
| 10486 |
auto result_type = ops[0]; |
| 10487 |
auto id = ops[1]; |
| 10488 |
auto &type = get<SPIRType>(result_type); |
| 10489 |
|
| 10490 |
if (type.vecsize > 1) |
| 10491 |
emit_unrolled_binary_op(result_type, id, ops[2], ops[3], "&&", false, SPIRType::Unknown); |
| 10492 |
else |
| 10493 |
GLSL_BOP(&&); |
| 10494 |
break; |
| 10495 |
} |
| 10496 |
|
| 10497 |
case OpLogicalNot: |
| 10498 |
{ |
| 10499 |
auto &type = get<SPIRType>(ops[0]); |
| 10500 |
if (type.vecsize > 1) |
| 10501 |
GLSL_UFOP(not ); |
| 10502 |
else |
| 10503 |
GLSL_UOP(!); |
| 10504 |
break; |
| 10505 |
} |
| 10506 |
|
| 10507 |
case OpIEqual: |
| 10508 |
{ |
| 10509 |
if (expression_type(ops[2]).vecsize > 1) |
| 10510 |
GLSL_BFOP_CAST(equal, int_type); |
| 10511 |
else |
| 10512 |
GLSL_BOP_CAST(==, int_type); |
| 10513 |
break; |
| 10514 |
} |
| 10515 |
|
| 10516 |
case OpLogicalEqual: |
| 10517 |
case OpFOrdEqual: |
| 10518 |
{ |
| 10519 |
if (expression_type(ops[2]).vecsize > 1) |
| 10520 |
GLSL_BFOP(equal); |
| 10521 |
else |
| 10522 |
GLSL_BOP(==); |
| 10523 |
break; |
| 10524 |
} |
| 10525 |
|
| 10526 |
case OpINotEqual: |
| 10527 |
{ |
| 10528 |
if (expression_type(ops[2]).vecsize > 1) |
| 10529 |
GLSL_BFOP_CAST(notEqual, int_type); |
| 10530 |
else |
| 10531 |
GLSL_BOP_CAST(!=, int_type); |
| 10532 |
break; |
| 10533 |
} |
| 10534 |
|
| 10535 |
case OpLogicalNotEqual: |
| 10536 |
case OpFOrdNotEqual: |
| 10537 |
{ |
| 10538 |
if (expression_type(ops[2]).vecsize > 1) |
| 10539 |
GLSL_BFOP(notEqual); |
| 10540 |
else |
| 10541 |
GLSL_BOP(!=); |
| 10542 |
break; |
| 10543 |
} |
| 10544 |
|
| 10545 |
case OpUGreaterThan: |
| 10546 |
case OpSGreaterThan: |
| 10547 |
{ |
| 10548 |
auto type = opcode == OpUGreaterThan ? uint_type : int_type; |
| 10549 |
if (expression_type(ops[2]).vecsize > 1) |
| 10550 |
GLSL_BFOP_CAST(greaterThan, type); |
| 10551 |
else |
| 10552 |
GLSL_BOP_CAST(>, type); |
| 10553 |
break; |
| 10554 |
} |
| 10555 |
|
| 10556 |
case OpFOrdGreaterThan: |
| 10557 |
{ |
| 10558 |
if (expression_type(ops[2]).vecsize > 1) |
| 10559 |
GLSL_BFOP(greaterThan); |
| 10560 |
else |
| 10561 |
GLSL_BOP(>); |
| 10562 |
break; |
| 10563 |
} |
| 10564 |
|
| 10565 |
case OpUGreaterThanEqual: |
| 10566 |
case OpSGreaterThanEqual: |
| 10567 |
{ |
| 10568 |
auto type = opcode == OpUGreaterThanEqual ? uint_type : int_type; |
| 10569 |
if (expression_type(ops[2]).vecsize > 1) |
| 10570 |
GLSL_BFOP_CAST(greaterThanEqual, type); |
| 10571 |
else |
| 10572 |
GLSL_BOP_CAST(>=, type); |
| 10573 |
break; |
| 10574 |
} |
| 10575 |
|
| 10576 |
case OpFOrdGreaterThanEqual: |
| 10577 |
{ |
| 10578 |
if (expression_type(ops[2]).vecsize > 1) |
| 10579 |
GLSL_BFOP(greaterThanEqual); |
| 10580 |
else |
| 10581 |
GLSL_BOP(>=); |
| 10582 |
break; |
| 10583 |
} |
| 10584 |
|
| 10585 |
case OpULessThan: |
| 10586 |
case OpSLessThan: |
| 10587 |
{ |
| 10588 |
auto type = opcode == OpULessThan ? uint_type : int_type; |
| 10589 |
if (expression_type(ops[2]).vecsize > 1) |
| 10590 |
GLSL_BFOP_CAST(lessThan, type); |
| 10591 |
else |
| 10592 |
GLSL_BOP_CAST(<, type); |
| 10593 |
break; |
| 10594 |
} |
| 10595 |
|
| 10596 |
case OpFOrdLessThan: |
| 10597 |
{ |
| 10598 |
if (expression_type(ops[2]).vecsize > 1) |
| 10599 |
GLSL_BFOP(lessThan); |
| 10600 |
else |
| 10601 |
GLSL_BOP(<); |
| 10602 |
break; |
| 10603 |
} |
| 10604 |
|
| 10605 |
case OpULessThanEqual: |
| 10606 |
case OpSLessThanEqual: |
| 10607 |
{ |
| 10608 |
auto type = opcode == OpULessThanEqual ? uint_type : int_type; |
| 10609 |
if (expression_type(ops[2]).vecsize > 1) |
| 10610 |
GLSL_BFOP_CAST(lessThanEqual, type); |
| 10611 |
else |
| 10612 |
GLSL_BOP_CAST(<=, type); |
| 10613 |
break; |
| 10614 |
} |
| 10615 |
|
| 10616 |
case OpFOrdLessThanEqual: |
| 10617 |
{ |
| 10618 |
if (expression_type(ops[2]).vecsize > 1) |
| 10619 |
GLSL_BFOP(lessThanEqual); |
| 10620 |
else |
| 10621 |
GLSL_BOP(<=); |
| 10622 |
break; |
| 10623 |
} |
| 10624 |
|
| 10625 |
// Conversion |
| 10626 |
case OpSConvert: |
| 10627 |
case OpConvertSToF: |
| 10628 |
case OpUConvert: |
| 10629 |
case OpConvertUToF: |
| 10630 |
{ |
| 10631 |
auto input_type = opcode == OpSConvert || opcode == OpConvertSToF ? int_type : uint_type; |
| 10632 |
uint32_t result_type = ops[0]; |
| 10633 |
uint32_t id = ops[1]; |
| 10634 |
|
| 10635 |
auto &type = get<SPIRType>(result_type); |
| 10636 |
auto &arg_type = expression_type(ops[2]); |
| 10637 |
auto func = type_to_glsl_constructor(type); |
| 10638 |
|
| 10639 |
if (arg_type.width < type.width || type_is_floating_point(type)) |
| 10640 |
emit_unary_func_op_cast(result_type, id, ops[2], func.c_str(), input_type, type.basetype); |
| 10641 |
else |
| 10642 |
emit_unary_func_op(result_type, id, ops[2], func.c_str()); |
| 10643 |
break; |
| 10644 |
} |
| 10645 |
|
| 10646 |
case OpConvertFToU: |
| 10647 |
case OpConvertFToS: |
| 10648 |
{ |
| 10649 |
// Cast to expected arithmetic type, then potentially bitcast away to desired signedness. |
| 10650 |
uint32_t result_type = ops[0]; |
| 10651 |
uint32_t id = ops[1]; |
| 10652 |
auto &type = get<SPIRType>(result_type); |
| 10653 |
auto expected_type = type; |
| 10654 |
auto &float_type = expression_type(ops[2]); |
| 10655 |
expected_type.basetype = |
| 10656 |
opcode == OpConvertFToS ? to_signed_basetype(type.width) : to_unsigned_basetype(type.width); |
| 10657 |
|
| 10658 |
auto func = type_to_glsl_constructor(expected_type); |
| 10659 |
emit_unary_func_op_cast(result_type, id, ops[2], func.c_str(), float_type.basetype, expected_type.basetype); |
| 10660 |
break; |
| 10661 |
} |
| 10662 |
|
| 10663 |
case OpFConvert: |
| 10664 |
{ |
| 10665 |
uint32_t result_type = ops[0]; |
| 10666 |
uint32_t id = ops[1]; |
| 10667 |
|
| 10668 |
auto func = type_to_glsl_constructor(get<SPIRType>(result_type)); |
| 10669 |
emit_unary_func_op(result_type, id, ops[2], func.c_str()); |
| 10670 |
break; |
| 10671 |
} |
| 10672 |
|
| 10673 |
case OpBitcast: |
| 10674 |
{ |
| 10675 |
uint32_t result_type = ops[0]; |
| 10676 |
uint32_t id = ops[1]; |
| 10677 |
uint32_t arg = ops[2]; |
| 10678 |
|
| 10679 |
if (!emit_complex_bitcast(result_type, id, arg)) |
| 10680 |
{ |
| 10681 |
auto op = bitcast_glsl_op(get<SPIRType>(result_type), expression_type(arg)); |
| 10682 |
emit_unary_func_op(result_type, id, arg, op.c_str()); |
| 10683 |
} |
| 10684 |
break; |
| 10685 |
} |
| 10686 |
|
| 10687 |
case OpQuantizeToF16: |
| 10688 |
{ |
| 10689 |
uint32_t result_type = ops[0]; |
| 10690 |
uint32_t id = ops[1]; |
| 10691 |
uint32_t arg = ops[2]; |
| 10692 |
|
| 10693 |
string op; |
| 10694 |
auto &type = get<SPIRType>(result_type); |
| 10695 |
|
| 10696 |
switch (type.vecsize) |
| 10697 |
{ |
| 10698 |
case 1: |
| 10699 |
op = join("unpackHalf2x16(packHalf2x16(vec2(", to_expression(arg), "))).x"); |
| 10700 |
break; |
| 10701 |
case 2: |
| 10702 |
op = join("unpackHalf2x16(packHalf2x16(", to_expression(arg), "))"); |
| 10703 |
break; |
| 10704 |
case 3: |
| 10705 |
{ |
| 10706 |
auto op0 = join("unpackHalf2x16(packHalf2x16(", to_expression(arg), ".xy))"); |
| 10707 |
auto op1 = join("unpackHalf2x16(packHalf2x16(", to_expression(arg), ".zz)).x"); |
| 10708 |
op = join("vec3(", op0, ", ", op1, ")"); |
| 10709 |
break; |
| 10710 |
} |
| 10711 |
case 4: |
| 10712 |
{ |
| 10713 |
auto op0 = join("unpackHalf2x16(packHalf2x16(", to_expression(arg), ".xy))"); |
| 10714 |
auto op1 = join("unpackHalf2x16(packHalf2x16(", to_expression(arg), ".zw))"); |
| 10715 |
op = join("vec4(", op0, ", ", op1, ")"); |
| 10716 |
break; |
| 10717 |
} |
| 10718 |
default: |
| 10719 |
SPIRV_CROSS_THROW("Illegal argument to OpQuantizeToF16."); |
| 10720 |
} |
| 10721 |
|
| 10722 |
emit_op(result_type, id, op, should_forward(arg)); |
| 10723 |
inherit_expression_dependencies(id, arg); |
| 10724 |
break; |
| 10725 |
} |
| 10726 |
|
| 10727 |
// Derivatives |
| 10728 |
case OpDPdx: |
| 10729 |
GLSL_UFOP(dFdx); |
| 10730 |
if (is_legacy_es()) |
| 10731 |
require_extension_internal("GL_OES_standard_derivatives"); |
| 10732 |
register_control_dependent_expression(ops[1]); |
| 10733 |
break; |
| 10734 |
|
| 10735 |
case OpDPdy: |
| 10736 |
GLSL_UFOP(dFdy); |
| 10737 |
if (is_legacy_es()) |
| 10738 |
require_extension_internal("GL_OES_standard_derivatives"); |
| 10739 |
register_control_dependent_expression(ops[1]); |
| 10740 |
break; |
| 10741 |
|
| 10742 |
case OpDPdxFine: |
| 10743 |
GLSL_UFOP(dFdxFine); |
| 10744 |
if (options.es) |
| 10745 |
{ |
| 10746 |
SPIRV_CROSS_THROW("GL_ARB_derivative_control is unavailable in OpenGL ES."); |
| 10747 |
} |
| 10748 |
if (options.version < 450) |
| 10749 |
require_extension_internal("GL_ARB_derivative_control"); |
| 10750 |
register_control_dependent_expression(ops[1]); |
| 10751 |
break; |
| 10752 |
|
| 10753 |
case OpDPdyFine: |
| 10754 |
GLSL_UFOP(dFdyFine); |
| 10755 |
if (options.es) |
| 10756 |
{ |
| 10757 |
SPIRV_CROSS_THROW("GL_ARB_derivative_control is unavailable in OpenGL ES."); |
| 10758 |
} |
| 10759 |
if (options.version < 450) |
| 10760 |
require_extension_internal("GL_ARB_derivative_control"); |
| 10761 |
register_control_dependent_expression(ops[1]); |
| 10762 |
break; |
| 10763 |
|
| 10764 |
case OpDPdxCoarse: |
| 10765 |
if (options.es) |
| 10766 |
{ |
| 10767 |
SPIRV_CROSS_THROW("GL_ARB_derivative_control is unavailable in OpenGL ES."); |
| 10768 |
} |
| 10769 |
GLSL_UFOP(dFdxCoarse); |
| 10770 |
if (options.version < 450) |
| 10771 |
require_extension_internal("GL_ARB_derivative_control"); |
| 10772 |
register_control_dependent_expression(ops[1]); |
| 10773 |
break; |
| 10774 |
|
| 10775 |
case OpDPdyCoarse: |
| 10776 |
GLSL_UFOP(dFdyCoarse); |
| 10777 |
if (options.es) |
| 10778 |
{ |
| 10779 |
SPIRV_CROSS_THROW("GL_ARB_derivative_control is unavailable in OpenGL ES."); |
| 10780 |
} |
| 10781 |
if (options.version < 450) |
| 10782 |
require_extension_internal("GL_ARB_derivative_control"); |
| 10783 |
register_control_dependent_expression(ops[1]); |
| 10784 |
break; |
| 10785 |
|
| 10786 |
case OpFwidth: |
| 10787 |
GLSL_UFOP(fwidth); |
| 10788 |
if (is_legacy_es()) |
| 10789 |
require_extension_internal("GL_OES_standard_derivatives"); |
| 10790 |
register_control_dependent_expression(ops[1]); |
| 10791 |
break; |
| 10792 |
|
| 10793 |
case OpFwidthCoarse: |
| 10794 |
GLSL_UFOP(fwidthCoarse); |
| 10795 |
if (options.es) |
| 10796 |
{ |
| 10797 |
SPIRV_CROSS_THROW("GL_ARB_derivative_control is unavailable in OpenGL ES."); |
| 10798 |
} |
| 10799 |
if (options.version < 450) |
| 10800 |
require_extension_internal("GL_ARB_derivative_control"); |
| 10801 |
register_control_dependent_expression(ops[1]); |
| 10802 |
break; |
| 10803 |
|
| 10804 |
case OpFwidthFine: |
| 10805 |
GLSL_UFOP(fwidthFine); |
| 10806 |
if (options.es) |
| 10807 |
{ |
| 10808 |
SPIRV_CROSS_THROW("GL_ARB_derivative_control is unavailable in OpenGL ES."); |
| 10809 |
} |
| 10810 |
if (options.version < 450) |
| 10811 |
require_extension_internal("GL_ARB_derivative_control"); |
| 10812 |
register_control_dependent_expression(ops[1]); |
| 10813 |
break; |
| 10814 |
|
| 10815 |
// Bitfield |
| 10816 |
case OpBitFieldInsert: |
| 10817 |
{ |
| 10818 |
emit_bitfield_insert_op(ops[0], ops[1], ops[2], ops[3], ops[4], ops[5], "bitfieldInsert", SPIRType::Int); |
| 10819 |
break; |
| 10820 |
} |
| 10821 |
|
| 10822 |
case OpBitFieldSExtract: |
| 10823 |
{ |
| 10824 |
emit_trinary_func_op_bitextract(ops[0], ops[1], ops[2], ops[3], ops[4], "bitfieldExtract", int_type, int_type, |
| 10825 |
SPIRType::Int, SPIRType::Int); |
| 10826 |
break; |
| 10827 |
} |
| 10828 |
|
| 10829 |
case OpBitFieldUExtract: |
| 10830 |
{ |
| 10831 |
emit_trinary_func_op_bitextract(ops[0], ops[1], ops[2], ops[3], ops[4], "bitfieldExtract", uint_type, uint_type, |
| 10832 |
SPIRType::Int, SPIRType::Int); |
| 10833 |
break; |
| 10834 |
} |
| 10835 |
|
| 10836 |
case OpBitReverse: |
| 10837 |
// BitReverse does not have issues with sign since result type must match input type. |
| 10838 |
GLSL_UFOP(bitfieldReverse); |
| 10839 |
break; |
| 10840 |
|
| 10841 |
case OpBitCount: |
| 10842 |
{ |
| 10843 |
auto basetype = expression_type(ops[2]).basetype; |
| 10844 |
emit_unary_func_op_cast(ops[0], ops[1], ops[2], "bitCount", basetype, int_type); |
| 10845 |
break; |
| 10846 |
} |
| 10847 |
|
| 10848 |
// Atomics |
| 10849 |
case OpAtomicExchange: |
| 10850 |
{ |
| 10851 |
uint32_t result_type = ops[0]; |
| 10852 |
uint32_t id = ops[1]; |
| 10853 |
uint32_t ptr = ops[2]; |
| 10854 |
// Ignore semantics for now, probably only relevant to CL. |
| 10855 |
uint32_t val = ops[5]; |
| 10856 |
const char *op = check_atomic_image(ptr) ? "imageAtomicExchange" : "atomicExchange"; |
| 10857 |
forced_temporaries.insert(id); |
| 10858 |
emit_binary_func_op(result_type, id, ptr, val, op); |
| 10859 |
flush_all_atomic_capable_variables(); |
| 10860 |
break; |
| 10861 |
} |
| 10862 |
|
| 10863 |
case OpAtomicCompareExchange: |
| 10864 |
{ |
| 10865 |
uint32_t result_type = ops[0]; |
| 10866 |
uint32_t id = ops[1]; |
| 10867 |
uint32_t ptr = ops[2]; |
| 10868 |
uint32_t val = ops[6]; |
| 10869 |
uint32_t comp = ops[7]; |
| 10870 |
const char *op = check_atomic_image(ptr) ? "imageAtomicCompSwap" : "atomicCompSwap"; |
| 10871 |
|
| 10872 |
forced_temporaries.insert(id); |
| 10873 |
emit_trinary_func_op(result_type, id, ptr, comp, val, op); |
| 10874 |
flush_all_atomic_capable_variables(); |
| 10875 |
break; |
| 10876 |
} |
| 10877 |
|
| 10878 |
case OpAtomicLoad: |
| 10879 |
{ |
| 10880 |
// In plain GLSL, we have no atomic loads, so emulate this by fetch adding by 0 and hope compiler figures it out. |
| 10881 |
// Alternatively, we could rely on KHR_memory_model, but that's not very helpful for GL. |
| 10882 |
auto &type = expression_type(ops[2]); |
| 10883 |
forced_temporaries.insert(ops[1]); |
| 10884 |
bool atomic_image = check_atomic_image(ops[2]); |
| 10885 |
bool unsigned_type = (type.basetype == SPIRType::UInt) || |
| 10886 |
(atomic_image && get<SPIRType>(type.image.type).basetype == SPIRType::UInt); |
| 10887 |
const char *op = atomic_image ? "imageAtomicAdd" : "atomicAdd"; |
| 10888 |
const char *increment = unsigned_type ? "0u" : "0"; |
| 10889 |
emit_op(ops[0], ops[1], join(op, "(", to_expression(ops[2]), ", ", increment, ")"), false); |
| 10890 |
flush_all_atomic_capable_variables(); |
| 10891 |
break; |
| 10892 |
} |
| 10893 |
|
| 10894 |
case OpAtomicStore: |
| 10895 |
{ |
| 10896 |
// In plain GLSL, we have no atomic stores, so emulate this with an atomic exchange where we don't consume the result. |
| 10897 |
// Alternatively, we could rely on KHR_memory_model, but that's not very helpful for GL. |
| 10898 |
uint32_t ptr = ops[0]; |
| 10899 |
// Ignore semantics for now, probably only relevant to CL. |
| 10900 |
uint32_t val = ops[3]; |
| 10901 |
const char *op = check_atomic_image(ptr) ? "imageAtomicExchange" : "atomicExchange"; |
| 10902 |
statement(op, "(", to_expression(ptr), ", ", to_expression(val), ");"); |
| 10903 |
flush_all_atomic_capable_variables(); |
| 10904 |
break; |
| 10905 |
} |
| 10906 |
|
| 10907 |
case OpAtomicIIncrement: |
| 10908 |
case OpAtomicIDecrement: |
| 10909 |
{ |
| 10910 |
forced_temporaries.insert(ops[1]); |
| 10911 |
auto &type = expression_type(ops[2]); |
| 10912 |
if (type.storage == StorageClassAtomicCounter) |
| 10913 |
{ |
| 10914 |
// Legacy GLSL stuff, not sure if this is relevant to support. |
| 10915 |
if (opcode == OpAtomicIIncrement) |
| 10916 |
GLSL_UFOP(atomicCounterIncrement); |
| 10917 |
else |
| 10918 |
GLSL_UFOP(atomicCounterDecrement); |
| 10919 |
} |
| 10920 |
else |
| 10921 |
{ |
| 10922 |
bool atomic_image = check_atomic_image(ops[2]); |
| 10923 |
bool unsigned_type = (type.basetype == SPIRType::UInt) || |
| 10924 |
(atomic_image && get<SPIRType>(type.image.type).basetype == SPIRType::UInt); |
| 10925 |
const char *op = atomic_image ? "imageAtomicAdd" : "atomicAdd"; |
| 10926 |
|
| 10927 |
const char *increment = nullptr; |
| 10928 |
if (opcode == OpAtomicIIncrement && unsigned_type) |
| 10929 |
increment = "1u"; |
| 10930 |
else if (opcode == OpAtomicIIncrement) |
| 10931 |
increment = "1"; |
| 10932 |
else if (unsigned_type) |
| 10933 |
increment = "uint(-1)"; |
| 10934 |
else |
| 10935 |
increment = "-1"; |
| 10936 |
|
| 10937 |
emit_op(ops[0], ops[1], join(op, "(", to_expression(ops[2]), ", ", increment, ")"), false); |
| 10938 |
} |
| 10939 |
|
| 10940 |
flush_all_atomic_capable_variables(); |
| 10941 |
break; |
| 10942 |
} |
| 10943 |
|
| 10944 |
case OpAtomicIAdd: |
| 10945 |
{ |
| 10946 |
const char *op = check_atomic_image(ops[2]) ? "imageAtomicAdd" : "atomicAdd"; |
| 10947 |
forced_temporaries.insert(ops[1]); |
| 10948 |
emit_binary_func_op(ops[0], ops[1], ops[2], ops[5], op); |
| 10949 |
flush_all_atomic_capable_variables(); |
| 10950 |
break; |
| 10951 |
} |
| 10952 |
|
| 10953 |
case OpAtomicISub: |
| 10954 |
{ |
| 10955 |
const char *op = check_atomic_image(ops[2]) ? "imageAtomicAdd" : "atomicAdd"; |
| 10956 |
forced_temporaries.insert(ops[1]); |
| 10957 |
auto expr = join(op, "(", to_expression(ops[2]), ", -", to_enclosed_expression(ops[5]), ")"); |
| 10958 |
emit_op(ops[0], ops[1], expr, should_forward(ops[2]) && should_forward(ops[5])); |
| 10959 |
flush_all_atomic_capable_variables(); |
| 10960 |
break; |
| 10961 |
} |
| 10962 |
|
| 10963 |
case OpAtomicSMin: |
| 10964 |
case OpAtomicUMin: |
| 10965 |
{ |
| 10966 |
const char *op = check_atomic_image(ops[2]) ? "imageAtomicMin" : "atomicMin"; |
| 10967 |
forced_temporaries.insert(ops[1]); |
| 10968 |
emit_binary_func_op(ops[0], ops[1], ops[2], ops[5], op); |
| 10969 |
flush_all_atomic_capable_variables(); |
| 10970 |
break; |
| 10971 |
} |
| 10972 |
|
| 10973 |
case OpAtomicSMax: |
| 10974 |
case OpAtomicUMax: |
| 10975 |
{ |
| 10976 |
const char *op = check_atomic_image(ops[2]) ? "imageAtomicMax" : "atomicMax"; |
| 10977 |
forced_temporaries.insert(ops[1]); |
| 10978 |
emit_binary_func_op(ops[0], ops[1], ops[2], ops[5], op); |
| 10979 |
flush_all_atomic_capable_variables(); |
| 10980 |
break; |
| 10981 |
} |
| 10982 |
|
| 10983 |
case OpAtomicAnd: |
| 10984 |
{ |
| 10985 |
const char *op = check_atomic_image(ops[2]) ? "imageAtomicAnd" : "atomicAnd"; |
| 10986 |
forced_temporaries.insert(ops[1]); |
| 10987 |
emit_binary_func_op(ops[0], ops[1], ops[2], ops[5], op); |
| 10988 |
flush_all_atomic_capable_variables(); |
| 10989 |
break; |
| 10990 |
} |
| 10991 |
|
| 10992 |
case OpAtomicOr: |
| 10993 |
{ |
| 10994 |
const char *op = check_atomic_image(ops[2]) ? "imageAtomicOr" : "atomicOr"; |
| 10995 |
forced_temporaries.insert(ops[1]); |
| 10996 |
emit_binary_func_op(ops[0], ops[1], ops[2], ops[5], op); |
| 10997 |
flush_all_atomic_capable_variables(); |
| 10998 |
break; |
| 10999 |
} |
| 11000 |
|
| 11001 |
case OpAtomicXor: |
| 11002 |
{ |
| 11003 |
const char *op = check_atomic_image(ops[2]) ? "imageAtomicXor" : "atomicXor"; |
| 11004 |
forced_temporaries.insert(ops[1]); |
| 11005 |
emit_binary_func_op(ops[0], ops[1], ops[2], ops[5], op); |
| 11006 |
flush_all_atomic_capable_variables(); |
| 11007 |
break; |
| 11008 |
} |
| 11009 |
|
| 11010 |
// Geometry shaders |
| 11011 |
case OpEmitVertex: |
| 11012 |
statement("EmitVertex();"); |
| 11013 |
break; |
| 11014 |
|
| 11015 |
case OpEndPrimitive: |
| 11016 |
statement("EndPrimitive();"); |
| 11017 |
break; |
| 11018 |
|
| 11019 |
case OpEmitStreamVertex: |
| 11020 |
{ |
| 11021 |
if (options.es) |
| 11022 |
SPIRV_CROSS_THROW("Multi-stream geometry shaders not supported in ES."); |
| 11023 |
else if (!options.es && options.version < 400) |
| 11024 |
SPIRV_CROSS_THROW("Multi-stream geometry shaders only supported in GLSL 400."); |
| 11025 |
|
| 11026 |
auto stream_expr = to_expression(ops[0]); |
| 11027 |
if (expression_type(ops[0]).basetype != SPIRType::Int) |
| 11028 |
stream_expr = join("int(", stream_expr, ")"); |
| 11029 |
statement("EmitStreamVertex(", stream_expr, ");"); |
| 11030 |
break; |
| 11031 |
} |
| 11032 |
|
| 11033 |
case OpEndStreamPrimitive: |
| 11034 |
{ |
| 11035 |
if (options.es) |
| 11036 |
SPIRV_CROSS_THROW("Multi-stream geometry shaders not supported in ES."); |
| 11037 |
else if (!options.es && options.version < 400) |
| 11038 |
SPIRV_CROSS_THROW("Multi-stream geometry shaders only supported in GLSL 400."); |
| 11039 |
|
| 11040 |
auto stream_expr = to_expression(ops[0]); |
| 11041 |
if (expression_type(ops[0]).basetype != SPIRType::Int) |
| 11042 |
stream_expr = join("int(", stream_expr, ")"); |
| 11043 |
statement("EndStreamPrimitive(", stream_expr, ");"); |
| 11044 |
break; |
| 11045 |
} |
| 11046 |
|
| 11047 |
// Textures |
| 11048 |
case OpImageSampleExplicitLod: |
| 11049 |
case OpImageSampleProjExplicitLod: |
| 11050 |
case OpImageSampleDrefExplicitLod: |
| 11051 |
case OpImageSampleProjDrefExplicitLod: |
| 11052 |
case OpImageSampleImplicitLod: |
| 11053 |
case OpImageSampleProjImplicitLod: |
| 11054 |
case OpImageSampleDrefImplicitLod: |
| 11055 |
case OpImageSampleProjDrefImplicitLod: |
| 11056 |
case OpImageFetch: |
| 11057 |
case OpImageGather: |
| 11058 |
case OpImageDrefGather: |
| 11059 |
// Gets a bit hairy, so move this to a separate instruction. |
| 11060 |
emit_texture_op(instruction, false); |
| 11061 |
break; |
| 11062 |
|
| 11063 |
case OpImageSparseSampleExplicitLod: |
| 11064 |
case OpImageSparseSampleProjExplicitLod: |
| 11065 |
case OpImageSparseSampleDrefExplicitLod: |
| 11066 |
case OpImageSparseSampleProjDrefExplicitLod: |
| 11067 |
case OpImageSparseSampleImplicitLod: |
| 11068 |
case OpImageSparseSampleProjImplicitLod: |
| 11069 |
case OpImageSparseSampleDrefImplicitLod: |
| 11070 |
case OpImageSparseSampleProjDrefImplicitLod: |
| 11071 |
case OpImageSparseFetch: |
| 11072 |
case OpImageSparseGather: |
| 11073 |
case OpImageSparseDrefGather: |
| 11074 |
// Gets a bit hairy, so move this to a separate instruction. |
| 11075 |
emit_texture_op(instruction, true); |
| 11076 |
break; |
| 11077 |
|
| 11078 |
case OpImageSparseTexelsResident: |
| 11079 |
if (options.es) |
| 11080 |
SPIRV_CROSS_THROW("Sparse feedback is not supported in GLSL."); |
| 11081 |
require_extension_internal("GL_ARB_sparse_texture2"); |
| 11082 |
emit_unary_func_op_cast(ops[0], ops[1], ops[2], "sparseTexelsResidentARB", int_type, SPIRType::Boolean); |
| 11083 |
break; |
| 11084 |
|
| 11085 |
case OpImage: |
| 11086 |
{ |
| 11087 |
uint32_t result_type = ops[0]; |
| 11088 |
uint32_t id = ops[1]; |
| 11089 |
|
| 11090 |
// Suppress usage tracking. |
| 11091 |
auto &e = emit_op(result_type, id, to_expression(ops[2]), true, true); |
| 11092 |
|
| 11093 |
// When using the image, we need to know which variable it is actually loaded from. |
| 11094 |
auto *var = maybe_get_backing_variable(ops[2]); |
| 11095 |
e.loaded_from = var ? var->self : ID(0); |
| 11096 |
break; |
| 11097 |
} |
| 11098 |
|
| 11099 |
case OpImageQueryLod: |
| 11100 |
{ |
| 11101 |
if (!options.es && options.version < 400) |
| 11102 |
{ |
| 11103 |
require_extension_internal("GL_ARB_texture_query_lod"); |
| 11104 |
// For some reason, the ARB spec is all-caps. |
| 11105 |
GLSL_BFOP(textureQueryLOD); |
| 11106 |
} |
| 11107 |
else if (options.es) |
| 11108 |
SPIRV_CROSS_THROW("textureQueryLod not supported in ES profile."); |
| 11109 |
else |
| 11110 |
GLSL_BFOP(textureQueryLod); |
| 11111 |
register_control_dependent_expression(ops[1]); |
| 11112 |
break; |
| 11113 |
} |
| 11114 |
|
| 11115 |
case OpImageQueryLevels: |
| 11116 |
{ |
| 11117 |
uint32_t result_type = ops[0]; |
| 11118 |
uint32_t id = ops[1]; |
| 11119 |
|
| 11120 |
if (!options.es && options.version < 430) |
| 11121 |
require_extension_internal("GL_ARB_texture_query_levels"); |
| 11122 |
if (options.es) |
| 11123 |
SPIRV_CROSS_THROW("textureQueryLevels not supported in ES profile."); |
| 11124 |
|
| 11125 |
auto expr = join("textureQueryLevels(", convert_separate_image_to_expression(ops[2]), ")"); |
| 11126 |
auto &restype = get<SPIRType>(ops[0]); |
| 11127 |
expr = bitcast_expression(restype, SPIRType::Int, expr); |
| 11128 |
emit_op(result_type, id, expr, true); |
| 11129 |
break; |
| 11130 |
} |
| 11131 |
|
| 11132 |
case OpImageQuerySamples: |
| 11133 |
{ |
| 11134 |
auto &type = expression_type(ops[2]); |
| 11135 |
uint32_t result_type = ops[0]; |
| 11136 |
uint32_t id = ops[1]; |
| 11137 |
|
| 11138 |
string expr; |
| 11139 |
if (type.image.sampled == 2) |
| 11140 |
expr = join("imageSamples(", to_expression(ops[2]), ")"); |
| 11141 |
else |
| 11142 |
expr = join("textureSamples(", convert_separate_image_to_expression(ops[2]), ")"); |
| 11143 |
|
| 11144 |
auto &restype = get<SPIRType>(ops[0]); |
| 11145 |
expr = bitcast_expression(restype, SPIRType::Int, expr); |
| 11146 |
emit_op(result_type, id, expr, true); |
| 11147 |
break; |
| 11148 |
} |
| 11149 |
|
| 11150 |
case OpSampledImage: |
| 11151 |
{ |
| 11152 |
uint32_t result_type = ops[0]; |
| 11153 |
uint32_t id = ops[1]; |
| 11154 |
emit_sampled_image_op(result_type, id, ops[2], ops[3]); |
| 11155 |
inherit_expression_dependencies(id, ops[2]); |
| 11156 |
inherit_expression_dependencies(id, ops[3]); |
| 11157 |
break; |
| 11158 |
} |
| 11159 |
|
| 11160 |
case OpImageQuerySizeLod: |
| 11161 |
{ |
| 11162 |
uint32_t result_type = ops[0]; |
| 11163 |
uint32_t id = ops[1]; |
| 11164 |
|
| 11165 |
auto expr = join("textureSize(", convert_separate_image_to_expression(ops[2]), ", ", |
| 11166 |
bitcast_expression(SPIRType::Int, ops[3]), ")"); |
| 11167 |
auto &restype = get<SPIRType>(ops[0]); |
| 11168 |
expr = bitcast_expression(restype, SPIRType::Int, expr); |
| 11169 |
emit_op(result_type, id, expr, true); |
| 11170 |
break; |
| 11171 |
} |
| 11172 |
|
| 11173 |
// Image load/store |
| 11174 |
case OpImageRead: |
| 11175 |
case OpImageSparseRead: |
| 11176 |
{ |
| 11177 |
// We added Nonreadable speculatively to the OpImage variable due to glslangValidator |
| 11178 |
// not adding the proper qualifiers. |
| 11179 |
// If it turns out we need to read the image after all, remove the qualifier and recompile. |
| 11180 |
auto *var = maybe_get_backing_variable(ops[2]); |
| 11181 |
if (var) |
| 11182 |
{ |
| 11183 |
auto &flags = ir.meta[var->self].decoration.decoration_flags; |
| 11184 |
if (flags.get(DecorationNonReadable)) |
| 11185 |
{ |
| 11186 |
flags.clear(DecorationNonReadable); |
| 11187 |
force_recompile(); |
| 11188 |
} |
| 11189 |
} |
| 11190 |
|
| 11191 |
uint32_t result_type = ops[0]; |
| 11192 |
uint32_t id = ops[1]; |
| 11193 |
|
| 11194 |
bool pure; |
| 11195 |
string imgexpr; |
| 11196 |
auto &type = expression_type(ops[2]); |
| 11197 |
|
| 11198 |
if (var && var->remapped_variable) // Remapped input, just read as-is without any op-code |
| 11199 |
{ |
| 11200 |
if (type.image.ms) |
| 11201 |
SPIRV_CROSS_THROW("Trying to remap multisampled image to variable, this is not possible."); |
| 11202 |
|
| 11203 |
auto itr = |
| 11204 |
find_if(begin(pls_inputs), end(pls_inputs), [var](const PlsRemap &pls) { return pls.id == var->self; }); |
| 11205 |
|
| 11206 |
if (itr == end(pls_inputs)) |
| 11207 |
{ |
| 11208 |
// For non-PLS inputs, we rely on subpass type remapping information to get it right |
| 11209 |
// since ImageRead always returns 4-component vectors and the backing type is opaque. |
| 11210 |
if (!var->remapped_components) |
| 11211 |
SPIRV_CROSS_THROW("subpassInput was remapped, but remap_components is not set correctly."); |
| 11212 |
imgexpr = remap_swizzle(get<SPIRType>(result_type), var->remapped_components, to_expression(ops[2])); |
| 11213 |
} |
| 11214 |
else |
| 11215 |
{ |
| 11216 |
// PLS input could have different number of components than what the SPIR expects, swizzle to |
| 11217 |
// the appropriate vector size. |
| 11218 |
uint32_t components = pls_format_to_components(itr->format); |
| 11219 |
imgexpr = remap_swizzle(get<SPIRType>(result_type), components, to_expression(ops[2])); |
| 11220 |
} |
| 11221 |
pure = true; |
| 11222 |
} |
| 11223 |
else if (type.image.dim == DimSubpassData) |
| 11224 |
{ |
| 11225 |
if (var && subpass_input_is_framebuffer_fetch(var->self)) |
| 11226 |
{ |
| 11227 |
imgexpr = to_expression(var->self); |
| 11228 |
} |
| 11229 |
else if (options.vulkan_semantics) |
| 11230 |
{ |
| 11231 |
// With Vulkan semantics, use the proper Vulkan GLSL construct. |
| 11232 |
if (type.image.ms) |
| 11233 |
{ |
| 11234 |
uint32_t operands = ops[4]; |
| 11235 |
if (operands != ImageOperandsSampleMask || length != 6) |
| 11236 |
SPIRV_CROSS_THROW("Multisampled image used in OpImageRead, but unexpected " |
| 11237 |
"operand mask was used."); |
| 11238 |
|
| 11239 |
uint32_t samples = ops[5]; |
| 11240 |
imgexpr = join("subpassLoad(", to_expression(ops[2]), ", ", to_expression(samples), ")"); |
| 11241 |
} |
| 11242 |
else |
| 11243 |
imgexpr = join("subpassLoad(", to_expression(ops[2]), ")"); |
| 11244 |
} |
| 11245 |
else |
| 11246 |
{ |
| 11247 |
if (type.image.ms) |
| 11248 |
{ |
| 11249 |
uint32_t operands = ops[4]; |
| 11250 |
if (operands != ImageOperandsSampleMask || length != 6) |
| 11251 |
SPIRV_CROSS_THROW("Multisampled image used in OpImageRead, but unexpected " |
| 11252 |
"operand mask was used."); |
| 11253 |
|
| 11254 |
uint32_t samples = ops[5]; |
| 11255 |
imgexpr = join("texelFetch(", to_expression(ops[2]), ", ivec2(gl_FragCoord.xy), ", |
| 11256 |
to_expression(samples), ")"); |
| 11257 |
} |
| 11258 |
else |
| 11259 |
{ |
| 11260 |
// Implement subpass loads via texture barrier style sampling. |
| 11261 |
imgexpr = join("texelFetch(", to_expression(ops[2]), ", ivec2(gl_FragCoord.xy), 0)"); |
| 11262 |
} |
| 11263 |
} |
| 11264 |
imgexpr = remap_swizzle(get<SPIRType>(result_type), 4, imgexpr); |
| 11265 |
pure = true; |
| 11266 |
} |
| 11267 |
else |
| 11268 |
{ |
| 11269 |
bool sparse = opcode == OpImageSparseRead; |
| 11270 |
uint32_t sparse_code_id = 0; |
| 11271 |
uint32_t sparse_texel_id = 0; |
| 11272 |
if (sparse) |
| 11273 |
emit_sparse_feedback_temporaries(ops[0], ops[1], sparse_code_id, sparse_texel_id); |
| 11274 |
|
| 11275 |
// imageLoad only accepts int coords, not uint. |
| 11276 |
auto coord_expr = to_expression(ops[3]); |
| 11277 |
auto target_coord_type = expression_type(ops[3]); |
| 11278 |
target_coord_type.basetype = SPIRType::Int; |
| 11279 |
coord_expr = bitcast_expression(target_coord_type, expression_type(ops[3]).basetype, coord_expr); |
| 11280 |
|
| 11281 |
// Plain image load/store. |
| 11282 |
if (sparse) |
| 11283 |
{ |
| 11284 |
if (type.image.ms) |
| 11285 |
{ |
| 11286 |
uint32_t operands = ops[4]; |
| 11287 |
if (operands != ImageOperandsSampleMask || length != 6) |
| 11288 |
SPIRV_CROSS_THROW("Multisampled image used in OpImageRead, but unexpected " |
| 11289 |
"operand mask was used."); |
| 11290 |
|
| 11291 |
uint32_t samples = ops[5]; |
| 11292 |
statement(to_expression(sparse_code_id), " = sparseImageLoadARB(", to_expression(ops[2]), ", ", |
| 11293 |
coord_expr, ", ", to_expression(samples), ", ", to_expression(sparse_texel_id), ");"); |
| 11294 |
} |
| 11295 |
else |
| 11296 |
{ |
| 11297 |
statement(to_expression(sparse_code_id), " = sparseImageLoadARB(", to_expression(ops[2]), ", ", |
| 11298 |
coord_expr, ", ", to_expression(sparse_texel_id), ");"); |
| 11299 |
} |
| 11300 |
imgexpr = join(type_to_glsl(get<SPIRType>(result_type)), "(", to_expression(sparse_code_id), ", ", |
| 11301 |
to_expression(sparse_texel_id), ")"); |
| 11302 |
} |
| 11303 |
else |
| 11304 |
{ |
| 11305 |
if (type.image.ms) |
| 11306 |
{ |
| 11307 |
uint32_t operands = ops[4]; |
| 11308 |
if (operands != ImageOperandsSampleMask || length != 6) |
| 11309 |
SPIRV_CROSS_THROW("Multisampled image used in OpImageRead, but unexpected " |
| 11310 |
"operand mask was used."); |
| 11311 |
|
| 11312 |
uint32_t samples = ops[5]; |
| 11313 |
imgexpr = |
| 11314 |
join("imageLoad(", to_expression(ops[2]), ", ", coord_expr, ", ", to_expression(samples), ")"); |
| 11315 |
} |
| 11316 |
else |
| 11317 |
imgexpr = join("imageLoad(", to_expression(ops[2]), ", ", coord_expr, ")"); |
| 11318 |
} |
| 11319 |
|
| 11320 |
if (!sparse) |
| 11321 |
imgexpr = remap_swizzle(get<SPIRType>(result_type), 4, imgexpr); |
| 11322 |
pure = false; |
| 11323 |
} |
| 11324 |
|
| 11325 |
if (var && var->forwardable) |
| 11326 |
{ |
| 11327 |
bool forward = forced_temporaries.find(id) == end(forced_temporaries); |
| 11328 |
auto &e = emit_op(result_type, id, imgexpr, forward); |
| 11329 |
|
| 11330 |
// We only need to track dependencies if we're reading from image load/store. |
| 11331 |
if (!pure) |
| 11332 |
{ |
| 11333 |
e.loaded_from = var->self; |
| 11334 |
if (forward) |
| 11335 |
var->dependees.push_back(id); |
| 11336 |
} |
| 11337 |
} |
| 11338 |
else |
| 11339 |
emit_op(result_type, id, imgexpr, false); |
| 11340 |
|
| 11341 |
inherit_expression_dependencies(id, ops[2]); |
| 11342 |
if (type.image.ms) |
| 11343 |
inherit_expression_dependencies(id, ops[5]); |
| 11344 |
break; |
| 11345 |
} |
| 11346 |
|
| 11347 |
case OpImageTexelPointer: |
| 11348 |
{ |
| 11349 |
uint32_t result_type = ops[0]; |
| 11350 |
uint32_t id = ops[1]; |
| 11351 |
|
| 11352 |
auto coord_expr = to_expression(ops[3]); |
| 11353 |
auto target_coord_type = expression_type(ops[3]); |
| 11354 |
target_coord_type.basetype = SPIRType::Int; |
| 11355 |
coord_expr = bitcast_expression(target_coord_type, expression_type(ops[3]).basetype, coord_expr); |
| 11356 |
|
| 11357 |
auto expr = join(to_expression(ops[2]), ", ", coord_expr); |
| 11358 |
if (has_decoration(id, DecorationNonUniformEXT) || has_decoration(ops[2], DecorationNonUniformEXT)) |
| 11359 |
convert_non_uniform_expression(expression_type(ops[2]), expr); |
| 11360 |
|
| 11361 |
auto &e = set<SPIRExpression>(id, expr, result_type, true); |
| 11362 |
|
| 11363 |
// When using the pointer, we need to know which variable it is actually loaded from. |
| 11364 |
auto *var = maybe_get_backing_variable(ops[2]); |
| 11365 |
e.loaded_from = var ? var->self : ID(0); |
| 11366 |
inherit_expression_dependencies(id, ops[3]); |
| 11367 |
break; |
| 11368 |
} |
| 11369 |
|
| 11370 |
case OpImageWrite: |
| 11371 |
{ |
| 11372 |
// We added Nonwritable speculatively to the OpImage variable due to glslangValidator |
| 11373 |
// not adding the proper qualifiers. |
| 11374 |
// If it turns out we need to write to the image after all, remove the qualifier and recompile. |
| 11375 |
auto *var = maybe_get_backing_variable(ops[0]); |
| 11376 |
if (var) |
| 11377 |
{ |
| 11378 |
auto &flags = ir.meta[var->self].decoration.decoration_flags; |
| 11379 |
if (flags.get(DecorationNonWritable)) |
| 11380 |
{ |
| 11381 |
flags.clear(DecorationNonWritable); |
| 11382 |
force_recompile(); |
| 11383 |
} |
| 11384 |
} |
| 11385 |
|
| 11386 |
auto &type = expression_type(ops[0]); |
| 11387 |
auto &value_type = expression_type(ops[2]); |
| 11388 |
auto store_type = value_type; |
| 11389 |
store_type.vecsize = 4; |
| 11390 |
|
| 11391 |
// imageStore only accepts int coords, not uint. |
| 11392 |
auto coord_expr = to_expression(ops[1]); |
| 11393 |
auto target_coord_type = expression_type(ops[1]); |
| 11394 |
target_coord_type.basetype = SPIRType::Int; |
| 11395 |
coord_expr = bitcast_expression(target_coord_type, expression_type(ops[1]).basetype, coord_expr); |
| 11396 |
|
| 11397 |
if (type.image.ms) |
| 11398 |
{ |
| 11399 |
uint32_t operands = ops[3]; |
| 11400 |
if (operands != ImageOperandsSampleMask || length != 5) |
| 11401 |
SPIRV_CROSS_THROW("Multisampled image used in OpImageWrite, but unexpected operand mask was used."); |
| 11402 |
uint32_t samples = ops[4]; |
| 11403 |
statement("imageStore(", to_expression(ops[0]), ", ", coord_expr, ", ", to_expression(samples), ", ", |
| 11404 |
remap_swizzle(store_type, value_type.vecsize, to_expression(ops[2])), ");"); |
| 11405 |
} |
| 11406 |
else |
| 11407 |
statement("imageStore(", to_expression(ops[0]), ", ", coord_expr, ", ", |
| 11408 |
remap_swizzle(store_type, value_type.vecsize, to_expression(ops[2])), ");"); |
| 11409 |
|
| 11410 |
if (var && variable_storage_is_aliased(*var)) |
| 11411 |
flush_all_aliased_variables(); |
| 11412 |
break; |
| 11413 |
} |
| 11414 |
|
| 11415 |
case OpImageQuerySize: |
| 11416 |
{ |
| 11417 |
auto &type = expression_type(ops[2]); |
| 11418 |
uint32_t result_type = ops[0]; |
| 11419 |
uint32_t id = ops[1]; |
| 11420 |
|
| 11421 |
if (type.basetype == SPIRType::Image) |
| 11422 |
{ |
| 11423 |
string expr; |
| 11424 |
if (type.image.sampled == 2) |
| 11425 |
{ |
| 11426 |
// The size of an image is always constant. |
| 11427 |
expr = join("imageSize(", to_expression(ops[2]), ")"); |
| 11428 |
} |
| 11429 |
else |
| 11430 |
{ |
| 11431 |
// This path is hit for samplerBuffers and multisampled images which do not have LOD. |
| 11432 |
expr = join("textureSize(", convert_separate_image_to_expression(ops[2]), ")"); |
| 11433 |
} |
| 11434 |
|
| 11435 |
auto &restype = get<SPIRType>(ops[0]); |
| 11436 |
expr = bitcast_expression(restype, SPIRType::Int, expr); |
| 11437 |
emit_op(result_type, id, expr, true); |
| 11438 |
} |
| 11439 |
else |
| 11440 |
SPIRV_CROSS_THROW("Invalid type for OpImageQuerySize."); |
| 11441 |
break; |
| 11442 |
} |
| 11443 |
|
| 11444 |
// Compute |
| 11445 |
case OpControlBarrier: |
| 11446 |
case OpMemoryBarrier: |
| 11447 |
{ |
| 11448 |
uint32_t execution_scope = 0; |
| 11449 |
uint32_t memory; |
| 11450 |
uint32_t semantics; |
| 11451 |
|
| 11452 |
if (opcode == OpMemoryBarrier) |
| 11453 |
{ |
| 11454 |
memory = evaluate_constant_u32(ops[0]); |
| 11455 |
semantics = evaluate_constant_u32(ops[1]); |
| 11456 |
} |
| 11457 |
else |
| 11458 |
{ |
| 11459 |
execution_scope = evaluate_constant_u32(ops[0]); |
| 11460 |
memory = evaluate_constant_u32(ops[1]); |
| 11461 |
semantics = evaluate_constant_u32(ops[2]); |
| 11462 |
} |
| 11463 |
|
| 11464 |
if (execution_scope == ScopeSubgroup || memory == ScopeSubgroup) |
| 11465 |
{ |
| 11466 |
// OpControlBarrier with ScopeSubgroup is subgroupBarrier() |
| 11467 |
if (opcode != OpControlBarrier) |
| 11468 |
{ |
| 11469 |
request_subgroup_feature(ShaderSubgroupSupportHelper::SubgroupMemBarrier); |
| 11470 |
} |
| 11471 |
else |
| 11472 |
{ |
| 11473 |
request_subgroup_feature(ShaderSubgroupSupportHelper::SubgroupBarrier); |
| 11474 |
} |
| 11475 |
} |
| 11476 |
|
| 11477 |
if (execution_scope != ScopeSubgroup && get_entry_point().model == ExecutionModelTessellationControl) |
| 11478 |
{ |
| 11479 |
// Control shaders only have barriers, and it implies memory barriers. |
| 11480 |
if (opcode == OpControlBarrier) |
| 11481 |
statement("barrier();"); |
| 11482 |
break; |
| 11483 |
} |
| 11484 |
|
| 11485 |
// We only care about these flags, acquire/release and friends are not relevant to GLSL. |
| 11486 |
semantics = mask_relevant_memory_semantics(semantics); |
| 11487 |
|
| 11488 |
if (opcode == OpMemoryBarrier) |
| 11489 |
{ |
| 11490 |
// If we are a memory barrier, and the next instruction is a control barrier, check if that memory barrier |
| 11491 |
// does what we need, so we avoid redundant barriers. |
| 11492 |
const Instruction *next = get_next_instruction_in_block(instruction); |
| 11493 |
if (next && next->op == OpControlBarrier) |
| 11494 |
{ |
| 11495 |
auto *next_ops = stream(*next); |
| 11496 |
uint32_t next_memory = evaluate_constant_u32(next_ops[1]); |
| 11497 |
uint32_t next_semantics = evaluate_constant_u32(next_ops[2]); |
| 11498 |
next_semantics = mask_relevant_memory_semantics(next_semantics); |
| 11499 |
|
| 11500 |
bool memory_scope_covered = false; |
| 11501 |
if (next_memory == memory) |
| 11502 |
memory_scope_covered = true; |
| 11503 |
else if (next_semantics == MemorySemanticsWorkgroupMemoryMask) |
| 11504 |
{ |
| 11505 |
// If we only care about workgroup memory, either Device or Workgroup scope is fine, |
| 11506 |
// scope does not have to match. |
| 11507 |
if ((next_memory == ScopeDevice || next_memory == ScopeWorkgroup) && |
| 11508 |
(memory == ScopeDevice || memory == ScopeWorkgroup)) |
| 11509 |
{ |
| 11510 |
memory_scope_covered = true; |
| 11511 |
} |
| 11512 |
} |
| 11513 |
else if (memory == ScopeWorkgroup && next_memory == ScopeDevice) |
| 11514 |
{ |
| 11515 |
// The control barrier has device scope, but the memory barrier just has workgroup scope. |
| 11516 |
memory_scope_covered = true; |
| 11517 |
} |
| 11518 |
|
| 11519 |
// If we have the same memory scope, and all memory types are covered, we're good. |
| 11520 |
if (memory_scope_covered && (semantics & next_semantics) == semantics) |
| 11521 |
break; |
| 11522 |
} |
| 11523 |
} |
| 11524 |
|
| 11525 |
// We are synchronizing some memory or syncing execution, |
| 11526 |
// so we cannot forward any loads beyond the memory barrier. |
| 11527 |
if (semantics || opcode == OpControlBarrier) |
| 11528 |
{ |
| 11529 |
assert(current_emitting_block); |
| 11530 |
flush_control_dependent_expressions(current_emitting_block->self); |
| 11531 |
flush_all_active_variables(); |
| 11532 |
} |
| 11533 |
|
| 11534 |
if (memory == ScopeWorkgroup) // Only need to consider memory within a group |
| 11535 |
{ |
| 11536 |
if (semantics == MemorySemanticsWorkgroupMemoryMask) |
| 11537 |
{ |
| 11538 |
// OpControlBarrier implies a memory barrier for shared memory as well. |
| 11539 |
bool implies_shared_barrier = opcode == OpControlBarrier && execution_scope == ScopeWorkgroup; |
| 11540 |
if (!implies_shared_barrier) |
| 11541 |
statement("memoryBarrierShared();"); |
| 11542 |
} |
| 11543 |
else if (semantics != 0) |
| 11544 |
statement("groupMemoryBarrier();"); |
| 11545 |
} |
| 11546 |
else if (memory == ScopeSubgroup) |
| 11547 |
{ |
| 11548 |
const uint32_t all_barriers = |
| 11549 |
MemorySemanticsWorkgroupMemoryMask | MemorySemanticsUniformMemoryMask | MemorySemanticsImageMemoryMask; |
| 11550 |
|
| 11551 |
if (semantics & (MemorySemanticsCrossWorkgroupMemoryMask | MemorySemanticsSubgroupMemoryMask)) |
| 11552 |
{ |
| 11553 |
// These are not relevant for GLSL, but assume it means memoryBarrier(). |
| 11554 |
// memoryBarrier() does everything, so no need to test anything else. |
| 11555 |
statement("subgroupMemoryBarrier();"); |
| 11556 |
} |
| 11557 |
else if ((semantics & all_barriers) == all_barriers) |
| 11558 |
{ |
| 11559 |
// Short-hand instead of emitting 3 barriers. |
| 11560 |
statement("subgroupMemoryBarrier();"); |
| 11561 |
} |
| 11562 |
else |
| 11563 |
{ |
| 11564 |
// Pick out individual barriers. |
| 11565 |
if (semantics & MemorySemanticsWorkgroupMemoryMask) |
| 11566 |
statement("subgroupMemoryBarrierShared();"); |
| 11567 |
if (semantics & MemorySemanticsUniformMemoryMask) |
| 11568 |
statement("subgroupMemoryBarrierBuffer();"); |
| 11569 |
if (semantics & MemorySemanticsImageMemoryMask) |
| 11570 |
statement("subgroupMemoryBarrierImage();"); |
| 11571 |
} |
| 11572 |
} |
| 11573 |
else |
| 11574 |
{ |
| 11575 |
const uint32_t all_barriers = |
| 11576 |
MemorySemanticsWorkgroupMemoryMask | MemorySemanticsUniformMemoryMask | MemorySemanticsImageMemoryMask; |
| 11577 |
|
| 11578 |
if (semantics & (MemorySemanticsCrossWorkgroupMemoryMask | MemorySemanticsSubgroupMemoryMask)) |
| 11579 |
{ |
| 11580 |
// These are not relevant for GLSL, but assume it means memoryBarrier(). |
| 11581 |
// memoryBarrier() does everything, so no need to test anything else. |
| 11582 |
statement("memoryBarrier();"); |
| 11583 |
} |
| 11584 |
else if ((semantics & all_barriers) == all_barriers) |
| 11585 |
{ |
| 11586 |
// Short-hand instead of emitting 4 barriers. |
| 11587 |
statement("memoryBarrier();"); |
| 11588 |
} |
| 11589 |
else |
| 11590 |
{ |
| 11591 |
// Pick out individual barriers. |
| 11592 |
if (semantics & MemorySemanticsWorkgroupMemoryMask) |
| 11593 |
statement("memoryBarrierShared();"); |
| 11594 |
if (semantics & MemorySemanticsUniformMemoryMask) |
| 11595 |
statement("memoryBarrierBuffer();"); |
| 11596 |
if (semantics & MemorySemanticsImageMemoryMask) |
| 11597 |
statement("memoryBarrierImage();"); |
| 11598 |
} |
| 11599 |
} |
| 11600 |
|
| 11601 |
if (opcode == OpControlBarrier) |
| 11602 |
{ |
| 11603 |
if (execution_scope == ScopeSubgroup) |
| 11604 |
statement("subgroupBarrier();"); |
| 11605 |
else |
| 11606 |
statement("barrier();"); |
| 11607 |
} |
| 11608 |
break; |
| 11609 |
} |
| 11610 |
|
| 11611 |
case OpExtInst: |
| 11612 |
{ |
| 11613 |
uint32_t extension_set = ops[2]; |
| 11614 |
|
| 11615 |
if (get<SPIRExtension>(extension_set).ext == SPIRExtension::GLSL) |
| 11616 |
{ |
| 11617 |
emit_glsl_op(ops[0], ops[1], ops[3], &ops[4], length - 4); |
| 11618 |
} |
| 11619 |
else if (get<SPIRExtension>(extension_set).ext == SPIRExtension::SPV_AMD_shader_ballot) |
| 11620 |
{ |
| 11621 |
emit_spv_amd_shader_ballot_op(ops[0], ops[1], ops[3], &ops[4], length - 4); |
| 11622 |
} |
| 11623 |
else if (get<SPIRExtension>(extension_set).ext == SPIRExtension::SPV_AMD_shader_explicit_vertex_parameter) |
| 11624 |
{ |
| 11625 |
emit_spv_amd_shader_explicit_vertex_parameter_op(ops[0], ops[1], ops[3], &ops[4], length - 4); |
| 11626 |
} |
| 11627 |
else if (get<SPIRExtension>(extension_set).ext == SPIRExtension::SPV_AMD_shader_trinary_minmax) |
| 11628 |
{ |
| 11629 |
emit_spv_amd_shader_trinary_minmax_op(ops[0], ops[1], ops[3], &ops[4], length - 4); |
| 11630 |
} |
| 11631 |
else if (get<SPIRExtension>(extension_set).ext == SPIRExtension::SPV_AMD_gcn_shader) |
| 11632 |
{ |
| 11633 |
emit_spv_amd_gcn_shader_op(ops[0], ops[1], ops[3], &ops[4], length - 4); |
| 11634 |
} |
| 11635 |
else if (get<SPIRExtension>(extension_set).ext == SPIRExtension::SPV_debug_info) |
| 11636 |
{ |
| 11637 |
break; // Ignore SPIR-V debug information extended instructions. |
| 11638 |
} |
| 11639 |
else |
| 11640 |
{ |
| 11641 |
statement("// unimplemented ext op ", instruction.op); |
| 11642 |
break; |
| 11643 |
} |
| 11644 |
|
| 11645 |
break; |
| 11646 |
} |
| 11647 |
|
| 11648 |
// Legacy sub-group stuff ... |
| 11649 |
case OpSubgroupBallotKHR: |
| 11650 |
{ |
| 11651 |
uint32_t result_type = ops[0]; |
| 11652 |
uint32_t id = ops[1]; |
| 11653 |
string expr; |
| 11654 |
expr = join("uvec4(unpackUint2x32(ballotARB(" + to_expression(ops[2]) + ")), 0u, 0u)"); |
| 11655 |
emit_op(result_type, id, expr, should_forward(ops[2])); |
| 11656 |
|
| 11657 |
require_extension_internal("GL_ARB_shader_ballot"); |
| 11658 |
inherit_expression_dependencies(id, ops[2]); |
| 11659 |
register_control_dependent_expression(ops[1]); |
| 11660 |
break; |
| 11661 |
} |
| 11662 |
|
| 11663 |
case OpSubgroupFirstInvocationKHR: |
| 11664 |
{ |
| 11665 |
uint32_t result_type = ops[0]; |
| 11666 |
uint32_t id = ops[1]; |
| 11667 |
emit_unary_func_op(result_type, id, ops[2], "readFirstInvocationARB"); |
| 11668 |
|
| 11669 |
require_extension_internal("GL_ARB_shader_ballot"); |
| 11670 |
register_control_dependent_expression(ops[1]); |
| 11671 |
break; |
| 11672 |
} |
| 11673 |
|
| 11674 |
case OpSubgroupReadInvocationKHR: |
| 11675 |
{ |
| 11676 |
uint32_t result_type = ops[0]; |
| 11677 |
uint32_t id = ops[1]; |
| 11678 |
emit_binary_func_op(result_type, id, ops[2], ops[3], "readInvocationARB"); |
| 11679 |
|
| 11680 |
require_extension_internal("GL_ARB_shader_ballot"); |
| 11681 |
register_control_dependent_expression(ops[1]); |
| 11682 |
break; |
| 11683 |
} |
| 11684 |
|
| 11685 |
case OpSubgroupAllKHR: |
| 11686 |
{ |
| 11687 |
uint32_t result_type = ops[0]; |
| 11688 |
uint32_t id = ops[1]; |
| 11689 |
emit_unary_func_op(result_type, id, ops[2], "allInvocationsARB"); |
| 11690 |
|
| 11691 |
require_extension_internal("GL_ARB_shader_group_vote"); |
| 11692 |
register_control_dependent_expression(ops[1]); |
| 11693 |
break; |
| 11694 |
} |
| 11695 |
|
| 11696 |
case OpSubgroupAnyKHR: |
| 11697 |
{ |
| 11698 |
uint32_t result_type = ops[0]; |
| 11699 |
uint32_t id = ops[1]; |
| 11700 |
emit_unary_func_op(result_type, id, ops[2], "anyInvocationARB"); |
| 11701 |
|
| 11702 |
require_extension_internal("GL_ARB_shader_group_vote"); |
| 11703 |
register_control_dependent_expression(ops[1]); |
| 11704 |
break; |
| 11705 |
} |
| 11706 |
|
| 11707 |
case OpSubgroupAllEqualKHR: |
| 11708 |
{ |
| 11709 |
uint32_t result_type = ops[0]; |
| 11710 |
uint32_t id = ops[1]; |
| 11711 |
emit_unary_func_op(result_type, id, ops[2], "allInvocationsEqualARB"); |
| 11712 |
|
| 11713 |
require_extension_internal("GL_ARB_shader_group_vote"); |
| 11714 |
register_control_dependent_expression(ops[1]); |
| 11715 |
break; |
| 11716 |
} |
| 11717 |
|
| 11718 |
case OpGroupIAddNonUniformAMD: |
| 11719 |
case OpGroupFAddNonUniformAMD: |
| 11720 |
{ |
| 11721 |
uint32_t result_type = ops[0]; |
| 11722 |
uint32_t id = ops[1]; |
| 11723 |
emit_unary_func_op(result_type, id, ops[4], "addInvocationsNonUniformAMD"); |
| 11724 |
|
| 11725 |
require_extension_internal("GL_AMD_shader_ballot"); |
| 11726 |
register_control_dependent_expression(ops[1]); |
| 11727 |
break; |
| 11728 |
} |
| 11729 |
|
| 11730 |
case OpGroupFMinNonUniformAMD: |
| 11731 |
case OpGroupUMinNonUniformAMD: |
| 11732 |
case OpGroupSMinNonUniformAMD: |
| 11733 |
{ |
| 11734 |
uint32_t result_type = ops[0]; |
| 11735 |
uint32_t id = ops[1]; |
| 11736 |
emit_unary_func_op(result_type, id, ops[4], "minInvocationsNonUniformAMD"); |
| 11737 |
|
| 11738 |
require_extension_internal("GL_AMD_shader_ballot"); |
| 11739 |
register_control_dependent_expression(ops[1]); |
| 11740 |
break; |
| 11741 |
} |
| 11742 |
|
| 11743 |
case OpGroupFMaxNonUniformAMD: |
| 11744 |
case OpGroupUMaxNonUniformAMD: |
| 11745 |
case OpGroupSMaxNonUniformAMD: |
| 11746 |
{ |
| 11747 |
uint32_t result_type = ops[0]; |
| 11748 |
uint32_t id = ops[1]; |
| 11749 |
emit_unary_func_op(result_type, id, ops[4], "maxInvocationsNonUniformAMD"); |
| 11750 |
|
| 11751 |
require_extension_internal("GL_AMD_shader_ballot"); |
| 11752 |
register_control_dependent_expression(ops[1]); |
| 11753 |
break; |
| 11754 |
} |
| 11755 |
|
| 11756 |
case OpFragmentMaskFetchAMD: |
| 11757 |
{ |
| 11758 |
auto &type = expression_type(ops[2]); |
| 11759 |
uint32_t result_type = ops[0]; |
| 11760 |
uint32_t id = ops[1]; |
| 11761 |
|
| 11762 |
if (type.image.dim == spv::DimSubpassData) |
| 11763 |
{ |
| 11764 |
emit_unary_func_op(result_type, id, ops[2], "fragmentMaskFetchAMD"); |
| 11765 |
} |
| 11766 |
else |
| 11767 |
{ |
| 11768 |
emit_binary_func_op(result_type, id, ops[2], ops[3], "fragmentMaskFetchAMD"); |
| 11769 |
} |
| 11770 |
|
| 11771 |
require_extension_internal("GL_AMD_shader_fragment_mask"); |
| 11772 |
break; |
| 11773 |
} |
| 11774 |
|
| 11775 |
case OpFragmentFetchAMD: |
| 11776 |
{ |
| 11777 |
auto &type = expression_type(ops[2]); |
| 11778 |
uint32_t result_type = ops[0]; |
| 11779 |
uint32_t id = ops[1]; |
| 11780 |
|
| 11781 |
if (type.image.dim == spv::DimSubpassData) |
| 11782 |
{ |
| 11783 |
emit_binary_func_op(result_type, id, ops[2], ops[4], "fragmentFetchAMD"); |
| 11784 |
} |
| 11785 |
else |
| 11786 |
{ |
| 11787 |
emit_trinary_func_op(result_type, id, ops[2], ops[3], ops[4], "fragmentFetchAMD"); |
| 11788 |
} |
| 11789 |
|
| 11790 |
require_extension_internal("GL_AMD_shader_fragment_mask"); |
| 11791 |
break; |
| 11792 |
} |
| 11793 |
|
| 11794 |
// Vulkan 1.1 sub-group stuff ... |
| 11795 |
case OpGroupNonUniformElect: |
| 11796 |
case OpGroupNonUniformBroadcast: |
| 11797 |
case OpGroupNonUniformBroadcastFirst: |
| 11798 |
case OpGroupNonUniformBallot: |
| 11799 |
case OpGroupNonUniformInverseBallot: |
| 11800 |
case OpGroupNonUniformBallotBitExtract: |
| 11801 |
case OpGroupNonUniformBallotBitCount: |
| 11802 |
case OpGroupNonUniformBallotFindLSB: |
| 11803 |
case OpGroupNonUniformBallotFindMSB: |
| 11804 |
case OpGroupNonUniformShuffle: |
| 11805 |
case OpGroupNonUniformShuffleXor: |
| 11806 |
case OpGroupNonUniformShuffleUp: |
| 11807 |
case OpGroupNonUniformShuffleDown: |
| 11808 |
case OpGroupNonUniformAll: |
| 11809 |
case OpGroupNonUniformAny: |
| 11810 |
case OpGroupNonUniformAllEqual: |
| 11811 |
case OpGroupNonUniformFAdd: |
| 11812 |
case OpGroupNonUniformIAdd: |
| 11813 |
case OpGroupNonUniformFMul: |
| 11814 |
case OpGroupNonUniformIMul: |
| 11815 |
case OpGroupNonUniformFMin: |
| 11816 |
case OpGroupNonUniformFMax: |
| 11817 |
case OpGroupNonUniformSMin: |
| 11818 |
case OpGroupNonUniformSMax: |
| 11819 |
case OpGroupNonUniformUMin: |
| 11820 |
case OpGroupNonUniformUMax: |
| 11821 |
case OpGroupNonUniformBitwiseAnd: |
| 11822 |
case OpGroupNonUniformBitwiseOr: |
| 11823 |
case OpGroupNonUniformBitwiseXor: |
| 11824 |
case OpGroupNonUniformQuadSwap: |
| 11825 |
case OpGroupNonUniformQuadBroadcast: |
| 11826 |
emit_subgroup_op(instruction); |
| 11827 |
break; |
| 11828 |
|
| 11829 |
case OpFUnordEqual: |
| 11830 |
case OpFUnordNotEqual: |
| 11831 |
case OpFUnordLessThan: |
| 11832 |
case OpFUnordGreaterThan: |
| 11833 |
case OpFUnordLessThanEqual: |
| 11834 |
case OpFUnordGreaterThanEqual: |
| 11835 |
{ |
| 11836 |
// GLSL doesn't specify if floating point comparisons are ordered or unordered, |
| 11837 |
// but glslang always emits ordered floating point compares for GLSL. |
| 11838 |
// To get unordered compares, we can test the opposite thing and invert the result. |
| 11839 |
// This way, we force true when there is any NaN present. |
| 11840 |
uint32_t op0 = ops[2]; |
| 11841 |
uint32_t op1 = ops[3]; |
| 11842 |
|
| 11843 |
string expr; |
| 11844 |
if (expression_type(op0).vecsize > 1) |
| 11845 |
{ |
| 11846 |
const char *comp_op = nullptr; |
| 11847 |
switch (opcode) |
| 11848 |
{ |
| 11849 |
case OpFUnordEqual: |
| 11850 |
comp_op = "notEqual"; |
| 11851 |
break; |
| 11852 |
|
| 11853 |
case OpFUnordNotEqual: |
| 11854 |
comp_op = "equal"; |
| 11855 |
break; |
| 11856 |
|
| 11857 |
case OpFUnordLessThan: |
| 11858 |
comp_op = "greaterThanEqual"; |
| 11859 |
break; |
| 11860 |
|
| 11861 |
case OpFUnordLessThanEqual: |
| 11862 |
comp_op = "greaterThan"; |
| 11863 |
break; |
| 11864 |
|
| 11865 |
case OpFUnordGreaterThan: |
| 11866 |
comp_op = "lessThanEqual"; |
| 11867 |
break; |
| 11868 |
|
| 11869 |
case OpFUnordGreaterThanEqual: |
| 11870 |
comp_op = "lessThan"; |
| 11871 |
break; |
| 11872 |
|
| 11873 |
default: |
| 11874 |
assert(0); |
| 11875 |
break; |
| 11876 |
} |
| 11877 |
|
| 11878 |
expr = join("not(", comp_op, "(", to_unpacked_expression(op0), ", ", to_unpacked_expression(op1), "))"); |
| 11879 |
} |
| 11880 |
else |
| 11881 |
{ |
| 11882 |
const char *comp_op = nullptr; |
| 11883 |
switch (opcode) |
| 11884 |
{ |
| 11885 |
case OpFUnordEqual: |
| 11886 |
comp_op = " != "; |
| 11887 |
break; |
| 11888 |
|
| 11889 |
case OpFUnordNotEqual: |
| 11890 |
comp_op = " == "; |
| 11891 |
break; |
| 11892 |
|
| 11893 |
case OpFUnordLessThan: |
| 11894 |
comp_op = " >= "; |
| 11895 |
break; |
| 11896 |
|
| 11897 |
case OpFUnordLessThanEqual: |
| 11898 |
comp_op = " > "; |
| 11899 |
break; |
| 11900 |
|
| 11901 |
case OpFUnordGreaterThan: |
| 11902 |
comp_op = " <= "; |
| 11903 |
break; |
| 11904 |
|
| 11905 |
case OpFUnordGreaterThanEqual: |
| 11906 |
comp_op = " < "; |
| 11907 |
break; |
| 11908 |
|
| 11909 |
default: |
| 11910 |
assert(0); |
| 11911 |
break; |
| 11912 |
} |
| 11913 |
|
| 11914 |
expr = join("!(", to_enclosed_unpacked_expression(op0), comp_op, to_enclosed_unpacked_expression(op1), ")"); |
| 11915 |
} |
| 11916 |
|
| 11917 |
emit_op(ops[0], ops[1], expr, should_forward(op0) && should_forward(op1)); |
| 11918 |
inherit_expression_dependencies(ops[1], op0); |
| 11919 |
inherit_expression_dependencies(ops[1], op1); |
| 11920 |
break; |
| 11921 |
} |
| 11922 |
|
| 11923 |
case OpReportIntersectionNV: |
| 11924 |
statement("reportIntersectionNV(", to_expression(ops[0]), ", ", to_expression(ops[1]), ");"); |
| 11925 |
flush_control_dependent_expressions(current_emitting_block->self); |
| 11926 |
break; |
| 11927 |
case OpIgnoreIntersectionNV: |
| 11928 |
statement("ignoreIntersectionNV();"); |
| 11929 |
flush_control_dependent_expressions(current_emitting_block->self); |
| 11930 |
break; |
| 11931 |
case OpTerminateRayNV: |
| 11932 |
statement("terminateRayNV();"); |
| 11933 |
flush_control_dependent_expressions(current_emitting_block->self); |
| 11934 |
break; |
| 11935 |
case OpTraceNV: |
| 11936 |
statement("traceNV(", to_expression(ops[0]), ", ", to_expression(ops[1]), ", ", to_expression(ops[2]), ", ", |
| 11937 |
to_expression(ops[3]), ", ", to_expression(ops[4]), ", ", to_expression(ops[5]), ", ", |
| 11938 |
to_expression(ops[6]), ", ", to_expression(ops[7]), ", ", to_expression(ops[8]), ", ", |
| 11939 |
to_expression(ops[9]), ", ", to_expression(ops[10]), ");"); |
| 11940 |
flush_control_dependent_expressions(current_emitting_block->self); |
| 11941 |
break; |
| 11942 |
case OpExecuteCallableNV: |
| 11943 |
statement("executeCallableNV(", to_expression(ops[0]), ", ", to_expression(ops[1]), ");"); |
| 11944 |
flush_control_dependent_expressions(current_emitting_block->self); |
| 11945 |
break; |
| 11946 |
|
| 11947 |
case OpConvertUToPtr: |
| 11948 |
{ |
| 11949 |
auto &type = get<SPIRType>(ops[0]); |
| 11950 |
if (type.storage != StorageClassPhysicalStorageBufferEXT) |
| 11951 |
SPIRV_CROSS_THROW("Only StorageClassPhysicalStorageBufferEXT is supported by OpConvertUToPtr."); |
| 11952 |
|
| 11953 |
auto op = type_to_glsl(type); |
| 11954 |
emit_unary_func_op(ops[0], ops[1], ops[2], op.c_str()); |
| 11955 |
break; |
| 11956 |
} |
| 11957 |
|
| 11958 |
case OpConvertPtrToU: |
| 11959 |
{ |
| 11960 |
auto &type = get<SPIRType>(ops[0]); |
| 11961 |
auto &ptr_type = expression_type(ops[2]); |
| 11962 |
if (ptr_type.storage != StorageClassPhysicalStorageBufferEXT) |
| 11963 |
SPIRV_CROSS_THROW("Only StorageClassPhysicalStorageBufferEXT is supported by OpConvertPtrToU."); |
| 11964 |
|
| 11965 |
auto op = type_to_glsl(type); |
| 11966 |
emit_unary_func_op(ops[0], ops[1], ops[2], op.c_str()); |
| 11967 |
break; |
| 11968 |
} |
| 11969 |
|
| 11970 |
case OpUndef: |
| 11971 |
// Undefined value has been declared. |
| 11972 |
break; |
| 11973 |
|
| 11974 |
case OpLine: |
| 11975 |
{ |
| 11976 |
emit_line_directive(ops[0], ops[1]); |
| 11977 |
break; |
| 11978 |
} |
| 11979 |
|
| 11980 |
case OpNoLine: |
| 11981 |
break; |
| 11982 |
|
| 11983 |
case OpDemoteToHelperInvocationEXT: |
| 11984 |
if (!options.vulkan_semantics) |
| 11985 |
SPIRV_CROSS_THROW("GL_EXT_demote_to_helper_invocation is only supported in Vulkan GLSL."); |
| 11986 |
require_extension_internal("GL_EXT_demote_to_helper_invocation"); |
| 11987 |
statement(backend.demote_literal, ";"); |
| 11988 |
break; |
| 11989 |
|
| 11990 |
case OpIsHelperInvocationEXT: |
| 11991 |
if (!options.vulkan_semantics) |
| 11992 |
SPIRV_CROSS_THROW("GL_EXT_demote_to_helper_invocation is only supported in Vulkan GLSL."); |
| 11993 |
require_extension_internal("GL_EXT_demote_to_helper_invocation"); |
| 11994 |
emit_op(ops[0], ops[1], "helperInvocationEXT()", false); |
| 11995 |
break; |
| 11996 |
|
| 11997 |
case OpBeginInvocationInterlockEXT: |
| 11998 |
// If the interlock is complex, we emit this elsewhere. |
| 11999 |
if (!interlocked_is_complex) |
| 12000 |
{ |
| 12001 |
if (options.es) |
| 12002 |
statement("beginInvocationInterlockNV();"); |
| 12003 |
else |
| 12004 |
statement("beginInvocationInterlockARB();"); |
| 12005 |
|
| 12006 |
flush_all_active_variables(); |
| 12007 |
// Make sure forwarding doesn't propagate outside interlock region. |
| 12008 |
} |
| 12009 |
break; |
| 12010 |
|
| 12011 |
case OpEndInvocationInterlockEXT: |
| 12012 |
// If the interlock is complex, we emit this elsewhere. |
| 12013 |
if (!interlocked_is_complex) |
| 12014 |
{ |
| 12015 |
if (options.es) |
| 12016 |
statement("endInvocationInterlockNV();"); |
| 12017 |
else |
| 12018 |
statement("endInvocationInterlockARB();"); |
| 12019 |
|
| 12020 |
flush_all_active_variables(); |
| 12021 |
// Make sure forwarding doesn't propagate outside interlock region. |
| 12022 |
} |
| 12023 |
break; |
| 12024 |
|
| 12025 |
default: |
| 12026 |
statement("// unimplemented op ", instruction.op); |
| 12027 |
break; |
| 12028 |
} |
| 12029 |
} |
| 12030 |
|
| 12031 |
// Appends function arguments, mapped from global variables, beyond the specified arg index. |
| 12032 |
// This is used when a function call uses fewer arguments than the function defines. |
| 12033 |
// This situation may occur if the function signature has been dynamically modified to |
| 12034 |
// extract global variables referenced from within the function, and convert them to |
| 12035 |
// function arguments. This is necessary for shader languages that do not support global |
| 12036 |
// access to shader input content from within a function (eg. Metal). Each additional |
| 12037 |
// function args uses the name of the global variable. Function nesting will modify the |
| 12038 |
// functions and function calls all the way up the nesting chain. |
| 12039 |
void CompilerGLSL::append_global_func_args(const SPIRFunction &func, uint32_t index, SmallVector<string> &arglist) |
| 12040 |
{ |
| 12041 |
auto &args = func.arguments; |
| 12042 |
uint32_t arg_cnt = uint32_t(args.size()); |
| 12043 |
for (uint32_t arg_idx = index; arg_idx < arg_cnt; arg_idx++) |
| 12044 |
{ |
| 12045 |
auto &arg = args[arg_idx]; |
| 12046 |
assert(arg.alias_global_variable); |
| 12047 |
|
| 12048 |
// If the underlying variable needs to be declared |
| 12049 |
// (ie. a local variable with deferred declaration), do so now. |
| 12050 |
uint32_t var_id = get<SPIRVariable>(arg.id).basevariable; |
| 12051 |
if (var_id) |
| 12052 |
flush_variable_declaration(var_id); |
| 12053 |
|
| 12054 |
arglist.push_back(to_func_call_arg(arg, arg.id)); |
| 12055 |
} |
| 12056 |
} |
| 12057 |
|
| 12058 |
string CompilerGLSL::to_member_name(const SPIRType &type, uint32_t index) |
| 12059 |
{ |
| 12060 |
if (type.type_alias != TypeID(0) && |
| 12061 |
!has_extended_decoration(type.type_alias, SPIRVCrossDecorationBufferBlockRepacked)) |
| 12062 |
{ |
| 12063 |
return to_member_name(get<SPIRType>(type.type_alias), index); |
| 12064 |
} |
| 12065 |
|
| 12066 |
auto &memb = ir.meta[type.self].members; |
| 12067 |
if (index < memb.size() && !memb[index].alias.empty()) |
| 12068 |
return memb[index].alias; |
| 12069 |
else |
| 12070 |
return join("_m", index); |
| 12071 |
} |
| 12072 |
|
| 12073 |
string CompilerGLSL::to_member_reference(uint32_t, const SPIRType &type, uint32_t index, bool) |
| 12074 |
{ |
| 12075 |
return join(".", to_member_name(type, index)); |
| 12076 |
} |
| 12077 |
|
| 12078 |
string CompilerGLSL::to_multi_member_reference(const SPIRType &type, const SmallVector<uint32_t> &indices) |
| 12079 |
{ |
| 12080 |
string ret; |
| 12081 |
auto *member_type = &type; |
| 12082 |
for (auto &index : indices) |
| 12083 |
{ |
| 12084 |
ret += join(".", to_member_name(*member_type, index)); |
| 12085 |
member_type = &get<SPIRType>(member_type->member_types[index]); |
| 12086 |
} |
| 12087 |
return ret; |
| 12088 |
} |
| 12089 |
|
| 12090 |
void CompilerGLSL::add_member_name(SPIRType &type, uint32_t index) |
| 12091 |
{ |
| 12092 |
auto &memb = ir.meta[type.self].members; |
| 12093 |
if (index < memb.size() && !memb[index].alias.empty()) |
| 12094 |
{ |
| 12095 |
auto &name = memb[index].alias; |
| 12096 |
if (name.empty()) |
| 12097 |
return; |
| 12098 |
|
| 12099 |
ParsedIR::sanitize_identifier(name, true, true); |
| 12100 |
update_name_cache(type.member_name_cache, name); |
| 12101 |
} |
| 12102 |
} |
| 12103 |
|
| 12104 |
// Checks whether the ID is a row_major matrix that requires conversion before use |
| 12105 |
bool CompilerGLSL::is_non_native_row_major_matrix(uint32_t id) |
| 12106 |
{ |
| 12107 |
// Natively supported row-major matrices do not need to be converted. |
| 12108 |
// Legacy targets do not support row major. |
| 12109 |
if (backend.native_row_major_matrix && !is_legacy()) |
| 12110 |
return false; |
| 12111 |
|
| 12112 |
auto *e = maybe_get<SPIRExpression>(id); |
| 12113 |
if (e) |
| 12114 |
return e->need_transpose; |
| 12115 |
else |
| 12116 |
return has_decoration(id, DecorationRowMajor); |
| 12117 |
} |
| 12118 |
|
| 12119 |
// Checks whether the member is a row_major matrix that requires conversion before use |
| 12120 |
bool CompilerGLSL::member_is_non_native_row_major_matrix(const SPIRType &type, uint32_t index) |
| 12121 |
{ |
| 12122 |
// Natively supported row-major matrices do not need to be converted. |
| 12123 |
if (backend.native_row_major_matrix && !is_legacy()) |
| 12124 |
return false; |
| 12125 |
|
| 12126 |
// Non-matrix or column-major matrix types do not need to be converted. |
| 12127 |
if (!has_member_decoration(type.self, index, DecorationRowMajor)) |
| 12128 |
return false; |
| 12129 |
|
| 12130 |
// Only square row-major matrices can be converted at this time. |
| 12131 |
// Converting non-square matrices will require defining custom GLSL function that |
| 12132 |
// swaps matrix elements while retaining the original dimensional form of the matrix. |
| 12133 |
const auto mbr_type = get<SPIRType>(type.member_types[index]); |
| 12134 |
if (mbr_type.columns != mbr_type.vecsize) |
| 12135 |
SPIRV_CROSS_THROW("Row-major matrices must be square on this platform."); |
| 12136 |
|
| 12137 |
return true; |
| 12138 |
} |
| 12139 |
|
| 12140 |
// Checks if we need to remap physical type IDs when declaring the type in a buffer. |
| 12141 |
bool CompilerGLSL::member_is_remapped_physical_type(const SPIRType &type, uint32_t index) const |
| 12142 |
{ |
| 12143 |
return has_extended_member_decoration(type.self, index, SPIRVCrossDecorationPhysicalTypeID); |
| 12144 |
} |
| 12145 |
|
| 12146 |
// Checks whether the member is in packed data type, that might need to be unpacked. |
| 12147 |
bool CompilerGLSL::member_is_packed_physical_type(const SPIRType &type, uint32_t index) const |
| 12148 |
{ |
| 12149 |
return has_extended_member_decoration(type.self, index, SPIRVCrossDecorationPhysicalTypePacked); |
| 12150 |
} |
| 12151 |
|
| 12152 |
// Wraps the expression string in a function call that converts the |
| 12153 |
// row_major matrix result of the expression to a column_major matrix. |
| 12154 |
// Base implementation uses the standard library transpose() function. |
| 12155 |
// Subclasses may override to use a different function. |
| 12156 |
string CompilerGLSL::convert_row_major_matrix(string exp_str, const SPIRType &exp_type, uint32_t /* physical_type_id */, |
| 12157 |
bool /*is_packed*/) |
| 12158 |
{ |
| 12159 |
strip_enclosed_expression(exp_str); |
| 12160 |
if (!is_matrix(exp_type)) |
| 12161 |
{ |
| 12162 |
auto column_index = exp_str.find_last_of('['); |
| 12163 |
if (column_index == string::npos) |
| 12164 |
return exp_str; |
| 12165 |
|
| 12166 |
auto column_expr = exp_str.substr(column_index); |
| 12167 |
exp_str.resize(column_index); |
| 12168 |
|
| 12169 |
auto transposed_expr = type_to_glsl_constructor(exp_type) + "("; |
| 12170 |
|
| 12171 |
// Loading a column from a row-major matrix. Unroll the load. |
| 12172 |
for (uint32_t c = 0; c < exp_type.vecsize; c++) |
| 12173 |
{ |
| 12174 |
transposed_expr += join(exp_str, '[', c, ']', column_expr); |
| 12175 |
if (c + 1 < exp_type.vecsize) |
| 12176 |
transposed_expr += ", "; |
| 12177 |
} |
| 12178 |
|
| 12179 |
transposed_expr += ")"; |
| 12180 |
return transposed_expr; |
| 12181 |
} |
| 12182 |
else if (options.version < 120) |
| 12183 |
{ |
| 12184 |
// GLSL 110, ES 100 do not have transpose(), so emulate it. Note that |
| 12185 |
// these GLSL versions do not support non-square matrices. |
| 12186 |
if (exp_type.vecsize == 2 && exp_type.columns == 2) |
| 12187 |
{ |
| 12188 |
if (!requires_transpose_2x2) |
| 12189 |
{ |
| 12190 |
requires_transpose_2x2 = true; |
| 12191 |
force_recompile(); |
| 12192 |
} |
| 12193 |
} |
| 12194 |
else if (exp_type.vecsize == 3 && exp_type.columns == 3) |
| 12195 |
{ |
| 12196 |
if (!requires_transpose_3x3) |
| 12197 |
{ |
| 12198 |
requires_transpose_3x3 = true; |
| 12199 |
force_recompile(); |
| 12200 |
} |
| 12201 |
} |
| 12202 |
else if (exp_type.vecsize == 4 && exp_type.columns == 4) |
| 12203 |
{ |
| 12204 |
if (!requires_transpose_4x4) |
| 12205 |
{ |
| 12206 |
requires_transpose_4x4 = true; |
| 12207 |
force_recompile(); |
| 12208 |
} |
| 12209 |
} |
| 12210 |
else |
| 12211 |
SPIRV_CROSS_THROW("Non-square matrices are not supported in legacy GLSL, cannot transpose."); |
| 12212 |
return join("SPIRV_Cross_Transpose(", exp_str, ")"); |
| 12213 |
} |
| 12214 |
else |
| 12215 |
return join("transpose(", exp_str, ")"); |
| 12216 |
} |
| 12217 |
|
| 12218 |
string CompilerGLSL::variable_decl(const SPIRType &type, const string &name, uint32_t id) |
| 12219 |
{ |
| 12220 |
string type_name = type_to_glsl(type, id); |
| 12221 |
remap_variable_type_name(type, name, type_name); |
| 12222 |
return join(type_name, " ", name, type_to_array_glsl(type)); |
| 12223 |
} |
| 12224 |
|
| 12225 |
// Emit a structure member. Subclasses may override to modify output, |
| 12226 |
// or to dynamically add a padding member if needed. |
| 12227 |
void CompilerGLSL::emit_struct_member(const SPIRType &type, uint32_t member_type_id, uint32_t index, |
| 12228 |
const string &qualifier, uint32_t) |
| 12229 |
{ |
| 12230 |
auto &membertype = get<SPIRType>(member_type_id); |
| 12231 |
|
| 12232 |
Bitset memberflags; |
| 12233 |
auto &memb = ir.meta[type.self].members; |
| 12234 |
if (index < memb.size()) |
| 12235 |
memberflags = memb[index].decoration_flags; |
| 12236 |
|
| 12237 |
string qualifiers; |
| 12238 |
bool is_block = ir.meta[type.self].decoration.decoration_flags.get(DecorationBlock) || |
| 12239 |
ir.meta[type.self].decoration.decoration_flags.get(DecorationBufferBlock); |
| 12240 |
|
| 12241 |
if (is_block) |
| 12242 |
qualifiers = to_interpolation_qualifiers(memberflags); |
| 12243 |
|
| 12244 |
statement(layout_for_member(type, index), qualifiers, qualifier, flags_to_qualifiers_glsl(membertype, memberflags), |
| 12245 |
variable_decl(membertype, to_member_name(type, index)), ";"); |
| 12246 |
} |
| 12247 |
|
| 12248 |
void CompilerGLSL::emit_struct_padding_target(const SPIRType &) |
| 12249 |
{ |
| 12250 |
} |
| 12251 |
|
| 12252 |
const char *CompilerGLSL::flags_to_qualifiers_glsl(const SPIRType &type, const Bitset &flags) |
| 12253 |
{ |
| 12254 |
// GL_EXT_buffer_reference variables can be marked as restrict. |
| 12255 |
if (flags.get(DecorationRestrictPointerEXT)) |
| 12256 |
return "restrict "; |
| 12257 |
|
| 12258 |
// Structs do not have precision qualifiers, neither do doubles (desktop only anyways, so no mediump/highp). |
| 12259 |
if (type.basetype != SPIRType::Float && type.basetype != SPIRType::Int && type.basetype != SPIRType::UInt && |
| 12260 |
type.basetype != SPIRType::Image && type.basetype != SPIRType::SampledImage && |
| 12261 |
type.basetype != SPIRType::Sampler) |
| 12262 |
return ""; |
| 12263 |
|
| 12264 |
if (options.es) |
| 12265 |
{ |
| 12266 |
auto &execution = get_entry_point(); |
| 12267 |
|
| 12268 |
if (flags.get(DecorationRelaxedPrecision)) |
| 12269 |
{ |
| 12270 |
bool implied_fmediump = type.basetype == SPIRType::Float && |
| 12271 |
options.fragment.default_float_precision == Options::Mediump && |
| 12272 |
execution.model == ExecutionModelFragment; |
| 12273 |
|
| 12274 |
bool implied_imediump = (type.basetype == SPIRType::Int || type.basetype == SPIRType::UInt) && |
| 12275 |
options.fragment.default_int_precision == Options::Mediump && |
| 12276 |
execution.model == ExecutionModelFragment; |
| 12277 |
|
| 12278 |
return implied_fmediump || implied_imediump ? "" : "mediump "; |
| 12279 |
} |
| 12280 |
else |
| 12281 |
{ |
| 12282 |
bool implied_fhighp = |
| 12283 |
type.basetype == SPIRType::Float && ((options.fragment.default_float_precision == Options::Highp && |
| 12284 |
execution.model == ExecutionModelFragment) || |
| 12285 |
(execution.model != ExecutionModelFragment)); |
| 12286 |
|
| 12287 |
bool implied_ihighp = (type.basetype == SPIRType::Int || type.basetype == SPIRType::UInt) && |
| 12288 |
((options.fragment.default_int_precision == Options::Highp && |
| 12289 |
execution.model == ExecutionModelFragment) || |
| 12290 |
(execution.model != ExecutionModelFragment)); |
| 12291 |
|
| 12292 |
return implied_fhighp || implied_ihighp ? "" : "highp "; |
| 12293 |
} |
| 12294 |
} |
| 12295 |
else if (backend.allow_precision_qualifiers) |
| 12296 |
{ |
| 12297 |
// Vulkan GLSL supports precision qualifiers, even in desktop profiles, which is convenient. |
| 12298 |
// The default is highp however, so only emit mediump in the rare case that a shader has these. |
| 12299 |
if (flags.get(DecorationRelaxedPrecision)) |
| 12300 |
return "mediump "; |
| 12301 |
else |
| 12302 |
return ""; |
| 12303 |
} |
| 12304 |
else |
| 12305 |
return ""; |
| 12306 |
} |
| 12307 |
|
| 12308 |
const char *CompilerGLSL::to_precision_qualifiers_glsl(uint32_t id) |
| 12309 |
{ |
| 12310 |
auto &type = expression_type(id); |
| 12311 |
bool use_precision_qualifiers = backend.allow_precision_qualifiers || options.es; |
| 12312 |
if (use_precision_qualifiers && (type.basetype == SPIRType::Image || type.basetype == SPIRType::SampledImage)) |
| 12313 |
{ |
| 12314 |
// Force mediump for the sampler type. We cannot declare 16-bit or smaller image types. |
| 12315 |
auto &result_type = get<SPIRType>(type.image.type); |
| 12316 |
if (result_type.width < 32) |
| 12317 |
return "mediump "; |
| 12318 |
} |
| 12319 |
return flags_to_qualifiers_glsl(type, ir.meta[id].decoration.decoration_flags); |
| 12320 |
} |
| 12321 |
|
| 12322 |
string CompilerGLSL::to_qualifiers_glsl(uint32_t id) |
| 12323 |
{ |
| 12324 |
auto &flags = ir.meta[id].decoration.decoration_flags; |
| 12325 |
string res; |
| 12326 |
|
| 12327 |
auto *var = maybe_get<SPIRVariable>(id); |
| 12328 |
|
| 12329 |
if (var && var->storage == StorageClassWorkgroup && !backend.shared_is_implied) |
| 12330 |
res += "shared "; |
| 12331 |
|
| 12332 |
res += to_interpolation_qualifiers(flags); |
| 12333 |
if (var) |
| 12334 |
res += to_storage_qualifiers_glsl(*var); |
| 12335 |
|
| 12336 |
auto &type = expression_type(id); |
| 12337 |
if (type.image.dim != DimSubpassData && type.image.sampled == 2) |
| 12338 |
{ |
| 12339 |
if (flags.get(DecorationCoherent)) |
| 12340 |
res += "coherent "; |
| 12341 |
if (flags.get(DecorationRestrict)) |
| 12342 |
res += "restrict "; |
| 12343 |
if (flags.get(DecorationNonWritable)) |
| 12344 |
res += "readonly "; |
| 12345 |
if (flags.get(DecorationNonReadable)) |
| 12346 |
res += "writeonly "; |
| 12347 |
} |
| 12348 |
|
| 12349 |
res += to_precision_qualifiers_glsl(id); |
| 12350 |
|
| 12351 |
return res; |
| 12352 |
} |
| 12353 |
|
| 12354 |
string CompilerGLSL::argument_decl(const SPIRFunction::Parameter &arg) |
| 12355 |
{ |
| 12356 |
// glslangValidator seems to make all arguments pointer no matter what which is rather bizarre ... |
| 12357 |
auto &type = expression_type(arg.id); |
| 12358 |
const char *direction = ""; |
| 12359 |
|
| 12360 |
if (type.pointer) |
| 12361 |
{ |
| 12362 |
if (arg.write_count && arg.read_count) |
| 12363 |
direction = "inout "; |
| 12364 |
else if (arg.write_count) |
| 12365 |
direction = "out "; |
| 12366 |
} |
| 12367 |
|
| 12368 |
return join(direction, to_qualifiers_glsl(arg.id), variable_decl(type, to_name(arg.id), arg.id)); |
| 12369 |
} |
| 12370 |
|
| 12371 |
string CompilerGLSL::to_initializer_expression(const SPIRVariable &var) |
| 12372 |
{ |
| 12373 |
return to_expression(var.initializer); |
| 12374 |
} |
| 12375 |
|
| 12376 |
string CompilerGLSL::to_zero_initialized_expression(uint32_t type_id) |
| 12377 |
{ |
| 12378 |
#ifndef NDEBUG |
| 12379 |
auto &type = get<SPIRType>(type_id); |
| 12380 |
assert(type.storage == StorageClassPrivate || type.storage == StorageClassFunction || |
| 12381 |
type.storage == StorageClassGeneric); |
| 12382 |
#endif |
| 12383 |
uint32_t id = ir.increase_bound_by(1); |
| 12384 |
ir.make_constant_null(id, type_id, false); |
| 12385 |
return constant_expression(get<SPIRConstant>(id)); |
| 12386 |
} |
| 12387 |
|
| 12388 |
bool CompilerGLSL::type_can_zero_initialize(const SPIRType &type) const |
| 12389 |
{ |
| 12390 |
if (type.pointer) |
| 12391 |
return false; |
| 12392 |
|
| 12393 |
if (!type.array.empty() && options.flatten_multidimensional_arrays) |
| 12394 |
return false; |
| 12395 |
|
| 12396 |
for (auto &literal : type.array_size_literal) |
| 12397 |
if (!literal) |
| 12398 |
return false; |
| 12399 |
|
| 12400 |
for (auto &memb : type.member_types) |
| 12401 |
if (!type_can_zero_initialize(get<SPIRType>(memb))) |
| 12402 |
return false; |
| 12403 |
|
| 12404 |
return true; |
| 12405 |
} |
| 12406 |
|
| 12407 |
string CompilerGLSL::variable_decl(const SPIRVariable &variable) |
| 12408 |
{ |
| 12409 |
// Ignore the pointer type since GLSL doesn't have pointers. |
| 12410 |
auto &type = get_variable_data_type(variable); |
| 12411 |
|
| 12412 |
if (type.pointer_depth > 1) |
| 12413 |
SPIRV_CROSS_THROW("Cannot declare pointer-to-pointer types."); |
| 12414 |
|
| 12415 |
auto res = join(to_qualifiers_glsl(variable.self), variable_decl(type, to_name(variable.self), variable.self)); |
| 12416 |
|
| 12417 |
if (variable.loop_variable && variable.static_expression) |
| 12418 |
{ |
| 12419 |
uint32_t expr = variable.static_expression; |
| 12420 |
if (ir.ids[expr].get_type() != TypeUndef) |
| 12421 |
res += join(" = ", to_expression(variable.static_expression)); |
| 12422 |
else if (options.force_zero_initialized_variables && type_can_zero_initialize(type)) |
| 12423 |
res += join(" = ", to_zero_initialized_expression(get_variable_data_type_id(variable))); |
| 12424 |
} |
| 12425 |
else if (variable.initializer) |
| 12426 |
{ |
| 12427 |
uint32_t expr = variable.initializer; |
| 12428 |
if (ir.ids[expr].get_type() != TypeUndef) |
| 12429 |
res += join(" = ", to_initializer_expression(variable)); |
| 12430 |
else if (options.force_zero_initialized_variables && type_can_zero_initialize(type)) |
| 12431 |
res += join(" = ", to_zero_initialized_expression(get_variable_data_type_id(variable))); |
| 12432 |
} |
| 12433 |
|
| 12434 |
return res; |
| 12435 |
} |
| 12436 |
|
| 12437 |
const char *CompilerGLSL::to_pls_qualifiers_glsl(const SPIRVariable &variable) |
| 12438 |
{ |
| 12439 |
auto &flags = ir.meta[variable.self].decoration.decoration_flags; |
| 12440 |
if (flags.get(DecorationRelaxedPrecision)) |
| 12441 |
return "mediump "; |
| 12442 |
else |
| 12443 |
return "highp "; |
| 12444 |
} |
| 12445 |
|
| 12446 |
string CompilerGLSL::pls_decl(const PlsRemap &var) |
| 12447 |
{ |
| 12448 |
auto &variable = get<SPIRVariable>(var.id); |
| 12449 |
|
| 12450 |
SPIRType type; |
| 12451 |
type.vecsize = pls_format_to_components(var.format); |
| 12452 |
type.basetype = pls_format_to_basetype(var.format); |
| 12453 |
|
| 12454 |
return join(to_pls_layout(var.format), to_pls_qualifiers_glsl(variable), type_to_glsl(type), " ", |
| 12455 |
to_name(variable.self)); |
| 12456 |
} |
| 12457 |
|
| 12458 |
uint32_t CompilerGLSL::to_array_size_literal(const SPIRType &type) const |
| 12459 |
{ |
| 12460 |
return to_array_size_literal(type, uint32_t(type.array.size() - 1)); |
| 12461 |
} |
| 12462 |
|
| 12463 |
uint32_t CompilerGLSL::to_array_size_literal(const SPIRType &type, uint32_t index) const |
| 12464 |
{ |
| 12465 |
assert(type.array.size() == type.array_size_literal.size()); |
| 12466 |
|
| 12467 |
if (type.array_size_literal[index]) |
| 12468 |
{ |
| 12469 |
return type.array[index]; |
| 12470 |
} |
| 12471 |
else |
| 12472 |
{ |
| 12473 |
// Use the default spec constant value. |
| 12474 |
// This is the best we can do. |
| 12475 |
return evaluate_constant_u32(type.array[index]); |
| 12476 |
} |
| 12477 |
} |
| 12478 |
|
| 12479 |
string CompilerGLSL::to_array_size(const SPIRType &type, uint32_t index) |
| 12480 |
{ |
| 12481 |
assert(type.array.size() == type.array_size_literal.size()); |
| 12482 |
|
| 12483 |
auto &size = type.array[index]; |
| 12484 |
if (!type.array_size_literal[index]) |
| 12485 |
return to_expression(size); |
| 12486 |
else if (size) |
| 12487 |
return convert_to_string(size); |
| 12488 |
else if (!backend.unsized_array_supported) |
| 12489 |
{ |
| 12490 |
// For runtime-sized arrays, we can work around |
| 12491 |
// lack of standard support for this by simply having |
| 12492 |
// a single element array. |
| 12493 |
// |
| 12494 |
// Runtime length arrays must always be the last element |
| 12495 |
// in an interface block. |
| 12496 |
return "1"; |
| 12497 |
} |
| 12498 |
else |
| 12499 |
return ""; |
| 12500 |
} |
| 12501 |
|
| 12502 |
string CompilerGLSL::type_to_array_glsl(const SPIRType &type) |
| 12503 |
{ |
| 12504 |
if (type.pointer && type.storage == StorageClassPhysicalStorageBufferEXT && type.basetype != SPIRType::Struct) |
| 12505 |
{ |
| 12506 |
// We are using a wrapped pointer type, and we should not emit any array declarations here. |
| 12507 |
return ""; |
| 12508 |
} |
| 12509 |
|
| 12510 |
if (type.array.empty()) |
| 12511 |
return ""; |
| 12512 |
|
| 12513 |
if (options.flatten_multidimensional_arrays) |
| 12514 |
{ |
| 12515 |
string res; |
| 12516 |
res += "["; |
| 12517 |
for (auto i = uint32_t(type.array.size()); i; i--) |
| 12518 |
{ |
| 12519 |
res += enclose_expression(to_array_size(type, i - 1)); |
| 12520 |
if (i > 1) |
| 12521 |
res += " * "; |
| 12522 |
} |
| 12523 |
res += "]"; |
| 12524 |
return res; |
| 12525 |
} |
| 12526 |
else |
| 12527 |
{ |
| 12528 |
if (type.array.size() > 1) |
| 12529 |
{ |
| 12530 |
if (!options.es && options.version < 430) |
| 12531 |
require_extension_internal("GL_ARB_arrays_of_arrays"); |
| 12532 |
else if (options.es && options.version < 310) |
| 12533 |
SPIRV_CROSS_THROW("Arrays of arrays not supported before ESSL version 310. " |
| 12534 |
"Try using --flatten-multidimensional-arrays or set " |
| 12535 |
"options.flatten_multidimensional_arrays to true."); |
| 12536 |
} |
| 12537 |
|
| 12538 |
string res; |
| 12539 |
for (auto i = uint32_t(type.array.size()); i; i--) |
| 12540 |
{ |
| 12541 |
res += "["; |
| 12542 |
res += to_array_size(type, i - 1); |
| 12543 |
res += "]"; |
| 12544 |
} |
| 12545 |
return res; |
| 12546 |
} |
| 12547 |
} |
| 12548 |
|
| 12549 |
string CompilerGLSL::image_type_glsl(const SPIRType &type, uint32_t id) |
| 12550 |
{ |
| 12551 |
auto &imagetype = get<SPIRType>(type.image.type); |
| 12552 |
string res; |
| 12553 |
|
| 12554 |
switch (imagetype.basetype) |
| 12555 |
{ |
| 12556 |
case SPIRType::Int: |
| 12557 |
case SPIRType::Short: |
| 12558 |
case SPIRType::SByte: |
| 12559 |
res = "i"; |
| 12560 |
break; |
| 12561 |
case SPIRType::UInt: |
| 12562 |
case SPIRType::UShort: |
| 12563 |
case SPIRType::UByte: |
| 12564 |
res = "u"; |
| 12565 |
break; |
| 12566 |
default: |
| 12567 |
break; |
| 12568 |
} |
| 12569 |
|
| 12570 |
// For half image types, we will force mediump for the sampler, and cast to f16 after any sampling operation. |
| 12571 |
// We cannot express a true half texture type in GLSL. Neither for short integer formats for that matter. |
| 12572 |
|
| 12573 |
if (type.basetype == SPIRType::Image && type.image.dim == DimSubpassData && options.vulkan_semantics) |
| 12574 |
return res + "subpassInput" + (type.image.ms ? "MS" : ""); |
| 12575 |
else if (type.basetype == SPIRType::Image && type.image.dim == DimSubpassData && |
| 12576 |
subpass_input_is_framebuffer_fetch(id)) |
| 12577 |
{ |
| 12578 |
SPIRType sampled_type = get<SPIRType>(type.image.type); |
| 12579 |
sampled_type.vecsize = 4; |
| 12580 |
return type_to_glsl(sampled_type); |
| 12581 |
} |
| 12582 |
|
| 12583 |
// If we're emulating subpassInput with samplers, force sampler2D |
| 12584 |
// so we don't have to specify format. |
| 12585 |
if (type.basetype == SPIRType::Image && type.image.dim != DimSubpassData) |
| 12586 |
{ |
| 12587 |
// Sampler buffers are always declared as samplerBuffer even though they might be separate images in the SPIR-V. |
| 12588 |
if (type.image.dim == DimBuffer && type.image.sampled == 1) |
| 12589 |
res += "sampler"; |
| 12590 |
else |
| 12591 |
res += type.image.sampled == 2 ? "image" : "texture"; |
| 12592 |
} |
| 12593 |
else |
| 12594 |
res += "sampler"; |
| 12595 |
|
| 12596 |
switch (type.image.dim) |
| 12597 |
{ |
| 12598 |
case Dim1D: |
| 12599 |
res += "1D"; |
| 12600 |
break; |
| 12601 |
case Dim2D: |
| 12602 |
res += "2D"; |
| 12603 |
break; |
| 12604 |
case Dim3D: |
| 12605 |
res += "3D"; |
| 12606 |
break; |
| 12607 |
case DimCube: |
| 12608 |
res += "Cube"; |
| 12609 |
break; |
| 12610 |
case DimRect: |
| 12611 |
if (options.es) |
| 12612 |
SPIRV_CROSS_THROW("Rectangle textures are not supported on OpenGL ES."); |
| 12613 |
|
| 12614 |
if (is_legacy_desktop()) |
| 12615 |
require_extension_internal("GL_ARB_texture_rectangle"); |
| 12616 |
|
| 12617 |
res += "2DRect"; |
| 12618 |
break; |
| 12619 |
|
| 12620 |
case DimBuffer: |
| 12621 |
if (options.es && options.version < 320) |
| 12622 |
require_extension_internal("GL_OES_texture_buffer"); |
| 12623 |
else if (!options.es && options.version < 300) |
| 12624 |
require_extension_internal("GL_EXT_texture_buffer_object"); |
| 12625 |
res += "Buffer"; |
| 12626 |
break; |
| 12627 |
|
| 12628 |
case DimSubpassData: |
| 12629 |
res += "2D"; |
| 12630 |
break; |
| 12631 |
default: |
| 12632 |
SPIRV_CROSS_THROW("Only 1D, 2D, 2DRect, 3D, Buffer, InputTarget and Cube textures supported."); |
| 12633 |
} |
| 12634 |
|
| 12635 |
if (type.image.ms) |
| 12636 |
res += "MS"; |
| 12637 |
if (type.image.arrayed) |
| 12638 |
{ |
| 12639 |
if (is_legacy_desktop()) |
| 12640 |
require_extension_internal("GL_EXT_texture_array"); |
| 12641 |
res += "Array"; |
| 12642 |
} |
| 12643 |
|
| 12644 |
// "Shadow" state in GLSL only exists for samplers and combined image samplers. |
| 12645 |
if (((type.basetype == SPIRType::SampledImage) || (type.basetype == SPIRType::Sampler)) && |
| 12646 |
image_is_comparison(type, id)) |
| 12647 |
{ |
| 12648 |
res += "Shadow"; |
| 12649 |
} |
| 12650 |
|
| 12651 |
return res; |
| 12652 |
} |
| 12653 |
|
| 12654 |
string CompilerGLSL::type_to_glsl_constructor(const SPIRType &type) |
| 12655 |
{ |
| 12656 |
if (backend.use_array_constructor && type.array.size() > 1) |
| 12657 |
{ |
| 12658 |
if (options.flatten_multidimensional_arrays) |
| 12659 |
SPIRV_CROSS_THROW("Cannot flatten constructors of multidimensional array constructors, " |
| 12660 |
"e.g. float[][]()."); |
| 12661 |
else if (!options.es && options.version < 430) |
| 12662 |
require_extension_internal("GL_ARB_arrays_of_arrays"); |
| 12663 |
else if (options.es && options.version < 310) |
| 12664 |
SPIRV_CROSS_THROW("Arrays of arrays not supported before ESSL version 310."); |
| 12665 |
} |
| 12666 |
|
| 12667 |
auto e = type_to_glsl(type); |
| 12668 |
if (backend.use_array_constructor) |
| 12669 |
{ |
| 12670 |
for (uint32_t i = 0; i < type.array.size(); i++) |
| 12671 |
e += "[]"; |
| 12672 |
} |
| 12673 |
return e; |
| 12674 |
} |
| 12675 |
|
| 12676 |
// The optional id parameter indicates the object whose type we are trying |
| 12677 |
// to find the description for. It is optional. Most type descriptions do not |
| 12678 |
// depend on a specific object's use of that type. |
| 12679 |
string CompilerGLSL::type_to_glsl(const SPIRType &type, uint32_t id) |
| 12680 |
{ |
| 12681 |
if (type.pointer && type.storage == StorageClassPhysicalStorageBufferEXT && type.basetype != SPIRType::Struct) |
| 12682 |
{ |
| 12683 |
// Need to create a magic type name which compacts the entire type information. |
| 12684 |
string name = type_to_glsl(get_pointee_type(type)); |
| 12685 |
for (size_t i = 0; i < type.array.size(); i++) |
| 12686 |
{ |
| 12687 |
if (type.array_size_literal[i]) |
| 12688 |
name += join(type.array[i], "_"); |
| 12689 |
else |
| 12690 |
name += join("id", type.array[i], "_"); |
| 12691 |
} |
| 12692 |
name += "Pointer"; |
| 12693 |
return name; |
| 12694 |
} |
| 12695 |
|
| 12696 |
switch (type.basetype) |
| 12697 |
{ |
| 12698 |
case SPIRType::Struct: |
| 12699 |
// Need OpName lookup here to get a "sensible" name for a struct. |
| 12700 |
if (backend.explicit_struct_type) |
| 12701 |
return join("struct ", to_name(type.self)); |
| 12702 |
else |
| 12703 |
return to_name(type.self); |
| 12704 |
|
| 12705 |
case SPIRType::Image: |
| 12706 |
case SPIRType::SampledImage: |
| 12707 |
return image_type_glsl(type, id); |
| 12708 |
|
| 12709 |
case SPIRType::Sampler: |
| 12710 |
// The depth field is set by calling code based on the variable ID of the sampler, effectively reintroducing |
| 12711 |
// this distinction into the type system. |
| 12712 |
return comparison_ids.count(id) ? "samplerShadow" : "sampler"; |
| 12713 |
|
| 12714 |
case SPIRType::AccelerationStructure: |
| 12715 |
return "accelerationStructureNV"; |
| 12716 |
|
| 12717 |
case SPIRType::Void: |
| 12718 |
return "void"; |
| 12719 |
|
| 12720 |
default: |
| 12721 |
break; |
| 12722 |
} |
| 12723 |
|
| 12724 |
if (type.basetype == SPIRType::UInt && is_legacy()) |
| 12725 |
SPIRV_CROSS_THROW("Unsigned integers are not supported on legacy targets."); |
| 12726 |
|
| 12727 |
if (type.vecsize == 1 && type.columns == 1) // Scalar builtin |
| 12728 |
{ |
| 12729 |
switch (type.basetype) |
| 12730 |
{ |
| 12731 |
case SPIRType::Boolean: |
| 12732 |
return "bool"; |
| 12733 |
case SPIRType::SByte: |
| 12734 |
return backend.basic_int8_type; |
| 12735 |
case SPIRType::UByte: |
| 12736 |
return backend.basic_uint8_type; |
| 12737 |
case SPIRType::Short: |
| 12738 |
return backend.basic_int16_type; |
| 12739 |
case SPIRType::UShort: |
| 12740 |
return backend.basic_uint16_type; |
| 12741 |
case SPIRType::Int: |
| 12742 |
return backend.basic_int_type; |
| 12743 |
case SPIRType::UInt: |
| 12744 |
return backend.basic_uint_type; |
| 12745 |
case SPIRType::AtomicCounter: |
| 12746 |
return "atomic_uint"; |
| 12747 |
case SPIRType::Half: |
| 12748 |
return "float16_t"; |
| 12749 |
case SPIRType::Float: |
| 12750 |
return "float"; |
| 12751 |
case SPIRType::Double: |
| 12752 |
return "double"; |
| 12753 |
case SPIRType::Int64: |
| 12754 |
return "int64_t"; |
| 12755 |
case SPIRType::UInt64: |
| 12756 |
return "uint64_t"; |
| 12757 |
default: |
| 12758 |
return "???"; |
| 12759 |
} |
| 12760 |
} |
| 12761 |
else if (type.vecsize > 1 && type.columns == 1) // Vector builtin |
| 12762 |
{ |
| 12763 |
switch (type.basetype) |
| 12764 |
{ |
| 12765 |
case SPIRType::Boolean: |
| 12766 |
return join("bvec", type.vecsize); |
| 12767 |
case SPIRType::SByte: |
| 12768 |
return join("i8vec", type.vecsize); |
| 12769 |
case SPIRType::UByte: |
| 12770 |
return join("u8vec", type.vecsize); |
| 12771 |
case SPIRType::Short: |
| 12772 |
return join("i16vec", type.vecsize); |
| 12773 |
case SPIRType::UShort: |
| 12774 |
return join("u16vec", type.vecsize); |
| 12775 |
case SPIRType::Int: |
| 12776 |
return join("ivec", type.vecsize); |
| 12777 |
case SPIRType::UInt: |
| 12778 |
return join("uvec", type.vecsize); |
| 12779 |
case SPIRType::Half: |
| 12780 |
return join("f16vec", type.vecsize); |
| 12781 |
case SPIRType::Float: |
| 12782 |
return join("vec", type.vecsize); |
| 12783 |
case SPIRType::Double: |
| 12784 |
return join("dvec", type.vecsize); |
| 12785 |
case SPIRType::Int64: |
| 12786 |
return join("i64vec", type.vecsize); |
| 12787 |
case SPIRType::UInt64: |
| 12788 |
return join("u64vec", type.vecsize); |
| 12789 |
default: |
| 12790 |
return "???"; |
| 12791 |
} |
| 12792 |
} |
| 12793 |
else if (type.vecsize == type.columns) // Simple Matrix builtin |
| 12794 |
{ |
| 12795 |
switch (type.basetype) |
| 12796 |
{ |
| 12797 |
case SPIRType::Boolean: |
| 12798 |
return join("bmat", type.vecsize); |
| 12799 |
case SPIRType::Int: |
| 12800 |
return join("imat", type.vecsize); |
| 12801 |
case SPIRType::UInt: |
| 12802 |
return join("umat", type.vecsize); |
| 12803 |
case SPIRType::Half: |
| 12804 |
return join("f16mat", type.vecsize); |
| 12805 |
case SPIRType::Float: |
| 12806 |
return join("mat", type.vecsize); |
| 12807 |
case SPIRType::Double: |
| 12808 |
return join("dmat", type.vecsize); |
| 12809 |
// Matrix types not supported for int64/uint64. |
| 12810 |
default: |
| 12811 |
return "???"; |
| 12812 |
} |
| 12813 |
} |
| 12814 |
else |
| 12815 |
{ |
| 12816 |
switch (type.basetype) |
| 12817 |
{ |
| 12818 |
case SPIRType::Boolean: |
| 12819 |
return join("bmat", type.columns, "x", type.vecsize); |
| 12820 |
case SPIRType::Int: |
| 12821 |
return join("imat", type.columns, "x", type.vecsize); |
| 12822 |
case SPIRType::UInt: |
| 12823 |
return join("umat", type.columns, "x", type.vecsize); |
| 12824 |
case SPIRType::Half: |
| 12825 |
return join("f16mat", type.columns, "x", type.vecsize); |
| 12826 |
case SPIRType::Float: |
| 12827 |
return join("mat", type.columns, "x", type.vecsize); |
| 12828 |
case SPIRType::Double: |
| 12829 |
return join("dmat", type.columns, "x", type.vecsize); |
| 12830 |
// Matrix types not supported for int64/uint64. |
| 12831 |
default: |
| 12832 |
return "???"; |
| 12833 |
} |
| 12834 |
} |
| 12835 |
} |
| 12836 |
|
| 12837 |
void CompilerGLSL::add_variable(unordered_set<string> &variables_primary, |
| 12838 |
const unordered_set<string> &variables_secondary, string &name) |
| 12839 |
{ |
| 12840 |
if (name.empty()) |
| 12841 |
return; |
| 12842 |
|
| 12843 |
ParsedIR::sanitize_underscores(name); |
| 12844 |
if (ParsedIR::is_globally_reserved_identifier(name, true)) |
| 12845 |
{ |
| 12846 |
name.clear(); |
| 12847 |
return; |
| 12848 |
} |
| 12849 |
|
| 12850 |
update_name_cache(variables_primary, variables_secondary, name); |
| 12851 |
} |
| 12852 |
|
| 12853 |
void CompilerGLSL::add_local_variable_name(uint32_t id) |
| 12854 |
{ |
| 12855 |
add_variable(local_variable_names, block_names, ir.meta[id].decoration.alias); |
| 12856 |
} |
| 12857 |
|
| 12858 |
void CompilerGLSL::add_resource_name(uint32_t id) |
| 12859 |
{ |
| 12860 |
add_variable(resource_names, block_names, ir.meta[id].decoration.alias); |
| 12861 |
} |
| 12862 |
|
| 12863 |
void CompilerGLSL::add_header_line(const std::string &line) |
| 12864 |
{ |
| 12865 |
header_lines.push_back(line); |
| 12866 |
} |
| 12867 |
|
| 12868 |
bool CompilerGLSL::has_extension(const std::string &ext) const |
| 12869 |
{ |
| 12870 |
auto itr = find(begin(forced_extensions), end(forced_extensions), ext); |
| 12871 |
return itr != end(forced_extensions); |
| 12872 |
} |
| 12873 |
|
| 12874 |
void CompilerGLSL::require_extension(const std::string &ext) |
| 12875 |
{ |
| 12876 |
if (!has_extension(ext)) |
| 12877 |
forced_extensions.push_back(ext); |
| 12878 |
} |
| 12879 |
|
| 12880 |
void CompilerGLSL::require_extension_internal(const string &ext) |
| 12881 |
{ |
| 12882 |
if (backend.supports_extensions && !has_extension(ext)) |
| 12883 |
{ |
| 12884 |
forced_extensions.push_back(ext); |
| 12885 |
force_recompile(); |
| 12886 |
} |
| 12887 |
} |
| 12888 |
|
| 12889 |
void CompilerGLSL::flatten_buffer_block(VariableID id) |
| 12890 |
{ |
| 12891 |
auto &var = get<SPIRVariable>(id); |
| 12892 |
auto &type = get<SPIRType>(var.basetype); |
| 12893 |
auto name = to_name(type.self, false); |
| 12894 |
auto &flags = ir.meta[type.self].decoration.decoration_flags; |
| 12895 |
|
| 12896 |
if (!type.array.empty()) |
| 12897 |
SPIRV_CROSS_THROW(name + " is an array of UBOs."); |
| 12898 |
if (type.basetype != SPIRType::Struct) |
| 12899 |
SPIRV_CROSS_THROW(name + " is not a struct."); |
| 12900 |
if (!flags.get(DecorationBlock)) |
| 12901 |
SPIRV_CROSS_THROW(name + " is not a block."); |
| 12902 |
if (type.member_types.empty()) |
| 12903 |
SPIRV_CROSS_THROW(name + " is an empty struct."); |
| 12904 |
|
| 12905 |
flattened_buffer_blocks.insert(id); |
| 12906 |
} |
| 12907 |
|
| 12908 |
bool CompilerGLSL::builtin_translates_to_nonarray(spv::BuiltIn /*builtin*/) const |
| 12909 |
{ |
| 12910 |
return false; // GLSL itself does not need to translate array builtin types to non-array builtin types |
| 12911 |
} |
| 12912 |
|
| 12913 |
bool CompilerGLSL::check_atomic_image(uint32_t id) |
| 12914 |
{ |
| 12915 |
auto &type = expression_type(id); |
| 12916 |
if (type.storage == StorageClassImage) |
| 12917 |
{ |
| 12918 |
if (options.es && options.version < 320) |
| 12919 |
require_extension_internal("GL_OES_shader_image_atomic"); |
| 12920 |
|
| 12921 |
auto *var = maybe_get_backing_variable(id); |
| 12922 |
if (var) |
| 12923 |
{ |
| 12924 |
auto &flags = ir.meta[var->self].decoration.decoration_flags; |
| 12925 |
if (flags.get(DecorationNonWritable) || flags.get(DecorationNonReadable)) |
| 12926 |
{ |
| 12927 |
flags.clear(DecorationNonWritable); |
| 12928 |
flags.clear(DecorationNonReadable); |
| 12929 |
force_recompile(); |
| 12930 |
} |
| 12931 |
} |
| 12932 |
return true; |
| 12933 |
} |
| 12934 |
else |
| 12935 |
return false; |
| 12936 |
} |
| 12937 |
|
| 12938 |
void CompilerGLSL::add_function_overload(const SPIRFunction &func) |
| 12939 |
{ |
| 12940 |
Hasher hasher; |
| 12941 |
for (auto &arg : func.arguments) |
| 12942 |
{ |
| 12943 |
// Parameters can vary with pointer type or not, |
| 12944 |
// but that will not change the signature in GLSL/HLSL, |
| 12945 |
// so strip the pointer type before hashing. |
| 12946 |
uint32_t type_id = get_pointee_type_id(arg.type); |
| 12947 |
auto &type = get<SPIRType>(type_id); |
| 12948 |
|
| 12949 |
if (!combined_image_samplers.empty()) |
| 12950 |
{ |
| 12951 |
// If we have combined image samplers, we cannot really trust the image and sampler arguments |
| 12952 |
// we pass down to callees, because they may be shuffled around. |
| 12953 |
// Ignore these arguments, to make sure that functions need to differ in some other way |
| 12954 |
// to be considered different overloads. |
| 12955 |
if (type.basetype == SPIRType::SampledImage || |
| 12956 |
(type.basetype == SPIRType::Image && type.image.sampled == 1) || type.basetype == SPIRType::Sampler) |
| 12957 |
{ |
| 12958 |
continue; |
| 12959 |
} |
| 12960 |
} |
| 12961 |
|
| 12962 |
hasher.u32(type_id); |
| 12963 |
} |
| 12964 |
uint64_t types_hash = hasher.get(); |
| 12965 |
|
| 12966 |
auto function_name = to_name(func.self); |
| 12967 |
auto itr = function_overloads.find(function_name); |
| 12968 |
if (itr != end(function_overloads)) |
| 12969 |
{ |
| 12970 |
// There exists a function with this name already. |
| 12971 |
auto &overloads = itr->second; |
| 12972 |
if (overloads.count(types_hash) != 0) |
| 12973 |
{ |
| 12974 |
// Overload conflict, assign a new name. |
| 12975 |
add_resource_name(func.self); |
| 12976 |
function_overloads[to_name(func.self)].insert(types_hash); |
| 12977 |
} |
| 12978 |
else |
| 12979 |
{ |
| 12980 |
// Can reuse the name. |
| 12981 |
overloads.insert(types_hash); |
| 12982 |
} |
| 12983 |
} |
| 12984 |
else |
| 12985 |
{ |
| 12986 |
// First time we see this function name. |
| 12987 |
add_resource_name(func.self); |
| 12988 |
function_overloads[to_name(func.self)].insert(types_hash); |
| 12989 |
} |
| 12990 |
} |
| 12991 |
|
| 12992 |
void CompilerGLSL::emit_function_prototype(SPIRFunction &func, const Bitset &return_flags) |
| 12993 |
{ |
| 12994 |
if (func.self != ir.default_entry_point) |
| 12995 |
add_function_overload(func); |
| 12996 |
|
| 12997 |
// Avoid shadow declarations. |
| 12998 |
local_variable_names = resource_names; |
| 12999 |
|
| 13000 |
string decl; |
| 13001 |
|
| 13002 |
auto &type = get<SPIRType>(func.return_type); |
| 13003 |
decl += flags_to_qualifiers_glsl(type, return_flags); |
| 13004 |
decl += type_to_glsl(type); |
| 13005 |
decl += type_to_array_glsl(type); |
| 13006 |
decl += " "; |
| 13007 |
|
| 13008 |
if (func.self == ir.default_entry_point) |
| 13009 |
{ |
| 13010 |
// If we need complex fallback in GLSL, we just wrap main() in a function |
| 13011 |
// and interlock the entire shader ... |
| 13012 |
if (interlocked_is_complex) |
| 13013 |
decl += "spvMainInterlockedBody"; |
| 13014 |
else |
| 13015 |
decl += "main"; |
| 13016 |
|
| 13017 |
processing_entry_point = true; |
| 13018 |
} |
| 13019 |
else |
| 13020 |
decl += to_name(func.self); |
| 13021 |
|
| 13022 |
decl += "("; |
| 13023 |
SmallVector<string> arglist; |
| 13024 |
for (auto &arg : func.arguments) |
| 13025 |
{ |
| 13026 |
// Do not pass in separate images or samplers if we're remapping |
| 13027 |
// to combined image samplers. |
| 13028 |
if (skip_argument(arg.id)) |
| 13029 |
continue; |
| 13030 |
|
| 13031 |
// Might change the variable name if it already exists in this function. |
| 13032 |
// SPIRV OpName doesn't have any semantic effect, so it's valid for an implementation |
| 13033 |
// to use same name for variables. |
| 13034 |
// Since we want to make the GLSL debuggable and somewhat sane, use fallback names for variables which are duplicates. |
| 13035 |
add_local_variable_name(arg.id); |
| 13036 |
|
| 13037 |
arglist.push_back(argument_decl(arg)); |
| 13038 |
|
| 13039 |
// Hold a pointer to the parameter so we can invalidate the readonly field if needed. |
| 13040 |
auto *var = maybe_get<SPIRVariable>(arg.id); |
| 13041 |
if (var) |
| 13042 |
var->parameter = &arg; |
| 13043 |
} |
| 13044 |
|
| 13045 |
for (auto &arg : func.shadow_arguments) |
| 13046 |
{ |
| 13047 |
// Might change the variable name if it already exists in this function. |
| 13048 |
// SPIRV OpName doesn't have any semantic effect, so it's valid for an implementation |
| 13049 |
// to use same name for variables. |
| 13050 |
// Since we want to make the GLSL debuggable and somewhat sane, use fallback names for variables which are duplicates. |
| 13051 |
add_local_variable_name(arg.id); |
| 13052 |
|
| 13053 |
arglist.push_back(argument_decl(arg)); |
| 13054 |
|
| 13055 |
// Hold a pointer to the parameter so we can invalidate the readonly field if needed. |
| 13056 |
auto *var = maybe_get<SPIRVariable>(arg.id); |
| 13057 |
if (var) |
| 13058 |
var->parameter = &arg; |
| 13059 |
} |
| 13060 |
|
| 13061 |
decl += merge(arglist); |
| 13062 |
decl += ")"; |
| 13063 |
statement(decl); |
| 13064 |
} |
| 13065 |
|
| 13066 |
void CompilerGLSL::emit_function(SPIRFunction &func, const Bitset &return_flags) |
| 13067 |
{ |
| 13068 |
// Avoid potential cycles. |
| 13069 |
if (func.active) |
| 13070 |
return; |
| 13071 |
func.active = true; |
| 13072 |
|
| 13073 |
// If we depend on a function, emit that function before we emit our own function. |
| 13074 |
for (auto block : func.blocks) |
| 13075 |
{ |
| 13076 |
auto &b = get<SPIRBlock>(block); |
| 13077 |
for (auto &i : b.ops) |
| 13078 |
{ |
| 13079 |
auto ops = stream(i); |
| 13080 |
auto op = static_cast<Op>(i.op); |
| 13081 |
|
| 13082 |
if (op == OpFunctionCall) |
| 13083 |
{ |
| 13084 |
// Recursively emit functions which are called. |
| 13085 |
uint32_t id = ops[2]; |
| 13086 |
emit_function(get<SPIRFunction>(id), ir.meta[ops[1]].decoration.decoration_flags); |
| 13087 |
} |
| 13088 |
} |
| 13089 |
} |
| 13090 |
|
| 13091 |
if (func.entry_line.file_id != 0) |
| 13092 |
emit_line_directive(func.entry_line.file_id, func.entry_line.line_literal); |
| 13093 |
emit_function_prototype(func, return_flags); |
| 13094 |
begin_scope(); |
| 13095 |
|
| 13096 |
if (func.self == ir.default_entry_point) |
| 13097 |
emit_entry_point_declarations(); |
| 13098 |
|
| 13099 |
current_function = &func; |
| 13100 |
auto &entry_block = get<SPIRBlock>(func.entry_block); |
| 13101 |
|
| 13102 |
sort(begin(func.constant_arrays_needed_on_stack), end(func.constant_arrays_needed_on_stack)); |
| 13103 |
for (auto &array : func.constant_arrays_needed_on_stack) |
| 13104 |
{ |
| 13105 |
auto &c = get<SPIRConstant>(array); |
| 13106 |
auto &type = get<SPIRType>(c.constant_type); |
| 13107 |
statement(variable_decl(type, join("_", array, "_array_copy")), " = ", constant_expression(c), ";"); |
| 13108 |
} |
| 13109 |
|
| 13110 |
for (auto &v : func.local_variables) |
| 13111 |
{ |
| 13112 |
auto &var = get<SPIRVariable>(v); |
| 13113 |
var.deferred_declaration = false; |
| 13114 |
|
| 13115 |
if (var.storage == StorageClassWorkgroup) |
| 13116 |
{ |
| 13117 |
// Special variable type which cannot have initializer, |
| 13118 |
// need to be declared as standalone variables. |
| 13119 |
// Comes from MSL which can push global variables as local variables in main function. |
| 13120 |
add_local_variable_name(var.self); |
| 13121 |
statement(variable_decl(var), ";"); |
| 13122 |
var.deferred_declaration = false; |
| 13123 |
} |
| 13124 |
else if (var.storage == StorageClassPrivate) |
| 13125 |
{ |
| 13126 |
// These variables will not have had their CFG usage analyzed, so move it to the entry block. |
| 13127 |
// Comes from MSL which can push global variables as local variables in main function. |
| 13128 |
// We could just declare them right now, but we would miss out on an important initialization case which is |
| 13129 |
// LUT declaration in MSL. |
| 13130 |
// If we don't declare the variable when it is assigned we're forced to go through a helper function |
| 13131 |
// which copies elements one by one. |
| 13132 |
add_local_variable_name(var.self); |
| 13133 |
|
| 13134 |
if (var.initializer) |
| 13135 |
{ |
| 13136 |
statement(variable_decl(var), ";"); |
| 13137 |
var.deferred_declaration = false; |
| 13138 |
} |
| 13139 |
else |
| 13140 |
{ |
| 13141 |
auto &dominated = entry_block.dominated_variables; |
| 13142 |
if (find(begin(dominated), end(dominated), var.self) == end(dominated)) |
| 13143 |
entry_block.dominated_variables.push_back(var.self); |
| 13144 |
var.deferred_declaration = true; |
| 13145 |
} |
| 13146 |
} |
| 13147 |
else if (var.storage == StorageClassFunction && var.remapped_variable && var.static_expression) |
| 13148 |
{ |
| 13149 |
// No need to declare this variable, it has a static expression. |
| 13150 |
var.deferred_declaration = false; |
| 13151 |
} |
| 13152 |
else if (expression_is_lvalue(v)) |
| 13153 |
{ |
| 13154 |
add_local_variable_name(var.self); |
| 13155 |
|
| 13156 |
// Loop variables should never be declared early, they are explicitly emitted in a loop. |
| 13157 |
if (var.initializer && !var.loop_variable) |
| 13158 |
statement(variable_decl_function_local(var), ";"); |
| 13159 |
else |
| 13160 |
{ |
| 13161 |
// Don't declare variable until first use to declutter the GLSL output quite a lot. |
| 13162 |
// If we don't touch the variable before first branch, |
| 13163 |
// declare it then since we need variable declaration to be in top scope. |
| 13164 |
var.deferred_declaration = true; |
| 13165 |
} |
| 13166 |
} |
| 13167 |
else |
| 13168 |
{ |
| 13169 |
// HACK: SPIR-V in older glslang output likes to use samplers and images as local variables, but GLSL does not allow this. |
| 13170 |
// For these types (non-lvalue), we enforce forwarding through a shadowed variable. |
| 13171 |
// This means that when we OpStore to these variables, we just write in the expression ID directly. |
| 13172 |
// This breaks any kind of branching, since the variable must be statically assigned. |
| 13173 |
// Branching on samplers and images would be pretty much impossible to fake in GLSL. |
| 13174 |
var.statically_assigned = true; |
| 13175 |
} |
| 13176 |
|
| 13177 |
var.loop_variable_enable = false; |
| 13178 |
|
| 13179 |
// Loop variables are never declared outside their for-loop, so block any implicit declaration. |
| 13180 |
if (var.loop_variable) |
| 13181 |
var.deferred_declaration = false; |
| 13182 |
} |
| 13183 |
|
| 13184 |
// Enforce declaration order for regression testing purposes. |
| 13185 |
for (auto &block_id : func.blocks) |
| 13186 |
{ |
| 13187 |
auto &block = get<SPIRBlock>(block_id); |
| 13188 |
sort(begin(block.dominated_variables), end(block.dominated_variables)); |
| 13189 |
} |
| 13190 |
|
| 13191 |
for (auto &line : current_function->fixup_hooks_in) |
| 13192 |
line(); |
| 13193 |
|
| 13194 |
emit_block_chain(entry_block); |
| 13195 |
|
| 13196 |
end_scope(); |
| 13197 |
processing_entry_point = false; |
| 13198 |
statement(""); |
| 13199 |
|
| 13200 |
// Make sure deferred declaration state for local variables is cleared when we are done with function. |
| 13201 |
// We risk declaring Private/Workgroup variables in places we are not supposed to otherwise. |
| 13202 |
for (auto &v : func.local_variables) |
| 13203 |
{ |
| 13204 |
auto &var = get<SPIRVariable>(v); |
| 13205 |
var.deferred_declaration = false; |
| 13206 |
} |
| 13207 |
} |
| 13208 |
|
| 13209 |
void CompilerGLSL::emit_fixup() |
| 13210 |
{ |
| 13211 |
if (is_vertex_like_shader()) |
| 13212 |
{ |
| 13213 |
if (options.vertex.fixup_clipspace) |
| 13214 |
{ |
| 13215 |
const char *suffix = backend.float_literal_suffix ? "f" : ""; |
| 13216 |
statement("gl_Position.z = 2.0", suffix, " * gl_Position.z - gl_Position.w;"); |
| 13217 |
} |
| 13218 |
|
| 13219 |
if (options.vertex.flip_vert_y) |
| 13220 |
statement("gl_Position.y = -gl_Position.y;"); |
| 13221 |
} |
| 13222 |
} |
| 13223 |
|
| 13224 |
void CompilerGLSL::flush_phi(BlockID from, BlockID to) |
| 13225 |
{ |
| 13226 |
auto &child = get<SPIRBlock>(to); |
| 13227 |
if (child.ignore_phi_from_block == from) |
| 13228 |
return; |
| 13229 |
|
| 13230 |
unordered_set<uint32_t> temporary_phi_variables; |
| 13231 |
|
| 13232 |
for (auto itr = begin(child.phi_variables); itr != end(child.phi_variables); ++itr) |
| 13233 |
{ |
| 13234 |
auto &phi = *itr; |
| 13235 |
|
| 13236 |
if (phi.parent == from) |
| 13237 |
{ |
| 13238 |
auto &var = get<SPIRVariable>(phi.function_variable); |
| 13239 |
|
| 13240 |
// A Phi variable might be a loop variable, so flush to static expression. |
| 13241 |
if (var.loop_variable && !var.loop_variable_enable) |
| 13242 |
var.static_expression = phi.local_variable; |
| 13243 |
else |
| 13244 |
{ |
| 13245 |
flush_variable_declaration(phi.function_variable); |
| 13246 |
|
| 13247 |
// Check if we are going to write to a Phi variable that another statement will read from |
| 13248 |
// as part of another Phi node in our target block. |
| 13249 |
// For this case, we will need to copy phi.function_variable to a temporary, and use that for future reads. |
| 13250 |
// This is judged to be extremely rare, so deal with it here using a simple, but suboptimal algorithm. |
| 13251 |
bool need_saved_temporary = |
| 13252 |
find_if(itr + 1, end(child.phi_variables), [&](const SPIRBlock::Phi &future_phi) -> bool { |
| 13253 |
return future_phi.local_variable == ID(phi.function_variable) && future_phi.parent == from; |
| 13254 |
}) != end(child.phi_variables); |
| 13255 |
|
| 13256 |
if (need_saved_temporary) |
| 13257 |
{ |
| 13258 |
// Need to make sure we declare the phi variable with a copy at the right scope. |
| 13259 |
// We cannot safely declare a temporary here since we might be inside a continue block. |
| 13260 |
if (!var.allocate_temporary_copy) |
| 13261 |
{ |
| 13262 |
var.allocate_temporary_copy = true; |
| 13263 |
force_recompile(); |
| 13264 |
} |
| 13265 |
statement("_", phi.function_variable, "_copy", " = ", to_name(phi.function_variable), ";"); |
| 13266 |
temporary_phi_variables.insert(phi.function_variable); |
| 13267 |
} |
| 13268 |
|
| 13269 |
// This might be called in continue block, so make sure we |
| 13270 |
// use this to emit ESSL 1.0 compliant increments/decrements. |
| 13271 |
auto lhs = to_expression(phi.function_variable); |
| 13272 |
|
| 13273 |
string rhs; |
| 13274 |
if (temporary_phi_variables.count(phi.local_variable)) |
| 13275 |
rhs = join("_", phi.local_variable, "_copy"); |
| 13276 |
else |
| 13277 |
rhs = to_pointer_expression(phi.local_variable); |
| 13278 |
|
| 13279 |
if (!optimize_read_modify_write(get<SPIRType>(var.basetype), lhs, rhs)) |
| 13280 |
statement(lhs, " = ", rhs, ";"); |
| 13281 |
} |
| 13282 |
|
| 13283 |
register_write(phi.function_variable); |
| 13284 |
} |
| 13285 |
} |
| 13286 |
} |
| 13287 |
|
| 13288 |
void CompilerGLSL::branch_to_continue(BlockID from, BlockID to) |
| 13289 |
{ |
| 13290 |
auto &to_block = get<SPIRBlock>(to); |
| 13291 |
if (from == to) |
| 13292 |
return; |
| 13293 |
|
| 13294 |
assert(is_continue(to)); |
| 13295 |
if (to_block.complex_continue) |
| 13296 |
{ |
| 13297 |
// Just emit the whole block chain as is. |
| 13298 |
auto usage_counts = expression_usage_counts; |
| 13299 |
|
| 13300 |
emit_block_chain(to_block); |
| 13301 |
|
| 13302 |
// Expression usage counts are moot after returning from the continue block. |
| 13303 |
expression_usage_counts = usage_counts; |
| 13304 |
} |
| 13305 |
else |
| 13306 |
{ |
| 13307 |
auto &from_block = get<SPIRBlock>(from); |
| 13308 |
bool outside_control_flow = false; |
| 13309 |
uint32_t loop_dominator = 0; |
| 13310 |
|
| 13311 |
// FIXME: Refactor this to not use the old loop_dominator tracking. |
| 13312 |
if (from_block.merge_block) |
| 13313 |
{ |
| 13314 |
// If we are a loop header, we don't set the loop dominator, |
| 13315 |
// so just use "self" here. |
| 13316 |
loop_dominator = from; |
| 13317 |
} |
| 13318 |
else if (from_block.loop_dominator != BlockID(SPIRBlock::NoDominator)) |
| 13319 |
{ |
| 13320 |
loop_dominator = from_block.loop_dominator; |
| 13321 |
} |
| 13322 |
|
| 13323 |
if (loop_dominator != 0) |
| 13324 |
{ |
| 13325 |
auto &cfg = get_cfg_for_current_function(); |
| 13326 |
|
| 13327 |
// For non-complex continue blocks, we implicitly branch to the continue block |
| 13328 |
// by having the continue block be part of the loop header in for (; ; continue-block). |
| 13329 |
outside_control_flow = cfg.node_terminates_control_flow_in_sub_graph(loop_dominator, from); |
| 13330 |
} |
| 13331 |
|
| 13332 |
// Some simplification for for-loops. We always end up with a useless continue; |
| 13333 |
// statement since we branch to a loop block. |
| 13334 |
// Walk the CFG, if we unconditionally execute the block calling continue assuming we're in the loop block, |
| 13335 |
// we can avoid writing out an explicit continue statement. |
| 13336 |
// Similar optimization to return statements if we know we're outside flow control. |
| 13337 |
if (!outside_control_flow) |
| 13338 |
statement("continue;"); |
| 13339 |
} |
| 13340 |
} |
| 13341 |
|
| 13342 |
void CompilerGLSL::branch(BlockID from, BlockID to) |
| 13343 |
{ |
| 13344 |
flush_phi(from, to); |
| 13345 |
flush_control_dependent_expressions(from); |
| 13346 |
|
| 13347 |
bool to_is_continue = is_continue(to); |
| 13348 |
|
| 13349 |
// This is only a continue if we branch to our loop dominator. |
| 13350 |
if ((ir.block_meta[to] & ParsedIR::BLOCK_META_LOOP_HEADER_BIT) != 0 && get<SPIRBlock>(from).loop_dominator == to) |
| 13351 |
{ |
| 13352 |
// This can happen if we had a complex continue block which was emitted. |
| 13353 |
// Once the continue block tries to branch to the loop header, just emit continue; |
| 13354 |
// and end the chain here. |
| 13355 |
statement("continue;"); |
| 13356 |
} |
| 13357 |
else if (from != to && is_break(to)) |
| 13358 |
{ |
| 13359 |
// We cannot break to ourselves, so check explicitly for from != to. |
| 13360 |
// This case can trigger if a loop header is all three of these things: |
| 13361 |
// - Continue block |
| 13362 |
// - Loop header |
| 13363 |
// - Break merge target all at once ... |
| 13364 |
|
| 13365 |
// Very dirty workaround. |
| 13366 |
// Switch constructs are able to break, but they cannot break out of a loop at the same time. |
| 13367 |
// Only sensible solution is to make a ladder variable, which we declare at the top of the switch block, |
| 13368 |
// write to the ladder here, and defer the break. |
| 13369 |
// The loop we're breaking out of must dominate the switch block, or there is no ladder breaking case. |
| 13370 |
if (current_emitting_switch && is_loop_break(to) && |
| 13371 |
current_emitting_switch->loop_dominator != BlockID(SPIRBlock::NoDominator) && |
| 13372 |
get<SPIRBlock>(current_emitting_switch->loop_dominator).merge_block == to) |
| 13373 |
{ |
| 13374 |
if (!current_emitting_switch->need_ladder_break) |
| 13375 |
{ |
| 13376 |
force_recompile(); |
| 13377 |
current_emitting_switch->need_ladder_break = true; |
| 13378 |
} |
| 13379 |
|
| 13380 |
statement("_", current_emitting_switch->self, "_ladder_break = true;"); |
| 13381 |
} |
| 13382 |
statement("break;"); |
| 13383 |
} |
| 13384 |
else if (to_is_continue || from == to) |
| 13385 |
{ |
| 13386 |
// For from == to case can happen for a do-while loop which branches into itself. |
| 13387 |
// We don't mark these cases as continue blocks, but the only possible way to branch into |
| 13388 |
// ourselves is through means of continue blocks. |
| 13389 |
|
| 13390 |
// If we are merging to a continue block, there is no need to emit the block chain for continue here. |
| 13391 |
// We can branch to the continue block after we merge execution. |
| 13392 |
|
| 13393 |
// Here we make use of structured control flow rules from spec: |
| 13394 |
// 2.11: - the merge block declared by a header block cannot be a merge block declared by any other header block |
| 13395 |
// - each header block must strictly dominate its merge block, unless the merge block is unreachable in the CFG |
| 13396 |
// If we are branching to a merge block, we must be inside a construct which dominates the merge block. |
| 13397 |
auto &block_meta = ir.block_meta[to]; |
| 13398 |
bool branching_to_merge = |
| 13399 |
(block_meta & (ParsedIR::BLOCK_META_SELECTION_MERGE_BIT | ParsedIR::BLOCK_META_MULTISELECT_MERGE_BIT | |
| 13400 |
ParsedIR::BLOCK_META_LOOP_MERGE_BIT)) != 0; |
| 13401 |
if (!to_is_continue || !branching_to_merge) |
| 13402 |
branch_to_continue(from, to); |
| 13403 |
} |
| 13404 |
else if (!is_conditional(to)) |
| 13405 |
emit_block_chain(get<SPIRBlock>(to)); |
| 13406 |
|
| 13407 |
// It is important that we check for break before continue. |
| 13408 |
// A block might serve two purposes, a break block for the inner scope, and |
| 13409 |
// a continue block in the outer scope. |
| 13410 |
// Inner scope always takes precedence. |
| 13411 |
} |
| 13412 |
|
| 13413 |
void CompilerGLSL::branch(BlockID from, uint32_t cond, BlockID true_block, BlockID false_block) |
| 13414 |
{ |
| 13415 |
auto &from_block = get<SPIRBlock>(from); |
| 13416 |
BlockID merge_block = from_block.merge == SPIRBlock::MergeSelection ? from_block.next_block : BlockID(0); |
| 13417 |
|
| 13418 |
// If we branch directly to our selection merge target, we don't need a code path. |
| 13419 |
bool true_block_needs_code = true_block != merge_block || flush_phi_required(from, true_block); |
| 13420 |
bool false_block_needs_code = false_block != merge_block || flush_phi_required(from, false_block); |
| 13421 |
|
| 13422 |
if (!true_block_needs_code && !false_block_needs_code) |
| 13423 |
return; |
| 13424 |
|
| 13425 |
emit_block_hints(get<SPIRBlock>(from)); |
| 13426 |
|
| 13427 |
if (true_block_needs_code) |
| 13428 |
{ |
| 13429 |
statement("if (", to_expression(cond), ")"); |
| 13430 |
begin_scope(); |
| 13431 |
branch(from, true_block); |
| 13432 |
end_scope(); |
| 13433 |
|
| 13434 |
if (false_block_needs_code) |
| 13435 |
{ |
| 13436 |
statement("else"); |
| 13437 |
begin_scope(); |
| 13438 |
branch(from, false_block); |
| 13439 |
end_scope(); |
| 13440 |
} |
| 13441 |
} |
| 13442 |
else if (false_block_needs_code) |
| 13443 |
{ |
| 13444 |
// Only need false path, use negative conditional. |
| 13445 |
statement("if (!", to_enclosed_expression(cond), ")"); |
| 13446 |
begin_scope(); |
| 13447 |
branch(from, false_block); |
| 13448 |
end_scope(); |
| 13449 |
} |
| 13450 |
} |
| 13451 |
|
| 13452 |
// FIXME: This currently cannot handle complex continue blocks |
| 13453 |
// as in do-while. |
| 13454 |
// This should be seen as a "trivial" continue block. |
| 13455 |
string CompilerGLSL::emit_continue_block(uint32_t continue_block, bool follow_true_block, bool follow_false_block) |
| 13456 |
{ |
| 13457 |
auto *block = &get<SPIRBlock>(continue_block); |
| 13458 |
|
| 13459 |
// While emitting the continue block, declare_temporary will check this |
| 13460 |
// if we have to emit temporaries. |
| 13461 |
current_continue_block = block; |
| 13462 |
|
| 13463 |
SmallVector<string> statements; |
| 13464 |
|
| 13465 |
// Capture all statements into our list. |
| 13466 |
auto *old = redirect_statement; |
| 13467 |
redirect_statement = &statements; |
| 13468 |
|
| 13469 |
// Stamp out all blocks one after each other. |
| 13470 |
while ((ir.block_meta[block->self] & ParsedIR::BLOCK_META_LOOP_HEADER_BIT) == 0) |
| 13471 |
{ |
| 13472 |
// Write out all instructions we have in this block. |
| 13473 |
emit_block_instructions(*block); |
| 13474 |
|
| 13475 |
// For plain branchless for/while continue blocks. |
| 13476 |
if (block->next_block) |
| 13477 |
{ |
| 13478 |
flush_phi(continue_block, block->next_block); |
| 13479 |
block = &get<SPIRBlock>(block->next_block); |
| 13480 |
} |
| 13481 |
// For do while blocks. The last block will be a select block. |
| 13482 |
else if (block->true_block && follow_true_block) |
| 13483 |
{ |
| 13484 |
flush_phi(continue_block, block->true_block); |
| 13485 |
block = &get<SPIRBlock>(block->true_block); |
| 13486 |
} |
| 13487 |
else if (block->false_block && follow_false_block) |
| 13488 |
{ |
| 13489 |
flush_phi(continue_block, block->false_block); |
| 13490 |
block = &get<SPIRBlock>(block->false_block); |
| 13491 |
} |
| 13492 |
else |
| 13493 |
{ |
| 13494 |
SPIRV_CROSS_THROW("Invalid continue block detected!"); |
| 13495 |
} |
| 13496 |
} |
| 13497 |
|
| 13498 |
// Restore old pointer. |
| 13499 |
redirect_statement = old; |
| 13500 |
|
| 13501 |
// Somewhat ugly, strip off the last ';' since we use ',' instead. |
| 13502 |
// Ideally, we should select this behavior in statement(). |
| 13503 |
for (auto &s : statements) |
| 13504 |
{ |
| 13505 |
if (!s.empty() && s.back() == ';') |
| 13506 |
s.erase(s.size() - 1, 1); |
| 13507 |
} |
| 13508 |
|
| 13509 |
current_continue_block = nullptr; |
| 13510 |
return merge(statements); |
| 13511 |
} |
| 13512 |
|
| 13513 |
void CompilerGLSL::emit_while_loop_initializers(const SPIRBlock &block) |
| 13514 |
{ |
| 13515 |
// While loops do not take initializers, so declare all of them outside. |
| 13516 |
for (auto &loop_var : block.loop_variables) |
| 13517 |
{ |
| 13518 |
auto &var = get<SPIRVariable>(loop_var); |
| 13519 |
statement(variable_decl(var), ";"); |
| 13520 |
} |
| 13521 |
} |
| 13522 |
|
| 13523 |
string CompilerGLSL::emit_for_loop_initializers(const SPIRBlock &block) |
| 13524 |
{ |
| 13525 |
if (block.loop_variables.empty()) |
| 13526 |
return ""; |
| 13527 |
|
| 13528 |
bool same_types = for_loop_initializers_are_same_type(block); |
| 13529 |
// We can only declare for loop initializers if all variables are of same type. |
| 13530 |
// If we cannot do this, declare individual variables before the loop header. |
| 13531 |
|
| 13532 |
// We might have a loop variable candidate which was not assigned to for some reason. |
| 13533 |
uint32_t missing_initializers = 0; |
| 13534 |
for (auto &variable : block.loop_variables) |
| 13535 |
{ |
| 13536 |
uint32_t expr = get<SPIRVariable>(variable).static_expression; |
| 13537 |
|
| 13538 |
// Sometimes loop variables are initialized with OpUndef, but we can just declare |
| 13539 |
// a plain variable without initializer in this case. |
| 13540 |
if (expr == 0 || ir.ids[expr].get_type() == TypeUndef) |
| 13541 |
missing_initializers++; |
| 13542 |
} |
| 13543 |
|
| 13544 |
if (block.loop_variables.size() == 1 && missing_initializers == 0) |
| 13545 |
{ |
| 13546 |
return variable_decl(get<SPIRVariable>(block.loop_variables.front())); |
| 13547 |
} |
| 13548 |
else if (!same_types || missing_initializers == uint32_t(block.loop_variables.size())) |
| 13549 |
{ |
| 13550 |
for (auto &loop_var : block.loop_variables) |
| 13551 |
statement(variable_decl(get<SPIRVariable>(loop_var)), ";"); |
| 13552 |
return ""; |
| 13553 |
} |
| 13554 |
else |
| 13555 |
{ |
| 13556 |
// We have a mix of loop variables, either ones with a clear initializer, or ones without. |
| 13557 |
// Separate the two streams. |
| 13558 |
string expr; |
| 13559 |
|
| 13560 |
for (auto &loop_var : block.loop_variables) |
| 13561 |
{ |
| 13562 |
uint32_t static_expr = get<SPIRVariable>(loop_var).static_expression; |
| 13563 |
if (static_expr == 0 || ir.ids[static_expr].get_type() == TypeUndef) |
| 13564 |
{ |
| 13565 |
statement(variable_decl(get<SPIRVariable>(loop_var)), ";"); |
| 13566 |
} |
| 13567 |
else |
| 13568 |
{ |
| 13569 |
auto &var = get<SPIRVariable>(loop_var); |
| 13570 |
auto &type = get_variable_data_type(var); |
| 13571 |
if (expr.empty()) |
| 13572 |
{ |
| 13573 |
// For loop initializers are of the form <type id = value, id = value, id = value, etc ... |
| 13574 |
expr = join(to_qualifiers_glsl(var.self), type_to_glsl(type), " "); |
| 13575 |
} |
| 13576 |
else |
| 13577 |
{ |
| 13578 |
expr += ", "; |
| 13579 |
// In MSL, being based on C++, the asterisk marking a pointer |
| 13580 |
// binds to the identifier, not the type. |
| 13581 |
if (type.pointer) |
| 13582 |
expr += "* "; |
| 13583 |
} |
| 13584 |
|
| 13585 |
expr += join(to_name(loop_var), " = ", to_pointer_expression(var.static_expression)); |
| 13586 |
} |
| 13587 |
} |
| 13588 |
return expr; |
| 13589 |
} |
| 13590 |
} |
| 13591 |
|
| 13592 |
bool CompilerGLSL::for_loop_initializers_are_same_type(const SPIRBlock &block) |
| 13593 |
{ |
| 13594 |
if (block.loop_variables.size() <= 1) |
| 13595 |
return true; |
| 13596 |
|
| 13597 |
uint32_t expected = 0; |
| 13598 |
Bitset expected_flags; |
| 13599 |
for (auto &var : block.loop_variables) |
| 13600 |
{ |
| 13601 |
// Don't care about uninitialized variables as they will not be part of the initializers. |
| 13602 |
uint32_t expr = get<SPIRVariable>(var).static_expression; |
| 13603 |
if (expr == 0 || ir.ids[expr].get_type() == TypeUndef) |
| 13604 |
continue; |
| 13605 |
|
| 13606 |
if (expected == 0) |
| 13607 |
{ |
| 13608 |
expected = get<SPIRVariable>(var).basetype; |
| 13609 |
expected_flags = get_decoration_bitset(var); |
| 13610 |
} |
| 13611 |
else if (expected != get<SPIRVariable>(var).basetype) |
| 13612 |
return false; |
| 13613 |
|
| 13614 |
// Precision flags and things like that must also match. |
| 13615 |
if (expected_flags != get_decoration_bitset(var)) |
| 13616 |
return false; |
| 13617 |
} |
| 13618 |
|
| 13619 |
return true; |
| 13620 |
} |
| 13621 |
|
| 13622 |
bool CompilerGLSL::attempt_emit_loop_header(SPIRBlock &block, SPIRBlock::Method method) |
| 13623 |
{ |
| 13624 |
SPIRBlock::ContinueBlockType continue_type = continue_block_type(get<SPIRBlock>(block.continue_block)); |
| 13625 |
|
| 13626 |
if (method == SPIRBlock::MergeToSelectForLoop || method == SPIRBlock::MergeToSelectContinueForLoop) |
| 13627 |
{ |
| 13628 |
uint32_t current_count = statement_count; |
| 13629 |
// If we're trying to create a true for loop, |
| 13630 |
// we need to make sure that all opcodes before branch statement do not actually emit any code. |
| 13631 |
// We can then take the condition expression and create a for (; cond ; ) { body; } structure instead. |
| 13632 |
emit_block_instructions(block); |
| 13633 |
|
| 13634 |
bool condition_is_temporary = forced_temporaries.find(block.condition) == end(forced_temporaries); |
| 13635 |
|
| 13636 |
// This can work! We only did trivial things which could be forwarded in block body! |
| 13637 |
if (current_count == statement_count && condition_is_temporary) |
| 13638 |
{ |
| 13639 |
switch (continue_type) |
| 13640 |
{ |
| 13641 |
case SPIRBlock::ForLoop: |
| 13642 |
{ |
| 13643 |
// This block may be a dominating block, so make sure we flush undeclared variables before building the for loop header. |
| 13644 |
flush_undeclared_variables(block); |
| 13645 |
|
| 13646 |
// Important that we do this in this order because |
| 13647 |
// emitting the continue block can invalidate the condition expression. |
| 13648 |
auto initializer = emit_for_loop_initializers(block); |
| 13649 |
auto condition = to_expression(block.condition); |
| 13650 |
|
| 13651 |
// Condition might have to be inverted. |
| 13652 |
if (execution_is_noop(get<SPIRBlock>(block.true_block), get<SPIRBlock>(block.merge_block))) |
| 13653 |
condition = join("!", enclose_expression(condition)); |
| 13654 |
|
| 13655 |
emit_block_hints(block); |
| 13656 |
if (method != SPIRBlock::MergeToSelectContinueForLoop) |
| 13657 |
{ |
| 13658 |
auto continue_block = emit_continue_block(block.continue_block, false, false); |
| 13659 |
statement("for (", initializer, "; ", condition, "; ", continue_block, ")"); |
| 13660 |
} |
| 13661 |
else |
| 13662 |
statement("for (", initializer, "; ", condition, "; )"); |
| 13663 |
break; |
| 13664 |
} |
| 13665 |
|
| 13666 |
case SPIRBlock::WhileLoop: |
| 13667 |
{ |
| 13668 |
// This block may be a dominating block, so make sure we flush undeclared variables before building the while loop header. |
| 13669 |
flush_undeclared_variables(block); |
| 13670 |
emit_while_loop_initializers(block); |
| 13671 |
emit_block_hints(block); |
| 13672 |
|
| 13673 |
auto condition = to_expression(block.condition); |
| 13674 |
// Condition might have to be inverted. |
| 13675 |
if (execution_is_noop(get<SPIRBlock>(block.true_block), get<SPIRBlock>(block.merge_block))) |
| 13676 |
condition = join("!", enclose_expression(condition)); |
| 13677 |
|
| 13678 |
statement("while (", condition, ")"); |
| 13679 |
break; |
| 13680 |
} |
| 13681 |
|
| 13682 |
default: |
| 13683 |
block.disable_block_optimization = true; |
| 13684 |
force_recompile(); |
| 13685 |
begin_scope(); // We'll see an end_scope() later. |
| 13686 |
return false; |
| 13687 |
} |
| 13688 |
|
| 13689 |
begin_scope(); |
| 13690 |
return true; |
| 13691 |
} |
| 13692 |
else |
| 13693 |
{ |
| 13694 |
block.disable_block_optimization = true; |
| 13695 |
force_recompile(); |
| 13696 |
begin_scope(); // We'll see an end_scope() later. |
| 13697 |
return false; |
| 13698 |
} |
| 13699 |
} |
| 13700 |
else if (method == SPIRBlock::MergeToDirectForLoop) |
| 13701 |
{ |
| 13702 |
auto &child = get<SPIRBlock>(block.next_block); |
| 13703 |
|
| 13704 |
// This block may be a dominating block, so make sure we flush undeclared variables before building the for loop header. |
| 13705 |
flush_undeclared_variables(child); |
| 13706 |
|
| 13707 |
uint32_t current_count = statement_count; |
| 13708 |
|
| 13709 |
// If we're trying to create a true for loop, |
| 13710 |
// we need to make sure that all opcodes before branch statement do not actually emit any code. |
| 13711 |
// We can then take the condition expression and create a for (; cond ; ) { body; } structure instead. |
| 13712 |
emit_block_instructions(child); |
| 13713 |
|
| 13714 |
bool condition_is_temporary = forced_temporaries.find(child.condition) == end(forced_temporaries); |
| 13715 |
|
| 13716 |
if (current_count == statement_count && condition_is_temporary) |
| 13717 |
{ |
| 13718 |
uint32_t target_block = child.true_block; |
| 13719 |
|
| 13720 |
switch (continue_type) |
| 13721 |
{ |
| 13722 |
case SPIRBlock::ForLoop: |
| 13723 |
{ |
| 13724 |
// Important that we do this in this order because |
| 13725 |
// emitting the continue block can invalidate the condition expression. |
| 13726 |
auto initializer = emit_for_loop_initializers(block); |
| 13727 |
auto condition = to_expression(child.condition); |
| 13728 |
|
| 13729 |
// Condition might have to be inverted. |
| 13730 |
if (execution_is_noop(get<SPIRBlock>(child.true_block), get<SPIRBlock>(block.merge_block))) |
| 13731 |
{ |
| 13732 |
condition = join("!", enclose_expression(condition)); |
| 13733 |
target_block = child.false_block; |
| 13734 |
} |
| 13735 |
|
| 13736 |
auto continue_block = emit_continue_block(block.continue_block, false, false); |
| 13737 |
emit_block_hints(block); |
| 13738 |
statement("for (", initializer, "; ", condition, "; ", continue_block, ")"); |
| 13739 |
break; |
| 13740 |
} |
| 13741 |
|
| 13742 |
case SPIRBlock::WhileLoop: |
| 13743 |
{ |
| 13744 |
emit_while_loop_initializers(block); |
| 13745 |
emit_block_hints(block); |
| 13746 |
|
| 13747 |
auto condition = to_expression(child.condition); |
| 13748 |
// Condition might have to be inverted. |
| 13749 |
if (execution_is_noop(get<SPIRBlock>(child.true_block), get<SPIRBlock>(block.merge_block))) |
| 13750 |
{ |
| 13751 |
condition = join("!", enclose_expression(condition)); |
| 13752 |
target_block = child.false_block; |
| 13753 |
} |
| 13754 |
|
| 13755 |
statement("while (", condition, ")"); |
| 13756 |
break; |
| 13757 |
} |
| 13758 |
|
| 13759 |
default: |
| 13760 |
block.disable_block_optimization = true; |
| 13761 |
force_recompile(); |
| 13762 |
begin_scope(); // We'll see an end_scope() later. |
| 13763 |
return false; |
| 13764 |
} |
| 13765 |
|
| 13766 |
begin_scope(); |
| 13767 |
branch(child.self, target_block); |
| 13768 |
return true; |
| 13769 |
} |
| 13770 |
else |
| 13771 |
{ |
| 13772 |
block.disable_block_optimization = true; |
| 13773 |
force_recompile(); |
| 13774 |
begin_scope(); // We'll see an end_scope() later. |
| 13775 |
return false; |
| 13776 |
} |
| 13777 |
} |
| 13778 |
else |
| 13779 |
return false; |
| 13780 |
} |
| 13781 |
|
| 13782 |
void CompilerGLSL::flush_undeclared_variables(SPIRBlock &block) |
| 13783 |
{ |
| 13784 |
for (auto &v : block.dominated_variables) |
| 13785 |
flush_variable_declaration(v); |
| 13786 |
} |
| 13787 |
|
| 13788 |
void CompilerGLSL::emit_hoisted_temporaries(SmallVector<pair<TypeID, ID>> &temporaries) |
| 13789 |
{ |
| 13790 |
// If we need to force temporaries for certain IDs due to continue blocks, do it before starting loop header. |
| 13791 |
// Need to sort these to ensure that reference output is stable. |
| 13792 |
sort(begin(temporaries), end(temporaries), |
| 13793 |
[](const pair<TypeID, ID> &a, const pair<TypeID, ID> &b) { return a.second < b.second; }); |
| 13794 |
|
| 13795 |
for (auto &tmp : temporaries) |
| 13796 |
{ |
| 13797 |
add_local_variable_name(tmp.second); |
| 13798 |
auto &flags = ir.meta[tmp.second].decoration.decoration_flags; |
| 13799 |
auto &type = get<SPIRType>(tmp.first); |
| 13800 |
|
| 13801 |
// Not all targets support pointer literals, so don't bother with that case. |
| 13802 |
string initializer; |
| 13803 |
if (options.force_zero_initialized_variables && type_can_zero_initialize(type)) |
| 13804 |
initializer = join(" = ", to_zero_initialized_expression(tmp.first)); |
| 13805 |
|
| 13806 |
statement(flags_to_qualifiers_glsl(type, flags), variable_decl(type, to_name(tmp.second)), initializer, ";"); |
| 13807 |
|
| 13808 |
hoisted_temporaries.insert(tmp.second); |
| 13809 |
forced_temporaries.insert(tmp.second); |
| 13810 |
|
| 13811 |
// The temporary might be read from before it's assigned, set up the expression now. |
| 13812 |
set<SPIRExpression>(tmp.second, to_name(tmp.second), tmp.first, true); |
| 13813 |
} |
| 13814 |
} |
| 13815 |
|
| 13816 |
void CompilerGLSL::emit_block_chain(SPIRBlock &block) |
| 13817 |
{ |
| 13818 |
bool select_branch_to_true_block = false; |
| 13819 |
bool select_branch_to_false_block = false; |
| 13820 |
bool skip_direct_branch = false; |
| 13821 |
bool emitted_loop_header_variables = false; |
| 13822 |
bool force_complex_continue_block = false; |
| 13823 |
ValueSaver<uint32_t> loop_level_saver(current_loop_level); |
| 13824 |
|
| 13825 |
if (block.merge == SPIRBlock::MergeLoop) |
| 13826 |
add_loop_level(); |
| 13827 |
|
| 13828 |
emit_hoisted_temporaries(block.declare_temporary); |
| 13829 |
|
| 13830 |
SPIRBlock::ContinueBlockType continue_type = SPIRBlock::ContinueNone; |
| 13831 |
if (block.continue_block) |
| 13832 |
{ |
| 13833 |
continue_type = continue_block_type(get<SPIRBlock>(block.continue_block)); |
| 13834 |
// If we know we cannot emit a loop, mark the block early as a complex loop so we don't force unnecessary recompiles. |
| 13835 |
if (continue_type == SPIRBlock::ComplexLoop) |
| 13836 |
block.complex_continue = true; |
| 13837 |
} |
| 13838 |
|
| 13839 |
// If we have loop variables, stop masking out access to the variable now. |
| 13840 |
for (auto var_id : block.loop_variables) |
| 13841 |
{ |
| 13842 |
auto &var = get<SPIRVariable>(var_id); |
| 13843 |
var.loop_variable_enable = true; |
| 13844 |
// We're not going to declare the variable directly, so emit a copy here. |
| 13845 |
emit_variable_temporary_copies(var); |
| 13846 |
} |
| 13847 |
|
| 13848 |
// Remember deferred declaration state. We will restore it before returning. |
| 13849 |
SmallVector<bool, 64> rearm_dominated_variables(block.dominated_variables.size()); |
| 13850 |
for (size_t i = 0; i < block.dominated_variables.size(); i++) |
| 13851 |
{ |
| 13852 |
uint32_t var_id = block.dominated_variables[i]; |
| 13853 |
auto &var = get<SPIRVariable>(var_id); |
| 13854 |
rearm_dominated_variables[i] = var.deferred_declaration; |
| 13855 |
} |
| 13856 |
|
| 13857 |
// This is the method often used by spirv-opt to implement loops. |
| 13858 |
// The loop header goes straight into the continue block. |
| 13859 |
// However, don't attempt this on ESSL 1.0, because if a loop variable is used in a continue block, |
| 13860 |
// it *MUST* be used in the continue block. This loop method will not work. |
| 13861 |
if (!is_legacy_es() && block_is_loop_candidate(block, SPIRBlock::MergeToSelectContinueForLoop)) |
| 13862 |
{ |
| 13863 |
flush_undeclared_variables(block); |
| 13864 |
if (attempt_emit_loop_header(block, SPIRBlock::MergeToSelectContinueForLoop)) |
| 13865 |
{ |
| 13866 |
if (execution_is_noop(get<SPIRBlock>(block.true_block), get<SPIRBlock>(block.merge_block))) |
| 13867 |
select_branch_to_false_block = true; |
| 13868 |
else |
| 13869 |
select_branch_to_true_block = true; |
| 13870 |
|
| 13871 |
emitted_loop_header_variables = true; |
| 13872 |
force_complex_continue_block = true; |
| 13873 |
} |
| 13874 |
} |
| 13875 |
// This is the older loop behavior in glslang which branches to loop body directly from the loop header. |
| 13876 |
else if (block_is_loop_candidate(block, SPIRBlock::MergeToSelectForLoop)) |
| 13877 |
{ |
| 13878 |
flush_undeclared_variables(block); |
| 13879 |
if (attempt_emit_loop_header(block, SPIRBlock::MergeToSelectForLoop)) |
| 13880 |
{ |
| 13881 |
// The body of while, is actually just the true (or false) block, so always branch there unconditionally. |
| 13882 |
if (execution_is_noop(get<SPIRBlock>(block.true_block), get<SPIRBlock>(block.merge_block))) |
| 13883 |
select_branch_to_false_block = true; |
| 13884 |
else |
| 13885 |
select_branch_to_true_block = true; |
| 13886 |
|
| 13887 |
emitted_loop_header_variables = true; |
| 13888 |
} |
| 13889 |
} |
| 13890 |
// This is the newer loop behavior in glslang which branches from Loop header directly to |
| 13891 |
// a new block, which in turn has a OpBranchSelection without a selection merge. |
| 13892 |
else if (block_is_loop_candidate(block, SPIRBlock::MergeToDirectForLoop)) |
| 13893 |
{ |
| 13894 |
flush_undeclared_variables(block); |
| 13895 |
if (attempt_emit_loop_header(block, SPIRBlock::MergeToDirectForLoop)) |
| 13896 |
{ |
| 13897 |
skip_direct_branch = true; |
| 13898 |
emitted_loop_header_variables = true; |
| 13899 |
} |
| 13900 |
} |
| 13901 |
else if (continue_type == SPIRBlock::DoWhileLoop) |
| 13902 |
{ |
| 13903 |
flush_undeclared_variables(block); |
| 13904 |
emit_while_loop_initializers(block); |
| 13905 |
emitted_loop_header_variables = true; |
| 13906 |
// We have some temporaries where the loop header is the dominator. |
| 13907 |
// We risk a case where we have code like: |
| 13908 |
// for (;;) { create-temporary; break; } consume-temporary; |
| 13909 |
// so force-declare temporaries here. |
| 13910 |
emit_hoisted_temporaries(block.potential_declare_temporary); |
| 13911 |
statement("do"); |
| 13912 |
begin_scope(); |
| 13913 |
|
| 13914 |
emit_block_instructions(block); |
| 13915 |
} |
| 13916 |
else if (block.merge == SPIRBlock::MergeLoop) |
| 13917 |
{ |
| 13918 |
flush_undeclared_variables(block); |
| 13919 |
emit_while_loop_initializers(block); |
| 13920 |
emitted_loop_header_variables = true; |
| 13921 |
|
| 13922 |
// We have a generic loop without any distinguishable pattern like for, while or do while. |
| 13923 |
get<SPIRBlock>(block.continue_block).complex_continue = true; |
| 13924 |
continue_type = SPIRBlock::ComplexLoop; |
| 13925 |
|
| 13926 |
// We have some temporaries where the loop header is the dominator. |
| 13927 |
// We risk a case where we have code like: |
| 13928 |
// for (;;) { create-temporary; break; } consume-temporary; |
| 13929 |
// so force-declare temporaries here. |
| 13930 |
emit_hoisted_temporaries(block.potential_declare_temporary); |
| 13931 |
statement("for (;;)"); |
| 13932 |
begin_scope(); |
| 13933 |
|
| 13934 |
emit_block_instructions(block); |
| 13935 |
} |
| 13936 |
else |
| 13937 |
{ |
| 13938 |
emit_block_instructions(block); |
| 13939 |
} |
| 13940 |
|
| 13941 |
// If we didn't successfully emit a loop header and we had loop variable candidates, we have a problem |
| 13942 |
// as writes to said loop variables might have been masked out, we need a recompile. |
| 13943 |
if (!emitted_loop_header_variables && !block.loop_variables.empty()) |
| 13944 |
{ |
| 13945 |
force_recompile(); |
| 13946 |
for (auto var : block.loop_variables) |
| 13947 |
get<SPIRVariable>(var).loop_variable = false; |
| 13948 |
block.loop_variables.clear(); |
| 13949 |
} |
| 13950 |
|
| 13951 |
flush_undeclared_variables(block); |
| 13952 |
bool emit_next_block = true; |
| 13953 |
|
| 13954 |
// Handle end of block. |
| 13955 |
switch (block.terminator) |
| 13956 |
{ |
| 13957 |
case SPIRBlock::Direct: |
| 13958 |
// True when emitting complex continue block. |
| 13959 |
if (block.loop_dominator == block.next_block) |
| 13960 |
{ |
| 13961 |
branch(block.self, block.next_block); |
| 13962 |
emit_next_block = false; |
| 13963 |
} |
| 13964 |
// True if MergeToDirectForLoop succeeded. |
| 13965 |
else if (skip_direct_branch) |
| 13966 |
emit_next_block = false; |
| 13967 |
else if (is_continue(block.next_block) || is_break(block.next_block) || is_conditional(block.next_block)) |
| 13968 |
{ |
| 13969 |
branch(block.self, block.next_block); |
| 13970 |
emit_next_block = false; |
| 13971 |
} |
| 13972 |
break; |
| 13973 |
|
| 13974 |
case SPIRBlock::Select: |
| 13975 |
// True if MergeToSelectForLoop or MergeToSelectContinueForLoop succeeded. |
| 13976 |
if (select_branch_to_true_block) |
| 13977 |
{ |
| 13978 |
if (force_complex_continue_block) |
| 13979 |
{ |
| 13980 |
assert(block.true_block == block.continue_block); |
| 13981 |
|
| 13982 |
// We're going to emit a continue block directly here, so make sure it's marked as complex. |
| 13983 |
auto &complex_continue = get<SPIRBlock>(block.continue_block).complex_continue; |
| 13984 |
bool old_complex = complex_continue; |
| 13985 |
complex_continue = true; |
| 13986 |
branch(block.self, block.true_block); |
| 13987 |
complex_continue = old_complex; |
| 13988 |
} |
| 13989 |
else |
| 13990 |
branch(block.self, block.true_block); |
| 13991 |
} |
| 13992 |
else if (select_branch_to_false_block) |
| 13993 |
{ |
| 13994 |
if (force_complex_continue_block) |
| 13995 |
{ |
| 13996 |
assert(block.false_block == block.continue_block); |
| 13997 |
|
| 13998 |
// We're going to emit a continue block directly here, so make sure it's marked as complex. |
| 13999 |
auto &complex_continue = get<SPIRBlock>(block.continue_block).complex_continue; |
| 14000 |
bool old_complex = complex_continue; |
| 14001 |
complex_continue = true; |
| 14002 |
branch(block.self, block.false_block); |
| 14003 |
complex_continue = old_complex; |
| 14004 |
} |
| 14005 |
else |
| 14006 |
branch(block.self, block.false_block); |
| 14007 |
} |
| 14008 |
else |
| 14009 |
branch(block.self, block.condition, block.true_block, block.false_block); |
| 14010 |
break; |
| 14011 |
|
| 14012 |
case SPIRBlock::MultiSelect: |
| 14013 |
{ |
| 14014 |
auto &type = expression_type(block.condition); |
| 14015 |
bool unsigned_case = |
| 14016 |
type.basetype == SPIRType::UInt || type.basetype == SPIRType::UShort || type.basetype == SPIRType::UByte; |
| 14017 |
|
| 14018 |
if (block.merge == SPIRBlock::MergeNone) |
| 14019 |
SPIRV_CROSS_THROW("Switch statement is not structured"); |
| 14020 |
|
| 14021 |
if (type.basetype == SPIRType::UInt64 || type.basetype == SPIRType::Int64) |
| 14022 |
{ |
| 14023 |
// SPIR-V spec suggests this is allowed, but we cannot support it in higher level languages. |
| 14024 |
SPIRV_CROSS_THROW("Cannot use 64-bit switch selectors."); |
| 14025 |
} |
| 14026 |
|
| 14027 |
const char *label_suffix = ""; |
| 14028 |
if (type.basetype == SPIRType::UInt && backend.uint32_t_literal_suffix) |
| 14029 |
label_suffix = "u"; |
| 14030 |
else if (type.basetype == SPIRType::UShort) |
| 14031 |
label_suffix = backend.uint16_t_literal_suffix; |
| 14032 |
else if (type.basetype == SPIRType::Short) |
| 14033 |
label_suffix = backend.int16_t_literal_suffix; |
| 14034 |
|
| 14035 |
SPIRBlock *old_emitting_switch = current_emitting_switch; |
| 14036 |
current_emitting_switch = █ |
| 14037 |
|
| 14038 |
if (block.need_ladder_break) |
| 14039 |
statement("bool _", block.self, "_ladder_break = false;"); |
| 14040 |
|
| 14041 |
// Find all unique case constructs. |
| 14042 |
unordered_map<uint32_t, SmallVector<uint32_t>> case_constructs; |
| 14043 |
SmallVector<uint32_t> block_declaration_order; |
| 14044 |
SmallVector<uint32_t> literals_to_merge; |
| 14045 |
|
| 14046 |
// If a switch case branches to the default block for some reason, we can just remove that literal from consideration |
| 14047 |
// and let the default: block handle it. |
| 14048 |
// 2.11 in SPIR-V spec states that for fall-through cases, there is a very strict declaration order which we can take advantage of here. |
| 14049 |
// We only need to consider possible fallthrough if order[i] branches to order[i + 1]. |
| 14050 |
for (auto &c : block.cases) |
| 14051 |
{ |
| 14052 |
if (c.block != block.next_block && c.block != block.default_block) |
| 14053 |
{ |
| 14054 |
if (!case_constructs.count(c.block)) |
| 14055 |
block_declaration_order.push_back(c.block); |
| 14056 |
case_constructs[c.block].push_back(c.value); |
| 14057 |
} |
| 14058 |
else if (c.block == block.next_block && block.default_block != block.next_block) |
| 14059 |
{ |
| 14060 |
// We might have to flush phi inside specific case labels. |
| 14061 |
// If we can piggyback on default:, do so instead. |
| 14062 |
literals_to_merge.push_back(c.value); |
| 14063 |
} |
| 14064 |
} |
| 14065 |
|
| 14066 |
// Empty literal array -> default. |
| 14067 |
if (block.default_block != block.next_block) |
| 14068 |
{ |
| 14069 |
auto &default_block = get<SPIRBlock>(block.default_block); |
| 14070 |
|
| 14071 |
// We need to slide in the default block somewhere in this chain |
| 14072 |
// if there are fall-through scenarios since the default is declared separately in OpSwitch. |
| 14073 |
// Only consider trivial fall-through cases here. |
| 14074 |
size_t num_blocks = block_declaration_order.size(); |
| 14075 |
bool injected_block = false; |
| 14076 |
|
| 14077 |
for (size_t i = 0; i < num_blocks; i++) |
| 14078 |
{ |
| 14079 |
auto &case_block = get<SPIRBlock>(block_declaration_order[i]); |
| 14080 |
if (execution_is_direct_branch(case_block, default_block)) |
| 14081 |
{ |
| 14082 |
// Fallthrough to default block, we must inject the default block here. |
| 14083 |
block_declaration_order.insert(begin(block_declaration_order) + i + 1, block.default_block); |
| 14084 |
injected_block = true; |
| 14085 |
break; |
| 14086 |
} |
| 14087 |
else if (execution_is_direct_branch(default_block, case_block)) |
| 14088 |
{ |
| 14089 |
// Default case is falling through to another case label, we must inject the default block here. |
| 14090 |
block_declaration_order.insert(begin(block_declaration_order) + i, block.default_block); |
| 14091 |
injected_block = true; |
| 14092 |
break; |
| 14093 |
} |
| 14094 |
} |
| 14095 |
|
| 14096 |
// Order does not matter. |
| 14097 |
if (!injected_block) |
| 14098 |
block_declaration_order.push_back(block.default_block); |
| 14099 |
else if (is_legacy_es()) |
| 14100 |
SPIRV_CROSS_THROW("Default case label fallthrough to other case label is not supported in ESSL 1.0."); |
| 14101 |
|
| 14102 |
case_constructs[block.default_block] = {}; |
| 14103 |
} |
| 14104 |
|
| 14105 |
size_t num_blocks = block_declaration_order.size(); |
| 14106 |
|
| 14107 |
const auto to_case_label = [](uint32_t literal, bool is_unsigned_case) -> string { |
| 14108 |
return is_unsigned_case ? convert_to_string(literal) : convert_to_string(int32_t(literal)); |
| 14109 |
}; |
| 14110 |
|
| 14111 |
const auto to_legacy_case_label = [&](uint32_t condition, const SmallVector<uint32_t> &labels, |
| 14112 |
const char *suffix) -> string { |
| 14113 |
string ret; |
| 14114 |
size_t count = labels.size(); |
| 14115 |
for (size_t i = 0; i < count; i++) |
| 14116 |
{ |
| 14117 |
if (i) |
| 14118 |
ret += " || "; |
| 14119 |
ret += join(count > 1 ? "(" : "", to_enclosed_expression(condition), " == ", labels[i], suffix, |
| 14120 |
count > 1 ? ")" : ""); |
| 14121 |
} |
| 14122 |
return ret; |
| 14123 |
}; |
| 14124 |
|
| 14125 |
// We need to deal with a complex scenario for OpPhi. If we have case-fallthrough and Phi in the picture, |
| 14126 |
// we need to flush phi nodes outside the switch block in a branch, |
| 14127 |
// and skip any Phi handling inside the case label to make fall-through work as expected. |
| 14128 |
// This kind of code-gen is super awkward and it's a last resort. Normally we would want to handle this |
| 14129 |
// inside the case label if at all possible. |
| 14130 |
for (size_t i = 1; backend.support_case_fallthrough && i < num_blocks; i++) |
| 14131 |
{ |
| 14132 |
if (flush_phi_required(block.self, block_declaration_order[i]) && |
| 14133 |
flush_phi_required(block_declaration_order[i - 1], block_declaration_order[i])) |
| 14134 |
{ |
| 14135 |
uint32_t target_block = block_declaration_order[i]; |
| 14136 |
|
| 14137 |
// Make sure we flush Phi, it might have been marked to be ignored earlier. |
| 14138 |
get<SPIRBlock>(target_block).ignore_phi_from_block = 0; |
| 14139 |
|
| 14140 |
auto &literals = case_constructs[target_block]; |
| 14141 |
|
| 14142 |
if (literals.empty()) |
| 14143 |
{ |
| 14144 |
// Oh boy, gotta make a complete negative test instead! o.o |
| 14145 |
// Find all possible literals that would *not* make us enter the default block. |
| 14146 |
// If none of those literals match, we flush Phi ... |
| 14147 |
SmallVector<string> conditions; |
| 14148 |
for (size_t j = 0; j < num_blocks; j++) |
| 14149 |
{ |
| 14150 |
auto &negative_literals = case_constructs[block_declaration_order[j]]; |
| 14151 |
for (auto &case_label : negative_literals) |
| 14152 |
conditions.push_back(join(to_enclosed_expression(block.condition), |
| 14153 |
" != ", to_case_label(case_label, unsigned_case))); |
| 14154 |
} |
| 14155 |
|
| 14156 |
statement("if (", merge(conditions, " && "), ")"); |
| 14157 |
begin_scope(); |
| 14158 |
flush_phi(block.self, target_block); |
| 14159 |
end_scope(); |
| 14160 |
} |
| 14161 |
else |
| 14162 |
{ |
| 14163 |
SmallVector<string> conditions; |
| 14164 |
conditions.reserve(literals.size()); |
| 14165 |
for (auto &case_label : literals) |
| 14166 |
conditions.push_back(join(to_enclosed_expression(block.condition), |
| 14167 |
" == ", to_case_label(case_label, unsigned_case))); |
| 14168 |
statement("if (", merge(conditions, " || "), ")"); |
| 14169 |
begin_scope(); |
| 14170 |
flush_phi(block.self, target_block); |
| 14171 |
end_scope(); |
| 14172 |
} |
| 14173 |
|
| 14174 |
// Mark the block so that we don't flush Phi from header to case label. |
| 14175 |
get<SPIRBlock>(target_block).ignore_phi_from_block = block.self; |
| 14176 |
} |
| 14177 |
} |
| 14178 |
|
| 14179 |
// If there is only one default block, and no cases, this is a case where SPIRV-opt decided to emulate |
| 14180 |
// non-structured exits with the help of a switch block. |
| 14181 |
// This is buggy on FXC, so just emit the logical equivalent of a do { } while(false), which is more idiomatic. |
| 14182 |
bool degenerate_switch = block.default_block != block.merge_block && block.cases.empty(); |
| 14183 |
|
| 14184 |
if (degenerate_switch || is_legacy_es()) |
| 14185 |
{ |
| 14186 |
// ESSL 1.0 is not guaranteed to support do/while. |
| 14187 |
if (is_legacy_es()) |
| 14188 |
{ |
| 14189 |
uint32_t counter = statement_count; |
| 14190 |
statement("for (int SPIRV_Cross_Dummy", counter, " = 0; SPIRV_Cross_Dummy", counter, |
| 14191 |
" < 1; SPIRV_Cross_Dummy", counter, "++)"); |
| 14192 |
} |
| 14193 |
else |
| 14194 |
statement("do"); |
| 14195 |
} |
| 14196 |
else |
| 14197 |
{ |
| 14198 |
emit_block_hints(block); |
| 14199 |
statement("switch (", to_expression(block.condition), ")"); |
| 14200 |
} |
| 14201 |
begin_scope(); |
| 14202 |
|
| 14203 |
for (size_t i = 0; i < num_blocks; i++) |
| 14204 |
{ |
| 14205 |
uint32_t target_block = block_declaration_order[i]; |
| 14206 |
auto &literals = case_constructs[target_block]; |
| 14207 |
|
| 14208 |
if (literals.empty()) |
| 14209 |
{ |
| 14210 |
// Default case. |
| 14211 |
if (!degenerate_switch) |
| 14212 |
{ |
| 14213 |
if (is_legacy_es()) |
| 14214 |
statement("else"); |
| 14215 |
else |
| 14216 |
statement("default:"); |
| 14217 |
} |
| 14218 |
} |
| 14219 |
else |
| 14220 |
{ |
| 14221 |
if (is_legacy_es()) |
| 14222 |
{ |
| 14223 |
statement((i ? "else " : ""), "if (", to_legacy_case_label(block.condition, literals, label_suffix), |
| 14224 |
")"); |
| 14225 |
} |
| 14226 |
else |
| 14227 |
{ |
| 14228 |
for (auto &case_literal : literals) |
| 14229 |
{ |
| 14230 |
// The case label value must be sign-extended properly in SPIR-V, so we can assume 32-bit values here. |
| 14231 |
statement("case ", to_case_label(case_literal, unsigned_case), label_suffix, ":"); |
| 14232 |
} |
| 14233 |
} |
| 14234 |
} |
| 14235 |
|
| 14236 |
auto &case_block = get<SPIRBlock>(target_block); |
| 14237 |
if (backend.support_case_fallthrough && i + 1 < num_blocks && |
| 14238 |
execution_is_direct_branch(case_block, get<SPIRBlock>(block_declaration_order[i + 1]))) |
| 14239 |
{ |
| 14240 |
// We will fall through here, so just terminate the block chain early. |
| 14241 |
// We still need to deal with Phi potentially. |
| 14242 |
// No need for a stack-like thing here since we only do fall-through when there is a |
| 14243 |
// single trivial branch to fall-through target.. |
| 14244 |
current_emitting_switch_fallthrough = true; |
| 14245 |
} |
| 14246 |
else |
| 14247 |
current_emitting_switch_fallthrough = false; |
| 14248 |
|
| 14249 |
if (!degenerate_switch) |
| 14250 |
begin_scope(); |
| 14251 |
branch(block.self, target_block); |
| 14252 |
if (!degenerate_switch) |
| 14253 |
end_scope(); |
| 14254 |
|
| 14255 |
current_emitting_switch_fallthrough = false; |
| 14256 |
} |
| 14257 |
|
| 14258 |
// Might still have to flush phi variables if we branch from loop header directly to merge target. |
| 14259 |
if (flush_phi_required(block.self, block.next_block)) |
| 14260 |
{ |
| 14261 |
if (block.default_block == block.next_block || !literals_to_merge.empty()) |
| 14262 |
{ |
| 14263 |
for (auto &case_literal : literals_to_merge) |
| 14264 |
statement("case ", to_case_label(case_literal, unsigned_case), label_suffix, ":"); |
| 14265 |
|
| 14266 |
if (block.default_block == block.next_block) |
| 14267 |
{ |
| 14268 |
if (is_legacy_es()) |
| 14269 |
statement("else"); |
| 14270 |
else |
| 14271 |
statement("default:"); |
| 14272 |
} |
| 14273 |
|
| 14274 |
begin_scope(); |
| 14275 |
flush_phi(block.self, block.next_block); |
| 14276 |
statement("break;"); |
| 14277 |
end_scope(); |
| 14278 |
} |
| 14279 |
} |
| 14280 |
|
| 14281 |
if (degenerate_switch && !is_legacy_es()) |
| 14282 |
end_scope_decl("while(false)"); |
| 14283 |
else |
| 14284 |
end_scope(); |
| 14285 |
|
| 14286 |
if (block.need_ladder_break) |
| 14287 |
{ |
| 14288 |
statement("if (_", block.self, "_ladder_break)"); |
| 14289 |
begin_scope(); |
| 14290 |
statement("break;"); |
| 14291 |
end_scope(); |
| 14292 |
} |
| 14293 |
|
| 14294 |
current_emitting_switch = old_emitting_switch; |
| 14295 |
break; |
| 14296 |
} |
| 14297 |
|
| 14298 |
case SPIRBlock::Return: |
| 14299 |
{ |
| 14300 |
for (auto &line : current_function->fixup_hooks_out) |
| 14301 |
line(); |
| 14302 |
|
| 14303 |
if (processing_entry_point) |
| 14304 |
emit_fixup(); |
| 14305 |
|
| 14306 |
auto &cfg = get_cfg_for_current_function(); |
| 14307 |
|
| 14308 |
if (block.return_value) |
| 14309 |
{ |
| 14310 |
auto &type = expression_type(block.return_value); |
| 14311 |
if (!type.array.empty() && !backend.can_return_array) |
| 14312 |
{ |
| 14313 |
// If we cannot return arrays, we will have a special out argument we can write to instead. |
| 14314 |
// The backend is responsible for setting this up, and redirection the return values as appropriate. |
| 14315 |
if (ir.ids[block.return_value].get_type() != TypeUndef) |
| 14316 |
{ |
| 14317 |
emit_array_copy("SPIRV_Cross_return_value", block.return_value, StorageClassFunction, |
| 14318 |
get_expression_effective_storage_class(block.return_value)); |
| 14319 |
} |
| 14320 |
|
| 14321 |
if (!cfg.node_terminates_control_flow_in_sub_graph(current_function->entry_block, block.self) || |
| 14322 |
block.loop_dominator != BlockID(SPIRBlock::NoDominator)) |
| 14323 |
{ |
| 14324 |
statement("return;"); |
| 14325 |
} |
| 14326 |
} |
| 14327 |
else |
| 14328 |
{ |
| 14329 |
// OpReturnValue can return Undef, so don't emit anything for this case. |
| 14330 |
if (ir.ids[block.return_value].get_type() != TypeUndef) |
| 14331 |
statement("return ", to_expression(block.return_value), ";"); |
| 14332 |
} |
| 14333 |
} |
| 14334 |
else if (!cfg.node_terminates_control_flow_in_sub_graph(current_function->entry_block, block.self) || |
| 14335 |
block.loop_dominator != BlockID(SPIRBlock::NoDominator)) |
| 14336 |
{ |
| 14337 |
// If this block is the very final block and not called from control flow, |
| 14338 |
// we do not need an explicit return which looks out of place. Just end the function here. |
| 14339 |
// In the very weird case of for(;;) { return; } executing return is unconditional, |
| 14340 |
// but we actually need a return here ... |
| 14341 |
statement("return;"); |
| 14342 |
} |
| 14343 |
break; |
| 14344 |
} |
| 14345 |
|
| 14346 |
case SPIRBlock::Kill: |
| 14347 |
statement(backend.discard_literal, ";"); |
| 14348 |
break; |
| 14349 |
|
| 14350 |
case SPIRBlock::Unreachable: |
| 14351 |
emit_next_block = false; |
| 14352 |
break; |
| 14353 |
|
| 14354 |
default: |
| 14355 |
SPIRV_CROSS_THROW("Unimplemented block terminator."); |
| 14356 |
} |
| 14357 |
|
| 14358 |
if (block.next_block && emit_next_block) |
| 14359 |
{ |
| 14360 |
// If we hit this case, we're dealing with an unconditional branch, which means we will output |
| 14361 |
// that block after this. If we had selection merge, we already flushed phi variables. |
| 14362 |
if (block.merge != SPIRBlock::MergeSelection) |
| 14363 |
{ |
| 14364 |
flush_phi(block.self, block.next_block); |
| 14365 |
// For a direct branch, need to remember to invalidate expressions in the next linear block instead. |
| 14366 |
get<SPIRBlock>(block.next_block).invalidate_expressions = block.invalidate_expressions; |
| 14367 |
} |
| 14368 |
|
| 14369 |
// For switch fallthrough cases, we terminate the chain here, but we still need to handle Phi. |
| 14370 |
if (!current_emitting_switch_fallthrough) |
| 14371 |
{ |
| 14372 |
// For merge selects we might have ignored the fact that a merge target |
| 14373 |
// could have been a break; or continue; |
| 14374 |
// We will need to deal with it here. |
| 14375 |
if (is_loop_break(block.next_block)) |
| 14376 |
{ |
| 14377 |
// Cannot check for just break, because switch statements will also use break. |
| 14378 |
assert(block.merge == SPIRBlock::MergeSelection); |
| 14379 |
statement("break;"); |
| 14380 |
} |
| 14381 |
else if (is_continue(block.next_block)) |
| 14382 |
{ |
| 14383 |
assert(block.merge == SPIRBlock::MergeSelection); |
| 14384 |
branch_to_continue(block.self, block.next_block); |
| 14385 |
} |
| 14386 |
else if (BlockID(block.self) != block.next_block) |
| 14387 |
emit_block_chain(get<SPIRBlock>(block.next_block)); |
| 14388 |
} |
| 14389 |
} |
| 14390 |
|
| 14391 |
if (block.merge == SPIRBlock::MergeLoop) |
| 14392 |
{ |
| 14393 |
if (continue_type == SPIRBlock::DoWhileLoop) |
| 14394 |
{ |
| 14395 |
// Make sure that we run the continue block to get the expressions set, but this |
| 14396 |
// should become an empty string. |
| 14397 |
// We have no fallbacks if we cannot forward everything to temporaries ... |
| 14398 |
const auto &continue_block = get<SPIRBlock>(block.continue_block); |
| 14399 |
bool positive_test = execution_is_noop(get<SPIRBlock>(continue_block.true_block), |
| 14400 |
get<SPIRBlock>(continue_block.loop_dominator)); |
| 14401 |
|
| 14402 |
uint32_t current_count = statement_count; |
| 14403 |
auto statements = emit_continue_block(block.continue_block, positive_test, !positive_test); |
| 14404 |
if (statement_count != current_count) |
| 14405 |
{ |
| 14406 |
// The DoWhile block has side effects, force ComplexLoop pattern next pass. |
| 14407 |
get<SPIRBlock>(block.continue_block).complex_continue = true; |
| 14408 |
force_recompile(); |
| 14409 |
} |
| 14410 |
|
| 14411 |
// Might have to invert the do-while test here. |
| 14412 |
auto condition = to_expression(continue_block.condition); |
| 14413 |
if (!positive_test) |
| 14414 |
condition = join("!", enclose_expression(condition)); |
| 14415 |
|
| 14416 |
end_scope_decl(join("while (", condition, ")")); |
| 14417 |
} |
| 14418 |
else |
| 14419 |
end_scope(); |
| 14420 |
|
| 14421 |
loop_level_saver.release(); |
| 14422 |
|
| 14423 |
// We cannot break out of two loops at once, so don't check for break; here. |
| 14424 |
// Using block.self as the "from" block isn't quite right, but it has the same scope |
| 14425 |
// and dominance structure, so it's fine. |
| 14426 |
if (is_continue(block.merge_block)) |
| 14427 |
branch_to_continue(block.self, block.merge_block); |
| 14428 |
else |
| 14429 |
emit_block_chain(get<SPIRBlock>(block.merge_block)); |
| 14430 |
} |
| 14431 |
|
| 14432 |
// Forget about control dependent expressions now. |
| 14433 |
block.invalidate_expressions.clear(); |
| 14434 |
|
| 14435 |
// After we return, we must be out of scope, so if we somehow have to re-emit this function, |
| 14436 |
// re-declare variables if necessary. |
| 14437 |
assert(rearm_dominated_variables.size() == block.dominated_variables.size()); |
| 14438 |
for (size_t i = 0; i < block.dominated_variables.size(); i++) |
| 14439 |
{ |
| 14440 |
uint32_t var = block.dominated_variables[i]; |
| 14441 |
get<SPIRVariable>(var).deferred_declaration = rearm_dominated_variables[i]; |
| 14442 |
} |
| 14443 |
|
| 14444 |
// Just like for deferred declaration, we need to forget about loop variable enable |
| 14445 |
// if our block chain is reinstantiated later. |
| 14446 |
for (auto &var_id : block.loop_variables) |
| 14447 |
get<SPIRVariable>(var_id).loop_variable_enable = false; |
| 14448 |
} |
| 14449 |
|
| 14450 |
void CompilerGLSL::begin_scope() |
| 14451 |
{ |
| 14452 |
statement("{"); |
| 14453 |
indent++; |
| 14454 |
} |
| 14455 |
|
| 14456 |
void CompilerGLSL::end_scope() |
| 14457 |
{ |
| 14458 |
if (!indent) |
| 14459 |
SPIRV_CROSS_THROW("Popping empty indent stack."); |
| 14460 |
indent--; |
| 14461 |
statement("}"); |
| 14462 |
} |
| 14463 |
|
| 14464 |
void CompilerGLSL::end_scope(const string &trailer) |
| 14465 |
{ |
| 14466 |
if (!indent) |
| 14467 |
SPIRV_CROSS_THROW("Popping empty indent stack."); |
| 14468 |
indent--; |
| 14469 |
statement("}", trailer); |
| 14470 |
} |
| 14471 |
|
| 14472 |
void CompilerGLSL::end_scope_decl() |
| 14473 |
{ |
| 14474 |
if (!indent) |
| 14475 |
SPIRV_CROSS_THROW("Popping empty indent stack."); |
| 14476 |
indent--; |
| 14477 |
statement("};"); |
| 14478 |
} |
| 14479 |
|
| 14480 |
void CompilerGLSL::end_scope_decl(const string &decl) |
| 14481 |
{ |
| 14482 |
if (!indent) |
| 14483 |
SPIRV_CROSS_THROW("Popping empty indent stack."); |
| 14484 |
indent--; |
| 14485 |
statement("} ", decl, ";"); |
| 14486 |
} |
| 14487 |
|
| 14488 |
void CompilerGLSL::check_function_call_constraints(const uint32_t *args, uint32_t length) |
| 14489 |
{ |
| 14490 |
// If our variable is remapped, and we rely on type-remapping information as |
| 14491 |
// well, then we cannot pass the variable as a function parameter. |
| 14492 |
// Fixing this is non-trivial without stamping out variants of the same function, |
| 14493 |
// so for now warn about this and suggest workarounds instead. |
| 14494 |
for (uint32_t i = 0; i < length; i++) |
| 14495 |
{ |
| 14496 |
auto *var = maybe_get<SPIRVariable>(args[i]); |
| 14497 |
if (!var || !var->remapped_variable) |
| 14498 |
continue; |
| 14499 |
|
| 14500 |
auto &type = get<SPIRType>(var->basetype); |
| 14501 |
if (type.basetype == SPIRType::Image && type.image.dim == DimSubpassData) |
| 14502 |
{ |
| 14503 |
SPIRV_CROSS_THROW("Tried passing a remapped subpassInput variable to a function. " |
| 14504 |
"This will not work correctly because type-remapping information is lost. " |
| 14505 |
"To workaround, please consider not passing the subpass input as a function parameter, " |
| 14506 |
"or use in/out variables instead which do not need type remapping information."); |
| 14507 |
} |
| 14508 |
} |
| 14509 |
} |
| 14510 |
|
| 14511 |
const Instruction *CompilerGLSL::get_next_instruction_in_block(const Instruction &instr) |
| 14512 |
{ |
| 14513 |
// FIXME: This is kind of hacky. There should be a cleaner way. |
| 14514 |
auto offset = uint32_t(&instr - current_emitting_block->ops.data()); |
| 14515 |
if ((offset + 1) < current_emitting_block->ops.size()) |
| 14516 |
return ¤t_emitting_block->ops[offset + 1]; |
| 14517 |
else |
| 14518 |
return nullptr; |
| 14519 |
} |
| 14520 |
|
| 14521 |
uint32_t CompilerGLSL::mask_relevant_memory_semantics(uint32_t semantics) |
| 14522 |
{ |
| 14523 |
return semantics & (MemorySemanticsAtomicCounterMemoryMask | MemorySemanticsImageMemoryMask | |
| 14524 |
MemorySemanticsWorkgroupMemoryMask | MemorySemanticsUniformMemoryMask | |
| 14525 |
MemorySemanticsCrossWorkgroupMemoryMask | MemorySemanticsSubgroupMemoryMask); |
| 14526 |
} |
| 14527 |
|
| 14528 |
void CompilerGLSL::emit_array_copy(const string &lhs, uint32_t rhs_id, StorageClass, StorageClass) |
| 14529 |
{ |
| 14530 |
statement(lhs, " = ", to_expression(rhs_id), ";"); |
| 14531 |
} |
| 14532 |
|
| 14533 |
void CompilerGLSL::unroll_array_from_complex_load(uint32_t target_id, uint32_t source_id, std::string &expr) |
| 14534 |
{ |
| 14535 |
if (!backend.force_gl_in_out_block) |
| 14536 |
return; |
| 14537 |
// This path is only relevant for GL backends. |
| 14538 |
|
| 14539 |
auto *var = maybe_get<SPIRVariable>(source_id); |
| 14540 |
if (!var) |
| 14541 |
return; |
| 14542 |
|
| 14543 |
if (var->storage != StorageClassInput) |
| 14544 |
return; |
| 14545 |
|
| 14546 |
auto &type = get_variable_data_type(*var); |
| 14547 |
if (type.array.empty()) |
| 14548 |
return; |
| 14549 |
|
| 14550 |
auto builtin = BuiltIn(get_decoration(var->self, DecorationBuiltIn)); |
| 14551 |
bool is_builtin = is_builtin_variable(*var) && (builtin == BuiltInPointSize || builtin == BuiltInPosition); |
| 14552 |
bool is_tess = is_tessellation_shader(); |
| 14553 |
bool is_patch = has_decoration(var->self, DecorationPatch); |
| 14554 |
|
| 14555 |
// Tessellation input arrays are special in that they are unsized, so we cannot directly copy from it. |
| 14556 |
// We must unroll the array load. |
| 14557 |
// For builtins, we couldn't catch this case normally, |
| 14558 |
// because this is resolved in the OpAccessChain in most cases. |
| 14559 |
// If we load the entire array, we have no choice but to unroll here. |
| 14560 |
if (!is_patch && (is_builtin || is_tess)) |
| 14561 |
{ |
| 14562 |
auto new_expr = join("_", target_id, "_unrolled"); |
| 14563 |
statement(variable_decl(type, new_expr, target_id), ";"); |
| 14564 |
string array_expr; |
| 14565 |
if (type.array_size_literal.back()) |
| 14566 |
{ |
| 14567 |
array_expr = convert_to_string(type.array.back()); |
| 14568 |
if (type.array.back() == 0) |
| 14569 |
SPIRV_CROSS_THROW("Cannot unroll an array copy from unsized array."); |
| 14570 |
} |
| 14571 |
else |
| 14572 |
array_expr = to_expression(type.array.back()); |
| 14573 |
|
| 14574 |
// The array size might be a specialization constant, so use a for-loop instead. |
| 14575 |
statement("for (int i = 0; i < int(", array_expr, "); i++)"); |
| 14576 |
begin_scope(); |
| 14577 |
if (is_builtin) |
| 14578 |
statement(new_expr, "[i] = gl_in[i].", expr, ";"); |
| 14579 |
else |
| 14580 |
statement(new_expr, "[i] = ", expr, "[i];"); |
| 14581 |
end_scope(); |
| 14582 |
|
| 14583 |
expr = move(new_expr); |
| 14584 |
} |
| 14585 |
} |
| 14586 |
|
| 14587 |
void CompilerGLSL::cast_from_builtin_load(uint32_t source_id, std::string &expr, const SPIRType &expr_type) |
| 14588 |
{ |
| 14589 |
auto *var = maybe_get_backing_variable(source_id); |
| 14590 |
if (var) |
| 14591 |
source_id = var->self; |
| 14592 |
|
| 14593 |
// Only interested in standalone builtin variables. |
| 14594 |
if (!has_decoration(source_id, DecorationBuiltIn)) |
| 14595 |
return; |
| 14596 |
|
| 14597 |
auto builtin = static_cast<BuiltIn>(get_decoration(source_id, DecorationBuiltIn)); |
| 14598 |
auto expected_type = expr_type.basetype; |
| 14599 |
|
| 14600 |
// TODO: Fill in for more builtins. |
| 14601 |
switch (builtin) |
| 14602 |
{ |
| 14603 |
case BuiltInLayer: |
| 14604 |
case BuiltInPrimitiveId: |
| 14605 |
case BuiltInViewportIndex: |
| 14606 |
case BuiltInInstanceId: |
| 14607 |
case BuiltInInstanceIndex: |
| 14608 |
case BuiltInVertexId: |
| 14609 |
case BuiltInVertexIndex: |
| 14610 |
case BuiltInSampleId: |
| 14611 |
case BuiltInBaseVertex: |
| 14612 |
case BuiltInBaseInstance: |
| 14613 |
case BuiltInDrawIndex: |
| 14614 |
case BuiltInFragStencilRefEXT: |
| 14615 |
case BuiltInInstanceCustomIndexNV: |
| 14616 |
expected_type = SPIRType::Int; |
| 14617 |
break; |
| 14618 |
|
| 14619 |
case BuiltInGlobalInvocationId: |
| 14620 |
case BuiltInLocalInvocationId: |
| 14621 |
case BuiltInWorkgroupId: |
| 14622 |
case BuiltInLocalInvocationIndex: |
| 14623 |
case BuiltInWorkgroupSize: |
| 14624 |
case BuiltInNumWorkgroups: |
| 14625 |
case BuiltInIncomingRayFlagsNV: |
| 14626 |
case BuiltInLaunchIdNV: |
| 14627 |
case BuiltInLaunchSizeNV: |
| 14628 |
expected_type = SPIRType::UInt; |
| 14629 |
break; |
| 14630 |
|
| 14631 |
default: |
| 14632 |
break; |
| 14633 |
} |
| 14634 |
|
| 14635 |
if (expected_type != expr_type.basetype) |
| 14636 |
expr = bitcast_expression(expr_type, expected_type, expr); |
| 14637 |
} |
| 14638 |
|
| 14639 |
void CompilerGLSL::cast_to_builtin_store(uint32_t target_id, std::string &expr, const SPIRType &expr_type) |
| 14640 |
{ |
| 14641 |
// Only interested in standalone builtin variables. |
| 14642 |
if (!has_decoration(target_id, DecorationBuiltIn)) |
| 14643 |
return; |
| 14644 |
|
| 14645 |
auto builtin = static_cast<BuiltIn>(get_decoration(target_id, DecorationBuiltIn)); |
| 14646 |
auto expected_type = expr_type.basetype; |
| 14647 |
|
| 14648 |
// TODO: Fill in for more builtins. |
| 14649 |
switch (builtin) |
| 14650 |
{ |
| 14651 |
case BuiltInLayer: |
| 14652 |
case BuiltInPrimitiveId: |
| 14653 |
case BuiltInViewportIndex: |
| 14654 |
case BuiltInFragStencilRefEXT: |
| 14655 |
expected_type = SPIRType::Int; |
| 14656 |
break; |
| 14657 |
|
| 14658 |
default: |
| 14659 |
break; |
| 14660 |
} |
| 14661 |
|
| 14662 |
if (expected_type != expr_type.basetype) |
| 14663 |
{ |
| 14664 |
auto type = expr_type; |
| 14665 |
type.basetype = expected_type; |
| 14666 |
expr = bitcast_expression(type, expr_type.basetype, expr); |
| 14667 |
} |
| 14668 |
} |
| 14669 |
|
| 14670 |
void CompilerGLSL::convert_non_uniform_expression(const SPIRType &type, std::string &expr) |
| 14671 |
{ |
| 14672 |
if (*backend.nonuniform_qualifier == '\0') |
| 14673 |
return; |
| 14674 |
|
| 14675 |
// Handle SPV_EXT_descriptor_indexing. |
| 14676 |
if (type.basetype == SPIRType::Sampler || type.basetype == SPIRType::SampledImage || |
| 14677 |
type.basetype == SPIRType::Image) |
| 14678 |
{ |
| 14679 |
// The image/sampler ID must be declared as non-uniform. |
| 14680 |
// However, it is not legal GLSL to have |
| 14681 |
// nonuniformEXT(samplers[index]), so we must move the nonuniform qualifier |
| 14682 |
// to the array indexing, like |
| 14683 |
// samplers[nonuniformEXT(index)]. |
| 14684 |
// While the access chain will generally be nonuniformEXT, it's not necessarily so, |
| 14685 |
// so we might have to fixup the OpLoad-ed expression late. |
| 14686 |
|
| 14687 |
auto start_array_index = expr.find_first_of('['); |
| 14688 |
|
| 14689 |
if (start_array_index == string::npos) |
| 14690 |
return; |
| 14691 |
|
| 14692 |
// Check for the edge case that a non-arrayed resource was marked to be nonuniform, |
| 14693 |
// and the bracket we found is actually part of non-resource related data. |
| 14694 |
if (expr.find_first_of(',') < start_array_index) |
| 14695 |
return; |
| 14696 |
|
| 14697 |
// We've opened a bracket, track expressions until we can close the bracket. |
| 14698 |
// This must be our image index. |
| 14699 |
size_t end_array_index = string::npos; |
| 14700 |
unsigned bracket_count = 1; |
| 14701 |
for (size_t index = start_array_index + 1; index < expr.size(); index++) |
| 14702 |
{ |
| 14703 |
if (expr[index] == ']') |
| 14704 |
{ |
| 14705 |
if (--bracket_count == 0) |
| 14706 |
{ |
| 14707 |
end_array_index = index; |
| 14708 |
break; |
| 14709 |
} |
| 14710 |
} |
| 14711 |
else if (expr[index] == '[') |
| 14712 |
bracket_count++; |
| 14713 |
} |
| 14714 |
|
| 14715 |
assert(bracket_count == 0); |
| 14716 |
|
| 14717 |
// Doesn't really make sense to declare a non-arrayed image with nonuniformEXT, but there's |
| 14718 |
// nothing we can do here to express that. |
| 14719 |
if (start_array_index == string::npos || end_array_index == string::npos || end_array_index < start_array_index) |
| 14720 |
return; |
| 14721 |
|
| 14722 |
start_array_index++; |
| 14723 |
|
| 14724 |
expr = join(expr.substr(0, start_array_index), backend.nonuniform_qualifier, "(", |
| 14725 |
expr.substr(start_array_index, end_array_index - start_array_index), ")", |
| 14726 |
expr.substr(end_array_index, string::npos)); |
| 14727 |
} |
| 14728 |
} |
| 14729 |
|
| 14730 |
void CompilerGLSL::emit_block_hints(const SPIRBlock &) |
| 14731 |
{ |
| 14732 |
} |
| 14733 |
|
| 14734 |
void CompilerGLSL::preserve_alias_on_reset(uint32_t id) |
| 14735 |
{ |
| 14736 |
preserved_aliases[id] = get_name(id); |
| 14737 |
} |
| 14738 |
|
| 14739 |
void CompilerGLSL::reset_name_caches() |
| 14740 |
{ |
| 14741 |
for (auto &preserved : preserved_aliases) |
| 14742 |
set_name(preserved.first, preserved.second); |
| 14743 |
|
| 14744 |
preserved_aliases.clear(); |
| 14745 |
resource_names.clear(); |
| 14746 |
block_input_names.clear(); |
| 14747 |
block_output_names.clear(); |
| 14748 |
block_ubo_names.clear(); |
| 14749 |
block_ssbo_names.clear(); |
| 14750 |
block_names.clear(); |
| 14751 |
function_overloads.clear(); |
| 14752 |
} |
| 14753 |
|
| 14754 |
void CompilerGLSL::fixup_type_alias() |
| 14755 |
{ |
| 14756 |
// Due to how some backends work, the "master" type of type_alias must be a block-like type if it exists. |
| 14757 |
ir.for_each_typed_id<SPIRType>([&](uint32_t self, SPIRType &type) { |
| 14758 |
if (!type.type_alias) |
| 14759 |
return; |
| 14760 |
|
| 14761 |
if (has_decoration(type.self, DecorationBlock) || has_decoration(type.self, DecorationBufferBlock)) |
| 14762 |
{ |
| 14763 |
// Top-level block types should never alias anything else. |
| 14764 |
type.type_alias = 0; |
| 14765 |
} |
| 14766 |
else if (type_is_block_like(type) && type.self == ID(self)) |
| 14767 |
{ |
| 14768 |
// A block-like type is any type which contains Offset decoration, but not top-level blocks, |
| 14769 |
// i.e. blocks which are placed inside buffers. |
| 14770 |
// Become the master. |
| 14771 |
ir.for_each_typed_id<SPIRType>([&](uint32_t other_id, SPIRType &other_type) { |
| 14772 |
if (other_id == self) |
| 14773 |
return; |
| 14774 |
|
| 14775 |
if (other_type.type_alias == type.type_alias) |
| 14776 |
other_type.type_alias = self; |
| 14777 |
}); |
| 14778 |
|
| 14779 |
this->get<SPIRType>(type.type_alias).type_alias = self; |
| 14780 |
type.type_alias = 0; |
| 14781 |
} |
| 14782 |
}); |
| 14783 |
} |
| 14784 |
|
| 14785 |
void CompilerGLSL::reorder_type_alias() |
| 14786 |
{ |
| 14787 |
// Reorder declaration of types so that the master of the type alias is always emitted first. |
| 14788 |
// We need this in case a type B depends on type A (A must come before in the vector), but A is an alias of a type Abuffer, which |
| 14789 |
// means declaration of A doesn't happen (yet), and order would be B, ABuffer and not ABuffer, B. Fix this up here. |
| 14790 |
auto loop_lock = ir.create_loop_hard_lock(); |
| 14791 |
|
| 14792 |
auto &type_ids = ir.ids_for_type[TypeType]; |
| 14793 |
for (auto alias_itr = begin(type_ids); alias_itr != end(type_ids); ++alias_itr) |
| 14794 |
{ |
| 14795 |
auto &type = get<SPIRType>(*alias_itr); |
| 14796 |
if (type.type_alias != TypeID(0) && |
| 14797 |
!has_extended_decoration(type.type_alias, SPIRVCrossDecorationBufferBlockRepacked)) |
| 14798 |
{ |
| 14799 |
// We will skip declaring this type, so make sure the type_alias type comes before. |
| 14800 |
auto master_itr = find(begin(type_ids), end(type_ids), ID(type.type_alias)); |
| 14801 |
assert(master_itr != end(type_ids)); |
| 14802 |
|
| 14803 |
if (alias_itr < master_itr) |
| 14804 |
{ |
| 14805 |
// Must also swap the type order for the constant-type joined array. |
| 14806 |
auto &joined_types = ir.ids_for_constant_or_type; |
| 14807 |
auto alt_alias_itr = find(begin(joined_types), end(joined_types), *alias_itr); |
| 14808 |
auto alt_master_itr = find(begin(joined_types), end(joined_types), *master_itr); |
| 14809 |
assert(alt_alias_itr != end(joined_types)); |
| 14810 |
assert(alt_master_itr != end(joined_types)); |
| 14811 |
|
| 14812 |
swap(*alias_itr, *master_itr); |
| 14813 |
swap(*alt_alias_itr, *alt_master_itr); |
| 14814 |
} |
| 14815 |
} |
| 14816 |
} |
| 14817 |
} |
| 14818 |
|
| 14819 |
void CompilerGLSL::emit_line_directive(uint32_t file_id, uint32_t line_literal) |
| 14820 |
{ |
| 14821 |
// If we are redirecting statements, ignore the line directive. |
| 14822 |
// Common case here is continue blocks. |
| 14823 |
if (redirect_statement) |
| 14824 |
return; |
| 14825 |
|
| 14826 |
if (options.emit_line_directives) |
| 14827 |
{ |
| 14828 |
require_extension_internal("GL_GOOGLE_cpp_style_line_directive"); |
| 14829 |
statement_no_indent("#line ", line_literal, " \"", get<SPIRString>(file_id).str, "\""); |
| 14830 |
} |
| 14831 |
} |
| 14832 |
|
| 14833 |
void CompilerGLSL::propagate_nonuniform_qualifier(uint32_t id) |
| 14834 |
{ |
| 14835 |
// SPIR-V might only tag the very last ID with NonUniformEXT, but for codegen, |
| 14836 |
// we need to know NonUniformEXT a little earlier, when the resource is actually loaded. |
| 14837 |
// Back-propagate the qualifier based on the expression dependency chain. |
| 14838 |
|
| 14839 |
if (!has_decoration(id, DecorationNonUniformEXT)) |
| 14840 |
{ |
| 14841 |
set_decoration(id, DecorationNonUniformEXT); |
| 14842 |
force_recompile(); |
| 14843 |
} |
| 14844 |
|
| 14845 |
auto *e = maybe_get<SPIRExpression>(id); |
| 14846 |
auto *combined = maybe_get<SPIRCombinedImageSampler>(id); |
| 14847 |
auto *chain = maybe_get<SPIRAccessChain>(id); |
| 14848 |
if (e) |
| 14849 |
{ |
| 14850 |
for (auto &expr : e->expression_dependencies) |
| 14851 |
propagate_nonuniform_qualifier(expr); |
| 14852 |
for (auto &expr : e->implied_read_expressions) |
| 14853 |
propagate_nonuniform_qualifier(expr); |
| 14854 |
} |
| 14855 |
else if (combined) |
| 14856 |
{ |
| 14857 |
propagate_nonuniform_qualifier(combined->image); |
| 14858 |
propagate_nonuniform_qualifier(combined->sampler); |
| 14859 |
} |
| 14860 |
else if (chain) |
| 14861 |
{ |
| 14862 |
for (auto &expr : chain->implied_read_expressions) |
| 14863 |
propagate_nonuniform_qualifier(expr); |
| 14864 |
} |
| 14865 |
} |
| 14866 |
|
| 14867 |
void CompilerGLSL::emit_copy_logical_type(uint32_t lhs_id, uint32_t lhs_type_id, uint32_t rhs_id, uint32_t rhs_type_id, |
| 14868 |
SmallVector<uint32_t> chain) |
| 14869 |
{ |
| 14870 |
// Fully unroll all member/array indices one by one. |
| 14871 |
|
| 14872 |
auto &lhs_type = get<SPIRType>(lhs_type_id); |
| 14873 |
auto &rhs_type = get<SPIRType>(rhs_type_id); |
| 14874 |
|
| 14875 |
if (!lhs_type.array.empty()) |
| 14876 |
{ |
| 14877 |
// Could use a loop here to support specialization constants, but it gets rather complicated with nested array types, |
| 14878 |
// and this is a rather obscure opcode anyways, keep it simple unless we are forced to. |
| 14879 |
uint32_t array_size = to_array_size_literal(lhs_type); |
| 14880 |
chain.push_back(0); |
| 14881 |
|
| 14882 |
for (uint32_t i = 0; i < array_size; i++) |
| 14883 |
{ |
| 14884 |
chain.back() = i; |
| 14885 |
emit_copy_logical_type(lhs_id, lhs_type.parent_type, rhs_id, rhs_type.parent_type, chain); |
| 14886 |
} |
| 14887 |
} |
| 14888 |
else if (lhs_type.basetype == SPIRType::Struct) |
| 14889 |
{ |
| 14890 |
chain.push_back(0); |
| 14891 |
uint32_t member_count = uint32_t(lhs_type.member_types.size()); |
| 14892 |
for (uint32_t i = 0; i < member_count; i++) |
| 14893 |
{ |
| 14894 |
chain.back() = i; |
| 14895 |
emit_copy_logical_type(lhs_id, lhs_type.member_types[i], rhs_id, rhs_type.member_types[i], chain); |
| 14896 |
} |
| 14897 |
} |
| 14898 |
else |
| 14899 |
{ |
| 14900 |
// Need to handle unpack/packing fixups since this can differ wildly between the logical types, |
| 14901 |
// particularly in MSL. |
| 14902 |
// To deal with this, we emit access chains and go through emit_store_statement |
| 14903 |
// to deal with all the special cases we can encounter. |
| 14904 |
|
| 14905 |
AccessChainMeta lhs_meta, rhs_meta; |
| 14906 |
auto lhs = access_chain_internal(lhs_id, chain.data(), uint32_t(chain.size()), |
| 14907 |
ACCESS_CHAIN_INDEX_IS_LITERAL_BIT, &lhs_meta); |
| 14908 |
auto rhs = access_chain_internal(rhs_id, chain.data(), uint32_t(chain.size()), |
| 14909 |
ACCESS_CHAIN_INDEX_IS_LITERAL_BIT, &rhs_meta); |
| 14910 |
|
| 14911 |
uint32_t id = ir.increase_bound_by(2); |
| 14912 |
lhs_id = id; |
| 14913 |
rhs_id = id + 1; |
| 14914 |
|
| 14915 |
{ |
| 14916 |
auto &lhs_expr = set<SPIRExpression>(lhs_id, move(lhs), lhs_type_id, true); |
| 14917 |
lhs_expr.need_transpose = lhs_meta.need_transpose; |
| 14918 |
|
| 14919 |
if (lhs_meta.storage_is_packed) |
| 14920 |
set_extended_decoration(lhs_id, SPIRVCrossDecorationPhysicalTypePacked); |
| 14921 |
if (lhs_meta.storage_physical_type != 0) |
| 14922 |
set_extended_decoration(lhs_id, SPIRVCrossDecorationPhysicalTypeID, lhs_meta.storage_physical_type); |
| 14923 |
|
| 14924 |
forwarded_temporaries.insert(lhs_id); |
| 14925 |
suppressed_usage_tracking.insert(lhs_id); |
| 14926 |
} |
| 14927 |
|
| 14928 |
{ |
| 14929 |
auto &rhs_expr = set<SPIRExpression>(rhs_id, move(rhs), rhs_type_id, true); |
| 14930 |
rhs_expr.need_transpose = rhs_meta.need_transpose; |
| 14931 |
|
| 14932 |
if (rhs_meta.storage_is_packed) |
| 14933 |
set_extended_decoration(rhs_id, SPIRVCrossDecorationPhysicalTypePacked); |
| 14934 |
if (rhs_meta.storage_physical_type != 0) |
| 14935 |
set_extended_decoration(rhs_id, SPIRVCrossDecorationPhysicalTypeID, rhs_meta.storage_physical_type); |
| 14936 |
|
| 14937 |
forwarded_temporaries.insert(rhs_id); |
| 14938 |
suppressed_usage_tracking.insert(rhs_id); |
| 14939 |
} |
| 14940 |
|
| 14941 |
emit_store_statement(lhs_id, rhs_id); |
| 14942 |
} |
| 14943 |
} |
| 14944 |
|
| 14945 |
bool CompilerGLSL::subpass_input_is_framebuffer_fetch(uint32_t id) const |
| 14946 |
{ |
| 14947 |
if (!has_decoration(id, DecorationInputAttachmentIndex)) |
| 14948 |
return false; |
| 14949 |
|
| 14950 |
uint32_t input_attachment_index = get_decoration(id, DecorationInputAttachmentIndex); |
| 14951 |
for (auto &remap : subpass_to_framebuffer_fetch_attachment) |
| 14952 |
if (remap.first == input_attachment_index) |
| 14953 |
return true; |
| 14954 |
|
| 14955 |
return false; |
| 14956 |
} |
| 14957 |
|
| 14958 |
const SPIRVariable *CompilerGLSL::find_subpass_input_by_attachment_index(uint32_t index) const |
| 14959 |
{ |
| 14960 |
const SPIRVariable *ret = nullptr; |
| 14961 |
ir.for_each_typed_id<SPIRVariable>([&](uint32_t, const SPIRVariable &var) { |
| 14962 |
if (has_decoration(var.self, DecorationInputAttachmentIndex) && |
| 14963 |
get_decoration(var.self, DecorationInputAttachmentIndex) == index) |
| 14964 |
{ |
| 14965 |
ret = &var; |
| 14966 |
} |
| 14967 |
}); |
| 14968 |
return ret; |
| 14969 |
} |
| 14970 |
|
| 14971 |
const SPIRVariable *CompilerGLSL::find_color_output_by_location(uint32_t location) const |
| 14972 |
{ |
| 14973 |
const SPIRVariable *ret = nullptr; |
| 14974 |
ir.for_each_typed_id<SPIRVariable>([&](uint32_t, const SPIRVariable &var) { |
| 14975 |
if (var.storage == StorageClassOutput && get_decoration(var.self, DecorationLocation) == location) |
| 14976 |
ret = &var; |
| 14977 |
}); |
| 14978 |
return ret; |
| 14979 |
} |
| 14980 |
|
| 14981 |
void CompilerGLSL::emit_inout_fragment_outputs_copy_to_subpass_inputs() |
| 14982 |
{ |
| 14983 |
for (auto &remap : subpass_to_framebuffer_fetch_attachment) |
| 14984 |
{ |
| 14985 |
auto *subpass_var = find_subpass_input_by_attachment_index(remap.first); |
| 14986 |
auto *output_var = find_color_output_by_location(remap.second); |
| 14987 |
if (!subpass_var) |
| 14988 |
continue; |
| 14989 |
if (!output_var) |
| 14990 |
SPIRV_CROSS_THROW("Need to declare the corresponding fragment output variable to be able " |
| 14991 |
"to read from it."); |
| 14992 |
if (is_array(get<SPIRType>(output_var->basetype))) |
| 14993 |
SPIRV_CROSS_THROW("Cannot use GL_EXT_shader_framebuffer_fetch with arrays of color outputs."); |
| 14994 |
|
| 14995 |
auto &func = get<SPIRFunction>(get_entry_point().self); |
| 14996 |
func.fixup_hooks_in.push_back([=]() { |
| 14997 |
if (is_legacy()) |
| 14998 |
{ |
| 14999 |
statement(to_expression(subpass_var->self), " = ", "gl_LastFragData[", |
| 15000 |
get_decoration(output_var->self, DecorationLocation), "];"); |
| 15001 |
} |
| 15002 |
else |
| 15003 |
{ |
| 15004 |
uint32_t num_rt_components = this->get<SPIRType>(output_var->basetype).vecsize; |
| 15005 |
statement(to_expression(subpass_var->self), vector_swizzle(num_rt_components, 0), " = ", |
| 15006 |
to_expression(output_var->self), ";"); |
| 15007 |
} |
| 15008 |
}); |
| 15009 |
} |
| 15010 |
} |
| 15011 |
|
| 15012 |
bool CompilerGLSL::variable_is_depth_or_compare(VariableID id) const |
| 15013 |
{ |
| 15014 |
return image_is_comparison(get<SPIRType>(get<SPIRVariable>(id).basetype), id); |
| 15015 |
} |
| 15016 |
|
| 15017 |
const char *CompilerGLSL::ShaderSubgroupSupportHelper::get_extension_name(Candidate c) |
| 15018 |
{ |
| 15019 |
static const char *const retval[CandidateCount] = { "GL_KHR_shader_subgroup_ballot", |
| 15020 |
"GL_KHR_shader_subgroup_basic", |
| 15021 |
"GL_KHR_shader_subgroup_vote", |
| 15022 |
"GL_NV_gpu_shader_5", |
| 15023 |
"GL_NV_shader_thread_group", |
| 15024 |
"GL_NV_shader_thread_shuffle", |
| 15025 |
"GL_ARB_shader_ballot", |
| 15026 |
"GL_ARB_shader_group_vote", |
| 15027 |
"GL_AMD_gcn_shader" }; |
| 15028 |
return retval[c]; |
| 15029 |
} |
| 15030 |
|
| 15031 |
SmallVector<std::string> CompilerGLSL::ShaderSubgroupSupportHelper::get_extra_required_extension_names(Candidate c) |
| 15032 |
{ |
| 15033 |
switch (c) |
| 15034 |
{ |
| 15035 |
case ARB_shader_ballot: |
| 15036 |
return { "GL_ARB_shader_int64" }; |
| 15037 |
case AMD_gcn_shader: |
| 15038 |
return { "GL_AMD_gpu_shader_int64", "GL_NV_gpu_shader5" }; |
| 15039 |
default: |
| 15040 |
return {}; |
| 15041 |
} |
| 15042 |
} |
| 15043 |
|
| 15044 |
const char *CompilerGLSL::ShaderSubgroupSupportHelper::get_extra_required_extension_predicate(Candidate c) |
| 15045 |
{ |
| 15046 |
switch (c) |
| 15047 |
{ |
| 15048 |
case ARB_shader_ballot: |
| 15049 |
return "defined(GL_ARB_shader_int64)"; |
| 15050 |
case AMD_gcn_shader: |
| 15051 |
return "(defined(GL_AMD_gpu_shader_int64) || defined(GL_NV_gpu_shader5))"; |
| 15052 |
default: |
| 15053 |
return ""; |
| 15054 |
} |
| 15055 |
} |
| 15056 |
|
| 15057 |
CompilerGLSL::ShaderSubgroupSupportHelper::FeatureVector CompilerGLSL::ShaderSubgroupSupportHelper:: |
| 15058 |
get_feature_dependencies(Feature feature) |
| 15059 |
{ |
| 15060 |
switch (feature) |
| 15061 |
{ |
| 15062 |
case SubgroupAllEqualT: |
| 15063 |
return { SubgroupBrodcast_First, SubgroupAll_Any_AllEqualBool }; |
| 15064 |
case SubgroupElect: |
| 15065 |
return { SubgroupBallotFindLSB_MSB, SubgroupBallot, SubgroupInvocationID }; |
| 15066 |
case SubgroupInverseBallot_InclBitCount_ExclBitCout: |
| 15067 |
return { SubgroupMask }; |
| 15068 |
case SubgroupBallotBitCount: |
| 15069 |
return { SubgroupBallot }; |
| 15070 |
default: |
| 15071 |
return {}; |
| 15072 |
} |
| 15073 |
} |
| 15074 |
|
| 15075 |
CompilerGLSL::ShaderSubgroupSupportHelper::FeatureMask CompilerGLSL::ShaderSubgroupSupportHelper:: |
| 15076 |
get_feature_dependency_mask(Feature feature) |
| 15077 |
{ |
| 15078 |
return build_mask(get_feature_dependencies(feature)); |
| 15079 |
} |
| 15080 |
|
| 15081 |
bool CompilerGLSL::ShaderSubgroupSupportHelper::can_feature_be_implemented_without_extensions(Feature feature) |
| 15082 |
{ |
| 15083 |
static const bool retval[FeatureCount] = { false, false, false, false, false, false, |
| 15084 |
true, // SubgroupBalloFindLSB_MSB |
| 15085 |
false, false, false, false, |
| 15086 |
true, // SubgroupMemBarrier - replaced with workgroup memory barriers |
| 15087 |
false, false, true, false }; |
| 15088 |
|
| 15089 |
return retval[feature]; |
| 15090 |
} |
| 15091 |
|
| 15092 |
CompilerGLSL::ShaderSubgroupSupportHelper::Candidate CompilerGLSL::ShaderSubgroupSupportHelper:: |
| 15093 |
get_KHR_extension_for_feature(Feature feature) |
| 15094 |
{ |
| 15095 |
static const Candidate extensions[FeatureCount] = { |
| 15096 |
KHR_shader_subgroup_ballot, KHR_shader_subgroup_basic, KHR_shader_subgroup_basic, KHR_shader_subgroup_basic, |
| 15097 |
KHR_shader_subgroup_basic, KHR_shader_subgroup_ballot, KHR_shader_subgroup_ballot, KHR_shader_subgroup_vote, |
| 15098 |
KHR_shader_subgroup_vote, KHR_shader_subgroup_basic, KHR_shader_subgroup_ballot, KHR_shader_subgroup_basic, |
| 15099 |
KHR_shader_subgroup_basic, KHR_shader_subgroup_ballot, KHR_shader_subgroup_ballot, KHR_shader_subgroup_ballot |
| 15100 |
}; |
| 15101 |
|
| 15102 |
return extensions[feature]; |
| 15103 |
} |
| 15104 |
|
| 15105 |
void CompilerGLSL::ShaderSubgroupSupportHelper::request_feature(Feature feature) |
| 15106 |
{ |
| 15107 |
feature_mask |= (FeatureMask(1) << feature) | get_feature_dependency_mask(feature); |
| 15108 |
} |
| 15109 |
|
| 15110 |
bool CompilerGLSL::ShaderSubgroupSupportHelper::is_feature_requested(Feature feature) const |
| 15111 |
{ |
| 15112 |
return (feature_mask & (1u << feature)) != 0; |
| 15113 |
} |
| 15114 |
|
| 15115 |
CompilerGLSL::ShaderSubgroupSupportHelper::Result CompilerGLSL::ShaderSubgroupSupportHelper::resolve() const |
| 15116 |
{ |
| 15117 |
Result res; |
| 15118 |
|
| 15119 |
for (uint32_t i = 0u; i < FeatureCount; ++i) |
| 15120 |
{ |
| 15121 |
if (feature_mask & (1u << i)) |
| 15122 |
{ |
| 15123 |
auto feature = static_cast<Feature>(i); |
| 15124 |
std::unordered_set<uint32_t> unique_candidates; |
| 15125 |
|
| 15126 |
auto candidates = get_candidates_for_feature(feature); |
| 15127 |
unique_candidates.insert(candidates.begin(), candidates.end()); |
| 15128 |
|
| 15129 |
auto deps = get_feature_dependencies(feature); |
| 15130 |
for (Feature d : deps) |
| 15131 |
{ |
| 15132 |
candidates = get_candidates_for_feature(d); |
| 15133 |
if (!candidates.empty()) |
| 15134 |
unique_candidates.insert(candidates.begin(), candidates.end()); |
| 15135 |
} |
| 15136 |
|
| 15137 |
for (uint32_t c : unique_candidates) |
| 15138 |
++res.weights[static_cast<Candidate>(c)]; |
| 15139 |
} |
| 15140 |
} |
| 15141 |
|
| 15142 |
return res; |
| 15143 |
} |
| 15144 |
|
| 15145 |
CompilerGLSL::ShaderSubgroupSupportHelper::CandidateVector CompilerGLSL::ShaderSubgroupSupportHelper:: |
| 15146 |
get_candidates_for_feature(Feature ft, const Result &r) |
| 15147 |
{ |
| 15148 |
auto c = get_candidates_for_feature(ft); |
| 15149 |
auto cmp = [&r](Candidate a, Candidate b) { |
| 15150 |
if (r.weights[a] == r.weights[b]) |
| 15151 |
return a < b; // Prefer candidates with lower enum value |
| 15152 |
return r.weights[a] > r.weights[b]; |
| 15153 |
}; |
| 15154 |
std::sort(c.begin(), c.end(), cmp); |
| 15155 |
return c; |
| 15156 |
} |
| 15157 |
|
| 15158 |
CompilerGLSL::ShaderSubgroupSupportHelper::CandidateVector CompilerGLSL::ShaderSubgroupSupportHelper:: |
| 15159 |
get_candidates_for_feature(Feature feature) |
| 15160 |
{ |
| 15161 |
switch (feature) |
| 15162 |
{ |
| 15163 |
case SubgroupMask: |
| 15164 |
return { KHR_shader_subgroup_ballot, NV_shader_thread_group, ARB_shader_ballot }; |
| 15165 |
case SubgroupSize: |
| 15166 |
return { KHR_shader_subgroup_basic, NV_shader_thread_group, AMD_gcn_shader, ARB_shader_ballot }; |
| 15167 |
case SubgroupInvocationID: |
| 15168 |
return { KHR_shader_subgroup_basic, NV_shader_thread_group, ARB_shader_ballot }; |
| 15169 |
case SubgroupID: |
| 15170 |
return { KHR_shader_subgroup_basic, NV_shader_thread_group }; |
| 15171 |
case NumSubgroups: |
| 15172 |
return { KHR_shader_subgroup_basic, NV_shader_thread_group }; |
| 15173 |
case SubgroupBrodcast_First: |
| 15174 |
return { KHR_shader_subgroup_ballot, NV_shader_thread_shuffle, ARB_shader_ballot }; |
| 15175 |
case SubgroupBallotFindLSB_MSB: |
| 15176 |
return { KHR_shader_subgroup_ballot, NV_shader_thread_group }; |
| 15177 |
case SubgroupAll_Any_AllEqualBool: |
| 15178 |
return { KHR_shader_subgroup_vote, NV_gpu_shader_5, ARB_shader_group_vote, AMD_gcn_shader }; |
| 15179 |
case SubgroupAllEqualT: |
| 15180 |
return {}; // depends on other features only |
| 15181 |
case SubgroupElect: |
| 15182 |
return {}; // depends on other features only |
| 15183 |
case SubgroupBallot: |
| 15184 |
return { KHR_shader_subgroup_ballot, NV_shader_thread_group, ARB_shader_ballot }; |
| 15185 |
case SubgroupBarrier: |
| 15186 |
return { KHR_shader_subgroup_basic, NV_shader_thread_group, ARB_shader_ballot, AMD_gcn_shader }; |
| 15187 |
case SubgroupMemBarrier: |
| 15188 |
return { KHR_shader_subgroup_basic }; |
| 15189 |
case SubgroupInverseBallot_InclBitCount_ExclBitCout: |
| 15190 |
return {}; |
| 15191 |
case SubgroupBallotBitExtract: |
| 15192 |
return { NV_shader_thread_group }; |
| 15193 |
case SubgroupBallotBitCount: |
| 15194 |
return {}; |
| 15195 |
default: |
| 15196 |
return {}; |
| 15197 |
} |
| 15198 |
} |
| 15199 |
|
| 15200 |
CompilerGLSL::ShaderSubgroupSupportHelper::FeatureMask CompilerGLSL::ShaderSubgroupSupportHelper::build_mask( |
| 15201 |
const SmallVector<Feature> &features) |
| 15202 |
{ |
| 15203 |
FeatureMask mask = 0; |
| 15204 |
for (Feature f : features) |
| 15205 |
mask |= FeatureMask(1) << f; |
| 15206 |
return mask; |
| 15207 |
} |
| 15208 |
|
| 15209 |
CompilerGLSL::ShaderSubgroupSupportHelper::Result::Result() |
| 15210 |
{ |
| 15211 |
for (auto &weight : weights) |
| 15212 |
weight = 0; |
| 15213 |
|
| 15214 |
// Make sure KHR_shader_subgroup extensions are always prefered. |
| 15215 |
const uint32_t big_num = FeatureCount; |
| 15216 |
weights[KHR_shader_subgroup_ballot] = big_num; |
| 15217 |
weights[KHR_shader_subgroup_basic] = big_num; |
| 15218 |
weights[KHR_shader_subgroup_vote] = big_num; |
| 15219 |
} |
| 15220 |
|
| 15221 |
void CompilerGLSL::request_workaround_wrapper_overload(TypeID id) |
| 15222 |
{ |
| 15223 |
// Must be ordered to maintain deterministic output, so vector is appropriate. |
| 15224 |
if (find(begin(workaround_ubo_load_overload_types), end(workaround_ubo_load_overload_types), id) == |
| 15225 |
end(workaround_ubo_load_overload_types)) |
| 15226 |
{ |
| 15227 |
force_recompile(); |
| 15228 |
workaround_ubo_load_overload_types.push_back(id); |
| 15229 |
} |
| 15230 |
} |
| 15231 |
|
| 15232 |
void CompilerGLSL::rewrite_load_for_wrapped_row_major(std::string &expr, TypeID loaded_type, ID ptr) |
| 15233 |
{ |
| 15234 |
// Loading row-major matrices from UBOs on older AMD Windows OpenGL drivers is problematic. |
| 15235 |
// To load these types correctly, we must first wrap them in a dummy function which only purpose is to |
| 15236 |
// ensure row_major decoration is actually respected. |
| 15237 |
auto *var = maybe_get_backing_variable(ptr); |
| 15238 |
if (!var) |
| 15239 |
return; |
| 15240 |
|
| 15241 |
auto &backing_type = get<SPIRType>(var->basetype); |
| 15242 |
bool is_ubo = backing_type.basetype == SPIRType::Struct && backing_type.storage == StorageClassUniform && |
| 15243 |
has_decoration(backing_type.self, DecorationBlock); |
| 15244 |
if (!is_ubo) |
| 15245 |
return; |
| 15246 |
|
| 15247 |
auto *type = &get<SPIRType>(loaded_type); |
| 15248 |
bool rewrite = false; |
| 15249 |
|
| 15250 |
if (is_matrix(*type)) |
| 15251 |
{ |
| 15252 |
// To avoid adding a lot of unnecessary meta tracking to forward the row_major state, |
| 15253 |
// we will simply look at the base struct itself. It is exceptionally rare to mix and match row-major/col-major state. |
| 15254 |
// If there is any row-major action going on, we apply the workaround. |
| 15255 |
// It is harmless to apply the workaround to column-major matrices, so this is still a valid solution. |
| 15256 |
// If an access chain occurred, the workaround is not required, so loading vectors or scalars don't need workaround. |
| 15257 |
type = &backing_type; |
| 15258 |
} |
| 15259 |
|
| 15260 |
if (type->basetype == SPIRType::Struct) |
| 15261 |
{ |
| 15262 |
// If we're loading a struct where any member is a row-major matrix, apply the workaround. |
| 15263 |
for (uint32_t i = 0; i < uint32_t(type->member_types.size()); i++) |
| 15264 |
{ |
| 15265 |
if (combined_decoration_for_member(*type, i).get(DecorationRowMajor)) |
| 15266 |
{ |
| 15267 |
rewrite = true; |
| 15268 |
break; |
| 15269 |
} |
| 15270 |
} |
| 15271 |
} |
| 15272 |
|
| 15273 |
if (rewrite) |
| 15274 |
{ |
| 15275 |
request_workaround_wrapper_overload(loaded_type); |
| 15276 |
expr = join("SPIRV_Cross_workaround_load_row_major(", expr, ")"); |
| 15277 |
} |
| 15278 |
} |