validate_image.cpp 77 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158
  1. // Copyright (c) 2017 Google Inc.
  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. // Validates correctness of image instructions.
  17. #include <string>
  18. #include "source/diagnostic.h"
  19. #include "source/opcode.h"
  20. #include "source/spirv_target_env.h"
  21. #include "source/util/bitutils.h"
  22. #include "source/val/instruction.h"
  23. #include "source/val/validate.h"
  24. #include "source/val/validate_scopes.h"
  25. #include "source/val/validation_state.h"
  26. namespace spvtools {
  27. namespace val {
  28. namespace {
  29. // Performs compile time check that all SpvImageOperandsXXX cases are handled in
  30. // this module. If SpvImageOperandsXXX list changes, this function will fail the
  31. // build.
  32. // For all other purposes this is a placeholder function.
  33. bool CheckAllImageOperandsHandled() {
  34. SpvImageOperandsMask enum_val = SpvImageOperandsBiasMask;
  35. // Some improvised code to prevent the compiler from considering enum_val
  36. // constant and optimizing the switch away.
  37. uint32_t stack_var = 0;
  38. if (reinterpret_cast<uintptr_t>(&stack_var) % 256)
  39. enum_val = SpvImageOperandsLodMask;
  40. switch (enum_val) {
  41. // Please update the validation rules in this module if you are changing
  42. // the list of image operands, and add new enum values to this switch.
  43. case SpvImageOperandsMaskNone:
  44. return false;
  45. case SpvImageOperandsBiasMask:
  46. case SpvImageOperandsLodMask:
  47. case SpvImageOperandsGradMask:
  48. case SpvImageOperandsConstOffsetMask:
  49. case SpvImageOperandsOffsetMask:
  50. case SpvImageOperandsConstOffsetsMask:
  51. case SpvImageOperandsSampleMask:
  52. case SpvImageOperandsMinLodMask:
  53. // TODO(dneto): Support image operands related to the Vulkan memory model.
  54. // https://gitlab.khronos.org/spirv/spirv-tools/issues/32
  55. case SpvImageOperandsMakeTexelAvailableKHRMask:
  56. case SpvImageOperandsMakeTexelVisibleKHRMask:
  57. case SpvImageOperandsNonPrivateTexelKHRMask:
  58. case SpvImageOperandsVolatileTexelKHRMask:
  59. case SpvImageOperandsSignExtendMask:
  60. case SpvImageOperandsZeroExtendMask:
  61. return true;
  62. }
  63. return false;
  64. }
  65. // Used by GetImageTypeInfo. See OpTypeImage spec for more information.
  66. struct ImageTypeInfo {
  67. uint32_t sampled_type = 0;
  68. SpvDim dim = SpvDimMax;
  69. uint32_t depth = 0;
  70. uint32_t arrayed = 0;
  71. uint32_t multisampled = 0;
  72. uint32_t sampled = 0;
  73. SpvImageFormat format = SpvImageFormatMax;
  74. SpvAccessQualifier access_qualifier = SpvAccessQualifierMax;
  75. };
  76. // Provides information on image type. |id| should be object of either
  77. // OpTypeImage or OpTypeSampledImage type. Returns false in case of failure
  78. // (not a valid id, failed to parse the instruction, etc).
  79. bool GetImageTypeInfo(const ValidationState_t& _, uint32_t id,
  80. ImageTypeInfo* info) {
  81. if (!id || !info) return false;
  82. const Instruction* inst = _.FindDef(id);
  83. assert(inst);
  84. if (inst->opcode() == SpvOpTypeSampledImage) {
  85. inst = _.FindDef(inst->word(2));
  86. assert(inst);
  87. }
  88. if (inst->opcode() != SpvOpTypeImage) return false;
  89. const size_t num_words = inst->words().size();
  90. if (num_words != 9 && num_words != 10) return false;
  91. info->sampled_type = inst->word(2);
  92. info->dim = static_cast<SpvDim>(inst->word(3));
  93. info->depth = inst->word(4);
  94. info->arrayed = inst->word(5);
  95. info->multisampled = inst->word(6);
  96. info->sampled = inst->word(7);
  97. info->format = static_cast<SpvImageFormat>(inst->word(8));
  98. info->access_qualifier = num_words < 10
  99. ? SpvAccessQualifierMax
  100. : static_cast<SpvAccessQualifier>(inst->word(9));
  101. return true;
  102. }
  103. bool IsImplicitLod(SpvOp opcode) {
  104. switch (opcode) {
  105. case SpvOpImageSampleImplicitLod:
  106. case SpvOpImageSampleDrefImplicitLod:
  107. case SpvOpImageSampleProjImplicitLod:
  108. case SpvOpImageSampleProjDrefImplicitLod:
  109. case SpvOpImageSparseSampleImplicitLod:
  110. case SpvOpImageSparseSampleDrefImplicitLod:
  111. case SpvOpImageSparseSampleProjImplicitLod:
  112. case SpvOpImageSparseSampleProjDrefImplicitLod:
  113. return true;
  114. default:
  115. break;
  116. }
  117. return false;
  118. }
  119. bool IsExplicitLod(SpvOp opcode) {
  120. switch (opcode) {
  121. case SpvOpImageSampleExplicitLod:
  122. case SpvOpImageSampleDrefExplicitLod:
  123. case SpvOpImageSampleProjExplicitLod:
  124. case SpvOpImageSampleProjDrefExplicitLod:
  125. case SpvOpImageSparseSampleExplicitLod:
  126. case SpvOpImageSparseSampleDrefExplicitLod:
  127. case SpvOpImageSparseSampleProjExplicitLod:
  128. case SpvOpImageSparseSampleProjDrefExplicitLod:
  129. return true;
  130. default:
  131. break;
  132. }
  133. return false;
  134. }
  135. bool IsValidLodOperand(const ValidationState_t& _, SpvOp opcode) {
  136. switch (opcode) {
  137. case SpvOpImageRead:
  138. case SpvOpImageWrite:
  139. case SpvOpImageSparseRead:
  140. return _.HasCapability(SpvCapabilityImageReadWriteLodAMD);
  141. default:
  142. return IsExplicitLod(opcode);
  143. }
  144. }
  145. bool IsValidGatherLodBiasAMD(const ValidationState_t& _, SpvOp opcode) {
  146. switch (opcode) {
  147. case SpvOpImageGather:
  148. case SpvOpImageSparseGather:
  149. return _.HasCapability(SpvCapabilityImageGatherBiasLodAMD);
  150. default:
  151. break;
  152. }
  153. return false;
  154. }
  155. // Returns true if the opcode is a Image instruction which applies
  156. // homogenous projection to the coordinates.
  157. bool IsProj(SpvOp opcode) {
  158. switch (opcode) {
  159. case SpvOpImageSampleProjImplicitLod:
  160. case SpvOpImageSampleProjDrefImplicitLod:
  161. case SpvOpImageSparseSampleProjImplicitLod:
  162. case SpvOpImageSparseSampleProjDrefImplicitLod:
  163. case SpvOpImageSampleProjExplicitLod:
  164. case SpvOpImageSampleProjDrefExplicitLod:
  165. case SpvOpImageSparseSampleProjExplicitLod:
  166. case SpvOpImageSparseSampleProjDrefExplicitLod:
  167. return true;
  168. default:
  169. break;
  170. }
  171. return false;
  172. }
  173. // Returns the number of components in a coordinate used to access a texel in
  174. // a single plane of an image with the given parameters.
  175. uint32_t GetPlaneCoordSize(const ImageTypeInfo& info) {
  176. uint32_t plane_size = 0;
  177. // If this switch breaks your build, please add new values below.
  178. switch (info.dim) {
  179. case SpvDim1D:
  180. case SpvDimBuffer:
  181. plane_size = 1;
  182. break;
  183. case SpvDim2D:
  184. case SpvDimRect:
  185. case SpvDimSubpassData:
  186. plane_size = 2;
  187. break;
  188. case SpvDim3D:
  189. case SpvDimCube:
  190. // For Cube direction vector is used instead of UV.
  191. plane_size = 3;
  192. break;
  193. case SpvDimMax:
  194. assert(0);
  195. break;
  196. }
  197. return plane_size;
  198. }
  199. // Returns minimal number of coordinates based on image dim, arrayed and whether
  200. // the instruction uses projection coordinates.
  201. uint32_t GetMinCoordSize(SpvOp opcode, const ImageTypeInfo& info) {
  202. if (info.dim == SpvDimCube &&
  203. (opcode == SpvOpImageRead || opcode == SpvOpImageWrite ||
  204. opcode == SpvOpImageSparseRead)) {
  205. // These opcodes use UV for Cube, not direction vector.
  206. return 3;
  207. }
  208. return GetPlaneCoordSize(info) + info.arrayed + (IsProj(opcode) ? 1 : 0);
  209. }
  210. // Checks ImageOperand bitfield and respective operands.
  211. // word_index is the index of the first word after the image-operand mask word.
  212. spv_result_t ValidateImageOperands(ValidationState_t& _,
  213. const Instruction* inst,
  214. const ImageTypeInfo& info,
  215. uint32_t word_index) {
  216. static const bool kAllImageOperandsHandled = CheckAllImageOperandsHandled();
  217. (void)kAllImageOperandsHandled;
  218. const SpvOp opcode = inst->opcode();
  219. const size_t num_words = inst->words().size();
  220. const bool have_explicit_mask = (word_index - 1 < num_words);
  221. const uint32_t mask = have_explicit_mask ? inst->word(word_index - 1) : 0u;
  222. if (have_explicit_mask) {
  223. // NonPrivate, Volatile, SignExtend, ZeroExtend take no operand words.
  224. const uint32_t mask_bits_having_operands =
  225. mask & ~uint32_t(SpvImageOperandsNonPrivateTexelKHRMask |
  226. SpvImageOperandsVolatileTexelKHRMask |
  227. SpvImageOperandsSignExtendMask |
  228. SpvImageOperandsZeroExtendMask);
  229. size_t expected_num_image_operand_words =
  230. spvtools::utils::CountSetBits(mask_bits_having_operands);
  231. if (mask & SpvImageOperandsGradMask) {
  232. // Grad uses two words.
  233. ++expected_num_image_operand_words;
  234. }
  235. if (expected_num_image_operand_words != num_words - word_index) {
  236. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  237. << "Number of image operand ids doesn't correspond to the bit "
  238. "mask";
  239. }
  240. } else if (num_words != word_index - 1) {
  241. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  242. << "Number of image operand ids doesn't correspond to the bit mask";
  243. }
  244. if (info.multisampled & (0 == (mask & SpvImageOperandsSampleMask))) {
  245. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  246. << "Image Operand Sample is required for operation on "
  247. "multi-sampled image";
  248. }
  249. // After this point, only set bits in the image operands mask can cause
  250. // the module to be invalid.
  251. if (mask == 0) return SPV_SUCCESS;
  252. if (spvtools::utils::CountSetBits(
  253. mask & (SpvImageOperandsOffsetMask | SpvImageOperandsConstOffsetMask |
  254. SpvImageOperandsConstOffsetsMask)) > 1) {
  255. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  256. << _.VkErrorID(4662)
  257. << "Image Operands Offset, ConstOffset, ConstOffsets cannot be used "
  258. << "together";
  259. }
  260. const bool is_implicit_lod = IsImplicitLod(opcode);
  261. const bool is_explicit_lod = IsExplicitLod(opcode);
  262. const bool is_valid_lod_operand = IsValidLodOperand(_, opcode);
  263. const bool is_valid_gather_lod_bias_amd = IsValidGatherLodBiasAMD(_, opcode);
  264. // The checks should be done in the order of definition of OperandImage.
  265. if (mask & SpvImageOperandsBiasMask) {
  266. if (!is_implicit_lod && !is_valid_gather_lod_bias_amd) {
  267. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  268. << "Image Operand Bias can only be used with ImplicitLod opcodes";
  269. }
  270. const uint32_t type_id = _.GetTypeId(inst->word(word_index++));
  271. if (!_.IsFloatScalarType(type_id)) {
  272. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  273. << "Expected Image Operand Bias to be float scalar";
  274. }
  275. if (info.dim != SpvDim1D && info.dim != SpvDim2D && info.dim != SpvDim3D &&
  276. info.dim != SpvDimCube) {
  277. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  278. << "Image Operand Bias requires 'Dim' parameter to be 1D, 2D, 3D "
  279. "or Cube";
  280. }
  281. // Multisampled is already checked.
  282. }
  283. if (mask & SpvImageOperandsLodMask) {
  284. if (!is_valid_lod_operand && opcode != SpvOpImageFetch &&
  285. opcode != SpvOpImageSparseFetch && !is_valid_gather_lod_bias_amd) {
  286. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  287. << "Image Operand Lod can only be used with ExplicitLod opcodes "
  288. << "and OpImageFetch";
  289. }
  290. if (mask & SpvImageOperandsGradMask) {
  291. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  292. << "Image Operand bits Lod and Grad cannot be set at the same "
  293. "time";
  294. }
  295. const uint32_t type_id = _.GetTypeId(inst->word(word_index++));
  296. if (is_explicit_lod || is_valid_gather_lod_bias_amd) {
  297. if (!_.IsFloatScalarType(type_id)) {
  298. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  299. << "Expected Image Operand Lod to be float scalar when used "
  300. << "with ExplicitLod";
  301. }
  302. } else {
  303. if (!_.IsIntScalarType(type_id)) {
  304. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  305. << "Expected Image Operand Lod to be int scalar when used with "
  306. << "OpImageFetch";
  307. }
  308. }
  309. if (info.dim != SpvDim1D && info.dim != SpvDim2D && info.dim != SpvDim3D &&
  310. info.dim != SpvDimCube) {
  311. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  312. << "Image Operand Lod requires 'Dim' parameter to be 1D, 2D, 3D "
  313. "or Cube";
  314. }
  315. // Multisampled is already checked.
  316. }
  317. if (mask & SpvImageOperandsGradMask) {
  318. if (!is_explicit_lod) {
  319. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  320. << "Image Operand Grad can only be used with ExplicitLod opcodes";
  321. }
  322. const uint32_t dx_type_id = _.GetTypeId(inst->word(word_index++));
  323. const uint32_t dy_type_id = _.GetTypeId(inst->word(word_index++));
  324. if (!_.IsFloatScalarOrVectorType(dx_type_id) ||
  325. !_.IsFloatScalarOrVectorType(dy_type_id)) {
  326. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  327. << "Expected both Image Operand Grad ids to be float scalars or "
  328. << "vectors";
  329. }
  330. const uint32_t plane_size = GetPlaneCoordSize(info);
  331. const uint32_t dx_size = _.GetDimension(dx_type_id);
  332. const uint32_t dy_size = _.GetDimension(dy_type_id);
  333. if (plane_size != dx_size) {
  334. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  335. << "Expected Image Operand Grad dx to have " << plane_size
  336. << " components, but given " << dx_size;
  337. }
  338. if (plane_size != dy_size) {
  339. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  340. << "Expected Image Operand Grad dy to have " << plane_size
  341. << " components, but given " << dy_size;
  342. }
  343. // Multisampled is already checked.
  344. }
  345. if (mask & SpvImageOperandsConstOffsetMask) {
  346. if (info.dim == SpvDimCube) {
  347. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  348. << "Image Operand ConstOffset cannot be used with Cube Image "
  349. "'Dim'";
  350. }
  351. const uint32_t id = inst->word(word_index++);
  352. const uint32_t type_id = _.GetTypeId(id);
  353. if (!_.IsIntScalarOrVectorType(type_id)) {
  354. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  355. << "Expected Image Operand ConstOffset to be int scalar or "
  356. << "vector";
  357. }
  358. if (!spvOpcodeIsConstant(_.GetIdOpcode(id))) {
  359. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  360. << "Expected Image Operand ConstOffset to be a const object";
  361. }
  362. const uint32_t plane_size = GetPlaneCoordSize(info);
  363. const uint32_t offset_size = _.GetDimension(type_id);
  364. if (plane_size != offset_size) {
  365. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  366. << "Expected Image Operand ConstOffset to have " << plane_size
  367. << " components, but given " << offset_size;
  368. }
  369. }
  370. if (mask & SpvImageOperandsOffsetMask) {
  371. if (info.dim == SpvDimCube) {
  372. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  373. << "Image Operand Offset cannot be used with Cube Image 'Dim'";
  374. }
  375. const uint32_t id = inst->word(word_index++);
  376. const uint32_t type_id = _.GetTypeId(id);
  377. if (!_.IsIntScalarOrVectorType(type_id)) {
  378. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  379. << "Expected Image Operand Offset to be int scalar or "
  380. << "vector";
  381. }
  382. const uint32_t plane_size = GetPlaneCoordSize(info);
  383. const uint32_t offset_size = _.GetDimension(type_id);
  384. if (plane_size != offset_size) {
  385. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  386. << "Expected Image Operand Offset to have " << plane_size
  387. << " components, but given " << offset_size;
  388. }
  389. if (!_.options()->before_hlsl_legalization &&
  390. spvIsVulkanEnv(_.context()->target_env)) {
  391. if (opcode != SpvOpImageGather && opcode != SpvOpImageDrefGather &&
  392. opcode != SpvOpImageSparseGather &&
  393. opcode != SpvOpImageSparseDrefGather) {
  394. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  395. << _.VkErrorID(4663)
  396. << "Image Operand Offset can only be used with "
  397. "OpImage*Gather operations";
  398. }
  399. }
  400. }
  401. if (mask & SpvImageOperandsConstOffsetsMask) {
  402. if (opcode != SpvOpImageGather && opcode != SpvOpImageDrefGather &&
  403. opcode != SpvOpImageSparseGather &&
  404. opcode != SpvOpImageSparseDrefGather) {
  405. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  406. << "Image Operand ConstOffsets can only be used with "
  407. "OpImageGather and OpImageDrefGather";
  408. }
  409. if (info.dim == SpvDimCube) {
  410. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  411. << "Image Operand ConstOffsets cannot be used with Cube Image "
  412. "'Dim'";
  413. }
  414. const uint32_t id = inst->word(word_index++);
  415. const uint32_t type_id = _.GetTypeId(id);
  416. const Instruction* type_inst = _.FindDef(type_id);
  417. assert(type_inst);
  418. if (type_inst->opcode() != SpvOpTypeArray) {
  419. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  420. << "Expected Image Operand ConstOffsets to be an array of size 4";
  421. }
  422. uint64_t array_size = 0;
  423. if (!_.GetConstantValUint64(type_inst->word(3), &array_size)) {
  424. assert(0 && "Array type definition is corrupt");
  425. }
  426. if (array_size != 4) {
  427. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  428. << "Expected Image Operand ConstOffsets to be an array of size 4";
  429. }
  430. const uint32_t component_type = type_inst->word(2);
  431. if (!_.IsIntVectorType(component_type) ||
  432. _.GetDimension(component_type) != 2) {
  433. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  434. << "Expected Image Operand ConstOffsets array componenets to be "
  435. "int vectors of size 2";
  436. }
  437. if (!spvOpcodeIsConstant(_.GetIdOpcode(id))) {
  438. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  439. << "Expected Image Operand ConstOffsets to be a const object";
  440. }
  441. }
  442. if (mask & SpvImageOperandsSampleMask) {
  443. if (opcode != SpvOpImageFetch && opcode != SpvOpImageRead &&
  444. opcode != SpvOpImageWrite && opcode != SpvOpImageSparseFetch &&
  445. opcode != SpvOpImageSparseRead) {
  446. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  447. << "Image Operand Sample can only be used with OpImageFetch, "
  448. << "OpImageRead, OpImageWrite, OpImageSparseFetch and "
  449. << "OpImageSparseRead";
  450. }
  451. if (info.multisampled == 0) {
  452. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  453. << "Image Operand Sample requires non-zero 'MS' parameter";
  454. }
  455. const uint32_t type_id = _.GetTypeId(inst->word(word_index++));
  456. if (!_.IsIntScalarType(type_id)) {
  457. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  458. << "Expected Image Operand Sample to be int scalar";
  459. }
  460. }
  461. if (mask & SpvImageOperandsMinLodMask) {
  462. if (!is_implicit_lod && !(mask & SpvImageOperandsGradMask)) {
  463. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  464. << "Image Operand MinLod can only be used with ImplicitLod "
  465. << "opcodes or together with Image Operand Grad";
  466. }
  467. const uint32_t type_id = _.GetTypeId(inst->word(word_index++));
  468. if (!_.IsFloatScalarType(type_id)) {
  469. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  470. << "Expected Image Operand MinLod to be float scalar";
  471. }
  472. if (info.dim != SpvDim1D && info.dim != SpvDim2D && info.dim != SpvDim3D &&
  473. info.dim != SpvDimCube) {
  474. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  475. << "Image Operand MinLod requires 'Dim' parameter to be 1D, 2D, "
  476. "3D or Cube";
  477. }
  478. if (info.multisampled != 0) {
  479. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  480. << "Image Operand MinLod requires 'MS' parameter to be 0";
  481. }
  482. }
  483. if (mask & SpvImageOperandsMakeTexelAvailableKHRMask) {
  484. // Checked elsewhere: capability and memory model are correct.
  485. if (opcode != SpvOpImageWrite) {
  486. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  487. << "Image Operand MakeTexelAvailableKHR can only be used with Op"
  488. << spvOpcodeString(SpvOpImageWrite) << ": Op"
  489. << spvOpcodeString(opcode);
  490. }
  491. if (!(mask & SpvImageOperandsNonPrivateTexelKHRMask)) {
  492. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  493. << "Image Operand MakeTexelAvailableKHR requires "
  494. "NonPrivateTexelKHR is also specified: Op"
  495. << spvOpcodeString(opcode);
  496. }
  497. const auto available_scope = inst->word(word_index++);
  498. if (auto error = ValidateMemoryScope(_, inst, available_scope))
  499. return error;
  500. }
  501. if (mask & SpvImageOperandsMakeTexelVisibleKHRMask) {
  502. // Checked elsewhere: capability and memory model are correct.
  503. if (opcode != SpvOpImageRead && opcode != SpvOpImageSparseRead) {
  504. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  505. << "Image Operand MakeTexelVisibleKHR can only be used with Op"
  506. << spvOpcodeString(SpvOpImageRead) << " or Op"
  507. << spvOpcodeString(SpvOpImageSparseRead) << ": Op"
  508. << spvOpcodeString(opcode);
  509. }
  510. if (!(mask & SpvImageOperandsNonPrivateTexelKHRMask)) {
  511. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  512. << "Image Operand MakeTexelVisibleKHR requires NonPrivateTexelKHR "
  513. "is also specified: Op"
  514. << spvOpcodeString(opcode);
  515. }
  516. const auto visible_scope = inst->word(word_index++);
  517. if (auto error = ValidateMemoryScope(_, inst, visible_scope)) return error;
  518. }
  519. if (mask & SpvImageOperandsSignExtendMask) {
  520. // Checked elsewhere: SPIR-V 1.4 version or later.
  521. // "The texel value is converted to the target value via sign extension.
  522. // Only valid when the texel type is a scalar or vector of integer type."
  523. //
  524. // We don't have enough information to know what the texel type is.
  525. // In OpenCL, knowledge is deferred until runtime: the image SampledType is
  526. // void, and the Format is Unknown.
  527. // In Vulkan, the texel type is only known in all cases by the pipeline
  528. // setup.
  529. }
  530. if (mask & SpvImageOperandsZeroExtendMask) {
  531. // Checked elsewhere: SPIR-V 1.4 version or later.
  532. // "The texel value is converted to the target value via zero extension.
  533. // Only valid when the texel type is a scalar or vector of integer type."
  534. //
  535. // We don't have enough information to know what the texel type is.
  536. // In OpenCL, knowledge is deferred until runtime: the image SampledType is
  537. // void, and the Format is Unknown.
  538. // In Vulkan, the texel type is only known in all cases by the pipeline
  539. // setup.
  540. }
  541. return SPV_SUCCESS;
  542. }
  543. // Checks some of the validation rules which are common to multiple opcodes.
  544. spv_result_t ValidateImageCommon(ValidationState_t& _, const Instruction* inst,
  545. const ImageTypeInfo& info) {
  546. const SpvOp opcode = inst->opcode();
  547. if (IsProj(opcode)) {
  548. if (info.dim != SpvDim1D && info.dim != SpvDim2D && info.dim != SpvDim3D &&
  549. info.dim != SpvDimRect) {
  550. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  551. << "Expected Image 'Dim' parameter to be 1D, 2D, 3D or Rect";
  552. }
  553. if (info.multisampled != 0) {
  554. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  555. << "Expected Image 'MS' parameter to be 0";
  556. }
  557. if (info.arrayed != 0) {
  558. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  559. << "Expected Image 'arrayed' parameter to be 0";
  560. }
  561. }
  562. if (opcode == SpvOpImageRead || opcode == SpvOpImageSparseRead ||
  563. opcode == SpvOpImageWrite) {
  564. if (info.sampled == 0) {
  565. } else if (info.sampled == 2) {
  566. if (info.dim == SpvDim1D && !_.HasCapability(SpvCapabilityImage1D)) {
  567. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  568. << "Capability Image1D is required to access storage image";
  569. } else if (info.dim == SpvDimRect &&
  570. !_.HasCapability(SpvCapabilityImageRect)) {
  571. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  572. << "Capability ImageRect is required to access storage image";
  573. } else if (info.dim == SpvDimBuffer &&
  574. !_.HasCapability(SpvCapabilityImageBuffer)) {
  575. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  576. << "Capability ImageBuffer is required to access storage image";
  577. } else if (info.dim == SpvDimCube && info.arrayed == 1 &&
  578. !_.HasCapability(SpvCapabilityImageCubeArray)) {
  579. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  580. << "Capability ImageCubeArray is required to access "
  581. << "storage image";
  582. }
  583. if (info.multisampled == 1 &&
  584. !_.HasCapability(SpvCapabilityImageMSArray)) {
  585. #if 0
  586. // TODO([email protected]) The description of this rule in the spec
  587. // is unclear and Glslang doesn't declare ImageMSArray. Need to clarify
  588. // and reenable.
  589. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  590. << "Capability ImageMSArray is required to access storage "
  591. << "image";
  592. #endif
  593. }
  594. } else {
  595. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  596. << "Expected Image 'Sampled' parameter to be 0 or 2";
  597. }
  598. }
  599. return SPV_SUCCESS;
  600. }
  601. // Returns true if opcode is *ImageSparse*, false otherwise.
  602. bool IsSparse(SpvOp opcode) {
  603. switch (opcode) {
  604. case SpvOpImageSparseSampleImplicitLod:
  605. case SpvOpImageSparseSampleExplicitLod:
  606. case SpvOpImageSparseSampleDrefImplicitLod:
  607. case SpvOpImageSparseSampleDrefExplicitLod:
  608. case SpvOpImageSparseSampleProjImplicitLod:
  609. case SpvOpImageSparseSampleProjExplicitLod:
  610. case SpvOpImageSparseSampleProjDrefImplicitLod:
  611. case SpvOpImageSparseSampleProjDrefExplicitLod:
  612. case SpvOpImageSparseFetch:
  613. case SpvOpImageSparseGather:
  614. case SpvOpImageSparseDrefGather:
  615. case SpvOpImageSparseTexelsResident:
  616. case SpvOpImageSparseRead: {
  617. return true;
  618. }
  619. default: { return false; }
  620. }
  621. return false;
  622. }
  623. // Checks sparse image opcode result type and returns the second struct member.
  624. // Returns inst.type_id for non-sparse image opcodes.
  625. // Not valid for sparse image opcodes which do not return a struct.
  626. spv_result_t GetActualResultType(ValidationState_t& _, const Instruction* inst,
  627. uint32_t* actual_result_type) {
  628. const SpvOp opcode = inst->opcode();
  629. if (IsSparse(opcode)) {
  630. const Instruction* const type_inst = _.FindDef(inst->type_id());
  631. assert(type_inst);
  632. if (!type_inst || type_inst->opcode() != SpvOpTypeStruct) {
  633. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  634. << "Expected Result Type to be OpTypeStruct";
  635. }
  636. if (type_inst->words().size() != 4 ||
  637. !_.IsIntScalarType(type_inst->word(2))) {
  638. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  639. << "Expected Result Type to be a struct containing an int "
  640. "scalar and a texel";
  641. }
  642. *actual_result_type = type_inst->word(3);
  643. } else {
  644. *actual_result_type = inst->type_id();
  645. }
  646. return SPV_SUCCESS;
  647. }
  648. // Returns a string describing actual result type of an opcode.
  649. // Not valid for sparse image opcodes which do not return a struct.
  650. const char* GetActualResultTypeStr(SpvOp opcode) {
  651. if (IsSparse(opcode)) return "Result Type's second member";
  652. return "Result Type";
  653. }
  654. spv_result_t ValidateTypeImage(ValidationState_t& _, const Instruction* inst) {
  655. assert(inst->type_id() == 0);
  656. ImageTypeInfo info;
  657. if (!GetImageTypeInfo(_, inst->word(1), &info)) {
  658. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  659. << "Corrupt image type definition";
  660. }
  661. if (_.IsIntScalarType(info.sampled_type) &&
  662. (64 == _.GetBitWidth(info.sampled_type)) &&
  663. !_.HasCapability(SpvCapabilityInt64ImageEXT)) {
  664. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  665. << "Capability Int64ImageEXT is required when using Sampled Type of "
  666. "64-bit int";
  667. }
  668. const auto target_env = _.context()->target_env;
  669. if (spvIsVulkanEnv(target_env)) {
  670. if ((!_.IsFloatScalarType(info.sampled_type) &&
  671. !_.IsIntScalarType(info.sampled_type)) ||
  672. ((32 != _.GetBitWidth(info.sampled_type)) &&
  673. (64 != _.GetBitWidth(info.sampled_type))) ||
  674. ((64 == _.GetBitWidth(info.sampled_type)) &&
  675. _.IsFloatScalarType(info.sampled_type))) {
  676. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  677. << _.VkErrorID(4656)
  678. << "Expected Sampled Type to be a 32-bit int, 64-bit int or "
  679. "32-bit float scalar type for Vulkan environment";
  680. }
  681. } else if (spvIsOpenCLEnv(target_env)) {
  682. if (!_.IsVoidType(info.sampled_type)) {
  683. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  684. << "Sampled Type must be OpTypeVoid in the OpenCL environment.";
  685. }
  686. } else {
  687. const SpvOp sampled_type_opcode = _.GetIdOpcode(info.sampled_type);
  688. if (sampled_type_opcode != SpvOpTypeVoid &&
  689. sampled_type_opcode != SpvOpTypeInt &&
  690. sampled_type_opcode != SpvOpTypeFloat) {
  691. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  692. << "Expected Sampled Type to be either void or"
  693. << " numerical scalar type";
  694. }
  695. }
  696. // Dim is checked elsewhere.
  697. if (info.depth > 2) {
  698. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  699. << "Invalid Depth " << info.depth << " (must be 0, 1 or 2)";
  700. }
  701. if (info.arrayed > 1) {
  702. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  703. << "Invalid Arrayed " << info.arrayed << " (must be 0 or 1)";
  704. }
  705. if (spvIsOpenCLEnv(target_env)) {
  706. if ((info.arrayed == 1) && (info.dim != SpvDim1D) &&
  707. (info.dim != SpvDim2D)) {
  708. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  709. << "In the OpenCL environment, Arrayed may only be set to 1 "
  710. << "when Dim is either 1D or 2D.";
  711. }
  712. }
  713. if (info.multisampled > 1) {
  714. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  715. << "Invalid MS " << info.multisampled << " (must be 0 or 1)";
  716. }
  717. if (spvIsOpenCLEnv(target_env)) {
  718. if (info.multisampled != 0) {
  719. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  720. << "MS must be 0 in the OpenCL environment.";
  721. }
  722. }
  723. if (info.sampled > 2) {
  724. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  725. << "Invalid Sampled " << info.sampled << " (must be 0, 1 or 2)";
  726. }
  727. if (spvIsVulkanEnv(target_env)) {
  728. if (info.sampled == 0) {
  729. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  730. << _.VkErrorID(4657)
  731. << "Sampled must be 1 or 2 in the Vulkan environment.";
  732. }
  733. }
  734. if (spvIsOpenCLEnv(_.context()->target_env)) {
  735. if (info.sampled != 0) {
  736. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  737. << "Sampled must be 0 in the OpenCL environment.";
  738. }
  739. }
  740. if (info.dim == SpvDimSubpassData) {
  741. if (info.sampled != 2) {
  742. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  743. << "Dim SubpassData requires Sampled to be 2";
  744. }
  745. if (info.format != SpvImageFormatUnknown) {
  746. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  747. << "Dim SubpassData requires format Unknown";
  748. }
  749. }
  750. // Format and Access Qualifier are also checked elsewhere.
  751. if (spvIsOpenCLEnv(_.context()->target_env)) {
  752. if (info.access_qualifier == SpvAccessQualifierMax) {
  753. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  754. << "In the OpenCL environment, the optional Access Qualifier"
  755. << " must be present.";
  756. }
  757. }
  758. if (info.multisampled && (info.sampled == 2) &&
  759. (info.dim != SpvDimSubpassData)) {
  760. if (!_.HasCapability(SpvCapabilityStorageImageMultisample)) {
  761. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  762. << "Capability StorageImageMultisample is required when using "
  763. "multisampled storage image";
  764. }
  765. }
  766. return SPV_SUCCESS;
  767. }
  768. spv_result_t ValidateTypeSampledImage(ValidationState_t& _,
  769. const Instruction* inst) {
  770. const uint32_t image_type = inst->word(2);
  771. if (_.GetIdOpcode(image_type) != SpvOpTypeImage) {
  772. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  773. << "Expected Image to be of type OpTypeImage";
  774. }
  775. ImageTypeInfo info;
  776. if (!GetImageTypeInfo(_, image_type, &info)) {
  777. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  778. << "Corrupt image type definition";
  779. }
  780. // OpenCL requires Sampled=0, checked elsewhere.
  781. // Vulkan uses the Sampled=1 case.
  782. if ((info.sampled != 0) && (info.sampled != 1)) {
  783. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  784. << _.VkErrorID(4657)
  785. << "Sampled image type requires an image type with \"Sampled\" "
  786. "operand set to 0 or 1";
  787. }
  788. return SPV_SUCCESS;
  789. }
  790. bool IsAllowedSampledImageOperand(SpvOp opcode) {
  791. switch (opcode) {
  792. case SpvOpSampledImage:
  793. case SpvOpImageSampleImplicitLod:
  794. case SpvOpImageSampleExplicitLod:
  795. case SpvOpImageSampleDrefImplicitLod:
  796. case SpvOpImageSampleDrefExplicitLod:
  797. case SpvOpImageSampleProjImplicitLod:
  798. case SpvOpImageSampleProjExplicitLod:
  799. case SpvOpImageSampleProjDrefImplicitLod:
  800. case SpvOpImageSampleProjDrefExplicitLod:
  801. case SpvOpImageGather:
  802. case SpvOpImageDrefGather:
  803. case SpvOpImage:
  804. case SpvOpImageQueryLod:
  805. case SpvOpImageSparseSampleImplicitLod:
  806. case SpvOpImageSparseSampleExplicitLod:
  807. case SpvOpImageSparseSampleDrefImplicitLod:
  808. case SpvOpImageSparseSampleDrefExplicitLod:
  809. case SpvOpImageSparseGather:
  810. case SpvOpImageSparseDrefGather:
  811. case SpvOpCopyObject:
  812. return true;
  813. default:
  814. return false;
  815. }
  816. }
  817. spv_result_t ValidateSampledImage(ValidationState_t& _,
  818. const Instruction* inst) {
  819. if (_.GetIdOpcode(inst->type_id()) != SpvOpTypeSampledImage) {
  820. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  821. << "Expected Result Type to be OpTypeSampledImage.";
  822. }
  823. const uint32_t image_type = _.GetOperandTypeId(inst, 2);
  824. if (_.GetIdOpcode(image_type) != SpvOpTypeImage) {
  825. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  826. << "Expected Image to be of type OpTypeImage.";
  827. }
  828. ImageTypeInfo info;
  829. if (!GetImageTypeInfo(_, image_type, &info)) {
  830. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  831. << "Corrupt image type definition";
  832. }
  833. // TODO([email protected]) Check compatibility of result type and received
  834. // image.
  835. if (spvIsVulkanEnv(_.context()->target_env)) {
  836. if (info.sampled != 1) {
  837. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  838. << "Expected Image 'Sampled' parameter to be 1 "
  839. << "for Vulkan environment.";
  840. }
  841. } else {
  842. if (info.sampled != 0 && info.sampled != 1) {
  843. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  844. << "Expected Image 'Sampled' parameter to be 0 or 1";
  845. }
  846. }
  847. if (info.dim == SpvDimSubpassData) {
  848. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  849. << "Expected Image 'Dim' parameter to be not SubpassData.";
  850. }
  851. if (_.GetIdOpcode(_.GetOperandTypeId(inst, 3)) != SpvOpTypeSampler) {
  852. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  853. << "Expected Sampler to be of type OpTypeSampler";
  854. }
  855. // We need to validate 2 things:
  856. // * All OpSampledImage instructions must be in the same block in which their
  857. // Result <id> are consumed.
  858. // * Result <id> from OpSampledImage instructions must not appear as operands
  859. // to OpPhi instructions or OpSelect instructions, or any instructions other
  860. // than the image lookup and query instructions specified to take an operand
  861. // whose type is OpTypeSampledImage.
  862. std::vector<Instruction*> consumers = _.getSampledImageConsumers(inst->id());
  863. if (!consumers.empty()) {
  864. for (auto consumer_instr : consumers) {
  865. const auto consumer_opcode = consumer_instr->opcode();
  866. if (consumer_instr->block() != inst->block()) {
  867. return _.diag(SPV_ERROR_INVALID_ID, inst)
  868. << "All OpSampledImage instructions must be in the same block "
  869. "in "
  870. "which their Result <id> are consumed. OpSampledImage Result "
  871. "Type <id> '"
  872. << _.getIdName(inst->id())
  873. << "' has a consumer in a different basic "
  874. "block. The consumer instruction <id> is '"
  875. << _.getIdName(consumer_instr->id()) << "'.";
  876. }
  877. if (consumer_opcode == SpvOpPhi || consumer_opcode == SpvOpSelect) {
  878. return _.diag(SPV_ERROR_INVALID_ID, inst)
  879. << "Result <id> from OpSampledImage instruction must not appear "
  880. "as "
  881. "operands of Op"
  882. << spvOpcodeString(static_cast<SpvOp>(consumer_opcode)) << "."
  883. << " Found result <id> '" << _.getIdName(inst->id())
  884. << "' as an operand of <id> '"
  885. << _.getIdName(consumer_instr->id()) << "'.";
  886. }
  887. if (!IsAllowedSampledImageOperand(consumer_opcode)) {
  888. return _.diag(SPV_ERROR_INVALID_ID, inst)
  889. << "Result <id> from OpSampledImage instruction must not appear "
  890. "as operand for Op"
  891. << spvOpcodeString(static_cast<SpvOp>(consumer_opcode))
  892. << ", since it is not specificed as taking an "
  893. << "OpTypeSampledImage."
  894. << " Found result <id> '" << _.getIdName(inst->id())
  895. << "' as an operand of <id> '"
  896. << _.getIdName(consumer_instr->id()) << "'.";
  897. }
  898. }
  899. }
  900. return SPV_SUCCESS;
  901. }
  902. spv_result_t ValidateImageTexelPointer(ValidationState_t& _,
  903. const Instruction* inst) {
  904. const auto result_type = _.FindDef(inst->type_id());
  905. if (result_type->opcode() != SpvOpTypePointer) {
  906. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  907. << "Expected Result Type to be OpTypePointer";
  908. }
  909. const auto storage_class = result_type->GetOperandAs<uint32_t>(1);
  910. if (storage_class != SpvStorageClassImage) {
  911. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  912. << "Expected Result Type to be OpTypePointer whose Storage Class "
  913. "operand is Image";
  914. }
  915. const auto ptr_type = result_type->GetOperandAs<uint32_t>(2);
  916. const auto ptr_opcode = _.GetIdOpcode(ptr_type);
  917. if (ptr_opcode != SpvOpTypeInt && ptr_opcode != SpvOpTypeFloat &&
  918. ptr_opcode != SpvOpTypeVoid) {
  919. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  920. << "Expected Result Type to be OpTypePointer whose Type operand "
  921. "must be a scalar numerical type or OpTypeVoid";
  922. }
  923. const auto image_ptr = _.FindDef(_.GetOperandTypeId(inst, 2));
  924. if (!image_ptr || image_ptr->opcode() != SpvOpTypePointer) {
  925. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  926. << "Expected Image to be OpTypePointer";
  927. }
  928. const auto image_type = image_ptr->GetOperandAs<uint32_t>(2);
  929. if (_.GetIdOpcode(image_type) != SpvOpTypeImage) {
  930. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  931. << "Expected Image to be OpTypePointer with Type OpTypeImage";
  932. }
  933. ImageTypeInfo info;
  934. if (!GetImageTypeInfo(_, image_type, &info)) {
  935. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  936. << "Corrupt image type definition";
  937. }
  938. if (info.sampled_type != ptr_type) {
  939. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  940. << "Expected Image 'Sampled Type' to be the same as the Type "
  941. "pointed to by Result Type";
  942. }
  943. if (info.dim == SpvDimSubpassData) {
  944. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  945. << "Image Dim SubpassData cannot be used with OpImageTexelPointer";
  946. }
  947. const uint32_t coord_type = _.GetOperandTypeId(inst, 3);
  948. if (!coord_type || !_.IsIntScalarOrVectorType(coord_type)) {
  949. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  950. << "Expected Coordinate to be integer scalar or vector";
  951. }
  952. uint32_t expected_coord_size = 0;
  953. if (info.arrayed == 0) {
  954. expected_coord_size = GetPlaneCoordSize(info);
  955. } else if (info.arrayed == 1) {
  956. switch (info.dim) {
  957. case SpvDim1D:
  958. expected_coord_size = 2;
  959. break;
  960. case SpvDimCube:
  961. case SpvDim2D:
  962. expected_coord_size = 3;
  963. break;
  964. default:
  965. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  966. << "Expected Image 'Dim' must be one of 1D, 2D, or Cube when "
  967. "Arrayed is 1";
  968. break;
  969. }
  970. }
  971. const uint32_t actual_coord_size = _.GetDimension(coord_type);
  972. if (expected_coord_size != actual_coord_size) {
  973. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  974. << "Expected Coordinate to have " << expected_coord_size
  975. << " components, but given " << actual_coord_size;
  976. }
  977. const uint32_t sample_type = _.GetOperandTypeId(inst, 4);
  978. if (!sample_type || !_.IsIntScalarType(sample_type)) {
  979. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  980. << "Expected Sample to be integer scalar";
  981. }
  982. if (info.multisampled == 0) {
  983. uint64_t ms = 0;
  984. if (!_.GetConstantValUint64(inst->GetOperandAs<uint32_t>(4), &ms) ||
  985. ms != 0) {
  986. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  987. << "Expected Sample for Image with MS 0 to be a valid <id> for "
  988. "the value 0";
  989. }
  990. }
  991. if (spvIsVulkanEnv(_.context()->target_env)) {
  992. if ((info.format != SpvImageFormatR64i) &&
  993. (info.format != SpvImageFormatR64ui) &&
  994. (info.format != SpvImageFormatR32f) &&
  995. (info.format != SpvImageFormatR32i) &&
  996. (info.format != SpvImageFormatR32ui)) {
  997. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  998. << _.VkErrorID(4658)
  999. << "Expected the Image Format in Image to be R64i, R64ui, R32f, "
  1000. "R32i, or R32ui for Vulkan environment";
  1001. }
  1002. }
  1003. return SPV_SUCCESS;
  1004. }
  1005. spv_result_t ValidateImageLod(ValidationState_t& _, const Instruction* inst) {
  1006. const SpvOp opcode = inst->opcode();
  1007. uint32_t actual_result_type = 0;
  1008. if (spv_result_t error = GetActualResultType(_, inst, &actual_result_type)) {
  1009. return error;
  1010. }
  1011. if (!_.IsIntVectorType(actual_result_type) &&
  1012. !_.IsFloatVectorType(actual_result_type)) {
  1013. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  1014. << "Expected " << GetActualResultTypeStr(opcode)
  1015. << " to be int or float vector type";
  1016. }
  1017. if (_.GetDimension(actual_result_type) != 4) {
  1018. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  1019. << "Expected " << GetActualResultTypeStr(opcode)
  1020. << " to have 4 components";
  1021. }
  1022. const uint32_t image_type = _.GetOperandTypeId(inst, 2);
  1023. if (_.GetIdOpcode(image_type) != SpvOpTypeSampledImage) {
  1024. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  1025. << "Expected Sampled Image to be of type OpTypeSampledImage";
  1026. }
  1027. ImageTypeInfo info;
  1028. if (!GetImageTypeInfo(_, image_type, &info)) {
  1029. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  1030. << "Corrupt image type definition";
  1031. }
  1032. if (spv_result_t result = ValidateImageCommon(_, inst, info)) return result;
  1033. if (info.multisampled) {
  1034. // When using image operands, the Sample image operand is required if and
  1035. // only if the image is multisampled (MS=1). The Sample image operand is
  1036. // only allowed for fetch, read, and write.
  1037. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  1038. << "Sampling operation is invalid for multisample image";
  1039. }
  1040. if (_.GetIdOpcode(info.sampled_type) != SpvOpTypeVoid) {
  1041. const uint32_t texel_component_type =
  1042. _.GetComponentType(actual_result_type);
  1043. if (texel_component_type != info.sampled_type) {
  1044. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  1045. << "Expected Image 'Sampled Type' to be the same as "
  1046. << GetActualResultTypeStr(opcode) << " components";
  1047. }
  1048. }
  1049. const uint32_t coord_type = _.GetOperandTypeId(inst, 3);
  1050. if ((opcode == SpvOpImageSampleExplicitLod ||
  1051. opcode == SpvOpImageSparseSampleExplicitLod) &&
  1052. _.HasCapability(SpvCapabilityKernel)) {
  1053. if (!_.IsFloatScalarOrVectorType(coord_type) &&
  1054. !_.IsIntScalarOrVectorType(coord_type)) {
  1055. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  1056. << "Expected Coordinate to be int or float scalar or vector";
  1057. }
  1058. } else {
  1059. if (!_.IsFloatScalarOrVectorType(coord_type)) {
  1060. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  1061. << "Expected Coordinate to be float scalar or vector";
  1062. }
  1063. }
  1064. const uint32_t min_coord_size = GetMinCoordSize(opcode, info);
  1065. const uint32_t actual_coord_size = _.GetDimension(coord_type);
  1066. if (min_coord_size > actual_coord_size) {
  1067. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  1068. << "Expected Coordinate to have at least " << min_coord_size
  1069. << " components, but given only " << actual_coord_size;
  1070. }
  1071. const uint32_t mask = inst->words().size() <= 5 ? 0 : inst->word(5);
  1072. if (mask & SpvImageOperandsConstOffsetMask) {
  1073. if (spvIsOpenCLEnv(_.context()->target_env)) {
  1074. if (opcode == SpvOpImageSampleExplicitLod) {
  1075. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  1076. << "ConstOffset image operand not allowed "
  1077. << "in the OpenCL environment.";
  1078. }
  1079. }
  1080. }
  1081. if (spv_result_t result =
  1082. ValidateImageOperands(_, inst, info, /* word_index = */ 6))
  1083. return result;
  1084. return SPV_SUCCESS;
  1085. }
  1086. spv_result_t ValidateImageDrefLod(ValidationState_t& _,
  1087. const Instruction* inst) {
  1088. const SpvOp opcode = inst->opcode();
  1089. uint32_t actual_result_type = 0;
  1090. if (spv_result_t error = GetActualResultType(_, inst, &actual_result_type)) {
  1091. return error;
  1092. }
  1093. if (!_.IsIntScalarType(actual_result_type) &&
  1094. !_.IsFloatScalarType(actual_result_type)) {
  1095. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  1096. << "Expected " << GetActualResultTypeStr(opcode)
  1097. << " to be int or float scalar type";
  1098. }
  1099. const uint32_t image_type = _.GetOperandTypeId(inst, 2);
  1100. if (_.GetIdOpcode(image_type) != SpvOpTypeSampledImage) {
  1101. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  1102. << "Expected Sampled Image to be of type OpTypeSampledImage";
  1103. }
  1104. ImageTypeInfo info;
  1105. if (!GetImageTypeInfo(_, image_type, &info)) {
  1106. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  1107. << "Corrupt image type definition";
  1108. }
  1109. if (spv_result_t result = ValidateImageCommon(_, inst, info)) return result;
  1110. if (info.multisampled) {
  1111. // When using image operands, the Sample image operand is required if and
  1112. // only if the image is multisampled (MS=1). The Sample image operand is
  1113. // only allowed for fetch, read, and write.
  1114. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  1115. << "Dref sampling operation is invalid for multisample image";
  1116. }
  1117. if (actual_result_type != info.sampled_type) {
  1118. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  1119. << "Expected Image 'Sampled Type' to be the same as "
  1120. << GetActualResultTypeStr(opcode);
  1121. }
  1122. const uint32_t coord_type = _.GetOperandTypeId(inst, 3);
  1123. if (!_.IsFloatScalarOrVectorType(coord_type)) {
  1124. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  1125. << "Expected Coordinate to be float scalar or vector";
  1126. }
  1127. const uint32_t min_coord_size = GetMinCoordSize(opcode, info);
  1128. const uint32_t actual_coord_size = _.GetDimension(coord_type);
  1129. if (min_coord_size > actual_coord_size) {
  1130. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  1131. << "Expected Coordinate to have at least " << min_coord_size
  1132. << " components, but given only " << actual_coord_size;
  1133. }
  1134. const uint32_t dref_type = _.GetOperandTypeId(inst, 4);
  1135. if (!_.IsFloatScalarType(dref_type) || _.GetBitWidth(dref_type) != 32) {
  1136. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  1137. << "Expected Dref to be of 32-bit float type";
  1138. }
  1139. if (spv_result_t result =
  1140. ValidateImageOperands(_, inst, info, /* word_index = */ 7))
  1141. return result;
  1142. return SPV_SUCCESS;
  1143. }
  1144. spv_result_t ValidateImageFetch(ValidationState_t& _, const Instruction* inst) {
  1145. uint32_t actual_result_type = 0;
  1146. if (spv_result_t error = GetActualResultType(_, inst, &actual_result_type)) {
  1147. return error;
  1148. }
  1149. const SpvOp opcode = inst->opcode();
  1150. if (!_.IsIntVectorType(actual_result_type) &&
  1151. !_.IsFloatVectorType(actual_result_type)) {
  1152. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  1153. << "Expected " << GetActualResultTypeStr(opcode)
  1154. << " to be int or float vector type";
  1155. }
  1156. if (_.GetDimension(actual_result_type) != 4) {
  1157. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  1158. << "Expected " << GetActualResultTypeStr(opcode)
  1159. << " to have 4 components";
  1160. }
  1161. const uint32_t image_type = _.GetOperandTypeId(inst, 2);
  1162. if (_.GetIdOpcode(image_type) != SpvOpTypeImage) {
  1163. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  1164. << "Expected Image to be of type OpTypeImage";
  1165. }
  1166. ImageTypeInfo info;
  1167. if (!GetImageTypeInfo(_, image_type, &info)) {
  1168. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  1169. << "Corrupt image type definition";
  1170. }
  1171. if (_.GetIdOpcode(info.sampled_type) != SpvOpTypeVoid) {
  1172. const uint32_t result_component_type =
  1173. _.GetComponentType(actual_result_type);
  1174. if (result_component_type != info.sampled_type) {
  1175. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  1176. << "Expected Image 'Sampled Type' to be the same as "
  1177. << GetActualResultTypeStr(opcode) << " components";
  1178. }
  1179. }
  1180. if (info.dim == SpvDimCube) {
  1181. return _.diag(SPV_ERROR_INVALID_DATA, inst) << "Image 'Dim' cannot be Cube";
  1182. }
  1183. if (info.sampled != 1) {
  1184. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  1185. << "Expected Image 'Sampled' parameter to be 1";
  1186. }
  1187. const uint32_t coord_type = _.GetOperandTypeId(inst, 3);
  1188. if (!_.IsIntScalarOrVectorType(coord_type)) {
  1189. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  1190. << "Expected Coordinate to be int scalar or vector";
  1191. }
  1192. const uint32_t min_coord_size = GetMinCoordSize(opcode, info);
  1193. const uint32_t actual_coord_size = _.GetDimension(coord_type);
  1194. if (min_coord_size > actual_coord_size) {
  1195. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  1196. << "Expected Coordinate to have at least " << min_coord_size
  1197. << " components, but given only " << actual_coord_size;
  1198. }
  1199. if (spv_result_t result =
  1200. ValidateImageOperands(_, inst, info, /* word_index = */ 6))
  1201. return result;
  1202. return SPV_SUCCESS;
  1203. }
  1204. spv_result_t ValidateImageGather(ValidationState_t& _,
  1205. const Instruction* inst) {
  1206. uint32_t actual_result_type = 0;
  1207. if (spv_result_t error = GetActualResultType(_, inst, &actual_result_type))
  1208. return error;
  1209. const SpvOp opcode = inst->opcode();
  1210. if (!_.IsIntVectorType(actual_result_type) &&
  1211. !_.IsFloatVectorType(actual_result_type)) {
  1212. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  1213. << "Expected " << GetActualResultTypeStr(opcode)
  1214. << " to be int or float vector type";
  1215. }
  1216. if (_.GetDimension(actual_result_type) != 4) {
  1217. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  1218. << "Expected " << GetActualResultTypeStr(opcode)
  1219. << " to have 4 components";
  1220. }
  1221. const uint32_t image_type = _.GetOperandTypeId(inst, 2);
  1222. if (_.GetIdOpcode(image_type) != SpvOpTypeSampledImage) {
  1223. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  1224. << "Expected Sampled Image to be of type OpTypeSampledImage";
  1225. }
  1226. ImageTypeInfo info;
  1227. if (!GetImageTypeInfo(_, image_type, &info)) {
  1228. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  1229. << "Corrupt image type definition";
  1230. }
  1231. if (info.multisampled) {
  1232. // When using image operands, the Sample image operand is required if and
  1233. // only if the image is multisampled (MS=1). The Sample image operand is
  1234. // only allowed for fetch, read, and write.
  1235. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  1236. << "Gather operation is invalid for multisample image";
  1237. }
  1238. if (opcode == SpvOpImageDrefGather || opcode == SpvOpImageSparseDrefGather ||
  1239. _.GetIdOpcode(info.sampled_type) != SpvOpTypeVoid) {
  1240. const uint32_t result_component_type =
  1241. _.GetComponentType(actual_result_type);
  1242. if (result_component_type != info.sampled_type) {
  1243. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  1244. << "Expected Image 'Sampled Type' to be the same as "
  1245. << GetActualResultTypeStr(opcode) << " components";
  1246. }
  1247. }
  1248. if (info.dim != SpvDim2D && info.dim != SpvDimCube &&
  1249. info.dim != SpvDimRect) {
  1250. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  1251. << "Expected Image 'Dim' cannot be Cube";
  1252. }
  1253. const uint32_t coord_type = _.GetOperandTypeId(inst, 3);
  1254. if (!_.IsFloatScalarOrVectorType(coord_type)) {
  1255. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  1256. << "Expected Coordinate to be float scalar or vector";
  1257. }
  1258. const uint32_t min_coord_size = GetMinCoordSize(opcode, info);
  1259. const uint32_t actual_coord_size = _.GetDimension(coord_type);
  1260. if (min_coord_size > actual_coord_size) {
  1261. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  1262. << "Expected Coordinate to have at least " << min_coord_size
  1263. << " components, but given only " << actual_coord_size;
  1264. }
  1265. if (opcode == SpvOpImageGather || opcode == SpvOpImageSparseGather) {
  1266. const uint32_t component = inst->GetOperandAs<uint32_t>(4);
  1267. const uint32_t component_index_type = _.GetTypeId(component);
  1268. if (!_.IsIntScalarType(component_index_type) ||
  1269. _.GetBitWidth(component_index_type) != 32) {
  1270. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  1271. << "Expected Component to be 32-bit int scalar";
  1272. }
  1273. if (spvIsVulkanEnv(_.context()->target_env)) {
  1274. if (!spvOpcodeIsConstant(_.GetIdOpcode(component))) {
  1275. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  1276. << _.VkErrorID(4664)
  1277. << "Expected Component Operand to be a const object for Vulkan "
  1278. "environment";
  1279. }
  1280. }
  1281. } else {
  1282. assert(opcode == SpvOpImageDrefGather ||
  1283. opcode == SpvOpImageSparseDrefGather);
  1284. const uint32_t dref_type = _.GetOperandTypeId(inst, 4);
  1285. if (!_.IsFloatScalarType(dref_type) || _.GetBitWidth(dref_type) != 32) {
  1286. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  1287. << "Expected Dref to be of 32-bit float type";
  1288. }
  1289. }
  1290. if (spv_result_t result =
  1291. ValidateImageOperands(_, inst, info, /* word_index = */ 7))
  1292. return result;
  1293. return SPV_SUCCESS;
  1294. }
  1295. spv_result_t ValidateImageRead(ValidationState_t& _, const Instruction* inst) {
  1296. const SpvOp opcode = inst->opcode();
  1297. uint32_t actual_result_type = 0;
  1298. if (spv_result_t error = GetActualResultType(_, inst, &actual_result_type)) {
  1299. return error;
  1300. }
  1301. if (!_.IsIntScalarOrVectorType(actual_result_type) &&
  1302. !_.IsFloatScalarOrVectorType(actual_result_type)) {
  1303. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  1304. << "Expected " << GetActualResultTypeStr(opcode)
  1305. << " to be int or float scalar or vector type";
  1306. }
  1307. const auto target_env = _.context()->target_env;
  1308. // Vulkan requires the result to be a 4-element int or float
  1309. // vector.
  1310. if (spvIsVulkanEnv(target_env)) {
  1311. if (_.GetDimension(actual_result_type) != 4) {
  1312. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  1313. << _.VkErrorID(4780) << "Expected "
  1314. << GetActualResultTypeStr(opcode) << " to have 4 components";
  1315. }
  1316. } // Check OpenCL below, after we get the image info.
  1317. const uint32_t image_type = _.GetOperandTypeId(inst, 2);
  1318. if (_.GetIdOpcode(image_type) != SpvOpTypeImage) {
  1319. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  1320. << "Expected Image to be of type OpTypeImage";
  1321. }
  1322. ImageTypeInfo info;
  1323. if (!GetImageTypeInfo(_, image_type, &info)) {
  1324. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  1325. << "Corrupt image type definition";
  1326. }
  1327. if (spvIsOpenCLEnv(target_env)) {
  1328. // In OpenCL, a read from a depth image returns a scalar float. In other
  1329. // cases, the result is always a 4-element vector.
  1330. // https://www.khronos.org/registry/OpenCL/specs/3.0-unified/html/OpenCL_Env.html#_data_format_for_reading_and_writing_images
  1331. // https://www.khronos.org/registry/OpenCL/specs/3.0-unified/html/OpenCL_C.html#image-read-and-write-functions
  1332. // The builtins for reading depth images are:
  1333. // float read_imagef(aQual image2d_depth_t image, int2 coord)
  1334. // float read_imagef(aQual image2d_array_depth_t image, int4 coord)
  1335. if (info.depth) {
  1336. if (!_.IsFloatScalarType(actual_result_type)) {
  1337. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  1338. << "Expected " << GetActualResultTypeStr(opcode)
  1339. << " from a depth image read to result in a scalar float value";
  1340. }
  1341. } else {
  1342. if (_.GetDimension(actual_result_type) != 4) {
  1343. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  1344. << "Expected " << GetActualResultTypeStr(opcode)
  1345. << " to have 4 components";
  1346. }
  1347. }
  1348. }
  1349. if (info.dim == SpvDimSubpassData) {
  1350. if (opcode == SpvOpImageSparseRead) {
  1351. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  1352. << "Image Dim SubpassData cannot be used with ImageSparseRead";
  1353. }
  1354. _.function(inst->function()->id())
  1355. ->RegisterExecutionModelLimitation(
  1356. SpvExecutionModelFragment,
  1357. std::string("Dim SubpassData requires Fragment execution model: ") +
  1358. spvOpcodeString(opcode));
  1359. }
  1360. if (_.GetIdOpcode(info.sampled_type) != SpvOpTypeVoid) {
  1361. const uint32_t result_component_type =
  1362. _.GetComponentType(actual_result_type);
  1363. if (result_component_type != info.sampled_type) {
  1364. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  1365. << "Expected Image 'Sampled Type' to be the same as "
  1366. << GetActualResultTypeStr(opcode) << " components";
  1367. }
  1368. }
  1369. if (spv_result_t result = ValidateImageCommon(_, inst, info)) return result;
  1370. const uint32_t coord_type = _.GetOperandTypeId(inst, 3);
  1371. if (!_.IsIntScalarOrVectorType(coord_type)) {
  1372. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  1373. << "Expected Coordinate to be int scalar or vector";
  1374. }
  1375. const uint32_t min_coord_size = GetMinCoordSize(opcode, info);
  1376. const uint32_t actual_coord_size = _.GetDimension(coord_type);
  1377. if (min_coord_size > actual_coord_size) {
  1378. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  1379. << "Expected Coordinate to have at least " << min_coord_size
  1380. << " components, but given only " << actual_coord_size;
  1381. }
  1382. if (spvIsVulkanEnv(_.context()->target_env)) {
  1383. if (info.format == SpvImageFormatUnknown && info.dim != SpvDimSubpassData &&
  1384. !_.HasCapability(SpvCapabilityStorageImageReadWithoutFormat)) {
  1385. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  1386. << "Capability StorageImageReadWithoutFormat is required to "
  1387. << "read storage image";
  1388. }
  1389. }
  1390. const uint32_t mask = inst->words().size() <= 5 ? 0 : inst->word(5);
  1391. if (mask & SpvImageOperandsConstOffsetMask) {
  1392. if (spvIsOpenCLEnv(_.context()->target_env)) {
  1393. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  1394. << "ConstOffset image operand not allowed "
  1395. << "in the OpenCL environment.";
  1396. }
  1397. }
  1398. if (spv_result_t result =
  1399. ValidateImageOperands(_, inst, info, /* word_index = */ 6))
  1400. return result;
  1401. return SPV_SUCCESS;
  1402. }
  1403. spv_result_t ValidateImageWrite(ValidationState_t& _, const Instruction* inst) {
  1404. const uint32_t image_type = _.GetOperandTypeId(inst, 0);
  1405. if (_.GetIdOpcode(image_type) != SpvOpTypeImage) {
  1406. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  1407. << "Expected Image to be of type OpTypeImage";
  1408. }
  1409. ImageTypeInfo info;
  1410. if (!GetImageTypeInfo(_, image_type, &info)) {
  1411. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  1412. << "Corrupt image type definition";
  1413. }
  1414. if (info.dim == SpvDimSubpassData) {
  1415. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  1416. << "Image 'Dim' cannot be SubpassData";
  1417. }
  1418. if (spv_result_t result = ValidateImageCommon(_, inst, info)) return result;
  1419. const uint32_t coord_type = _.GetOperandTypeId(inst, 1);
  1420. if (!_.IsIntScalarOrVectorType(coord_type)) {
  1421. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  1422. << "Expected Coordinate to be int scalar or vector";
  1423. }
  1424. const uint32_t min_coord_size = GetMinCoordSize(inst->opcode(), info);
  1425. const uint32_t actual_coord_size = _.GetDimension(coord_type);
  1426. if (min_coord_size > actual_coord_size) {
  1427. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  1428. << "Expected Coordinate to have at least " << min_coord_size
  1429. << " components, but given only " << actual_coord_size;
  1430. }
  1431. // TODO([email protected]) The spec doesn't explicitely say what the type
  1432. // of texel should be.
  1433. const uint32_t texel_type = _.GetOperandTypeId(inst, 2);
  1434. if (!_.IsIntScalarOrVectorType(texel_type) &&
  1435. !_.IsFloatScalarOrVectorType(texel_type)) {
  1436. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  1437. << "Expected Texel to be int or float vector or scalar";
  1438. }
  1439. #if 0
  1440. // TODO: See above.
  1441. if (_.GetDimension(texel_type) != 4) {
  1442. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  1443. << "Expected Texel to have 4 components";
  1444. }
  1445. #endif
  1446. if (_.GetIdOpcode(info.sampled_type) != SpvOpTypeVoid) {
  1447. const uint32_t texel_component_type = _.GetComponentType(texel_type);
  1448. if (texel_component_type != info.sampled_type) {
  1449. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  1450. << "Expected Image 'Sampled Type' to be the same as Texel "
  1451. << "components";
  1452. }
  1453. }
  1454. if (spvIsVulkanEnv(_.context()->target_env)) {
  1455. if (info.format == SpvImageFormatUnknown && info.dim != SpvDimSubpassData &&
  1456. !_.HasCapability(SpvCapabilityStorageImageWriteWithoutFormat)) {
  1457. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  1458. << "Capability StorageImageWriteWithoutFormat is required to "
  1459. "write "
  1460. << "to storage image";
  1461. }
  1462. }
  1463. if (inst->words().size() > 4) {
  1464. if (spvIsOpenCLEnv(_.context()->target_env)) {
  1465. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  1466. << "Optional Image Operands are not allowed in the OpenCL "
  1467. << "environment.";
  1468. }
  1469. }
  1470. if (spv_result_t result =
  1471. ValidateImageOperands(_, inst, info, /* word_index = */ 5))
  1472. return result;
  1473. return SPV_SUCCESS;
  1474. }
  1475. spv_result_t ValidateImage(ValidationState_t& _, const Instruction* inst) {
  1476. const uint32_t result_type = inst->type_id();
  1477. if (_.GetIdOpcode(result_type) != SpvOpTypeImage) {
  1478. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  1479. << "Expected Result Type to be OpTypeImage";
  1480. }
  1481. const uint32_t sampled_image_type = _.GetOperandTypeId(inst, 2);
  1482. const Instruction* sampled_image_type_inst = _.FindDef(sampled_image_type);
  1483. assert(sampled_image_type_inst);
  1484. if (sampled_image_type_inst->opcode() != SpvOpTypeSampledImage) {
  1485. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  1486. << "Expected Sample Image to be of type OpTypeSampleImage";
  1487. }
  1488. if (sampled_image_type_inst->word(2) != result_type) {
  1489. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  1490. << "Expected Sample Image image type to be equal to Result Type";
  1491. }
  1492. return SPV_SUCCESS;
  1493. }
  1494. spv_result_t ValidateImageQuerySizeLod(ValidationState_t& _,
  1495. const Instruction* inst) {
  1496. const uint32_t result_type = inst->type_id();
  1497. if (!_.IsIntScalarOrVectorType(result_type)) {
  1498. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  1499. << "Expected Result Type to be int scalar or vector type";
  1500. }
  1501. const uint32_t image_type = _.GetOperandTypeId(inst, 2);
  1502. if (_.GetIdOpcode(image_type) != SpvOpTypeImage) {
  1503. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  1504. << "Expected Image to be of type OpTypeImage";
  1505. }
  1506. ImageTypeInfo info;
  1507. if (!GetImageTypeInfo(_, image_type, &info)) {
  1508. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  1509. << "Corrupt image type definition";
  1510. }
  1511. uint32_t expected_num_components = info.arrayed;
  1512. switch (info.dim) {
  1513. case SpvDim1D:
  1514. expected_num_components += 1;
  1515. break;
  1516. case SpvDim2D:
  1517. case SpvDimCube:
  1518. expected_num_components += 2;
  1519. break;
  1520. case SpvDim3D:
  1521. expected_num_components += 3;
  1522. break;
  1523. default:
  1524. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  1525. << "Image 'Dim' must be 1D, 2D, 3D or Cube";
  1526. }
  1527. if (info.multisampled != 0) {
  1528. return _.diag(SPV_ERROR_INVALID_DATA, inst) << "Image 'MS' must be 0";
  1529. }
  1530. const auto target_env = _.context()->target_env;
  1531. if (spvIsVulkanEnv(target_env)) {
  1532. if (info.sampled != 1) {
  1533. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  1534. << _.VkErrorID(4659)
  1535. << "OpImageQuerySizeLod must only consume an \"Image\" operand "
  1536. "whose type has its \"Sampled\" operand set to 1";
  1537. }
  1538. }
  1539. uint32_t result_num_components = _.GetDimension(result_type);
  1540. if (result_num_components != expected_num_components) {
  1541. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  1542. << "Result Type has " << result_num_components << " components, "
  1543. << "but " << expected_num_components << " expected";
  1544. }
  1545. const uint32_t lod_type = _.GetOperandTypeId(inst, 3);
  1546. if (!_.IsIntScalarType(lod_type)) {
  1547. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  1548. << "Expected Level of Detail to be int scalar";
  1549. }
  1550. return SPV_SUCCESS;
  1551. }
  1552. spv_result_t ValidateImageQuerySize(ValidationState_t& _,
  1553. const Instruction* inst) {
  1554. const uint32_t result_type = inst->type_id();
  1555. if (!_.IsIntScalarOrVectorType(result_type)) {
  1556. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  1557. << "Expected Result Type to be int scalar or vector type";
  1558. }
  1559. const uint32_t image_type = _.GetOperandTypeId(inst, 2);
  1560. if (_.GetIdOpcode(image_type) != SpvOpTypeImage) {
  1561. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  1562. << "Expected Image to be of type OpTypeImage";
  1563. }
  1564. ImageTypeInfo info;
  1565. if (!GetImageTypeInfo(_, image_type, &info)) {
  1566. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  1567. << "Corrupt image type definition";
  1568. }
  1569. uint32_t expected_num_components = info.arrayed;
  1570. switch (info.dim) {
  1571. case SpvDim1D:
  1572. case SpvDimBuffer:
  1573. expected_num_components += 1;
  1574. break;
  1575. case SpvDim2D:
  1576. case SpvDimCube:
  1577. case SpvDimRect:
  1578. expected_num_components += 2;
  1579. break;
  1580. case SpvDim3D:
  1581. expected_num_components += 3;
  1582. break;
  1583. default:
  1584. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  1585. << "Image 'Dim' must be 1D, Buffer, 2D, Cube, 3D or Rect";
  1586. }
  1587. if (info.dim == SpvDim1D || info.dim == SpvDim2D || info.dim == SpvDim3D ||
  1588. info.dim == SpvDimCube) {
  1589. if (info.multisampled != 1 && info.sampled != 0 && info.sampled != 2) {
  1590. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  1591. << "Image must have either 'MS'=1 or 'Sampled'=0 or 'Sampled'=2";
  1592. }
  1593. }
  1594. uint32_t result_num_components = _.GetDimension(result_type);
  1595. if (result_num_components != expected_num_components) {
  1596. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  1597. << "Result Type has " << result_num_components << " components, "
  1598. << "but " << expected_num_components << " expected";
  1599. }
  1600. return SPV_SUCCESS;
  1601. }
  1602. spv_result_t ValidateImageQueryFormatOrOrder(ValidationState_t& _,
  1603. const Instruction* inst) {
  1604. if (!_.IsIntScalarType(inst->type_id())) {
  1605. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  1606. << "Expected Result Type to be int scalar type";
  1607. }
  1608. if (_.GetIdOpcode(_.GetOperandTypeId(inst, 2)) != SpvOpTypeImage) {
  1609. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  1610. << "Expected operand to be of type OpTypeImage";
  1611. }
  1612. return SPV_SUCCESS;
  1613. }
  1614. spv_result_t ValidateImageQueryLod(ValidationState_t& _,
  1615. const Instruction* inst) {
  1616. _.function(inst->function()->id())
  1617. ->RegisterExecutionModelLimitation(
  1618. [&](SpvExecutionModel model, std::string* message) {
  1619. if (model != SpvExecutionModelFragment &&
  1620. model != SpvExecutionModelGLCompute) {
  1621. if (message) {
  1622. *message = std::string(
  1623. "OpImageQueryLod requires Fragment or GLCompute execution "
  1624. "model");
  1625. }
  1626. return false;
  1627. }
  1628. return true;
  1629. });
  1630. _.function(inst->function()->id())
  1631. ->RegisterLimitation([](const ValidationState_t& state,
  1632. const Function* entry_point,
  1633. std::string* message) {
  1634. const auto* models = state.GetExecutionModels(entry_point->id());
  1635. const auto* modes = state.GetExecutionModes(entry_point->id());
  1636. if (models->find(SpvExecutionModelGLCompute) != models->end() &&
  1637. modes->find(SpvExecutionModeDerivativeGroupLinearNV) ==
  1638. modes->end() &&
  1639. modes->find(SpvExecutionModeDerivativeGroupQuadsNV) ==
  1640. modes->end()) {
  1641. if (message) {
  1642. *message = std::string(
  1643. "OpImageQueryLod requires DerivativeGroupQuadsNV "
  1644. "or DerivativeGroupLinearNV execution mode for GLCompute "
  1645. "execution model");
  1646. }
  1647. return false;
  1648. }
  1649. return true;
  1650. });
  1651. const uint32_t result_type = inst->type_id();
  1652. if (!_.IsFloatVectorType(result_type)) {
  1653. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  1654. << "Expected Result Type to be float vector type";
  1655. }
  1656. if (_.GetDimension(result_type) != 2) {
  1657. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  1658. << "Expected Result Type to have 2 components";
  1659. }
  1660. const uint32_t image_type = _.GetOperandTypeId(inst, 2);
  1661. if (_.GetIdOpcode(image_type) != SpvOpTypeSampledImage) {
  1662. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  1663. << "Expected Image operand to be of type OpTypeSampledImage";
  1664. }
  1665. ImageTypeInfo info;
  1666. if (!GetImageTypeInfo(_, image_type, &info)) {
  1667. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  1668. << "Corrupt image type definition";
  1669. }
  1670. if (info.dim != SpvDim1D && info.dim != SpvDim2D && info.dim != SpvDim3D &&
  1671. info.dim != SpvDimCube) {
  1672. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  1673. << "Image 'Dim' must be 1D, 2D, 3D or Cube";
  1674. }
  1675. const uint32_t coord_type = _.GetOperandTypeId(inst, 3);
  1676. if (_.HasCapability(SpvCapabilityKernel)) {
  1677. if (!_.IsFloatScalarOrVectorType(coord_type) &&
  1678. !_.IsIntScalarOrVectorType(coord_type)) {
  1679. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  1680. << "Expected Coordinate to be int or float scalar or vector";
  1681. }
  1682. } else {
  1683. if (!_.IsFloatScalarOrVectorType(coord_type)) {
  1684. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  1685. << "Expected Coordinate to be float scalar or vector";
  1686. }
  1687. }
  1688. const uint32_t min_coord_size = GetPlaneCoordSize(info);
  1689. const uint32_t actual_coord_size = _.GetDimension(coord_type);
  1690. if (min_coord_size > actual_coord_size) {
  1691. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  1692. << "Expected Coordinate to have at least " << min_coord_size
  1693. << " components, but given only " << actual_coord_size;
  1694. }
  1695. // The operad is a sampled image.
  1696. // The sampled image type is already checked to be parameterized by an image
  1697. // type with Sampled=0 or Sampled=1. Vulkan bans Sampled=0, and so we have
  1698. // Sampled=1. So the validator already enforces Vulkan VUID 4659:
  1699. // OpImageQuerySizeLod must only consume an “Image” operand whose type has
  1700. // its "Sampled" operand set to 1
  1701. return SPV_SUCCESS;
  1702. }
  1703. spv_result_t ValidateImageSparseLod(ValidationState_t& _,
  1704. const Instruction* inst) {
  1705. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  1706. << "Instruction reserved for future use, use of this instruction "
  1707. << "is invalid";
  1708. }
  1709. spv_result_t ValidateImageQueryLevelsOrSamples(ValidationState_t& _,
  1710. const Instruction* inst) {
  1711. if (!_.IsIntScalarType(inst->type_id())) {
  1712. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  1713. << "Expected Result Type to be int scalar type";
  1714. }
  1715. const uint32_t image_type = _.GetOperandTypeId(inst, 2);
  1716. if (_.GetIdOpcode(image_type) != SpvOpTypeImage) {
  1717. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  1718. << "Expected Image to be of type OpTypeImage";
  1719. }
  1720. ImageTypeInfo info;
  1721. if (!GetImageTypeInfo(_, image_type, &info)) {
  1722. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  1723. << "Corrupt image type definition";
  1724. }
  1725. const SpvOp opcode = inst->opcode();
  1726. if (opcode == SpvOpImageQueryLevels) {
  1727. if (info.dim != SpvDim1D && info.dim != SpvDim2D && info.dim != SpvDim3D &&
  1728. info.dim != SpvDimCube) {
  1729. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  1730. << "Image 'Dim' must be 1D, 2D, 3D or Cube";
  1731. }
  1732. const auto target_env = _.context()->target_env;
  1733. if (spvIsVulkanEnv(target_env)) {
  1734. if (info.sampled != 1) {
  1735. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  1736. << _.VkErrorID(4659)
  1737. << "OpImageQueryLevels must only consume an \"Image\" operand "
  1738. "whose type has its \"Sampled\" operand set to 1";
  1739. }
  1740. }
  1741. } else {
  1742. assert(opcode == SpvOpImageQuerySamples);
  1743. if (info.dim != SpvDim2D) {
  1744. return _.diag(SPV_ERROR_INVALID_DATA, inst) << "Image 'Dim' must be 2D";
  1745. }
  1746. if (info.multisampled != 1) {
  1747. return _.diag(SPV_ERROR_INVALID_DATA, inst) << "Image 'MS' must be 1";
  1748. }
  1749. }
  1750. return SPV_SUCCESS;
  1751. }
  1752. spv_result_t ValidateImageSparseTexelsResident(ValidationState_t& _,
  1753. const Instruction* inst) {
  1754. if (!_.IsBoolScalarType(inst->type_id())) {
  1755. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  1756. << "Expected Result Type to be bool scalar type";
  1757. }
  1758. const uint32_t resident_code_type = _.GetOperandTypeId(inst, 2);
  1759. if (!_.IsIntScalarType(resident_code_type)) {
  1760. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  1761. << "Expected Resident Code to be int scalar";
  1762. }
  1763. return SPV_SUCCESS;
  1764. }
  1765. } // namespace
  1766. // Validates correctness of image instructions.
  1767. spv_result_t ImagePass(ValidationState_t& _, const Instruction* inst) {
  1768. const SpvOp opcode = inst->opcode();
  1769. if (IsImplicitLod(opcode)) {
  1770. _.function(inst->function()->id())
  1771. ->RegisterExecutionModelLimitation([opcode](SpvExecutionModel model,
  1772. std::string* message) {
  1773. if (model != SpvExecutionModelFragment &&
  1774. model != SpvExecutionModelGLCompute) {
  1775. if (message) {
  1776. *message =
  1777. std::string(
  1778. "ImplicitLod instructions require Fragment or GLCompute "
  1779. "execution model: ") +
  1780. spvOpcodeString(opcode);
  1781. }
  1782. return false;
  1783. }
  1784. return true;
  1785. });
  1786. _.function(inst->function()->id())
  1787. ->RegisterLimitation([opcode](const ValidationState_t& state,
  1788. const Function* entry_point,
  1789. std::string* message) {
  1790. const auto* models = state.GetExecutionModels(entry_point->id());
  1791. const auto* modes = state.GetExecutionModes(entry_point->id());
  1792. if (models->find(SpvExecutionModelGLCompute) != models->end() &&
  1793. modes->find(SpvExecutionModeDerivativeGroupLinearNV) ==
  1794. modes->end() &&
  1795. modes->find(SpvExecutionModeDerivativeGroupQuadsNV) ==
  1796. modes->end()) {
  1797. if (message) {
  1798. *message =
  1799. std::string(
  1800. "ImplicitLod instructions require DerivativeGroupQuadsNV "
  1801. "or DerivativeGroupLinearNV execution mode for GLCompute "
  1802. "execution model: ") +
  1803. spvOpcodeString(opcode);
  1804. }
  1805. return false;
  1806. }
  1807. return true;
  1808. });
  1809. }
  1810. switch (opcode) {
  1811. case SpvOpTypeImage:
  1812. return ValidateTypeImage(_, inst);
  1813. case SpvOpTypeSampledImage:
  1814. return ValidateTypeSampledImage(_, inst);
  1815. case SpvOpSampledImage:
  1816. return ValidateSampledImage(_, inst);
  1817. case SpvOpImageTexelPointer:
  1818. return ValidateImageTexelPointer(_, inst);
  1819. case SpvOpImageSampleImplicitLod:
  1820. case SpvOpImageSampleExplicitLod:
  1821. case SpvOpImageSampleProjImplicitLod:
  1822. case SpvOpImageSampleProjExplicitLod:
  1823. case SpvOpImageSparseSampleImplicitLod:
  1824. case SpvOpImageSparseSampleExplicitLod:
  1825. return ValidateImageLod(_, inst);
  1826. case SpvOpImageSampleDrefImplicitLod:
  1827. case SpvOpImageSampleDrefExplicitLod:
  1828. case SpvOpImageSampleProjDrefImplicitLod:
  1829. case SpvOpImageSampleProjDrefExplicitLod:
  1830. case SpvOpImageSparseSampleDrefImplicitLod:
  1831. case SpvOpImageSparseSampleDrefExplicitLod:
  1832. return ValidateImageDrefLod(_, inst);
  1833. case SpvOpImageFetch:
  1834. case SpvOpImageSparseFetch:
  1835. return ValidateImageFetch(_, inst);
  1836. case SpvOpImageGather:
  1837. case SpvOpImageDrefGather:
  1838. case SpvOpImageSparseGather:
  1839. case SpvOpImageSparseDrefGather:
  1840. return ValidateImageGather(_, inst);
  1841. case SpvOpImageRead:
  1842. case SpvOpImageSparseRead:
  1843. return ValidateImageRead(_, inst);
  1844. case SpvOpImageWrite:
  1845. return ValidateImageWrite(_, inst);
  1846. case SpvOpImage:
  1847. return ValidateImage(_, inst);
  1848. case SpvOpImageQueryFormat:
  1849. case SpvOpImageQueryOrder:
  1850. return ValidateImageQueryFormatOrOrder(_, inst);
  1851. case SpvOpImageQuerySizeLod:
  1852. return ValidateImageQuerySizeLod(_, inst);
  1853. case SpvOpImageQuerySize:
  1854. return ValidateImageQuerySize(_, inst);
  1855. case SpvOpImageQueryLod:
  1856. return ValidateImageQueryLod(_, inst);
  1857. case SpvOpImageQueryLevels:
  1858. case SpvOpImageQuerySamples:
  1859. return ValidateImageQueryLevelsOrSamples(_, inst);
  1860. case SpvOpImageSparseSampleProjImplicitLod:
  1861. case SpvOpImageSparseSampleProjExplicitLod:
  1862. case SpvOpImageSparseSampleProjDrefImplicitLod:
  1863. case SpvOpImageSparseSampleProjDrefExplicitLod:
  1864. return ValidateImageSparseLod(_, inst);
  1865. case SpvOpImageSparseTexelsResident:
  1866. return ValidateImageSparseTexelsResident(_, inst);
  1867. default:
  1868. break;
  1869. }
  1870. return SPV_SUCCESS;
  1871. }
  1872. } // namespace val
  1873. } // namespace spvtools