validate_image.cpp 87 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415
  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. if (opcode != spv::Op::OpImageGather &&
  404. opcode != spv::Op::OpImageDrefGather &&
  405. opcode != spv::Op::OpImageSparseGather &&
  406. opcode != spv::Op::OpImageSparseDrefGather) {
  407. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  408. << _.VkErrorID(4663)
  409. << "Image Operand Offset can only be used with "
  410. "OpImage*Gather operations";
  411. }
  412. }
  413. }
  414. if (mask & uint32_t(spv::ImageOperandsMask::ConstOffsets)) {
  415. if (opcode != spv::Op::OpImageGather &&
  416. opcode != spv::Op::OpImageDrefGather &&
  417. opcode != spv::Op::OpImageSparseGather &&
  418. opcode != spv::Op::OpImageSparseDrefGather) {
  419. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  420. << "Image Operand ConstOffsets can only be used with "
  421. "OpImageGather and OpImageDrefGather";
  422. }
  423. if (info.dim == spv::Dim::Cube) {
  424. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  425. << "Image Operand ConstOffsets cannot be used with Cube Image "
  426. "'Dim'";
  427. }
  428. const uint32_t id = inst->word(word_index++);
  429. const uint32_t type_id = _.GetTypeId(id);
  430. const Instruction* type_inst = _.FindDef(type_id);
  431. assert(type_inst);
  432. if (type_inst->opcode() != spv::Op::OpTypeArray) {
  433. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  434. << "Expected Image Operand ConstOffsets to be an array of size 4";
  435. }
  436. uint64_t array_size = 0;
  437. if (!_.GetConstantValUint64(type_inst->word(3), &array_size)) {
  438. assert(0 && "Array type definition is corrupt");
  439. }
  440. if (array_size != 4) {
  441. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  442. << "Expected Image Operand ConstOffsets to be an array of size 4";
  443. }
  444. const uint32_t component_type = type_inst->word(2);
  445. if (!_.IsIntVectorType(component_type) ||
  446. _.GetDimension(component_type) != 2) {
  447. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  448. << "Expected Image Operand ConstOffsets array components to be "
  449. "int vectors of size 2";
  450. }
  451. if (!spvOpcodeIsConstant(_.GetIdOpcode(id))) {
  452. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  453. << "Expected Image Operand ConstOffsets to be a const object";
  454. }
  455. }
  456. if (mask & uint32_t(spv::ImageOperandsMask::Sample)) {
  457. if (opcode != spv::Op::OpImageFetch && opcode != spv::Op::OpImageRead &&
  458. opcode != spv::Op::OpImageWrite &&
  459. opcode != spv::Op::OpImageSparseFetch &&
  460. opcode != spv::Op::OpImageSparseRead) {
  461. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  462. << "Image Operand Sample can only be used with OpImageFetch, "
  463. << "OpImageRead, OpImageWrite, OpImageSparseFetch and "
  464. << "OpImageSparseRead";
  465. }
  466. if (info.multisampled == 0) {
  467. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  468. << "Image Operand Sample requires non-zero 'MS' parameter";
  469. }
  470. const uint32_t type_id = _.GetTypeId(inst->word(word_index++));
  471. if (!_.IsIntScalarType(type_id)) {
  472. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  473. << "Expected Image Operand Sample to be int scalar";
  474. }
  475. }
  476. if (mask & uint32_t(spv::ImageOperandsMask::MinLod)) {
  477. if (!is_implicit_lod && !(mask & uint32_t(spv::ImageOperandsMask::Grad))) {
  478. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  479. << "Image Operand MinLod can only be used with ImplicitLod "
  480. << "opcodes or together with Image Operand Grad";
  481. }
  482. const uint32_t type_id = _.GetTypeId(inst->word(word_index++));
  483. if (!_.IsFloatScalarType(type_id)) {
  484. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  485. << "Expected Image Operand MinLod to be float scalar";
  486. }
  487. if (info.dim != spv::Dim::Dim1D && info.dim != spv::Dim::Dim2D &&
  488. info.dim != spv::Dim::Dim3D && info.dim != spv::Dim::Cube) {
  489. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  490. << "Image Operand MinLod requires 'Dim' parameter to be 1D, 2D, "
  491. "3D or Cube";
  492. }
  493. if (info.multisampled != 0) {
  494. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  495. << "Image Operand MinLod requires 'MS' parameter to be 0";
  496. }
  497. }
  498. if (mask & uint32_t(spv::ImageOperandsMask::MakeTexelAvailableKHR)) {
  499. // Checked elsewhere: capability and memory model are correct.
  500. if (opcode != spv::Op::OpImageWrite) {
  501. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  502. << "Image Operand MakeTexelAvailableKHR can only be used with Op"
  503. << spvOpcodeString(spv::Op::OpImageWrite) << ": Op"
  504. << spvOpcodeString(opcode);
  505. }
  506. if (!(mask & uint32_t(spv::ImageOperandsMask::NonPrivateTexelKHR))) {
  507. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  508. << "Image Operand MakeTexelAvailableKHR requires "
  509. "NonPrivateTexelKHR is also specified: Op"
  510. << spvOpcodeString(opcode);
  511. }
  512. const auto available_scope = inst->word(word_index++);
  513. if (auto error = ValidateMemoryScope(_, inst, available_scope))
  514. return error;
  515. }
  516. if (mask & uint32_t(spv::ImageOperandsMask::MakeTexelVisibleKHR)) {
  517. // Checked elsewhere: capability and memory model are correct.
  518. if (opcode != spv::Op::OpImageRead &&
  519. opcode != spv::Op::OpImageSparseRead) {
  520. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  521. << "Image Operand MakeTexelVisibleKHR can only be used with Op"
  522. << spvOpcodeString(spv::Op::OpImageRead) << " or Op"
  523. << spvOpcodeString(spv::Op::OpImageSparseRead) << ": Op"
  524. << spvOpcodeString(opcode);
  525. }
  526. if (!(mask & uint32_t(spv::ImageOperandsMask::NonPrivateTexelKHR))) {
  527. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  528. << "Image Operand MakeTexelVisibleKHR requires NonPrivateTexelKHR "
  529. "is also specified: Op"
  530. << spvOpcodeString(opcode);
  531. }
  532. const auto visible_scope = inst->word(word_index++);
  533. if (auto error = ValidateMemoryScope(_, inst, visible_scope)) return error;
  534. }
  535. if (mask & uint32_t(spv::ImageOperandsMask::SignExtend)) {
  536. // Checked elsewhere: SPIR-V 1.4 version or later.
  537. // "The texel value is converted to the target value via sign extension.
  538. // Only valid when the texel type is a scalar or vector of integer type."
  539. //
  540. // We don't have enough information to know what the texel type is.
  541. // In OpenCL, knowledge is deferred until runtime: the image SampledType is
  542. // void, and the Format is Unknown.
  543. // In Vulkan, the texel type is only known in all cases by the pipeline
  544. // setup.
  545. }
  546. if (mask & uint32_t(spv::ImageOperandsMask::ZeroExtend)) {
  547. // Checked elsewhere: SPIR-V 1.4 version or later.
  548. // "The texel value is converted to the target value via zero extension.
  549. // Only valid when the texel type is a scalar or vector of integer type."
  550. //
  551. // We don't have enough information to know what the texel type is.
  552. // In OpenCL, knowledge is deferred until runtime: the image SampledType is
  553. // void, and the Format is Unknown.
  554. // In Vulkan, the texel type is only known in all cases by the pipeline
  555. // setup.
  556. }
  557. if (mask & uint32_t(spv::ImageOperandsMask::Offsets)) {
  558. // TODO: add validation
  559. }
  560. if (mask & uint32_t(spv::ImageOperandsMask::Nontemporal)) {
  561. // Checked elsewhere: SPIR-V 1.6 version or later.
  562. }
  563. return SPV_SUCCESS;
  564. }
  565. // Validate OpImage*Proj* instructions
  566. spv_result_t ValidateImageProj(ValidationState_t& _, const Instruction* inst,
  567. const ImageTypeInfo& info) {
  568. if (info.dim != spv::Dim::Dim1D && info.dim != spv::Dim::Dim2D &&
  569. info.dim != spv::Dim::Dim3D && info.dim != spv::Dim::Rect) {
  570. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  571. << "Expected Image 'Dim' parameter to be 1D, 2D, 3D or Rect";
  572. }
  573. if (info.multisampled != 0) {
  574. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  575. << "Expected Image 'MS' parameter to be 0";
  576. }
  577. if (info.arrayed != 0) {
  578. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  579. << "Expected Image 'arrayed' parameter to be 0";
  580. }
  581. return SPV_SUCCESS;
  582. }
  583. // Validate OpImage*Read and OpImage*Write instructions
  584. spv_result_t ValidateImageReadWrite(ValidationState_t& _,
  585. const Instruction* inst,
  586. const ImageTypeInfo& info) {
  587. if (info.sampled == 2) {
  588. if (info.dim == spv::Dim::Dim1D &&
  589. !_.HasCapability(spv::Capability::Image1D)) {
  590. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  591. << "Capability Image1D is required to access storage image";
  592. } else if (info.dim == spv::Dim::Rect &&
  593. !_.HasCapability(spv::Capability::ImageRect)) {
  594. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  595. << "Capability ImageRect is required to access storage image";
  596. } else if (info.dim == spv::Dim::Buffer &&
  597. !_.HasCapability(spv::Capability::ImageBuffer)) {
  598. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  599. << "Capability ImageBuffer is required to access storage image";
  600. } else if (info.dim == spv::Dim::Cube && info.arrayed == 1 &&
  601. !_.HasCapability(spv::Capability::ImageCubeArray)) {
  602. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  603. << "Capability ImageCubeArray is required to access "
  604. << "storage image";
  605. }
  606. if (info.multisampled == 1 &&
  607. !_.HasCapability(spv::Capability::ImageMSArray)) {
  608. #if 0
  609. // TODO([email protected]) The description of this rule in the spec
  610. // is unclear and Glslang doesn't declare ImageMSArray. Need to clarify
  611. // and reenable.
  612. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  613. << "Capability ImageMSArray is required to access storage "
  614. << "image";
  615. #endif
  616. }
  617. } else if (info.sampled != 0) {
  618. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  619. << "Expected Image 'Sampled' parameter to be 0 or 2";
  620. }
  621. return SPV_SUCCESS;
  622. }
  623. // Returns true if opcode is *ImageSparse*, false otherwise.
  624. bool IsSparse(spv::Op opcode) {
  625. switch (opcode) {
  626. case spv::Op::OpImageSparseSampleImplicitLod:
  627. case spv::Op::OpImageSparseSampleExplicitLod:
  628. case spv::Op::OpImageSparseSampleDrefImplicitLod:
  629. case spv::Op::OpImageSparseSampleDrefExplicitLod:
  630. case spv::Op::OpImageSparseSampleProjImplicitLod:
  631. case spv::Op::OpImageSparseSampleProjExplicitLod:
  632. case spv::Op::OpImageSparseSampleProjDrefImplicitLod:
  633. case spv::Op::OpImageSparseSampleProjDrefExplicitLod:
  634. case spv::Op::OpImageSparseFetch:
  635. case spv::Op::OpImageSparseGather:
  636. case spv::Op::OpImageSparseDrefGather:
  637. case spv::Op::OpImageSparseTexelsResident:
  638. case spv::Op::OpImageSparseRead: {
  639. return true;
  640. }
  641. default: { return false; }
  642. }
  643. return false;
  644. }
  645. // Checks sparse image opcode result type and returns the second struct member.
  646. // Returns inst.type_id for non-sparse image opcodes.
  647. // Not valid for sparse image opcodes which do not return a struct.
  648. spv_result_t GetActualResultType(ValidationState_t& _, const Instruction* inst,
  649. uint32_t* actual_result_type) {
  650. const spv::Op opcode = inst->opcode();
  651. if (IsSparse(opcode)) {
  652. const Instruction* const type_inst = _.FindDef(inst->type_id());
  653. assert(type_inst);
  654. if (!type_inst || type_inst->opcode() != spv::Op::OpTypeStruct) {
  655. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  656. << "Expected Result Type to be OpTypeStruct";
  657. }
  658. if (type_inst->words().size() != 4 ||
  659. !_.IsIntScalarType(type_inst->word(2))) {
  660. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  661. << "Expected Result Type to be a struct containing an int "
  662. "scalar and a texel";
  663. }
  664. *actual_result_type = type_inst->word(3);
  665. } else {
  666. *actual_result_type = inst->type_id();
  667. }
  668. return SPV_SUCCESS;
  669. }
  670. // Returns a string describing actual result type of an opcode.
  671. // Not valid for sparse image opcodes which do not return a struct.
  672. const char* GetActualResultTypeStr(spv::Op opcode) {
  673. if (IsSparse(opcode)) return "Result Type's second member";
  674. return "Result Type";
  675. }
  676. spv_result_t ValidateTypeImage(ValidationState_t& _, const Instruction* inst) {
  677. assert(inst->type_id() == 0);
  678. ImageTypeInfo info;
  679. if (!GetImageTypeInfo(_, inst->word(1), &info)) {
  680. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  681. << "Corrupt image type definition";
  682. }
  683. if (_.IsIntScalarType(info.sampled_type) &&
  684. (64 == _.GetBitWidth(info.sampled_type)) &&
  685. !_.HasCapability(spv::Capability::Int64ImageEXT)) {
  686. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  687. << "Capability Int64ImageEXT is required when using Sampled Type of "
  688. "64-bit int";
  689. }
  690. const auto target_env = _.context()->target_env;
  691. if (spvIsVulkanEnv(target_env)) {
  692. if ((!_.IsFloatScalarType(info.sampled_type) &&
  693. !_.IsIntScalarType(info.sampled_type)) ||
  694. ((32 != _.GetBitWidth(info.sampled_type)) &&
  695. (64 != _.GetBitWidth(info.sampled_type))) ||
  696. ((64 == _.GetBitWidth(info.sampled_type)) &&
  697. _.IsFloatScalarType(info.sampled_type))) {
  698. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  699. << _.VkErrorID(4656)
  700. << "Expected Sampled Type to be a 32-bit int, 64-bit int or "
  701. "32-bit float scalar type for Vulkan environment";
  702. }
  703. } else if (spvIsOpenCLEnv(target_env)) {
  704. if (!_.IsVoidType(info.sampled_type)) {
  705. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  706. << "Sampled Type must be OpTypeVoid in the OpenCL environment.";
  707. }
  708. } else {
  709. const spv::Op sampled_type_opcode = _.GetIdOpcode(info.sampled_type);
  710. if (sampled_type_opcode != spv::Op::OpTypeVoid &&
  711. sampled_type_opcode != spv::Op::OpTypeInt &&
  712. sampled_type_opcode != spv::Op::OpTypeFloat) {
  713. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  714. << "Expected Sampled Type to be either void or"
  715. << " numerical scalar type";
  716. }
  717. }
  718. // Universal checks on image type operands
  719. // Dim and Format and Access Qualifier are checked elsewhere.
  720. if (info.depth > 2) {
  721. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  722. << "Invalid Depth " << info.depth << " (must be 0, 1 or 2)";
  723. }
  724. if (info.arrayed > 1) {
  725. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  726. << "Invalid Arrayed " << info.arrayed << " (must be 0 or 1)";
  727. }
  728. if (info.multisampled > 1) {
  729. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  730. << "Invalid MS " << info.multisampled << " (must be 0 or 1)";
  731. }
  732. if (info.sampled > 2) {
  733. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  734. << "Invalid Sampled " << info.sampled << " (must be 0, 1 or 2)";
  735. }
  736. if (info.dim == spv::Dim::SubpassData) {
  737. if (info.sampled != 2) {
  738. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  739. << _.VkErrorID(6214) << "Dim SubpassData requires Sampled to be 2";
  740. }
  741. if (info.format != spv::ImageFormat::Unknown) {
  742. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  743. << "Dim SubpassData requires format Unknown";
  744. }
  745. } else if (info.dim == spv::Dim::TileImageDataEXT) {
  746. if (_.IsVoidType(info.sampled_type)) {
  747. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  748. << "Dim TileImageDataEXT requires Sampled Type to be not "
  749. "OpTypeVoid";
  750. }
  751. if (info.sampled != 2) {
  752. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  753. << "Dim TileImageDataEXT requires Sampled to be 2";
  754. }
  755. if (info.format != spv::ImageFormat::Unknown) {
  756. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  757. << "Dim TileImageDataEXT requires format Unknown";
  758. }
  759. if (info.depth != 0) {
  760. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  761. << "Dim TileImageDataEXT requires Depth to be 0";
  762. }
  763. if (info.arrayed != 0) {
  764. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  765. << "Dim TileImageDataEXT requires Arrayed to be 0";
  766. }
  767. } else {
  768. if (info.multisampled && (info.sampled == 2) &&
  769. !_.HasCapability(spv::Capability::StorageImageMultisample)) {
  770. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  771. << "Capability StorageImageMultisample is required when using "
  772. "multisampled storage image";
  773. }
  774. }
  775. if (spvIsOpenCLEnv(target_env)) {
  776. if ((info.arrayed == 1) && (info.dim != spv::Dim::Dim1D) &&
  777. (info.dim != spv::Dim::Dim2D)) {
  778. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  779. << "In the OpenCL environment, Arrayed may only be set to 1 "
  780. << "when Dim is either 1D or 2D.";
  781. }
  782. if (info.multisampled != 0) {
  783. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  784. << "MS must be 0 in the OpenCL environment.";
  785. }
  786. if (info.sampled != 0) {
  787. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  788. << "Sampled must be 0 in the OpenCL environment.";
  789. }
  790. if (info.access_qualifier == spv::AccessQualifier::Max) {
  791. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  792. << "In the OpenCL environment, the optional Access Qualifier"
  793. << " must be present.";
  794. }
  795. }
  796. if (spvIsVulkanEnv(target_env)) {
  797. if (info.sampled == 0) {
  798. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  799. << _.VkErrorID(4657)
  800. << "Sampled must be 1 or 2 in the Vulkan environment.";
  801. }
  802. if (info.dim == spv::Dim::SubpassData && info.arrayed != 0) {
  803. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  804. << _.VkErrorID(6214) << "Dim SubpassData requires Arrayed to be 0";
  805. }
  806. }
  807. return SPV_SUCCESS;
  808. }
  809. spv_result_t ValidateTypeSampledImage(ValidationState_t& _,
  810. const Instruction* inst) {
  811. const uint32_t image_type = inst->word(2);
  812. if (_.GetIdOpcode(image_type) != spv::Op::OpTypeImage) {
  813. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  814. << "Expected Image to be of type OpTypeImage";
  815. }
  816. ImageTypeInfo info;
  817. if (!GetImageTypeInfo(_, image_type, &info)) {
  818. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  819. << "Corrupt image type definition";
  820. }
  821. // OpenCL requires Sampled=0, checked elsewhere.
  822. // Vulkan uses the Sampled=1 case.
  823. // If Dim is TileImageDataEXT, Sampled must be 2 and this is validated
  824. // elsewhere.
  825. if ((info.sampled != 0) && (info.sampled != 1)) {
  826. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  827. << _.VkErrorID(4657)
  828. << "Sampled image type requires an image type with \"Sampled\" "
  829. "operand set to 0 or 1";
  830. }
  831. // This covers both OpTypeSampledImage and OpSampledImage.
  832. if (_.version() >= SPV_SPIRV_VERSION_WORD(1, 6) &&
  833. info.dim == spv::Dim::Buffer) {
  834. return _.diag(SPV_ERROR_INVALID_ID, inst)
  835. << "In SPIR-V 1.6 or later, sampled image dimension must not be "
  836. "Buffer";
  837. }
  838. return SPV_SUCCESS;
  839. }
  840. bool IsAllowedSampledImageOperand(spv::Op opcode, ValidationState_t& _) {
  841. switch (opcode) {
  842. case spv::Op::OpSampledImage:
  843. case spv::Op::OpImageSampleImplicitLod:
  844. case spv::Op::OpImageSampleExplicitLod:
  845. case spv::Op::OpImageSampleDrefImplicitLod:
  846. case spv::Op::OpImageSampleDrefExplicitLod:
  847. case spv::Op::OpImageSampleProjImplicitLod:
  848. case spv::Op::OpImageSampleProjExplicitLod:
  849. case spv::Op::OpImageSampleProjDrefImplicitLod:
  850. case spv::Op::OpImageSampleProjDrefExplicitLod:
  851. case spv::Op::OpImageGather:
  852. case spv::Op::OpImageDrefGather:
  853. case spv::Op::OpImage:
  854. case spv::Op::OpImageQueryLod:
  855. case spv::Op::OpImageSparseSampleImplicitLod:
  856. case spv::Op::OpImageSparseSampleExplicitLod:
  857. case spv::Op::OpImageSparseSampleDrefImplicitLod:
  858. case spv::Op::OpImageSparseSampleDrefExplicitLod:
  859. case spv::Op::OpImageSparseGather:
  860. case spv::Op::OpImageSparseDrefGather:
  861. case spv::Op::OpCopyObject:
  862. case spv::Op::OpImageSampleWeightedQCOM:
  863. case spv::Op::OpImageBoxFilterQCOM:
  864. case spv::Op::OpImageBlockMatchSSDQCOM:
  865. case spv::Op::OpImageBlockMatchSADQCOM:
  866. return true;
  867. case spv::Op::OpStore:
  868. if (_.HasCapability(spv::Capability::BindlessTextureNV)) return true;
  869. return false;
  870. default:
  871. return false;
  872. }
  873. }
  874. spv_result_t ValidateSampledImage(ValidationState_t& _,
  875. const Instruction* inst) {
  876. if (_.GetIdOpcode(inst->type_id()) != spv::Op::OpTypeSampledImage) {
  877. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  878. << "Expected Result Type to be OpTypeSampledImage.";
  879. }
  880. const uint32_t image_type = _.GetOperandTypeId(inst, 2);
  881. if (_.GetIdOpcode(image_type) != spv::Op::OpTypeImage) {
  882. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  883. << "Expected Image to be of type OpTypeImage.";
  884. }
  885. ImageTypeInfo info;
  886. if (!GetImageTypeInfo(_, image_type, &info)) {
  887. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  888. << "Corrupt image type definition";
  889. }
  890. // TODO([email protected]) Check compatibility of result type and received
  891. // image.
  892. if (spvIsVulkanEnv(_.context()->target_env)) {
  893. if (info.sampled != 1) {
  894. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  895. << _.VkErrorID(6671)
  896. << "Expected Image 'Sampled' parameter to be 1 for Vulkan "
  897. "environment.";
  898. }
  899. } else {
  900. if (info.sampled != 0 && info.sampled != 1) {
  901. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  902. << "Expected Image 'Sampled' parameter to be 0 or 1";
  903. }
  904. }
  905. if (info.dim == spv::Dim::SubpassData) {
  906. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  907. << "Expected Image 'Dim' parameter to be not SubpassData.";
  908. }
  909. if (_.GetIdOpcode(_.GetOperandTypeId(inst, 3)) != spv::Op::OpTypeSampler) {
  910. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  911. << "Expected Sampler to be of type OpTypeSampler";
  912. }
  913. // We need to validate 2 things:
  914. // * All OpSampledImage instructions must be in the same block in which their
  915. // Result <id> are consumed.
  916. // * Result <id> from OpSampledImage instructions must not appear as operands
  917. // to OpPhi instructions or OpSelect instructions, or any instructions other
  918. // than the image lookup and query instructions specified to take an operand
  919. // whose type is OpTypeSampledImage.
  920. std::vector<Instruction*> consumers = _.getSampledImageConsumers(inst->id());
  921. if (!consumers.empty()) {
  922. for (auto consumer_instr : consumers) {
  923. const auto consumer_opcode = consumer_instr->opcode();
  924. if (consumer_instr->block() != inst->block()) {
  925. return _.diag(SPV_ERROR_INVALID_ID, inst)
  926. << "All OpSampledImage instructions must be in the same block "
  927. "in "
  928. "which their Result <id> are consumed. OpSampledImage Result "
  929. "Type <id> "
  930. << _.getIdName(inst->id())
  931. << " has a consumer in a different basic "
  932. "block. The consumer instruction <id> is "
  933. << _.getIdName(consumer_instr->id()) << ".";
  934. }
  935. if (consumer_opcode == spv::Op::OpPhi ||
  936. consumer_opcode == spv::Op::OpSelect) {
  937. return _.diag(SPV_ERROR_INVALID_ID, inst)
  938. << "Result <id> from OpSampledImage instruction must not appear "
  939. "as "
  940. "operands of Op"
  941. << spvOpcodeString(static_cast<spv::Op>(consumer_opcode)) << "."
  942. << " Found result <id> " << _.getIdName(inst->id())
  943. << " as an operand of <id> " << _.getIdName(consumer_instr->id())
  944. << ".";
  945. }
  946. if (!IsAllowedSampledImageOperand(consumer_opcode, _)) {
  947. return _.diag(SPV_ERROR_INVALID_ID, inst)
  948. << "Result <id> from OpSampledImage instruction must not appear "
  949. "as operand for Op"
  950. << spvOpcodeString(static_cast<spv::Op>(consumer_opcode))
  951. << ", since it is not specified as taking an "
  952. << "OpTypeSampledImage."
  953. << " Found result <id> " << _.getIdName(inst->id())
  954. << " as an operand of <id> " << _.getIdName(consumer_instr->id())
  955. << ".";
  956. }
  957. }
  958. }
  959. const Instruction* ld_inst;
  960. {
  961. int t_idx = inst->GetOperandAs<int>(2);
  962. ld_inst = _.FindDef(t_idx);
  963. }
  964. if (ld_inst->opcode() == spv::Op::OpLoad) {
  965. int texture_id = ld_inst->GetOperandAs<int>(2); // variable to load
  966. _.RegisterQCOMImageProcessingTextureConsumer(texture_id, ld_inst, inst);
  967. }
  968. return SPV_SUCCESS;
  969. }
  970. spv_result_t ValidateImageTexelPointer(ValidationState_t& _,
  971. const Instruction* inst) {
  972. const auto result_type = _.FindDef(inst->type_id());
  973. if (result_type->opcode() != spv::Op::OpTypePointer) {
  974. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  975. << "Expected Result Type to be OpTypePointer";
  976. }
  977. const auto storage_class = result_type->GetOperandAs<spv::StorageClass>(1);
  978. if (storage_class != spv::StorageClass::Image) {
  979. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  980. << "Expected Result Type to be OpTypePointer whose Storage Class "
  981. "operand is Image";
  982. }
  983. const auto ptr_type = result_type->GetOperandAs<uint32_t>(2);
  984. const auto ptr_opcode = _.GetIdOpcode(ptr_type);
  985. if (ptr_opcode != spv::Op::OpTypeInt && ptr_opcode != spv::Op::OpTypeFloat &&
  986. ptr_opcode != spv::Op::OpTypeVoid) {
  987. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  988. << "Expected Result Type to be OpTypePointer whose Type operand "
  989. "must be a scalar numerical type or OpTypeVoid";
  990. }
  991. const auto image_ptr = _.FindDef(_.GetOperandTypeId(inst, 2));
  992. if (!image_ptr || image_ptr->opcode() != spv::Op::OpTypePointer) {
  993. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  994. << "Expected Image to be OpTypePointer";
  995. }
  996. const auto image_type = image_ptr->GetOperandAs<uint32_t>(2);
  997. if (_.GetIdOpcode(image_type) != spv::Op::OpTypeImage) {
  998. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  999. << "Expected Image to be OpTypePointer with Type OpTypeImage";
  1000. }
  1001. ImageTypeInfo info;
  1002. if (!GetImageTypeInfo(_, image_type, &info)) {
  1003. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  1004. << "Corrupt image type definition";
  1005. }
  1006. if (info.sampled_type != ptr_type) {
  1007. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  1008. << "Expected Image 'Sampled Type' to be the same as the Type "
  1009. "pointed to by Result Type";
  1010. }
  1011. if (info.dim == spv::Dim::SubpassData) {
  1012. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  1013. << "Image Dim SubpassData cannot be used with OpImageTexelPointer";
  1014. }
  1015. if (info.dim == spv::Dim::TileImageDataEXT) {
  1016. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  1017. << "Image Dim TileImageDataEXT cannot be used with "
  1018. "OpImageTexelPointer";
  1019. }
  1020. const uint32_t coord_type = _.GetOperandTypeId(inst, 3);
  1021. if (!coord_type || !_.IsIntScalarOrVectorType(coord_type)) {
  1022. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  1023. << "Expected Coordinate to be integer scalar or vector";
  1024. }
  1025. uint32_t expected_coord_size = 0;
  1026. if (info.arrayed == 0) {
  1027. expected_coord_size = GetPlaneCoordSize(info);
  1028. } else if (info.arrayed == 1) {
  1029. switch (info.dim) {
  1030. case spv::Dim::Dim1D:
  1031. expected_coord_size = 2;
  1032. break;
  1033. case spv::Dim::Cube:
  1034. case spv::Dim::Dim2D:
  1035. expected_coord_size = 3;
  1036. break;
  1037. default:
  1038. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  1039. << "Expected Image 'Dim' must be one of 1D, 2D, or Cube when "
  1040. "Arrayed is 1";
  1041. break;
  1042. }
  1043. }
  1044. const uint32_t actual_coord_size = _.GetDimension(coord_type);
  1045. if (expected_coord_size != actual_coord_size) {
  1046. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  1047. << "Expected Coordinate to have " << expected_coord_size
  1048. << " components, but given " << actual_coord_size;
  1049. }
  1050. const uint32_t sample_type = _.GetOperandTypeId(inst, 4);
  1051. if (!sample_type || !_.IsIntScalarType(sample_type)) {
  1052. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  1053. << "Expected Sample to be integer scalar";
  1054. }
  1055. if (info.multisampled == 0) {
  1056. uint64_t ms = 0;
  1057. if (!_.GetConstantValUint64(inst->GetOperandAs<uint32_t>(4), &ms) ||
  1058. ms != 0) {
  1059. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  1060. << "Expected Sample for Image with MS 0 to be a valid <id> for "
  1061. "the value 0";
  1062. }
  1063. }
  1064. if (spvIsVulkanEnv(_.context()->target_env)) {
  1065. if ((info.format != spv::ImageFormat::R64i) &&
  1066. (info.format != spv::ImageFormat::R64ui) &&
  1067. (info.format != spv::ImageFormat::R32f) &&
  1068. (info.format != spv::ImageFormat::R32i) &&
  1069. (info.format != spv::ImageFormat::R32ui)) {
  1070. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  1071. << _.VkErrorID(4658)
  1072. << "Expected the Image Format in Image to be R64i, R64ui, R32f, "
  1073. "R32i, or R32ui for Vulkan environment";
  1074. }
  1075. }
  1076. return SPV_SUCCESS;
  1077. }
  1078. spv_result_t ValidateImageLod(ValidationState_t& _, const Instruction* inst) {
  1079. const spv::Op opcode = inst->opcode();
  1080. uint32_t actual_result_type = 0;
  1081. if (spv_result_t error = GetActualResultType(_, inst, &actual_result_type)) {
  1082. return error;
  1083. }
  1084. if (!_.IsIntVectorType(actual_result_type) &&
  1085. !_.IsFloatVectorType(actual_result_type)) {
  1086. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  1087. << "Expected " << GetActualResultTypeStr(opcode)
  1088. << " to be int or float vector type";
  1089. }
  1090. if (_.GetDimension(actual_result_type) != 4) {
  1091. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  1092. << "Expected " << GetActualResultTypeStr(opcode)
  1093. << " to have 4 components";
  1094. }
  1095. const uint32_t image_type = _.GetOperandTypeId(inst, 2);
  1096. if (_.GetIdOpcode(image_type) != spv::Op::OpTypeSampledImage) {
  1097. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  1098. << "Expected Sampled Image to be of type OpTypeSampledImage";
  1099. }
  1100. ImageTypeInfo info;
  1101. if (!GetImageTypeInfo(_, image_type, &info)) {
  1102. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  1103. << "Corrupt image type definition";
  1104. }
  1105. if (IsProj(opcode)) {
  1106. if (spv_result_t result = ValidateImageProj(_, inst, info)) return result;
  1107. }
  1108. if (info.multisampled) {
  1109. // When using image operands, the Sample image operand is required if and
  1110. // only if the image is multisampled (MS=1). The Sample image operand is
  1111. // only allowed for fetch, read, and write.
  1112. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  1113. << "Sampling operation is invalid for multisample image";
  1114. }
  1115. if (_.GetIdOpcode(info.sampled_type) != spv::Op::OpTypeVoid) {
  1116. const uint32_t texel_component_type =
  1117. _.GetComponentType(actual_result_type);
  1118. if (texel_component_type != info.sampled_type) {
  1119. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  1120. << "Expected Image 'Sampled Type' to be the same as "
  1121. << GetActualResultTypeStr(opcode) << " components";
  1122. }
  1123. }
  1124. const uint32_t coord_type = _.GetOperandTypeId(inst, 3);
  1125. if ((opcode == spv::Op::OpImageSampleExplicitLod ||
  1126. opcode == spv::Op::OpImageSparseSampleExplicitLod) &&
  1127. _.HasCapability(spv::Capability::Kernel)) {
  1128. if (!_.IsFloatScalarOrVectorType(coord_type) &&
  1129. !_.IsIntScalarOrVectorType(coord_type)) {
  1130. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  1131. << "Expected Coordinate to be int or float scalar or vector";
  1132. }
  1133. } else {
  1134. if (!_.IsFloatScalarOrVectorType(coord_type)) {
  1135. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  1136. << "Expected Coordinate to be float scalar or vector";
  1137. }
  1138. }
  1139. const uint32_t min_coord_size = GetMinCoordSize(opcode, info);
  1140. const uint32_t actual_coord_size = _.GetDimension(coord_type);
  1141. if (min_coord_size > actual_coord_size) {
  1142. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  1143. << "Expected Coordinate to have at least " << min_coord_size
  1144. << " components, but given only " << actual_coord_size;
  1145. }
  1146. const uint32_t mask = inst->words().size() <= 5 ? 0 : inst->word(5);
  1147. if (mask & uint32_t(spv::ImageOperandsMask::ConstOffset)) {
  1148. if (spvIsOpenCLEnv(_.context()->target_env)) {
  1149. if (opcode == spv::Op::OpImageSampleExplicitLod) {
  1150. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  1151. << "ConstOffset image operand not allowed "
  1152. << "in the OpenCL environment.";
  1153. }
  1154. }
  1155. }
  1156. if (spv_result_t result =
  1157. ValidateImageOperands(_, inst, info, /* word_index = */ 6))
  1158. return result;
  1159. return SPV_SUCCESS;
  1160. }
  1161. // Validates anything OpImage*Dref* instruction
  1162. spv_result_t ValidateImageDref(ValidationState_t& _, const Instruction* inst,
  1163. const ImageTypeInfo& info) {
  1164. const uint32_t dref_type = _.GetOperandTypeId(inst, 4);
  1165. if (!_.IsFloatScalarType(dref_type) || _.GetBitWidth(dref_type) != 32) {
  1166. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  1167. << "Expected Dref to be of 32-bit float type";
  1168. }
  1169. if (spvIsVulkanEnv(_.context()->target_env)) {
  1170. if (info.dim == spv::Dim::Dim3D) {
  1171. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  1172. << _.VkErrorID(4777)
  1173. << "In Vulkan, OpImage*Dref* instructions must not use images "
  1174. "with a 3D Dim";
  1175. }
  1176. }
  1177. return SPV_SUCCESS;
  1178. }
  1179. spv_result_t ValidateImageDrefLod(ValidationState_t& _,
  1180. const Instruction* inst) {
  1181. const spv::Op opcode = inst->opcode();
  1182. uint32_t actual_result_type = 0;
  1183. if (spv_result_t error = GetActualResultType(_, inst, &actual_result_type)) {
  1184. return error;
  1185. }
  1186. if (!_.IsIntScalarType(actual_result_type) &&
  1187. !_.IsFloatScalarType(actual_result_type)) {
  1188. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  1189. << "Expected " << GetActualResultTypeStr(opcode)
  1190. << " to be int or float scalar type";
  1191. }
  1192. const uint32_t image_type = _.GetOperandTypeId(inst, 2);
  1193. if (_.GetIdOpcode(image_type) != spv::Op::OpTypeSampledImage) {
  1194. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  1195. << "Expected Sampled Image to be of type OpTypeSampledImage";
  1196. }
  1197. ImageTypeInfo info;
  1198. if (!GetImageTypeInfo(_, image_type, &info)) {
  1199. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  1200. << "Corrupt image type definition";
  1201. }
  1202. if (IsProj(opcode)) {
  1203. if (spv_result_t result = ValidateImageProj(_, inst, info)) return result;
  1204. }
  1205. if (info.multisampled) {
  1206. // When using image operands, the Sample image operand is required if and
  1207. // only if the image is multisampled (MS=1). The Sample image operand is
  1208. // only allowed for fetch, read, and write.
  1209. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  1210. << "Dref sampling operation is invalid for multisample image";
  1211. }
  1212. if (actual_result_type != info.sampled_type) {
  1213. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  1214. << "Expected Image 'Sampled Type' to be the same as "
  1215. << GetActualResultTypeStr(opcode);
  1216. }
  1217. const uint32_t coord_type = _.GetOperandTypeId(inst, 3);
  1218. if (!_.IsFloatScalarOrVectorType(coord_type)) {
  1219. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  1220. << "Expected Coordinate to be float scalar or vector";
  1221. }
  1222. const uint32_t min_coord_size = GetMinCoordSize(opcode, info);
  1223. const uint32_t actual_coord_size = _.GetDimension(coord_type);
  1224. if (min_coord_size > actual_coord_size) {
  1225. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  1226. << "Expected Coordinate to have at least " << min_coord_size
  1227. << " components, but given only " << actual_coord_size;
  1228. }
  1229. if (spv_result_t result = ValidateImageDref(_, inst, info)) return result;
  1230. if (spv_result_t result =
  1231. ValidateImageOperands(_, inst, info, /* word_index = */ 7))
  1232. return result;
  1233. return SPV_SUCCESS;
  1234. }
  1235. spv_result_t ValidateImageFetch(ValidationState_t& _, const Instruction* inst) {
  1236. uint32_t actual_result_type = 0;
  1237. if (spv_result_t error = GetActualResultType(_, inst, &actual_result_type)) {
  1238. return error;
  1239. }
  1240. const spv::Op opcode = inst->opcode();
  1241. if (!_.IsIntVectorType(actual_result_type) &&
  1242. !_.IsFloatVectorType(actual_result_type)) {
  1243. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  1244. << "Expected " << GetActualResultTypeStr(opcode)
  1245. << " to be int or float vector type";
  1246. }
  1247. if (_.GetDimension(actual_result_type) != 4) {
  1248. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  1249. << "Expected " << GetActualResultTypeStr(opcode)
  1250. << " to have 4 components";
  1251. }
  1252. const uint32_t image_type = _.GetOperandTypeId(inst, 2);
  1253. if (_.GetIdOpcode(image_type) != spv::Op::OpTypeImage) {
  1254. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  1255. << "Expected Image to be of type OpTypeImage";
  1256. }
  1257. ImageTypeInfo info;
  1258. if (!GetImageTypeInfo(_, image_type, &info)) {
  1259. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  1260. << "Corrupt image type definition";
  1261. }
  1262. if (_.GetIdOpcode(info.sampled_type) != spv::Op::OpTypeVoid) {
  1263. const uint32_t result_component_type =
  1264. _.GetComponentType(actual_result_type);
  1265. if (result_component_type != info.sampled_type) {
  1266. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  1267. << "Expected Image 'Sampled Type' to be the same as "
  1268. << GetActualResultTypeStr(opcode) << " components";
  1269. }
  1270. }
  1271. if (info.dim == spv::Dim::Cube) {
  1272. return _.diag(SPV_ERROR_INVALID_DATA, inst) << "Image 'Dim' cannot be Cube";
  1273. }
  1274. if (info.sampled != 1) {
  1275. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  1276. << "Expected Image 'Sampled' parameter to be 1";
  1277. }
  1278. const uint32_t coord_type = _.GetOperandTypeId(inst, 3);
  1279. if (!_.IsIntScalarOrVectorType(coord_type)) {
  1280. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  1281. << "Expected Coordinate to be int scalar or vector";
  1282. }
  1283. const uint32_t min_coord_size = GetMinCoordSize(opcode, info);
  1284. const uint32_t actual_coord_size = _.GetDimension(coord_type);
  1285. if (min_coord_size > actual_coord_size) {
  1286. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  1287. << "Expected Coordinate to have at least " << min_coord_size
  1288. << " components, but given only " << actual_coord_size;
  1289. }
  1290. if (spv_result_t result =
  1291. ValidateImageOperands(_, inst, info, /* word_index = */ 6))
  1292. return result;
  1293. return SPV_SUCCESS;
  1294. }
  1295. spv_result_t ValidateImageGather(ValidationState_t& _,
  1296. const Instruction* inst) {
  1297. uint32_t actual_result_type = 0;
  1298. if (spv_result_t error = GetActualResultType(_, inst, &actual_result_type))
  1299. return error;
  1300. const spv::Op opcode = inst->opcode();
  1301. if (!_.IsIntVectorType(actual_result_type) &&
  1302. !_.IsFloatVectorType(actual_result_type)) {
  1303. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  1304. << "Expected " << GetActualResultTypeStr(opcode)
  1305. << " to be int or float vector type";
  1306. }
  1307. if (_.GetDimension(actual_result_type) != 4) {
  1308. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  1309. << "Expected " << GetActualResultTypeStr(opcode)
  1310. << " to have 4 components";
  1311. }
  1312. const uint32_t image_type = _.GetOperandTypeId(inst, 2);
  1313. if (_.GetIdOpcode(image_type) != spv::Op::OpTypeSampledImage) {
  1314. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  1315. << "Expected Sampled Image to be of type OpTypeSampledImage";
  1316. }
  1317. ImageTypeInfo info;
  1318. if (!GetImageTypeInfo(_, image_type, &info)) {
  1319. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  1320. << "Corrupt image type definition";
  1321. }
  1322. if (info.multisampled) {
  1323. // When using image operands, the Sample image operand is required if and
  1324. // only if the image is multisampled (MS=1). The Sample image operand is
  1325. // only allowed for fetch, read, and write.
  1326. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  1327. << "Gather operation is invalid for multisample image";
  1328. }
  1329. if (opcode == spv::Op::OpImageDrefGather ||
  1330. opcode == spv::Op::OpImageSparseDrefGather ||
  1331. _.GetIdOpcode(info.sampled_type) != spv::Op::OpTypeVoid) {
  1332. const uint32_t result_component_type =
  1333. _.GetComponentType(actual_result_type);
  1334. if (result_component_type != info.sampled_type) {
  1335. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  1336. << "Expected Image 'Sampled Type' to be the same as "
  1337. << GetActualResultTypeStr(opcode) << " components";
  1338. }
  1339. }
  1340. if (info.dim != spv::Dim::Dim2D && info.dim != spv::Dim::Cube &&
  1341. info.dim != spv::Dim::Rect) {
  1342. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  1343. << _.VkErrorID(4777)
  1344. << "Expected Image 'Dim' to be 2D, Cube, or Rect";
  1345. }
  1346. const uint32_t coord_type = _.GetOperandTypeId(inst, 3);
  1347. if (!_.IsFloatScalarOrVectorType(coord_type)) {
  1348. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  1349. << "Expected Coordinate to be float scalar or vector";
  1350. }
  1351. const uint32_t min_coord_size = GetMinCoordSize(opcode, info);
  1352. const uint32_t actual_coord_size = _.GetDimension(coord_type);
  1353. if (min_coord_size > actual_coord_size) {
  1354. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  1355. << "Expected Coordinate to have at least " << min_coord_size
  1356. << " components, but given only " << actual_coord_size;
  1357. }
  1358. if (opcode == spv::Op::OpImageGather ||
  1359. opcode == spv::Op::OpImageSparseGather) {
  1360. const uint32_t component = inst->GetOperandAs<uint32_t>(4);
  1361. const uint32_t component_index_type = _.GetTypeId(component);
  1362. if (!_.IsIntScalarType(component_index_type) ||
  1363. _.GetBitWidth(component_index_type) != 32) {
  1364. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  1365. << "Expected Component to be 32-bit int scalar";
  1366. }
  1367. if (spvIsVulkanEnv(_.context()->target_env)) {
  1368. if (!spvOpcodeIsConstant(_.GetIdOpcode(component))) {
  1369. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  1370. << _.VkErrorID(4664)
  1371. << "Expected Component Operand to be a const object for Vulkan "
  1372. "environment";
  1373. }
  1374. }
  1375. } else {
  1376. assert(opcode == spv::Op::OpImageDrefGather ||
  1377. opcode == spv::Op::OpImageSparseDrefGather);
  1378. if (spv_result_t result = ValidateImageDref(_, inst, info)) return result;
  1379. }
  1380. if (spv_result_t result =
  1381. ValidateImageOperands(_, inst, info, /* word_index = */ 7))
  1382. return result;
  1383. return SPV_SUCCESS;
  1384. }
  1385. spv_result_t ValidateImageRead(ValidationState_t& _, const Instruction* inst) {
  1386. const spv::Op opcode = inst->opcode();
  1387. uint32_t actual_result_type = 0;
  1388. if (spv_result_t error = GetActualResultType(_, inst, &actual_result_type)) {
  1389. return error;
  1390. }
  1391. if (!_.IsIntScalarOrVectorType(actual_result_type) &&
  1392. !_.IsFloatScalarOrVectorType(actual_result_type)) {
  1393. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  1394. << "Expected " << GetActualResultTypeStr(opcode)
  1395. << " to be int or float scalar or vector type";
  1396. }
  1397. const auto target_env = _.context()->target_env;
  1398. // Vulkan requires the result to be a 4-element int or float
  1399. // vector.
  1400. if (spvIsVulkanEnv(target_env)) {
  1401. if (_.GetDimension(actual_result_type) != 4) {
  1402. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  1403. << _.VkErrorID(4780) << "Expected "
  1404. << GetActualResultTypeStr(opcode) << " to have 4 components";
  1405. }
  1406. } // Check OpenCL below, after we get the image info.
  1407. const uint32_t image_type = _.GetOperandTypeId(inst, 2);
  1408. if (_.GetIdOpcode(image_type) != spv::Op::OpTypeImage) {
  1409. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  1410. << "Expected Image to be of type OpTypeImage";
  1411. }
  1412. ImageTypeInfo info;
  1413. if (!GetImageTypeInfo(_, image_type, &info)) {
  1414. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  1415. << "Corrupt image type definition";
  1416. }
  1417. if (spvIsOpenCLEnv(target_env)) {
  1418. // In OpenCL, a read from a depth image returns a scalar float. In other
  1419. // cases, the result is always a 4-element vector.
  1420. // https://www.khronos.org/registry/OpenCL/specs/3.0-unified/html/OpenCL_Env.html#_data_format_for_reading_and_writing_images
  1421. // https://www.khronos.org/registry/OpenCL/specs/3.0-unified/html/OpenCL_C.html#image-read-and-write-functions
  1422. // The builtins for reading depth images are:
  1423. // float read_imagef(aQual image2d_depth_t image, int2 coord)
  1424. // float read_imagef(aQual image2d_array_depth_t image, int4 coord)
  1425. if (info.depth) {
  1426. if (!_.IsFloatScalarType(actual_result_type)) {
  1427. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  1428. << "Expected " << GetActualResultTypeStr(opcode)
  1429. << " from a depth image read to result in a scalar float value";
  1430. }
  1431. } else {
  1432. if (_.GetDimension(actual_result_type) != 4) {
  1433. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  1434. << "Expected " << GetActualResultTypeStr(opcode)
  1435. << " to have 4 components";
  1436. }
  1437. }
  1438. const uint32_t mask = inst->words().size() <= 5 ? 0 : inst->word(5);
  1439. if (mask & uint32_t(spv::ImageOperandsMask::ConstOffset)) {
  1440. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  1441. << "ConstOffset image operand not allowed "
  1442. << "in the OpenCL environment.";
  1443. }
  1444. }
  1445. if (info.dim == spv::Dim::SubpassData) {
  1446. if (opcode == spv::Op::OpImageSparseRead) {
  1447. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  1448. << "Image Dim SubpassData cannot be used with ImageSparseRead";
  1449. }
  1450. _.function(inst->function()->id())
  1451. ->RegisterExecutionModelLimitation(
  1452. spv::ExecutionModel::Fragment,
  1453. std::string("Dim SubpassData requires Fragment execution model: ") +
  1454. spvOpcodeString(opcode));
  1455. }
  1456. if (info.dim == spv::Dim::TileImageDataEXT) {
  1457. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  1458. << "Image Dim TileImageDataEXT cannot be used with "
  1459. << spvOpcodeString(opcode);
  1460. }
  1461. if (_.GetIdOpcode(info.sampled_type) != spv::Op::OpTypeVoid) {
  1462. const uint32_t result_component_type =
  1463. _.GetComponentType(actual_result_type);
  1464. if (result_component_type != info.sampled_type) {
  1465. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  1466. << "Expected Image 'Sampled Type' to be the same as "
  1467. << GetActualResultTypeStr(opcode) << " components";
  1468. }
  1469. }
  1470. if (spv_result_t result = ValidateImageReadWrite(_, inst, info))
  1471. return result;
  1472. const uint32_t coord_type = _.GetOperandTypeId(inst, 3);
  1473. if (!_.IsIntScalarOrVectorType(coord_type)) {
  1474. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  1475. << "Expected Coordinate to be int scalar or vector";
  1476. }
  1477. const uint32_t min_coord_size = GetMinCoordSize(opcode, info);
  1478. const uint32_t actual_coord_size = _.GetDimension(coord_type);
  1479. if (min_coord_size > actual_coord_size) {
  1480. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  1481. << "Expected Coordinate to have at least " << min_coord_size
  1482. << " components, but given only " << actual_coord_size;
  1483. }
  1484. if (spvIsVulkanEnv(_.context()->target_env)) {
  1485. if (info.format == spv::ImageFormat::Unknown &&
  1486. info.dim != spv::Dim::SubpassData &&
  1487. !_.HasCapability(spv::Capability::StorageImageReadWithoutFormat)) {
  1488. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  1489. << "Capability StorageImageReadWithoutFormat is required to "
  1490. << "read storage image";
  1491. }
  1492. }
  1493. if (spv_result_t result =
  1494. ValidateImageOperands(_, inst, info, /* word_index = */ 6))
  1495. return result;
  1496. return SPV_SUCCESS;
  1497. }
  1498. spv_result_t ValidateImageWrite(ValidationState_t& _, const Instruction* inst) {
  1499. const uint32_t image_type = _.GetOperandTypeId(inst, 0);
  1500. if (_.GetIdOpcode(image_type) != spv::Op::OpTypeImage) {
  1501. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  1502. << "Expected Image to be of type OpTypeImage";
  1503. }
  1504. ImageTypeInfo info;
  1505. if (!GetImageTypeInfo(_, image_type, &info)) {
  1506. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  1507. << "Corrupt image type definition";
  1508. }
  1509. if (info.dim == spv::Dim::SubpassData) {
  1510. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  1511. << "Image 'Dim' cannot be SubpassData";
  1512. }
  1513. if (info.dim == spv::Dim::TileImageDataEXT) {
  1514. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  1515. << "Image 'Dim' cannot be TileImageDataEXT";
  1516. }
  1517. if (spv_result_t result = ValidateImageReadWrite(_, inst, info))
  1518. return result;
  1519. const uint32_t coord_type = _.GetOperandTypeId(inst, 1);
  1520. if (!_.IsIntScalarOrVectorType(coord_type)) {
  1521. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  1522. << "Expected Coordinate to be int scalar or vector";
  1523. }
  1524. const uint32_t min_coord_size = GetMinCoordSize(inst->opcode(), info);
  1525. const uint32_t actual_coord_size = _.GetDimension(coord_type);
  1526. if (min_coord_size > actual_coord_size) {
  1527. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  1528. << "Expected Coordinate to have at least " << min_coord_size
  1529. << " components, but given only " << actual_coord_size;
  1530. }
  1531. // because it needs to match with 'Sampled Type' the Texel can't be a boolean
  1532. const uint32_t texel_type = _.GetOperandTypeId(inst, 2);
  1533. if (!_.IsIntScalarOrVectorType(texel_type) &&
  1534. !_.IsFloatScalarOrVectorType(texel_type)) {
  1535. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  1536. << "Expected Texel to be int or float vector or scalar";
  1537. }
  1538. if (_.GetIdOpcode(info.sampled_type) != spv::Op::OpTypeVoid) {
  1539. const uint32_t texel_component_type = _.GetComponentType(texel_type);
  1540. if (texel_component_type != info.sampled_type) {
  1541. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  1542. << "Expected Image 'Sampled Type' to be the same as Texel "
  1543. << "components";
  1544. }
  1545. }
  1546. if (spvIsVulkanEnv(_.context()->target_env)) {
  1547. if (info.format == spv::ImageFormat::Unknown &&
  1548. info.dim != spv::Dim::SubpassData &&
  1549. !_.HasCapability(spv::Capability::StorageImageWriteWithoutFormat)) {
  1550. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  1551. << "Capability StorageImageWriteWithoutFormat is required to "
  1552. "write "
  1553. << "to storage image";
  1554. }
  1555. }
  1556. if (inst->words().size() > 4) {
  1557. if (spvIsOpenCLEnv(_.context()->target_env)) {
  1558. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  1559. << "Optional Image Operands are not allowed in the OpenCL "
  1560. << "environment.";
  1561. }
  1562. }
  1563. if (spv_result_t result =
  1564. ValidateImageOperands(_, inst, info, /* word_index = */ 5))
  1565. return result;
  1566. return SPV_SUCCESS;
  1567. }
  1568. spv_result_t ValidateImage(ValidationState_t& _, const Instruction* inst) {
  1569. const uint32_t result_type = inst->type_id();
  1570. if (_.GetIdOpcode(result_type) != spv::Op::OpTypeImage) {
  1571. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  1572. << "Expected Result Type to be OpTypeImage";
  1573. }
  1574. const uint32_t sampled_image_type = _.GetOperandTypeId(inst, 2);
  1575. const Instruction* sampled_image_type_inst = _.FindDef(sampled_image_type);
  1576. assert(sampled_image_type_inst);
  1577. if (sampled_image_type_inst->opcode() != spv::Op::OpTypeSampledImage) {
  1578. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  1579. << "Expected Sample Image to be of type OpTypeSampleImage";
  1580. }
  1581. if (sampled_image_type_inst->word(2) != result_type) {
  1582. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  1583. << "Expected Sample Image image type to be equal to Result Type";
  1584. }
  1585. return SPV_SUCCESS;
  1586. }
  1587. spv_result_t ValidateImageQuerySizeLod(ValidationState_t& _,
  1588. const Instruction* inst) {
  1589. const uint32_t result_type = inst->type_id();
  1590. if (!_.IsIntScalarOrVectorType(result_type)) {
  1591. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  1592. << "Expected Result Type to be int scalar or vector type";
  1593. }
  1594. const uint32_t image_type = _.GetOperandTypeId(inst, 2);
  1595. if (_.GetIdOpcode(image_type) != spv::Op::OpTypeImage) {
  1596. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  1597. << "Expected Image to be of type OpTypeImage";
  1598. }
  1599. ImageTypeInfo info;
  1600. if (!GetImageTypeInfo(_, image_type, &info)) {
  1601. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  1602. << "Corrupt image type definition";
  1603. }
  1604. uint32_t expected_num_components = info.arrayed;
  1605. switch (info.dim) {
  1606. case spv::Dim::Dim1D:
  1607. expected_num_components += 1;
  1608. break;
  1609. case spv::Dim::Dim2D:
  1610. case spv::Dim::Cube:
  1611. expected_num_components += 2;
  1612. break;
  1613. case spv::Dim::Dim3D:
  1614. expected_num_components += 3;
  1615. break;
  1616. default:
  1617. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  1618. << "Image 'Dim' must be 1D, 2D, 3D or Cube";
  1619. }
  1620. if (info.multisampled != 0) {
  1621. return _.diag(SPV_ERROR_INVALID_DATA, inst) << "Image 'MS' must be 0";
  1622. }
  1623. const auto target_env = _.context()->target_env;
  1624. if (spvIsVulkanEnv(target_env)) {
  1625. if (info.sampled != 1) {
  1626. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  1627. << _.VkErrorID(4659)
  1628. << "OpImageQuerySizeLod must only consume an \"Image\" operand "
  1629. "whose type has its \"Sampled\" operand set to 1";
  1630. }
  1631. }
  1632. uint32_t result_num_components = _.GetDimension(result_type);
  1633. if (result_num_components != expected_num_components) {
  1634. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  1635. << "Result Type has " << result_num_components << " components, "
  1636. << "but " << expected_num_components << " expected";
  1637. }
  1638. const uint32_t lod_type = _.GetOperandTypeId(inst, 3);
  1639. if (!_.IsIntScalarType(lod_type)) {
  1640. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  1641. << "Expected Level of Detail to be int scalar";
  1642. }
  1643. return SPV_SUCCESS;
  1644. }
  1645. spv_result_t ValidateImageQuerySize(ValidationState_t& _,
  1646. const Instruction* inst) {
  1647. const uint32_t result_type = inst->type_id();
  1648. if (!_.IsIntScalarOrVectorType(result_type)) {
  1649. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  1650. << "Expected Result Type to be int scalar or vector type";
  1651. }
  1652. const uint32_t image_type = _.GetOperandTypeId(inst, 2);
  1653. if (_.GetIdOpcode(image_type) != spv::Op::OpTypeImage) {
  1654. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  1655. << "Expected Image to be of type OpTypeImage";
  1656. }
  1657. ImageTypeInfo info;
  1658. if (!GetImageTypeInfo(_, image_type, &info)) {
  1659. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  1660. << "Corrupt image type definition";
  1661. }
  1662. uint32_t expected_num_components = info.arrayed;
  1663. switch (info.dim) {
  1664. case spv::Dim::Dim1D:
  1665. case spv::Dim::Buffer:
  1666. expected_num_components += 1;
  1667. break;
  1668. case spv::Dim::Dim2D:
  1669. case spv::Dim::Cube:
  1670. case spv::Dim::Rect:
  1671. expected_num_components += 2;
  1672. break;
  1673. case spv::Dim::Dim3D:
  1674. expected_num_components += 3;
  1675. break;
  1676. default:
  1677. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  1678. << "Image 'Dim' must be 1D, Buffer, 2D, Cube, 3D or Rect";
  1679. }
  1680. if (info.dim == spv::Dim::Dim1D || info.dim == spv::Dim::Dim2D ||
  1681. info.dim == spv::Dim::Dim3D || info.dim == spv::Dim::Cube) {
  1682. if (info.multisampled != 1 && info.sampled != 0 && info.sampled != 2) {
  1683. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  1684. << "Image must have either 'MS'=1 or 'Sampled'=0 or 'Sampled'=2";
  1685. }
  1686. }
  1687. uint32_t result_num_components = _.GetDimension(result_type);
  1688. if (result_num_components != expected_num_components) {
  1689. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  1690. << "Result Type has " << result_num_components << " components, "
  1691. << "but " << expected_num_components << " expected";
  1692. }
  1693. return SPV_SUCCESS;
  1694. }
  1695. spv_result_t ValidateImageQueryFormatOrOrder(ValidationState_t& _,
  1696. const Instruction* inst) {
  1697. if (!_.IsIntScalarType(inst->type_id())) {
  1698. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  1699. << "Expected Result Type to be int scalar type";
  1700. }
  1701. const uint32_t image_type = _.GetOperandTypeId(inst, 2);
  1702. if (_.GetIdOpcode(image_type) != spv::Op::OpTypeImage) {
  1703. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  1704. << "Expected operand to be of type OpTypeImage";
  1705. }
  1706. ImageTypeInfo info;
  1707. if (!GetImageTypeInfo(_, image_type, &info)) {
  1708. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  1709. << "Corrupt image type definition";
  1710. }
  1711. if (info.dim == spv::Dim::TileImageDataEXT) {
  1712. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  1713. << "Image 'Dim' cannot be TileImageDataEXT";
  1714. }
  1715. return SPV_SUCCESS;
  1716. }
  1717. spv_result_t ValidateImageQueryLod(ValidationState_t& _,
  1718. const Instruction* inst) {
  1719. _.function(inst->function()->id())
  1720. ->RegisterExecutionModelLimitation(
  1721. [&](spv::ExecutionModel model, std::string* message) {
  1722. if (model != spv::ExecutionModel::Fragment &&
  1723. model != spv::ExecutionModel::GLCompute) {
  1724. if (message) {
  1725. *message = std::string(
  1726. "OpImageQueryLod requires Fragment or GLCompute execution "
  1727. "model");
  1728. }
  1729. return false;
  1730. }
  1731. return true;
  1732. });
  1733. _.function(inst->function()->id())
  1734. ->RegisterLimitation([](const ValidationState_t& state,
  1735. const Function* entry_point,
  1736. std::string* message) {
  1737. const auto* models = state.GetExecutionModels(entry_point->id());
  1738. const auto* modes = state.GetExecutionModes(entry_point->id());
  1739. if (models->find(spv::ExecutionModel::GLCompute) != models->end() &&
  1740. modes->find(spv::ExecutionMode::DerivativeGroupLinearNV) ==
  1741. modes->end() &&
  1742. modes->find(spv::ExecutionMode::DerivativeGroupQuadsNV) ==
  1743. modes->end()) {
  1744. if (message) {
  1745. *message = std::string(
  1746. "OpImageQueryLod requires DerivativeGroupQuadsNV "
  1747. "or DerivativeGroupLinearNV execution mode for GLCompute "
  1748. "execution model");
  1749. }
  1750. return false;
  1751. }
  1752. return true;
  1753. });
  1754. const uint32_t result_type = inst->type_id();
  1755. if (!_.IsFloatVectorType(result_type)) {
  1756. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  1757. << "Expected Result Type to be float vector type";
  1758. }
  1759. if (_.GetDimension(result_type) != 2) {
  1760. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  1761. << "Expected Result Type to have 2 components";
  1762. }
  1763. const uint32_t image_type = _.GetOperandTypeId(inst, 2);
  1764. if (_.GetIdOpcode(image_type) != spv::Op::OpTypeSampledImage) {
  1765. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  1766. << "Expected Image operand to be of type OpTypeSampledImage";
  1767. }
  1768. ImageTypeInfo info;
  1769. if (!GetImageTypeInfo(_, image_type, &info)) {
  1770. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  1771. << "Corrupt image type definition";
  1772. }
  1773. if (info.dim != spv::Dim::Dim1D && info.dim != spv::Dim::Dim2D &&
  1774. info.dim != spv::Dim::Dim3D && info.dim != spv::Dim::Cube) {
  1775. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  1776. << "Image 'Dim' must be 1D, 2D, 3D or Cube";
  1777. }
  1778. const uint32_t coord_type = _.GetOperandTypeId(inst, 3);
  1779. if (_.HasCapability(spv::Capability::Kernel)) {
  1780. if (!_.IsFloatScalarOrVectorType(coord_type) &&
  1781. !_.IsIntScalarOrVectorType(coord_type)) {
  1782. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  1783. << "Expected Coordinate to be int or float scalar or vector";
  1784. }
  1785. } else {
  1786. if (!_.IsFloatScalarOrVectorType(coord_type)) {
  1787. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  1788. << "Expected Coordinate to be float scalar or vector";
  1789. }
  1790. }
  1791. const uint32_t min_coord_size = GetPlaneCoordSize(info);
  1792. const uint32_t actual_coord_size = _.GetDimension(coord_type);
  1793. if (min_coord_size > actual_coord_size) {
  1794. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  1795. << "Expected Coordinate to have at least " << min_coord_size
  1796. << " components, but given only " << actual_coord_size;
  1797. }
  1798. // The operand is a sampled image.
  1799. // The sampled image type is already checked to be parameterized by an image
  1800. // type with Sampled=0 or Sampled=1. Vulkan bans Sampled=0, and so we have
  1801. // Sampled=1. So the validator already enforces Vulkan VUID 4659:
  1802. // OpImageQuerySizeLod must only consume an "Image" operand whose type has
  1803. // its "Sampled" operand set to 1
  1804. return SPV_SUCCESS;
  1805. }
  1806. spv_result_t ValidateImageSparseLod(ValidationState_t& _,
  1807. const Instruction* inst) {
  1808. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  1809. << "Instruction reserved for future use, use of this instruction "
  1810. << "is invalid";
  1811. }
  1812. spv_result_t ValidateImageQueryLevelsOrSamples(ValidationState_t& _,
  1813. const Instruction* inst) {
  1814. if (!_.IsIntScalarType(inst->type_id())) {
  1815. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  1816. << "Expected Result Type to be int scalar type";
  1817. }
  1818. const uint32_t image_type = _.GetOperandTypeId(inst, 2);
  1819. if (_.GetIdOpcode(image_type) != spv::Op::OpTypeImage) {
  1820. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  1821. << "Expected Image to be of type OpTypeImage";
  1822. }
  1823. ImageTypeInfo info;
  1824. if (!GetImageTypeInfo(_, image_type, &info)) {
  1825. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  1826. << "Corrupt image type definition";
  1827. }
  1828. const spv::Op opcode = inst->opcode();
  1829. if (opcode == spv::Op::OpImageQueryLevels) {
  1830. if (info.dim != spv::Dim::Dim1D && info.dim != spv::Dim::Dim2D &&
  1831. info.dim != spv::Dim::Dim3D && info.dim != spv::Dim::Cube) {
  1832. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  1833. << "Image 'Dim' must be 1D, 2D, 3D or Cube";
  1834. }
  1835. const auto target_env = _.context()->target_env;
  1836. if (spvIsVulkanEnv(target_env)) {
  1837. if (info.sampled != 1) {
  1838. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  1839. << _.VkErrorID(4659)
  1840. << "OpImageQueryLevels must only consume an \"Image\" operand "
  1841. "whose type has its \"Sampled\" operand set to 1";
  1842. }
  1843. }
  1844. } else {
  1845. assert(opcode == spv::Op::OpImageQuerySamples);
  1846. if (info.dim != spv::Dim::Dim2D) {
  1847. return _.diag(SPV_ERROR_INVALID_DATA, inst) << "Image 'Dim' must be 2D";
  1848. }
  1849. if (info.multisampled != 1) {
  1850. return _.diag(SPV_ERROR_INVALID_DATA, inst) << "Image 'MS' must be 1";
  1851. }
  1852. }
  1853. return SPV_SUCCESS;
  1854. }
  1855. spv_result_t ValidateImageSparseTexelsResident(ValidationState_t& _,
  1856. const Instruction* inst) {
  1857. if (!_.IsBoolScalarType(inst->type_id())) {
  1858. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  1859. << "Expected Result Type to be bool scalar type";
  1860. }
  1861. const uint32_t resident_code_type = _.GetOperandTypeId(inst, 2);
  1862. if (!_.IsIntScalarType(resident_code_type)) {
  1863. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  1864. << "Expected Resident Code to be int scalar";
  1865. }
  1866. return SPV_SUCCESS;
  1867. }
  1868. spv_result_t ValidateImageProcessingQCOMDecoration(ValidationState_t& _, int id,
  1869. spv::Decoration decor) {
  1870. const Instruction* si_inst = nullptr;
  1871. const Instruction* ld_inst = _.FindDef(id);
  1872. if (ld_inst->opcode() == spv::Op::OpSampledImage) {
  1873. si_inst = ld_inst;
  1874. int t_idx = si_inst->GetOperandAs<int>(2); // texture
  1875. ld_inst = _.FindDef(t_idx);
  1876. }
  1877. if (ld_inst->opcode() != spv::Op::OpLoad) {
  1878. return _.diag(SPV_ERROR_INVALID_DATA, ld_inst) << "Expect to see OpLoad";
  1879. }
  1880. int texture_id = ld_inst->GetOperandAs<int>(2); // variable to load
  1881. if (!_.HasDecoration(texture_id, decor)) {
  1882. return _.diag(SPV_ERROR_INVALID_DATA, ld_inst)
  1883. << "Missing decoration WeightTextureQCOM/BlockMatchTextureQCOM";
  1884. }
  1885. return SPV_SUCCESS;
  1886. }
  1887. spv_result_t ValidateImageProcessingQCOM(ValidationState_t& _,
  1888. const Instruction* inst) {
  1889. spv_result_t res = SPV_SUCCESS;
  1890. const spv::Op opcode = inst->opcode();
  1891. switch (opcode) {
  1892. case spv::Op::OpImageSampleWeightedQCOM: {
  1893. int wi_idx = inst->GetOperandAs<int>(4); // weight
  1894. res = ValidateImageProcessingQCOMDecoration(
  1895. _, wi_idx, spv::Decoration::WeightTextureQCOM);
  1896. break;
  1897. }
  1898. case spv::Op::OpImageBlockMatchSSDQCOM:
  1899. case spv::Op::OpImageBlockMatchSADQCOM: {
  1900. int tgt_idx = inst->GetOperandAs<int>(2); // target
  1901. res = ValidateImageProcessingQCOMDecoration(
  1902. _, tgt_idx, spv::Decoration::BlockMatchTextureQCOM);
  1903. if (res != SPV_SUCCESS) break;
  1904. int ref_idx = inst->GetOperandAs<int>(4); // reference
  1905. res = ValidateImageProcessingQCOMDecoration(
  1906. _, ref_idx, spv::Decoration::BlockMatchTextureQCOM);
  1907. break;
  1908. }
  1909. default:
  1910. break;
  1911. }
  1912. return res;
  1913. }
  1914. } // namespace
  1915. // Validates correctness of image instructions.
  1916. spv_result_t ImagePass(ValidationState_t& _, const Instruction* inst) {
  1917. const spv::Op opcode = inst->opcode();
  1918. if (IsImplicitLod(opcode)) {
  1919. _.function(inst->function()->id())
  1920. ->RegisterExecutionModelLimitation([opcode](spv::ExecutionModel model,
  1921. std::string* message) {
  1922. if (model != spv::ExecutionModel::Fragment &&
  1923. model != spv::ExecutionModel::GLCompute) {
  1924. if (message) {
  1925. *message =
  1926. std::string(
  1927. "ImplicitLod instructions require Fragment or GLCompute "
  1928. "execution model: ") +
  1929. spvOpcodeString(opcode);
  1930. }
  1931. return false;
  1932. }
  1933. return true;
  1934. });
  1935. _.function(inst->function()->id())
  1936. ->RegisterLimitation([opcode](const ValidationState_t& state,
  1937. const Function* entry_point,
  1938. std::string* message) {
  1939. const auto* models = state.GetExecutionModels(entry_point->id());
  1940. const auto* modes = state.GetExecutionModes(entry_point->id());
  1941. if (models &&
  1942. models->find(spv::ExecutionModel::GLCompute) != models->end() &&
  1943. (!modes ||
  1944. (modes->find(spv::ExecutionMode::DerivativeGroupLinearNV) ==
  1945. modes->end() &&
  1946. modes->find(spv::ExecutionMode::DerivativeGroupQuadsNV) ==
  1947. modes->end()))) {
  1948. if (message) {
  1949. *message =
  1950. std::string(
  1951. "ImplicitLod instructions require DerivativeGroupQuadsNV "
  1952. "or DerivativeGroupLinearNV execution mode for GLCompute "
  1953. "execution model: ") +
  1954. spvOpcodeString(opcode);
  1955. }
  1956. return false;
  1957. }
  1958. return true;
  1959. });
  1960. }
  1961. switch (opcode) {
  1962. case spv::Op::OpTypeImage:
  1963. return ValidateTypeImage(_, inst);
  1964. case spv::Op::OpTypeSampledImage:
  1965. return ValidateTypeSampledImage(_, inst);
  1966. case spv::Op::OpSampledImage:
  1967. return ValidateSampledImage(_, inst);
  1968. case spv::Op::OpImageTexelPointer:
  1969. return ValidateImageTexelPointer(_, inst);
  1970. case spv::Op::OpImageSampleImplicitLod:
  1971. case spv::Op::OpImageSampleExplicitLod:
  1972. case spv::Op::OpImageSampleProjImplicitLod:
  1973. case spv::Op::OpImageSampleProjExplicitLod:
  1974. case spv::Op::OpImageSparseSampleImplicitLod:
  1975. case spv::Op::OpImageSparseSampleExplicitLod:
  1976. return ValidateImageLod(_, inst);
  1977. case spv::Op::OpImageSampleDrefImplicitLod:
  1978. case spv::Op::OpImageSampleDrefExplicitLod:
  1979. case spv::Op::OpImageSampleProjDrefImplicitLod:
  1980. case spv::Op::OpImageSampleProjDrefExplicitLod:
  1981. case spv::Op::OpImageSparseSampleDrefImplicitLod:
  1982. case spv::Op::OpImageSparseSampleDrefExplicitLod:
  1983. return ValidateImageDrefLod(_, inst);
  1984. case spv::Op::OpImageFetch:
  1985. case spv::Op::OpImageSparseFetch:
  1986. return ValidateImageFetch(_, inst);
  1987. case spv::Op::OpImageGather:
  1988. case spv::Op::OpImageDrefGather:
  1989. case spv::Op::OpImageSparseGather:
  1990. case spv::Op::OpImageSparseDrefGather:
  1991. return ValidateImageGather(_, inst);
  1992. case spv::Op::OpImageRead:
  1993. case spv::Op::OpImageSparseRead:
  1994. return ValidateImageRead(_, inst);
  1995. case spv::Op::OpImageWrite:
  1996. return ValidateImageWrite(_, inst);
  1997. case spv::Op::OpImage:
  1998. return ValidateImage(_, inst);
  1999. case spv::Op::OpImageQueryFormat:
  2000. case spv::Op::OpImageQueryOrder:
  2001. return ValidateImageQueryFormatOrOrder(_, inst);
  2002. case spv::Op::OpImageQuerySizeLod:
  2003. return ValidateImageQuerySizeLod(_, inst);
  2004. case spv::Op::OpImageQuerySize:
  2005. return ValidateImageQuerySize(_, inst);
  2006. case spv::Op::OpImageQueryLod:
  2007. return ValidateImageQueryLod(_, inst);
  2008. case spv::Op::OpImageQueryLevels:
  2009. case spv::Op::OpImageQuerySamples:
  2010. return ValidateImageQueryLevelsOrSamples(_, inst);
  2011. case spv::Op::OpImageSparseSampleProjImplicitLod:
  2012. case spv::Op::OpImageSparseSampleProjExplicitLod:
  2013. case spv::Op::OpImageSparseSampleProjDrefImplicitLod:
  2014. case spv::Op::OpImageSparseSampleProjDrefExplicitLod:
  2015. return ValidateImageSparseLod(_, inst);
  2016. case spv::Op::OpImageSparseTexelsResident:
  2017. return ValidateImageSparseTexelsResident(_, inst);
  2018. case spv::Op::OpImageSampleWeightedQCOM:
  2019. case spv::Op::OpImageBoxFilterQCOM:
  2020. case spv::Op::OpImageBlockMatchSSDQCOM:
  2021. case spv::Op::OpImageBlockMatchSADQCOM:
  2022. return ValidateImageProcessingQCOM(_, inst);
  2023. default:
  2024. break;
  2025. }
  2026. return SPV_SUCCESS;
  2027. }
  2028. bool IsImageInstruction(const spv::Op opcode) {
  2029. switch (opcode) {
  2030. case spv::Op::OpImageSampleImplicitLod:
  2031. case spv::Op::OpImageSampleDrefImplicitLod:
  2032. case spv::Op::OpImageSampleProjImplicitLod:
  2033. case spv::Op::OpImageSampleProjDrefImplicitLod:
  2034. case spv::Op::OpImageSparseSampleImplicitLod:
  2035. case spv::Op::OpImageSparseSampleDrefImplicitLod:
  2036. case spv::Op::OpImageSparseSampleProjImplicitLod:
  2037. case spv::Op::OpImageSparseSampleProjDrefImplicitLod:
  2038. case spv::Op::OpImageSampleExplicitLod:
  2039. case spv::Op::OpImageSampleDrefExplicitLod:
  2040. case spv::Op::OpImageSampleProjExplicitLod:
  2041. case spv::Op::OpImageSampleProjDrefExplicitLod:
  2042. case spv::Op::OpImageSparseSampleExplicitLod:
  2043. case spv::Op::OpImageSparseSampleDrefExplicitLod:
  2044. case spv::Op::OpImageSparseSampleProjExplicitLod:
  2045. case spv::Op::OpImageSparseSampleProjDrefExplicitLod:
  2046. case spv::Op::OpImage:
  2047. case spv::Op::OpImageFetch:
  2048. case spv::Op::OpImageSparseFetch:
  2049. case spv::Op::OpImageGather:
  2050. case spv::Op::OpImageDrefGather:
  2051. case spv::Op::OpImageSparseGather:
  2052. case spv::Op::OpImageSparseDrefGather:
  2053. case spv::Op::OpImageRead:
  2054. case spv::Op::OpImageSparseRead:
  2055. case spv::Op::OpImageWrite:
  2056. case spv::Op::OpImageQueryFormat:
  2057. case spv::Op::OpImageQueryOrder:
  2058. case spv::Op::OpImageQuerySizeLod:
  2059. case spv::Op::OpImageQuerySize:
  2060. case spv::Op::OpImageQueryLod:
  2061. case spv::Op::OpImageQueryLevels:
  2062. case spv::Op::OpImageQuerySamples:
  2063. case spv::Op::OpImageSampleWeightedQCOM:
  2064. case spv::Op::OpImageBoxFilterQCOM:
  2065. case spv::Op::OpImageBlockMatchSSDQCOM:
  2066. case spv::Op::OpImageBlockMatchSADQCOM:
  2067. return true;
  2068. default:
  2069. break;
  2070. }
  2071. return false;
  2072. }
  2073. spv_result_t ValidateQCOMImageProcessingTextureUsages(ValidationState_t& _,
  2074. const Instruction* inst) {
  2075. const spv::Op opcode = inst->opcode();
  2076. if (!IsImageInstruction(opcode)) return SPV_SUCCESS;
  2077. switch (opcode) {
  2078. case spv::Op::OpImageSampleWeightedQCOM:
  2079. case spv::Op::OpImageBoxFilterQCOM:
  2080. case spv::Op::OpImageBlockMatchSSDQCOM:
  2081. case spv::Op::OpImageBlockMatchSADQCOM:
  2082. break;
  2083. default:
  2084. for (size_t i = 0; i < inst->operands().size(); ++i) {
  2085. int id = inst->GetOperandAs<int>(i);
  2086. const Instruction* operand_inst = _.FindDef(id);
  2087. if (operand_inst == nullptr) continue;
  2088. if (operand_inst->opcode() == spv::Op::OpLoad) {
  2089. if (_.IsQCOMImageProcessingTextureConsumer(id)) {
  2090. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  2091. << "Illegal use of QCOM image processing decorated texture";
  2092. }
  2093. }
  2094. if (operand_inst->opcode() == spv::Op::OpSampledImage) {
  2095. if (_.IsQCOMImageProcessingTextureConsumer(id)) {
  2096. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  2097. << "Illegal use of QCOM image processing decorated texture";
  2098. }
  2099. }
  2100. }
  2101. break;
  2102. }
  2103. return SPV_SUCCESS;
  2104. }
  2105. } // namespace val
  2106. } // namespace spvtools