validate_image.cpp 76 KB

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