validate_image.cpp 63 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823
  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([email protected]) 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. bool IsAllowedSampledImageOperand(SpvOp opcode) {
  649. switch (opcode) {
  650. case SpvOpSampledImage:
  651. case SpvOpImageSampleImplicitLod:
  652. case SpvOpImageSampleExplicitLod:
  653. case SpvOpImageSampleDrefImplicitLod:
  654. case SpvOpImageSampleDrefExplicitLod:
  655. case SpvOpImageSampleProjImplicitLod:
  656. case SpvOpImageSampleProjExplicitLod:
  657. case SpvOpImageSampleProjDrefImplicitLod:
  658. case SpvOpImageSampleProjDrefExplicitLod:
  659. case SpvOpImageGather:
  660. case SpvOpImageDrefGather:
  661. case SpvOpImage:
  662. case SpvOpImageQueryLod:
  663. case SpvOpImageSparseSampleImplicitLod:
  664. case SpvOpImageSparseSampleExplicitLod:
  665. case SpvOpImageSparseSampleDrefImplicitLod:
  666. case SpvOpImageSparseSampleDrefExplicitLod:
  667. case SpvOpImageSparseGather:
  668. case SpvOpImageSparseDrefGather:
  669. return true;
  670. default:
  671. return false;
  672. }
  673. }
  674. spv_result_t ValidateSampledImage(ValidationState_t& _,
  675. const Instruction* inst) {
  676. if (_.GetIdOpcode(inst->type_id()) != SpvOpTypeSampledImage) {
  677. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  678. << "Expected Result Type to be OpTypeSampledImage.";
  679. }
  680. const uint32_t image_type = _.GetOperandTypeId(inst, 2);
  681. if (_.GetIdOpcode(image_type) != SpvOpTypeImage) {
  682. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  683. << "Expected Image to be of type OpTypeImage.";
  684. }
  685. ImageTypeInfo info;
  686. if (!GetImageTypeInfo(_, image_type, &info)) {
  687. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  688. << "Corrupt image type definition";
  689. }
  690. // TODO([email protected]) Check compatibility of result type and received
  691. // image.
  692. if (spvIsVulkanEnv(_.context()->target_env)) {
  693. if (info.sampled != 1) {
  694. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  695. << "Expected Image 'Sampled' parameter to be 1 "
  696. << "for Vulkan environment.";
  697. }
  698. } else {
  699. if (info.sampled != 0 && info.sampled != 1) {
  700. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  701. << "Expected Image 'Sampled' parameter to be 0 or 1";
  702. }
  703. }
  704. if (info.dim == SpvDimSubpassData) {
  705. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  706. << "Expected Image 'Dim' parameter to be not SubpassData.";
  707. }
  708. if (_.GetIdOpcode(_.GetOperandTypeId(inst, 3)) != SpvOpTypeSampler) {
  709. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  710. << "Expected Sampler to be of type OpTypeSampler";
  711. }
  712. // We need to validate 2 things:
  713. // * All OpSampledImage instructions must be in the same block in which their
  714. // Result <id> are consumed.
  715. // * Result <id> from OpSampledImage instructions must not appear as operands
  716. // to OpPhi instructions or OpSelect instructions, or any instructions other
  717. // than the image lookup and query instructions specified to take an operand
  718. // whose type is OpTypeSampledImage.
  719. std::vector<Instruction*> consumers = _.getSampledImageConsumers(inst->id());
  720. if (!consumers.empty()) {
  721. for (auto consumer_instr : consumers) {
  722. const auto consumer_opcode = consumer_instr->opcode();
  723. if (consumer_instr->block() != inst->block()) {
  724. return _.diag(SPV_ERROR_INVALID_ID, inst)
  725. << "All OpSampledImage instructions must be in the same block "
  726. "in "
  727. "which their Result <id> are consumed. OpSampledImage Result "
  728. "Type <id> '"
  729. << _.getIdName(inst->id())
  730. << "' has a consumer in a different basic "
  731. "block. The consumer instruction <id> is '"
  732. << _.getIdName(consumer_instr->id()) << "'.";
  733. }
  734. if (consumer_opcode == SpvOpPhi || consumer_opcode == SpvOpSelect) {
  735. return _.diag(SPV_ERROR_INVALID_ID, inst)
  736. << "Result <id> from OpSampledImage instruction must not appear "
  737. "as "
  738. "operands of Op"
  739. << spvOpcodeString(static_cast<SpvOp>(consumer_opcode)) << "."
  740. << " Found result <id> '" << _.getIdName(inst->id())
  741. << "' as an operand of <id> '"
  742. << _.getIdName(consumer_instr->id()) << "'.";
  743. }
  744. if (!IsAllowedSampledImageOperand(consumer_opcode)) {
  745. return _.diag(SPV_ERROR_INVALID_ID, inst)
  746. << "Result <id> from OpSampledImage instruction must not appear "
  747. "as operand for Op"
  748. << spvOpcodeString(static_cast<SpvOp>(consumer_opcode))
  749. << ", since it is not specificed as taking an "
  750. << "OpTypeSampledImage."
  751. << " Found result <id> '" << _.getIdName(inst->id())
  752. << "' as an operand of <id> '"
  753. << _.getIdName(consumer_instr->id()) << "'.";
  754. }
  755. }
  756. }
  757. return SPV_SUCCESS;
  758. }
  759. spv_result_t ValidateImageTexelPointer(ValidationState_t& _,
  760. const Instruction* inst) {
  761. const auto result_type = _.FindDef(inst->type_id());
  762. if (result_type->opcode() != SpvOpTypePointer) {
  763. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  764. << "Expected Result Type to be OpTypePointer";
  765. }
  766. const auto storage_class = result_type->GetOperandAs<uint32_t>(1);
  767. if (storage_class != SpvStorageClassImage) {
  768. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  769. << "Expected Result Type to be OpTypePointer whose Storage Class "
  770. "operand is Image";
  771. }
  772. const auto ptr_type = result_type->GetOperandAs<uint32_t>(2);
  773. const auto ptr_opcode = _.GetIdOpcode(ptr_type);
  774. if (ptr_opcode != SpvOpTypeInt && ptr_opcode != SpvOpTypeFloat &&
  775. ptr_opcode != SpvOpTypeVoid) {
  776. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  777. << "Expected Result Type to be OpTypePointer whose Type operand "
  778. "must be a scalar numerical type or OpTypeVoid";
  779. }
  780. const auto image_ptr = _.FindDef(_.GetOperandTypeId(inst, 2));
  781. if (!image_ptr || image_ptr->opcode() != SpvOpTypePointer) {
  782. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  783. << "Expected Image to be OpTypePointer";
  784. }
  785. const auto image_type = image_ptr->GetOperandAs<uint32_t>(2);
  786. if (_.GetIdOpcode(image_type) != SpvOpTypeImage) {
  787. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  788. << "Expected Image to be OpTypePointer with Type OpTypeImage";
  789. }
  790. ImageTypeInfo info;
  791. if (!GetImageTypeInfo(_, image_type, &info)) {
  792. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  793. << "Corrupt image type definition";
  794. }
  795. if (info.sampled_type != ptr_type) {
  796. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  797. << "Expected Image 'Sampled Type' to be the same as the Type "
  798. "pointed to by Result Type";
  799. }
  800. if (info.dim == SpvDimSubpassData) {
  801. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  802. << "Image Dim SubpassData cannot be used with OpImageTexelPointer";
  803. }
  804. const uint32_t coord_type = _.GetOperandTypeId(inst, 3);
  805. if (!coord_type || !_.IsIntScalarOrVectorType(coord_type)) {
  806. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  807. << "Expected Coordinate to be integer scalar or vector";
  808. }
  809. uint32_t expected_coord_size = 0;
  810. if (info.arrayed == 0) {
  811. expected_coord_size = GetPlaneCoordSize(info);
  812. } else if (info.arrayed == 1) {
  813. switch (info.dim) {
  814. case SpvDim1D:
  815. expected_coord_size = 2;
  816. break;
  817. case SpvDimCube:
  818. case SpvDim2D:
  819. expected_coord_size = 3;
  820. break;
  821. default:
  822. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  823. << "Expected Image 'Dim' must be one of 1D, 2D, or Cube when "
  824. "Arrayed is 1";
  825. break;
  826. }
  827. }
  828. const uint32_t actual_coord_size = _.GetDimension(coord_type);
  829. if (expected_coord_size != actual_coord_size) {
  830. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  831. << "Expected Coordinate to have " << expected_coord_size
  832. << " components, but given " << actual_coord_size;
  833. }
  834. const uint32_t sample_type = _.GetOperandTypeId(inst, 4);
  835. if (!sample_type || !_.IsIntScalarType(sample_type)) {
  836. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  837. << "Expected Sample to be integer scalar";
  838. }
  839. if (info.multisampled == 0) {
  840. uint64_t ms = 0;
  841. if (!_.GetConstantValUint64(inst->GetOperandAs<uint32_t>(4), &ms) ||
  842. ms != 0) {
  843. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  844. << "Expected Sample for Image with MS 0 to be a valid <id> for "
  845. "the value 0";
  846. }
  847. }
  848. return SPV_SUCCESS;
  849. }
  850. spv_result_t ValidateImageLod(ValidationState_t& _, const Instruction* inst) {
  851. const SpvOp opcode = inst->opcode();
  852. uint32_t actual_result_type = 0;
  853. if (spv_result_t error = GetActualResultType(_, inst, &actual_result_type)) {
  854. return error;
  855. }
  856. if (!_.IsIntVectorType(actual_result_type) &&
  857. !_.IsFloatVectorType(actual_result_type)) {
  858. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  859. << "Expected " << GetActualResultTypeStr(opcode)
  860. << " to be int or float vector type";
  861. }
  862. if (_.GetDimension(actual_result_type) != 4) {
  863. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  864. << "Expected " << GetActualResultTypeStr(opcode)
  865. << " to have 4 components";
  866. }
  867. const uint32_t image_type = _.GetOperandTypeId(inst, 2);
  868. if (_.GetIdOpcode(image_type) != SpvOpTypeSampledImage) {
  869. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  870. << "Expected Sampled Image to be of type OpTypeSampledImage";
  871. }
  872. ImageTypeInfo info;
  873. if (!GetImageTypeInfo(_, image_type, &info)) {
  874. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  875. << "Corrupt image type definition";
  876. }
  877. if (spv_result_t result = ValidateImageCommon(_, inst, info)) return result;
  878. if (_.GetIdOpcode(info.sampled_type) != SpvOpTypeVoid) {
  879. const uint32_t texel_component_type =
  880. _.GetComponentType(actual_result_type);
  881. if (texel_component_type != info.sampled_type) {
  882. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  883. << "Expected Image 'Sampled Type' to be the same as "
  884. << GetActualResultTypeStr(opcode) << " components";
  885. }
  886. }
  887. const uint32_t coord_type = _.GetOperandTypeId(inst, 3);
  888. if ((opcode == SpvOpImageSampleExplicitLod ||
  889. opcode == SpvOpImageSparseSampleExplicitLod) &&
  890. _.HasCapability(SpvCapabilityKernel)) {
  891. if (!_.IsFloatScalarOrVectorType(coord_type) &&
  892. !_.IsIntScalarOrVectorType(coord_type)) {
  893. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  894. << "Expected Coordinate to be int or float scalar or vector";
  895. }
  896. } else {
  897. if (!_.IsFloatScalarOrVectorType(coord_type)) {
  898. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  899. << "Expected Coordinate to be float scalar or vector";
  900. }
  901. }
  902. const uint32_t min_coord_size = GetMinCoordSize(opcode, info);
  903. const uint32_t actual_coord_size = _.GetDimension(coord_type);
  904. if (min_coord_size > actual_coord_size) {
  905. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  906. << "Expected Coordinate to have at least " << min_coord_size
  907. << " components, but given only " << actual_coord_size;
  908. }
  909. if (inst->words().size() <= 5) {
  910. assert(IsImplicitLod(opcode));
  911. return SPV_SUCCESS;
  912. }
  913. const uint32_t mask = inst->word(5);
  914. if (spv_result_t result =
  915. ValidateImageOperands(_, inst, info, mask, /* word_index = */ 6))
  916. return result;
  917. return SPV_SUCCESS;
  918. }
  919. spv_result_t ValidateImageDrefLod(ValidationState_t& _,
  920. const Instruction* inst) {
  921. const SpvOp opcode = inst->opcode();
  922. uint32_t actual_result_type = 0;
  923. if (spv_result_t error = GetActualResultType(_, inst, &actual_result_type)) {
  924. return error;
  925. }
  926. if (!_.IsIntScalarType(actual_result_type) &&
  927. !_.IsFloatScalarType(actual_result_type)) {
  928. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  929. << "Expected " << GetActualResultTypeStr(opcode)
  930. << " to be int or float scalar type";
  931. }
  932. const uint32_t image_type = _.GetOperandTypeId(inst, 2);
  933. if (_.GetIdOpcode(image_type) != SpvOpTypeSampledImage) {
  934. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  935. << "Expected Sampled Image to be of type OpTypeSampledImage";
  936. }
  937. ImageTypeInfo info;
  938. if (!GetImageTypeInfo(_, image_type, &info)) {
  939. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  940. << "Corrupt image type definition";
  941. }
  942. if (spv_result_t result = ValidateImageCommon(_, inst, info)) return result;
  943. if (actual_result_type != info.sampled_type) {
  944. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  945. << "Expected Image 'Sampled Type' to be the same as "
  946. << GetActualResultTypeStr(opcode);
  947. }
  948. const uint32_t coord_type = _.GetOperandTypeId(inst, 3);
  949. if (!_.IsFloatScalarOrVectorType(coord_type)) {
  950. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  951. << "Expected Coordinate to be float scalar or vector";
  952. }
  953. const uint32_t min_coord_size = GetMinCoordSize(opcode, info);
  954. const uint32_t actual_coord_size = _.GetDimension(coord_type);
  955. if (min_coord_size > actual_coord_size) {
  956. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  957. << "Expected Coordinate to have at least " << min_coord_size
  958. << " components, but given only " << actual_coord_size;
  959. }
  960. const uint32_t dref_type = _.GetOperandTypeId(inst, 4);
  961. if (!_.IsFloatScalarType(dref_type) || _.GetBitWidth(dref_type) != 32) {
  962. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  963. << "Expected Dref to be of 32-bit float type";
  964. }
  965. if (inst->words().size() <= 6) {
  966. assert(IsImplicitLod(opcode));
  967. return SPV_SUCCESS;
  968. }
  969. const uint32_t mask = inst->word(6);
  970. if (spv_result_t result =
  971. ValidateImageOperands(_, inst, info, mask, /* word_index = */ 7))
  972. return result;
  973. return SPV_SUCCESS;
  974. }
  975. spv_result_t ValidateImageFetch(ValidationState_t& _, const Instruction* inst) {
  976. uint32_t actual_result_type = 0;
  977. if (spv_result_t error = GetActualResultType(_, inst, &actual_result_type)) {
  978. return error;
  979. }
  980. const SpvOp opcode = inst->opcode();
  981. if (!_.IsIntVectorType(actual_result_type) &&
  982. !_.IsFloatVectorType(actual_result_type)) {
  983. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  984. << "Expected " << GetActualResultTypeStr(opcode)
  985. << " to be int or float vector type";
  986. }
  987. if (_.GetDimension(actual_result_type) != 4) {
  988. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  989. << "Expected " << GetActualResultTypeStr(opcode)
  990. << " to have 4 components";
  991. }
  992. const uint32_t image_type = _.GetOperandTypeId(inst, 2);
  993. if (_.GetIdOpcode(image_type) != SpvOpTypeImage) {
  994. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  995. << "Expected Image to be of type OpTypeImage";
  996. }
  997. ImageTypeInfo info;
  998. if (!GetImageTypeInfo(_, image_type, &info)) {
  999. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  1000. << "Corrupt image type definition";
  1001. }
  1002. if (_.GetIdOpcode(info.sampled_type) != SpvOpTypeVoid) {
  1003. const uint32_t result_component_type =
  1004. _.GetComponentType(actual_result_type);
  1005. if (result_component_type != info.sampled_type) {
  1006. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  1007. << "Expected Image 'Sampled Type' to be the same as "
  1008. << GetActualResultTypeStr(opcode) << " components";
  1009. }
  1010. }
  1011. if (info.dim == SpvDimCube) {
  1012. return _.diag(SPV_ERROR_INVALID_DATA, inst) << "Image 'Dim' cannot be Cube";
  1013. }
  1014. if (info.sampled != 1) {
  1015. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  1016. << "Expected Image 'Sampled' parameter to be 1";
  1017. }
  1018. const uint32_t coord_type = _.GetOperandTypeId(inst, 3);
  1019. if (!_.IsIntScalarOrVectorType(coord_type)) {
  1020. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  1021. << "Expected Coordinate to be int scalar or vector";
  1022. }
  1023. const uint32_t min_coord_size = GetMinCoordSize(opcode, info);
  1024. const uint32_t actual_coord_size = _.GetDimension(coord_type);
  1025. if (min_coord_size > actual_coord_size) {
  1026. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  1027. << "Expected Coordinate to have at least " << min_coord_size
  1028. << " components, but given only " << actual_coord_size;
  1029. }
  1030. if (inst->words().size() <= 5) return SPV_SUCCESS;
  1031. const uint32_t mask = inst->word(5);
  1032. if (spv_result_t result =
  1033. ValidateImageOperands(_, inst, info, mask, /* word_index = */ 6))
  1034. return result;
  1035. return SPV_SUCCESS;
  1036. }
  1037. spv_result_t ValidateImageGather(ValidationState_t& _,
  1038. const Instruction* inst) {
  1039. uint32_t actual_result_type = 0;
  1040. if (spv_result_t error = GetActualResultType(_, inst, &actual_result_type))
  1041. return error;
  1042. const SpvOp opcode = inst->opcode();
  1043. if (!_.IsIntVectorType(actual_result_type) &&
  1044. !_.IsFloatVectorType(actual_result_type)) {
  1045. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  1046. << "Expected " << GetActualResultTypeStr(opcode)
  1047. << " to be int or float vector type";
  1048. }
  1049. if (_.GetDimension(actual_result_type) != 4) {
  1050. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  1051. << "Expected " << GetActualResultTypeStr(opcode)
  1052. << " to have 4 components";
  1053. }
  1054. const uint32_t image_type = _.GetOperandTypeId(inst, 2);
  1055. if (_.GetIdOpcode(image_type) != SpvOpTypeSampledImage) {
  1056. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  1057. << "Expected Sampled Image to be of type OpTypeSampledImage";
  1058. }
  1059. ImageTypeInfo info;
  1060. if (!GetImageTypeInfo(_, image_type, &info)) {
  1061. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  1062. << "Corrupt image type definition";
  1063. }
  1064. if (opcode == SpvOpImageDrefGather || opcode == SpvOpImageSparseDrefGather ||
  1065. _.GetIdOpcode(info.sampled_type) != SpvOpTypeVoid) {
  1066. const uint32_t result_component_type =
  1067. _.GetComponentType(actual_result_type);
  1068. if (result_component_type != info.sampled_type) {
  1069. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  1070. << "Expected Image 'Sampled Type' to be the same as "
  1071. << GetActualResultTypeStr(opcode) << " components";
  1072. }
  1073. }
  1074. if (info.dim != SpvDim2D && info.dim != SpvDimCube &&
  1075. info.dim != SpvDimRect) {
  1076. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  1077. << "Expected Image 'Dim' cannot be Cube";
  1078. }
  1079. const uint32_t coord_type = _.GetOperandTypeId(inst, 3);
  1080. if (!_.IsFloatScalarOrVectorType(coord_type)) {
  1081. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  1082. << "Expected Coordinate to be float scalar or vector";
  1083. }
  1084. const uint32_t min_coord_size = GetMinCoordSize(opcode, info);
  1085. const uint32_t actual_coord_size = _.GetDimension(coord_type);
  1086. if (min_coord_size > actual_coord_size) {
  1087. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  1088. << "Expected Coordinate to have at least " << min_coord_size
  1089. << " components, but given only " << actual_coord_size;
  1090. }
  1091. if (opcode == SpvOpImageGather || opcode == SpvOpImageSparseGather) {
  1092. const uint32_t component_index_type = _.GetOperandTypeId(inst, 4);
  1093. if (!_.IsIntScalarType(component_index_type) ||
  1094. _.GetBitWidth(component_index_type) != 32) {
  1095. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  1096. << "Expected Component to be 32-bit int scalar";
  1097. }
  1098. } else {
  1099. assert(opcode == SpvOpImageDrefGather ||
  1100. opcode == SpvOpImageSparseDrefGather);
  1101. const uint32_t dref_type = _.GetOperandTypeId(inst, 4);
  1102. if (!_.IsFloatScalarType(dref_type) || _.GetBitWidth(dref_type) != 32) {
  1103. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  1104. << "Expected Dref to be of 32-bit float type";
  1105. }
  1106. }
  1107. if (inst->words().size() <= 6) return SPV_SUCCESS;
  1108. const uint32_t mask = inst->word(6);
  1109. if (spv_result_t result =
  1110. ValidateImageOperands(_, inst, info, mask, /* word_index = */ 7))
  1111. return result;
  1112. return SPV_SUCCESS;
  1113. }
  1114. spv_result_t ValidateImageRead(ValidationState_t& _, const Instruction* inst) {
  1115. const SpvOp opcode = inst->opcode();
  1116. uint32_t actual_result_type = 0;
  1117. if (spv_result_t error = GetActualResultType(_, inst, &actual_result_type)) {
  1118. return error;
  1119. }
  1120. if (!_.IsIntScalarOrVectorType(actual_result_type) &&
  1121. !_.IsFloatScalarOrVectorType(actual_result_type)) {
  1122. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  1123. << "Expected " << GetActualResultTypeStr(opcode)
  1124. << " to be int or float scalar or vector type";
  1125. }
  1126. #if 0
  1127. // TODO([email protected]) Disabled until the spec is clarified.
  1128. if (_.GetDimension(actual_result_type) != 4) {
  1129. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  1130. << "Expected " << GetActualResultTypeStr(opcode)
  1131. << " to have 4 components";
  1132. }
  1133. #endif
  1134. const uint32_t image_type = _.GetOperandTypeId(inst, 2);
  1135. if (_.GetIdOpcode(image_type) != SpvOpTypeImage) {
  1136. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  1137. << "Expected Image to be of type OpTypeImage";
  1138. }
  1139. ImageTypeInfo info;
  1140. if (!GetImageTypeInfo(_, image_type, &info)) {
  1141. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  1142. << "Corrupt image type definition";
  1143. }
  1144. if (info.dim == SpvDimSubpassData) {
  1145. if (opcode == SpvOpImageSparseRead) {
  1146. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  1147. << "Image Dim SubpassData cannot be used with ImageSparseRead";
  1148. }
  1149. _.function(inst->function()->id())
  1150. ->RegisterExecutionModelLimitation(
  1151. SpvExecutionModelFragment,
  1152. std::string("Dim SubpassData requires Fragment execution model: ") +
  1153. spvOpcodeString(opcode));
  1154. }
  1155. if (_.GetIdOpcode(info.sampled_type) != SpvOpTypeVoid) {
  1156. const uint32_t result_component_type =
  1157. _.GetComponentType(actual_result_type);
  1158. if (result_component_type != info.sampled_type) {
  1159. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  1160. << "Expected Image 'Sampled Type' to be the same as "
  1161. << GetActualResultTypeStr(opcode) << " components";
  1162. }
  1163. }
  1164. if (spv_result_t result = ValidateImageCommon(_, inst, info)) return result;
  1165. const uint32_t coord_type = _.GetOperandTypeId(inst, 3);
  1166. if (!_.IsIntScalarOrVectorType(coord_type)) {
  1167. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  1168. << "Expected Coordinate to be int scalar or vector";
  1169. }
  1170. const uint32_t min_coord_size = GetMinCoordSize(opcode, info);
  1171. const uint32_t actual_coord_size = _.GetDimension(coord_type);
  1172. if (min_coord_size > actual_coord_size) {
  1173. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  1174. << "Expected Coordinate to have at least " << min_coord_size
  1175. << " components, but given only " << actual_coord_size;
  1176. }
  1177. if (info.format == SpvImageFormatUnknown && info.dim != SpvDimSubpassData &&
  1178. !_.HasCapability(SpvCapabilityStorageImageReadWithoutFormat)) {
  1179. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  1180. << "Capability StorageImageReadWithoutFormat is required to "
  1181. << "read storage image";
  1182. }
  1183. if (inst->words().size() <= 5) return SPV_SUCCESS;
  1184. const uint32_t mask = inst->word(5);
  1185. if (spv_result_t result =
  1186. ValidateImageOperands(_, inst, info, mask, /* word_index = */ 6))
  1187. return result;
  1188. return SPV_SUCCESS;
  1189. }
  1190. spv_result_t ValidateImageWrite(ValidationState_t& _, const Instruction* inst) {
  1191. const uint32_t image_type = _.GetOperandTypeId(inst, 0);
  1192. if (_.GetIdOpcode(image_type) != SpvOpTypeImage) {
  1193. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  1194. << "Expected Image to be of type OpTypeImage";
  1195. }
  1196. ImageTypeInfo info;
  1197. if (!GetImageTypeInfo(_, image_type, &info)) {
  1198. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  1199. << "Corrupt image type definition";
  1200. }
  1201. if (info.dim == SpvDimSubpassData) {
  1202. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  1203. << "Image 'Dim' cannot be SubpassData";
  1204. }
  1205. if (spv_result_t result = ValidateImageCommon(_, inst, info)) return result;
  1206. const uint32_t coord_type = _.GetOperandTypeId(inst, 1);
  1207. if (!_.IsIntScalarOrVectorType(coord_type)) {
  1208. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  1209. << "Expected Coordinate to be int scalar or vector";
  1210. }
  1211. const uint32_t min_coord_size = GetMinCoordSize(inst->opcode(), info);
  1212. const uint32_t actual_coord_size = _.GetDimension(coord_type);
  1213. if (min_coord_size > actual_coord_size) {
  1214. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  1215. << "Expected Coordinate to have at least " << min_coord_size
  1216. << " components, but given only " << actual_coord_size;
  1217. }
  1218. // TODO([email protected]) The spec doesn't explicitely say what the type
  1219. // of texel should be.
  1220. const uint32_t texel_type = _.GetOperandTypeId(inst, 2);
  1221. if (!_.IsIntScalarOrVectorType(texel_type) &&
  1222. !_.IsFloatScalarOrVectorType(texel_type)) {
  1223. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  1224. << "Expected Texel to be int or float vector or scalar";
  1225. }
  1226. #if 0
  1227. // TODO: See above.
  1228. if (_.GetDimension(texel_type) != 4) {
  1229. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  1230. << "Expected Texel to have 4 components";
  1231. }
  1232. #endif
  1233. if (_.GetIdOpcode(info.sampled_type) != SpvOpTypeVoid) {
  1234. const uint32_t texel_component_type = _.GetComponentType(texel_type);
  1235. if (texel_component_type != info.sampled_type) {
  1236. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  1237. << "Expected Image 'Sampled Type' to be the same as Texel "
  1238. << "components";
  1239. }
  1240. }
  1241. if (info.format == SpvImageFormatUnknown && info.dim != SpvDimSubpassData &&
  1242. !_.HasCapability(SpvCapabilityStorageImageWriteWithoutFormat)) {
  1243. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  1244. << "Capability StorageImageWriteWithoutFormat is required to "
  1245. "write "
  1246. << "to storage image";
  1247. }
  1248. if (inst->words().size() <= 4) return SPV_SUCCESS;
  1249. const uint32_t mask = inst->word(4);
  1250. if (spv_result_t result =
  1251. ValidateImageOperands(_, inst, info, mask, /* word_index = */ 5))
  1252. return result;
  1253. return SPV_SUCCESS;
  1254. }
  1255. spv_result_t ValidateImage(ValidationState_t& _, const Instruction* inst) {
  1256. const uint32_t result_type = inst->type_id();
  1257. if (_.GetIdOpcode(result_type) != SpvOpTypeImage) {
  1258. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  1259. << "Expected Result Type to be OpTypeImage";
  1260. }
  1261. const uint32_t sampled_image_type = _.GetOperandTypeId(inst, 2);
  1262. const Instruction* sampled_image_type_inst = _.FindDef(sampled_image_type);
  1263. assert(sampled_image_type_inst);
  1264. if (sampled_image_type_inst->opcode() != SpvOpTypeSampledImage) {
  1265. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  1266. << "Expected Sample Image to be of type OpTypeSampleImage";
  1267. }
  1268. if (sampled_image_type_inst->word(2) != result_type) {
  1269. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  1270. << "Expected Sample Image image type to be equal to Result Type";
  1271. }
  1272. return SPV_SUCCESS;
  1273. }
  1274. spv_result_t ValidateImageQuerySizeLod(ValidationState_t& _,
  1275. const Instruction* inst) {
  1276. const uint32_t result_type = inst->type_id();
  1277. if (!_.IsIntScalarOrVectorType(result_type)) {
  1278. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  1279. << "Expected Result Type to be int scalar or vector type";
  1280. }
  1281. const uint32_t image_type = _.GetOperandTypeId(inst, 2);
  1282. if (_.GetIdOpcode(image_type) != SpvOpTypeImage) {
  1283. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  1284. << "Expected Image to be of type OpTypeImage";
  1285. }
  1286. ImageTypeInfo info;
  1287. if (!GetImageTypeInfo(_, image_type, &info)) {
  1288. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  1289. << "Corrupt image type definition";
  1290. }
  1291. uint32_t expected_num_components = info.arrayed;
  1292. switch (info.dim) {
  1293. case SpvDim1D:
  1294. expected_num_components += 1;
  1295. break;
  1296. case SpvDim2D:
  1297. case SpvDimCube:
  1298. expected_num_components += 2;
  1299. break;
  1300. case SpvDim3D:
  1301. expected_num_components += 3;
  1302. break;
  1303. default:
  1304. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  1305. << "Image 'Dim' must be 1D, 2D, 3D or Cube";
  1306. }
  1307. if (info.multisampled != 0) {
  1308. return _.diag(SPV_ERROR_INVALID_DATA, inst) << "Image 'MS' must be 0";
  1309. }
  1310. uint32_t result_num_components = _.GetDimension(result_type);
  1311. if (result_num_components != expected_num_components) {
  1312. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  1313. << "Result Type has " << result_num_components << " components, "
  1314. << "but " << expected_num_components << " expected";
  1315. }
  1316. const uint32_t lod_type = _.GetOperandTypeId(inst, 3);
  1317. if (!_.IsIntScalarType(lod_type)) {
  1318. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  1319. << "Expected Level of Detail to be int scalar";
  1320. }
  1321. return SPV_SUCCESS;
  1322. }
  1323. spv_result_t ValidateImageQuerySize(ValidationState_t& _,
  1324. const Instruction* inst) {
  1325. const uint32_t result_type = inst->type_id();
  1326. if (!_.IsIntScalarOrVectorType(result_type)) {
  1327. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  1328. << "Expected Result Type to be int scalar or vector type";
  1329. }
  1330. const uint32_t image_type = _.GetOperandTypeId(inst, 2);
  1331. if (_.GetIdOpcode(image_type) != SpvOpTypeImage) {
  1332. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  1333. << "Expected Image to be of type OpTypeImage";
  1334. }
  1335. ImageTypeInfo info;
  1336. if (!GetImageTypeInfo(_, image_type, &info)) {
  1337. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  1338. << "Corrupt image type definition";
  1339. }
  1340. uint32_t expected_num_components = info.arrayed;
  1341. switch (info.dim) {
  1342. case SpvDim1D:
  1343. case SpvDimBuffer:
  1344. expected_num_components += 1;
  1345. break;
  1346. case SpvDim2D:
  1347. case SpvDimCube:
  1348. case SpvDimRect:
  1349. expected_num_components += 2;
  1350. break;
  1351. case SpvDim3D:
  1352. expected_num_components += 3;
  1353. break;
  1354. default:
  1355. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  1356. << "Image 'Dim' must be 1D, Buffer, 2D, Cube, 3D or Rect";
  1357. }
  1358. if (info.dim == SpvDim1D || info.dim == SpvDim2D || info.dim == SpvDim3D ||
  1359. info.dim == SpvDimCube) {
  1360. if (info.multisampled != 1 && info.sampled != 0 && info.sampled != 2) {
  1361. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  1362. << "Image must have either 'MS'=1 or 'Sampled'=0 or 'Sampled'=2";
  1363. }
  1364. }
  1365. uint32_t result_num_components = _.GetDimension(result_type);
  1366. if (result_num_components != expected_num_components) {
  1367. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  1368. << "Result Type has " << result_num_components << " components, "
  1369. << "but " << expected_num_components << " expected";
  1370. }
  1371. return SPV_SUCCESS;
  1372. }
  1373. spv_result_t ValidateImageQueryFormatOrOrder(ValidationState_t& _,
  1374. const Instruction* inst) {
  1375. if (!_.IsIntScalarType(inst->type_id())) {
  1376. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  1377. << "Expected Result Type to be int scalar type";
  1378. }
  1379. if (_.GetIdOpcode(_.GetOperandTypeId(inst, 2)) != SpvOpTypeImage) {
  1380. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  1381. << "Expected operand to be of type OpTypeImage";
  1382. }
  1383. return SPV_SUCCESS;
  1384. }
  1385. spv_result_t ValidateImageQueryLod(ValidationState_t& _,
  1386. const Instruction* inst) {
  1387. _.function(inst->function()->id())
  1388. ->RegisterExecutionModelLimitation(
  1389. SpvExecutionModelFragment,
  1390. "OpImageQueryLod requires Fragment execution model");
  1391. const uint32_t result_type = inst->type_id();
  1392. if (!_.IsFloatVectorType(result_type)) {
  1393. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  1394. << "Expected Result Type to be float vector type";
  1395. }
  1396. if (_.GetDimension(result_type) != 2) {
  1397. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  1398. << "Expected Result Type to have 2 components";
  1399. }
  1400. const uint32_t image_type = _.GetOperandTypeId(inst, 2);
  1401. if (_.GetIdOpcode(image_type) != SpvOpTypeSampledImage) {
  1402. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  1403. << "Expected Image operand to be of type OpTypeSampledImage";
  1404. }
  1405. ImageTypeInfo info;
  1406. if (!GetImageTypeInfo(_, image_type, &info)) {
  1407. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  1408. << "Corrupt image type definition";
  1409. }
  1410. if (info.dim != SpvDim1D && info.dim != SpvDim2D && info.dim != SpvDim3D &&
  1411. info.dim != SpvDimCube) {
  1412. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  1413. << "Image 'Dim' must be 1D, 2D, 3D or Cube";
  1414. }
  1415. const uint32_t coord_type = _.GetOperandTypeId(inst, 3);
  1416. if (_.HasCapability(SpvCapabilityKernel)) {
  1417. if (!_.IsFloatScalarOrVectorType(coord_type) &&
  1418. !_.IsIntScalarOrVectorType(coord_type)) {
  1419. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  1420. << "Expected Coordinate to be int or float scalar or vector";
  1421. }
  1422. } else {
  1423. if (!_.IsFloatScalarOrVectorType(coord_type)) {
  1424. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  1425. << "Expected Coordinate to be float scalar or vector";
  1426. }
  1427. }
  1428. const uint32_t min_coord_size = GetPlaneCoordSize(info);
  1429. const uint32_t actual_coord_size = _.GetDimension(coord_type);
  1430. if (min_coord_size > actual_coord_size) {
  1431. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  1432. << "Expected Coordinate to have at least " << min_coord_size
  1433. << " components, but given only " << actual_coord_size;
  1434. }
  1435. return SPV_SUCCESS;
  1436. }
  1437. spv_result_t ValidateImageSparseLod(ValidationState_t& _,
  1438. const Instruction* inst) {
  1439. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  1440. << "Instruction reserved for future use, use of this instruction "
  1441. << "is invalid";
  1442. }
  1443. spv_result_t ValidateImageQueryLevelsOrSamples(ValidationState_t& _,
  1444. const Instruction* inst) {
  1445. if (!_.IsIntScalarType(inst->type_id())) {
  1446. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  1447. << "Expected Result Type to be int scalar type";
  1448. }
  1449. const uint32_t image_type = _.GetOperandTypeId(inst, 2);
  1450. if (_.GetIdOpcode(image_type) != SpvOpTypeImage) {
  1451. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  1452. << "Expected Image to be of type OpTypeImage";
  1453. }
  1454. ImageTypeInfo info;
  1455. if (!GetImageTypeInfo(_, image_type, &info)) {
  1456. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  1457. << "Corrupt image type definition";
  1458. }
  1459. const SpvOp opcode = inst->opcode();
  1460. if (opcode == SpvOpImageQueryLevels) {
  1461. if (info.dim != SpvDim1D && info.dim != SpvDim2D && info.dim != SpvDim3D &&
  1462. info.dim != SpvDimCube) {
  1463. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  1464. << "Image 'Dim' must be 1D, 2D, 3D or Cube";
  1465. }
  1466. } else {
  1467. assert(opcode == SpvOpImageQuerySamples);
  1468. if (info.dim != SpvDim2D) {
  1469. return _.diag(SPV_ERROR_INVALID_DATA, inst) << "Image 'Dim' must be 2D";
  1470. }
  1471. if (info.multisampled != 1) {
  1472. return _.diag(SPV_ERROR_INVALID_DATA, inst) << "Image 'MS' must be 1";
  1473. }
  1474. }
  1475. return SPV_SUCCESS;
  1476. }
  1477. spv_result_t ValidateImageSparseTexelsResident(ValidationState_t& _,
  1478. const Instruction* inst) {
  1479. if (!_.IsBoolScalarType(inst->type_id())) {
  1480. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  1481. << "Expected Result Type to be bool scalar type";
  1482. }
  1483. const uint32_t resident_code_type = _.GetOperandTypeId(inst, 2);
  1484. if (!_.IsIntScalarType(resident_code_type)) {
  1485. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  1486. << "Expected Resident Code to be int scalar";
  1487. }
  1488. return SPV_SUCCESS;
  1489. }
  1490. } // namespace
  1491. // Validates correctness of image instructions.
  1492. spv_result_t ImagePass(ValidationState_t& _, const Instruction* inst) {
  1493. const SpvOp opcode = inst->opcode();
  1494. if (IsImplicitLod(opcode)) {
  1495. _.function(inst->function()->id())
  1496. ->RegisterExecutionModelLimitation(
  1497. SpvExecutionModelFragment,
  1498. "ImplicitLod instructions require Fragment execution model");
  1499. }
  1500. switch (opcode) {
  1501. case SpvOpTypeImage:
  1502. return ValidateTypeImage(_, inst);
  1503. case SpvOpTypeSampledImage:
  1504. return ValidateTypeSampledImage(_, inst);
  1505. case SpvOpSampledImage:
  1506. return ValidateSampledImage(_, inst);
  1507. case SpvOpImageTexelPointer:
  1508. return ValidateImageTexelPointer(_, inst);
  1509. case SpvOpImageSampleImplicitLod:
  1510. case SpvOpImageSampleExplicitLod:
  1511. case SpvOpImageSampleProjImplicitLod:
  1512. case SpvOpImageSampleProjExplicitLod:
  1513. case SpvOpImageSparseSampleImplicitLod:
  1514. case SpvOpImageSparseSampleExplicitLod:
  1515. return ValidateImageLod(_, inst);
  1516. case SpvOpImageSampleDrefImplicitLod:
  1517. case SpvOpImageSampleDrefExplicitLod:
  1518. case SpvOpImageSampleProjDrefImplicitLod:
  1519. case SpvOpImageSampleProjDrefExplicitLod:
  1520. case SpvOpImageSparseSampleDrefImplicitLod:
  1521. case SpvOpImageSparseSampleDrefExplicitLod:
  1522. return ValidateImageDrefLod(_, inst);
  1523. case SpvOpImageFetch:
  1524. case SpvOpImageSparseFetch:
  1525. return ValidateImageFetch(_, inst);
  1526. case SpvOpImageGather:
  1527. case SpvOpImageDrefGather:
  1528. case SpvOpImageSparseGather:
  1529. case SpvOpImageSparseDrefGather:
  1530. return ValidateImageGather(_, inst);
  1531. case SpvOpImageRead:
  1532. case SpvOpImageSparseRead:
  1533. return ValidateImageRead(_, inst);
  1534. case SpvOpImageWrite:
  1535. return ValidateImageWrite(_, inst);
  1536. case SpvOpImage:
  1537. return ValidateImage(_, inst);
  1538. case SpvOpImageQueryFormat:
  1539. case SpvOpImageQueryOrder:
  1540. return ValidateImageQueryFormatOrOrder(_, inst);
  1541. case SpvOpImageQuerySizeLod:
  1542. return ValidateImageQuerySizeLod(_, inst);
  1543. case SpvOpImageQuerySize:
  1544. return ValidateImageQuerySize(_, inst);
  1545. case SpvOpImageQueryLod:
  1546. return ValidateImageQueryLod(_, inst);
  1547. case SpvOpImageQueryLevels:
  1548. case SpvOpImageQuerySamples:
  1549. return ValidateImageQueryLevelsOrSamples(_, inst);
  1550. case SpvOpImageSparseSampleProjImplicitLod:
  1551. case SpvOpImageSparseSampleProjExplicitLod:
  1552. case SpvOpImageSparseSampleProjDrefImplicitLod:
  1553. case SpvOpImageSparseSampleProjDrefExplicitLod:
  1554. return ValidateImageSparseLod(_, inst);
  1555. case SpvOpImageSparseTexelsResident:
  1556. return ValidateImageSparseTexelsResident(_, inst);
  1557. default:
  1558. break;
  1559. }
  1560. return SPV_SUCCESS;
  1561. }
  1562. } // namespace val
  1563. } // namespace spvtools