validate_image.cpp 62 KB

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