validate_image.cpp 78 KB

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