validate_image.cpp 70 KB

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