validate_image.cpp 59 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670
  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 "validate.h"
  16. #include "diagnostic.h"
  17. #include "opcode.h"
  18. #include "spirv_target_env.h"
  19. #include "util/bitutils.h"
  20. #include "val/instruction.h"
  21. #include "val/validation_state.h"
  22. namespace libspirv {
  23. namespace {
  24. // Performs compile time check that all SpvImageOperandsXXX cases are handled in
  25. // this module. If SpvImageOperandsXXX list changes, this function will fail the
  26. // build.
  27. // For all other purposes this is a dummy function.
  28. bool CheckAllImageOperandsHandled() {
  29. SpvImageOperandsMask enum_val = SpvImageOperandsBiasMask;
  30. // Some improvised code to prevent the compiler from considering enum_val
  31. // constant and optimizing the switch away.
  32. uint32_t stack_var = 0;
  33. if (reinterpret_cast<uintptr_t>(&stack_var) % 256)
  34. enum_val = SpvImageOperandsLodMask;
  35. switch (enum_val) {
  36. // Please update the validation rules in this module if you are changing
  37. // the list of image operands, and add new enum values to this switch.
  38. case SpvImageOperandsMaskNone:
  39. return false;
  40. case SpvImageOperandsBiasMask:
  41. case SpvImageOperandsLodMask:
  42. case SpvImageOperandsGradMask:
  43. case SpvImageOperandsConstOffsetMask:
  44. case SpvImageOperandsOffsetMask:
  45. case SpvImageOperandsConstOffsetsMask:
  46. case SpvImageOperandsSampleMask:
  47. case SpvImageOperandsMinLodMask:
  48. return true;
  49. }
  50. return false;
  51. }
  52. // Used by GetImageTypeInfo. See OpTypeImage spec for more information.
  53. struct ImageTypeInfo {
  54. uint32_t sampled_type = 0;
  55. SpvDim dim = SpvDimMax;
  56. uint32_t depth = 0;
  57. uint32_t arrayed = 0;
  58. uint32_t multisampled = 0;
  59. uint32_t sampled = 0;
  60. SpvImageFormat format = SpvImageFormatMax;
  61. SpvAccessQualifier access_qualifier = SpvAccessQualifierMax;
  62. };
  63. // Provides information on image type. |id| should be object of either
  64. // OpTypeImage or OpTypeSampledImage type. Returns false in case of failure
  65. // (not a valid id, failed to parse the instruction, etc).
  66. bool GetImageTypeInfo(const ValidationState_t& _, uint32_t id,
  67. ImageTypeInfo* info) {
  68. if (!id || !info) return false;
  69. const Instruction* inst = _.FindDef(id);
  70. assert(inst);
  71. if (inst->opcode() == SpvOpTypeSampledImage) {
  72. inst = _.FindDef(inst->word(2));
  73. assert(inst);
  74. }
  75. if (inst->opcode() != SpvOpTypeImage) return false;
  76. const size_t num_words = inst->words().size();
  77. if (num_words != 9 && num_words != 10) return false;
  78. info->sampled_type = inst->word(2);
  79. info->dim = static_cast<SpvDim>(inst->word(3));
  80. info->depth = inst->word(4);
  81. info->arrayed = inst->word(5);
  82. info->multisampled = inst->word(6);
  83. info->sampled = inst->word(7);
  84. info->format = static_cast<SpvImageFormat>(inst->word(8));
  85. info->access_qualifier = num_words < 10
  86. ? SpvAccessQualifierMax
  87. : static_cast<SpvAccessQualifier>(inst->word(9));
  88. return true;
  89. }
  90. bool IsImplicitLod(SpvOp opcode) {
  91. switch (opcode) {
  92. case SpvOpImageSampleImplicitLod:
  93. case SpvOpImageSampleDrefImplicitLod:
  94. case SpvOpImageSampleProjImplicitLod:
  95. case SpvOpImageSampleProjDrefImplicitLod:
  96. case SpvOpImageSparseSampleImplicitLod:
  97. case SpvOpImageSparseSampleDrefImplicitLod:
  98. case SpvOpImageSparseSampleProjImplicitLod:
  99. case SpvOpImageSparseSampleProjDrefImplicitLod:
  100. return true;
  101. default:
  102. break;
  103. };
  104. return false;
  105. }
  106. bool IsExplicitLod(SpvOp opcode) {
  107. switch (opcode) {
  108. case SpvOpImageSampleExplicitLod:
  109. case SpvOpImageSampleDrefExplicitLod:
  110. case SpvOpImageSampleProjExplicitLod:
  111. case SpvOpImageSampleProjDrefExplicitLod:
  112. case SpvOpImageSparseSampleExplicitLod:
  113. case SpvOpImageSparseSampleDrefExplicitLod:
  114. case SpvOpImageSparseSampleProjExplicitLod:
  115. case SpvOpImageSparseSampleProjDrefExplicitLod:
  116. return true;
  117. default:
  118. break;
  119. };
  120. return false;
  121. }
  122. // Returns true if the opcode is a Image instruction which applies
  123. // homogenous projection to the coordinates.
  124. bool IsProj(SpvOp opcode) {
  125. switch (opcode) {
  126. case SpvOpImageSampleProjImplicitLod:
  127. case SpvOpImageSampleProjDrefImplicitLod:
  128. case SpvOpImageSparseSampleProjImplicitLod:
  129. case SpvOpImageSparseSampleProjDrefImplicitLod:
  130. case SpvOpImageSampleProjExplicitLod:
  131. case SpvOpImageSampleProjDrefExplicitLod:
  132. case SpvOpImageSparseSampleProjExplicitLod:
  133. case SpvOpImageSparseSampleProjDrefExplicitLod:
  134. return true;
  135. default:
  136. break;
  137. };
  138. return false;
  139. }
  140. // Returns the number of components in a coordinate used to access a texel in
  141. // a single plane of an image with the given parameters.
  142. uint32_t GetPlaneCoordSize(const ImageTypeInfo& info) {
  143. uint32_t plane_size = 0;
  144. // If this switch breaks your build, please add new values below.
  145. switch (info.dim) {
  146. case SpvDim1D:
  147. case SpvDimBuffer:
  148. plane_size = 1;
  149. break;
  150. case SpvDim2D:
  151. case SpvDimRect:
  152. case SpvDimSubpassData:
  153. plane_size = 2;
  154. break;
  155. case SpvDim3D:
  156. case SpvDimCube:
  157. // For Cube direction vector is used instead of UV.
  158. plane_size = 3;
  159. break;
  160. case SpvDimMax:
  161. assert(0);
  162. break;
  163. }
  164. return plane_size;
  165. }
  166. // Returns minimal number of coordinates based on image dim, arrayed and whether
  167. // the instruction uses projection coordinates.
  168. uint32_t GetMinCoordSize(SpvOp opcode, const ImageTypeInfo& info) {
  169. if (info.dim == SpvDimCube &&
  170. (opcode == SpvOpImageRead || opcode == SpvOpImageWrite ||
  171. opcode == SpvOpImageSparseRead)) {
  172. // These opcodes use UV for Cube, not direction vector.
  173. return 3;
  174. }
  175. return GetPlaneCoordSize(info) + info.arrayed + (IsProj(opcode) ? 1 : 0);
  176. }
  177. // Checks ImageOperand bitfield and respective operands.
  178. spv_result_t ValidateImageOperands(ValidationState_t& _,
  179. const spv_parsed_instruction_t& inst,
  180. const ImageTypeInfo& info, uint32_t mask,
  181. uint32_t word_index) {
  182. static const bool kAllImageOperandsHandled = CheckAllImageOperandsHandled();
  183. (void)kAllImageOperandsHandled;
  184. const SpvOp opcode = static_cast<SpvOp>(inst.opcode);
  185. const uint32_t num_words = inst.num_words;
  186. size_t expected_num_image_operand_words = spvutils::CountSetBits(mask);
  187. if (mask & SpvImageOperandsGradMask) {
  188. // Grad uses two words.
  189. ++expected_num_image_operand_words;
  190. }
  191. if (expected_num_image_operand_words != num_words - word_index) {
  192. return _.diag(SPV_ERROR_INVALID_DATA)
  193. << "Number of image operand ids doesn't correspond to the bit mask: "
  194. << spvOpcodeString(opcode);
  195. }
  196. if (spvutils::CountSetBits(mask & (SpvImageOperandsOffsetMask |
  197. SpvImageOperandsConstOffsetMask |
  198. SpvImageOperandsConstOffsetsMask)) > 1) {
  199. return _.diag(SPV_ERROR_INVALID_DATA)
  200. << "Image Operands Offset, ConstOffset, ConstOffsets cannot be used "
  201. << "together: " << spvOpcodeString(opcode);
  202. };
  203. const bool is_implicit_lod = IsImplicitLod(opcode);
  204. const bool is_explicit_lod = IsExplicitLod(opcode);
  205. // The checks should be done in the order of definition of OperandImage.
  206. if (mask & SpvImageOperandsBiasMask) {
  207. if (!is_implicit_lod) {
  208. return _.diag(SPV_ERROR_INVALID_DATA)
  209. << "Image Operand Bias can only be used with ImplicitLod opcodes: "
  210. << spvOpcodeString(opcode);
  211. };
  212. const uint32_t type_id = _.GetTypeId(inst.words[word_index++]);
  213. if (!_.IsFloatScalarType(type_id)) {
  214. return _.diag(SPV_ERROR_INVALID_DATA)
  215. << "Expected Image Operand Bias to be float scalar: "
  216. << spvOpcodeString(opcode);
  217. }
  218. if (info.dim != SpvDim1D && info.dim != SpvDim2D && info.dim != SpvDim3D &&
  219. info.dim != SpvDimCube) {
  220. return _.diag(SPV_ERROR_INVALID_DATA)
  221. << "Image Operand Bias requires 'Dim' parameter to be 1D, 2D, 3D "
  222. "or "
  223. << "Cube: " << spvOpcodeString(opcode);
  224. }
  225. if (info.multisampled != 0) {
  226. return _.diag(SPV_ERROR_INVALID_DATA)
  227. << "Image Operand Bias requires 'MS' parameter to be 0: "
  228. << spvOpcodeString(opcode);
  229. }
  230. }
  231. if (mask & SpvImageOperandsLodMask) {
  232. if (!is_explicit_lod && opcode != SpvOpImageFetch &&
  233. opcode != SpvOpImageSparseFetch) {
  234. return _.diag(SPV_ERROR_INVALID_DATA)
  235. << "Image Operand Lod can only be used with ExplicitLod opcodes "
  236. << "and OpImageFetch: " << spvOpcodeString(opcode);
  237. };
  238. if (mask & SpvImageOperandsGradMask) {
  239. return _.diag(SPV_ERROR_INVALID_DATA)
  240. << "Image Operand bits Lod and Grad cannot be set at the same "
  241. "time: "
  242. << spvOpcodeString(opcode);
  243. }
  244. const uint32_t type_id = _.GetTypeId(inst.words[word_index++]);
  245. if (is_explicit_lod) {
  246. if (!_.IsFloatScalarType(type_id)) {
  247. return _.diag(SPV_ERROR_INVALID_DATA)
  248. << "Expected Image Operand Lod to be float scalar when used "
  249. << "with ExplicitLod: " << spvOpcodeString(opcode);
  250. }
  251. } else {
  252. if (!_.IsIntScalarType(type_id)) {
  253. return _.diag(SPV_ERROR_INVALID_DATA)
  254. << "Expected Image Operand Lod to be int scalar when used with "
  255. << "OpImageFetch";
  256. }
  257. }
  258. if (info.dim != SpvDim1D && info.dim != SpvDim2D && info.dim != SpvDim3D &&
  259. info.dim != SpvDimCube) {
  260. return _.diag(SPV_ERROR_INVALID_DATA)
  261. << "Image Operand Lod requires 'Dim' parameter to be 1D, 2D, 3D "
  262. "or "
  263. << "Cube: " << spvOpcodeString(opcode);
  264. }
  265. if (info.multisampled != 0) {
  266. return _.diag(SPV_ERROR_INVALID_DATA)
  267. << "Image Operand Lod requires 'MS' parameter to be 0: "
  268. << spvOpcodeString(opcode);
  269. }
  270. }
  271. if (mask & SpvImageOperandsGradMask) {
  272. if (!is_explicit_lod) {
  273. return _.diag(SPV_ERROR_INVALID_DATA)
  274. << "Image Operand Grad can only be used with ExplicitLod opcodes: "
  275. << spvOpcodeString(opcode);
  276. };
  277. const uint32_t dx_type_id = _.GetTypeId(inst.words[word_index++]);
  278. const uint32_t dy_type_id = _.GetTypeId(inst.words[word_index++]);
  279. if (!_.IsFloatScalarOrVectorType(dx_type_id) ||
  280. !_.IsFloatScalarOrVectorType(dy_type_id)) {
  281. return _.diag(SPV_ERROR_INVALID_DATA)
  282. << "Expected both Image Operand Grad ids to be float scalars or "
  283. << "vectors: " << spvOpcodeString(opcode);
  284. }
  285. const uint32_t plane_size = GetPlaneCoordSize(info);
  286. const uint32_t dx_size = _.GetDimension(dx_type_id);
  287. const uint32_t dy_size = _.GetDimension(dy_type_id);
  288. if (plane_size != dx_size) {
  289. return _.diag(SPV_ERROR_INVALID_DATA)
  290. << "Expected Image Operand Grad dx to have " << plane_size
  291. << " components, but given " << dx_size << ": "
  292. << spvOpcodeString(opcode);
  293. }
  294. if (plane_size != dy_size) {
  295. return _.diag(SPV_ERROR_INVALID_DATA)
  296. << "Expected Image Operand Grad dy to have " << plane_size
  297. << " components, but given " << dy_size << ": "
  298. << spvOpcodeString(opcode);
  299. }
  300. if (info.multisampled != 0) {
  301. return _.diag(SPV_ERROR_INVALID_DATA)
  302. << "Image Operand Grad requires 'MS' parameter to be 0: "
  303. << spvOpcodeString(opcode);
  304. }
  305. }
  306. if (mask & SpvImageOperandsConstOffsetMask) {
  307. if (info.dim == SpvDimCube) {
  308. return _.diag(SPV_ERROR_INVALID_DATA)
  309. << "Image Operand ConstOffset cannot be used with Cube Image "
  310. "'Dim': "
  311. << spvOpcodeString(opcode);
  312. }
  313. const uint32_t id = inst.words[word_index++];
  314. const uint32_t type_id = _.GetTypeId(id);
  315. if (!_.IsIntScalarOrVectorType(type_id)) {
  316. return _.diag(SPV_ERROR_INVALID_DATA)
  317. << "Expected Image Operand ConstOffset to be int scalar or "
  318. << "vector: " << spvOpcodeString(opcode);
  319. }
  320. if (!spvOpcodeIsConstant(_.GetIdOpcode(id))) {
  321. return _.diag(SPV_ERROR_INVALID_DATA)
  322. << "Expected Image Operand ConstOffset to be a const object: "
  323. << spvOpcodeString(opcode);
  324. }
  325. const uint32_t plane_size = GetPlaneCoordSize(info);
  326. const uint32_t offset_size = _.GetDimension(type_id);
  327. if (plane_size != offset_size) {
  328. return _.diag(SPV_ERROR_INVALID_DATA)
  329. << "Expected Image Operand ConstOffset to have " << plane_size
  330. << " components, but given " << offset_size << ": "
  331. << spvOpcodeString(opcode);
  332. }
  333. }
  334. if (mask & SpvImageOperandsOffsetMask) {
  335. if (info.dim == SpvDimCube) {
  336. return _.diag(SPV_ERROR_INVALID_DATA)
  337. << "Image Operand Offset cannot be used with Cube Image 'Dim': "
  338. << spvOpcodeString(opcode);
  339. }
  340. const uint32_t id = inst.words[word_index++];
  341. const uint32_t type_id = _.GetTypeId(id);
  342. if (!_.IsIntScalarOrVectorType(type_id)) {
  343. return _.diag(SPV_ERROR_INVALID_DATA)
  344. << "Expected Image Operand Offset to be int scalar or "
  345. << "vector: " << spvOpcodeString(opcode);
  346. }
  347. const uint32_t plane_size = GetPlaneCoordSize(info);
  348. const uint32_t offset_size = _.GetDimension(type_id);
  349. if (plane_size != offset_size) {
  350. return _.diag(SPV_ERROR_INVALID_DATA)
  351. << "Expected Image Operand Offset to have " << plane_size
  352. << " components, but given " << offset_size << ": "
  353. << spvOpcodeString(opcode);
  354. }
  355. }
  356. if (mask & SpvImageOperandsConstOffsetsMask) {
  357. if (opcode != SpvOpImageGather && opcode != SpvOpImageDrefGather &&
  358. opcode != SpvOpImageSparseGather &&
  359. opcode != SpvOpImageSparseDrefGather) {
  360. return _.diag(SPV_ERROR_INVALID_DATA)
  361. << "Image Operand ConstOffsets can only be used with "
  362. "OpImageGather "
  363. << "and OpImageDrefGather: " << spvOpcodeString(opcode);
  364. }
  365. if (info.dim == SpvDimCube) {
  366. return _.diag(SPV_ERROR_INVALID_DATA)
  367. << "Image Operand ConstOffsets cannot be used with Cube Image "
  368. "'Dim': "
  369. << spvOpcodeString(opcode);
  370. }
  371. const uint32_t id = inst.words[word_index++];
  372. const uint32_t type_id = _.GetTypeId(id);
  373. const Instruction* type_inst = _.FindDef(type_id);
  374. assert(type_inst);
  375. if (type_inst->opcode() != SpvOpTypeArray) {
  376. return _.diag(SPV_ERROR_INVALID_DATA)
  377. << "Expected Image Operand ConstOffsets to be an array of size 4: "
  378. << spvOpcodeString(opcode);
  379. }
  380. uint64_t array_size = 0;
  381. if (!_.GetConstantValUint64(type_inst->word(3), &array_size)) {
  382. assert(0 && "Array type definition is corrupt");
  383. }
  384. if (array_size != 4) {
  385. return _.diag(SPV_ERROR_INVALID_DATA)
  386. << "Expected Image Operand ConstOffsets to be an array of size 4: "
  387. << spvOpcodeString(opcode);
  388. }
  389. const uint32_t component_type = type_inst->word(2);
  390. if (!_.IsIntVectorType(component_type) ||
  391. _.GetDimension(component_type) != 2) {
  392. return _.diag(SPV_ERROR_INVALID_DATA)
  393. << "Expected Image Operand ConstOffsets array componenets to be "
  394. "int "
  395. << "vectors of size 2: " << spvOpcodeString(opcode);
  396. }
  397. if (!spvOpcodeIsConstant(_.GetIdOpcode(id))) {
  398. return _.diag(SPV_ERROR_INVALID_DATA)
  399. << "Expected Image Operand ConstOffsets to be a const object: "
  400. << spvOpcodeString(opcode);
  401. }
  402. }
  403. if (mask & SpvImageOperandsSampleMask) {
  404. if (opcode != SpvOpImageFetch && opcode != SpvOpImageRead &&
  405. opcode != SpvOpImageWrite && opcode != SpvOpImageSparseFetch &&
  406. opcode != SpvOpImageSparseRead) {
  407. return _.diag(SPV_ERROR_INVALID_DATA)
  408. << "Image Operand Sample can only be used with OpImageFetch, "
  409. << "OpImageRead, OpImageWrite, OpImageSparseFetch and "
  410. << "OpImageSparseRead: " << spvOpcodeString(opcode);
  411. }
  412. if (info.multisampled == 0) {
  413. return _.diag(SPV_ERROR_INVALID_DATA)
  414. << "Image Operand Sample requires non-zero 'MS' parameter: "
  415. << spvOpcodeString(opcode);
  416. }
  417. const uint32_t type_id = _.GetTypeId(inst.words[word_index++]);
  418. if (!_.IsIntScalarType(type_id)) {
  419. return _.diag(SPV_ERROR_INVALID_DATA)
  420. << "Expected Image Operand Sample to be int scalar: "
  421. << spvOpcodeString(opcode);
  422. }
  423. }
  424. if (mask & SpvImageOperandsMinLodMask) {
  425. if (!is_implicit_lod && !(mask & SpvImageOperandsGradMask)) {
  426. return _.diag(SPV_ERROR_INVALID_DATA)
  427. << "Image Operand MinLod can only be used with ImplicitLod "
  428. << "opcodes or together with Image Operand Grad: "
  429. << spvOpcodeString(opcode);
  430. };
  431. const uint32_t type_id = _.GetTypeId(inst.words[word_index++]);
  432. if (!_.IsFloatScalarType(type_id)) {
  433. return _.diag(SPV_ERROR_INVALID_DATA)
  434. << "Expected Image Operand MinLod to be float scalar: "
  435. << spvOpcodeString(opcode);
  436. }
  437. if (info.dim != SpvDim1D && info.dim != SpvDim2D && info.dim != SpvDim3D &&
  438. info.dim != SpvDimCube) {
  439. return _.diag(SPV_ERROR_INVALID_DATA)
  440. << "Image Operand MinLod requires 'Dim' parameter to be 1D, 2D, "
  441. "3D "
  442. << "or Cube: " << spvOpcodeString(opcode);
  443. }
  444. if (info.multisampled != 0) {
  445. return _.diag(SPV_ERROR_INVALID_DATA)
  446. << "Image Operand MinLod requires 'MS' parameter to be 0: "
  447. << spvOpcodeString(opcode);
  448. }
  449. }
  450. return SPV_SUCCESS;
  451. }
  452. // Checks some of the validation rules which are common to multiple opcodes.
  453. spv_result_t ValidateImageCommon(ValidationState_t& _,
  454. const spv_parsed_instruction_t& inst,
  455. const ImageTypeInfo& info) {
  456. const SpvOp opcode = static_cast<SpvOp>(inst.opcode);
  457. if (IsProj(opcode)) {
  458. if (info.dim != SpvDim1D && info.dim != SpvDim2D && info.dim != SpvDim3D &&
  459. info.dim != SpvDimRect) {
  460. return _.diag(SPV_ERROR_INVALID_DATA)
  461. << "Expected Image 'Dim' parameter to be 1D, 2D, 3D or Rect: "
  462. << spvOpcodeString(opcode);
  463. }
  464. if (info.multisampled != 0) {
  465. return _.diag(SPV_ERROR_INVALID_DATA)
  466. << "Image Image 'MS' parameter to be 0: "
  467. << spvOpcodeString(opcode);
  468. }
  469. if (info.arrayed != 0) {
  470. return _.diag(SPV_ERROR_INVALID_DATA)
  471. << "Image Image 'arrayed' parameter to be 0: "
  472. << spvOpcodeString(opcode);
  473. }
  474. }
  475. if (opcode == SpvOpImageRead || opcode == SpvOpImageSparseRead ||
  476. opcode == SpvOpImageWrite) {
  477. if (info.sampled == 0) {
  478. } else if (info.sampled == 2) {
  479. if (info.dim == SpvDim1D && !_.HasCapability(SpvCapabilityImage1D)) {
  480. return _.diag(SPV_ERROR_INVALID_DATA)
  481. << "Capability Image1D is required to access storage image: "
  482. << spvOpcodeString(opcode);
  483. } else if (info.dim == SpvDimRect &&
  484. !_.HasCapability(SpvCapabilityImageRect)) {
  485. return _.diag(SPV_ERROR_INVALID_DATA)
  486. << "Capability ImageRect is required to access storage image: "
  487. << spvOpcodeString(opcode);
  488. } else if (info.dim == SpvDimBuffer &&
  489. !_.HasCapability(SpvCapabilityImageBuffer)) {
  490. return _.diag(SPV_ERROR_INVALID_DATA)
  491. << "Capability ImageBuffer is required to access storage image: "
  492. << spvOpcodeString(opcode);
  493. } else if (info.dim == SpvDimCube && info.arrayed == 1 &&
  494. !_.HasCapability(SpvCapabilityImageCubeArray)) {
  495. return _.diag(SPV_ERROR_INVALID_DATA)
  496. << "Capability ImageCubeArray is required to access storage "
  497. << "image: " << spvOpcodeString(opcode);
  498. }
  499. if (info.multisampled == 1 &&
  500. !_.HasCapability(SpvCapabilityImageMSArray)) {
  501. #if 0
  502. // TODO([email protected]) The description of this rule in the spec
  503. // is unclear and Glslang doesn't declare ImageMSArray. Need to clarify
  504. // and reenable.
  505. return _.diag(SPV_ERROR_INVALID_DATA)
  506. << "Capability ImageMSArray is required to access storage "
  507. << "image: " << spvOpcodeString(opcode);
  508. #endif
  509. }
  510. } else {
  511. return _.diag(SPV_ERROR_INVALID_DATA)
  512. << "Expected Image 'Sampled' parameter to be 0 or 2: "
  513. << spvOpcodeString(opcode);
  514. }
  515. }
  516. return SPV_SUCCESS;
  517. }
  518. // Returns true if opcode is *ImageSparse*, false otherwise.
  519. bool IsSparse(SpvOp opcode) {
  520. switch (opcode) {
  521. case SpvOpImageSparseSampleImplicitLod:
  522. case SpvOpImageSparseSampleExplicitLod:
  523. case SpvOpImageSparseSampleDrefImplicitLod:
  524. case SpvOpImageSparseSampleDrefExplicitLod:
  525. case SpvOpImageSparseSampleProjImplicitLod:
  526. case SpvOpImageSparseSampleProjExplicitLod:
  527. case SpvOpImageSparseSampleProjDrefImplicitLod:
  528. case SpvOpImageSparseSampleProjDrefExplicitLod:
  529. case SpvOpImageSparseFetch:
  530. case SpvOpImageSparseGather:
  531. case SpvOpImageSparseDrefGather:
  532. case SpvOpImageSparseTexelsResident:
  533. case SpvOpImageSparseRead: {
  534. return true;
  535. }
  536. default: { return false; }
  537. }
  538. return false;
  539. }
  540. // Checks sparse image opcode result type and returns the second struct member.
  541. // Returns inst.type_id for non-sparse image opcodes.
  542. // Not valid for sparse image opcodes which do not return a struct.
  543. spv_result_t GetActualResultType(ValidationState_t& _,
  544. const spv_parsed_instruction_t& inst,
  545. uint32_t* actual_result_type) {
  546. const SpvOp opcode = static_cast<SpvOp>(inst.opcode);
  547. if (IsSparse(opcode)) {
  548. const Instruction* const type_inst = _.FindDef(inst.type_id);
  549. assert(type_inst);
  550. if (!type_inst || type_inst->opcode() != SpvOpTypeStruct) {
  551. return _.diag(SPV_ERROR_INVALID_DATA)
  552. << spvOpcodeString(opcode)
  553. << ": expected Result Type to be OpTypeStruct";
  554. }
  555. if (type_inst->words().size() != 4 ||
  556. !_.IsIntScalarType(type_inst->word(2))) {
  557. return _.diag(SPV_ERROR_INVALID_DATA)
  558. << spvOpcodeString(opcode)
  559. << ": expected Result Type to be a struct containing an int "
  560. "scalar "
  561. << "and a texel";
  562. }
  563. *actual_result_type = type_inst->word(3);
  564. } else {
  565. *actual_result_type = inst.type_id;
  566. }
  567. return SPV_SUCCESS;
  568. }
  569. // Returns a string describing actual result type of an opcode.
  570. // Not valid for sparse image opcodes which do not return a struct.
  571. const char* GetActualResultTypeStr(SpvOp opcode) {
  572. if (IsSparse(opcode)) return "Result Type's second member";
  573. return "Result Type";
  574. }
  575. } // namespace
  576. // Validates correctness of image instructions.
  577. spv_result_t ImagePass(ValidationState_t& _,
  578. const spv_parsed_instruction_t* inst) {
  579. const SpvOp opcode = static_cast<SpvOp>(inst->opcode);
  580. const uint32_t result_type = inst->type_id;
  581. if (IsImplicitLod(opcode)) {
  582. _.current_function().RegisterExecutionModelLimitation(
  583. SpvExecutionModelFragment,
  584. "ImplicitLod instructions require Fragment execution model");
  585. }
  586. switch (opcode) {
  587. case SpvOpTypeImage: {
  588. assert(result_type == 0);
  589. ImageTypeInfo info;
  590. if (!GetImageTypeInfo(_, inst->words[1], &info)) {
  591. return _.diag(SPV_ERROR_INVALID_DATA)
  592. << "OpTypeImage: corrupt definition";
  593. }
  594. if (spvIsVulkanEnv(_.context()->target_env)) {
  595. if ((!_.IsFloatScalarType(info.sampled_type) &&
  596. !_.IsIntScalarType(info.sampled_type)) ||
  597. 32 != _.GetBitWidth(info.sampled_type)) {
  598. return _.diag(SPV_ERROR_INVALID_DATA)
  599. << spvOpcodeString(opcode)
  600. << ": expected Sampled Type to be a 32-bit int or float "
  601. "scalar type for Vulkan environment";
  602. }
  603. } else {
  604. const SpvOp sampled_type_opcode = _.GetIdOpcode(info.sampled_type);
  605. if (sampled_type_opcode != SpvOpTypeVoid &&
  606. sampled_type_opcode != SpvOpTypeInt &&
  607. sampled_type_opcode != SpvOpTypeFloat) {
  608. return _.diag(SPV_ERROR_INVALID_DATA)
  609. << spvOpcodeString(opcode)
  610. << ": expected Sampled Type to be either void or numerical "
  611. << "scalar type";
  612. }
  613. }
  614. // Dim is checked elsewhere.
  615. if (info.depth > 2) {
  616. return _.diag(SPV_ERROR_INVALID_DATA)
  617. << spvOpcodeString(opcode) << ": invalid Depth " << info.depth
  618. << " (must be 0, 1 or 2)";
  619. }
  620. if (info.arrayed > 1) {
  621. return _.diag(SPV_ERROR_INVALID_DATA)
  622. << spvOpcodeString(opcode) << ": invalid Arrayed "
  623. << info.arrayed << " (must be 0 or 1)";
  624. }
  625. if (info.multisampled > 1) {
  626. return _.diag(SPV_ERROR_INVALID_DATA)
  627. << spvOpcodeString(opcode) << ": invalid MS "
  628. << info.multisampled << " (must be 0 or 1)";
  629. }
  630. if (info.sampled > 2) {
  631. return _.diag(SPV_ERROR_INVALID_DATA)
  632. << spvOpcodeString(opcode) << ": invalid Sampled "
  633. << info.sampled << " (must be 0, 1 or 2)";
  634. }
  635. if (info.dim == SpvDimSubpassData) {
  636. if (info.sampled != 2) {
  637. return _.diag(SPV_ERROR_INVALID_DATA)
  638. << spvOpcodeString(opcode)
  639. << ": Dim SubpassData requires Sampled to be 2";
  640. }
  641. if (info.format != SpvImageFormatUnknown) {
  642. return _.diag(SPV_ERROR_INVALID_DATA)
  643. << spvOpcodeString(opcode)
  644. << ": Dim SubpassData requires format Unknown";
  645. }
  646. }
  647. // Format and Access Qualifier are checked elsewhere.
  648. break;
  649. }
  650. case SpvOpTypeSampledImage: {
  651. const uint32_t image_type = inst->words[2];
  652. if (_.GetIdOpcode(image_type) != SpvOpTypeImage) {
  653. return _.diag(SPV_ERROR_INVALID_DATA)
  654. << spvOpcodeString(opcode)
  655. << ": expected Image to be of type OpTypeImage";
  656. }
  657. break;
  658. }
  659. case SpvOpSampledImage: {
  660. if (_.GetIdOpcode(result_type) != SpvOpTypeSampledImage) {
  661. return _.diag(SPV_ERROR_INVALID_DATA)
  662. << "Expected Result Type to be OpTypeSampledImage: "
  663. << spvOpcodeString(opcode);
  664. }
  665. const uint32_t image_type = _.GetOperandTypeId(inst, 2);
  666. if (_.GetIdOpcode(image_type) != SpvOpTypeImage) {
  667. return _.diag(SPV_ERROR_INVALID_DATA)
  668. << "Expected Image to be of type OpTypeImage: "
  669. << spvOpcodeString(opcode);
  670. }
  671. ImageTypeInfo info;
  672. if (!GetImageTypeInfo(_, image_type, &info)) {
  673. return _.diag(SPV_ERROR_INVALID_DATA)
  674. << "Corrupt image type definition";
  675. }
  676. // TODO([email protected]) Check compatibility of result type and received
  677. // image.
  678. if (spvIsVulkanEnv(_.context()->target_env)) {
  679. if (info.sampled != 1) {
  680. return _.diag(SPV_ERROR_INVALID_DATA)
  681. << "Expected Image 'Sampled' parameter to be 1 for Vulkan "
  682. "environment: "
  683. << spvOpcodeString(opcode);
  684. }
  685. } else {
  686. if (info.sampled != 0 && info.sampled != 1) {
  687. return _.diag(SPV_ERROR_INVALID_DATA)
  688. << "Expected Image 'Sampled' parameter to be 0 or 1: "
  689. << spvOpcodeString(opcode);
  690. }
  691. }
  692. if (info.dim == SpvDimSubpassData) {
  693. return _.diag(SPV_ERROR_INVALID_DATA)
  694. << "Expected Image 'Dim' parameter to be not SubpassData: "
  695. << spvOpcodeString(opcode);
  696. }
  697. if (_.GetIdOpcode(_.GetOperandTypeId(inst, 3)) != SpvOpTypeSampler) {
  698. return _.diag(SPV_ERROR_INVALID_DATA)
  699. << "Expected Sampler to be of type OpTypeSampler: "
  700. << spvOpcodeString(opcode);
  701. }
  702. break;
  703. }
  704. case SpvOpImageSampleImplicitLod:
  705. case SpvOpImageSampleExplicitLod:
  706. case SpvOpImageSampleProjImplicitLod:
  707. case SpvOpImageSampleProjExplicitLod:
  708. case SpvOpImageSparseSampleImplicitLod:
  709. case SpvOpImageSparseSampleExplicitLod: {
  710. uint32_t actual_result_type = 0;
  711. if (spv_result_t error =
  712. GetActualResultType(_, *inst, &actual_result_type)) {
  713. return error;
  714. }
  715. if (!_.IsIntVectorType(actual_result_type) &&
  716. !_.IsFloatVectorType(actual_result_type)) {
  717. return _.diag(SPV_ERROR_INVALID_DATA)
  718. << "Expected " << GetActualResultTypeStr(opcode)
  719. << " to be int or float vector type: "
  720. << spvOpcodeString(opcode);
  721. }
  722. if (_.GetDimension(actual_result_type) != 4) {
  723. return _.diag(SPV_ERROR_INVALID_DATA)
  724. << "Expected " << GetActualResultTypeStr(opcode)
  725. << " to have 4 components: " << spvOpcodeString(opcode);
  726. }
  727. const uint32_t image_type = _.GetOperandTypeId(inst, 2);
  728. if (_.GetIdOpcode(image_type) != SpvOpTypeSampledImage) {
  729. return _.diag(SPV_ERROR_INVALID_DATA)
  730. << "Expected Sampled Image to be of type OpTypeSampledImage: "
  731. << spvOpcodeString(opcode);
  732. }
  733. ImageTypeInfo info;
  734. if (!GetImageTypeInfo(_, image_type, &info)) {
  735. return _.diag(SPV_ERROR_INVALID_DATA)
  736. << "Corrupt image type definition";
  737. }
  738. if (spv_result_t result = ValidateImageCommon(_, *inst, info))
  739. return result;
  740. if (_.GetIdOpcode(info.sampled_type) != SpvOpTypeVoid) {
  741. const uint32_t texel_component_type =
  742. _.GetComponentType(actual_result_type);
  743. if (texel_component_type != info.sampled_type) {
  744. return _.diag(SPV_ERROR_INVALID_DATA)
  745. << "Expected Image 'Sampled Type' to be the same as "
  746. << GetActualResultTypeStr(opcode)
  747. << " components: " << spvOpcodeString(opcode);
  748. }
  749. }
  750. const uint32_t coord_type = _.GetOperandTypeId(inst, 3);
  751. if ((opcode == SpvOpImageSampleExplicitLod ||
  752. opcode == SpvOpImageSparseSampleExplicitLod) &&
  753. _.HasCapability(SpvCapabilityKernel)) {
  754. if (!_.IsFloatScalarOrVectorType(coord_type) &&
  755. !_.IsIntScalarOrVectorType(coord_type)) {
  756. return _.diag(SPV_ERROR_INVALID_DATA)
  757. << "Expected Coordinate to be int or float scalar or vector: "
  758. << spvOpcodeString(opcode);
  759. }
  760. } else {
  761. if (!_.IsFloatScalarOrVectorType(coord_type)) {
  762. return _.diag(SPV_ERROR_INVALID_DATA)
  763. << "Expected Coordinate to be float scalar or vector: "
  764. << spvOpcodeString(opcode);
  765. }
  766. }
  767. const uint32_t min_coord_size = GetMinCoordSize(opcode, info);
  768. const uint32_t actual_coord_size = _.GetDimension(coord_type);
  769. if (min_coord_size > actual_coord_size) {
  770. return _.diag(SPV_ERROR_INVALID_DATA)
  771. << "Expected Coordinate to have at least " << min_coord_size
  772. << " components, but given only " << actual_coord_size << ": "
  773. << spvOpcodeString(opcode);
  774. }
  775. if (inst->num_words <= 5) {
  776. assert(IsImplicitLod(opcode));
  777. break;
  778. }
  779. const uint32_t mask = inst->words[5];
  780. if (spv_result_t result =
  781. ValidateImageOperands(_, *inst, info, mask, /* word_index = */ 6))
  782. return result;
  783. break;
  784. }
  785. case SpvOpImageSampleDrefImplicitLod:
  786. case SpvOpImageSampleDrefExplicitLod:
  787. case SpvOpImageSampleProjDrefImplicitLod:
  788. case SpvOpImageSampleProjDrefExplicitLod:
  789. case SpvOpImageSparseSampleDrefImplicitLod:
  790. case SpvOpImageSparseSampleDrefExplicitLod: {
  791. uint32_t actual_result_type = 0;
  792. if (spv_result_t error =
  793. GetActualResultType(_, *inst, &actual_result_type)) {
  794. return error;
  795. }
  796. if (!_.IsIntScalarType(actual_result_type) &&
  797. !_.IsFloatScalarType(actual_result_type)) {
  798. return _.diag(SPV_ERROR_INVALID_DATA)
  799. << "Expected " << GetActualResultTypeStr(opcode)
  800. << " to be int or float scalar type: "
  801. << spvOpcodeString(opcode);
  802. }
  803. const uint32_t image_type = _.GetOperandTypeId(inst, 2);
  804. if (_.GetIdOpcode(image_type) != SpvOpTypeSampledImage) {
  805. return _.diag(SPV_ERROR_INVALID_DATA)
  806. << "Expected Sampled Image to be of type OpTypeSampledImage: "
  807. << spvOpcodeString(opcode);
  808. }
  809. ImageTypeInfo info;
  810. if (!GetImageTypeInfo(_, image_type, &info)) {
  811. return _.diag(SPV_ERROR_INVALID_DATA)
  812. << "Corrupt image type definition";
  813. }
  814. if (spv_result_t result = ValidateImageCommon(_, *inst, info))
  815. return result;
  816. if (actual_result_type != info.sampled_type) {
  817. return _.diag(SPV_ERROR_INVALID_DATA)
  818. << "Expected Image 'Sampled Type' to be the same as "
  819. << GetActualResultTypeStr(opcode) << ": "
  820. << spvOpcodeString(opcode);
  821. }
  822. const uint32_t coord_type = _.GetOperandTypeId(inst, 3);
  823. if (!_.IsFloatScalarOrVectorType(coord_type)) {
  824. return _.diag(SPV_ERROR_INVALID_DATA)
  825. << "Expected Coordinate to be float scalar or vector: "
  826. << spvOpcodeString(opcode);
  827. }
  828. const uint32_t min_coord_size = GetMinCoordSize(opcode, info);
  829. const uint32_t actual_coord_size = _.GetDimension(coord_type);
  830. if (min_coord_size > actual_coord_size) {
  831. return _.diag(SPV_ERROR_INVALID_DATA)
  832. << "Expected Coordinate to have at least " << min_coord_size
  833. << " components, but given only " << actual_coord_size << ": "
  834. << spvOpcodeString(opcode);
  835. }
  836. const uint32_t dref_type = _.GetOperandTypeId(inst, 4);
  837. if (!_.IsFloatScalarType(dref_type) || _.GetBitWidth(dref_type) != 32) {
  838. return _.diag(SPV_ERROR_INVALID_DATA)
  839. << spvOpcodeString(opcode)
  840. << ": Expected Dref to be of 32-bit float type";
  841. }
  842. if (inst->num_words <= 6) {
  843. assert(IsImplicitLod(opcode));
  844. break;
  845. }
  846. const uint32_t mask = inst->words[6];
  847. if (spv_result_t result =
  848. ValidateImageOperands(_, *inst, info, mask, /* word_index = */ 7))
  849. return result;
  850. break;
  851. }
  852. case SpvOpImageFetch:
  853. case SpvOpImageSparseFetch: {
  854. uint32_t actual_result_type = 0;
  855. if (spv_result_t error =
  856. GetActualResultType(_, *inst, &actual_result_type)) {
  857. return error;
  858. }
  859. if (!_.IsIntVectorType(actual_result_type) &&
  860. !_.IsFloatVectorType(actual_result_type)) {
  861. return _.diag(SPV_ERROR_INVALID_DATA)
  862. << "Expected " << GetActualResultTypeStr(opcode)
  863. << " to be int or float vector type: "
  864. << spvOpcodeString(opcode);
  865. }
  866. if (_.GetDimension(actual_result_type) != 4) {
  867. return _.diag(SPV_ERROR_INVALID_DATA)
  868. << "Expected " << GetActualResultTypeStr(opcode)
  869. << " to have 4 components: " << spvOpcodeString(opcode);
  870. }
  871. const uint32_t image_type = _.GetOperandTypeId(inst, 2);
  872. if (_.GetIdOpcode(image_type) != SpvOpTypeImage) {
  873. return _.diag(SPV_ERROR_INVALID_DATA)
  874. << "Expected Image to be of type OpTypeImage: "
  875. << spvOpcodeString(opcode);
  876. }
  877. ImageTypeInfo info;
  878. if (!GetImageTypeInfo(_, image_type, &info)) {
  879. return _.diag(SPV_ERROR_INVALID_DATA)
  880. << "Corrupt image type definition";
  881. }
  882. if (_.GetIdOpcode(info.sampled_type) != SpvOpTypeVoid) {
  883. const uint32_t result_component_type =
  884. _.GetComponentType(actual_result_type);
  885. if (result_component_type != info.sampled_type) {
  886. return _.diag(SPV_ERROR_INVALID_DATA)
  887. << "Expected Image 'Sampled Type' to be the same as "
  888. << GetActualResultTypeStr(opcode)
  889. << " components: " << spvOpcodeString(opcode);
  890. }
  891. }
  892. if (info.dim == SpvDimCube) {
  893. return _.diag(SPV_ERROR_INVALID_DATA)
  894. << "Image 'Dim' cannot be Cube: " << spvOpcodeString(opcode);
  895. }
  896. if (info.sampled != 1) {
  897. return _.diag(SPV_ERROR_INVALID_DATA)
  898. << "Expected Image 'Sampled' parameter to be 1: "
  899. << spvOpcodeString(opcode);
  900. }
  901. const uint32_t coord_type = _.GetOperandTypeId(inst, 3);
  902. if (!_.IsIntScalarOrVectorType(coord_type)) {
  903. return _.diag(SPV_ERROR_INVALID_DATA)
  904. << "Expected Coordinate to be int scalar or vector: "
  905. << spvOpcodeString(opcode);
  906. }
  907. const uint32_t min_coord_size = GetMinCoordSize(opcode, info);
  908. const uint32_t actual_coord_size = _.GetDimension(coord_type);
  909. if (min_coord_size > actual_coord_size) {
  910. return _.diag(SPV_ERROR_INVALID_DATA)
  911. << "Expected Coordinate to have at least " << min_coord_size
  912. << " components, but given only " << actual_coord_size << ": "
  913. << spvOpcodeString(opcode);
  914. }
  915. if (inst->num_words <= 5) break;
  916. const uint32_t mask = inst->words[5];
  917. if (spv_result_t result =
  918. ValidateImageOperands(_, *inst, info, mask, /* word_index = */ 6))
  919. return result;
  920. break;
  921. }
  922. case SpvOpImageGather:
  923. case SpvOpImageDrefGather:
  924. case SpvOpImageSparseGather:
  925. case SpvOpImageSparseDrefGather: {
  926. uint32_t actual_result_type = 0;
  927. if (spv_result_t error =
  928. GetActualResultType(_, *inst, &actual_result_type)) {
  929. return error;
  930. }
  931. if (!_.IsIntVectorType(actual_result_type) &&
  932. !_.IsFloatVectorType(actual_result_type)) {
  933. return _.diag(SPV_ERROR_INVALID_DATA)
  934. << "Expected " << GetActualResultTypeStr(opcode)
  935. << " to be int or float vector type: "
  936. << spvOpcodeString(opcode);
  937. }
  938. if (_.GetDimension(actual_result_type) != 4) {
  939. return _.diag(SPV_ERROR_INVALID_DATA)
  940. << "Expected " << GetActualResultTypeStr(opcode)
  941. << " to have 4 components: " << spvOpcodeString(opcode);
  942. }
  943. const uint32_t image_type = _.GetOperandTypeId(inst, 2);
  944. if (_.GetIdOpcode(image_type) != SpvOpTypeSampledImage) {
  945. return _.diag(SPV_ERROR_INVALID_DATA)
  946. << "Expected Sampled Image to be of type OpTypeSampledImage: "
  947. << spvOpcodeString(opcode);
  948. }
  949. ImageTypeInfo info;
  950. if (!GetImageTypeInfo(_, image_type, &info)) {
  951. return _.diag(SPV_ERROR_INVALID_DATA)
  952. << "Corrupt image type definition";
  953. }
  954. if (opcode == SpvOpImageDrefGather ||
  955. opcode == SpvOpImageSparseDrefGather ||
  956. _.GetIdOpcode(info.sampled_type) != SpvOpTypeVoid) {
  957. const uint32_t result_component_type =
  958. _.GetComponentType(actual_result_type);
  959. if (result_component_type != info.sampled_type) {
  960. return _.diag(SPV_ERROR_INVALID_DATA)
  961. << "Expected Image 'Sampled Type' to be the same as "
  962. << GetActualResultTypeStr(opcode)
  963. << " components: " << spvOpcodeString(opcode);
  964. }
  965. }
  966. if (info.dim != SpvDim2D && info.dim != SpvDimCube &&
  967. info.dim != SpvDimRect) {
  968. return _.diag(SPV_ERROR_INVALID_DATA)
  969. << "Expected Image 'Dim' cannot be Cube: "
  970. << spvOpcodeString(opcode);
  971. }
  972. const uint32_t coord_type = _.GetOperandTypeId(inst, 3);
  973. if (!_.IsFloatScalarOrVectorType(coord_type)) {
  974. return _.diag(SPV_ERROR_INVALID_DATA)
  975. << "Expected Coordinate to be float scalar or vector: "
  976. << spvOpcodeString(opcode);
  977. }
  978. const uint32_t min_coord_size = GetMinCoordSize(opcode, info);
  979. const uint32_t actual_coord_size = _.GetDimension(coord_type);
  980. if (min_coord_size > actual_coord_size) {
  981. return _.diag(SPV_ERROR_INVALID_DATA)
  982. << "Expected Coordinate to have at least " << min_coord_size
  983. << " components, but given only " << actual_coord_size << ": "
  984. << spvOpcodeString(opcode);
  985. }
  986. if (opcode == SpvOpImageGather || opcode == SpvOpImageSparseGather) {
  987. const uint32_t component_index_type = _.GetOperandTypeId(inst, 4);
  988. if (!_.IsIntScalarType(component_index_type) ||
  989. _.GetBitWidth(component_index_type) != 32) {
  990. return _.diag(SPV_ERROR_INVALID_DATA)
  991. << "Expected Component to be 32-bit int scalar: "
  992. << spvOpcodeString(opcode);
  993. }
  994. } else {
  995. assert(opcode == SpvOpImageDrefGather ||
  996. opcode == SpvOpImageSparseDrefGather);
  997. const uint32_t dref_type = _.GetOperandTypeId(inst, 4);
  998. if (!_.IsFloatScalarType(dref_type) || _.GetBitWidth(dref_type) != 32) {
  999. return _.diag(SPV_ERROR_INVALID_DATA)
  1000. << spvOpcodeString(opcode)
  1001. << ": Expected Dref to be of 32-bit float type";
  1002. }
  1003. }
  1004. if (inst->num_words <= 6) break;
  1005. const uint32_t mask = inst->words[6];
  1006. if (spv_result_t result =
  1007. ValidateImageOperands(_, *inst, info, mask, /* word_index = */ 7))
  1008. return result;
  1009. break;
  1010. }
  1011. case SpvOpImageRead:
  1012. case SpvOpImageSparseRead: {
  1013. uint32_t actual_result_type = 0;
  1014. if (spv_result_t error =
  1015. GetActualResultType(_, *inst, &actual_result_type)) {
  1016. return error;
  1017. }
  1018. if (!_.IsIntScalarOrVectorType(actual_result_type) &&
  1019. !_.IsFloatScalarOrVectorType(actual_result_type)) {
  1020. return _.diag(SPV_ERROR_INVALID_DATA)
  1021. << "Expected " << GetActualResultTypeStr(opcode)
  1022. << " to be int or float scalar or vector type: "
  1023. << spvOpcodeString(opcode);
  1024. }
  1025. #if 0
  1026. // TODO([email protected]) Disabled until the spec is clarified.
  1027. if (_.GetDimension(actual_result_type) != 4) {
  1028. return _.diag(SPV_ERROR_INVALID_DATA)
  1029. << "Expected " << GetActualResultTypeStr(opcode)
  1030. << " to have 4 components: " << spvOpcodeString(opcode);
  1031. }
  1032. #endif
  1033. const uint32_t image_type = _.GetOperandTypeId(inst, 2);
  1034. if (_.GetIdOpcode(image_type) != SpvOpTypeImage) {
  1035. return _.diag(SPV_ERROR_INVALID_DATA)
  1036. << "Expected Image to be of type OpTypeImage: "
  1037. << spvOpcodeString(opcode);
  1038. }
  1039. ImageTypeInfo info;
  1040. if (!GetImageTypeInfo(_, image_type, &info)) {
  1041. return _.diag(SPV_ERROR_INVALID_DATA)
  1042. << "Corrupt image type definition";
  1043. }
  1044. if (info.dim == SpvDimSubpassData) {
  1045. if (opcode == SpvOpImageSparseRead) {
  1046. return _.diag(SPV_ERROR_INVALID_DATA)
  1047. << "Image Dim SubpassData cannot be used with "
  1048. << spvOpcodeString(opcode);
  1049. }
  1050. _.current_function().RegisterExecutionModelLimitation(
  1051. SpvExecutionModelFragment,
  1052. std::string("Dim SubpassData requires Fragment execution model: ") +
  1053. spvOpcodeString(opcode));
  1054. }
  1055. if (_.GetIdOpcode(info.sampled_type) != SpvOpTypeVoid) {
  1056. const uint32_t result_component_type =
  1057. _.GetComponentType(actual_result_type);
  1058. if (result_component_type != info.sampled_type) {
  1059. return _.diag(SPV_ERROR_INVALID_DATA)
  1060. << "Expected Image 'Sampled Type' to be the same as "
  1061. << GetActualResultTypeStr(opcode)
  1062. << " components: " << spvOpcodeString(opcode);
  1063. }
  1064. }
  1065. if (spv_result_t result = ValidateImageCommon(_, *inst, info))
  1066. return result;
  1067. const uint32_t coord_type = _.GetOperandTypeId(inst, 3);
  1068. if (!_.IsIntScalarOrVectorType(coord_type)) {
  1069. return _.diag(SPV_ERROR_INVALID_DATA)
  1070. << "Expected Coordinate to be int scalar or vector: "
  1071. << spvOpcodeString(opcode);
  1072. }
  1073. const uint32_t min_coord_size = GetMinCoordSize(opcode, info);
  1074. const uint32_t actual_coord_size = _.GetDimension(coord_type);
  1075. if (min_coord_size > actual_coord_size) {
  1076. return _.diag(SPV_ERROR_INVALID_DATA)
  1077. << "Expected Coordinate to have at least " << min_coord_size
  1078. << " components, but given only " << actual_coord_size << ": "
  1079. << spvOpcodeString(opcode);
  1080. }
  1081. if (info.format == SpvImageFormatUnknown &&
  1082. info.dim != SpvDimSubpassData &&
  1083. !_.HasCapability(SpvCapabilityStorageImageReadWithoutFormat)) {
  1084. return _.diag(SPV_ERROR_INVALID_DATA)
  1085. << "Capability StorageImageReadWithoutFormat is required to "
  1086. << "read storage image: " << spvOpcodeString(opcode);
  1087. }
  1088. if (inst->num_words <= 5) break;
  1089. const uint32_t mask = inst->words[5];
  1090. if (spv_result_t result =
  1091. ValidateImageOperands(_, *inst, info, mask, /* word_index = */ 6))
  1092. return result;
  1093. break;
  1094. }
  1095. case SpvOpImageWrite: {
  1096. const uint32_t image_type = _.GetOperandTypeId(inst, 0);
  1097. if (_.GetIdOpcode(image_type) != SpvOpTypeImage) {
  1098. return _.diag(SPV_ERROR_INVALID_DATA)
  1099. << "Expected Image to be of type OpTypeImage: "
  1100. << spvOpcodeString(opcode);
  1101. }
  1102. ImageTypeInfo info;
  1103. if (!GetImageTypeInfo(_, image_type, &info)) {
  1104. return _.diag(SPV_ERROR_INVALID_DATA)
  1105. << "Corrupt image type definition";
  1106. }
  1107. if (info.dim == SpvDimSubpassData) {
  1108. return _.diag(SPV_ERROR_INVALID_DATA)
  1109. << "Image 'Dim' cannot be SubpassData: "
  1110. << spvOpcodeString(opcode);
  1111. }
  1112. if (spv_result_t result = ValidateImageCommon(_, *inst, info))
  1113. return result;
  1114. const uint32_t coord_type = _.GetOperandTypeId(inst, 1);
  1115. if (!_.IsIntScalarOrVectorType(coord_type)) {
  1116. return _.diag(SPV_ERROR_INVALID_DATA)
  1117. << "Expected Coordinate to be int scalar or vector: "
  1118. << spvOpcodeString(opcode);
  1119. }
  1120. const uint32_t min_coord_size = GetMinCoordSize(opcode, info);
  1121. const uint32_t actual_coord_size = _.GetDimension(coord_type);
  1122. if (min_coord_size > actual_coord_size) {
  1123. return _.diag(SPV_ERROR_INVALID_DATA)
  1124. << "Expected Coordinate to have at least " << min_coord_size
  1125. << " components, but given only " << actual_coord_size << ": "
  1126. << spvOpcodeString(opcode);
  1127. }
  1128. // TODO([email protected]) The spec doesn't explicitely say what the type
  1129. // of texel should be.
  1130. const uint32_t texel_type = _.GetOperandTypeId(inst, 2);
  1131. if (!_.IsIntScalarOrVectorType(texel_type) &&
  1132. !_.IsFloatScalarOrVectorType(texel_type)) {
  1133. return _.diag(SPV_ERROR_INVALID_DATA)
  1134. << "Expected Texel to be int or float vector or scalar: "
  1135. << spvOpcodeString(opcode);
  1136. }
  1137. #if 0
  1138. // TODO: See above.
  1139. if (_.GetDimension(texel_type) != 4) {
  1140. return _.diag(SPV_ERROR_INVALID_DATA)
  1141. << "Expected Texel to have 4 components: "
  1142. << spvOpcodeString(opcode);
  1143. }
  1144. #endif
  1145. if (_.GetIdOpcode(info.sampled_type) != SpvOpTypeVoid) {
  1146. const uint32_t texel_component_type = _.GetComponentType(texel_type);
  1147. if (texel_component_type != info.sampled_type) {
  1148. return _.diag(SPV_ERROR_INVALID_DATA)
  1149. << "Expected Image 'Sampled Type' to be the same as Texel "
  1150. << "components: " << spvOpcodeString(opcode);
  1151. }
  1152. }
  1153. if (info.format == SpvImageFormatUnknown &&
  1154. info.dim != SpvDimSubpassData &&
  1155. !_.HasCapability(SpvCapabilityStorageImageWriteWithoutFormat)) {
  1156. return _.diag(SPV_ERROR_INVALID_DATA)
  1157. << "Capability StorageImageWriteWithoutFormat is required to "
  1158. "write "
  1159. << "to storage image: " << spvOpcodeString(opcode);
  1160. }
  1161. if (inst->num_words <= 4) break;
  1162. const uint32_t mask = inst->words[4];
  1163. if (spv_result_t result =
  1164. ValidateImageOperands(_, *inst, info, mask, /* word_index = */ 5))
  1165. return result;
  1166. break;
  1167. }
  1168. case SpvOpImage: {
  1169. if (_.GetIdOpcode(result_type) != SpvOpTypeImage) {
  1170. return _.diag(SPV_ERROR_INVALID_DATA)
  1171. << "Expected Result Type to be OpTypeImage: "
  1172. << spvOpcodeString(opcode);
  1173. }
  1174. const uint32_t sampled_image_type = _.GetOperandTypeId(inst, 2);
  1175. const Instruction* sampled_image_type_inst =
  1176. _.FindDef(sampled_image_type);
  1177. assert(sampled_image_type_inst);
  1178. if (sampled_image_type_inst->opcode() != SpvOpTypeSampledImage) {
  1179. return _.diag(SPV_ERROR_INVALID_DATA)
  1180. << "Expected Sample Image to be of type OpTypeSampleImage: "
  1181. << spvOpcodeString(opcode);
  1182. }
  1183. if (sampled_image_type_inst->word(2) != result_type) {
  1184. return _.diag(SPV_ERROR_INVALID_DATA)
  1185. << "Expected Sample Image image type to be equal to Result "
  1186. "Type: "
  1187. << spvOpcodeString(opcode);
  1188. }
  1189. break;
  1190. }
  1191. case SpvOpImageQueryFormat:
  1192. case SpvOpImageQueryOrder: {
  1193. if (!_.IsIntScalarType(result_type)) {
  1194. return _.diag(SPV_ERROR_INVALID_DATA)
  1195. << "Expected Result Type to be int scalar type: "
  1196. << spvOpcodeString(opcode);
  1197. }
  1198. if (_.GetIdOpcode(_.GetOperandTypeId(inst, 2)) != SpvOpTypeImage) {
  1199. return _.diag(SPV_ERROR_INVALID_DATA)
  1200. << "Expected operand to be of type OpTypeImage: "
  1201. << spvOpcodeString(opcode);
  1202. }
  1203. break;
  1204. }
  1205. case SpvOpImageQuerySizeLod: {
  1206. if (!_.IsIntScalarOrVectorType(result_type)) {
  1207. return _.diag(SPV_ERROR_INVALID_DATA)
  1208. << "Expected Result Type to be int scalar or vector type: "
  1209. << spvOpcodeString(opcode);
  1210. }
  1211. const uint32_t image_type = _.GetOperandTypeId(inst, 2);
  1212. if (_.GetIdOpcode(image_type) != SpvOpTypeImage) {
  1213. return _.diag(SPV_ERROR_INVALID_DATA)
  1214. << "Expected Image to be of type OpTypeImage: "
  1215. << spvOpcodeString(opcode);
  1216. }
  1217. ImageTypeInfo info;
  1218. if (!GetImageTypeInfo(_, image_type, &info)) {
  1219. return _.diag(SPV_ERROR_INVALID_DATA)
  1220. << "Corrupt image type definition";
  1221. }
  1222. uint32_t expected_num_components = info.arrayed;
  1223. switch (info.dim) {
  1224. case SpvDim1D:
  1225. expected_num_components += 1;
  1226. break;
  1227. case SpvDim2D:
  1228. case SpvDimCube:
  1229. expected_num_components += 2;
  1230. break;
  1231. case SpvDim3D:
  1232. expected_num_components += 3;
  1233. break;
  1234. default:
  1235. return _.diag(SPV_ERROR_INVALID_DATA)
  1236. << "Image 'Dim' must be 1D, 2D, 3D or Cube: "
  1237. << spvOpcodeString(opcode);
  1238. };
  1239. if (info.multisampled != 0) {
  1240. return _.diag(SPV_ERROR_INVALID_DATA)
  1241. << "Image 'MS' must be 0: " << spvOpcodeString(opcode);
  1242. }
  1243. uint32_t result_num_components = _.GetDimension(result_type);
  1244. if (result_num_components != expected_num_components) {
  1245. return _.diag(SPV_ERROR_INVALID_DATA)
  1246. << "Result Type has " << result_num_components << " components, "
  1247. << "but " << expected_num_components
  1248. << " expected: " << spvOpcodeString(opcode);
  1249. }
  1250. const uint32_t lod_type = _.GetOperandTypeId(inst, 3);
  1251. if (!_.IsIntScalarType(lod_type)) {
  1252. return _.diag(SPV_ERROR_INVALID_DATA)
  1253. << "Expected Level of Detail to be int scalar: "
  1254. << spvOpcodeString(opcode);
  1255. }
  1256. break;
  1257. }
  1258. case SpvOpImageQuerySize: {
  1259. if (!_.IsIntScalarOrVectorType(result_type)) {
  1260. return _.diag(SPV_ERROR_INVALID_DATA)
  1261. << "Expected Result Type to be int scalar or vector type: "
  1262. << spvOpcodeString(opcode);
  1263. }
  1264. const uint32_t image_type = _.GetOperandTypeId(inst, 2);
  1265. if (_.GetIdOpcode(image_type) != SpvOpTypeImage) {
  1266. return _.diag(SPV_ERROR_INVALID_DATA)
  1267. << "Expected Image to be of type OpTypeImage: "
  1268. << spvOpcodeString(opcode);
  1269. }
  1270. #if 0
  1271. // TODO([email protected]) The spec doesn't whitelist all Dims supported by
  1272. // GLSL. Need to verify if there is an error and reenable.
  1273. ImageTypeInfo info;
  1274. if (!GetImageTypeInfo(_, image_type, &info)) {
  1275. return _.diag(SPV_ERROR_INVALID_DATA)
  1276. << "Corrupt image type definition";
  1277. }
  1278. uint32_t expected_num_components = info.arrayed;
  1279. switch (info.dim) {
  1280. case SpvDimBuffer:
  1281. expected_num_components += 1;
  1282. break;
  1283. case SpvDim2D:
  1284. if (info.multisampled != 1 && info.sampled != 0 &&
  1285. info.sampled != 2) {
  1286. return _.diag(SPV_ERROR_INVALID_DATA)
  1287. << "Expected either 'MS'=1 or 'Sampled'=0 or 'Sampled'=2 "
  1288. << "for 2D dim: " << spvOpcodeString(opcode);
  1289. }
  1290. case SpvDimRect:
  1291. expected_num_components += 2;
  1292. break;
  1293. case SpvDim3D:
  1294. expected_num_components += 3;
  1295. if (info.sampled != 0 &&
  1296. info.sampled != 2) {
  1297. return _.diag(SPV_ERROR_INVALID_DATA)
  1298. << "Expected either 'Sampled'=0 or 'Sampled'=2 "
  1299. << "for 3D dim: " << spvOpcodeString(opcode);
  1300. }
  1301. break;
  1302. default:
  1303. return _.diag(SPV_ERROR_INVALID_DATA)
  1304. << "Image 'Dim' must be Buffer, 2D, 3D or Rect: "
  1305. << spvOpcodeString(opcode);
  1306. };
  1307. if (info.multisampled != 0) {
  1308. return _.diag(SPV_ERROR_INVALID_DATA)
  1309. << "Image 'MS' must be 0: " << spvOpcodeString(opcode);
  1310. }
  1311. uint32_t result_num_components = _.GetDimension(result_type);
  1312. if (result_num_components != expected_num_components) {
  1313. return _.diag(SPV_ERROR_INVALID_DATA)
  1314. << "Result Type has " << result_num_components << " components, "
  1315. << "but " << expected_num_components << " expected: "
  1316. << spvOpcodeString(opcode);
  1317. }
  1318. #endif
  1319. break;
  1320. }
  1321. case SpvOpImageQueryLod: {
  1322. _.current_function().RegisterExecutionModelLimitation(
  1323. SpvExecutionModelFragment,
  1324. "OpImageQueryLod requires Fragment execution model");
  1325. if (!_.IsFloatVectorType(result_type)) {
  1326. return _.diag(SPV_ERROR_INVALID_DATA)
  1327. << "Expected Result Type to be float vector type: "
  1328. << spvOpcodeString(opcode);
  1329. }
  1330. if (_.GetDimension(result_type) != 2) {
  1331. return _.diag(SPV_ERROR_INVALID_DATA)
  1332. << "Expected Result Type to have 2 components: "
  1333. << spvOpcodeString(opcode);
  1334. }
  1335. const uint32_t image_type = _.GetOperandTypeId(inst, 2);
  1336. if (_.GetIdOpcode(image_type) != SpvOpTypeSampledImage) {
  1337. return _.diag(SPV_ERROR_INVALID_DATA)
  1338. << "Expected Image operand to be of type OpTypeSampledImage: "
  1339. << spvOpcodeString(opcode);
  1340. }
  1341. ImageTypeInfo info;
  1342. if (!GetImageTypeInfo(_, image_type, &info)) {
  1343. return _.diag(SPV_ERROR_INVALID_DATA)
  1344. << "Corrupt image type definition";
  1345. }
  1346. if (info.dim != SpvDim1D && info.dim != SpvDim2D &&
  1347. info.dim != SpvDim3D && info.dim != SpvDimCube) {
  1348. return _.diag(SPV_ERROR_INVALID_DATA)
  1349. << "Image 'Dim' must be 1D, 2D, 3D or Cube: "
  1350. << spvOpcodeString(opcode);
  1351. }
  1352. const uint32_t coord_type = _.GetOperandTypeId(inst, 3);
  1353. if (_.HasCapability(SpvCapabilityKernel)) {
  1354. if (!_.IsFloatScalarOrVectorType(coord_type) &&
  1355. !_.IsIntScalarOrVectorType(coord_type)) {
  1356. return _.diag(SPV_ERROR_INVALID_DATA)
  1357. << "Expected Coordinate to be int or float scalar or vector: "
  1358. << spvOpcodeString(opcode);
  1359. }
  1360. } else {
  1361. if (!_.IsFloatScalarOrVectorType(coord_type)) {
  1362. return _.diag(SPV_ERROR_INVALID_DATA)
  1363. << "Expected Coordinate to be float scalar or vector: "
  1364. << spvOpcodeString(opcode);
  1365. }
  1366. }
  1367. const uint32_t min_coord_size = GetPlaneCoordSize(info);
  1368. const uint32_t actual_coord_size = _.GetDimension(coord_type);
  1369. if (min_coord_size > actual_coord_size) {
  1370. return _.diag(SPV_ERROR_INVALID_DATA)
  1371. << "Expected Coordinate to have at least " << min_coord_size
  1372. << " components, but given only " << actual_coord_size << ": "
  1373. << spvOpcodeString(opcode);
  1374. }
  1375. break;
  1376. }
  1377. case SpvOpImageQueryLevels:
  1378. case SpvOpImageQuerySamples: {
  1379. if (!_.IsIntScalarType(result_type)) {
  1380. return _.diag(SPV_ERROR_INVALID_DATA)
  1381. << "Expected Result Type to be int scalar type: "
  1382. << spvOpcodeString(opcode);
  1383. }
  1384. const uint32_t image_type = _.GetOperandTypeId(inst, 2);
  1385. if (_.GetIdOpcode(image_type) != SpvOpTypeImage) {
  1386. return _.diag(SPV_ERROR_INVALID_DATA)
  1387. << "Expected Image to be of type OpTypeImage: "
  1388. << spvOpcodeString(opcode);
  1389. }
  1390. ImageTypeInfo info;
  1391. if (!GetImageTypeInfo(_, image_type, &info)) {
  1392. return _.diag(SPV_ERROR_INVALID_DATA)
  1393. << "Corrupt image type definition";
  1394. }
  1395. if (opcode == SpvOpImageQueryLevels) {
  1396. if (info.dim != SpvDim1D && info.dim != SpvDim2D &&
  1397. info.dim != SpvDim3D && info.dim != SpvDimCube) {
  1398. return _.diag(SPV_ERROR_INVALID_DATA)
  1399. << "Image 'Dim' must be 1D, 2D, 3D or Cube: "
  1400. << spvOpcodeString(opcode);
  1401. }
  1402. } else {
  1403. assert(opcode == SpvOpImageQuerySamples);
  1404. if (info.dim != SpvDim2D) {
  1405. return _.diag(SPV_ERROR_INVALID_DATA)
  1406. << "Image 'Dim' must be 2D: " << spvOpcodeString(opcode);
  1407. }
  1408. if (info.multisampled != 1) {
  1409. return _.diag(SPV_ERROR_INVALID_DATA)
  1410. << "Image 'MS' must be 1: " << spvOpcodeString(opcode);
  1411. }
  1412. }
  1413. break;
  1414. }
  1415. case SpvOpImageSparseSampleProjImplicitLod:
  1416. case SpvOpImageSparseSampleProjExplicitLod:
  1417. case SpvOpImageSparseSampleProjDrefImplicitLod:
  1418. case SpvOpImageSparseSampleProjDrefExplicitLod: {
  1419. return _.diag(SPV_ERROR_INVALID_DATA)
  1420. << spvOpcodeString(opcode)
  1421. << ": instruction reserved for future use, "
  1422. << "use of this instruction is invalid";
  1423. }
  1424. case SpvOpImageSparseTexelsResident: {
  1425. if (!_.IsBoolScalarType(result_type)) {
  1426. return _.diag(SPV_ERROR_INVALID_DATA)
  1427. << spvOpcodeString(opcode)
  1428. << ": expected Result Type to be bool scalar type";
  1429. }
  1430. const uint32_t resident_code_type = _.GetOperandTypeId(inst, 2);
  1431. if (!_.IsIntScalarType(resident_code_type)) {
  1432. return _.diag(SPV_ERROR_INVALID_DATA)
  1433. << spvOpcodeString(opcode)
  1434. << ": expected Resident Code to be int scalar";
  1435. }
  1436. break;
  1437. }
  1438. default:
  1439. break;
  1440. }
  1441. return SPV_SUCCESS;
  1442. }
  1443. } // namespace libspirv