2
0

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