validate_image.cpp 77 KB

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