Coverage Report

Created: 2024-09-11 07:09

/src/spirv-tools/source/val/validate_builtins.cpp
Line
Count
Source (jump to first uncovered line)
1
// Copyright (c) 2018 Google LLC.
2
// Modifications Copyright (C) 2020 Advanced Micro Devices, Inc. All rights
3
// reserved.
4
//
5
// Licensed under the Apache License, Version 2.0 (the "License");
6
// you may not use this file except in compliance with the License.
7
// You may obtain a copy of the License at
8
//
9
//     http://www.apache.org/licenses/LICENSE-2.0
10
//
11
// Unless required by applicable law or agreed to in writing, software
12
// distributed under the License is distributed on an "AS IS" BASIS,
13
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14
// See the License for the specific language governing permissions and
15
// limitations under the License.
16
17
// Validates correctness of built-in variables.
18
19
#include <array>
20
#include <functional>
21
#include <list>
22
#include <map>
23
#include <set>
24
#include <sstream>
25
#include <stack>
26
#include <string>
27
#include <vector>
28
29
#include "source/opcode.h"
30
#include "source/spirv_target_env.h"
31
#include "source/util/bitutils.h"
32
#include "source/val/instruction.h"
33
#include "source/val/validate.h"
34
#include "source/val/validation_state.h"
35
36
namespace spvtools {
37
namespace val {
38
namespace {
39
40
// Returns a short textual description of the id defined by the given
41
// instruction.
42
0
std::string GetIdDesc(const Instruction& inst) {
43
0
  std::ostringstream ss;
44
0
  ss << "ID <" << inst.id() << "> (Op" << spvOpcodeString(inst.opcode()) << ")";
45
0
  return ss.str();
46
0
}
47
48
// Gets underlying data type which is
49
// - member type if instruction is OpTypeStruct
50
//   (member index is taken from decoration).
51
// - data type if id creates a pointer.
52
// - type of the constant if instruction is OpConst or OpSpecConst.
53
//
54
// Fails in any other case. The function is based on built-ins allowed by
55
// the Vulkan spec.
56
// TODO: If non-Vulkan validation rules are added then it might need
57
// to be refactored.
58
spv_result_t GetUnderlyingType(ValidationState_t& _,
59
                               const Decoration& decoration,
60
                               const Instruction& inst,
61
0
                               uint32_t* underlying_type) {
62
0
  if (decoration.struct_member_index() != Decoration::kInvalidMember) {
63
0
    if (inst.opcode() != spv::Op::OpTypeStruct) {
64
0
      return _.diag(SPV_ERROR_INVALID_DATA, &inst)
65
0
             << GetIdDesc(inst)
66
0
             << "Attempted to get underlying data type via member index for "
67
0
                "non-struct type.";
68
0
    }
69
0
    *underlying_type = inst.word(decoration.struct_member_index() + 2);
70
0
    return SPV_SUCCESS;
71
0
  }
72
73
0
  if (inst.opcode() == spv::Op::OpTypeStruct) {
74
0
    return _.diag(SPV_ERROR_INVALID_DATA, &inst)
75
0
           << GetIdDesc(inst)
76
0
           << " did not find an member index to get underlying data type for "
77
0
              "struct type.";
78
0
  }
79
80
0
  if (spvOpcodeIsConstant(inst.opcode())) {
81
0
    *underlying_type = inst.type_id();
82
0
    return SPV_SUCCESS;
83
0
  }
84
85
0
  spv::StorageClass storage_class;
86
0
  if (!_.GetPointerTypeInfo(inst.type_id(), underlying_type, &storage_class)) {
87
0
    return _.diag(SPV_ERROR_INVALID_DATA, &inst)
88
0
           << GetIdDesc(inst)
89
0
           << " is decorated with BuiltIn. BuiltIn decoration should only be "
90
0
              "applied to struct types, variables and constants.";
91
0
  }
92
0
  return SPV_SUCCESS;
93
0
}
94
95
// Returns Storage Class used by the instruction if applicable.
96
// Returns spv::StorageClass::Max if not.
97
0
spv::StorageClass GetStorageClass(const Instruction& inst) {
98
0
  switch (inst.opcode()) {
99
0
    case spv::Op::OpTypePointer:
100
0
    case spv::Op::OpTypeUntypedPointerKHR:
101
0
    case spv::Op::OpTypeForwardPointer: {
102
0
      return spv::StorageClass(inst.word(2));
103
0
    }
104
0
    case spv::Op::OpVariable: {
105
0
      return spv::StorageClass(inst.word(3));
106
0
    }
107
0
    case spv::Op::OpUntypedVariableKHR: {
108
0
      return spv::StorageClass(inst.word(4));
109
0
    }
110
0
    case spv::Op::OpGenericCastToPtrExplicit: {
111
0
      return spv::StorageClass(inst.word(4));
112
0
    }
113
0
    default: { break; }
114
0
  }
115
0
  return spv::StorageClass::Max;
116
0
}
117
118
typedef enum VUIDError_ {
119
  VUIDErrorExecutionModel = 0,
120
  VUIDErrorStorageClass = 1,
121
  VUIDErrorType = 2,
122
  VUIDErrorMax,
123
} VUIDError;
124
125
const static uint32_t NumVUIDBuiltins = 39;
126
127
typedef struct {
128
  spv::BuiltIn builtIn;
129
  uint32_t vuid[VUIDErrorMax];  // execution mode, storage class, type VUIDs
130
} BuiltinVUIDMapping;
131
132
// Many built-ins have the same checks (Storage Class, Type, etc)
133
// This table provides a nice LUT for the VUIDs
134
std::array<BuiltinVUIDMapping, NumVUIDBuiltins> builtinVUIDInfo = {{
135
    // clang-format off
136
    {spv::BuiltIn::SubgroupEqMask,            {0,    4370, 4371}},
137
    {spv::BuiltIn::SubgroupGeMask,            {0,    4372, 4373}},
138
    {spv::BuiltIn::SubgroupGtMask,            {0,    4374, 4375}},
139
    {spv::BuiltIn::SubgroupLeMask,            {0,    4376, 4377}},
140
    {spv::BuiltIn::SubgroupLtMask,            {0,    4378, 4379}},
141
    {spv::BuiltIn::SubgroupLocalInvocationId, {0,    4380, 4381}},
142
    {spv::BuiltIn::SubgroupSize,              {0,    4382, 4383}},
143
    {spv::BuiltIn::GlobalInvocationId,        {4236, 4237, 4238}},
144
    {spv::BuiltIn::LocalInvocationId,         {4281, 4282, 4283}},
145
    {spv::BuiltIn::NumWorkgroups,             {4296, 4297, 4298}},
146
    {spv::BuiltIn::NumSubgroups,              {4293, 4294, 4295}},
147
    {spv::BuiltIn::SubgroupId,                {4367, 4368, 4369}},
148
    {spv::BuiltIn::WorkgroupId,               {4422, 4423, 4424}},
149
    {spv::BuiltIn::HitKindKHR,                {4242, 4243, 4244}},
150
    {spv::BuiltIn::HitTNV,                    {4245, 4246, 4247}},
151
    {spv::BuiltIn::InstanceCustomIndexKHR,    {4251, 4252, 4253}},
152
    {spv::BuiltIn::InstanceId,                {4254, 4255, 4256}},
153
    {spv::BuiltIn::RayGeometryIndexKHR,       {4345, 4346, 4347}},
154
    {spv::BuiltIn::ObjectRayDirectionKHR,     {4299, 4300, 4301}},
155
    {spv::BuiltIn::ObjectRayOriginKHR,        {4302, 4303, 4304}},
156
    {spv::BuiltIn::ObjectToWorldKHR,          {4305, 4306, 4307}},
157
    {spv::BuiltIn::WorldToObjectKHR,          {4434, 4435, 4436}},
158
    {spv::BuiltIn::IncomingRayFlagsKHR,       {4248, 4249, 4250}},
159
    {spv::BuiltIn::RayTminKHR,                {4351, 4352, 4353}},
160
    {spv::BuiltIn::RayTmaxKHR,                {4348, 4349, 4350}},
161
    {spv::BuiltIn::WorldRayDirectionKHR,      {4428, 4429, 4430}},
162
    {spv::BuiltIn::WorldRayOriginKHR,         {4431, 4432, 4433}},
163
    {spv::BuiltIn::LaunchIdKHR,               {4266, 4267, 4268}},
164
    {spv::BuiltIn::LaunchSizeKHR,             {4269, 4270, 4271}},
165
    {spv::BuiltIn::FragInvocationCountEXT,    {4217, 4218, 4219}},
166
    {spv::BuiltIn::FragSizeEXT,               {4220, 4221, 4222}},
167
    {spv::BuiltIn::FragStencilRefEXT,         {4223, 4224, 4225}},
168
    {spv::BuiltIn::FullyCoveredEXT,           {4232, 4233, 4234}},
169
    {spv::BuiltIn::CullMaskKHR,               {6735, 6736, 6737}},
170
    {spv::BuiltIn::BaryCoordKHR,              {4154, 4155, 4156}},
171
    {spv::BuiltIn::BaryCoordNoPerspKHR,       {4160, 4161, 4162}},
172
    {spv::BuiltIn::PrimitivePointIndicesEXT,  {7041, 7043, 7044}},
173
    {spv::BuiltIn::PrimitiveLineIndicesEXT,   {7047, 7049, 7050}},
174
    {spv::BuiltIn::PrimitiveTriangleIndicesEXT, {7053, 7055, 7056}},
175
    // clang-format on
176
}};
177
178
0
uint32_t GetVUIDForBuiltin(spv::BuiltIn builtIn, VUIDError type) {
179
0
  uint32_t vuid = 0;
180
0
  for (const auto& iter: builtinVUIDInfo) {
181
0
    if (iter.builtIn == builtIn) {
182
0
      assert(type < VUIDErrorMax);
183
0
      vuid = iter.vuid[type];
184
0
      break;
185
0
    }
186
0
  }
187
0
  return vuid;
188
0
}
189
190
bool IsExecutionModelValidForRtBuiltIn(spv::BuiltIn builtin,
191
0
                                       spv::ExecutionModel stage) {
192
0
  switch (builtin) {
193
0
    case spv::BuiltIn::HitKindKHR:
194
0
    case spv::BuiltIn::HitTNV:
195
0
      if (stage == spv::ExecutionModel::AnyHitKHR ||
196
0
          stage == spv::ExecutionModel::ClosestHitKHR) {
197
0
        return true;
198
0
      }
199
0
      break;
200
0
    case spv::BuiltIn::InstanceCustomIndexKHR:
201
0
    case spv::BuiltIn::InstanceId:
202
0
    case spv::BuiltIn::RayGeometryIndexKHR:
203
0
    case spv::BuiltIn::ObjectRayDirectionKHR:
204
0
    case spv::BuiltIn::ObjectRayOriginKHR:
205
0
    case spv::BuiltIn::ObjectToWorldKHR:
206
0
    case spv::BuiltIn::WorldToObjectKHR:
207
0
      switch (stage) {
208
0
        case spv::ExecutionModel::IntersectionKHR:
209
0
        case spv::ExecutionModel::AnyHitKHR:
210
0
        case spv::ExecutionModel::ClosestHitKHR:
211
0
          return true;
212
0
        default:
213
0
          return false;
214
0
      }
215
0
      break;
216
0
    case spv::BuiltIn::IncomingRayFlagsKHR:
217
0
    case spv::BuiltIn::RayTminKHR:
218
0
    case spv::BuiltIn::RayTmaxKHR:
219
0
    case spv::BuiltIn::WorldRayDirectionKHR:
220
0
    case spv::BuiltIn::WorldRayOriginKHR:
221
0
    case spv::BuiltIn::CullMaskKHR:
222
0
      switch (stage) {
223
0
        case spv::ExecutionModel::IntersectionKHR:
224
0
        case spv::ExecutionModel::AnyHitKHR:
225
0
        case spv::ExecutionModel::ClosestHitKHR:
226
0
        case spv::ExecutionModel::MissKHR:
227
0
          return true;
228
0
        default:
229
0
          return false;
230
0
      }
231
0
      break;
232
0
    case spv::BuiltIn::LaunchIdKHR:
233
0
    case spv::BuiltIn::LaunchSizeKHR:
234
0
      switch (stage) {
235
0
        case spv::ExecutionModel::RayGenerationKHR:
236
0
        case spv::ExecutionModel::IntersectionKHR:
237
0
        case spv::ExecutionModel::AnyHitKHR:
238
0
        case spv::ExecutionModel::ClosestHitKHR:
239
0
        case spv::ExecutionModel::MissKHR:
240
0
        case spv::ExecutionModel::CallableKHR:
241
0
          return true;
242
0
        default:
243
0
          return false;
244
0
      }
245
0
      break;
246
0
    default:
247
0
      break;
248
0
  }
249
0
  return false;
250
0
}
251
252
// Helper class managing validation of built-ins.
253
// TODO: Generic functionality of this class can be moved into
254
// ValidationState_t to be made available to other users.
255
class BuiltInsValidator {
256
 public:
257
11.4k
  BuiltInsValidator(ValidationState_t& vstate) : _(vstate) {}
258
259
  // Run validation.
260
  spv_result_t Run();
261
262
 private:
263
  // Goes through all decorations in the module, if decoration is BuiltIn
264
  // calls ValidateSingleBuiltInAtDefinition().
265
  spv_result_t ValidateBuiltInsAtDefinition();
266
267
  // Validates the instruction defining an id with built-in decoration.
268
  // Can be called multiple times for the same id, if multiple built-ins are
269
  // specified. Seeds id_to_at_reference_checks_ with decorated ids if needed.
270
  spv_result_t ValidateSingleBuiltInAtDefinition(const Decoration& decoration,
271
                                                 const Instruction& inst);
272
273
  // The following section contains functions which are called when id defined
274
  // by |inst| is decorated with BuiltIn |decoration|.
275
  // Most functions are specific to a single built-in and have naming scheme:
276
  // ValidateXYZAtDefinition. Some functions are common to multiple kinds of
277
  // BuiltIn.
278
  spv_result_t ValidateClipOrCullDistanceAtDefinition(
279
      const Decoration& decoration, const Instruction& inst);
280
  spv_result_t ValidateFragCoordAtDefinition(const Decoration& decoration,
281
                                             const Instruction& inst);
282
  spv_result_t ValidateFragDepthAtDefinition(const Decoration& decoration,
283
                                             const Instruction& inst);
284
  spv_result_t ValidateFrontFacingAtDefinition(const Decoration& decoration,
285
                                               const Instruction& inst);
286
  spv_result_t ValidateHelperInvocationAtDefinition(
287
      const Decoration& decoration, const Instruction& inst);
288
  spv_result_t ValidateInvocationIdAtDefinition(const Decoration& decoration,
289
                                                const Instruction& inst);
290
  spv_result_t ValidateInstanceIndexAtDefinition(const Decoration& decoration,
291
                                                 const Instruction& inst);
292
  spv_result_t ValidateLayerOrViewportIndexAtDefinition(
293
      const Decoration& decoration, const Instruction& inst);
294
  spv_result_t ValidatePatchVerticesAtDefinition(const Decoration& decoration,
295
                                                 const Instruction& inst);
296
  spv_result_t ValidatePointCoordAtDefinition(const Decoration& decoration,
297
                                              const Instruction& inst);
298
  spv_result_t ValidatePointSizeAtDefinition(const Decoration& decoration,
299
                                             const Instruction& inst);
300
  spv_result_t ValidatePositionAtDefinition(const Decoration& decoration,
301
                                            const Instruction& inst);
302
  spv_result_t ValidatePrimitiveIdAtDefinition(const Decoration& decoration,
303
                                               const Instruction& inst);
304
  spv_result_t ValidateSampleIdAtDefinition(const Decoration& decoration,
305
                                            const Instruction& inst);
306
  spv_result_t ValidateSampleMaskAtDefinition(const Decoration& decoration,
307
                                              const Instruction& inst);
308
  spv_result_t ValidateSamplePositionAtDefinition(const Decoration& decoration,
309
                                                  const Instruction& inst);
310
  spv_result_t ValidateTessCoordAtDefinition(const Decoration& decoration,
311
                                             const Instruction& inst);
312
  spv_result_t ValidateTessLevelOuterAtDefinition(const Decoration& decoration,
313
                                                  const Instruction& inst);
314
  spv_result_t ValidateTessLevelInnerAtDefinition(const Decoration& decoration,
315
                                                  const Instruction& inst);
316
  spv_result_t ValidateVertexIndexAtDefinition(const Decoration& decoration,
317
                                               const Instruction& inst);
318
  spv_result_t ValidateVertexIdAtDefinition(const Decoration& decoration,
319
                                            const Instruction& inst);
320
  spv_result_t ValidateLocalInvocationIndexAtDefinition(
321
      const Decoration& decoration, const Instruction& inst);
322
  spv_result_t ValidateWorkgroupSizeAtDefinition(const Decoration& decoration,
323
                                                 const Instruction& inst);
324
  spv_result_t ValidateBaseInstanceOrVertexAtDefinition(
325
      const Decoration& decoration, const Instruction& inst);
326
  spv_result_t ValidateDrawIndexAtDefinition(const Decoration& decoration,
327
                                             const Instruction& inst);
328
  spv_result_t ValidateViewIndexAtDefinition(const Decoration& decoration,
329
                                             const Instruction& inst);
330
  spv_result_t ValidateDeviceIndexAtDefinition(const Decoration& decoration,
331
                                               const Instruction& inst);
332
  spv_result_t ValidateFragInvocationCountAtDefinition(const Decoration& decoration,
333
                                               const Instruction& inst);
334
  spv_result_t ValidateFragSizeAtDefinition(const Decoration& decoration,
335
                                               const Instruction& inst);
336
  spv_result_t ValidateFragStencilRefAtDefinition(const Decoration& decoration,
337
                                               const Instruction& inst);
338
  spv_result_t ValidateFullyCoveredAtDefinition(const Decoration& decoration,
339
                                               const Instruction& inst);
340
  // Used for GlobalInvocationId, LocalInvocationId, NumWorkgroups, WorkgroupId.
341
  spv_result_t ValidateComputeShaderI32Vec3InputAtDefinition(
342
      const Decoration& decoration, const Instruction& inst);
343
  spv_result_t ValidateNVSMOrARMCoreBuiltinsAtDefinition(const Decoration& decoration,
344
                                              const Instruction& inst);
345
  // Used for BaryCoord, BaryCoordNoPersp.
346
  spv_result_t ValidateFragmentShaderF32Vec3InputAtDefinition(
347
      const Decoration& decoration, const Instruction& inst);
348
  // Used for SubgroupEqMask, SubgroupGeMask, SubgroupGtMask, SubgroupLtMask,
349
  // SubgroupLeMask.
350
  spv_result_t ValidateI32Vec4InputAtDefinition(const Decoration& decoration,
351
                                                const Instruction& inst);
352
  // Used for SubgroupLocalInvocationId, SubgroupSize.
353
  spv_result_t ValidateI32InputAtDefinition(const Decoration& decoration,
354
                                            const Instruction& inst);
355
  // Used for SubgroupId, NumSubgroups.
356
  spv_result_t ValidateComputeI32InputAtDefinition(const Decoration& decoration,
357
                                                   const Instruction& inst);
358
359
  spv_result_t ValidatePrimitiveShadingRateAtDefinition(
360
      const Decoration& decoration, const Instruction& inst);
361
362
  spv_result_t ValidateShadingRateAtDefinition(const Decoration& decoration,
363
                                               const Instruction& inst);
364
365
  spv_result_t ValidateRayTracingBuiltinsAtDefinition(
366
      const Decoration& decoration, const Instruction& inst);
367
368
  spv_result_t ValidateMeshShadingEXTBuiltinsAtDefinition(
369
      const Decoration& decoration, const Instruction& inst);
370
371
  // The following section contains functions which are called when id defined
372
  // by |referenced_inst| is
373
  // 1. referenced by |referenced_from_inst|
374
  // 2. dependent on |built_in_inst| which is decorated with BuiltIn
375
  // |decoration|. Most functions are specific to a single built-in and have
376
  // naming scheme: ValidateXYZAtReference. Some functions are common to
377
  // multiple kinds of BuiltIn.
378
  spv_result_t ValidateFragCoordAtReference(
379
      const Decoration& decoration, const Instruction& built_in_inst,
380
      const Instruction& referenced_inst,
381
      const Instruction& referenced_from_inst);
382
383
  spv_result_t ValidateFragDepthAtReference(
384
      const Decoration& decoration, const Instruction& built_in_inst,
385
      const Instruction& referenced_inst,
386
      const Instruction& referenced_from_inst);
387
388
  spv_result_t ValidateFrontFacingAtReference(
389
      const Decoration& decoration, const Instruction& built_in_inst,
390
      const Instruction& referenced_inst,
391
      const Instruction& referenced_from_inst);
392
393
  spv_result_t ValidateHelperInvocationAtReference(
394
      const Decoration& decoration, const Instruction& built_in_inst,
395
      const Instruction& referenced_inst,
396
      const Instruction& referenced_from_inst);
397
398
  spv_result_t ValidateInvocationIdAtReference(
399
      const Decoration& decoration, const Instruction& built_in_inst,
400
      const Instruction& referenced_inst,
401
      const Instruction& referenced_from_inst);
402
403
  spv_result_t ValidateInstanceIndexAtReference(
404
      const Decoration& decoration, const Instruction& built_in_inst,
405
      const Instruction& referenced_inst,
406
      const Instruction& referenced_from_inst);
407
408
  spv_result_t ValidatePatchVerticesAtReference(
409
      const Decoration& decoration, const Instruction& built_in_inst,
410
      const Instruction& referenced_inst,
411
      const Instruction& referenced_from_inst);
412
413
  spv_result_t ValidatePointCoordAtReference(
414
      const Decoration& decoration, const Instruction& built_in_inst,
415
      const Instruction& referenced_inst,
416
      const Instruction& referenced_from_inst);
417
418
  spv_result_t ValidatePointSizeAtReference(
419
      const Decoration& decoration, const Instruction& built_in_inst,
420
      const Instruction& referenced_inst,
421
      const Instruction& referenced_from_inst);
422
423
  spv_result_t ValidatePositionAtReference(
424
      const Decoration& decoration, const Instruction& built_in_inst,
425
      const Instruction& referenced_inst,
426
      const Instruction& referenced_from_inst);
427
428
  spv_result_t ValidatePrimitiveIdAtReference(
429
      const Decoration& decoration, const Instruction& built_in_inst,
430
      const Instruction& referenced_inst,
431
      const Instruction& referenced_from_inst);
432
433
  spv_result_t ValidateSampleIdAtReference(
434
      const Decoration& decoration, const Instruction& built_in_inst,
435
      const Instruction& referenced_inst,
436
      const Instruction& referenced_from_inst);
437
438
  spv_result_t ValidateSampleMaskAtReference(
439
      const Decoration& decoration, const Instruction& built_in_inst,
440
      const Instruction& referenced_inst,
441
      const Instruction& referenced_from_inst);
442
443
  spv_result_t ValidateSamplePositionAtReference(
444
      const Decoration& decoration, const Instruction& built_in_inst,
445
      const Instruction& referenced_inst,
446
      const Instruction& referenced_from_inst);
447
448
  spv_result_t ValidateTessCoordAtReference(
449
      const Decoration& decoration, const Instruction& built_in_inst,
450
      const Instruction& referenced_inst,
451
      const Instruction& referenced_from_inst);
452
453
  spv_result_t ValidateTessLevelAtReference(
454
      const Decoration& decoration, const Instruction& built_in_inst,
455
      const Instruction& referenced_inst,
456
      const Instruction& referenced_from_inst);
457
458
  spv_result_t ValidateLocalInvocationIndexAtReference(
459
      const Decoration& decoration, const Instruction& built_in_inst,
460
      const Instruction& referenced_inst,
461
      const Instruction& referenced_from_inst);
462
463
  spv_result_t ValidateVertexIndexAtReference(
464
      const Decoration& decoration, const Instruction& built_in_inst,
465
      const Instruction& referenced_inst,
466
      const Instruction& referenced_from_inst);
467
468
  spv_result_t ValidateLayerOrViewportIndexAtReference(
469
      const Decoration& decoration, const Instruction& built_in_inst,
470
      const Instruction& referenced_inst,
471
      const Instruction& referenced_from_inst);
472
473
  spv_result_t ValidateWorkgroupSizeAtReference(
474
      const Decoration& decoration, const Instruction& built_in_inst,
475
      const Instruction& referenced_inst,
476
      const Instruction& referenced_from_inst);
477
478
  spv_result_t ValidateClipOrCullDistanceAtReference(
479
      const Decoration& decoration, const Instruction& built_in_inst,
480
      const Instruction& referenced_inst,
481
      const Instruction& referenced_from_inst);
482
483
  spv_result_t ValidateBaseInstanceOrVertexAtReference(
484
      const Decoration& decoration, const Instruction& built_in_inst,
485
      const Instruction& referenced_inst,
486
      const Instruction& referenced_from_inst);
487
488
  spv_result_t ValidateDrawIndexAtReference(
489
      const Decoration& decoration, const Instruction& built_in_inst,
490
      const Instruction& referenced_inst,
491
      const Instruction& referenced_from_inst);
492
493
  spv_result_t ValidateViewIndexAtReference(
494
      const Decoration& decoration, const Instruction& built_in_inst,
495
      const Instruction& referenced_inst,
496
      const Instruction& referenced_from_inst);
497
498
  spv_result_t ValidateDeviceIndexAtReference(
499
      const Decoration& decoration, const Instruction& built_in_inst,
500
      const Instruction& referenced_inst,
501
      const Instruction& referenced_from_inst);
502
503
  spv_result_t ValidateFragInvocationCountAtReference(
504
      const Decoration& decoration, const Instruction& built_in_inst,
505
      const Instruction& referenced_inst,
506
      const Instruction& referenced_from_inst);
507
508
  spv_result_t ValidateFragSizeAtReference(
509
      const Decoration& decoration, const Instruction& built_in_inst,
510
      const Instruction& referenced_inst,
511
      const Instruction& referenced_from_inst);
512
513
  spv_result_t ValidateFragStencilRefAtReference(
514
      const Decoration& decoration, const Instruction& built_in_inst,
515
      const Instruction& referenced_inst,
516
      const Instruction& referenced_from_inst);
517
518
  spv_result_t ValidateFullyCoveredAtReference(
519
      const Decoration& decoration, const Instruction& built_in_inst,
520
      const Instruction& referenced_inst,
521
      const Instruction& referenced_from_inst);
522
523
  // Used for GlobalInvocationId, LocalInvocationId, NumWorkgroups, WorkgroupId.
524
  spv_result_t ValidateComputeShaderI32Vec3InputAtReference(
525
      const Decoration& decoration, const Instruction& built_in_inst,
526
      const Instruction& referenced_inst,
527
      const Instruction& referenced_from_inst);
528
529
  // Used for BaryCoord, BaryCoordNoPersp.
530
  spv_result_t ValidateFragmentShaderF32Vec3InputAtReference(
531
      const Decoration& decoration, const Instruction& built_in_inst,
532
      const Instruction& referenced_inst,
533
      const Instruction& referenced_from_inst);
534
535
  // Used for SubgroupId and NumSubgroups.
536
  spv_result_t ValidateComputeI32InputAtReference(
537
      const Decoration& decoration, const Instruction& built_in_inst,
538
      const Instruction& referenced_inst,
539
      const Instruction& referenced_from_inst);
540
541
  spv_result_t ValidateNVSMOrARMCoreBuiltinsAtReference(
542
      const Decoration& decoration, const Instruction& built_in_inst,
543
      const Instruction& referenced_inst,
544
      const Instruction& referenced_from_inst);
545
546
  spv_result_t ValidatePrimitiveShadingRateAtReference(
547
      const Decoration& decoration, const Instruction& built_in_inst,
548
      const Instruction& referenced_inst,
549
      const Instruction& referenced_from_inst);
550
551
  spv_result_t ValidateShadingRateAtReference(
552
      const Decoration& decoration, const Instruction& built_in_inst,
553
      const Instruction& referenced_inst,
554
      const Instruction& referenced_from_inst);
555
556
  spv_result_t ValidateRayTracingBuiltinsAtReference(
557
      const Decoration& decoration, const Instruction& built_in_inst,
558
      const Instruction& referenced_inst,
559
      const Instruction& referenced_from_inst);
560
561
  spv_result_t ValidateMeshShadingEXTBuiltinsAtReference(
562
      const Decoration& decoration, const Instruction& built_in_inst,
563
      const Instruction& referenced_inst,
564
      const Instruction& referenced_from_inst);
565
566
  // Validates that |built_in_inst| is not (even indirectly) referenced from
567
  // within a function which can be called with |execution_model|.
568
  //
569
  // |vuid| - Vulkan ID for the error, or a negative value if none.
570
  // |comment| - text explaining why the restriction was imposed.
571
  // |decoration| - BuiltIn decoration which causes the restriction.
572
  // |referenced_inst| - instruction which is dependent on |built_in_inst| and
573
  //                     defines the id which was referenced.
574
  // |referenced_from_inst| - instruction which references id defined by
575
  //                          |referenced_inst| from within a function.
576
  spv_result_t ValidateNotCalledWithExecutionModel(
577
      int vuid, const char* comment, spv::ExecutionModel execution_model,
578
      const Decoration& decoration, const Instruction& built_in_inst,
579
      const Instruction& referenced_inst,
580
      const Instruction& referenced_from_inst);
581
582
  // The following section contains functions which check that the decorated
583
  // variable has the type specified in the function name. |diag| would be
584
  // called with a corresponding error message, if validation is not successful.
585
  spv_result_t ValidateBool(
586
      const Decoration& decoration, const Instruction& inst,
587
      const std::function<spv_result_t(const std::string& message)>& diag);
588
  spv_result_t ValidateI(
589
      const Decoration& decoration, const Instruction& inst,
590
      const std::function<spv_result_t(const std::string& message)>& diag);
591
  spv_result_t ValidateI32(
592
      const Decoration& decoration, const Instruction& inst,
593
      const std::function<spv_result_t(const std::string& message)>& diag);
594
  spv_result_t ValidateI32Vec(
595
      const Decoration& decoration, const Instruction& inst,
596
      uint32_t num_components,
597
      const std::function<spv_result_t(const std::string& message)>& diag);
598
  spv_result_t ValidateI32Arr(
599
      const Decoration& decoration, const Instruction& inst,
600
      const std::function<spv_result_t(const std::string& message)>& diag);
601
  spv_result_t ValidateArrayedI32Vec(
602
      const Decoration& decoration, const Instruction& inst,
603
      uint32_t num_components,
604
      const std::function<spv_result_t(const std::string& message)>& diag);
605
  spv_result_t ValidateOptionalArrayedI32(
606
      const Decoration& decoration, const Instruction& inst,
607
      const std::function<spv_result_t(const std::string& message)>& diag);
608
  spv_result_t ValidateI32Helper(
609
      const Decoration& decoration, const Instruction& inst,
610
      const std::function<spv_result_t(const std::string& message)>& diag,
611
      uint32_t underlying_type);
612
  spv_result_t ValidateF32(
613
      const Decoration& decoration, const Instruction& inst,
614
      const std::function<spv_result_t(const std::string& message)>& diag);
615
  spv_result_t ValidateOptionalArrayedF32(
616
      const Decoration& decoration, const Instruction& inst,
617
      const std::function<spv_result_t(const std::string& message)>& diag);
618
  spv_result_t ValidateF32Helper(
619
      const Decoration& decoration, const Instruction& inst,
620
      const std::function<spv_result_t(const std::string& message)>& diag,
621
      uint32_t underlying_type);
622
  spv_result_t ValidateF32Vec(
623
      const Decoration& decoration, const Instruction& inst,
624
      uint32_t num_components,
625
      const std::function<spv_result_t(const std::string& message)>& diag);
626
  spv_result_t ValidateOptionalArrayedF32Vec(
627
      const Decoration& decoration, const Instruction& inst,
628
      uint32_t num_components,
629
      const std::function<spv_result_t(const std::string& message)>& diag);
630
  spv_result_t ValidateF32VecHelper(
631
      const Decoration& decoration, const Instruction& inst,
632
      uint32_t num_components,
633
      const std::function<spv_result_t(const std::string& message)>& diag,
634
      uint32_t underlying_type);
635
  // If |num_components| is zero, the number of components is not checked.
636
  spv_result_t ValidateF32Arr(
637
      const Decoration& decoration, const Instruction& inst,
638
      uint32_t num_components,
639
      const std::function<spv_result_t(const std::string& message)>& diag);
640
  spv_result_t ValidateOptionalArrayedF32Arr(
641
      const Decoration& decoration, const Instruction& inst,
642
      uint32_t num_components,
643
      const std::function<spv_result_t(const std::string& message)>& diag);
644
  spv_result_t ValidateF32ArrHelper(
645
      const Decoration& decoration, const Instruction& inst,
646
      uint32_t num_components,
647
      const std::function<spv_result_t(const std::string& message)>& diag,
648
      uint32_t underlying_type);
649
  spv_result_t ValidateF32Mat(
650
      const Decoration& decoration, const Instruction& inst,
651
      uint32_t req_num_rows, uint32_t req_num_columns,
652
      const std::function<spv_result_t(const std::string& message)>& diag);
653
654
  // Generates strings like "Member #0 of struct ID <2>".
655
  std::string GetDefinitionDesc(const Decoration& decoration,
656
                                const Instruction& inst) const;
657
658
  // Generates strings like "ID <51> (OpTypePointer) is referencing ID <2>
659
  // (OpTypeStruct) which is decorated with BuiltIn Position".
660
  std::string GetReferenceDesc(
661
      const Decoration& decoration, const Instruction& built_in_inst,
662
      const Instruction& referenced_inst,
663
      const Instruction& referenced_from_inst,
664
      spv::ExecutionModel execution_model = spv::ExecutionModel::Max) const;
665
666
  // Generates strings like "ID <51> (OpTypePointer) uses storage class
667
  // UniformConstant".
668
  std::string GetStorageClassDesc(const Instruction& inst) const;
669
670
  // Updates inner working of the class. Is called sequentially for every
671
  // instruction.
672
  void Update(const Instruction& inst);
673
674
  ValidationState_t& _;
675
676
  // Mapping id -> list of rules which validate instruction referencing the
677
  // id. Rules can create new rules and add them to this container.
678
  // Using std::map, and not std::unordered_map to avoid iterator invalidation
679
  // during rehashing.
680
  std::map<uint32_t, std::list<std::function<spv_result_t(const Instruction&)>>>
681
      id_to_at_reference_checks_;
682
683
  // Id of the function we are currently inside. 0 if not inside a function.
684
  uint32_t function_id_ = 0;
685
686
  // Entry points which can (indirectly) call the current function.
687
  // The pointer either points to a vector inside to function_to_entry_points_
688
  // or to no_entry_points_. The pointer is guaranteed to never be null.
689
  const std::vector<uint32_t> no_entry_points;
690
  const std::vector<uint32_t>* entry_points_ = &no_entry_points;
691
692
  // Execution models with which the current function can be called.
693
  std::set<spv::ExecutionModel> execution_models_;
694
};
695
696
0
void BuiltInsValidator::Update(const Instruction& inst) {
697
0
  const spv::Op opcode = inst.opcode();
698
0
  if (opcode == spv::Op::OpFunction) {
699
    // Entering a function.
700
0
    assert(function_id_ == 0);
701
0
    function_id_ = inst.id();
702
0
    execution_models_.clear();
703
0
    entry_points_ = &_.FunctionEntryPoints(function_id_);
704
    // Collect execution models from all entry points from which the current
705
    // function can be called.
706
0
    for (const uint32_t entry_point : *entry_points_) {
707
0
      if (const auto* models = _.GetExecutionModels(entry_point)) {
708
0
        execution_models_.insert(models->begin(), models->end());
709
0
      }
710
0
    }
711
0
  }
712
713
0
  if (opcode == spv::Op::OpFunctionEnd) {
714
    // Exiting a function.
715
0
    assert(function_id_ != 0);
716
0
    function_id_ = 0;
717
0
    entry_points_ = &no_entry_points;
718
0
    execution_models_.clear();
719
0
  }
720
0
}
721
722
std::string BuiltInsValidator::GetDefinitionDesc(
723
0
    const Decoration& decoration, const Instruction& inst) const {
724
0
  std::ostringstream ss;
725
0
  if (decoration.struct_member_index() != Decoration::kInvalidMember) {
726
0
    assert(inst.opcode() == spv::Op::OpTypeStruct);
727
0
    ss << "Member #" << decoration.struct_member_index();
728
0
    ss << " of struct ID <" << inst.id() << ">";
729
0
  } else {
730
0
    ss << GetIdDesc(inst);
731
0
  }
732
0
  return ss.str();
733
0
}
734
735
std::string BuiltInsValidator::GetReferenceDesc(
736
    const Decoration& decoration, const Instruction& built_in_inst,
737
    const Instruction& referenced_inst, const Instruction& referenced_from_inst,
738
0
    spv::ExecutionModel execution_model) const {
739
0
  std::ostringstream ss;
740
0
  ss << GetIdDesc(referenced_from_inst) << " is referencing "
741
0
     << GetIdDesc(referenced_inst);
742
0
  if (built_in_inst.id() != referenced_inst.id()) {
743
0
    ss << " which is dependent on " << GetIdDesc(built_in_inst);
744
0
  }
745
746
0
  ss << " which is decorated with BuiltIn ";
747
0
  ss << _.grammar().lookupOperandName(SPV_OPERAND_TYPE_BUILT_IN,
748
0
                                      (uint32_t)decoration.builtin());
749
0
  if (function_id_) {
750
0
    ss << " in function <" << function_id_ << ">";
751
0
    if (execution_model != spv::ExecutionModel::Max) {
752
0
      ss << " called with execution model ";
753
0
      ss << _.grammar().lookupOperandName(SPV_OPERAND_TYPE_EXECUTION_MODEL,
754
0
                                          uint32_t(execution_model));
755
0
    }
756
0
  }
757
0
  ss << ".";
758
0
  return ss.str();
759
0
}
760
761
std::string BuiltInsValidator::GetStorageClassDesc(
762
0
    const Instruction& inst) const {
763
0
  std::ostringstream ss;
764
0
  ss << GetIdDesc(inst) << " uses storage class ";
765
0
  ss << _.grammar().lookupOperandName(SPV_OPERAND_TYPE_STORAGE_CLASS,
766
0
                                      uint32_t(GetStorageClass(inst)));
767
0
  ss << ".";
768
0
  return ss.str();
769
0
}
770
771
spv_result_t BuiltInsValidator::ValidateBool(
772
    const Decoration& decoration, const Instruction& inst,
773
0
    const std::function<spv_result_t(const std::string& message)>& diag) {
774
0
  uint32_t underlying_type = 0;
775
0
  if (spv_result_t error =
776
0
          GetUnderlyingType(_, decoration, inst, &underlying_type)) {
777
0
    return error;
778
0
  }
779
780
0
  if (!_.IsBoolScalarType(underlying_type)) {
781
0
    return diag(GetDefinitionDesc(decoration, inst) + " is not a bool scalar.");
782
0
  }
783
784
0
  return SPV_SUCCESS;
785
0
}
786
787
spv_result_t BuiltInsValidator::ValidateI(
788
    const Decoration& decoration, const Instruction& inst,
789
0
    const std::function<spv_result_t(const std::string& message)>& diag) {
790
0
  uint32_t underlying_type = 0;
791
0
  if (spv_result_t error =
792
0
          GetUnderlyingType(_, decoration, inst, &underlying_type)) {
793
0
    return error;
794
0
  }
795
796
0
  if (!_.IsIntScalarType(underlying_type)) {
797
0
    return diag(GetDefinitionDesc(decoration, inst) + " is not an int scalar.");
798
0
  }
799
800
0
  return SPV_SUCCESS;
801
0
}
802
803
spv_result_t BuiltInsValidator::ValidateI32(
804
    const Decoration& decoration, const Instruction& inst,
805
0
    const std::function<spv_result_t(const std::string& message)>& diag) {
806
0
  uint32_t underlying_type = 0;
807
0
  if (spv_result_t error =
808
0
          GetUnderlyingType(_, decoration, inst, &underlying_type)) {
809
0
    return error;
810
0
  }
811
812
0
  return ValidateI32Helper(decoration, inst, diag, underlying_type);
813
0
}
814
815
spv_result_t BuiltInsValidator::ValidateOptionalArrayedI32(
816
    const Decoration& decoration, const Instruction& inst,
817
0
    const std::function<spv_result_t(const std::string& message)>& diag) {
818
0
  uint32_t underlying_type = 0;
819
0
  if (spv_result_t error =
820
0
          GetUnderlyingType(_, decoration, inst, &underlying_type)) {
821
0
    return error;
822
0
  }
823
824
  // Strip the array, if present.
825
0
  if (_.GetIdOpcode(underlying_type) == spv::Op::OpTypeArray) {
826
0
    underlying_type = _.FindDef(underlying_type)->word(2u);
827
0
  }
828
829
0
  return ValidateI32Helper(decoration, inst, diag, underlying_type);
830
0
}
831
832
spv_result_t BuiltInsValidator::ValidateI32Helper(
833
    const Decoration& decoration, const Instruction& inst,
834
    const std::function<spv_result_t(const std::string& message)>& diag,
835
0
    uint32_t underlying_type) {
836
0
  if (!_.IsIntScalarType(underlying_type)) {
837
0
    return diag(GetDefinitionDesc(decoration, inst) + " is not an int scalar.");
838
0
  }
839
840
0
  const uint32_t bit_width = _.GetBitWidth(underlying_type);
841
0
  if (bit_width != 32) {
842
0
    std::ostringstream ss;
843
0
    ss << GetDefinitionDesc(decoration, inst) << " has bit width " << bit_width
844
0
       << ".";
845
0
    return diag(ss.str());
846
0
  }
847
848
0
  return SPV_SUCCESS;
849
0
}
850
851
spv_result_t BuiltInsValidator::ValidateOptionalArrayedF32(
852
    const Decoration& decoration, const Instruction& inst,
853
0
    const std::function<spv_result_t(const std::string& message)>& diag) {
854
0
  uint32_t underlying_type = 0;
855
0
  if (spv_result_t error =
856
0
          GetUnderlyingType(_, decoration, inst, &underlying_type)) {
857
0
    return error;
858
0
  }
859
860
  // Strip the array, if present.
861
0
  if (_.GetIdOpcode(underlying_type) == spv::Op::OpTypeArray) {
862
0
    underlying_type = _.FindDef(underlying_type)->word(2u);
863
0
  }
864
865
0
  return ValidateF32Helper(decoration, inst, diag, underlying_type);
866
0
}
867
868
spv_result_t BuiltInsValidator::ValidateF32(
869
    const Decoration& decoration, const Instruction& inst,
870
0
    const std::function<spv_result_t(const std::string& message)>& diag) {
871
0
  uint32_t underlying_type = 0;
872
0
  if (spv_result_t error =
873
0
          GetUnderlyingType(_, decoration, inst, &underlying_type)) {
874
0
    return error;
875
0
  }
876
877
0
  return ValidateF32Helper(decoration, inst, diag, underlying_type);
878
0
}
879
880
spv_result_t BuiltInsValidator::ValidateF32Helper(
881
    const Decoration& decoration, const Instruction& inst,
882
    const std::function<spv_result_t(const std::string& message)>& diag,
883
0
    uint32_t underlying_type) {
884
0
  if (!_.IsFloatScalarType(underlying_type)) {
885
0
    return diag(GetDefinitionDesc(decoration, inst) +
886
0
                " is not a float scalar.");
887
0
  }
888
889
0
  const uint32_t bit_width = _.GetBitWidth(underlying_type);
890
0
  if (bit_width != 32) {
891
0
    std::ostringstream ss;
892
0
    ss << GetDefinitionDesc(decoration, inst) << " has bit width " << bit_width
893
0
       << ".";
894
0
    return diag(ss.str());
895
0
  }
896
897
0
  return SPV_SUCCESS;
898
0
}
899
900
spv_result_t BuiltInsValidator::ValidateI32Vec(
901
    const Decoration& decoration, const Instruction& inst,
902
    uint32_t num_components,
903
0
    const std::function<spv_result_t(const std::string& message)>& diag) {
904
0
  uint32_t underlying_type = 0;
905
0
  if (spv_result_t error =
906
0
          GetUnderlyingType(_, decoration, inst, &underlying_type)) {
907
0
    return error;
908
0
  }
909
910
0
  if (!_.IsIntVectorType(underlying_type)) {
911
0
    return diag(GetDefinitionDesc(decoration, inst) + " is not an int vector.");
912
0
  }
913
914
0
  const uint32_t actual_num_components = _.GetDimension(underlying_type);
915
0
  if (_.GetDimension(underlying_type) != num_components) {
916
0
    std::ostringstream ss;
917
0
    ss << GetDefinitionDesc(decoration, inst) << " has "
918
0
       << actual_num_components << " components.";
919
0
    return diag(ss.str());
920
0
  }
921
922
0
  const uint32_t bit_width = _.GetBitWidth(underlying_type);
923
0
  if (bit_width != 32) {
924
0
    std::ostringstream ss;
925
0
    ss << GetDefinitionDesc(decoration, inst)
926
0
       << " has components with bit width " << bit_width << ".";
927
0
    return diag(ss.str());
928
0
  }
929
930
0
  return SPV_SUCCESS;
931
0
}
932
933
spv_result_t BuiltInsValidator::ValidateArrayedI32Vec(
934
    const Decoration& decoration, const Instruction& inst,
935
    uint32_t num_components,
936
0
    const std::function<spv_result_t(const std::string& message)>& diag) {
937
0
  uint32_t underlying_type = 0;
938
0
  if (spv_result_t error =
939
0
          GetUnderlyingType(_, decoration, inst, &underlying_type)) {
940
0
    return error;
941
0
  }
942
943
0
  const Instruction* const type_inst = _.FindDef(underlying_type);
944
0
  if (type_inst->opcode() != spv::Op::OpTypeArray) {
945
0
    return diag(GetDefinitionDesc(decoration, inst) + " is not an array.");
946
0
  }
947
948
0
  const uint32_t component_type = type_inst->word(2);
949
0
  if (!_.IsIntVectorType(component_type)) {
950
0
    return diag(GetDefinitionDesc(decoration, inst) + " is not an int vector.");
951
0
  }
952
953
0
  const uint32_t actual_num_components = _.GetDimension(component_type);
954
0
  if (_.GetDimension(component_type) != num_components) {
955
0
    std::ostringstream ss;
956
0
    ss << GetDefinitionDesc(decoration, inst) << " has "
957
0
       << actual_num_components << " components.";
958
0
    return diag(ss.str());
959
0
  }
960
961
0
  const uint32_t bit_width = _.GetBitWidth(component_type);
962
0
  if (bit_width != 32) {
963
0
    std::ostringstream ss;
964
0
    ss << GetDefinitionDesc(decoration, inst)
965
0
       << " has components with bit width " << bit_width << ".";
966
0
    return diag(ss.str());
967
0
  }
968
969
0
  return SPV_SUCCESS;
970
0
}
971
972
spv_result_t BuiltInsValidator::ValidateOptionalArrayedF32Vec(
973
    const Decoration& decoration, const Instruction& inst,
974
    uint32_t num_components,
975
0
    const std::function<spv_result_t(const std::string& message)>& diag) {
976
0
  uint32_t underlying_type = 0;
977
0
  if (spv_result_t error =
978
0
          GetUnderlyingType(_, decoration, inst, &underlying_type)) {
979
0
    return error;
980
0
  }
981
982
  // Strip the array, if present.
983
0
  if (_.GetIdOpcode(underlying_type) == spv::Op::OpTypeArray) {
984
0
    underlying_type = _.FindDef(underlying_type)->word(2u);
985
0
  }
986
987
0
  return ValidateF32VecHelper(decoration, inst, num_components, diag,
988
0
                              underlying_type);
989
0
}
990
991
spv_result_t BuiltInsValidator::ValidateF32Vec(
992
    const Decoration& decoration, const Instruction& inst,
993
    uint32_t num_components,
994
0
    const std::function<spv_result_t(const std::string& message)>& diag) {
995
0
  uint32_t underlying_type = 0;
996
0
  if (spv_result_t error =
997
0
          GetUnderlyingType(_, decoration, inst, &underlying_type)) {
998
0
    return error;
999
0
  }
1000
1001
0
  return ValidateF32VecHelper(decoration, inst, num_components, diag,
1002
0
                              underlying_type);
1003
0
}
1004
1005
spv_result_t BuiltInsValidator::ValidateF32VecHelper(
1006
    const Decoration& decoration, const Instruction& inst,
1007
    uint32_t num_components,
1008
    const std::function<spv_result_t(const std::string& message)>& diag,
1009
0
    uint32_t underlying_type) {
1010
0
  if (!_.IsFloatVectorType(underlying_type)) {
1011
0
    return diag(GetDefinitionDesc(decoration, inst) +
1012
0
                " is not a float vector.");
1013
0
  }
1014
1015
0
  const uint32_t actual_num_components = _.GetDimension(underlying_type);
1016
0
  if (_.GetDimension(underlying_type) != num_components) {
1017
0
    std::ostringstream ss;
1018
0
    ss << GetDefinitionDesc(decoration, inst) << " has "
1019
0
       << actual_num_components << " components.";
1020
0
    return diag(ss.str());
1021
0
  }
1022
1023
0
  const uint32_t bit_width = _.GetBitWidth(underlying_type);
1024
0
  if (bit_width != 32) {
1025
0
    std::ostringstream ss;
1026
0
    ss << GetDefinitionDesc(decoration, inst)
1027
0
       << " has components with bit width " << bit_width << ".";
1028
0
    return diag(ss.str());
1029
0
  }
1030
1031
0
  return SPV_SUCCESS;
1032
0
}
1033
1034
spv_result_t BuiltInsValidator::ValidateI32Arr(
1035
    const Decoration& decoration, const Instruction& inst,
1036
0
    const std::function<spv_result_t(const std::string& message)>& diag) {
1037
0
  uint32_t underlying_type = 0;
1038
0
  if (spv_result_t error =
1039
0
          GetUnderlyingType(_, decoration, inst, &underlying_type)) {
1040
0
    return error;
1041
0
  }
1042
1043
0
  const Instruction* const type_inst = _.FindDef(underlying_type);
1044
0
  if (type_inst->opcode() != spv::Op::OpTypeArray) {
1045
0
    return diag(GetDefinitionDesc(decoration, inst) + " is not an array.");
1046
0
  }
1047
1048
0
  const uint32_t component_type = type_inst->word(2);
1049
0
  if (!_.IsIntScalarType(component_type)) {
1050
0
    return diag(GetDefinitionDesc(decoration, inst) +
1051
0
                " components are not int scalar.");
1052
0
  }
1053
1054
0
  const uint32_t bit_width = _.GetBitWidth(component_type);
1055
0
  if (bit_width != 32) {
1056
0
    std::ostringstream ss;
1057
0
    ss << GetDefinitionDesc(decoration, inst)
1058
0
       << " has components with bit width " << bit_width << ".";
1059
0
    return diag(ss.str());
1060
0
  }
1061
1062
0
  return SPV_SUCCESS;
1063
0
}
1064
1065
spv_result_t BuiltInsValidator::ValidateF32Arr(
1066
    const Decoration& decoration, const Instruction& inst,
1067
    uint32_t num_components,
1068
0
    const std::function<spv_result_t(const std::string& message)>& diag) {
1069
0
  uint32_t underlying_type = 0;
1070
0
  if (spv_result_t error =
1071
0
          GetUnderlyingType(_, decoration, inst, &underlying_type)) {
1072
0
    return error;
1073
0
  }
1074
1075
0
  return ValidateF32ArrHelper(decoration, inst, num_components, diag,
1076
0
                              underlying_type);
1077
0
}
1078
1079
spv_result_t BuiltInsValidator::ValidateOptionalArrayedF32Arr(
1080
    const Decoration& decoration, const Instruction& inst,
1081
    uint32_t num_components,
1082
0
    const std::function<spv_result_t(const std::string& message)>& diag) {
1083
0
  uint32_t underlying_type = 0;
1084
0
  if (spv_result_t error =
1085
0
          GetUnderlyingType(_, decoration, inst, &underlying_type)) {
1086
0
    return error;
1087
0
  }
1088
1089
  // Strip an extra layer of arraying if present.
1090
0
  if (_.GetIdOpcode(underlying_type) == spv::Op::OpTypeArray) {
1091
0
    uint32_t subtype = _.FindDef(underlying_type)->word(2u);
1092
0
    if (_.GetIdOpcode(subtype) == spv::Op::OpTypeArray) {
1093
0
      underlying_type = subtype;
1094
0
    }
1095
0
  }
1096
1097
0
  return ValidateF32ArrHelper(decoration, inst, num_components, diag,
1098
0
                              underlying_type);
1099
0
}
1100
1101
spv_result_t BuiltInsValidator::ValidateF32ArrHelper(
1102
    const Decoration& decoration, const Instruction& inst,
1103
    uint32_t num_components,
1104
    const std::function<spv_result_t(const std::string& message)>& diag,
1105
0
    uint32_t underlying_type) {
1106
0
  const Instruction* const type_inst = _.FindDef(underlying_type);
1107
0
  if (type_inst->opcode() != spv::Op::OpTypeArray) {
1108
0
    return diag(GetDefinitionDesc(decoration, inst) + " is not an array.");
1109
0
  }
1110
1111
0
  const uint32_t component_type = type_inst->word(2);
1112
0
  if (!_.IsFloatScalarType(component_type)) {
1113
0
    return diag(GetDefinitionDesc(decoration, inst) +
1114
0
                " components are not float scalar.");
1115
0
  }
1116
1117
0
  const uint32_t bit_width = _.GetBitWidth(component_type);
1118
0
  if (bit_width != 32) {
1119
0
    std::ostringstream ss;
1120
0
    ss << GetDefinitionDesc(decoration, inst)
1121
0
       << " has components with bit width " << bit_width << ".";
1122
0
    return diag(ss.str());
1123
0
  }
1124
1125
0
  if (num_components != 0) {
1126
0
    uint64_t actual_num_components = 0;
1127
0
    if (!_.EvalConstantValUint64(type_inst->word(3), &actual_num_components)) {
1128
0
      assert(0 && "Array type definition is corrupt");
1129
0
    }
1130
0
    if (actual_num_components != num_components) {
1131
0
      std::ostringstream ss;
1132
0
      ss << GetDefinitionDesc(decoration, inst) << " has "
1133
0
         << actual_num_components << " components.";
1134
0
      return diag(ss.str());
1135
0
    }
1136
0
  }
1137
1138
0
  return SPV_SUCCESS;
1139
0
}
1140
1141
spv_result_t BuiltInsValidator::ValidateF32Mat(
1142
    const Decoration& decoration, const Instruction& inst,
1143
    uint32_t req_num_rows, uint32_t req_num_columns,
1144
0
    const std::function<spv_result_t(const std::string& message)>& diag) {
1145
0
  uint32_t underlying_type = 0;
1146
0
  uint32_t num_rows = 0;
1147
0
  uint32_t num_cols = 0;
1148
0
  uint32_t col_type = 0;
1149
0
  uint32_t component_type = 0;
1150
0
  if (spv_result_t error =
1151
0
          GetUnderlyingType(_, decoration, inst, &underlying_type)) {
1152
0
    return error;
1153
0
  }
1154
0
  if (!_.GetMatrixTypeInfo(underlying_type, &num_rows, &num_cols, &col_type,
1155
0
                           &component_type) ||
1156
0
      num_rows != req_num_rows || num_cols != req_num_columns) {
1157
0
    std::ostringstream ss;
1158
0
    ss << GetDefinitionDesc(decoration, inst) << " has columns " << num_cols
1159
0
       << " and rows " << num_rows << " not equal to expected "
1160
0
       << req_num_columns << "x" << req_num_rows << ".";
1161
0
    return diag(ss.str());
1162
0
  }
1163
1164
0
  return ValidateF32VecHelper(decoration, inst, req_num_rows, diag, col_type);
1165
0
}
1166
1167
spv_result_t BuiltInsValidator::ValidateNotCalledWithExecutionModel(
1168
    int vuid, const char* comment, spv::ExecutionModel execution_model,
1169
    const Decoration& decoration, const Instruction& built_in_inst,
1170
    const Instruction& referenced_inst,
1171
0
    const Instruction& referenced_from_inst) {
1172
0
  if (function_id_) {
1173
0
    if (execution_models_.count(execution_model)) {
1174
0
      const char* execution_model_str = _.grammar().lookupOperandName(
1175
0
          SPV_OPERAND_TYPE_EXECUTION_MODEL, uint32_t(execution_model));
1176
0
      const char* built_in_str = _.grammar().lookupOperandName(
1177
0
          SPV_OPERAND_TYPE_BUILT_IN, (uint32_t)decoration.builtin());
1178
0
      return _.diag(SPV_ERROR_INVALID_DATA, &referenced_from_inst)
1179
0
             << (vuid < 0 ? std::string("") : _.VkErrorID(vuid)) << comment
1180
0
             << " " << GetIdDesc(referenced_inst) << " depends on "
1181
0
             << GetIdDesc(built_in_inst) << " which is decorated with BuiltIn "
1182
0
             << built_in_str << "."
1183
0
             << " Id <" << referenced_inst.id() << "> is later referenced by "
1184
0
             << GetIdDesc(referenced_from_inst) << " in function <"
1185
0
             << function_id_ << "> which is called with execution model "
1186
0
             << execution_model_str << ".";
1187
0
    }
1188
0
  } else {
1189
    // Propagate this rule to all dependant ids in the global scope.
1190
0
    id_to_at_reference_checks_[referenced_from_inst.id()].push_back(
1191
0
        std::bind(&BuiltInsValidator::ValidateNotCalledWithExecutionModel, this,
1192
0
                  vuid, comment, execution_model, decoration, built_in_inst,
1193
0
                  referenced_from_inst, std::placeholders::_1));
1194
0
  }
1195
0
  return SPV_SUCCESS;
1196
0
}
1197
1198
spv_result_t BuiltInsValidator::ValidateClipOrCullDistanceAtDefinition(
1199
0
    const Decoration& decoration, const Instruction& inst) {
1200
  // Seed at reference checks with this built-in.
1201
0
  return ValidateClipOrCullDistanceAtReference(decoration, inst, inst, inst);
1202
0
}
1203
1204
spv_result_t BuiltInsValidator::ValidateClipOrCullDistanceAtReference(
1205
    const Decoration& decoration, const Instruction& built_in_inst,
1206
    const Instruction& referenced_inst,
1207
0
    const Instruction& referenced_from_inst) {
1208
0
  uint32_t operand = (uint32_t)decoration.builtin();
1209
0
  if (spvIsVulkanEnv(_.context()->target_env)) {
1210
0
    const spv::StorageClass storage_class = GetStorageClass(referenced_from_inst);
1211
0
    if (storage_class != spv::StorageClass::Max &&
1212
0
        storage_class != spv::StorageClass::Input &&
1213
0
        storage_class != spv::StorageClass::Output) {
1214
0
      uint32_t vuid =
1215
0
          (decoration.builtin() == spv::BuiltIn::ClipDistance) ? 4190 : 4199;
1216
0
      return _.diag(SPV_ERROR_INVALID_DATA, &referenced_from_inst)
1217
0
             << _.VkErrorID(vuid) << "Vulkan spec allows BuiltIn "
1218
0
             << _.grammar().lookupOperandName(SPV_OPERAND_TYPE_BUILT_IN,
1219
0
                                              operand)
1220
0
             << " to be only used for variables with Input or Output storage "
1221
0
                "class. "
1222
0
             << GetReferenceDesc(decoration, built_in_inst, referenced_inst,
1223
0
                                 referenced_from_inst)
1224
0
             << " " << GetStorageClassDesc(referenced_from_inst);
1225
0
    }
1226
1227
0
    if (storage_class == spv::StorageClass::Input) {
1228
0
      assert(function_id_ == 0);
1229
0
      uint32_t vuid =
1230
0
          (decoration.builtin() == spv::BuiltIn::ClipDistance) ? 4188 : 4197;
1231
0
      id_to_at_reference_checks_[referenced_from_inst.id()].push_back(std::bind(
1232
0
          &BuiltInsValidator::ValidateNotCalledWithExecutionModel, this, vuid,
1233
0
          "Vulkan spec doesn't allow BuiltIn ClipDistance/CullDistance to be "
1234
0
          "used for variables with Input storage class if execution model is "
1235
0
          "Vertex.",
1236
0
          spv::ExecutionModel::Vertex, decoration, built_in_inst,
1237
0
          referenced_from_inst, std::placeholders::_1));
1238
0
      id_to_at_reference_checks_[referenced_from_inst.id()].push_back(std::bind(
1239
0
          &BuiltInsValidator::ValidateNotCalledWithExecutionModel, this, vuid,
1240
0
          "Vulkan spec doesn't allow BuiltIn ClipDistance/CullDistance to be "
1241
0
          "used for variables with Input storage class if execution model is "
1242
0
          "MeshNV.",
1243
0
          spv::ExecutionModel::MeshNV, decoration, built_in_inst,
1244
0
          referenced_from_inst, std::placeholders::_1));
1245
0
      id_to_at_reference_checks_[referenced_from_inst.id()].push_back(std::bind(
1246
0
          &BuiltInsValidator::ValidateNotCalledWithExecutionModel, this, vuid,
1247
0
          "Vulkan spec doesn't allow BuiltIn ClipDistance/CullDistance to be "
1248
0
          "used for variables with Input storage class if execution model is "
1249
0
          "MeshEXT.",
1250
0
          spv::ExecutionModel::MeshEXT, decoration, built_in_inst,
1251
0
          referenced_from_inst, std::placeholders::_1));
1252
0
    }
1253
1254
0
    if (storage_class == spv::StorageClass::Output) {
1255
0
      assert(function_id_ == 0);
1256
0
      uint32_t vuid =
1257
0
          (decoration.builtin() == spv::BuiltIn::ClipDistance) ? 4189 : 4198;
1258
0
      id_to_at_reference_checks_[referenced_from_inst.id()].push_back(std::bind(
1259
0
          &BuiltInsValidator::ValidateNotCalledWithExecutionModel, this, vuid,
1260
0
          "Vulkan spec doesn't allow BuiltIn ClipDistance/CullDistance to be "
1261
0
          "used for variables with Output storage class if execution model is "
1262
0
          "Fragment.",
1263
0
          spv::ExecutionModel::Fragment, decoration, built_in_inst,
1264
0
          referenced_from_inst, std::placeholders::_1));
1265
0
    }
1266
1267
0
    for (const spv::ExecutionModel execution_model : execution_models_) {
1268
0
      switch (execution_model) {
1269
0
        case spv::ExecutionModel::Fragment:
1270
0
        case spv::ExecutionModel::Vertex: {
1271
0
          if (spv_result_t error = ValidateF32Arr(
1272
0
                  decoration, built_in_inst, /* Any number of components */ 0,
1273
0
                  [this, &decoration, &referenced_from_inst](
1274
0
                      const std::string& message) -> spv_result_t {
1275
0
                    uint32_t vuid =
1276
0
                        (decoration.builtin() == spv::BuiltIn::ClipDistance)
1277
0
                            ? 4191
1278
0
                            : 4200;
1279
0
                    return _.diag(SPV_ERROR_INVALID_DATA, &referenced_from_inst)
1280
0
                           << _.VkErrorID(vuid)
1281
0
                           << "According to the Vulkan spec BuiltIn "
1282
0
                           << _.grammar().lookupOperandName(
1283
0
                                  SPV_OPERAND_TYPE_BUILT_IN,
1284
0
                                  (uint32_t)decoration.builtin())
1285
0
                           << " variable needs to be a 32-bit float array. "
1286
0
                           << message;
1287
0
                  })) {
1288
0
            return error;
1289
0
          }
1290
0
          break;
1291
0
        }
1292
0
        case spv::ExecutionModel::TessellationControl:
1293
0
        case spv::ExecutionModel::TessellationEvaluation:
1294
0
        case spv::ExecutionModel::Geometry:
1295
0
        case spv::ExecutionModel::MeshNV:
1296
0
        case spv::ExecutionModel::MeshEXT: {
1297
0
          if (decoration.struct_member_index() != Decoration::kInvalidMember) {
1298
            // The outer level of array is applied on the variable.
1299
0
            if (spv_result_t error = ValidateF32Arr(
1300
0
                    decoration, built_in_inst, /* Any number of components */ 0,
1301
0
                    [this, &decoration, &referenced_from_inst](
1302
0
                        const std::string& message) -> spv_result_t {
1303
0
                      uint32_t vuid =
1304
0
                          (decoration.builtin() == spv::BuiltIn::ClipDistance)
1305
0
                              ? 4191
1306
0
                              : 4200;
1307
0
                      return _.diag(SPV_ERROR_INVALID_DATA,
1308
0
                                    &referenced_from_inst)
1309
0
                             << _.VkErrorID(vuid)
1310
0
                             << "According to the Vulkan spec BuiltIn "
1311
0
                             << _.grammar().lookupOperandName(
1312
0
                                    SPV_OPERAND_TYPE_BUILT_IN,
1313
0
                                    (uint32_t)decoration.builtin())
1314
0
                             << " variable needs to be a 32-bit float array. "
1315
0
                             << message;
1316
0
                    })) {
1317
0
              return error;
1318
0
            }
1319
0
          } else {
1320
0
            if (spv_result_t error = ValidateOptionalArrayedF32Arr(
1321
0
                    decoration, built_in_inst, /* Any number of components */ 0,
1322
0
                    [this, &decoration, &referenced_from_inst](
1323
0
                        const std::string& message) -> spv_result_t {
1324
0
                      uint32_t vuid =
1325
0
                          (decoration.builtin() == spv::BuiltIn::ClipDistance)
1326
0
                              ? 4191
1327
0
                              : 4200;
1328
0
                      return _.diag(SPV_ERROR_INVALID_DATA,
1329
0
                                    &referenced_from_inst)
1330
0
                             << _.VkErrorID(vuid)
1331
0
                             << "According to the Vulkan spec BuiltIn "
1332
0
                             << _.grammar().lookupOperandName(
1333
0
                                    SPV_OPERAND_TYPE_BUILT_IN,
1334
0
                                    (uint32_t)decoration.builtin())
1335
0
                             << " variable needs to be a 32-bit float array. "
1336
0
                             << message;
1337
0
                    })) {
1338
0
              return error;
1339
0
            }
1340
0
          }
1341
0
          break;
1342
0
        }
1343
1344
0
        default: {
1345
0
          uint32_t vuid = (decoration.builtin() == spv::BuiltIn::ClipDistance)
1346
0
                              ? 4187
1347
0
                              : 4196;
1348
0
          return _.diag(SPV_ERROR_INVALID_DATA, &referenced_from_inst)
1349
0
                 << _.VkErrorID(vuid) << "Vulkan spec allows BuiltIn "
1350
0
                 << _.grammar().lookupOperandName(SPV_OPERAND_TYPE_BUILT_IN,
1351
0
                                                  operand)
1352
0
                 << " to be used only with Fragment, Vertex, "
1353
0
                    "TessellationControl, TessellationEvaluation or Geometry "
1354
0
                    "execution models. "
1355
0
                 << GetReferenceDesc(decoration, built_in_inst, referenced_inst,
1356
0
                                     referenced_from_inst, execution_model);
1357
0
        }
1358
0
      }
1359
0
    }
1360
0
  }
1361
1362
0
  if (function_id_ == 0) {
1363
    // Propagate this rule to all dependant ids in the global scope.
1364
0
    id_to_at_reference_checks_[referenced_from_inst.id()].push_back(
1365
0
        std::bind(&BuiltInsValidator::ValidateClipOrCullDistanceAtReference,
1366
0
                  this, decoration, built_in_inst, referenced_from_inst,
1367
0
                  std::placeholders::_1));
1368
0
  }
1369
1370
0
  return SPV_SUCCESS;
1371
0
}
1372
1373
spv_result_t BuiltInsValidator::ValidateFragCoordAtDefinition(
1374
0
    const Decoration& decoration, const Instruction& inst) {
1375
0
  if (spvIsVulkanEnv(_.context()->target_env)) {
1376
0
    if (spv_result_t error = ValidateF32Vec(
1377
0
            decoration, inst, 4,
1378
0
            [this, &inst](const std::string& message) -> spv_result_t {
1379
0
              return _.diag(SPV_ERROR_INVALID_DATA, &inst)
1380
0
                     << _.VkErrorID(4212) << "According to the "
1381
0
                     << spvLogStringForEnv(_.context()->target_env)
1382
0
                     << " spec BuiltIn FragCoord "
1383
0
                        "variable needs to be a 4-component 32-bit float "
1384
0
                        "vector. "
1385
0
                     << message;
1386
0
            })) {
1387
0
      return error;
1388
0
    }
1389
0
  }
1390
1391
  // Seed at reference checks with this built-in.
1392
0
  return ValidateFragCoordAtReference(decoration, inst, inst, inst);
1393
0
}
1394
1395
spv_result_t BuiltInsValidator::ValidateFragCoordAtReference(
1396
    const Decoration& decoration, const Instruction& built_in_inst,
1397
    const Instruction& referenced_inst,
1398
0
    const Instruction& referenced_from_inst) {
1399
0
  if (spvIsVulkanEnv(_.context()->target_env)) {
1400
0
    const spv::StorageClass storage_class = GetStorageClass(referenced_from_inst);
1401
0
    if (storage_class != spv::StorageClass::Max &&
1402
0
        storage_class != spv::StorageClass::Input) {
1403
0
      return _.diag(SPV_ERROR_INVALID_DATA, &referenced_from_inst)
1404
0
             << _.VkErrorID(4211) << spvLogStringForEnv(_.context()->target_env)
1405
0
             << " spec allows BuiltIn FragCoord to be only used for "
1406
0
                "variables with Input storage class. "
1407
0
             << GetReferenceDesc(decoration, built_in_inst, referenced_inst,
1408
0
                                 referenced_from_inst)
1409
0
             << " " << GetStorageClassDesc(referenced_from_inst);
1410
0
    }
1411
1412
0
    for (const spv::ExecutionModel execution_model : execution_models_) {
1413
0
      if (execution_model != spv::ExecutionModel::Fragment) {
1414
0
        return _.diag(SPV_ERROR_INVALID_DATA, &referenced_from_inst)
1415
0
               << _.VkErrorID(4210)
1416
0
               << spvLogStringForEnv(_.context()->target_env)
1417
0
               << " spec allows BuiltIn FragCoord to be used only with "
1418
0
                  "Fragment execution model. "
1419
0
               << GetReferenceDesc(decoration, built_in_inst, referenced_inst,
1420
0
                                   referenced_from_inst, execution_model);
1421
0
      }
1422
0
    }
1423
0
  }
1424
1425
0
  if (function_id_ == 0) {
1426
    // Propagate this rule to all dependant ids in the global scope.
1427
0
    id_to_at_reference_checks_[referenced_from_inst.id()].push_back(std::bind(
1428
0
        &BuiltInsValidator::ValidateFragCoordAtReference, this, decoration,
1429
0
        built_in_inst, referenced_from_inst, std::placeholders::_1));
1430
0
  }
1431
1432
0
  return SPV_SUCCESS;
1433
0
}
1434
1435
spv_result_t BuiltInsValidator::ValidateFragDepthAtDefinition(
1436
0
    const Decoration& decoration, const Instruction& inst) {
1437
0
  if (spvIsVulkanEnv(_.context()->target_env)) {
1438
0
    if (spv_result_t error = ValidateF32(
1439
0
            decoration, inst,
1440
0
            [this, &inst](const std::string& message) -> spv_result_t {
1441
0
              return _.diag(SPV_ERROR_INVALID_DATA, &inst)
1442
0
                     << _.VkErrorID(4215) << "According to the "
1443
0
                     << spvLogStringForEnv(_.context()->target_env)
1444
0
                     << " spec BuiltIn FragDepth "
1445
0
                        "variable needs to be a 32-bit float scalar. "
1446
0
                     << message;
1447
0
            })) {
1448
0
      return error;
1449
0
    }
1450
0
  }
1451
1452
  // Seed at reference checks with this built-in.
1453
0
  return ValidateFragDepthAtReference(decoration, inst, inst, inst);
1454
0
}
1455
1456
spv_result_t BuiltInsValidator::ValidateFragDepthAtReference(
1457
    const Decoration& decoration, const Instruction& built_in_inst,
1458
    const Instruction& referenced_inst,
1459
0
    const Instruction& referenced_from_inst) {
1460
0
  if (spvIsVulkanEnv(_.context()->target_env)) {
1461
0
    const spv::StorageClass storage_class = GetStorageClass(referenced_from_inst);
1462
0
    if (storage_class != spv::StorageClass::Max &&
1463
0
        storage_class != spv::StorageClass::Output) {
1464
0
      return _.diag(SPV_ERROR_INVALID_DATA, &referenced_from_inst)
1465
0
             << _.VkErrorID(4214) << spvLogStringForEnv(_.context()->target_env)
1466
0
             << " spec allows BuiltIn FragDepth to be only used for "
1467
0
                "variables with Output storage class. "
1468
0
             << GetReferenceDesc(decoration, built_in_inst, referenced_inst,
1469
0
                                 referenced_from_inst)
1470
0
             << " " << GetStorageClassDesc(referenced_from_inst);
1471
0
    }
1472
1473
0
    for (const spv::ExecutionModel execution_model : execution_models_) {
1474
0
      if (execution_model != spv::ExecutionModel::Fragment) {
1475
0
        return _.diag(SPV_ERROR_INVALID_DATA, &referenced_from_inst)
1476
0
               << _.VkErrorID(4213)
1477
0
               << spvLogStringForEnv(_.context()->target_env)
1478
0
               << " spec allows BuiltIn FragDepth to be used only with "
1479
0
                  "Fragment execution model. "
1480
0
               << GetReferenceDesc(decoration, built_in_inst, referenced_inst,
1481
0
                                   referenced_from_inst, execution_model);
1482
0
      }
1483
0
    }
1484
1485
0
    for (const uint32_t entry_point : *entry_points_) {
1486
      // Every entry point from which this function is called needs to have
1487
      // Execution Mode DepthReplacing.
1488
0
      const auto* modes = _.GetExecutionModes(entry_point);
1489
0
      if (!modes || !modes->count(spv::ExecutionMode::DepthReplacing)) {
1490
0
        return _.diag(SPV_ERROR_INVALID_DATA, &referenced_from_inst)
1491
0
               << _.VkErrorID(4216)
1492
0
               << spvLogStringForEnv(_.context()->target_env)
1493
0
               << " spec requires DepthReplacing execution mode to be "
1494
0
                  "declared when using BuiltIn FragDepth. "
1495
0
               << GetReferenceDesc(decoration, built_in_inst, referenced_inst,
1496
0
                                   referenced_from_inst);
1497
0
      }
1498
0
    }
1499
0
  }
1500
1501
0
  if (function_id_ == 0) {
1502
    // Propagate this rule to all dependant ids in the global scope.
1503
0
    id_to_at_reference_checks_[referenced_from_inst.id()].push_back(std::bind(
1504
0
        &BuiltInsValidator::ValidateFragDepthAtReference, this, decoration,
1505
0
        built_in_inst, referenced_from_inst, std::placeholders::_1));
1506
0
  }
1507
1508
0
  return SPV_SUCCESS;
1509
0
}
1510
1511
spv_result_t BuiltInsValidator::ValidateFrontFacingAtDefinition(
1512
0
    const Decoration& decoration, const Instruction& inst) {
1513
0
  if (spvIsVulkanEnv(_.context()->target_env)) {
1514
0
    if (spv_result_t error = ValidateBool(
1515
0
            decoration, inst,
1516
0
            [this, &inst](const std::string& message) -> spv_result_t {
1517
0
              return _.diag(SPV_ERROR_INVALID_DATA, &inst)
1518
0
                     << _.VkErrorID(4231) << "According to the "
1519
0
                     << spvLogStringForEnv(_.context()->target_env)
1520
0
                     << " spec BuiltIn FrontFacing "
1521
0
                        "variable needs to be a bool scalar. "
1522
0
                     << message;
1523
0
            })) {
1524
0
      return error;
1525
0
    }
1526
0
  }
1527
1528
  // Seed at reference checks with this built-in.
1529
0
  return ValidateFrontFacingAtReference(decoration, inst, inst, inst);
1530
0
}
1531
1532
spv_result_t BuiltInsValidator::ValidateFrontFacingAtReference(
1533
    const Decoration& decoration, const Instruction& built_in_inst,
1534
    const Instruction& referenced_inst,
1535
0
    const Instruction& referenced_from_inst) {
1536
0
  if (spvIsVulkanEnv(_.context()->target_env)) {
1537
0
    const spv::StorageClass storage_class = GetStorageClass(referenced_from_inst);
1538
0
    if (storage_class != spv::StorageClass::Max &&
1539
0
        storage_class != spv::StorageClass::Input) {
1540
0
      return _.diag(SPV_ERROR_INVALID_DATA, &referenced_from_inst)
1541
0
             << _.VkErrorID(4230) << spvLogStringForEnv(_.context()->target_env)
1542
0
             << " spec allows BuiltIn FrontFacing to be only used for "
1543
0
                "variables with Input storage class. "
1544
0
             << GetReferenceDesc(decoration, built_in_inst, referenced_inst,
1545
0
                                 referenced_from_inst)
1546
0
             << " " << GetStorageClassDesc(referenced_from_inst);
1547
0
    }
1548
1549
0
    for (const spv::ExecutionModel execution_model : execution_models_) {
1550
0
      if (execution_model != spv::ExecutionModel::Fragment) {
1551
0
        return _.diag(SPV_ERROR_INVALID_DATA, &referenced_from_inst)
1552
0
               << _.VkErrorID(4229)
1553
0
               << spvLogStringForEnv(_.context()->target_env)
1554
0
               << " spec allows BuiltIn FrontFacing to be used only with "
1555
0
                  "Fragment execution model. "
1556
0
               << GetReferenceDesc(decoration, built_in_inst, referenced_inst,
1557
0
                                   referenced_from_inst, execution_model);
1558
0
      }
1559
0
    }
1560
0
  }
1561
1562
0
  if (function_id_ == 0) {
1563
    // Propagate this rule to all dependant ids in the global scope.
1564
0
    id_to_at_reference_checks_[referenced_from_inst.id()].push_back(std::bind(
1565
0
        &BuiltInsValidator::ValidateFrontFacingAtReference, this, decoration,
1566
0
        built_in_inst, referenced_from_inst, std::placeholders::_1));
1567
0
  }
1568
1569
0
  return SPV_SUCCESS;
1570
0
}
1571
1572
spv_result_t BuiltInsValidator::ValidateHelperInvocationAtDefinition(
1573
0
    const Decoration& decoration, const Instruction& inst) {
1574
0
  if (spvIsVulkanEnv(_.context()->target_env)) {
1575
0
    if (spv_result_t error = ValidateBool(
1576
0
            decoration, inst,
1577
0
            [this, &inst](const std::string& message) -> spv_result_t {
1578
0
              return _.diag(SPV_ERROR_INVALID_DATA, &inst)
1579
0
                     << _.VkErrorID(4241)
1580
0
                     << "According to the Vulkan spec BuiltIn HelperInvocation "
1581
0
                        "variable needs to be a bool scalar. "
1582
0
                     << message;
1583
0
            })) {
1584
0
      return error;
1585
0
    }
1586
0
  }
1587
1588
  // Seed at reference checks with this built-in.
1589
0
  return ValidateHelperInvocationAtReference(decoration, inst, inst, inst);
1590
0
}
1591
1592
spv_result_t BuiltInsValidator::ValidateHelperInvocationAtReference(
1593
    const Decoration& decoration, const Instruction& built_in_inst,
1594
    const Instruction& referenced_inst,
1595
0
    const Instruction& referenced_from_inst) {
1596
0
  if (spvIsVulkanEnv(_.context()->target_env)) {
1597
0
    const spv::StorageClass storage_class = GetStorageClass(referenced_from_inst);
1598
0
    if (storage_class != spv::StorageClass::Max &&
1599
0
        storage_class != spv::StorageClass::Input) {
1600
0
      return _.diag(SPV_ERROR_INVALID_DATA, &referenced_from_inst)
1601
0
             << _.VkErrorID(4240)
1602
0
             << "Vulkan spec allows BuiltIn HelperInvocation to be only used "
1603
0
                "for variables with Input storage class. "
1604
0
             << GetReferenceDesc(decoration, built_in_inst, referenced_inst,
1605
0
                                 referenced_from_inst)
1606
0
             << " " << GetStorageClassDesc(referenced_from_inst);
1607
0
    }
1608
1609
0
    for (const spv::ExecutionModel execution_model : execution_models_) {
1610
0
      if (execution_model != spv::ExecutionModel::Fragment) {
1611
0
        return _.diag(SPV_ERROR_INVALID_DATA, &referenced_from_inst)
1612
0
               << _.VkErrorID(4239)
1613
0
               << "Vulkan spec allows BuiltIn HelperInvocation to be used only "
1614
0
                  "with Fragment execution model. "
1615
0
               << GetReferenceDesc(decoration, built_in_inst, referenced_inst,
1616
0
                                   referenced_from_inst, execution_model);
1617
0
      }
1618
0
    }
1619
0
  }
1620
1621
0
  if (function_id_ == 0) {
1622
    // Propagate this rule to all dependant ids in the global scope.
1623
0
    id_to_at_reference_checks_[referenced_from_inst.id()].push_back(
1624
0
        std::bind(&BuiltInsValidator::ValidateHelperInvocationAtReference, this,
1625
0
                  decoration, built_in_inst, referenced_from_inst,
1626
0
                  std::placeholders::_1));
1627
0
  }
1628
1629
0
  return SPV_SUCCESS;
1630
0
}
1631
1632
spv_result_t BuiltInsValidator::ValidateInvocationIdAtDefinition(
1633
0
    const Decoration& decoration, const Instruction& inst) {
1634
0
  if (spvIsVulkanEnv(_.context()->target_env)) {
1635
0
    if (spv_result_t error = ValidateI32(
1636
0
            decoration, inst,
1637
0
            [this, &inst](const std::string& message) -> spv_result_t {
1638
0
              return _.diag(SPV_ERROR_INVALID_DATA, &inst)
1639
0
                     << _.VkErrorID(4259)
1640
0
                     << "According to the Vulkan spec BuiltIn InvocationId "
1641
0
                        "variable needs to be a 32-bit int scalar. "
1642
0
                     << message;
1643
0
            })) {
1644
0
      return error;
1645
0
    }
1646
0
  }
1647
1648
  // Seed at reference checks with this built-in.
1649
0
  return ValidateInvocationIdAtReference(decoration, inst, inst, inst);
1650
0
}
1651
1652
spv_result_t BuiltInsValidator::ValidateInvocationIdAtReference(
1653
    const Decoration& decoration, const Instruction& built_in_inst,
1654
    const Instruction& referenced_inst,
1655
0
    const Instruction& referenced_from_inst) {
1656
0
  if (spvIsVulkanEnv(_.context()->target_env)) {
1657
0
    const spv::StorageClass storage_class = GetStorageClass(referenced_from_inst);
1658
0
    if (storage_class != spv::StorageClass::Max &&
1659
0
        storage_class != spv::StorageClass::Input) {
1660
0
      return _.diag(SPV_ERROR_INVALID_DATA, &referenced_from_inst)
1661
0
             << _.VkErrorID(4258)
1662
0
             << "Vulkan spec allows BuiltIn InvocationId to be only used for "
1663
0
                "variables with Input storage class. "
1664
0
             << GetReferenceDesc(decoration, built_in_inst, referenced_inst,
1665
0
                                 referenced_from_inst)
1666
0
             << " " << GetStorageClassDesc(referenced_from_inst);
1667
0
    }
1668
1669
0
    for (const spv::ExecutionModel execution_model : execution_models_) {
1670
0
      if (execution_model != spv::ExecutionModel::TessellationControl &&
1671
0
          execution_model != spv::ExecutionModel::Geometry) {
1672
0
        return _.diag(SPV_ERROR_INVALID_DATA, &referenced_from_inst)
1673
0
               << _.VkErrorID(4257)
1674
0
               << "Vulkan spec allows BuiltIn InvocationId to be used only "
1675
0
                  "with TessellationControl or Geometry execution models. "
1676
0
               << GetReferenceDesc(decoration, built_in_inst, referenced_inst,
1677
0
                                   referenced_from_inst, execution_model);
1678
0
      }
1679
0
    }
1680
0
  }
1681
1682
0
  if (function_id_ == 0) {
1683
    // Propagate this rule to all dependant ids in the global scope.
1684
0
    id_to_at_reference_checks_[referenced_from_inst.id()].push_back(std::bind(
1685
0
        &BuiltInsValidator::ValidateInvocationIdAtReference, this, decoration,
1686
0
        built_in_inst, referenced_from_inst, std::placeholders::_1));
1687
0
  }
1688
1689
0
  return SPV_SUCCESS;
1690
0
}
1691
1692
spv_result_t BuiltInsValidator::ValidateInstanceIndexAtDefinition(
1693
0
    const Decoration& decoration, const Instruction& inst) {
1694
0
  if (spvIsVulkanEnv(_.context()->target_env)) {
1695
0
    if (spv_result_t error = ValidateI32(
1696
0
            decoration, inst,
1697
0
            [this, &inst](const std::string& message) -> spv_result_t {
1698
0
              return _.diag(SPV_ERROR_INVALID_DATA, &inst)
1699
0
                     << _.VkErrorID(4265) << "According to the "
1700
0
                     << spvLogStringForEnv(_.context()->target_env)
1701
0
                     << " spec BuiltIn InstanceIndex "
1702
0
                        "variable needs to be a 32-bit int scalar. "
1703
0
                     << message;
1704
0
            })) {
1705
0
      return error;
1706
0
    }
1707
0
  }
1708
1709
  // Seed at reference checks with this built-in.
1710
0
  return ValidateInstanceIndexAtReference(decoration, inst, inst, inst);
1711
0
}
1712
1713
spv_result_t BuiltInsValidator::ValidateInstanceIndexAtReference(
1714
    const Decoration& decoration, const Instruction& built_in_inst,
1715
    const Instruction& referenced_inst,
1716
0
    const Instruction& referenced_from_inst) {
1717
0
  if (spvIsVulkanEnv(_.context()->target_env)) {
1718
0
    const spv::StorageClass storage_class = GetStorageClass(referenced_from_inst);
1719
0
    if (storage_class != spv::StorageClass::Max &&
1720
0
        storage_class != spv::StorageClass::Input) {
1721
0
      return _.diag(SPV_ERROR_INVALID_DATA, &referenced_from_inst)
1722
0
             << _.VkErrorID(4264) << spvLogStringForEnv(_.context()->target_env)
1723
0
             << " spec allows BuiltIn InstanceIndex to be only used for "
1724
0
                "variables with Input storage class. "
1725
0
             << GetReferenceDesc(decoration, built_in_inst, referenced_inst,
1726
0
                                 referenced_from_inst)
1727
0
             << " " << GetStorageClassDesc(referenced_from_inst);
1728
0
    }
1729
1730
0
    for (const spv::ExecutionModel execution_model : execution_models_) {
1731
0
      if (execution_model != spv::ExecutionModel::Vertex) {
1732
0
        return _.diag(SPV_ERROR_INVALID_DATA, &referenced_from_inst)
1733
0
               << _.VkErrorID(4263)
1734
0
               << spvLogStringForEnv(_.context()->target_env)
1735
0
               << " spec allows BuiltIn InstanceIndex to be used only "
1736
0
                  "with Vertex execution model. "
1737
0
               << GetReferenceDesc(decoration, built_in_inst, referenced_inst,
1738
0
                                   referenced_from_inst, execution_model);
1739
0
      }
1740
0
    }
1741
0
  }
1742
1743
0
  if (function_id_ == 0) {
1744
    // Propagate this rule to all dependant ids in the global scope.
1745
0
    id_to_at_reference_checks_[referenced_from_inst.id()].push_back(std::bind(
1746
0
        &BuiltInsValidator::ValidateInstanceIndexAtReference, this, decoration,
1747
0
        built_in_inst, referenced_from_inst, std::placeholders::_1));
1748
0
  }
1749
1750
0
  return SPV_SUCCESS;
1751
0
}
1752
1753
spv_result_t BuiltInsValidator::ValidatePatchVerticesAtDefinition(
1754
0
    const Decoration& decoration, const Instruction& inst) {
1755
0
  if (spvIsVulkanEnv(_.context()->target_env)) {
1756
0
    if (spv_result_t error = ValidateI32(
1757
0
            decoration, inst,
1758
0
            [this, &inst](const std::string& message) -> spv_result_t {
1759
0
              return _.diag(SPV_ERROR_INVALID_DATA, &inst)
1760
0
                     << _.VkErrorID(4310)
1761
0
                     << "According to the Vulkan spec BuiltIn PatchVertices "
1762
0
                        "variable needs to be a 32-bit int scalar. "
1763
0
                     << message;
1764
0
            })) {
1765
0
      return error;
1766
0
    }
1767
0
  }
1768
1769
  // Seed at reference checks with this built-in.
1770
0
  return ValidatePatchVerticesAtReference(decoration, inst, inst, inst);
1771
0
}
1772
1773
spv_result_t BuiltInsValidator::ValidatePatchVerticesAtReference(
1774
    const Decoration& decoration, const Instruction& built_in_inst,
1775
    const Instruction& referenced_inst,
1776
0
    const Instruction& referenced_from_inst) {
1777
0
  if (spvIsVulkanEnv(_.context()->target_env)) {
1778
0
    const spv::StorageClass storage_class = GetStorageClass(referenced_from_inst);
1779
0
    if (storage_class != spv::StorageClass::Max &&
1780
0
        storage_class != spv::StorageClass::Input) {
1781
0
      return _.diag(SPV_ERROR_INVALID_DATA, &referenced_from_inst)
1782
0
             << _.VkErrorID(4309)
1783
0
             << "Vulkan spec allows BuiltIn PatchVertices to be only used for "
1784
0
                "variables with Input storage class. "
1785
0
             << GetReferenceDesc(decoration, built_in_inst, referenced_inst,
1786
0
                                 referenced_from_inst)
1787
0
             << " " << GetStorageClassDesc(referenced_from_inst);
1788
0
    }
1789
1790
0
    for (const spv::ExecutionModel execution_model : execution_models_) {
1791
0
      if (execution_model != spv::ExecutionModel::TessellationControl &&
1792
0
          execution_model != spv::ExecutionModel::TessellationEvaluation) {
1793
0
        return _.diag(SPV_ERROR_INVALID_DATA, &referenced_from_inst)
1794
0
               << _.VkErrorID(4308)
1795
0
               << "Vulkan spec allows BuiltIn PatchVertices to be used only "
1796
0
                  "with TessellationControl or TessellationEvaluation "
1797
0
                  "execution models. "
1798
0
               << GetReferenceDesc(decoration, built_in_inst, referenced_inst,
1799
0
                                   referenced_from_inst, execution_model);
1800
0
      }
1801
0
    }
1802
0
  }
1803
1804
0
  if (function_id_ == 0) {
1805
    // Propagate this rule to all dependant ids in the global scope.
1806
0
    id_to_at_reference_checks_[referenced_from_inst.id()].push_back(std::bind(
1807
0
        &BuiltInsValidator::ValidatePatchVerticesAtReference, this, decoration,
1808
0
        built_in_inst, referenced_from_inst, std::placeholders::_1));
1809
0
  }
1810
1811
0
  return SPV_SUCCESS;
1812
0
}
1813
1814
spv_result_t BuiltInsValidator::ValidatePointCoordAtDefinition(
1815
0
    const Decoration& decoration, const Instruction& inst) {
1816
0
  if (spvIsVulkanEnv(_.context()->target_env)) {
1817
0
    if (spv_result_t error = ValidateF32Vec(
1818
0
            decoration, inst, 2,
1819
0
            [this, &inst](const std::string& message) -> spv_result_t {
1820
0
              return _.diag(SPV_ERROR_INVALID_DATA, &inst)
1821
0
                     << _.VkErrorID(4313)
1822
0
                     << "According to the Vulkan spec BuiltIn PointCoord "
1823
0
                        "variable needs to be a 2-component 32-bit float "
1824
0
                        "vector. "
1825
0
                     << message;
1826
0
            })) {
1827
0
      return error;
1828
0
    }
1829
0
  }
1830
1831
  // Seed at reference checks with this built-in.
1832
0
  return ValidatePointCoordAtReference(decoration, inst, inst, inst);
1833
0
}
1834
1835
spv_result_t BuiltInsValidator::ValidatePointCoordAtReference(
1836
    const Decoration& decoration, const Instruction& built_in_inst,
1837
    const Instruction& referenced_inst,
1838
0
    const Instruction& referenced_from_inst) {
1839
0
  if (spvIsVulkanEnv(_.context()->target_env)) {
1840
0
    const spv::StorageClass storage_class = GetStorageClass(referenced_from_inst);
1841
0
    if (storage_class != spv::StorageClass::Max &&
1842
0
        storage_class != spv::StorageClass::Input) {
1843
0
      return _.diag(SPV_ERROR_INVALID_DATA, &referenced_from_inst)
1844
0
             << _.VkErrorID(4312)
1845
0
             << "Vulkan spec allows BuiltIn PointCoord to be only used for "
1846
0
                "variables with Input storage class. "
1847
0
             << GetReferenceDesc(decoration, built_in_inst, referenced_inst,
1848
0
                                 referenced_from_inst)
1849
0
             << " " << GetStorageClassDesc(referenced_from_inst);
1850
0
    }
1851
1852
0
    for (const spv::ExecutionModel execution_model : execution_models_) {
1853
0
      if (execution_model != spv::ExecutionModel::Fragment) {
1854
0
        return _.diag(SPV_ERROR_INVALID_DATA, &referenced_from_inst)
1855
0
               << _.VkErrorID(4311)
1856
0
               << "Vulkan spec allows BuiltIn PointCoord to be used only with "
1857
0
                  "Fragment execution model. "
1858
0
               << GetReferenceDesc(decoration, built_in_inst, referenced_inst,
1859
0
                                   referenced_from_inst, execution_model);
1860
0
      }
1861
0
    }
1862
0
  }
1863
1864
0
  if (function_id_ == 0) {
1865
    // Propagate this rule to all dependant ids in the global scope.
1866
0
    id_to_at_reference_checks_[referenced_from_inst.id()].push_back(std::bind(
1867
0
        &BuiltInsValidator::ValidatePointCoordAtReference, this, decoration,
1868
0
        built_in_inst, referenced_from_inst, std::placeholders::_1));
1869
0
  }
1870
1871
0
  return SPV_SUCCESS;
1872
0
}
1873
1874
spv_result_t BuiltInsValidator::ValidatePointSizeAtDefinition(
1875
0
    const Decoration& decoration, const Instruction& inst) {
1876
  // Seed at reference checks with this built-in.
1877
0
  return ValidatePointSizeAtReference(decoration, inst, inst, inst);
1878
0
}
1879
1880
spv_result_t BuiltInsValidator::ValidatePointSizeAtReference(
1881
    const Decoration& decoration, const Instruction& built_in_inst,
1882
    const Instruction& referenced_inst,
1883
0
    const Instruction& referenced_from_inst) {
1884
0
  if (spvIsVulkanEnv(_.context()->target_env)) {
1885
0
    const spv::StorageClass storage_class = GetStorageClass(referenced_from_inst);
1886
0
    if (storage_class != spv::StorageClass::Max &&
1887
0
        storage_class != spv::StorageClass::Input &&
1888
0
        storage_class != spv::StorageClass::Output) {
1889
0
      return _.diag(SPV_ERROR_INVALID_DATA, &referenced_from_inst)
1890
0
             << _.VkErrorID(4316)
1891
0
             << "Vulkan spec allows BuiltIn PointSize to be only used for "
1892
0
                "variables with Input or Output storage class. "
1893
0
             << GetReferenceDesc(decoration, built_in_inst, referenced_inst,
1894
0
                                 referenced_from_inst)
1895
0
             << " " << GetStorageClassDesc(referenced_from_inst);
1896
0
    }
1897
1898
0
    if (storage_class == spv::StorageClass::Input) {
1899
0
      assert(function_id_ == 0);
1900
0
      id_to_at_reference_checks_[referenced_from_inst.id()].push_back(std::bind(
1901
0
          &BuiltInsValidator::ValidateNotCalledWithExecutionModel, this, 4315,
1902
0
          "Vulkan spec doesn't allow BuiltIn PointSize to be used for "
1903
0
          "variables with Input storage class if execution model is "
1904
0
          "Vertex.",
1905
0
          spv::ExecutionModel::Vertex, decoration, built_in_inst,
1906
0
          referenced_from_inst, std::placeholders::_1));
1907
0
    }
1908
1909
0
    for (const spv::ExecutionModel execution_model : execution_models_) {
1910
0
      switch (execution_model) {
1911
0
        case spv::ExecutionModel::Vertex: {
1912
0
          if (spv_result_t error = ValidateF32(
1913
0
                  decoration, built_in_inst,
1914
0
                  [this, &referenced_from_inst](
1915
0
                      const std::string& message) -> spv_result_t {
1916
0
                    return _.diag(SPV_ERROR_INVALID_DATA, &referenced_from_inst)
1917
0
                           << _.VkErrorID(4317)
1918
0
                           << "According to the Vulkan spec BuiltIn PointSize "
1919
0
                              "variable needs to be a 32-bit float scalar. "
1920
0
                           << message;
1921
0
                  })) {
1922
0
            return error;
1923
0
          }
1924
0
          break;
1925
0
        }
1926
0
        case spv::ExecutionModel::TessellationControl:
1927
0
        case spv::ExecutionModel::TessellationEvaluation:
1928
0
        case spv::ExecutionModel::Geometry:
1929
0
        case spv::ExecutionModel::MeshNV:
1930
0
        case spv::ExecutionModel::MeshEXT: {
1931
          // PointSize can be a per-vertex variable for tessellation control,
1932
          // tessellation evaluation and geometry shader stages. In such cases
1933
          // variables will have an array of 32-bit floats.
1934
0
          if (decoration.struct_member_index() != Decoration::kInvalidMember) {
1935
            // The array is on the variable, so this must be a 32-bit float.
1936
0
            if (spv_result_t error = ValidateF32(
1937
0
                    decoration, built_in_inst,
1938
0
                    [this, &referenced_from_inst](
1939
0
                        const std::string& message) -> spv_result_t {
1940
0
                      return _.diag(SPV_ERROR_INVALID_DATA,
1941
0
                                    &referenced_from_inst)
1942
0
                             << _.VkErrorID(4317)
1943
0
                             << "According to the Vulkan spec BuiltIn "
1944
0
                                "PointSize variable needs to be a 32-bit "
1945
0
                                "float scalar. "
1946
0
                             << message;
1947
0
                    })) {
1948
0
              return error;
1949
0
            }
1950
0
          } else {
1951
0
            if (spv_result_t error = ValidateOptionalArrayedF32(
1952
0
                    decoration, built_in_inst,
1953
0
                    [this, &referenced_from_inst](
1954
0
                        const std::string& message) -> spv_result_t {
1955
0
                      return _.diag(SPV_ERROR_INVALID_DATA,
1956
0
                                    &referenced_from_inst)
1957
0
                             << _.VkErrorID(4317)
1958
0
                             << "According to the Vulkan spec BuiltIn "
1959
0
                                "PointSize variable needs to be a 32-bit "
1960
0
                                "float scalar. "
1961
0
                             << message;
1962
0
                    })) {
1963
0
              return error;
1964
0
            }
1965
0
          }
1966
0
          break;
1967
0
        }
1968
1969
0
        default: {
1970
0
          return _.diag(SPV_ERROR_INVALID_DATA, &referenced_from_inst)
1971
0
                 << _.VkErrorID(4314)
1972
0
                 << "Vulkan spec allows BuiltIn PointSize to be used only with "
1973
0
                    "Vertex, TessellationControl, TessellationEvaluation or "
1974
0
                    "Geometry execution models. "
1975
0
                 << GetReferenceDesc(decoration, built_in_inst, referenced_inst,
1976
0
                                     referenced_from_inst, execution_model);
1977
0
        }
1978
0
      }
1979
0
    }
1980
0
  }
1981
1982
0
  if (function_id_ == 0) {
1983
    // Propagate this rule to all dependant ids in the global scope.
1984
0
    id_to_at_reference_checks_[referenced_from_inst.id()].push_back(std::bind(
1985
0
        &BuiltInsValidator::ValidatePointSizeAtReference, this, decoration,
1986
0
        built_in_inst, referenced_from_inst, std::placeholders::_1));
1987
0
  }
1988
1989
0
  return SPV_SUCCESS;
1990
0
}
1991
1992
spv_result_t BuiltInsValidator::ValidatePositionAtDefinition(
1993
0
    const Decoration& decoration, const Instruction& inst) {
1994
  // Seed at reference checks with this built-in.
1995
0
  return ValidatePositionAtReference(decoration, inst, inst, inst);
1996
0
}
1997
1998
spv_result_t BuiltInsValidator::ValidatePositionAtReference(
1999
    const Decoration& decoration, const Instruction& built_in_inst,
2000
    const Instruction& referenced_inst,
2001
0
    const Instruction& referenced_from_inst) {
2002
0
  if (spvIsVulkanEnv(_.context()->target_env)) {
2003
0
    const spv::StorageClass storage_class = GetStorageClass(referenced_from_inst);
2004
0
    if (storage_class != spv::StorageClass::Max &&
2005
0
        storage_class != spv::StorageClass::Input &&
2006
0
        storage_class != spv::StorageClass::Output) {
2007
0
      return _.diag(SPV_ERROR_INVALID_DATA, &referenced_from_inst)
2008
0
             << _.VkErrorID(4320) << "Vulkan spec allows BuiltIn Position to be only used for "
2009
0
                "variables with Input or Output storage class. "
2010
0
             << GetReferenceDesc(decoration, built_in_inst, referenced_inst,
2011
0
                                 referenced_from_inst)
2012
0
             << " " << GetStorageClassDesc(referenced_from_inst);
2013
0
    }
2014
2015
0
    if (storage_class == spv::StorageClass::Input) {
2016
0
      assert(function_id_ == 0);
2017
0
      id_to_at_reference_checks_[referenced_from_inst.id()].push_back(std::bind(
2018
0
          &BuiltInsValidator::ValidateNotCalledWithExecutionModel, this, 4319,
2019
0
          "Vulkan spec doesn't allow BuiltIn Position to be used "
2020
0
          "for variables "
2021
0
          "with Input storage class if execution model is Vertex.",
2022
0
          spv::ExecutionModel::Vertex, decoration, built_in_inst,
2023
0
          referenced_from_inst, std::placeholders::_1));
2024
0
      id_to_at_reference_checks_[referenced_from_inst.id()].push_back(std::bind(
2025
0
          &BuiltInsValidator::ValidateNotCalledWithExecutionModel, this, 4319,
2026
0
          "Vulkan spec doesn't allow BuiltIn Position to be used "
2027
0
          "for variables "
2028
0
          "with Input storage class if execution model is MeshNV.",
2029
0
          spv::ExecutionModel::MeshNV, decoration, built_in_inst,
2030
0
          referenced_from_inst, std::placeholders::_1));
2031
0
      id_to_at_reference_checks_[referenced_from_inst.id()].push_back(std::bind(
2032
0
          &BuiltInsValidator::ValidateNotCalledWithExecutionModel, this, 4319,
2033
0
          "Vulkan spec doesn't allow BuiltIn Position to be used "
2034
0
          "for variables "
2035
0
          "with Input storage class if execution model is MeshEXT.",
2036
0
          spv::ExecutionModel::MeshEXT, decoration, built_in_inst,
2037
0
          referenced_from_inst, std::placeholders::_1));
2038
0
    }
2039
2040
0
    for (const spv::ExecutionModel execution_model : execution_models_) {
2041
0
      switch (execution_model) {
2042
0
        case spv::ExecutionModel::Vertex: {
2043
0
          if (spv_result_t error = ValidateF32Vec(
2044
0
                  decoration, built_in_inst, 4,
2045
0
                  [this, &referenced_from_inst](
2046
0
                      const std::string& message) -> spv_result_t {
2047
0
                    return _.diag(SPV_ERROR_INVALID_DATA, &referenced_from_inst)
2048
0
                           << _.VkErrorID(4321)
2049
0
                           << "According to the Vulkan spec BuiltIn Position "
2050
0
                              "variable needs to be a 4-component 32-bit float "
2051
0
                              "vector. "
2052
0
                           << message;
2053
0
                  })) {
2054
0
            return error;
2055
0
          }
2056
0
          break;
2057
0
        }
2058
0
        case spv::ExecutionModel::Geometry:
2059
0
        case spv::ExecutionModel::TessellationControl:
2060
0
        case spv::ExecutionModel::TessellationEvaluation:
2061
0
        case spv::ExecutionModel::MeshNV:
2062
0
        case spv::ExecutionModel::MeshEXT: {
2063
          // Position can be a per-vertex variable for tessellation control,
2064
          // tessellation evaluation, geometry and mesh shader stages. In such
2065
          // cases variables will have an array of 4-component 32-bit float
2066
          // vectors.
2067
0
          if (decoration.struct_member_index() != Decoration::kInvalidMember) {
2068
            // The array is on the variable, so this must be a 4-component
2069
            // 32-bit float vector.
2070
0
            if (spv_result_t error = ValidateF32Vec(
2071
0
                    decoration, built_in_inst, 4,
2072
0
                    [this, &referenced_from_inst](
2073
0
                        const std::string& message) -> spv_result_t {
2074
0
                      return _.diag(SPV_ERROR_INVALID_DATA,
2075
0
                                    &referenced_from_inst)
2076
0
                             << _.VkErrorID(4321)
2077
0
                             << "According to the Vulkan spec BuiltIn Position "
2078
0
                                "variable needs to be a 4-component 32-bit "
2079
0
                                "float vector. "
2080
0
                             << message;
2081
0
                    })) {
2082
0
              return error;
2083
0
            }
2084
0
          } else {
2085
0
            if (spv_result_t error = ValidateOptionalArrayedF32Vec(
2086
0
                    decoration, built_in_inst, 4,
2087
0
                    [this, &referenced_from_inst](
2088
0
                        const std::string& message) -> spv_result_t {
2089
0
                      return _.diag(SPV_ERROR_INVALID_DATA,
2090
0
                                    &referenced_from_inst)
2091
0
                             << _.VkErrorID(4321)
2092
0
                             << "According to the Vulkan spec BuiltIn Position "
2093
0
                                "variable needs to be a 4-component 32-bit "
2094
0
                                "float vector. "
2095
0
                             << message;
2096
0
                    })) {
2097
0
              return error;
2098
0
            }
2099
0
          }
2100
0
          break;
2101
0
        }
2102
2103
0
        default: {
2104
0
          return _.diag(SPV_ERROR_INVALID_DATA, &referenced_from_inst)
2105
0
                 << _.VkErrorID(4318)
2106
0
                 << "Vulkan spec allows BuiltIn Position to be used only "
2107
0
                    "with Vertex, TessellationControl, TessellationEvaluation"
2108
0
                    " or Geometry execution models. "
2109
0
                 << GetReferenceDesc(decoration, built_in_inst, referenced_inst,
2110
0
                                     referenced_from_inst, execution_model);
2111
0
        }
2112
0
      }
2113
0
    }
2114
0
  }
2115
2116
0
  if (function_id_ == 0) {
2117
    // Propagate this rule to all dependant ids in the global scope.
2118
0
    id_to_at_reference_checks_[referenced_from_inst.id()].push_back(std::bind(
2119
0
        &BuiltInsValidator::ValidatePositionAtReference, this, decoration,
2120
0
        built_in_inst, referenced_from_inst, std::placeholders::_1));
2121
0
  }
2122
2123
0
  return SPV_SUCCESS;
2124
0
}
2125
2126
spv_result_t BuiltInsValidator::ValidatePrimitiveIdAtDefinition(
2127
0
    const Decoration& decoration, const Instruction& inst) {
2128
0
  if (spvIsVulkanEnv(_.context()->target_env)) {
2129
    // PrimitiveId can be a per-primitive variable for mesh shader stage.
2130
    // In such cases variable will have an array of 32-bit integers.
2131
0
    if (decoration.struct_member_index() != Decoration::kInvalidMember) {
2132
      // This must be a 32-bit int scalar.
2133
0
      if (spv_result_t error = ValidateI32(
2134
0
              decoration, inst,
2135
0
              [this, &inst](const std::string& message) -> spv_result_t {
2136
0
                return _.diag(SPV_ERROR_INVALID_DATA, &inst)
2137
0
                       << _.VkErrorID(4337)
2138
0
                       << "According to the Vulkan spec BuiltIn PrimitiveId "
2139
0
                          "variable needs to be a 32-bit int scalar. "
2140
0
                       << message;
2141
0
              })) {
2142
0
        return error;
2143
0
      }
2144
0
    } else {
2145
0
      if (spv_result_t error = ValidateOptionalArrayedI32(
2146
0
              decoration, inst,
2147
0
              [this, &inst](const std::string& message) -> spv_result_t {
2148
0
                return _.diag(SPV_ERROR_INVALID_DATA, &inst)
2149
0
                       << _.VkErrorID(4337)
2150
0
                       << "According to the Vulkan spec BuiltIn PrimitiveId "
2151
0
                          "variable needs to be a 32-bit int scalar. "
2152
0
                       << message;
2153
0
              })) {
2154
0
        return error;
2155
0
      }
2156
0
    }
2157
0
  }
2158
2159
  // Seed at reference checks with this built-in.
2160
0
  return ValidatePrimitiveIdAtReference(decoration, inst, inst, inst);
2161
0
}
2162
2163
spv_result_t BuiltInsValidator::ValidatePrimitiveIdAtReference(
2164
    const Decoration& decoration, const Instruction& built_in_inst,
2165
    const Instruction& referenced_inst,
2166
0
    const Instruction& referenced_from_inst) {
2167
0
  if (spvIsVulkanEnv(_.context()->target_env)) {
2168
0
    const spv::StorageClass storage_class = GetStorageClass(referenced_from_inst);
2169
0
    if (storage_class != spv::StorageClass::Max &&
2170
0
        storage_class != spv::StorageClass::Input &&
2171
0
        storage_class != spv::StorageClass::Output) {
2172
0
      return _.diag(SPV_ERROR_INVALID_DATA, &referenced_from_inst)
2173
0
             << "Vulkan spec allows BuiltIn PrimitiveId to be only used for "
2174
0
                "variables with Input or Output storage class. "
2175
0
             << GetReferenceDesc(decoration, built_in_inst, referenced_inst,
2176
0
                                 referenced_from_inst)
2177
0
             << " " << GetStorageClassDesc(referenced_from_inst);
2178
0
    }
2179
2180
0
    if (storage_class == spv::StorageClass::Output) {
2181
0
      assert(function_id_ == 0);
2182
0
      id_to_at_reference_checks_[referenced_from_inst.id()].push_back(std::bind(
2183
0
          &BuiltInsValidator::ValidateNotCalledWithExecutionModel, this, 4334,
2184
0
          "Vulkan spec doesn't allow BuiltIn PrimitiveId to be used for "
2185
0
          "variables with Output storage class if execution model is "
2186
0
          "TessellationControl.",
2187
0
          spv::ExecutionModel::TessellationControl, decoration, built_in_inst,
2188
0
          referenced_from_inst, std::placeholders::_1));
2189
0
      id_to_at_reference_checks_[referenced_from_inst.id()].push_back(std::bind(
2190
0
          &BuiltInsValidator::ValidateNotCalledWithExecutionModel, this, 4334,
2191
0
          "Vulkan spec doesn't allow BuiltIn PrimitiveId to be used for "
2192
0
          "variables with Output storage class if execution model is "
2193
0
          "TessellationEvaluation.",
2194
0
          spv::ExecutionModel::TessellationEvaluation, decoration, built_in_inst,
2195
0
          referenced_from_inst, std::placeholders::_1));
2196
0
      id_to_at_reference_checks_[referenced_from_inst.id()].push_back(std::bind(
2197
0
          &BuiltInsValidator::ValidateNotCalledWithExecutionModel, this, 4334,
2198
0
          "Vulkan spec doesn't allow BuiltIn PrimitiveId to be used for "
2199
0
          "variables with Output storage class if execution model is "
2200
0
          "Fragment.",
2201
0
          spv::ExecutionModel::Fragment, decoration, built_in_inst,
2202
0
          referenced_from_inst, std::placeholders::_1));
2203
0
      id_to_at_reference_checks_[referenced_from_inst.id()].push_back(std::bind(
2204
0
          &BuiltInsValidator::ValidateNotCalledWithExecutionModel, this, 4334,
2205
0
          "Vulkan spec doesn't allow BuiltIn PrimitiveId to be used for "
2206
0
          "variables with Output storage class if execution model is "
2207
0
          "IntersectionKHR.",
2208
0
          spv::ExecutionModel::IntersectionKHR, decoration, built_in_inst,
2209
0
          referenced_from_inst, std::placeholders::_1));
2210
0
      id_to_at_reference_checks_[referenced_from_inst.id()].push_back(std::bind(
2211
0
          &BuiltInsValidator::ValidateNotCalledWithExecutionModel, this, 4334,
2212
0
          "Vulkan spec doesn't allow BuiltIn PrimitiveId to be used for "
2213
0
          "variables with Output storage class if execution model is "
2214
0
          "AnyHitKHR.",
2215
0
          spv::ExecutionModel::AnyHitKHR, decoration, built_in_inst,
2216
0
          referenced_from_inst, std::placeholders::_1));
2217
0
      id_to_at_reference_checks_[referenced_from_inst.id()].push_back(std::bind(
2218
0
          &BuiltInsValidator::ValidateNotCalledWithExecutionModel, this, 4334,
2219
0
          "Vulkan spec doesn't allow BuiltIn PrimitiveId to be used for "
2220
0
          "variables with Output storage class if execution model is "
2221
0
          "ClosestHitKHR.",
2222
0
          spv::ExecutionModel::ClosestHitKHR, decoration, built_in_inst,
2223
0
          referenced_from_inst, std::placeholders::_1));
2224
0
    }
2225
2226
0
    for (const spv::ExecutionModel execution_model : execution_models_) {
2227
0
      switch (execution_model) {
2228
0
        case spv::ExecutionModel::Fragment:
2229
0
        case spv::ExecutionModel::TessellationControl:
2230
0
        case spv::ExecutionModel::TessellationEvaluation:
2231
0
        case spv::ExecutionModel::Geometry:
2232
0
        case spv::ExecutionModel::MeshNV:
2233
0
        case spv::ExecutionModel::MeshEXT:
2234
0
        case spv::ExecutionModel::IntersectionKHR:
2235
0
        case spv::ExecutionModel::AnyHitKHR:
2236
0
        case spv::ExecutionModel::ClosestHitKHR: {
2237
          // Ok.
2238
0
          break;
2239
0
        }
2240
2241
0
        default: {
2242
0
          return _.diag(SPV_ERROR_INVALID_DATA, &referenced_from_inst)
2243
0
                 << _.VkErrorID(4330)
2244
0
                 << "Vulkan spec allows BuiltIn PrimitiveId to be used only "
2245
0
                    "with Fragment, TessellationControl, "
2246
0
                    "TessellationEvaluation, Geometry, MeshNV, MeshEXT, "
2247
0
                    "IntersectionKHR, AnyHitKHR, and ClosestHitKHR execution models. "
2248
0
                 << GetReferenceDesc(decoration, built_in_inst, referenced_inst,
2249
0
                                     referenced_from_inst, execution_model);
2250
0
        }
2251
0
      }
2252
0
    }
2253
0
  }
2254
2255
0
  if (function_id_ == 0) {
2256
    // Propagate this rule to all dependant ids in the global scope.
2257
0
    id_to_at_reference_checks_[referenced_from_inst.id()].push_back(std::bind(
2258
0
        &BuiltInsValidator::ValidatePrimitiveIdAtReference, this, decoration,
2259
0
        built_in_inst, referenced_from_inst, std::placeholders::_1));
2260
0
  }
2261
2262
0
  return SPV_SUCCESS;
2263
0
}
2264
2265
spv_result_t BuiltInsValidator::ValidateSampleIdAtDefinition(
2266
0
    const Decoration& decoration, const Instruction& inst) {
2267
0
  if (spvIsVulkanEnv(_.context()->target_env)) {
2268
0
    if (spv_result_t error = ValidateI32(
2269
0
            decoration, inst,
2270
0
            [this, &inst](const std::string& message) -> spv_result_t {
2271
0
              return _.diag(SPV_ERROR_INVALID_DATA, &inst)
2272
0
                     << _.VkErrorID(4356)
2273
0
                     << "According to the Vulkan spec BuiltIn SampleId "
2274
0
                        "variable needs to be a 32-bit int scalar. "
2275
0
                     << message;
2276
0
            })) {
2277
0
      return error;
2278
0
    }
2279
0
  }
2280
2281
  // Seed at reference checks with this built-in.
2282
0
  return ValidateSampleIdAtReference(decoration, inst, inst, inst);
2283
0
}
2284
2285
spv_result_t BuiltInsValidator::ValidateSampleIdAtReference(
2286
    const Decoration& decoration, const Instruction& built_in_inst,
2287
    const Instruction& referenced_inst,
2288
0
    const Instruction& referenced_from_inst) {
2289
0
  if (spvIsVulkanEnv(_.context()->target_env)) {
2290
0
    const spv::StorageClass storage_class = GetStorageClass(referenced_from_inst);
2291
0
    if (storage_class != spv::StorageClass::Max &&
2292
0
        storage_class != spv::StorageClass::Input) {
2293
0
      return _.diag(SPV_ERROR_INVALID_DATA, &referenced_from_inst)
2294
0
             << _.VkErrorID(4355)
2295
0
             << "Vulkan spec allows BuiltIn SampleId to be only used for "
2296
0
                "variables with Input storage class. "
2297
0
             << GetReferenceDesc(decoration, built_in_inst, referenced_inst,
2298
0
                                 referenced_from_inst)
2299
0
             << " " << GetStorageClassDesc(referenced_from_inst);
2300
0
    }
2301
2302
0
    for (const spv::ExecutionModel execution_model : execution_models_) {
2303
0
      if (execution_model != spv::ExecutionModel::Fragment) {
2304
0
        return _.diag(SPV_ERROR_INVALID_DATA, &referenced_from_inst)
2305
0
               << _.VkErrorID(4354)
2306
0
               << "Vulkan spec allows BuiltIn SampleId to be used only with "
2307
0
                  "Fragment execution model. "
2308
0
               << GetReferenceDesc(decoration, built_in_inst, referenced_inst,
2309
0
                                   referenced_from_inst, execution_model);
2310
0
      }
2311
0
    }
2312
0
  }
2313
2314
0
  if (function_id_ == 0) {
2315
    // Propagate this rule to all dependant ids in the global scope.
2316
0
    id_to_at_reference_checks_[referenced_from_inst.id()].push_back(std::bind(
2317
0
        &BuiltInsValidator::ValidateSampleIdAtReference, this, decoration,
2318
0
        built_in_inst, referenced_from_inst, std::placeholders::_1));
2319
0
  }
2320
2321
0
  return SPV_SUCCESS;
2322
0
}
2323
2324
spv_result_t BuiltInsValidator::ValidateSampleMaskAtDefinition(
2325
0
    const Decoration& decoration, const Instruction& inst) {
2326
0
  if (spvIsVulkanEnv(_.context()->target_env)) {
2327
0
    if (spv_result_t error = ValidateI32Arr(
2328
0
            decoration, inst,
2329
0
            [this, &inst](const std::string& message) -> spv_result_t {
2330
0
              return _.diag(SPV_ERROR_INVALID_DATA, &inst)
2331
0
                     << _.VkErrorID(4359)
2332
0
                     << "According to the Vulkan spec BuiltIn SampleMask "
2333
0
                        "variable needs to be a 32-bit int array. "
2334
0
                     << message;
2335
0
            })) {
2336
0
      return error;
2337
0
    }
2338
0
  }
2339
2340
  // Seed at reference checks with this built-in.
2341
0
  return ValidateSampleMaskAtReference(decoration, inst, inst, inst);
2342
0
}
2343
2344
spv_result_t BuiltInsValidator::ValidateSampleMaskAtReference(
2345
    const Decoration& decoration, const Instruction& built_in_inst,
2346
    const Instruction& referenced_inst,
2347
0
    const Instruction& referenced_from_inst) {
2348
0
  if (spvIsVulkanEnv(_.context()->target_env)) {
2349
0
    const spv::StorageClass storage_class = GetStorageClass(referenced_from_inst);
2350
0
    if (storage_class != spv::StorageClass::Max &&
2351
0
        storage_class != spv::StorageClass::Input &&
2352
0
        storage_class != spv::StorageClass::Output) {
2353
0
      return _.diag(SPV_ERROR_INVALID_DATA, &referenced_from_inst)
2354
0
             << _.VkErrorID(4358)
2355
0
             << "Vulkan spec allows BuiltIn SampleMask to be only used for "
2356
0
                "variables with Input or Output storage class. "
2357
0
             << GetReferenceDesc(decoration, built_in_inst, referenced_inst,
2358
0
                                 referenced_from_inst)
2359
0
             << " " << GetStorageClassDesc(referenced_from_inst);
2360
0
    }
2361
2362
0
    for (const spv::ExecutionModel execution_model : execution_models_) {
2363
0
      if (execution_model != spv::ExecutionModel::Fragment) {
2364
0
        return _.diag(SPV_ERROR_INVALID_DATA, &referenced_from_inst)
2365
0
               << _.VkErrorID(4357)
2366
0
               << "Vulkan spec allows BuiltIn SampleMask to be used only "
2367
0
                  "with "
2368
0
                  "Fragment execution model. "
2369
0
               << GetReferenceDesc(decoration, built_in_inst, referenced_inst,
2370
0
                                   referenced_from_inst, execution_model);
2371
0
      }
2372
0
    }
2373
0
  }
2374
2375
0
  if (function_id_ == 0) {
2376
    // Propagate this rule to all dependant ids in the global scope.
2377
0
    id_to_at_reference_checks_[referenced_from_inst.id()].push_back(std::bind(
2378
0
        &BuiltInsValidator::ValidateSampleMaskAtReference, this, decoration,
2379
0
        built_in_inst, referenced_from_inst, std::placeholders::_1));
2380
0
  }
2381
2382
0
  return SPV_SUCCESS;
2383
0
}
2384
2385
spv_result_t BuiltInsValidator::ValidateSamplePositionAtDefinition(
2386
0
    const Decoration& decoration, const Instruction& inst) {
2387
0
  if (spvIsVulkanEnv(_.context()->target_env)) {
2388
0
    if (spv_result_t error = ValidateF32Vec(
2389
0
            decoration, inst, 2,
2390
0
            [this, &inst](const std::string& message) -> spv_result_t {
2391
0
              return _.diag(SPV_ERROR_INVALID_DATA, &inst)
2392
0
                     << _.VkErrorID(4362)
2393
0
                     << "According to the Vulkan spec BuiltIn SamplePosition "
2394
0
                        "variable needs to be a 2-component 32-bit float "
2395
0
                        "vector. "
2396
0
                     << message;
2397
0
            })) {
2398
0
      return error;
2399
0
    }
2400
0
  }
2401
2402
  // Seed at reference checks with this built-in.
2403
0
  return ValidateSamplePositionAtReference(decoration, inst, inst, inst);
2404
0
}
2405
2406
spv_result_t BuiltInsValidator::ValidateSamplePositionAtReference(
2407
    const Decoration& decoration, const Instruction& built_in_inst,
2408
    const Instruction& referenced_inst,
2409
0
    const Instruction& referenced_from_inst) {
2410
0
  if (spvIsVulkanEnv(_.context()->target_env)) {
2411
0
    const spv::StorageClass storage_class = GetStorageClass(referenced_from_inst);
2412
0
    if (storage_class != spv::StorageClass::Max &&
2413
0
        storage_class != spv::StorageClass::Input) {
2414
0
      return _.diag(SPV_ERROR_INVALID_DATA, &referenced_from_inst)
2415
0
             << _.VkErrorID(4361)
2416
0
             << "Vulkan spec allows BuiltIn SamplePosition to be only used "
2417
0
                "for "
2418
0
                "variables with Input storage class. "
2419
0
             << GetReferenceDesc(decoration, built_in_inst, referenced_inst,
2420
0
                                 referenced_from_inst)
2421
0
             << " " << GetStorageClassDesc(referenced_from_inst);
2422
0
    }
2423
2424
0
    for (const spv::ExecutionModel execution_model : execution_models_) {
2425
0
      if (execution_model != spv::ExecutionModel::Fragment) {
2426
0
        return _.diag(SPV_ERROR_INVALID_DATA, &referenced_from_inst)
2427
0
               << _.VkErrorID(4360)
2428
0
               << "Vulkan spec allows BuiltIn SamplePosition to be used only "
2429
0
                  "with "
2430
0
                  "Fragment execution model. "
2431
0
               << GetReferenceDesc(decoration, built_in_inst, referenced_inst,
2432
0
                                   referenced_from_inst, execution_model);
2433
0
      }
2434
0
    }
2435
0
  }
2436
2437
0
  if (function_id_ == 0) {
2438
    // Propagate this rule to all dependant ids in the global scope.
2439
0
    id_to_at_reference_checks_[referenced_from_inst.id()].push_back(std::bind(
2440
0
        &BuiltInsValidator::ValidateSamplePositionAtReference, this, decoration,
2441
0
        built_in_inst, referenced_from_inst, std::placeholders::_1));
2442
0
  }
2443
2444
0
  return SPV_SUCCESS;
2445
0
}
2446
2447
spv_result_t BuiltInsValidator::ValidateTessCoordAtDefinition(
2448
0
    const Decoration& decoration, const Instruction& inst) {
2449
0
  if (spvIsVulkanEnv(_.context()->target_env)) {
2450
0
    if (spv_result_t error = ValidateF32Vec(
2451
0
            decoration, inst, 3,
2452
0
            [this, &inst](const std::string& message) -> spv_result_t {
2453
0
              return _.diag(SPV_ERROR_INVALID_DATA, &inst)
2454
0
                     << _.VkErrorID(4389)
2455
0
                     << "According to the Vulkan spec BuiltIn TessCoord "
2456
0
                        "variable needs to be a 3-component 32-bit float "
2457
0
                        "vector. "
2458
0
                     << message;
2459
0
            })) {
2460
0
      return error;
2461
0
    }
2462
0
  }
2463
2464
  // Seed at reference checks with this built-in.
2465
0
  return ValidateTessCoordAtReference(decoration, inst, inst, inst);
2466
0
}
2467
2468
spv_result_t BuiltInsValidator::ValidateTessCoordAtReference(
2469
    const Decoration& decoration, const Instruction& built_in_inst,
2470
    const Instruction& referenced_inst,
2471
0
    const Instruction& referenced_from_inst) {
2472
0
  if (spvIsVulkanEnv(_.context()->target_env)) {
2473
0
    const spv::StorageClass storage_class = GetStorageClass(referenced_from_inst);
2474
0
    if (storage_class != spv::StorageClass::Max &&
2475
0
        storage_class != spv::StorageClass::Input) {
2476
0
      return _.diag(SPV_ERROR_INVALID_DATA, &referenced_from_inst)
2477
0
             << _.VkErrorID(4388)
2478
0
             << "Vulkan spec allows BuiltIn TessCoord to be only used for "
2479
0
                "variables with Input storage class. "
2480
0
             << GetReferenceDesc(decoration, built_in_inst, referenced_inst,
2481
0
                                 referenced_from_inst)
2482
0
             << " " << GetStorageClassDesc(referenced_from_inst);
2483
0
    }
2484
2485
0
    for (const spv::ExecutionModel execution_model : execution_models_) {
2486
0
      if (execution_model != spv::ExecutionModel::TessellationEvaluation) {
2487
0
        return _.diag(SPV_ERROR_INVALID_DATA, &referenced_from_inst)
2488
0
               << _.VkErrorID(4387)
2489
0
               << "Vulkan spec allows BuiltIn TessCoord to be used only with "
2490
0
                  "TessellationEvaluation execution model. "
2491
0
               << GetReferenceDesc(decoration, built_in_inst, referenced_inst,
2492
0
                                   referenced_from_inst, execution_model);
2493
0
      }
2494
0
    }
2495
0
  }
2496
2497
0
  if (function_id_ == 0) {
2498
    // Propagate this rule to all dependant ids in the global scope.
2499
0
    id_to_at_reference_checks_[referenced_from_inst.id()].push_back(std::bind(
2500
0
        &BuiltInsValidator::ValidateTessCoordAtReference, this, decoration,
2501
0
        built_in_inst, referenced_from_inst, std::placeholders::_1));
2502
0
  }
2503
2504
0
  return SPV_SUCCESS;
2505
0
}
2506
2507
spv_result_t BuiltInsValidator::ValidateTessLevelOuterAtDefinition(
2508
0
    const Decoration& decoration, const Instruction& inst) {
2509
0
  if (spvIsVulkanEnv(_.context()->target_env)) {
2510
0
    if (spv_result_t error = ValidateF32Arr(
2511
0
            decoration, inst, 4,
2512
0
            [this, &inst](const std::string& message) -> spv_result_t {
2513
0
              return _.diag(SPV_ERROR_INVALID_DATA, &inst)
2514
0
                     << _.VkErrorID(4393)
2515
0
                     << "According to the Vulkan spec BuiltIn TessLevelOuter "
2516
0
                        "variable needs to be a 4-component 32-bit float "
2517
0
                        "array. "
2518
0
                     << message;
2519
0
            })) {
2520
0
      return error;
2521
0
    }
2522
0
  }
2523
2524
  // Seed at reference checks with this built-in.
2525
0
  return ValidateTessLevelAtReference(decoration, inst, inst, inst);
2526
0
}
2527
2528
spv_result_t BuiltInsValidator::ValidateTessLevelInnerAtDefinition(
2529
0
    const Decoration& decoration, const Instruction& inst) {
2530
0
  if (spvIsVulkanEnv(_.context()->target_env)) {
2531
0
    if (spv_result_t error = ValidateF32Arr(
2532
0
            decoration, inst, 2,
2533
0
            [this, &inst](const std::string& message) -> spv_result_t {
2534
0
              return _.diag(SPV_ERROR_INVALID_DATA, &inst)
2535
0
                     << _.VkErrorID(4397)
2536
0
                     << "According to the Vulkan spec BuiltIn TessLevelOuter "
2537
0
                        "variable needs to be a 2-component 32-bit float "
2538
0
                        "array. "
2539
0
                     << message;
2540
0
            })) {
2541
0
      return error;
2542
0
    }
2543
0
  }
2544
2545
  // Seed at reference checks with this built-in.
2546
0
  return ValidateTessLevelAtReference(decoration, inst, inst, inst);
2547
0
}
2548
2549
spv_result_t BuiltInsValidator::ValidateTessLevelAtReference(
2550
    const Decoration& decoration, const Instruction& built_in_inst,
2551
    const Instruction& referenced_inst,
2552
0
    const Instruction& referenced_from_inst) {
2553
0
  uint32_t operand = (uint32_t)decoration.builtin();
2554
0
  if (spvIsVulkanEnv(_.context()->target_env)) {
2555
0
    const spv::StorageClass storage_class = GetStorageClass(referenced_from_inst);
2556
0
    if (storage_class != spv::StorageClass::Max &&
2557
0
        storage_class != spv::StorageClass::Input &&
2558
0
        storage_class != spv::StorageClass::Output) {
2559
0
      return _.diag(SPV_ERROR_INVALID_DATA, &referenced_from_inst)
2560
0
             << "Vulkan spec allows BuiltIn "
2561
0
             << _.grammar().lookupOperandName(SPV_OPERAND_TYPE_BUILT_IN,
2562
0
                                              operand)
2563
0
             << " to be only used for variables with Input or Output storage "
2564
0
                "class. "
2565
0
             << GetReferenceDesc(decoration, built_in_inst, referenced_inst,
2566
0
                                 referenced_from_inst)
2567
0
             << " " << GetStorageClassDesc(referenced_from_inst);
2568
0
    }
2569
2570
0
    if (storage_class == spv::StorageClass::Input) {
2571
0
      assert(function_id_ == 0);
2572
0
      uint32_t vuid =
2573
0
          (decoration.builtin() == spv::BuiltIn::TessLevelOuter) ? 4391 : 4395;
2574
0
      id_to_at_reference_checks_[referenced_from_inst.id()].push_back(std::bind(
2575
0
          &BuiltInsValidator::ValidateNotCalledWithExecutionModel, this, vuid,
2576
0
          "Vulkan spec doesn't allow TessLevelOuter/TessLevelInner to be "
2577
0
          "used "
2578
0
          "for variables with Input storage class if execution model is "
2579
0
          "TessellationControl.",
2580
0
          spv::ExecutionModel::TessellationControl, decoration, built_in_inst,
2581
0
          referenced_from_inst, std::placeholders::_1));
2582
0
    }
2583
2584
0
    if (storage_class == spv::StorageClass::Output) {
2585
0
      assert(function_id_ == 0);
2586
0
      uint32_t vuid =
2587
0
          (decoration.builtin() == spv::BuiltIn::TessLevelOuter) ? 4392 : 4396;
2588
0
      id_to_at_reference_checks_[referenced_from_inst.id()].push_back(std::bind(
2589
0
          &BuiltInsValidator::ValidateNotCalledWithExecutionModel, this, vuid,
2590
0
          "Vulkan spec doesn't allow TessLevelOuter/TessLevelInner to be "
2591
0
          "used "
2592
0
          "for variables with Output storage class if execution model is "
2593
0
          "TessellationEvaluation.",
2594
0
          spv::ExecutionModel::TessellationEvaluation, decoration, built_in_inst,
2595
0
          referenced_from_inst, std::placeholders::_1));
2596
0
    }
2597
2598
0
    for (const spv::ExecutionModel execution_model : execution_models_) {
2599
0
      switch (execution_model) {
2600
0
        case spv::ExecutionModel::TessellationControl:
2601
0
        case spv::ExecutionModel::TessellationEvaluation: {
2602
          // Ok.
2603
0
          break;
2604
0
        }
2605
2606
0
        default: {
2607
0
          uint32_t vuid = (spv::BuiltIn(operand) == spv::BuiltIn::TessLevelOuter) ? 4390 : 4394;
2608
0
          return _.diag(SPV_ERROR_INVALID_DATA, &referenced_from_inst)
2609
0
                 << _.VkErrorID(vuid) << "Vulkan spec allows BuiltIn "
2610
0
                 << _.grammar().lookupOperandName(SPV_OPERAND_TYPE_BUILT_IN,
2611
0
                                                  operand)
2612
0
                 << " to be used only with TessellationControl or "
2613
0
                    "TessellationEvaluation execution models. "
2614
0
                 << GetReferenceDesc(decoration, built_in_inst, referenced_inst,
2615
0
                                     referenced_from_inst, execution_model);
2616
0
        }
2617
0
      }
2618
0
    }
2619
0
  }
2620
2621
0
  if (function_id_ == 0) {
2622
    // Propagate this rule to all dependant ids in the global scope.
2623
0
    id_to_at_reference_checks_[referenced_from_inst.id()].push_back(std::bind(
2624
0
        &BuiltInsValidator::ValidateTessLevelAtReference, this, decoration,
2625
0
        built_in_inst, referenced_from_inst, std::placeholders::_1));
2626
0
  }
2627
2628
0
  return SPV_SUCCESS;
2629
0
}
2630
2631
spv_result_t BuiltInsValidator::ValidateVertexIndexAtDefinition(
2632
0
    const Decoration& decoration, const Instruction& inst) {
2633
0
  if (spvIsVulkanEnv(_.context()->target_env)) {
2634
0
    if (spv_result_t error = ValidateI32(
2635
0
            decoration, inst,
2636
0
            [this, &inst](const std::string& message) -> spv_result_t {
2637
0
              return _.diag(SPV_ERROR_INVALID_DATA, &inst)
2638
0
                     << _.VkErrorID(4400) << "According to the "
2639
0
                     << spvLogStringForEnv(_.context()->target_env)
2640
0
                     << " spec BuiltIn VertexIndex variable needs to be a "
2641
0
                        "32-bit int scalar. "
2642
0
                     << message;
2643
0
            })) {
2644
0
      return error;
2645
0
    }
2646
0
  }
2647
2648
  // Seed at reference checks with this built-in.
2649
0
  return ValidateVertexIndexAtReference(decoration, inst, inst, inst);
2650
0
}
2651
2652
spv_result_t BuiltInsValidator::ValidateVertexIdAtDefinition(
2653
0
    const Decoration& decoration, const Instruction& inst) {
2654
0
  (void)decoration;
2655
0
  if (spvIsVulkanEnv(_.context()->target_env)) {
2656
0
    return _.diag(SPV_ERROR_INVALID_DATA, &inst)
2657
0
           << "Vulkan spec doesn't allow BuiltIn VertexId "
2658
0
              "to be used.";
2659
0
  }
2660
2661
0
  return SPV_SUCCESS;
2662
0
}
2663
2664
spv_result_t BuiltInsValidator::ValidateLocalInvocationIndexAtDefinition(
2665
0
    const Decoration& decoration, const Instruction& inst) {
2666
  // Seed at reference checks with this built-in.
2667
0
  return ValidateLocalInvocationIndexAtReference(decoration, inst, inst, inst);
2668
0
}
2669
2670
spv_result_t BuiltInsValidator::ValidateLocalInvocationIndexAtReference(
2671
    const Decoration& decoration, const Instruction& built_in_inst,
2672
    const Instruction&,
2673
0
    const Instruction& referenced_from_inst) {
2674
0
  if (function_id_ == 0) {
2675
    // Propagate this rule to all dependant ids in the global scope.
2676
0
    id_to_at_reference_checks_[referenced_from_inst.id()].push_back(
2677
0
        std::bind(&BuiltInsValidator::ValidateLocalInvocationIndexAtReference,
2678
0
                  this, decoration, built_in_inst, referenced_from_inst,
2679
0
                  std::placeholders::_1));
2680
0
  }
2681
2682
0
  return SPV_SUCCESS;
2683
0
}
2684
2685
spv_result_t BuiltInsValidator::ValidateVertexIndexAtReference(
2686
    const Decoration& decoration, const Instruction& built_in_inst,
2687
    const Instruction& referenced_inst,
2688
0
    const Instruction& referenced_from_inst) {
2689
0
  if (spvIsVulkanEnv(_.context()->target_env)) {
2690
0
    const spv::StorageClass storage_class = GetStorageClass(referenced_from_inst);
2691
0
    if (storage_class != spv::StorageClass::Max &&
2692
0
        storage_class != spv::StorageClass::Input) {
2693
0
      return _.diag(SPV_ERROR_INVALID_DATA, &referenced_from_inst)
2694
0
             << _.VkErrorID(4399) << spvLogStringForEnv(_.context()->target_env)
2695
0
             << " spec allows BuiltIn VertexIndex to be only used for "
2696
0
                "variables with Input storage class. "
2697
0
             << GetReferenceDesc(decoration, built_in_inst, referenced_inst,
2698
0
                                 referenced_from_inst)
2699
0
             << " " << GetStorageClassDesc(referenced_from_inst);
2700
0
    }
2701
2702
0
    for (const spv::ExecutionModel execution_model : execution_models_) {
2703
0
      if (execution_model != spv::ExecutionModel::Vertex) {
2704
0
        return _.diag(SPV_ERROR_INVALID_DATA, &referenced_from_inst)
2705
0
               << _.VkErrorID(4398)
2706
0
               << spvLogStringForEnv(_.context()->target_env)
2707
0
               << " spec allows BuiltIn VertexIndex to be used only with "
2708
0
                  "Vertex execution model. "
2709
0
               << GetReferenceDesc(decoration, built_in_inst, referenced_inst,
2710
0
                                   referenced_from_inst, execution_model);
2711
0
      }
2712
0
    }
2713
0
  }
2714
2715
0
  if (function_id_ == 0) {
2716
    // Propagate this rule to all dependant ids in the global scope.
2717
0
    id_to_at_reference_checks_[referenced_from_inst.id()].push_back(std::bind(
2718
0
        &BuiltInsValidator::ValidateVertexIndexAtReference, this, decoration,
2719
0
        built_in_inst, referenced_from_inst, std::placeholders::_1));
2720
0
  }
2721
2722
0
  return SPV_SUCCESS;
2723
0
}
2724
2725
spv_result_t BuiltInsValidator::ValidateLayerOrViewportIndexAtDefinition(
2726
0
    const Decoration& decoration, const Instruction& inst) {
2727
0
  if (spvIsVulkanEnv(_.context()->target_env)) {
2728
    // This can be a per-primitive variable for mesh shader stage.
2729
    // In such cases variable will have an array of 32-bit integers.
2730
0
    if (decoration.struct_member_index() != Decoration::kInvalidMember) {
2731
      // This must be a 32-bit int scalar.
2732
0
      if (spv_result_t error = ValidateI32(
2733
0
              decoration, inst,
2734
0
              [this, &decoration,
2735
0
               &inst](const std::string& message) -> spv_result_t {
2736
0
                uint32_t vuid =
2737
0
                    (decoration.builtin() == spv::BuiltIn::Layer) ? 4276 : 4408;
2738
0
                return _.diag(SPV_ERROR_INVALID_DATA, &inst)
2739
0
                       << _.VkErrorID(vuid)
2740
0
                       << "According to the Vulkan spec BuiltIn "
2741
0
                       << _.grammar().lookupOperandName(
2742
0
                              SPV_OPERAND_TYPE_BUILT_IN,
2743
0
                              (uint32_t)decoration.builtin())
2744
0
                       << "variable needs to be a 32-bit int scalar. "
2745
0
                       << message;
2746
0
              })) {
2747
0
        return error;
2748
0
      }
2749
0
    } else {
2750
0
      if (spv_result_t error = ValidateOptionalArrayedI32(
2751
0
              decoration, inst,
2752
0
              [this, &decoration,
2753
0
               &inst](const std::string& message) -> spv_result_t {
2754
0
                uint32_t vuid =
2755
0
                    (decoration.builtin() == spv::BuiltIn::Layer) ? 4276 : 4408;
2756
0
                return _.diag(SPV_ERROR_INVALID_DATA, &inst)
2757
0
                       << _.VkErrorID(vuid)
2758
0
                       << "According to the Vulkan spec BuiltIn "
2759
0
                       << _.grammar().lookupOperandName(
2760
0
                              SPV_OPERAND_TYPE_BUILT_IN,
2761
0
                              (uint32_t)decoration.builtin())
2762
0
                       << "variable needs to be a 32-bit int scalar. "
2763
0
                       << message;
2764
0
              })) {
2765
0
        return error;
2766
0
      }
2767
0
    }
2768
0
  }
2769
2770
  // Seed at reference checks with this built-in.
2771
0
  return ValidateLayerOrViewportIndexAtReference(decoration, inst, inst, inst);
2772
0
}
2773
2774
spv_result_t BuiltInsValidator::ValidateLayerOrViewportIndexAtReference(
2775
    const Decoration& decoration, const Instruction& built_in_inst,
2776
    const Instruction& referenced_inst,
2777
0
    const Instruction& referenced_from_inst) {
2778
0
  uint32_t operand = (uint32_t)decoration.builtin();
2779
0
  if (spvIsVulkanEnv(_.context()->target_env)) {
2780
0
    const spv::StorageClass storage_class = GetStorageClass(referenced_from_inst);
2781
0
    if (storage_class != spv::StorageClass::Max &&
2782
0
        storage_class != spv::StorageClass::Input &&
2783
0
        storage_class != spv::StorageClass::Output) {
2784
0
      return _.diag(SPV_ERROR_INVALID_DATA, &referenced_from_inst)
2785
0
             << "Vulkan spec allows BuiltIn "
2786
0
             << _.grammar().lookupOperandName(SPV_OPERAND_TYPE_BUILT_IN,
2787
0
                                              operand)
2788
0
             << " to be only used for variables with Input or Output storage "
2789
0
                "class. "
2790
0
             << GetReferenceDesc(decoration, built_in_inst, referenced_inst,
2791
0
                                 referenced_from_inst)
2792
0
             << " " << GetStorageClassDesc(referenced_from_inst);
2793
0
    }
2794
2795
0
    if (storage_class == spv::StorageClass::Input) {
2796
0
      assert(function_id_ == 0);
2797
0
      for (const auto em :
2798
0
           {spv::ExecutionModel::Vertex, spv::ExecutionModel::TessellationEvaluation,
2799
0
            spv::ExecutionModel::Geometry, spv::ExecutionModel::MeshNV,
2800
0
            spv::ExecutionModel::MeshEXT}) {
2801
0
        id_to_at_reference_checks_[referenced_from_inst.id()].push_back(
2802
0
            std::bind(&BuiltInsValidator::ValidateNotCalledWithExecutionModel,
2803
0
                      this, ((spv::BuiltIn(operand) == spv::BuiltIn::Layer) ? 4274 : 4406),
2804
0
                      "Vulkan spec doesn't allow BuiltIn Layer and "
2805
0
                      "ViewportIndex to be "
2806
0
                      "used for variables with Input storage class if "
2807
0
                      "execution model is Vertex, TessellationEvaluation, "
2808
0
                      "Geometry, MeshNV or MeshEXT.",
2809
0
                      em, decoration, built_in_inst, referenced_from_inst,
2810
0
                      std::placeholders::_1));
2811
0
      }
2812
0
    }
2813
2814
0
    if (storage_class == spv::StorageClass::Output) {
2815
0
      assert(function_id_ == 0);
2816
0
      id_to_at_reference_checks_[referenced_from_inst.id()].push_back(
2817
0
          std::bind(&BuiltInsValidator::ValidateNotCalledWithExecutionModel,
2818
0
                    this, ((spv::BuiltIn(operand) == spv::BuiltIn::Layer) ? 4275 : 4407),
2819
0
                    "Vulkan spec doesn't allow BuiltIn Layer and "
2820
0
                    "ViewportIndex to be "
2821
0
                    "used for variables with Output storage class if "
2822
0
                    "execution model is "
2823
0
                    "Fragment.",
2824
0
                    spv::ExecutionModel::Fragment, decoration, built_in_inst,
2825
0
                    referenced_from_inst, std::placeholders::_1));
2826
0
    }
2827
2828
0
    for (const spv::ExecutionModel execution_model : execution_models_) {
2829
0
      switch (execution_model) {
2830
0
        case spv::ExecutionModel::Geometry:
2831
0
        case spv::ExecutionModel::Fragment:
2832
0
        case spv::ExecutionModel::MeshNV:
2833
0
        case spv::ExecutionModel::MeshEXT:
2834
          // Ok.
2835
0
          break;
2836
0
        case spv::ExecutionModel::Vertex:
2837
0
        case spv::ExecutionModel::TessellationEvaluation: {
2838
0
          if (!_.HasCapability(spv::Capability::ShaderViewportIndexLayerEXT)) {
2839
0
            if (spv::BuiltIn(operand) == spv::BuiltIn::ViewportIndex &&
2840
0
                _.HasCapability(spv::Capability::ShaderViewportIndex))
2841
0
              break;  // Ok
2842
0
            if (spv::BuiltIn(operand) == spv::BuiltIn::Layer &&
2843
0
                _.HasCapability(spv::Capability::ShaderLayer))
2844
0
              break;  // Ok
2845
2846
0
            const char* capability = "ShaderViewportIndexLayerEXT";
2847
2848
0
            if (spv::BuiltIn(operand) == spv::BuiltIn::ViewportIndex)
2849
0
              capability = "ShaderViewportIndexLayerEXT or ShaderViewportIndex";
2850
0
            if (spv::BuiltIn(operand) == spv::BuiltIn::Layer)
2851
0
              capability = "ShaderViewportIndexLayerEXT or ShaderLayer";
2852
2853
0
            uint32_t vuid = (spv::BuiltIn(operand) == spv::BuiltIn::Layer) ? 4273 : 4405;
2854
0
            return _.diag(SPV_ERROR_INVALID_DATA, &referenced_from_inst)
2855
0
                   << _.VkErrorID(vuid) << "Using BuiltIn "
2856
0
                   << _.grammar().lookupOperandName(SPV_OPERAND_TYPE_BUILT_IN,
2857
0
                                                    operand)
2858
0
                   << " in Vertex or Tessellation execution model requires the "
2859
0
                   << capability << " capability.";
2860
0
          }
2861
0
          break;
2862
0
        }
2863
0
        default: {
2864
0
          uint32_t vuid = (spv::BuiltIn(operand) == spv::BuiltIn::Layer) ? 4272 : 4404;
2865
0
          return _.diag(SPV_ERROR_INVALID_DATA, &referenced_from_inst)
2866
0
                 << _.VkErrorID(vuid) << "Vulkan spec allows BuiltIn "
2867
0
                 << _.grammar().lookupOperandName(SPV_OPERAND_TYPE_BUILT_IN,
2868
0
                                                  operand)
2869
0
                 << " to be used only with Vertex, TessellationEvaluation, "
2870
0
                    "Geometry, or Fragment execution models. "
2871
0
                 << GetReferenceDesc(decoration, built_in_inst, referenced_inst,
2872
0
                                     referenced_from_inst, execution_model);
2873
0
        }
2874
0
      }
2875
0
    }
2876
0
  }
2877
2878
0
  if (function_id_ == 0) {
2879
    // Propagate this rule to all dependant ids in the global scope.
2880
0
    id_to_at_reference_checks_[referenced_from_inst.id()].push_back(
2881
0
        std::bind(&BuiltInsValidator::ValidateLayerOrViewportIndexAtReference,
2882
0
                  this, decoration, built_in_inst, referenced_from_inst,
2883
0
                  std::placeholders::_1));
2884
0
  }
2885
2886
0
  return SPV_SUCCESS;
2887
0
}
2888
2889
spv_result_t BuiltInsValidator::ValidateFragmentShaderF32Vec3InputAtDefinition(
2890
0
    const Decoration& decoration, const Instruction& inst) {
2891
0
  if (spvIsVulkanEnv(_.context()->target_env)) {
2892
0
    const spv::BuiltIn builtin = decoration.builtin();
2893
0
    if (spv_result_t error = ValidateF32Vec(
2894
0
            decoration, inst, 3,
2895
0
            [this, &inst, builtin](const std::string& message) -> spv_result_t {
2896
0
              uint32_t vuid = GetVUIDForBuiltin(builtin, VUIDErrorType);
2897
0
              return _.diag(SPV_ERROR_INVALID_DATA, &inst)
2898
0
                     << _.VkErrorID(vuid) << "According to the "
2899
0
                     << spvLogStringForEnv(_.context()->target_env)
2900
0
                     << " spec BuiltIn "
2901
0
                     << _.grammar().lookupOperandName(SPV_OPERAND_TYPE_BUILT_IN,
2902
0
                                                      uint32_t(builtin))
2903
0
                     << " variable needs to be a 3-component 32-bit float "
2904
0
                        "vector. "
2905
0
                     << message;
2906
0
            })) {
2907
0
      return error;
2908
0
    }
2909
0
  }
2910
2911
  // Seed at reference checks with this built-in.
2912
0
  return ValidateFragmentShaderF32Vec3InputAtReference(decoration, inst, inst,
2913
0
                                                      inst);
2914
0
}
2915
2916
spv_result_t BuiltInsValidator::ValidateFragmentShaderF32Vec3InputAtReference(
2917
    const Decoration& decoration, const Instruction& built_in_inst,
2918
    const Instruction& referenced_inst,
2919
0
    const Instruction& referenced_from_inst) {
2920
2921
0
  if (spvIsVulkanEnv(_.context()->target_env)) {
2922
0
    const spv::BuiltIn builtin = decoration.builtin();
2923
0
    const spv::StorageClass storage_class = GetStorageClass(referenced_from_inst);
2924
0
    if (storage_class != spv::StorageClass::Max &&
2925
0
        storage_class != spv::StorageClass::Input) {
2926
0
      uint32_t vuid = GetVUIDForBuiltin(builtin, VUIDErrorStorageClass);
2927
0
      return _.diag(SPV_ERROR_INVALID_DATA, &referenced_from_inst)
2928
0
             << _.VkErrorID(vuid) << spvLogStringForEnv(_.context()->target_env)
2929
0
             << " spec allows BuiltIn "
2930
0
             << _.grammar().lookupOperandName(SPV_OPERAND_TYPE_BUILT_IN, uint32_t(builtin))
2931
0
             << " to be only used for variables with Input storage class. "
2932
0
             << GetReferenceDesc(decoration, built_in_inst, referenced_inst,
2933
0
                                 referenced_from_inst)
2934
0
             << " " << GetStorageClassDesc(referenced_from_inst);
2935
0
    }
2936
2937
0
    for (const spv::ExecutionModel execution_model : execution_models_) {
2938
0
      if (execution_model != spv::ExecutionModel::Fragment) {
2939
0
        uint32_t vuid = GetVUIDForBuiltin(builtin, VUIDErrorExecutionModel);
2940
0
        return _.diag(SPV_ERROR_INVALID_DATA, &referenced_from_inst)
2941
0
               << _.VkErrorID(vuid)
2942
0
               << spvLogStringForEnv(_.context()->target_env)
2943
0
               << " spec allows BuiltIn "
2944
0
               << _.grammar().lookupOperandName(SPV_OPERAND_TYPE_BUILT_IN, uint32_t(builtin))
2945
0
               << " to be used only with Fragment execution model. "
2946
0
               << GetReferenceDesc(decoration, built_in_inst, referenced_inst,
2947
0
                                   referenced_from_inst, execution_model);
2948
0
      }
2949
0
    }
2950
0
  }
2951
2952
0
  if (function_id_ == 0) {
2953
    // Propagate this rule to all dependant ids in the global scope.
2954
0
    id_to_at_reference_checks_[referenced_from_inst.id()].push_back(std::bind(
2955
0
        &BuiltInsValidator::ValidateFragmentShaderF32Vec3InputAtReference, this,
2956
0
        decoration, built_in_inst, referenced_from_inst,
2957
0
        std::placeholders::_1));
2958
0
  }
2959
2960
0
  return SPV_SUCCESS;
2961
0
}
2962
2963
spv_result_t BuiltInsValidator::ValidateComputeShaderI32Vec3InputAtDefinition(
2964
0
    const Decoration& decoration, const Instruction& inst) {
2965
0
  if (spvIsVulkanEnv(_.context()->target_env)) {
2966
0
    const spv::BuiltIn builtin = decoration.builtin();
2967
0
    if (spv_result_t error = ValidateI32Vec(
2968
0
            decoration, inst, 3,
2969
0
            [this, &inst, builtin](const std::string& message) -> spv_result_t {
2970
0
              uint32_t vuid = GetVUIDForBuiltin(builtin, VUIDErrorType);
2971
0
              return _.diag(SPV_ERROR_INVALID_DATA, &inst)
2972
0
                     << _.VkErrorID(vuid) << "According to the "
2973
0
                     << spvLogStringForEnv(_.context()->target_env)
2974
0
                     << " spec BuiltIn "
2975
0
                     << _.grammar().lookupOperandName(SPV_OPERAND_TYPE_BUILT_IN,
2976
0
                                                      uint32_t(builtin))
2977
0
                     << " variable needs to be a 3-component 32-bit int "
2978
0
                        "vector. "
2979
0
                     << message;
2980
0
            })) {
2981
0
      return error;
2982
0
    }
2983
0
  }
2984
2985
  // Seed at reference checks with this built-in.
2986
0
  return ValidateComputeShaderI32Vec3InputAtReference(decoration, inst, inst,
2987
0
                                                      inst);
2988
0
}
2989
2990
spv_result_t BuiltInsValidator::ValidateComputeShaderI32Vec3InputAtReference(
2991
    const Decoration& decoration, const Instruction& built_in_inst,
2992
    const Instruction& referenced_inst,
2993
0
    const Instruction& referenced_from_inst) {
2994
0
  if (spvIsVulkanEnv(_.context()->target_env)) {
2995
0
    const spv::BuiltIn builtin = decoration.builtin();
2996
0
    const spv::StorageClass storage_class = GetStorageClass(referenced_from_inst);
2997
0
    if (storage_class != spv::StorageClass::Max &&
2998
0
        storage_class != spv::StorageClass::Input) {
2999
0
      uint32_t vuid = GetVUIDForBuiltin(builtin, VUIDErrorStorageClass);
3000
0
      return _.diag(SPV_ERROR_INVALID_DATA, &referenced_from_inst)
3001
0
             << _.VkErrorID(vuid) << spvLogStringForEnv(_.context()->target_env)
3002
0
             << " spec allows BuiltIn "
3003
0
             << _.grammar().lookupOperandName(SPV_OPERAND_TYPE_BUILT_IN, uint32_t(builtin))
3004
0
             << " to be only used for variables with Input storage class. "
3005
0
             << GetReferenceDesc(decoration, built_in_inst, referenced_inst,
3006
0
                                 referenced_from_inst)
3007
0
             << " " << GetStorageClassDesc(referenced_from_inst);
3008
0
    }
3009
3010
0
    for (const spv::ExecutionModel execution_model : execution_models_) {
3011
0
      bool has_vulkan_model = execution_model == spv::ExecutionModel::GLCompute ||
3012
0
                              execution_model == spv::ExecutionModel::TaskNV ||
3013
0
                              execution_model == spv::ExecutionModel::MeshNV ||
3014
0
                              execution_model == spv::ExecutionModel::TaskEXT ||
3015
0
                              execution_model == spv::ExecutionModel::MeshEXT;
3016
3017
0
      if (spvIsVulkanEnv(_.context()->target_env) && !has_vulkan_model) {
3018
0
        uint32_t vuid = GetVUIDForBuiltin(builtin, VUIDErrorExecutionModel);
3019
0
        return _.diag(SPV_ERROR_INVALID_DATA, &referenced_from_inst)
3020
0
               << _.VkErrorID(vuid)
3021
0
               << spvLogStringForEnv(_.context()->target_env)
3022
0
               << " spec allows BuiltIn "
3023
0
               << _.grammar().lookupOperandName(SPV_OPERAND_TYPE_BUILT_IN, uint32_t(builtin))
3024
0
               << " to be used only with GLCompute, MeshNV, TaskNV, MeshEXT or"
3025
0
               << " TaskEXT execution model. "
3026
0
               << GetReferenceDesc(decoration, built_in_inst, referenced_inst,
3027
0
                                   referenced_from_inst, execution_model);
3028
0
      }
3029
0
    }
3030
0
  }
3031
3032
0
  if (function_id_ == 0) {
3033
    // Propagate this rule to all dependant ids in the global scope.
3034
0
    id_to_at_reference_checks_[referenced_from_inst.id()].push_back(std::bind(
3035
0
        &BuiltInsValidator::ValidateComputeShaderI32Vec3InputAtReference, this,
3036
0
        decoration, built_in_inst, referenced_from_inst,
3037
0
        std::placeholders::_1));
3038
0
  }
3039
3040
0
  return SPV_SUCCESS;
3041
0
}
3042
3043
spv_result_t BuiltInsValidator::ValidateComputeI32InputAtDefinition(
3044
0
    const Decoration& decoration, const Instruction& inst) {
3045
0
  if (spvIsVulkanEnv(_.context()->target_env)) {
3046
0
    const spv::BuiltIn builtin = decoration.builtin();
3047
0
    if (decoration.struct_member_index() != Decoration::kInvalidMember) {
3048
0
      return _.diag(SPV_ERROR_INVALID_DATA, &inst)
3049
0
             << "BuiltIn "
3050
0
             << _.grammar().lookupOperandName(SPV_OPERAND_TYPE_BUILT_IN, uint32_t(builtin))
3051
0
             << " cannot be used as a member decoration ";
3052
0
    }
3053
0
    if (spv_result_t error = ValidateI32(
3054
0
            decoration, inst,
3055
0
            [this, &inst, builtin](const std::string& message) -> spv_result_t {
3056
0
              uint32_t vuid = GetVUIDForBuiltin(builtin, VUIDErrorType);
3057
0
              return _.diag(SPV_ERROR_INVALID_DATA, &inst)
3058
0
                     << _.VkErrorID(vuid)
3059
0
                     << "According to the "
3060
0
                     << spvLogStringForEnv(_.context()->target_env)
3061
0
                     << " spec BuiltIn "
3062
0
                     << _.grammar().lookupOperandName(SPV_OPERAND_TYPE_BUILT_IN, uint32_t(builtin))
3063
0
                     << " variable needs to be a 32-bit int "
3064
0
                        "vector. "
3065
0
                     << message;
3066
0
            })) {
3067
0
      return error;
3068
0
    }
3069
0
  }
3070
3071
  // Seed at reference checks with this built-in.
3072
0
  return ValidateComputeI32InputAtReference(decoration, inst, inst, inst);
3073
0
}
3074
3075
spv_result_t BuiltInsValidator::ValidateComputeI32InputAtReference(
3076
    const Decoration& decoration, const Instruction& built_in_inst,
3077
    const Instruction& referenced_inst,
3078
0
    const Instruction& referenced_from_inst) {
3079
0
  if (spvIsVulkanEnv(_.context()->target_env)) {
3080
0
    const spv::BuiltIn builtin = decoration.builtin();
3081
0
    const spv::StorageClass storage_class = GetStorageClass(referenced_from_inst);
3082
0
    if (storage_class != spv::StorageClass::Max &&
3083
0
        storage_class != spv::StorageClass::Input) {
3084
0
      uint32_t vuid = GetVUIDForBuiltin(builtin, VUIDErrorStorageClass);
3085
0
      return _.diag(SPV_ERROR_INVALID_DATA, &referenced_from_inst)
3086
0
             << _.VkErrorID(vuid)
3087
0
             << spvLogStringForEnv(_.context()->target_env)
3088
0
             << " spec allows BuiltIn "
3089
0
             << _.grammar().lookupOperandName(SPV_OPERAND_TYPE_BUILT_IN, uint32_t(builtin))
3090
0
             << " to be only used for variables with Input storage class. "
3091
0
             << GetReferenceDesc(decoration, built_in_inst, referenced_inst,
3092
0
                                 referenced_from_inst)
3093
0
             << " " << GetStorageClassDesc(referenced_from_inst);
3094
0
    }
3095
3096
0
    for (const spv::ExecutionModel execution_model : execution_models_) {
3097
0
      bool has_vulkan_model = execution_model == spv::ExecutionModel::GLCompute ||
3098
0
                              execution_model == spv::ExecutionModel::TaskNV ||
3099
0
                              execution_model == spv::ExecutionModel::MeshNV ||
3100
0
                              execution_model == spv::ExecutionModel::TaskEXT ||
3101
0
                              execution_model == spv::ExecutionModel::MeshEXT;
3102
0
      if (spvIsVulkanEnv(_.context()->target_env) && !has_vulkan_model) {
3103
0
        uint32_t vuid = GetVUIDForBuiltin(builtin, VUIDErrorExecutionModel);
3104
0
        return _.diag(SPV_ERROR_INVALID_DATA, &referenced_from_inst)
3105
0
               << _.VkErrorID(vuid)
3106
0
               << spvLogStringForEnv(_.context()->target_env)
3107
0
               << " spec allows BuiltIn "
3108
0
               << _.grammar().lookupOperandName(SPV_OPERAND_TYPE_BUILT_IN, uint32_t(builtin))
3109
0
               << " to be used only with GLCompute, MeshNV, TaskNV, MeshEXT or "
3110
0
               << "TaskEXT execution model. "
3111
0
               << GetReferenceDesc(decoration, built_in_inst, referenced_inst,
3112
0
                                   referenced_from_inst, execution_model);
3113
0
      }
3114
0
    }
3115
0
  }
3116
3117
0
  if (function_id_ == 0) {
3118
    // Propagate this rule to all dependant ids in the global scope.
3119
0
    id_to_at_reference_checks_[referenced_from_inst.id()].push_back(
3120
0
        std::bind(&BuiltInsValidator::ValidateComputeI32InputAtReference, this,
3121
0
                  decoration, built_in_inst, referenced_from_inst,
3122
0
                  std::placeholders::_1));
3123
0
  }
3124
3125
0
  return SPV_SUCCESS;
3126
0
}
3127
3128
spv_result_t BuiltInsValidator::ValidateI32InputAtDefinition(
3129
0
    const Decoration& decoration, const Instruction& inst) {
3130
0
  if (spvIsVulkanEnv(_.context()->target_env)) {
3131
0
    const spv::BuiltIn builtin = decoration.builtin();
3132
0
    if (decoration.struct_member_index() != Decoration::kInvalidMember) {
3133
0
      return _.diag(SPV_ERROR_INVALID_DATA, &inst)
3134
0
             << "BuiltIn "
3135
0
             << _.grammar().lookupOperandName(SPV_OPERAND_TYPE_BUILT_IN, uint32_t(builtin))
3136
0
             << " cannot be used as a member decoration ";
3137
0
    }
3138
0
    if (spv_result_t error = ValidateI32(
3139
0
            decoration, inst,
3140
0
            [this, &inst, builtin](const std::string& message) -> spv_result_t {
3141
0
              uint32_t vuid = GetVUIDForBuiltin(builtin, VUIDErrorType);
3142
0
              return _.diag(SPV_ERROR_INVALID_DATA, &inst)
3143
0
                     << _.VkErrorID(vuid)
3144
0
                     << "According to the "
3145
0
                     << spvLogStringForEnv(_.context()->target_env)
3146
0
                     << " spec BuiltIn "
3147
0
                     << _.grammar().lookupOperandName(SPV_OPERAND_TYPE_BUILT_IN, uint32_t(builtin))
3148
0
                     << " variable needs to be a 32-bit int. " << message;
3149
0
            })) {
3150
0
      return error;
3151
0
    }
3152
3153
0
    const spv::StorageClass storage_class = GetStorageClass(inst);
3154
0
    if (storage_class != spv::StorageClass::Max &&
3155
0
        storage_class != spv::StorageClass::Input) {
3156
0
      uint32_t vuid = GetVUIDForBuiltin(builtin, VUIDErrorStorageClass);
3157
0
      return _.diag(SPV_ERROR_INVALID_DATA, &inst)
3158
0
             << _.VkErrorID(vuid)
3159
0
             << spvLogStringForEnv(_.context()->target_env)
3160
0
             << " spec allows BuiltIn "
3161
0
             << _.grammar().lookupOperandName(SPV_OPERAND_TYPE_BUILT_IN, uint32_t(builtin))
3162
0
             << " to be only used for variables with Input storage class. "
3163
0
             << GetReferenceDesc(decoration, inst, inst, inst) << " "
3164
0
             << GetStorageClassDesc(inst);
3165
0
    }
3166
0
  }
3167
3168
0
  return SPV_SUCCESS;
3169
0
}
3170
3171
spv_result_t BuiltInsValidator::ValidateI32Vec4InputAtDefinition(
3172
0
    const Decoration& decoration, const Instruction& inst) {
3173
0
  if (spvIsVulkanEnv(_.context()->target_env)) {
3174
0
    const spv::BuiltIn builtin = decoration.builtin();
3175
0
    if (decoration.struct_member_index() != Decoration::kInvalidMember) {
3176
0
      return _.diag(SPV_ERROR_INVALID_DATA, &inst)
3177
0
             << "BuiltIn "
3178
0
             << _.grammar().lookupOperandName(SPV_OPERAND_TYPE_BUILT_IN, uint32_t(builtin))
3179
0
             << " cannot be used as a member decoration ";
3180
0
    }
3181
0
    if (spv_result_t error = ValidateI32Vec(
3182
0
            decoration, inst, 4,
3183
0
            [this, &inst, builtin](const std::string& message) -> spv_result_t {
3184
0
              uint32_t vuid = GetVUIDForBuiltin(builtin, VUIDErrorType);
3185
0
              return _.diag(SPV_ERROR_INVALID_DATA, &inst)
3186
0
                     << _.VkErrorID(vuid)
3187
0
                     << "According to the "
3188
0
                     << spvLogStringForEnv(_.context()->target_env)
3189
0
                     << " spec BuiltIn "
3190
0
                     << _.grammar().lookupOperandName(SPV_OPERAND_TYPE_BUILT_IN, uint32_t(builtin))
3191
0
                     << " variable needs to be a 4-component 32-bit int "
3192
0
                        "vector. "
3193
0
                     << message;
3194
0
            })) {
3195
0
      return error;
3196
0
    }
3197
3198
0
    const spv::StorageClass storage_class = GetStorageClass(inst);
3199
0
    if (storage_class != spv::StorageClass::Max &&
3200
0
        storage_class != spv::StorageClass::Input) {
3201
0
      uint32_t vuid = GetVUIDForBuiltin(builtin, VUIDErrorStorageClass);
3202
0
      return _.diag(SPV_ERROR_INVALID_DATA, &inst)
3203
0
             << _.VkErrorID(vuid)
3204
0
             << spvLogStringForEnv(_.context()->target_env)
3205
0
             << " spec allows BuiltIn "
3206
0
             << _.grammar().lookupOperandName(SPV_OPERAND_TYPE_BUILT_IN, uint32_t(builtin))
3207
0
             << " to be only used for variables with Input storage class. "
3208
0
             << GetReferenceDesc(decoration, inst, inst, inst) << " "
3209
0
             << GetStorageClassDesc(inst);
3210
0
    }
3211
0
  }
3212
3213
0
  return SPV_SUCCESS;
3214
0
}
3215
3216
spv_result_t BuiltInsValidator::ValidateWorkgroupSizeAtDefinition(
3217
0
    const Decoration& decoration, const Instruction& inst) {
3218
0
  if (spvIsVulkanEnv(_.context()->target_env)) {
3219
0
    if (spvIsVulkanEnv(_.context()->target_env) &&
3220
0
        !spvOpcodeIsConstant(inst.opcode())) {
3221
0
      return _.diag(SPV_ERROR_INVALID_DATA, &inst)
3222
0
             << _.VkErrorID(4426)
3223
0
             << "Vulkan spec requires BuiltIn WorkgroupSize to be a "
3224
0
                "constant. "
3225
0
             << GetIdDesc(inst) << " is not a constant.";
3226
0
    }
3227
3228
0
    if (spv_result_t error = ValidateI32Vec(
3229
0
            decoration, inst, 3,
3230
0
            [this, &inst](const std::string& message) -> spv_result_t {
3231
0
              return _.diag(SPV_ERROR_INVALID_DATA, &inst)
3232
0
                     << _.VkErrorID(4427) << "According to the "
3233
0
                     << spvLogStringForEnv(_.context()->target_env)
3234
0
                     << " spec BuiltIn WorkgroupSize variable needs to be a "
3235
0
                        "3-component 32-bit int vector. "
3236
0
                     << message;
3237
0
            })) {
3238
0
      return error;
3239
0
    }
3240
0
  }
3241
3242
  // Seed at reference checks with this built-in.
3243
0
  return ValidateWorkgroupSizeAtReference(decoration, inst, inst, inst);
3244
0
}
3245
3246
spv_result_t BuiltInsValidator::ValidateWorkgroupSizeAtReference(
3247
    const Decoration& decoration, const Instruction& built_in_inst,
3248
    const Instruction& referenced_inst,
3249
0
    const Instruction& referenced_from_inst) {
3250
0
  if (spvIsVulkanEnv(_.context()->target_env)) {
3251
0
    for (const spv::ExecutionModel execution_model : execution_models_) {
3252
0
      if (execution_model != spv::ExecutionModel::GLCompute &&
3253
0
          execution_model != spv::ExecutionModel::TaskNV &&
3254
0
          execution_model != spv::ExecutionModel::MeshNV &&
3255
0
          execution_model != spv::ExecutionModel::TaskEXT &&
3256
0
          execution_model != spv::ExecutionModel::MeshEXT) {
3257
0
        return _.diag(SPV_ERROR_INVALID_DATA, &referenced_from_inst)
3258
0
               << _.VkErrorID(4425)
3259
0
               << spvLogStringForEnv(_.context()->target_env)
3260
0
               << " spec allows BuiltIn "
3261
0
               << _.grammar().lookupOperandName(SPV_OPERAND_TYPE_BUILT_IN,
3262
0
                                                (uint32_t)decoration.builtin())
3263
0
               << " to be used only with GLCompute, MeshNV, TaskNV, MeshEXT or "
3264
0
               << "TaskEXT execution model. "
3265
0
               << GetReferenceDesc(decoration, built_in_inst, referenced_inst,
3266
0
                                   referenced_from_inst, execution_model);
3267
0
      }
3268
0
    }
3269
0
  }
3270
3271
0
  if (function_id_ == 0) {
3272
    // Propagate this rule to all dependant ids in the global scope.
3273
0
    id_to_at_reference_checks_[referenced_from_inst.id()].push_back(std::bind(
3274
0
        &BuiltInsValidator::ValidateWorkgroupSizeAtReference, this, decoration,
3275
0
        built_in_inst, referenced_from_inst, std::placeholders::_1));
3276
0
  }
3277
3278
0
  return SPV_SUCCESS;
3279
0
}
3280
3281
spv_result_t BuiltInsValidator::ValidateBaseInstanceOrVertexAtDefinition(
3282
0
    const Decoration& decoration, const Instruction& inst) {
3283
0
  if (spvIsVulkanEnv(_.context()->target_env)) {
3284
0
    if (spv_result_t error = ValidateI32(
3285
0
            decoration, inst,
3286
0
            [this, &inst,
3287
0
             &decoration](const std::string& message) -> spv_result_t {
3288
0
              uint32_t vuid =
3289
0
                  (decoration.builtin() == spv::BuiltIn::BaseInstance) ? 4183
3290
0
                                                                       : 4186;
3291
0
              return _.diag(SPV_ERROR_INVALID_DATA, &inst)
3292
0
                     << _.VkErrorID(vuid)
3293
0
                     << "According to the Vulkan spec BuiltIn "
3294
0
                     << _.grammar().lookupOperandName(
3295
0
                            SPV_OPERAND_TYPE_BUILT_IN,
3296
0
                            (uint32_t)decoration.builtin())
3297
0
                     << " variable needs to be a 32-bit int scalar. "
3298
0
                     << message;
3299
0
            })) {
3300
0
      return error;
3301
0
    }
3302
0
  }
3303
3304
0
  return ValidateBaseInstanceOrVertexAtReference(decoration, inst, inst, inst);
3305
0
}
3306
3307
spv_result_t BuiltInsValidator::ValidateBaseInstanceOrVertexAtReference(
3308
    const Decoration& decoration, const Instruction& built_in_inst,
3309
    const Instruction& referenced_inst,
3310
0
    const Instruction& referenced_from_inst) {
3311
0
  uint32_t operand = (uint32_t)decoration.builtin();
3312
0
  if (spvIsVulkanEnv(_.context()->target_env)) {
3313
0
    const spv::StorageClass storage_class = GetStorageClass(referenced_from_inst);
3314
0
    if (storage_class != spv::StorageClass::Max &&
3315
0
        storage_class != spv::StorageClass::Input) {
3316
0
      uint32_t vuid = (spv::BuiltIn(operand) == spv::BuiltIn::BaseInstance) ? 4182 : 4185;
3317
0
      return _.diag(SPV_ERROR_INVALID_DATA, &referenced_from_inst)
3318
0
             << _.VkErrorID(vuid) << "Vulkan spec allows BuiltIn "
3319
0
             << _.grammar().lookupOperandName(SPV_OPERAND_TYPE_BUILT_IN,
3320
0
                                              operand)
3321
0
             << " to be only used for variables with Input storage class. "
3322
0
             << GetReferenceDesc(decoration, built_in_inst, referenced_inst,
3323
0
                                 referenced_from_inst)
3324
0
             << " " << GetStorageClassDesc(referenced_from_inst);
3325
0
    }
3326
3327
0
    for (const spv::ExecutionModel execution_model : execution_models_) {
3328
0
      if (execution_model != spv::ExecutionModel::Vertex) {
3329
0
        uint32_t vuid = (spv::BuiltIn(operand) == spv::BuiltIn::BaseInstance) ? 4181 : 4184;
3330
0
        return _.diag(SPV_ERROR_INVALID_DATA, &referenced_from_inst)
3331
0
               << _.VkErrorID(vuid) << "Vulkan spec allows BuiltIn "
3332
0
               << _.grammar().lookupOperandName(SPV_OPERAND_TYPE_BUILT_IN,
3333
0
                                                operand)
3334
0
               << " to be used only with Vertex execution model. "
3335
0
               << GetReferenceDesc(decoration, built_in_inst, referenced_inst,
3336
0
                                   referenced_from_inst, execution_model);
3337
0
      }
3338
0
    }
3339
0
  }
3340
3341
0
  if (function_id_ == 0) {
3342
    // Propagate this rule to all dependant ids in the global scope.
3343
0
    id_to_at_reference_checks_[referenced_from_inst.id()].push_back(
3344
0
        std::bind(&BuiltInsValidator::ValidateBaseInstanceOrVertexAtReference,
3345
0
                  this, decoration, built_in_inst, referenced_from_inst,
3346
0
                  std::placeholders::_1));
3347
0
  }
3348
3349
0
  return SPV_SUCCESS;
3350
0
}
3351
3352
spv_result_t BuiltInsValidator::ValidateDrawIndexAtDefinition(
3353
0
    const Decoration& decoration, const Instruction& inst) {
3354
0
  if (spvIsVulkanEnv(_.context()->target_env)) {
3355
0
    if (spv_result_t error = ValidateI32(
3356
0
            decoration, inst,
3357
0
            [this, &inst,
3358
0
             &decoration](const std::string& message) -> spv_result_t {
3359
0
              return _.diag(SPV_ERROR_INVALID_DATA, &inst)
3360
0
                     << _.VkErrorID(4209)
3361
0
                     << "According to the Vulkan spec BuiltIn "
3362
0
                     << _.grammar().lookupOperandName(
3363
0
                            SPV_OPERAND_TYPE_BUILT_IN,
3364
0
                            (uint32_t)decoration.builtin())
3365
0
                     << " variable needs to be a 32-bit int scalar. "
3366
0
                     << message;
3367
0
            })) {
3368
0
      return error;
3369
0
    }
3370
0
  }
3371
3372
0
  return ValidateDrawIndexAtReference(decoration, inst, inst, inst);
3373
0
}
3374
3375
spv_result_t BuiltInsValidator::ValidateDrawIndexAtReference(
3376
    const Decoration& decoration, const Instruction& built_in_inst,
3377
    const Instruction& referenced_inst,
3378
0
    const Instruction& referenced_from_inst) {
3379
0
  uint32_t operand = (uint32_t)decoration.builtin();
3380
0
  if (spvIsVulkanEnv(_.context()->target_env)) {
3381
0
    const spv::StorageClass storage_class = GetStorageClass(referenced_from_inst);
3382
0
    if (storage_class != spv::StorageClass::Max &&
3383
0
        storage_class != spv::StorageClass::Input) {
3384
0
      return _.diag(SPV_ERROR_INVALID_DATA, &referenced_from_inst)
3385
0
             << _.VkErrorID(4208) << "Vulkan spec allows BuiltIn "
3386
0
             << _.grammar().lookupOperandName(SPV_OPERAND_TYPE_BUILT_IN,
3387
0
                                              operand)
3388
0
             << " to be only used for variables with Input storage class. "
3389
0
             << GetReferenceDesc(decoration, built_in_inst, referenced_inst,
3390
0
                                 referenced_from_inst)
3391
0
             << " " << GetStorageClassDesc(referenced_from_inst);
3392
0
    }
3393
3394
0
    for (const spv::ExecutionModel execution_model : execution_models_) {
3395
0
      if (execution_model != spv::ExecutionModel::Vertex &&
3396
0
          execution_model != spv::ExecutionModel::MeshNV &&
3397
0
          execution_model != spv::ExecutionModel::TaskNV &&
3398
0
          execution_model != spv::ExecutionModel::MeshEXT &&
3399
0
          execution_model != spv::ExecutionModel::TaskEXT) {
3400
0
        return _.diag(SPV_ERROR_INVALID_DATA, &referenced_from_inst)
3401
0
               << _.VkErrorID(4207) << "Vulkan spec allows BuiltIn "
3402
0
               << _.grammar().lookupOperandName(SPV_OPERAND_TYPE_BUILT_IN,
3403
0
                                                operand)
3404
0
               << " to be used only with Vertex, MeshNV, TaskNV , MeshEXT or"
3405
0
               << " TaskEXT execution "
3406
0
                  "model. "
3407
0
               << GetReferenceDesc(decoration, built_in_inst, referenced_inst,
3408
0
                                   referenced_from_inst, execution_model);
3409
0
      }
3410
0
    }
3411
0
  }
3412
3413
0
  if (function_id_ == 0) {
3414
    // Propagate this rule to all dependant ids in the global scope.
3415
0
    id_to_at_reference_checks_[referenced_from_inst.id()].push_back(std::bind(
3416
0
        &BuiltInsValidator::ValidateDrawIndexAtReference, this, decoration,
3417
0
        built_in_inst, referenced_from_inst, std::placeholders::_1));
3418
0
  }
3419
3420
0
  return SPV_SUCCESS;
3421
0
}
3422
3423
spv_result_t BuiltInsValidator::ValidateViewIndexAtDefinition(
3424
0
    const Decoration& decoration, const Instruction& inst) {
3425
0
  if (spvIsVulkanEnv(_.context()->target_env)) {
3426
0
    if (spv_result_t error = ValidateI32(
3427
0
            decoration, inst,
3428
0
            [this, &inst,
3429
0
             &decoration](const std::string& message) -> spv_result_t {
3430
0
              return _.diag(SPV_ERROR_INVALID_DATA, &inst)
3431
0
                     << _.VkErrorID(4403)
3432
0
                     << "According to the Vulkan spec BuiltIn "
3433
0
                     << _.grammar().lookupOperandName(
3434
0
                            SPV_OPERAND_TYPE_BUILT_IN,
3435
0
                            (uint32_t)decoration.builtin())
3436
0
                     << " variable needs to be a 32-bit int scalar. "
3437
0
                     << message;
3438
0
            })) {
3439
0
      return error;
3440
0
    }
3441
0
  }
3442
3443
0
  return ValidateViewIndexAtReference(decoration, inst, inst, inst);
3444
0
}
3445
3446
spv_result_t BuiltInsValidator::ValidateViewIndexAtReference(
3447
    const Decoration& decoration, const Instruction& built_in_inst,
3448
    const Instruction& referenced_inst,
3449
0
    const Instruction& referenced_from_inst) {
3450
0
  uint32_t operand = (uint32_t)decoration.builtin();
3451
0
  if (spvIsVulkanEnv(_.context()->target_env)) {
3452
0
    const spv::StorageClass storage_class = GetStorageClass(referenced_from_inst);
3453
0
    if (storage_class != spv::StorageClass::Max &&
3454
0
        storage_class != spv::StorageClass::Input) {
3455
0
      return _.diag(SPV_ERROR_INVALID_DATA, &referenced_from_inst)
3456
0
             << _.VkErrorID(4402) << "Vulkan spec allows BuiltIn "
3457
0
             << _.grammar().lookupOperandName(SPV_OPERAND_TYPE_BUILT_IN,
3458
0
                                              operand)
3459
0
             << " to be only used for variables with Input storage class. "
3460
0
             << GetReferenceDesc(decoration, built_in_inst, referenced_inst,
3461
0
                                 referenced_from_inst)
3462
0
             << " " << GetStorageClassDesc(referenced_from_inst);
3463
0
    }
3464
3465
0
    for (const spv::ExecutionModel execution_model : execution_models_) {
3466
0
      if (execution_model == spv::ExecutionModel::GLCompute) {
3467
0
        return _.diag(SPV_ERROR_INVALID_DATA, &referenced_from_inst)
3468
0
               << _.VkErrorID(4401) << "Vulkan spec allows BuiltIn "
3469
0
               << _.grammar().lookupOperandName(SPV_OPERAND_TYPE_BUILT_IN,
3470
0
                                                operand)
3471
0
               << " to be not be used with GLCompute execution model. "
3472
0
               << GetReferenceDesc(decoration, built_in_inst, referenced_inst,
3473
0
                                   referenced_from_inst, execution_model);
3474
0
      }
3475
0
    }
3476
0
  }
3477
3478
0
  if (function_id_ == 0) {
3479
    // Propagate this rule to all dependant ids in the global scope.
3480
0
    id_to_at_reference_checks_[referenced_from_inst.id()].push_back(std::bind(
3481
0
        &BuiltInsValidator::ValidateViewIndexAtReference, this, decoration,
3482
0
        built_in_inst, referenced_from_inst, std::placeholders::_1));
3483
0
  }
3484
3485
0
  return SPV_SUCCESS;
3486
0
}
3487
3488
spv_result_t BuiltInsValidator::ValidateDeviceIndexAtDefinition(
3489
0
    const Decoration& decoration, const Instruction& inst) {
3490
0
  if (spvIsVulkanEnv(_.context()->target_env)) {
3491
0
    if (spv_result_t error = ValidateI32(
3492
0
            decoration, inst,
3493
0
            [this, &inst,
3494
0
             &decoration](const std::string& message) -> spv_result_t {
3495
0
              return _.diag(SPV_ERROR_INVALID_DATA, &inst)
3496
0
                     << _.VkErrorID(4206)
3497
0
                     << "According to the Vulkan spec BuiltIn "
3498
0
                     << _.grammar().lookupOperandName(
3499
0
                            SPV_OPERAND_TYPE_BUILT_IN,
3500
0
                            (uint32_t)decoration.builtin())
3501
0
                     << " variable needs to be a 32-bit int scalar. "
3502
0
                     << message;
3503
0
            })) {
3504
0
      return error;
3505
0
    }
3506
0
  }
3507
3508
0
  return ValidateDeviceIndexAtReference(decoration, inst, inst, inst);
3509
0
}
3510
3511
spv_result_t BuiltInsValidator::ValidateDeviceIndexAtReference(
3512
    const Decoration& decoration, const Instruction& built_in_inst,
3513
    const Instruction& referenced_inst,
3514
0
    const Instruction& referenced_from_inst) {
3515
0
  uint32_t operand = (uint32_t)decoration.builtin();
3516
0
  if (spvIsVulkanEnv(_.context()->target_env)) {
3517
0
    const spv::StorageClass storage_class = GetStorageClass(referenced_from_inst);
3518
0
    if (storage_class != spv::StorageClass::Max &&
3519
0
        storage_class != spv::StorageClass::Input) {
3520
0
      return _.diag(SPV_ERROR_INVALID_DATA, &referenced_from_inst)
3521
0
             << _.VkErrorID(4205) << "Vulkan spec allows BuiltIn "
3522
0
             << _.grammar().lookupOperandName(SPV_OPERAND_TYPE_BUILT_IN,
3523
0
                                              operand)
3524
0
             << " to be only used for variables with Input storage class. "
3525
0
             << GetReferenceDesc(decoration, built_in_inst, referenced_inst,
3526
0
                                 referenced_from_inst)
3527
0
             << " " << GetStorageClassDesc(referenced_from_inst);
3528
0
    }
3529
0
  }
3530
3531
0
  if (function_id_ == 0) {
3532
    // Propagate this rule to all dependant ids in the global scope.
3533
0
    id_to_at_reference_checks_[referenced_from_inst.id()].push_back(std::bind(
3534
0
        &BuiltInsValidator::ValidateDeviceIndexAtReference, this, decoration,
3535
0
        built_in_inst, referenced_from_inst, std::placeholders::_1));
3536
0
  }
3537
3538
0
  return SPV_SUCCESS;
3539
0
}
3540
3541
spv_result_t BuiltInsValidator::ValidateFragInvocationCountAtDefinition(const Decoration& decoration,
3542
0
                                            const Instruction& inst) {
3543
3544
0
  if (spvIsVulkanEnv(_.context()->target_env)) {
3545
0
    const spv::BuiltIn builtin = decoration.builtin();
3546
0
    if (spv_result_t error = ValidateI32(
3547
0
            decoration, inst,
3548
0
            [this, &inst, &builtin](const std::string& message) -> spv_result_t {
3549
0
              uint32_t vuid = GetVUIDForBuiltin(builtin, VUIDErrorType);
3550
0
              return _.diag(SPV_ERROR_INVALID_DATA, &inst)
3551
0
                     << _.VkErrorID(vuid) << "According to the "
3552
0
                     << spvLogStringForEnv(_.context()->target_env)
3553
0
                     << " spec BuiltIn "
3554
0
                     << _.grammar().lookupOperandName(SPV_OPERAND_TYPE_BUILT_IN,
3555
0
                                                      uint32_t(builtin))
3556
0
                     << " variable needs to be a 32-bit int scalar. "
3557
0
                     << message;
3558
0
            })) {
3559
0
      return error;
3560
0
    }
3561
0
  }
3562
3563
0
  return ValidateFragInvocationCountAtReference(decoration, inst, inst, inst);
3564
0
}
3565
3566
spv_result_t BuiltInsValidator::ValidateFragInvocationCountAtReference(
3567
    const Decoration& decoration, const Instruction& built_in_inst,
3568
    const Instruction& referenced_inst,
3569
0
    const Instruction& referenced_from_inst) {
3570
3571
0
  if (spvIsVulkanEnv(_.context()->target_env)) {
3572
0
    const spv::BuiltIn builtin = decoration.builtin();
3573
0
    const spv::StorageClass storage_class = GetStorageClass(referenced_from_inst);
3574
0
    if (storage_class != spv::StorageClass::Max &&
3575
0
        storage_class != spv::StorageClass::Input) {
3576
0
      uint32_t vuid = GetVUIDForBuiltin(builtin, VUIDErrorStorageClass);
3577
0
      return _.diag(SPV_ERROR_INVALID_DATA, &referenced_from_inst)
3578
0
             << _.VkErrorID(vuid) << spvLogStringForEnv(_.context()->target_env)
3579
0
             << " spec allows BuiltIn "
3580
0
             << _.grammar().lookupOperandName(SPV_OPERAND_TYPE_BUILT_IN, uint32_t(builtin))
3581
0
             << " to be only used for variables with Input storage class. "
3582
0
             << GetReferenceDesc(decoration, built_in_inst, referenced_inst,
3583
0
                                 referenced_from_inst)
3584
0
             << " " << GetStorageClassDesc(referenced_from_inst);
3585
0
    }
3586
3587
0
    for (const spv::ExecutionModel execution_model : execution_models_) {
3588
0
      if (execution_model != spv::ExecutionModel::Fragment) {
3589
0
        uint32_t vuid = GetVUIDForBuiltin(builtin, VUIDErrorExecutionModel);
3590
0
        return _.diag(SPV_ERROR_INVALID_DATA, &referenced_from_inst)
3591
0
               << _.VkErrorID(vuid)
3592
0
               << spvLogStringForEnv(_.context()->target_env)
3593
0
               << " spec allows BuiltIn "
3594
0
               << _.grammar().lookupOperandName(SPV_OPERAND_TYPE_BUILT_IN, uint32_t(builtin))
3595
0
               << " to be used only with Fragment execution model. "
3596
0
               << GetReferenceDesc(decoration, built_in_inst, referenced_inst,
3597
0
                                   referenced_from_inst, execution_model);
3598
0
      }
3599
0
    }
3600
0
  }
3601
3602
0
  if (function_id_ == 0) {
3603
    // Propagate this rule to all dependant ids in the global scope.
3604
0
    id_to_at_reference_checks_[referenced_from_inst.id()].push_back(std::bind(
3605
0
        &BuiltInsValidator::ValidateFragInvocationCountAtReference, this, decoration,
3606
0
        built_in_inst, referenced_from_inst, std::placeholders::_1));
3607
0
  }
3608
3609
0
  return SPV_SUCCESS;
3610
0
}
3611
3612
spv_result_t BuiltInsValidator::ValidateFragSizeAtDefinition(const Decoration& decoration,
3613
0
                                            const Instruction& inst) {
3614
0
  if (spvIsVulkanEnv(_.context()->target_env)) {
3615
0
    const spv::BuiltIn builtin = decoration.builtin();
3616
0
    if (spv_result_t error = ValidateI32Vec(
3617
0
            decoration, inst, 2,
3618
0
            [this, &inst, &builtin](const std::string& message) -> spv_result_t {
3619
0
              uint32_t vuid = GetVUIDForBuiltin(builtin, VUIDErrorType);
3620
0
              return _.diag(SPV_ERROR_INVALID_DATA, &inst)
3621
0
                     << _.VkErrorID(vuid) << "According to the "
3622
0
                     << spvLogStringForEnv(_.context()->target_env)
3623
0
                     << " spec BuiltIn "
3624
0
                     << _.grammar().lookupOperandName(SPV_OPERAND_TYPE_BUILT_IN,
3625
0
                                                      uint32_t(builtin))
3626
0
                     << " variable needs to be a 2-component 32-bit int vector. "
3627
0
                     << message;
3628
0
            })) {
3629
0
      return error;
3630
0
    }
3631
0
  }
3632
3633
0
  return ValidateFragSizeAtReference(decoration, inst, inst, inst);
3634
0
}
3635
3636
spv_result_t BuiltInsValidator::ValidateFragSizeAtReference(
3637
    const Decoration& decoration, const Instruction& built_in_inst,
3638
    const Instruction& referenced_inst,
3639
0
    const Instruction& referenced_from_inst) {
3640
3641
0
  if (spvIsVulkanEnv(_.context()->target_env)) {
3642
0
    const spv::BuiltIn builtin = decoration.builtin();
3643
0
    const spv::StorageClass storage_class = GetStorageClass(referenced_from_inst);
3644
0
    if (storage_class != spv::StorageClass::Max &&
3645
0
        storage_class != spv::StorageClass::Input) {
3646
0
      uint32_t vuid = GetVUIDForBuiltin(builtin, VUIDErrorStorageClass);
3647
0
      return _.diag(SPV_ERROR_INVALID_DATA, &referenced_from_inst)
3648
0
             << _.VkErrorID(vuid) << spvLogStringForEnv(_.context()->target_env)
3649
0
             << " spec allows BuiltIn "
3650
0
             << _.grammar().lookupOperandName(SPV_OPERAND_TYPE_BUILT_IN, uint32_t(builtin))
3651
0
             << " to be only used for variables with Input storage class. "
3652
0
             << GetReferenceDesc(decoration, built_in_inst, referenced_inst,
3653
0
                                 referenced_from_inst)
3654
0
             << " " << GetStorageClassDesc(referenced_from_inst);
3655
0
    }
3656
3657
0
    for (const spv::ExecutionModel execution_model : execution_models_) {
3658
0
      if (execution_model != spv::ExecutionModel::Fragment) {
3659
0
        uint32_t vuid = GetVUIDForBuiltin(builtin, VUIDErrorExecutionModel);
3660
0
        return _.diag(SPV_ERROR_INVALID_DATA, &referenced_from_inst)
3661
0
               << _.VkErrorID(vuid)
3662
0
               << spvLogStringForEnv(_.context()->target_env)
3663
0
               << " spec allows BuiltIn "
3664
0
               << _.grammar().lookupOperandName(SPV_OPERAND_TYPE_BUILT_IN, uint32_t(builtin))
3665
0
               << " to be used only with Fragment execution model. "
3666
0
               << GetReferenceDesc(decoration, built_in_inst, referenced_inst,
3667
0
                                   referenced_from_inst, execution_model);
3668
0
      }
3669
0
    }
3670
0
  }
3671
3672
0
  if (function_id_ == 0) {
3673
    // Propagate this rule to all dependant ids in the global scope.
3674
0
    id_to_at_reference_checks_[referenced_from_inst.id()].push_back(std::bind(
3675
0
        &BuiltInsValidator::ValidateFragSizeAtReference, this, decoration,
3676
0
        built_in_inst, referenced_from_inst, std::placeholders::_1));
3677
0
  }
3678
3679
0
  return SPV_SUCCESS;
3680
0
}
3681
3682
spv_result_t BuiltInsValidator::ValidateFragStencilRefAtDefinition(const Decoration& decoration,
3683
0
                                            const Instruction& inst) {
3684
0
  if (spvIsVulkanEnv(_.context()->target_env)) {
3685
0
    const spv::BuiltIn builtin = decoration.builtin();
3686
0
    if (spv_result_t error = ValidateI(
3687
0
            decoration, inst,
3688
0
            [this, &inst, &builtin](const std::string& message) -> spv_result_t {
3689
0
              uint32_t vuid = GetVUIDForBuiltin(builtin, VUIDErrorType);
3690
0
              return _.diag(SPV_ERROR_INVALID_DATA, &inst)
3691
0
                     << _.VkErrorID(vuid) << "According to the "
3692
0
                     << spvLogStringForEnv(_.context()->target_env)
3693
0
                     << " spec BuiltIn "
3694
0
                     << _.grammar().lookupOperandName(SPV_OPERAND_TYPE_BUILT_IN,
3695
0
                                                      uint32_t(builtin))
3696
0
                     << " variable needs to be a int scalar. "
3697
0
                     << message;
3698
0
            })) {
3699
0
      return error;
3700
0
    }
3701
0
  }
3702
3703
0
  return ValidateFragStencilRefAtReference(decoration, inst, inst, inst);
3704
0
}
3705
3706
spv_result_t BuiltInsValidator::ValidateFragStencilRefAtReference(
3707
    const Decoration& decoration, const Instruction& built_in_inst,
3708
    const Instruction& referenced_inst,
3709
0
    const Instruction& referenced_from_inst) {
3710
3711
0
  if (spvIsVulkanEnv(_.context()->target_env)) {
3712
0
    const spv::BuiltIn builtin = decoration.builtin();
3713
0
    const spv::StorageClass storage_class = GetStorageClass(referenced_from_inst);
3714
0
    if (storage_class != spv::StorageClass::Max &&
3715
0
        storage_class != spv::StorageClass::Output) {
3716
0
      uint32_t vuid = GetVUIDForBuiltin(builtin, VUIDErrorStorageClass);
3717
0
      return _.diag(SPV_ERROR_INVALID_DATA, &referenced_from_inst)
3718
0
             << _.VkErrorID(vuid) << spvLogStringForEnv(_.context()->target_env)
3719
0
             << " spec allows BuiltIn "
3720
0
             << _.grammar().lookupOperandName(SPV_OPERAND_TYPE_BUILT_IN, uint32_t(builtin))
3721
0
             << " to be only used for variables with Output storage class. "
3722
0
             << GetReferenceDesc(decoration, built_in_inst, referenced_inst,
3723
0
                                 referenced_from_inst)
3724
0
             << " " << GetStorageClassDesc(referenced_from_inst);
3725
0
    }
3726
3727
0
    for (const spv::ExecutionModel execution_model : execution_models_) {
3728
0
      if (execution_model != spv::ExecutionModel::Fragment) {
3729
0
        uint32_t vuid = GetVUIDForBuiltin(builtin, VUIDErrorExecutionModel);
3730
0
        return _.diag(SPV_ERROR_INVALID_DATA, &referenced_from_inst)
3731
0
               << _.VkErrorID(vuid)
3732
0
               << spvLogStringForEnv(_.context()->target_env)
3733
0
               << " spec allows BuiltIn "
3734
0
               << _.grammar().lookupOperandName(SPV_OPERAND_TYPE_BUILT_IN, uint32_t(builtin))
3735
0
               << " to be used only with Fragment execution model. "
3736
0
               << GetReferenceDesc(decoration, built_in_inst, referenced_inst,
3737
0
                                   referenced_from_inst, execution_model);
3738
0
      }
3739
0
    }
3740
0
  }
3741
3742
0
  if (function_id_ == 0) {
3743
    // Propagate this rule to all dependant ids in the global scope.
3744
0
    id_to_at_reference_checks_[referenced_from_inst.id()].push_back(std::bind(
3745
0
        &BuiltInsValidator::ValidateFragStencilRefAtReference, this, decoration,
3746
0
        built_in_inst, referenced_from_inst, std::placeholders::_1));
3747
0
  }
3748
3749
0
  return SPV_SUCCESS;
3750
0
}
3751
3752
spv_result_t BuiltInsValidator::ValidateFullyCoveredAtDefinition(const Decoration& decoration,
3753
0
                                               const Instruction& inst) {
3754
0
  if (spvIsVulkanEnv(_.context()->target_env)) {
3755
0
    const spv::BuiltIn builtin = decoration.builtin();
3756
0
    if (spv_result_t error = ValidateBool(
3757
0
            decoration, inst,
3758
0
            [this, &inst, &builtin](const std::string& message) -> spv_result_t {
3759
0
              uint32_t vuid = GetVUIDForBuiltin(builtin, VUIDErrorType);
3760
0
              return _.diag(SPV_ERROR_INVALID_DATA, &inst)
3761
0
                     << _.VkErrorID(vuid) << "According to the "
3762
0
                     << spvLogStringForEnv(_.context()->target_env)
3763
0
                     << " spec BuiltIn "
3764
0
                     << _.grammar().lookupOperandName(SPV_OPERAND_TYPE_BUILT_IN,
3765
0
                                                      uint32_t(builtin))
3766
0
                     << " variable needs to be a bool scalar. "
3767
0
                     << message;
3768
0
            })) {
3769
0
      return error;
3770
0
    }
3771
0
  }
3772
3773
0
  return ValidateFullyCoveredAtReference(decoration, inst, inst, inst);
3774
0
}
3775
3776
spv_result_t BuiltInsValidator::ValidateFullyCoveredAtReference(
3777
    const Decoration& decoration, const Instruction& built_in_inst,
3778
    const Instruction& referenced_inst,
3779
0
    const Instruction& referenced_from_inst) {
3780
3781
0
  if (spvIsVulkanEnv(_.context()->target_env)) {
3782
0
    const spv::BuiltIn builtin = decoration.builtin();
3783
0
    const spv::StorageClass storage_class = GetStorageClass(referenced_from_inst);
3784
0
    if (storage_class != spv::StorageClass::Max &&
3785
0
        storage_class != spv::StorageClass::Input) {
3786
0
      uint32_t vuid = GetVUIDForBuiltin(builtin, VUIDErrorStorageClass);
3787
0
      return _.diag(SPV_ERROR_INVALID_DATA, &referenced_from_inst)
3788
0
             << _.VkErrorID(vuid) << spvLogStringForEnv(_.context()->target_env)
3789
0
             << " spec allows BuiltIn "
3790
0
             << _.grammar().lookupOperandName(SPV_OPERAND_TYPE_BUILT_IN, uint32_t(builtin))
3791
0
             << " to be only used for variables with Input storage class. "
3792
0
             << GetReferenceDesc(decoration, built_in_inst, referenced_inst,
3793
0
                                 referenced_from_inst)
3794
0
             << " " << GetStorageClassDesc(referenced_from_inst);
3795
0
    }
3796
3797
0
    for (const spv::ExecutionModel execution_model : execution_models_) {
3798
0
      if (execution_model != spv::ExecutionModel::Fragment) {
3799
0
        uint32_t vuid = GetVUIDForBuiltin(builtin, VUIDErrorExecutionModel);
3800
0
        return _.diag(SPV_ERROR_INVALID_DATA, &referenced_from_inst)
3801
0
               << _.VkErrorID(vuid)
3802
0
               << spvLogStringForEnv(_.context()->target_env)
3803
0
               << " spec allows BuiltIn "
3804
0
               << _.grammar().lookupOperandName(SPV_OPERAND_TYPE_BUILT_IN, uint32_t(builtin))
3805
0
               << " to be used only with Fragment execution model. "
3806
0
               << GetReferenceDesc(decoration, built_in_inst, referenced_inst,
3807
0
                                   referenced_from_inst, execution_model);
3808
0
      }
3809
0
    }
3810
0
  }
3811
3812
0
  if (function_id_ == 0) {
3813
    // Propagate this rule to all dependant ids in the global scope.
3814
0
    id_to_at_reference_checks_[referenced_from_inst.id()].push_back(std::bind(
3815
0
        &BuiltInsValidator::ValidateFullyCoveredAtReference, this, decoration,
3816
0
        built_in_inst, referenced_from_inst, std::placeholders::_1));
3817
0
  }
3818
3819
0
  return SPV_SUCCESS;
3820
0
}
3821
3822
spv_result_t BuiltInsValidator::ValidateNVSMOrARMCoreBuiltinsAtDefinition(
3823
0
    const Decoration& decoration, const Instruction& inst) {
3824
0
  if (spvIsVulkanEnv(_.context()->target_env)) {
3825
0
    if (spv_result_t error = ValidateI32(
3826
0
            decoration, inst,
3827
0
            [this, &inst,
3828
0
             &decoration](const std::string& message) -> spv_result_t {
3829
0
              return _.diag(SPV_ERROR_INVALID_DATA, &inst)
3830
0
                     << "According to the "
3831
0
                     << spvLogStringForEnv(_.context()->target_env)
3832
0
                     << " spec BuiltIn "
3833
0
                     << _.grammar().lookupOperandName(
3834
0
                            SPV_OPERAND_TYPE_BUILT_IN,
3835
0
                            (uint32_t)decoration.builtin())
3836
0
                     << " variable needs to be a 32-bit int scalar. "
3837
0
                     << message;
3838
0
            })) {
3839
0
      return error;
3840
0
    }
3841
0
  }
3842
3843
  // Seed at reference checks with this built-in.
3844
0
  return ValidateNVSMOrARMCoreBuiltinsAtReference(decoration, inst, inst, inst);
3845
0
}
3846
3847
spv_result_t BuiltInsValidator::ValidateNVSMOrARMCoreBuiltinsAtReference(
3848
    const Decoration& decoration, const Instruction& built_in_inst,
3849
    const Instruction& referenced_inst,
3850
0
    const Instruction& referenced_from_inst) {
3851
0
  if (spvIsVulkanEnv(_.context()->target_env)) {
3852
0
    const spv::StorageClass storage_class = GetStorageClass(referenced_from_inst);
3853
0
    if (storage_class != spv::StorageClass::Max &&
3854
0
        storage_class != spv::StorageClass::Input) {
3855
0
      return _.diag(SPV_ERROR_INVALID_DATA, &referenced_from_inst)
3856
0
             << spvLogStringForEnv(_.context()->target_env)
3857
0
             << " spec allows BuiltIn "
3858
0
             << _.grammar().lookupOperandName(SPV_OPERAND_TYPE_BUILT_IN,
3859
0
                                              (uint32_t)decoration.builtin())
3860
0
             << " to be only used for "
3861
0
                "variables with Input storage class. "
3862
0
             << GetReferenceDesc(decoration, built_in_inst, referenced_inst,
3863
0
                                 referenced_from_inst)
3864
0
             << " " << GetStorageClassDesc(referenced_from_inst);
3865
0
    }
3866
0
  }
3867
3868
0
  if (function_id_ == 0) {
3869
    // Propagate this rule to all dependant ids in the global scope.
3870
0
    id_to_at_reference_checks_[referenced_from_inst.id()].push_back(std::bind(
3871
0
        &BuiltInsValidator::ValidateNVSMOrARMCoreBuiltinsAtReference, this, decoration,
3872
0
        built_in_inst, referenced_from_inst, std::placeholders::_1));
3873
0
  }
3874
3875
0
  return SPV_SUCCESS;
3876
0
}
3877
3878
spv_result_t BuiltInsValidator::ValidatePrimitiveShadingRateAtDefinition(
3879
0
    const Decoration& decoration, const Instruction& inst) {
3880
0
  if (spvIsVulkanEnv(_.context()->target_env)) {
3881
0
    if (spv_result_t error = ValidateI32(
3882
0
            decoration, inst,
3883
0
            [this, &inst,
3884
0
             &decoration](const std::string& message) -> spv_result_t {
3885
0
              return _.diag(SPV_ERROR_INVALID_DATA, &inst)
3886
0
                     << _.VkErrorID(4486)
3887
0
                     << "According to the Vulkan spec BuiltIn "
3888
0
                     << _.grammar().lookupOperandName(
3889
0
                            SPV_OPERAND_TYPE_BUILT_IN,
3890
0
                            (uint32_t)decoration.builtin())
3891
0
                     << " variable needs to be a 32-bit int scalar. "
3892
0
                     << message;
3893
0
            })) {
3894
0
      return error;
3895
0
    }
3896
0
  }
3897
3898
  // Seed at reference checks with this built-in.
3899
0
  return ValidatePrimitiveShadingRateAtReference(decoration, inst, inst, inst);
3900
0
}
3901
3902
spv_result_t BuiltInsValidator::ValidatePrimitiveShadingRateAtReference(
3903
    const Decoration& decoration, const Instruction& built_in_inst,
3904
    const Instruction& referenced_inst,
3905
0
    const Instruction& referenced_from_inst) {
3906
0
  if (spvIsVulkanEnv(_.context()->target_env)) {
3907
0
    const spv::StorageClass storage_class = GetStorageClass(referenced_from_inst);
3908
0
    if (storage_class != spv::StorageClass::Max &&
3909
0
        storage_class != spv::StorageClass::Output) {
3910
0
      return _.diag(SPV_ERROR_INVALID_DATA, &referenced_from_inst)
3911
0
             << _.VkErrorID(4485) << "Vulkan spec allows BuiltIn "
3912
0
             << _.grammar().lookupOperandName(SPV_OPERAND_TYPE_BUILT_IN,
3913
0
                                              (uint32_t)decoration.builtin())
3914
0
             << " to be only used for variables with Output storage class. "
3915
0
             << GetReferenceDesc(decoration, built_in_inst, referenced_inst,
3916
0
                                 referenced_from_inst)
3917
0
             << " " << GetStorageClassDesc(referenced_from_inst);
3918
0
    }
3919
3920
0
    for (const spv::ExecutionModel execution_model : execution_models_) {
3921
0
      switch (execution_model) {
3922
0
        case spv::ExecutionModel::Vertex:
3923
0
        case spv::ExecutionModel::Geometry:
3924
0
        case spv::ExecutionModel::MeshNV:
3925
0
        case spv::ExecutionModel::MeshEXT:
3926
0
          break;
3927
0
        default: {
3928
0
          return _.diag(SPV_ERROR_INVALID_DATA, &referenced_from_inst)
3929
0
                 << _.VkErrorID(4484) << "Vulkan spec allows BuiltIn "
3930
0
                 << _.grammar().lookupOperandName(
3931
0
                        SPV_OPERAND_TYPE_BUILT_IN,
3932
0
                        (uint32_t)decoration.builtin())
3933
0
                 << " to be used only with Vertex, Geometry, or MeshNV "
3934
0
                    "execution models. "
3935
0
                 << GetReferenceDesc(decoration, built_in_inst, referenced_inst,
3936
0
                                     referenced_from_inst, execution_model);
3937
0
        }
3938
0
      }
3939
0
    }
3940
0
  }
3941
3942
0
  if (function_id_ == 0) {
3943
    // Propagate this rule to all dependant ids in the global scope.
3944
0
    id_to_at_reference_checks_[referenced_from_inst.id()].push_back(
3945
0
        std::bind(&BuiltInsValidator::ValidatePrimitiveShadingRateAtReference,
3946
0
                  this, decoration, built_in_inst, referenced_from_inst,
3947
0
                  std::placeholders::_1));
3948
0
  }
3949
3950
0
  return SPV_SUCCESS;
3951
0
}
3952
3953
spv_result_t BuiltInsValidator::ValidateShadingRateAtDefinition(
3954
0
    const Decoration& decoration, const Instruction& inst) {
3955
0
  if (spvIsVulkanEnv(_.context()->target_env)) {
3956
0
    if (spv_result_t error = ValidateI32(
3957
0
            decoration, inst,
3958
0
            [this, &inst,
3959
0
             &decoration](const std::string& message) -> spv_result_t {
3960
0
              return _.diag(SPV_ERROR_INVALID_DATA, &inst)
3961
0
                     << _.VkErrorID(4492)
3962
0
                     << "According to the Vulkan spec BuiltIn "
3963
0
                     << _.grammar().lookupOperandName(
3964
0
                            SPV_OPERAND_TYPE_BUILT_IN,
3965
0
                            (uint32_t)decoration.builtin())
3966
0
                     << " variable needs to be a 32-bit int scalar. "
3967
0
                     << message;
3968
0
            })) {
3969
0
      return error;
3970
0
    }
3971
0
  }
3972
3973
  // Seed at reference checks with this built-in.
3974
0
  return ValidateShadingRateAtReference(decoration, inst, inst, inst);
3975
0
}
3976
3977
spv_result_t BuiltInsValidator::ValidateShadingRateAtReference(
3978
    const Decoration& decoration, const Instruction& built_in_inst,
3979
    const Instruction& referenced_inst,
3980
0
    const Instruction& referenced_from_inst) {
3981
0
  if (spvIsVulkanEnv(_.context()->target_env)) {
3982
0
    const spv::StorageClass storage_class = GetStorageClass(referenced_from_inst);
3983
0
    if (storage_class != spv::StorageClass::Max &&
3984
0
        storage_class != spv::StorageClass::Input) {
3985
0
      return _.diag(SPV_ERROR_INVALID_DATA, &referenced_from_inst)
3986
0
             << _.VkErrorID(4491) << "Vulkan spec allows BuiltIn "
3987
0
             << _.grammar().lookupOperandName(SPV_OPERAND_TYPE_BUILT_IN,
3988
0
                                              (uint32_t)decoration.builtin())
3989
0
             << " to be only used for variables with Input storage class. "
3990
0
             << GetReferenceDesc(decoration, built_in_inst, referenced_inst,
3991
0
                                 referenced_from_inst)
3992
0
             << " " << GetStorageClassDesc(referenced_from_inst);
3993
0
    }
3994
3995
0
    for (const spv::ExecutionModel execution_model : execution_models_) {
3996
0
      if (execution_model != spv::ExecutionModel::Fragment) {
3997
0
        return _.diag(SPV_ERROR_INVALID_DATA, &referenced_from_inst)
3998
0
               << _.VkErrorID(4490) << "Vulkan spec allows BuiltIn "
3999
0
               << _.grammar().lookupOperandName(SPV_OPERAND_TYPE_BUILT_IN,
4000
0
                                                (uint32_t)decoration.builtin())
4001
0
               << " to be used only with the Fragment execution model. "
4002
0
               << GetReferenceDesc(decoration, built_in_inst, referenced_inst,
4003
0
                                   referenced_from_inst, execution_model);
4004
0
      }
4005
0
    }
4006
0
  }
4007
4008
0
  if (function_id_ == 0) {
4009
    // Propagate this rule to all dependant ids in the global scope.
4010
0
    id_to_at_reference_checks_[referenced_from_inst.id()].push_back(std::bind(
4011
0
        &BuiltInsValidator::ValidateShadingRateAtReference, this, decoration,
4012
0
        built_in_inst, referenced_from_inst, std::placeholders::_1));
4013
0
  }
4014
4015
0
  return SPV_SUCCESS;
4016
0
}
4017
4018
spv_result_t BuiltInsValidator::ValidateRayTracingBuiltinsAtDefinition(
4019
0
    const Decoration& decoration, const Instruction& inst) {
4020
0
  if (spvIsVulkanEnv(_.context()->target_env)) {
4021
0
    const spv::BuiltIn builtin = decoration.builtin();
4022
0
    switch (builtin) {
4023
0
      case spv::BuiltIn::HitTNV:
4024
0
      case spv::BuiltIn::RayTminKHR:
4025
0
      case spv::BuiltIn::RayTmaxKHR:
4026
        // f32 scalar
4027
0
        if (spv_result_t error = ValidateF32(
4028
0
                decoration, inst,
4029
0
                [this, &inst,
4030
0
                 builtin](const std::string& message) -> spv_result_t {
4031
0
                  uint32_t vuid = GetVUIDForBuiltin(builtin, VUIDErrorType);
4032
0
                  return _.diag(SPV_ERROR_INVALID_DATA, &inst)
4033
0
                         << _.VkErrorID(vuid)
4034
0
                         << "According to the Vulkan spec BuiltIn "
4035
0
                         << _.grammar().lookupOperandName(
4036
0
                                SPV_OPERAND_TYPE_BUILT_IN, uint32_t(builtin))
4037
0
                         << " variable needs to be a 32-bit float scalar. "
4038
0
                         << message;
4039
0
                })) {
4040
0
          return error;
4041
0
        }
4042
0
        break;
4043
0
      case spv::BuiltIn::HitKindKHR:
4044
0
      case spv::BuiltIn::InstanceCustomIndexKHR:
4045
0
      case spv::BuiltIn::InstanceId:
4046
0
      case spv::BuiltIn::RayGeometryIndexKHR:
4047
0
      case spv::BuiltIn::IncomingRayFlagsKHR:
4048
0
      case spv::BuiltIn::CullMaskKHR:
4049
        // i32 scalar
4050
0
        if (spv_result_t error = ValidateI32(
4051
0
                decoration, inst,
4052
0
                [this, &inst,
4053
0
                 builtin](const std::string& message) -> spv_result_t {
4054
0
                  uint32_t vuid = GetVUIDForBuiltin(builtin, VUIDErrorType);
4055
0
                  return _.diag(SPV_ERROR_INVALID_DATA, &inst)
4056
0
                         << _.VkErrorID(vuid)
4057
0
                         << "According to the Vulkan spec BuiltIn "
4058
0
                         << _.grammar().lookupOperandName(
4059
0
                                SPV_OPERAND_TYPE_BUILT_IN, uint32_t(builtin))
4060
0
                         << " variable needs to be a 32-bit int scalar. "
4061
0
                         << message;
4062
0
                })) {
4063
0
          return error;
4064
0
        }
4065
0
        break;
4066
0
      case spv::BuiltIn::ObjectRayDirectionKHR:
4067
0
      case spv::BuiltIn::ObjectRayOriginKHR:
4068
0
      case spv::BuiltIn::WorldRayDirectionKHR:
4069
0
      case spv::BuiltIn::WorldRayOriginKHR:
4070
        // f32 vec3
4071
0
        if (spv_result_t error = ValidateF32Vec(
4072
0
                decoration, inst, 3,
4073
0
                [this, &inst,
4074
0
                 builtin](const std::string& message) -> spv_result_t {
4075
0
                  uint32_t vuid = GetVUIDForBuiltin(builtin, VUIDErrorType);
4076
0
                  return _.diag(SPV_ERROR_INVALID_DATA, &inst)
4077
0
                         << _.VkErrorID(vuid)
4078
0
                         << "According to the Vulkan spec BuiltIn "
4079
0
                         << _.grammar().lookupOperandName(
4080
0
                                SPV_OPERAND_TYPE_BUILT_IN, uint32_t(builtin))
4081
0
                         << " variable needs to be a 3-component 32-bit float "
4082
0
                            "vector. "
4083
0
                         << message;
4084
0
                })) {
4085
0
          return error;
4086
0
        }
4087
0
        break;
4088
0
      case spv::BuiltIn::LaunchIdKHR:
4089
0
      case spv::BuiltIn::LaunchSizeKHR:
4090
        // i32 vec3
4091
0
        if (spv_result_t error = ValidateI32Vec(
4092
0
                decoration, inst, 3,
4093
0
                [this, &inst,
4094
0
                 builtin](const std::string& message) -> spv_result_t {
4095
0
                  uint32_t vuid = GetVUIDForBuiltin(builtin, VUIDErrorType);
4096
0
                  return _.diag(SPV_ERROR_INVALID_DATA, &inst)
4097
0
                         << _.VkErrorID(vuid)
4098
0
                         << "According to the Vulkan spec BuiltIn "
4099
0
                         << _.grammar().lookupOperandName(
4100
0
                                SPV_OPERAND_TYPE_BUILT_IN, uint32_t(builtin))
4101
0
                         << " variable needs to be a 3-component 32-bit int "
4102
0
                            "vector. "
4103
0
                         << message;
4104
0
                })) {
4105
0
          return error;
4106
0
        }
4107
0
        break;
4108
0
      case spv::BuiltIn::ObjectToWorldKHR:
4109
0
      case spv::BuiltIn::WorldToObjectKHR:
4110
        // f32 mat4x3
4111
0
        if (spv_result_t error = ValidateF32Mat(
4112
0
                decoration, inst, 3, 4,
4113
0
                [this, &inst,
4114
0
                 builtin](const std::string& message) -> spv_result_t {
4115
0
                  uint32_t vuid = GetVUIDForBuiltin(builtin, VUIDErrorType);
4116
0
                  return _.diag(SPV_ERROR_INVALID_DATA, &inst)
4117
0
                         << _.VkErrorID(vuid)
4118
0
                         << "According to the Vulkan spec BuiltIn "
4119
0
                         << _.grammar().lookupOperandName(
4120
0
                                SPV_OPERAND_TYPE_BUILT_IN, uint32_t(builtin))
4121
0
                         << " variable needs to be a matrix with"
4122
0
                         << " 4 columns of 3-component vectors of 32-bit "
4123
0
                            "floats. "
4124
0
                         << message;
4125
0
                })) {
4126
0
          return error;
4127
0
        }
4128
0
        break;
4129
0
      default:
4130
0
        assert(0 && "Unexpected ray tracing builtin");
4131
0
        break;
4132
0
    }
4133
0
  }
4134
4135
  // Seed at reference checks with this built-in.
4136
0
  return ValidateRayTracingBuiltinsAtReference(decoration, inst, inst, inst);
4137
0
}
4138
4139
spv_result_t BuiltInsValidator::ValidateRayTracingBuiltinsAtReference(
4140
    const Decoration& decoration, const Instruction& built_in_inst,
4141
    const Instruction& referenced_inst,
4142
0
    const Instruction& referenced_from_inst) {
4143
0
  if (spvIsVulkanEnv(_.context()->target_env)) {
4144
0
    const spv::BuiltIn builtin = decoration.builtin();
4145
0
    const spv::StorageClass storage_class = GetStorageClass(referenced_from_inst);
4146
0
    if (storage_class != spv::StorageClass::Max &&
4147
0
        storage_class != spv::StorageClass::Input) {
4148
0
      uint32_t vuid = GetVUIDForBuiltin(builtin, VUIDErrorStorageClass);
4149
0
      return _.diag(SPV_ERROR_INVALID_DATA, &referenced_from_inst)
4150
0
             << _.VkErrorID(vuid) << "Vulkan spec allows BuiltIn "
4151
0
             << _.grammar().lookupOperandName(SPV_OPERAND_TYPE_BUILT_IN,
4152
0
                                              (uint32_t)decoration.builtin())
4153
0
             << " to be only used for variables with Input storage class. "
4154
0
             << GetReferenceDesc(decoration, built_in_inst, referenced_inst,
4155
0
                                 referenced_from_inst)
4156
0
             << " " << GetStorageClassDesc(referenced_from_inst);
4157
0
    }
4158
4159
0
    for (const spv::ExecutionModel execution_model : execution_models_) {
4160
0
      if (!IsExecutionModelValidForRtBuiltIn(builtin, execution_model)) {
4161
0
        uint32_t vuid = GetVUIDForBuiltin(builtin, VUIDErrorExecutionModel);
4162
0
        return _.diag(SPV_ERROR_INVALID_DATA, &referenced_from_inst)
4163
0
               << _.VkErrorID(vuid) << "Vulkan spec does not allow BuiltIn "
4164
0
               << _.grammar().lookupOperandName(SPV_OPERAND_TYPE_BUILT_IN,
4165
0
                                                (uint32_t)decoration.builtin())
4166
0
               << " to be used with the execution model "
4167
0
               << _.grammar().lookupOperandName(
4168
0
                      SPV_OPERAND_TYPE_EXECUTION_MODEL,
4169
0
                      uint32_t(execution_model))
4170
0
               << ".\n"
4171
0
               << GetReferenceDesc(decoration, built_in_inst, referenced_inst,
4172
0
                                   referenced_from_inst, execution_model);
4173
0
      }
4174
0
    }
4175
0
  }
4176
4177
0
  if (function_id_ == 0) {
4178
    // Propagate this rule to all dependant ids in the global scope.
4179
0
    id_to_at_reference_checks_[referenced_from_inst.id()].push_back(
4180
0
        std::bind(&BuiltInsValidator::ValidateRayTracingBuiltinsAtReference,
4181
0
                  this, decoration, built_in_inst, referenced_from_inst,
4182
0
                  std::placeholders::_1));
4183
0
  }
4184
4185
0
  return SPV_SUCCESS;
4186
0
}
4187
4188
spv_result_t BuiltInsValidator::ValidateMeshShadingEXTBuiltinsAtDefinition(
4189
0
    const Decoration& decoration, const Instruction& inst) {
4190
0
  if (spvIsVulkanEnv(_.context()->target_env)) {
4191
0
    const spv::BuiltIn builtin = decoration.builtin();
4192
0
    uint32_t vuid = GetVUIDForBuiltin(builtin, VUIDErrorType);
4193
0
    if (builtin == spv::BuiltIn::PrimitivePointIndicesEXT) {
4194
0
      if (spv_result_t error = ValidateI32Arr(
4195
0
              decoration, inst,
4196
0
              [this, &inst, &decoration,
4197
0
               &vuid](const std::string& message) -> spv_result_t {
4198
0
                return _.diag(SPV_ERROR_INVALID_DATA, &inst)
4199
0
                       << _.VkErrorID(vuid) << "According to the "
4200
0
                       << spvLogStringForEnv(_.context()->target_env)
4201
0
                       << " spec BuiltIn "
4202
0
                       << _.grammar().lookupOperandName(
4203
0
                              SPV_OPERAND_TYPE_BUILT_IN,
4204
0
                              (uint32_t)decoration.builtin())
4205
0
                       << " variable needs to be a 32-bit int array."
4206
0
                       << message;
4207
0
              })) {
4208
0
        return error;
4209
0
      }
4210
0
    }
4211
0
    if (builtin == spv::BuiltIn::PrimitiveLineIndicesEXT) {
4212
0
      if (spv_result_t error = ValidateArrayedI32Vec(
4213
0
              decoration, inst, 2,
4214
0
              [this, &inst, &decoration,
4215
0
               &vuid](const std::string& message) -> spv_result_t {
4216
0
                return _.diag(SPV_ERROR_INVALID_DATA, &inst)
4217
0
                       << _.VkErrorID(vuid) << "According to the "
4218
0
                       << spvLogStringForEnv(_.context()->target_env)
4219
0
                       << " spec BuiltIn "
4220
0
                       << _.grammar().lookupOperandName(
4221
0
                              SPV_OPERAND_TYPE_BUILT_IN,
4222
0
                              (uint32_t)decoration.builtin())
4223
0
                       << " variable needs to be a 2-component 32-bit int "
4224
0
                          "array."
4225
0
                       << message;
4226
0
              })) {
4227
0
        return error;
4228
0
      }
4229
0
    }
4230
0
    if (builtin == spv::BuiltIn::PrimitiveTriangleIndicesEXT) {
4231
0
      if (spv_result_t error = ValidateArrayedI32Vec(
4232
0
              decoration, inst, 3,
4233
0
              [this, &inst, &decoration,
4234
0
               &vuid](const std::string& message) -> spv_result_t {
4235
0
                return _.diag(SPV_ERROR_INVALID_DATA, &inst)
4236
0
                       << _.VkErrorID(vuid) << "According to the "
4237
0
                       << spvLogStringForEnv(_.context()->target_env)
4238
0
                       << " spec BuiltIn "
4239
0
                       << _.grammar().lookupOperandName(
4240
0
                              SPV_OPERAND_TYPE_BUILT_IN,
4241
0
                              (uint32_t)decoration.builtin())
4242
0
                       << " variable needs to be a 3-component 32-bit int "
4243
0
                          "array."
4244
0
                       << message;
4245
0
              })) {
4246
0
        return error;
4247
0
      }
4248
0
    }
4249
0
  }
4250
  // Seed at reference checks with this built-in.
4251
0
  return ValidateMeshShadingEXTBuiltinsAtReference(decoration, inst, inst,
4252
0
                                                   inst);
4253
0
}
4254
4255
spv_result_t BuiltInsValidator::ValidateMeshShadingEXTBuiltinsAtReference(
4256
    const Decoration& decoration, const Instruction& built_in_inst,
4257
    const Instruction& referenced_inst,
4258
0
    const Instruction& referenced_from_inst) {
4259
0
  if (spvIsVulkanEnv(_.context()->target_env)) {
4260
0
    const spv::BuiltIn builtin = decoration.builtin();
4261
0
    const spv::StorageClass storage_class =
4262
0
        GetStorageClass(referenced_from_inst);
4263
0
    if (storage_class != spv::StorageClass::Max &&
4264
0
        storage_class != spv::StorageClass::Output) {
4265
0
      uint32_t vuid = GetVUIDForBuiltin(builtin, VUIDErrorStorageClass);
4266
0
      return _.diag(SPV_ERROR_INVALID_DATA, &referenced_from_inst)
4267
0
             << _.VkErrorID(vuid) << spvLogStringForEnv(_.context()->target_env)
4268
0
             << " spec allows BuiltIn "
4269
0
             << _.grammar().lookupOperandName(SPV_OPERAND_TYPE_BUILT_IN,
4270
0
                                              uint32_t(builtin))
4271
0
             << " to be only used for variables with Output storage class. "
4272
0
             << GetReferenceDesc(decoration, built_in_inst, referenced_inst,
4273
0
                                 referenced_from_inst)
4274
0
             << " " << GetStorageClassDesc(referenced_from_inst);
4275
0
    }
4276
4277
0
    for (const spv::ExecutionModel execution_model : execution_models_) {
4278
0
      if (execution_model != spv::ExecutionModel::MeshEXT) {
4279
0
        uint32_t vuid = GetVUIDForBuiltin(builtin, VUIDErrorExecutionModel);
4280
0
        return _.diag(SPV_ERROR_INVALID_DATA, &referenced_from_inst)
4281
0
               << _.VkErrorID(vuid)
4282
0
               << spvLogStringForEnv(_.context()->target_env)
4283
0
               << " spec allows BuiltIn "
4284
0
               << _.grammar().lookupOperandName(SPV_OPERAND_TYPE_BUILT_IN,
4285
0
                                                uint32_t(builtin))
4286
0
               << " to be used only with MeshEXT execution model. "
4287
0
               << GetReferenceDesc(decoration, built_in_inst, referenced_inst,
4288
0
                                   referenced_from_inst, execution_model);
4289
0
      }
4290
0
    }
4291
0
  }
4292
4293
0
  if (function_id_ == 0) {
4294
    // Propagate this rule to all dependant ids in the global scope.
4295
0
    id_to_at_reference_checks_[referenced_from_inst.id()].push_back(
4296
0
        std::bind(&BuiltInsValidator::ValidateMeshShadingEXTBuiltinsAtReference,
4297
0
                  this, decoration, built_in_inst, referenced_from_inst,
4298
0
                  std::placeholders::_1));
4299
0
  }
4300
4301
0
  return SPV_SUCCESS;
4302
0
}
4303
4304
spv_result_t BuiltInsValidator::ValidateSingleBuiltInAtDefinition(
4305
1.12k
    const Decoration& decoration, const Instruction& inst) {
4306
1.12k
  const spv::BuiltIn label = decoration.builtin();
4307
4308
1.12k
  if (!spvIsVulkanEnv(_.context()->target_env)) {
4309
    // Early return. All currently implemented rules are based on Vulkan spec.
4310
    //
4311
    // TODO: If you are adding validation rules for environments other than
4312
    // Vulkan (or general rules which are not environment independent), then
4313
    // you need to modify or remove this condition. Consider also adding early
4314
    // returns into BuiltIn-specific rules, so that the system doesn't spawn new
4315
    // rules which don't do anything.
4316
1.12k
    return SPV_SUCCESS;
4317
1.12k
  }
4318
4319
  // If you are adding a new BuiltIn enum, please register it here.
4320
  // If the newly added enum has validation rules associated with it
4321
  // consider leaving a TODO and/or creating an issue.
4322
0
  switch (label) {
4323
0
    case spv::BuiltIn::ClipDistance:
4324
0
    case spv::BuiltIn::CullDistance: {
4325
0
      return ValidateClipOrCullDistanceAtDefinition(decoration, inst);
4326
0
    }
4327
0
    case spv::BuiltIn::FragCoord: {
4328
0
      return ValidateFragCoordAtDefinition(decoration, inst);
4329
0
    }
4330
0
    case spv::BuiltIn::FragDepth: {
4331
0
      return ValidateFragDepthAtDefinition(decoration, inst);
4332
0
    }
4333
0
    case spv::BuiltIn::FrontFacing: {
4334
0
      return ValidateFrontFacingAtDefinition(decoration, inst);
4335
0
    }
4336
0
    case spv::BuiltIn::GlobalInvocationId:
4337
0
    case spv::BuiltIn::LocalInvocationId:
4338
0
    case spv::BuiltIn::NumWorkgroups:
4339
0
    case spv::BuiltIn::WorkgroupId: {
4340
0
      return ValidateComputeShaderI32Vec3InputAtDefinition(decoration, inst);
4341
0
    }
4342
0
    case spv::BuiltIn::BaryCoordKHR:
4343
0
    case spv::BuiltIn::BaryCoordNoPerspKHR: {
4344
0
      return ValidateFragmentShaderF32Vec3InputAtDefinition(decoration, inst);
4345
0
    }
4346
0
    case spv::BuiltIn::HelperInvocation: {
4347
0
      return ValidateHelperInvocationAtDefinition(decoration, inst);
4348
0
    }
4349
0
    case spv::BuiltIn::InvocationId: {
4350
0
      return ValidateInvocationIdAtDefinition(decoration, inst);
4351
0
    }
4352
0
    case spv::BuiltIn::InstanceIndex: {
4353
0
      return ValidateInstanceIndexAtDefinition(decoration, inst);
4354
0
    }
4355
0
    case spv::BuiltIn::Layer:
4356
0
    case spv::BuiltIn::ViewportIndex: {
4357
0
      return ValidateLayerOrViewportIndexAtDefinition(decoration, inst);
4358
0
    }
4359
0
    case spv::BuiltIn::PatchVertices: {
4360
0
      return ValidatePatchVerticesAtDefinition(decoration, inst);
4361
0
    }
4362
0
    case spv::BuiltIn::PointCoord: {
4363
0
      return ValidatePointCoordAtDefinition(decoration, inst);
4364
0
    }
4365
0
    case spv::BuiltIn::PointSize: {
4366
0
      return ValidatePointSizeAtDefinition(decoration, inst);
4367
0
    }
4368
0
    case spv::BuiltIn::Position: {
4369
0
      return ValidatePositionAtDefinition(decoration, inst);
4370
0
    }
4371
0
    case spv::BuiltIn::PrimitiveId: {
4372
0
      return ValidatePrimitiveIdAtDefinition(decoration, inst);
4373
0
    }
4374
0
    case spv::BuiltIn::SampleId: {
4375
0
      return ValidateSampleIdAtDefinition(decoration, inst);
4376
0
    }
4377
0
    case spv::BuiltIn::SampleMask: {
4378
0
      return ValidateSampleMaskAtDefinition(decoration, inst);
4379
0
    }
4380
0
    case spv::BuiltIn::SamplePosition: {
4381
0
      return ValidateSamplePositionAtDefinition(decoration, inst);
4382
0
    }
4383
0
    case spv::BuiltIn::SubgroupId:
4384
0
    case spv::BuiltIn::NumSubgroups: {
4385
0
      return ValidateComputeI32InputAtDefinition(decoration, inst);
4386
0
    }
4387
0
    case spv::BuiltIn::SubgroupLocalInvocationId:
4388
0
    case spv::BuiltIn::SubgroupSize: {
4389
0
      return ValidateI32InputAtDefinition(decoration, inst);
4390
0
    }
4391
0
    case spv::BuiltIn::SubgroupEqMask:
4392
0
    case spv::BuiltIn::SubgroupGeMask:
4393
0
    case spv::BuiltIn::SubgroupGtMask:
4394
0
    case spv::BuiltIn::SubgroupLeMask:
4395
0
    case spv::BuiltIn::SubgroupLtMask: {
4396
0
      return ValidateI32Vec4InputAtDefinition(decoration, inst);
4397
0
    }
4398
0
    case spv::BuiltIn::TessCoord: {
4399
0
      return ValidateTessCoordAtDefinition(decoration, inst);
4400
0
    }
4401
0
    case spv::BuiltIn::TessLevelOuter: {
4402
0
      return ValidateTessLevelOuterAtDefinition(decoration, inst);
4403
0
    }
4404
0
    case spv::BuiltIn::TessLevelInner: {
4405
0
      return ValidateTessLevelInnerAtDefinition(decoration, inst);
4406
0
    }
4407
0
    case spv::BuiltIn::VertexIndex: {
4408
0
      return ValidateVertexIndexAtDefinition(decoration, inst);
4409
0
    }
4410
0
    case spv::BuiltIn::WorkgroupSize: {
4411
0
      return ValidateWorkgroupSizeAtDefinition(decoration, inst);
4412
0
    }
4413
0
    case spv::BuiltIn::VertexId: {
4414
0
      return ValidateVertexIdAtDefinition(decoration, inst);
4415
0
    }
4416
0
    case spv::BuiltIn::LocalInvocationIndex: {
4417
0
      return ValidateLocalInvocationIndexAtDefinition(decoration, inst);
4418
0
    }
4419
0
    case spv::BuiltIn::CoreIDARM:
4420
0
    case spv::BuiltIn::CoreCountARM:
4421
0
    case spv::BuiltIn::CoreMaxIDARM:
4422
0
    case spv::BuiltIn::WarpIDARM:
4423
0
    case spv::BuiltIn::WarpMaxIDARM:
4424
0
    case spv::BuiltIn::WarpsPerSMNV:
4425
0
    case spv::BuiltIn::SMCountNV:
4426
0
    case spv::BuiltIn::WarpIDNV:
4427
0
    case spv::BuiltIn::SMIDNV: {
4428
0
      return ValidateNVSMOrARMCoreBuiltinsAtDefinition(decoration, inst);
4429
0
    }
4430
0
    case spv::BuiltIn::BaseInstance:
4431
0
    case spv::BuiltIn::BaseVertex: {
4432
0
      return ValidateBaseInstanceOrVertexAtDefinition(decoration, inst);
4433
0
    }
4434
0
    case spv::BuiltIn::DrawIndex: {
4435
0
      return ValidateDrawIndexAtDefinition(decoration, inst);
4436
0
    }
4437
0
    case spv::BuiltIn::ViewIndex: {
4438
0
      return ValidateViewIndexAtDefinition(decoration, inst);
4439
0
    }
4440
0
    case spv::BuiltIn::DeviceIndex: {
4441
0
      return ValidateDeviceIndexAtDefinition(decoration, inst);
4442
0
    }
4443
0
    case spv::BuiltIn::FragInvocationCountEXT: {
4444
      // alias spv::BuiltIn::InvocationsPerPixelNV
4445
0
      return ValidateFragInvocationCountAtDefinition(decoration, inst);
4446
0
    }
4447
0
    case spv::BuiltIn::FragSizeEXT: {
4448
      // alias spv::BuiltIn::FragmentSizeNV
4449
0
      return ValidateFragSizeAtDefinition(decoration, inst);
4450
0
    }
4451
0
    case spv::BuiltIn::FragStencilRefEXT: {
4452
0
      return ValidateFragStencilRefAtDefinition(decoration, inst);
4453
0
    }
4454
0
    case spv::BuiltIn::FullyCoveredEXT:{
4455
0
      return ValidateFullyCoveredAtDefinition(decoration, inst);
4456
0
    }
4457
    // Ray tracing builtins
4458
0
    case spv::BuiltIn::HitKindKHR:  // alias spv::BuiltIn::HitKindNV
4459
0
    case spv::BuiltIn::HitTNV:      // NOT present in KHR
4460
0
    case spv::BuiltIn::InstanceId:
4461
0
    case spv::BuiltIn::LaunchIdKHR:           // alias spv::BuiltIn::LaunchIdNV
4462
0
    case spv::BuiltIn::LaunchSizeKHR:         // alias spv::BuiltIn::LaunchSizeNV
4463
0
    case spv::BuiltIn::WorldRayOriginKHR:     // alias spv::BuiltIn::WorldRayOriginNV
4464
0
    case spv::BuiltIn::WorldRayDirectionKHR:  // alias spv::BuiltIn::WorldRayDirectionNV
4465
0
    case spv::BuiltIn::ObjectRayOriginKHR:    // alias spv::BuiltIn::ObjectRayOriginNV
4466
0
    case spv::BuiltIn::ObjectRayDirectionKHR:   // alias
4467
                                            // spv::BuiltIn::ObjectRayDirectionNV
4468
0
    case spv::BuiltIn::RayTminKHR:              // alias spv::BuiltIn::RayTminNV
4469
0
    case spv::BuiltIn::RayTmaxKHR:              // alias spv::BuiltIn::RayTmaxNV
4470
0
    case spv::BuiltIn::InstanceCustomIndexKHR:  // alias
4471
                                            // spv::BuiltIn::InstanceCustomIndexNV
4472
0
    case spv::BuiltIn::ObjectToWorldKHR:        // alias spv::BuiltIn::ObjectToWorldNV
4473
0
    case spv::BuiltIn::WorldToObjectKHR:        // alias spv::BuiltIn::WorldToObjectNV
4474
0
    case spv::BuiltIn::IncomingRayFlagsKHR:    // alias spv::BuiltIn::IncomingRayFlagsNV
4475
0
    case spv::BuiltIn::RayGeometryIndexKHR:    // NOT present in NV
4476
0
    case spv::BuiltIn::CullMaskKHR: {
4477
0
      return ValidateRayTracingBuiltinsAtDefinition(decoration, inst);
4478
0
    }
4479
0
    case spv::BuiltIn::PrimitivePointIndicesEXT:
4480
0
    case spv::BuiltIn::PrimitiveLineIndicesEXT:
4481
0
    case spv::BuiltIn::PrimitiveTriangleIndicesEXT: {
4482
0
      return ValidateMeshShadingEXTBuiltinsAtDefinition(decoration, inst);
4483
0
    }
4484
0
    case spv::BuiltIn::PrimitiveShadingRateKHR: {
4485
0
      return ValidatePrimitiveShadingRateAtDefinition(decoration, inst);
4486
0
    }
4487
0
    case spv::BuiltIn::ShadingRateKHR: {
4488
0
      return ValidateShadingRateAtDefinition(decoration, inst);
4489
0
    }
4490
0
    default:
4491
      // No validation rules (for the moment).
4492
0
      break;
4493
0
  }
4494
0
  return SPV_SUCCESS;
4495
0
}
4496
4497
11.4k
spv_result_t BuiltInsValidator::ValidateBuiltInsAtDefinition() {
4498
145k
  for (const auto& kv : _.id_decorations()) {
4499
145k
    const uint32_t id = kv.first;
4500
145k
    const auto& decorations = kv.second;
4501
145k
    if (decorations.empty()) {
4502
108k
      continue;
4503
108k
    }
4504
4505
36.8k
    const Instruction* inst = _.FindDef(id);
4506
36.8k
    assert(inst);
4507
4508
40.5k
    for (const auto& decoration : kv.second) {
4509
40.5k
      if (decoration.dec_type() != spv::Decoration::BuiltIn) {
4510
39.4k
        continue;
4511
39.4k
      }
4512
4513
1.12k
      if (spv_result_t error =
4514
1.12k
              ValidateSingleBuiltInAtDefinition(decoration, *inst)) {
4515
0
        return error;
4516
0
      }
4517
1.12k
    }
4518
36.8k
  }
4519
4520
11.4k
  return SPV_SUCCESS;
4521
11.4k
}
4522
4523
11.4k
spv_result_t BuiltInsValidator::Run() {
4524
  // First pass: validate all built-ins at definition and seed
4525
  // id_to_at_reference_checks_ with built-ins.
4526
11.4k
  if (auto error = ValidateBuiltInsAtDefinition()) {
4527
0
    return error;
4528
0
  }
4529
4530
11.4k
  if (id_to_at_reference_checks_.empty()) {
4531
    // No validation tasks were seeded. Nothing else to do.
4532
11.4k
    return SPV_SUCCESS;
4533
11.4k
  }
4534
4535
  // Second pass: validate every id reference in the module using
4536
  // rules in id_to_at_reference_checks_.
4537
0
  for (const Instruction& inst : _.ordered_instructions()) {
4538
0
    Update(inst);
4539
4540
0
    std::set<uint32_t> already_checked;
4541
4542
0
    for (const auto& operand : inst.operands()) {
4543
0
      if (!spvIsIdType(operand.type)) {
4544
        // Not id.
4545
0
        continue;
4546
0
      }
4547
4548
0
      const uint32_t id = inst.word(operand.offset);
4549
0
      if (id == inst.id()) {
4550
        // No need to check result id.
4551
0
        continue;
4552
0
      }
4553
4554
0
      if (!already_checked.insert(id).second) {
4555
        // The instruction has already referenced this id.
4556
0
        continue;
4557
0
      }
4558
4559
      // Instruction references the id. Run all checks associated with the id
4560
      // on the instruction. id_to_at_reference_checks_ can be modified in the
4561
      // process, iterators are safe because it's a tree-based map.
4562
0
      const auto it = id_to_at_reference_checks_.find(id);
4563
0
      if (it != id_to_at_reference_checks_.end()) {
4564
0
        for (const auto& check : it->second) {
4565
0
          if (spv_result_t error = check(inst)) {
4566
0
            return error;
4567
0
          }
4568
0
        }
4569
0
      }
4570
0
    }
4571
0
  }
4572
4573
0
  return SPV_SUCCESS;
4574
0
}
4575
4576
}  // namespace
4577
4578
// Validates correctness of built-in variables.
4579
11.4k
spv_result_t ValidateBuiltIns(ValidationState_t& _) {
4580
11.4k
  BuiltInsValidator validator(_);
4581
11.4k
  return validator.Run();
4582
11.4k
}
4583
4584
}  // namespace val
4585
}  // namespace spvtools