forked from KhronosGroup/SPIRV-Tools
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvalidate_image.cpp
More file actions
2840 lines (2504 loc) · 104 KB
/
validate_image.cpp
File metadata and controls
2840 lines (2504 loc) · 104 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// Copyright (c) 2017 Google Inc.
// Modifications Copyright (C) 2020 Advanced Micro Devices, Inc. All rights
// reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
// Validates correctness of image instructions.
#include <string>
#include "source/opcode.h"
#include "source/spirv_constant.h"
#include "source/spirv_target_env.h"
#include "source/util/bitutils.h"
#include "source/val/instruction.h"
#include "source/val/validate.h"
#include "source/val/validate_scopes.h"
#include "source/val/validation_state.h"
namespace spvtools {
namespace val {
namespace {
// Performs compile time check that all spv::ImageOperandsMask::XXX cases are
// handled in this module. If spv::ImageOperandsMask::XXX list changes, this
// function will fail the build. For all other purposes this is a placeholder
// function.
bool CheckAllImageOperandsHandled() {
spv::ImageOperandsMask enum_val = spv::ImageOperandsMask::Bias;
// Some improvised code to prevent the compiler from considering enum_val
// constant and optimizing the switch away.
uint32_t stack_var = 0;
if (reinterpret_cast<uintptr_t>(&stack_var) % 256)
enum_val = spv::ImageOperandsMask::Lod;
switch (enum_val) {
// Please update the validation rules in this module if you are changing
// the list of image operands, and add new enum values to this switch.
case spv::ImageOperandsMask::MaskNone:
return false;
case spv::ImageOperandsMask::Bias:
case spv::ImageOperandsMask::Lod:
case spv::ImageOperandsMask::Grad:
case spv::ImageOperandsMask::ConstOffset:
case spv::ImageOperandsMask::Offset:
case spv::ImageOperandsMask::ConstOffsets:
case spv::ImageOperandsMask::Sample:
case spv::ImageOperandsMask::MinLod:
// TODO(dneto): Support image operands related to the Vulkan memory model.
// https://gitlab.khronos.org/spirv/spirv-tools/issues/32
case spv::ImageOperandsMask::MakeTexelAvailableKHR:
case spv::ImageOperandsMask::MakeTexelVisibleKHR:
case spv::ImageOperandsMask::NonPrivateTexelKHR:
case spv::ImageOperandsMask::VolatileTexelKHR:
case spv::ImageOperandsMask::SignExtend:
case spv::ImageOperandsMask::ZeroExtend:
// TODO(jaebaek): Move this line properly after handling image offsets
// operand. This line temporarily fixes CI failure that
// blocks other PRs.
// https://github.com/KhronosGroup/SPIRV-Tools/issues/4565
case spv::ImageOperandsMask::Offsets:
case spv::ImageOperandsMask::Nontemporal:
return true;
}
return false;
}
// Used by GetImageTypeInfo. See OpTypeImage spec for more information.
struct ImageTypeInfo {
uint32_t sampled_type = 0;
spv::Dim dim = spv::Dim::Max;
uint32_t depth = 0;
uint32_t arrayed = 0;
uint32_t multisampled = 0;
uint32_t sampled = 0;
spv::ImageFormat format = spv::ImageFormat::Max;
spv::AccessQualifier access_qualifier = spv::AccessQualifier::Max;
};
// Provides information on image type. |id| should be object of either
// OpTypeImage or OpTypeSampledImage type. Returns false in case of failure
// (not a valid id, failed to parse the instruction, etc).
bool GetImageTypeInfo(const ValidationState_t& _, uint32_t id,
ImageTypeInfo* info) {
if (!id || !info) return false;
const Instruction* inst = _.FindDef(id);
assert(inst);
if (inst->opcode() == spv::Op::OpTypeSampledImage) {
inst = _.FindDef(inst->word(2));
assert(inst);
}
if (inst->opcode() != spv::Op::OpTypeImage) return false;
const size_t num_words = inst->words().size();
if (num_words != 9 && num_words != 10) return false;
info->sampled_type = inst->word(2);
info->dim = static_cast<spv::Dim>(inst->word(3));
info->depth = inst->word(4);
info->arrayed = inst->word(5);
info->multisampled = inst->word(6);
info->sampled = inst->word(7);
info->format = static_cast<spv::ImageFormat>(inst->word(8));
info->access_qualifier =
num_words < 10 ? spv::AccessQualifier::Max
: static_cast<spv::AccessQualifier>(inst->word(9));
return true;
}
bool IsImplicitLod(spv::Op opcode) {
switch (opcode) {
case spv::Op::OpImageSampleImplicitLod:
case spv::Op::OpImageSampleDrefImplicitLod:
case spv::Op::OpImageSampleProjImplicitLod:
case spv::Op::OpImageSampleProjDrefImplicitLod:
case spv::Op::OpImageSparseSampleImplicitLod:
case spv::Op::OpImageSparseSampleDrefImplicitLod:
case spv::Op::OpImageSparseSampleProjImplicitLod:
case spv::Op::OpImageSparseSampleProjDrefImplicitLod:
return true;
default:
break;
}
return false;
}
bool IsExplicitLod(spv::Op opcode) {
switch (opcode) {
case spv::Op::OpImageSampleExplicitLod:
case spv::Op::OpImageSampleDrefExplicitLod:
case spv::Op::OpImageSampleProjExplicitLod:
case spv::Op::OpImageSampleProjDrefExplicitLod:
case spv::Op::OpImageSparseSampleExplicitLod:
case spv::Op::OpImageSparseSampleDrefExplicitLod:
case spv::Op::OpImageSparseSampleProjExplicitLod:
case spv::Op::OpImageSparseSampleProjDrefExplicitLod:
return true;
default:
break;
}
return false;
}
bool IsValidLodOperand(const ValidationState_t& _, spv::Op opcode) {
switch (opcode) {
case spv::Op::OpImageRead:
case spv::Op::OpImageWrite:
case spv::Op::OpImageSparseRead:
return _.HasCapability(spv::Capability::ImageReadWriteLodAMD);
default:
return IsExplicitLod(opcode);
}
}
bool IsValidGatherLodBiasAMD(const ValidationState_t& _, spv::Op opcode) {
switch (opcode) {
case spv::Op::OpImageGather:
case spv::Op::OpImageSparseGather:
return _.HasCapability(spv::Capability::ImageGatherBiasLodAMD);
default:
break;
}
return false;
}
// Signed or Unsigned Integer Format
bool IsIntImageFormat(spv::ImageFormat format) {
switch (format) {
case spv::ImageFormat::Rgba32i:
case spv::ImageFormat::Rgba16i:
case spv::ImageFormat::Rgba8i:
case spv::ImageFormat::R32i:
case spv::ImageFormat::Rg32i:
case spv::ImageFormat::Rg16i:
case spv::ImageFormat::Rg8i:
case spv::ImageFormat::R16i:
case spv::ImageFormat::R8i:
case spv::ImageFormat::Rgba32ui:
case spv::ImageFormat::Rgba16ui:
case spv::ImageFormat::Rgba8ui:
case spv::ImageFormat::R32ui:
case spv::ImageFormat::Rgb10a2ui:
case spv::ImageFormat::Rg32ui:
case spv::ImageFormat::Rg16ui:
case spv::ImageFormat::Rg8ui:
case spv::ImageFormat::R16ui:
case spv::ImageFormat::R8ui:
case spv::ImageFormat::R64ui:
case spv::ImageFormat::R64i:
return true;
default:
break;
}
return false;
}
bool IsInt64ImageFormat(spv::ImageFormat format) {
switch (format) {
case spv::ImageFormat::R64ui:
case spv::ImageFormat::R64i:
return true;
default:
break;
}
return false;
}
bool IsSignedIntImageFormat(spv::ImageFormat format) {
switch (format) {
case spv::ImageFormat::Rgba32i:
case spv::ImageFormat::Rgba16i:
case spv::ImageFormat::Rgba8i:
case spv::ImageFormat::R32i:
case spv::ImageFormat::Rg32i:
case spv::ImageFormat::Rg16i:
case spv::ImageFormat::Rg8i:
case spv::ImageFormat::R16i:
case spv::ImageFormat::R8i:
case spv::ImageFormat::R64i:
return true;
default:
break;
}
return false;
}
bool IsFloatImageFormat(spv::ImageFormat format) {
switch (format) {
case spv::ImageFormat::Rgba32f:
case spv::ImageFormat::Rgba16f:
case spv::ImageFormat::R32f:
case spv::ImageFormat::Rgba8:
case spv::ImageFormat::Rgba8Snorm:
case spv::ImageFormat::Rg32f:
case spv::ImageFormat::Rg16f:
case spv::ImageFormat::R11fG11fB10f:
case spv::ImageFormat::R16f:
case spv::ImageFormat::Rgba16:
case spv::ImageFormat::Rgb10A2:
case spv::ImageFormat::Rg16:
case spv::ImageFormat::Rg8:
case spv::ImageFormat::R16:
case spv::ImageFormat::R8:
case spv::ImageFormat::Rgba16Snorm:
case spv::ImageFormat::Rg16Snorm:
case spv::ImageFormat::Rg8Snorm:
case spv::ImageFormat::R16Snorm:
case spv::ImageFormat::R8Snorm:
return true;
default:
break;
}
return false;
}
bool IsImageSparse(spv::Op opcode) {
switch (opcode) {
case spv::Op::OpImageSparseSampleImplicitLod:
case spv::Op::OpImageSparseSampleExplicitLod:
case spv::Op::OpImageSparseSampleDrefImplicitLod:
case spv::Op::OpImageSparseSampleDrefExplicitLod:
case spv::Op::OpImageSparseSampleProjImplicitLod:
case spv::Op::OpImageSparseSampleProjExplicitLod:
case spv::Op::OpImageSparseSampleProjDrefImplicitLod:
case spv::Op::OpImageSparseSampleProjDrefExplicitLod:
case spv::Op::OpImageSparseFetch:
case spv::Op::OpImageSparseGather:
case spv::Op::OpImageSparseDrefGather:
case spv::Op::OpImageSparseTexelsResident:
case spv::Op::OpImageSparseRead:
return true;
default:
break;
}
return false;
}
// Returns true if the opcode is a Image instruction which applies
// homogenous projection to the coordinates.
bool IsProj(spv::Op opcode) {
switch (opcode) {
case spv::Op::OpImageSampleProjImplicitLod:
case spv::Op::OpImageSampleProjDrefImplicitLod:
case spv::Op::OpImageSparseSampleProjImplicitLod:
case spv::Op::OpImageSparseSampleProjDrefImplicitLod:
case spv::Op::OpImageSampleProjExplicitLod:
case spv::Op::OpImageSampleProjDrefExplicitLod:
case spv::Op::OpImageSparseSampleProjExplicitLod:
case spv::Op::OpImageSparseSampleProjDrefExplicitLod:
return true;
default:
break;
}
return false;
}
// Returns the number of components in a coordinate used to access a texel in
// a single plane of an image with the given parameters.
uint32_t GetPlaneCoordSize(const ImageTypeInfo& info) {
uint32_t plane_size = 0;
// If this switch breaks your build, please add new values below.
switch (info.dim) {
case spv::Dim::Dim1D:
case spv::Dim::Buffer:
plane_size = 1;
break;
case spv::Dim::Dim2D:
case spv::Dim::Rect:
case spv::Dim::SubpassData:
case spv::Dim::TileImageDataEXT:
plane_size = 2;
break;
case spv::Dim::Dim3D:
case spv::Dim::Cube:
// For Cube direction vector is used instead of UV.
plane_size = 3;
break;
case spv::Dim::Max:
default:
assert(0);
break;
}
return plane_size;
}
// Returns minimal number of coordinates based on image dim, arrayed and whether
// the instruction uses projection coordinates.
uint32_t GetMinCoordSize(spv::Op opcode, const ImageTypeInfo& info) {
if (info.dim == spv::Dim::Cube &&
(opcode == spv::Op::OpImageRead || opcode == spv::Op::OpImageWrite ||
opcode == spv::Op::OpImageSparseRead)) {
// These opcodes use UV for Cube, not direction vector.
return 3;
}
if (opcode == spv::Op::OpImageQueryLod) {
return GetPlaneCoordSize(info);
}
if (opcode == spv::Op::OpImageTexelPointer) {
if (info.arrayed == 0) {
return GetPlaneCoordSize(info);
} else if (info.dim == spv::Dim::Dim1D) {
return 2;
} else if (info.dim == spv::Dim::Cube || info.dim == spv::Dim::Dim2D) {
return 3;
} else {
assert(false);
return 0; // caught elsewhere
}
}
return GetPlaneCoordSize(info) + info.arrayed + (IsProj(opcode) ? 1 : 0);
}
// Checks ImageOperand bitfield and respective operands.
// word_index is the index of the first word after the image-operand mask word.
spv_result_t ValidateImageOperands(ValidationState_t& _,
const Instruction* inst,
const ImageTypeInfo& info,
uint32_t word_index) {
static const bool kAllImageOperandsHandled = CheckAllImageOperandsHandled();
(void)kAllImageOperandsHandled;
const spv::Op opcode = inst->opcode();
const size_t num_words = inst->words().size();
const bool have_explicit_mask = (word_index - 1 < num_words);
const uint32_t mask = have_explicit_mask ? inst->word(word_index - 1) : 0u;
if (have_explicit_mask) {
// NonPrivate, Volatile, SignExtend, ZeroExtend take no operand words.
const uint32_t mask_bits_having_operands =
mask & ~uint32_t(spv::ImageOperandsMask::NonPrivateTexelKHR |
spv::ImageOperandsMask::VolatileTexelKHR |
spv::ImageOperandsMask::SignExtend |
spv::ImageOperandsMask::ZeroExtend |
spv::ImageOperandsMask::Nontemporal);
size_t expected_num_image_operand_words =
spvtools::utils::CountSetBits(mask_bits_having_operands);
if (mask & uint32_t(spv::ImageOperandsMask::Grad)) {
// Grad uses two words.
++expected_num_image_operand_words;
}
if (expected_num_image_operand_words != num_words - word_index) {
return _.diag(SPV_ERROR_INVALID_DATA, inst)
<< "Number of image operand ids doesn't correspond to the bit "
"mask";
}
} else if (num_words != word_index - 1) {
return _.diag(SPV_ERROR_INVALID_DATA, inst)
<< "Number of image operand ids doesn't correspond to the bit mask";
}
if (info.multisampled &
(0 == (mask & uint32_t(spv::ImageOperandsMask::Sample)))) {
return _.diag(SPV_ERROR_INVALID_DATA, inst)
<< "Image Operand Sample is required for operation on "
"multi-sampled image";
}
// The following OpTypeImage checks are done here as they depend of if the
// SignExtend and ZeroExtend are used to override the signedness
const bool is_sign_extend =
mask & uint32_t(spv::ImageOperandsMask::SignExtend);
const bool is_zero_extend =
mask & uint32_t(spv::ImageOperandsMask::ZeroExtend);
if (spvIsVulkanEnv(_.context()->target_env)) {
if (info.format != spv::ImageFormat::Unknown &&
_.IsIntScalarType(info.sampled_type)) {
const bool is_format_signed = IsSignedIntImageFormat(info.format);
// (vkspec.html#spirvenv-image-signedness) has order signedness is set by
bool is_sampled_type_signed =
is_sign_extend
? true
: (is_zero_extend
? false
: (_.IsSignedIntScalarType(info.sampled_type) ? true
: false));
if (is_format_signed != is_sampled_type_signed) {
return _.diag(SPV_ERROR_INVALID_DATA, inst)
<< _.VkErrorID(4965)
<< "Image Format signedness does not match Sample Type operand "
"including possible SignExtend or ZeroExtend operand";
}
}
}
// After this point, only set bits in the image operands mask can cause
// the module to be invalid.
if (mask == 0) return SPV_SUCCESS;
if (spvtools::utils::CountSetBits(
mask & uint32_t(spv::ImageOperandsMask::Offset |
spv::ImageOperandsMask::ConstOffset |
spv::ImageOperandsMask::ConstOffsets |
spv::ImageOperandsMask::Offsets)) > 1) {
return _.diag(SPV_ERROR_INVALID_DATA, inst)
<< "Image Operands Offset, ConstOffset, ConstOffsets, Offsets "
"cannot be used together";
}
const bool is_implicit_lod = IsImplicitLod(opcode);
const bool is_explicit_lod = IsExplicitLod(opcode);
const bool is_valid_lod_operand = IsValidLodOperand(_, opcode);
const bool is_valid_gather_lod_bias_amd = IsValidGatherLodBiasAMD(_, opcode);
// The checks should be done in the order of definition of OperandImage.
if (mask & uint32_t(spv::ImageOperandsMask::Bias)) {
if (!is_implicit_lod && !is_valid_gather_lod_bias_amd) {
return _.diag(SPV_ERROR_INVALID_DATA, inst)
<< "Image Operand Bias can only be used with ImplicitLod opcodes";
}
const uint32_t bias_type_id = _.GetTypeId(inst->word(word_index++));
if (!_.IsFloatScalarType(bias_type_id, 32)) {
return _.diag(SPV_ERROR_INVALID_DATA, inst)
<< "Expected Image Operand Bias to be a 32-bit float scalar";
}
if (info.dim != spv::Dim::Dim1D && info.dim != spv::Dim::Dim2D &&
info.dim != spv::Dim::Dim3D && info.dim != spv::Dim::Cube) {
return _.diag(SPV_ERROR_INVALID_DATA, inst)
<< "Image Operand Bias requires 'Dim' parameter to be 1D, 2D, 3D "
"or Cube";
}
// - |Sample| operand is required to have MS != 0
// - |Sample| is only allowed with [Fetch, Write, or Read]
// - |Bias| can only be used with |ImplicitLod| opcodes
// Multisampled is already checked in all cases
}
if (mask & uint32_t(spv::ImageOperandsMask::Lod)) {
if (!is_valid_lod_operand && opcode != spv::Op::OpImageFetch &&
opcode != spv::Op::OpImageSparseFetch &&
!is_valid_gather_lod_bias_amd) {
return _.diag(SPV_ERROR_INVALID_DATA, inst)
<< "Image Operand Lod can only be used with ExplicitLod opcodes "
<< "and OpImageFetch";
}
if (mask & uint32_t(spv::ImageOperandsMask::Grad)) {
return _.diag(SPV_ERROR_INVALID_DATA, inst)
<< "Image Operand bits Lod and Grad cannot be set at the same "
"time";
}
const uint32_t lod_type_id = _.GetTypeId(inst->word(word_index++));
if (is_explicit_lod || is_valid_gather_lod_bias_amd) {
if (!_.IsFloatScalarType(lod_type_id, 32)) {
return _.diag(SPV_ERROR_INVALID_DATA, inst)
<< "Expected Image Operand Lod to be a 32-bit float scalar when "
"used "
<< "with ExplicitLod";
}
} else {
if (!_.IsIntScalarType(lod_type_id, 32)) {
return _.diag(SPV_ERROR_INVALID_DATA, inst)
<< "Expected Image Operand Lod to be a 32-bit int scalar when "
"used with "
<< "OpImageFetch";
}
}
if (info.dim != spv::Dim::Dim1D && info.dim != spv::Dim::Dim2D &&
info.dim != spv::Dim::Dim3D && info.dim != spv::Dim::Cube) {
return _.diag(SPV_ERROR_INVALID_DATA, inst)
<< "Image Operand Lod requires 'Dim' parameter to be 1D, 2D, 3D "
"or Cube";
}
if (info.multisampled != 0) {
return _.diag(SPV_ERROR_INVALID_DATA, inst)
<< "Image Operand Lod requires 'MS' parameter to be 0";
}
}
if (mask & uint32_t(spv::ImageOperandsMask::Grad)) {
if (!is_explicit_lod) {
return _.diag(SPV_ERROR_INVALID_DATA, inst)
<< "Image Operand Grad can only be used with ExplicitLod opcodes";
}
const uint32_t dx_type_id = _.GetTypeId(inst->word(word_index++));
const uint32_t dy_type_id = _.GetTypeId(inst->word(word_index++));
if (!_.IsFloatScalarOrVectorType(dx_type_id) ||
_.GetBitWidth(dx_type_id) != 32 ||
!_.IsFloatScalarOrVectorType(dy_type_id) ||
_.GetBitWidth(dy_type_id) != 32) {
return _.diag(SPV_ERROR_INVALID_DATA, inst)
<< "Expected both Image Operand Grad ids to be 32-bit float "
"scalars or "
<< "vectors";
}
const uint32_t plane_size = GetPlaneCoordSize(info);
const uint32_t dx_size = _.GetDimension(dx_type_id);
const uint32_t dy_size = _.GetDimension(dy_type_id);
if (plane_size != dx_size) {
return _.diag(SPV_ERROR_INVALID_DATA, inst)
<< "Expected Image Operand Grad dx to have " << plane_size
<< " components, but given " << dx_size;
}
if (plane_size != dy_size) {
return _.diag(SPV_ERROR_INVALID_DATA, inst)
<< "Expected Image Operand Grad dy to have " << plane_size
<< " components, but given " << dy_size;
}
// - |Sample| operand is required to have MS != 0
// - |Sample| is only allowed with [Fetch, Write, or Read]
// - |Grad| can only be used with |ExplicitLod| opcodes
// Multisampled is already checked in all cases
}
if (mask & uint32_t(spv::ImageOperandsMask::ConstOffset)) {
if (info.dim == spv::Dim::Cube) {
return _.diag(SPV_ERROR_INVALID_DATA, inst)
<< "Image Operand ConstOffset cannot be used with Cube Image "
"'Dim'";
}
const uint32_t offset_id = inst->word(word_index++);
const uint32_t offset_type_id = _.GetTypeId(offset_id);
if (!_.IsIntScalarOrVectorType(offset_type_id) ||
_.GetBitWidth(offset_type_id) != 32) {
return _.diag(SPV_ERROR_INVALID_DATA, inst)
<< "Expected Image Operand ConstOffset to be a 32-bit int scalar "
"or "
<< "vector";
}
if (!spvOpcodeIsConstant(_.GetIdOpcode(offset_id))) {
return _.diag(SPV_ERROR_INVALID_DATA, inst)
<< "Expected Image Operand ConstOffset to be a const object";
}
const uint32_t plane_size = GetPlaneCoordSize(info);
const uint32_t offset_size = _.GetDimension(offset_type_id);
if (plane_size != offset_size) {
return _.diag(SPV_ERROR_INVALID_DATA, inst)
<< "Expected Image Operand ConstOffset to have " << plane_size
<< " components, but given " << offset_size;
}
}
if (mask & uint32_t(spv::ImageOperandsMask::Offset)) {
if (info.dim == spv::Dim::Cube) {
return _.diag(SPV_ERROR_INVALID_DATA, inst)
<< "Image Operand Offset cannot be used with Cube Image 'Dim'";
}
const uint32_t offset_id = inst->word(word_index++);
const uint32_t offset_type_id = _.GetTypeId(offset_id);
if (!_.IsIntScalarOrVectorType(offset_type_id) ||
_.GetBitWidth(offset_type_id) != 32) {
return _.diag(SPV_ERROR_INVALID_DATA, inst)
<< "Expected Image Operand Offset to be a 32-bit int scalar or "
<< "vector";
}
const uint32_t plane_size = GetPlaneCoordSize(info);
const uint32_t offset_size = _.GetDimension(offset_type_id);
if (plane_size != offset_size) {
return _.diag(SPV_ERROR_INVALID_DATA, inst)
<< "Expected Image Operand Offset to have " << plane_size
<< " components, but given " << offset_size;
}
if (!_.options()->before_hlsl_legalization &&
spvIsVulkanEnv(_.context()->target_env) &&
!_.options()->allow_offset_texture_operand) {
if (opcode != spv::Op::OpImageGather &&
opcode != spv::Op::OpImageDrefGather &&
opcode != spv::Op::OpImageSparseGather &&
opcode != spv::Op::OpImageSparseDrefGather) {
return _.diag(SPV_ERROR_INVALID_DATA, inst)
<< _.VkErrorID(10213)
<< "Image Operand Offset can only be used with "
"OpImage*Gather operations."
<< _.MissingFeature("maintenance8 feature",
"--allow-offset-texture-operand", false);
}
}
}
if (mask & uint32_t(spv::ImageOperandsMask::ConstOffsets)) {
if (opcode != spv::Op::OpImageGather &&
opcode != spv::Op::OpImageDrefGather &&
opcode != spv::Op::OpImageSparseGather &&
opcode != spv::Op::OpImageSparseDrefGather) {
return _.diag(SPV_ERROR_INVALID_DATA, inst)
<< "Image Operand ConstOffsets can only be used with "
"OpImageGather and OpImageDrefGather";
}
if (info.dim == spv::Dim::Cube) {
return _.diag(SPV_ERROR_INVALID_DATA, inst)
<< "Image Operand ConstOffsets cannot be used with Cube Image "
"'Dim'";
}
const uint32_t offset_id = inst->word(word_index++);
const uint32_t offset_type_id = _.GetTypeId(offset_id);
const Instruction* type_inst = _.FindDef(offset_type_id);
assert(type_inst);
if (type_inst->opcode() != spv::Op::OpTypeArray) {
return _.diag(SPV_ERROR_INVALID_DATA, inst)
<< "Expected Image Operand ConstOffsets to be an array of size 4";
}
uint64_t array_size = 0;
if (!_.EvalConstantValUint64(type_inst->word(3), &array_size)) {
assert(0 && "Array type definition is corrupt");
}
if (array_size != 4) {
return _.diag(SPV_ERROR_INVALID_DATA, inst)
<< "Expected Image Operand ConstOffsets to be an array of size 4";
}
const uint32_t component_type = type_inst->word(2);
if (!_.IsIntVectorType(component_type) ||
_.GetDimension(component_type) != 2 ||
_.GetBitWidth(component_type) != 32) {
return _.diag(SPV_ERROR_INVALID_DATA, inst)
<< "Expected Image Operand ConstOffsets array components to be a "
"32-bit int vectors of size 2";
}
if (!spvOpcodeIsConstant(_.GetIdOpcode(offset_id))) {
return _.diag(SPV_ERROR_INVALID_DATA, inst)
<< "Expected Image Operand ConstOffsets to be a const object";
}
}
if (mask & uint32_t(spv::ImageOperandsMask::Sample)) {
if (opcode != spv::Op::OpImageFetch && opcode != spv::Op::OpImageRead &&
opcode != spv::Op::OpImageWrite &&
opcode != spv::Op::OpImageSparseFetch &&
opcode != spv::Op::OpImageSparseRead) {
return _.diag(SPV_ERROR_INVALID_DATA, inst)
<< "Image Operand Sample can only be used with OpImageFetch, "
<< "OpImageRead, OpImageWrite, OpImageSparseFetch and "
<< "OpImageSparseRead";
}
if (info.multisampled == 0) {
return _.diag(SPV_ERROR_INVALID_DATA, inst)
<< "Image Operand Sample requires non-zero 'MS' parameter";
}
const uint32_t sample_type_id = _.GetTypeId(inst->word(word_index++));
if (!_.IsIntScalarType(sample_type_id, 32)) {
return _.diag(SPV_ERROR_INVALID_DATA, inst)
<< "Expected Image Operand Sample to be a 32-bit int scalar";
}
}
if (mask & uint32_t(spv::ImageOperandsMask::MinLod)) {
if (!is_implicit_lod && !(mask & uint32_t(spv::ImageOperandsMask::Grad))) {
return _.diag(SPV_ERROR_INVALID_DATA, inst)
<< "Image Operand MinLod can only be used with ImplicitLod "
<< "opcodes or together with Image Operand Grad";
}
const uint32_t minlod_type_id = _.GetTypeId(inst->word(word_index++));
if (!_.IsFloatScalarType(minlod_type_id, 32)) {
return _.diag(SPV_ERROR_INVALID_DATA, inst)
<< "Expected Image Operand MinLod to be a 32-bit float scalar";
}
if (info.dim != spv::Dim::Dim1D && info.dim != spv::Dim::Dim2D &&
info.dim != spv::Dim::Dim3D && info.dim != spv::Dim::Cube) {
return _.diag(SPV_ERROR_INVALID_DATA, inst)
<< "Image Operand MinLod requires 'Dim' parameter to be 1D, 2D, "
"3D or Cube";
}
if (info.multisampled != 0) {
return _.diag(SPV_ERROR_INVALID_DATA, inst)
<< "Image Operand MinLod requires 'MS' parameter to be 0";
}
}
if (mask & uint32_t(spv::ImageOperandsMask::MakeTexelAvailableKHR)) {
// Checked elsewhere: capability and memory model are correct.
if (opcode != spv::Op::OpImageWrite) {
return _.diag(SPV_ERROR_INVALID_DATA, inst)
<< "Image Operand MakeTexelAvailableKHR can only be used with Op"
<< spvOpcodeString(spv::Op::OpImageWrite) << ": Op"
<< spvOpcodeString(opcode);
}
if (!(mask & uint32_t(spv::ImageOperandsMask::NonPrivateTexelKHR))) {
return _.diag(SPV_ERROR_INVALID_DATA, inst)
<< "Image Operand MakeTexelAvailableKHR requires "
"NonPrivateTexelKHR is also specified: Op"
<< spvOpcodeString(opcode);
}
const auto available_scope = inst->word(word_index++);
if (auto error = ValidateMemoryScope(_, inst, available_scope))
return error;
}
if (mask & uint32_t(spv::ImageOperandsMask::MakeTexelVisibleKHR)) {
// Checked elsewhere: capability and memory model are correct.
if (opcode != spv::Op::OpImageRead &&
opcode != spv::Op::OpImageSparseRead) {
return _.diag(SPV_ERROR_INVALID_DATA, inst)
<< "Image Operand MakeTexelVisibleKHR can only be used with Op"
<< spvOpcodeString(spv::Op::OpImageRead) << " or Op"
<< spvOpcodeString(spv::Op::OpImageSparseRead) << ": Op"
<< spvOpcodeString(opcode);
}
if (!(mask & uint32_t(spv::ImageOperandsMask::NonPrivateTexelKHR))) {
return _.diag(SPV_ERROR_INVALID_DATA, inst)
<< "Image Operand MakeTexelVisibleKHR requires NonPrivateTexelKHR "
"is also specified: Op"
<< spvOpcodeString(opcode);
}
const auto visible_scope = inst->word(word_index++);
if (auto error = ValidateMemoryScope(_, inst, visible_scope)) return error;
}
// Checked elsewhere: SPIR-V 1.4 version or later.
if (is_sign_extend || is_zero_extend) {
// We don't have enough information to know what the |texel value type| is.
// In OpenCL, knowledge is deferred until runtime: the image SampledType is
// void, and the Format is Unknown.
// In Vulkan, the texel type is only known in all cases by the pipeline
// setup.
if (opcode == spv::Op::OpImageWrite) {
// OpImageWrite has no result type.
// TODO - Add Validation
} else if (IsImageSparse(opcode)) {
// Sparse image read/sample return a struct.
// TODO - Add Validation
} else {
if (is_sign_extend && !_.IsIntScalarOrVectorType(inst->type_id())) {
return _.diag(SPV_ERROR_INVALID_DATA, inst)
<< "Using SignExtend, but result type is not a scalar or vector "
"integer type.";
} else if (is_zero_extend &&
!_.IsUnsignedIntScalarOrVectorType(inst->type_id())) {
return _.diag(SPV_ERROR_INVALID_DATA, inst)
<< "Using ZeroExtend, but result type is a signed integer type.";
}
}
}
if (mask & uint32_t(spv::ImageOperandsMask::Offsets)) {
// TODO: add validation
}
if (mask & uint32_t(spv::ImageOperandsMask::Nontemporal)) {
// Checked elsewhere: SPIR-V 1.6 version or later.
}
return SPV_SUCCESS;
}
// Validate OpImage*Proj* instructions
spv_result_t ValidateImageProj(ValidationState_t& _, const Instruction* inst,
const ImageTypeInfo& info) {
if (info.dim != spv::Dim::Dim1D && info.dim != spv::Dim::Dim2D &&
info.dim != spv::Dim::Dim3D && info.dim != spv::Dim::Rect) {
return _.diag(SPV_ERROR_INVALID_DATA, inst)
<< "Expected Image 'Dim' parameter to be 1D, 2D, 3D or Rect";
}
if (info.multisampled != 0) {
return _.diag(SPV_ERROR_INVALID_DATA, inst)
<< "Expected Image 'MS' parameter to be 0";
}
if (info.arrayed != 0) {
return _.diag(SPV_ERROR_INVALID_DATA, inst)
<< "Expected Image 'arrayed' parameter to be 0";
}
return SPV_SUCCESS;
}
// Validate OpImage*Read and OpImage*Write instructions
spv_result_t ValidateImageReadWrite(ValidationState_t& _,
const Instruction* inst,
const ImageTypeInfo& info) {
if (info.sampled == 2) {
if (info.dim == spv::Dim::Dim1D &&
!_.HasCapability(spv::Capability::Image1D)) {
return _.diag(SPV_ERROR_INVALID_DATA, inst)
<< "Capability Image1D is required to access storage image";
} else if (info.dim == spv::Dim::Rect &&
!_.HasCapability(spv::Capability::ImageRect)) {
return _.diag(SPV_ERROR_INVALID_DATA, inst)
<< "Capability ImageRect is required to access storage image";
} else if (info.dim == spv::Dim::Buffer &&
!_.HasCapability(spv::Capability::ImageBuffer)) {
return _.diag(SPV_ERROR_INVALID_DATA, inst)
<< "Capability ImageBuffer is required to access storage image";
} else if (info.dim == spv::Dim::Cube && info.arrayed == 1 &&
!_.HasCapability(spv::Capability::ImageCubeArray)) {
return _.diag(SPV_ERROR_INVALID_DATA, inst)
<< "Capability ImageCubeArray is required to access "
<< "storage image";
}
if (info.multisampled == 1 && info.arrayed == 1 && info.sampled == 2 &&
!_.HasCapability(spv::Capability::ImageMSArray)) {
return _.diag(SPV_ERROR_INVALID_DATA, inst)
<< "Capability ImageMSArray is required to access storage "
<< "image";
}
} else if (info.sampled != 0) {
return _.diag(SPV_ERROR_INVALID_DATA, inst)
<< "Expected Image 'Sampled' parameter to be 0 or 2";
}
return SPV_SUCCESS;
}
// Returns true if opcode is *ImageSparse*, false otherwise.
bool IsSparse(spv::Op opcode) {
switch (opcode) {
case spv::Op::OpImageSparseSampleImplicitLod:
case spv::Op::OpImageSparseSampleExplicitLod:
case spv::Op::OpImageSparseSampleDrefImplicitLod:
case spv::Op::OpImageSparseSampleDrefExplicitLod:
case spv::Op::OpImageSparseSampleProjImplicitLod:
case spv::Op::OpImageSparseSampleProjExplicitLod:
case spv::Op::OpImageSparseSampleProjDrefImplicitLod:
case spv::Op::OpImageSparseSampleProjDrefExplicitLod:
case spv::Op::OpImageSparseFetch:
case spv::Op::OpImageSparseGather:
case spv::Op::OpImageSparseDrefGather:
case spv::Op::OpImageSparseTexelsResident:
case spv::Op::OpImageSparseRead: {
return true;
}
default: { return false; }
}
return false;
}
// Checks sparse image opcode result type and returns the second struct member.
// Returns inst.type_id for non-sparse image opcodes.
// Not valid for sparse image opcodes which do not return a struct.
spv_result_t GetActualResultType(ValidationState_t& _, const Instruction* inst,
uint32_t* actual_result_type) {
const spv::Op opcode = inst->opcode();
if (IsSparse(opcode)) {
const Instruction* const type_inst = _.FindDef(inst->type_id());
assert(type_inst);
if (!type_inst || type_inst->opcode() != spv::Op::OpTypeStruct) {
return _.diag(SPV_ERROR_INVALID_DATA, inst)
<< "Expected Result Type to be OpTypeStruct";
}
if (type_inst->words().size() != 4 ||
!_.IsIntScalarType(type_inst->word(2))) {
return _.diag(SPV_ERROR_INVALID_DATA, inst)
<< "Expected Result Type to be a struct containing an int "
"scalar and a texel";
}
*actual_result_type = type_inst->word(3);
} else {
*actual_result_type = inst->type_id();
}
return SPV_SUCCESS;
}
// Returns a string describing actual result type of an opcode.
// Not valid for sparse image opcodes which do not return a struct.
const char* GetActualResultTypeStr(spv::Op opcode) {
if (IsSparse(opcode)) return "Result Type's second member";
return "Result Type";
}
spv_result_t ValidateTypeImage(ValidationState_t& _, const Instruction* inst) {
assert(inst->type_id() == 0);
ImageTypeInfo info;
if (!GetImageTypeInfo(_, inst->word(1), &info)) {
return _.diag(SPV_ERROR_INVALID_DATA, inst)
<< "Corrupt image type definition";
}
if (_.IsIntScalarType(info.sampled_type, 64) &&
!_.HasCapability(spv::Capability::Int64ImageEXT)) {
return _.diag(SPV_ERROR_INVALID_DATA, inst)
<< "Capability Int64ImageEXT is required when using Sampled Type of "
"64-bit int";
}
const auto target_env = _.context()->target_env;
if (spvIsVulkanEnv(target_env)) {
if (!_.IsFloatScalarType(info.sampled_type, 32) &&
!_.IsIntScalarType(info.sampled_type, 32) &&
!_.IsIntScalarType(info.sampled_type, 64)) {
return _.diag(SPV_ERROR_INVALID_DATA, inst)
<< _.VkErrorID(4656)
<< "Expected Sampled Type to be a 32-bit int, 64-bit int or "
"32-bit float scalar type for Vulkan environment";
}
} else if (spvIsOpenCLEnv(target_env)) {
if (!_.IsVoidType(info.sampled_type)) {
return _.diag(SPV_ERROR_INVALID_DATA, inst)
<< "Sampled Type must be OpTypeVoid in the OpenCL environment.";
}
} else {
const spv::Op sampled_type_opcode = _.GetIdOpcode(info.sampled_type);
if (sampled_type_opcode != spv::Op::OpTypeVoid &&
sampled_type_opcode != spv::Op::OpTypeInt &&
sampled_type_opcode != spv::Op::OpTypeFloat) {
return _.diag(SPV_ERROR_INVALID_DATA, inst)
<< "Expected Sampled Type to be either void or"
<< " numerical scalar type";
}
}
// Universal checks on image type operands
// Dim and Format and Access Qualifier are checked elsewhere.
if (info.depth > 2) {
return _.diag(SPV_ERROR_INVALID_DATA, inst)
<< "Invalid Depth " << info.depth << " (must be 0, 1 or 2)";
}