validate_image.cpp 69 KB

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