| 1 | // Copyright (C) 2021 The Qt Company Ltd. |
| 2 | // SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only |
| 3 | |
| 4 | #include "qspirvshader_p.h" |
| 5 | #include "qspirvshaderremap_p.h" |
| 6 | #include <private/qshaderdescription_p.h> |
| 7 | #include <private/qshader_p.h> |
| 8 | |
| 9 | #include <spirv_cross_c.h> |
| 10 | |
| 11 | QT_BEGIN_NAMESPACE |
| 12 | |
| 13 | struct QSpirvShaderPrivate |
| 14 | { |
| 15 | ~QSpirvShaderPrivate(); |
| 16 | |
| 17 | void createCompiler(spvc_backend backend); |
| 18 | void reflect(); |
| 19 | |
| 20 | QShaderDescription::InOutVariable inOutVar(const spvc_reflected_resource &r); |
| 21 | QShaderDescription::BlockVariable blockVar(spvc_type_id typeId, uint32_t memberIdx); |
| 22 | |
| 23 | QShader::Stage stage; |
| 24 | QByteArray ir; |
| 25 | QShaderDescription shaderDescription; |
| 26 | |
| 27 | spvc_context ctx = nullptr; |
| 28 | spvc_compiler glslGen = nullptr; |
| 29 | spvc_compiler hlslGen = nullptr; |
| 30 | spvc_compiler mslGen = nullptr; |
| 31 | |
| 32 | QString spirvCrossErrorMsg; |
| 33 | }; |
| 34 | |
| 35 | QSpirvShaderPrivate::~QSpirvShaderPrivate() |
| 36 | { |
| 37 | spvc_context_destroy(context: ctx); |
| 38 | } |
| 39 | |
| 40 | void QSpirvShaderPrivate::createCompiler(spvc_backend backend) |
| 41 | { |
| 42 | if (!ctx) { |
| 43 | if (spvc_context_create(context: &ctx) != SPVC_SUCCESS) { |
| 44 | qWarning(msg: "Failed to create SPIRV-Cross context" ); |
| 45 | return; |
| 46 | } |
| 47 | } |
| 48 | |
| 49 | const SpvId *spirv = reinterpret_cast<const SpvId *>(ir.constData()); |
| 50 | size_t wordCount = size_t(ir.size()) / sizeof(SpvId); |
| 51 | spvc_parsed_ir parsedIr; |
| 52 | if (spvc_context_parse_spirv(context: ctx, spirv, word_count: wordCount, parsed_ir: &parsedIr) != SPVC_SUCCESS) { |
| 53 | qWarning(msg: "Failed to parse SPIR-V: %s" , spvc_context_get_last_error_string(context: ctx)); |
| 54 | return; |
| 55 | } |
| 56 | |
| 57 | spvc_compiler *outCompiler = nullptr; |
| 58 | switch (backend) { |
| 59 | case SPVC_BACKEND_GLSL: |
| 60 | outCompiler = &glslGen; |
| 61 | break; |
| 62 | case SPVC_BACKEND_HLSL: |
| 63 | outCompiler = &hlslGen; |
| 64 | break; |
| 65 | case SPVC_BACKEND_MSL: |
| 66 | outCompiler = &mslGen; |
| 67 | break; |
| 68 | default: |
| 69 | return; |
| 70 | } |
| 71 | |
| 72 | if (spvc_context_create_compiler(context: ctx, backend, parsed_ir: parsedIr, |
| 73 | mode: SPVC_CAPTURE_MODE_TAKE_OWNERSHIP, compiler: outCompiler) != SPVC_SUCCESS) |
| 74 | { |
| 75 | qWarning(msg: "Failed to create SPIRV-Cross compiler: %s" , spvc_context_get_last_error_string(context: ctx)); |
| 76 | return; |
| 77 | } |
| 78 | } |
| 79 | |
| 80 | static QShaderDescription::VariableType matVarType(const spvc_type &t, QShaderDescription::VariableType compType) |
| 81 | { |
| 82 | const unsigned vecsize = spvc_type_get_vector_size(type: t); |
| 83 | switch (spvc_type_get_columns(type: t)) { |
| 84 | case 2: |
| 85 | return QShaderDescription::VariableType(compType + 4 + (vecsize == 3 ? 1 : vecsize == 4 ? 2 : 0)); |
| 86 | case 3: |
| 87 | return QShaderDescription::VariableType(compType + 7 + (vecsize == 2 ? 1 : vecsize == 4 ? 2 : 0)); |
| 88 | case 4: |
| 89 | return QShaderDescription::VariableType(compType + 10 + (vecsize == 2 ? 1 : vecsize == 3 ? 2 : 0)); |
| 90 | default: |
| 91 | return QShaderDescription::Unknown; |
| 92 | } |
| 93 | } |
| 94 | |
| 95 | static QShaderDescription::VariableType vecVarType(const spvc_type &t, QShaderDescription::VariableType compType) |
| 96 | { |
| 97 | switch (spvc_type_get_vector_size(type: t)) { |
| 98 | case 1: |
| 99 | return compType; |
| 100 | case 2: |
| 101 | return QShaderDescription::VariableType(compType + 1); |
| 102 | case 3: |
| 103 | return QShaderDescription::VariableType(compType + 2); |
| 104 | case 4: |
| 105 | return QShaderDescription::VariableType(compType + 3); |
| 106 | default: |
| 107 | return QShaderDescription::Unknown; |
| 108 | } |
| 109 | } |
| 110 | |
| 111 | static QShaderDescription::VariableType sampledImageVarType(const spvc_type &t) |
| 112 | { |
| 113 | switch (spvc_type_get_image_dimension(type: t)) { |
| 114 | case SpvDim1D: |
| 115 | return spvc_type_get_image_arrayed(type: t) ? QShaderDescription::Sampler1DArray |
| 116 | : QShaderDescription::Sampler1D; |
| 117 | case SpvDim2D: |
| 118 | return spvc_type_get_image_arrayed(type: t) |
| 119 | ? (spvc_type_get_image_multisampled(type: t) ? QShaderDescription::Sampler2DMSArray |
| 120 | : QShaderDescription::Sampler2DArray) |
| 121 | : (spvc_type_get_image_multisampled(type: t) ? QShaderDescription::Sampler2DMS |
| 122 | : QShaderDescription::Sampler2D); |
| 123 | case SpvDim3D: |
| 124 | return spvc_type_get_image_arrayed(type: t) ? QShaderDescription::Sampler3DArray |
| 125 | : QShaderDescription::Sampler3D; |
| 126 | case SpvDimCube: |
| 127 | return spvc_type_get_image_arrayed(type: t) ? QShaderDescription::SamplerCubeArray |
| 128 | : QShaderDescription::SamplerCube; |
| 129 | case SpvDimRect: |
| 130 | return QShaderDescription::SamplerRect; |
| 131 | case SpvDimBuffer: |
| 132 | return QShaderDescription::SamplerBuffer; |
| 133 | default: |
| 134 | return QShaderDescription::Unknown; |
| 135 | } |
| 136 | } |
| 137 | |
| 138 | static QShaderDescription::VariableType imageVarType(const spvc_type &t) |
| 139 | { |
| 140 | switch (spvc_type_get_image_dimension(type: t)) { |
| 141 | case SpvDim1D: |
| 142 | return spvc_type_get_image_arrayed(type: t) ? QShaderDescription::Image1DArray |
| 143 | : QShaderDescription::Image1D; |
| 144 | case SpvDim2D: |
| 145 | return spvc_type_get_image_arrayed(type: t) |
| 146 | ? (spvc_type_get_image_multisampled(type: t) ? QShaderDescription::Image2DMSArray |
| 147 | : QShaderDescription::Image2DArray) |
| 148 | : (spvc_type_get_image_multisampled(type: t) ? QShaderDescription::Image2DMS |
| 149 | : QShaderDescription::Image2D); |
| 150 | case SpvDim3D: |
| 151 | return spvc_type_get_image_arrayed(type: t) ? QShaderDescription::Image3DArray |
| 152 | : QShaderDescription::Image3D; |
| 153 | case SpvDimCube: |
| 154 | return spvc_type_get_image_arrayed(type: t) ? QShaderDescription::ImageCubeArray |
| 155 | : QShaderDescription::ImageCube; |
| 156 | case SpvDimRect: |
| 157 | return QShaderDescription::ImageRect; |
| 158 | case SpvDimBuffer: |
| 159 | return QShaderDescription::ImageBuffer; |
| 160 | default: |
| 161 | return QShaderDescription::Unknown; |
| 162 | } |
| 163 | } |
| 164 | |
| 165 | static QShaderDescription::VariableType varType(const spvc_type &t) |
| 166 | { |
| 167 | QShaderDescription::VariableType vt = QShaderDescription::Unknown; |
| 168 | const spvc_basetype basetype = spvc_type_get_basetype(type: t); |
| 169 | switch (basetype) { |
| 170 | case SPVC_BASETYPE_FP32: |
| 171 | vt = spvc_type_get_columns(type: t) > 1 ? matVarType(t, compType: QShaderDescription::Float) |
| 172 | : vecVarType(t, compType: QShaderDescription::Float); |
| 173 | break; |
| 174 | case SPVC_BASETYPE_FP64: |
| 175 | vt = spvc_type_get_columns(type: t) > 1 ? matVarType(t, compType: QShaderDescription::Double) |
| 176 | : vecVarType(t, compType: QShaderDescription::Double); |
| 177 | break; |
| 178 | case SPVC_BASETYPE_UINT32: |
| 179 | vt = vecVarType(t, compType: QShaderDescription::Uint); |
| 180 | break; |
| 181 | case SPVC_BASETYPE_INT32: |
| 182 | vt = vecVarType(t, compType: QShaderDescription::Int); |
| 183 | break; |
| 184 | case SPVC_BASETYPE_BOOLEAN: |
| 185 | vt = vecVarType(t, compType: QShaderDescription::Uint); |
| 186 | break; |
| 187 | case SPVC_BASETYPE_SAMPLED_IMAGE: |
| 188 | vt = sampledImageVarType(t); |
| 189 | break; |
| 190 | case SPVC_BASETYPE_IMAGE: |
| 191 | vt = imageVarType(t); |
| 192 | break; |
| 193 | case SPVC_BASETYPE_SAMPLER: |
| 194 | vt = QShaderDescription::Sampler; |
| 195 | break; |
| 196 | case SPVC_BASETYPE_STRUCT: |
| 197 | vt = QShaderDescription::Struct; |
| 198 | break; |
| 199 | default: |
| 200 | // can encounter types we do not (yet) handle, return Unknown for those |
| 201 | qWarning(msg: "Unsupported base type %d" , basetype); |
| 202 | break; |
| 203 | } |
| 204 | return vt; |
| 205 | } |
| 206 | |
| 207 | QShaderDescription::InOutVariable QSpirvShaderPrivate::inOutVar(const spvc_reflected_resource &r) |
| 208 | { |
| 209 | QShaderDescription::InOutVariable v; |
| 210 | v.name = r.name; |
| 211 | |
| 212 | spvc_type baseTypeHandle = spvc_compiler_get_type_handle(compiler: glslGen, id: r.base_type_id); |
| 213 | v.type = varType(t: baseTypeHandle); |
| 214 | |
| 215 | spvc_type typeHandle = spvc_compiler_get_type_handle(compiler: glslGen, id: r.type_id); |
| 216 | for (unsigned i = 0, dimCount = spvc_type_get_num_array_dimensions(type: typeHandle); i < dimCount; ++i) |
| 217 | v.arrayDims.append(t: int(spvc_type_get_array_dimension(type: typeHandle, dimension: i))); |
| 218 | |
| 219 | if (spvc_compiler_has_decoration(compiler: glslGen, id: r.id, decoration: SpvDecorationLocation)) |
| 220 | v.location = spvc_compiler_get_decoration(compiler: glslGen, id: r.id, decoration: SpvDecorationLocation); |
| 221 | |
| 222 | if (spvc_compiler_has_decoration(compiler: glslGen, id: r.id, decoration: SpvDecorationBinding)) |
| 223 | v.binding = spvc_compiler_get_decoration(compiler: glslGen, id: r.id, decoration: SpvDecorationBinding); |
| 224 | |
| 225 | if (spvc_compiler_has_decoration(compiler: glslGen, id: r.id, decoration: SpvDecorationDescriptorSet)) |
| 226 | v.descriptorSet = spvc_compiler_get_decoration(compiler: glslGen, id: r.id, decoration: SpvDecorationDescriptorSet); |
| 227 | |
| 228 | if (spvc_compiler_has_decoration(compiler: glslGen, id: r.id, decoration: SpvDecorationPatch)) |
| 229 | v.perPatch = spvc_compiler_get_decoration(compiler: glslGen, id: r.id, decoration: SpvDecorationPatch); |
| 230 | |
| 231 | if (spvc_type_get_basetype(type: baseTypeHandle) == SPVC_BASETYPE_IMAGE) { |
| 232 | v.imageFormat = QShaderDescription::ImageFormat(spvc_type_get_image_storage_format(type: baseTypeHandle)); |
| 233 | |
| 234 | v.imageFlags.setFlag(flag: QShaderDescription::ImageFlag::WriteOnlyImage, |
| 235 | on: spvc_compiler_has_decoration(compiler: glslGen, id: r.id, decoration: SpvDecorationNonReadable)); |
| 236 | |
| 237 | v.imageFlags.setFlag(flag: QShaderDescription::ImageFlag::ReadOnlyImage, |
| 238 | on: spvc_compiler_has_decoration(compiler: glslGen, id: r.id, decoration: SpvDecorationNonWritable)); |
| 239 | } |
| 240 | |
| 241 | if (v.type == QShaderDescription::Struct) { |
| 242 | const unsigned count = spvc_type_get_num_member_types(type: baseTypeHandle); |
| 243 | const spvc_type_id id = spvc_type_get_base_type_id(type: baseTypeHandle); |
| 244 | |
| 245 | for (unsigned idx = 0; idx < count; ++idx) { |
| 246 | v.structMembers.append(t: blockVar(typeId: id, memberIdx: idx)); |
| 247 | v.perPatch |= bool(spvc_compiler_has_member_decoration(compiler: glslGen, id, member_index: idx, decoration: SpvDecorationPatch)); |
| 248 | } |
| 249 | } |
| 250 | |
| 251 | return v; |
| 252 | } |
| 253 | |
| 254 | QShaderDescription::BlockVariable QSpirvShaderPrivate::blockVar(spvc_type_id typeId, uint32_t memberIdx) |
| 255 | { |
| 256 | QShaderDescription::BlockVariable v; |
| 257 | v.name = spvc_compiler_get_member_name(compiler: glslGen, id: typeId, member_index: memberIdx); |
| 258 | |
| 259 | spvc_type t = spvc_compiler_get_type_handle(compiler: glslGen, id: typeId); |
| 260 | spvc_type_id memberTypeId = spvc_type_get_member_type(type: t, index: memberIdx); |
| 261 | spvc_type memberType = spvc_compiler_get_type_handle(compiler: glslGen, id: memberTypeId); |
| 262 | v.type = varType(t: memberType); |
| 263 | v.offset = -1; |
| 264 | |
| 265 | unsigned offset = 0; |
| 266 | if (spvc_compiler_type_struct_member_offset(compiler: glslGen, type: t, index: memberIdx, offset: &offset) == SPVC_SUCCESS) |
| 267 | v.offset = int(offset); |
| 268 | |
| 269 | size_t size = 0; |
| 270 | if (spvc_compiler_get_declared_struct_member_size(compiler: glslGen, type: t, index: memberIdx, size: &size) == SPVC_SUCCESS) |
| 271 | v.size = int(size); |
| 272 | |
| 273 | for (unsigned i = 0, dimCount = spvc_type_get_num_array_dimensions(type: memberType); i < dimCount; ++i) |
| 274 | v.arrayDims.append(t: int(spvc_type_get_array_dimension(type: memberType, dimension: i))); |
| 275 | |
| 276 | if (spvc_compiler_has_member_decoration(compiler: glslGen, id: typeId, member_index: memberIdx, decoration: SpvDecorationArrayStride)) { |
| 277 | unsigned stride = 0; |
| 278 | if (spvc_compiler_type_struct_member_array_stride(compiler: glslGen, type: t, index: memberIdx, stride: &stride) == SPVC_SUCCESS) |
| 279 | v.arrayStride = int(stride); |
| 280 | } |
| 281 | |
| 282 | if (spvc_compiler_has_member_decoration(compiler: glslGen, id: typeId, member_index: memberIdx, decoration: SpvDecorationMatrixStride)) { |
| 283 | unsigned stride = 0; |
| 284 | if (spvc_compiler_type_struct_member_matrix_stride(compiler: glslGen, type: t, index: memberIdx, stride: &stride) == SPVC_SUCCESS) |
| 285 | v.matrixStride = int(stride); |
| 286 | } |
| 287 | |
| 288 | if (spvc_compiler_has_member_decoration(compiler: glslGen, id: typeId, member_index: memberIdx, decoration: SpvDecorationRowMajor)) |
| 289 | v.matrixIsRowMajor = true; |
| 290 | |
| 291 | if (v.type == QShaderDescription::Struct) { |
| 292 | unsigned count = spvc_type_get_num_member_types(type: memberType); |
| 293 | for (unsigned idx = 0; idx < count; ++idx) |
| 294 | v.structMembers.append(t: blockVar(typeId: spvc_type_get_base_type_id(type: memberType), memberIdx: idx)); |
| 295 | } |
| 296 | |
| 297 | return v; |
| 298 | } |
| 299 | |
| 300 | void QSpirvShaderPrivate::reflect() |
| 301 | { |
| 302 | if (!glslGen) |
| 303 | return; |
| 304 | |
| 305 | shaderDescription = QShaderDescription(); |
| 306 | QShaderDescriptionPrivate *dd = QShaderDescriptionPrivate::get(desc: &shaderDescription); |
| 307 | |
| 308 | dd->localSize[0] = spvc_compiler_get_execution_mode_argument_by_index(compiler: glslGen, mode: SpvExecutionModeLocalSize, index: 0); |
| 309 | dd->localSize[1] = spvc_compiler_get_execution_mode_argument_by_index(compiler: glslGen, mode: SpvExecutionModeLocalSize, index: 1); |
| 310 | dd->localSize[2] = spvc_compiler_get_execution_mode_argument_by_index(compiler: glslGen, mode: SpvExecutionModeLocalSize, index: 2); |
| 311 | |
| 312 | dd->tessOutVertCount = spvc_compiler_get_execution_mode_argument(compiler: glslGen, mode: SpvExecutionModeOutputVertices); |
| 313 | dd->tessMode = QShaderDescription::UnknownTessellationMode; |
| 314 | dd->tessWind = QShaderDescription::UnknownTessellationWindingOrder; |
| 315 | dd->tessPart = QShaderDescription::UnknownTessellationPartitioning; |
| 316 | |
| 317 | const SpvExecutionMode *execModes = nullptr; |
| 318 | size_t execModeCount = 0; |
| 319 | if (spvc_compiler_get_execution_modes(compiler: glslGen, modes: &execModes, num_modes: &execModeCount) != SPVC_SUCCESS) { |
| 320 | qWarning(msg: "Failed to get shader execution modes: %s" , spvc_context_get_last_error_string(context: ctx)); |
| 321 | return; |
| 322 | } |
| 323 | for (size_t i = 0; i < execModeCount; ++i) { |
| 324 | switch (execModes[i]) { |
| 325 | case SpvExecutionModeTriangles: |
| 326 | dd->tessMode = QShaderDescription::TrianglesTessellationMode; |
| 327 | break; |
| 328 | case SpvExecutionModeQuads: |
| 329 | dd->tessMode = QShaderDescription::QuadTessellationMode; |
| 330 | break; |
| 331 | case SpvExecutionModeIsolines: |
| 332 | qWarning(msg: "Isoline execution mode used for tessellation," |
| 333 | " this is not portable and may not work as expected." ); |
| 334 | dd->tessMode = QShaderDescription::IsolineTessellationMode; |
| 335 | break; |
| 336 | case SpvExecutionModeSpacingEqual: |
| 337 | dd->tessPart = QShaderDescription::EqualTessellationPartitioning; |
| 338 | break; |
| 339 | case SpvExecutionModeSpacingFractionalEven: |
| 340 | dd->tessPart = QShaderDescription::FractionalEvenTessellationPartitioning; |
| 341 | break; |
| 342 | case SpvExecutionModeSpacingFractionalOdd: |
| 343 | dd->tessPart = QShaderDescription::FractionalOddTessellationPartitioning; |
| 344 | break; |
| 345 | case SpvExecutionModeVertexOrderCw: |
| 346 | dd->tessWind = QShaderDescription::CwTessellationWindingOrder; |
| 347 | break; |
| 348 | case SpvExecutionModeVertexOrderCcw : |
| 349 | dd->tessWind = QShaderDescription::CcwTessellationWindingOrder; |
| 350 | break; |
| 351 | default: |
| 352 | break; |
| 353 | } |
| 354 | } |
| 355 | |
| 356 | // For builtin inputs/outputs (think of things like gl_Position (or |
| 357 | // gl_out[].gl_Position), gl_InvocationID, gl_TessLevelOuter, etc.) we only |
| 358 | // want the ones that are active (really read or written). |
| 359 | spvc_compiler_update_active_builtins(compiler: glslGen); |
| 360 | |
| 361 | spvc_resources resources; |
| 362 | if (spvc_compiler_create_shader_resources(compiler: glslGen, resources: &resources) != SPVC_SUCCESS) { |
| 363 | qWarning(msg: "Failed to get shader resources: %s" , spvc_context_get_last_error_string(context: ctx)); |
| 364 | return; |
| 365 | } |
| 366 | |
| 367 | const spvc_reflected_resource *resourceList = nullptr; |
| 368 | const spvc_reflected_builtin_resource *builtinResourceList = nullptr; |
| 369 | size_t resourceListCount = 0; |
| 370 | |
| 371 | if (spvc_resources_get_resource_list_for_type(resources, type: SPVC_RESOURCE_TYPE_STAGE_INPUT, |
| 372 | resource_list: &resourceList, resource_size: &resourceListCount) == SPVC_SUCCESS) |
| 373 | { |
| 374 | for (size_t i = 0; i < resourceListCount; ++i) { |
| 375 | const QShaderDescription::InOutVariable v = inOutVar(r: resourceList[i]); |
| 376 | if (v.type != QShaderDescription::Unknown) |
| 377 | dd->inVars.append(t: v); |
| 378 | } |
| 379 | } |
| 380 | |
| 381 | if (spvc_resources_get_resource_list_for_type(resources, type: SPVC_RESOURCE_TYPE_STAGE_OUTPUT, |
| 382 | resource_list: &resourceList, resource_size: &resourceListCount) == SPVC_SUCCESS) |
| 383 | { |
| 384 | for (size_t i = 0; i < resourceListCount; ++i) { |
| 385 | const QShaderDescription::InOutVariable v = inOutVar(r: resourceList[i]); |
| 386 | if (v.type != QShaderDescription::Unknown) |
| 387 | dd->outVars.append(t: v); |
| 388 | } |
| 389 | } |
| 390 | |
| 391 | if (spvc_resources_get_builtin_resource_list_for_type(resources, type: SPVC_BUILTIN_RESOURCE_TYPE_STAGE_INPUT, |
| 392 | resource_list: &builtinResourceList, resource_size: &resourceListCount) == SPVC_SUCCESS) |
| 393 | { |
| 394 | for (size_t i = 0; i < resourceListCount; ++i) { |
| 395 | if (spvc_compiler_has_active_builtin(compiler: glslGen, builtin: builtinResourceList[i].builtin, storage: SpvStorageClassInput)) { |
| 396 | QShaderDescription::BuiltinVariable v; |
| 397 | v.type = QShaderDescription::BuiltinType(builtinResourceList[i].builtin); |
| 398 | |
| 399 | spvc_type type = spvc_compiler_get_type_handle( |
| 400 | compiler: glslGen, id: builtinResourceList[i].value_type_id); |
| 401 | v.varType = varType(t: type); |
| 402 | |
| 403 | for (unsigned i = 0, dimCount = spvc_type_get_num_array_dimensions(type); |
| 404 | i < dimCount; ++i) |
| 405 | v.arrayDims.append(t: int(spvc_type_get_array_dimension(type, dimension: i))); |
| 406 | |
| 407 | dd->inBuiltins.append(t: v); |
| 408 | } |
| 409 | } |
| 410 | std::sort(first: dd->inBuiltins.begin(), last: dd->inBuiltins.end(), |
| 411 | comp: [](const QShaderDescription::BuiltinVariable &a, const QShaderDescription::BuiltinVariable &b) |
| 412 | { |
| 413 | return a.type < b.type; |
| 414 | }); |
| 415 | } |
| 416 | |
| 417 | if (spvc_resources_get_builtin_resource_list_for_type(resources, type: SPVC_BUILTIN_RESOURCE_TYPE_STAGE_OUTPUT, |
| 418 | resource_list: &builtinResourceList, resource_size: &resourceListCount) == SPVC_SUCCESS) |
| 419 | { |
| 420 | for (size_t i = 0; i < resourceListCount; ++i) { |
| 421 | if (spvc_compiler_has_active_builtin(compiler: glslGen, builtin: builtinResourceList[i].builtin, storage: SpvStorageClassOutput)) { |
| 422 | QShaderDescription::BuiltinVariable v; |
| 423 | v.type = QShaderDescription::BuiltinType(builtinResourceList[i].builtin); |
| 424 | |
| 425 | spvc_type type = spvc_compiler_get_type_handle( |
| 426 | compiler: glslGen, id: builtinResourceList[i].value_type_id); |
| 427 | v.varType = varType(t: type); |
| 428 | |
| 429 | for (unsigned i = 0, dimCount = spvc_type_get_num_array_dimensions(type); |
| 430 | i < dimCount; ++i) |
| 431 | v.arrayDims.append(t: int(spvc_type_get_array_dimension(type, dimension: i))); |
| 432 | |
| 433 | dd->outBuiltins.append(t: v); |
| 434 | } |
| 435 | } |
| 436 | std::sort(first: dd->outBuiltins.begin(), last: dd->outBuiltins.end(), |
| 437 | comp: [](const QShaderDescription::BuiltinVariable &a, const QShaderDescription::BuiltinVariable &b) |
| 438 | { |
| 439 | return a.type < b.type; |
| 440 | }); |
| 441 | } |
| 442 | |
| 443 | // uniform blocks map to either a uniform buffer or a plain struct |
| 444 | if (spvc_resources_get_resource_list_for_type(resources, type: SPVC_RESOURCE_TYPE_UNIFORM_BUFFER, |
| 445 | resource_list: &resourceList, resource_size: &resourceListCount) == SPVC_SUCCESS) |
| 446 | { |
| 447 | for (size_t i = 0; i < resourceListCount; ++i) { |
| 448 | const spvc_reflected_resource &r(resourceList[i]); |
| 449 | spvc_type t = spvc_compiler_get_type_handle(compiler: glslGen, id: r.base_type_id); |
| 450 | QShaderDescription::UniformBlock block; |
| 451 | block.blockName = r.name; |
| 452 | |
| 453 | // the simple case: |
| 454 | // layout(...) uniform blk { T v; } inst; |
| 455 | // gives us blockName "blk" and structName "inst" because |
| 456 | // in GLSL without uniform blocks this ends up being |
| 457 | // struct blk { T v; }; uniform blk inst; |
| 458 | // or, where real UBs are used, for instance Metal: |
| 459 | // struct blk { T v; }; constant blk& inst [[buffer(N)]] |
| 460 | block.structName = spvc_compiler_get_name(compiler: glslGen, id: r.id); |
| 461 | |
| 462 | // the annoying case: |
| 463 | // layout(...) uniform blk { T v; }; |
| 464 | // here structName is empty and the generated code (when no uniform |
| 465 | // blocks) uses _ID as a fallback name: |
| 466 | // struct blk { T v; }; uniform blk _ID; |
| 467 | // or, with real uniform buffers, f.ex. Metal: |
| 468 | // struct blk { T v; }; constant blk& _ID [[buffer(N)]] |
| 469 | // Let's make sure the fallback name is filled in correctly. |
| 470 | if (block.structName.isEmpty()) { |
| 471 | // The catch (matters only when uniform blocks are not used in |
| 472 | // the output) is that ID is per-shader and so may differ |
| 473 | // between shaders, meaning that having the same uniform block |
| 474 | // in a vertex and fragment shader will lead to each stage |
| 475 | // having its own set of uniforms corresponding to the ub |
| 476 | // members (ofc, inactive uniforms may get optimized out by the |
| 477 | // compiler, so the duplication then is only there for members |
| 478 | // used in both stages). Not much we can do about that here, |
| 479 | // though. The GL backend of QRhi can deal with this. |
| 480 | block.structName = QByteArrayLiteral("_" ) + QByteArray::number(r.id); |
| 481 | } |
| 482 | |
| 483 | size_t size = 0; |
| 484 | spvc_compiler_get_declared_struct_size(compiler: glslGen, struct_type: t, size: &size); |
| 485 | block.size = int(size); |
| 486 | if (spvc_compiler_has_decoration(compiler: glslGen, id: r.id, decoration: SpvDecorationBinding)) |
| 487 | block.binding = int(spvc_compiler_get_decoration(compiler: glslGen, id: r.id, decoration: SpvDecorationBinding)); |
| 488 | if (spvc_compiler_has_decoration(compiler: glslGen, id: r.id, decoration: SpvDecorationDescriptorSet)) |
| 489 | block.descriptorSet = int(spvc_compiler_get_decoration(compiler: glslGen, id: r.id, decoration: SpvDecorationDescriptorSet)); |
| 490 | |
| 491 | unsigned count = spvc_type_get_num_member_types(type: t); |
| 492 | for (unsigned idx = 0; idx < count; ++idx) { |
| 493 | const QShaderDescription::BlockVariable v = blockVar(typeId: r.base_type_id, memberIdx: idx); |
| 494 | if (v.type != QShaderDescription::Unknown) |
| 495 | block.members.append(t: v); |
| 496 | } |
| 497 | |
| 498 | dd->uniformBlocks.append(t: block); |
| 499 | } |
| 500 | } |
| 501 | |
| 502 | // push constant blocks map to a plain GLSL struct regardless of version |
| 503 | if (spvc_resources_get_resource_list_for_type(resources, type: SPVC_RESOURCE_TYPE_PUSH_CONSTANT, |
| 504 | resource_list: &resourceList, resource_size: &resourceListCount) == SPVC_SUCCESS) |
| 505 | { |
| 506 | for (size_t i = 0; i < resourceListCount; ++i) { |
| 507 | const spvc_reflected_resource &r(resourceList[i]); |
| 508 | spvc_type t = spvc_compiler_get_type_handle(compiler: glslGen, id: r.base_type_id); |
| 509 | QShaderDescription::PushConstantBlock block; |
| 510 | block.name = spvc_compiler_get_name(compiler: glslGen, id: r.id); |
| 511 | size_t size = 0; |
| 512 | spvc_compiler_get_declared_struct_size(compiler: glslGen, struct_type: t, size: &size); |
| 513 | block.size = int(size); |
| 514 | unsigned count = spvc_type_get_num_member_types(type: t); |
| 515 | for (unsigned idx = 0; idx < count; ++idx) { |
| 516 | const QShaderDescription::BlockVariable v = blockVar(typeId: r.base_type_id, memberIdx: idx); |
| 517 | if (v.type != QShaderDescription::Unknown) |
| 518 | block.members.append(t: v); |
| 519 | } |
| 520 | dd->pushConstantBlocks.append(t: block); |
| 521 | } |
| 522 | } |
| 523 | |
| 524 | if (spvc_resources_get_resource_list_for_type(resources, type: SPVC_RESOURCE_TYPE_STORAGE_BUFFER, |
| 525 | resource_list: &resourceList, resource_size: &resourceListCount) == SPVC_SUCCESS) |
| 526 | { |
| 527 | for (size_t i = 0; i < resourceListCount; ++i) { |
| 528 | const spvc_reflected_resource &r(resourceList[i]); |
| 529 | spvc_type t = spvc_compiler_get_type_handle(compiler: glslGen, id: r.base_type_id); |
| 530 | QShaderDescription::StorageBlock block; |
| 531 | block.blockName = r.name; |
| 532 | block.instanceName = spvc_compiler_get_name(compiler: glslGen, id: r.id); |
| 533 | size_t size = 0; |
| 534 | spvc_compiler_get_declared_struct_size(compiler: glslGen, struct_type: t, size: &size); |
| 535 | block.knownSize = int(size); |
| 536 | if (spvc_compiler_has_decoration(compiler: glslGen, id: r.id, decoration: SpvDecorationBinding)) |
| 537 | block.binding = int(spvc_compiler_get_decoration(compiler: glslGen, id: r.id, decoration: SpvDecorationBinding)); |
| 538 | if (spvc_compiler_has_decoration(compiler: glslGen, id: r.id, decoration: SpvDecorationDescriptorSet)) |
| 539 | block.descriptorSet = int(spvc_compiler_get_decoration(compiler: glslGen, id: r.id, decoration: SpvDecorationDescriptorSet)); |
| 540 | size_t offset0 = 0; |
| 541 | spvc_compiler_get_declared_struct_size_runtime_array(compiler: glslGen, struct_type: t, array_size: 0, size: &offset0); |
| 542 | size_t offset1 = 0; |
| 543 | spvc_compiler_get_declared_struct_size_runtime_array(compiler: glslGen, struct_type: t, array_size: 1, size: &offset1); |
| 544 | block.runtimeArrayStride = int(offset1 - offset0); |
| 545 | const SpvDecoration *decorations; |
| 546 | spvc_compiler_get_buffer_block_decorations(compiler: glslGen, id: r.id, decorations: &decorations, num_decorations: &size); |
| 547 | for (uint i = 0; i < size; ++i) { |
| 548 | switch (decorations[i]) { |
| 549 | case SpvDecorationCoherent: |
| 550 | block.qualifierFlags.setFlag(flag: QShaderDescription::QualifierCoherent); |
| 551 | break; |
| 552 | case SpvDecorationVolatile: |
| 553 | block.qualifierFlags.setFlag(flag: QShaderDescription::QualifierVolatile); |
| 554 | break; |
| 555 | case SpvDecorationRestrict: |
| 556 | block.qualifierFlags.setFlag(flag: QShaderDescription::QualifierRestrict); |
| 557 | break; |
| 558 | case SpvDecorationNonWritable: |
| 559 | block.qualifierFlags.setFlag(flag: QShaderDescription::QualifierReadOnly); |
| 560 | break; |
| 561 | case SpvDecorationNonReadable: |
| 562 | block.qualifierFlags.setFlag(flag: QShaderDescription::QualifierWriteOnly); |
| 563 | break; |
| 564 | default: |
| 565 | break; |
| 566 | } |
| 567 | } |
| 568 | unsigned count = spvc_type_get_num_member_types(type: t); |
| 569 | for (unsigned idx = 0; idx < count; ++idx) { |
| 570 | const QShaderDescription::BlockVariable v = blockVar(typeId: r.base_type_id, memberIdx: idx); |
| 571 | if (v.type != QShaderDescription::Unknown) |
| 572 | block.members.append(t: v); |
| 573 | } |
| 574 | dd->storageBlocks.append(t: block); |
| 575 | } |
| 576 | } |
| 577 | |
| 578 | if (spvc_resources_get_resource_list_for_type(resources, type: SPVC_RESOURCE_TYPE_SAMPLED_IMAGE, |
| 579 | resource_list: &resourceList, resource_size: &resourceListCount) == SPVC_SUCCESS) |
| 580 | { |
| 581 | for (size_t i = 0; i < resourceListCount; ++i) { |
| 582 | const spvc_reflected_resource &r(resourceList[i]); |
| 583 | const QShaderDescription::InOutVariable v = inOutVar(r); |
| 584 | if (v.type != QShaderDescription::Unknown) |
| 585 | dd->combinedImageSamplers.append(t: v); |
| 586 | } |
| 587 | } |
| 588 | |
| 589 | if (spvc_resources_get_resource_list_for_type(resources, type: SPVC_RESOURCE_TYPE_SEPARATE_IMAGE, |
| 590 | resource_list: &resourceList, resource_size: &resourceListCount) == SPVC_SUCCESS) |
| 591 | { |
| 592 | for (size_t i = 0; i < resourceListCount; ++i) { |
| 593 | const spvc_reflected_resource &r(resourceList[i]); |
| 594 | const QShaderDescription::InOutVariable v = inOutVar(r); |
| 595 | if (v.type != QShaderDescription::Unknown) |
| 596 | dd->separateImages.append(t: v); |
| 597 | } |
| 598 | } |
| 599 | |
| 600 | if (spvc_resources_get_resource_list_for_type(resources, type: SPVC_RESOURCE_TYPE_SEPARATE_SAMPLERS, |
| 601 | resource_list: &resourceList, resource_size: &resourceListCount) == SPVC_SUCCESS) |
| 602 | { |
| 603 | for (size_t i = 0; i < resourceListCount; ++i) { |
| 604 | const spvc_reflected_resource &r(resourceList[i]); |
| 605 | const QShaderDescription::InOutVariable v = inOutVar(r); |
| 606 | if (v.type != QShaderDescription::Unknown) |
| 607 | dd->separateSamplers.append(t: v); |
| 608 | } |
| 609 | } |
| 610 | |
| 611 | if (spvc_resources_get_resource_list_for_type(resources, type: SPVC_RESOURCE_TYPE_STORAGE_IMAGE, |
| 612 | resource_list: &resourceList, resource_size: &resourceListCount) == SPVC_SUCCESS) |
| 613 | { |
| 614 | for (size_t i = 0; i < resourceListCount; ++i) { |
| 615 | const spvc_reflected_resource &r(resourceList[i]); |
| 616 | const QShaderDescription::InOutVariable v = inOutVar(r); |
| 617 | if (v.type != QShaderDescription::Unknown) |
| 618 | dd->storageImages.append(t: v); |
| 619 | } |
| 620 | } |
| 621 | } |
| 622 | |
| 623 | QSpirvShader::QSpirvShader() |
| 624 | : d(new QSpirvShaderPrivate) |
| 625 | { |
| 626 | } |
| 627 | |
| 628 | QSpirvShader::~QSpirvShader() |
| 629 | { |
| 630 | delete d; |
| 631 | } |
| 632 | |
| 633 | void QSpirvShader::setSpirvBinary(const QByteArray &spirv, QShader::Stage stage) |
| 634 | { |
| 635 | d->stage = stage; |
| 636 | d->ir = spirv; |
| 637 | d->createCompiler(backend: SPVC_BACKEND_GLSL); |
| 638 | d->reflect(); |
| 639 | } |
| 640 | |
| 641 | QShaderDescription QSpirvShader::shaderDescription() const |
| 642 | { |
| 643 | return d->shaderDescription; |
| 644 | } |
| 645 | |
| 646 | QByteArray QSpirvShader::spirvBinary() const |
| 647 | { |
| 648 | return d->ir; |
| 649 | } |
| 650 | |
| 651 | QByteArray QSpirvShader::remappedSpirvBinary(RemapFlags flags, QString *errorMessage) const |
| 652 | { |
| 653 | QSpirvShaderRemapper remapper; |
| 654 | QByteArray result = remapper.remap(ir: d->ir, flags); |
| 655 | if (errorMessage) |
| 656 | *errorMessage = remapper.errorMessage(); |
| 657 | return result; |
| 658 | } |
| 659 | |
| 660 | QByteArray QSpirvShader::translateToGLSL(int version, |
| 661 | GlslFlags flags, |
| 662 | QShader::Stage stage, |
| 663 | const MultiViewInfo &multiViewInfo, |
| 664 | QVector<SeparateToCombinedImageSamplerMapping> *separateToCombinedImageSamplerMappings) const |
| 665 | { |
| 666 | d->spirvCrossErrorMsg.clear(); |
| 667 | |
| 668 | d->createCompiler(backend: SPVC_BACKEND_GLSL); |
| 669 | if (!d->glslGen) |
| 670 | return QByteArray(); |
| 671 | |
| 672 | spvc_compiler_options options = nullptr; |
| 673 | if (spvc_compiler_create_compiler_options(compiler: d->glslGen, options: &options) != SPVC_SUCCESS) |
| 674 | return QByteArray(); |
| 675 | |
| 676 | spvc_compiler_options_set_uint(options, option: SPVC_COMPILER_OPTION_GLSL_VERSION, |
| 677 | value: version); |
| 678 | spvc_compiler_options_set_bool(options, option: SPVC_COMPILER_OPTION_GLSL_ES, |
| 679 | value: flags.testFlag(flag: GlslFlag::GlslEs)); |
| 680 | spvc_compiler_options_set_bool(options, option: SPVC_COMPILER_OPTION_FIXUP_DEPTH_CONVENTION, |
| 681 | value: flags.testFlag(flag: GlslFlag::FixClipSpace)); |
| 682 | spvc_compiler_options_set_bool(options, option: SPVC_COMPILER_OPTION_GLSL_ES_DEFAULT_FLOAT_PRECISION_HIGHP, |
| 683 | value: !flags.testFlag(flag: GlslFlag::FragDefaultMediump)); |
| 684 | // The gl backend of QRhi is not prepared for UBOs atm. Have a uniform (heh) |
| 685 | // behavior regardless of the GLSL version. |
| 686 | spvc_compiler_options_set_bool(options, option: SPVC_COMPILER_OPTION_GLSL_EMIT_UNIFORM_BUFFER_AS_PLAIN_UNIFORMS, |
| 687 | value: true); |
| 688 | // Do not emit binding qualifiers for samplers (and for uniform blocks, but |
| 689 | // those we just disabled above). |
| 690 | spvc_compiler_options_set_bool(options, option: SPVC_COMPILER_OPTION_GLSL_ENABLE_420PACK_EXTENSION, |
| 691 | value: false); |
| 692 | |
| 693 | if (stage == QShader::VertexStage && multiViewInfo.viewCount > 1) { |
| 694 | spvc_compiler_options_set_uint(options, option: SPVC_COMPILER_OPTION_GLSL_OVR_MULTIVIEW_VIEW_COUNT, |
| 695 | value: uint(multiViewInfo.viewCount)); |
| 696 | } |
| 697 | |
| 698 | spvc_compiler_install_compiler_options(compiler: d->glslGen, options); |
| 699 | |
| 700 | // Let's say the shader has these separate imagers and samplers: |
| 701 | // layout(binding = 2) uniform texture2D sepTex; |
| 702 | // layout(binding = 3) uniform sampler sepSampler; |
| 703 | // layout(binding = 4) uniform sampler sepSampler2; |
| 704 | // where sepTex is used with both samplers in the shader. |
| 705 | // For OpenGL this is mapped to "combined image samplers", i.e. the |
| 706 | // traditional sampler2Ds with arbitrary names: |
| 707 | // uniform sampler2D _39; |
| 708 | // uniform sampler2D _41; |
| 709 | // This mapping (2+3 -> "_39", 2+4 -> "_41") needs to be exposed to the caller. |
| 710 | if (spvc_compiler_build_combined_image_samplers(compiler: d->glslGen) != SPVC_SUCCESS) { |
| 711 | d->spirvCrossErrorMsg = QString::fromUtf8(utf8: spvc_context_get_last_error_string(context: d->ctx)); |
| 712 | return QByteArray(); |
| 713 | } |
| 714 | if (separateToCombinedImageSamplerMappings) { |
| 715 | const spvc_combined_image_sampler *combinedImageSamplerMappings = nullptr; |
| 716 | size_t numCombinedImageSamplerMappings = 0; |
| 717 | if (spvc_compiler_get_combined_image_samplers(compiler: d->glslGen, |
| 718 | samplers: &combinedImageSamplerMappings, |
| 719 | num_samplers: &numCombinedImageSamplerMappings) == SPVC_SUCCESS) |
| 720 | { |
| 721 | for (size_t i = 0; i < numCombinedImageSamplerMappings; ++i) { |
| 722 | const spvc_combined_image_sampler *mapping = &combinedImageSamplerMappings[i]; |
| 723 | QByteArray combinedName = spvc_compiler_get_name(compiler: d->glslGen, id: mapping->combined_id); |
| 724 | if (combinedName.isEmpty()) |
| 725 | combinedName = QByteArrayLiteral("_" ) + QByteArray::number(mapping->combined_id); |
| 726 | QByteArray textureName = spvc_compiler_get_name(compiler: d->glslGen, id: mapping->image_id); |
| 727 | QByteArray samplerName = spvc_compiler_get_name(compiler: d->glslGen, id: mapping->sampler_id); |
| 728 | separateToCombinedImageSamplerMappings->append(t: { .textureName: textureName, .samplerName: samplerName, .combinedSamplerName: combinedName }); |
| 729 | } |
| 730 | } |
| 731 | } |
| 732 | |
| 733 | const char *result = nullptr; |
| 734 | if (spvc_compiler_compile(compiler: d->glslGen, source: &result) != SPVC_SUCCESS) { |
| 735 | d->spirvCrossErrorMsg = QString::fromUtf8(utf8: spvc_context_get_last_error_string(context: d->ctx)); |
| 736 | return QByteArray(); |
| 737 | } |
| 738 | |
| 739 | // We used to fix up the result to complement GL_ARB_shading_language_420pack |
| 740 | // with GL_ARB_separate_shader_objects to make Mesa happy, but the 420pack |
| 741 | // is never relied on now so no need to do anything here. |
| 742 | return result; |
| 743 | } |
| 744 | |
| 745 | QByteArray QSpirvShader::translateToHLSL(int version, QShader::NativeResourceBindingMap *nativeBindings) const |
| 746 | { |
| 747 | d->spirvCrossErrorMsg.clear(); |
| 748 | |
| 749 | d->createCompiler(backend: SPVC_BACKEND_HLSL); |
| 750 | if (!d->hlslGen) |
| 751 | return QByteArray(); |
| 752 | |
| 753 | spvc_compiler_options options = nullptr; |
| 754 | if (spvc_compiler_create_compiler_options(compiler: d->hlslGen, options: &options) != SPVC_SUCCESS) |
| 755 | return QByteArray(); |
| 756 | spvc_compiler_options_set_uint(options, option: SPVC_COMPILER_OPTION_HLSL_SHADER_MODEL, |
| 757 | value: version); |
| 758 | spvc_compiler_options_set_bool(options, option: SPVC_COMPILER_OPTION_HLSL_POINT_SIZE_COMPAT, |
| 759 | value: true); |
| 760 | spvc_compiler_options_set_bool(options, option: SPVC_COMPILER_OPTION_HLSL_POINT_COORD_COMPAT, |
| 761 | value: true); |
| 762 | spvc_compiler_options_set_bool(options, option: SPVC_COMPILER_OPTION_HLSL_FORCE_STORAGE_BUFFER_AS_UAV, |
| 763 | value: true); |
| 764 | spvc_compiler_install_compiler_options(compiler: d->hlslGen, options); |
| 765 | |
| 766 | // D3D11_COMMONSHADER_SAMPLER_SLOT_COUNT is 16, so we cannot have combined |
| 767 | // image/sampler bindings above 15. Problem is, the (SPIR-V) binding |
| 768 | // namespace is common with uniform buffers and other resources, and so |
| 769 | // having many uniform buffers and samplers can push the sampler binding to |
| 770 | // above 15 even when there are less than 16 samplers in total. So cannot |
| 771 | // just rely on SPIRV-Cross setting registers t<binding> and s<binding> for |
| 772 | // the SRV and sampler for each combined image/sampler. Instead, we want |
| 773 | // t<slot> and s<slot> where slot is 0-based, with its own namespace, and |
| 774 | // is then exposed in the native resource binding map so QRhi can map |
| 775 | // between (SPIR-V) bindings and D3D sampler slots. |
| 776 | const SpvExecutionModel stage = spvc_compiler_get_execution_model(compiler: d->hlslGen); |
| 777 | int regBinding = 0; // SRVs and samplers |
| 778 | for (const QShaderDescription::InOutVariable &var : d->shaderDescription.combinedImageSamplers()) { |
| 779 | spvc_hlsl_resource_binding bindingMapping; |
| 780 | bindingMapping.stage = stage; // will be per-stage but we have a per-shader NativeResourceBindingMap so it's ok |
| 781 | bindingMapping.desc_set = var.descriptorSet; |
| 782 | bindingMapping.binding = var.binding; |
| 783 | bindingMapping.srv.register_space = 0; |
| 784 | bindingMapping.srv.register_binding = regBinding; // t0, t1, ... |
| 785 | bindingMapping.sampler.register_space = 0; |
| 786 | bindingMapping.sampler.register_binding = regBinding; // s0, s1, ... |
| 787 | spvc_compiler_hlsl_add_resource_binding(compiler: d->hlslGen, binding: &bindingMapping); |
| 788 | nativeBindings->insert(key: var.binding, value: { regBinding, regBinding }); |
| 789 | regBinding += var.arrayDims.isEmpty() ? 1 : var.arrayDims.first(); |
| 790 | } |
| 791 | // In fact, with separate images and samplers added to the mix, the manual |
| 792 | // mapping specification is essential so that the HLSL nicely uses t0, t1, |
| 793 | // t2, ... and s0, s1, s2, ... for all the images and samplers incl. both |
| 794 | // combined and separate. |
| 795 | int firstSeparateImageReg = regBinding; |
| 796 | for (const QShaderDescription::InOutVariable &var : d->shaderDescription.separateImages()) { |
| 797 | spvc_hlsl_resource_binding bindingMapping; |
| 798 | bindingMapping.stage = stage; |
| 799 | bindingMapping.desc_set = var.descriptorSet; |
| 800 | bindingMapping.binding = var.binding; |
| 801 | bindingMapping.srv.register_space = 0; |
| 802 | bindingMapping.srv.register_binding = regBinding; // tN, tN+1, ..., where N is the next reg after the combined image sampler ones |
| 803 | spvc_compiler_hlsl_add_resource_binding(compiler: d->hlslGen, binding: &bindingMapping); |
| 804 | nativeBindings->insert(key: var.binding, value: { regBinding, -1 }); |
| 805 | regBinding += var.arrayDims.isEmpty() ? 1 : var.arrayDims.first(); |
| 806 | } |
| 807 | regBinding = firstSeparateImageReg; |
| 808 | for (const QShaderDescription::InOutVariable &var : d->shaderDescription.separateSamplers()) { |
| 809 | spvc_hlsl_resource_binding bindingMapping; |
| 810 | bindingMapping.stage = stage; |
| 811 | bindingMapping.desc_set = var.descriptorSet; |
| 812 | bindingMapping.binding = var.binding; |
| 813 | bindingMapping.sampler.register_space = 0; |
| 814 | bindingMapping.sampler.register_binding = regBinding; // sN, sN+1, ..., where N is the next reg after the combined image sampler ones |
| 815 | spvc_compiler_hlsl_add_resource_binding(compiler: d->hlslGen, binding: &bindingMapping); |
| 816 | nativeBindings->insert(key: var.binding, value: { regBinding, -1 }); |
| 817 | regBinding += var.arrayDims.isEmpty() ? 1 : var.arrayDims.first(); |
| 818 | } |
| 819 | |
| 820 | regBinding = 0; // CBVs |
| 821 | for (const QShaderDescription::UniformBlock &blk : d->shaderDescription.uniformBlocks()) { |
| 822 | spvc_hlsl_resource_binding bindingMapping; |
| 823 | bindingMapping.stage = stage; |
| 824 | bindingMapping.desc_set = blk.descriptorSet; |
| 825 | bindingMapping.binding = blk.binding; |
| 826 | bindingMapping.cbv.register_space = 0; |
| 827 | bindingMapping.cbv.register_binding = regBinding; // b0, b1, ... |
| 828 | spvc_compiler_hlsl_add_resource_binding(compiler: d->hlslGen, binding: &bindingMapping); |
| 829 | nativeBindings->insert(key: blk.binding, value: { regBinding, -1 }); |
| 830 | regBinding += 1; |
| 831 | } |
| 832 | |
| 833 | regBinding = 0; // UAVs |
| 834 | for (const QShaderDescription::StorageBlock &blk : d->shaderDescription.storageBlocks()) { |
| 835 | // readonly is also mapped to UAV due to FORCE_STORAGE_BUFFER_AS_UAV. (would be an SRV by default) |
| 836 | spvc_hlsl_resource_binding bindingMapping; |
| 837 | bindingMapping.stage = stage; |
| 838 | bindingMapping.desc_set = blk.descriptorSet; |
| 839 | bindingMapping.binding = blk.binding; |
| 840 | bindingMapping.uav.register_space = 0; |
| 841 | bindingMapping.uav.register_binding = regBinding; // u0, u1, ... |
| 842 | spvc_compiler_hlsl_add_resource_binding(compiler: d->hlslGen, binding: &bindingMapping); |
| 843 | nativeBindings->insert(key: blk.binding, value: { regBinding, -1 }); |
| 844 | regBinding += 1; |
| 845 | } |
| 846 | for (const QShaderDescription::InOutVariable &var : d->shaderDescription.storageImages()) { |
| 847 | spvc_hlsl_resource_binding bindingMapping; |
| 848 | bindingMapping.stage = stage; |
| 849 | bindingMapping.desc_set = var.descriptorSet; |
| 850 | bindingMapping.binding = var.binding; |
| 851 | bindingMapping.uav.register_space = 0; |
| 852 | bindingMapping.uav.register_binding = regBinding; // u0, u1, ... |
| 853 | spvc_compiler_hlsl_add_resource_binding(compiler: d->hlslGen, binding: &bindingMapping); |
| 854 | nativeBindings->insert(key: var.binding, value: { regBinding, -1 }); |
| 855 | regBinding += 1; |
| 856 | } |
| 857 | |
| 858 | const char *result = nullptr; |
| 859 | if (spvc_compiler_compile(compiler: d->hlslGen, source: &result) != SPVC_SUCCESS) { |
| 860 | d->spirvCrossErrorMsg = QString::fromUtf8(utf8: spvc_context_get_last_error_string(context: d->ctx)); |
| 861 | return QByteArray(); |
| 862 | } |
| 863 | |
| 864 | return QByteArray(result); |
| 865 | } |
| 866 | |
| 867 | QByteArray QSpirvShader::translateToMSL(int version, |
| 868 | MslFlags flags, |
| 869 | QShader::Stage stage, |
| 870 | QShader::NativeResourceBindingMap *nativeBindings, |
| 871 | QShader::NativeShaderInfo *shaderInfo, |
| 872 | const MultiViewInfo &multiViewInfo, |
| 873 | const TessellationInfo &tessInfo) const |
| 874 | { |
| 875 | d->spirvCrossErrorMsg.clear(); |
| 876 | |
| 877 | d->createCompiler(backend: SPVC_BACKEND_MSL); |
| 878 | if (!d->mslGen) |
| 879 | return QByteArray(); |
| 880 | |
| 881 | spvc_compiler_options options = nullptr; |
| 882 | if (spvc_compiler_create_compiler_options(compiler: d->mslGen, options: &options) != SPVC_SUCCESS) |
| 883 | return QByteArray(); |
| 884 | |
| 885 | spvc_compiler_options_set_uint(options, option: SPVC_COMPILER_OPTION_MSL_VERSION, |
| 886 | SPVC_MAKE_MSL_VERSION(version / 10, version % 10, 0)); |
| 887 | |
| 888 | if (flags.testFlag(flag: MslFlag::VertexAsCompute)) { |
| 889 | spvc_compiler_options_set_bool(options, option: SPVC_COMPILER_OPTION_MSL_VERTEX_FOR_TESSELLATION, value: 1); |
| 890 | spvc_compiler_options_set_bool(options, option: SPVC_COMPILER_OPTION_MSL_CAPTURE_OUTPUT_TO_BUFFER, value: 1); |
| 891 | spvc_compiler_options_set_bool(options, option: SPVC_COMPILER_OPTION_MSL_DISABLE_RASTERIZATION, value: 1); |
| 892 | |
| 893 | spvc_msl_index_type indexType = SPVC_MSL_INDEX_TYPE_NONE; |
| 894 | if (flags.testFlag(flag: MslFlag::WithUInt16Index)) |
| 895 | indexType = SPVC_MSL_INDEX_TYPE_UINT16; |
| 896 | else if (flags.testFlag(flag: MslFlag::WithUInt32Index)) |
| 897 | indexType = SPVC_MSL_INDEX_TYPE_UINT32; |
| 898 | spvc_compiler_options_set_uint(options, option: SPVC_COMPILER_OPTION_MSL_VERTEX_INDEX_TYPE, value: indexType); |
| 899 | } |
| 900 | |
| 901 | // for tessellation; matches the defaults; not sure how to query so just set them |
| 902 | uint spvIndicesBufferIndex = 21; |
| 903 | uint spvInBufferIndex = 22; |
| 904 | uint spvTessLevelBufferIndex = 26; |
| 905 | uint spvPatchOutBufferIndex = 27; |
| 906 | uint spvOutBufferIndex = 28; |
| 907 | uint spvIndirectParamsBufferIndex = 29; |
| 908 | |
| 909 | spvc_compiler_options_set_uint(options, option: SPVC_COMPILER_OPTION_MSL_SHADER_INDEX_BUFFER_INDEX, value: spvIndicesBufferIndex); |
| 910 | spvc_compiler_options_set_uint(options, option: SPVC_COMPILER_OPTION_MSL_SHADER_INPUT_BUFFER_INDEX, value: spvInBufferIndex); |
| 911 | spvc_compiler_options_set_uint(options, option: SPVC_COMPILER_OPTION_MSL_SHADER_TESS_FACTOR_OUTPUT_BUFFER_INDEX, value: spvTessLevelBufferIndex); |
| 912 | spvc_compiler_options_set_uint(options, option: SPVC_COMPILER_OPTION_MSL_SHADER_PATCH_OUTPUT_BUFFER_INDEX, value: spvPatchOutBufferIndex); |
| 913 | spvc_compiler_options_set_uint(options, option: SPVC_COMPILER_OPTION_MSL_SHADER_OUTPUT_BUFFER_INDEX, value: spvOutBufferIndex); |
| 914 | spvc_compiler_options_set_uint(options, option: SPVC_COMPILER_OPTION_MSL_INDIRECT_PARAMS_BUFFER_INDEX, value: spvIndirectParamsBufferIndex); |
| 915 | |
| 916 | // for buffer size buffer; matches defaults |
| 917 | uint spvBufferSizeBufferIndex = 25; |
| 918 | spvc_compiler_options_set_uint(options, option: SPVC_COMPILER_OPTION_MSL_BUFFER_SIZE_BUFFER_INDEX, value: spvBufferSizeBufferIndex); |
| 919 | |
| 920 | if (stage == QShader::TessellationControlStage) { |
| 921 | // required to get the kind of tess.control inputs we need |
| 922 | spvc_compiler_options_set_bool(options, option: SPVC_COMPILER_OPTION_MSL_MULTI_PATCH_WORKGROUP, value: 1); |
| 923 | |
| 924 | // the execution mode needs to be known (e.g. to switch between |
| 925 | // MTLTriangleTessellationFactorsHalf and MTLQuadTessellationFactorsHalf) |
| 926 | SpvExecutionMode execMode = SpvExecutionModeTriangles; |
| 927 | switch (tessInfo.infoForTesc.mode) { |
| 928 | case QShaderDescription::QuadTessellationMode: |
| 929 | execMode = SpvExecutionModeQuads; |
| 930 | break; |
| 931 | case QShaderDescription::IsolineTessellationMode: |
| 932 | d->spirvCrossErrorMsg = QLatin1String("Isoline tessellation mode is not supported with Metal" ); |
| 933 | return QByteArray(); |
| 934 | default: |
| 935 | break; |
| 936 | } |
| 937 | spvc_compiler_set_execution_mode(compiler: d->mslGen, mode: execMode); |
| 938 | } |
| 939 | |
| 940 | if (stage == QShader::TessellationEvaluationStage) |
| 941 | spvc_compiler_set_execution_mode_with_arguments(compiler: d->mslGen, mode: SpvExecutionModeOutputVertices, arg0: uint(tessInfo.infoForTese.vertexCount), arg1: 0, arg2: 0); |
| 942 | |
| 943 | uint spvViewMaskBufferIndex = 24; |
| 944 | const bool isMultiView = stage == QShader::VertexStage && multiViewInfo.viewCount > 1; |
| 945 | if (isMultiView) { |
| 946 | spvc_compiler_options_set_bool(options, option: SPVC_COMPILER_OPTION_MSL_MULTIVIEW, value: 1); |
| 947 | spvc_compiler_options_set_bool(options, option: SPVC_COMPILER_OPTION_MSL_MULTIVIEW_LAYERED_RENDERING, value: 1); |
| 948 | spvc_compiler_options_set_uint(options, option: SPVC_COMPILER_OPTION_MSL_VIEW_MASK_BUFFER_INDEX, value: spvViewMaskBufferIndex); |
| 949 | } |
| 950 | |
| 951 | // leave platform set to macOS, it won't matter in practice (hopefully) |
| 952 | spvc_compiler_install_compiler_options(compiler: d->mslGen, options); |
| 953 | |
| 954 | const char *result = nullptr; |
| 955 | if (spvc_compiler_compile(compiler: d->mslGen, source: &result) != SPVC_SUCCESS) { |
| 956 | d->spirvCrossErrorMsg = QString::fromUtf8(utf8: spvc_context_get_last_error_string(context: d->ctx)); |
| 957 | return QByteArray(); |
| 958 | } |
| 959 | |
| 960 | if (nativeBindings) { |
| 961 | spvc_resources resources; |
| 962 | if (spvc_compiler_create_shader_resources(compiler: d->mslGen, resources: &resources) == SPVC_SUCCESS) { |
| 963 | const spvc_reflected_resource *resourceList = nullptr; |
| 964 | size_t resourceListCount = 0; |
| 965 | // A nativeBinding of -1 means unused. This also fits |
| 966 | // *_get_automatic_resource_binding() which returns uint32_t(-1) |
| 967 | // when there is no binding, which can happen when the uniform |
| 968 | // block, sampler, etc. is not actively used in the shader. The map |
| 969 | // must always be complete, including a (binding -> -1) mapping for |
| 970 | // inactive resources as well. The second value of the pair is only |
| 971 | // relevant for combined image samplers, and is -1 otherwise. |
| 972 | if (spvc_resources_get_resource_list_for_type(resources, type: SPVC_RESOURCE_TYPE_UNIFORM_BUFFER, |
| 973 | resource_list: &resourceList, resource_size: &resourceListCount) == SPVC_SUCCESS) |
| 974 | { |
| 975 | for (size_t i = 0; i < resourceListCount; ++i) { |
| 976 | unsigned binding = spvc_compiler_get_decoration(compiler: d->mslGen, id: resourceList[i].id, decoration: SpvDecorationBinding); |
| 977 | unsigned nativeBinding = spvc_compiler_msl_get_automatic_resource_binding(compiler: d->mslGen, id: resourceList[i].id); |
| 978 | nativeBindings->insert(key: int(binding), value: { int(nativeBinding), -1 }); |
| 979 | } |
| 980 | } |
| 981 | if (spvc_resources_get_resource_list_for_type(resources, type: SPVC_RESOURCE_TYPE_STORAGE_BUFFER, |
| 982 | resource_list: &resourceList, resource_size: &resourceListCount) == SPVC_SUCCESS) |
| 983 | { |
| 984 | for (size_t i = 0; i < resourceListCount; ++i) { |
| 985 | unsigned binding = spvc_compiler_get_decoration(compiler: d->mslGen, id: resourceList[i].id, decoration: SpvDecorationBinding); |
| 986 | unsigned nativeBinding = spvc_compiler_msl_get_automatic_resource_binding(compiler: d->mslGen, id: resourceList[i].id); |
| 987 | nativeBindings->insert(key: int(binding), value: { int(nativeBinding), -1 }); |
| 988 | } |
| 989 | } |
| 990 | if (spvc_resources_get_resource_list_for_type(resources, type: SPVC_RESOURCE_TYPE_SAMPLED_IMAGE, |
| 991 | resource_list: &resourceList, resource_size: &resourceListCount) == SPVC_SUCCESS) |
| 992 | { |
| 993 | for (size_t i = 0; i < resourceListCount; ++i) { |
| 994 | unsigned binding = spvc_compiler_get_decoration(compiler: d->mslGen, id: resourceList[i].id, decoration: SpvDecorationBinding); |
| 995 | unsigned nativeTextureBinding = spvc_compiler_msl_get_automatic_resource_binding(compiler: d->mslGen, id: resourceList[i].id); |
| 996 | unsigned nativeSamplerBinding = spvc_compiler_msl_get_automatic_resource_binding_secondary(compiler: d->mslGen, id: resourceList[i].id); |
| 997 | nativeBindings->insert(key: int(binding), value: { int(nativeTextureBinding), int(nativeSamplerBinding) }); |
| 998 | } |
| 999 | } |
| 1000 | if (spvc_resources_get_resource_list_for_type(resources, type: SPVC_RESOURCE_TYPE_SEPARATE_IMAGE, |
| 1001 | resource_list: &resourceList, resource_size: &resourceListCount) == SPVC_SUCCESS) |
| 1002 | { |
| 1003 | for (size_t i = 0; i < resourceListCount; ++i) { |
| 1004 | unsigned binding = spvc_compiler_get_decoration(compiler: d->mslGen, id: resourceList[i].id, decoration: SpvDecorationBinding); |
| 1005 | unsigned nativeTextureBinding = spvc_compiler_msl_get_automatic_resource_binding(compiler: d->mslGen, id: resourceList[i].id); |
| 1006 | nativeBindings->insert(key: int(binding), value: { int(nativeTextureBinding), -1 }); |
| 1007 | } |
| 1008 | } |
| 1009 | if (spvc_resources_get_resource_list_for_type(resources, type: SPVC_RESOURCE_TYPE_SEPARATE_SAMPLERS, |
| 1010 | resource_list: &resourceList, resource_size: &resourceListCount) == SPVC_SUCCESS) |
| 1011 | { |
| 1012 | for (size_t i = 0; i < resourceListCount; ++i) { |
| 1013 | unsigned binding = spvc_compiler_get_decoration(compiler: d->mslGen, id: resourceList[i].id, decoration: SpvDecorationBinding); |
| 1014 | unsigned nativeSamplerBinding = spvc_compiler_msl_get_automatic_resource_binding(compiler: d->mslGen, id: resourceList[i].id); |
| 1015 | nativeBindings->insert(key: int(binding), value: { int(nativeSamplerBinding), -1 }); |
| 1016 | } |
| 1017 | } |
| 1018 | if (spvc_resources_get_resource_list_for_type(resources, type: SPVC_RESOURCE_TYPE_SAMPLED_IMAGE, |
| 1019 | resource_list: &resourceList, resource_size: &resourceListCount) == SPVC_SUCCESS) |
| 1020 | { |
| 1021 | for (size_t i = 0; i < resourceListCount; ++i) { |
| 1022 | unsigned binding = spvc_compiler_get_decoration(compiler: d->mslGen, id: resourceList[i].id, decoration: SpvDecorationBinding); |
| 1023 | unsigned nativeTextureBinding = spvc_compiler_msl_get_automatic_resource_binding(compiler: d->mslGen, id: resourceList[i].id); |
| 1024 | unsigned nativeSamplerBinding = spvc_compiler_msl_get_automatic_resource_binding_secondary(compiler: d->mslGen, id: resourceList[i].id); |
| 1025 | nativeBindings->insert(key: int(binding), value: { int(nativeTextureBinding), int(nativeSamplerBinding) }); |
| 1026 | } |
| 1027 | } |
| 1028 | if (spvc_resources_get_resource_list_for_type(resources, type: SPVC_RESOURCE_TYPE_STORAGE_IMAGE, |
| 1029 | resource_list: &resourceList, resource_size: &resourceListCount) == SPVC_SUCCESS) |
| 1030 | { |
| 1031 | for (size_t i = 0; i < resourceListCount; ++i) { |
| 1032 | unsigned binding = spvc_compiler_get_decoration(compiler: d->mslGen, id: resourceList[i].id, decoration: SpvDecorationBinding); |
| 1033 | unsigned nativeBinding = spvc_compiler_msl_get_automatic_resource_binding(compiler: d->mslGen, id: resourceList[i].id); |
| 1034 | nativeBindings->insert(key: int(binding), value: { int(nativeBinding), -1 }); |
| 1035 | } |
| 1036 | } |
| 1037 | } |
| 1038 | } |
| 1039 | |
| 1040 | if (spvc_compiler_msl_needs_swizzle_buffer(compiler: d->mslGen)) |
| 1041 | qWarning(msg: "Translated Metal shader needs swizzle buffer, this is unexpected" ); |
| 1042 | |
| 1043 | if (spvc_compiler_msl_needs_buffer_size_buffer(compiler: d->mslGen)) |
| 1044 | shaderInfo->extraBufferBindings[QShaderPrivate::MslBufferSizeBufferBinding] = spvBufferSizeBufferIndex; |
| 1045 | |
| 1046 | if (isMultiView) |
| 1047 | shaderInfo->extraBufferBindings[QShaderPrivate::MslMultiViewMaskBufferBinding] = spvViewMaskBufferIndex; |
| 1048 | |
| 1049 | // (Aim to) only store extraBufferBindings entries for things that really |
| 1050 | // are present, because the presence of a key can already trigger certain |
| 1051 | // behavior during rendering. (e.g. generating and exposing the |
| 1052 | // corresponding implicit buffer) |
| 1053 | switch (spvc_compiler_get_execution_model(compiler: d->mslGen)) { |
| 1054 | case SpvExecutionModelVertex: |
| 1055 | if (flags.testFlag(flag: MslFlag::VertexAsCompute)) { |
| 1056 | if (spvc_compiler_msl_needs_output_buffer(compiler: d->mslGen)) |
| 1057 | shaderInfo->extraBufferBindings[QShaderPrivate::MslTessVertTescOutputBufferBinding] = spvOutBufferIndex; |
| 1058 | if (flags.testFlag(flag: MslFlag::WithUInt16Index) || flags.testFlag(flag: MslFlag::WithUInt32Index)) { |
| 1059 | // not given that this buffer is really used in the code, |
| 1060 | // depends on e.g. the usage of gl_VertexIndex, but not sure how |
| 1061 | // to tell that here |
| 1062 | shaderInfo->extraBufferBindings[QShaderPrivate::MslTessVertIndicesBufferBinding] = spvIndicesBufferIndex; |
| 1063 | } |
| 1064 | } |
| 1065 | break; |
| 1066 | case SpvExecutionModelTessellationControl: |
| 1067 | shaderInfo->extraBufferBindings[QShaderPrivate::MslTessTescInputBufferBinding] = spvInBufferIndex; |
| 1068 | shaderInfo->extraBufferBindings[QShaderPrivate::MslTessTescTessLevelBufferBinding] = spvTessLevelBufferIndex; |
| 1069 | shaderInfo->extraBufferBindings[QShaderPrivate::MslTessTescParamsBufferBinding] = spvIndirectParamsBufferIndex; |
| 1070 | if (spvc_compiler_msl_needs_output_buffer(compiler: d->mslGen)) |
| 1071 | shaderInfo->extraBufferBindings[QShaderPrivate::MslTessVertTescOutputBufferBinding] = spvOutBufferIndex; |
| 1072 | if (spvc_compiler_msl_needs_patch_output_buffer(compiler: d->mslGen)) |
| 1073 | shaderInfo->extraBufferBindings[QShaderPrivate::MslTessTescPatchOutputBufferBinding] = spvPatchOutBufferIndex; |
| 1074 | break; |
| 1075 | default: |
| 1076 | break; |
| 1077 | } |
| 1078 | |
| 1079 | return QByteArray(result); |
| 1080 | } |
| 1081 | |
| 1082 | QString QSpirvShader::translationErrorMessage() const |
| 1083 | { |
| 1084 | return d->spirvCrossErrorMsg; |
| 1085 | } |
| 1086 | |
| 1087 | QT_END_NAMESPACE |
| 1088 | |