validate_image.cpp 78 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206
  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. // Checks some of the validation rules which are common to multiple opcodes.
  559. spv_result_t ValidateImageCommon(ValidationState_t& _, const Instruction* inst,
  560. const ImageTypeInfo& info) {
  561. const SpvOp opcode = inst->opcode();
  562. if (IsProj(opcode)) {
  563. if (info.dim != SpvDim1D && info.dim != SpvDim2D && info.dim != SpvDim3D &&
  564. info.dim != SpvDimRect) {
  565. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  566. << "Expected Image 'Dim' parameter to be 1D, 2D, 3D or Rect";
  567. }
  568. if (info.multisampled != 0) {
  569. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  570. << "Expected Image 'MS' parameter to be 0";
  571. }
  572. if (info.arrayed != 0) {
  573. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  574. << "Expected Image 'arrayed' parameter to be 0";
  575. }
  576. }
  577. if (opcode == SpvOpImageRead || opcode == SpvOpImageSparseRead ||
  578. opcode == SpvOpImageWrite) {
  579. if (info.sampled == 0) {
  580. } else 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 &&
  599. !_.HasCapability(SpvCapabilityImageMSArray)) {
  600. #if 0
  601. // TODO([email protected]) The description of this rule in the spec
  602. // is unclear and Glslang doesn't declare ImageMSArray. Need to clarify
  603. // and reenable.
  604. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  605. << "Capability ImageMSArray is required to access storage "
  606. << "image";
  607. #endif
  608. }
  609. } else {
  610. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  611. << "Expected Image 'Sampled' parameter to be 0 or 2";
  612. }
  613. }
  614. return SPV_SUCCESS;
  615. }
  616. // Returns true if opcode is *ImageSparse*, false otherwise.
  617. bool IsSparse(SpvOp opcode) {
  618. switch (opcode) {
  619. case SpvOpImageSparseSampleImplicitLod:
  620. case SpvOpImageSparseSampleExplicitLod:
  621. case SpvOpImageSparseSampleDrefImplicitLod:
  622. case SpvOpImageSparseSampleDrefExplicitLod:
  623. case SpvOpImageSparseSampleProjImplicitLod:
  624. case SpvOpImageSparseSampleProjExplicitLod:
  625. case SpvOpImageSparseSampleProjDrefImplicitLod:
  626. case SpvOpImageSparseSampleProjDrefExplicitLod:
  627. case SpvOpImageSparseFetch:
  628. case SpvOpImageSparseGather:
  629. case SpvOpImageSparseDrefGather:
  630. case SpvOpImageSparseTexelsResident:
  631. case SpvOpImageSparseRead: {
  632. return true;
  633. }
  634. default: { return false; }
  635. }
  636. return false;
  637. }
  638. // Checks sparse image opcode result type and returns the second struct member.
  639. // Returns inst.type_id for non-sparse image opcodes.
  640. // Not valid for sparse image opcodes which do not return a struct.
  641. spv_result_t GetActualResultType(ValidationState_t& _, const Instruction* inst,
  642. uint32_t* actual_result_type) {
  643. const SpvOp opcode = inst->opcode();
  644. if (IsSparse(opcode)) {
  645. const Instruction* const type_inst = _.FindDef(inst->type_id());
  646. assert(type_inst);
  647. if (!type_inst || type_inst->opcode() != SpvOpTypeStruct) {
  648. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  649. << "Expected Result Type to be OpTypeStruct";
  650. }
  651. if (type_inst->words().size() != 4 ||
  652. !_.IsIntScalarType(type_inst->word(2))) {
  653. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  654. << "Expected Result Type to be a struct containing an int "
  655. "scalar and a texel";
  656. }
  657. *actual_result_type = type_inst->word(3);
  658. } else {
  659. *actual_result_type = inst->type_id();
  660. }
  661. return SPV_SUCCESS;
  662. }
  663. // Returns a string describing actual result type of an opcode.
  664. // Not valid for sparse image opcodes which do not return a struct.
  665. const char* GetActualResultTypeStr(SpvOp opcode) {
  666. if (IsSparse(opcode)) return "Result Type's second member";
  667. return "Result Type";
  668. }
  669. spv_result_t ValidateTypeImage(ValidationState_t& _, const Instruction* inst) {
  670. assert(inst->type_id() == 0);
  671. ImageTypeInfo info;
  672. if (!GetImageTypeInfo(_, inst->word(1), &info)) {
  673. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  674. << "Corrupt image type definition";
  675. }
  676. if (_.IsIntScalarType(info.sampled_type) &&
  677. (64 == _.GetBitWidth(info.sampled_type)) &&
  678. !_.HasCapability(SpvCapabilityInt64ImageEXT)) {
  679. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  680. << "Capability Int64ImageEXT is required when using Sampled Type of "
  681. "64-bit int";
  682. }
  683. const auto target_env = _.context()->target_env;
  684. if (spvIsVulkanEnv(target_env)) {
  685. if ((!_.IsFloatScalarType(info.sampled_type) &&
  686. !_.IsIntScalarType(info.sampled_type)) ||
  687. ((32 != _.GetBitWidth(info.sampled_type)) &&
  688. (64 != _.GetBitWidth(info.sampled_type))) ||
  689. ((64 == _.GetBitWidth(info.sampled_type)) &&
  690. _.IsFloatScalarType(info.sampled_type))) {
  691. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  692. << _.VkErrorID(4656)
  693. << "Expected Sampled Type to be a 32-bit int, 64-bit int or "
  694. "32-bit float scalar type for Vulkan environment";
  695. }
  696. } else if (spvIsOpenCLEnv(target_env)) {
  697. if (!_.IsVoidType(info.sampled_type)) {
  698. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  699. << "Sampled Type must be OpTypeVoid in the OpenCL environment.";
  700. }
  701. } else {
  702. const SpvOp sampled_type_opcode = _.GetIdOpcode(info.sampled_type);
  703. if (sampled_type_opcode != SpvOpTypeVoid &&
  704. sampled_type_opcode != SpvOpTypeInt &&
  705. sampled_type_opcode != SpvOpTypeFloat) {
  706. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  707. << "Expected Sampled Type to be either void or"
  708. << " numerical scalar type";
  709. }
  710. }
  711. // Dim is checked elsewhere.
  712. if (info.depth > 2) {
  713. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  714. << "Invalid Depth " << info.depth << " (must be 0, 1 or 2)";
  715. }
  716. if (info.arrayed > 1) {
  717. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  718. << "Invalid Arrayed " << info.arrayed << " (must be 0 or 1)";
  719. }
  720. if (spvIsOpenCLEnv(target_env)) {
  721. if ((info.arrayed == 1) && (info.dim != SpvDim1D) &&
  722. (info.dim != SpvDim2D)) {
  723. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  724. << "In the OpenCL environment, Arrayed may only be set to 1 "
  725. << "when Dim is either 1D or 2D.";
  726. }
  727. }
  728. if (info.multisampled > 1) {
  729. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  730. << "Invalid MS " << info.multisampled << " (must be 0 or 1)";
  731. }
  732. if (spvIsOpenCLEnv(target_env)) {
  733. if (info.multisampled != 0) {
  734. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  735. << "MS must be 0 in the OpenCL environment.";
  736. }
  737. }
  738. if (info.sampled > 2) {
  739. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  740. << "Invalid Sampled " << info.sampled << " (must be 0, 1 or 2)";
  741. }
  742. if (spvIsVulkanEnv(target_env)) {
  743. if (info.sampled == 0) {
  744. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  745. << _.VkErrorID(4657)
  746. << "Sampled must be 1 or 2 in the Vulkan environment.";
  747. }
  748. }
  749. if (spvIsOpenCLEnv(_.context()->target_env)) {
  750. if (info.sampled != 0) {
  751. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  752. << "Sampled must be 0 in the OpenCL environment.";
  753. }
  754. }
  755. if (info.dim == SpvDimSubpassData) {
  756. if (info.sampled != 2) {
  757. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  758. << _.VkErrorID(6214) << "Dim SubpassData requires Sampled to be 2";
  759. }
  760. if (info.format != SpvImageFormatUnknown) {
  761. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  762. << "Dim SubpassData requires format Unknown";
  763. }
  764. if (spvIsVulkanEnv(target_env)) {
  765. if (info.arrayed != 0) {
  766. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  767. << _.VkErrorID(6214)
  768. << "Dim SubpassData requires Arrayed to be 0";
  769. }
  770. }
  771. }
  772. // Format and Access Qualifier are also checked elsewhere.
  773. if (spvIsOpenCLEnv(_.context()->target_env)) {
  774. if (info.access_qualifier == SpvAccessQualifierMax) {
  775. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  776. << "In the OpenCL environment, the optional Access Qualifier"
  777. << " must be present.";
  778. }
  779. }
  780. if (info.multisampled && (info.sampled == 2) &&
  781. (info.dim != SpvDimSubpassData)) {
  782. if (!_.HasCapability(SpvCapabilityStorageImageMultisample)) {
  783. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  784. << "Capability StorageImageMultisample is required when using "
  785. "multisampled storage image";
  786. }
  787. }
  788. return SPV_SUCCESS;
  789. }
  790. spv_result_t ValidateTypeSampledImage(ValidationState_t& _,
  791. const Instruction* inst) {
  792. const uint32_t image_type = inst->word(2);
  793. if (_.GetIdOpcode(image_type) != SpvOpTypeImage) {
  794. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  795. << "Expected Image to be of type OpTypeImage";
  796. }
  797. ImageTypeInfo info;
  798. if (!GetImageTypeInfo(_, image_type, &info)) {
  799. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  800. << "Corrupt image type definition";
  801. }
  802. // OpenCL requires Sampled=0, checked elsewhere.
  803. // Vulkan uses the Sampled=1 case.
  804. if ((info.sampled != 0) && (info.sampled != 1)) {
  805. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  806. << _.VkErrorID(4657)
  807. << "Sampled image type requires an image type with \"Sampled\" "
  808. "operand set to 0 or 1";
  809. }
  810. // This covers both OpTypeSampledImage and OpSampledImage.
  811. if (_.version() >= SPV_SPIRV_VERSION_WORD(1, 6) && info.dim == SpvDimBuffer) {
  812. return _.diag(SPV_ERROR_INVALID_ID, inst)
  813. << "In SPIR-V 1.6 or later, sampled image dimension must not be "
  814. "Buffer";
  815. }
  816. return SPV_SUCCESS;
  817. }
  818. bool IsAllowedSampledImageOperand(SpvOp opcode) {
  819. switch (opcode) {
  820. case SpvOpSampledImage:
  821. case SpvOpImageSampleImplicitLod:
  822. case SpvOpImageSampleExplicitLod:
  823. case SpvOpImageSampleDrefImplicitLod:
  824. case SpvOpImageSampleDrefExplicitLod:
  825. case SpvOpImageSampleProjImplicitLod:
  826. case SpvOpImageSampleProjExplicitLod:
  827. case SpvOpImageSampleProjDrefImplicitLod:
  828. case SpvOpImageSampleProjDrefExplicitLod:
  829. case SpvOpImageGather:
  830. case SpvOpImageDrefGather:
  831. case SpvOpImage:
  832. case SpvOpImageQueryLod:
  833. case SpvOpImageSparseSampleImplicitLod:
  834. case SpvOpImageSparseSampleExplicitLod:
  835. case SpvOpImageSparseSampleDrefImplicitLod:
  836. case SpvOpImageSparseSampleDrefExplicitLod:
  837. case SpvOpImageSparseGather:
  838. case SpvOpImageSparseDrefGather:
  839. case SpvOpCopyObject:
  840. return true;
  841. default:
  842. return false;
  843. }
  844. }
  845. spv_result_t ValidateSampledImage(ValidationState_t& _,
  846. const Instruction* inst) {
  847. if (_.GetIdOpcode(inst->type_id()) != SpvOpTypeSampledImage) {
  848. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  849. << "Expected Result Type to be OpTypeSampledImage.";
  850. }
  851. const uint32_t image_type = _.GetOperandTypeId(inst, 2);
  852. if (_.GetIdOpcode(image_type) != SpvOpTypeImage) {
  853. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  854. << "Expected Image to be of type OpTypeImage.";
  855. }
  856. ImageTypeInfo info;
  857. if (!GetImageTypeInfo(_, image_type, &info)) {
  858. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  859. << "Corrupt image type definition";
  860. }
  861. // TODO([email protected]) Check compatibility of result type and received
  862. // image.
  863. if (spvIsVulkanEnv(_.context()->target_env)) {
  864. if (info.sampled != 1) {
  865. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  866. << "Expected Image 'Sampled' parameter to be 1 "
  867. << "for Vulkan environment.";
  868. }
  869. } else {
  870. if (info.sampled != 0 && info.sampled != 1) {
  871. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  872. << "Expected Image 'Sampled' parameter to be 0 or 1";
  873. }
  874. }
  875. if (info.dim == SpvDimSubpassData) {
  876. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  877. << "Expected Image 'Dim' parameter to be not SubpassData.";
  878. }
  879. if (_.GetIdOpcode(_.GetOperandTypeId(inst, 3)) != SpvOpTypeSampler) {
  880. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  881. << "Expected Sampler to be of type OpTypeSampler";
  882. }
  883. // We need to validate 2 things:
  884. // * All OpSampledImage instructions must be in the same block in which their
  885. // Result <id> are consumed.
  886. // * Result <id> from OpSampledImage instructions must not appear as operands
  887. // to OpPhi instructions or OpSelect instructions, or any instructions other
  888. // than the image lookup and query instructions specified to take an operand
  889. // whose type is OpTypeSampledImage.
  890. std::vector<Instruction*> consumers = _.getSampledImageConsumers(inst->id());
  891. if (!consumers.empty()) {
  892. for (auto consumer_instr : consumers) {
  893. const auto consumer_opcode = consumer_instr->opcode();
  894. if (consumer_instr->block() != inst->block()) {
  895. return _.diag(SPV_ERROR_INVALID_ID, inst)
  896. << "All OpSampledImage instructions must be in the same block "
  897. "in "
  898. "which their Result <id> are consumed. OpSampledImage Result "
  899. "Type <id> '"
  900. << _.getIdName(inst->id())
  901. << "' has a consumer in a different basic "
  902. "block. The consumer instruction <id> is '"
  903. << _.getIdName(consumer_instr->id()) << "'.";
  904. }
  905. if (consumer_opcode == SpvOpPhi || consumer_opcode == SpvOpSelect) {
  906. return _.diag(SPV_ERROR_INVALID_ID, inst)
  907. << "Result <id> from OpSampledImage instruction must not appear "
  908. "as "
  909. "operands of Op"
  910. << spvOpcodeString(static_cast<SpvOp>(consumer_opcode)) << "."
  911. << " Found result <id> '" << _.getIdName(inst->id())
  912. << "' as an operand of <id> '"
  913. << _.getIdName(consumer_instr->id()) << "'.";
  914. }
  915. if (!IsAllowedSampledImageOperand(consumer_opcode)) {
  916. return _.diag(SPV_ERROR_INVALID_ID, inst)
  917. << "Result <id> from OpSampledImage instruction must not appear "
  918. "as operand for Op"
  919. << spvOpcodeString(static_cast<SpvOp>(consumer_opcode))
  920. << ", since it is not specified as taking an "
  921. << "OpTypeSampledImage."
  922. << " Found result <id> '" << _.getIdName(inst->id())
  923. << "' as an operand of <id> '"
  924. << _.getIdName(consumer_instr->id()) << "'.";
  925. }
  926. }
  927. }
  928. return SPV_SUCCESS;
  929. }
  930. spv_result_t ValidateImageTexelPointer(ValidationState_t& _,
  931. const Instruction* inst) {
  932. const auto result_type = _.FindDef(inst->type_id());
  933. if (result_type->opcode() != SpvOpTypePointer) {
  934. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  935. << "Expected Result Type to be OpTypePointer";
  936. }
  937. const auto storage_class = result_type->GetOperandAs<uint32_t>(1);
  938. if (storage_class != SpvStorageClassImage) {
  939. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  940. << "Expected Result Type to be OpTypePointer whose Storage Class "
  941. "operand is Image";
  942. }
  943. const auto ptr_type = result_type->GetOperandAs<uint32_t>(2);
  944. const auto ptr_opcode = _.GetIdOpcode(ptr_type);
  945. if (ptr_opcode != SpvOpTypeInt && ptr_opcode != SpvOpTypeFloat &&
  946. ptr_opcode != SpvOpTypeVoid) {
  947. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  948. << "Expected Result Type to be OpTypePointer whose Type operand "
  949. "must be a scalar numerical type or OpTypeVoid";
  950. }
  951. const auto image_ptr = _.FindDef(_.GetOperandTypeId(inst, 2));
  952. if (!image_ptr || image_ptr->opcode() != SpvOpTypePointer) {
  953. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  954. << "Expected Image to be OpTypePointer";
  955. }
  956. const auto image_type = image_ptr->GetOperandAs<uint32_t>(2);
  957. if (_.GetIdOpcode(image_type) != SpvOpTypeImage) {
  958. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  959. << "Expected Image to be OpTypePointer with Type OpTypeImage";
  960. }
  961. ImageTypeInfo info;
  962. if (!GetImageTypeInfo(_, image_type, &info)) {
  963. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  964. << "Corrupt image type definition";
  965. }
  966. if (info.sampled_type != ptr_type) {
  967. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  968. << "Expected Image 'Sampled Type' to be the same as the Type "
  969. "pointed to by Result Type";
  970. }
  971. if (info.dim == SpvDimSubpassData) {
  972. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  973. << "Image Dim SubpassData cannot be used with OpImageTexelPointer";
  974. }
  975. const uint32_t coord_type = _.GetOperandTypeId(inst, 3);
  976. if (!coord_type || !_.IsIntScalarOrVectorType(coord_type)) {
  977. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  978. << "Expected Coordinate to be integer scalar or vector";
  979. }
  980. uint32_t expected_coord_size = 0;
  981. if (info.arrayed == 0) {
  982. expected_coord_size = GetPlaneCoordSize(info);
  983. } else if (info.arrayed == 1) {
  984. switch (info.dim) {
  985. case SpvDim1D:
  986. expected_coord_size = 2;
  987. break;
  988. case SpvDimCube:
  989. case SpvDim2D:
  990. expected_coord_size = 3;
  991. break;
  992. default:
  993. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  994. << "Expected Image 'Dim' must be one of 1D, 2D, or Cube when "
  995. "Arrayed is 1";
  996. break;
  997. }
  998. }
  999. const uint32_t actual_coord_size = _.GetDimension(coord_type);
  1000. if (expected_coord_size != actual_coord_size) {
  1001. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  1002. << "Expected Coordinate to have " << expected_coord_size
  1003. << " components, but given " << actual_coord_size;
  1004. }
  1005. const uint32_t sample_type = _.GetOperandTypeId(inst, 4);
  1006. if (!sample_type || !_.IsIntScalarType(sample_type)) {
  1007. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  1008. << "Expected Sample to be integer scalar";
  1009. }
  1010. if (info.multisampled == 0) {
  1011. uint64_t ms = 0;
  1012. if (!_.GetConstantValUint64(inst->GetOperandAs<uint32_t>(4), &ms) ||
  1013. ms != 0) {
  1014. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  1015. << "Expected Sample for Image with MS 0 to be a valid <id> for "
  1016. "the value 0";
  1017. }
  1018. }
  1019. if (spvIsVulkanEnv(_.context()->target_env)) {
  1020. if ((info.format != SpvImageFormatR64i) &&
  1021. (info.format != SpvImageFormatR64ui) &&
  1022. (info.format != SpvImageFormatR32f) &&
  1023. (info.format != SpvImageFormatR32i) &&
  1024. (info.format != SpvImageFormatR32ui)) {
  1025. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  1026. << _.VkErrorID(4658)
  1027. << "Expected the Image Format in Image to be R64i, R64ui, R32f, "
  1028. "R32i, or R32ui for Vulkan environment";
  1029. }
  1030. }
  1031. return SPV_SUCCESS;
  1032. }
  1033. spv_result_t ValidateImageLod(ValidationState_t& _, const Instruction* inst) {
  1034. const SpvOp opcode = inst->opcode();
  1035. uint32_t actual_result_type = 0;
  1036. if (spv_result_t error = GetActualResultType(_, inst, &actual_result_type)) {
  1037. return error;
  1038. }
  1039. if (!_.IsIntVectorType(actual_result_type) &&
  1040. !_.IsFloatVectorType(actual_result_type)) {
  1041. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  1042. << "Expected " << GetActualResultTypeStr(opcode)
  1043. << " to be int or float vector type";
  1044. }
  1045. if (_.GetDimension(actual_result_type) != 4) {
  1046. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  1047. << "Expected " << GetActualResultTypeStr(opcode)
  1048. << " to have 4 components";
  1049. }
  1050. const uint32_t image_type = _.GetOperandTypeId(inst, 2);
  1051. if (_.GetIdOpcode(image_type) != SpvOpTypeSampledImage) {
  1052. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  1053. << "Expected Sampled Image to be of type OpTypeSampledImage";
  1054. }
  1055. ImageTypeInfo info;
  1056. if (!GetImageTypeInfo(_, image_type, &info)) {
  1057. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  1058. << "Corrupt image type definition";
  1059. }
  1060. if (spv_result_t result = ValidateImageCommon(_, inst, info)) return result;
  1061. if (info.multisampled) {
  1062. // When using image operands, the Sample image operand is required if and
  1063. // only if the image is multisampled (MS=1). The Sample image operand is
  1064. // only allowed for fetch, read, and write.
  1065. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  1066. << "Sampling operation is invalid for multisample image";
  1067. }
  1068. if (_.GetIdOpcode(info.sampled_type) != SpvOpTypeVoid) {
  1069. const uint32_t texel_component_type =
  1070. _.GetComponentType(actual_result_type);
  1071. if (texel_component_type != info.sampled_type) {
  1072. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  1073. << "Expected Image 'Sampled Type' to be the same as "
  1074. << GetActualResultTypeStr(opcode) << " components";
  1075. }
  1076. }
  1077. const uint32_t coord_type = _.GetOperandTypeId(inst, 3);
  1078. if ((opcode == SpvOpImageSampleExplicitLod ||
  1079. opcode == SpvOpImageSparseSampleExplicitLod) &&
  1080. _.HasCapability(SpvCapabilityKernel)) {
  1081. if (!_.IsFloatScalarOrVectorType(coord_type) &&
  1082. !_.IsIntScalarOrVectorType(coord_type)) {
  1083. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  1084. << "Expected Coordinate to be int or float scalar or vector";
  1085. }
  1086. } else {
  1087. if (!_.IsFloatScalarOrVectorType(coord_type)) {
  1088. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  1089. << "Expected Coordinate to be float scalar or vector";
  1090. }
  1091. }
  1092. const uint32_t min_coord_size = GetMinCoordSize(opcode, info);
  1093. const uint32_t actual_coord_size = _.GetDimension(coord_type);
  1094. if (min_coord_size > actual_coord_size) {
  1095. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  1096. << "Expected Coordinate to have at least " << min_coord_size
  1097. << " components, but given only " << actual_coord_size;
  1098. }
  1099. const uint32_t mask = inst->words().size() <= 5 ? 0 : inst->word(5);
  1100. if (mask & SpvImageOperandsConstOffsetMask) {
  1101. if (spvIsOpenCLEnv(_.context()->target_env)) {
  1102. if (opcode == SpvOpImageSampleExplicitLod) {
  1103. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  1104. << "ConstOffset image operand not allowed "
  1105. << "in the OpenCL environment.";
  1106. }
  1107. }
  1108. }
  1109. if (spv_result_t result =
  1110. ValidateImageOperands(_, inst, info, /* word_index = */ 6))
  1111. return result;
  1112. return SPV_SUCCESS;
  1113. }
  1114. // Validates anything OpImage*Dref* instruction
  1115. spv_result_t ValidateImageDref(ValidationState_t& _, const Instruction* inst,
  1116. const ImageTypeInfo& info) {
  1117. const uint32_t dref_type = _.GetOperandTypeId(inst, 4);
  1118. if (!_.IsFloatScalarType(dref_type) || _.GetBitWidth(dref_type) != 32) {
  1119. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  1120. << "Expected Dref to be of 32-bit float type";
  1121. }
  1122. if (spvIsVulkanEnv(_.context()->target_env)) {
  1123. if (info.dim == SpvDim3D) {
  1124. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  1125. << _.VkErrorID(4777)
  1126. << "In Vulkan, OpImage*Dref* instructions must not use images "
  1127. "with a 3D Dim";
  1128. }
  1129. }
  1130. return SPV_SUCCESS;
  1131. }
  1132. spv_result_t ValidateImageDrefLod(ValidationState_t& _,
  1133. const Instruction* inst) {
  1134. const SpvOp opcode = inst->opcode();
  1135. uint32_t actual_result_type = 0;
  1136. if (spv_result_t error = GetActualResultType(_, inst, &actual_result_type)) {
  1137. return error;
  1138. }
  1139. if (!_.IsIntScalarType(actual_result_type) &&
  1140. !_.IsFloatScalarType(actual_result_type)) {
  1141. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  1142. << "Expected " << GetActualResultTypeStr(opcode)
  1143. << " to be int or float scalar type";
  1144. }
  1145. const uint32_t image_type = _.GetOperandTypeId(inst, 2);
  1146. if (_.GetIdOpcode(image_type) != SpvOpTypeSampledImage) {
  1147. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  1148. << "Expected Sampled Image to be of type OpTypeSampledImage";
  1149. }
  1150. ImageTypeInfo info;
  1151. if (!GetImageTypeInfo(_, image_type, &info)) {
  1152. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  1153. << "Corrupt image type definition";
  1154. }
  1155. if (spv_result_t result = ValidateImageCommon(_, inst, info)) return result;
  1156. if (info.multisampled) {
  1157. // When using image operands, the Sample image operand is required if and
  1158. // only if the image is multisampled (MS=1). The Sample image operand is
  1159. // only allowed for fetch, read, and write.
  1160. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  1161. << "Dref sampling operation is invalid for multisample image";
  1162. }
  1163. if (actual_result_type != info.sampled_type) {
  1164. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  1165. << "Expected Image 'Sampled Type' to be the same as "
  1166. << GetActualResultTypeStr(opcode);
  1167. }
  1168. const uint32_t coord_type = _.GetOperandTypeId(inst, 3);
  1169. if (!_.IsFloatScalarOrVectorType(coord_type)) {
  1170. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  1171. << "Expected Coordinate to be float scalar or vector";
  1172. }
  1173. const uint32_t min_coord_size = GetMinCoordSize(opcode, info);
  1174. const uint32_t actual_coord_size = _.GetDimension(coord_type);
  1175. if (min_coord_size > actual_coord_size) {
  1176. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  1177. << "Expected Coordinate to have at least " << min_coord_size
  1178. << " components, but given only " << actual_coord_size;
  1179. }
  1180. if (spv_result_t result = ValidateImageDref(_, inst, info)) return result;
  1181. if (spv_result_t result =
  1182. ValidateImageOperands(_, inst, info, /* word_index = */ 7))
  1183. return result;
  1184. return SPV_SUCCESS;
  1185. }
  1186. spv_result_t ValidateImageFetch(ValidationState_t& _, const Instruction* inst) {
  1187. uint32_t actual_result_type = 0;
  1188. if (spv_result_t error = GetActualResultType(_, inst, &actual_result_type)) {
  1189. return error;
  1190. }
  1191. const SpvOp opcode = inst->opcode();
  1192. if (!_.IsIntVectorType(actual_result_type) &&
  1193. !_.IsFloatVectorType(actual_result_type)) {
  1194. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  1195. << "Expected " << GetActualResultTypeStr(opcode)
  1196. << " to be int or float vector type";
  1197. }
  1198. if (_.GetDimension(actual_result_type) != 4) {
  1199. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  1200. << "Expected " << GetActualResultTypeStr(opcode)
  1201. << " to have 4 components";
  1202. }
  1203. const uint32_t image_type = _.GetOperandTypeId(inst, 2);
  1204. if (_.GetIdOpcode(image_type) != SpvOpTypeImage) {
  1205. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  1206. << "Expected Image to be of type OpTypeImage";
  1207. }
  1208. ImageTypeInfo info;
  1209. if (!GetImageTypeInfo(_, image_type, &info)) {
  1210. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  1211. << "Corrupt image type definition";
  1212. }
  1213. if (_.GetIdOpcode(info.sampled_type) != SpvOpTypeVoid) {
  1214. const uint32_t result_component_type =
  1215. _.GetComponentType(actual_result_type);
  1216. if (result_component_type != info.sampled_type) {
  1217. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  1218. << "Expected Image 'Sampled Type' to be the same as "
  1219. << GetActualResultTypeStr(opcode) << " components";
  1220. }
  1221. }
  1222. if (info.dim == SpvDimCube) {
  1223. return _.diag(SPV_ERROR_INVALID_DATA, inst) << "Image 'Dim' cannot be Cube";
  1224. }
  1225. if (info.sampled != 1) {
  1226. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  1227. << "Expected Image 'Sampled' parameter to be 1";
  1228. }
  1229. const uint32_t coord_type = _.GetOperandTypeId(inst, 3);
  1230. if (!_.IsIntScalarOrVectorType(coord_type)) {
  1231. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  1232. << "Expected Coordinate to be int scalar or vector";
  1233. }
  1234. const uint32_t min_coord_size = GetMinCoordSize(opcode, info);
  1235. const uint32_t actual_coord_size = _.GetDimension(coord_type);
  1236. if (min_coord_size > actual_coord_size) {
  1237. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  1238. << "Expected Coordinate to have at least " << min_coord_size
  1239. << " components, but given only " << actual_coord_size;
  1240. }
  1241. if (spv_result_t result =
  1242. ValidateImageOperands(_, inst, info, /* word_index = */ 6))
  1243. return result;
  1244. return SPV_SUCCESS;
  1245. }
  1246. spv_result_t ValidateImageGather(ValidationState_t& _,
  1247. const Instruction* inst) {
  1248. uint32_t actual_result_type = 0;
  1249. if (spv_result_t error = GetActualResultType(_, inst, &actual_result_type))
  1250. return error;
  1251. const SpvOp opcode = inst->opcode();
  1252. if (!_.IsIntVectorType(actual_result_type) &&
  1253. !_.IsFloatVectorType(actual_result_type)) {
  1254. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  1255. << "Expected " << GetActualResultTypeStr(opcode)
  1256. << " to be int or float vector type";
  1257. }
  1258. if (_.GetDimension(actual_result_type) != 4) {
  1259. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  1260. << "Expected " << GetActualResultTypeStr(opcode)
  1261. << " to have 4 components";
  1262. }
  1263. const uint32_t image_type = _.GetOperandTypeId(inst, 2);
  1264. if (_.GetIdOpcode(image_type) != SpvOpTypeSampledImage) {
  1265. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  1266. << "Expected Sampled Image to be of type OpTypeSampledImage";
  1267. }
  1268. ImageTypeInfo info;
  1269. if (!GetImageTypeInfo(_, image_type, &info)) {
  1270. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  1271. << "Corrupt image type definition";
  1272. }
  1273. if (info.multisampled) {
  1274. // When using image operands, the Sample image operand is required if and
  1275. // only if the image is multisampled (MS=1). The Sample image operand is
  1276. // only allowed for fetch, read, and write.
  1277. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  1278. << "Gather operation is invalid for multisample image";
  1279. }
  1280. if (opcode == SpvOpImageDrefGather || opcode == SpvOpImageSparseDrefGather ||
  1281. _.GetIdOpcode(info.sampled_type) != SpvOpTypeVoid) {
  1282. const uint32_t result_component_type =
  1283. _.GetComponentType(actual_result_type);
  1284. if (result_component_type != info.sampled_type) {
  1285. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  1286. << "Expected Image 'Sampled Type' to be the same as "
  1287. << GetActualResultTypeStr(opcode) << " components";
  1288. }
  1289. }
  1290. if (info.dim != SpvDim2D && info.dim != SpvDimCube &&
  1291. info.dim != SpvDimRect) {
  1292. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  1293. << _.VkErrorID(4777)
  1294. << "Expected Image 'Dim' to be 2D, Cube, or Rect";
  1295. }
  1296. const uint32_t coord_type = _.GetOperandTypeId(inst, 3);
  1297. if (!_.IsFloatScalarOrVectorType(coord_type)) {
  1298. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  1299. << "Expected Coordinate to be float scalar or vector";
  1300. }
  1301. const uint32_t min_coord_size = GetMinCoordSize(opcode, info);
  1302. const uint32_t actual_coord_size = _.GetDimension(coord_type);
  1303. if (min_coord_size > actual_coord_size) {
  1304. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  1305. << "Expected Coordinate to have at least " << min_coord_size
  1306. << " components, but given only " << actual_coord_size;
  1307. }
  1308. if (opcode == SpvOpImageGather || opcode == SpvOpImageSparseGather) {
  1309. const uint32_t component = inst->GetOperandAs<uint32_t>(4);
  1310. const uint32_t component_index_type = _.GetTypeId(component);
  1311. if (!_.IsIntScalarType(component_index_type) ||
  1312. _.GetBitWidth(component_index_type) != 32) {
  1313. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  1314. << "Expected Component to be 32-bit int scalar";
  1315. }
  1316. if (spvIsVulkanEnv(_.context()->target_env)) {
  1317. if (!spvOpcodeIsConstant(_.GetIdOpcode(component))) {
  1318. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  1319. << _.VkErrorID(4664)
  1320. << "Expected Component Operand to be a const object for Vulkan "
  1321. "environment";
  1322. }
  1323. }
  1324. } else {
  1325. assert(opcode == SpvOpImageDrefGather ||
  1326. opcode == SpvOpImageSparseDrefGather);
  1327. if (spv_result_t result = ValidateImageDref(_, inst, info)) return result;
  1328. }
  1329. if (spv_result_t result =
  1330. ValidateImageOperands(_, inst, info, /* word_index = */ 7))
  1331. return result;
  1332. return SPV_SUCCESS;
  1333. }
  1334. spv_result_t ValidateImageRead(ValidationState_t& _, const Instruction* inst) {
  1335. const SpvOp opcode = inst->opcode();
  1336. uint32_t actual_result_type = 0;
  1337. if (spv_result_t error = GetActualResultType(_, inst, &actual_result_type)) {
  1338. return error;
  1339. }
  1340. if (!_.IsIntScalarOrVectorType(actual_result_type) &&
  1341. !_.IsFloatScalarOrVectorType(actual_result_type)) {
  1342. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  1343. << "Expected " << GetActualResultTypeStr(opcode)
  1344. << " to be int or float scalar or vector type";
  1345. }
  1346. const auto target_env = _.context()->target_env;
  1347. // Vulkan requires the result to be a 4-element int or float
  1348. // vector.
  1349. if (spvIsVulkanEnv(target_env)) {
  1350. if (_.GetDimension(actual_result_type) != 4) {
  1351. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  1352. << _.VkErrorID(4780) << "Expected "
  1353. << GetActualResultTypeStr(opcode) << " to have 4 components";
  1354. }
  1355. } // Check OpenCL below, after we get the image info.
  1356. const uint32_t image_type = _.GetOperandTypeId(inst, 2);
  1357. if (_.GetIdOpcode(image_type) != SpvOpTypeImage) {
  1358. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  1359. << "Expected Image to be of type OpTypeImage";
  1360. }
  1361. ImageTypeInfo info;
  1362. if (!GetImageTypeInfo(_, image_type, &info)) {
  1363. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  1364. << "Corrupt image type definition";
  1365. }
  1366. if (spvIsOpenCLEnv(target_env)) {
  1367. // In OpenCL, a read from a depth image returns a scalar float. In other
  1368. // cases, the result is always a 4-element vector.
  1369. // https://www.khronos.org/registry/OpenCL/specs/3.0-unified/html/OpenCL_Env.html#_data_format_for_reading_and_writing_images
  1370. // https://www.khronos.org/registry/OpenCL/specs/3.0-unified/html/OpenCL_C.html#image-read-and-write-functions
  1371. // The builtins for reading depth images are:
  1372. // float read_imagef(aQual image2d_depth_t image, int2 coord)
  1373. // float read_imagef(aQual image2d_array_depth_t image, int4 coord)
  1374. if (info.depth) {
  1375. if (!_.IsFloatScalarType(actual_result_type)) {
  1376. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  1377. << "Expected " << GetActualResultTypeStr(opcode)
  1378. << " from a depth image read to result in a scalar float value";
  1379. }
  1380. } else {
  1381. if (_.GetDimension(actual_result_type) != 4) {
  1382. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  1383. << "Expected " << GetActualResultTypeStr(opcode)
  1384. << " to have 4 components";
  1385. }
  1386. }
  1387. }
  1388. if (info.dim == SpvDimSubpassData) {
  1389. if (opcode == SpvOpImageSparseRead) {
  1390. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  1391. << "Image Dim SubpassData cannot be used with ImageSparseRead";
  1392. }
  1393. _.function(inst->function()->id())
  1394. ->RegisterExecutionModelLimitation(
  1395. SpvExecutionModelFragment,
  1396. std::string("Dim SubpassData requires Fragment execution model: ") +
  1397. spvOpcodeString(opcode));
  1398. }
  1399. if (_.GetIdOpcode(info.sampled_type) != SpvOpTypeVoid) {
  1400. const uint32_t result_component_type =
  1401. _.GetComponentType(actual_result_type);
  1402. if (result_component_type != info.sampled_type) {
  1403. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  1404. << "Expected Image 'Sampled Type' to be the same as "
  1405. << GetActualResultTypeStr(opcode) << " components";
  1406. }
  1407. }
  1408. if (spv_result_t result = ValidateImageCommon(_, inst, info)) return result;
  1409. const uint32_t coord_type = _.GetOperandTypeId(inst, 3);
  1410. if (!_.IsIntScalarOrVectorType(coord_type)) {
  1411. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  1412. << "Expected Coordinate to be int scalar or vector";
  1413. }
  1414. const uint32_t min_coord_size = GetMinCoordSize(opcode, info);
  1415. const uint32_t actual_coord_size = _.GetDimension(coord_type);
  1416. if (min_coord_size > actual_coord_size) {
  1417. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  1418. << "Expected Coordinate to have at least " << min_coord_size
  1419. << " components, but given only " << actual_coord_size;
  1420. }
  1421. if (spvIsVulkanEnv(_.context()->target_env)) {
  1422. if (info.format == SpvImageFormatUnknown && info.dim != SpvDimSubpassData &&
  1423. !_.HasCapability(SpvCapabilityStorageImageReadWithoutFormat)) {
  1424. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  1425. << "Capability StorageImageReadWithoutFormat is required to "
  1426. << "read storage image";
  1427. }
  1428. }
  1429. const uint32_t mask = inst->words().size() <= 5 ? 0 : inst->word(5);
  1430. if (mask & SpvImageOperandsConstOffsetMask) {
  1431. if (spvIsOpenCLEnv(_.context()->target_env)) {
  1432. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  1433. << "ConstOffset image operand not allowed "
  1434. << "in the OpenCL environment.";
  1435. }
  1436. }
  1437. if (spv_result_t result =
  1438. ValidateImageOperands(_, inst, info, /* word_index = */ 6))
  1439. return result;
  1440. return SPV_SUCCESS;
  1441. }
  1442. spv_result_t ValidateImageWrite(ValidationState_t& _, const Instruction* inst) {
  1443. const uint32_t image_type = _.GetOperandTypeId(inst, 0);
  1444. if (_.GetIdOpcode(image_type) != SpvOpTypeImage) {
  1445. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  1446. << "Expected Image to be of type OpTypeImage";
  1447. }
  1448. ImageTypeInfo info;
  1449. if (!GetImageTypeInfo(_, image_type, &info)) {
  1450. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  1451. << "Corrupt image type definition";
  1452. }
  1453. if (info.dim == SpvDimSubpassData) {
  1454. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  1455. << "Image 'Dim' cannot be SubpassData";
  1456. }
  1457. if (spv_result_t result = ValidateImageCommon(_, inst, info)) return result;
  1458. const uint32_t coord_type = _.GetOperandTypeId(inst, 1);
  1459. if (!_.IsIntScalarOrVectorType(coord_type)) {
  1460. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  1461. << "Expected Coordinate to be int scalar or vector";
  1462. }
  1463. const uint32_t min_coord_size = GetMinCoordSize(inst->opcode(), info);
  1464. const uint32_t actual_coord_size = _.GetDimension(coord_type);
  1465. if (min_coord_size > actual_coord_size) {
  1466. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  1467. << "Expected Coordinate to have at least " << min_coord_size
  1468. << " components, but given only " << actual_coord_size;
  1469. }
  1470. // TODO([email protected]) The spec doesn't explicitly say what the type
  1471. // of texel should be.
  1472. const uint32_t texel_type = _.GetOperandTypeId(inst, 2);
  1473. if (!_.IsIntScalarOrVectorType(texel_type) &&
  1474. !_.IsFloatScalarOrVectorType(texel_type)) {
  1475. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  1476. << "Expected Texel to be int or float vector or scalar";
  1477. }
  1478. #if 0
  1479. // TODO: See above.
  1480. if (_.GetDimension(texel_type) != 4) {
  1481. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  1482. << "Expected Texel to have 4 components";
  1483. }
  1484. #endif
  1485. if (_.GetIdOpcode(info.sampled_type) != SpvOpTypeVoid) {
  1486. const uint32_t texel_component_type = _.GetComponentType(texel_type);
  1487. if (texel_component_type != info.sampled_type) {
  1488. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  1489. << "Expected Image 'Sampled Type' to be the same as Texel "
  1490. << "components";
  1491. }
  1492. }
  1493. if (spvIsVulkanEnv(_.context()->target_env)) {
  1494. if (info.format == SpvImageFormatUnknown && info.dim != SpvDimSubpassData &&
  1495. !_.HasCapability(SpvCapabilityStorageImageWriteWithoutFormat)) {
  1496. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  1497. << "Capability StorageImageWriteWithoutFormat is required to "
  1498. "write "
  1499. << "to storage image";
  1500. }
  1501. }
  1502. if (inst->words().size() > 4) {
  1503. if (spvIsOpenCLEnv(_.context()->target_env)) {
  1504. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  1505. << "Optional Image Operands are not allowed in the OpenCL "
  1506. << "environment.";
  1507. }
  1508. }
  1509. if (spv_result_t result =
  1510. ValidateImageOperands(_, inst, info, /* word_index = */ 5))
  1511. return result;
  1512. return SPV_SUCCESS;
  1513. }
  1514. spv_result_t ValidateImage(ValidationState_t& _, const Instruction* inst) {
  1515. const uint32_t result_type = inst->type_id();
  1516. if (_.GetIdOpcode(result_type) != SpvOpTypeImage) {
  1517. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  1518. << "Expected Result Type to be OpTypeImage";
  1519. }
  1520. const uint32_t sampled_image_type = _.GetOperandTypeId(inst, 2);
  1521. const Instruction* sampled_image_type_inst = _.FindDef(sampled_image_type);
  1522. assert(sampled_image_type_inst);
  1523. if (sampled_image_type_inst->opcode() != SpvOpTypeSampledImage) {
  1524. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  1525. << "Expected Sample Image to be of type OpTypeSampleImage";
  1526. }
  1527. if (sampled_image_type_inst->word(2) != result_type) {
  1528. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  1529. << "Expected Sample Image image type to be equal to Result Type";
  1530. }
  1531. return SPV_SUCCESS;
  1532. }
  1533. spv_result_t ValidateImageQuerySizeLod(ValidationState_t& _,
  1534. const Instruction* inst) {
  1535. const uint32_t result_type = inst->type_id();
  1536. if (!_.IsIntScalarOrVectorType(result_type)) {
  1537. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  1538. << "Expected Result Type to be int scalar or vector type";
  1539. }
  1540. const uint32_t image_type = _.GetOperandTypeId(inst, 2);
  1541. if (_.GetIdOpcode(image_type) != SpvOpTypeImage) {
  1542. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  1543. << "Expected Image to be of type OpTypeImage";
  1544. }
  1545. ImageTypeInfo info;
  1546. if (!GetImageTypeInfo(_, image_type, &info)) {
  1547. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  1548. << "Corrupt image type definition";
  1549. }
  1550. uint32_t expected_num_components = info.arrayed;
  1551. switch (info.dim) {
  1552. case SpvDim1D:
  1553. expected_num_components += 1;
  1554. break;
  1555. case SpvDim2D:
  1556. case SpvDimCube:
  1557. expected_num_components += 2;
  1558. break;
  1559. case SpvDim3D:
  1560. expected_num_components += 3;
  1561. break;
  1562. default:
  1563. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  1564. << "Image 'Dim' must be 1D, 2D, 3D or Cube";
  1565. }
  1566. if (info.multisampled != 0) {
  1567. return _.diag(SPV_ERROR_INVALID_DATA, inst) << "Image 'MS' must be 0";
  1568. }
  1569. const auto target_env = _.context()->target_env;
  1570. if (spvIsVulkanEnv(target_env)) {
  1571. if (info.sampled != 1) {
  1572. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  1573. << _.VkErrorID(4659)
  1574. << "OpImageQuerySizeLod must only consume an \"Image\" operand "
  1575. "whose type has its \"Sampled\" operand set to 1";
  1576. }
  1577. }
  1578. uint32_t result_num_components = _.GetDimension(result_type);
  1579. if (result_num_components != expected_num_components) {
  1580. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  1581. << "Result Type has " << result_num_components << " components, "
  1582. << "but " << expected_num_components << " expected";
  1583. }
  1584. const uint32_t lod_type = _.GetOperandTypeId(inst, 3);
  1585. if (!_.IsIntScalarType(lod_type)) {
  1586. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  1587. << "Expected Level of Detail to be int scalar";
  1588. }
  1589. return SPV_SUCCESS;
  1590. }
  1591. spv_result_t ValidateImageQuerySize(ValidationState_t& _,
  1592. const Instruction* inst) {
  1593. const uint32_t result_type = inst->type_id();
  1594. if (!_.IsIntScalarOrVectorType(result_type)) {
  1595. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  1596. << "Expected Result Type to be int scalar or vector type";
  1597. }
  1598. const uint32_t image_type = _.GetOperandTypeId(inst, 2);
  1599. if (_.GetIdOpcode(image_type) != SpvOpTypeImage) {
  1600. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  1601. << "Expected Image to be of type OpTypeImage";
  1602. }
  1603. ImageTypeInfo info;
  1604. if (!GetImageTypeInfo(_, image_type, &info)) {
  1605. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  1606. << "Corrupt image type definition";
  1607. }
  1608. uint32_t expected_num_components = info.arrayed;
  1609. switch (info.dim) {
  1610. case SpvDim1D:
  1611. case SpvDimBuffer:
  1612. expected_num_components += 1;
  1613. break;
  1614. case SpvDim2D:
  1615. case SpvDimCube:
  1616. case SpvDimRect:
  1617. expected_num_components += 2;
  1618. break;
  1619. case SpvDim3D:
  1620. expected_num_components += 3;
  1621. break;
  1622. default:
  1623. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  1624. << "Image 'Dim' must be 1D, Buffer, 2D, Cube, 3D or Rect";
  1625. }
  1626. if (info.dim == SpvDim1D || info.dim == SpvDim2D || info.dim == SpvDim3D ||
  1627. info.dim == SpvDimCube) {
  1628. if (info.multisampled != 1 && info.sampled != 0 && info.sampled != 2) {
  1629. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  1630. << "Image must have either 'MS'=1 or 'Sampled'=0 or 'Sampled'=2";
  1631. }
  1632. }
  1633. uint32_t result_num_components = _.GetDimension(result_type);
  1634. if (result_num_components != expected_num_components) {
  1635. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  1636. << "Result Type has " << result_num_components << " components, "
  1637. << "but " << expected_num_components << " expected";
  1638. }
  1639. return SPV_SUCCESS;
  1640. }
  1641. spv_result_t ValidateImageQueryFormatOrOrder(ValidationState_t& _,
  1642. const Instruction* inst) {
  1643. if (!_.IsIntScalarType(inst->type_id())) {
  1644. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  1645. << "Expected Result Type to be int scalar type";
  1646. }
  1647. if (_.GetIdOpcode(_.GetOperandTypeId(inst, 2)) != SpvOpTypeImage) {
  1648. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  1649. << "Expected operand to be of type OpTypeImage";
  1650. }
  1651. return SPV_SUCCESS;
  1652. }
  1653. spv_result_t ValidateImageQueryLod(ValidationState_t& _,
  1654. const Instruction* inst) {
  1655. _.function(inst->function()->id())
  1656. ->RegisterExecutionModelLimitation(
  1657. [&](SpvExecutionModel model, std::string* message) {
  1658. if (model != SpvExecutionModelFragment &&
  1659. model != SpvExecutionModelGLCompute) {
  1660. if (message) {
  1661. *message = std::string(
  1662. "OpImageQueryLod requires Fragment or GLCompute execution "
  1663. "model");
  1664. }
  1665. return false;
  1666. }
  1667. return true;
  1668. });
  1669. _.function(inst->function()->id())
  1670. ->RegisterLimitation([](const ValidationState_t& state,
  1671. const Function* entry_point,
  1672. std::string* message) {
  1673. const auto* models = state.GetExecutionModels(entry_point->id());
  1674. const auto* modes = state.GetExecutionModes(entry_point->id());
  1675. if (models->find(SpvExecutionModelGLCompute) != models->end() &&
  1676. modes->find(SpvExecutionModeDerivativeGroupLinearNV) ==
  1677. modes->end() &&
  1678. modes->find(SpvExecutionModeDerivativeGroupQuadsNV) ==
  1679. modes->end()) {
  1680. if (message) {
  1681. *message = std::string(
  1682. "OpImageQueryLod requires DerivativeGroupQuadsNV "
  1683. "or DerivativeGroupLinearNV execution mode for GLCompute "
  1684. "execution model");
  1685. }
  1686. return false;
  1687. }
  1688. return true;
  1689. });
  1690. const uint32_t result_type = inst->type_id();
  1691. if (!_.IsFloatVectorType(result_type)) {
  1692. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  1693. << "Expected Result Type to be float vector type";
  1694. }
  1695. if (_.GetDimension(result_type) != 2) {
  1696. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  1697. << "Expected Result Type to have 2 components";
  1698. }
  1699. const uint32_t image_type = _.GetOperandTypeId(inst, 2);
  1700. if (_.GetIdOpcode(image_type) != SpvOpTypeSampledImage) {
  1701. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  1702. << "Expected Image operand to be of type OpTypeSampledImage";
  1703. }
  1704. ImageTypeInfo info;
  1705. if (!GetImageTypeInfo(_, image_type, &info)) {
  1706. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  1707. << "Corrupt image type definition";
  1708. }
  1709. if (info.dim != SpvDim1D && info.dim != SpvDim2D && info.dim != SpvDim3D &&
  1710. info.dim != SpvDimCube) {
  1711. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  1712. << "Image 'Dim' must be 1D, 2D, 3D or Cube";
  1713. }
  1714. const uint32_t coord_type = _.GetOperandTypeId(inst, 3);
  1715. if (_.HasCapability(SpvCapabilityKernel)) {
  1716. if (!_.IsFloatScalarOrVectorType(coord_type) &&
  1717. !_.IsIntScalarOrVectorType(coord_type)) {
  1718. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  1719. << "Expected Coordinate to be int or float scalar or vector";
  1720. }
  1721. } else {
  1722. if (!_.IsFloatScalarOrVectorType(coord_type)) {
  1723. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  1724. << "Expected Coordinate to be float scalar or vector";
  1725. }
  1726. }
  1727. const uint32_t min_coord_size = GetPlaneCoordSize(info);
  1728. const uint32_t actual_coord_size = _.GetDimension(coord_type);
  1729. if (min_coord_size > actual_coord_size) {
  1730. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  1731. << "Expected Coordinate to have at least " << min_coord_size
  1732. << " components, but given only " << actual_coord_size;
  1733. }
  1734. // The operad is a sampled image.
  1735. // The sampled image type is already checked to be parameterized by an image
  1736. // type with Sampled=0 or Sampled=1. Vulkan bans Sampled=0, and so we have
  1737. // Sampled=1. So the validator already enforces Vulkan VUID 4659:
  1738. // OpImageQuerySizeLod must only consume an “Image” operand whose type has
  1739. // its "Sampled" operand set to 1
  1740. return SPV_SUCCESS;
  1741. }
  1742. spv_result_t ValidateImageSparseLod(ValidationState_t& _,
  1743. const Instruction* inst) {
  1744. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  1745. << "Instruction reserved for future use, use of this instruction "
  1746. << "is invalid";
  1747. }
  1748. spv_result_t ValidateImageQueryLevelsOrSamples(ValidationState_t& _,
  1749. const Instruction* inst) {
  1750. if (!_.IsIntScalarType(inst->type_id())) {
  1751. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  1752. << "Expected Result Type to be int scalar type";
  1753. }
  1754. const uint32_t image_type = _.GetOperandTypeId(inst, 2);
  1755. if (_.GetIdOpcode(image_type) != SpvOpTypeImage) {
  1756. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  1757. << "Expected Image to be of type OpTypeImage";
  1758. }
  1759. ImageTypeInfo info;
  1760. if (!GetImageTypeInfo(_, image_type, &info)) {
  1761. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  1762. << "Corrupt image type definition";
  1763. }
  1764. const SpvOp opcode = inst->opcode();
  1765. if (opcode == SpvOpImageQueryLevels) {
  1766. if (info.dim != SpvDim1D && info.dim != SpvDim2D && info.dim != SpvDim3D &&
  1767. info.dim != SpvDimCube) {
  1768. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  1769. << "Image 'Dim' must be 1D, 2D, 3D or Cube";
  1770. }
  1771. const auto target_env = _.context()->target_env;
  1772. if (spvIsVulkanEnv(target_env)) {
  1773. if (info.sampled != 1) {
  1774. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  1775. << _.VkErrorID(4659)
  1776. << "OpImageQueryLevels must only consume an \"Image\" operand "
  1777. "whose type has its \"Sampled\" operand set to 1";
  1778. }
  1779. }
  1780. } else {
  1781. assert(opcode == SpvOpImageQuerySamples);
  1782. if (info.dim != SpvDim2D) {
  1783. return _.diag(SPV_ERROR_INVALID_DATA, inst) << "Image 'Dim' must be 2D";
  1784. }
  1785. if (info.multisampled != 1) {
  1786. return _.diag(SPV_ERROR_INVALID_DATA, inst) << "Image 'MS' must be 1";
  1787. }
  1788. }
  1789. return SPV_SUCCESS;
  1790. }
  1791. spv_result_t ValidateImageSparseTexelsResident(ValidationState_t& _,
  1792. const Instruction* inst) {
  1793. if (!_.IsBoolScalarType(inst->type_id())) {
  1794. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  1795. << "Expected Result Type to be bool scalar type";
  1796. }
  1797. const uint32_t resident_code_type = _.GetOperandTypeId(inst, 2);
  1798. if (!_.IsIntScalarType(resident_code_type)) {
  1799. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  1800. << "Expected Resident Code to be int scalar";
  1801. }
  1802. return SPV_SUCCESS;
  1803. }
  1804. } // namespace
  1805. // Validates correctness of image instructions.
  1806. spv_result_t ImagePass(ValidationState_t& _, const Instruction* inst) {
  1807. const SpvOp opcode = inst->opcode();
  1808. if (IsImplicitLod(opcode)) {
  1809. _.function(inst->function()->id())
  1810. ->RegisterExecutionModelLimitation([opcode](SpvExecutionModel model,
  1811. std::string* message) {
  1812. if (model != SpvExecutionModelFragment &&
  1813. model != SpvExecutionModelGLCompute) {
  1814. if (message) {
  1815. *message =
  1816. std::string(
  1817. "ImplicitLod instructions require Fragment or GLCompute "
  1818. "execution model: ") +
  1819. spvOpcodeString(opcode);
  1820. }
  1821. return false;
  1822. }
  1823. return true;
  1824. });
  1825. _.function(inst->function()->id())
  1826. ->RegisterLimitation([opcode](const ValidationState_t& state,
  1827. const Function* entry_point,
  1828. std::string* message) {
  1829. const auto* models = state.GetExecutionModels(entry_point->id());
  1830. const auto* modes = state.GetExecutionModes(entry_point->id());
  1831. if (models &&
  1832. models->find(SpvExecutionModelGLCompute) != models->end() &&
  1833. (!modes ||
  1834. (modes->find(SpvExecutionModeDerivativeGroupLinearNV) ==
  1835. modes->end() &&
  1836. modes->find(SpvExecutionModeDerivativeGroupQuadsNV) ==
  1837. modes->end()))) {
  1838. if (message) {
  1839. *message =
  1840. std::string(
  1841. "ImplicitLod instructions require DerivativeGroupQuadsNV "
  1842. "or DerivativeGroupLinearNV execution mode for GLCompute "
  1843. "execution model: ") +
  1844. spvOpcodeString(opcode);
  1845. }
  1846. return false;
  1847. }
  1848. return true;
  1849. });
  1850. }
  1851. switch (opcode) {
  1852. case SpvOpTypeImage:
  1853. return ValidateTypeImage(_, inst);
  1854. case SpvOpTypeSampledImage:
  1855. return ValidateTypeSampledImage(_, inst);
  1856. case SpvOpSampledImage:
  1857. return ValidateSampledImage(_, inst);
  1858. case SpvOpImageTexelPointer:
  1859. return ValidateImageTexelPointer(_, inst);
  1860. case SpvOpImageSampleImplicitLod:
  1861. case SpvOpImageSampleExplicitLod:
  1862. case SpvOpImageSampleProjImplicitLod:
  1863. case SpvOpImageSampleProjExplicitLod:
  1864. case SpvOpImageSparseSampleImplicitLod:
  1865. case SpvOpImageSparseSampleExplicitLod:
  1866. return ValidateImageLod(_, inst);
  1867. case SpvOpImageSampleDrefImplicitLod:
  1868. case SpvOpImageSampleDrefExplicitLod:
  1869. case SpvOpImageSampleProjDrefImplicitLod:
  1870. case SpvOpImageSampleProjDrefExplicitLod:
  1871. case SpvOpImageSparseSampleDrefImplicitLod:
  1872. case SpvOpImageSparseSampleDrefExplicitLod:
  1873. return ValidateImageDrefLod(_, inst);
  1874. case SpvOpImageFetch:
  1875. case SpvOpImageSparseFetch:
  1876. return ValidateImageFetch(_, inst);
  1877. case SpvOpImageGather:
  1878. case SpvOpImageDrefGather:
  1879. case SpvOpImageSparseGather:
  1880. case SpvOpImageSparseDrefGather:
  1881. return ValidateImageGather(_, inst);
  1882. case SpvOpImageRead:
  1883. case SpvOpImageSparseRead:
  1884. return ValidateImageRead(_, inst);
  1885. case SpvOpImageWrite:
  1886. return ValidateImageWrite(_, inst);
  1887. case SpvOpImage:
  1888. return ValidateImage(_, inst);
  1889. case SpvOpImageQueryFormat:
  1890. case SpvOpImageQueryOrder:
  1891. return ValidateImageQueryFormatOrOrder(_, inst);
  1892. case SpvOpImageQuerySizeLod:
  1893. return ValidateImageQuerySizeLod(_, inst);
  1894. case SpvOpImageQuerySize:
  1895. return ValidateImageQuerySize(_, inst);
  1896. case SpvOpImageQueryLod:
  1897. return ValidateImageQueryLod(_, inst);
  1898. case SpvOpImageQueryLevels:
  1899. case SpvOpImageQuerySamples:
  1900. return ValidateImageQueryLevelsOrSamples(_, inst);
  1901. case SpvOpImageSparseSampleProjImplicitLod:
  1902. case SpvOpImageSparseSampleProjExplicitLod:
  1903. case SpvOpImageSparseSampleProjDrefImplicitLod:
  1904. case SpvOpImageSparseSampleProjDrefExplicitLod:
  1905. return ValidateImageSparseLod(_, inst);
  1906. case SpvOpImageSparseTexelsResident:
  1907. return ValidateImageSparseTexelsResident(_, inst);
  1908. default:
  1909. break;
  1910. }
  1911. return SPV_SUCCESS;
  1912. }
  1913. } // namespace val
  1914. } // namespace spvtools