validate_image.cpp 94 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556
  1. // Copyright (c) 2017 Google Inc.
  2. // Modifications Copyright (C) 2020 Advanced Micro Devices, Inc. All rights
  3. // reserved.
  4. //
  5. // Licensed under the Apache License, Version 2.0 (the "License");
  6. // you may not use this file except in compliance with the License.
  7. // You may obtain a copy of the License at
  8. //
  9. // http://www.apache.org/licenses/LICENSE-2.0
  10. //
  11. // Unless required by applicable law or agreed to in writing, software
  12. // distributed under the License is distributed on an "AS IS" BASIS,
  13. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. // See the License for the specific language governing permissions and
  15. // limitations under the License.
  16. // Validates correctness of image instructions.
  17. #include <string>
  18. #include "source/opcode.h"
  19. #include "source/spirv_constant.h"
  20. #include "source/spirv_target_env.h"
  21. #include "source/util/bitutils.h"
  22. #include "source/val/instruction.h"
  23. #include "source/val/validate.h"
  24. #include "source/val/validate_scopes.h"
  25. #include "source/val/validation_state.h"
  26. namespace spvtools {
  27. namespace val {
  28. namespace {
  29. // Performs compile time check that all spv::ImageOperandsMask::XXX cases are
  30. // handled in this module. If spv::ImageOperandsMask::XXX list changes, this
  31. // function will fail the build. For all other purposes this is a placeholder
  32. // function.
  33. bool CheckAllImageOperandsHandled() {
  34. spv::ImageOperandsMask enum_val = spv::ImageOperandsMask::Bias;
  35. // Some improvised code to prevent the compiler from considering enum_val
  36. // constant and optimizing the switch away.
  37. uint32_t stack_var = 0;
  38. if (reinterpret_cast<uintptr_t>(&stack_var) % 256)
  39. enum_val = spv::ImageOperandsMask::Lod;
  40. switch (enum_val) {
  41. // Please update the validation rules in this module if you are changing
  42. // the list of image operands, and add new enum values to this switch.
  43. case spv::ImageOperandsMask::MaskNone:
  44. return false;
  45. case spv::ImageOperandsMask::Bias:
  46. case spv::ImageOperandsMask::Lod:
  47. case spv::ImageOperandsMask::Grad:
  48. case spv::ImageOperandsMask::ConstOffset:
  49. case spv::ImageOperandsMask::Offset:
  50. case spv::ImageOperandsMask::ConstOffsets:
  51. case spv::ImageOperandsMask::Sample:
  52. case spv::ImageOperandsMask::MinLod:
  53. // TODO(dneto): Support image operands related to the Vulkan memory model.
  54. // https://gitlab.khronos.org/spirv/spirv-tools/issues/32
  55. case spv::ImageOperandsMask::MakeTexelAvailableKHR:
  56. case spv::ImageOperandsMask::MakeTexelVisibleKHR:
  57. case spv::ImageOperandsMask::NonPrivateTexelKHR:
  58. case spv::ImageOperandsMask::VolatileTexelKHR:
  59. case spv::ImageOperandsMask::SignExtend:
  60. case spv::ImageOperandsMask::ZeroExtend:
  61. // TODO(jaebaek): Move this line properly after handling image offsets
  62. // operand. This line temporarily fixes CI failure that
  63. // blocks other PRs.
  64. // https://github.com/KhronosGroup/SPIRV-Tools/issues/4565
  65. case spv::ImageOperandsMask::Offsets:
  66. case spv::ImageOperandsMask::Nontemporal:
  67. return true;
  68. }
  69. return false;
  70. }
  71. // Used by GetImageTypeInfo. See OpTypeImage spec for more information.
  72. struct ImageTypeInfo {
  73. uint32_t sampled_type = 0;
  74. spv::Dim dim = spv::Dim::Max;
  75. uint32_t depth = 0;
  76. uint32_t arrayed = 0;
  77. uint32_t multisampled = 0;
  78. uint32_t sampled = 0;
  79. spv::ImageFormat format = spv::ImageFormat::Max;
  80. spv::AccessQualifier access_qualifier = spv::AccessQualifier::Max;
  81. };
  82. // Provides information on image type. |id| should be object of either
  83. // OpTypeImage or OpTypeSampledImage type. Returns false in case of failure
  84. // (not a valid id, failed to parse the instruction, etc).
  85. bool GetImageTypeInfo(const ValidationState_t& _, uint32_t id,
  86. ImageTypeInfo* info) {
  87. if (!id || !info) return false;
  88. const Instruction* inst = _.FindDef(id);
  89. assert(inst);
  90. if (inst->opcode() == spv::Op::OpTypeSampledImage) {
  91. inst = _.FindDef(inst->word(2));
  92. assert(inst);
  93. }
  94. if (inst->opcode() != spv::Op::OpTypeImage) return false;
  95. const size_t num_words = inst->words().size();
  96. if (num_words != 9 && num_words != 10) return false;
  97. info->sampled_type = inst->word(2);
  98. info->dim = static_cast<spv::Dim>(inst->word(3));
  99. info->depth = inst->word(4);
  100. info->arrayed = inst->word(5);
  101. info->multisampled = inst->word(6);
  102. info->sampled = inst->word(7);
  103. info->format = static_cast<spv::ImageFormat>(inst->word(8));
  104. info->access_qualifier =
  105. num_words < 10 ? spv::AccessQualifier::Max
  106. : static_cast<spv::AccessQualifier>(inst->word(9));
  107. return true;
  108. }
  109. bool IsImplicitLod(spv::Op opcode) {
  110. switch (opcode) {
  111. case spv::Op::OpImageSampleImplicitLod:
  112. case spv::Op::OpImageSampleDrefImplicitLod:
  113. case spv::Op::OpImageSampleProjImplicitLod:
  114. case spv::Op::OpImageSampleProjDrefImplicitLod:
  115. case spv::Op::OpImageSparseSampleImplicitLod:
  116. case spv::Op::OpImageSparseSampleDrefImplicitLod:
  117. case spv::Op::OpImageSparseSampleProjImplicitLod:
  118. case spv::Op::OpImageSparseSampleProjDrefImplicitLod:
  119. return true;
  120. default:
  121. break;
  122. }
  123. return false;
  124. }
  125. bool IsExplicitLod(spv::Op opcode) {
  126. switch (opcode) {
  127. case spv::Op::OpImageSampleExplicitLod:
  128. case spv::Op::OpImageSampleDrefExplicitLod:
  129. case spv::Op::OpImageSampleProjExplicitLod:
  130. case spv::Op::OpImageSampleProjDrefExplicitLod:
  131. case spv::Op::OpImageSparseSampleExplicitLod:
  132. case spv::Op::OpImageSparseSampleDrefExplicitLod:
  133. case spv::Op::OpImageSparseSampleProjExplicitLod:
  134. case spv::Op::OpImageSparseSampleProjDrefExplicitLod:
  135. return true;
  136. default:
  137. break;
  138. }
  139. return false;
  140. }
  141. bool IsValidLodOperand(const ValidationState_t& _, spv::Op opcode) {
  142. switch (opcode) {
  143. case spv::Op::OpImageRead:
  144. case spv::Op::OpImageWrite:
  145. case spv::Op::OpImageSparseRead:
  146. return _.HasCapability(spv::Capability::ImageReadWriteLodAMD);
  147. default:
  148. return IsExplicitLod(opcode);
  149. }
  150. }
  151. bool IsValidGatherLodBiasAMD(const ValidationState_t& _, spv::Op opcode) {
  152. switch (opcode) {
  153. case spv::Op::OpImageGather:
  154. case spv::Op::OpImageSparseGather:
  155. return _.HasCapability(spv::Capability::ImageGatherBiasLodAMD);
  156. default:
  157. break;
  158. }
  159. return false;
  160. }
  161. // Returns true if the opcode is a Image instruction which applies
  162. // homogenous projection to the coordinates.
  163. bool IsProj(spv::Op opcode) {
  164. switch (opcode) {
  165. case spv::Op::OpImageSampleProjImplicitLod:
  166. case spv::Op::OpImageSampleProjDrefImplicitLod:
  167. case spv::Op::OpImageSparseSampleProjImplicitLod:
  168. case spv::Op::OpImageSparseSampleProjDrefImplicitLod:
  169. case spv::Op::OpImageSampleProjExplicitLod:
  170. case spv::Op::OpImageSampleProjDrefExplicitLod:
  171. case spv::Op::OpImageSparseSampleProjExplicitLod:
  172. case spv::Op::OpImageSparseSampleProjDrefExplicitLod:
  173. return true;
  174. default:
  175. break;
  176. }
  177. return false;
  178. }
  179. // Returns the number of components in a coordinate used to access a texel in
  180. // a single plane of an image with the given parameters.
  181. uint32_t GetPlaneCoordSize(const ImageTypeInfo& info) {
  182. uint32_t plane_size = 0;
  183. // If this switch breaks your build, please add new values below.
  184. switch (info.dim) {
  185. case spv::Dim::Dim1D:
  186. case spv::Dim::Buffer:
  187. plane_size = 1;
  188. break;
  189. case spv::Dim::Dim2D:
  190. case spv::Dim::Rect:
  191. case spv::Dim::SubpassData:
  192. case spv::Dim::TileImageDataEXT:
  193. plane_size = 2;
  194. break;
  195. case spv::Dim::Dim3D:
  196. case spv::Dim::Cube:
  197. // For Cube direction vector is used instead of UV.
  198. plane_size = 3;
  199. break;
  200. case spv::Dim::Max:
  201. default:
  202. assert(0);
  203. break;
  204. }
  205. return plane_size;
  206. }
  207. // Returns minimal number of coordinates based on image dim, arrayed and whether
  208. // the instruction uses projection coordinates.
  209. uint32_t GetMinCoordSize(spv::Op opcode, const ImageTypeInfo& info) {
  210. if (info.dim == spv::Dim::Cube &&
  211. (opcode == spv::Op::OpImageRead || opcode == spv::Op::OpImageWrite ||
  212. opcode == spv::Op::OpImageSparseRead)) {
  213. // These opcodes use UV for Cube, not direction vector.
  214. return 3;
  215. }
  216. return GetPlaneCoordSize(info) + info.arrayed + (IsProj(opcode) ? 1 : 0);
  217. }
  218. // Checks ImageOperand bitfield and respective operands.
  219. // word_index is the index of the first word after the image-operand mask word.
  220. spv_result_t ValidateImageOperands(ValidationState_t& _,
  221. const Instruction* inst,
  222. const ImageTypeInfo& info,
  223. uint32_t word_index) {
  224. static const bool kAllImageOperandsHandled = CheckAllImageOperandsHandled();
  225. (void)kAllImageOperandsHandled;
  226. const spv::Op opcode = inst->opcode();
  227. const size_t num_words = inst->words().size();
  228. const bool have_explicit_mask = (word_index - 1 < num_words);
  229. const uint32_t mask = have_explicit_mask ? inst->word(word_index - 1) : 0u;
  230. if (have_explicit_mask) {
  231. // NonPrivate, Volatile, SignExtend, ZeroExtend take no operand words.
  232. const uint32_t mask_bits_having_operands =
  233. mask & ~uint32_t(spv::ImageOperandsMask::NonPrivateTexelKHR |
  234. spv::ImageOperandsMask::VolatileTexelKHR |
  235. spv::ImageOperandsMask::SignExtend |
  236. spv::ImageOperandsMask::ZeroExtend |
  237. spv::ImageOperandsMask::Nontemporal);
  238. size_t expected_num_image_operand_words =
  239. spvtools::utils::CountSetBits(mask_bits_having_operands);
  240. if (mask & uint32_t(spv::ImageOperandsMask::Grad)) {
  241. // Grad uses two words.
  242. ++expected_num_image_operand_words;
  243. }
  244. if (expected_num_image_operand_words != num_words - word_index) {
  245. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  246. << "Number of image operand ids doesn't correspond to the bit "
  247. "mask";
  248. }
  249. } else if (num_words != word_index - 1) {
  250. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  251. << "Number of image operand ids doesn't correspond to the bit mask";
  252. }
  253. if (info.multisampled &
  254. (0 == (mask & uint32_t(spv::ImageOperandsMask::Sample)))) {
  255. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  256. << "Image Operand Sample is required for operation on "
  257. "multi-sampled image";
  258. }
  259. // After this point, only set bits in the image operands mask can cause
  260. // the module to be invalid.
  261. if (mask == 0) return SPV_SUCCESS;
  262. if (spvtools::utils::CountSetBits(
  263. mask & uint32_t(spv::ImageOperandsMask::Offset |
  264. spv::ImageOperandsMask::ConstOffset |
  265. spv::ImageOperandsMask::ConstOffsets |
  266. spv::ImageOperandsMask::Offsets)) > 1) {
  267. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  268. << "Image Operands Offset, ConstOffset, ConstOffsets, Offsets "
  269. "cannot be used together";
  270. }
  271. const bool is_implicit_lod = IsImplicitLod(opcode);
  272. const bool is_explicit_lod = IsExplicitLod(opcode);
  273. const bool is_valid_lod_operand = IsValidLodOperand(_, opcode);
  274. const bool is_valid_gather_lod_bias_amd = IsValidGatherLodBiasAMD(_, opcode);
  275. // The checks should be done in the order of definition of OperandImage.
  276. if (mask & uint32_t(spv::ImageOperandsMask::Bias)) {
  277. if (!is_implicit_lod && !is_valid_gather_lod_bias_amd) {
  278. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  279. << "Image Operand Bias can only be used with ImplicitLod opcodes";
  280. }
  281. const uint32_t type_id = _.GetTypeId(inst->word(word_index++));
  282. if (!_.IsFloatScalarType(type_id)) {
  283. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  284. << "Expected Image Operand Bias to be float scalar";
  285. }
  286. if (info.dim != spv::Dim::Dim1D && info.dim != spv::Dim::Dim2D &&
  287. info.dim != spv::Dim::Dim3D && info.dim != spv::Dim::Cube) {
  288. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  289. << "Image Operand Bias requires 'Dim' parameter to be 1D, 2D, 3D "
  290. "or Cube";
  291. }
  292. // Multisampled is already checked.
  293. }
  294. if (mask & uint32_t(spv::ImageOperandsMask::Lod)) {
  295. if (!is_valid_lod_operand && opcode != spv::Op::OpImageFetch &&
  296. opcode != spv::Op::OpImageSparseFetch &&
  297. !is_valid_gather_lod_bias_amd) {
  298. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  299. << "Image Operand Lod can only be used with ExplicitLod opcodes "
  300. << "and OpImageFetch";
  301. }
  302. if (mask & uint32_t(spv::ImageOperandsMask::Grad)) {
  303. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  304. << "Image Operand bits Lod and Grad cannot be set at the same "
  305. "time";
  306. }
  307. const uint32_t type_id = _.GetTypeId(inst->word(word_index++));
  308. if (is_explicit_lod || is_valid_gather_lod_bias_amd) {
  309. if (!_.IsFloatScalarType(type_id)) {
  310. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  311. << "Expected Image Operand Lod to be float scalar when used "
  312. << "with ExplicitLod";
  313. }
  314. } else {
  315. if (!_.IsIntScalarType(type_id)) {
  316. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  317. << "Expected Image Operand Lod to be int scalar when used with "
  318. << "OpImageFetch";
  319. }
  320. }
  321. if (info.dim != spv::Dim::Dim1D && info.dim != spv::Dim::Dim2D &&
  322. info.dim != spv::Dim::Dim3D && info.dim != spv::Dim::Cube) {
  323. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  324. << "Image Operand Lod requires 'Dim' parameter to be 1D, 2D, 3D "
  325. "or Cube";
  326. }
  327. // Multisampled is already checked.
  328. }
  329. if (mask & uint32_t(spv::ImageOperandsMask::Grad)) {
  330. if (!is_explicit_lod) {
  331. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  332. << "Image Operand Grad can only be used with ExplicitLod opcodes";
  333. }
  334. const uint32_t dx_type_id = _.GetTypeId(inst->word(word_index++));
  335. const uint32_t dy_type_id = _.GetTypeId(inst->word(word_index++));
  336. if (!_.IsFloatScalarOrVectorType(dx_type_id) ||
  337. !_.IsFloatScalarOrVectorType(dy_type_id)) {
  338. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  339. << "Expected both Image Operand Grad ids to be float scalars or "
  340. << "vectors";
  341. }
  342. const uint32_t plane_size = GetPlaneCoordSize(info);
  343. const uint32_t dx_size = _.GetDimension(dx_type_id);
  344. const uint32_t dy_size = _.GetDimension(dy_type_id);
  345. if (plane_size != dx_size) {
  346. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  347. << "Expected Image Operand Grad dx to have " << plane_size
  348. << " components, but given " << dx_size;
  349. }
  350. if (plane_size != dy_size) {
  351. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  352. << "Expected Image Operand Grad dy to have " << plane_size
  353. << " components, but given " << dy_size;
  354. }
  355. // Multisampled is already checked.
  356. }
  357. if (mask & uint32_t(spv::ImageOperandsMask::ConstOffset)) {
  358. if (info.dim == spv::Dim::Cube) {
  359. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  360. << "Image Operand ConstOffset cannot be used with Cube Image "
  361. "'Dim'";
  362. }
  363. const uint32_t id = inst->word(word_index++);
  364. const uint32_t type_id = _.GetTypeId(id);
  365. if (!_.IsIntScalarOrVectorType(type_id)) {
  366. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  367. << "Expected Image Operand ConstOffset to be int scalar or "
  368. << "vector";
  369. }
  370. if (!spvOpcodeIsConstant(_.GetIdOpcode(id))) {
  371. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  372. << "Expected Image Operand ConstOffset to be a const object";
  373. }
  374. const uint32_t plane_size = GetPlaneCoordSize(info);
  375. const uint32_t offset_size = _.GetDimension(type_id);
  376. if (plane_size != offset_size) {
  377. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  378. << "Expected Image Operand ConstOffset to have " << plane_size
  379. << " components, but given " << offset_size;
  380. }
  381. }
  382. if (mask & uint32_t(spv::ImageOperandsMask::Offset)) {
  383. if (info.dim == spv::Dim::Cube) {
  384. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  385. << "Image Operand Offset cannot be used with Cube Image 'Dim'";
  386. }
  387. const uint32_t id = inst->word(word_index++);
  388. const uint32_t type_id = _.GetTypeId(id);
  389. if (!_.IsIntScalarOrVectorType(type_id)) {
  390. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  391. << "Expected Image Operand Offset to be int scalar or "
  392. << "vector";
  393. }
  394. const uint32_t plane_size = GetPlaneCoordSize(info);
  395. const uint32_t offset_size = _.GetDimension(type_id);
  396. if (plane_size != offset_size) {
  397. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  398. << "Expected Image Operand Offset to have " << plane_size
  399. << " components, but given " << offset_size;
  400. }
  401. if (!_.options()->before_hlsl_legalization &&
  402. spvIsVulkanEnv(_.context()->target_env) &&
  403. !_.options()->allow_offset_texture_operand) {
  404. if (opcode != spv::Op::OpImageGather &&
  405. opcode != spv::Op::OpImageDrefGather &&
  406. opcode != spv::Op::OpImageSparseGather &&
  407. opcode != spv::Op::OpImageSparseDrefGather) {
  408. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  409. << _.VkErrorID(10213)
  410. << "Image Operand Offset can only be used with "
  411. "OpImage*Gather operations";
  412. }
  413. }
  414. }
  415. if (mask & uint32_t(spv::ImageOperandsMask::ConstOffsets)) {
  416. if (opcode != spv::Op::OpImageGather &&
  417. opcode != spv::Op::OpImageDrefGather &&
  418. opcode != spv::Op::OpImageSparseGather &&
  419. opcode != spv::Op::OpImageSparseDrefGather) {
  420. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  421. << "Image Operand ConstOffsets can only be used with "
  422. "OpImageGather and OpImageDrefGather";
  423. }
  424. if (info.dim == spv::Dim::Cube) {
  425. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  426. << "Image Operand ConstOffsets cannot be used with Cube Image "
  427. "'Dim'";
  428. }
  429. const uint32_t id = inst->word(word_index++);
  430. const uint32_t type_id = _.GetTypeId(id);
  431. const Instruction* type_inst = _.FindDef(type_id);
  432. assert(type_inst);
  433. if (type_inst->opcode() != spv::Op::OpTypeArray) {
  434. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  435. << "Expected Image Operand ConstOffsets to be an array of size 4";
  436. }
  437. uint64_t array_size = 0;
  438. if (!_.EvalConstantValUint64(type_inst->word(3), &array_size)) {
  439. assert(0 && "Array type definition is corrupt");
  440. }
  441. if (array_size != 4) {
  442. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  443. << "Expected Image Operand ConstOffsets to be an array of size 4";
  444. }
  445. const uint32_t component_type = type_inst->word(2);
  446. if (!_.IsIntVectorType(component_type) ||
  447. _.GetDimension(component_type) != 2) {
  448. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  449. << "Expected Image Operand ConstOffsets array components to be "
  450. "int vectors of size 2";
  451. }
  452. if (!spvOpcodeIsConstant(_.GetIdOpcode(id))) {
  453. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  454. << "Expected Image Operand ConstOffsets to be a const object";
  455. }
  456. }
  457. if (mask & uint32_t(spv::ImageOperandsMask::Sample)) {
  458. if (opcode != spv::Op::OpImageFetch && opcode != spv::Op::OpImageRead &&
  459. opcode != spv::Op::OpImageWrite &&
  460. opcode != spv::Op::OpImageSparseFetch &&
  461. opcode != spv::Op::OpImageSparseRead) {
  462. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  463. << "Image Operand Sample can only be used with OpImageFetch, "
  464. << "OpImageRead, OpImageWrite, OpImageSparseFetch and "
  465. << "OpImageSparseRead";
  466. }
  467. if (info.multisampled == 0) {
  468. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  469. << "Image Operand Sample requires non-zero 'MS' parameter";
  470. }
  471. const uint32_t type_id = _.GetTypeId(inst->word(word_index++));
  472. if (!_.IsIntScalarType(type_id)) {
  473. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  474. << "Expected Image Operand Sample to be int scalar";
  475. }
  476. }
  477. if (mask & uint32_t(spv::ImageOperandsMask::MinLod)) {
  478. if (!is_implicit_lod && !(mask & uint32_t(spv::ImageOperandsMask::Grad))) {
  479. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  480. << "Image Operand MinLod can only be used with ImplicitLod "
  481. << "opcodes or together with Image Operand Grad";
  482. }
  483. const uint32_t type_id = _.GetTypeId(inst->word(word_index++));
  484. if (!_.IsFloatScalarType(type_id)) {
  485. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  486. << "Expected Image Operand MinLod to be float scalar";
  487. }
  488. if (info.dim != spv::Dim::Dim1D && info.dim != spv::Dim::Dim2D &&
  489. info.dim != spv::Dim::Dim3D && info.dim != spv::Dim::Cube) {
  490. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  491. << "Image Operand MinLod requires 'Dim' parameter to be 1D, 2D, "
  492. "3D or Cube";
  493. }
  494. if (info.multisampled != 0) {
  495. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  496. << "Image Operand MinLod requires 'MS' parameter to be 0";
  497. }
  498. }
  499. if (mask & uint32_t(spv::ImageOperandsMask::MakeTexelAvailableKHR)) {
  500. // Checked elsewhere: capability and memory model are correct.
  501. if (opcode != spv::Op::OpImageWrite) {
  502. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  503. << "Image Operand MakeTexelAvailableKHR can only be used with Op"
  504. << spvOpcodeString(spv::Op::OpImageWrite) << ": Op"
  505. << spvOpcodeString(opcode);
  506. }
  507. if (!(mask & uint32_t(spv::ImageOperandsMask::NonPrivateTexelKHR))) {
  508. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  509. << "Image Operand MakeTexelAvailableKHR requires "
  510. "NonPrivateTexelKHR is also specified: Op"
  511. << spvOpcodeString(opcode);
  512. }
  513. const auto available_scope = inst->word(word_index++);
  514. if (auto error = ValidateMemoryScope(_, inst, available_scope))
  515. return error;
  516. }
  517. if (mask & uint32_t(spv::ImageOperandsMask::MakeTexelVisibleKHR)) {
  518. // Checked elsewhere: capability and memory model are correct.
  519. if (opcode != spv::Op::OpImageRead &&
  520. opcode != spv::Op::OpImageSparseRead) {
  521. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  522. << "Image Operand MakeTexelVisibleKHR can only be used with Op"
  523. << spvOpcodeString(spv::Op::OpImageRead) << " or Op"
  524. << spvOpcodeString(spv::Op::OpImageSparseRead) << ": Op"
  525. << spvOpcodeString(opcode);
  526. }
  527. if (!(mask & uint32_t(spv::ImageOperandsMask::NonPrivateTexelKHR))) {
  528. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  529. << "Image Operand MakeTexelVisibleKHR requires NonPrivateTexelKHR "
  530. "is also specified: Op"
  531. << spvOpcodeString(opcode);
  532. }
  533. const auto visible_scope = inst->word(word_index++);
  534. if (auto error = ValidateMemoryScope(_, inst, visible_scope)) return error;
  535. }
  536. if (mask & uint32_t(spv::ImageOperandsMask::SignExtend)) {
  537. // Checked elsewhere: SPIR-V 1.4 version or later.
  538. // "The texel value is converted to the target value via sign extension.
  539. // Only valid when the texel type is a scalar or vector of integer type."
  540. //
  541. // We don't have enough information to know what the texel type is.
  542. // In OpenCL, knowledge is deferred until runtime: the image SampledType is
  543. // void, and the Format is Unknown.
  544. // In Vulkan, the texel type is only known in all cases by the pipeline
  545. // setup.
  546. }
  547. if (mask & uint32_t(spv::ImageOperandsMask::ZeroExtend)) {
  548. // Checked elsewhere: SPIR-V 1.4 version or later.
  549. // "The texel value is converted to the target value via zero extension.
  550. // Only valid when the texel type is a scalar or vector of integer type."
  551. //
  552. // We don't have enough information to know what the texel type is.
  553. // In OpenCL, knowledge is deferred until runtime: the image SampledType is
  554. // void, and the Format is Unknown.
  555. // In Vulkan, the texel type is only known in all cases by the pipeline
  556. // setup.
  557. }
  558. if (mask & uint32_t(spv::ImageOperandsMask::Offsets)) {
  559. // TODO: add validation
  560. }
  561. if (mask & uint32_t(spv::ImageOperandsMask::Nontemporal)) {
  562. // Checked elsewhere: SPIR-V 1.6 version or later.
  563. }
  564. return SPV_SUCCESS;
  565. }
  566. // Validate OpImage*Proj* instructions
  567. spv_result_t ValidateImageProj(ValidationState_t& _, const Instruction* inst,
  568. const ImageTypeInfo& info) {
  569. if (info.dim != spv::Dim::Dim1D && info.dim != spv::Dim::Dim2D &&
  570. info.dim != spv::Dim::Dim3D && info.dim != spv::Dim::Rect) {
  571. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  572. << "Expected Image 'Dim' parameter to be 1D, 2D, 3D or Rect";
  573. }
  574. if (info.multisampled != 0) {
  575. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  576. << "Expected Image 'MS' parameter to be 0";
  577. }
  578. if (info.arrayed != 0) {
  579. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  580. << "Expected Image 'arrayed' parameter to be 0";
  581. }
  582. return SPV_SUCCESS;
  583. }
  584. // Validate OpImage*Read and OpImage*Write instructions
  585. spv_result_t ValidateImageReadWrite(ValidationState_t& _,
  586. const Instruction* inst,
  587. const ImageTypeInfo& info) {
  588. if (info.sampled == 2) {
  589. if (info.dim == spv::Dim::Dim1D &&
  590. !_.HasCapability(spv::Capability::Image1D)) {
  591. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  592. << "Capability Image1D is required to access storage image";
  593. } else if (info.dim == spv::Dim::Rect &&
  594. !_.HasCapability(spv::Capability::ImageRect)) {
  595. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  596. << "Capability ImageRect is required to access storage image";
  597. } else if (info.dim == spv::Dim::Buffer &&
  598. !_.HasCapability(spv::Capability::ImageBuffer)) {
  599. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  600. << "Capability ImageBuffer is required to access storage image";
  601. } else if (info.dim == spv::Dim::Cube && info.arrayed == 1 &&
  602. !_.HasCapability(spv::Capability::ImageCubeArray)) {
  603. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  604. << "Capability ImageCubeArray is required to access "
  605. << "storage image";
  606. }
  607. if (info.multisampled == 1 && info.arrayed == 1 && info.sampled == 2 &&
  608. !_.HasCapability(spv::Capability::ImageMSArray)) {
  609. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  610. << "Capability ImageMSArray is required to access storage "
  611. << "image";
  612. }
  613. } else if (info.sampled != 0) {
  614. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  615. << "Expected Image 'Sampled' parameter to be 0 or 2";
  616. }
  617. return SPV_SUCCESS;
  618. }
  619. // Returns true if opcode is *ImageSparse*, false otherwise.
  620. bool IsSparse(spv::Op opcode) {
  621. switch (opcode) {
  622. case spv::Op::OpImageSparseSampleImplicitLod:
  623. case spv::Op::OpImageSparseSampleExplicitLod:
  624. case spv::Op::OpImageSparseSampleDrefImplicitLod:
  625. case spv::Op::OpImageSparseSampleDrefExplicitLod:
  626. case spv::Op::OpImageSparseSampleProjImplicitLod:
  627. case spv::Op::OpImageSparseSampleProjExplicitLod:
  628. case spv::Op::OpImageSparseSampleProjDrefImplicitLod:
  629. case spv::Op::OpImageSparseSampleProjDrefExplicitLod:
  630. case spv::Op::OpImageSparseFetch:
  631. case spv::Op::OpImageSparseGather:
  632. case spv::Op::OpImageSparseDrefGather:
  633. case spv::Op::OpImageSparseTexelsResident:
  634. case spv::Op::OpImageSparseRead: {
  635. return true;
  636. }
  637. default: { return false; }
  638. }
  639. return false;
  640. }
  641. // Checks sparse image opcode result type and returns the second struct member.
  642. // Returns inst.type_id for non-sparse image opcodes.
  643. // Not valid for sparse image opcodes which do not return a struct.
  644. spv_result_t GetActualResultType(ValidationState_t& _, const Instruction* inst,
  645. uint32_t* actual_result_type) {
  646. const spv::Op opcode = inst->opcode();
  647. if (IsSparse(opcode)) {
  648. const Instruction* const type_inst = _.FindDef(inst->type_id());
  649. assert(type_inst);
  650. if (!type_inst || type_inst->opcode() != spv::Op::OpTypeStruct) {
  651. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  652. << "Expected Result Type to be OpTypeStruct";
  653. }
  654. if (type_inst->words().size() != 4 ||
  655. !_.IsIntScalarType(type_inst->word(2))) {
  656. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  657. << "Expected Result Type to be a struct containing an int "
  658. "scalar and a texel";
  659. }
  660. *actual_result_type = type_inst->word(3);
  661. } else {
  662. *actual_result_type = inst->type_id();
  663. }
  664. return SPV_SUCCESS;
  665. }
  666. // Returns a string describing actual result type of an opcode.
  667. // Not valid for sparse image opcodes which do not return a struct.
  668. const char* GetActualResultTypeStr(spv::Op opcode) {
  669. if (IsSparse(opcode)) return "Result Type's second member";
  670. return "Result Type";
  671. }
  672. spv_result_t ValidateTypeImage(ValidationState_t& _, const Instruction* inst) {
  673. assert(inst->type_id() == 0);
  674. ImageTypeInfo info;
  675. if (!GetImageTypeInfo(_, inst->word(1), &info)) {
  676. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  677. << "Corrupt image type definition";
  678. }
  679. if (_.IsIntScalarType(info.sampled_type) &&
  680. (64 == _.GetBitWidth(info.sampled_type)) &&
  681. !_.HasCapability(spv::Capability::Int64ImageEXT)) {
  682. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  683. << "Capability Int64ImageEXT is required when using Sampled Type of "
  684. "64-bit int";
  685. }
  686. const auto target_env = _.context()->target_env;
  687. if (spvIsVulkanEnv(target_env)) {
  688. if ((!_.IsFloatScalarType(info.sampled_type) &&
  689. !_.IsIntScalarType(info.sampled_type)) ||
  690. ((32 != _.GetBitWidth(info.sampled_type)) &&
  691. (64 != _.GetBitWidth(info.sampled_type))) ||
  692. ((64 == _.GetBitWidth(info.sampled_type)) &&
  693. _.IsFloatScalarType(info.sampled_type))) {
  694. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  695. << _.VkErrorID(4656)
  696. << "Expected Sampled Type to be a 32-bit int, 64-bit int or "
  697. "32-bit float scalar type for Vulkan environment";
  698. }
  699. } else if (spvIsOpenCLEnv(target_env)) {
  700. if (!_.IsVoidType(info.sampled_type)) {
  701. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  702. << "Sampled Type must be OpTypeVoid in the OpenCL environment.";
  703. }
  704. } else {
  705. const spv::Op sampled_type_opcode = _.GetIdOpcode(info.sampled_type);
  706. if (sampled_type_opcode != spv::Op::OpTypeVoid &&
  707. sampled_type_opcode != spv::Op::OpTypeInt &&
  708. sampled_type_opcode != spv::Op::OpTypeFloat) {
  709. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  710. << "Expected Sampled Type to be either void or"
  711. << " numerical scalar type";
  712. }
  713. }
  714. // Universal checks on image type operands
  715. // Dim and Format and Access Qualifier are checked elsewhere.
  716. if (info.depth > 2) {
  717. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  718. << "Invalid Depth " << info.depth << " (must be 0, 1 or 2)";
  719. }
  720. if (info.arrayed > 1) {
  721. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  722. << "Invalid Arrayed " << info.arrayed << " (must be 0 or 1)";
  723. }
  724. if (info.multisampled > 1) {
  725. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  726. << "Invalid MS " << info.multisampled << " (must be 0 or 1)";
  727. }
  728. if (info.sampled > 2) {
  729. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  730. << "Invalid Sampled " << info.sampled << " (must be 0, 1 or 2)";
  731. }
  732. if (info.dim == spv::Dim::SubpassData) {
  733. if (info.sampled != 2) {
  734. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  735. << _.VkErrorID(6214) << "Dim SubpassData requires Sampled to be 2";
  736. }
  737. if (info.format != spv::ImageFormat::Unknown) {
  738. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  739. << "Dim SubpassData requires format Unknown";
  740. }
  741. } else if (info.dim == spv::Dim::TileImageDataEXT) {
  742. if (_.IsVoidType(info.sampled_type)) {
  743. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  744. << "Dim TileImageDataEXT requires Sampled Type to be not "
  745. "OpTypeVoid";
  746. }
  747. if (info.sampled != 2) {
  748. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  749. << "Dim TileImageDataEXT requires Sampled to be 2";
  750. }
  751. if (info.format != spv::ImageFormat::Unknown) {
  752. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  753. << "Dim TileImageDataEXT requires format Unknown";
  754. }
  755. if (info.depth != 0) {
  756. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  757. << "Dim TileImageDataEXT requires Depth to be 0";
  758. }
  759. if (info.arrayed != 0) {
  760. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  761. << "Dim TileImageDataEXT requires Arrayed to be 0";
  762. }
  763. } else {
  764. if (info.multisampled && (info.sampled == 2) &&
  765. !_.HasCapability(spv::Capability::StorageImageMultisample)) {
  766. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  767. << "Capability StorageImageMultisample is required when using "
  768. "multisampled storage image";
  769. }
  770. }
  771. if (spvIsOpenCLEnv(target_env)) {
  772. if ((info.arrayed == 1) && (info.dim != spv::Dim::Dim1D) &&
  773. (info.dim != spv::Dim::Dim2D)) {
  774. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  775. << "In the OpenCL environment, Arrayed may only be set to 1 "
  776. << "when Dim is either 1D or 2D.";
  777. }
  778. if (info.multisampled != 0) {
  779. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  780. << "MS must be 0 in the OpenCL environment.";
  781. }
  782. if (info.sampled != 0) {
  783. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  784. << "Sampled must be 0 in the OpenCL environment.";
  785. }
  786. if (info.access_qualifier == spv::AccessQualifier::Max) {
  787. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  788. << "In the OpenCL environment, the optional Access Qualifier"
  789. << " must be present.";
  790. }
  791. }
  792. if (spvIsVulkanEnv(target_env)) {
  793. if (info.sampled == 0) {
  794. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  795. << _.VkErrorID(4657)
  796. << "Sampled must be 1 or 2 in the Vulkan environment.";
  797. }
  798. if (info.dim == spv::Dim::SubpassData && info.arrayed != 0) {
  799. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  800. << _.VkErrorID(6214)
  801. << "Dim SubpassData requires Arrayed to be 0 in the Vulkan "
  802. "environment";
  803. }
  804. if (info.dim == spv::Dim::Rect) {
  805. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  806. << _.VkErrorID(9638)
  807. << "Dim must not be Rect in the Vulkan environment";
  808. }
  809. }
  810. return SPV_SUCCESS;
  811. }
  812. spv_result_t ValidateTypeSampledImage(ValidationState_t& _,
  813. const Instruction* inst) {
  814. const uint32_t image_type = inst->word(2);
  815. if (_.GetIdOpcode(image_type) != spv::Op::OpTypeImage) {
  816. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  817. << "Expected Image to be of type OpTypeImage";
  818. }
  819. ImageTypeInfo info;
  820. if (!GetImageTypeInfo(_, image_type, &info)) {
  821. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  822. << "Corrupt image type definition";
  823. }
  824. // OpenCL requires Sampled=0, checked elsewhere.
  825. // Vulkan uses the Sampled=1 case.
  826. // If Dim is TileImageDataEXT, Sampled must be 2 and this is validated
  827. // elsewhere.
  828. if ((info.sampled != 0) && (info.sampled != 1)) {
  829. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  830. << _.VkErrorID(4657)
  831. << "Sampled image type requires an image type with \"Sampled\" "
  832. "operand set to 0 or 1";
  833. }
  834. // This covers both OpTypeSampledImage and OpSampledImage.
  835. if (_.version() >= SPV_SPIRV_VERSION_WORD(1, 6) &&
  836. info.dim == spv::Dim::Buffer) {
  837. return _.diag(SPV_ERROR_INVALID_ID, inst)
  838. << "In SPIR-V 1.6 or later, sampled image dimension must not be "
  839. "Buffer";
  840. }
  841. return SPV_SUCCESS;
  842. }
  843. bool IsAllowedSampledImageOperand(spv::Op opcode, ValidationState_t& _) {
  844. switch (opcode) {
  845. case spv::Op::OpSampledImage:
  846. case spv::Op::OpImageSampleImplicitLod:
  847. case spv::Op::OpImageSampleExplicitLod:
  848. case spv::Op::OpImageSampleDrefImplicitLod:
  849. case spv::Op::OpImageSampleDrefExplicitLod:
  850. case spv::Op::OpImageSampleProjImplicitLod:
  851. case spv::Op::OpImageSampleProjExplicitLod:
  852. case spv::Op::OpImageSampleProjDrefImplicitLod:
  853. case spv::Op::OpImageSampleProjDrefExplicitLod:
  854. case spv::Op::OpImageGather:
  855. case spv::Op::OpImageDrefGather:
  856. case spv::Op::OpImage:
  857. case spv::Op::OpImageQueryLod:
  858. case spv::Op::OpImageSparseSampleImplicitLod:
  859. case spv::Op::OpImageSparseSampleExplicitLod:
  860. case spv::Op::OpImageSparseSampleDrefImplicitLod:
  861. case spv::Op::OpImageSparseSampleDrefExplicitLod:
  862. case spv::Op::OpImageSparseGather:
  863. case spv::Op::OpImageSparseDrefGather:
  864. case spv::Op::OpCopyObject:
  865. case spv::Op::OpImageSampleWeightedQCOM:
  866. case spv::Op::OpImageBoxFilterQCOM:
  867. case spv::Op::OpImageBlockMatchSSDQCOM:
  868. case spv::Op::OpImageBlockMatchSADQCOM:
  869. case spv::Op::OpImageBlockMatchWindowSADQCOM:
  870. case spv::Op::OpImageBlockMatchWindowSSDQCOM:
  871. case spv::Op::OpImageBlockMatchGatherSADQCOM:
  872. case spv::Op::OpImageBlockMatchGatherSSDQCOM:
  873. case spv::Op::OpImageSampleFootprintNV:
  874. return true;
  875. case spv::Op::OpStore:
  876. if (_.HasCapability(spv::Capability::BindlessTextureNV)) return true;
  877. return false;
  878. default:
  879. return false;
  880. }
  881. }
  882. spv_result_t ValidateSampledImage(ValidationState_t& _,
  883. const Instruction* inst) {
  884. auto type_inst = _.FindDef(inst->type_id());
  885. if (type_inst->opcode() != spv::Op::OpTypeSampledImage) {
  886. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  887. << "Expected Result Type to be OpTypeSampledImage.";
  888. }
  889. const uint32_t image_type = _.GetOperandTypeId(inst, 2);
  890. if (_.GetIdOpcode(image_type) != spv::Op::OpTypeImage) {
  891. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  892. << "Expected Image to be of type OpTypeImage.";
  893. }
  894. ImageTypeInfo info;
  895. if (!GetImageTypeInfo(_, image_type, &info)) {
  896. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  897. << "Corrupt image type definition";
  898. }
  899. // Image operands must match except for depth.
  900. auto sampled_image_id = type_inst->GetOperandAs<uint32_t>(1);
  901. if (sampled_image_id != image_type) {
  902. ImageTypeInfo sampled_info;
  903. if (!GetImageTypeInfo(_, sampled_image_id, &sampled_info)) {
  904. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  905. << "Corrupt image type definition";
  906. }
  907. if (info.sampled_type != sampled_info.sampled_type ||
  908. info.dim != sampled_info.dim || info.arrayed != sampled_info.arrayed ||
  909. info.multisampled != sampled_info.multisampled ||
  910. info.sampled != sampled_info.sampled ||
  911. info.format != sampled_info.format ||
  912. info.access_qualifier != sampled_info.access_qualifier) {
  913. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  914. << "Image operands must match result image operands except for "
  915. "depth";
  916. }
  917. }
  918. if (spvIsVulkanEnv(_.context()->target_env)) {
  919. if (info.sampled != 1) {
  920. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  921. << _.VkErrorID(6671)
  922. << "Expected Image 'Sampled' parameter to be 1 for Vulkan "
  923. "environment.";
  924. }
  925. } else {
  926. if (info.sampled != 0 && info.sampled != 1) {
  927. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  928. << "Expected Image 'Sampled' parameter to be 0 or 1";
  929. }
  930. }
  931. if (info.dim == spv::Dim::SubpassData) {
  932. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  933. << "Expected Image 'Dim' parameter to be not SubpassData.";
  934. }
  935. if (_.GetIdOpcode(_.GetOperandTypeId(inst, 3)) != spv::Op::OpTypeSampler) {
  936. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  937. << "Expected Sampler to be of type OpTypeSampler";
  938. }
  939. // We need to validate 2 things:
  940. // * All OpSampledImage instructions must be in the same block in which their
  941. // Result <id> are consumed.
  942. // * Result <id> from OpSampledImage instructions must not appear as operands
  943. // to OpPhi instructions or OpSelect instructions, or any instructions other
  944. // than the image lookup and query instructions specified to take an operand
  945. // whose type is OpTypeSampledImage.
  946. std::vector<Instruction*> consumers = _.getSampledImageConsumers(inst->id());
  947. if (!consumers.empty()) {
  948. for (auto consumer_instr : consumers) {
  949. const auto consumer_opcode = consumer_instr->opcode();
  950. if (consumer_instr->block() != inst->block()) {
  951. return _.diag(SPV_ERROR_INVALID_ID, inst)
  952. << "All OpSampledImage instructions must be in the same block "
  953. "in "
  954. "which their Result <id> are consumed. OpSampledImage Result "
  955. "Type <id> "
  956. << _.getIdName(inst->id())
  957. << " has a consumer in a different basic "
  958. "block. The consumer instruction <id> is "
  959. << _.getIdName(consumer_instr->id()) << ".";
  960. }
  961. if (consumer_opcode == spv::Op::OpPhi ||
  962. consumer_opcode == spv::Op::OpSelect) {
  963. return _.diag(SPV_ERROR_INVALID_ID, inst)
  964. << "Result <id> from OpSampledImage instruction must not appear "
  965. "as "
  966. "operands of Op"
  967. << spvOpcodeString(static_cast<spv::Op>(consumer_opcode)) << "."
  968. << " Found result <id> " << _.getIdName(inst->id())
  969. << " as an operand of <id> " << _.getIdName(consumer_instr->id())
  970. << ".";
  971. }
  972. if (!IsAllowedSampledImageOperand(consumer_opcode, _)) {
  973. return _.diag(SPV_ERROR_INVALID_ID, inst)
  974. << "Result <id> from OpSampledImage instruction must not appear "
  975. "as operand for Op"
  976. << spvOpcodeString(static_cast<spv::Op>(consumer_opcode))
  977. << ", since it is not specified as taking an "
  978. << "OpTypeSampledImage."
  979. << " Found result <id> " << _.getIdName(inst->id())
  980. << " as an operand of <id> " << _.getIdName(consumer_instr->id())
  981. << ".";
  982. }
  983. }
  984. }
  985. const Instruction* ld_inst;
  986. {
  987. int t_idx = inst->GetOperandAs<int>(2);
  988. ld_inst = _.FindDef(t_idx);
  989. }
  990. if (ld_inst->opcode() == spv::Op::OpLoad) {
  991. int texture_id = ld_inst->GetOperandAs<int>(2); // variable to load
  992. _.RegisterQCOMImageProcessingTextureConsumer(texture_id, ld_inst, inst);
  993. }
  994. return SPV_SUCCESS;
  995. }
  996. spv_result_t ValidateImageTexelPointer(ValidationState_t& _,
  997. const Instruction* inst) {
  998. const auto result_type = _.FindDef(inst->type_id());
  999. if (result_type->opcode() != spv::Op::OpTypePointer &&
  1000. result_type->opcode() != spv::Op::OpTypeUntypedPointerKHR) {
  1001. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  1002. << "Expected Result Type to be a pointer";
  1003. }
  1004. const auto storage_class = result_type->GetOperandAs<spv::StorageClass>(1);
  1005. if (storage_class != spv::StorageClass::Image) {
  1006. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  1007. << "Expected Result Type to be a pointer whose Storage Class "
  1008. "operand is Image";
  1009. }
  1010. uint32_t ptr_type = 0;
  1011. if (result_type->opcode() == spv::Op::OpTypePointer) {
  1012. ptr_type = result_type->GetOperandAs<uint32_t>(2);
  1013. const auto ptr_opcode = _.GetIdOpcode(ptr_type);
  1014. if (ptr_opcode != spv::Op::OpTypeInt &&
  1015. ptr_opcode != spv::Op::OpTypeFloat &&
  1016. ptr_opcode != spv::Op::OpTypeVoid &&
  1017. !(ptr_opcode == spv::Op::OpTypeVector &&
  1018. _.HasCapability(spv::Capability::AtomicFloat16VectorNV) &&
  1019. _.IsFloat16Vector2Or4Type(ptr_type))) {
  1020. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  1021. << "Expected Result Type to be a pointer whose Type operand "
  1022. "must be a scalar numerical type or OpTypeVoid";
  1023. }
  1024. }
  1025. const auto image_ptr = _.FindDef(_.GetOperandTypeId(inst, 2));
  1026. if (!image_ptr || image_ptr->opcode() != spv::Op::OpTypePointer) {
  1027. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  1028. << "Expected Image to be OpTypePointer";
  1029. }
  1030. const auto image_type = image_ptr->GetOperandAs<uint32_t>(2);
  1031. if (_.GetIdOpcode(image_type) != spv::Op::OpTypeImage) {
  1032. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  1033. << "Expected Image to be OpTypePointer with Type OpTypeImage";
  1034. }
  1035. ImageTypeInfo info;
  1036. if (!GetImageTypeInfo(_, image_type, &info)) {
  1037. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  1038. << "Corrupt image type definition";
  1039. }
  1040. if (result_type->opcode() == spv::Op::OpTypePointer &&
  1041. info.sampled_type != ptr_type &&
  1042. !(_.HasCapability(spv::Capability::AtomicFloat16VectorNV) &&
  1043. _.IsFloat16Vector2Or4Type(ptr_type) &&
  1044. _.GetIdOpcode(info.sampled_type) == spv::Op::OpTypeFloat &&
  1045. ((_.GetDimension(ptr_type) == 2 &&
  1046. info.format == spv::ImageFormat::Rg16f) ||
  1047. (_.GetDimension(ptr_type) == 4 &&
  1048. info.format == spv::ImageFormat::Rgba16f)))) {
  1049. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  1050. << "Expected Image 'Sampled Type' to be the same as the Type "
  1051. "pointed to by Result Type";
  1052. }
  1053. if (info.dim == spv::Dim::SubpassData) {
  1054. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  1055. << "Image Dim SubpassData cannot be used with OpImageTexelPointer";
  1056. }
  1057. if (info.dim == spv::Dim::TileImageDataEXT) {
  1058. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  1059. << "Image Dim TileImageDataEXT cannot be used with "
  1060. "OpImageTexelPointer";
  1061. }
  1062. const uint32_t coord_type = _.GetOperandTypeId(inst, 3);
  1063. if (!coord_type || !_.IsIntScalarOrVectorType(coord_type)) {
  1064. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  1065. << "Expected Coordinate to be integer scalar or vector";
  1066. }
  1067. uint32_t expected_coord_size = 0;
  1068. if (info.arrayed == 0) {
  1069. expected_coord_size = GetPlaneCoordSize(info);
  1070. } else if (info.arrayed == 1) {
  1071. switch (info.dim) {
  1072. case spv::Dim::Dim1D:
  1073. expected_coord_size = 2;
  1074. break;
  1075. case spv::Dim::Cube:
  1076. case spv::Dim::Dim2D:
  1077. expected_coord_size = 3;
  1078. break;
  1079. default:
  1080. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  1081. << "Expected Image 'Dim' must be one of 1D, 2D, or Cube when "
  1082. "Arrayed is 1";
  1083. break;
  1084. }
  1085. }
  1086. const uint32_t actual_coord_size = _.GetDimension(coord_type);
  1087. if (expected_coord_size != actual_coord_size) {
  1088. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  1089. << "Expected Coordinate to have " << expected_coord_size
  1090. << " components, but given " << actual_coord_size;
  1091. }
  1092. const uint32_t sample_type = _.GetOperandTypeId(inst, 4);
  1093. if (!sample_type || !_.IsIntScalarType(sample_type)) {
  1094. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  1095. << "Expected Sample to be integer scalar";
  1096. }
  1097. if (info.multisampled == 0) {
  1098. uint64_t ms = 0;
  1099. if (!_.EvalConstantValUint64(inst->GetOperandAs<uint32_t>(4), &ms) ||
  1100. ms != 0) {
  1101. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  1102. << "Expected Sample for Image with MS 0 to be a valid <id> for "
  1103. "the value 0";
  1104. }
  1105. }
  1106. if (spvIsVulkanEnv(_.context()->target_env)) {
  1107. if ((info.format != spv::ImageFormat::R64i) &&
  1108. (info.format != spv::ImageFormat::R64ui) &&
  1109. (info.format != spv::ImageFormat::R32f) &&
  1110. (info.format != spv::ImageFormat::R32i) &&
  1111. (info.format != spv::ImageFormat::R32ui) &&
  1112. !((info.format == spv::ImageFormat::Rg16f ||
  1113. info.format == spv::ImageFormat::Rgba16f) &&
  1114. _.HasCapability(spv::Capability::AtomicFloat16VectorNV))) {
  1115. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  1116. << _.VkErrorID(4658)
  1117. << "Expected the Image Format in Image to be R64i, R64ui, R32f, "
  1118. "R32i, or R32ui for Vulkan environment";
  1119. }
  1120. }
  1121. return SPV_SUCCESS;
  1122. }
  1123. spv_result_t ValidateImageLod(ValidationState_t& _, const Instruction* inst) {
  1124. const spv::Op opcode = inst->opcode();
  1125. uint32_t actual_result_type = 0;
  1126. if (spv_result_t error = GetActualResultType(_, inst, &actual_result_type)) {
  1127. return error;
  1128. }
  1129. if (!_.IsIntVectorType(actual_result_type) &&
  1130. !_.IsFloatVectorType(actual_result_type)) {
  1131. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  1132. << "Expected " << GetActualResultTypeStr(opcode)
  1133. << " to be int or float vector type";
  1134. }
  1135. if (_.GetDimension(actual_result_type) != 4) {
  1136. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  1137. << "Expected " << GetActualResultTypeStr(opcode)
  1138. << " to have 4 components";
  1139. }
  1140. const uint32_t image_type = _.GetOperandTypeId(inst, 2);
  1141. if (_.GetIdOpcode(image_type) != spv::Op::OpTypeSampledImage) {
  1142. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  1143. << "Expected Sampled Image to be of type OpTypeSampledImage";
  1144. }
  1145. ImageTypeInfo info;
  1146. if (!GetImageTypeInfo(_, image_type, &info)) {
  1147. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  1148. << "Corrupt image type definition";
  1149. }
  1150. if (IsProj(opcode)) {
  1151. if (spv_result_t result = ValidateImageProj(_, inst, info)) return result;
  1152. }
  1153. if (info.multisampled) {
  1154. // When using image operands, the Sample image operand is required if and
  1155. // only if the image is multisampled (MS=1). The Sample image operand is
  1156. // only allowed for fetch, read, and write.
  1157. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  1158. << "Sampling operation is invalid for multisample image";
  1159. }
  1160. if (_.GetIdOpcode(info.sampled_type) != spv::Op::OpTypeVoid) {
  1161. const uint32_t texel_component_type =
  1162. _.GetComponentType(actual_result_type);
  1163. if (texel_component_type != info.sampled_type) {
  1164. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  1165. << "Expected Image 'Sampled Type' to be the same as "
  1166. << GetActualResultTypeStr(opcode) << " components";
  1167. }
  1168. }
  1169. const uint32_t coord_type = _.GetOperandTypeId(inst, 3);
  1170. if ((opcode == spv::Op::OpImageSampleExplicitLod ||
  1171. opcode == spv::Op::OpImageSparseSampleExplicitLod) &&
  1172. _.HasCapability(spv::Capability::Kernel)) {
  1173. if (!_.IsFloatScalarOrVectorType(coord_type) &&
  1174. !_.IsIntScalarOrVectorType(coord_type)) {
  1175. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  1176. << "Expected Coordinate to be int or float scalar or vector";
  1177. }
  1178. } else {
  1179. if (!_.IsFloatScalarOrVectorType(coord_type)) {
  1180. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  1181. << "Expected Coordinate to be float scalar or vector";
  1182. }
  1183. }
  1184. const uint32_t min_coord_size = GetMinCoordSize(opcode, info);
  1185. const uint32_t actual_coord_size = _.GetDimension(coord_type);
  1186. if (min_coord_size > actual_coord_size) {
  1187. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  1188. << "Expected Coordinate to have at least " << min_coord_size
  1189. << " components, but given only " << actual_coord_size;
  1190. }
  1191. const uint32_t mask = inst->words().size() <= 5 ? 0 : inst->word(5);
  1192. if (mask & uint32_t(spv::ImageOperandsMask::ConstOffset)) {
  1193. if (spvIsOpenCLEnv(_.context()->target_env)) {
  1194. if (opcode == spv::Op::OpImageSampleExplicitLod) {
  1195. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  1196. << "ConstOffset image operand not allowed "
  1197. << "in the OpenCL environment.";
  1198. }
  1199. }
  1200. }
  1201. if (spv_result_t result =
  1202. ValidateImageOperands(_, inst, info, /* word_index = */ 6))
  1203. return result;
  1204. return SPV_SUCCESS;
  1205. }
  1206. // Validates anything OpImage*Dref* instruction
  1207. spv_result_t ValidateImageDref(ValidationState_t& _, const Instruction* inst,
  1208. const ImageTypeInfo& info) {
  1209. const uint32_t dref_type = _.GetOperandTypeId(inst, 4);
  1210. if (!_.IsFloatScalarType(dref_type) || _.GetBitWidth(dref_type) != 32) {
  1211. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  1212. << "Expected Dref to be of 32-bit float type";
  1213. }
  1214. if (spvIsVulkanEnv(_.context()->target_env)) {
  1215. if (info.dim == spv::Dim::Dim3D) {
  1216. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  1217. << _.VkErrorID(4777)
  1218. << "In Vulkan, OpImage*Dref* instructions must not use images "
  1219. "with a 3D Dim";
  1220. }
  1221. }
  1222. return SPV_SUCCESS;
  1223. }
  1224. spv_result_t ValidateImageDrefLod(ValidationState_t& _,
  1225. const Instruction* inst) {
  1226. const spv::Op opcode = inst->opcode();
  1227. uint32_t actual_result_type = 0;
  1228. if (spv_result_t error = GetActualResultType(_, inst, &actual_result_type)) {
  1229. return error;
  1230. }
  1231. if (!_.IsIntScalarType(actual_result_type) &&
  1232. !_.IsFloatScalarType(actual_result_type)) {
  1233. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  1234. << "Expected " << GetActualResultTypeStr(opcode)
  1235. << " to be int or float scalar type";
  1236. }
  1237. const uint32_t image_type = _.GetOperandTypeId(inst, 2);
  1238. if (_.GetIdOpcode(image_type) != spv::Op::OpTypeSampledImage) {
  1239. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  1240. << "Expected Sampled Image to be of type OpTypeSampledImage";
  1241. }
  1242. ImageTypeInfo info;
  1243. if (!GetImageTypeInfo(_, image_type, &info)) {
  1244. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  1245. << "Corrupt image type definition";
  1246. }
  1247. if (IsProj(opcode)) {
  1248. if (spv_result_t result = ValidateImageProj(_, inst, info)) return result;
  1249. }
  1250. if (info.multisampled) {
  1251. // When using image operands, the Sample image operand is required if and
  1252. // only if the image is multisampled (MS=1). The Sample image operand is
  1253. // only allowed for fetch, read, and write.
  1254. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  1255. << "Dref sampling operation is invalid for multisample image";
  1256. }
  1257. if (actual_result_type != info.sampled_type) {
  1258. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  1259. << "Expected Image 'Sampled Type' to be the same as "
  1260. << GetActualResultTypeStr(opcode);
  1261. }
  1262. const uint32_t coord_type = _.GetOperandTypeId(inst, 3);
  1263. if (!_.IsFloatScalarOrVectorType(coord_type)) {
  1264. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  1265. << "Expected Coordinate to be float scalar or vector";
  1266. }
  1267. const uint32_t min_coord_size = GetMinCoordSize(opcode, info);
  1268. const uint32_t actual_coord_size = _.GetDimension(coord_type);
  1269. if (min_coord_size > actual_coord_size) {
  1270. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  1271. << "Expected Coordinate to have at least " << min_coord_size
  1272. << " components, but given only " << actual_coord_size;
  1273. }
  1274. if (spv_result_t result = ValidateImageDref(_, inst, info)) return result;
  1275. if (spv_result_t result =
  1276. ValidateImageOperands(_, inst, info, /* word_index = */ 7))
  1277. return result;
  1278. return SPV_SUCCESS;
  1279. }
  1280. spv_result_t ValidateImageFetch(ValidationState_t& _, const Instruction* inst) {
  1281. uint32_t actual_result_type = 0;
  1282. if (spv_result_t error = GetActualResultType(_, inst, &actual_result_type)) {
  1283. return error;
  1284. }
  1285. const spv::Op opcode = inst->opcode();
  1286. if (!_.IsIntVectorType(actual_result_type) &&
  1287. !_.IsFloatVectorType(actual_result_type)) {
  1288. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  1289. << "Expected " << GetActualResultTypeStr(opcode)
  1290. << " to be int or float vector type";
  1291. }
  1292. if (_.GetDimension(actual_result_type) != 4) {
  1293. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  1294. << "Expected " << GetActualResultTypeStr(opcode)
  1295. << " to have 4 components";
  1296. }
  1297. const uint32_t image_type = _.GetOperandTypeId(inst, 2);
  1298. if (_.GetIdOpcode(image_type) != spv::Op::OpTypeImage) {
  1299. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  1300. << "Expected Image to be of type OpTypeImage";
  1301. }
  1302. ImageTypeInfo info;
  1303. if (!GetImageTypeInfo(_, image_type, &info)) {
  1304. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  1305. << "Corrupt image type definition";
  1306. }
  1307. if (_.GetIdOpcode(info.sampled_type) != spv::Op::OpTypeVoid) {
  1308. const uint32_t result_component_type =
  1309. _.GetComponentType(actual_result_type);
  1310. if (result_component_type != info.sampled_type) {
  1311. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  1312. << "Expected Image 'Sampled Type' to be the same as "
  1313. << GetActualResultTypeStr(opcode) << " components";
  1314. }
  1315. }
  1316. if (info.dim == spv::Dim::Cube) {
  1317. return _.diag(SPV_ERROR_INVALID_DATA, inst) << "Image 'Dim' cannot be Cube";
  1318. }
  1319. if (info.sampled != 1) {
  1320. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  1321. << "Expected Image 'Sampled' parameter to be 1";
  1322. }
  1323. const uint32_t coord_type = _.GetOperandTypeId(inst, 3);
  1324. if (!_.IsIntScalarOrVectorType(coord_type)) {
  1325. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  1326. << "Expected Coordinate to be int scalar or vector";
  1327. }
  1328. const uint32_t min_coord_size = GetMinCoordSize(opcode, info);
  1329. const uint32_t actual_coord_size = _.GetDimension(coord_type);
  1330. if (min_coord_size > actual_coord_size) {
  1331. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  1332. << "Expected Coordinate to have at least " << min_coord_size
  1333. << " components, but given only " << actual_coord_size;
  1334. }
  1335. if (spv_result_t result =
  1336. ValidateImageOperands(_, inst, info, /* word_index = */ 6))
  1337. return result;
  1338. return SPV_SUCCESS;
  1339. }
  1340. spv_result_t ValidateImageGather(ValidationState_t& _,
  1341. const Instruction* inst) {
  1342. uint32_t actual_result_type = 0;
  1343. if (spv_result_t error = GetActualResultType(_, inst, &actual_result_type))
  1344. return error;
  1345. const spv::Op opcode = inst->opcode();
  1346. if (!_.IsIntVectorType(actual_result_type) &&
  1347. !_.IsFloatVectorType(actual_result_type)) {
  1348. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  1349. << "Expected " << GetActualResultTypeStr(opcode)
  1350. << " to be int or float vector type";
  1351. }
  1352. if (_.GetDimension(actual_result_type) != 4) {
  1353. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  1354. << "Expected " << GetActualResultTypeStr(opcode)
  1355. << " to have 4 components";
  1356. }
  1357. const uint32_t image_type = _.GetOperandTypeId(inst, 2);
  1358. if (_.GetIdOpcode(image_type) != spv::Op::OpTypeSampledImage) {
  1359. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  1360. << "Expected Sampled Image to be of type OpTypeSampledImage";
  1361. }
  1362. ImageTypeInfo info;
  1363. if (!GetImageTypeInfo(_, image_type, &info)) {
  1364. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  1365. << "Corrupt image type definition";
  1366. }
  1367. if (info.multisampled) {
  1368. // When using image operands, the Sample image operand is required if and
  1369. // only if the image is multisampled (MS=1). The Sample image operand is
  1370. // only allowed for fetch, read, and write.
  1371. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  1372. << "Gather operation is invalid for multisample image";
  1373. }
  1374. if (opcode == spv::Op::OpImageDrefGather ||
  1375. opcode == spv::Op::OpImageSparseDrefGather ||
  1376. _.GetIdOpcode(info.sampled_type) != spv::Op::OpTypeVoid) {
  1377. const uint32_t result_component_type =
  1378. _.GetComponentType(actual_result_type);
  1379. if (result_component_type != info.sampled_type) {
  1380. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  1381. << "Expected Image 'Sampled Type' to be the same as "
  1382. << GetActualResultTypeStr(opcode) << " components";
  1383. }
  1384. }
  1385. if (info.dim != spv::Dim::Dim2D && info.dim != spv::Dim::Cube &&
  1386. info.dim != spv::Dim::Rect) {
  1387. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  1388. << _.VkErrorID(4777)
  1389. << "Expected Image 'Dim' to be 2D, Cube, or Rect";
  1390. }
  1391. const uint32_t coord_type = _.GetOperandTypeId(inst, 3);
  1392. if (!_.IsFloatScalarOrVectorType(coord_type)) {
  1393. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  1394. << "Expected Coordinate to be float scalar or vector";
  1395. }
  1396. const uint32_t min_coord_size = GetMinCoordSize(opcode, info);
  1397. const uint32_t actual_coord_size = _.GetDimension(coord_type);
  1398. if (min_coord_size > actual_coord_size) {
  1399. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  1400. << "Expected Coordinate to have at least " << min_coord_size
  1401. << " components, but given only " << actual_coord_size;
  1402. }
  1403. if (opcode == spv::Op::OpImageGather ||
  1404. opcode == spv::Op::OpImageSparseGather) {
  1405. const uint32_t component = inst->GetOperandAs<uint32_t>(4);
  1406. const uint32_t component_index_type = _.GetTypeId(component);
  1407. if (!_.IsIntScalarType(component_index_type) ||
  1408. _.GetBitWidth(component_index_type) != 32) {
  1409. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  1410. << "Expected Component to be 32-bit int scalar";
  1411. }
  1412. if (spvIsVulkanEnv(_.context()->target_env)) {
  1413. if (!spvOpcodeIsConstant(_.GetIdOpcode(component))) {
  1414. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  1415. << _.VkErrorID(4664)
  1416. << "Expected Component Operand to be a const object for Vulkan "
  1417. "environment";
  1418. }
  1419. }
  1420. } else {
  1421. assert(opcode == spv::Op::OpImageDrefGather ||
  1422. opcode == spv::Op::OpImageSparseDrefGather);
  1423. if (spv_result_t result = ValidateImageDref(_, inst, info)) return result;
  1424. }
  1425. if (spv_result_t result =
  1426. ValidateImageOperands(_, inst, info, /* word_index = */ 7))
  1427. return result;
  1428. return SPV_SUCCESS;
  1429. }
  1430. spv_result_t ValidateImageRead(ValidationState_t& _, const Instruction* inst) {
  1431. const spv::Op opcode = inst->opcode();
  1432. uint32_t actual_result_type = 0;
  1433. if (spv_result_t error = GetActualResultType(_, inst, &actual_result_type)) {
  1434. return error;
  1435. }
  1436. if (!_.IsIntScalarOrVectorType(actual_result_type) &&
  1437. !_.IsFloatScalarOrVectorType(actual_result_type)) {
  1438. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  1439. << "Expected " << GetActualResultTypeStr(opcode)
  1440. << " to be int or float scalar or vector type";
  1441. }
  1442. const auto target_env = _.context()->target_env;
  1443. // Vulkan requires the result to be a 4-element int or float
  1444. // vector.
  1445. if (spvIsVulkanEnv(target_env)) {
  1446. if (_.GetDimension(actual_result_type) != 4) {
  1447. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  1448. << _.VkErrorID(4780) << "Expected "
  1449. << GetActualResultTypeStr(opcode) << " to have 4 components";
  1450. }
  1451. } // Check OpenCL below, after we get the image info.
  1452. const uint32_t image_type = _.GetOperandTypeId(inst, 2);
  1453. if (_.GetIdOpcode(image_type) != spv::Op::OpTypeImage) {
  1454. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  1455. << "Expected Image to be of type OpTypeImage";
  1456. }
  1457. ImageTypeInfo info;
  1458. if (!GetImageTypeInfo(_, image_type, &info)) {
  1459. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  1460. << "Corrupt image type definition";
  1461. }
  1462. if (spvIsOpenCLEnv(target_env)) {
  1463. // In OpenCL, a read from a depth image returns a scalar float. In other
  1464. // cases, the result is always a 4-element vector.
  1465. // https://www.khronos.org/registry/OpenCL/specs/3.0-unified/html/OpenCL_Env.html#_data_format_for_reading_and_writing_images
  1466. // https://www.khronos.org/registry/OpenCL/specs/3.0-unified/html/OpenCL_C.html#image-read-and-write-functions
  1467. // The builtins for reading depth images are:
  1468. // float read_imagef(aQual image2d_depth_t image, int2 coord)
  1469. // float read_imagef(aQual image2d_array_depth_t image, int4 coord)
  1470. if (info.depth) {
  1471. if (!_.IsFloatScalarType(actual_result_type)) {
  1472. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  1473. << "Expected " << GetActualResultTypeStr(opcode)
  1474. << " from a depth image read to result in a scalar float value";
  1475. }
  1476. } else {
  1477. if (_.GetDimension(actual_result_type) != 4) {
  1478. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  1479. << "Expected " << GetActualResultTypeStr(opcode)
  1480. << " to have 4 components";
  1481. }
  1482. }
  1483. const uint32_t mask = inst->words().size() <= 5 ? 0 : inst->word(5);
  1484. if (mask & uint32_t(spv::ImageOperandsMask::ConstOffset)) {
  1485. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  1486. << "ConstOffset image operand not allowed "
  1487. << "in the OpenCL environment.";
  1488. }
  1489. }
  1490. if (info.dim == spv::Dim::SubpassData) {
  1491. if (opcode == spv::Op::OpImageSparseRead) {
  1492. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  1493. << "Image Dim SubpassData cannot be used with ImageSparseRead";
  1494. }
  1495. _.function(inst->function()->id())
  1496. ->RegisterExecutionModelLimitation(
  1497. spv::ExecutionModel::Fragment,
  1498. std::string("Dim SubpassData requires Fragment execution model: ") +
  1499. spvOpcodeString(opcode));
  1500. }
  1501. if (info.dim == spv::Dim::TileImageDataEXT) {
  1502. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  1503. << "Image Dim TileImageDataEXT cannot be used with "
  1504. << spvOpcodeString(opcode);
  1505. }
  1506. if (_.GetIdOpcode(info.sampled_type) != spv::Op::OpTypeVoid) {
  1507. const uint32_t result_component_type =
  1508. _.GetComponentType(actual_result_type);
  1509. if (result_component_type != info.sampled_type) {
  1510. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  1511. << "Expected Image 'Sampled Type' to be the same as "
  1512. << GetActualResultTypeStr(opcode) << " components";
  1513. }
  1514. }
  1515. if (spv_result_t result = ValidateImageReadWrite(_, inst, info))
  1516. return result;
  1517. const uint32_t coord_type = _.GetOperandTypeId(inst, 3);
  1518. if (!_.IsIntScalarOrVectorType(coord_type)) {
  1519. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  1520. << "Expected Coordinate to be int scalar or vector";
  1521. }
  1522. const uint32_t min_coord_size = GetMinCoordSize(opcode, info);
  1523. const uint32_t actual_coord_size = _.GetDimension(coord_type);
  1524. if (min_coord_size > actual_coord_size) {
  1525. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  1526. << "Expected Coordinate to have at least " << min_coord_size
  1527. << " components, but given only " << actual_coord_size;
  1528. }
  1529. if (spvIsVulkanEnv(_.context()->target_env)) {
  1530. if (info.format == spv::ImageFormat::Unknown &&
  1531. info.dim != spv::Dim::SubpassData &&
  1532. !_.HasCapability(spv::Capability::StorageImageReadWithoutFormat)) {
  1533. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  1534. << "Capability StorageImageReadWithoutFormat is required to "
  1535. << "read storage image";
  1536. }
  1537. }
  1538. if (spv_result_t result =
  1539. ValidateImageOperands(_, inst, info, /* word_index = */ 6))
  1540. return result;
  1541. return SPV_SUCCESS;
  1542. }
  1543. spv_result_t ValidateImageWrite(ValidationState_t& _, const Instruction* inst) {
  1544. const uint32_t image_type = _.GetOperandTypeId(inst, 0);
  1545. if (_.GetIdOpcode(image_type) != spv::Op::OpTypeImage) {
  1546. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  1547. << "Expected Image to be of type OpTypeImage";
  1548. }
  1549. ImageTypeInfo info;
  1550. if (!GetImageTypeInfo(_, image_type, &info)) {
  1551. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  1552. << "Corrupt image type definition";
  1553. }
  1554. if (info.dim == spv::Dim::SubpassData) {
  1555. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  1556. << "Image 'Dim' cannot be SubpassData";
  1557. }
  1558. if (info.dim == spv::Dim::TileImageDataEXT) {
  1559. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  1560. << "Image 'Dim' cannot be TileImageDataEXT";
  1561. }
  1562. if (spv_result_t result = ValidateImageReadWrite(_, inst, info))
  1563. return result;
  1564. const uint32_t coord_type = _.GetOperandTypeId(inst, 1);
  1565. if (!_.IsIntScalarOrVectorType(coord_type)) {
  1566. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  1567. << "Expected Coordinate to be int scalar or vector";
  1568. }
  1569. const uint32_t min_coord_size = GetMinCoordSize(inst->opcode(), info);
  1570. const uint32_t actual_coord_size = _.GetDimension(coord_type);
  1571. if (min_coord_size > actual_coord_size) {
  1572. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  1573. << "Expected Coordinate to have at least " << min_coord_size
  1574. << " components, but given only " << actual_coord_size;
  1575. }
  1576. // because it needs to match with 'Sampled Type' the Texel can't be a boolean
  1577. const uint32_t texel_type = _.GetOperandTypeId(inst, 2);
  1578. if (!_.IsIntScalarOrVectorType(texel_type) &&
  1579. !_.IsFloatScalarOrVectorType(texel_type)) {
  1580. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  1581. << "Expected Texel to be int or float vector or scalar";
  1582. }
  1583. if (_.GetIdOpcode(info.sampled_type) != spv::Op::OpTypeVoid) {
  1584. const uint32_t texel_component_type = _.GetComponentType(texel_type);
  1585. if (texel_component_type != info.sampled_type) {
  1586. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  1587. << "Expected Image 'Sampled Type' to be the same as Texel "
  1588. << "components";
  1589. }
  1590. }
  1591. if (spvIsVulkanEnv(_.context()->target_env)) {
  1592. if (info.format == spv::ImageFormat::Unknown &&
  1593. info.dim != spv::Dim::SubpassData &&
  1594. !_.HasCapability(spv::Capability::StorageImageWriteWithoutFormat)) {
  1595. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  1596. << "Capability StorageImageWriteWithoutFormat is required to "
  1597. "write "
  1598. << "to storage image";
  1599. }
  1600. }
  1601. if (inst->words().size() > 4) {
  1602. if (spvIsOpenCLEnv(_.context()->target_env)) {
  1603. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  1604. << "Optional Image Operands are not allowed in the OpenCL "
  1605. << "environment.";
  1606. }
  1607. }
  1608. if (spv_result_t result =
  1609. ValidateImageOperands(_, inst, info, /* word_index = */ 5))
  1610. return result;
  1611. return SPV_SUCCESS;
  1612. }
  1613. spv_result_t ValidateImage(ValidationState_t& _, const Instruction* inst) {
  1614. const uint32_t result_type = inst->type_id();
  1615. if (_.GetIdOpcode(result_type) != spv::Op::OpTypeImage) {
  1616. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  1617. << "Expected Result Type to be OpTypeImage";
  1618. }
  1619. const uint32_t sampled_image_type = _.GetOperandTypeId(inst, 2);
  1620. const Instruction* sampled_image_type_inst = _.FindDef(sampled_image_type);
  1621. assert(sampled_image_type_inst);
  1622. if (sampled_image_type_inst->opcode() != spv::Op::OpTypeSampledImage) {
  1623. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  1624. << "Expected Sample Image to be of type OpTypeSampleImage";
  1625. }
  1626. if (sampled_image_type_inst->word(2) != result_type) {
  1627. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  1628. << "Expected Sample Image image type to be equal to Result Type";
  1629. }
  1630. return SPV_SUCCESS;
  1631. }
  1632. spv_result_t ValidateImageQuerySizeLod(ValidationState_t& _,
  1633. const Instruction* inst) {
  1634. const uint32_t result_type = inst->type_id();
  1635. if (!_.IsIntScalarOrVectorType(result_type)) {
  1636. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  1637. << "Expected Result Type to be int scalar or vector type";
  1638. }
  1639. const uint32_t image_type = _.GetOperandTypeId(inst, 2);
  1640. if (_.GetIdOpcode(image_type) != spv::Op::OpTypeImage) {
  1641. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  1642. << "Expected Image to be of type OpTypeImage";
  1643. }
  1644. ImageTypeInfo info;
  1645. if (!GetImageTypeInfo(_, image_type, &info)) {
  1646. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  1647. << "Corrupt image type definition";
  1648. }
  1649. uint32_t expected_num_components = info.arrayed;
  1650. switch (info.dim) {
  1651. case spv::Dim::Dim1D:
  1652. expected_num_components += 1;
  1653. break;
  1654. case spv::Dim::Dim2D:
  1655. case spv::Dim::Cube:
  1656. expected_num_components += 2;
  1657. break;
  1658. case spv::Dim::Dim3D:
  1659. expected_num_components += 3;
  1660. break;
  1661. default:
  1662. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  1663. << "Image 'Dim' must be 1D, 2D, 3D or Cube";
  1664. }
  1665. if (info.multisampled != 0) {
  1666. return _.diag(SPV_ERROR_INVALID_DATA, inst) << "Image 'MS' must be 0";
  1667. }
  1668. const auto target_env = _.context()->target_env;
  1669. if (spvIsVulkanEnv(target_env)) {
  1670. if (info.sampled != 1) {
  1671. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  1672. << _.VkErrorID(4659)
  1673. << "OpImageQuerySizeLod must only consume an \"Image\" operand "
  1674. "whose type has its \"Sampled\" operand set to 1";
  1675. }
  1676. }
  1677. uint32_t result_num_components = _.GetDimension(result_type);
  1678. if (result_num_components != expected_num_components) {
  1679. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  1680. << "Result Type has " << result_num_components << " components, "
  1681. << "but " << expected_num_components << " expected";
  1682. }
  1683. const uint32_t lod_type = _.GetOperandTypeId(inst, 3);
  1684. if (!_.IsIntScalarType(lod_type)) {
  1685. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  1686. << "Expected Level of Detail to be int scalar";
  1687. }
  1688. return SPV_SUCCESS;
  1689. }
  1690. spv_result_t ValidateImageQuerySize(ValidationState_t& _,
  1691. const Instruction* inst) {
  1692. const uint32_t result_type = inst->type_id();
  1693. if (!_.IsIntScalarOrVectorType(result_type)) {
  1694. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  1695. << "Expected Result Type to be int scalar or vector type";
  1696. }
  1697. const uint32_t image_type = _.GetOperandTypeId(inst, 2);
  1698. if (_.GetIdOpcode(image_type) != spv::Op::OpTypeImage) {
  1699. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  1700. << "Expected Image to be of type OpTypeImage";
  1701. }
  1702. ImageTypeInfo info;
  1703. if (!GetImageTypeInfo(_, image_type, &info)) {
  1704. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  1705. << "Corrupt image type definition";
  1706. }
  1707. uint32_t expected_num_components = info.arrayed;
  1708. switch (info.dim) {
  1709. case spv::Dim::Dim1D:
  1710. case spv::Dim::Buffer:
  1711. expected_num_components += 1;
  1712. break;
  1713. case spv::Dim::Dim2D:
  1714. case spv::Dim::Cube:
  1715. case spv::Dim::Rect:
  1716. expected_num_components += 2;
  1717. break;
  1718. case spv::Dim::Dim3D:
  1719. expected_num_components += 3;
  1720. break;
  1721. default:
  1722. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  1723. << "Image 'Dim' must be 1D, Buffer, 2D, Cube, 3D or Rect";
  1724. }
  1725. if (info.dim == spv::Dim::Dim1D || info.dim == spv::Dim::Dim2D ||
  1726. info.dim == spv::Dim::Dim3D || info.dim == spv::Dim::Cube) {
  1727. if (info.multisampled != 1 && info.sampled != 0 && info.sampled != 2) {
  1728. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  1729. << "Image must have either 'MS'=1 or 'Sampled'=0 or 'Sampled'=2";
  1730. }
  1731. }
  1732. uint32_t result_num_components = _.GetDimension(result_type);
  1733. if (result_num_components != expected_num_components) {
  1734. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  1735. << "Result Type has " << result_num_components << " components, "
  1736. << "but " << expected_num_components << " expected";
  1737. }
  1738. return SPV_SUCCESS;
  1739. }
  1740. spv_result_t ValidateImageQueryFormatOrOrder(ValidationState_t& _,
  1741. const Instruction* inst) {
  1742. if (!_.IsIntScalarType(inst->type_id())) {
  1743. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  1744. << "Expected Result Type to be int scalar type";
  1745. }
  1746. const uint32_t image_type = _.GetOperandTypeId(inst, 2);
  1747. if (_.GetIdOpcode(image_type) != spv::Op::OpTypeImage) {
  1748. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  1749. << "Expected operand to be of type OpTypeImage";
  1750. }
  1751. ImageTypeInfo info;
  1752. if (!GetImageTypeInfo(_, image_type, &info)) {
  1753. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  1754. << "Corrupt image type definition";
  1755. }
  1756. if (info.dim == spv::Dim::TileImageDataEXT) {
  1757. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  1758. << "Image 'Dim' cannot be TileImageDataEXT";
  1759. }
  1760. return SPV_SUCCESS;
  1761. }
  1762. spv_result_t ValidateImageQueryLod(ValidationState_t& _,
  1763. const Instruction* inst) {
  1764. _.function(inst->function()->id())
  1765. ->RegisterExecutionModelLimitation(
  1766. [&](spv::ExecutionModel model, std::string* message) {
  1767. if (model != spv::ExecutionModel::Fragment &&
  1768. model != spv::ExecutionModel::GLCompute &&
  1769. model != spv::ExecutionModel::MeshEXT &&
  1770. model != spv::ExecutionModel::TaskEXT) {
  1771. if (message) {
  1772. *message = std::string(
  1773. "OpImageQueryLod requires Fragment, GLCompute, MeshEXT or "
  1774. "TaskEXT execution model");
  1775. }
  1776. return false;
  1777. }
  1778. return true;
  1779. });
  1780. _.function(inst->function()->id())
  1781. ->RegisterLimitation([](const ValidationState_t& state,
  1782. const Function* entry_point,
  1783. std::string* message) {
  1784. const auto* models = state.GetExecutionModels(entry_point->id());
  1785. const auto* modes = state.GetExecutionModes(entry_point->id());
  1786. if (models &&
  1787. (models->find(spv::ExecutionModel::GLCompute) != models->end() ||
  1788. models->find(spv::ExecutionModel::MeshEXT) != models->end() ||
  1789. models->find(spv::ExecutionModel::TaskEXT) != models->end()) &&
  1790. (!modes ||
  1791. (modes->find(spv::ExecutionMode::DerivativeGroupLinearKHR) ==
  1792. modes->end() &&
  1793. modes->find(spv::ExecutionMode::DerivativeGroupQuadsKHR) ==
  1794. modes->end()))) {
  1795. if (message) {
  1796. *message = std::string(
  1797. "OpImageQueryLod requires DerivativeGroupQuadsKHR "
  1798. "or DerivativeGroupLinearKHR execution mode for GLCompute, "
  1799. "MeshEXT or TaskEXT execution model");
  1800. }
  1801. return false;
  1802. }
  1803. return true;
  1804. });
  1805. const uint32_t result_type = inst->type_id();
  1806. if (!_.IsFloatVectorType(result_type)) {
  1807. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  1808. << "Expected Result Type to be float vector type";
  1809. }
  1810. if (_.GetDimension(result_type) != 2) {
  1811. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  1812. << "Expected Result Type to have 2 components";
  1813. }
  1814. const uint32_t image_type = _.GetOperandTypeId(inst, 2);
  1815. if (_.GetIdOpcode(image_type) != spv::Op::OpTypeSampledImage) {
  1816. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  1817. << "Expected Image operand to be of type OpTypeSampledImage";
  1818. }
  1819. ImageTypeInfo info;
  1820. if (!GetImageTypeInfo(_, image_type, &info)) {
  1821. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  1822. << "Corrupt image type definition";
  1823. }
  1824. if (info.dim != spv::Dim::Dim1D && info.dim != spv::Dim::Dim2D &&
  1825. info.dim != spv::Dim::Dim3D && info.dim != spv::Dim::Cube) {
  1826. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  1827. << "Image 'Dim' must be 1D, 2D, 3D or Cube";
  1828. }
  1829. const uint32_t coord_type = _.GetOperandTypeId(inst, 3);
  1830. if (_.HasCapability(spv::Capability::Kernel)) {
  1831. if (!_.IsFloatScalarOrVectorType(coord_type) &&
  1832. !_.IsIntScalarOrVectorType(coord_type)) {
  1833. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  1834. << "Expected Coordinate to be int or float scalar or vector";
  1835. }
  1836. } else {
  1837. if (!_.IsFloatScalarOrVectorType(coord_type)) {
  1838. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  1839. << "Expected Coordinate to be float scalar or vector";
  1840. }
  1841. }
  1842. const uint32_t min_coord_size = GetPlaneCoordSize(info);
  1843. const uint32_t actual_coord_size = _.GetDimension(coord_type);
  1844. if (min_coord_size > actual_coord_size) {
  1845. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  1846. << "Expected Coordinate to have at least " << min_coord_size
  1847. << " components, but given only " << actual_coord_size;
  1848. }
  1849. // The operand is a sampled image.
  1850. // The sampled image type is already checked to be parameterized by an image
  1851. // type with Sampled=0 or Sampled=1. Vulkan bans Sampled=0, and so we have
  1852. // Sampled=1. So the validator already enforces Vulkan VUID 4659:
  1853. // OpImageQuerySizeLod must only consume an "Image" operand whose type has
  1854. // its "Sampled" operand set to 1
  1855. return SPV_SUCCESS;
  1856. }
  1857. spv_result_t ValidateImageSparseLod(ValidationState_t& _,
  1858. const Instruction* inst) {
  1859. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  1860. << "Instruction reserved for future use, use of this instruction "
  1861. << "is invalid";
  1862. }
  1863. spv_result_t ValidateImageQueryLevelsOrSamples(ValidationState_t& _,
  1864. const Instruction* inst) {
  1865. if (!_.IsIntScalarType(inst->type_id())) {
  1866. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  1867. << "Expected Result Type to be int scalar type";
  1868. }
  1869. const uint32_t image_type = _.GetOperandTypeId(inst, 2);
  1870. if (_.GetIdOpcode(image_type) != spv::Op::OpTypeImage) {
  1871. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  1872. << "Expected Image to be of type OpTypeImage";
  1873. }
  1874. ImageTypeInfo info;
  1875. if (!GetImageTypeInfo(_, image_type, &info)) {
  1876. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  1877. << "Corrupt image type definition";
  1878. }
  1879. const spv::Op opcode = inst->opcode();
  1880. if (opcode == spv::Op::OpImageQueryLevels) {
  1881. if (info.dim != spv::Dim::Dim1D && info.dim != spv::Dim::Dim2D &&
  1882. info.dim != spv::Dim::Dim3D && info.dim != spv::Dim::Cube) {
  1883. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  1884. << "Image 'Dim' must be 1D, 2D, 3D or Cube";
  1885. }
  1886. const auto target_env = _.context()->target_env;
  1887. if (spvIsVulkanEnv(target_env)) {
  1888. if (info.sampled != 1) {
  1889. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  1890. << _.VkErrorID(4659)
  1891. << "OpImageQueryLevels must only consume an \"Image\" operand "
  1892. "whose type has its \"Sampled\" operand set to 1";
  1893. }
  1894. }
  1895. } else {
  1896. assert(opcode == spv::Op::OpImageQuerySamples);
  1897. if (info.dim != spv::Dim::Dim2D) {
  1898. return _.diag(SPV_ERROR_INVALID_DATA, inst) << "Image 'Dim' must be 2D";
  1899. }
  1900. if (info.multisampled != 1) {
  1901. return _.diag(SPV_ERROR_INVALID_DATA, inst) << "Image 'MS' must be 1";
  1902. }
  1903. }
  1904. return SPV_SUCCESS;
  1905. }
  1906. spv_result_t ValidateImageSparseTexelsResident(ValidationState_t& _,
  1907. const Instruction* inst) {
  1908. if (!_.IsBoolScalarType(inst->type_id())) {
  1909. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  1910. << "Expected Result Type to be bool scalar type";
  1911. }
  1912. const uint32_t resident_code_type = _.GetOperandTypeId(inst, 2);
  1913. if (!_.IsIntScalarType(resident_code_type)) {
  1914. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  1915. << "Expected Resident Code to be int scalar";
  1916. }
  1917. return SPV_SUCCESS;
  1918. }
  1919. spv_result_t ValidateImageProcessingQCOMDecoration(ValidationState_t& _, int id,
  1920. spv::Decoration decor) {
  1921. const Instruction* si_inst = nullptr;
  1922. const Instruction* ld_inst = _.FindDef(id);
  1923. bool is_intf_obj = (ld_inst->opcode() == spv::Op::OpSampledImage);
  1924. if (is_intf_obj == true) {
  1925. si_inst = ld_inst;
  1926. int t_idx = si_inst->GetOperandAs<int>(2); // texture
  1927. ld_inst = _.FindDef(t_idx);
  1928. }
  1929. if (ld_inst->opcode() != spv::Op::OpLoad) {
  1930. return _.diag(SPV_ERROR_INVALID_DATA, ld_inst) << "Expect to see OpLoad";
  1931. }
  1932. int texture_id = ld_inst->GetOperandAs<int>(2); // variable to load
  1933. if (!_.HasDecoration(texture_id, decor)) {
  1934. return _.diag(SPV_ERROR_INVALID_DATA, ld_inst)
  1935. << "Missing decoration " << _.SpvDecorationString(decor);
  1936. }
  1937. return SPV_SUCCESS;
  1938. }
  1939. spv_result_t ValidateImageProcessing2QCOMWindowDecoration(ValidationState_t& _,
  1940. int id) {
  1941. const Instruction* ld_inst = _.FindDef(id);
  1942. bool is_intf_obj = (ld_inst->opcode() != spv::Op::OpSampledImage);
  1943. if (is_intf_obj == true) {
  1944. if (ld_inst->opcode() != spv::Op::OpLoad) {
  1945. return _.diag(SPV_ERROR_INVALID_DATA, ld_inst) << "Expect to see OpLoad";
  1946. }
  1947. int texture_id = ld_inst->GetOperandAs<int>(2); // variable to load
  1948. spv::Decoration decor = spv::Decoration::BlockMatchTextureQCOM;
  1949. if (!_.HasDecoration(texture_id, decor)) {
  1950. return _.diag(SPV_ERROR_INVALID_DATA, ld_inst)
  1951. << "Missing decoration " << _.SpvDecorationString(decor);
  1952. }
  1953. decor = spv::Decoration::BlockMatchSamplerQCOM;
  1954. if (!_.HasDecoration(texture_id, decor)) {
  1955. return _.diag(SPV_ERROR_INVALID_DATA, ld_inst)
  1956. << "Missing decoration " << _.SpvDecorationString(decor);
  1957. }
  1958. } else {
  1959. const Instruction* si_inst = ld_inst;
  1960. int t_idx = si_inst->GetOperandAs<int>(2); // texture
  1961. const Instruction* t_ld_inst = _.FindDef(t_idx);
  1962. if (t_ld_inst->opcode() != spv::Op::OpLoad) {
  1963. return _.diag(SPV_ERROR_INVALID_DATA, t_ld_inst)
  1964. << "Expect to see OpLoad";
  1965. }
  1966. int texture_id = t_ld_inst->GetOperandAs<int>(2); // variable to load
  1967. spv::Decoration decor = spv::Decoration::BlockMatchTextureQCOM;
  1968. if (!_.HasDecoration(texture_id, decor)) {
  1969. return _.diag(SPV_ERROR_INVALID_DATA, ld_inst)
  1970. << "Missing decoration " << _.SpvDecorationString(decor);
  1971. }
  1972. int s_idx = si_inst->GetOperandAs<int>(3); // sampler
  1973. const Instruction* s_ld_inst = _.FindDef(s_idx);
  1974. if (s_ld_inst->opcode() != spv::Op::OpLoad) {
  1975. return _.diag(SPV_ERROR_INVALID_DATA, s_ld_inst)
  1976. << "Expect to see OpLoad";
  1977. }
  1978. int sampler_id = s_ld_inst->GetOperandAs<int>(2); // variable to load
  1979. decor = spv::Decoration::BlockMatchSamplerQCOM;
  1980. if (!_.HasDecoration(sampler_id, decor)) {
  1981. return _.diag(SPV_ERROR_INVALID_DATA, ld_inst)
  1982. << "Missing decoration " << _.SpvDecorationString(decor);
  1983. }
  1984. }
  1985. return SPV_SUCCESS;
  1986. }
  1987. spv_result_t ValidateImageProcessingQCOM(ValidationState_t& _,
  1988. const Instruction* inst) {
  1989. spv_result_t res = SPV_SUCCESS;
  1990. const spv::Op opcode = inst->opcode();
  1991. switch (opcode) {
  1992. case spv::Op::OpImageSampleWeightedQCOM: {
  1993. int wi_idx = inst->GetOperandAs<int>(4); // weight
  1994. res = ValidateImageProcessingQCOMDecoration(
  1995. _, wi_idx, spv::Decoration::WeightTextureQCOM);
  1996. break;
  1997. }
  1998. case spv::Op::OpImageBlockMatchSSDQCOM:
  1999. case spv::Op::OpImageBlockMatchSADQCOM: {
  2000. int tgt_idx = inst->GetOperandAs<int>(2); // target
  2001. res = ValidateImageProcessingQCOMDecoration(
  2002. _, tgt_idx, spv::Decoration::BlockMatchTextureQCOM);
  2003. if (res != SPV_SUCCESS) break;
  2004. int ref_idx = inst->GetOperandAs<int>(4); // reference
  2005. res = ValidateImageProcessingQCOMDecoration(
  2006. _, ref_idx, spv::Decoration::BlockMatchTextureQCOM);
  2007. break;
  2008. }
  2009. case spv::Op::OpImageBlockMatchWindowSSDQCOM:
  2010. case spv::Op::OpImageBlockMatchWindowSADQCOM: {
  2011. int tgt_idx = inst->GetOperandAs<int>(2); // target
  2012. res = ValidateImageProcessing2QCOMWindowDecoration(_, tgt_idx);
  2013. if (res != SPV_SUCCESS) break;
  2014. int ref_idx = inst->GetOperandAs<int>(4); // reference
  2015. res = ValidateImageProcessing2QCOMWindowDecoration(_, ref_idx);
  2016. break;
  2017. }
  2018. case spv::Op::OpImageBlockMatchGatherSSDQCOM:
  2019. case spv::Op::OpImageBlockMatchGatherSADQCOM: {
  2020. int tgt_idx = inst->GetOperandAs<int>(2); // target
  2021. res = ValidateImageProcessingQCOMDecoration(
  2022. _, tgt_idx, spv::Decoration::BlockMatchTextureQCOM);
  2023. if (res != SPV_SUCCESS) break;
  2024. int ref_idx = inst->GetOperandAs<int>(4); // reference
  2025. res = ValidateImageProcessingQCOMDecoration(
  2026. _, ref_idx, spv::Decoration::BlockMatchTextureQCOM);
  2027. break;
  2028. }
  2029. default:
  2030. break;
  2031. }
  2032. return res;
  2033. }
  2034. } // namespace
  2035. // Validates correctness of image instructions.
  2036. spv_result_t ImagePass(ValidationState_t& _, const Instruction* inst) {
  2037. const spv::Op opcode = inst->opcode();
  2038. if (IsImplicitLod(opcode)) {
  2039. _.function(inst->function()->id())
  2040. ->RegisterExecutionModelLimitation([opcode](spv::ExecutionModel model,
  2041. std::string* message) {
  2042. if (model != spv::ExecutionModel::Fragment &&
  2043. model != spv::ExecutionModel::GLCompute &&
  2044. model != spv::ExecutionModel::MeshEXT &&
  2045. model != spv::ExecutionModel::TaskEXT) {
  2046. if (message) {
  2047. *message =
  2048. std::string(
  2049. "ImplicitLod instructions require Fragment, GLCompute, "
  2050. "MeshEXT or TaskEXT execution model: ") +
  2051. spvOpcodeString(opcode);
  2052. }
  2053. return false;
  2054. }
  2055. return true;
  2056. });
  2057. _.function(inst->function()->id())
  2058. ->RegisterLimitation([opcode](const ValidationState_t& state,
  2059. const Function* entry_point,
  2060. std::string* message) {
  2061. const auto* models = state.GetExecutionModels(entry_point->id());
  2062. const auto* modes = state.GetExecutionModes(entry_point->id());
  2063. if (models &&
  2064. (models->find(spv::ExecutionModel::GLCompute) != models->end() ||
  2065. models->find(spv::ExecutionModel::MeshEXT) != models->end() ||
  2066. models->find(spv::ExecutionModel::TaskEXT) != models->end()) &&
  2067. (!modes ||
  2068. (modes->find(spv::ExecutionMode::DerivativeGroupLinearKHR) ==
  2069. modes->end() &&
  2070. modes->find(spv::ExecutionMode::DerivativeGroupQuadsKHR) ==
  2071. modes->end()))) {
  2072. if (message) {
  2073. *message = std::string(
  2074. "ImplicitLod instructions require "
  2075. "DerivativeGroupQuadsKHR "
  2076. "or DerivativeGroupLinearKHR execution mode for "
  2077. "GLCompute, "
  2078. "MeshEXT or TaskEXT execution model: ") +
  2079. spvOpcodeString(opcode);
  2080. }
  2081. return false;
  2082. }
  2083. return true;
  2084. });
  2085. }
  2086. switch (opcode) {
  2087. case spv::Op::OpTypeImage:
  2088. return ValidateTypeImage(_, inst);
  2089. case spv::Op::OpTypeSampledImage:
  2090. return ValidateTypeSampledImage(_, inst);
  2091. case spv::Op::OpSampledImage:
  2092. return ValidateSampledImage(_, inst);
  2093. case spv::Op::OpImageTexelPointer:
  2094. return ValidateImageTexelPointer(_, inst);
  2095. case spv::Op::OpImageSampleImplicitLod:
  2096. case spv::Op::OpImageSampleExplicitLod:
  2097. case spv::Op::OpImageSampleProjImplicitLod:
  2098. case spv::Op::OpImageSampleProjExplicitLod:
  2099. case spv::Op::OpImageSparseSampleImplicitLod:
  2100. case spv::Op::OpImageSparseSampleExplicitLod:
  2101. return ValidateImageLod(_, inst);
  2102. case spv::Op::OpImageSampleDrefImplicitLod:
  2103. case spv::Op::OpImageSampleDrefExplicitLod:
  2104. case spv::Op::OpImageSampleProjDrefImplicitLod:
  2105. case spv::Op::OpImageSampleProjDrefExplicitLod:
  2106. case spv::Op::OpImageSparseSampleDrefImplicitLod:
  2107. case spv::Op::OpImageSparseSampleDrefExplicitLod:
  2108. return ValidateImageDrefLod(_, inst);
  2109. case spv::Op::OpImageFetch:
  2110. case spv::Op::OpImageSparseFetch:
  2111. return ValidateImageFetch(_, inst);
  2112. case spv::Op::OpImageGather:
  2113. case spv::Op::OpImageDrefGather:
  2114. case spv::Op::OpImageSparseGather:
  2115. case spv::Op::OpImageSparseDrefGather:
  2116. return ValidateImageGather(_, inst);
  2117. case spv::Op::OpImageRead:
  2118. case spv::Op::OpImageSparseRead:
  2119. return ValidateImageRead(_, inst);
  2120. case spv::Op::OpImageWrite:
  2121. return ValidateImageWrite(_, inst);
  2122. case spv::Op::OpImage:
  2123. return ValidateImage(_, inst);
  2124. case spv::Op::OpImageQueryFormat:
  2125. case spv::Op::OpImageQueryOrder:
  2126. return ValidateImageQueryFormatOrOrder(_, inst);
  2127. case spv::Op::OpImageQuerySizeLod:
  2128. return ValidateImageQuerySizeLod(_, inst);
  2129. case spv::Op::OpImageQuerySize:
  2130. return ValidateImageQuerySize(_, inst);
  2131. case spv::Op::OpImageQueryLod:
  2132. return ValidateImageQueryLod(_, inst);
  2133. case spv::Op::OpImageQueryLevels:
  2134. case spv::Op::OpImageQuerySamples:
  2135. return ValidateImageQueryLevelsOrSamples(_, inst);
  2136. case spv::Op::OpImageSparseSampleProjImplicitLod:
  2137. case spv::Op::OpImageSparseSampleProjExplicitLod:
  2138. case spv::Op::OpImageSparseSampleProjDrefImplicitLod:
  2139. case spv::Op::OpImageSparseSampleProjDrefExplicitLod:
  2140. return ValidateImageSparseLod(_, inst);
  2141. case spv::Op::OpImageSparseTexelsResident:
  2142. return ValidateImageSparseTexelsResident(_, inst);
  2143. case spv::Op::OpImageSampleWeightedQCOM:
  2144. case spv::Op::OpImageBoxFilterQCOM:
  2145. case spv::Op::OpImageBlockMatchSSDQCOM:
  2146. case spv::Op::OpImageBlockMatchSADQCOM:
  2147. case spv::Op::OpImageBlockMatchWindowSADQCOM:
  2148. case spv::Op::OpImageBlockMatchWindowSSDQCOM:
  2149. case spv::Op::OpImageBlockMatchGatherSADQCOM:
  2150. case spv::Op::OpImageBlockMatchGatherSSDQCOM:
  2151. return ValidateImageProcessingQCOM(_, inst);
  2152. default:
  2153. break;
  2154. }
  2155. return SPV_SUCCESS;
  2156. }
  2157. bool IsImageInstruction(const spv::Op opcode) {
  2158. switch (opcode) {
  2159. case spv::Op::OpImageSampleImplicitLod:
  2160. case spv::Op::OpImageSampleDrefImplicitLod:
  2161. case spv::Op::OpImageSampleProjImplicitLod:
  2162. case spv::Op::OpImageSampleProjDrefImplicitLod:
  2163. case spv::Op::OpImageSparseSampleImplicitLod:
  2164. case spv::Op::OpImageSparseSampleDrefImplicitLod:
  2165. case spv::Op::OpImageSparseSampleProjImplicitLod:
  2166. case spv::Op::OpImageSparseSampleProjDrefImplicitLod:
  2167. case spv::Op::OpImageSampleExplicitLod:
  2168. case spv::Op::OpImageSampleDrefExplicitLod:
  2169. case spv::Op::OpImageSampleProjExplicitLod:
  2170. case spv::Op::OpImageSampleProjDrefExplicitLod:
  2171. case spv::Op::OpImageSparseSampleExplicitLod:
  2172. case spv::Op::OpImageSparseSampleDrefExplicitLod:
  2173. case spv::Op::OpImageSparseSampleProjExplicitLod:
  2174. case spv::Op::OpImageSparseSampleProjDrefExplicitLod:
  2175. case spv::Op::OpImage:
  2176. case spv::Op::OpImageFetch:
  2177. case spv::Op::OpImageSparseFetch:
  2178. case spv::Op::OpImageGather:
  2179. case spv::Op::OpImageDrefGather:
  2180. case spv::Op::OpImageSparseGather:
  2181. case spv::Op::OpImageSparseDrefGather:
  2182. case spv::Op::OpImageRead:
  2183. case spv::Op::OpImageSparseRead:
  2184. case spv::Op::OpImageWrite:
  2185. case spv::Op::OpImageQueryFormat:
  2186. case spv::Op::OpImageQueryOrder:
  2187. case spv::Op::OpImageQuerySizeLod:
  2188. case spv::Op::OpImageQuerySize:
  2189. case spv::Op::OpImageQueryLod:
  2190. case spv::Op::OpImageQueryLevels:
  2191. case spv::Op::OpImageQuerySamples:
  2192. case spv::Op::OpImageSampleWeightedQCOM:
  2193. case spv::Op::OpImageBoxFilterQCOM:
  2194. case spv::Op::OpImageBlockMatchSSDQCOM:
  2195. case spv::Op::OpImageBlockMatchSADQCOM:
  2196. case spv::Op::OpImageBlockMatchWindowSADQCOM:
  2197. case spv::Op::OpImageBlockMatchWindowSSDQCOM:
  2198. case spv::Op::OpImageBlockMatchGatherSADQCOM:
  2199. case spv::Op::OpImageBlockMatchGatherSSDQCOM:
  2200. return true;
  2201. default:
  2202. break;
  2203. }
  2204. return false;
  2205. }
  2206. spv_result_t ValidateQCOMImageProcessingTextureUsages(ValidationState_t& _,
  2207. const Instruction* inst) {
  2208. const spv::Op opcode = inst->opcode();
  2209. if (!IsImageInstruction(opcode)) return SPV_SUCCESS;
  2210. switch (opcode) {
  2211. case spv::Op::OpImageSampleWeightedQCOM:
  2212. case spv::Op::OpImageBoxFilterQCOM:
  2213. case spv::Op::OpImageBlockMatchSSDQCOM:
  2214. case spv::Op::OpImageBlockMatchSADQCOM:
  2215. break;
  2216. case spv::Op::OpImageBlockMatchWindowSADQCOM:
  2217. case spv::Op::OpImageBlockMatchWindowSSDQCOM:
  2218. case spv::Op::OpImageBlockMatchGatherSADQCOM:
  2219. case spv::Op::OpImageBlockMatchGatherSSDQCOM:
  2220. break;
  2221. default:
  2222. for (size_t i = 0; i < inst->operands().size(); ++i) {
  2223. int id = inst->GetOperandAs<int>(i);
  2224. const Instruction* operand_inst = _.FindDef(id);
  2225. if (operand_inst == nullptr) continue;
  2226. if (operand_inst->opcode() == spv::Op::OpLoad) {
  2227. if (_.IsQCOMImageProcessingTextureConsumer(id)) {
  2228. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  2229. << "Illegal use of QCOM image processing decorated texture";
  2230. }
  2231. }
  2232. if (operand_inst->opcode() == spv::Op::OpSampledImage) {
  2233. if (_.IsQCOMImageProcessingTextureConsumer(id)) {
  2234. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  2235. << "Illegal use of QCOM image processing decorated texture";
  2236. }
  2237. }
  2238. }
  2239. break;
  2240. }
  2241. return SPV_SUCCESS;
  2242. }
  2243. } // namespace val
  2244. } // namespace spvtools